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