1
0
This repository has been archived on 2023-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
dtux__zigbeemanager/zigateBackend.cpp

115 lines
3.0 KiB
C++
Raw Normal View History

2023-04-08 15:06:36 +02:00
#include "main.h"
#include <unistd.h>
using namespace std;
ZigateBackend::ZigateBackend()
{
bool result;
this->resultCodes.insert(0x8000, "Status");
while (1)
{
result = this->findSerialDevice();
if (result)
{
debug("Device found :" + this->serialDevicePath, INFO);
break;
}else
{
cout << "Device not found, waiting 10 seconds before retrying" << endl;
sleep(10);
}
}
//resultCodes.insert();
}
ZigateBackend::~ZigateBackend()
{
}
QByteArray ZigateBackend::checksum(QByteArray msgType, QByteArray length, QByteArray datas)
{
quint16 temp = 0;
int i;
QString str = msgType.mid(0,2);
cout << str.toStdString() << endl;
temp ^= qFromLittleEndian<quint16>(msgType[0] + msgType[1]);
temp ^= qFromLittleEndian<quint16>(msgType[2] + msgType[3]);
temp ^= qFromLittleEndian<quint16>(length[0] + length[1]);
temp ^= qFromLittleEndian<quint16>(length[2] + length[3]);
for (i=0;i<=(datas.count());i+=2)
{
temp ^= qFromLittleEndian<quint16>(datas[i] + datas[i+1]);
}
return QByteArray::number(temp, 16);
}
QByteArray ZigateBackend::transcode(QByteArray datas)
{
QByteArray msg = "";
int i;
QByteArray byte;
if (datas.count()%2 != 0)
{
return "-1";
}
for (i=0;i<datas.count();i+=2)
{
byte = datas.mid(i,2);
cout << byte.toStdString() << " => " << byte.toUInt(nullptr, 16) << endl;
if (byte.toUInt(nullptr, 16)>0x10)
{
msg += byte;
}else
{
msg.append("02");
msg.append(QByteArray::number(byte.toUInt(nullptr, 16) ^ 0x10, 16));
}
}
return msg;
}
void ZigateBackend::sendCmd(QByteArray cmd, QByteArray len, QByteArray datas)
{
QByteArray msg;
msg = QByteArray::fromHex("01");
msg += QByteArray::fromHex(transcode(cmd));
msg += QByteArray::fromHex(transcode(len));
if (!datas.isEmpty())
{
msg += QByteArray::fromHex(checksum(cmd, len, datas));
msg += QByteArray::fromHex(transcode(datas));
}else{
msg += QByteArray::fromHex(checksum(cmd, len, "0"));
}
msg += QByteArray::fromHex("03");
cout << msg.toStdString() << endl;
}
bool ZigateBackend::findSerialDevice()
{
const auto serialPortInfos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &portInfo : serialPortInfos)
{
if (portInfo.serialNumber() == "ZIGATE+")
{
this->portName = portInfo.portName();
this->serialDevicePath = portInfo.systemLocation();
this->serialDeviceDescription = portInfo.description();
this->serialDeviceManufacturer = portInfo.manufacturer();
this->serialDeviceNumber = portInfo.serialNumber();
this->serialDeviceVendor = portInfo.vendorIdentifier();
this->serialDeviceProduct = portInfo.productIdentifier();
return true;
}
}
return false;
}