Skip to content

Commit 6ee7bfe

Browse files
committed
Added wolfMQTT client wolfIP support
1 parent 9e75822 commit 6ee7bfe

8 files changed

Lines changed: 267 additions & 106 deletions

File tree

examples/mqttexample.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ typedef struct _MQTTCtx {
200200
unsigned int useNonBlockMode:1; /* set to use non-blocking mode.
201201
network callbacks can return MQTT_CODE_CONTINUE to indicate "would block" */
202202
#endif
203+
#ifdef WOLFMQTT_WOLFIP
204+
struct wolfIP *stack; /* wolfIP TCP/IP stack instance */
205+
#endif
203206
} MQTTCtx;
204207

205208

examples/mqttnet.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,124 @@ static int NetDisconnect(void *context)
11571157
return 0;
11581158
}
11591159

1160+
/* -------------------------------------------------------------------------- */
1161+
/* WOLFIP TCP/IP STACK NETWORK CALLBACK EXAMPLE */
1162+
/* -------------------------------------------------------------------------- */
1163+
#elif defined(WOLFMQTT_WOLFIP)
1164+
1165+
static int NetDisconnect(void *context)
1166+
{
1167+
SocketContext *sock = (SocketContext*)context;
1168+
if (sock) {
1169+
if (sock->fd != SOCKET_INVALID) {
1170+
wolfIP_sock_close(sock->stack, sock->fd);
1171+
sock->fd = SOCKET_INVALID;
1172+
}
1173+
sock->stat = SOCK_BEGIN;
1174+
}
1175+
return 0;
1176+
}
1177+
1178+
static int NetConnect(void *context, const char* host, word16 port,
1179+
int timeout_ms)
1180+
{
1181+
SocketContext *sock = (SocketContext*)context;
1182+
struct wolfIP_sockaddr_in addr;
1183+
int rc;
1184+
1185+
(void)timeout_ms;
1186+
1187+
if (context == NULL || host == NULL) {
1188+
return MQTT_CODE_ERROR_BAD_ARG;
1189+
}
1190+
1191+
switch (sock->stat) {
1192+
case SOCK_BEGIN:
1193+
{
1194+
/* Create TCP socket */
1195+
sock->fd = wolfIP_sock_socket(sock->stack, AF_INET,
1196+
IPSTACK_SOCK_STREAM, 0);
1197+
if (sock->fd < 0) {
1198+
return MQTT_CODE_ERROR_NETWORK;
1199+
}
1200+
1201+
sock->stat = SOCK_CONN;
1202+
}
1203+
FALL_THROUGH;
1204+
1205+
case SOCK_CONN:
1206+
{
1207+
/* Set up address and initiate connect */
1208+
XMEMSET(&addr, 0, sizeof(addr));
1209+
addr.sin_family = AF_INET;
1210+
addr.sin_port = ee16(port);
1211+
addr.sin_addr.s_addr = atoip4(host);
1212+
1213+
rc = wolfIP_sock_connect(sock->stack, sock->fd,
1214+
(struct wolfIP_sockaddr *)&addr, sizeof(addr));
1215+
if (rc == 0) {
1216+
return MQTT_CODE_SUCCESS;
1217+
}
1218+
if (rc == -WOLFIP_EAGAIN || rc == -11) {
1219+
return MQTT_CODE_CONTINUE;
1220+
}
1221+
1222+
/* Connection failed */
1223+
NetDisconnect(context);
1224+
return MQTT_CODE_ERROR_NETWORK;
1225+
}
1226+
1227+
default:
1228+
break;
1229+
}
1230+
1231+
return MQTT_CODE_ERROR_NETWORK;
1232+
}
1233+
1234+
static int NetRead(void *context, byte* buf, int buf_len, int timeout_ms)
1235+
{
1236+
SocketContext *sock = (SocketContext*)context;
1237+
int rc;
1238+
1239+
(void)timeout_ms;
1240+
1241+
if (context == NULL || buf == NULL || buf_len <= 0) {
1242+
return MQTT_CODE_ERROR_BAD_ARG;
1243+
}
1244+
1245+
rc = wolfIP_sock_recv(sock->stack, sock->fd, buf, buf_len, 0);
1246+
if (rc == -WOLFIP_EAGAIN || rc == -1) {
1247+
return MQTT_CODE_CONTINUE;
1248+
}
1249+
if (rc <= 0) {
1250+
return MQTT_CODE_ERROR_NETWORK;
1251+
}
1252+
return rc;
1253+
}
1254+
1255+
static int NetWrite(void *context, const byte* buf, int buf_len,
1256+
int timeout_ms)
1257+
{
1258+
SocketContext *sock = (SocketContext*)context;
1259+
int rc;
1260+
1261+
(void)timeout_ms;
1262+
1263+
if (context == NULL || buf == NULL || buf_len <= 0) {
1264+
return MQTT_CODE_ERROR_BAD_ARG;
1265+
}
1266+
1267+
rc = wolfIP_sock_send(sock->stack, sock->fd, buf, buf_len, 0);
1268+
if (rc == -WOLFIP_EAGAIN || rc == -1) {
1269+
return MQTT_CODE_CONTINUE;
1270+
}
1271+
if (rc <= 0) {
1272+
return MQTT_CODE_ERROR_NETWORK;
1273+
}
1274+
return rc;
1275+
}
1276+
1277+
11601278
/* -------------------------------------------------------------------------- */
11611279
/* GENERIC BSD SOCKET TCP NETWORK CALLBACK EXAMPLE */
11621280
/* -------------------------------------------------------------------------- */
@@ -1807,6 +1925,11 @@ int MqttClientNet_Init(MqttNet* net, MQTTCtx* mqttCtx)
18071925
#endif
18081926
sockCtx->stat = SOCK_BEGIN;
18091927
sockCtx->mqttCtx = mqttCtx;
1928+
#ifdef WOLFMQTT_WOLFIP
1929+
if (mqttCtx != NULL) {
1930+
sockCtx->stack = mqttCtx->stack;
1931+
}
1932+
#endif
18101933

18111934
#if defined(WOLFMQTT_MULTITHREAD) && defined(WOLFMQTT_ENABLE_STDIN_CAP)
18121935
/* setup the pipe for waking select() */

examples/mqttnet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ typedef struct _SocketContext {
7070
NX_IP *ipPtr;
7171
NX_PACKET *nxPacket;
7272
ULONG nxOffset;
73+
#endif
74+
#ifdef WOLFMQTT_WOLFIP
75+
struct wolfIP *stack;
7376
#endif
7477
MQTTCtx* mqttCtx;
7578
} SocketContext;

examples/mqttport.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ extern "C" {
7373
#include "lwip/sockets.h"
7474
#include "lwip/netdb.h"
7575

76+
/* wolfIP TCP/IP stack */
77+
#elif defined(WOLFMQTT_WOLFIP)
78+
#include "wolfip.h"
79+
80+
#define SOCKET_T int
81+
#define SOCKET_INVALID (-1)
82+
#define SOCK_ADDR_IN struct wolfIP_sockaddr_in
83+
#define NO_FILESYSTEM
84+
7685
/* User defined IO */
7786
#elif defined(WOLFMQTT_USER_IO)
7887
#include "userio_template.h"

0 commit comments

Comments
 (0)