11c1b0f19Sopenharmony_ci/*
21c1b0f19Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
31c1b0f19Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41c1b0f19Sopenharmony_ci * you may not use this file except in compliance with the License.
51c1b0f19Sopenharmony_ci * You may obtain a copy of the License at
61c1b0f19Sopenharmony_ci *
71c1b0f19Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81c1b0f19Sopenharmony_ci *
91c1b0f19Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101c1b0f19Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111c1b0f19Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121c1b0f19Sopenharmony_ci * See the License for the specific language governing permissions and
131c1b0f19Sopenharmony_ci * limitations under the License.
141c1b0f19Sopenharmony_ci */
151c1b0f19Sopenharmony_ci
161c1b0f19Sopenharmony_ci#include "dcamera_hisysevent_adapter.h"
171c1b0f19Sopenharmony_ci
181c1b0f19Sopenharmony_ci#include "hisysevent.h"
191c1b0f19Sopenharmony_ci#include "securec.h"
201c1b0f19Sopenharmony_ci
211c1b0f19Sopenharmony_ci#include "anonymous_string.h"
221c1b0f19Sopenharmony_ci#include "distributed_camera_errno.h"
231c1b0f19Sopenharmony_ci#include "distributed_hardware_log.h"
241c1b0f19Sopenharmony_ci
251c1b0f19Sopenharmony_cinamespace OHOS {
261c1b0f19Sopenharmony_cinamespace DistributedHardware {
271c1b0f19Sopenharmony_cinamespace {
281c1b0f19Sopenharmony_ciconstexpr int32_t MSG_MAX_LEN = 2048;
291c1b0f19Sopenharmony_ciconstexpr int32_t ENUM_STREAMTYPE_LEN = 2;
301c1b0f19Sopenharmony_ciconstexpr int32_t ENUM_ENCODETYPE_LEN = 4;
311c1b0f19Sopenharmony_ciusing HiSysEventNameSpace = OHOS::HiviewDFX::HiSysEvent;
321c1b0f19Sopenharmony_ciconst std::string ENUM_STREAMTYPE_STRINGS[] = {
331c1b0f19Sopenharmony_ci    "CONTINUOUS_FRAME", "SNAPSHOT_FRAME"
341c1b0f19Sopenharmony_ci};
351c1b0f19Sopenharmony_ciconst std::string ENUM_ENCODETYPE_STRINGS[] = {
361c1b0f19Sopenharmony_ci    "ENCODE_TYPE_NULL", "ENCODE_TYPE_H264", "ENCODE_TYPE_H265", "ENCODE_TYPE_JPEG"
371c1b0f19Sopenharmony_ci};
381c1b0f19Sopenharmony_ci}
391c1b0f19Sopenharmony_ci
401c1b0f19Sopenharmony_civoid ReportDcamerInitFail(const std::string& eventName, int32_t errCode, const std::string& errMsg)
411c1b0f19Sopenharmony_ci{
421c1b0f19Sopenharmony_ci    int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::DISTRIBUTED_CAMERA,
431c1b0f19Sopenharmony_ci        eventName,
441c1b0f19Sopenharmony_ci        HiSysEventNameSpace::EventType::FAULT,
451c1b0f19Sopenharmony_ci        "ERRCODE", errCode,
461c1b0f19Sopenharmony_ci        "MSG", errMsg);
471c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
481c1b0f19Sopenharmony_ci        DHLOGE("Write HiSysEvent error ret %{public}d, errMsg %{public}s.", ret, errMsg.c_str());
491c1b0f19Sopenharmony_ci    }
501c1b0f19Sopenharmony_ci}
511c1b0f19Sopenharmony_ci
521c1b0f19Sopenharmony_civoid ReportRegisterCameraFail(const std::string& eventName, const std::string& devId, const std::string& dhId,
531c1b0f19Sopenharmony_ci    std::string version, const std::string& errMsg)
541c1b0f19Sopenharmony_ci{
551c1b0f19Sopenharmony_ci    int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::DISTRIBUTED_CAMERA,
561c1b0f19Sopenharmony_ci        eventName,
571c1b0f19Sopenharmony_ci        HiSysEventNameSpace::EventType::FAULT,
581c1b0f19Sopenharmony_ci        "DEVID", devId,
591c1b0f19Sopenharmony_ci        "DHID", dhId,
601c1b0f19Sopenharmony_ci        "VERSION", version,
611c1b0f19Sopenharmony_ci        "MSG", errMsg);
621c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
631c1b0f19Sopenharmony_ci        DHLOGE("Write HiSysEvent error ret %d, devId %s, dhId %s, errMsg %s.", ret, GetAnonyString(devId).c_str(),
641c1b0f19Sopenharmony_ci            dhId.c_str(), errMsg.c_str());
651c1b0f19Sopenharmony_ci    }
661c1b0f19Sopenharmony_ci}
671c1b0f19Sopenharmony_ci
681c1b0f19Sopenharmony_civoid ReportDcamerOptFail(const std::string& eventName, int32_t errCode, const std::string& errMsg)
691c1b0f19Sopenharmony_ci{
701c1b0f19Sopenharmony_ci    int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::DISTRIBUTED_CAMERA,
711c1b0f19Sopenharmony_ci        eventName,
721c1b0f19Sopenharmony_ci        HiSysEventNameSpace::EventType::FAULT,
731c1b0f19Sopenharmony_ci        "ERRCODE", errCode,
741c1b0f19Sopenharmony_ci        "MSG", errMsg);
751c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
761c1b0f19Sopenharmony_ci        DHLOGE("Write HiSysEvent error ret %{public}d, errMsg %{public}s.", ret, errMsg.c_str());
771c1b0f19Sopenharmony_ci    }
781c1b0f19Sopenharmony_ci}
791c1b0f19Sopenharmony_ci
801c1b0f19Sopenharmony_civoid ReportSaEvent(const std::string& eventName, int32_t saId, const std::string& errMsg)
811c1b0f19Sopenharmony_ci{
821c1b0f19Sopenharmony_ci    int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::DISTRIBUTED_CAMERA,
831c1b0f19Sopenharmony_ci        eventName,
841c1b0f19Sopenharmony_ci        HiSysEventNameSpace::EventType::BEHAVIOR,
851c1b0f19Sopenharmony_ci        "SAID", saId,
861c1b0f19Sopenharmony_ci        "MSG", errMsg);
871c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
881c1b0f19Sopenharmony_ci        DHLOGE("Write HiSysEvent error, ret:%{public}d, errMsg %{public}s.", ret, errMsg.c_str());
891c1b0f19Sopenharmony_ci    }
901c1b0f19Sopenharmony_ci}
911c1b0f19Sopenharmony_ci
921c1b0f19Sopenharmony_civoid ReportRegisterCameraEvent(const std::string& eventName, const std::string& devId, const std::string& dhId,
931c1b0f19Sopenharmony_ci    std::string version, const std::string& errMsg)
941c1b0f19Sopenharmony_ci{
951c1b0f19Sopenharmony_ci    int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::DISTRIBUTED_CAMERA,
961c1b0f19Sopenharmony_ci        eventName,
971c1b0f19Sopenharmony_ci        HiSysEventNameSpace::EventType::BEHAVIOR,
981c1b0f19Sopenharmony_ci        "DEVID", devId,
991c1b0f19Sopenharmony_ci        "DHID", dhId,
1001c1b0f19Sopenharmony_ci        "VERSION", version,
1011c1b0f19Sopenharmony_ci        "MSG", errMsg);
1021c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
1031c1b0f19Sopenharmony_ci        DHLOGE("Write HiSysEvent error, ret:%{public}d, errMsg %{public}s.", ret, errMsg.c_str());
1041c1b0f19Sopenharmony_ci    }
1051c1b0f19Sopenharmony_ci}
1061c1b0f19Sopenharmony_ci
1071c1b0f19Sopenharmony_civoid ReportCameraOperaterEvent(const std::string& eventName, const std::string& devId, const std::string& dhId,
1081c1b0f19Sopenharmony_ci    const std::string& errMsg)
1091c1b0f19Sopenharmony_ci{
1101c1b0f19Sopenharmony_ci    int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::DISTRIBUTED_CAMERA,
1111c1b0f19Sopenharmony_ci        eventName,
1121c1b0f19Sopenharmony_ci        HiSysEventNameSpace::EventType::BEHAVIOR,
1131c1b0f19Sopenharmony_ci        "DEVID", devId,
1141c1b0f19Sopenharmony_ci        "DHID", dhId,
1151c1b0f19Sopenharmony_ci        "MSG", errMsg);
1161c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
1171c1b0f19Sopenharmony_ci        DHLOGE("Write HiSysEvent error, ret:%{public}d, errMsg %{public}s.", ret, errMsg.c_str());
1181c1b0f19Sopenharmony_ci    }
1191c1b0f19Sopenharmony_ci}
1201c1b0f19Sopenharmony_ci
1211c1b0f19Sopenharmony_civoid ReportStartCaptureEvent(const std::string& eventName, EventCaptureInfo& capture, const std::string& errMsg)
1221c1b0f19Sopenharmony_ci{
1231c1b0f19Sopenharmony_ci    if (capture.encodeType_ < 0 || capture.encodeType_ >= ENUM_ENCODETYPE_LEN ||
1241c1b0f19Sopenharmony_ci        capture.type_ < 0 || capture.type_ >= ENUM_STREAMTYPE_LEN) {
1251c1b0f19Sopenharmony_ci        DHLOGE("Invalid capture parameters.");
1261c1b0f19Sopenharmony_ci        return;
1271c1b0f19Sopenharmony_ci    }
1281c1b0f19Sopenharmony_ci    int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::DISTRIBUTED_CAMERA,
1291c1b0f19Sopenharmony_ci        eventName,
1301c1b0f19Sopenharmony_ci        HiSysEventNameSpace::EventType::BEHAVIOR,
1311c1b0f19Sopenharmony_ci        "WIDTH", capture.width_,
1321c1b0f19Sopenharmony_ci        "HEIGHT", capture.height_,
1331c1b0f19Sopenharmony_ci        "FORMAT", capture.format_,
1341c1b0f19Sopenharmony_ci        "ISCAPTURE", capture.isCapture_ ? "true" : "false",
1351c1b0f19Sopenharmony_ci        "ENCODETYPE", ENUM_ENCODETYPE_STRINGS[capture.encodeType_],
1361c1b0f19Sopenharmony_ci        "STREAMTYPE", ENUM_STREAMTYPE_STRINGS[capture.type_],
1371c1b0f19Sopenharmony_ci        "MSG", errMsg);
1381c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
1391c1b0f19Sopenharmony_ci        DHLOGE("Write HiSysEvent error, ret:%{public}d, errMsg %{public}s.", ret, errMsg.c_str());
1401c1b0f19Sopenharmony_ci    }
1411c1b0f19Sopenharmony_ci}
1421c1b0f19Sopenharmony_ci
1431c1b0f19Sopenharmony_cistd::string CreateMsg(const char *format, ...)
1441c1b0f19Sopenharmony_ci{
1451c1b0f19Sopenharmony_ci    va_list args;
1461c1b0f19Sopenharmony_ci    va_start(args, format);
1471c1b0f19Sopenharmony_ci    char msg[MSG_MAX_LEN] = {0};
1481c1b0f19Sopenharmony_ci    if (vsnprintf_s(msg, sizeof(msg), sizeof(msg) - 1, format, args) < 0) {
1491c1b0f19Sopenharmony_ci        DHLOGE("failed to call vsnprintf_s");
1501c1b0f19Sopenharmony_ci        va_end(args);
1511c1b0f19Sopenharmony_ci        return "";
1521c1b0f19Sopenharmony_ci    }
1531c1b0f19Sopenharmony_ci    va_end(args);
1541c1b0f19Sopenharmony_ci    return msg;
1551c1b0f19Sopenharmony_ci}
1561c1b0f19Sopenharmony_ci} // namespace DistributedHardware
1571c1b0f19Sopenharmony_ci} // namespace OHOS