forked from blues/note-c
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn_helpers.c
More file actions
1943 lines (1781 loc) · 63.8 KB
/
n_helpers.c
File metadata and controls
1943 lines (1781 loc) · 63.8 KB
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* @file n_helpers.c
*
* Written by Ray Ozzie and Blues Inc. team.
*
* Copyright (c) 2019 Blues Inc. MIT License. Use of this source code is
* governed by licenses granted by the copyright holder including that found in
* the
* <a href="https://github.com/blues/note-c/blob/master/LICENSE">LICENSE</a>
* file.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include "n_lib.h"
// When interfacing with the Notecard, it is generally encouraged that the JSON object manipulation and
// calls to the note-arduino library are done directly at point of need. However, there are cases in which
// it's convenient to have a wrapper. The most common reason is when it's best to have a suppression timer
// on the actual call to the Notecard so as not to assault the I2C bus or UART on a continuing basis - the most
// common examples of this being "card.time" and any other Notecard state that needs to be polled for an app,
// such as the device location, its voltage level, and so on. This file contains this kind of wrapper,
// just implemented here as a convenience to all developers.
// Time-related suppression timer and cache
static uint32_t timeBaseSetAtMs = 0;
static JTIME timeBaseSec = 0;
static bool timeBaseSetManually = false;
static uint32_t suppressionTimerSecs = 10;
static uint32_t refreshTimerSecs = 86400;
static uint32_t timeTimer = 0;
static uint32_t timeRefreshTimer = 0;
static bool zoneStillUnavailable = true;
static bool zoneForceRefresh = false;
static char curZone[10] = {0};
static char curArea[64] = {0};
static char curCountry[8] = "";
static int curZoneOffsetMins = 0;
// Location-related suppression timer and cache
static uint32_t locationTimer = 0;
static char locationLastErr[64] = {0};
static bool locationValid = false;
// Connection-related suppression timer and cache
static uint32_t connectivityTimer = 0;
static bool cardConnected = false;
// Turbo communications mode, for special use cases and well-tested hardware
bool cardTurboIO = false;
// Service config-related suppression timer and cache
static uint32_t serviceConfigTimer = 0;
static char scDevice[128] = {0};
static char scSN[128] = {0};
static char scProduct[128] = {0};
static char scService[128] = {0};
// For date conversions
#define daysByMonth(y) ((y)&03||(y)==0?normalYearDaysByMonth:leapYearDaysByMonth)
static short leapYearDaysByMonth[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
static short normalYearDaysByMonth[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
static const char *daynames[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
// Forwards
static bool timerExpiredSecs(uint32_t *timer, uint32_t periodSecs);
static int ytodays(int year);
//**************************************************************************/
/*!
@brief Determine if the card time is "real" calendar/clock time, or if
it is simply the millisecond clock since boot.
@returns A boolean indicating whether the current time is valid.
*/
/**************************************************************************/
bool NoteTimeValid()
{
timeTimer = 0;
return NoteTimeValidST();
}
//**************************************************************************/
/*!
@brief Determine if if the suppression-timer card time is valid.
@returns A boolean indicating whether the current time is valid.
*/
/**************************************************************************/
bool NoteTimeValidST()
{
NoteTimeST();
return (timeBaseSec != 0);
}
//**************************************************************************/
/*!
@brief Get the current epoch time, unsuppressed.
@returns The current time.
*/
/**************************************************************************/
JTIME NoteTime()
{
timeTimer = 0;
return NoteTimeST();
}
//**************************************************************************/
/*!
@brief Set the number of minutes between refreshes of the time
from the Notecard, to help minimize clock drift on this host.
Set this to 0 for no auto-refresh; it defaults to daily.
@returns Nothing
*/
/**************************************************************************/
void NoteTimeRefreshMins(uint32_t mins)
{
refreshTimerSecs = mins * 60;
}
//**************************************************************************/
/*!
@brief Set the time.
@param seconds The UNIX Epoch time.
*/
/**************************************************************************/
static void setTime(JTIME seconds)
{
timeBaseSec = seconds;
timeBaseSetAtMs = _GetMs();
}
//**************************************************************************/
/*!
@brief Set the time from a source that is NOT the Notecard
@param seconds The UNIX Epoch time, or 0 to set back to automatic Notecard time
@param offset The local time zone offset, in minutes, to adjust UTC
@param zone The optional local time zone name (3 character c-string)
@param zone The optional country
@param area The optional region
*/
/**************************************************************************/
void NoteTimeSet(JTIME secondsUTC, int offset, char *zone, char *country, char *area)
{
if (secondsUTC == 0) {
timeBaseSec = 0;
timeBaseSetAtMs = 0;
timeBaseSetManually = false;
zoneStillUnavailable = true;
zoneForceRefresh = false;
} else {
timeBaseSec = secondsUTC;
timeBaseSetAtMs = _GetMs();
timeBaseSetManually = true;
zoneStillUnavailable = false;
curZoneOffsetMins = offset;
strlcpy(curZone, zone == NULL ? "UTC" : zone, sizeof(curZone));
strlcpy(curArea, area == NULL ? "" : area, sizeof(curArea));
strlcpy(curCountry, country == NULL ? "" : country, sizeof(curCountry));
}
}
//**************************************************************************/
/*!
@brief Print a full, newline-terminated line.
@param line The c-string line to print.
*/
/**************************************************************************/
void NotePrintln(const char *line)
{
NotePrint(line);
NotePrint(c_newline);
}
//**************************************************************************/
/*!
@brief Log text "raw" to either the active debug console or to the
Notecard's USB console.
@param text A debug string for output.
*/
/**************************************************************************/
bool NotePrint(const char *text)
{
bool success = false;
if (NoteIsDebugOutputActive()) {
NoteDebug(text);
return true;
}
int inLog = 0;
if (inLog++ != 0) {
inLog--;
return false;
}
J *req = NoteNewRequest("card.log");
JAddStringToObject(req, "text", text);
success = NoteRequest(req);
inLog--;
return success;
}
//**************************************************************************/
/*!
@brief Get the current epoch time as known by the module. If it isn't known
by the module, just return the time since boot as indicated by the
millisecond clock.
@returns The current time, or the time since boot.
*/
/**************************************************************************/
JTIME NoteTimeST()
{
// Handle timer tick wrap by resetting the base
uint32_t nowMs = _GetMs();
if (timeBaseSec != 0 && nowMs < timeBaseSetAtMs) {
int64_t actualTimeMs = 0x100000000LL + (int64_t) nowMs;
int64_t elapsedTimeMs = actualTimeMs - (int64_t) timeBaseSetAtMs;
uint32_t elapsedTimeSecs = (uint32_t) (elapsedTimeMs / 1000);
timeBaseSec += elapsedTimeSecs;
timeBaseSetAtMs = nowMs;
}
// If it's time to refresh the time, do so
if (refreshTimerSecs != 0 && timerExpiredSecs(&timeRefreshTimer, refreshTimerSecs)) {
timeTimer = 0;
}
// If we haven't yet fetched the time, or if we still need the timezone, do so with a suppression
// timer so that we don't hammer the module before it's had a chance to connect to the network to fetch time.
if (!timeBaseSetManually && (timeTimer == 0 || timeBaseSec == 0 || zoneStillUnavailable || zoneForceRefresh)) {
if (timerExpiredSecs(&timeTimer, suppressionTimerSecs)) {
// Request time and zone info from the card
J *rsp = NoteRequestResponse(NoteNewRequest("card.time"));
if (rsp != NULL) {
if (!NoteResponseError(rsp)) {
JTIME seconds = JGetInt(rsp, "time");
if (seconds != 0) {
// Set the time
setTime(seconds);
// Get the zone
char *z = JGetString(rsp, "zone");
if (z[0] != '\0') {
char zone[64];
strlcpy(zone, z, sizeof(zone));
// Only use the 3-letter abbrev
char *sep = strchr(zone, ',');
if (sep == NULL) {
zone[0] = '\0';
} else {
*sep = '\0';
}
zoneStillUnavailable = (memcmp(zone, "UTC", 3) == 0);
zoneForceRefresh = false;
strlcpy(curZone, zone, sizeof(curZone));
curZoneOffsetMins = JGetInt(rsp, "minutes");
strlcpy(curCountry, JGetString(rsp, "country"), sizeof(curCountry));
strlcpy(curArea, JGetString(rsp, "area"), sizeof(curArea));
}
}
}
NoteDeleteResponse(rsp);
}
}
}
// Adjust the base time by the number of seconds that have elapsed since the base.
JTIME adjustedTime = timeBaseSec + (int32_t) (((int64_t) nowMs - (int64_t) timeBaseSetAtMs) / 1000);
// Done
return adjustedTime;
}
//**************************************************************************/
/*!
@brief Set suppression timer secs, returning the previous value
@param New suppression timer seconds
@returns Previous suppression timer seconds
*/
/**************************************************************************/
uint32_t NoteSetSTSecs(uint32_t secs)
{
uint32_t prev = suppressionTimerSecs;
suppressionTimerSecs = secs;
return prev;
}
//**************************************************************************/
/*!
@brief Return local region info, if known. Returns true if valid.
@param retCountry (out) The country.
@param retArea (out) The area value.
@param retZone (out) The timezone value.
@param retZoneOffset (out) The timezone offset.
@returns boolean indicating if the region information is valid.
*/
/**************************************************************************/
bool NoteRegion(char **retCountry, char **retArea, char **retZone, int *retZoneOffset)
{
NoteTimeST();
if (zoneStillUnavailable) {
if (retCountry != NULL) {
*retCountry = (char *) "";
}
if (retArea != NULL) {
*retArea = (char *) "";
}
if (retZone != NULL) {
*retZone = (char *) "UTC";
}
if (retZoneOffset != NULL) {
*retZoneOffset = 0;
}
return false;
}
if (retCountry != NULL) {
*retCountry = curCountry;
}
if (retArea != NULL) {
*retArea = curArea;
}
if (retZone != NULL) {
*retZone = curZone;
}
if (retZoneOffset != NULL) {
*retZoneOffset = curZoneOffsetMins;
}
return true;;
}
//**************************************************************************/
/*!
@brief Return local region info, if known. Returns true if valid.
@param year, month, day, hour, minute, second - pointers to where to return time/date
@param retZone (in-out) if NULL, local time will be returned in UTC, else returns pointer to zone string
@returns boolean indicating if either the zone or DST have changed since last call
@note only call this if time is valid
*/
/**************************************************************************/
bool NoteLocalTimeST(uint16_t *retYear, uint8_t *retMonth, uint8_t *retDay, uint8_t *retHour, uint8_t *retMinute, uint8_t *retSecond, char **retWeekday, char **retZone)
{
// Preset
if (retYear != NULL) {
*retYear = 0;
}
if (retMonth != NULL) {
*retMonth = 0;
}
if (retDay != NULL) {
*retDay = 0;
}
if (retHour != NULL) {
*retHour = 0;
}
if (retMinute != NULL) {
*retMinute = 0;
}
if (retSecond != NULL) {
*retSecond = 0;
}
if (retWeekday != NULL) {
*retWeekday = (char *) "";
}
if (retZone != NULL) {
*retZone = (char *) "";
}
// Exit if time isn't yet valid
if (!NoteTimeValidST()) {
return false;
}
// Get the current time and region, and compute local time
char *currentZone;
int currentOffsetMins;
JTIME currentEpochTime = NoteTimeST();
bool regionAvailable = NoteRegion(NULL, NULL, ¤tZone, ¤tOffsetMins);
// Offset the epoch time by time zone offset
currentEpochTime += currentOffsetMins * 60;
// Convert from unix epoch time to local date/time
int i, year, mon;
int32_t days;
uint32_t secs;
secs = (uint32_t) currentEpochTime + ((70*365L+17)*86400LU);
days = secs / 86400;
if (retWeekday != NULL) {
*retWeekday = (char *) daynames[(days + 1) % 7];
}
for (year = days / 365; days < (i = ytodays(year) + 365L * year); ) {
--year;
}
days -= i;
if (retYear != NULL) {
*retYear = (uint16_t) year+1900;
}
short *pm = daysByMonth(year);
for (mon = 12; days < pm[--mon]; ) ;
if (retMonth != NULL) {
*retMonth = (uint8_t) mon+1;
}
if (retDay != NULL) {
*retDay = days - pm[mon] + 1;
}
secs = secs % 86400;
uint8_t currentHour = (uint8_t) (secs / 3600);
if (retHour != NULL) {
*retHour = currentHour;
}
secs %= 3600;
if (retMinute != NULL) {
*retMinute = (uint8_t) (secs/60);
}
if (retSecond != NULL) {
*retSecond = (uint8_t) (secs % 60);
}
if (retZone != NULL) {
*retZone = currentZone;
}
// Determine whether or not we should refresh to check whether DST offset has changed, which
// is between midnight and 3am local time (if available).
static uint8_t lastHour;
static bool lastInfoIsKnown = false;
if (regionAvailable && lastInfoIsKnown) {
if ((lastHour != 1 && currentHour == 1) || (lastHour != 2 && currentHour == 2) || (lastHour != 3 && currentHour == 3)) {
zoneForceRefresh = true;
}
}
lastInfoIsKnown = true;
lastHour = currentHour;
// Done
return regionAvailable;
}
// Figure out how many days at start of the year
static int ytodays(int year)
{
int days = 0;
if (0 < year) {
days = (year - 1) / 4;
} else if (year <= -4) {
days = year / 4;
}
return days + daysByMonth(year)[0];
}
//**************************************************************************/
/*!
@brief See if the card location is valid. If it is OFF, it is
treated as valid.
@param errbuf (out) A buffer with any error information to return.
@param errbuflen The length of the error buffer.
@returns boolean indicating if the location information is valid.
*/
/**************************************************************************/
bool NoteLocationValid(char *errbuf, uint32_t errbuflen)
{
locationTimer = 0;
locationValid = false;
locationLastErr[0] = '\0';
return NoteLocationValidST(errbuf, errbuflen);
}
//**************************************************************************/
/*!
@brief See if the card location is valid, time-suppressed. If it is OFF,
it is treated as valid.
@param errbuf (out) A buffer with any error information to return.
@param errbuflen The length of the error buffer.
@returns boolean indicating if the location information is valid.
*/
/**************************************************************************/
bool NoteLocationValidST(char *errbuf, uint32_t errbuflen)
{
// Preset
if (errbuf != NULL) {
strlcpy(errbuf, locationLastErr, errbuflen);
}
// If it was ever valid, return true
if (locationValid) {
locationLastErr[0] = '\0';
if (errbuf != NULL) {
*errbuf = '\0';
}
return true;
}
// If we haven't yet fetched the location, do so with a suppression
// timer so that we don't hammer the module before it's had a chance to
// connect to the gps to fetch location.
if (!timerExpiredSecs(&locationTimer, suppressionTimerSecs)) {
return false;
}
// Request location from the card
J *rsp = NoteRequestResponse(NoteNewRequest("card.location"));
if (rsp == NULL) {
return false;
}
// If valid, or the location mode is OFF, we're done
if (!NoteResponseError(rsp) || strcmp(JGetString(rsp, "mode"), "off") == 0) {
NoteDeleteResponse(rsp);
locationValid = true;
locationLastErr[0] = '\0';
if (errbuf != NULL) {
*errbuf = '\0';
}
return true;
}
// Remember the error for next iteration
strlcpy(locationLastErr, JGetString(rsp, "err"), sizeof(locationLastErr));
if (errbuf != NULL) {
strlcpy(errbuf, locationLastErr, errbuflen);
}
NoteDeleteResponse(rsp);
return false;
}
//**************************************************************************/
/*!
@brief Set a service environment variable default string.
@param variable The variable key.
@param buf The variable value.
@returns boolean indicating if variable was set.
*/
/**************************************************************************/
bool NoteSetEnvDefault(const char *variable, char *buf)
{
bool success = false;
J *req = NoteNewRequest("env.default");
if (req != NULL) {
JAddStringToObject(req, "name", variable);
JAddStringToObject(req, "text", buf);
success = NoteRequest(req);
}
return success;
}
//**************************************************************************/
/*!
@brief Set a service environment variable default integer.
@param variable The variable key.
@param defaultVal The variable value.
@returns boolean indicating if variable was set.
*/
/**************************************************************************/
bool NoteSetEnvDefaultInt(const char *variable, long int defaultVal)
{
char buf[32];
JItoA(defaultVal, buf);
return NoteSetEnvDefault(variable, buf);
}
//**************************************************************************/
/*!
@brief Set a service environment variable default number.
@param variable The variable key.
@param defaultVal The variable value.
@returns boolean indicating if variable was set.
*/
/**************************************************************************/
bool NoteSetEnvDefaultNumber(const char *variable, JNUMBER defaultVal)
{
char buf[JNTOA_MAX];
JNtoA(defaultVal, buf, -1);
return NoteSetEnvDefault(variable, buf);
}
//**************************************************************************/
/*!
@brief Get a service environment variable number.
@param variable The variable key.
@param defaultVal The default variable value.
@returns environment variable value.
*/
/**************************************************************************/
JNUMBER NoteGetEnvNumber(const char *variable, JNUMBER defaultVal)
{
char buf[JNTOA_MAX], buf2[JNTOA_MAX];;
JNtoA(defaultVal, buf2, -1);
NoteGetEnv(variable, buf2, buf, sizeof(buf));
return JAtoN((const char*)buf, NULL);
}
//**************************************************************************/
/*!
@brief Get a service environment variable integer.
@param variable The variable key.
@param defaultVal The default variable value.
@returns environment variable value.
*/
/**************************************************************************/
long int NoteGetEnvInt(const char *variable, long int defaultVal)
{
char buf[32], buf2[32];;
JItoA(defaultVal, buf2);
NoteGetEnv(variable, buf2, buf, sizeof(buf));
return atoi(buf);
}
//**************************************************************************/
/*!
@brief Get a service environment variable.
@param variable The variable key.
@param defaultVal The variable value.
@param buf (out) The buffer in which to place the variable value.
@param buflen The length of the output buffer.
@returns true if there is no error (JSON response with no explicit error)
*/
/**************************************************************************/
bool NoteGetEnv(const char *variable, const char *defaultVal, char *buf, uint32_t buflen)
{
bool success = false;
if (defaultVal == NULL) {
buf[0] = '\0';
} else {
strlcpy(buf, defaultVal, buflen);
}
J *req = NoteNewRequest("env.get");
if (req != NULL) {
JAddStringToObject(req, "name", variable);
J *rsp = NoteRequestResponse(req);
if (rsp != NULL) {
if (!NoteResponseError(rsp)) {
success = true;
char *val = JGetString(rsp, "text");
if (val[0] != '\0') {
strlcpy(buf, val, buflen);
}
}
NoteDeleteResponse(rsp);
}
}
return success;
}
//**************************************************************************/
/*!
@brief Determine if the Notecard is connected to the network.
@returns boolean. `true` if connected.
*/
/**************************************************************************/
bool NoteIsConnected()
{
connectivityTimer = 0;
return NoteIsConnectedST();
}
//**************************************************************************/
/*!
@brief Determine if the Notecard is connected to the network.
@returns boolean. `true` if connected.
*/
/**************************************************************************/
bool NoteIsConnectedST()
{
if (timerExpiredSecs(&connectivityTimer, suppressionTimerSecs)) {
J *rsp = NoteRequestResponse(NoteNewRequest("hub.status"));
if (rsp != NULL) {
if (!NoteResponseError(rsp)) {
cardConnected = JGetBool(rsp, "connected");
}
NoteDeleteResponse(rsp);
}
}
return cardConnected;
}
//**************************************************************************/
/*!
@brief Get Full Network Status via `hub.status`.
@param statusBuf (out) a buffer to populate with the status response.
@param statusBufLen The length of the status buffer
@returns boolean. `true` if the status was retrieved.
*/
/**************************************************************************/
bool NoteGetNetStatus(char *statusBuf, int statusBufLen)
{
bool success = false;
statusBuf[0] = '\0';
J *rsp = NoteRequestResponse(NoteNewRequest("hub.status"));
if (rsp != NULL) {
success = !NoteResponseError(rsp);
if (success) {
strlcpy(statusBuf, JGetString(rsp, "status"), statusBufLen);
}
NoteDeleteResponse(rsp);
}
return success;
}
//**************************************************************************/
/*!
@brief Make a `card.version` request
@param versionBuf (out) a buffer to populate with the version response.
@param versionBufLen The length of the version buffer
@returns boolean. `true` if the version information was retrieved.
*/
/**************************************************************************/
bool NoteGetVersion(char *versionBuf, int versionBufLen)
{
bool success = false;
versionBuf[0] = '\0';
J *rsp = NoteRequestResponse(NoteNewRequest("card.version"));
if (rsp != NULL) {
success = !NoteResponseError(rsp);
if (success) {
strlcpy(versionBuf, JGetString(rsp, "version"), versionBufLen);
}
NoteDeleteResponse(rsp);
}
return success;
}
//**************************************************************************/
/*!
@brief Make a `card.location` request
@param retLat (out) The retrieved latitude value.
@param retLon (out) The retrieved longitude value.
@param time (out) The retrieved time.
@param statusBuf (out) a buffer to populate with the location response.
@param statusBufLen The length of the location buffer
@returns boolean. `true` if the location information was retrieved.
*/
/**************************************************************************/
bool NoteGetLocation(JNUMBER *retLat, JNUMBER *retLon, JTIME *time, char *statusBuf, int statusBufLen)
{
bool locValid = false;
if (statusBuf != NULL) {
*statusBuf = '\0';
}
if (retLat != NULL) {
*retLat = 0.0;
}
if (retLon != NULL) {
*retLon = 0.0;
}
if (time != NULL) {
*time = 0;
}
J *rsp = NoteRequestResponse(NoteNewRequest("card.location"));
if (rsp != NULL) {
if (statusBuf != NULL) {
strlcpy(statusBuf, JGetString(rsp, "err"), statusBufLen);
}
if (JIsPresent(rsp, "lat") && JIsPresent(rsp, "lon")) {
if (retLat != NULL) {
*retLat = JGetNumber(rsp, "lat");
}
if (retLon != NULL) {
*retLon = JGetNumber(rsp, "lon");
}
locValid = true;
}
JTIME seconds = JGetInt(rsp, "time");
if (seconds != 0 && time != NULL) {
*time = seconds;
}
NoteDeleteResponse(rsp);
}
return locValid;
}
//**************************************************************************/
/*!
@brief Set the card location to a static value.
@param lat The latitude value to set on the Notecard.
@param lon The longitude value to set on the Notecard.
@returns boolean. `true` if the location information was set.
*/
/**************************************************************************/
bool NoteSetLocation(JNUMBER lat, JNUMBER lon)
{
bool success = false;
J *req = NoteNewRequest("card.location.mode");
if (req != NULL) {
JAddStringToObject(req, "mode", "fixed");
JAddNumberToObject(req, "lat", lat);
JAddNumberToObject(req, "lon", lon);
success = NoteRequest(req);
}
return success;
}
//**************************************************************************/
/*!
@brief Clear last known Location.
@returns boolean. `true` if the location information was cleared.
*/
/**************************************************************************/
bool NoteClearLocation()
{
bool success = false;
J *req = NoteNewRequest("card.location.mode");
if (req != NULL) {
JAddBoolToObject(req, "delete", true);
success = NoteRequest(req);
}
return success;
}
//**************************************************************************/
/*!
@brief Get the current location Mode.
@param modeBuf (out) a buffer to populate with the location mode response.
@param modeBufLen The length of the mode buffer
@returns boolean. `true` if the mode information was retrieved.
*/
/**************************************************************************/
bool NoteGetLocationMode(char *modeBuf, int modeBufLen)
{
bool success = false;
modeBuf[0] = '\0';
J *rsp = NoteRequestResponse(NoteNewRequest("card.location.mode"));
if (rsp != NULL) {
success = !NoteResponseError(rsp);
if (success) {
strlcpy(modeBuf, JGetString(rsp, "mode"), modeBufLen);
}
NoteDeleteResponse(rsp);
}
return success;
}
//**************************************************************************/
/*!
@brief Make a `card.location.mode` request.
@param mode the Notecard location mode to set.
@param seconds The value of the `seconds` field for the request.
@returns boolean. `true` if the mode was set.
*/
/**************************************************************************/
bool NoteSetLocationMode(const char *mode, uint32_t seconds)
{
bool success = false;
J *req = NoteNewRequest("card.location.mode");
if (req != NULL) {
if (mode[0] == '\0') {
mode = "-";
}
JAddStringToObject(req, "mode", mode);
JAddNumberToObject(req, "seconds", seconds);
success = NoteRequest(req);
}
return success;
}
//**************************************************************************/
/*!
@brief Get the current service configuration information, unsuppressed.
@param productBuf (out) a buffer to populate with the ProductUID from the
response.
@param productBufLen The length of the ProductUID buffer.
@param serviceBuf (out) a buffer to populate with the host url from the
response.
@param serviceBufLen The length of the host buffer.
@param deviceBuf (out) a buffer to populate with the DeviceUID from the
response.
@param deviceBufLen The length of the DeviceUID buffer.
@param snBuf (out) a buffer to populate with the Product Serial Number
from the response.
@param snBufLen The length of the Serial Number buffer.
@returns boolean. `true` if the service configuration was obtained.
*/
/**************************************************************************/
bool NoteGetServiceConfig(char *productBuf, int productBufLen, char *serviceBuf, int serviceBufLen, char *deviceBuf, int deviceBufLen, char *snBuf, int snBufLen)
{
serviceConfigTimer = 0;
return NoteGetServiceConfigST(productBuf, productBufLen, serviceBuf, serviceBufLen, deviceBuf, deviceBufLen, snBuf, snBufLen);
}
//**************************************************************************/
/*!
@brief Get the current service configuration information, with
a supression timer.
@param productBuf (out) a buffer to populate with the ProductUID from the
response.
@param productBufLen The length of the ProductUID buffer.
@param serviceBuf (out) a buffer to populate with the host url from the
response.
@param serviceBufLen The length of the host buffer.
@param deviceBuf (out) a buffer to populate with the DeviceUID from the
response.
@param deviceBufLen The length of the DeviceUID buffer.
@param snBuf (out) a buffer to populate with the Product Serial Number
from the response.
@param snBufLen The length of the Serial Number buffer.
@returns boolean. `true` if the service configuration was obtained.
*/
/**************************************************************************/
bool NoteGetServiceConfigST(char *productBuf, int productBufLen, char *serviceBuf, int serviceBufLen, char *deviceBuf, int deviceBufLen, char *snBuf, int snBufLen)
{
bool success = false;
// Use cache except for a rare refresh
if (scProduct[0] == '\0' || scDevice[0] == '\0' || timerExpiredSecs(&serviceConfigTimer, 4*60*60)) {
J *rsp = NoteRequestResponse(NoteNewRequest("hub.get"));
if (rsp != NULL) {
success = !NoteResponseError(rsp);
if (success) {
strlcpy(scProduct, JGetString(rsp, "product"), sizeof(scProduct));
strlcpy(scService, JGetString(rsp, "host"), sizeof(scService));
strlcpy(scDevice, JGetString(rsp, "device"), sizeof(scDevice));
strlcpy(scSN, JGetString(rsp, "sn"), sizeof(scSN));
}
NoteDeleteResponse(rsp);
}
} else {
success = true;
}
// Done
if (productBuf != NULL) {
strlcpy(productBuf, scProduct, productBufLen);
}
if (serviceBuf != NULL) {
strlcpy(serviceBuf, scService, serviceBufLen);
}
if (deviceBuf != NULL) {
strlcpy(deviceBuf, scDevice, deviceBufLen);
}
if (snBuf != NULL) {
strlcpy(snBuf, scSN, snBufLen);
}
return success;
}
//**************************************************************************/
/*!
@brief Get Status of the Notecard, unsuppressed.
@param statusBuf (out) a buffer to populate with the Notecard status
from the response.
@param statusBufLen The length of the status buffer.
@param bootTime (out) The Notecard boot time.
@param retUSB (out) Whether the Notecard is powered over USB.
@param retSignals (out) Whether the Notecard has a network signal.
@returns boolean. `true` if the card status was obtained.
*/
/**************************************************************************/
bool NoteGetStatus(char *statusBuf, int statusBufLen, JTIME *bootTime, bool *retUSB, bool *retSignals)
{
bool success = false;
if (statusBuf != NULL) {
statusBuf[0] = '\0';
}
if (bootTime != NULL) {
*bootTime = 0;
}
if (retUSB != NULL) {
*retUSB = false;
}
if (retSignals != NULL) {
*retSignals = false;
}
J *rsp = NoteRequestResponse(NoteNewRequest("card.status"));
if (rsp != NULL) {
success = !NoteResponseError(rsp);
if (success) {
if (statusBuf != NULL) {
strlcpy(statusBuf, JGetString(rsp, "status"), statusBufLen);
}
if (bootTime != NULL) {
*bootTime = JGetInt(rsp, "time");
}
if (retUSB != NULL) {
*retUSB = JGetBool(rsp, "usb");
}
if (retSignals != NULL && JGetBool(rsp, "connected")) {
*retSignals = (JGetInt(rsp, "signals") > 0);
}
}
NoteDeleteResponse(rsp);
}
return success;
}
//**************************************************************************/
/*!
@brief Get Status of the Notecard, with a supression timer.
@param statusBuf (out) a buffer to populate with the Notecard status
from the response.
@param statusBufLen The length of the status buffer.
@param bootTime (out) The Notecard boot time.
@param retUSB (out) Whether the Notecard is powered over USB.
@param retSignals (out) Whether the Notecard has a network signal.
@returns boolean. `true` if the card status was obtained.
*/