1 #ifndef __NETDEV_H__ 2 #define __NETDEV_H__ 3 4 #include <stdbool.h> 5 #include <aos/list.h> 6 #include <lwip/sockets.h> 7 8 #ifdef __cplusplus 9 extern "C" { 10 #endif 11 12 #define ASSERT (void) 13 14 #ifndef NETDEV_NAME_MAX 15 #define NETDEV_NAME_MAX 20U 16 #endif 17 18 /* the maximum of all used hardware address lengths */ 19 #ifndef NETDEV_HWADDR_MAX_LEN 20 #define NETDEV_HWADDR_MAX_LEN 8U 21 #endif 22 23 /* the maximum of dns server number supported */ 24 #ifndef NETDEV_DNS_SERVERS_NUM 25 #define NETDEV_DNS_SERVERS_NUM 2U 26 #endif 27 28 /* whether the network interface device is 'up' (set by the network interface driver or application) */ 29 #define NETDEV_FLAG_UP 0x01U 30 /* if set, the network interface device has broadcast capability, only supported in the 'lwIP' stack */ 31 #define NETDEV_FLAG_BROADCAST 0x02U 32 /* if set, the network interface device has an active link (set by the network interface driver) */ 33 #define NETDEV_FLAG_LINK_UP 0x04U 34 /* if set, the network interface device is an ethernet device using ARP, only supported in the 'lwIP' stack */ 35 #define NETDEV_FLAG_ETHARP 0x08U 36 /* if set, the network interface device is an ethernet device, only supported in the 'lwIP' stack */ 37 #define NETDEV_FLAG_ETHERNET 0x10U 38 /* if set, the network interface device has IGMP capability, only supported in the 'lwIP' stack */ 39 #define NETDEV_FLAG_IGMP 0x20U 40 /* if set, the network interface device has MLD6 capability, only supported in the 'lwIP' stack */ 41 #define NETDEV_FLAG_MLD6 0x40U 42 /* if set, the network interface device connected to internet successfully (set by the network interface driver) */ 43 #define NETDEV_FLAG_INTERNET_UP 0x80U 44 /* if set, the network interface device has DHCP capability (set by the network interface device driver or application) */ 45 #define NETDEV_FLAG_DHCP 0x100U 46 #define NETDEV_FLAG_DHCPD 0x200U 47 48 enum netdev_cb_type 49 { 50 NETDEV_CB_ADDR_IP, /* IP address */ 51 NETDEV_CB_ADDR_NETMASK, /* subnet mask */ 52 NETDEV_CB_ADDR_GATEWAY, /* netmask */ 53 NETDEV_CB_ADDR_DNS_SERVER, /* dns server */ 54 NETDEV_CB_STATUS_UP, /* changed to 'up' */ 55 NETDEV_CB_STATUS_DOWN, /* changed to 'down' */ 56 NETDEV_CB_STATUS_LINK_UP, /* changed to 'link up' */ 57 NETDEV_CB_STATUS_LINK_DOWN, /* changed to 'link down' */ 58 NETDEV_CB_STATUS_INTERNET_UP, /* changed to 'internet up' */ 59 NETDEV_CB_STATUS_INTERNET_DOWN, /* changed to 'internet down' */ 60 NETDEV_CB_STATUS_DHCP_ENABLE, /* enable DHCP capability */ 61 NETDEV_CB_STATUS_DHCP_DISABLE, /* disable DHCP capability */ 62 }; 63 64 struct netdev; 65 66 /* function prototype for network interface device status or address change callback functions */ 67 typedef void (*netdev_callback_fn )(struct netdev *netdev, enum netdev_cb_type type); 68 69 struct netdev_ops; 70 71 /* network interface device object */ 72 struct netdev 73 { 74 slist_t list; 75 76 char name[NETDEV_NAME_MAX]; /* network interface device name */ 77 ip_addr_t ip_addr; /* IP address */ 78 ip_addr_t netmask; /* subnet mask */ 79 ip_addr_t gw; /* gateway */ 80 ip_addr_t dns_servers[NETDEV_DNS_SERVERS_NUM]; /* DNS server */ 81 uint8_t hwaddr_len; /* hardware address length */ 82 uint8_t hwaddr[NETDEV_HWADDR_MAX_LEN]; /* hardware address */ 83 84 uint16_t flags; /* network interface device status flag */ 85 uint16_t mtu; /* maximum transfer unit (in bytes) */ 86 const struct netdev_ops *ops; /* network interface device operations */ 87 88 netdev_callback_fn status_callback; /* network interface device flags change callback */ 89 netdev_callback_fn addr_callback; /* network interface device address information change callback */ 90 91 #ifdef AOS_USING_SAL 92 void *sal_user_data; /* user-specific data for SAL */ 93 #endif /* AOS_USING_SAL */ 94 void *user_data; /* user-specific data */ 95 }; 96 97 /* The list of network interface device */ 98 extern struct netdev *netdev_list; 99 /* The default network interface device */ 100 extern struct netdev *netdev_default; 101 102 /* The network interface device ping response object */ 103 struct netdev_ping_resp 104 { 105 ip_addr_t ip_addr; /* response IP address */ 106 uint16_t data_len; /* response data length */ 107 uint16_t ttl; /* time to live */ 108 uint64_t ticks; /* response time, unit us */ 109 void *user_data; /* user-specific data */ 110 }; 111 112 /* The network interface device operations */ 113 struct netdev_ops 114 { 115 /* set network interface device hardware status operations */ 116 int (*set_up)(struct netdev *netdev); 117 int (*set_down)(struct netdev *netdev); 118 119 int (*set_default_netif)(struct netdev *netdev); 120 121 /* set network interface device address information operations */ 122 int (*set_addr_info)(struct netdev *netdev, ip_addr_t *ip_addr, ip_addr_t *netmask, ip_addr_t *gw); 123 int (*set_dns_server)(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server); 124 int (*set_dhcp)(struct netdev *netdev, bool is_enabled); 125 int (*set_dhcpd)(struct netdev *netdev, bool is_enabled); 126 127 /* set network interface device common network interface device operations */ 128 int (*ping)(struct netdev *netdev, const char *host, size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp); 129 void (*netstat)(struct netdev *netdev); 130 131 }; 132 133 /* The network interface device registered and unregistered*/ 134 int netdev_register(struct netdev *netdev, const char *name, void *user_data); 135 int netdev_unregister(struct netdev *netdev); 136 137 /* Get network interface device object */ 138 struct netdev *netdev_get_first_by_flags(uint16_t flags); 139 struct netdev *netdev_get_by_ipaddr(ip_addr_t *ip_addr); 140 struct netdev *netdev_get_by_name(const char *name); 141 #ifdef AOS_USING_SAL 142 struct netdev *netdev_get_by_family(int family); 143 int netdev_family_get(struct netdev *netdev); 144 #endif /* AOS_USING_SAL */ 145 146 /* Set default network interface device in list */ 147 void netdev_set_default(struct netdev *netdev); 148 149 /* Set network interface device status */ 150 int netdev_set_up(struct netdev *netdev); 151 int netdev_set_down(struct netdev *netdev); 152 int netdev_dhcp_enabled(struct netdev *netdev, bool is_enabled); 153 154 /* Get network interface device status */ 155 #define netdev_is_up(netdev) (((netdev)->flags & NETDEV_FLAG_UP) ? (uint8_t)1 : (uint8_t)0) 156 #define netdev_is_link_up(netdev) (((netdev)->flags & NETDEV_FLAG_LINK_UP) ? (uint8_t)1 : (uint8_t)0) 157 #define netdev_is_internet_up(netdev) (((netdev)->flags & NETDEV_FLAG_INTERNET_UP) ? (uint8_t)1 : (uint8_t)0) 158 #define netdev_is_dhcp_enabled(netdev) (((netdev)->flags & NETDEV_FLAG_DHCP) ? (uint8_t)1 : (uint8_t)0) 159 #define netdev_is_dhcpd_enabled(netdev) (((netdev)->flags & NETDEV_FLAG_DHCPD) ? (uint8_t)1 : (uint8_t)0) 160 161 /* Set network interface device address */ 162 int netdev_set_ipaddr(struct netdev *netdev, const ip_addr_t *ipaddr); 163 int netdev_set_netmask(struct netdev *netdev, const ip_addr_t *netmask); 164 int netdev_set_gw(struct netdev *netdev, const ip_addr_t *gw); 165 int netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, const ip_addr_t *dns_server); 166 167 /* Set network interface device callback, it can be called when the status or address changed */ 168 void netdev_set_status_callback(struct netdev *netdev, netdev_callback_fn status_callback); 169 170 /* Set network interface device status and address, this function can only be called in the network interface device driver */ 171 void netdev_low_level_set_ipaddr(struct netdev *netdev, const ip_addr_t *ipaddr); 172 void netdev_low_level_set_netmask(struct netdev *netdev, const ip_addr_t *netmask); 173 void netdev_low_level_set_gw(struct netdev *netdev, const ip_addr_t *gw); 174 void netdev_low_level_set_dns_server(struct netdev *netdev, uint8_t dns_num, const ip_addr_t *dns_server); 175 void netdev_low_level_set_status(struct netdev *netdev, bool is_up); 176 void netdev_low_level_set_link_status(struct netdev *netdev, bool is_up); 177 void netdev_low_level_set_dhcp_status(struct netdev *netdev, bool is_enable); 178 179 #ifdef __cplusplus 180 } 181 #endif 182 183 #endif /* __NETDEV_H__ */ 184