@@ -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() */
0 commit comments