rkaiq 5.0.6

Rockchip RKAIQ bindings for Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! 系统控制。
//!
//! 系统控制部分包含了 AIQ 公共属性配置,初始化 AIQ、运行 AIQ、退出AIQ,设置 AIQ 各模块等功能。
use super::context::Context;
use super::error::XCamError;
use super::ffi;
use super::types::{
    AlgoContext, AlgoDescComm, CpslCap, CpslCfg, CpslInfo, ModuleId, Rect, StaticInfo, WorkingMode,
    XCamResult,
};
use std::ffi::{CStr, CString};

/// 一个描述静态信息枚举器的类型。
pub struct StaticMetas {
    index: i32,
}

impl StaticMetas {
    pub fn new() -> Self {
        Self { index: 0 }
    }
}

impl Default for StaticMetas {
    fn default() -> Self {
        Self::new()
    }
}

impl Iterator for StaticMetas {
    type Item = ffi::rk_aiq_static_info_t;

    fn next(&mut self) -> Option<Self::Item> {
        enum_static_metas(self.index)
            .map(|x| {
                self.index += 1;
                x
            })
            .ok()
    }
}

pub trait SystemControl {
    fn prepare(&self, width: u32, height: u32, mode: WorkingMode) -> XCamResult<()>;

    fn start(&self) -> XCamResult<()>;

    fn stop(&self, keep_ext_hw_st: bool) -> XCamResult<()>;

    fn enable_module<T: Into<ModuleId>>(&self, id: T) -> XCamResult<()>;

    fn disable_module<T: Into<ModuleId>>(&self, id: T) -> XCamResult<()>;

    fn is_module_enabled<T: Into<ModuleId>>(&self, id: T) -> bool;

    fn register_lib(&self, algo_lib_des: AlgoDescComm) -> XCamResult<()>;

    fn unregister_lib(&self, algo_type: i32, lib_id: i32) -> XCamResult<()>;

    fn enable_ax_lib(&self, algo_type: i32, lib_id: i32) -> XCamResult<()>;

    fn disable_ax_lib(&self, algo_type: i32, lib_id: i32) -> XCamResult<()>;

    fn is_ax_lib_enabled(&self, algo_type: i32, lib_id: i32) -> bool;

    /// 获取使能算法库的上下文结构体。
    ///
    /// # Safety
    /// 请确保返回值仅在 Context 生存期间使用。
    unsafe fn get_enabled_ax_lib_ctx(&self, algo_type: i32) -> *const AlgoContext;

    /// 获取补光灯控制信息。
    fn get_cps_lt_info(&self) -> XCamResult<CpslInfo>;

    /// 查询补光灯的支持能力。
    fn query_cps_lt_cap(&self) -> XCamResult<CpslCap>;

    /// 设置补光灯控制信息。
    fn set_cps_lt_cfg<T: Into<CpslCfg>>(&self, cfg: T) -> XCamResult<()>;

    fn update_iq<T: Into<Vec<u8>>>(&self, iq_file: T) -> XCamResult<()>;

    fn get_crop(&self) -> XCamResult<Rect>;

    fn set_crop(&self, crop: Rect) -> XCamResult<()>;
}

