@@ -46,6 +46,25 @@ static void _receive_message(NX_UDP_SOCKET *socket) {
4646 /* Recieve the packet */
4747 status = nx_udp_socket_receive (socket , & packet , NX_NO_WAIT );
4848 if (status == NX_SUCCESS ) {
49+ PRINTLN_INFO ("Recieved UDP socket!" );
50+
51+ /* Get packet information (for debugging). */
52+ ULONG ip_address ;
53+ UINT protocol ;
54+ UINT port ;
55+ UINT interface_index ;
56+ status = nx_udp_packet_info_extract (packet , & ip_address , & protocol , & port , & interface_index );
57+ if (status != NX_SUCCESS ) {
58+ PRINTLN_WARNING ("Failed to extract UDP packet information (Status: %d/%s)." , status , nx_status_toString (status ));
59+ }
60+ else {
61+ uint8_t ip_address_byte1 = (ip_address >> 24 ) & 0xFF ;
62+ uint8_t ip_address_byte2 = (ip_address >> 16 ) & 0xFF ;
63+ uint8_t ip_address_byte3 = (ip_address >> 8 ) & 0xFF ;
64+ uint8_t ip_address_byte4 = ip_address & 0xFF ;
65+ PRINTLN_INFO ("UDP Packet - IP: %d.%d.%d.%d, Port: %d, Protocol: %d, Interface: %d" , ip_address_byte1 , ip_address_byte2 , ip_address_byte3 , ip_address_byte4 , port , protocol , interface_index );
66+ }
67+
4968 /* Extract message from packet */
5069 status = nx_packet_data_extract_offset (
5170 packet , // Packet to extract from
@@ -55,12 +74,12 @@ static void _receive_message(NX_UDP_SOCKET *socket) {
5574 & bytes_copied // Stores how many bytes were actually copied to &message
5675 );
5776 if (bytes_copied < sizeof (ethernet_message_t )) {
58- DEBUG_PRINTLN ( "WARNING: Received ethernet message was smaller than expected (only received %lu of %u expected bytes)." , bytes_copied , sizeof (ethernet_message_t ));
77+ PRINTLN_WARNING ( " Received ethernet message was smaller than expected (only received %lu of %u expected bytes)." , bytes_copied , sizeof (ethernet_message_t ));
5978 }
6079
6180 /* Process received message */
6281 if (status == NX_SUCCESS ) {
63- DEBUG_PRINTLN ("Received ethernet message! (Sender ID: %d, Message ID: %d)." , message .sender_id , message .message_id );
82+ PRINTLN_INFO ("Received ethernet message! (Sender ID: %d, Message ID: %d)." , message .sender_id , message .message_id );
6483 device .on_recieve (message );
6584 }
6685 }
@@ -77,7 +96,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
7796
7897 /* Make sure device isn't already initialized */
7998 if (device .is_initialized ) {
80- DEBUG_PRINTLN ( "ERROR: Ethernet is already initialized." );
99+ PRINTLN_ERROR ( " Ethernet is already initialized." );
81100 return U_ERROR ;
82101 }
83102
@@ -95,7 +114,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
95114 _PACKET_POOL_SIZE // Size of the pool's memory area
96115 );
97116 if (status != NX_SUCCESS ) {
98- DEBUG_PRINTLN ( "ERROR: Failed to create packet pool (Status: %d/%s)." , status , nx_status_toString (status ));
117+ PRINTLN_ERROR ( " Failed to create packet pool (Status: %d/%s)." , status , nx_status_toString (status ));
99118 return status ;
100119 }
101120
@@ -112,7 +131,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
112131 _IP_THREAD_PRIORITY // Priority of the IP thread
113132 );
114133 if (status != NX_SUCCESS ) {
115- DEBUG_PRINTLN ( "ERROR: Failed to create IP instance (Status: %d/%s)." , status , nx_status_toString (status ));
134+ PRINTLN_ERROR ( " Failed to create IP instance (Status: %d/%s)." , status , nx_status_toString (status ));
116135 return status ;
117136 }
118137
@@ -123,22 +142,22 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
123142 _ARP_CACHE_SIZE // Size of ARP cache
124143 );
125144 if (status != NX_SUCCESS ) {
126- DEBUG_PRINTLN ( "ERROR: Failed to enable ARP (Status: %d/%s)." , status , nx_status_toString (status ));
145+ PRINTLN_ERROR ( " Failed to enable ARP (Status: %d/%s)." , status , nx_status_toString (status ));
127146 return status ;
128147 }
129148
130149
131150 /* Enable UDP */
132151 status = nx_udp_enable (& device .ip );
133152 if (status != NX_SUCCESS ) {
134- DEBUG_PRINTLN ( "ERROR: Failed to enable UDP (Status: %d/%s)." , status , nx_status_toString (status ));
153+ PRINTLN_ERROR ( " Failed to enable UDP (Status: %d/%s)." , status , nx_status_toString (status ));
135154 return status ;
136155 }
137156
138157 /* Enable igmp */
139158 status = nx_igmp_enable (& device .ip );
140159 if (status != NX_SUCCESS ) {
141- DEBUG_PRINTLN ( "ERROR: Failed to enable igmp (Status: %d/%s)." , status , nx_status_toString (status ));
160+ PRINTLN_ERROR ( " Failed to enable igmp (Status: %d/%s)." , status , nx_status_toString (status ));
142161 return status ;
143162 }
144163
@@ -156,7 +175,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
156175 ULONG address = ETH_IP (i );
157176 status = nx_igmp_multicast_join (& device .ip , address );
158177 if (status != NX_SUCCESS ) {
159- DEBUG_PRINTLN ( "ERROR: Failed to join multicast group (Status: %d/%s, Address: %lu)." , status , nx_status_toString (status ), address );
178+ PRINTLN_ERROR ( " Failed to join multicast group (Status: %d/%s, Address: %lu)." , status , nx_status_toString (status ), address );
160179 }
161180 }
162181 }
@@ -172,7 +191,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
172191 _UDP_QUEUE_MAXIMUM // UDP queue maximum
173192 );
174193 if (status != NX_SUCCESS ) {
175- DEBUG_PRINTLN ( "ERROR: Failed to create UDP socket (Status: %d/%s)." , status , nx_status_toString (status ));
194+ PRINTLN_ERROR ( " Failed to create UDP socket (Status: %d/%s)." , status , nx_status_toString (status ));
176195 return status ;
177196 }
178197
@@ -183,7 +202,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
183202 TX_WAIT_FOREVER // Wait forever
184203 );
185204 if (status != NX_SUCCESS ) {
186- DEBUG_PRINTLN ( "ERROR: Failed to bind UDP socket (Status: %d/%s)." , status , nx_status_toString (status ));
205+ PRINTLN_ERROR ( " Failed to bind UDP socket (Status: %d/%s)." , status , nx_status_toString (status ));
187206 nx_udp_socket_delete (& device .socket );
188207 return status ;
189208 }
@@ -194,7 +213,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
194213 & _receive_message // Callback function
195214 );
196215 if (status != NX_SUCCESS ) {
197- DEBUG_PRINTLN ( "ERROR: Failed to set recieve callback (Status: %d/%s)." , status , nx_status_toString (status ));
216+ PRINTLN_ERROR ( " Failed to set recieve callback (Status: %d/%s)." , status , nx_status_toString (status ));
198217 nx_udp_socket_unbind (& device .socket );
199218 nx_udp_socket_delete (& device .socket );
200219 return status ;
@@ -203,7 +222,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
203222 /* Mark device as initialized. */
204223 device .is_initialized = true;
205224
206- DEBUG_PRINTLN ("Ran ethernet_init()." );
225+ PRINTLN_INFO ("Ran ethernet_init()." );
207226 return NX_SUCCESS ;
208227}
209228
@@ -213,7 +232,7 @@ ethernet_message_t ethernet_create_message(uint8_t message_id, ethernet_node_t r
213232
214233 /* Check data length */
215234 if (data_length > ETH_MESSAGE_SIZE ) {
216- DEBUG_PRINTLN ( "ERROR: Data length exceeds maximum (message_id: %d)." , message_id );
235+ PRINTLN_ERROR ( " Data length exceeds maximum (message_id: %d)." , message_id );
217236 return message ; // Return empty message.
218237 }
219238
@@ -234,13 +253,13 @@ uint8_t ethernet_send_message(ethernet_message_t *message) {
234253
235254 /* Check if ethernet is initialized */
236255 if (!device .is_initialized ) {
237- DEBUG_PRINTLN ( "ERROR: Ethernet device is not initialized, so ethernet_send_message() will not work." );
256+ PRINTLN_ERROR ( " Ethernet device is not initialized, so ethernet_send_message() will not work." );
238257 return U_ERROR ;
239258 }
240259
241260 /* Check data length */
242261 if (message -> data_length > ETH_MESSAGE_SIZE ) {
243- DEBUG_PRINTLN ( "ERROR: Data length exceeds maximum (Message ID: %d)." , message -> message_id );
262+ PRINTLN_ERROR ( " Data length exceeds maximum (Message ID: %d)." , message -> message_id );
244263 return U_ERROR ;
245264 }
246265
@@ -252,7 +271,7 @@ uint8_t ethernet_send_message(ethernet_message_t *message) {
252271 TX_WAIT_FOREVER // Wait indefinitely until a packet is available
253272 );
254273 if (status != NX_SUCCESS ) {
255- DEBUG_PRINTLN ( "ERROR: Failed to allocate packet (Status: %d/%s, Message ID: %d)." , status , nx_status_toString (status ), message -> message_id );
274+ PRINTLN_ERROR ( " Failed to allocate packet (Status: %d/%s, Message ID: %d)." , status , nx_status_toString (status ), message -> message_id );
256275 return U_ERROR ;
257276 }
258277
@@ -265,7 +284,7 @@ uint8_t ethernet_send_message(ethernet_message_t *message) {
265284 TX_WAIT_FOREVER // Wait indefinitely
266285 );
267286 if (status != NX_SUCCESS ) {
268- DEBUG_PRINTLN ( "ERROR: Failed to append data to packet (Status: %d/%s, Message ID: %d)." , status , nx_status_toString (status ), message -> message_id );
287+ PRINTLN_ERROR ( " Failed to append data to packet (Status: %d/%s, Message ID: %d)." , status , nx_status_toString (status ), message -> message_id );
269288 nx_packet_release (packet );
270289 return U_ERROR ;
271290 }
@@ -278,12 +297,12 @@ uint8_t ethernet_send_message(ethernet_message_t *message) {
278297 ETH_UDP_PORT
279298 );
280299 if (status != NX_SUCCESS ) {
281- DEBUG_PRINTLN ( "ERROR: Failed to send packet (Status: %d/%s, Message ID: %d)." , status , nx_status_toString (status ), message -> message_id );
300+ PRINTLN_ERROR ( " Failed to send packet (Status: %d/%s, Message ID: %d)." , status , nx_status_toString (status ), message -> message_id );
282301 nx_packet_release (packet );
283302 return U_ERROR ;
284303 }
285304
286- DEBUG_PRINTLN ("Sent ethernet message (Recipient ID: %d, Message ID: %d)." , message -> recipient_id , message -> message_id );
305+ PRINTLN_INFO ("Sent ethernet message (Recipient ID: %d, Message ID: %d)." , message -> recipient_id , message -> message_id );
287306 return U_SUCCESS ;
288307}
289308// clang-format on
0 commit comments