Skip to content

Commit

Permalink
Merge pull request swift-nav#1 from JoshuaGross/jgross-track-msg
Browse files Browse the repository at this point in the history
Support 64-bit unsigned ints in the JavaScript version of libsbp.
  • Loading branch information
ljbade authored Oct 27, 2016
2 parents 7ccefa0 + 27f6f3c commit cd3ed45
Show file tree
Hide file tree
Showing 24 changed files with 713 additions and 99 deletions.
25 changes: 22 additions & 3 deletions generator/sbpg/targets/javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
'u8': 'writeUInt8',
'u16': 'writeUInt16LE',
'u32': 'writeUInt32LE',
'u64': 'writeUIntLE', # only supports up to 48 bits of accuracy
'u64': 'writeUInt64LE', # only supports up to 48 bits of accuracy
's8': 'writeInt8',
's16': 'writeInt16LE',
's32': 'writeInt32LE',
's64': 'writeIntLE', # only supports up to 48 bits of accuracy
's64': 'writeInt64LE', # only supports up to 48 bits of accuracy
'float': 'writeFloatLE',
'double': 'writeDoubleLE',
}
Expand Down Expand Up @@ -97,6 +97,19 @@ def buffer_size(type_id, type_map=BUFFER_SIZE_MAP):
else:
return "this.fields.%s.length" % type_id.identifier

def array_fill_size(f):
if builtin_type(f.options['fill'].value):
return buffer_size(f.options['fill'].value)
else:
return buffer_size(f.type_id)

def array_length(f):
if 'size' in f.options:
return f.options.get('size').value
if 'size_fn' in f.options:
return "'%s'" % f.options.get('size_fn').value
return 'null'

