-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOLCB_Event_Handler.cpp
353 lines (322 loc) · 10 KB
/
OLCB_Event_Handler.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
/*
* OLCB_Event_Handler.cpp
*
* Created on: Sep 1, 2011
* Author: dgoodman
*
* Much of this code taken from PCE.h and PCE.cpp, written by who, I know not. Bob Jacobsen perhaps?
*/
#include "OLCB_Event_Handler.h"
void OLCB_Event_Handler::update(void) //this method should be overridden to detect conditions for the production of events
{
if(!isPermitted())
{
return;
}
// see if any replies are waiting to send
//Serial.println("update===========");
while (_sendEvent < _numEvents)
{
//Serial.print("_sendEvent: ");
//Serial.println(_sendEvent);
//Serial.print("_numEvents: ");
//Serial.println(_numEvents);
// OK to send, see if marked for some cause
// ToDo: This only sends _either_ producer ID'd or consumer ID'd, not both
if ( (_events[_sendEvent].flags & (IDENT_FLAG | OLCB_Event::CAN_PRODUCE_FLAG)) == (IDENT_FLAG | OLCB_Event::CAN_PRODUCE_FLAG))
{
//Serial.println("identify produced events!");
if(_link->sendProducerIdentified(NID, &_events[_sendEvent]))
{
_events[_sendEvent].flags &= ~IDENT_FLAG; // reset flag
}
break; // only send one from this loop
}
else if ( (_events[_sendEvent].flags & (IDENT_FLAG | OLCB_Event::CAN_CONSUME_FLAG)) == (IDENT_FLAG | OLCB_Event::CAN_CONSUME_FLAG))
{
//Serial.println("identify consumed events!");
if(_link->sendConsumerIdentified(NID, &_events[_sendEvent]))
{
_events[_sendEvent].flags &= ~IDENT_FLAG; // reset flag
}
break; // only send one from this loop
}
else if (_events[_sendEvent].flags & PRODUCE_FLAG)
{
//Serial.print("producing ");
//Serial.println(_sendEvent, DEC);
if(_link->sendPCER(NID, &_events[_sendEvent]))
_events[_sendEvent].flags &= ~PRODUCE_FLAG; // reset flag
break; // only send one from this loop
}
else if (_events[_sendEvent].flags & TEACH_FLAG)
{
//Serial.println("ABOUT TO SEND TEACH MESSAGE");
//Serial.println(_sendEvent, DEC);
//Serial.println(_events[_sendEvent].val[6], HEX);
//Serial.println(_events[_sendEvent].val[7], HEX);
_link->sendLearnEvent(NID, &_events[_sendEvent]);
_events[_sendEvent].flags &= ~TEACH_FLAG; // reset flag
break; // only send one from this loop
}
else
{
// just skip
++_sendEvent;
}
}
}
void OLCB_Event_Handler::loadEvents(OLCB_Event* events, uint16_t numEvents)
{
_events = events;
_numEvents = numEvents;
_halfEvents = _numEvents>>1;
//Serial.println("loadevents");
//Serial.println(_numEvents, DEC);
//Serial.println(_halfEvents, DEC);
//Serial.println("loadEvents");
for(uint16_t i = 0; i < _numEvents; ++i)
{
_events[i].flags = 0;
//Serial.print(i, DEC);
//Serial.print(" ");
//Serial.println(_events[i].flags, HEX);
}
}
bool OLCB_Event_Handler::produce(uint16_t index) //OLCB_Event *event) //call to produce an event with ID = EID
{
//Serial.print("produce() ");
//Serial.println(index);
bool retval = false;
// int8_t index = 0;
// while (-1 != (index = event->findIndexInArray(_events, _numEvents, index)))
// {
// // yes, we have to reply with ConsumerIdentified
if (_events[index].flags & OLCB_Event::CAN_PRODUCE_FLAG)
{
retval = true;
_events[index].flags |= PRODUCE_FLAG;
_sendEvent = min(_sendEvent, index);
//_events[index].print();
}
// ++index;
// if (index>=_numEvents) break;
// }
return retval;
}
//this method should be overridden to handle the consumption of events.
//return true if we were able to successfully consume the event, false otherwise
bool OLCB_Event_Handler::consume(uint16_t index)
{
return false;
}
/* Protocol level interactions for every kind of virtual node */
bool OLCB_Event_Handler::handleMessage(OLCB_Buffer *buffer)
{
//Serial.println("in OLCB_Event_Handler::handleMessage");
bool retval = false;
//first, check to see if it is an event
if(buffer->isPCEventReport()) //it's what is colloquially known as an event! Or technically as a Producer-Consumer Event Report (!?)
{
//Serial.println("PCE");
//great, get the EID, and call consume
OLCB_Event event;
buffer->getEventID(&event);
//check whether the event is in our array
retval = handlePCEventReport(&event);
}
else if(buffer->isIdentifyProducers()) //we may need to respond to this one
{
//Serial.println("IdentifyProducers");
// see if we produce the listed event
OLCB_Event event;
buffer->getEventID(&event);
retval = handleIdentifyProducers(&event);
}
else if(buffer->isIdentifyConsumers())
{
//Serial.println("IdentifyConsumers");
// see if we consume the listed event
OLCB_Event event;
buffer->getEventID(&event);
retval = handleIdentifyConsumers(&event);
}
else if(buffer->isIdentifyEvents() || buffer->isIdentifyEventsAddressed())
{
//Serial.println("IdentifyEvents");
// See if addressed to us
if(buffer->isIdentifyEventsAddressed()) //addressed variant
{
//Serial.println("addressed");
OLCB_NodeID n;
buffer->getDestNID(&n);
//Serial.print("got Identify Events for ");
//n.print();
//NID->print();
if(n.empty() || *NID == n)//addressed to us
{
//Serial.println("to us!");
retval = handleIdentifyEvents();
}
}
else
{
//Serial.println("global!");
retval = handleIdentifyEvents();
}
}
else if(buffer->isLearnEvent())
{
//Serial.println("LearnEvent");
OLCB_Event event;
buffer->getEventID(&event);
retval = handleLearnEvent(&event);
}
return retval; //default: not something for us to handle!
}
bool OLCB_Event_Handler::handleIdentifyEvents(void)
{
for (int i = 0; i < _numEvents; ++i)
{
//Serial.print("flagging event ");
//Serial.println(i, DEC);
//Serial.println(_events[i].flags, HEX);
_events[i].flags |= IDENT_FLAG;
}
_sendEvent = 0;
return true;
}
bool OLCB_Event_Handler::handlePCEventReport(OLCB_Event *event)
{
bool retval = false;
int16_t index = 0;
//Serial.println("Received PCER");
//Serial.println("Can consume?");
while (-1 != (index = event->findIndexInArray(_events, _numEvents, index)))
{
if (_events[index].flags & OLCB_Event::CAN_CONSUME_FLAG)
{
//Serial.println("Yes");
// yes, notify our own code
if(consume(index))
{
retval = true;
}
}
++index;
if (index>=_numEvents)
{
break;
}
}
// if(!retval)
// //Serial.println("No");
return retval;
}
bool OLCB_Event_Handler::handleIdentifyProducers(OLCB_Event *event)
{
bool retval = false;
int16_t index = 0;
// find producers of event
while (-1 != (index = event->findIndexInArray(_events, _numEvents, index)))
{
// yes, we have to reply with ProducerIdentified
if (_events[index].flags & OLCB_Event::CAN_PRODUCE_FLAG)
{
retval = true;
_events[index].flags |= IDENT_FLAG;
_sendEvent = min(_sendEvent, index);
}
++index;
if (index>=_numEvents) break;
}
// ToDo: add identify flags so that events that are both produced and consumed
// have only one form sent in response to a specific request.
return retval;
}
bool OLCB_Event_Handler::handleIdentifyConsumers(OLCB_Event *event)
{
bool retval = false;
int index = 0;
// find consumers of event
//event->print();
while (-1 != (index = event->findIndexInArray(_events, _numEvents, index)))
{
//Serial.print("found event at ");
//Serial.println(index, DEC);
// yes, we have to reply with ConsumerIdentified
if (_events[index].flags & OLCB_Event::CAN_CONSUME_FLAG)
{
retval = true;
_events[index].flags |= IDENT_FLAG;
_sendEvent = min(_sendEvent, index);
}
++index;
if (index>=_numEvents) break;
}
//Serial.println(index, DEC);
return retval;
}
bool OLCB_Event_Handler::handleLearnEvent(OLCB_Event *event)
{
//Serial.println(event->val[7]);
bool save = false;
for (uint16_t i=0; i<_numEvents; ++i)
{
if ( (_events[i].flags & LEARN_FLAG ) != 0 )
{
memcpy(_events[i].val, event->val, 8);
_events[i].flags |= IDENT_FLAG; // notify new eventID
_events[i].flags &= ~LEARN_FLAG; // enough learning
_sendEvent = min(_sendEvent, i);
save = true;
}
}
if (save)
{
store(); //TODO check for success here!
}
return save;
}
void OLCB_Event_Handler::newEvent(int index, bool p, bool c)
{
_events[index].flags |= IDENT_FLAG;
_sendEvent = min(_sendEvent, index);
if (p) _events[index].flags |= OLCB_Event::CAN_PRODUCE_FLAG;
if (c) _events[index].flags |= OLCB_Event::CAN_CONSUME_FLAG;
//Serial.print("new event ");
//Serial.print(index, DEC);
//Serial.print(" ");
//Serial.println(_events[index].flags, HEX);
}
void OLCB_Event_Handler::markToLearn(int index, bool mark)
{
if (mark)
_events[index].flags |= LEARN_FLAG;
else
_events[index].flags &= ~LEARN_FLAG;
}
void OLCB_Event_Handler::markToTeach(int index, bool mark)
{
if (mark)
_events[index].flags |= TEACH_FLAG;
else
_events[index].flags &= ~TEACH_FLAG;
_sendEvent = min(_sendEvent, index);
}
bool OLCB_Event_Handler::markedToLearn(int index)
{
return(_events[index].flags & LEARN_FLAG);
}
bool OLCB_Event_Handler::markedToTeach(int index)
{
return(_events[index].flags & TEACH_FLAG);
}
bool OLCB_Event_Handler::store(void)
{
return false; //false for not saved!
}
bool OLCB_Event_Handler::load(void)
{
return false;
}