1010#include "u_tx_general.h"
1111#include <string.h>
1212#include <stdio.h>
13+ #include <sys/_types.h>
1314#if ETH_ENABLE_MQTT
1415#include "nxd_mqtt_client.h"
1516#endif
2526
2627/* The DEFAULT_PAYLOAD_SIZE should match with RxBuffLen configured via MX_ETH_Init */
2728#define DEFAULT_PAYLOAD_SIZE 1524
28- #define NX_APP_PACKET_POOL_SIZE ((DEFAULT_PAYLOAD_SIZE + sizeof(NX_PACKET)) * 10 )
29- #define MQTT_CLIENT_STACK_SIZE 4096
29+ #define NX_APP_PACKET_POOL_SIZE ((DEFAULT_PAYLOAD_SIZE + sizeof(NX_PACKET)) * 100 )
30+ #define MQTT_CLIENT_STACK_SIZE 8192
3031
3132extern ETH_HandleTypeDef heth ;
3233
3334/* DEVICE INFO */
3435typedef struct {
35- /* NetX Objects */
36+ uint8_t node_id ;
37+
38+ NX_IP ip ;
39+ UCHAR ip_memory [_IP_THREAD_STACK_SIZE ];
40+ DriverFunction
41+ driver ; /* Set by the user. Used to communicate with the driver layer. */
42+ OnRecieve on_recieve ; /* Set by the user. Called when a message is recieved. */
43+
44+ NX_PACKET_POOL packet_pool ;
45+ UCHAR packet_pool_memory [NX_APP_PACKET_POOL_SIZE ];
46+
47+ UCHAR arp_cache_memory [_ARP_CACHE_SIZE ];
48+
3649 #if ETH_ENABLE_MANUAL_UDP_MULTICAST
3750 NX_UDP_SOCKET socket ;
3851 #endif
52+
3953 #if ETH_ENABLE_MQTT
4054 NXD_MQTT_CLIENT mqtt_client ;
4155 UCHAR mqtt_thread_stack [MQTT_CLIENT_STACK_SIZE / sizeof (ULONG )];
4256 #endif
43- NX_PACKET_POOL packet_pool ;
44- NX_IP ip ;
57+
58+
4559 NX_PTP_CLIENT ptp_client ;
4660 SHORT ptp_utc_offset ;
47-
48- /* Static memory for NetX stuff */
49- UCHAR packet_pool_memory [NX_APP_PACKET_POOL_SIZE ];
50- UCHAR ip_memory [_IP_THREAD_STACK_SIZE ];
51- UCHAR arp_cache_memory [_ARP_CACHE_SIZE ];
5261 ULONG ptp_stack [2048 / sizeof (ULONG )];
5362
54- /* Device config variables */
5563 bool is_initialized ;
56- uint8_t node_id ;
57- DriverFunction
58- driver ; /* Set by the user. Used to communicate with the driver layer. */
59- OnRecieve on_recieve ; /* Set by the user. Called when a message is recieved. */
6064} _ethernet_device_t ;
6165static _ethernet_device_t device = { 0 };
6266
6367/* CALLBACK FUNCTIONS */
6468#if ETH_ENABLE_MQTT
6569static VOID _mqtt_disconnect_callback (NXD_MQTT_CLIENT * client_ptr )
6670{
67- printf ("client disconnected from server\n" );
71+ PRINTLN_WARNING ("client disconnected from server\n" );
6872}
6973
7074static VOID _mqtt_recieve_callback (NXD_MQTT_CLIENT * client_ptr , UINT number_of_messages )
@@ -231,16 +235,6 @@ UINT ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve on_
231235 return status ;
232236 }
233237
234- status = nx_arp_gratuitous_send (
235- & device .ip , // IP instance
236- NX_NULL
237- );
238- if (status != NX_SUCCESS ) {
239- PRINTLN_ERROR ("Failed to enable ARP (Status: %d/%s)." , status , nx_status_toString (status ));
240- return status ;
241- }
242-
243-
244238
245239 /* Enable igmp */
246240#if ETH_ENABLE_IGMP || ETH_ENABLE_MANUAL_UDP_MULTICAST
@@ -264,6 +258,12 @@ UINT ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve on_
264258 return status ;
265259 }
266260
261+ status = nx_tcp_enable (& device .ip );
262+ if (status != NX_SUCCESS ) {
263+ PRINTLN_ERROR ("Failed to enable TCP (Status: %d/%s)." , status , nx_status_toString (status ));
264+ return status ;
265+ }
266+
267267#if ETH_ENABLE_MANUAL_UDP_MULTICAST
268268 /* Set up multicast groups.
269269 * (This iterates through every possible node combination between 0b00000001 and 0b11111111.
@@ -342,10 +342,10 @@ UINT ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve on_
342342
343343#if ETH_ENABLE_MQTT
344344/* Create MQTT client instance. */
345- char * client_id = "" ;
346- UINT client_id_size = sprintf (client_id , "FW -%d" , device .node_id );
345+ char client_id [ 8 ] = "" ;
346+ UINT client_id_size = sprintf (client_id , "FWD -%d" , device .node_id );
347347
348- status = nxd_mqtt_client_create (& device .mqtt_client , client_id ,
348+ status = nxd_mqtt_client_create (& device .mqtt_client , "MQTT client" ,
349349 client_id , client_id_size , & device .ip , & device .packet_pool ,
350350 (VOID * )device .mqtt_thread_stack , sizeof (device .mqtt_thread_stack ),
351351 _MQTT_THREAD_PRIORITY , NX_NULL , 0 );
@@ -361,28 +361,27 @@ UINT ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve on_
361361 return status ;
362362 }
363363
364+ /* Set the receive notify function. */
365+ status = nxd_mqtt_client_receive_notify_set (& device .mqtt_client , _mqtt_recieve_callback );
366+ if (status != NX_SUCCESS ) {
367+ PRINTLN_ERROR ("Failed to set mqtt recv notification (Status: %d/%s)." , status , nx_status_toString (status ));
368+ return status ;
369+ }
370+
364371 NXD_ADDRESS server_ip ;
365372 server_ip .nxd_ip_version = 4 ;
366373 server_ip .nxd_ip_address .v4 = ETH_MQTT_SERVER_IP ;
367374 /* Start the connection to the server. */
368375 status = nxd_mqtt_client_connect (& device .mqtt_client , & server_ip , ETH_MQTT_SERVER_PORT ,
369- 300 , NX_TRUE , NX_WAIT_FOREVER );
376+ 1000 , NX_TRUE , NX_WAIT_FOREVER );
370377 if (status != NX_SUCCESS ) {
371378 PRINTLN_ERROR ("Failed to connect to MQTT client (Status: %d/%s)." , status , nx_status_toString (status ));
372- return status ;
373- }
374-
375- /* Set the receive notify function. */
376- status = nxd_mqtt_client_receive_notify_set (& device .mqtt_client , _mqtt_recieve_callback );
377- if (status != NX_SUCCESS ) {
378- PRINTLN_ERROR ("Failed to set mqtt recv notification (Status: %d/%s)." , status , nx_status_toString (status ));
379- return status ;
379+ } else {
380380 }
381381#endif
382382 /* Mark device as initialized. */
383383 device .is_initialized = true;
384384
385- PRINTLN_INFO ("Ran ethernet_init()." );
386385 return NX_SUCCESS ;
387386}
388387
@@ -495,8 +494,32 @@ uint8_t ethernet_send_message(ethernet_message_t *message) {
495494#endif
496495
497496#if ETH_ENABLE_MQTT
498- uint32_t ethernet_mqtt_publish (char * topic_name , UINT topic_size , char * message , UINT message_size ) {
499- return nxd_mqtt_client_publish (& device .mqtt_client , topic_name , topic_size , message , message_size , NX_FALSE , 0 , MS_TO_TICKS (100 ));
497+ UINT ethernet_mqtt_publish (char * topic_name , UINT topic_size , char * message , UINT message_size ) {
498+ return nxd_mqtt_client_publish (& device .mqtt_client , topic_name , topic_size - 1 , message , message_size , NX_FALSE , 0 , MS_TO_TICKS (100 ));
499+ }
500+
501+ UINT ethernet_mqtt_reconnect (void ) {
502+ NXD_ADDRESS server_ip ;
503+ server_ip .nxd_ip_version = 4 ;
504+ server_ip .nxd_ip_address .v4 = ETH_MQTT_SERVER_IP ;
505+ // TODO fix bug that breaks reconnection with 0x10007
506+ nxd_mqtt_client_delete (& device .mqtt_client );
507+
508+ char client_id [8 ] = "" ;
509+ UINT client_id_size = sprintf (client_id , "FWD-%d" , device .node_id );
510+
511+ UINT status = nxd_mqtt_client_create (& device .mqtt_client , "MQTT client" ,
512+ client_id , client_id_size , & device .ip , & device .packet_pool ,
513+ (VOID * )device .mqtt_thread_stack , sizeof (device .mqtt_thread_stack ),
514+ _MQTT_THREAD_PRIORITY , NX_NULL , 0 );
515+ if (status != NX_SUCCESS ) {
516+ PRINTLN_ERROR ("Failed to create MQTT client (Status: %d/%s)." , status , nx_status_toString (status ));
517+ return status ;
518+ }
519+
520+ /* Start the connection to the server. */
521+ return nxd_mqtt_client_connect (& device .mqtt_client , & server_ip , ETH_MQTT_SERVER_PORT ,
522+ 1000 , NX_TRUE , NX_WAIT_FOREVER );
500523}
501524#endif
502525
0 commit comments