1 /*
2 * Copyright (c) 2022 Chipsea Technologies (Shenzhen) Corp., 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 "dmd_report.h"
17
18 #include "datetime_ex.h"
19 #include "hisysevent.h"
20
21 #include "medical_errors.h"
22 #include "medical_log_domain.h"
23
24 namespace OHOS {
25 namespace Sensors {
26 using namespace OHOS::HiviewDFX;
27
28 namespace {
29 constexpr HiLogLabel LABEL = { LOG_CORE, MedicalSensorLogDomain::MEDICAL_SENSOR_UTILS, "DmdReport" };
30 constexpr int32_t SECONDS_HALF_HOUR = 1800;
31 } // namespace
32
33 std::map<int32_t, int64_t> DmdReport::eventMap_ = {
34 { JNI_ENV_VAR_EXCEPTION, 0 },
35 { CLASS_NOT_FOUND, 0 },
36 { NATIVE_METHOD_REGISTER_EXCEPTION, 0 },
37 { JAVA_VM_THREAD_ATTACH_EXCEPTION, 0 },
38 { SENSOR_SERVICE_EXCEPTION, 0 },
39 { MISC_SERVICE_EXCEPTION, 0 },
40 { SENSOR_SERVICE_IPC_EXCEPTION, 0 },
41 { MISC_SERVICE_IPC_EXCEPTION, 0 },
42 { SENSOR_HDF_SERVICE_EXCEPTION, 0 },
43 { SENSOR_DATA_CHANNEL_EXCEPTION, 0 },
44 };
45
46 std::mutex DmdReport::eventMutex_;
47
GetEventName(int32_t eventId)48 static std::string GetEventName(int32_t eventId)
49 {
50 switch (eventId) {
51 case JNI_ENV_VAR_EXCEPTION:
52 return "JniEnvVarException";
53 case CLASS_NOT_FOUND:
54 return "ClassNotFound";
55 case NATIVE_METHOD_REGISTER_EXCEPTION:
56 return "NativeMethodRegisterException";
57 case JAVA_VM_THREAD_ATTACH_EXCEPTION:
58 return "JavaVmThreadAttachException";
59 case SENSOR_SERVICE_EXCEPTION:
60 return "SensorServiceException";
61 case MISC_SERVICE_EXCEPTION:
62 return "MiscServiceException";
63 case SENSOR_SERVICE_IPC_EXCEPTION:
64 return "SensorServiceIpcException";
65 case MISC_SERVICE_IPC_EXCEPTION:
66 return "MiscServiceIpcException";
67 case SENSOR_HDF_SERVICE_EXCEPTION:
68 return "SensorHidlServiceException";
69 case SENSOR_DATA_CHANNEL_EXCEPTION:
70 return "SensorDataChannelException";
71 default:
72 return "";
73 }
74 }
75
ReportException(int32_t eventId,const std::string & interfaceName,int32_t error)76 void DmdReport::ReportException(int32_t eventId, const std::string &interfaceName, int32_t error)
77 {
78 HiLog::Debug(LABEL, "%{public}s begin", __func__);
79 std::lock_guard<std::mutex> eventLock(eventMutex_);
80 auto eventIt = eventMap_.find(eventId);
81 if (eventIt == eventMap_.end()) {
82 HiLog::Error(LABEL, "%{public}s eventId : %{public}d is not supported", __func__, eventId);
83 return;
84 }
85 int64_t curTime = GetSecondsSince1970ToNow();
86 if ((curTime - eventIt->second) > SECONDS_HALF_HOUR) {
87 HiviewDFX::HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::MEDICAL_SENSOR, GetEventName(eventId),
88 HiviewDFX::HiSysEvent::EventType::FAULT, interfaceName, error);
89 HiLog::Error(LABEL, "%{public}s eventId : %{public}d, interfaceName=%{public}s, error=%{public}d",
90 __func__, eventId, interfaceName.c_str(), error);
91 eventMap_[eventId] = curTime;
92 HiLog::Debug(LABEL, "%{public}s end", __func__);
93 return;
94 }
95 HiLog::Warn(LABEL, "%{public}s eventId is reported every half an hour", __func__);
96 }
97 } // namespace Sensors
98 } // namespace OHOS
99