Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions lib/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ export enum ConnectionStatus {
* but has not been connected via the browser security UI.
*/
NO_AUTHORIZED_DEVICE = "NO_AUTHORIZED_DEVICE",
/**
* Disconnecting.
*/
DISCONNECTING = "DISCONNECTING",
Comment on lines -84 to -87
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used, but was added in my last PR. I forgot to remove it

/**
* Authorized device available but we haven't connected to it.
*/
Expand Down
91 changes: 58 additions & 33 deletions lib/usb-radio-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export interface MicrobitRadioBridgeConnectionOptions {
logging: Logging;
}

interface ConnectCallbacks {
onConnecting: () => void;
onReconnecting: () => void;
onFail: () => void;
onSuccess: () => void;
}

/**
* Wraps around a USB connection to implement a subset of services over a serial protocol.
*
Expand All @@ -48,7 +55,7 @@ export class MicrobitRadioBridgeConnection
private disconnectPromise: Promise<void> | undefined;
private serialSessionOpen = false;

private delegateStatusListner = (e: ConnectionStatusEvent) => {
private delegateStatusListener = (e: ConnectionStatusEvent) => {
const currentStatus = this.status;
if (e.status !== ConnectionStatus.CONNECTED) {
this.setStatus(e.status);
Expand Down Expand Up @@ -84,11 +91,11 @@ export class MicrobitRadioBridgeConnection
async initialize(): Promise<void> {
await this.delegate.initialize();
this.setStatus(this.statusFromDelegate());
this.delegate.addEventListener("status", this.delegateStatusListner);
this.delegate.addEventListener("status", this.delegateStatusListener);
}

dispose(): void {
this.delegate.removeEventListener("status", this.delegateStatusListner);
this.delegate.removeEventListener("status", this.delegateStatusListener);
this.delegate.dispose();
}

Expand Down Expand Up @@ -124,36 +131,39 @@ export class MicrobitRadioBridgeConnection
this.remoteDeviceId,
this.delegate,
this.dispatchTypedEvent.bind(this),
this.setStatus.bind(this),
() => {
// Remote connection lost
this.logging.event({
type: "Serial",
message: "Serial connection lost 1",
});
// This is the point we tell the consumer that we're trying to reconnect
// in the background.
// Leave serial connection running in case the remote device comes back.
},
() => {
// Remote connection... even more lost?
this.logging.event({
type: "Serial",
message: "Serial connection lost 2",
});
this.serialSession?.dispose();
{
onConnecting: () => this.setStatus(ConnectionStatus.CONNECTING),
onReconnecting: () => {
// Leave serial connection running in case the remote device comes back.
if (this.status !== ConnectionStatus.RECONNECTING) {
this.setStatus(ConnectionStatus.RECONNECTING);
}
},
onFail: () => {
if (this.status !== ConnectionStatus.DISCONNECTED) {
this.setStatus(ConnectionStatus.DISCONNECTED);
}
this.serialSession?.dispose();
this.serialSessionOpen = false;
},
onSuccess: () => {
if (this.status !== ConnectionStatus.CONNECTED) {
this.setStatus(ConnectionStatus.CONNECTED);
}
this.serialSessionOpen = true;
},
},
);

await this.serialSession.connect();
this.serialSessionOpen = true;

this.logging.event({
type: "Connect",
message: "Serial connect success",
});
return this.status;
} catch (e) {
this.serialSessionOpen = false;
this.logging.error("Failed to initialise serial protocol", e);
this.logging.event({
type: "Connect",
Expand All @@ -174,8 +184,13 @@ export class MicrobitRadioBridgeConnection
})();
}

private log(v: any) {
this.logging.log(v);
}

private setStatus(status: ConnectionStatus) {
this.status = status;
this.log("Radio connection status " + status);
this.dispatchTypedEvent("status", new ConnectionStatusEvent(status));
}

Expand Down Expand Up @@ -267,18 +282,15 @@ class RadioBridgeSerialSession {
private remoteDeviceId: number,
private delegate: MicrobitWebUSBConnection,
private dispatchTypedEvent: TypedServiceEventDispatcher,
private onStatusChanged: (status: ConnectionStatus) => void,
private onRemoteConnectionLost1: () => void,
private onRemoteConnectionLost2: () => void,
private callbacks: ConnectCallbacks,
) {}

async connect() {
this.delegate.addEventListener("serialdata", this.serialDataListener);
this.delegate.addEventListener("serialerror", this.serialErrorListener);

try {
this.callbacks.onConnecting();
await this.handshake();
this.onStatusChanged(ConnectionStatus.CONNECTED);

this.logging.log(`Serial: using remote device id ${this.remoteDeviceId}`);
const remoteMbIdCommand = protocol.generateCmdRemoteMbId(
Expand Down Expand Up @@ -319,8 +331,9 @@ class RadioBridgeSerialSession {
await periodicMessagePromise;

this.startConnectionCheck();
this.callbacks.onSuccess();
} catch (e) {
this.dispose();
this.callbacks.onFail();
}
}

Expand All @@ -335,8 +348,6 @@ class RadioBridgeSerialSession {
this.delegate.removeEventListener("serialdata", this.serialDataListener);
this.delegate.removeEventListener("serialerror", this.serialErrorListener);
await this.delegate.softwareReset();

this.onStatusChanged(ConnectionStatus.DISCONNECTED);
}

private async sendCmdWaitResponse(
Expand All @@ -358,19 +369,33 @@ class RadioBridgeSerialSession {
private startConnectionCheck() {
// Check for connection lost
if (this.connectionCheckIntervalId === undefined) {
this.connectionCheckIntervalId = setInterval(async () => {
this.connectionCheckIntervalId = setInterval(() => {
if (
this.lastReceivedMessageTimestamp &&
Date.now() - this.lastReceivedMessageTimestamp <= 1_000
) {
this.callbacks.onSuccess();
}
if (
this.lastReceivedMessageTimestamp &&
Date.now() - this.lastReceivedMessageTimestamp > 1_000
) {
this.onRemoteConnectionLost1();
this.logging.event({
type: "Serial",
message: "Serial connection lost - attempting to reconnect",
});
this.callbacks.onReconnecting();
}
if (
this.lastReceivedMessageTimestamp &&
Date.now() - this.lastReceivedMessageTimestamp >
connectTimeoutDuration
) {
this.onRemoteConnectionLost2();
this.logging.event({
type: "Serial",
message: "Serial connection lost",
});
this.callbacks.onFail();
}
}, 1000);
}
Expand Down