@@ -378,6 +378,186 @@ static int NetRead(void *context, byte* buf, int buf_len,
378378}
379379
380380
381+ /* -------------------------------------------------------------------------- */
382+ /* NETX SOCKET BACKEND EXAMPLE */
383+ /* -------------------------------------------------------------------------- */
384+ #elif defined(HAVE_NETX )
385+
386+ static int NetDisconnect (void * context )
387+ {
388+ SocketContext * sock = (SocketContext * )context ;
389+ if (sock ) {
390+ nx_tcp_socket_disconnect (& sock -> fd , NX_NO_WAIT );
391+ nx_tcp_socket_delete (& sock -> fd );
392+
393+ sock -> stat = SOCK_BEGIN ;
394+ }
395+ return 0 ;
396+ }
397+
398+ static int NetConnect (void * context , const char * host , word16 port ,
399+ int timeout_ms )
400+ {
401+ SocketContext * sock = (SocketContext * )context ;
402+ int rc = MQTT_CODE_ERROR_NETWORK ;
403+ MQTTCtx * mqttCtx = sock -> mqttCtx ;
404+ UINT status ;
405+ NXD_ADDRESS ipAddress ;
406+
407+ /* Get address information for host and locate IPv4 */
408+ switch (sock -> stat ) {
409+ case SOCK_BEGIN :
410+ {
411+ if (mqttCtx -> debug_on ) {
412+ PRINTF ("NetConnect: Host %s, Port %u, Timeout %d ms, "
413+ "Use TLS %d" , host , port , timeout_ms , mqttCtx -> use_tls );
414+ }
415+ #ifdef HAVE_NETX_DNS
416+ /* Convert hostname to IP address using NETX DUO DNS */
417+ status = nxd_dns_host_by_name_get (sock -> ipPtr , (UCHAR * )host , & ipAddress , timeout_ms );
418+ if (status != NX_SUCCESS ) {
419+ PRINTF ("DNS lookup failed: %d" , status );
420+ return MQTT_CODE_ERROR_NETWORK ;
421+ }
422+ #else
423+ PRINTF ("DNS lookup not avilable" );
424+ return MQTT_CODE_ERROR_NETWORK ;
425+ #endif
426+ status = nx_tcp_socket_create (sock -> ipPtr , & sock -> fd ,
427+ "MQTT Socket" , NX_IP_NORMAL ,
428+ NX_FRAGMENT_OKAY , NX_IP_TIME_TO_LIVE ,
429+ 1024 , NX_NULL , NX_NULL );
430+ if (status != NX_SUCCESS ) {
431+ PRINTF ("Socket create failed: %d" , status );
432+ return MQTT_CODE_ERROR_NETWORK ;
433+ }
434+
435+ /* Bind the socket to a local port */
436+ status = nx_tcp_client_socket_bind (& sock -> fd , port , NX_WAIT_FOREVER );
437+ if (status != NX_SUCCESS ) {
438+ PRINTF ("Socket bind failed: %d" , status );
439+ return MQTT_CODE_ERROR_NETWORK ;
440+ }
441+
442+ sock -> stat = SOCK_CONN ;
443+ }
444+ FALL_THROUGH ;
445+
446+ case SOCK_CONN :
447+ {
448+ /* Connect to server using NETX DUO */
449+ status = nxd_tcp_client_socket_connect (& sock -> fd , & ipAddress , port , timeout_ms );
450+ if (status != NX_SUCCESS ) {
451+ if (status == NX_WAIT_ABORTED ) {
452+ return MQTT_CODE_CONTINUE ;
453+ }
454+ PRINTF ("Socket connect failed: %d" , status );
455+ NetDisconnect (context );
456+ return MQTT_CODE_ERROR_NETWORK ;
457+ }
458+ return MQTT_CODE_SUCCESS ;
459+ }
460+
461+ default :
462+ rc = MQTT_CODE_ERROR_BAD_ARG ;
463+ break ;
464+ } /* switch */
465+
466+ return rc ;
467+ }
468+
469+
470+ static int NetWrite (void * context , const byte * buf , int buf_len ,
471+ int timeout_ms )
472+ {
473+ SocketContext * sock = (SocketContext * )context ;
474+ NX_PACKET * packet ;
475+ NX_PACKET_POOL * pool ; /* shorthand */
476+ UINT status ;
477+
478+ if (sock == NULL ) {
479+ PRINTF ("NetX Send NULL parameters" );
480+ return MQTT_CODE_ERROR_BAD_ARG ;
481+ }
482+
483+ pool = sock -> fd .nx_tcp_socket_ip_ptr -> nx_ip_default_packet_pool ;
484+ status = nx_packet_allocate (pool , & packet , NX_TCP_PACKET ,
485+ timeout_ms );
486+ if (status != NX_SUCCESS ) {
487+ PRINTF ("NetX Send packet alloc error" );
488+ return MQTT_CODE_ERROR_NETWORK ;
489+ }
490+
491+ status = nx_packet_data_append (packet , (VOID * )buf , buf_len , pool , timeout_ms );
492+ if (status != NX_SUCCESS ) {
493+ nx_packet_release (packet );
494+ PRINTF ("NetX Send data append error" );
495+ return MQTT_CODE_ERROR_NETWORK ;
496+ }
497+
498+ status = nx_tcp_socket_send (& sock -> fd , packet , timeout_ms );
499+ if (status != NX_SUCCESS ) {
500+ nx_packet_release (packet );
501+ PRINTF ("NetX Send socket send error" );
502+ return MQTT_CODE_ERROR_NETWORK ;
503+ }
504+
505+ return buf_len ;
506+ }
507+
508+
509+ static int NetRead (void * context , byte * buf , int buf_len ,
510+ int timeout_ms )
511+ {
512+ SocketContext * sock = (SocketContext * )context ;
513+ ULONG left ;
514+ ULONG total ;
515+ ULONG copied = 0 ;
516+ UINT status ;
517+
518+ if (sock == NULL ) {
519+ PRINTF ("NetX Recv NULL parameters" );
520+ return MQTT_CODE_ERROR_BAD_ARG ;
521+ }
522+
523+ if (sock -> nxPacket == NULL ) {
524+ status = nx_tcp_socket_receive (& sock -> fd , & sock -> nxPacket ,
525+ timeout_ms );
526+ if (status != NX_SUCCESS ) {
527+ PRINTF ("NetX Recv receive error" );
528+ return MQTT_CODE_ERROR_NETWORK ;
529+ }
530+ }
531+
532+ if (sock -> nxPacket ) {
533+ status = nx_packet_length_get (sock -> nxPacket , & total );
534+ if (status != NX_SUCCESS ) {
535+ PRINTF ("NetX Recv length get error" );
536+ return MQTT_CODE_ERROR_NETWORK ;
537+ }
538+
539+ left = total - sock -> nxOffset ;
540+ status = nx_packet_data_extract_offset (sock -> nxPacket , sock -> nxOffset ,
541+ buf , buf_len , & copied );
542+ if (status != NX_SUCCESS ) {
543+ PRINTF ("NetX Recv data extract offset error" );
544+ return MQTT_CODE_ERROR_NETWORK ;
545+ }
546+
547+ sock -> nxOffset += copied ;
548+
549+ if (copied == left ) {
550+ PRINTF ("NetX Recv Drained packet" );
551+ nx_packet_release (sock -> nxPacket );
552+ sock -> nxPacket = NULL ;
553+ sock -> nxOffset = 0 ;
554+ }
555+ }
556+
557+ return copied ;
558+ }
559+
560+
381561/* -------------------------------------------------------------------------- */
382562/* CURL EASY SOCKET BACKEND EXAMPLE */
383563/* -------------------------------------------------------------------------- */
@@ -1606,7 +1786,9 @@ int MqttClientNet_Init(MqttNet* net, MQTTCtx* mqttCtx)
16061786#if defined(ENABLE_MQTT_CURL )
16071787 sockCtx -> curl = NULL ;
16081788#endif
1789+ #if !defined(HAVE_NETX )
16091790 sockCtx -> fd = SOCKET_INVALID ;
1791+ #endif
16101792 sockCtx -> stat = SOCK_BEGIN ;
16111793 sockCtx -> mqttCtx = mqttCtx ;
16121794
0 commit comments