From 7f3234fef6c5905c3c05ba3a5424ff70944a0330 Mon Sep 17 00:00:00 2001 From: Peter Hall <33176108+IamPete1@users.noreply.github.com> Date: Mon, 11 Nov 2024 22:31:02 +0000 Subject: [PATCH 1/2] Generator: Javascript: whitespace fixup --- generator/mavgen_javascript.py | 443 ++++++++++++++++----------------- 1 file changed, 220 insertions(+), 223 deletions(-) diff --git a/generator/mavgen_javascript.py b/generator/mavgen_javascript.py index 79dbcfb1d..5598dc9bd 100644 --- a/generator/mavgen_javascript.py +++ b/generator/mavgen_javascript.py @@ -111,7 +111,7 @@ def generate_preamble(outf, msgs, args, xml): ${MAVHEAD}.header.prototype.pack = function() { return jspack.Pack('BBBBBBBHB', [${PROTOCOL_MARKER}, this.mlen, this.incompat_flags, this.compat_flags, this.seq, this.srcSystem, this.srcComponent, ((this.msgId & 0xFF) << 8) | ((this.msgId >> 8) & 0xFF), this.msgId>>16]); } - """, {'PROTOCOL_MARKER' : xml.protocol_marker, +""", {'PROTOCOL_MARKER' : xml.protocol_marker, 'MAVHEAD': get_mavhead(xml)}) # Mavlink1 else: @@ -120,7 +120,7 @@ def generate_preamble(outf, msgs, args, xml): ${MAVHEAD}.header.prototype.pack = function() { return jspack.Pack('BBBBBB', [${PROTOCOL_MARKER}, this.mlen, this.seq, this.srcSystem, this.srcComponent, this.msgId]); } - """, {'PROTOCOL_MARKER' : xml.protocol_marker, +""", {'PROTOCOL_MARKER' : xml.protocol_marker, 'MAVHEAD': get_mavhead(xml)}) t.write(outf, """ @@ -131,7 +131,7 @@ def generate_preamble(outf, msgs, args, xml): // Convenience setter to facilitate turning the unpacked array of data into member properties ${MAVHEAD}.message.prototype.set = function(args,verbose) { -// inspect + // inspect _.each(this.fieldnames, function(e, i) { var num = parseInt(i,10); if (this.hasOwnProperty(e) && isNaN(num) ){ // asking for an attribute that's non-numeric is ok unless its already an attribute we have @@ -139,6 +139,7 @@ def generate_preamble(outf, msgs, args, xml): } }, this); //console.log(this.fieldnames); + // then modify _.each(this.fieldnames, function(e, i) { this[e] = args[i]; @@ -390,19 +391,19 @@ def generate_mavlink_class(outf, msgs, xml): ${MAVHEAD}.messages.bad_data.prototype = new ${MAVHEAD}.message; // MAVLink signing state class -MAVLinkSigning = function MAVLinkSigning(object){ - this.secret_key = new Buffer.from([]) ; //new Buffer.from([ 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 ]) // secret key must be a Buffer obj of 32 length - this.timestamp = 1 - this.link_id = 0 - this.sign_outgoing = false // todo false this - this.allow_unsigned_callback = undefined - this.stream_timestamps = {} - this.sig_count = 0 - this.badsig_count = 0 - this.goodsig_count = 0 - this.unsigned_count = 0 - this.reject_count = 0 -} +MAVLinkSigning = function MAVLinkSigning(object){ + this.secret_key = new Buffer.from([]); //new Buffer.from([ 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 ]) // secret key must be a Buffer obj of 32 length + this.timestamp = 1 + this.link_id = 0 + this.sign_outgoing = false // todo false this + this.allow_unsigned_callback = undefined + this.stream_timestamps = {} + this.sig_count = 0 + this.badsig_count = 0 + this.goodsig_count = 0 + this.unsigned_count = 0 + this.reject_count = 0 +} /* MAVLink protocol handling class */ ${MAVPROCESSOR} = function(logger, srcSystem, srcComponent) { @@ -412,14 +413,14 @@ def generate_mavlink_class(outf, msgs, xml): this.seq = 0; this.buf = new Buffer.from([]); this.bufInError = new Buffer.from([]); - + this.srcSystem = (typeof srcSystem === 'undefined') ? 0 : srcSystem; this.srcComponent = (typeof srcComponent === 'undefined') ? 0 : srcComponent; this.have_prefix_error = false; // The first packet we expect is a valid header, 6 bytes. - this.protocol_marker = ${PROTOCOL_MARKER}; + this.protocol_marker = ${PROTOCOL_MARKER}; this.expected_length = ${MAVHEAD}.HEADER_LEN; this.little_endian = true; @@ -432,7 +433,7 @@ def generate_mavlink_class(outf, msgs, xml): this.total_receive_errors = 0; this.startup_time = Date.now(); - // optional , but when used we store signing state in this object: + // optional , but when used we store signing state in this object: this.signing = new MAVLinkSigning(); } @@ -496,16 +497,16 @@ def generate_mavlink_class(outf, msgs, xml): // us know if we have signing enabled, which affects the real-world length by the signature-block length of 13 bytes. // once successful, 'this.expected_length' is correctly set for the whole packet. ${MAVPROCESSOR}.prototype.parseLength = function() { - - if( this.buf.length >= 3 ) { - var unpacked = jspack.Unpack('BBB', this.buf.slice(0, 3)); - var magic = unpacked[0]; // stx ie fd or fe etc - this.expected_length = unpacked[1] + ${MAVHEAD}.HEADER_LEN + 2 // length of message + header + CRC (ie non-signed length) - this.incompat_flags = unpacked[2]; - // mavlink2 only.. in mavlink1, incompat_flags var above is actually the 'seq', but for this test its ok. - if ((magic == ${MAVHEAD}.PROTOCOL_MARKER_V2 ) && ( this.incompat_flags & ${MAVHEAD}.MAVLINK_IFLAG_SIGNED )){ - this.expected_length += ${MAVHEAD}.MAVLINK_SIGNATURE_BLOCK_LEN; - } + + if( this.buf.length >= 3 ) { + var unpacked = jspack.Unpack('BBB', this.buf.slice(0, 3)); + var magic = unpacked[0]; // stx ie fd or fe etc + this.expected_length = unpacked[1] + ${MAVHEAD}.HEADER_LEN + 2 // length of message + header + CRC (ie non-signed length) + this.incompat_flags = unpacked[2]; + // mavlink2 only.. in mavlink1, incompat_flags var above is actually the 'seq', but for this test its ok. + if ((magic == ${MAVHEAD}.PROTOCOL_MARKER_V2 ) && ( this.incompat_flags & ${MAVHEAD}.MAVLINK_IFLAG_SIGNED )){ + this.expected_length += ${MAVHEAD}.MAVLINK_SIGNATURE_BLOCK_LEN; + } } } @@ -516,19 +517,17 @@ def generate_mavlink_class(outf, msgs, xml): var m = null; try { - this.pushBuffer(c); this.parsePrefix(); this.parseLength(); m = this.parsePayload(); } catch(e) { - this.log('error', e.message); this.total_receive_errors += 1; m = new ${MAVHEAD}.messages.bad_data(this.bufInError, e.message); this.bufInError = new Buffer.from([]); - + } // emit a packet-specific message as well as a generic message, user/s can choose to use either or both of these. @@ -624,91 +623,91 @@ def generate_mavlink_class(outf, msgs, xml): //check signature on incoming message , many of the comments in this file come from the python impl ${MAVPROCESSOR}.prototype.check_signature = function(msgbuf, srcSystem, srcComponent) { - //if (isinstance(msgbuf, array.array)){ - // msgbuf = msgbuf.tostring() - //} - if ( Buffer.isBuffer(msgbuf) ) { - msgbuf = toArrayBuffer(msgbuf); - } - - //timestamp_buf = msgbuf[-12:-6] - var timestamp_buf= msgbuf.slice(-12,-6); - - //link_id = msgbuf[-13] - var link_id= new Buffer.from(msgbuf.slice(-13,-12)); // just a single byte really, but returned as a buffer + //if (isinstance(msgbuf, array.array)){ + // msgbuf = msgbuf.tostring() + //} + if ( Buffer.isBuffer(msgbuf) ) { + msgbuf = toArrayBuffer(msgbuf); + } + + //timestamp_buf = msgbuf[-12:-6] + var timestamp_buf= msgbuf.slice(-12,-6); + + //link_id = msgbuf[-13] + var link_id= new Buffer.from(msgbuf.slice(-13,-12)); // just a single byte really, but returned as a buffer link_id = link_id[0]; // get the first byte. - - //self.mav_sign_unpacker = jspack.Unpack(' 0)){ - var len_if_signed = mlen+signature_len; - //console.log("Packet appears signed && labeled as signed, OK. msgId=" + msgId); - - } else if ((mlen == actual_len_nosign) && (signature_len > 0)){ - - var len_if_signed = mlen+signature_len; + // is packet supposed to be signed? + if ( incompat_flags & ${MAVHEAD}.MAVLINK_IFLAG_SIGNED ){ + signature_len = ${MAVHEAD}.MAVLINK_SIGNATURE_BLOCK_LEN; + } else { + signature_len = 0; + } + + // header's declared len compared to packets actual len + var actual_len = (msgbuf.length - (${MAVHEAD}.HEADER_LEN + 2 + signature_len)); + var actual_len_nosign = (msgbuf.length - (${MAVHEAD}.HEADER_LEN + 2 )); + + if ((mlen == actual_len) && (signature_len > 0)){ + var len_if_signed = mlen+signature_len; + //console.log("Packet appears signed && labeled as signed, OK. msgId=" + msgId); + + } else if ((mlen == actual_len_nosign) && (signature_len > 0)){ + + var len_if_signed = mlen+signature_len; throw new Error("Packet appears unsigned when labeled as signed. Got actual_len "+actual_len_nosign+" expected " + len_if_signed + ", msgId=" + msgId); - - } else if( mlen != actual_len) { + + } else if( mlen != actual_len) { throw new Error("Invalid MAVLink message length. Got " + (msgbuf.length - (${MAVHEAD}.HEADER_LEN + 2)) + " expected " + mlen + ", msgId=" + msgId); - } - + } + if( false === _.has(${MAVHEAD}.map, msgId) ) { throw new Error("Unknown MAVLink message ID (" + msgId + ")"); } - // here's the common chunks of packet we want to work with below.. - var headerBuf= msgbuf.slice(${MAVHEAD}.HEADER_LEN); // first10 - var sigBuf = msgbuf.slice(-signature_len); // last 13 or nothing - var crcBuf1 = msgbuf.slice(-2); // either last-2 or last-2-prior-to-signature - var crcBuf2 = msgbuf.slice(-15,-13); // either last-2 or last-2-prior-to-signature - var payloadBuf = msgbuf.slice(${MAVHEAD}.HEADER_LEN, -(signature_len+2)); // the remaining bit between the header and the crc - var crcCheckBuf = msgbuf.slice(1, -(signature_len+2)); // the part uses to calculate the crc - ie between the magic and signature, + // here's the common chunks of packet we want to work with below.. + var headerBuf= msgbuf.slice(${MAVHEAD}.HEADER_LEN); // first10 + var sigBuf = msgbuf.slice(-signature_len); // last 13 or nothing + var crcBuf1 = msgbuf.slice(-2); // either last-2 or last-2-prior-to-signature + var crcBuf2 = msgbuf.slice(-15,-13); // either last-2 or last-2-prior-to-signature + var payloadBuf = msgbuf.slice(${MAVHEAD}.HEADER_LEN, -(signature_len+2)); // the remaining bit between the header and the crc + var crcCheckBuf = msgbuf.slice(1, -(signature_len+2)); // the part uses to calculate the crc - ie between the magic and signature, // decode the payload // refs: (fmt, type, order_map, crc_extra) = ${MAVHEAD}.map[msgId] var decoder = ${MAVHEAD}.map[msgId]; // decode the checksum - var receivedChecksum = undefined; - if ( signature_len == 0 ) { // unsigned - try { - receivedChecksum = jspack.Unpack(' Date: Mon, 11 Nov 2024 22:52:56 +0000 Subject: [PATCH 2/2] Generator: Javascript: use `forEach` and `Uint8Array` --- generator/mavgen_javascript.py | 115 +++++++++++++-------------------- 1 file changed, 45 insertions(+), 70 deletions(-) diff --git a/generator/mavgen_javascript.py b/generator/mavgen_javascript.py index 5598dc9bd..1e1cd608c 100644 --- a/generator/mavgen_javascript.py +++ b/generator/mavgen_javascript.py @@ -35,17 +35,9 @@ def generate_preamble(outf, msgs, args, xml): */ jspack = require("jspack").jspack, - _ = require("underscore"), events = require("events"), // for .emit(..), MAVLink20Processor inherits from events.EventEmitter util = require("util"); -var Buffer = require('buffer').Buffer; // required in react - no impact in node -var Long = require('long'); - -// Add a convenience method to Buffer -Buffer.prototype.toByteArray = function () { - return Array.prototype.slice.call(this, 0) -} ${MAVHEAD} = function(){}; @@ -54,7 +46,7 @@ def generate_preamble(outf, msgs, args, xml): var bytes = buffer; var crcOUT = crcIN === undefined ? 0xffff : crcIN; - _.each(bytes, function(e) { + bytes.forEach(function(e) { var tmp = e ^ (crcOUT & 0xff); tmp = (tmp ^ (tmp << 4)) & 0xff; crcOUT = (crcOUT >> 8) ^ (tmp << 8) ^ (tmp << 3) ^ (tmp >> 4); @@ -132,16 +124,15 @@ def generate_preamble(outf, msgs, args, xml): // Convenience setter to facilitate turning the unpacked array of data into member properties ${MAVHEAD}.message.prototype.set = function(args,verbose) { // inspect - _.each(this.fieldnames, function(e, i) { + this.fieldnames.forEach(function(e, i) { var num = parseInt(i,10); if (this.hasOwnProperty(e) && isNaN(num) ){ // asking for an attribute that's non-numeric is ok unless its already an attribute we have if ( verbose >= 1) { console.log("WARNING, overwriting an existing property is DANGEROUS:"+e+" ==>"+i+"==>"+args[i]+" -> "+JSON.stringify(this)); } } }, this); - //console.log(this.fieldnames); -// then modify - _.each(this.fieldnames, function(e, i) { + // then modify + this.fieldnames.forEach(function(e, i) { this[e] = args[i]; }, this); }; @@ -167,11 +158,11 @@ def generate_preamble(outf, msgs, args, xml): // first add the linkid(1 byte) and timestamp(6 bytes) that start the signature this._msgbuf = this._msgbuf.concat(jspack.Pack(' payloadBuf.length) { - payloadBuf = Buffer.concat([payloadBuf, Buffer.alloc(paylen - payloadBuf.length)]); + payloadBuf = this.concat_buffer(payloadBuf, new Uint8Array(paylen - payloadBuf.length).fill(0)); } """) @@ -915,7 +890,7 @@ def generate_mavlink_class(outf, msgs, xml): if (elementsInMsg == actualElementsInMsg) { // Reorder the fields to match the order map - _.each(t, function(e, i, l) { + t.forEach(function(e, i, l) { args[i] = t[decoder.order_map[i]] }); } else { @@ -959,7 +934,7 @@ def generate_mavlink_class(outf, msgs, xml): } // Finally reorder the fields to match the order map - _.each(t, function(e, i, l) { + t.forEach(function(e, i, l) { args[i] = tempArgs[decoder.order_map[i]] }); }