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 "gatt_service_over_bredr.h"
17 #include "gatt_defines.h"
18 #include "bt_def.h"
19 #include "class_creator.h"
20 #include "log.h"
21 #include "sdp.h"
22
23 namespace OHOS {
24 namespace bluetooth {
RegisterSDP(uint16_t startHandle,uint16_t endHandle)25 int GattServiceOverBredr::RegisterSDP(uint16_t startHandle, uint16_t endHandle)
26 {
27 LOG_DEBUG("[GenericAttributeService]::%{public}s", __FUNCTION__);
28
29 sdpHandle_ = SDP_CreateServiceRecord();
30 if (sdpHandle_ == 0) {
31 LOG_ERROR("%{public}s: SDP_CreateServiceRecord Failed!", __FUNCTION__);
32 return false;
33 }
34
35 int ret = SdpAddServiceClassIdList();
36 if (ret != BT_SUCCESS) {
37 LOG_WARN("%{public}s::SDP_AddServiceClassIdList Failed", __FUNCTION__);
38 }
39
40 ret = SdpAddProtocol(startHandle, endHandle);
41 if (ret != BT_SUCCESS) {
42 LOG_WARN("%{public}s::SDP_AddProtocolDescriptorList Failed", __FUNCTION__);
43 }
44
45 ret = SdpAddBrowseGroupList();
46 if (ret != BT_SUCCESS) {
47 LOG_WARN("%{public}s::SDP_AddBrowseGroupList Failed", __FUNCTION__);
48 }
49
50 ret = SDP_RegisterServiceRecord(sdpHandle_);
51 if (ret != BT_SUCCESS) {
52 LOG_ERROR("%{public}s::SDP_RegisterServiceRecord Failed", __FUNCTION__);
53 return GattStatus::GATT_FAILURE;
54 }
55
56 return GattStatus::GATT_SUCCESS;
57 }
58
DeregisterSDP()59 void GattServiceOverBredr::DeregisterSDP()
60 {
61 LOG_DEBUG("[GenericAttributeService]::%{public}s", __FUNCTION__);
62 int ret = SDP_DeregisterServiceRecord(sdpHandle_);
63 if (ret != BT_SUCCESS) {
64 LOG_ERROR("%{public}s::SDP_DeregisterServiceRecord Failed", __FUNCTION__);
65 }
66
67 ret = SDP_DestroyServiceRecord(sdpHandle_);
68 if (ret != BT_SUCCESS) {
69 LOG_ERROR("%{public}s::SDP_DestroyServiceRecord Failed", __FUNCTION__);
70 }
71 }
72
SdpAddServiceClassIdList() const73 int GattServiceOverBredr::SdpAddServiceClassIdList() const
74 {
75 BtUuid classid;
76 classid.type = BT_UUID_16;
77 classid.uuid16 = UUID_GENERIC_ATTRIBUTE_SERVICE;
78 return SDP_AddServiceClassIdList(sdpHandle_, &classid, 1);
79 }
80
SdpAddProtocol(uint16_t startHandle,uint16_t endHandle) const81 int GattServiceOverBredr::SdpAddProtocol(uint16_t startHandle, uint16_t endHandle) const
82 {
83 static const size_t gattsProtocolNum = 2;
84 static const size_t gattProtParmNum = 2;
85 static const uint16_t gattPsmAtt = 0x001F;
86 SdpProtocolDescriptor protocol[gattsProtocolNum];
87 protocol[0].protocolUuid.type = BT_UUID_16;
88 protocol[0].protocolUuid.uuid16 = UUID_PROTOCOL_L2CAP;
89 protocol[0].parameterNumber = 1;
90 protocol[0].parameter[0].type = SDP_TYPE_UINT_16;
91 protocol[0].parameter[0].value = gattPsmAtt;
92
93 protocol[1].protocolUuid.type = BT_UUID_16;
94 protocol[1].protocolUuid.uuid16 = UUID_PROTOCOL_ATT;
95 protocol[1].parameterNumber = gattProtParmNum;
96 protocol[1].parameter[0].type = SDP_TYPE_UINT_16;
97 protocol[1].parameter[0].value = startHandle;
98 protocol[1].parameter[1].type = SDP_TYPE_UINT_16;
99 protocol[1].parameter[1].value = endHandle;
100 return SDP_AddProtocolDescriptorList(sdpHandle_, protocol, gattsProtocolNum);
101 }
102
SdpAddBrowseGroupList() const103 int GattServiceOverBredr::SdpAddBrowseGroupList() const
104 {
105 BtUuid browseGroupList[1];
106 browseGroupList[0].type = BT_UUID_16;
107 browseGroupList[0].uuid16 = SDP_PUBLIC_BROWSE_GROUP_ROOT_UUID;
108 return SDP_AddBrowseGroupList(sdpHandle_, browseGroupList, 1);
109 }
110
111 REGISTER_CLASS_CREATOR(GattServiceOverBredr);
112 } // namespace bluetooth
113 } // namespace OHOS