c语言嵌入式回调函数,嵌入式MCU C语言实现回调函数设计

1.有这么一个列表维护这全部回调函数的指针 回调函数的注册就是给列表的添加回到函数指针函数

遍历列表逐个调指针 便可实现回调prototype

typedef void (*tls_netif_status_event_fn)(u8 status);

struct tls_netif_status_event

{

struct dl_list list;

tls_netif_status_event_fn status_callback;

};

2.函数指针类型定义 形式以下指针

typedef err_t (*netif_init_fn)(struct netif *netif);

typedef err_t (*netif_input_fn)(struct pbuf *p, struct netif *inp);

typedef err_t (*netif_linkoutput_fn)(struct netif *netif, struct pbuf *p);

/** Function prototype for netif status- or link-callback functions. */

typedef void (*netif_status_callback_fn)(struct netif *netif);

/** Function prototype for netif igmp_mac_filter functions */

typedef err_t (*netif_igmp_mac_filter_fn)(struct netif *netif,ip_addr_t *group, u8_t action);

2.将回调函数加入列表中code

err_t tls_netif_add_status_event(tls_netif_status_event_fn event_fn)

{

u32 cpu_sr;

struct tls_netif_status_event *evt;

//if exist, remove from event list first.

tls_netif_remove_status_event(event_fn);

evt = tls_mem_alloc(sizeof(struct tls_netif_status_event));

if(evt==NULL)

return -1;

memset(evt, 0, sizeof(struct tls_netif_status_event));

evt->status_callback = event_fn;

cpu_sr = tls_os_set_critical();

dl_list_add_tail(&netif_status_event.list, &evt->list);

tls_os_release_critical(cpu_sr);

return 0;

}

2.1将回调函数从列表中删除接口

err_t tls_netif_remove_status_event(tls_netif_status_event_fn event_fn)

{

struct tls_netif_status_event *status_event;

bool is_exist = FALSE;

u32 cpu_sr;

if(dl_list_empty(&netif_status_event.list))

return 0;

dl_list_for_each(status_event, &netif_status_event.list, struct tls_netif_status_event, list)

{

if(status_event->status_callback == event_fn)

{

is_exist = TRUE;

break;

}

}

if(is_exist)

{

cpu_sr = tls_os_set_critical();

dl_list_del(&status_event->list);

tls_os_release_critical(cpu_sr);

tls_mem_free(status_event);

}

return 0;

}

注册使用ip

netif_set_status_callback(nif, netif_status_changed);

tls_wifi_status_change_cb_register(wifi_status_changed);

注册函数接口rem

void tls_wifi_status_change_cb_register(void (*callback)(u8 status));

最后 有一个周期执行的函数 遍历列表 逐个调用列表的函数指针input

static void wifi_status_changed(u8 status)

{

struct tls_netif_status_event *status_event;

dl_list_for_each(status_event, &netif_status_event.list, struct tls_netif_status_event, list)

{

if(status_event->status_callback != NULL)

{

switch(status)

{

case WIFI_JOIN_SUCCESS:

status_event->status_callback(NETIF_WIFI_JOIN_SUCCESS);

break;

case WIFI_JOIN_FAILED:

status_event->status_callback(NETIF_WIFI_JOIN_FAILED);

break;

case WIFI_DISCONNECTED:

status_event->status_callback(NETIF_WIFI_DISCONNECTED);

break;

#if TLS_CONFIG_APSTA

case WIFI_APSTA_STA_JOIN_SUCCESS:

status_event->status_callback(NETIF_WIFI_APSTA_STA_SUCCESS);

break;

case WIFI_APSTA_AP_JOIN_SUCCESS:

status_event->status_callback(NETIF_WIFI_APSTA_AP_SUCCESS);

break;

#endif

default:

break;

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值