• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_sdp.h"
17 #include "btstack.h"
18 #include "log.h"
19 #include "pbap_pse_def.h"
20 #include "sdp.h"
21 namespace OHOS {
22 namespace bluetooth {
PbapPseSdp(uint8_t rfcommScn,uint16_t l2capPsm)23 PbapPseSdp::PbapPseSdp(uint8_t rfcommScn, uint16_t l2capPsm) : rfcommScn_(rfcommScn), l2capPsm_(l2capPsm)
24 {}
25 
Register()26 int PbapPseSdp::Register()
27 {
28     PBAP_PSE_LOG_INFO("Call %{public}s", __PRETTY_FUNCTION__);
29     int retVal = BT_SUCCESS;
30 
31     sdpHandle_ = SDP_CreateServiceRecord();
32 
33     retVal = AddServiceClassIdList();
34     if (retVal != BT_SUCCESS) {
35         PBAP_PSE_LOG_ERROR("Sdp AddServiceClassIdList retVal = %{public}d", retVal);
36         return retVal;
37     }
38 
39     retVal = AddProtocolDescriptorList();
40     if (retVal != BT_SUCCESS) {
41         PBAP_PSE_LOG_ERROR("Sdp AddProtocolDescriptorList retVal = %{public}d", retVal);
42         return retVal;
43     }
44 
45     retVal = AddAttributes();
46     if (retVal != BT_SUCCESS) {
47         PBAP_PSE_LOG_ERROR("Sdp AddAttributes retVal = %{public}d", retVal);
48         return retVal;
49     }
50 
51     retVal = AddServiceName();
52     if (retVal != BT_SUCCESS) {
53         PBAP_PSE_LOG_ERROR("Sdp AddServiceName retVal = %{public}d", retVal);
54         return retVal;
55     }
56 
57     retVal = AddBluetoothProfileDescriptorList();
58     if (retVal != BT_SUCCESS) {
59         PBAP_PSE_LOG_ERROR("Sdp AddBluetoothProfileDescriptorList retVal = %{public}d", retVal);
60         return retVal;
61     }
62 
63     retVal = AddBrowseGroupList();
64     if (retVal != BT_SUCCESS) {
65         PBAP_PSE_LOG_ERROR("Sdp AddBrowseGroupList retVal = %{public}d", retVal);
66         return retVal;
67     }
68 
69     retVal = RegisterServiceRecord();
70     if (retVal != BT_SUCCESS) {
71         PBAP_PSE_LOG_ERROR("Sdp RegisterServiceRecord retVal = %{public}d", retVal);
72         return retVal;
73     }
74 
75     return retVal;
76 }
77 
Deregister() const78 void PbapPseSdp::Deregister() const
79 {
80     PBAP_PSE_LOG_INFO("Call %{public}s", __PRETTY_FUNCTION__);
81     int retVal = BT_SUCCESS;
82 
83     retVal = SDP_DeregisterServiceRecord(sdpHandle_);
84     if (retVal != BT_SUCCESS) {
85         PBAP_PSE_LOG_ERROR("Sdp SDP_DeregisterServiceRecord retVal = %{public}d", retVal);
86     }
87 
88     retVal = SDP_DestroyServiceRecord(sdpHandle_);
89     if (retVal != BT_SUCCESS) {
90         PBAP_PSE_LOG_ERROR("Sdp SDP_DestroyServiceRecord retVal = %{public}d", retVal);
91     }
92 }
93 
AddServiceClassIdList() const94 int PbapPseSdp::AddServiceClassIdList() const
95 {
96     PBAP_PSE_LOG_INFO("Call %{public}s", __PRETTY_FUNCTION__);
97     BtUuid classId[1];
98     classId[0].type = BT_UUID_16;
99     classId[0].uuid16 = PBAP_PSE_UUID16;
100 
101     // ServiceClassID
102     int retVal = SDP_AddServiceClassIdList(sdpHandle_, classId, 1);
103     if (retVal != BT_SUCCESS) {
104         PBAP_PSE_LOG_ERROR("Sdp AddServiceClassIdList retVal = %{public}d", retVal);
105         return retVal;
106     }
107     return retVal;
108 }
109 
AddProtocolDescriptorList() const110 int PbapPseSdp::AddProtocolDescriptorList() const
111 {
112     PBAP_PSE_LOG_INFO("Call %{public}s", __PRETTY_FUNCTION__);
113     SdpProtocolDescriptor protocol[PABP_PROTOCOL_DESCRIPTOR_NUMBER];
114     uint8_t index = 0;
115     protocol[index].protocolUuid.type = BT_UUID_16;
116     protocol[index].protocolUuid.uuid16 = UUID_PROTOCOL_L2CAP;
117     protocol[index].parameterNumber = 0;
118     index++;
119     protocol[index].protocolUuid.type = BT_UUID_16;
120     protocol[index].protocolUuid.uuid16 = UUID_PROTOCOL_RFCOMM;
121     protocol[index].parameterNumber = 1;
122     protocol[index].parameter[0].type = SDP_TYPE_UINT_8;
123     protocol[index].parameter[0].value = rfcommScn_;
124     index++;
125     protocol[index].protocolUuid.type = BT_UUID_16;
126     protocol[index].protocolUuid.uuid16 = UUID_PROTOCOL_OBEX;
127     protocol[index].parameterNumber = 0;
128     int retVal = SDP_AddProtocolDescriptorList(sdpHandle_, protocol, PABP_PROTOCOL_DESCRIPTOR_NUMBER);
129     if (retVal != BT_SUCCESS) {
130         PBAP_PSE_LOG_ERROR("Sdp AddProtocolDescriptorList retVal = %{public}d", retVal);
131     }
132     return retVal;
133 }
134 
AddAttributes()135 int PbapPseSdp::AddAttributes()
136 {
137     PBAP_PSE_LOG_INFO("Call %{public}s", __PRETTY_FUNCTION__);
138 #ifdef PBAP_PSE_L2CAP_ENABLE
139     // GoepL2CapPsm
140     int retValL2cap =
141         SDP_AddAttribute(sdpHandle_, PABP_GOEP_L2CAP_PSM_ATTRIBUTE_ID, SDP_TYPE_UINT_16, &l2capPsm_, sizeof(l2capPsm_));
142     if (retValL2cap != BT_SUCCESS) {
143         PBAP_PSE_LOG_ERROR("Sdp SDP_AddAttribute retVal = %{public}d", retValL2cap);
144         return retValL2cap;
145     }
146     PBAP_PSE_LOG_INFO(
147         "PABP_GOEP_L2CAP_PSM_ATTRIBUTE_ID:[%#X], value:[%#X]", PABP_GOEP_L2CAP_PSM_ATTRIBUTE_ID, l2capPsm_);
148 #endif
149     uint8_t supportedRepos = PBAP_PSE_SUPPORTED_REPOSITORIES;
150     // Supported Repositories
151     int retVal = SDP_AddAttribute(
152         sdpHandle_, PBAP_SUPPORTED_REPOSITORIES_ATTRIBUTE_ID, SDP_TYPE_UINT_8, &supportedRepos, sizeof(supportedRepos));
153     if (retVal != BT_SUCCESS) {
154         PBAP_PSE_LOG_ERROR("Sdp SDP_AddAttribute retVal = %{public}d", retVal);
155         return retVal;
156     }
157     PBAP_PSE_LOG_INFO("PBAP_SUPPORTED_REPOSITORIES_ATTRIBUTE_ID:[%#X], value:[%#X]",
158         PBAP_SUPPORTED_REPOSITORIES_ATTRIBUTE_ID,
159         supportedRepos);
160 
161     // PbapSupportedFeatures
162     uint32_t supportedFeatures = PBAP_PSE_SUPPORTED_FEATURES;
163     retVal = SDP_AddAttribute(sdpHandle_,
164         PBAP_SUPPORTED_FEATURES_ATTRIBUTE_ID,
165         SDP_TYPE_UINT_32,
166         &supportedFeatures,
167         sizeof(supportedFeatures));
168     if (retVal != BT_SUCCESS) {
169         PBAP_PSE_LOG_ERROR("Sdp SDP_AddAttribute retVal = %{public}d", retVal);
170         return retVal;
171     }
172     PBAP_PSE_LOG_INFO("PBAP_SUPPORTED_FEATURES_ATTRIBUTE_ID:[%#X], value:[%#X]",
173         PBAP_SUPPORTED_FEATURES_ATTRIBUTE_ID,
174         supportedFeatures);
175     return retVal;
176 }
177 
AddBluetoothProfileDescriptorList() const178 int PbapPseSdp::AddBluetoothProfileDescriptorList() const
179 {
180     PBAP_PSE_LOG_INFO("Call %{public}s", __PRETTY_FUNCTION__);
181     // Bluetooth Profile Descriptor List
182     SdpProfileDescriptor profileDescriptor;
183     profileDescriptor.profileUuid.type = BT_UUID_16;
184     profileDescriptor.profileUuid.uuid16 = PBAP_PSE_PROFILE_UUID16;
185     profileDescriptor.versionNumber = PBAP_PSE_VERSION_NUMBER;
186     // create Bluetooth Profile Descriptor List
187     int retVal = SDP_AddBluetoothProfileDescriptorList(sdpHandle_, &profileDescriptor, 1);
188     if (retVal != BT_SUCCESS) {
189         PBAP_PSE_LOG_ERROR("Sdp AddBluetoothProfileDescriptorList retVal = %{public}d", retVal);
190     }
191     return retVal;
192 }
193 
AddServiceName() const194 int PbapPseSdp::AddServiceName() const
195 {
196     PBAP_PSE_LOG_INFO("Call %{public}s", __PRETTY_FUNCTION__);
197     // Service Name
198     int retVal = SDP_AddServiceName(
199         sdpHandle_, SDP_ATTRIBUTE_PRIMARY_LANGUAGE_BASE, PBAP_PSE_SERVICE_NAME.c_str(), PBAP_PSE_SERVICE_NAME.length());
200     if (retVal != BT_SUCCESS) {
201         PBAP_PSE_LOG_ERROR("Sdp AddServiceName retVal = %{public}d", retVal);
202     }
203     return retVal;
204 }
205 
AddBrowseGroupList() const206 int PbapPseSdp::AddBrowseGroupList() const
207 {
208     PBAP_PSE_LOG_INFO("Call %{public}s", __PRETTY_FUNCTION__);
209     BtUuid btUuid = {BT_UUID_16, {SDP_PUBLIC_BROWSE_GROUP_ROOT_UUID}};
210     int retVal = SDP_AddBrowseGroupList(sdpHandle_, &btUuid, 1);
211     if (retVal != BT_SUCCESS) {
212         PBAP_PSE_LOG_ERROR("Sdp AddBrowseGroupList retVal = %{public}d", retVal);
213     }
214     return retVal;
215 }
216 
RegisterServiceRecord() const217 int PbapPseSdp::RegisterServiceRecord() const
218 {
219     PBAP_PSE_LOG_INFO("Call %{public}s", __PRETTY_FUNCTION__);
220     int retVal = SDP_RegisterServiceRecord(sdpHandle_);
221     if (retVal != BT_SUCCESS) {
222         PBAP_PSE_LOG_ERROR("Sdp RegisterServiceRecord retVal = %{public}d", retVal);
223     }
224     return retVal;
225 }
226 }  // namespace bluetooth
227 }  // namespace OHOS