impl SystemControl for Context {
    fn prepare(&self, width: u32, height: u32, mode: WorkingMode) -> XCamResult<()> {
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi2_sysctl_prepare(
                self.internal.as_ptr(),
                width,
                height,
                mode.into(),
            ))
            .ok()
        }
    }

    fn start(&self) -> XCamResult<()> {
        unsafe { XCamError::from(ffi::rk_aiq_uapi2_sysctl_start(self.internal.as_ptr())).ok() }
    }

    fn stop(&self, keep_ext_hw_st: bool) -> XCamResult<()> {
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi2_sysctl_stop(
                self.internal.as_ptr(),
                keep_ext_hw_st,
            ))
            .ok()
        }
    }

    fn enable_module<T: Into<ModuleId>>(&self, id: T) -> XCamResult<()> {
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi2_sysctl_setModuleCtl(
                self.internal.as_ptr(),
                id.into(),
                true,
            ))
            .ok()
        }
    }

    fn disable_module<T: Into<ModuleId>>(&self, id: T) -> XCamResult<()> {
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi2_sysctl_setModuleCtl(
                self.internal.as_ptr(),
                id.into(),
                false,
            ))
            .ok()
        }
    }

    fn is_module_enabled<T: Into<ModuleId>>(&self, id: T) -> bool {
        let mut enabled = false;
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi2_sysctl_getModuleCtl(
                self.internal.as_ptr(),
                id.into(),
                &mut enabled,
            ))
            .ok()
            .map_or(false, |_| enabled)
        }
    }

    fn register_lib(&self, mut algo_lib_des: AlgoDescComm) -> XCamResult<()> {
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi_sysctl_regLib(
                self.internal.as_ptr(),
                &mut algo_lib_des,
            ))
            .ok()
        }
    }

    fn unregister_lib(&self, algo_type: i32, lib_id: i32) -> XCamResult<()> {
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi_sysctl_unRegLib(
                self.internal.as_ptr(),
                algo_type,
                lib_id,
            ))
            .ok()
        }
    }

    fn enable_ax_lib(&self, algo_type: i32, lib_id: i32) -> XCamResult<()> {
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi2_sysctl_enableAxlib(
                self.internal.as_ptr(),
                algo_type,
                lib_id,
                true,
            ))
            .ok()
        }
    }

    fn disable_ax_lib(&self, algo_type: i32, lib_id: i32) -> XCamResult<()> {
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi2_sysctl_enableAxlib(
                self.internal.as_ptr(),
                algo_type,
                lib_id,
                false,
            ))
            .ok()
        }
    }

    fn is_ax_lib_enabled(&self, algo_type: i32, lib_id: i32) -> bool {
        unsafe { ffi::rk_aiq_uapi_sysctl_getAxlibStatus(self.internal.as_ptr(), algo_type, lib_id) }
    }

    unsafe fn get_enabled_ax_lib_ctx(&self, algo_type: i32) -> *const AlgoContext {
        ffi::rk_aiq_uapi2_sysctl_getEnabledAxlibCtx(self.internal.as_ptr(), algo_type)
    }

    fn get_cps_lt_info(&self) -> XCamResult<CpslInfo> {
        let mut info = CpslInfo::default();
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi2_sysctl_getCpsLtInfo(
                self.internal.as_ptr(),
                &mut info,
            ))
            .ok()
            .map(|_| info)
        }
    }

    fn query_cps_lt_cap(&self) -> XCamResult<CpslCap> {
        let mut cap = CpslCap::default();
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi2_sysctl_queryCpsLtCap(
                self.internal.as_ptr(),
                &mut cap,
            ))
            .ok()
            .map(|_| cap)
        }
    }

    fn set_cps_lt_cfg<T: Into<CpslCfg>>(&self, cfg: T) -> XCamResult<()> {
        let mut cfg = cfg.into();
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi2_sysctl_setCpsLtCfg(
                self.internal.as_ptr(),
                &mut cfg,
            ))
            .ok()
        }
    }

    fn update_iq<T: Into<Vec<u8>>>(&self, iq_file: T) -> XCamResult<()> {
        let iq_file = CString::new(iq_file).unwrap();
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi2_sysctl_updateIq(
                self.internal.as_ptr(),
                iq_file.as_ptr() as *mut _,
            ))
            .ok()
        }
    }

    fn get_crop(&self) -> XCamResult<Rect> {
        let mut crop = Rect::default();
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi2_sysctl_getCrop(
                self.internal.as_ptr(),
                &mut crop,
            ))
            .ok()
            .map(|_| crop)
        }
    }

    fn set_crop(&self, crop: Rect) -> XCamResult<()> {
        unsafe {
            XCamError::from(ffi::rk_aiq_uapi_sysctl_setCrop(
                self.internal.as_ptr(),
                crop,
            ))
            .ok()
        }
    }
}

/// 查询 video 结点所对应的 sensor entity name。
///
/// # Note
///
/// 参数必须为 ISPP scale 结点路径。
pub fn get_binded_sensor_entity_name<T: Into<Vec<u8>>>(vd: T) -> Option<String> {
    let vd = CString::new(vd).expect("CString::new failed");
    unsafe {
        let ptr = ffi::rk_aiq_uapi2_sysctl_getBindedSnsEntNmByVd(vd.as_ptr());
        if ptr.is_null() {
            CStr::from_ptr(ptr).to_str().map(ToString::to_string).ok()
        } else {
            None
        }
    }
}

