1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "gap_def.h"
17 #include "gap_task_internal.h"
18
19 #include "allocator.h"
20
21 #include "btm/btm_thread.h"
22 #include "log.h"
23
GapBlockInTaskProcess(void * ctx)24 void GapBlockInTaskProcess(void *ctx)
25 {
26 GapRunTaskBlockInfo *info = ctx;
27 if (info->func != NULL) {
28 info->func(info->ctx);
29 }
30 if (info->event != NULL) {
31 EventSet(info->event);
32 }
33 }
34
GapUnBlockInTaskProcess(void * ctx)35 NO_SANITIZE("cfi") void GapUnBlockInTaskProcess(void *ctx)
36 {
37 GapRunTaskUnBlockInfo *info = ctx;
38 if (info != NULL) {
39 if (info->func != NULL) {
40 info->func(info->ctx);
41 }
42 if (info->free != NULL) {
43 info->free(info->ctx);
44 }
45 if (info->ctx != NULL) {
46 MEM_MALLOC.free(info->ctx);
47 }
48 MEM_MALLOC.free(info);
49 }
50 }
51
GapRunTaskBlockProcess(void (* func)(void *),void * ctx)52 int GapRunTaskBlockProcess(void (*func)(void *), void *ctx)
53 {
54 GapRunTaskBlockInfo *info = MEM_MALLOC.alloc(sizeof(GapRunTaskBlockInfo));
55 if (info == NULL) {
56 return BT_NO_MEMORY;
57 }
58
59 info->event = EventCreate(true);
60 info->ctx = ctx;
61 info->func = func;
62
63 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_GAP, GapBlockInTaskProcess, info);
64 if (ret == BT_SUCCESS) {
65 ret = EventWait(info->event, WAIT_TIME);
66 if (ret == EVENT_WAIT_TIMEOUT_ERR) {
67 HILOGE("EventWait result is timeout");
68 ret = BT_TIMEOUT;
69 } else if (ret == EVENT_WAIT_OTHER_ERR) {
70 HILOGE("EventWait result is wait err");
71 ret = BT_OS_ERROR;
72 }
73 }
74
75 EventDelete(info->event);
76 MEM_MALLOC.free(info);
77 return ret;
78 }
79
GapRunTaskUnBlockProcess(void (* const func)(void *),void * ctx,void (* const free)(void *))80 int GapRunTaskUnBlockProcess(void (*const func)(void *), void *ctx, void (*const free)(void *))
81 {
82 GapRunTaskUnBlockInfo *info = MEM_MALLOC.alloc(sizeof(GapRunTaskUnBlockInfo));
83 if (info == NULL) {
84 return BT_NO_MEMORY;
85 }
86
87 info->ctx = ctx;
88 info->func = func;
89 info->free = free;
90
91 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_GAP, GapUnBlockInTaskProcess, info);
92
93 return ret;
94 }
95