forked from exosite-garage/arduino_exosite_library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExosite.cpp
580 lines (483 loc) · 16.7 KB
/
Exosite.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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//*****************************************************************************
//
// exosite.cpp - Prototypes for the Exosite Cloud API
//
// Copyright (c) 2012 Exosite LLC. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Exosite LLC nor the names of its contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
//*****************************************************************************
#include "Exosite.h"
/*==============================================================================
* Exosite
*
* constructor for Exosite class
*=============================================================================*/
Exosite::Exosite(Client *_client)
{
client = _client;
fetchNVCIK();
}
Exosite::Exosite(const char *_cik, Client *_client)
{
strncpy(cik, _cik, 41);
client = _client;
}
Exosite::Exosite(const String _cik, Client *_client)
{
_cik.toCharArray(cik, 41);
client = _client;
}
/*==============================================================================
* writeRead
*
* One step read and write to Exosite using char arrays.
*=============================================================================*/
boolean Exosite::writeRead(const char* writeString, const char* readString, char** returnString){
ret = false;
stringPos = 0;
DataRx= false;
RxLoop = true;
timeout_time = 0;
time_now = 0;
timeout = 3000; // 3 seconds
varPtr = aliasList;
Serial.print(F("Connecting to Exosite..."));
if (!client->connected()) {
Serial.print("No Existing Connection, Opening One...");
client->stop();
client->connect(serverName,80);
}
if (client->connected()) {
client->flush();
Serial.println(F("Connected"));
// Send request using Exosite basic HTTP API
client->print(F("POST /onep:v1/stack/alias?"));
client->print(readString);
client->println(F(" HTTP/1.1"));
client->println(F("Host: m2.exosite.com"));
client->print(F("User-Agent: Exosite-Activator/"));
client->print(ACTIVATOR_VERSION);
client->print(F(" Arduino/"));
client->println(ARDUINO);
client->print(F("X-Exosite-CIK: "));
client->println(cik);
client->println(F("Accept: application/x-www-form-urlencoded; charset=utf-8"));
client->println(F("Content-Type: application/x-www-form-urlencoded; charset=utf-8"));
client->print(F("Content-Length: "));
client->println(strlen(writeString)); //calculate length
client->println();
client->println(writeString);
// Read from the nic or the IC buffer overflows with no warning and goes out to lunch
timeout_time = millis()+ timeout;
#if EXOSITEDEBUG > 1
Serial.println(F("Sent"));
#endif
while ((timeout_time > time_now) && RxLoop) {
if (client->available()) {
if (!DataRx)
DataRx= true;
c = client->read();
rxdata[stringPos] = c;
#if EXOSITEDEBUG > 2
Serial.print(c);
#endif
stringPos += 1;
} else {
#if EXOSITEDEBUG > 4
Serial.println(F("No More Data"));
#endif
rxdata[stringPos] = 0;
if (DataRx) {
DataRx = false;
RxLoop = false;
#if EXOSITEDEBUG > 1
Serial.println("HTTP Response:");
Serial.println(rxdata);
#endif
if (strstr(rxdata, "HTTP/1.1 200 OK")) {
#ifdef EXOSITEDEBUG
Serial.println(F("HTTP Status: 200"));
#endif
ret = true;
varPtr = strstr(rxdata, "\r\n\r\n") + 4;
*returnString = (char*) realloc(*returnString, (rxdata + stringPos + 1) - varPtr);
if(*returnString == 0)
break;
strncpy(*returnString, varPtr, (rxdata + stringPos + 1) - varPtr);
}else if(strstr(rxdata, "HTTP/1.1 204 No Content")){
#ifdef EXOSITEDEBUG
Serial.println(F("HTTP Status: 204"));
#endif
ret = true;
} else {
#ifdef EXOSITEDEBUG
Serial.println(F("Warning Unknown Response: "));
varPtr = strstr(rxdata, "\n");
*varPtr = '\0';
Serial.println(rxdata);
#endif
}
}
}
time_now = millis();
}
if(timeout_time <= time_now){
Serial.println(F("Error: HTTP Response Timeout"));
}
}else{
Serial.println(F("Error: Can't Open Connection to Exosite."));
}
#ifdef EXOSITEDEBUG
Serial.println(F("End Char ReadWrite"));
#endif
return ret;
}
/*==============================================================================
* writeRead
*
* One step read and write to Exosite using Arduino String objects.
*=============================================================================*/
boolean Exosite::writeRead(const String &writeString, const String &readString, String &returnString){
#ifdef EXOSITEDEBUGMEM
Serial.print(getFreeMemory());
Serial.println(F(" = Free Memory String Start"));
Serial.println(writeString);
Serial.println(readString);
#endif
char *writeCharString, *readCharString, *returnCharString;
writeCharString = (char*)malloc(sizeof(char) * writeString.length()+1);
readCharString = (char*)malloc(sizeof(char) * readString.length()+1);
returnCharString = (char*)malloc(sizeof(char) * 32);
#ifdef EXOSITEDEBUGMEM
Serial.print(getFreeMemory());
Serial.println(F(" = Free Memory String Chars Allocated"));
Serial.print(F("Return Address Before: "));
Serial.println((intptr_t)returnCharString);
#endif
if(writeCharString == 0 || readCharString == 0 || returnCharString == 0){
Serial.println(F("Not Enough Ram! Failing!"));
while(1);
}
writeString.toCharArray(writeCharString, writeString.length()+1);
readString.toCharArray(readCharString, readString.length()+1);
#ifdef EXOSITEDEBUGMEM
Serial.print(getFreeMemory());
Serial.println(F(" = Free Memory String Start Char Write"));
#endif
if(this->writeRead(writeCharString, readCharString, &returnCharString)){
#ifdef EXOSITEDEBUGMEM
Serial.print(getFreeMemory());
Serial.println(F(" = Free Memory String Char Write Finished"));
Serial.print(F("Return Address After: "));
Serial.println((intptr_t)returnCharString);
#endif
returnString = String(returnCharString);
#ifdef EXOSITEDEBUGMEM
Serial.print(getFreeMemory());
Serial.println(F(" = Free Memory String Chars Copied"));
#endif
ret = true;
}else{
Serial.println(F("Error Communicating with Exosite"));
ret = false;
}
free(writeCharString);
free(readCharString);
free(returnCharString);
#ifdef EXOSITEDEBUGMEM
Serial.print(getFreeMemory());
Serial.println(F(" = Free Memory String Chars Freed"));
#endif
return ret;
}
/*==============================================================================
* provision
*
* Provision on Exosite Platform, activate device and get cik.
*=============================================================================*/
boolean Exosite::provision(const char* vendorString, const char* modelString, const char* snString){
ret = false;
stringPos = 0;
DataRx= false;
timeout_time = 0;
time_now = 0;
timeout = 3000; // 3 seconds
const char* vendorParameter = "vendor=";
const char* modelParameter = "&model=";
const char* snParameter = "&sn=";
size_t writeStringLen = strlen(vendorString) +
strlen(modelString) +
strlen(snString) +
strlen(vendorParameter) +
strlen(modelParameter) +
strlen(snParameter) +
1;
char *writeString = (char*)malloc(sizeof(char) * (writeStringLen));
// Assemble Parameter String
varPtr = writeString;
strcpy(varPtr, vendorParameter);
varPtr = varPtr + strlen(vendorParameter);
strcpy(varPtr, vendorString);
varPtr = varPtr + strlen(vendorString);
strcpy(varPtr, modelParameter);
varPtr = varPtr + strlen(modelParameter);
strcpy(varPtr, modelString);
varPtr = varPtr + strlen(modelString);
strcpy(varPtr, snParameter);
varPtr = varPtr + strlen(snParameter);
strcpy(varPtr, snString);
Serial.print(F("Connecting to Exosite (Provision)..."));
if (!client->connected()) {
Serial.print("No Existing Connection, Opening One...");
client->stop();
client->connect(serverName,80);
}
if (client->connected()) {
client->flush();
Serial.println(F("Connected"));
// Send request using Exosite basic HTTP API
client->println(F("POST /provision/activate HTTP/1.1"));
client->println(F("Host: m2.exosite.com"));
client->print(F("User-Agent: Exosite-Activator/"));
client->print(ACTIVATOR_VERSION);
client->print(F(" Arduino/"));
client->println(ARDUINO);
client->println(F("Content-Type: application/x-www-form-urlencoded; charset=utf-8"));
client->print(F("Content-Length: "));
client->println(strlen(writeString)); //calculate length
client->println();
client->println(writeString);
// Read from the nic or the IC buffer overflows with no warning and goes out to lunch
timeout_time = millis()+ timeout;
#ifdef EXOSITEDEBUG
Serial.print(F("Sent: "));
Serial.println(writeString);
#endif
while ((timeout_time > time_now)) {
if (client->available()) {
if (!DataRx)
DataRx= true;
c = client->read();
rxdata[stringPos] = c;
stringPos += 1;
} else {
rxdata[stringPos] = 0;
if (DataRx) {
#ifdef EXOSITEDEBUG
Serial.println(F("HTTP Response:"));
Serial.println(rxdata);
#endif
if (strstr(rxdata, "HTTP/1.1 200 OK")) {
Serial.println(F("Activated Successfully"));
varPtr = strstr(rxdata, "\r\n\r\n") + 4;
if(strlen(varPtr) == 40 && this->isHex(varPtr, 40)){
strncpy(cik, varPtr, 41);
saveNVCIK();
ret = true;
}else{
Serial.print(F("CRITICAL: Got 200 Response, Wasn't a Valid CIK"));
#if EXOSITEDEBUG > 9
Serial.print(F("CRITICAL: Failing Hard to Preserve State"));
while(1);
#endif
ret = false;
}
}else if(strstr(rxdata, "HTTP/1.1 404 Not Found")){
Serial.println(F("Error: A client matching the given parameters was not found in the provisioning system. (404)"));
ret = false;
}else if(strstr(rxdata, "HTTP/1.1 409 Conflict")){
Serial.println(F("Warning: The client specified is already in use. (409)"));
ret = false;
} else {
Serial.println(F("Warning: Unknown Response:"));
varPtr = strstr(rxdata, "\n");
*varPtr = '\0';
Serial.println(rxdata);
ret = false;
}
break;
}
}
time_now = millis();
}
#ifdef EXOSITEDEBUG
if(timeout_time <= time_now){
Serial.println(F("HTTP Response Timeout"));
}
#endif
}else{
Serial.println(F("Error: Can't Open Connection to Exosite."));
}
free(writeString);
#ifdef EXOSITEDEBUG
Serial.println(F("End of Provision"));
#endif
return ret;
}
/*==============================================================================
* saveNVCIK
*
* Write the CIK to EEPROM
*=============================================================================*/
boolean Exosite::saveNVCIK(){
for(int i = 0; i < 40; i++){
EEPROM.write(CIK_EEPROM_ADDRESS + i, cik[i]);
}
return true;
}
/*==============================================================================
* fetchNVCIK
*
* Fetch the CIK from EEPROM
*=============================================================================*/
boolean Exosite::fetchNVCIK(){
char tempBuf[41];
for(int i = 0; i < 40; i++){
tempBuf[i] = EEPROM.read(CIK_EEPROM_ADDRESS + i);
}
tempBuf[40] = 0;
if(strlen(tempBuf) == 40){
strcpy(cik, tempBuf);
return true;
}else{
return false;
}
}
/*==============================================================================
* time
*
* Gets the server time as a unix timestamp from m2.exosite.com/timestamp.
*=============================================================================*/
unsigned long Exosite::time(){
unsigned long timestamp = 0;
stringPos = 0;
DataRx= false;
timeout_time = 0;
time_now = 0;
timeout = 3000; // 3 seconds
Serial.print(F("Connecting to Exosite (Time)..."));
if (!client->connected()) {
Serial.print("No Existing Connection, Opening One...");
client->stop();
client->connect(serverName,80);
}
if (client->connected()) {
client->flush();
Serial.println(F("Connected"));
// Send request using Exosite basic HTTP API
client->println(F("GET /timestamp HTTP/1.1"));
client->println(F("Host: m2.exosite.com"));
client->print(F("User-Agent: Exosite-Activator/"));
client->print(ACTIVATOR_VERSION);
client->print(F(" Arduino/"));
client->println(ARDUINO);
client->println();
// Read from the nic or the IC buffer overflows with no warning and goes out to lunch
timeout_time = millis()+ timeout;
#ifdef EXOSITEDEBUG
Serial.print(F("Sent"));
#endif
while ((timeout_time > time_now)) {
if (client->available()) {
if (!DataRx)
DataRx= true;
c = client->read();
rxdata[stringPos] = c;
stringPos += 1;
} else {
rxdata[stringPos] = 0;
if (DataRx) {
#ifdef EXOSITEDEBUG
Serial.println(F("HTTP Response:"));
Serial.println(rxdata);
#endif
if (strstr(rxdata, "HTTP/1.1 200 OK")) {
varPtr = strstr(rxdata, "\r\n\r\n") + 4;
Serial.print(F("Current Time is: "));
Serial.println(varPtr);
timestamp = strtoul(varPtr, NULL, 10);
}else {
Serial.println(F("Warning: Unknown Response:"));
varPtr = strstr(rxdata, "\n");
*varPtr = '\0';
Serial.println(rxdata);
}
break;
}
}
time_now = millis();
}
#ifdef EXOSITEDEBUG
if(timeout_time <= time_now){
Serial.println(F("HTTP Response Timeout"));
}
#endif
}else{
Serial.println(F("Error: Can't Open Connection to Exosite."));
}
#ifdef EXOSITEDEBUG
Serial.println(F("End of Time"));
#endif
return timestamp;
}
boolean Exosite::isHex(char *str, int len){
for(int i = 0; i < len; i++){
if(!((str[i] >= '0' && str[i] <= '9') ||
(str[i] >= 'A' && str[i] <= 'F') ||
(str[i] >= 'a' && str[i] <= 'f'))){
return false;
}
}
return true;
}
/*==============================================================================
* DEPRECIATED METHODS
*=============================================================================*/
/*==============================================================================
* sendToCloud
*
* send data to cloud
*=============================================================================*/
int Exosite::sendToCloud(String res, int value){
String readString = "";
String returnString = "";
String writeString;
writeString = res + "=" + value;
if(this->writeRead(writeString, readString, returnString)){
return 1;
}else{
Serial.println(F("Error Communicating with Exosite"));
return 0;
}
}
/*==============================================================================
* readFromCloud
*
* read data from cloud
*=============================================================================*/
int Exosite::readFromCloud(String readString ,String* returnString){
String writeString = "";
if(this->writeRead(writeString, readString, *returnString)){
return 1;
}else{
Serial.println(F("Error Communicating with Exosite"));
return 0;
}
}