• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //!
17 use hilog_rust::{error, hilog, HiLogLabel, LogType};
18 use std::ffi::{c_char, CString};
19 use std::ffi::CStr;
20 
21 const LOG_LABEL: HiLogLabel = HiLogLabel {
22     log_type: LogType::LogCore,
23     domain: 0xd002800,
24     tag: "MMIRustKey",
25 };
26 
27 static RET_OK: i32 = 0;
28 static RET_ERR: i32 = -1;
29 static KEY_ELEMENT_SIZE: usize = 4;
30 const NPOS: usize = usize::MAX;
31 
32 #[no_mangle]
ReadConfigInfo( info_line: *const c_char, len: i32, element_key: *mut i32, element_value: *mut i32, ) -> i3233 extern "C" fn ReadConfigInfo(
34     info_line: *const c_char,
35     len: i32,
36     element_key: *mut i32,
37     element_value: *mut i32,
38 ) -> i32 {
39     if len <= 0 {
40         error!(LOG_LABEL, "The config info format is error");
41         return RET_ERR;
42     }
43     let mut key: i32 = 0;
44     let mut value: i32 = 0;
45     let c_str: &CStr = unsafe { CStr::from_ptr(info_line) };
46     let line_str: String = c_str.to_str().unwrap().to_owned();
47     let pos = line_str.find('#');
48     if let Some(pos) = pos {
49         if pos != NPOS || pos != 0 {
50             error!(LOG_LABEL, "The comment line format is error");
51             return RET_ERR;
52         }
53     }
54     if !line_str.is_empty() && !line_str.starts_with('#') {
55         let key_element: Vec<&str> = line_str.split(' ').collect();
56         if key_element.len() != KEY_ELEMENT_SIZE {
57             error!(LOG_LABEL, "The key value data is incomplete");
58             return RET_ERR;
59         }
60         if key_element[1].parse::<i32>().is_err() || key_element[2].parse::<i32>().is_err() {
61             error!(LOG_LABEL, "Get key value is invalid");
62             return RET_ERR;
63         }
64         key = key_element[1].parse::<i32>().unwrap();
65         value = key_element[2].parse::<i32>().unwrap();
66     }
67     unsafe {
68         *element_key = key;
69         *element_value = value;
70     }
71     RET_OK
72 }
73 
74 #[test]
test_read_config_info_normal()75 fn test_read_config_info_normal()
76 {
77     let info = String::from("KEY_BTN_0 256 3100 HOS_KEY_BTN_0");
78     let info_line = info.as_ptr() as *const u8;
79     let len: i32 = 33;
80     let mut element_key: i32 = 0;
81     let mut element_value: i32 = 0;
82     let ret: i32 = ReadConfigInfo(info_line, len, &mut element_key as *mut i32, &mut element_value as *mut i32);
83     assert_eq!(ret, RET_OK);
84 }
85 
86 #[test]
test_read_config_info_invalid()87 fn test_read_config_info_invalid()
88 {
89     let info = "#KEY_BTN_0 256 3100 HOS_KEY_BTN_0";
90     let info_line = info.as_ptr() as *const u8;
91     let len: i32 = 34;
92     let mut element_key: i32 = 0;
93     let mut element_value: i32 = 0;
94     let ret: i32 = ReadConfigInfo(info_line, len, &mut element_key as *mut i32, &mut element_value as *mut i32);
95     assert_eq!(ret, RET_ERR);
96 }
97 
98 #[test]
test_read_config_info_len_invalid()99 fn test_read_config_info_len_invalid()
100 {
101     let info = "KEY_BTN_0 256 3100 HOS_KEY_BTN_0";
102     let info_line = info.as_ptr() as *const u8;
103     let len: i32 = 0;
104     let mut element_key: i32 = 0;
105     let mut element_value: i32 = 0;
106     let ret: i32 = ReadConfigInfo(info_line, len, &mut element_key as *mut i32, &mut element_value as *mut i32);
107     assert_eq!(ret, RET_ERR);
108 }