1 /*
2  * Copyright (C) 2021-2022 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 "a2dp_avdtp.h"
17 #include <cstring>
18 #include "a2dp_service.h"
19 #include "a2dp_sink.h"
20 #include "a2dp_source.h"
21 #include "log.h"
22 #include "raw_address.h"
23 #include "securec.h"
24 
25 namespace OHOS {
26 namespace bluetooth {
27 const int MEDIA_CATEGORY = 7;
28 const int CATEGORY_NOT_SUPPORTED = 0x29;
29 const int CATEGORY_NO_MEANING = 0x00;
A2dpAvdtp(const uint8_t role)30 A2dpAvdtp::A2dpAvdtp(const uint8_t role)
31 {
32     peerRole_ = role;
33 }
34 
RegisterAvdtp(const AvdtRegisterParam & rcb)35 uint16_t A2dpAvdtp::RegisterAvdtp(const AvdtRegisterParam &rcb)
36 {
37     const_cast<AvdtRegisterParam &>(rcb).ctrlCallback = ProcAvdtpEvent;
38     AVDT_Register(&const_cast<AvdtRegisterParam &>(rcb));
39 
40     return A2DP_SUCCESS;
41 }
42 
CreateStream(const BtAddr & addr,const uint8_t codecIndex,uint16_t & handle)43 uint16_t A2dpAvdtp::CreateStream(const BtAddr &addr, const uint8_t codecIndex, uint16_t &handle)
44 {
45     uint16_t ret = AVDT_CreateStream(&const_cast<BtAddr &>(addr), &handle, codecIndex);
46     return ret;
47 }
48 
RegisterLocalSEP(AvdtStreamConfig cfg[6],const uint8_t number) const49 uint16_t A2dpAvdtp::RegisterLocalSEP(AvdtStreamConfig cfg[6], const uint8_t number) const
50 {
51     LOG_INFO("[A2dpAvdtp] %{public}s number(%u)\n", __func__, number);
52 
53     uint16_t ret = AVDT_RegisterLocalSEP(cfg, const_cast<uint8_t &>(number));
54     return ret;
55 }
56 
ConnectReq(const BtAddr & addr) const57 uint16_t A2dpAvdtp::ConnectReq(const BtAddr &addr) const
58 {
59     uint16_t ret = AVDT_ConnectReq(&const_cast<BtAddr &>(addr), peerRole_);
60     if (ret == A2DP_SUCCESS) {
61         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to call AVDT_ConnectReq() \n", __func__);
62     } else {
63         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to call AVDT_ConnectReq() \n", __func__);
64     }
65 
66     return ret;
67 }
68 
DisconnectReq(const BtAddr & addr)69 uint16_t A2dpAvdtp::DisconnectReq(const BtAddr &addr)
70 {
71     uint16_t ret = AVDT_DisconnectReq(&const_cast<BtAddr &>(addr));
72     if (ret == A2DP_SUCCESS) {
73         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to call AVDT_DisconnectReq() \n", __func__);
74     } else {
75         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to AVDT_DisconnectReq() \n", __func__);
76     }
77 
78     return ret;
79 }
80 
DiscoverReq(const BtAddr & addr,const uint8_t maxNum,uint8_t & label)81 uint16_t A2dpAvdtp::DiscoverReq(const BtAddr &addr, const uint8_t maxNum, uint8_t &label)
82 {
83     uint16_t ret = AVDT_DiscoverReq(&const_cast<BtAddr &>(addr), maxNum, &label);
84     if (ret == A2DP_SUCCESS) {
85         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to call AVDT_DiscoverReq() \n", __func__);
86     } else {
87         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to AVDT_DiscoverReq() \n", __func__);
88     }
89 
90     return ret;
91 }
92 
GetCapabilityReq(const BtAddr & addr,const uint8_t seid,uint8_t & label)93 uint16_t A2dpAvdtp::GetCapabilityReq(const BtAddr &addr, const uint8_t seid, uint8_t &label)
94 {
95     LOG_INFO("[A2dpAvdtp] %{public}s  acpseid(%u)\n", __func__, seid);
96 
97     uint16_t ret = AVDT_GetCapReq(&const_cast<BtAddr &>(addr), seid, &label);
98     if (ret == A2DP_SUCCESS) {
99         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to call AVDT_GetCapReq() \n", __func__);
100     } else {
101         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to GetCapabilityReq() \n", __func__);
102     }
103 
104     return ret;
105 }
106 
GetAllCapabilityReq(const BtAddr & addr,const uint8_t seid,uint8_t & label)107 uint16_t A2dpAvdtp::GetAllCapabilityReq(const BtAddr &addr, const uint8_t seid, uint8_t &label)
108 {
109     LOG_INFO("[A2dpAvdtp] %{public}s  acpseid(%u)\n", __func__, seid);
110 
111     uint16_t ret = AVDT_GetAllCapReq(&const_cast<BtAddr &>(addr), seid, &label);
112     if (ret == A2DP_SUCCESS) {
113         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to call AVDT_GetAllCapReq() \n", __func__);
114     } else {
115         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to GetAllCapabilityReq() \n", __func__);
116     }
117 
118     return ret;
119 }
120 
SetConfigureReq(const BtAddr & addr,const uint16_t handle,const uint8_t seid,const AvdtSepConfig & cfg,uint8_t & label)121 uint16_t A2dpAvdtp::SetConfigureReq(
122     const BtAddr &addr, const uint16_t handle, const uint8_t seid, const AvdtSepConfig &cfg, uint8_t &label)
123 {
124     LOG_INFO("[A2dpAvdtp] %{public}s\n", __func__);
125     for (int i = 0; i < A2DP_CODEC_SIZE; i++) {
126         LOG_INFO("[A2dpAvdtp]%{public}s Data[0x:%x]\n", __func__, cfg.codecInfo[i]);
127     }
128     uint16_t ret = AVDT_SetConfigReq(handle, seid, &const_cast<AvdtSepConfig &>(cfg), &label);
129     if (ret == A2DP_SUCCESS) {
130         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to call AVDT_SetConfigReq() \n", __func__);
131     } else {
132         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to SetConfigureReq() \n", __func__);
133     }
134 
135     return ret;
136 }
137 
SetConfigureRsp(const uint16_t handle,const uint8_t label,const AvdtCatetory category)138 uint16_t A2dpAvdtp::SetConfigureRsp(const uint16_t handle, const uint8_t label, const AvdtCatetory category)
139 {
140     LOG_INFO("[A2dpAvdtp] %{public}s handle[%u] label[%u]\n", __func__, handle, label);
141 
142     uint16_t ret = AVDT_SetConfigRsp(handle, label, category);
143     if (ret == A2DP_SUCCESS) {
144         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to call AVDT_SetConfigRsp() \n", __func__);
145     } else {
146         LOG_INFO("[A2dpAvdtp] %{public}s Failed to AVDT_SetConfigRsp() \n", __func__);
147     }
148 
149     return ret;
150 }
151 
ReconfigureRsp(const uint16_t handle,const uint8_t label,const AvdtCatetory category)152 uint16_t A2dpAvdtp::ReconfigureRsp(const uint16_t handle, const uint8_t label, const AvdtCatetory category)
153 {
154     LOG_INFO("[A2dpAvdtp] %{public}s handle[%u] label[%u]\n", __func__, handle, label);
155 
156     uint16_t ret = AVDT_ReconfigRsp(handle, label, category);
157     if (ret == A2DP_SUCCESS) {
158         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to call AVDT_ReconfigRsp() \n", __func__);
159     } else {
160         LOG_INFO("[A2dpAvdtp] %{public}s Failed to AVDT_ReconfigRsp() \n", __func__);
161     }
162 
163     return ret;
164 }
165 
WriteStream(uint16_t handle,Packet * pkt,uint32_t timeStamp,uint8_t payloadType,uint16_t marker)166 uint16_t A2dpAvdtp::WriteStream(
167     uint16_t handle, Packet *pkt, uint32_t timeStamp, uint8_t payloadType, uint16_t marker)
168 {
169     uint16_t ret = AVDT_WriteReq(handle, pkt, timeStamp, payloadType, marker);
170     return ret;
171 }
172 
OpenReq(const uint16_t handle,uint8_t & label)173 uint16_t A2dpAvdtp::OpenReq(const uint16_t handle, uint8_t &label)
174 {
175     uint16_t ret = AVDT_OpenReq(handle, &label);
176     if (ret == A2DP_SUCCESS) {
177         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to call AVDT_OpenReq() \n", __func__);
178     } else {
179         LOG_INFO("[A2dpAvdtp] %{public}s Failed to AVDT_OpenReq() \n", __func__);
180     }
181 
182     return ret;
183 }
184 
DelayReq(const uint16_t handle,uint8_t & label,uint16_t delayValue)185 uint16_t A2dpAvdtp::DelayReq(const uint16_t handle, uint8_t &label, uint16_t delayValue)
186 {
187     uint16_t ret = AVDT_DelayReq(handle, &label, delayValue);
188     return ret;
189 }
190 
CloseReq(const uint16_t handle)191 uint16_t A2dpAvdtp::CloseReq(const uint16_t handle)
192 {
193     LOG_INFO("[A2dpAvdtp] %{public}s handle(%u)\n", __func__, handle);
194 
195     uint16_t ret = AVDT_CloseReq(handle);
196     if (ret == A2DP_SUCCESS) {
197         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to call AVDT_CloseReq() \n", __func__);
198     } else {
199         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to CloseReq() \n", __func__);
200     }
201 
202     return ret;
203 }
204 
CloseRsp(const uint16_t handle,const uint8_t label,const uint8_t errCode)205 uint16_t A2dpAvdtp::CloseRsp(const uint16_t handle, const uint8_t label, const uint8_t errCode)
206 {
207     uint16_t ret = AVDT_CloseRsp(handle, label, errCode);
208     if (ret == A2DP_SUCCESS) {
209         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to call AVDT_CloseRsp() \n", __func__);
210     } else {
211         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to CloseRsp() \n", __func__);
212     }
213 
214     return ret;
215 }
216 
AbortReq(const uint16_t handle,uint8_t & label)217 uint16_t A2dpAvdtp::AbortReq(const uint16_t handle, uint8_t &label)
218 {
219     uint16_t ret = AVDT_AbortReq(handle, &label);
220     return ret;
221 }
222 
SuspendReq(const uint16_t handle,uint8_t & label)223 uint16_t A2dpAvdtp::SuspendReq(const uint16_t handle, uint8_t &label)
224 {
225     LOG_INFO("[A2dpAvdtp] %{public}s handle(%u)\n", __func__, handle);
226 
227     uint16_t handles[] = {handle};
228     uint16_t ret = AVDT_SuspendReq(handles, A2DP_STREAM_HANDLES_NUMBER, &label);
229     if (ret == A2DP_SUCCESS) {
230         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to call AVDT_SuspendReq() \n", __func__);
231     } else {
232         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to AVDT_SuspendReq() \n", __func__);
233     }
234 
235     return ret;
236 }
237 
StartReq(const uint16_t handle,uint8_t & label)238 uint16_t A2dpAvdtp::StartReq(const uint16_t handle, uint8_t &label)
239 {
240     LOG_INFO("[A2dpAvdtp] %{public}s handle[%u]\n", __func__, handle);
241 
242     uint16_t handles[] = {handle};
243     uint16_t ret = AVDT_StartReq(handles, A2DP_STREAM_HANDLES_NUMBER, &label);
244     if (ret == A2DP_SUCCESS) {
245         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to call AVDT_StartReq() \n", __func__);
246     } else {
247         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to AVDT_StartReq() \n", __func__);
248     }
249 
250     return ret;
251 }
252 
SuspendRsp(const uint16_t handle,const uint8_t label,const uint16_t errHandle,const uint8_t errCode)253 uint16_t A2dpAvdtp::SuspendRsp(
254     const uint16_t handle, const uint8_t label, const uint16_t errHandle, const uint8_t errCode)
255 {
256     LOG_INFO("[A2dpAvdtp] %{public}s handle[%u]\n", __func__, handle);
257 
258     uint16_t ret = AVDT_SuspendRsp(handle, label, errHandle, errCode);
259     return ret;
260 }
261 
StartRsp(const uint16_t handle,const uint8_t label,const uint16_t errHandle,const uint8_t errCode)262 uint16_t A2dpAvdtp::StartRsp(
263     const uint16_t handle, const uint8_t label, const uint16_t errHandle, const uint8_t errCode)
264 {
265     LOG_INFO("[A2dpAvdtp] %{public}s handle[%u]\n", __func__, handle);
266 
267     uint16_t ret = AVDT_StartRsp(handle, label, errHandle, errCode);
268     return ret;
269 }
270 
ReconfigureReq(const uint16_t handle,const AvdtSepConfig & cfg,uint8_t & label)271 uint16_t A2dpAvdtp::ReconfigureReq(const uint16_t handle, const AvdtSepConfig &cfg, uint8_t &label)
272 {
273     uint16_t ret = AVDT_ReconfigReq(handle, &const_cast<AvdtSepConfig &>(cfg), &label);
274     if (ret == A2DP_SUCCESS) {
275         LOG_DEBUG("[A2dpAvdtp] %{public}s Success to AVDT_ReconfigReq() \n", __func__);
276     } else {
277         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to AVDT_ReconfigReq() \n", __func__);
278     }
279 
280     return ret;
281 }
282 
ProcAvdtpEvent(uint16_t handle,const BtAddr * bdAddr,uint8_t event,const AvdtCtrlData * data,uint8_t role)283 void A2dpAvdtp::ProcAvdtpEvent(
284     uint16_t handle, const BtAddr *bdAddr, uint8_t event, const AvdtCtrlData *data, uint8_t role)
285 {
286     LOG_INFO("[A2dpAvdtp]%{public}s role[%u], event[%u] handle[%u] ", __func__, role, event, handle);
287 
288     std::unique_ptr<A2dpAvdtMsg> msg = std::make_unique<A2dpAvdtMsg>();
289     msg->handle = handle;
290     msg->event = event;
291     msg->role = role;
292     (void)memcpy_s(&msg->a2dpMsg.msg, sizeof(AvdtCtrlData), data, sizeof(AvdtCtrlData));
293 
294     utility::Message msgData(A2DP_AVDTP_EVT, (int)A2DP_SUCCESS, msg.release());
295     SwitchThreadToA2dpService(role, *bdAddr, msgData);
296 }
297 
SwitchThreadToA2dpService(uint8_t role,BtAddr bdAddr,utility::Message msgData)298 void A2dpAvdtp::SwitchThreadToA2dpService(uint8_t role, BtAddr bdAddr, utility::Message msgData)
299 {
300     A2dpService *service = GetServiceInstance(role);
301     RawAddress peerAddr = RawAddress::ConvertToString(bdAddr.addr);
302 
303     if (service != nullptr) {
304         service->PostEvent(msgData, peerAddr);
305     }
306 }
307 
ParseAvdtpDisconnectInd(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg)308 uint8_t A2dpAvdtp::ParseAvdtpDisconnectInd(const uint16_t handle,
309     const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg)
310 {
311     A2dpProfile *profile = GetProfileInstance(role);
312     if (profile == nullptr) {
313         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
314         return EVT_CONNECT_IND;
315     }
316 
317     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
318     if (peer == nullptr) {
319         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
320         return EVT_CONNECT_IND;
321     }
322     peer->StopSignalingTimer();
323 
324     msg.a2dpMsg.stream.addr = bdAddr;
325     msg.a2dpMsg.stream.handle = handle;
326 
327     return EVT_DISCONNECT_IND;
328 }
329 
ParseAvdtpConfigureInd(const uint16_t handle,const BtAddr bdAddr,const AvdtCtrlData & data,const uint8_t role,A2dpAvdtMsg & msg)330 uint8_t A2dpAvdtp::ParseAvdtpConfigureInd(
331     const uint16_t handle, const BtAddr bdAddr, const AvdtCtrlData &data, const uint8_t role, A2dpAvdtMsg &msg)
332 {
333     LOG_INFO("[A2dpAvdtp] %{public}s role(%u)\n", __func__, role);
334 
335     A2dpProfile *profile = GetProfileInstance(role);
336     if (profile == nullptr) {
337         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
338         return EVT_CONNECT_IND;
339     }
340 
341     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
342     if (peer == nullptr) {
343         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
344         return EVT_CONNECT_IND;
345     }
346     if (peer->GetInitSide()) {
347         LOG_INFO("[A2dpAvdtp] %{public}s it's initSide, reject the AVDT_CONFIG_IND_EVT \n", __func__);
348         AvdtCatetory category = {.errCode = AVDT_ERR_BAD_STATE, .category = CATEGORY_NO_MEANING};
349         SetConfigureRsp(handle, data.configInd.hdr.label, category);
350         return EVT_CONNECT_IND;
351     }
352 
353     msg.a2dpMsg.configRsp.addr = bdAddr;
354     msg.a2dpMsg.configRsp.handle = static_cast<uint8_t>(handle);
355     msg.a2dpMsg.configRsp.label = data.configInd.hdr.label;
356     (void)memcpy_s(msg.a2dpMsg.configRsp.codecInfo, A2DP_CODEC_SIZE, data.configInd.cfg.codecInfo, A2DP_CODEC_SIZE);
357     msg.a2dpMsg.configRsp.role = role;
358 
359     peer->SetCurrentCmd(EVT_SETCONFIG_IND);
360     peer->SetStreamHandle(handle);
361     peer->UpdatePeerMtu(data.configInd.hdr.mtu);
362     if (role == A2DP_ROLE_SOURCE) {
363         peer->SetInitSide(false);
364         peer->SetIntSeid(handle);
365         peer->SetAcpSeid(data.configInd.acpSeid);
366         peer->SetPeerCapInfo(handle, peer->GetAcpSeid(), data.configInd.cfg, role);
367     }
368 
369     return EVT_SETCONFIG_IND;
370 }
371 
ParseAvdtpReconfigureInd(const uint16_t handle,const BtAddr bdAddr,const AvdtCtrlData & data,const uint8_t role,A2dpAvdtMsg & msg)372 uint8_t A2dpAvdtp::ParseAvdtpReconfigureInd(
373     const uint16_t handle, const BtAddr bdAddr, const AvdtCtrlData &data, const uint8_t role, A2dpAvdtMsg &msg)
374 {
375     bool ret = false;
376     uint8_t resultConfig[A2DP_CODEC_SIZE];
377     (void)memset_s(resultConfig, A2DP_CODEC_SIZE, 0, A2DP_CODEC_SIZE);
378     uint8_t *pResultConfig = &resultConfig[0];
379     A2dpProfile *profile = GetProfileInstance(role);
380     if (profile == nullptr) {
381         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
382         return EVT_CONNECT_IND;
383     }
384     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
385     if (peer == nullptr) {
386         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
387         return EVT_CONNECT_IND;
388     }
389 
390     peer->StopSignalingTimer();
391     msg.a2dpMsg.msg = data;
392     msg.a2dpMsg.configRsp.addr = bdAddr;
393     msg.a2dpMsg.configRsp.handle = static_cast<uint8_t>(handle);
394     msg.a2dpMsg.configRsp.label = data.reconfigInd.hdr.label;
395     uint8_t *pCodecInfo = const_cast<uint8_t *>(data.reconfigInd.cfg.codecInfo);
396 
397     if (role == AVDT_ROLE_SNK) {
398         if (peer->GetCodecConfigure()->SetPeerSourceCodecCapabilities(pCodecInfo)) {
399             ret = peer->GetCodecConfigure()->SetSinkCodecConfig(pCodecInfo, pResultConfig);
400         } else {
401             LOG_ERROR("[A2dpAvdtp] %{public}s Failed to SetPeerSourceCodecCapabilities()\n", __func__);
402         }
403     } else {
404         if (peer->GetCodecConfigure()->SetPeerSinkCodecCapabilities(pCodecInfo)) {
405             ret = peer->GetCodecConfigure()->SetCodecConfig(pCodecInfo, pResultConfig);
406         } else {
407             LOG_ERROR("[A2dpAvdtp] %{public}s Failed to SetPeerSinkCodecCapabilities()\n", __func__);
408         }
409     }
410     LOG_INFO("[A2dpAvdtp] %{public}s SetCodecConfig[%{public}d]\n", __func__, ret);
411 
412     if (ret) {
413         msg.a2dpMsg.configRsp.category.errCode = AVDT_SUCCESS;
414     } else {
415         msg.a2dpMsg.configRsp.category.category = MEDIA_CATEGORY;  // Media Codec 7
416         msg.a2dpMsg.configRsp.category.errCode = CATEGORY_NOT_SUPPORTED;    // not supported
417     }
418     peer->SetCurrentCmd(EVT_RECONFIG_IND);
419 
420     return EVT_RECONFIG_IND;
421 }
422 
ParseAvdtpOpenInd(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg)423 uint8_t A2dpAvdtp::ParseAvdtpOpenInd(
424     const uint16_t handle, const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg)
425 {
426     A2dpProfile *profile = GetProfileInstance(role);
427     if (profile == nullptr) {
428         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
429         return EVT_CONNECT_IND;
430     }
431 
432     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
433     if (peer == nullptr) {
434         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
435         return EVT_CONNECT_IND;
436     }
437 
438     peer->StopSignalingTimer();
439     peer->SetStreamHandle(handle);
440     msg.a2dpMsg.stream.addr = bdAddr;
441     msg.a2dpMsg.stream.handle = handle;
442 
443     return EVT_OPEN_IND;
444 }
445 
ParseAvdtpCloseInd(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)446 uint8_t A2dpAvdtp::ParseAvdtpCloseInd(
447     const uint16_t handle, const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
448 {
449     A2dpProfile *profile = GetProfileInstance(role);
450     if (profile == nullptr) {
451         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
452         return EVT_CONNECT_IND;
453     }
454     A2dpProfilePeer *profilePeer = profile->FindPeerByAddress(bdAddr);
455     if (profilePeer == nullptr) {
456         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
457         return EVT_CONNECT_IND;
458     }
459 
460     msg.a2dpMsg.stream.addr = bdAddr;
461     msg.a2dpMsg.stream.handle = handle;
462     msg.a2dpMsg.stream.label = data.closeInd.label;
463 
464     profilePeer->SetStreamHandle(handle);
465     profilePeer->StopSignalingTimer();
466     profilePeer->SetCurrentCmd(EVT_CLOSE_IND);
467     profilePeer->SetSignalingTimer(A2DP_ACCEPT_CLOSING_SIGNALLING_TIMEOUT_MS, false);
468 
469     return EVT_CLOSE_IND;
470 }
471 
ParseAvdtpCloseChannelInd(const BtAddr bdAddr,const uint16_t handle,const uint8_t role,A2dpAvdtMsg & msg)472 uint8_t A2dpAvdtp::ParseAvdtpCloseChannelInd(
473     const BtAddr bdAddr, const uint16_t handle, const uint8_t role, A2dpAvdtMsg &msg)
474 {
475     A2dpProfile *profile = GetProfileInstance(role);
476     if (profile == nullptr) {
477         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
478         return EVT_CONNECT_IND;
479     }
480     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
481     if (peer == nullptr) {
482         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
483         return EVT_CONNECT_IND;
484     }
485 
486     peer->StopSignalingTimer();
487     msg.a2dpMsg.stream.addr = bdAddr;
488 
489     return EVT_CLOSE_TRANS_IND;
490 }
491 
ParseAvdtpStartInd(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)492 uint8_t A2dpAvdtp::ParseAvdtpStartInd(
493     const uint16_t handle, const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
494 {
495     A2dpProfile *profile = GetProfileInstance(role);
496     if (profile == nullptr) {
497         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
498         return EVT_CONNECT_IND;
499     }
500 
501     msg.a2dpMsg.stream.addr = bdAddr;
502     msg.a2dpMsg.stream.handle = handle;
503     msg.a2dpMsg.stream.label = data.startInd.label;
504 
505     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
506     if (peer == nullptr) {
507         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
508         return EVT_CONNECT_IND;
509     }
510     peer->UpdatePeerEdr((uint8_t)data.startInd.mtu);
511     return EVT_START_IND;
512 }
513 
ParseAvdtpSuspendInd(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)514 uint8_t A2dpAvdtp::ParseAvdtpSuspendInd(
515     const uint16_t handle, const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
516 {
517     LOG_INFO("[A2dpAvdtp] %{public}s role(%u)\n", __func__, role);
518 
519     msg.a2dpMsg.stream.addr = bdAddr;
520     msg.a2dpMsg.stream.handle = handle;
521     msg.a2dpMsg.stream.label = data.suspendInd.label;
522 
523     return EVT_SUSPEND_IND;
524 }
525 
ParseAvdtpDelayReportInd(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)526 uint8_t A2dpAvdtp::ParseAvdtpDelayReportInd(
527     const uint16_t handle, const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
528 {
529     LOG_INFO("[A2dpAvdtp] %{public}s role(%{public}u), delay(%{public}d)\n", __func__, role, data.delayRptInd.delay);
530 
531     msg.a2dpMsg.delayReportInfo.addr = bdAddr;
532     msg.a2dpMsg.delayReportInfo.handle = handle;
533     msg.a2dpMsg.delayReportInfo.delayValue = data.delayRptInd.delay;
534 
535     return EVT_DELAY_IND;
536 }
537 
ParseAvdtpConnectCFM(const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)538 uint8_t A2dpAvdtp::ParseAvdtpConnectCFM(
539     const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
540 {
541     LOG_INFO("[A2dpAvdtp] %{public}s errcode(%u)\n", __func__, data.connectCfm.errCode);
542 
543     A2dpProfile *profile = GetProfileInstance(role);
544     if (profile == nullptr) {
545         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
546         return EVT_CONNECT_IND;
547     }
548     A2dpProfilePeer *peer = profile->FindOrCreatePeer(bdAddr, role);
549     if (peer == nullptr) {
550         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
551         return EVT_CONNECT_IND;
552     }
553     if (data.connectCfm.errCode) {
554         LOG_ERROR("[A2dpAvdtp] %{public}s Peer reject the connect req!!\n", __func__);
555         peer->SetInitSide(false);
556         return EVT_DISCONNECT_CFM;
557     }
558     peer->SetCurrentCmd(EVT_DISCOVER_REQ);
559     peer->SetSignalingTimer(A2DP_ACCEPT_SIGNALLING_TIMEOUT_MS, false);
560     peer->UpdatePeerMtu(data.connectCfm.mtu);
561 
562     msg.a2dpMsg.connectInfo.addr = bdAddr;
563     msg.a2dpMsg.connectInfo.errCode = data.connectCfm.errCode;
564 
565     return EVT_DISCOVER_REQ;
566 }
567 
ParseAvdtpDisconnectCFM(const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg)568 uint8_t A2dpAvdtp::ParseAvdtpDisconnectCFM(const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg)
569 {
570     A2dpProfile *profile = GetProfileInstance(role);
571     if (profile == nullptr) {
572         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
573         return EVT_CONNECT_IND;
574     }
575     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
576     if (peer == nullptr) {
577         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
578         return EVT_CONNECT_IND;
579     }
580 
581     peer->StopSignalingTimer();
582     msg.a2dpMsg.stream.addr = bdAddr;
583 
584     return EVT_DISCONNECT_CFM;
585 }
586 
ParseAvdtpDiscoverCFM(const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)587 uint8_t A2dpAvdtp::ParseAvdtpDiscoverCFM(
588     const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
589 {
590     uint8_t cmd = EVT_CONNECT_IND;
591     A2dpProfile *profile = GetProfileInstance(role);
592     if (profile == nullptr) {
593         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
594         return cmd;
595     }
596     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
597     if (peer == nullptr) {
598         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
599         return cmd;
600     }
601 
602     peer->StopSignalingTimer();
603     peer->SetPeerSepInfo(data.discoverCfm);
604     msg.a2dpMsg.stream.addr = bdAddr;
605     msg.a2dpMsg.stream.acpSeid = peer->GetPeerSEPInformation().seid;
606     peer->UpdateSepIndex();
607     peer->SetAcpSeid(msg.a2dpMsg.stream.acpSeid);
608 
609     if (peer->GetAvdtpVersion()) {
610         cmd = EVT_GET_ALLCAP_REQ;
611         peer->SetCurrentCmd(EVT_GET_ALLCAP_REQ);
612     } else {
613         cmd = EVT_GET_CAP_REQ;
614         peer->SetCurrentCmd(EVT_GET_CAP_REQ);
615     }
616     peer->SetSignalingTimer(A2DP_ACCEPT_SIGNALLING_TIMEOUT_MS, false);
617 
618     return cmd;
619 }
620 
ParseAvdtpGetCapabilityCFM(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)621 uint8_t A2dpAvdtp::ParseAvdtpGetCapabilityCFM(
622     const uint16_t handle, const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
623 {
624     uint8_t evt = EVT_GET_CAP_REQ;
625     A2dpProfile *profile = GetProfileInstance(role);
626     bool capabilityComplete = false;
627     if (profile == nullptr) {
628         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
629         return EVT_CONNECT_IND;
630     }
631     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
632     if (peer == nullptr) {
633         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
634         return EVT_CONNECT_IND;
635     }
636 
637     peer->StopSignalingTimer();
638     peer->SetPeerCapInfo(handle, peer->GetAcpSeid(), data.getCapCfm.cfg, role);
639 
640     msg.a2dpMsg.stream.addr = bdAddr;
641     msg.a2dpMsg.stream.acpSeid = peer->GetPeerSEPInformation().seid;
642     LOG_INFO("[A2dpAvdtp] %{public}s Get acp seid(%u) \n", __func__, msg.a2dpMsg.stream.acpSeid);
643     if (peer->GetPeerCapComplete(role)) {
644         capabilityComplete = true;
645     }
646     if (msg.a2dpMsg.stream.acpSeid != 0 && !capabilityComplete) {
647         peer->UpdateSepIndex();
648         peer->SetAcpSeid(msg.a2dpMsg.stream.acpSeid);
649         peer->SetSignalingTimer(A2DP_ACCEPT_SIGNALLING_TIMEOUT_MS, false);
650     } else {
651         if (peer->JudgeCapabilityMatched(role) && peer->GetInitSide()) {
652             peer->SetConfigure();
653             evt = EVT_SETCONFIG_REQ;
654             (void)memcpy_s(&msg.a2dpMsg.configStream, sizeof(ConfigureStream),
655                 (&peer->GetConfigure()), sizeof(ConfigureStream));
656             peer->SetCurrentCmd(EVT_SETCONFIG_REQ);
657             peer->SetSignalingTimer(A2DP_ACCEPT_SIGNALLING_TIMEOUT_MS, false);
658         } else if (!peer->GetInitSide()) {
659             evt = EVT_CONNECT_IND;
660         }
661     }
662     return evt;
663 }
664 
ParseAvdtpGetALLCapCFM(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)665 uint8_t A2dpAvdtp::ParseAvdtpGetALLCapCFM(
666     const uint16_t handle, const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
667 {
668     uint8_t evt = EVT_CONNECT_IND;
669     A2dpProfile *profile = GetProfileInstance(role);
670     bool capabilityComplete = false;
671     if (profile == nullptr) {
672         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
673         return EVT_CONNECT_IND;
674     }
675     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
676     if (peer == nullptr) {
677         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
678         return EVT_CONNECT_IND;
679     }
680 
681     peer->StopSignalingTimer();
682     peer->SetPeerCapInfo(handle, peer->GetAcpSeid(), data.getCapCfm.cfg, role);
683 
684     msg.a2dpMsg.stream.addr = bdAddr;
685     msg.a2dpMsg.stream.acpSeid = peer->GetPeerSEPInformation().seid;
686 
687     if (peer->GetPeerCapComplete(role)) {
688         capabilityComplete = true;
689     }
690 
691     if (msg.a2dpMsg.stream.acpSeid != 0 && !capabilityComplete) {
692         peer->UpdateSepIndex();
693         peer->SetAcpSeid(msg.a2dpMsg.stream.acpSeid);
694         evt = EVT_GET_ALLCAP_REQ;
695     } else {
696         if (peer->JudgeCapabilityMatched(role) && peer->GetInitSide()) {
697             peer->SetConfigure();
698             evt = EVT_SETCONFIG_REQ;
699             (void)memcpy_s(&msg.a2dpMsg.configStream, sizeof(ConfigureStream), (&peer->GetConfigure()),
700                 sizeof(ConfigureStream));
701             peer->SetCurrentCmd(EVT_SETCONFIG_REQ);
702         } else if (!peer->GetInitSide()) {
703             evt = EVT_CONNECT_IND;
704         }
705     }
706 
707     return evt;
708 }
709 
ParseAvdtpConfigureCFM(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)710 uint8_t A2dpAvdtp::ParseAvdtpConfigureCFM(
711     const uint16_t handle, const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
712 {
713     A2dpProfile *profile = GetProfileInstance(role);
714     if (profile == nullptr) {
715         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
716         return EVT_CONNECT_IND;
717     }
718     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
719     if (peer == nullptr) {
720         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
721         return EVT_CONNECT_IND;
722     }
723 
724     if (role == A2DP_ROLE_SINK) {
725         LOG_INFO("[A2dpAvdtp] %{public}s role(%{public}u)  to send delay reporting\n", __func__, role);
726         uint8_t label = 0;
727         DelayReq(handle, label, 0);
728     }
729 
730     peer->StopSignalingTimer();
731     msg.a2dpMsg.stream.handle = handle;
732     msg.a2dpMsg.stream.addr = bdAddr;
733     peer->SetIntSeid(static_cast<uint8_t>(handle));
734     peer->SetStreamHandle(handle);
735     peer->SetCurrentCmd(EVT_OPEN_REQ);
736     peer->SetSignalingTimer(A2DP_SIGNALLING_TIMEOUT_MS, false);
737 
738     return EVT_OPEN_REQ;
739 }
740 
ParseAvdtpGetConfigureCFM(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,const A2dpAvdtMsg & msg,const AvdtCtrlData & data)741 uint8_t A2dpAvdtp::ParseAvdtpGetConfigureCFM(const uint16_t handle, const BtAddr bdAddr,
742     const uint8_t role, const A2dpAvdtMsg &msg, const AvdtCtrlData &data)
743 {
744     A2dpProfile *profile = GetProfileInstance(role);
745     if (profile == nullptr) {
746         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
747         return EVT_CONNECT_IND;
748     }
749     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
750     if (peer == nullptr) {
751         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
752         return EVT_CONNECT_IND;
753     }
754 
755     peer->StopSignalingTimer();
756 
757     return EVT_GETCONFIG_CFM;
758 }
759 
ParseAvdtpReconfigureCFM(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)760 uint8_t A2dpAvdtp::ParseAvdtpReconfigureCFM(
761     const uint16_t handle, const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
762 {
763     A2dpProfile *profile = GetProfileInstance(role);
764     if (profile == nullptr) {
765         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
766         return EVT_CONNECT_IND;
767     }
768     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
769     if (peer == nullptr) {
770         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
771         return EVT_CONNECT_IND;
772     }
773 
774     peer->StopSignalingTimer();
775     if (data.reconfigCfm.hdr.errCode) {
776         peer->SetRestart(false);
777         if (peer->GetReconfigTag()) {
778             peer->SetReconfigTag(false);
779             return EVT_CONNECT_IND;
780         }
781         peer->SetReconfigTag(true);
782         A2dpService *service = GetServiceInstance(role);
783         if (service != nullptr) {
784             A2dpSrcCodecStatus codecStatus = service->GetCodecStatus(RawAddress::ConvertToString(bdAddr.addr));
785             profile->SetUserCodecConfigure(bdAddr, codecStatus.codecInfo);
786         }
787         LOG_ERROR("[A2dpAvdtp] %{public}s errCode(0x%x)\n", __func__, data.reconfigCfm.hdr.errCode);
788         return EVT_CONNECT_IND;
789     }
790     if (peer->GetReconfigTag()) {
791         peer->SetReconfigTag(false);
792     }
793     peer->SetIntSeid(static_cast<uint8_t>(handle));
794     peer->SetStreamHandle(handle);
795     msg.a2dpMsg.stream.addr = bdAddr;
796     msg.a2dpMsg.stream.handle = handle;
797 
798     return EVT_RECONFIG_CFM;
799 }
800 
ParseAvdtpOpenCFM(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)801 uint8_t A2dpAvdtp::ParseAvdtpOpenCFM(
802     const uint16_t handle, const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
803 {
804     A2dpProfile *profile = GetProfileInstance(role);
805     if (profile == nullptr) {
806         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
807         return EVT_CONNECT_IND;
808     }
809     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
810     if (peer == nullptr) {
811         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
812         return EVT_CONNECT_IND;
813     }
814 
815     peer->StopSignalingTimer();
816     peer->UpdatePeerMtu(data.connectCfm.mtu);
817     msg.a2dpMsg.stream.addr = bdAddr;
818     msg.a2dpMsg.stream.handle = handle;
819     msg.a2dpMsg.stream.errCode = data.connectCfm.errCode;
820 
821     return EVT_OPEN_CFM;
822 }
823 
ParseAvdtpStartCFM(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)824 uint8_t A2dpAvdtp::ParseAvdtpStartCFM(
825     const uint16_t handle, const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
826 {
827     A2dpProfile *profile = GetProfileInstance(role);
828     if (profile == nullptr) {
829         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
830         return EVT_CONNECT_IND;
831     }
832     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
833     if (peer == nullptr) {
834         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
835         return EVT_CONNECT_IND;
836     }
837 
838     peer->StopSignalingTimer();
839     msg.a2dpMsg.stream.addr = bdAddr;
840     msg.a2dpMsg.stream.handle = handle;
841     peer->UpdatePeerEdr(static_cast<uint8_t>(data.startCfm.mtu));
842     return EVT_START_CFM;
843 }
844 
ParseAvdtpSuspendCFM(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)845 uint8_t A2dpAvdtp::ParseAvdtpSuspendCFM(
846     const uint16_t handle, const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
847 {
848     A2dpProfile *profile = GetProfileInstance(role);
849     if (profile == nullptr) {
850         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
851         return EVT_CONNECT_IND;
852     }
853     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
854     if (peer == nullptr) {
855         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
856         return EVT_CONNECT_IND;
857     }
858 
859     peer->StopSignalingTimer();
860     msg.a2dpMsg.stream.addr = bdAddr;
861     msg.a2dpMsg.stream.handle = handle;
862 
863     return EVT_SUSPEND_CFM;
864 }
865 
ParseAvdtpStreamCloseCFM(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,A2dpAvdtMsg & msg,const AvdtCtrlData & data)866 uint8_t A2dpAvdtp::ParseAvdtpStreamCloseCFM(
867     const uint16_t handle, const BtAddr bdAddr, const uint8_t role, A2dpAvdtMsg &msg, const AvdtCtrlData &data)
868 {
869     A2dpProfile *profile = GetProfileInstance(role);
870     if (profile == nullptr) {
871         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
872         return EVT_CONNECT_IND;
873     }
874     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
875     if (peer == nullptr) {
876         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
877         return EVT_CONNECT_IND;
878     }
879 
880     peer->StopSignalingTimer();
881     msg.a2dpMsg.stream.addr = bdAddr;
882     msg.a2dpMsg.stream.handle = handle;
883 
884     return EVT_CLOSE_CFM;
885 }
886 
ParseAvdtpAbortCFM(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,const A2dpAvdtMsg & msg,const AvdtCtrlData & data)887 uint8_t A2dpAvdtp::ParseAvdtpAbortCFM(const uint16_t handle, const BtAddr bdAddr,
888     const uint8_t role, const A2dpAvdtMsg &msg, const AvdtCtrlData &data)
889 {
890     A2dpProfile *profile = GetProfileInstance(role);
891     if (profile == nullptr) {
892         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
893         return EVT_CONNECT_IND;
894     }
895     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
896     if (peer == nullptr) {
897         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
898         return EVT_CONNECT_IND;
899     }
900 
901     peer->StopSignalingTimer();
902 
903     return EVT_ABORT_CFM;
904 }
905 
ParseAvdtpWriteCFM(const uint16_t handle,const BtAddr bdAddr,const uint8_t role,const A2dpAvdtMsg & msg,const AvdtCtrlData & data)906 uint8_t A2dpAvdtp::ParseAvdtpWriteCFM(const uint16_t handle, const BtAddr bdAddr,
907     const uint8_t role, const A2dpAvdtMsg &msg, const AvdtCtrlData &data)
908 {
909     LOG_INFO("[A2dpAvdtp] %{public}s role(%u)\n", __func__, role);
910     A2dpProfile *profile = GetProfileInstance(role);
911     if (profile == nullptr) {
912         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get profile instance \n", __func__);
913         return EVT_CONNECT_IND;
914     }
915     A2dpProfilePeer *peer = profile->FindPeerByAddress(bdAddr);
916     if (peer == nullptr) {
917         LOG_ERROR("[A2dpAvdtp] %{public}s Failed to get peer instance \n", __func__);
918         return EVT_CONNECT_IND;
919     }
920     return EVT_WRITE_CFM;
921 }
922 
ParseAvdtpCallbackContent(const uint16_t handle,const BtAddr bdAddr,const uint8_t event,const AvdtCtrlData & data,const uint8_t role) const923 A2dpAvdtMsg *A2dpAvdtp::ParseAvdtpCallbackContent(const uint16_t handle, const BtAddr bdAddr,
924     const uint8_t event, const AvdtCtrlData &data, const uint8_t role) const
925 {
926     LOG_INFO("[A2dpAvdtp] %{public}s event(%u) role(%u)\n", __func__, event, role);
927     std::unique_ptr<A2dpAvdtMsg> msg = std::make_unique<A2dpAvdtMsg>();
928     uint8_t avdtEvent = EVT_CONNECT_IND;
929 
930     switch (event) {
931         case AVDT_CONNECT_IND_EVT:
932             break;
933         case AVDT_DISCONNECT_IND_EVT:
934             avdtEvent = ParseAvdtpDisconnectInd(handle, bdAddr, role, *msg.get());
935             break;
936         case AVDT_CONFIG_IND_EVT:
937             avdtEvent = ParseAvdtpConfigureInd(handle, bdAddr, data, role, *msg.get());
938             break;
939         case AVDT_OPEN_IND_EVT:
940             avdtEvent = ParseAvdtpOpenInd(handle, bdAddr, role, *msg.get());
941             break;
942         case AVDT_START_IND_EVT:
943             avdtEvent = ParseAvdtpStartInd(handle, bdAddr, role, *msg.get(), data);
944             break;
945         case AVDT_SUSPEND_IND_EVT:
946             avdtEvent = ParseAvdtpSuspendInd(handle, bdAddr, role, *msg.get(), data);
947             break;
948         case AVDT_RECONFIG_IND_EVT:
949             avdtEvent = ParseAvdtpReconfigureInd(handle, bdAddr, data, role, *msg.get());
950             break;
951         case AVDT_CLOSE_IND_EVT:
952             avdtEvent = ParseAvdtpCloseInd(handle, bdAddr, role, *msg.get(), data);
953             break;
954         case AVDT_CLOSE_TRANS_IND_EVT:
955             avdtEvent = ParseAvdtpCloseChannelInd(bdAddr, handle, role, *msg.get());
956             break;
957         case AVDT_DELAY_REPORT_IND_EVT:
958             avdtEvent = ParseAvdtpDelayReportInd(handle, bdAddr, role, *msg.get(), data);
959             break;
960         case AVDT_CONNECT_CFM_EVT:
961             avdtEvent = ParseAvdtpConnectCFM(bdAddr, role, *msg.get(), data);
962             break;
963         case AVDT_DISCONNECT_CFM_EVT:
964             avdtEvent = ParseAvdtpDisconnectCFM(bdAddr, role, *msg.get());
965             break;
966         case AVDT_DISCOVER_CFM_EVT:
967             avdtEvent = ParseAvdtpDiscoverCFM(bdAddr, role, *msg.get(), data);
968             break;
969         case AVDT_GETCAP_CFM_EVT:
970             avdtEvent = ParseAvdtpGetCapabilityCFM(handle, bdAddr, role, *msg.get(), data);
971             break;
972         case AVDT_GET_ALLCAP_CFM_EVT:
973             avdtEvent = ParseAvdtpGetALLCapCFM(handle, bdAddr, role, *msg.get(), data);
974             break;
975         case AVDT_CONFIG_CFM_EVT:
976             avdtEvent = ParseAvdtpConfigureCFM(handle, bdAddr, role, *msg.get(), data);
977             break;
978         case AVDT_OPEN_CFM_EVT:
979             avdtEvent = ParseAvdtpOpenCFM(handle, bdAddr, role, *msg.get(), data);
980             break;
981         case AVDT_START_CFM_EVT:
982             avdtEvent = ParseAvdtpStartCFM(handle, bdAddr, role, *msg.get(), data);
983             break;
984         case AVDT_SUSPEND_CFM_EVT:
985             avdtEvent = ParseAvdtpSuspendCFM(handle, bdAddr, role, *msg.get(), data);
986             break;
987         case AVDT_CLOSE_CFM_EVT:
988             avdtEvent = ParseAvdtpStreamCloseCFM(handle, bdAddr, role, *msg.get(), data);
989             break;
990         case AVDT_GETCONFIG_CFM_EVT:
991             avdtEvent = ParseAvdtpGetConfigureCFM(handle, bdAddr, role, *msg.get(), data);
992             break;
993         case AVDT_RECONFIG_CFM_EVT:
994             avdtEvent = ParseAvdtpReconfigureCFM(handle, bdAddr, role, *msg.get(), data);
995             break;
996         case AVDT_ABORT_CFM_EVT:
997             avdtEvent = ParseAvdtpAbortCFM(handle, bdAddr, role, *msg.get(), data);
998             break;
999         case AVDT_WRITE_CFM_EVT:
1000             avdtEvent = ParseAvdtpWriteCFM(handle, bdAddr, role, *msg.get(), data);
1001             break;
1002         default:
1003             break;
1004     }
1005     msg->event = avdtEvent;
1006     if (avdtEvent == EVT_CONNECT_IND) {
1007         return nullptr;
1008     } else {
1009         return msg.release();
1010     }
1011 }
1012 }  // namespace bluetooth
1013 }  // namespace OHOS
1014