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_sdp_server.h"
17 #include "log.h"
18 #include "socket_def.h"
19 
20 namespace OHOS {
21 namespace bluetooth {
RegisterSdpService(const std::string & name,const Uuid & uuid,uint8_t scn)22 int SocketSdpServer::RegisterSdpService(const std::string &name, const Uuid &uuid, uint8_t scn)
23 {
24     LOG_INFO("[sock]%{public}s", __func__);
25 
26     sdpHandle_ = SDP_CreateServiceRecord();
27 
28     int ret = AddServiceClassId(uuid);
29     if (ret != BT_SUCCESS) {
30         LOG_ERROR("[sock]%{public}s AddServiceClassId error", __FUNCTION__);
31         return ret;
32     }
33 
34     LOG_INFO("[sock]%{public}s AddProtocol scn:%hhu", __func__, scn);
35     ret = AddProtocol(scn);
36     if (ret != BT_SUCCESS) {
37         LOG_ERROR("[sock]%{public}s AddProtocol error", __FUNCTION__);
38         return ret;
39     }
40 
41     ret = AddProfile();
42     if (ret != BT_SUCCESS) {
43         LOG_ERROR("[sock]%{public}s AddProfile error", __FUNCTION__);
44         return ret;
45     }
46 
47     ret = AddServiceName(name);
48     if (ret != BT_SUCCESS) {
49         LOG_ERROR("[sock]%{public}s AddServiceName error", __FUNCTION__);
50         return ret;
51     }
52 
53     ret = AddBrowseGroupList();
54     if (ret != BT_SUCCESS) {
55         LOG_ERROR("[sock]%{public}s AddBrowseGroupList error", __FUNCTION__);
56         return ret;
57     }
58 
59     ret = SDP_RegisterServiceRecord(sdpHandle_);
60     if (ret != BT_SUCCESS) {
61         LOG_ERROR("[sock]%{public}s SDP_RegisterServiceRecord error", __FUNCTION__);
62         return ret;
63     }
64 
65     return ret;
66 }
67 
UnregisterSdpService()68 int SocketSdpServer::UnregisterSdpService()
69 {
70     LOG_INFO("[sock]%{public}s", __func__);
71 
72     int ret = SDP_DeregisterServiceRecord(sdpHandle_);
73     if (ret != BT_SUCCESS) {
74         LOG_ERROR("[sock]%{public}s SDP_DeregisterServiceRecord error", __FUNCTION__);
75         return ret;
76     }
77 
78     ret = SDP_DestroyServiceRecord(sdpHandle_);
79     if (ret != BT_SUCCESS) {
80         LOG_ERROR("[sock]%{public}s SDP_DestroyServiceRecord error", __FUNCTION__);
81         return ret;
82     }
83 
84     return ret;
85 }
86 
AddServiceClassId(const Uuid & uuid)87 int SocketSdpServer::AddServiceClassId(const Uuid &uuid)
88 {
89     BtUuid classid[1];
90     classid[0].type = BT_UUID_128;
91     uuid.ConvertToBytesLE(classid[0].uuid128);
92 
93     return SDP_AddServiceClassIdList(sdpHandle_, classid, SPP_CLASSID_NUM);
94 }
95 
AddProtocol(uint8_t scn)96 int SocketSdpServer::AddProtocol(uint8_t scn)
97 {
98     SdpProtocolDescriptor protocol[SPP_PRRTOCOL_NUM];
99     protocol[0].protocolUuid.type = BT_UUID_16;
100     protocol[0].protocolUuid.uuid16 = UUID_PROTOCOL_L2CAP;
101     protocol[0].parameterNumber = 0;
102     protocol[1].protocolUuid.type = BT_UUID_16;
103     protocol[1].protocolUuid.uuid16 = UUID_PROTOCOL_RFCOMM;
104     protocol[1].parameterNumber = 1;
105     protocol[1].parameter[0].type = SDP_TYPE_UINT_8;
106     protocol[1].parameter[0].value = scn;
107 
108     return SDP_AddProtocolDescriptorList(sdpHandle_, protocol, SPP_PRRTOCOL_NUM);
109 }
110 
AddProfile()111 int SocketSdpServer::AddProfile()
112 {
113     SdpProfileDescriptor profile[SPP_PROFILE_NUM];
114     profile[0].profileUuid.type = BT_UUID_16;
115     profile[0].profileUuid.uuid16 = UUID_SERVCLASS_SERIAL_PORT;
116     profile[0].versionNumber = SPP_PROFILE_VERSION;
117 
118     return SDP_AddBluetoothProfileDescriptorList(sdpHandle_, profile, SPP_PROFILE_NUM);
119 }
120 
AddServiceName(const std::string & name)121 int SocketSdpServer::AddServiceName(const std::string &name)
122 {
123     return SDP_AddServiceName(sdpHandle_, SDP_ATTRIBUTE_PRIMARY_LANGUAGE_BASE, name.c_str(), name.length());
124 }
125 
AddBrowseGroupList()126 int SocketSdpServer::AddBrowseGroupList()
127 {
128     BtUuid browseGroupList[SPP_BROWSE_LIST_NUM];
129     browseGroupList[0].type = BT_UUID_16;
130     browseGroupList[0].uuid16 = SDP_PUBLIC_BROWSE_GROUP_ROOT_UUID;
131 
132     return SDP_AddBrowseGroupList(sdpHandle_, browseGroupList, SPP_BROWSE_LIST_NUM);
133 }
134 }  // namespace bluetooth
135 }  // namespace OHOS