@@ -32,17 +32,9 @@ def generate_preamble(outf, msgs, args, xml):
3232*/
3333
3434jspack = require("jspack").jspack,
35- _ = require("underscore"),
3635 events = require("events"), // for .emit(..), MAVLink20Processor inherits from events.EventEmitter
3736 util = require("util");
3837
39- var Buffer = require('buffer').Buffer; // required in react - no impact in node
40- var Long = require('long');
41-
42- // Add a convenience method to Buffer
43- Buffer.prototype.toByteArray = function () {
44- return Array.prototype.slice.call(this, 0)
45- }
4638
4739${MAVHEAD} = function(){};
4840
@@ -51,7 +43,7 @@ def generate_preamble(outf, msgs, args, xml):
5143
5244 var bytes = buffer;
5345 var crcOUT = crcIN === undefined ? 0xffff : crcIN;
54- _.each(bytes, function(e) {
46+ bytes.forEach( function(e) {
5547 var tmp = e ^ (crcOUT & 0xff);
5648 tmp = (tmp ^ (tmp << 4)) & 0xff;
5749 crcOUT = (crcOUT >> 8) ^ (tmp << 8) ^ (tmp << 3) ^ (tmp >> 4);
@@ -129,16 +121,15 @@ def generate_preamble(outf, msgs, args, xml):
129121// Convenience setter to facilitate turning the unpacked array of data into member properties
130122${MAVHEAD}.message.prototype.set = function(args,verbose) {
131123 // inspect
132- _.each( this.fieldnames, function(e, i) {
124+ this.fieldnames.forEach( function(e, i) {
133125 var num = parseInt(i,10);
134126 if (this.hasOwnProperty(e) && isNaN(num) ){ // asking for an attribute that's non-numeric is ok unless its already an attribute we have
135127 if ( verbose >= 1) { console.log("WARNING, overwriting an existing property is DANGEROUS:"+e+" ==>"+i+"==>"+args[i]+" -> "+JSON.stringify(this)); }
136128 }
137129 }, this);
138- //console.log(this.fieldnames);
139130
140- // then modify
141- _.each( this.fieldnames, function(e, i) {
131+ // then modify
132+ this.fieldnames.forEach( function(e, i) {
142133 this[e] = args[i];
143134 }, this);
144135};
@@ -164,11 +155,11 @@ def generate_preamble(outf, msgs, args, xml):
164155 // first add the linkid(1 byte) and timestamp(6 bytes) that start the signature
165156 this._msgbuf = this._msgbuf.concat(jspack.Pack('<BIH', [mav.signing.link_id, tlow, thigh ] ) );
166157
167- h.update(mav.signing.secret_key); // secret is already a Buffer
168- h.update(new Buffer .from(this._msgbuf));
158+ h.update(mav.signing.secret_key);
159+ h.update(new Uint8Array .from(this._msgbuf));
169160 var hashDigest = h.digest();
170161 sig = hashDigest.slice(0,6)
171- this._msgbuf = this._msgbuf.concat( ... sig );
162+ this._msgbuf = this._msgbuf.concat( ... sig );
172163
173164 mav.signing.timestamp += 1
174165}
@@ -389,7 +380,7 @@ def generate_mavlink_class(outf, msgs, xml):
389380
390381// MAVLink signing state class
391382MAVLinkSigning = function MAVLinkSigning(object){
392- 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
383+ this.secret_key = new Uint8Array();
393384 this.timestamp = 1
394385 this.link_id = 0
395386 this.sign_outgoing = false // todo false this
@@ -408,8 +399,8 @@ def generate_mavlink_class(outf, msgs, xml):
408399 this.logger = logger;
409400
410401 this.seq = 0;
411- this.buf = new Buffer.from([] );
412- this.bufInError = new Buffer.from([] );
402+ this.buf = new Uint8Array( );
403+ this.bufInError = new Uint8Array( );
413404
414405 this.srcSystem = (typeof srcSystem === 'undefined') ? 0 : srcSystem;
415406 this.srcComponent = (typeof srcComponent === 'undefined') ? 0 : srcComponent;
@@ -465,12 +456,18 @@ def generate_mavlink_class(outf, msgs, xml):
465456 return ( ret <= 0 ) ? 1 : ret;
466457}
467458
459+ // Combine two buffers into one
460+ ${MAVPROCESSOR}.prototype.concat_buffer = function(A, B) {
461+ const out = new Uint8Array(A.length + B.length)
462+ out.set(A, 0)
463+ out.set(B, A.length)
464+ return out
465+ }
466+
468467// add data to the local buffer
469468${MAVPROCESSOR}.prototype.pushBuffer = function(data) {
470- if(data) {
471- this.buf = Buffer.concat([this.buf, data]); // python calls this self.buf.extend(c)
472- this.total_bytes_received += data.length;
473- }
469+ this.buf = this.concat_buffer(this.buf, new Uint8Array([data]));
470+ this.total_bytes_received += 1;
474471}
475472
476473// Decode prefix. Elides the prefix.
@@ -511,6 +508,10 @@ def generate_mavlink_class(outf, msgs, xml):
511508// input some data bytes, possibly returning a new message - python equiv function is called parse_char / __parse_char_legacy
512509${MAVPROCESSOR}.prototype.parseChar = function(c) {
513510
511+ if (c == null) {
512+ return
513+ }
514+
514515 var m = null;
515516
516517 try {
@@ -523,7 +524,7 @@ def generate_mavlink_class(outf, msgs, xml):
523524 this.log('error', e.message);
524525 this.total_receive_errors += 1;
525526 m = new ${MAVHEAD}.messages.bad_data(this.bufInError, e.message);
526- this.bufInError = new Buffer.from([] );
527+ this.bufInError = new Uint8Array( );
527528
528529 }
529530
@@ -575,15 +576,15 @@ def generate_mavlink_class(outf, msgs, xml):
575576
576577// input some data bytes, possibly returning an array of new messages
577578${MAVPROCESSOR}.prototype.parseBuffer = function(s) {
578-
579+
579580 // Get a message, if one is available in the stream.
580581 var m = this.parseChar(s);
581582
582583 // No messages available, bail.
583584 if ( null === m ) {
584585 return null;
585586 }
586-
587+
587588 // While more valid messages can be read from the existing buffer, add
588589 // them to the array of new messages and return them.
589590 var ret = [m];
@@ -598,47 +599,21 @@ def generate_mavlink_class(outf, msgs, xml):
598599
599600}
600601
601- // from Buffer to ArrayBuffer
602- function toArrayBuffer(buf) {
603- var ab = new ArrayBuffer(buf.length);
604- var view = new Uint8Array(ab);
605- for (var i = 0; i < buf.length; ++i) {
606- view[i] = buf[i];
607- }
608- return ab;
609- }
610- // and back
611- function toBuffer(ab) {
612- var buf = Buffer.alloc(ab.byteLength);
613- var view = new Uint8Array(ab);
614- for (var i = 0; i < buf.length; ++i) {
615- buf[i] = view[i];
616- }
617- return buf;
618- }
619-
620602//check signature on incoming message , many of the comments in this file come from the python impl
621- ${MAVPROCESSOR}.prototype.check_signature = function(msgbuf, srcSystem, srcComponent) {
622-
623- //if (isinstance(msgbuf, array.array)){
624- // msgbuf = msgbuf.tostring()
625- //}
626- if ( Buffer.isBuffer(msgbuf) ) {
627- msgbuf = toArrayBuffer(msgbuf);
628- }
603+ ${MAVPROCESSOR}.prototype.check_signature = function(msgbuf, srcSystem, srcComponent) {
629604
630605 //timestamp_buf = msgbuf[-12:-6]
631606 var timestamp_buf= msgbuf.slice(-12,-6);
632607
633608 //link_id = msgbuf[-13]
634- var link_id= new Buffer .from(msgbuf.slice(-13,-12)); // just a single byte really, but returned as a buffer
609+ var link_id = new Uint8Array .from(msgbuf.slice(-13,-12)); // just a single byte really, but returned as a buffer
635610 link_id = link_id[0]; // get the first byte.
636611
637612 //self.mav_sign_unpacker = jspack.Unpack('<IH')
638613 // (tlow, thigh) = self.mav_sign_unpacker.unpack(timestamp_buf)
639614
640- // I means unsigned 4bytes, H means unsigned 2 byte
641- var t = jspack.Unpack('<IH',new Buffer .from(timestamp_buf))
615+ // I means unsigned 4bytes, H means unsigned 2 bytes
616+ var t = jspack.Unpack('<IH',new Uint8Array .from(timestamp_buf))
642617 const [tlow, thigh] = t;
643618
644619 // due to js not being able to shift numbers more than 32, we'll use this instead..
@@ -679,24 +654,24 @@ def generate_mavlink_class(outf, msgs, xml):
679654
680655 // just the last 6 of 13 available are the actual sig . ie excluding the linkid(1) and timestamp(6)
681656 var sigpart = msgbuf.slice(-6);
682- sigpart = new Buffer .from(sigpart);
657+ sigpart = new Uint8Array .from(sigpart);
683658 // not sig part 0- end-minus-6
684659 var notsigpart = msgbuf.slice(0,-6);
685- notsigpart = new Buffer .from(notsigpart);
660+ notsigpart = new Uint8Array .from(notsigpart);
686661
687662 h.update(this.signing.secret_key); // secret is already a Buffer
688663 //var tmp = h.copy().digest();
689664 h.update(notsigpart);
690665 //var tmp2 = h.copy().digest()
691666 var hashDigest = h.digest();
692667 sig1 = hashDigest.slice(0,6)
693-
668+
694669 //sig1 = str(h.digest())[:6]
695670 //sig2 = str(msgbuf)[-6:]
696671
697672 // can't just compare sigs, need a full buffer compare like this...
698673 //if (sig1 != sigpart){
699- if (Buffer .compare(sig1,sigpart)){
674+ if (Uint8Array .compare(sig1,sigpart)){
700675 //console.log('sig mismatch',sig1,sigpart)
701676 return false
702677 }
@@ -719,7 +694,7 @@ def generate_mavlink_class(outf, msgs, xml):
719694 # Mavlink2 only
720695 if (xml .protocol_marker == 253 ):
721696 t .write (outf , """
722- unpacked = jspack.Unpack('cBBBBBBHB ', msgbuf.slice(0, 10)); // the H in here causes msgIDlow to takeup 2 bytes, the rest 1
697+ unpacked = jspack.Unpack('BBBBBBBHB ', msgbuf.slice(0, 10)); // the H in here causes msgIDlow to takeup 2 bytes, the rest 1
723698 magic = unpacked[0];
724699 mlen = unpacked[1];
725700 incompat_flags = unpacked[2];
@@ -775,8 +750,8 @@ def generate_mavlink_class(outf, msgs, xml):
775750 return;
776751 }
777752
778- if (magic.charCodeAt(0) != this.protocol_marker) {
779- throw new Error("Invalid MAVLink prefix ("+magic.charCodeAt(0) +")");
753+ if (magic != this.protocol_marker) {
754+ throw new Error("Invalid MAVLink prefix ("+magic+")");
780755 }
781756
782757 // is packet supposed to be signed?
@@ -804,13 +779,13 @@ def generate_mavlink_class(outf, msgs, xml):
804779
805780 }
806781
807- if( false === _.has( ${MAVHEAD}.map, msgId) ) {
782+ if (!(msgId in ${MAVHEAD}.map) ) {
808783 throw new Error("Unknown MAVLink message ID (" + msgId + ")");
809784 }
810785
811786 // here's the common chunks of packet we want to work with below..
812- var headerBuf= msgbuf.slice(${MAVHEAD}.HEADER_LEN); // first10
813- var sigBuf = msgbuf.slice(-signature_len); // last 13 or nothing
787+ // var headerBuf= msgbuf.slice(${MAVHEAD}.HEADER_LEN); // first10
788+ // var sigBuf = msgbuf.slice(-signature_len); // last 13 or nothing
814789 var crcBuf1 = msgbuf.slice(-2); // either last-2 or last-2-prior-to-signature
815790 var crcBuf2 = msgbuf.slice(-15,-13); // either last-2 or last-2-prior-to-signature
816791 var payloadBuf = msgbuf.slice(${MAVHEAD}.HEADER_LEN, -(signature_len+2)); // the remaining bit between the header and the crc
@@ -890,9 +865,9 @@ def generate_mavlink_class(outf, msgs, xml):
890865 # Mavlink2 only
891866 if (xml .protocol_marker == 253 ):
892867 t .write (outf , """
893- //put any truncated 0's back in (ie zero-pad )
868+ //put any truncated 0's back in (ie zero-pad )
894869 if (paylen > payloadBuf.length) {
895- payloadBuf = Buffer.concat([ payloadBuf, Buffer.alloc (paylen - payloadBuf.length)] );
870+ payloadBuf = this.concat_buffer( payloadBuf, new Uint8Array (paylen - payloadBuf.length).fill(0) );
896871 }
897872""" )
898873
@@ -912,7 +887,7 @@ def generate_mavlink_class(outf, msgs, xml):
912887
913888 if (elementsInMsg == actualElementsInMsg) {
914889 // Reorder the fields to match the order map
915- _.each(t, function(e, i, l) {
890+ t.forEach( function(e, i, l) {
916891 args[i] = t[decoder.order_map[i]]
917892 });
918893 } else {
@@ -956,7 +931,7 @@ def generate_mavlink_class(outf, msgs, xml):
956931 }
957932
958933 // Finally reorder the fields to match the order map
959- _.each(t, function(e, i, l) {
934+ t.forEach( function(e, i, l) {
960935 args[i] = tempArgs[decoder.order_map[i]]
961936 });
962937 }
0 commit comments