1 /* 2 * Copyright (c) 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 #ifndef OHOS_DM_FREEZE_PROCESS_V2_H 16 #define OHOS_DM_FREEZE_PROCESS_V2_H 17 #include <map> 18 #include <memory> 19 #include <mutex> 20 21 #include "deviceprofile_connector.h" 22 #include "dm_auth_context.h" 23 #include "dm_single_instance.h" 24 #include "kv_adapter_manager.h" 25 26 namespace OHOS { 27 namespace DistributedHardware { 28 typedef struct DeviceFreezeState { 29 int64_t startFreezeTimeStamp; 30 int64_t stopFreezeTimeStamp; DeviceFreezeStateDeviceFreezeState31 explicit DeviceFreezeState() : startFreezeTimeStamp(0), stopFreezeTimeStamp(0) {} IsEmptyDeviceFreezeState32 bool IsEmpty() const 33 { 34 return startFreezeTimeStamp == 0 && stopFreezeTimeStamp == 0; 35 } ResetDeviceFreezeState36 void Reset() 37 { 38 startFreezeTimeStamp = 0; 39 stopFreezeTimeStamp = 0; 40 } 41 } DeviceFreezeState; 42 43 typedef struct BindFailedEvents { 44 std::vector<int64_t> failedTimeStamps; 45 std::vector<int64_t> freezeTimeStamps; BindFailedEventsBindFailedEvents46 explicit BindFailedEvents() : failedTimeStamps(), freezeTimeStamps() {} IsEmptyBindFailedEvents47 bool IsEmpty() const 48 { 49 return failedTimeStamps.empty() && freezeTimeStamps.empty(); 50 } ResetBindFailedEvents51 void Reset() 52 { 53 failedTimeStamps.clear(); 54 freezeTimeStamps.clear(); 55 } 56 } BindFailedEvents; 57 58 class FreezeProcess { 59 DM_DECLARE_SINGLE_INSTANCE_BASE(FreezeProcess); 60 public: 61 bool IsFrozen(int64_t &remainingFrozenTime); 62 int32_t DeleteFreezeRecord(); 63 int32_t UpdateFreezeRecord(); 64 bool IsNeedFreeze(std::shared_ptr<DmAuthContext> context); 65 private: 66 FreezeProcess() = default; 67 ~FreezeProcess() = default; 68 int32_t SyncFreezeData(); 69 int32_t ConvertJsonToDeviceFreezeState(const std::string &result, DeviceFreezeState &freezeStateObj); 70 int32_t ConvertJsonToBindFailedEvents(const std::string &result, BindFailedEvents &bindFailedEvents); 71 void ConvertBindFailedEventsToJson(const BindFailedEvents &value, std::string &result); 72 void ConvertDeviceFreezeStateToJson(const DeviceFreezeState &value, std::string &result); 73 int32_t CleanFreezeRecord(int64_t nowTime); 74 int32_t CleanBindFailedEvents(int64_t reservedDataTimeStamp); 75 int32_t CleanFreezeState(int64_t reservedDataTimeStamp); 76 int32_t UpdateFreezeState(int64_t nowTime); 77 void CalculateNextFreezeTime(int64_t nowFreezeTime, int64_t &nextFreezeTime); 78 79 private: 80 bool isSynced_ = false; 81 DeviceFreezeState freezeStateCache_; 82 BindFailedEvents bindFailedEventsCache_; 83 std::mutex freezeStateCacheMtx_; 84 std::mutex bindFailedEventsCacheMtx_; 85 std::mutex isSyncedMtx_; 86 }; 87 88 } // namespace DistributedHardware 89 } // namespace OHOS 90 #endif // OHOS_DM_FREEZE_PROCESS_V2_H