12498b56bSopenharmony_ci/* 22498b56bSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 42498b56bSopenharmony_ci * you may not use this file except in compliance with the License. 52498b56bSopenharmony_ci * You may obtain a copy of the License at 62498b56bSopenharmony_ci * 72498b56bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 82498b56bSopenharmony_ci * 92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 122498b56bSopenharmony_ci * See the License for the specific language governing permissions and 132498b56bSopenharmony_ci * limitations under the License. 142498b56bSopenharmony_ci */ 152498b56bSopenharmony_ci 162498b56bSopenharmony_ci#ifndef LOG_IOCTL_H 172498b56bSopenharmony_ci#define LOG_IOCTL_H 182498b56bSopenharmony_ci 192498b56bSopenharmony_ci#include <vector> 202498b56bSopenharmony_ci#include <functional> 212498b56bSopenharmony_ci#include <securec.h> 222498b56bSopenharmony_ci 232498b56bSopenharmony_ci#include "hilog_common.h" 242498b56bSopenharmony_ci#include "log_utils.h" 252498b56bSopenharmony_ci#include "hilog_cmd.h" 262498b56bSopenharmony_ci#include "seq_packet_socket_client.h" 272498b56bSopenharmony_ci 282498b56bSopenharmony_cinamespace OHOS { 292498b56bSopenharmony_cinamespace HiviewDFX { 302498b56bSopenharmony_ciusing namespace std; 312498b56bSopenharmony_ci 322498b56bSopenharmony_ciclass LogIoctl { 332498b56bSopenharmony_cipublic: 342498b56bSopenharmony_ci LogIoctl(IoctlCmd rqst, IoctlCmd rsp); 352498b56bSopenharmony_ci ~LogIoctl() = default; 362498b56bSopenharmony_ci 372498b56bSopenharmony_ci template<typename T1, typename T2> 382498b56bSopenharmony_ci int Request(const T1& rqst, std::function<int(const T2& rsp)> handle); 392498b56bSopenharmony_ci int RequestOutput(const OutputRqst& rqst, std::function<int(const OutputRsp& rsp)> handle); 402498b56bSopenharmony_ci int RequestStatsQuery(const StatsQueryRqst& rqst, std::function<int(const StatsQueryRsp& rsp)> handle); 412498b56bSopenharmony_ci 422498b56bSopenharmony_ciprivate: 432498b56bSopenharmony_ci SeqPacketSocketClient socket; 442498b56bSopenharmony_ci int socketInit = -1; 452498b56bSopenharmony_ci IoctlCmd rqstCmd; 462498b56bSopenharmony_ci IoctlCmd rspCmd; 472498b56bSopenharmony_ci static constexpr int DEFAULT_RECV_BUF_LEN = MAX_LOG_LEN * 2; 482498b56bSopenharmony_ci 492498b56bSopenharmony_ci int SendMsgHeader(IoctlCmd cmd, size_t len); 502498b56bSopenharmony_ci int ReceiveMsgHeaer(MsgHeader& hdr); 512498b56bSopenharmony_ci int GetRsp(char* rsp, int len); 522498b56bSopenharmony_ci template<typename T1, typename T2> 532498b56bSopenharmony_ci int RequestMsgHead(const T1& rqst); 542498b56bSopenharmony_ci 552498b56bSopenharmony_ci int ReceiveAndProcessOutputRsp(std::function<int(const OutputRsp& rsp)> handle); 562498b56bSopenharmony_ci int ReceiveAndProcessStatsQueryRsp(std::function<int(const StatsQueryRsp& rsp)> handle); 572498b56bSopenharmony_ci int ReceiveProcTagStats(StatsQueryRsp &rsp); 582498b56bSopenharmony_ci int ReceiveProcLogTypeStats(StatsQueryRsp &rsp); 592498b56bSopenharmony_ci int ReceiveProcStats(StatsQueryRsp &rsp); 602498b56bSopenharmony_ci int ReceiveDomainTagStats(StatsQueryRsp &rsp); 612498b56bSopenharmony_ci int ReceiveDomainStats(StatsQueryRsp &rsp); 622498b56bSopenharmony_ci int ReceiveLogTypeDomainStats(StatsQueryRsp &rsp); 632498b56bSopenharmony_ci void DeleteLogStatsInfo(StatsQueryRsp &rsp); 642498b56bSopenharmony_ci}; 652498b56bSopenharmony_ci 662498b56bSopenharmony_citemplate<typename T1, typename T2> 672498b56bSopenharmony_ciint LogIoctl::RequestMsgHead(const T1& rqst) 682498b56bSopenharmony_ci{ 692498b56bSopenharmony_ci // 0. Send reqeust message and process the response header 702498b56bSopenharmony_ci int ret = SendMsgHeader(rqstCmd, sizeof(T1)); 712498b56bSopenharmony_ci if (ret != RET_SUCCESS) { 722498b56bSopenharmony_ci return ret; 732498b56bSopenharmony_ci } 742498b56bSopenharmony_ci ret = socket.WriteAll(reinterpret_cast<const char*>(&rqst), sizeof(T1)); 752498b56bSopenharmony_ci if (ret < 0) { 762498b56bSopenharmony_ci return ERR_SOCKET_WRITE_CMD_FAIL; 772498b56bSopenharmony_ci } 782498b56bSopenharmony_ci // 1. Receive msg header 792498b56bSopenharmony_ci MsgHeader hdr = { 0 }; 802498b56bSopenharmony_ci ret = ReceiveMsgHeaer(hdr); 812498b56bSopenharmony_ci if (ret != RET_SUCCESS) { 822498b56bSopenharmony_ci return ret; 832498b56bSopenharmony_ci } 842498b56bSopenharmony_ci if (hdr.cmd == static_cast<uint8_t>(IoctlCmd::RSP_ERROR)) { 852498b56bSopenharmony_ci return hdr.err; 862498b56bSopenharmony_ci } 872498b56bSopenharmony_ci if (hdr.cmd != static_cast<uint8_t>(rspCmd)) { 882498b56bSopenharmony_ci return RET_FAIL; 892498b56bSopenharmony_ci } 902498b56bSopenharmony_ci if (hdr.len != sizeof(T2)) { 912498b56bSopenharmony_ci return ERR_MSG_LEN_INVALID; 922498b56bSopenharmony_ci } 932498b56bSopenharmony_ci return RET_SUCCESS; 942498b56bSopenharmony_ci} 952498b56bSopenharmony_ci 962498b56bSopenharmony_citemplate<typename T1, typename T2> 972498b56bSopenharmony_ciint LogIoctl::Request(const T1& rqst, std::function<int(const T2& rsp)> handle) 982498b56bSopenharmony_ci{ 992498b56bSopenharmony_ci if (rqstCmd == IoctlCmd::OUTPUT_RQST || rqstCmd == IoctlCmd::STATS_QUERY_RQST) { 1002498b56bSopenharmony_ci std::cout << "Request API not support this command" << endl; 1012498b56bSopenharmony_ci return RET_FAIL; 1022498b56bSopenharmony_ci } 1032498b56bSopenharmony_ci int ret = RequestMsgHead<T1, T2>(rqst); 1042498b56bSopenharmony_ci if (ret != RET_SUCCESS) { 1052498b56bSopenharmony_ci return ret; 1062498b56bSopenharmony_ci } 1072498b56bSopenharmony_ci // common case, Get Response and callback 1082498b56bSopenharmony_ci T2 rsp = { 0 }; 1092498b56bSopenharmony_ci ret = GetRsp(reinterpret_cast<char*>(&rsp), sizeof(T2)); 1102498b56bSopenharmony_ci if (ret != RET_SUCCESS) { 1112498b56bSopenharmony_ci return ret; 1122498b56bSopenharmony_ci } 1132498b56bSopenharmony_ci return handle(rsp); 1142498b56bSopenharmony_ci} 1152498b56bSopenharmony_ci} // namespace HiviewDFX 1162498b56bSopenharmony_ci} // namespace OHOS 1172498b56bSopenharmony_ci#endif // LOG_IOCTL_H