Skip to content

Commit a18f6ab

Browse files
authored
Update note-c for release (#95)
* updated version information * remove note-c before re-add * Squashed 'src/note-c/' content from commit afa46a6 git-subtree-dir: src/note-c git-subtree-split: afa46a6708ba4dd491ea4b844db73eed82b4ece5
1 parent 2a5b1b1 commit a18f6ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2211
-246
lines changed

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Blues Wireless Notecard
2-
version=1.3.17
2+
version=1.3.18
33
author=Blues Wireless
44
maintainer=Blues Wireless <[email protected]>
55
sentence=An easy to use Notecard Library for Arduino.

src/note-c/.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ jobs:
2222
- name: Install astyle
2323
run: sudo apt-get install -y astyle
2424
- name: Check formatting
25-
run: ./scripts/run_astyle.sh --check
25+
run: ./scripts/run_astyle.sh

src/note-c/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ html/
55

66
# VS Code workspace files
77
*.code-workspace
8-
*.orig
8+
*.orig

src/note-c/CMakeLists.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,16 @@ if(BUILD_TESTS)
7373
NoteI2CReceive;
7474
NoteLockI2C;
7575
NoteUnlockI2C;
76-
NoteI2CReset"
76+
NoteI2CReset;
77+
NoteSerialAvailable;
78+
NoteSerialTransmit;
79+
NoteSerialReceive;
80+
NoteSerialReset;
81+
NoteIsDebugOutputActive;
82+
NoteDebug;
83+
NotePrint;
84+
NoteNewCommand;
85+
NoteRequest"
7786
)
7887
foreach(MOCKED_FN ${MOCKED_FNS})
7988
string(APPEND OBJCOPY_WEAKEN "-W ${MOCKED_FN} ")

src/note-c/n_cjson_helpers.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ J *JGetObject(J *rsp, const char *field)
8383

8484
//**************************************************************************/
8585
/*!
86-
@brief Return the boolean repsentation of an item.
86+
@brief Return the boolean representation of an item.
8787
@param item The JSON item.
8888
@returns The boolean value.
8989
*/
@@ -98,7 +98,7 @@ bool JBoolValue(J *item)
9898

9999
//**************************************************************************/
100100
/*!
101-
@brief Return the string repsentation of an item.
101+
@brief Return the string representation of an item.
102102
@param item The JSON item.
103103
@returns The string value, or empty string, if NULL.
104104
*/
@@ -113,7 +113,7 @@ char *JStringValue(J *item)
113113

114114
//**************************************************************************/
115115
/*!
116-
@brief Return the number repsentation of an item.
116+
@brief Return the number representation of an item.
117117
@param item The JSON item.
118118
@returns The number, or 0.0, if NULL.
119119
*/
@@ -151,7 +151,7 @@ JNUMBER JGetNumber(J *rsp, const char *field)
151151

152152
//**************************************************************************/
153153
/*!
154-
@brief Return the integer repsentation of an item.
154+
@brief Return the integer representation of an item.
155155
@param item The JSON item.
156156
@returns The number, or 0, if NULL.
157157
*/
@@ -546,7 +546,7 @@ int JGetType(J *rsp, const char *field)
546546
}
547547
return JTYPE_NUMBER;
548548
case JRaw:
549-
case JString:
549+
case JString: {
550550
v = item->valuestring;
551551
if (v == NULL || v[0] == 0) {
552552
return JTYPE_STRING_BLANK;
@@ -576,6 +576,7 @@ int JGetType(J *rsp, const char *field)
576576
return JTYPE_STRING_BOOL_FALSE;
577577
}
578578
return JTYPE_STRING;
579+
}
579580
case JObject:
580581
return JTYPE_OBJECT;
581582
case JArray:

src/note-c/n_helpers.c

