use std::ffi::c_char;
use std::ptr;
use rtcm3::Rtcm3Message;
use rtcm3::message::{CellData, GlonassBias, GpsMsm7, SatelliteData, StationArp};
pub const RTCM3_FFI_MAX_SATELLITES: usize = 64;
pub const RTCM3_FFI_MAX_CELLS: usize = 64;
pub struct Rtcm3FfiMessage {
message: Rtcm3Message,
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Rtcm3FfiError {
Ok = 0,
NullInput = 1,
Frame = 2,
Bit = 3,
Message = 4,
}
impl From<rtcm3::Rtcm3Error> for Rtcm3FfiError {
fn from(e: rtcm3::Rtcm3Error) -> Self {
match e {
rtcm3::Rtcm3Error::Frame(_) => Self::Frame,
rtcm3::Rtcm3Error::Bit(_) => Self::Bit,
_ => Self::Message,
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rtcm3_decode(
buf: *const u8,
len: usize,
out_error: *mut Rtcm3FfiError,
) -> *mut Rtcm3FfiMessage {
let set_error = |code: Rtcm3FfiError| {
if !out_error.is_null() {
unsafe {
out_error.write(code);
}
}
};
if buf.is_null() {
set_error(Rtcm3FfiError::NullInput);
return ptr::null_mut();
}
let bytes = unsafe { core::slice::from_raw_parts(buf, len) };
match rtcm3::decode(bytes) {
Ok(message) => {
set_error(Rtcm3FfiError::Ok);
Box::into_raw(Box::new(Rtcm3FfiMessage { message }))
}
Err(e) => {
set_error(Rtcm3FfiError::from(e));
ptr::null_mut()
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rtcm3_message_free(msg: *mut Rtcm3FfiMessage) {
if !msg.is_null() {
drop(unsafe { Box::from_raw(msg) });
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rtcm3_message_type(msg: *const Rtcm3FfiMessage) -> u16 {
match unsafe { msg.as_ref() } {
Some(m) => match m.message {
Rtcm3Message::StationArp(_) => 1005,
Rtcm3Message::GlonassBias(_) => 1230,
Rtcm3Message::GpsMsm7(_) => 1077,
_ => 0xFFFF,
},
None => 0xFFFF,
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Rtcm3StationArp {
pub reference_station_id: u16,
pub itrf_realization_year: u8,
pub gps_indicator: bool,
pub glonass_indicator: bool,
pub galileo_indicator: bool,
pub reference_station_indicator: bool,
pub ecef_x_raw: i64,
pub single_oscillator_indicator: bool,
pub ecef_y_raw: i64,
pub quarter_cycle_indicator: u8,
pub ecef_z_raw: i64,
}
impl From<StationArp> for Rtcm3StationArp {
fn from(m: StationArp) -> Self {
Self {
reference_station_id: m.reference_station_id,
itrf_realization_year: m.itrf_realization_year,
gps_indicator: m.gps_indicator,
glonass_indicator: m.glonass_indicator,
galileo_indicator: m.galileo_indicator,
reference_station_indicator: m.reference_station_indicator,
ecef_x_raw: m.ecef_x_raw,
single_oscillator_indicator: m.single_oscillator_indicator,
ecef_y_raw: m.ecef_y_raw,
quarter_cycle_indicator: m.quarter_cycle_indicator,
ecef_z_raw: m.ecef_z_raw,
}
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Rtcm3GlonassBias {
pub reference_station_id: u16,
pub code_phase_bias_indicator: bool,
pub l1_ca_bias_present: bool,
pub l1_ca_bias_raw: i32,
pub l1_p_bias_present: bool,
pub l1_p_bias_raw: i32,
pub l2_ca_bias_present: bool,
pub l2_ca_bias_raw: i32,
pub l2_p_bias_present: bool,
pub l2_p_bias_raw: i32,
}
impl From<GlonassBias> for Rtcm3GlonassBias {
fn from(m: GlonassBias) -> Self {
Self {
reference_station_id: m.reference_station_id,
code_phase_bias_indicator: m.code_phase_bias_indicator,
l1_ca_bias_present: m.l1_ca_bias_raw.is_some(),
l1_ca_bias_raw: m.l1_ca_bias_raw.unwrap_or(0),
l1_p_bias_present: m.l1_p_bias_raw.is_some(),
l1_p_bias_raw: m.l1_p_bias_raw.unwrap_or(0),
l2_ca_bias_present: m.l2_ca_bias_raw.is_some(),
l2_ca_bias_raw: m.l2_ca_bias_raw.unwrap_or(0),
l2_p_bias_present: m.l2_p_bias_raw.is_some(),
l2_p_bias_raw: m.l2_p_bias_raw.unwrap_or(0),
}
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Rtcm3SatelliteData {
pub rough_range_ms_raw: u8,
pub extended_info_raw: u8,
pub rough_range_mod_raw: u16,
pub rough_phase_range_rate_raw: i16,
}
impl From<SatelliteData> for Rtcm3SatelliteData {
fn from(s: SatelliteData) -> Self {
Self {
rough_range_ms_raw: s.rough_range_ms_raw,
extended_info_raw: s.extended_info_raw,
rough_range_mod_raw: s.rough_range_mod_raw,
rough_phase_range_rate_raw: s.rough_phase_range_rate_raw,
}
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Rtcm3CellData {
pub fine_pseudorange_raw: i32,
pub fine_phase_range_raw: i32,
pub lock_time_raw: u16,
pub half_cycle_ambiguity: bool,
pub cnr_raw: u16,
pub fine_phase_range_rate_raw: i16,
}
impl From<CellData> for Rtcm3CellData {
fn from(c: CellData) -> Self {
Self {
fine_pseudorange_raw: c.fine_pseudorange_raw,
fine_phase_range_raw: c.fine_phase_range_raw,
lock_time_raw: c.lock_time_raw,
half_cycle_ambiguity: c.half_cycle_ambiguity,
cnr_raw: c.cnr_raw,
fine_phase_range_rate_raw: c.fine_phase_range_rate_raw,
}
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Rtcm3GpsMsm7 {
pub reference_station_id: u16,
pub gps_epoch_time_tow_ms: u32,
pub multiple_message_bit: bool,
pub issue_of_data_station: u8,
pub clock_steering_indicator: u8,
pub external_clock_indicator: u8,
pub smoothing_indicator: bool,
pub smoothing_interval_raw: u8,
pub satellite_mask: u64,
pub signal_mask: u32,
pub cell_mask: u64,
pub num_satellites: u32,
pub num_signals: u32,
pub num_cells: u32,
pub satellites: [Rtcm3SatelliteData; RTCM3_FFI_MAX_SATELLITES],
pub cells: [Rtcm3CellData; RTCM3_FFI_MAX_CELLS],
}
impl From<GpsMsm7> for Rtcm3GpsMsm7 {
fn from(m: GpsMsm7) -> Self {
Self {
reference_station_id: m.reference_station_id,
gps_epoch_time_tow_ms: m.gps_epoch_time_tow_ms,
multiple_message_bit: m.multiple_message_bit,
issue_of_data_station: m.issue_of_data_station,
clock_steering_indicator: m.clock_steering_indicator,
external_clock_indicator: m.external_clock_indicator,
smoothing_indicator: m.smoothing_indicator,
smoothing_interval_raw: m.smoothing_interval_raw,
satellite_mask: m.satellite_mask,
signal_mask: m.signal_mask,
cell_mask: m.cell_mask,
num_satellites: m.num_satellites(),
num_signals: m.num_signals(),
num_cells: m.num_cells(),
satellites: m.satellites.map(Into::into),
cells: m.cells.map(Into::into),
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rtcm3_message_as_station_arp(
msg: *const Rtcm3FfiMessage,
out: *mut Rtcm3StationArp,
) -> bool {
let Some(m) = (unsafe { msg.as_ref() }) else {
return false;
};
let Rtcm3Message::StationArp(inner) = m.message else {
return false;
};
unsafe {
out.write(inner.into());
}
true
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rtcm3_message_as_glonass_bias(
msg: *const Rtcm3FfiMessage,
out: *mut Rtcm3GlonassBias,
) -> bool {
let Some(m) = (unsafe { msg.as_ref() }) else {
return false;
};
let Rtcm3Message::GlonassBias(inner) = m.message else {
return false;
};
unsafe {
out.write(inner.into());
}
true
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rtcm3_message_as_gps_msm7(
msg: *const Rtcm3FfiMessage,
out: *mut Rtcm3GpsMsm7,
) -> bool {
let Some(m) = (unsafe { msg.as_ref() }) else {
return false;
};
let Rtcm3Message::GpsMsm7(inner) = m.message else {
return false;
};
unsafe {
out.write(inner.into());
}
true
}
#[unsafe(no_mangle)]
pub extern "C" fn rtcm3_version() -> *const c_char {
const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "\0");
VERSION.as_ptr().cast::<c_char>()
}