API Reference¶
+ +These pages cover the core functions and types that make up the note-c API.
+Dynamic Memory and Timing Hooks¶
+Functions¶
+-
+
-
+void NoteSetFn(mallocFn mallocHook, freeFn freeHook, delayMsFn delayMsHook, getMsFn getMsHook)¶
Set the platform-specific memory and timing hooks.
+-
+
- Parameters: +
-
+
mallocHook – The platform-specific memory allocation function (i.e.
malloc
).
+freeHook – The platform-specific memory free function (i.e.
free
).
+delayMsHook – The platform-specific millisecond delay function.
+getMsHook – The platform-specific millisecond counter function.
+
+
Types¶
+-
+
-
+typedef void *(*mallocFn)(size_t size)¶
The type for the memory allocation hook.
+-
+
- Param size: +
The number of bytes to allocate.
+
+- Return: +
A pointer to the newly allocated memory or NULL on failure.
+
+
-
+
-
+typedef void (*freeFn)(void *mem)¶
The type for the memory freeing hook.
+-
+
- Param mem: +
Pointer to the memory to free.
+
+
-
+
-
+typedef void (*delayMsFn)(uint32_t ms)¶
The type for the millisecond delay hook.
+-
+
- Param ms: +
The number of milliseconds to delay for.
+
+
-
+
-
+typedef uint32_t (*getMsFn)(void)¶
The type for the millisecond counter hook.
+-
+
- Return: +
The value of the millisecond counter.
+
+
I/O Hooks¶
+Serial Hooks¶
+Functions¶
+-
+
-
+void NoteSetFnSerial(serialResetFn resetFn, serialTransmitFn transmitFn, serialAvailableFn availFn, serialReceiveFn receiveFn)¶
Set the platform-specific hooks for communicating with the Notecard over serial.
+-
+
- Parameters: +
-
+
resetFn – The platform-specific serial reset function.
+transmitFn – The platform-specific serial transmit function.
+availFn – The platform-specific serial available function.
+receiveFn – The platform-specific serial receive function.
+
+
Types¶
+-
+
-
+typedef bool (*serialResetFn)(void)¶
The type for the serial reset hook.
+This hook is used to reset the serial peripheral used to communicate with the Notecard.
+-
+
- Return: +
+true
on success andfalse
on failure.
+
-
+
-
+typedef void (*serialTransmitFn)(uint8_t *txBuf, size_t txBufSize, bool flush)¶
The type for the serial transmit hook.
+-
+
- Param txBuf: +
A buffer of bytes to transmit to the Notecard.
+
+- Param txBufSize: +
The size, in bytes, of
+txBuf
.
+- Param flush: +
If true, flush the serial peripheral’s transmit buffer.
+
+
-
+
-
+typedef bool (*serialAvailableFn)(void)¶
The type for the serial available hook.
+-
+
- Return: +
+true
if there’s data to read andfalse
otherwise.
+
-
+
-
+typedef char (*serialReceiveFn)(void)¶
The type for the serial receive hook.
+-
+
- Return: +
The received byte.
+
+
I2C¶
+Functions¶
+-
+
-
+void NoteSetFnI2C(uint32_t notecardAddr, uint32_t maxTransmitSize, i2cResetFn resetFn, i2cTransmitFn transmitFn, i2cReceiveFn receiveFn)¶
Set the platform-specific hooks for communicating with the Notecard over I2C, as well as the I2C address of the Notecard and maximum transmission size.
+-
+
- Parameters: +
-
+
notecardAddr – The I2C address of the Notecard. Pass 0 to use the default address.
+maxTransmitSize – The max number of bytes to send to the Notecard in a single I2C segment. Pass 0 to use the default maximum transmission size.
+resetFn – The platform-specific I2C reset function.
+transmitFn – The platform-specific I2C transmit function.
+receiveFn – The platform-specific I2C receive function.
+
+
Types¶
+-
+
-
+typedef bool (*i2cResetFn)(uint16_t address)¶
The type for the I2C reset hook.
+This hook is used to reset the I2C peripheral used to communicate with the Notecard.
+-
+
- Param address: +
The I2C address of the Notecard.
+
+
-
+
-
+typedef const char *(*i2cTransmitFn)(uint16_t address, uint8_t *txBuf, uint16_t txBufSize)¶
The type for the I2C transmit hook.
+This hook is used to send a buffer of bytes to the Notecard.
+-
+
- Param address: +
The I2C address of the Notecard to transmit the data to.
+
+- Param txBuf: +
A buffer of bytes to transmit to the Notecard.
+
+- Param txBufSize: +
The size, in bytes, of
+txBuf
.
+- Return: +
NULL on success and an error string on failure.
+
+
-
+
-
+typedef const char *(*i2cReceiveFn)(uint16_t address, uint8_t *rxBuf, uint16_t rxBufSize, uint32_t *available)¶
The type for the I2C receive hook.
+This hook is used to receive a buffer of bytes from the Notecard.
+-
+
- Param address: +
The I2C address of the Notecard sending the data to receive.
+
+- Param rxBuf: +
A buffer to hold the data received from the Notecard.
+
+- Param rxBufSize: +
The size, in bytes, of rxBuf.
+
+- Param available: +
The number of bytes remaining to be received, if any.
+
+- Return: +
NULL on success and an error string on failure.
+
+
Macros¶
+-
+
-
+NOTE_I2C_ADDR_DEFAULT¶
The default I2C address of the Notecard.
+
-
+
-
+NOTE_I2C_MAX_DEFAULT¶
The maximum number of bytes to request from or transmit to the Notecard in a single chunk.
+
Mutex Hooks¶
+Notecard Mutex¶
+Functions¶
+ + +Types¶
+-
+
-
+typedef void (*mutexFn)(void)¶
The type for the various mutex (i.e. lock/unlock) hooks.
+
I2C Mutex¶
+Functions¶
+ + +Types¶
+See mutexFn
.
Notecard API Calls¶
+Functions¶
+-
+
-
+J *NoteNewRequest(const char *request)¶
Create a new JSON request.
+Creates a dynamically allocated
+J
object with one field"req"
whose value is the passed in request string.-
+
- Parameters: +
-
+
request – The name of the request, for example
hub.set
.
+
+- Returns: +
A
+J
object with the “req” field populated.
+
-
+
-
+J *NoteRequestResponse(J *req)¶
Send a request to the Notecard and return the response.
+The passed in request object is always freed, regardless of if the request was successful or not.
+++ + +See also
+
+NoteResponseError
to check the response for errors.
-
+
-
+J *NoteRequestResponseWithRetry(J *req, uint32_t timeoutSeconds)¶
Send a request to the Notecard, retrying it until it succeeds or it times out, and return the response.
+The passed in request object is always freed, regardless of if the request was successful or not.
+++ + +See also
+
+NoteResponseError
to check the response for errors.
-
+
-
+bool NoteRequest(J *req)¶
Send a request to the Notecard.
+The passed in request object is always freed, regardless of if the request was successful or not. The response from the Notecard, if any, is freed and not returned to the caller.
+++ +See also
+
+NoteRequestResponse
if you need to work with the response.-
+
- Parameters: +
-
+
req – Pointer to a
J
request object.
+
+- Returns: +
+true
if successful andfalse
if an error occurs (e.g. out of memory or the response from the Notecard has an “err” field). If req is a command rather than a request, atrue
return value indicates that the command was sent without error. However, since the Notecard sends no response to commands, it does not guarantee that the command was received and processed by the Notecard.
+
-
+
-
+bool NoteRequestWithRetry(J *req, uint32_t timeoutSeconds)¶
Send a request to the Notecard, retrying it until it succeeds or it times out.
+The passed in request object is always freed, regardless of if the request was successful or not. The response from the Notecard, if any, is freed and not returned to the caller.
+++ +See also
+
+NoteRequestResponseWithRetry
if you need to work with the response.-
+
- Parameters: +
-
+
req – Pointer to a
J
request object.
+timeoutSeconds – Time limit for retires, in seconds, if there is no response, or if the response contains an I/O error.
+
+- Returns: +
+true
if successful andfalse
if an error occurs (e.g. out of memory or the response from the Notecard has an “err” field).
+
-
+
-
+NoteResponseError(rsp)¶
Check if the Notecard response contains an error.
+-
+
- Parameters: +
-
+
rsp – The response to check.
+
+- Returns: +
+true
if there’s an error andfalse
if there’s not.
+
-
+
-
+NoteDeleteResponse(rsp)¶
Free a response from the Notecard.
+-
+
- Parameters: +
-
+
rsp – The response to free.
+
+
-
+
-
+char *NoteRequestResponseJSON(const char *reqJSON)¶
Send a request to the Notecard and return the response.
+Unlike
+NoteRequestResponse
, this function expects the request to be a valid JSON C-string, rather than aJ
object. The string is expected to be newline-terminated, otherwise the call produces undefined behavior. The response is returned as a dynamically allocated JSON C-string. The response string is verbatim what was sent by the Notecard, which IS newline-terminated. The caller is responsible for freeing the response string. If the request was a command (i.e. it uses “cmd” instead of “req”), this function returns NULL, because the Notecard does not send a response to commands.++Note
+When a “cmd” is sent, it is not possible to determine if an error occurred.
+-
+
- Parameters: +
-
+
reqJSON – A valid newline-terminated JSON C-string containing the request.
+
+- Returns: +
A newline-terminated JSON C-string with the response, or NULL if there was no response or if there was an error.
+
+
JSON Manipulation¶
+Functions¶
+-
+
-
+J *JCreateObject(void)¶
Create a new
+J
object.To free the object, use
+JDelete
.-
+
- Returns: +
A pointer to the newly-created object.
+
+
-
+
-
+J *JAddBoolToObject(J *const object, const char *const name, const Jbool boolean)¶
Add a boolean field to a
+J
object.-
+
- Parameters: +
-
+
object – The object to add the field to.
+name – The name of the field.
+boolean – The value of the field.
+
+- Returns: +
A pointer to the newly-added boolean field or NULL on error.
+
+
-
+
-
+J *JAddNumberToObject(J *const object, const char *const name, const JNUMBER number)¶
Add a number field to a
+J
object.-
+
- Parameters: +
-
+
object – The object to add the field to.
+name – The name of the field.
+number – The value of the field.
+
+- Returns: +
A pointer to the newly-added number field or NULL on error.
+
+
-
+
-
+J *JAddStringToObject(J *const object, const char *const name, const char *const string)¶
Add a string field to a
+J
object.-
+
- Parameters: +
-
+
object – The object to add the field to.
+name – The name of the field.
+string – The value of the field.
+
+- Returns: +
A pointer to the newly-added string field or NULL on error.
+
+
-
+
-
+J *JAddObjectToObject(J *const object, const char *const name)¶
Add an object field to another
+J
object.-
+
- Parameters: +
-
+
object – The object to add the field to.
+name – The name of the field.
+
+- Returns: +
A pointer to the newly-added object field or NULL on error.
+
+
-
+
-
+J *JAddArrayToObject(J *const object, const char *const name)¶
Add an array field to a
+J
object.-
+
- Parameters: +
-
+
object – The object to add the field to.
+name – The name of the field.
+
+- Returns: +
A pointer to the newly-added array field or NULL on error.
+
+
-
+
-
+J *JParse(const char *value)¶
Parse the passed in C-string as JSON and return a
+J
object representing it.-
+
- Parameters: +
-
+
value – The JSON object as a C-string.
+
+- Returns: +
A
+J
object or NULL on error (e.g. the string was invalid JSON).
+
-
+
-
+char *JPrintUnformatted(const J *item)¶
Get the unformatted string representation of a
+J
object.The string returned by this function is dynamically allocated and MUST be freed by the caller with
+JFree
. Unformatted means that the minimum JSON string is produced, without any additional whitespace.-
+
- Parameters: +
-
+
item – The JSON object to get the unformatted string representation of.
+
+- Returns: +
The string or NULL on error.
+
+
-
+
-
+void *JMalloc(size_t size)¶
Dynamically allocate a block of memory of the given size.
+This is simply a wrapper around the memory allocation function provided by the user via
+NoteSetFn
.-
+
- Parameters: +
-
+
size – The number of bytes to allocate.
+
+- Returns: +
A pointer to the first byte of the allocated memory or NULL on error.
+
+
-
+
-
+void JFree(void *object)¶
Free a block of dynamically allocated memory.
+This is simply a wrapper around the memory free function provided by the user via
+NoteSetFn
.-
+
- Parameters: +
-
+
p – A pointer to the block of memory to free.
+
+
Types¶
+-
+
-
+struct J¶
The core JSON object type used by note-c.
+When using note-c, treat this struct as opaque. You should never have to work directly with its members.
+
Macros¶
+-
+
-
+N_CJSON_NESTING_LIMIT¶
The maximum nesting level for JSON objects before a parsing error.
+Default value:
+100
For example, if you have a JSON object that contains multiple, nested objects like this
+ And the nesting level exceeds+{ + "x": + { + "x:" + { + . + . + . + } + } +} +
N_CJSON_NESTING_LIMIT
, then callingJParse
on aJ *
representing this object will return an error (NULL). +This exists to prevent the cJSON parser from causing a stack overflow. The user may override this macro at build time (e.g. -DN_CJSON_NESTING_LIMIT=200) to increase or reduce the limit.
+