Skip to content

Commit 954ad42

Browse files
add port for NETX
1 parent 320ed37 commit 954ad42

5 files changed

Lines changed: 197 additions & 2 deletions

File tree

examples/mqttexample.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ int mqtt_check_timeout(int rc, word32* start_sec, word32 timeout_sec)
584584
#endif /* WOLFMQTT_NONBLOCK */
585585

586586

587-
#ifdef ENABLE_MQTT_TLS
587+
#if defined(ENABLE_MQTT_TLS) && !defined(EXTERNAL_MQTT_TLS_CALLBACK)
588588

589589
#ifdef WOLFSSL_ENCRYPTED_KEYS
590590
int mqtt_password_cb(char* passwd, int sz, int rw, void* userdata)

examples/mqttexample.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
#endif
9999

100100
/* certs are either static or extern, depending on the specific example */
101+
#ifndef EXTERNAL_MQTT_TLS_CALLBACK
101102
#ifdef WOLFMQTT_EXTERN_CERT
102103
#undef WOLFMQTT_EXAMPLE_CERT
103104
#define WOLFMQTT_EXAMPLE_CERT /* init extern from mqttexample.h */
@@ -108,7 +109,7 @@
108109
#undef WOLFMQTT_EXAMPLE_CERT
109110
#define WOLFMQTT_EXAMPLE_CERT static
110111
#endif
111-
112+
#endif /* !EXTERNAL_MQTT_TLS_CALLBACK */
112113
/* MQTT Client state */
113114
typedef enum _MQTTCtxState {
114115
WMQ_BEGIN = 0,

examples/mqttnet.c

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

examples/mqttnet.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ typedef struct _SocketContext {
5555
#endif
5656
#ifdef ENABLE_MQTT_WEBSOCKET
5757
void* websocket_ctx;
58+
#endif
59+
#ifdef HAVE_NETX
60+
NX_IP *ipPtr;
61+
NX_PACKET *nxPacket;
62+
ULONG nxOffset;
5863
#endif
5964
MQTTCtx* mqttCtx;
6065
} SocketContext;

examples/mqttport.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ extern "C" {
7777
#elif defined(WOLFMQTT_USER_IO)
7878
#include "userio_template.h"
7979

80+
/* NetX */
81+
#elif defined(HAVE_NETX)
82+
#include "nx_api.h"
83+
84+
#define SOCKET_T NX_TCP_SOCKET
85+
#define SOCK_ADDR_IN NXD_ADDRESS
86+
8087
/* Windows */
8188
#elif defined(USE_WINDOWS_API)
8289
#include <winsock2.h>

0 commit comments

Comments
 (0)