• 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_data_connection_server.h"
17 
18 
19 #include "hfp_ag_defines.h"
20 #include "hfp_ag_profile_event_sender.h"
21 #include "hfp_ag_rfcomm_connection.h"
22 #include "log_util.h"
23 #include "raw_address.h"
24 
25 namespace OHOS {
26 namespace bluetooth {
GetInstance()27 HfpAgDataConnectionServer &HfpAgDataConnectionServer::GetInstance()
28 {
29     static HfpAgDataConnectionServer instance;
30     return instance;
31 }
32 
ProcessDataConnectionServerCallback(uint16_t handle,uint32_t eventId,const std::string & inComingAddr)33 void HfpAgDataConnectionServer::ProcessDataConnectionServerCallback(
34     uint16_t handle, uint32_t eventId, const std::string &inComingAddr)
35 {
36     auto addr = HfpAgRfcommConnection::GetRemoteAddressByHandle(handle);
37     HILOGI("Event from rfcomm device: %{public}s, handle: %{public}hu, eventId: %{public}u",
38         GetEncryptAddr(addr).c_str(), handle, eventId);
39     int event = HFP_AG_INVALID_EVT;
40     switch (eventId) {
41         case RFCOMM_CHANNEL_EV_CONNECT_INCOMING:
42             addr = inComingAddr;
43             HfpAgRfcommConnection::AddConnectionDevice(handle, inComingAddr);
44             event = HFP_AG_CONNECT_REQUEST_EVT;
45             break;
46         case RFCOMM_CHANNEL_EV_CONNECT_SUCCESS:
47             event = HFP_AG_CONNECTED_EVT;
48             break;
49         case RFCOMM_CHANNEL_EV_CONNECT_FAIL:
50             event = HFP_AG_CONNECT_FAILED_EVT;
51             break;
52         case RFCOMM_CHANNEL_EV_DISCONNECTED:
53         case RFCOMM_CHANNEL_EV_DISCONNECT_SUCCESS:
54             HfpAgRfcommConnection::RemoveConnectionDevice(handle);
55             event = HFP_AG_DISCONNECTED_EVT;
56             break;
57         case RFCOMM_CHANNEL_EV_DISCONNECT_FAIL:
58             event = HFP_AG_DISCONNECT_FAILED_EVT;
59             break;
60         case RFCOMM_CHANNEL_EV_REV_DATA:
61             event = HFP_AG_DATA_AVAILABLE_EVT;
62             break;
63         default:
64             LOG_INFO("[HFP AG]%{public}s():Invalid event from rfcomm eventId[%u]", __FUNCTION__, eventId);
65             break;
66     }
67 
68     HfpAgProfileEventSender::GetInstance().ConnectRequest(addr, handle, event);
69     return;
70 }
71 
DataConnectionServerCallback(uint16_t handle,uint32_t eventId,const void * eventData,void * context)72 void HfpAgDataConnectionServer::DataConnectionServerCallback(
73     uint16_t handle, uint32_t eventId, const void *eventData, void *context)
74 {
75     LOG_INFO("[HFP AG]%{public}s():", __PRETTY_FUNCTION__);
76     std::string addr;
77 
78     if (eventId == RFCOMM_CHANNEL_EV_CONNECT_INCOMING) {
79         auto evtData = const_cast<RfcommIncomingInfo *>(static_cast<const RfcommIncomingInfo *>(eventData));
80         addr = RawAddress::ConvertToString(evtData->addr.addr).GetAddress();
81     }
82     if (HfpAgProfileEventSender::GetInstance().GetDispatchter() == nullptr) {
83         HILOGI("GetDispatchter() return nullptr");
84         return;
85     }
86 
87     HfpAgProfileEventSender::GetInstance().GetDispatchter()->PostTask(
88         std::bind(&HfpAgDataConnectionServer::ProcessDataConnectionServerCallback, handle, eventId, addr));
89     return;
90 }
91 
RegisterServer() const92 int HfpAgDataConnectionServer::RegisterServer() const
93 {
94     return rfcommServer_.RegisterServer(&HfpAgDataConnectionServer::DataConnectionServerCallback);
95 }
96 
RemoveServer()97 int HfpAgDataConnectionServer::RemoveServer()
98 {
99     return rfcommServer_.RemoveServer();
100 }
101 
AcceptConnection(uint16_t handle) const102 int HfpAgDataConnectionServer::AcceptConnection(uint16_t handle) const
103 {
104     return rfcommServer_.AcceptConnection(handle);
105 }
106 
RejectConnection(uint16_t handle) const107 int HfpAgDataConnectionServer::RejectConnection(uint16_t handle) const
108 {
109     HfpAgRfcommConnection::RemoveConnectionDevice(handle);
110     return rfcommServer_.RejectConnection(handle);
111 }
112 
AssignLocalScn()113 uint8_t HfpAgDataConnectionServer::AssignLocalScn()
114 {
115     return rfcommServer_.AssignLocalScn();
116 }
117 
GetLocalScn() const118 uint8_t HfpAgDataConnectionServer::GetLocalScn() const
119 {
120     return rfcommServer_.GetLocalScn();
121 }
122 }  // namespace bluetooth
123 }  // namespace OHOS