Skip to content

Commit c386678

Browse files
committed
working
1 parent 7945187 commit c386678

3 files changed

Lines changed: 109 additions & 42 deletions

File tree

NetX/inc/u_nx_ethernet.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ typedef struct __attribute__((__packed__)) {
6666

6767
/* Function Pointers (for initialization). */
6868
typedef void (*DriverFunction)(NX_IP_DRIVER *); /* User-supplied network driver used to send and receive IP packets. */
69+
70+
#if ETH_ENABLE_MANUAL_UDP_MULTICAST
71+
typedef void (*OnRecieve)(ethernet_message_t message); /* User-supplied function that will be called whenever an ethernet message is recieved. */
72+
#else
6973
typedef void (*OnRecieve)(ethernet_message_t message); /* User-supplied function that will be called whenever an ethernet message is recieved. */
74+
#endif
7075

7176
/**
7277
* @brief Initializes the NetX ethernet system in a repo.
@@ -105,7 +110,13 @@ uint8_t ethernet_send_message(ethernet_message_t *message);
105110
* @param message_size The message size in bytes
106111
* @return The error code.
107112
*/
108-
uint32_t ethernet_mqtt_publish(char* topic_name, UINT topic_size, char* message, UINT message_size);
113+
UINT ethernet_mqtt_publish(char* topic_name, UINT topic_size, char* message, UINT message_size);
114+
115+
/**
116+
* Connect to a disconnected MQTT server (NX_MQTT_NOT_CONNECTED)
117+
* Will yield while trying to connect
118+
*/
119+
UINT ethernet_mqtt_reconnect(void);
109120

110121
/**
111122
*
@@ -124,6 +135,9 @@ uint32_t ethernet_mqtt_publish(char* topic_name, UINT topic_size, char* message,
124135
*/
125136
NX_PTP_DATE_TIME ethernet_get_time(void);
126137

138+
/**
139+
* Debugging, print the status of ARP statistics
140+
*/
127141
UINT ethernent_print_arp_status(void);
128142

129143
// clang-format on

NetX/src/u_nx_ethernet.c

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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
@@ -25,46 +26,49 @@
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

3132
extern ETH_HandleTypeDef heth;
3233

3334
/* DEVICE INFO */
3435
typedef 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;
6165
static _ethernet_device_t device = { 0 };
6266

6367
/* CALLBACK FUNCTIONS */
6468
#if ETH_ENABLE_MQTT
6569
static 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

7074
static 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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
diff --git a/Middlewares/ST/netxduo/common/drivers/ethernet/nx_stm32_eth_driver.c b/Middlewares/ST/netxduo/common/drivers/ethernet/nx_stm32_eth_driver.c
2+
index d93161a..4e35bca 100644
3+
--- a/Middlewares/ST/netxduo/common/drivers/ethernet/nx_stm32_eth_driver.c
4+
+++ b/Middlewares/ST/netxduo/common/drivers/ethernet/nx_stm32_eth_driver.c
5+
@@ -265,6 +265,12 @@ NX_INTERFACE *interface_ptr;
6+
}
7+
#endif /* NX_ENABLE_INTERFACE_CAPABILITY */
8+
9+
+ case NX_DRIVER_GET_STATE:
10+
+ {
11+
+ *(driver_req_ptr->nx_ip_driver_return_ptr) = nx_driver_information.nx_driver_information_state;
12+
+ break;
13+
+ }
14+
+
15+
default:
16+
17+
18+
diff --git a/Middlewares/ST/netxduo/common/drivers/ethernet/nx_stm32_eth_driver.h b/Middlewares/ST/netxduo/common/drivers/ethernet/nx_stm32_eth_driver.h
19+
index 0198ed9..a94fa14 100644
20+
--- a/Middlewares/ST/netxduo/common/drivers/ethernet/nx_stm32_eth_driver.h
21+
+++ b/Middlewares/ST/netxduo/common/drivers/ethernet/nx_stm32_eth_driver.h
22+
@@ -72,6 +72,8 @@ extern "C" {
23+
#define NX_DRIVER_STATE_INITIALIZED 3
24+
#define NX_DRIVER_STATE_LINK_ENABLED 4
25+
26+
+#define NX_DRIVER_GET_STATE 51
27+
+
28+
#define NX_DRIVER_ERROR 90
29+
30+

0 commit comments

Comments
 (0)