Add BluetoothGATTServer::isRunning implementation 17/244317/3
authorRafal Walczyna <r.walczyna@samsung.com>
Thu, 17 Sep 2020 07:32:14 +0000 (09:32 +0200)
committerPiotr Kosko <p.kosko@samsung.com>
Thu, 17 Sep 2020 12:20:58 +0000 (12:20 +0000)
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 <r.walczyna@samsung.com>
src/bluetooth/bluetooth_api.js
src/bluetooth/bluetooth_gatt_server.cc
src/bluetooth/bluetooth_gatt_server.h

index 9a5ed9e..b55bc24 100755 (executable)
@@ -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');
index 2c7bb9f..d1a4bd6 100644 (file)
@@ -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<std::string>();
@@ -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<picojson::object>();
+
+    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
index 770d83f..9dc72be 100644 (file)
@@ -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();