1 /*
2 * Copyright (c) 2006-2018, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2018-08-19 tyx the first version
9 */
10
11 #include <rthw.h>
12 #include <rtthread.h>
13 #include <wlan_workqueue.h>
14 #include <ipc/workqueue.h>
15
16 #define DBG_ENABLE
17 #define DBG_LEVEL DBG_INFO
18 #define DBG_SECTION_NAME "WLAN.work"
19 #define DBG_COLOR
20 #include <rtdbg.h>
21
22 struct rt_wlan_work
23 {
24 struct rt_work work;
25 void (*fun)(void *parameter);
26 void *parameter;
27 };
28
29 static struct rt_workqueue *wlan_workqueue;
30
rt_wlan_workqueue_fun(struct rt_work * work,void * work_data)31 static void rt_wlan_workqueue_fun(struct rt_work *work, void *work_data)
32 {
33 struct rt_wlan_work *wlan_work = work_data;
34
35 wlan_work->fun(wlan_work->parameter);
36 rt_free(wlan_work);
37 }
38
rt_wlan_get_workqueue(void)39 struct rt_workqueue *rt_wlan_get_workqueue(void)
40 {
41 return wlan_workqueue;
42 }
43
rt_wlan_workqueue_dowork(void (* func)(void * parameter),void * parameter)44 rt_err_t rt_wlan_workqueue_dowork(void (*func)(void *parameter), void *parameter)
45 {
46 struct rt_wlan_work *wlan_work;
47 rt_err_t err = RT_EOK;
48
49 LOG_D("F:%s is run", __FUNCTION__);
50 if (func == RT_NULL)
51 {
52 LOG_E("F:%s L:%d func is null", __FUNCTION__, __LINE__);
53 return -RT_EINVAL;
54 }
55
56 if (wlan_workqueue == RT_NULL)
57 {
58 LOG_E("F:%s L:%d not init wlan work queue", __FUNCTION__, __LINE__);
59 return -RT_ERROR;
60 }
61
62 wlan_work = rt_malloc(sizeof(struct rt_wlan_work));
63 if (wlan_work == RT_NULL)
64 {
65 LOG_E("F:%s L:%d create work failed", __FUNCTION__, __LINE__);
66 return -RT_ENOMEM;
67 }
68 wlan_work->fun = func;
69 wlan_work->parameter = parameter;
70 rt_work_init(&wlan_work->work, rt_wlan_workqueue_fun, wlan_work);
71 err = rt_workqueue_dowork(wlan_workqueue, &wlan_work->work);
72 if (err != RT_EOK)
73 {
74 LOG_E("F:%s L:%d do work failed", __FUNCTION__, __LINE__);
75 rt_free(wlan_work);
76 return err;
77 }
78 return err;
79 }
80
rt_wlan_workqueue_init(void)81 int rt_wlan_workqueue_init(void)
82 {
83 static rt_int8_t _init_flag = 0;
84
85 if (_init_flag == 0)
86 {
87 wlan_workqueue = rt_workqueue_create(RT_WLAN_WORKQUEUE_THREAD_NAME, RT_WLAN_WORKQUEUE_THREAD_SIZE,
88 RT_WLAN_WORKQUEUE_THREAD_PRIO);
89 if (wlan_workqueue == RT_NULL)
90 {
91 LOG_E("F:%s L:%d wlan work queue create failed", __FUNCTION__, __LINE__);
92 return -1;
93 }
94 _init_flag = 1;
95 return 0;
96 }
97 return 0;
98 }
99 INIT_PREV_EXPORT(rt_wlan_workqueue_init);
100