• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "ipc/daemon.h"
17 
18 #include <exception>
19 #include <stdexcept>
20 
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "device/device_manager_agent.h"
24 #include "iremote_object.h"
25 #include "mountpoint/mount_manager.h"
26 #include "system_ability_definition.h"
27 #include "utils_log.h"
28 
29 namespace OHOS {
30 namespace Storage {
31 namespace DistributedFile {
32 using namespace std;
33 
34 REGISTER_SYSTEM_ABILITY_BY_ID(Daemon, FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID, true);
35 
PublishSA()36 void Daemon::PublishSA()
37 {
38     LOGI("Begin to init");
39     if (!registerToService_) {
40         bool ret = SystemAbility::Publish(this);
41         if (!ret) {
42             throw runtime_error("Failed to publish the daemon");
43         }
44         registerToService_ = true;
45     }
46     LOGI("Init finished successfully");
47 }
48 
RegisterOsAccount()49 void Daemon::RegisterOsAccount()
50 {
51     EventFwk::MatchingSkills matchingSkills;
52     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
53     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
54     subScriber_ = std::make_shared<OsAccountObserver>(subscribeInfo);
55     bool subRet = EventFwk::CommonEventManager::SubscribeCommonEvent(subScriber_);
56     if (!subRet) {
57         LOGE("Subscribe common event failed");
58     }
59 }
60 
OnStart()61 void Daemon::OnStart()
62 {
63     LOGI("Begin to start service");
64     if (state_ == ServiceRunningState::STATE_RUNNING) {
65         LOGD("Daemon has already started");
66         return;
67     }
68 
69     try {
70         PublishSA();
71         AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
72     } catch (const exception &e) {
73         LOGE("%{public}s", e.what());
74     }
75 
76     state_ = ServiceRunningState::STATE_RUNNING;
77     LOGI("Start service successfully");
78 }
79 
OnStop()80 void Daemon::OnStop()
81 {
82     LOGI("Begin to stop");
83     state_ = ServiceRunningState::STATE_NOT_START;
84     registerToService_ = false;
85     bool subRet = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subScriber_);
86     if (!subRet) {
87         LOGE("UnSubscribe common event failed");
88     }
89     subScriber_ = nullptr;
90     LOGI("Stop finished successfully");
91 }
92 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)93 void Daemon::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
94 {
95     (void)systemAbilityId;
96     (void)deviceId;
97     RegisterOsAccount();
98 }
99 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)100 void Daemon::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
101 {
102     (void)deviceId;
103     if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
104         LOGE("systemAbilityId is not COMMON_EVENT_SERVICE_ID");
105         return;
106     }
107 
108     if (subScriber_ == nullptr) {
109         LOGE("Daemon::OnRemoveSystemAbility subscriberPtr is nullptr");
110         return;
111     }
112 
113     bool subscribeResult = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subScriber_);
114     LOGI("Daemon::OnRemoveSystemAbility subscribeResult = %{public}d", subscribeResult);
115     subScriber_ = nullptr;
116 }
117 
OpenP2PConnection(const DistributedHardware::DmDeviceInfo & deviceInfo)118 int32_t Daemon::OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo)
119 {
120     LOGI("Open P2P Connection");
121     std::thread([=]() {
122         int32_t ret = DeviceManagerAgent::GetInstance()->OnDeviceP2POnline(deviceInfo);
123         LOGI("Open P2P Connection result %d", ret);
124         return ret;
125         }).detach();
126     return 0;
127 }
128 
CloseP2PConnection(const DistributedHardware::DmDeviceInfo & deviceInfo)129 int32_t Daemon::CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo)
130 {
131     LOGI("Close P2P Connection");
132     std::thread([=]() {
133         int32_t ret = DeviceManagerAgent::GetInstance()->OnDeviceP2POffline(deviceInfo);
134         LOGI("Close P2P Connection result %d", ret);
135         return ret;
136         }).detach();
137     return 0;
138 }
139 } // namespace DistributedFile
140 } // namespace Storage
141 } // namespace OHOS