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 "hfp_ag_data_connection.h"
17
18 #include "btm.h"
19 #include "hfp_ag_profile_event_sender.h"
20 #include "log_util.h"
21 #include "raw_address.h"
22
23 namespace OHOS {
24 namespace bluetooth {
25 uint32_t HfpAgDataConnection::g_localFeatures {HFP_AG_FEATURES_DEFAULT};
26
DataConnectionCallback(uint16_t handle,uint32_t eventId,const void * eventData,void * context)27 void HfpAgDataConnection::DataConnectionCallback(uint16_t handle, uint32_t eventId,
28 const void *eventData, void *context)
29 {
30 LOG_INFO("[HFP AG]%{public}s():", __PRETTY_FUNCTION__);
31 if (HfpAgProfileEventSender::GetInstance().GetDispatchter() == nullptr) {
32 HILOGI("GetDispatchter() return nullptr");
33 return;
34 }
35 HfpAgProfileEventSender::GetInstance().GetDispatchter()->PostTask(
36 std::bind(&HfpAgDataConnection::ProcessDataConnectionCallback, handle, eventId));
37 return;
38 }
39
ProcessDataConnectionCallback(uint16_t handle,uint32_t eventId)40 void HfpAgDataConnection::ProcessDataConnectionCallback(uint16_t handle, uint32_t eventId)
41 {
42 auto addr = HfpAgRfcommConnection::GetRemoteAddressByHandle(handle);
43 HILOGI("Event from rfcomm device: %{public}s, handle: %{public}hu, eventId: %{public}u",
44 GetEncryptAddr(addr).c_str(), handle, eventId);
45 int event = HFP_AG_INVALID_EVT;
46 switch (eventId) {
47 case RFCOMM_CHANNEL_EV_CONNECT_SUCCESS:
48 event = HFP_AG_CONNECTED_EVT;
49 break;
50 case RFCOMM_CHANNEL_EV_CONNECT_FAIL:
51 event = HFP_AG_CONNECT_FAILED_EVT;
52 break;
53 case RFCOMM_CHANNEL_EV_DISCONNECTED:
54 case RFCOMM_CHANNEL_EV_DISCONNECT_SUCCESS:
55 HfpAgRfcommConnection::RemoveConnectionDevice(handle);
56 event = HFP_AG_DISCONNECTED_EVT;
57 break;
58 case RFCOMM_CHANNEL_EV_DISCONNECT_FAIL:
59 event = HFP_AG_DISCONNECT_FAILED_EVT;
60 break;
61 case RFCOMM_CHANNEL_EV_REV_DATA:
62 event = HFP_AG_DATA_AVAILABLE_EVT;
63 break;
64 default:
65 break;
66 }
67
68 HfpAgProfileEventSender::GetInstance().UpdateConnectState(addr, event);
69 return;
70 }
71
Init()72 int HfpAgDataConnection::Init()
73 {
74 g_localFeatures = HFP_AG_FEATURES_DEFAULT;
75 g_localSupportCodecs = HFP_AG_CODEC_NONE;
76 HfpAgRfcommConnection::Init();
77
78 BtmLocalSupportedCodecs *pCodecs = nullptr;
79 int ret = BTM_GetLocalSupportedCodecs(&pCodecs);
80 HFP_AG_RETURN_IF_FAIL(ret);
81
82 if ((pCodecs != nullptr) && (pCodecs->numberOfSupportedCodecs != 0)) {
83 for (size_t i = 0; i < pCodecs->numberOfSupportedCodecs; i++) {
84 if (pCodecs->supportedCodecs[i] == ANUM_MSBC) {
85 g_localFeatures |= HFP_AG_FEATURES_CODEC_NEGOTIATION;
86 g_localSupportCodecs |= HFP_AG_CODEC_MSBC;
87 LOG_INFO("[HFP AG]%{public}s(): Support MSBC", __FUNCTION__);
88 } else if (pCodecs->supportedCodecs[i] == ANUM_CVSD) {
89 g_localSupportCodecs |= HFP_AG_CODEC_CVSD;
90 LOG_INFO("[HFP AG]%{public}s(): Support CVSD", __FUNCTION__);
91 } else {
92 LOG_INFO("[HFP AG]%{public}s(): Support other codecId[%hhu]",
93 __FUNCTION__, pCodecs->supportedCodecs[i]);
94 }
95 }
96 } else {
97 LOG_INFO("[HFP AG]%{public}s(): Get local support codecs failed!", __FUNCTION__);
98 g_localSupportCodecs = HFP_AG_CODEC_DEFAULT;
99 }
100
101 if (BTM_IsControllerSupportEsco()) {
102 g_localFeatures |= HFP_AG_FEATURES_SUPPORT_ESCO;
103 LOG_INFO("[HFP AG]%{public}s(): Support ESCO", __FUNCTION__);
104 }
105 return ret;
106 }
107
CleanUp()108 void HfpAgDataConnection::CleanUp()
109 {
110 g_localFeatures = HFP_AG_FEATURES_DEFAULT;
111 HfpAgRfcommConnection::CleanUp();
112 }
113
GetLocalFeatures()114 uint32_t HfpAgDataConnection::GetLocalFeatures()
115 {
116 return g_localFeatures;
117 }
118
IsCodecNegotiationSupport() const119 bool HfpAgDataConnection::IsCodecNegotiationSupport() const
120 {
121 return ((g_localFeatures & HFP_AG_FEATURES_CODEC_NEGOTIATION) &&
122 (remoteFeatures_ & HFP_AG_HF_FEATURES_CODEC_NEGOTIATION));
123 }
124
IsEscoSupport()125 bool HfpAgDataConnection::IsEscoSupport()
126 {
127 return BTM_IsControllerSupportEsco();
128 }
129
IsEscoS4Support() const130 bool HfpAgDataConnection::IsEscoS4Support() const
131 {
132 return ((g_localFeatures & HFP_AG_FEATURES_SUPPORT_ESCO) && (remoteFeatures_ & HFP_AG_HF_FEATURES_ESCO));
133 }
134
Connect()135 int HfpAgDataConnection::Connect()
136 {
137 return rfcommConnection_.Connect();
138 }
139
Disconnect() const140 int HfpAgDataConnection::Disconnect() const
141 {
142 return rfcommConnection_.Disconnect();
143 }
144
ReadData(Packet ** pkt) const145 int HfpAgDataConnection::ReadData(Packet **pkt) const
146 {
147 return rfcommConnection_.ReadData(pkt);
148 }
149
WriteData(Packet & pkt) const150 int HfpAgDataConnection::WriteData(Packet &pkt) const
151 {
152 return rfcommConnection_.WriteData(pkt);
153 }
154
SetRemoteAddr(const std::string & addr)155 void HfpAgDataConnection::SetRemoteAddr(const std::string &addr)
156 {
157 remoteAddr_ = addr;
158 rfcommConnection_.SetRemoteAddr(addr);
159 }
160
SetRole(int role)161 void HfpAgDataConnection::SetRole(int role)
162 {
163 role_ = role;
164 }
165
GetRole() const166 int HfpAgDataConnection::GetRole() const
167 {
168 return role_;
169 }
170
SetConnectionHandle(uint16_t handle)171 void HfpAgDataConnection::SetConnectionHandle(uint16_t handle)
172 {
173 rfcommConnection_.SetConnectionHandle(handle);
174 }
175
GetConnectionHandle() const176 uint16_t HfpAgDataConnection::GetConnectionHandle() const
177 {
178 return rfcommConnection_.GetConnectionHandle();
179 }
180
SetSdpInfo(HfpAgRemoteSdpInfo sdpInfo)181 void HfpAgDataConnection::SetSdpInfo(HfpAgRemoteSdpInfo sdpInfo)
182 {
183 sdpInfo_ = sdpInfo;
184 rfcommConnection_.SetRemoteScn(sdpInfo.remoteServerChannelNumber);
185 }
186
SetSlcConnectState(bool state)187 void HfpAgDataConnection::SetSlcConnectState(bool state)
188 {
189 HILOGI("[HFP AG]:state = %{public}d", state);
190 slcConnected_ = state;
191 }
192
GetRemoteScn() const193 uint8_t HfpAgDataConnection::GetRemoteScn() const
194 {
195 return rfcommConnection_.GetRemoteScn();
196 }
197 } // namespace bluetooth
198 } // namespace OHOS