Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ if(BUILD_TESTS)
NoteI2CReceive;
NoteLockI2C;
NoteUnlockI2C;
NoteI2CReset"
NoteI2CReset;
NoteSerialAvailable;
NoteSerialTransmit;
NoteSerialReceive;
NoteSerialReset;
NoteIsDebugOutputActive;
NoteDebug;
NotePrint"
)
foreach(MOCKED_FN ${MOCKED_FNS})
string(APPEND OBJCOPY_WEAKEN "-W ${MOCKED_FN} ")
Expand Down
8 changes: 4 additions & 4 deletions n_cjson_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ J *JGetObject(J *rsp, const char *field)

//**************************************************************************/
/*!
@brief Return the boolean repsentation of an item.
@brief Return the boolean representation of an item.
@param item The JSON item.
@returns The boolean value.
*/
Expand All @@ -98,7 +98,7 @@ bool JBoolValue(J *item)

//**************************************************************************/
/*!
@brief Return the string repsentation of an item.
@brief Return the string representation of an item.
@param item The JSON item.
@returns The string value, or empty string, if NULL.
*/
Expand All @@ -113,7 +113,7 @@ char *JStringValue(J *item)

//**************************************************************************/
/*!
@brief Return the number repsentation of an item.
@brief Return the number representation of an item.
@param item The JSON item.
@returns The number, or 0.0, if NULL.
*/
Expand Down Expand Up @@ -151,7 +151,7 @@ JNUMBER JGetNumber(J *rsp, const char *field)

//**************************************************************************/
/*!
@brief Return the integer repsentation of an item.
@brief Return the integer representation of an item.
@param item The JSON item.
@returns The number, or 0, if NULL.
*/
Expand Down
11 changes: 4 additions & 7 deletions n_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,16 @@ void NotePrintln(const char *line)
bool NotePrint(const char *text)
{
bool success = false;

if (NoteIsDebugOutputActive()) {
NoteDebug(text);
return true;
}
int inLog = 0;
if (inLog++ != 0) {
inLog--;
return false;
}

J *req = NoteNewRequest("card.log");
JAddStringToObject(req, "text", text);
success = NoteRequest(req);
inLog--;

return success;
}

Expand Down Expand Up @@ -328,7 +325,7 @@ bool NoteRegion(char **retCountry, char **retArea, char **retZone, int *retZoneO
if (retZoneOffset != NULL) {
*retZoneOffset = curZoneOffsetMins;
}
return true;;
return true;
}

