-
Notifications
You must be signed in to change notification settings - Fork 2
/
MLPacketGen.h
409 lines (362 loc) · 12.7 KB
/
MLPacketGen.h
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
#ifndef MLPACKETGEN_H
#define MLPACKETGEN_H
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "MLPacketComm.h"
////////////////////////////////////////////////////////////////////////////
class MLPayloadGen {
public:
static uint32_t convertHexToDec(const uint8_t *src, int numSrc, bool littleEndian = true);
public:
MLPayloadGen(uint16_t cmdId = 0, uint8_t optionFlags = 0, uint8_t *optionData = NULL, uint8_t version = 0x0B) {
_version = version;
_cmdId = cmdId;
_optionFlags = optionFlags;
if (_optionFlags & 0x01 && optionData != NULL) {
_optionDataLen = 3;
memcpy(_optionData, optionData, _optionDataLen);
} else {
_optionDataLen = 0;
}
}
void setMLPayloadOption(uint8_t optionFlags, uint8_t* optionData) {
_optionFlags = optionFlags;
if (_optionFlags & 0x01 && optionData != NULL) {
_optionDataLen = 3;
memcpy(_optionData, optionData, _optionDataLen);
} else {
_optionDataLen = 0;
}
}
virtual void setPayload(const uint8_t *payload, int szPayload) {} // should implement for assign payload from buffer
// payload = prefix + [custom] + postfix
virtual int getPayload(uint8_t *payload) {
// prefix ... postfix
return getPayloadPrefix(payload);
}
int getPayloadPrefix(uint8_t *payload) {
// prefix
payload[0] = _version;
payload[1] = _cmdId & 0xFF;
payload[2] = _cmdId >> 8;
return 3;
}
int getPayloadPostfix(uint8_t *payload, int pos) {
if (_version >= 0x0C) // MOSTLink 1.5: ver 0x0C, no optional-info
return pos;
else { // MOSTLink 1.4: ver 0x0B, use optinal-info
// postfix
payload[pos++] = _optionFlags;
if (_optionDataLen > 0) {
memcpy(&payload[pos], _optionData, _optionDataLen);
pos += _optionDataLen;
}
return pos;
}
}
uint8_t getVersion() { return _version; }
uint16_t getCmdId() { return _cmdId; }
uint8_t getOptionFlags() { return _optionFlags; }
uint8_t* getOptionData(uint8_t& optionDataLen) {
optionDataLen = _optionDataLen;
return _optionData;
}
uint8_t getOptionDataLen() { return _optionDataLen; }
protected:
// prefix
uint8_t _version;
uint16_t _cmdId;
// postfix
uint8_t _optionFlags;
uint8_t _optionData[ML_MAX_OPTION_DATA_SIZE];
uint8_t _optionDataLen;
};
////////////////////////////////////////////////////////////////////////////
class MLReqSetLoraConfigGen : public MLPayloadGen {
public:
MLReqSetLoraConfigGen(uint8_t channelID, long freq, uint8_t dataRate, uint8_t power, uint8_t wakeupInterval, uint8_t groupId);
int getPayload(uint8_t *payload);
void setPayload(const uint8_t *payload, int szPayload);
uint32_t getFrequency() { return _frequency; }
uint8_t getDataRate() { return _dataRate; }
uint8_t getPower() { return _power; }
uint8_t getWakeupInterval() { return _wakeupInterval; }
uint8_t getGroupId() { return _groupId; }
private:
uint8_t _channelID;
uint32_t _frequency;
uint8_t _dataRate;
uint8_t _power;
uint8_t _wakeupInterval;
uint8_t _groupId;
};
class MLReqDataPayloadGen : public MLPayloadGen {
public:
MLReqDataPayloadGen(uint16_t resInterval, uint8_t dataLen, uint8_t *data);
int getPayload(uint8_t *payload);
uint16_t getResInterval() { return _resInterval; }
uint8_t getDataLen() { return _dataLen; }
uint8_t* getData(uint8_t& dataLen) {
dataLen = _dataLen;
return _data;
}
private:
uint16_t _resInterval;
uint8_t _dataLen;
uint8_t _data[ML_MAX_DATA_SIZE];
};
class MLNotifyLocationGen : public MLPayloadGen {
public:
MLNotifyLocationGen(uint32_t dateTime, mllocation location, uint8_t notifyType, uint8_t gpsStatus);
int getPayload(uint8_t *payload);
uint32_t getDataTime() { return _dateTime; }
void getLocation(mllocation& location) {
location.longtitude = _location.longtitude;
location.latitude = _location.latitude;
}
uint8_t getNotifyTYpe() { return _notifyType; }
uint8_t getGpsStatus() { return _gpsStatus; }
private:
uint32_t _dateTime;
mllocation _location;
uint8_t _notifyType;
uint8_t _gpsStatus;
};
class MLAnsSetLoraConfigGen : public MLPayloadGen {
public:
MLAnsSetLoraConfigGen(uint8_t errorCode);
int getPayload(uint8_t *payload);
uint8_t getErrorCode() { return _errorCode; }
private:
uint8_t _errorCode;
};
class MLAnsDataPayloadGen : public MLPayloadGen {
public:
MLAnsDataPayloadGen(uint8_t errorCode, uint8_t dataLen, uint8_t *data);
int getPayload(uint8_t *payload);
uint8_t getErrorCode() { return _errorCode; }
uint8_t getDataLen() { return _dataLen; }
uint8_t* getData(uint8_t& dataLen) {
dataLen = _dataLen;
return _data;
}
private:
uint8_t _errorCode;
uint8_t _dataLen;
uint8_t _data[ML_MAX_DATA_SIZE];
};
class MLRetConfigGeofGen : public MLPayloadGen {
public:
MLRetConfigGeofGen(uint16_t geofRadius, uint16_t resInterval, mllocation location);
int getPayload(uint8_t *payload);
uint16_t getGeoFRadius() { return _geofRadius; }
uint16_t getResInterval() { return _resInterval; }
void getLocation(mllocation& location) {
location.longtitude = _location.longtitude;
location.latitude = _location.latitude;
}
private:
uint16_t _geofRadius;
uint16_t _resInterval;
mllocation _location;
};
//////////////////////////////////////////////////////////////////////////////////
class MLNotifyVindunoPayloadGen : public MLPayloadGen {
public:
MLNotifyVindunoPayloadGen(uint8_t *apiKey, float soil_1, float soil_2, float soil_3, float soli_4,
float sysVoltage, float humidity, float temperature, float reserved);
int getPayload(uint8_t *payload);
uint8_t* getApiKey() { return _apiKey; }
float getSoil1() { return _soil_1; }
float getSoil2() { return _soil_2; }
float getSoil3() { return _soil_3; }
float getSoil4() { return _soil_4; }
float getSysVoltage() { return _sysVoltage; }
float getHumidity() { return _humidity; }
float getTemperature() { return _temperature; }
float getReserved() { return _reserved; }
private:
uint8_t _apiKey[VINDUINO_API_KEY_LEN];
float _soil_1;
float _soil_2;
float _soil_3;
float _soil_4;
float _sysVoltage;
float _humidity;
float _temperature;
float _reserved;
};
class MLNotifyThingspeakPayloadGen : public MLNotifyVindunoPayloadGen {
public:
MLNotifyThingspeakPayloadGen(uint8_t *apiKey, float f0, float f1, float f2, float f3, float f4, float f5, float f6, float f7);
};
//////////////////////////////////////////////////////////////////////////////////
class MLReqAuthChallengePayloadGen : public MLPayloadGen {
public:
MLReqAuthChallengePayloadGen(uint8_t *keyHMAC) : MLPayloadGen(CMD_REQ_AUTH_CHALLENGE) {
memcpy(_keyHMAC, keyHMAC, 4);
}
int getPayload(uint8_t *payload)
{
// prefix
int pos = getPayloadPrefix(payload);
memcpy(payload + pos, _keyHMAC, 4);
pos += 4;
// postfix
pos = getPayloadPostfix(payload, pos);
return pos;
}
uint8_t* getKeyHMAC() { return _keyHMAC; }
private:
uint8_t _keyHMAC[4];
};
class MLReqAuthJoinPayloadGen : public MLPayloadGen {
public:
MLReqAuthJoinPayloadGen() : MLPayloadGen(CMD_REQ_AUTH_JOIN) {}
int getPayload(uint8_t *payload)
{
// prefix
int pos = getPayloadPrefix(payload);
// postfix
pos = getPayloadPostfix(payload, pos);
return pos;
}
};
#define SIZE_AUTH_RESPONSE_HMAC 16
class MLAnsAuthResponsePayloadGen : public MLPayloadGen {
public:
MLAnsAuthResponsePayloadGen(uint8_t *dataHMAC) : MLPayloadGen(CMD_ANS_AUTH_RESPONSE)
{
memcpy(_dataHMAC, dataHMAC, SIZE_AUTH_RESPONSE_HMAC);
}
int getPayload(uint8_t *payload)
{
// prefix
int pos = getPayloadPrefix(payload);
memcpy(&payload[pos], _dataHMAC, SIZE_AUTH_RESPONSE_HMAC);
pos += SIZE_AUTH_RESPONSE_HMAC;
// postfix
pos = getPayloadPostfix(payload, pos);
return pos;
}
uint8_t* getDataMAC() { return _dataHMAC; }
private:
uint8_t _dataHMAC[SIZE_AUTH_RESPONSE_HMAC];
};
//////////////////////////////////////////////////////////////////////////////////
// MediaTek Cloud Sandbox (MCS)
class MLReqLoginMcsPayloadGen : public MLPayloadGen {
public:
MLReqLoginMcsPayloadGen(uint8_t dataLen, uint8_t *data) : MLPayloadGen(CMD_REQ_LOGIN_MCS)
{
_dataLen = dataLen;
memcpy(_data, data, dataLen);
}
int getPayload(uint8_t *payload)
{
// prefix
int pos = getPayloadPrefix(payload);
payload[pos++] = _dataLen;
memcpy(&payload[pos], _data, _dataLen);
pos += _dataLen;
// postfix
pos = getPayloadPostfix(payload, pos);
return pos;
}
uint8_t getDataLen() { return _dataLen; }
uint8_t* getData() {
return _data;
}
private:
uint8_t _dataLen;
uint8_t _data[ML_MAX_DATA_SIZE];
};
class MLSendMcsCommandPayloadGen : public MLReqLoginMcsPayloadGen {
public:
MLSendMcsCommandPayloadGen(uint8_t dataLen, uint8_t *data) : MLReqLoginMcsPayloadGen(dataLen, data)
{
_cmdId = CMD_SEND_MCS_COMMAND;
}
};
class MLNotifyMcsCommandPayloadGen : public MLReqLoginMcsPayloadGen {
public:
MLNotifyMcsCommandPayloadGen(uint8_t dataLen, uint8_t *data) : MLReqLoginMcsPayloadGen(dataLen, data)
{
_cmdId = CMD_NOTIFY_MCS_COMMAND;
}
};
class MLReqLogoutPayloadGen : public MLReqAuthJoinPayloadGen {
public:
MLReqLogoutPayloadGen() : MLReqAuthJoinPayloadGen()
{
_cmdId = CMD_REQ_LOGOUT;
}
};
class MLReqStillAlivePayloadGen : public MLReqAuthJoinPayloadGen {
public:
MLReqStillAlivePayloadGen() : MLReqAuthJoinPayloadGen()
{
_cmdId = CMD_REQ_STILL_ALIVE;
}
};
//////////////////////////////////////////////////////////////////////////////////
// myDevices: Cayenne
class MLReqLoginMydevicesPayloadGen : public MLReqLoginMcsPayloadGen {
public:
MLReqLoginMydevicesPayloadGen(uint8_t dataLen, uint8_t *data) : MLReqLoginMcsPayloadGen(dataLen, data)
{
_cmdId = CMD_REQ_LOGIN_MYDEVICES;
}
};
class MLSendMydevicesCommandPayloadGen : public MLReqLoginMcsPayloadGen {
public:
MLSendMydevicesCommandPayloadGen(uint8_t dataLen, uint8_t *data) : MLReqLoginMcsPayloadGen(dataLen, data)
{
_cmdId = CMD_SEND_MYDEVICES_COMMAND;
}
};
class MLNotifyMydevicesCommandPayloadGen : public MLReqLoginMcsPayloadGen {
public:
MLNotifyMydevicesCommandPayloadGen(uint8_t dataLen, uint8_t *data) : MLReqLoginMcsPayloadGen(dataLen, data)
{
_cmdId = CMD_NOTIFY_MYDEVICES_COMMAND;
}
};
class MLReqLogoutMydevicesPayloadGen : public MLReqLogoutPayloadGen {
public:
MLReqLogoutMydevicesPayloadGen() : MLReqLogoutPayloadGen()
{
_cmdId = CMD_REQ_LOGOUT_MYDEVICES;
}
};
//////////////////////////////////////////////////////////////////////////////////
class MLPacketGen {
public:
MLPacketGen(uint8_t version = 0x0B);
MLPacketGen(uint8_t ackBit, uint8_t receiverFlag, uint8_t packetType, uint8_t direction, uint8_t *id, uint8_t version = 0x0B);
~MLPacketGen() {
setMLPayloadGen(NULL);
}
void setMLPacket(uint8_t ackBit, uint8_t receiverFlag, uint8_t packetType, uint8_t direction, uint8_t *id, uint8_t version);
void setMLPayloadGen(MLPayloadGen *mlpayloadGen);
int getMLPacket(uint8_t *mlpacket);
uint8_t getAckBit() { return _ackBit; }
uint8_t getReceiverFlag() { return _receiverFlag; }
uint8_t getPacketType() { return _packetType; }
uint8_t getDirection() { return _direction; }
uint8_t* getID() { return _id; }
uint8_t getVersion() { return _version; }
MLPayloadGen* getMLPayload() { return _mlPayloadGen; }
private:
int getMLPayload(uint8_t *payload);
private:
uint8_t _ackBit;
uint8_t _receiverFlag;
uint8_t _packetType;
uint8_t _direction;
uint8_t _id[ML_PK_ID_SIZE];
uint8_t _version;
MLPayloadGen *_mlPayloadGen;
};
#endif /* MLPACKETGEN_H */