Skip to content

Commit 434020b

Browse files
committed
Merge pull request #154 from mfine/mfine-protocol-version
Deprecate bootloader handshake + send SBP protocol version
2 parents f0d6f67 + 28624cf commit 434020b

File tree

13 files changed

+315
-45
lines changed

13 files changed

+315
-45
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ sbp_out.*
6262

6363
# Virtual Envs
6464
/*env
65+
66+
RELEASE-VERSION

c/include/libsbp/bootload.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@
4545
* The handshake message response from the device establishes a
4646
* handshake between the device bootloader and the host. The
4747
* request from the host is MSG_BOOTLOADER_HANDSHAKE_HOST. The
48-
* payload string contains the bootloader version number, but
49-
* returns an empty string for earlier versions.
48+
* payload contains the bootloader version number and the SBP
49+
* protocol version number.
5050
*/
51-
#define SBP_MSG_BOOTLOADER_HANDSHAKE_DEVICE 0x00B0
51+
#define SBP_MSG_BOOTLOADER_HANDSHAKE_DEVICE 0x00B4
5252
typedef struct __attribute__((packed)) {
53-
u8 handshake[0]; /**< Version number string (not NULL terminated) */
53+
u32 flags; /**< Bootloader flags */
54+
char* version; /**< Bootloader version number */
5455
} msg_bootloader_handshake_device_t;
5556

5657

c/include/libsbp/deprecated.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,25 @@
2626
#include "common.h"
2727

2828

