1 /*
2 * Copyright (c) 2022 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 "dcamera_hisysevent_adapter.h"
17
18 #include "hisysevent.h"
19 #include "securec.h"
20
21 #include "distributed_camera_errno.h"
22 #include "distributed_hardware_log.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
26 namespace {
27 constexpr int32_t MSG_MAX_LEN = 2048;
28 using HiSysEventNameSpace = OHOS::HiviewDFX::HiSysEvent;
29 const std::string DOMAIN_STR = std::string(HiSysEventNameSpace::Domain::DISTRIBUTED_CAMERA);
30 const std::string ENUM_STREAMTYPE_STRINGS[] = {
31 "CONTINUOUS_FRAME", "SNAPSHOT_FRAME"
32 };
33 const std::string ENUM_ENCODETYPE_STRINGS[] = {
34 "ENCODE_TYPE_NULL", "ENCODE_TYPE_H264", "ENCODE_TYPE_H265", "ENCODE_TYPE_JPEG"
35 };
36 }
37
ReportDcamerInitFail(const std::string & eventName,int32_t errCode,const std::string & errMsg)38 void ReportDcamerInitFail(const std::string& eventName, int32_t errCode, const std::string& errMsg)
39 {
40 int32_t ret = HiSysEventNameSpace::Write(DOMAIN_STR,
41 eventName,
42 HiSysEventNameSpace::EventType::FAULT,
43 "ERRCODE", errCode,
44 "MSG", errMsg);
45 if (ret != DCAMERA_OK) {
46 DHLOGE("Write HiSysEvent error ret %d, errMsg %s.", ret, errMsg.c_str());
47 }
48 }
49
ReportRegisterCameraFail(const std::string & eventName,const std::string & devId,const std::string & dhId,std::string version,const std::string & errMsg)50 void ReportRegisterCameraFail(const std::string& eventName, const std::string& devId, const std::string& dhId,
51 std::string version, const std::string& errMsg)
52 {
53 int32_t ret = HiSysEventNameSpace::Write(DOMAIN_STR,
54 eventName,
55 HiSysEventNameSpace::EventType::FAULT,
56 "DEVID", devId,
57 "DHID", dhId,
58 "VERSION", version,
59 "MSG", errMsg);
60 if (ret != DCAMERA_OK) {
61 DHLOGE("Write HiSysEvent error ret %d, devId %s, dhId %s, errMsg %s.", ret, devId.c_str(),
62 dhId.c_str(), errMsg.c_str());
63 }
64 }
65
ReportDcamerOptFail(const std::string & eventName,int32_t errCode,const std::string & errMsg)66 void ReportDcamerOptFail(const std::string& eventName, int32_t errCode, const std::string& errMsg)
67 {
68 int32_t ret = HiSysEventNameSpace::Write(DOMAIN_STR,
69 eventName,
70 HiSysEventNameSpace::EventType::FAULT,
71 "ERRCODE", errCode,
72 "MSG", errMsg);
73 if (ret != DCAMERA_OK) {
74 DHLOGE("Write HiSysEvent error ret %d, errMsg %s.", ret, errMsg.c_str());
75 }
76 }
77
ReportSaEvent(const std::string & eventName,int32_t saId,const std::string & errMsg)78 void ReportSaEvent(const std::string& eventName, int32_t saId, const std::string& errMsg)
79 {
80 int32_t ret = HiSysEventNameSpace::Write(DOMAIN_STR,
81 eventName,
82 HiSysEventNameSpace::EventType::BEHAVIOR,
83 "SAID", saId,
84 "MSG", errMsg);
85 if (ret != DCAMERA_OK) {
86 DHLOGE("Write HiSysEvent error, ret:%d, errMsg %s.", ret, errMsg.c_str());
87 }
88 }
89
ReportRegisterCameraEvent(const std::string & eventName,const std::string & devId,const std::string & dhId,std::string version,const std::string & errMsg)90 void ReportRegisterCameraEvent(const std::string& eventName, const std::string& devId, const std::string& dhId,
91 std::string version, const std::string& errMsg)
92 {
93 int32_t ret = HiSysEventNameSpace::Write(DOMAIN_STR,
94 eventName,
95 HiSysEventNameSpace::EventType::BEHAVIOR,
96 "DEVID", devId,
97 "DHID", dhId,
98 "VERSION", version,
99 "MSG", errMsg);
100 if (ret != DCAMERA_OK) {
101 DHLOGE("Write HiSysEvent error, ret:%d, errMsg %s.", ret, errMsg.c_str());
102 }
103 }
104
ReportCameraOperaterEvent(const std::string & eventName,const std::string & devId,const std::string & dhId,const std::string & errMsg)105 void ReportCameraOperaterEvent(const std::string& eventName, const std::string& devId, const std::string& dhId,
106 const std::string& errMsg)
107 {
108 int32_t ret = HiSysEventNameSpace::Write(DOMAIN_STR,
109 eventName,
110 HiSysEventNameSpace::EventType::BEHAVIOR,
111 "DEVID", devId,
112 "DHID", dhId,
113 "MSG", errMsg);
114 if (ret != DCAMERA_OK) {
115 DHLOGE("Write HiSysEvent error, ret:%d, errMsg %s.", ret, errMsg.c_str());
116 }
117 }
118
ReportStartCaptureEvent(const std::string & eventName,EventCaptureInfo & capture,const std::string & errMsg)119 void ReportStartCaptureEvent(const std::string& eventName, EventCaptureInfo& capture, const std::string& errMsg)
120 {
121 int32_t ret = HiSysEventNameSpace::Write(DOMAIN_STR,
122 eventName,
123 HiSysEventNameSpace::EventType::BEHAVIOR,
124 "WIDTH", capture.width_,
125 "HEIGHT", capture.height_,
126 "FORMAT", capture.format_,
127 "ISCAPTURE", capture.isCapture_ ? "true" : "false",
128 "ENCODETYPE", ENUM_ENCODETYPE_STRINGS[capture.encodeType_],
129 "STREAMTYPE", ENUM_STREAMTYPE_STRINGS[capture.type_],
130 "MSG", errMsg);
131 if (ret != DCAMERA_OK) {
132 DHLOGE("Write HiSysEvent error, ret:%d, errMsg %s.", ret, errMsg.c_str());
133 }
134 }
135
CreateMsg(const char * format,...)136 std::string CreateMsg(const char *format, ...)
137 {
138 va_list args;
139 (void)memset_s(&args, sizeof(va_list), 0, sizeof(va_list));
140 va_start(args, format);
141 char msg[MSG_MAX_LEN] = {0};
142 if (vsnprintf_s(msg, sizeof(msg), sizeof(msg) - 1, format, args) < 0) {
143 DHLOGE("failed to call vsnprintf_s");
144 va_end(args);
145 return "";
146 }
147 va_end(args);
148 return msg;
149 }
150 } // namespace DistributedHardware
151 } // namespace OHOS