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 "stationary_server.h"
17
18 #include "hisysevent.h"
19 #include "hitrace_meter.h"
20
21 #include "default_params.h"
22 #include "devicestatus_define.h"
23 #include "devicestatus_dumper.h"
24 #include "devicestatus_hisysevent.h"
25 #include "stationary_params.h"
26
27 #undef LOG_TAG
28 #define LOG_TAG "StationaryServer"
29
30 namespace OHOS {
31 namespace Msdp {
32 namespace DeviceStatus {
33
StationaryServer()34 StationaryServer::StationaryServer()
35 {
36 manager_.Init();
37 }
38
Enable(CallingContext & context,MessageParcel & data,MessageParcel & reply)39 int32_t StationaryServer::Enable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
40 {
41 CALL_DEBUG_ENTER;
42 return RET_ERR;
43 }
44
Disable(CallingContext & context,MessageParcel & data,MessageParcel & reply)45 int32_t StationaryServer::Disable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
46 {
47 CALL_DEBUG_ENTER;
48 return RET_ERR;
49 }
50
Start(CallingContext & context,MessageParcel & data,MessageParcel & reply)51 int32_t StationaryServer::Start(CallingContext &context, MessageParcel &data, MessageParcel &reply)
52 {
53 CALL_DEBUG_ENTER;
54 return RET_ERR;
55 }
56
Stop(CallingContext & context,MessageParcel & data,MessageParcel & reply)57 int32_t StationaryServer::Stop(CallingContext &context, MessageParcel &data, MessageParcel &reply)
58 {
59 CALL_DEBUG_ENTER;
60 return RET_ERR;
61 }
62
AddWatch(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)63 int32_t StationaryServer::AddWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
64 {
65 if (id != StationaryRequestID::SUBSCRIBE_STATIONARY) {
66 FI_HILOGE("Unexpected request ID (%{public}u)", id);
67 return RET_ERR;
68 }
69 SubscribeStationaryParam param {};
70
71 if (!param.Unmarshalling(data)) {
72 FI_HILOGE("SubscribeStationaryParam::Unmarshalling fail");
73 return RET_ERR;
74 }
75 Subscribe(context, param.type_, param.event_, param.latency_, param.callback_);
76 return RET_OK;
77 }
78
RemoveWatch(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)79 int32_t StationaryServer::RemoveWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
80 {
81 if (id != StationaryRequestID::UNSUBSCRIBE_STATIONARY) {
82 FI_HILOGE("Unexpected request ID (%{public}u)", id);
83 return RET_ERR;
84 }
85 UnsubscribeStationaryParam param {};
86
87 if (!param.Unmarshalling(data)) {
88 FI_HILOGE("UnsubscribeStationaryParam::Unmarshalling fail");
89 return RET_ERR;
90 }
91 Unsubscribe(context, param.type_, param.event_, param.callback_);
92 return RET_OK;
93 }
94
SetParam(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)95 int32_t StationaryServer::SetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
96 {
97 CALL_DEBUG_ENTER;
98 return RET_ERR;
99 }
100
GetParam(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)101 int32_t StationaryServer::GetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
102 {
103 if (id != StationaryRequestID::GET_STATIONARY_DATA) {
104 FI_HILOGE("Unexpected request ID (%{public}u)", id);
105 return RET_ERR;
106 }
107 GetStaionaryDataParam param {};
108
109 if (!param.Unmarshalling(data)) {
110 FI_HILOGE("GetStaionaryDataParam::Unmarshalling fail");
111 return RET_ERR;
112 }
113 GetStaionaryDataReply dataReply { GetCache(context, param.type_) };
114 if (!dataReply.Marshalling(reply)) {
115 FI_HILOGE("GetStaionaryDataReply::Marshalling fail");
116 return RET_ERR;
117 }
118 return RET_OK;
119 }
120
Control(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)121 int32_t StationaryServer::Control(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
122 {
123 CALL_DEBUG_ENTER;
124 return RET_ERR;
125 }
126
Subscribe(CallingContext & context,Type type,ActivityEvent event,ReportLatencyNs latency,sptr<IRemoteDevStaCallback> callback)127 void StationaryServer::Subscribe(CallingContext &context, Type type, ActivityEvent event,
128 ReportLatencyNs latency, sptr<IRemoteDevStaCallback> callback)
129 {
130 FI_HILOGI("SubscribeStationary(type:%{public}d,event:%{public}d,latency:%{public}d)", type, event, latency);
131 auto appInfo = std::make_shared<AppInfo>();
132 appInfo->uid = context.uid;
133 appInfo->pid = context.pid;
134 appInfo->tokenId = context.tokenId;
135 manager_.GetPackageName(appInfo->tokenId, appInfo->packageName);
136 appInfo->type = type;
137 appInfo->callback = callback;
138 DS_DUMPER->SaveAppInfo(appInfo);
139 StartTrace(HITRACE_TAG_MSDP, "serviceSubscribeStart");
140 manager_.Subscribe(type, event, latency, callback);
141 FinishTrace(HITRACE_TAG_MSDP);
142 ReportSensorSysEvent(context, type, true);
143 WriteSubscribeHiSysEvent(appInfo->uid, appInfo->packageName, type);
144 }
145
Unsubscribe(CallingContext & context,Type type,ActivityEvent event,sptr<IRemoteDevStaCallback> callback)146 void StationaryServer::Unsubscribe(CallingContext &context, Type type,
147 ActivityEvent event, sptr<IRemoteDevStaCallback> callback)
148 {
149 FI_HILOGI("UnsubscribeStationary(type:%{public}d,event:%{public}d)", type, event);
150 auto appInfo = std::make_shared<AppInfo>();
151 appInfo->uid = context.uid;
152 appInfo->pid = context.pid;
153 appInfo->tokenId = context.tokenId;
154 appInfo->packageName = DS_DUMPER->GetPackageName(appInfo->tokenId);
155 appInfo->type = type;
156 appInfo->callback = callback;
157 DS_DUMPER->RemoveAppInfo(appInfo);
158 StartTrace(HITRACE_TAG_MSDP, "serviceUnSubscribeStart");
159 manager_.Unsubscribe(type, event, callback);
160 FinishTrace(HITRACE_TAG_MSDP);
161 ReportSensorSysEvent(context, type, false);
162 WriteUnSubscribeHiSysEvent(appInfo->uid, appInfo->packageName, type);
163 }
164
GetCache(CallingContext & context,const Type & type)165 Data StationaryServer::GetCache(CallingContext &context, const Type &type)
166 {
167 return manager_.GetLatestDeviceStatusData(type);
168 }
169
ReportSensorSysEvent(CallingContext & context,int32_t type,bool enable)170 void StationaryServer::ReportSensorSysEvent(CallingContext &context, int32_t type, bool enable)
171 {
172 std::string packageName;
173 manager_.GetPackageName(context.tokenId, packageName);
174 int32_t ret = HiSysEventWrite(
175 OHOS::HiviewDFX::HiSysEvent::Domain::MSDP,
176 std::string(enable ? "Subscribe" : "Unsubscribe"),
177 OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
178 "UID", context.uid,
179 "PKGNAME", packageName,
180 "TYPE", type);
181 if (ret != 0) {
182 FI_HILOGE("HiviewDFX write failed, error:%{public}d", ret);
183 }
184 }
185 } // namespace DeviceStatus
186 } // namespace Msdp
187 } // namespace OHOS