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_server.h"
17 #include "gap_if.h"
18 #include "log.h"
19
20 namespace OHOS {
21 namespace bluetooth {
OppGapServer(uint8_t rfcommScn,uint16_t l2capPsm)22 OppGapServer::OppGapServer(uint8_t rfcommScn, uint16_t l2capPsm) : rfcommScn_(rfcommScn), l2capPsm_(l2capPsm)
23 {}
24
Register() const25 int OppGapServer::Register() const
26 {
27 HILOGI("[OPP GAP SERVER]Call");
28 int retVal = BT_SUCCESS;
29 GapSecChannel secChannel;
30 secChannel.l2capPsm = l2capPsm_;
31 secChannel.rfcommChannel = rfcommScn_;
32 // register for l2cap
33 GapServiceSecurityInfo securityInfoL2cap;
34 securityInfoL2cap.direction = INCOMING;
35 securityInfoL2cap.serviceId = GAP_Service::OPP_SERVER;
36 securityInfoL2cap.protocolId = GAP_SecMultiplexingProtocol::SEC_PROTOCOL_L2CAP;
37 securityInfoL2cap.channelId = secChannel;
38 retVal = GAPIF_RegisterServiceSecurity(nullptr,
39 &securityInfoL2cap,
40 GAP_SEC_IN_AUTHENTICATION | GAP_SEC_IN_ENCRYPTION | GAP_SEC_OUT_AUTHENTICATION | GAP_SEC_OUT_ENCRYPTION);
41 if (retVal != BT_SUCCESS) {
42 HILOGE("[OPP GAP SERVER]Gap GAPIF_RegisterServiceSecurity retVal = %{public}d", retVal);
43 return retVal;
44 }
45 // register for rfcomm
46 GapServiceSecurityInfo securityInfoRfcomm;
47 securityInfoRfcomm.direction = INCOMING;
48 securityInfoRfcomm.serviceId = GAP_Service::OPP_SERVER;
49 securityInfoRfcomm.protocolId = GAP_SecMultiplexingProtocol::SEC_PROTOCOL_RFCOMM;
50 securityInfoRfcomm.channelId = secChannel;
51 retVal = GAPIF_RegisterServiceSecurity(nullptr,
52 &securityInfoRfcomm,
53 GAP_SEC_IN_AUTHENTICATION | GAP_SEC_IN_ENCRYPTION | GAP_SEC_OUT_AUTHENTICATION | GAP_SEC_OUT_ENCRYPTION);
54 if (retVal != BT_SUCCESS) {
55 HILOGE("[OPP GAP SERVER]Gap GAPIF_RegisterServiceSecurity retVal = %{public}d", retVal);
56 return retVal;
57 }
58 return retVal;
59 }
60
Deregister() const61 void OppGapServer::Deregister() const
62 {
63 HILOGI("[OPP GAP SERVER]Call");
64 GapSecChannel secChannel;
65 secChannel.l2capPsm = l2capPsm_;
66 secChannel.rfcommChannel = rfcommScn_;
67 int retVal = BT_SUCCESS;
68 GapServiceSecurityInfo securityInfoL2cap;
69 securityInfoL2cap.direction = INCOMING;
70 securityInfoL2cap.serviceId = GAP_Service::OPP_SERVER;
71 securityInfoL2cap.protocolId = GAP_SecMultiplexingProtocol::SEC_PROTOCOL_L2CAP;
72 securityInfoL2cap.channelId = secChannel;
73 retVal = GAPIF_DeregisterServiceSecurity(nullptr, &securityInfoL2cap);
74 if (retVal != BT_SUCCESS) {
75 HILOGE("[OPP GAP SERVER]Gap GAPIF_DeregisterServiceSecurity retVal = %{public}d", retVal);
76 }
77
78 GapServiceSecurityInfo securityInfoRfcomm;
79 securityInfoRfcomm.direction = INCOMING;
80 securityInfoRfcomm.serviceId = GAP_Service::OPP_SERVER;
81 securityInfoRfcomm.protocolId = GAP_SecMultiplexingProtocol::SEC_PROTOCOL_RFCOMM;
82 securityInfoRfcomm.channelId = secChannel;
83 retVal = GAPIF_DeregisterServiceSecurity(nullptr, &securityInfoRfcomm);
84 if (retVal != BT_SUCCESS) {
85 HILOGE("[OPP GAP SERVER]Gap GAPIF_DeregisterServiceSecurity retVal = %{public}d", retVal);
86 }
87 }
88 } // namespace bluetooth
89 } // namespace OHOS