1cc290419Sopenharmony_ci/*
2cc290419Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3cc290419Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cc290419Sopenharmony_ci * you may not use this file except in compliance with the License.
5cc290419Sopenharmony_ci * You may obtain a copy of the License at
6cc290419Sopenharmony_ci *
7cc290419Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cc290419Sopenharmony_ci *
9cc290419Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cc290419Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cc290419Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cc290419Sopenharmony_ci * See the License for the specific language governing permissions and
13cc290419Sopenharmony_ci * limitations under the License.
14cc290419Sopenharmony_ci */
15cc290419Sopenharmony_ci
16cc290419Sopenharmony_ci#![allow(missing_docs)]
17cc290419Sopenharmony_ci
18cc290419Sopenharmony_ciuse crate::config::*;
19cc290419Sopenharmony_ciuse std::ffi::CString;
20cc290419Sopenharmony_ci
21cc290419Sopenharmony_ciextern "C" {
22cc290419Sopenharmony_ci    fn SetParameterEx(key: *const libc::c_char, val: *const libc::c_char) -> libc::c_int;
23cc290419Sopenharmony_ci    fn GetParameterEx(
24cc290419Sopenharmony_ci        key: *const libc::c_char,
25cc290419Sopenharmony_ci        def: *const libc::c_char,
26cc290419Sopenharmony_ci        val: *mut libc::c_char,
27cc290419Sopenharmony_ci        len: libc::c_uint,
28cc290419Sopenharmony_ci    ) -> libc::c_int;
29cc290419Sopenharmony_ci    #[allow(dead_code)]
30cc290419Sopenharmony_ci    fn WaitParameterEx(
31cc290419Sopenharmony_ci        key: *const libc::c_char,
32cc290419Sopenharmony_ci        val: *const libc::c_char,
33cc290419Sopenharmony_ci        timeout: libc::c_int,
34cc290419Sopenharmony_ci    ) -> libc::c_int;
35cc290419Sopenharmony_ci}
36cc290419Sopenharmony_ci
37cc290419Sopenharmony_cipub fn set_dev_item(key: &str, val: &str) -> bool {
38cc290419Sopenharmony_ci    let ckey = match CString::new(key) {
39cc290419Sopenharmony_ci        Ok(v) => v,
40cc290419Sopenharmony_ci        Err(_) => return false,
41cc290419Sopenharmony_ci    };
42cc290419Sopenharmony_ci    let cval = match CString::new(val) {
43cc290419Sopenharmony_ci        Ok(v) => v,
44cc290419Sopenharmony_ci        Err(_) => return false,
45cc290419Sopenharmony_ci    };
46cc290419Sopenharmony_ci
47cc290419Sopenharmony_ci    unsafe {
48cc290419Sopenharmony_ci        let ret = SetParameterEx(ckey.as_ptr(), cval.as_ptr());
49cc290419Sopenharmony_ci        ret == 0
50cc290419Sopenharmony_ci    }
51cc290419Sopenharmony_ci}
52cc290419Sopenharmony_ci
53cc290419Sopenharmony_cipub fn get_dev_item(key: &str, def: &str) -> (bool, String) {
54cc290419Sopenharmony_ci    let ckey = match CString::new(key) {
55cc290419Sopenharmony_ci        Ok(v) => v,
56cc290419Sopenharmony_ci        Err(_) => return (false, String::new()),
57cc290419Sopenharmony_ci    };
58cc290419Sopenharmony_ci    let cdef = match CString::new(def) {
59cc290419Sopenharmony_ci        Ok(v) => v,
60cc290419Sopenharmony_ci        Err(_) => return (false, String::new()),
61cc290419Sopenharmony_ci    };
62cc290419Sopenharmony_ci    let mut out: [u8; HDC_PARAMETER_VALUE_MAX_LEN] = [0; HDC_PARAMETER_VALUE_MAX_LEN];
63cc290419Sopenharmony_ci
64cc290419Sopenharmony_ci    unsafe {
65cc290419Sopenharmony_ci        let bytes = GetParameterEx(
66cc290419Sopenharmony_ci            ckey.as_ptr(),
67cc290419Sopenharmony_ci            cdef.as_ptr(),
68cc290419Sopenharmony_ci            out.as_mut_ptr() as *mut libc::c_char,
69cc290419Sopenharmony_ci            512,
70cc290419Sopenharmony_ci        );
71cc290419Sopenharmony_ci        let output = match String::from_utf8(out.to_vec()) {
72cc290419Sopenharmony_ci            Ok(v) => v.trim().to_string(),
73cc290419Sopenharmony_ci            Err(_) => return (false, String::new()),
74cc290419Sopenharmony_ci        };
75cc290419Sopenharmony_ci        let (val, _) = output.split_at(bytes as usize);
76cc290419Sopenharmony_ci        (bytes >= 0, val.to_string())
77cc290419Sopenharmony_ci    }
78cc290419Sopenharmony_ci}
79cc290419Sopenharmony_ci
80cc290419Sopenharmony_ci#[allow(dead_code)]
81cc290419Sopenharmony_cipub fn wait_dev_item(key: &str, val: &str, timeout: i32) -> bool {
82cc290419Sopenharmony_ci    let ckey = match CString::new(key) {
83cc290419Sopenharmony_ci        Ok(v) => v,
84cc290419Sopenharmony_ci        Err(_) => return false,
85cc290419Sopenharmony_ci    };
86cc290419Sopenharmony_ci    let cval = match CString::new(val) {
87cc290419Sopenharmony_ci        Ok(v) => v,
88cc290419Sopenharmony_ci        Err(_) => return false,
89cc290419Sopenharmony_ci    };
90cc290419Sopenharmony_ci    unsafe { WaitParameterEx(ckey.as_ptr(), cval.as_ptr(), timeout) == 0 }
91cc290419Sopenharmony_ci}
92