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 "sys_event.h"
17 
18 #include "init_module_engine.h"
19 #include "hisysevent_c.h"
20 #include "plugin_adapter.h"
21 
22 #define STARTUP_DOMAIN "INIT"
23 
24 #define KEY_TOTAL_TIME  "TOTAL_TIME"
25 #define KEY_DETAILED_TIME "DETAILED_TIME"
26 #define KEY_REASON "REASON"
27 #define KEY_FIRST "ISFIRST"
28 
ReportStartupTimeEvent(const StartupTimeEvent * startupTime)29 static void ReportStartupTimeEvent(const StartupTimeEvent *startupTime)
30 {
31     HiSysEventParam params[] = {
32         {
33             .name = KEY_TOTAL_TIME,
34             .t = HISYSEVENT_INT64,
35             .v = { .i64 = startupTime->totalTime },
36             .arraySize = 0,
37         },
38         {
39             .name = KEY_DETAILED_TIME,
40             .t = HISYSEVENT_STRING,
41             .v = { .s = startupTime->detailTime },
42             .arraySize = 0,
43         },
44         {
45             .name = KEY_REASON,
46             .t = HISYSEVENT_STRING,
47             .v = { .s = startupTime->reason },
48             .arraySize = 0,
49         },
50         {
51             .name = KEY_FIRST,
52             .t = HISYSEVENT_INT32,
53             .v = { .i32 = startupTime->firstStart },
54             .arraySize = 0,
55         }
56     };
57     int ret = OH_HiSysEvent_Write(STARTUP_DOMAIN, "STARTUP_TIME",
58         HISYSEVENT_BEHAVIOR, params, sizeof(params) / sizeof(params[0]));
59     PLUGIN_ONLY_LOG(ret == 0, "Failed to write event ret %d", ret);
60 }
61 
ReportSysEvent(const StartupEvent * event)62 void ReportSysEvent(const StartupEvent *event)
63 {
64     PLUGIN_CHECK(event != NULL, return, "Invalid events");
65     if (event->type == STARTUP_TIME) {
66         StartupTimeEvent *startupTime = (StartupTimeEvent *)(event);
67         ReportStartupTimeEvent(startupTime);
68     }
69 }
70