@@ -35,17 +35,9 @@ def generate_preamble(outf, msgs, args, xml):
3535*/
3636
3737jspack = require("jspack").jspack,
38- _ = require("underscore"),
3938 events = require("events"), // for .emit(..), MAVLink20Processor inherits from events.EventEmitter
4039 util = require("util");
4140
42- var Buffer = require('buffer').Buffer; // required in react - no impact in node
43- var Long = require('long');
44-
45- // Add a convenience method to Buffer
46- Buffer.prototype.toByteArray = function () {
47- return Array.prototype.slice.call(this, 0)
48- }
4941
5042${MAVHEAD} = function(){};
5143
@@ -54,7 +46,7 @@ def generate_preamble(outf, msgs, args, xml):
5446
5547 var bytes = buffer;
5648 var crcOUT = crcIN === undefined ? 0xffff : crcIN;
57- _.each(bytes, function(e) {
49+ bytes.forEach( function(e) {
5850 var tmp = e ^ (crcOUT & 0xff);
5951 tmp = (tmp ^ (tmp << 4)) & 0xff;
6052 crcOUT = (crcOUT >> 8) ^ (tmp << 8) ^ (tmp << 3) ^ (tmp >> 4);
@@ -132,16 +124,15 @@ def generate_preamble(outf, msgs, args, xml):
132124// Convenience setter to facilitate turning the unpacked array of data into member properties
133125${MAVHEAD}.message.prototype.set = function(args,verbose) {
134126 // inspect
135- _.each( this.fieldnames, function(e, i) {
127+ this.fieldnames.forEach( function(e, i) {
136128 var num = parseInt(i,10);
137129 if (this.hasOwnProperty(e) && isNaN(num) ){ // asking for an attribute that's non-numeric is ok unless its already an attribute we have
138130 if ( verbose >= 1) { console.log("WARNING, overwriting an existing property is DANGEROUS:"+e+" ==>"+i+"==>"+args[i]+" -> "+JSON.stringify(this)); }
139131 }
140132 }, this);
141- //console.log(this.fieldnames);
142133
143- // then modify
144- _.each( this.fieldnames, function(e, i) {
134+ // then modify
135+ this.fieldnames.forEach( function(e, i) {
145136 this[e] = args[i];
146137 }, this);
147138};
@@ -167,11 +158,11 @@ def generate_preamble(outf, msgs, args, xml):
167158 // first add the linkid(1 byte) and timestamp(6 bytes) that start the signature
168159 this._msgbuf = this._msgbuf.concat(jspack.Pack('<BIH', [mav.signing.link_id, tlow, thigh ] ) );
169160
170- h.update(mav.signing.secret_key); // secret is already a Buffer
171- h.update(new Buffer .from(this._msgbuf));
161+ h.update(mav.signing.secret_key);
162+ h.update(new Uint8Array .from(this._msgbuf));
172163 var hashDigest = h.digest();
173164 sig = hashDigest.slice(0,6)
174- this._msgbuf = this._msgbuf.concat( ... sig );
165+ this._msgbuf = this._msgbuf.concat( ... sig );
175166
176167 mav.signing.timestamp += 1
177168}
@@ -392,7 +383,7 @@ def generate_mavlink_class(outf, msgs, xml):
392383
393384// MAVLink signing state class
394385MAVLinkSigning = function MAVLinkSigning(object){
395- 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
386+ this.secret_key = new Uint8Array();
396387 this.timestamp = 1
397388 this.link_id = 0
398389 this.sign_outgoing = false // todo false this
@@ -411,8 +402,8 @@ def generate_mavlink_class(outf, msgs, xml):
411402 this.logger = logger;
412403
413404 this.seq = 0;
414- this.buf = new Buffer.from([] );
415- this.bufInError = new Buffer.from([] );
405+ this.buf = new Uint8Array( );
406+ this.bufInError = new Uint8Array( );
416407
417408 this.srcSystem = (typeof srcSystem === 'undefined') ? 0 : srcSystem;
418409 this.srcComponent = (typeof srcComponent === 'undefined') ? 0 : srcComponent;
@@ -468,12 +459,18 @@ def generate_mavlink_class(outf, msgs, xml):
468459 return ( ret <= 0 ) ? 1 : ret;
469460}
470461
462+ // Combine two buffers into one
463+ ${MAVPROCESSOR}.prototype.concat_buffer = function(A, B) {
464+ const out = new Uint8Array(A.length + B.length)
465+ out.set(A, 0)
466+ out.set(B, A.length)
467+ return out
468+ }
469+
471470// add data to the local buffer
472471${MAVPROCESSOR}.prototype.pushBuffer = function(data) {
473- if(data) {
474- this.buf = Buffer.concat([this.buf, data]); // python calls this self.buf.extend(c)
475- this.total_bytes_received += data.length;
476- }
472+ this.buf = this.concat_buffer(this.buf, new Uint8Array([data]));
473+ this.total_bytes_received += 1;
477474}
478475
479476// Decode prefix. Elides the prefix.
@@ -514,6 +511,10 @@ def generate_mavlink_class(outf, msgs, xml):
514511// input some data bytes, possibly returning a new message - python equiv function is called parse_char / __parse_char_legacy
515512${MAVPROCESSOR}.prototype.parseChar = function(c) {
516513
514+ if (c == null) {
515+ return
516+ }
517+
517518 var m = null;
518519
519520 try {
@@ -526,7 +527,7 @@ def generate_mavlink_class(outf, msgs, xml):
526527 this.log('error', e.message);
527528 this.total_receive_errors += 1;
528529 m = new ${MAVHEAD}.messages.bad_data(this.bufInError, e.message);
529- this.bufInError = new Buffer.from([] );
530+ this.bufInError = new Uint8Array( );
530531
531532 }
532533
@@ -578,15 +579,15 @@ def generate_mavlink_class(outf, msgs, xml):
578579
579580// input some data bytes, possibly returning an array of new messages
580581${MAVPROCESSOR}.prototype.parseBuffer = function(s) {
581-
582+
582583 // Get a message, if one is available in the stream.
583584 var m = this.parseChar(s);
584585
585586 // No messages available, bail.
586587 if ( null === m ) {
587588 return null;
588589 }
589-
590+
590591 // While more valid messages can be read from the existing buffer, add
591592 // them to the array of new messages and return them.
592593 var ret = [m];
@@ -601,47 +602,21 @@ def generate_mavlink_class(outf, msgs, xml):
601602
602603}
603604
604- // from Buffer to ArrayBuffer
605- function toArrayBuffer(buf) {
606- var ab = new ArrayBuffer(buf.length);
607- var view = new Uint8Array(ab);
608- for (var i = 0; i < buf.length; ++i) {
609- view[i] = buf[i];
610- }
611- return ab;
612- }
613- // and back
614- function toBuffer(ab) {
615- var buf = Buffer.alloc(ab.byteLength);
616- var view = new Uint8Array(ab);
617- for (var i = 0; i < buf.length; ++i) {
618- buf[i] = view[i];
619- }
620- return buf;
621- }
622-
623605//check signature on incoming message , many of the comments in this file come from the python impl
624- ${MAVPROCESSOR}.prototype.check_signature = function(msgbuf, srcSystem, srcComponent) {
625-
626- //if (isinstance(msgbuf, array.array)){
627- // msgbuf = msgbuf.tostring()
628- //}
629- if ( Buffer.isBuffer(msgbuf) ) {
630- msgbuf = toArrayBuffer(msgbuf);
631- }
606+ ${MAVPROCESSOR}.prototype.check_signature = function(msgbuf, srcSystem, srcComponent) {
632607
633608 //timestamp_buf = msgbuf[-12:-6]
634609 var timestamp_buf= msgbuf.slice(-12,-6);
635610
636611 //link_id = msgbuf[-13]
637- var link_id= new Buffer .from(msgbuf.slice(-13,-12)); // just a single byte really, but returned as a buffer
612+ var link_id = new Uint8Array .from(msgbuf.slice(-13,-12)); // just a single byte really, but returned as a buffer
638613 link_id = link_id[0]; // get the first byte.
639614
640615 //self.mav_sign_unpacker = jspack.Unpack('<IH')
641616 // (tlow, thigh) = self.mav_sign_unpacker.unpack(timestamp_buf)
642617
643- // I means unsigned 4bytes, H means unsigned 2 byte
644- var t = jspack.Unpack('<IH',new Buffer .from(timestamp_buf))
618+ // I means unsigned 4bytes, H means unsigned 2 bytes
619+ var t = jspack.Unpack('<IH',new Uint8Array .from(timestamp_buf))
645620 const [tlow, thigh] = t;
646621
647622 // due to js not being able to shift numbers more than 32, we'll use this instead..
@@ -682,24 +657,24 @@ def generate_mavlink_class(outf, msgs, xml):
682657
683658 // just the last 6 of 13 available are the actual sig . ie excluding the linkid(1) and timestamp(6)
684659 var sigpart = msgbuf.slice(-6);
685- sigpart = new Buffer .from(sigpart);
660+ sigpart = new Uint8Array .from(sigpart);
686661 // not sig part 0- end-minus-6
687662 var notsigpart = msgbuf.slice(0,-6);
688- notsigpart = new Buffer .from(notsigpart);
663+ notsigpart = new Uint8Array .from(notsigpart);
689664
690665 h.update(this.signing.secret_key); // secret is already a Buffer
691666 //var tmp = h.copy().digest();
692667 h.update(notsigpart);
693668 //var tmp2 = h.copy().digest()
694669 var hashDigest = h.digest();
695670 sig1 = hashDigest.slice(0,6)
696-
671+
697672 //sig1 = str(h.digest())[:6]
698673 //sig2 = str(msgbuf)[-6:]
699674
700675 // can't just compare sigs, need a full buffer compare like this...
701676 //if (sig1 != sigpart){
702- if (Buffer .compare(sig1,sigpart)){
677+ if (Uint8Array .compare(sig1,sigpart)){
703678 //console.log('sig mismatch',sig1,sigpart)
704679 return false
705680 }
@@ -722,7 +697,7 @@ def generate_mavlink_class(outf, msgs, xml):
722697 # Mavlink2 only
723698 if (xml .protocol_marker == 253 ):
724699 t .write (outf , """
725- unpacked = jspack.Unpack('cBBBBBBHB ', msgbuf.slice(0, 10)); // the H in here causes msgIDlow to takeup 2 bytes, the rest 1
700+ unpacked = jspack.Unpack('BBBBBBBHB ', msgbuf.slice(0, 10)); // the H in here causes msgIDlow to takeup 2 bytes, the rest 1
726701 magic = unpacked[0];
727702 mlen = unpacked[1];
728703 incompat_flags = unpacked[2];
@@ -778,8 +753,8 @@ def generate_mavlink_class(outf, msgs, xml):
778753 return;
779754 }
780755
781- if (magic.charCodeAt(0) != this.protocol_marker) {
782- throw new Error("Invalid MAVLink prefix ("+magic.charCodeAt(0) +")");
756+ if (magic != this.protocol_marker) {
757+ throw new Error("Invalid MAVLink prefix ("+magic+")");
783758 }
784759
785760 // is packet supposed to be signed?
@@ -807,13 +782,13 @@ def generate_mavlink_class(outf, msgs, xml):
807782
808783 }
809784
810- if( false === _.has( ${MAVHEAD}.map, msgId) ) {
785+ if (!(msgId in ${MAVHEAD}.map) ) {
811786 throw new Error("Unknown MAVLink message ID (" + msgId + ")");
812787 }
813788
814789 // here's the common chunks of packet we want to work with below..
815- var headerBuf= msgbuf.slice(${MAVHEAD}.HEADER_LEN); // first10
816- var sigBuf = msgbuf.slice(-signature_len); // last 13 or nothing
790+ // var headerBuf= msgbuf.slice(${MAVHEAD}.HEADER_LEN); // first10
791+ // var sigBuf = msgbuf.slice(-signature_len); // last 13 or nothing
817792 var crcBuf1 = msgbuf.slice(-2); // either last-2 or last-2-prior-to-signature
818793 var crcBuf2 = msgbuf.slice(-15,-13); // either last-2 or last-2-prior-to-signature
819794 var payloadBuf = msgbuf.slice(${MAVHEAD}.HEADER_LEN, -(signature_len+2)); // the remaining bit between the header and the crc
@@ -893,9 +868,9 @@ def generate_mavlink_class(outf, msgs, xml):
893868 # Mavlink2 only
894869 if (xml .protocol_marker == 253 ):
895870 t .write (outf , """
896- //put any truncated 0's back in (ie zero-pad )
871+ //put any truncated 0's back in (ie zero-pad )
897872 if (paylen > payloadBuf.length) {
898- payloadBuf = Buffer.concat([ payloadBuf, Buffer.alloc (paylen - payloadBuf.length)] );
873+ payloadBuf = this.concat_buffer( payloadBuf, new Uint8Array (paylen - payloadBuf.length).fill(0) );
899874 }
900875""" )
901876
@@ -915,7 +890,7 @@ def generate_mavlink_class(outf, msgs, xml):
915890
916891 if (elementsInMsg == actualElementsInMsg) {
917892 // Reorder the fields to match the order map
918- _.each(t, function(e, i, l) {
893+ t.forEach( function(e, i, l) {
919894 args[i] = t[decoder.order_map[i]]
920895 });
921896 } else {
@@ -959,7 +934,7 @@ def generate_mavlink_class(outf, msgs, xml):
959934 }
960935
961936 // Finally reorder the fields to match the order map
962- _.each(t, function(e, i, l) {
937+ t.forEach( function(e, i, l) {
963938 args[i] = tempArgs[decoder.order_map[i]]
964939 });
965940 }
0 commit comments