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_dev.h"
16 #include "avctp_int.h"
17 #include "avctp_st.h"
18 #include "log.h"
19 #include "securec.h"
20
21 /*****************************************************************************
22 * Globle Data Define
23 ****************************************************************************/
24 /*****************************************************************************
25 * Function Define
26 ****************************************************************************/
27 /*
28 * Function AvctGetCbDevByAddress
29 * Description This function is called to get the device block by peer device address.
30 * Param[in] peerAddr point of the peer address
31 * Return AvctCbDev the point of control channel block.
32 */
AvctGetCbDevByAddress(const BtAddr * peerAddr)33 AvctCbDev *AvctGetCbDevByAddress(const BtAddr *peerAddr)
34 {
35 LOG_INFO("[AVCT] %{public}s: ", __func__);
36 AvctCbDev *cbDev = NULL;
37 for (uint8_t i = 0; i < AVCT_MAX_DEVICES; i++) {
38 if ((g_avctMng.cbDev[i].alloced) &&
39 (!memcmp((char *)g_avctMng.cbDev[i].peerAddr.addr, (char *)&(peerAddr->addr), BT_ADDR_LENGTH))) {
40 cbDev = &g_avctMng.cbDev[i];
41 LOG_DEBUG("[AVCT] %{public}s: Device id is %hhu", __func__, i);
42 break;
43 }
44 }
45 return cbDev;
46 }
47
48 /*
49 * Function AvctGetCbDevByChId
50 * Description This function is called to get the device block by channel link id.
51 * Param[in] chId channel link id
52 * Return AvctCbDev the point of device block.
53 */
AvctGetCbDevByChId(uint16_t chId)54 AvctCbDev *AvctGetCbDevByChId(uint16_t chId)
55 {
56 LOG_INFO("[AVCT] %{public}s: channel id is 0x%x", __func__, chId);
57 AvctCbDev *cbDev = NULL;
58 for (uint8_t i = 0; i < AVCT_MAX_DEVICES; i++) {
59 LOG_DEBUG("[AVCT] %{public}s: g_avctMng.cbDev[%hhu].chLink is %hhu", __func__, i, g_avctMng.cbDev[i].chLink);
60 if (((g_avctMng.cbDev[i].chLink & AVCT_ALLOC_CTRL) && (g_avctMng.cbDev[i].cbCtrl->chId == chId)) ||
61 ((g_avctMng.cbDev[i].chLink & AVCT_ALLOC_BR) && (g_avctMng.cbDev[i].cbBr->chId == chId))) {
62 cbDev = &g_avctMng.cbDev[i];
63 LOG_DEBUG("[AVCT] %{public}s: Device id is %hhu", __func__, i);
64 break;
65 }
66 }
67 return cbDev;
68 }
69
70 /*
71 * Function AvctCbDevAlloc
72 * Description This function is called to alloce memory for the device block.
73 * Param[in] peerAddr device address.
74 * Return AvctCbDev the point of device block.
75 */
AvctCbDevAlloc(const BtAddr * peerAddr)76 AvctCbDev *AvctCbDevAlloc(const BtAddr *peerAddr)
77 {
78 LOG_INFO("[AVCT] %{public}s:", __func__);
79 AvctCbDev *cbDev = NULL;
80 for (uint8_t i = 0; i < AVCT_MAX_DEVICES; i++) {
81 if (!g_avctMng.cbDev[i].alloced) {
82 cbDev = &g_avctMng.cbDev[i];
83 (void)memset_s(cbDev, sizeof(AvctCbDev), 0, sizeof(AvctCbDev));
84 cbDev->alloced = true;
85 (void)memcpy_s(&(cbDev->peerAddr), sizeof(BtAddr), peerAddr, sizeof(BtAddr));
86 LOG_DEBUG("[AVCT] %{public}s: Device id is %hhu", __func__, i);
87 break;
88 }
89 }
90 return cbDev;
91 }
92
93 /*
94 * Function AvctCbDevDealloc
95 * Description This function is called to dealloce memory for the device block.
96 * Param[in] cbDev The point to device block.
97 * Return void
98 */
AvctCbDevDealloc(AvctCbDev * cbDev)99 void AvctCbDevDealloc(AvctCbDev *cbDev)
100 {
101 LOG_INFO("[AVCT] %{public}s: ", __func__);
102 if (cbDev->cbCtrl != NULL) {
103 AvctCbChDealloc(&(cbDev->cbCtrl));
104 }
105 if (cbDev->cbBr != NULL) {
106 AvctCbChDealloc(&(cbDev->cbBr));
107 }
108 (void)memset_s(cbDev, sizeof(AvctCbDev), 0, sizeof(AvctCbDev));
109 return;
110 }
111
112 /*
113 * Function AvctCbChAlloc
114 * Description This function is called to alloc memory for CbCtrl.
115 * Param[in] *connParam oint to the info of the connection request.
116 * Such as the Role、Profile ID、Callback func point.
117 * Param[in] void
118 * Return AvctCbCh* channel link point.
119 */
AvctCbChAlloc()120 AvctCbCh *AvctCbChAlloc()
121 {
122 LOG_INFO("[AVCT] %{public}s:", __func__);
123 AvctCbCh *cbCh = malloc(sizeof(AvctCbCh));
124 if (cbCh != NULL) {
125 (void)memset_s(cbCh, sizeof(AvctCbCh), 0, sizeof(AvctCbCh));
126 }
127 return cbCh;
128 }
129
130 /*
131 * Function AvctCbChDealloc
132 * Description This function is called to dealloc memory for CbCtrl.
133 * Param[in] *cbCh The point of the chanel block.
134 * Return void.
135 */
AvctCbChDealloc(AvctCbCh ** cbCh)136 void AvctCbChDealloc(AvctCbCh **cbCh)
137 {
138 LOG_INFO("[AVCT] %{public}s:", __func__);
139 if (*cbCh != NULL) {
140 free(*cbCh);
141 *cbCh = NULL;
142 }
143 return;
144 }