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
GapBlockInTaskProcess(void * ctx)23 void GapBlockInTaskProcess(void *ctx)
24 {
25 GapRunTaskBlockInfo *info = ctx;
26 if (info->func != NULL) {
27 info->func(info->ctx);
28 }
29 if (info->event != NULL) {
30 EventSet(info->event);
31 }
32 }
33
GapUnBlockInTaskProcess(void * ctx)34 NO_SANITIZE("cfi") void GapUnBlockInTaskProcess(void *ctx)
35 {
36 GapRunTaskUnBlockInfo *info = ctx;
37 if (info != NULL) {
38 if (info->func != NULL) {
39 info->func(info->ctx);
40 }
41 if (info->free != NULL) {
42 info->free(info->ctx);
43 }
44 if (info->ctx != NULL) {
45 MEM_MALLOC.free(info->ctx);
46 }
47 MEM_MALLOC.free(info);
48 }
49 }
50
GapRunTaskBlockProcess(void (* func)(void *),void * ctx)51 int GapRunTaskBlockProcess(void (*func)(void *), void *ctx)
52 {
53 GapRunTaskBlockInfo *info = MEM_MALLOC.alloc(sizeof(GapRunTaskBlockInfo));
54 if (info == NULL) {
55 return BT_NO_MEMORY;
56 }
57
58 info->event = EventCreate(true);
59 info->ctx = ctx;
60 info->func = func;
61
62 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_GAP, GapBlockInTaskProcess, info);
63 if (ret == BT_NO_ERROR) {
64 ret = EventWait(info->event, WAIT_TIME);
65 if (ret == EVENT_WAIT_TIMEOUT_ERR) {
66 ret = BT_TIMEOUT;
67 } else if (ret == EVENT_WAIT_OTHER_ERR) {
68 ret = BT_OS_ERROR;
69 }
70 }
71
72 EventDelete(info->event);
73 MEM_MALLOC.free(info);
74 return ret;
75 }
76
GapRunTaskUnBlockProcess(void (* const func)(void *),void * ctx,void (* const free)(void *))77 int GapRunTaskUnBlockProcess(void (*const func)(void *), void *ctx, void (*const free)(void *))
78 {
79 GapRunTaskUnBlockInfo *info = MEM_MALLOC.alloc(sizeof(GapRunTaskUnBlockInfo));
80 if (info == NULL) {
81 return BT_NO_MEMORY;
82 }
83
84 info->ctx = ctx;
85 info->func = func;
86 info->free = free;
87
88 int ret = BTM_RunTaskInProcessingQueue(PROCESSING_QUEUE_ID_GAP, GapUnBlockInTaskProcess, info);
89
90 return ret;
91 }
92