Skip to content

Commit

Permalink
fix: lint issues within new features
Browse files Browse the repository at this point in the history
  • Loading branch information
stoprocent committed Oct 25, 2023
1 parent 1e0b643 commit d9b60eb
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 80 deletions.
38 changes: 19 additions & 19 deletions examples/uart-bind-params.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
const noble = require('../with-custom-binding')
const noble = require('../with-custom-binding');

// Needs to export env: BLUETOOTH_HCI_SOCKET_FORCE_UART=1

const noble_uart_a = noble ({ bindParams: { uart: { port: "/dev/tty.usbmodem1..." } } } )
const noble_uart_b = noble ({ bindParams: { uart: { port: "/dev/tty.usbmodem2..." } } } )
const nobleUartA = noble({ bindParams: { uart: { port: '/dev/tty.usbmodem1...' } } });
const nobleUartB = noble({ bindParams: { uart: { port: '/dev/tty.usbmodem2...' } } });

noble_uart_a.on('discover', peripheral => {
console.log("UART A", peripheral.address)
})
nobleUartA.on('discover', peripheral => {
console.log('UART A', peripheral.address);
});

noble_uart_b.on('discover', peripheral => {
console.log("UART B", peripheral.address)
})
nobleUartB.on('discover', peripheral => {
console.log('UART B', peripheral.address);
});

