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