@@ -165,6 +165,36 @@ static char PART[ RECV_BUFFER_LEN ];
165165 */
166166static TlsTransportStatus_t nce_connect ( NetworkContext_t * pxNetworkContext );
167167
168+ /*-----------------------------------------------------------*/
169+ #ifdef democonfigRANGE_SIZE
170+
171+ /**
172+ * @brief The total byte length of the original response.
173+ *
174+ * @param[in] PART: A buffer to save received data (response header).
175+ *
176+ * @return the total byte of the original response.
177+ */
178+
179+ static int response_length ( static char * PART );
180+
181+ /**
182+ * @brief request the onboarding service with a range defined in demo_config.
183+ *
184+ * @param[in] status: The status of previous operations.
185+ *
186+ * @param[in] pxNetworkContext The parameter network context.
187+ *
188+ * @param[in] completeResponse: A buffer to collect complete onboarding response.
189+ *
190+ *
191+ * @return The status of the onboarding request.
192+ */
193+ static uint8_t onboarding_request ( uint8_t status ,
194+ NetworkContext_t * pxNetworkContext ,
195+ char * * completeResponse );
196+ #endif
197+
168198/**
169199 * @brief re-initialize MQTT connection information
170200 * from received onboarding response.
@@ -220,63 +250,72 @@ uint8_t nce_onboard( char ** pThingName,
220250 }
221251
222252 /* Build onboarding request. */
223- char packetToSent [ 100 ];
224-
225- memset ( packetToSent , '\0' , 100 * sizeof ( char ) );
226- sprintf ( packetToSent , "GET /device-api/onboarding HTTP/1.1\r\n"
227- "Host: %s\r\n"
228- "Accept: text/csv\r\n\r\n" , ONBOARDING_ENDPOINT );
229- LogInfo ( ( "Send onboarding request:\r\n%.*s" ,
230- strlen ( packetToSent ),
231- packetToSent ) );
232-
233- /* Send onboarding request. */
234- int32_t sentBytes = TLS_FreeRTOS_send ( & xNetworkContext ,
235- & packetToSent ,
236- strlen ( packetToSent ) );
253+ #ifdef democonfigRANGE_SIZE
254+ status = onboarding_request ( status , & xNetworkContext , & completeResponse );
237255
238- configASSERT ( sentBytes > 0 );
256+ if ( status == EXIT_FAILURE )
257+ {
258+ return status ;
259+ }
260+ #else
261+ char packetToSent [ 100 ];
239262
240- if ( sentBytes <= 0 )
241- {
242- LogError ( ( "Failed to send onboarding request." ) );
243- return status ;
244- }
263+ memset ( packetToSent , '\0' , 100 * sizeof ( char ) );
264+ sprintf ( packetToSent , "GET /device-api/onboarding HTTP/1.1\r\n"
265+ "Host: %s\r\n"
266+ "Accept: text/csv\r\n\r\n" , ONBOARDING_ENDPOINT );
267+ LogInfo ( ( "Send onboarding request:\r\n%.*s" ,
268+ strlen ( packetToSent ),
269+ packetToSent ) );
245270
246- /* Receive onboarding response . */
247- int32_t recvBytes = TLS_FreeRTOS_recv ( & xNetworkContext ,
248- & PART [ 0 ] ,
249- RECV_BUFFER_LEN );
271+ /* Send onboarding request . */
272+ int32_t sentBytes = TLS_FreeRTOS_send ( & xNetworkContext ,
273+ & packetToSent ,
274+ strlen ( packetToSent ) );
250275
251- if ( recvBytes < 0 )
252- {
253- LogError ( ( "Failed to receive onboarding response." ) );
254- return status ;
255- }
276+ configASSERT ( sentBytes > 0 );
256277
257- LogDebug ( ( "Received raw response: %d bytes." , recvBytes ) );
258- LogDebug ( ( "\r\n%.*s" , recvBytes , PART ) );
259- strcat ( completeResponse ,
260- strstr ( PART , "Express\r\n\r\n\"" ) + strlen ( "Express\r\n\r\n\"" ) ) ;
261- memset ( PART , ( int8_t ) '\0' , sizeof ( PART ) );
278+ if ( sentBytes <= 0 )
279+ {
280+ LogError ( ( "Failed to send onboarding request." ) );
281+ return status ;
282+ }
262283
263- while ( recvBytes == RECV_BUFFER_LEN )
264- {
265- recvBytes = TLS_FreeRTOS_recv ( & xNetworkContext ,
266- & PART [ 0 ],
267- RECV_BUFFER_LEN );
284+ /* Receive onboarding response. */
285+ int32_t recvBytes = TLS_FreeRTOS_recv ( & xNetworkContext ,
286+ & PART [ 0 ],
287+ RECV_BUFFER_LEN );
268288
269289 if ( recvBytes < 0 )
270290 {
271291 LogError ( ( "Failed to receive onboarding response." ) );
272292 return status ;
273293 }
274294
275- LogDebug ( ( "Received raw response: %d bytes." , strlen ( PART ) ) );
276- LogDebug ( ( "\r\n%.*s" , strlen ( PART ), PART ) );
277- strcat ( completeResponse , PART );
295+ LogDebug ( ( "Received raw response: %d bytes." , recvBytes ) );
296+ LogDebug ( ( "\r\n%.*s" , recvBytes , PART ) );
297+ strcat ( completeResponse ,
298+ strstr ( PART , "Express\r\n\r\n" ) + strlen ( "Express\r\n\r\n" ) );
278299 memset ( PART , ( int8_t ) '\0' , sizeof ( PART ) );
279- }
300+
301+ while ( recvBytes == RECV_BUFFER_LEN )
302+ {
303+ recvBytes = TLS_FreeRTOS_recv ( & xNetworkContext ,
304+ & PART [ 0 ],
305+ RECV_BUFFER_LEN );
306+
307+ if ( recvBytes < 0 )
308+ {
309+ LogError ( ( "Failed to receive onboarding response." ) );
310+ return status ;
311+ }
312+
313+ LogDebug ( ( "Received raw response: %d bytes." , strlen ( PART ) ) );
314+ LogDebug ( ( "\r\n%.*s" , strlen ( PART ), PART ) );
315+ strcat ( completeResponse , PART );
316+ memset ( PART , ( int8_t ) '\0' , sizeof ( PART ) );
317+ }
318+ #endif /* ifdef democonfigRANGE_SIZE */
280319
281320 LogInfo ( ( " Onboarding response is received." ) );
282321
@@ -411,7 +450,8 @@ static uint8_t nceReinitConnParams( char * completeResponse,
411450 return status ;
412451 }
413452
414- memcpy ( nceThingName , token , strSize );
453+ /* In the token we have now "ICCID" so we add 1 to start from the first number of ICCID */
454+ memcpy ( nceThingName , token + 1 , strSize );
415455 LogDebug ( ( "Thing name is: %s." , nceThingName ) );
416456
417457 /* Walk through other tokens. */
@@ -596,5 +636,117 @@ char * str_replace( char * orig,
596636 strcpy ( tmp , orig );
597637 return result ;
598638}
639+ /*-----------------------------------------------------------*/
640+ #ifdef democonfigRANGE_SIZE
641+ static int response_length ( char * PART )
642+ {
643+ int pch = strstr ( PART , "bytes" );
644+
645+ int rangeStart , rangeEnd , rangeOriginalSize ;
646+
647+ if ( 3 == sscanf ( pch ,
648+ "%*[^0123456789]%d%*[^0123456789]%d%*[^0123456789]%d" ,
649+ & rangeStart ,
650+ & rangeEnd ,
651+ & rangeOriginalSize ) )
652+ {
653+ LogDebug ( ( "The size of the onboarding response is: %d\r\n" ,
654+ rangeOriginalSize ) );
655+ return rangeOriginalSize ;
656+ }
657+ else
658+ {
659+ return EXIT_FAILURE ;
660+ }
661+ }
662+ /*-----------------------------------------------------------*/
599663
664+ static uint8_t onboarding_request ( uint8_t status ,
665+ NetworkContext_t * pxNetworkContext ,
666+ char * * completeResponse )
667+ {
668+ char packetToSent [ 130 ];
669+ int rangeStart = 0 ;
670+ int rangeEnd = democonfigRANGE_SIZE ;
671+ int rangeOriginalSize ;
672+
673+ memset ( packetToSent , '\0' , 130 * sizeof ( char ) );
674+
675+ do
676+ {
677+ sprintf ( packetToSent , "GET /device-api/onboarding HTTP/1.1\r\n"
678+ "Host: %s\r\n"
679+ "Range: bytes=%d-%d\r\n"
680+ "Accept: text/csv\r\n\r\n" , ONBOARDING_ENDPOINT , rangeStart , rangeEnd );
681+ LogInfo ( ( "Send onboarding request:\r\n%.*s" ,
682+ strlen ( packetToSent ),
683+ packetToSent ) );
684+
685+ /* Send onboarding request. */
686+ int32_t sentBytes = TLS_FreeRTOS_send ( & xNetworkContext ,
687+ & packetToSent ,
688+ strlen ( packetToSent ) );
689+
690+ configASSERT ( sentBytes > 0 );
691+
692+ if ( sentBytes <= 0 )
693+ {
694+ LogError ( ( "Failed to send onboarding request." ) );
695+ return status ;
696+ }
697+
698+ /* Receive onboarding response. */
699+ int32_t recvBytes = TLS_FreeRTOS_recv ( & xNetworkContext ,
700+ & PART [ 0 ],
701+ RECV_BUFFER_LEN );
702+
703+ if ( rangeEnd == democonfigRANGE_SIZE )
704+ {
705+ rangeOriginalSize = response_length ( PART );
706+
707+ if ( rangeOriginalSize == EXIT_FAILURE )
708+ {
709+ LogError ( ( "Failed to get complete onboarding response size." ) );
710+ return status ;
711+ }
712+ }
713+
714+ if ( recvBytes < 0 )
715+ {
716+ LogError ( ( "Failed to receive onboarding response." ) );
717+ return status ;
718+ }
719+
720+ LogDebug ( ( "Received raw response: %d bytes." , recvBytes ) );
721+ LogDebug ( ( "\r\n%.*s" , recvBytes , PART ) );
722+ strcat ( completeResponse ,
723+ strstr ( PART , "Express\r\n\r\n" ) + strlen ( "Express\r\n\r\n" ) );
724+ memset ( PART , ( int8_t ) '\0' , sizeof ( PART ) );
725+
726+ while ( recvBytes == RECV_BUFFER_LEN )
727+ {
728+ recvBytes = TLS_FreeRTOS_recv ( & xNetworkContext ,
729+ & PART [ 0 ],
730+ RECV_BUFFER_LEN );
731+
732+ if ( recvBytes < 0 )
733+ {
734+ LogError ( ( "Failed to receive onboarding response." ) );
735+ return status ;
736+ }
737+
738+ LogDebug ( ( "Received raw response: %d bytes." , strlen ( PART ) ) );
739+ LogDebug ( ( "\r\n%.*s" , strlen ( PART ), PART ) );
740+ strcat ( completeResponse , PART );
741+ memset ( PART , ( int8_t ) '\0' , sizeof ( PART ) );
742+ }
743+
744+ rangeStart = rangeEnd + 1 ;
745+ rangeEnd += democonfigRANGE_SIZE ;
746+ } while ( rangeStart < rangeOriginalSize );
747+
748+ status = EXIT_SUCCESS ;
749+ return status ;
750+ }
751+ #endif /* ifdef democonfigRANGE_SIZE */
600752/*-----------------------------------------------------------*/
0 commit comments