1 /*
2 * Copyright (c) 2023 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 "dm_binding.h"
17
18 #include <string>
19
20 #include "device_manager.h"
21 #include "dm_device_info.h"
22
23 #include "devicestatus_define.h"
24 #include "dm_binding_internal.h"
25
26 #undef LOG_TAG
27 #define LOG_TAG "DmBinding"
28
29 namespace {
30 #define DIS_HARDWARE OHOS::DistributedHardware::DeviceManager::GetInstance()
31 } // namespace
32
DeviceInitCallBack(void (* cb)())33 DeviceInitCallBack::DeviceInitCallBack(void (*cb)()) : callback_(cb)
34 {
35 }
36
OnRemoteDied()37 void DeviceInitCallBack::OnRemoteDied()
38 {
39 CALL_DEBUG_ENTER;
40 CHKPV(callback_);
41 callback_();
42 }
43
DmDeviceStateCallback(CRegisterDevStateCallback callbacks)44 DmDeviceStateCallback::DmDeviceStateCallback(CRegisterDevStateCallback callbacks)
45 {
46 CALL_DEBUG_ENTER;
47 InitCallback(callbacks);
48 }
49
OnDeviceOnline(const OHOS::DistributedHardware::DmDeviceInfo & deviceInfo)50 void DmDeviceStateCallback::OnDeviceOnline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo)
51 {
52 CALL_DEBUG_ENTER;
53 CHKPV(onlineCallback_);
54 CDmDeviceInfo* cDeviceInfo = CreateCDeviceInfo(deviceInfo);
55 CHKPV(cDeviceInfo);
56 onlineCallback_(cDeviceInfo);
57 }
58
OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceInfo & deviceInfo)59 void DmDeviceStateCallback::OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo)
60 {
61 CALL_DEBUG_ENTER;
62 CHKPV(changedCallback_);
63 CDmDeviceInfo* cDeviceInfo = CreateCDeviceInfo(deviceInfo);
64 CHKPV(cDeviceInfo);
65 changedCallback_(cDeviceInfo);
66 }
67
OnDeviceReady(const OHOS::DistributedHardware::DmDeviceInfo & deviceInfo)68 void DmDeviceStateCallback::OnDeviceReady(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo)
69 {
70 CALL_DEBUG_ENTER;
71 CHKPV(readyCallback_);
72 CDmDeviceInfo* cDeviceInfo = CreateCDeviceInfo(deviceInfo);
73 CHKPV(cDeviceInfo);
74 readyCallback_(cDeviceInfo);
75 }
76
OnDeviceOffline(const OHOS::DistributedHardware::DmDeviceInfo & deviceInfo)77 void DmDeviceStateCallback::OnDeviceOffline(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo)
78 {
79 CALL_DEBUG_ENTER;
80 CHKPV(offlineCallback_);
81 CDmDeviceInfo* cDeviceInfo = CreateCDeviceInfo(deviceInfo);
82 CHKPV(cDeviceInfo);
83 offlineCallback_(cDeviceInfo);
84 }
85
InitCallback(CRegisterDevStateCallback callbacks)86 void DmDeviceStateCallback::InitCallback(CRegisterDevStateCallback callbacks)
87 {
88 CALL_DEBUG_ENTER;
89 CHKPV(callbacks.onDeviceOnline);
90 CHKPV(callbacks.onDeviceChanged);
91 CHKPV(callbacks.onDeviceReady);
92 CHKPV(callbacks.onDeviceOffline);
93 onlineCallback_ = callbacks.onDeviceOnline;
94 changedCallback_ = callbacks.onDeviceChanged;
95 readyCallback_ = callbacks.onDeviceReady;
96 offlineCallback_ = callbacks.onDeviceOffline;
97 }
98
CreateCDeviceInfo(const OHOS::DistributedHardware::DmDeviceInfo & deviceInfo)99 CDmDeviceInfo* DmDeviceStateCallback::CreateCDeviceInfo(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo)
100 {
101 CALL_DEBUG_ENTER;
102 CDmDeviceInfo* cDeviceInfo = new (std::nothrow) CDmDeviceInfo;
103 cDeviceInfo->deviceId = new (std::nothrow) char[sizeof(deviceInfo.deviceId)];
104 if (strcpy_s(cDeviceInfo->deviceId, sizeof(deviceInfo.deviceId), deviceInfo.deviceId) != EOK) {
105 FI_HILOGE("Invalid deviceId:\'%{public}s\'", GetAnonyString(deviceInfo.deviceId).c_str());
106 return nullptr;
107 }
108 cDeviceInfo->deviceName = new (std::nothrow) char[sizeof(deviceInfo.deviceName)];
109 if (strcpy_s(cDeviceInfo->deviceName, sizeof(deviceInfo.deviceName), deviceInfo.deviceName) != EOK) {
110 FI_HILOGE("Invalid deviceName");
111 return nullptr;
112 }
113 cDeviceInfo->deviceTypeId = deviceInfo.deviceTypeId;
114 cDeviceInfo->networkId = new (std::nothrow) char[sizeof(deviceInfo.networkId)];
115 if (strcpy_s(cDeviceInfo->networkId, sizeof(deviceInfo.networkId), deviceInfo.networkId) != EOK) {
116 FI_HILOGE("Invalid networkId");
117 return nullptr;
118 }
119 cDeviceInfo->range = deviceInfo.range;
120 cDeviceInfo->authForm = static_cast<CDmAuthForm>(deviceInfo.authForm);
121 return cDeviceInfo;
122 }
123
CInitDeviceManager(const char * pkgName,void (* callback)())124 bool CInitDeviceManager(const char* pkgName, void (*callback)())
125 {
126 CALL_DEBUG_ENTER;
127 CHKPF(callback);
128 auto initCallback = std::make_shared<DeviceInitCallBack>(callback);
129 std::string sPkgName(pkgName);
130 int32_t ret = DIS_HARDWARE.InitDeviceManager(sPkgName, initCallback);
131 if (ret != 0) {
132 FI_HILOGE("Init device manager failed, ret:%{public}d", ret);
133 return false;
134 }
135 return true;
136 }
137
CRegisterDevState(const char * pkgName,const char * extra,CRegisterDevStateCallback callbacks)138 bool CRegisterDevState(const char* pkgName, const char* extra, CRegisterDevStateCallback callbacks)
139 {
140 CALL_DEBUG_ENTER;
141 auto stateCallback = std::make_shared<DmDeviceStateCallback>(callbacks);
142 std::string sPkgName(pkgName);
143 std::string sExtra(extra);
144 int32_t ret = DIS_HARDWARE.RegisterDevStateCallback(sPkgName, sExtra, stateCallback);
145 if (ret != 0) {
146 FI_HILOGE("Register devStateCallback failed, ret:%{public}d", ret);
147 return false;
148 }
149 return true;
150 }
151
CUnRegisterDevState(const char * pkgName,const char * extra)152 bool CUnRegisterDevState(const char* pkgName, const char* extra)
153 {
154 CALL_DEBUG_ENTER;
155 std::string sPkgName(pkgName);
156 std::string sExtra(extra);
157 int32_t ret = DIS_HARDWARE.UnRegisterDevStateCallback(sPkgName, sExtra);
158 if (ret != 0) {
159 FI_HILOGE("UnRegister devStateCallback failed, ret:%{public}d", ret);
160 return false;
161 }
162 return true;
163 }
164
CDestroyDmDeviceInfo(CDmDeviceInfo * deviceInfo)165 void CDestroyDmDeviceInfo(CDmDeviceInfo* deviceInfo)
166 {
167 CALL_DEBUG_ENTER;
168 CHKPV(deviceInfo);
169 deviceInfo = nullptr;
170 delete deviceInfo;
171 }