From ea89fcee23e92d97bb59187739e6f1d2abcae5fa Mon Sep 17 00:00:00 2001 From: Rafal Walczyna Date: Thu, 17 Sep 2020 09:32:14 +0200 Subject: [PATCH] Add BluetoothGATTServer::isRunning implementation Property isRunning has been added to JS BluetoothGATTServer Changes of property are handled by C++ function BluetoothGATTServer::SetRunningState. It should be called instead of changing BluetoothGATTServer::running_ value manually. [ACR] https://code.sec.samsung.net/jira/browse/TWDAPI-263 [Verification] Tested in Chrome developer console Change-Id: I423dee736624df61d2cd98096407f33e6003e6ea Signed-off-by: Rafal Walczyna --- src/bluetooth/bluetooth_api.js | 18 ++++++++++++++++++ src/bluetooth/bluetooth_gatt_server.cc | 25 ++++++++++++++++++++----- src/bluetooth/bluetooth_gatt_server.h | 7 +++++-- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/bluetooth/bluetooth_api.js b/src/bluetooth/bluetooth_api.js index 9a5ed9e0..b55bc240 100755 --- a/src/bluetooth/bluetooth_api.js +++ b/src/bluetooth/bluetooth_api.js @@ -3739,6 +3739,11 @@ BluetoothAdapter.prototype.getBluetoothProfileHandler = function() { // class BluetoothGATTServer //////////////////////// var _BluetoothGATTServerServices = []; +var _isBluetoothGATTServerRunning = false; + +function _BluetoothGattServerIsRunningChangeListener(result) { + _isBluetoothGATTServerRunning = result.state; +} /* * This set is used in BluetoothGATTServer::start() to check which services @@ -3755,8 +3760,21 @@ var BluetoothGATTServer = function() { return _BluetoothGATTServerServices; }, set: function() {} + }, + isRunning: { + enumerable: true, + get: function() { + return _isBluetoothGATTServerRunning; + }, + set: function() {} } }); + + // Register listener for managing GATTServer start / stop + native.addListener( + 'BluetoothGattServerIsRunningChangeListener', + _BluetoothGattServerIsRunningChangeListener + ); }; var AbortError = new WebAPIException('AbortError', 'An unknown error occurred'); diff --git a/src/bluetooth/bluetooth_gatt_server.cc b/src/bluetooth/bluetooth_gatt_server.cc index 2c7bb9f5..d1a4bd67 100644 --- a/src/bluetooth/bluetooth_gatt_server.cc +++ b/src/bluetooth/bluetooth_gatt_server.cc @@ -15,6 +15,7 @@ */ #include "bluetooth/bluetooth_gatt_server.h" +#include "bluetooth/bluetooth_instance.h" #include "bluetooth/bluetooth_util.h" #include "common/logger.h" @@ -27,7 +28,7 @@ using namespace common::tools; namespace extension { namespace bluetooth { -BluetoothGATTServer::BluetoothGATTServer(const BluetoothInstance& instance, +BluetoothGATTServer::BluetoothGATTServer(BluetoothInstance& instance, BluetoothGATTServerService& service) : instance_{instance}, service_{service}, @@ -82,7 +83,7 @@ void BluetoothGATTServer::Start(picojson::object& out) { } LoggerD("bt_gatt_server_start(): success"); - running_ = true; + SetRunningState(true); ReportSuccess(out); } @@ -112,12 +113,11 @@ void BluetoothGATTServer::Stop(picojson::object& out) { ReportError(result, &out); } else { ReportSuccess(out); - running_ = false; + SetRunningState(false); } } -void BluetoothGATTServer::GetConnectionMtu(const picojson::value& args, - picojson::object& out) { +void BluetoothGATTServer::GetConnectionMtu(const picojson::value& args, picojson::object& out) { ScopeLogger(); const auto client_address = args.get("clientAddress").get(); @@ -403,5 +403,20 @@ bool BluetoothGATTServer::DestroyDescriptor(int total, int index, bt_gatt_h hand return true; } +void BluetoothGATTServer::SetRunningState(bool state) { + ScopeLogger(); + if (state != running_) { + LoggerD("Changing GATTServer running state to: %s", state ? "start" : "stop"); + running_ = state; + picojson::value result{picojson::object{}}; + auto& obj = result.get(); + + obj.insert(std::make_pair("listenerId", "BluetoothGattServerIsRunningChangeListener")); + obj.insert(std::make_pair("state", picojson::value(running_))); + + Instance::PostMessage(&instance_, result.serialize().c_str()); + } +} + } // namespace bluetooth } // namespace extension diff --git a/src/bluetooth/bluetooth_gatt_server.h b/src/bluetooth/bluetooth_gatt_server.h index 770d83fa..9dc72be2 100644 --- a/src/bluetooth/bluetooth_gatt_server.h +++ b/src/bluetooth/bluetooth_gatt_server.h @@ -32,7 +32,7 @@ class BluetoothInstance; class BluetoothGATTServer { public: - BluetoothGATTServer(const BluetoothInstance& instance, BluetoothGATTServerService& service); + BluetoothGATTServer(BluetoothInstance& instance, BluetoothGATTServerService& service); ~BluetoothGATTServer(); void Start(picojson::object& out); @@ -50,12 +50,15 @@ class BluetoothGATTServer { static bool DestroyDescriptor(int total, int index, bt_gatt_h handle, void* user_data); private: - const BluetoothInstance& instance_; + BluetoothInstance& instance_; BluetoothGATTServerService& service_; bool initialized_; + // always use SetRunningState(bool) to change value of running_ bool running_; bt_gatt_server_h handle_; + void SetRunningState(bool state); + PlatformResult UnregisterAllServicesImpl(); PlatformResult DestroyAllGATTObjects(); PlatformResult Initialize(); -- 2.34.1