• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_rfcomm_connection.h"
17 
18 #include "btstack.h"
19 #include "hfp_hf_gap_client.h"
20 
21 namespace OHOS {
22 namespace bluetooth {
23 std::map<uint16_t, std::string> HfpHfRfcommConnection::g_devMap;
24 
Init()25 int HfpHfRfcommConnection::Init()
26 {
27     g_devMap.clear();
28     return BT_SUCCESS;
29 }
30 
CleanUp()31 void HfpHfRfcommConnection::CleanUp()
32 {
33     g_devMap.clear();
34 }
35 
Create()36 int HfpHfRfcommConnection::Create()
37 {
38     int ret = gap_->RegisterServiceSecurity(remoteAddr_, remoteScn_);
39     HFP_HF_RETURN_IF_FAIL(ret);
40 
41     RfcommConnectReqInfo info = {
42         remoteAddr_, remoteScn_, HFP_HF_COMMAND_MTU,
43         HFP_RFCOMM_CONNECTION_EVENTS, fn_, nullptr
44     };
45     ret = RFCOMM_ConnectChannel(&info, &connHandle_);
46 
47     HFP_HF_RETURN_IF_FAIL(ret);
48     return BT_SUCCESS;
49 }
50 
Connect()51 int HfpHfRfcommConnection::Connect()
52 {
53     int ret = Create();
54     HFP_HF_RETURN_IF_FAIL(ret);
55 
56     std::string addr = RawAddress::ConvertToString(remoteAddr_.addr).GetAddress();
57     HfpHfRfcommConnection::AddConnectionDevice(connHandle_, addr);
58 
59     return BT_SUCCESS;
60 }
61 
Disconnect() const62 int HfpHfRfcommConnection::Disconnect() const
63 {
64     int ret = RFCOMM_DisconnectChannel(connHandle_);
65     HFP_HF_RETURN_IF_FAIL(ret);
66 
67     ret = gap_->DeregisterServiceSecurity(remoteAddr_);
68     HFP_HF_RETURN_IF_FAIL(ret);
69 
70     return BT_SUCCESS;
71 }
72 
ReadData(Packet ** pkt) const73 int HfpHfRfcommConnection::ReadData(Packet **pkt) const
74 {
75     int ret = RFCOMM_Read(connHandle_, pkt);
76     if ((ret != RFCOMM_SUCCESS) && (ret != RFCOMM_NO_DATA)) {
77         LOG_ERROR("[HFP HF]%{public}s():ret[%{public}d]", __FUNCTION__, ret);
78     }
79     return ret;
80 }
81 
WriteData(Packet & pkt) const82 int HfpHfRfcommConnection::WriteData(Packet &pkt) const
83 {
84     int ret = RFCOMM_Write(connHandle_, &pkt);
85     HFP_HF_RETURN_IF_FAIL(ret);
86     return BT_SUCCESS;
87 }
88 
SetConnectionHandle(uint16_t handle)89 void HfpHfRfcommConnection::SetConnectionHandle(uint16_t handle)
90 {
91     connHandle_ = handle;
92 }
93 
GetConnectionHandle() const94 uint16_t HfpHfRfcommConnection::GetConnectionHandle() const
95 {
96     return connHandle_;
97 }
98 
SetRemoteAddr(const std::string & addr)99 void HfpHfRfcommConnection::SetRemoteAddr(const std::string &addr)
100 {
101     RawAddress rawAddr(addr);
102     rawAddr.ConvertToUint8(remoteAddr_.addr);
103     remoteAddr_.type = BT_PUBLIC_DEVICE_ADDRESS;
104 }
105 
SetRemoteScn(uint8_t scn)106 void HfpHfRfcommConnection::SetRemoteScn(uint8_t scn)
107 {
108     remoteScn_ = scn;
109 }
110 
GetRemoteScn() const111 uint8_t HfpHfRfcommConnection::GetRemoteScn() const
112 {
113     return remoteScn_;
114 }
115 
GetRemoteAddressByHandle(uint16_t handle)116 std::string HfpHfRfcommConnection::GetRemoteAddressByHandle(uint16_t handle)
117 {
118     auto it = g_devMap.find(handle);
119     if (it != g_devMap.end()) {
120         return g_devMap[handle];
121     } else {
122         return "";
123     }
124 }
125 
AddConnectionDevice(uint16_t handle,std::string addr)126 void HfpHfRfcommConnection::AddConnectionDevice(uint16_t handle, std::string addr)
127 {
128     g_devMap.insert(std::make_pair<uint16_t, std::string>(std::move(handle), std::move(addr)));
129 }
130 
RemoveConnectionDevice(uint16_t handle)131 void HfpHfRfcommConnection::RemoveConnectionDevice(uint16_t handle)
132 {
133     auto it = g_devMap.find(handle);
134     if (it != g_devMap.end()) {
135         g_devMap.erase(it);
136     }
137 }
138 }  // namespace bluetooth
139 }  // namespace OHOS