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 "opp_sdp_server.h"
17
18 #include "log.h"
19 #include "opp_defines.h"
20 #include "sdp.h"
21
22 namespace OHOS {
23 namespace bluetooth {
Register(uint8_t rfcommScn,uint16_t l2capPsm)24 int OppSdpServer::Register(uint8_t rfcommScn, uint16_t l2capPsm)
25 {
26 HILOGI("[OPP SDP SERVER]:enter");
27 int result = BT_SUCCESS;
28
29 sdpHandle_ = SDP_CreateServiceRecord();
30
31 result = AddServiceClassIdList();
32 if (result != BT_SUCCESS) {
33 HILOGE("[OPP SDP SERVER] Sdp AddServiceClassIdList result = %{public}d", result);
34 return result;
35 }
36
37 result = AddProtocolDescriptorList(rfcommScn);
38 if (result != BT_SUCCESS) {
39 HILOGE("[OPP SDP SERVER] Sdp AddProtocolDescriptorList result = %{public}d", result);
40 return result;
41 }
42
43 result = AddBrowseGroupList();
44 if (result != BT_SUCCESS) {
45 HILOGE("[OPP SDP SERVER] Sdp AddBrowseGroupList result = %{public}d", result);
46 return result;
47 }
48
49 result = AddBluetoothProfileDescriptorList();
50 if (result != BT_SUCCESS) {
51 HILOGE("[OPP SDP SERVER] Sdp AddBluetoothProfileDescriptorList result = %{public}d", result);
52 return result;
53 }
54
55 result = AddServiceName();
56 if (result != BT_SUCCESS) {
57 HILOGE("[OPP SDP SERVER] Sdp AddServiceName result = %{public}d", result);
58 return result;
59 }
60
61 result = AddL2capPsm(l2capPsm);
62 if (result != BT_SUCCESS) {
63 HILOGE("[OPP SDP SERVER] Sdp AddL2capPsm result = %{public}d", result);
64 return result;
65 }
66
67 result = AddSupportedFormatsList();
68 if (result != BT_SUCCESS) {
69 HILOGE("[OPP SDP SERVER] Sdp AddSupportedFormatsList result = %{public}d", result);
70 return result;
71 }
72
73 result = SDP_RegisterServiceRecord(sdpHandle_);
74 if (result != BT_SUCCESS) {
75 HILOGE("[OPP SDP SERVER] RegisterServiceRecord result = %{public}d", result);
76 return result;
77 }
78
79 return result;
80 }
81
Deregister()82 void OppSdpServer::Deregister()
83 {
84 HILOGI("[OPP SDP SERVER] Call");
85 int result = BT_SUCCESS;
86
87 result = SDP_DeregisterServiceRecord(sdpHandle_);
88 if (result != BT_SUCCESS) {
89 HILOGE("[PAN SDP] SDP_DeregisterServiceRecord result = %{public}d", result);
90 return;
91 }
92
93 result = SDP_DestroyServiceRecord(sdpHandle_);
94 if (result != BT_SUCCESS) {
95 HILOGE("[OPP SDP SERVER] SDP_DestroyServiceRecord result = %{public}d", result);
96 }
97 sdpHandle_ = 0;
98 }
99
AddServiceClassIdList() const100 int OppSdpServer::AddServiceClassIdList() const
101 {
102 HILOGI("[OPP SDP SERVER] Call");
103 BtUuid classId[1];
104 classId[0].type = BT_UUID_16;
105 classId[0].uuid16 = OPP_UUID16;
106
107 // ServiceClassID
108 return SDP_AddServiceClassIdList(sdpHandle_, classId, 1);
109 }
110
AddProtocolDescriptorList(uint8_t rfcommScn) const111 int OppSdpServer::AddProtocolDescriptorList(uint8_t rfcommScn) const
112 {
113 HILOGI("[OPP SDP SERVER] Call");
114 SdpProtocolDescriptor protocol[OPP_PROTOCOL_DESCRIPTOR_NUMBER];
115 uint8_t index = 0;
116 protocol[index].protocolUuid.type = BT_UUID_16;
117 protocol[index].protocolUuid.uuid16 = UUID_PROTOCOL_L2CAP;
118 protocol[index].parameterNumber = 0;
119 index++;
120 protocol[index].protocolUuid.type = BT_UUID_16;
121 protocol[index].protocolUuid.uuid16 = UUID_PROTOCOL_RFCOMM;
122 protocol[index].parameterNumber = 1;
123 protocol[index].parameter[0].type = SDP_TYPE_UINT_8;
124 protocol[index].parameter[0].value = rfcommScn;
125 index++;
126 protocol[index].protocolUuid.type = BT_UUID_16;
127 protocol[index].protocolUuid.uuid16 = UUID_PROTOCOL_OBEX;
128 protocol[index].parameterNumber = 0;
129 return SDP_AddProtocolDescriptorList(sdpHandle_, protocol, OPP_PROTOCOL_DESCRIPTOR_NUMBER);
130 }
131
AddBrowseGroupList() const132 int OppSdpServer::AddBrowseGroupList() const
133 {
134 HILOGI("[OPP SDP SERVER] Call");
135 BtUuid btUuid = {BT_UUID_16, {SDP_PUBLIC_BROWSE_GROUP_ROOT_UUID}};
136 return SDP_AddBrowseGroupList(sdpHandle_, &btUuid, 1);
137 }
138
AddBluetoothProfileDescriptorList() const139 int OppSdpServer::AddBluetoothProfileDescriptorList() const
140 {
141 HILOGI("[OPP SDP SERVER] Call");
142 // Bluetooth Profile Descriptor List
143 SdpProfileDescriptor profileDescriptor;
144 profileDescriptor.profileUuid.type = BT_UUID_16;
145 profileDescriptor.profileUuid.uuid16 = OPP_UUID16;
146 profileDescriptor.versionNumber = OPP_VERSION_NUMBER;
147 // create Bluetooth Profile Descriptor List
148 return SDP_AddBluetoothProfileDescriptorList(sdpHandle_, &profileDescriptor, 1);
149 }
150
AddServiceName() const151 int OppSdpServer::AddServiceName() const
152 {
153 HILOGI("[OPP SDP SERVER] Call");
154 // Service Name
155 return SDP_AddServiceName(sdpHandle_, SDP_ATTRIBUTE_PRIMARY_LANGUAGE_BASE,
156 OPP_SERVICE_NAME.c_str(), OPP_SERVICE_NAME.length());
157 }
158
AddL2capPsm(uint16_t l2capPsm) const159 int OppSdpServer::AddL2capPsm(uint16_t l2capPsm) const
160 {
161 HILOGI("[OPP SDP SERVER] Call");
162 // GoepL2CapPsm
163 return SDP_AddAttribute(sdpHandle_,
164 OPP_GOEP_L2CAP_PSM_ATTRIBUTE_ID, SDP_TYPE_UINT_16, &l2capPsm, sizeof(l2capPsm));
165 }
166
AddSupportedFormatsList() const167 int OppSdpServer::AddSupportedFormatsList() const
168 {
169 HILOGI("[OPP SDP SERVER] Call");
170 return SDP_AddSequenceAttribute(sdpHandle_, OPP_GOEP_SUPPORTED_FORMATS_LIST_ATTRIBUTE_ID,
171 reinterpret_cast<uint8_t*>(const_cast<uint8_t*>(OPP_SUPPORTED_FORMATS_LIST_DATA)),
172 sizeof(OPP_SUPPORTED_FORMATS_LIST_DATA));
173 }
174 } // namespace bluetooth
175 } // namespace OHOS