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