Skip to content

Commit

Permalink
chore: move strings to PROGMEM
Browse files Browse the repository at this point in the history
  • Loading branch information
zfields committed Nov 6, 2022
1 parent f82ece2 commit 60480e7
Show file tree
Hide file tree
Showing 15 changed files with 1,143 additions and 347 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*.gcov
vgcore.*

.vscode/
build/
coverage/

Doxyfile*
Expand Down
6 changes: 4 additions & 2 deletions src/note-c/n_b64.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@
* Base64 encoder/decoder. Originally Apache file ap_base64.c
*/

#include <avr/pgmspace.h>
#include <string.h>

#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,
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions src/note-c/n_cjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -1003,15 +1003,15 @@ 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);
}

N_CJSON_PUBLIC(char *) JPrintUnformatted(const J *item)
{
if (item == NULL) {
return (char *)"";
return (char *)c_nullstring;
}
return (char*)print(item, false);
}
Expand All @@ -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) {
Expand Down
20 changes: 10 additions & 10 deletions src/note-c/n_cjson_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ bool JBoolValue(J *item)
char *JStringValue(J *item)
{
if (item == NULL) {
return (char *)"";
return (char *)c_nullstring;
}
return item->valuestring;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -494,23 +494,23 @@ 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;
}
222 changes: 209 additions & 13 deletions src/note-c/n_const.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,214 @@
*
*/

#include <avr/pgmspace.h>

#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_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_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";
Loading

0 comments on commit 60480e7

Please sign in to comment.