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