Skip to content

Commit 215631b

Browse files
authored
chore:update note-c (#66)
* remove note-c before re-add * Squashed 'src/note-c/' content from commit 931dc8a git-subtree-dir: src/note-c git-subtree-split: 931dc8a38d7095d9ade84eaf8e9b23562ae61060
1 parent d699a4b commit 215631b

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

src/note-c/n_cjson_helpers.c

+33-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ bool JContainsString(J *rsp, const char *field, const char *substr)
305305
//**************************************************************************/
306306
/*!
307307
@brief Add a binary to a Note as a Base64-encoded string.
308-
@param req The JSON request object.
308+
@param req The JSON object to which the field should be added
309309
@param fieldName The field to set.
310310
@param binaryData The binary data to set.
311311
@param binaryDataLen The length of the binary string.
@@ -329,6 +329,38 @@ bool JAddBinaryToObject(J *req, const char *fieldName, const void *binaryData, u
329329
return true;
330330
}
331331

332+
//**************************************************************************/
333+
/*!
334+
@brief Get binary from an object that is expected to be a Base64-encoded string.
335+
@param rsp The JSON object containing the field.
336+
@param fieldName The field to get data from.
337+
@param retBinaryData The binary data object allocated. (Use standard "free" method to free it.)
338+
@param retBinaryDataLen The length of the binary data.
339+
@returns bool. Whether the binary data was allocated and returned.
340+
*/
341+
/**************************************************************************/
342+
bool JGetBinaryFromObject(J *rsp, const char *fieldName, uint8_t **retBinaryData, uint32_t *retBinaryDataLen)
343+
{
344+
345+
char *payload = JGetString(rsp, fieldName);
346+
if (payload[0] == '\0') {
347+
return false;
348+
}
349+
350+
// Allocate a buffer for the payload
351+
char *p = (char *) _Malloc(JB64DecodeLen(payload));
352+
if (p == NULL) {
353+
return false;
354+
}
355+
uint32_t actualLen = JB64Decode(p, payload);
356+
357+
// Return the binary to the caller
358+
*retBinaryData = p;
359+
*retBinaryDataLen = actualLen;
360+
return true;
361+
362+
}
363+
332364
//**************************************************************************/
333365
/*!
334366
@brief Get the object name.

src/note-c/n_request.c

-15
Original file line numberDiff line numberDiff line change
@@ -312,21 +312,6 @@ char *NoteRequestResponseJSON(char *reqJSON)
312312

313313
}
314314

315-
/**************************************************************************/
316-
/*!
317-
@brief Override-able method to return user agent object
318-
@returns a `J` cJSON object with the user agent object.
319-
*/
320-
/**************************************************************************/
321-
#if defined(_MSC_VER)
322-
J *NoteUserAgent()
323-
#else
324-
__attribute__((weak)) J *NoteUserAgent()
325-
#endif
326-
{
327-
return NULL;
328-
}
329-
330315
/**************************************************************************/
331316
/*!
332317
@brief Initiate a transaction to the Notecard and return the response.

src/note-c/n_ua.c

+22-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,29 @@ static char *n_cpu_name = NULL;
2929

3030
/**************************************************************************/
3131
/*!
32-
@brief Method to return user agent object
32+
@brief Override-able method to add more data to the user agent object
3333
@returns a `J` cJSON object with the user agent object.
3434
*/
3535
/**************************************************************************/
36+
#if defined(_MSC_VER)
37+
void NoteUserAgentUpdate(J *ua)
38+
#else
39+
__attribute__((weak)) void NoteUserAgentUpdate(J *ua)
40+
#endif
41+
{
42+
}
43+
44+
/**************************************************************************/
45+
/*!
46+
@brief Override-able method to return user agent object
47+
@returns a `J` cJSON object with the user agent object.
48+
*/
49+
/**************************************************************************/
50+
#if defined(_MSC_VER)
3651
J *NoteUserAgent()
52+
#else
53+
__attribute__((weak)) J *NoteUserAgent()
54+
#endif
3755
{
3856

3957
J *ua = JCreateObject();
@@ -76,8 +94,6 @@ J *NoteUserAgent()
7694
n_cpu_name = "esp32";
7795
#elif defined(ARDUINO_ARCH_ESP8266)
7896
n_cpu_name = "esp8266";
79-
#elif defined(ARDUINO_ARCH_MBED)
80-
n_cpu_name = "mbed";
8197
#elif defined(ARDUINO_ARCH_MEGAAVR)
8298
n_cpu_name = "megaavr";
8399
#elif defined(ARDUINO_ARCH_NRF52840)
@@ -145,6 +161,9 @@ J *NoteUserAgent()
145161
JAddStringToObject(ua, "os_version", n_os_version);
146162
}
147163

164+
// Add more data to the UA from a higher level
165+
NoteUserAgentUpdate(ua);
166+
148167
return ua;
149168

150169
}

src/note-c/note.h

+2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ void NoteSetI2CAddress(uint32_t i2caddress);
139139

140140
// User agent
141141
J *NoteUserAgent(void);
142+
void NoteUserAgentUpdate(J *ua);
142143
void NoteSetUserAgent(char *agent);
143144
void NoteSetUserAgentOS(char *os_name, char *os_platform, char *os_family, char *os_version);
144145
void NoteSetUserAgentCPU(int cpu_mem, int cpu_mhz, int cpu_cores, char *cpu_vendor, char *cpu_name);
@@ -181,6 +182,7 @@ bool JIsNullString(J *rsp, const char *field);
181182
bool JIsExactString(J *rsp, const char *field, const char *teststr);
182183
bool JContainsString(J *rsp, const char *field, const char *substr);
183184
bool JAddBinaryToObject(J *req, const char *fieldName, const void *binaryData, uint32_t binaryDataLen);
185+
bool JGetBinaryFromObject(J *rsp, const char *fieldName, uint8_t **retBinaryData, uint32_t *retBinaryDataLen);
184186
const char *JGetItemName(const J * item);
185187
char *JAllocString(uint8_t *buffer, uint32_t len);
186188
const char *JType(J *item);

0 commit comments

Comments
 (0)