blob: 43a4827de12c875afa925347fd3b86657b29bf3b [file] [log] [blame]
Paolo Bonzinicde3c422024-10-18 16:30:56 +02001// Copyright 2024, Linaro Limited
2// Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
3// SPDX-License-Identifier: GPL-2.0-or-later
4
Paolo Bonzini9f7d4522024-10-24 13:53:59 +02005use std::{ffi::CStr, os::raw::c_void};
Paolo Bonzinicde3c422024-10-18 16:30:56 +02006
7use qemu_api::{
8 bindings::*,
Paolo Bonzini718e2552024-10-25 08:23:53 +02009 c_str, declare_properties, define_property,
Paolo Bonzinicde3c422024-10-18 16:30:56 +020010 definitions::{Class, ObjectImpl},
Paolo Bonzinice4a1442024-10-25 09:20:16 +020011 device_class, device_class_init,
Manos Pitsidianakis0a65e412024-10-25 07:55:50 +020012 zeroable::Zeroable,
Paolo Bonzinicde3c422024-10-18 16:30:56 +020013};
14
15#[test]
16fn test_device_decl_macros() {
17 // Test that macros can compile.
Manos Pitsidianakis0a65e412024-10-25 07:55:50 +020018 pub static VMSTATE: VMStateDescription = VMStateDescription {
Paolo Bonzini718e2552024-10-25 08:23:53 +020019 name: c_str!("name").as_ptr(),
Paolo Bonzinicde3c422024-10-18 16:30:56 +020020 unmigratable: true,
Manos Pitsidianakis0a65e412024-10-25 07:55:50 +020021 ..Zeroable::ZERO
22 };
Paolo Bonzinicde3c422024-10-18 16:30:56 +020023
Junjie Maof3518402024-10-24 12:25:15 +020024 #[derive(qemu_api_macros::offsets)]
Paolo Bonzinicde3c422024-10-18 16:30:56 +020025 #[repr(C)]
26 #[derive(qemu_api_macros::Object)]
27 pub struct DummyState {
28 pub _parent: DeviceState,
29 pub migrate_clock: bool,
30 }
31
32 #[repr(C)]
33 pub struct DummyClass {
34 pub _parent: DeviceClass,
35 }
36
37 declare_properties! {
38 DUMMY_PROPERTIES,
39 define_property!(
Paolo Bonzini718e2552024-10-25 08:23:53 +020040 c_str!("migrate-clk"),
Paolo Bonzinicde3c422024-10-18 16:30:56 +020041 DummyState,
42 migrate_clock,
43 unsafe { &qdev_prop_bool },
44 bool
45 ),
46 }
47
48 device_class_init! {
49 dummy_class_init,
50 props => DUMMY_PROPERTIES,
51 realize_fn => None,
52 legacy_reset_fn => None,
53 vmsd => VMSTATE,
54 }
55
56 impl ObjectImpl for DummyState {
57 type Class = DummyClass;
58 const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self };
Paolo Bonzini718e2552024-10-25 08:23:53 +020059 const TYPE_NAME: &'static CStr = c_str!("dummy");
Paolo Bonzinice4a1442024-10-25 09:20:16 +020060 const PARENT_TYPE_NAME: Option<&'static CStr> = Some(device_class::TYPE_DEVICE);
Paolo Bonzinicde3c422024-10-18 16:30:56 +020061 const ABSTRACT: bool = false;
62 const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
63 const INSTANCE_POST_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
64 const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
65 }
66
67 impl Class for DummyClass {
Paolo Bonzini9f7d4522024-10-24 13:53:59 +020068 const CLASS_INIT: Option<unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void)> =
69 Some(dummy_class_init);
Paolo Bonzinicde3c422024-10-18 16:30:56 +020070 const CLASS_BASE_INIT: Option<
Paolo Bonzini9f7d4522024-10-24 13:53:59 +020071 unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void),
Paolo Bonzinicde3c422024-10-18 16:30:56 +020072 > = 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}