Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SBP fields when materializing child objects. #130

Merged
merged 1 commit into from
Apr 23, 2015
Merged
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
12 changes: 9 additions & 3 deletions generator/sbpg/targets/resources/sbp_construct_template.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from construct import *
import json
from sbp import SBP
from sbp.utils import fmt_repr, exclude_fields, walk_json_dict
from sbp.utils import fmt_repr, exclude_fields, walk_json_dict, containerize
import six

((*- for i in include *))
Expand Down Expand Up @@ -102,6 +102,8 @@ class ((( m.identifier | classnameify )))(SBP):
(((f.desc)))
((*- endif *))
((*- endfor *))
sender : int
Optional sender ID, defaults to 0
((*- endif *))

"""
Expand All @@ -120,8 +122,11 @@ class ((( m.identifier | classnameify )))(SBP):
((*- else *))
self.payload = sbp.payload
((*- endif *))
((*- if m.fields*))
else:
super( ((( m.identifier | classnameify ))), self).__init__()
self.msg_type = SBP_(((m.identifier)))
self.sender = kwargs.pop('sender', 0)
((*- if m.fields*))
((*- for f in m.fields *))
self.(((f.identifier))) = kwargs.pop('(((f.identifier)))')
((*- endfor *))
Expand All @@ -142,7 +147,7 @@ class ((( m.identifier | classnameify )))(SBP):
"""Produce a framed/packed SBP message.

"""
c = Container(**exclude_fields(self))
c = containerize(exclude_fields(self))
self.payload = ((( m.identifier | classnameify )))._parser.build(c)
return self.pack()

Expand All @@ -156,6 +161,7 @@ class ((( m.identifier | classnameify )))(SBP):
return ((( m.identifier | classnameify )))(sbp)

def to_json_dict(self):
self.to_binary()
d = super( ((( m.identifier | classnameify ))), self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
Expand Down
21 changes: 15 additions & 6 deletions python/sbp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,30 @@ def __init__(self, msg_type=None, sender=None,
def __eq__(self, other):
return self.__dict__ == other.__dict__

def pack(self):
"""Pack to framed binary message.
def __update(self):
raise NotImplementedError("Internal update used by children.")

def _get_framed(self):
"""Returns the framed message and updates the CRC.

"""
self.length = len(self.payload)
framed_msg = struct.pack('<BHHB',
self.preamble,
self.msg_type,
self.sender,
len(self.payload))
self.length)
framed_msg += self.payload
crc = crc16(framed_msg[1:], 0)
framed_msg += struct.pack('<H', crc)
self.crc = crc16(framed_msg[1:], 0)
framed_msg += struct.pack('<H', self.crc)
return framed_msg

def pack(self):
"""Pack to framed binary message.

"""
return self._get_framed()

