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 "socket_gap_server.h"
17 #include "log.h"
18 
19 namespace OHOS {
20 namespace bluetooth {
RegisterServiceSecurity(uint8_t scn,int securityFlag,GAP_Service serviceId)21 int SocketGapServer::RegisterServiceSecurity(
22     uint8_t scn, int securityFlag, GAP_Service serviceId)
23 {
24     LOG_INFO("[sock]%{public}s", __func__);
25 
26     uint16_t securityLevel = 0;
27     GapSecChannel secChan {scn};
28     GAP_ServiceConnectDirection connectDirection = INCOMING;
29     GapServiceSecurityInfo serviceInfo;
30     serviceInfo.channelId = secChan;
31     serviceInfo.direction = connectDirection;
32     serviceInfo.protocolId = SEC_PROTOCOL_RFCOMM;
33     serviceInfo.serviceId = serviceId;
34 
35     if (securityFlag != 0) {
36         securityLevel =
37             GAP_SEC_IN_ENCRYPTION | GAP_SEC_OUT_ENCRYPTION | GAP_SEC_IN_AUTHENTICATION | GAP_SEC_OUT_AUTHENTICATION;
38     }
39 
40     LOG_INFO("[sock]%{public}s serviceId:%{public}d securityLevel:%hu", __func__, serviceId, securityLevel);
41     return GAPIF_RegisterServiceSecurity(nullptr, &serviceInfo, securityLevel);
42 }
43 
UnregisterSecurity(const BtAddr addr,uint8_t scn,GAP_Service serviceId)44 int SocketGapServer::UnregisterSecurity(const BtAddr addr, uint8_t scn, GAP_Service serviceId)
45 {
46     LOG_INFO("[sock]%{public}s", __func__);
47     GAP_ServiceConnectDirection connectDirection;
48     GapSecChannel secChan {scn};
49     connectDirection = INCOMING;
50     GapServiceSecurityInfo serviceInfo;
51     serviceInfo.channelId = secChan;
52     serviceInfo.direction = connectDirection;
53     serviceInfo.protocolId = SEC_PROTOCOL_RFCOMM;
54     serviceInfo.serviceId = serviceId;
55 
56     LOG_INFO("[sock]%{public}s serviceId:%{public}d ", __func__, serviceId);
57 
58     return GAPIF_DeregisterServiceSecurity(&addr, &serviceInfo);
59 }
60 }  // namespace bluetooth
61 }  // namespace OHOS