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