• 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 "di_service.h"
17 
18 #include "class_creator.h"
19 #include "log.h"
20 #include "sdp.h"
21 
22 namespace OHOS {
23 namespace bluetooth {
DIService()24 DIService::DIService() : utility::Context(PROFILE_NAME_DI, "1.2.1"), config_(DIConfig::GetInstance())
25 {}
26 
~DIService()27 DIService::~DIService()
28 {}
29 
GetContext()30 utility::Context *DIService::GetContext()
31 {
32     return this;
33 }
34 
Enable()35 void DIService::Enable()
36 {
37     LOG_DEBUG("[DIService]::%{public}s", __FUNCTION__);
38     GetDispatcher()->PostTask(std::bind(&DIService::StartUp, this));
39 }
40 
Disable()41 void DIService::Disable()
42 {
43     LOG_DEBUG("[DIService]::%{public}s", __FUNCTION__);
44     GetDispatcher()->PostTask(std::bind(&DIService::ShutDown, this));
45 }
46 
StartUp()47 void DIService::StartUp()
48 {
49     LOG_DEBUG("[DIService]::%{public}s", __FUNCTION__);
50     bool result = config_.LoadConfigFile();
51     if (result) {
52         LoadDeviceInfo();
53     } else {
54         LOG_ERROR("[DIService]:%{public}s() load config file failed", __FUNCTION__);
55     }
56 
57     result = RegisterDIService();
58     if (!result) {
59         LOG_ERROR("[DIService]:%{public}s() RegisterDIService failed", __FUNCTION__);
60     }
61 
62     GetContext()->OnEnable(PROFILE_NAME_DI, result);
63 }
64 
ShutDown()65 void DIService::ShutDown()
66 {
67     LOG_DEBUG("[DIService]::%{public}s", __FUNCTION__);
68 
69     bool result = DeregisterDIService();
70     if (!result) {
71         LOG_ERROR("[DIService]:%{public}s() DeregisterDIService failed", __FUNCTION__);
72     }
73 
74     GetContext()->OnDisable(PROFILE_NAME_DI, result);
75 }
76 
LoadDeviceInfo()77 void DIService::LoadDeviceInfo()
78 {
79     LOG_DEBUG("[DIService]::%{public}s", __FUNCTION__);
80 
81     priRecord_ = config_.GetPrimaryRecord();
82     specId_ = config_.GetSpecificaitonId();
83     vendorId_ = config_.GetVendorId();
84     procId_ = config_.GetProductId();
85     version_ = config_.GetVersion();
86     venIdSrc_ = config_.GetVendorIdSource();
87 
88     LOG_DEBUG("Get Device info config:");
89     LOG_DEBUG("Primary record is: %{public}d", priRecord_);
90     LOG_DEBUG("Specification ID is: %u", specId_);
91     LOG_DEBUG("Vendor ID is: %u", vendorId_);
92     LOG_DEBUG("Product ID is: %u", procId_);
93     LOG_DEBUG("Version is: %u", version_);
94     LOG_DEBUG("Vender ID Source is: %u", venIdSrc_);
95 }
96 
RegisterDIService()97 bool DIService::RegisterDIService()
98 {
99     LOG_DEBUG("[DIService]::%{public}s", __FUNCTION__);
100 
101     uint32_t handle = SDP_CreateServiceRecord();
102     if (handle == 0) {
103         LOG_ERROR("%{public}s: SDP_CreateServiceRecord Failed!", __FUNCTION__);
104         return false;
105     }
106     handle_ = handle;
107 
108     BtUuid classid;
109     classid.type = BT_UUID_16;
110     classid.uuid16 = UUID_SERVICE_CLASS_PNP_INFORMATION;
111     int ret = SDP_AddServiceClassIdList(handle, &classid, CLASS_ID_NUMBER);
112     if (ret != BT_NO_ERROR) {
113         LOG_WARN("%{public}s::SDP_AddServiceClassIdList Failed", __FUNCTION__);
114     }
115 
116     ret = SDP_AddAttribute(handle, ATTR_ID_SPECIFICATION_ID, SDP_TYPE_UINT_16, (void *)&specId_, sizeof(specId_));
117     if (ret != BT_NO_ERROR) {
118         LOG_WARN("%{public}s::SDP_AddAttribute ATTR_ID_SPECIFICATION_ID Failed", __FUNCTION__);
119     }
120 
121     ret = SDP_AddAttribute(handle, ATTR_ID_VENDOR_ID, SDP_TYPE_UINT_16, (void *)&vendorId_, sizeof(vendorId_));
122     if (ret != BT_NO_ERROR) {
123         LOG_WARN("%{public}s::SDP_AddAttribute ATTR_ID_VENDOR_ID Failed", __FUNCTION__);
124     }
125 
126     ret = SDP_AddAttribute(handle, ATTR_ID_PRODUCT_ID, SDP_TYPE_UINT_16, (void *)&procId_, sizeof(procId_));
127     if (ret != BT_NO_ERROR) {
128         LOG_WARN("%{public}s::SDP_AddAttribute ATTR_ID_PRODUCT_ID Failed", __FUNCTION__);
129     }
130 
131     ret = SDP_AddAttribute(handle, ATTR_ID_VERSION, SDP_TYPE_UINT_16, (void *)&version_, sizeof(version_));
132     if (ret != BT_NO_ERROR) {
133         LOG_WARN("%{public}s::SDP_AddAttribute ATTR_ID_VERSION Failed", __FUNCTION__);
134     }
135 
136     ret = SDP_AddAttribute(handle, ATTR_ID_PRIMARY_RECORD, SDP_TYPE_BOOL, (void *)&priRecord_, sizeof(priRecord_));
137     if (ret != BT_NO_ERROR) {
138         LOG_WARN("%{public}s::SDP_AddAttribute ATTR_ID_PRIMARY_RECORD Failed", __FUNCTION__);
139     }
140 
141     ret = SDP_AddAttribute(handle, ATTR_ID_VENDOR_ID_SOURCE, SDP_TYPE_UINT_16, (void *)&venIdSrc_, sizeof(venIdSrc_));
142     if (ret != BT_NO_ERROR) {
143         LOG_WARN("%{public}s::SDP_AddAttribute ATTR_ID_VENDOR_ID_SOURCE Failed", __FUNCTION__);
144     }
145 
146     ret = SDP_RegisterServiceRecord(handle);
147     if (ret != BT_NO_ERROR) {
148         LOG_ERROR("%{public}s::SDP_RegisterServiceRecord Failed", __FUNCTION__);
149         return false;
150     }
151 
152     return true;
153 }
154 
DeregisterDIService() const155 bool DIService::DeregisterDIService() const
156 {
157     LOG_DEBUG("[DIService]::%{public}s", __FUNCTION__);
158     int ret = SDP_DeregisterServiceRecord(handle_);
159     if (ret != BT_NO_ERROR) {
160         LOG_ERROR("%{public}s::SDP_DeregisterServiceRecord Failed", __FUNCTION__);
161         return false;
162     }
163 
164     ret = SDP_DestroyServiceRecord(handle_);
165     if (ret != BT_NO_ERROR) {
166         LOG_ERROR("%{public}s::SDP_DestroyServiceRecord Failed", __FUNCTION__);
167         return false;
168     }
169 
170     return true;
171 }
172 
Connect(const RawAddress & device)173 int DIService::Connect(const RawAddress &device)
174 {
175     return 0;
176 }
177 
Disconnect(const RawAddress & device)178 int DIService::Disconnect(const RawAddress &device)
179 {
180     return 0;
181 }
182 
GetConnectDevices()183 std::list<RawAddress> DIService::GetConnectDevices()
184 {
185     std::list<RawAddress> addr;
186     return addr;
187 }
188 
GetConnectState()189 int DIService::GetConnectState()
190 {
191     return 0;
192 }
193 
GetMaxConnectNum()194 int DIService::GetMaxConnectNum()
195 {
196     return 0;
197 }
198 
199 REGISTER_CLASS_CREATOR(DIService);
200 }  // namespace bluetooth
201 }  // namespace OHOS