Skip to content

Commit fff7740

Browse files
authored
fix: Update note-c (#91)
* chore: Update .gitignore * remove note-c before re-add * Squashed 'src/note-c/' content from commit e8007a7 git-subtree-dir: src/note-c git-subtree-split: e8007a770122b0e2232288df04c51904ad12635d * chore: update `note-c`
1 parent f82ece2 commit fff7740

File tree

8 files changed

+122
-14
lines changed

8 files changed

+122
-14
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*.gcov
77
vgcore.*
88

9+
.vscode/
10+
build/
911
coverage/
1012

1113
Doxyfile*

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Blues Wireless Notecard
2-
version=1.3.16
2+
version=1.3.17
33
author=Blues Wireless
44
maintainer=Blues Wireless <[email protected]>
55
sentence=An easy to use Notecard Library for Arduino.

src/note-c/n_cjson_helpers.c

+69
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,72 @@ const char *JType(J *item)
514514
}
515515
return "invalid";
516516
}
517+
518+
//**************************************************************************/
519+
/*!
520+
@brief Return the type of an item, as an int usable in a switch statement
521+
@param json object
522+
@param field within the json object
523+
@returns The type
524+
*/
525+
/**************************************************************************/
526+
int JGetType(J *rsp, const char *field)
527+
{
528+
const char *v;
529+
if (rsp == NULL || field == NULL) {
530+
return JTYPE_NOT_PRESENT;
531+
}
532+
J *item = JGetObjectItem(rsp, field);
533+
if (item == NULL) {
534+
return JTYPE_NOT_PRESENT;
535+
}
536+
switch (item->type & 0xff) {
537+
case JTrue:
538+
return JTYPE_BOOL_TRUE;
539+
case JFalse:
540+
return JTYPE_BOOL_FALSE;
541+
case JNULL:
542+
return JTYPE_NULL;
543+
case JNumber:
544+
if (item->valueint == 0 && item->valuenumber == 0) {
545+
return JTYPE_NUMBER_ZERO;
546+
}
547+
return JTYPE_NUMBER;
548+
case JRaw:
549+
case JString:
550+
v = item->valuestring;
551+
if (v == NULL || v[0] == 0) {
552+
return JTYPE_STRING_BLANK;
553+
}
554+
int vlen = strlen(v);
555+
char *endstr;
556+
JNUMBER value = JAtoN(v, &endstr);
557+
if (endstr[0] == 0) {
558+
if (value == 0) {
559+
return JTYPE_STRING_ZERO;
560+
}
561+
return JTYPE_STRING_NUMBER;
562+
}
563+
if (vlen == 4 && (
564+
(v[0] == 't' || v[0] == 'T')
565+
&& (v[1] == 'r' || v[1] == 'R')
566+
&& (v[2] == 'u' || v[2] == 'U')
567+
&& (v[3] == 'e' || v[3] == 'E'))) {
568+
return JTYPE_STRING_BOOL_TRUE;
569+
}
570+
if (vlen == 5 && (
571+
(v[0] == 'f' || v[0] == 'F')
572+
&& (v[1] == 'a' || v[1] == 'A')
573+
&& (v[2] == 'l' || v[2] == 'L')
574+
&& (v[3] == 's' || v[3] == 'S')
575+
&& (v[4] == 'e' || v[4] == 'E'))) {
576+
return JTYPE_STRING_BOOL_FALSE;
577+
}
578+
return JTYPE_STRING;
579+
case JObject:
580+
return JTYPE_OBJECT;
581+
case JArray:
582+
return JTYPE_ARRAY;
583+
}
584+
return JTYPE_NOT_PRESENT;
585+
}

src/note-c/n_edge.h

+13
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,26 @@ typedef struct {
6363
char orientation[20];
6464
#define MOTIONPOINT_MOTION_COUNT "motion"
6565
uint32_t motionCount;
66+
#define MOTIONPOINT_TILT_COUNT "tilt"
67+
uint32_t tiltCount;
6668
} MotionPoint;
6769

