Skip to content

Commit

Permalink
add friendly_name method to rust java python [DEVINFRA-1045] (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-kong authored Dec 11, 2022
1 parent 711b048 commit b4708cf
Show file tree
Hide file tree
Showing 387 changed files with 4,608 additions and 3 deletions.
1 change: 1 addition & 0 deletions generator/sbpg/specs/yaml_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
Optional('fields'): bitfield}})
definition = Schema({identifier:
{Optional('id'): sbp_identifier,
Optional('friendly_name'): description,
Optional('short_desc'): description,
Optional('desc'): description,
Optional('replaced_by'): [identifier],
Expand Down
25 changes: 24 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=""):
self.identifier = identifier
self.sbp_id = sbp_id
self.short_desc = short_desc
Expand All @@ -83,6 +84,7 @@ def __init__(self, identifier=None,
self.fields = fields or []
self.public = public
self.static = True
self.friendly_name = friendly_name or get_friendly_name(identifier)

@property
def max_type_len(self):
Expand Down Expand Up @@ -215,6 +217,27 @@ def __repr__(self):
##############################################################################
#

def get_friendly_name(identifier):
shorten_keyword = {
"TRACKING": "TRK",
"MEASUREMENT": "MEAS",
"INDEX": "IDX",
"NETWORK": "NET",
"EPHEMERIS": "EPH",
"_": " "
}
f_name = 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

def is_message(obj):
return isinstance(obj, Definition) and getattr(obj, 'sbp_id', None)

Expand Down
2 changes: 2 additions & 0 deletions generator/sbpg/targets/resources/MessageTest.java.j2
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class (((s.suite_name)))Test {
Number value;
Number expected;
((*- for f in t.fieldskeys *))(((compare_value( (((f))), (((t.fields[f]))) ))))((*- endfor *))

org.junit.Assert.assertNotEquals("", msg.getFriendlyName());
}
((*- endfor *))
}
24 changes: 23 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,7 @@ 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, Sbp, SbpMessage, TryFromSbpError};

pub use bytes::{Buf, BufMut};

Expand Down Expand Up @@ -85,6 +85,10 @@ pub trait SbpMessage: WireFormat + Clone + Sized {
fn gps_time(&self) -> Option<Result<crate::time::MessageTime, crate::time::GpsTimeError>> {
None
}
/// Get friendly name associated with the message.
fn friendly_name(&self) -> &'static str {
""
}
}

/// Implemented by messages who's message name and type are known at compile time.
Expand All @@ -96,6 +100,11 @@ 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;
}

/// The error returned when using [TryFrom] to convert [Sbp] to the wrong message type.
#[derive(Debug, Clone)]
pub struct TryFromSbpError(pub Sbp);
Expand Down Expand Up @@ -264,6 +273,19 @@ impl SbpMessage for Sbp {
},
}
}

fn friendly_name(&self) -> &'static str {
match self {
((*- for m in msgs *))
Sbp::(((m.msg_name)))(msg) => {
msg.friendly_name()
},
((*- endfor -*))
Sbp::Unknown(msg) => {
msg.friendly_name()
},
}
}
}