pub fn get_static_metas<T: Into<Vec<u8>>>(vd: T) -> XCamResult<StaticInfo> {
    let vd = CString::new(vd).expect("CString::new failed");
    unsafe {
        let mut data = ffi::rk_aiq_static_info_t::default();
        XCamError::from(ffi::rk_aiq_uapi2_sysctl_getStaticMetas(
            vd.as_ptr(),
            &mut data,
        ))
        .ok()
        .map(|_| data)
    }
}

/// 枚举 AIQ 获取到的静态信息。
///
/// # Parameters
/// * `index` - 索引号,从 0 开始。
pub fn enum_static_metas(index: i32) -> XCamResult<StaticInfo> {
    unsafe {
        let mut data = ffi::rk_aiq_static_info_t::default();
        XCamError::from(ffi::rk_aiq_uapi2_sysctl_enumStaticMetas(index, &mut data))
            .ok()
            .map(|_| data)
    }
}

/// 预先初始化 AIQ 系统配置。
pub fn pre_init(sns_ent_name: &str, mode: WorkingMode, iq_file: &str) -> XCamResult<()> {
    let sns = CString::new(sns_ent_name).expect("CString::new failed");
    let iq = CString::new(iq_file).expect("CString::new failed");
    unsafe {
        XCamError::from(ffi::rk_aiq_uapi2_sysctl_preInit(
            sns.as_ptr(),
            mode.into(),
            iq.as_ptr(),
        ))
        .ok()
    }
}

/// 设置全局日志等级。
#[cfg(feature = "fullv")]
pub fn set_gll(level: i32) {
    unsafe {
        #[cfg(any(feature = "v2_0", feature = "v3_0"))]
        ffi::rk_aiq_uapi_sysctl_set_gll(level);
        #[cfg(any(feature = "v4_0", feature = "v5_0"))]
        ffi::rk_aiq_uapi2_sysctl_set_gll(level);
    }
}

/// 获取全局日志等级。
#[cfg(feature = "fullv")]
pub fn get_gll() -> i32 {
    #[cfg(any(feature = "v2_0", feature = "v3_0"))]
    unsafe {
        ffi::rk_aiq_uapi_sysctl_get_gll()
    }
    #[cfg(any(feature = "v4_0", feature = "v5_0"))]
    unsafe {
        ffi::rk_aiq_uapi2_sysctl_get_gll()
    }
}

/// 初始化 RKAIQ 库。
#[cfg(feature = "fullv")]
pub fn init_lib() {
    unsafe {
        ffi::rk_aiq_init_lib();
    }
}

/// 释放 RKAIQ 库。
#[cfg(feature = "fullv")]
pub fn deinit_lib() {
    unsafe {
        ffi::rk_aiq_deinit_lib();
    }
}

/// 释放 RKAIQ 库。
#[cfg(feature = "fullv")]
pub fn set_log_callback(
    cb: Option<unsafe extern "C" fn(i32, *const std::os::raw::c_char, *const std::os::raw::c_char)>,
) {
    unsafe {
        #[cfg(any(feature = "v2_0", feature = "v3_0"))]
        ffi::rk_aiq_set_log_callback(cb);
        #[cfg(any(feature = "v4_0", feature = "v5_0"))]
        ffi::rk_aiq_uapi2_set_log_callback(cb);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_context() {
        let r = Context::new("m00_b_ov5695 4-0036-1", "/etc/iqfiles");
        assert_eq!(r.is_ok(), true);
        if let Ok(ctx) = r {
            assert_eq!(ctx.prepare(2592, 1944, WorkingMode::Normal), Ok(()));
            assert_eq!(ctx.start(), Ok(()));
            assert_eq!(ctx.stop(false), Ok(()));
        }
    }

    #[test]
    fn test_get_binded_sensor_entity_name() {
        let r = get_binded_sensor_entity_name("/dev/video0");
        assert_eq!(r, None);
    }

    #[test]
    fn test_get_static_metas() {
        let r = get_static_metas("m00_b_ov5695 4-0036-1");
        assert_eq!(r.is_ok(), true);
        println!("{:?}", r.unwrap());
    }
}