forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Channel.h
342 lines (282 loc) · 9.36 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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http:https://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 RemoteEventMsg;
class SensorEventMsg;
template<class HalType,class List1Type,class List3Type,class List4Type,int PeerCount,class List0Type=List0>
class Channel {
Device<HalType,List0Type>* dev;
bool change; // the status is changed, we may need to send a status
bool inhi;
uint8_t num ; // channels per device
uint16_t addr ; // start address in eeprom
public:
typedef List1Type List1;
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 == peer(i) ) {
return true;
}
}
return false;
}
uint8_t peerfor (const HMID& hmid) const {
for( uint8_t i=0; i<peers(); ++i ) {
if( hmid == peer(i) ) {
return i;
}
}
return 0xff;
}
Peer peer (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);
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( 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( peer(i).valid()==false ) {
return i;
}
}
return 0xff;
}
bool deletepeer (const Peer& p) const {
for( uint8_t i=0; i<peers(); ++i ) {
if( peer(i) == p ) {
deletepeer(i);
}
}
return true;
}
void firstinit () {
storage.clearData(address(),size());
List1Type cl = getList1();
cl.defaults();
}
List1Type getList1 () const {
// we start always with list1
return List1Type(address());
}
List3Type getList3 (const Peer& p) const {
uint16_t liststart = 0x00;
if( hasList3() == true ) {
for( uint8_t i=0; i<peers(); ++i ) {
if( peer(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( peer(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 hasList3 () {
return List3Type::size() > 0;
}
static bool hasList4 () {
return List4Type::size() > 0;
}
bool process (__attribute__((unused)) const ActionSetMsg& 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();
return addr + offset;
}
return 0x00;
}
};
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 peer (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;
virtual bool process (const ActionSetMsg& msg) = 0;
virtual bool process (const RemoteEventMsg& msg) = 0;
virtual bool process (const SensorEventMsg& msg) = 0;
virtual uint8_t status () const = 0;
virtual uint8_t flags () const = 0;
virtual void patchStatus (Message& msg) = 0;
virtual void configChanged () = 0;
virtual GenericList getList1 () const = 0;
virtual GenericList getList3 (const Peer& p) const = 0;
virtual GenericList getList4 (const Peer& p) 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 peer (uint8_t idx) const { return ch.peer(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(); }
virtual bool process (const ActionSetMsg& 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); }
virtual uint8_t status () const { return ch.status(); }
virtual uint8_t flags () const { 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 getList3 (const Peer& p) const { return ch.getList3(p); }
virtual GenericList getList4 (const Peer& p) const { return ch.getList4(p); }
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