• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "device_manager_utils.h"
16 
17 #include <string>
18 #include <map>
19 #include <mutex>
20 
21 #include "device_manager_impl.h"
22 #include "dm_anonymous.h"
23 #include "dm_constants.h"
24 #include "dm_log.h"
25 
26 namespace OHOS::DistributedHardware {
27 
28 namespace {
29     constexpr std::size_t MAX_MALLOC_SIZE = 0x10000;
30 }
31 
OnRemoteDied()32 void DmFfiInitCallback::OnRemoteDied()
33 {
34     DeviceManagerFfiImpl *deviceManagerFfi = DeviceManagerFfiImpl::GetDeviceManagerFfi(bundleName_);
35     if (deviceManagerFfi == nullptr) {
36         LOGE("OnRemoteDied, deviceManagerFfi not find for bundleName %{public}s", bundleName_.c_str());
37     }
38     LOGI("OnRemoteDied, deviceManagerFfi bundleName %{public}s", bundleName_.c_str());
39 }
40 
OnDeviceOnline(const DmDeviceBasicInfo & deviceBasicInfo)41 void DmFfiDeviceStatusCallback::OnDeviceOnline(const DmDeviceBasicInfo &deviceBasicInfo)
42 {
43     DeviceManagerFfiImpl *deviceManagerFfi = DeviceManagerFfiImpl::GetDeviceManagerFfi(bundleName_);
44     if (deviceManagerFfi == nullptr) {
45         LOGE("OnDeviceOnline, deviceManagerFfi not find for bundleName %{public}s", bundleName_.c_str());
46     } else {
47         deviceManagerFfi->OnDeviceStatusChange(DmFfiDevStatusChange::UNKNOWN, deviceBasicInfo);
48     }
49 }
50 
OnDeviceReady(const DmDeviceBasicInfo & deviceBasicInfo)51 void DmFfiDeviceStatusCallback::OnDeviceReady(const DmDeviceBasicInfo &deviceBasicInfo)
52 {
53     DeviceManagerFfiImpl *deviceManagerFfi = DeviceManagerFfiImpl::GetDeviceManagerFfi(bundleName_);
54     if (deviceManagerFfi == nullptr) {
55         LOGE("OnDeviceReady, deviceManagerFfi not find for bundleName %{public}s", bundleName_.c_str());
56     } else {
57         deviceManagerFfi->OnDeviceStatusChange(DmFfiDevStatusChange::AVAILABLE, deviceBasicInfo);
58     }
59 }
60 
OnDeviceOffline(const DmDeviceBasicInfo & deviceBasicInfo)61 void DmFfiDeviceStatusCallback::OnDeviceOffline(const DmDeviceBasicInfo &deviceBasicInfo)
62 {
63     DeviceManagerFfiImpl *deviceManagerFfi = DeviceManagerFfiImpl::GetDeviceManagerFfi(bundleName_);
64     if (deviceManagerFfi == nullptr) {
65         LOGE("OnDeviceOffline, deviceManagerFfi not find for bundleName %{public}s", bundleName_.c_str());
66     } else {
67         deviceManagerFfi->OnDeviceStatusChange(DmFfiDevStatusChange::UNAVAILABLE, deviceBasicInfo);
68     }
69 }
70 
OnDeviceChanged(const DmDeviceBasicInfo & deviceBasicInfo)71 void DmFfiDeviceStatusCallback::OnDeviceChanged(const DmDeviceBasicInfo &deviceBasicInfo)
72 {
73     DeviceManagerFfiImpl *deviceManagerFfi = DeviceManagerFfiImpl::GetDeviceManagerFfi(bundleName_);
74     if (deviceManagerFfi == nullptr) {
75         LOGE("OnDeviceChanged, deviceManagerFfi not find for bundleName %{public}s", bundleName_.c_str());
76     } else {
77         deviceManagerFfi->OnDeviceNameChange(deviceBasicInfo.deviceName);
78     }
79 }
80 
OnDeviceFound(uint16_t subscribeId,const DmDeviceBasicInfo & deviceBasicInfo)81 void DmFfiDiscoveryCallback::OnDeviceFound(uint16_t subscribeId, const DmDeviceBasicInfo &deviceBasicInfo)
82 {
83     LOGI("OnDeviceFound for %{public}s, subscribeId %{public}d", bundleName_.c_str(), (int32_t)subscribeId);
84     DeviceManagerFfiImpl *deviceManagerFfi = DeviceManagerFfiImpl::GetDeviceManagerFfi(bundleName_);
85     if (deviceManagerFfi == nullptr) {
86         LOGE("OnDeviceFound, deviceManagerFfi not find for bundleName %{public}s", bundleName_.c_str());
87     } else {
88         deviceManagerFfi->OnDeviceFound(subscribeId, deviceBasicInfo);
89     }
90 }
91 
OnDiscoveryFailed(uint16_t subscribeId,int32_t failedReason)92 void DmFfiDiscoveryCallback::OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason)
93 {
94     LOGI("OnDiscoveryFailed for %{public}s, subscribeId %{public}d", bundleName_.c_str(), (int32_t)subscribeId);
95     DeviceManagerFfiImpl *deviceManagerFfi = DeviceManagerFfiImpl::GetDeviceManagerFfi(bundleName_);
96     if (deviceManagerFfi == nullptr) {
97         LOGE("OnDiscoveryFailed, deviceManagerFfi not find for bundleName %{public}s", bundleName_.c_str());
98     } else {
99         deviceManagerFfi->OnDiscoveryFailed(subscribeId, failedReason);
100     }
101 }
102 
OnDiscoverySuccess(uint16_t subscribeId)103 void DmFfiDiscoveryCallback::OnDiscoverySuccess(uint16_t subscribeId)
104 {
105     DeviceManagerFfiImpl *deviceManagerFfi = DeviceManagerFfiImpl::GetDeviceManagerFfi(bundleName_);
106     if (deviceManagerFfi == nullptr) {
107         LOGE("OnDiscoverySuccess, deviceManagerFfi not find for bundleName %{public}s", bundleName_.c_str());
108         return;
109     }
110     LOGI("DiscoverySuccess for %{public}s, subscribeId %{public}d", bundleName_.c_str(), (int32_t)subscribeId);
111 }
112 
IncreaseRefCount()113 void DmFfiDiscoveryCallback::IncreaseRefCount()
114 {
115     refCount_++;
116 }
117 
DecreaseRefCount()118 void DmFfiDiscoveryCallback::DecreaseRefCount()
119 {
120     refCount_--;
121 }
122 
GetRefCount()123 int32_t DmFfiDiscoveryCallback::GetRefCount()
124 {
125     return refCount_;
126 }
127 
OnPublishResult(int32_t publishId,int32_t publishResult)128 void DmFfiPublishCallback::OnPublishResult(int32_t publishId, int32_t publishResult)
129 {
130     LOGI("OnPublishResult for %{public}s, publishId %{public}d, publishResult %{public}d", bundleName_.c_str(),
131         publishId, publishResult);
132     DeviceManagerFfiImpl *deviceManagerFfi = DeviceManagerFfiImpl::GetDeviceManagerFfi(bundleName_);
133     if (deviceManagerFfi == nullptr) {
134         LOGE("OnPublishResult, deviceManagerFfi failed for bundleName %{public}s", bundleName_.c_str());
135     } else {
136         deviceManagerFfi->OnPublishResult(publishId, publishResult);
137     }
138 }
139 
IncreaseRefCount()140 void DmFfiPublishCallback::IncreaseRefCount()
141 {
142     refCount_++;
143 }
144 
DecreaseRefCount()145 void DmFfiPublishCallback::DecreaseRefCount()
146 {
147     refCount_--;
148 }
149 
GetRefCount()150 int32_t DmFfiPublishCallback::GetRefCount()
151 {
152     return refCount_;
153 }
154 
OnAuthResult(const std::string & deviceId,const std::string & token,int32_t status,int32_t reason)155 void DmFfiAuthenticateCallback::OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status,
156     int32_t reason)
157 {
158     DeviceManagerFfiImpl *deviceManagerFfi = DeviceManagerFfiImpl::GetDeviceManagerFfi(bundleName_);
159     if (deviceManagerFfi == nullptr) {
160         LOGE("OnAuthResult, deviceManagerFfi not find for bundleName %{public}s", bundleName_.c_str());
161     } else {
162         deviceManagerFfi->OnAuthResult(deviceId, token, status, reason);
163     }
164 }
165 
OnCall(const std::string & paramJson)166 void DmFfiDeviceManagerUiCallback::OnCall(const std::string &paramJson)
167 {
168     DeviceManagerFfiImpl *deviceManagerFfi = DeviceManagerFfiImpl::GetDeviceManagerFfi(bundleName_);
169     if (deviceManagerFfi == nullptr) {
170         LOGE("OnCall, deviceManagerFfi not find for bundleName %{public}s", bundleName_.c_str());
171     } else {
172         deviceManagerFfi->OnDmUiCall(paramJson);
173     }
174 }
175 
OnBindResult(const PeerTargetId & targetId,int32_t result,int32_t status,std::string content)176 void DmFfiBindTargetCallback::OnBindResult(const PeerTargetId &targetId, int32_t result, int32_t status,
177     std::string content)
178 {
179     (void)targetId;
180     DeviceManagerFfiImpl *deviceManagerFfi = DeviceManagerFfiImpl::GetDeviceManagerFfi(bundleName_);
181     if (deviceManagerFfi == nullptr) {
182         LOGE("OnBindResult, deviceManagerFfi not find for bundleName %{public}s", bundleName_.c_str());
183     } else {
184         deviceManagerFfi->OnAuthResult(content, "", status, result);
185     }
186 }
187 
GetDeviceTypeById(DmDeviceType type)188 const std::string &GetDeviceTypeById(DmDeviceType type)
189 {
190     const static std::pair<const DmDeviceType, const std::string &> mapArray[] = {
191         {DEVICE_TYPE_UNKNOWN, std::string(DEVICE_TYPE_UNKNOWN_STRING)},
192         {DEVICE_TYPE_PHONE, std::string(DEVICE_TYPE_PHONE_STRING)},
193         {DEVICE_TYPE_PAD, std::string(DEVICE_TYPE_PAD_STRING)},
194         {DEVICE_TYPE_TV, std::string(DEVICE_TYPE_TV_STRING)},
195         {DEVICE_TYPE_CAR, std::string(DEVICE_TYPE_CAR_STRING)},
196         {DEVICE_TYPE_WATCH, std::string(DEVICE_TYPE_WATCH_STRING)},
197         {DEVICE_TYPE_WIFI_CAMERA, std::string(DEVICE_TYPE_WIFICAMERA_STRING)},
198         {DEVICE_TYPE_PC, std::string(DEVICE_TYPE_PC_STRING)},
199         {DEVICE_TYPE_SMART_DISPLAY, std::string(DEVICE_TYPE_SMART_DISPLAY_STRING)},
200         {DEVICE_TYPE_2IN1, std::string(DEVICE_TYPE_2IN1_STRING)},
201     };
202     for (const auto& item : mapArray) {
203         if (item.first == type) {
204             return item.second;
205         }
206     }
207     return mapArray[0].second;
208 }
209 
MallocCStr(const char * in)210 char *MallocCStr(const char *in)
211 {
212     std::size_t len = strlen(in);
213     if (len >= MAX_MALLOC_SIZE) {
214         return nullptr;
215     }
216     char *result = static_cast<char *>(malloc(len + 1));
217     if (result == nullptr) {
218         LOGE("Malloc failed.");
219         return nullptr;
220     }
221     std::char_traits<char>::copy(result, in, len + 1);
222     return result;
223 }
224 } // OHOS::DistributedHardware
225