@@ -558,39 +558,84 @@ int memfault_zephyr_port_ota_update(const sMemfaultOtaUpdateHandler *handler) {
558
558
}
559
559
560
560
int memfault_zephyr_port_post_data (void ) {
561
- int rv = -1 ;
561
+ sMemfaultHttpContext ctx = { 0 } ;
562
562
563
+ int rv = memfault_zephyr_port_http_open_socket (& ctx );
564
+ if (rv < 0 ) {
565
+ return rv ;
566
+ }
567
+
568
+ memfault_zephyr_port_http_upload_sdk_data (& ctx );
569
+
570
+ memfault_zephyr_port_http_close_socket (& ctx );
571
+
572
+ return 0 ;
573
+ }
574
+
575
+ int memfault_zephyr_port_http_open_socket (sMemfaultHttpContext * ctx ) {
563
576
const char * host = MEMFAULT_HTTP_GET_CHUNKS_API_HOST ();
564
- const int port = MEMFAULT_HTTP_GET_CHUNKS_API_PORT ();
577
+ const int port = MEMFAULT_HTTP_GET_CHUNKS_API_PORT ();
565
578
566
- struct addrinfo * res = NULL ;
567
- const int sock_fd = prv_open_socket (& res , host , port );
568
- if (sock_fd < 0 ) {
569
- goto cleanup ;
579
+ memfault_zephyr_port_http_close_socket (ctx );
580
+
581
+ ctx -> sock_fd = prv_open_socket (& (ctx -> res ), host , port );
582
+
583
+ if (ctx -> sock_fd < 0 ) {
584
+ memfault_zephyr_port_http_close_socket (ctx );
585
+ return -1 ;
570
586
}
571
587
588
+ return 0 ;
589
+ }
590
+
591
+ void memfault_zephyr_port_http_close_socket (sMemfaultHttpContext * ctx ) {
592
+ if (ctx -> sock_fd > 0 ) {
593
+ close (ctx -> sock_fd );
594
+ }
595
+ ctx -> sock_fd = 0 ;
596
+
597
+ if (ctx -> res != NULL ) {
598
+ freeaddrinfo (ctx -> res );
599
+ ctx -> res = NULL ;
600
+ }
601
+ }
602
+
603
+ bool memfault_zephyr_port_http_is_connected (sMemfaultHttpContext * ctx ) { return ctx -> sock_fd > 0 ; }
604
+
605
+ void memfault_zephyr_port_http_upload_sdk_data (sMemfaultHttpContext * ctx ) {
572
606
int max_messages_to_send = 5 ;
573
607
#if CONFIG_MEMFAULT_HTTP_MAX_POST_SIZE && CONFIG_MEMFAULT_RAM_BACKED_COREDUMP
574
608
// The largest data type we will send is a coredump. If CONFIG_MEMFAULT_HTTP_MAX_POST_SIZE
575
609
// is being used, make sure we issue enough HTTP POSTS such that an entire coredump will be sent.
576
- max_messages_to_send = MEMFAULT_MAX (max_messages_to_send ,
577
- CONFIG_MEMFAULT_RAM_BACKED_COREDUMP_SIZE / CONFIG_MEMFAULT_HTTP_MAX_POST_SIZE );
610
+ max_messages_to_send =
611
+ MEMFAULT_MAX (max_messages_to_send ,
612
+ CONFIG_MEMFAULT_RAM_BACKED_COREDUMP_SIZE / CONFIG_MEMFAULT_HTTP_MAX_POST_SIZE );
578
613
#endif
579
614
580
615
while (max_messages_to_send -- > 0 ) {
581
- if (!prv_send_next_msg (sock_fd )) {
616
+ if (!prv_send_next_msg (ctx -> sock_fd )) {
582
617
break ;
583
618
}
584
- if (!prv_wait_for_http_response (sock_fd )) {
619
+ if (!prv_wait_for_http_response (ctx -> sock_fd )) {
585
620
break ;
586
621
}
587
622
}
623
+ }
588
624
589
- close (sock_fd );
625
+ int memfault_zephyr_port_http_post_chunk (sMemfaultHttpContext * ctx , void * p_data , size_t data_len ) {
626
+ if (!memfault_zephyr_port_http_is_connected (ctx )) {
627
+ return -1 ;
628
+ }
590
629
591
- // if we got here, everything succeeded!
592
- rv = 0 ;
593
- cleanup :
594
- freeaddrinfo (res );
595
- return rv ;
630
+ memfault_http_start_chunk_post (prv_send_data , & (ctx -> sock_fd ), data_len );
631
+
632
+ if (!prv_try_send (ctx -> sock_fd , p_data , data_len )) {
633
+ return -1 ;
634
+ }
635
+
636
+ if (!prv_wait_for_http_response (ctx -> sock_fd )) {
637
+ return -1 ;
638
+ }
639
+
640
+ return 0 ;
596
641
}
0 commit comments