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 #![allow(missing_docs)]
17
18 use hdc::config::*;
19 use std::ffi::CString;
20
21 extern "C" {
SetParameterEx(key: *const libc::c_char, val: *const libc::c_char) -> libc::c_int22 fn SetParameterEx(key: *const libc::c_char, val: *const libc::c_char) -> libc::c_int;
GetParameterEx( key: *const libc::c_char, def: *const libc::c_char, val: *mut libc::c_char, len: libc::c_uint, ) -> libc::c_int23 fn GetParameterEx(
24 key: *const libc::c_char,
25 def: *const libc::c_char,
26 val: *mut libc::c_char,
27 len: libc::c_uint,
28 ) -> libc::c_int;
29 #[allow(dead_code)]
WaitParameterEx( key: *const libc::c_char, val: *const libc::c_char, timeout: libc::c_int, ) -> libc::c_int30 fn WaitParameterEx(
31 key: *const libc::c_char,
32 val: *const libc::c_char,
33 timeout: libc::c_int,
34 ) -> libc::c_int;
35 }
36
set_dev_item(key: &str, val: &str) -> bool37 pub fn set_dev_item(key: &str, val: &str) -> bool {
38 let ckey = CString::new(key).unwrap();
39 let cval = CString::new(val).unwrap();
40
41 unsafe {
42 let ret = SetParameterEx(ckey.as_ptr(), cval.as_ptr());
43 println!("set param:{} val:{} ret:{}", key, val, ret);
44 ret == 0
45 }
46 }
47
get_dev_item(key: &str, def: &str) -> (bool, String)48 pub fn get_dev_item(key: &str, def: &str) -> (bool, String) {
49 let ckey = CString::new(key).unwrap();
50 let cdef = CString::new(def).unwrap();
51 let mut out: [u8; HDC_PARAMETER_VALUE_MAX_LEN] = [0; HDC_PARAMETER_VALUE_MAX_LEN];
52
53 unsafe {
54 let bytes = GetParameterEx(
55 ckey.as_ptr(),
56 cdef.as_ptr(),
57 out.as_mut_ptr() as *mut u8,
58 512,
59 );
60 let output = String::from_utf8(out.to_vec()).unwrap().trim().to_string();
61 let (val, _) = output.split_at(bytes as usize);
62 println!("get param:{} bytes:{} val:{}", key, bytes, val);
63 (bytes >= 0, val.to_string())
64 }
65 }
66
67 #[allow(dead_code)]
wait_dev_item(key: &str, val: &str, timeout: i32) -> bool68 pub fn wait_dev_item(key: &str, val: &str, timeout: i32) -> bool {
69 let ckey = CString::new(key).unwrap();
70 let cval = CString::new(val).unwrap();
71
72 unsafe { WaitParameterEx(ckey.as_ptr(), cval.as_ptr(), timeout) == 0 }
73 }
74