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 <atomic> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <thread> 24 #include <vector> 25 26 #include "devicestatus_data_parse.h" 27 #include "stationary_data.h" 28 #include "devicestatus_msdp_interface.h" 29 30 namespace OHOS { 31 namespace Msdp { 32 namespace DeviceStatus { 33 class DeviceStatusMsdpMock final : public IMsdp { 34 public: 35 enum EventType { 36 EVENT_UEVENT_FD, 37 EVENT_TIMER_FD 38 }; 39 40 DeviceStatusMsdpMock(); 41 ~DeviceStatusMsdpMock(); 42 43 bool Init(); 44 void InitMockStore(); 45 int32_t SetTimerInterval(int32_t interval); 46 void CloseTimer(); 47 void InitTimer(); 48 void TimerCallback(); 49 int32_t RegisterTimerCallback(int32_t fd, const EventType et); 50 void StartThread(); 51 void LoopingThreadEntry(); 52 ErrCode Enable(Type type) override; 53 ErrCode Disable(Type type) override; 54 ErrCode DisableCount(Type type) override; 55 ErrCode RegisterCallback(std::shared_ptr<IMsdp::MsdpAlgoCallback> callback) override; 56 ErrCode UnregisterCallback() override; 57 ErrCode NotifyMsdpImpl(const Data& data); 58 int32_t GetDeviceStatusData(); GetCallbackImpl()59 std::shared_ptr<MsdpAlgoCallback> 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<MsdpAlgoCallback> callback_ { nullptr }; 68 std::map<int32_t, Callback> callbacks_; 69 std::unique_ptr<DeviceStatusDataParse> dataParse_ { nullptr }; 70 int32_t timerInterval_ { -1 }; 71 int32_t timerFd_ { -1 }; 72 int32_t epFd_ { -1 }; 73 std::mutex mutex_; 74 std::vector<Type> enabledType_; 75 std::atomic<bool> alive_ { false }; 76 std::shared_ptr<std::thread> thread_ { nullptr }; 77 }; 78 } // namespace DeviceStatus 79 } // namespace Msdp 80 } // namespace OHOS 81 #endif // DEVICESTATUS_MSDP_MOCK_H 82