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 #include "avctp_br_l2cap.h"
16 #include "avctp_br.h"
17 #include "avctp_ctrl.h"
18 #include "avctp_dev.h"
19 #include "avctp_gap.h"
20 #include "avctp_st.h"
21 #include "log.h"
22 #include "securec.h"
23 
24 /*************************************************************************************************
25  * Functions Declare
26  *************************************************************************************************/
27 
28 /*****************************************************************************
29  * Globle Data  Define
30  ****************************************************************************/
31 /* L2CAP callback function structure */
32 const L2capService G_AVCT_BR_L2C_SVR = {
33     AvctBrRecvConnectionReqCallback,
34     AvctBrRecvConnectionRspCallback,
35     AvctBrRecvConfigReqCallback,
36     AvctBrRecvConfigRspCallback,
37     AvctBrRecvDisconnectionReqCallback,
38     AvctBrRecvDisconnectionRspCallback,
39     AvctBrDisconnectAbnormalCBack,
40     AvctBrRecvDataCBack,
41     AvctBrRemoteBusyCBack
42     };
43 
44 /*****************************************************************************
45  * Function
46  ****************************************************************************/
47 /*
48  * Function     AvctBrRecvConnectionReqCallback
49  * Description  This function is called back when reveive connect req from peer.
50  * Param[in]    aclHandle  ACL handle.
51  * Param[in]    lcid   The link channel id.
52  * Param[in]    id
53  * Param[in]    psm
54  * Return       void
55  */
56 
AvctBrRecvConnectionReqCallback(uint16_t lcid,uint8_t id,const L2capConnectionInfo * info,uint16_t lpsm,void * ctx)57 void AvctBrRecvConnectionReqCallback(
58     uint16_t lcid, uint8_t id, const L2capConnectionInfo *info, uint16_t lpsm, void *ctx)
59 {
60     LOG_INFO("[AVCT] %{public}s:lcid(0x%x),lpsm(0x%x)", __func__, lcid, lpsm);
61     AvctBrRecvConnectionReqCallbackTskParam *param = malloc(sizeof(AvctBrRecvConnectionReqCallbackTskParam));
62     if (param == NULL) {
63         LOG_ERROR("[AVCT] %{public}s: memory malloc failed", __func__);
64         return;
65     }
66     (void)memset_s(
67         param, sizeof(AvctBrRecvConnectionReqCallbackTskParam), 0, sizeof(AvctBrRecvConnectionReqCallbackTskParam));
68 
69     param->lcid = lcid;
70     param->id = id;
71     (void)memcpy_s(&param->info, sizeof(L2capConnectionInfo), info, sizeof(L2capConnectionInfo));
72     param->lpsm = lpsm;
73     param->ctx = ctx;
74     if (AvctAsyncProcess(AvctBrRecvConnectionReqCallbackTsk, param)) {
75         free(param);
76     }
77     return;
78 }
79 
AvctBrRecvConnectionReqCallbackTsk(void * context)80 void AvctBrRecvConnectionReqCallbackTsk(void *context)
81 {
82     AvctBrRecvConnectionReqCallbackTskParam *param = (AvctBrRecvConnectionReqCallbackTskParam *)context;
83     AvctBrRecvConnectionReqCBack(param->lcid, param->id, &param->info, param->lpsm, param->ctx);
84     free(param);
85     return;
86 }
87 
AvctBrRecvConnectionReqCBack(uint16_t lcid,uint8_t id,const L2capConnectionInfo * info,uint16_t lpsm,void * ctx)88 void AvctBrRecvConnectionReqCBack(uint16_t lcid, uint8_t id, const L2capConnectionInfo *info, uint16_t lpsm, void *ctx)
89 {
90     LOG_DEBUG("[AVCT] %{public}s:lcid(0x%x),lpsm(0x%x)", __func__, lcid, lpsm);
91     if (lpsm == AVCT_BR_PSM) {
92         AvctRequestSecurityBy(info->handle, info->addr, lcid, id, lpsm);
93     } else {
94         L2CIF_ConnectRsp(lcid, id, L2CAP_PSM_NOT_SUPPORTED, 0, NULL);
95     }
96     return;
97 }
98 
99 /*
100  * Function     AvctBrRecvConnectionReqAct
101  * Description  Receive Connection Request action func
102  * Param[in]    addr    address.
103  * Param[in]    aclHandle   acl handle.
104  * Param[in]    lcid    link channel id
105  * Param[in]    id      id
106  * Param[in]    psm     psm
107  * Return       void
108  */
AvctBrRecvConnectionReqAct(const BtAddr * addr,uint16_t aclHandle,uint16_t lcid,uint8_t id,uint16_t psm)109 void AvctBrRecvConnectionReqAct(const BtAddr *addr, uint16_t aclHandle, uint16_t lcid, uint8_t id, uint16_t psm)
110 {
111     LOG_INFO("[AVCT] %{public}s: lcid(0x%x)", __func__, lcid);
112     AvctCbCh *cbBr = NULL;
113     AvctCbDev *cbDev = NULL;
114     uint16_t result = L2CAP_CONNECTION_SUCCESSFUL;
115     if (psm != AVCT_BR_PSM) {
116         LOG_ERROR("[AVCT]The Psm is not Avct");
117         result = L2CAP_PSM_NOT_SUPPORTED;
118     } else {
119         /* Check the devcie connection is exist */
120         cbDev = AvctGetCbDevByAddress(addr);
121         if (cbDev == NULL) {
122             /* No resource,can't connect */
123             result = L2CAP_NO_RESOURCES_AVAILABLE;
124         } else if (!(cbDev->chLink & AVCT_ALLOC_CTRL)) {
125             /* The control channel is not connect,can't connect */
126             result = L2CAP_NO_RESOURCES_AVAILABLE;
127         } else if (cbDev->chLink & AVCT_ALLOC_BR) {
128             /* The br channel is exist */
129             result = L2CAP_SOURCE_CID_ALREADY_ALLOCATED;
130         } else {
131             /* Alloc br channel resource */
132             cbBr = AvctCbChAlloc();
133             if (cbBr == NULL) {
134                 result = L2CAP_NO_RESOURCES_AVAILABLE;
135             }
136         }
137     }
138     /* send L2CAP connection response  */
139     L2CIF_ConnectRsp(lcid, id, result, 0, NULL);
140 
141     /* if connect success,send config requeset */
142     if (result == L2CAP_CONNECTION_SUCCESSFUL) {
143         cbBr->chId = lcid;
144         cbBr->chState = AVCT_CH_CFG;
145         cbDev->cbBr = cbBr;
146         cbDev->chLink |= AVCT_ALLOC_BR;
147         cbDev->cbBr->ia = AVCT_ACPT;
148         L2capConfigInfo cfg = {0};
149         cfg.mtu = g_avctMng.rxMtuBr;
150         cfg.flushTimeout = 0xFFFF;
151         cfg.fcs = 0x01;
152         cfg.rfc.mode = L2CAP_ENHANCED_RETRANSMISSION_MODE;
153         if (L2CIF_ConfigReq(lcid, &cfg, NULL)) {
154             LOG_ERROR("[AVCT] %{public}s:L2CIF_ConfigReq failed.", __func__);
155         }
156     }
157     return;
158 }
159 
160 /*
161  * Function     AvctBrRecvConnectionRspCBack
162  * Description  This function is called back when reveived connection response.
163  *              from peer.
164  * Param[in]    lcid The link channel id.
165  * Param[in]    result  the connection request result
166  * Param[in]    status
167  * Return       void
168  */
AvctBrRecvConnectionRspCallback(uint16_t lcid,const L2capConnectionInfo * info,uint16_t result,uint16_t status,void * ctx)169 void AvctBrRecvConnectionRspCallback(
170     uint16_t lcid, const L2capConnectionInfo *info, uint16_t result, uint16_t status, void *ctx)
171 {
172     LOG_INFO("[AVCT] %{public}s: lcid(0x%x), result(%{public}d)", __func__, lcid, result);
173     AvctBrRecvConnectionRspCallbackTskParam *param = malloc(sizeof(AvctBrRecvConnectionRspCallbackTskParam));
174     if (param == NULL) {
175         LOG_ERROR("[AVCT] %{public}s: memory malloc failed", __func__);
176         return;
177     }
178     (void)memset_s(
179         param, sizeof(AvctBrRecvConnectionRspCallbackTskParam), 0, sizeof(AvctBrRecvConnectionRspCallbackTskParam));
180     param->lcid = lcid;
181     (void)memcpy_s(&param->info, sizeof(L2capConnectionInfo), info, sizeof(L2capConnectionInfo));
182     param->result = result;
183     param->status = status;
184     param->ctx = ctx;
185     if (AvctAsyncProcess(AvctBrRecvConnectionRspCallbackTsk, param)) {
186         free(param);
187     }
188     return;
189 }
190 
AvctBrRecvConnectionRspCallbackTsk(void * context)191 void AvctBrRecvConnectionRspCallbackTsk(void *context)
192 {
193     AvctBrRecvConnectionRspCallbackTskParam *param = (AvctBrRecvConnectionRspCallbackTskParam *)context;
194     AvctBrRecvConnectionRspCBack(param->lcid, &param->info, param->result, param->status, param->ctx);
195     free(param);
196     return;
197 }
198 
AvctBrRecvConnectionRspCBack(uint16_t lcid,const L2capConnectionInfo * info,uint16_t result,uint16_t status,void * ctx)199 void AvctBrRecvConnectionRspCBack(
200     uint16_t lcid, const L2capConnectionInfo *info, uint16_t result, uint16_t status, void *ctx)
201 {
202     LOG_DEBUG("[AVCT] %{public}s: lcid(0x%x), result(%{public}d)", __func__, lcid, result);
203     AvctCbDev *cbDev = AvctGetCbDevByChId(lcid);
204     if (cbDev == NULL) {
205         LOG_ERROR("[AVCT] %{public}s:AvctGetCbDevByChId(0x%x) failed.", __func__, lcid);
206         return;
207     }
208     if (cbDev->cbBr->chState == AVCT_CH_CONN) {
209         if (result == L2CAP_CONNECTION_PENDING) {
210             LOG_INFO("[AVCT] %{public}s: Connect req result is pending, do nothing", __func__);
211             return;
212         }
213         if (result == L2CAP_CONNECTION_SUCCESSFUL) {
214             /* connect success ,send config request */
215             cbDev->cbBr->chState = AVCT_CH_CFG;
216             L2capConfigInfo cfg = {0};
217             (void)memset_s(&cfg, sizeof(L2capConfigInfo), 0, sizeof(L2capConfigInfo));
218             cfg.mtu = g_avctMng.rxMtuBr;
219             cfg.flushTimeout = 0xFFFF;
220             cfg.fcs = 0x01;
221             cfg.rfc.mode = L2CAP_ENHANCED_RETRANSMISSION_MODE;
222             if (L2CIF_ConfigReq(lcid, &cfg, NULL)) {
223                 LOG_ERROR("[AVCT] %{public}s:L2CIF_ConfigReq failed.", __func__);
224             }
225         } else {
226             /* connect failed,send event to up app */
227             AvctEvtData evtData;
228             evtData.result = result;
229             AvctCbBrEvt(cbDev, AVCT_REV_DISCONN_EVT, &evtData);
230         }
231     }
232     return;
233 }
234 
235 /*
236  * Function     AvctBrRecvConfigReqCBack
237  * Description  This function is called back when reveived config request
238  *              from peer.
239  * Param[in]    *addr  The address of the peer device.
240  * Param[in]    lcid The link channel id.
241  * Param[in]    id
242  * Param[in]    psm
243  * Return       void
244  */
AvctBrRecvConfigReqCallback(uint16_t lcid,uint8_t id,const L2capConfigInfo * cfg,void * ctx)245 void AvctBrRecvConfigReqCallback(uint16_t lcid, uint8_t id, const L2capConfigInfo *cfg, void *ctx)
246 {
247     LOG_INFO("[AVCT] %{public}s: lcid(0x%x), mtu(%hu)", __func__, lcid, cfg->mtu);
248     AvctBrRecvConfigReqCallbackTskParam *param = malloc(sizeof(AvctBrRecvConfigReqCallbackTskParam));
249     if (param == NULL) {
250         LOG_ERROR("[AVCT] %{public}s: memory malloc failed", __func__);
251         return;
252     }
253     (void)memset_s(param, sizeof(AvctBrRecvConfigReqCallbackTskParam), 0, sizeof(AvctBrRecvConfigReqCallbackTskParam));
254     param->lcid = lcid;
255     param->id = id;
256     (void)memcpy_s(&param->cfg, sizeof(L2capConfigInfo), cfg, sizeof(L2capConfigInfo));
257     param->ctx = ctx;
258     if (AvctAsyncProcess(AvctBrRecvConfigReqCallbackTsk, param)) {
259         free(param);
260     }
261     return;
262 }
263 
AvctBrRecvConfigReqCallbackTsk(void * context)264 void AvctBrRecvConfigReqCallbackTsk(void *context)
265 {
266     AvctBrRecvConfigReqCallbackTskParam *param = (AvctBrRecvConfigReqCallbackTskParam *)context;
267     AvctBrRecvConfigReqCBack(param->lcid, param->id, &param->cfg, param->ctx);
268     free(param);
269     return;
270 }
271 
AvctBrRecvConfigReqCBack(uint16_t lcid,uint8_t id,const L2capConfigInfo * cfg,void * ctx)272 void AvctBrRecvConfigReqCBack(uint16_t lcid, uint8_t id, const L2capConfigInfo *cfg, void *ctx)
273 {
274     LOG_DEBUG("[AVCT] %{public}s: lcid(0x%x), mtu(%hu)", __func__, lcid, cfg->mtu);
275     uint16_t result = L2CAP_SUCCESS;
276     AvctCbDev *cbDev = AvctGetCbDevByChId(lcid);
277     if (cbDev != NULL) {
278         /* send L2CAP configure response */
279         if (L2CIF_ConfigRsp(lcid, id, cfg, result, NULL)) {
280             LOG_ERROR("[AVCT] %{public}s:L2CIF_ConfigReq failed.", __func__);
281         }
282         cbDev->cbBr->peerMtu = cfg->mtu;
283         cbDev->cbBr->chCfgSt |= AVCT_L2C_CFG_REQ;
284         /* There are two step of config process */
285         if (cbDev->cbBr->chCfgSt & AVCT_L2C_CFG_RSP) {
286             /* config req/rsp reveiced */
287             if (cbDev->cbBr->chResult == L2CAP_SUCCESS) {
288                 cbDev->cbBr->chState = AVCT_CH_OPENED;
289                 AvctEvtData evtData;
290                 evtData.result = AVCT_SUCCESS;
291                 AvctCbBrEvt(cbDev, AVCT_REV_CONN_EVT, &evtData);
292             }
293         }
294     } else {
295         result = L2CAP_REJECTED;
296         if (L2CIF_ConfigRsp(lcid, id, cfg, result, NULL)) {
297             LOG_ERROR("[AVCT] %{public}s:L2CIF_ConfigReq failed.", __func__);
298         }
299     }
300     return;
301 }
302 
303 /*
304  * Function     AvctBrRecvConfigRspCBack
305  * Description  This function is called back when reveived config response
306  *              from peer.
307  * Param[in]    *addr   The address of the peer device.
308  * Param[in]    lcid    The link channel id.
309  * Param[in]    cfg     config info
310  * Param[in]    result  the result of config request.
311  * Return       void
312  */
AvctBrRecvConfigRspCallback(uint16_t lcid,const L2capConfigInfo * cfg,uint16_t result,void * ctx)313 void AvctBrRecvConfigRspCallback(uint16_t lcid, const L2capConfigInfo *cfg, uint16_t result, void *ctx)
314 {
315     LOG_INFO("[AVCT] %{public}s: lcid(0x%x),result(%{public}d)", __func__, lcid, result);
316     AvctBrRecvConfigRspCallbackTskParam *param = malloc(sizeof(AvctBrRecvConfigRspCallbackTskParam));
317     if (param == NULL) {
318         LOG_ERROR("[AVCT] %{public}s: memory malloc failed", __func__);
319         return;
320     }
321     (void)memset_s(param, sizeof(AvctBrRecvConfigRspCallbackTskParam), 0, sizeof(AvctBrRecvConfigRspCallbackTskParam));
322     param->lcid = lcid;
323     (void)memcpy_s(&param->cfg, sizeof(L2capConfigInfo), cfg, sizeof(L2capConfigInfo));
324     param->result = result;
325     param->ctx = ctx;
326     if (AvctAsyncProcess(AvctBrRecvConfigRspCallbackTsk, param)) {
327         free(param);
328     }
329     return;
330 }
331 
AvctBrRecvConfigRspCallbackTsk(void * context)332 void AvctBrRecvConfigRspCallbackTsk(void *context)
333 {
334     AvctBrRecvConfigRspCallbackTskParam *param = (AvctBrRecvConfigRspCallbackTskParam *)context;
335     AvctBrRecvConfigRspCBack(param->lcid, &param->cfg, param->result, param->ctx);
336     free(param);
337     return;
338 }
339 
AvctBrRecvConfigRspCBack(uint16_t lcid,const L2capConfigInfo * cfg,uint16_t result,void * ctx)340 void AvctBrRecvConfigRspCBack(uint16_t lcid, const L2capConfigInfo *cfg, uint16_t result, void *ctx)
341 {
342     LOG_DEBUG("[AVCT] %{public}s: lcid(0x%x),result(%{public}d)", __func__, lcid, result);
343     AvctCbDev *cbDev = AvctGetCbDevByChId(lcid);
344     if (cbDev == NULL) {
345         LOG_ERROR("[AVCT] %{public}s:AvctGetCbDevByChId(0x%x) failed.", __func__, lcid);
346         return;
347     }
348     if (cbDev->cbBr->chState == AVCT_CH_CFG) {
349         if (result == L2CAP_PENDING) {
350             LOG_INFO("[AVCT] %{public}s: Config RSP result is pending, do nothing!", __func__);
351             return;
352         }
353         if (result == L2CAP_SUCCESS) {
354             cbDev->cbBr->chCfgSt |= AVCT_L2C_CFG_RSP;
355             if (cbDev->cbBr->chCfgSt & AVCT_L2C_CFG_REQ) {
356                 /* config req/rsp reveiced */
357                 cbDev->cbBr->chState = AVCT_CH_OPENED;
358                 AvctEvtData evtData;
359                 evtData.result = AVCT_SUCCESS;
360                 AvctCbBrEvt(cbDev, AVCT_REV_CONN_EVT, &evtData);
361             }
362         } else {
363             /* result is Failed ,send disconnect req */
364             cbDev->cbBr->chResult = result;
365             L2CIF_DisconnectionReq(lcid, NULL);
366         }
367     }
368     return;
369 }
370 
371 /*
372  * Function     AvctBrRecvDisconnectionReqCBack
373  * Description  This function is called back when reveived disconnection request
374  *              from peer.
375  * Param[in]    lcid    The link channel id.
376  * Param[in]    id
377  * Return       void
378  */
AvctBrRecvDisconnectionReqCallback(uint16_t lcid,uint8_t id,void * ctx)379 void AvctBrRecvDisconnectionReqCallback(uint16_t lcid, uint8_t id, void *ctx)
380 {
381     LOG_INFO("[AVCT] %{public}s: lcid(0x%x)", __func__, lcid);
382     AvctBrRecvDisconnectionReqCallbackTskParam *param = malloc(sizeof(AvctBrRecvDisconnectionReqCallbackTskParam));
383     if (param == NULL) {
384         LOG_ERROR("[AVCT] %{public}s: memory malloc failed", __func__);
385         return;
386     }
387     (void)memset_s(param,
388         sizeof(AvctBrRecvDisconnectionReqCallbackTskParam),
389         0,
390         sizeof(AvctBrRecvDisconnectionReqCallbackTskParam));
391     param->lcid = lcid;
392     param->id = id;
393     param->ctx = ctx;
394     if (AvctAsyncProcess(AvctBrRecvDisconnectionReqCallbackTsk, param)) {
395         free(param);
396     }
397     return;
398 }
399 
AvctBrRecvDisconnectionReqCallbackTsk(void * context)400 void AvctBrRecvDisconnectionReqCallbackTsk(void *context)
401 {
402     AvctBrRecvDisconnectionReqCallbackTskParam *param = (AvctBrRecvDisconnectionReqCallbackTskParam *)context;
403     AvctBrRecvDisconnectionReqCBack(param->lcid, param->id, param->ctx);
404     free(param);
405     return;
406 }
407 
AvctBrRecvDisconnectionReqCBack(uint16_t lcid,uint8_t id,void * ctx)408 void AvctBrRecvDisconnectionReqCBack(uint16_t lcid, uint8_t id, void *ctx)
409 {
410     LOG_DEBUG("[AVCT] %{public}s: lcid(0x%x)", __func__, lcid);
411     /* Send disconnect rsp */
412     L2CIF_DisconnectionRsp(lcid, id, NULL);
413     AvctCbDev *cbDev = AvctGetCbDevByChId(lcid);
414     if (cbDev != NULL) {
415         /* Send Close Event */
416         AvctEvtData evtData;
417         evtData.result = AVCT_SUCCESS;
418         AvctCbBrEvt(cbDev, AVCT_REV_DISCONN_EVT, &evtData);
419     }
420     return;
421 }
422 
423 /*
424  * Function     AvctBrRecvDisconnectionRspCBack
425  * Description  This function is called back when reveived disconnection
426  *              response from peer.
427  * Param[in]    lcid    The link channel id.
428  * Return       void
429  */
AvctBrRecvDisconnectionRspCallback(uint16_t lcid,void * ctx)430 void AvctBrRecvDisconnectionRspCallback(uint16_t lcid, void *ctx)
431 {
432     LOG_INFO("[AVCT] %{public}s:lcid(0x%x)", __func__, lcid);
433     AvctBrRecvDisconnectionRspCallbackTskParam *param = malloc(sizeof(AvctBrRecvDisconnectionRspCallbackTskParam));
434     if (param == NULL) {
435         LOG_ERROR("[AVCT] %{public}s: memory malloc failed", __func__);
436         return;
437     }
438     (void)memset_s(param,
439         sizeof(AvctBrRecvDisconnectionRspCallbackTskParam),
440         0,
441         sizeof(AvctBrRecvDisconnectionRspCallbackTskParam));
442     param->lcid = lcid;
443     param->ctx = ctx;
444     if (AvctAsyncProcess(AvctBrRecvDisconnectionRspCallbackTsk, param)) {
445         free(param);
446     }
447     return;
448 }
449 
AvctBrRecvDisconnectionRspCallbackTsk(void * context)450 void AvctBrRecvDisconnectionRspCallbackTsk(void *context)
451 {
452     AvctBrRecvDisconnectionRspCallbackTskParam *param = (AvctBrRecvDisconnectionRspCallbackTskParam *)context;
453     AvctBrRecvDisconnectionRspCBack(param->lcid, param->ctx);
454     free(param);
455     return;
456 }
457 
AvctBrRecvDisconnectionRspCBack(uint16_t lcid,void * ctx)458 void AvctBrRecvDisconnectionRspCBack(uint16_t lcid, void *ctx)
459 {
460     LOG_DEBUG("[AVCT] %{public}s:lcid(0x%x)", __func__, lcid);
461     AvctCbDev *cbDev = AvctGetCbDevByChId(lcid);
462     if (cbDev != NULL) {
463         /* Send Close Event */
464         AvctEvtData evtData;
465         evtData.result = cbDev->cbBr->chResult;
466         cbDev->cbBr->chResult = 0;
467         AvctCbBrEvt(cbDev, AVCT_REV_DISCONN_EVT, &evtData);
468     }
469     return;
470 }
471 
472 /*
473  * Function     AvctBrDisconnectAbnormalCBack
474  * Description  This function is called back when Disconnected abnormal, such as
475  *              acl disconnected or link loss response from peer.
476  * Param[in]    lcid    The link channel id.
477  * Param[in]    reason  abnormal reason
478  * Return       void
479  */
AvctBrDisconnectAbnormalCallback(uint16_t lcid,uint8_t reason,void * ctx)480 void AvctBrDisconnectAbnormalCallback(uint16_t lcid, uint8_t reason, void *ctx)
481 {
482     LOG_INFO("[AVCT] %{public}s: lcid(0x%x)", __func__, lcid);
483     AvctBrDisconnectAbnormalCallbackTskParam *param = malloc(sizeof(AvctBrDisconnectAbnormalCallbackTskParam));
484     if (param == NULL) {
485         LOG_ERROR("[AVCT] %{public}s: memory malloc failed", __func__);
486         return;
487     }
488     (void)memset_s(
489         param, sizeof(AvctBrDisconnectAbnormalCallbackTskParam), 0, sizeof(AvctBrDisconnectAbnormalCallbackTskParam));
490     param->lcid = lcid;
491     param->reason = reason;
492     param->ctx = ctx;
493     if (AvctAsyncProcess(AvctBrDisconnectAbnormalCallbackTsk, param)) {
494         free(param);
495     }
496     return;
497 }
498 
AvctBrDisconnectAbnormalCallbackTsk(void * context)499 void AvctBrDisconnectAbnormalCallbackTsk(void *context)
500 {
501     AvctBrDisconnectAbnormalCallbackTskParam *param = (AvctBrDisconnectAbnormalCallbackTskParam *)context;
502     AvctBrDisconnectAbnormalCBack(param->lcid, param->reason, param->ctx);
503     free(param);
504     return;
505 }
506 
AvctBrDisconnectAbnormalCBack(uint16_t lcid,uint8_t reason,void * ctx)507 void AvctBrDisconnectAbnormalCBack(uint16_t lcid, uint8_t reason, void *ctx)
508 {
509     LOG_DEBUG("[AVCT] %{public}s: lcid(0x%x)", __func__, lcid);
510     AvctCbDev *cbDev = AvctGetCbDevByChId(lcid);
511     if (cbDev != NULL) {
512         if (reason == L2CAP_STATE_COLLISION) {
513             LOG_WARN("[AVCT] %{public}s:Connect Req failed! reason:L2CAP_STATE_COLLISION ", __func__);
514             /* Connect Req failed, Need connect retry */
515             cbDev->cbBr->chCfgSt = 0;
516             cbDev->cbBr->chState = AVCT_CH_IDLE;
517             cbDev->cbBr->stState = AVCT_IDLE_ST;
518             AvctCbBrEvt(cbDev, AVCT_BIND_EVT, NULL);
519         } else {
520             AvctEvtData evtData;
521             evtData.result = AVCT_FAILED;
522             AvctCbBrEvt(cbDev, AVCT_REV_DISCONN_EVT, &evtData);
523         }
524     }
525     return;
526 }
527 
528 /*
529  * Function     AvctBrRecvDataCBack
530  * Description  This function is called back when receive message from peer.
531  * Param[in]    lcid    The link channel id.
532  * Param[in]    *packet message data
533  * Return       void
534  */
AvctBrRecvDataCallback(uint16_t lcid,const Packet * pkt,void * ctx)535 void AvctBrRecvDataCallback(uint16_t lcid, const Packet *pkt, void *ctx)
536 {
537     LOG_INFO("[AVCT] %{public}s:lcid(0x%x),PacketSize(%u)", __func__, lcid, PacketSize(pkt));
538     AvctBrRecvDataCallbackTskParam *param = malloc(sizeof(AvctBrRecvDataCallbackTskParam));
539     if (param == NULL) {
540         LOG_ERROR("[AVCT] %{public}s: memory malloc failed", __func__);
541         return;
542     }
543     (void)memset_s(param, sizeof(AvctBrRecvDataCallbackTskParam), 0, sizeof(AvctBrRecvDataCallbackTskParam));
544     param->lcid = lcid;
545     param->pkt = PacketRefMalloc(pkt);
546     param->ctx = ctx;
547     if (AvctAsyncProcess(AvctBrRecvDataCallbackTsk, param)) {
548         PacketFree(param->pkt);
549         free(param);
550     }
551     return;
552 }
553 
AvctBrRecvDataCallbackTsk(void * context)554 void AvctBrRecvDataCallbackTsk(void *context)
555 {
556     AvctBrRecvDataCallbackTskParam *param = (AvctBrRecvDataCallbackTskParam *)context;
557     AvctBrRecvDataCBack(param->lcid, param->pkt, param->ctx);
558     PacketFree(param->pkt);
559     free(param);
560     return;
561 }
562 
AvctBrRecvDataCBack(uint16_t lcid,Packet * pkt,void * ctx)563 void AvctBrRecvDataCBack(uint16_t lcid, Packet *pkt, void *ctx)
564 {
565     LOG_DEBUG("[AVCT] %{public}s:lcid(0x%x),PacketSize(%u)", __func__, lcid, PacketSize(pkt));
566     AvctCbDev *cbDev = AvctGetCbDevByChId(lcid);
567     if (cbDev != NULL) {
568         AvctEvtData evtData;
569         evtData.buf = pkt;
570         AvctCbBrEvt(cbDev, AVCT_REV_MSG_EVT, &evtData);
571     } else {
572         /* Free pkt memory */
573         LOG_WARN("[AVCT] %{public}s:Can't find device by lcid(0x%x)", __func__, lcid);
574     }
575     return;
576 }
577 
578 /*
579  * Function     AvctBrRemoteBusyCBack
580  * Description  This function is called back when remote peer is busy in
581  *              Enhanced mode.
582  * Param[in]    lcid    The link channel id.
583  * Param[in]    isBusy  busy/unbusy
584  * Return       void
585  */
AvctBrRemoteBusyCallback(uint16_t lcid,uint8_t isBusy,void * ctx)586 void AvctBrRemoteBusyCallback(uint16_t lcid, uint8_t isBusy, void *ctx)
587 {
588     AvctBrRemoteBusyCallbackTskParam *param = malloc(sizeof(AvctBrRemoteBusyCallbackTskParam));
589     if (param == NULL) {
590         LOG_ERROR("[AVCT] %{public}s: memory malloc failed", __func__);
591         return;
592     }
593     (void)memset_s(param, sizeof(AvctBrRemoteBusyCallbackTskParam), 0, sizeof(AvctBrRemoteBusyCallbackTskParam));
594     param->lcid = lcid;
595     param->isBusy = isBusy;
596     param->ctx = ctx;
597     if (AvctAsyncProcess(AvctBrRemoteBusyCallbackTsk, param)) {
598         free(param);
599     }
600     return;
601 }
602 
AvctBrRemoteBusyCallbackTsk(void * context)603 void AvctBrRemoteBusyCallbackTsk(void *context)
604 {
605     AvctBrRemoteBusyCallbackTskParam *param = (AvctBrRemoteBusyCallbackTskParam *)context;
606     AvctBrRemoteBusyCBack(param->lcid, param->isBusy, param->ctx);
607     free(param);
608     return;
609 }
610 
AvctBrRemoteBusyCBack(uint16_t lcid,uint8_t isBusy,void * ctx)611 void AvctBrRemoteBusyCBack(uint16_t lcid, uint8_t isBusy, void *ctx)
612 {
613     LOG_INFO("[AVCT] %{public}s:lcid(0x%x),isBusy(%hhu)", __func__, lcid, isBusy);
614     AvctCbDev *cbDev = AvctGetCbDevByChId(lcid);
615     if (cbDev != NULL) {
616         AvctEvtData evtData;
617         if (isBusy) {
618             cbDev->cbBr->chState = AVCT_CH_BUSY;
619             evtData.result = AVCT_BUSY_ST;
620             AvctCbBrEvt(cbDev, AVCT_REV_BUSY_EVT, &evtData);
621         } else {
622             if (cbDev->cbBr->chState == AVCT_CH_BUSY) {
623                 cbDev->cbBr->chState = AVCT_CH_OPENED;
624                 evtData.result = AVCT_OPENED_ST;
625                 AvctCbBrEvt(cbDev, AVCT_REV_BUSY_EVT, &evtData);
626             }
627         }
628     }
629     return;
630 }
631 
632 /*
633  * Function     AvctBrwL2CIFConnectReqCallback
634  * Description  L2cap connect request check callback func.
635  * Param[in]    result Check result.
636  * Param[in]    lcid   lcid.
637  * Param[out]   addr   peer address
638  * Return       void
639  */
AvctBrwL2CIFConnectReqCallback(const BtAddr * addr,uint16_t lcid,int result,void * context)640 void AvctBrwL2CIFConnectReqCallback(const BtAddr *addr, uint16_t lcid, int result, void *context)
641 {
642     LOG_INFO("[AVCT] %{public}s: lcid(0x%x), result(%{public}d)", __func__, lcid, result);
643     AvctBrwL2CIFConnectReqCallbackTskParam *param = malloc(sizeof(AvctBrwL2CIFConnectReqCallbackTskParam));
644     if (param == NULL) {
645         LOG_ERROR("[AVCT] %{public}s: memory malloc failed", __func__);
646         return;
647     }
648     (void)memset_s(
649         param, sizeof(AvctBrwL2CIFConnectReqCallbackTskParam), 0, sizeof(AvctBrwL2CIFConnectReqCallbackTskParam));
650     (void)memcpy_s(&param->addr, sizeof(BtAddr), addr, sizeof(BtAddr));
651     param->lcid = lcid;
652     param->result = result;
653     param->context = context;
654     if (AvctAsyncProcess(AvctBrwL2CIFConnectReqCallbackTsk, param)) {
655         free(param);
656     }
657     return;
658 }
659 
AvctBrwL2CIFConnectReqCallbackTsk(void * context)660 void AvctBrwL2CIFConnectReqCallbackTsk(void *context)
661 {
662     AvctBrwL2CIFConnectReqCallbackTskParam *param = (AvctBrwL2CIFConnectReqCallbackTskParam *)context;
663     AvctBrwL2CIFConnectResult(&param->addr, param->lcid, param->result, param->context);
664     free(param);
665     return;
666 }
667 
AvctBrwL2CIFConnectResult(const BtAddr * addr,uint16_t lcid,int result,void * context)668 void AvctBrwL2CIFConnectResult(const BtAddr *addr, uint16_t lcid, int result, void *context)
669 {
670     LOG_DEBUG("[AVCT] %{public}s: lcid(0x%x), result(%{public}d)", __func__, lcid, result);
671     AvctCbDev *cbDev = AvctGetCbDevByAddress(addr);
672     if ((cbDev != NULL) && (cbDev->cbBr != NULL)) {
673         cbDev->cbBr->chId = lcid;
674         if (lcid == 0) {
675             /* connect req failed, send close event */
676             AvctEvtData evtData;
677             evtData.result = AVCT_FAILED;
678             AvctCbBrEvt(cbDev, AVCT_REV_DISCONN_EVT, &evtData);
679         }
680     } else {
681         LOG_WARN("[AVCT]%{public}s:Can't find control block by bdAddr((%02x:%02x:%02x:%02x:%02x:%02x)",
682             __func__,
683             BT_ADDR_FMT_DSC(addr->addr));
684     }
685     return;
686 }