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 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 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "P2pLoopMsgHandler callback recv null info");
45 return;
46 }
47 if (info->callback == NULL) {
48 SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "P2pLoopMsgHandler callback function is null");
49 return;
50 }
51 info->callback(msg->what, info->cbPara);
52 }
53
54
P2pLoopInit()55 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 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 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 int32_t P2pLoopProcDelay(P2pLoopProcessFunc callback, void *para, uint64_t delayMillis, P2pLoopMsg msgType)
107 {
108 P2pCallbackInfo *cbinfo = SoftBusCalloc(sizeof(P2pCallbackInfo));
109 if (cbinfo == NULL) {
110 return SOFTBUS_ERR;
111 }
112 cbinfo->callback = callback;
113 cbinfo->cbPara = para;
114 SoftBusMessage *msg = P2pCreateLoopMsg(msgType, (char *)cbinfo);
115 if (msg == 0) {
116 SoftBusFree(cbinfo);
117 return SOFTBUS_ERR;
118 }
119 g_p2pcHandler.looper->PostMessageDelay(g_p2pcHandler.looper, msg, delayMillis);
120 return SOFTBUS_OK;
121 }
122
P2pRemoveMessageFunc(const SoftBusMessage * msg,void * para)123 static int32_t P2pRemoveMessageFunc(const SoftBusMessage *msg, void *para)
124 {
125 if (para == NULL) {
126 return SOFTBUS_ERR;
127 }
128
129 if (msg == NULL) {
130 return SOFTBUS_ERR;
131 }
132 P2pCallbackInfo *info = (P2pCallbackInfo *)para;
133 P2pCallbackInfo *delInfo = (P2pCallbackInfo *)msg->obj;
134 if (delInfo == NULL) {
135 return SOFTBUS_ERR;
136 }
137 if (msg->what == info->msgType) {
138 if (info->callback != 0) {
139 info->callback(info->msgType, delInfo->cbPara);
140 }
141 return SOFTBUS_OK;
142 }
143 return SOFTBUS_ERR;
144 }
145
P2pLoopProcDelayDel(P2pLoopProcessFunc callback,P2pLoopMsg msgType)146 int32_t P2pLoopProcDelayDel(P2pLoopProcessFunc callback, P2pLoopMsg msgType)
147 {
148 P2pCallbackInfo cbinfo = {0};
149
150 cbinfo.callback = callback;
151 cbinfo.cbPara = NULL;
152 cbinfo.msgType = (int32_t)msgType;
153
154 g_p2pcHandler.looper->RemoveMessageCustom(g_p2pcHandler.looper, &g_p2pcHandler,
155 P2pRemoveMessageFunc, (void*)&cbinfo);
156 return SOFTBUS_OK;
157 }