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 
16 #include "dfx/distributed_ue.h"
17 #include "dfx/dms_continue_time_dumper.h"
18 #include "dms_constant.h"
19 
20 #include "hisysevent.h"
21 
22 namespace OHOS {
23 namespace DistributedSchedule {
24 namespace {
25     const std::string TAG = "DmsUE";
26     const std::string NO_FAILED_EXTRAINFO = "NULL";
27     constexpr int32_t DEVICE_TYPE_UNKNOWN = 0;
28 }
29 
30 IMPLEMENT_SINGLE_INSTANCE(DmsUE);
31 
NotifyDockShowIcon(const std::string & bundleName,const std::string & abilityName,const std::string & networkId,int32_t errCode)32 bool DmsUE::NotifyDockShowIcon(const std::string& bundleName, const std::string& abilityName,
33     const std::string& networkId, int32_t errCode)
34 {
35     int32_t res = ERR_OK;
36     if (errCode == ERR_OK) {
37         res = HiSysEventWrite(
38             CONTINUATION_DOMAIN,
39             SHOW_CONTINUATION_ICON,
40             HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
41             PNAMEID, CONTINUATION_DOMAIN,
42             PVERSIONID, Constants::DMS_VERSION,
43             BUNDLE_NAME, bundleName,
44             ABILITY_NAME, abilityName,
45             SOURCE_DEVICE_TYPE, GetDeviceTypeByNetworkId(networkId));
46     } else {
47         res = HiSysEventWrite(
48             CONTINUATION_DOMAIN,
49             SHOW_CONTINUATION_ICON,
50             HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
51             PNAMEID, CONTINUATION_DOMAIN,
52             PVERSIONID, Constants::DMS_VERSION,
53             BUNDLE_NAME, bundleName,
54             ABILITY_NAME, abilityName,
55             SOURCE_DEVICE_TYPE, GetDeviceTypeByNetworkId(networkId),
56             ERROR_CODE_UE, errCode);
57     }
58     if (res != ERR_OK) {
59         HILOGE("NotifyDockShowIcon error, res:%{public}d", res);
60         return false;
61     }
62     return true;
63 }
64 
TriggerDmsContinue(const std::string & bundleName,const std::string & abilityName,const std::string & networkId,int32_t errCode)65 bool DmsUE::TriggerDmsContinue(const std::string& bundleName, const std::string& abilityName,
66     const std::string& networkId, int32_t errCode)
67 {
68     int32_t res = ERR_OK;
69     if (errCode == ERR_OK) {
70         res = HiSysEventWrite(
71             CONTINUATION_DOMAIN,
72             CLICK_CONTINUATION_ICON,
73             HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
74             PNAMEID, CONTINUATION_DOMAIN,
75             PVERSIONID, Constants::DMS_VERSION,
76             BUNDLE_NAME, bundleName,
77             ABILITY_NAME, abilityName,
78             SOURCE_DEVICE_TYPE, GetDeviceTypeByNetworkId(networkId));
79     } else {
80         res = HiSysEventWrite(
81             CONTINUATION_DOMAIN,
82             CLICK_CONTINUATION_ICON,
83             HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
84             PNAMEID, CONTINUATION_DOMAIN,
85             PVERSIONID, Constants::DMS_VERSION,
86             BUNDLE_NAME, bundleName,
87             ABILITY_NAME, abilityName,
88             SOURCE_DEVICE_TYPE, GetDeviceTypeByNetworkId(networkId),
89             ERROR_CODE_UE, errCode);
90     }
91     if (res != ERR_OK) {
92         HILOGE("TriggerDmsContinue error, res:%{public}d", res);
93         return false;
94     }
95     return true;
96 }
97 
DmsContinueComplete(const std::string & bundleName,const std::string & abilityName,const std::string & networkId,int32_t errCode)98 bool DmsUE::DmsContinueComplete(const std::string& bundleName, const std::string& abilityName,
99     const std::string& networkId, int32_t errCode)
100 {
101     int32_t res = ERR_OK;
102     if (errCode == ERR_OK) {
103         res = HiSysEventWrite(
104             CONTINUATION_DOMAIN,
105             COMPLETE_OF_CONTINUATION,
106             HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
107             PNAMEID, CONTINUATION_DOMAIN,
108             PVERSIONID, Constants::DMS_VERSION,
109             BUNDLE_NAME, bundleName,
110             ABILITY_NAME, abilityName,
111             SOURCE_DEVICE_TYPE, GetDeviceTypeByNetworkId(networkId),
112             CONTINUATION_STATE, static_cast<int32_t>(continuationState::CONTINUATION_SUCC),
113             FAILED_EXTRAINFO, NO_FAILED_EXTRAINFO,
114             CONTINUATION_DURATION, DmsContinueTime::GetInstance().GetTotalTime());
115     } else {
116         res = HiSysEventWrite(
117             CONTINUATION_DOMAIN,
118             COMPLETE_OF_CONTINUATION,
119             HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
120             PNAMEID, CONTINUATION_DOMAIN,
121             PVERSIONID, Constants::DMS_VERSION,
122             BUNDLE_NAME, bundleName,
123             ABILITY_NAME, abilityName,
124             SOURCE_DEVICE_TYPE, GetDeviceTypeByNetworkId(networkId),
125             CONTINUATION_STATE, static_cast<int32_t>(continuationState::CONTINUATION_FAIL),
126             FAILED_EXTRAINFO, ConvertErrCodeToStr(errCode),
127             CONTINUATION_DURATION, DmsContinueTime::GetInstance().GetTotalTime(),
128             ERROR_CODE_UE, errCode);
129     }
130     if (res != ERR_OK) {
131         HILOGE("DmsContinueComplete error, res:%{public}d", res);
132         return false;
133     }
134     return true;
135 }
136 
OriginalSwitchState(bool isContinueSwitchOn,int32_t errCode)137 bool DmsUE::OriginalSwitchState(bool isContinueSwitchOn, int32_t errCode)
138 {
139     int32_t res = ERR_OK;
140     std::string switchState = isContinueSwitchOn ? "1" : "0";
141     if (errCode == ERR_OK) {
142         res = HiSysEventWrite(
143             CONTINUATION_DOMAIN,
144             ORIGINAL_SWITCH_STATE,
145             HiviewDFX::HiSysEvent::EventType::STATISTIC,
146             PNAMEID, CONTINUATION_DOMAIN,
147             PVERSIONID, Constants::DMS_VERSION,
148             SWITCH_STATE, switchState);
149     } else {
150         res = HiSysEventWrite(
151             CONTINUATION_DOMAIN,
152             ORIGINAL_SWITCH_STATE,
153             HiviewDFX::HiSysEvent::EventType::STATISTIC,
154             PNAMEID, CONTINUATION_DOMAIN,
155             PVERSIONID, Constants::DMS_VERSION,
156             SWITCH_STATE, switchState,
157             ERROR_CODE_UE, errCode);
158     }
159     if (res != ERR_OK) {
160         HILOGE("OriginalSwitchState error, res:%{public}d", res);
161         return false;
162     }
163     return true;
164 }
165 
ChangedSwitchState(bool isContinueSwitchOn,int32_t errCode)166 bool DmsUE::ChangedSwitchState(bool isContinueSwitchOn, int32_t errCode)
167 {
168     int32_t res = ERR_OK;
169     std::string switchState = isContinueSwitchOn ? "1" : "0";
170     if (errCode == ERR_OK) {
171         res = HiSysEventWrite(
172             CONTINUATION_DOMAIN,
173             CHANGED_SWITCH_STATE,
174             HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
175             PNAMEID, CONTINUATION_DOMAIN,
176             PVERSIONID, Constants::DMS_VERSION,
177             SWITCH_STATE, switchState);
178     } else {
179         res = HiSysEventWrite(
180             CONTINUATION_DOMAIN,
181             CHANGED_SWITCH_STATE,
182             HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
183             PNAMEID, CONTINUATION_DOMAIN,
184             PVERSIONID, Constants::DMS_VERSION,
185             SWITCH_STATE, switchState,
186             ERROR_CODE_UE, errCode);
187     }
188     if (res != ERR_OK) {
189         HILOGE("ChangedSwitchState error, res:%{public}d", res);
190         return false;
191     }
192     return true;
193 }
194 
GetDeviceTypeByNetworkId(std::string networkId)195 int32_t DmsUE::GetDeviceTypeByNetworkId(std::string networkId)
196 {
197     auto deviceInfo = DtbschedmgrDeviceInfoStorage::GetInstance().GetDeviceInfoById(networkId);
198     if (deviceInfo == nullptr) {
199         return DEVICE_TYPE_UNKNOWN;
200     }
201     return deviceInfo->GetDeviceType();
202 }
203 
ConvertErrCodeToStr(int32_t errorCode)204 std::string DmsUE::ConvertErrCodeToStr(int32_t errorCode)
205 {
206     if (errorCode == ERR_OK) {
207         return NO_FAILED_EXTRAINFO;
208     }
209     switch (errorCode) {
210         case INVALID_PARAMETERS_ERR:
211             return "invalid parameters";
212         case INVALID_REMOTE_PARAMETERS_ERR:
213             return "remote invalid parameters";
214         case DMS_START_CONTROL_PERMISSION_DENIED:
215             return "start control permission check failed";
216         case NO_MISSION_INFO_FOR_MISSION_ID:
217             return "failed to get the missionInfo of the specified missionId";
218         case OPERATION_DEVICE_NOT_INITIATOR_OR_TARGET:
219             return "the operation device must be the target device to be continued";
220         case CONTINUE_ALREADY_IN_PROGRESS:
221             return "the local continuation task is already in progress";
222         case MISSION_FOR_CONTINUING_IS_NOT_ALIVE:
223             return "the mission for continuing is not alive, try again after restart mission.";
224         case CONTINUE_SEND_EVENT_FAILED:
225             return "sending event failed during continuation";
226         case CONTINUE_STATE_MACHINE_INVALID_STATE:
227             return "state machine receving invalid state code";
228         case CONTINUE_SESSION_SHUTDOWN:
229             return "session shutdown during continuation";
230         case CONTINUE_CALL_CONTINUE_ABILITY_FAILED:
231             return "calling ContinueAbility failed during continuation";
232         case CONTINUE_CALL_START_ABILITY_FAILED:
233             return "calling StartAbility failed during continuation";
234         case CONTINUE_SINK_ABILITY_TERMINATED:
235             return "sink Ability abnormal termination during continuation";
236         default:
237             return "internal error";
238     }
239 }
240 } // namespace DistributedSchedule
241 } // namespace OHOS