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 "device_manager_agent.h"
17 #include "dfsu_exception.h"
18 #include "distributedfile_service.h"
19 #include "softbus_agent.h"
20 #include "utils_log.h"
21
22 namespace OHOS {
23 namespace Storage {
24 namespace DistributedFile {
DeviceManagerAgent()25 DeviceManagerAgent::DeviceManagerAgent() {}
26
~DeviceManagerAgent()27 DeviceManagerAgent::~DeviceManagerAgent()
28 {
29 StopInstance();
30 }
StartInstance()31 void DeviceManagerAgent::StartInstance()
32 {
33 // the time sequence can ensure there is no resource competition
34 alreadyOnlineDev_.clear();
35 try {
36 RegisterToExternalDm();
37 } catch (const DfsuException &e) {
38 // do not throw exception
39 } catch (const std::exception &e) {
40 // do not throw exception
41 }
42 }
43
StopInstance()44 void DeviceManagerAgent::StopInstance()
45 {
46 try {
47 UnregisterFromExternalDm();
48 } catch (const DfsuException &e) {
49 LOGE("Unregister devicemagager callback failed.");
50 } catch (const std::exception &e) {
51 LOGE("Unexpected Low Level exception");
52 }
53 }
54
OnDeviceOnline(const DistributedHardware::DmDeviceInfo & deviceInfo)55 void DeviceManagerAgent::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo)
56 {
57 std::unique_lock<std::mutex> lock(devsRecordMutex_);
58 std::string cid = std::string(deviceInfo.deviceId);
59 alreadyOnlineDev_.insert(cid);
60 SoftbusAgent::GetInstance()->OnDeviceOnline(cid);
61 }
62
OnDeviceOffline(const DistributedHardware::DmDeviceInfo & deviceInfo)63 void DeviceManagerAgent::OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo)
64 {
65 std::unique_lock<std::mutex> lock(devsRecordMutex_);
66 std::string cid = std::string(deviceInfo.deviceId);
67 auto softBusAgent = SoftbusAgent::GetInstance();
68 softBusAgent->OnDeviceOffline(cid);
69
70 alreadyOnlineDev_.erase(cid);
71 LOGI("cid %{public}s offline, left online devices num %{public}d", cid.c_str(), alreadyOnlineDev_.size());
72 }
73
OnRemoteDied()74 void DeviceManagerAgent::OnRemoteDied()
75 {
76 LOGI("device manager service died");
77 try {
78 UnregisterFromExternalDm();
79 RegisterToExternalDm();
80 } catch (const DfsuException &e) {
81 LOGE("Reregister devicemagager callback failed.");
82 } catch (const std::exception &e) {
83 LOGE("Unexpected Low Level exception");
84 }
85 }
86
RegisterToExternalDm()87 void DeviceManagerAgent::RegisterToExternalDm()
88 {
89 auto &deviceManager = DistributedHardware::DeviceManager::GetInstance();
90 int errCode = deviceManager.InitDeviceManager(DistributedFileService::pkgName_, shared_from_this());
91 if (errCode != 0) {
92 ThrowException(errCode, "Failed to InitDeviceManager");
93 }
94 std::string extra = "";
95 errCode = deviceManager.RegisterDevStateCallback(DistributedFileService::pkgName_, extra, shared_from_this());
96 if (errCode != 0) {
97 ThrowException(errCode, "Failed to RegisterDevStateCallback");
98 }
99 LOGI("RegisterToExternalDm Succeed");
100 }
101
UnregisterFromExternalDm()102 void DeviceManagerAgent::UnregisterFromExternalDm()
103 {
104 auto &deviceManager = DistributedHardware::DeviceManager::GetInstance();
105 int errCode = deviceManager.UnRegisterDevStateCallback(DistributedFileService::pkgName_);
106 if (errCode != 0) {
107 ThrowException(errCode, "Failed to UnRegisterDevStateCallback");
108 }
109 errCode = deviceManager.UnInitDeviceManager(DistributedFileService::pkgName_);
110 if (errCode != 0) {
111 ThrowException(errCode, "Failed to UnInitDeviceManager");
112 }
113 LOGI("UnregisterFromExternalDm Succeed");
114 }
115 } // namespace DistributedFile
116 } // namespace Storage
117 } // namespace OHOS