@@ -42,14 +42,14 @@ static void _DelayIO()
42
42
@param jsonResponse
43
43
An out parameter c-string buffer that will contain the JSON
44
44
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 .
46
46
*/
47
47
/**************************************************************************/
48
48
const char * i2cNoteTransaction (char * json , char * * jsonResponse )
49
49
{
50
50
51
51
// Append newline to the transaction
52
- int jsonLen = strlen (json );
52
+ size_t jsonLen = strlen (json );
53
53
uint8_t * transmitBuf = (uint8_t * ) _Malloc (jsonLen + 1 );
54
54
if (transmitBuf == NULL ) {
55
55
return ERRSTR ("insufficient memory" ,c_mem );
@@ -63,11 +63,16 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
63
63
// Transmit the request in chunks, but also in segments so as not to overwhelm the notecard's interrupt buffers
64
64
const char * estr ;
65
65
uint8_t * chunk = transmitBuf ;
66
- uint32_t sentInSegment = 0 ;
66
+ uint16_t sentInSegment = 0 ;
67
67
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
+
69
74
_DelayIO ();
70
- estr = _I2CTransmit (_I2CAddress (), chunk , chunklen );
75
+ estr = _I2CTransmit (_I2CAddress (), chunk , chunkLen );
71
76
if (estr != NULL ) {
72
77
_Free (transmitBuf );
73
78
_I2CReset (_I2CAddress ());
@@ -79,9 +84,9 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
79
84
_UnlockI2C ();
80
85
return estr ;
81
86
}
82
- chunk += chunklen ;
83
- jsonLen -= chunklen ;
84
- sentInSegment += chunklen ;
87
+ chunk += chunkLen ;
88
+ jsonLen -= chunkLen ;
89
+ sentInSegment += chunkLen ;
85
90
if (sentInSegment > CARD_REQUEST_I2C_SEGMENT_MAX_LEN ) {
86
91
sentInSegment = 0 ;
87
92
if (!cardTurboIO ) {
@@ -107,7 +112,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
107
112
// our json parser requires a null-terminated string.
108
113
int growlen = ALLOC_CHUNK ;
109
114
int jsonbufAllocLen = growlen ;
110
- char * jsonbuf = (char * ) _Malloc (jsonbufAllocLen + 1 );
115
+ uint8_t * jsonbuf = (uint8_t * ) _Malloc (jsonbufAllocLen + 1 );
111
116
if (jsonbuf == NULL ) {
112
117
#ifdef ERRDBG
113
118
_Debug ("transaction: jsonbuf malloc failed\n" );
@@ -120,18 +125,18 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
120
125
// buffer we used to transmit, and will grow it as necessary.
121
126
bool receivedNewline = false;
122
127
int jsonbufLen = 0 ;
123
- int chunklen = 0 ;
128
+ uint16_t chunkLen = 0 ;
124
129
uint32_t startMs = _GetMs ();
125
130
while (true) {
126
131
127
132
// 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 ;
131
136
} else {
132
137
jsonbufAllocLen += growlen ;
133
138
}
134
- char * jsonbufNew = (char * ) _Malloc (jsonbufAllocLen + 1 );
139
+ uint8_t * jsonbufNew = (uint8_t * ) _Malloc (jsonbufAllocLen + 1 );
135
140
if (jsonbufNew == NULL ) {
136
141
#ifdef ERRDBG
137
142
_Debug ("transaction: jsonbuf grow malloc failed\n" );
@@ -148,7 +153,8 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
148
153
// Read the chunk
149
154
uint32_t available ;
150
155
_DelayIO ();
151
- const char * err = _I2CReceive (_I2CAddress (), (uint8_t * ) & jsonbuf [jsonbufLen ], chunklen , & available );
156
+ const char * err = _I2CReceive (_I2CAddress (), & jsonbuf [jsonbufLen ],
157
+ chunkLen , & available );
152
158
if (err != NULL ) {
153
159
_Free (jsonbuf );
154
160
#ifdef ERRDBG
@@ -159,7 +165,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
159
165
}
160
166
161
167
// We've now received the chunk
162
- jsonbufLen += chunklen ;
168
+ jsonbufLen += chunkLen ;
163
169
164
170
// If the last byte of the chunk is \n, chances are that we're done. However, just so
165
171
// 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)
168
174
receivedNewline = true;
169
175
}
170
176
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 ;
173
182
174
183
// If there's something available on the notecard for us to receive, do it
175
- if (chunklen > 0 ) {
184
+ if (chunkLen > 0 ) {
176
185
continue ;
177
186
}
178
187
@@ -205,7 +214,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
205
214
jsonbuf [jsonbufLen ] = '\0' ;
206
215
207
216
// Return it
208
- * jsonResponse = jsonbuf ;
217
+ * jsonResponse = ( char * ) jsonbuf ;
209
218
return NULL ;
210
219
}
211
220
@@ -242,16 +251,16 @@ bool i2cNoteReset()
242
251
for (retries = 0 ; transmitErr == NULL && !notecardReady && retries < 3 ; retries ++ ) {
243
252
244
253
// Loop to drain all chunks of data that may be ready to transmit to us
245
- int chunklen = 0 ;
254
+ uint16_t chunkLen = 0 ;
246
255
while (true) {
247
256
248
257
// Read the next chunk of available data
249
258
uint32_t available ;
250
259
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 ;
253
262
_DelayIO ();
254
- const char * err = _I2CReceive (_I2CAddress (), buffer , chunklen , & available );
263
+ const char * err = _I2CReceive (_I2CAddress (), buffer , chunkLen , & available );
255
264
if (err ) {
256
265
break ;
257
266
}
@@ -262,8 +271,10 @@ bool i2cNoteReset()
262
271
break ;
263
272
}
264
273
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 ;
267
278
268
279
}
269
280
0 commit comments