@@ -91,6 +91,8 @@ static void trel_browse_notifier(mdns_result_t *result)
9191
9292static void trel_recv_task (void * ctx )
9393{
94+ esp_err_t ret = ESP_OK ;
95+ OT_UNUSED_VARIABLE (ret );
9496 ot_trel_recv_task_t * task_ctx = (ot_trel_recv_task_t * )ctx ;
9597 struct pbuf * recv_buf = task_ctx -> p ;
9698 uint8_t * data_buf = (uint8_t * )recv_buf -> payload ;
@@ -99,58 +101,47 @@ static void trel_recv_task(void *ctx)
99101
100102 if (recv_buf -> next != NULL ) {
101103 data_buf = (uint8_t * )malloc (recv_buf -> tot_len );
102- if (data_buf != NULL ) {
103- length = recv_buf -> tot_len ;
104- data_buf_to_free = data_buf ;
105- pbuf_copy_partial (recv_buf , data_buf , recv_buf -> tot_len , 0 );
106- } else {
107- ESP_LOGE (OT_PLAT_LOG_TAG , "Failed to allocate data buf when receiving Thread TREL message" );
108- ExitNow ();
109- }
104+ ESP_GOTO_ON_FALSE (data_buf , ESP_ERR_NO_MEM , exit , OT_PLAT_LOG_TAG , "Failed to allocate data buf when receiving Thread TREL message" );
105+ length = recv_buf -> tot_len ;
106+ data_buf_to_free = data_buf ;
107+ pbuf_copy_partial (recv_buf , data_buf , recv_buf -> tot_len , 0 );
110108 }
111109 otPlatTrelHandleReceived (esp_openthread_get_instance (), data_buf , length , task_ctx -> source_addr );
112110
113111exit :
114- if (data_buf_to_free ) {
115- free (data_buf_to_free );
116- }
117- pbuf_free (recv_buf );
118- if (task_ctx ) {
119- free (task_ctx -> source_addr );
112+ if (recv_buf ) {
113+ pbuf_free (recv_buf );
120114 }
115+ free (data_buf_to_free );
116+ free (task_ctx -> source_addr );
121117 free (task_ctx );
122118}
123119
120+ // TZ-1704
124121static void handle_trel_udp_recv (void * ctx , struct udp_pcb * pcb , struct pbuf * p , const ip_addr_t * addr , uint16_t port )
125122{
123+ esp_err_t ret = ESP_OK ;
126124 ESP_LOGD (OT_PLAT_LOG_TAG , "Receive from %s:%d" , ip6addr_ntoa (& (addr -> u_addr .ip6 )), port );
127125
128126 ot_trel_recv_task_t * task_ctx = (ot_trel_recv_task_t * )malloc (sizeof (ot_trel_recv_task_t ));
129- if (task_ctx == NULL ) {
130- ESP_LOGE (OT_PLAT_LOG_TAG , "Failed to allocate buf for Thread TREL" );
131- ExitNow ();
132- }
127+ ESP_GOTO_ON_FALSE (task_ctx , ESP_ERR_NO_MEM , exit , OT_PLAT_LOG_TAG , "Failed to allocate buf for Thread TREL" );
133128 task_ctx -> p = p ;
134129 task_ctx -> source_addr = (otSockAddr * )malloc (sizeof (otSockAddr ));
135- if (task_ctx -> source_addr == NULL ) {
136- ESP_LOGE (OT_PLAT_LOG_TAG , "Failed to allocate buf for Thread TREL" );
137- ExitNow ();
138- }
130+ ESP_GOTO_ON_FALSE (task_ctx -> source_addr , ESP_ERR_NO_MEM , exit , OT_PLAT_LOG_TAG , "Failed to allocate buf for Thread TREL" );
139131 memset (task_ctx -> source_addr , 0 , sizeof (otSockAddr ));
140132 task_ctx -> source_addr -> mPort = port ;
141133 memcpy (& task_ctx -> source_addr -> mAddress .mFields .m32 , addr -> u_addr .ip6 .addr , sizeof (addr -> u_addr .ip6 .addr ));
142134
143- if (esp_openthread_task_queue_post (trel_recv_task , task_ctx ) != ESP_OK ) {
144- ESP_LOGE (OT_PLAT_LOG_TAG , "Failed to receive OpenThread TREL message" );
145- ExitNow ();
146- }
147- return ;
135+ ESP_GOTO_ON_ERROR (esp_openthread_task_queue_post (trel_recv_task , task_ctx ), exit , OT_PLAT_LOG_TAG , "Failed to receive OpenThread TREL message" );
136+
148137exit :
149- if ( task_ctx ) {
138+ if ( ret != ESP_OK ) {
150139 free (task_ctx -> source_addr );
140+ free (task_ctx );
141+ if (p ) {
142+ pbuf_free (p );
143+ }
151144 }
152- free (task_ctx );
153- pbuf_free (p );
154145}
155146
156147static esp_err_t ot_new_trel (void * ctx )
@@ -176,7 +167,7 @@ void otPlatTrelEnable(otInstance *aInstance, uint16_t *aUdpPort)
176167 esp_openthread_task_switching_lock_acquire (portMAX_DELAY );
177168}
178169
179- static void trel_send_task (void * ctx )
170+ static esp_err_t trel_send_task (void * ctx )
180171{
181172 err_t err = ERR_OK ;
182173 struct pbuf * send_buf = NULL ;
@@ -190,42 +181,31 @@ static void trel_send_task(void *ctx)
190181 task -> pcb -> flags = (task -> pcb -> flags & (~UDP_FLAGS_MULTICAST_LOOP ));
191182 task -> pcb -> local_port = s_ot_trel .port ;
192183 send_buf = pbuf_alloc (PBUF_TRANSPORT , task -> length , PBUF_RAM );
193- if (send_buf == NULL ) {
194- ESP_LOGE (OT_PLAT_LOG_TAG , "Failed to allocate data buf when sending Thread TREL message" );
195- ExitNow ();
196- }
184+ ESP_RETURN_ON_FALSE (send_buf , ESP_ERR_NO_MEM , OT_PLAT_LOG_TAG , "Failed to allocate data buf when sending Thread TREL message" );
197185 memcpy (send_buf -> payload , task -> payload , task -> length );
198186 err = udp_sendto_if (task -> pcb , send_buf , & task -> peer_addr , task -> peer_port , netif_get_by_index (task -> pcb -> netif_idx ));
199- if (err != ERR_OK ) {
200- ESP_LOGE (OT_PLAT_LOG_TAG , "Fail to send trel msg to %s:%d %d (%d)" , ip6addr_ntoa (& (task -> peer_addr .u_addr .ip6 )), task -> peer_port , task -> pcb -> netif_idx , err );
201- }
202- exit :
203187 pbuf_free (send_buf );
204- free (task );
188+ ESP_RETURN_ON_FALSE (err == ERR_OK , ESP_FAIL , OT_PLAT_LOG_TAG , "Fail to send trel msg to %s:%d %d (%d)" , ip6addr_ntoa (& (task -> peer_addr .u_addr .ip6 )), task -> peer_port , task -> pcb -> netif_idx , err );
189+ return ESP_OK ;
205190}
206191
207192void otPlatTrelSend (otInstance * aInstance ,
208193 const uint8_t * aUdpPayload ,
209194 uint16_t aUdpPayloadLen ,
210195 const otSockAddr * aDestSockAddr )
211196{
212- if (!s_trel_netif ) {
213- ESP_LOGE (OT_PLAT_LOG_TAG , "None Thread TREL interface" );
214- return ;
215- }
216- ot_trel_send_task_t * task = (ot_trel_send_task_t * )malloc (sizeof (ot_trel_send_task_t ));
217- if (task == NULL ) {
218- ESP_LOGE (OT_PLAT_LOG_TAG , "Failed to allocate buf for Thread TREL" );
219- return ;
220- }
221- memcpy (task -> peer_addr .u_addr .ip6 .addr , aDestSockAddr -> mAddress .mFields .m32 , OT_IP6_ADDRESS_SIZE );
222- task -> peer_port = aDestSockAddr -> mPort ;
223- ESP_LOGD (OT_PLAT_LOG_TAG , "send trel msg to %s:%d" , ip6addr_ntoa (& (task -> peer_addr .u_addr .ip6 )), task -> peer_port );
224- task -> payload = aUdpPayload ;
225- task -> length = aUdpPayloadLen ;
197+ esp_err_t err = ESP_OK ;
198+ ot_trel_send_task_t task ;
199+
200+ ESP_RETURN_ON_FALSE (s_trel_netif , , OT_PLAT_LOG_TAG , "None Thread TREL interface" );
201+ memcpy (task .peer_addr .u_addr .ip6 .addr , aDestSockAddr -> mAddress .mFields .m32 , OT_IP6_ADDRESS_SIZE );
202+ task .peer_port = aDestSockAddr -> mPort ;
203+ task .payload = aUdpPayload ;
204+ task .length = aUdpPayloadLen ;
226205 esp_openthread_task_switching_lock_release ();
227- tcpip_callback (trel_send_task , task );
206+ err = esp_netif_tcpip_exec (trel_send_task , & task );
228207 esp_openthread_task_switching_lock_acquire (portMAX_DELAY );
208+ ESP_RETURN_ON_FALSE (err == ESP_OK , , OT_PLAT_LOG_TAG , "Failed to send TREL message" );
229209}
230210
231211void otPlatTrelNotifyPeerSocketAddressDifference (otInstance * aInstance ,
0 commit comments