Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions generator/sbpg/specs/yaml2.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ def mk_definition(defn):
assert not short_desc.endswith("."), f"{identifier}: Remove . from: `{short_desc}`"
assert all(x in string.printable for x in short_desc), f"Unprintable: {short_desc}"
assert len(short_desc.splitlines()) == 1, f"Multi-line short_desc: {short_desc}"

friendly_name = contents.get("friendly_name", "")
message_display = contents.get("message_display", "")
return sbp.resolve_type(sbp.Definition(identifier=identifier,
sbp_id=contents.get('id', None),
short_desc=short_desc,
Expand All @@ -281,6 +284,8 @@ def mk_definition(defn):
fields=fs,
public=contents.get('public', True),
embedded_type=contents.get('embedded_type', False),
friendly_name=friendly_name,
message_display=message_display
))

def mk_field(field):
Expand Down
2 changes: 2 additions & 0 deletions generator/sbpg/specs/yaml_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
Optional('fields'): bitfield}})
definition = Schema({identifier:
{Optional('id'): sbp_identifier,
Optional('friendly_name'): description,
Optional('message_display'): description,
Optional('short_desc'): description,
Optional('desc'): description,
Optional('replaced_by'): [identifier],
Expand Down
5 changes: 4 additions & 1 deletion generator/sbpg/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def __repr__(self):
class Definition(object):
def __init__(self, identifier=None,
sbp_id=None, short_desc=None, desc=None, type_id=None,
fields=None, public=False, embedded_type=False):
fields=None, public=False, embedded_type=False,
friendly_name="", message_display=""):
self.identifier = identifier
self.sbp_id = sbp_id
self.short_desc = short_desc
Expand All @@ -83,6 +84,8 @@ def __init__(self, identifier=None,
self.fields = fields or []
self.public = public
self.static = True
self.friendly_name = friendly_name
self.message_display = message_display

@property
def max_type_len(self):
Expand Down
14 changes: 13 additions & 1 deletion generator/sbpg/targets/resources/rust/sbp_messages_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ mod lib {
pub use crate::time;
pub use crate::wire_format::{PayloadParseError, WireFormat};

pub use super::{ConcreteMessage, Sbp, SbpMessage, TryFromSbpError};
pub use super::{
ConcreteMessage, FriendlyName, MessageDisplay, Sbp, SbpMessage, TryFromSbpError,
};

pub use bytes::{Buf, BufMut};

Expand Down Expand Up @@ -96,6 +98,16 @@ pub trait ConcreteMessage: SbpMessage + TryFrom<Sbp, Error = TryFromSbpError> {
const MESSAGE_NAME: &'static str;
}

/// Friendly name representation of Sbp message
pub trait FriendlyName {
fn friendly_name() -> &'static str;
}

/// Enriched fields display of Sbp messages
pub trait MessageDisplay {
fn message_display(&self) -> String;
}

/// The error returned when using [TryFrom] to convert [Sbp] to the wrong message type.
#[derive(Debug, Clone)]
pub struct TryFromSbpError;
Expand Down
16 changes: 16 additions & 0 deletions generator/sbpg/targets/resources/rust/sbp_messages_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ impl TryFrom<Sbp> for (((m.msg_name))) {
}
((* endif *))

impl FriendlyName for (((m.msg_name))) {
fn friendly_name() -> &'static str {
"(((m.friendly_name)))"
}
}

impl MessageDisplay for (((m.msg_name))) {
fn message_display(&self) -> String {
((* if m.enrich_fields *))
format!("(((m.enrich_display)))", (((m.enrich_fields))) )
((* else *))
"(((m.message_display)))".to_string()
((* endif *))
}
}

impl WireFormat for (((m.msg_name))) {
const MIN_LEN: usize =
((*- if not m.fields *))
Expand Down
52 changes: 51 additions & 1 deletion generator/sbpg/targets/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
Generator for rust target.
"""

import re

from jinja2.environment import Environment
from jinja2.utils import pass_environment

Expand Down Expand Up @@ -330,6 +332,30 @@ def get_bitfield(field):
return items


def get_friendly_name(msg):
if msg.friendly_name:
return msg.friendly_name
shorten_keyword = {
"TRACKING": "TRK",
"MEASUREMENT": "MEAS",
"INDEX": "IDX",
"NETWORK": "NET",
"EPHEMERIS": "EPH",
"_": " "
}
f_name = msg.identifier
if f_name.endswith("_GNSS"):
f_name = f_name[:-5] + " GNSS-only"

# replace MSG_
if f_name.startswith("MSG_"):
f_name = f_name[4:]

for key, item in shorten_keyword.items():
f_name = f_name.replace(key, item)
return f_name


class FieldItem(object):
def __init__(self, msg, package_specs, field):
self.identifier = field.identifier
Expand All @@ -351,6 +377,24 @@ def __init__(self, msg, package_specs, field):
if self.options.get("fields", False):
self.bitfield = get_bitfield(self)

# pattern to capture {{groups}} for enriched message display
# optional {{group}:1} to denote format!("{:1}", field) roundings
ENRICH_PAT = re.compile("{{([^}]+)}(:\\d+)?}")
# pattern to capture field to represent self.@field or self.#field.message_display()
ENRICH_FIELD_PAT = re.compile("([@#]\\w+)")

# for enriched field display
def extract_self_field(match_obj):
match = match_obj.group()
assert match[0] == '#' or match[0] == '@'
if match[0] == '#':
return f"self.{match[1:]}.message_display()"
return f"self.{match[1:]}"


def map_to_fields(f):
return re.sub(ENRICH_FIELD_PAT, extract_self_field, f[0])


class MsgItem(object):
def __init__(self, msg, package, package_specs):
Expand All @@ -370,7 +414,13 @@ def __init__(self, msg, package, package_specs):
if len(field.bitfield) > 0:
self.has_bitfield = True
self.gps_time_fn = gps_time_fn(self)

self.friendly_name = get_friendly_name(msg)
self.message_display = msg.message_display
if self.message_display:
# match regex, capture all {{field}} enclosed by two brackets
enrich_fields = re.findall(ENRICH_PAT, self.message_display)
self.enrich_fields = ', '.join(map(map_to_fields, enrich_fields))
self.enrich_display = re.sub(ENRICH_PAT, "{\\2}", self.message_display)

class PackageItem(object):
def __init__(self, package, package_specs):
Expand Down
96 changes: 96 additions & 0 deletions rust/sbp/src/messages/acquisition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ pub mod acq_sv_profile {
pub cp: u32,
}

impl FriendlyName for AcqSvProfile {
fn friendly_name() -> &'static str {
"AcqSvProfile"
}
}

impl MessageDisplay for AcqSvProfile {
fn message_display(&self) -> String {
"".to_string()
}
}

impl WireFormat for AcqSvProfile {
const MIN_LEN: usize = <u8 as WireFormat>::MIN_LEN
+ <u8 as WireFormat>::MIN_LEN
Expand Down Expand Up @@ -190,6 +202,18 @@ pub mod acq_sv_profile_dep {
pub cp: u32,
}

impl FriendlyName for AcqSvProfileDep {
fn friendly_name() -> &'static str {
"AcqSvProfileDep"
}
}

impl MessageDisplay for AcqSvProfileDep {
fn message_display(&self) -> String {
"".to_string()
}
}

impl WireFormat for AcqSvProfileDep {
const MIN_LEN: usize = <u8 as WireFormat>::MIN_LEN
+ <u8 as WireFormat>::MIN_LEN
Expand Down Expand Up @@ -318,6 +342,18 @@ pub mod msg_acq_result {
}
}

impl FriendlyName for MsgAcqResult {
fn friendly_name() -> &'static str {
"ACQ RESULT"
}
}

impl MessageDisplay for MsgAcqResult {
fn message_display(&self) -> String {
"".to_string()
}
}

impl WireFormat for MsgAcqResult {
const MIN_LEN: usize = <f32 as WireFormat>::MIN_LEN
+ <f32 as WireFormat>::MIN_LEN
Expand Down Expand Up @@ -414,6 +450,18 @@ pub mod msg_acq_result_dep_a {
}
}

impl FriendlyName for MsgAcqResultDepA {
fn friendly_name() -> &'static str {
"ACQ RESULT DEP A"
}
}

impl MessageDisplay for MsgAcqResultDepA {
fn message_display(&self) -> String {
"".to_string()
}
}

impl WireFormat for MsgAcqResultDepA {
const MIN_LEN: usize = <f32 as WireFormat>::MIN_LEN
+ <f32 as WireFormat>::MIN_LEN
Expand Down Expand Up @@ -509,6 +557,18 @@ pub mod msg_acq_result_dep_b {
}
}

impl FriendlyName for MsgAcqResultDepB {
fn friendly_name() -> &'static str {
"ACQ RESULT DEP B"
}
}

impl MessageDisplay for MsgAcqResultDepB {
fn message_display(&self) -> String {
"".to_string()
}
}

impl WireFormat for MsgAcqResultDepB {
const MIN_LEN: usize = <f32 as WireFormat>::MIN_LEN
+ <f32 as WireFormat>::MIN_LEN
Expand Down Expand Up @@ -603,6 +663,18 @@ pub mod msg_acq_result_dep_c {
}
}

impl FriendlyName for MsgAcqResultDepC {
fn friendly_name() -> &'static str {
"ACQ RESULT DEP C"
}
}

impl MessageDisplay for MsgAcqResultDepC {
fn message_display(&self) -> String {
"".to_string()
}
}

impl WireFormat for MsgAcqResultDepC {
const MIN_LEN: usize = <f32 as WireFormat>::MIN_LEN
+ <f32 as WireFormat>::MIN_LEN
Expand Down Expand Up @@ -689,6 +761,18 @@ pub mod msg_acq_sv_profile {
}
}

impl FriendlyName for MsgAcqSvProfile {
fn friendly_name() -> &'static str {
"ACQ SV PROFILE"
}
}

impl MessageDisplay for MsgAcqSvProfile {
fn message_display(&self) -> String {
"".to_string()
}
}

impl WireFormat for MsgAcqSvProfile {
const MIN_LEN: usize = <Vec<AcqSvProfile> as WireFormat>::MIN_LEN;
fn len(&self) -> usize {
Expand Down Expand Up @@ -762,6 +846,18 @@ pub mod msg_acq_sv_profile_dep {
}
}

impl FriendlyName for MsgAcqSvProfileDep {
fn friendly_name() -> &'static str {
"ACQ SV PROFILE DEP"
}
}

impl MessageDisplay for MsgAcqSvProfileDep {
fn message_display(&self) -> String {
"".to_string()
}
}

impl WireFormat for MsgAcqSvProfileDep {
const MIN_LEN: usize = <Vec<AcqSvProfileDep> as WireFormat>::MIN_LEN;
fn len(&self) -> usize {
Expand Down
Loading