impl WireFormat for Sbp {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ impl SbpMessage for (((m.msg_name))) {
(((m.gps_time_fn)))
}

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

impl TryFrom<Sbp> for (((m.msg_name))) {
type Error = TryFromSbpError;
fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
Expand Down
6 changes: 6 additions & 0 deletions generator/sbpg/targets/resources/sbp_construct_template.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ class ((( m.identifier | classnameify )))(SBP):
self.payload = ((( m.identifier | classnameify )))._parser.build(c)
return self.pack()

def friendly_name(self):
"""Produces friendly human-readable name for this message
"""
return "(((m.friendly_name)))"

def into_buffer(self, buf, offset):
"""Produce a framed/packed SBP message into the provided buffer and offset.
Expand Down
5 changes: 5 additions & 0 deletions generator/sbpg/targets/resources/sbp_java.java.j2
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public class (((m.identifier | classnameify))) extends SBPMessage {
((*- endfor *)) ((*- endif *))
return obj;
}

@Override
public String getFriendlyName() {
return "(((m.friendly_name)))";
}
}
((*- endif *))
((*- if not m.is_real_message *))
Expand Down
1 change: 1 addition & 0 deletions generator/sbpg/targets/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ 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 = msg.friendly_name


class PackageItem(object):
Expand Down
4 changes: 4 additions & 0 deletions java/src/com/swiftnav/sbp/SBPMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ protected void build(Builder builder) {
assert false : "Subclass must override build()";
}

public String getFriendlyName() {
return "";
}

/** There is no exposed access to this class outside of libsbp. */
@SuppressWarnings("unchecked")
public class Parser {
Expand Down
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/acquisition/MsgAcqResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,9 @@ public JSONObject toJSON() {
obj.put("sid", sid.toJSON());
return obj;
}

@Override
public String getFriendlyName() {
return "ACQ RESULT";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/acquisition/MsgAcqResultDepA.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,9 @@ public JSONObject toJSON() {
obj.put("prn", prn);
return obj;
}

@Override
public String getFriendlyName() {
return "ACQ RESULT DEP A";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/acquisition/MsgAcqResultDepB.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,9 @@ public JSONObject toJSON() {
obj.put("sid", sid.toJSON());
return obj;
}

@Override
public String getFriendlyName() {
return "ACQ RESULT DEP B";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/acquisition/MsgAcqResultDepC.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ public JSONObject toJSON() {
obj.put("sid", sid.toJSON());
return obj;
}

@Override
public String getFriendlyName() {
return "ACQ RESULT DEP C";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/acquisition/MsgAcqSvProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ public JSONObject toJSON() {
obj.put("acq_sv_profile", SBPStruct.toJSONArray(acq_sv_profile));
return obj;
}

@Override
public String getFriendlyName() {
return "ACQ SV PROFILE";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/acquisition/MsgAcqSvProfileDep.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ public JSONObject toJSON() {
obj.put("acq_sv_profile", SBPStruct.toJSONArray(acq_sv_profile));
return obj;
}

@Override
public String getFriendlyName() {
return "ACQ SV PROFILE DEP";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ public JSONObject toJSON() {
obj.put("handshake", new JSONArray(handshake));
return obj;
}

@Override
public String getFriendlyName() {
return "BOOTLOADER HANDSHAKE DEP A";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ public JSONObject toJSON() {
JSONObject obj = super.toJSON();
return obj;
}

@Override
public String getFriendlyName() {
return "BOOTLOADER HANDSHAKE REQ";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ public JSONObject toJSON() {
obj.put("version", version);
return obj;
}

@Override
public String getFriendlyName() {
return "BOOTLOADER HANDSHAKE RESP";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ public JSONObject toJSON() {
obj.put("jump", jump);
return obj;
}

@Override
public String getFriendlyName() {
return "BOOTLOADER JUMP TO APP";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/bootload/MsgNapDeviceDnaReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public JSONObject toJSON() {
JSONObject obj = super.toJSON();
return obj;
}

@Override
public String getFriendlyName() {
return "NAP DEVICE DNA REQ";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/bootload/MsgNapDeviceDnaResp.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ public JSONObject toJSON() {
obj.put("dna", new JSONArray(dna));
return obj;
}

@Override
public String getFriendlyName() {
return "NAP DEVICE DNA RESP";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/ext_events/MsgExtEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@ public JSONObject toJSON() {
obj.put("pin", pin);
return obj;
}

@Override
public String getFriendlyName() {
return "EXT EVENT";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/file_io/MsgFileioConfigReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ public JSONObject toJSON() {
obj.put("sequence", sequence);
return obj;
}

@Override
public String getFriendlyName() {
return "FILEIO CONFIG REQ";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/file_io/MsgFileioConfigResp.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ public JSONObject toJSON() {
obj.put("fileio_version", fileio_version);
return obj;
}

@Override
public String getFriendlyName() {
return "FILEIO CONFIG RESP";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/file_io/MsgFileioReadDirReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ public JSONObject toJSON() {
obj.put("dirname", dirname);
return obj;
}

@Override
public String getFriendlyName() {
return "FILEIO READ DIR REQ";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/file_io/MsgFileioReadDirResp.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ public JSONObject toJSON() {
obj.put("contents", new JSONArray(contents));
return obj;
}

@Override
public String getFriendlyName() {
return "FILEIO READ DIR RESP";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/file_io/MsgFileioReadReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ public JSONObject toJSON() {
obj.put("filename", filename);
return obj;
}

@Override
public String getFriendlyName() {
return "FILEIO READ REQ";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/file_io/MsgFileioReadResp.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ public JSONObject toJSON() {
obj.put("contents", new JSONArray(contents));
return obj;
}

@Override
public String getFriendlyName() {
return "FILEIO READ RESP";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/file_io/MsgFileioRemove.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ public JSONObject toJSON() {
obj.put("filename", filename);
return obj;
}

@Override
public String getFriendlyName() {
return "FILEIO REMOVE";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/file_io/MsgFileioWriteReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ public JSONObject toJSON() {
obj.put("data", new JSONArray(data));
return obj;
}

@Override
public String getFriendlyName() {
return "FILEIO WRITE REQ";
}
}
5 changes: 5 additions & 0 deletions java/src/com/swiftnav/sbp/file_io/MsgFileioWriteResp.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ public JSONObject toJSON() {
obj.put("sequence", sequence);
return obj;
}

@Override
public String getFriendlyName() {
return "FILEIO WRITE RESP";
}
}
Loading

0 comments on commit b4708cf

Please sign in to comment.