hassio-plejd/plejd/ble.js

616 lines
16 KiB
JavaScript
Raw Normal View History

2019-12-15 17:42:57 +01:00
const noble = require('@icanos/noble');
2019-12-15 16:34:07 +01:00
const crypto = require('crypto');
const xor = require('buffer-xor');
const _ = require('lodash');
const EventEmitter = require('events');
2019-12-21 15:08:03 +00:00
let debug = '';
2019-12-15 16:34:07 +01:00
const getLogger = () => {
const consoleLogger = msg => console.log('plejd', msg);
if (debug === 'console') {
return consoleLogger;
}
// > /dev/null
return _.noop;
};
const logger = getLogger();
// UUIDs
const PLEJD_SERVICE = "31ba000160854726be45040c957391b5"
const DATA_UUID = "31ba000460854726be45040c957391b5"
const LAST_DATA_UUID = "31ba000560854726be45040c957391b5"
const AUTH_UUID = "31ba000960854726be45040c957391b5"
const PING_UUID = "31ba000a60854726be45040c957391b5"
const STATE_IDLE = 'idle';
const STATE_SCANNING = 'scanning';
const STATE_CONNECTING = 'connecting';
const STATE_CONNECTED = 'connected';
const STATE_AUTHENTICATED = 'authenticated';
const STATE_DISCONNECTED = 'disconnected';
const STATE_UNINITIALIZED = 'uninitialized';
const STATE_INITIALIZED = 'initialized';
2019-12-21 15:01:15 +00:00
const BLE_CMD_DIM_CHANGE = '00c8';
const BLE_CMD_DIM2_CHANGE = '0098';
const BLE_CMD_STATE_CHANGE = '0097';
const BLE_CMD_SCENE_TRIG = '0021';
2019-12-15 16:34:07 +01:00
class PlejdService extends EventEmitter {
constructor(cryptoKey, keepAlive = false) {
super();
this.cryptoKey = Buffer.from(cryptoKey.replace(/-/g, ''), 'hex');
// Keeps track of the current state
this.state = STATE_IDLE;
// Keeps track of discovered devices
this.devices = {};
// Keeps track of the currently connected device
this.device = null;
2019-12-19 09:41:16 +00:00
this.deviceAddress = null;
2019-12-18 22:12:57 +00:00
this.deviceIdx = 0;
2019-12-15 16:34:07 +01:00
2019-12-19 09:41:16 +00:00
this.writeQueue = [];
2019-12-21 15:01:15 +00:00
this.plejdDevices = {};
2019-12-15 16:34:07 +01:00
// Holds a reference to all characteristics
this.characteristicState = STATE_UNINITIALIZED;
this.characteristics = {
data: null,
lastData: null,
auth: null,
ping: null
};
2019-12-18 22:12:57 +00:00
logger('wiring events and waiting for BLE interface to power up.');
2019-12-15 17:42:57 +01:00
this.wireEvents();
2019-12-15 16:34:07 +01:00
}
2019-12-21 15:01:15 +00:00
turnOn(id, command) {
logger('turning on ' + id + ' at brightness ' + (!command.brightness ? 255 : command.brightness));
const brightness = command.brightness ? command.brightness : 0;
if (command.transition) {
// we have a transition time, split the target brightness
// into pieces spread of the transition time
const steps = command.transition * 2;
const brightnessStep = brightness / steps;
let i = 0;
const transitionRef = setInterval(() => {
this._turnOn(id, (brightnessStep * i) + 1);
2019-12-19 09:41:16 +00:00
2019-12-21 15:01:15 +00:00
if (i >= steps) {
clearInterval(transitionRef);
}
i++;
}, 500);
}
else {
this._turnOn(id, brightness);
}
}
_turnOn(id, brightness) {
2019-12-19 09:41:16 +00:00
var payload;
2019-12-21 15:01:15 +00:00
if (!brightness || brightness === 0) {
logger('no brightness specified, setting to previous known.');
2019-12-19 09:41:16 +00:00
payload = Buffer.from((id).toString(16).padStart(2, '0') + '0110009701', 'hex');
} else {
2019-12-21 15:01:15 +00:00
logger('brightness is ' + brightness);
2019-12-19 09:41:16 +00:00
brightness = brightness << 8 | brightness;
payload = Buffer.from((id).toString(16).padStart(2, '0') + '0110009801' + (brightness).toString(16).padStart(4, '0'), 'hex');
}
this.write(payload);
}
2019-12-21 15:01:15 +00:00
turnOff(id, command) {
2019-12-19 09:41:16 +00:00
logger('turning off ' + id);
2019-12-21 15:01:15 +00:00
if (command.transition) {
// we have a transition time, split the target brightness (which will be 0)
// into pieces spread of the transition time
const initialBrightness = this.plejdDevices[id].dim;
const steps = command.transition * 2;
const brightnessStep = initialBrightness / steps;
let currentBrightness = initialBrightness;
let i = 0;
const transitionRef = setInterval(() => {
currentBrightness = initialBrightness - (brightnessStep * i);
if (currentBrightness <= 0) {
clearInterval(transitionRef);
}
this._turnOn(id, currentBrightness);
if (i >= steps) {
clearInterval(transitionRef);
2019-12-21 15:52:35 +00:00
// finally, we turn it off
this._turnOff(id);
2019-12-21 15:01:15 +00:00
}
i++;
}, 500);
}
2019-12-21 15:52:35 +00:00
else {
this._turnOff(id);
}
}
2019-12-21 15:01:15 +00:00
2019-12-21 15:52:35 +00:00
_turnOff(id) {
2019-12-19 09:41:16 +00:00
var payload = Buffer.from((id).toString(16).padStart(2, '0') + '0110009700', 'hex');
this.write(payload);
}
2019-12-15 16:34:07 +01:00
scan() {
logger('scan()');
if (this.state === STATE_SCANNING) {
console.log('error: already scanning, please wait.');
return;
}
this.state = STATE_SCANNING;
2019-12-18 22:12:57 +00:00
noble.startScanning([PLEJD_SERVICE]);
2019-12-19 09:41:16 +00:00
2019-12-15 16:34:07 +01:00
setTimeout(() => {
noble.stopScanning();
this.state = STATE_IDLE;
2019-12-18 22:12:57 +00:00
const foundDeviceCount = Object.values(this.devices).length;
logger('scan completed, found ' + foundDeviceCount + ' device(s).');
if (foundDeviceCount == 0) {
console.log('warning: no devices found. will not do anything else.');
}
else {
this.emit('scanComplete', this.devices);
}
2019-12-15 16:34:07 +01:00
}, 5000);
}
connect(uuid = null) {
2019-12-18 22:12:57 +00:00
const self = this;
2019-12-15 17:42:57 +01:00
if (this.state === STATE_CONNECTING) {
console.log('warning: currently connecting to a device, please wait...');
return;
}
2019-12-15 16:34:07 +01:00
if (!uuid) {
2019-12-18 22:12:57 +00:00
this.device = Object.values(this.devices)[this.deviceIdx];
2019-12-15 16:34:07 +01:00
}
else {
this.device = this.devices[uuid];
if (!this.device) {
console.log('error: could not find a device with uuid: ' + uuid);
return;
}
}
2019-12-18 22:12:57 +00:00
if (!this.device) {
console.log('error: reached end of device list. cannot continue.');
return;
}
2019-12-19 09:41:16 +00:00
this.deviceAddress = this._reverseBuffer(
Buffer.from(
String(this.device.address)
.replace(/\-/g, '')
.replace(/\:/g, ''), 'hex'
)
);
2019-12-21 15:01:15 +00:00
console.log('connecting to ' + this.device.id + ' with addr ' + this.device.address + ' and rssi ' + this.device.rssi);
2019-12-18 22:12:57 +00:00
setTimeout(() => {
if (self.state !== STATE_CONNECTED && self.state !== STATE_AUTHENTICATED) {
if (self.deviceIdx < Object.keys(self.devices).length) {
logger('connection timed out after 10 s. trying next.');
self.deviceIdx++;
self.connect();
}
}
}, 10 * 1000);
2019-12-15 17:42:57 +01:00
this.state = STATE_CONNECTING;
2019-12-18 22:12:57 +00:00
this.device.connect((err) => {
self.onDeviceConnected(err);
});
}
reset() {
logger('reset()');
this.state = STATE_IDLE;
2019-12-15 16:34:07 +01:00
}
2019-12-15 17:42:57 +01:00
disconnect() {
logger('disconnect()');
if (this.state !== STATE_CONNECTED) {
return;
}
2019-12-19 09:41:16 +00:00
clearInterval(this.pingRef);
this.unsubscribeCharacteristics();
2019-12-15 17:42:57 +01:00
this.device.disconnect();
2019-12-19 09:41:16 +00:00
this.state = STATE_DISCONNECTED;
2019-12-15 17:42:57 +01:00
}
2019-12-15 16:34:07 +01:00
authenticate() {
logger('authenticate()');
const self = this;
if (this.state !== STATE_CONNECTED) {
console.log('error: need to be connected and not previously authenticated (new connection).');
return;
}
this.characteristics.auth.write(Buffer.from([0]), false, (err) => {
if (err) {
console.log('error: failed to authenticate: ' + err);
return;
}
self.characteristics.auth.read((err, data) => {
if (err) {
console.log('error: failed to read auth response: ' + err);
return;
}
var resp = self._createChallengeResponse(self.cryptoKey, data);
self.characteristics.auth.write(resp, false, (err) => {
if (err) {
console.log('error: failed to challenge: ' + err);
return;
}
self.state = STATE_AUTHENTICATED;
self.emit('authenticated');
});
})
});
}
2019-12-19 09:41:16 +00:00
write(data) {
if (this.state !== STATE_AUTHENTICATED) {
logger('error: not connected.');
this.writeQueue.push(data);
return false;
}
const encryptedData = this._encryptDecrypt(this.cryptoKey, this.deviceAddress, data);
this.characteristics.data.write(encryptedData, false);
let writeData;
while ((writeData = this.writeQueue.shift()) !== undefined) {
this.characteristics.data.write(this._encryptDecrypt(this.cryptoKey, this.deviceAddress, writeData), false);
}
}
2019-12-15 16:34:07 +01:00
onAuthenticated() {
// Start ping
logger('onAuthenticated()');
this.startPing();
}
startPing() {
logger('startPing()');
clearInterval(this.pingRef);
this.pingRef = setInterval(async () => {
if (this.state === STATE_AUTHENTICATED) {
logger('ping');
this.ping();
}
2019-12-19 09:41:16 +00:00
else if (this.state === STATE_DISCONNECTED) {
console.log('warning: device disconnected, stop ping.');
}
2019-12-15 16:34:07 +01:00
else {
console.log('error: ping failed, not connected.');
}
}, 3000);
}
onPingSuccess(nr) {
logger('pong: ' + nr);
}
onPingFailed(error) {
logger('onPingFailed(' + error + ')');
logger('stopping ping and reconnecting.');
clearInterval(this.pingRef);
2019-12-19 09:41:16 +00:00
this.unsubscribeCharacteristics();
2019-12-15 16:34:07 +01:00
this.state = STATE_DISCONNECTED;
2019-12-19 09:41:16 +00:00
2019-12-15 16:34:07 +01:00
this.connect(this.device.id);
}
ping() {
logger('ping()');
if (this.state !== STATE_AUTHENTICATED) {
console.log('error: needs to be authenticated before pinging.');
return;
}
const self = this;
var ping = crypto.randomBytes(1);
try {
this.characteristics.ping.write(ping, false, (err) => {
if (err) {
console.log('error: unable to send ping: ' + err);
self.emit('pingFailed');
return;
}
2019-12-18 22:12:57 +00:00
this.characteristics.ping.read((err, data) => {
2019-12-15 16:34:07 +01:00
if (err) {
console.log('error: unable to read ping: ' + err);
self.emit('pingFailed');
return;
}
if (((ping[0] + 1) & 0xff) !== data[0]) {
self.emit('pingFailed');
return;
}
else {
self.emit('pingSuccess', data[0]);
}
});
});
}
catch (error) {
console.log('error: writing to plejd: ' + error);
self.emit('pingFailed', error);
}
}
onDeviceConnected(err) {
logger('onDeviceConnected()');
const self = this;
2019-12-18 22:12:57 +00:00
if (err) {
console.log('error: failed to connect to device: ' + err + '. picking next.');
this.deviceIdx++;
this.reset();
this.connect();
return;
}
2019-12-15 16:34:07 +01:00
this.state = STATE_CONNECTED;
if (this.characteristicState === STATE_UNINITIALIZED) {
// We need to discover the characteristics
2019-12-18 22:12:57 +00:00
logger('discovering services and characteristics');
setTimeout(() => {
if (this.characteristicState === STATE_UNINITIALIZED) {
console.log('error: discovering characteristics timed out. trying next device.');
self.deviceIdx++;
self.disconnect();
self.connect();
}
}, 5000);
2019-12-15 16:34:07 +01:00
this.device.discoverSomeServicesAndCharacteristics([PLEJD_SERVICE], [], async (err, services, characteristics) => {
if (err) {
console.log('error: failed to discover services: ' + err);
return;
}
2019-12-18 22:12:57 +00:00
if (self.state !== STATE_CONNECTED || self.characteristicState !== STATE_UNINITIALIZED) {
// in case our time out triggered before we got here.
console.log('warning: found characteristics in invalid state. ignoring.');
return;
}
logger('found ' + characteristics.length + ' characteristic(s).');
2019-12-15 16:34:07 +01:00
characteristics.forEach((ch) => {
if (DATA_UUID == ch.uuid) {
logger('found DATA characteristic.');
self.characteristics.data = ch;
}
else if (LAST_DATA_UUID == ch.uuid) {
logger('found LAST_DATA characteristic.');
self.characteristics.lastData = ch;
}
else if (AUTH_UUID == ch.uuid) {
logger('found AUTH characteristic.');
self.characteristics.auth = ch;
}
else if (PING_UUID == ch.uuid) {
logger('found PING characteristic.');
self.characteristics.ping = ch;
}
});
2019-12-18 22:12:57 +00:00
if (self.characteristics.data
&& self.characteristics.lastData
&& self.characteristics.auth
&& self.characteristics.ping) {
2019-12-15 16:34:07 +01:00
self.characteristicState = STATE_INITIALIZED;
2019-12-19 09:41:16 +00:00
// subscribe to notifications
this.subscribeCharacteristics();
2019-12-15 16:34:07 +01:00
self.emit('deviceCharacteristicsComplete', self.device);
}
});
}
}
onDeviceCharacteristicsComplete(device) {
logger('onDeviceCharacteristicsComplete(' + device.id + ')');
this.authenticate();
}
onDeviceDiscovered(device) {
logger('onDeviceDiscovered(' + device.id + ')');
2019-12-18 22:12:57 +00:00
if (device.advertisement.localName === 'P mesh') {
logger('device is P mesh');
this.devices[device.id] = device;
}
2019-12-15 16:34:07 +01:00
}
onDeviceDisconnected() {
logger('onDeviceDisconnected()');
if (!this.device) {
console.log('warning: reconnect will not be performed.');
return;
}
// we just want to reconnect
this.connect(this.device.id);
}
onDeviceScanComplete() {
logger('onDeviceScanComplete()');
2019-12-18 22:12:57 +00:00
console.log('trying to connect to the mesh network.');
this.connect();
2019-12-15 16:34:07 +01:00
}
onInterfaceStateChanged(state) {
logger('onInterfaceStateChanged(' + state + ')');
if (state === 'poweredOn') {
this.scan();
}
}
2019-12-19 09:41:16 +00:00
onLastDataUpdated(data, isNotification) {
const decoded = this._encryptDecrypt(this.cryptoKey, this.deviceAddress, data);
let state = 0;
let dim = 0;
let device = parseInt(decoded[0], 10);
2019-12-21 15:01:15 +00:00
if (decoded.length < 5) {
// ignore the notification since too small
return;
}
const cmd = decoded.toString('hex', 3, 5);
if (debug) {
logger('raw event received: ' + decoded.toString('hex'));
}
if (cmd === BLE_CMD_DIM_CHANGE || cmd === BLE_CMD_DIM2_CHANGE) {
2019-12-19 09:41:16 +00:00
state = parseInt(decoded.toString('hex', 5, 6), 10);
dim = parseInt(decoded.toString('hex', 6, 8), 16) >> 8;
logger('d: ' + device + ' got state+dim update: ' + state + ' - ' + dim);
2019-12-21 15:01:15 +00:00
this.emit('stateChanged', device, { state: state, brightness: dim });
2019-12-19 09:41:16 +00:00
}
2019-12-21 15:01:15 +00:00
else if (cmd === BLE_CMD_STATE_CHANGE) {
2019-12-19 09:41:16 +00:00
state = parseInt(decoded.toString('hex', 5, 6), 10);
logger('d: ' + device + ' got state update: ' + state);
2019-12-21 15:01:15 +00:00
this.emit('stateChanged', device, { state: state });
2019-12-19 09:41:16 +00:00
}
2019-12-21 15:01:15 +00:00
else if (cmd === BLE_CMD_SCENE_TRIG) {
const scene = parseInt(decoded.toString('hex', 5, 6), 10);
this.emit('sceneTriggered', device, scene);
}
this.plejdDevices[device] = {
state: state,
dim: dim
};
2019-12-19 09:41:16 +00:00
}
2019-12-15 16:34:07 +01:00
wireEvents() {
2019-12-18 22:12:57 +00:00
logger('wireEvents()');
const self = this;
noble.on('stateChange', this.onInterfaceStateChanged.bind(self));
//noble.on('scanStop', this.onDeviceScanComplete.bind(self));
noble.on('discover', this.onDeviceDiscovered.bind(self));
noble.on('disconnect', this.onDeviceDisconnected.bind(self));
this.on('scanComplete', this.onDeviceScanComplete.bind(this));
this.on('deviceCharacteristicsComplete', this.onDeviceCharacteristicsComplete.bind(self));
this.on('authenticated', this.onAuthenticated.bind(self));
this.on('pingFailed', this.onPingFailed.bind(self));
this.on('pingSuccess', this.onPingSuccess.bind(self));
2019-12-15 16:34:07 +01:00
}
2019-12-19 09:41:16 +00:00
subscribeCharacteristics() {
if (this.characteristics.lastData) {
this.characteristics.lastData.subscribe((err) => {
if (err) {
console.log('error: could not subscribe to event.');
}
});
this.characteristics.lastData.on('data', this.onLastDataUpdated.bind(this));
}
}
unsubscribeCharacteristics() {
if (this.characteristics.lastData) {
this.characteristics.lastData.unsubscribe((err) => {
if (err) {
console.log('error: could not unsubscribe from event.');
}
});
}
}
2019-12-15 16:34:07 +01:00
_createChallengeResponse(key, challenge) {
const intermediate = crypto.createHash('sha256').update(xor(key, challenge)).digest();
const part1 = intermediate.subarray(0, 16);
const part2 = intermediate.subarray(16);
const resp = xor(part1, part2);
return resp;
}
_encryptDecrypt(key, addr, data) {
var buf = Buffer.concat([addr, addr, addr.subarray(0, 4)]);
var cipher = crypto.createCipheriv("aes-128-ecb", key, '');
cipher.setAutoPadding(false);
var ct = cipher.update(buf).toString('hex');
ct += cipher.final().toString('hex');
ct = Buffer.from(ct, 'hex');
var output = "";
for (var i = 0, length = data.length; i < length; i++) {
output += String.fromCharCode(data[i] ^ ct[i % 16]);
}
return Buffer.from(output, 'ascii');
}
_reverseBuffer(src) {
var buffer = Buffer.allocUnsafe(src.length)
for (var i = 0, j = src.length - 1; i <= j; ++i, --j) {
buffer[i] = src[j]
buffer[j] = src[i]
}
return buffer
}
2019-12-15 17:42:57 +01:00
}
module.exports = PlejdService;