29+
/** Bootloading handshake response (host <= device)
30+
*
31+
* The handshake message response from the device establishes a
32+
* handshake between the device bootloader and the host. The
33+
* request from the host is MSG_BOOTLOADER_HANDSHAKE_HOST. The
34+
* payload string contains the bootloader version number, but
35+
* returns an empty string for earlier versions.
36+
*/
37+
#define SBP_MSG_BOOTLOADER_HANDSHAKE_DEPRECATED 0x00B0
38+
typedef struct __attribute__((packed)) {
39+
u8 handshake[0]; /**< Version number string (not NULL terminated) */
40+
} msg_bootloader_handshake_deprecated_t;
41+
42+
2943
/** Deprecated
3044
*
3145
* Deprecated.
3246
*/
33-
#define SBP_MSG_EPHEMERIS_DEPRECATED 0x001A
47+
#define SBP_MSG_EPHEMERIS_DEPRECATED 0x001A
3448
typedef struct __attribute__((packed)) {
3549
double tgd; /**< Group delay differential between L1 and L2 [s] */
3650
double c_rs; /**< Amplitude of the sine harmonic correction term to the orbit radius [m] */

python/sbp/bootload.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ def __repr__(self):
5959
return fmt_repr(self)
6060

6161

62-
SBP_MSG_BOOTLOADER_HANDSHAKE_DEVICE = 0x00B0
62+
SBP_MSG_BOOTLOADER_HANDSHAKE_DEVICE = 0x00B4
6363
class MsgBootloaderHandshakeDevice(SBP):
64-
"""SBP class for message MSG_BOOTLOADER_HANDSHAKE_DEVICE (0x00B0).
64+
"""SBP class for message MSG_BOOTLOADER_HANDSHAKE_DEVICE (0x00B4).
6565
6666
You can have MSG_BOOTLOADER_HANDSHAKE_DEVICE inherent its fields directly
6767
from an inherited SBP object, or construct it inline using a dict
@@ -71,22 +71,25 @@ class MsgBootloaderHandshakeDevice(SBP):
7171
The handshake message response from the device establishes a
7272
handshake between the device bootloader and the host. The
7373
request from the host is MSG_BOOTLOADER_HANDSHAKE_HOST. The
74-
payload string contains the bootloader version number, but
75-
returns an empty string for earlier versions.
74+
payload contains the bootloader version number and the SBP
75+
protocol version number.
7676
7777
7878
Parameters
7979
----------
8080
sbp : SBP
8181
SBP parent object to inherit from.
82-
handshake : array
83-
Version number string (not NULL terminated)
82+
flags : int
83+
Bootloader flags
84+
version : string
85+
Bootloader version number
8486
sender : int
8587
Optional sender ID, defaults to 0
8688
8789
"""
8890
_parser = Struct("MsgBootloaderHandshakeDevice",
89-
OptionalGreedyRange(ULInt8('handshake')),)
91+
ULInt32('flags'),
92+
CString('version', six.b('\n')),)
9093

9194
def __init__(self, sbp=None, **kwargs):
9295
if sbp:
@@ -96,7 +99,8 @@ def __init__(self, sbp=None, **kwargs):
9699
super( MsgBootloaderHandshakeDevice, self).__init__()
97100
self.msg_type = SBP_MSG_BOOTLOADER_HANDSHAKE_DEVICE
98101
self.sender = kwargs.pop('sender', 0)
99-
self.handshake = kwargs.pop('handshake')
102+
self.flags = kwargs.pop('flags')
103+
self.version = kwargs.pop('version')
100104

101105
def __repr__(self):
102106
return fmt_repr(self)
@@ -315,7 +319,7 @@ def to_json_dict(self):
315319

316320
msg_classes = {
317321
0x00B3: MsgBootloaderHandshakeHost,
318-
0x00B0: MsgBootloaderHandshakeDevice,
322+
0x00B4: MsgBootloaderHandshakeDevice,
319323
0x00B1: MsgBootloaderJumpToApp,
320324
0x00DE: MsgNapDeviceDnaHost,
321325
0x00DD: MsgNapDeviceDnaDevice,

python/sbp/deprecated.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,80 @@
2424
# Please do not hand edit!
2525

2626

27+
SBP_MSG_BOOTLOADER_HANDSHAKE_DEPRECATED = 0x00B0
28+
class MsgBootloaderHandshakeDeprecated(SBP):
29+
"""SBP class for message MSG_BOOTLOADER_HANDSHAKE_DEPRECATED (0x00B0).
30+
31+
You can have MSG_BOOTLOADER_HANDSHAKE_DEPRECATED inherent its fields directly
32+
from an inherited SBP object, or construct it inline using a dict
33+
of its fields.
34+
35+
36+
The handshake message response from the device establishes a
37+
handshake between the device bootloader and the host. The
38+
request from the host is MSG_BOOTLOADER_HANDSHAKE_HOST. The
39+
payload string contains the bootloader version number, but
40+
returns an empty string for earlier versions.
41+
42+
43+
Parameters
44+
----------
45+
sbp : SBP
46+
SBP parent object to inherit from.
47+
handshake : array
48+
Version number string (not NULL terminated)
49+
sender : int
50+
Optional sender ID, defaults to 0
51+
52+
"""
53+
_parser = Struct("MsgBootloaderHandshakeDeprecated",
54+
OptionalGreedyRange(ULInt8('handshake')),)
55+
56+
def __init__(self, sbp=None, **kwargs):
57+
if sbp:
58+
self.__dict__.update(sbp.__dict__)
59+
self.from_binary(sbp.payload)
60+
else:
61+
super( MsgBootloaderHandshakeDeprecated, self).__init__()
62+
self.msg_type = SBP_MSG_BOOTLOADER_HANDSHAKE_DEPRECATED
63+
self.sender = kwargs.pop('sender', 0)
64+
self.handshake = kwargs.pop('handshake')
65+
66+
def __repr__(self):
67+
return fmt_repr(self)
68+
69+
def from_binary(self, d):
70+
"""Given a binary payload d, update the appropriate payload fields of
71+
the message.
72+
73+
"""
74+
p = MsgBootloaderHandshakeDeprecated._parser.parse(d)
75+
self.__dict__.update(dict(p.viewitems()))
76+
77+
def to_binary(self):
78+
"""Produce a framed/packed SBP message.
79+
80+
"""
81+
c = containerize(exclude_fields(self))
82+
self.payload = MsgBootloaderHandshakeDeprecated._parser.build(c)
83+
return self.pack()
84+
85+
@staticmethod
86+
def from_json(s):
87+
"""Given a JSON-encoded string s, build a message object.
88+
89+
"""
90+
d = json.loads(s)
91+
sbp = SBP.from_json_dict(d)
92+
return MsgBootloaderHandshakeDeprecated(sbp)
93+
94+
def to_json_dict(self):
95+
self.to_binary()
96+
d = super( MsgBootloaderHandshakeDeprecated, self).to_json_dict()
97+
j = walk_json_dict(exclude_fields(self))
98+
d.update(j)
99+
return d
100+
27101
SBP_MSG_EPHEMERIS_DEPRECATED = 0x001A
28102
class MsgEphemerisDeprecated(SBP):
29103
"""SBP class for message MSG_EPHEMERIS_DEPRECATED (0x001A).
@@ -195,5 +269,6 @@ def to_json_dict(self):
195269

196270

197271
msg_classes = {
272+
0x00B0: MsgBootloaderHandshakeDeprecated,
198273
0x001A: MsgEphemerisDeprecated,
199274
}

python/sbp/version.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Author: Douglas Creager <dcreager@dcreager.net>
4+
# This file is placed into the public domain.
5+
# Adapted from Gist: https://gist.github.com/3067886
6+
7+
# Calculates the current version number. If possible, this is the
8+
# output of “git describe”, modified to conform to the versioning
9+
# scheme that setuptools uses. If “git describe” returns an error
10+
# (most likely because we're in an unpacked copy of a release tarball,
11+
# rather than in a git working copy), then we fall back on reading the
12+
# contents of the RELEASE-VERSION file.
13+
#
14+
# To use this script, simply import it your setup.py file, and use the
15+
# results of get_git_version() as your package version:
16+
#
17+
# from version import *
18+
#
19+
# setup(
20+
# version=get_git_version(),
21+
# .
22+
# .
23+
# .
24+
# )
25+
#
26+
# This will automatically update the RELEASE-VERSION file, if
27+
# necessary. Note that the RELEASE-VERSION file should *not* be
28+
# checked into git; please add it to your top-level .gitignore file.
29+
#
30+
# You'll probably want to distribute the RELEASE-VERSION file in your
31+
# sdist tarballs; to do this, just create a MANIFEST.in file that
32+
# contains the following line:
33+
#
34+
# include RELEASE-VERSION
35+
36+
__all__ = ("get_git_version")
37+
38+
from subprocess import Popen, PIPE
39+
import os
40+
41+
def call_git_describe():
42+
try:
43+
p = Popen(['git', 'describe', '--tags', '--dirty', '--always'],
44+
stdout=PIPE, stderr=PIPE)
45+
p.stderr.close()
46+
line = p.stdout.readlines()[0]
47+
return line.strip()
48+
except:
49+
return None
50+
51+
52+
def read_release_version():
53+
try:
54+
f = open(os.path.join(os.path.dirname(__file__), 'RELEASE-VERSION'), "r")
55+
try:
56+
version = f.readlines()[0]
57+
return version.strip()
58+
finally:
59+
f.close()
60+
except:
61+
return None
62+
63+
64+
def write_release_version(version):
65+
f = open(os.path.join(os.path.dirname(__file__), 'RELEASE-VERSION'), "w")
66+
f.write("%s\n" % version)
67+
f.close()
68+
69+
70+
def get_git_version():
71+
# Read in the version that's currently in RELEASE-VERSION.
72+
release_version = read_release_version()
73+
74+
# First try to get the current version using “git describe”.
75+
version = call_git_describe()
76+
77+
# Take off the leading if present.
78+
if version is not None and version[0] == 'v':
79+
version = version[1:]
80+
81+
#adapt to PEP 386 compatible versioning scheme
82+
version = pep386adapt(version)
83+
84+
# If that doesn't work, fall back on the value that's in
85+
# RELEASE-VERSION.
86+
if version is None:
87+
version = release_version
88+
89+
# If we still don't have anything, that's an error.
90+
if version is None:
91+
raise ValueError("Cannot find the version number!")
92+
93+
# If the current version is different from what's in the
94+
# RELEASE-VERSION file, update the file to be current.
95+
if version != release_version:
96+
write_release_version(version)
97+
98+
# Finally, return the current version.
99+
return version
100+
101+
102+
def pep386adapt(version):
103+
if version is not None and '-' in version:
104+
# adapt git-describe version to be in line with PEP 386
105+
# Break PEP 386 a bit here and append the Git hash
106+
parts = version.split('-')
107+
if len(parts) > 2:
108+
version = '%s.post%s-%s' % (
109+
parts[0], parts[1],
110+
'-'.join(parts[2:])
111+
)
112+
return version
113+
else:
114+
return version
115+
116+
VERSION = get_git_version()
117+
118+
if __name__ == "__main__":
119+
print get_git_version()
120+

python/setup.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
from setuptools import setup
44
import os
5-
6-
VERSION = "0.43"
5+
from sbp.version import VERSION
76

87
CLASSIFIERS = [
98
'Intended Audience :: Developers',
@@ -30,6 +29,10 @@
3029
'win32',
3130
]
3231

32+
PACKAGE_DATA = { 'sbp' : [
33+
'RELEASE-VERSION',
34+
] }
35+
3336
cwd = os.path.abspath(os.path.dirname(__file__))
3437
with open(cwd + '/README.rst') as f:
3538
readme = f.read()
@@ -47,6 +50,7 @@
4750
classifiers=CLASSIFIERS,
4851
packages=PACKAGES,
4952
platforms=PLATFORMS,
53+
package_data=PACKAGE_DATA,
5054
install_requires=INSTALL_REQUIRES,
5155
use_2to3=False,
5256
zip_safe=False)

python/tests/sbp/test_table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_table_count():
3434
Test number of available messages to deserialize.
3535
3636
"""
37-
number_of_messages = 51
37+
number_of_messages = 52
3838
assert len(_SBP_TABLE) == number_of_messages
3939

4040
def test_table_unqiue_count():

0 commit comments

Comments
 (0)