From 7b60025570501943787277e9780dfcbb72050dcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20G=C3=BChring?= Date: Mon, 8 Jan 2018 16:45:57 +0100 Subject: [PATCH 1/3] Implement set and get bluetooth of devices. --- lib/units/device/index.js | 1 + lib/units/device/plugins/bluetooth.js | 53 +++++++++++++++++++ lib/units/device/plugins/service.js | 29 ++++++++++ lib/units/websocket/index.js | 20 +++++++ lib/wire/wire.proto | 9 ++++ .../components/stf/control/control-service.js | 10 ++++ .../device-settings-controller.js | 18 +++++++ .../device-settings/device-settings.pug | 12 ++++- vendor/STFService/wire.proto | 18 +++++++ 9 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 lib/units/device/plugins/bluetooth.js diff --git a/lib/units/device/index.js b/lib/units/device/index.js index 08be946b32..1607b63eb0 100644 --- a/lib/units/device/index.js +++ b/lib/units/device/index.js @@ -38,6 +38,7 @@ module.exports = function(options) { .dependency(require('./plugins/account')) .dependency(require('./plugins/ringer')) .dependency(require('./plugins/wifi')) + .dependency(require('./plugins/bluetooth')) .dependency(require('./plugins/sd')) .dependency(require('./plugins/filesystem')) .define(function(options, heartbeat, solo) { diff --git a/lib/units/device/plugins/bluetooth.js b/lib/units/device/plugins/bluetooth.js new file mode 100644 index 0000000000..85083c6889 --- /dev/null +++ b/lib/units/device/plugins/bluetooth.js @@ -0,0 +1,53 @@ +var syrup = require('stf-syrup') + +var logger = require('../../../util/logger') +var wire = require('../../../wire') +var wireutil = require('../../../wire/util') + +module.exports = syrup.serial() + .dependency(require('./service')) + .dependency(require('../support/router')) + .dependency(require('../support/push')) + .define(function(options, service, router, push) { + var log = logger.createLogger('device:plugins:bluetooth') + + router.on(wire.BluetoothSetEnabledMessage, function(channel, message) { + var reply = wireutil.reply(options.serial) + log.info('Setting Bluetooth "%s"', message.enabled) + service.setBluetoothEnabled(message.enabled) + .timeout(30000) + .then(function() { + push.send([ + channel + , reply.okay() + ]) + }) + .catch(function(err) { + log.error('Setting Bluetooth enabled failed', err.stack) + push.send([ + channel + , reply.fail(err.message) + ]) + }) + }) + + router.on(wire.BluetoothGetStatusMessage, function(channel) { + var reply = wireutil.reply(options.serial) + log.info('Getting Bluetooth status') + service.getBluetoothStatus() + .timeout(30000) + .then(function(enabled) { + push.send([ + channel + , reply.okay(enabled ? 'bluetooth_enabled' : 'bluetooth_disabled') + ]) + }) + .catch(function(err) { + log.error('Getting Bluetooth status failed', err.stack) + push.send([ + channel + , reply.fail(err.message) + ]) + }) + }) + }) diff --git a/lib/units/device/plugins/service.js b/lib/units/device/plugins/service.js index 459c1e5a2b..c1dc340b72 100644 --- a/lib/units/device/plugins/service.js +++ b/lib/units/device/plugins/service.js @@ -672,6 +672,35 @@ module.exports = syrup.serial() }) } + plugin.setBluetoothEnabled = function(enabled) { + return runServiceCommand( + apk.wire.MessageType.SET_BLUETOOTH_ENABLED + , new apk.wire.SetBluetoothEnabledRequest(enabled) + ) + .timeout(10000) + .then(function(data) { + var response = apk.wire.SetBluetoothEnabledResponse.decode(data) + if (!response.success) { + throw new Error('Unable to set Bluetooth') + } + }) + } + + plugin.getBluetoothStatus = function() { + return runServiceCommand( + apk.wire.MessageType.GET_BLUETOOTH_STATUS + , new apk.wire.GetBluetoothStatusRequest() + ) + .timeout(10000) + .then(function(data) { + var response = apk.wire.GetBluetoothStatusResponse.decode(data) + if (response.success) { + return response.status + } + throw new Error('Unable to get Bluetooth status') + }) + } + plugin.getSdStatus = function() { return runServiceCommand( apk.wire.MessageType.GET_SD_STATUS diff --git a/lib/units/websocket/index.js b/lib/units/websocket/index.js index 5ed6919495..9b31345160 100644 --- a/lib/units/websocket/index.js +++ b/lib/units/websocket/index.js @@ -673,6 +673,26 @@ module.exports = function(options) { ) ]) }) + .on('bluetooth.set', function(channel, responseChannel, data) { + joinChannel(responseChannel) + push.send([ + channel + , wireutil.transaction( + responseChannel + , new wire.BluetoothSetEnabledMessage(data.enabled) + ) + ]) + }) + .on('bluetooth.get', function(channel, responseChannel) { + joinChannel(responseChannel) + push.send([ + channel + , wireutil.transaction( + responseChannel + , new wire.BluetoothGetStatusMessage() + ) + ]) + }) .on('group.invite', function(channel, responseChannel, data) { joinChannel(responseChannel) push.send([ diff --git a/lib/wire/wire.proto b/lib/wire/wire.proto index 2b2c60ca69..ed5fa6a2a1 100644 --- a/lib/wire/wire.proto +++ b/lib/wire/wire.proto @@ -67,6 +67,8 @@ enum MessageType { RingerGetMessage = 64; WifiSetEnabledMessage = 57; WifiGetStatusMessage = 58; + BluetoothSetEnabledMessage = 94; + BluetoothGetStatusMessage = 95; AccountAddMenuMessage = 59; AccountAddMessage = 60; AccountCheckMessage = 63; @@ -531,6 +533,13 @@ message WifiSetEnabledMessage { message WifiGetStatusMessage { } +message BluetoothSetEnabledMessage { + required bool enabled = 1; +} + +message BluetoothGetStatusMessage { +} + // Events, these must be kept in sync with STFService/wire.proto message AirplaneModeEvent { diff --git a/res/app/components/stf/control/control-service.js b/res/app/components/stf/control/control-service.js index a274030a79..fa38966175 100644 --- a/res/app/components/stf/control/control-service.js +++ b/res/app/components/stf/control/control-service.js @@ -293,6 +293,16 @@ module.exports = function ControlServiceFactory( return sendTwoWay('wifi.get') } + this.setBluetoothEnabled = function(enabled) { + return sendTwoWay('bluetooth.set', { + enabled: enabled + }) + } + + this.getBluetoothStatus = function() { + return sendTwoWay('bluetooth.get') + } + window.cc = this } diff --git a/res/app/control-panes/automation/device-settings/device-settings-controller.js b/res/app/control-panes/automation/device-settings/device-settings-controller.js index 22de39d20e..14ac897cc6 100644 --- a/res/app/control-panes/automation/device-settings/device-settings-controller.js +++ b/res/app/control-panes/automation/device-settings/device-settings-controller.js @@ -19,6 +19,24 @@ module.exports = function DeviceSettingsCtrl($scope, $timeout) { } } + function getBluetoothStatus() { + if ($scope.control) { + $scope.control.getBluetoothStatus().then(function(result) { + $scope.$apply(function() { + $scope.bluetoothEnabled = (result.lastData === 'bluetooth_enabled') + }) + }) + } + } + getBluetoothStatus() + + $scope.toggleBluetooth = function(enable) { + if ($scope.control) { + $scope.control.setBluetoothEnabled(enable) + $timeout(getBluetoothStatus, 2500) + } + } + $scope.$watch('ringerMode', function(newValue, oldValue) { if (oldValue) { if ($scope.control) { diff --git a/res/app/control-panes/automation/device-settings/device-settings.pug b/res/app/control-panes/automation/device-settings/device-settings.pug index 6e31287fdb..ec207972b6 100644 --- a/res/app/control-panes/automation/device-settings/device-settings.pug +++ b/res/app/control-panes/automation/device-settings/device-settings.pug @@ -4,7 +4,7 @@ span(translate) Device Settings .widget-content.padded .row - .col-md-6 + .col-md-4 h6(translate) Manner Mode .btn-group label.btn.btn-sm.btn-primary-outline(ng-model='ringerMode', uib-btn-radio='"SILENT"', uib-tooltip='{{"Silent Mode" | translate}}') @@ -14,7 +14,7 @@ label.btn.btn-sm.btn-primary-outline(ng-model='ringerMode', uib-btn-radio='"NORMAL"', uib-tooltip='{{"Normal Mode" | translate}}') i.fa.fa-volume-up.fa-fw - .col-md-6 + .col-md-4 h6(translate) WiFi .btn-group label.btn.btn-sm.btn-primary-outline(ng-model='wifiEnabled', ng-click='toggleWifi(false)', uib-btn-radio='false', uib-tooltip='{{"Disable WiFi" | translate}}') @@ -22,6 +22,14 @@ label.btn.btn-sm.btn-primary-outline(ng-model='wifiEnabled', ng-click='toggleWifi(true)', uib-btn-radio='true', uib-tooltip='{{"Enable WiFi" | translate}}') i.fa.fa-wifi.fa-fw + .col-md-4 + h6(translate) Bluetooth + .btn-group + label.btn.btn-sm.btn-primary-outline(ng-model='bluetoothEnabled', ng-click='toggleBluetooth(false)', uib-btn-radio='false', uib-tooltip='{{"Disable WiFi" | translate}}') + i.fa.fa-power-off.fa-fw + label.btn.btn-sm.btn-primary-outline(ng-model='bluetoothEnabled', ng-click='toggleBluetooth(true)', uib-btn-radio='true', uib-tooltip='{{"Enable WiFi" | translate}}') + i.fa.fa-wifi.fa-fw + //.row .col-md-12 h6(translate) Lock Rotation diff --git a/vendor/STFService/wire.proto b/vendor/STFService/wire.proto index f0a03fa09e..26e4c33c68 100644 --- a/vendor/STFService/wire.proto +++ b/vendor/STFService/wire.proto @@ -20,12 +20,14 @@ enum MessageType { GET_SD_STATUS = 25; GET_VERSION = 8; GET_WIFI_STATUS = 23; + GET_BLUETOOTH_STATUS = 29; SET_CLIPBOARD = 9; SET_KEYGUARD_STATE = 10; SET_RINGER_MODE = 21; SET_ROTATION = 12; SET_WAKE_LOCK = 11; SET_WIFI_ENABLED = 22; + SET_BLUETOOTH_ENABLED = 30; SET_MASTER_MUTE = 28; EVENT_AIRPLANE_MODE = 13; EVENT_BATTERY = 14; @@ -246,6 +248,22 @@ message GetWifiStatusResponse { required bool status = 2; } +message SetBluetoothEnabledRequest { + required bool enabled = 1; +} + +message SetBluetoothEnabledResponse { + required bool success = 1; +} + +message GetBluetoothStatusRequest { +} + +message GetBluetoothStatusResponse { + required bool success = 1; + required bool status = 2; +} + message GetSdStatusRequest { } From 9f54238ad10448f7463f0ae8b974eb7bf3a70960 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20G=C3=BChring?= Date: Thu, 11 Jan 2018 11:04:26 +0100 Subject: [PATCH 2/3] Change WiFi symbol to Bluetooth symbol in device settings view. --- .../automation/device-settings/device-settings.pug | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/res/app/control-panes/automation/device-settings/device-settings.pug b/res/app/control-panes/automation/device-settings/device-settings.pug index ec207972b6..e03def0f52 100644 --- a/res/app/control-panes/automation/device-settings/device-settings.pug +++ b/res/app/control-panes/automation/device-settings/device-settings.pug @@ -28,7 +28,7 @@ label.btn.btn-sm.btn-primary-outline(ng-model='bluetoothEnabled', ng-click='toggleBluetooth(false)', uib-btn-radio='false', uib-tooltip='{{"Disable WiFi" | translate}}') i.fa.fa-power-off.fa-fw label.btn.btn-sm.btn-primary-outline(ng-model='bluetoothEnabled', ng-click='toggleBluetooth(true)', uib-btn-radio='true', uib-tooltip='{{"Enable WiFi" | translate}}') - i.fa.fa-wifi.fa-fw + i.fa.fa-bluetooth-b.fa-fw //.row .col-md-12 From 1c33940d560058bfdc745b6ef74e1fee42b9ac64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20G=C3=BChring?= Date: Mon, 23 Jul 2018 13:25:30 +0200 Subject: [PATCH 3/3] Change order in wire.proto for BluetoothSetEnabledMessage --- lib/wire/wire.proto | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/wire/wire.proto b/lib/wire/wire.proto index ed5fa6a2a1..6066cce25c 100644 --- a/lib/wire/wire.proto +++ b/lib/wire/wire.proto @@ -67,7 +67,6 @@ enum MessageType { RingerGetMessage = 64; WifiSetEnabledMessage = 57; WifiGetStatusMessage = 58; - BluetoothSetEnabledMessage = 94; BluetoothGetStatusMessage = 95; AccountAddMenuMessage = 59; AccountAddMessage = 60; @@ -80,6 +79,7 @@ enum MessageType { FileSystemGetMessage = 82; ConnectStartedMessage = 92; ConnectStoppedMessage = 93; + BluetoothSetEnabledMessage = 94; } message ConnectStartedMessage {