11401458bSopenharmony_ci/*
21401458bSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
31401458bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41401458bSopenharmony_ci * you may not use this file except in compliance with the License.
51401458bSopenharmony_ci * You may obtain a copy of the License at
61401458bSopenharmony_ci *
71401458bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81401458bSopenharmony_ci *
91401458bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101401458bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111401458bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121401458bSopenharmony_ci * See the License for the specific language governing permissions and
131401458bSopenharmony_ci * limitations under the License.
141401458bSopenharmony_ci */
151401458bSopenharmony_ci
161401458bSopenharmony_ci//! Rust interfaces for crate hisysevent.
171401458bSopenharmony_ci
181401458bSopenharmony_cimod sys_event_manager;
191401458bSopenharmony_cimod sys_event;
201401458bSopenharmony_cimod utils;
211401458bSopenharmony_ci
221401458bSopenharmony_ci#[macro_use]
231401458bSopenharmony_cipub mod macros;
241401458bSopenharmony_ci
251401458bSopenharmony_cipub use sys_event_manager::{HiSysEventRecord, Querier, Watcher};
261401458bSopenharmony_ci
271401458bSopenharmony_cipub use sys_event::{HiSysEventParam, HiSysEventParamType, HiSysEventParamValue, parse_type_len,
281401458bSopenharmony_ci    build_string_arrays};
291401458bSopenharmony_ci
301401458bSopenharmony_ci/// Enumerate system event types.
311401458bSopenharmony_ci#[non_exhaustive]
321401458bSopenharmony_ci#[derive(Copy, Clone)]
331401458bSopenharmony_cipub enum EventType {
341401458bSopenharmony_ci    /// Fault event.
351401458bSopenharmony_ci    Fault = 1,
361401458bSopenharmony_ci
371401458bSopenharmony_ci    /// Statistic event.
381401458bSopenharmony_ci    Statistic = 2,
391401458bSopenharmony_ci
401401458bSopenharmony_ci    /// Security event.
411401458bSopenharmony_ci    Security = 3,
421401458bSopenharmony_ci
431401458bSopenharmony_ci    /// System behavior event.
441401458bSopenharmony_ci    Behavior = 4,
451401458bSopenharmony_ci}
461401458bSopenharmony_ci
471401458bSopenharmony_ci/// Write system event.
481401458bSopenharmony_cipub fn write(event_domain: &str, event_name: &str, event_type: EventType,
491401458bSopenharmony_ci    event_params: &[HiSysEventParam]) -> i32 {
501401458bSopenharmony_ci    sys_event::write(event_domain, event_name, event_type as std::ffi::c_int, event_params)
511401458bSopenharmony_ci}
521401458bSopenharmony_ci
531401458bSopenharmony_ci/// Enumerate search system event rule type.
541401458bSopenharmony_ci#[non_exhaustive]
551401458bSopenharmony_ci#[derive(Copy, Clone)]
561401458bSopenharmony_cipub enum RuleType {
571401458bSopenharmony_ci    /// Whole word match.
581401458bSopenharmony_ci    WholeWord = 1,
591401458bSopenharmony_ci
601401458bSopenharmony_ci    /// Prefix match.
611401458bSopenharmony_ci    Prefix = 2,
621401458bSopenharmony_ci
631401458bSopenharmony_ci    /// Regular match.
641401458bSopenharmony_ci    Regular = 3,
651401458bSopenharmony_ci}
661401458bSopenharmony_ci
671401458bSopenharmony_ci/// Definition arguments for query system event information.
681401458bSopenharmony_ci#[derive(Copy, Clone)]
691401458bSopenharmony_cipub struct QueryArg {
701401458bSopenharmony_ci    /// Begin time.
711401458bSopenharmony_ci    pub begin_time: i64,
721401458bSopenharmony_ci
731401458bSopenharmony_ci    /// End time.
741401458bSopenharmony_ci    pub end_time: i64,
751401458bSopenharmony_ci
761401458bSopenharmony_ci    /// Max number of receive system event.
771401458bSopenharmony_ci    pub max_events: i32,
781401458bSopenharmony_ci}
791401458bSopenharmony_ci
801401458bSopenharmony_ci/// Definition event for query system event information.
811401458bSopenharmony_cipub struct QueryRule<'a> {
821401458bSopenharmony_ci    /// The domain of the event.
831401458bSopenharmony_ci    pub domain: &'a str,
841401458bSopenharmony_ci
851401458bSopenharmony_ci    /// List of event name.
861401458bSopenharmony_ci    pub event_list: Vec<&'a str>,
871401458bSopenharmony_ci
881401458bSopenharmony_ci    /// extra condition for event query.
891401458bSopenharmony_ci    pub condition: &'a str,
901401458bSopenharmony_ci}
911401458bSopenharmony_ci
921401458bSopenharmony_ci/// Query system event.
931401458bSopenharmony_cipub fn query(query_arg: &QueryArg, query_rules: &[QueryRule], querier: &Querier) -> i32 {
941401458bSopenharmony_ci    sys_event_manager::query(query_arg, query_rules, querier)
951401458bSopenharmony_ci}
961401458bSopenharmony_ci
971401458bSopenharmony_ci/// Definition listener rule for system event information.
981401458bSopenharmony_ci#[derive(Copy, Clone)]
991401458bSopenharmony_cipub struct WatchRule<'a> {
1001401458bSopenharmony_ci    /// The domain of the event.
1011401458bSopenharmony_ci    pub domain: &'a str,
1021401458bSopenharmony_ci
1031401458bSopenharmony_ci    /// The name of the event.
1041401458bSopenharmony_ci    pub name: &'a str,
1051401458bSopenharmony_ci
1061401458bSopenharmony_ci    /// The tag of the event.
1071401458bSopenharmony_ci    pub tag: &'a str,
1081401458bSopenharmony_ci
1091401458bSopenharmony_ci    /// The rule of match system event.
1101401458bSopenharmony_ci    pub rule_type: RuleType,
1111401458bSopenharmony_ci
1121401458bSopenharmony_ci    /// The type of match system event.
1131401458bSopenharmony_ci    pub event_type: EventType,
1141401458bSopenharmony_ci}
1151401458bSopenharmony_ci
1161401458bSopenharmony_ci/// Add watcher to watch system event.
1171401458bSopenharmony_cipub fn add_watcher(watcher: &Watcher, watch_rules: &[WatchRule]) -> i32 {
1181401458bSopenharmony_ci    sys_event_manager::add_watcher(watcher, watch_rules)
1191401458bSopenharmony_ci}
1201401458bSopenharmony_ci
1211401458bSopenharmony_ci/// Remove watcher.
1221401458bSopenharmony_cipub fn remove_watcher(watcher: &Watcher) -> i32 {
1231401458bSopenharmony_ci    sys_event_manager::remove_watcher(watcher)
1241401458bSopenharmony_ci}
125