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_conn.h"
16 #include "avctp_st.h"
17 #include "log.h"
18 #include "securec.h"
19 
20 /*****************************************************************************
21  * Globle Data  Define
22  ****************************************************************************/
23 /*****************************************************************************
24  * Function
25  ****************************************************************************/
26 /*
27  * Function     AvctCbConnAlloc
28  * Description  This function is called to alloc connection resource.
29  * Param[in]   *connParam  point to the info of the connection request.
30  *              Such as the Role、Profile ID、Callback func point.
31  * Return      connection object point; NULL if there is no connection resource.
32  */
AvctCbConnAlloc(const AvctConnectParam * connParm)33 AvctCbConn *AvctCbConnAlloc(const AvctConnectParam *connParm)
34 {
35     LOG_DEBUG("[AVCT] %{public}s:", __func__);
36     AvctCbConn *cbConn = NULL;
37     for (uint8_t i = 0; i < AVCT_MAX_CONNECTS; i++) {
38         if (g_avctMng.cbConn[i].status == 0) {
39             g_avctMng.cbConn[i].status = AVCT_CONN_ALLOC;
40             g_avctMng.cbConn[i].connId = i;
41             g_avctMng.cbConn[i].cbDev = NULL;
42             (void)memcpy_s(
43                 &g_avctMng.cbConn[i].connParam, sizeof(AvctConnectParam), connParm, sizeof(AvctConnectParam));
44             cbConn = &g_avctMng.cbConn[i];
45             LOG_DEBUG("[AVCT] %{public}s:connId is %hhu\n", __func__, i);
46             break;
47         }
48     }
49     return cbConn;
50 }
51 
52 /*
53  * Function     AvctCbConnDealloc
54  * Description  This function is called to dealloc connection resource.
55  * Param[in]    *cbConn  point to the info of the connection control block.
56  * Return       void
57  */
AvctCbConnDealloc(AvctCbConn * cbConn)58 void AvctCbConnDealloc(AvctCbConn *cbConn)
59 {
60     LOG_DEBUG("[AVCT] %{public}s:", __func__);
61     if (cbConn->connParam.role == AVCT_ACPT) {
62         cbConn->cbDev = NULL;
63         cbConn->status = AVCT_CONN_ALLOC;
64     } else {
65         (void)memset_s(cbConn, sizeof(AvctCbConn), 0, sizeof(AvctCbConn));
66     }
67     return;
68 }
69 /*
70  * Function     AvctCbConnEvtCallback
71  * Description  This function is called to send evet callback to app.
72  * Param[in]    *cbConn  point to the info of the connection control block.
73  * Param[in]    event
74  * Param[in]    result
75  * Param[in]    peerAddr  peer device address
76  * Return       void
77  */
78 NO_SANITIZE("cfi")
AvctCbConnEvtCallback(const AvctCbConn * cbConn,uint8_t event,uint16_t result,const BtAddr * peerAddr)79 void AvctCbConnEvtCallback(const AvctCbConn *cbConn, uint8_t event, uint16_t result, const BtAddr *peerAddr)
80 {
81     LOG_DEBUG("[AVCT]---%{public}s: connId(%hhu) EventId(%hhu), result(%hu)", __func__, cbConn->connId, event, result);
82     AvctChannelEventCallback cback = cbConn->connParam.chEvtCallback;
83     if (cback != NULL) {
84         (*cback)(cbConn->connId, event, result, peerAddr, cbConn->connParam.context);
85     }
86     return;
87 }
88 /*
89  * Function     AvctGetCbConnByConnId
90  * Description  This function is called to get the connection by connId.
91  * Param[in]   connId  connection ID.
92  * Return      AvctCbConn  the point of connection control block
93  */
AvctGetCbConnByConnId(uint8_t connId)94 AvctCbConn *AvctGetCbConnByConnId(uint8_t connId)
95 {
96     LOG_DEBUG("[AVCT] %{public}s:", __func__);
97     AvctCbConn *cbConn = NULL;
98     if ((connId < AVCT_MAX_CONNECTS) && (g_avctMng.cbConn[connId].status != 0)) {
99         cbConn = &g_avctMng.cbConn[connId];
100         LOG_DEBUG("[AVCT] %{public}s:conn status(%hhu)", __func__, g_avctMng.cbConn[connId].status);
101     }
102     return cbConn;
103 }
104 
105 /*
106  * Function     AvctGetCbConnByPid
107  * Description  This function is called to get the connection by cbCtrl and pid.
108  * Param[in]   *cbDev  point to the device block.
109  * Param[in]   pid  profile id.
110  * Return      void
111  */
AvctGetCbConnByPid(const AvctCbDev * cbDev,uint16_t pid)112 AvctCbConn *AvctGetCbConnByPid(const AvctCbDev *cbDev, uint16_t pid)
113 {
114     LOG_DEBUG("[AVCT] %{public}s:pid (0x%x)", __func__, pid);
115     AvctCbConn *cbConn = NULL;
116     for (uint8_t i = 0; i < AVCT_MAX_CONNECTS; i++) {
117         if ((g_avctMng.cbConn[i].status == AVCT_CONN_BIND) && (g_avctMng.cbConn[i].cbDev == cbDev) &&
118             (g_avctMng.cbConn[i].connParam.pid == pid)) {
119             cbConn = &g_avctMng.cbConn[i];
120             break;
121         }
122     }
123     return cbConn;
124 }
125