[net][netdev]Add more hook functions

This commit is contained in:
tyx 2022-10-29 09:17:55 +08:00 committed by guo
parent 8aaa57e2ea
commit 7ec14378ef
2 changed files with 41 additions and 0 deletions

View File

@ -67,6 +67,8 @@ enum netdev_cb_type
NETDEV_CB_STATUS_INTERNET_DOWN, /* changed to 'internet down' */
NETDEV_CB_STATUS_DHCP_ENABLE, /* enable DHCP capability */
NETDEV_CB_STATUS_DHCP_DISABLE, /* disable DHCP capability */
NETDEV_CB_REGISTER, /* netdev register */
NETDEV_CB_DEFAULT_CHANGE, /* netdev default change */
};
struct netdev;
@ -157,6 +159,7 @@ int netdev_family_get(struct netdev *netdev);
/* Set default network interface device in list */
void netdev_set_default(struct netdev *netdev);
void netdev_set_default_change_callback(netdev_callback_fn register_callback);
/* Set network interface device status */
int netdev_set_up(struct netdev *netdev);
@ -176,6 +179,7 @@ int netdev_set_gw(struct netdev *netdev, const ip_addr_t *gw);
int netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, const ip_addr_t *dns_server);
/* Set network interface device callback, it can be called when the status or address changed */
void netdev_set_register_callback(netdev_callback_fn status_callback);
void netdev_set_status_callback(struct netdev *netdev, netdev_callback_fn status_callback);
void netdev_set_addr_callback(struct netdev *netdev, netdev_callback_fn addr_callback);

View File

@ -31,6 +31,9 @@
struct netdev *netdev_list = RT_NULL;
/* The default network interface device */
struct netdev *netdev_default = RT_NULL;
/* The global network register callback */
static netdev_callback_fn g_netdev_register_callback = RT_NULL;
static netdev_callback_fn g_netdev_default_change_callback = RT_NULL;
/**
* This function will register network interface device and
@ -112,6 +115,12 @@ int netdev_register(struct netdev *netdev, const char *name, void *user_data)
netdev_set_default(netdev_list);
}
/* execute netdev register callback */
if (g_netdev_register_callback)
{
g_netdev_register_callback(netdev, NETDEV_CB_REGISTER);
}
return RT_EOK;
}
@ -179,6 +188,17 @@ int netdev_unregister(struct netdev *netdev)
return -RT_ERROR;
}
/**
* This function will set register callback
*
* @param register_callback the network register callback
*
*/
void netdev_set_register_callback(netdev_callback_fn register_callback)
{
g_netdev_register_callback = register_callback;
}
/**
* This function will get the first network interface device
* with the flags in network interface device list.
@ -373,10 +393,27 @@ void netdev_set_default(struct netdev *netdev)
{
netdev->ops->set_default(netdev);
}
/* execture application netdev default change callback */
if (g_netdev_default_change_callback)
{
g_netdev_default_change_callback(netdev, NETDEV_CB_DEFAULT_CHANGE);
}
LOG_D("Setting default network interface device name(%s) successfully.", netdev->name);
}
}
/**
* This function will set defalut netdev change callback
*
* @param register_callback the network default change callback
*
*/
void netdev_set_default_change_callback(netdev_callback_fn register_callback)
{
g_netdev_default_change_callback = register_callback;
}
/**
* This function will enable network interface device .
*