//**************************************************************************/
Expand Down
63 changes: 37 additions & 26 deletions n_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ static void _DelayIO()
@param jsonResponse
An out parameter c-string buffer that will contain the JSON
response from the Notercard.
@returns a c-string with an error, or `NULL` if no error ocurred.
@returns a c-string with an error, or `NULL` if no error occurred.
*/
/**************************************************************************/
const char *i2cNoteTransaction(char *json, char **jsonResponse)
{

// Append newline to the transaction
int jsonLen = strlen(json);
size_t jsonLen = strlen(json);
uint8_t *transmitBuf = (uint8_t *) _Malloc(jsonLen+1);
if (transmitBuf == NULL) {
return ERRSTR("insufficient memory",c_mem);
Expand All @@ -63,11 +63,16 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
// Transmit the request in chunks, but also in segments so as not to overwhelm the notecard's interrupt buffers
const char *estr;
uint8_t *chunk = transmitBuf;
uint32_t sentInSegment = 0;
uint16_t sentInSegment = 0;
while (jsonLen > 0) {
int chunklen = (uint8_t) (jsonLen > (int)_I2CMax() ? (int)_I2CMax() : jsonLen);
// Constrain chunkLen to fit into 16 bits (_I2CTransmit takes the buffer
// size as a uint16_t).
uint16_t chunkLen = (jsonLen > 0xFFFF) ? 0xFFFF : jsonLen;
// Constrain chunkLen to be <= _I2CMax().
chunkLen = (chunkLen > _I2CMax()) ? _I2CMax() : chunkLen;

_DelayIO();
estr = _I2CTransmit(_I2CAddress(), chunk, chunklen);
estr = _I2CTransmit(_I2CAddress(), chunk, chunkLen);
if (estr != NULL) {
_Free(transmitBuf);
_I2CReset(_I2CAddress());
Expand All @@ -79,9 +84,9 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
_UnlockI2C();
return estr;
}
chunk += chunklen;
jsonLen -= chunklen;
sentInSegment += chunklen;
chunk += chunkLen;
jsonLen -= chunkLen;
sentInSegment += chunkLen;
if (sentInSegment > CARD_REQUEST_I2C_SEGMENT_MAX_LEN) {
sentInSegment = 0;
if (!cardTurboIO) {
Expand All @@ -107,7 +112,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
// our json parser requires a null-terminated string.
int growlen = ALLOC_CHUNK;
int jsonbufAllocLen = growlen;
char *jsonbuf = (char *) _Malloc(jsonbufAllocLen+1);
uint8_t *jsonbuf = (uint8_t *) _Malloc(jsonbufAllocLen+1);
if (jsonbuf == NULL) {
#ifdef ERRDBG
_Debug("transaction: jsonbuf malloc failed\n");
Expand All @@ -120,18 +125,18 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
// buffer we used to transmit, and will grow it as necessary.
bool receivedNewline = false;
int jsonbufLen = 0;
int chunklen = 0;
uint16_t chunkLen = 0;
uint32_t startMs = _GetMs();
while (true) {

// Grow the buffer as necessary to read this next chunk
if (jsonbufLen + chunklen > jsonbufAllocLen) {
if (chunklen > growlen) {
jsonbufAllocLen += chunklen;
if (jsonbufLen + chunkLen > jsonbufAllocLen) {
if (chunkLen > growlen) {
jsonbufAllocLen += chunkLen;
} else {
jsonbufAllocLen += growlen;
}
char *jsonbufNew = (char *) _Malloc(jsonbufAllocLen+1);
uint8_t *jsonbufNew = (uint8_t *) _Malloc(jsonbufAllocLen+1);
if (jsonbufNew == NULL) {
#ifdef ERRDBG
_Debug("transaction: jsonbuf grow malloc failed\n");
Expand All @@ -148,7 +153,8 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
// Read the chunk
uint32_t available;
_DelayIO();
const char *err = _I2CReceive(_I2CAddress(), (uint8_t *) &jsonbuf[jsonbufLen], chunklen, &available);
const char *err = _I2CReceive(_I2CAddress(), &jsonbuf[jsonbufLen],
chunkLen, &available);
if (err != NULL) {
_Free(jsonbuf);
#ifdef ERRDBG
Expand All @@ -159,7 +165,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
}

// We've now received the chunk
jsonbufLen += chunklen;
jsonbufLen += chunkLen;

// If the last byte of the chunk is \n, chances are that we're done. However, just so
// that we pull everything pending from the module, we only exit when we've received
Expand All @@ -168,11 +174,14 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
receivedNewline = true;
}

// For the next iteration, read the min of what's available and what we're permitted to read
chunklen = (int) (available > _I2CMax() ? _I2CMax() : available);
// Constrain chunkLen to fit into 16 bits (_I2CReceive takes the buffer
// size as a uint16_t).
chunkLen = (available > 0xFFFF) ? 0xFFFF : available;
// Constrain chunkLen to be <= _I2CMax().
chunkLen = (chunkLen > _I2CMax()) ? _I2CMax() : chunkLen;

// If there's something available on the notecard for us to receive, do it
if (chunklen > 0) {
if (chunkLen > 0) {
continue;
}

Expand Down Expand Up @@ -205,7 +214,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
jsonbuf[jsonbufLen] = '\0';

// Return it
*jsonResponse = jsonbuf;
*jsonResponse = (char *)jsonbuf;
return NULL;
}

Expand Down Expand Up @@ -242,16 +251,16 @@ bool i2cNoteReset()
for (retries=0; transmitErr==NULL && !notecardReady && retries<3; retries++) {

// Loop to drain all chunks of data that may be ready to transmit to us
int chunklen = 0;
uint16_t chunkLen = 0;
while (true) {

// Read the next chunk of available data
uint32_t available;
uint8_t buffer[128];
chunklen = (chunklen > (int)sizeof(buffer)) ? (int)sizeof(buffer) : chunklen;
chunklen = (chunklen > (int)_I2CMax()) ? (int)_I2CMax() : chunklen;
chunkLen = (chunkLen > sizeof(buffer)) ? sizeof(buffer) : chunkLen;
chunkLen = (chunkLen > _I2CMax()) ? _I2CMax() : chunkLen;
_DelayIO();
const char *err = _I2CReceive(_I2CAddress(), buffer, chunklen, &available);
const char *err = _I2CReceive(_I2CAddress(), buffer, chunkLen, &available);
if (err) {
break;
}
Expand All @@ -262,8 +271,10 @@ bool i2cNoteReset()
break;
}

// Read everything that's left on the module
chunklen = available;
// Read the minimum of the available bytes left to read and what
// will fit into a 16-bit unsigned value (_I2CReceive takes the
// buffer size as a uint16_t).
chunkLen = (available > 0xFFFF) ? 0xFFFF : available;

}

Expand Down
7 changes: 4 additions & 3 deletions n_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const char *serialNoteTransaction(char *json, char **jsonResponse)
if (segLen > CARD_REQUEST_SERIAL_SEGMENT_MAX_LEN) {
segLen = CARD_REQUEST_SERIAL_SEGMENT_MAX_LEN;
}
_SerialTransmit((uint8_t *)&transmitBuf[segOff], segLen, false);
_SerialTransmit(&transmitBuf[segOff], segLen, false);
segOff += segLen;
segLeft -= segLen;
if (segLeft == 0) {
Expand Down Expand Up @@ -212,8 +212,9 @@ bool serialNoteReset()
_Debug("no notecard\n");
#endif
_DelayMs(500);
_SerialReset();

if (!_SerialReset()) {
return false;
}
}

// Done
Expand Down
22 changes: 20 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ add_test(NoteGetEnv_test)
add_test(NotePayload_test)
add_test(i2cNoteTransaction_test)
add_test(i2cNoteReset_test)
add_test(serialNoteTransaction_test)
add_test(serialNoteReset_test)
add_test(NoteSetFn_test)
add_test(NoteDebug_test)
add_test(NoteTransactionHooks_test)
add_test(NoteSerialHooks_test)
add_test(NotePrint_test)
add_test(NotePrintln_test)

if(COVERAGE)
find_program(LCOV lcov REQUIRED)
Expand All @@ -77,11 +85,21 @@ if(COVERAGE)
COMMAND ${CMAKE_CTEST_COMMAND}
WORKING_DIRECTORY ${CMAKE_CURENT_BINARY_DIR}
)
# These files are third party code that we aren't interested in testing
# ourselves, so we don't care about coverage for them.
set(
EXCLUDE_FROM_COVERAGE
"n_atof.c;n_b64.c;n_cjson.c;n_ftoa.c;n_md5.c;n_str.c"
)
foreach(EXCLUDE_FILE ${EXCLUDE_FROM_COVERAGE})
string(APPEND LCOV_EXCLUDE "--exclude '*/${EXCLUDE_FILE}' ")
endforeach()
separate_arguments(LCOV_EXCLUDE_LIST NATIVE_COMMAND "${LCOV_EXCLUDE}")
# Run lcov to produce a coverage report in the coverage directory.
add_custom_command(
TARGET coverage POST_BUILD
COMMAND lcov --capture --no-external --directory ${NOTE_C_SRC_DIR} --output-file lcov.info --rc lcov_branch_coverage=1
COMMAND lcov --summary lcov.info
COMMAND lcov --capture --no-external --directory ${NOTE_C_SRC_DIR} --rc lcov_branch_coverage=1 ${LCOV_EXCLUDE_LIST} --output-file lcov.info
COMMAND lcov --summary --rc lcov_branch_coverage=1 lcov.info
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/coverage
)
# The tests have to be built before we can generate the coverage report.
Expand Down
Loading