Skip to content

Commit 16ae2ac

Browse files
authored
cleanup and explicit use web prefix for protocols (#4500)
* cleanup * more cleanup * Reset connectionInfo * Fix todo * Fix default * More explicit
1 parent 52d3b8d commit 16ae2ac

File tree

8 files changed

+50
-82
lines changed

8 files changed

+50
-82
lines changed

src/components/port-picker/PortsInput.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
>
4343
{{ connectedUsbDevice.displayName }}
4444
</option>
45-
<option v-if="showSerialOption" value="requestpermission">
45+
<option v-if="showSerialOption" value="requestpermissionserial">
4646
{{ $t("portsSelectPermission") }}
4747
</option>
4848
<option v-if="showBluetoothOption" value="requestpermissionbluetooth">
@@ -173,8 +173,8 @@ export default defineComponent({
173173
174174
const onChangePort = (event) => {
175175
const value = event.target.value;
176-
if (value === "requestpermission") {
177-
EventBus.$emit("ports-input:request-permission");
176+
if (value === "requestpermissionserial") {
177+
EventBus.$emit("ports-input:request-permission-serial");
178178
} else if (value === "requestpermissionbluetooth") {
179179
EventBus.$emit("ports-input:request-permission-bluetooth");
180180
} else if (value === "requestpermissionusb") {

src/js/data_storage.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const CONFIGURATOR = {
1111

1212
connectionValid: false,
1313
connectionValidCliOnly: false,
14-
bluetoothMode: false,
15-
manualMode: false,
1614
virtualMode: false,
1715
virtualApiVersion: "0.0.1",
1816
cliActive: false,

src/js/port_handler.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ const PortHandler = new (function () {
5050
})();
5151

5252
PortHandler.initialize = function () {
53-
EventBus.$on("ports-input:request-permission-bluetooth", () => this.requestDevicePermission("bluetooth"));
54-
EventBus.$on("ports-input:request-permission", () => this.requestDevicePermission("serial"));
53+
EventBus.$on("ports-input:request-permission-bluetooth", () => this.requestDevicePermission("webbluetooth"));
54+
EventBus.$on("ports-input:request-permission-serial", () => this.requestDevicePermission("webserial"));
5555
EventBus.$on("ports-input:request-permission-usb", () => this.requestDevicePermission("usb"));
5656
EventBus.$on("ports-input:change", this.onChangeSelectedPort.bind(this));
5757

@@ -60,9 +60,9 @@ PortHandler.initialize = function () {
6060
const detail = event.detail;
6161

6262
if (detail?.path?.startsWith("bluetooth")) {
63-
this.handleDeviceAdded(detail, "bluetooth");
63+
this.handleDeviceAdded(detail, "webbluetooth");
6464
} else {
65-
this.handleDeviceAdded(detail, "serial");
65+
this.handleDeviceAdded(detail, "webserial");
6666
}
6767
});
6868

@@ -81,8 +81,8 @@ PortHandler.initialize = function () {
8181
PortHandler.refreshAllDeviceLists = async function () {
8282
// Update all device lists in parallel
8383
return Promise.all([
84-
this.updateDeviceList("serial"),
85-
this.updateDeviceList("bluetooth"),
84+
this.updateDeviceList("webserial"),
85+
this.updateDeviceList("webbluetooth"),
8686
this.updateDeviceList("usb"),
8787
]).then(() => {
8888
this.selectActivePort();
@@ -112,16 +112,16 @@ PortHandler.removedSerialDevice = function (device) {
112112
if (!devicePath) {
113113
console.warn(`${this.logHead} Device removal event missing path information`, device);
114114
// Still update ports, but don't try to use the undefined path
115-
this.updateDeviceList("serial").then(() => {
115+
this.updateDeviceList("webserial").then(() => {
116116
this.selectActivePort();
117117
});
118118
return;
119119
}
120120

121121
// Update the appropriate ports list based on the device type
122122
const updatePromise = devicePath.startsWith("bluetooth")
123-
? this.updateDeviceList("bluetooth")
124-
: this.updateDeviceList("serial");
123+
? this.updateDeviceList("webbluetooth")
124+
: this.updateDeviceList("webserial");
125125

126126
const wasSelectedPort = this.portPicker.selectedPort === devicePath;
127127

@@ -177,7 +177,9 @@ PortHandler.requestDevicePermission = function (deviceType) {
177177

178178
PortHandler.sortPorts = function (ports) {
179179
return ports.sort(function (a, b) {
180-
return a.path.localeCompare(b.path, window.navigator.language, {
180+
const locale = typeof window !== "undefined" && window.navigator ? window.navigator.language : "en";
181+
182+
return a.path.localeCompare(b.path, locale, {
181183
numeric: true,
182184
sensitivity: "base",
183185
});
@@ -282,7 +284,7 @@ PortHandler.handleDeviceAdded = function (device, deviceType) {
282284

283285
// Update the appropriate device list
284286
const updatePromise =
285-
deviceType === "bluetooth" ? this.updateDeviceList("bluetooth") : this.updateDeviceList("serial");
287+
deviceType === "webbluetooth" ? this.updateDeviceList("webbluetooth") : this.updateDeviceList("webserial");
286288

287289
updatePromise.then(() => {
288290
const selectedPort = this.selectActivePort(device);
@@ -304,19 +306,19 @@ PortHandler.updateDeviceList = async function (deviceType) {
304306

305307
try {
306308
switch (deviceType) {
307-
case "bluetooth":
309+
case "webbluetooth":
308310
if (this.showBluetoothOption) {
309-
ports = await serial.getDevices("bluetooth");
311+
ports = await serial.getDevices("webbluetooth");
310312
}
311313
break;
312314
case "usb":
313315
if (this.showUsbOption) {
314316
ports = await WEBUSBDFU.getDevices();
315317
}
316318
break;
317-
case "serial":
319+
case "webserial":
318320
if (this.showSerialOption) {
319-
ports = await serial.getDevices("serial");
321+
ports = await serial.getDevices("webserial");
320322
}
321323
break;
322324
default:
@@ -329,7 +331,7 @@ PortHandler.updateDeviceList = async function (deviceType) {
329331

330332
// Update the appropriate properties based on device type
331333
switch (deviceType) {
332-
case "bluetooth":
334+
case "webbluetooth":
333335
this.bluetoothAvailable = orderedPorts.length > 0;
334336
this.currentBluetoothPorts = [...orderedPorts];
335337
console.log(`${this.logHead} Found bluetooth port(s)`, orderedPorts);
@@ -339,12 +341,14 @@ PortHandler.updateDeviceList = async function (deviceType) {
339341
this.currentUsbPorts = [...orderedPorts];
340342
console.log(`${this.logHead} Found DFU port(s)`, orderedPorts);
341343
break;
342-
case "serial":
343-
default:
344+
case "webserial":
344345
this.portAvailable = orderedPorts.length > 0;
345346
this.currentSerialPorts = [...orderedPorts];
346347
console.log(`${this.logHead} Found serial port(s)`, orderedPorts);
347348
break;
349+
default:
350+
console.warn(`${this.logHead} Unknown device type for updating ports: ${deviceType}`);
351+
return [];
348352
}
349353

350354
return orderedPorts;

src/js/protocols/WebBluetooth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class WebBluetooth extends EventTarget {
4444

4545
this.bluetooth.addEventListener("connect", (e) => this.handleNewDevice(e.target));
4646
this.bluetooth.addEventListener("disconnect", (e) => this.handleRemovedDevice(e.target));
47-
this.bluetooth.addEventListener("gatserverdisconnected", (e) => this.handleRemovedDevice(e.target));
47+
this.bluetooth.addEventListener("gattserverdisconnected", (e) => this.handleRemovedDevice(e.target));
4848

4949
this.loadDevices();
5050
}

src/js/protocols/WebSerial.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { webSerialDevices, vendorIdNames } from "./devices";
22
import GUI from "../gui";
33

4-
const logHead = "[SERIAL]";
4+
const logHead = "[WEBSERIAL]";
55

66
async function* streamAsyncIterable(reader, keepReadingFlag) {
77
try {
@@ -308,13 +308,16 @@ class WebSerial extends EventTarget {
308308

309309
this.connectionId = false;
310310
this.bitrate = 0;
311+
this.connectionInfo = null; // Reset connectionInfo
311312
this.closeRequested = false;
312313

313314
this.dispatchEvent(new CustomEvent("disconnect", { detail: true }));
314315
return true;
315316
} catch (error) {
316317
console.error(`${logHead} Error disconnecting:`, error);
317318
this.closeRequested = false;
319+
// Ensure connectionInfo is reset even on error if port was potentially open
320+
this.connectionInfo = null;
318321
this.dispatchEvent(new CustomEvent("disconnect", { detail: false }));
319322
return false;
320323
} finally {

src/js/serial.js

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,18 @@ class Serial extends EventTarget {
1818

1919
// Initialize the available protocols
2020
this._webSerial = new WebSerial();
21-
this._bluetooth = new WebBluetooth();
22-
this._websocket = new Websocket();
21+
this._webBluetooth = new WebBluetooth();
22+
this._webSocket = new Websocket();
2323
this._virtual = new VirtualSerial();
2424

2525
// Update protocol map to use consistent naming
2626
this._protocolMap = {
27-
serial: this._webSerial, // TODO: should be 'webserial'
28-
bluetooth: this._bluetooth, // TODO: should be 'webbluetooth'
29-
websocket: this._websocket,
27+
webserial: this._webSerial,
28+
webbluetooth: this._webBluetooth,
29+
websocket: this._webSocket,
3030
virtual: this._virtual,
3131
};
3232

33-
// Initialize with default protocol
34-
this.selectProtocol(false);
35-
3633
// Forward events from all protocols to the Serial class
3734
this._setupEventForwarding();
3835
}
@@ -61,10 +58,10 @@ class Serial extends EventTarget {
6158
if (protocol === this._webSerial) {
6259
return "webserial";
6360
}
64-
if (protocol === this._bluetooth) {
61+
if (protocol === this._webBluetooth) {
6562
return "webbluetooth";
6663
}
67-
if (protocol === this._websocket) {
64+
if (protocol === this._webSocket) {
6865
return "websocket";
6966
}
7067
if (protocol === this._virtual) {
@@ -77,7 +74,7 @@ class Serial extends EventTarget {
7774
* Set up event forwarding from all protocols to the Serial class
7875
*/
7976
_setupEventForwarding() {
80-
const protocols = [this._webSerial, this._bluetooth, this._websocket, this._virtual];
77+
const protocols = [this._webSerial, this._webBluetooth, this._webSocket, this._virtual];
8178
const events = ["addedDevice", "removedDevice", "connect", "disconnect", "receive"];
8279

8380
protocols.forEach((protocol) => {
@@ -114,59 +111,28 @@ class Serial extends EventTarget {
114111
}
115112

116113
/**
117-
* Selects the appropriate protocol based on port path or CONFIGURATOR settings
114+
* Selects the appropriate protocol based on port path
118115
* @param {string|null} portPath - Optional port path to determine protocol
119116
* @param {boolean} forceDisconnect - Whether to force disconnect from current protocol
120117
*/
121118
selectProtocol(portPath = null, forceDisconnect = true) {
122-
// Determine which protocol to use based on port path first, then fall back to CONFIGURATOR
119+
// Determine which protocol to use based on port path
123120
let newProtocol;
124121

125122
if (portPath) {
126123
// Select protocol based on port path
127124
if (portPath === "virtual") {
128125
console.log(`${this.logHead} Using virtual protocol (based on port path)`);
129126
newProtocol = this._virtual;
130-
// Update CONFIGURATOR flags for consistency
131-
CONFIGURATOR.virtualMode = true;
132-
CONFIGURATOR.bluetoothMode = false;
133-
CONFIGURATOR.manualMode = false;
134127
} else if (portPath === "manual") {
135128
console.log(`${this.logHead} Using websocket protocol (based on port path)`);
136-
newProtocol = this._websocket;
137-
// Update CONFIGURATOR flags for consistency
138-
CONFIGURATOR.virtualMode = false;
139-
CONFIGURATOR.bluetoothMode = false;
140-
CONFIGURATOR.manualMode = true;
129+
newProtocol = this._webSocket;
141130
} else if (portPath.startsWith("bluetooth")) {
142131
console.log(`${this.logHead} Using bluetooth protocol (based on port path: ${portPath})`);
143-
newProtocol = this._bluetooth;
144-
// Update CONFIGURATOR flags for consistency
145-
CONFIGURATOR.virtualMode = false;
146-
CONFIGURATOR.bluetoothMode = true;
147-
CONFIGURATOR.manualMode = false;
132+
newProtocol = this._webBluetooth;
148133
} else {
149134
console.log(`${this.logHead} Using web serial protocol (based on port path: ${portPath})`);
150135
newProtocol = this._webSerial;
151-
// Update CONFIGURATOR flags for consistency
152-
CONFIGURATOR.virtualMode = false;
153-
CONFIGURATOR.bluetoothMode = false;
154-
CONFIGURATOR.manualMode = false;
155-
}
156-
} else {
157-
// Fall back to CONFIGURATOR flags if no port path is provided
158-
if (CONFIGURATOR.virtualMode) {
159-
console.log(`${this.logHead} Using virtual protocol (based on CONFIGURATOR flags)`);
160-
newProtocol = this._virtual;
161-
} else if (CONFIGURATOR.manualMode) {
162-
console.log(`${this.logHead} Using websocket protocol (based on CONFIGURATOR flags)`);
163-
newProtocol = this._websocket;
164-
} else if (CONFIGURATOR.bluetoothMode) {
165-
console.log(`${this.logHead} Using bluetooth protocol (based on CONFIGURATOR flags)`);
166-
newProtocol = this._bluetooth;
167-
} else {
168-
console.log(`${this.logHead} Using web serial protocol (based on CONFIGURATOR flags)`);
169-
newProtocol = this._webSerial;
170136
}
171137
}
172138

@@ -184,8 +150,6 @@ class Serial extends EventTarget {
184150
// Set new protocol
185151
this._protocol = newProtocol;
186152
console.log(`${this.logHead} Protocol switched successfully to:`, this._protocol);
187-
} else {
188-
console.log(`${this.logHead} Same protocol selected, no switch needed`);
189153
}
190154

191155
return this._protocol;
@@ -251,8 +215,6 @@ class Serial extends EventTarget {
251215
return false;
252216
}
253217

254-
console.log(`${this.logHead} Disconnecting from current protocol`, this._protocol);
255-
256218
try {
257219
// Handle case where we're already disconnected
258220
if (!this._protocol.connected) {
@@ -292,7 +254,7 @@ class Serial extends EventTarget {
292254

293255
/**
294256
* Get devices from a specific protocol type or current protocol
295-
* @param {string} protocolType - Optional protocol type ('serial', 'bluetooth', 'websocket', 'virtual')
257+
* @param {string} protocolType - Optional protocol type ('webserial', 'webbluetooth', 'websocket', 'virtual')
296258
* @returns {Promise<Array>} - List of devices
297259
*/
298260
async getDevices(protocolType = null) {

src/js/serial_backend.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,9 @@ function connectDisconnect() {
125125

126126
// Set configuration flags for consistency with other code
127127
CONFIGURATOR.virtualMode = selectedPort === "virtual";
128-
CONFIGURATOR.bluetoothMode = selectedPort.startsWith("bluetooth");
129-
CONFIGURATOR.manualMode = selectedPort === "manual";
130128

131129
// Select the appropriate protocol based directly on the port path
132130
serial.selectProtocol(selectedPort);
133-
console.log("Serial protocol selected:", serial._protocol, "using port", portName);
134131

135132
if (CONFIGURATOR.virtualMode) {
136133
CONFIGURATOR.virtualApiVersion = PortHandler.portPicker.virtualMspVersion;
@@ -186,7 +183,11 @@ function finishClose(finishedCallback) {
186183
$("#dialogResetToCustomDefaults")[0].close();
187184
}
188185

189-
serial.disconnect(onClosed);
186+
serial.disconnect();
187+
188+
if (CONFIGURATOR.virtualMode) {
189+
onClosed(true);
190+
}
190191

191192
MSP.disconnect_cleanup();
192193
PortUsage.reset();

src/js/utils/checkBrowserCompatibility.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ export function checkBrowserCompatibility() {
108108
.css({
109109
height: "100%",
110110
display: "grid",
111-
"background-image": "url(../images/osd-bg-1.jpg",
111+
"background-image": "url(../images/osd-bg-1.jpg)",
112112
"background-size": "cover",
113113
"background-repeat": "no-repeat",
114114
})
115115
.append(newDiv);
116116

117-
$("div").append(errorMessage).css({
117+
$(newDiv).append(errorMessage).css({
118118
"font-size": "16px",
119119
"background-color": "var(--surface-200)",
120120
color: "var(--text)",

0 commit comments

Comments
 (0)