@@ -6,206 +6,194 @@ LOG_MODULE_DECLARE(sketch, LOG_LEVEL_NONE);
6
6
struct net_mgmt_event_callback NetworkInterface::mgmt_cb;
7
7
struct net_dhcpv4_option_callback NetworkInterface::dhcp_cb;
8
8
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 ;
14
12
15
- if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
16
- return ;
17
- }
13
+ if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
14
+ return ;
15
+ }
18
16
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];
21
19
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
+ }
26
23
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
+ }
42
35
}
43
36
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];
50
40
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)));
53
42
}
54
43
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);
59
48
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));
63
51
64
- net_dhcpv4_add_option_callback (&dhcp_cb);
52
+ net_dhcpv4_add_option_callback (&dhcp_cb);
65
53
66
- net_dhcpv4_start (netif);
54
+ net_dhcpv4_start (netif);
67
55
68
- LOG_INF (" DHCPv4 started...\n " );
56
+ LOG_INF (" DHCPv4 started...\n " );
69
57
70
- return 0 ;
58
+ return 0 ;
71
59
}
72
60
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;
77
64
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
+ }
82
69
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
+ }
87
74
88
- net_if_ipv4_set_gw (netif, &addr);
75
+ net_if_ipv4_set_gw (netif, &addr);
89
76
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
+ }
93
80
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
+ }
97
84
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. */
99
86
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
+ }
104
91
105
- LOG_INF (" DHCPv4 server started...\n " );
92
+ LOG_INF (" DHCPv4 server started...\n " );
106
93
}
107
94
108
95
IPAddress 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 );
110
97
}
111
98
112
99
IPAddress 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 );
114
101
}
102
+
115
103
IPAddress NetworkInterface::gatewayIP () {
116
- return IPAddress (netif->config .ip .ipv4 ->gw .s_addr );
104
+ return IPAddress (netif->config .ip .ipv4 ->gw .s_addr );
117
105
}
106
+
118
107
IPAddress NetworkInterface::dnsServerIP () {
119
- return arduino::INADDR_NONE;
108
+ return arduino::INADDR_NONE;
120
109
}
121
110
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 };
125
114
126
- memcpy (¶ms.mac_address , &new_mac, sizeof (struct net_eth_addr ));
115
+ memcpy (¶ms.mac_address , &new_mac, sizeof (struct net_eth_addr ));
127
116
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
129
118
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
139
127
}
140
128
141
129
int 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 ;
146
134
}
147
135
148
136
bool NetworkInterface::disconnect () {
149
- return (net_if_down (netif) == 0 );
137
+ return (net_if_down (netif) == 0 );
150
138
}
151
139
152
140
bool 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;
158
146
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
+ }
163
151
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
+ }
168
156
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 ;
172
160
}
173
161
174
162
bool NetworkInterface::setLocalIP (IPAddress ip) {
175
- struct in_addr addr;
176
- addr.s_addr = ip;
163
+ struct in_addr addr;
164
+ addr.s_addr = ip;
177
165
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
+ }
182
170
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 ;
185
173
}
186
174
187
175
bool 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;
190
178
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
+ }
195
183
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 ;
198
186
}
199
187
200
188
bool 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;
203
191
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 ;
207
195
}
208
196
209
197
bool 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
211
199
}
0 commit comments