4
4
5
5
/*
6
6
7
- Copyright (c) 2018-2023 , Terje Io
7
+ Copyright (c) 2018-2025 , Terje Io
8
8
Copyright (c) 2022, @Henrikastro
9
9
All rights reserved.
10
10
@@ -60,6 +60,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60
60
61
61
#include "grbl/report.h"
62
62
#include "grbl/nvs_buffer.h"
63
+ #include "grbl/task.h"
64
+
63
65
#include "networking/networking.h"
64
66
65
67
#define SYSTICK_INT_PRIORITY 0x80
@@ -87,7 +89,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
87
89
88
90
//
89
91
90
- static volatile bool linkUp = false ;
92
+ static char if_name [ NETIF_NAMESIZE ] = "" ;
91
93
static stream_type_t active_stream = StreamType_Null ;
92
94
static network_settings_t network , ethernet ;
93
95
static network_services_t services = {0 }, allowed_services ;
@@ -96,6 +98,7 @@ static on_report_options_ptr on_report_options;
96
98
static on_stream_changed_ptr on_stream_changed ;
97
99
static uint8_t mac_addr [6 ] = {0 };
98
100
static esp_netif_ip_info_t * ip_info = NULL ;
101
+ static network_flags_t network_status = {};
99
102
100
103
static char netservices [NETWORK_SERVICES_LEN ] = "" ;
101
104
@@ -110,6 +113,42 @@ static char *enet_ip_address (void)
110
113
return ip ;
111
114
}
112
115
116
+ static network_info_t * get_info (const char * interface )
117
+ {
118
+ static network_info_t info = {};
119
+
120
+ if (interface == if_name ) {
121
+
122
+ memcpy (& info .status , & network , sizeof (network_settings_t ));
123
+
124
+ info .interface = (const char * )if_name ;
125
+ info .is_ethernet = true;
126
+ info .link_up = network_status .link_up ;
127
+ info .mbps = 100 ;
128
+ info .status .services = services ;
129
+ * info .mac = * info .status .ip = * info .status .gateway = * info .status .mask = '\0' ;
130
+
131
+ struct netif * netif = netif_default ;
132
+
133
+ if (netif ) {
134
+
135
+ sprintf (info .mac , MAC_FORMAT_STRING , mac_addr [0 ], mac_addr [1 ], mac_addr [2 ], mac_addr [3 ], mac_addr [4 ], mac_addr [5 ]);
136
+
137
+ if (network_status .link_up ) {
138
+ strcpy (info .status .ip , enet_ip_address ());
139
+ }
140
+ }
141
+
142
+ #if MQTT_ENABLE
143
+ networking_make_mqtt_clientid (info .mac , info .mqtt_client_id );
144
+ #endif
145
+
146
+ return & info ;
147
+ }
148
+
149
+ return NULL ;
150
+ }
151
+
113
152
static void report_options (bool newopt )
114
153
{
115
154
on_report_options (newopt );
@@ -122,50 +161,35 @@ static void report_options (bool newopt)
122
161
#endif
123
162
} else {
124
163
125
- network_info_t * net = networking_get_info () ;
164
+ network_info_t * network ;
126
165
127
- hal .stream .write ("[MAC:" );
128
- hal .stream .write (net -> mac );
129
- hal .stream .write ("]" ASCII_EOL );
166
+ if ((network = get_info (if_name ))) {
130
167
131
- hal .stream .write ("[IP :" );
132
- hal .stream .write (net -> status . ip );
133
- hal .stream .write ("]" ASCII_EOL );
168
+ hal .stream .write ("[MAC :" );
169
+ hal .stream .write (network -> mac );
170
+ hal .stream .write ("]" ASCII_EOL );
134
171
135
- if (active_stream == StreamType_Telnet || active_stream == StreamType_WebSocket ) {
136
- hal .stream .write ("[NETCON:" );
137
- hal .stream .write (active_stream == StreamType_Telnet ? "Telnet" : "Websocket" );
172
+ hal .stream .write ("[IP:" );
173
+ hal .stream .write (network -> status .ip );
138
174
hal .stream .write ("]" ASCII_EOL );
175
+
176
+ if (active_stream == StreamType_Telnet || active_stream == StreamType_WebSocket ) {
177
+ hal .stream .write ("[NETCON:" );
178
+ hal .stream .write (active_stream == StreamType_Telnet ? "Telnet" : "Websocket" );
179
+ hal .stream .write ("]" ASCII_EOL );
180
+ }
139
181
}
140
182
}
141
183
}
142
184
143
- network_info_t * networking_get_info (void )
185
+ static void status_event_out (void * data )
144
186
{
145
- static network_info_t info ;
146
-
147
- memcpy (& info .status , & network , sizeof (network_settings_t ));
148
-
149
- sprintf (info .mac , MAC_FORMAT_STRING , mac_addr [0 ], mac_addr [1 ], mac_addr [2 ], mac_addr [3 ], mac_addr [4 ], mac_addr [5 ]);
150
-
151
- if (ip_info )
152
- strcpy (info .status .ip , enet_ip_address ());
153
-
154
- if (info .status .ip_mode == IpMode_DHCP ) {
155
- * info .status .gateway = '\0' ;
156
- * info .status .mask = '\0' ;
157
- }
158
-
159
- info .is_ethernet = true;
160
- info .link_up = linkUp ;
161
- info .mbps = 100 ;
162
- info .status .services = services ;
163
-
164
- #if MQTT_ENABLE
165
- networking_make_mqtt_clientid (info .mac , info .mqtt_client_id );
166
- #endif
187
+ networking .event (if_name , (network_status_t ){ .value = (uint32_t )data });
188
+ }
167
189
168
- return & info ;
190
+ static void status_event_publish (network_flags_t changed )
191
+ {
192
+ task_add_immediate (status_event_out , (void * )((network_status_t ){ .changed = changed , .flags = network_status }).value );
169
193
}
170
194
171
195
static void lwIPHostTimerHandler (void * arg )
@@ -208,6 +232,11 @@ static void start_services (void)
208
232
#if TELNET_ENABLE || WEBSOCKET_ENABLE || FTP_ENABLE
209
233
sys_timeout (STREAM_POLL_INTERVAL , lwIPHostTimerHandler , NULL );
210
234
#endif
235
+
236
+ if (!network_status .ip_aquired ) {
237
+ network_status .ip_aquired = On ;
238
+ status_event_publish ((network_flags_t ){ .ip_aquired = On });
239
+ }
211
240
}
212
241
213
242
/** Event handler for Ethernet events */
@@ -216,13 +245,15 @@ static void eth_event_handler (void *arg, esp_event_base_t event_base, int32_t e
216
245
switch (event_id ) {
217
246
218
247
case ETHERNET_EVENT_CONNECTED :
219
- linkUp = true;
220
248
esp_eth_ioctl (* (esp_eth_handle_t * )event_data , ETH_CMD_G_MAC_ADDR , mac_addr );
249
+ network_status .link_up = On ;
250
+ status_event_publish ((network_flags_t ){ .link_up = On });
221
251
break ;
222
252
223
253
case ETHERNET_EVENT_DISCONNECTED :
224
- linkUp = false;
225
254
ip_info = NULL ;
255
+ network_status .link_up = Off ;
256
+ status_event_publish ((network_flags_t ){ .link_up = On });
226
257
break ;
227
258
/*
228
259
case ETHERNET_EVENT_START:
@@ -237,7 +268,7 @@ static void eth_event_handler (void *arg, esp_event_base_t event_base, int32_t e
237
268
}
238
269
239
270
/** Event handler for IP_EVENT_ETH_GOT_IP */
240
- static void got_ip_event_handler (void * arg , esp_event_base_t event_base , int32_t event_id , void * event_data )
271
+ static void got_ip_event_handler (void * arg , esp_event_base_t event_base , int32_t event_id , void * event_data )
241
272
{
242
273
static esp_netif_ip_info_t info ;
243
274
@@ -339,6 +370,11 @@ bool enet_start (void)
339
370
/* start Ethernet driver state machine */
340
371
ESP_ERROR_CHECK (esp_eth_start (eth_handle ));
341
372
373
+ netif_index_to_name (1 , if_name );
374
+
375
+ network_status .interface_up = On ;
376
+ status_event_publish ((network_flags_t ){ .interface_up = On });
377
+
342
378
return true;
343
379
}
344
380
@@ -544,10 +580,11 @@ bool enet_init (void)
544
580
545
581
settings_register (& setting_details );
546
582
583
+ networking .get_info = get_info ;
547
584
allowed_services .mask = networking_get_services_list ((char * )netservices ).mask ;
548
585
}
549
586
550
587
return true;
551
588
}
552
589
553
- #endif
590
+ #endif // ETHERNET_ENABLE
0 commit comments