@@ -33,17 +33,9 @@ def generate_preamble(outf, msgs, args, xml):
3333*/
3434
3535jspack = require("jspack").jspack,
36- _ = require("underscore"),
3736 events = require("events"), // for .emit(..), MAVLink20Processor inherits from events.EventEmitter
3837 util = require("util");
3938
40- var Buffer = require('buffer').Buffer; // required in react - no impact in node
41- var Long = require('long');
42-
43- // Add a convenience method to Buffer
44- Buffer.prototype.toByteArray = function () {
45- return Array.prototype.slice.call(this, 0)
46- }
4739
4840${MAVHEAD} = function(){};
4941
@@ -52,7 +44,7 @@ def generate_preamble(outf, msgs, args, xml):
5244
5345 var bytes = buffer;
5446 var crcOUT = crcIN === undefined ? 0xffff : crcIN;
55- _.each(bytes, function(e) {
47+ bytes.forEach( function(e) {
5648 var tmp = e ^ (crcOUT & 0xff);
5749 tmp = (tmp ^ (tmp << 4)) & 0xff;
5850 crcOUT = (crcOUT >> 8) ^ (tmp << 8) ^ (tmp << 3) ^ (tmp >> 4);
@@ -130,16 +122,15 @@ def generate_preamble(outf, msgs, args, xml):
130122// Convenience setter to facilitate turning the unpacked array of data into member properties
131123${MAVHEAD}.message.prototype.set = function(args,verbose) {
132124 // inspect
133- _.each( this.fieldnames, function(e, i) {
125+ this.fieldnames.forEach( function(e, i) {
134126 var num = parseInt(i,10);
135127 if (this.hasOwnProperty(e) && isNaN(num) ){ // asking for an attribute that's non-numeric is ok unless its already an attribute we have
136128 if ( verbose >= 1) { console.log("WARNING, overwriting an existing property is DANGEROUS:"+e+" ==>"+i+"==>"+args[i]+" -> "+JSON.stringify(this)); }
137129 }
138130 }, this);
139- //console.log(this.fieldnames);
140131
141- // then modify
142- _.each( this.fieldnames, function(e, i) {
132+ // then modify
133+ this.fieldnames.forEach( function(e, i) {
143134 this[e] = args[i];
144135 }, this);
145136};
@@ -165,11 +156,11 @@ def generate_preamble(outf, msgs, args, xml):
165156 // first add the linkid(1 byte) and timestamp(6 bytes) that start the signature
166157 this._msgbuf = this._msgbuf.concat(jspack.Pack('<BIH', [mav.signing.link_id, tlow, thigh ] ) );
167158
168- h.update(mav.signing.secret_key); // secret is already a Buffer
169- h.update(new Buffer .from(this._msgbuf));
159+ h.update(mav.signing.secret_key);
160+ h.update(new Uint8Array .from(this._msgbuf));
170161 var hashDigest = h.digest();
171162 sig = hashDigest.slice(0,6)
172- this._msgbuf = this._msgbuf.concat( ... sig );
163+ this._msgbuf = this._msgbuf.concat( ... sig );
173164
174165 mav.signing.timestamp += 1
175166}
@@ -390,7 +381,7 @@ def generate_mavlink_class(outf, msgs, xml):
390381
391382// MAVLink signing state class
392383MAVLinkSigning = function MAVLinkSigning(object){
393- 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
384+ this.secret_key = new Uint8Array();
394385 this.timestamp = 1
395386 this.link_id = 0
396387 this.sign_outgoing = false // todo false this
@@ -409,8 +400,8 @@ def generate_mavlink_class(outf, msgs, xml):
409400 this.logger = logger;
410401
411402 this.seq = 0;
412- this.buf = new Buffer.from([] );
413- this.bufInError = new Buffer.from([] );
403+ this.buf = new Uint8Array( );
404+ this.bufInError = new Uint8Array( );
414405
415406 this.srcSystem = (typeof srcSystem === 'undefined') ? 0 : srcSystem;
416407 this.srcComponent = (typeof srcComponent === 'undefined') ? 0 : srcComponent;
@@ -466,12 +457,18 @@ def generate_mavlink_class(outf, msgs, xml):
466457 return ( ret <= 0 ) ? 1 : ret;
467458}
468459
460+ // Combine two buffers into one
461+ ${MAVPROCESSOR}.prototype.concat_buffer = function(A, B) {
462+ const out = new Uint8Array(A.length + B.length)
463+ out.set(A, 0)
464+ out.set(B, A.length)
465+ return out
466+ }
467+
469468// add data to the local buffer
470469${MAVPROCESSOR}.prototype.pushBuffer = function(data) {
471- if(data) {
472- this.buf = Buffer.concat([this.buf, data]); // python calls this self.buf.extend(c)
473- this.total_bytes_received += data.length;
474- }
470+ this.buf = this.concat_buffer(this.buf, new Uint8Array([data]));
471+ this.total_bytes_received += 1;
475472}
476473
477474// Decode prefix. Elides the prefix.
@@ -512,6 +509,10 @@ def generate_mavlink_class(outf, msgs, xml):
512509// input some data bytes, possibly returning a new message - python equiv function is called parse_char / __parse_char_legacy
513510${MAVPROCESSOR}.prototype.parseChar = function(c) {
514511
512+ if (c == null) {
513+ return
514+ }
515+
515516 var m = null;
516517
517518 try {
@@ -524,7 +525,7 @@ def generate_mavlink_class(outf, msgs, xml):
524525 this.log('error', e.message);
525526 this.total_receive_errors += 1;
526527 m = new ${MAVHEAD}.messages.bad_data(this.bufInError, e.message);
527- this.bufInError = new Buffer.from([] );
528+ this.bufInError = new Uint8Array( );
528529
529530 }
530531
@@ -576,15 +577,15 @@ def generate_mavlink_class(outf, msgs, xml):
576577
577578// input some data bytes, possibly returning an array of new messages
578579${MAVPROCESSOR}.prototype.parseBuffer = function(s) {
579-
580+
580581 // Get a message, if one is available in the stream.
581582 var m = this.parseChar(s);
582583
583584 // No messages available, bail.
584585 if ( null === m ) {
585586 return null;
586587 }
587-
588+
588589 // While more valid messages can be read from the existing buffer, add
589590 // them to the array of new messages and return them.
590591 var ret = [m];
@@ -599,47 +600,21 @@ def generate_mavlink_class(outf, msgs, xml):
599600
600601}
601602
602- // from Buffer to ArrayBuffer
603- function toArrayBuffer(buf) {
604- var ab = new ArrayBuffer(buf.length);
605- var view = new Uint8Array(ab);
606- for (var i = 0; i < buf.length; ++i) {
607- view[i] = buf[i];
608- }
609- return ab;
610- }
611- // and back
612- function toBuffer(ab) {
613- var buf = Buffer.alloc(ab.byteLength);
614- var view = new Uint8Array(ab);
615- for (var i = 0; i < buf.length; ++i) {
616- buf[i] = view[i];
617- }
618- return buf;
619- }
620-
621603//check signature on incoming message , many of the comments in this file come from the python impl
622- ${MAVPROCESSOR}.prototype.check_signature = function(msgbuf, srcSystem, srcComponent) {
623-
624- //if (isinstance(msgbuf, array.array)){
625- // msgbuf = msgbuf.tostring()
626- //}
627- if ( Buffer.isBuffer(msgbuf) ) {
628- msgbuf = toArrayBuffer(msgbuf);
629- }
604+ ${MAVPROCESSOR}.prototype.check_signature = function(msgbuf, srcSystem, srcComponent) {
630605
631606 //timestamp_buf = msgbuf[-12:-6]
632607 var timestamp_buf= msgbuf.slice(-12,-6);
633608
634609 //link_id = msgbuf[-13]
635- var link_id= new Buffer .from(msgbuf.slice(-13,-12)); // just a single byte really, but returned as a buffer
610+ var link_id = new Uint8Array .from(msgbuf.slice(-13,-12)); // just a single byte really, but returned as a buffer
636611 link_id = link_id[0]; // get the first byte.
637612
638613 //self.mav_sign_unpacker = jspack.Unpack('<IH')
639614 // (tlow, thigh) = self.mav_sign_unpacker.unpack(timestamp_buf)
640615
641- // I means unsigned 4bytes, H means unsigned 2 byte
642- var t = jspack.Unpack('<IH',new Buffer .from(timestamp_buf))
616+ // I means unsigned 4bytes, H means unsigned 2 bytes
617+ var t = jspack.Unpack('<IH',new Uint8Array .from(timestamp_buf))
643618 const [tlow, thigh] = t;
644619
645620 // due to js not being able to shift numbers more than 32, we'll use this instead..
@@ -680,24 +655,24 @@ def generate_mavlink_class(outf, msgs, xml):
680655
681656 // just the last 6 of 13 available are the actual sig . ie excluding the linkid(1) and timestamp(6)
682657 var sigpart = msgbuf.slice(-6);
683- sigpart = new Buffer .from(sigpart);
658+ sigpart = new Uint8Array .from(sigpart);
684659 // not sig part 0- end-minus-6
685660 var notsigpart = msgbuf.slice(0,-6);
686- notsigpart = new Buffer .from(notsigpart);
661+ notsigpart = new Uint8Array .from(notsigpart);
687662
688663 h.update(this.signing.secret_key); // secret is already a Buffer
689664 //var tmp = h.copy().digest();
690665 h.update(notsigpart);
691666 //var tmp2 = h.copy().digest()
692667 var hashDigest = h.digest();
693668 sig1 = hashDigest.slice(0,6)
694-
669+
695670 //sig1 = str(h.digest())[:6]
696671 //sig2 = str(msgbuf)[-6:]
697672
698673 // can't just compare sigs, need a full buffer compare like this...
699674 //if (sig1 != sigpart){
700- if (Buffer .compare(sig1,sigpart)){
675+ if (Uint8Array .compare(sig1,sigpart)){
701676 //console.log('sig mismatch',sig1,sigpart)
702677 return false
703678 }
@@ -720,7 +695,7 @@ def generate_mavlink_class(outf, msgs, xml):
720695 # Mavlink2 only
721696 if (xml .protocol_marker == 253 ):
722697 t .write (outf , """
723- unpacked = jspack.Unpack('cBBBBBBHB ', msgbuf.slice(0, 10)); // the H in here causes msgIDlow to takeup 2 bytes, the rest 1
698+ unpacked = jspack.Unpack('BBBBBBBHB ', msgbuf.slice(0, 10)); // the H in here causes msgIDlow to takeup 2 bytes, the rest 1
724699 magic = unpacked[0];
725700 mlen = unpacked[1];
726701 incompat_flags = unpacked[2];
@@ -776,8 +751,8 @@ def generate_mavlink_class(outf, msgs, xml):
776751 return;
777752 }
778753
779- if (magic.charCodeAt(0) != this.protocol_marker) {
780- throw new Error("Invalid MAVLink prefix ("+magic.charCodeAt(0) +")");
754+ if (magic != this.protocol_marker) {
755+ throw new Error("Invalid MAVLink prefix ("+magic+")");
781756 }
782757
783758 // is packet supposed to be signed?
@@ -805,13 +780,13 @@ def generate_mavlink_class(outf, msgs, xml):
805780
806781 }
807782
808- if( false === _.has( ${MAVHEAD}.map, msgId) ) {
783+ if (!(msgId in ${MAVHEAD}.map) ) {
809784 throw new Error("Unknown MAVLink message ID (" + msgId + ")");
810785 }
811786
812787 // here's the common chunks of packet we want to work with below..
813- var headerBuf= msgbuf.slice(${MAVHEAD}.HEADER_LEN); // first10
814- var sigBuf = msgbuf.slice(-signature_len); // last 13 or nothing
788+ // var headerBuf= msgbuf.slice(${MAVHEAD}.HEADER_LEN); // first10
789+ // var sigBuf = msgbuf.slice(-signature_len); // last 13 or nothing
815790 var crcBuf1 = msgbuf.slice(-2); // either last-2 or last-2-prior-to-signature
816791 var crcBuf2 = msgbuf.slice(-15,-13); // either last-2 or last-2-prior-to-signature
817792 var payloadBuf = msgbuf.slice(${MAVHEAD}.HEADER_LEN, -(signature_len+2)); // the remaining bit between the header and the crc
@@ -891,9 +866,9 @@ def generate_mavlink_class(outf, msgs, xml):
891866 # Mavlink2 only
892867 if (xml .protocol_marker == 253 ):
893868 t .write (outf , """
894- //put any truncated 0's back in (ie zero-pad )
869+ //put any truncated 0's back in (ie zero-pad )
895870 if (paylen > payloadBuf.length) {
896- payloadBuf = Buffer.concat([ payloadBuf, Buffer.alloc (paylen - payloadBuf.length)] );
871+ payloadBuf = this.concat_buffer( payloadBuf, new Uint8Array (paylen - payloadBuf.length).fill(0) );
897872 }
898873""" )
899874
@@ -913,7 +888,7 @@ def generate_mavlink_class(outf, msgs, xml):
913888
914889 if (elementsInMsg == actualElementsInMsg) {
915890 // Reorder the fields to match the order map
916- _.each(t, function(e, i, l) {
891+ t.forEach( function(e, i, l) {
917892 args[i] = t[decoder.order_map[i]]
918893 });
919894 } else {
@@ -957,7 +932,7 @@ def generate_mavlink_class(outf, msgs, xml):
957932 }
958933
959934 // Finally reorder the fields to match the order map
960- _.each(t, function(e, i, l) {
935+ t.forEach( function(e, i, l) {
961936 args[i] = tempArgs[decoder.order_map[i]]
962937 });
963938 }
0 commit comments