1 /* 2 * Copyright (c) 2006-2021, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2019-03-18 ChenYong First version 9 */ 10 11 #ifndef __NETDEV_H__ 12 #define __NETDEV_H__ 13 14 #include <rtthread.h> 15 16 #ifdef __cplusplus 17 extern "C" { 18 #endif 19 20 /* the maximum of all used hardware address lengths */ 21 #ifndef NETDEV_HWADDR_MAX_LEN 22 #define NETDEV_HWADDR_MAX_LEN 8U 23 #endif 24 25 /* the maximum of dns server number supported */ 26 #ifndef NETDEV_DNS_SERVERS_NUM 27 #define NETDEV_DNS_SERVERS_NUM 2U 28 #endif 29 30 #if NETDEV_IPV6 31 /* the maximum of dns server number supported */ 32 #ifndef NETDEV_IPV6_NUM_ADDRESSES 33 #define NETDEV_IPV6_NUM_ADDRESSES 3U 34 #endif 35 #endif /* NETDEV_IPV6 */ 36 37 /* whether the network interface device is 'up' (set by the network interface driver or application) */ 38 #define NETDEV_FLAG_UP 0x01U 39 /* if set, the network interface device has broadcast capability, only supported in the 'lwIP' stack */ 40 #define NETDEV_FLAG_BROADCAST 0x02U 41 /* if set, the network interface device has an active link (set by the network interface driver) */ 42 #define NETDEV_FLAG_LINK_UP 0x04U 43 /* if set, the network interface device is an ethernet device using ARP, only supported in the 'lwIP' stack */ 44 #define NETDEV_FLAG_ETHARP 0x08U 45 /* if set, the network interface device is an ethernet device, only supported in the 'lwIP' stack */ 46 #define NETDEV_FLAG_ETHERNET 0x10U 47 /* if set, the network interface device has IGMP capability, only supported in the 'lwIP' stack */ 48 #define NETDEV_FLAG_IGMP 0x20U 49 /* if set, the network interface device has MLD6 capability, only supported in the 'lwIP' stack */ 50 #define NETDEV_FLAG_MLD6 0x40U 51 /* if set, the network interface device connected to internet successfully (set by the network interface driver) */ 52 #define NETDEV_FLAG_INTERNET_UP 0x80U 53 /* if set, the network interface device has DHCP capability (set by the network interface device driver or application) */ 54 #define NETDEV_FLAG_DHCP 0x100U 55 56 enum netdev_cb_type 57 { 58 NETDEV_CB_ADDR_IP, /* IP address */ 59 NETDEV_CB_ADDR_NETMASK, /* subnet mask */ 60 NETDEV_CB_ADDR_GATEWAY, /* netmask */ 61 NETDEV_CB_ADDR_DNS_SERVER, /* dns server */ 62 NETDEV_CB_STATUS_UP, /* changed to 'up' */ 63 NETDEV_CB_STATUS_DOWN, /* changed to 'down' */ 64 NETDEV_CB_STATUS_LINK_UP, /* changed to 'link up' */ 65 NETDEV_CB_STATUS_LINK_DOWN, /* changed to 'link down' */ 66 NETDEV_CB_STATUS_INTERNET_UP, /* changed to 'internet up' */ 67 NETDEV_CB_STATUS_INTERNET_DOWN, /* changed to 'internet down' */ 68 NETDEV_CB_STATUS_DHCP_ENABLE, /* enable DHCP capability */ 69 NETDEV_CB_STATUS_DHCP_DISABLE, /* disable DHCP capability */ 70 NETDEV_CB_REGISTER, /* netdev register */ 71 NETDEV_CB_DEFAULT_CHANGE, /* netdev default change */ 72 }; 73 74 struct netdev; 75 76 /* function prototype for network interface device status or address change callback functions */ 77 typedef void (*netdev_callback_fn )(struct netdev *netdev, enum netdev_cb_type type); 78 79 struct netdev_ops; 80 81 /* network interface device object */ 82 struct netdev 83 { 84 rt_slist_t list; 85 86 char name[RT_NAME_MAX]; /* network interface device name */ 87 ip_addr_t ip_addr; /* IP address */ 88 ip_addr_t netmask; /* subnet mask */ 89 ip_addr_t gw; /* gateway */ 90 #if NETDEV_IPV6 91 ip_addr_t ip6_addr[NETDEV_IPV6_NUM_ADDRESSES]; /* array of IPv6 addresses */ 92 #endif /* NETDEV_IPV6 */ 93 ip_addr_t dns_servers[NETDEV_DNS_SERVERS_NUM]; /* DNS server */ 94 uint8_t hwaddr_len; /* hardware address length */ 95 uint8_t hwaddr[NETDEV_HWADDR_MAX_LEN]; /* hardware address */ 96 97 uint16_t flags; /* network interface device status flag */ 98 uint16_t mtu; /* maximum transfer unit (in bytes) */ 99 const struct netdev_ops *ops; /* network interface device operations */ 100 101 netdev_callback_fn status_callback; /* network interface device flags change callback */ 102 netdev_callback_fn addr_callback; /* network interface device address information change callback */ 103 104 int ifindex; /* network interface device ifindex */ 105 106 #ifdef RT_USING_SAL 107 void *sal_user_data; /* user-specific data for SAL */ 108 #endif /* RT_USING_SAL */ 109 void *user_data; /* user-specific data */ 110 }; 111 112 /* The list of network interface device */ 113 extern struct netdev *netdev_list; 114 /* The default network interface device */ 115 extern struct netdev *netdev_default; 116 /* The network interface device ping response object */ 117 struct netdev_ping_resp 118 { 119 ip_addr_t ip_addr; /* response IP address */ 120 uint16_t data_len; /* response data length */ 121 uint16_t ttl; /* time to live */ 122 uint32_t ticks; /* response time, unit tick */ 123 void *user_data; /* user-specific data */ 124 }; 125 126 /* The network interface device operations */ 127 struct netdev_ops 128 { 129 /* set network interface device hardware status operations */ 130 int (*set_up)(struct netdev *netdev); 131 int (*set_down)(struct netdev *netdev); 132 133 /* set network interface device address information operations */ 134 int (*set_addr_info)(struct netdev *netdev, ip_addr_t *ip_addr, ip_addr_t *netmask, ip_addr_t *gw); 135 int (*set_dns_server)(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server); 136 int (*set_dhcp)(struct netdev *netdev, rt_bool_t is_enabled); 137 138 #ifdef RT_USING_FINSH 139 /* set network interface device common network interface device operations */ 140 int (*ping)(struct netdev *netdev, const char *host, size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp, rt_bool_t isbind); 141 void (*netstat)(struct netdev *netdev); 142 #endif 143 144 /* set default network interface device in current network stack*/ 145 int (*set_default)(struct netdev *netdev); 146 }; 147 148 /* The network interface device registered and unregistered*/ 149 int netdev_register(struct netdev *netdev, const char *name, void *user_data); 150 int netdev_unregister(struct netdev *netdev); 151 152 /* Get network interface device object */ 153 struct netdev *netdev_get_first_by_flags(uint16_t flags); 154 struct netdev *netdev_get_by_ipaddr(ip_addr_t *ip_addr); 155 struct netdev *netdev_get_by_name(const char *name); 156 struct netdev *netdev_get_by_ifindex(int ifindex); 157 #ifdef RT_USING_SAL 158 struct netdev *netdev_get_by_family(int family); 159 int netdev_family_get(struct netdev *netdev); 160 #endif /* RT_USING_SAL */ 161 #if defined(SAL_USING_AF_NETLINK) 162 int netdev_getnetdev(struct msg_buf *msg, int (*cb)(struct msg_buf *m_buf, struct netdev *nd, int nd_num, int index, int ipvx)); 163 #endif 164 165 /* Set default network interface device in list */ 166 void netdev_set_default(struct netdev *netdev); 167 void netdev_set_default_change_callback(netdev_callback_fn register_callback); 168 169 /* Set network interface device status */ 170 int netdev_set_up(struct netdev *netdev); 171 int netdev_set_down(struct netdev *netdev); 172 int netdev_dhcp_enabled(struct netdev *netdev, rt_bool_t is_enabled); 173 174 /* Get network interface device status */ 175 #define netdev_is_up(netdev) (((netdev)->flags & NETDEV_FLAG_UP) ? (uint8_t)1 : (uint8_t)0) 176 #define netdev_is_link_up(netdev) (((netdev)->flags & NETDEV_FLAG_LINK_UP) ? (uint8_t)1 : (uint8_t)0) 177 #define netdev_is_internet_up(netdev) (((netdev)->flags & NETDEV_FLAG_INTERNET_UP) ? (uint8_t)1 : (uint8_t)0) 178 #define netdev_is_dhcp_enabled(netdev) (((netdev)->flags & NETDEV_FLAG_DHCP) ? (uint8_t)1 : (uint8_t)0) 179 180 /* Set network interface device address */ 181 int netdev_set_ipaddr(struct netdev *netdev, const ip_addr_t *ipaddr); 182 int netdev_set_netmask(struct netdev *netdev, const ip_addr_t *netmask); 183 int netdev_set_gw(struct netdev *netdev, const ip_addr_t *gw); 184 void netdev_set_dns(char *netdev_name, uint8_t dns_num, char *dns_server); 185 int netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, const ip_addr_t *dns_server); 186 void netdev_set_if(char *netdev_name, char *ip_addr, char *gw_addr, char *nm_addr); 187 188 /* Set network interface device callback, it can be called when the status or address changed */ 189 void netdev_set_register_callback(netdev_callback_fn status_callback); 190 void netdev_set_status_callback(struct netdev *netdev, netdev_callback_fn status_callback); 191 void netdev_set_addr_callback(struct netdev *netdev, netdev_callback_fn addr_callback); 192 193 /* Set network interface device status and address, this function can only be called in the network interface device driver */ 194 void netdev_low_level_set_ipaddr(struct netdev *netdev, const ip_addr_t *ipaddr); 195 void netdev_low_level_set_netmask(struct netdev *netdev, const ip_addr_t *netmask); 196 void netdev_low_level_set_gw(struct netdev *netdev, const ip_addr_t *gw); 197 void netdev_low_level_set_dns_server(struct netdev *netdev, uint8_t dns_num, const ip_addr_t *dns_server); 198 void netdev_low_level_set_status(struct netdev *netdev, rt_bool_t is_up); 199 void netdev_low_level_set_link_status(struct netdev *netdev, rt_bool_t is_up); 200 void netdev_low_level_set_internet_status(struct netdev *netdev, rt_bool_t is_up); 201 void netdev_low_level_set_dhcp_status(struct netdev *netdev, rt_bool_t is_enable); 202 203 #ifdef __cplusplus 204 } 205 #endif 206 207 #endif /* __NETDEV_H__ */ 208