2014-08-09 17:38:46 +00:00
|
|
|
'use strict';
|
|
|
|
|
2013-11-09 02:52:06 +00:00
|
|
|
var MSP = {
|
|
|
|
state: 0,
|
2014-08-09 17:38:46 +00:00
|
|
|
message_direction: 1,
|
2013-11-09 02:52:06 +00:00
|
|
|
code: 0,
|
2016-08-06 13:31:10 +00:00
|
|
|
dataView: 0,
|
2013-11-09 02:52:06 +00:00
|
|
|
message_length_expected: 0,
|
|
|
|
message_length_received: 0,
|
2014-08-14 14:41:49 +00:00
|
|
|
message_buffer: null,
|
|
|
|
message_buffer_uint8_view: null,
|
2013-11-09 02:52:06 +00:00
|
|
|
message_checksum: 0,
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-12-05 09:34:10 +00:00
|
|
|
callbacks: [],
|
2014-01-26 17:24:46 +00:00
|
|
|
packet_error: 0,
|
2015-07-04 22:21:14 +00:00
|
|
|
unsupported: 0,
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2016-04-19 23:38:23 +00:00
|
|
|
last_received_timestamp: null,
|
2016-08-06 13:31:10 +00:00
|
|
|
listeners: [],
|
2015-03-03 22:56:48 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
read: function (readInfo) {
|
|
|
|
var data = new Uint8Array(readInfo.data);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
for (var i = 0; i < data.length; i++) {
|
|
|
|
switch (this.state) {
|
|
|
|
case 0: // sync char 1
|
|
|
|
if (data[i] == 36) { // $
|
|
|
|
this.state++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: // sync char 2
|
|
|
|
if (data[i] == 77) { // M
|
|
|
|
this.state++;
|
|
|
|
} else { // restart and try again
|
|
|
|
this.state = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // direction (should be >)
|
2015-07-04 22:21:14 +00:00
|
|
|
this.unsupported = 0;
|
2014-09-21 18:31:57 +00:00
|
|
|
if (data[i] == 62) { // >
|
|
|
|
this.message_direction = 1;
|
2015-07-04 22:21:14 +00:00
|
|
|
} else if (data[i] == 60) { // <
|
2014-09-21 18:31:57 +00:00
|
|
|
this.message_direction = 0;
|
2015-07-04 22:21:14 +00:00
|
|
|
} else if (data[i] == 33) { // !
|
|
|
|
// FC reports unsupported message error
|
|
|
|
this.unsupported = 1;
|
2014-09-21 18:31:57 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
this.state++;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
this.message_length_expected = data[i];
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
this.message_checksum = data[i];
|
2013-11-09 02:52:06 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
// setup arraybuffer
|
|
|
|
this.message_buffer = new ArrayBuffer(this.message_length_expected);
|
|
|
|
this.message_buffer_uint8_view = new Uint8Array(this.message_buffer);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-02 17:36:28 +00:00
|
|
|
this.state++;
|
2014-09-21 18:31:57 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
this.code = data[i];
|
|
|
|
this.message_checksum ^= data[i];
|
|
|
|
|
|
|
|
if (this.message_length_expected > 0) {
|
|
|
|
// process payload
|
|
|
|
this.state++;
|
|
|
|
} else {
|
|
|
|
// no payload
|
|
|
|
this.state += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 5: // payload
|
|
|
|
this.message_buffer_uint8_view[this.message_length_received] = data[i];
|
|
|
|
this.message_checksum ^= data[i];
|
|
|
|
this.message_length_received++;
|
|
|
|
|
|
|
|
if (this.message_length_received >= this.message_length_expected) {
|
|
|
|
this.state++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
if (this.message_checksum == data[i]) {
|
2016-08-06 13:31:10 +00:00
|
|
|
// message received, store dataview
|
|
|
|
this.dataView = new DataView(this.message_buffer, 0, this.message_length_expected);
|
2014-09-21 18:31:57 +00:00
|
|
|
} else {
|
|
|
|
console.log('code: ' + this.code + ' - crc failed');
|
2016-08-06 13:31:10 +00:00
|
|
|
this.dataView = null;
|
2014-09-21 18:31:57 +00:00
|
|
|
this.packet_error++;
|
|
|
|
}
|
|
|
|
// Reset variables
|
|
|
|
this.message_length_received = 0;
|
2014-03-02 17:36:28 +00:00
|
|
|
this.state = 0;
|
2016-08-06 13:31:10 +00:00
|
|
|
this.notify();
|
2014-09-21 18:31:57 +00:00
|
|
|
break;
|
2014-09-21 18:40:33 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
console.log('Unknown state detected: ' + this.state);
|
2014-09-21 18:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-19 23:38:23 +00:00
|
|
|
this.last_received_timestamp = Date.now();
|
2014-09-21 18:31:57 +00:00
|
|
|
},
|
2016-08-06 13:31:10 +00:00
|
|
|
notify: function() {
|
|
|
|
var self = this;
|
|
|
|
this.listeners.forEach(function(listener) {
|
|
|
|
listener(self);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
listen: function(listener) {
|
|
|
|
if (this.listeners[0] == undefined) {
|
|
|
|
this.listeners[0] = listener;
|
2013-11-09 02:52:06 +00:00
|
|
|
}
|
2016-08-06 13:31:10 +00:00
|
|
|
else if (this.listeners.indexOf(listener) == -1) {
|
|
|
|
this.listeners.push(listener);
|
2014-03-02 17:36:28 +00:00
|
|
|
}
|
2014-09-21 18:31:57 +00:00
|
|
|
},
|
|
|
|
send_message: function (code, data, callback_sent, callback_msp) {
|
|
|
|
var bufferOut,
|
|
|
|
bufView;
|
2014-03-02 17:36:28 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
// always reserve 6 bytes for protocol overhead !
|
|
|
|
if (data) {
|
|
|
|
var size = data.length + 6,
|
|
|
|
checksum = 0;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
bufferOut = new ArrayBuffer(size);
|
|
|
|
bufView = new Uint8Array(bufferOut);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
bufView[0] = 36; // $
|
|
|
|
bufView[1] = 77; // M
|
|
|
|
bufView[2] = 60; // <
|
|
|
|
bufView[3] = data.length;
|
|
|
|
bufView[4] = code;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
checksum = bufView[3] ^ bufView[4];
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
for (var i = 0; i < data.length; i++) {
|
|
|
|
bufView[i + 5] = data[i];
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
checksum ^= bufView[i + 5];
|
2014-03-08 05:25:15 +00:00
|
|
|
}
|
2014-05-15 11:12:13 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
bufView[5 + data.length] = checksum;
|
|
|
|
} else {
|
|
|
|
bufferOut = new ArrayBuffer(6);
|
|
|
|
bufView = new Uint8Array(bufferOut);
|
|
|
|
|
|
|
|
bufView[0] = 36; // $
|
|
|
|
bufView[1] = 77; // M
|
|
|
|
bufView[2] = 60; // <
|
|
|
|
bufView[3] = 0; // data length
|
|
|
|
bufView[4] = code; // code
|
|
|
|
bufView[5] = bufView[3] ^ bufView[4]; // checksum
|
2014-03-02 17:36:28 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 09:24:20 +00:00
|
|
|
var obj = {'code': code, 'requestBuffer': bufferOut, 'callback': (callback_msp) ? callback_msp : false, 'timer': false};
|
|
|
|
|
|
|
|
var requestExists = false;
|
2014-09-21 18:31:57 +00:00
|
|
|
for (var i = 0; i < MSP.callbacks.length; i++) {
|
|
|
|
if (MSP.callbacks[i].code == code) {
|
2014-12-05 09:24:20 +00:00
|
|
|
// request already exist, we will just attach
|
|
|
|
requestExists = true;
|
|
|
|
break;
|
2014-05-15 11:12:13 +00:00
|
|
|
}
|
2013-11-09 02:52:06 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-12-05 09:24:20 +00:00
|
|
|
if (!requestExists) {
|
|
|
|
obj.timer = setInterval(function () {
|
|
|
|
console.log('MSP data request timed-out: ' + code);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-12-05 09:24:20 +00:00
|
|
|
serial.send(bufferOut, false);
|
|
|
|
}, 1000); // we should be able to define timeout in the future
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
MSP.callbacks.push(obj);
|
2014-03-02 17:36:28 +00:00
|
|
|
|
2014-12-05 09:24:20 +00:00
|
|
|
// always send messages with data payload (even when there is a message already in the queue)
|
|
|
|
if (data || !requestExists) {
|
|
|
|
serial.send(bufferOut, function (sendInfo) {
|
2015-04-28 08:20:48 +00:00
|
|
|
if (sendInfo.bytesSent == bufferOut.byteLength) {
|
2014-12-05 09:24:20 +00:00
|
|
|
if (callback_sent) callback_sent();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-09-21 18:31:57 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
2016-06-24 09:06:34 +00:00
|
|
|
/**
|
|
|
|
* resolves: {command: code, data: data, length: message_length}
|
|
|
|
*/
|
|
|
|
promise: function(code, data) {
|
|
|
|
var self = this;
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
self.send_message(code, data, false, function(data) {
|
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2014-09-21 18:31:57 +00:00
|
|
|
callbacks_cleanup: function () {
|
|
|
|
for (var i = 0; i < this.callbacks.length; i++) {
|
|
|
|
clearInterval(this.callbacks[i].timer);
|
2014-03-02 17:36:28 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-09-21 18:31:57 +00:00
|
|
|
this.callbacks = [];
|
|
|
|
},
|
|
|
|
disconnect_cleanup: function () {
|
|
|
|
this.state = 0; // reset packet state for "clean" initial entry (this is only required if user hot-disconnects)
|
|
|
|
this.packet_error = 0; // reset CRC packet error counter for next session
|
|
|
|
|
|
|
|
this.callbacks_cleanup();
|
|
|
|
}
|
2016-08-06 13:31:10 +00:00
|
|
|
};
|