netdev.h 8.1 KB

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