Skip to content

Commit 3560e43

Browse files
committed
[refact](netdev): Expose netdev_set_dns and netdev_set_if functions
[Descriptions]: 1. Make these functions public to avoid code duplication and allow direct usage by other components. 2. Apply clang-format for consistent code style. [Root Cause]:NA [Side Effects]:NA
1 parent 45eea78 commit 3560e43

File tree

2 files changed

+183
-169
lines changed

2 files changed

+183
-169
lines changed

components/net/netdev/include/netdev.h

+60-58
Original file line numberDiff line numberDiff line change
@@ -19,62 +19,62 @@ extern "C" {
1919

2020
/* the maximum of all used hardware address lengths */
2121
#ifndef NETDEV_HWADDR_MAX_LEN
22-
#define NETDEV_HWADDR_MAX_LEN 8U
22+
#define NETDEV_HWADDR_MAX_LEN 8U
2323
#endif
2424

2525
/* the maximum of dns server number supported */
2626
#ifndef NETDEV_DNS_SERVERS_NUM
27-
#define NETDEV_DNS_SERVERS_NUM 2U
27+
#define NETDEV_DNS_SERVERS_NUM 2U
2828
#endif
2929

3030
#if NETDEV_IPV6
3131
/* the maximum of dns server number supported */
3232
#ifndef NETDEV_IPV6_NUM_ADDRESSES
33-
#define NETDEV_IPV6_NUM_ADDRESSES 3U
33+
#define NETDEV_IPV6_NUM_ADDRESSES 3U
3434
#endif
3535
#endif /* NETDEV_IPV6 */
3636

3737
/* whether the network interface device is 'up' (set by the network interface driver or application) */
38-
#define NETDEV_FLAG_UP 0x01U
38+
#define NETDEV_FLAG_UP 0x01U
3939
/* if set, the network interface device has broadcast capability, only supported in the 'lwIP' stack */
40-
#define NETDEV_FLAG_BROADCAST 0x02U
40+
#define NETDEV_FLAG_BROADCAST 0x02U
4141
/* if set, the network interface device has an active link (set by the network interface driver) */
42-
#define NETDEV_FLAG_LINK_UP 0x04U
42+
#define NETDEV_FLAG_LINK_UP 0x04U
4343
/* if set, the network interface device is an ethernet device using ARP, only supported in the 'lwIP' stack */
44-
#define NETDEV_FLAG_ETHARP 0x08U
44+
#define NETDEV_FLAG_ETHARP 0x08U
4545
/* if set, the network interface device is an ethernet device, only supported in the 'lwIP' stack */
46-
#define NETDEV_FLAG_ETHERNET 0x10U
46+
#define NETDEV_FLAG_ETHERNET 0x10U
4747
/* if set, the network interface device has IGMP capability, only supported in the 'lwIP' stack */
48-
#define NETDEV_FLAG_IGMP 0x20U
48+
#define NETDEV_FLAG_IGMP 0x20U
4949
/* if set, the network interface device has MLD6 capability, only supported in the 'lwIP' stack */
50-
#define NETDEV_FLAG_MLD6 0x40U
50+
#define NETDEV_FLAG_MLD6 0x40U
5151
/* if set, the network interface device connected to internet successfully (set by the network interface driver) */
52-
#define NETDEV_FLAG_INTERNET_UP 0x80U
52+
#define NETDEV_FLAG_INTERNET_UP 0x80U
5353
/* if set, the network interface device has DHCP capability (set by the network interface device driver or application) */
54-
#define NETDEV_FLAG_DHCP 0x100U
54+
#define NETDEV_FLAG_DHCP 0x100U
5555

5656
enum netdev_cb_type
5757
{
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 */
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 */
7272
};
7373

7474
struct netdev;
7575

7676
/* 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);
77+
typedef void (*netdev_callback_fn)(struct netdev *netdev, enum netdev_cb_type type);
7878

7979
struct netdev_ops;
8080

@@ -83,30 +83,30 @@ struct netdev
8383
{
8484
rt_slist_t list;
8585

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 */
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 */
9090
#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 */
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 */
9696

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 */
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 */
100100

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 */
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 */
103103

104-
int ifindex; /* network interface device ifindex */
104+
int ifindex; /* network interface device ifindex */
105105

106106
#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 */
107+
void *sal_user_data; /* user-specific data for SAL */
108+
#endif /* RT_USING_SAL */
109+
void *user_data; /* user-specific data */
110110
};
111111

112112
/* The list of network interface device */
@@ -116,11 +116,11 @@ extern struct netdev *netdev_default;
116116
/* The network interface device ping response object */
117117
struct netdev_ping_resp
118118
{
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 */
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 */
124124
};
125125

126126
/* The network interface device operations */
@@ -156,7 +156,7 @@ struct netdev *netdev_get_by_name(const char *name);
156156
struct netdev *netdev_get_by_ifindex(int ifindex);
157157
#ifdef RT_USING_SAL
158158
struct netdev *netdev_get_by_family(int family);
159-
int netdev_family_get(struct netdev *netdev);
159+
int netdev_family_get(struct netdev *netdev);
160160
#endif /* RT_USING_SAL */
161161
#if defined(SAL_USING_AF_NETLINK)
162162
int netdev_getnetdev(struct msg_buf *msg, int (*cb)(struct msg_buf *m_buf, struct netdev *nd, int nd_num, int index, int ipvx));
@@ -172,16 +172,18 @@ int netdev_set_down(struct netdev *netdev);
172172
int netdev_dhcp_enabled(struct netdev *netdev, rt_bool_t is_enabled);
173173

174174
/* 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)
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)
178178
#define netdev_is_dhcp_enabled(netdev) (((netdev)->flags & NETDEV_FLAG_DHCP) ? (uint8_t)1 : (uint8_t)0)
179179

180180
/* 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-
int netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, const ip_addr_t *dns_server);
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);
185187

186188
/* Set network interface device callback, it can be called when the status or address changed */
187189
void netdev_set_register_callback(netdev_callback_fn status_callback);

0 commit comments

Comments
 (0)