1 /*
2 * Copyright (C) 2022 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 "opp_gap_client.h"
17 #include "btstack.h"
18 #include "log.h"
19 #include "opp_defines.h"
20 #include "opp_message.h"
21 #include "opp_service.h"
22 #include "securec.h"
23
24 namespace OHOS {
25 namespace bluetooth {
OppGapClient(const std::string & address,GapSecChannel channel,bool rfcommOrPsm)26 OppGapClient::OppGapClient(
27 const std::string &address, GapSecChannel channel, bool rfcommOrPsm)
28 : address_(address), gapSecChannel_(channel), l2capOrRfcomm_(rfcommOrPsm)
29 {}
30
~OppGapClient()31 OppGapClient::~OppGapClient()
32 {
33 Deregister();
34 }
35
Register()36 int OppGapClient::Register()
37 {
38 HILOGI("[OPP GAP CLIENT]start");
39
40 BtAddr btAddr;
41 btAddr.type = BT_PUBLIC_DEVICE_ADDRESS;
42 RawAddress rawAddr(address_);
43 rawAddr.ConvertToUint8(btAddr.addr, sizeof(btAddr.addr));
44
45 GapServiceSecurityInfo securityInfo;
46 securityInfo.direction = OUTGOING;
47 securityInfo.serviceId = GAP_Service::OPP_CLIENT;
48 securityInfo.channelId.l2capPsm = 0;
49 securityInfo.protocolId = GAP_SecMultiplexingProtocol::SEC_PROTOCOL_RFCOMM;
50 if (l2capOrRfcomm_) {
51 securityInfo.protocolId = GAP_SecMultiplexingProtocol::SEC_PROTOCOL_L2CAP;
52 securityInfo.channelId.l2capPsm = gapSecChannel_.l2capPsm;
53 HILOGI("[OPP GAP CLIENT]Register L2cap psm 0x%04X", gapSecChannel_.l2capPsm);
54 } else {
55 securityInfo.channelId.rfcommChannel = gapSecChannel_.rfcommChannel;
56 HILOGI("[OPP GAP CLIENT]Register Rfcomm psm 0x%02X", gapSecChannel_.rfcommChannel);
57 }
58 registered_ = true;
59 int ret = GAPIF_RegisterServiceSecurity(&btAddr,
60 &securityInfo,
61 GAP_SEC_IN_AUTHENTICATION | GAP_SEC_IN_ENCRYPTION | GAP_SEC_OUT_AUTHENTICATION | GAP_SEC_OUT_ENCRYPTION);
62 HILOGI("[OPP GAP CLIENT]end, GAPIF_RegisterServiceSecurity=%{public}d", ret);
63 return ret;
64 }
65
Deregister()66 int OppGapClient::Deregister()
67 {
68 HILOGI("[OPP GAP CLIENT]start");
69
70 if (!registered_) {
71 return BT_SUCCESS;
72 }
73
74 BtAddr btAddr;
75 (void)memset_s(&btAddr, sizeof(btAddr), 0, sizeof(btAddr));
76 RawAddress rawAddr(address_);
77 rawAddr.ConvertToUint8(btAddr.addr, sizeof(btAddr.addr));
78
79 GapServiceSecurityInfo securityInfo;
80 (void)memset_s(&securityInfo, sizeof(securityInfo), 0, sizeof(securityInfo));
81 securityInfo.direction = OUTGOING;
82 securityInfo.serviceId = GAP_Service::OPP_CLIENT;
83
84 if (l2capOrRfcomm_) {
85 securityInfo.protocolId = GAP_SecMultiplexingProtocol::SEC_PROTOCOL_L2CAP;
86 } else {
87 securityInfo.protocolId = GAP_SecMultiplexingProtocol::SEC_PROTOCOL_RFCOMM;
88 }
89 securityInfo.channelId = gapSecChannel_;
90 registered_ = false;
91
92 HILOGI("[OPP GAP CLIENT]end");
93 return GAPIF_DeregisterServiceSecurity(&btAddr, &securityInfo);
94 }
95
RequestSecurity()96 int OppGapClient::RequestSecurity()
97 {
98 HILOGI("[OPP GAP CLIENT]start");
99 if (!l2capOrRfcomm_) {
100 OppMessage event(OPP_GAP_CMPL_EVT, OPP_GAP_SUCCESS);
101 event.dev_ = address_;
102 OppService::GetService()->PostEvent(event);
103 HILOGI("[OPP GAP CLIENT]end, rfcomm");
104 return BT_SUCCESS;
105 }
106
107 BtAddr btAddr;
108 (void)memset_s(&btAddr, sizeof(btAddr), 0, sizeof(btAddr));
109 RawAddress rawAddr(address_);
110 rawAddr.ConvertToUint8(btAddr.addr, sizeof(btAddr.addr));
111
112 GapRequestSecurityParam requestParam;
113 (void)memset_s(&requestParam, sizeof(requestParam), 0, sizeof(requestParam));
114 GapServiceSecurityInfo &securityInfo = requestParam.info;
115 (void)memset_s(&securityInfo, sizeof(securityInfo), 0, sizeof(securityInfo));
116 securityInfo.direction = OUTGOING;
117 securityInfo.serviceId = GAP_Service::OPP_CLIENT;
118 securityInfo.protocolId = GAP_SecMultiplexingProtocol::SEC_PROTOCOL_L2CAP;
119 securityInfo.channelId = gapSecChannel_;
120 requestParam.callback = &OppGapClient::RequestSecurityCallback;
121 requestParam.context = this;
122 int ret = GAPIF_RequestSecurity(&btAddr, &requestParam);
123 HILOGI("[OPP GAP CLIENT]end, GAPIF_RequestSecurity=%{public}d", ret);
124 return ret;
125 }
126
RequestSecurityCallback(uint16_t result) const127 void OppGapClient::RequestSecurityCallback(uint16_t result) const
128 {
129 // RequestSecurity callback
130 HILOGI("[OPP GAP CLIENT]start");
131 if (result == GAP_STATUS_SUCCESS) {
132 OppMessage event(OPP_GAP_CMPL_EVT, OPP_GAP_SUCCESS);
133 event.dev_ = address_;
134 OppService::GetService()->PostEvent(event);
135 } else {
136 OppMessage event(OPP_GAP_CMPL_EVT, OPP_GAP_FAILED);
137 event.dev_ = address_;
138 OppService::GetService()->PostEvent(event);
139 HILOGE("[OPP GAP CLIENT]gap request security failed.");
140 }
141 HILOGI("[OPP GAP CLIENT]end");
142 }
143
RequestSecurityCallback(uint16_t result,GapServiceSecurityInfo security,void * context)144 void OppGapClient::RequestSecurityCallback(uint16_t result, GapServiceSecurityInfo security, void *context)
145 {
146 HILOGI("[OPP GAP CLIENT]start, result = %{public}d", int(result));
147 auto pThis = static_cast<OppGapClient *>(context);
148 if (pThis != nullptr) {
149 pThis->RequestSecurityCallback(result);
150 }
151 HILOGI("[OPP GAP CLIENT]end");
152 }
153 } // namespace bluetooth
154 } // namespace OHOS