@homebridge/hap-nodejs 2.3.1-beta.3 → 2.3.1-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hap.d.ts +3 -0
- package/dist/hap.js +3 -0
- package/dist/hap.js.map +1 -1
- package/dist/lib/HAPServer.d.ts +5 -0
- package/dist/lib/HAPServer.js +23 -10
- package/dist/lib/HAPServer.js.map +1 -1
- package/dist/lib/controller/DoorbellController.d.ts +59 -0
- package/dist/lib/controller/DoorbellController.js +85 -0
- package/dist/lib/controller/DoorbellController.js.map +1 -1
- package/dist/lib/lock/AccessCodeManagement.d.ts +135 -0
- package/dist/lib/lock/AccessCodeManagement.js +328 -0
- package/dist/lib/lock/AccessCodeManagement.js.map +1 -0
- package/dist/lib/lock/NFCAccessManagement.d.ts +200 -0
- package/dist/lib/lock/NFCAccessManagement.js +443 -0
- package/dist/lib/lock/NFCAccessManagement.js.map +1 -0
- package/dist/lib/util/error-status.d.ts +64 -0
- package/dist/lib/util/error-status.js +100 -0
- package/dist/lib/util/error-status.js.map +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { AccessCode as AccessCodeService } from '../definitions/index.js';
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
3
|
+
/**
|
|
4
|
+
* The result of one access code operation, as sent back to the Home app.
|
|
5
|
+
*
|
|
6
|
+
* @group Lock
|
|
7
|
+
*/
|
|
8
|
+
export declare enum AccessCodeResponseStatus {
|
|
9
|
+
SUCCESS = 0,
|
|
10
|
+
UNKNOWN = 1,
|
|
11
|
+
EXCEEDED_MAXIMUM_ALLOWED_ACCESS_CODES = 2,
|
|
12
|
+
TOO_MANY_BULK_OPERATIONS = 3,
|
|
13
|
+
DUPLICATE = 4,
|
|
14
|
+
SMALLER_THAN_MINIMUM_LENGTH = 5,
|
|
15
|
+
LARGER_THAN_MAXIMUM_LENGTH = 6,
|
|
16
|
+
INVALID_CHARACTER = 7,
|
|
17
|
+
INVALID_REQUEST = 8,
|
|
18
|
+
DOES_NOT_EXIST = 9
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @group Lock
|
|
22
|
+
*/
|
|
23
|
+
export declare enum AccessCodeManagementEvent {
|
|
24
|
+
ACCESS_CODES_UPDATED = "update-access-codes"
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* One access code stored on the lock.
|
|
28
|
+
*
|
|
29
|
+
* @group Lock
|
|
30
|
+
*/
|
|
31
|
+
export interface StoredAccessCode {
|
|
32
|
+
/** The number the lock gave this code. The Home app uses it to read, change and remove the code. */
|
|
33
|
+
identifier: number;
|
|
34
|
+
/** The code itself, digits only. */
|
|
35
|
+
accessCode: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* @group Lock
|
|
39
|
+
*/
|
|
40
|
+
export interface AccessCodeManagementOptions {
|
|
41
|
+
/** The shortest code the lock accepts. Default 4. */
|
|
42
|
+
minimumLength?: number;
|
|
43
|
+
/** The longest code the lock accepts. Default 8. */
|
|
44
|
+
maximumLength?: number;
|
|
45
|
+
/** How many codes the lock can hold. Default 16. */
|
|
46
|
+
maximumAccessCodes?: number;
|
|
47
|
+
/** The codes already on the lock, for example the ones saved before a restart. */
|
|
48
|
+
accessCodes?: StoredAccessCode[];
|
|
49
|
+
/** Supply your own instance to sideload the AccessCode service. */
|
|
50
|
+
service?: AccessCodeService;
|
|
51
|
+
}
|
|
52
|
+
export declare interface AccessCodeManagement {
|
|
53
|
+
on(event: 'update-access-codes', listener: (accessCodes: StoredAccessCode[]) => void): this;
|
|
54
|
+
emit(event: 'update-access-codes', accessCodes: StoredAccessCode[]): boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Handles the AccessCode service of a lock with a keypad: the Home app lists, adds, changes and removes the codes
|
|
58
|
+
* people type on the keypad.
|
|
59
|
+
*
|
|
60
|
+
* The codes are kept in memory. Listen for {@link AccessCodeManagementEvent.ACCESS_CODES_UPDATED} to save them and to
|
|
61
|
+
* send them to the lock, and pass the saved ones back as {@link AccessCodeManagementOptions.accessCodes} on the next start.
|
|
62
|
+
*
|
|
63
|
+
* HomeKit does not document this service. The layout comes from Apple's own HomeKit code, and matches the public
|
|
64
|
+
* reverse engineering in https://github.com/rednblkx/homekit-lock-access-code.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* // an accessory restored from the cache already has the service: hand it over, so no second one is added
|
|
69
|
+
* const cachedService = accessory.getService(Service.AccessCode);
|
|
70
|
+
* const accessCodes = new AccessCodeManagement({ accessCodes: savedCodes, service: cachedService });
|
|
71
|
+
* if (!cachedService) {
|
|
72
|
+
* accessory.addService(accessCodes.getService());
|
|
73
|
+
* }
|
|
74
|
+
*
|
|
75
|
+
* accessCodes.on(AccessCodeManagementEvent.ACCESS_CODES_UPDATED, (codes) => {
|
|
76
|
+
* // save the codes, and send them to the lock
|
|
77
|
+
* });
|
|
78
|
+
*
|
|
79
|
+
* // when someone types a code on the keypad
|
|
80
|
+
* if (accessCodes.hasAccessCode('1234')) {
|
|
81
|
+
* // unlock
|
|
82
|
+
* }
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @group Lock
|
|
86
|
+
*/
|
|
87
|
+
export declare class AccessCodeManagement extends EventEmitter {
|
|
88
|
+
private readonly accessCodeService;
|
|
89
|
+
private readonly minimumLength;
|
|
90
|
+
private readonly maximumLength;
|
|
91
|
+
private readonly maximumAccessCodes;
|
|
92
|
+
private readonly accessCodes;
|
|
93
|
+
/**
|
|
94
|
+
* A counter which goes up by one whenever the codes change.
|
|
95
|
+
* How the Home app uses the Configuration State characteristic is not confirmed; a change counter is the usual meaning.
|
|
96
|
+
*/
|
|
97
|
+
private configurationState;
|
|
98
|
+
/**
|
|
99
|
+
* Instantiates a new AccessCodeManagement.
|
|
100
|
+
*
|
|
101
|
+
* @param {AccessCodeManagementOptions} options - the limits of the lock, the codes it holds and your own AccessCode service
|
|
102
|
+
*/
|
|
103
|
+
constructor(options?: AccessCodeManagementOptions);
|
|
104
|
+
/**
|
|
105
|
+
* @returns the AccessCode service
|
|
106
|
+
*/
|
|
107
|
+
getService(): AccessCodeService;
|
|
108
|
+
/**
|
|
109
|
+
* @returns the codes stored on the lock, in the order of their identifiers
|
|
110
|
+
*/
|
|
111
|
+
getAccessCodes(): StoredAccessCode[];
|
|
112
|
+
/**
|
|
113
|
+
* Whether a code typed on the keypad is one of the stored codes.
|
|
114
|
+
*
|
|
115
|
+
* @param accessCode - the code which was typed
|
|
116
|
+
*/
|
|
117
|
+
hasAccessCode(accessCode: string): boolean;
|
|
118
|
+
/**
|
|
119
|
+
* This destroys the AccessCodeManagement.
|
|
120
|
+
* It unregisters the SET handler it has associated with the given AccessCode service.
|
|
121
|
+
* It removes all event handlers which were registered to this object.
|
|
122
|
+
*/
|
|
123
|
+
destroy(): void;
|
|
124
|
+
private handleControlPointWrite;
|
|
125
|
+
private read;
|
|
126
|
+
private add;
|
|
127
|
+
private update;
|
|
128
|
+
private remove;
|
|
129
|
+
/**
|
|
130
|
+
* @returns the status for a code which cannot be stored, or `undefined` for a code which can
|
|
131
|
+
*/
|
|
132
|
+
private validate;
|
|
133
|
+
private encodeResponse;
|
|
134
|
+
private setupServiceHandlers;
|
|
135
|
+
}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
3
|
+
import { Characteristic } from '../Characteristic.js';
|
|
4
|
+
import { Service } from '../Service.js';
|
|
5
|
+
import * as tlv from '../util/tlv.js';
|
|
6
|
+
var AccessCodeSupportedConfigurationTypes;
|
|
7
|
+
(function (AccessCodeSupportedConfigurationTypes) {
|
|
8
|
+
AccessCodeSupportedConfigurationTypes[AccessCodeSupportedConfigurationTypes["CHARACTER_SET"] = 1] = "CHARACTER_SET";
|
|
9
|
+
AccessCodeSupportedConfigurationTypes[AccessCodeSupportedConfigurationTypes["MINIMUM_VALUE_LENGTH"] = 2] = "MINIMUM_VALUE_LENGTH";
|
|
10
|
+
AccessCodeSupportedConfigurationTypes[AccessCodeSupportedConfigurationTypes["MAXIMUM_VALUE_LENGTH"] = 3] = "MAXIMUM_VALUE_LENGTH";
|
|
11
|
+
AccessCodeSupportedConfigurationTypes[AccessCodeSupportedConfigurationTypes["MAXIMUM_ACCESS_CODES"] = 4] = "MAXIMUM_ACCESS_CODES";
|
|
12
|
+
})(AccessCodeSupportedConfigurationTypes || (AccessCodeSupportedConfigurationTypes = {}));
|
|
13
|
+
var AccessCodeControlTypes;
|
|
14
|
+
(function (AccessCodeControlTypes) {
|
|
15
|
+
AccessCodeControlTypes[AccessCodeControlTypes["OPERATION_TYPE"] = 1] = "OPERATION_TYPE";
|
|
16
|
+
AccessCodeControlTypes[AccessCodeControlTypes["REQUEST"] = 2] = "REQUEST";
|
|
17
|
+
AccessCodeControlTypes[AccessCodeControlTypes["RESPONSE"] = 3] = "RESPONSE";
|
|
18
|
+
})(AccessCodeControlTypes || (AccessCodeControlTypes = {}));
|
|
19
|
+
var AccessCodeControlRequestTypes;
|
|
20
|
+
(function (AccessCodeControlRequestTypes) {
|
|
21
|
+
AccessCodeControlRequestTypes[AccessCodeControlRequestTypes["IDENTIFIER"] = 1] = "IDENTIFIER";
|
|
22
|
+
AccessCodeControlRequestTypes[AccessCodeControlRequestTypes["ACCESS_CODE"] = 2] = "ACCESS_CODE";
|
|
23
|
+
})(AccessCodeControlRequestTypes || (AccessCodeControlRequestTypes = {}));
|
|
24
|
+
var AccessCodeControlResponseTypes;
|
|
25
|
+
(function (AccessCodeControlResponseTypes) {
|
|
26
|
+
AccessCodeControlResponseTypes[AccessCodeControlResponseTypes["IDENTIFIER"] = 1] = "IDENTIFIER";
|
|
27
|
+
AccessCodeControlResponseTypes[AccessCodeControlResponseTypes["ACCESS_CODE"] = 2] = "ACCESS_CODE";
|
|
28
|
+
AccessCodeControlResponseTypes[AccessCodeControlResponseTypes["FLAGS"] = 3] = "FLAGS";
|
|
29
|
+
AccessCodeControlResponseTypes[AccessCodeControlResponseTypes["STATUS_CODE"] = 4] = "STATUS_CODE";
|
|
30
|
+
})(AccessCodeControlResponseTypes || (AccessCodeControlResponseTypes = {}));
|
|
31
|
+
/** The only character set HomeKit defines: the digits 0 to 9. */
|
|
32
|
+
const CHARACTER_SET_ARABIC_NUMERALS = 0x01;
|
|
33
|
+
var AccessCodeOperation;
|
|
34
|
+
(function (AccessCodeOperation) {
|
|
35
|
+
AccessCodeOperation[AccessCodeOperation["LIST"] = 1] = "LIST";
|
|
36
|
+
AccessCodeOperation[AccessCodeOperation["READ"] = 2] = "READ";
|
|
37
|
+
AccessCodeOperation[AccessCodeOperation["ADD"] = 3] = "ADD";
|
|
38
|
+
AccessCodeOperation[AccessCodeOperation["UPDATE"] = 4] = "UPDATE";
|
|
39
|
+
AccessCodeOperation[AccessCodeOperation["REMOVE"] = 5] = "REMOVE";
|
|
40
|
+
})(AccessCodeOperation || (AccessCodeOperation = {}));
|
|
41
|
+
/**
|
|
42
|
+
* The result of one access code operation, as sent back to the Home app.
|
|
43
|
+
*
|
|
44
|
+
* @group Lock
|
|
45
|
+
*/
|
|
46
|
+
export var AccessCodeResponseStatus;
|
|
47
|
+
(function (AccessCodeResponseStatus) {
|
|
48
|
+
AccessCodeResponseStatus[AccessCodeResponseStatus["SUCCESS"] = 0] = "SUCCESS";
|
|
49
|
+
AccessCodeResponseStatus[AccessCodeResponseStatus["UNKNOWN"] = 1] = "UNKNOWN";
|
|
50
|
+
AccessCodeResponseStatus[AccessCodeResponseStatus["EXCEEDED_MAXIMUM_ALLOWED_ACCESS_CODES"] = 2] = "EXCEEDED_MAXIMUM_ALLOWED_ACCESS_CODES";
|
|
51
|
+
AccessCodeResponseStatus[AccessCodeResponseStatus["TOO_MANY_BULK_OPERATIONS"] = 3] = "TOO_MANY_BULK_OPERATIONS";
|
|
52
|
+
AccessCodeResponseStatus[AccessCodeResponseStatus["DUPLICATE"] = 4] = "DUPLICATE";
|
|
53
|
+
AccessCodeResponseStatus[AccessCodeResponseStatus["SMALLER_THAN_MINIMUM_LENGTH"] = 5] = "SMALLER_THAN_MINIMUM_LENGTH";
|
|
54
|
+
AccessCodeResponseStatus[AccessCodeResponseStatus["LARGER_THAN_MAXIMUM_LENGTH"] = 6] = "LARGER_THAN_MAXIMUM_LENGTH";
|
|
55
|
+
AccessCodeResponseStatus[AccessCodeResponseStatus["INVALID_CHARACTER"] = 7] = "INVALID_CHARACTER";
|
|
56
|
+
AccessCodeResponseStatus[AccessCodeResponseStatus["INVALID_REQUEST"] = 8] = "INVALID_REQUEST";
|
|
57
|
+
AccessCodeResponseStatus[AccessCodeResponseStatus["DOES_NOT_EXIST"] = 9] = "DOES_NOT_EXIST";
|
|
58
|
+
})(AccessCodeResponseStatus || (AccessCodeResponseStatus = {}));
|
|
59
|
+
/**
|
|
60
|
+
* @group Lock
|
|
61
|
+
*/
|
|
62
|
+
export var AccessCodeManagementEvent;
|
|
63
|
+
(function (AccessCodeManagementEvent) {
|
|
64
|
+
AccessCodeManagementEvent["ACCESS_CODES_UPDATED"] = "update-access-codes";
|
|
65
|
+
})(AccessCodeManagementEvent || (AccessCodeManagementEvent = {}));
|
|
66
|
+
/**
|
|
67
|
+
* Handles the AccessCode service of a lock with a keypad: the Home app lists, adds, changes and removes the codes
|
|
68
|
+
* people type on the keypad.
|
|
69
|
+
*
|
|
70
|
+
* The codes are kept in memory. Listen for {@link AccessCodeManagementEvent.ACCESS_CODES_UPDATED} to save them and to
|
|
71
|
+
* send them to the lock, and pass the saved ones back as {@link AccessCodeManagementOptions.accessCodes} on the next start.
|
|
72
|
+
*
|
|
73
|
+
* HomeKit does not document this service. The layout comes from Apple's own HomeKit code, and matches the public
|
|
74
|
+
* reverse engineering in https://github.com/rednblkx/homekit-lock-access-code.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* // an accessory restored from the cache already has the service: hand it over, so no second one is added
|
|
79
|
+
* const cachedService = accessory.getService(Service.AccessCode);
|
|
80
|
+
* const accessCodes = new AccessCodeManagement({ accessCodes: savedCodes, service: cachedService });
|
|
81
|
+
* if (!cachedService) {
|
|
82
|
+
* accessory.addService(accessCodes.getService());
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* accessCodes.on(AccessCodeManagementEvent.ACCESS_CODES_UPDATED, (codes) => {
|
|
86
|
+
* // save the codes, and send them to the lock
|
|
87
|
+
* });
|
|
88
|
+
*
|
|
89
|
+
* // when someone types a code on the keypad
|
|
90
|
+
* if (accessCodes.hasAccessCode('1234')) {
|
|
91
|
+
* // unlock
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @group Lock
|
|
96
|
+
*/
|
|
97
|
+
export class AccessCodeManagement extends EventEmitter {
|
|
98
|
+
accessCodeService;
|
|
99
|
+
minimumLength;
|
|
100
|
+
maximumLength;
|
|
101
|
+
maximumAccessCodes;
|
|
102
|
+
accessCodes = new Map();
|
|
103
|
+
/**
|
|
104
|
+
* A counter which goes up by one whenever the codes change.
|
|
105
|
+
* How the Home app uses the Configuration State characteristic is not confirmed; a change counter is the usual meaning.
|
|
106
|
+
*/
|
|
107
|
+
configurationState = 0;
|
|
108
|
+
/**
|
|
109
|
+
* Instantiates a new AccessCodeManagement.
|
|
110
|
+
*
|
|
111
|
+
* @param {AccessCodeManagementOptions} options - the limits of the lock, the codes it holds and your own AccessCode service
|
|
112
|
+
*/
|
|
113
|
+
constructor(options = {}) {
|
|
114
|
+
super();
|
|
115
|
+
this.minimumLength = options.minimumLength ?? 4;
|
|
116
|
+
this.maximumLength = options.maximumLength ?? 8;
|
|
117
|
+
this.maximumAccessCodes = options.maximumAccessCodes ?? 16;
|
|
118
|
+
for (const { identifier, accessCode } of options.accessCodes ?? []) {
|
|
119
|
+
this.accessCodes.set(identifier, accessCode);
|
|
120
|
+
}
|
|
121
|
+
this.accessCodeService = options.service || new Service.AccessCode();
|
|
122
|
+
this.setupServiceHandlers();
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* @returns the AccessCode service
|
|
126
|
+
*/
|
|
127
|
+
getService() {
|
|
128
|
+
return this.accessCodeService;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* @returns the codes stored on the lock, in the order of their identifiers
|
|
132
|
+
*/
|
|
133
|
+
getAccessCodes() {
|
|
134
|
+
return [...this.accessCodes]
|
|
135
|
+
.sort(([a], [b]) => a - b)
|
|
136
|
+
.map(([identifier, accessCode]) => ({ identifier, accessCode }));
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Whether a code typed on the keypad is one of the stored codes.
|
|
140
|
+
*
|
|
141
|
+
* @param accessCode - the code which was typed
|
|
142
|
+
*/
|
|
143
|
+
hasAccessCode(accessCode) {
|
|
144
|
+
return [
|
|
145
|
+
...this.accessCodes.values(),
|
|
146
|
+
].includes(accessCode);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* This destroys the AccessCodeManagement.
|
|
150
|
+
* It unregisters the SET handler it has associated with the given AccessCode service.
|
|
151
|
+
* It removes all event handlers which were registered to this object.
|
|
152
|
+
*/
|
|
153
|
+
destroy() {
|
|
154
|
+
this.removeAllListeners();
|
|
155
|
+
this.accessCodeService.getCharacteristic(Characteristic.AccessCodeControlPoint).removeOnSet();
|
|
156
|
+
}
|
|
157
|
+
handleControlPointWrite(value) {
|
|
158
|
+
let operation;
|
|
159
|
+
let results;
|
|
160
|
+
let changed = false;
|
|
161
|
+
try {
|
|
162
|
+
const objects = tlv.decodeWithLists(Buffer.from(value, 'base64'));
|
|
163
|
+
const operationData = objects[AccessCodeControlTypes.OPERATION_TYPE];
|
|
164
|
+
operation = operationData
|
|
165
|
+
? tlv.readVariableUIntLE(operationData)
|
|
166
|
+
: undefined;
|
|
167
|
+
const requestData = objects[AccessCodeControlTypes.REQUEST];
|
|
168
|
+
const requests = (requestData === undefined
|
|
169
|
+
? []
|
|
170
|
+
: Array.isArray(requestData)
|
|
171
|
+
? requestData
|
|
172
|
+
: [requestData])
|
|
173
|
+
.map((data) => {
|
|
174
|
+
const fields = tlv.decode(data);
|
|
175
|
+
const identifier = fields[AccessCodeControlRequestTypes.IDENTIFIER];
|
|
176
|
+
return {
|
|
177
|
+
identifier: identifier
|
|
178
|
+
? tlv.readVariableUIntLE(identifier)
|
|
179
|
+
: undefined,
|
|
180
|
+
accessCode: fields[AccessCodeControlRequestTypes.ACCESS_CODE]?.toString('utf8'),
|
|
181
|
+
};
|
|
182
|
+
});
|
|
183
|
+
switch (operation) {
|
|
184
|
+
case AccessCodeOperation.LIST:
|
|
185
|
+
results = this.getAccessCodes().map(code => ({ ...code, status: AccessCodeResponseStatus.SUCCESS }));
|
|
186
|
+
break;
|
|
187
|
+
case AccessCodeOperation.READ:
|
|
188
|
+
results = requests.map(({ identifier }) => this.read(identifier));
|
|
189
|
+
break;
|
|
190
|
+
case AccessCodeOperation.ADD:
|
|
191
|
+
results = requests.map(({ accessCode }) => this.add(accessCode));
|
|
192
|
+
break;
|
|
193
|
+
case AccessCodeOperation.UPDATE:
|
|
194
|
+
results = requests.map(({ identifier, accessCode }) => this.update(identifier, accessCode));
|
|
195
|
+
break;
|
|
196
|
+
case AccessCodeOperation.REMOVE:
|
|
197
|
+
results = requests.map(({ identifier }) => this.remove(identifier));
|
|
198
|
+
break;
|
|
199
|
+
default:
|
|
200
|
+
results = [
|
|
201
|
+
{ status: AccessCodeResponseStatus.INVALID_REQUEST },
|
|
202
|
+
];
|
|
203
|
+
}
|
|
204
|
+
// READ never changes anything, and LIST has no requests
|
|
205
|
+
changed = operation !== AccessCodeOperation.LIST && operation !== AccessCodeOperation.READ
|
|
206
|
+
&& results.some(result => result.status === AccessCodeResponseStatus.SUCCESS);
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
results = [
|
|
210
|
+
{ status: AccessCodeResponseStatus.INVALID_REQUEST },
|
|
211
|
+
];
|
|
212
|
+
}
|
|
213
|
+
if (changed) {
|
|
214
|
+
this.configurationState = this.configurationState >= 0xFFFF
|
|
215
|
+
? 1
|
|
216
|
+
: this.configurationState + 1;
|
|
217
|
+
this.accessCodeService.updateCharacteristic(Characteristic.ConfigurationState, this.configurationState);
|
|
218
|
+
const accessCodes = this.getAccessCodes();
|
|
219
|
+
setTimeout(() => {
|
|
220
|
+
// the codes of this write, not the state at the time the timer fires: another write may have followed by then
|
|
221
|
+
this.emit(AccessCodeManagementEvent.ACCESS_CODES_UPDATED, accessCodes);
|
|
222
|
+
}, 0).unref();
|
|
223
|
+
}
|
|
224
|
+
return this.encodeResponse(operation, results).toString('base64');
|
|
225
|
+
}
|
|
226
|
+
read(identifier) {
|
|
227
|
+
const accessCode = identifier === undefined
|
|
228
|
+
? undefined
|
|
229
|
+
: this.accessCodes.get(identifier);
|
|
230
|
+
return accessCode === undefined
|
|
231
|
+
? { identifier, status: AccessCodeResponseStatus.DOES_NOT_EXIST }
|
|
232
|
+
: { identifier, accessCode, status: AccessCodeResponseStatus.SUCCESS };
|
|
233
|
+
}
|
|
234
|
+
add(accessCode) {
|
|
235
|
+
const invalid = this.validate(accessCode);
|
|
236
|
+
if (invalid !== undefined || accessCode === undefined) {
|
|
237
|
+
return { accessCode, status: invalid ?? AccessCodeResponseStatus.INVALID_REQUEST };
|
|
238
|
+
}
|
|
239
|
+
if (this.accessCodes.size >= this.maximumAccessCodes) {
|
|
240
|
+
return { accessCode, status: AccessCodeResponseStatus.EXCEEDED_MAXIMUM_ALLOWED_ACCESS_CODES };
|
|
241
|
+
}
|
|
242
|
+
let identifier = 0;
|
|
243
|
+
while (this.accessCodes.has(identifier)) {
|
|
244
|
+
identifier++;
|
|
245
|
+
}
|
|
246
|
+
this.accessCodes.set(identifier, accessCode);
|
|
247
|
+
return { identifier, accessCode, status: AccessCodeResponseStatus.SUCCESS };
|
|
248
|
+
}
|
|
249
|
+
update(identifier, accessCode) {
|
|
250
|
+
if (identifier === undefined || !this.accessCodes.has(identifier)) {
|
|
251
|
+
return { identifier, status: AccessCodeResponseStatus.DOES_NOT_EXIST };
|
|
252
|
+
}
|
|
253
|
+
const invalid = this.validate(accessCode, identifier);
|
|
254
|
+
if (invalid !== undefined || accessCode === undefined) {
|
|
255
|
+
return { identifier, status: invalid ?? AccessCodeResponseStatus.INVALID_REQUEST };
|
|
256
|
+
}
|
|
257
|
+
this.accessCodes.set(identifier, accessCode);
|
|
258
|
+
return { identifier, accessCode, status: AccessCodeResponseStatus.SUCCESS };
|
|
259
|
+
}
|
|
260
|
+
remove(identifier) {
|
|
261
|
+
const result = this.read(identifier);
|
|
262
|
+
if (result.status === AccessCodeResponseStatus.SUCCESS && identifier !== undefined) {
|
|
263
|
+
this.accessCodes.delete(identifier);
|
|
264
|
+
}
|
|
265
|
+
return result;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* @returns the status for a code which cannot be stored, or `undefined` for a code which can
|
|
269
|
+
*/
|
|
270
|
+
validate(accessCode, ignoreIdentifier) {
|
|
271
|
+
if (accessCode === undefined) {
|
|
272
|
+
return AccessCodeResponseStatus.INVALID_REQUEST;
|
|
273
|
+
}
|
|
274
|
+
if (!/^\d*$/.test(accessCode)) {
|
|
275
|
+
return AccessCodeResponseStatus.INVALID_CHARACTER;
|
|
276
|
+
}
|
|
277
|
+
if (accessCode.length < this.minimumLength) {
|
|
278
|
+
return AccessCodeResponseStatus.SMALLER_THAN_MINIMUM_LENGTH;
|
|
279
|
+
}
|
|
280
|
+
if (accessCode.length > this.maximumLength) {
|
|
281
|
+
return AccessCodeResponseStatus.LARGER_THAN_MAXIMUM_LENGTH;
|
|
282
|
+
}
|
|
283
|
+
for (const [identifier, existing] of this.accessCodes) {
|
|
284
|
+
if (existing === accessCode && identifier !== ignoreIdentifier) {
|
|
285
|
+
return AccessCodeResponseStatus.DUPLICATE;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return undefined;
|
|
289
|
+
}
|
|
290
|
+
encodeResponse(operation, results) {
|
|
291
|
+
const responses = results.map((result) => {
|
|
292
|
+
const fields = [];
|
|
293
|
+
if (result.identifier !== undefined) {
|
|
294
|
+
fields.push(tlv.encode(AccessCodeControlResponseTypes.IDENTIFIER, tlv.writeVariableUIntLE(result.identifier)));
|
|
295
|
+
}
|
|
296
|
+
if (result.accessCode !== undefined) {
|
|
297
|
+
fields.push(tlv.encode(AccessCodeControlResponseTypes.ACCESS_CODE, result.accessCode));
|
|
298
|
+
}
|
|
299
|
+
fields.push(tlv.encode(AccessCodeControlResponseTypes.FLAGS, 0), tlv.encode(AccessCodeControlResponseTypes.STATUS_CODE, result.status));
|
|
300
|
+
return Buffer.concat(fields);
|
|
301
|
+
});
|
|
302
|
+
return Buffer.concat([
|
|
303
|
+
// the operation is echoed: Apple's HomeKit daemon is reported to crash on an answer without it
|
|
304
|
+
tlv.encode(AccessCodeControlTypes.OPERATION_TYPE, operation ?? 0),
|
|
305
|
+
// an empty list is no entry at all, not an entry of zero length
|
|
306
|
+
...(responses.length > 0
|
|
307
|
+
? [
|
|
308
|
+
tlv.encode(AccessCodeControlTypes.RESPONSE, responses),
|
|
309
|
+
]
|
|
310
|
+
: []),
|
|
311
|
+
]);
|
|
312
|
+
}
|
|
313
|
+
setupServiceHandlers() {
|
|
314
|
+
this.accessCodeService.getCharacteristic(Characteristic.AccessCodeSupportedConfiguration)
|
|
315
|
+
.updateValue(Buffer.concat([
|
|
316
|
+
tlv.encode(AccessCodeSupportedConfigurationTypes.CHARACTER_SET, CHARACTER_SET_ARABIC_NUMERALS),
|
|
317
|
+
tlv.encode(AccessCodeSupportedConfigurationTypes.MINIMUM_VALUE_LENGTH, this.minimumLength),
|
|
318
|
+
tlv.encode(AccessCodeSupportedConfigurationTypes.MAXIMUM_VALUE_LENGTH, this.maximumLength),
|
|
319
|
+
tlv.encode(AccessCodeSupportedConfigurationTypes.MAXIMUM_ACCESS_CODES, this.maximumAccessCodes),
|
|
320
|
+
]).toString('base64'));
|
|
321
|
+
this.accessCodeService.getCharacteristic(Characteristic.ConfigurationState)
|
|
322
|
+
.updateValue(this.configurationState);
|
|
323
|
+
this.accessCodeService.getCharacteristic(Characteristic.AccessCodeControlPoint)
|
|
324
|
+
.onSet(value => this.handleControlPointWrite(value))
|
|
325
|
+
.updateValue('');
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
//# sourceMappingURL=AccessCodeManagement.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AccessCodeManagement.js","sourceRoot":"","sources":["../../../src/lib/lock/AccessCodeManagement.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE1C,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAA;AAErC,IAAK,qCAKJ;AALD,WAAK,qCAAqC;IACxC,mHAAoB,CAAA;IACpB,iIAA2B,CAAA;IAC3B,iIAA2B,CAAA;IAC3B,iIAA2B,CAAA;AAC7B,CAAC,EALI,qCAAqC,KAArC,qCAAqC,QAKzC;AAED,IAAK,sBAIJ;AAJD,WAAK,sBAAsB;IACzB,uFAAqB,CAAA;IACrB,yEAAc,CAAA;IACd,2EAAe,CAAA;AACjB,CAAC,EAJI,sBAAsB,KAAtB,sBAAsB,QAI1B;AAED,IAAK,6BAGJ;AAHD,WAAK,6BAA6B;IAChC,6FAAiB,CAAA;IACjB,+FAAkB,CAAA;AACpB,CAAC,EAHI,6BAA6B,KAA7B,6BAA6B,QAGjC;AAED,IAAK,8BAKJ;AALD,WAAK,8BAA8B;IACjC,+FAAiB,CAAA;IACjB,iGAAkB,CAAA;IAClB,qFAAY,CAAA;IACZ,iGAAkB,CAAA;AACpB,CAAC,EALI,8BAA8B,KAA9B,8BAA8B,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B,EAAE,IAAI,CAAC,kBAAkB,CAAC;SAChG,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAA;QAExB,IAAI,CAAC,iBAAiB,CAAC,iBAAiB,CAAC,cAAc,CAAC,kBAAkB,CAAC;aACxE,WAAW,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAA;QAEvC,IAAI,CAAC,iBAAiB,CAAC,iBAAiB,CAAC,cAAc,CAAC,sBAAsB,CAAC;aAC5E,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,IAAI,CAAC,uBAAuB,CAAC,KAAe,CAAC,CAAC;aAC7D,WAAW,CAAC,EAAE,CAAC,CAAA;IACpB,CAAC;CACF"}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { NFCAccess } from '../definitions/index.js';
|
|
2
|
+
import { Buffer } from 'node:buffer';
|
|
3
|
+
import { EventEmitter } from 'node:events';
|
|
4
|
+
/**
|
|
5
|
+
* The result of one NFC Access operation, as sent back to the Home app.
|
|
6
|
+
*
|
|
7
|
+
* @group Lock
|
|
8
|
+
*/
|
|
9
|
+
export declare enum NFCAccessResponseStatus {
|
|
10
|
+
SUCCESS = 0,
|
|
11
|
+
OUT_OF_RESOURCES = 1,
|
|
12
|
+
DUPLICATE = 2,
|
|
13
|
+
DOES_NOT_EXIST = 3,
|
|
14
|
+
NOT_SUPPORTED = 4
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The colour of the home key card in Wallet. These four are the only values the Home app accepts.
|
|
18
|
+
*
|
|
19
|
+
* @group Lock
|
|
20
|
+
*/
|
|
21
|
+
export declare enum HomeKeyFinish {
|
|
22
|
+
BLACK = 0,
|
|
23
|
+
TAN = 14341582,
|
|
24
|
+
SILVER = 14935011,
|
|
25
|
+
GOLD = 15521450
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* @group Lock
|
|
29
|
+
*/
|
|
30
|
+
export declare enum NFCAccessManagementEvent {
|
|
31
|
+
STATE_UPDATED = "update-state"
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The key a lock uses to prove itself to a phone or a watch. There is one for a whole home.
|
|
35
|
+
* It is a private key: store it as carefully as a pairing key, and never log it.
|
|
36
|
+
*
|
|
37
|
+
* @group Lock
|
|
38
|
+
*/
|
|
39
|
+
export interface NFCAccessReaderKey {
|
|
40
|
+
/** The first 8 bytes of SHA-256("key-identifier" + private key), as hex. */
|
|
41
|
+
identifier: string;
|
|
42
|
+
/** The NIST P-256 private key, 32 bytes as hex. */
|
|
43
|
+
privateKey: string;
|
|
44
|
+
/** The reader identifier the Home app sent with the key, as hex. */
|
|
45
|
+
readerIdentifier: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The public key of one home user. It is only stored when the Home app sends one.
|
|
49
|
+
*
|
|
50
|
+
* @group Lock
|
|
51
|
+
*/
|
|
52
|
+
export interface NFCAccessIssuerKey {
|
|
53
|
+
/** The first 8 bytes of SHA-256("key-identifier" + public key), as hex. */
|
|
54
|
+
identifier: string;
|
|
55
|
+
/** The Ed25519 public key, 32 bytes as hex. */
|
|
56
|
+
publicKey: string;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The public key of the home key in the Wallet of one phone or watch.
|
|
60
|
+
*
|
|
61
|
+
* @group Lock
|
|
62
|
+
*/
|
|
63
|
+
export interface NFCAccessDeviceCredentialKey {
|
|
64
|
+
/** The first 6 bytes of SHA-1(0x04 + public key), as hex. */
|
|
65
|
+
identifier: string;
|
|
66
|
+
/** The NIST P-256 public key, X and Y without the 0x04 prefix: 64 bytes as hex. */
|
|
67
|
+
publicKey: string;
|
|
68
|
+
/** The identifier of the issuer key of the user this device belongs to, as hex. */
|
|
69
|
+
issuerKeyIdentifier: string;
|
|
70
|
+
/** `false` for a key the Home app suspended. */
|
|
71
|
+
active: boolean;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Everything the Home app has stored on the lock for home keys. All of it survives `JSON.stringify()`.
|
|
75
|
+
*
|
|
76
|
+
* @group Lock
|
|
77
|
+
*/
|
|
78
|
+
export interface NFCAccessState {
|
|
79
|
+
readerKey?: NFCAccessReaderKey;
|
|
80
|
+
issuerKeys: NFCAccessIssuerKey[];
|
|
81
|
+
deviceCredentialKeys: NFCAccessDeviceCredentialKey[];
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* @group Lock
|
|
85
|
+
*/
|
|
86
|
+
export interface NFCAccessManagementOptions {
|
|
87
|
+
/** How many issuer keys the lock can hold. Default 16. */
|
|
88
|
+
maximumIssuerKeys?: number;
|
|
89
|
+
/** How many suspended device credential keys the lock can hold. Default 16. */
|
|
90
|
+
maximumSuspendedDeviceCredentialKeys?: number;
|
|
91
|
+
/** How many device credential keys the lock can hold in all. Default 16. */
|
|
92
|
+
maximumDeviceCredentialKeys?: number;
|
|
93
|
+
/** The state saved before a restart. */
|
|
94
|
+
state?: NFCAccessState;
|
|
95
|
+
/** Supply your own instance to sideload the NFCAccess service. */
|
|
96
|
+
service?: NFCAccess;
|
|
97
|
+
}
|
|
98
|
+
export declare interface NFCAccessManagement {
|
|
99
|
+
on(event: 'update-state', listener: (state: NFCAccessState) => void): this;
|
|
100
|
+
emit(event: 'update-state', state: NFCAccessState): boolean;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Builds the value of the Hardware Finish characteristic for a card colour.
|
|
104
|
+
* Set it on the AccessoryInformation service of a lock with home keys.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* accessory.getService(Service.AccessoryInformation)!
|
|
109
|
+
* .setCharacteristic(Characteristic.HardwareFinish, encodeHardwareFinish(HomeKeyFinish.TAN));
|
|
110
|
+
* ```
|
|
111
|
+
* @param finish - the colour of the home key card in Wallet
|
|
112
|
+
* @group Lock
|
|
113
|
+
*/
|
|
114
|
+
export declare function encodeHardwareFinish(finish: HomeKeyFinish): string;
|
|
115
|
+
/**
|
|
116
|
+
* Handles the NFCAccess service of a lock: the part of Apple Home Key which runs over HomeKit.
|
|
117
|
+
* The Home app hands the lock a reader key, and the public key of the home key on each phone and watch.
|
|
118
|
+
*
|
|
119
|
+
* This is only half of Home Key. The other half is the NFC reader at the door, which talks to the phone with
|
|
120
|
+
* those keys. HAP-NodeJS has no part in that: without a reader, the key shows in Wallet and opens nothing.
|
|
121
|
+
*
|
|
122
|
+
* Listen for {@link NFCAccessManagementEvent.STATE_UPDATED} to save the state and to hand it to the reader, and
|
|
123
|
+
* pass the saved state back as {@link NFCAccessManagementOptions.state} on the next start.
|
|
124
|
+
*
|
|
125
|
+
* The state holds a private key. Save it in a file of your own which nobody has a reason to share. A Homebridge
|
|
126
|
+
* plugin must not put it in the context of the accessory: that is written to the `cachedAccessories` file,
|
|
127
|
+
* which people post in bug reports.
|
|
128
|
+
*
|
|
129
|
+
* The lock also needs a LockMechanism service, and the colour of the card comes from the Hardware Finish
|
|
130
|
+
* characteristic of the AccessoryInformation service: see {@link encodeHardwareFinish}.
|
|
131
|
+
*
|
|
132
|
+
* HomeKit does not document this service. The layout comes from Apple's own HomeKit code, and the handling of
|
|
133
|
+
* reader keys and device credential keys matches the plugin homebridge-fake-homekey. The handling of issuer keys
|
|
134
|
+
* and the answers to a list of device credential keys are not confirmed against a Home app.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* // an accessory restored from the cache already has the service: hand it over, so no second one is added
|
|
139
|
+
* const cachedService = accessory.getService(Service.NFCAccess);
|
|
140
|
+
* const nfcAccess = new NFCAccessManagement({ state: savedState, service: cachedService });
|
|
141
|
+
* if (!cachedService) {
|
|
142
|
+
* accessory.addService(nfcAccess.getService());
|
|
143
|
+
* }
|
|
144
|
+
* accessory.getService(Service.AccessoryInformation)!
|
|
145
|
+
* .setCharacteristic(Characteristic.HardwareFinish, encodeHardwareFinish(HomeKeyFinish.TAN));
|
|
146
|
+
*
|
|
147
|
+
* nfcAccess.on(NFCAccessManagementEvent.STATE_UPDATED, (state) => {
|
|
148
|
+
* // save the state in a file of your own (not in the accessory context), and hand it to the NFC reader
|
|
149
|
+
* });
|
|
150
|
+
* ```
|
|
151
|
+
*
|
|
152
|
+
* @group Lock
|
|
153
|
+
*/
|
|
154
|
+
export declare class NFCAccessManagement extends EventEmitter {
|
|
155
|
+
private readonly nfcAccessService;
|
|
156
|
+
private readonly maximumIssuerKeys;
|
|
157
|
+
private readonly maximumSuspendedDeviceCredentialKeys;
|
|
158
|
+
private readonly maximumDeviceCredentialKeys;
|
|
159
|
+
private state;
|
|
160
|
+
/**
|
|
161
|
+
* Instantiates a new NFCAccessManagement.
|
|
162
|
+
*
|
|
163
|
+
* @param {NFCAccessManagementOptions} options - the limits of the lock, its saved state and your own NFCAccess service
|
|
164
|
+
*/
|
|
165
|
+
constructor(options?: NFCAccessManagementOptions);
|
|
166
|
+
/**
|
|
167
|
+
* The identifier of a reader key or an issuer key: the first 8 bytes of SHA-256("key-identifier" + key).
|
|
168
|
+
*
|
|
169
|
+
* @param key - the private key of a reader key, or the public key of an issuer key
|
|
170
|
+
*/
|
|
171
|
+
static keyIdentifier(key: Buffer): Buffer;
|
|
172
|
+
/**
|
|
173
|
+
* @returns the NFCAccess service
|
|
174
|
+
*/
|
|
175
|
+
getService(): NFCAccess;
|
|
176
|
+
/**
|
|
177
|
+
* @returns a copy of everything the Home app has stored on the lock. It holds a private key.
|
|
178
|
+
*/
|
|
179
|
+
getState(): NFCAccessState;
|
|
180
|
+
/**
|
|
181
|
+
* This destroys the NFCAccessManagement.
|
|
182
|
+
* It unregisters the SET handler it has associated with the given NFCAccess service.
|
|
183
|
+
* It removes all event handlers which were registered to this object.
|
|
184
|
+
*/
|
|
185
|
+
destroy(): void;
|
|
186
|
+
private handleControlPointWrite;
|
|
187
|
+
/**
|
|
188
|
+
* @returns the answers to one reader key request, without the type of the response around them
|
|
189
|
+
*/
|
|
190
|
+
private handleReaderKey;
|
|
191
|
+
/**
|
|
192
|
+
* @returns the answers to one device credential key request, without the type of the response around them
|
|
193
|
+
*/
|
|
194
|
+
private handleDeviceCredentialKey;
|
|
195
|
+
/**
|
|
196
|
+
* @returns the answers to one issuer key request, without the type of the response around them
|
|
197
|
+
*/
|
|
198
|
+
private handleIssuerKey;
|
|
199
|
+
private setupServiceHandlers;
|
|
200
|
+
}
|