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_server.h"
17
18 #include "hfp_hf_defines.h"
19 #include "hfp_hf_profile_event_sender.h"
20 #include "hfp_hf_rfcomm_connection.h"
21 #include "log_util.h"
22 #include "raw_address.h"
23
24 namespace OHOS {
25 namespace bluetooth {
GetInstance()26 HfpHfDataConnectionServer &HfpHfDataConnectionServer::GetInstance()
27 {
28 static HfpHfDataConnectionServer instance;
29 return instance;
30 }
31
ProcessDataConnectionServerCallback(uint16_t handle,uint32_t eventId,const std::string & inComingAddr)32 void HfpHfDataConnectionServer::ProcessDataConnectionServerCallback(
33 uint16_t handle, uint32_t eventId, const std::string &inComingAddr)
34 {
35 auto addr = HfpHfRfcommConnection::GetRemoteAddressByHandle(handle);
36 HILOGI("Event from rfcomm device: %{public}s, handle: %{public}hu, eventId:%{public}u",
37 GetEncryptAddr(addr).c_str(), handle, eventId);
38 int event = HFP_HF_INVALID_EVT;
39 switch (eventId) {
40 case RFCOMM_CHANNEL_EV_CONNECT_INCOMING:
41 addr = inComingAddr;
42 HfpHfRfcommConnection::AddConnectionDevice(handle, inComingAddr);
43 event = HFP_HF_CONNECT_REQUEST_EVT;
44 break;
45 case RFCOMM_CHANNEL_EV_CONNECT_SUCCESS:
46 event = HFP_HF_CONNECTED_EVT;
47 break;
48 case RFCOMM_CHANNEL_EV_CONNECT_FAIL:
49 event = HFP_HF_CONNECT_FAILED_EVT;
50 break;
51 case RFCOMM_CHANNEL_EV_DISCONNECTED:
52 case RFCOMM_CHANNEL_EV_DISCONNECT_SUCCESS:
53 HfpHfRfcommConnection::RemoveConnectionDevice(handle);
54 event = HFP_HF_DISCONNECTED_EVT;
55 break;
56 case RFCOMM_CHANNEL_EV_DISCONNECT_FAIL:
57 event = HFP_HF_DISCONNECT_FAILED_EVT;
58 break;
59 case RFCOMM_CHANNEL_EV_REV_DATA:
60 event = HFP_HF_DATA_AVAILABLE_EVT;
61 break;
62 default:
63 LOG_DEBUG("[HFP HF]%{public}s():Invalid event from rfcomm eventId[%u]", __FUNCTION__, eventId);
64 break;
65 }
66
67 HfpHfProfileEventSender::GetInstance().ConnectRequest(addr, handle, event);
68 return;
69 }
70
DataConnectionServerCallback(uint16_t handle,uint32_t eventId,const void * eventData,void * context)71 void HfpHfDataConnectionServer::DataConnectionServerCallback(
72 uint16_t handle, uint32_t eventId, const void *eventData, void *context)
73 {
74 LOG_DEBUG("[HFP HF]%{public}s():", __PRETTY_FUNCTION__);
75 std::string addr;
76
77 if (eventId == RFCOMM_CHANNEL_EV_CONNECT_INCOMING) {
78 auto evtData = const_cast<RfcommIncomingInfo *>(static_cast<const RfcommIncomingInfo *>(eventData));
79 addr = RawAddress::ConvertToString(evtData->addr.addr).GetAddress();
80 }
81
82 HfpHfProfileEventSender::GetInstance().GetDispatchter()->PostTask(
83 std::bind(&HfpHfDataConnectionServer::ProcessDataConnectionServerCallback, handle, eventId, addr));
84 return;
85 }
86
RegisterServer() const87 int HfpHfDataConnectionServer::RegisterServer() const
88 {
89 return rfcommServer_.RegisterServer(&HfpHfDataConnectionServer::DataConnectionServerCallback);
90 }
91
RemoveServer()92 int HfpHfDataConnectionServer::RemoveServer()
93 {
94 return rfcommServer_.RemoveServer();
95 }
96
AcceptConnection(uint16_t handle) const97 int HfpHfDataConnectionServer::AcceptConnection(uint16_t handle) const
98 {
99 return rfcommServer_.AcceptConnection(handle);
100 }
101
RejectConnection(uint16_t handle) const102 int HfpHfDataConnectionServer::RejectConnection(uint16_t handle) const
103 {
104 HfpHfRfcommConnection::RemoveConnectionDevice(handle);
105 return rfcommServer_.RejectConnection(handle);
106 }
107
AssignLocalScn()108 uint8_t HfpHfDataConnectionServer::AssignLocalScn()
109 {
110 return rfcommServer_.AssignLocalScn();
111 }
112
GetLocalScn() const113 uint8_t HfpHfDataConnectionServer::GetLocalScn() const
114 {
115 return rfcommServer_.GetLocalScn();
116 }
117 } // namespace bluetooth
118 } // namespace OHOS