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_hf_data_connection.h"
17
18 #include "btm.h"
19 #include "hfp_hf_profile_event_sender.h"
20 #include "log_util.h"
21 #include "raw_address.h"
22
23 namespace OHOS {
24 namespace bluetooth {
25 uint32_t HfpHfDataConnection::g_localFeatures {HFP_HF_FEATURES_DEFAULT};
26
ProcessDataConnectionCallback(uint16_t handle,uint32_t eventId)27 void HfpHfDataConnection::ProcessDataConnectionCallback(uint16_t handle, uint32_t eventId)
28 {
29 auto addr = HfpHfRfcommConnection::GetRemoteAddressByHandle(handle);
30 HILOGI("Event from rfcomm device: %{public}s, handle: %{public}hu, eventId: %{public}u",
31 GetEncryptAddr(addr).c_str(), handle, eventId);
32 int event = HFP_HF_INVALID_EVT;
33 switch (eventId) {
34 case RFCOMM_CHANNEL_EV_CONNECT_SUCCESS:
35 event = HFP_HF_CONNECTED_EVT;
36 break;
37 case RFCOMM_CHANNEL_EV_CONNECT_FAIL:
38 event = HFP_HF_CONNECT_FAILED_EVT;
39 break;
40 case RFCOMM_CHANNEL_EV_DISCONNECTED:
41 case RFCOMM_CHANNEL_EV_DISCONNECT_SUCCESS:
42 HfpHfRfcommConnection::RemoveConnectionDevice(handle);
43 event = HFP_HF_DISCONNECTED_EVT;
44 break;
45 case RFCOMM_CHANNEL_EV_DISCONNECT_FAIL:
46 event = HFP_HF_DISCONNECT_FAILED_EVT;
47 break;
48 case RFCOMM_CHANNEL_EV_REV_DATA:
49 event = HFP_HF_DATA_AVAILABLE_EVT;
50 break;
51 default:
52 break;
53 }
54
55 HfpHfProfileEventSender::GetInstance().UpdateConnectState(addr, event);
56 return;
57 }
58
DataConnectionCallback(uint16_t handle,uint32_t eventId,const void * eventData,void * context)59 void HfpHfDataConnection::DataConnectionCallback(uint16_t handle, uint32_t eventId,
60 const void *eventData, void *context)
61 {
62 LOG_DEBUG("[HFP HF]%{public}s():", __PRETTY_FUNCTION__);
63 HfpHfProfileEventSender::GetInstance().GetDispatchter()->PostTask(
64 std::bind(&HfpHfDataConnection::ProcessDataConnectionCallback, handle, eventId));
65
66 return;
67 }
68
Init()69 int HfpHfDataConnection::Init()
70 {
71 g_localSupportCodecs = HFP_HF_CODEC_NONE;
72 g_localFeatures = HFP_HF_FEATURES_DEFAULT;
73 HfpHfRfcommConnection::Init();
74
75 BtmLocalSupportedCodecs *pCodecs = nullptr;
76 int ret = BTM_GetLocalSupportedCodecs(&pCodecs);
77 HFP_HF_RETURN_IF_FAIL(ret);
78
79 if (pCodecs != nullptr) {
80 for (size_t i = 0; i < pCodecs->numberOfSupportedCodecs; i++) {
81 if (pCodecs->supportedCodecs[i] == ANUM_MSBC) {
82 g_localFeatures |= HFP_HF_FEATURES_CODEC_NEGOTIATION;
83 g_localSupportCodecs |= HFP_HF_CODEC_MSBC;
84 LOG_DEBUG("[HFP HF]%{public}s(): Support MSBC", __FUNCTION__);
85 } else if (pCodecs->supportedCodecs[i] == ANUM_CVSD) {
86 g_localSupportCodecs |= HFP_HF_CODEC_CVSD;
87 LOG_DEBUG("[HFP HF]%{public}s(): Support CVSD", __FUNCTION__);
88 } else {
89 LOG_DEBUG("[HFP HF]%{public}s(): Support other codecId[%hhu]",
90 __FUNCTION__, pCodecs->supportedCodecs[i]);
91 }
92 }
93 } else {
94 LOG_DEBUG("[HFP HF]%{public}s(): Get local support codecs failed!", __FUNCTION__);
95 }
96
97 if (BTM_IsControllerSupportEsco()) {
98 g_localFeatures |= HFP_HF_FEATURES_ESCO;
99 LOG_DEBUG("[HFP HF]%{public}s(): Support ESCO", __FUNCTION__);
100 }
101 return ret;
102 }
103
CleanUp()104 void HfpHfDataConnection::CleanUp()
105 {
106 HfpHfRfcommConnection::CleanUp();
107 g_localFeatures = HFP_HF_FEATURES_DEFAULT;
108 }
109
GetLocalFeatures()110 uint32_t HfpHfDataConnection::GetLocalFeatures()
111 {
112 return g_localFeatures;
113 }
114
Connect()115 int HfpHfDataConnection::Connect()
116 {
117 return rfcommConnection_.Connect();
118 }
119
Disconnect() const120 int HfpHfDataConnection::Disconnect() const
121 {
122 return rfcommConnection_.Disconnect();
123 }
124
ReadData(Packet ** pkt) const125 int HfpHfDataConnection::ReadData(Packet **pkt) const
126 {
127 return rfcommConnection_.ReadData(pkt);
128 }
129
WriteData(Packet & pkt) const130 int HfpHfDataConnection::WriteData(Packet &pkt) const
131 {
132 return rfcommConnection_.WriteData(pkt);
133 }
134
SetRemoteAddr(const std::string & addr)135 void HfpHfDataConnection::SetRemoteAddr(const std::string &addr)
136 {
137 remoteAddr_ = addr;
138 rfcommConnection_.SetRemoteAddr(addr);
139 }
140
SetRole(int role)141 void HfpHfDataConnection::SetRole(int role)
142 {
143 role_ = role;
144 }
145
GetRole() const146 int HfpHfDataConnection::GetRole() const
147 {
148 return role_;
149 }
150
SetConnectionHandle(uint16_t handle)151 void HfpHfDataConnection::SetConnectionHandle(uint16_t handle)
152 {
153 rfcommConnection_.SetConnectionHandle(handle);
154 }
155
GetConnectionHandle() const156 uint16_t HfpHfDataConnection::GetConnectionHandle() const
157 {
158 return rfcommConnection_.GetConnectionHandle();
159 }
160
SetSdpInfo(HfpHfRemoteSdpInfo sdpInfo)161 void HfpHfDataConnection::SetSdpInfo(HfpHfRemoteSdpInfo sdpInfo)
162 {
163 sdpInfo_ = sdpInfo;
164 rfcommConnection_.SetRemoteScn(sdpInfo.remoteServerChannelNumber);
165 }
166
SetSlcConnectState(bool state)167 void HfpHfDataConnection::SetSlcConnectState(bool state)
168 {
169 slcConnected_ = state;
170 }
171
IsCodecNegotiationSupport() const172 bool HfpHfDataConnection::IsCodecNegotiationSupport() const
173 {
174 return ((g_localFeatures & HFP_HF_FEATURES_CODEC_NEGOTIATION) &&
175 (remoteFeatures_ & HFP_HF_AG_FEATURES_CODEC_NEGOTIATION));
176 }
177
IsEscoSupport()178 bool HfpHfDataConnection::IsEscoSupport()
179 {
180 return BTM_IsControllerSupportEsco();
181 }
182
IsEscoS4Support() const183 bool HfpHfDataConnection::IsEscoS4Support() const
184 {
185 return ((g_localFeatures & HFP_HF_FEATURES_ESCO) && (remoteFeatures_ & HFP_HF_AG_FEATURES_SUPPORT_ESCO));
186 }
187
GetRemoteScn() const188 uint8_t HfpHfDataConnection::GetRemoteScn() const
189 {
190 return rfcommConnection_.GetRemoteScn();
191 }
192 } // namespace bluetooth
193 } // namespace OHOS