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