def jsdoc_format(type_id, jsdoc=JSDOC_CODE):
"""
Formats type for jsdoc.
Expand Down Expand Up @@ -151,11 +164,17 @@ def star_wordwrap(s):
def star_wordwrap_indent(s):
return "\n * ".join(textwrap.wrap(' '.join(s.split('\n')), 80))

def islist(x):
return isinstance(x, list)

JENV.tests['builtinType'] = builtin_type
JENV.tests['list'] = islist

JENV.filters['js_classnameify'] = js_classnameify
JENV.filters['writeBuffer'] = write_buffer
JENV.filters['bufferSize'] = buffer_size
JENV.filters['arrayFillSize'] = array_fill_size
JENV.filters['arrayLength'] = array_length
JENV.filters['construct_js'] = construct_format
JENV.filters['jsdoc'] = jsdoc_format
JENV.filters['starWordWrap'] = star_wordwrap
Expand All @@ -171,7 +190,7 @@ def render_source(output_dir, package_spec, jenv=JENV):
destination_filename = "%s/%s.js" % (directory, name)
py_template = jenv.get_template(TEMPLATE_NAME)
module_path = ".".join(package_spec.identifier.split(".")[1:-1])
includeMap = {'gnss_signal':'GnssSignal'}
includeMap = {'gnss_signal':'GnssSignal', 'observation': ['ObsGPSTime', 'CarrierPhase'] }
includes = [".".join(i.split(".")[:-1]) for i in package_spec.includes]
includes = [(i, includeMap.get(i)) for i in includes if i != "types"]
with open(destination_filename, 'w') as f:
Expand Down
16 changes: 12 additions & 4 deletions generator/sbpg/targets/resources/sbp_js.js.j2
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@
***********************/

var SBP = require('./sbp');
var Parser = require('binary-parser').Parser;
var Parser = require('./parser');
var Int64 = require('node-int64');
var UInt64 = require('cuint').UINT64;
((*- for (file, ident) in include *))
((*- if ident is list *))
((*- for ident2 in ident *))
var (((ident2))) = require("./(((file)))").(((ident2)));
((*- endfor *))
((*- else *))
var (((ident))) = require("./(((file)))").(((ident)));
((*- endif *))
((*- endfor *))

((*- for m in msgs *))
Expand Down Expand Up @@ -60,11 +68,11 @@ var ((( m.identifier | js_classnameify ))) = function (sbp, fields) {
((*- if f.type_id is builtinType *))
((( m.identifier | js_classnameify ))).prototype.fieldSpec.push(['(((f.identifier)))', '(((f.type_id | writeBuffer)))', (((f.type_id | bufferSize)))]);
((*- elif f.type_id == 'array' and f.options['fill'].value is builtinType *))
((( m.identifier | js_classnameify ))).prototype.fieldSpec.push(['(((f.identifier)))', 'array', '(((f.options['fill'].value | writeBuffer)))', function () { return (((f.options['fill'].value | bufferSize))); }]);
((( m.identifier | js_classnameify ))).prototype.fieldSpec.push(['(((f.identifier)))', 'array', '(((f.options['fill'].value | writeBuffer)))', function () { return (((f | arrayFillSize))); }, ((( f | arrayLength )))]);
((*- elif f.type_id == 'array' *))
((( m.identifier | js_classnameify ))).prototype.fieldSpec.push(['(((f.identifier)))', 'array', ((( f.options['fill'].value ))).prototype.fieldSpec, function () { return (((f.type_id | bufferSize))); }]);
((( m.identifier | js_classnameify ))).prototype.fieldSpec.push(['(((f.identifier)))', 'array', ((( f.options['fill'].value ))).prototype.fieldSpec, function () { return (((f | arrayFillSize))); }, ((( f | arrayLength )))]);
((*- elif f.type_id == 'string' *))
((( m.identifier | js_classnameify ))).prototype.fieldSpec.push(['(((f.identifier)))', 'string']);
((( m.identifier | js_classnameify ))).prototype.fieldSpec.push(['(((f.identifier)))', 'string', ((( f | arrayLength )))]);
((*- else *))
((( m.identifier | js_classnameify ))).prototype.fieldSpec.push(['(((f.identifier)))', ((( f.type_id ))).prototype.fieldSpec]);
((*- endif *))
Expand Down
24 changes: 13 additions & 11 deletions javascript/sbp/acquisition.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
***********************/

var SBP = require('./sbp');
var Parser = require('binary-parser').Parser;
var Parser = require('./parser');
var Int64 = require('node-int64');
var UInt64 = require('cuint').UINT64;
var GnssSignal = require("./gnss_signal").GnssSignal;

/**
Expand Down Expand Up @@ -115,9 +117,9 @@ MsgAcqResultDepA.prototype.fieldSpec.push(['prn', 'writeUInt8', 1]);
* @field bin_width number (unsigned 16-bit int, 2 bytes) Acq frequency bin width
* @field timestamp number (unsigned 32-bit int, 4 bytes) Timestamp of the job complete event
* @field time_spent number (unsigned 32-bit int, 4 bytes) Time spent to search for sid.code
* @field cf_min number (unsigned 32-bit int, 4 bytes) Doppler range lowest frequency
* @field cf_max number (unsigned 32-bit int, 4 bytes) Doppler range highest frequency
* @field cf number (unsigned 32-bit int, 4 bytes) Doppler value of detected peak. Only valid if status is '1'
* @field cf_min number (signed 32-bit int, 4 bytes) Doppler range lowest frequency
* @field cf_max number (signed 32-bit int, 4 bytes) Doppler range highest frequency
* @field cf number (signed 32-bit int, 4 bytes) Doppler value of detected peak. Only valid if status is '1'
* @field cp number (unsigned 32-bit int, 4 bytes) Codephase of detected peak. Only valid if status is '1'
*
* @param sbp An SBP object with a payload to be decoded.
Expand All @@ -142,9 +144,9 @@ AcqSvProfile.prototype.parser = new Parser()
.uint16('bin_width')
.uint32('timestamp')
.uint32('time_spent')
.uint32('cf_min')
.uint32('cf_max')
.uint32('cf')
.int32('cf_min')
.int32('cf_max')
.int32('cf')
.uint32('cp');
AcqSvProfile.prototype.fieldSpec = [];
AcqSvProfile.prototype.fieldSpec.push(['job_type', 'writeUInt8', 1]);
Expand All @@ -155,9 +157,9 @@ AcqSvProfile.prototype.fieldSpec.push(['sid', GnssSignal.prototype.fieldSpec]);
AcqSvProfile.prototype.fieldSpec.push(['bin_width', 'writeUInt16LE', 2]);
AcqSvProfile.prototype.fieldSpec.push(['timestamp', 'writeUInt32LE', 4]);
AcqSvProfile.prototype.fieldSpec.push(['time_spent', 'writeUInt32LE', 4]);
AcqSvProfile.prototype.fieldSpec.push(['cf_min', 'writeUInt32LE', 4]);
AcqSvProfile.prototype.fieldSpec.push(['cf_max', 'writeUInt32LE', 4]);
AcqSvProfile.prototype.fieldSpec.push(['cf', 'writeUInt32LE', 4]);
AcqSvProfile.prototype.fieldSpec.push(['cf_min', 'writeInt32LE', 4]);
AcqSvProfile.prototype.fieldSpec.push(['cf_max', 'writeInt32LE', 4]);
AcqSvProfile.prototype.fieldSpec.push(['cf', 'writeInt32LE', 4]);
AcqSvProfile.prototype.fieldSpec.push(['cp', 'writeUInt32LE', 4]);

/**
Expand Down Expand Up @@ -186,7 +188,7 @@ MsgAcqSvProfile.prototype.parser = new Parser()
.endianess('little')
.array('acq_sv_profile', { type: AcqSvProfile.prototype.parser, readUntil: 'eof' });
MsgAcqSvProfile.prototype.fieldSpec = [];
MsgAcqSvProfile.prototype.fieldSpec.push(['acq_sv_profile', 'array', AcqSvProfile.prototype.fieldSpec, function () { return this.fields.array.length; }]);
MsgAcqSvProfile.prototype.fieldSpec.push(['acq_sv_profile', 'array', AcqSvProfile.prototype.fieldSpec, function () { return this.fields.array.length; }, null]);

module.exports = {
0x0014: MsgAcqResult,
Expand Down
10 changes: 6 additions & 4 deletions javascript/sbp/bootload.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
***********************/

var SBP = require('./sbp');
var Parser = require('binary-parser').Parser;
var Parser = require('./parser');
var Int64 = require('node-int64');
var UInt64 = require('cuint').UINT64;

/**
* SBP class for message MSG_BOOTLOADER_HANDSHAKE_REQ (0x00B3).
Expand Down Expand Up @@ -78,7 +80,7 @@ MsgBootloaderHandshakeResp.prototype.parser = new Parser()
.string('version', { greedy: true });
MsgBootloaderHandshakeResp.prototype.fieldSpec = [];
MsgBootloaderHandshakeResp.prototype.fieldSpec.push(['flags', 'writeUInt32LE', 4]);
MsgBootloaderHandshakeResp.prototype.fieldSpec.push(['version', 'string']);
MsgBootloaderHandshakeResp.prototype.fieldSpec.push(['version', 'string', null]);

/**
* SBP class for message MSG_BOOTLOADER_JUMP_TO_APP (0x00B1).
Expand Down Expand Up @@ -162,7 +164,7 @@ MsgNapDeviceDnaResp.prototype.parser = new Parser()
.endianess('little')
.array('dna', { length: 8, type: 'uint8' });
MsgNapDeviceDnaResp.prototype.fieldSpec = [];
MsgNapDeviceDnaResp.prototype.fieldSpec.push(['dna', 'array', 'writeUInt8', function () { return 1; }]);
MsgNapDeviceDnaResp.prototype.fieldSpec.push(['dna', 'array', 'writeUInt8', function () { return 1; }, 8]);

/**
* SBP class for message MSG_BOOTLOADER_HANDSHAKE_DEP_A (0x00B0).
Expand All @@ -189,7 +191,7 @@ MsgBootloaderHandshakeDepA.prototype.parser = new Parser()
.endianess('little')
.array('handshake', { type: 'uint8', readUntil: 'eof' });
MsgBootloaderHandshakeDepA.prototype.fieldSpec = [];
MsgBootloaderHandshakeDepA.prototype.fieldSpec.push(['handshake', 'array', 'writeUInt8', function () { return 1; }]);
MsgBootloaderHandshakeDepA.prototype.fieldSpec.push(['handshake', 'array', 'writeUInt8', function () { return 1; }, null]);

module.exports = {
0x00B3: MsgBootloaderHandshakeReq,
Expand Down
4 changes: 3 additions & 1 deletion javascript/sbp/ext_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
***********************/

var SBP = require('./sbp');
var Parser = require('binary-parser').Parser;
var Parser = require('./parser');
var Int64 = require('node-int64');
var UInt64 = require('cuint').UINT64;

/**
* SBP class for message MSG_EXT_EVENT (0x0101).
Expand Down
18 changes: 10 additions & 8 deletions javascript/sbp/file_io.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
***********************/

var SBP = require('./sbp');
var Parser = require('binary-parser').Parser;
var Parser = require('./parser');
var Int64 = require('node-int64');
var UInt64 = require('cuint').UINT64;

/**
* SBP class for message MSG_FILEIO_READ_REQ (0x00A8).
Expand Down Expand Up @@ -66,7 +68,7 @@ MsgFileioReadReq.prototype.fieldSpec = [];
MsgFileioReadReq.prototype.fieldSpec.push(['sequence', 'writeUInt32LE', 4]);
MsgFileioReadReq.prototype.fieldSpec.push(['offset', 'writeUInt32LE', 4]);
MsgFileioReadReq.prototype.fieldSpec.push(['chunk_size', 'writeUInt8', 1]);
MsgFileioReadReq.prototype.fieldSpec.push(['filename', 'string']);
MsgFileioReadReq.prototype.fieldSpec.push(['filename', 'string', null]);

/**
* SBP class for message MSG_FILEIO_READ_RESP (0x00A3).
Expand Down Expand Up @@ -99,7 +101,7 @@ MsgFileioReadResp.prototype.parser = new Parser()
.array('contents', { type: 'uint8', readUntil: 'eof' });
MsgFileioReadResp.prototype.fieldSpec = [];
MsgFileioReadResp.prototype.fieldSpec.push(['sequence', 'writeUInt32LE', 4]);
MsgFileioReadResp.prototype.fieldSpec.push(['contents', 'array', 'writeUInt8', function () { return 1; }]);
MsgFileioReadResp.prototype.fieldSpec.push(['contents', 'array', 'writeUInt8', function () { return 1; }, null]);

/**
* SBP class for message MSG_FILEIO_READ_DIR_REQ (0x00A9).
Expand Down Expand Up @@ -139,7 +141,7 @@ MsgFileioReadDirReq.prototype.parser = new Parser()
MsgFileioReadDirReq.prototype.fieldSpec = [];
MsgFileioReadDirReq.prototype.fieldSpec.push(['sequence', 'writeUInt32LE', 4]);
MsgFileioReadDirReq.prototype.fieldSpec.push(['offset', 'writeUInt32LE', 4]);
MsgFileioReadDirReq.prototype.fieldSpec.push(['dirname', 'string']);
MsgFileioReadDirReq.prototype.fieldSpec.push(['dirname', 'string', null]);

/**
* SBP class for message MSG_FILEIO_READ_DIR_RESP (0x00AA).
Expand Down Expand Up @@ -173,7 +175,7 @@ MsgFileioReadDirResp.prototype.parser = new Parser()
.array('contents', { type: 'uint8', readUntil: 'eof' });
MsgFileioReadDirResp.prototype.fieldSpec = [];
MsgFileioReadDirResp.prototype.fieldSpec.push(['sequence', 'writeUInt32LE', 4]);
MsgFileioReadDirResp.prototype.fieldSpec.push(['contents', 'array', 'writeUInt8', function () { return 1; }]);
MsgFileioReadDirResp.prototype.fieldSpec.push(['contents', 'array', 'writeUInt8', function () { return 1; }, null]);

/**
* SBP class for message MSG_FILEIO_REMOVE (0x00AC).
Expand Down Expand Up @@ -203,7 +205,7 @@ MsgFileioRemove.prototype.parser = new Parser()
.endianess('little')
.string('filename', { greedy: true });
MsgFileioRemove.prototype.fieldSpec = [];
MsgFileioRemove.prototype.fieldSpec.push(['filename', 'string']);
MsgFileioRemove.prototype.fieldSpec.push(['filename', 'string', null]);

/**
* SBP class for message MSG_FILEIO_WRITE_REQ (0x00AD).
Expand Down Expand Up @@ -243,8 +245,8 @@ MsgFileioWriteReq.prototype.parser = new Parser()
MsgFileioWriteReq.prototype.fieldSpec = [];
MsgFileioWriteReq.prototype.fieldSpec.push(['sequence', 'writeUInt32LE', 4]);
MsgFileioWriteReq.prototype.fieldSpec.push(['offset', 'writeUInt32LE', 4]);
MsgFileioWriteReq.prototype.fieldSpec.push(['filename', 'string']);
MsgFileioWriteReq.prototype.fieldSpec.push(['data', 'array', 'writeUInt8', function () { return 1; }]);
MsgFileioWriteReq.prototype.fieldSpec.push(['filename', 'string', null]);
MsgFileioWriteReq.prototype.fieldSpec.push(['data', 'array', 'writeUInt8', function () { return 1; }, null]);

/**
* SBP class for message MSG_FILEIO_WRITE_RESP (0x00AB).
Expand Down
16 changes: 9 additions & 7 deletions javascript/sbp/flash.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
***********************/

var SBP = require('./sbp');
var Parser = require('binary-parser').Parser;
var Parser = require('./parser');
var Int64 = require('node-int64');
var UInt64 = require('cuint').UINT64;

/**
* SBP class for message MSG_FLASH_PROGRAM (0x00E6).
Expand Down Expand Up @@ -59,9 +61,9 @@ MsgFlashProgram.prototype.parser = new Parser()
.array('data', { type: 'uint8', length: 'addr_len' });
MsgFlashProgram.prototype.fieldSpec = [];
MsgFlashProgram.prototype.fieldSpec.push(['target', 'writeUInt8', 1]);
MsgFlashProgram.prototype.fieldSpec.push(['addr_start', 'array', 'writeUInt8', function () { return 1; }]);
MsgFlashProgram.prototype.fieldSpec.push(['addr_start', 'array', 'writeUInt8', function () { return 1; }, 3]);
MsgFlashProgram.prototype.fieldSpec.push(['addr_len', 'writeUInt8', 1]);
MsgFlashProgram.prototype.fieldSpec.push(['data', 'array', 'writeUInt8', function () { return 1; }]);
MsgFlashProgram.prototype.fieldSpec.push(['data', 'array', 'writeUInt8', function () { return 1; }, 'addr_len']);

/**
* SBP class for message MSG_FLASH_DONE (0x00E0).
Expand Down Expand Up @@ -126,7 +128,7 @@ MsgFlashReadReq.prototype.parser = new Parser()
.uint8('addr_len');
MsgFlashReadReq.prototype.fieldSpec = [];
MsgFlashReadReq.prototype.fieldSpec.push(['target', 'writeUInt8', 1]);
MsgFlashReadReq.prototype.fieldSpec.push(['addr_start', 'array', 'writeUInt8', function () { return 1; }]);
MsgFlashReadReq.prototype.fieldSpec.push(['addr_start', 'array', 'writeUInt8', function () { return 1; }, 3]);
MsgFlashReadReq.prototype.fieldSpec.push(['addr_len', 'writeUInt8', 1]);

/**
Expand Down Expand Up @@ -163,7 +165,7 @@ MsgFlashReadResp.prototype.parser = new Parser()
.uint8('addr_len');
MsgFlashReadResp.prototype.fieldSpec = [];
MsgFlashReadResp.prototype.fieldSpec.push(['target', 'writeUInt8', 1]);
MsgFlashReadResp.prototype.fieldSpec.push(['addr_start', 'array', 'writeUInt8', function () { return 1; }]);
MsgFlashReadResp.prototype.fieldSpec.push(['addr_start', 'array', 'writeUInt8', function () { return 1; }, 3]);
MsgFlashReadResp.prototype.fieldSpec.push(['addr_len', 'writeUInt8', 1]);

/**
Expand Down Expand Up @@ -306,7 +308,7 @@ MsgStmUniqueIdResp.prototype.parser = new Parser()
.endianess('little')
.array('stm_id', { length: 12, type: 'uint8' });
MsgStmUniqueIdResp.prototype.fieldSpec = [];
MsgStmUniqueIdResp.prototype.fieldSpec.push(['stm_id', 'array', 'writeUInt8', function () { return 1; }]);
MsgStmUniqueIdResp.prototype.fieldSpec.push(['stm_id', 'array', 'writeUInt8', function () { return 1; }, 12]);

/**
* SBP class for message MSG_M25_FLASH_WRITE_STATUS (0x00F3).
Expand Down Expand Up @@ -334,7 +336,7 @@ MsgM25FlashWriteStatus.prototype.parser = new Parser()
.endianess('little')
.array('status', { length: 1, type: 'uint8' });
MsgM25FlashWriteStatus.prototype.fieldSpec = [];
MsgM25FlashWriteStatus.prototype.fieldSpec.push(['status', 'array', 'writeUInt8', function () { return 1; }]);
MsgM25FlashWriteStatus.prototype.fieldSpec.push(['status', 'array', 'writeUInt8', function () { return 1; }, 1]);

module.exports = {
0x00E6: MsgFlashProgram,
Expand Down
4 changes: 3 additions & 1 deletion javascript/sbp/gnss_signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
***********************/

var SBP = require('./sbp');
var Parser = require('binary-parser').Parser;
var Parser = require('./parser');
var Int64 = require('node-int64');
var UInt64 = require('cuint').UINT64;

/**
* SBP class for message fragment GnssSignal
Expand Down
10 changes: 6 additions & 4 deletions javascript/sbp/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
***********************/

var SBP = require('./sbp');
var Parser = require('binary-parser').Parser;
var Parser = require('./parser');
var Int64 = require('node-int64');
var UInt64 = require('cuint').UINT64;

/**
* SBP class for message MSG_LOG (0x0401).
Expand Down Expand Up @@ -51,7 +53,7 @@ MsgLog.prototype.parser = new Parser()
.string('text', { greedy: true });
MsgLog.prototype.fieldSpec = [];
MsgLog.prototype.fieldSpec.push(['level', 'writeUInt8', 1]);
MsgLog.prototype.fieldSpec.push(['text', 'string']);
MsgLog.prototype.fieldSpec.push(['text', 'string', null]);

/**
* SBP class for message MSG_TWEET (0x0012).
Expand All @@ -78,7 +80,7 @@ MsgTweet.prototype.parser = new Parser()
.endianess('little')
.string('tweet', { length: 140 });
MsgTweet.prototype.fieldSpec = [];
MsgTweet.prototype.fieldSpec.push(['tweet', 'string']);
MsgTweet.prototype.fieldSpec.push(['tweet', 'string', 140]);

/**
* SBP class for message MSG_PRINT_DEP (0x0010).
Expand All @@ -105,7 +107,7 @@ MsgPrintDep.prototype.parser = new Parser()
.endianess('little')
.string('text', { greedy: true });
MsgPrintDep.prototype.fieldSpec = [];
MsgPrintDep.prototype.fieldSpec.push(['text', 'string']);
MsgPrintDep.prototype.fieldSpec.push(['text', 'string', null]);

module.exports = {
0x0401: MsgLog,
Expand Down
4 changes: 3 additions & 1 deletion javascript/sbp/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
***********************/

var SBP = require('./sbp');
var Parser = require('binary-parser').Parser;
var Parser = require('./parser');
var Int64 = require('node-int64');
var UInt64 = require('cuint').UINT64;

/**
* SBP class for message MSG_GPS_TIME (0x0100).
Expand Down
Loading

0 comments on commit cd3ed45

Please sign in to comment.