• 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 
16 #include "distributed_device_manager.h"
17 
18 #include "ans_log_wrapper.h"
19 #include "distributed_extension_service.h"
20 
21 namespace OHOS {
22 namespace Notification {
23 
24 namespace {
25 constexpr char const APP_ID[] = "com.ohos.notification_service.3203";
26 }
27 
OnRemoteDied()28 void DmsInitCallback::OnRemoteDied()
29 {
30     ANS_LOGW("Dms on remote died.");
31 }
32 
OnDeviceOnline(const DmDeviceInfo & deviceInfo)33 void DmsStateCallback::OnDeviceOnline(const DmDeviceInfo &deviceInfo)
34 {
35     ANS_LOGI("AnsDevice online %{public}d, %{public}d, %{public}s, %{public}s", deviceInfo.deviceTypeId,
36         deviceInfo.networkType, StringAnonymous(deviceInfo.deviceId).c_str(),
37         StringAnonymous(deviceInfo.networkId).c_str());
38     DistributedExtensionService::GetInstance().OnDeviceOnline(deviceInfo);
39 }
40 
OnDeviceOffline(const DmDeviceInfo & deviceInfo)41 void DmsStateCallback::OnDeviceOffline(const DmDeviceInfo &deviceInfo)
42 {
43     ANS_LOGI("AnsDevice offline %{public}d, %{public}d, %{public}s, %{public}s", deviceInfo.deviceTypeId,
44         deviceInfo.networkType, StringAnonymous(deviceInfo.deviceId).c_str(),
45         StringAnonymous(deviceInfo.networkId).c_str());
46     DistributedExtensionService::GetInstance().OnDeviceOffline(deviceInfo);
47 }
48 
OnDeviceChanged(const DmDeviceInfo & deviceInfo)49 void DmsStateCallback::OnDeviceChanged(const DmDeviceInfo &deviceInfo)
50 {
51     ANS_LOGI("AnsDevice change %{public}d, %{public}d, %{public}s, %{public}s", deviceInfo.deviceTypeId,
52         deviceInfo.networkType, StringAnonymous(deviceInfo.deviceId).c_str(),
53         StringAnonymous(deviceInfo.networkId).c_str());
54     DistributedExtensionService::GetInstance().OnDeviceChanged(deviceInfo);
55 }
56 
OnDeviceReady(const DmDeviceInfo & deviceInfo)57 void DmsStateCallback::OnDeviceReady(const DmDeviceInfo &deviceInfo)
58 {
59     ANS_LOGI("AnsDevice ready %{public}d, %{public}d, %{public}s, %{public}s", deviceInfo.deviceTypeId,
60         deviceInfo.networkType, StringAnonymous(deviceInfo.deviceId).c_str(),
61         StringAnonymous(deviceInfo.networkId).c_str());
62 }
63 
GetInstance()64 DistributedDeviceManager& DistributedDeviceManager::GetInstance()
65 {
66     static DistributedDeviceManager distributedDeviceManager;
67     return distributedDeviceManager;
68 }
69 
InitTrustList()70 void DistributedDeviceManager::InitTrustList()
71 {
72     if (!RegisterDms(false)) {
73         return;
74     }
75     std::vector<DmDeviceInfo> deviceInfoList;
76     int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(APP_ID, "",
77         true, deviceInfoList);
78     if (ret != 0) {
79         ANS_LOGE("Get trust list failed, ret:%{public}d", ret);
80         return;
81     }
82     for (auto& deviceInfo : deviceInfoList) {
83         ANS_LOGI("AnsDevice trustlist %{public}d, %{public}d, %{public}s, %{public}s", deviceInfo.deviceTypeId,
84             deviceInfo.networkType, StringAnonymous(deviceInfo.deviceId).c_str(),
85             StringAnonymous(deviceInfo.networkId).c_str());
86         DistributedExtensionService::GetInstance().OnDeviceOnline(deviceInfo);
87     }
88 }
89 
RegisterDms(bool forceInit)90 bool DistributedDeviceManager::RegisterDms(bool forceInit)
91 {
92     if (hasInit.load() && !forceInit) {
93         ANS_LOGW("init device manager has inited.");
94         return true;
95     }
96     if (initCallback_ == nullptr) {
97         initCallback_ = std::make_shared<DmsInitCallback>();
98     }
99     int32_t ret = DeviceManager::GetInstance().InitDeviceManager(APP_ID, initCallback_);
100     if (ret != 0) {
101         ANS_LOGE("init device manager failed, ret:%{public}d", ret);
102         return false;
103     }
104 
105     if (stateCallback_ == nullptr) {
106         stateCallback_ = std::make_shared<DmsStateCallback>();
107     }
108     ret = DistributedHardware::DeviceManager::GetInstance().RegisterDevStateCallback(APP_ID, "", stateCallback_);
109     if (ret != 0) {
110         ANS_LOGE("register state callback failed, ret:%{public}d", ret);
111         return false;
112     }
113     hasInit.store(true);
114     ANS_LOGI("Notification distributed register dms successfully.");
115     return true;
116 }
117 }
118 }
119