|
| 1 | +import { Service } from "./bluetooth-device-wrapper.js"; |
| 2 | +import { profile } from "./bluetooth-profile.js"; |
| 3 | +import { BackgroundErrorEvent, DeviceError } from "./device.js"; |
| 4 | +import { |
| 5 | + CharacteristicDataTarget, |
| 6 | + TypedServiceEvent, |
| 7 | + TypedServiceEventDispatcher, |
| 8 | +} from "./service-events.js"; |
| 9 | +import { UARTDataEvent } from "./uart.js"; |
| 10 | + |
| 11 | +export class UARTService implements Service { |
| 12 | + constructor( |
| 13 | + private txCharacteristic: BluetoothRemoteGATTCharacteristic, |
| 14 | + private rxCharacteristic: BluetoothRemoteGATTCharacteristic, |
| 15 | + private dispatchTypedEvent: TypedServiceEventDispatcher, |
| 16 | + private queueGattOperation: <R>(action: () => Promise<R>) => Promise<R>, |
| 17 | + ) { |
| 18 | + this.txCharacteristic.addEventListener( |
| 19 | + "characteristicvaluechanged", |
| 20 | + (event: Event) => { |
| 21 | + const target = event.target as CharacteristicDataTarget; |
| 22 | + const value = new Uint8Array(target.value.buffer); |
| 23 | + this.dispatchTypedEvent("uartdata", new UARTDataEvent(value)); |
| 24 | + }, |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + static async createService( |
| 29 | + gattServer: BluetoothRemoteGATTServer, |
| 30 | + dispatcher: TypedServiceEventDispatcher, |
| 31 | + queueGattOperation: <R>(action: () => Promise<R>) => Promise<R>, |
| 32 | + listenerInit: boolean, |
| 33 | + ): Promise<UARTService | undefined> { |
| 34 | + let uartService: BluetoothRemoteGATTService; |
| 35 | + try { |
| 36 | + uartService = await gattServer.getPrimaryService(profile.uart.id); |
| 37 | + } catch (err) { |
| 38 | + if (listenerInit) { |
| 39 | + dispatcher("backgrounderror", new BackgroundErrorEvent(err as string)); |
| 40 | + return; |
| 41 | + } else { |
| 42 | + throw new DeviceError({ |
| 43 | + code: "service-missing", |
| 44 | + message: err as string, |
| 45 | + }); |
| 46 | + } |
| 47 | + } |
| 48 | + const rxCharacteristic = await uartService.getCharacteristic( |
| 49 | + profile.uart.characteristics.rx.id, |
| 50 | + ); |
| 51 | + const txCharacteristic = await uartService.getCharacteristic( |
| 52 | + profile.uart.characteristics.tx.id, |
| 53 | + ); |
| 54 | + return new UARTService( |
| 55 | + txCharacteristic, |
| 56 | + rxCharacteristic, |
| 57 | + dispatcher, |
| 58 | + queueGattOperation, |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + async startNotifications(type: TypedServiceEvent): Promise<void> { |
| 63 | + if (type === "uartdata") { |
| 64 | + await this.txCharacteristic.startNotifications(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + async stopNotifications(type: TypedServiceEvent): Promise<void> { |
| 69 | + if (type === "uartdata") { |
| 70 | + await this.txCharacteristic.stopNotifications(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + async writeData(value: Uint8Array): Promise<void> { |
| 75 | + const dataView = new DataView(value.buffer); |
| 76 | + return this.queueGattOperation(() => |
| 77 | + this.rxCharacteristic.writeValueWithoutResponse(dataView), |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
0 commit comments