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 "p2plink_loop.h"
17
18 #include "message_handler.h"
19 #include "softbus_adapter_mem.h"
20 #include "softbus_def.h"
21 #include "softbus_errcode.h"
22 #include "softbus_log.h"
23
24 #define P2P_LOOP_NAME "p2ploop"
25 SoftBusLooper *g_p2pLooper = 0;
26 static SoftBusHandler g_p2pcHandler = {
27 .name ="g_p2pHandler"
28 };
29
30 typedef struct {
31 P2pLoopProcessFunc callback;
32 void *cbPara;
33 int32_t msgType;
34 } P2pCallbackInfo;
35
P2pLoopMsgHandler(SoftBusMessage * msg)36 NO_SANITIZE("cfi") static void P2pLoopMsgHandler(SoftBusMessage *msg)
37 {
38 if (msg == 0) {
39 return;
40 }
41
42 P2pCallbackInfo *info = (P2pCallbackInfo *)msg->obj;
43 if (info == NULL) {
44 CLOGE("P2pLoopMsgHandler callback recv null info");
45 return;
46 }
47 if (info->callback == NULL) {
48 CLOGE("P2pLoopMsgHandler callback function is null");
49 return;
50 }
51 info->callback(msg->what, info->cbPara);
52 }
53
54
P2pLoopInit()55 NO_SANITIZE("cfi") int32_t P2pLoopInit()
56 {
57 g_p2pcHandler.looper = CreateNewLooper(P2P_LOOP_NAME);
58 if (g_p2pcHandler.looper == 0) {
59 return SOFTBUS_ERR;
60 }
61 g_p2pcHandler.HandleMessage = P2pLoopMsgHandler;
62 return SOFTBUS_OK;
63 }
64
P2pFreeLoopMsg(SoftBusMessage * msg)65 NO_SANITIZE("cfi") static void P2pFreeLoopMsg(SoftBusMessage *msg)
66 {
67 if (msg != NULL) {
68 if (msg->obj != NULL) {
69 SoftBusFree(msg->obj);
70 }
71 SoftBusFree((void *)msg);
72 }
73 }
74
P2pCreateLoopMsg(int32_t what,char * data)75 static SoftBusMessage *P2pCreateLoopMsg(int32_t what, char *data)
76 {
77 SoftBusMessage *msg = NULL;
78 msg = SoftBusCalloc(sizeof(SoftBusMessage));
79 if (msg == NULL) {
80 return NULL;
81 }
82 msg->what = what;
83 msg->handler = &g_p2pcHandler;
84 msg->FreeMessage = P2pFreeLoopMsg;
85 msg->obj = (void *)data;
86 return msg;
87 }
88
P2pLoopProc(P2pLoopProcessFunc callback,void * para,P2pLoopMsg msgType)89 NO_SANITIZE("cfi") int32_t P2pLoopProc(P2pLoopProcessFunc callback, void *para, P2pLoopMsg msgType)
90 {
91 P2pCallbackInfo *cbinfo = SoftBusCalloc(sizeof(P2pCallbackInfo));
92 if (cbinfo == NULL) {
93 return SOFTBUS_ERR;
94 }
95 cbinfo->callback = callback;
96 cbinfo->cbPara = para;
97 SoftBusMessage *msg = P2pCreateLoopMsg(msgType, (char *)cbinfo);
98 if (msg == NULL) {
99 SoftBusFree(cbinfo);
100 return SOFTBUS_ERR;
101 }
102 g_p2pcHandler.looper->PostMessage(g_p2pcHandler.looper, msg);
103 return SOFTBUS_OK;
104 }
105
P2pLoopProcDelay(P2pLoopProcessFunc callback,void * para,uint64_t delayMillis,P2pLoopMsg msgType)106 NO_SANITIZE("cfi") int32_t P2pLoopProcDelay(P2pLoopProcessFunc callback, void *para, uint64_t delayMillis,
107 P2pLoopMsg msgType)
108 {
109 P2pCallbackInfo *cbinfo = SoftBusCalloc(sizeof(P2pCallbackInfo));
110 if (cbinfo == NULL) {
111 return SOFTBUS_ERR;
112 }
113 cbinfo->callback = callback;
114 cbinfo->cbPara = para;
115 SoftBusMessage *msg = P2pCreateLoopMsg(msgType, (char *)cbinfo);
116 if (msg == 0) {
117 SoftBusFree(cbinfo);
118 return SOFTBUS_ERR;
119 }
120 g_p2pcHandler.looper->PostMessageDelay(g_p2pcHandler.looper, msg, delayMillis);
121 return SOFTBUS_OK;
122 }
123
P2pRemoveMessageFunc(const SoftBusMessage * msg,void * para)124 NO_SANITIZE("cfi") static int32_t P2pRemoveMessageFunc(const SoftBusMessage *msg, void *para)
125 {
126 if (para == NULL) {
127 return SOFTBUS_ERR;
128 }
129
130 if (msg == NULL) {
131 return SOFTBUS_ERR;
132 }
133 P2pCallbackInfo *info = (P2pCallbackInfo *)para;
134 P2pCallbackInfo *delInfo = (P2pCallbackInfo *)msg->obj;
135 if (delInfo == NULL) {
136 return SOFTBUS_ERR;
137 }
138 if (msg->what == info->msgType) {
139 if (info->callback != 0) {
140 info->callback(info->msgType, delInfo->cbPara);
141 }
142 return SOFTBUS_OK;
143 }
144 return SOFTBUS_ERR;
145 }
146
P2pLoopProcDelayDel(P2pLoopProcessFunc callback,P2pLoopMsg msgType)147 NO_SANITIZE("cfi") int32_t P2pLoopProcDelayDel(P2pLoopProcessFunc callback, P2pLoopMsg msgType)
148 {
149 P2pCallbackInfo cbinfo = {0};
150
151 cbinfo.callback = callback;
152 cbinfo.cbPara = NULL;
153 cbinfo.msgType = (int32_t)msgType;
154
155 g_p2pcHandler.looper->RemoveMessageCustom(g_p2pcHandler.looper, &g_p2pcHandler,
156 P2pRemoveMessageFunc, (void*)&cbinfo);
157 return SOFTBUS_OK;
158 }