-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPanInterface.cpp
423 lines (355 loc) · 11.6 KB
/
PanInterface.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/**
* @file PanInterface.cpp
* @Author BeeeOn team
* @date
* @brief
*/
#include "PanInterface.h"
using namespace std;
using namespace Poco::Net;
using Poco::AutoPtr;
using Poco::FastMutex;
using Poco::File;
using Poco::Logger;
using Poco::Mutex;
using Poco::Util::IniFileConfiguration;
PanInterface::PanInterface(IOTMessage _msg, shared_ptr<Aggregator> _agg) :
msg(_msg),
agg(_agg),
log(Poco::Logger::get("Adaapp-SPI"))
{
AutoPtr<IniFileConfiguration> cfg;
try {
cfg = new IniFileConfiguration(string(MODULES_DIR)+string(MOD_PAN)+".ini");
}
catch (Poco::Exception& ex) {
log.fatal("Exception with config file reading:\n" + ex.displayText());
exit(EXIT_FAILURE);
}
msg.time = (long long int)time(0);
tt = fillDeviceTable();
log.information("Types Table loaded.");
}
void PanInterface::set_pan(shared_ptr<PanInterface> _pan) {
pan = _pan;
}
void PanInterface::sendCmd(vector<uint8_t> cmd) {
agg->sendToPANviaMQTT(cmd);
}
void PanInterface::sendCommandToPAN(Command cmd) {
if (cmd.state == "update") {
log.information( "Device " + toStringFromLongInt(cmd.euid) + " ( " + toStringFromLongHex(cmd.euid) + " ) -> Update in " + toStringFromInt(cmd.time) + "s.");
}
else if (cmd.state == "set") {
log.information( "Device " + toStringFromLongHex(cmd.euid) + "-> Set actuator value(s) command:");
setActuators(cmd);
}
else if (cmd.state == "listen") {
log.information( "Sensor pairing state command.");
setPairingMode();
}
else if (cmd.state == "clean") {
log.information("Clean command to delete sensor.");
deleteDevice(cmd.euid);
}
else if (cmd.state == "reset") {
log.information("Reset command to reset PAN coordinator to factory defaults.");
sendCmd({PAN_RESET}); // TODO search for module_id refresh time first and sent to sensor then??
}
else if (cmd.state == "error") {
log.error("Error state in command -> Nothing to send via SPI!");
}
else {
log.error("Unknown state of incomming command from server! Command state = " + cmd.state );
}
}
void PanInterface::setPairingMode() {
sendCmd({SET_JOIN_MODE, 1});
}
void PanInterface::setActuators(Command cmd) {
// Message format: TYPE, PROTO, ADDR(MSB), ADDR, ADDR, ADDR(LSB), COUNT, MODULE_ID, LENGTH, XB_DATA {, TYPE, LENGTH, XB_DATA...}
log.information("Function set actuators");
cmd.print();
vector<uint8_t> msg;
msg.push_back(SET_ACTUATORS);
msg.push_back(static_cast<uint8_t>(stoi(cmd.protocol_version) & 0x00FF));
if (cmd.state != "listen") {
msg.push_back(static_cast<uint8_t>((cmd.euid & 0xFF000000) >> 24));
msg.push_back(static_cast<uint8_t>((cmd.euid & 0xFF0000) >> 16));
msg.push_back(static_cast<uint8_t>((cmd.euid & 0xFF00) >> 8));
msg.push_back(static_cast<uint8_t>(cmd.euid & 0x00FF));
msg.push_back(cmd.values.size());
for (auto item : cmd.values) {
unsigned int module_id = item.first;
int size = 0;
msg.push_back(module_id & 0xFF);
TT_Module actuator_T;
log.information("Looking up module \"" + toStringFromHex(module_id) + "\" of device with ID \"" + toStringFromHex(cmd.device_id) + "\" in TT_Table.");
// Search for the device
auto dev = tt.find(cmd.device_id);
if (dev == tt.end()) {
log.error("Device with ID " + toStringFromHex(cmd.device_id) + " was not found in TT_Table!");
return;
}
// Search for the module
auto mod = dev->second.modules.find(module_id);
if (mod == dev->second.modules.end()) {
log.error("Module with ID " + toStringFromHex(module_id) + " was not found in TT_Table for device " + toStringFromHex(cmd.device_id) + "!");
return;
}
else {
size = mod->second.size;
actuator_T = mod->second;
}
msg.push_back(size);
float val_f = item.second;
val_f = agg->convertValue(actuator_T, val_f, true);
int val = (int)round(val_f);
for (int i = size-1; i >= 0; i--) {
uint8_t tttt = val >> (8*i);
uint8_t tmp_v = tttt & 0x0FF;
msg.push_back(tmp_v);
}
}
}
sendCmd(msg);
}
void PanInterface::deleteDevice(euid_t id) {
log.information("Delete device called with sensor_id = " + to_string(id));
vector<uint8_t> msg;
msg.push_back(UNPAIR_SENSOR);
msg.push_back(static_cast<uint8_t>((id & 0xFF000000) >> 24));
msg.push_back(static_cast<uint8_t>((id & 0xFF0000) >> 16));
msg.push_back(static_cast<uint8_t>((id & 0xFF00) >> 8));
msg.push_back(static_cast<uint8_t>(id & 0x00FF));
sendCmd(msg);
}
#define U0 1.8 // Drained batteries [V]
#define Up 3.0 // New batteries[V]
/**
* Conversion of batteries voltage to percents
* @param bat_actual Current voltage [V]
* @return Battery capacity in percents
*/
int PanInterface::convertBattery(float bat_actual) {
float bat_percent = 0;
if (bat_actual > (U0 + 0.01))
bat_percent = (bat_actual - U0) / (Up - U0) * 100;
else
bat_percent = 1;
if (bat_percent > 130.0)
log.warning("Battery level is over 130%, this can indicate error. battery level: " + to_string(bat_actual) + "V, " +to_string(bat_percent)+"%");
if (bat_percent > 100.0)
bat_percent = 100.0;
return round(bat_percent);
}
void PanInterface::msgFromPAN(uint8_t msg_type, std::vector<uint8_t> data) {
log.information("New message from PAN: type =" + toStringFromHex(msg_type) + ", message length=" + std::to_string(data.size()));
Device sensor;
log.information("Name of received command: ");
if (theBigSwitch(msg_type, log)) {
return;
}
if (msg_type == WAIT_FOR_ADAPTER) {
sendCmd({RESTORE_YES});
}
else if ((msg_type == FROM_SENSOR_MSG)) {
try {
// Iterator over bytes in the message
unsigned int pos = 0;
// data[0]
sensor.version = data.at(pos++);
// data[1..4]
sensor.euid = 0;
for(int i = 0; i < 4; i++) {
sensor.euid = (sensor.euid << 8) + data.at(pos++);
}
// data[5]
float rssi = data.at(pos++);
// data[6..7]
sensor.device_id = (data.at(pos) << 8) + data.at(pos+1);
pos += 2;
// data[8]
sensor.pairs = data.at(pos++);
// Search for respective device
TT_Device dev;
auto d_it = tt.find(sensor.device_id);
if (d_it == tt.end()) {
log.error("This device with id (" + toStringFromHex(sensor.device_id) + ") is not known!");
return;
}
dev = d_it->second;
// data[9..data.size()], pairs:
// <module id(1B), value (xB according to TT)>
log.information("Loaded device_id=" + toStringFromHex(sensor.device_id) + ", EUID=" + toStringFromLongHex(sensor.euid) + ", pairs: " + toStringFromInt(sensor.pairs));
for (int i=0; i < sensor.pairs; i++) {
int tmp_id = data.at(pos++); // 1B module_id to types_table
// Search for respective device (to module)
TT_Module module;
try {
module = getModuleFromIDs(sensor.device_id, tmp_id); // from TT
}
catch (...) {
log.error("This module_id (" + toStringFromHex(tmp_id) + ") is not known!");
return;
}
int tmp_val_orig = 0;
for (int i = 0; i < module.size; i++)
tmp_val_orig = (tmp_val_orig << 8) + data.at(pos++);
float tmp_val_float = tmp_val_orig;
msg.priority = (module.module_is_actuator ? MSG_PRIO_ACTUATOR : MSG_PRIO_SENSOR);
log.information("Loaded module_id " + toStringFromHex(tmp_id) + "(type \"" + toStringFromHex(module.module_type) + "\") with size [B]: " + std::to_string(module.size) + ". tmp_val_orig: " + toStringFromFloat(tmp_val_float));
tmp_val_float = agg->convertValue(module, tmp_val_float);
// Handle special values
// Batteries
float new_battery;
bool status = true; // validity value
switch (module.module_id) {
// FIXME module_id and device_id must be tested here
case 0x03: //this module represents battery voltage
// convert from [V] to [%]
new_battery = convertBattery(tmp_val_float);
log.information("Converted battery - orig value: "+toStringFromFloat(tmp_val_float)+"V, new: " + toStringFromFloat(new_battery) + "%.");
sensor.values.push_back({tmp_id, new_battery});
break;
default:
for (auto val_unavailable: module.unavailableValue) { // unavailable values can exists more
// convert unavailable value from table exactly the same as incoming value
if (tmp_val_orig == val_unavailable) {
tmp_val_float = 0.0;
status = false;
break;
}
}
sensor.values.push_back({tmp_id, tmp_val_float, status});
}
}
// Add RSSI if it is in module of speficied device
try {
auto t = tt.find(sensor.device_id);
for (auto mod : (*t).second.modules) {
if (mod.second.module_type == 0x09)
sensor.values.push_back({mod.second.module_id, rssi});
}
}
catch (...) { }
msg.device = sensor;
msg.state = "data";
msg.offset = 0;
msg.device.pairs = msg.device.values.size(); // It might be changed because of special values
msg.time = time(NULL);
// TODO? Send just message header if there wasn't any sensor?
if (msg.device.pairs > 0) {
agg->sendData(msg);
}
log.information("Message sent to server.");
}
catch (std::out_of_range) {
log.error("Data are in incorrect format (out_of_range exception)");
return;
}
}
}
TT_Module PanInterface::getModuleFromIDs(long int device_id, int module_id) {
auto devs_it = tt.find(device_id);
if (devs_it == tt.end()) {
log.error("PanInterface::getModuleFromIDs - Device with ID \"" + toStringFromHex(device_id) + "\" was not found in TT!");
throw("Unknow device");
}
auto mods_it = devs_it->second.modules.find(module_id);
if (mods_it == devs_it->second.modules.end()) {
log.error("PanInterface::getModuleFromIDs - Module with ID \"" + toStringFromHex(module_id) + "\" was not found in Device with ID \"" + toStringFromHex(device_id) + "\"!");
throw("Unknow device");
}
return (mods_it->second);
}
/**
* Print message type
* @param cmd Command number
* @param log We need log to write outside of namespace
* @return 0 if AdaApp knows the command, 1 otherwise
*/
int theBigSwitch(int cmd, Logger& log) {
switch (cmd) {
case (WAIT_FOR_ADAPTER) :
log.information("WAIT_FOR_ADAPTER");
break;
case(NVM_MYPANID):
log.information("NVM_MYPANID");
break;
case(NVM_CURRCHANNEL):
log.information("NVM_CURRCHANNEL");
break;
case(NVM_CONMODE):
log.information("NVM_CONMODE");
break;
case(NVM_CONTABLE):
log.information("NVM_CONTABLE");
break;
case(NVM_CONTABLEINDEX):
log.information("NVM_CONTABLEINDEX");
break;
case(NVM_OUTFRAMECOUNTER):
log.information("NVM_OUTFRAMECOUNTER");
break;
case(NVM_SHORTADDRESS):
log.information("NVM_SHORTADDRESS");
break;
case(NVM_PARENT):
log.information("NVM_PARENT");
break;
case(NVM_ROUTING):
log.information("NVM_ROUTING");
break;
case(NVM_NEIGHBOURROUTING):
log.information("NVM_NEIGHBOURROUTING");
break;
case(NVM_FAMILYTREE):
log.information("NVM_FAMILYTREE");
break;
case(NVM_ROLE):
log.information("NVM_ROLE");
break;
case(RELOAD_NETWORK):
log.information("RELOAD_NETWORK");
break;
case(FROM_SENSOR_MSG):
log.information("FROM_SENSOR_MSG");
break;
case(SET_JOIN_MODE):
log.information("SET_JOIN_MODE");
break;
case(RESET_JOIN_MODE):
log.information("RESET_JOIN_MODE");
break;
case(JOIN_RESP):
log.information("JOIN_RESP");
break;
case(JOIN_REQUEST):
log.information("JOIN_REQUEST");
break;
case(TO_SENSOR_MSG):
log.information("TO_SENSOR_MSG");
break;
case(RESTORE_YES):
log.information("RESTORE_YES");
break;
case(RESTORE_NO):
log.information("RESTORE_NO");
break;
case(SET_ACTUATORS):
log.information("SET_ACTUATORS");
break;
case(SENSOR_SLEEP):
log.information("SENSOR_SLEEP");
break;
case(UNPAIR_SENSOR):
log.information("UNPAIR_SENSOR");
break;
default:
log.information("Unknow command (" + toStringFromInt(cmd) + ")");
return 1;
break;
}
return 0;
}