Skip to content

Commit

Permalink
try again with dbus-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
bwp91 committed Aug 29, 2024
1 parent 3c5bb99 commit c96d569
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 91 deletions.
47 changes: 44 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@homebridge/ciao": "^1.3.1",
"@homebridge/dbus-native": "^0.6.0",
"bonjour-hap": "^3.8.0",
"dbus-ts": "^0.0.7",
"debug": "^4.3.6",
"fast-srp-hap": "^2.0.4",
"futoin-hkdf": "^1.5.3",
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1248,10 +1248,10 @@ export class Accessory extends EventEmitter {
})
break
case MDNSAdvertiser.AVAHI:
this._advertiser = new AvahiAdvertiser(this._accessoryInfo)
this._advertiser = await AvahiAdvertiser.create(this._accessoryInfo)
break
case MDNSAdvertiser.RESOLVED:
this._advertiser = new ResolvedAdvertiser(this._accessoryInfo)
this._advertiser = await ResolvedAdvertiser.create(this._accessoryInfo)
break
}
debug('[%s] Advertiser created', this.displayName)
Expand Down
88 changes: 42 additions & 46 deletions src/lib/Advertiser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// <reference path="../../@types/bonjour-hap.d.ts" />
import type { CiaoService, MDNSServerOptions, Responder, ServiceTxt } from '@homebridge/ciao'
import type { InterfaceName, IPAddress } from '@homebridge/ciao/lib/NetworkManager.js'
import type { DBusInterface, MessageBus } from '@homebridge/dbus-native'
import type { BonjourHAP, BonjourHAPService } from 'bonjour-hap'
import type { DBusInterface, MessageBus } from 'dbus-ts'

import type { AccessoryInfo } from './model/AccessoryInfo'

Expand All @@ -12,8 +12,8 @@ import { createHash } from 'node:crypto'
import { EventEmitter } from 'node:events'

import { getResponder, ServiceEvent, ServiceType } from '@homebridge/ciao'
import { systemBus } from '@homebridge/dbus-native'
import bonjour from 'bonjour-hap'
import { systemBus } from 'dbus-ts'
import createDebug from 'debug'

import { PromiseTimeout } from './util/promise-utils.js'
Expand Down Expand Up @@ -336,24 +336,20 @@ export class DBusInvokeError extends Error {
}
}

function dbusInvoke(bus: MessageBus, destination: string, path: string, dbusInterface: string, member: string, others?: any): Promise<any> {
return new Promise((resolve, reject) => {
const command = {
destination,
path,
interface: dbusInterface,
member,
...(others || {}),
}
async function dbusInvoke(bus: MessageBus, destination: string, path: string, dbusInterface: string, member: string, others?: any): Promise<any> {
const command = {
destination,
path,
interface: dbusInterface,
member,
...(others || {}),
}

bus.invoke(command, (err, result) => {
if (err) {
reject(new DBusInvokeError(err))
} else {
resolve(result)
}
})
})
try {
return await bus.invoke(command)
} catch (err) {
throw new DBusInvokeError(err)
}
}

/**
Expand Down Expand Up @@ -393,18 +389,20 @@ export class AvahiAdvertiser extends EventEmitter implements Advertiser {

private readonly stateChangeHandler: (state: AvahiServerState) => void

constructor(accessoryInfo: AccessoryInfo) {
constructor(accessoryInfo: AccessoryInfo, bus: MessageBus) {
super()
this.accessoryInfo = accessoryInfo
this.setupHash = CiaoAdvertiser.computeSetupHash(accessoryInfo)

debug(`Preparing Advertiser for '${this.accessoryInfo.displayName}' using Avahi backend!`)

this.bus = systemBus()

this.bus = bus
this.stateChangeHandler = this.handleStateChangedEvent.bind(this)
}

static async create(accessoryInfo: AccessoryInfo): Promise<AvahiAdvertiser> {
const bus = await systemBus()
return new AvahiAdvertiser(accessoryInfo, bus)
}

private createTxt(): Array<Buffer> {
return Object
.entries(CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash))
Expand Down Expand Up @@ -518,7 +516,7 @@ export class AvahiAdvertiser extends EventEmitter implements Advertiser {

public static async isAvailable(): Promise<boolean> {
debug('Checking Avahi/DBus availability...')
const bus = systemBus()
const bus = await systemBus()
debug('Avahi/DBus connection established!')

try {
Expand Down Expand Up @@ -548,8 +546,8 @@ export class AvahiAdvertiser extends EventEmitter implements Advertiser {
}
}

private static avahiInvoke(bus: MessageBus, path: string, dbusInterface: string, member: string, others?: any): Promise<any> {
return dbusInvoke(
private static async avahiInvoke(bus: MessageBus, path: string, dbusInterface: string, member: string, others?: any): Promise<any> {
return await dbusInvoke(
bus,
'org.freedesktop.Avahi',
path,
Expand All @@ -559,18 +557,13 @@ export class AvahiAdvertiser extends EventEmitter implements Advertiser {
)
}

private static avahiInterface(bus: MessageBus, dbusInterface: string): Promise<DBusInterface> {
return new Promise((resolve, reject) => {
bus
.getService('org.freedesktop.Avahi')
.getInterface('/', `org.freedesktop.Avahi.${dbusInterface}`, (error, iface) => {
if (error || !iface) {
reject(error ?? new Error('Interface not present!'))
} else {
resolve(iface)
}
})
})
private static async avahiInterface(bus: MessageBus, dbusInterface: string): Promise<DBusInterface> {
const service = bus.getService('org.freedesktop.Avahi')
const iface = await service.getInterface('/', `org.freedesktop.Avahi.${dbusInterface}`)
if (!iface) {
throw new Error('Interface not present!')
}
return iface
}
}

Expand All @@ -597,16 +590,19 @@ export class ResolvedAdvertiser extends EventEmitter implements Advertiser {
private bus?: MessageBus
private path?: string

constructor(accessoryInfo: AccessoryInfo) {
constructor(accessoryInfo: AccessoryInfo, bus: MessageBus) {
super()
this.accessoryInfo = accessoryInfo
this.setupHash = CiaoAdvertiser.computeSetupHash(accessoryInfo)

this.bus = systemBus()

this.bus = bus
debug(`Preparing Advertiser for '${this.accessoryInfo.displayName}' using systemd-resolved backend!`)
}

static async create(accessoryInfo: AccessoryInfo): Promise<ResolvedAdvertiser> {
const bus = await systemBus()
return new ResolvedAdvertiser(accessoryInfo, bus)
}

private createTxt(): ResolvedServiceTxt {
return Object
.entries(CiaoAdvertiser.createTxt(this.accessoryInfo, this.setupHash))
Expand Down Expand Up @@ -693,7 +689,7 @@ export class ResolvedAdvertiser extends EventEmitter implements Advertiser {
}

public static async isAvailable(): Promise<boolean> {
const bus = systemBus()
const bus = await systemBus()

try {
try {
Expand Down Expand Up @@ -745,8 +741,8 @@ export class ResolvedAdvertiser extends EventEmitter implements Advertiser {
}
}

private static resolvedInvoke(bus: MessageBus, dbusInterface: string, member: string, others?: any): Promise<any> {
return dbusInvoke(
private static async resolvedInvoke(bus: MessageBus, dbusInterface: string, member: string, others?: any): Promise<any> {
return await dbusInvoke(
bus,
'org.freedesktop.resolve1',
'/org/freedesktop/resolve1',
Expand Down
40 changes: 0 additions & 40 deletions src/types/dbus-native.d.ts

This file was deleted.

0 comments on commit c96d569

Please sign in to comment.