@@ -1131,7 +1131,7 @@ ssize_t nanocoap_get_blockwise_to_buf(nanocoap_sock_t *sock, const char *path,
11311131 return (res < 0 ) ? (ssize_t )res : (ssize_t )_buf .len ;
11321132}
11331133
1134- int nanocoap_server (sock_udp_ep_t * local , uint8_t * buf , size_t bufsize )
1134+ int nanocoap_server (sock_udp_ep_t * local , void * rsp_buf , size_t rsp_buf_len )
11351135{
11361136 sock_udp_t sock ;
11371137 sock_udp_ep_t remote ;
@@ -1145,11 +1145,20 @@ int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
11451145
11461146 ssize_t res = sock_udp_create (& sock , local , NULL , 0 );
11471147 if (res != 0 ) {
1148- return -1 ;
1148+ return res ;
11491149 }
11501150
1151+ void * buf ;
1152+ void * buf_ctx = NULL ;
1153+
11511154 while (1 ) {
11521155
1156+ if (buf_ctx ) {
1157+ /* free the buffer */
1158+ res = sock_udp_recv_buf_aux (& sock , & buf , & buf_ctx , 0 , NULL , NULL );
1159+ assert (res == 0 );
1160+ }
1161+
11531162 sock_udp_aux_rx_t * aux_in_ptr = NULL ;
11541163#ifdef MODULE_SOCK_AUX_LOCAL
11551164 sock_udp_aux_rx_t aux_in = {
@@ -1158,14 +1167,14 @@ int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
11581167 aux_in_ptr = & aux_in ;
11591168#endif
11601169
1161- res = sock_udp_recv_aux (& sock , buf , bufsize , SOCK_NO_TIMEOUT ,
1162- & remote , aux_in_ptr );
1170+ res = sock_udp_recv_buf_aux (& sock , & buf , & buf_ctx , SOCK_NO_TIMEOUT ,
1171+ & remote , aux_in_ptr );
11631172 if (res <= 0 ) {
11641173 DEBUG ("nanocoap: error receiving UDP packet %" PRIdSIZE "\n" , res );
11651174 continue ;
11661175 }
11671176 coap_pkt_t pkt ;
1168- if (coap_parse (& pkt , ( uint8_t * ) buf , res ) < 0 ) {
1177+ if (coap_parse (& pkt , buf , res ) < 0 ) {
11691178 DEBUG ("nanocoap: error parsing packet\n" );
11701179 continue ;
11711180 }
@@ -1183,12 +1192,12 @@ int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
11831192 }
11841193 ctx .local = & aux_in .local ;
11851194#endif
1186- if ((res = coap_handle_req (& pkt , buf , bufsize , & ctx )) <= 0 ) {
1195+ if ((res = coap_handle_req (& pkt , rsp_buf , rsp_buf_len , & ctx )) <= 0 ) {
11871196 DEBUG ("nanocoap: error handling request %" PRIdSIZE "\n" , res );
11881197 continue ;
11891198 }
11901199
1191- sock_udp_send_aux (& sock , buf , res , & remote , aux_out_ptr );
1200+ sock_udp_send_aux (& sock , rsp_buf , res , & remote , aux_out_ptr );
11921201 }
11931202
11941203 return 0 ;
0 commit comments