This repository has been archived by the owner on Jan 3, 2020. It is now read-only.
/
VirtualShield.cpp
877 lines (758 loc) · 22.7 KB
/
VirtualShield.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
/*
Copyright(c) Microsoft Open Technologies, Inc. All rights reserved.
The MIT License(MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "VirtualShield.h"
#if defined(_WINDOWS_) || defined(_WINRT_DLL)
#include <malloc.h>
#else
#include <alloca.h>
#endif
#include <string.h>
#include <ArduinoJson.h>
#include "SensorModels.h"
#if defined(ARDUINO) && ARDUINO > 100
// Define the serial port that is used to talk to the virtual shield.
#define VIRTUAL_SERIAL_PORT0 Serial
// Force a link to printf float functionality. There is a bug where some boards do not link this properly.
asm(".global _printf_float");
// For 32 bit boards, strlen_PF takes a char*, so this cast isn't needed
#if defined(ARDUINO_SAMD_MKR1000) || defined(_VARIANT_ARDUINO_101_X_)
#define uint_farptr_t
#endif
// If it has dual serial ports, prefer the second one (pins 0, 1) for bluetooth.
#if defined(__AVR_ATmega32U4__) || defined(ARDUINO_SAMD_MKR1000) || defined(_VARIANT_ARDUINO_101_X_) || (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define VIRTUAL_SERIAL_PORT1 Serial1
#define debugSerial
#define debugSerialIn
#else
#define VIRTUAL_SERIAL_PORT1 Serial
#endif
#else
#include "..\Time.h"
using namespace Windows::Storage::Streams;
using namespace Microsoft::Maker::Time;
#endif
static const int SERIAL_ERROR = -1;
static const int SERIAL_SUCCESS = 0;
// Strings used to build virtual shield commands.
const PROGMEM char MESSAGE_SERVICE_START[] = "{'Service':'";
const PROGMEM char MESSAGE_SERVICE_TO_ID[] = "','Id':";
const PROGMEM char MESSAGE_QUOTE[] = "'";
const PROGMEM char MESSAGE_SEPARATOR[] = ",'";
const PROGMEM char MESSAGE_PAIR_SEPARATOR[] = "':";
const PROGMEM char MESSAGE_END2[] = "}";
const PROGMEM char ARRAY_START[] = "[{";
const PROGMEM char ARRAY_END[] = "}]";
const PROGMEM char NONTEXT_END[] = "}";
const PROGMEM char MESSAGE_END[] = "'}";
const PROGMEM char SERVICE_NAME_SERVICE[] = "SYSTEM";
const PROGMEM char PONG[] = "PONG";
const PROGMEM char TYPE[] = "TYPE";
const PROGMEM char START[] = "START";
const PROGMEM char LEN[] = "LEN";
const char AWAITING_MESSAGE[] = "{}";
const char SYSTEM_EVENT = '!';
const int requestInterval = 1000;
const int perMessageInterval = 25;
const int maxRememberedSensors = 10;
const int maxReadBuffer = 160;
const int maxJsonReadBuffer = maxReadBuffer + 2;
const int maxJsonWriteBuffer = 32767;
char readBuffer[maxReadBuffer];
int readBufferIndex = 0;
int bracketCount = 0;
uint32_t lastOpenRequest = 0;
bool isArrayStarted = false;
int recentEventErrorId = 0;
Sensor* sensors[maxRememberedSensors];
int sensorCount = 0;
/// <summary>
/// Initializes a new instance of the <see cref="VirtualShield"/> class.
/// </summary>
VirtualShield::VirtualShield()
{
}
/// <summary>
/// Adds a sensor to the list of known sensors in order to match and dispatch for incoming events.
/// </summary>
/// <param name="sensor">The sensor to add.</param>
/// <returns>true if it was added, false if the maxRememberedSensors is reached.</returns>
bool VirtualShield::addSensor(Sensor* sensor) {
if (sensorCount == maxRememberedSensors)
{
return false;
}
sensors[sensorCount++] = sensor;
return true;
}
/// <summary>
/// Begins the specified bit rate.
/// </summary>
/// <param name="bitRate">The bit rate to use for the virtual shield serial connection.</param>
void VirtualShield::begin(long bitRate)
{
_VShieldSerial = &VIRTUAL_SERIAL_PORT1;
#if defined(ARDUINO) && ARDUINO >= 100
reinterpret_cast<HardwareSerial *>(_VShieldSerial)->begin(bitRate);
#else
_VShieldSerial->begin(static_cast<uint32_t>(bitRate), SerialConfig::SERIAL_8N1);
#endif
delay(500);
sendStart();
if (this->onConnect)
{
this->onConnect(&recentEvent);
}
if (this->onRefresh)
{
this->onRefresh(&recentEvent);
}
}
/// <summary>
/// Begins the specified stream.
/// </summary>
/// <param name="bitRate">The bit rate to use for the virtual shield serial connection.</param>
#if defined(ARDUINO) && ARDUINO > 100
void VirtualShield::begin(Stream &s)
{
_VShieldSerial = &s;
delay(500);
sendStart();
if (this->onConnect)
{
this->onConnect(&recentEvent);
}
if (this->onRefresh)
{
this->onRefresh(&recentEvent);
}
}
#elif defined(_WINRT_DLL)
void VirtualShield::begin(IStream^ s)
{
_VShieldSerial = s;
sendStart();
if (this->onConnect)
{
this->onConnect(&recentEvent);
}
if (this->onRefresh)
{
this->onRefresh(&recentEvent);
}
}
#endif
/// <summary>
/// Blocks while waiting for a specific id-based response (only when blocking is true and allowAutoBlocking is true).
/// </summary>
int VirtualShield::block(int id, bool blocking, int32_t timeout, int watchForResultId)
{
return allowAutoBlocking && blocking ? waitFor(id, timeout, watchForResultId) : id;
}
/// <summary>
/// Flushes this instance onto the serial port.
/// </summary>
void VirtualShield::flush()
{
_VShieldSerial->flush();
lastOpenRequest = millis();
}
long lastClosure = 0;
long lastOpening = 0;
bool inEvent = false;
/// <summary>
/// Gets zero or one available events for processing.
/// </summary>
/// <param name="shieldEvent">The address of ShieldEvent to populate.</param>
/// <returns>true if an event was populated</returns>
bool VirtualShield::getEvent(ShieldEvent* shieldEvent) {
bool hasEvent = false;
if (_VShieldSerial->available() == 0 && millis() > lastOpenRequest + requestInterval) //and timing!
{
#ifdef debugSerial
Serial.print(AWAITING_MESSAGE);
#endif
_VShieldSerial->write(AWAITING_MESSAGE[0]);
_VShieldSerial->write(AWAITING_MESSAGE[1]);
_VShieldSerial->flush();
lastOpenRequest = millis();
}
bool hadData = false;
while (!inEvent && _VShieldSerial->available() > 0) {
hadData = true;
char c = static_cast<char>(_VShieldSerial->read());
#ifdef debugSerialIn
Serial.print(c);
#endif
if (readBufferIndex < maxReadBuffer-1) {
readBuffer[readBufferIndex++] = c;
}
else
{
int i = readBufferIndex;
while (i >= 0)
{
if (readBuffer[i] == '{')
{
memmove(readBuffer, readBuffer + i, maxReadBuffer - i);
readBufferIndex = maxReadBuffer - i;
readBuffer[readBufferIndex++] = c;
bracketCount = 0;
for (int j = 0; j<readBufferIndex; j++)
{
if (readBuffer[j] == '{')
{
processInChar(shieldEvent, hasEvent, c);
}
}
break;
}
i--;
}
}
if (processInChar(shieldEvent, hasEvent, c))
{
break;
}
}
if (hadData)
{
lastOpenRequest = millis() - requestInterval + perMessageInterval;
}
if (lastClosure - lastOpening > requestInterval)
{
readBufferIndex = 0;
bracketCount = 0;
}
return hasEvent;
}
/// <summary>
/// Process single incoming character - pass to full handler when recognized
/// </summary>
/// <param name="shieldEvent">The shield event.</param>
bool VirtualShield::processInChar(ShieldEvent* shieldEvent, bool& hasEvent, char c)
{
if (c == '{') {
if (bracketCount++ == 0)
{
lastOpening = millis();
}
}
else if (c == '}') {
if (--bracketCount < 1) {
bracketCount = 0;
lastClosure = millis();
if (readBufferIndex < maxReadBuffer) {
readBuffer[readBufferIndex++] = '\0';
inEvent = true;
#ifdef debugSerial
Serial.print(AWAITING_MESSAGE);
#endif
_VShieldSerial->write(AWAITING_MESSAGE[0]);
_VShieldSerial->write(AWAITING_MESSAGE[1]);
_VShieldSerial->flush();
lastOpenRequest = millis();
onStringReceived(readBuffer, readBufferIndex, shieldEvent);
inEvent = false;
hasEvent = true;
readBufferIndex = 0;
return true;
}
readBufferIndex = 0;
}
}
return false;
}
/// <summary>
/// Sends the ping back form a ping request.
/// </summary>
/// <param name="shieldEvent">The shield event.</param>
void VirtualShield::sendStart()
{
EPtr eptrs[] = { EPtr(ACTION, START), EPtr(MemPtr, TYPE, "!"), EPtr(LEN, maxReadBuffer) };
writeAll(SERVICE_NAME_SERVICE, eptrs, 3);
}
/// <summary>
/// Sends the ping back form a ping request.
/// </summary>
/// <param name="shieldEvent">The shield event.</param>
void VirtualShield::sendPingBack(ShieldEvent *)
{
EPtr eptrs[] = { EPtr(ACTION, PONG), EPtr(MemPtr, TYPE, "!") };
writeAll(SERVICE_NAME_SERVICE, eptrs, 2);
}
/// <summary>
/// Event called when a valid json message was received.
/// Dispatches to added sensors that match the incoming Type.
/// </summary>
/// <param name="root">The root json object.</param>
/// <param name="shieldEvent">The shield event.</param>
void VirtualShield::onJsonReceived(JsonObject& root, ShieldEvent* shieldEvent) {
const char* sensorType = static_cast<const char *>(root["Type"]);
shieldEvent->tag = static_cast<const char*>(root["Tag"]);
shieldEvent->id = static_cast<int>(root["Pid"]);
if (shieldEvent->id == 0) {
shieldEvent->id = static_cast<int>(root["Id"]);
}
shieldEvent->resultId = static_cast<long>(root["ResultId"]);
shieldEvent->result = static_cast<const char *>(root["Result"]);
shieldEvent->resultHash = hash(shieldEvent->result);
shieldEvent->action = static_cast<const char *>(root["Action"]);
shieldEvent->actionHash = hash(shieldEvent->action);
shieldEvent->value = static_cast<double>(root["Value"]);
if (sensorType) {
// special '!' Type which means remote device just connected/reconnected
if (sensorType[0] == SYSTEM_EVENT)
{
shieldEvent->cargo = &root;
bool refresh = false;
switch (shieldEvent->resultHash)
{
case PING_HASH:
sendPingBack(shieldEvent);
break;
case REFRESH_HASH:
refresh = true;
break;
case CONNECT_HASH:
refresh = true;
if (onConnect)
{
onConnect(shieldEvent);
}
break;
case SUSPEND_HASH:
if (onSuspend)
{
onSuspend(shieldEvent);
}
break;
case RESUME_HASH:
refresh = true;
if (onResume)
{
onResume(shieldEvent);
}
break;
}
if (refresh && onRefresh)
{
onRefresh(shieldEvent);
}
}
else
{
const char sensorTypeChar = sensorType[0];
for (int i = 0; i < sensorCount; i++)
{
// check each sensor for matching Type
if (sensors[i]->sensorType == sensorTypeChar) {
sensors[i]->onJsonReceived(root, shieldEvent);
if (shieldEvent->shieldEventType == SensorShieldEventType) {
SensorEvent* sensorEvent = static_cast<SensorEvent*>(shieldEvent);
sensorEvent->sensor = sensors[i];
}
break;
}
}
}
}
if (onEvent)
{
onEvent(shieldEvent);
}
}
/// <summary>
/// Event callback for when a full json string is received.
/// </summary>
/// <param name="json">The json string.</param>
/// <param name="shieldEvent">The shield event to populate.</param>
void VirtualShield::onJsonStringReceived(char* json, ShieldEvent* shieldEvent) {
#if defined(_WINDOWS_) || defined(_WINRT_DLL)
DynamicJsonBuffer jsonBuffer;
#else
StaticJsonBuffer<maxJsonReadBuffer> jsonBuffer;
#endif
JsonObject& root = jsonBuffer.parseObject(json);
if (root.success()) {
onJsonReceived(root, shieldEvent);
}
else
{
//Serial.print("FAILED");
}
}
/// <summary>
/// Event callback for when a full string is received.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="length">The length.</param>
/// <param name="shieldEvent">The shield event.</param>
int VirtualShield::onStringReceived(char* buffer, size_t length, ShieldEvent* shieldEvent) {
if (length >= maxJsonReadBuffer) { return SERIAL_ERROR; }
char *json = static_cast<char*>(alloca(length + 1));
if (!json) { return SERIAL_ERROR; }
strncpy(json, buffer, length);
json[length] = '\0';
onJsonStringReceived(json, shieldEvent);
return SERIAL_SUCCESS;
}
/// <summary>
/// Receives events as long as they exist, or until an optional timeout occurs.
/// </summary>
/// <param name="watchForId">An id to return true if found. Otherwise true is returned for any events processed.</param>
/// <param name="timeout">The timeout in milliseconds.</param>
/// <returns>true if the id matched an incoming event, or if no id, any event.</returns>
bool VirtualShield::checkSensors(int watchForId, int32_t timeout, int watchForResultId) {
bool hadEvents = false;
uint32_t started = millis();
recentEventErrorId = 0;
while (getEvent(&recentEvent) && (timeout == 0 || started+timeout <= millis()) ) {
hadEvents = (watchForId == 0 || recentEvent.id == watchForId) && (watchForResultId == -1 || recentEvent.resultId == watchForResultId);
}
return hadEvents;
}
//------------------------------------------------------------//
/// <summary>
/// Writes the specified text to the communication channel.
/// </summary>
/// <param name="text">The text.</param>
void VirtualShield::write(const char* text)
{
#if defined(_WINRT_DLL)
_VShieldSerial->write(Platform::ArrayReference<uint8_t>(reinterpret_cast<uint8_t *>(const_cast<char *>(text)), strnlen(text, maxJsonWriteBuffer)));
#else
_VShieldSerial->write(text);
#endif
}
/// <summary>
/// Writes the service name only to the communication channel.
/// </summary>
/// <param name="serviceName">Name of the service.</param>
/// <returns>int.</returns>
int VirtualShield::writeAll(const char* serviceName) {
uint8_t id = beginWrite(serviceName);
if (endWrite() != SERIAL_SUCCESS) return SERIAL_ERROR;
return id;
}
/// <summary>
/// Blocks and awaits an event with an id.
/// </summary>
/// <param name="id">The id.</param>
/// <param name="timeout">The timeout.</param>
/// <returns>The matching id, or zero if not found.</returns>
int VirtualShield::waitFor(int id, uint32_t timeout, bool asSuccess, int resultId)
{
if (id < 0)
{
return id;
}
timeout = timeout + millis();
bool found = false;
while (!found && millis() < timeout) {
found = checkSensors(id, 0, resultId);
}
return found ? (asSuccess && id < 0 ? 0 : id) : 0;
}
/// <summary>
/// Returns true
/// </summary>
/// <param name="id">The id.</param>
/// <param name="timeout">The timeout.</param>
/// <returns>The matching id, or zero if not found.</returns>
bool VirtualShield::hasError(ShieldEvent* shieldEvent) {
return shieldEvent ? shieldEvent->resultId < 0 : recentEvent.resultId < 0;
}
/// <summary>
/// Begins a service write operation to the communication channel. Increments a message id, includes and returns the id.
/// </summary>
/// <param name="serviceName">Name of the service.</param>
/// <returns>The new id of the message or a negative error..</returns>
int VirtualShield::beginWrite(const char* serviceName)
{
int id = nextId++;
if (nextId < 0) //let's stay positive
{
nextId = 1;
}
if (sendFlashStringOnSerial(MESSAGE_SERVICE_START) != SERIAL_SUCCESS) return SERIAL_ERROR;
if (sendFlashStringOnSerial(serviceName) != SERIAL_SUCCESS) return SERIAL_ERROR;
if (sendFlashStringOnSerial(MESSAGE_SERVICE_TO_ID) != SERIAL_SUCCESS) return SERIAL_ERROR;
_VShieldSerial->print(id);
#ifdef debugSerial
Serial.print(id);
#endif
return id;
}
/// <summary>
/// Writes all EPtr values to the communication channel.
/// </summary>
/// <param name="serviceName">Name of the service.</param>
/// <param name="values">The values.</param>
/// <param name="count">The count of values.</param>
/// <returns>The new id of the message or a negative error.</returns>
int VirtualShield::writeAll(const char* serviceName, EPtr values[], unsigned int count, Attr extraAttributes[], int extraAttributeCount, const char sensorType) {
uint8_t id = beginWrite(serviceName);
for (size_t i = 0; i < count; i++)
{
write(values[i]);
}
if (sensorType)
{
write(EPtr(TYPE, sensorType));
}
for (int i = 0; i < extraAttributeCount; i++)
{
write(extraAttributes[i]);
}
if (endWrite() != SERIAL_SUCCESS) return SERIAL_ERROR;
return id;
}
/// <summary>
/// Writes the specified eptr.
/// </summary>
/// <param name="eptr">The eptr.</param>
/// <returns>Zero if no error, negative if an error.</returns>
int VirtualShield::write(EPtr eptr) const
{
if (eptr.ptrType == None)
{
return SERIAL_SUCCESS;
}
if (eptr.ptrType == ArrayEnd)
{
if (sendFlashStringOnSerial(ARRAY_END) != SERIAL_SUCCESS) return SERIAL_ERROR;
return SERIAL_SUCCESS;
}
if (isArrayStarted)
{
if (sendFlashStringOnSerial(MESSAGE_QUOTE) != SERIAL_SUCCESS) return SERIAL_ERROR;
isArrayStarted = false;
}
else
{
if (sendFlashStringOnSerial(MESSAGE_SEPARATOR) != SERIAL_SUCCESS) return SERIAL_ERROR;
}
if (eptr.keyIsMem)
{
#if defined(_WINRT_DLL)
_VShieldSerial->write(Platform::ArrayReference<uint8_t>(reinterpret_cast<uint8_t *>(const_cast<char *>(eptr.key)), strnlen(eptr.key, maxJsonWriteBuffer)));
#else
_VShieldSerial->print(eptr.key);
#ifdef debugSerial
Serial.print(eptr.key);
#endif
#endif
}
else
{
if (sendFlashStringOnSerial(eptr.key) != SERIAL_SUCCESS) return SERIAL_ERROR;
}
if (sendFlashStringOnSerial(MESSAGE_PAIR_SEPARATOR) != SERIAL_SUCCESS) return SERIAL_ERROR;
if (eptr.asText)
{
if (sendFlashStringOnSerial(MESSAGE_QUOTE) != SERIAL_SUCCESS) return SERIAL_ERROR;
}
writeValue(eptr);
if (eptr.asText)
{
if (sendFlashStringOnSerial(MESSAGE_QUOTE) != SERIAL_SUCCESS) return SERIAL_ERROR;
}
return SERIAL_SUCCESS;
}
int VirtualShield::writeValue(EPtr eptr, int start) const
{
int valueIndex = 0;
int formatPositionIndex = 0;
int result = SERIAL_SUCCESS;
switch (eptr.ptrType)
{
case ArrayStart:
result = sendFlashStringOnSerial(ARRAY_START);
isArrayStarted = true;
break;
case ProgPtr:
result = sendFlashStringOnSerial(eptr.value, start, true);
break;
case MemPtr:
{
const char* scanner = eptr.value;
scanner = eptr.value;
int count = eptr.length;
while (count == -1 ? scanner[0] : count-- > 0) {
if (!eptr.encoded && (scanner[0] == '\'' || scanner[0] == '\\' )) {
_VShieldSerial->write('\\');
#ifdef debugSerial
Serial.write('\\');
#endif
}
_VShieldSerial->write(scanner[0]);
#ifdef debugSerial
Serial.write(scanner[0]);
#endif
scanner++;
}
break;
}
case Char:
_VShieldSerial->write(eptr.charValue);
#ifdef debugSerial
Serial.print(eptr.charValue);
#endif
break;
case Int:
_VShieldSerial->print(eptr.intValue);
#ifdef debugSerial
Serial.print(eptr.intValue);
#endif
break;
case Uint:
_VShieldSerial->print(eptr.uintValue);
#ifdef debugSerial
Serial.print(eptr.uintValue);
#endif
break;
case Long:
_VShieldSerial->print(static_cast<int32_t>(eptr.longValue));
#ifdef debugSerial
Serial.print(eptr.longValue);
#endif
break;
case Double:
_VShieldSerial->print(eptr.doubleValue, 4);
#ifdef debugSerial
Serial.print(eptr.doubleValue, 4);
#endif
break;
case Bool:
_VShieldSerial->print(eptr.boolValue);
#ifdef debugSerial
Serial.print((bool)eptr.doubleValue);
#endif
break;
case Format:
//Serial.print(eptr.eptrs[1].doubleValue);
//assume format is from flash
while (valueIndex == 0 || formatPositionIndex > 0)
{
formatPositionIndex = writeValue(eptr.eptrs[0], formatPositionIndex);
if (formatPositionIndex == 0)
{
break;
}
result = writeValue(eptr.eptrs[++valueIndex], -1);
if (result != SERIAL_SUCCESS)
{
break;
}
}
break;
default:
break;
}
return result;
}
int VirtualShield::parseToHash(const char* text, unsigned int *hash, int hashCount, char separator, unsigned int length)
{
int index = 0;
int start = 0;
int hashIndex = 0;
int count = 0;
while ((length == static_cast<unsigned int>(-1) || length-- > 0) && (text[index] || index > start))
{
if (!text[index] || text[index] == separator || length == 0)
{
hash[hashIndex++] = VirtualShield::hash(text+start, index-start + (length == 0));
start = index + 1;
if (++count == hashCount || !text[index])
{
break;
}
}
index = index + 1;
}
return count;
}
// per Paul Larson - Microsoft Research
uint16_t VirtualShield::hash(const char* s, size_t len, unsigned int seed)
{
if (!s) { return seed; }
uint16_t hash = seed;
while ((len == static_cast<size_t>(-2)) ? *s : len-- > 0)
{
hash = hash * 101 + *s++;
}
return hash;
}
/// <summary>
/// Ends the write operation.
/// </summary>
/// <returns>Zero if no error, negative if an error.</returns>
int VirtualShield::endWrite()
{
if (sendFlashStringOnSerial(MESSAGE_END2) != SERIAL_SUCCESS) return SERIAL_ERROR;
this->flush();
return SERIAL_SUCCESS;
}
int VirtualShield::directToSerial(const char* cmd)
{
#if defined(_WINRT_DLL)
_VShieldSerial->write(Platform::ArrayReference<uint8_t>(reinterpret_cast<uint8_t *>(const_cast<char *>(cmd)), strnlen(cmd, maxJsonWriteBuffer)));
#else
_VShieldSerial->print(cmd);
#endif
return SERIAL_SUCCESS;
}
/// <summary>
/// Sends the flash (PROGMEM) string on the communication channel.
/// </summary>
/// <param name="flashStringAdr">The flash (PROGMEM) string address.</param>
/// <returns>Zero if no error, negative if an error.</returns>
int VirtualShield::sendFlashStringOnSerial(const char* flashStringAdr, int start, bool encode) const
{
char dataChar = '\0';
const size_t actualStart = start < 0 ? 0 : start;
const bool isFormatted = start > -1;
#if defined(_WINDOWS_) || defined(_WINRT_DLL)
for (size_t i = actualStart; i < strlen(flashStringAdr); i++)
{
dataChar = *(flashStringAdr + i);
#else
for (size_t i = actualStart; i < strlen_PF(uint_farptr_t(flashStringAdr)); i++)
{
dataChar = pgm_read_byte_near(flashStringAdr + i);
#endif
if (isFormatted && dataChar == '~')
{
return i + 1;
}
if (encode && dataChar == '\'')
{
_VShieldSerial->write('\\');
#ifdef debugSerial
Serial.write('\\');
#endif
}
_VShieldSerial->write((char)dataChar);
#ifdef debugSerial
Serial.print((char)dataChar);
#endif
}
return SERIAL_SUCCESS;
}