Skip to content

Commit

Permalink
temp: add mock Bluetooth Calls
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-sanderson committed Dec 5, 2024
1 parent 68a87e5 commit b118c84
Showing 1 changed file with 99 additions and 28 deletions.
127 changes: 99 additions & 28 deletions packages/transport-bluetooth/src/client/bluetooth-ipc-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,85 @@ export class BluetoothApiImpl extends TypedEmitter<BluetoothApiEvents> implement
}

async connectDevice(uuid: string) {
try {
await this.api.connect();
} catch (error) {
return { success: false, error: error.message };
}
// try {
// await this.api.connect();
// } catch (error) {
// return { success: false, error: error.message };
// }

const emitStatus = (event: DeviceConnectionStatus) => {
this.emit('device-connection-status', event);
};

this.api.on('device_connection_status', event =>
emitStatus({ uuid: event.uuid, type: event.phase }),
);
this.api.on('device_pairing', event => {
if (!event.paired) {
emitStatus({
uuid,
type: 'pairing',
pin: event.pin,
});
} else {
emitStatus({
uuid: event.uuid,
type: 'paired',
});
}
// Todo: enter pairing mode
const UUID = 'hci0/dev_E1_43_47_BA_6A_69';
// const linuxPin = { uuid: UUID, type: 'pairing' as const, pin: '' };
const windowsPin = {
uuid: UUID,
type: 'pairing' as const,
pin: '123456',
};

// UI:
// - click button -> "Connecting"
// - "pair-device-event" -> pairing -> show PIN Modal
// - (Win/Linux) "pair-device-event" -> paired -> hide PIN Modal
// - "connect-device-event":start -> connecting -> hide PIN Modal (for Mac)
// - "connect-device-event" :done -> connected, but ...connect-connecting again

// 1. [Win, Lin, Mac] In case of windows, this is where we get PIN, on Mac we may not get this
await new Promise(resolve => setTimeout(resolve, 2000));
emitStatus(windowsPin);

// 2. [Win, Lin] Simulates that user confirmed PIN on the device
await new Promise(resolve => setTimeout(resolve, 4000));
emitStatus({
uuid: UUID,
type: 'paired',
});

try {
const result = await this.api.sendMessage('connect_device', uuid);
console.warn('Connect result', result);
} catch (error) {
return { success: false, error: error.message };
}
// 3. [Win, Lin, Mac] Simulates that device is starting to connect
await new Promise(resolve => setTimeout(resolve, 2000));
emitStatus({
uuid: UUID,
type: 'connecting',
});

// 4. [Win, Lin, Mac] Simulates that device is starting to connect
await new Promise(resolve => setTimeout(resolve, 2000));
emitStatus({
uuid: UUID,
type: 'connected',
});

return { success: true } as const;

// this.api.on('device_connection_status', event =>
// emitStatus({ uuid: event.uuid, type: event.phase }),
// );
// this.api.on('device_pairing', event => {
// if (!event.paired) {
// emitStatus({
// uuid,
// type: 'pairing',
// pin: event.pin,
// });
// } else {
// emitStatus({
// uuid: event.uuid,
// type: 'paired',
// });
// }
// });
//
// try {
// const result = await this.api.sendMessage('connect_device', uuid);
// console.warn('Connect result', result);
// } catch (error) {
// return { success: false, error: error.message };
// }
//
// return { success: true } as const;
}

async forgetDevice(id: string): Promise<any> {
Expand Down Expand Up @@ -113,7 +156,35 @@ export class BluetoothApiImpl extends TypedEmitter<BluetoothApiEvents> implement
// return devs.filter(d => d.data.length > 0 && (d.paired || d.data[0] === 1));
};
const emitSelect = ({ devices }: { devices: BluetoothDevice[] }) => {
this.emit('device-list-update', connectableDevices(devices));
this.emit('device-list-update', [
{
name: 'TrezorZephyr',
internal_model: 1,
model_variant: 3,
uuid: 'hci0/dev_E1_43_47_BA_6A_69',
connected: false,
timestamp: 1732787043,
rssi: 0,
pairing_mode: false,
paired: true,
data: [
// 1. "pairing_mode" - prvni byte jesli device vubec je pairovatelny (a ma se vubec zobrazit v ui)
0,

// 2. "model_variant" - jeden byte, vlastne nemusi to byt jen color, nekdy v budocnosti treba tam muze byt i nejaka jina specificka vlastnost,
// typu, ma NFC, nema NFC. neni to zatim nikde zdefiniovany mapa musi teprv vzniknout vajemnou domluvou, trezor zatim posila zahardcodovanou
// value, udelal jsem na to nejakou TODO utilitku v jedne z komponent
1,

// 3. "internal_model" - jeden byte, bude nejak namapovany cislo na enum DeviceIntrenalModel v trezor/connect.
// opet neni nikde zdefiniovany, trezor posila zahardcodovany, a je na to TODO utilitka v te componente
3,
],
},
...devices,
]);

// this.emit('device-list-update', connectableDevices(devices));
};
const emitAdapterState = ({ powered }: { powered: boolean }) => {
this.emit('adapter-event', powered);
Expand Down

0 comments on commit b118c84

Please sign in to comment.