|
| 1 | +// Bindings to the C API. |
| 2 | + |
| 3 | +use std::ffi::CStr; |
| 4 | + |
| 5 | +extern "C" { |
| 6 | + pub fn orb_advertise(meta: *const Metadata, data: *const u8) -> usize; |
| 7 | + pub fn orb_advertise_queue(meta: *const Metadata, data: *const u8, queue_size: u32) -> usize; |
| 8 | + pub fn orb_advertise_multi(meta: *const Metadata, data: *const u8, instance: *mut i32, priority: i32) -> usize; |
| 9 | + pub fn orb_advertise_multi_queue(meta: *const Metadata, data: *const u8, instance: *mut i32, priority: i32, queue_size: u32) -> usize; |
| 10 | + pub fn orb_unadvertise(handle: usize) -> i32; |
| 11 | + pub fn orb_publish(meta: *const Metadata, handle: usize, data: *const u8) -> i32; |
| 12 | + pub fn orb_subscribe(meta: *const Metadata) -> i32; |
| 13 | + pub fn orb_subscribe_multi(meta: *const Metadata, instance: u32) -> i32; |
| 14 | + pub fn orb_unsubscribe(handle: i32) -> i32; |
| 15 | + pub fn orb_copy(meta: *const Metadata, handle: i32, buffer: *mut u8) -> i32; |
| 16 | + pub fn orb_check(handle: i32, updated: *mut bool) -> i32; |
| 17 | + pub fn orb_stat(handle: i32, time: *mut u64) -> i32; |
| 18 | + pub fn orb_exists(meta: *const Metadata, instance: i32) -> i32; |
| 19 | + pub fn orb_group_count(meta: *const Metadata) -> i32; |
| 20 | + pub fn orb_priority(handle: i32, priority: *mut i32) -> i32; |
| 21 | + pub fn orb_set_interval(handle: i32, interval: u32) -> i32; |
| 22 | + pub fn orb_get_interval(handle: i32, interval: *mut u32) -> i32; |
| 23 | +} |
| 24 | + |
| 25 | +#[repr(C)] |
| 26 | +#[derive(Copy, Clone)] |
| 27 | +pub struct Metadata { |
| 28 | + #[doc(hidden)] |
| 29 | + pub _name: *const u8, |
| 30 | + #[doc(hidden)] |
| 31 | + pub _size: u16, |
| 32 | + #[doc(hidden)] |
| 33 | + pub _size_no_padding: u16, |
| 34 | + #[doc(hidden)] |
| 35 | + pub _fields: *const u8, |
| 36 | +} |
| 37 | + |
| 38 | +unsafe impl Sync for Metadata {} |
| 39 | + |
| 40 | +impl Metadata { |
| 41 | + pub fn name_cstr(&self) -> &CStr { |
| 42 | + unsafe { CStr::from_ptr(self._name as *const _) } |
| 43 | + } |
| 44 | + pub fn name(&self) -> &str { |
| 45 | + unsafe { std::str::from_utf8_unchecked(self.name_cstr().to_bytes()) } |
| 46 | + } |
| 47 | + pub fn size(&self) -> u16 { |
| 48 | + self._size |
| 49 | + } |
| 50 | + pub fn size_no_padding(&self) -> u16 { |
| 51 | + self._size_no_padding |
| 52 | + } |
| 53 | + pub fn fields_cstr(&self) -> &CStr { |
| 54 | + unsafe { CStr::from_ptr(self._fields as *const _) } |
| 55 | + } |
| 56 | + pub fn fields(&self) -> &str { |
| 57 | + unsafe { std::str::from_utf8_unchecked(self.fields_cstr().to_bytes()) } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[macro_export] |
| 62 | +macro_rules! ORB_ID { |
| 63 | + ($name:ident) => { |
| 64 | + ORB_ID!(@ concat!("__orb_", stringify!($name))) |
| 65 | + }; |
| 66 | + (@ $name:expr) => { |
| 67 | + unsafe { |
| 68 | + extern "C" { |
| 69 | + #[link_name=$name] |
| 70 | + static metadata: px4::uorb::Metadata; |
| 71 | + } |
| 72 | + &metadata |
| 73 | + } |
| 74 | + }; |
| 75 | +} |
0 commit comments