@staticmethod
def unpack(d):
"""Unpack and return a framed binary message.
Expand Down Expand Up @@ -145,4 +155,3 @@ def to_json_dict(self):
'length': self.length,
'payload': base64.standard_b64encode(self.payload),
'crc': self.crc}

10 changes: 8 additions & 2 deletions python/sbp/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from construct import *
import json
from sbp import SBP
from sbp.utils import fmt_repr, exclude_fields, walk_json_dict
from sbp.utils import fmt_repr, exclude_fields, walk_json_dict, containerize
import six

# Automatically generated from piksi/yaml/swiftnav/sbp/acquisition.yaml with generate.py.
Expand Down Expand Up @@ -56,6 +56,8 @@ class MsgAcqResult(SBP):
PRN-1 identifier of the satellite signal for which
acquisition was attempted

sender : int
Optional sender ID, defaults to 0

"""
_parser = Struct("MsgAcqResult",
Expand All @@ -69,6 +71,9 @@ def __init__(self, sbp=None, **kwargs):
self.__dict__.update(sbp.__dict__)
self.from_binary(sbp.payload)
else:
super( MsgAcqResult, self).__init__()
self.msg_type = SBP_MSG_ACQ_RESULT
self.sender = kwargs.pop('sender', 0)
self.snr = kwargs.pop('snr')
self.cp = kwargs.pop('cp')
self.cf = kwargs.pop('cf')
Expand All @@ -89,7 +94,7 @@ def to_binary(self):
"""Produce a framed/packed SBP message.

"""
c = Container(**exclude_fields(self))
c = containerize(exclude_fields(self))
self.payload = MsgAcqResult._parser.build(c)
return self.pack()

Expand All @@ -103,6 +108,7 @@ def from_json(s):
return MsgAcqResult(sbp)

def to_json_dict(self):
self.to_binary()
d = super( MsgAcqResult, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
Expand Down
26 changes: 22 additions & 4 deletions python/sbp/bootload.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from construct import *
import json
from sbp import SBP
from sbp.utils import fmt_repr, exclude_fields, walk_json_dict
from sbp.utils import fmt_repr, exclude_fields, walk_json_dict, containerize
import six

# Automatically generated from piksi/yaml/swiftnav/sbp/bootload.yaml with generate.py.
Expand Down Expand Up @@ -51,6 +51,8 @@ class MsgBootloaderHandshake(SBP):
SBP parent object to inherit from.
handshake : array
Version number string (not NULL terminated)
sender : int
Optional sender ID, defaults to 0

"""
_parser = Struct("MsgBootloaderHandshake",
Expand All @@ -61,6 +63,9 @@ def __init__(self, sbp=None, **kwargs):
self.__dict__.update(sbp.__dict__)
self.from_binary(sbp.payload)
else:
super( MsgBootloaderHandshake, self).__init__()
self.msg_type = SBP_MSG_BOOTLOADER_HANDSHAKE
self.sender = kwargs.pop('sender', 0)
self.handshake = kwargs.pop('handshake')

def __repr__(self):
Expand All @@ -78,7 +83,7 @@ def to_binary(self):
"""Produce a framed/packed SBP message.

"""
c = Container(**exclude_fields(self))
c = containerize(exclude_fields(self))
self.payload = MsgBootloaderHandshake._parser.build(c)
return self.pack()

Expand All @@ -92,6 +97,7 @@ def from_json(s):
return MsgBootloaderHandshake(sbp)

def to_json_dict(self):
self.to_binary()
d = super( MsgBootloaderHandshake, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
Expand All @@ -115,6 +121,8 @@ class MsgBootloaderJumpToApp(SBP):
SBP parent object to inherit from.
jump : int
Ignored by the device
sender : int
Optional sender ID, defaults to 0

"""
_parser = Struct("MsgBootloaderJumpToApp",
Expand All @@ -125,6 +133,9 @@ def __init__(self, sbp=None, **kwargs):
self.__dict__.update(sbp.__dict__)
self.from_binary(sbp.payload)
else:
super( MsgBootloaderJumpToApp, self).__init__()
self.msg_type = SBP_MSG_BOOTLOADER_JUMP_TO_APP
self.sender = kwargs.pop('sender', 0)
self.jump = kwargs.pop('jump')

def __repr__(self):
Expand All @@ -142,7 +153,7 @@ def to_binary(self):
"""Produce a framed/packed SBP message.

"""
c = Container(**exclude_fields(self))
c = containerize(exclude_fields(self))
self.payload = MsgBootloaderJumpToApp._parser.build(c)
return self.pack()

Expand All @@ -156,6 +167,7 @@ def from_json(s):
return MsgBootloaderJumpToApp(sbp)

def to_json_dict(self):
self.to_binary()
d = super( MsgBootloaderJumpToApp, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
Expand Down Expand Up @@ -186,6 +198,8 @@ class MsgNapDeviceDna(SBP):
57-bit SwiftNAP FPGA Device ID. Remaining bits are padded
on the right.

sender : int
Optional sender ID, defaults to 0

"""
_parser = Struct("MsgNapDeviceDna",
Expand All @@ -196,6 +210,9 @@ def __init__(self, sbp=None, **kwargs):
self.__dict__.update(sbp.__dict__)
self.from_binary(sbp.payload)
else:
super( MsgNapDeviceDna, self).__init__()
self.msg_type = SBP_MSG_NAP_DEVICE_DNA
self.sender = kwargs.pop('sender', 0)
self.dna = kwargs.pop('dna')

def __repr__(self):
Expand All @@ -213,7 +230,7 @@ def to_binary(self):
"""Produce a framed/packed SBP message.

"""
c = Container(**exclude_fields(self))
c = containerize(exclude_fields(self))
self.payload = MsgNapDeviceDna._parser.build(c)
return self.pack()

Expand All @@ -227,6 +244,7 @@ def from_json(s):
return MsgNapDeviceDna(sbp)

def to_json_dict(self):
self.to_binary()
d = super( MsgNapDeviceDna, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
Expand Down
10 changes: 8 additions & 2 deletions python/sbp/ext_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from construct import *
import json
from sbp import SBP
from sbp.utils import fmt_repr, exclude_fields, walk_json_dict
from sbp.utils import fmt_repr, exclude_fields, walk_json_dict, containerize
import six

# Automatically generated from piksi/yaml/swiftnav/sbp/ext_events.yaml with generate.py.
Expand Down Expand Up @@ -55,6 +55,8 @@ class MsgExtEvent(SBP):
Flags
pin : int
Pin number. 0..9 = DEBUG0..9.
sender : int
Optional sender ID, defaults to 0

"""
_parser = Struct("MsgExtEvent",
Expand All @@ -69,6 +71,9 @@ def __init__(self, sbp=None, **kwargs):
self.__dict__.update(sbp.__dict__)
self.from_binary(sbp.payload)
else:
super( MsgExtEvent, self).__init__()
self.msg_type = SBP_MSG_EXT_EVENT
self.sender = kwargs.pop('sender', 0)
self.wn = kwargs.pop('wn')
self.tow = kwargs.pop('tow')
self.ns = kwargs.pop('ns')
Expand All @@ -90,7 +95,7 @@ def to_binary(self):
"""Produce a framed/packed SBP message.

"""
c = Container(**exclude_fields(self))
c = containerize(exclude_fields(self))
self.payload = MsgExtEvent._parser.build(c)
return self.pack()

Expand All @@ -104,6 +109,7 @@ def from_json(s):
return MsgExtEvent(sbp)

def to_json_dict(self):
self.to_binary()
d = super( MsgExtEvent, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
Expand Down
Loading