From 2de68734e6d5fb8c377bbc96a589cb5cdad94ad6 Mon Sep 17 00:00:00 2001 From: "Zachary J. Fields" Date: Fri, 28 Oct 2022 08:25:09 -0500 Subject: [PATCH 1/2] chore: move strings to PROGMEM --- src/note-c/n_b64.c | 6 +- src/note-c/n_cjson.c | 20 +- src/note-c/n_cjson_helpers.c | 20 +- src/note-c/n_const.c | 224 ++++++++++++- src/note-c/n_edge.h | 121 +++---- src/note-c/n_ftoa.c | 10 +- src/note-c/n_helpers.c | 290 ++++++++-------- src/note-c/n_hooks.c | 18 +- src/note-c/n_i2c.c | 26 +- src/note-c/n_lib.h | 633 +++++++++++++++++++++++++++++++++-- src/note-c/n_request.c | 28 +- src/note-c/n_serial.c | 28 +- src/note-c/n_ua.c | 68 ++-- src/note-c/note.h | 8 +- 14 files changed, 1152 insertions(+), 348 deletions(-) diff --git a/src/note-c/n_b64.c b/src/note-c/n_b64.c index e718ce6..26fdc5e 100644 --- a/src/note-c/n_b64.c +++ b/src/note-c/n_b64.c @@ -79,11 +79,13 @@ * Base64 encoder/decoder. Originally Apache file ap_base64.c */ +#include #include + #include "n_lib.h" /* aaaack but it's fast and const should make it shared text page. */ -static const unsigned char pr2six[256] = { +static const unsigned char pr2six[256] PROGMEM = { /* ASCII table */ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, @@ -163,7 +165,7 @@ int JB64Decode(char *bufplain, const char *bufcoded) return nbytesdecoded; } -static const char basis_64[] = +static const char basis_64[] PROGMEM = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int JB64EncodeLen(int len) diff --git a/src/note-c/n_cjson.c b/src/note-c/n_cjson.c index 3af9dcd..25fa7b9 100644 --- a/src/note-c/n_cjson.c +++ b/src/note-c/n_cjson.c @@ -144,7 +144,7 @@ static unsigned char* Jstrdup(const unsigned char* string) return NULL; } - length = strlen((const char*)string) + sizeof(""); + length = strlen((const char*)string) + 1; copy = (unsigned char*)_Malloc(length); if (copy == NULL) { return NULL; @@ -412,18 +412,18 @@ static Jbool print_number(const J * const item, printbuffer * const output_buffe /* This checks for NaN and Infinity */ if ((d * 0) != 0) { char *nbuf = (char *) number_buffer; - strcpy(nbuf, "null"); + strcpy(nbuf, c_null); length = strlen(nbuf); } else { #if !MINIMIZE_CLIB_DEPENDENCIES JNUMBER test; /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ - length = sprintf((char*)number_buffer, "%1.15g", d); + length = sprintf((char*)number_buffer, c_fmt_1_15g, d); /* Check whether the original double can be recovered */ - if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || ((JNUMBER)test != d)) { + if ((sscanf((char*)number_buffer, c_fmt_lg, &test) != 1) || ((JNUMBER)test != d)) { /* If not, print with 17 decimal places of precision */ - length = sprintf((char*)number_buffer, "%1.17g", d); + length = sprintf((char*)number_buffer, c_fmt_1_17g, d); } #else char *nbuf = (char *) number_buffer; @@ -438,7 +438,7 @@ static Jbool print_number(const J * const item, printbuffer * const output_buffe } /* reserve appropriate space in the output */ - output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); + output_pointer = ensure(output_buffer, (size_t)length + 1); if (output_pointer == NULL) { return false; } @@ -870,7 +870,7 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) return NULL; } - if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) { + if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), c_utf8_bom, 3) == 0)) { buffer->offset += 3; } @@ -1003,7 +1003,7 @@ static unsigned char *print(const J * const item, Jbool format) N_CJSON_PUBLIC(char *) JPrint(const J *item) { if (item == NULL) { - return (char *)""; + return (char *)c_nullstring; } return (char*)print(item, true); } @@ -1011,7 +1011,7 @@ N_CJSON_PUBLIC(char *) JPrint(const J *item) N_CJSON_PUBLIC(char *) JPrintUnformatted(const J *item) { if (item == NULL) { - return (char *)""; + return (char *)c_nullstring; } return (char*)print(item, false); } @@ -1021,7 +1021,7 @@ N_CJSON_PUBLIC(char *) JPrintBuffered(const J *item, int prebuffer, Jbool fmt) printbuffer p = { 0, 0, 0, 0, 0, 0 }; if (item == NULL) { - return (char *)""; + return (char *)c_nullstring; } if (prebuffer < 0) { diff --git a/src/note-c/n_cjson_helpers.c b/src/note-c/n_cjson_helpers.c index 1cf49cf..da2119d 100644 --- a/src/note-c/n_cjson_helpers.c +++ b/src/note-c/n_cjson_helpers.c @@ -106,7 +106,7 @@ bool JBoolValue(J *item) char *JStringValue(J *item) { if (item == NULL) { - return (char *)""; + return (char *)c_nullstring; } return item->valuestring; } @@ -392,7 +392,7 @@ bool JGetBinaryFromObject(J *rsp, const char *fieldName, uint8_t **retBinaryData const char *JGetItemName(const J * item) { if (item == NULL || item->string == NULL) { - return ""; + return c_nullstring; } return item->string; } @@ -494,25 +494,25 @@ char *JAllocString(uint8_t *buffer, uint32_t len) const char *JType(J *item) { if (item == NULL) { - return ""; + return c_nullstring; } switch (item->type & 0xff) { case JTrue: case JFalse: - return "bool"; + return c_bool; case JNULL: - return "null"; + return c_null; case JNumber: - return "number"; + return c_number; case JRaw: case JString: - return "string"; + return c_string; case JObject: - return "object"; + return c_object; case JArray: - return "array"; + return c_array; } - return "invalid"; + return c_invalid; } //**************************************************************************/ diff --git a/src/note-c/n_const.c b/src/note-c/n_const.c index c9c90da..cf885b8 100644 --- a/src/note-c/n_const.c +++ b/src/note-c/n_const.c @@ -11,18 +11,216 @@ * */ +#include + #include "n_lib.h" -const char *c_null = "null"; -const char *c_false = "false"; -const char *c_true = "true"; -const char *c_nullstring = ""; -const char *c_newline = "\r\n"; -const char *c_mem = "mem"; -const char *c_iotimeout = "timeout {io}"; -const char *c_err = "err"; -const char *c_req = "req"; -const char *c_cmd = "cmd"; -const char *c_bad = "bad"; -const char *c_iobad = "bad {io}"; -const char *c_ioerr = "{io}"; +const char c_agent[] PROGMEM = "agent"; +const char c_align[] PROGMEM = "align"; +const char c_arch_arc32[] PROGMEM = "arc32"; +const char c_arch_avr[] PROGMEM = "avr"; +const char c_arch_esp32[] PROGMEM = "esp32"; +const char c_arch_esp8266[] PROGMEM = "esp8266"; +const char c_arch_megaavr[] PROGMEM = "megaavr"; +const char c_arch_nrf52840[] PROGMEM = "nrf52840"; +const char c_arch_nrf52[] PROGMEM = "nrf52"; +const char c_arch_nrf51[] PROGMEM = "nrf51"; +const char c_arch_pic32[] PROGMEM = "pic32"; +const char c_arch_sam[] PROGMEM = "sam"; +const char c_arch_samd[] PROGMEM = "samd"; +const char c_arch_spresence[] PROGMEM = "spresence"; +const char c_arch_stm32[] PROGMEM = "stm32"; +const char c_arch_stm32f0[] PROGMEM = "stm32f0"; +const char c_arch_stm32f1[] PROGMEM = "stm32f1"; +const char c_arch_stm32f4[] PROGMEM = "stm32f4"; +const char c_arch_stm32g0[] PROGMEM = "stm32g0"; +const char c_arch_stm32l4[] PROGMEM = "stm32l4"; +const char c_arch_stm32u5[] PROGMEM = "stm32u5"; +const char c_arch_swan_r5[] PROGMEM = "swan_r5"; +const char c_area[] PROGMEM = "area"; +const char c_array[] PROGMEM = "array"; +const char c_asterisk[] PROGMEM = "*"; +const char c_bad[] PROGMEM = "bad"; +const char c_bearing[] PROGMEM = "bearing"; +const char c_began_loc_dop[] PROGMEM = "began_loc_dop"; +const char c_began_loc_lat[] PROGMEM = "began_loc_lat"; +const char c_began_loc_lon[] PROGMEM = "began_loc_lon"; +const char c_began_loc_when[] PROGMEM = "began_loc_when"; +const char c_began_motion_when[] PROGMEM = "began_motion_when"; +const char c_began_when[] PROGMEM = "began_when"; +const char c_body[] PROGMEM = "body"; +const char c_bool[] PROGMEM = "bool"; +const char c_card_attn[] PROGMEM = "card.attn"; +const char c_card_contact[] PROGMEM = "card.contact"; +const char c_card_location[] PROGMEM = "card.location"; +const char c_card_location_mode[] PROGMEM = "card.location.mode"; +const char c_card_log[] PROGMEM = "card.log"; +const char c_card_restore[] PROGMEM = "card.restore"; +const char c_card_status[] PROGMEM = "card.status"; +const char c_card_temp[] PROGMEM = "card.temp"; +const char c_card_time[] PROGMEM = "card.time"; +const char c_card_version[] PROGMEM = "card.version"; +const char c_card_voltage[] PROGMEM = "card.voltage"; +const char c_charging[] PROGMEM = "charging"; +const char c_cmd[] PROGMEM = "cmd"; +const char c_colon[] PROGMEM = ":"; +const char c_compiler[] PROGMEM = "compiler"; +const char c_comma[] PROGMEM = ","; +const char c_connected[] PROGMEM = "connected"; +const char c_country[] PROGMEM = "country"; +const char c_cpu_cores[] PROGMEM = "cpu_cores"; +const char c_cpu_mem[] PROGMEM = "cpu_mem"; +const char c_cpu_mhz[] PROGMEM = "cpu_mhz"; +const char c_cpu_name[] PROGMEM = "cpu_name"; +const char c_cpu_vendor[] PROGMEM = "cpu_vendor"; +const char c_curly_brace_close[] PROGMEM = "}"; +const char c_curly_brace_open[] PROGMEM = "{"; +const char c_dbg_msg_about_to_sleep[] PROGMEM = "ABOUT TO SLEEP\n"; +const char c_dbg_msg_awakened_successfully[] PROGMEM = "AWAKENED SUCCESSFULLY\n"; +const char c_dbg_msg_cannot_convert_to_json[] PROGMEM = "can't convert to JSON"; +const char c_dbg_msg_card_restored[] PROGMEM = "CARD RESTORED\n"; +const char c_dbg_msg_did_not_sleep[] PROGMEM = "DIDN'T SLEEP\n"; +const char c_dbg_msg_i2c_not_active[] PROGMEM = "i2c not active"; +const char c_dbg_msg_i2c_or_serial_interface_must_be_selected[] PROGMEM = "i2c or serial interface must be selected"; +const char c_dbg_msg_i2c_receive_error[] PROGMEM = "i2c receive error\n"; +const char c_dbg_msg_i2c_transmit[] PROGMEM = "i2c transmit"; +const char c_dbg_msg_insufficient_memory[] PROGMEM = "insufficient memory"; +const char c_dbg_msg_invalid_data_received_on_serial_port_from_notecard[] PROGMEM = "invalid data received on serial port from notecard\n"; +const char c_dbg_msg_invalid_json[] PROGMEM = "invalid JSON: "; +const char c_dbg_msg_io_bad[] PROGMEM = "bad {io}"; +const char c_dbg_msg_io_err[] PROGMEM = "{io}"; +const char c_dbg_msg_io_serial_communications_error[] PROGMEM = "serial communications error {io}"; +const char c_dbg_msg_io_timeout[] PROGMEM = "timeout {io}"; +const char c_dbg_msg_io_transaction_incomplete[] PROGMEM = "transaction incomplete {io}"; +const char c_dbg_msg_io_transaction_timeout[] PROGMEM = "transaction timeout {io}"; +const char c_dbg_msg_io_unrecognized_response_from_card[] PROGMEM = "unrecognized response from card {io}"; +const char c_dbg_msg_no_notecard[] PROGMEM = "no notecard\n"; +const char c_dbg_msg_notecard_not_responding[] PROGMEM = "notecard not responding\n"; +const char c_dbg_msg_received_only_partial_reply_after_timeout[] PROGMEM = "received only partial reply after timeout:\n"; +const char c_dbg_msg_reply_to_request_did_not_arrive_from_module_in_time[] PROGMEM = "reply to request didn't arrive from module in time\n"; +const char c_dbg_msg_request_or_response_was_lost[] PROGMEM = "request or response was lost {io}"; +const char c_dbg_msg_requesting_sleep[] PROGMEM = "requesting sleep\n"; +const char c_dbg_msg_transaction_jsonbuf_malloc_failed[] PROGMEM = "transaction: jsonbuf malloc failed\n"; +const char c_dbg_msg_unrecognized_data_from_notecard[] PROGMEM = "unrecognized data from notecard\n"; +const char c_delete[] PROGMEM = "delete"; +const char c_device[] PROGMEM = "device"; +const char c_distance[] PROGMEM = "distance"; +const char c_dop[] PROGMEM = "dop"; +const char c_edge_notefile[] PROGMEM = "_edge.qi"; +const char c_email[] PROGMEM = "email"; +const char c_ended_loc_dop[] PROGMEM = "ended_loc_dop"; +const char c_ended_loc_lat[] PROGMEM = "ended_loc_lat"; +const char c_ended_loc_lon[] PROGMEM = "ended_loc_lon"; +const char c_ended_loc_when[] PROGMEM = "ended_loc_when"; +const char c_ended_motion_when[] PROGMEM = "ended_motion_when"; +const char c_ended_when[] PROGMEM = "ended_when"; +const char c_env_default[] PROGMEM = "env.default"; +const char c_env_get[] PROGMEM = "env.get"; +const char c_err[] PROGMEM = "err"; +const char c_FAIL[] PROGMEM = "FAIL"; +const char c_false[] PROGMEM = "false"; +const char c_file[] PROGMEM = "file"; +const char c_fixed[] PROGMEM = "fixed"; +const char c_fmt_1_15g[] PROGMEM = "%1.15g"; +const char c_fmt_1_17g[] PROGMEM = "%1.17g"; +const char c_fmt_lg[] PROGMEM = "%lg"; +const char c_free[] PROGMEM = "free"; +const char c_heartbeat[] PROGMEM = "heartbeat"; +const char c_HEX[] PROGMEM = "0123456789ABCDEF"; +const char c_hex[] PROGMEM = "0123456789abcdef"; +const char c_host[] PROGMEM = "host"; +const char c_hub_get[] PROGMEM = "hub.get"; +const char c_hub_set[] PROGMEM = "hub.set"; +const char c_hub_status[] PROGMEM = "hub.status"; +const char c_humidity[] PROGMEM = "humidity"; +const char c_hyphen[] PROGMEM = "-"; +const char c_i2c[] PROGMEM = "i2c"; +const char c_inbound[] PROGMEM = "inbound"; +const char c_INF[] PROGMEM = "INF"; +const char c_inf[] PROGMEM = "inf"; +const char c_invalid[] PROGMEM = "invalid"; +const char c_jcount[] PROGMEM = "jcount"; +const char c_journey[] PROGMEM = "journey"; +const char c_lat[] PROGMEM = "lat"; +const char c_level[] PROGMEM = "level"; +const char c_loc_lat[] PROGMEM = "loc_lat"; +const char c_loc_lon[] PROGMEM = "loc_lon"; +const char c_log[] PROGMEM = "log"; +const char c_lon[] PROGMEM = "lon"; +const char c_malloc[] PROGMEM = "malloc"; +const char c_mem[] PROGMEM = "mem"; +const char c_meters[] PROGMEM = "meters"; +const char c_minutes[] PROGMEM = "minutes"; +const char c_mode[] PROGMEM = "mode"; +const char c_motion[] PROGMEM = "motion"; +const char c_movements[] PROGMEM = "movements"; +const char c_mtime[] PROGMEM = "mtime"; +const char c_name[] PROGMEM = "name"; +const char c_NAN[] PROGMEM = "NAN"; +const char c_nan[] PROGMEM = "nan"; +const char c_newline[] PROGMEM = "\r\n"; +const char c_no_sat[] PROGMEM = "no-sat"; +const char c_note_add[] PROGMEM = "note.add"; +const char c_note_c[] PROGMEM = "note_c"; +const char c_note_event[] PROGMEM = "note.event"; +const char c_note_get[] PROGMEM = "note.get"; +const char c_note_template[] PROGMEM = "note.template"; +const char c_null[] PROGMEM = "null"; +const char c_null_paren[] PROGMEM = "(null)"; +const char c_nullstring[] PROGMEM = ""; +const char c_number[] PROGMEM = "number"; +const char c_object[] PROGMEM = "object"; +const char c_off[] PROGMEM = "off"; +const char c_org[] PROGMEM = "org"; +const char c_orientation[] PROGMEM = "orientation"; +const char c_os_family[] PROGMEM = "os_family"; +const char c_os_name[] PROGMEM = "os_name"; +const char c_os_platform[] PROGMEM = "os_platform"; +const char c_os_version[] PROGMEM = "os_version"; +const char c_outbound[] PROGMEM = "outbound"; +const char c_payload[] PROGMEM = "payload"; +const char c_point[] PROGMEM = "point"; +const char c_pressure[] PROGMEM = "pressure"; +const char c_product[] PROGMEM = "product"; +const char c_quotation_mark[] PROGMEM = "\""; +const char c_req[] PROGMEM = "req"; +const char c_req_interface[] PROGMEM = "req_interface"; +const char c_role[] PROGMEM = "role"; +const char c_route[] PROGMEM = "route"; +const char c_scan[] PROGMEM = "scan"; +const char c_scan_format_2g[] PROGMEM = "xmcc,xmnc,xlac,xcid,xrssi"; +const char c_scan_format_3g[] PROGMEM = "xmcc,xmnc,xlac,xcid,xpsc,xrscp"; +const char c_scan_format_4g[] PROGMEM = "xmcc,xmnc,xtac,xcid,xpci,rssi,rsrp,rsrq,xband,xchan"; +const char c_scan_format_5g[] PROGMEM = "xmcc,xmnc,xtac,xcid,xpci,rssi,rsrp,rsrq,xband,xchan"; +const char c_scan_format_gps[] PROGMEM = "epochsecs,olc,hdop"; +const char c_scan_format_time[] PROGMEM = "epochsecs"; +const char c_scan_format_wifi[] PROGMEM = "xbssid,xchannel,xfreq,rssi,snr,\"ssid\""; +const char c_seconds[] PROGMEM = "seconds"; +const char c_secs[] PROGMEM = "secs"; +const char c_serial[] PROGMEM = "serial"; +const char c_signals[] PROGMEM = "signals"; +const char c_sleep[] PROGMEM = "sleep"; +const char c_sn[] PROGMEM = "sn"; +const char c_space[] PROGMEM = " "; +const char c_start[] PROGMEM = "start"; +const char c_status[] PROGMEM = "status"; +const char c_string[] PROGMEM = "string"; +const char c_subsystem[] PROGMEM = "subsystem"; +const char c_sync[] PROGMEM = "sync"; +const char c_synclog_notefile[] PROGMEM = "_synclog.qi"; +const char c_temperature[] PROGMEM = "temperature"; +const char c_text[] PROGMEM = "text"; +const char c_time[] PROGMEM = "time"; +const char c_track[] PROGMEM = "track"; +const char c_true[] PROGMEM = "true"; +const char c_type[] PROGMEM = "type"; +const char c_unknown[] PROGMEM = "unknown"; +const char c_usb[] PROGMEM = "usb"; +const char c_UTC[] PROGMEM = "UTC"; +const char c_utf8_bom[] PROGMEM = "\xEF\xBB\xBF"; +const char c_value[] PROGMEM = "value"; +const char c_velocity[] PROGMEM = "velocity"; +const char c_version[] PROGMEM = "version"; +const char c_voltage[] PROGMEM = "voltage"; +const char c_web_dot[] PROGMEM = "web."; +const char c_zone[] PROGMEM = "zone"; diff --git a/src/note-c/n_edge.h b/src/note-c/n_edge.h index 75c3726..9a38f44 100644 --- a/src/note-c/n_edge.h +++ b/src/note-c/n_edge.h @@ -6,62 +6,64 @@ #pragma once +#include "n_lib.h" + // For standard tracking, the data format of a single point -#define TRACKTYPE_NORMAL "" -#define TRACKTYPE_HEARTBEAT "heartbeat" -#define TRACKTYPE_USB_CHANGE "usb" -#define TRACKTYPE_NO_SAT "no-sat" +#define TRACKTYPE_NORMAL c_nullstring +#define TRACKTYPE_HEARTBEAT c_heartbeat +#define TRACKTYPE_USB_CHANGE c_usb +#define TRACKTYPE_NO_SAT c_no_sat typedef struct { -#define TRACKPOINT_MEASUREMENT_TIME "mtime" +#define TRACKPOINT_MEASUREMENT_TIME c_mtime double mtime; -#define TRACKPOINT_LAT "lat" +#define TRACKPOINT_LAT c_lat double lat; -#define TRACKPOINT_LON "lon" +#define TRACKPOINT_LON c_lon double lon; -#define TRACKPOINT_TIME "time" +#define TRACKPOINT_TIME c_time uint32_t time; -#define TRACKPOINT_HDOP "dop" +#define TRACKPOINT_HDOP c_dop double hdop; -#define TRACKPOINT_JOURNEY_TIME "journey" +#define TRACKPOINT_JOURNEY_TIME c_journey uint32_t journeyTime; -#define TRACKPOINT_JOURNEY_COUNT "jcount" +#define TRACKPOINT_JOURNEY_COUNT c_jcount uint32_t journeyCount; -#define TRACKPOINT_TYPE "status" +#define TRACKPOINT_TYPE c_status char trackType[32]; -#define TRACKPOINT_MOTION_COUNT "motion" +#define TRACKPOINT_MOTION_COUNT c_motion uint32_t motionCount; -#define TRACKPOINT_SECONDS "seconds" +#define TRACKPOINT_SECONDS c_seconds int32_t seconds; -#define TRACKPOINT_DISTANCE "distance" +#define TRACKPOINT_DISTANCE c_distance double distance; -#define TRACKPOINT_BEARING "bearing" +#define TRACKPOINT_BEARING c_bearing double bearing; -#define TRACKPOINT_VELOCITY "velocity" +#define TRACKPOINT_VELOCITY c_velocity double velocity; -#define TRACKPOINT_TEMPERATURE "temperature" +#define TRACKPOINT_TEMPERATURE c_temperature double temperature; -#define TRACKPOINT_HUMIDITY "humidity" +#define TRACKPOINT_HUMIDITY c_humidity double humidity; -#define TRACKPOINT_PRESSURE "pressure" +#define TRACKPOINT_PRESSURE c_pressure double pressure; -#define TRACKPOINT_VOLTAGE "voltage" +#define TRACKPOINT_VOLTAGE c_voltage double voltage; -#define TRACKPOINT_USB "usb" +#define TRACKPOINT_USB c_usb bool usb; -#define TRACKPOINT_CHARGING "charging" +#define TRACKPOINT_CHARGING c_charging bool charging; } TrackPoint; // MotionPoint is the data structure that we use when tracking motion typedef struct { -#define MOTIONPOINT_MEASUREMENT_TIME "mtime" +#define MOTIONPOINT_MEASUREMENT_TIME c_mtime double mtime; -#define MOTIONPOINT_MOVEMENTS "movements" +#define MOTIONPOINT_MOVEMENTS c_movements char movements[250]; -#define MOTIONPOINT_ORIENTATION "orientation" +#define MOTIONPOINT_ORIENTATION c_orientation char orientation[20]; -#define MOTIONPOINT_MOTION_COUNT "motion" +#define MOTIONPOINT_MOTION_COUNT c_motion uint32_t motionCount; #define MOTIONPOINT_TILT_COUNT "tilt" uint32_t tiltCount; @@ -78,51 +80,52 @@ typedef struct { } LogData; // Format of the edge entry which is the dequeued note body -#define EDGE_NOTEFILE "_edge.qi" -#define EDGETYPE_SCAN "scan" -#define EDGETYPE_TRACKPOINT "track" -#define EDGETYPE_MOTIONPOINT "motion" -#define EDGETYPE_LOG "log" +#define EDGE_NOTEFILE c_edge_notefile +#define EDGETYPE_SCAN c_scan +#define EDGETYPE_TRACKPOINT c_track +#define EDGETYPE_MOTIONPOINT c_motion +#define EDGETYPE_LOG c_log + typedef struct { -#define EDGE_TYPE "type" +#define EDGE_TYPE c_type char edgeType[32]; -#define EDGE_TRACKPOINT "point" +#define EDGE_TRACKPOINT c_point TrackPoint trackPoint; -#define EDGE_MOTIONPOINT "motion" +#define EDGE_MOTIONPOINT c_motion MotionPoint motionPoint; -#define EDGE_SCAN_SECS "secs" +#define EDGE_SCAN_SECS c_secs uint32_t scanSecs; -#define EDGE_SCAN_METERS "meters" +#define EDGE_SCAN_METERS c_meters uint32_t scanMeters; -#define EDGE_SCAN_BEARING "bearing" +#define EDGE_SCAN_BEARING c_bearing uint32_t scanBearing; -#define EDGE_SCAN_BEGAN_TIME "began_when" +#define EDGE_SCAN_BEGAN_TIME c_began_when uint32_t scanBeganTime; -#define EDGE_SCAN_BEGAN_LOC_TIME "began_loc_when" +#define EDGE_SCAN_BEGAN_LOC_TIME c_began_loc_when uint32_t scanBeganLocTime; -#define EDGE_SCAN_BEGAN_LOC_LAT "began_loc_lat" +#define EDGE_SCAN_BEGAN_LOC_LAT c_began_loc_lat double scanBeganLocLat; -#define EDGE_SCAN_BEGAN_LOC_LON "began_loc_lon" +#define EDGE_SCAN_BEGAN_LOC_LON c_began_loc_lon double scanBeganLocLon; -#define EDGE_SCAN_BEGAN_LOC_HDOP "began_loc_dop" +#define EDGE_SCAN_BEGAN_LOC_HDOP c_began_loc_dop double scanBeganLocHDOP; -#define EDGE_SCAN_BEGAN_MOTION_TIME "began_motion_when" +#define EDGE_SCAN_BEGAN_MOTION_TIME c_began_motion_when uint32_t scanBeganMotionTime; -#define EDGE_SCAN_ENDED_TIME "ended_when" +#define EDGE_SCAN_ENDED_TIME c_ended_when uint32_t scanEndedTime; -#define EDGE_SCAN_ENDED_LOC_TIME "ended_loc_when" +#define EDGE_SCAN_ENDED_LOC_TIME c_ended_loc_when uint32_t scanEndedLocTime; -#define EDGE_SCAN_ENDED_LOC_LAT "ended_loc_lat" +#define EDGE_SCAN_ENDED_LOC_LAT c_ended_loc_lat double scanEndedLocLat; -#define EDGE_SCAN_ENDED_LOC_LON "ended_loc_lon" +#define EDGE_SCAN_ENDED_LOC_LON c_ended_loc_lon double scanEndedLocLon; -#define EDGE_SCAN_ENDED_LOC_HDOP "ended_loc_dop" +#define EDGE_SCAN_ENDED_LOC_HDOP c_ended_loc_dop double scanEndedLocHDOP; -#define EDGE_SCAN_ENDED_MOTION_TIME "ended_motion_when" +#define EDGE_SCAN_ENDED_MOTION_TIME c_ended_motion_when uint32_t scanEndedMotionTime; -#define EDGE_SCAN_LOC_LAT "loc_lat" +#define EDGE_SCAN_LOC_LAT c_loc_lat double scanLocLat; -#define EDGE_SCAN_LOC_LON "loc_lon" +#define EDGE_SCAN_LOC_LON c_loc_lon double scanLocLon; } EdgeData; @@ -140,10 +143,10 @@ typedef struct { #define SCAN_TYPE_CELLTIME 't' // FORMAT_TIME #define SCAN_TYPE_WIFITIME 's' // FORMAT_TIME #define SCAN_TYPE_GPS 'd' // FORMAT_GPS -#define SCAN_FORMAT_2G "xmcc,xmnc,xlac,xcid,xrssi" -#define SCAN_FORMAT_3G "xmcc,xmnc,xlac,xcid,xpsc,xrscp" -#define SCAN_FORMAT_4G "xmcc,xmnc,xtac,xcid,xpci,rssi,rsrp,rsrq,xband,xchan" -#define SCAN_FORMAT_5G "xmcc,xmnc,xtac,xcid,xpci,rssi,rsrp,rsrq,xband,xchan" -#define SCAN_FORMAT_WIFI "xbssid,xchannel,xfreq,rssi,snr,\"ssid\"" -#define SCAN_FORMAT_TIME "epochsecs" -#define SCAN_FORMAT_GPS "epochsecs,olc,hdop" +#define SCAN_FORMAT_2G c_scan_format_2g +#define SCAN_FORMAT_3G c_scan_format_3g +#define SCAN_FORMAT_4G c_scan_format_4g +#define SCAN_FORMAT_5G c_scan_format_5g +#define SCAN_FORMAT_WIFI c_scan_format_wifi +#define SCAN_FORMAT_TIME c_scan_format_time +#define SCAN_FORMAT_GPS c_scan_format_gps diff --git a/src/note-c/n_ftoa.c b/src/note-c/n_ftoa.c index 9ced495..3376ae3 100644 --- a/src/note-c/n_ftoa.c +++ b/src/note-c/n_ftoa.c @@ -63,7 +63,7 @@ char * JNtoA(JNUMBER f, char * buf, int precision) } fmtflt(buf, &len, JNTOA_MAX, f, -1, precision, flags, &overflow); if (overflow) { - strcpy(buf, "*"); + strcpy(buf, c_asterisk); } buf[len] = '\0'; return buf; @@ -113,9 +113,9 @@ fmtflt(char *str, size_t *len, size_t size, JNUMBER fvalue, int width, } if (isnan(fvalue)) { - infnan = (flags & PRINT_F_UP) ? "NAN" : "nan"; + infnan = (flags & PRINT_F_UP) ? c_NAN : c_nan; } else if (isinf(fvalue)) { - infnan = (flags & PRINT_F_UP) ? "INF" : "inf"; + infnan = (flags & PRINT_F_UP) ? c_INF : c_inf; } if (infnan != NULL) { @@ -376,7 +376,7 @@ static void fmtstr(char *str, size_t *len, size_t size, const char *value, int w int noprecision = (precision == -1); if (value == NULL) { /* We're forgiving. */ - value = "(null)"; + value = c_null_paren; } /* If a precision was specified, don't read the string past it. */ @@ -441,7 +441,7 @@ static int getexponent(JNUMBER value) static int convert(uintmax_t value, char *buf, size_t size, int base, int caps) { - const char *digits = caps ? "0123456789ABCDEF" : "0123456789abcdef"; + const char *digits = caps ? c_HEX : c_hex; size_t pos = 0; /* We return an unterminated buffer with the digits in reverse order. */ diff --git a/src/note-c/n_helpers.c b/src/note-c/n_helpers.c index 7219ffd..af439d8 100644 --- a/src/note-c/n_helpers.c +++ b/src/note-c/n_helpers.c @@ -11,9 +11,11 @@ * */ +#include #include #include #include + #include "n_lib.h" // When interfacing with the Notecard, it is generally encouraged that the JSON object manipulation and @@ -36,7 +38,7 @@ static bool zoneStillUnavailable = true; static bool zoneForceRefresh = false; static char curZone[10] = {0}; static char curArea[64] = {0}; -static char curCountry[8] = ""; +static char curCountry[8] = {0}; static int curZoneOffsetMins = 0; // Location-related suppression timer and cache @@ -60,9 +62,9 @@ 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"}; +static const short leapYearDaysByMonth[] PROGMEM = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; +static const short normalYearDaysByMonth[] PROGMEM = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; +static const char * const daynames[] PROGMEM = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // Forwards static bool timerExpiredSecs(uint32_t *timer, uint32_t periodSecs); @@ -154,9 +156,9 @@ void NoteTimeSet(JTIME secondsUTC, int offset, char *zone, char *country, char * 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)); + strlcpy(curZone, zone == NULL ? c_UTC : zone, sizeof(curZone)); + strlcpy(curArea, area == NULL ? c_nullstring : area, sizeof(curArea)); + strlcpy(curCountry, country == NULL ? c_nullstring : country, sizeof(curCountry)); } } @@ -188,8 +190,8 @@ bool NotePrint(const char *text) return true; } - J *req = NoteNewRequest("card.log"); - JAddStringToObject(req, "text", text); + J *req = NoteNewRequest(c_card_log); + JAddStringToObject(req, c_text, text); success = NoteRequest(req); return success; @@ -227,17 +229,17 @@ JTIME NoteTimeST() if (timerExpiredSecs(&timeTimer, suppressionTimerSecs)) { // Request time and zone info from the card - J *rsp = NoteRequestResponse(NoteNewRequest("card.time")); + J *rsp = NoteRequestResponse(NoteNewRequest(c_card_time)); if (rsp != NULL) { if (!NoteResponseError(rsp)) { - JTIME seconds = JGetInt(rsp, "time"); + JTIME seconds = JGetInt(rsp, c_time); if (seconds != 0) { // Set the time setTime(seconds); // Get the zone - char *z = JGetString(rsp, "zone"); + char *z = JGetString(rsp, c_zone); if (z[0] != '\0') { char zone[64]; strlcpy(zone, z, sizeof(zone)); @@ -248,12 +250,12 @@ JTIME NoteTimeST() } else { *sep = '\0'; } - zoneStillUnavailable = (memcmp(zone, "UTC", 3) == 0); + zoneStillUnavailable = (memcmp(zone, c_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)); + curZoneOffsetMins = JGetInt(rsp, c_minutes); + strlcpy(curCountry, JGetString(rsp, c_country), sizeof(curCountry)); + strlcpy(curArea, JGetString(rsp, c_area), sizeof(curArea)); } } @@ -300,13 +302,13 @@ bool NoteRegion(char **retCountry, char **retArea, char **retZone, int *retZoneO NoteTimeST(); if (zoneStillUnavailable) { if (retCountry != NULL) { - *retCountry = (char *) ""; + *retCountry = (char *) c_nullstring; } if (retArea != NULL) { - *retArea = (char *) ""; + *retArea = (char *) c_nullstring; } if (retZone != NULL) { - *retZone = (char *) "UTC"; + *retZone = (char *) c_UTC; } if (retZoneOffset != NULL) { *retZoneOffset = 0; @@ -360,10 +362,10 @@ bool NoteLocalTimeST(uint16_t *retYear, uint8_t *retMonth, uint8_t *retDay, uint *retSecond = 0; } if (retWeekday != NULL) { - *retWeekday = (char *) ""; + *retWeekday = (char *) c_nullstring; } if (retZone != NULL) { - *retZone = (char *) ""; + *retZone = (char *) c_nullstring; } // Exit if time isn't yet valid @@ -396,7 +398,7 @@ bool NoteLocalTimeST(uint16_t *retYear, uint8_t *retMonth, uint8_t *retDay, uint if (retYear != NULL) { *retYear = (uint16_t) year+1900; } - short *pm = daysByMonth(year); + const short *pm = daysByMonth(year); for (mon = 12; days < pm[--mon]; ) ; if (retMonth != NULL) { *retMonth = (uint8_t) mon+1; @@ -500,13 +502,13 @@ bool NoteLocationValidST(char *errbuf, uint32_t errbuflen) } // Request location from the card - J *rsp = NoteRequestResponse(NoteNewRequest("card.location")); + J *rsp = NoteRequestResponse(NoteNewRequest(c_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) { + if (!NoteResponseError(rsp) || strcmp(JGetString(rsp, c_mode), c_off) == 0) { NoteDeleteResponse(rsp); locationValid = true; locationLastErr[0] = '\0'; @@ -517,7 +519,7 @@ bool NoteLocationValidST(char *errbuf, uint32_t errbuflen) } // Remember the error for next iteration - strlcpy(locationLastErr, JGetString(rsp, "err"), sizeof(locationLastErr)); + strlcpy(locationLastErr, JGetString(rsp, c_err), sizeof(locationLastErr)); if (errbuf != NULL) { strlcpy(errbuf, locationLastErr, errbuflen); } @@ -537,10 +539,10 @@ bool NoteLocationValidST(char *errbuf, uint32_t errbuflen) bool NoteSetEnvDefault(const char *variable, char *buf) { bool success = false; - J *req = NoteNewRequest("env.default"); + J *req = NoteNewRequest(c_env_default); if (req != NULL) { - JAddStringToObject(req, "name", variable); - JAddStringToObject(req, "text", buf); + JAddStringToObject(req, c_name, variable); + JAddStringToObject(req, c_text, buf); success = NoteRequest(req); } return success; @@ -629,14 +631,14 @@ bool NoteGetEnv(const char *variable, const char *defaultVal, char *buf, uint32_ } else { strlcpy(buf, defaultVal, buflen); } - J *req = NoteNewRequest("env.get"); + J *req = NoteNewRequest(c_env_get); if (req != NULL) { - JAddStringToObject(req, "name", variable); + JAddStringToObject(req, c_name, variable); J *rsp = NoteRequestResponse(req); if (rsp != NULL) { if (!NoteResponseError(rsp)) { success = true; - char *val = JGetString(rsp, "text"); + char *val = JGetString(rsp, c_text); if (val[0] != '\0') { strlcpy(buf, val, buflen); } @@ -668,10 +670,10 @@ bool NoteIsConnected() bool NoteIsConnectedST() { if (timerExpiredSecs(&connectivityTimer, suppressionTimerSecs)) { - J *rsp = NoteRequestResponse(NoteNewRequest("hub.status")); + J *rsp = NoteRequestResponse(NoteNewRequest(c_hub_status)); if (rsp != NULL) { if (!NoteResponseError(rsp)) { - cardConnected = JGetBool(rsp, "connected"); + cardConnected = JGetBool(rsp, c_connected); } NoteDeleteResponse(rsp); } @@ -691,11 +693,11 @@ bool NoteGetNetStatus(char *statusBuf, int statusBufLen) { bool success = false; statusBuf[0] = '\0'; - J *rsp = NoteRequestResponse(NoteNewRequest("hub.status")); + J *rsp = NoteRequestResponse(NoteNewRequest(c_hub_status)); if (rsp != NULL) { success = !NoteResponseError(rsp); if (success) { - strlcpy(statusBuf, JGetString(rsp, "status"), statusBufLen); + strlcpy(statusBuf, JGetString(rsp, c_status), statusBufLen); } NoteDeleteResponse(rsp); } @@ -714,11 +716,11 @@ bool NoteGetVersion(char *versionBuf, int versionBufLen) { bool success = false; versionBuf[0] = '\0'; - J *rsp = NoteRequestResponse(NoteNewRequest("card.version")); + J *rsp = NoteRequestResponse(NoteNewRequest(c_card_version)); if (rsp != NULL) { success = !NoteResponseError(rsp); if (success) { - strlcpy(versionBuf, JGetString(rsp, "version"), versionBufLen); + strlcpy(versionBuf, JGetString(rsp, c_version), versionBufLen); } NoteDeleteResponse(rsp); } @@ -751,21 +753,21 @@ bool NoteGetLocation(JNUMBER *retLat, JNUMBER *retLon, JTIME *time, char *status if (time != NULL) { *time = 0; } - J *rsp = NoteRequestResponse(NoteNewRequest("card.location")); + J *rsp = NoteRequestResponse(NoteNewRequest(c_card_location)); if (rsp != NULL) { if (statusBuf != NULL) { - strlcpy(statusBuf, JGetString(rsp, "status"), statusBufLen); + strlcpy(statusBuf, JGetString(rsp, c_status), statusBufLen); } - if (JIsPresent(rsp, "lat") && JIsPresent(rsp, "lon")) { + if (JIsPresent(rsp, c_lat) && JIsPresent(rsp, c_lon)) { if (retLat != NULL) { - *retLat = JGetNumber(rsp, "lat"); + *retLat = JGetNumber(rsp, c_lat); } if (retLon != NULL) { - *retLon = JGetNumber(rsp, "lon"); + *retLon = JGetNumber(rsp, c_lon); } locValid = true; } - JTIME seconds = JGetInt(rsp, "time"); + JTIME seconds = JGetInt(rsp, c_time); if (seconds != 0 && time != NULL) { *time = seconds; } @@ -785,11 +787,11 @@ bool NoteGetLocation(JNUMBER *retLat, JNUMBER *retLon, JTIME *time, char *status bool NoteSetLocation(JNUMBER lat, JNUMBER lon) { bool success = false; - J *req = NoteNewRequest("card.location.mode"); + J *req = NoteNewRequest(c_card_location_mode); if (req != NULL) { - JAddStringToObject(req, "mode", "fixed"); - JAddNumberToObject(req, "lat", lat); - JAddNumberToObject(req, "lon", lon); + JAddStringToObject(req, c_mode, c_fixed); + JAddNumberToObject(req, c_lat, lat); + JAddNumberToObject(req, c_lon, lon); success = NoteRequest(req); } return success; @@ -804,9 +806,9 @@ bool NoteSetLocation(JNUMBER lat, JNUMBER lon) bool NoteClearLocation() { bool success = false; - J *req = NoteNewRequest("card.location.mode"); + J *req = NoteNewRequest(c_card_location_mode); if (req != NULL) { - JAddBoolToObject(req, "delete", true); + JAddBoolToObject(req, c_delete, true); success = NoteRequest(req); } return success; @@ -824,11 +826,11 @@ bool NoteGetLocationMode(char *modeBuf, int modeBufLen) { bool success = false; modeBuf[0] = '\0'; - J *rsp = NoteRequestResponse(NoteNewRequest("card.location.mode")); + J *rsp = NoteRequestResponse(NoteNewRequest(c_card_location_mode)); if (rsp != NULL) { success = !NoteResponseError(rsp); if (success) { - strlcpy(modeBuf, JGetString(rsp, "mode"), modeBufLen); + strlcpy(modeBuf, JGetString(rsp, c_mode), modeBufLen); } NoteDeleteResponse(rsp); } @@ -846,13 +848,13 @@ bool NoteGetLocationMode(char *modeBuf, int modeBufLen) bool NoteSetLocationMode(const char *mode, uint32_t seconds) { bool success = false; - J *req = NoteNewRequest("card.location.mode"); + J *req = NoteNewRequest(c_card_location_mode); if (req != NULL) { if (mode[0] == '\0') { - mode = "-"; + mode = c_hyphen; } - JAddStringToObject(req, "mode", mode); - JAddNumberToObject(req, "seconds", seconds); + JAddStringToObject(req, c_mode, mode); + JAddNumberToObject(req, c_seconds, seconds); success = NoteRequest(req); } return success; @@ -907,14 +909,14 @@ bool NoteGetServiceConfigST(char *productBuf, int productBufLen, char *serviceBu // 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")); + J *rsp = NoteRequestResponse(NoteNewRequest(c_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)); + strlcpy(scProduct, JGetString(rsp, c_product), sizeof(scProduct)); + strlcpy(scService, JGetString(rsp, c_host), sizeof(scService)); + strlcpy(scDevice, JGetString(rsp, c_device), sizeof(scDevice)); + strlcpy(scSN, JGetString(rsp, c_sn), sizeof(scSN)); } NoteDeleteResponse(rsp); } @@ -965,21 +967,21 @@ bool NoteGetStatus(char *statusBuf, int statusBufLen, JTIME *bootTime, bool *ret if (retSignals != NULL) { *retSignals = false; } - J *rsp = NoteRequestResponse(NoteNewRequest("card.status")); + J *rsp = NoteRequestResponse(NoteNewRequest(c_card_status)); if (rsp != NULL) { success = !NoteResponseError(rsp); if (success) { if (statusBuf != NULL) { - strlcpy(statusBuf, JGetString(rsp, "status"), statusBufLen); + strlcpy(statusBuf, JGetString(rsp, c_status), statusBufLen); } if (bootTime != NULL) { - *bootTime = JGetInt(rsp, "time"); + *bootTime = JGetInt(rsp, c_time); } if (retUSB != NULL) { - *retUSB = JGetBool(rsp, "usb"); + *retUSB = JGetBool(rsp, c_usb); } - if (retSignals != NULL && JGetBool(rsp, "connected")) { - *retSignals = (JGetInt(rsp, "signals") > 0); + if (retSignals != NULL && JGetBool(rsp, c_connected)) { + *retSignals = (JGetInt(rsp, c_signals) > 0); } } NoteDeleteResponse(rsp); @@ -1010,15 +1012,15 @@ bool NoteGetStatusST(char *statusBuf, int statusBufLen, JTIME *bootTime, bool *r // Refresh if it's time to do so if (timerExpiredSecs(&statusTimer, suppressionTimerSecs)) { - J *rsp = NoteRequestResponse(NoteNewRequest("card.status")); + J *rsp = NoteRequestResponse(NoteNewRequest(c_card_status)); if (rsp != NULL) { success = !NoteResponseError(rsp); if (success) { - strlcpy(lastStatus, JGetString(rsp, "status"), sizeof(lastStatus)); - lastBootTime = JGetInt(rsp, "time"); - lastUSB = JGetBool(rsp, "usb"); - if (JGetBool(rsp, "connected")) { - lastSignals = (JGetInt(rsp, "signals") > 0); + strlcpy(lastStatus, JGetString(rsp, c_status), sizeof(lastStatus)); + lastBootTime = JGetInt(rsp, c_time); + lastUSB = JGetBool(rsp, c_usb); + if (JGetBool(rsp, c_connected)) { + lastSignals = (JGetInt(rsp, c_signals) > 0); } else { lastSignals = false; } @@ -1105,33 +1107,33 @@ bool NoteSleep(char *stateb64, uint32_t seconds, const char *modes) bool success = false; // Trace - _Debug("ABOUT TO SLEEP\n"); + _Debug(c_dbg_msg_about_to_sleep); // Use a Command rather than a Request so that the Notecard doesn't try to send // a response back to us, which would cause a communications error on that end. - _Debug("requesting sleep\n"); - J *req = NoteNewCommand("card.attn"); + _Debug(c_dbg_msg_requesting_sleep); + J *req = NoteNewCommand(c_card_attn); if (req != NULL) { // Add the base64 item in a wonderful way that doesn't strdup the huge string if (stateb64 != NULL) { J *stringReferenceItem = JCreateStringReference(stateb64); if (stringReferenceItem != NULL) { - JAddItemToObject(req, "payload", stringReferenceItem); + JAddItemToObject(req, c_payload, stringReferenceItem); } } char modestr[64]; - strlcpy(modestr, "sleep", sizeof(modestr)); + strlcpy(modestr, c_sleep, sizeof(modestr)); if (modes != NULL) { - strlcat(modestr, ",", sizeof(modestr)); + strlcat(modestr, c_comma, sizeof(modestr)); strlcat(modestr, modes, sizeof(modestr)); } - JAddStringToObject(req, "mode", modestr); - JAddNumberToObject(req, "seconds", seconds); + JAddStringToObject(req, c_mode, modestr); + JAddNumberToObject(req, c_seconds, seconds); success = NoteRequest(req); } // Trace - _Debug("DIDN'T SLEEP\n"); + _Debug(c_dbg_msg_did_not_sleep); // Done return success; @@ -1181,11 +1183,11 @@ bool NotePayloadRetrieveAfterSleep(NotePayloadDesc *desc) } // Send the Notecard a request to retrieve the saved state - J *req = NoteNewRequest("card.attn"); + J *req = NoteNewRequest(c_card_attn); if (req == NULL) { return false; } - JAddBoolToObject(req, "start", true); + JAddBoolToObject(req, c_start, true); J *rsp = NoteRequestResponse(req); if (rsp == NULL) { return false; @@ -1196,7 +1198,7 @@ bool NotePayloadRetrieveAfterSleep(NotePayloadDesc *desc) } // Note the current time, if the field is present - JTIME seconds = JGetInt(rsp, "time"); + JTIME seconds = JGetInt(rsp, c_time); if (seconds != 0) { setTime(seconds); } @@ -1208,7 +1210,7 @@ bool NotePayloadRetrieveAfterSleep(NotePayloadDesc *desc) } // Exit if no payload, knowing that we expected one - char *payload = JGetString(rsp, "payload"); + char *payload = JGetString(rsp, c_payload); if (payload[0] == '\0') { NoteDeleteResponse(rsp); return false; @@ -1230,7 +1232,7 @@ bool NotePayloadRetrieveAfterSleep(NotePayloadDesc *desc) desc->length = actualLen; // State restored - _Debug("AWAKENED SUCCESSFULLY\n"); + _Debug(c_dbg_msg_awakened_successfully); NoteDeleteResponse(rsp); return true; } @@ -1248,9 +1250,9 @@ bool NoteFactoryReset(bool deleteConfigSettings) bool success = false; // Perform the restore-to-factor-settings transaction - J *req = NoteNewRequest("card.restore"); + J *req = NoteNewRequest(c_card_restore); if (req != NULL) { - JAddBoolToObject(req, "delete", deleteConfigSettings); + JAddBoolToObject(req, c_delete, deleteConfigSettings); success = NoteRequest(req); } @@ -1261,7 +1263,7 @@ bool NoteFactoryReset(bool deleteConfigSettings) // Wait for serial to stabilize after it reboots _DelayMs(5000); - _Debug("CARD RESTORED\n"); + _Debug(c_dbg_msg_card_restored); // Reset the Notecard while (!NoteReset()) { @@ -1283,12 +1285,12 @@ bool NoteFactoryReset(bool deleteConfigSettings) bool NoteSetProductID(const char *productID) { bool success = false; - J *req = NoteNewRequest("hub.set"); + J *req = NoteNewRequest(c_hub_set); if (req != NULL) { if (productID[0] == '\0') { - JAddStringToObject(req, "product", "-"); + JAddStringToObject(req, c_product, c_hyphen); } else { - JAddStringToObject(req, "product", productID); + JAddStringToObject(req, c_product, productID); } success = NoteRequest(req); } @@ -1307,12 +1309,12 @@ bool NoteSetProductID(const char *productID) bool NoteSetSerialNumber(const char *sn) { bool success = false; - J *req = NoteNewRequest("hub.set"); + J *req = NoteNewRequest(c_hub_set); if (req != NULL) { if (sn[0] == '\0') { - JAddStringToObject(req, "sn", "-"); + JAddStringToObject(req, c_sn, c_hyphen); } else { - JAddStringToObject(req, "sn", sn); + JAddStringToObject(req, c_sn, sn); } success = NoteRequest(req); } @@ -1337,14 +1339,14 @@ bool NoteSetSerialNumber(const char *sn) bool NoteSetUploadMode(const char *uploadMode, int uploadMinutes, bool align) { bool success = false; - J *req = NoteNewRequest("hub.set"); + J *req = NoteNewRequest(c_hub_set); if (req != NULL) { - JAddStringToObject(req, "mode", uploadMode); + JAddStringToObject(req, c_mode, uploadMode); if (uploadMinutes != 0) { - JAddNumberToObject(req, "outbound", uploadMinutes); + JAddNumberToObject(req, c_outbound, uploadMinutes); // Setting this flag aligns uploads to be grouped within the period, // rather than counting the number of minutes from "first modified". - JAddBoolToObject(req, "align", align); + JAddBoolToObject(req, c_align, align); } success = NoteRequest(req); } @@ -1372,21 +1374,21 @@ bool NoteSetUploadMode(const char *uploadMode, int uploadMinutes, bool align) bool NoteSetSyncMode(const char *uploadMode, int uploadMinutes, int downloadMinutes, bool align, bool sync) { bool success = false; - J *req = NoteNewRequest("hub.set"); + J *req = NoteNewRequest(c_hub_set); if (req != NULL) { - JAddStringToObject(req, "mode", uploadMode); + JAddStringToObject(req, c_mode, uploadMode); if (uploadMinutes != 0) { - JAddNumberToObject(req, "outbound", uploadMinutes); + JAddNumberToObject(req, c_outbound, uploadMinutes); // Setting this flag aligns uploads to be grouped within the period, // rather than counting the number of minutes from "first modified". - JAddBoolToObject(req, "align", align); + JAddBoolToObject(req, c_align, align); } if (downloadMinutes != 0) { - JAddNumberToObject(req, "inbound", downloadMinutes); + JAddNumberToObject(req, c_inbound, downloadMinutes); } // Setting this flag when mode is "continuous" causes an immediate sync // when a file is modified on the service side via HTTP - JAddBoolToObject(req, "sync", sync); + JAddBoolToObject(req, c_sync, sync); success = NoteRequest(req); } return success; @@ -1403,13 +1405,13 @@ bool NoteSetSyncMode(const char *uploadMode, int uploadMinutes, int downloadMinu /**************************************************************************/ bool NoteTemplate(const char *target, J *body) { - J *req = NoteNewRequest("note.template"); + J *req = NoteNewRequest(c_note_template); if (req == NULL) { JDelete(body); return false; } - JAddStringToObject(req, "file", target); - JAddItemToObject(req, "body", body); + JAddStringToObject(req, c_file, target); + JAddItemToObject(req, c_body, body); return NoteRequest(req); } @@ -1427,7 +1429,7 @@ bool NoteAdd(const char *target, J *body, bool urgent) { // Initiate the request - J *req = NoteNewRequest("note.add"); + J *req = NoteNewRequest(c_note_add); if (req == NULL) { JDelete(body); return false; @@ -1435,12 +1437,12 @@ bool NoteAdd(const char *target, J *body, bool urgent) // Add the target notefile and body to the request. Note that // JAddItemToObject passes ownership of the object to req - JAddStringToObject(req, "file", target); - JAddItemToObject(req, "body", body); + JAddStringToObject(req, c_file, target); + JAddItemToObject(req, c_body, body); // Initiate sync NOW if it's urgent if (urgent) { - JAddBoolToObject(req, "start", true); + JAddBoolToObject(req, c_start, true); } // Perform the transaction @@ -1463,15 +1465,15 @@ bool NoteSendToRoute(const char *method, const char *routeAlias, char *notefile, { // Create the new event - J *req = NoteNewRequest("note.event"); + J *req = NoteNewRequest(c_note_event); if (req == NULL) { JDelete(body); return false; } // Add the body item and the Notefile name - JAddItemToObject(req, "body", body); - JAddStringToObject(req, "file", notefile); + JAddItemToObject(req, c_body, body); + JAddStringToObject(req, c_file, notefile); // Perform the transaction to convert it to an event J *rsp = NoteRequestResponse(req); @@ -1486,18 +1488,18 @@ bool NoteSendToRoute(const char *method, const char *routeAlias, char *notefile, } // Extract the event, which we'll use as the body for the next transaction - body = JDetachItemFromObject(rsp, "body"); + body = JDetachItemFromObject(rsp, c_body); NoteDeleteResponse(rsp); // Create the post transaction char request[32]; - strlcpy(request, "web.", sizeof(request)); + strlcpy(request, c_web_dot, sizeof(request)); strlcat(request, method, sizeof(request)); req = NoteNewRequest(request); // Add the body, and the alias of the route on the notehub, hard-wired here - JAddItemToObject(req, "body", body); - JAddStringToObject(req, "route", routeAlias); + JAddItemToObject(req, c_body, body); + JAddStringToObject(req, c_route, routeAlias); // Perform the transaction return NoteRequest(req); @@ -1515,10 +1517,10 @@ bool NoteGetVoltage(JNUMBER *voltage) { bool success = false; *voltage = 0.0; - J *rsp = NoteRequestResponse(NoteNewRequest("card.voltage")); + J *rsp = NoteRequestResponse(NoteNewRequest(c_card_voltage)); if (rsp != NULL) { if (!NoteResponseError(rsp)) { - *voltage = JGetNumber(rsp, "value"); + *voltage = JGetNumber(rsp, c_value); success = true; } NoteDeleteResponse(rsp); @@ -1537,10 +1539,10 @@ bool NoteGetTemperature(JNUMBER *temp) { bool success = false; *temp = 0.0; - J *rsp = NoteRequestResponse(NoteNewRequest("card.temp")); + J *rsp = NoteRequestResponse(NoteNewRequest(c_card_temp)); if (rsp != NULL) { if (!NoteResponseError(rsp)) { - *temp = JGetNumber(rsp, "value"); + *temp = JGetNumber(rsp, c_value); success = true; } NoteDeleteResponse(rsp); @@ -1579,21 +1581,21 @@ bool NoteGetContact(char *nameBuf, int nameBufLen, char *orgBuf, int orgBufLen, *emailBuf = '\0'; } - J *rsp = NoteRequestResponse(NoteNewRequest("card.contact")); + J *rsp = NoteRequestResponse(NoteNewRequest(c_card_contact)); if (rsp != NULL) { success = !NoteResponseError(rsp); if (success) { if (nameBuf != NULL) { - strlcpy(nameBuf, JGetString(rsp, "name"), nameBufLen); + strlcpy(nameBuf, JGetString(rsp, c_name), nameBufLen); } if (orgBuf != NULL) { - strlcpy(orgBuf, JGetString(rsp, "org"), orgBufLen); + strlcpy(orgBuf, JGetString(rsp, c_org), orgBufLen); } if (roleBuf != NULL) { - strlcpy(roleBuf, JGetString(rsp, "role"), roleBufLen); + strlcpy(roleBuf, JGetString(rsp, c_role), roleBufLen); } if (emailBuf != NULL) { - strlcpy(emailBuf, JGetString(rsp, "email"), emailBufLen); + strlcpy(emailBuf, JGetString(rsp, c_email), emailBufLen); } } NoteDeleteResponse(rsp); @@ -1614,21 +1616,21 @@ bool NoteGetContact(char *nameBuf, int nameBufLen, char *orgBuf, int orgBufLen, /**************************************************************************/ bool NoteSetContact(char *nameBuf, char *orgBuf, char *roleBuf, char *emailBuf) { - J *req = NoteNewRequest("card.contact"); + J *req = NoteNewRequest(c_card_contact); if (req == NULL) { return false; } if (nameBuf != NULL) { - JAddStringToObject(req, "name", nameBuf); + JAddStringToObject(req, c_name, nameBuf); } if (orgBuf != NULL) { - JAddStringToObject(req, "org", orgBuf); + JAddStringToObject(req, c_org, orgBuf); } if (roleBuf != NULL) { - JAddStringToObject(req, "role", roleBuf); + JAddStringToObject(req, c_role, roleBuf); } if (emailBuf != NULL) { - JAddStringToObject(req, "email", emailBuf); + JAddStringToObject(req, c_email, emailBuf); } return NoteRequest(req); } @@ -1677,12 +1679,12 @@ bool NoteDebugSyncStatus(int pollFrequencyMs, int maxLevel) } // Get the next queued status note - J *req = NoteNewRequest("note.get"); + J *req = NoteNewRequest(c_note_get); if (req == NULL) { return false; } - JAddStringToObject(req, "file", "_synclog.qi"); - JAddBoolToObject(req, "delete", true); + JAddStringToObject(req, c_file, c_synclog_notefile); + JAddBoolToObject(req, c_delete, true); NoteSuspendTransactionDebug(); J *rsp = NoteRequestResponse(req); NoteResumeTransactionDebug(); @@ -1700,14 +1702,16 @@ bool NoteDebugSyncStatus(int pollFrequencyMs, int maxLevel) } // Get the note's body - J *body = JGetObject(rsp, "body"); + J *body = JGetObject(rsp, c_body); if (body != NULL) { - if (maxLevel < 0 || JGetInt(body, "level") <= maxLevel) { - _Debug("sync: "); - _Debug(JGetString(body, "subsystem")); - _Debug(" "); - _Debug(JGetString(body, "text")); - _Debug("\n"); + if (maxLevel < 0 || JGetInt(body, c_level) <= maxLevel) { + _Debug(c_sync); + _Debug(c_colon); + _Debug(c_space); + _Debug(JGetString(body, c_subsystem)); + _Debug(c_space); + _Debug(JGetString(body, c_text)); + _Debug(c_newline); } } diff --git a/src/note-c/n_hooks.c b/src/note-c/n_hooks.c index f0f63b1..6284a90 100644 --- a/src/note-c/n_hooks.c +++ b/src/note-c/n_hooks.c @@ -445,11 +445,11 @@ void *malloc_show(size_t len) { char str[16]; JItoA(len, str); - hookDebugOutput("malloc "); + hookDebugOutput(c_malloc); hookDebugOutput(str); void *p = hookMalloc(len); if (p == NULL) { - hookDebugOutput("FAIL"); + hookDebugOutput(c_FAIL); } else { htoa32((uint32_t)p, str); hookDebugOutput(str); @@ -488,7 +488,7 @@ void NoteFree(void *p) #if NOTE_SHOW_MALLOC char str[16]; htoa32((uint32_t)p, str); - hookDebugOutput("free"); + hookDebugOutput(c_free); hookDebugOutput(str); #endif hookFree(p); @@ -578,11 +578,11 @@ const char *NoteActiveInterface() { switch (hookActiveInterface) { case interfaceSerial: - return "serial"; + return c_serial; case interfaceI2C: - return "i2c"; + return c_i2c; } - return "unknown"; + return c_unknown; } //**************************************************************************/ @@ -673,7 +673,7 @@ const char *NoteI2CTransmit(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size if (hookActiveInterface == interfaceI2C && hookI2CTransmit != NULL) { return hookI2CTransmit(DevAddress, pBuffer, Size); } - return "i2c not active"; + return c_dbg_msg_i2c_not_active; } //**************************************************************************/ @@ -692,7 +692,7 @@ const char *NoteI2CReceive(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size, if (hookActiveInterface == interfaceI2C && hookI2CReceive != NULL) { return hookI2CReceive(DevAddress, pBuffer, Size, available); } - return "i2c not active"; + return c_dbg_msg_i2c_not_active; } //**************************************************************************/ @@ -771,7 +771,7 @@ bool NoteHardReset() const char *NoteJSONTransaction(char *json, char **jsonResponse) { if (notecardTransaction == NULL || hookActiveInterface == interfaceNone) { - return "i2c or serial interface must be selected"; + return c_dbg_msg_i2c_or_serial_interface_must_be_selected; } return notecardTransaction(json, jsonResponse); } diff --git a/src/note-c/n_i2c.c b/src/note-c/n_i2c.c index beb8092..ddf8635 100644 --- a/src/note-c/n_i2c.c +++ b/src/note-c/n_i2c.c @@ -52,7 +52,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse) size_t jsonLen = strlen(json); uint8_t *transmitBuf = (uint8_t *) _Malloc(jsonLen+1); if (transmitBuf == NULL) { - return ERRSTR("insufficient memory",c_mem); + return ERRSTR(c_dbg_msg_insufficient_memory,c_mem); } memcpy(transmitBuf, json, jsonLen); transmitBuf[jsonLen++] = '\n'; @@ -77,9 +77,11 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse) _Free(transmitBuf); _I2CReset(_I2CAddress()); #ifdef ERRDBG - _Debug("i2c transmit: "); + _Debug(c_dbg_msg_i2c_transmit); + _Debug(c_colon); + _Debug(c_space); _Debug(estr); - _Debug("\n"); + _Debug(c_newline); #endif _UnlockI2C(); return estr; @@ -115,10 +117,10 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse) uint8_t *jsonbuf = (uint8_t *) _Malloc(jsonbufAllocLen+1); if (jsonbuf == NULL) { #ifdef ERRDBG - _Debug("transaction: jsonbuf malloc failed\n"); + _Debug(c_dbg_msg_transaction_jsonbuf_malloc_failed); #endif _UnlockI2C(); - return ERRSTR("insufficient memory",c_mem); + return ERRSTR(c_dbg_msg_insufficient_memory,c_mem); } // Loop, building a reply buffer out of received chunks. We'll build the reply in the same @@ -139,11 +141,11 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse) uint8_t *jsonbufNew = (uint8_t *) _Malloc(jsonbufAllocLen+1); if (jsonbufNew == NULL) { #ifdef ERRDBG - _Debug("transaction: jsonbuf grow malloc failed\n"); + _Debug(c_dbg_msg_transaction_jsonbuf_malloc_failed); #endif _Free(jsonbuf); _UnlockI2C(); - return ERRSTR("insufficient memory",c_mem); + return ERRSTR(c_dbg_msg_insufficient_memory,c_mem); } memcpy(jsonbufNew, jsonbuf, jsonbufLen); _Free(jsonbuf); @@ -158,7 +160,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse) if (err != NULL) { _Free(jsonbuf); #ifdef ERRDBG - _Debug("i2c receive error\n"); + _Debug(c_dbg_msg_i2c_receive_error); #endif _UnlockI2C(); return err; @@ -194,10 +196,10 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse) if (_GetMs() >= startMs + (NOTECARD_TRANSACTION_TIMEOUT_SEC*1000)) { _Free(jsonbuf); #ifdef ERRDBG - _Debug("reply to request didn't arrive from module in time\n"); + _Debug(c_dbg_msg_reply_to_request_did_not_arrive_from_module_in_time); #endif _UnlockI2C(); - return ERRSTR("request or response was lost {io}",c_iotimeout); + return ERRSTR(c_dbg_msg_request_or_response_was_lost,c_dbg_msg_io_timeout); } // Delay, simply waiting for the Note to process the request @@ -240,7 +242,7 @@ bool i2cNoteReset() // the remainder of any pending partial reply from a previously-aborted session. // If we get a failure on transmitting the \n, it means that the notecard isn't even present. _DelayIO(); - const char *transmitErr = _I2CTransmit(_I2CAddress(), (uint8_t *)"\n", 1); + const char *transmitErr = _I2CTransmit(_I2CAddress(), (uint8_t *)c_newline, c_newline_len); if (!cardTurboIO) { _DelayMs(CARD_REQUEST_I2C_SEGMENT_DELAY_MS); } @@ -288,7 +290,7 @@ bool i2cNoteReset() // Reinitialize i2c if there's no response if (!notecardReady) { _I2CReset(_I2CAddress()); - _Debug(ERRSTR("notecard not responding\n", "no notecard\n")); + _Debug(ERRSTR(c_dbg_msg_notecard_not_responding, c_dbg_msg_no_notecard)); } // Done with the bus diff --git a/src/note-c/n_lib.h b/src/note-c/n_lib.h index 101be80..b1271cf 100644 --- a/src/note-c/n_lib.h +++ b/src/note-c/n_lib.h @@ -98,45 +98,632 @@ const char *NoteJSONTransaction(char *json, char **jsonResponse); bool NoteIsDebugOutputActive(void); // Constants, a global optimization to save static string memory -extern const char *c_null; -#define c_null_len 4 +extern const char c_agent[]; +#define c_agent_len 5 + +extern const char c_align[]; +#define c_align_len 5 + +extern const char c_arch_arc32[]; +#define c_arch_arc32_len 5 + +extern const char c_arch_avr[]; +#define c_arch_avr_len 3 + +extern const char c_arch_esp32[]; +#define c_arch_esp32_len 5 + +extern const char c_arch_esp8266[]; +#define c_arch_esp8266_len 7 + +extern const char c_arch_megaavr[]; +#define c_arch_megaavr_len 7 + +extern const char c_arch_nrf52840[]; +#define c_arch_nrf52840_len 8 + +extern const char c_arch_nrf52[]; +#define c_arch_nrf52_len 5 + +extern const char c_arch_nrf51[]; +#define c_arch_nrf51_len 5 + +extern const char c_arch_pic32[]; +#define c_arch_pic32_len 5 + +extern const char c_arch_sam[]; +#define c_arch_sam_len 3 + +extern const char c_arch_samd[]; +#define c_arch_samd_len 4 + +extern const char c_arch_spresence[]; +#define c_arch_spresence_len 9 + +extern const char c_arch_stm32[]; +#define c_arch_stm32_len 5 + +extern const char c_arch_stm32f0[]; +#define c_arch_stm32f0_len 7 + +extern const char c_arch_stm32f1[]; +#define c_arch_stm32f1_len 7 + +extern const char c_arch_stm32f4[]; +#define c_arch_stm32f4_len 7 + +extern const char c_arch_stm32g0[]; +#define c_arch_stm32g0_len 7 + +extern const char c_arch_stm32l4[]; +#define c_arch_stm32l4_len 7 + +extern const char c_arch_stm32u5[]; +#define c_arch_stm32u5_len 7 + +extern const char c_arch_swan_r5[]; +#define c_arch_swan_r5_len 7 + +extern const char c_area[]; +#define c_area_len 4 + +extern const char c_array[]; +#define c_array_len 5 + +extern const char c_asterisk[]; +#define c_asterisk_len 1 + +extern const char c_bad[]; +#define c_bad_len 3 + +extern const char c_bearing[]; +#define c_bearing_len 7 + +extern const char c_began_loc_dop[]; +#define c_began_loc_dop_len 13 + +extern const char c_began_loc_lat[]; +#define c_began_loc_lat_len 13 + +extern const char c_began_loc_lon[]; +#define c_began_loc_lon_len 13 + +extern const char c_began_loc_when[]; +#define c_began_loc_when_len 14 + +extern const char c_began_motion_when[]; +#define c_began_motion_when_len 17 + +extern const char c_began_when[]; +#define c_began_when_len 10 + +extern const char c_body[]; +#define c_body_len 4 + +extern const char c_bool[]; +#define c_bool_len 4 + +extern const char c_card_attn[]; +#define c_card_attn_len 9 + +extern const char c_card_contact[]; +#define c_card_contact_len 12 + +extern const char c_card_location[]; +#define c_card_location_len 13 + +extern const char c_card_location_mode[]; +#define c_card_location_mode_len 18 + +extern const char c_card_log[]; +#define c_card_log_len 8 + +extern const char c_card_restore[]; +#define c_card_restore_len 12 + +extern const char c_card_status[]; +#define c_card_status_len 11 + +extern const char c_card_temp[]; +#define c_card_temp_len 9 + +extern const char c_card_time[]; +#define c_card_time_len 9 + +extern const char c_card_version[]; +#define c_card_version_len 12 + +extern const char c_card_voltage[]; +#define c_card_voltage_len 12 + +extern const char c_charging[]; +#define c_charging_len 8 + +extern const char c_cmd[]; +#define c_cmd_len 3 + +extern const char c_colon[]; +#define c_colon_len 1 + +extern const char c_compiler[]; +#define c_compiler_len 8 + +extern const char c_comma[]; +#define c_comma_len 1 + +extern const char c_connected[]; +#define c_connected_len 9 + +extern const char c_country[]; +#define c_country_len 7 + +extern const char c_cpu_cores[]; +#define c_cpu_cores_len 9 + +extern const char c_cpu_mem[]; +#define c_cpu_mem_len 7 + +extern const char c_cpu_mhz[]; +#define c_cpu_mhz_len 7 + +extern const char c_cpu_name[]; +#define c_cpu_name_len 8 + +extern const char c_cpu_vendor[]; +#define c_cpu_vendor_len 10 + +extern const char c_curly_brace_close[]; +#define c_curly_brace_close_len 1 + +extern const char c_curly_brace_open[]; +#define c_curly_brace_open_len 1 + +extern const char c_dbg_msg_about_to_sleep[]; +#define c_dbg_msg_about_to_sleep_len 15 + +extern const char c_dbg_msg_awakened_successfully[]; +#define c_dbg_msg_awakened_successfully_len 22 + +extern const char c_dbg_msg_cannot_convert_to_json[]; +#define c_dbg_msg_cannot_convert_to_json_len 21 + +extern const char c_dbg_msg_card_restored[]; +#define c_dbg_msg_card_restored_len 14 + +extern const char c_dbg_msg_did_not_sleep[]; +#define c_dbg_msg_did_not_sleep_len 13 + +extern const char c_dbg_msg_i2c_not_active[]; +#define c_dbg_msg_i2c_not_active_len 14 + +extern const char c_dbg_msg_i2c_or_serial_interface_must_be_selected[]; +#define c_dbg_msg_i2c_or_serial_interface_must_be_selected_len 40 + +extern const char c_dbg_msg_i2c_receive_error[]; +#define c_dbg_msg_i2c_receive_error_len 18 + +extern const char c_dbg_msg_i2c_transmit[]; +#define c_dbg_msg_i2c_transmit_len 12 + +extern const char c_dbg_msg_insufficient_memory[]; +#define c_dbg_msg_insufficient_memory_len 19 + +extern const char c_dbg_msg_invalid_data_received_on_serial_port_from_notecard[]; +#define c_dbg_msg_invalid_data_received_on_serial_port_from_notecard_len 51 + +extern const char c_dbg_msg_invalid_json[]; +#define c_dbg_msg_invalid_json_len 14 + +extern const char c_dbg_msg_io_bad[]; +#define c_dbg_msg_io_bad_len 8 + +extern const char c_dbg_msg_io_err[]; +#define c_dbg_msg_io_err_len 4 + +extern const char c_dbg_msg_io_serial_communications_error[]; +#define c_dbg_msg_io_serial_communications_error_len 32 + +extern const char c_dbg_msg_io_timeout[]; +#define c_dbg_msg_io_timeout_len 12 + +extern const char c_dbg_msg_io_transaction_incomplete[]; +#define c_dbg_msg_io_transaction_incomplete_len 27 + +extern const char c_dbg_msg_io_transaction_timeout[]; +#define c_dbg_msg_io_transaction_timeout_len 24 + +extern const char c_dbg_msg_io_unrecognized_response_from_card[]; +#define c_dbg_msg_io_unrecognized_response_from_card_len 36 + +extern const char c_dbg_msg_no_notecard[]; +#define c_dbg_msg_no_notecard_len 12 + +extern const char c_dbg_msg_notecard_not_responding[]; +#define c_dbg_msg_notecard_not_responding_len 24 + +extern const char c_dbg_msg_received_only_partial_reply_after_timeout[]; +#define c_dbg_msg_received_only_partial_reply_after_timeout_len 43 + +extern const char c_dbg_msg_reply_to_request_did_not_arrive_from_module_in_time[]; +#define c_dbg_msg_reply_to_request_did_not_arrive_from_module_in_time_len 51 + +extern const char c_dbg_msg_request_or_response_was_lost[]; +#define c_dbg_msg_request_or_response_was_lost_len 33 + +extern const char c_dbg_msg_requesting_sleep[]; +#define c_dbg_msg_requesting_sleep_len 17 + +extern const char c_dbg_msg_transaction_jsonbuf_malloc_failed[]; +#define c_dbg_msg_transaction_jsonbuf_malloc_failed_len 35 + +extern const char c_dbg_msg_unrecognized_data_from_notecard[]; +#define c_dbg_msg_unrecognized_data_from_notecard_len 32 + +extern const char c_delete[]; +#define c_delete_len 6 + +extern const char c_device[]; +#define c_device_len 6 + +extern const char c_distance[]; +#define c_distance_len 8 + +extern const char c_dop[]; +#define c_dop_len 3 + +extern const char c_edge_notefile[]; +#define c_edge_notefile_len 8 + +extern const char c_email[]; +#define c_email_len 5 + +extern const char c_ended_loc_dop[]; +#define c_ended_loc_dop_len 13 + +extern const char c_ended_loc_lat[]; +#define c_ended_loc_lat_len 13 + +extern const char c_ended_loc_lon[]; +#define c_ended_loc_lon_len 13 + +extern const char c_ended_loc_when[]; +#define c_ended_loc_when_len 14 + +extern const char c_ended_motion_when[]; +#define c_ended_motion_when_len 17 + +extern const char c_ended_when[]; +#define c_ended_when_len 10 + +extern const char c_env_default[]; +#define c_env_default_len 11 + +extern const char c_env_get[]; +#define c_env_get_len 7 + +extern const char c_err[]; +#define c_err_len 3 -extern const char *c_false; +extern const char c_FAIL[]; +#define c_FAIL_len 4 + +extern const char c_false[]; #define c_false_len 5 -extern const char *c_true; -#define c_true_len 4 +extern const char c_file[]; +#define c_file_len 4 -extern const char *c_nullstring; -#define c_nullstring_len 0 +extern const char c_fixed[]; +#define c_fixed_len 5 -extern const char *c_newline; -#define c_newline_len 2 +extern const char c_fmt_1_15g[]; +#define c_fmt_1_15g_len 6 + +extern const char c_fmt_1_17g[]; +#define c_fmt_1_17g_len 6 + +extern const char c_fmt_lg[]; +#define c_fmt_lg_len 3 + +extern const char c_free[]; +#define c_free_len 4 + +extern const char c_heartbeat[]; +#define c_heartbeat_len 9 + +extern const char c_HEX[]; +#define c_HEX_len 16 + +extern const char c_hex[]; +#define c_hex_len 16 + +extern const char c_host[]; +#define c_host_len 4 + +extern const char c_hub_get[]; +#define c_hub_get_len 7 + +extern const char c_hub_set[]; +#define c_hub_set_len 7 + +extern const char c_hub_status[]; +#define c_hub_status_len 10 + +extern const char c_humidity[]; +#define c_humidity_len 8 + +extern const char c_hyphen[]; +#define c_hyphen_len 1 + +extern const char c_i2c[]; +#define c_i2c_len 3 + +extern const char c_inbound[]; +#define c_inbound_len 7 + +extern const char c_INF[]; +#define c_INF_len 3 + +extern const char c_inf[]; +#define c_inf_len 3 + +extern const char c_invalid[]; +#define c_invalid_len 7 + +extern const char c_jcount[]; +#define c_jcount_len 6 + +extern const char c_journey[]; +#define c_journey_len 7 + +extern const char c_lat[]; +#define c_lat_len 3 + +extern const char c_level[]; +#define c_level_len 5 + +extern const char c_loc_lat[]; +#define c_loc_lat_len 7 -extern const char *c_mem; +extern const char c_loc_lon[]; +#define c_loc_lon_len 7 + +extern const char c_log[]; +#define c_log_len 3 + +extern const char c_lon[]; +#define c_lon_len 3 + +extern const char c_malloc[]; +#define c_malloc_len 6 + +extern const char c_mem[]; #define c_mem_len 3 -extern const char *c_iotimeout; -#define c_iotimeout_len 12 +extern const char c_meters[]; +#define c_meters_len 6 -extern const char *c_err; -#define c_err_len 3 +extern const char c_minutes[]; +#define c_minutes_len 7 + +extern const char c_mode[]; +#define c_mode_len 4 + +extern const char c_motion[]; +#define c_motion_len 6 + +extern const char c_movements[]; +#define c_movements_len 9 + +extern const char c_mtime[]; +#define c_mtime_len 5 + +extern const char c_name[]; +#define c_name_len 4 + +extern const char c_NAN[]; +#define c_NAN_len 3 + +extern const char c_nan[]; +#define c_nan_len 3 + +extern const char c_newline[]; +#define c_newline_len 2 + +extern const char c_no_sat[]; +#define c_no_sat_len 6 + +extern const char c_note_add[]; +#define c_note_add_len 8 + +extern const char c_note_c[]; +#define c_note_c_len 6 + +extern const char c_note_event[]; +#define c_note_event_len 10 + +extern const char c_note_get[]; +#define c_note_get_len 8 + +extern const char c_note_template[]; +#define c_note_template_len 13 + +extern const char c_null[]; +#define c_null_len 4 + +extern const char c_null_paren[]; +#define c_null_paren_len 6 + +extern const char c_nullstring[]; +#define c_nullstring_len 0 + +extern const char c_number[]; +#define c_number_len 6 -extern const char *c_req; +extern const char c_object[]; +#define c_object_len 6 + +extern const char c_off[]; +#define c_off_len 3 + +extern const char c_org[]; +#define c_org_len 3 + +extern const char c_orientation[]; +#define c_orientation_len 11 + +extern const char c_os_family[]; +#define c_os_family_len 9 + +extern const char c_os_name[]; +#define c_os_name_len 7 + +extern const char c_os_platform[]; +#define c_os_platform_len 11 + +extern const char c_os_version[]; +#define c_os_version_len 10 + +extern const char c_outbound[]; +#define c_outbound_len 8 + +extern const char c_payload[]; +#define c_payload_len 7 + +extern const char c_point[]; +#define c_point_len 5 + +extern const char c_pressure[]; +#define c_pressure_len 8 + +extern const char c_product[]; +#define c_product_len 7 + +extern const char c_quotation_mark[]; +#define c_quotation_mark_len 1 + +extern const char c_req[]; #define c_req_len 3 -extern const char *c_cmd; -#define c_cmd_len 3 +extern const char c_req_interface[]; +#define c_req_interface_len 13 -extern const char *c_bad; -#define c_bad_len 3 +extern const char c_role[]; +#define c_role_len 4 + +extern const char c_route[]; +#define c_route_len 5 + +extern const char c_scan[]; +#define c_scan_len 4 + +extern const char c_scan_format_2g[]; +#define c_scan_format_2g_len 25 + +extern const char c_scan_format_3g[]; +#define c_scan_format_3g_len 30 + +extern const char c_scan_format_4g[]; +#define c_scan_format_4g_len 51 + +extern const char c_scan_format_5g[]; +#define c_scan_format_5g_len 51 + +extern const char c_scan_format_gps[]; +#define c_scan_format_gps_len 18 + +extern const char c_scan_format_time[]; +#define c_scan_format_time_len 9 + +extern const char c_scan_format_wifi[]; +#define c_scan_format_wifi_len 37 + +extern const char c_seconds[]; +#define c_seconds_len 7 + +extern const char c_secs[]; +#define c_secs_len 4 + +extern const char c_serial[]; +#define c_serial_len 6 + +extern const char c_signals[]; +#define c_signals_len 7 + +extern const char c_sleep[]; +#define c_sleep_len 5 + +extern const char c_sn[]; +#define c_sn_len 2 + +extern const char c_space[]; +#define c_space_len 1 + +extern const char c_start[]; +#define c_start_len 5 + +extern const char c_status[]; +#define c_status_len 6 + +extern const char c_string[]; +#define c_string_len 6 + +extern const char c_subsystem[]; +#define c_subsystem_len 9 + +extern const char c_sync[]; +#define c_sync_len 4 + +extern const char c_synclog_notefile[]; +#define c_synclog_notefile_len 11 + +extern const char c_temperature[]; +#define c_temperature_len 11 + +extern const char c_text[]; +#define c_text_len 4 + +extern const char c_time[]; +#define c_time_len 4 + +extern const char c_track[]; +#define c_track_len 5 + +extern const char c_true[]; +#define c_true_len 4 + +extern const char c_type[]; +#define c_type_len 4 + +extern const char c_unknown[]; +#define c_unknown_len 7 + +extern const char c_usb[]; +#define c_usb_len 3 + +extern const char c_UTC[]; +#define c_UTC_len 3 + +extern const char c_utf8_bom[]; +#define c_utf8_bom_len 3 + +extern const char c_value[]; +#define c_value_len 5 + +extern const char c_velocity[]; +#define c_velocity_len 8 + +extern const char c_version[]; +#define c_version_len 7 -extern const char *c_iobad; -#define c_iobad_len 8 +extern const char c_voltage[]; +#define c_voltage_len 7 -extern const char *c_ioerr; -#define c_ioerr_len 4 +extern const char c_web_dot[]; +#define c_web_dot_len 4 +extern const char c_zone[]; +#define c_zone_len 4 // Readability wrappers. Anything starting with _ is simply calling the wrapper // function. diff --git a/src/note-c/n_request.c b/src/note-c/n_request.c index c46da9f..426a79c 100644 --- a/src/note-c/n_request.c +++ b/src/note-c/n_request.c @@ -34,9 +34,16 @@ static J *errDoc(const char *errmsg) JAddStringToObject(rspdoc, c_err, errmsg); } if (suppressShowTransactions == 0) { - _Debug("{\"err\":\""); + _Debug(c_curly_brace_open); + _Debug(c_quotation_mark); + _Debug(c_err); + _Debug(c_quotation_mark); + _Debug(c_colon); + _Debug(c_quotation_mark); _Debug(errmsg); - _Debug("\"}\n"); + _Debug(c_quotation_mark); + _Debug(c_curly_brace_close); + _Debug(c_newline); } return rspdoc; } @@ -137,6 +144,7 @@ bool NoteRequest(J *req) bool NoteRequestWithRetry(J *req, uint32_t timeoutSeconds) { J *rsp = NoteRequestResponseWithRetry(req, timeoutSeconds); + // If there is no response return false if (rsp == NULL) { return false; @@ -210,7 +218,7 @@ J *NoteRequestResponseWithRetry(J *req, uint32_t timeoutSeconds) rsp = NoteTransaction(req); // Loop if there is no response, or if there is an io error - if ( (rsp == NULL) || JContainsString(rsp, c_err, c_ioerr)) { + if ( (rsp == NULL) || JContainsString(rsp, c_err, c_dbg_msg_io_err)) { // Free error response if (rsp != NULL) { @@ -302,18 +310,18 @@ J *NoteTransaction(J *req) } // Determine the request or command type - const char *reqType = JGetString(req, "req"); - const char *cmdType = JGetString(req, "cmd"); + const char *reqType = JGetString(req, c_req); + const char *cmdType = JGetString(req, c_cmd); // Add the user agent object only when we're doing a hub.set and only when we're // specifying the product UID. The intent is that we only piggyback user agent // data when the host is initializing the Notecard, as opposed to every time // the host does a hub.set to change mode. #ifndef NOTE_DISABLE_USER_AGENT - if (!JIsPresent(req, "body") && (strcmp(reqType, "hub.set") == 0) && JIsPresent(req, "product")) { + if (!JIsPresent(req, c_body) && (strcmp(reqType, c_hub_set) == 0) && JIsPresent(req, c_product)) { J *body = NoteUserAgent(); if (body != NULL) { - JAddItemToObject(req, "body", body); + JAddItemToObject(req, c_body, body); } } #endif @@ -336,7 +344,7 @@ J *NoteTransaction(J *req) // Serialize the JSON request char *json = JPrintUnformatted(req); if (json == NULL) { - J *rsp = errDoc(ERRSTR("can't convert to JSON",c_bad)); + J *rsp = errDoc(ERRSTR(c_dbg_msg_cannot_convert_to_json,c_bad)); _UnlockNote(); _TransactionStop(); return rsp; @@ -378,11 +386,11 @@ J *NoteTransaction(J *req) J *rspdoc = JParse(responseJSON); if (rspdoc == NULL) { if (responseJSON != NULL) { - _Debug("invalid JSON: "); + _Debug(c_dbg_msg_invalid_json); _Debug(responseJSON); _Free(responseJSON); } - J *rsp = errDoc(ERRSTR("unrecognized response from card {io}",c_iobad)); + J *rsp = errDoc(ERRSTR(c_dbg_msg_io_unrecognized_response_from_card,c_dbg_msg_io_bad)); _UnlockNote(); _TransactionStop(); return rsp; diff --git a/src/note-c/n_serial.c b/src/note-c/n_serial.c index 1d5d3eb..8462622 100644 --- a/src/note-c/n_serial.c +++ b/src/note-c/n_serial.c @@ -34,7 +34,7 @@ const char *serialNoteTransaction(char *json, char **jsonResponse) int jsonLen = strlen(json); uint8_t *transmitBuf = (uint8_t *) _Malloc(jsonLen+c_newline_len); if (transmitBuf == NULL) { - return ERRSTR("insufficient memory",c_mem); + return ERRSTR(c_dbg_msg_insufficient_memory,c_mem); } memcpy(transmitBuf, json, jsonLen); memcpy(&transmitBuf[jsonLen], c_newline, c_newline_len); @@ -75,9 +75,9 @@ const char *serialNoteTransaction(char *json, char **jsonResponse) for (startMs = _GetMs(); !_SerialAvailable(); ) { if (_GetMs() >= startMs + (NOTECARD_TRANSACTION_TIMEOUT_SEC*1000)) { #ifdef ERRDBG - _Debug("reply to request didn't arrive from module in time\n"); + _Debug(c_dbg_msg_reply_to_request_did_not_arrive_from_module_in_time); #endif - return ERRSTR("transaction timeout {io}",c_iotimeout); + return ERRSTR(c_dbg_msg_io_transaction_timeout,c_dbg_msg_io_timeout); } if (!cardTurboIO) { _DelayMs(10); @@ -91,9 +91,9 @@ const char *serialNoteTransaction(char *json, char **jsonResponse) char *jsonbuf = (char *) _Malloc(jsonbufAllocLen+1); if (jsonbuf == NULL) { #ifdef ERRDBG - _Debug("transaction: jsonbuf malloc failed\n"); + _Debug(c_dbg_msg_transaction_jsonbuf_malloc_failed); #endif - return ERRSTR("insufficient memory",c_mem); + return ERRSTR(c_dbg_msg_insufficient_memory,c_mem); } int jsonbufLen = 0; char ch = 0; @@ -104,12 +104,12 @@ const char *serialNoteTransaction(char *json, char **jsonResponse) if (_GetMs() >= startMs + (NOTECARD_TRANSACTION_TIMEOUT_SEC*1000)) { #ifdef ERRDBG jsonbuf[jsonbufLen] = '\0'; - _Debug("received only partial reply after timeout:\n"); + _Debug(c_dbg_msg_received_only_partial_reply_after_timeout); _Debug(jsonbuf); - _Debug("\n"); + _Debug(c_newline); #endif _Free(jsonbuf); - return ERRSTR("transaction incomplete {io}",c_iotimeout); + return ERRSTR(c_dbg_msg_io_transaction_incomplete,c_dbg_msg_io_timeout); } if (!cardTurboIO) { _DelayMs(1); @@ -128,10 +128,10 @@ const char *serialNoteTransaction(char *json, char **jsonResponse) // if (ch == 0) { #ifdef ERRDBG - _Debug("invalid data received on serial port from notecard\n"); + _Debug(c_dbg_msg_invalid_data_received_on_serial_port_from_notecard); #endif _Free(jsonbuf); - return ERRSTR("serial communications error {io}",c_iotimeout); + return ERRSTR(c_dbg_msg_io_serial_communications_error,c_dbg_msg_io_timeout); } // Append into the json buffer @@ -141,10 +141,10 @@ const char *serialNoteTransaction(char *json, char **jsonResponse) char *jsonbufNew = (char *) _Malloc(jsonbufAllocLen+1); if (jsonbufNew == NULL) { #ifdef ERRDBG - _Debug("transaction: jsonbuf malloc grow failed\n"); + _Debug(c_dbg_msg_transaction_jsonbuf_malloc_failed); #endif _Free(jsonbuf); - return ERRSTR("insufficient memory",c_mem); + return ERRSTR(c_dbg_msg_insufficient_memory,c_mem); } memcpy(jsonbufNew, jsonbuf, jsonbufLen); _Free(jsonbuf); @@ -207,9 +207,9 @@ bool serialNoteReset() } #ifdef ERRDBG - _Debug(somethingFound ? "unrecognized data from notecard\n" : "notecard not responding\n"); + _Debug(somethingFound ? c_dbg_msg_unrecognized_data_from_notecard : c_dbg_msg_notecard_not_responding); #else - _Debug("no notecard\n"); + _Debug(c_dbg_msg_no_notecard); #endif _DelayMs(500); if (!_SerialReset()) { diff --git a/src/note-c/n_ua.c b/src/note-c/n_ua.c index a216b37..d898c18 100644 --- a/src/note-c/n_ua.c +++ b/src/note-c/n_ua.c @@ -16,7 +16,7 @@ #include "n_lib.h" // Override-able statics -static char *n_agent = (char *) "note-c"; +static char *n_agent = (char *) c_note_c; static char *n_os_name = NULL; static char *n_os_platform = NULL; static char *n_os_family = NULL; @@ -88,80 +88,80 @@ __attribute__((weak)) J *NoteUserAgent() #endif #if defined(ARDUINO_ARCH_ARC32) - n_cpu_name = (char *) "arc32"; + n_cpu_name = (char *) c_arch_arc32; #elif defined(ARDUINO_ARCH_AVR) - n_cpu_name = (char *) "avr"; + n_cpu_name = (char *) c_arch_avr; #elif defined(ARDUINO_ARCH_ESP32) - n_cpu_name = (char *) "esp32"; + n_cpu_name = (char *) c_arch_esp32; #elif defined(ARDUINO_ARCH_ESP8266) - n_cpu_name = (char *) "esp8266"; + n_cpu_name = (char *) c_arch_esp8266; #elif defined(ARDUINO_ARCH_MEGAAVR) - n_cpu_name = (char *) "megaavr"; + n_cpu_name = (char *) c_arch_megaavr; #elif defined(ARDUINO_ARCH_NRF52840) - n_cpu_name = (char *) "nrf52840"; + n_cpu_name = (char *) c_arch_nrf52840; #elif defined(ARDUINO_ARCH_NRF52) - n_cpu_name = (char *) "nrf52"; + n_cpu_name = (char *) c_arch_nrf52; #elif defined(ARDUINO_ARCH_NRF51) - n_cpu_name = (char *) "nrf51"; + n_cpu_name = (char *) c_arch_nrf51; #elif defined(ARDUINO_ARCH_PIC32) - n_cpu_name = (char *) "pic32"; + n_cpu_name = (char *) c_arch_pic32; #elif defined(ARDUINO_ARCH_SAMD) - n_cpu_name = (char *) "samd"; + n_cpu_name = (char *) c_arch_samd; #elif defined(ARDUINO_ARCH_SAM) - n_cpu_name = (char *) "sam"; + n_cpu_name = (char *) c_arch_sam; #elif defined(ARDUINO_ARCH_SPRESENSE) - n_cpu_name = (char *) "spresence"; + n_cpu_name = (char *) c_arch_spresence; #elif defined(ARDUINO_ARCH_STM32F0) - n_cpu_name = (char *) "stm32f0"; + n_cpu_name = (char *) c_arch_stm32f0; #elif defined(ARDUINO_ARCH_STM32F1) - n_cpu_name = (char *) "stm32f1"; + n_cpu_name = (char *) c_arch_stm32f1; #elif defined(ARDUINO_ARCH_STM32F4) - n_cpu_name = (char *) "stm32f4"; + n_cpu_name = (char *) c_arch_stm32f4; #elif defined(ARDUINO_ARCH_STM32G0) - n_cpu_name = (char *) "stm32g0"; + n_cpu_name = (char *) c_arch_stm32g0; #elif defined(ARDUINO_SWAN_R5) - n_cpu_name = (char *) "swan_r5"; + n_cpu_name = (char *) c_arch_swan_r5; #elif defined(ARDUINO_ARCH_STM32L4) - n_cpu_name = (char *) "stm32l4"; + n_cpu_name = (char *) c_arch_stm32l4; #elif defined(ARDUINO_ARCH_STM32U5) - n_cpu_name = (char *) "stm32u5"; + n_cpu_name = (char *) c_arch_stm32u5; #elif defined(ARDUINO_ARCH_STM32) - n_cpu_name = (char *) "stm32"; + n_cpu_name = (char *) c_arch_stm32; #else - n_cpu_name = (char *) ""; + n_cpu_name = (char *) c_nullstring; #endif - JAddStringToObject(ua, "agent", n_agent); - JAddStringToObject(ua, "compiler", compiler); - JAddStringToObject(ua, "req_interface", NoteActiveInterface()); + JAddStringToObject(ua, c_agent, n_agent); + JAddStringToObject(ua, c_compiler, compiler); + JAddStringToObject(ua, c_req_interface, NoteActiveInterface()); if (n_cpu_mem != 0) { - JAddNumberToObject(ua, "cpu_mem", n_cpu_mem); + JAddNumberToObject(ua, c_cpu_mem, n_cpu_mem); } if (n_cpu_mhz != 0) { - JAddNumberToObject(ua, "cpu_mhz", n_cpu_mhz); + JAddNumberToObject(ua, c_cpu_mhz, n_cpu_mhz); } if (n_cpu_cores != 0) { - JAddNumberToObject(ua, "cpu_cores", n_cpu_cores); + JAddNumberToObject(ua, c_cpu_cores, n_cpu_cores); } if (n_cpu_vendor != NULL) { - JAddStringToObject(ua, "cpu_vendor", n_cpu_vendor); + JAddStringToObject(ua, c_cpu_vendor, n_cpu_vendor); } if (n_cpu_name != NULL) { - JAddStringToObject(ua, "cpu_name", n_cpu_name); + JAddStringToObject(ua, c_cpu_name, n_cpu_name); } if (n_os_name != NULL) { - JAddStringToObject(ua, "os_name", n_os_name); + JAddStringToObject(ua, c_os_name, n_os_name); } if (n_os_platform != NULL) { - JAddStringToObject(ua, "os_platform", n_os_platform); + JAddStringToObject(ua, c_os_platform, n_os_platform); } if (n_os_family != NULL) { - JAddStringToObject(ua, "os_family", n_os_family); + JAddStringToObject(ua, c_os_family, n_os_family); } if (n_os_version != NULL) { - JAddStringToObject(ua, "os_version", n_os_version); + JAddStringToObject(ua, c_os_version, n_os_version); } // Add more data to the UA from a higher level diff --git a/src/note-c/note.h b/src/note-c/note.h index 3f690b7..d4157a7 100644 --- a/src/note-c/note.h +++ b/src/note-c/note.h @@ -105,8 +105,8 @@ typedef void (*txnStopFn) (void); bool NoteReset(void); void NoteResetRequired(void); #define NoteNewBody JCreateObject -#define NoteAddBodyToObject(a, b) JAddItemToObject(a, "body", b) -#define NoteGetBody(a) JGetObject(a, "body") +#define NoteAddBodyToObject(a, b) JAddItemToObject(a, c_body, b) +#define NoteGetBody(a) JGetObject(a, c_body) J *NoteNewRequest(const char *request); J *NoteNewCommand(const char *request); J *NoteRequestResponse(J *req); @@ -122,8 +122,8 @@ void NoteResumeTransactionDebug(void); bool NoteDebugSyncStatus(int pollFrequencyMs, int maxLevel); bool NoteRequest(J *req); bool NoteRequestWithRetry(J *req, uint32_t timeoutms); -#define NoteResponseError(rsp) (!JIsNullString(rsp, "err")) -#define NoteResponseErrorContains(rsp, errstr) (JContainsString(rsp, "err", errstr)) +#define NoteResponseError(rsp) (!JIsNullString(rsp, c_err)) +#define NoteResponseErrorContains(rsp, errstr) (JContainsString(rsp, c_err, errstr)) #define NoteDeleteResponse(rsp) JDelete(rsp) J *NoteTransaction(J *req); bool NoteErrorContains(const char *errstr, const char *errtype); From 64e9b904e6c0ac7ac26f039781001f8961659e0c Mon Sep 17 00:00:00 2001 From: "Zachary J. Fields" Date: Fri, 23 Dec 2022 09:11:58 -0600 Subject: [PATCH 2/2] WIP: git reset HEAD~ --- .devcontainer/devcontainer.json | 4 +- .../Example1_NotecardBasics.ino | 64 +++++++++++-------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 00dc9c8..0d6c5e1 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -7,8 +7,8 @@ "context": "..", // Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename. - //"dockerFile": "../.github/actions/compile-examples/Dockerfile", - "dockerFile": "../.github/actions/run-tests-in-container/Dockerfile", + "dockerFile": "../.github/actions/compile-examples/Dockerfile", + //"dockerFile": "../.github/actions/run-tests-in-container/Dockerfile", // Set *default* container specific settings.json values on container create. "settings": {}, diff --git a/examples/Example1_NotecardBasics/Example1_NotecardBasics.ino b/examples/Example1_NotecardBasics/Example1_NotecardBasics.ino index 33b09b7..2d8b680 100644 --- a/examples/Example1_NotecardBasics/Example1_NotecardBasics.ino +++ b/examples/Example1_NotecardBasics/Example1_NotecardBasics.ino @@ -17,7 +17,6 @@ // Include the Arduino library for the Notecard #include -#include // If the Notecard is connected to a serial port, define it here. For example, if you are using // the Adafruit Feather NRF52840 Express, the RX/TX pins (and thus the Notecard) are on Serial1. @@ -44,7 +43,7 @@ // This is the unique Product Identifier for your device #ifndef PRODUCT_UID -#define PRODUCT_UID "" // "com.my-company.my-name:my-project" +#define PRODUCT_UID "com.zakoverflow.test" // "com.my-company.my-name:my-project" #pragma message "PRODUCT_UID is not defined in this example. Please ensure your Notecard has a product identifier set before running this example or define it in code here. More details at https://dev.blues.io/tools-and-sdks/samples/product-uid" #endif @@ -55,6 +54,7 @@ Notecard notecard; void setup() { +#ifdef usbSerial // Set up for debug output. If you open Arduino's serial terminal window, you'll be able to // watch JSON objects being transferred to and from the Notecard for each request. On most // Arduino devices, Arduino's serial debug output is on the "Serial" device at 115200. @@ -63,55 +63,63 @@ void setup() // Note that the initial 2.5s delay is required by some Arduino cards before debug // UART output can be successfully displayed in the Arduino IDE, including the // Adafruit Feather nRF52840 Express. -#ifdef usbSerial - delay(2500); - usbSerial.begin(115200); notecard.setDebugOutputStream(usbSerial); + usbSerial.begin(9600); + const size_t start_wait_ms = millis(); + while (!usbSerial && ((millis() - start_wait_ms) < 5000)); + Serial.println("Serial READY"); #endif // Initialize the physical I/O channel to the Notecard #ifdef txRxPinsSerial notecard.begin(txRxPinsSerial, 9600); #else - Wire.begin(); - notecard.begin(); + Serial.println("1"); #endif // "newRequest()" uses the bundled "J" json package to allocate a "req", which is a JSON object // for the request to which we will then add Request arguments. The function allocates a "req" // request structure using malloc() and initializes its "req" field with the type of request. J *req = notecard.newRequest("hub.set"); - - // This command (required) causes the data to be delivered to the Project on notehub.io that has claimed - // this Product ID. (see above) - if (myProductID[0]) { - JAddStringToObject(req, "product", myProductID); + Serial.println("2"); + if (req) { + // This command (required) causes the data to be delivered to the Project on notehub.io that has claimed + // this Product ID. (see above) + if (myProductID[0]) { + JAddStringToObject(req, "product", myProductID); + Serial.println("3"); + } + // This command determines how often the Notecard connects to the service. If "continuous" the Notecard + // immediately establishes a session with the service at notehub.io, and keeps it active continuously. + // Because of the power requirements of a continuous connection, a battery powered device would instead + // only sample its sensors occasionally, and would only upload to the service on a periodic basis. + JAddStringToObject(req, "mode", "continuous"); + Serial.println("4"); + + // Issue the request, telling the Notecard how and how often to access the service. + // This results in a JSON message to Notecard formatted like: + // { "req" : "service.set", + // "product" : myProductID, + // "mode" : "continuous" + // } + // Note that sendRequest() always uses free() to release the request data structure, and it + // returns "true" if success and "false" if there is any failure. + notecard.sendRequest(req); + Serial.println("5"); } - // This command determines how often the Notecard connects to the service. If "continuous" the Notecard - // immediately establishes a session with the service at notehub.io, and keeps it active continuously. - // Because of the power requirements of a continuous connection, a battery powered device would instead - // only sample its sensors occasionally, and would only upload to the service on a periodic basis. - JAddStringToObject(req, "mode", "continuous"); - - // Issue the request, telling the Notecard how and how often to access the service. - // This results in a JSON message to Notecard formatted like: - // { "req" : "service.set", - // "product" : myProductID, - // "mode" : "continuous" - // } - // Note that sendRequest() always uses free() to release the request data structure, and it - // returns "true" if success and "false" if there is any failure. - notecard.sendRequest(req); + } // In the Arduino main loop which is called repeatedly, add outbound data every 15 seconds void loop() { + Serial.println("Begin `loop`"); // Count the simulated measurements that we send to the cloud, and stop the demo before long. static unsigned eventCounter = 0; - if (eventCounter++ > 25) { + if (eventCounter > 25) { + ++eventCounter; return; }