wlan_workqueue.c 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-08-19     tyx          the first version
 */

#include <rthw.h>
#include <rtthread.h>
#include <wlan_workqueue.h>
#include <ipc/workqueue.h>

#define DBG_ENABLE
#define DBG_LEVEL DBG_INFO
#define DBG_SECTION_NAME  "WLAN.work"
#define DBG_COLOR
#include <rtdbg.h>

struct rt_wlan_work
{
    struct rt_work work;
    void (*fun)(void *parameter);
    void *parameter;
};

static struct rt_workqueue *wlan_workqueue;

static void rt_wlan_workqueue_fun(struct rt_work *work, void *work_data)
{
    struct rt_wlan_work *wlan_work = work_data;

    wlan_work->fun(wlan_work->parameter);
    rt_free(wlan_work);
}

struct rt_workqueue *rt_wlan_get_workqueue(void)
{
    return wlan_workqueue;
}

rt_err_t rt_wlan_workqueue_dowork(void (*func)(void *parameter), void *parameter)
{
    struct rt_wlan_work *wlan_work;
    rt_err_t err = RT_EOK;

    LOG_D("F:%s is run", __FUNCTION__);
    if (func == RT_NULL)
    {
        LOG_E("F:%s L:%d func is null", __FUNCTION__, __LINE__);
        return -RT_EINVAL;
    }

    if (wlan_workqueue == RT_NULL)
    {
        LOG_E("F:%s L:%d not init wlan work queue", __FUNCTION__, __LINE__);
        return -RT_ERROR;
    }

    wlan_work = rt_malloc(sizeof(struct rt_wlan_work));
    if (wlan_work == RT_NULL)
    {
        LOG_E("F:%s L:%d create work failed", __FUNCTION__, __LINE__);
        return -RT_ENOMEM;
    }
    wlan_work->fun = func;
    wlan_work->parameter = parameter;
    rt_work_init(&wlan_work->work, rt_wlan_workqueue_fun, wlan_work);
    err = rt_workqueue_dowork(wlan_workqueue, &wlan_work->work);
    if (err != RT_EOK)
    {
        LOG_E("F:%s L:%d do work failed", __FUNCTION__, __LINE__);
        rt_free(wlan_work);
        return err;
    }
    return err;
}

int rt_wlan_workqueue_init(void)
{
    static rt_int8_t _init_flag = 0;

    if (_init_flag == 0)
    {
        wlan_workqueue = rt_workqueue_create(RT_WLAN_WORKQUEUE_THREAD_NAME, RT_WLAN_WORKQUEUE_THREAD_SIZE,
                                             RT_WLAN_WORKQUEUE_THREAD_PRIO);
        if (wlan_workqueue == RT_NULL)
        {
            LOG_E("F:%s L:%d wlan work queue create failed", __FUNCTION__, __LINE__);
            return -1;
        }
        _init_flag = 1;
        return 0;
    }
    return 0;
}
INIT_PREV_EXPORT(rt_wlan_workqueue_init);