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 HIDUMP_HELPER_H
17#define HIDUMP_HELPER_H
18
19#include <string>
20#include <vector>
21#include <mutex>
22#include <map>
23#include <unordered_map>
24
25#include <sys/epoll.h>
26#include <sys/inotify.h>
27
28#include "constants_dinput.h"
29#include "single_instance.h"
30
31namespace OHOS {
32namespace DistributedHardware {
33namespace DistributedInput {
34enum class HiDumperFlag {
35    GET_HELP = 0,
36    GET_NODE_INFO,
37    GET_SESSION_INFO,
38};
39
40struct NodeInfo {
41    std::string devId = "";
42    std::string virNodeName = "";
43    std::string inputDhId = "";
44};
45
46struct SessionInfo {
47    int32_t sesId = 0;
48    std::string mySesName = "";
49    std::string peerSesName = "";
50    SessionStatus sessionState = SessionStatus::CLOSED;
51};
52
53class HiDumper {
54DECLARE_SINGLE_INSTANCE_BASE(HiDumper);
55
56public:
57    bool HiDump(const std::vector<std::string> &args, std::string &result);
58    void SaveNodeInfo(const std::string &deviceId, const std::string &nodeName, const std::string &dhId);
59    void DeleteNodeInfo(const std::string &deviceId, const std::string &dhId);
60    void CreateSessionInfo(const std::string &remoteDevId, const int32_t &sessionId, const std::string &mySessionName,
61        const std::string &peerSessionName, const SessionStatus &sessionStatus);
62    void SetSessionStatus(const std::string &remoteDevId, const SessionStatus &sessionStatus);
63    void DeleteSessionInfo(const std::string &remoteDevId);
64private:
65    explicit HiDumper() = default;
66    ~HiDumper() = default;
67    int32_t ProcessDump(const std::string &args, std::string &result);
68    int32_t GetAllNodeInfos(std::string &result);
69    int32_t GetSessionInfo(std::string &result);
70    int32_t ShowHelp(std::string &result);
71private:
72    std::vector<NodeInfo> nodeInfos_;
73    std::mutex nodeMutex_;
74
75    // the unordered_map's key is remoteDevId.
76    std::unordered_map<std::string, SessionInfo> sessionInfos_;
77    std::mutex sessionMutex_;
78    std::mutex operationMutex_;
79};
80} // namespace DistributedInput
81} // namespace DistributedHardware
82} // namespace OHOS
83
84#endif // HIDUMP_HELPER_H