xref: /openbmc/qemu/rust/qemu-api/tests/tests.rs (revision 646b5378)
1 // Copyright 2024, Linaro Limited
2 // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
3 // SPDX-License-Identifier: GPL-2.0-or-later
4 
5 use core::ffi::CStr;
6 
7 use qemu_api::{
8     bindings::*,
9     declare_properties, define_property,
10     definitions::{Class, ObjectImpl},
11     device_class_init,
12     zeroable::Zeroable,
13 };
14 
15 #[test]
16 fn test_device_decl_macros() {
17     // Test that macros can compile.
18     pub static VMSTATE: VMStateDescription = VMStateDescription {
19         name: c"name".as_ptr(),
20         unmigratable: true,
21         ..Zeroable::ZERO
22     };
23 
24     #[repr(C)]
25     #[derive(qemu_api_macros::Object)]
26     pub struct DummyState {
27         pub _parent: DeviceState,
28         pub migrate_clock: bool,
29     }
30 
31     #[repr(C)]
32     pub struct DummyClass {
33         pub _parent: DeviceClass,
34     }
35 
36     declare_properties! {
37         DUMMY_PROPERTIES,
38             define_property!(
39                 c"migrate-clk",
40                 DummyState,
41                 migrate_clock,
42                 unsafe { &qdev_prop_bool },
43                 bool
44             ),
45     }
46 
47     device_class_init! {
48         dummy_class_init,
49         props => DUMMY_PROPERTIES,
50         realize_fn => None,
51         legacy_reset_fn => None,
52         vmsd => VMSTATE,
53     }
54 
55     impl ObjectImpl for DummyState {
56         type Class = DummyClass;
57         const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self };
58         const TYPE_NAME: &'static CStr = c"dummy";
59         const PARENT_TYPE_NAME: Option<&'static CStr> = Some(TYPE_DEVICE);
60         const ABSTRACT: bool = false;
61         const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
62         const INSTANCE_POST_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
63         const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
64     }
65 
66     impl Class for DummyClass {
67         const CLASS_INIT: Option<
68             unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut core::ffi::c_void),
69         > = Some(dummy_class_init);
70         const CLASS_BASE_INIT: Option<
71             unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut core::ffi::c_void),
72         > = None;
73     }
74 
75     unsafe {
76         module_call_init(module_init_type::MODULE_INIT_QOM);
77         object_unref(object_new(DummyState::TYPE_NAME.as_ptr()) as *mut _);
78     }
79 }
80