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 <refbase.h> 20 #include <singleton.h> 21 #include <map> 22 #include <memory> 23 #include <queue> 24 #include <set> 25 #include <string> 26 #include <vector> 27 28 #include "accesstoken_kit.h" 29 #include "devicestatus_data_utils.h" 30 #include "idevicestatus_callback.h" 31 32 namespace OHOS { 33 namespace Msdp { 34 const std::string ARG_DUMP_HELP = "-h"; 35 const std::string ARG_DUMP_DEVICESTATUS_SUBSCRIBER = "-s"; 36 const std::string ARG_DUMP_DEVICESTATUS_CHANGES = "-l"; 37 const std::string ARG_DUMP_DEVICESTATUS_CURRENT_STATE = "-c"; 38 constexpr uint32_t MAX_DEVICE_STATUS_SIZE = 10; 39 constexpr uint32_t BASE_YEAR = 1900; 40 constexpr uint32_t BASE_MON = 1; 41 struct AppInfo { 42 std::string startTime; 43 int32_t uid = 0; 44 int32_t pid = 0; 45 Security::AccessToken::AccessTokenID tokenId; 46 std::string packageName; 47 DevicestatusDataUtils::DevicestatusType type; 48 sptr<IdevicestatusCallback> callback; 49 }; 50 struct DeviceStatusRecord { 51 std::string startTime; 52 DevicestatusDataUtils::DevicestatusData data; 53 }; 54 class DevicestatusDumper final : public RefBase, 55 public Singleton<DevicestatusDumper> { 56 public: 57 DevicestatusDumper() = default; 58 ~DevicestatusDumper() = default; 59 void ParseCommand(int32_t fd, const std::vector<std::string> &args, 60 const std::vector<DevicestatusDataUtils::DevicestatusData> &datas); 61 void DumpHelpInfo(int32_t fd) const; 62 void DumpDevicestatusSubscriber(int32_t fd); 63 void DumpDevicestatusChanges(int32_t fd); 64 void DumpDevicestatusCurrentStatus(int32_t fd, 65 const std::vector<DevicestatusDataUtils::DevicestatusData> &datas) const; 66 void SaveAppInfo(std::shared_ptr<AppInfo> appInfo); 67 void RemoveAppInfo(std::shared_ptr<AppInfo> appInfo); 68 void pushDeviceStatus(const DevicestatusDataUtils::DevicestatusData& data); 69 private: 70 DISALLOW_COPY_AND_MOVE(DevicestatusDumper); 71 void DumpCurrentTime(std::string &startTime) const; 72 std::string GetStatusType(const DevicestatusDataUtils::DevicestatusType &type) const; 73 std::string GetDeviceState(const DevicestatusDataUtils::DevicestatusValue &type) const; 74 std::map<DevicestatusDataUtils::DevicestatusType, std::set<std::shared_ptr<AppInfo>>> \ 75 appInfoMap_; 76 std::queue<std::shared_ptr<DeviceStatusRecord>> deviceStatusQueue_; 77 std::mutex mutex_; 78 }; 79 } // namespace Msdp 80 } // namespace OHOS 81 #endif // DEVICESTATUS_DUMPER_H