noble_uart_a.on('stateChange', state => {
if(state === 'poweredOn') {
noble_uart_a.setAddress("00:11:22:33:44:01");
noble_uart_a.startScanning()
}
nobleUartA.on('stateChange', state => {
if (state === 'poweredOn') {
nobleUartA.setAddress('00:11:22:33:44:01');
nobleUartA.startScanning();
}
});

noble_uart_b.on('stateChange', state => {
if(state === 'poweredOn') {
noble_uart_b.setAddress("00:11:22:33:44:02");
noble_uart_b.startScanning()
}
nobleUartB.on('stateChange', state => {
if (state === 'poweredOn') {
nobleUartB.setAddress('00:11:22:33:44:02');
nobleUartB.startScanning();
}
});
10 changes: 5 additions & 5 deletions lib/hci-socket/hci.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const Hci = function (options) {
this._isDevUp = null;
this._isExtended = 'extended' in options && options.extended;
this._state = null;
this._bindParams = 'bindParams' in options ? options.bindParams : undefined
this._bindParams = 'bindParams' in options ? options.bindParams : undefined;

this._handleBuffers = {};

Expand Down Expand Up @@ -222,13 +222,13 @@ Hci.prototype.setCodedPhySupport = function () {

Hci.prototype.setAddress = function (address) {
// Command
const addr_cmd = vendorSpecific.setAddressCmd(this._manufacturer, address);
const addrCmd = vendorSpecific.setAddressCmd(this._manufacturer, address);

if (addr_cmd !== null && Buffer.isBuffer(addr_cmd)) {
if (addrCmd !== null && Buffer.isBuffer(addrCmd)) {
// Make Command Buffer
const cmd = Buffer.alloc(1 + addr_cmd.byteLength);
const cmd = Buffer.alloc(1 + addrCmd.byteLength);
cmd.writeUInt8(HCI_COMMAND_PKT, 0);
addr_cmd.copy(cmd, 1);
addrCmd.copy(cmd, 1);

debug(`set address - writing: ${cmd.toString('hex')}`);
this._socket.write(cmd);
Expand Down
74 changes: 39 additions & 35 deletions lib/hci-socket/vs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ const OCF_MRVL_WRITE_BD_ADDR = 0x0022;
const OCF_ERICSSON_STORE_IN_FLASH = 0x0022;
const ERICSSON_STORE_IN_FLASH_CP_SIZE = 0xFF;

function parseAddress(address) {
function parseAddress (address) {
// Parse MAC Address as in 00:00:00:00:00:00 into Buffer (needs to reverse byte order)
let macAddress = Buffer.from(address.split(':').reverse().join(''), 'hex');
const macAddress = Buffer.from(address.split(':').reverse().join(''), 'hex');

if (Buffer.isBuffer(macAddress) && macAddress.byteLength !== 6) {
throw new Error("Invalid MAC Address. Should be formated as 00:00:00:00:00:00 string.");
throw new Error('Invalid MAC Address. Should be formated as 00:00:00:00:00:00 string.');
}

return macAddress;
}

function csr_write_bd_addr(address) {
// eslint-disable-next-line camelcase
function csr_write_bd_addr (address) {
// Parse MAC Address
const macAddress = parseAddress(address);

Expand All @@ -32,15 +33,15 @@ function csr_write_bd_addr(address) {
}

// Base command
const base = Buffer.from([
const base = Buffer.from([
0x02, 0x00, 0x0c, 0x00, 0x11, 0x47, 0x03, 0x70,
0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
]);

// Command
const cmd = Buffer.alloc(3 + base.byteLength);

cmd.writeUInt16LE(0x00 | OGF_VENDOR_CMD << 10, 0);
cmd.writeUInt8(0xC2, 2);

Expand All @@ -57,15 +58,16 @@ function csr_write_bd_addr(address) {
return cmd;
}

function ericsson_store_in_flash(user_id, data) {
// eslint-disable-next-line camelcase
function ericsson_store_in_flash (user_id, data) {
// Check Data
if (Buffer.isBuffer(data) === false || data.byteLength > OCF_ERICSSON_STORE_IN_FLASH - 2) {
return null;
}

// Command
const cmd = Buffer.alloc(3 + ERICSSON_STORE_IN_FLASH_CP_SIZE);

cmd.writeUInt16LE(OCF_ERICSSON_STORE_IN_FLASH | OGF_VENDOR_CMD << 10, 0);
cmd.writeUInt8(ERICSSON_STORE_IN_FLASH_CP_SIZE, 2);
cmd.writeUInt8(user_id, 3); // user_id
Expand All @@ -75,28 +77,30 @@ function ericsson_store_in_flash(user_id, data) {
return cmd;
}

function st_write_bd_addr(address) {
// eslint-disable-next-line camelcase
function st_write_bd_addr (address) {
// Parse MAC Address
const macAddress = parseAddress(address);

if (macAddress === null) {
return null;
return null;
}

return ericsson_store_in_flash(0xFE, macAddress);
}

function mrvl_write_bd_addr(address) {
// eslint-disable-next-line camelcase
function mrvl_write_bd_addr (address) {
// Parse MAC Address
const macAddress = parseAddress(address);

if (macAddress === null) {
return null;
return null;
}

// Command
const cmd = Buffer.alloc(11);

cmd.writeUInt16LE(OCF_MRVL_WRITE_BD_ADDR | OGF_VENDOR_CMD << 10, 0);
cmd.writeUInt8(0x08, 2);
cmd.writeUInt8(0xFE, 3); // parameter_id
Expand All @@ -106,26 +110,26 @@ function mrvl_write_bd_addr(address) {
return cmd;
}

function write_common_bd_addr(OCF_VS_WRITE_BD_ADDR) {
// eslint-disable-next-line camelcase
function write_common_bd_addr (OCF_VS_WRITE_BD_ADDR) {
// Return a function
return (address) => {

// Parse MAC Address
const macAddress = parseAddress(address);

if (macAddress === null) {
return null;
}

// Command
const cmd = Buffer.alloc(9);

cmd.writeUInt16LE(OCF_VS_WRITE_BD_ADDR | OGF_VENDOR_CMD << 10, 0);
cmd.writeUInt8(0x06, 2);
macAddress.copy(cmd, 3); // bdaddr

return cmd;
}
// Parse MAC Address
const macAddress = parseAddress(address);

if (macAddress === null) {
return null;
}

// Command
const cmd = Buffer.alloc(9);

cmd.writeUInt16LE(OCF_VS_WRITE_BD_ADDR | OGF_VENDOR_CMD << 10, 0);
cmd.writeUInt8(0x06, 2);
macAddress.copy(cmd, 3); // bdaddr

return cmd;
};
}

const vendors = new Map();
Expand All @@ -144,9 +148,9 @@ module.exports = {
// Vendor Specific Set Address
setAddressCmd: (manufacturer, address) => {
const generateCommand = vendors.get(manufacturer);
if (typeof generateCommand === 'function' ) {
if (typeof generateCommand === 'function') {
return generateCommand(address) || null;
}
return null;
}
}
};
5 changes: 2 additions & 3 deletions lib/noble.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ Noble.prototype.onScanParametersSet = function () {
Noble.prototype.setAddress = function (address) {
if (this._bindings.setAddress) {
this._bindings.setAddress(address);
}
else {
} else {
this.emit('warning', 'current binding does not implement setAddress method.');
}
}
};

const startScanning = function (serviceUuids, allowDuplicates, callback) {
if (typeof serviceUuids === 'function') {
Expand Down
27 changes: 13 additions & 14 deletions test/lib/hci-socket/hci.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,49 +267,48 @@ describe('hci-socket hci', () => {
});

describe('setAddress', () => {

it('should write vendor specific (Linux Foundation) command based on read local version response', () => {
it('should write vendor specific (Linux Foundation) command based on read local version response', () => {
hci.readBdAddr = sinon.spy();
hci.setScanEnabled = sinon.spy();
hci.setScanParameters = sinon.spy();

const cmd = 4097;
const status = 0;
// hciVer=12, hciRev=0, lmpVer=12, manufacturer=1521, lmpSubVer=65535
const result = Buffer.from([0x0C, 0x00, 0x00, 0x0C, 0xF1, 0x05, 0xFF, 0xFF]);

hci.processCmdCompleteEvent(cmd, status, result);
hci.setAddress("11:22:33:44:55:66");

hci.setAddress('11:22:33:44:55:66');
assert.calledOnceWithExactly(hci._socket.write, Buffer.from([0x01, 0x06, 0xfc, 0x06, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11]));
});

it('should write vendor specific (Ericsson) command based on manufacturer value (', () => {
it('should write vendor specific (Ericsson) command based on manufacturer value (', () => {
hci._manufacturer = 0;
hci.readBdAddr = sinon.spy();
hci.setAddress("11:22:33:44:55:66");
hci.setAddress('11:22:33:44:55:66');
assert.calledOnceWithExactly(hci._socket.write, Buffer.from([0x01, 0x0d, 0xfc, 0x06, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11]));
});

it('should write vendor specific (Texas Instrument) command based on manufacturer value', () => {
it('should write vendor specific (Texas Instrument) command based on manufacturer value', () => {
hci._manufacturer = 13;
hci.readBdAddr = sinon.spy();
hci.setAddress("11:22:33:44:55:66");
hci.setAddress('11:22:33:44:55:66');
assert.calledOnceWithExactly(hci._socket.write, Buffer.from([0x01, 0x06, 0xfc, 0x06, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11]));
});

it('should write vendor specific (BCM) command based on manufacturer value', () => {
it('should write vendor specific (BCM) command based on manufacturer value', () => {
hci._manufacturer = 15;
hci.readBdAddr = sinon.spy();
hci.setAddress("11:22:33:44:55:66");
hci.setAddress('11:22:33:44:55:66');
assert.calledOnceWithExactly(hci._socket.write, Buffer.from([0x01, 0x01, 0xfc, 0x06, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11]));
});

it('should not write vendor specific command', () => {
hci.setAddress("11:22:33:44:55:66");
it('should not write vendor specific command', () => {
hci.setAddress('11:22:33:44:55:66');
assert.notCalled(hci._socket.write);
});
})
});

describe('setLeEventMask', () => {
it('should setLeEventMask', () => {
Expand Down
7 changes: 3 additions & 4 deletions test/lib/hci-socket/vs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ const { assert } = sinon;
const vendorSpecific = proxyquire('../../../lib/hci-socket/vs', { os: fakeOs });

describe('hci-socket vs', () => {

afterEach(() => {
sinon.reset();
});

describe('parseAddress', () => {
it('should convert to Buffer', () => {
assert.match(vendorSpecific.setAddressCmd(0, "00:11:22:33:44:55").slice(3), Buffer.from([0x55, 0x44, 0x33, 0x22, 0x11, 0x00]))
assert.match(vendorSpecific.setAddressCmd(0, '00:11:22:33:44:55').slice(3), Buffer.from([0x55, 0x44, 0x33, 0x22, 0x11, 0x00]));
});

it('should not convert to Buffer and throw an Error', () => {
should.throws(function() {
vendorSpecific.setAddressCmd(0, "00:11:22:33:44")
should.throws(function () {
vendorSpecific.setAddressCmd(0, '00:11:22:33:44');
});
});
});
Expand Down

0 comments on commit d9b60eb

Please sign in to comment.