Skip to content

Commit

Permalink
Moved to const enum for pretty much every enum definition.
Browse files Browse the repository at this point in the history
Fixed: internal encryption stuff an TLV types are not exported anymore
  • Loading branch information
Supereg committed Apr 18, 2020
1 parent 14e3270 commit e7e7d90
Show file tree
Hide file tree
Showing 18 changed files with 260 additions and 189 deletions.
2 changes: 1 addition & 1 deletion @types/bonjour-hap.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare module 'bonjour-hap' {

export enum Protocols {
export const enum Protocols {
TCP = 'tcp',
UDP = 'udp',
}
Expand Down
16 changes: 13 additions & 3 deletions src/accessories/Camera_accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
uuid,
VideoInfo
} from "..";
import {ChildProcess, spawn} from "child_process";
import { ChildProcess, spawn } from "child_process";
import ip from "ip";

const cameraUUID = uuid.generate('hap-nodejs:accessories:ip-camera');
Expand Down Expand Up @@ -167,8 +167,18 @@ class ExampleCamera implements CameraStreamingDelegate {
`-c:v libx264 -pix_fmt yuv420p -r ${fps} -an -sn -dn -b:v ${maxBitrate}k -bufsize ${2*maxBitrate}k -maxrate ${maxBitrate}k ` +
`-payload_type ${payloadType} -ssrc ${ssrc} -f rtp `; // -profile:v ${profile} -level:v ${level}

if (cryptoSuite !== SRTPCryptoSuites.NONE) { // actually ffmpeg just supports AES_CM_128_HMAC_SHA1_80
videoffmpegCommand += `-srtp_out_suite ${SRTPCryptoSuites[cryptoSuite]} -srtp_out_params ${videoSRTP} s`;
if (cryptoSuite !== SRTPCryptoSuites.NONE) {
let suite: string;
switch (cryptoSuite) {
case SRTPCryptoSuites.AES_CM_128_HMAC_SHA1_80: // actually ffmpeg just supports AES_CM_128_HMAC_SHA1_80
suite = "AES_CM_128_HMAC_SHA1_80";
break;
case SRTPCryptoSuites.AES_CM_256_HMAC_SHA1_80:
suite = "AES_CM_256_HMAC_SHA1_80";
break;
}

videoffmpegCommand += `-srtp_out_suite ${suite} -srtp_out_params ${videoSRTP} s`;
}

videoffmpegCommand += `rtp://${address}:${videoPort}?rtcpport=${videoPort}&localrtcpport=${videoPort}&pkt_size=${mtu}`;
Expand Down
6 changes: 3 additions & 3 deletions src/accessories/gstreamer-audioProducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import {

const debug = createDebug("Remote:GStreamer");

enum AudioType {
const enum AudioType {
GENERIC = 2049,
VOICE = 2048
}

enum Bandwidth {
const enum Bandwidth {
NARROW_BAND = 1101,
MEDIUM_BAND = 1102,
WIDE_BAND = 1103,
Expand All @@ -27,7 +27,7 @@ enum Bandwidth {
AUTO = -1000
}

enum BitrateType {
const enum BitrateType {
CONSTANT = 0,
VARIABLE = 1,
}
Expand Down
3 changes: 0 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ export * from './lib/gen';
export * from './lib/datastream';
export * from './lib/controller';

export * from './lib/util/chacha20poly1305';
export * from './lib/util/clone';
export * from './lib/util/encryption';
export * from './lib/util/hkdf';
export * from './lib/util/once';
export * from './lib/util/tlv';

Expand Down
50 changes: 28 additions & 22 deletions src/lib/Accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const debug = createDebug('Accessory');
const MAX_ACCESSORIES = 149; // Maximum number of bridged accessories per bridge.

// Known category values. Category is a hint to iOS clients about what "type" of Accessory this represents, for UI only.
export enum Categories {
export const enum Categories {
OTHER = 1,
BRIDGE = 2,
FAN = 3,
Expand All @@ -69,20 +69,20 @@ export enum Categories {
IP_CAMERA = 17, //Added to conform to HAP naming
VIDEO_DOORBELL = 18,
AIR_PURIFIER = 19,
AIR_HEATER = 20, //Not in HAP Spec
AIR_CONDITIONER = 21, //Not in HAP Spec
AIR_HUMIDIFIER = 22, //Not in HAP Spec
AIR_DEHUMIDIFIER = 23, // Not in HAP Spec
AIR_HEATER = 20,
AIR_CONDITIONER = 21,
AIR_HUMIDIFIER = 22,
AIR_DEHUMIDIFIER = 23,
APPLE_TV = 24,
HOMEPOD = 25, // HomePod
HOMEPOD = 25,
SPEAKER = 26,
AIRPORT = 27,
SPRINKLER = 28,
FAUCET = 29,
SHOWER_HEAD = 30,
TELEVISION = 31,
TARGET_CONTROLLER = 32, // Remote Control
ROUTER = 33, // HomeKit enabled router
ROUTER = 33,
AUDIO_RECEIVER = 34,
}

Expand Down Expand Up @@ -110,7 +110,7 @@ export interface ControllerContext {
}


export enum AccessoryEventTypes {
export const enum AccessoryEventTypes {
IDENTIFY = "identify",
LISTENING = "listening",
SERVICE_CONFIGURATION_CHANGE = "service-configurationChange",
Expand Down Expand Up @@ -149,7 +149,7 @@ export type ServiceCharacteristicChange = CharacteristicChange & {
service: Service;
};

export enum ResourceTypes {
export const enum ResourceTypes {
IMAGE = 'image',
}

Expand All @@ -160,7 +160,7 @@ export type Resource = {
'resource-type': ResourceTypes;
}

enum WriteRequestState {
const enum WriteRequestState {
REGULAR_REQUEST,
TIMED_WRITE_AUTHENTICATED,
TIMED_WRITE_REJECTED
Expand Down Expand Up @@ -197,7 +197,16 @@ type HandleSetCharacteristicsCallback = NodeCallback<CharacteristicData[]>;
*/
export class Accessory extends EventEmitter<Events> {

static Categories = Categories;
/**
* @deprecated won't be updated anymore. Please use the Categories const enum above. Scheduled to be removed in 2021-06.
*/
static Categories = Object.freeze({
OTHER: 1, BRIDGE: 2, FAN: 3, GARAGE_DOOR_OPENER: 4, LIGHTBULB: 5, DOOR_LOCK: 6, OUTLET: 7, SWITCH: 8, THERMOSTAT: 9, SENSOR: 10,
ALARM_SYSTEM: 11, SECURITY_SYSTEM: 11, DOOR: 12, WINDOW: 13, WINDOW_COVERING: 14, PROGRAMMABLE_SWITCH: 15, RANGE_EXTENDER: 16,
CAMERA: 17, IP_CAMERA: 17, VIDEO_DOORBELL: 18, AIR_PURIFIER: 19, AIR_HEATER: 20, AIR_CONDITIONER: 21, AIR_HUMIDIFIER: 22,
AIR_DEHUMIDIFIER: 23, APPLE_TV: 24, HOMEPOD: 25, SPEAKER: 26, AIRPORT: 27, SPRINKLER: 28, FAUCET: 29, SHOWER_HEAD: 30,
TELEVISION: 31, TARGET_CONTROLLER: 32, ROUTER: 33, AUDIO_RECEIVER: 34,
});

// NOTICE: when adding/changing properties, remember to possibly adjust the serialize/deserialize functions
aid: Nullable<number> = null; // assigned by us in assignIDs() or by a Bridge
Expand Down Expand Up @@ -1714,21 +1723,18 @@ export class Accessory extends EventEmitter<Events> {

}

const numberPattern = /^-?\d+$/;

function hapStatus(err: Error) {
let errorValue = Status.SERVICE_COMMUNICATION_FAILURE;

// Validate that the message is a valid HAPServer.Status
let value: number | string = 0; // default if not found or
if (numberPattern.test(err.message)) {
const value = parseInt(err.message);

for( const k in Status ) {
if (Status[k] == err.message)
{
value = err.message;
break;
if (value >= Status.INSUFFICIENT_PRIVILEGES && value <= Status.INSUFFICIENT_AUTHORIZATION) {
errorValue = value;
}
}

if ( value == 0 )
value = Status.SERVICE_COMMUNICATION_FAILURE; // default if not found or 0

return(parseInt(`${value}`));
return errorValue;
}
95 changes: 69 additions & 26 deletions src/lib/Characteristic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { EventEmitter } from './EventEmitter';
import * as HomeKitTypes from './gen';
import { toShortForm } from './util/uuid';

// Known HomeKit formats
export enum Formats {
export const enum Formats {
BOOL = 'bool',
INT = 'int',
FLOAT = 'float',
Expand All @@ -29,31 +28,35 @@ export enum Formats {
DATA = 'data',
TLV8 = 'tlv8',
ARRAY = 'array', //Not in HAP Spec
DICTIONARY = 'dict' //Not in HAP Spec
DICTIONARY = 'dict', //Not in HAP Spec
}

// Known HomeKit unit types
export enum Units {
// HomeKit only defines Celsius, for Fahrenheit, it requires iOS app to do the conversion.
CELSIUS = 'celsius',
export const enum Units {
CELSIUS = 'celsius', // celsius is the only temperature unit, conversion to fahrenheit is done on the iOS device
PERCENTAGE = 'percentage',
ARC_DEGREE = 'arcdegrees',
LUX = 'lux',
SECONDS = 'seconds'
SECONDS = 'seconds',
}

// Known HomeKit permission types
export enum Perms {
READ = 'pr', //Kept for backwards compatability
PAIRED_READ = 'pr', //Added to match HAP's terminology
WRITE = 'pw', //Kept for backwards compatability
PAIRED_WRITE = 'pw', //Added to match HAP's terminology
NOTIFY = 'ev', //Kept for backwards compatability
EVENTS = 'ev', //Added to match HAP's terminology
export const enum Perms {
/**
* @deprecated replaced by {@link PAIRED_READ}. Kept for backwards compatibility.
*/
READ = 'pr',
/**
* @deprecated replaced by {@link PAIRED_WRITE}. Kept for backwards compatibility.
*/
WRITE = 'pw',
PAIRED_READ = 'pr',
PAIRED_WRITE = 'pw',
NOTIFY = 'ev', // both terms are used in hap spec
EVENTS = 'ev',
ADDITIONAL_AUTHORIZATION = 'aa',
TIMED_WRITE = 'tw',
HIDDEN = 'hd',
WRITE_RESPONSE = 'wr'
WRITE_RESPONSE = 'wr',
}

export interface CharacteristicProps {
Expand All @@ -72,7 +75,7 @@ export interface CharacteristicProps {
adminOnlyAccess?: Access[];
}

export enum Access {
export const enum Access {
READ = 0x00,
WRITE = 0x01,
NOTIFY = 0x02
Expand All @@ -86,7 +89,7 @@ export interface SerializedCharacteristic {
eventOnlyCharacteristic: boolean,
}

export enum CharacteristicEventTypes {
export const enum CharacteristicEventTypes {
GET = "get",
SET = "set",
SUBSCRIBE = "subscribe",
Expand All @@ -105,11 +108,6 @@ type Events = {
[CharacteristicEventTypes.UNSUBSCRIBE]: VoidCallback;
}

/**
* @deprecated Use CharacteristicEventTypes instead
*/
export type EventCharacteristic = CharacteristicEventTypes.GET | CharacteristicEventTypes.SET | CharacteristicEventTypes.SUBSCRIBE | CharacteristicEventTypes.UNSUBSCRIBE | CharacteristicEventTypes.CHANGE;

/**
* Characteristic represents a particular typed variable that can be assigned to a Service. For instance, a
* "Hue" Characteristic might store a 'float' value of type 'arcdegrees'. You could add the Hue Characteristic
Expand Down Expand Up @@ -143,9 +141,54 @@ export type EventCharacteristic = CharacteristicEventTypes.GET | CharacteristicE
*/
export class Characteristic extends EventEmitter<Events> {

static Formats = Formats;
static Units = Units;
static Perms = Perms;
/**
* @deprecated won't be updated anymore. Please use the Formats const enum above. Scheduled to be removed in 2021-06.
*/
static Formats = Object.freeze({
BOOL: 'bool',
INT: 'int',
FLOAT: 'float',
STRING: 'string',
UINT8: 'uint8',
UINT16: 'uint16',
UINT32: 'uint32',
UINT64: 'uint64',
DATA: 'data',
TLV8: 'tlv8',
ARRAY: 'array', //Not in HAP Spec
DICTIONARY: 'dict' //Not in HAP Spec
});
/**
* @deprecated won't be updated anymore. Please use the Units const enum above. Scheduled to be removed in 2021-06.
*/
static Units = Object.freeze({
CELSIUS: "celsius",
PERCENTAGE: "percentage",
ARC_DEGREE: "arcdegrees",
LUX: "lux",
SECONDS: "seconds",
});
/**
* @deprecated won't be updated anymore. Please use the Perms const enum above. Scheduled to be removed in 2021-06.
*/
static Perms = Object.freeze({
/**
* @deprecated replaced by {@link PAIRED_READ}. Kept for backwards compatibility.
*/
READ: 'pr',
/**
* @deprecated replaced by {@link PAIRED_WRITE}. Kept for backwards compatibility.
*/
WRITE: 'pw',
PAIRED_READ: 'pr',
PAIRED_WRITE: 'pw',
NOTIFY: 'ev',
EVENTS: 'ev',
ADDITIONAL_AUTHORIZATION: 'aa',
TIMED_WRITE: 'tw',
HIDDEN: 'hd',
WRITE_RESPONSE: 'wr'
});

static AccessControlLevel: typeof HomeKitTypes.Generated.AccessControlLevel;
static AccessoryFlags: typeof HomeKitTypes.Generated.AccessoryFlags;
Expand Down
Loading

0 comments on commit e7e7d90

Please sign in to comment.