1 /*
2  * Copyright (c) 2023 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 "hisysevent_adapter.h"
17 #include "common_defs.h"
18 
19 #include "hisysevent.h"
20 
21 #define STR_CALL_EVENT "CALL_EVENT"
22 #define STR_FAULT_EVENT "FAULT_EVENT"
23 #define STR_STATISTIC_EVENT "STATISTIC_EVENT"
24 #define STR_OS_ACCOUNT_ID "OS_ACCOUNT_ID"
25 #define STR_FUNC_NAME "FUNC_NAME"
26 #define STR_FAULT_REASON "FAULT_REASON"
27 #define STR_CRED_TYPE "CRED_TYPE"
28 #define STR_GROUP_TYPE "GROUP_TYPE"
29 #define STR_COST_TIME "COST_TIME"
30 #define STR_PROTOCOL_TYPE "PROTOCOL_TYPE"
31 #define STR_APP_ID "APP_ID"
32 #define STR_REQ_ID "REQ_ID"
33 #define STR_CALL_RESULT "CALL_RESULT"
34 #define STR_UNKNOWN "unknown"
35 #define STR_PROCESS_CODE "PROCESS_CODE"
36 #define STR_EXECUTION_TIME "EXECUTION_TIME"
37 #define STR_EXT_INFO "EXT_INFO"
38 #define STR_FAULT_INFO "FAULT_INFO"
39 #define STR_ERROR_CODE "ERROR_CODE"
40 #define STR_PNAME_ID "PNAMEID"
41 #define STR_PVERSION_ID "PVERSIONID"
42 
DevAuthReportCallEvent(const DevAuthCallEvent eventData)43 void DevAuthReportCallEvent(const DevAuthCallEvent eventData)
44 {
45     HiSysEventWrite(
46         OHOS::HiviewDFX::HiSysEvent::Domain::DEVICE_AUTH,
47         STR_CALL_EVENT, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
48         STR_FUNC_NAME, ((eventData.funcName != NULL) ? eventData.funcName : STR_UNKNOWN),
49         STR_APP_ID, ((eventData.appId != NULL) ? eventData.appId : STR_UNKNOWN),
50         STR_OS_ACCOUNT_ID, eventData.osAccountId,
51         STR_CALL_RESULT, eventData.callResult,
52         STR_PROCESS_CODE, eventData.processCode,
53         STR_CRED_TYPE, eventData.credType,
54         STR_GROUP_TYPE, eventData.groupType,
55         STR_EXECUTION_TIME, eventData.executionTime,
56         STR_EXT_INFO, ((eventData.extInfo != NULL) ? eventData.extInfo : STR_UNKNOWN));
57 }
58 
DevAuthReportFaultEvent(DevAuthFaultEvent eventData)59 void DevAuthReportFaultEvent(DevAuthFaultEvent eventData)
60 {
61     HiSysEventWrite(
62         OHOS::HiviewDFX::HiSysEvent::Domain::DEVICE_AUTH,
63         STR_FAULT_EVENT, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
64         STR_APP_ID, ((eventData.appId != NULL) ? eventData.appId : STR_UNKNOWN),
65         STR_PROCESS_CODE, eventData.processCode,
66         STR_FUNC_NAME, ((eventData.funcName != NULL) ? eventData.funcName : STR_UNKNOWN),
67         STR_REQ_ID, eventData.reqId,
68         STR_ERROR_CODE, eventData.errorCode,
69         STR_FAULT_INFO, ((eventData.faultInfo != NULL) ? eventData.faultInfo : STR_UNKNOWN));
70 }
71 
DevAuthReportFaultEventWithErrCode(const char * funcName,const int32_t processCode,const int32_t errorCode)72 void DevAuthReportFaultEventWithErrCode(const char *funcName, const int32_t processCode, const int32_t errorCode)
73 {
74     DevAuthFaultEvent eventData;
75     eventData.appId = DEFAULT_APPID;
76     eventData.processCode = processCode;
77     eventData.funcName = funcName;
78     eventData.reqId = DEFAULT_REQ_ID;
79     eventData.errorCode = errorCode;
80     eventData.faultInfo = DEFAULT_FAULT_INFO;
81     DevAuthReportFaultEvent(eventData);
82 }
83 
DevAuthReportUeCallEvent(int32_t osAccountId,int32_t groupType,const char * appId,const char * funcName)84 void DevAuthReportUeCallEvent(int32_t osAccountId, int32_t groupType, const char *appId, const char *funcName)
85 {
86     HiSysEventWrite(
87         OHOS::HiviewDFX::HiSysEvent::Domain::DEVICE_AUTH_UE,
88         STR_CALL_EVENT, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
89         STR_APP_ID, ((appId != NULL) ? appId : STR_UNKNOWN),
90         STR_FUNC_NAME, ((funcName != NULL) ? funcName : STR_UNKNOWN),
91         STR_OS_ACCOUNT_ID, osAccountId,
92         STR_GROUP_TYPE, groupType,
93         STR_PNAME_ID, DEFAULT_PNAMEID,
94         STR_PVERSION_ID, DEFAULT_PVERSIONID);
95 }
96 
DevAuthReportUeCallEventByParams(int32_t osAccountId,const char * inParams,const char * appId,const char * funcName)97 void DevAuthReportUeCallEventByParams(int32_t osAccountId, const char *inParams, const char *appId,
98     const char *funcName)
99 {
100     int32_t groupType = GROUP_TYPE_INVALID;
101     CJson *params = CreateJsonFromString(inParams);
102     if (params != NULL) {
103         (void)GetIntFromJson(params, FIELD_GROUP_TYPE, &groupType);
104         FreeJson(params);
105     }
106     DEV_AUTH_REPORT_UE_CALL_EVENT(osAccountId, groupType, appId, funcName);
107 }