-
Notifications
You must be signed in to change notification settings - Fork 71
/
Channel.h
464 lines (388 loc) · 12.8 KB
/
Channel.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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __CHANNEL_H__
#define __CHANNEL_H__
#include "Peer.h"
#include "Storage.h"
namespace as {
template <class HalType,class List0Type> class Device;
class ActionSetMsg;
class ActionCommandMsg;
class RemoteEventMsg;
class SensorEventMsg;
template<class HalType,class List1Type,class List3Type,class List4Type,int PeerCount,class List0Type=List0, class List2Type=EmptyList>
class Channel {
Device<HalType,List0Type>* dev;
bool change : 1; // the status is changed, we may need to send a status
bool inhi : 1;
uint8_t num ; // channels per device
uint16_t addr ; // start address in eeprom
public:
typedef List1Type List1;
typedef List2Type List2;
typedef List3Type List3;
typedef List4Type List4;
typedef Device<HalType,List0Type> DeviceType;
public:
Channel () : dev(0), change(false), inhi(false), num(0), addr(0) {}
DeviceType& device () { return *dev; }
const DeviceType& device () const { return *dev; }
uint8_t number () const { return num; }
uint16_t address () const { return addr; }
uint8_t peers () const { return PeerCount; }
bool changed () const { return change; }
void changed (bool c) { change = c; }
void inhibit (bool value) { inhi = value; }
bool inhibit () const { return inhi; }
bool aesActive () const { return getList1().aesActive(); }
void setup(Device<HalType,List0Type>* dev,uint8_t number,uint16_t addr) {
this->dev = dev;
this->num = number;
this->addr = addr;
}
uint16_t size () const {
uint16_t size = sizeof(Peer);
size += List3::size() + List4::size();
size *= PeerCount;
size += List1::size();
return size;
}
bool has (const Peer& p) const {
for( uint8_t i=0; i<peers(); ++i ) {
if( p == peerat(i) ) {
return true;
}
}
return false;
}
uint8_t peerfor (const HMID& hmid) const {
for( uint8_t i=0; i<peers(); ++i ) {
if( hmid == peerat(i) ) {
return i;
}
}
return 0xff;
}
Peer peerat (uint8_t idx) const {
Peer result;
uint16_t paddr = peerAddress(idx);
if( paddr != 0 ) {
storage().getData(paddr,&result);
}
return result;
}
bool peer (const Peer& p) {
deletepeer(p);
uint8_t pidx = findpeer();
if( pidx != 0xff ) {
storage().setData(peerAddress(pidx),p);
if( hasList3() == true ) {
getList3(pidx).single();
}
return true;
}
return false;
}
bool peer (const Peer& p1,const Peer& p2) const {
deletepeer(p1);
deletepeer(p2);
uint8_t pidx1 = findpeer();
if( pidx1 != 0xff ) {
storage().setData(peerAddress(pidx1),p1);
uint8_t pidx2 = findpeer();
if( pidx2 != 0xff ) {
storage().setData(peerAddress(pidx2),p2);
if( hasList3() == true ) {
if( p1.odd() == true ) {
getList3(pidx1).odd();
getList3(pidx2).even();
}
else {
getList3(pidx2).odd();
getList3(pidx1).even();
}
}
return true;
}
else {
// free already stored data
deletepeer(p1);
}
}
return false;
}
bool deletepeer (uint8_t idx) const {
return storage().setData(peerAddress(idx),Peer());
}
uint8_t findpeer () const {
for( int i=0; i<peers(); ++i ) {
if( peerat(i).valid()==false ) {
return i;
}
}
return 0xff;
}
bool deletepeer (const Peer& p) const {
for( uint8_t i=0; i<peers(); ++i ) {
if( peerat(i) == p ) {
deletepeer(i);
}
}
return true;
}
void firstinit () {
storage().clearData(address(),size());
List1Type cl1 = getList1();
cl1.defaults();
if( hasList2() ) {
List2Type cl2 = getList2();
cl2.defaults();
}
}
List1Type getList1 () const {
// we start always with list1
return List1Type(address());
}
List2Type getList2 () const {
return List2Type(address()+List1::size());
}
List3Type getList3 (const Peer& p) const {
uint16_t liststart = 0x00;
if( hasList3() == true ) {
for( uint8_t i=0; i<peers(); ++i ) {
if( peerat(i) == p ) {
liststart = peerAddress(i) + sizeof(Peer);
break;
}
}
}
return List3Type(liststart);
}
List4Type getList4 (const Peer& p) const {
uint16_t liststart = 0x00;
if( hasList4() == true ) {
for( uint8_t i=0; i<peers(); ++i ) {
if( peerat(i) == p ) {
liststart = peerAddress(i) + sizeof(Peer) + List3::size();
break;
}
}
}
return List4Type(liststart);
}
List3Type getList3 (uint8_t pidx) const {
uint16_t liststart = 0x00;
if( hasList3() == true && pidx < peers() ) {
liststart = peerAddress(pidx) + sizeof(Peer);
}
return List3Type(liststart);
}
List4Type getList4 (uint8_t pidx) const {
uint16_t liststart = 0x00;
if( hasList4() == true && pidx < peers() ) {
liststart = peerAddress(pidx) + sizeof(Peer) + List3::size();
}
return List4Type(liststart);
}
static bool hasList2 () {
return List2Type::size() > 0;
}
static bool hasList3 () {
return List3Type::size() > 0;
}
static bool hasList4 () {
return List4Type::size() > 0;
}
void stop () {}
bool process (__attribute__((unused)) const ActionSetMsg& msg) {
return false;
}
bool process (__attribute__((unused)) const ActionCommandMsg& msg) {
return false;
}
bool process (__attribute__((unused)) const RemoteEventMsg& msg) {
return false;
}
bool process (__attribute__((unused)) const SensorEventMsg& msg) {
return false;
}
void patchStatus (__attribute__((unused)) Message& msg) {}
void configChanged () {}
protected:
uint16_t peerAddress (uint8_t pidx) const {
if( pidx < PeerCount ) {
uint16_t offset = sizeof(Peer);
offset += List3::size() + List4::size();
offset *= pidx;
offset += List1::size() + List2::size();
return addr + offset;
}
return 0x00;
}
};
template <class HalType,class List1Type,class List3Type,int PeerCount,class List0Type, class StateMachine, class List2Type=EmptyList>
class ActorChannel : public Channel<HalType,List1Type, List3Type,EmptyList,PeerCount,List0Type, List2Type >, public StateMachine {
public:
typedef Channel<HalType,List1Type, List3Type,EmptyList,PeerCount,List0Type, List2Type> BaseChannel;
uint8_t lastmsgcnt;
public:
ActorChannel () : BaseChannel(), lastmsgcnt(0xff) {}
~ActorChannel() {}
bool changed () const { return StateMachine::changed(); }
void changed (bool c) { StateMachine::changed(c); }
void setup(Device<HalType,List0Type>* dev,uint8_t number,uint16_t addr) {
BaseChannel::setup(dev,number,addr);
StateMachine::setup(this->getList1());
}
uint8_t status () const {
return StateMachine::status();
}
uint8_t flags () const {
uint8_t flags = StateMachine::flags();
if( this->device().battery().low() == true ) {
flags |= 0x80;
}
return flags;
}
void status (uint8_t stat, uint16_t delay) {
StateMachine::status(stat,delay);
}
void stop () {
StateMachine::stop();
}
bool process (__attribute__((unused)) const ActionCommandMsg& msg) {
return true;
}
bool process (const ActionSetMsg& msg) {
StateMachine::set( msg.value(), msg.ramp(), msg.delay() );
return true;
}
bool process (const RemoteEventMsg& msg) {
if( BaseChannel::hasList3() ) {
bool lg = msg.isLong();
Peer p(msg.peer());
uint8_t cnt = msg.counter();
List3Type l3 = BaseChannel::getList3(p);
if( l3.valid() == true ) {
// l3.dump();
typename List3Type::PeerList pl = lg ? l3.lg() : l3.sh();
// pl.dump();
if( lg == false || cnt != lastmsgcnt || pl.multiExec() == true ) {
lastmsgcnt = cnt;
StateMachine::remote(pl,cnt);
}
return true;
}
}
return false;
}
bool process (const SensorEventMsg& msg) {
if( BaseChannel::hasList3() ) {
bool lg = msg.isLong();
Peer p(msg.peer());
uint8_t cnt = msg.counter();
uint8_t value = msg.value();
List3Type l3 = BaseChannel::getList3(p);
if( l3.valid() == true ) {
// l3.dump();
typename List3Type::PeerList pl = lg ? l3.lg() : l3.sh();
// pl.dump();
StateMachine::sensor(pl,cnt,value);
return true;
}
}
return false;
}
};
template <class HalType,class List0Type=List0>
class VirtBaseChannel {
public:
VirtBaseChannel () {}
virtual ~VirtBaseChannel () {}
virtual void setup(Device<HalType,List0Type>* dev,uint8_t number,uint16_t addr) = 0;
virtual uint16_t size () const = 0;
virtual uint8_t number () const = 0;
virtual uint16_t address () const = 0;
virtual uint8_t peers () const = 0;
virtual bool changed () const = 0;
virtual void changed (bool c) = 0;
virtual void inhibit (bool value) = 0;
virtual bool inhibit () const = 0;
virtual bool aesActive () const = 0;
virtual bool has (const Peer& p) const = 0;
virtual Peer peerat (uint8_t idx) const = 0;
virtual bool peer (const Peer& p) = 0;
virtual bool peer (const Peer& p1,const Peer& p2) = 0;
virtual bool deletepeer (const Peer& p) = 0;
virtual void firstinit () = 0;
#ifndef SENSOR_ONLY
virtual void stop () = 0;
virtual bool process (const ActionSetMsg& msg) = 0;
virtual bool process (const ActionCommandMsg& msg) = 0;
virtual bool process (const RemoteEventMsg& msg) = 0;
virtual bool process (const SensorEventMsg& msg) = 0;
#endif
virtual uint8_t status () = 0;
virtual uint8_t flags () = 0;
virtual void patchStatus (Message& msg) = 0;
virtual void configChanged () = 0;
virtual GenericList getList1 () const = 0;
virtual GenericList getList2 () const = 0;
virtual GenericList getList3 (const Peer& p) const = 0;
virtual GenericList getList4 (const Peer& p) const = 0;
virtual bool hasList2 () const = 0;
virtual bool hasList3 () const = 0;
virtual bool hasList4 () const = 0;
};
template <class HalType,class ChannelType,class List0Type=List0>
class VirtChannel : public VirtBaseChannel<HalType,List0Type> {
ChannelType ch;
public:
VirtChannel () {}
virtual ~VirtChannel () {}
operator ChannelType& () { return ch; }
virtual void setup(Device<HalType,List0Type>* dev,uint8_t number,uint16_t addr) { ch.setup(dev,number,addr); }
virtual uint16_t size () const { return ch.size(); }
virtual uint8_t number () const { return ch.number(); }
virtual uint16_t address () const { return ch.address(); }
virtual uint8_t peers () const { return ch.peers(); }
virtual bool changed () const { return ch.changed(); }
virtual void changed (bool c) { ch.changed(c); }
virtual void inhibit (bool value) { ch.inhibit(value); }
virtual bool inhibit () const { return ch.inhibit(); }
virtual bool aesActive () const { return ch.aesActive(); }
virtual bool has (const Peer& p) const { return ch.has(p); };
virtual Peer peerat (uint8_t idx) const { return ch.peerat(idx); }
virtual bool peer (const Peer& p) { return ch.peer(p); }
virtual bool peer (const Peer& p1,const Peer& p2) { return ch.peer(p1,p2); }
virtual bool deletepeer (const Peer& p) { return ch.deletepeer(p); }
virtual void firstinit () { ch.firstinit(); }
#ifndef SENSOR_ONLY
virtual void stop () { ch.stop(); };
virtual bool process (const ActionSetMsg& msg) { return ch.process(msg); }
virtual bool process (const ActionCommandMsg& msg) { return ch.process(msg); }
virtual bool process (const RemoteEventMsg& msg) { return ch.process(msg); }
virtual bool process (const SensorEventMsg& msg) { return ch.process(msg); }
#endif
virtual uint8_t status () { return ch.status(); }
virtual uint8_t flags () { return ch.flags(); }
virtual void patchStatus (Message& msg) { ch.patchStatus(msg); }
virtual void configChanged () { ch.configChanged(); }
virtual GenericList getList1 () const { return ch.getList1(); }
virtual GenericList getList2 () const { return ch.getList2(); }
virtual GenericList getList3 (const Peer& p) const { return ch.getList3(p); }
virtual GenericList getList4 (const Peer& p) const { return ch.getList4(p); }
virtual bool hasList2 () const { return ChannelType::hasList2(); }
virtual bool hasList3 () const { return ChannelType::hasList3(); }
virtual bool hasList4 () const { return ChannelType::hasList4(); }
};
#define channelISR(chan,pin,mode,type) class __##pin##ISRHandler { \
public: \
static void isr () { chan.handleISR(); } \
}; \
pinMode(pin,mode); \
enableInterrupt(pin,__##pin##ISRHandler::isr,type);
}
#endif