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 "sdp.h"
17 #include "avrcp_ct_sdp.h"
18
19 namespace OHOS {
20 namespace bluetooth {
21 const int ATTR_NUMBER2 = 2;
22 const int ATTR_NUMBER1 = 1;
23 /// Number of items when add service class id list.
24 const uint16_t AVRC_SERVICE_CLASS_ID_LIST_NUMBER = 0x0002;
25 /// Number of items when add protocol descriptor.
26 const uint16_t AVRC_PROTOCOL_DESCRIPTOR_LIST_NUMBER = 0x0002;
27 /// Number of items when add additional protocol descriptor.
28 const uint16_t AVRC_ADDITIONAL_PROTOCOL_DESCRIPTOR_LIST_NUMBER = 0x0001;
29 /// Number of items when add bluetooth profile descriptor list.
30 const uint16_t AVRC_BLUETOOTH_PROFILE_DESCRIPTOR_LIST_NUMBER = 0x0001;
31 /// Number of items when add attributes.
32 const uint16_t AVRC_ATTRIBUTE_ID_SUPPORTED_FEATURES_NUMBER = 0x0001;
33 /// Number of items when add browse group list.
34 const uint16_t AVRC_ATTRIBUTE_ID_BROWSE_GROUP_LIST_NUMBER = 0x0001;
35 /// Length of target service name.
36 const uint16_t AVRC_SERVICE_NAME_LENGTH = 0x001C;
37
AvrcCtSdpManager(uint32_t features)38 AvrcCtSdpManager::AvrcCtSdpManager(uint32_t features) : sdpHandle_(0xFFFFFFFF), features_(features)
39 {
40 HILOGI("features: %{public}u", features);
41 }
42
~AvrcCtSdpManager()43 AvrcCtSdpManager::~AvrcCtSdpManager()
44 {
45 HILOGI("enter");
46 }
47
RegisterService(void)48 int AvrcCtSdpManager::RegisterService(void)
49 {
50 HILOGI("enter");
51
52 int result = BT_SUCCESS;
53 sdpHandle_ = SDP_CreateServiceRecord();
54
55 /// Service Class ID List.
56 BtUuid classIdList[AVRC_SERVICE_CLASS_ID_LIST_NUMBER];
57 classIdList[0].type = BT_UUID_16;
58 classIdList[0].uuid16 = AVRC_CT_AV_REMOTE_CONTROL;
59 classIdList[1].type = BT_UUID_16;
60 classIdList[1].uuid16 = AVRC_CT_AV_REMOTE_CONTROL_CONTROLLER;
61 result |= SDP_AddServiceClassIdList(sdpHandle_, classIdList, AVRC_SERVICE_CLASS_ID_LIST_NUMBER);
62
63 /// Protocol Descriptor List.
64 result |= AddProtocolDescriptorList();
65
66 /// Bluetooth Profile Descriptor List.
67 SdpProfileDescriptor profileDsc;
68 profileDsc.versionNumber = AVRC_CT_PROFILE_REV_1_6;
69 profileDsc.profileUuid.type = BT_UUID_16;
70 profileDsc.profileUuid.uuid16 = AVRC_CT_AV_REMOTE_CONTROL;
71 result |=
72 SDP_AddBluetoothProfileDescriptorList(sdpHandle_, &profileDsc, AVRC_BLUETOOTH_PROFILE_DESCRIPTOR_LIST_NUMBER);
73
74 /// Supported Features.
75 uint16_t attributes[AVRC_ATTRIBUTE_ID_SUPPORTED_FEATURES_NUMBER];
76 attributes[0] = features_;
77 result |= SDP_AddAttribute(sdpHandle_,
78 AVRC_CT_ATTRIBUTE_ID_SUPPORTED_FEATURES,
79 SDP_TYPE_UINT_16,
80 &attributes,
81 AVRC_ATTRIBUTE_ID_SUPPORTED_FEATURES_NUMBER);
82
83 /// Add nothing for "provider name", because it is optional and useless.
84
85 /// Service Name.
86 std::string serviceName = "AV Remote Control Controller";
87 result |= SDP_AddServiceName(
88 sdpHandle_, SDP_ATTRIBUTE_PRIMARY_LANGUAGE_BASE, serviceName.c_str(), AVRC_SERVICE_NAME_LENGTH);
89
90 /// Browse Group Identifiers.
91 BtUuid browseGroupList[AVRC_ATTRIBUTE_ID_BROWSE_GROUP_LIST_NUMBER];
92 browseGroupList[0].type = BT_UUID_16;
93 browseGroupList[0].uuid16 = SDP_PUBLIC_BROWSE_GROUP_ROOT_UUID;
94 result |= SDP_AddBrowseGroupList(sdpHandle_, browseGroupList, AVRC_ATTRIBUTE_ID_BROWSE_GROUP_LIST_NUMBER);
95
96 /// Register target service.
97 result |= SDP_RegisterServiceRecord(sdpHandle_);
98
99 (result == BT_SUCCESS) ? (result = BT_SUCCESS) : (result = RET_BAD_STATUS);
100
101 return result;
102 }
103
UnregisterService(void) const104 int AvrcCtSdpManager::UnregisterService(void) const
105 {
106 HILOGI("enter");
107
108 int result = BT_SUCCESS;
109
110 result |= SDP_DeregisterServiceRecord(sdpHandle_);
111 result |= SDP_DestroyServiceRecord(sdpHandle_);
112
113 (result == BT_SUCCESS) ? (result = BT_SUCCESS) : (result = RET_BAD_STATUS);
114
115 return result;
116 }
117
FindTgService(const RawAddress & rawAddr,void (* callback)(const BtAddr * btAddr,const SdpService * serviceArray,uint16_t serviceNum,void * context))118 int AvrcCtSdpManager::FindTgService(const RawAddress &rawAddr,
119 void (*callback)(const BtAddr *btAddr, const SdpService *serviceArray, uint16_t serviceNum, void *context))
120 {
121 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
122
123 BtAddr btAddr;
124 rawAddr.ConvertToUint8(btAddr.addr);
125
126 BtUuid classIdList[AVRC_SERVICE_CLASS_ID_LIST_NUMBER];
127 classIdList[0].type = BT_UUID_16;
128 classIdList[0].uuid16 = AVRC_CT_AV_REMOTE_CONTROL;
129 SdpUuid sdpUuid = {
130 .uuidNum = AVRC_SERVICE_CLASS_ID_LIST_NUMBER,
131 .uuid = classIdList
132 };
133
134 SdpAttributeIdList attributeIdList;
135 attributeIdList.type = SDP_TYPE_LIST;
136
137 attributeIdList.attributeIdList.attributeIdNumber = AVRC_SDP_ATTRIBUTE_NUM;
138 attributeIdList.attributeIdList.attributeId[0] = SDP_ATTRIBUTE_SERVICE_CLASS_ID_LIST;
139 attributeIdList.attributeIdList.attributeId[ATTR_NUMBER1] = SDP_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST;
140 attributeIdList.attributeIdList.attributeId[ATTR_NUMBER2] = AVRC_CT_ATTRIBUTE_ID_SUPPORTED_FEATURES;
141
142 int result = SDP_ServiceSearchAttribute(&btAddr, &sdpUuid, attributeIdList, nullptr, callback);
143 (result == BT_SUCCESS) ? (result = BT_SUCCESS) : (result = RET_BAD_STATUS);
144
145 return result;
146 }
147
AddProtocolDescriptorList()148 int AvrcCtSdpManager::AddProtocolDescriptorList()
149 {
150 HILOGI("enter");
151
152 int result = BT_SUCCESS;
153
154 SdpProtocolDescriptor dscList[AVRC_PROTOCOL_DESCRIPTOR_LIST_NUMBER];
155 dscList[0].parameter[0].type = SDP_TYPE_UINT_16;
156 dscList[0].parameter[0].value = UUID_PROTOCOL_AVCTP;
157 dscList[0].parameterNumber = 1;
158 dscList[0].protocolUuid.type = BT_UUID_16;
159 dscList[0].protocolUuid.uuid16 = UUID_PROTOCOL_L2CAP;
160 dscList[1].parameter[0].type = SDP_TYPE_UINT_16;
161 dscList[1].parameter[0].value = AVCT_REV_1_4;
162 dscList[1].parameterNumber = 1;
163 dscList[1].protocolUuid.type = BT_UUID_16;
164 dscList[1].protocolUuid.uuid16 = UUID_PROTOCOL_AVCTP;
165 result |= SDP_AddProtocolDescriptorList(sdpHandle_, dscList, AVRC_PROTOCOL_DESCRIPTOR_LIST_NUMBER);
166
167 if (IsSupportedCategory1() || IsSupportedCategory2()) {
168 /// Additional Protocol Descriptor List.
169 SdpAdditionalProtocolDescriptor addlDsc;
170 addlDsc.protocolDescriptorNumber = AVRC_PROTOCOL_DESCRIPTOR_LIST_NUMBER;
171 addlDsc.parameter[0].parameter[0].type = SDP_TYPE_UINT_16;
172 addlDsc.parameter[0].parameter[0].value = AVCT_BR_PSM;
173 addlDsc.parameter[0].parameterNumber = 1;
174 addlDsc.parameter[0].protocolUuid.type = BT_UUID_16;
175 addlDsc.parameter[0].protocolUuid.uuid16 = UUID_PROTOCOL_L2CAP;
176 addlDsc.parameter[1].parameter[0].type = SDP_TYPE_UINT_16;
177 addlDsc.parameter[1].parameter[0].value = AVCT_REV_1_4;
178 addlDsc.parameter[1].parameterNumber = 1;
179 addlDsc.parameter[1].protocolUuid.type = BT_UUID_16;
180 addlDsc.parameter[1].protocolUuid.uuid16 = UUID_PROTOCOL_AVCTP;
181 result |= SDP_AddAdditionalProtocolDescriptorList(
182 sdpHandle_, &addlDsc, AVRC_ADDITIONAL_PROTOCOL_DESCRIPTOR_LIST_NUMBER);
183 }
184
185 return result;
186 }
187 } // namespace bluetooth
188 } // namespace OHOS
189