forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Message.h
648 lines (546 loc) · 14.9 KB
/
Message.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - https://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __Message_h__
#define __Message_h__
#include "HMID.h"
#include "Peer.h"
#include "Defines.h"
#include "Debug.h"
//#define MaxDataLen 60 // maximum length of received bytes
#define MaxDataLen 30
namespace as {
// some forward declarations
class ConfigPeerAddMsg;
class ConfigPeerRemoveMsg;
class ConfigPeerListReqMsg;
class ConfigParamReqMsg;
class ConfigStartMsg;
class ConfigEndMsg;
class ConfigWriteIndexMsg;
class AckMsg;
class Ack2Msg;
class AckStatusMsg;
class NackMsg;
class AckAesMsg;
class AesChallengeMsg;
class AesResponseMsg;
class AesExchangeMsg;
class InfoActuatorStatusMsg;
class InfoParamResponsePairsMsg;
class InfoPeerListMsg;
class SerialInfoMsg;
class DeviceInfoMsg;
class RemoteEventMsg;
class SensorEventMsg;
class ActionMsg;
class ActionSetMsg;
class Message {
public:
enum Flags {
WKUP = 0x01, // send initially to keep the device awake
WKMEUP = 0x02, // awake - hurry up to send messages
BCAST = 0x04, // Device in Config mode
BURST = 0x10, // set if burst is required by device
BIDI = 0x20, // response is expected
RPTED = 0x40, // repeated (repeater operation)
RPTEN = 0x80, // set in every message. Meaning?
};
protected:
uint8_t len; // message length
uint8_t cnt; // counter, if it is an answer counter has to reflect the answered message, otherwise own counter has to be used
uint8_t flag; // see structure of message flags
uint8_t typ; // type of message
HMID fromID; // sender ID
HMID toID; // receiver id, broadcast for 0
uint8_t comm; // type of message
uint8_t subcom; // type of message
uint8_t pload[MaxDataLen]; // payload
public:
Message () : len(0), cnt(0), flag(0), typ(0), comm(0), subcom(0) {}
void clear () {
len = 0;
}
uint8_t* buffer () {
return &cnt;
}
const uint8_t* buffer () const {
return &cnt;
}
uint8_t buffersize () {
return sizeof(Message)-1;
}
uint8_t* data () {
return pload;
}
const uint8_t* data () const {
return pload;
}
uint8_t datasize () const {
return len >= 11 ? len - 11 : 0;
}
void append (uint8_t data) {
*(buffer() + len) = data;
len++;
}
void append (void* data,uint8_t l) {
memcpy(buffer()+len,data,l);
len += l;
}
void append (uint8_t d1, uint8_t d2, uint8_t d3) {
uint8_t* ptr = buffer() + len;
*ptr = d1;
*(ptr+1) = d2;
*(ptr+2) = d3;
len += 3;
}
void append (uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4) {
uint8_t* ptr = buffer() + len;
*ptr = d1;
*(ptr+1) = d2;
*(ptr+2) = d3;
*(ptr+3) = d4;
len += 4;
}
void initWithCount(uint8_t length, uint8_t typ, uint8_t flags, uint8_t comm) {
this->len = length;
this->typ = typ;
this->flag = flags;
this->comm = comm;
}
void init(uint8_t length, uint8_t cnt, uint8_t typ, uint8_t flags, uint8_t comm, uint8_t sub) {
initWithCount(length,typ,flags,comm);
this->cnt = cnt;
this->subcom = sub;
}
void length (uint8_t l) {
len = l;
}
uint8_t length () const {
return len;
}
void count (uint8_t c) {
cnt = c;
}
uint8_t count () const {
return cnt;
}
void from(const HMID& hmid) {
fromID = hmid;
}
const HMID& from () const {
return fromID;
}
HMID& from () {
return fromID;
}
void to(const HMID& hmid) {
toID = hmid;
if( hmid == HMID::broadcast ) {
flag |= BCAST;
flag &= ~BIDI;
}
}
const HMID& to () const {
return toID;
}
void type (uint8_t t) {
typ=t;
}
uint8_t type () const {
return typ;
}
void flags (uint8_t f) {
flag = f;
}
uint8_t flags () const {
return flag;
}
void command (uint8_t c) {
comm = c;
}
uint8_t command () const {
return comm;
}
void subcommand (uint8_t c) {
subcom = c;
}
uint8_t subcommand () const {
return subcom;
}
void decode () {
decode(buffer(),length());
}
void decode(uint8_t *buf, uint8_t len) {
uint8_t prev = buf[0];
buf[0] = (~buf[0]) ^ 0x89;
uint8_t i, t;
for (i=1; i<len-1; i++) {
t = buf[i];
buf[i] = (prev + 0xdc) ^ buf[i];
prev = t;
}
buf[i] ^= buf[1];
}
void encode () {
encode(buffer(),length());
}
void encode(uint8_t *buf,uint8_t len) {
buf[0] = (~buf[0]) ^ 0x89;
uint8_t buf2 = buf[1];
uint8_t prev = buf[0];
uint8_t i;
for (i=1; i<len-1; i++) {
prev = (prev + 0xdc) ^ buf[i];
buf[i] = prev;
}
buf[i] ^= buf2;
}
void dump () const {
DHEX(length());
DPRINT(F(" "));
DHEX(count());
DPRINT(F(" "));
DHEX(flags());
DPRINT(F(" "));
DHEX(type());
DPRINT(F(" "));
from().dump();
DPRINT(F(" "));
to().dump();
DPRINT(F(" "));
DHEX(buffer()+9,length()-9);
DPRINT(F(" - "));
DDECLN((uint32_t)millis());
}
void setRepeated () {
flag |= RPTED;
}
void setRpten () {
flag |= RPTEN;
}
void setAck () {
flag |= BIDI;
}
void clearAck () {
flag &= ~BIDI;
}
bool isKeepAwake () const {
return (flag & WKUP) == WKUP;
}
bool isWakeMeUp () const {
return (flag & WKMEUP) == WKMEUP;
}
void setWakeMeUp () {
flag |= WKMEUP;
}
void setBroadcast () {
flag |= BCAST;
}
bool isBroadcast () const {
return (flag & BCAST) == BCAST;
}
bool isRepeated () const {
return (flag & RPTED) == RPTED;
}
bool ackRequired () const {
return (flag & BIDI) == BIDI;
}
bool burstRequired () const {
return (flag & BURST) == BURST;
}
void burstRequired (bool value) {
if( value == true ) {
flag |= BURST;
}
else {
flag &= ~BURST;
}
}
bool isPairSerial () const {
return typ==AS_MESSAGE_CONFIG && subcom==AS_CONFIG_PAIR_SERIAL;
}
bool isAck () const {
return typ==AS_MESSAGE_RESPONSE && (comm & AS_RESPONSE_NACK) == AS_RESPONSE_ACK;
}
bool isNack () const {
return typ==AS_MESSAGE_RESPONSE && (comm & AS_RESPONSE_NACK) == AS_RESPONSE_NACK;
}
bool isResponseAes () const {
return typ==AS_MESSAGE_RESPONSE_AES;
}
bool isChallengeAes () const {
return typ==AS_MESSAGE_RESPONSE && (comm & AS_RESPONSE_AES_CHALLANGE) == AS_RESPONSE_AES_CHALLANGE;
}
bool isRemoteEvent () const {
return typ==AS_MESSAGE_REMOTE_EVENT;
}
// cast to specific read-only message types
const ConfigPeerAddMsg& configPeerAdd () const { return *(ConfigPeerAddMsg*)this; }
const ConfigPeerRemoveMsg& configPeerRemove () const { return *(ConfigPeerRemoveMsg*)this; }
const ConfigPeerListReqMsg& configPeerListReq () const { return *(ConfigPeerListReqMsg*)this; }
const ConfigParamReqMsg& configParamReq () const { return *(ConfigParamReqMsg*)this; }
const ConfigStartMsg& configStart () const { return *(ConfigStartMsg*)this; }
const ConfigEndMsg& configEnd () const { return *(ConfigEndMsg*)this; }
const ConfigWriteIndexMsg& configWriteIndex () const { return *(ConfigWriteIndexMsg*)this; }
const RemoteEventMsg& remoteEvent () const { return *(RemoteEventMsg*)this; }
const SensorEventMsg& sensorEvent () const { return *(SensorEventMsg*)this; }
const ActionMsg& action () const { return *(ActionMsg*)this; }
const ActionSetMsg& actionSet () const { return *(ActionSetMsg*)this; }
// cast to write message types
AckMsg& ack () { return *(AckMsg*)this; }
Ack2Msg& ack2 () { return *(Ack2Msg*)this; }
AckStatusMsg& ackStatus () { return *(AckStatusMsg*)this; }
NackMsg& nack () { return *(NackMsg*)this; }
AckAesMsg& ackAes () { return *(AckAesMsg*)this; }
AesChallengeMsg& aesChallenge () { return *(AesChallengeMsg*)this; }
AesResponseMsg& aesResponse () { return *(AesResponseMsg*)this; }
AesExchangeMsg& aesExchange () { return *(AesExchangeMsg*)this; }
InfoActuatorStatusMsg& infoActuatorStatus () { return *(InfoActuatorStatusMsg*)this; }
InfoParamResponsePairsMsg& infoParamResponsePairs () { return *(InfoParamResponsePairsMsg*)this; }
InfoPeerListMsg& infoPeerList () { return *(InfoPeerListMsg*)this; }
DeviceInfoMsg& deviceInfo () { return *(DeviceInfoMsg*)this; }
SerialInfoMsg& serialInfo () { return *(SerialInfoMsg*)this; }
};
class ConfigMsg : public Message {
protected:
ConfigMsg () {}
public:
uint8_t channel () const { return command(); }
};
class ConfigPeerAddMsg : public ConfigMsg {
protected:
ConfigPeerAddMsg () {}
public:
const Peer& peer1 () const { return *((const Peer*)data()); }
Peer peer2 () const { return Peer(peer1(),*(data()+sizeof(Peer))); }
// if channel of peer2 == 0 or
// both channels are the same then we peer single mode
uint8_t peers () const {
Peer p2 = peer2();
return (p2.channel() == 0) || (p2 == peer1()) ? 1 : 2;
}
};
class ConfigPeerRemoveMsg : public ConfigPeerAddMsg {
protected:
ConfigPeerRemoveMsg () {}
};
class ConfigPeerListReqMsg : public ConfigMsg {
protected:
ConfigPeerListReqMsg () {}
};
class ConfigParamReqMsg : public ConfigMsg {
protected:
ConfigParamReqMsg () {}
public:
const Peer& peer () const { return *((const Peer*)data()); }
uint8_t list () const { return *(data()+sizeof(Peer)); }
};
class ConfigStartMsg : public ConfigParamReqMsg {
protected:
ConfigStartMsg () {}
};
class ConfigEndMsg : public ConfigMsg {
protected:
ConfigEndMsg () {}
};
class ConfigWriteIndexMsg : public ConfigMsg {
protected:
ConfigWriteIndexMsg () {}
};
class RemoteEventMsg : public Message {
protected:
RemoteEventMsg() {}
public:
void init(uint8_t msgcnt,uint8_t ch,uint8_t counter,bool lg,bool lowbat) {
uint8_t flags = lg ? 0x40 : 0x00;
if( lowbat == true ) {
flags |= 0x80; // low battery
}
Message::init(0xb,msgcnt,0x40, BIDI|WKMEUP,(ch & 0x3f) | flags,counter);
}
Peer peer () const { return Peer(from(),command() & 0x3f); }
uint8_t counter () const { return subcommand(); }
bool isLong () const { return (command() & 0x40) == 0x40; }
};
class SensorEventMsg : public RemoteEventMsg {
protected:
SensorEventMsg() {}
public:
void init(uint8_t msgcnt,uint8_t ch,uint8_t counter,uint8_t value,bool lowbat) {
uint8_t flags = lowbat ? 0x80 : 0x00;
Message::init(0xd,msgcnt,0x41, BIDI|WKMEUP,(ch & 0x3f) | flags,counter);
*data() = value;
}
uint8_t value () const { return *data(); }
};
class ActionMsg : public Message {
protected:
ActionMsg() {}
public:
uint8_t channel () const { return subcommand(); }
};
class ActionSetMsg : public ActionMsg {
protected:
ActionSetMsg() {}
public:
uint8_t channel () const { return subcommand(); }
uint8_t value () const { return *data(); }
uint16_t ramp () const {
uint16_t value = 0;
if( datasize() >= 3) {
value = (*(data()+1) << 8) + *(data()+2);
}
return value;
}
uint16_t delay () const {
uint16_t dly = 0xffff;
if( datasize() >= 5) {
dly = (*(data()+3) << 8) + *(data()+4);
}
if( dly == 0 ) dly = 0xffff;
return dly;
}
};
class AckMsg : public Message {
public:
void init(uint8_t flags=0x00) {
initWithCount(0x0a,AS_MESSAGE_RESPONSE,flags,AS_RESPONSE_ACK);
}
};
class Ack2Msg : public Message {
public:
void init(uint8_t flags=0x00) {
initWithCount(0x0a,AS_MESSAGE_RESPONSE,flags,AS_RESPONSE_ACK2);
}
};
class AckStatusMsg : public Message {
public:
template <class ChannelType>
void init(const ChannelType& ch,uint8_t rssi) {
initWithCount(0x0e,AS_MESSAGE_RESPONSE,0x00,AS_RESPONSE_ACK_STATUS);
subcom = ch.number();
pload[0] = ch.status();
pload[1] = ch.flags();
pload[2] = rssi;
}
};
class NackMsg : public Message {
public:
void init() {
initWithCount(0x0a,AS_MESSAGE_RESPONSE,0x00,AS_RESPONSE_NACK);
}
};
class AckAesMsg : public Message {
public:
void init(const uint8_t* data) {
initWithCount(0x12,AS_MESSAGE_RESPONSE,0x00,AS_RESPONSE_ACK);
subcom = data[0];
pload[0] = data[1];
pload[1] = data[2];
pload[2] = data[3];
}
};
class AesChallengeMsg : public Message {
public:
void init(const Message& msg,uint8_t keyidx) {
initWithCount(0x11,AS_MESSAGE_RESPONSE,RPTEN|BIDI,AS_RESPONSE_AES_CHALLANGE);
to(msg.from());
from(msg.to());
count(msg.count());
uint8_t* tmp = data()-1;
for( uint8_t i=0; i<6; i++ ) {
*tmp = (uint8_t)rand();
tmp++;
}
*tmp = keyidx;
}
const uint8_t* challenge () const {
return data()-1;
}
uint8_t keyindex () const {
return *(challenge()+6);
}
};
class AesResponseMsg : public Message {
public:
void init(const Message& msg) {
initWithCount(0x19,AS_MESSAGE_RESPONSE_AES,BIDI,0x00);
count(msg.count());
}
uint8_t* data () {
return Message::data()-2;
}
};
class AesExchangeMsg : public Message {
public:
uint8_t* data () {
return Message::data()-2;
}
};
class InfoActuatorStatusMsg : public Message {
public:
template <class ChannelType>
void init (uint8_t count,const ChannelType& ch,uint8_t rssi) {
Message::init(0x0e,count,0x10,BIDI|WKMEUP,0x06,ch.number());
pload[0] = ch.status();
pload[1] = ch.flags();
pload[2] = rssi;
}
};
class InfoParamResponsePairsMsg : public Message {
public:
void init (uint8_t count) {
initWithCount(0x0b-1+(8*2),0x10,BIDI,0x02);
cnt = count;
}
uint8_t* data() { return Message::data()-1; }
void entries (uint8_t num) { length(0x0b-1+(num*2)); };
};
class InfoPeerListMsg : public Message {
public:
void init (uint8_t count) {
initWithCount(0x0b-1+(4*sizeof(Peer)),0x10,Message::BIDI,0x01);
cnt = count;
}
uint8_t* data() { return Message::data()-1; }
void entries (uint8_t num) { length(0x0b-1+(num*sizeof(Peer))); };
};
class DeviceInfoMsg : public Message {
public:
void init (__attribute__((unused)) const HMID& to,uint8_t count) {
Message::init(0x1a,count,0x00,0x00,0x00,0x00);
}
uint8_t* data() { return Message::data()-2; }
void fill(uint8_t firmversion,uint8_t modelid[2],const char* serial,uint8_t subtype,uint8_t devinfo[3]) {
uint8_t* buf = data();
*buf = firmversion;
memcpy(buf+1,modelid,2);
memcpy(buf+3,serial,10);
*(buf+13) = subtype;
memcpy(buf+14,devinfo,3);
}
void fill(uint8_t firmversion,uint8_t subtype) {
uint8_t* buf = data();
*buf = firmversion;
*(buf+13) = subtype;
}
uint8_t* serial () { return data() + 3; }
uint8_t* model () { return data() + 1; }
uint8_t* info () { return data() + 14; }
};
class SerialInfoMsg : public Message {
public:
void init (__attribute__((unused)) const HMID& to,uint8_t count) {
Message::init(0x14,count,0x10,0x00,0x00,0x00);
}
uint8_t* data() { return Message::data()-4; }
void fill(const char* serial) {
uint8_t* buf = data();
memcpy(buf+3,serial,10);
}
uint8_t* serial () { return data() + 3; }
};
}
#endif