• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "boomerang_server.h"
17 
18 #include "hisysevent.h"
19 #include "hitrace_meter.h"
20 #include "tokenid_kit.h"
21 #include "accesstoken_kit.h"
22 
23 #include "default_params.h"
24 #include "devicestatus_define.h"
25 #include "devicestatus_dumper.h"
26 #include "devicestatus_hisysevent.h"
27 #include "boomerang_params.h"
28 
29 #undef LOG_TAG
30 #define LOG_TAG "BoomerangServer"
31 
32 namespace OHOS {
33 namespace Msdp {
34 namespace DeviceStatus {
35 
BoomerangServer()36 BoomerangServer::BoomerangServer()
37 {
38     manager_.Init();
39 }
40 
Enable(CallingContext & context,MessageParcel & data,MessageParcel & reply)41 int32_t BoomerangServer::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 BoomerangServer::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 BoomerangServer::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 BoomerangServer::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 BoomerangServer::AddWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
66 {
67     switch (id) {
68         case BoomerangRequestID::ADD_BOOMERAMG_LISTENER: {
69             return Subscribe(context, data);
70         }
71         case BoomerangRequestID::NOTIFY_METADATA: {
72             return NotifyMetadataBindingEvent(context, data);
73         }
74         case BoomerangRequestID::ENCODE_IMAGE: {
75             return BoomerangEncodeImage(context, data);
76         }
77         case BoomerangRequestID::DECODE_IMAGE: {
78             return BoomerangDecodeImage(context, data);
79         }
80         default: {
81             FI_HILOGE("Unexpected request ID (%{public}u)", id);
82             return RET_ERR;
83         }
84     }
85 }
86 
RemoveWatch(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)87 int32_t BoomerangServer::RemoveWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
88 {
89     switch (id) {
90         case BoomerangRequestID::REMOVE_BOOMERAMG_LISTENER: {
91             return Unsubscribe(context, data);
92         }
93         default: {
94             FI_HILOGE("Unexpected request ID (%{public}u)", id);
95             return RET_ERR;
96         }
97     }
98 }
99 
SetParam(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)100 int32_t BoomerangServer::SetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
101 {
102     CALL_DEBUG_ENTER;
103     switch (id) {
104         case BoomerangRequestID::SUBMIT_METADATA: {
105             return SubmitMetadata(context, data);
106         }
107         default: {
108             FI_HILOGE("Unexpected request ID (%{public}u)", id);
109             return RET_ERR;
110         }
111     }
112     return RET_ERR;
113 }
114 
GetParam(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)115 int32_t BoomerangServer::GetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
116 {
117     CALL_DEBUG_ENTER;
118     return RET_OK;
119 }
120 
Control(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)121 int32_t BoomerangServer::Control(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
122 {
123     CALL_DEBUG_ENTER;
124     return RET_ERR;
125 }
126 
DumpDeviceStatusSubscriber(int32_t fd) const127 void BoomerangServer::DumpDeviceStatusSubscriber(int32_t fd) const
128 {
129     DS_DUMPER->DumpDeviceStatusSubscriber(fd);
130 }
131 
DumpDeviceStatusChanges(int32_t fd) const132 void BoomerangServer::DumpDeviceStatusChanges(int32_t fd) const
133 {
134     DS_DUMPER->DumpDeviceStatusChanges(fd);
135 }
136 
DumpCurrentDeviceStatus(int32_t fd)137 void BoomerangServer::DumpCurrentDeviceStatus(int32_t fd)
138 {
139     std::vector<Data> datas;
140 
141     for (auto type = TYPE_ABSOLUTE_STILL; type <= TYPE_LID_OPEN; type = static_cast<Type>(type + 1)) {
142         Data data = manager_.GetLatestDeviceStatusData(type);
143         if (data.value != OnChangedValue::VALUE_INVALID) {
144             datas.emplace_back(data);
145         }
146     }
147     DS_DUMPER->DumpDeviceStatusCurrentStatus(fd, datas);
148 }
149 
Subscribe(CallingContext & context,MessageParcel & data)150 int32_t BoomerangServer::Subscribe(CallingContext &context, MessageParcel &data)
151 {
152     SubscribeBoomerangParam param {};
153     if (!param.Unmarshalling(data)) {
154         FI_HILOGE("SubscribeStationaryParam::Unmarshalling fail");
155         return RET_ERR;
156     }
157     auto appInfo = std::make_shared<BoomerangAppInfo>();
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 = param.type_;
163     appInfo->boomerangCallback = param.callback_;
164     DS_DUMPER->SaveBoomerangAppInfo(appInfo);
165     manager_.Subscribe(param.type_, param.bundleName_, param.callback_);
166     return RET_OK;
167 }
168 
Unsubscribe(CallingContext & context,MessageParcel & data)169 int32_t BoomerangServer::Unsubscribe(CallingContext &context, MessageParcel &data)
170 {
171     SubscribeBoomerangParam param {};
172     if (!param.Unmarshalling(data)) {
173         FI_HILOGE("UnsubscribeStationaryParam::Unmarshalling fail");
174         return RET_ERR;
175     }
176     auto appInfo = std::make_shared<BoomerangAppInfo>();
177     appInfo->uid = context.uid;
178     appInfo->pid = context.pid;
179     appInfo->tokenId = context.tokenId;
180     appInfo->packageName = DS_DUMPER->GetPackageName(appInfo->tokenId);
181     appInfo->type = param.type_;
182     appInfo->boomerangCallback = param.callback_;
183     DS_DUMPER->RemoveBoomerangAppInfo(appInfo);
184     manager_.Unsubscribe(param.type_, param.bundleName_, param.callback_);
185     return RET_OK;
186 }
187 
NotifyMetadataBindingEvent(CallingContext & context,MessageParcel & data)188 int32_t BoomerangServer::NotifyMetadataBindingEvent(CallingContext &context, MessageParcel &data)
189 {
190     if (!IsSystemHAPCalling(context)) {
191         FI_HILOGE("The caller is not system hap");
192         return COMMON_NOT_SYSTEM_APP;
193     }
194     NotifyMetadataParam param {};
195     if (!param.Unmarshalling(data)) {
196         FI_HILOGE("GetNotifyMetadataParam::Unmarshalling fail");
197         return RET_ERR;
198     }
199     auto appInfo = std::make_shared<BoomerangAppInfo>();
200     appInfo->uid = context.uid;
201     appInfo->pid = context.pid;
202     appInfo->tokenId = context.tokenId;
203     appInfo->packageName = DS_DUMPER->GetPackageName(appInfo->tokenId);
204     appInfo->boomerangCallback = param.callback_;
205     DS_DUMPER->SetNotifyMetadatAppInfo(appInfo);
206     return manager_.NotifyMedata(param.bundleName_, param.callback_);
207 }
208 
BoomerangEncodeImage(CallingContext & context,MessageParcel & data)209 int32_t BoomerangServer::BoomerangEncodeImage(CallingContext &context, MessageParcel &data)
210 {
211     EncodeImageParam param {};
212     if (!IsSystemHAPCalling(context)) {
213         FI_HILOGE("The caller is not system hap");
214         return COMMON_NOT_SYSTEM_APP;
215     }
216     if (!param.Unmarshalling(data)) {
217         FI_HILOGE("GetNotifyMetadataParam::Unmarshalling fail");
218         return RET_ERR;
219     }
220     auto appInfo = std::make_shared<BoomerangAppInfo>();
221     appInfo->uid = context.uid;
222     appInfo->pid = context.pid;
223     appInfo->tokenId = context.tokenId;
224     appInfo->packageName = DS_DUMPER->GetPackageName(appInfo->tokenId);
225     appInfo->boomerangCallback = param.callback_;
226     DS_DUMPER->SetNotifyMetadatAppInfo(appInfo);
227     manager_.BoomerangEncodeImage(param.pixelMap_, param.metadata_, param.callback_);
228     return RET_OK;
229 }
230 
BoomerangDecodeImage(CallingContext & context,MessageParcel & data)231 int32_t BoomerangServer::BoomerangDecodeImage(CallingContext &context, MessageParcel &data)
232 {
233     DecodeImageParam param {};
234     if (!IsSystemHAPCalling(context)) {
235         FI_HILOGE("The caller is not system hap");
236         return COMMON_NOT_SYSTEM_APP;
237     }
238     if (!param.Unmarshalling(data)) {
239         FI_HILOGE("GetNotifyMetadataParam::Unmarshalling fail");
240         return RET_ERR;
241     }
242     auto appInfo = std::make_shared<BoomerangAppInfo>();
243     appInfo->uid = context.uid;
244     appInfo->pid = context.pid;
245     appInfo->tokenId = context.tokenId;
246     appInfo->packageName = DS_DUMPER->GetPackageName(appInfo->tokenId);
247     appInfo->boomerangCallback = param.callback_;
248     DS_DUMPER->SetNotifyMetadatAppInfo(appInfo);
249     manager_.BoomerangDecodeImage(param.pixelMap_, param.callback_);
250     return RET_OK;
251 }
252 
SubmitMetadata(CallingContext & context,MessageParcel & data)253 int32_t BoomerangServer::SubmitMetadata(CallingContext &context, MessageParcel &data)
254 {
255     MetadataParam param {};
256     if (!param.Unmarshalling(data)) {
257         FI_HILOGE("GetNotifyMetadataParam::Unmarshalling fail");
258         return RET_ERR;
259     }
260     manager_.SubmitMetadata(param.metadata_);
261     return RET_OK;
262 }
263 
GetCache(CallingContext & context,const Type & type)264 Data BoomerangServer::GetCache(CallingContext &context, const Type &type)
265 {
266     return manager_.GetLatestDeviceStatusData(type);
267 }
268 
ReportSensorSysEvent(CallingContext & context,int32_t type,bool enable)269 void BoomerangServer::ReportSensorSysEvent(CallingContext &context, int32_t type, bool enable)
270 {
271     std::string packageName;
272     manager_.GetPackageName(context.tokenId, packageName);
273     int32_t ret = HiSysEventWrite(
274         OHOS::HiviewDFX::HiSysEvent::Domain::MSDP,
275         std::string(enable ? "Subscribe" : "Unsubscribe"),
276         OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
277         "UID", context.uid,
278         "PKGNAME", packageName,
279         "TYPE", type);
280     if (ret != 0) {
281         FI_HILOGE("HiviewDFX write failed, error:%{public}d", ret);
282     }
283 }
284 
IsSystemServiceCalling(CallingContext & context)285 bool BoomerangServer::IsSystemServiceCalling(CallingContext &context)
286 {
287     auto flag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(context.tokenId);
288     if ((flag == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) ||
289         (flag == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL)) {
290         FI_HILOGI("system service calling, flag:%{public}u", flag);
291         return true;
292     }
293     return false;
294 }
295 
IsSystemHAPCalling(CallingContext & context)296 bool BoomerangServer::IsSystemHAPCalling(CallingContext &context)
297 {
298     if (IsSystemServiceCalling(context)) {
299         return true;
300     }
301     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(context.fullTokenId);
302 }
303 } // namespace DeviceStatus
304 } // namespace Msdp
305 } // namespace OHOS