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

Minor JSON fixes #110

Merged
merged 8 commits into from
Apr 16, 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
21 changes: 9 additions & 12 deletions generator/sbpg/targets/resources/sbp_construct_template.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,20 @@ class ((( m.identifier | classnameify )))(SBP):
self.payload = ((( m.identifier | classnameify )))._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( ((( m.identifier | classnameify ))), self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

@staticmethod
def from_json(data):
"""Given a JSON-encoded message, build an object.
def from_json(s):
"""Given a JSON-encoded string s, build a message object.

"""
d = json.loads(data)
d = json.loads(s)
sbp = SBP.from_json_dict(d)
return ((( m.identifier | classnameify )))(sbp)

def to_json_dict(self):
d = super( ((( m.identifier | classnameify ))), self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return d
((*- endif *))
((* endif *))
((*- else *))
Expand Down
33 changes: 26 additions & 7 deletions python/sbp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from construct import *
import base64
import json
import struct

SBP_PREAMBLE = 0x55
Expand Down Expand Up @@ -111,14 +112,31 @@ def __repr__(self):
fmt = "<SBP (preamble=0x%X, msg_type=0x%X, sender=%s, length=%d, payload=%s, crc=0x%X)>"
return fmt % p

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = self.to_json_dict()
return json.dumps(d)

@staticmethod
def from_json(s):
"""Given a JSON-encoded message, build an object.

"""
d = json.loads(s)
sbp = SBP.from_json_dict(d)
return sbp

@staticmethod
def from_json_dict(data):
msg_type = data['msg_type']
sender = data['sender']
length = data['length']
payload = base64.standard_b64decode(data['payload'])
crc = data['crc']
return SBP(msg_type, sender, length, payload, crc)
def from_json_dict(d):
sbp = SBP()
sbp.msg_type = d.pop('msg_type')
sbp.sender = d.pop('sender')
sbp.length = d.pop('length')
sbp.payload = base64.standard_b64decode(d.pop('payload'))
sbp.crc = d.pop('crc')
return sbp

def to_json_dict(self):
return {'preamble': self.preamble,
Expand All @@ -127,3 +145,4 @@ def to_json_dict(self):
'length': self.length,
'payload': base64.standard_b64encode(self.payload),
'crc': self.crc}

23 changes: 10 additions & 13 deletions python/sbp/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import six

# Automatically generated from piksi/yaml/swiftnav/sbp/acquisition.yaml
# with generate.py at 2015-04-15 12:17:09.634778. Please do not hand edit!
# with generate.py at 2015-04-15 15:32:14.097072. Please do not hand edit!


SBP_MSG_ACQ_RESULT = 0x0015
Expand Down Expand Up @@ -93,23 +93,20 @@ def to_binary(self):
self.payload = MsgAcqResult._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( MsgAcqResult, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

@staticmethod
def from_json(data):
"""Given a JSON-encoded message, build an object.
def from_json(s):
"""Given a JSON-encoded string s, build a message object.

"""
d = json.loads(data)
d = json.loads(s)
sbp = SBP.from_json_dict(d)
return MsgAcqResult(sbp)

def to_json_dict(self):
d = super( MsgAcqResult, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return d


msg_classes = {
Expand Down
96 changes: 39 additions & 57 deletions python/sbp/bootload.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,7 @@
import six

# Automatically generated from piksi/yaml/swiftnav/sbp/bootload.yaml
# with generate.py at 2015-04-15 12:17:09.616516. Please do not hand edit!


class StringAdapter(Adapter):
"""
"""
def _encode(self, obj, context):
return obj

def _decode(self, obj, context):
return obj
# with generate.py at 2015-04-15 15:32:14.101381. Please do not hand edit!


SBP_MSG_BOOTLOADER_HANDSHAKE = 0x00B0
Expand All @@ -48,7 +38,7 @@ class MsgBootloaderHandshake(SBP):
from an inherited SBP object, or construct it inline using a dict
of its fields.


The handshake message establishes a handshake between the device
bootloader and the host. The payload string contains the
bootloader version number, but returns an empty string for
Expand All @@ -60,21 +50,22 @@ class MsgBootloaderHandshake(SBP):
sbp : SBP
SBP parent object to inherit from.
handshake : string
Version number (NULL padded)
Version number (NULL terminated)

"""
_parser = Struct("MsgBootloaderHandshake", CString('handshake'))
_parser = Struct("MsgBootloaderHandshake",
CString('handshake', six.b('\n')),)

def __init__(self, sbp=None, **kwargs):
if sbp:
self.__dict__.update(sbp.__dict__)
self.from_binary(sbp.payload + '\x00')
self.from_binary(sbp.payload)
else:
self.handshake = kwargs.pop('handshake')

def __repr__(self):
return fmt_repr(self)

def from_binary(self, d):
"""Given a binary payload d, update the appropriate payload fields of
the message.
Expand All @@ -91,24 +82,21 @@ def to_binary(self):
self.payload = MsgBootloaderHandshake._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( MsgBootloaderHandshake, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

@staticmethod
def from_json(data):
"""Given a JSON-encoded message, build an object.
def from_json(s):
"""Given a JSON-encoded string s, build a message object.

"""
d = json.loads(data)
d = json.loads(s)
sbp = SBP.from_json_dict(d)
return MsgBootloaderHandshake(sbp)

def to_json_dict(self):
d = super( MsgBootloaderHandshake, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return d

SBP_MSG_BOOTLOADER_JUMP_TO_APP = 0x00B1
class MsgBootloaderJumpToApp(SBP):
"""SBP class for message MSG_BOOTLOADER_JUMP_TO_APP (0x00B1).
Expand All @@ -117,7 +105,7 @@ class MsgBootloaderJumpToApp(SBP):
from an inherited SBP object, or construct it inline using a dict
of its fields.


The host initiates the bootloader to jump to the application.


Expand All @@ -141,7 +129,7 @@ def __init__(self, sbp=None, **kwargs):

def __repr__(self):
return fmt_repr(self)

def from_binary(self, d):
"""Given a binary payload d, update the appropriate payload fields of
the message.
Expand All @@ -158,24 +146,21 @@ def to_binary(self):
self.payload = MsgBootloaderJumpToApp._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( MsgBootloaderJumpToApp, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

@staticmethod
def from_json(data):
"""Given a JSON-encoded message, build an object.
def from_json(s):
"""Given a JSON-encoded string s, build a message object.

"""
d = json.loads(data)
d = json.loads(s)
sbp = SBP.from_json_dict(d)
return MsgBootloaderJumpToApp(sbp)

def to_json_dict(self):
d = super( MsgBootloaderJumpToApp, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return d

SBP_MSG_NAP_DEVICE_DNA = 0x00DD
class MsgNapDeviceDna(SBP):
"""SBP class for message MSG_NAP_DEVICE_DNA (0x00DD).
Expand All @@ -184,7 +169,7 @@ class MsgNapDeviceDna(SBP):
from an inherited SBP object, or construct it inline using a dict
of its fields.


The device message from the host reads a unique device
identifier from the SwiftNAP, an FPGA. The host requests the ID
by sending a MSG_NAP_DEVICE_DNA with an empty payload. The
Expand Down Expand Up @@ -215,7 +200,7 @@ def __init__(self, sbp=None, **kwargs):

def __repr__(self):
return fmt_repr(self)

def from_binary(self, d):
"""Given a binary payload d, update the appropriate payload fields of
the message.
Expand All @@ -232,27 +217,24 @@ def to_binary(self):
self.payload = MsgNapDeviceDna._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( MsgNapDeviceDna, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

@staticmethod
def from_json(data):
"""Given a JSON-encoded message, build an object.
def from_json(s):
"""Given a JSON-encoded string s, build a message object.

"""
d = json.loads(data)
d = json.loads(s)
sbp = SBP.from_json_dict(d)
return MsgNapDeviceDna(sbp)

def to_json_dict(self):
d = super( MsgNapDeviceDna, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return d


msg_classes = {
0x00B0: MsgBootloaderHandshake,
0x00B1: MsgBootloaderJumpToApp,
0x00DD: MsgNapDeviceDna,
}
}
2 changes: 1 addition & 1 deletion python/sbp/client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import sys
import time

from .. import logging import SBP_MSG_PRINT
from ..logging import SBP_MSG_PRINT
from .drivers.file_driver import FileDriver
from .drivers.pyserial_driver import PySerialDriver
from .drivers.pyftdi_driver import PyFTDIDriver
Expand Down
23 changes: 10 additions & 13 deletions python/sbp/ext_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import six

# Automatically generated from piksi/yaml/swiftnav/sbp/ext_events.yaml
# with generate.py at 2015-04-15 12:17:09.620534. Please do not hand edit!
# with generate.py at 2015-04-15 15:32:14.096163. Please do not hand edit!


SBP_MSG_EXT_EVENT = 0x0101
Expand Down Expand Up @@ -94,23 +94,20 @@ def to_binary(self):
self.payload = MsgExtEvent._parser.build(c)
return self.pack()

def to_json(self):
"""Produce a JSON-encoded SBP message.

"""
d = super( MsgExtEvent, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return json.dumps(d)

@staticmethod
def from_json(data):
"""Given a JSON-encoded message, build an object.
def from_json(s):
"""Given a JSON-encoded string s, build a message object.

"""
d = json.loads(data)
d = json.loads(s)
sbp = SBP.from_json_dict(d)
return MsgExtEvent(sbp)

def to_json_dict(self):
d = super( MsgExtEvent, self).to_json_dict()
j = walk_json_dict(exclude_fields(self))
d.update(j)
return d


msg_classes = {
Expand Down
Loading