-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86cbb6a
commit 2645519
Showing
7 changed files
with
178 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Service } from "./bluetooth-device-wrapper.js"; | ||
import { profile } from "./bluetooth-profile.js"; | ||
import { BackgroundErrorEvent, DeviceError } from "./device.js"; | ||
import { | ||
CharacteristicDataTarget, | ||
TypedServiceEvent, | ||
TypedServiceEventDispatcher, | ||
} from "./service-events.js"; | ||
import { UARTDataEvent } from "./uart.js"; | ||
|
||
export class UARTService implements Service { | ||
constructor( | ||
private txCharacteristic: BluetoothRemoteGATTCharacteristic, | ||
private rxCharacteristic: BluetoothRemoteGATTCharacteristic, | ||
private dispatchTypedEvent: TypedServiceEventDispatcher, | ||
private queueGattOperation: <R>(action: () => Promise<R>) => Promise<R>, | ||
) { | ||
this.txCharacteristic.addEventListener( | ||
"characteristicvaluechanged", | ||
(event: Event) => { | ||
const target = event.target as CharacteristicDataTarget; | ||
const value = new Uint8Array(target.value.buffer); | ||
this.dispatchTypedEvent("uartdata", new UARTDataEvent(value)); | ||
}, | ||
); | ||
} | ||
|
||
static async createService( | ||
gattServer: BluetoothRemoteGATTServer, | ||
dispatcher: TypedServiceEventDispatcher, | ||
queueGattOperation: <R>(action: () => Promise<R>) => Promise<R>, | ||
listenerInit: boolean, | ||
): Promise<UARTService | undefined> { | ||
let uartService: BluetoothRemoteGATTService; | ||
try { | ||
uartService = await gattServer.getPrimaryService(profile.uart.id); | ||
} catch (err) { | ||
if (listenerInit) { | ||
dispatcher("backgrounderror", new BackgroundErrorEvent(err as string)); | ||
return; | ||
} else { | ||
throw new DeviceError({ | ||
code: "service-missing", | ||
message: err as string, | ||
}); | ||
} | ||
} | ||
const rxCharacteristic = await uartService.getCharacteristic( | ||
profile.uart.characteristics.rx.id, | ||
); | ||
const txCharacteristic = await uartService.getCharacteristic( | ||
profile.uart.characteristics.tx.id, | ||
); | ||
return new UARTService( | ||
txCharacteristic, | ||
rxCharacteristic, | ||
dispatcher, | ||
queueGattOperation, | ||
); | ||
} | ||
|
||
async startNotifications(type: TypedServiceEvent): Promise<void> { | ||
if (type === "uartdata") { | ||
await this.txCharacteristic.startNotifications(); | ||
} | ||
} | ||
|
||
async stopNotifications(type: TypedServiceEvent): Promise<void> { | ||
if (type === "uartdata") { | ||
await this.txCharacteristic.stopNotifications(); | ||
} | ||
} | ||
|
||
async writeData(value: Uint8Array): Promise<void> { | ||
const dataView = new DataView(value.buffer); | ||
return this.queueGattOperation(() => | ||
this.rxCharacteristic.writeValueWithoutResponse(dataView), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class UARTDataEvent extends Event { | ||
constructor(public readonly value: Uint8Array) { | ||
super("uartdata"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters