1 /*
2  * Copyright (C) 2023-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  * Description: cast engine dfx.
15  * Author: wangxueshuang
16  * Create: 2023-06-05
17  */
18 
19 #include "cast_engine_dfx.h"
20 
21 #include <string>
22 
23 #include "cast_engine_log.h"
24 #include "hisysevent.h"
25 #include "json.hpp"
26 
27 using nlohmann::json;
28 
29 namespace OHOS {
30 namespace CastEngine {
31 DEFINE_CAST_ENGINE_LABEL("Cast-Dfx");
32 
33 json CastEngineDfx::jsonSteamInfo_ = {};
34 json CastEngineDfx::jsonLocalDeviceInfo_ = {};
35 json CastEngineDfx::jsonRemoteDeviceInfo_ = {};
36 json CastEngineDfx::jsonConnectInfo_ = {};
37 const std::string CastEngineDfx::PACKAGE_NAME = "CastEngineService";
38 const std::string CastEngineDfx::SEQUENTIAL_ID_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
39 
WriteErrorEvent(int32_t errorCode)40 void CastEngineDfx::WriteErrorEvent(int32_t errorCode)
41 {
42     CLOGD("In.");
43     HiSysEventWrite(CAST_ENGINE_DFX_DOMAIN_NAME, "CAST_ENGINE_ERR", HiviewDFX::HiSysEvent::EventType::FAULT,
44         "ERROR_CODE", errorCode);
45 }
46 
SetStreamInfo(const std::string &streamInfoKey, const std::string &streamInfoValue)47 void CastEngineDfx::SetStreamInfo(const std::string &streamInfoKey, const std::string &streamInfoValue)
48 {
49     CLOGD("In.");
50     jsonSteamInfo_[streamInfoKey] = streamInfoValue;
51 }
52 
GetStreamInfo()53 std::string CastEngineDfx::GetStreamInfo()
54 {
55     CLOGD("In.");
56     return jsonSteamInfo_.dump();
57 }
58 
SetLocalDeviceInfo(const std::string &localDeviceInfoKey, const std::string &localDeviceInfoValue)59 void CastEngineDfx::SetLocalDeviceInfo(const std::string &localDeviceInfoKey, const std::string &localDeviceInfoValue)
60 {
61     CLOGD("In.");
62     jsonLocalDeviceInfo_[localDeviceInfoKey] = localDeviceInfoValue;
63 }
64 
GetLocalDeviceInfo()65 std::string CastEngineDfx::GetLocalDeviceInfo()
66 {
67     CLOGD("In.");
68     return jsonLocalDeviceInfo_.dump();
69 }
70 
SetRemoteDeviceInfo(const std::string &remoteDeviceInfoKey, const std::string &remoteDeviceInfoValue)71 void CastEngineDfx::SetRemoteDeviceInfo(const std::string &remoteDeviceInfoKey,
72     const std::string &remoteDeviceInfoValue)
73 {
74     CLOGD("In.");
75     jsonRemoteDeviceInfo_[remoteDeviceInfoKey] = remoteDeviceInfoValue;
76 }
77 
GetRemoteDeviceInfo()78 std::string CastEngineDfx::GetRemoteDeviceInfo()
79 {
80     CLOGD("In.");
81     return jsonRemoteDeviceInfo_.dump();
82 }
83 
SetConnectInfo(const std::string &connectInfoKey, const std::string &connectInfoValue)84 void CastEngineDfx::SetConnectInfo(const std::string &connectInfoKey, const std::string &connectInfoValue)
85 {
86     CLOGD("In.");
87     jsonConnectInfo_[connectInfoKey] = connectInfoValue;
88 }
89 
GetConnectInfo()90 std::string CastEngineDfx::GetConnectInfo()
91 {
92     CLOGD("In.");
93     return jsonConnectInfo_.dump();
94 }
95 
GetSequentialId()96 std::string CastEngineDfx::GetSequentialId()
97 {
98     CLOGD("In.");
99     std::string sequentialId = "";
100     srand(static_cast<unsigned>(time(NULL)));
101     for (int i = 0; i < SN_LENGTH; i++) {
102         int number = rand() % SEQUENTIAL_ID_CHARS_LENGTH;
103         sequentialId.push_back(SEQUENTIAL_ID_CHARS[number]);
104     }
105     CLOGD("sequential ID: %s.", sequentialId.c_str());
106     return sequentialId;
107 }
108 
GetBizPackageName()109 std::string CastEngineDfx::GetBizPackageName()
110 {
111     CLOGD("In.");
112     return PACKAGE_NAME;
113 }
114 
GetBIZSceneType(int protocols)115 int GetBIZSceneType(int protocols)
116 {
117     static std::map<ProtocolType, BIZSceneType> typeRelation {
118         {ProtocolType::CAST_PLUS_MIRROR, BIZSceneType::CAST_MIRROR},
119         {ProtocolType::CAST_PLUS_STREAM, BIZSceneType::CAST_STREAM},
120         {ProtocolType::DLNA, BIZSceneType::DLNA},
121         {ProtocolType::MIRACAST, BIZSceneType::MIRACAST},
122         {ProtocolType::COOPERATION, BIZSceneType::COOPERATION}
123     };
124 
125     auto sceneType = BIZSceneType::CAST_MIRROR;
126     if (auto itr = typeRelation.find(static_cast<ProtocolType>(protocols)); itr != typeRelation.cend()) {
127         sceneType = itr->second;
128     }
129 
130     return static_cast<int>(sceneType);
131 }
132 
HiSysEventWriteWrap(const std::string& funcName, const RadarParamInt& paramInt, const RadarParamString& paramStr)133 void HiSysEventWriteWrap(const std::string& funcName, const RadarParamInt& paramInt,
134     const RadarParamString& paramStr)
135 {
136     int32_t bizState = -1;
137     if (auto it = paramInt.find("BIZ_STATE"); it != paramInt.cend()) {
138         bizState = it->second;
139     }
140 
141     if (bizState == -1) {
142         HiSysEventWrite(
143             CAST_ENGINE_DFX_DOMAIN_NAME, EVENT_NAME, HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "ORG_PKG", ORG_PKG,
144             "FUNC", funcName,
145             "BIZ_SCENE", paramInt.at("BIZ_SCENE"),
146             "BIZ_STAGE", paramInt.at("BIZ_STAGE"),
147             "STAGE_RES", paramInt.at("STAGE_RES"),
148             "ERROR_CODE", paramInt.at("ERROR_CODE"),
149             "TO_CALL_PKG", paramStr.at("TO_CALL_PKG"),
150             "LOCAL_SESS_NAME", paramStr.at("LOCAL_SESS_NAME"),
151             "PEER_SESS_NAME", paramStr.at("PEER_SESS_NAME"),
152             "PEER_UDID", paramStr.at("PEER_UDID"));
153     } else {
154         HiSysEventWrite(
155             CAST_ENGINE_DFX_DOMAIN_NAME, EVENT_NAME, HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "ORG_PKG", ORG_PKG,
156             "FUNC", funcName,
157             "BIZ_SCENE", paramInt.at("BIZ_SCENE"),
158             "BIZ_STATE", paramInt.at("BIZ_STATE"),
159             "BIZ_STAGE", paramInt.at("BIZ_STAGE"),
160             "STAGE_RES", paramInt.at("STAGE_RES"),
161             "ERROR_CODE", paramInt.at("ERROR_CODE"),
162             "TO_CALL_PKG", paramStr.at("TO_CALL_PKG"),
163             "LOCAL_SESS_NAME", paramStr.at("LOCAL_SESS_NAME"),
164             "PEER_SESS_NAME", paramStr.at("PEER_SESS_NAME"),
165             "PEER_UDID", paramStr.at("PEER_UDID"));
166     }
167 }
168 } // namespace CastEngine
169 } // namespace OHOS
170