1/*
2 * Copyright (c) 2021 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 LOG_QUERIER_H
17#define LOG_QUERIER_H
18
19#include <array>
20#include <vector>
21#include <atomic>
22#include <condition_variable>
23#include <future>
24#include <memory>
25#include <mutex>
26#include <socket.h>
27
28#include <hilog_common.h>
29#include <hilog_cmd.h>
30
31#include "log_persister.h"
32#include "log_stats.h"
33#include "log_buffer.h"
34#include "log_collector.h"
35
36namespace OHOS {
37namespace HiviewDFX {
38using CmdList = std::vector<IoctlCmd>;
39
40class ServiceController  {
41public:
42    static constexpr int MAX_DATA_LEN = 2048;
43    using PacketBuf = std::array<char, MAX_DATA_LEN>;
44
45    ServiceController(std::unique_ptr<Socket> communicationSocket, LogCollector& collector, HilogBuffer& hilogBuffer,
46        HilogBuffer& kmsgBuffer);
47    ~ServiceController();
48
49    void CommunicationLoop(std::atomic<bool>& stopLoop, const CmdList& list);
50
51private:
52    int GetMsgHeader(MsgHeader& hdr);
53    int GetRqst(const MsgHeader& hdr, char* rqst, int expectedLen);
54    void WriteErrorRsp(int code);
55    void WriteRspHeader(IoctlCmd cmd, size_t len);
56    template<typename T>
57    void RequestHandler(const MsgHeader& hdr, std::function<void(const T& rqst)> handle);
58
59    // util
60    int CheckOutputRqst(const OutputRqst& rqst);
61    void LogFilterFromOutputRqst(const OutputRqst& rqst, LogFilter& filter);
62    int CheckPersistStartRqst(const PersistStartRqst &rqst);
63    void PersistStartRqst2Msg(const PersistStartRqst &rqst, LogPersistStartMsg &msg);
64    // log query
65    int WriteQueryResponse(OptCRef<HilogData> pData);
66    // statistics
67    void SendOverallStats(const LogStats& stats);
68    void SendLogTypeDomainStats(const LogStats& stats);
69    void SendDomainStats(const LogStats& stats);
70    void SendDomainTagStats(const LogStats& stats);
71    void SendProcStats(const LogStats& stats);
72    void SendProcLogTypeStats(const LogStats& stats);
73    void SendProcTagStats(const LogStats& stats);
74    void SendTagStats(const TagTable &tagTable);
75    // cmd handlers
76    void HandleOutputRqst(const OutputRqst &rqst);
77    void HandlePersistStartRqst(const PersistStartRqst &rqst);
78    void HandlePersistStopRqst(const PersistStopRqst &rqst);
79    void HandlePersistQueryRqst(const PersistQueryRqst& rqst);
80    void HandlePersistRefreshRqst(const PersistRefreshRqst& rqst);
81    void HandlePersistClearRqst();
82    void HandleBufferSizeGetRqst(const BufferSizeGetRqst& rqst);
83    void HandleBufferSizeSetRqst(const BufferSizeSetRqst& rqst);
84    void HandleStatsQueryRqst(const StatsQueryRqst& rqst);
85    void HandleStatsClearRqst(const StatsClearRqst& rqst);
86    void HandleDomainFlowCtrlRqst(const DomainFlowCtrlRqst& rqst);
87    void HandleLogRemoveRqst(const LogRemoveRqst& rqst);
88    void HandleLogKmsgEnableRqst(const KmsgEnableRqst& rqst);
89
90    void NotifyForNewData();
91    bool IsValidCmd(const CmdList& list, IoctlCmd cmd);
92
93    std::unique_ptr<Socket> m_communicationSocket;
94    LogCollector& m_logCollector;
95    HilogBuffer& m_hilogBuffer;
96    HilogBuffer& m_kmsgBuffer;
97    HilogBuffer::ReaderId m_hilogBufferReader;
98    HilogBuffer::ReaderId m_kmsgBufferReader;
99    std::condition_variable m_notifyNewDataCv;
100    std::mutex m_notifyNewDataMtx;
101};
102
103template<typename T>
104void ServiceController::RequestHandler(const MsgHeader& hdr, std::function<void(const T& rqst)> handle)
105{
106    std::vector<char> buffer(hdr.len, 0);
107    char *data = buffer.data();
108    int ret = GetRqst(hdr, data, sizeof(T));
109    if (ret != RET_SUCCESS) {
110        std::cout << "Error GetRqst" << std::endl;
111        return;
112    }
113    T *rqst = reinterpret_cast<T *>(data);
114    handle(*rqst);
115}
116
117int RestorePersistJobs(HilogBuffer& hilogBuffer, HilogBuffer& kmsgBuffer);
118} // namespace HiviewDFX
119} // namespace OHOS
120#endif
121