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_MSDP_MOCK_H 17 #define DEVICESTATUS_MSDP_MOCK_H 18 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <thread> 23 #include <vector> 24 25 #include "devicestatus_data_parse.h" 26 #include "devicestatus_data_utils.h" 27 #include "devicestatus_msdp_interface.h" 28 29 namespace OHOS { 30 namespace Msdp { 31 namespace DeviceStatus { 32 class DeviceStatusMsdpMock final : public DevicestatusMsdpInterface { 33 public: 34 DeviceStatusMsdpMock() = default; 35 ~DeviceStatusMsdpMock() = default; 36 37 enum EventType { 38 EVENT_UEVENT_FD, 39 EVENT_TIMER_FD, 40 }; 41 42 ErrCode Enable(DevicestatusDataUtils::DevicestatusType type) override; 43 ErrCode Disable(DevicestatusDataUtils::DevicestatusType type) override; 44 ErrCode DisableCount(DevicestatusDataUtils::DevicestatusType type) override; 45 ErrCode RegisterCallback(std::shared_ptr<MsdpAlgorithmCallback> callback) override; 46 ErrCode UnregisterCallback() override; 47 48 bool Init(); 49 void InitMockStore(); 50 void SetTimerInterval(int32_t interval); 51 void CloseTimer(); 52 void InitTimer(); 53 void TimerCallback(); 54 int32_t RegisterTimerCallback(const int32_t fd, const EventType et); 55 void StartThread(); 56 void LoopingThreadEntry(); 57 ErrCode NotifyMsdpImpl(const DevicestatusDataUtils::DevicestatusData& data); 58 void GetDeviceStatusData(); GetCallbackImpl()59 std::shared_ptr<MsdpAlgorithmCallback> GetCallbackImpl() 60 { 61 std::unique_lock lock(mutex_); 62 return callback_; 63 } 64 65 private: 66 using Callback = std::function<void(DeviceStatusMsdpMock*)>; 67 std::shared_ptr<MsdpAlgorithmCallback> callback_; 68 std::map<int32_t, Callback> callbacks_; 69 std::unique_ptr<DeviceStatusDataParse> dataParse_; 70 bool scFlag_ {true}; 71 int32_t timerInterval_ {-1}; 72 int32_t timerFd_ {-1}; 73 int32_t epFd_ {-1}; 74 std::mutex mutex_; 75 static std::vector<int32_t> enabledType_; 76 bool alive_ {true}; 77 }; 78 } // namespace DeviceStatus 79 } // namespace Msdp 80 } // namespace OHOS 81 #endif // DEVICESTATUS_MSDP_MOCK_H 82