-
Notifications
You must be signed in to change notification settings - Fork 8
/
xPL.cpp
404 lines (341 loc) · 10.6 KB
/
xPL.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
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
/*
* xPL.Arduino v0.1, xPL Implementation for Arduino
*
* This code is parsing a xPL message stored in 'received' buffer
* - isolate and store in 'line' buffer each part of the message -> detection of EOL character (DEC 10)
* - analyse 'line', function of its number and store information in xpl_header memory
* - check for each step if the message respect xPL protocol
* - parse each command line
*
* Copyright (C) 2012 [email protected], [email protected]
* Original version by [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "xPL.h"
#define XPL_LINE_MESSAGE_BUFFER_MAX 128 // max length of a line // maximum command in a xpl message
#define XPL_END_OF_LINE 10
// define the line number identifier
#define XPL_MESSAGE_TYPE_IDENTIFIER 1
#define XPL_OPEN_HEADER 2
#define XPL_HOP_COUNT 3
#define XPL_SOURCE 4
#define XPL_TARGET 5
#define XPL_CLOSE_HEADER 6
#define XPL_SCHEMA_IDENTIFIER 7
#define XPL_OPEN_SCHEMA 8
// Heartbeat request class definition
//prog_char XPL_HBEAT_REQUEST_CLASS_ID[] PROGMEM = "hbeat";
//prog_char XPL_HBEAT_REQUEST_TYPE_ID[] PROGMEM = "request";
//prog_char XPL_HBEAT_ANSWER_CLASS_ID[] PROGMEM = "hbeat";
//prog_char XPL_HBEAT_ANSWER_TYPE_ID[] PROGMEM = "basic"; //app, basic
#define XPL_HBEAT_REQUEST_CLASS_ID "hbeat"
#define XPL_HBEAT_REQUEST_TYPE_ID "request"
#define XPL_HBEAT_ANSWER_CLASS_ID "hbeat"
#define XPL_HBEAT_ANSWER_TYPE_ID "app"
/* xPL Class */
xPL::xPL()
{
udp_port = XPL_UDP_PORT;
SendExternal = NULL;
#ifdef ENABLE_PARSING
AfterParseAction = NULL;
last_heartbeat = 0;
hbeat_interval = XPL_DEFAULT_HEARTBEAT_INTERVAL;
xpl_accepted = XPL_ACCEPT_ALL;
#endif
}
xPL::~xPL()
{
}
/// Set the source of outgoing xPL messages
void xPL::SetSource_P(const PROGMEM char * _vendorId, const PROGMEM char * _deviceId, const PROGMEM char * _instanceId)
{
memcpy_P(source.vendor_id, _vendorId, XPL_VENDOR_ID_MAX);
memcpy_P(source.device_id, _deviceId, XPL_DEVICE_ID_MAX);
memcpy_P(source.instance_id, _instanceId, XPL_INSTANCE_ID_MAX);
}
/**
* \brief Send an xPL message
* \details There is no validation of the message, it is sent as is.
* \param buffer buffer containing the xPL message.
*/
void xPL::SendMessage(char *_buffer)
{
(*SendExternal)(_buffer);
}
/**
* \brief Send an xPL message
* \details There is no validation of the message, it is sent as is.
* \param message An xPL message.
* \param _useDefaultSource if true, insert the default source (defined in SetSource) on the message.
*/
void xPL::SendMessage(xPL_Message *_message, bool _useDefaultSource)
{
if(_useDefaultSource)
{
_message->SetSource(source.vendor_id, source.device_id, source.instance_id);
}
char *message_buffer = _message->toString();
SendMessage(message_buffer);
free(message_buffer);
}
#ifdef ENABLE_PARSING
/**
* \brief xPL Stuff
* \details Send heartbeat messages at "hbeat_interval" interval
*/
void xPL::Process()
{
static bool bFirstRun = true;
// Check heartbeat + send
if ((millis()-last_heartbeat >= (unsigned long)hbeat_interval * 1000)
|| (bFirstRun && millis() > 3000))
{
SendHBeat();
bFirstRun = false;
}
}
/**
* \brief Parse an ingoing xPL message
* \details Parse a message, check for hearbeat request and call user defined callback for post processing.
* \param buffer buffer of the ingoing UDP Packet
*/
void xPL::ParseInputMessage(char* _buffer)
{
xPL_Message* xPLMessage = new xPL_Message();
Parse(xPLMessage, _buffer);
// check if the message is an hbeat.request to send a heartbeat
if (CheckHBeatRequest(xPLMessage))
{
SendHBeat();
}
// call the user defined callback to execute an action
if(AfterParseAction != NULL)
{
(*AfterParseAction)(xPLMessage);
}
delete xPLMessage;
}
/**
* \brief Check the xPL message target
* \details Check if the xPL message is for us
* \param _message an xPL message
*/
bool xPL::TargetIsMe(xPL_Message * _message)
{
if (memcmp(_message->target.vendor_id, source.vendor_id, strlen(source.vendor_id)) != 0)
return false;
if (memcmp(_message->target.device_id, source.device_id, strlen(source.device_id)) != 0)
return false;
if (memcmp(_message->target.instance_id, source.instance_id, strlen(source.instance_id)) != 0)
return false;
return true;
}
/**
* \brief Send a heartbeat message
*/
void xPL::SendHBeat()
{
last_heartbeat = millis();
char buffer[XPL_MESSAGE_BUFFER_MAX];
// sprintf_P(buffer, PSTR("xpl-stat\n{\nhop=1\nsource=%s-%s.%s\ntarget=*\n}\n%s.%s\n{\ninterval=%d\n}\n"), source.vendor_id, source.device_id, source.instance_id, XPL_HBEAT_ANSWER_CLASS_ID, XPL_HBEAT_ANSWER_TYPE_ID, hbeat_interval);
sprintf_P(buffer, PSTR("xpl-stat\n{\nhop=1\nsource=%s-%s.%s\ntarget=*\n}\n%s.%s\n{\ninterval=%d\nport=3865\nremote-ip=192.168.4.133\nversion=1.0\n}\n"), source.vendor_id, source.device_id, source.instance_id, XPL_HBEAT_ANSWER_CLASS_ID, XPL_HBEAT_ANSWER_TYPE_ID, hbeat_interval);
//(*SendExternal)(buffer);
SendMessage(buffer);
}
/**
* \brief Check if the message is a heartbeat request
* \param _message an xPL message
*/
inline bool xPL::CheckHBeatRequest(xPL_Message* _message)
{
if (!TargetIsMe(_message))
return false;
return _message->IsSchema(XPL_HBEAT_REQUEST_CLASS_ID, XPL_HBEAT_REQUEST_TYPE_ID);
}
/**
* \brief Parse a buffer and generate a xPL_Message
* \details Line based xPL parser
* \param _xPLMessage the result xPL message
* \param _message the buffer
*/
void xPL::Parse(xPL_Message* _xPLMessage, char* _buffer)
{
int len = strlen(_buffer);
byte j=0;
byte line=0;
int result=0;
char lineBuffer[XPL_LINE_MESSAGE_BUFFER_MAX+1];
// read each character of the message
for(byte i = 0; i < len; i++)
{
// load byte by byte in 'line' buffer, until '\n' is detected
if(_buffer[i] == XPL_END_OF_LINE) // is it a linefeed (ASCII: 10 decimal)
{
++line;
lineBuffer[j]='\0'; // add the end of string id
if(line <= XPL_OPEN_SCHEMA)
{
// first part: header and schema determination
// we analyse the line, function of the line number in the xpl message
result = AnalyseHeaderLine(_xPLMessage, lineBuffer ,line);
}
if(line > XPL_OPEN_SCHEMA)
{
// second part: command line
// we analyse the specific command line, function of the line number in the xpl message
result = AnalyseCommandLine(_xPLMessage, lineBuffer, line-9, j);
if(result == _xPLMessage->command_count+1)
break;
}
if (result < 0) break;
j = 0; // reset the buffer pointer
clearStr(lineBuffer); // clear the buffer
}
else
{
// next character
lineBuffer[j++] = _buffer[i];
}
}
}
/**
* \brief Parse the header part of the xPL message line by line
* \param _xPLMessage the result xPL message
* \param _buffer the line to parse
* \param _line the line number
*/
byte xPL::AnalyseHeaderLine(xPL_Message* _xPLMessage, char* _buffer, byte _line)
{
switch (_line)
{
case XPL_MESSAGE_TYPE_IDENTIFIER: //message type identifier
if (memcmp_P(_buffer,PSTR("xpl-"),4)==0) //xpl
{
if (memcmp_P(_buffer+4,PSTR("cmnd"),4)==0) //command type
{
_xPLMessage->type=XPL_CMND; //xpl-cmnd
}
else if (memcmp_P(_buffer+4,PSTR("stat"),4)==0) //statut type
{
_xPLMessage->type=XPL_STAT; //xpl-stat
}
else if (memcmp_P(_buffer+4,PSTR("trig"),4)==0) // trigger type
{
_xPLMessage->type=XPL_TRIG; //xpl-trig
}
}
else
{
return 0; //unknown message
}
return 1;
break;
case XPL_OPEN_HEADER: //header begin
if (memcmp(_buffer,"{",1)==0)
{
return 2;
}
else
{
return -2;
}
break;
case XPL_HOP_COUNT: //hop
if (sscanf_P(_buffer, XPL_HOP_COUNT_PARSER, &_xPLMessage->hop))
{
return 3;
}
else
{
return -3;
}
break;
case XPL_SOURCE: //source
if (sscanf_P(_buffer, XPL_SOURCE_PARSER, &_xPLMessage->source.vendor_id, &_xPLMessage->source.device_id, &_xPLMessage->source.instance_id) == 3)
{
return 4;
}
else
{
return -4;
}
break;
case XPL_TARGET: //target
if (sscanf_P(_buffer, XPL_TARGET_PARSER, &_xPLMessage->target.vendor_id, &_xPLMessage->target.device_id, &_xPLMessage->target.instance_id) == 3)
{
return 5;
}
else
{
if(memcmp(_xPLMessage->target.vendor_id,"*", 1) == 0) // check if broadcast message
{
return 5;
}
else
{
return -5;
}
}
break;
case XPL_CLOSE_HEADER: //header end
if (memcmp(_buffer,"}",1)==0)
{
return 6;
}
else
{
return -6;
}
break;
case XPL_SCHEMA_IDENTIFIER: //schema
sscanf_P(_buffer, XPL_SCHEMA_PARSER, &_xPLMessage->schema.class_id, &_xPLMessage->schema.type_id);
return 7;
break;
case XPL_OPEN_SCHEMA: //header begin
if (memcmp(_buffer,"{",1)==0)
{
return 8;
}
else
{
return -8;
}
break;
}
return -100;
}
/**
* \brief Parse the body part of the xPL message line by line
* \param _xPLMessage the result xPL message
* \param _buffer the line to parse
* \param _command_line the line number
*/
byte xPL::AnalyseCommandLine(xPL_Message * _xPLMessage, char *_buffer, byte _command_line, byte line_length)
{
if (memcmp(_buffer,"}",1) == 0) // End of schema
{
return _xPLMessage->command_count+1;
}
else // parse the next command
{
struct_command newcmd;
sscanf_P(_buffer, XPL_COMMAND_PARSER, &newcmd.name, &newcmd.value);
_xPLMessage->AddCommand(newcmd.name, newcmd.value);
return _command_line;
}
}
#endif