-
Notifications
You must be signed in to change notification settings - Fork 4
/
green_power_controller.cpp
141 lines (115 loc) · 3.73 KB
/
green_power_controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (c) 2012-2023 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#include <QDataStream>
#include <stdint.h>
#include <deconz/green_power_controller.h>
#include <deconz/dbg_trace.h>
#include <deconz/util.h>
static deCONZ::GreenPowerController *_gpCtrl = nullptr;
static const char *gpdLqi[] = { "poor", "moderate", "high", "excellent" };
#define MAX_RECORDS 8
struct GP_Frame
{
uint32_t gpdSrcId;
uint32_t frameCounter;
uint8_t gpdCommandId;
};
static unsigned lastReceivedIter;
static GP_Frame lastReceivedGP[MAX_RECORDS];
namespace deCONZ {
class GreenPowerControllerPrivate
{
public:
bool testZgpProxy = false;
};
static int isUnknownIndication(const deCONZ::GpDataIndication &ind)
{
GP_Frame *fr;
for (unsigned i = 0; i < MAX_RECORDS; i++)
{
fr = &lastReceivedGP[i];
if (fr->gpdSrcId != ind.gpdSrcId()) continue;
if (fr->frameCounter != ind.frameCounter()) continue;
if (fr->gpdCommandId != ind.gpdCommandId()) continue;
return 0;
}
// fresh, keep record
fr = &lastReceivedGP[lastReceivedIter % MAX_RECORDS];
lastReceivedIter++;
fr->gpdSrcId = ind.gpdSrcId();
fr->frameCounter = ind.frameCounter();
fr->gpdCommandId = ind.gpdCommandId();
return 1;
}
GreenPowerController::GreenPowerController(QObject *parent) :
QObject(parent),
d_ptr(new GreenPowerControllerPrivate)
{
Q_ASSERT(_gpCtrl == 0);
_gpCtrl = this;
d_ptr->testZgpProxy = appArgumentNumeric("--zgp-proxy-test", 0) > 0;
}
GreenPowerController::~GreenPowerController()
{
delete d_ptr;
d_ptr = nullptr;
_gpCtrl = nullptr;
}
GreenPowerController *GreenPowerController::instance()
{
DBG_Assert(_gpCtrl != nullptr);
return _gpCtrl;
}
void GreenPowerController::processIncomingData(const QByteArray &data)
{
QDataStream stream(data);
stream.setByteOrder(QDataStream::LittleEndian);
deCONZ::GpDataIndication ind;
if (ind.readFromStream(stream))
{
// frames are received 3 times, forward only one
if (isUnknownIndication(ind))
{
if (DBG_IsEnabled(DBG_ZGP))
{
char hex[256];
if (data.size() < ((sizeof(hex) / 2) - 1) && DBG_HexToAscii(data.data(), data.length(), hex))
{
DBG_Printf(DBG_ZGP, "ZGP srcId: 0x%08X cmd: 0x%02X frameCounter: %u (0x%s)\n", ind.gpdSrcId(), ind.gpdCommandId(), ind.frameCounter(), hex);
}
}
if (d_ptr->testZgpProxy)
{
DBG_Printf(DBG_ZGP, "ZGP ignore message with frameCounter: %u (test proxy)\n", ind.frameCounter());
}
else
{
emit gpDataIndication(ind);
}
}
}
}
void GreenPowerController::processIncomingProxyNotification(const QByteArray &data)
{
QDataStream stream(data);
stream.setByteOrder(QDataStream::LittleEndian);
deCONZ::GpDataIndication ind;
if (ind.readFromStreamGpNotification(stream))
{
// frames are received 3 times, forward only one
if (isUnknownIndication(ind) ||
(ind.gpdCommandId() == GppCommandIdCommissioningNotification)) // forward all GPP comissioning notifications
{
DBG_Printf(DBG_ZGP, "ZGP via GPP proxy 0x%04X for GPD srcId: 0x%08X cmd: 0x%02X frameCounter: %u, GPD lqi: %s, rssi: %d\n", ind.gppShortAddress(), ind.gpdSrcId(), ind.gpdCommandId(), ind.frameCounter(), gpdLqi[ind.gppLqi()], ind.gppRssi());
emit gpDataIndication(ind);
}
}
}
} // namespace deCONZ