70+
// LogData is the data structure that we use when capturing hub.log information
71+
typedef struct {
72+
#define LOGDATA_MESSAGE "message"
73+
char message[256];
74+
#define LOGDATA_METHOD "method"
75+
char method[64];
76+
#define LOGDATA_ALERT "alert"
77+
bool alert;
78+
} LogData;
79+
6880
// Format of the edge entry which is the dequeued note body
6981
#define EDGE_NOTEFILE "_edge.qi"
7082
#define EDGETYPE_SCAN "scan"
7183
#define EDGETYPE_TRACKPOINT "track"
7284
#define EDGETYPE_MOTIONPOINT "motion"
85+
#define EDGETYPE_LOG "log"
7386
typedef struct {
7487
#define EDGE_TYPE "type"
7588
char edgeType[32];

src/note-c/n_i2c.c

+16-12
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,28 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
5757
memcpy(transmitBuf, json, jsonLen);
5858
transmitBuf[jsonLen++] = '\n';
5959

60+
// Lock over the entire transaction
61+
_LockI2C();
62+
6063
// Transmit the request in chunks, but also in segments so as not to overwhelm the notecard's interrupt buffers
6164
const char *estr;
6265
uint8_t *chunk = transmitBuf;
6366
uint32_t sentInSegment = 0;
6467
while (jsonLen > 0) {
6568
int chunklen = (uint8_t) (jsonLen > (int)_I2CMax() ? (int)_I2CMax() : jsonLen);
66-
_LockI2C();
6769
_DelayIO();
6870
estr = _I2CTransmit(_I2CAddress(), chunk, chunklen);
6971
if (estr != NULL) {
7072
_Free(transmitBuf);
7173
_I2CReset(_I2CAddress());
72-
_UnlockI2C();
7374
#ifdef ERRDBG
7475
_Debug("i2c transmit: ");
7576
_Debug(estr);
7677
_Debug("\n");
7778
#endif
79+
_UnlockI2C();
7880
return estr;
7981
}
80-
_UnlockI2C();
8182
chunk += chunklen;
8283
jsonLen -= chunklen;
8384
sentInSegment += chunklen;
@@ -97,6 +98,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
9798

9899
// If no reply expected, we're done
99100
if (jsonResponse == NULL) {
101+
_UnlockI2C();
100102
return NULL;
101103
}
102104

@@ -110,6 +112,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
110112
#ifdef ERRDBG
111113
_Debug("transaction: jsonbuf malloc failed\n");
112114
#endif
115+
_UnlockI2C();
113116
return ERRSTR("insufficient memory",c_mem);
114117
}
115118

@@ -134,6 +137,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
134137
_Debug("transaction: jsonbuf grow malloc failed\n");
135138
#endif
136139
_Free(jsonbuf);
140+
_UnlockI2C();
137141
return ERRSTR("insufficient memory",c_mem);
138142
}
139143
memcpy(jsonbufNew, jsonbuf, jsonbufLen);
@@ -143,15 +147,14 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
143147

144148
// Read the chunk
145149
uint32_t available;
146-
_LockI2C();
147150
_DelayIO();
148151
const char *err = _I2CReceive(_I2CAddress(), (uint8_t *) &jsonbuf[jsonbufLen], chunklen, &available);
149-
_UnlockI2C();
150152
if (err != NULL) {
151153
_Free(jsonbuf);
152154
#ifdef ERRDBG
153155
_Debug("i2c receive error\n");
154156
#endif
157+
_UnlockI2C();
155158
return err;
156159
}
157160

@@ -184,6 +187,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
184187
#ifdef ERRDBG
185188
_Debug("reply to request didn't arrive from module in time\n");
186189
#endif
190+
_UnlockI2C();
187191
return ERRSTR("request or response was lost {io}",c_iotimeout);
188192
}
189193

@@ -194,6 +198,9 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
194198

195199
}
196200

201+
// Done with the bus
202+
_UnlockI2C();
203+
197204
// Null-terminate it, using the +1 space that we'd allocated in the buffer
198205
jsonbuf[jsonbufLen] = '\0';
199206

@@ -215,21 +222,19 @@ bool i2cNoteReset()
215222
// Reset the I2C subsystem and exit if failure
216223
_LockI2C();
217224
bool success = _I2CReset(_I2CAddress());
218-
_UnlockI2C();
219225
if (!success) {
226+
_UnlockI2C();
220227
return false;
221228
}
222229

