|
1 | 1 | #include "SocketHelpers.h"
|
2 | 2 |
|
| 3 | +#include <zephyr/logging/log.h> |
| 4 | +LOG_MODULE_DECLARE(sketch, CONFIG_LOG_DEFAULT_LEVEL); |
| 5 | + |
3 | 6 | struct net_mgmt_event_callback NetworkInterface::mgmt_cb;
|
4 | 7 | struct net_dhcpv4_option_callback NetworkInterface::dhcp_cb;
|
| 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; |
| 14 | + |
| 15 | + if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) { |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) { |
| 20 | + char buf[NET_IPV4_ADDR_LEN]; |
| 21 | + |
| 22 | + if (iface->config.ip.ipv4->unicast[i].ipv4.addr_type != |
| 23 | + NET_ADDR_DHCP) { |
| 24 | + continue; |
| 25 | + } |
| 26 | + |
| 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 | + } |
| 42 | +} |
| 43 | + |
| 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]; |
| 50 | + |
| 51 | + LOG_INF("DHCP Option %d: %s", cb->option, |
| 52 | + net_addr_ntop(AF_INET, cb->data, buf, sizeof(buf))); |
| 53 | +} |
| 54 | + |
| 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); |
| 59 | + |
| 60 | + net_dhcpv4_init_option_callback(&dhcp_cb, option_handler, |
| 61 | + DHCP_OPTION_NTP, ntp_server, |
| 62 | + sizeof(ntp_server)); |
| 63 | + |
| 64 | + net_dhcpv4_add_option_callback(&dhcp_cb); |
| 65 | + |
| 66 | + net_dhcpv4_start(netif); |
| 67 | + |
| 68 | + LOG_INF("DHCPv4 started...\n"); |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 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; |
| 77 | + |
| 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 | + } |
| 82 | + |
| 83 | + if (net_addr_pton(AF_INET, _netmask, &netmaskAddr)) { |
| 84 | + LOG_ERR("Invalid netmask: %s", _netmask); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + net_if_ipv4_set_gw(netif, &addr); |
| 89 | + |
| 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 | + } |
| 93 | + |
| 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 | + } |
| 97 | + |
| 98 | + addr.s4_addr[3] += 10; /* Starting IPv4 address for DHCPv4 address pool. */ |
| 99 | + |
| 100 | + if (net_dhcpv4_server_start(netif, &addr) != 0) { |
| 101 | + LOG_ERR("DHCP server is not started for desired IP"); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + LOG_INF("DHCPv4 server started...\n"); |
| 106 | +} |
| 107 | + |
| 108 | +IPAddress NetworkInterface::localIP() { |
| 109 | + return IPAddress(netif->config.ip.ipv4->unicast[0].ipv4.address.in_addr.s_addr); |
| 110 | +} |
| 111 | + |
| 112 | +IPAddress NetworkInterface::subnetMask() { |
| 113 | + return IPAddress(netif->config.ip.ipv4->unicast[0].netmask.s_addr); |
| 114 | +} |
| 115 | +IPAddress NetworkInterface::gatewayIP() { |
| 116 | + return IPAddress(netif->config.ip.ipv4->gw.s_addr); |
| 117 | +} |
| 118 | +IPAddress NetworkInterface::dnsServerIP() { |
| 119 | + return arduino::INADDR_NONE; |
| 120 | +} |
| 121 | + |
| 122 | +IPAddress NetworkInterface::dnsIP(int n) { |
| 123 | + //TODO |
| 124 | +} |
| 125 | + |
| 126 | +void NetworkInterface::setMACAddress(const uint8_t* mac) { |
| 127 | + struct net_eth_addr new_mac; |
| 128 | + struct ethernet_req_params params = { 0 }; |
| 129 | + |
| 130 | + memcpy(¶ms.mac_address, &new_mac, sizeof(struct net_eth_addr)); |
| 131 | + |
| 132 | + net_if_down(netif); // Ensure the interface is down before changing the MAC address |
| 133 | + |
| 134 | + int ret = net_mgmt(NET_REQUEST_ETHERNET_SET_MAC_ADDRESS, netif, |
| 135 | + ¶ms, sizeof(params)); |
| 136 | + if (ret != 0) { |
| 137 | + LOG_ERR("Failed to set MAC address via net_mgmt, ret=%d", ret); |
| 138 | + } else { |
| 139 | + LOG_INF("MAC address set successfully via net_mgmt"); |
| 140 | + } |
| 141 | + |
| 142 | + net_if_up(netif); // Bring the interface back up after changing the MAC address |
| 143 | +} |
| 144 | + |
| 145 | +int NetworkInterface::begin(bool blocking, uint32_t additional_event_mask) { |
| 146 | + dhcp(); |
| 147 | + int ret = net_mgmt_event_wait_on_iface(netif, NET_EVENT_IPV4_ADDR_ADD | additional_event_mask, |
| 148 | + NULL, NULL, NULL, blocking ? K_FOREVER : K_SECONDS(1)); |
| 149 | + return (ret == 0) ? 1 : 0; |
| 150 | +} |
| 151 | + |
| 152 | +bool NetworkInterface::disconnect() { |
| 153 | + return (net_if_down(netif) == 0); |
| 154 | +} |
0 commit comments