@@ -6,206 +6,194 @@ LOG_MODULE_DECLARE(sketch, LOG_LEVEL_NONE);
66struct net_mgmt_event_callback NetworkInterface::mgmt_cb;
77struct net_dhcpv4_option_callback NetworkInterface::dhcp_cb;
88
9- void NetworkInterface::event_handler (struct net_mgmt_event_callback *cb,
10- uint64_t mgmt_event,
11- struct net_if *iface)
12- {
13- int i = 0 ;
9+ void NetworkInterface::event_handler (struct net_mgmt_event_callback *cb, uint64_t mgmt_event,
10+ struct net_if *iface) {
11+ int i = 0 ;
1412
15- if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
16- return ;
17- }
13+ if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
14+ return ;
15+ }
1816
19- for (i = 0 ; i < NET_IF_MAX_IPV4_ADDR; i++) {
20- char buf[NET_IPV4_ADDR_LEN];
17+ for (i = 0 ; i < NET_IF_MAX_IPV4_ADDR; i++) {
18+ char buf[NET_IPV4_ADDR_LEN];
2119
22- if (iface->config .ip .ipv4 ->unicast [i].ipv4 .addr_type !=
23- NET_ADDR_DHCP) {
24- continue ;
25- }
20+ if (iface->config .ip .ipv4 ->unicast [i].ipv4 .addr_type != NET_ADDR_DHCP) {
21+ continue ;
22+ }
2623
27- LOG_INF (" Address[%d]: %s" , net_if_get_by_iface (iface),
28- net_addr_ntop (AF_INET,
29- &iface->config .ip .ipv4 ->unicast [i].ipv4 .address .in_addr ,
30- buf, sizeof (buf)));
31- LOG_INF (" Subnet[%d]: %s" , net_if_get_by_iface (iface),
32- net_addr_ntop (AF_INET,
33- &iface->config .ip .ipv4 ->unicast [i].netmask ,
34- buf, sizeof (buf)));
35- LOG_INF (" Router[%d]: %s" , net_if_get_by_iface (iface),
36- net_addr_ntop (AF_INET,
37- &iface->config .ip .ipv4 ->gw ,
38- buf, sizeof (buf)));
39- LOG_INF (" Lease time[%d]: %u seconds" , net_if_get_by_iface (iface),
40- iface->config .dhcpv4 .lease_time );
41- }
24+ LOG_INF (" Address[%d]: %s" , net_if_get_by_iface (iface),
25+ net_addr_ntop (AF_INET, &iface->config .ip .ipv4 ->unicast [i].ipv4 .address .in_addr , buf,
26+ sizeof (buf)));
27+ LOG_INF (
28+ " Subnet[%d]: %s" , net_if_get_by_iface (iface),
29+ net_addr_ntop (AF_INET, &iface->config .ip .ipv4 ->unicast [i].netmask , buf, sizeof (buf)));
30+ LOG_INF (" Router[%d]: %s" , net_if_get_by_iface (iface),
31+ net_addr_ntop (AF_INET, &iface->config .ip .ipv4 ->gw , buf, sizeof (buf)));
32+ LOG_INF (" Lease time[%d]: %u seconds" , net_if_get_by_iface (iface),
33+ iface->config .dhcpv4 .lease_time );
34+ }
4235}
4336
44- void NetworkInterface::option_handler (struct net_dhcpv4_option_callback *cb,
45- size_t length,
46- enum net_dhcpv4_msg_type msg_type,
47- struct net_if *iface)
48- {
49- char buf[NET_IPV4_ADDR_LEN];
37+ void NetworkInterface::option_handler (struct net_dhcpv4_option_callback *cb, size_t length,
38+ enum net_dhcpv4_msg_type msg_type, struct net_if *iface) {
39+ char buf[NET_IPV4_ADDR_LEN];
5040
51- LOG_INF (" DHCP Option %d: %s" , cb->option ,
52- net_addr_ntop (AF_INET, cb->data , buf, sizeof (buf)));
41+ LOG_INF (" DHCP Option %d: %s" , cb->option , net_addr_ntop (AF_INET, cb->data , buf, sizeof (buf)));
5342}
5443
55- int NetworkInterface::dhcp ()
56- {
57- net_mgmt_init_event_callback (&mgmt_cb, event_handler, NET_EVENT_IPV4_ADDR_ADD | NET_EVENT_IF_UP | NET_EVENT_IF_DOWN);
58- net_mgmt_add_event_callback (&mgmt_cb);
44+ int NetworkInterface::dhcp () {
45+ net_mgmt_init_event_callback (&mgmt_cb, event_handler,
46+ NET_EVENT_IPV4_ADDR_ADD | NET_EVENT_IF_UP | NET_EVENT_IF_DOWN);
47+ net_mgmt_add_event_callback (&mgmt_cb);
5948
60- net_dhcpv4_init_option_callback (&dhcp_cb, option_handler,
61- DHCP_OPTION_NTP, ntp_server,
62- sizeof (ntp_server));
49+ net_dhcpv4_init_option_callback (&dhcp_cb, option_handler, DHCP_OPTION_NTP, ntp_server,
50+ sizeof (ntp_server));
6351
64- net_dhcpv4_add_option_callback (&dhcp_cb);
52+ net_dhcpv4_add_option_callback (&dhcp_cb);
6553
66- net_dhcpv4_start (netif);
54+ net_dhcpv4_start (netif);
6755
68- LOG_INF (" DHCPv4 started...\n " );
56+ LOG_INF (" DHCPv4 started...\n " );
6957
70- return 0 ;
58+ return 0 ;
7159}
7260
73- void NetworkInterface::enable_dhcpv4_server (struct net_if *netif, char * _netmask)
74- {
75- static struct in_addr addr;
76- static struct in_addr netmaskAddr;
61+ void NetworkInterface::enable_dhcpv4_server (struct net_if *netif, char *_netmask) {
62+ static struct in_addr addr;
63+ static struct in_addr netmaskAddr;
7764
78- if (net_addr_pton (AF_INET, String (localIP ()).c_str (), &addr)) {
79- LOG_ERR (" Invalid address: %s" , String (localIP ()).c_str ());
80- return ;
81- }
65+ if (net_addr_pton (AF_INET, String (localIP ()).c_str (), &addr)) {
66+ LOG_ERR (" Invalid address: %s" , String (localIP ()).c_str ());
67+ return ;
68+ }
8269
83- if (net_addr_pton (AF_INET, _netmask, &netmaskAddr)) {
84- LOG_ERR (" Invalid netmask: %s" , _netmask);
85- return ;
86- }
70+ if (net_addr_pton (AF_INET, _netmask, &netmaskAddr)) {
71+ LOG_ERR (" Invalid netmask: %s" , _netmask);
72+ return ;
73+ }
8774
88- net_if_ipv4_set_gw (netif, &addr);
75+ net_if_ipv4_set_gw (netif, &addr);
8976
90- if (net_if_ipv4_addr_add (netif, &addr, NET_ADDR_MANUAL, 0 ) == NULL ) {
91- LOG_ERR (" unable to set IP address for AP interface" );
92- }
77+ if (net_if_ipv4_addr_add (netif, &addr, NET_ADDR_MANUAL, 0 ) == NULL ) {
78+ LOG_ERR (" unable to set IP address for AP interface" );
79+ }
9380
94- if (!net_if_ipv4_set_netmask_by_addr (netif, &addr, &netmaskAddr)) {
95- LOG_ERR (" Unable to set netmask for AP interface: %s" , _netmask);
96- }
81+ if (!net_if_ipv4_set_netmask_by_addr (netif, &addr, &netmaskAddr)) {
82+ LOG_ERR (" Unable to set netmask for AP interface: %s" , _netmask);
83+ }
9784
98- addr.s4_addr [3 ] += 10 ; /* Starting IPv4 address for DHCPv4 address pool. */
85+ addr.s4_addr [3 ] += 10 ; /* Starting IPv4 address for DHCPv4 address pool. */
9986
100- if (net_dhcpv4_server_start (netif, &addr) != 0 ) {
101- LOG_ERR (" DHCP server is not started for desired IP" );
102- return ;
103- }
87+ if (net_dhcpv4_server_start (netif, &addr) != 0 ) {
88+ LOG_ERR (" DHCP server is not started for desired IP" );
89+ return ;
90+ }
10491
105- LOG_INF (" DHCPv4 server started...\n " );
92+ LOG_INF (" DHCPv4 server started...\n " );
10693}
10794
10895IPAddress NetworkInterface::localIP () {
109- return IPAddress (netif->config .ip .ipv4 ->unicast [0 ].ipv4 .address .in_addr .s_addr );
96+ return IPAddress (netif->config .ip .ipv4 ->unicast [0 ].ipv4 .address .in_addr .s_addr );
11097}
11198
11299IPAddress NetworkInterface::subnetMask () {
113- return IPAddress (netif->config .ip .ipv4 ->unicast [0 ].netmask .s_addr );
100+ return IPAddress (netif->config .ip .ipv4 ->unicast [0 ].netmask .s_addr );
114101}
102+
115103IPAddress NetworkInterface::gatewayIP () {
116- return IPAddress (netif->config .ip .ipv4 ->gw .s_addr );
104+ return IPAddress (netif->config .ip .ipv4 ->gw .s_addr );
117105}
106+
118107IPAddress NetworkInterface::dnsServerIP () {
119- return arduino::INADDR_NONE;
108+ return arduino::INADDR_NONE;
120109}
121110
122- void NetworkInterface::setMACAddress (const uint8_t * mac) {
123- struct net_eth_addr new_mac;
124- struct ethernet_req_params params = { 0 };
111+ void NetworkInterface::setMACAddress (const uint8_t * mac) {
112+ struct net_eth_addr new_mac;
113+ struct ethernet_req_params params = {0 };
125114
126- memcpy (¶ms.mac_address , &new_mac, sizeof (struct net_eth_addr ));
115+ memcpy (¶ms.mac_address , &new_mac, sizeof (struct net_eth_addr ));
127116
128- net_if_down (netif); // Ensure the interface is down before changing the MAC address
117+ net_if_down (netif); // Ensure the interface is down before changing the MAC address
129118
130- int ret = net_mgmt (NET_REQUEST_ETHERNET_SET_MAC_ADDRESS, netif,
131- ¶ms, sizeof (params));
132- if (ret != 0 ) {
133- LOG_ERR (" Failed to set MAC address via net_mgmt, ret=%d" , ret);
134- } else {
135- LOG_INF (" MAC address set successfully via net_mgmt" );
136- }
137-
138- net_if_up (netif); // Bring the interface back up after changing the MAC address
119+ int ret = net_mgmt (NET_REQUEST_ETHERNET_SET_MAC_ADDRESS, netif, ¶ms, sizeof (params));
120+ if (ret != 0 ) {
121+ LOG_ERR (" Failed to set MAC address via net_mgmt, ret=%d" , ret);
122+ } else {
123+ LOG_INF (" MAC address set successfully via net_mgmt" );
124+ }
125+
126+ net_if_up (netif); // Bring the interface back up after changing the MAC address
139127}
140128
141129int NetworkInterface::begin (bool blocking, uint32_t additional_event_mask) {
142- dhcp ();
143- int ret = net_mgmt_event_wait_on_iface (netif, NET_EVENT_IPV4_ADDR_ADD | additional_event_mask,
144- NULL , NULL , NULL , blocking ? K_FOREVER : K_SECONDS (1 ));
145- return (ret == 0 ) ? 1 : 0 ;
130+ dhcp ();
131+ int ret = net_mgmt_event_wait_on_iface (netif, NET_EVENT_IPV4_ADDR_ADD | additional_event_mask,
132+ NULL , NULL , NULL , blocking ? K_FOREVER : K_SECONDS (1 ));
133+ return (ret == 0 ) ? 1 : 0 ;
146134}
147135
148136bool NetworkInterface::disconnect () {
149- return (net_if_down (netif) == 0 );
137+ return (net_if_down (netif) == 0 );
150138}
151139
152140bool NetworkInterface::setLocalIPFull (IPAddress ip, IPAddress subnet, IPAddress gateway) {
153- struct in_addr ip_addr, subnet_addr, gw_addr;
154-
155- ip_addr.s_addr = ip;
156- subnet_addr.s_addr = subnet;
157- gw_addr.s_addr = gateway;
141+ struct in_addr ip_addr, subnet_addr, gw_addr;
142+
143+ ip_addr.s_addr = ip;
144+ subnet_addr.s_addr = subnet;
145+ gw_addr.s_addr = gateway;
158146
159- if (!net_if_ipv4_addr_add (netif, &ip_addr, NET_ADDR_MANUAL, 0 )) {
160- LOG_ERR (" Failed to set static IP address" );
161- return false ;
162- }
147+ if (!net_if_ipv4_addr_add (netif, &ip_addr, NET_ADDR_MANUAL, 0 )) {
148+ LOG_ERR (" Failed to set static IP address" );
149+ return false ;
150+ }
163151
164- if (!net_if_ipv4_set_netmask_by_addr (netif, &ip_addr, &subnet_addr)) {
165- LOG_ERR (" Failed to set subnet mask" );
166- return false ;
167- }
152+ if (!net_if_ipv4_set_netmask_by_addr (netif, &ip_addr, &subnet_addr)) {
153+ LOG_ERR (" Failed to set subnet mask" );
154+ return false ;
155+ }
168156
169- net_if_ipv4_set_gw (netif, &gw_addr);
170- LOG_INF (" Static IP configured" );
171- return true ;
157+ net_if_ipv4_set_gw (netif, &gw_addr);
158+ LOG_INF (" Static IP configured" );
159+ return true ;
172160}
173161
174162bool NetworkInterface::setLocalIP (IPAddress ip) {
175- struct in_addr addr;
176- addr.s_addr = ip;
163+ struct in_addr addr;
164+ addr.s_addr = ip;
177165
178- if (!net_if_ipv4_addr_add (netif, &addr, NET_ADDR_MANUAL, 0 )) {
179- LOG_ERR (" Failed to set local IP address" );
180- return false ;
181- }
166+ if (!net_if_ipv4_addr_add (netif, &addr, NET_ADDR_MANUAL, 0 )) {
167+ LOG_ERR (" Failed to set local IP address" );
168+ return false ;
169+ }
182170
183- LOG_INF (" Local IP address set: %s" , ip.toString ().c_str ());
184- return true ;
171+ LOG_INF (" Local IP address set: %s" , ip.toString ().c_str ());
172+ return true ;
185173}
186174
187175bool NetworkInterface::setSubnetMask (IPAddress subnet) {
188- struct in_addr netmask_addr;
189- netmask_addr.s_addr = subnet;
176+ struct in_addr netmask_addr;
177+ netmask_addr.s_addr = subnet;
190178
191- if (!net_if_ipv4_set_netmask_by_addr (netif, &netmask_addr, &netmask_addr)) {
192- LOG_ERR (" Failed to set subnet mask" );
193- return false ;
194- }
179+ if (!net_if_ipv4_set_netmask_by_addr (netif, &netmask_addr, &netmask_addr)) {
180+ LOG_ERR (" Failed to set subnet mask" );
181+ return false ;
182+ }
195183
196- LOG_INF (" Subnet mask set: %s" , subnet.toString ().c_str ());
197- return true ;
184+ LOG_INF (" Subnet mask set: %s" , subnet.toString ().c_str ());
185+ return true ;
198186}
199187
200188bool NetworkInterface::setGatewayIP (IPAddress gateway) {
201- struct in_addr gw_addr;
202- gw_addr.s_addr = gateway;
189+ struct in_addr gw_addr;
190+ gw_addr.s_addr = gateway;
203191
204- net_if_ipv4_set_gw (netif, &gw_addr);
205- LOG_INF (" Gateway IP set: %s" , gateway.toString ().c_str ());
206- return true ;
192+ net_if_ipv4_set_gw (netif, &gw_addr);
193+ LOG_INF (" Gateway IP set: %s" , gateway.toString ().c_str ());
194+ return true ;
207195}
208196
209197bool NetworkInterface::setDnsServerIP (IPAddress dns_server) {
210- return false ; // DNS server dynamic configuration is not supported
198+ return false ; // DNS server dynamic configuration is not supported
211199}
0 commit comments