LoraModuleInterface dev.

This commit is contained in:
2023-03-26 15:28:31 -04:00
parent 526f4f76d8
commit 181334f059
17 changed files with 435 additions and 31 deletions
@@ -1,4 +1,6 @@
#include "LoraModuleInterface.h"
#include <QDataStream>
CLoraModuleInterface::CLoraModuleInterface()
{
@@ -9,12 +11,21 @@ CLoraModuleInterface::CLoraModuleInterface()
mLoraModuleIFStatusTimer->setInterval(1000);
mLoraModuleIFStatusTimer->start();
connect(mLoraModuleIFStatusTimer,SIGNAL(timeout()),this,SLOT(LoraModuleStatusTimerExpired()));
mLoraDeviceCommSurrogate = new CNetworkCommIFSurrogate;
mLoraDeviceCommSurrogate->mMyNetworkAddress = 1;
mLoraDeviceCommSurrogate->mMyNetworkID = ID_MASTER;
connect(mLoraDeviceCommSurrogate,&CNetworkCommIFSurrogate::ValidFrameReady,this,&CLoraModuleInterface::LoraRemoteDeviceDataReceived);
connect(mLoraModuleIFStatusTimer,&QTimer::timeout,this,&CLoraModuleInterface::LoraModuleStatusTimerExpired);
}
CLoraModuleInterface::~CLoraModuleInterface()
{
delete mLoraModuleIFStatusTimer;
delete mLoraDeviceCommSurrogate;
}
@@ -25,8 +36,10 @@ int CLoraModuleInterface::NewFrameReceived(int DeviceID, int DeviceAddress, int
{
case LORA_IF_GET_STATUS_RESPONSE:
{
unsigned char model = Data.at(0);
qDebug("LoraModuleInterface status received : %d",model);
QDataStream Strm(Data);
Strm >> mLoraModuleStatus;
qDebug("Lora device status received");
break;
}
case LORA_IF_SEND_FRAME_RESPONSE:
@@ -36,22 +49,27 @@ int CLoraModuleInterface::NewFrameReceived(int DeviceID, int DeviceAddress, int
}
case LORA_IF_NEW_FRAME_RESPONSE: //LoraInterface received a message from the Lora end point (chalet for example). Let's decode it and send it to the destination device
{
CNetworkProtocol *mTempProtocol = new CNetworkProtocol; //Let's instanciate a temp protocol class to avoid messing with our own derived class...
int FwdDeviceID;
int FwdDeviceAddress;
int FwdMessageID;
int FwdDataSize;
QByteArray FwdData;
if(mTempProtocol->ProtocolAnalyseBufferStatically(Data,FwdDeviceID,FwdDeviceAddress,FwdMessageID,FwdDataSize,FwdData) == PROTOCOL_RET_OK_PACKET_COMPLETE)
{
mDevicePtr->NewDeviceFrameReceived(FwdDeviceID,FwdDeviceAddress,FwdMessageID,FwdDataSize,FwdData);
}
else
{
qDebug("LoraModuleInterface received an invalid packet from end Lora device in NewFrameReceived");
}
delete mTempProtocol;
//Use the surrogate to analyse the buffer. When frame is complete it will trigger a signal.
mLoraDeviceCommSurrogate->AnalyzeRxBuffer(Data);
// CNetworkProtocol *mTempProtocol = new CNetworkProtocol; //Let's instanciate a temp protocol class to avoid messing with our own derived class...
// int FwdDeviceID;
// int FwdDeviceAddress;
// int FwdMessageID;
// int FwdDataSize;
// QByteArray FwdData;
// if(mTempProtocol->ProtocolAnalyseBufferStatically(Data,FwdDeviceID,FwdDeviceAddress,FwdMessageID,FwdDataSize,FwdData) == PROTOCOL_RET_OK_PACKET_COMPLETE)
// {
// mDevicePtr->NewDeviceFrameReceived(FwdDeviceID,FwdDeviceAddress,FwdMessageID,FwdDataSize,FwdData);
// }
// else
// {
// qDebug("LoraModuleInterface received an invalid packet from end Lora device in NewFrameReceived");
// }
// delete mTempProtocol;
break;
}
case LORA_IF_GET_MODULE_CONFIG_RESPONSE:
@@ -86,6 +104,21 @@ int CLoraModuleInterface::NewFrameReceived(int DeviceID, int DeviceAddress, int
int CLoraModuleInterface::SendNetworkMessage(int DeviceID, int DeviceAddress, int MessageID, int DataSize, QByteArray *Data)
{
qDebug("LoraInterfce: New frame received from MasterCtrl class");
//Get the payload by building the Msg packet
QByteArray Payload = GetTxPacket(MessageID,0,Data->data(),DataSize,DeviceAddress,DeviceID);
//Prepend the destination lora address & channel
Payload.prepend(mLoraPreamble);
// Payload.prepend(mDestLoraAddress);
// Payload.prepend((char)(mDestLoraChannel &0x00FF));
// Payload.prepend((char)((mDestLoraAddress >>8)));
//Now get the packet to communicate with LoraInterface uC
QByteArray Packet = GetTxPacket(LORA_IF_SEND_FRAME_REQUEST,0,Payload,Payload.size(),1,ID_LORA_INTERFACE_DEVICE);
mLoraModuleIFSerialPort.write(Packet);
mLoraModuleIFSerialPort.waitForBytesWritten(500);
return RET_OK;
}
@@ -93,7 +126,7 @@ int CLoraModuleInterface::SendNetworkMessage(int DeviceID, int DeviceAddress, in
void CLoraModuleInterface::LoraModuleStatusTimerExpired()
{
QByteArray Packet = GetTxPacket(LORA_IF_GET_STATUS_REQUEST,0,0,0,1,ID_LORA_INTERFACE);
QByteArray Packet = GetTxPacket(LORA_IF_GET_STATUS_REQUEST,0,0,0,1,ID_LORA_INTERFACE_DEVICE);
mLoraModuleIFSerialPort.write(Packet);
mLoraModuleIFSerialPort.waitForBytesWritten(500);
}
@@ -130,9 +163,37 @@ int CLoraModuleInterface::SetLoraModuleInterfaceParameters(QString ComPort, qint
return RET_OK;
}
int CLoraModuleInterface::SetLoraDestinationAddress(quint16 Address, quint8 Channel)
{
mDestLoraAddress = Address;
mDestLoraChannel = Channel;
mLoraPreamble.clear();
mLoraPreamble.append(mDestLoraChannel);
mLoraPreamble.prepend((char)(mDestLoraAddress &0x00FF));
mLoraPreamble.prepend((char)((mDestLoraAddress >>8)));
return RET_OK;
}
void CLoraModuleInterface::NewLoraModuleIFDataReady()
{
QByteArray NewData = mLoraModuleIFSerialPort.readAll();
AnalyzeRxBuffer(NewData);
}
void CLoraModuleInterface::LoraRemoteDeviceDataReceived(int FwdDeviceID, int FwdDeviceAddress, int FwdMessageID, int FwdDataSize, QByteArray FwdData)
{
//Multiple devices could be connected to the interface. Find the sender
//NOTE: This needs a major rework of the inheritance scheme!
QObject *SenderObj = QObject::sender();
if(SenderObj == mLoraDeviceCommSurrogate)
{
mDevicePtr->NewDeviceFrameReceived(FwdDeviceID,FwdDeviceAddress,FwdMessageID,FwdDataSize,FwdData);
}
}
void CLoraModuleInterface::LoraRemoteDeviceReceiveError(CNetworkProtocol::ProtocolRetValues RetID)
{
}
@@ -10,6 +10,9 @@
#include <QTimer>
#include <QSerialPort>
#include <QByteArrayList>
#include "NetworkCommIFSurrogate.h"
#include "LoraModuleInterfaceData.h"
class CLoraModuleInterface : public QObject, public CNetworkProtocol, public CAbstractNetworkCommIF
{
@@ -20,13 +23,19 @@ public:
virtual ~CLoraModuleInterface();
int SetLoraModuleInterfaceParameters(QString ComPort,qint32 BaudRate);
int SetLoraDestinationAddress(quint16 Address, quint8 Channel);
CLoraModuleInterfaceStatus GetModuleIFStatus() {return mLoraModuleStatus;}
QTimer *mLoraModuleIFStatusTimer;
QSerialPort mLoraModuleIFSerialPort;
QString mLoraModuleIFComPortName;
qint32 mLoraModuleIFComPortBaudRate;
CNetworkCommIFSurrogate *mLoraDeviceCommSurrogate;
unsigned short mDestLoraAddress;
unsigned char mDestLoraChannel;
QByteArray mLoraPreamble;
CLoraModuleInterfaceStatus mLoraModuleStatus;
@@ -39,6 +48,9 @@ public:
public slots:
void LoraModuleStatusTimerExpired();
void NewLoraModuleIFDataReady();
void LoraRemoteDeviceDataReceived(int DeviceID, int DeviceAddress, int MessageID, int DataSize, QByteArray Data);
void LoraRemoteDeviceReceiveError(CNetworkProtocol::ProtocolRetValues RetID);
};
#endif // LORAMODULEINTERFACE_H
@@ -0,0 +1,73 @@
#include "LoraModuleInterfaceData.h"
CLoraModuleInterfaceStatus::CLoraModuleInterfaceStatus()
{
}
CLoraModuleInterfaceStatus& CLoraModuleInterfaceStatus::operator = (const CLoraModuleInterfaceStatus &rhs)
{
if(this == &rhs)
return *this;
mModuleModel = rhs.mModuleModel;
mModuleInternalAddress = rhs.mModuleInternalAddress;
mModuleUARTParity = rhs.mModuleUARTParity;
mModuleUARTRate = rhs.mModuleUARTRate;
mModuleAirRate = rhs.mModuleAirRate;
mModuleSubPacket = rhs.mModuleSubPacket;
mModuleRSSIEnabled = rhs.mModuleRSSIEnabled;
mModuleTxPower = rhs.mModuleTxPower;
mModuleInternalChannel = rhs.mModuleInternalChannel;
mModuleRSSIByteEnabled = rhs.mModuleRSSIByteEnabled;
mModuleTxMethod = rhs.mModuleTxMethod;
mModuleLBTEnabled = rhs.mModuleLBTEnabled;
mModuleWORCycle = rhs.mModuleWORCycle;
mModuleAmbientRSSI = rhs.mModuleAmbientRSSI;
mModuleLastRxRSSI = rhs.mModuleLastRxRSSI;
return *this;
}
QDataStream &operator<<(QDataStream &out, const CLoraModuleInterfaceStatus &source)
{
out << source.mModuleModel
<< source.mModuleInternalAddress
<< source.mModuleUARTParity
<< source.mModuleUARTRate
<< source.mModuleAirRate
<< source.mModuleSubPacket
<< source.mModuleRSSIEnabled
<< source.mModuleTxPower
<< source.mModuleInternalChannel
<< source.mModuleRSSIByteEnabled
<< source.mModuleTxMethod
<< source.mModuleLBTEnabled
<< source.mModuleWORCycle
<< source.mModuleAmbientRSSI
<< source.mModuleLastRxRSSI;
return out;
}
QDataStream &operator>>(QDataStream &in, CLoraModuleInterfaceStatus &dest)
{
in >> dest.mModuleModel
>> dest.mModuleInternalAddress
>> dest.mModuleUARTParity
>> dest.mModuleUARTRate
>> dest.mModuleAirRate
>> dest.mModuleSubPacket
>> dest.mModuleRSSIEnabled
>> dest.mModuleTxPower
>> dest.mModuleInternalChannel
>> dest.mModuleRSSIByteEnabled
>> dest.mModuleTxMethod
>> dest.mModuleLBTEnabled
>> dest.mModuleWORCycle
>> dest.mModuleAmbientRSSI
>> dest.mModuleLastRxRSSI;
return in;
}
@@ -0,0 +1,105 @@
#ifndef LORAMODULEINTERFACEDATA_H
#define LORAMODULEINTERFACEDATA_H
#include <QtGlobal>
#include <QDataStream>
class CLoraModuleInterfaceStatus
{
public:
enum eE220UartRates
{
E220_UART_1200 = 0,
E220_UART_2400,
E220_UART_4800,
E220_UART_9600,
E220_UART_19200,
E220_UART_38400,
E220_UART_57600,
E220_UART_115200
};
enum eE220ParityBit
{
E220_UART_8N1=0,
E220_UART_8O1,
E220_UART_8E1,
E220_UART_8N1_bis
};
enum e220AirDataRates
{
E220_AIR_RATE_24K = 0,
E220_AIR_RATE_24K_1,
E220_AIR_RATE_24K_2,
E220_AIR_RATE_48K,
E220_AIR_RATE_96K,
E220_AIR_RATE_192K,
E220_AIR_RATE_384K,
E220_AIR_RATE_625K
};
enum e220PacketSizes
{
E220_PACKET_200,
E220_PACKET_128,
E220_PACKET_64,
E220_PACKET_32
};
enum e220TransmitPower
{
E220_TX_PWR_30,
E220_TX_PWR_27,
E220_TX_PWR_24,
E220_TX_PWR_21
};
enum e220WORCycles
{
E220_WOR_500MS,
E220_WOR_1000MS,
E220_WOR_1500MS,
E220_WOR_2000MS,
E220_WOR_2500MS,
E220_WOR_3000MS,
E220_WOR_3500MS,
E220_WOR_4000MS
};
CLoraModuleInterfaceStatus();
quint8 mModuleModel;
quint16 mModuleInternalAddress;
quint8 mModuleUARTParity;
quint8 mModuleUARTRate;
quint8 mModuleAirRate;
quint8 mModuleSubPacket;
quint8 mModuleRSSIEnabled;
quint8 mModuleTxPower;
quint8 mModuleInternalChannel;
quint8 mModuleRSSIByteEnabled;
quint8 mModuleTxMethod;
quint8 mModuleLBTEnabled;
quint8 mModuleWORCycle;
quint8 mModuleAmbientRSSI;
quint8 mModuleLastRxRSSI;
CLoraModuleInterfaceStatus& operator=(const CLoraModuleInterfaceStatus &rhs);
};
QDataStream &operator<<(QDataStream &out, const CLoraModuleInterfaceStatus &source);
QDataStream &operator>>(QDataStream &in, CLoraModuleInterfaceStatus &dest);
#endif // LORAMODULEINTERFACEDATA_H
@@ -0,0 +1,39 @@
#include "LoraModuleInterfaceInterface.h"
CLoraModuleInterfaceInterface::CLoraModuleInterfaceInterface(int Address, CAbstractNetworkCommIF *NetworkInterface, CLoraModuleInterface *DevicePtr):
CNetworkDevice(ID_LORA_INTERFACE_INTERFACE,Address,NetworkInterface)
{
mLoraModuleIFPtr = DevicePtr;
}
int CLoraModuleInterfaceInterface::NewDeviceFrameReceived(int DeviceID, int DeviceAddress, int MessageID, int DataSize, QByteArray Data)
{
Q_UNUSED(DeviceID)
Q_UNUSED(DeviceAddress)
Q_UNUSED(DataSize)
Q_UNUSED(Data)
switch(MessageID)
{
case LORA_MODULE_IF_INTERFACE_ACK:
{
break;
}
case LORA_MODULE_IF_INTERFACE_GENERAL_STATUS_REQUEST:
{
QByteArray StatusData;
QDataStream Strm(&StatusData,QIODevice::WriteOnly);
Strm << mLoraModuleIFPtr->GetModuleIFStatus();
mNetworkInterfacePtr->SendNetworkMessage(ID_LORA_INTERFACE_INTERFACE,mDeviceAddress,LORA_MODULE_IF_INTERFACE_GENERAL_STATUS_RESPONSE,StatusData.size(),&StatusData);
break;
}
case LORA_MODULE_IF_INTERFACE_GENERAL_STATUS_RESPONSE:
{
qDebug("CLoraModuleInterfaceInterface : Received an invalid command from remote");
break;
}
}
return RET_OK;
}
@@ -0,0 +1,22 @@
#ifndef LORAMODULEINTERFACEINTERFACE_H
#define LORAMODULEINTERFACEINTERFACE_H
#include <QObject>
#include "NetworkDevice.h"
#include "LoraModuleInterface.h"
class CLoraModuleInterfaceInterface: public QObject, public CNetworkDevice
{
Q_OBJECT
public:
explicit CLoraModuleInterfaceInterface(int Address, CAbstractNetworkCommIF *NetworkInterface, CLoraModuleInterface *DevicePtr);
virtual int NewDeviceFrameReceived(int DeviceID, int DeviceAddress, int MessageID, int DataSize, QByteArray Data);
CLoraModuleInterface *mLoraModuleIFPtr;
signals:
public slots:
};
#endif // LORAMODULEINTERFACEINTERFACE_H