• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 "devicestatus_define.h"
24 #include "devicestatus_dumper.h"
25 #include "devicestatus_hisysevent.h"
26 
27 #undef LOG_TAG
28 #define LOG_TAG "BoomerangServer"
29 
30 namespace OHOS {
31 namespace Msdp {
32 namespace DeviceStatus {
33 
BoomerangServer()34 BoomerangServer::BoomerangServer()
35 {
36     manager_.Init();
37 }
38 
DumpDeviceStatusSubscriber(int32_t fd) const39 void BoomerangServer::DumpDeviceStatusSubscriber(int32_t fd) const
40 {
41     DS_DUMPER->DumpDeviceStatusSubscriber(fd);
42 }
43 
DumpDeviceStatusChanges(int32_t fd) const44 void BoomerangServer::DumpDeviceStatusChanges(int32_t fd) const
45 {
46     DS_DUMPER->DumpDeviceStatusChanges(fd);
47 }
48 
DumpCurrentDeviceStatus(int32_t fd)49 void BoomerangServer::DumpCurrentDeviceStatus(int32_t fd)
50 {
51     std::vector<Data> datas;
52 
53     for (auto type = TYPE_ABSOLUTE_STILL; type <= TYPE_LID_OPEN; type = static_cast<Type>(type + 1)) {
54         Data data = manager_.GetLatestDeviceStatusData(type);
55         if (data.value != OnChangedValue::VALUE_INVALID) {
56             datas.emplace_back(data);
57         }
58     }
59     DS_DUMPER->DumpDeviceStatusCurrentStatus(fd, datas);
60 }
61 
SubscribeCallback(CallingContext & context,int32_t type,const std::string & bundleName,const sptr<IRemoteBoomerangCallback> & subCallback)62 int32_t BoomerangServer::SubscribeCallback(CallingContext &context, int32_t type, const std::string& bundleName,
63     const sptr<IRemoteBoomerangCallback>& subCallback)
64 {
65     CHKPR(subCallback, RET_ERR);
66     BoomerangType boomerangType = static_cast<BoomerangType>(type);
67     auto appInfo = std::make_shared<BoomerangAppInfo>();
68     appInfo->uid = context.uid;
69     appInfo->pid = context.pid;
70     appInfo->tokenId = context.tokenId;
71     manager_.GetPackageName(appInfo->tokenId, appInfo->packageName);
72     appInfo->type = boomerangType;
73     appInfo->boomerangCallback = subCallback;
74     DS_DUMPER->SaveBoomerangAppInfo(appInfo);
75     int32_t ret = manager_.Subscribe(type, bundleName, subCallback);
76     if (ret != RET_OK) {
77         FI_HILOGE("boomerang Subscribe failed");
78         DS_DUMPER->RemoveBoomerangAppInfo(appInfo);
79     }
80     return ret;
81 }
82 
UnsubscribeCallback(CallingContext & context,int32_t type,const std::string & bundleName,const sptr<IRemoteBoomerangCallback> & unsubCallback)83 int32_t BoomerangServer::UnsubscribeCallback(CallingContext &context, int32_t type, const std::string& bundleName,
84     const sptr<IRemoteBoomerangCallback>& unsubCallback)
85 {
86     CHKPR(unsubCallback, RET_ERR);
87     BoomerangType boomerangType = static_cast<BoomerangType>(type);
88     auto appInfo = std::make_shared<BoomerangAppInfo>();
89     appInfo->uid = context.uid;
90     appInfo->pid = context.pid;
91     appInfo->tokenId = context.tokenId;
92     appInfo->packageName = DS_DUMPER->GetPackageName(appInfo->tokenId);
93     appInfo->type = boomerangType;
94     appInfo->boomerangCallback = unsubCallback;
95     DS_DUMPER->RemoveBoomerangAppInfo(appInfo);
96     int32_t ret = manager_.Unsubscribe(type, bundleName, unsubCallback);
97     if (ret != RET_OK) {
98         FI_HILOGE("boomerang Unsubscribe failed");
99         DS_DUMPER->SaveBoomerangAppInfo(appInfo);
100     }
101     return ret;
102 }
103 
NotifyMetadataBindingEvent(CallingContext & context,const std::string & bundleName,const sptr<IRemoteBoomerangCallback> & notifyCallback)104 int32_t BoomerangServer::NotifyMetadataBindingEvent(CallingContext &context, const std::string& bundleName,
105     const sptr<IRemoteBoomerangCallback>& notifyCallback)
106 {
107     if (!IsSystemHAPCalling(context)) {
108         FI_HILOGE("The caller is not system hap");
109         return COMMON_NOT_SYSTEM_APP;
110     }
111     CHKPR(notifyCallback, RET_ERR);
112     auto appInfo = std::make_shared<BoomerangAppInfo>();
113     appInfo->uid = context.uid;
114     appInfo->pid = context.pid;
115     appInfo->tokenId = context.tokenId;
116     appInfo->packageName = DS_DUMPER->GetPackageName(appInfo->tokenId);
117     appInfo->boomerangCallback = notifyCallback;
118     DS_DUMPER->SetNotifyMetadatAppInfo(appInfo);
119     int32_t ret = manager_.NotifyMetadata(bundleName, notifyCallback);
120     if (ret != RET_OK) {
121         FI_HILOGE("boomerang NotifyMetadataBindingEvent failed");
122     }
123     return ret;
124 }
125 
BoomerangEncodeImage(CallingContext & context,const std::shared_ptr<Media::PixelMap> & pixelMap,const std::string & metaData,const sptr<IRemoteBoomerangCallback> & encodeCallback)126 int32_t BoomerangServer::BoomerangEncodeImage(CallingContext &context, const std::shared_ptr<Media::PixelMap>& pixelMap,
127     const std::string& metaData, const sptr<IRemoteBoomerangCallback>& encodeCallback)
128 {
129     if (!IsSystemHAPCalling(context)) {
130         FI_HILOGE("The caller is not system hap");
131         return COMMON_NOT_SYSTEM_APP;
132     }
133     CHKPR(encodeCallback, RET_ERR);
134     auto appInfo = std::make_shared<BoomerangAppInfo>();
135     appInfo->uid = context.uid;
136     appInfo->pid = context.pid;
137     appInfo->tokenId = context.tokenId;
138     appInfo->packageName = DS_DUMPER->GetPackageName(appInfo->tokenId);
139     appInfo->boomerangCallback = encodeCallback;
140     DS_DUMPER->SetNotifyMetadatAppInfo(appInfo);
141     int32_t ret = manager_.BoomerangEncodeImage(pixelMap, metaData, encodeCallback);
142     if (ret != RET_OK) {
143         FI_HILOGE("boomerang EncodeImage failed");
144     }
145     return ret;
146 }
147 
BoomerangDecodeImage(CallingContext & context,const std::shared_ptr<Media::PixelMap> & pixelMap,const sptr<IRemoteBoomerangCallback> & decodeCallback)148 int32_t BoomerangServer::BoomerangDecodeImage(CallingContext &context, const std::shared_ptr<Media::PixelMap>& pixelMap,
149     const sptr<IRemoteBoomerangCallback>& decodeCallback)
150 {
151     if (!IsSystemHAPCalling(context)) {
152         FI_HILOGE("The caller is not system hap");
153         return COMMON_NOT_SYSTEM_APP;
154     }
155     CHKPR(decodeCallback, RET_ERR);
156     auto appInfo = std::make_shared<BoomerangAppInfo>();
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->boomerangCallback = decodeCallback;
162     DS_DUMPER->SetNotifyMetadatAppInfo(appInfo);
163     int32_t ret = manager_.BoomerangDecodeImage(pixelMap, decodeCallback);
164     if (ret != RET_OK) {
165         FI_HILOGE("boomerang DecodeImage failed");
166     }
167     return ret;
168 }
169 
SubmitMetadata(CallingContext & context,const std::string & metaData)170 int32_t BoomerangServer::SubmitMetadata(CallingContext &context, const std::string& metaData)
171 {
172     int32_t ret = manager_.SubmitMetadata(metaData);
173     if (ret != RET_OK) {
174         FI_HILOGE("boomerang submit metada failed");
175     }
176     return ret;
177 }
178 
GetCache(CallingContext & context,const Type & type)179 Data BoomerangServer::GetCache(CallingContext &context, const Type &type)
180 {
181     return manager_.GetLatestDeviceStatusData(type);
182 }
183 
ReportSensorSysEvent(CallingContext & context,int32_t type,bool enable)184 void BoomerangServer::ReportSensorSysEvent(CallingContext &context, int32_t type, bool enable)
185 {
186     std::string packageName;
187     manager_.GetPackageName(context.tokenId, packageName);
188     int32_t ret = HiSysEventWrite(
189         OHOS::HiviewDFX::HiSysEvent::Domain::MSDP,
190         std::string(enable ? "Subscribe" : "Unsubscribe"),
191         OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
192         "UID", context.uid,
193         "PKGNAME", packageName,
194         "TYPE", type);
195     if (ret != 0) {
196         FI_HILOGE("HiviewDFX write failed, error:%{public}d", ret);
197     }
198 }
199 
IsSystemServiceCalling(CallingContext & context)200 bool BoomerangServer::IsSystemServiceCalling(CallingContext &context)
201 {
202     auto flag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(context.tokenId);
203     if ((flag == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) ||
204         (flag == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL)) {
205         FI_HILOGI("system service calling, flag:%{public}u", flag);
206         return true;
207     }
208     return false;
209 }
210 
IsSystemHAPCalling(CallingContext & context)211 bool BoomerangServer::IsSystemHAPCalling(CallingContext &context)
212 {
213     if (IsSystemServiceCalling(context)) {
214         return true;
215     }
216     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(context.fullTokenId);
217 }
218 } // namespace DeviceStatus
219 } // namespace Msdp
220 } // namespace OHOS