1/*
2 * Copyright (c) 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
16#include "errorcode_convertor.h"
17#include "net_event_report.h"
18#include "net_manager_constants.h"
19#include "netfirewall_hisysevent.h"
20
21namespace OHOS {
22namespace NetManagerStandard {
23namespace {
24constexpr const char *NET_FIREWALL_CONF_FAULT = "NET_FIREWALL_CONF_FAULT";
25constexpr const char *NET_FIREWALL_REQ_FAULT = "NET_FIREWALL_REQ_FAULT";
26constexpr const char *NET_FIREWALL_LOG_REQ_FAULT = "NET_FIREWALL_LOG_REQ_FAULT";
27constexpr const char *NET_FIREWALL_INIT_FAULT = "NET_FIREWALL_INIT_FAULT";
28constexpr const char *NET_FIREWALL_CONF_BEHAVIOR = "NET_FIREWALL_CONF_BEHAVIOR";
29constexpr const char *NET_FIREWALL_REQ_BEHAVIOR = "NET_FIREWALL_REQ_BEHAVIOR";
30constexpr const char *NET_FIREWALL_LOG_REQ_BEHAVIOR = "NET_FIREWALL_LOG_REQ_BEHAVIOR";
31
32constexpr const char *EVENT_KEY_FIREWALL_USER_ID = "userId";
33constexpr const char *EVENT_KEY_FIREWALL_ERROR_TYPE = "errorType";
34constexpr const char *EVENT_KEY_FIREWALL_ERROR_MSG = "errorMsg";
35}
36
37NetFirewallHisysEvent &NetFirewallHisysEvent::GetInstance()
38{
39    static NetFirewallHisysEvent instance;
40    return instance;
41}
42
43void NetFirewallHisysEvent::SendFirewallConfigReport(int32_t userId, int32_t &errorCode)
44{
45    NetFirewallHisysEvent instance = GetInstance();
46    if (errorCode == FIREWALL_SUCCESS) {
47        instance.SendNetFirewallRuleBehavior(userId, NET_FIREWALL_CONF_BEHAVIOR);
48    } else {
49        NetFirewallEvent eventInfo;
50        eventInfo.userId = userId;
51        eventInfo.errorType = errorCode;
52        NetBaseErrorCodeConvertor convertor;
53        eventInfo.errorMsg = convertor.ConvertErrorCode(errorCode);
54        instance.SendNetFirewallRuleFault(eventInfo, NET_FIREWALL_CONF_FAULT);
55    }
56}
57
58void NetFirewallHisysEvent::SendFirewallRequestReport(const int32_t userId, int32_t &errorCode)
59{
60    NetFirewallHisysEvent instance = GetInstance();
61    if (errorCode == FIREWALL_SUCCESS) {
62        instance.SendNetFirewallRuleBehavior(userId, NET_FIREWALL_REQ_BEHAVIOR);
63    } else {
64        NetFirewallEvent eventInfo;
65        eventInfo.userId = userId;
66        eventInfo.errorType = errorCode;
67        NetBaseErrorCodeConvertor convertor;
68        eventInfo.errorMsg = convertor.ConvertErrorCode(errorCode);
69        instance.SendNetFirewallRuleFault(eventInfo, NET_FIREWALL_REQ_FAULT);
70    }
71}
72
73void NetFirewallHisysEvent::SendRecordRequestReport(const int32_t userId, int32_t &errorCode)
74{
75    NetFirewallHisysEvent instance = GetInstance();
76    if (errorCode == FIREWALL_SUCCESS) {
77        instance.SendNetFirewallBehavior(userId, NET_FIREWALL_LOG_REQ_BEHAVIOR);
78    } else {
79        NetFirewallEvent eventInfo;
80        eventInfo.userId = userId;
81        eventInfo.errorType = errorCode;
82        NetBaseErrorCodeConvertor convertor;
83        eventInfo.errorMsg = convertor.ConvertErrorCode(errorCode);
84        instance.SendNetFirewallFault(eventInfo, NET_FIREWALL_LOG_REQ_FAULT);
85    }
86}
87
88void NetFirewallHisysEvent::SendInitDefaultRequestReport(const int32_t userId, int32_t &errorCode)
89{
90    NetFirewallEvent eventInfo;
91    eventInfo.userId = userId;
92    eventInfo.errorType = errorCode;
93    NetBaseErrorCodeConvertor convertor;
94    eventInfo.errorMsg = convertor.ConvertErrorCode(errorCode);
95    GetInstance().SendNetFirewallFault(eventInfo, NET_FIREWALL_INIT_FAULT);
96}
97
98void NetFirewallHisysEvent::SendNetFirewallRuleFault(const NetFirewallEvent &event, const std::string &eventName)
99{
100    HiSysEventWrite(HiSysEvent::Domain::NETMANAGER_STANDARD, eventName, HiSysEvent::EventType::FAULT,
101        EVENT_KEY_FIREWALL_USER_ID, event.userId, EVENT_KEY_FIREWALL_ERROR_TYPE, event.errorType,
102        EVENT_KEY_FIREWALL_ERROR_MSG, event.errorMsg);
103}
104
105void NetFirewallHisysEvent::SendNetFirewallRuleBehavior(const int32_t userId, const std::string &eventName)
106{
107    HiSysEventWrite(HiSysEvent::Domain::NETMANAGER_STANDARD, eventName, HiSysEvent::EventType::BEHAVIOR,
108        EVENT_KEY_FIREWALL_USER_ID, userId);
109}
110
111void NetFirewallHisysEvent::SendNetFirewallFault(const NetFirewallEvent &event, const std::string &eventName)
112{
113    HiSysEventWrite(HiSysEvent::Domain::NETMANAGER_STANDARD, eventName, HiSysEvent::EventType::FAULT,
114        EVENT_KEY_FIREWALL_USER_ID, event.userId, EVENT_KEY_FIREWALL_ERROR_TYPE, event.errorType,
115        EVENT_KEY_FIREWALL_ERROR_MSG, event.errorMsg);
116}
117
118void NetFirewallHisysEvent::SendNetFirewallBehavior(const int32_t userId, const std::string &eventName)
119{
120    HiSysEventWrite(HiSysEvent::Domain::NETMANAGER_STANDARD, eventName, HiSysEvent::EventType::BEHAVIOR,
121        EVENT_KEY_FIREWALL_USER_ID, userId);
122}
123} // namespace NetManagerStandard
124} // namespace OHOS