1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-19 14:25:14 +03:00

Extract out virtual serial (#3998)

* Extract old virtual serial for compatability

* Update src/js/virtualSerial.js

Co-authored-by: Mark Haslinghuis <mark@numloq.nl>

---------

Co-authored-by: nerdCopter <56646290+nerdCopter@users.noreply.github.com>
Co-authored-by: Mark Haslinghuis <mark@numloq.nl>
This commit is contained in:
Tomas Chmelevskij 2024-07-10 21:21:49 +02:00 committed by GitHub
parent dc1e86262e
commit 3cffa09ae3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 73 additions and 689 deletions

View file

@ -1,131 +1,19 @@
import GUI from './gui.js';
import { isWeb } from "./utils/isWeb";
/**
* Encapsulates the Clipboard logic, depending on web or nw
*
*/
const Clipboard = {
_nwClipboard: null,
available : null,
readAvailable : null,
writeAvailable : null,
writeText : null,
readText : null,
};
Clipboard._configureClipboardAsNwJs = function(nwGui) {
console.log('NW GUI Clipboard available');
this.available = true;
this.readAvailable = true;
this.writeAvailable = true;
this._nwClipboard = nwGui.Clipboard.get();
this.writeText = function(text, onSuccess, onError) {
try {
this._nwClipboard.set(text, "text");
} catch (err) {
if (onError) {
onError(err);
// naming BFClipboard to avoid conflict with Clipboard API
class BFClipboard {
constructor() {
this.writeText = null;
this.readText = null;
}
writeText(text, onSuccess, onError) {
navigator.clipboard
.writeText(text)
.then(() => onSuccess?.(text), onError);
}
readText(onSuccess, onError) {
navigator.clipboard
.readText()
.then((text) => onSuccess?.(text), onError);
}
}
if (onSuccess) {
onSuccess(text);
}
};
this.readText = function(onSuccess, onError) {
let text = '';
try {
text = this._nwClipboard.get("text");
} catch (err) {
if (onError) {
onError(err);
}
}
if (onSuccess) {
onSuccess(text);
}
};
};
Clipboard._configureClipboardAsCordova = function() {
console.log('Cordova Clipboard available');
this.available = true;
this.readAvailable = true;
this.writeAvailable = true;
this.writeText = function(text, onSuccess, onError) {
cordova.plugins.clipboard.copy(text, onSuccess, onError);
};
this.readText = function(onSuccess, onError) {
cordova.plugins.clipboard.paste(onSuccess, onError);
};
};
Clipboard._configureClipboardAsWeb = function() {
console.log('Web Clipboard available');
this.available = true;
this.readAvailable = true;
this.writeAvailable = true;
this.writeText = function(text, onSuccess, onError) {
navigator.clipboard.writeText(text).then(
() => onSuccess?.(text),
onError,
);
};
this.readText = function(onSuccess, onError) {
navigator.clipboard.readText().then(
(text) => onSuccess?.(text),
onError,
);
};
};
Clipboard._configureClipboardAsOther = function() {
console.warn('NO Clipboard available');
this.available = false;
this.readAvailable = false;
this.writeAvailable = false;
this.writeText = function(text, onSuccess, onError) {
onError('Clipboard not available');
};
this.readText = function(onSuccess, onError) {
onError('Clipboard not available');
};
};
if (GUI.isNWJS()){
Clipboard._configureClipboardAsNwJs(GUI.nwGui);
} else if (GUI.isCordova()) {
Clipboard._configureClipboardAsCordova();
} else if (isWeb()) {
Clipboard._configureClipboardAsWeb();
} else {
Clipboard._configureClipboardAsOther();
}
export default Clipboard;
export default new BFClipboard();

View file

@ -8,11 +8,10 @@ import './msp/MSPHelper.js';
import { i18n } from './localization.js';
import GUI, { TABS } from './gui.js';
import { get as getConfig, set as setConfig } from './ConfigStorage.js';
import { tracking, checkSetupAnalytics } from './Analytics.js';
import { checkSetupAnalytics } from './Analytics.js';
import { initializeSerialBackend } from './serial_backend.js';
import FC from './fc.js';
import CONFIGURATOR from './data_storage.js';
import serial from './serial.js';
import CliAutoComplete from './CliAutoComplete.js';
import DarkTheme, { setDarkTheme } from './DarkTheme.js';
import UI_PHONES from './phones_ui.js';
@ -110,75 +109,6 @@ function appReady() {
});
}
function closeSerial() {
// automatically close the port when application closes
const connectionId = serial.connectionId;
if (connectionId && CONFIGURATOR.connectionValid && !CONFIGURATOR.virtualMode) {
// code below is handmade MSP message (without pretty JS wrapper), it behaves exactly like MSP.send_message
// sending exit command just in case the cli tab was open.
// reset motors to default (mincommand)
let bufferOut = new ArrayBuffer(5),
bufView = new Uint8Array(bufferOut);
bufView[0] = 0x65; // e
bufView[1] = 0x78; // x
bufView[2] = 0x69; // i
bufView[3] = 0x74; // t
bufView[4] = 0x0D; // enter
const sendFn = (serial.connectionType === 'serial' ? chrome.serial.send : chrome.sockets.tcp.send);
sendFn(connectionId, bufferOut, function () {
console.log('Send exit');
});
setTimeout(function() {
bufferOut = new ArrayBuffer(22);
bufView = new Uint8Array(bufferOut);
let checksum = 0;
bufView[0] = 36; // $
bufView[1] = 77; // M
bufView[2] = 60; // <
bufView[3] = 16; // data length
bufView[4] = 214; // MSP_SET_MOTOR
checksum = bufView[3] ^ bufView[4];
for (let i = 0; i < 16; i += 2) {
bufView[i + 5] = FC.MOTOR_CONFIG.mincommand & 0x00FF;
bufView[i + 6] = FC.MOTOR_CONFIG.mincommand >> 8;
checksum ^= bufView[i + 5];
checksum ^= bufView[i + 6];
}
bufView[5 + 16] = checksum;
sendFn(connectionId, bufferOut, function () {
serial.disconnect();
});
}, 100);
} else if (connectionId) {
serial.disconnect();
}
}
function closeHandler() {
if (!GUI.isCordova()) {
this.hide();
}
tracking.sendEvent(tracking.EVENT_CATEGORIES.APPLICATION, 'AppClose', { sessionControl: 'end' });
closeSerial();
if (!GUI.isCordova()) {
this.close(true);
}
}
//Process to execute to real start the app
function startProcess() {
// translate to user-selected language
@ -256,8 +186,6 @@ function startProcess() {
if (GUI.allowedTabs.indexOf(tab) < 0 && tab === "firmware_flasher") {
if (GUI.connected_to || GUI.connecting_to) {
$('a.connect').click();
} else {
serial.disconnect();
}
$('div.open_firmware_flasher a.flash').click();
} else if (GUI.allowedTabs.indexOf(tab) < 0) {

View file

@ -4,11 +4,12 @@ import { i18n } from "../localization";
import GUI from "../gui";
import MSP from "../msp";
import FC from "../fc";
import { serialShim } from "../serial_shim";
import MSPCodes from "./MSPCodes";
import CONFIGURATOR from "../data_storage";
import serial from "../webSerial";
import { gui_log } from "../gui_log";
const serial = serialShim();
/**
* This seems to be mainly used in firmware flasher parts.
*/

View file

@ -1,4 +1,6 @@
import serial from "./serial";
import { serialShim } from "./serial_shim";
const serial = serialShim();
const PortUsage = {
previous_received: 0,

View file

@ -1,469 +0,0 @@
import GUI from "./gui";
import { i18n } from "./localization";
import FC from "./fc";
import CONFIGURATOR from "./data_storage";
import { gui_log } from "./gui_log";
import inflection from "inflection";
import PortHandler from "./port_handler";
import { checkChromeRuntimeError } from "./utils/common";
import { serialDevices } from './serial_devices';
import $ from 'jquery';
const serial = {
connected: false,
connectionId: false,
openCanceled: false,
bitrate: 0,
bytesReceived: 0,
bytesSent: 0,
failed: 0,
connectionType: 'serial', // 'serial' or 'tcp' or 'virtual'
connectionIP: '127.0.0.1',
connectionPort: 5761,
transmitting: false,
outputBuffer: [],
serialDevices,
connect: function (path, options, callback) {
const self = this;
const testUrl = path.match(/^tcp:\/\/([A-Za-z0-9\.-]+)(?:\:(\d+))?$/);
if (testUrl) {
self.connectTcp(testUrl[1], testUrl[2], options, callback);
} else if (path === 'virtual') {
self.connectVirtual(callback);
} else {
self.connectSerial(path, options, callback);
}
},
connectSerial: function (path, options, callback) {
const self = this;
self.connectionType = 'serial';
chrome.serial.connect(path, options, function (connectionInfo) {
self.failed = checkChromeRuntimeError();
if (connectionInfo && !self.openCanceled && !self.failed) {
self.connected = true;
self.connectionId = connectionInfo.connectionId;
self.bitrate = connectionInfo.bitrate;
self.bytesReceived = 0;
self.bytesSent = 0;
self.failed = 0;
self.onReceive.addListener(function log_bytesReceived(info) {
self.bytesReceived += info.data.byteLength;
});
self.onReceiveError.addListener(function watch_for_on_receive_errors(info) {
switch (info.error) {
case 'system_error': // we might be able to recover from this one
if (!self.failed++) {
chrome.serial.setPaused(self.connectionId, false, function () {
self.getInfo(function (getInfo) {
checkChromeRuntimeError();
if (getInfo) {
if (!getInfo.paused) {
console.log(`${self.connectionType}: connection recovered from last onReceiveError`);
self.failed = 0;
} else {
console.log(`${self.connectionType}: connection did not recover from last onReceiveError, disconnecting`);
gui_log(i18n.getMessage('serialUnrecoverable'));
self.errorHandler(getInfo.error, 'receive');
}
}
});
});
}
break;
case 'overrun':
// wait 50 ms and attempt recovery
self.error = info.error;
setTimeout(function() {
chrome.serial.setPaused(info.connectionId, false, function() {
checkChromeRuntimeError();
self.getInfo(function (getInfo) {
if (getInfo) {
if (getInfo.paused) {
// assume unrecoverable, disconnect
console.log(`${self.connectionType}: connection did not recover from ${self.error} condition, disconnecting`);
gui_log(i18n.getMessage('serialUnrecoverable'));
self.errorHandler(getInfo.error, 'receive');
}
else {
console.log(`${self.connectionType}: connection recovered from ${self.error} condition`);
}
}
});
});
}, 50);
break;
case 'timeout':
// No data has been received for receiveTimeout milliseconds.
// We will do nothing.
break;
case 'frame_error':
case 'parity_error':
gui_log(i18n.getMessage(`serialError${inflection.camelize(info.error)}`));
self.errorHandler(info.error, 'receive');
break;
case 'break': // This seems to be the error that is thrown under NW.js in Windows when the device reboots after typing 'exit' in CLI
case 'disconnected':
case 'device_lost':
default:
self.errorHandler(info.error, 'receive');
break;
}
});
console.log(`${self.connectionType}: connection opened with ID: ${connectionInfo.connectionId} , Baud: ${connectionInfo.bitrate}`);
if (callback) {
callback(connectionInfo);
}
} else {
if (connectionInfo && self.openCanceled) {
// connection opened, but this connect sequence was canceled
// we will disconnect without triggering any callbacks
self.connectionId = connectionInfo.connectionId;
console.log(`${self.connectionType}: connection opened with ID: ${connectionInfo.connectionId} , but request was canceled, disconnecting`);
// some bluetooth dongles/dongle drivers really doesn't like to be closed instantly, adding a small delay
setTimeout(function initialization() {
self.openCanceled = false;
self.disconnect(function resetUI() {
console.log(`${self.connectionType}: connect sequence was cancelled, disconnecting...`);
});
}, 150);
} else if (self.openCanceled) {
// connection didn't open and sequence was canceled, so we will do nothing
console.log(`${self.connectionType}: connection didn\'t open and request was canceled`);
self.openCanceled = false;
} else {
console.log(`${self.connectionType}: failed to open serial port`);
}
if (callback) {
callback(false);
}
}
});
},
connectTcp: function (ip, port, options, callback) {
const self = this;
self.connectionIP = ip;
self.connectionPort = port || 5761;
self.connectionPort = parseInt(self.connectionPort);
self.connectionType = 'tcp';
chrome.sockets.tcp.create({
persistent: false,
name: 'Betaflight',
bufferSize: 65535,
}, function(createInfo) {
if (createInfo && !self.openCanceled || !checkChromeRuntimeError()) {
self.connectionId = createInfo.socketId;
self.bitrate = 115200; // fake
self.bytesReceived = 0;
self.bytesSent = 0;
self.failed = 0;
chrome.sockets.tcp.connect(createInfo.socketId, self.connectionIP, self.connectionPort, function (result) {
if (result === 0 || !checkChromeRuntimeError()) {
chrome.sockets.tcp.setNoDelay(createInfo.socketId, true, function (noDelayResult) {
if (noDelayResult === 0 || !checkChromeRuntimeError()) {
self.onReceive.addListener(function log_bytesReceived(info) {
self.bytesReceived += info.data.byteLength;
});
self.onReceiveError.addListener(function watch_for_on_receive_errors(info) {
if (info.socketId !== self.connectionId) return;
if (self.connectionType === 'tcp' && info.resultCode < 0) {
self.errorHandler(info.resultCode, 'receive');
}
});
self.connected = true;
console.log(`${self.connectionType}: connection opened with ID ${createInfo.socketId} , url: ${self.connectionIP}:${self.connectionPort}`);
if (callback) {
callback(createInfo);
}
}
});
} else {
console.log(`${self.connectionType}: failed to connect with result ${result}`);
if (callback) {
callback(false);
}
}
});
}
});
},
connectVirtual: function (callback) {
const self = this;
self.connectionType = 'virtual';
if (!self.openCanceled) {
self.connected = true;
self.connectionId = 'virtual';
self.bitrate = 115200;
self.bytesReceived = 0;
self.bytesSent = 0;
self.failed = 0;
callback();
}
},
disconnect: function (callback) {
const self = this;
const id = self.connectionId;
self.connected = false;
self.emptyOutputBuffer();
if (self.connectionId) {
// remove listeners
for (let i = (self.onReceive.listeners.length - 1); i >= 0; i--) {
self.onReceive.removeListener(self.onReceive.listeners[i]);
}
for (let i = (self.onReceiveError.listeners.length - 1); i >= 0; i--) {
self.onReceiveError.removeListener(self.onReceiveError.listeners[i]);
}
let status = true;
if (self.connectionType !== 'virtual') {
if (self.connectionType === 'tcp') {
chrome.sockets.tcp.disconnect(self.connectionId, function () {
checkChromeRuntimeError();
console.log(`${self.connectionType}: disconnecting socket.`);
});
}
const disconnectFn = (self.connectionType === 'serial') ? chrome.serial.disconnect : chrome.sockets.tcp.close;
disconnectFn(self.connectionId, function (result) {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
}
result = result || self.connectionType === 'tcp';
console.log(`${self.connectionType}: ${result ? 'closed' : 'failed to close'} connection with ID: ${id}, Sent: ${self.bytesSent} bytes, Received: ${self.bytesReceived} bytes`);
status = result;
});
} else {
CONFIGURATOR.virtualMode = false;
self.connectionType = false;
}
self.connectionId = false;
self.bitrate = 0;
if (callback) {
callback(status);
}
} else {
// connection wasn't opened, so we won't try to close anything
// instead we will rise canceled flag which will prevent connect from continueing further after being canceled
self.openCanceled = true;
}
},
getDevices: function (callback) {
const self = this;
chrome.serial.getDevices(function (devices_array) {
const devices = [];
devices_array.forEach(function (device) {
const isKnownSerialDevice = self.serialDevices.some(el => el.vendorId === device.vendorId) && self.serialDevices.some(el => el.productId === device.productId);
if (isKnownSerialDevice || PortHandler.showAllSerialDevices) {
devices.push({
path: device.path,
displayName: device.displayName,
vendorId: device.vendorId,
productId: device.productId,
});
}
});
callback(devices);
});
},
getInfo: function (callback) {
const chromeType = (this.connectionType === 'serial') ? chrome.serial : chrome.sockets.tcp;
chromeType.getInfo(this.connectionId, callback);
},
send: function (data, callback) {
const self = this;
self.outputBuffer.push({'data': data, 'callback': callback});
function _send() {
// store inside separate variables in case array gets destroyed
const _data = self.outputBuffer[0].data;
const _callback = self.outputBuffer[0].callback;
if (!self.connected) {
console.log(`${self.connectionType}: attempting to send when disconnected. ID: ${self.connectionId}`);
if (_callback) {
_callback({
bytesSent: 0,
error: 'undefined',
});
}
return;
}
const sendFn = (self.connectionType === 'serial') ? chrome.serial.send : chrome.sockets.tcp.send;
sendFn(self.connectionId, _data, function (sendInfo) {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
}
if (sendInfo === undefined) {
console.log('undefined send error');
if (_callback) {
_callback({
bytesSent: 0,
error: 'undefined',
});
}
return;
}
if (self.connectionType === 'tcp' && sendInfo.resultCode < 0) {
self.errorHandler(sendInfo.resultCode, 'send');
return;
}
// track sent bytes for statistics
self.bytesSent += sendInfo.bytesSent;
// fire callback
if (_callback) {
_callback(sendInfo);
}
// remove data for current transmission from the buffer
self.outputBuffer.shift();
// if there is any data in the queue fire send immediately, otherwise stop trasmitting
if (self.outputBuffer.length) {
// keep the buffer withing reasonable limits
if (self.outputBuffer.length > 100) {
let counter = 0;
while (self.outputBuffer.length > 100) {
self.outputBuffer.pop();
counter++;
}
console.log(`${self.connectionType}: send buffer overflowing, dropped: ${counter}`);
}
_send();
} else {
self.transmitting = false;
}
});
}
if (!self.transmitting && self.connected) {
self.transmitting = true;
_send();
}
},
onReceive: {
listeners: [],
addListener: function (function_reference) {
const chromeType = (serial.connectionType === 'serial') ? chrome.serial : chrome.sockets.tcp;
chromeType.onReceive.addListener(function_reference);
this.listeners.push(function_reference);
},
removeListener: function (function_reference) {
const chromeType = (serial.connectionType === 'serial') ? chrome.serial : chrome.sockets.tcp;
for (let i = (this.listeners.length - 1); i >= 0; i--) {
if (this.listeners[i] == function_reference) {
chromeType.onReceive.removeListener(function_reference);
this.listeners.splice(i, 1);
break;
}
}
},
},
onReceiveError: {
listeners: [],
addListener: function (function_reference) {
const chromeType = (serial.connectionType === 'serial') ? chrome.serial : chrome.sockets.tcp;
chromeType.onReceiveError.addListener(function_reference);
this.listeners.push(function_reference);
},
removeListener: function (function_reference) {
const chromeType = (serial.connectionType === 'serial') ? chrome.serial : chrome.sockets.tcp;
for (let i = (this.listeners.length - 1); i >= 0; i--) {
if (this.listeners[i] == function_reference) {
chromeType.onReceiveError.removeListener(function_reference);
this.listeners.splice(i, 1);
break;
}
}
},
},
emptyOutputBuffer: function () {
this.outputBuffer = [];
this.transmitting = false;
},
errorHandler: function (result, direction) {
const self = this;
self.connected = false;
FC.CONFIG.armingDisabled = false;
FC.CONFIG.runawayTakeoffPreventionDisabled = false;
let message;
if (self.connectionType === 'tcp') {
switch (result){
case -15:
// connection is lost, cannot write to it anymore, preventing further disconnect attempts
message = 'error: ERR_SOCKET_NOT_CONNECTED';
console.log(`${self.connectionType}: ${direction} ${message}: ${result}`);
self.connectionId = false;
return;
case -21:
message = 'error: NETWORK_CHANGED';
break;
case -100:
message = 'error: CONNECTION_CLOSED';
break;
case -102:
message = 'error: CONNECTION_REFUSED';
break;
case -105:
message = 'error: NAME_NOT_RESOLVED';
break;
case -106:
message = 'error: INTERNET_DISCONNECTED';
break;
case -109:
message = 'error: ADDRESS_UNREACHABLE';
break;
}
}
const resultMessage = message ? `${message} ${result}` : result;
console.warn(`${self.connectionType}: ${resultMessage} ID: ${self.connectionId} (${direction})`);
if (GUI.connected_to || GUI.connecting_to) {
$('a.connect').trigger('click');
} else {
serial.disconnect();
}
},
};
export default serial;

View file

@ -130,8 +130,9 @@ function connectDisconnect() {
// Hack to get virtual working on the web
serial = serialShim();
serial.connect('virtual', {}, onOpenVirtual);
serial.connect(onOpenVirtual);
} else {
CONFIGURATOR.virtualMode = false;
serial = serialShim();
// Explicitly disconnect the event listeners before attaching the new ones.
serial.removeEventListener('connect', connectHandler);

View file

@ -1,6 +1,6 @@
import CONFIGURATOR from "./data_storage";
import serialNWJS from "./serial.js";
import serialWeb from "./webSerial.js";
import BT from "./protocols/bluetooth.js";
import virtualSerial from "./virtualSerial.js";
export let serialShim = () => CONFIGURATOR.virtualMode ? serialNWJS : CONFIGURATOR.bluetoothMode ? BT : serialWeb;
export let serialShim = () => CONFIGURATOR.virtualMode ? virtualSerial: CONFIGURATOR.bluetoothMode ? BT : serialWeb;

View file

@ -221,13 +221,9 @@ cli.initialize = function (callback) {
clearHistory();
});
if (Clipboard.available) {
self.GUI.copyButton.click(function() {
copyToClipboard(self.outputHistory);
});
} else {
self.GUI.copyButton.hide();
}
$('a.load').on('click', function() {
loadFile();

View file

@ -5,7 +5,6 @@ import { have_sensor } from "../sensor_helpers";
import FC from "../fc";
import MSP from "../msp";
import MSPCodes from "../msp/MSPCodes";
import serial from "../serial";
import * as d3 from 'd3';
import $ from 'jquery';
import semver from 'semver';
@ -517,8 +516,6 @@ sensors.initialize = function (callback) {
};
sensors.cleanup = function (callback) {
serial.emptyOutputBuffer();
if (callback) callback();
};

View file

@ -252,9 +252,6 @@ vtx.initialize = function (callback) {
$(".vtx_table_save_pending").toggle(TABS.vtx.vtxTableSavePending);
$(".factory_band").toggle(TABS.vtx.vtxTableFactoryBandsSupported);
// Buttons
$('.clipboard_available').toggle(Clipboard.available && Clipboard.readAvailable);
// Insert actual values in the fields
// Values of the selected mode
$("#vtx_frequency").val(FC.VTX_CONFIG.vtx_frequency);

43
src/js/virtualSerial.js Normal file
View file

@ -0,0 +1,43 @@
const VIRTUAL = "virtual";
/**
* Stripped down version of our nwjs based serial port implementation
* which is required to still have virtual serial port support in the
* browser.
*/
class VirtualSerial {
constructor() {
this.connected = false;
this.connectionId = false;
this.openCanceled = false;
this.bitrate = 0;
this.bytesReceived = 0;
this.bytesSent = 0;
this.failed = 0;
this.connectionType = VIRTUAL;
this.transmitting = false;
this.outputBuffer = [];
}
connect(callback) {
if (!this.openCanceled) {
this.connected = true;
this.connectionId = VIRTUAL;
this.bitrate = 115200;
callback();
}
}
disconnect(callback) {
this.connected = false;
this.outputBuffer = [];
this.transmitting = false;
if (this.connectionId) {
this.connectionId = false;
this.bitrate = 0;
if (callback) {
callback(true);
}
}
}
}
export default new VirtualSerial();