1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci#ifndef DEVICESTATUS_MSDP_MOCK_H
17f857971dSopenharmony_ci#define DEVICESTATUS_MSDP_MOCK_H
18f857971dSopenharmony_ci
19f857971dSopenharmony_ci#include <atomic>
20f857971dSopenharmony_ci#include <map>
21f857971dSopenharmony_ci#include <memory>
22f857971dSopenharmony_ci#include <mutex>
23f857971dSopenharmony_ci#include <thread>
24f857971dSopenharmony_ci#include <vector>
25f857971dSopenharmony_ci
26f857971dSopenharmony_ci#include "devicestatus_data_parse.h"
27f857971dSopenharmony_ci#include "stationary_data.h"
28f857971dSopenharmony_ci#include "devicestatus_msdp_interface.h"
29f857971dSopenharmony_ci
30f857971dSopenharmony_cinamespace OHOS {
31f857971dSopenharmony_cinamespace Msdp {
32f857971dSopenharmony_cinamespace DeviceStatus {
33f857971dSopenharmony_ciclass DeviceStatusMsdpMock final : public IMsdp {
34f857971dSopenharmony_cipublic:
35f857971dSopenharmony_ci    enum EventType {
36f857971dSopenharmony_ci        EVENT_UEVENT_FD,
37f857971dSopenharmony_ci        EVENT_TIMER_FD
38f857971dSopenharmony_ci    };
39f857971dSopenharmony_ci
40f857971dSopenharmony_ci    DeviceStatusMsdpMock();
41f857971dSopenharmony_ci    ~DeviceStatusMsdpMock();
42f857971dSopenharmony_ci
43f857971dSopenharmony_ci    bool Init();
44f857971dSopenharmony_ci    void InitMockStore();
45f857971dSopenharmony_ci    int32_t SetTimerInterval(int32_t interval);
46f857971dSopenharmony_ci    void CloseTimer();
47f857971dSopenharmony_ci    void InitTimer();
48f857971dSopenharmony_ci    void TimerCallback();
49f857971dSopenharmony_ci    int32_t RegisterTimerCallback(int32_t fd, const EventType et);
50f857971dSopenharmony_ci    void StartThread();
51f857971dSopenharmony_ci    void LoopingThreadEntry();
52f857971dSopenharmony_ci    ErrCode Enable(Type type) override;
53f857971dSopenharmony_ci    ErrCode Disable(Type type) override;
54f857971dSopenharmony_ci    ErrCode DisableCount(Type type) override;
55f857971dSopenharmony_ci    ErrCode RegisterCallback(std::shared_ptr<IMsdp::MsdpAlgoCallback> callback) override;
56f857971dSopenharmony_ci    ErrCode UnregisterCallback() override;
57f857971dSopenharmony_ci    ErrCode NotifyMsdpImpl(const Data &data);
58f857971dSopenharmony_ci    int32_t GetDeviceStatusData();
59f857971dSopenharmony_ci    std::shared_ptr<MsdpAlgoCallback> GetCallbackImpl()
60f857971dSopenharmony_ci    {
61f857971dSopenharmony_ci        std::unique_lock lock(mutex_);
62f857971dSopenharmony_ci        return callback_;
63f857971dSopenharmony_ci    }
64f857971dSopenharmony_ci
65f857971dSopenharmony_ciprivate:
66f857971dSopenharmony_ci    using Callback = std::function<void(DeviceStatusMsdpMock*)>;
67f857971dSopenharmony_ci    std::shared_ptr<MsdpAlgoCallback> callback_ { nullptr };
68f857971dSopenharmony_ci    std::map<int32_t, Callback> callbacks_;
69f857971dSopenharmony_ci    std::unique_ptr<DeviceStatusDataParse> dataParse_ { nullptr };
70f857971dSopenharmony_ci    int32_t timerInterval_ { -1 };
71f857971dSopenharmony_ci    int32_t timerFd_ { -1 };
72f857971dSopenharmony_ci    int32_t epFd_ { -1 };
73f857971dSopenharmony_ci    std::mutex mutex_;
74f857971dSopenharmony_ci    std::vector<Type> enabledType_;
75f857971dSopenharmony_ci    std::atomic<bool> alive_ { false };
76f857971dSopenharmony_ci    std::thread thread_;
77f857971dSopenharmony_ci};
78f857971dSopenharmony_ci} // namespace DeviceStatus
79f857971dSopenharmony_ci} // namespace Msdp
80f857971dSopenharmony_ci} // namespace OHOS
81f857971dSopenharmony_ci#endif // DEVICESTATUS_MSDP_MOCK_H
82