Skip to content

Commit

Permalink
fix: Handle Huddly BASE devices properly
Browse files Browse the repository at this point in the history
Since there is no transport implementation for Huddly BASE adapter, we
need to make sure that device-api-usb exits immediately when such device
is sent over for usb transport validation.

Also refactoring the usage of huddly VID and PID by referencing their
values from SDK instead of hardcoding them here.
  • Loading branch information
brikendr committed Aug 25, 2021
1 parent d177baa commit fc26aaa
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
12 changes: 10 additions & 2 deletions src/index.ts
Expand Up @@ -7,6 +7,11 @@ import IDeviceDiscovery from '@huddly/sdk/lib/src/interfaces/iDeviceDiscovery';
import DeviceApiOpts from '@huddly/sdk/lib/src/interfaces/IDeviceApiOpts';
import DeviceDiscoveryManager from './manager';
import Logger from '@huddly/sdk/lib/src/utilitis/logger';
import {
HUDDLY_GO_PID,
HUDDLY_L1_PID,
HUDDLY_BASE_PID,
} from '@huddly/sdk/lib/src/components/device/factory';

export default class HuddlyDeviceAPIUSB implements IHuddlyDeviceAPI {
eventEmitter: EventEmitter;
Expand All @@ -32,8 +37,11 @@ export default class HuddlyDeviceAPIUSB implements IHuddlyDeviceAPI {
}

async getValidatedTransport(device): Promise<ITransport> {
if (device.productId === 0x11 || device.productId === 3e9) {
Logger.warn('HLink is not supported for this Huddly device', 'Device API USB');
if ([HUDDLY_GO_PID, HUDDLY_L1_PID, HUDDLY_BASE_PID].includes(device.productId)) {
Logger.warn(
`HLink is not supported for Huddly device with PID ${device.productId}`,
'Device API USB'
);
return undefined;
}
try {
Expand Down
6 changes: 3 additions & 3 deletions src/manager.ts
Expand Up @@ -2,9 +2,9 @@ import BulkUsb, { BulkUsbDevice } from './bulkusbdevice';
import EventEmitter from 'events';
import IDeviceDiscovery from '@huddly/sdk/lib/src/interfaces/iDeviceDiscovery';
import Logger from '@huddly/sdk/lib/src/utilitis/logger';
import { HUDDLY_VID } from '@huddly/sdk/lib/src/components/device/factory';

export default class DeviceDiscoveryManager implements IDeviceDiscovery {
readonly HUDDLY_VID: number = 0x2bd9;
private attachedDevices: Array<any> = [];
eventEmitter: EventEmitter;
pollInterval: any;
Expand All @@ -31,7 +31,7 @@ export default class DeviceDiscoveryManager implements IDeviceDiscovery {
discoverCameras(): void {}

private deviceAttached(attachedDevice): void {
if (attachedDevice.vid !== this.HUDDLY_VID) {
if (attachedDevice.vid !== HUDDLY_VID) {
return;
}
const newDevice = this.getDeviceObject(attachedDevice);
Expand All @@ -42,7 +42,7 @@ export default class DeviceDiscoveryManager implements IDeviceDiscovery {
}

private deviceDetached(removedDevice): void {
if (removedDevice.vid !== this.HUDDLY_VID) {
if (removedDevice.vid !== HUDDLY_VID) {
return;
}
this.attachedDevices = this.attachedDevices.filter((d) => !removedDevice.equals(d));
Expand Down
3 changes: 1 addition & 2 deletions tests/bulkusbdeviceSingleton.spec.ts
Expand Up @@ -136,8 +136,7 @@ describe('BulkUsbDeviceSingleton', () => {
});
// Two list loops
await sleep(251);
dummyCpp.listDevices.yields([
]);
dummyCpp.listDevices.yields([]);
await sleep(251);
expect(detachSpy).to.have.been.calledOnce;
});
Expand Down
10 changes: 5 additions & 5 deletions tests/manager.spec.ts
Expand Up @@ -4,7 +4,7 @@ import sinonChai from 'sinon-chai';
import BulkUsb from './../src/bulkusbdevice';
import DeviceDiscoveryManager from './../src/manager';
import { EventEmitter } from 'events';
import { doesNotReject } from 'assert';
import { HUDDLY_VID } from '@huddly/sdk/lib/src/components/device/factory';

chai.should();
chai.use(sinonChai);
Expand Down Expand Up @@ -150,7 +150,7 @@ describe('HuddlyUsbDeviceManager', () => {
done();
});
devicemanager.registerForHotplugEvents(emitter);
attachStub.callArgWith(0, { id: '123', serialNumber: 'SRL123', pid: 0x21, vid: devicemanager.HUDDLY_VID, onDetach: () => {}});
attachStub.callArgWith(0, { id: '123', serialNumber: 'SRL123', pid: 0x21, vid: HUDDLY_VID, onDetach: () => {}});
});

it('should fire detach events for all undiscovered cached cameras', (done) => {
Expand All @@ -159,8 +159,8 @@ describe('HuddlyUsbDeviceManager', () => {
done();
});
devicemanager.registerForHotplugEvents(emitter);
attachStub.callArgWith(0, { id: '123', serialNumber: 'SRL123', pid: 0x21, vid: devicemanager.HUDDLY_VID, onDetach: (cb) => {
cb({ id: '456', serialNumber: 'SRL456', pid: 0x21, vid: devicemanager.HUDDLY_VID });
attachStub.callArgWith(0, { id: '123', serialNumber: 'SRL123', pid: 0x21, vid: HUDDLY_VID, onDetach: (cb) => {
cb({ id: '456', serialNumber: 'SRL456', pid: 0x21, vid: HUDDLY_VID });
}});

});
Expand All @@ -173,7 +173,7 @@ describe('HuddlyUsbDeviceManager', () => {
* This device will serve as a detached device.
*/
listDeviceStub = sinon.stub(BulkUsb, 'listDevices').resolves([
{ id: '123', serialNumber: 'SRL123', pid: 0x21, vid: devicemanager.HUDDLY_VID, onDetach: (cb) => {} },
{ id: '123', serialNumber: 'SRL123', pid: 0x21, vid: HUDDLY_VID, onDetach: (cb) => {} },
]);
});

Expand Down

0 comments on commit fc26aaa

Please sign in to comment.