Skip to content

Commit 2ecd8ac

Browse files
authored
fix: I2c verification (#103)
* fix: I2c verification Add an additional test to ensure available never exceeds (NoteI2c::REQUEST_MAX_SIZE - NoteI2c::REQUEST_HEADER_SIZE) * remove note-c before re-add * Squashed 'src/note-c/' content from commit e52a4a1 git-subtree-dir: src/note-c git-subtree-split: e52a4a1bcf993e2aab8158075a926bed3c01324d * Update version
1 parent 6446d76 commit 2ecd8ac

16 files changed

+406
-134
lines changed

.vscode/extensions.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [],
5+
"unwantedRecommendations": [
6+
"ms-vscode.cpptools-extension-pack"
7+
]
8+
}

.vscode/tasks.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"version": "2.0.0",
33
"tasks": [
44
{
5-
"type": "cppbuild",
65
"label": "Compile and Run ALL Tests",
6+
"type": "cppbuild",
77
"command": "./test/run_all_tests.sh",
88
"args": [],
99
"options": {
@@ -18,8 +18,8 @@
1818
}
1919
},
2020
{
21-
"type": "cppbuild",
2221
"label": "Compile Notecard Tests",
22+
"type": "cppbuild",
2323
"command": "g++",
2424
"args": [
2525
"-Wall",
@@ -54,8 +54,8 @@
5454
}
5555
},
5656
{
57-
"type": "cppbuild",
5857
"label": "Compile NoteI2c Arduino Tests",
58+
"type": "cppbuild",
5959
"command": "g++",
6060
"args": [
6161
"-Wall",
@@ -85,8 +85,8 @@
8585
}
8686
},
8787
{
88-
"type": "cppbuild",
8988
"label": "Compile NoteI2c Arduino Tests (-DWIRE_HAS_END)",
89+
"type": "cppbuild",
9090
"command": "g++",
9191
"args": [
9292
"-Wall",
@@ -117,8 +117,8 @@
117117
}
118118
},
119119
{
120-
"type": "cppbuild",
121120
"label": "Compile NoteLog Arduino Tests",
121+
"type": "cppbuild",
122122
"command": "g++",
123123
"args": [
124124
"-Wall",
@@ -148,8 +148,8 @@
148148
}
149149
},
150150
{
151-
"type": "cppbuild",
152151
"label": "Compile NoteSerial Arduino Tests",
152+
"type": "cppbuild",
153153
"command": "g++",
154154
"args": [
155155
"-Wall",
@@ -179,8 +179,8 @@
179179
}
180180
},
181181
{
182-
"type": "cppbuild",
183182
"label": "Compile NoteTxn Arduino Tests",
183+
"type": "cppbuild",
184184
"command": "g++",
185185
"args": [
186186
"-Wall",
@@ -210,8 +210,8 @@
210210
}
211211
},
212212
{
213-
"type": "shell",
214213
"label": "Clear Test Binaries",
214+
"type": "shell",
215215
"command": "rm -rf *.tests",
216216
"options": {
217217
"cwd": "${workspaceFolder}"

library.properties

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

src/NoteI2c.hpp

+13
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ class NoteI2c
6969
*/
7070
/**************************************************************************/
7171
static const size_t REQUEST_HEADER_SIZE = 2;
72+
73+
/**************************************************************************/
74+
/*!
75+
@brief Maximum size of a Serial-Over-I2C request.
76+
77+
@details The requests made to and responses received from the low-level
78+
I2C controller can be no larger than (REQUEST_MAX_SIZE
79+
- REQUEST_HEADER_SIZE).
80+
81+
@see NoteI2c::receive
82+
*/
83+
/**************************************************************************/
84+
static const size_t REQUEST_MAX_SIZE = 255;
7285
};
7386

7487
/******************************************************************************/

src/NoteI2c_Arduino.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,20 @@ NoteI2c_Arduino::receive (
8787
} else if (response_length != request_length) {
8888
result = ERRSTR("serial-over-i2c: unexpected raw byte count {io}",i2cerr);
8989
} else {
90-
// Update available with remaining bytes
91-
*available_ = _i2cPort.read();
90+
// Ensure available byte count is within expected range
91+
static const size_t AVAILBLE_MAX = (NoteI2c::REQUEST_MAX_SIZE - NoteI2c::REQUEST_HEADER_SIZE);
92+
uint32_t available = _i2cPort.read();
93+
if (available > AVAILBLE_MAX) {
94+
result = ERRSTR("serial-over-i2c: available byte count greater than max allowed {io}",i2cerr);
95+
}
9296
// Ensure protocol response length matches size request
93-
if (requested_byte_count_ != static_cast<uint8_t>(_i2cPort.read())) {
97+
else if (requested_byte_count_ != static_cast<uint8_t>(_i2cPort.read())) {
9498
result = ERRSTR("serial-over-i2c: unexpected protocol byte count {io}",i2cerr);
95-
} else {
99+
}
100+
// Update available with remaining bytes
101+
else {
102+
*available_ = available;
103+
96104
for (size_t i = 0 ; i < requested_byte_count_ ; ++i) {
97105
//TODO: Perf test against indexed buffer writes
98106
*buffer_++ = _i2cPort.read();

src/note-c/CMakeLists.txt

+18-71
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,29 @@ if(NOT EXISTS ${PROJECT_BINARY_DIR}/.gitignore)
1515
endif()
1616

1717
option(NOTE_C_BUILD_TESTS "Build tests." OFF)
18-
option(BUILD_SHARED_LIBS "Build shared libraries instead of static." OFF)
1918
option(NOTE_C_COVERAGE "Compile for test NOTE_C_COVERAGE reporting." OFF)
2019
option(NOTE_C_MEM_CHECK "Run tests with Valgrind." OFF)
2120

2221
set(NOTE_C_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
23-
add_library(
22+
add_library(note_c SHARED)
23+
target_sources(
2424
note_c
25-
${NOTE_C_SRC_DIR}/n_atof.c
26-
${NOTE_C_SRC_DIR}/n_cjson.c
27-
${NOTE_C_SRC_DIR}/n_const.c
28-
${NOTE_C_SRC_DIR}/n_helpers.c
29-
${NOTE_C_SRC_DIR}/n_i2c.c
30-
${NOTE_C_SRC_DIR}/n_printf.c
31-
${NOTE_C_SRC_DIR}/n_serial.c
32-
${NOTE_C_SRC_DIR}/n_ua.c
33-
${NOTE_C_SRC_DIR}/n_b64.c
34-
${NOTE_C_SRC_DIR}/n_cjson_helpers.c
35-
${NOTE_C_SRC_DIR}/n_ftoa.c
36-
${NOTE_C_SRC_DIR}/n_hooks.c
37-
${NOTE_C_SRC_DIR}/n_md5.c
38-
${NOTE_C_SRC_DIR}/n_request.c
39-
${NOTE_C_SRC_DIR}/n_str.c
25+
PRIVATE
26+
${NOTE_C_SRC_DIR}/n_atof.c
27+
${NOTE_C_SRC_DIR}/n_cjson.c
28+
${NOTE_C_SRC_DIR}/n_const.c
29+
${NOTE_C_SRC_DIR}/n_helpers.c
30+
${NOTE_C_SRC_DIR}/n_i2c.c
31+
${NOTE_C_SRC_DIR}/n_printf.c
32+
${NOTE_C_SRC_DIR}/n_serial.c
33+
${NOTE_C_SRC_DIR}/n_ua.c
34+
${NOTE_C_SRC_DIR}/n_b64.c
35+
${NOTE_C_SRC_DIR}/n_cjson_helpers.c
36+
${NOTE_C_SRC_DIR}/n_ftoa.c
37+
${NOTE_C_SRC_DIR}/n_hooks.c
38+
${NOTE_C_SRC_DIR}/n_md5.c
39+
${NOTE_C_SRC_DIR}/n_request.c
40+
${NOTE_C_SRC_DIR}/n_str.c
4041
)
4142
target_compile_options(
4243
note_c
@@ -73,59 +74,5 @@ if(NOTE_C_BUILD_TESTS)
7374
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/test/include
7475
)
7576

76-
# If we don't weaken the functions we're mocking in the tests, the linker
77-
# will complain about multiple function definitions: the mocked one and the
78-
# "real" one from note-c. Weakening the real function causes the mock
79-
# function, if defined, to override the real one. If no mock is defined, the
80-
# real one will be used. So, every time a developer needs to mock a function
81-
# in a test, they need to make sure it's included in the MOCKED_FNS list
82-
# below.
83-
set(
84-
MOCKED_FNS
85-
"NoteReset;
86-
NoteJSONTransaction;
87-
NoteTransaction;
88-
NoteGetMs;
89-
NoteRequestResponse;
90-
NoteMalloc;
91-
NoteI2CTransmit;
92-
NoteI2CReceive;
93-
NoteLockI2C;
94-
NoteUnlockI2C;
95-
NoteI2CReset;
96-
NoteSerialAvailable;
97-
NoteSerialTransmit;
98-
NoteSerialReceive;
99-
NoteSerialReset;
100-
NoteIsDebugOutputActive;
101-
NoteDebug;
102-
NotePrint;
103-
NoteNewCommand;
104-
NoteRequest;
105-
JCreateObject;
106-
NoteTransactionStart;
107-
NoteUserAgent;
108-
serialNoteReset;
109-
serialNoteTransaction;
110-
i2cNoteReset;
111-
i2cNoteTransaction;
112-
JCreateStringValue;
113-
NoteSetEnvDefault;
114-
NoteSleep;
115-
NotePayloadRetrieveAfterSleep;
116-
NoteTimeValidST;
117-
NoteTimeST;
118-
NoteRegion"
119-
)
120-
foreach(MOCKED_FN ${MOCKED_FNS})
121-
string(APPEND OBJCOPY_WEAKEN "-W ${MOCKED_FN} ")
122-
endforeach()
123-
separate_arguments(OBJCOPY_WEAKEN_LIST NATIVE_COMMAND "${OBJCOPY_WEAKEN}")
124-
add_custom_command(TARGET note_c POST_BUILD
125-
COMMAND ${CMAKE_OBJCOPY} ${OBJCOPY_WEAKEN_LIST}
126-
$<TARGET_FILE:note_c>
127-
COMMENT "Weakening mocked functions."
128-
)
129-
13077
add_subdirectory(test)
13178
endif(NOTE_C_BUILD_TESTS)

0 commit comments

Comments
 (0)