• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_hardware_service.h"
17 
18 #include "if_system_ability_manager.h"
19 #include "ipc_skeleton.h"
20 #include "ipc_types.h"
21 #include "iservice_registry.h"
22 #include "string_ex.h"
23 #include "system_ability_definition.h"
24 
25 #include "access_manager.h"
26 #include "dh_utils_hisysevent.h"
27 #include "distributed_hardware_errno.h"
28 #include "distributed_hardware_log.h"
29 #include "distributed_hardware_manager_factory.h"
30 #include "publisher.h"
31 
32 namespace OHOS {
33 namespace DistributedHardware {
34 REGISTER_SYSTEM_ABILITY_BY_ID(DistributedHardwareService, DISTRIBUTED_HARDWARE_SA_ID, true);
35 
DistributedHardwareService(int32_t saId,bool runOnCreate)36 DistributedHardwareService::DistributedHardwareService(int32_t saId, bool runOnCreate)
37     : SystemAbility(saId, runOnCreate)
38 {
39 }
40 
OnStart()41 void DistributedHardwareService::OnStart()
42 {
43     DHLOGI("DistributedHardwareService::OnStart start");
44     HiSysEventWriteMsg(DHFWK_INIT_BEGIN, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
45         "dhfwk sa start on demand.");
46 
47     if (state_ == ServiceRunningState::STATE_RUNNING) {
48         DHLOGI("DistributedHardwareService has already started.");
49         return;
50     }
51     if (!Init()) {
52         DHLOGE("failed to init DistributedHardwareService");
53         return;
54     }
55     state_ = ServiceRunningState::STATE_RUNNING;
56     DHLOGI("DistributedHardwareService::OnStart start service success.");
57 }
58 
Init()59 bool DistributedHardwareService::Init()
60 {
61     DHLOGI("DistributedHardwareService::Init ready to init.");
62     if (!registerToService_) {
63         bool ret = Publish(this);
64         if (!ret) {
65             DHLOGE("DistributedHardwareService::Init Publish failed!");
66             HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
67                 "dhfwk sa init publish failed.");
68             return false;
69         }
70         registerToService_ = true;
71     }
72     auto ret = AccessManager::GetInstance()->Init();
73     if (ret != DH_FWK_SUCCESS) {
74         DHLOGE("DistributedHardwareService::Init failed.");
75         HiSysEventWriteErrCodeMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
76             ret, "dhfwk sa AccessManager init fail.");
77         return false;
78     }
79     DHLOGI("DistributedHardwareService::Init init success.");
80     HiSysEventWriteMsg(DHFWK_INIT_END, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
81         "dhfwk sa init success.");
82     return true;
83 }
84 
OnStop()85 void DistributedHardwareService::OnStop()
86 {
87     DHLOGI("DistributedHardwareService::OnStop ready to stop service.");
88     state_ = ServiceRunningState::STATE_NOT_START;
89     registerToService_ = false;
90 }
91 
RegisterPublisherListener(const DHTopic topic,const sptr<IPublisherListener> & listener)92 int32_t DistributedHardwareService::RegisterPublisherListener(const DHTopic topic,
93     const sptr<IPublisherListener> &listener)
94 {
95     Publisher::GetInstance().RegisterListener(topic, listener);
96     return DH_FWK_SUCCESS;
97 }
98 
UnregisterPublisherListener(const DHTopic topic,const sptr<IPublisherListener> & listener)99 int32_t DistributedHardwareService::UnregisterPublisherListener(const DHTopic topic,
100     const sptr<IPublisherListener> &listener)
101 {
102     Publisher::GetInstance().UnregisterListener(topic, listener);
103     return DH_FWK_SUCCESS;
104 }
105 
PublishMessage(const DHTopic topic,const std::string & msg)106 int32_t DistributedHardwareService::PublishMessage(const DHTopic topic, const std::string &msg)
107 {
108     Publisher::GetInstance().PublishMessage(topic, msg);
109     return DH_FWK_SUCCESS;
110 }
111 
Dump(int32_t fd,const std::vector<std::u16string> & args)112 int DistributedHardwareService::Dump(int32_t fd, const std::vector<std::u16string>& args)
113 {
114     DHLOGI("DistributedHardwareService  Dump.");
115 
116     std::vector<std::string> argsStr {};
117     for (auto item : args) {
118         argsStr.emplace_back(Str16ToStr8(item));
119     }
120 
121     std::string result("");
122     int ret = AccessManager::GetInstance()->Dump(argsStr, result);
123     if (ret != DH_FWK_SUCCESS) {
124         DHLOGE("Dump error, ret = %d", ret);
125     }
126 
127     if (dprintf(fd, "%s\n", result.c_str()) < 0) {
128         DHLOGE("Hidump dprintf error");
129         ret = ERR_DH_FWK_HIDUMP_DPRINTF_ERROR;
130     }
131 
132     return ret;
133 }
134 } // namespace DistributedHardware
135 } // namespace OHOS
136