From 3aada5db01cc11a3a2d032d68b458948a2a4da70 Mon Sep 17 00:00:00 2001 From: "Zachary J. Fields" Date: Fri, 8 Nov 2024 20:31:48 -0600 Subject: [PATCH 1/2] chore: rename non-public API --- docs/library_initialization.rst | 34 +- n_cjson.c | 328 +++++------ n_cobs.c | 14 +- n_helpers.c | 72 +-- n_hooks.c | 56 +- n_i2c.c | 30 +- n_lib.h | 98 ++-- n_request.c | 51 +- n_serial.c | 12 +- n_ua.c | 2 +- test/CMakeLists.txt | 24 +- .../lib/notecard_binary/NotecardComms.cpp | 4 +- test/include/test_static.h | 14 +- test/src/JSON_number_handling_test.cpp | 2 +- test/src/NoteBinaryCodecDecode_test.cpp | 20 +- test/src/NoteBinaryCodecEncode_test.cpp | 20 +- .../NoteBinaryCodecMaxDecodedLength_test.cpp | 14 +- .../NoteBinaryCodecMaxEncodedLength_test.cpp | 14 +- test/src/NoteBinaryStoreReceive_test.cpp | 58 +- test/src/NoteBinaryStoreTransmit_test.cpp | 32 +- test/src/NoteDebug_test.cpp | 4 +- test/src/NotePrint_test.cpp | 12 +- test/src/NoteRequestResponseJSON_test.cpp | 106 ++-- test/src/NoteReset_test.cpp | 16 +- test/src/NoteResponseError_test.cpp | 12 +- test/src/NoteSerialHooks_test.cpp | 10 +- test/src/NoteSetFnI2C_test.cpp | 44 +- test/src/NoteSetFnMutex_test.cpp | 12 +- test/src/NoteSetFnNoteMutex_test.cpp | 12 +- test/src/NoteSetFnSerial_test.cpp | 53 +- test/src/NoteTransactionHooks_test.cpp | 12 +- test/src/NoteTransaction_test.cpp | 97 ++-- .../src/{crcAdd_test.cpp => _crcAdd_test.cpp} | 12 +- .../{crcError_test.cpp => _crcError_test.cpp} | 22 +- ...e_test.cpp => _i2cChunkedReceive_test.cpp} | 98 ++-- ..._test.cpp => _i2cChunkedTransmit_test.cpp} | 58 +- ..._test.cpp => _i2cNoteQueryLength_test.cpp} | 56 +- test/src/_i2cNoteReset_test.cpp | 509 ++++++++++++++++++ ..._test.cpp => _i2cNoteTransaction_test.cpp} | 90 ++-- ...{Jtolower_test.cpp => _j_tolower_test.cpp} | 16 +- ...est.cpp => _serialChunkedReceive_test.cpp} | 86 +-- ...st.cpp => _serialChunkedTransmit_test.cpp} | 64 +-- test/src/_serialNoteReset_test.cpp | 135 +++++ ...st.cpp => _serialNoteTransaction_test.cpp} | 166 +++--- test/src/i2cNoteReset_test.cpp | 509 ------------------ test/src/serialNoteReset_test.cpp | 135 ----- 46 files changed, 1619 insertions(+), 1626 deletions(-) rename test/src/{crcAdd_test.cpp => _crcAdd_test.cpp} (82%) rename test/src/{crcError_test.cpp => _crcError_test.cpp} (77%) rename test/src/{i2cChunkedReceive_test.cpp => _i2cChunkedReceive_test.cpp} (79%) rename test/src/{i2cChunkedTransmit_test.cpp => _i2cChunkedTransmit_test.cpp} (59%) rename test/src/{i2cNoteQueryLength_test.cpp => _i2cNoteQueryLength_test.cpp} (61%) create mode 100644 test/src/_i2cNoteReset_test.cpp rename test/src/{i2cNoteTransaction_test.cpp => _i2cNoteTransaction_test.cpp} (69%) rename test/src/{Jtolower_test.cpp => _j_tolower_test.cpp} (80%) rename test/src/{serialChunkedReceive_test.cpp => _serialChunkedReceive_test.cpp} (75%) rename test/src/{serialChunkedTransmit_test.cpp => _serialChunkedTransmit_test.cpp} (57%) create mode 100644 test/src/_serialNoteReset_test.cpp rename test/src/{serialNoteTransaction_test.cpp => _serialNoteTransaction_test.cpp} (55%) delete mode 100644 test/src/i2cNoteReset_test.cpp delete mode 100644 test/src/serialNoteReset_test.cpp diff --git a/docs/library_initialization.rst b/docs/library_initialization.rst index 9b84c366..12356059 100644 --- a/docs/library_initialization.rst +++ b/docs/library_initialization.rst @@ -144,17 +144,17 @@ The code below from `Src/main.c 'Z') { return c; @@ -130,7 +130,7 @@ NOTE_C_STATIC char Jtolower(char c) } /* Case insensitive string comparison, doesn't consider two NULL pointers equal though */ -static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2) +NOTE_C_STATIC int _case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2) { if ((string1 == NULL) || (string2 == NULL)) { return 1; @@ -140,16 +140,16 @@ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned return 0; } - for(; Jtolower(*string1) == Jtolower(*string2); (void)string1++, string2++) { + for(; _j_tolower(*string1) == _j_tolower(*string2); (void)string1++, string2++) { if (*string1 == '\0') { return 0; } } - return Jtolower(*string1) - Jtolower(*string2); + return _j_tolower(*string1) - _j_tolower(*string2); } -static unsigned char* Jstrdup(const unsigned char* string) +NOTE_C_STATIC unsigned char* _j_strdup(const unsigned char* string) { size_t length = 0; unsigned char *copy = NULL; @@ -197,7 +197,7 @@ N_CJSON_PUBLIC(void) JFree(void *p) } /* Internal constructor. */ -static J *JNew_Item(void) +NOTE_C_STATIC J *_jNew_Item(void) { J* node = (J*)_Malloc(sizeof(J)); if (node) { @@ -232,7 +232,7 @@ N_CJSON_PUBLIC(void) JDelete(J *item) } /* get the decimal point character of the current locale */ -static unsigned char get_decimal_point(void) +NOTE_C_STATIC unsigned char _get_decimal_point(void) { #ifdef ENABLE_LOCALES struct lconv *lconv = localeconv(); @@ -258,12 +258,12 @@ typedef struct { #define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) /* Parse the input text to generate a number, and populate the result into item. */ -static Jbool parse_number(J * const item, parse_buffer * const input_buffer) +NOTE_C_STATIC Jbool _parse_number(J * const item, parse_buffer * const input_buffer) { JNUMBER number = 0; unsigned char *after_end = NULL; unsigned char number_c_string[64]; - unsigned char decimal_point = get_decimal_point(); + unsigned char decimal_point = _get_decimal_point(); size_t i = 0; if ((input_buffer == NULL) || (input_buffer->content == NULL)) { @@ -354,7 +354,7 @@ typedef struct { } printbuffer; /* realloc printbuffer if necessary to have at least "needed" bytes more */ -static unsigned char* ensure(printbuffer * const p, size_t needed) +NOTE_C_STATIC unsigned char* _ensure(printbuffer * const p, size_t needed) { unsigned char *newbuffer = NULL; size_t newsize = 0; @@ -414,7 +414,7 @@ static unsigned char* ensure(printbuffer * const p, size_t needed) } /* calculate the new length of the string in a printbuffer and update the offset */ -static void update_offset(printbuffer * const buffer) +NOTE_C_STATIC void _update_offset(printbuffer * const buffer) { const unsigned char *buffer_pointer = NULL; if ((buffer == NULL) || (buffer->buffer == NULL)) { @@ -426,7 +426,7 @@ static void update_offset(printbuffer * const buffer) } /* Render the number nicely from the given item into a string. */ -static Jbool print_number(const J * const item, printbuffer * const output_buffer) +NOTE_C_STATIC Jbool _print_number(const J * const item, printbuffer * const output_buffer) { if (item == NULL) { return false; @@ -438,7 +438,7 @@ static Jbool print_number(const J * const item, printbuffer * const output_buffe int length = 0; size_t i = 0; unsigned char number_buffer[JNTOA_MAX]; /* temporary buffer to print the number into */ - unsigned char decimal_point = get_decimal_point(); + unsigned char decimal_point = _get_decimal_point(); if (output_buffer == NULL) { return false; @@ -467,7 +467,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 + sizeof("")); if (output_pointer == NULL) { return false; } @@ -490,7 +490,7 @@ static Jbool print_number(const J * const item, printbuffer * const output_buffe } /* parse 4 digit hexadecimal number */ -static unsigned long parse_hex4(const unsigned char * const input) +NOTE_C_STATIC unsigned long _parse_hex4(const unsigned char * const input) { unsigned long int h = 0; size_t i = 0; @@ -518,7 +518,7 @@ static unsigned long parse_hex4(const unsigned char * const input) /* converts a UTF-16 literal to UTF-8 * A literal can be one or two sequences of the form \uXXXX */ -static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer) +NOTE_C_STATIC unsigned char _utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer) { long unsigned int codepoint = 0; unsigned long int first_code = 0; @@ -534,7 +534,7 @@ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_poi } /* get the first utf16 sequence */ - first_code = parse_hex4(first_sequence + 2); + first_code = _parse_hex4(first_sequence + 2); /* check that the code is valid */ if (((first_code >= 0xDC00) && (first_code <= 0xDFFF))) { @@ -558,7 +558,7 @@ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_poi } /* get the second utf16 sequence */ - second_code = parse_hex4(second_sequence + 2); + second_code = _parse_hex4(second_sequence + 2); /* check that the code is valid */ if ((second_code < 0xDC00) || (second_code > 0xDFFF)) { /* invalid second half of the surrogate pair */ @@ -618,7 +618,7 @@ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_poi } /* Parse the input text into an unescaped cinput, and populate item. */ -static Jbool parse_string(J * const item, parse_buffer * const input_buffer) +NOTE_C_STATIC Jbool _parse_string(J * const item, parse_buffer * const input_buffer) { const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; @@ -695,7 +695,7 @@ static Jbool parse_string(J * const item, parse_buffer * const input_buffer) /* UTF-16 literal */ case 'u': - sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); + sequence_length = _utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); if (sequence_length == 0) { /* failed to convert UTF16-literal to UTF-8 */ goto fail; @@ -733,7 +733,7 @@ static Jbool parse_string(J * const item, parse_buffer * const input_buffer) } /* Convert a 16-bit number to 4 hex digits, null-terminating it */ -void n_htoa16(uint16_t n, unsigned char *p) +void _n_htoa16(uint16_t n, unsigned char *p) { int i; for (i=0; i<4; i++) { @@ -749,7 +749,7 @@ void n_htoa16(uint16_t n, unsigned char *p) } /* Render the cstring provided to an escaped version that can be printed. */ -static Jbool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) +NOTE_C_STATIC Jbool _print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) { const unsigned char *input_pointer = NULL; unsigned char *output = NULL; @@ -764,7 +764,7 @@ static Jbool print_string_ptr(const unsigned char * const input, printbuffer * c /* empty string */ if (input == NULL) { - output = ensure(output_buffer, 2); // sizeof("\"\"") + output = _ensure(output_buffer, 2); // sizeof("\"\"") if (output == NULL) { return false; } @@ -798,7 +798,7 @@ static Jbool print_string_ptr(const unsigned char * const input, printbuffer * c } output_length = (size_t)(input_pointer - input) + escape_characters; - output = ensure(output_buffer, output_length + 2); // sizeof("\"\"") + output = _ensure(output_buffer, output_length + 2); // sizeof("\"\"") if (output == NULL) { return false; } @@ -848,7 +848,7 @@ static Jbool print_string_ptr(const unsigned char * const input, printbuffer * c default: /* escape and print as unicode codepoint */ *output_pointer++ = 'u'; - n_htoa16(*input_pointer, output_pointer); + _n_htoa16(*input_pointer, output_pointer); output_pointer += 4; break; } @@ -860,22 +860,22 @@ static Jbool print_string_ptr(const unsigned char * const input, printbuffer * c return true; } -/* Invoke print_string_ptr (which is useful) on an item. */ -static Jbool print_string(const J * const item, printbuffer * const p) +/* Invoke _print_string_ptr (which is useful) on an item. */ +NOTE_C_STATIC Jbool _print_string(const J * const item, printbuffer * const p) { - return print_string_ptr((unsigned char*)item->valuestring, p); + return _print_string_ptr((unsigned char*)item->valuestring, p); } /* Predeclare these prototypes. */ -static Jbool parse_value(J * const item, parse_buffer * const input_buffer); -static Jbool print_value(const J * const item, printbuffer * const output_buffer); -static Jbool parse_array(J * const item, parse_buffer * const input_buffer); -static Jbool print_array(const J * const item, printbuffer * const output_buffer); -static Jbool parse_object(J * const item, parse_buffer * const input_buffer); -static Jbool print_object(const J * const item, printbuffer * const output_buffer); +NOTE_C_STATIC Jbool _parse_value(J * const item, parse_buffer * const input_buffer); +NOTE_C_STATIC Jbool _print_value(const J * const item, printbuffer * const output_buffer); +NOTE_C_STATIC Jbool _parse_array(J * const item, parse_buffer * const input_buffer); +NOTE_C_STATIC Jbool _print_array(const J * const item, printbuffer * const output_buffer); +NOTE_C_STATIC Jbool _parse_object(J * const item, parse_buffer * const input_buffer); +NOTE_C_STATIC Jbool _print_object(const J * const item, printbuffer * const output_buffer); /* Utility to jump whitespace and cr/lf */ -static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) +NOTE_C_STATIC parse_buffer *_buffer_skip_whitespace(parse_buffer * const buffer) { if ((buffer == NULL) || (buffer->content == NULL)) { return NULL; @@ -893,7 +893,7 @@ static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) } /* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */ -static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) +NOTE_C_STATIC parse_buffer *_skip_utf8_bom(parse_buffer * const buffer) { if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0)) { return NULL; @@ -924,19 +924,19 @@ N_CJSON_PUBLIC(J *) JParseWithOpts(const char *value, const char **return_parse_ buffer.length = strlen((const char*)value) + 1; // Trailing '\0' buffer.offset = 0; - item = JNew_Item(); + item = _jNew_Item(); if (item == NULL) { /* memory fail */ goto fail; } - if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) { + if (!_parse_value(item, _buffer_skip_whitespace(_skip_utf8_bom(&buffer)))) { /* parse failure. ep is set. */ goto fail; } /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ if (require_null_terminated) { - buffer_skip_whitespace(&buffer); + _buffer_skip_whitespace(&buffer); if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') { goto fail; } @@ -988,7 +988,7 @@ N_CJSON_PUBLIC(J *) JParse(const char *value) #define cjson_min(a, b) ((a < b) ? a : b) -static unsigned char *print(const J * const item, Jbool format, Jbool omitempty) +NOTE_C_STATIC unsigned char *_print(const J * const item, Jbool format, Jbool omitempty) { static const size_t default_buffer_size = 128; printbuffer buffer[1]; @@ -1006,10 +1006,10 @@ static unsigned char *print(const J * const item, Jbool format, Jbool omitempty) } /* print the value */ - if (!print_value(item, buffer)) { + if (!_print_value(item, buffer)) { goto fail; } - update_offset(buffer); + _update_offset(buffer); /* copy the JSON over to a new buffer */ printed = (unsigned char*) _Malloc(buffer->offset + 1); @@ -1042,7 +1042,7 @@ N_CJSON_PUBLIC(char *) JPrint(const J *item) if (item == NULL) { return NULL; } - return (char*)print(item, true, false); + return (char*)_print(item, true, false); } /*! @@ -1061,7 +1061,7 @@ N_CJSON_PUBLIC(char *) JPrintUnformatted(const J *item) if (item == NULL) { return NULL; } - return (char*)print(item, false, false); + return (char*)_print(item, false, false); } N_CJSON_PUBLIC(char *) JPrintUnformattedOmitEmpty(const J *item) @@ -1069,7 +1069,7 @@ N_CJSON_PUBLIC(char *) JPrintUnformattedOmitEmpty(const J *item) if (item == NULL) { return NULL; } - return (char*)print(item, false, true); + return (char*)_print(item, false, true); } N_CJSON_PUBLIC(char *) JPrintBuffered(const J *item, int prebuffer, Jbool fmt) @@ -1094,7 +1094,7 @@ N_CJSON_PUBLIC(char *) JPrintBuffered(const J *item, int prebuffer, Jbool fmt) p.noalloc = false; p.format = fmt; - if (!print_value(item, &p)) { + if (!_print_value(item, &p)) { _Free(p.buffer); return NULL; } @@ -1102,7 +1102,7 @@ N_CJSON_PUBLIC(char *) JPrintBuffered(const J *item, int prebuffer, Jbool fmt) return (char*)p.buffer; } -static Jbool printPreallocated(J *item, char *buf, const int len, const Jbool fmt, const Jbool omit) +NOTE_C_STATIC Jbool _printPreallocated(J *item, char *buf, const int len, const Jbool fmt, const Jbool omit) { printbuffer p = { 0, 0, 0, 0, 0, 0, 0 }; @@ -1120,21 +1120,21 @@ static Jbool printPreallocated(J *item, char *buf, const int len, const Jbool fm p.format = fmt; p.omitempty = omit; - return print_value(item, &p); + return _print_value(item, &p); } N_CJSON_PUBLIC(Jbool) JPrintPreallocatedOmitEmpty(J *item, char *buf, const int len, const Jbool fmt) { - return printPreallocated(item, buf, len, fmt, true); + return _printPreallocated(item, buf, len, fmt, true); } N_CJSON_PUBLIC(Jbool) JPrintPreallocated(J *item, char *buf, const int len, const Jbool fmt) { - return printPreallocated(item, buf, len, fmt, false); + return _printPreallocated(item, buf, len, fmt, false); } /* Parser core - when encountering text, process appropriately. */ -static Jbool parse_value(J * const item, parse_buffer * const input_buffer) +NOTE_C_STATIC Jbool _parse_value(J * const item, parse_buffer * const input_buffer) { if (item == NULL) { return false; @@ -1165,26 +1165,26 @@ static Jbool parse_value(J * const item, parse_buffer * const input_buffer) } /* string */ if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"')) { - return parse_string(item, input_buffer); + return _parse_string(item, input_buffer); } /* number */ if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9')))) { - return parse_number(item, input_buffer); + return _parse_number(item, input_buffer); } /* array */ if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '[')) { - return parse_array(item, input_buffer); + return _parse_array(item, input_buffer); } /* object */ if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{')) { - return parse_object(item, input_buffer); + return _parse_object(item, input_buffer); } return false; } /* Render a value to text. */ -static Jbool print_value(const J * const item, printbuffer * const output_buffer) +NOTE_C_STATIC Jbool _print_value(const J * const item, printbuffer * const output_buffer) { unsigned char *output = NULL; @@ -1194,7 +1194,7 @@ static Jbool print_value(const J * const item, printbuffer * const output_buffer switch ((item->type) & 0xFF) { case JNULL: - output = ensure(output_buffer, c_null_len+1); + output = _ensure(output_buffer, c_null_len+1); if (output == NULL) { return false; } @@ -1202,7 +1202,7 @@ static Jbool print_value(const J * const item, printbuffer * const output_buffer return true; case JFalse: - output = ensure(output_buffer, c_false_len+1); + output = _ensure(output_buffer, c_false_len+1); if (output == NULL) { return false; } @@ -1210,7 +1210,7 @@ static Jbool print_value(const J * const item, printbuffer * const output_buffer return true; case JTrue: - output = ensure(output_buffer, c_true_len+1); + output = _ensure(output_buffer, c_true_len+1); if (output == NULL) { return false; } @@ -1218,7 +1218,7 @@ static Jbool print_value(const J * const item, printbuffer * const output_buffer return true; case JNumber: - return print_number(item, output_buffer); + return _print_number(item, output_buffer); case JRaw: { size_t raw_length = 0; @@ -1227,7 +1227,7 @@ static Jbool print_value(const J * const item, printbuffer * const output_buffer } raw_length = strlen(item->valuestring) + 1; // Trailing '\0'; - output = ensure(output_buffer, raw_length); + output = _ensure(output_buffer, raw_length); if (output == NULL) { return false; } @@ -1236,13 +1236,13 @@ static Jbool print_value(const J * const item, printbuffer * const output_buffer } case JString: - return print_string(item, output_buffer); + return _print_string(item, output_buffer); case JArray: - return print_array(item, output_buffer); + return _print_array(item, output_buffer); case JObject: - return print_object(item, output_buffer); + return _print_object(item, output_buffer); default: return false; @@ -1250,7 +1250,7 @@ static Jbool print_value(const J * const item, printbuffer * const output_buffer } /* Build an array from input text. */ -static Jbool parse_array(J * const item, parse_buffer * const input_buffer) +NOTE_C_STATIC Jbool _parse_array(J * const item, parse_buffer * const input_buffer) { J *head = NULL; /* head of the linked list */ J *current_item = NULL; @@ -1266,7 +1266,7 @@ static Jbool parse_array(J * const item, parse_buffer * const input_buffer) } input_buffer->offset++; - buffer_skip_whitespace(input_buffer); + _buffer_skip_whitespace(input_buffer); if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']')) { /* empty array */ goto success; @@ -1283,7 +1283,7 @@ static Jbool parse_array(J * const item, parse_buffer * const input_buffer) /* loop through the comma separated array elements */ do { /* allocate next item */ - J *new_item = JNew_Item(); + J *new_item = _jNew_Item(); if (new_item == NULL) { goto fail; /* allocation failure */ } @@ -1301,11 +1301,11 @@ static Jbool parse_array(J * const item, parse_buffer * const input_buffer) /* parse next value */ input_buffer->offset++; - buffer_skip_whitespace(input_buffer); - if (!parse_value(current_item, input_buffer)) { + _buffer_skip_whitespace(input_buffer); + if (!_parse_value(current_item, input_buffer)) { goto fail; /* failed to parse value */ } - buffer_skip_whitespace(input_buffer); + _buffer_skip_whitespace(input_buffer); } while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']') { @@ -1331,7 +1331,7 @@ static Jbool parse_array(J * const item, parse_buffer * const input_buffer) } /* Render an array to text */ -static Jbool print_array(const J * const item, printbuffer * const output_buffer) +NOTE_C_STATIC Jbool _print_array(const J * const item, printbuffer * const output_buffer) { unsigned char *output_pointer = NULL; size_t length = 0; @@ -1343,7 +1343,7 @@ static Jbool print_array(const J * const item, printbuffer * const output_buffer /* Compose the output array. */ /* opening square bracket */ - output_pointer = ensure(output_buffer, 1); + output_pointer = _ensure(output_buffer, 1); if (output_pointer == NULL) { return false; } @@ -1353,13 +1353,13 @@ static Jbool print_array(const J * const item, printbuffer * const output_buffer output_buffer->depth++; while (current_element != NULL) { - if (!print_value(current_element, output_buffer)) { + if (!_print_value(current_element, output_buffer)) { return false; } - update_offset(output_buffer); + _update_offset(output_buffer); if (current_element->next) { length = (size_t) (output_buffer->format ? 2 : 1); - output_pointer = ensure(output_buffer, length + 1); + output_pointer = _ensure(output_buffer, length + 1); if (output_pointer == NULL) { return false; } @@ -1373,7 +1373,7 @@ static Jbool print_array(const J * const item, printbuffer * const output_buffer current_element = current_element->next; } - output_pointer = ensure(output_buffer, 2); + output_pointer = _ensure(output_buffer, 2); if (output_pointer == NULL) { return false; } @@ -1385,7 +1385,7 @@ static Jbool print_array(const J * const item, printbuffer * const output_buffer } /* Build an object from the text. */ -static Jbool parse_object(J * const item, parse_buffer * const input_buffer) +NOTE_C_STATIC Jbool _parse_object(J * const item, parse_buffer * const input_buffer) { J *head = NULL; /* linked list head */ J *current_item = NULL; @@ -1400,7 +1400,7 @@ static Jbool parse_object(J * const item, parse_buffer * const input_buffer) } input_buffer->offset++; - buffer_skip_whitespace(input_buffer); + _buffer_skip_whitespace(input_buffer); if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}')) { goto success; /* empty object */ } @@ -1416,7 +1416,7 @@ static Jbool parse_object(J * const item, parse_buffer * const input_buffer) /* loop through the comma separated array elements */ do { /* allocate next item */ - J *new_item = JNew_Item(); + J *new_item = _jNew_Item(); if (new_item == NULL) { goto fail; /* allocation failure */ } @@ -1434,11 +1434,11 @@ static Jbool parse_object(J * const item, parse_buffer * const input_buffer) /* parse the name of the child */ input_buffer->offset++; - buffer_skip_whitespace(input_buffer); - if (!parse_string(current_item, input_buffer)) { + _buffer_skip_whitespace(input_buffer); + if (!_parse_string(current_item, input_buffer)) { goto fail; /* faile to parse name */ } - buffer_skip_whitespace(input_buffer); + _buffer_skip_whitespace(input_buffer); /* swap valuestring and string, because we parsed the name */ current_item->string = current_item->valuestring; @@ -1450,11 +1450,11 @@ static Jbool parse_object(J * const item, parse_buffer * const input_buffer) /* parse the value */ input_buffer->offset++; - buffer_skip_whitespace(input_buffer); - if (!parse_value(current_item, input_buffer)) { + _buffer_skip_whitespace(input_buffer); + if (!_parse_value(current_item, input_buffer)) { goto fail; /* failed to parse value */ } - buffer_skip_whitespace(input_buffer); + _buffer_skip_whitespace(input_buffer); } while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}')) { @@ -1479,7 +1479,7 @@ static Jbool parse_object(J * const item, parse_buffer * const input_buffer) } /* See if there is another non-omitted item looking forward */ -static bool last_non_omitted_object(J * item, printbuffer * const output_buffer) +NOTE_C_STATIC bool _last_non_omitted_object(J * item, printbuffer * const output_buffer) { if (!output_buffer->omitempty) { return (item->next == 0); @@ -1495,7 +1495,7 @@ static bool last_non_omitted_object(J * item, printbuffer * const output_buffer) } /* Render an object to text. */ -static Jbool print_object(const J * const item, printbuffer * const output_buffer) +NOTE_C_STATIC Jbool _print_object(const J * const item, printbuffer * const output_buffer) { unsigned char *output_pointer = NULL; size_t length = 0; @@ -1507,7 +1507,7 @@ static Jbool print_object(const J * const item, printbuffer * const output_buffe /* Compose the output: */ length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */ - output_pointer = ensure(output_buffer, length + 1); + output_pointer = _ensure(output_buffer, length + 1); if (output_pointer == NULL) { return false; } @@ -1527,7 +1527,7 @@ static Jbool print_object(const J * const item, printbuffer * const output_buffe #else int needed = output_buffer->depth * PRINT_TAB_CHARS; #endif - output_pointer = ensure(output_buffer, needed); + output_pointer = _ensure(output_buffer, needed); if (output_pointer == NULL) { return false; } @@ -1555,13 +1555,13 @@ static Jbool print_object(const J * const item, printbuffer * const output_buffe if (!omit) { /* print key */ - if (!print_string_ptr((unsigned char*)current_item->string, output_buffer)) { + if (!_print_string_ptr((unsigned char*)current_item->string, output_buffer)) { return false; } - update_offset(output_buffer); + _update_offset(output_buffer); length = (size_t) (output_buffer->format ? 2 : 1); - output_pointer = ensure(output_buffer, length); + output_pointer = _ensure(output_buffer, length); if (output_pointer == NULL) { return false; } @@ -1576,15 +1576,15 @@ static Jbool print_object(const J * const item, printbuffer * const output_buffe output_buffer->offset += length; /* print value */ - if (!print_value(current_item, output_buffer)) { + if (!_print_value(current_item, output_buffer)) { return false; } - update_offset(output_buffer); + _update_offset(output_buffer); /* print comma if not last */ - bool more_fields_coming = !last_non_omitted_object(current_item, output_buffer); + bool more_fields_coming = !_last_non_omitted_object(current_item, output_buffer); length = (size_t) ((output_buffer->format ? 1 : 0) + (more_fields_coming ? 1 : 0)); - output_pointer = ensure(output_buffer, length + 1); + output_pointer = _ensure(output_buffer, length + 1); if (output_pointer == NULL) { return false; } @@ -1608,7 +1608,7 @@ static Jbool print_object(const J * const item, printbuffer * const output_buffe int needed = output_buffer->format ? ((output_buffer->depth - 1) * PRINT_TAB_CHARS) : 0; #endif needed += 2; // }\0 - output_pointer = ensure(output_buffer, needed); + output_pointer = _ensure(output_buffer, needed); if (output_pointer == NULL) { return false; } @@ -1653,7 +1653,7 @@ N_CJSON_PUBLIC(int) JGetArraySize(const J *array) return (int)size; } -static J* get_array_item(const J *array, size_t index) +NOTE_C_STATIC J* _get_array_item(const J *array, size_t index) { J *current_child = NULL; @@ -1679,10 +1679,10 @@ N_CJSON_PUBLIC(J *) JGetArrayItem(const J *array, int index) return NULL; } - return get_array_item(array, (size_t)index); + return _get_array_item(array, (size_t)index); } -static J *get_object_item(const J * const object, const char * const name, const Jbool case_sensitive) +NOTE_C_STATIC J *_get_object_item(const J * const object, const char * const name, const Jbool case_sensitive) { J *current_element = NULL; @@ -1696,7 +1696,7 @@ static J *get_object_item(const J * const object, const char * const name, const current_element = current_element->next; } } else { - while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) { + while ((current_element != NULL) && (_case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) { current_element = current_element->next; } } @@ -1709,7 +1709,7 @@ N_CJSON_PUBLIC(J *) JGetObjectItem(const J * const object, const char * const st if (object == NULL) { return NULL; } - return get_object_item(object, string, false); + return _get_object_item(object, string, false); } N_CJSON_PUBLIC(J *) JGetObjectItemCaseSensitive(const J * const object, const char * const string) @@ -1717,7 +1717,7 @@ N_CJSON_PUBLIC(J *) JGetObjectItemCaseSensitive(const J * const object, const ch if (object == NULL) { return NULL; } - return get_object_item(object, string, true); + return _get_object_item(object, string, true); } N_CJSON_PUBLIC(Jbool) JHasObjectItem(const J *object, const char *string) @@ -1729,21 +1729,21 @@ N_CJSON_PUBLIC(Jbool) JHasObjectItem(const J *object, const char *string) } /* Utility for array list handling. */ -static void suffix_object(J *prev, J *item) +NOTE_C_STATIC void _suffix_object(J *prev, J *item) { prev->next = item; item->prev = prev; } /* Utility for handling references. */ -static J *create_reference(const J *item) +NOTE_C_STATIC J *_create_reference(const J *item) { J *reference = NULL; if (item == NULL) { return NULL; } - reference = JNew_Item(); + reference = _jNew_Item(); if (reference == NULL) { return NULL; } @@ -1755,7 +1755,7 @@ static J *create_reference(const J *item) return reference; } -static Jbool add_item_to_array(J *array, J *item) +NOTE_C_STATIC Jbool _add_item_to_array(J *array, J *item) { J *child = NULL; @@ -1773,7 +1773,7 @@ static Jbool add_item_to_array(J *array, J *item) while (child->next) { child = child->next; } - suffix_object(child, item); + _suffix_object(child, item); } return true; @@ -1785,7 +1785,7 @@ N_CJSON_PUBLIC(void) JAddItemToArray(J *array, J *item) if (array == NULL || item == NULL) { return; } - add_item_to_array(array, item); + _add_item_to_array(array, item); } #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) @@ -1795,7 +1795,7 @@ N_CJSON_PUBLIC(void) JAddItemToArray(J *array, J *item) #pragma GCC diagnostic ignored "-Wcast-qual" #endif /* helper function to cast away const */ -static void* cast_away_const(const void* string) +NOTE_C_STATIC void* _cast_away_const(const void* string) { return (void*)string; } @@ -1804,7 +1804,7 @@ static void* cast_away_const(const void* string) #endif -static Jbool add_item_to_object(J * const object, const char * const string, J * const item, const Jbool constant_key) +NOTE_C_STATIC Jbool _add_item_to_object(J * const object, const char * const string, J * const item, const Jbool constant_key) { char *new_key = NULL; int new_type = JInvalid; @@ -1814,10 +1814,10 @@ static Jbool add_item_to_object(J * const object, const char * const string, J * } if (constant_key) { - new_key = (char*)cast_away_const(string); + new_key = (char*)_cast_away_const(string); new_type = item->type | JStringIsConst; } else { - new_key = (char*)Jstrdup((const unsigned char*)string); + new_key = (char*)_j_strdup((const unsigned char*)string); if (new_key == NULL) { return false; } @@ -1832,7 +1832,7 @@ static Jbool add_item_to_object(J * const object, const char * const string, J * item->string = new_key; item->type = new_type; - return add_item_to_array(object, item); + return _add_item_to_array(object, item); } N_CJSON_PUBLIC(void) JAddItemToObject(J *object, const char *string, J *item) @@ -1840,7 +1840,7 @@ N_CJSON_PUBLIC(void) JAddItemToObject(J *object, const char *string, J *item) if (object == NULL || string == NULL || item == NULL) { return; } - add_item_to_object(object, string, item, false); + _add_item_to_object(object, string, item, false); } /* Add an item to an object with constant string as key */ @@ -1849,7 +1849,7 @@ N_CJSON_PUBLIC(void) JAddItemToObjectCS(J *object, const char *string, J *item) if (object == NULL || string == NULL || item == NULL) { return; } - add_item_to_object(object, string, item, true); + _add_item_to_object(object, string, item, true); } N_CJSON_PUBLIC(void) JAddItemReferenceToArray(J *array, J *item) @@ -1857,7 +1857,7 @@ N_CJSON_PUBLIC(void) JAddItemReferenceToArray(J *array, J *item) if (array == NULL || item == NULL) { return; } - add_item_to_array(array, create_reference(item)); + _add_item_to_array(array, _create_reference(item)); } N_CJSON_PUBLIC(void) JAddItemReferenceToObject(J *object, const char *string, J *item) @@ -1865,7 +1865,7 @@ N_CJSON_PUBLIC(void) JAddItemReferenceToObject(J *object, const char *string, J if (object == NULL || string == NULL || item == NULL) { return; } - add_item_to_object(object, string, create_reference(item), false); + _add_item_to_object(object, string, _create_reference(item), false); } N_CJSON_PUBLIC(J*) JAddTrueToObject(J * const object, const char * const name) @@ -1875,7 +1875,7 @@ N_CJSON_PUBLIC(J*) JAddTrueToObject(J * const object, const char * const name) } J *true_item = JCreateTrue(); - if (add_item_to_object(object, name, true_item, false)) { + if (_add_item_to_object(object, name, true_item, false)) { return true_item; } @@ -1890,7 +1890,7 @@ N_CJSON_PUBLIC(J*) JAddFalseToObject(J * const object, const char * const name) } J *false_item = JCreateFalse(); - if (add_item_to_object(object, name, false_item, false)) { + if (_add_item_to_object(object, name, false_item, false)) { return false_item; } @@ -1914,7 +1914,7 @@ N_CJSON_PUBLIC(J*) JAddBoolToObject(J * const object, const char * const name, c } J *bool_item = JCreateBool(boolean); - if (add_item_to_object(object, name, bool_item, false)) { + if (_add_item_to_object(object, name, bool_item, false)) { return bool_item; } @@ -1938,7 +1938,7 @@ N_CJSON_PUBLIC(J*) JAddNumberToObject(J * const object, const char * const name, } J *number_item = JCreateNumber(number); - if (add_item_to_object(object, name, number_item, false)) { + if (_add_item_to_object(object, name, number_item, false)) { return number_item; } @@ -1953,7 +1953,7 @@ N_CJSON_PUBLIC(J*) JAddIntToObject(J * const object, const char * const name, co } J *integer_item = JCreateInteger(integer); - if (add_item_to_object(object, name, integer_item, false)) { + if (_add_item_to_object(object, name, integer_item, false)) { return integer_item; } @@ -1977,7 +1977,7 @@ N_CJSON_PUBLIC(J*) JAddStringToObject(J * const object, const char * const name, } J *string_item = JCreateString(string); - if (add_item_to_object(object, name, string_item, false)) { + if (_add_item_to_object(object, name, string_item, false)) { return string_item; } @@ -1992,7 +1992,7 @@ N_CJSON_PUBLIC(J*) JAddRawToObject(J * const object, const char * const name, co } J *raw_item = JCreateRaw(raw); - if (add_item_to_object(object, name, raw_item, false)) { + if (_add_item_to_object(object, name, raw_item, false)) { return raw_item; } @@ -2015,7 +2015,7 @@ N_CJSON_PUBLIC(J*) JAddObjectToObject(J * const object, const char * const name) } J *object_item = JCreateObject(); - if (add_item_to_object(object, name, object_item, false)) { + if (_add_item_to_object(object, name, object_item, false)) { return object_item; } @@ -2038,7 +2038,7 @@ N_CJSON_PUBLIC(J*) JAddArrayToObject(J * const object, const char * const name) } J *array = JCreateArray(); - if (add_item_to_object(object, name, array, false)) { + if (_add_item_to_object(object, name, array, false)) { return array; } @@ -2081,7 +2081,7 @@ N_CJSON_PUBLIC(J *) JDetachItemFromArray(J *array, int which) return NULL; } - return JDetachItemViaPointer(array, get_array_item(array, (size_t)which)); + return JDetachItemViaPointer(array, _get_array_item(array, (size_t)which)); } N_CJSON_PUBLIC(void) JDeleteItemFromArray(J *array, int which) @@ -2143,9 +2143,9 @@ N_CJSON_PUBLIC(void) JInsertItemInArray(J *array, int which, J *newitem) return; } - after_inserted = get_array_item(array, (size_t)which); + after_inserted = _get_array_item(array, (size_t)which); if (after_inserted == NULL) { - add_item_to_array(array, newitem); + _add_item_to_array(array, newitem); return; } @@ -2199,10 +2199,10 @@ N_CJSON_PUBLIC(void) JReplaceItemInArray(J *array, int which, J *newitem) return; } - JReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); + JReplaceItemViaPointer(array, _get_array_item(array, (size_t)which), newitem); } -static Jbool replace_item_in_object(J *object, const char *string, J *replacement, Jbool case_sensitive) +NOTE_C_STATIC Jbool _replace_item_in_object(J *object, const char *string, J *replacement, Jbool case_sensitive) { if (object == NULL || replacement == NULL || string == NULL) { return false; @@ -2212,10 +2212,10 @@ static Jbool replace_item_in_object(J *object, const char *string, J *replacemen if (!(replacement->type & JStringIsConst) && (replacement->string != NULL)) { _Free(replacement->string); } - replacement->string = (char*)Jstrdup((const unsigned char*)string); + replacement->string = (char*)_j_strdup((const unsigned char*)string); replacement->type &= ~JStringIsConst; - JReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); + JReplaceItemViaPointer(object, _get_object_item(object, string, case_sensitive), replacement); return true; } @@ -2225,7 +2225,7 @@ N_CJSON_PUBLIC(void) JReplaceItemInObject(J *object, const char *string, J *newi if (object == NULL || newitem == NULL) { return; } - replace_item_in_object(object, string, newitem, false); + _replace_item_in_object(object, string, newitem, false); } N_CJSON_PUBLIC(void) JReplaceItemInObjectCaseSensitive(J *object, const char *string, J *newitem) @@ -2233,12 +2233,12 @@ N_CJSON_PUBLIC(void) JReplaceItemInObjectCaseSensitive(J *object, const char *st if (object == NULL || newitem == NULL) { return; } - replace_item_in_object(object, string, newitem, true); + _replace_item_in_object(object, string, newitem, true); } N_CJSON_PUBLIC(J *) JCreateTrue(void) { - J *item = JNew_Item(); + J *item = _jNew_Item(); if(item) { item->type = JTrue; } @@ -2247,7 +2247,7 @@ N_CJSON_PUBLIC(J *) JCreateTrue(void) N_CJSON_PUBLIC(J *) JCreateFalse(void) { - J *item = JNew_Item(); + J *item = _jNew_Item(); if(item) { item->type = JFalse; } @@ -2256,7 +2256,7 @@ N_CJSON_PUBLIC(J *) JCreateFalse(void) N_CJSON_PUBLIC(J *) JCreateBool(Jbool b) { - J *item = JNew_Item(); + J *item = _jNew_Item(); if(item) { item->type = b ? JTrue : JFalse; } @@ -2265,7 +2265,7 @@ N_CJSON_PUBLIC(J *) JCreateBool(Jbool b) N_CJSON_PUBLIC(J *) JCreateNumber(JNUMBER num) { - J *item = JNew_Item(); + J *item = _jNew_Item(); if(item) { item->type = JNumber; item->valuenumber = num; @@ -2284,7 +2284,7 @@ N_CJSON_PUBLIC(J *) JCreateNumber(JNUMBER num) N_CJSON_PUBLIC(J *) JCreateInteger(JINTEGER integer) { - J *item = JNew_Item(); + J *item = _jNew_Item(); if(item) { item->type = JNumber; item->valuenumber = (JNUMBER)integer; @@ -2295,10 +2295,10 @@ N_CJSON_PUBLIC(J *) JCreateInteger(JINTEGER integer) N_CJSON_PUBLIC(J *) JCreateString(const char *string) { - J *item = JNew_Item(); + J *item = _jNew_Item(); if(item) { item->type = JString; - item->valuestring = (char*)Jstrdup((const unsigned char*)string); + item->valuestring = (char*)_j_strdup((const unsigned char*)string); if(!item->valuestring) { JDelete(item); return NULL; @@ -2309,20 +2309,20 @@ N_CJSON_PUBLIC(J *) JCreateString(const char *string) N_CJSON_PUBLIC(J *) JCreateStringValue(const char *string) { - J *item = JNew_Item(); + J *item = _jNew_Item(); if (item != NULL) { item->type = JString; - item->valuestring = (char*)cast_away_const(string); + item->valuestring = (char*)_cast_away_const(string); } return item; } N_CJSON_PUBLIC(J *) JCreateStringReference(const char *string) { - J *item = JNew_Item(); + J *item = _jNew_Item(); if (item != NULL) { item->type = JString | JIsReference; - item->valuestring = (char*)cast_away_const(string); + item->valuestring = (char*)_cast_away_const(string); } return item; } @@ -2332,10 +2332,10 @@ N_CJSON_PUBLIC(J *) JCreateObjectReference(const J *child) if (child == NULL) { return NULL; } - J *item = JNew_Item(); + J *item = _jNew_Item(); if (item != NULL) { item->type = JObject | JIsReference; - item->child = (J*)cast_away_const(child); + item->child = (J*)_cast_away_const(child); } return item; } @@ -2345,20 +2345,20 @@ N_CJSON_PUBLIC(J *) JCreateArrayReference(const J *child) if (child == NULL) { return NULL; } - J *item = JNew_Item(); + J *item = _jNew_Item(); if (item != NULL) { item->type = JArray | JIsReference; - item->child = (J*)cast_away_const(child); + item->child = (J*)_cast_away_const(child); } return item; } N_CJSON_PUBLIC(J *) JCreateRaw(const char *raw) { - J *item = JNew_Item(); + J *item = _jNew_Item(); if(item) { item->type = JRaw; - item->valuestring = (char*)Jstrdup((const unsigned char*)raw); + item->valuestring = (char*)_j_strdup((const unsigned char*)raw); if(!item->valuestring) { JDelete(item); return NULL; @@ -2369,7 +2369,7 @@ N_CJSON_PUBLIC(J *) JCreateRaw(const char *raw) N_CJSON_PUBLIC(J *) JCreateArray(void) { - J *item = JNew_Item(); + J *item = _jNew_Item(); if(item) { item->type=JArray; } @@ -2385,7 +2385,7 @@ N_CJSON_PUBLIC(J *) JCreateArray(void) */ N_CJSON_PUBLIC(J *) JCreateObject(void) { - J *item = JNew_Item(); + J *item = _jNew_Item(); if (item) { item->type = JObject; } @@ -2414,7 +2414,7 @@ N_CJSON_PUBLIC(J *) JCreateIntArray(const long int *numbers, int count) if(!i) { a->child = n; } else { - suffix_object(p, n); + _suffix_object(p, n); } p = n; } @@ -2444,7 +2444,7 @@ N_CJSON_PUBLIC(J *) JCreateNumberArray(const JNUMBER *numbers, int count) if(!i) { a->child = n; } else { - suffix_object(p, n); + _suffix_object(p, n); } p = n; } @@ -2474,7 +2474,7 @@ N_CJSON_PUBLIC(J *) JCreateStringArray(const char **strings, int count) if(!i) { a->child = n; } else { - suffix_object(p,n); + _suffix_object(p,n); } p = n; } @@ -2495,7 +2495,7 @@ N_CJSON_PUBLIC(J *) JDuplicate(const J *item, Jbool recurse) goto fail; } /* Create new item */ - newitem = JNew_Item(); + newitem = _jNew_Item(); if (!newitem) { goto fail; } @@ -2504,13 +2504,13 @@ N_CJSON_PUBLIC(J *) JDuplicate(const J *item, Jbool recurse) newitem->valueint = item->valueint; newitem->valuenumber = item->valuenumber; if (item->valuestring) { - newitem->valuestring = (char*)Jstrdup((unsigned char*)item->valuestring); + newitem->valuestring = (char*)_j_strdup((unsigned char*)item->valuestring); if (!newitem->valuestring) { goto fail; } } if (item->string) { - newitem->string = (item->type&JStringIsConst) ? item->string : (char*)Jstrdup((unsigned char*)item->string); + newitem->string = (item->type&JStringIsConst) ? item->string : (char*)_j_strdup((unsigned char*)item->string); if (!newitem->string) { goto fail; } @@ -2755,7 +2755,7 @@ N_CJSON_PUBLIC(Jbool) JCompare(const J * const a, const J * const b, const Jbool J *b_element = NULL; JArrayForEach(a_element, a) { /* TODO This has O(n^2) runtime, which is horrible! */ - b_element = get_object_item(b, a_element->string, case_sensitive); + b_element = _get_object_item(b, a_element->string, case_sensitive); if (b_element == NULL) { return false; } @@ -2768,7 +2768,7 @@ N_CJSON_PUBLIC(Jbool) JCompare(const J * const a, const J * const b, const Jbool /* doing this twice, once on a and b to prevent true comparison if a subset of b * TODO: Do this the proper way, this is just a fix for now */ JArrayForEach(b_element, b) { - a_element = get_object_item(a, b_element->string, case_sensitive); + a_element = _get_object_item(a, b_element->string, case_sensitive); if (a_element == NULL) { return false; } diff --git a/n_cobs.c b/n_cobs.c index 426a5a1a..caaec14e 100644 --- a/n_cobs.c +++ b/n_cobs.c @@ -24,7 +24,7 @@ @return the length of the decoded data */ /**************************************************************************/ -uint32_t cobsDecode(uint8_t *ptr, uint32_t length, uint8_t eop, uint8_t *dst) +uint32_t _cobsDecode(uint8_t *ptr, uint32_t length, uint8_t eop, uint8_t *dst) { const uint8_t *start = dst, *end = ptr + length; uint8_t code = 0xFF, copy = 0; @@ -60,13 +60,13 @@ uint32_t cobsDecode(uint8_t *ptr, uint32_t length, uint8_t eop, uint8_t *dst) @return the length of the encoded data - @note You may use `cobsEncodedLength()` to calculate the required size for + @note You may use `_cobsEncodedLength()` to calculate the required size for the buffer pointed to by the `dst` parameter. - @see cobsEncodedLength() + @see _cobsEncodedLength() */ /**************************************************************************/ -uint32_t cobsEncode(uint8_t *ptr, uint32_t length, uint8_t eop, uint8_t *dst) +uint32_t _cobsEncode(uint8_t *ptr, uint32_t length, uint8_t eop, uint8_t *dst) { uint8_t ch; uint8_t *start = dst; @@ -100,7 +100,7 @@ uint32_t cobsEncode(uint8_t *ptr, uint32_t length, uint8_t eop, uint8_t *dst) @note The computed length does not include the EOP (end-of-packet) marker */ /**************************************************************************/ -uint32_t cobsEncodedLength(const uint8_t *ptr, uint32_t length) +uint32_t _cobsEncodedLength(const uint8_t *ptr, uint32_t length) { uint8_t ch; uint32_t dst = 1; @@ -135,7 +135,7 @@ uint32_t cobsEncodedLength(const uint8_t *ptr, uint32_t length) @note An additional byte is added for the EOP (end-of-packet) marker. */ /**************************************************************************/ -uint32_t cobsEncodedMaxLength(uint32_t length) +uint32_t _cobsEncodedMaxLength(uint32_t length) { const uint32_t overheadBytes = ((length / 254) + ((length % 254) > 0)); return (length + overheadBytes + COBS_EOP_OVERHEAD); @@ -154,7 +154,7 @@ uint32_t cobsEncodedMaxLength(uint32_t length) @note An additional byte is added for the EOP (end-of-packet) marker. */ /**************************************************************************/ -uint32_t cobsGuaranteedFit(uint32_t bufLen) +uint32_t _cobsGuaranteedFit(uint32_t bufLen) { uint32_t cobsOverhead = 1 + (bufLen / 254) + COBS_EOP_OVERHEAD; return (cobsOverhead > bufLen ? 0 : (bufLen - cobsOverhead)); diff --git a/n_helpers.c b/n_helpers.c index 131989bf..6454a250 100644 --- a/n_helpers.c +++ b/n_helpers.c @@ -77,12 +77,12 @@ static char scService[128] = {0}; #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 char *dayNames[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // Forwards -NOTE_C_STATIC void setTime(JTIME seconds); -NOTE_C_STATIC bool timerExpiredSecs(uint32_t *timer, uint32_t periodSecs); -NOTE_C_STATIC int yToDays(int year); +NOTE_C_STATIC void _setTime(JTIME seconds); +NOTE_C_STATIC bool _timerExpiredSecs(uint32_t *timer, uint32_t periodSecs); +NOTE_C_STATIC int _yToDays(int year); static const char NOTE_C_BINARY_EOP = '\n'; @@ -114,11 +114,11 @@ uint32_t NoteBinaryCodecDecode(const uint8_t *encData, uint32_t encDataLen, if (encData == NULL || decBuf == NULL) { NOTE_C_LOG_ERROR(ERRSTR("NULL parameter", c_err)); result = 0; - } else if (decBufSize < cobsGuaranteedFit(encDataLen)) { + } else if (decBufSize < _cobsGuaranteedFit(encDataLen)) { NOTE_C_LOG_ERROR(ERRSTR("output buffer too small", c_err)); result = 0; } else { - result = cobsDecode((uint8_t *)encData, encDataLen, NOTE_C_BINARY_EOP, decBuf); + result = _cobsDecode((uint8_t *)encData, encDataLen, NOTE_C_BINARY_EOP, decBuf); } return result; @@ -156,15 +156,15 @@ uint32_t NoteBinaryCodecEncode(const uint8_t *decData, uint32_t decDataLen, if (decData == NULL || encBuf == NULL) { NOTE_C_LOG_ERROR(ERRSTR("NULL parameter", c_err)); result = 0; - } else if ((encBufSize < cobsEncodedMaxLength(decDataLen)) - && (encBufSize < cobsEncodedLength(decData, decDataLen))) { - // NOTE: `cobsEncodedMaxLength()` provides a constant time [O(1)] means + } else if ((encBufSize < _cobsEncodedMaxLength(decDataLen)) + && (encBufSize < _cobsEncodedLength(decData, decDataLen))) { + // NOTE: `_cobsEncodedMaxLength()` provides a constant time [O(1)] means // of checking the buffer size. Only when it fails will the linear - // time [O(n)] check, `cobsEncodedLength()`, be invoked. + // time [O(n)] check, `_cobsEncodedLength()`, be invoked. NOTE_C_LOG_ERROR(ERRSTR("output buffer too small", c_err)); result = 0; } else { - result = cobsEncode((uint8_t *)decData, decDataLen, NOTE_C_BINARY_EOP, encBuf); + result = _cobsEncode((uint8_t *)decData, decDataLen, NOTE_C_BINARY_EOP, encBuf); } return result; @@ -198,7 +198,7 @@ uint32_t NoteBinaryCodecEncode(const uint8_t *decData, uint32_t decDataLen, /**************************************************************************/ uint32_t NoteBinaryCodecMaxDecodedLength(uint32_t bufferSize) { - return cobsGuaranteedFit(bufferSize); + return _cobsGuaranteedFit(bufferSize); } //**************************************************************************/ @@ -213,7 +213,7 @@ uint32_t NoteBinaryCodecMaxDecodedLength(uint32_t bufferSize) /**************************************************************************/ uint32_t NoteBinaryCodecMaxEncodedLength(uint32_t unencodedLength) { - return cobsEncodedMaxLength(unencodedLength); + return _cobsEncodedMaxLength(unencodedLength); } //**************************************************************************/ @@ -346,7 +346,7 @@ const char * NoteBinaryStoreReceive(uint8_t *buffer, uint32_t bufLen, NOTE_C_LOG_ERROR(err); return err; } - if (bufLen < cobsEncodedMaxLength(decodedLen)) { + if (bufLen < _cobsEncodedMaxLength(decodedLen)) { const char *err = ERRSTR("insufficient buffer size", c_bad); NOTE_C_LOG_ERROR(err); return err; @@ -368,9 +368,9 @@ const char * NoteBinaryStoreReceive(uint8_t *buffer, uint32_t bufLen, JAddIntToObject(req, "length", decodedLen); // We already have the Notecard lock, so call - // noteTransactionShouldLock with `lockNotecard` set to false so we + // _noteTransactionShouldLock with `lockNotecard` set to false so we // don't try to lock again. - J *rsp = noteTransactionShouldLock(req, false); + J *rsp = _noteTransactionShouldLock(req, false); JDelete(req); // Ensure the transaction doesn't return an error. if (!rsp || NoteResponseError(rsp)) { @@ -525,11 +525,11 @@ const char * NoteBinaryStoreTransmit(uint8_t *unencodedData, uint32_t unencodedL const char *err = ERRSTR("unencodedData cannot be NULL", c_err); NOTE_C_LOG_ERROR(err); return err; - } else if ((bufLen < cobsEncodedMaxLength(unencodedLen)) - && (bufLen < (cobsEncodedLength(unencodedData, unencodedLen) + 1))) { - // NOTE: `cobsEncodedMaxLength()` provides a constant time [O(1)] means + } else if ((bufLen < _cobsEncodedMaxLength(unencodedLen)) + && (bufLen < (_cobsEncodedLength(unencodedData, unencodedLen) + 1))) { + // NOTE: `_cobsEncodedMaxLength()` provides a constant time [O(1)] means // of checking the buffer size. Only when it fails will the linear - // time [O(n)] check, `cobsEncodedLength()`, be invoked. + // time [O(n)] check, `_cobsEncodedLength()`, be invoked. const char *err = ERRSTR("insufficient buffer size", c_bad); NOTE_C_LOG_ERROR(err); return err; @@ -623,9 +623,9 @@ const char * NoteBinaryStoreTransmit(uint8_t *unencodedData, uint32_t unencodedL JAddStringToObject(req, "status", hashString); // We already have the Notecard lock, so call - // noteTransactionShouldLock with `lockNotecard` set to false so we + // _noteTransactionShouldLock with `lockNotecard` set to false so we // don't try to lock again. - rsp = noteTransactionShouldLock(req, false); + rsp = _noteTransactionShouldLock(req, false); JDelete(req); // Ensure the transaction doesn't return an error. if (!rsp || NoteResponseError(rsp)) { @@ -763,7 +763,7 @@ void NoteTimeRefreshMins(uint32_t mins) @param seconds The UNIX Epoch time. */ /**************************************************************************/ -NOTE_C_STATIC void setTime(JTIME seconds) +NOTE_C_STATIC void _setTime(JTIME seconds) { timeBaseSec = seconds; timeBaseSetAtMs = _GetMs(); @@ -823,7 +823,7 @@ bool NotePrint(const char *text) { bool success = false; - if (noteIsDebugOutputActive()) { + if (_noteIsDebugOutputActive()) { NoteDebug(text); return true; } @@ -857,7 +857,7 @@ JTIME NoteTimeST(void) } // If it's time to refresh the time, do so - if (refreshTimerSecs != 0 && timerExpiredSecs(&timeRefreshTimer, refreshTimerSecs)) { + if (refreshTimerSecs != 0 && _timerExpiredSecs(&timeRefreshTimer, refreshTimerSecs)) { timeTimer = 0; } @@ -865,7 +865,7 @@ JTIME NoteTimeST(void) // so with a suppression timer so that we don't hammer the module before // it's had a chance to connect to the network to fetch time. if (!timeBaseSetManually && (timeTimer == 0 || timeBaseSec == 0 || zoneStillUnavailable || zoneForceRefresh)) { - if (timerExpiredSecs(&timeTimer, suppressionTimerSecs)) { + if (_timerExpiredSecs(&timeTimer, suppressionTimerSecs)) { // Request time and zone info from the card J *rsp = NoteRequestResponse(NoteNewRequest("card.time")); @@ -875,7 +875,7 @@ JTIME NoteTimeST(void) if (seconds != 0) { // Set the time - setTime(seconds); + _setTime(seconds); // Get the zone char *z = JGetString(rsp, "zone"); @@ -1040,9 +1040,9 @@ bool NoteLocalTimeST(uint16_t *retYear, uint8_t *retMonth, uint8_t *retDay, secs = (uint32_t) currentEpochTime + ((70*365L+17)*86400LU); days = secs / 86400; if (retWeekday != NULL) { - *retWeekday = (char *) daynames[(days + 1) % 7]; + *retWeekday = (char *) dayNames[(days + 1) % 7]; } - for (year = days / 365; days < (i = yToDays(year) + 365L * year); ) { + for (year = days / 365; days < (i = _yToDays(year) + 365L * year); ) { --year; } days -= i; @@ -1091,7 +1091,7 @@ bool NoteLocalTimeST(uint16_t *retYear, uint8_t *retMonth, uint8_t *retDay, } // Figure out how many days at start of the year -NOTE_C_STATIC int yToDays(int year) +NOTE_C_STATIC int _yToDays(int year) { int days = 0; if (0 < year) { @@ -1148,7 +1148,7 @@ bool NoteLocationValidST(char *errbuf, uint32_t errbuflen) // If we haven't yet fetched the location, do so with a suppression // timer so that we don't hammer the module before it's had a chance to // connect to the gps to fetch location. - if (!timerExpiredSecs(&locationTimer, suppressionTimerSecs)) { + if (!_timerExpiredSecs(&locationTimer, suppressionTimerSecs)) { return false; } @@ -1320,7 +1320,7 @@ bool NoteIsConnected(void) /**************************************************************************/ bool NoteIsConnectedST(void) { - if (timerExpiredSecs(&connectivityTimer, suppressionTimerSecs)) { + if (_timerExpiredSecs(&connectivityTimer, suppressionTimerSecs)) { J *rsp = NoteRequestResponse(NoteNewRequest("hub.status")); if (rsp != NULL) { if (!NoteResponseError(rsp)) { @@ -1559,7 +1559,7 @@ bool NoteGetServiceConfigST(char *productBuf, int productBufLen, char *serviceBu bool success = false; // Use cache except for a rare refresh - if (scProduct[0] == '\0' || scDevice[0] == '\0' || timerExpiredSecs(&serviceConfigTimer, 4*60*60)) { + if (scProduct[0] == '\0' || scDevice[0] == '\0' || _timerExpiredSecs(&serviceConfigTimer, 4*60*60)) { J *rsp = NoteRequestResponse(NoteNewRequest("hub.get")); if (rsp != NULL) { success = !NoteResponseError(rsp); @@ -1630,7 +1630,7 @@ bool NoteGetStatusST(char *statusBuf, int statusBufLen, JTIME *bootTime, bool *r static bool lastSignals = false; // Refresh if it's time to do so - if (timerExpiredSecs(&statusTimer, suppressionTimerSecs)) { + if (_timerExpiredSecs(&statusTimer, suppressionTimerSecs)) { J *rsp = NoteRequestResponse(NoteNewRequest("card.status")); if (rsp != NULL) { success = !NoteResponseError(rsp); @@ -1828,7 +1828,7 @@ bool NotePayloadRetrieveAfterSleep(NotePayloadDesc *desc) // Note the current time, if the field is present JTIME seconds = JGetInt(rsp, "time"); if (seconds != 0) { - setTime(seconds); + _setTime(seconds); } // If we didn't expect any state to be restored, we're done @@ -2273,7 +2273,7 @@ bool NoteSetContact(char *nameBuf, char *orgBuf, char *roleBuf, char *emailBuf) // reset to 0 after boot and every wake. This returns true if the specified // interval has elapsed, in seconds, and it updates the timer if it expires so // that we will go another period. -NOTE_C_STATIC bool timerExpiredSecs(uint32_t *timer, uint32_t periodSecs) +NOTE_C_STATIC bool _timerExpiredSecs(uint32_t *timer, uint32_t periodSecs) { bool expired = false; diff --git a/n_hooks.c b/n_hooks.c index 3e00d0e2..6a1a6c9f 100644 --- a/n_hooks.c +++ b/n_hooks.c @@ -243,7 +243,7 @@ void NoteSetFnDebugOutput(debugOutputFn fn) provided. */ /**************************************************************************/ -bool noteIsDebugOutputActive(void) +bool _noteIsDebugOutputActive(void) { return hookDebugOutput != NULL; } @@ -324,10 +324,10 @@ void NoteSetFnSerial(serialResetFn resetFn, serialTransmitFn transmitFn, hookSerialAvailable = availFn; hookSerialReceive = receiveFn; - notecardReset = serialNoteReset; - notecardTransaction = serialNoteTransaction; - notecardChunkedReceive = serialChunkedReceive; - notecardChunkedTransmit = serialChunkedTransmit; + notecardReset = _serialNoteReset; + notecardTransaction = _serialNoteTransaction; + notecardChunkedReceive = _serialChunkedReceive; + notecardChunkedTransmit = _serialChunkedTransmit; } /*! @@ -357,10 +357,10 @@ void NoteSetFnI2C(uint32_t notecardAddr, uint32_t maxTransmitSize, hookI2CTransmit = transmitFn; hookI2CReceive = receiveFn; - notecardReset = i2cNoteReset; - notecardTransaction = i2cNoteTransaction; - notecardChunkedReceive = i2cChunkedReceive; - notecardChunkedTransmit = i2cChunkedTransmit; + notecardReset = _i2cNoteReset; + notecardTransaction = _i2cNoteTransaction; + notecardChunkedReceive = _i2cChunkedReceive; + notecardChunkedTransmit = _i2cChunkedTransmit; } //**************************************************************************/ @@ -499,7 +499,7 @@ void NoteDelayMs(uint32_t ms) @param p the buffer to return it into */ /**************************************************************************/ -void n_htoa32(uint32_t n, char *p) +void _n_htoa32(uint32_t n, char *p) { int i; for (i=0; i<8; i++) { @@ -532,7 +532,7 @@ void *malloc_show(size_t len) if (p == NULL) { hookDebugOutput("FAIL"); } else { - n_htoa32((uint32_t)p, str); + _n_htoa32((uint32_t)p, str); hookDebugOutput(str); } return p; @@ -568,7 +568,7 @@ void NoteFree(void *p) if (hookFree != NULL) { #if NOTE_SHOW_MALLOC char str[16]; - n_htoa32((uint32_t)p, str); + _n_htoa32((uint32_t)p, str); hookDebugOutput("free"); hookDebugOutput(str); #endif @@ -605,7 +605,7 @@ void NoteUnlockI2C(void) @brief Lock the Notecard using the platform-specific hook. */ /**************************************************************************/ -void noteLockNote(void) +void _noteLockNote(void) { if (hookLockNote != NULL) { hookLockNote(); @@ -617,7 +617,7 @@ void noteLockNote(void) @brief Unlock the Notecard using the platform-specific hook. */ /**************************************************************************/ -void noteUnlockNote(void) +void _noteUnlockNote(void) { if (hookUnlockNote != NULL) { hookUnlockNote(); @@ -629,7 +629,7 @@ void noteUnlockNote(void) @brief Indicate that we're initiating a transaction using the platform-specific hook. */ /**************************************************************************/ -bool noteTransactionStart(uint32_t timeoutMs) +bool _noteTransactionStart(uint32_t timeoutMs) { if (hookTransactionStart != NULL) { return hookTransactionStart(timeoutMs); @@ -642,7 +642,7 @@ bool noteTransactionStart(uint32_t timeoutMs) @brief Indicate that we've completed a transaction using the platform-specific hook. */ /**************************************************************************/ -void noteTransactionStop(void) +void _noteTransactionStop(void) { if (hookTransactionStop != NULL) { hookTransactionStop(); @@ -655,7 +655,7 @@ void noteTransactionStop(void) @returns A string */ /**************************************************************************/ -const char *noteActiveInterface(void) +const char *_noteActiveInterface(void) { switch (hookActiveInterface) { case interfaceSerial: @@ -672,7 +672,7 @@ const char *noteActiveInterface(void) @returns A boolean indicating whether the Serial bus was reset successfully. */ /**************************************************************************/ -bool noteSerialReset(void) +bool _noteSerialReset(void) { if (hookActiveInterface == interfaceSerial && hookSerialReset != NULL) { return hookSerialReset(); @@ -688,7 +688,7 @@ bool noteSerialReset(void) @param flush `true` to flush the bytes upon transmit. */ /**************************************************************************/ -void noteSerialTransmit(uint8_t *text, size_t len, bool flush) +void _noteSerialTransmit(uint8_t *text, size_t len, bool flush) { if (hookActiveInterface == interfaceSerial && hookSerialTransmit != NULL) { hookSerialTransmit(text, len, flush); @@ -702,7 +702,7 @@ void noteSerialTransmit(uint8_t *text, size_t len, bool flush) @returns A boolean indicating whether the Serial bus is available to read. */ /**************************************************************************/ -bool noteSerialAvailable(void) +bool _noteSerialAvailable(void) { if (hookActiveInterface == interfaceSerial && hookSerialAvailable != NULL) { return hookSerialAvailable(); @@ -717,7 +717,7 @@ bool noteSerialAvailable(void) @returns A character from the Serial bus. */ /**************************************************************************/ -char noteSerialReceive(void) +char _noteSerialReceive(void) { if (hookActiveInterface == interfaceSerial && hookSerialReceive != NULL) { return hookSerialReceive(); @@ -731,7 +731,7 @@ char noteSerialReceive(void) @returns A boolean indicating whether the I2C bus was reset successfully. */ /**************************************************************************/ -bool noteI2CReset(uint16_t DevAddress) +bool _noteI2CReset(uint16_t DevAddress) { if (hookActiveInterface == interfaceI2C && hookI2CReset != NULL) { return hookI2CReset(DevAddress); @@ -749,7 +749,7 @@ bool noteI2CReset(uint16_t DevAddress) if the bus is not active. */ /**************************************************************************/ -const char *noteI2CTransmit(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size) +const char *_noteI2CTransmit(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size) { if (hookActiveInterface == interfaceI2C && hookI2CTransmit != NULL) { return hookI2CTransmit(DevAddress, pBuffer, Size); @@ -768,7 +768,7 @@ const char *noteI2CTransmit(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size if the bus is not active. */ /**************************************************************************/ -const char *noteI2CReceive(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size, uint32_t *available) +const char *_noteI2CReceive(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size, uint32_t *available) { if (hookActiveInterface == interfaceI2C && hookI2CReceive != NULL) { return hookI2CReceive(DevAddress, pBuffer, Size, available); @@ -830,7 +830,7 @@ uint32_t NoteI2CMax(void) @returns A boolean indicating whether the Notecard has been reset successfully. */ /**************************************************************************/ -bool noteHardReset(void) +bool _noteHardReset(void) { if (notecardReset == NULL) { return true; @@ -857,7 +857,7 @@ bool noteHardReset(void) or the hook has not been set. */ /**************************************************************************/ -const char *noteJSONTransaction(const char *request, size_t reqLen, char **response, uint32_t timeoutMs) +const char *_noteJSONTransaction(const char *request, size_t reqLen, char **response, uint32_t timeoutMs) { if (notecardTransaction == NULL || hookActiveInterface == interfaceNone) { return "i2c or serial interface must be selected"; @@ -883,7 +883,7 @@ const char *noteJSONTransaction(const char *request, size_t reqLen, char **respo @returns A c-string with an error, or `NULL` if no error ocurred. */ /**************************************************************************/ -const char *noteChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, +const char *_noteChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, uint32_t timeoutMs, uint32_t *available) { if (notecardChunkedReceive == NULL || hookActiveInterface == interfaceNone) { @@ -902,7 +902,7 @@ const char *noteChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, @returns A c-string with an error, or `NULL` if no error ocurred. */ /**************************************************************************/ -const char *noteChunkedTransmit(uint8_t *buffer, uint32_t size, bool delay) +const char *_noteChunkedTransmit(uint8_t *buffer, uint32_t size, bool delay) { if (notecardChunkedTransmit == NULL || hookActiveInterface == interfaceNone) { return "i2c or serial interface must be selected"; diff --git a/n_i2c.c b/n_i2c.c index deb22698..2754ed44 100644 --- a/n_i2c.c +++ b/n_i2c.c @@ -22,8 +22,8 @@ #endif // Forwards -NOTE_C_STATIC void delayIO(void); -NOTE_C_STATIC const char * i2cNoteQueryLength(uint32_t * available, uint32_t timeoutMs); +NOTE_C_STATIC void _delayIO(void); +NOTE_C_STATIC const char * _i2cNoteQueryLength(uint32_t * available, uint32_t timeoutMs); /**************************************************************************/ /*! @@ -33,7 +33,7 @@ NOTE_C_STATIC const char * i2cNoteQueryLength(uint32_t * available, uint32_t tim empirically based on a number of commercial devices. */ /**************************************************************************/ -NOTE_C_STATIC void delayIO(void) +NOTE_C_STATIC void _delayIO(void) { if (!cardTurboIO) { _DelayMs(6); @@ -49,7 +49,7 @@ NOTE_C_STATIC void delayIO(void) I2C read request can be issued. */ /**************************************************************************/ -NOTE_C_STATIC const char * i2cNoteQueryLength(uint32_t * available, +NOTE_C_STATIC const char * _i2cNoteQueryLength(uint32_t * available, uint32_t timeoutMs) { uint8_t dummy_buffer = 0; @@ -88,14 +88,14 @@ NOTE_C_STATIC const char * i2cNoteQueryLength(uint32_t * available, @returns a c-string with an error, or `NULL` if no error occurred. */ /**************************************************************************/ -const char *i2cNoteTransaction(const char *request, size_t reqLen, char **response, uint32_t timeoutMs) +const char *_i2cNoteTransaction(const char *request, size_t reqLen, char **response, uint32_t timeoutMs) { const char *err = NULL; // Lock over the entire transaction _LockI2C(); - err = i2cChunkedTransmit((uint8_t *)request, reqLen, true); + err = _i2cChunkedTransmit((uint8_t *)request, reqLen, true); if (err) { _UnlockI2C(); return err; @@ -107,13 +107,13 @@ const char *i2cNoteTransaction(const char *request, size_t reqLen, char **respon return NULL; } - delayIO(); + _delayIO(); // Allocate a buffer for input, noting that we always put the +1 in the // alloc so we can be assured that it can be null-terminated. This must be // the case because json parsing requires a null-terminated string. uint32_t available = 0; - err = i2cNoteQueryLength(&available, timeoutMs); + err = _i2cNoteQueryLength(&available, timeoutMs); if (err) { NOTE_C_LOG_ERROR(ERRSTR("failed to query Notecard", c_err)); _UnlockI2C(); @@ -137,7 +137,7 @@ const char *i2cNoteTransaction(const char *request, size_t reqLen, char **respon uint32_t jsonbufAvailLen = (jsonbufAllocLen - jsonbufLen); // Append into the json buffer - const char *err = i2cChunkedReceive((uint8_t *)(jsonbuf + jsonbufLen), &jsonbufAvailLen, true, (CARD_INTRA_TRANSACTION_TIMEOUT_SEC * 1000), &available); + const char *err = _i2cChunkedReceive((uint8_t *)(jsonbuf + jsonbufLen), &jsonbufAvailLen, true, (CARD_INTRA_TRANSACTION_TIMEOUT_SEC * 1000), &available); if (err) { if (jsonbuf) { _Free(jsonbuf); @@ -196,7 +196,7 @@ const char *i2cNoteTransaction(const char *request, size_t reqLen, char **respon @returns a boolean. `true` if the reset was successful, `false`, if not. */ /**************************************************************************/ -bool i2cNoteReset(void) +bool _i2cNoteReset(void) { bool notecardReady = false; @@ -212,7 +212,7 @@ bool i2cNoteReset(void) _UnlockI2C(); return false; } - delayIO(); + _delayIO(); // The guaranteed behavior for robust resyncing is to send two newlines // and wait for two echoed blank lines in return. @@ -300,7 +300,7 @@ bool i2cNoteReset(void) NOTE_C_LOG_ERROR(ERRSTR("error encountered during I2C reset hook execution", c_err)); break; } - delayIO(); + _delayIO(); } } else { notecardReady = true; @@ -336,7 +336,7 @@ bool i2cNoteReset(void) @returns A c-string with an error, or `NULL` if no error ocurred. */ /**************************************************************************/ -const char *i2cChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, uint32_t timeoutMs, uint32_t *available) +const char *_i2cChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, uint32_t timeoutMs, uint32_t *available) { // Load buffer with chunked I2C values size_t received = 0; @@ -427,7 +427,7 @@ const char *i2cChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, uint3 @returns A c-string with an error, or `NULL` if no error ocurred. */ /**************************************************************************/ -const char *i2cChunkedTransmit(uint8_t *buffer, uint32_t size, bool delay) +const char *_i2cChunkedTransmit(uint8_t *buffer, uint32_t size, bool delay) { // Transmit the request in chunks, but also in segments so as not to // overwhelm the notecard's interrupt buffers @@ -441,7 +441,7 @@ const char *i2cChunkedTransmit(uint8_t *buffer, uint32_t size, bool delay) // Constrain chunkLen to be <= _I2CMax(). chunkLen = (chunkLen > _I2CMax()) ? _I2CMax() : chunkLen; - delayIO(); + _delayIO(); estr = _I2CTransmit(_I2CAddress(), chunk, chunkLen); if (estr != NULL) { _I2CReset(_I2CAddress()); diff --git a/n_lib.h b/n_lib.h index 60d59b78..d76859f6 100644 --- a/n_lib.h +++ b/n_lib.h @@ -105,46 +105,46 @@ extern "C" { #endif // Transactions -J *noteTransactionShouldLock(J *req, bool lockNotecard); -const char *i2cNoteTransaction(const char *request, size_t reqLen, char **response, uint32_t timeoutMs); -bool i2cNoteReset(void); -const char *serialNoteTransaction(const char *request, size_t reqLen, char **response, uint32_t timeoutMs); -bool serialNoteReset(void); -const char *i2cChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, uint32_t timeoutMs, uint32_t *available); -const char *i2cChunkedTransmit(uint8_t *buffer, uint32_t size, bool delay); -const char *serialChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, uint32_t timeoutMs, uint32_t *available); -const char *serialChunkedTransmit(uint8_t *buffer, uint32_t size, bool delay); +J *_noteTransactionShouldLock(J *req, bool lockNotecard); +const char *_i2cNoteTransaction(const char *request, size_t reqLen, char **response, uint32_t timeoutMs); +bool _i2cNoteReset(void); +const char *_serialNoteTransaction(const char *request, size_t reqLen, char **response, uint32_t timeoutMs); +bool _serialNoteReset(void); +const char *_i2cChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, uint32_t timeoutMs, uint32_t *available); +const char *_i2cChunkedTransmit(uint8_t *buffer, uint32_t size, bool delay); +const char *_serialChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, uint32_t timeoutMs, uint32_t *available); +const char *_serialChunkedTransmit(uint8_t *buffer, uint32_t size, bool delay); // Hooks -void noteLockNote(void); -void noteUnlockNote(void); -bool noteTransactionStart(uint32_t timeoutMs); -void noteTransactionStop(void); -const char *noteActiveInterface(void); -bool noteSerialReset(void); -void noteSerialTransmit(uint8_t *, size_t, bool); -bool noteSerialAvailable(void); -char noteSerialReceive(void); -bool noteI2CReset(uint16_t DevAddress); -const char *noteI2CTransmit(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size); -const char *noteI2CReceive(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size, uint32_t *avail); -bool noteHardReset(void); -const char *noteJSONTransaction(const char *request, size_t reqLen, char **response, uint32_t timeoutMs); -const char *noteChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, uint32_t timeoutMs, uint32_t *available); -const char *noteChunkedTransmit(uint8_t *buffer, uint32_t size, bool delay); -bool noteIsDebugOutputActive(void); +void _noteLockNote(void); +void _noteUnlockNote(void); +bool _noteTransactionStart(uint32_t timeoutMs); +void _noteTransactionStop(void); +const char *_noteActiveInterface(void); +bool _noteSerialReset(void); +void _noteSerialTransmit(uint8_t *, size_t, bool); +bool _noteSerialAvailable(void); +char _noteSerialReceive(void); +bool _noteI2CReset(uint16_t DevAddress); +const char *_noteI2CTransmit(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size); +const char *_noteI2CReceive(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size, uint32_t *avail); +bool _noteHardReset(void); +const char *_noteJSONTransaction(const char *request, size_t reqLen, char **response, uint32_t timeoutMs); +const char *_noteChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, uint32_t timeoutMs, uint32_t *available); +const char *_noteChunkedTransmit(uint8_t *buffer, uint32_t size, bool delay); +bool _noteIsDebugOutputActive(void); // Utilities -void n_htoa32(uint32_t n, char *p); -void n_htoa16(uint16_t n, unsigned char *p); -uint64_t n_atoh(char *p, int maxLen); +void _n_htoa32(uint32_t n, char *p); +void _n_htoa16(uint16_t n, unsigned char *p); +uint64_t _n_atoh(char *p, int maxLen); // COBS Helpers -uint32_t cobsDecode(uint8_t *ptr, uint32_t length, uint8_t eop, uint8_t *dst); -uint32_t cobsEncode(uint8_t *ptr, uint32_t length, uint8_t eop, uint8_t *dst); -uint32_t cobsEncodedLength(const uint8_t *ptr, uint32_t length); -uint32_t cobsEncodedMaxLength(uint32_t length); -uint32_t cobsGuaranteedFit(uint32_t bufLen); +uint32_t _cobsDecode(uint8_t *ptr, uint32_t length, uint8_t eop, uint8_t *dst); +uint32_t _cobsEncode(uint8_t *ptr, uint32_t length, uint8_t eop, uint8_t *dst); +uint32_t _cobsEncodedLength(const uint8_t *ptr, uint32_t length); +uint32_t _cobsEncodedMaxLength(uint32_t length); +uint32_t _cobsGuaranteedFit(uint32_t bufLen); // Turbo I/O mode extern bool cardTurboIO; @@ -197,21 +197,21 @@ extern const char *c_badbinerr; // Readability wrappers. Anything starting with _ is simply calling the wrapper // function. -#define _LockNote noteLockNote -#define _UnlockNote noteUnlockNote -#define _TransactionStart noteTransactionStart -#define _TransactionStop noteTransactionStop -#define _SerialReset noteSerialReset -#define _SerialTransmit noteSerialTransmit -#define _SerialAvailable noteSerialAvailable -#define _SerialReceive noteSerialReceive -#define _I2CReset noteI2CReset -#define _I2CTransmit noteI2CTransmit -#define _I2CReceive noteI2CReceive -#define _Reset noteHardReset -#define _Transaction noteJSONTransaction -#define _ChunkedReceive noteChunkedReceive -#define _ChunkedTransmit noteChunkedTransmit +#define _LockNote _noteLockNote +#define _UnlockNote _noteUnlockNote +#define _TransactionStart _noteTransactionStart +#define _TransactionStop _noteTransactionStop +#define _SerialReset _noteSerialReset +#define _SerialTransmit _noteSerialTransmit +#define _SerialAvailable _noteSerialAvailable +#define _SerialReceive _noteSerialReceive +#define _I2CReset _noteI2CReset +#define _I2CTransmit _noteI2CTransmit +#define _I2CReceive _noteI2CReceive +#define _Reset _noteHardReset +#define _Transaction _noteJSONTransaction +#define _ChunkedReceive _noteChunkedReceive +#define _ChunkedTransmit _noteChunkedTransmit #define _Malloc NoteMalloc #define _Free NoteFree #define _GetMs NoteGetMs diff --git a/n_request.c b/n_request.c index f856934f..0773c813 100644 --- a/n_request.c +++ b/n_request.c @@ -30,9 +30,9 @@ static uint16_t lastRequestSeqno = 0; #define CRC_FIELD_LENGTH 22 // ,"crc":"SSSS:CCCCCCCC" #define CRC_FIELD_NAME_OFFSET 1 #define CRC_FIELD_NAME_TEST "\"crc\":\"" -NOTE_C_STATIC int32_t crc32(const void* data, size_t length); -NOTE_C_STATIC char * crcAdd(char *json, uint16_t seqno); -NOTE_C_STATIC bool crcError(char *json, uint16_t shouldBeSeqno); +NOTE_C_STATIC int32_t _crc32(const void* data, size_t length); +NOTE_C_STATIC char * _crcAdd(char *json, uint16_t seqno); +NOTE_C_STATIC bool _crcError(char *json, uint16_t shouldBeSeqno); static bool notecardSupportsCrc = false; #endif @@ -430,7 +430,7 @@ char * NoteRequestResponseJSON(const char *reqJSON) */ J *NoteTransaction(J *req) { - return noteTransactionShouldLock(req, true); + return _noteTransactionShouldLock(req, true); } /**************************************************************************/ @@ -445,7 +445,7 @@ J *NoteTransaction(J *req) insufficient memory. */ /**************************************************************************/ -J *noteTransactionShouldLock(J *req, bool lockNotecard) +J *_noteTransactionShouldLock(J *req, bool lockNotecard) { // Validate in case of memory failure of the requestor if (req == NULL) { @@ -512,13 +512,13 @@ J *noteTransactionShouldLock(J *req, bool lockNotecard) // modulus of seqno never are mistaken as being the same request being retried. uint8_t lastRequestRetries = 0; #ifndef NOTE_C_LOW_MEM - bool lastRequestCrcAdded = false; + bool lastRequest_crcAdded = false; if (!noResponseExpected) { - char *newJson = crcAdd(json, lastRequestSeqno); + char *newJson = _crcAdd(json, lastRequestSeqno); if (newJson != NULL) { JFree(json); json = newJson; - lastRequestCrcAdded = true; + lastRequest_crcAdded = true; } } #endif // !NOTE_C_LOW_MEM @@ -614,7 +614,7 @@ J *noteTransactionShouldLock(J *req, bool lockNotecard) // If we sent a CRC in the request, examine the response JSON to see if // it has a CRC error. Note that the CRC is stripped from the // responseJSON as a side-effect of this method. - if (lastRequestCrcAdded && crcError(responseJSON, lastRequestSeqno)) { + if (lastRequest_crcAdded && _crcError(responseJSON, lastRequestSeqno)) { JFree(responseJSON); errStr = "crc error {io}"; lastRequestRetries++; @@ -793,10 +793,11 @@ void NoteErrorClean(char *begin) @returns The converted number. */ -uint64_t n_atoh(char *p, int maxLen) +uint64_t _n_atoh(char *p, int maxLen) { uint64_t n = 0; char *ep = p+maxLen; + while (p < ep) { char ch = *p++; bool digit = (ch >= '0' && ch <= '9'); @@ -815,6 +816,7 @@ uint64_t n_atoh(char *p, int maxLen) n += 10 + (ch - 'A'); } } + return (n); } @@ -835,16 +837,18 @@ static uint32_t lut[16] = { @returns The CRC32 of the buffer. */ -NOTE_C_STATIC int32_t crc32(const void* data, size_t length) +NOTE_C_STATIC int32_t _crc32(const void* data, size_t length) { uint32_t previousCrc32 = 0; uint32_t crc = ~previousCrc32; unsigned char* current = (unsigned char*) data; + while (length--) { crc = lut[(crc ^ *current ) & 0x0F] ^ (crc >> 4); crc = lut[(crc ^ (*current >> 4)) & 0x0F] ^ (crc >> 4); current++; } + return ~crc; } @@ -861,7 +865,7 @@ NOTE_C_STATIC int32_t crc32(const void* data, size_t length) @returns A dynamically-allocated version of the passed in buffer with the CRC field added or NULL on error. */ -NOTE_C_STATIC char *crcAdd(char *json, uint16_t seqno) +NOTE_C_STATIC char *_crcAdd(char *json, uint16_t seqno) { // Allocate a block the size of the input json plus the size of @@ -869,10 +873,12 @@ NOTE_C_STATIC char *crcAdd(char *json, uint16_t seqno) // this will be replaced with a combination of 4 hex digits of the // seqno plus 8 hex digits of the CRC32, and the '}' will be // transformed into ',"crc":"SSSS:CCCCCCCC"}' where SSSS is the - // seqno and CCCCCCCC is the crc32. Note that the comma is + // seqno and CCCCCCCC is the CRC32. Note that the comma is // replaced with a space if the input json doesn't contain // any fields, so that we always return compliant JSON. + size_t jsonLen = strlen(json); + // Minimum JSON is "{}" and must end with a closing "}". if (jsonLen < 2 || json[jsonLen-1] != '}') { return NULL; @@ -881,8 +887,10 @@ NOTE_C_STATIC char *crcAdd(char *json, uint16_t seqno) if (newJson == NULL) { return NULL; } + bool isEmptyObject = (memchr(json, ':', jsonLen) == NULL); size_t newJsonLen = jsonLen-1; + memcpy(newJson, json, newJsonLen); newJson[newJsonLen++] = (isEmptyObject ? ' ' : ','); // Replace } newJson[newJsonLen++] = '"'; // +1 @@ -892,14 +900,15 @@ NOTE_C_STATIC char *crcAdd(char *json, uint16_t seqno) newJson[newJsonLen++] = '"'; // +5 newJson[newJsonLen++] = ':'; // +6 newJson[newJsonLen++] = '"'; // +7 - n_htoa16(seqno, (uint8_t *) &newJson[newJsonLen]); + _n_htoa16(seqno, (uint8_t *) &newJson[newJsonLen]); newJsonLen += 4; // +11 newJson[newJsonLen++] = ':'; // +12 - n_htoa32(crc32(json, jsonLen), &newJson[newJsonLen]); + _n_htoa32(_crc32(json, jsonLen), &newJson[newJsonLen]); newJsonLen += 8; // +20 newJson[newJsonLen++] = '"'; // +21 newJson[newJsonLen++] = '}'; // +22 == CRC_FIELD_LENGTH newJson[newJsonLen] = '\0'; // null-terminated as it came in + return newJson; } @@ -919,31 +928,35 @@ NOTE_C_STATIC char *crcAdd(char *json, uint16_t seqno) @returns `true` if there's an error and `false` otherwise. */ -NOTE_C_STATIC bool crcError(char *json, uint16_t shouldBeSeqno) +NOTE_C_STATIC bool _crcError(char *json, uint16_t shouldBeSeqno) { // Strip off any crlf for crc calculation size_t jsonLen = strlen(json); while (jsonLen > 0 && json[jsonLen-1] <= ' ') { jsonLen--; } + // Minimum valid JSON is "{}" (2 bytes) and must end with a closing "}". if (jsonLen < CRC_FIELD_LENGTH+2 || json[jsonLen-1] != '}') { return false; } + // See if it has a compliant CRC field size_t fieldOffset = ((jsonLen-1) - CRC_FIELD_LENGTH); if (memcmp(&json[fieldOffset+CRC_FIELD_NAME_OFFSET], CRC_FIELD_NAME_TEST, sizeof(CRC_FIELD_NAME_TEST)-1) != 0) { // If we've seen a CRC before, we should see one every time return notecardSupportsCrc ? true : false; } + // If we get here, we've seen at least one CRC from the Notecard, so we should expect it. notecardSupportsCrc = true; char *p = &json[fieldOffset + CRC_FIELD_NAME_OFFSET + (sizeof(CRC_FIELD_NAME_TEST)-1)]; - uint16_t actualSeqno = (uint16_t) n_atoh(p, 4); - uint32_t actualCrc32 = (uint32_t) n_atoh(p+5, 8); + uint16_t actualSeqno = (uint16_t) _n_atoh(p, 4); + uint32_t actualCrc32 = (uint32_t) _n_atoh(p+5, 8); json[fieldOffset++] = '}'; json[fieldOffset] = '\0'; - uint32_t shouldBeCrc32 = crc32(json, fieldOffset); + uint32_t shouldBeCrc32 = _crc32(json, fieldOffset); + return (shouldBeSeqno != actualSeqno || shouldBeCrc32 != actualCrc32); } diff --git a/n_serial.c b/n_serial.c index ede41893..a31f5385 100644 --- a/n_serial.c +++ b/n_serial.c @@ -31,7 +31,7 @@ @returns a c-string with an error, or `NULL` if no error occurred. */ /**************************************************************************/ -const char *serialNoteTransaction(const char *request, size_t reqLen, char **response, uint32_t timeoutMs) +const char *_serialNoteTransaction(const char *request, size_t reqLen, char **response, uint32_t timeoutMs) { // Strip off the newline and optional carriage return characters. This // allows for standardized output to be reapplied. @@ -40,7 +40,7 @@ const char *serialNoteTransaction(const char *request, size_t reqLen, char **res reqLen--; // remove carriage return if it exists } - const char *err = serialChunkedTransmit((uint8_t *)request, reqLen, true); + const char *err = _serialChunkedTransmit((uint8_t *)request, reqLen, true); if (err) { NOTE_C_LOG_ERROR(err); return err; @@ -86,7 +86,7 @@ const char *serialNoteTransaction(const char *request, size_t reqLen, char **res uint32_t jsonbufAvailLen = (jsonbufAllocLen - jsonbufLen); // Append into the json buffer - const char *err = serialChunkedReceive((uint8_t *)(jsonbuf + jsonbufLen), &jsonbufAvailLen, true, (CARD_INTRA_TRANSACTION_TIMEOUT_SEC * 1000), &available); + const char *err = _serialChunkedReceive((uint8_t *)(jsonbuf + jsonbufLen), &jsonbufAvailLen, true, (CARD_INTRA_TRANSACTION_TIMEOUT_SEC * 1000), &available); if (err) { _Free(jsonbuf); NOTE_C_LOG_ERROR(ERRSTR("error occured during receive", c_iobad)); @@ -132,7 +132,7 @@ const char *serialNoteTransaction(const char *request, size_t reqLen, char **res @returns a boolean. `true` if the reset was successful, `false`, if not. */ /**************************************************************************/ -bool serialNoteReset(void) +bool _serialNoteReset(void) { NOTE_C_LOG_DEBUG("resetting Serial interface..."); @@ -213,7 +213,7 @@ bool serialNoteReset(void) @returns A c-string with an error, or `NULL` if no error ocurred. */ /**************************************************************************/ -const char *serialChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, uint32_t timeoutMs, uint32_t *available) +const char *_serialChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, uint32_t timeoutMs, uint32_t *available) { size_t received = 0; bool overflow = (received >= *size); @@ -279,7 +279,7 @@ const char *serialChunkedReceive(uint8_t *buffer, uint32_t *size, bool delay, ui @returns A c-string with an error, or `NULL` if no error ocurred. */ /**************************************************************************/ -const char *serialChunkedTransmit(uint8_t *buffer, uint32_t size, bool delay) +const char *_serialChunkedTransmit(uint8_t *buffer, uint32_t size, bool delay) { // Transmit the request in segments so as not to overwhelm the Notecard's // interrupt buffers diff --git a/n_ua.c b/n_ua.c index f4a46ac9..f07829a5 100644 --- a/n_ua.c +++ b/n_ua.c @@ -129,7 +129,7 @@ __attribute__((weak)) J *NoteUserAgent() JAddStringToObject(ua, "agent", n_agent); JAddStringToObject(ua, "compiler", compiler); - JAddStringToObject(ua, "req_interface", noteActiveInterface()); + JAddStringToObject(ua, "req_interface", _noteActiveInterface()); // Add CPU Details if (n_cpu_cores != 0) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7607ac80..288864e8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -56,13 +56,18 @@ macro(add_test TEST_NAME) catch_discover_tests(${TEST_NAME}) endmacro(add_test) -add_test(crcAdd_test) -add_test(crcError_test) -add_test(i2cChunkedReceive_test) -add_test(i2cChunkedTransmit_test) -add_test(i2cNoteQueryLength_test) -add_test(i2cNoteReset_test) -add_test(i2cNoteTransaction_test) +add_test(_crcAdd_test) +add_test(_crcError_test) +add_test(_i2cChunkedReceive_test) +add_test(_i2cChunkedTransmit_test) +add_test(_i2cNoteQueryLength_test) +add_test(_i2cNoteReset_test) +add_test(_i2cNoteTransaction_test) +add_test(_j_tolower_test) +add_test(_serialChunkedReceive_test) +add_test(_serialChunkedTransmit_test) +add_test(_serialNoteReset_test) +add_test(_serialNoteTransaction_test) add_test(JAddBinaryToObject_test) add_test(JAllocString_test) add_test(JAtoI_test) @@ -89,7 +94,6 @@ add_test(JNumberValue_test) add_test(JPrintUnformatted_test) add_test(JSON_number_handling_test) add_test(JStringValue_test) -add_test(Jtolower_test) add_test(JType_test) add_test(NoteAdd_test) add_test(NoteBinaryCodecDecode_test) @@ -162,10 +166,6 @@ add_test(NoteTransaction_test) add_test(NoteTransactionHooks_test) add_test(NoteUserAgent_test) add_test(NoteWake_test) -add_test(serialChunkedReceive_test) -add_test(serialChunkedTransmit_test) -add_test(serialNoteReset_test) -add_test(serialNoteTransaction_test) if(NOTE_C_COVERAGE) find_program(LCOV lcov REQUIRED) diff --git a/test/hitl/card.binary/lib/notecard_binary/NotecardComms.cpp b/test/hitl/card.binary/lib/notecard_binary/NotecardComms.cpp index cff063f5..748dd6db 100644 --- a/test/hitl/card.binary/lib/notecard_binary/NotecardComms.cpp +++ b/test/hitl/card.binary/lib/notecard_binary/NotecardComms.cpp @@ -98,7 +98,7 @@ bool uninitialize_notecard_interface(NotecardInterface iface) #endif extern "C" { - uint32_t cobsEncode(uint8_t *ptr, uint32_t length, uint8_t eop, uint8_t *dst); + uint32_t _cobsEncode(uint8_t *ptr, uint32_t length, uint8_t eop, uint8_t *dst); } size_t readDataUntilTimeout(Stream& serial, size_t timeout, uint8_t* buf, size_t bufLen, size_t dataLen, size_t& duration) @@ -107,7 +107,7 @@ size_t readDataUntilTimeout(Stream& serial, size_t timeout, uint8_t* buf, size_t // encode the data to match what's on the wire const size_t dataShift = (bufLen - dataLen); memmove(buf + dataShift, buf, dataLen); - size_t encLen = cobsEncode(buf + dataShift, dataLen, '\n', buf); + size_t encLen = _cobsEncode(buf + dataShift, dataLen, '\n', buf); buf[encLen] = '\n'; diff --git a/test/include/test_static.h b/test/include/test_static.h index 5e679906..d4470152 100644 --- a/test/include/test_static.h +++ b/test/include/test_static.h @@ -12,13 +12,13 @@ extern "C" { #endif // Make these normally static functions externally visible if building tests. -char *crcAdd(char *json, uint16_t seqno); -bool crcError(char *json, uint16_t shouldBeSeqno); -void delayIO(void); -const char * i2cNoteQueryLength(uint32_t * available, uint32_t timeoutMs); -void setTime(JTIME seconds); -bool timerExpiredSecs(uint32_t *timer, uint32_t periodSecs); -char Jtolower(char c); +char *_crcAdd(char *json, uint16_t seqno); +bool _crcError(char *json, uint16_t shouldBeSeqno); +void _delayIO(void); +const char * _i2cNoteQueryLength(uint32_t * available, uint32_t timeoutMs); +void _setTime(JTIME seconds); +bool _timerExpiredSecs(uint32_t *timer, uint32_t periodSecs); +char _j_tolower(char c); #ifdef __cplusplus } diff --git a/test/src/JSON_number_handling_test.cpp b/test/src/JSON_number_handling_test.cpp index 9c636891..06eaba3a 100644 --- a/test/src/JSON_number_handling_test.cpp +++ b/test/src/JSON_number_handling_test.cpp @@ -18,7 +18,7 @@ // We treat most of the JSON code as "tested" in the sense that it comes from a // tested third party library. However, we have made some changes to the // underlying code. For example, we've tweaked the number parsing code (see -// parse_number and print_number in n_cjson.c). +// _parse_number and _print_number in n_cjson.c). namespace { diff --git a/test/src/NoteBinaryCodecDecode_test.cpp b/test/src/NoteBinaryCodecDecode_test.cpp index 78a79cfe..24520a79 100644 --- a/test/src/NoteBinaryCodecDecode_test.cpp +++ b/test/src/NoteBinaryCodecDecode_test.cpp @@ -19,7 +19,7 @@ #include "n_lib.h" DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(uint32_t, cobsDecode, uint8_t *, uint32_t, uint8_t, uint8_t *) +FAKE_VALUE_FUNC(uint32_t, _cobsDecode, uint8_t *, uint32_t, uint8_t, uint8_t *) uint8_t encData[12]; const uint32_t encDataLen = sizeof(encData); @@ -52,7 +52,7 @@ SCENARIO("NoteBinaryCodecDecode") } } WHEN("decBufLen is less than the size required for the worst-case decoding") { - uint32_t badDecLen = (cobsGuaranteedFit(encDataLen) - 1); + uint32_t badDecLen = (_cobsGuaranteedFit(encDataLen) - 1); decLen = NoteBinaryCodecDecode(encData, encDataLen, decBuf, badDecLen); THEN("The decoded length is zero") { @@ -63,18 +63,18 @@ SCENARIO("NoteBinaryCodecDecode") GIVEN("Parameters are in order") { const uint32_t EXPECTED_RESULT = 79; - cobsDecode_fake.return_val = EXPECTED_RESULT; + _cobsDecode_fake.return_val = EXPECTED_RESULT; decLen = NoteBinaryCodecDecode(encData, encDataLen, decBuf, decBufLen); - THEN("cobsDecode is invoked") { - CHECK(cobsDecode_fake.call_count > 0); + THEN("_cobsDecode is invoked") { + CHECK(_cobsDecode_fake.call_count > 0); } - WHEN("cobsDecode is invoked") { + WHEN("_cobsDecode is invoked") { THEN("The parameters are passed without modification") { - CHECK(cobsDecode_fake.arg0_val == encData); - CHECK(cobsDecode_fake.arg1_val == encDataLen); - CHECK(cobsDecode_fake.arg3_val == decBuf); + CHECK(_cobsDecode_fake.arg0_val == encData); + CHECK(_cobsDecode_fake.arg1_val == encDataLen); + CHECK(_cobsDecode_fake.arg3_val == decBuf); } THEN("The result is returned without modification") { @@ -83,7 +83,7 @@ SCENARIO("NoteBinaryCodecDecode") } } - RESET_FAKE(cobsDecode); + RESET_FAKE(_cobsDecode); } } diff --git a/test/src/NoteBinaryCodecEncode_test.cpp b/test/src/NoteBinaryCodecEncode_test.cpp index 595855e3..22c0f105 100644 --- a/test/src/NoteBinaryCodecEncode_test.cpp +++ b/test/src/NoteBinaryCodecEncode_test.cpp @@ -21,7 +21,7 @@ #include "n_lib.h" DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(uint32_t, cobsEncode, uint8_t *, uint32_t, uint8_t, uint8_t *) +FAKE_VALUE_FUNC(uint32_t, _cobsEncode, uint8_t *, uint32_t, uint8_t, uint8_t *) uint8_t decData[10] = "Hi Blues!"; uint32_t decDataLen = strlen((const char *)decData); @@ -53,7 +53,7 @@ SCENARIO("NoteBinaryCodecEncode") } } WHEN("encBufLen is less than the size required for in place encoding") { - uint32_t badOutLen = (cobsEncodedLength(decData, decDataLen) - 1); + uint32_t badOutLen = (_cobsEncodedLength(decData, decDataLen) - 1); encLen = NoteBinaryCodecEncode(decData, decDataLen, encBuf, badOutLen); THEN("The encoded length is zero") { @@ -64,18 +64,18 @@ SCENARIO("NoteBinaryCodecEncode") GIVEN("Parameters are in order") { const uint32_t EXPECTED_RESULT = 79; - cobsEncode_fake.return_val = EXPECTED_RESULT; + _cobsEncode_fake.return_val = EXPECTED_RESULT; encLen = NoteBinaryCodecEncode(decData, decDataLen, encBuf, encBufLen); - THEN("cobsEncode is invoked") { - CHECK(cobsEncode_fake.call_count > 0); + THEN("_cobsEncode is invoked") { + CHECK(_cobsEncode_fake.call_count > 0); } - WHEN("cobsEncode is invoked") { + WHEN("_cobsEncode is invoked") { THEN("The parameters are passed without modification") { - CHECK(cobsEncode_fake.arg0_val == decData); - CHECK(cobsEncode_fake.arg1_val == decDataLen); - CHECK(cobsEncode_fake.arg3_val == encBuf); + CHECK(_cobsEncode_fake.arg0_val == decData); + CHECK(_cobsEncode_fake.arg1_val == decDataLen); + CHECK(_cobsEncode_fake.arg3_val == encBuf); } THEN("The result is returned without modification") { @@ -84,7 +84,7 @@ SCENARIO("NoteBinaryCodecEncode") } } - RESET_FAKE(cobsEncode); + RESET_FAKE(_cobsEncode); } } diff --git a/test/src/NoteBinaryCodecMaxDecodedLength_test.cpp b/test/src/NoteBinaryCodecMaxDecodedLength_test.cpp index e59fc23b..0eb69c4c 100644 --- a/test/src/NoteBinaryCodecMaxDecodedLength_test.cpp +++ b/test/src/NoteBinaryCodecMaxDecodedLength_test.cpp @@ -19,7 +19,7 @@ #include "n_lib.h" DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(uint32_t, cobsGuaranteedFit, uint32_t) +FAKE_VALUE_FUNC(uint32_t, _cobsGuaranteedFit, uint32_t) const uint32_t bufferSize = 10; @@ -30,16 +30,16 @@ SCENARIO("NoteBinaryCodecMaxDecodedLength") { GIVEN("Parameters are in order") { const uint32_t EXPECTED_RESULT = 79; - cobsGuaranteedFit_fake.return_val = EXPECTED_RESULT; + _cobsGuaranteedFit_fake.return_val = EXPECTED_RESULT; const uint32_t result = NoteBinaryCodecMaxDecodedLength(bufferSize); - THEN("cobsGuaranteedFit is invoked") { - CHECK(cobsGuaranteedFit_fake.call_count > 0); + THEN("_cobsGuaranteedFit is invoked") { + CHECK(_cobsGuaranteedFit_fake.call_count > 0); } - WHEN("cobsGuaranteedFit is invoked") { + WHEN("_cobsGuaranteedFit is invoked") { THEN("The parameters are passed without modification") { - CHECK(cobsGuaranteedFit_fake.arg0_val == bufferSize); + CHECK(_cobsGuaranteedFit_fake.arg0_val == bufferSize); } THEN("The result is returned without modification") { @@ -48,7 +48,7 @@ SCENARIO("NoteBinaryCodecMaxDecodedLength") } } - RESET_FAKE(cobsGuaranteedFit); + RESET_FAKE(_cobsGuaranteedFit); } } diff --git a/test/src/NoteBinaryCodecMaxEncodedLength_test.cpp b/test/src/NoteBinaryCodecMaxEncodedLength_test.cpp index 76089384..f78d27df 100644 --- a/test/src/NoteBinaryCodecMaxEncodedLength_test.cpp +++ b/test/src/NoteBinaryCodecMaxEncodedLength_test.cpp @@ -19,7 +19,7 @@ #include "n_lib.h" DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(uint32_t, cobsEncodedMaxLength, uint32_t) +FAKE_VALUE_FUNC(uint32_t, _cobsEncodedMaxLength, uint32_t) const uint32_t unencodedLen = 10; @@ -30,16 +30,16 @@ SCENARIO("NoteBinaryCodecMaxEncodedLength") { GIVEN("Parameters are in order") { const uint32_t EXPECTED_RESULT = 79; - cobsEncodedMaxLength_fake.return_val = EXPECTED_RESULT; + _cobsEncodedMaxLength_fake.return_val = EXPECTED_RESULT; const uint32_t result = NoteBinaryCodecMaxEncodedLength(unencodedLen); - THEN("cobsEncodedMaxLength is invoked") { - CHECK(cobsEncodedMaxLength_fake.call_count > 0); + THEN("_cobsEncodedMaxLength is invoked") { + CHECK(_cobsEncodedMaxLength_fake.call_count > 0); } - WHEN("cobsEncodedMaxLength is invoked") { + WHEN("_cobsEncodedMaxLength is invoked") { THEN("The parameters are passed without modification") { - CHECK(cobsEncodedMaxLength_fake.arg0_val == unencodedLen); + CHECK(_cobsEncodedMaxLength_fake.arg0_val == unencodedLen); } THEN("The result is returned without modification") { @@ -48,7 +48,7 @@ SCENARIO("NoteBinaryCodecMaxEncodedLength") } } - RESET_FAKE(cobsEncodedMaxLength); + RESET_FAKE(_cobsEncodedMaxLength); } } diff --git a/test/src/NoteBinaryStoreReceive_test.cpp b/test/src/NoteBinaryStoreReceive_test.cpp index 31615325..8a5436b0 100644 --- a/test/src/NoteBinaryStoreReceive_test.cpp +++ b/test/src/NoteBinaryStoreReceive_test.cpp @@ -19,15 +19,15 @@ #include "n_lib.h" DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(J *, NoteNewRequest, const char *) +FAKE_VALUE_FUNC(const char *, _noteChunkedReceive, uint8_t *, uint32_t *, bool, + uint32_t, uint32_t *) +FAKE_VOID_FUNC(_noteLockNote) +FAKE_VALUE_FUNC(J *, _noteTransactionShouldLock, J *, bool) +FAKE_VOID_FUNC(_noteUnlockNote) FAKE_VALUE_FUNC(uint32_t, NoteBinaryCodecDecode, const uint8_t *, uint32_t, uint8_t *, uint32_t) FAKE_VALUE_FUNC(const char *, NoteBinaryStoreEncodedLength, uint32_t *) -FAKE_VALUE_FUNC(J *, noteTransactionShouldLock, J *, bool) -FAKE_VALUE_FUNC(const char *, noteChunkedReceive, uint8_t *, uint32_t *, bool, - uint32_t, uint32_t *) -FAKE_VOID_FUNC(noteLockNote) -FAKE_VOID_FUNC(noteUnlockNote) +FAKE_VALUE_FUNC(J *, NoteNewRequest, const char *) // Most of these variables have to be global because they're accessed in // lambda functions used as fakes for various note-c functions. They can't be @@ -61,7 +61,7 @@ SCENARIO("NoteBinaryStoreReceive") return NULL; }; - noteTransactionShouldLock_fake.custom_fake = [](J *req, bool) -> J * { + _noteTransactionShouldLock_fake.custom_fake = [](J *req, bool) -> J * { J *rsp = JCreateObject(); char hash[NOTE_MD5_HASH_STRING_SIZE] = {0}; NoteMD5HashString((unsigned char *)rawMsg, rawMsgLen, hash, @@ -110,7 +110,7 @@ SCENARIO("NoteBinaryStoreReceive") } GIVEN("The response to the card.binary.get request has an error") { - noteTransactionShouldLock_fake.custom_fake = [](J *req, bool) -> J * { + _noteTransactionShouldLock_fake.custom_fake = [](J *req, bool) -> J * { J *rsp = JCreateObject(); JAddStringToObject(rsp, "err", "some error"); @@ -120,29 +120,29 @@ SCENARIO("NoteBinaryStoreReceive") WHEN("NoteBinaryStoreReceive is called") { const char *err = NoteBinaryStoreReceive(buf, bufLen, decodedOffset, decodedLen); - REQUIRE(noteTransactionShouldLock_fake.call_count > 0); + REQUIRE(_noteTransactionShouldLock_fake.call_count > 0); THEN("An error is returned") { CHECK(err != NULL); } } } - GIVEN("noteChunkedReceive returns an error") { - noteChunkedReceive_fake.return_val = "some error"; + GIVEN("_noteChunkedReceive returns an error") { + _noteChunkedReceive_fake.return_val = "some error"; WHEN("NoteBinaryStoreReceive is called") { const char *err = NoteBinaryStoreReceive(buf, bufLen, decodedOffset, decodedLen); - REQUIRE(noteChunkedReceive_fake.call_count > 0); + REQUIRE(_noteChunkedReceive_fake.call_count > 0); THEN("An error is returned") { CHECK(err != NULL); } } } - GIVEN("noteChunkedReceive indicates there's unexpectedly more data " + GIVEN("_noteChunkedReceive indicates there's unexpectedly more data " "available") { - noteChunkedReceive_fake.custom_fake = [](uint8_t *, uint32_t *, bool, + _noteChunkedReceive_fake.custom_fake = [](uint8_t *, uint32_t *, bool, uint32_t, uint32_t *available) -> const char* { *available = 1; @@ -152,7 +152,7 @@ SCENARIO("NoteBinaryStoreReceive") WHEN("NoteBinaryStoreReceive is called") { const char *err = NoteBinaryStoreReceive(buf, bufLen, decodedOffset, decodedLen); - REQUIRE(noteChunkedReceive_fake.call_count > 0); + REQUIRE(_noteChunkedReceive_fake.call_count > 0); THEN("An error is returned") { CHECK(err != NULL); } @@ -161,9 +161,9 @@ SCENARIO("NoteBinaryStoreReceive") GIVEN("The binary payload is received") { NoteBinaryCodecDecode_fake.custom_fake = [](const uint8_t *encData, uint32_t encDataLen, uint8_t *decBuf, uint32_t) -> uint32_t { - return cobsDecode((uint8_t *)encData, encDataLen, '\n', decBuf); + return _cobsDecode((uint8_t *)encData, encDataLen, '\n', decBuf); }; - noteChunkedReceive_fake.custom_fake = [](uint8_t *buffer, uint32_t *size, + _noteChunkedReceive_fake.custom_fake = [](uint8_t *buffer, uint32_t *size, bool, uint32_t, uint32_t *available) -> const char* { uint32_t outLen = NoteBinaryCodecEncode((uint8_t *)rawMsg, rawMsgLen, buffer, *size); @@ -175,7 +175,7 @@ SCENARIO("NoteBinaryStoreReceive") }; AND_GIVEN("The computed MD5 hash doesn't match the status field") { - noteTransactionShouldLock_fake.custom_fake = [](J *req, bool) -> J * { + _noteTransactionShouldLock_fake.custom_fake = [](J *req, bool) -> J * { J *rsp = JCreateObject(); JAddStringToObject(rsp, "status", "garbage"); @@ -186,8 +186,8 @@ SCENARIO("NoteBinaryStoreReceive") WHEN("NoteBinaryStoreReceive is called") { const char *err = NoteBinaryStoreReceive(buf, bufLen, decodedOffset, decodedLen); - REQUIRE(noteChunkedReceive_fake.call_count > 0); - REQUIRE(noteTransactionShouldLock_fake.call_count > 0); + REQUIRE(_noteChunkedReceive_fake.call_count > 0); + REQUIRE(_noteTransactionShouldLock_fake.call_count > 0); THEN("An error is returned") { CHECK(err != NULL); } @@ -197,14 +197,14 @@ SCENARIO("NoteBinaryStoreReceive") AND_GIVEN("The decoded length does not match the reqeusted length") { decodedLen = rawMsgLen; NoteBinaryCodecDecode_fake.custom_fake = [](const uint8_t *encData, uint32_t encDataLen, uint8_t *decBuf, uint32_t) -> uint32_t { - return (cobsDecode((uint8_t *)encData, encDataLen, '\n', decBuf) - 1); + return (_cobsDecode((uint8_t *)encData, encDataLen, '\n', decBuf) - 1); }; WHEN("NoteBinaryStoreReceive is called") { const char *err = NoteBinaryStoreReceive(buf, bufLen, decodedOffset, decodedLen); - REQUIRE(noteChunkedReceive_fake.call_count > 0); - REQUIRE(noteTransactionShouldLock_fake.call_count > 0); + REQUIRE(_noteChunkedReceive_fake.call_count > 0); + REQUIRE(_noteTransactionShouldLock_fake.call_count > 0); THEN("An error is returned") { CHECK(err != NULL); } @@ -216,7 +216,7 @@ SCENARIO("NoteBinaryStoreReceive") uint32_t decodedLen = rawMsgLen; const char *err = NoteBinaryStoreReceive(buf, bufLen, decodedOffset, decodedLen); - REQUIRE(noteChunkedReceive_fake.call_count > 0); + REQUIRE(_noteChunkedReceive_fake.call_count > 0); THEN("No error is returned") { CHECK(err == NULL); } @@ -229,15 +229,15 @@ SCENARIO("NoteBinaryStoreReceive") } } - CHECK(noteLockNote_fake.call_count == noteUnlockNote_fake.call_count); + CHECK(_noteLockNote_fake.call_count == _noteUnlockNote_fake.call_count); - RESET_FAKE(noteTransactionShouldLock); + RESET_FAKE(_noteChunkedReceive); + RESET_FAKE(_noteLockNote); + RESET_FAKE(_noteTransactionShouldLock); + RESET_FAKE(_noteUnlockNote); RESET_FAKE(NoteBinaryCodecDecode); RESET_FAKE(NoteBinaryStoreEncodedLength); - RESET_FAKE(noteChunkedReceive); - RESET_FAKE(noteLockNote); RESET_FAKE(NoteNewRequest); - RESET_FAKE(noteUnlockNote); } } diff --git a/test/src/NoteBinaryStoreTransmit_test.cpp b/test/src/NoteBinaryStoreTransmit_test.cpp index 5de140b4..40aa6c06 100644 --- a/test/src/NoteBinaryStoreTransmit_test.cpp +++ b/test/src/NoteBinaryStoreTransmit_test.cpp @@ -19,12 +19,12 @@ #include "n_lib.h" DEFINE_FFF_GLOBALS +FAKE_VALUE_FUNC(const char *, _noteChunkedTransmit, uint8_t *, uint32_t, bool) +FAKE_VOID_FUNC(_noteLockNote) +FAKE_VALUE_FUNC(J *, _noteTransactionShouldLock, J *, bool) +FAKE_VOID_FUNC(_noteUnlockNote) FAKE_VALUE_FUNC(J *, NoteNewRequest, const char *) FAKE_VALUE_FUNC(J *, NoteRequestResponse, J *) -FAKE_VALUE_FUNC(J *, noteTransactionShouldLock, J *, bool) -FAKE_VALUE_FUNC(const char *, noteChunkedTransmit, uint8_t *, uint32_t, bool) -FAKE_VOID_FUNC(noteLockNote) -FAKE_VOID_FUNC(noteUnlockNote) uint8_t buf[32] = {0xDE, 0xAD, 0xBE, 0xEF}; uint32_t dataLen = 4; @@ -183,7 +183,7 @@ SCENARIO("NoteBinaryStoreTransmit") }; // Discover the actual encoded length of the data - const uint32_t tempBufLen = cobsEncodedMaxLength(dataLen); + const uint32_t tempBufLen = _cobsEncodedMaxLength(dataLen); uint8_t *tempBuf = (uint8_t *)malloc(tempBufLen); uint32_t encLen = NoteBinaryCodecEncode(buf, dataLen, tempBuf, tempBufLen); REQUIRE(encLen > 0); @@ -230,7 +230,7 @@ SCENARIO("NoteBinaryStoreTransmit") } AND_GIVEN("The card.binary.put request fails") { - noteTransactionShouldLock_fake.custom_fake = [](J *req, bool) -> J * { + _noteTransactionShouldLock_fake.custom_fake = [](J *req, bool) -> J * { return NULL; }; @@ -247,11 +247,11 @@ SCENARIO("NoteBinaryStoreTransmit") } } - AND_GIVEN("noteChunkedTransmit fails") { - noteTransactionShouldLock_fake.custom_fake = [](J *req, bool) -> J * { + AND_GIVEN("_noteChunkedTransmit fails") { + _noteTransactionShouldLock_fake.custom_fake = [](J *req, bool) -> J * { return JCreateObject(); }; - noteChunkedTransmit_fake.return_val = "some error"; + _noteChunkedTransmit_fake.return_val = "some error"; WHEN("NoteBinaryStoreTransmit is called") { const char *err = NoteBinaryStoreTransmit(buf, dataLen, bufLen, 0); @@ -268,10 +268,10 @@ SCENARIO("NoteBinaryStoreTransmit") AND_GIVEN("The response to the card.binary request after the " "transmission indicates a problem") { - noteTransactionShouldLock_fake.custom_fake = [](J *req, bool) -> J * { + _noteTransactionShouldLock_fake.custom_fake = [](J *req, bool) -> J * { return JCreateObject(); }; - noteChunkedTransmit_fake.return_val = NULL; + _noteChunkedTransmit_fake.return_val = NULL; auto initial = [](J *req) -> J * { JDelete(req); @@ -392,14 +392,14 @@ SCENARIO("NoteBinaryStoreTransmit") } } - CHECK(noteLockNote_fake.call_count == noteUnlockNote_fake.call_count); + CHECK(_noteLockNote_fake.call_count == _noteUnlockNote_fake.call_count); + RESET_FAKE(_noteChunkedTransmit); + RESET_FAKE(_noteLockNote); + RESET_FAKE(_noteTransactionShouldLock); + RESET_FAKE(_noteUnlockNote); RESET_FAKE(NoteNewRequest); RESET_FAKE(NoteRequestResponse); - RESET_FAKE(noteTransactionShouldLock); - RESET_FAKE(noteChunkedTransmit); - RESET_FAKE(noteLockNote); - RESET_FAKE(noteUnlockNote); } } diff --git a/test/src/NoteDebug_test.cpp b/test/src/NoteDebug_test.cpp index 377dc23d..0e26a5c6 100644 --- a/test/src/NoteDebug_test.cpp +++ b/test/src/NoteDebug_test.cpp @@ -47,7 +47,7 @@ SCENARIO("NoteDebug") memset(&state, 0, sizeof(state)); SECTION("Hook not set") { - CHECK(!noteIsDebugOutputActive()); + CHECK(!_noteIsDebugOutputActive()); NoteDebug(NULL); CHECK(!state.debugOutputCalled); @@ -57,7 +57,7 @@ SCENARIO("NoteDebug") NoteSetFnDebugOutput(MyDebugOutput); SECTION("Active") { - CHECK(noteIsDebugOutputActive()); + CHECK(_noteIsDebugOutputActive()); } SECTION("Hook called") { diff --git a/test/src/NotePrint_test.cpp b/test/src/NotePrint_test.cpp index 85498ab9..200190af 100644 --- a/test/src/NotePrint_test.cpp +++ b/test/src/NotePrint_test.cpp @@ -19,10 +19,10 @@ #include "n_lib.h" DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(bool, noteIsDebugOutputActive) +FAKE_VALUE_FUNC(bool, _noteIsDebugOutputActive) +FAKE_VOID_FUNC(NoteDebug, const char *) FAKE_VALUE_FUNC(J *, NoteNewRequest, const char *) FAKE_VALUE_FUNC(bool, NoteRequest, J *) -FAKE_VOID_FUNC(NoteDebug, const char *) namespace { @@ -34,14 +34,14 @@ SCENARIO("NotePrint") const char msg[] = "Hello world!"; SECTION("Debug") { - noteIsDebugOutputActive_fake.return_val = true; + _noteIsDebugOutputActive_fake.return_val = true; CHECK(NotePrint(msg)); CHECK(NoteDebug_fake.call_count > 0); } SECTION("card.log") { - noteIsDebugOutputActive_fake.return_val = false; + _noteIsDebugOutputActive_fake.return_val = false; SECTION("NoteNewRequest fails") { NoteNewRequest_fake.return_val = NULL; @@ -75,10 +75,10 @@ SCENARIO("NotePrint") } - RESET_FAKE(noteIsDebugOutputActive); + RESET_FAKE(_noteIsDebugOutputActive); + RESET_FAKE(NoteDebug); RESET_FAKE(NoteNewRequest); RESET_FAKE(NoteRequest); - RESET_FAKE(NoteDebug); } } diff --git a/test/src/NoteRequestResponseJSON_test.cpp b/test/src/NoteRequestResponseJSON_test.cpp index 50eb4b62..b35af34b 100644 --- a/test/src/NoteRequestResponseJSON_test.cpp +++ b/test/src/NoteRequestResponseJSON_test.cpp @@ -19,14 +19,14 @@ #include "n_lib.h" DEFINE_FFF_GLOBALS +FAKE_VALUE_FUNC(const char *, _noteJSONTransaction, const char *, size_t, char **, uint32_t) +FAKE_VOID_FUNC(_noteLockNote) +FAKE_VALUE_FUNC(bool, _noteTransactionStart, uint32_t) +FAKE_VOID_FUNC(_noteTransactionStop) +FAKE_VOID_FUNC(_noteUnlockNote) FAKE_VALUE_FUNC(N_CJSON_PUBLIC(J *), JParse, const char *) -FAKE_VALUE_FUNC(bool, noteTransactionStart, uint32_t) -FAKE_VOID_FUNC(noteTransactionStop) -FAKE_VOID_FUNC(noteLockNote) -FAKE_VALUE_FUNC(void *, NoteMalloc, size_t) FAKE_VOID_FUNC(NoteFree, void *) -FAKE_VOID_FUNC(noteUnlockNote) -FAKE_VALUE_FUNC(const char *, noteJSONTransaction, const char *, size_t, char **, uint32_t) +FAKE_VALUE_FUNC(void *, NoteMalloc, size_t) namespace { @@ -40,14 +40,14 @@ SCENARIO("NoteRequestResponseJSON") }; NoteMalloc_fake.custom_fake = malloc; NoteFree_fake.custom_fake = free; - noteTransactionStart_fake.return_val = true; + _noteTransactionStart_fake.return_val = true; GIVEN("The request is NULL") { WHEN("NoteRequestResponseJSON is called") { char *rsp = NoteRequestResponseJSON(NULL); - THEN("noteJSONTransaction is not called") { - CHECK(noteJSONTransaction_fake.call_count == 0); + THEN("_noteJSONTransaction is not called") { + CHECK(_noteJSONTransaction_fake.call_count == 0); } THEN("The response is NULL") { @@ -60,8 +60,8 @@ SCENARIO("NoteRequestResponseJSON") WHEN("NoteRequestResponseJSON is called") { char *rsp = NoteRequestResponseJSON(""); - THEN("noteJSONTransaction is not called") { - CHECK(noteJSONTransaction_fake.call_count == 0); + THEN("_noteJSONTransaction is not called") { + CHECK(_noteJSONTransaction_fake.call_count == 0); } THEN("The response is NULL") { @@ -76,10 +76,10 @@ SCENARIO("NoteRequestResponseJSON") WHEN("NoteRequestResponseJSON is called") { char *rsp = NoteRequestResponseJSON(req); - THEN("noteJSONTransaction is called with NULL for the response " + THEN("_noteJSONTransaction is called with NULL for the response " "parameter") { - REQUIRE(noteJSONTransaction_fake.call_count > 0); - CHECK(noteJSONTransaction_fake.arg2_history[0] == NULL); + REQUIRE(_noteJSONTransaction_fake.call_count > 0); + CHECK(_noteJSONTransaction_fake.arg2_history[0] == NULL); } } } @@ -96,18 +96,18 @@ SCENARIO("NoteRequestResponseJSON") WHEN("NoteRequestResponseJSON is called") { char *rsp = NoteRequestResponseJSON(req); - THEN("noteJSONTransaction is called once for each command") { - CHECK(noteJSONTransaction_fake.call_count == command_count); + THEN("_noteJSONTransaction is called once for each command") { + CHECK(_noteJSONTransaction_fake.call_count == command_count); } - THEN("noteJSONTransaction is called with the correct pointer and " + THEN("_noteJSONTransaction is called with the correct pointer and " "length for each command") { - CHECK(noteJSONTransaction_fake.arg0_history[0] == req); - CHECK(noteJSONTransaction_fake.arg1_history[0] == + CHECK(_noteJSONTransaction_fake.arg0_history[0] == req); + CHECK(_noteJSONTransaction_fake.arg1_history[0] == (sizeof(cmd1) - 1)); - CHECK(noteJSONTransaction_fake.arg0_history[1] == + CHECK(_noteJSONTransaction_fake.arg0_history[1] == (req + sizeof(cmd1) - 1)); - CHECK(noteJSONTransaction_fake.arg1_history[1] == + CHECK(_noteJSONTransaction_fake.arg1_history[1] == (sizeof(cmd2) - 1)); } } @@ -119,10 +119,10 @@ SCENARIO("NoteRequestResponseJSON") WHEN("NoteRequestResponseJSON is called") { char *rsp = NoteRequestResponseJSON(req); - THEN("noteJSONTransaction is called with a valid pointer for the " + THEN("_noteJSONTransaction is called with a valid pointer for the " "response parameter") { - REQUIRE(noteJSONTransaction_fake.call_count > 0); - CHECK(noteJSONTransaction_fake.arg2_history[0] != NULL); + REQUIRE(_noteJSONTransaction_fake.call_count > 0); + CHECK(_noteJSONTransaction_fake.arg2_history[0] != NULL); } } } @@ -136,10 +136,10 @@ SCENARIO("NoteRequestResponseJSON") THEN("The request is still treated as a regular request and not a " "command") { - REQUIRE(noteJSONTransaction_fake.call_count > 0); + REQUIRE(_noteJSONTransaction_fake.call_count > 0); // The response parameter is non-NULL, meaning we expect a // response (i.e. it's not a command). - CHECK(noteJSONTransaction_fake.arg2_history[0] != NULL); + CHECK(_noteJSONTransaction_fake.arg2_history[0] != NULL); } } } @@ -152,9 +152,9 @@ SCENARIO("NoteRequestResponseJSON") NoteMalloc_fake.return_val = NULL; char *rsp = NoteRequestResponseJSON(req); - THEN("noteJSONTransaction is not called") { + THEN("_noteJSONTransaction is not called") { REQUIRE(NoteMalloc_fake.call_count > 0); - CHECK(noteJSONTransaction_fake.call_count == 0); + CHECK(_noteJSONTransaction_fake.call_count == 0); } THEN("NoteFree is not called") { @@ -172,22 +172,22 @@ SCENARIO("NoteRequestResponseJSON") char *rsp = NoteRequestResponseJSON(req); THEN("NoteMalloc is called") { - REQUIRE(noteJSONTransaction_fake.call_count > 0); + REQUIRE(_noteJSONTransaction_fake.call_count > 0); CHECK(NoteMalloc_fake.call_count > 0); } - THEN("noteJSONTransaction is called with the newline appended") { - REQUIRE(noteJSONTransaction_fake.call_count > 0); - REQUIRE(noteJSONTransaction_fake.arg0_val != NULL); + THEN("_noteJSONTransaction is called with the newline appended") { + REQUIRE(_noteJSONTransaction_fake.call_count > 0); + REQUIRE(_noteJSONTransaction_fake.arg0_val != NULL); // We cannot test the value of the string passed to - // `noteJSONTransaction` because it is a pointer to a string + // `_noteJSONTransaction` because it is a pointer to a string // freed by `NoteFree`. We can only check that the length // of the string is one longer than would normally be expected. - CHECK(noteJSONTransaction_fake.arg1_val == sizeof(req)); + CHECK(_noteJSONTransaction_fake.arg1_val == sizeof(req)); } THEN("NoteFree is called to free the memory allocated by malloc") { - REQUIRE(noteJSONTransaction_fake.call_count > 0); + REQUIRE(_noteJSONTransaction_fake.call_count > 0); CHECK(NoteFree_fake.call_count == NoteMalloc_fake.call_count); } } @@ -198,9 +198,9 @@ SCENARIO("NoteRequestResponseJSON") JParse_fake.return_val = NULL; char *rsp = NoteRequestResponseJSON(cmd); - THEN("noteJSONTransaction is not called") { + THEN("_noteJSONTransaction is not called") { REQUIRE(NoteMalloc_fake.call_count > 0); - CHECK(noteJSONTransaction_fake.call_count == 0); + CHECK(_noteJSONTransaction_fake.call_count == 0); } THEN("NoteFree is called") { @@ -221,51 +221,51 @@ SCENARIO("NoteRequestResponseJSON") char *rsp = NoteRequestResponseJSON(req); THEN("The Notecard is locked and unlocked") { - REQUIRE(noteJSONTransaction_fake.call_count > 0); - CHECK(noteLockNote_fake.call_count == 1); - CHECK(noteUnlockNote_fake.call_count == 1); + REQUIRE(_noteJSONTransaction_fake.call_count > 0); + CHECK(_noteLockNote_fake.call_count == 1); + CHECK(_noteUnlockNote_fake.call_count == 1); } THEN("The transaction start/stop function are used to prepare the " "Notecard for the transaction") { - REQUIRE(noteJSONTransaction_fake.call_count > 0); - CHECK(noteTransactionStart_fake.call_count == 1); - CHECK(noteTransactionStop_fake.call_count == 1); + REQUIRE(_noteJSONTransaction_fake.call_count > 0); + CHECK(_noteTransactionStart_fake.call_count == 1); + CHECK(_noteTransactionStop_fake.call_count == 1); } THEN("NoteMalloc is not called") { - REQUIRE(noteJSONTransaction_fake.call_count > 0); + REQUIRE(_noteJSONTransaction_fake.call_count > 0); CHECK(NoteMalloc_fake.call_count == 0); } THEN("NoteFree is not called") { - REQUIRE(noteJSONTransaction_fake.call_count > 0); + REQUIRE(_noteJSONTransaction_fake.call_count > 0); CHECK(NoteFree_fake.call_count == 0); } } AND_GIVEN("The transaction with the Notecard fails to start") { - noteTransactionStart_fake.return_val = false; + _noteTransactionStart_fake.return_val = false; WHEN("NoteRequestResponseJSON is called") { char *rsp = NoteRequestResponseJSON(req); THEN("NULL is returned") { - REQUIRE(noteTransactionStart_fake.call_count > 0); + REQUIRE(_noteTransactionStart_fake.call_count > 0); CHECK(rsp == NULL); } } } } + RESET_FAKE(_noteJSONTransaction); + RESET_FAKE(_noteLockNote); + RESET_FAKE(_noteTransactionStart); + RESET_FAKE(_noteTransactionStop); + RESET_FAKE(_noteUnlockNote); RESET_FAKE(JParse); - RESET_FAKE(NoteMalloc); RESET_FAKE(NoteFree); - RESET_FAKE(noteTransactionStart); - RESET_FAKE(noteTransactionStop); - RESET_FAKE(noteLockNote); - RESET_FAKE(noteUnlockNote); - RESET_FAKE(noteJSONTransaction); + RESET_FAKE(NoteMalloc); } } diff --git a/test/src/NoteReset_test.cpp b/test/src/NoteReset_test.cpp index e7aa7f26..2a00ffb3 100644 --- a/test/src/NoteReset_test.cpp +++ b/test/src/NoteReset_test.cpp @@ -19,7 +19,7 @@ #include "n_lib.h" DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(bool, noteHardReset) +FAKE_VALUE_FUNC(bool, _noteHardReset) namespace { @@ -29,20 +29,20 @@ SCENARIO("NoteReset") GIVEN("NoteReset is called") { NoteReset(); - THEN("noteHardReset is invoked") { - CHECK(noteHardReset_fake.call_count > 0); + THEN("_noteHardReset is invoked") { + CHECK(_noteHardReset_fake.call_count > 0); } - WHEN("`noteHardReset` returns `false`") { - noteHardReset_fake.return_val = false; + WHEN("`_noteHardReset` returns `false`") { + _noteHardReset_fake.return_val = false; THEN("`NoteReset` also returns `false`") { bool result = NoteReset(); CHECK(result == false); } } - WHEN("`noteHardReset` returns `true`") { - noteHardReset_fake.return_val = true; + WHEN("`_noteHardReset` returns `true`") { + _noteHardReset_fake.return_val = true; THEN("`NoteReset` also returns `true`") { bool result = NoteReset(); CHECK(result == true); @@ -50,7 +50,7 @@ SCENARIO("NoteReset") } } - RESET_FAKE(noteHardReset); + RESET_FAKE(_noteHardReset); } } diff --git a/test/src/NoteResponseError_test.cpp b/test/src/NoteResponseError_test.cpp index ef1c6f57..6ae7b53a 100644 --- a/test/src/NoteResponseError_test.cpp +++ b/test/src/NoteResponseError_test.cpp @@ -19,11 +19,10 @@ // DEFINE_FFF_GLOBALS // FAKE_VALUE_FUNC(bool, NoteReset) -// FAKE_VALUE_FUNC(const char *, noteJSONTransaction, const char *, size_t, char **, uint32_t) -// FAKE_VALUE_FUNC(bool, noteTransactionStart, uint32_t) +// FAKE_VALUE_FUNC(const char *, _noteJSONTransaction, const char *, size_t, char **, uint32_t) +// FAKE_VALUE_FUNC(bool, _noteTransactionStart, uint32_t) // FAKE_VALUE_FUNC(J *, NoteUserAgent) -// FAKE_VALUE_FUNC(bool, crcError, char *, uint16_t) - +// FAKE_VALUE_FUNC(bool, _crcError, char *, uint16_t) SCENARIO("NoteResponseError") { @@ -32,8 +31,8 @@ SCENARIO("NoteResponseError") // // NoteReset's mock should succeed unless the test explicitly instructs // // it to fail. // NoteReset_fake.return_val = true; - // noteTransactionStart_fake.return_val = true; - // crcError_fake.return_val = false; + // _noteTransactionStart_fake.return_val = true; + // _crcError_fake.return_val = false; SECTION("No err field, returns false") { J* rsp = JParse("{\"test\":\"me\"}"); @@ -48,6 +47,5 @@ SCENARIO("NoteResponseError") CHECK((hasError ==true && rsp != NULL)); JDelete(rsp); } - } diff --git a/test/src/NoteSerialHooks_test.cpp b/test/src/NoteSerialHooks_test.cpp index d6e8f95f..5fcccbdb 100644 --- a/test/src/NoteSerialHooks_test.cpp +++ b/test/src/NoteSerialHooks_test.cpp @@ -11,8 +11,6 @@ * */ - - #include #include "n_lib.h" @@ -38,8 +36,8 @@ void MyTxnStop() SCENARIO("NoteSerialHooks") { SECTION("Hooks not set") { - noteTransactionStart(0); - noteTransactionStop(); + _noteTransactionStart(0); + _noteTransactionStop(); CHECK(!txnStartCalled); CHECK(!txnStopCalled); @@ -47,8 +45,8 @@ SCENARIO("NoteSerialHooks") SECTION("Hooks set") { NoteSetFnTransaction(MyTxnStart, MyTxnStop); - noteTransactionStart(0); - noteTransactionStop(); + _noteTransactionStart(0); + _noteTransactionStop(); CHECK(txnStartCalled); CHECK(txnStopCalled); diff --git a/test/src/NoteSetFnI2C_test.cpp b/test/src/NoteSetFnI2C_test.cpp index d2960240..3487b412 100644 --- a/test/src/NoteSetFnI2C_test.cpp +++ b/test/src/NoteSetFnI2C_test.cpp @@ -19,8 +19,8 @@ #include "n_lib.h" DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(bool, i2cNoteReset) -FAKE_VALUE_FUNC(const char *, i2cNoteTransaction, const char *, size_t, char **, uint32_t) +FAKE_VALUE_FUNC(bool, _i2cNoteReset) +FAKE_VALUE_FUNC(const char *, _i2cNoteTransaction, const char *, size_t, char **, uint32_t) namespace { @@ -52,29 +52,29 @@ SCENARIO("NoteSetFnI2C") { char req[] = "{ \"req\": \"note.add\" }\n"; char *resp = NULL; - i2cNoteReset_fake.return_val = true; - i2cNoteTransaction_fake.return_val = NULL; + _i2cNoteReset_fake.return_val = true; + _i2cNoteTransaction_fake.return_val = NULL; const uint16_t i2cAddr = 0xBEEF; const uint32_t i2cMax = 16; NoteSetFnI2C(i2cAddr, i2cMax, I2CReset, I2CTransmit, I2CReceive); - CHECK(noteI2CReset(i2cAddr)); + CHECK(_noteI2CReset(i2cAddr)); CHECK(i2cResetCalled == 1); - CHECK(noteI2CTransmit(i2cAddr, (uint8_t *)req, strlen(req)) == NULL); + CHECK(_noteI2CTransmit(i2cAddr, (uint8_t *)req, strlen(req)) == NULL); CHECK(i2cTransmitCalled == 1); - CHECK(noteI2CReceive(i2cAddr, NULL, 0, NULL) == NULL); + CHECK(_noteI2CReceive(i2cAddr, NULL, 0, NULL) == NULL); CHECK(i2cReceiveCalled == 1); - CHECK(strcmp(noteActiveInterface(), "i2c") == 0); + CHECK(strcmp(_noteActiveInterface(), "i2c") == 0); - CHECK(noteHardReset()); - CHECK(i2cNoteReset_fake.call_count == 1); + CHECK(_noteHardReset()); + CHECK(_i2cNoteReset_fake.call_count == 1); - CHECK(noteJSONTransaction(req, strlen(req), &resp, CARD_INTER_TRANSACTION_TIMEOUT_SEC) == NULL); - CHECK(i2cNoteTransaction_fake.call_count == 1); + CHECK(_noteJSONTransaction(req, strlen(req), &resp, CARD_INTER_TRANSACTION_TIMEOUT_SEC) == NULL); + CHECK(_i2cNoteTransaction_fake.call_count == 1); CHECK(NoteI2CAddress() == i2cAddr); CHECK(NoteI2CMax() == i2cMax); @@ -90,28 +90,28 @@ SCENARIO("NoteSetFnI2C") NoteSetI2CAddress(0); NoteSetFnDisabled(); - CHECK(noteI2CReset(i2cAddr)); + CHECK(_noteI2CReset(i2cAddr)); CHECK(i2cResetCalled == 1); - CHECK(noteI2CTransmit(i2cAddr, (uint8_t *)req, strlen(req)) != NULL); + CHECK(_noteI2CTransmit(i2cAddr, (uint8_t *)req, strlen(req)) != NULL); CHECK(i2cTransmitCalled == 1); - CHECK(noteI2CReceive(i2cAddr, NULL, 0, NULL) != NULL); + CHECK(_noteI2CReceive(i2cAddr, NULL, 0, NULL) != NULL); CHECK(i2cReceiveCalled == 1); - CHECK(strcmp(noteActiveInterface(), "unknown") == 0); + CHECK(strcmp(_noteActiveInterface(), "unknown") == 0); - CHECK(noteHardReset()); - CHECK(i2cNoteReset_fake.call_count == 1); + CHECK(_noteHardReset()); + CHECK(_i2cNoteReset_fake.call_count == 1); - CHECK(noteJSONTransaction(req, strlen(req), &resp, CARD_INTER_TRANSACTION_TIMEOUT_SEC) != NULL); - CHECK(i2cNoteTransaction_fake.call_count == 1); + CHECK(_noteJSONTransaction(req, strlen(req), &resp, CARD_INTER_TRANSACTION_TIMEOUT_SEC) != NULL); + CHECK(_i2cNoteTransaction_fake.call_count == 1); CHECK(NoteI2CAddress() == NOTE_I2C_ADDR_DEFAULT); CHECK(NoteI2CMax() == NOTE_I2C_MAX_DEFAULT); - RESET_FAKE(i2cNoteReset); - RESET_FAKE(i2cNoteTransaction); + RESET_FAKE(_i2cNoteReset); + RESET_FAKE(_i2cNoteTransaction); } } diff --git a/test/src/NoteSetFnMutex_test.cpp b/test/src/NoteSetFnMutex_test.cpp index c8ed7ac3..a7e7efee 100644 --- a/test/src/NoteSetFnMutex_test.cpp +++ b/test/src/NoteSetFnMutex_test.cpp @@ -11,8 +11,6 @@ * */ - - #include #include "n_lib.h" @@ -55,10 +53,10 @@ SCENARIO("NoteSetFnMutex") NoteUnlockI2C(); CHECK(unlockI2CCalled == 1); - noteLockNote(); + _noteLockNote(); CHECK(lockNoteCalled == 1); - noteUnlockNote(); + _noteUnlockNote(); CHECK(unlockNoteCalled == 1); // Unset the callbacks and ensure they aren't called again. @@ -70,13 +68,11 @@ SCENARIO("NoteSetFnMutex") NoteUnlockI2C(); CHECK(unlockI2CCalled == 1); - noteLockNote(); + _noteLockNote(); CHECK(lockNoteCalled == 1); - noteUnlockNote(); + _noteUnlockNote(); CHECK(unlockNoteCalled == 1); } } - - diff --git a/test/src/NoteSetFnNoteMutex_test.cpp b/test/src/NoteSetFnNoteMutex_test.cpp index c6f39bff..3c0412a4 100644 --- a/test/src/NoteSetFnNoteMutex_test.cpp +++ b/test/src/NoteSetFnNoteMutex_test.cpp @@ -11,8 +11,6 @@ * */ - - #include #include "n_lib.h" @@ -37,22 +35,20 @@ SCENARIO("NoteSetFnNoteMutex") { NoteSetFnNoteMutex(LockNote, UnlockNote); - noteLockNote(); + _noteLockNote(); CHECK(lockNoteCalled == 1); - noteUnlockNote(); + _noteUnlockNote(); CHECK(unlockNoteCalled == 1); // Unset the callbacks and ensure they aren't called again. NoteSetFnNoteMutex(NULL, NULL); - noteLockNote(); + _noteLockNote(); CHECK(lockNoteCalled == 1); - noteUnlockNote(); + _noteUnlockNote(); CHECK(unlockNoteCalled == 1); } } - - diff --git a/test/src/NoteSetFnSerial_test.cpp b/test/src/NoteSetFnSerial_test.cpp index 312deb17..0da7c1e0 100644 --- a/test/src/NoteSetFnSerial_test.cpp +++ b/test/src/NoteSetFnSerial_test.cpp @@ -11,16 +11,14 @@ * */ - - #include #include "fff.h" #include "n_lib.h" DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(bool, serialNoteReset) -FAKE_VALUE_FUNC(const char *, serialNoteTransaction, const char *, size_t, char **, uint32_t) +FAKE_VALUE_FUNC(bool, _serialNoteReset) +FAKE_VALUE_FUNC(const char *, _serialNoteTransaction, const char *, size_t, char **, uint32_t) namespace { @@ -53,65 +51,62 @@ char serialReceive() return 'a'; } - SCENARIO("NoteSetFnSerial") { char req[] = "{ \"req\": \"note.add\" }\n"; char *resp = NULL; - serialNoteReset_fake.return_val = true; - serialNoteTransaction_fake.return_val = NULL; + _serialNoteReset_fake.return_val = true; + _serialNoteTransaction_fake.return_val = NULL; NoteSetFnSerial(serialReset, serialTransmit, serialAvailable, serialReceive); - CHECK(noteSerialReset()); + CHECK(_noteSerialReset()); CHECK(serialResetCalled == 1); - noteSerialTransmit((uint8_t *)req, strlen(req), false); + _noteSerialTransmit((uint8_t *)req, strlen(req), false); CHECK(serialTransmitCalled == 1); - CHECK(noteSerialAvailable()); + CHECK(_noteSerialAvailable()); CHECK(serialAvailableCalled == 1); - CHECK(noteSerialReceive() == 'a'); + CHECK(_noteSerialReceive() == 'a'); CHECK(serialReceiveCalled == 1); - CHECK(strcmp(noteActiveInterface(), "serial") == 0); + CHECK(strcmp(_noteActiveInterface(), "serial") == 0); - CHECK(noteHardReset()); - CHECK(serialNoteReset_fake.call_count == 1); + CHECK(_noteHardReset()); + CHECK(_serialNoteReset_fake.call_count == 1); - CHECK(noteJSONTransaction(req, strlen(req), &resp, CARD_INTER_TRANSACTION_TIMEOUT_SEC) == NULL); - CHECK(serialNoteTransaction_fake.call_count == 1); + CHECK(_noteJSONTransaction(req, strlen(req), &resp, CARD_INTER_TRANSACTION_TIMEOUT_SEC) == NULL); + CHECK(_serialNoteTransaction_fake.call_count == 1); // Unset the callbacks and ensure they aren't called again. NoteSetFnSerial(NULL, NULL, NULL, NULL); NoteSetFnDisabled(); - CHECK(noteSerialReset()); + CHECK(_noteSerialReset()); CHECK(serialResetCalled == 1); - noteSerialTransmit((uint8_t *)req, strlen(req), false); + _noteSerialTransmit((uint8_t *)req, strlen(req), false); CHECK(serialTransmitCalled == 1); - CHECK(!noteSerialAvailable()); + CHECK(!_noteSerialAvailable()); CHECK(serialAvailableCalled == 1); - CHECK(noteSerialReceive() == 0); + CHECK(_noteSerialReceive() == 0); CHECK(serialReceiveCalled == 1); - CHECK(strcmp(noteActiveInterface(), "unknown") == 0); + CHECK(strcmp(_noteActiveInterface(), "unknown") == 0); - CHECK(noteHardReset()); - CHECK(serialNoteReset_fake.call_count == 1); + CHECK(_noteHardReset()); + CHECK(_serialNoteReset_fake.call_count == 1); - CHECK(noteJSONTransaction(req, strlen(req), &resp, CARD_INTER_TRANSACTION_TIMEOUT_SEC) != NULL); - CHECK(serialNoteTransaction_fake.call_count == 1); + CHECK(_noteJSONTransaction(req, strlen(req), &resp, CARD_INTER_TRANSACTION_TIMEOUT_SEC) != NULL); + CHECK(_serialNoteTransaction_fake.call_count == 1); - RESET_FAKE(serialNoteReset); - RESET_FAKE(serialNoteTransaction); + RESET_FAKE(_serialNoteReset); + RESET_FAKE(_serialNoteTransaction); } } - - diff --git a/test/src/NoteTransactionHooks_test.cpp b/test/src/NoteTransactionHooks_test.cpp index 13fb3a3b..bdbbd5e4 100644 --- a/test/src/NoteTransactionHooks_test.cpp +++ b/test/src/NoteTransactionHooks_test.cpp @@ -11,8 +11,6 @@ * */ - - #include #include "n_lib.h" @@ -38,8 +36,8 @@ void MyTxnStop() SCENARIO("NoteTransactionHooks") { SECTION("Hooks not set") { - noteTransactionStart(0); - noteTransactionStop(); + _noteTransactionStart(0); + _noteTransactionStop(); CHECK(!txnStartCalled); CHECK(!txnStopCalled); @@ -47,8 +45,8 @@ SCENARIO("NoteTransactionHooks") SECTION("Hooks set") { NoteSetFnTransaction(MyTxnStart, MyTxnStop); - noteTransactionStart(0); - noteTransactionStop(); + _noteTransactionStart(0); + _noteTransactionStop(); CHECK(txnStartCalled); CHECK(txnStopCalled); @@ -59,5 +57,3 @@ SCENARIO("NoteTransactionHooks") } } - - diff --git a/test/src/NoteTransaction_test.cpp b/test/src/NoteTransaction_test.cpp index 3c7f0c96..aaed2390 100644 --- a/test/src/NoteTransaction_test.cpp +++ b/test/src/NoteTransaction_test.cpp @@ -18,16 +18,16 @@ #include "test_static.h" DEFINE_FFF_GLOBALS +FAKE_VALUE_FUNC(bool, _crcError, char *, uint16_t) +FAKE_VALUE_FUNC(const char *, _noteJSONTransaction, const char *, size_t, char **, uint32_t) +FAKE_VALUE_FUNC(bool, _noteTransactionStart, uint32_t) FAKE_VALUE_FUNC(bool, NoteReset) -FAKE_VALUE_FUNC(const char *, noteJSONTransaction, const char *, size_t, char **, uint32_t) -FAKE_VALUE_FUNC(bool, noteTransactionStart, uint32_t) FAKE_VALUE_FUNC(J *, NoteUserAgent) -FAKE_VALUE_FUNC(bool, crcError, char *, uint16_t) namespace { -const char *noteJSONTransactionValid(const char *, size_t, char **resp, uint32_t) +const char *_noteJSONTransactionValid(const char *, size_t, char **resp, uint32_t) { static char respString[] = "{ \"total\": 1 }"; @@ -40,7 +40,7 @@ const char *noteJSONTransactionValid(const char *, size_t, char **resp, uint32_t return NULL; } -const char *noteJSONTransactionBadJSON(const char *, size_t, char **resp, uint32_t) +const char *_noteJSONTransactionBadJSON(const char *, size_t, char **resp, uint32_t) { static char respString[] = "Bad JSON"; @@ -53,7 +53,7 @@ const char *noteJSONTransactionBadJSON(const char *, size_t, char **resp, uint32 return NULL; } -const char *noteJSONTransactionIOError(const char *, size_t, char **resp, uint32_t) +const char *_noteJSONTransactionIOError(const char *, size_t, char **resp, uint32_t) { static char respString[] = "{\"err\": \"{io}\"}"; @@ -66,7 +66,7 @@ const char *noteJSONTransactionIOError(const char *, size_t, char **resp, uint32 return NULL; } -const char *noteJSONTransactionNotSupportedError(const char *, size_t, char **resp, uint32_t) +const char *_noteJSONTransactionNotSupportedError(const char *, size_t, char **resp, uint32_t) { static char respString[] = "{\"err\": \"{not-supported}\"}"; @@ -86,15 +86,15 @@ SCENARIO("NoteTransaction") // NoteReset's mock should succeed unless the test explicitly instructs // it to fail. NoteReset_fake.return_val = true; - noteTransactionStart_fake.return_val = true; - crcError_fake.return_val = false; + _noteTransactionStart_fake.return_val = true; + _crcError_fake.return_val = false; SECTION("Passing a NULL request returns NULL") { CHECK(NoteTransaction(NULL) == NULL); } - SECTION("noteTransactionStart fails") { - noteTransactionStart_fake.return_val = false; + SECTION("_noteTransactionStart fails") { + _noteTransactionStart_fake.return_val = false; J *req = NoteNewRequest("note.add"); REQUIRE(req != NULL); @@ -106,11 +106,11 @@ SCENARIO("NoteTransaction") SECTION("A response is expected and the response is valid") { J *req = NoteNewRequest("note.add"); REQUIRE(req != NULL); - noteJSONTransaction_fake.custom_fake = noteJSONTransactionValid; + _noteJSONTransaction_fake.custom_fake = _noteJSONTransactionValid; J *resp = NoteTransaction(req); - CHECK(noteJSONTransaction_fake.call_count == 1); + CHECK(_noteJSONTransaction_fake.call_count == 1); CHECK(resp != NULL); // Ensure there's no error in the response. CHECK(!NoteResponseError(resp)); @@ -122,13 +122,13 @@ SCENARIO("NoteTransaction") SECTION("A response is expected and the response has an error") { J *req = NoteNewRequest("note.add"); REQUIRE(req != NULL); - noteJSONTransaction_fake.return_val = "This is an error."; + _noteJSONTransaction_fake.return_val = "This is an error."; J *resp = NoteTransaction(req); // Ensure the mock is called at least once // Here the error causes multiple invocations by retries - CHECK(noteJSONTransaction_fake.call_count >= 1); + CHECK(_noteJSONTransaction_fake.call_count >= 1); // Ensure there's an error in the response. CHECK(resp != NULL); @@ -141,7 +141,7 @@ SCENARIO("NoteTransaction") WHEN("The transaction is successful and the response has a {bad-bin} error") { J *req = NoteNewRequest("note.add"); REQUIRE(req != NULL); - noteJSONTransaction_fake.custom_fake = [](const char *, size_t, char **response, uint32_t) -> const char * { + _noteJSONTransaction_fake.custom_fake = [](const char *, size_t, char **response, uint32_t) -> const char * { const char rsp_str[] = "{\"err\":\"{bad-bin}\"}"; *response = (char *)malloc(sizeof(rsp_str)); strncpy(*response, rsp_str, sizeof(rsp_str)); @@ -152,14 +152,14 @@ SCENARIO("NoteTransaction") // Ensure the mock is called at least once // Here the error causes multiple invocations by retries - REQUIRE(noteJSONTransaction_fake.call_count >= 1); + REQUIRE(_noteJSONTransaction_fake.call_count >= 1); THEN("An error is returned") { CHECK(resp != NULL); } THEN("The transaction is not retried") { - CHECK(noteJSONTransaction_fake.call_count == 1); + CHECK(_noteJSONTransaction_fake.call_count == 1); } JDelete(req); @@ -169,7 +169,7 @@ SCENARIO("NoteTransaction") WHEN("The transaction is successful and the response contains invalid JSON") { J *req = NoteNewRequest("note.add"); REQUIRE(req != NULL); - noteJSONTransaction_fake.custom_fake = [](const char *, size_t, char **response, uint32_t) -> const char * { + _noteJSONTransaction_fake.custom_fake = [](const char *, size_t, char **response, uint32_t) -> const char * { const char rsp_str[] = "{Looks like JSON, but won't parse}"; *response = (char *)malloc(sizeof(rsp_str)); strncpy(*response, rsp_str, sizeof(rsp_str)); @@ -180,14 +180,14 @@ SCENARIO("NoteTransaction") // Ensure the mock is called at least once // Here the error causes multiple invocations by retries - REQUIRE(noteJSONTransaction_fake.call_count >= 1); + REQUIRE(_noteJSONTransaction_fake.call_count >= 1); THEN("An error is returned") { CHECK(resp != NULL); } THEN("The transaction is retried") { - CHECK(noteJSONTransaction_fake.call_count == (1 + CARD_REQUEST_RETRIES_ALLOWED)); + CHECK(_noteJSONTransaction_fake.call_count == (1 + CARD_REQUEST_RETRIES_ALLOWED)); } JDelete(req); @@ -197,7 +197,7 @@ SCENARIO("NoteTransaction") WHEN("The transaction is successful, and contains UTF-8 encoded characters in valid JSON") { J* req = NoteNewRequest("card.time"); REQUIRE(req != NULL); - noteJSONTransaction_fake.custom_fake = [] (const char *, size_t, char **response, uint32_t) -> const char * { + _noteJSONTransaction_fake.custom_fake = [] (const char *, size_t, char **response, uint32_t) -> const char * { const char rsp_str[] = "{\"minutes\":-300,\"lat\":19.432608,\"lon\":-99.133122,\"area\":\"Ciudad de México\",\"country\":\"MX\",\"zone\":\"CDT,America/Mexico_City\",\"time\":1726006340}"; *response = (char *)malloc(sizeof(rsp_str)); strncpy(*response, rsp_str, sizeof(rsp_str)); @@ -216,7 +216,7 @@ SCENARIO("NoteTransaction") WHEN("The transaction is successful, and contains UTF-16 encoded characters in valid JSON") { J* req = NoteNewRequest("card.time"); REQUIRE(req != NULL); - noteJSONTransaction_fake.custom_fake = [] (const char *, size_t, char **response, uint32_t) -> const char * { + _noteJSONTransaction_fake.custom_fake = [] (const char *, size_t, char **response, uint32_t) -> const char * { const char16_t rsp_str[] = u"{\"minutes\":-300,\"lat\":19.432608,\"lon\":-99.133122,\"area\":\"Ciudad de México\",\"country\":\"MX\",\"zone\":\"CDT,America/Mexico_City\",\"time\":1726006340}"; *response = (char *)malloc(sizeof(rsp_str)); memcpy(*response, rsp_str, sizeof(rsp_str)); @@ -239,8 +239,8 @@ SCENARIO("NoteTransaction") SECTION("Bad CRC") { J *req = NoteNewRequest("note.add"); REQUIRE(req != NULL); - noteJSONTransaction_fake.custom_fake = noteJSONTransactionValid; - crcError_fake.return_val = true; + _noteJSONTransaction_fake.custom_fake = _noteJSONTransactionValid; + _crcError_fake.return_val = true; J *resp = NoteTransaction(req); @@ -255,7 +255,7 @@ SCENARIO("NoteTransaction") SECTION("I/O error") { J *req = NoteNewRequest("note.add"); REQUIRE(req != NULL); - noteJSONTransaction_fake.custom_fake = noteJSONTransactionIOError; + _noteJSONTransaction_fake.custom_fake = _noteJSONTransactionIOError; J *resp = NoteTransaction(req); @@ -270,9 +270,9 @@ SCENARIO("NoteTransaction") J *req = NoteNewRequest("note.add"); REQUIRE(req != NULL); - AND_GIVEN("noteJSONTransaction returns a response with a \"not " + AND_GIVEN("_noteJSONTransaction returns a response with a \"not " "supported\" error") { - noteJSONTransaction_fake.custom_fake = noteJSONTransactionNotSupportedError; + _noteJSONTransaction_fake.custom_fake = _noteJSONTransactionNotSupportedError; WHEN("NoteTransaction is called") { J *rsp = NoteTransaction(req); @@ -281,8 +281,8 @@ SCENARIO("NoteTransaction") CHECK(JContainsString(rsp, c_err, c_unsupported)); } - THEN("noteJSONTransaction is only called once (no retries)") { - CHECK(noteJSONTransaction_fake.call_count == 1); + THEN("_noteJSONTransaction is only called once (no retries)") { + CHECK(_noteJSONTransaction_fake.call_count == 1); } JDelete(rsp); @@ -303,7 +303,7 @@ SCENARIO("NoteTransaction") CHECK(NoteReset_fake.call_count == 1); // The transaction shouldn't be attempted if reset failed. - CHECK(noteJSONTransaction_fake.call_count == 0); + CHECK(_noteJSONTransaction_fake.call_count == 0); // The response should be null if reset failed. CHECK(resp == NULL); @@ -320,7 +320,7 @@ SCENARIO("NoteTransaction") // The transaction shouldn't be attempted if the request couldn't be // serialized. - CHECK(noteJSONTransaction_fake.call_count == 0); + CHECK(_noteJSONTransaction_fake.call_count == 0); // Ensure there's an error in the response. CHECK(resp != NULL); CHECK(NoteResponseError(resp)); @@ -332,11 +332,11 @@ SCENARIO("NoteTransaction") SECTION("No response is expected") { J *req = NoteNewCommand("note.add"); REQUIRE(req != NULL); - noteJSONTransaction_fake.custom_fake = noteJSONTransactionValid; + _noteJSONTransaction_fake.custom_fake = _noteJSONTransactionValid; J *resp = NoteTransaction(req); - CHECK(noteJSONTransaction_fake.call_count == 1); + CHECK(_noteJSONTransaction_fake.call_count == 1); CHECK(resp != NULL); // Ensure there's no error in the response. CHECK(!NoteResponseError(resp)); @@ -352,11 +352,11 @@ SCENARIO("NoteTransaction") SECTION("Parsing the JSON response fails") { J *req = NoteNewRequest("note.add"); REQUIRE(req != NULL); - noteJSONTransaction_fake.custom_fake = noteJSONTransactionBadJSON; + _noteJSONTransaction_fake.custom_fake = _noteJSONTransactionBadJSON; J *resp = NoteTransaction(req); - CHECK(noteJSONTransaction_fake.call_count >= 1); + CHECK(_noteJSONTransaction_fake.call_count >= 1); CHECK(resp != NULL); // Ensure there's an error in the response. CHECK(NoteResponseError(resp)); @@ -370,7 +370,7 @@ SCENARIO("NoteTransaction") J *req = NoteNewRequest("hub.set"); REQUIRE(req != NULL); JAddStringToObject(req, "product", "a.b.c:d"); - noteJSONTransaction_fake.custom_fake = noteJSONTransactionValid; + _noteJSONTransaction_fake.custom_fake = _noteJSONTransactionValid; NoteUserAgent_fake.return_val = JCreateObject(); J *resp = NoteTransaction(req); @@ -387,7 +387,7 @@ SCENARIO("NoteTransaction") REQUIRE(req != NULL); J *resp = NoteTransaction(req); - CHECK(noteJSONTransaction_fake.arg3_val == (CARD_INTER_TRANSACTION_TIMEOUT_SEC * 1000)); + CHECK(_noteJSONTransaction_fake.arg3_val == (CARD_INTER_TRANSACTION_TIMEOUT_SEC * 1000)); JDelete(req); JDelete(resp); @@ -399,7 +399,7 @@ SCENARIO("NoteTransaction") JAddIntToObject(req, "milliseconds", 9171979); J *resp = NoteTransaction(req); - CHECK(noteJSONTransaction_fake.arg3_val == 9171979); + CHECK(_noteJSONTransaction_fake.arg3_val == 9171979); JDelete(req); JDelete(resp); @@ -411,7 +411,7 @@ SCENARIO("NoteTransaction") JAddIntToObject(req, "seconds", 917); J *resp = NoteTransaction(req); - CHECK(noteJSONTransaction_fake.arg3_val == (917 * 1000)); + CHECK(_noteJSONTransaction_fake.arg3_val == (917 * 1000)); JDelete(req); JDelete(resp); @@ -424,7 +424,7 @@ SCENARIO("NoteTransaction") JAddIntToObject(req, "milliseconds", 9171979); J *resp = NoteTransaction(req); - CHECK(noteJSONTransaction_fake.arg3_val == 9171979); + CHECK(_noteJSONTransaction_fake.arg3_val == 9171979); JDelete(req); JDelete(resp); @@ -436,7 +436,7 @@ SCENARIO("NoteTransaction") JAddIntToObject(req, "milliseconds", 9171979); J *resp = NoteTransaction(req); - CHECK(noteJSONTransaction_fake.arg3_val == 9171979); + CHECK(_noteJSONTransaction_fake.arg3_val == 9171979); JDelete(req); JDelete(resp); @@ -448,7 +448,7 @@ SCENARIO("NoteTransaction") JAddIntToObject(req, "seconds", 1979); J *resp = NoteTransaction(req); - CHECK(noteJSONTransaction_fake.arg3_val == (1979 * 1000)); + CHECK(_noteJSONTransaction_fake.arg3_val == (1979 * 1000)); JDelete(req); JDelete(resp); @@ -461,7 +461,7 @@ SCENARIO("NoteTransaction") JAddIntToObject(req, "milliseconds", 9171979); J *resp = NoteTransaction(req); - CHECK(noteJSONTransaction_fake.arg3_val == 9171979); + CHECK(_noteJSONTransaction_fake.arg3_val == 9171979); JDelete(req); JDelete(resp); @@ -472,16 +472,17 @@ SCENARIO("NoteTransaction") REQUIRE(req != NULL); J *resp = NoteTransaction(req); - CHECK(noteJSONTransaction_fake.arg3_val == (90 * 1000)); + CHECK(_noteJSONTransaction_fake.arg3_val == (90 * 1000)); JDelete(req); JDelete(resp); } + RESET_FAKE(_crcError); + RESET_FAKE(_noteJSONTransaction); + RESET_FAKE(_noteTransactionStart); RESET_FAKE(NoteReset); - RESET_FAKE(noteJSONTransaction); - RESET_FAKE(noteTransactionStart); - RESET_FAKE(crcError); + RESET_FAKE(NoteUserAgent); } } diff --git a/test/src/crcAdd_test.cpp b/test/src/_crcAdd_test.cpp similarity index 82% rename from test/src/crcAdd_test.cpp rename to test/src/_crcAdd_test.cpp index 272dbb8a..0b795901 100644 --- a/test/src/crcAdd_test.cpp +++ b/test/src/_crcAdd_test.cpp @@ -1,5 +1,5 @@ /*! - * @file crcAdd_test.cpp + * @file _crcAdd_test.cpp * * Written by the Blues Inc. team. * @@ -25,7 +25,7 @@ FAKE_VALUE_FUNC(void *, NoteMalloc, size_t) namespace { -SCENARIO("crcAdd") +SCENARIO("_crcAdd") { NoteSetFnDefault(malloc, free, NULL, NULL); @@ -35,7 +35,7 @@ SCENARIO("crcAdd") SECTION("NoteMalloc fails") { NoteMalloc_fake.return_val = NULL; - CHECK(crcAdd(validReq, seqNo) == NULL); + CHECK(_crcAdd(validReq, seqNo) == NULL); } SECTION("NoteMalloc succeeds") { @@ -45,16 +45,16 @@ SCENARIO("crcAdd") char invalidJsonReq[] = "{\"req\":"; SECTION("Empty string") { - CHECK(crcAdd(emptyStringReq, seqNo) == NULL); + CHECK(_crcAdd(emptyStringReq, seqNo) == NULL); } SECTION("Invalid JSON") { - CHECK(crcAdd(invalidJsonReq, seqNo) == NULL); + CHECK(_crcAdd(invalidJsonReq, seqNo) == NULL); } SECTION("Valid JSON") { const char expectedNewJson[] = "{\"req\": \"hub.sync\",\"crc\":\"0001:DF2B9115\"}"; - char *newJson = crcAdd(validReq, seqNo); + char *newJson = _crcAdd(validReq, seqNo); REQUIRE(newJson != NULL); CHECK(strcmp(expectedNewJson, newJson) == 0); diff --git a/test/src/crcError_test.cpp b/test/src/_crcError_test.cpp similarity index 77% rename from test/src/crcError_test.cpp rename to test/src/_crcError_test.cpp index 26115b9a..7e59ac9e 100644 --- a/test/src/crcError_test.cpp +++ b/test/src/_crcError_test.cpp @@ -1,5 +1,5 @@ /*! - * @file crcError_test.cpp + * @file _crcError_test.cpp * * Written by the Blues Inc. team. * @@ -20,7 +20,7 @@ namespace { -SCENARIO("crcError") +SCENARIO("_crcError") { NoteSetFnDefault(malloc, free, NULL, NULL); @@ -29,25 +29,25 @@ SCENARIO("crcError") SECTION("Empty string") { char json[] = ""; - CHECK(!crcError(json, seqNo)); + CHECK(!_crcError(json, seqNo)); } SECTION("Invalid JSON") { char json[] = "{\"req\":"; - CHECK(!crcError(json, seqNo)); + CHECK(!_crcError(json, seqNo)); } SECTION("No CRC field") { char json[] = "{\"req\": \"hub.sync\"}"; - CHECK(!crcError(json, seqNo)); + CHECK(!_crcError(json, seqNo)); } SECTION("CRC field at unexpected position") { char json[] = "{\"crc\":\"0009:10BAC79A\",\"req\": \"hub.sync\"}"; - CHECK(!crcError(json, seqNo)); + CHECK(!_crcError(json, seqNo)); } SECTION("Valid JSON and CRC field present") { @@ -55,21 +55,21 @@ SCENARIO("crcError") SECTION("CRC doesn't match") { char json[] = "{\"req\":\"hub.sync\",\"crc\":\"0001:DEADBEEF\"}"; - CHECK(crcError(json, seqNo)); + CHECK(_crcError(json, seqNo)); } SECTION("Sequence number doesn't match") { char json[] = "{\"req\":\"hub.sync\",\"crc\":\"0009:10BAC79A\"}"; - CHECK(crcError(json, seqNo)); + CHECK(_crcError(json, seqNo)); } SECTION("Everything matches") { char json[] = "{\"req\":\"hub.sync\"}"; - char *jsonWithCrc = crcAdd(json, seqNo); + char *jsonWithCrc = _crcAdd(json, seqNo); REQUIRE(jsonWithCrc != NULL); - CHECK(!crcError(jsonWithCrc, seqNo)); + CHECK(!_crcError(jsonWithCrc, seqNo)); NoteFree(jsonWithCrc); } @@ -78,7 +78,7 @@ SCENARIO("crcError") char json[] = "{\"req\":\"hub.sync\",\"crc\":\"0001:10BAC79A\"}\r\n"; // Trailing \r\n should be ignored. - CHECK(!crcError(json, seqNo)); + CHECK(!_crcError(json, seqNo)); } } } diff --git a/test/src/i2cChunkedReceive_test.cpp b/test/src/_i2cChunkedReceive_test.cpp similarity index 79% rename from test/src/i2cChunkedReceive_test.cpp rename to test/src/_i2cChunkedReceive_test.cpp index 99346d01..d2db13ed 100644 --- a/test/src/i2cChunkedReceive_test.cpp +++ b/test/src/_i2cChunkedReceive_test.cpp @@ -1,5 +1,5 @@ /*! - * @file i2cChunkedReceive_test.cpp + * @file _i2cChunkedReceive_test.cpp * * Written by the Blues Inc. team. * @@ -21,7 +21,7 @@ #include "time_mocks.h" DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(const char *, noteI2CReceive, uint16_t, uint8_t *, uint16_t, +FAKE_VALUE_FUNC(const char *, _noteI2CReceive, uint16_t, uint8_t *, uint16_t, uint32_t *) FAKE_VALUE_FUNC(uint32_t, NoteGetMs) FAKE_VOID_FUNC(NoteDelayMs, uint32_t) @@ -29,7 +29,7 @@ FAKE_VOID_FUNC(NoteDelayMs, uint32_t) namespace { -const char *noteI2CReceiveInfinite(uint16_t, uint8_t *buf, uint16_t size, +const char *_noteI2CReceiveInfinite(uint16_t, uint8_t *buf, uint16_t size, uint32_t *available) { memset(buf, 'a', size); @@ -38,7 +38,7 @@ const char *noteI2CReceiveInfinite(uint16_t, uint8_t *buf, uint16_t size, return NULL; } -SCENARIO("i2cChunkedReceive") +SCENARIO("_i2cChunkedReceive") { NoteGetMs_fake.custom_fake = NoteGetMsIncrement; @@ -49,13 +49,13 @@ SCENARIO("i2cChunkedReceive") uint8_t buf[] = {0xAB}; uint32_t zeroSize = 0; - AND_GIVEN("noteI2CReceive reports that here are bytes available from " + AND_GIVEN("_noteI2CReceive reports that here are bytes available from " "the Notecard") { - noteI2CReceive_fake.custom_fake = noteI2CReceiveInfinite; + _noteI2CReceive_fake.custom_fake = _noteI2CReceiveInfinite; - WHEN("i2cChunkedReceive is called") { + WHEN("_i2cChunkedReceive is called") { uint32_t originalAvailable = available; - const char *err = i2cChunkedReceive(buf, &zeroSize, true, + const char *err = _i2cChunkedReceive(buf, &zeroSize, true, timeoutMs, &available); THEN("No error is returned") { @@ -67,20 +67,20 @@ SCENARIO("i2cChunkedReceive") } THEN("available is exactly the number of bytes reported " - "available by noteI2CReceive") { + "available by _noteI2CReceive") { CHECK(available == NOTE_I2C_MAX_DEFAULT); } } } } - GIVEN("noteI2CReceive returns an error") { + GIVEN("_noteI2CReceive returns an error") { uint8_t buf[] = {0xAB}; uint32_t size = sizeof(buf); - noteI2CReceive_fake.return_val = "some error"; + _noteI2CReceive_fake.return_val = "some error"; - WHEN("i2cChunkedReceive is called") { - const char *err = i2cChunkedReceive(buf, &size, true, timeoutMs, + WHEN("_i2cChunkedReceive is called") { + const char *err = _i2cChunkedReceive(buf, &size, true, timeoutMs, &available); THEN("An error is returned") { @@ -92,10 +92,10 @@ SCENARIO("i2cChunkedReceive") GIVEN("The output buffer is too small") { uint8_t buf[NOTE_I2C_MAX_DEFAULT] = {0}; uint32_t size = sizeof(buf); - noteI2CReceive_fake.custom_fake = noteI2CReceiveInfinite; + _noteI2CReceive_fake.custom_fake = _noteI2CReceiveInfinite; - WHEN("i2cChunkedReceive is called") { - const char *err = i2cChunkedReceive(buf, &size, true, timeoutMs, + WHEN("_i2cChunkedReceive is called") { + const char *err = _i2cChunkedReceive(buf, &size, true, timeoutMs, &available); THEN("No error is returned") { @@ -103,17 +103,17 @@ SCENARIO("i2cChunkedReceive") } THEN("The output size is exactly the number of bytes returned by " - "noteI2CReceive") { + "_noteI2CReceive") { CHECK(size == NOTE_I2C_MAX_DEFAULT); } THEN("available is exactly the number of bytes reported available " - "by noteI2CReceive") { + "by _noteI2CReceive") { CHECK(available == NOTE_I2C_MAX_DEFAULT); } THEN("The output buffer contains exactly the bytes returned by " - "noteI2CReceive") { + "_noteI2CReceive") { uint8_t expectedBuf[sizeof(buf)]; memset(expectedBuf, 'a', sizeof(expectedBuf)); @@ -126,9 +126,9 @@ SCENARIO("i2cChunkedReceive") uint8_t buf[NOTE_I2C_MAX_DEFAULT * 3] = {0}; uint32_t size = sizeof(buf); - noteI2CReceive_fake.custom_fake = [](uint16_t, uint8_t *buf, + _noteI2CReceive_fake.custom_fake = [](uint16_t, uint8_t *buf, uint16_t size, uint32_t *available) -> const char* { - // If noteI2CReceive is called with size 0, the caller is querying + // If _noteI2CReceive is called with size 0, the caller is querying // the Notecard for how many bytes are available. Here, we report // back that there are NOTE_I2C_MAX_DEFAULT * 2 bytes available. if (size == 0) @@ -161,8 +161,8 @@ SCENARIO("i2cChunkedReceive") size_t numBytesExpected = 10; available = 10; - WHEN("i2cChunkedReceive is called") { - const char *err = i2cChunkedReceive(buf, &size, true, timeoutMs, + WHEN("_i2cChunkedReceive is called") { + const char *err = _i2cChunkedReceive(buf, &size, true, timeoutMs, &available); THEN("No error is returned") { @@ -170,7 +170,7 @@ SCENARIO("i2cChunkedReceive") } THEN("The output size is exactly the number of bytes returned" - "by noteI2CReceive") { + "by _noteI2CReceive") { CHECK(size == numBytesExpected); } @@ -179,7 +179,7 @@ SCENARIO("i2cChunkedReceive") } THEN("The output buffer contains exactly the bytes returned by " - "noteI2CReceive") { + "_noteI2CReceive") { uint8_t expectedBuf[10]; memset(expectedBuf, 'a', sizeof(expectedBuf) - 1); expectedBuf[sizeof(expectedBuf) - 1] = '\n'; @@ -193,8 +193,8 @@ SCENARIO("i2cChunkedReceive") size_t numBytesExpected = NOTE_I2C_MAX_DEFAULT * 2; available = 0; - WHEN("i2cChunkedReceive is called") { - const char *err = i2cChunkedReceive(buf, &size, true, timeoutMs, + WHEN("_i2cChunkedReceive is called") { + const char *err = _i2cChunkedReceive(buf, &size, true, timeoutMs, &available); THEN("No error is returned") { @@ -202,7 +202,7 @@ SCENARIO("i2cChunkedReceive") } THEN("The output size is exactly the number of bytes returned" - "by noteI2CReceive") { + "by _noteI2CReceive") { CHECK(size == numBytesExpected); } @@ -211,7 +211,7 @@ SCENARIO("i2cChunkedReceive") } THEN("The output buffer contains exactly the bytes returned by " - "noteI2CReceive") { + "_noteI2CReceive") { uint8_t expectedBuf[NOTE_I2C_MAX_DEFAULT * 2]; memset(expectedBuf, 'a', sizeof(expectedBuf) - 1); expectedBuf[sizeof(expectedBuf) - 1] = '\n'; @@ -222,13 +222,13 @@ SCENARIO("i2cChunkedReceive") } } - GIVEN("End-of-packet (\\n) is received, but noteI2CReceive indicates more " + GIVEN("End-of-packet (\\n) is received, but _noteI2CReceive indicates more " "is still available to read") { uint8_t buf[NOTE_I2C_MAX_DEFAULT * 3] = {0}; uint32_t size = sizeof(buf); size_t numBytesExpected = NOTE_I2C_MAX_DEFAULT * 2; - // On the first call, noteI2CReceive reports back that + // On the first call, _noteI2CReceive reports back that // NOTE_I2C_MAX_DEFAULT are available to read. auto bytesAvailable = [](uint16_t, uint8_t *buf, uint16_t size, uint32_t *available) -> const char* { @@ -262,10 +262,10 @@ SCENARIO("i2cChunkedReceive") fullPacketButMoreAvailable, excessData }; - SET_CUSTOM_FAKE_SEQ(noteI2CReceive, recvFakeSequence, 3); + SET_CUSTOM_FAKE_SEQ(_noteI2CReceive, recvFakeSequence, 3); - WHEN("i2cChunkedReceive is called") { - const char *err = i2cChunkedReceive(buf, &size, true, timeoutMs, + WHEN("_i2cChunkedReceive is called") { + const char *err = _i2cChunkedReceive(buf, &size, true, timeoutMs, &available); THEN("No error is returned") { @@ -273,7 +273,7 @@ SCENARIO("i2cChunkedReceive") } THEN("The output size is exactly the number of bytes returned by " - "noteI2CReceive") { + "_noteI2CReceive") { CHECK(size == numBytesExpected); } @@ -282,7 +282,7 @@ SCENARIO("i2cChunkedReceive") } THEN("The output buffer contains exactly the bytes returned by " - "noteI2CReceive") { + "_noteI2CReceive") { uint8_t expectedBuf[NOTE_I2C_MAX_DEFAULT * 2]; memset(expectedBuf, 'a', NOTE_I2C_MAX_DEFAULT - 1); expectedBuf[NOTE_I2C_MAX_DEFAULT - 1] = '\n'; @@ -295,7 +295,7 @@ SCENARIO("i2cChunkedReceive") } GIVEN("There's nothing to read from the Notecard") { - noteI2CReceive_fake.custom_fake = [](uint16_t, uint8_t *buf, + _noteI2CReceive_fake.custom_fake = [](uint16_t, uint8_t *buf, uint16_t size, uint32_t *available) -> const char* { *available = 0; @@ -304,8 +304,8 @@ SCENARIO("i2cChunkedReceive") uint8_t buf[NOTE_I2C_MAX_DEFAULT] = {0}; uint32_t size = sizeof(buf); - WHEN("i2cChunkedReceive is called") { - const char *err = i2cChunkedReceive(buf, &size, true, timeoutMs, + WHEN("_i2cChunkedReceive is called") { + const char *err = _i2cChunkedReceive(buf, &size, true, timeoutMs, &available); THEN("An error is returned") { @@ -326,8 +326,8 @@ SCENARIO("i2cChunkedReceive") } AND_GIVEN("The delay parameter is false") { - WHEN("i2cChunkedReceive is called") { - i2cChunkedReceive(buf, &size, false, timeoutMs, &available); + WHEN("_i2cChunkedReceive is called") { + _i2cChunkedReceive(buf, &size, false, timeoutMs, &available); THEN("NoteDelayMs is never called") { CHECK(NoteDelayMs_fake.call_count == 0); @@ -336,8 +336,8 @@ SCENARIO("i2cChunkedReceive") } AND_GIVEN("The delay parameter is true") { - WHEN("i2cChunkedReceive is called") { - i2cChunkedReceive(buf, &size, true, timeoutMs, &available); + WHEN("_i2cChunkedReceive is called") { + _i2cChunkedReceive(buf, &size, true, timeoutMs, &available); THEN("NoteDelayMs is called") { CHECK(NoteDelayMs_fake.call_count > 0); @@ -348,7 +348,7 @@ SCENARIO("i2cChunkedReceive") GIVEN("There's initially data to read from the Notecard, but then there's " "nothing available and we never receive the \\n") { - // First, noteI2CReceive will report that NOTE_I2C_MAX_DEFAULT bytes are + // First, _noteI2CReceive will report that NOTE_I2C_MAX_DEFAULT bytes are // available to read. auto bytesAvailable = [](uint16_t, uint8_t *buf, uint16_t size, uint32_t *available) -> const char* { @@ -368,7 +368,7 @@ SCENARIO("i2cChunkedReceive") }; // Finally, any subsequent calls will just set available to 0 and do // nothing else. This will eventually lead to a timeout in - // i2cChunkedReceive because it's still waiting for the newline. + // _i2cChunkedReceive because it's still waiting for the newline. auto nothingAvailable = [](uint16_t, uint8_t *buf, uint16_t size, uint32_t *available) -> const char* { *available = 0; @@ -381,13 +381,13 @@ SCENARIO("i2cChunkedReceive") partialPacket, nothingAvailable }; - SET_CUSTOM_FAKE_SEQ(noteI2CReceive, recvFakeSequence, 3); + SET_CUSTOM_FAKE_SEQ(_noteI2CReceive, recvFakeSequence, 3); uint8_t buf[NOTE_I2C_MAX_DEFAULT] = {0}; uint32_t size = sizeof(buf); - WHEN("i2cChunkedReceive is called") { - const char *err = i2cChunkedReceive(buf, &size, true, timeoutMs, + WHEN("_i2cChunkedReceive is called") { + const char *err = _i2cChunkedReceive(buf, &size, true, timeoutMs, &available); THEN("An error is returned") { @@ -408,7 +408,7 @@ SCENARIO("i2cChunkedReceive") } } - RESET_FAKE(noteI2CReceive); + RESET_FAKE(_noteI2CReceive); RESET_FAKE(NoteGetMs); RESET_FAKE(NoteDelayMs); } diff --git a/test/src/i2cChunkedTransmit_test.cpp b/test/src/_i2cChunkedTransmit_test.cpp similarity index 59% rename from test/src/i2cChunkedTransmit_test.cpp rename to test/src/_i2cChunkedTransmit_test.cpp index e349b8dc..2fb2a044 100644 --- a/test/src/i2cChunkedTransmit_test.cpp +++ b/test/src/_i2cChunkedTransmit_test.cpp @@ -1,5 +1,5 @@ /*! - * @file i2cChunkedTransmit_test.cpp + * @file _i2cChunkedTransmit_test.cpp * * Written by the Blues Inc. team. * @@ -20,8 +20,8 @@ #include "test_static.h" DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(const char *, noteI2CTransmit, uint16_t, uint8_t *, uint16_t) -FAKE_VALUE_FUNC(bool, noteI2CReset, uint16_t) +FAKE_VALUE_FUNC(const char *, _noteI2CTransmit, uint16_t, uint8_t *, uint16_t) +FAKE_VALUE_FUNC(bool, _noteI2CReset, uint16_t) FAKE_VOID_FUNC(NoteDelayMs, uint32_t) namespace @@ -30,14 +30,14 @@ namespace uint8_t transmitBuf[CARD_REQUEST_I2C_SEGMENT_MAX_LEN * 2]; size_t transmitBufIdx = 0; -const char *noteI2CTransmitCapture(uint16_t, uint8_t *buf, uint16_t size) +const char *_noteI2CTransmitCapture(uint16_t, uint8_t *buf, uint16_t size) { memcpy(transmitBuf + transmitBufIdx, buf, size); return NULL; } -SCENARIO("i2cChunkedTransmit") +SCENARIO("_i2cChunkedTransmit") { bool delay = true; transmitBufIdx = 0; @@ -46,11 +46,11 @@ SCENARIO("i2cChunkedTransmit") uint8_t buf[] = {0xAB}; size_t size = 0; - WHEN("i2cChunkedTransmit is called") { - const char *err = i2cChunkedTransmit(buf, size, delay); + WHEN("_i2cChunkedTransmit is called") { + const char *err = _i2cChunkedTransmit(buf, size, delay); THEN("Nothing is transmitted") { - CHECK(noteI2CTransmit_fake.call_count == 0); + CHECK(_noteI2CTransmit_fake.call_count == 0); } THEN("No error is returned") { @@ -59,20 +59,20 @@ SCENARIO("i2cChunkedTransmit") } } - GIVEN("noteI2CTransmit returns an error") { + GIVEN("_noteI2CTransmit returns an error") { uint8_t buf[] = {0xAB}; size_t size = sizeof(buf); - noteI2CTransmit_fake.return_val = "some error"; + _noteI2CTransmit_fake.return_val = "some error"; - WHEN("i2cChunkedTransmit is called") { - const char *err = i2cChunkedTransmit(buf, size, delay); + WHEN("_i2cChunkedTransmit is called") { + const char *err = _i2cChunkedTransmit(buf, size, delay); THEN("An error is returned") { CHECK(err != NULL); } - THEN("noteI2CReset is called") { - CHECK(noteI2CReset_fake.call_count > 0); + THEN("_noteI2CReset is called") { + CHECK(_noteI2CReset_fake.call_count > 0); } } } @@ -81,7 +81,7 @@ SCENARIO("i2cChunkedTransmit") uint8_t *buf = NULL; size_t size = 0; const char *err = NULL; - noteI2CTransmit_fake.custom_fake = noteI2CTransmitCapture; + _noteI2CTransmit_fake.custom_fake = _noteI2CTransmitCapture; AND_GIVEN("The input buffer can be sent in one chunk") { uint8_t oneChunkBuf[NOTE_I2C_MAX_DEFAULT]; @@ -89,11 +89,11 @@ SCENARIO("i2cChunkedTransmit") size = sizeof(oneChunkBuf); memset(oneChunkBuf, 1, size); - WHEN("i2cChunkedTransmit is called") { - err = i2cChunkedTransmit(buf, size, delay); + WHEN("_i2cChunkedTransmit is called") { + err = _i2cChunkedTransmit(buf, size, delay); - THEN("The data is sent in 1 call to noteI2CTransmit") { - CHECK(noteI2CTransmit_fake.call_count == 1); + THEN("The data is sent in 1 call to _noteI2CTransmit") { + CHECK(_noteI2CTransmit_fake.call_count == 1); } } } @@ -104,11 +104,11 @@ SCENARIO("i2cChunkedTransmit") size = sizeof(multiChunkBuf); memset(multiChunkBuf, 2, size); - WHEN("i2cChunkedTransmit is called") { - err = i2cChunkedTransmit(buf, size, delay); + WHEN("_i2cChunkedTransmit is called") { + err = _i2cChunkedTransmit(buf, size, delay); - THEN("The data is sent in > 1 call to noteI2CTransmit") { - CHECK(noteI2CTransmit_fake.call_count > 1); + THEN("The data is sent in > 1 call to _noteI2CTransmit") { + CHECK(_noteI2CTransmit_fake.call_count > 1); } } } @@ -120,8 +120,8 @@ SCENARIO("i2cChunkedTransmit") size = sizeof(greaterThanSegmentBuf); memset(greaterThanSegmentBuf, 3, size); - WHEN("i2cChunkedTransmit is called") { - err = i2cChunkedTransmit(buf, size, delay); + WHEN("_i2cChunkedTransmit is called") { + err = _i2cChunkedTransmit(buf, size, delay); } } @@ -129,14 +129,14 @@ SCENARIO("i2cChunkedTransmit") CHECK(err == NULL); } - THEN("The data passed to noteI2CTransmit is exactly the same as " - "what was passed to i2cChunkedTransmit") { + THEN("The data passed to _noteI2CTransmit is exactly the same as " + "what was passed to _i2cChunkedTransmit") { memcmp(buf, transmitBuf, sizeof(buf) == 0); } } - RESET_FAKE(noteI2CTransmit); - RESET_FAKE(noteI2CReset); + RESET_FAKE(_noteI2CTransmit); + RESET_FAKE(_noteI2CReset); RESET_FAKE(NoteDelayMs); } diff --git a/test/src/i2cNoteQueryLength_test.cpp b/test/src/_i2cNoteQueryLength_test.cpp similarity index 61% rename from test/src/i2cNoteQueryLength_test.cpp rename to test/src/_i2cNoteQueryLength_test.cpp index 1384bdd7..e2820da9 100644 --- a/test/src/i2cNoteQueryLength_test.cpp +++ b/test/src/_i2cNoteQueryLength_test.cpp @@ -1,5 +1,5 @@ /*! - * @file i2cNoteQueryLength_test.cpp + * @file _i2cNoteQueryLength_test.cpp * * Written by the Blues Inc. team. * @@ -21,7 +21,7 @@ #include "time_mocks.h" DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(const char *, noteI2CReceive, uint16_t, uint8_t *, uint16_t, +FAKE_VALUE_FUNC(const char *, _noteI2CReceive, uint16_t, uint8_t *, uint16_t, uint32_t *) FAKE_VALUE_FUNC(uint32_t, NoteGetMs) @@ -31,7 +31,7 @@ namespace // Something's immediately available. // Nothing available at the start, then something becomes available. -const char * noteI2CReceiveNotAvailable(uint16_t, uint8_t *, uint16_t, +const char * _noteI2CReceiveNotAvailable(uint16_t, uint8_t *, uint16_t, uint32_t *available) { *available = 0; @@ -43,7 +43,7 @@ enum { NUM_BYTES_AVAILABLE = 3 }; -const char * noteI2CReceiveAvailable(uint16_t, uint8_t *, uint16_t, +const char * _noteI2CReceiveAvailable(uint16_t, uint8_t *, uint16_t, uint32_t *available) { *available = NUM_BYTES_AVAILABLE; @@ -51,17 +51,17 @@ const char * noteI2CReceiveAvailable(uint16_t, uint8_t *, uint16_t, return NULL; } -SCENARIO("i2cNoteQueryLength") +SCENARIO("_i2cNoteQueryLength") { NoteGetMs_fake.custom_fake = NoteGetMsIncrement; uint32_t available = 0; uint32_t timeoutMs = 5000; - GIVEN("noteI2CReceive returns an error") { - noteI2CReceive_fake.return_val = "some error"; + GIVEN("_noteI2CReceive returns an error") { + _noteI2CReceive_fake.return_val = "some error"; - WHEN("i2cNoteQueryLength is called") { - const char *err = i2cNoteQueryLength(&available, timeoutMs); + WHEN("_i2cNoteQueryLength is called") { + const char *err = _i2cNoteQueryLength(&available, timeoutMs); THEN("An error is returned") { CHECK(err != NULL); @@ -70,10 +70,10 @@ SCENARIO("i2cNoteQueryLength") } GIVEN("Bytes are never available to read") { - noteI2CReceive_fake.custom_fake = noteI2CReceiveNotAvailable; + _noteI2CReceive_fake.custom_fake = _noteI2CReceiveNotAvailable; - WHEN("i2cNoteQueryLength is called") { - const char *err = i2cNoteQueryLength(&available, timeoutMs); + WHEN("_i2cNoteQueryLength is called") { + const char *err = _i2cNoteQueryLength(&available, timeoutMs); THEN("An error is returned") { REQUIRE(err != NULL); @@ -86,34 +86,34 @@ SCENARIO("i2cNoteQueryLength") } GIVEN("Bytes are immediately available to read") { - noteI2CReceive_fake.custom_fake = noteI2CReceiveAvailable; + _noteI2CReceive_fake.custom_fake = _noteI2CReceiveAvailable; - WHEN("i2cNoteQueryLength is called") { - const char *err = i2cNoteQueryLength(&available, timeoutMs); + WHEN("_i2cNoteQueryLength is called") { + const char *err = _i2cNoteQueryLength(&available, timeoutMs); THEN("No error is returned") { CHECK(err == NULL); } THEN("The available out parameter reports the same number of " - "available bytes as noteI2CReceive reported") { + "available bytes as _noteI2CReceive reported") { CHECK(available == NUM_BYTES_AVAILABLE); } } } GIVEN("Bytes are immediately available to read") { - noteI2CReceive_fake.custom_fake = noteI2CReceiveAvailable; + _noteI2CReceive_fake.custom_fake = _noteI2CReceiveAvailable; - WHEN("i2cNoteQueryLength is called") { - const char *err = i2cNoteQueryLength(&available, timeoutMs); + WHEN("_i2cNoteQueryLength is called") { + const char *err = _i2cNoteQueryLength(&available, timeoutMs); THEN("No error is returned") { CHECK(err == NULL); } THEN("The available out parameter reports the same number of " - "available bytes as noteI2CReceive reported") { + "available bytes as _noteI2CReceive reported") { CHECK(available == NUM_BYTES_AVAILABLE); } } @@ -123,27 +123,27 @@ SCENARIO("i2cNoteQueryLength") "available") { const char * (*customI2CReciveFakes[]) (uint16_t, uint8_t *, uint16_t, uint32_t *available) = { - noteI2CReceiveNotAvailable, - noteI2CReceiveNotAvailable, - noteI2CReceiveAvailable + _noteI2CReceiveNotAvailable, + _noteI2CReceiveNotAvailable, + _noteI2CReceiveAvailable }; - SET_CUSTOM_FAKE_SEQ(noteI2CReceive, customI2CReciveFakes, 3); + SET_CUSTOM_FAKE_SEQ(_noteI2CReceive, customI2CReciveFakes, 3); - WHEN("i2cNoteQueryLength is called") { - const char *err = i2cNoteQueryLength(&available, timeoutMs); + WHEN("_i2cNoteQueryLength is called") { + const char *err = _i2cNoteQueryLength(&available, timeoutMs); THEN("No error is returned") { CHECK(err == NULL); } THEN("The available out parameter reports the same number of " - "available bytes as noteI2CReceive reported") { + "available bytes as _noteI2CReceive reported") { CHECK(available == NUM_BYTES_AVAILABLE); } } } - RESET_FAKE(noteI2CReceive); + RESET_FAKE(_noteI2CReceive); RESET_FAKE(NoteGetMs); } diff --git a/test/src/_i2cNoteReset_test.cpp b/test/src/_i2cNoteReset_test.cpp new file mode 100644 index 00000000..43a3c831 --- /dev/null +++ b/test/src/_i2cNoteReset_test.cpp @@ -0,0 +1,509 @@ +/*! + * @file _i2cNoteReset_test.cpp + * + * Written by the Blues Inc. team. + * + * Copyright (c) 2023 Blues Inc. MIT License. Use of this source code is + * governed by licenses granted by the copyright holder including that found in + * the + * LICENSE + * file. + * + */ + + + +#include +#include "fff.h" + +#include "n_lib.h" +#include "test_static.h" + +DEFINE_FFF_GLOBALS +FAKE_VOID_FUNC(_delayIO) +FAKE_VOID_FUNC(NoteDelayMs, uint32_t) +FAKE_VALUE_FUNC(uint32_t, NoteGetMs) +FAKE_VALUE_FUNC(bool, _noteI2CReset, uint16_t) +FAKE_VALUE_FUNC(const char *, _noteI2CTransmit, uint16_t, uint8_t *, uint16_t) +FAKE_VALUE_FUNC(const char *, _noteI2CReceive, uint16_t, uint8_t *, uint16_t, + uint32_t *) +FAKE_VOID_FUNC(NoteLockI2C) +FAKE_VOID_FUNC(NoteUnlockI2C) + +namespace +{ + +static uint32_t rtc_ms = 0; + +void NoteDelayMs_mock(uint32_t delayMs) +{ + rtc_ms += delayMs; +} + +uint32_t NoteGetMs_mock(void) +{ + return rtc_ms++; +} + +const char * _noteI2CReceive_CleanResetSeq0(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) +{ + if (available) { + *available = 2; + } + return nullptr; +} + +const char * _noteI2CReceive_CleanResetSeq1(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) +{ + if (available) { + *available = 0; + } + if (buffer) { + buffer[0] = '\r'; + buffer[1] = '\n'; + } + return nullptr; +} + +const char * _noteI2CReceive_AvailGTBufferMessageSeq0(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) +{ + if (available) { + *available = (ALLOC_CHUNK + 1); + } + return nullptr; +} + +const char * _noteI2CReceive_AvailGTBufferMessageSeq1(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) +{ + if (available) { + *available = 0; + } + return nullptr; +} + +const char * _noteI2CReceive_AvailGTFFFFMessageSeq0(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) +{ + if (available) { + *available = 19790917; + } + return nullptr; +} + +const char * _noteI2CReceive_AvailGTFFFFMessageSeq1(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) +{ + if (available) { + *available = 0; + } + return nullptr; +} + +const char * _noteI2CReceive_AvailGTMaxMessageSeq0(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) +{ + if (available) { + *available = NoteI2CMax(); + } + return nullptr; +} + +const char * _noteI2CReceive_AvailGTMaxMessageSeq1(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) +{ + if (available) { + *available = 0; + } + return nullptr; +} + +const char * _noteI2CReceive_ClearMessageSeq0(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) +{ + if (available) { + *available = 4; + } + return nullptr; +} + +const char * _noteI2CReceive_ClearMessageSeq1(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) +{ + if (available) { + *available = 0; + } + if (buffer) { + buffer[0] = '{'; + buffer[1] = '}'; + buffer[2] = '\r'; + buffer[3] = '\n'; + } + return nullptr; +} + +const char * _noteI2CReceive_ClearMessageSeq2(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) +{ + if (available) { + *available = 2; + } + return nullptr; +} + +const char * _noteI2CReceive_ClearMessageSeq3(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) +{ + if (available) { + *available = 0; + } + if (buffer) { + buffer[0] = '\r'; + buffer[1] = '\n'; + } + return nullptr; +} + +SCENARIO("_i2cNoteReset") +{ + //TODO: Refactor GIVEN/WHEN/THEN block grouping and names + WHEN("Invoked") { + NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; + NoteGetMs_fake.custom_fake = NoteGetMs_mock; + _i2cNoteReset(); + + THEN("Delay for `CARD_REQUEST_I2C_SEGMENT_DELAY_MS`") { + CHECK(NoteDelayMs_fake.call_count > 0); + CHECK(NoteDelayMs_fake.arg0_history[0] == CARD_REQUEST_I2C_SEGMENT_DELAY_MS); + } + + THEN("The I2C mutex will be taken") { + CHECK(NoteLockI2C_fake.call_count > 0); + } + + THEN("`_noteI2CReset()` is called") { + CHECK(_noteI2CReset_fake.call_count > 0); + } + AND_THEN("The first parameter is the Notecard address") { + CHECK(_noteI2CReset_fake.arg0_history[0] == NoteI2CAddress()); + } + } + + GIVEN("`_noteI2CReset()` is called") { + NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; + NoteGetMs_fake.custom_fake = NoteGetMs_mock; + + WHEN("`_noteI2CReset()` fails") { + _noteI2CReset_fake.return_val = false; + const bool success = _i2cNoteReset(); + + THEN("The I2C mutex will be released") { + CHECK(NoteUnlockI2C_fake.call_count == NoteLockI2C_fake.call_count); + } + AND_THEN("`_i2cNoteReset()` fails") { + CHECK(success == false); + } + + THEN("Exit immediately without retrying") { + CHECK(_noteI2CReset_fake.call_count == 1); + } + } + + WHEN("`_noteI2CReset()` succeeds") { + _noteI2CReset_fake.return_val = true; + _i2cNoteReset(); + + THEN("Then the I/O delay is respected") { + CHECK(_delayIO_fake.call_count > 0); + } + } + } + + GIVEN("`_noteI2CReset()` succeeds") { + NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; + NoteGetMs_fake.custom_fake = NoteGetMs_mock; + _noteI2CReset_fake.return_val = true; + const bool success = _i2cNoteReset(); + + THEN("`_noteI2CTransmit()` is called") { + CHECK(_noteI2CTransmit_fake.call_count > 0); + } + THEN("The first parameter is the Notecard address") { + CHECK(_noteI2CTransmit_fake.arg0_history[0] == NoteI2CAddress()); + } + THEN("The second parameter is a string containing a single newline") { + CHECK(_noteI2CTransmit_fake.arg1_history[0][0] == '\n'); + } + THEN("The third parameter is the string length of 1") { + CHECK(_noteI2CTransmit_fake.arg2_history[0] == sizeof(char)); + } + } + + GIVEN("`_noteI2CTransmit()` is called") { + NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; + NoteGetMs_fake.custom_fake = NoteGetMs_mock; + _noteI2CReset_fake.return_val = true; + + WHEN("`_noteI2CTransmit()` fails") { + _noteI2CTransmit_fake.return_val = "most likely a NACK has occurred"; + const bool success = _i2cNoteReset(); + + THEN("Delay for `CARD_REQUEST_I2C_NACK_WAIT_MS` milliseconds") { + CHECK(NoteDelayMs_fake.call_count > 1); + CHECK(NoteDelayMs_fake.arg0_history[1] == CARD_REQUEST_I2C_NACK_WAIT_MS); + } + + THEN("`_noteI2CReceive()` is not called") { + CHECK(_noteI2CReceive_fake.call_count == 0); + } + + THEN("`_noteI2CTransmit()` will retry `CARD_RESET_SYNC_RETRIES` times") { + CHECK(_noteI2CTransmit_fake.call_count == CARD_RESET_SYNC_RETRIES); + } + + THEN("The I2C mutex will be released") { + CHECK(NoteUnlockI2C_fake.call_count == NoteLockI2C_fake.call_count); + } + AND_THEN("`_i2cNoteReset()` fails") { + CHECK(success == false); + } + } + + WHEN("`_noteI2CTransmit()` succeeds") { + _noteI2CTransmit_fake.return_val = nullptr; + const bool success = _i2cNoteReset(); + + THEN("Delay for `CARD_REQUEST_I2C_SEGMENT_DELAY_MS`") { + CHECK(NoteDelayMs_fake.call_count > 1); + CHECK(NoteDelayMs_fake.arg0_history[1] == CARD_REQUEST_I2C_SEGMENT_DELAY_MS); + } + + THEN("`_noteI2CReceive()` is called to query the response") { + CHECK(_noteI2CReceive_fake.call_count > 0); + } + AND_THEN("The first parameter is the Notecard address") { + CHECK(_noteI2CReceive_fake.arg0_history[0] == NoteI2CAddress()); + } + AND_THEN("The second parameter is a non-NULL static buffer used to drain bytes") { + CHECK(_noteI2CReceive_fake.arg1_history[0] != nullptr); + } + AND_THEN("The third parameter is zero to indicate a query request") { + CHECK(_noteI2CReceive_fake.arg2_history[0] == 0); + } + AND_THEN("The fourth parameter is a non-NULL unsigned integer pointer used for the query response") { + CHECK(_noteI2CReceive_fake.arg3_history[0] != nullptr); + } + } + } + + GIVEN("`_noteI2CReceive()` is called") { + NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; + NoteGetMs_fake.custom_fake = NoteGetMs_mock; + _noteI2CReset_fake.return_val = true; + _noteI2CTransmit_fake.return_val = nullptr; + + WHEN("`_noteI2CReceive()` fails") { + _noteI2CReceive_fake.return_val = "the Notecard ESP commonly generates protocol errors"; + const bool success = _i2cNoteReset(); + + THEN("Delay for `CARD_REQUEST_I2C_SEGMENT_DELAY_MS`") { + CHECK(NoteDelayMs_fake.call_count > 2); + CHECK(NoteDelayMs_fake.arg0_history[2] == CARD_REQUEST_I2C_SEGMENT_DELAY_MS); + } + AND_THEN("Will retry `_noteI2CReceive()`") { + CHECK(_noteI2CReceive_fake.call_count > CARD_RESET_SYNC_RETRIES); + } + + THEN("The I2C mutex will be released") { + CHECK(NoteUnlockI2C_fake.call_count == NoteLockI2C_fake.call_count); + } + AND_THEN("`_i2cNoteReset()` fails") { + CHECK(success == false); + } + } + WHEN("`_noteI2CReceive()` succeeds") { + const char * (*ClearMessageSeq[])(uint16_t, uint8_t *, uint16_t, uint32_t *) = { + _noteI2CReceive_ClearMessageSeq0, + _noteI2CReceive_ClearMessageSeq1, + _noteI2CReceive_ClearMessageSeq2, + _noteI2CReceive_ClearMessageSeq3, + }; + SET_CUSTOM_FAKE_SEQ(_noteI2CReceive, ClearMessageSeq, 4); + const bool success = _i2cNoteReset(); + + THEN("Delay for `CARD_REQUEST_I2C_CHUNK_DELAY_MS`") { + CHECK(NoteDelayMs_fake.call_count > 2); + CHECK(NoteDelayMs_fake.arg0_history[2] == CARD_REQUEST_I2C_CHUNK_DELAY_MS); + } + + THEN("`_noteI2CReceive()` is called to receive the response") { + CHECK(_noteI2CReceive_fake.call_count > 1); + } + AND_THEN("The first parameter is the Notecard address") { + CHECK(_noteI2CReceive_fake.arg0_val == NoteI2CAddress()); + } + AND_THEN("The second parameter is a non-NULL static buffer used to drain bytes") { + CHECK(_noteI2CReceive_fake.arg1_val != nullptr); + } + AND_THEN("The third parameter is populated with the available value from the previous query") { + REQUIRE(_noteI2CReceive_fake.call_count >= 4); + CHECK(_noteI2CReceive_fake.arg2_history[0] == 0); + CHECK(_noteI2CReceive_fake.arg2_history[1] == 4); + CHECK(_noteI2CReceive_fake.arg2_history[2] == 0); + CHECK(_noteI2CReceive_fake.arg2_history[3] == 2); + } + AND_THEN("The fourth parameter is a non-NULL unsigned integer pointer used for the query response") { + CHECK(_noteI2CReceive_fake.arg3_val != nullptr); + } + } + } + + GIVEN("`_noteI2CReceive()` succeeds") { + NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; + NoteGetMs_fake.custom_fake = NoteGetMs_mock; + _noteI2CReset_fake.return_val = true; + _noteI2CTransmit_fake.return_val = nullptr; + + WHEN("`_noteI2CReceive()` available returns a value greater than that which can be contained by uint16_t") { + const char * (*AvailGTFFFFMessageSeq[])(uint16_t, uint8_t *, uint16_t, uint32_t *) = { + _noteI2CReceive_AvailGTFFFFMessageSeq0, + _noteI2CReceive_AvailGTFFFFMessageSeq1, + }; + SET_CUSTOM_FAKE_SEQ(_noteI2CReceive, AvailGTFFFFMessageSeq, 2); + const bool success = _i2cNoteReset(); + + THEN("The third parameter is populated with the available value from the previous query") { + REQUIRE(_noteI2CReceive_fake.call_count >= 2); + CHECK(_noteI2CReceive_fake.arg2_history[0] == 0); + CHECK(_noteI2CReceive_fake.arg2_history[1] == NoteI2CMax()); + } + } + + WHEN("`_noteI2CReceive()` available returns a value greater than the buffer size") { + const char * (*AvailGTBufferMessageSeq[])(uint16_t, uint8_t *, uint16_t, uint32_t *) = { + _noteI2CReceive_AvailGTBufferMessageSeq0, + _noteI2CReceive_AvailGTBufferMessageSeq1, + }; + SET_CUSTOM_FAKE_SEQ(_noteI2CReceive, AvailGTBufferMessageSeq, 2); + const bool success = _i2cNoteReset(); + + THEN("The third parameter is populated with a value capped at the buffer size") { + REQUIRE(_noteI2CReceive_fake.call_count >= 2); + CHECK(_noteI2CReceive_fake.arg2_val <= ALLOC_CHUNK); + } + } + + WHEN("`_noteI2CReceive()` available returns a value greater than the maximum size allowed by the Serial-over-I2C protocol") { + const char * (*AvailGTMaxMessageSeq[])(uint16_t, uint8_t *, uint16_t, uint32_t *) = { + _noteI2CReceive_AvailGTMaxMessageSeq0, + _noteI2CReceive_AvailGTMaxMessageSeq1, + }; + SET_CUSTOM_FAKE_SEQ(_noteI2CReceive, AvailGTMaxMessageSeq, 2); + const bool success = _i2cNoteReset(); + + THEN("The third parameter is populated with a value capped at NoteI2CMax()") { + REQUIRE(_noteI2CReceive_fake.call_count >= 2); + CHECK(_noteI2CReceive_fake.arg2_val <= NoteI2CMax()); + } + } + } + + GIVEN("`_noteI2CReceive()` will drain the Notecard I2C interface for `CARD_RESET_DRAIN_MS`") { + NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; + NoteGetMs_fake.custom_fake = NoteGetMs_mock; + _noteI2CReset_fake.return_val = true; + _noteI2CTransmit_fake.return_val = nullptr; + + WHEN("`_noteI2CReceive()` does not fail") { + rtc_ms = 0; + _noteI2CReceive_fake.return_val = nullptr; + const bool success = _i2cNoteReset(); + + THEN("`_noteI2CReceive()` will be called for `CARD_RESET_DRAIN_MS`") { + // Must account for both the priming delay and the post transmit delay + CHECK(rtc_ms >= CARD_RESET_DRAIN_MS + (CARD_REQUEST_I2C_SEGMENT_DELAY_MS * 2)); + } + } + + WHEN("`_noteI2CReceive()` receives any char that is neither \\r nor \\n") { + const char * (*ClearMessageSeq[])(uint16_t, uint8_t *, uint16_t, uint32_t *) = { + _noteI2CReceive_ClearMessageSeq0, + _noteI2CReceive_ClearMessageSeq1, + _noteI2CReceive_ClearMessageSeq2, + _noteI2CReceive_ClearMessageSeq3, + }; + SET_CUSTOM_FAKE_SEQ(_noteI2CReceive, ClearMessageSeq, 4); + const bool success = _i2cNoteReset(); + + THEN("`_i2cNoteReset` fails") { + CHECK(success == false); + } + } + + WHEN("`_noteI2CReceive()` receives nothing") { + _noteI2CReceive_fake.return_val = nullptr; + const bool success = _i2cNoteReset(); + + THEN("`_i2cNoteReset` fails") { + CHECK(success == false); + } + THEN("`_noteI2CReset()` is called") { + CHECK(_noteI2CReset_fake.call_count > 1); + } + } + + AND_GIVEN("`_noteI2CReceive()` receives nothing") { + _noteI2CReceive_fake.return_val = nullptr; + + WHEN("`_noteI2CReset()` fails") { + bool retSeq[] = { true, false }; + SET_RETURN_SEQ(_noteI2CReset, retSeq, 2); + const bool success = _i2cNoteReset(); + THEN("The I2C mutex will be released") { + CHECK(NoteUnlockI2C_fake.call_count == NoteLockI2C_fake.call_count); + } + THEN("Exit immediately without retrying") { + CHECK(_noteI2CReset_fake.call_count == 2); + } + } + + WHEN("`_noteI2CReset()` succeeds") { + _i2cNoteReset(); + + THEN("Then the I/O delay is respected") { + // 1 + pre loop + // 10 retries (simple _noteI2CReceive_fake returns zero --> nothing was found 10x) + CHECK(_delayIO_fake.call_count > 10); + } + } + } + + WHEN("`_noteI2CReceive()` receives only `\\r` and/or `\\n`") { + const char * (*CleanResetSeq[])(uint16_t, uint8_t *, uint16_t, uint32_t *) = { + _noteI2CReceive_CleanResetSeq0, + _noteI2CReceive_CleanResetSeq1, + }; + SET_CUSTOM_FAKE_SEQ(_noteI2CReceive, CleanResetSeq, 2); + const bool success = _i2cNoteReset(); + + THEN("Exit retry loop immediately") { + CHECK(_noteI2CTransmit_fake.call_count == 1); + } + + THEN("`_i2cNoteReset` succeeds") { + CHECK(success); + } + AND_THEN("The I2C mutex will be released") { + CHECK(NoteUnlockI2C_fake.call_count == NoteLockI2C_fake.call_count); + } + } + } + + // Sanity check against stray return paths + CHECK(NoteLockI2C_fake.call_count == 1); + CHECK(NoteUnlockI2C_fake.call_count == 1); + + RESET_FAKE(_delayIO); + RESET_FAKE(NoteDelayMs); + RESET_FAKE(NoteGetMs); + RESET_FAKE(_noteI2CReset); + RESET_FAKE(_noteI2CTransmit); + RESET_FAKE(_noteI2CReceive); + RESET_FAKE(NoteLockI2C); + RESET_FAKE(NoteUnlockI2C); +} + +} + + diff --git a/test/src/i2cNoteTransaction_test.cpp b/test/src/_i2cNoteTransaction_test.cpp similarity index 69% rename from test/src/i2cNoteTransaction_test.cpp rename to test/src/_i2cNoteTransaction_test.cpp index b86b8ba2..4783affb 100644 --- a/test/src/i2cNoteTransaction_test.cpp +++ b/test/src/_i2cNoteTransaction_test.cpp @@ -1,5 +1,5 @@ /*! - * @file i2cNoteTransaction_test.cpp + * @file _i2cNoteTransaction_test.cpp * * Written by the Blues Inc. team. * @@ -23,15 +23,15 @@ DEFINE_FFF_GLOBALS FAKE_VALUE_FUNC(void *, NoteMalloc, size_t) FAKE_VOID_FUNC(NoteLockI2C) FAKE_VOID_FUNC(NoteUnlockI2C) -FAKE_VALUE_FUNC(const char *, i2cChunkedTransmit, uint8_t *, uint32_t, bool) -FAKE_VALUE_FUNC(const char *, i2cNoteQueryLength, uint32_t *, uint32_t) -FAKE_VALUE_FUNC(const char *, i2cChunkedReceive, uint8_t *, uint32_t *, bool, +FAKE_VALUE_FUNC(const char *, _i2cChunkedTransmit, uint8_t *, uint32_t, bool) +FAKE_VALUE_FUNC(const char *, _i2cNoteQueryLength, uint32_t *, uint32_t) +FAKE_VALUE_FUNC(const char *, _i2cChunkedReceive, uint8_t *, uint32_t *, bool, uint32_t, uint32_t *) namespace { -SCENARIO("i2cNoteTransaction") +SCENARIO("_i2cNoteTransaction") { NoteSetFnDefault(NULL, free, NULL, NULL); @@ -43,14 +43,14 @@ SCENARIO("i2cNoteTransaction") NoteMalloc_fake.custom_fake = malloc; uint32_t timeoutMs = CARD_INTER_TRANSACTION_TIMEOUT_SEC; - GIVEN("i2cChunkedTransmit returns an error") { - i2cChunkedTransmit_fake.return_val = "some error"; + GIVEN("_i2cChunkedTransmit returns an error") { + _i2cChunkedTransmit_fake.return_val = "some error"; - WHEN("i2cNoteTransaction is called") { - err = i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_i2cNoteTransaction is called") { + err = _i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); THEN("An error is returned") { - REQUIRE(i2cChunkedTransmit_fake.call_count > 0); + REQUIRE(_i2cChunkedTransmit_fake.call_count > 0); CHECK(err != NULL); } } @@ -58,8 +58,8 @@ SCENARIO("i2cNoteTransaction") GIVEN("The response parameter is NULL so no Notecard response is " "expected") { - WHEN("i2cNoteTransaction is called") { - err = i2cNoteTransaction(req, strlen(req), NULL, timeoutMs); + WHEN("_i2cNoteTransaction is called") { + err = _i2cNoteTransaction(req, strlen(req), NULL, timeoutMs); THEN("No error is returned") { CHECK(err == NULL); @@ -67,22 +67,22 @@ SCENARIO("i2cNoteTransaction") } } - GIVEN("i2cNoteQueryLength returns an error") { - i2cNoteQueryLength_fake.return_val = "some error"; + GIVEN("_i2cNoteQueryLength returns an error") { + _i2cNoteQueryLength_fake.return_val = "some error"; - WHEN("i2cNoteTransaction is called") { - err = i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_i2cNoteTransaction is called") { + err = _i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); THEN("An error is returned") { - REQUIRE(i2cNoteQueryLength_fake.call_count > 0); + REQUIRE(_i2cNoteQueryLength_fake.call_count > 0); CHECK(err != NULL); } } } - GIVEN("i2cNoteQueryLength reports bytes are available to read from the" + GIVEN("_i2cNoteQueryLength reports bytes are available to read from the" " Notecard") { - i2cNoteQueryLength_fake.custom_fake = [](uint32_t * available, + _i2cNoteQueryLength_fake.custom_fake = [](uint32_t * available, uint32_t) -> const char* { *available = ALLOC_CHUNK; @@ -93,8 +93,8 @@ SCENARIO("i2cNoteTransaction") NoteMalloc_fake.custom_fake = NULL; NoteMalloc_fake.return_val = NULL; - WHEN("i2cNoteTransaction is called") { - err = i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_i2cNoteTransaction is called") { + err = _i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); THEN("An error is returned") { REQUIRE(NoteMalloc_fake.call_count > 0); @@ -103,14 +103,14 @@ SCENARIO("i2cNoteTransaction") } } - AND_GIVEN("i2cChunkedReceive returns an error") { - i2cChunkedReceive_fake.return_val = "some error"; + AND_GIVEN("_i2cChunkedReceive returns an error") { + _i2cChunkedReceive_fake.return_val = "some error"; - WHEN("i2cNoteTransaction is called") { - err = i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_i2cNoteTransaction is called") { + err = _i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); THEN("An error is returned") { - REQUIRE(i2cChunkedReceive_fake.call_count > 0); + REQUIRE(_i2cChunkedReceive_fake.call_count > 0); CHECK(err != NULL); } } @@ -119,8 +119,8 @@ SCENARIO("i2cNoteTransaction") AND_GIVEN("A single chunk is required to read the full response from " "the Notecard") { // Write out the number of bytes reported available on the prior - // call to i2cNoteQueryLength and report no more bytes available. - i2cChunkedReceive_fake.custom_fake = [](uint8_t *buf, uint32_t *size, bool, uint32_t, + // call to _i2cNoteQueryLength and report no more bytes available. + _i2cChunkedReceive_fake.custom_fake = [](uint8_t *buf, uint32_t *size, bool, uint32_t, uint32_t *available) -> const char* { memset(buf, 'a', *available - 1); buf[*available - 1] = '\n'; @@ -130,20 +130,20 @@ SCENARIO("i2cNoteTransaction") return NULL; }; - WHEN("i2cNoteTransaction is called") { - err = i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_i2cNoteTransaction is called") { + err = _i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); - // If i2cChunkedReceive is called more than once, the response + // If _i2cChunkedReceive is called more than once, the response // took more than one chunk, and this test is no longer testing // what it intends to. - REQUIRE(i2cChunkedReceive_fake.call_count == 1); + REQUIRE(_i2cChunkedReceive_fake.call_count == 1); THEN("No error is returned") { CHECK(err == NULL); } THEN("The response buffer contains exactly what was returned by" - " i2cChunkedReceive") { + " _i2cChunkedReceive") { char expectedRsp[ALLOC_CHUNK + 1]; size_t expectedSize = sizeof(expectedRsp); memset(expectedRsp, 'a', expectedSize - 2); @@ -159,7 +159,7 @@ SCENARIO("i2cNoteTransaction") AND_GIVEN("Multiple chunks are required to read the full response from " "the Notecard") { - // On the first call to i2cChunkedReceive, a string of a's is + // On the first call to _i2cChunkedReceive, a string of a's is // written to the output buffer, and the Notecard reports that there // are still ALLOC_CHUNK bytes to read. auto firstChunk = [](uint8_t *buf, uint32_t *size, bool, uint32_t, @@ -187,23 +187,23 @@ SCENARIO("i2cNoteTransaction") firstChunk, secondChunk }; - SET_CUSTOM_FAKE_SEQ(i2cChunkedReceive, recvFakeSequence, 2); + SET_CUSTOM_FAKE_SEQ(_i2cChunkedReceive, recvFakeSequence, 2); - WHEN("i2cNoteTransaction is called") { - err = i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_i2cNoteTransaction is called") { + err = _i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); - // Ensure that i2cChunkedReceive was actually called more than + // Ensure that _i2cChunkedReceive was actually called more than // once. If it isn't, then something has gone awry and this test // is no longer testing what it intends to, or there's been a // regression/bug. - REQUIRE(i2cChunkedReceive_fake.call_count > 1); + REQUIRE(_i2cChunkedReceive_fake.call_count > 1); THEN("No error is returned") { CHECK(err == NULL); } THEN("The response buffer contains exactly what was returned by" - " i2cChunkedReceive") { + " _i2cChunkedReceive") { char expectedRsp[ALLOC_CHUNK * 2 + 1]; size_t expectedSize = sizeof(expectedRsp); memset(expectedRsp, 'a', expectedSize - 2); @@ -233,8 +233,8 @@ SCENARIO("i2cNoteTransaction") }; SET_CUSTOM_FAKE_SEQ(NoteMalloc, mallocFakeSequence, 2); - WHEN("i2cNoteTransaction is called") { - err = i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_i2cNoteTransaction is called") { + err = _i2cNoteTransaction(req, strlen(req), &rsp, timeoutMs); THEN("An error is returned") { CHECK(err != NULL); @@ -253,9 +253,9 @@ SCENARIO("i2cNoteTransaction") RESET_FAKE(NoteMalloc); RESET_FAKE(NoteLockI2C); RESET_FAKE(NoteUnlockI2C); - RESET_FAKE(i2cChunkedTransmit); - RESET_FAKE(i2cNoteQueryLength); - RESET_FAKE(i2cChunkedReceive); + RESET_FAKE(_i2cChunkedTransmit); + RESET_FAKE(_i2cNoteQueryLength); + RESET_FAKE(_i2cChunkedReceive); } } diff --git a/test/src/Jtolower_test.cpp b/test/src/_j_tolower_test.cpp similarity index 80% rename from test/src/Jtolower_test.cpp rename to test/src/_j_tolower_test.cpp index 6707422f..b1a78107 100644 --- a/test/src/Jtolower_test.cpp +++ b/test/src/_j_tolower_test.cpp @@ -1,5 +1,5 @@ /*! - * @file Jtolower_test.cpp + * @file _j_tolower_test.cpp * * Written by the Blues Inc. team. * @@ -21,16 +21,16 @@ namespace { -SCENARIO("Jtolower") +SCENARIO("_j_tolower") { const char alphabetUpper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const char alphabetLower[] = "abcdefghijklmnopqrstuvwxyz"; char result[sizeof(alphabetUpper)] = {0}; GIVEN("The uppercase characters of the alphabet") { - WHEN("Jtolower is called on each letter") { + WHEN("_j_tolower is called on each letter") { for (size_t i = 0; i < sizeof(alphabetUpper); ++i) { - result[i] = Jtolower(alphabetUpper[i]); + result[i] = _j_tolower(alphabetUpper[i]); } THEN("The corresponding lowercase letter is returned") { @@ -42,9 +42,9 @@ SCENARIO("Jtolower") } GIVEN("The lowercase characters of the alphabet") { - WHEN("Jtolower is called on each letter") { + WHEN("_j_tolower is called on each letter") { for (size_t i = 0; i < sizeof(alphabetLower); ++i) { - result[i] = Jtolower(alphabetLower[i]); + result[i] = _j_tolower(alphabetLower[i]); } THEN("The same lowercase letter is returned") { @@ -58,8 +58,8 @@ SCENARIO("Jtolower") GIVEN("A non-letter char") { char invalid = '.'; - WHEN("Jtolower is called on that char") { - char invalidResult = Jtolower(invalid); + WHEN("_j_tolower is called on that char") { + char invalidResult = _j_tolower(invalid); THEN("The non-letter char is returned") { CHECK(invalidResult == invalid); diff --git a/test/src/serialChunkedReceive_test.cpp b/test/src/_serialChunkedReceive_test.cpp similarity index 75% rename from test/src/serialChunkedReceive_test.cpp rename to test/src/_serialChunkedReceive_test.cpp index 5b92b9fb..62664a9a 100644 --- a/test/src/serialChunkedReceive_test.cpp +++ b/test/src/_serialChunkedReceive_test.cpp @@ -1,5 +1,5 @@ /*! - * @file serialChunkedReceive_test.cpp + * @file _serialChunkedReceive_test.cpp * * Written by the Blues Inc. team. * @@ -20,8 +20,8 @@ #include "n_lib.h" DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(bool, noteSerialAvailable) -FAKE_VALUE_FUNC(char, noteSerialReceive) +FAKE_VALUE_FUNC(bool, _noteSerialAvailable) +FAKE_VALUE_FUNC(char, _noteSerialReceive) FAKE_VALUE_FUNC(uint32_t, NoteGetMs) FAKE_VOID_FUNC(NoteDelayMs, uint32_t) @@ -34,12 +34,12 @@ char *populateRecvBuf(size_t size) char *buf = (char *)malloc(size); memset(buf, 'a', size - 1); buf[size - 1] = '\n'; - SET_RETURN_SEQ(noteSerialReceive, buf, size); + SET_RETURN_SEQ(_noteSerialReceive, buf, size); return buf; } -SCENARIO("serialChunkedReceive") +SCENARIO("_serialChunkedReceive") { NoteSetFnDefault(malloc, free, NULL, NULL); @@ -55,17 +55,17 @@ SCENARIO("serialChunkedReceive") uint32_t size = sizeof(buf); bool delay = false; const uint32_t timeoutMs = 3000; - // 37 is not significant. serialChunkedReceive will return either a 1 or 0 + // 37 is not significant. _serialChunkedReceive will return either a 1 or 0 // in the "available" parameter. 37 is simply "not 1 or 0" -- that means we // can validate that available is changed to the correct value where // appropriate. uint32_t available = 37; - GIVEN("noteSerialAvailable indicates nothing is available to read") { - noteSerialAvailable_fake.return_val = false; + GIVEN("_noteSerialAvailable indicates nothing is available to read") { + _noteSerialAvailable_fake.return_val = false; - WHEN("serialChunkedReceive is called") { - const char *err = serialChunkedReceive(buf, &size, delay, timeoutMs, + WHEN("_serialChunkedReceive is called") { + const char *err = _serialChunkedReceive(buf, &size, delay, timeoutMs, &available); THEN("An error is returned") { @@ -83,11 +83,11 @@ SCENARIO("serialChunkedReceive") } GIVEN("The Notecard has 1 fewer bytes than the size of the output buffer") { - noteSerialAvailable_fake.return_val = true; + _noteSerialAvailable_fake.return_val = true; char *recvBuf = populateRecvBuf(sizeof(buf) - 1); - WHEN("serialChunkedReceive is called") { - const char *err = serialChunkedReceive(buf, &size, delay, timeoutMs, + WHEN("_serialChunkedReceive is called") { + const char *err = _serialChunkedReceive(buf, &size, delay, timeoutMs, &available); THEN("No error is returned") { @@ -95,12 +95,12 @@ SCENARIO("serialChunkedReceive") } THEN("The returned size is exactly the number of bytes received " - "with noteSerialReceive") { + "with _noteSerialReceive") { CHECK(size == sizeof(buf) - 1); } THEN("The output buffer has exactly the bytes returned by " - "noteSerialReceive") { + "_noteSerialReceive") { CHECK(memcmp(buf, recvBuf, size) == 0); } @@ -114,11 +114,11 @@ SCENARIO("serialChunkedReceive") GIVEN("The Notecard has exactly the same number of bytes as the output " "buffer") { - noteSerialAvailable_fake.return_val = true; + _noteSerialAvailable_fake.return_val = true; char *recvBuf = populateRecvBuf(sizeof(buf)); - WHEN("serialChunkedReceive is called") { - const char *err = serialChunkedReceive(buf, &size, delay, timeoutMs, + WHEN("_serialChunkedReceive is called") { + const char *err = _serialChunkedReceive(buf, &size, delay, timeoutMs, &available); THEN("No error is returned") { @@ -126,12 +126,12 @@ SCENARIO("serialChunkedReceive") } THEN("The returned size is exactly the number of bytes received " - "with noteSerialReceive") { + "with _noteSerialReceive") { CHECK(size == sizeof(buf)); } THEN("The output buffer has exactly the bytes returned by " - "noteSerialReceive") { + "_noteSerialReceive") { CHECK(memcmp(buf, recvBuf, size) == 0); } @@ -144,11 +144,11 @@ SCENARIO("serialChunkedReceive") } GIVEN("The Notecard has 3 more bytes than the size of the output buffer") { - noteSerialAvailable_fake.return_val = true; + _noteSerialAvailable_fake.return_val = true; char *recvBuf = populateRecvBuf(sizeof(buf) + 3); - WHEN("serialChunkedReceive is called") { - const char *err = serialChunkedReceive(buf, &size, delay, timeoutMs, + WHEN("_serialChunkedReceive is called") { + const char *err = _serialChunkedReceive(buf, &size, delay, timeoutMs, &available); THEN("No error is returned") { @@ -156,12 +156,12 @@ SCENARIO("serialChunkedReceive") } THEN("The returned size is exactly the number of bytes received " - "with noteSerialReceive") { + "with _noteSerialReceive") { CHECK(size == sizeof(buf)); } THEN("The output buffer has exactly the bytes returned by " - "noteSerialReceive") { + "_noteSerialReceive") { CHECK(memcmp(buf, recvBuf, size) == 0); } @@ -174,10 +174,10 @@ SCENARIO("serialChunkedReceive") } GIVEN("A 0-length output buffer is provided") { - WHEN("serialChunkedReceive is called") { + WHEN("_serialChunkedReceive is called") { uint32_t originalAvailable = available; uint32_t zeroSize = 0; - const char *err = serialChunkedReceive(buf, &zeroSize, delay, + const char *err = _serialChunkedReceive(buf, &zeroSize, delay, timeoutMs, &available); THEN("No error is returned") { @@ -200,12 +200,12 @@ SCENARIO("serialChunkedReceive") memset(serialAvailableReturnVals, true, numAvailableBytes); size_t numAvailRetVals = sizeof(serialAvailableReturnVals) / sizeof(*serialAvailableReturnVals); - SET_RETURN_SEQ(noteSerialAvailable, serialAvailableReturnVals, + SET_RETURN_SEQ(_noteSerialAvailable, serialAvailableReturnVals, numAvailRetVals); char *recvBuf = populateRecvBuf(sizeof(buf)); - WHEN("serialChunkedReceive is called") { - const char *err = serialChunkedReceive(buf, &size, delay, timeoutMs, + WHEN("_serialChunkedReceive is called") { + const char *err = _serialChunkedReceive(buf, &size, delay, timeoutMs, &available); THEN("An error is returned") { @@ -235,12 +235,12 @@ SCENARIO("serialChunkedReceive") size_t numAvailRetVals = sizeof(serialAvailableReturnVals) / sizeof(*serialAvailableReturnVals); size_t numAvailableBytes = 4; - SET_RETURN_SEQ(noteSerialAvailable, serialAvailableReturnVals, + SET_RETURN_SEQ(_noteSerialAvailable, serialAvailableReturnVals, numAvailRetVals); char *recvBuf = populateRecvBuf(numAvailableBytes); - WHEN("serialChunkedReceive is called") { - const char *err = serialChunkedReceive(buf, &size, delay, timeoutMs, + WHEN("_serialChunkedReceive is called") { + const char *err = _serialChunkedReceive(buf, &size, delay, timeoutMs, &available); THEN("No error is returned") { @@ -248,12 +248,12 @@ SCENARIO("serialChunkedReceive") } THEN("The returned size is exactly the number of bytes received " - "with noteSerialReceive") { + "with _noteSerialReceive") { CHECK(size == numAvailableBytes); } THEN("The output buffer has exactly the bytes returned by " - "noteSerialReceive") { + "_noteSerialReceive") { CHECK(memcmp(buf, recvBuf, size) == 0); } @@ -266,10 +266,10 @@ SCENARIO("serialChunkedReceive") } GIVEN("The delay parameter is false") { - noteSerialAvailable_fake.return_val = false; + _noteSerialAvailable_fake.return_val = false; - WHEN("serialChunkedReceive is called") { - serialChunkedReceive(buf, &size, false, timeoutMs, &available); + WHEN("_serialChunkedReceive is called") { + _serialChunkedReceive(buf, &size, false, timeoutMs, &available); THEN("NoteDelayMs is never called while waiting for bytes to become" " available to read") { @@ -279,10 +279,10 @@ SCENARIO("serialChunkedReceive") } GIVEN("The delay parameter is true") { - noteSerialAvailable_fake.return_val = false; + _noteSerialAvailable_fake.return_val = false; - WHEN("serialChunkedReceive is called") { - serialChunkedReceive(buf, &size, true, timeoutMs, &available); + WHEN("_serialChunkedReceive is called") { + _serialChunkedReceive(buf, &size, true, timeoutMs, &available); THEN("NoteDelayMs is called while waiting for bytes to become" " available to read") { @@ -291,8 +291,8 @@ SCENARIO("serialChunkedReceive") } } - RESET_FAKE(noteSerialAvailable); - RESET_FAKE(noteSerialReceive); + RESET_FAKE(_noteSerialAvailable); + RESET_FAKE(_noteSerialReceive); RESET_FAKE(NoteGetMs); RESET_FAKE(NoteDelayMs); } diff --git a/test/src/serialChunkedTransmit_test.cpp b/test/src/_serialChunkedTransmit_test.cpp similarity index 57% rename from test/src/serialChunkedTransmit_test.cpp rename to test/src/_serialChunkedTransmit_test.cpp index b0855bec..00f7d694 100644 --- a/test/src/serialChunkedTransmit_test.cpp +++ b/test/src/_serialChunkedTransmit_test.cpp @@ -1,5 +1,5 @@ /*! - * @file serialChunkedTransmit_test.cpp + * @file _serialChunkedTransmit_test.cpp * * Written by the Blues Inc. team. * @@ -20,33 +20,33 @@ #include "n_lib.h" DEFINE_FFF_GLOBALS -FAKE_VOID_FUNC(noteSerialTransmit, uint8_t *, size_t, bool) +FAKE_VOID_FUNC(_noteSerialTransmit, uint8_t *, size_t, bool) FAKE_VOID_FUNC(NoteDelayMs, uint32_t) namespace { -// This buffer will hold the bytes passed to noteSerialTransmit. We can then -// check that the bytes passed to serialChunkedTransmit are passed to -// noteSerialTransmit without alteration. +// This buffer will hold the bytes passed to _noteSerialTransmit. We can then +// check that the bytes passed to _serialChunkedTransmit are passed to +// _noteSerialTransmit without alteration. uint8_t accumulatedBuf[CARD_REQUEST_SERIAL_SEGMENT_MAX_LEN * 2]; size_t accumulatedIdx = 0; -void noteSerialTransmitAccumulate(uint8_t *buf, size_t size, bool) +void _noteSerialTransmitAccumulate(uint8_t *buf, size_t size, bool) { memcpy(accumulatedBuf + accumulatedIdx, buf, size); accumulatedIdx += size; } -SCENARIO("serialChunkedTransmit") +SCENARIO("_serialChunkedTransmit") { memset(accumulatedBuf, 0, sizeof(accumulatedBuf)); accumulatedIdx = 0; GIVEN("A 0-length transmit buffer") { - WHEN("serialChunkedTransmit is called") { + WHEN("_serialChunkedTransmit is called") { uint8_t buf[] = {0x01}; - const char *err = serialChunkedTransmit(buf, 0, true); + const char *err = _serialChunkedTransmit(buf, 0, true); THEN("No error is returned") { CHECK(err == NULL); @@ -55,80 +55,80 @@ SCENARIO("serialChunkedTransmit") } GIVEN("A buffer with size less than the maximum serial segment length") { - noteSerialTransmit_fake.custom_fake = noteSerialTransmitAccumulate; + _noteSerialTransmit_fake.custom_fake = _noteSerialTransmitAccumulate; uint8_t buf[CARD_REQUEST_SERIAL_SEGMENT_MAX_LEN - 1]; size_t size = sizeof(buf); memset(buf, 1, size); - WHEN("serialChunkedTransmit is called") { - const char *err = serialChunkedTransmit(buf, size, true); + WHEN("_serialChunkedTransmit is called") { + const char *err = _serialChunkedTransmit(buf, size, true); THEN("No error is returned") { CHECK(err == NULL); } - THEN("The buffer is passed verbatim to noteSerialTransmit") { + THEN("The buffer is passed verbatim to _noteSerialTransmit") { CHECK(memcmp(buf, accumulatedBuf, size) == 0); } - THEN("noteSerialTransmit is only called once, since the buffer fits" + THEN("_noteSerialTransmit is only called once, since the buffer fits" " in a single segment") { - CHECK(noteSerialTransmit_fake.call_count == 1); + CHECK(_noteSerialTransmit_fake.call_count == 1); } } } // TODO: Come up with a way to reduce duplication here. GIVEN("A buffer with size equal to the maximum serial segment length") { - noteSerialTransmit_fake.custom_fake = noteSerialTransmitAccumulate; + _noteSerialTransmit_fake.custom_fake = _noteSerialTransmitAccumulate; uint8_t buf[CARD_REQUEST_SERIAL_SEGMENT_MAX_LEN]; size_t size = sizeof(buf); memset(buf, 1, size); - WHEN("serialChunkedTransmit is called") { - const char *err = serialChunkedTransmit(buf, size, true); + WHEN("_serialChunkedTransmit is called") { + const char *err = _serialChunkedTransmit(buf, size, true); THEN("No error is returned") { CHECK(err == NULL); } - THEN("The buffer is passed verbatim to noteSerialTransmit") { + THEN("The buffer is passed verbatim to _noteSerialTransmit") { CHECK(memcmp(buf, accumulatedBuf, size) == 0); } - THEN("noteSerialTransmit is only called once, since the buffer fits" + THEN("_noteSerialTransmit is only called once, since the buffer fits" " in a single segment") { - CHECK(noteSerialTransmit_fake.call_count == 1); + CHECK(_noteSerialTransmit_fake.call_count == 1); } } } GIVEN("A buffer with size greater than the maximum serial segment length") { - noteSerialTransmit_fake.custom_fake = noteSerialTransmitAccumulate; + _noteSerialTransmit_fake.custom_fake = _noteSerialTransmitAccumulate; uint8_t buf[CARD_REQUEST_SERIAL_SEGMENT_MAX_LEN + 10]; size_t size = sizeof(buf); memset(buf, 1, size); - WHEN("serialChunkedTransmit is called") { - const char *err = serialChunkedTransmit(buf, size, true); + WHEN("_serialChunkedTransmit is called") { + const char *err = _serialChunkedTransmit(buf, size, true); THEN("No error is returned") { CHECK(err == NULL); } - THEN("The buffer is passed verbatim to noteSerialTransmit") { + THEN("The buffer is passed verbatim to _noteSerialTransmit") { CHECK(memcmp(buf, accumulatedBuf, size) == 0); } - THEN("noteSerialTransmit is called more than once, since the buffer" + THEN("_noteSerialTransmit is called more than once, since the buffer" " doesn't fit in a single segment") { - CHECK(noteSerialTransmit_fake.call_count > 1); + CHECK(_noteSerialTransmit_fake.call_count > 1); } } AND_GIVEN("The delay parameter is false") { - WHEN("serialChunkedTransmit is called") { - const char *err = serialChunkedTransmit(buf, size, false); + WHEN("_serialChunkedTransmit is called") { + const char *err = _serialChunkedTransmit(buf, size, false); THEN("NoteDelayMs is never called") { CHECK(NoteDelayMs_fake.call_count == 0); @@ -137,8 +137,8 @@ SCENARIO("serialChunkedTransmit") } AND_GIVEN("The delay parameter is true") { - WHEN("serialChunkedTransmit is called") { - const char *err = serialChunkedTransmit(buf, size, true); + WHEN("_serialChunkedTransmit is called") { + const char *err = _serialChunkedTransmit(buf, size, true); THEN("NoteDelayMs is called") { CHECK(NoteDelayMs_fake.call_count > 0); @@ -147,7 +147,7 @@ SCENARIO("serialChunkedTransmit") } } - RESET_FAKE(noteSerialTransmit); + RESET_FAKE(_noteSerialTransmit); RESET_FAKE(NoteDelayMs); } diff --git a/test/src/_serialNoteReset_test.cpp b/test/src/_serialNoteReset_test.cpp new file mode 100644 index 00000000..74e89d09 --- /dev/null +++ b/test/src/_serialNoteReset_test.cpp @@ -0,0 +1,135 @@ +/*! + * @file _serialNoteReset_test.cpp + * + * Written by the Blues Inc. team. + * + * Copyright (c) 2023 Blues Inc. MIT License. Use of this source code is + * governed by licenses granted by the copyright holder including that found in + * the + * LICENSE + * file. + * + */ + + + +#include +#include "fff.h" + +#include "n_lib.h" + +DEFINE_FFF_GLOBALS +FAKE_VALUE_FUNC(bool, _noteSerialReset) +FAKE_VOID_FUNC(_noteSerialTransmit, uint8_t *, size_t, bool) +FAKE_VALUE_FUNC(bool, _noteSerialAvailable) +FAKE_VALUE_FUNC(char, _noteSerialReceive) +FAKE_VALUE_FUNC(uint32_t, NoteGetMs) + +namespace +{ + +uint32_t NoteGetMsMock() +{ + static uint32_t ret = 500; + + // Cycle through returning 0, 1, and 500. 500 ms is the timeout of the + // receive loop in _serialNoteReset. + switch (ret) { + case 0: + ret = 1; + break; + case 1: + ret = 500; + break; + case 500: + ret = 0; + break; + } + + return ret; +} + +SCENARIO("_serialNoteReset") +{ + SECTION("_noteSerialReset fails") { + _noteSerialReset_fake.return_val = false; + + CHECK(!_serialNoteReset()); + CHECK(_noteSerialReset_fake.call_count == 1); + CHECK(_noteSerialTransmit_fake.call_count == 0); + } + + SECTION("Serial never available") { + _noteSerialReset_fake.return_val = true; + _noteSerialAvailable_fake.return_val = false; + NoteGetMs_fake.custom_fake = NoteGetMsMock; + + CHECK(!_serialNoteReset()); + // _serialNoteReset has retry logic, so we expect retries. + CHECK(_noteSerialTransmit_fake.call_count > 1); + CHECK(_noteSerialReceive_fake.call_count == 0); + } + + SECTION("Character received") { + NoteGetMs_fake.custom_fake = NoteGetMsMock; + bool serialAvailRetVals[] = {true, false}; + SET_RETURN_SEQ(_noteSerialAvailable, serialAvailRetVals, 2); + + SECTION("Non-control character received") { + _noteSerialReceive_fake.return_val = 'a'; + + SECTION("Retry") { + _noteSerialReset_fake.return_val = true; + + CHECK(!_serialNoteReset()); + // Expect retries. + CHECK(_noteSerialTransmit_fake.call_count > 1); + } + + SECTION("_noteSerialReset fails before retry possible") { + bool _noteSerialResetRetVals[] = {true, false}; + SET_RETURN_SEQ(_noteSerialReset, _noteSerialResetRetVals, 2); + + CHECK(!_serialNoteReset()); + // No retries. + CHECK(_noteSerialTransmit_fake.call_count == 1); + } + } + + SECTION("Only control character received") { + _noteSerialReset_fake.return_val = true; + _noteSerialReceive_fake.return_val = '\n'; + + CHECK(_serialNoteReset()); + // There should be no retrying. + CHECK(_noteSerialTransmit_fake.call_count == 1); + } + + CHECK(_noteSerialReceive_fake.call_count > 0); + } + + SECTION("NoteGetMs overflow") { + _noteSerialReset_fake.return_val = true; + _noteSerialReceive_fake.return_val = '\n'; + bool serialAvailRetVals[] = {true, false}; + SET_RETURN_SEQ(_noteSerialAvailable, serialAvailRetVals, 2); + uint32_t getMsReturnVals[] = { + UINT32_MAX - 500, + UINT32_MAX - 400, + 0 + }; + SET_RETURN_SEQ(NoteGetMs, getMsReturnVals, 3); + + CHECK(_serialNoteReset()); + } + + RESET_FAKE(_noteSerialReset); + RESET_FAKE(_noteSerialTransmit); + RESET_FAKE(_noteSerialAvailable); + RESET_FAKE(_noteSerialReceive); + RESET_FAKE(NoteGetMs); +} + +} + + diff --git a/test/src/serialNoteTransaction_test.cpp b/test/src/_serialNoteTransaction_test.cpp similarity index 55% rename from test/src/serialNoteTransaction_test.cpp rename to test/src/_serialNoteTransaction_test.cpp index 3a06063c..4558e921 100644 --- a/test/src/serialNoteTransaction_test.cpp +++ b/test/src/_serialNoteTransaction_test.cpp @@ -1,5 +1,5 @@ /*! - * @file serialNoteTransaction_test.cpp + * @file _serialNoteTransaction_test.cpp * * Written by the Blues Inc. team. * @@ -20,12 +20,12 @@ DEFINE_FFF_GLOBALS FAKE_VALUE_FUNC(void *, NoteMalloc, size_t) -FAKE_VALUE_FUNC(bool, noteSerialAvailable) -FAKE_VALUE_FUNC(char, noteSerialReceive) +FAKE_VALUE_FUNC(bool, _noteSerialAvailable) +FAKE_VALUE_FUNC(char, _noteSerialReceive) FAKE_VALUE_FUNC(uint32_t, NoteGetMs) -FAKE_VOID_FUNC(noteSerialTransmit, uint8_t *, size_t, bool) -FAKE_VALUE_FUNC(const char *, serialChunkedTransmit, uint8_t *, uint32_t, bool); -FAKE_VALUE_FUNC(const char *, serialChunkedReceive, uint8_t *, uint32_t *, bool, uint32_t, uint32_t *) +FAKE_VOID_FUNC(_noteSerialTransmit, uint8_t *, size_t, bool) +FAKE_VALUE_FUNC(const char *, _serialChunkedTransmit, uint8_t *, uint32_t, bool); +FAKE_VALUE_FUNC(const char *, _serialChunkedReceive, uint8_t *, uint32_t *, bool, uint32_t, uint32_t *) namespace { @@ -34,7 +34,7 @@ char transmitBuf[CARD_REQUEST_SERIAL_SEGMENT_MAX_LEN * 2]; size_t transmitBufLen = 0; bool resetTransmitBufLen = false; -void noteSerialTransmitAppend(uint8_t *buf, size_t len, bool) +void _noteSerialTransmitAppend(uint8_t *buf, size_t len, bool) { if (resetTransmitBufLen) { transmitBufLen = 0; @@ -53,14 +53,14 @@ void noteSerialTransmitAppend(uint8_t *buf, size_t len, bool) transmitBufLen += len; } -const char *serialChunkedTransmitAppend(uint8_t *buf, uint32_t len, bool) +const char *_serialChunkedTransmitAppend(uint8_t *buf, uint32_t len, bool) { - noteSerialTransmitAppend(buf, len, true); + _noteSerialTransmitAppend(buf, len, true); return NULL; } -const char *serialChunkedReceiveNothing(uint8_t *, uint32_t *size, bool, +const char *_serialChunkedReceiveNothing(uint8_t *, uint32_t *size, bool, uint32_t, uint32_t *available) { *size = 0; @@ -69,7 +69,7 @@ const char *serialChunkedReceiveNothing(uint8_t *, uint32_t *size, bool, return NULL; } -const char *serialChunkedReceiveOneAndDone(uint8_t *buf, uint32_t *size, bool, +const char *_serialChunkedReceiveOneAndDone(uint8_t *buf, uint32_t *size, bool, uint32_t, uint32_t *available) { *buf = '\n'; @@ -81,14 +81,14 @@ const char *serialChunkedReceiveOneAndDone(uint8_t *buf, uint32_t *size, bool, #define SERIAL_CHUNKED_RECEIVE_MULTIPLE_SIZE (2 * ALLOC_CHUNK) -size_t serialChunkedReceiveMultipleLeft = SERIAL_CHUNKED_RECEIVE_MULTIPLE_SIZE; +size_t _serialChunkedReceiveMultipleLeft = SERIAL_CHUNKED_RECEIVE_MULTIPLE_SIZE; -const char *serialChunkedReceiveMultiple(uint8_t *buf, uint32_t *size, bool, +const char *_serialChunkedReceiveMultiple(uint8_t *buf, uint32_t *size, bool, uint32_t, uint32_t *available) { memset(buf, 1, *size); - serialChunkedReceiveMultipleLeft -= *size; - *available = serialChunkedReceiveMultipleLeft; + _serialChunkedReceiveMultipleLeft -= *size; + *available = _serialChunkedReceiveMultipleLeft; return NULL; } @@ -98,7 +98,7 @@ void *MallocNull(size_t) return NULL; } -SCENARIO("serialNoteTransaction") +SCENARIO("_serialNoteTransaction") { NoteSetFnDefault(NULL, free, NULL, NULL); @@ -106,13 +106,13 @@ SCENARIO("serialNoteTransaction") const uint32_t timeoutMs = CARD_INTER_TRANSACTION_TIMEOUT_SEC; GIVEN("A valid JSON request C-string and a NULL response pointer") { - noteSerialTransmit_fake.custom_fake = noteSerialTransmitAppend; - serialChunkedTransmit_fake.custom_fake = serialChunkedTransmitAppend; + _noteSerialTransmit_fake.custom_fake = _noteSerialTransmitAppend; + _serialChunkedTransmit_fake.custom_fake = _serialChunkedTransmitAppend; - WHEN("serialNoteTransaction is called") { - const char *err = serialNoteTransaction(req, strlen(req), NULL, timeoutMs); + WHEN("_serialNoteTransaction is called") { + const char *err = _serialNoteTransaction(req, strlen(req), NULL, timeoutMs); - THEN("serialNoteTransaction returns NULL (no error)") { + THEN("_serialNoteTransaction returns NULL (no error)") { CHECK(err == NULL); } @@ -123,19 +123,19 @@ SCENARIO("serialNoteTransaction") CHECK(!memcmp(transmitBuf + rawReqLen, c_newline, c_newline_len)); } - THEN("noteSerialReceive is not called") { - CHECK(noteSerialReceive_fake.call_count == 0); + THEN("_noteSerialReceive is not called") { + CHECK(_noteSerialReceive_fake.call_count == 0); } } - AND_GIVEN("serialChunkedTransmit returns an error") { - serialChunkedTransmit_fake.custom_fake = NULL; - serialChunkedTransmit_fake.return_val = "some error"; + AND_GIVEN("_serialChunkedTransmit returns an error") { + _serialChunkedTransmit_fake.custom_fake = NULL; + _serialChunkedTransmit_fake.return_val = "some error"; - WHEN("serialNoteTransaction is called") { - const char *err = serialNoteTransaction(req, strlen(req), NULL, timeoutMs); + WHEN("_serialNoteTransaction is called") { + const char *err = _serialNoteTransaction(req, strlen(req), NULL, timeoutMs); - THEN("serialNoteTransaction returns an error") { + THEN("_serialNoteTransaction returns an error") { CHECK(err != NULL); } } @@ -147,22 +147,22 @@ SCENARIO("serialNoteTransaction") AND_GIVEN("Allocating a buffer to hold the response fails") { NoteMalloc_fake.return_val = NULL; - noteSerialAvailable_fake.return_val = true; + _noteSerialAvailable_fake.return_val = true; - WHEN("serialNoteTransaction is called") { - const char *err = serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_serialNoteTransaction is called") { + const char *err = _serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); THEN("NoteMalloc is called") { REQUIRE(NoteMalloc_fake.call_count > 0); } - THEN("serialNoteTransaction returns an error") { + THEN("_serialNoteTransaction returns an error") { CHECK(err != NULL); } - THEN("noteSerialReceive is not called (no bytes received from " + THEN("_noteSerialReceive is not called (no bytes received from " "the Notecard)") { - CHECK(noteSerialReceive_fake.call_count == 0); + CHECK(_noteSerialReceive_fake.call_count == 0); } THEN("The response pointer is unchanged") { @@ -171,9 +171,9 @@ SCENARIO("serialNoteTransaction") } } - AND_GIVEN("noteSerialAvailable never indicates bytes are available from" + AND_GIVEN("_noteSerialAvailable never indicates bytes are available from" " the Notecard") { - noteSerialAvailable_fake.return_val = false; + _noteSerialAvailable_fake.return_val = false; NoteMalloc_fake.custom_fake = malloc; uint32_t getMsReturnVals[3]; @@ -190,15 +190,15 @@ SCENARIO("serialNoteTransaction") SET_RETURN_SEQ(NoteGetMs, getMsReturnVals, 3); - WHEN("serialNoteTransaction is called") { - const char* err = serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_serialNoteTransaction is called") { + const char* err = _serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); - THEN("noteSerialReceive is not called (no bytes received " + THEN("_noteSerialReceive is not called (no bytes received " "from the Notecard)") { - CHECK(noteSerialReceive_fake.call_count == 0); + CHECK(_noteSerialReceive_fake.call_count == 0); } - THEN("serialNoteTransaction returns an error") { + THEN("_serialNoteTransaction returns an error") { REQUIRE(err != NULL); } @@ -213,15 +213,15 @@ SCENARIO("serialNoteTransaction") } } - AND_GIVEN("serialChunkedReceive returns an error") { - noteSerialAvailable_fake.return_val = true; + AND_GIVEN("_serialChunkedReceive returns an error") { + _noteSerialAvailable_fake.return_val = true; NoteMalloc_fake.custom_fake = malloc; - serialChunkedReceive_fake.return_val = "some error"; + _serialChunkedReceive_fake.return_val = "some error"; - WHEN("serialNoteTransaction is called") { - const char *err = serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_serialNoteTransaction is called") { + const char *err = _serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); - THEN("serialNoteTransaction returns an error") { + THEN("_serialNoteTransaction returns an error") { CHECK(err != NULL); } @@ -231,15 +231,15 @@ SCENARIO("serialNoteTransaction") } } - AND_GIVEN("serialChunkedReceive indicates there's nothing to receive") { - noteSerialAvailable_fake.return_val = true; + AND_GIVEN("_serialChunkedReceive indicates there's nothing to receive") { + _noteSerialAvailable_fake.return_val = true; NoteMalloc_fake.custom_fake = malloc; - serialChunkedReceive_fake.custom_fake = serialChunkedReceiveNothing; + _serialChunkedReceive_fake.custom_fake = _serialChunkedReceiveNothing; - WHEN("serialNoteTransaction is called") { - const char *err = serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_serialNoteTransaction is called") { + const char *err = _serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); - THEN("serialNoteTransaction returns NULL (no error)") { + THEN("_serialNoteTransaction returns NULL (no error)") { CHECK(err == NULL); } @@ -256,28 +256,28 @@ SCENARIO("serialNoteTransaction") } AND_GIVEN("The response is small enough for one call to " - "serialChunkedReceive") { - noteSerialAvailable_fake.return_val = true; + "_serialChunkedReceive") { + _noteSerialAvailable_fake.return_val = true; NoteMalloc_fake.custom_fake = malloc; - serialChunkedReceive_fake.custom_fake = - serialChunkedReceiveOneAndDone; + _serialChunkedReceive_fake.custom_fake = + _serialChunkedReceiveOneAndDone; - WHEN("serialNoteTransaction is called") { - const char *err = serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_serialNoteTransaction is called") { + const char *err = _serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); - THEN("serialNoteTransaction returns NULL (no error)") { + THEN("_serialNoteTransaction returns NULL (no error)") { CHECK(err == NULL); } - THEN("serialChunkedReceive is called exactly once") { - CHECK(serialChunkedReceive_fake.call_count == 1); + THEN("_serialChunkedReceive is called exactly once") { + CHECK(_serialChunkedReceive_fake.call_count == 1); } THEN("The response pointer is not NULL") { CHECK(rsp != NULL); AND_THEN("The response is exactly what was received by " - "serialChunkedReceive") { + "_serialChunkedReceive") { CHECK(strcmp(rsp, "\n") == 0); } } @@ -287,24 +287,24 @@ SCENARIO("serialNoteTransaction") } AND_GIVEN("The response is too big for one call to " - "serialChunkedReceive") { - noteSerialAvailable_fake.return_val = true; + "_serialChunkedReceive") { + _noteSerialAvailable_fake.return_val = true; NoteMalloc_fake.custom_fake = malloc; - serialChunkedReceive_fake.custom_fake = - serialChunkedReceiveMultiple; + _serialChunkedReceive_fake.custom_fake = + _serialChunkedReceiveMultiple; // TODO: Explain what we're doing here. - serialChunkedReceiveMultipleLeft = + _serialChunkedReceiveMultipleLeft = SERIAL_CHUNKED_RECEIVE_MULTIPLE_SIZE; - WHEN("serialNoteTransaction is called") { - const char *err = serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_serialNoteTransaction is called") { + const char *err = _serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); - THEN("serialNoteTransaction returns NULL (no error)") { + THEN("_serialNoteTransaction returns NULL (no error)") { CHECK(err == NULL); } - THEN("serialChunkedReceive is called more than once") { - CHECK(serialChunkedReceive_fake.call_count > 1); + THEN("_serialChunkedReceive is called more than once") { + CHECK(_serialChunkedReceive_fake.call_count > 1); } THEN("The response pointer is not NULL") { @@ -315,7 +315,7 @@ SCENARIO("serialNoteTransaction") SERIAL_CHUNKED_RECEIVE_MULTIPLE_SIZE); expectedRsp[SERIAL_CHUNKED_RECEIVE_MULTIPLE_SIZE] = '\0'; AND_THEN("The response is exactly what was received by " - "serialChunkedReceive") { + "_serialChunkedReceive") { CHECK(strcmp(rsp, expectedRsp) == 0); } } @@ -327,10 +327,10 @@ SCENARIO("serialNoteTransaction") void *(*mallocFns[])(size_t) = {malloc, MallocNull}; SET_CUSTOM_FAKE_SEQ(NoteMalloc, mallocFns, 2); - WHEN("serialNoteTransaction is called") { - const char *err = serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); + WHEN("_serialNoteTransaction is called") { + const char *err = _serialNoteTransaction(req, strlen(req), &rsp, timeoutMs); - THEN("serialNoteTransaction returns an error") { + THEN("_serialNoteTransaction returns an error") { CHECK(err != NULL); } @@ -343,12 +343,12 @@ SCENARIO("serialNoteTransaction") } RESET_FAKE(NoteMalloc); - RESET_FAKE(noteSerialAvailable); - RESET_FAKE(noteSerialTransmit); - RESET_FAKE(noteSerialReceive); + RESET_FAKE(_noteSerialAvailable); + RESET_FAKE(_noteSerialTransmit); + RESET_FAKE(_noteSerialReceive); RESET_FAKE(NoteGetMs); - RESET_FAKE(serialChunkedTransmit); - RESET_FAKE(serialChunkedReceive); + RESET_FAKE(_serialChunkedTransmit); + RESET_FAKE(_serialChunkedReceive); } } diff --git a/test/src/i2cNoteReset_test.cpp b/test/src/i2cNoteReset_test.cpp deleted file mode 100644 index 3da3b659..00000000 --- a/test/src/i2cNoteReset_test.cpp +++ /dev/null @@ -1,509 +0,0 @@ -/*! - * @file i2cNoteReset_test.cpp - * - * Written by the Blues Inc. team. - * - * Copyright (c) 2023 Blues Inc. MIT License. Use of this source code is - * governed by licenses granted by the copyright holder including that found in - * the - * LICENSE - * file. - * - */ - - - -#include -#include "fff.h" - -#include "n_lib.h" -#include "test_static.h" - -DEFINE_FFF_GLOBALS -FAKE_VOID_FUNC(delayIO) -FAKE_VOID_FUNC(NoteDelayMs, uint32_t) -FAKE_VALUE_FUNC(uint32_t, NoteGetMs) -FAKE_VALUE_FUNC(bool, noteI2CReset, uint16_t) -FAKE_VALUE_FUNC(const char *, noteI2CTransmit, uint16_t, uint8_t *, uint16_t) -FAKE_VALUE_FUNC(const char *, noteI2CReceive, uint16_t, uint8_t *, uint16_t, - uint32_t *) -FAKE_VOID_FUNC(NoteLockI2C) -FAKE_VOID_FUNC(NoteUnlockI2C) - -namespace -{ - -static uint32_t rtc_ms = 0; - -void NoteDelayMs_mock(uint32_t delayMs) -{ - rtc_ms += delayMs; -} - -uint32_t NoteGetMs_mock(void) -{ - return rtc_ms++; -} - -const char * noteI2CReceive_CleanResetSeq0(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) -{ - if (available) { - *available = 2; - } - return nullptr; -} - -const char * noteI2CReceive_CleanResetSeq1(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) -{ - if (available) { - *available = 0; - } - if (buffer) { - buffer[0] = '\r'; - buffer[1] = '\n'; - } - return nullptr; -} - -const char * noteI2CReceive_AvailGTBufferMessageSeq0(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) -{ - if (available) { - *available = (ALLOC_CHUNK + 1); - } - return nullptr; -} - -const char * noteI2CReceive_AvailGTBufferMessageSeq1(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) -{ - if (available) { - *available = 0; - } - return nullptr; -} - -const char * noteI2CReceive_AvailGTFFFFMessageSeq0(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) -{ - if (available) { - *available = 19790917; - } - return nullptr; -} - -const char * noteI2CReceive_AvailGTFFFFMessageSeq1(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) -{ - if (available) { - *available = 0; - } - return nullptr; -} - -const char * noteI2CReceive_AvailGTMaxMessageSeq0(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) -{ - if (available) { - *available = NoteI2CMax(); - } - return nullptr; -} - -const char * noteI2CReceive_AvailGTMaxMessageSeq1(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) -{ - if (available) { - *available = 0; - } - return nullptr; -} - -const char * noteI2CReceive_ClearMessageSeq0(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) -{ - if (available) { - *available = 4; - } - return nullptr; -} - -const char * noteI2CReceive_ClearMessageSeq1(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) -{ - if (available) { - *available = 0; - } - if (buffer) { - buffer[0] = '{'; - buffer[1] = '}'; - buffer[2] = '\r'; - buffer[3] = '\n'; - } - return nullptr; -} - -const char * noteI2CReceive_ClearMessageSeq2(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) -{ - if (available) { - *available = 2; - } - return nullptr; -} - -const char * noteI2CReceive_ClearMessageSeq3(uint16_t, uint8_t *buffer, uint16_t, uint32_t *available) -{ - if (available) { - *available = 0; - } - if (buffer) { - buffer[0] = '\r'; - buffer[1] = '\n'; - } - return nullptr; -} - -SCENARIO("i2cNoteReset") -{ - //TODO: Refactor GIVEN/WHEN/THEN block grouping and names - WHEN("Invoked") { - NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; - NoteGetMs_fake.custom_fake = NoteGetMs_mock; - i2cNoteReset(); - - THEN("Delay for `CARD_REQUEST_I2C_SEGMENT_DELAY_MS`") { - CHECK(NoteDelayMs_fake.call_count > 0); - CHECK(NoteDelayMs_fake.arg0_history[0] == CARD_REQUEST_I2C_SEGMENT_DELAY_MS); - } - - THEN("The I2C mutex will be taken") { - CHECK(NoteLockI2C_fake.call_count > 0); - } - - THEN("`noteI2CReset()` is called") { - CHECK(noteI2CReset_fake.call_count > 0); - } - AND_THEN("The first parameter is the Notecard address") { - CHECK(noteI2CReset_fake.arg0_history[0] == NoteI2CAddress()); - } - } - - GIVEN("`noteI2CReset()` is called") { - NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; - NoteGetMs_fake.custom_fake = NoteGetMs_mock; - - WHEN("`noteI2CReset()` fails") { - noteI2CReset_fake.return_val = false; - const bool success = i2cNoteReset(); - - THEN("The I2C mutex will be released") { - CHECK(NoteUnlockI2C_fake.call_count == NoteLockI2C_fake.call_count); - } - AND_THEN("`i2cNoteReset()` fails") { - CHECK(success == false); - } - - THEN("Exit immediately without retrying") { - CHECK(noteI2CReset_fake.call_count == 1); - } - } - - WHEN("`noteI2CReset()` succeeds") { - noteI2CReset_fake.return_val = true; - i2cNoteReset(); - - THEN("Then the I/O delay is respected") { - CHECK(delayIO_fake.call_count > 0); - } - } - } - - GIVEN("`noteI2CReset()` succeeds") { - NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; - NoteGetMs_fake.custom_fake = NoteGetMs_mock; - noteI2CReset_fake.return_val = true; - const bool success = i2cNoteReset(); - - THEN("`noteI2CTransmit()` is called") { - CHECK(noteI2CTransmit_fake.call_count > 0); - } - THEN("The first parameter is the Notecard address") { - CHECK(noteI2CTransmit_fake.arg0_history[0] == NoteI2CAddress()); - } - THEN("The second parameter is a string containing a single newline") { - CHECK(noteI2CTransmit_fake.arg1_history[0][0] == '\n'); - } - THEN("The third parameter is the string length of 1") { - CHECK(noteI2CTransmit_fake.arg2_history[0] == sizeof(char)); - } - } - - GIVEN("`noteI2CTransmit()` is called") { - NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; - NoteGetMs_fake.custom_fake = NoteGetMs_mock; - noteI2CReset_fake.return_val = true; - - WHEN("`noteI2CTransmit()` fails") { - noteI2CTransmit_fake.return_val = "most likely a NACK has occurred"; - const bool success = i2cNoteReset(); - - THEN("Delay for `CARD_REQUEST_I2C_NACK_WAIT_MS` milliseconds") { - CHECK(NoteDelayMs_fake.call_count > 1); - CHECK(NoteDelayMs_fake.arg0_history[1] == CARD_REQUEST_I2C_NACK_WAIT_MS); - } - - THEN("`noteI2CReceive()` is not called") { - CHECK(noteI2CReceive_fake.call_count == 0); - } - - THEN("`noteI2CTransmit()` will retry `CARD_RESET_SYNC_RETRIES` times") { - CHECK(noteI2CTransmit_fake.call_count == CARD_RESET_SYNC_RETRIES); - } - - THEN("The I2C mutex will be released") { - CHECK(NoteUnlockI2C_fake.call_count == NoteLockI2C_fake.call_count); - } - AND_THEN("`i2cNoteReset()` fails") { - CHECK(success == false); - } - } - - WHEN("`noteI2CTransmit()` succeeds") { - noteI2CTransmit_fake.return_val = nullptr; - const bool success = i2cNoteReset(); - - THEN("Delay for `CARD_REQUEST_I2C_SEGMENT_DELAY_MS`") { - CHECK(NoteDelayMs_fake.call_count > 1); - CHECK(NoteDelayMs_fake.arg0_history[1] == CARD_REQUEST_I2C_SEGMENT_DELAY_MS); - } - - THEN("`noteI2CReceive()` is called to query the response") { - CHECK(noteI2CReceive_fake.call_count > 0); - } - AND_THEN("The first parameter is the Notecard address") { - CHECK(noteI2CReceive_fake.arg0_history[0] == NoteI2CAddress()); - } - AND_THEN("The second parameter is a non-NULL static buffer used to drain bytes") { - CHECK(noteI2CReceive_fake.arg1_history[0] != nullptr); - } - AND_THEN("The third parameter is zero to indicate a query request") { - CHECK(noteI2CReceive_fake.arg2_history[0] == 0); - } - AND_THEN("The fourth parameter is a non-NULL unsigned integer pointer used for the query response") { - CHECK(noteI2CReceive_fake.arg3_history[0] != nullptr); - } - } - } - - GIVEN("`noteI2CReceive()` is called") { - NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; - NoteGetMs_fake.custom_fake = NoteGetMs_mock; - noteI2CReset_fake.return_val = true; - noteI2CTransmit_fake.return_val = nullptr; - - WHEN("`noteI2CReceive()` fails") { - noteI2CReceive_fake.return_val = "the Notecard ESP commonly generates protocol errors"; - const bool success = i2cNoteReset(); - - THEN("Delay for `CARD_REQUEST_I2C_SEGMENT_DELAY_MS`") { - CHECK(NoteDelayMs_fake.call_count > 2); - CHECK(NoteDelayMs_fake.arg0_history[2] == CARD_REQUEST_I2C_SEGMENT_DELAY_MS); - } - AND_THEN("Will retry `noteI2CReceive()`") { - CHECK(noteI2CReceive_fake.call_count > CARD_RESET_SYNC_RETRIES); - } - - THEN("The I2C mutex will be released") { - CHECK(NoteUnlockI2C_fake.call_count == NoteLockI2C_fake.call_count); - } - AND_THEN("`i2cNoteReset()` fails") { - CHECK(success == false); - } - } - WHEN("`noteI2CReceive()` succeeds") { - const char * (*ClearMessageSeq[])(uint16_t, uint8_t *, uint16_t, uint32_t *) = { - noteI2CReceive_ClearMessageSeq0, - noteI2CReceive_ClearMessageSeq1, - noteI2CReceive_ClearMessageSeq2, - noteI2CReceive_ClearMessageSeq3, - }; - SET_CUSTOM_FAKE_SEQ(noteI2CReceive, ClearMessageSeq, 4); - const bool success = i2cNoteReset(); - - THEN("Delay for `CARD_REQUEST_I2C_CHUNK_DELAY_MS`") { - CHECK(NoteDelayMs_fake.call_count > 2); - CHECK(NoteDelayMs_fake.arg0_history[2] == CARD_REQUEST_I2C_CHUNK_DELAY_MS); - } - - THEN("`noteI2CReceive()` is called to receive the response") { - CHECK(noteI2CReceive_fake.call_count > 1); - } - AND_THEN("The first parameter is the Notecard address") { - CHECK(noteI2CReceive_fake.arg0_val == NoteI2CAddress()); - } - AND_THEN("The second parameter is a non-NULL static buffer used to drain bytes") { - CHECK(noteI2CReceive_fake.arg1_val != nullptr); - } - AND_THEN("The third parameter is populated with the available value from the previous query") { - REQUIRE(noteI2CReceive_fake.call_count >= 4); - CHECK(noteI2CReceive_fake.arg2_history[0] == 0); - CHECK(noteI2CReceive_fake.arg2_history[1] == 4); - CHECK(noteI2CReceive_fake.arg2_history[2] == 0); - CHECK(noteI2CReceive_fake.arg2_history[3] == 2); - } - AND_THEN("The fourth parameter is a non-NULL unsigned integer pointer used for the query response") { - CHECK(noteI2CReceive_fake.arg3_val != nullptr); - } - } - } - - GIVEN("`noteI2CReceive()` succeeds") { - NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; - NoteGetMs_fake.custom_fake = NoteGetMs_mock; - noteI2CReset_fake.return_val = true; - noteI2CTransmit_fake.return_val = nullptr; - - WHEN("`noteI2CReceive()` available returns a value greater than that which can be contained by uint16_t") { - const char * (*AvailGTFFFFMessageSeq[])(uint16_t, uint8_t *, uint16_t, uint32_t *) = { - noteI2CReceive_AvailGTFFFFMessageSeq0, - noteI2CReceive_AvailGTFFFFMessageSeq1, - }; - SET_CUSTOM_FAKE_SEQ(noteI2CReceive, AvailGTFFFFMessageSeq, 2); - const bool success = i2cNoteReset(); - - THEN("The third parameter is populated with the available value from the previous query") { - REQUIRE(noteI2CReceive_fake.call_count >= 2); - CHECK(noteI2CReceive_fake.arg2_history[0] == 0); - CHECK(noteI2CReceive_fake.arg2_history[1] == NoteI2CMax()); - } - } - - WHEN("`noteI2CReceive()` available returns a value greater than the buffer size") { - const char * (*AvailGTBufferMessageSeq[])(uint16_t, uint8_t *, uint16_t, uint32_t *) = { - noteI2CReceive_AvailGTBufferMessageSeq0, - noteI2CReceive_AvailGTBufferMessageSeq1, - }; - SET_CUSTOM_FAKE_SEQ(noteI2CReceive, AvailGTBufferMessageSeq, 2); - const bool success = i2cNoteReset(); - - THEN("The third parameter is populated with a value capped at the buffer size") { - REQUIRE(noteI2CReceive_fake.call_count >= 2); - CHECK(noteI2CReceive_fake.arg2_val <= ALLOC_CHUNK); - } - } - - WHEN("`noteI2CReceive()` available returns a value greater than the maximum size allowed by the Serial-over-I2C protocol") { - const char * (*AvailGTMaxMessageSeq[])(uint16_t, uint8_t *, uint16_t, uint32_t *) = { - noteI2CReceive_AvailGTMaxMessageSeq0, - noteI2CReceive_AvailGTMaxMessageSeq1, - }; - SET_CUSTOM_FAKE_SEQ(noteI2CReceive, AvailGTMaxMessageSeq, 2); - const bool success = i2cNoteReset(); - - THEN("The third parameter is populated with a value capped at NoteI2CMax()") { - REQUIRE(noteI2CReceive_fake.call_count >= 2); - CHECK(noteI2CReceive_fake.arg2_val <= NoteI2CMax()); - } - } - } - - GIVEN("`noteI2CReceive()` will drain the Notecard I2C interface for `CARD_RESET_DRAIN_MS`") { - NoteDelayMs_fake.custom_fake = NoteDelayMs_mock; - NoteGetMs_fake.custom_fake = NoteGetMs_mock; - noteI2CReset_fake.return_val = true; - noteI2CTransmit_fake.return_val = nullptr; - - WHEN("`noteI2CReceive()` does not fail") { - rtc_ms = 0; - noteI2CReceive_fake.return_val = nullptr; - const bool success = i2cNoteReset(); - - THEN("`noteI2CReceive()` will be called for `CARD_RESET_DRAIN_MS`") { - // Must account for both the priming delay and the post transmit delay - CHECK(rtc_ms >= CARD_RESET_DRAIN_MS + (CARD_REQUEST_I2C_SEGMENT_DELAY_MS * 2)); - } - } - - WHEN("`noteI2CReceive()` receives any char that is neither \\r nor \\n") { - const char * (*ClearMessageSeq[])(uint16_t, uint8_t *, uint16_t, uint32_t *) = { - noteI2CReceive_ClearMessageSeq0, - noteI2CReceive_ClearMessageSeq1, - noteI2CReceive_ClearMessageSeq2, - noteI2CReceive_ClearMessageSeq3, - }; - SET_CUSTOM_FAKE_SEQ(noteI2CReceive, ClearMessageSeq, 4); - const bool success = i2cNoteReset(); - - THEN("`i2cNoteReset` fails") { - CHECK(success == false); - } - } - - WHEN("`noteI2CReceive()` receives nothing") { - noteI2CReceive_fake.return_val = nullptr; - const bool success = i2cNoteReset(); - - THEN("`i2cNoteReset` fails") { - CHECK(success == false); - } - THEN("`noteI2CReset()` is called") { - CHECK(noteI2CReset_fake.call_count > 1); - } - } - - AND_GIVEN("`noteI2CReceive()` receives nothing") { - noteI2CReceive_fake.return_val = nullptr; - - WHEN("`noteI2CReset()` fails") { - bool retSeq[] = { true, false }; - SET_RETURN_SEQ(noteI2CReset, retSeq, 2); - const bool success = i2cNoteReset(); - THEN("The I2C mutex will be released") { - CHECK(NoteUnlockI2C_fake.call_count == NoteLockI2C_fake.call_count); - } - THEN("Exit immediately without retrying") { - CHECK(noteI2CReset_fake.call_count == 2); - } - } - - WHEN("`noteI2CReset()` succeeds") { - i2cNoteReset(); - - THEN("Then the I/O delay is respected") { - // 1 + pre loop - // 10 retries (simple noteI2CReceive_fake returns zero --> nothing was found 10x) - CHECK(delayIO_fake.call_count > 10); - } - } - } - - WHEN("`noteI2CReceive()` receives only `\\r` and/or `\\n`") { - const char * (*CleanResetSeq[])(uint16_t, uint8_t *, uint16_t, uint32_t *) = { - noteI2CReceive_CleanResetSeq0, - noteI2CReceive_CleanResetSeq1, - }; - SET_CUSTOM_FAKE_SEQ(noteI2CReceive, CleanResetSeq, 2); - const bool success = i2cNoteReset(); - - THEN("Exit retry loop immediately") { - CHECK(noteI2CTransmit_fake.call_count == 1); - } - - THEN("`i2cNoteReset` succeeds") { - CHECK(success); - } - AND_THEN("The I2C mutex will be released") { - CHECK(NoteUnlockI2C_fake.call_count == NoteLockI2C_fake.call_count); - } - } - } - - // Sanity check against stray return paths - CHECK(NoteLockI2C_fake.call_count == 1); - CHECK(NoteUnlockI2C_fake.call_count == 1); - - RESET_FAKE(delayIO); - RESET_FAKE(NoteDelayMs); - RESET_FAKE(NoteGetMs); - RESET_FAKE(noteI2CReset); - RESET_FAKE(noteI2CTransmit); - RESET_FAKE(noteI2CReceive); - RESET_FAKE(NoteLockI2C); - RESET_FAKE(NoteUnlockI2C); -} - -} - - diff --git a/test/src/serialNoteReset_test.cpp b/test/src/serialNoteReset_test.cpp deleted file mode 100644 index e0b81195..00000000 --- a/test/src/serialNoteReset_test.cpp +++ /dev/null @@ -1,135 +0,0 @@ -/*! - * @file serialNoteReset_test.cpp - * - * Written by the Blues Inc. team. - * - * Copyright (c) 2023 Blues Inc. MIT License. Use of this source code is - * governed by licenses granted by the copyright holder including that found in - * the - * LICENSE - * file. - * - */ - - - -#include -#include "fff.h" - -#include "n_lib.h" - -DEFINE_FFF_GLOBALS -FAKE_VALUE_FUNC(bool, noteSerialReset) -FAKE_VOID_FUNC(noteSerialTransmit, uint8_t *, size_t, bool) -FAKE_VALUE_FUNC(bool, noteSerialAvailable) -FAKE_VALUE_FUNC(char, noteSerialReceive) -FAKE_VALUE_FUNC(uint32_t, NoteGetMs) - -namespace -{ - -uint32_t NoteGetMsMock() -{ - static uint32_t ret = 500; - - // Cycle through returning 0, 1, and 500. 500 ms is the timeout of the - // receive loop in serialNoteReset. - switch (ret) { - case 0: - ret = 1; - break; - case 1: - ret = 500; - break; - case 500: - ret = 0; - break; - } - - return ret; -} - -SCENARIO("serialNoteReset") -{ - SECTION("noteSerialReset fails") { - noteSerialReset_fake.return_val = false; - - CHECK(!serialNoteReset()); - CHECK(noteSerialReset_fake.call_count == 1); - CHECK(noteSerialTransmit_fake.call_count == 0); - } - - SECTION("Serial never available") { - noteSerialReset_fake.return_val = true; - noteSerialAvailable_fake.return_val = false; - NoteGetMs_fake.custom_fake = NoteGetMsMock; - - CHECK(!serialNoteReset()); - // serialNoteReset has retry logic, so we expect retries. - CHECK(noteSerialTransmit_fake.call_count > 1); - CHECK(noteSerialReceive_fake.call_count == 0); - } - - SECTION("Character received") { - NoteGetMs_fake.custom_fake = NoteGetMsMock; - bool serialAvailRetVals[] = {true, false}; - SET_RETURN_SEQ(noteSerialAvailable, serialAvailRetVals, 2); - - SECTION("Non-control character received") { - noteSerialReceive_fake.return_val = 'a'; - - SECTION("Retry") { - noteSerialReset_fake.return_val = true; - - CHECK(!serialNoteReset()); - // Expect retries. - CHECK(noteSerialTransmit_fake.call_count > 1); - } - - SECTION("noteSerialReset fails before retry possible") { - bool noteSerialResetRetVals[] = {true, false}; - SET_RETURN_SEQ(noteSerialReset, noteSerialResetRetVals, 2); - - CHECK(!serialNoteReset()); - // No retries. - CHECK(noteSerialTransmit_fake.call_count == 1); - } - } - - SECTION("Only control character received") { - noteSerialReset_fake.return_val = true; - noteSerialReceive_fake.return_val = '\n'; - - CHECK(serialNoteReset()); - // There should be no retrying. - CHECK(noteSerialTransmit_fake.call_count == 1); - } - - CHECK(noteSerialReceive_fake.call_count > 0); - } - - SECTION("NoteGetMs overflow") { - noteSerialReset_fake.return_val = true; - noteSerialReceive_fake.return_val = '\n'; - bool serialAvailRetVals[] = {true, false}; - SET_RETURN_SEQ(noteSerialAvailable, serialAvailRetVals, 2); - uint32_t getMsReturnVals[] = { - UINT32_MAX - 500, - UINT32_MAX - 400, - 0 - }; - SET_RETURN_SEQ(NoteGetMs, getMsReturnVals, 3); - - CHECK(serialNoteReset()); - } - - RESET_FAKE(noteSerialReset); - RESET_FAKE(noteSerialTransmit); - RESET_FAKE(noteSerialAvailable); - RESET_FAKE(noteSerialReceive); - RESET_FAKE(NoteGetMs); -} - -} - - From 57f1b7f55393cc86f51144e6a2a08ec3c6caf934 Mon Sep 17 00:00:00 2001 From: "Zachary J. Fields" Date: Sat, 9 Nov 2024 13:34:59 +0000 Subject: [PATCH 2/2] fix: Document generation --- .devcontainer/devcontainer.json | 1 + .vscode/tasks.json | 55 ++++++++++++++++++++++++++++++++- Dockerfile | 3 ++ docs/conf.py | 3 +- docs/library_initialization.rst | 14 ++++----- test/README.md | 2 +- 6 files changed, 68 insertions(+), 10 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index a1b99f38..6e0aa222 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -15,6 +15,7 @@ // Add the IDs of extensions you want installed when the container is created. "extensions": [ + "matepek.vscode-catch2-test-adapter", "ms-vscode.cpptools", "shardulm94.trailing-spaces", "twxs.cmake" diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 2802b545..e42b64fa 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -17,6 +17,24 @@ "isDefault": true } }, + { + "label": "Note-C: Compile and Run ALL Tests (with coverage)", + "type": "cppbuild", + "command": "./scripts/run_unit_tests.sh", + "args": [ + "--coverage", + ], + "options": { + "cwd": "${workspaceFolder}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": false + } + }, { "label": "Note-C: Compile and Run ALL Tests (with coverage and memory check)", "type": "cppbuild", @@ -51,6 +69,41 @@ "group": { "kind": "none" } + }, + { + "label": "Note-C: Generate Coverage HTML", + "type": "shell", + "command": "genhtml lcov.info -o tmp", + "args": [], + "options": { + "cwd": "${workspaceFolder}/build/test/coverage", + "env": { + "LC_ALL": "C" + } + }, + "problemMatcher": [], + "group": { + "kind": "none" + }, + "dependsOn": [ + "Note-C: Compile and Run ALL Tests (with coverage)" + ] + }, + { + "label": "Note-C: Generate Documentation", + "type": "shell", + "command": "./scripts/build_docs.sh", + "args": [], + "options": { + "cwd": "${workspaceFolder}", + "env": { + "LC_ALL": "C" + } + }, + "problemMatcher": [], + "group": { + "kind": "none" + } } ] -} \ No newline at end of file +} diff --git a/Dockerfile b/Dockerfile index 0368b33b..0d4b69e5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,6 +21,9 @@ ARG USER # Local Argument(s) +# Local Environment Variable(s) +ENV LC_ALL="C.UTF-8" + # Create Non-Root User RUN ["dash", "-c", "\ addgroup \ diff --git a/docs/conf.py b/docs/conf.py index 5a04823d..7b7a4a1b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -63,7 +63,8 @@ # Options for HTML output html_theme = 'sphinx_rtd_theme' -html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] +# WARNING: Calling get_html_theme_path is deprecated. If you are calling it to define html_theme_path, you are safe to remove that code. +#html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] html_logo = str(NOTE_C_BASE / 'assets' / 'blues_logo_no_text.png') html_theme_options = { 'logo_only': True, diff --git a/docs/library_initialization.rst b/docs/library_initialization.rst index 12356059..9f5ba40a 100644 --- a/docs/library_initialization.rst +++ b/docs/library_initialization.rst @@ -149,7 +149,7 @@ The code below from `Src/main.c