• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "coordination_hisysevent.h"
17 
18 #include "fi_log.h"
19 
20 namespace OHOS {
21 namespace Msdp {
22 namespace DeviceStatus {
23 namespace {
24 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "CoordinationHiSysEvent" };
25 } // namespace
26 
27 std::map<CoordinationState, std::string> CoordinationDFX::coordinationState_ = {
28     { CoordinationState::STATE_FREE, "STATE_FREE" },
29     { CoordinationState::STATE_IN, "STATE_IN" },
30     { CoordinationState::STATE_OUT, "STATE_OUT" }
31 };
32 
33 std::map<CoorType, std::pair<std::string, std::string>> CoordinationDFX::serialStr_ = {
34     { CoorType::PREPARE_SUCC, { "PREPARE_SUCCESS", "Prepare coordination successfully" } },
35     { CoorType::PREPARE_FAIL, { "PREPARE_FAILED", "Prepare coordination failed" } },
36     { CoorType::UNPREPARE_SUCC, { "UNPREPARE_SUCCESS", "Unprepare coordination successfully" } },
37     { CoorType::UNPREPARE_FAIL, { "UNPREPARE_FAILED", "Unprepare coordination failed" } },
38     { CoorType::ACTIVATE_SUCC, { "ACTIVATE_SUCCESS", "Start remote coordination successfully" } },
39     { CoorType::ACTIVATE_FAIL, { "ACTIVATE_FAILED", "Start remote coordination failed" } },
40     { CoorType::ACTIVATE_RESULT, { "ACTIVATE_RESULT", "Start remote accordination result failed" } },
41     { CoorType::DEACTIVATE_SUCC, { "DEACTIVATE_SUCCESS", "Stop remote accordination successfully" } },
42     { CoorType::DEACTIVATE_FAIL, { "DEACTIVATE_FAILED", "Stop remote accordination failed" } },
43     { CoorType::DEACTIVATE_RESULT, { "DEACTIVATE_RESULT", "Stop remote accordination result failed" } },
44     { CoorType::OPEN_SOFUBUS_RESULT_ONE, { "OPEN_SOFUBUS_RESULT", "Can Not Open Softbus As Init Failed" } },
45     { CoorType::OPEN_SOFUBUS_RESULT_TWO, { "OPEN_SOFUBUS_RESULT", "Can Not Open Softbus As Open Session Failed" } },
46     { CoorType::OPEN_SOFUBUS_RESULT_THREE, { "OPEN_SOFUBUS_RESULT", "Can Not Open Softbus As Waiting Time Out" } },
47     { CoorType::INPUT_START_REMOTE_INPUT, { "D_INPUT_START_REMOTE_INPUT", "D_input start remote input failed" } },
48     { CoorType::INPUT_STOP_REMOTE_O, { "D_INPUT_STOP_REMOTE_INPUT_O", "D_input stop remote input failed" } },
49     { CoorType::INPUT_STOP_REMOTE_T, { "D_INPUT_STOP_REMOTE_INPUT_T", "D_input stop remote input failed" } },
50     { CoorType::INPUT_PRE_REMOTE_O, { "D_INPUT_PREPARE_REMOTE_INPUT_O", "D_input prepare remote input failed" } },
51     { CoorType::INPUT_PRE_REMOTE_T, { "D_INPUT_PREPARE_REMOTE_INPUT_T", "D_input prepare remote input failed" } },
52     { CoorType::INPUT_UNPRE_REMOTE_O, { "D_INPUT_UNPREPARE_REMOTE_INPUT_O", "D_input unprepare remote input failed" } },
53     { CoorType::INPUT_UNPRE_REMOTE_T, { "D_INPUT_UNPREPARE_REMOTE_INPUT_T", "D_input unprepare remote input failed" } },
54     { CoorType::COOP_DRAG_SUCC, { "COOPERATE_DRAG_SUCCESS", "On coordination and the state change successfully" } },
55     { CoorType::COOP_DRAG_FAIL, { "COOPERATE_DRAG_FAILED", "The current coordination state is out" } },
56     { CoorType::COOP_DRAG_RESULT_SUCC, { "COOPERATE_DRAG_RESULT_SUCCESS", "Coordination drag result successfully" } },
57     { CoorType::COOP_DRAG_RESULT_FAIL, { "COOPERATE_DRAG_RESULT_FAILED", "Coordination drag result failed" } }
58 };
59 
60 
61 template<typename... Types>
WriteInputFunc(const CoorType & coorType,Types...paras)62 int32_t CoordinationDFX::WriteInputFunc(const CoorType &coorType, Types... paras)
63 {
64     if (serialStr_.find(coorType) == serialStr_.end()) {
65         FI_HILOGE("serialStr_ can't find the coordination hisysevent type");
66         return RET_ERR;
67     }
68     auto &[label, dec] = serialStr_[coorType];
69     OHOS::HiviewDFX::HiSysEvent::EventType eventType = (static_cast<int32_t>(coorType) & 1) ?
70         OHOS::HiviewDFX::HiSysEvent::EventType::FAULT : OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR;
71     int32_t ret = HiSysEventWrite(
72         OHOS::HiviewDFX::HiSysEvent::Domain::MSDP,
73         label,
74         eventType,
75         paras...,
76         "MSG",
77         dec);
78     if (ret != RET_OK) {
79         FI_HILOGE("HiviewDFX write failed, ret:%{public}d", ret);
80     }
81     return ret;
82 }
83 
WritePrepare(int32_t monitorId,OHOS::HiviewDFX::HiSysEvent::EventType type)84 int32_t CoordinationDFX::WritePrepare(int32_t monitorId, OHOS::HiviewDFX::HiSysEvent::EventType type)
85 {
86     if (type == OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR) {
87         return WriteInputFunc(CoorType::PREPARE_SUCC, "Monitor_Id", monitorId, "IsOpen", true);
88     }
89     return WriteInputFunc(CoorType::PREPARE_FAIL, "Monitor_Id", monitorId, "IsOpen", false);
90 }
91 
WriteUnprepare(OHOS::HiviewDFX::HiSysEvent::EventType type)92 int32_t CoordinationDFX::WriteUnprepare(OHOS::HiviewDFX::HiSysEvent::EventType type)
93 {
94     if (type == OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR) {
95         return WriteInputFunc(CoorType::UNPREPARE_SUCC, "IsClose", true);
96     }
97     return WriteInputFunc(CoorType::UNPREPARE_FAIL, "IsClose", false);
98 }
99 
WriteActivate(const std::string & localNetworkId,const std::string & remoteNetworkId,std::map<std::string,int32_t> sessionDevMap_,OHOS::HiviewDFX::HiSysEvent::EventType type)100 int32_t CoordinationDFX::WriteActivate(const std::string &localNetworkId, const std::string &remoteNetworkId,
101     std::map<std::string, int32_t> sessionDevMap_, OHOS::HiviewDFX::HiSysEvent::EventType type)
102 {
103     if (type == OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR) {
104         return WriteInputFunc(CoorType::ACTIVATE_SUCC, "localNetworkId", localNetworkId.substr(0, SUB_LEN),
105             "remoteNetworkId", remoteNetworkId.substr(0, SUB_LEN));
106     }
107     return WriteInputFunc(CoorType::ACTIVATE_FAIL, "localNetworkId", localNetworkId.substr(0, SUB_LEN),
108         "remoteNetworkId", remoteNetworkId.substr(0, SUB_LEN));
109 }
110 
WriteActivateResult(const std::string & remoteNetworkId,bool isSuccess)111 int32_t CoordinationDFX::WriteActivateResult(const std::string &remoteNetworkId, bool isSuccess)
112 {
113     return WriteInputFunc(CoorType::ACTIVATE_RESULT, "remoteNetworkId", remoteNetworkId.substr(0, SUB_LEN), "IsSuccess",
114         isSuccess);
115 }
116 
WriteDeactivate(const std::string & remoteNetworkId,std::map<std::string,int32_t> sessionDevMap_,OHOS::HiviewDFX::HiSysEvent::EventType type)117 int32_t CoordinationDFX::WriteDeactivate(const std::string &remoteNetworkId,
118     std::map<std::string, int32_t> sessionDevMap_, OHOS::HiviewDFX::HiSysEvent::EventType type)
119 {
120     if (sessionDevMap_.find(remoteNetworkId) == sessionDevMap_.end()) {
121         FI_HILOGE("sessionDevMap_ can't find the remoteNetworkId");
122         return RET_ERR;
123     }
124     int32_t sessionId = sessionDevMap_[remoteNetworkId];
125     if (type == OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR) {
126         return WriteInputFunc(CoorType::DEACTIVATE_SUCC, "remoteNetworkId", remoteNetworkId.substr(0, SUB_LEN),
127             "sessionId", sessionId);
128     }
129     return WriteInputFunc(CoorType::DEACTIVATE_FAIL, "remoteNetworkId", remoteNetworkId.substr(0, SUB_LEN),
130         "sessionId", sessionId);
131 }
132 
WriteDeactivateResult(const std::string & remoteNetworkId,std::map<std::string,int32_t> sessionDevMap_)133 int32_t CoordinationDFX::WriteDeactivateResult(const std::string &remoteNetworkId,
134     std::map<std::string, int32_t> sessionDevMap_)
135 {
136     if (sessionDevMap_.find(remoteNetworkId) == sessionDevMap_.end()) {
137         FI_HILOGE("sessionDevMap_ can't find the remoteNetworkId");
138         return RET_ERR;
139     }
140     int32_t sessionId = sessionDevMap_[remoteNetworkId];
141     return WriteInputFunc(CoorType::DEACTIVATE_RESULT, "remoteNetworkId", remoteNetworkId.substr(0, SUB_LEN),
142         "sessionId", sessionId);
143 }
144 
WriteOpenSoftbusResult(const std::string & remoteNetworkId,int32_t para,int32_t type)145 int32_t CoordinationDFX::WriteOpenSoftbusResult(const std::string &remoteNetworkId, int32_t para, int32_t type)
146 {
147     if (type == INIT_SIGN) {
148         return WriteInputFunc(CoorType::OPEN_SOFUBUS_RESULT_ONE, "remoteNetworkId", remoteNetworkId.substr(0, SUB_LEN),
149             "Initsign", para);
150     } else if (type == SESS_SIGN) {
151         return WriteInputFunc(CoorType::OPEN_SOFUBUS_RESULT_TWO, "remoteNetworkId", remoteNetworkId.substr(0, SUB_LEN),
152             "sessionId", para);
153     } else {
154         return WriteInputFunc(CoorType::OPEN_SOFUBUS_RESULT_THREE, "remoteNetworkId",
155             remoteNetworkId.substr(0, SUB_LEN), "status", para);
156     }
157 }
158 
WriteCooperateDrag(const std::string & remoteNetworkId,CoordinationState previousSta,CoordinationState updateSta)159 int32_t CoordinationDFX::WriteCooperateDrag(const std::string &remoteNetworkId, CoordinationState previousSta,
160     CoordinationState updateSta)
161 {
162     if (coordinationState_.find(previousSta) == coordinationState_.end()) {
163         FI_HILOGE("coordinationState_ can't find the previous coordination state");
164         return RET_ERR;
165     }
166     if (coordinationState_.find(updateSta) == coordinationState_.end()) {
167         FI_HILOGE("coordinationState_ can't find the updated coordination state");
168         return RET_ERR;
169     }
170     std::string preState = coordinationState_[previousSta];
171     std::string upState = coordinationState_[updateSta];
172     return WriteInputFunc(CoorType::COOP_DRAG_SUCC, "PreviousState", preState, "UpdateState", upState);
173 }
174 
WriteCooperateDrag(const std::string & remoteNetworkId,CoordinationState currentSta)175 int32_t CoordinationDFX::WriteCooperateDrag(const std::string &remoteNetworkId, CoordinationState currentSta)
176 {
177     if (currentSta != CoordinationState::STATE_OUT) {
178         return RET_ERR;
179     }
180     if (coordinationState_.find(currentSta) == coordinationState_.end()) {
181         FI_HILOGE("coordinationState_ can't find the current coordination state");
182         return RET_ERR;
183     }
184     std::string curState = coordinationState_[currentSta];
185     return WriteInputFunc(CoorType::COOP_DRAG_FAIL, "CurrentState", curState);
186 }
187 
WriteCooperateDragResult(const std::string & remoteNetworkId,const CoordinationState & currentSta,OHOS::HiviewDFX::HiSysEvent::EventType type)188 int32_t CoordinationDFX::WriteCooperateDragResult(const std::string &remoteNetworkId,
189     const CoordinationState &currentSta, OHOS::HiviewDFX::HiSysEvent::EventType type)
190 {
191     if (coordinationState_.find(currentSta) == coordinationState_.end()) {
192         FI_HILOGE("coordinationState_ can't find the current coordination state");
193         return RET_ERR;
194     }
195     std::string curState = coordinationState_[currentSta];
196     if (type == OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR) {
197         return WriteInputFunc(CoorType::COOP_DRAG_RESULT_SUCC, "remotedeviceId", remoteNetworkId.substr(0, SUB_LEN),
198             "CurCoordinationState", curState);
199     }
200     return WriteInputFunc(CoorType::COOP_DRAG_RESULT_FAIL, "remotedeviceId", remoteNetworkId.substr(0, SUB_LEN),
201         "CurCoordinationState", curState);
202 }
203 } // namespace DeviceStatus
204 } // namespace Msdp
205 } // namespace OHOS
206