+7-10
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,16 @@ void NotePrintln(const char *line)
182182
bool NotePrint(const char *text)
183183
{
184184
bool success = false;
185+
185186
if (NoteIsDebugOutputActive()) {
186187
NoteDebug(text);
187188
return true;
188189
}
189-
int inLog = 0;
190-
if (inLog++ != 0) {
191-
inLog--;
192-
return false;
193-
}
190+
194191
J *req = NoteNewRequest("card.log");
195192
JAddStringToObject(req, "text", text);
196193
success = NoteRequest(req);
197-
inLog--;
194+
198195
return success;
199196
}
200197

@@ -328,7 +325,7 @@ bool NoteRegion(char **retCountry, char **retArea, char **retZone, int *retZoneO
328325
if (retZoneOffset != NULL) {
329326
*retZoneOffset = curZoneOffsetMins;
330327
}
331-
return true;;
328+
return true;
332329
}
333330

334331
//**************************************************************************/
@@ -757,7 +754,7 @@ bool NoteGetLocation(JNUMBER *retLat, JNUMBER *retLon, JTIME *time, char *status
757754
J *rsp = NoteRequestResponse(NoteNewRequest("card.location"));
758755
if (rsp != NULL) {
759756
if (statusBuf != NULL) {
760-
strlcpy(statusBuf, JGetString(rsp, "err"), statusBufLen);
757+
strlcpy(statusBuf, JGetString(rsp, "status"), statusBufLen);
761758
}
762759
if (JIsPresent(rsp, "lat") && JIsPresent(rsp, "lon")) {
763760
if (retLat != NULL) {
@@ -992,7 +989,7 @@ bool NoteGetStatus(char *statusBuf, int statusBufLen, JTIME *bootTime, bool *ret
992989

993990
//**************************************************************************/
994991
/*!
995-
@brief Get Status of the Notecard, with a supression timer.
992+
@brief Get Status of the Notecard, with a suppression timer.
996993
@param statusBuf (out) a buffer to populate with the Notecard status
997994
from the response.
998995
@param statusBufLen The length of the status buffer.
@@ -1117,7 +1114,7 @@ bool NoteSleep(char *stateb64, uint32_t seconds, const char *modes)
11171114
if (req != NULL) {
11181115
// Add the base64 item in a wonderful way that doesn't strdup the huge string
11191116
if (stateb64 != NULL) {
1120-
J *stringReferenceItem = JCreateStringValue(stateb64);
1117+
J *stringReferenceItem = JCreateStringReference(stateb64);
11211118
if (stringReferenceItem != NULL) {
11221119
JAddItemToObject(req, "payload", stringReferenceItem);
11231120
}

src/note-c/n_i2c.c

+37-26
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ static void _DelayIO()
4242
@param jsonResponse
4343
An out parameter c-string buffer that will contain the JSON
4444
response from the Notercard.
45-
@returns a c-string with an error, or `NULL` if no error ocurred.
45+
@returns a c-string with an error, or `NULL` if no error occurred.
4646
*/
4747
/**************************************************************************/
4848
const char *i2cNoteTransaction(char *json, char **jsonResponse)
4949
{
5050

5151
// Append newline to the transaction
52-
int jsonLen = strlen(json);
52+
size_t jsonLen = strlen(json);
5353
uint8_t *transmitBuf = (uint8_t *) _Malloc(jsonLen+1);
5454
if (transmitBuf == NULL) {
5555
return ERRSTR("insufficient memory",c_mem);
@@ -63,11 +63,16 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
6363
// Transmit the request in chunks, but also in segments so as not to overwhelm the notecard's interrupt buffers
6464
const char *estr;
6565
uint8_t *chunk = transmitBuf;
66-
uint32_t sentInSegment = 0;
66+
uint16_t sentInSegment = 0;
6767
while (jsonLen > 0) {
68-
int chunklen = (uint8_t) (jsonLen > (int)_I2CMax() ? (int)_I2CMax() : jsonLen);
68+
// Constrain chunkLen to fit into 16 bits (_I2CTransmit takes the buffer
69+
// size as a uint16_t).
70+
uint16_t chunkLen = (jsonLen > 0xFFFF) ? 0xFFFF : jsonLen;
71+
// Constrain chunkLen to be <= _I2CMax().
72+
chunkLen = (chunkLen > _I2CMax()) ? _I2CMax() : chunkLen;
73+
6974
_DelayIO();
70-
estr = _I2CTransmit(_I2CAddress(), chunk, chunklen);
75+
estr = _I2CTransmit(_I2CAddress(), chunk, chunkLen);
7176
if (estr != NULL) {
7277
_Free(transmitBuf);
7378
_I2CReset(_I2CAddress());
@@ -79,9 +84,9 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
7984
_UnlockI2C();
8085
return estr;
8186
}
82-
chunk += chunklen;
83-
jsonLen -= chunklen;
84-
sentInSegment += chunklen;
87+
chunk += chunkLen;
88+
jsonLen -= chunkLen;
89+
sentInSegment += chunkLen;
8590
if (sentInSegment > CARD_REQUEST_I2C_SEGMENT_MAX_LEN) {
8691
sentInSegment = 0;
8792
if (!cardTurboIO) {
@@ -107,7 +112,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
107112
// our json parser requires a null-terminated string.
108113
int growlen = ALLOC_CHUNK;
109114
int jsonbufAllocLen = growlen;
110-
char *jsonbuf = (char *) _Malloc(jsonbufAllocLen+1);
115+
uint8_t *jsonbuf = (uint8_t *) _Malloc(jsonbufAllocLen+1);
111116
if (jsonbuf == NULL) {
112117
#ifdef ERRDBG
113118
_Debug("transaction: jsonbuf malloc failed\n");
@@ -120,18 +125,18 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
120125
// buffer we used to transmit, and will grow it as necessary.
121126
bool receivedNewline = false;
122127
int jsonbufLen = 0;
123-
int chunklen = 0;
128+
uint16_t chunkLen = 0;
124129
uint32_t startMs = _GetMs();
125130
while (true) {
126131

127132
// Grow the buffer as necessary to read this next chunk
128-
if (jsonbufLen + chunklen > jsonbufAllocLen) {
129-
if (chunklen > growlen) {
130-
jsonbufAllocLen += chunklen;
133+
if (jsonbufLen + chunkLen > jsonbufAllocLen) {
134+
if (chunkLen > growlen) {
135+
jsonbufAllocLen += chunkLen;
131136
} else {
132137
jsonbufAllocLen += growlen;
133138
}
134-
char *jsonbufNew = (char *) _Malloc(jsonbufAllocLen+1);
139+
uint8_t *jsonbufNew = (uint8_t *) _Malloc(jsonbufAllocLen+1);
135140
if (jsonbufNew == NULL) {
136141
#ifdef ERRDBG
137142
_Debug("transaction: jsonbuf grow malloc failed\n");
@@ -148,7 +153,8 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
148153
// Read the chunk
149154
uint32_t available;
150155
_DelayIO();
151-
const char *err = _I2CReceive(_I2CAddress(), (uint8_t *) &jsonbuf[jsonbufLen], chunklen, &available);
156+
const char *err = _I2CReceive(_I2CAddress(), &jsonbuf[jsonbufLen],
157+
chunkLen, &available);
152158
if (err != NULL) {
153159
_Free(jsonbuf);
154160
#ifdef ERRDBG
@@ -159,7 +165,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
159165
}
160166

161167
// We've now received the chunk
162-
jsonbufLen += chunklen;
168+
jsonbufLen += chunkLen;
163169

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

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

174183
// If there's something available on the notecard for us to receive, do it
175-
if (chunklen > 0) {
184+
if (chunkLen > 0) {
176185
continue;
177186
}
178187

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

207216
// Return it
208-
*jsonResponse = jsonbuf;
217+
*jsonResponse = (char *)jsonbuf;
209218
return NULL;
210219
}
211220

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

244253
// Loop to drain all chunks of data that may be ready to transmit to us
245-
int chunklen = 0;
254+
uint16_t chunkLen = 0;
246255
while (true) {
247256

248257
// Read the next chunk of available data
249258
uint32_t available;
250259
uint8_t buffer[128];
251-
chunklen = (chunklen > (int)sizeof(buffer)) ? (int)sizeof(buffer) : chunklen;
252-
chunklen = (chunklen > (int)_I2CMax()) ? (int)_I2CMax() : chunklen;
260+
chunkLen = (chunkLen > sizeof(buffer)) ? sizeof(buffer) : chunkLen;
261+
chunkLen = (chunkLen > _I2CMax()) ? _I2CMax() : chunkLen;
253262
_DelayIO();
254-
const char *err = _I2CReceive(_I2CAddress(), buffer, chunklen, &available);
263+
const char *err = _I2CReceive(_I2CAddress(), buffer, chunkLen, &available);
255264
if (err) {
256265
break;
257266
}
@@ -262,8 +271,10 @@ bool i2cNoteReset()
262271
break;
263272
}
264273

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

268279
}
269280

src/note-c/n_serial.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const char *serialNoteTransaction(char *json, char **jsonResponse)
4848
if (segLen > CARD_REQUEST_SERIAL_SEGMENT_MAX_LEN) {
4949
segLen = CARD_REQUEST_SERIAL_SEGMENT_MAX_LEN;
5050
}
51-
_SerialTransmit((uint8_t *)&transmitBuf[segOff], segLen, false);
51+
_SerialTransmit(&transmitBuf[segOff], segLen, false);
5252
segOff += segLen;
5353
segLeft -= segLen;
5454
if (segLeft == 0) {
@@ -212,8 +212,9 @@ bool serialNoteReset()
212212
_Debug("no notecard\n");
213213
#endif
214214
_DelayMs(500);
215-
_SerialReset();
216-
215+
if (!_SerialReset()) {
216+
return false;
217+
}
217218
}
218219

219220
// Done

src/note-c/scripts/run_astyle.sh

+10-30
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
# If a file shouldn't be formatted, it needs to be listed in the EXCLUDE_FILES
77
# array below with a path relative to the source code root.
88
#
9-
# Options:
10-
# --check: After formatting, check if any necessary formatting changes weren't
11-
# committed. If there are uncommitted changes, fail. This allows us
12-
# to ensure all committed code has been formatted prior to being
13-
# merged.
14-
#
159

1610
if [[ ! $(which astyle) ]]; then
1711
echo "astyle binary not found. Please install it and try again."
@@ -23,21 +17,9 @@ EXCLUDE_FILES=(
2317
test/include/fff.h
2418
)
2519

26-
CHECK=0
27-
28-
while [[ "$#" -gt 0 ]]; do
29-
case $1 in
30-
--check) CHECK=1 ;;
31-
*) echo "Unknown parameter: $1"; exit 1 ;;
32-
esac
33-
shift
34-
done
35-
36-
if [ $CHECK -eq 1 ]; then
37-
if [[ `git status --porcelain --untracked=no` ]]; then
38-
echo "Uncommitted changes detected. Please commit or discard any working tree changes prior to running this script with --check."
39-
exit 1
40-
fi
20+
if [[ `git status --porcelain --untracked=no` ]]; then
21+
echo "Local changes detected. Please commit, stash, or discard any working tree changes prior to running this script."
22+
exit 1
4123
fi
4224

4325
# Run astyle on all version-controlled C and C++ files.
@@ -49,13 +31,11 @@ do
4931
fi
5032
done
5133

52-
# If --check was given, fail the script if running astyle produced any new,
53-
# uncommitted changes.
54-
if [ $CHECK -eq 1 ]; then
55-
if [[ `git status --porcelain --untracked=no` ]]; then
56-
echo "Unformatted files detected. Run scripts/run_astyle.sh and amend your commit with the resulting formatting changes."
57-
exit 1
58-
else
59-
echo "Formatting clean."
60-
fi
34+
# If there are new formatting changes after running astyle, exit with a non-zero
35+
# value.
36+
if [[ `git status --porcelain --untracked=no` ]]; then
37+
echo "New formatting changes left in working tree."
38+
exit 1
39+
else
40+
echo "No new formatting changes."
6141
fi

0 commit comments

Comments
 (0)