2014-08-09 18:01:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-08-12 13:51:31 +00:00
|
|
|
TABS.cli = {
|
2014-08-10 04:01:44 +00:00
|
|
|
'validateText': "",
|
2015-12-16 12:55:35 +00:00
|
|
|
'currentLine': "",
|
2014-08-10 04:01:44 +00:00
|
|
|
'sequenceElements': 0
|
|
|
|
};
|
2014-06-20 08:29:01 +00:00
|
|
|
|
2014-08-12 14:05:22 +00:00
|
|
|
TABS.cli.initialize = function (callback) {
|
2014-07-10 17:16:53 +00:00
|
|
|
var self = this;
|
2014-10-01 10:58:09 +00:00
|
|
|
|
2014-10-01 11:16:22 +00:00
|
|
|
if (GUI.active_tab != 'cli') {
|
|
|
|
GUI.active_tab = 'cli';
|
|
|
|
}
|
2015-12-16 12:55:35 +00:00
|
|
|
|
2014-08-12 14:05:22 +00:00
|
|
|
$('#content').load("./tabs/cli.html", function () {
|
2014-05-06 14:48:46 +00:00
|
|
|
// translate to user-selected language
|
|
|
|
localize();
|
|
|
|
|
2014-08-10 04:01:44 +00:00
|
|
|
CONFIGURATOR.cliActive = true;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-24 16:06:54 +00:00
|
|
|
var textarea = $('.tab-cli textarea');
|
2014-05-26 12:11:30 +00:00
|
|
|
|
2014-08-10 04:01:44 +00:00
|
|
|
textarea.keypress(function (event) {
|
2014-01-24 16:06:54 +00:00
|
|
|
if (event.which == 13) { // enter
|
2014-05-26 12:11:30 +00:00
|
|
|
event.preventDefault(); // prevent the adding of new line
|
|
|
|
|
|
|
|
var out_string = textarea.val();
|
2014-01-24 16:06:54 +00:00
|
|
|
var out_arr = out_string.split("\n");
|
2014-07-10 17:16:53 +00:00
|
|
|
self.history.add(out_string.trim());
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-05-26 12:11:30 +00:00
|
|
|
var timeout_needle = 0;
|
2014-01-24 16:06:54 +00:00
|
|
|
for (var i = 0; i < out_arr.length; i++) {
|
2014-08-10 04:01:44 +00:00
|
|
|
self.sendSlowly(out_arr, i, timeout_needle++);
|
2014-01-24 16:06:54 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-05-26 12:11:30 +00:00
|
|
|
textarea.val('');
|
2013-04-12 17:17:27 +00:00
|
|
|
}
|
2014-01-24 16:06:54 +00:00
|
|
|
});
|
2013-11-01 12:33:19 +00:00
|
|
|
|
2014-08-12 14:05:22 +00:00
|
|
|
textarea.keyup(function (event) {
|
|
|
|
var keyUp = {38: true},
|
|
|
|
keyDown = {40: true};
|
2013-11-01 12:33:19 +00:00
|
|
|
|
2014-08-12 14:05:22 +00:00
|
|
|
if (event.keyCode in keyUp) {
|
2014-07-10 17:16:53 +00:00
|
|
|
textarea.val(self.history.prev());
|
2014-08-12 14:05:22 +00:00
|
|
|
}
|
2013-11-01 12:33:19 +00:00
|
|
|
|
2014-08-12 14:05:22 +00:00
|
|
|
if (event.keyCode in keyDown) {
|
2014-07-10 17:16:53 +00:00
|
|
|
textarea.val(self.history.next());
|
2014-08-12 14:05:22 +00:00
|
|
|
}
|
2014-01-24 16:06:54 +00:00
|
|
|
});
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-24 16:06:54 +00:00
|
|
|
// give input element user focus
|
2014-05-26 12:11:30 +00:00
|
|
|
textarea.focus();
|
2014-07-10 16:33:30 +00:00
|
|
|
|
2015-12-16 12:55:35 +00:00
|
|
|
GUI.timeout_add('enter_cli', function enter_cli() {
|
|
|
|
// Enter CLI mode
|
|
|
|
var bufferOut = new ArrayBuffer(1);
|
|
|
|
var bufView = new Uint8Array(bufferOut);
|
|
|
|
|
|
|
|
bufView[0] = 0x23; // #
|
|
|
|
|
|
|
|
serial.send(bufferOut);
|
|
|
|
}, 250);
|
|
|
|
|
2015-11-11 21:01:22 +00:00
|
|
|
GUI.content_ready(callback);
|
2013-07-29 06:14:49 +00:00
|
|
|
});
|
2014-07-10 16:33:30 +00:00
|
|
|
};
|
|
|
|
|
2014-08-12 13:51:31 +00:00
|
|
|
TABS.cli.history = {
|
2014-07-10 17:16:53 +00:00
|
|
|
history: [],
|
|
|
|
index: 0
|
|
|
|
};
|
|
|
|
|
2014-08-12 13:51:31 +00:00
|
|
|
TABS.cli.history.add = function (str) {
|
2014-07-10 17:16:53 +00:00
|
|
|
this.history.push(str);
|
|
|
|
this.index = this.history.length;
|
|
|
|
};
|
2014-08-12 13:51:31 +00:00
|
|
|
TABS.cli.history.prev = function () {
|
2014-07-10 17:16:53 +00:00
|
|
|
if (this.index > 0) this.index -= 1;
|
|
|
|
return this.history[this.index];
|
|
|
|
};
|
2014-08-12 13:51:31 +00:00
|
|
|
TABS.cli.history.next = function () {
|
2014-07-10 17:16:53 +00:00
|
|
|
if (this.index < this.history.length) this.index += 1;
|
|
|
|
return this.history[this.index - 1];
|
|
|
|
};
|
|
|
|
|
2014-08-12 13:51:31 +00:00
|
|
|
TABS.cli.sendSlowly = function (out_arr, i, timeout_needle) {
|
2014-08-10 04:01:44 +00:00
|
|
|
GUI.timeout_add('CLI_send_slowly', function () {
|
2014-03-08 05:25:15 +00:00
|
|
|
var bufferOut = new ArrayBuffer(out_arr[i].length + 1);
|
2013-09-05 10:34:02 +00:00
|
|
|
var bufView = new Uint8Array(bufferOut);
|
|
|
|
|
|
|
|
for (var c_key = 0; c_key < out_arr[i].length; c_key++) {
|
|
|
|
bufView[c_key] = out_arr[i].charCodeAt(c_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
bufView[out_arr[i].length] = 0x0D; // enter (\n)
|
|
|
|
|
2014-12-06 14:35:12 +00:00
|
|
|
serial.send(bufferOut);
|
2016-03-12 10:37:28 +00:00
|
|
|
if (out_arr[i].substring(1, 7) == 'profile') {
|
|
|
|
timeout_needle *= 2; // switching profiles needs additional time
|
|
|
|
}
|
|
|
|
}, timeout_needle * 15);
|
2014-08-10 04:01:44 +00:00
|
|
|
};
|
2013-04-13 10:38:06 +00:00
|
|
|
|
2014-08-12 13:51:31 +00:00
|
|
|
TABS.cli.read = function (readInfo) {
|
2014-08-10 04:01:44 +00:00
|
|
|
/* Some info about handling line feeds and carriage return
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-08-10 04:01:44 +00:00
|
|
|
line feed = LF = \n = 0x0A = 10
|
|
|
|
carriage return = CR = \r = 0x0D = 13
|
2013-05-19 17:36:57 +00:00
|
|
|
|
2014-08-10 04:01:44 +00:00
|
|
|
MAC only understands CR
|
|
|
|
Linux and Unix only understand LF
|
|
|
|
Windows understands (both) CRLF
|
|
|
|
Chrome OS currenty unknown
|
|
|
|
*/
|
|
|
|
var data = new Uint8Array(readInfo.data),
|
|
|
|
text = "";
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-22 19:35:37 +00:00
|
|
|
for (var i = 0; i < data.length; i++) {
|
2014-08-10 04:01:44 +00:00
|
|
|
if (CONFIGURATOR.cliValid) {
|
|
|
|
if (data[i] == 27 || this.sequenceElements > 0) { // ESC + other
|
|
|
|
this.sequenceElements++;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-22 19:51:12 +00:00
|
|
|
// delete previous space
|
2014-08-10 04:01:44 +00:00
|
|
|
if (this.sequenceElements == 1) {
|
2014-01-22 19:51:12 +00:00
|
|
|
text = text.substring(0, text.length -1);
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-22 19:51:12 +00:00
|
|
|
// Reset
|
2014-08-10 04:01:44 +00:00
|
|
|
if (this.sequenceElements >= 5) {
|
|
|
|
this.sequenceElements = 0;
|
2014-01-22 19:51:12 +00:00
|
|
|
}
|
2014-01-22 19:35:37 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-08-10 04:01:44 +00:00
|
|
|
if (this.sequenceElements == 0) {
|
2014-01-22 19:51:12 +00:00
|
|
|
switch (data[i]) {
|
|
|
|
case 10: // line feed
|
2014-04-10 21:06:12 +00:00
|
|
|
if (GUI.operating_system != "MacOS") {
|
2014-01-22 19:51:12 +00:00
|
|
|
text += "<br />";
|
|
|
|
}
|
2015-12-16 12:55:35 +00:00
|
|
|
this.currentLine = "";
|
2014-01-22 19:51:12 +00:00
|
|
|
break;
|
|
|
|
case 13: // carriage return
|
|
|
|
if (GUI.operating_system == "MacOS") {
|
|
|
|
text += "<br />";
|
|
|
|
}
|
2015-12-16 12:55:35 +00:00
|
|
|
this.currentLine = "";
|
2014-01-22 19:51:12 +00:00
|
|
|
break;
|
2015-10-23 13:42:32 +00:00
|
|
|
case 60:
|
|
|
|
text += '<';
|
|
|
|
break;
|
|
|
|
case 62:
|
|
|
|
text += '>';
|
|
|
|
break;
|
2015-11-05 17:22:39 +00:00
|
|
|
|
2014-01-22 19:51:12 +00:00
|
|
|
default:
|
|
|
|
text += String.fromCharCode(data[i]);
|
2015-12-16 12:55:35 +00:00
|
|
|
this.currentLine += String.fromCharCode(data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.currentLine == 'Rebooting') {
|
|
|
|
CONFIGURATOR.cliActive = false;
|
|
|
|
CONFIGURATOR.cliValid = false;
|
|
|
|
GUI.log(chrome.i18n.getMessage('cliReboot'));
|
|
|
|
GUI.log(chrome.i18n.getMessage('deviceRebooting'));
|
|
|
|
|
|
|
|
if (BOARD.find_board_definition(CONFIG.boardIdentifier).vcp) { // VCP-based flight controls may crash old drivers, we catch and reconnect
|
|
|
|
$('a.connect').click();
|
|
|
|
GUI.timeout_add('start_connection',function start_connection() {
|
|
|
|
$('a.connect').click();
|
2015-12-17 08:52:10 +00:00
|
|
|
},2500);
|
2015-12-16 12:55:35 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
GUI.timeout_add('waiting_for_bootup', function waiting_for_bootup() {
|
|
|
|
MSP.send_message(MSP_codes.MSP_IDENT, false, false, function () {
|
|
|
|
GUI.log(chrome.i18n.getMessage('deviceReady'));
|
2015-12-23 11:26:51 +00:00
|
|
|
if (!GUI.tab_switch_in_progress) {
|
2015-12-18 12:50:29 +00:00
|
|
|
$('#tabs ul.mode-connected .tab_setup a').click();
|
|
|
|
}
|
2015-12-16 12:55:35 +00:00
|
|
|
});
|
|
|
|
},1500); // 1500 ms seems to be just the right amount of delay to prevent data request timeouts
|
2014-01-22 19:51:12 +00:00
|
|
|
}
|
2014-01-22 19:35:37 +00:00
|
|
|
}
|
2014-01-22 19:51:12 +00:00
|
|
|
} else {
|
|
|
|
// try to catch part of valid CLI enter message
|
2014-08-10 04:01:44 +00:00
|
|
|
this.validateText += String.fromCharCode(data[i]);
|
2015-12-16 12:55:35 +00:00
|
|
|
text += String.fromCharCode(data[i]);
|
2013-05-19 17:36:57 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-08-10 04:01:44 +00:00
|
|
|
if (!CONFIGURATOR.cliValid && this.validateText.indexOf('CLI') != -1) {
|
2015-12-16 12:55:35 +00:00
|
|
|
GUI.log(chrome.i18n.getMessage('cliEnter'));
|
2014-08-10 04:01:44 +00:00
|
|
|
CONFIGURATOR.cliValid = true;
|
|
|
|
this.validateText = "";
|
2014-01-22 19:51:12 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-22 19:35:37 +00:00
|
|
|
$('.tab-cli .window .wrapper').append(text);
|
2014-03-08 05:25:15 +00:00
|
|
|
$('.tab-cli .window').scrollTop($('.tab-cli .window .wrapper').height());
|
2014-08-10 04:01:44 +00:00
|
|
|
};
|
|
|
|
|
2014-08-12 14:05:22 +00:00
|
|
|
TABS.cli.cleanup = function (callback) {
|
2015-12-16 12:55:35 +00:00
|
|
|
if (!CONFIGURATOR.connectionValid || !CONFIGURATOR.cliValid) {
|
2015-01-26 21:38:25 +00:00
|
|
|
if (callback) callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-10 04:01:44 +00:00
|
|
|
var bufferOut = new ArrayBuffer(5);
|
|
|
|
var bufView = new Uint8Array(bufferOut);
|
|
|
|
|
|
|
|
bufView[0] = 0x65; // e
|
|
|
|
bufView[1] = 0x78; // x
|
|
|
|
bufView[2] = 0x69; // i
|
|
|
|
bufView[3] = 0x74; // t
|
|
|
|
bufView[4] = 0x0D; // enter
|
|
|
|
|
2014-08-12 14:05:22 +00:00
|
|
|
serial.send(bufferOut, function (writeInfo) {
|
2014-08-10 04:01:44 +00:00
|
|
|
// we could handle this "nicely", but this will do for now
|
|
|
|
// (another approach is however much more complicated):
|
|
|
|
// we can setup an interval asking for data lets say every 200ms, when data arrives, callback will be triggered and tab switched
|
|
|
|
// we could probably implement this someday
|
2014-10-01 11:16:22 +00:00
|
|
|
GUI.timeout_add('waiting_for_bootup', function waiting_for_bootup() {
|
2014-08-10 04:01:44 +00:00
|
|
|
if (callback) callback();
|
2015-12-16 12:55:35 +00:00
|
|
|
}, 1000); // if we dont allow enough time to reboot, CRC of "first" command sent will fail, keep an eye for this one
|
2016-03-24 13:50:24 +00:00
|
|
|
CONFIGURATOR.cliActive = false;
|
2014-08-10 04:01:44 +00:00
|
|
|
});
|
2015-11-05 17:22:39 +00:00
|
|
|
};
|