1/* 2 * Copyright (c) 2022-2023 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#ifndef DEVICESTATUS_DUMPER_H 17#define DEVICESTATUS_DUMPER_H 18 19#include <map> 20#include <memory> 21#include <queue> 22#include <refbase.h> 23#include <set> 24#include <string> 25#include <vector> 26 27#include <singleton.h> 28 29#include "accesstoken_kit.h" 30#include "i_context.h" 31#include "stationary_callback.h" 32#include "stationary_data.h" 33 34namespace OHOS { 35namespace Msdp { 36namespace DeviceStatus { 37inline constexpr int32_t RET_NG { -1 }; 38 39struct AppInfo { 40 std::string startTime; 41 int32_t uid {}; 42 int32_t pid {}; 43 Security::AccessToken::AccessTokenID tokenId; 44 std::string packageName; 45 Type type { TYPE_INVALID }; 46 sptr<IRemoteDevStaCallback> callback { nullptr }; 47}; 48 49struct DeviceStatusRecord { 50 std::string startTime; 51 Data data; 52}; 53 54class DeviceStatusDumper final : public RefBase { 55 DECLARE_DELAYED_SINGLETON(DeviceStatusDumper); 56public: 57 int32_t Init(IContext *context); 58 void ParseCommand(int32_t fd, const std::vector<std::string> &args, const std::vector<Data> &datas); 59 void DumpHelpInfo(int32_t fd) const; 60 void SaveAppInfo(std::shared_ptr<AppInfo> appInfo); 61 void RemoveAppInfo(std::shared_ptr<AppInfo> appInfo); 62 void PushDeviceStatus(const Data &data); 63 std::string GetPackageName(Security::AccessToken::AccessTokenID tokenId); 64 65 void DumpDeviceStatusSubscriber(int32_t fd); 66 void DumpDeviceStatusChanges(int32_t fd); 67 void DumpDeviceStatusCurrentStatus(int32_t fd, const std::vector<Data> &datas) const; 68 69private: 70 DISALLOW_COPY_AND_MOVE(DeviceStatusDumper); 71 std::string GetStatusType(Type type) const; 72 std::string GetDeviceState(OnChangedValue type) const; 73 void ExecutDump(int32_t fd, const std::vector<Data> &datas, int32_t opt); 74 void DumpCheckDefine(int32_t fd); 75 void ChkDefineOutput(int32_t fd); 76 template<class ...Ts> 77 void CheckDefineOutput(int32_t fd, const char* fmt, Ts... args); 78 79private: 80 std::map<Type, std::set<std::shared_ptr<AppInfo>>> appInfos_; 81 std::queue<std::shared_ptr<DeviceStatusRecord>> deviceStatusQueue_; 82 std::mutex mutex_; 83 IContext *context_ { nullptr }; 84}; 85 86#define DS_DUMPER OHOS::DelayedSingleton<DeviceStatusDumper>::GetInstance() 87} // namespace DeviceStatus 88} // namespace Msdp 89} // namespace OHOS 90#endif // DEVICESTATUS_DUMPER_H 91