From 20f637dbdad084fa6982f44f8a4ea53b9d57dc10 Mon Sep 17 00:00:00 2001 From: camila0627 <491818890@qq.com> Date: Sat, 31 Aug 2024 15:51:06 +0800 Subject: [PATCH] updata L0 send data --- .../src/mini/mine_softbus_adapter.c | 107 +++++++++++++----- 1 file changed, 76 insertions(+), 31 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/src/mini/mine_softbus_adapter.c b/interfaces/inner_kits/native_cpp/src/mini/mine_softbus_adapter.c index 75136b79..3930b089 100644 --- a/interfaces/inner_kits/native_cpp/src/mini/mine_softbus_adapter.c +++ b/interfaces/inner_kits/native_cpp/src/mini/mine_softbus_adapter.c @@ -29,6 +29,7 @@ #include "los_mux.h" #include "los_sem.h" #include "los_swtmr.h" +#include "los_queue.h" #include "ohos_init.h" #include "parameter.h" #include "securec.h" @@ -167,7 +168,13 @@ static void MatchDealTask(void *param); static void OnPublishDeviceFound(const DeviceInfo *deviceInfo); static UINT32 g_discoveryMapMutex = 0; -static UINT32 g_matchQueueMutex = 0; + +static osThreadId_t g_matchDeviceTaskId = NULL; +static UINT32 g_matchDeviceQueueId = 0; + +const CHAR *g_matchDeviceQueueName = "matchDeviceQueue"; +static const UINT16 g_matchDeviceQueueLen = 5; +static const UINT16 g_matchDeviceMaxMsgSize = sizeof(DeviceInfo); static struct { int subscribeId; @@ -1257,31 +1264,61 @@ static void OnRefreshDiscoveryResult(int32_t refreshId, RefreshResult reason) int CreateSemaphoreAndMutex(void) { - UINT32 osRet = LOS_MuxCreate(&g_matchQueueMutex); + int retCode = DM_OK; + + UINT32 osRet = LOS_MuxCreate(&g_discoveryMapMutex); if (osRet != LOS_OK) { - DMLOGE("failed to create match queue mutex with ret: %d", osRet); - return ERR_DM_LITEOS_FAILED; + DMLOGE("failed to create discovery queue mutex with ret: %d", osRet); + retCode = ERR_DM_LITEOS_FAILED; + goto EXIT; } - osRet = LOS_MuxCreate(&g_discoveryMapMutex); + + osRet = LOS_QueueCreate(g_matchDeviceQueueName, g_matchDeviceQueueLen, &g_matchDeviceQueueId, 0, g_matchDeviceMaxMsgSize); if (osRet != LOS_OK) { - DMLOGE("failed to create discovery queue mutex with ret: %d", osRet); - return ERR_DM_LITEOS_FAILED; + DMLOGE("failed to create match queue mutex with ret: %d", osRet); + retCode = ERR_DM_LITEOS_FAILED; + goto EXIT; } - return DM_OK; + + // create thread + osThreadAttr_t attr = { 0 }; + attr.stack_size = LIC_LITEOS_M_STACK_SIZE; + attr.priority = osPriorityNormal; + attr.name = "DeviceManagerTask"; + g_matchDeviceTaskId = osThreadNew((osThreadFunc_t)MatchDealTask, NULL, &attr); + if (g_matchDeviceTaskId == NULL) { + DMLOGE("osThreadNew fail."); + retCode = ERR_DM_LITEOS_FAILED; + } + +EXIT: + if (retCode != DM_OK) { + DeleteSemaphoreAndMutex(); + } + + return retCode; } int DeleteSemaphoreAndMutex(void) { - UINT32 osRet = LOS_MuxDelete(g_matchQueueMutex); - if (osRet != LOS_OK && osRet != LOS_ERRNO_MUX_INVALID) { - DMLOGE("failed to delete match queue mutex with ret: %d", osRet); - return ERR_DM_LITEOS_FAILED; + if (g_matchDeviceTaskId != NULL) { + osStatus_t status = osThreadTerminate(g_matchDeviceTaskId); + if (status != osOK) { + DMLOGE("failed to terminate task ret: %d", status); + } } - osRet = LOS_MuxDelete(g_discoveryMapMutex); + + UINT32 osRet = LOS_MuxDelete(g_discoveryMapMutex); if (osRet != LOS_OK && osRet != LOS_ERRNO_MUX_INVALID) { DMLOGE("failed to delete discovery queue mutex with ret: %d", osRet); - return ERR_DM_LITEOS_FAILED; } + + osRet = LOS_QueueDelete(g_matchDeviceQueueId); + if (osRet != LOS_OK && osRet != LOS_ERRNO_QUEUE_NOT_FOUND) { + DMLOGE("failed to delete match device queue with ret: %d", osRet); + } + + return DM_OK; } static void OnPublishDeviceFound(const DeviceInfo *deviceInfo) @@ -1304,30 +1341,38 @@ static void OnPublishDeviceFound(const DeviceInfo *deviceInfo) } DMLOGI("broadcast data is received with DataLen: %u", custDataLen); - UINT32 osRet = LOS_MuxPend(g_matchQueueMutex, SOFTBUS_DELAY_TICK_COUNT); + + UINT32 osRet = LOS_QueueWriteCopy(g_matchDeviceQueueId, deviceInfo, sizeof(*deviceInfo), SOFTBUS_DELAY_TICK_COUNT); if (osRet != LOS_OK) { - DMLOGE("failed to pend match queue mutex with ret: %u.", osRet); + DMLOGE("failed to pend match queue with ret: %u.", osRet); return; } - osThreadAttr_t attr = { 0 }; - attr.stack_size = LIC_LITEOS_M_STACK_SIZE; - attr.priority = osPriorityNormal; - attr.name = "DeviceManagerTask"; - osThreadId_t *taskId = osThreadNew((osThreadFunc_t)MatchDealTask, (void *)deviceInfo, &attr); - if (taskId == NULL) { - DMLOGE("osThreadNew fail."); - } - osRet = LOS_MuxPost(g_matchQueueMutex); - if (osRet != LOS_OK) { - DMLOGE("failed to post match queue mutex with ret: %u.", osRet); - } } static void MatchDealTask(void *param) { - DeviceInfo deviceInfo = *((DeviceInfo *)param); - if (ParseBroadcastInfo(&deviceInfo) != DM_OK) { - DMLOGE("failed to parse broadcast info."); + (void)param; + + UINT32 osRet; + UINT32 buffLen; + DeviceInfo deviceInfo; + while (1) { + memset_s(&deviceInfo, sizeof(deviceInfo), 0, sizeof(deviceInfo)); + buffLen = sizeof(deviceInfo); + osRet = LOS_QueueReadCopy(g_matchDeviceQueueId, &deviceInfo, &buffLen, LOS_WAIT_FOREVER); + if (osRet != LOS_OK) { + DMLOGE("failed to read match device queue with ret: %u.", osRet); + continue; + } + if (buffLen != sizeof(deviceInfo)) { + DMLOGE("failed to read match device queue, buffLen %u, deviceLen %u.", buffLen, sizeof(deviceInfo)); + continue; + } + + int result = ParseBroadcastInfo(&deviceInfo); + if (result != DM_OK) { + DMLOGE("failed to parse broadcast info, ret %d.", result); + } } } -- Gitee