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 crate::config::*;
19 use std::ffi::CString;
20 
21 extern "C" {
SetParameterExnull22     fn SetParameterEx(key: *const libc::c_char, val: *const libc::c_char) -> libc::c_int;
GetParameterExnull23     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)]
WaitParameterExnull30     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_itemnull37 pub fn set_dev_item(key: &str, val: &str) -> bool {
38     let ckey = match CString::new(key) {
39         Ok(v) => v,
40         Err(_) => return false,
41     };
42     let cval = match CString::new(val) {
43         Ok(v) => v,
44         Err(_) => return false,
45     };
46 
47     unsafe {
48         let ret = SetParameterEx(ckey.as_ptr(), cval.as_ptr());
49         ret == 0
50     }
51 }
52 
get_dev_itemnull53 pub fn get_dev_item(key: &str, def: &str) -> (bool, String) {
54     let ckey = match CString::new(key) {
55         Ok(v) => v,
56         Err(_) => return (false, String::new()),
57     };
58     let cdef = match CString::new(def) {
59         Ok(v) => v,
60         Err(_) => return (false, String::new()),
61     };
62     let mut out: [u8; HDC_PARAMETER_VALUE_MAX_LEN] = [0; HDC_PARAMETER_VALUE_MAX_LEN];
63 
64     unsafe {
65         let bytes = GetParameterEx(
66             ckey.as_ptr(),
67             cdef.as_ptr(),
68             out.as_mut_ptr() as *mut libc::c_char,
69             512,
70         );
71         let output = match String::from_utf8(out.to_vec()) {
72             Ok(v) => v.trim().to_string(),
73             Err(_) => return (false, String::new()),
74         };
75         let (val, _) = output.split_at(bytes as usize);
76         (bytes >= 0, val.to_string())
77     }
78 }
79 
80 #[allow(dead_code)]
wait_dev_itemnull81 pub fn wait_dev_item(key: &str, val: &str, timeout: i32) -> bool {
82     let ckey = match CString::new(key) {
83         Ok(v) => v,
84         Err(_) => return false,
85     };
86     let cval = match CString::new(val) {
87         Ok(v) => v,
88         Err(_) => return false,
89     };
90     unsafe { WaitParameterEx(ckey.as_ptr(), cval.as_ptr(), timeout) == 0 }
91 }
92