1/*
2 * Copyright (c) 2022-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#include "dh_utils_hisysevent.h"
16
17#include <unordered_map>
18
19#include "distributed_hardware_errno.h"
20#include "distributed_hardware_log.h"
21
22namespace OHOS {
23namespace DistributedHardware {
24void HiSysEventWriteMsg(const std::string &status, const OHOS::HiviewDFX::HiSysEvent::EventType eventType,
25    const std::string &msg)
26{
27    int32_t res = HiSysEventWrite(
28        OHOS::HiviewDFX::HiSysEvent::Domain::DISTRIBUTED_HARDWARE_FWK,
29        status.c_str(),
30        eventType,
31        "MSG", msg.c_str());
32    if (res != DH_FWK_SUCCESS) {
33        DHLOGE("Write HiSysEvent error, res:%{public}d", res);
34    }
35}
36
37void HiSysEventWriteErrCodeMsg(const std::string &status, const OHOS::HiviewDFX::HiSysEvent::EventType eventType,
38    int32_t errCode, const std::string &msg)
39{
40    int32_t res = HiSysEventWrite(
41        OHOS::HiviewDFX::HiSysEvent::Domain::DISTRIBUTED_HARDWARE_FWK,
42        status.c_str(),
43        eventType,
44        "ERR_CODE", errCode,
45        "ERR_MSG", msg.c_str());
46    if (res != DH_FWK_SUCCESS) {
47        DHLOGE("Write HiSysEvent error, res:%{public}d", res);
48    }
49}
50
51void HiSysEventWriteReleaseMsg(const std::string &status, const OHOS::HiviewDFX::HiSysEvent::EventType eventType,
52    const DHType dhType, int32_t errCode, const std::string &msg)
53{
54    std::string dhTypeStr = "UNKNOWN";
55    auto it = DHTypeStrMap.find(dhType);
56    if (it != DHTypeStrMap.end()) {
57        dhTypeStr = it->second;
58    }
59
60    int32_t res = HiSysEventWrite(
61        OHOS::HiviewDFX::HiSysEvent::Domain::DISTRIBUTED_HARDWARE_FWK,
62        status.c_str(),
63        eventType,
64        "DHTYPE", dhTypeStr.c_str(),
65        "ERR_CODE", errCode,
66        "ERR_MSG", msg.c_str());
67    if (res != DH_FWK_SUCCESS) {
68        DHLOGE("Write HiSysEvent error, res:%{public}d", res);
69    }
70}
71
72void HiSysEventWriteCompOfflineMsg(const std::string &status, const OHOS::HiviewDFX::HiSysEvent::EventType eventType,
73    const std::string &anonyNetworkId, const std::string &msg)
74{
75    int32_t res = HiSysEventWrite(
76        OHOS::HiviewDFX::HiSysEvent::Domain::DISTRIBUTED_HARDWARE_FWK,
77        status.c_str(),
78        eventType,
79        "NETWORKID", anonyNetworkId.c_str(),
80        "MSG", msg.c_str());
81    if (res != DH_FWK_SUCCESS) {
82        DHLOGE("Write HiSysEvent error, res:%{public}d", res);
83    }
84}
85
86void HiSysEventWriteCompMgrFailedMsg(const std::string &status, const OHOS::HiviewDFX::HiSysEvent::EventType eventType,
87    const std::string &anonyDHId, int32_t errCode, const std::string &msg)
88{
89    int32_t res = HiSysEventWrite(
90        OHOS::HiviewDFX::HiSysEvent::Domain::DISTRIBUTED_HARDWARE_FWK,
91        status.c_str(),
92        eventType,
93        "DHID", anonyDHId.c_str(),
94        "ERR_CODE", errCode,
95        "ERR_MSG", msg.c_str());
96    if (res != DH_FWK_SUCCESS) {
97        DHLOGE("Write HiSysEvent error, res:%{public}d", res);
98    }
99}
100} // namespace DistributedHardware
101} // namespace OHOS