2017-08-22 01:11:07 +00:00
|
|
|
'use strict'
|
|
|
|
|
2017-11-22 14:58:45 +00:00
|
|
|
/** @module ble */
|
|
|
|
/** Bluetooth Low Energy module */
|
|
|
|
|
2017-12-13 23:35:59 +00:00
|
|
|
const util = require('util')
|
|
|
|
const os = require('os')
|
2017-08-28 12:49:47 +00:00
|
|
|
|
2017-12-13 23:35:59 +00:00
|
|
|
const log = require('../log')('ble')
|
|
|
|
const wifi = require('../wifi')
|
2017-08-22 01:11:07 +00:00
|
|
|
|
2017-12-13 23:40:14 +00:00
|
|
|
const DEVICE_NAME = process.env.DEVICE_NAME || 'intval3'
|
2017-11-22 14:58:45 +00:00
|
|
|
const SERVICE_ID = process.env.SERVICE_ID || 'intval3_ble'
|
|
|
|
const CHAR_ID = process.env.CHAR_ID || 'intval3char'
|
|
|
|
const WIFI_ID = process.env.WIFI_ID || 'wifichar'
|
2017-08-22 03:01:08 +00:00
|
|
|
const NETWORK = os.networkInterfaces()
|
|
|
|
const MAC = getMac() || spoofMac()
|
2017-12-13 23:34:00 +00:00
|
|
|
|
2017-12-13 23:40:14 +00:00
|
|
|
//Give the device a unique device name, needs to be in env
|
2017-12-13 23:31:59 +00:00
|
|
|
process.env.BLENO_DEVICE_NAME += '_' + MAC
|
2017-12-13 23:34:00 +00:00
|
|
|
const bleno = require('bleno')
|
|
|
|
|
|
|
|
|
2017-08-22 03:01:08 +00:00
|
|
|
let currentWifi = 'disconnected'
|
2018-01-04 21:10:38 +00:00
|
|
|
let currentAddr = null
|
2017-12-11 19:38:46 +00:00
|
|
|
let getState
|
2017-08-22 01:11:07 +00:00
|
|
|
|
|
|
|
const chars = []
|
|
|
|
|
|
|
|
function createChar(name, uuid, prop, write, read) {
|
|
|
|
function characteristic () {
|
|
|
|
bleno.Characteristic.call(this, {
|
|
|
|
uuid : uuid,
|
|
|
|
properties: prop
|
|
|
|
})
|
|
|
|
}
|
|
|
|
util.inherits(characteristic, bleno.Characteristic)
|
2017-08-22 03:45:56 +00:00
|
|
|
if (prop.indexOf('read') !== -1) {
|
2017-08-22 01:11:07 +00:00
|
|
|
//data, offset, withoutResponse, callback
|
|
|
|
characteristic.prototype.onReadRequest = read
|
|
|
|
}
|
2017-08-22 03:45:56 +00:00
|
|
|
if (prop.indexOf('write') !== -1) {
|
2017-08-22 01:11:07 +00:00
|
|
|
characteristic.prototype.onWriteRequest = write
|
|
|
|
}
|
2017-08-22 03:01:08 +00:00
|
|
|
chars.push(new characteristic())
|
|
|
|
}
|
|
|
|
|
2017-11-22 14:58:45 +00:00
|
|
|
function createChars (onWrite, onRead) {
|
|
|
|
createChar('intval3', CHAR_ID, ['read', 'write'], onWrite, onRead)
|
2017-08-22 03:41:27 +00:00
|
|
|
createChar('wifi', WIFI_ID, ['read', 'write'], onWifiWrite, onWifiRead)
|
2017-08-22 01:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onWifiWrite (data, offset, withoutResponse, callback) {
|
|
|
|
let result
|
|
|
|
let utf8
|
|
|
|
let obj
|
|
|
|
let ssid
|
|
|
|
let pwd
|
|
|
|
if (offset) {
|
2017-11-22 14:58:45 +00:00
|
|
|
log.warn(`Offset scenario`)
|
2017-08-22 01:11:07 +00:00
|
|
|
result = bleno.Characteristic.RESULT_ATTR_NOT_LONG
|
|
|
|
return callback(result)
|
|
|
|
}
|
|
|
|
utf8 = data.toString('utf8')
|
|
|
|
obj = JSON.parse(utf8)
|
|
|
|
ssid = obj.ssid
|
|
|
|
pwd = obj.pwd
|
2017-11-22 14:58:45 +00:00
|
|
|
log.info(`connecting to AP`, { ssid : ssid })
|
2018-01-01 02:32:09 +00:00
|
|
|
return wifi.createPSK(ssid, pwd, (err, hash, plaintext) => {
|
2017-08-22 01:11:07 +00:00
|
|
|
if (err) {
|
2018-01-01 02:32:09 +00:00
|
|
|
log.error('Error hashing wifi password', err)
|
|
|
|
result = bleno.Characteristic.RESULT_UNLIKELY_ERROR
|
2017-08-22 01:11:07 +00:00
|
|
|
return callback(result)
|
|
|
|
}
|
2018-01-01 02:32:09 +00:00
|
|
|
return wifi.setNetwork(ssid, plaintext, hash, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
log.error('Error configuring wifi', err)
|
|
|
|
result = bleno.Characteristic.RESULT_UNLIKELY_ERROR
|
|
|
|
return callback(result)
|
|
|
|
}
|
|
|
|
currentWifi = ssid
|
2018-01-04 21:10:38 +00:00
|
|
|
currentAddr = getIp()
|
2018-01-04 21:12:32 +00:00
|
|
|
log.info(`Connected to AP`, { ssid : ssid, ip : currentAddr })
|
2018-01-01 02:32:09 +00:00
|
|
|
result = bleno.Characteristic.RESULT_SUCCESS
|
|
|
|
return callback(result)
|
|
|
|
})
|
2017-08-22 01:11:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function onWifiRead (offset, callback) {
|
2019-03-27 22:36:39 +00:00
|
|
|
let result = bleno.Characteristic.RESULT_SUCCESS
|
|
|
|
let wifiRes = {}
|
2018-01-01 02:23:31 +00:00
|
|
|
let data
|
|
|
|
wifi.list((err, list) => {
|
|
|
|
if (err) {
|
|
|
|
result = bleno.Characteristic.RESULT_UNLIKELY_ERROR
|
|
|
|
return callback(result)
|
|
|
|
}
|
|
|
|
wifiRes.available = list
|
|
|
|
wifiRes.current = currentWifi
|
2018-01-04 21:10:38 +00:00
|
|
|
wifiRes.ip = currentAddr
|
2018-01-01 02:23:31 +00:00
|
|
|
log.info('Discovered available APs', { found : list.length })
|
|
|
|
data = new Buffer(JSON.stringify(wifiRes))
|
|
|
|
callback(result, data.slice(offset, data.length))
|
|
|
|
})
|
2017-08-22 01:11:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 03:01:08 +00:00
|
|
|
function getMac () {
|
|
|
|
const colonRe = new RegExp(':', 'g')
|
|
|
|
if (NETWORK && NETWORK.wlan0 && NETWORK.wlan0[0] && NETWORK.wlan0[0].mac) {
|
|
|
|
return NETWORK.wlan0[0].mac.replace(colonRe, '')
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
function spoofMac () {
|
|
|
|
const fs = require('fs')
|
|
|
|
const FSPATH = require.resolve('uuid')
|
|
|
|
const IDFILE = os.homedir() + '/.intval3id'
|
|
|
|
let uuid
|
|
|
|
let UUIDPATH
|
|
|
|
let TMP
|
|
|
|
let MACTMP
|
|
|
|
let dashRe
|
|
|
|
delete require.cache[FSPATH]
|
|
|
|
if (fs.existsSync(IDFILE)) {
|
|
|
|
return fs.readFileSync(IDFILE, 'utf8')
|
|
|
|
}
|
|
|
|
uuid = require('uuid').v4
|
|
|
|
UUIDPATH = require.resolve('uuid')
|
|
|
|
delete require.cache[UUIDPATH]
|
|
|
|
TMP = uuid()
|
|
|
|
MACTMP = TMP.replace(dashRe, '').substring(0, 12)
|
|
|
|
dashRe = new RegExp('-', 'g')
|
|
|
|
fs.writeFileSync(IDFILE, MACTMP, 'utf8')
|
|
|
|
return MACTMP
|
|
|
|
}
|
|
|
|
|
2018-01-04 21:10:38 +00:00
|
|
|
function getIp () {
|
|
|
|
let addr = null
|
|
|
|
let ipv4
|
|
|
|
const ifaces = os.networkInterfaces()
|
|
|
|
if (ifaces && ifaces.wlan0) {
|
|
|
|
ipv4 = ifaces.wlan0.filter(iface => {
|
|
|
|
if (iface.family === 'IPv4') {
|
|
|
|
return iface
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (ipv4.length === 1) {
|
2018-01-04 21:14:12 +00:00
|
|
|
addr = ipv4[0].address
|
2018-01-04 21:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:11:07 +00:00
|
|
|
|
2017-11-22 14:30:08 +00:00
|
|
|
function capitalize (s) {
|
|
|
|
return s[0].toUpperCase() + s.slice(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Class representing the bluetooth interface */
|
|
|
|
class BLE {
|
2017-11-22 14:58:45 +00:00
|
|
|
/**
|
|
|
|
* Establishes Bluetooth Low Energy services, accessible to process through this class
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-12-11 19:38:46 +00:00
|
|
|
constructor (bleGetState) {
|
2017-11-22 14:30:08 +00:00
|
|
|
log.info('Starting bluetooth service')
|
|
|
|
|
2017-12-11 19:38:46 +00:00
|
|
|
getState = bleGetState
|
|
|
|
|
2017-11-22 14:30:08 +00:00
|
|
|
bleno.on('stateChange', state => {
|
2017-11-22 14:58:45 +00:00
|
|
|
log.info('stateChange', { state : state })
|
2017-11-22 14:30:08 +00:00
|
|
|
if (state === 'poweredOn') {
|
2019-10-16 14:58:46 +00:00
|
|
|
log.info('Starting advertising', { DEVICE_NAME, DEVICE_ID : process.env.BLENO_DEVICE_NAME })
|
2017-12-13 23:31:59 +00:00
|
|
|
bleno.startAdvertising(DEVICE_NAME, [CHAR_ID])
|
2017-11-22 14:30:08 +00:00
|
|
|
} else {
|
|
|
|
bleno.stopAdvertising()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
bleno.on('advertisingStart', err => {
|
2017-11-22 14:58:45 +00:00
|
|
|
log.info('advertisingStart', { res : (err ? 'error ' + err : 'success') })
|
|
|
|
createChars(this._onWrite.bind(this), this._onRead.bind(this))
|
2017-11-22 14:30:08 +00:00
|
|
|
if (!err) {
|
|
|
|
bleno.setServices([
|
|
|
|
new bleno.PrimaryService({
|
|
|
|
uuid : SERVICE_ID, //hardcoded across panels
|
|
|
|
characteristics : chars
|
|
|
|
})
|
|
|
|
])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
bleno.on('accept', clientAddress => {
|
2017-11-22 14:58:45 +00:00
|
|
|
log.info('accept', { clientAddress : clientAddress })
|
2017-11-22 14:30:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
bleno.on('disconnect', clientAddress => {
|
2017-11-22 14:58:45 +00:00
|
|
|
log.info('disconnect', { clientAddress : clientAddress })
|
2017-11-22 14:30:08 +00:00
|
|
|
})
|
2018-01-01 02:23:31 +00:00
|
|
|
|
|
|
|
wifi.getNetwork((err, ssid) => {
|
|
|
|
if (err) {
|
|
|
|
return log.error('wifi.getNetwork', err)
|
|
|
|
}
|
|
|
|
currentWifi = ssid
|
2018-01-04 21:10:38 +00:00
|
|
|
currentAddr = getIp()
|
2018-01-04 21:12:32 +00:00
|
|
|
log.info('wifi.getNetwork', {ssid : ssid, ip : currentAddr })
|
2018-01-01 02:23:31 +00:00
|
|
|
})
|
2017-08-22 01:11:07 +00:00
|
|
|
}
|
2017-11-22 14:58:45 +00:00
|
|
|
_onWrite (data, offset, withoutResponse, callback) {
|
2017-12-11 19:38:46 +00:00
|
|
|
let result = {}
|
2017-11-22 14:58:45 +00:00
|
|
|
let utf8
|
|
|
|
let obj
|
2017-12-12 18:37:36 +00:00
|
|
|
let fn
|
2017-11-22 14:58:45 +00:00
|
|
|
if (offset) {
|
|
|
|
log.warn(`Offset scenario`)
|
|
|
|
result = bleno.Characteristic.RESULT_ATTR_NOT_LONG
|
|
|
|
return callback(result)
|
|
|
|
}
|
|
|
|
utf8 = data.toString('utf8')
|
|
|
|
obj = JSON.parse(utf8)
|
|
|
|
result = bleno.Characteristic.RESULT_SUCCESS
|
2017-12-12 18:37:36 +00:00
|
|
|
fn = `_on${capitalize(obj.type)}`
|
|
|
|
if (obj.type && this[fn]) {
|
|
|
|
return this[fn](obj, () => {
|
|
|
|
callback(result)
|
2017-12-12 18:09:13 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return callback(result)
|
|
|
|
}
|
2017-12-12 18:37:36 +00:00
|
|
|
|
2017-11-22 14:58:45 +00:00
|
|
|
}
|
|
|
|
_onRead (offset, callback) {
|
|
|
|
const result = bleno.Characteristic.RESULT_SUCCESS
|
2017-12-11 19:38:46 +00:00
|
|
|
const state = getState()
|
2017-12-11 19:44:27 +00:00
|
|
|
const data = new Buffer(JSON.stringify( state ))
|
2017-11-22 14:58:45 +00:00
|
|
|
callback(result, data.slice(offset, data.length))
|
|
|
|
}
|
2017-11-22 14:30:08 +00:00
|
|
|
/**
|
|
|
|
* Binds functions to events that are triggered by BLE messages
|
|
|
|
*
|
|
|
|
* @param {string} eventName Name of the event to to bind
|
|
|
|
* @param {function} callback Invoked when the event is triggered
|
|
|
|
*/
|
|
|
|
on (eventName, callback) {
|
|
|
|
this[`_on${capitalize(eventName)}`] = callback
|
2017-08-22 01:11:07 +00:00
|
|
|
}
|
2017-12-11 19:38:46 +00:00
|
|
|
|
2017-11-22 14:30:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 19:38:46 +00:00
|
|
|
module.exports = BLE
|