1 /* 2 * Copyright (c) 2024 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 #ifndef HIVIEWDFX_HIVIEW_UC_TELEMETRY_LISTENER_H 16 #define HIVIEWDFX_HIVIEW_UC_TELEMETRY_LISTENER_H 17 18 #include <map> 19 20 #include "event.h" 21 #include "plugin.h" 22 #include "trace_flow_controller.h" 23 #include "trace_state_machine.h" 24 #include "ffrt.h" 25 26 namespace OHOS::HiviewDFX { 27 namespace { 28 const int64_t SECONDS_TO_MS = 1000; 29 const int64_t MS_TO_US = 1000; 30 } 31 32 struct TelemetryParams { 33 int64_t traceDuration = 3600 * SECONDS_TO_MS; //ms 34 int64_t beginTime = -1; 35 std::string telemetryId; 36 std::string appFilterName; 37 std::string traceTag; 38 std::vector<std::string> saParams; 39 TelemetryPolicy tracePolicy; 40 }; 41 42 class TelemetryListener : public EventListener { 43 public: 44 void OnUnorderedEvent(const Event &msg) override; 45 GetListenerName()46 std::string GetListenerName() override 47 { 48 return "TelemetryListener"; 49 } 50 51 private: 52 std::string CheckValidParam(const Event &msg, TelemetryParams ¶ms, bool &isCloseMsg); 53 void HandleStart(const TelemetryParams ¶ms); 54 void HandleStop(); 55 void WriteErrorEvent(const std::string &error, const TelemetryParams ¶ms); 56 bool ProcessTraceTag(std::string &traceTag); 57 bool CheckTelemetryId(const Event &msg, TelemetryParams ¶ms, std::string &errorMsg); 58 bool CheckTraceTags(const Event &msg, TelemetryParams ¶ms, std::string &errorMsg); 59 bool CheckTracePolicy(const Event &msg, TelemetryParams ¶ms, std::string &errorMsg); 60 bool CheckSwitchValid(const Event &msg, bool &isCloseMsg, std::string &errorMsg); 61 bool CheckBeginTime(const Event &msg, TelemetryParams ¶ms, std::string &errorMsg); 62 bool InitTelemetryDbData(const Event &msg, bool &isTimeOut, const TelemetryParams ¶ms); 63 void GetSaNames(const Event &msg, TelemetryParams ¶ms); 64 65 private: 66 std::unique_ptr<ffrt::queue> taskQueue_ = nullptr; 67 ffrt::task_handle startTaskHandle_; 68 }; 69 70 } 71 72 #endif //HIVIEWDFX_HIVIEW_UC_TELEMETRY_LISTENER_H 73