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 34 namespace OHOS { 35 namespace Msdp { 36 namespace DeviceStatus { 37 inline constexpr int32_t RET_NG { -1 }; 38 39 struct 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 49 struct DeviceStatusRecord { 50 std::string startTime; 51 Data data; 52 }; 53 54 class DeviceStatusDumper final : public RefBase { 55 DECLARE_DELAYED_SINGLETON(DeviceStatusDumper); 56 public: 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 private: 66 DISALLOW_COPY_AND_MOVE(DeviceStatusDumper); 67 std::string GetStatusType(Type type) const; 68 std::string GetDeviceState(OnChangedValue type) const; 69 void ExecutDump(int32_t fd, const std::vector<Data> &datas, int32_t opt); 70 void DumpDeviceStatusSubscriber(int32_t fd); 71 void DumpDeviceStatusChanges(int32_t fd); 72 void DumpDeviceStatusCurrentStatus(int32_t fd, const std::vector<Data> &datas) const; 73 void DumpCheckDefine(int32_t fd); 74 void ChkDefineOutput(int32_t fd); 75 template<class ...Ts> 76 void CheckDefineOutput(int32_t fd, const char* fmt, Ts... args); 77 78 private: 79 std::map<Type, std::set<std::shared_ptr<AppInfo>>> appInfoMap_; 80 std::queue<std::shared_ptr<DeviceStatusRecord>> deviceStatusQueue_; 81 std::mutex mutex_; 82 IContext *context_ { nullptr }; 83 }; 84 85 #define DS_DUMPER OHOS::DelayedSingleton<DeviceStatusDumper>::GetInstance() 86 } // namespace DeviceStatus 87 } // namespace Msdp 88 } // namespace OHOS 89 #endif // DEVICESTATUS_DUMPER_H 90