Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writeValue - options improvements #20

Merged
merged 3 commits into from
Jan 24, 2021
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const {bluetooth, destroy} = createBluetooth()
| `Promise<String[]> getFlags()` | Defines how the characteristic value can be used. |
| `Promise<bool> isNotifying()` | True, if notifications or indications on this characteristic are currently enabled. |
| `Promise<Buffer> readValue(Number offset = 0)` | Issues a request to read the value of the characteristic and returns the value if the operation was successful. |
| `Promise<void> writeValue(Buffer buffer, Number offset = 0)` | Issues a request to write the value of the characteristic. |
| `Promise<void> writeValue(Buffer buffer, Number | WriteValueOptions options = {})` | Issues a request to write the value of the characteristic. Default options `{ offset: 0, type: 'reliable' }`. |
| `Promise<void> startNotifications()` | Starts a notification session from this characteristic if it supports value notifications or indications. |
| `Promise<void> stopNotifications()` | This method will cancel any previous StartNotify transaction. |
| `Promise<String> toString()` | User friendly characteristic name. |
Expand Down
15 changes: 10 additions & 5 deletions src/GattCharacteristic.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ class GattCharacteristic extends EventEmitter {
return Buffer.from(payload)
}

async writeValue (value, offset = 0) {
async writeValue (value, optionsOrOffset = {}) {
if (!Buffer.isBuffer(value)) {
throw new Error('Only buffers can be wrote')
}
const options = {
offset: buildTypedValue('uint16', offset),
type: buildTypedValue('string', 'reliable')

const options = typeof optionsOrOffset === 'number' ? { offset: optionsOrOffset } : optionsOrOffset
const mergedOptions = Object.assign({ offset: 0, type: 'reliable' }, options)

const callOptions = {
offset: buildTypedValue('uint16', mergedOptions.offset),
type: buildTypedValue('string', mergedOptions.type)
}

const { data } = value.toJSON()
await this.helper.callMethod('WriteValue', data, options)
await this.helper.callMethod('WriteValue', data, callOptions)
}

async startNotifications () {
Expand Down
7 changes: 6 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare namespace NodeBle {
getFlags(): Promise<string[]>;
isNotifying(): Promise<boolean>;
readValue(offset?: number): Promise<Buffer>;
writeValue(buffer: Buffer, offset?: number): Promise<void>;
writeValue(buffer: Buffer, optionsOrOffset?: number | WriteValueOptions): Promise<void>;
startNotifications(): Promise<void>;
stopNotifications(): Promise<void>;
toString(): Promise<string>;
Expand Down Expand Up @@ -75,6 +75,11 @@ declare namespace NodeBle {
destroy(): void;
bluetooth: Bluetooth;
};

interface WriteValueOptions {
offset?: number;
type?: 'reliable' | 'request' | 'command';
}
}

export = NodeBle;
Expand Down
19 changes: 18 additions & 1 deletion test/GattCharacteristic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jest.doMock('../src/BusHelper', () => {
}
}
})
const buildTypedValue = require('../src/buildTypedValue')
const GattCharacteristic = require('../src/GattCharacteristic')
const dbus = Symbol('dbus')

Expand All @@ -35,10 +36,26 @@ test('props', async () => {

test('read/write', async () => {
const characteristic = new GattCharacteristic(dbus, 'hci0', 'dev_00_00_00_00_00_00', 'characteristic0006', 'char008')
const writeValueOptions = (offset = 0, type = 'reliable') => {
return { offset: buildTypedValue('uint16', offset), type: buildTypedValue('string', type) }
}

await expect(characteristic.writeValue('not_a_buffer')).rejects.toThrow('Only buffers can be wrote')

await expect(characteristic.writeValue(Buffer.from('hello'), 5)).resolves.toBeUndefined()
expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), writeValueOptions(5))

await expect(characteristic.writeValue(Buffer.from('hello'))).resolves.toBeUndefined()
expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), expect.anything())
expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), writeValueOptions())

await expect(characteristic.writeValue(Buffer.from('hello'), { type: 'command' })).resolves.toBeUndefined()
expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), writeValueOptions(0, 'command'))

await expect(characteristic.writeValue(Buffer.from('hello'), { offset: 9, type: 'request' })).resolves.toBeUndefined()
expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), writeValueOptions(9, 'request'))

await expect(characteristic.writeValue(Buffer.from('hello'), 'incorrect argument')).resolves.toBeUndefined()
expect(characteristic.helper.callMethod).toHaveBeenCalledWith('WriteValue', expect.anything(), writeValueOptions())

characteristic.helper.callMethod.mockResolvedValueOnce([255, 100, 0])
await expect(characteristic.readValue()).resolves.toEqual(Buffer.from([255, 100, 0]))
Expand Down