OpenHarmony之分布式软总线coap_discover.c(三)

100 篇文章 0 订阅
100 篇文章 1 订阅

往期知识点记录:

前言

本文会分析到coap_discover.c中有关wifi事件的消息队列处理过程。

coap_discover.c wifi事件消息队列

/*
    函数功能: 注册wifi回调函数
    函数参数: callback : 待注册的wifi回调函数
*/
void RegisterWifiCallback(WIFI_PROC_FUNC callback)
{
    g_wifiCallback = callback;
}
/*
    函数功能:处理wifi事件
    函数参数:para : wifi处理函数的参数
*/
void CoapHandleWifiEvent(unsigned int para)
{
    if (g_wifiCallback != NULL) {
        g_wifiCallback(para);
    }
}
/*
    函数功能: 将coap wifi事件加入到消息队列中
    函数参数: 事件处理状态(更新,清除)
    函数返回: 无
    详细:
        1、 声明一个地址事件处理结构
        2. 注册处理函数和状态
        3. 调用WriteMsgQue发送到对应id的消息队列中
*/
void CoapWriteMsgQueue(int state)
{
    SOFTBUS_PRINT("[DISCOVERY] CoapWriteMsgQueue\n");
    AddressEventHandler handler;
    handler.handler = CoapHandleWifiEvent;  //设置处理函数
    handler.state = state;
    /* while a new event coming, it must stop the previous loop */
    g_queryIpFlag = 0;
    (void)WriteMsgQue(g_wifiQueueId, &handler, sizeof(AddressEventHandler));
}
/*
    函数功能: 从消息队列中读取消息并处理
    函数返回值: 无
    详细:
        1.声明一个事件处理结构体,用来保存从消息队列中读到的消息
        2. 从消息队列中读取消息
        3. 使用AddressEventHandler中处理函数处理事件
*/
void CoapWifiEventThread(unsigned int uwParam1, unsigned int uwParam2, unsigned int uwParam3, unsigned int uwParam4)
{
    //这个参数在这个函数体中没有用,为了防止编译器发出警告
    (void)uwParam1;
    (void)uwParam2;
    (void)uwParam3;
    (void)uwParam4;
    //事件处理结构体
    AddressEventHandler handle;
    unsigned int readSize;
    unsigned int ret;
    readSize = sizeof(AddressEventHandler);
    g_wifiTaskStart = 1; // 消息队列中消息
    while (g_wifiTaskStart) {
        //从消息队列中读取消息
        ret = ReadMsgQue(g_wifiQueueId, &handle, &readSize);
        if ((ret == 0) && (readSize == sizeof(AddressEventHandler))) {
            if (handle.handler == NULL) {
                continue;
            }
            //使用处理函数处理
            handle.handler(handle.state);
        }
    }
}
/*
    函数功能: 撤销wifi事件
    函数参数: 无
    函数返回值: 成功返回0,否则返回
    详细:
        1.判断是否还在处理事件,如果在处理那么就等待处理完成
        2.将g_msgQueTaskId设置为-1
        3.断开消息队列描述符g_wifiQueueId 和 与之对应消息队列间的连接
*/
int CoapDeinitWifiEvent(void)
{
    unsigned int ret;
    g_wifiTaskStart = 0;
#if defined(__LITEOS_M__) || defined(__LITEOS_RISCV__)
    if (g_msgQueTaskId != NULL) {
        ret = osThreadTerminate(g_msgQueTaskId);
        if (ret != 0) {
            return -1;
        }
        g_msgQueTaskId = NULL;
    }
#else
    //判断是否还在处理事件,如果在处理那么就等待处理完成
    if (g_msgQueTaskId != -1) {
        ret = pthread_join((pthread_t)g_msgQueTaskId, NULL);
        if (ret != 0) {
            SOFTBUS_PRINT("[DISCOVERY] pthread_join fail\n");
            return -1;
        }
        g_msgQueTaskId = -1;
    }
#endif
    if (g_wifiQueueId != -1) {
        ret = DeleteMsgQue(g_wifiQueueId);
        if (ret != 0) {
            return -1;
        }
        g_wifiQueueId = -1;
    }
#if defined(__LITEOS_M__) || defined(__LITEOS_RISCV__)
    WifiErrorCode error = UnRegisterWifiEvent(&g_coapEventHandler);
    if (error != WIFI_SUCCESS) {
        return -1;
    }
    g_coapEventHandler.OnWifiConnectionChanged = NULL;
#endif
    return NSTACKX_EOK;
}
/*
    函数处理: 创建消息队列处理线程
    函数参数:无
    函数返回值: 成功返回0,否则返回非零
    详细:
        1. 定义该线程的属性attr
        2. 创建线程,线程函数为CoapWifiEventThread,线程属性为attr,线程id=g_msgQueTaskId
*/
int CreateMsgQueThread(void)
{
#if defined(__LITEOS_M__) || defined(__LITEOS_RISCV__)
    if (g_msgQueTaskId != NULL) {
        return NSTACKX_EOK;
    }
    osThreadAttr_t attr;
    attr.name = "wifi_event";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    attr.priority = osPriorityNormal4; // COAP_DEFAULT_PRIO -> cmsis prio
    g_msgQueTaskId = osThreadNew((osThreadFunc_t)CoapWifiEventThread, NULL, &attr);
    if (NULL == g_msgQueTaskId) {
        SOFTBUS_PRINT("[DISCOVERY]Task Create fail\n");
        return NSTACKX_EOK;
    }
#else
    int ret;
    if (g_msgQueTaskId != -1) {
        return NSTACKX_EOK;
    }
    ThreadAttr attr = {"wifi_event", 0x800, 20, 0, 0};
    ret = CreateThread((Runnable)CoapWifiEventThread, NULL, &attr, (unsigned int*)&g_msgQueTaskId);
    if (ret != 0) {
        SOFTBUS_PRINT("[DISCOVERY]Task Create fail\n");
        return ret;
    }
#endif
    return NSTACKX_EOK;
}
/*
    函数功能: 初始化 wifi事件
    函数参数: 无
    函数返回值: 成功返回0,否则返回非零
    详细:
        1.如果wifi队列ID ==-1 ,表示之前没有创建消息队列,就创建消息队列
        2.创建消息队列,队列名"/wifiQue",长度WIFI_QUEUE_SIZE,队列ID g_wifiQueueId,最大消息大小sizeof(AddressEventHandler)
        3. 如果创建失败就撤销之前的操作
*/
int CoapInitWifiEvent(void)
{
    SOFTBUS_PRINT("[DISCOVERY] CoapInitWifiEvent\n");
    unsigned int ret;
    if (g_wifiQueueId == -1) {
        //创建消息队列 5*sizeof(AddressEventHandler)
        ret = CreateMsgQue("/wifiQue",
            WIFI_QUEUE_SIZE, (unsigned int*)&g_wifiQueueId,
            0, sizeof(AddressEventHandler));
        if (ret != 0) {  //创建消息队列失败,那么就销毁事件
            SOFTBUS_PRINT("[DISCOVERY]CreateMsgQue fail\n");
            (void)CoapDeinitWifiEvent();
            return ret;
        }
#if defined(__LITEOS_M__) || defined(__LITEOS_RISCV__)
        g_coapEventHandler.OnWifiConnectionChanged = CoapConnectionChangedHandler;
        WifiErrorCode error = RegisterWifiEvent(&g_coapEventHandler);
        if (error != WIFI_SUCCESS) {
            SOFTBUS_PRINT("[DISCOVERY]RegisterWifiEvent fail, error:%d\n", error);
            (void)CoapDeinitWifiEvent();
            g_wifiQueueId = -1;
            return error;
        }
#endif
    }
    return NSTACKX_EOK;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值