223230
// Synchronize by guaranteeing not only that I2C works, but that after we send \n that we drain
224231
// the remainder of any pending partial reply from a previously-aborted session.
225232
// If we get a failure on transmitting the \n, it means that the notecard isn't even present.
226-
_LockI2C();
227233
_DelayIO();
228234
const char *transmitErr = _I2CTransmit(_I2CAddress(), (uint8_t *)"\n", 1);
229235
if (!cardTurboIO) {
230236
_DelayMs(CARD_REQUEST_I2C_SEGMENT_DELAY_MS);
231237
}
232-
_UnlockI2C();
233238

234239
// This outer loop does retries on I2C error, and is simply here for robustness.
235240
bool notecardReady = false;
@@ -245,10 +250,8 @@ bool i2cNoteReset()
245250
uint8_t buffer[128];
246251
chunklen = (chunklen > (int)sizeof(buffer)) ? (int)sizeof(buffer) : chunklen;
247252
chunklen = (chunklen > (int)_I2CMax()) ? (int)_I2CMax() : chunklen;
248-
_LockI2C();
249253
_DelayIO();
250254
const char *err = _I2CReceive(_I2CAddress(), buffer, chunklen, &available);
251-
_UnlockI2C();
252255
if (err) {
253256
break;
254257
}
@@ -273,12 +276,13 @@ bool i2cNoteReset()
273276

274277
// Reinitialize i2c if there's no response
275278
if (!notecardReady) {
276-
_LockI2C();
277279
_I2CReset(_I2CAddress());
278-
_UnlockI2C();
279280
_Debug(ERRSTR("notecard not responding\n", "no notecard\n"));
280281
}
281282

283+
// Done with the bus
284+
_UnlockI2C();
285+
282286
// Done
283287
return notecardReady;
284288
}

src/note-c/n_request.c

+2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ bool NoteRequestWithRetry(J *req, uint32_t timeoutSeconds)
161161
// Free error response
162162
if (rsp != NULL) {
163163
JDelete(rsp);
164+
rsp = NULL;
164165
}
165166
} else {
166167

@@ -251,6 +252,7 @@ J *NoteRequestResponseWithRetry(J *req, uint32_t timeoutSeconds)
251252
// Free error response
252253
if (rsp != NULL) {
253254
JDelete(rsp);
255+
rsp = NULL;
254256
}
255257
} else {
256258

src/note-c/n_ua.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ __attribute__((weak)) J *NoteUserAgent()
119119
n_cpu_name = (char *) "stm32f4";
120120
#elif defined(ARDUINO_ARCH_STM32G0)
121121
n_cpu_name = (char *) "stm32g0";
122+
#elif defined(ARDUINO_SWAN_R5)
123+
n_cpu_name = (char *) "swan_r5";
122124
#elif defined(ARDUINO_ARCH_STM32L4)
123-
n_cpu_name = (char *) "stm32f4";
125+
n_cpu_name = (char *) "stm32l4";
124126
#elif defined(ARDUINO_ARCH_STM32U5)
125127
n_cpu_name = (char *) "stm32u5";
126128
#elif defined(ARDUINO_ARCH_STM32)

src/note-c/note.h

+16
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,22 @@ const char *JGetItemName(const J * item);
193193
char *JAllocString(uint8_t *buffer, uint32_t len);
194194
const char *JType(J *item);
195195

196+
#define JTYPE_NOT_PRESENT 0
197+
#define JTYPE_BOOL_TRUE 1
198+
#define JTYPE_BOOL_FALSE 2
199+
#define JTYPE_NULL 3
200+
#define JTYPE_NUMBER_ZERO 4
201+
#define JTYPE_NUMBER 5
202+
#define JTYPE_STRING_BLANK 6
203+
#define JTYPE_STRING_ZERO 7
204+
#define JTYPE_STRING_NUMBER 8
205+
#define JTYPE_STRING_BOOL_TRUE 9
206+
#define JTYPE_STRING_BOOL_FALSE 10
207+
#define JTYPE_STRING 11
208+
#define JTYPE_OBJECT 12
209+
#define JTYPE_ARRAY 13
210+
int JGetType(J *rsp, const char *field);
211+
196212
// Helper functions for apps that wish to limit their C library dependencies
197213
#define JNTOA_PRECISION (16)
198214
#define JNTOA_MAX (44)

0 commit comments

Comments
 (0)