• 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 "sdp.h"
17 #include "avrcp_ct_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 = 0x0002;
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 = 0x001C;
35 
AvrcCtSdpManager(uint32_t features)36 AvrcCtSdpManager::AvrcCtSdpManager(uint32_t features) : sdpHandle_(0xFFFFFFFF), features_(features)
37 {
38     HILOGI("features: %{public}u", features);
39 }
40 
~AvrcCtSdpManager()41 AvrcCtSdpManager::~AvrcCtSdpManager()
42 {
43     HILOGI("enter");
44 }
45 
RegisterService(void)46 int AvrcCtSdpManager::RegisterService(void)
47 {
48     HILOGI("enter");
49 
50     int 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_CT_AV_REMOTE_CONTROL;
57     classIdList[1].type = BT_UUID_16;
58     classIdList[1].uuid16 = AVRC_CT_AV_REMOTE_CONTROL_CONTROLLER;
59     result |= SDP_AddServiceClassIdList(sdpHandle_, classIdList, AVRC_SERVICE_CLASS_ID_LIST_NUMBER);
60 
61     /// Protocol Descriptor List.
62     result |= AddProtocolDescriptorList();
63 
64     /// Bluetooth Profile Descriptor List.
65     SdpProfileDescriptor profileDsc;
66     profileDsc.versionNumber = AVRC_CT_PROFILE_REV_1_6;
67     profileDsc.profileUuid.type = BT_UUID_16;
68     profileDsc.profileUuid.uuid16 = AVRC_CT_AV_REMOTE_CONTROL;
69     result |=
70         SDP_AddBluetoothProfileDescriptorList(sdpHandle_, &profileDsc, AVRC_BLUETOOTH_PROFILE_DESCRIPTOR_LIST_NUMBER);
71 
72     /// Supported Features.
73     uint16_t attributes[AVRC_ATTRIBUTE_ID_SUPPORTED_FEATURES_NUMBER];
74     attributes[0] = features_;
75     result |= SDP_AddAttribute(sdpHandle_,
76         AVRC_CT_ATTRIBUTE_ID_SUPPORTED_FEATURES,
77         SDP_TYPE_UINT_16,
78         &attributes,
79         AVRC_ATTRIBUTE_ID_SUPPORTED_FEATURES_NUMBER);
80 
81     /// Add nothing for "provider name", because it is optional and useless.
82 
83     /// Service Name.
84     std::string serviceName = "AV Remote Control Controller";
85     result |= SDP_AddServiceName(
86         sdpHandle_, SDP_ATTRIBUTE_PRIMARY_LANGUAGE_BASE, serviceName.c_str(), AVRC_SERVICE_NAME_LENGTH);
87 
88     /// Browse Group Identifiers.
89     BtUuid browseGroupList[AVRC_ATTRIBUTE_ID_BROWSE_GROUP_LIST_NUMBER];
90     browseGroupList[0].type = BT_UUID_16;
91     browseGroupList[0].uuid16 = SDP_PUBLIC_BROWSE_GROUP_ROOT_UUID;
92     result |= SDP_AddBrowseGroupList(sdpHandle_, browseGroupList, AVRC_ATTRIBUTE_ID_BROWSE_GROUP_LIST_NUMBER);
93 
94     /// Register target service.
95     result |= SDP_RegisterServiceRecord(sdpHandle_);
96 
97     (result == BT_SUCCESS) ? (result = BT_SUCCESS) : (result = RET_BAD_STATUS);
98 
99     return result;
100 }
101 
UnregisterService(void) const102 int AvrcCtSdpManager::UnregisterService(void) const
103 {
104     HILOGI("enter");
105 
106     int result = BT_SUCCESS;
107 
108     result |= SDP_DeregisterServiceRecord(sdpHandle_);
109     result |= SDP_DestroyServiceRecord(sdpHandle_);
110 
111     (result == BT_SUCCESS) ? (result = BT_SUCCESS) : (result = RET_BAD_STATUS);
112 
113     return result;
114 }
115 
FindTgService(const RawAddress & rawAddr,void (* callback)(const BtAddr * btAddr,const uint32_t * handleArray,uint16_t handleNum,void * context))116 int AvrcCtSdpManager::FindTgService(const RawAddress &rawAddr,
117     void (*callback)(const BtAddr *btAddr, const uint32_t *handleArray, uint16_t handleNum, void *context))
118 {
119     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
120 
121     BtAddr btAddr;
122     rawAddr.ConvertToUint8(btAddr.addr);
123 
124     BtUuid classIdList[AVRC_SERVICE_CLASS_ID_LIST_NUMBER - 1];
125     classIdList[0].type = BT_UUID_16;
126     classIdList[0].uuid16 = AVRC_CT_AV_REMOTE_CONTROL_TARGET;
127     SdpUuid sdpUuid = {.uuidNum = AVRC_SERVICE_CLASS_ID_LIST_NUMBER - 1, .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 AvrcCtSdpManager::AddProtocolDescriptorList()
136 {
137     HILOGI("enter");
138 
139     int 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