1 /*
2 * Copyright (c) 2022-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 "devicestatus_service.h"
17
18 #include <vector>
19 #include <ipc_skeleton.h>
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "string_ex.h"
23 #include "system_ability_definition.h"
24 #include "devicestatus_permission.h"
25 #include "devicestatus_common.h"
26 #include "devicestatus_dumper.h"
27 #include "hisysevent.h"
28 #include "hitrace_meter.h"
29
30 namespace OHOS {
31 namespace Msdp {
32 using namespace OHOS::HiviewDFX;
33 namespace {
34 auto ms = DelayedSpSingleton<DevicestatusService>::GetInstance();
35 const bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(ms.GetRefPtr());
36 }
DevicestatusService()37 DevicestatusService::DevicestatusService() : SystemAbility(MSDP_DEVICESTATUS_SERVICE_ID, true)
38 {
39 DEV_HILOGD(SERVICE, "Add SystemAbility");
40 }
41
~DevicestatusService()42 DevicestatusService::~DevicestatusService() {}
43
OnDump()44 void DevicestatusService::OnDump()
45 {
46 DEV_HILOGI(SERVICE, "OnDump");
47 }
48
OnStart()49 void DevicestatusService::OnStart()
50 {
51 DEV_HILOGI(SERVICE, "Enter");
52 if (ready_) {
53 DEV_HILOGE(SERVICE, "OnStart is ready, nothing to do");
54 return;
55 }
56
57 if (!Init()) {
58 DEV_HILOGE(SERVICE, "OnStart call init fail");
59 return;
60 }
61 if (!Publish(DelayedSpSingleton<DevicestatusService>::GetInstance())) {
62 DEV_HILOGE(SERVICE, "OnStart register to system ability manager failed");
63 return;
64 }
65 ready_ = true;
66 DEV_HILOGI(SERVICE, "OnStart and add system ability success");
67 }
68
OnStop()69 void DevicestatusService::OnStop()
70 {
71 DEV_HILOGI(SERVICE, "Enter");
72 if (!ready_) {
73 return;
74 }
75 ready_ = false;
76
77 if (devicestatusManager_ == nullptr) {
78 DEV_HILOGI(SERVICE, "devicestatusManager_ is null");
79 return;
80 }
81 devicestatusManager_->UnloadAlgorithm();
82 DEV_HILOGI(SERVICE, "unload algorithm library exit");
83 }
84
Dump(int fd,const std::vector<std::u16string> & args)85 int DevicestatusService::Dump(int fd, const std::vector<std::u16string>& args)
86 {
87 DEV_HILOGI(SERVICE, "dump DeviceStatusServiceInfo");
88 if (fd < 0) {
89 DEV_HILOGE(SERVICE, "fd is invalid");
90 return RET_ERR;
91 }
92 DevicestatusDumper &deviceStatusDumper = DevicestatusDumper::GetInstance();
93 if (args.empty()) {
94 DEV_HILOGE(SERVICE, "param cannot be empty");
95 dprintf(fd, "param cannot be empty\n");
96 deviceStatusDumper.DumpHelpInfo(fd);
97 return RET_ERR;
98 }
99 std::vector<std::string> argList = { "" };
100 std::transform(args.begin(), args.end(), std::back_inserter(argList),
101 [](const std::u16string &arg) {
102 return Str16ToStr8(arg);
103 });
104
105 DevicestatusDataUtils::DevicestatusType type;
106 std::vector<DevicestatusDataUtils::DevicestatusData> datas;
107 for (type = DevicestatusDataUtils::TYPE_STILL;
108 type <= DevicestatusDataUtils::TYPE_LID_OPEN;
109 type = (DevicestatusDataUtils::DevicestatusType)(type+1)) {
110 DevicestatusDataUtils::DevicestatusData data = GetCache(type);
111 if (data.value != DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID) {
112 datas.emplace_back(data);
113 }
114 }
115 deviceStatusDumper.ParseCommand(fd, argList, datas);
116 return RET_OK;
117 }
118
119
Init()120 bool DevicestatusService::Init()
121 {
122 DEV_HILOGI(SERVICE, "Enter");
123
124 if (!devicestatusManager_) {
125 devicestatusManager_ = std::make_shared<DevicestatusManager>(ms);
126 }
127 if (!devicestatusManager_->Init()) {
128 DEV_HILOGE(SERVICE, "OnStart init fail");
129 return false;
130 }
131
132 return true;
133 }
134
Subscribe(const DevicestatusDataUtils::DevicestatusType & type,const sptr<IdevicestatusCallback> & callback)135 void DevicestatusService::Subscribe(const DevicestatusDataUtils::DevicestatusType& type,
136 const sptr<IdevicestatusCallback>& callback)
137 {
138 DEV_HILOGI(SERVICE, "Enter");
139 if (devicestatusManager_ == nullptr) {
140 DEV_HILOGI(SERVICE, "UnSubscribe func is nullptr");
141 return;
142 }
143
144 auto appInfo = std::make_shared<AppInfo>();
145 appInfo->uid = GetCallingUid();
146 appInfo->pid = GetCallingPid();
147 appInfo->tokenId = GetCallingTokenID();
148 devicestatusManager_->GetPackageName(appInfo->tokenId, appInfo->packageName);
149 appInfo->type = type;
150 appInfo->callback = callback;
151 DevicestatusDumper::GetInstance().SaveAppInfo(appInfo);
152 StartTrace(HITRACE_TAG_MSDP, "serviceSubcribeStart");
153 devicestatusManager_->Subscribe(type, callback);
154 FinishTrace(HITRACE_TAG_MSDP);
155 ReportMsdpSysEvent(type, true);
156 }
157
UnSubscribe(const DevicestatusDataUtils::DevicestatusType & type,const sptr<IdevicestatusCallback> & callback)158 void DevicestatusService::UnSubscribe(const DevicestatusDataUtils::DevicestatusType& type,
159 const sptr<IdevicestatusCallback>& callback)
160 {
161 DEV_HILOGI(SERVICE, "Enter");
162 if (devicestatusManager_ == nullptr) {
163 DEV_HILOGI(SERVICE, "UnSubscribe func is nullptr");
164 return;
165 }
166
167 auto appInfo = std::make_shared<AppInfo>();
168 appInfo->uid = GetCallingUid();
169 appInfo->pid = GetCallingPid();
170 appInfo->tokenId = GetCallingTokenID();
171 devicestatusManager_->GetPackageName(appInfo->tokenId, appInfo->packageName);
172 appInfo->type = type;
173 appInfo->callback = callback;
174 DevicestatusDumper::GetInstance().RemoveAppInfo(appInfo);
175 StartTrace(HITRACE_TAG_MSDP, "serviceUnSubcribeStart");
176 devicestatusManager_->UnSubscribe(type, callback);
177 FinishTrace(HITRACE_TAG_MSDP);
178 ReportMsdpSysEvent(type, false);
179 }
180
GetCache(const DevicestatusDataUtils::DevicestatusType & type)181 DevicestatusDataUtils::DevicestatusData DevicestatusService::GetCache(const \
182 DevicestatusDataUtils::DevicestatusType& type)
183 {
184 DEV_HILOGI(SERVICE, "Enter");
185 if (devicestatusManager_ == nullptr) {
186 DevicestatusDataUtils::DevicestatusData data = {type, DevicestatusDataUtils::DevicestatusValue::VALUE_EXIT};
187 data.value = DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID;
188 DEV_HILOGI(SERVICE, "GetLatestDevicestatusData func is nullptr,return default!");
189 return data;
190 }
191 return devicestatusManager_->GetLatestDevicestatusData(type);
192 }
193
ReportMsdpSysEvent(const DevicestatusDataUtils::DevicestatusType & type,bool enable)194 void DevicestatusService::ReportMsdpSysEvent(const DevicestatusDataUtils::DevicestatusType& type, bool enable)
195 {
196 auto uid = this->GetCallingUid();
197 auto callerToken = this->GetCallingTokenID();
198 std::string packageName("");
199 devicestatusManager_->GetPackageName(callerToken, packageName);
200 std::string message;
201 if (enable) {
202 HiSysEvent::Write(HiSysEvent::Domain::MSDP, "SUBSCRIBE", HiSysEvent::EventType::STATISTIC,
203 "UID", uid, "PKGNAME", packageName, "TYPE", type);
204 return;
205 }
206 HiSysEvent::Write(HiSysEvent::Domain::MSDP, "UNSUBSCRIBE", HiSysEvent::EventType::STATISTIC,
207 "UID", uid, "PKGNAME", packageName, "TYPE", type);
208 }
209 } // namespace Msdp
210 } // namespace OHOS
211