@@ -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/**************************************************************************/
4848const 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
0 commit comments