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 "iremote_boomerang_callback.h" 31 #include "iremote_dev_sta_callback.h" 32 #include "i_context.h" 33 #include "stationary_data.h" 34 35 namespace OHOS { 36 namespace Msdp { 37 namespace DeviceStatus { 38 inline constexpr int32_t RET_NG { -1 }; 39 40 struct AppInfo { 41 std::string startTime; 42 int32_t uid {}; 43 int32_t pid {}; 44 Security::AccessToken::AccessTokenID tokenId; 45 std::string packageName; 46 Type type { TYPE_INVALID }; 47 sptr<IRemoteDevStaCallback> callback { nullptr }; 48 sptr<IRemoteBoomerangCallback> boomerangCallback { nullptr }; 49 }; 50 51 struct BoomerangAppInfo { 52 std::string startTime; 53 int32_t uid = 0; 54 int32_t pid = 0; 55 Security::AccessToken::AccessTokenID tokenId; 56 std::string packageName; 57 BoomerangType type { BOOMERANG_TYPE_INVALID }; 58 sptr<IRemoteBoomerangCallback> boomerangCallback { nullptr }; 59 }; 60 61 struct DeviceStatusRecord { 62 std::string startTime; 63 Data data; 64 }; 65 66 class DeviceStatusDumper final : public RefBase { 67 DECLARE_DELAYED_SINGLETON(DeviceStatusDumper); 68 public: 69 int32_t Init(IContext *context); 70 void ParseCommand(int32_t fd, const std::vector<std::string> &args, const std::vector<Data> &datas); 71 void DumpHelpInfo(int32_t fd) const; 72 void SaveAppInfo(std::shared_ptr<AppInfo> appInfo); 73 void RemoveAppInfo(std::shared_ptr<AppInfo> appInfo); 74 void PushDeviceStatus(const Data &data); 75 std::string GetPackageName(Security::AccessToken::AccessTokenID tokenId); 76 77 void DumpDeviceStatusSubscriber(int32_t fd); 78 void DumpDeviceStatusChanges(int32_t fd); 79 void DumpDeviceStatusCurrentStatus(int32_t fd, const std::vector<Data> &datas) const; 80 81 void SaveBoomerangAppInfo(std::shared_ptr<BoomerangAppInfo> appInfo); 82 void RemoveBoomerangAppInfo(std::shared_ptr<BoomerangAppInfo> appInfo); 83 void SetNotifyMetadatAppInfo(std::shared_ptr<BoomerangAppInfo> appInfo); 84 85 private: 86 DISALLOW_COPY_AND_MOVE(DeviceStatusDumper); 87 std::string GetStatusType(Type type) const; 88 std::string GetDeviceState(OnChangedValue type) const; 89 void ExecutDump(int32_t fd, const std::vector<Data> &datas, int32_t opt); 90 void DumpCheckDefine(int32_t fd); 91 void ChkDefineOutput(int32_t fd); 92 template<class ...Ts> 93 void CheckDefineOutput(int32_t fd, const char* fmt, Ts... args); 94 95 private: 96 std::map<Type, std::set<std::shared_ptr<AppInfo>>> appInfos_; 97 std::map<BoomerangType, std::set<std::shared_ptr<BoomerangAppInfo>>> boomerangAppInfos_; 98 std::shared_ptr<BoomerangAppInfo> notifyMetadatAppInfo_; 99 std::queue<std::shared_ptr<DeviceStatusRecord>> deviceStatusQueue_; 100 std::mutex mutex_; 101 IContext *context_ { nullptr }; 102 }; 103 104 #define DS_DUMPER OHOS::DelayedSingleton<DeviceStatusDumper>::GetInstance() 105 } // namespace DeviceStatus 106 } // namespace Msdp 107 } // namespace OHOS 108 #endif // DEVICESTATUS_DUMPER_H 109