forked from Sweet-Peas/SP3000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sense.cpp
420 lines (363 loc) · 9.42 KB
/
sense.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* sense.cpp
*
* Created on: 26 oct 2013
* Author: pontus
*/
#include <Arduino.h>
//#define DEBUG
#include "debug.hpp"
#include "settings.hpp"
#include "sense.hpp"
#include "socket.hpp"
#include "socket-support.hpp"
#include "http.hpp"
enum parse_state {
PARSE_NOT_STARTED = 0x00,
PARSE_TAG = 0x01,
PARSE_DATA = 0x02
};
#define PARSE_READ(x) do { if (data_available(s)) \
x = sp_read(s); \
else \
return -SENSE_ERR_JSON_ERROR; \
} while (0)
static char sense_key[32];
static uint32_t sense_IP = 0;
void sp_sense_set_key (char *key)
{
strncpy (sense_key, key, SENSE_KEY_LENGTH);
}
#if defined(USE_POST_PARSING)
/**
* Internal function
*
* read_and_parse
*
* Reads the json stream, parses the json entities and values as well as
* updates the json data structure.
*
* Currently only timetag and id is supported.
*/
static int read_and_parse (uint32_t s, SenseData* data)
{
byte parse = 0;
byte parsing = PARSE_NOT_STARTED;
char data_instance = -1;
uint16_t jsontag;
char *wptr;
while (data_available(s)) {
char c = sp_read(s);
switch (c) {
// Start indicator of json stream
case '[':
parse = 1;
break;
// Start indicator of data instance
case '{':
data_instance++;
break;
case '\"':
if (parsing == PARSE_NOT_STARTED) parsing = PARSE_TAG;
break;
case ':':
// Eat up the following "
if (data_available(s))
sp_read(s);
else
// If there's no more data something is wrong
return -SENSE_ERR_JSON_ERROR;
parsing = PARSE_DATA;
break;
// End indicator of data instance
case '}':
break;
// End indicator for json stream
case ']':
parse = 0;
return SENSE_OK;
break;
default:
break;
}
// Should we parse ?
if (parse) {
if (parsing == PARSE_TAG) {
jsontag = 0;
// Read until the end of the " block
PARSE_READ(c);
while (data_available(s)) {
if (c == '\"')
break;
jsontag += c;
PARSE_READ(c);
}
PRINT (F(" : Hash "));
PRINT (jsontag);
} else if (parsing == PARSE_DATA) {
switch (jsontag)
{
case 747: // timetag
wptr = (char *)&data[data_instance].timetag;
break;
case 205: // id
wptr = (char *)&data[data_instance].id;
break;
case 541: // value
case 704: // feed_id
case 1059: // response_id
wptr = NULL;
break;
default:
wptr = NULL;
break;
}
// Only write data if we have a valid pointer
if (wptr) {
PRINT (" - Data ");
PARSE_READ(c);
while ((c != '\"') && (c != ',')) {
*wptr++ = c;
PARSE_READ(c);
}
// Terminate the string
*wptr = 0;
parsing = PARSE_NOT_STARTED;
} else {
PRINT (" - NO SAVE ");
// Just skip to next
PARSE_READ(c);
while ((c != '\"') && (c != ',')) {
PARSE_READ(c);
}
parsing = PARSE_NOT_STARTED;
}
PRINTLN ();
}
}
}
return SENSE_OK;
}
#endif
//
// The debug server is used internally at Electronic Sweet Peas for developing
// this library.
//
//#define USE_DEBUG_SERVER
#define WEBSITE "api.sen.se"
#ifdef USE_DEBUG_SERVER
#define USE_PORT 8080
#else
#define USE_PORT 80
#endif
//
// This function connects to api.sen.se and returns the socket associated
// with the connection. If something fails during the attempt it returns
// with the appropriate error code.
//
static int sp_sense_connect(void)
{
int s;
int res = -1;
uint16_t port = USE_PORT;
// Number of resolve retries.
byte cnt = 10;
#ifdef DEBUG
char strip[16];
#endif
#ifndef USE_DEBUG_SERVER
// If IP have not been resolved already go ahead and do that.
if (!sense_IP) {
while (res < 0) {
PRINT (F("Resolving host: "));
PRINTLN (F(WEBSITE));
res = gethostbyname(WEBSITE, strlen(WEBSITE), &sense_IP);
if (res < 0 || sense_IP == 0) {
res = -1; // Just in case we received 0.0.0.0
PRINT (F("Could not resolve "));
PRINTLN (F(WEBSITE));
delay (1000);
if (!--cnt) {
PRINTLN (F("Really could not resolve"));
return -SENSE_ERR_SERVER_NOT_FOUND;
}
}
}
}
#else
sense_IP = packIP(192,168,0,102);
#endif
#ifdef DEBUG
sprintf (strip, "%d.%d.%d.%d", (byte)(sense_IP >> 24) & 0xff, (byte)(sense_IP >> 16) & 0xff,
(byte)(sense_IP >> 8) & 0xff, (byte)(sense_IP & 0xff));
PRINT (F("Remote host IP address: "));
PRINTLN (strip);
#endif
// Try connecting four times before giving up.
cnt = 4;
s = -1;
while (s < 0) {
s = sp_connect (sense_IP, port, IPPROTO_TCP);
if (s < 0) {
PRINT (F("Could not connect !"));
delay (1000);
if (!--cnt) {
PRINTLN (F("\r\nReally could not connect !"));
return -SENSE_ERR_NOT_CONNECTED;
}
PRINTLN (F(" Trying again."));
}
}
PRINTLN (F("Connected to remote server !"));
// Return socket
return (s);
}
int sp_sense_send (byte size, SenseData* data)
{
uint32_t s;
byte feed_id_size = 0;
uint16_t tx_data_len = 0;
int res = -1;
#ifdef DEBUG
char strip[16];
#endif
// Sanity check
if (!size && !data)
return -SENSE_ERR_PARAMETER;
// Establish connection to sen.se
if ((s = sp_sense_connect()) < 0) {
PRINT (F("Failed to connect to Sen.se !"));
return s;
}
PRINT (F("TX Socket: "));
PRINTLN (s);
// Calculate content length
// TODO: Remove the pesky feed_id_size, why o why
for (byte i=0; i<size; i++) {
unsigned long temp = data[i].feed_id;
feed_id_size = 1;
// Count the number of digits in feed_id
while ((temp = temp/10) > 0) feed_id_size++;
tx_data_len += feed_id_size;
tx_data_len += strlen(data[i].value);
tx_data_len += 23;
}
tx_data_len += 2;
tx_data_len += (size - 1);
PRINTLN (F("SEND: Sending http headers !"));
PRINT (tx_data_len);
PRINT (" - ");
PRINTLN (data->value);
sp_send (s, F("POST /events/?sense_key="));
sp_send (s, sense_key);
sp_send (s, F(" HTTP/1.1\n"));
sp_send (s, F("Host: api.sen.se\r\n"));
sp_send (s, F("Content-Type: application/json\r\n"));
sp_send (s, F("Content-Length: "));
sp_send (s, tx_data_len);
sp_send (s, F("\r\nConnection: Keep-Alive"));
sp_send (s, F("\r\n\r\n"));
PRINTLN (F("\r\nSending json data !"));
sp_send (s, F("["));
// Send groups of data
for(byte i=0;i<size;i++) {
sp_send (s, F("{\"feed_id\":"));
sp_send (s, data[i].feed_id);
sp_send (s, F(",\"value\":\""));
sp_send (s, data[i].value);
sp_send (s, F("\"}"));
if(size > 1 && i <= (size-2)) {
sp_send (s, F(","));
}
}
sp_send (s, F("]"));
sp_send (s, F("\r\n\r\n"));
PRINTLN (F("Reading http headers !"));
if ((res = read_http_headers(s)) < 0) {
PRINT (F("Error "));
PRINT (res);
PRINTLN (F(" occurred, terminating sen.se communication !"));
closesocket(s);
return res;
}
#if defined(USE_POST_PARSING)
// Read and parse the reply
PRINTLN (F("Read and parse json response !"));
res = read_and_parse (s, data);
#endif
PRINTLN (F("Closing socket and leaving !"));
closesocket(s);
return res;
}
//
// This is a simple and fault intolerant parser that will look for the third
// " character and then store the argument up to the next " character.
//
static int parse_poll_response (uint32_t s, char *buff)
{
byte markcnt = 0;
char c;
// Make sure we have some data coming
if (!data_available(s, 5))
return -SENSE_ERR_TIMEOUT;
while (data_available(s)) {
c = sp_read (s);
if ('\"' == c) {
if (++markcnt == 7) {
while (data_available(s)) {
if ((c = sp_read(s)) == '\"') {
*buff = 0;
return SENSE_OK;
}
*buff++ = c;
}
}
}
}
return -SENSE_ERR_JSON_ERROR;
}
int sp_sense_poll (uint32_t feed_id, char *output, boolean is_device)
{
uint32_t s;
int res;
// Sanity check
if (!feed_id || !output)
return -SENSE_ERR_PARAMETER;
// Establish connection to sen.se
if ((s = sp_sense_connect()) < 0) {
PRINT (F("Failed to connect to Sen.se !"));
return s;
}
PRINTLN (F("POLL: Sending http headers !"));
if (is_device)
sp_send (s, F("GET /devices/"));
else {
sp_send (s, F("GET /feeds/"));
}
sp_send (s, feed_id);
sp_send (s, F("/last_event/?sense_key="));
sp_send (s, sense_key);
sp_send (s, F(" HTTP/1.1\n"));
sp_send (s, F("Host: api.sen.se\n"));
sp_send (s, F("Content-Type: application/json\r\n"));
sp_send (s, F("Content-Length: 0\r\n"));
sp_send (s, F("Connection: Keep-Alive"));
sp_send (s, F("\r\n\r\n"));
PRINTLN (F("Reading http headers !"));
if ((res = read_http_headers(s)) < 0) {
PRINT (F("Error "));
PRINT (res);
PRINTLN (F(" occurred, terminating sen.se communication !"));
closesocket(s);
return res;
}
res = parse_poll_response (s, output);
PRINTLN (F("Closing socket and leaving !"));
closesocket(s);
return res;
}
int sp_sense_poll (uint32_t feed_id, char *output)
{
return sp_sense_poll (feed_id, output, false);
}
/* EOF */