From e27a9cc50317fd30ab35678a62694fc261d3ea68 Mon Sep 17 00:00:00 2001 From: eric Date: Tue, 14 May 2019 11:18:37 -0500 Subject: [PATCH 1/3] Add alternative optional service, control, packet & button uuids --- src/secure-dfu.ts | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/secure-dfu.ts b/src/secure-dfu.ts index c444877..cf05f0f 100644 --- a/src/secure-dfu.ts +++ b/src/secure-dfu.ts @@ -212,7 +212,8 @@ export class SecureDfu extends EventDispatcher { }); } - private gattConnect(device: BluetoothDevice): Promise> { + private gattConnect(device: BluetoothDevice, serviceUUID?: number|string): Promise> { + serviceUUID = serviceUUID || SecureDfu.SERVICE_UUID; return Promise.resolve() .then(() => { if (device.gatt.connected) return device.gatt; @@ -220,7 +221,7 @@ export class SecureDfu extends EventDispatcher { }) .then(server => { this.log("connected to gatt server"); - return server.getPrimaryService(SecureDfu.SERVICE_UUID) + return server.getPrimaryService(serviceUUID) .catch(() => { throw new Error("Unable to find DFU service"); }); @@ -393,15 +394,27 @@ export class SecureDfu extends EventDispatcher { * Scans for a device to update * @param buttonLess Scans for all devices and will automatically call `setDfuMode` * @param filters Alternative filters to use when scanning + * @param uuids Optional alternative uuids for service, control, packet or button * @returns Promise containing the device */ - public requestDevice(buttonLess: boolean, filters: Array): Promise { + public requestDevice( + buttonLess: boolean, + filters: Array, + uuids?: { + service?: number|string, + button?: number|string, + control?: number|string, + packet?: number|string + } + ): Promise { + uuids = uuids || {}; + const serviceId = uuids.service || SecureDfu.SERVICE_UUID; if (!buttonLess && !filters) { - filters = [ { services: [ SecureDfu.SERVICE_UUID ] } ]; + filters = [ { services: [ serviceId ] } ]; } const options: any = { - optionalServices: [ SecureDfu.SERVICE_UUID ] + optionalServices: [ serviceId ] }; if (filters) options.filters = filters; @@ -410,7 +423,7 @@ export class SecureDfu extends EventDispatcher { return this.bluetooth.requestDevice(options) .then(device => { if (buttonLess) { - return this.setDfuMode(device); + return this.setDfuMode(device, uuids); } return device; }); @@ -419,19 +432,25 @@ export class SecureDfu extends EventDispatcher { /** * Sets the DFU mode of a device, preparing it for update * @param device The device to switch mode + * @param uuids Optional alternative uuids for control, packet or button * @returns Promise containing the device if it is still on a valid state */ - public setDfuMode(device: BluetoothDevice): Promise { - return this.gattConnect(device) + public setDfuMode(device: BluetoothDevice, uuids?: { service?: number|string, control?: number|string, packet?: number|string, button?: number|string}): Promise { + uuids = uuids || {}; + const serviceId = uuids.service || SecureDfu.SERVICE_UUID; + const controlId = uuids.control || CONTROL_UUID; + const packetId = uuids.packet || PACKET_UUID; + const buttonId = uuids.button || BUTTON_UUID; + return this.gattConnect(device, serviceId) .then(characteristics => { this.log(`found ${characteristics.length} characteristic(s)`); const controlChar = characteristics.find(characteristic => { - return (characteristic.uuid === CONTROL_UUID); + return (characteristic.uuid === controlId); }); const packetChar = characteristics.find(characteristic => { - return (characteristic.uuid === PACKET_UUID); + return (characteristic.uuid === packetId); }); if (controlChar && packetChar) { @@ -439,7 +458,7 @@ export class SecureDfu extends EventDispatcher { } const buttonChar = characteristics.find(characteristic => { - return (characteristic.uuid === BUTTON_UUID); + return (characteristic.uuid === buttonId); }); if (!buttonChar) { From 4d7b9fc295fa761223108b091bb52511d3916b10 Mon Sep 17 00:00:00 2001 From: eric Date: Tue, 14 May 2019 15:28:39 -0500 Subject: [PATCH 2/3] Attend requested changes Add UuidOptions interface Use default values instead of optionals Lint --- src/secure-dfu.ts | 43 +++++++++++++++++++------------------------ src/uuid-options.ts | 6 ++++++ 2 files changed, 25 insertions(+), 24 deletions(-) create mode 100644 src/uuid-options.ts diff --git a/src/secure-dfu.ts b/src/secure-dfu.ts index cf05f0f..cc9c7aa 100644 --- a/src/secure-dfu.ts +++ b/src/secure-dfu.ts @@ -24,6 +24,7 @@ */ import { EventDispatcher } from "./dispatcher"; +import { UuidOptions } from "./uuid-options"; const CONTROL_UUID = "8ec90001-f315-4f60-9fb8-838830daea50"; const PACKET_UUID = "8ec90002-f315-4f60-9fb8-838830daea50"; @@ -140,6 +141,13 @@ export class SecureDfu extends EventDispatcher { */ public static EVENT_PROGRESS: string = "progress"; + private DEFAULT_UUIDS: UuidOptions = { + service: SecureDfu.SERVICE_UUID, + button: BUTTON_UUID, + control: CONTROL_UUID, + packet: PACKET_UUID + }; + private notifyFns: {} = {}; private controlChar: BluetoothRemoteGATTCharacteristic = null; private packetChar: BluetoothRemoteGATTCharacteristic = null; @@ -212,8 +220,7 @@ export class SecureDfu extends EventDispatcher { }); } - private gattConnect(device: BluetoothDevice, serviceUUID?: number|string): Promise> { - serviceUUID = serviceUUID || SecureDfu.SERVICE_UUID; + private gattConnect(device: BluetoothDevice, serviceUUID: number | string = SecureDfu.SERVICE_UUID): Promise> { return Promise.resolve() .then(() => { if (device.gatt.connected) return device.gatt; @@ -398,23 +405,16 @@ export class SecureDfu extends EventDispatcher { * @returns Promise containing the device */ public requestDevice( - buttonLess: boolean, - filters: Array, - uuids?: { - service?: number|string, - button?: number|string, - control?: number|string, - packet?: number|string - } + buttonLess: boolean, + filters: Array, + uuids: UuidOptions = this.DEFAULT_UUIDS ): Promise { - uuids = uuids || {}; - const serviceId = uuids.service || SecureDfu.SERVICE_UUID; if (!buttonLess && !filters) { - filters = [ { services: [ serviceId ] } ]; + filters = [ { services: [ uuids.service ] } ]; } const options: any = { - optionalServices: [ serviceId ] + optionalServices: [ uuids.service ] }; if (filters) options.filters = filters; @@ -435,22 +435,17 @@ export class SecureDfu extends EventDispatcher { * @param uuids Optional alternative uuids for control, packet or button * @returns Promise containing the device if it is still on a valid state */ - public setDfuMode(device: BluetoothDevice, uuids?: { service?: number|string, control?: number|string, packet?: number|string, button?: number|string}): Promise { - uuids = uuids || {}; - const serviceId = uuids.service || SecureDfu.SERVICE_UUID; - const controlId = uuids.control || CONTROL_UUID; - const packetId = uuids.packet || PACKET_UUID; - const buttonId = uuids.button || BUTTON_UUID; - return this.gattConnect(device, serviceId) + public setDfuMode(device: BluetoothDevice, uuids: UuidOptions = this.DEFAULT_UUIDS): Promise { + return this.gattConnect(device, uuids.service) .then(characteristics => { this.log(`found ${characteristics.length} characteristic(s)`); const controlChar = characteristics.find(characteristic => { - return (characteristic.uuid === controlId); + return (characteristic.uuid === uuids.control); }); const packetChar = characteristics.find(characteristic => { - return (characteristic.uuid === packetId); + return (characteristic.uuid === uuids.packet); }); if (controlChar && packetChar) { @@ -458,7 +453,7 @@ export class SecureDfu extends EventDispatcher { } const buttonChar = characteristics.find(characteristic => { - return (characteristic.uuid === buttonId); + return (characteristic.uuid === uuids.button); }); if (!buttonChar) { diff --git a/src/uuid-options.ts b/src/uuid-options.ts new file mode 100644 index 0000000..f8133a4 --- /dev/null +++ b/src/uuid-options.ts @@ -0,0 +1,6 @@ +export interface UuidOptions { + service?: number | string; + button?: number | string; + control?: number | string; + packet?: number | string; +} From c45f6f8b8de0f72412432ab2d40484939992ae33 Mon Sep 17 00:00:00 2001 From: eric Date: Wed, 15 May 2019 07:05:33 -0500 Subject: [PATCH 3/3] Merge uuid options with defaults Move UuidOptions interface into secure-dfu.ts --- src/secure-dfu.ts | 16 +++++++++++++++- src/uuid-options.ts | 6 ------ 2 files changed, 15 insertions(+), 7 deletions(-) delete mode 100644 src/uuid-options.ts diff --git a/src/secure-dfu.ts b/src/secure-dfu.ts index cc9c7aa..a31d453 100644 --- a/src/secure-dfu.ts +++ b/src/secure-dfu.ts @@ -24,7 +24,6 @@ */ import { EventDispatcher } from "./dispatcher"; -import { UuidOptions } from "./uuid-options"; const CONTROL_UUID = "8ec90001-f315-4f60-9fb8-838830daea50"; const PACKET_UUID = "8ec90002-f315-4f60-9fb8-838830daea50"; @@ -119,6 +118,13 @@ export interface BluetoothLEScanFilterInit { namePrefix?: string; } +export interface UuidOptions { + service?: number | string; + button?: number | string; + control?: number | string; + packet?: number | string; +} + /** * Secure Device Firmware Update class */ @@ -409,6 +415,10 @@ export class SecureDfu extends EventDispatcher { filters: Array, uuids: UuidOptions = this.DEFAULT_UUIDS ): Promise { + uuids = { + ...this.DEFAULT_UUIDS, + ...uuids + }; if (!buttonLess && !filters) { filters = [ { services: [ uuids.service ] } ]; } @@ -436,6 +446,10 @@ export class SecureDfu extends EventDispatcher { * @returns Promise containing the device if it is still on a valid state */ public setDfuMode(device: BluetoothDevice, uuids: UuidOptions = this.DEFAULT_UUIDS): Promise { + uuids = { + ...this.DEFAULT_UUIDS, + ...uuids + }; return this.gattConnect(device, uuids.service) .then(characteristics => { this.log(`found ${characteristics.length} characteristic(s)`); diff --git a/src/uuid-options.ts b/src/uuid-options.ts deleted file mode 100644 index f8133a4..0000000 --- a/src/uuid-options.ts +++ /dev/null @@ -1,6 +0,0 @@ -export interface UuidOptions { - service?: number | string; - button?: number | string; - control?: number | string; - packet?: number | string; -}