[Bluetooth] Allow to create multiple instances of BluetoothInstance class.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Wed, 8 Apr 2015 11:06:26 +0000 (13:06 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Tue, 14 Apr 2015 07:57:15 +0000 (16:57 +0900)
Change-Id: I0424009bd9cdb0d4a199b1b9601bb1518d1c7535

17 files changed:
src/bluetooth/bluetooth_adapter.cc
src/bluetooth/bluetooth_adapter.h
src/bluetooth/bluetooth_device.cc
src/bluetooth/bluetooth_device.h
src/bluetooth/bluetooth_extension.cc
src/bluetooth/bluetooth_health_application.cc
src/bluetooth/bluetooth_health_application.h
src/bluetooth/bluetooth_health_profile_handler.cc
src/bluetooth/bluetooth_health_profile_handler.h
src/bluetooth/bluetooth_instance.cc
src/bluetooth/bluetooth_instance.h
src/bluetooth/bluetooth_service_handler.cc
src/bluetooth/bluetooth_service_handler.h
src/bluetooth/bluetooth_socket.cc
src/bluetooth/bluetooth_socket.h
src/bluetooth/bluetooth_util.cc
src/bluetooth/bluetooth_util.h

index 52a5513954bc82eed1b634752312a820aec0e5d0..e8ff334a5dc5f1e0a084c314a40227c6a0c3b7ca 100644 (file)
 #include "common/extension.h"
 #include "common/task-queue.h"
 
-#include "bluetooth_class.h"
-#include "bluetooth_device.h"
-#include "bluetooth_privilege.h"
-#include "bluetooth_socket.h"
-#include "bluetooth_util.h"
+#include "bluetooth/bluetooth_class.h"
+#include "bluetooth/bluetooth_device.h"
+#include "bluetooth/bluetooth_instance.h"
+#include "bluetooth/bluetooth_privilege.h"
+#include "bluetooth/bluetooth_socket.h"
+#include "bluetooth/bluetooth_util.h"
 
 namespace extension {
 namespace bluetooth {
@@ -125,7 +126,7 @@ void BluetoothAdapter::StateChangedCB(int result, bt_adapter_state_e state, void
     data_obj->insert(std::make_pair(kAction, picojson::value(kOnStateChanged)));
     data_obj->insert(std::make_pair(kAdapterPowered, picojson::value(powered)));
 
-    util::FireEvent(kAdapterChangeCallbackEvent, value);
+    adapter->instance_.FireEvent(kAdapterChangeCallbackEvent, value);
   }
 
   if (adapter->user_request_list_[SET_POWERED]) {
@@ -147,7 +148,7 @@ void BluetoothAdapter::StateChangedCB(int result, bt_adapter_state_e state, void
         ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception");
     }
 
-    util::AsyncResponse(adapter->user_request_callback_[SET_POWERED], ret);
+    adapter->instance_.AsyncResponse(adapter->user_request_callback_[SET_POWERED], ret);
     adapter->user_request_list_[SET_POWERED] = false;
   }
 }
@@ -167,12 +168,12 @@ void BluetoothAdapter::NameChangedCB(char *name, void *user_data) {
   data_obj->insert(std::make_pair(kAction, picojson::value(kOnNameChanged)));
   data_obj->insert(std::make_pair(kName, picojson::value(name)));
 
-  util::FireEvent(kAdapterChangeCallbackEvent, value);
+  adapter->instance_.FireEvent(kAdapterChangeCallbackEvent, value);
 
   if (adapter->user_request_list_[SET_NAME] && name == adapter->requested_name_) {
     std::shared_ptr<picojson::value> response =
         std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
-    util::AsyncResponse(adapter->user_request_callback_[SET_NAME], response);
+    adapter->instance_.AsyncResponse(adapter->user_request_callback_[SET_NAME], response);
     adapter->user_request_list_[SET_NAME] = false;
   }
 }
@@ -197,7 +198,7 @@ void BluetoothAdapter::VisibilityChangedCB(int result, bt_adapter_visibility_mod
     data_obj->insert(std::make_pair(kAction, picojson::value(kOnVisibilityChanged)));
     data_obj->insert(std::make_pair(kAdapterVisible, picojson::value(visible)));
 
-    util::FireEvent(kAdapterChangeCallbackEvent, value);
+    adapter->instance_.FireEvent(kAdapterChangeCallbackEvent, value);
   }
 
   if (adapter->user_request_list_[SET_VISIBLE] && adapter->requested_visibility_ == mode) {
@@ -207,7 +208,7 @@ void BluetoothAdapter::VisibilityChangedCB(int result, bt_adapter_visibility_mod
       ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception");
     }
 
-    util::AsyncResponse(adapter->user_request_callback_[SET_VISIBLE], ret);
+    adapter->instance_.AsyncResponse(adapter->user_request_callback_[SET_VISIBLE], ret);
     adapter->user_request_list_[SET_VISIBLE] = false;
   }
 }
@@ -267,10 +268,10 @@ void BluetoothAdapter::DiscoveryStateChangedCB(
           adapter->discovered_devices_.clear();
 
           data_obj->insert(std::make_pair(kAction, picojson::value(kOnDiscoverStarted)));
-          util::FireEvent(kAdapterDiscoverSuccessEvent, value);
+          adapter->instance_.FireEvent(kAdapterDiscoverSuccessEvent, value);
         } else {
           ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"), data_obj);
-          util::FireEvent(kAdapterDiscoverErrorEvent, value);
+          adapter->instance_.FireEvent(kAdapterDiscoverErrorEvent, value);
           adapter->user_request_list_[DISCOVER_DEVICES] = false;
         }
       }
@@ -289,11 +290,11 @@ void BluetoothAdapter::DiscoveryStateChangedCB(
                                                   picojson::value(kOnDiscoverDisappeared)));
             disapeared_obj->insert(std::make_pair(kData, picojson::value(it)));
 
-            util::FireEvent(kAdapterDiscoverSuccessEvent, disapeared_val);
+            adapter->instance_.FireEvent(kAdapterDiscoverSuccessEvent, disapeared_val);
           }
 
           data_obj->insert(std::make_pair(kData, picojson::value(adapter->discovered_devices_)));
-          util::FireEvent(kAdapterDiscoverSuccessEvent, value);
+          adapter->instance_.FireEvent(kAdapterDiscoverSuccessEvent, value);
 
           adapter->user_request_list_[DISCOVER_DEVICES] = false;
         }
@@ -303,7 +304,7 @@ void BluetoothAdapter::DiscoveryStateChangedCB(
               std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
 
           ReportSuccess(response->get<picojson::object>());
-          util::AsyncResponse(adapter->user_request_callback_[STOP_DISCOVERY], response);
+          adapter->instance_.AsyncResponse(adapter->user_request_callback_[STOP_DISCOVERY], response);
 
           adapter->user_request_list_[STOP_DISCOVERY] = false;
         }
@@ -324,7 +325,7 @@ void BluetoothAdapter::DiscoveryStateChangedCB(
           BluetoothDevice::ToJson(discovery_info, &data.get<picojson::object>());
           adapter->discovered_devices_.push_back(data);
 
-          util::FireEvent(kAdapterDiscoverSuccessEvent, value);
+          adapter->instance_.FireEvent(kAdapterDiscoverSuccessEvent, value);
         }
       }
       break;
@@ -335,10 +336,11 @@ void BluetoothAdapter::DiscoveryStateChangedCB(
   }
 }
 
-BluetoothAdapter::BluetoothAdapter() :
+BluetoothAdapter::BluetoothAdapter(BluetoothInstance& instance) :
     is_visible_(false),
     is_powered_(false),
-    is_initialized_(false)
+    is_initialized_(false),
+    instance_(instance)
 {
   LoggerD("Entered");
   if (BT_ERROR_NONE == bt_initialize()) {
@@ -395,11 +397,6 @@ BluetoothAdapter::~BluetoothAdapter() {
   }
 }
 
-BluetoothAdapter& BluetoothAdapter::GetInstance() {
-  static BluetoothAdapter instance;
-  return instance;
-}
-
 std::string BluetoothAdapter::get_name() const {
   char* name = nullptr;
   std::string str_name = "";
@@ -451,19 +448,19 @@ void BluetoothAdapter::SetName(const picojson::value& data, picojson::object& ou
   PlatformResult result = PlatformResult(ErrorCode::NO_ERROR);
   if (!this->is_initialized()) {
     result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized.");
-    util::AsyncResponse(callback_handle, result);
+    instance_.AsyncResponse(callback_handle, result);
     return;
   }
 
   if (this->get_powered()) {
     if (get_name() == name) {
-      util::AsyncResponse(callback_handle, result);
+      instance_.AsyncResponse(callback_handle, result);
       return;
     }
 
     if (this->user_request_list_[SET_NAME]) {
       result = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested");
-      util::AsyncResponse(callback_handle, result);
+      instance_.AsyncResponse(callback_handle, result);
       return;
     }
 
@@ -491,7 +488,7 @@ void BluetoothAdapter::SetName(const picojson::value& data, picojson::object& ou
     this->user_request_list_[SET_NAME] = false;
   }
 
-  util::AsyncResponse(callback_handle, result);
+  instance_.AsyncResponse(callback_handle, result);
 }
 
 void BluetoothAdapter::SetPowered(const picojson::value& data, picojson::object& out) {
@@ -528,7 +525,7 @@ void BluetoothAdapter::SetPowered(const picojson::value& data, picojson::object&
     return;
   }
 
-  util::AsyncResponse(callback_handle, ret);
+  instance_.AsyncResponse(callback_handle, ret);
 }
 
 void BluetoothAdapter::SetVisible(const picojson::value& data, picojson::object& out) {
@@ -569,14 +566,14 @@ void BluetoothAdapter::SetVisible(const picojson::value& data, picojson::object&
     int time = 0;
     if (BT_ERROR_NONE != bt_adapter_get_visibility(&current , &time)) {
       result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception");
-      util::AsyncResponse(callback_handle, result);
+      instance_.AsyncResponse(callback_handle, result);
       return;
     }
 
     if (mode == current) {
       if (BT_ADAPTER_VISIBILITY_MODE_LIMITED_DISCOVERABLE != mode ||
           (unsigned int)time != timeout) {
-        util::AsyncResponse(callback_handle, result);
+        instance_.AsyncResponse(callback_handle, result);
         return;
       }
     }
@@ -601,7 +598,7 @@ void BluetoothAdapter::SetVisible(const picojson::value& data, picojson::object&
     result = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off");
   }
 
-  util::AsyncResponse(callback_handle, result);
+  instance_.AsyncResponse(callback_handle, result);
 }
 
 void BluetoothAdapter::DiscoverDevices(const picojson::value& /* data */, picojson::object& out) {
@@ -632,8 +629,8 @@ void BluetoothAdapter::DiscoverDevices(const picojson::value& /* data */, picojs
         std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
 
     ReportError(result, &response->get<picojson::object>());
-    TaskQueue::GetInstance().Async<picojson::value>([](const std::shared_ptr<picojson::value>& result) {
-      util::FireEvent(kAdapterDiscoverErrorEvent, result);
+    TaskQueue::GetInstance().Async<picojson::value>([this](const std::shared_ptr<picojson::value>& result) {
+      instance_.FireEvent(kAdapterDiscoverErrorEvent, result);
     }, response);
   }
 }
@@ -660,7 +657,7 @@ void BluetoothAdapter::StopDiscovery(const picojson::value& data, picojson::obje
     bt_adapter_is_discovering(&is_discovering);
 
     if (!is_discovering) {
-      util::AsyncResponse(callback_handle, result);
+      instance_.AsyncResponse(callback_handle, result);
       return;
     }
 
@@ -682,7 +679,7 @@ void BluetoothAdapter::StopDiscovery(const picojson::value& data, picojson::obje
   }
 
   if (result.IsError()) {
-    util::AsyncResponse(callback_handle, result);
+    instance_.AsyncResponse(callback_handle, result);
   }
 }
 
@@ -722,9 +719,9 @@ void BluetoothAdapter::GetKnownDevices(const picojson::value& data, picojson::ob
       ReportError(ret, &response->get<picojson::object>());
     }
   };
-  auto get_known_devices_response = [callback_handle](
+  auto get_known_devices_response = [this, callback_handle](
       const std::shared_ptr<picojson::value>& response) -> void {
-    util::SyncResponse(callback_handle, response);
+    instance_.SyncResponse(callback_handle, response);
   };
 
   TaskQueue::GetInstance().Queue<picojson::value>(
@@ -791,9 +788,9 @@ void BluetoothAdapter::GetDevice(const picojson::value& data, picojson::object&
     }
   };
 
-  auto get_device_response = [callback_handle](
+  auto get_device_response = [this, callback_handle](
       const std::shared_ptr<picojson::value>& response) -> void {
-    util::SyncResponse(callback_handle, response);
+    instance_.SyncResponse(callback_handle, response);
   };
 
   TaskQueue::GetInstance().Queue<picojson::value>(
@@ -806,8 +803,8 @@ void BluetoothAdapter::GetDevice(const picojson::value& data, picojson::object&
 
 class BondingHandler {
  public:
-  BondingHandler(double callback_handle, const std::string& address) :
-    callback_handle_(callback_handle), address_(address) {}
+  BondingHandler(BluetoothInstance& instance, double callback_handle, const std::string& address) :
+    instance_(instance) ,callback_handle_(callback_handle), address_(address) {}
 
   void set_address(const std::string& address) {
     address_ = address;
@@ -826,10 +823,11 @@ class BondingHandler {
       ReportSuccess(response->get<picojson::object>());
     }
 
-    util::AsyncResponse(callback_handle_, response);
+    instance_.AsyncResponse(callback_handle_, response);
   }
 
  private:
+  BluetoothInstance& instance_;
   double callback_handle_;
   std::string address_;
 };
@@ -899,7 +897,7 @@ void BluetoothAdapter::CreateBonding(const picojson::value& data, picojson::obje
         }
       };
 
-      BondingHandler* handler = new BondingHandler(callback_handle, address);
+      BondingHandler* handler = new BondingHandler(instance_, callback_handle, address);
       bt_device_set_bond_created_cb(bond_create_callback, handler);
       int ret = bt_device_create_bond(address.c_str());
 
@@ -932,7 +930,7 @@ void BluetoothAdapter::CreateBonding(const picojson::value& data, picojson::obje
     }
 
     if (result.IsError()) {
-      util::AsyncResponse(callback_handle, result);
+      instance_.AsyncResponse(callback_handle, result);
     }
   };
   TaskQueue::GetInstance().Queue(create_bonding);
@@ -998,7 +996,7 @@ void BluetoothAdapter::DestroyBonding(const picojson::value& data, picojson::obj
           }
         };
 
-        BondingHandler* handler = new BondingHandler(callback_handle, address);
+        BondingHandler* handler = new BondingHandler(instance_, callback_handle, address);
         bt_device_set_bond_destroyed_cb(bond_destroy_callback, handler);
 
         int ret = bt_device_destroy_bond(address.c_str());
@@ -1033,7 +1031,7 @@ void BluetoothAdapter::DestroyBonding(const picojson::value& data, picojson::obj
     }
 
     if (result.IsError()) {
-      util::AsyncResponse(callback_handle, result);
+      instance_.AsyncResponse(callback_handle, result);
     }
   };
   TaskQueue::GetInstance().Queue(destroy_bonding);
@@ -1116,8 +1114,8 @@ void BluetoothAdapter::RegisterRFCOMMServiceByUUID(const picojson::value& data,
     }
   };
 
-  auto rfcomm_response = [callback_handle](const std::shared_ptr<picojson::value>& response) -> void {
-    util::SyncResponse(callback_handle, response);
+  auto rfcomm_response = [this, callback_handle](const std::shared_ptr<picojson::value>& response) -> void {
+    instance_.SyncResponse(callback_handle, response);
   };
 
   TaskQueue::GetInstance().Queue<picojson::value>(
@@ -1155,7 +1153,7 @@ void BluetoothAdapter::UnregisterUUID(const std::string& uuid, int callback_hand
     result = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off");
   }
 
-  util::AsyncResponse(callback_handle, result);
+  instance_.AsyncResponse(callback_handle, result);
 }
 
 void BluetoothAdapter::GetBluetoothProfileHandler(const picojson::value& data,
@@ -1264,7 +1262,7 @@ void BluetoothAdapter::OnSocketConnected(
         iter->second.second = true;
 
         // Call BluetoothServiceHandler.onconnect
-        util::FireEvent("BLUETOOTH_SERVICE_ONCONNECT", BluetoothSocket::ToJson(connection));
+        object->instance_.FireEvent("BLUETOOTH_SERVICE_ONCONNECT", BluetoothSocket::ToJson(connection));
 
         // Update connected_sockets_
         object->connected_sockets_.push_back(connection->socket_fd);
@@ -1316,7 +1314,7 @@ void BluetoothAdapter::OnSocketConnected(
             ErrorCode::NOT_FOUND_ERR, "Not found"), &response->get<picojson::object>());
       }
 
-      util::AsyncResponse(request->second->callback_handle_, response);
+      object->instance_.AsyncResponse(request->second->callback_handle_, response);
 
       // request has been handled, can be safely removed
       object->connection_requests_.erase(request);
@@ -1370,7 +1368,7 @@ void BluetoothAdapter::OnSocketReceivedData(bt_socket_received_data_s* data, voi
   // Store received data
   object->StoreSocketData(data);
 
-  InvokeSocketOnMessageEvent(*it);
+  object->InvokeSocketOnMessageEvent(*it);
 }
 
 void BluetoothAdapter::ConnectToServiceByUUID(
@@ -1414,17 +1412,17 @@ void BluetoothAdapter::ConnectToServiceByUUID(
   }
 
   if (result.IsError()) {
-    util::AsyncResponse(callback_handle, result);
+    instance_.AsyncResponse(callback_handle, result);
   }
 }
 
 
-static void InvokeSocketEvent(int id, const char* event) {
+void BluetoothAdapter::InvokeSocketEvent(int id, const char* event) {
   picojson::value value = picojson::value(picojson::object());
   picojson::object& value_obj = value.get<picojson::object>();
   value_obj.insert(std::make_pair("id", picojson::value(std::to_string(id))));
   value_obj.insert(std::make_pair("event", picojson::value(event)));
-  util::FireEvent("BLUETOOTH_SOCKET_STATE_CHANGED", value);
+  instance_.FireEvent("BLUETOOTH_SOCKET_STATE_CHANGED", value);
 }
 
 void BluetoothAdapter::InvokeSocketOnMessageEvent(int id) {
@@ -1453,7 +1451,7 @@ void BluetoothAdapter::RemoveSocket(int socket) {
 
   connected_sockets_.erase(it);
 
-  BluetoothAdapter::InvokeSocketOnCloseEvent(*it);
+  InvokeSocketOnCloseEvent(*it);
 }
 
 void BluetoothAdapter::StoreSocketData(bt_socket_received_data_s* data) {
index b59acb2915fc7f2bd4886ca95beb8197d4355262..a7857809277c38ce92b5879b5e0677b468fea790 100644 (file)
@@ -38,6 +38,8 @@ enum AdapterAsyncEvent {
   STOP_DISCOVERY
 };
 
+class BluetoothInstance;
+
 class BluetoothAdapter {
  public:
   /**
@@ -284,7 +286,7 @@ class BluetoothAdapter {
    */
   void IsServiceConnected(const picojson::value& data, picojson::object& out);
 
-  static BluetoothAdapter& GetInstance();
+  explicit BluetoothAdapter(BluetoothInstance& instance);
   virtual ~BluetoothAdapter();
 
   std::string get_name() const;
@@ -305,7 +307,6 @@ class BluetoothAdapter {
   void UnregisterUUID(const std::string& uuid, int callback_handle);
 
  private:
-  BluetoothAdapter();
   BluetoothAdapter(const BluetoothAdapter&) = delete;
   BluetoothAdapter& operator=(const BluetoothAdapter&) = delete;
 
@@ -334,9 +335,9 @@ class BluetoothAdapter {
   static void OnSocketReceivedData(bt_socket_received_data_s* data,
                                    void* user_data);
 
-  static void InvokeSocketOnMessageEvent(int id);
-
-  static void InvokeSocketOnCloseEvent(int id);
+  void InvokeSocketEvent(int id, const char* event);
+  void InvokeSocketOnMessageEvent(int id);
+  void InvokeSocketOnCloseEvent(int id);
 
   bool is_visible_;
   bool is_powered_;
@@ -372,6 +373,8 @@ class BluetoothAdapter {
   RegisteredUuidMap registered_uuids_;
 
   std::unordered_map<int, std::list<char>> socket_data_;
+
+  BluetoothInstance& instance_;
 };
 
 } // namespace bluetooth
index 4f392a2311367f3aaaae7895d79d794168cdde43..f9166fff86ef27b203d05593d170b602f0b7e78c 100644 (file)
@@ -78,6 +78,10 @@ static void ToJsonFromUUID(char **service_uuid, int service_count, picojson::obj
   }
 }
 
+BluetoothDevice::BluetoothDevice(BluetoothAdapter& adapter)
+    : adapter_(adapter) {
+}
+
 void BluetoothDevice::ToJson(bt_device_info_s* info, picojson::object* device) {
   LoggerD("Entered");
   device->insert(std::make_pair(kDeviceName, picojson::value(std::string(info->remote_name))));
@@ -104,10 +108,9 @@ void BluetoothDevice::ConnectToServiceByUUID(const picojson::value& data, picojs
 
   const auto& args = util::GetArguments(data);
 
-  BluetoothAdapter::GetInstance().ConnectToServiceByUUID(
-      FromJson<std::string>(args, "address"),
-      FromJson<std::string>(args, "uuid"),
-      util::GetAsyncCallbackHandle(data));
+  adapter_.ConnectToServiceByUUID(FromJson<std::string>(args, "address"),
+                                  FromJson<std::string>(args, "uuid"),
+                                  util::GetAsyncCallbackHandle(data));
 
   ReportSuccess(out);
 }
index ad697bd6ff3071893fcacb632ac09660a7d71d59..3ac06afaf9b56ee5209ec6e99378e975c238634d 100644 (file)
 namespace extension {
 namespace bluetooth {
 
+class BluetoothAdapter;
+
 class BluetoothDevice {
  public:
+  explicit BluetoothDevice(BluetoothAdapter& adapter);
+
   /**
    * Signature: @code void connectToServiceByUUID(uuid, successCallback, errorCallback); @endcode
    * JSON: @code data: {method: 'BluetoothDevice_connectToServiceByUUID', args: {uuid: uuid}} @endcode
@@ -60,6 +64,9 @@ class BluetoothDevice {
                      picojson::object* device);
   static void ToJson(bt_adapter_device_discovery_info_s *info,
                      picojson::object* device);
+
+ private:
+  BluetoothAdapter& adapter_;
 };
 
 } // namespace bluetooth
index 9bd9086e9f212f69bfbdd607974ad40cb5bcf1a2..4663f3311d2e7721e620419f742e1bc4b89c72d6 100644 (file)
@@ -27,5 +27,5 @@ BluetoothExtension::BluetoothExtension() {
 BluetoothExtension::~BluetoothExtension() {}
 
 common::Instance* BluetoothExtension::CreateInstance() {
-  return &extension::bluetooth::BluetoothInstance::GetInstance();
+  return new extension::bluetooth::BluetoothInstance();
 }
index fc8cc4c0667c1958f142e804e7ace2fd8775ef39..aec0358ffab9d5feb7d6ab04fb832af9cf775da5 100644 (file)
@@ -35,6 +35,11 @@ const std::string kId = "_id";
 
 using namespace common;
 
+BluetoothHealthApplication::BluetoothHealthApplication(
+    BluetoothHealthProfileHandler& handler)
+    : handler_(handler) {
+}
+
 void BluetoothHealthApplication::Unregister(const picojson::value& data, picojson::object& out) {
   LoggerD("Entered");
 
@@ -42,9 +47,8 @@ void BluetoothHealthApplication::Unregister(const picojson::value& data, picojso
 
   const auto& args = util::GetArguments(data);
 
-  BluetoothHealthProfileHandler::GetInstance().UnregisterSinkAppAsync(
-      FromJson<std::string>(args, "id"),
-      util::GetAsyncCallbackHandle(data));
+  handler_.UnregisterSinkAppAsync(FromJson<std::string>(args, "id"),
+                                  util::GetAsyncCallbackHandle(data));
 
   tools::ReportSuccess(out);
 }
index 831dcb9176cc687d6d6a7f06e7e058781d5076b6..5ee9a9023aa8bae86ab9ead9f539408994cea69a 100644 (file)
 namespace extension {
 namespace bluetooth {
 
+class BluetoothHealthProfileHandler;
+
 class BluetoothHealthApplication {
  public:
+  explicit BluetoothHealthApplication(BluetoothHealthProfileHandler& handler);
+
   /**
    * Signature: @code void unregister(successCallback, errorCallback); @endcode
    * JSON: @code data: {method: 'BluetoothHealthApplication_unregister', args: {}} @endcode
@@ -46,6 +50,9 @@ class BluetoothHealthApplication {
                      const std::string& name,
                      const char* id,
                      picojson::object* out);
+
+ private:
+  BluetoothHealthProfileHandler& handler_;
 };
 
 } // namespace bluetooth
index f0b4d0a026557d5037176e9b0bf92505537b28f0..e46d9c002cfc81883caa7d89b705438255b10193 100644 (file)
 #include "common/extension.h"
 #include "common/task-queue.h"
 
-#include "bluetooth_adapter.h"
-#include "bluetooth_health_application.h"
-#include "bluetooth_health_channel.h"
-#include "bluetooth_privilege.h"
-#include "bluetooth_util.h"
+#include "bluetooth/bluetooth_adapter.h"
+#include "bluetooth/bluetooth_instance.h"
+#include "bluetooth/bluetooth_health_application.h"
+#include "bluetooth/bluetooth_health_channel.h"
+#include "bluetooth/bluetooth_privilege.h"
+#include "bluetooth/bluetooth_util.h"
 
 namespace extension {
 namespace bluetooth {
@@ -43,12 +44,8 @@ const std::string kOnMessage = "onmessage";
 const std::string kChangeCallback = "BluetoothHealthChannelChangeCallback";
 } //namespace
 
-BluetoothHealthProfileHandler& BluetoothHealthProfileHandler::GetInstance() {
-  static BluetoothHealthProfileHandler instance;
-  return instance;
-}
-
-BluetoothHealthProfileHandler::BluetoothHealthProfileHandler() {
+BluetoothHealthProfileHandler::BluetoothHealthProfileHandler(BluetoothInstance& instance)
+    : instance_(instance) {
   // initialize listeners
   if (BT_ERROR_NONE != bt_hdp_set_connection_state_changed_cb(OnConnected, OnDisconnected, this)) {
     LoggerE("bt_hdp_set_connection_state_changed_cb() failed");
@@ -126,7 +123,7 @@ void BluetoothHealthProfileHandler::OnConnected(int result,
       ReportSuccess(result, response_obj);
       bt_adapter_free_device_info(device_info);
 
-      util::FireEvent("BLUETOOTH_HEALTH_APPLICATION_CHANGED", response);
+      object->instance_.FireEvent("BLUETOOTH_HEALTH_APPLICATION_CHANGED", response);
     } else {
       LoggerE("Failed to get device info");
     }
@@ -159,7 +156,7 @@ void BluetoothHealthProfileHandler::OnConnected(int result,
                   &response->get<picojson::object>());
     }
 
-    util::AsyncResponse(request->second, response);
+    object->instance_.AsyncResponse(request->second, response);
 
     // request was handled, remove
     object->connection_requests_.erase(request);
@@ -190,7 +187,7 @@ void BluetoothHealthProfileHandler::OnDisconnected(int result,
 
     data_obj->insert(std::make_pair(kEvent, picojson::value(kOnClose)));
     data_obj->insert(std::make_pair(kId, picojson::value(std::to_string(channel))));
-    util::FireEvent(kChangeCallback, value);
+    object->instance_.FireEvent(kChangeCallback, value);
   }
 }
 
@@ -222,7 +219,7 @@ void BluetoothHealthProfileHandler::OnDataReceived(unsigned int channel,
       array.push_back(picojson::value(static_cast<double>(data[i])));
     }
 
-    util::FireEvent(kChangeCallback, value);
+    object->instance_.FireEvent(kChangeCallback, value);
   }
 }
 
@@ -275,8 +272,8 @@ void BluetoothHealthProfileHandler::RegisterSinkApp(const picojson::value& data,
     ReportError(platform_result, &response->get<picojson::object>());
   };
 
-  auto register_app_response = [callback_handle](const std::shared_ptr<picojson::value>& response) -> void {
-    util::SyncResponse(callback_handle, response);
+  auto register_app_response = [this, callback_handle](const std::shared_ptr<picojson::value>& response) -> void {
+    instance_.SyncResponse(callback_handle, response);
   };
 
   TaskQueue::GetInstance().Queue<picojson::value>(
@@ -328,7 +325,7 @@ void BluetoothHealthProfileHandler::ConnectToSource(const picojson::value& data,
   }
 
   if (result.IsError()) {
-    util::AsyncResponse(callback_handle, result);
+    instance_.AsyncResponse(callback_handle, result);
   }
 
   ReportSuccess(out);
@@ -376,8 +373,8 @@ void BluetoothHealthProfileHandler::UnregisterSinkAppAsync(const std::string& ap
     }
   };
 
-  auto unregister_app_response = [callback_handle](const std::shared_ptr<picojson::value>& response) -> void {
-    util::SyncResponse(callback_handle, response);
+  auto unregister_app_response = [this, callback_handle](const std::shared_ptr<picojson::value>& response) -> void {
+    instance_.SyncResponse(callback_handle, response);
   };
 
   TaskQueue::GetInstance().Queue<picojson::value>(
index 0ba22ddf60cade514370c10a8f640c9200e0706c..436d8efdc330e651a266e0cb46e4bea044e40f8d 100644 (file)
@@ -26,6 +26,8 @@
 namespace extension {
 namespace bluetooth {
 
+class BluetoothInstance;
+
 class BluetoothHealthProfileHandler {
  public:
   /**
@@ -64,14 +66,12 @@ class BluetoothHealthProfileHandler {
    */
   void ConnectToSource(const picojson::value& data, picojson::object& out);
 
-  static BluetoothHealthProfileHandler& GetInstance();
-
+  explicit BluetoothHealthProfileHandler(BluetoothInstance& instance);
   ~BluetoothHealthProfileHandler();
 
   void UnregisterSinkAppAsync(const std::string& app_id, int callback_handle);
 
  private:
-  BluetoothHealthProfileHandler();
   BluetoothHealthProfileHandler(const BluetoothHealthProfileHandler&) = delete;
   BluetoothHealthProfileHandler& operator=(const BluetoothHealthProfileHandler&) = delete;
 
@@ -95,6 +95,8 @@ class BluetoothHealthProfileHandler {
   std::set<std::string> registered_health_apps_;
   std::map<std::string, double> connection_requests_;
   std::set<unsigned int> connected_channels_;
+
+  BluetoothInstance& instance_;
 };
 
 } // namespace bluetooth
index 0327f179e88bd1d6a32e845f0b33f27a2867c583..765934789242208361208ff0bf3d43ae2bc6dffc 100644 (file)
@@ -7,40 +7,20 @@
 
 #include "common/converter.h"
 #include "common/logger.h"
-
-#include "bluetooth_adapter.h"
-#include "bluetooth_device.h"
-#include "bluetooth_health_application.h"
-#include "bluetooth_health_channel.h"
-#include "bluetooth_health_profile_handler.h"
-#include "bluetooth_service_handler.h"
-#include "bluetooth_socket.h"
-#include "bluetooth_util.h"
+#include "common/task-queue.h"
 
 namespace extension {
 namespace bluetooth {
 
 using namespace common;
 
-namespace {
-
-BluetoothAdapter* bluetooth_adapter = &BluetoothAdapter::GetInstance();
-BluetoothDevice bluetooth_device;
-BluetoothHealthApplication bluetooth_health_application;
-BluetoothHealthChannel bluetooth_health_channel;
-BluetoothHealthProfileHandler* bluetooth_health_profile_handler = &BluetoothHealthProfileHandler::GetInstance();
-BluetoothServiceHandler bluetooth_service_handler;
-BluetoothSocket bluetooth_socket;
-
-} // namespace
-
-BluetoothInstance& BluetoothInstance::GetInstance()
-{
-  static BluetoothInstance instance;
-  return instance;
-}
-
-BluetoothInstance::BluetoothInstance()
+BluetoothInstance::BluetoothInstance() :
+    bluetooth_adapter_(*this),
+    bluetooth_device_(bluetooth_adapter_),
+    bluetooth_health_profile_handler_(*this),
+    bluetooth_health_application_(bluetooth_health_profile_handler_),
+    bluetooth_service_handler_(bluetooth_adapter_),
+    bluetooth_socket_(bluetooth_adapter_)
 {
   LoggerD("Entered");
   using std::placeholders::_1;
@@ -53,71 +33,71 @@ BluetoothInstance::BluetoothInstance()
 
   // BluetoothAdapter
   REGISTER_ASYNC("BluetoothAdapter_setName",
-      std::bind(&BluetoothAdapter::SetName, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::SetName, &bluetooth_adapter_, _1, _2));
   REGISTER_ASYNC("BluetoothAdapter_setPowered",
-      std::bind(&BluetoothAdapter::SetPowered, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::SetPowered, &bluetooth_adapter_, _1, _2));
   REGISTER_ASYNC("BluetoothAdapter_setVisible",
-      std::bind(&BluetoothAdapter::SetVisible, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::SetVisible, &bluetooth_adapter_, _1, _2));
   REGISTER_SYNC("BluetoothAdapter_discoverDevices",
-      std::bind(&BluetoothAdapter::DiscoverDevices, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::DiscoverDevices, &bluetooth_adapter_, _1, _2));
   REGISTER_ASYNC("BluetoothAdapter_stopDiscovery",
-      std::bind(&BluetoothAdapter::StopDiscovery, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::StopDiscovery, &bluetooth_adapter_, _1, _2));
   REGISTER_ASYNC("BluetoothAdapter_getKnownDevices",
-      std::bind(&BluetoothAdapter::GetKnownDevices, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::GetKnownDevices, &bluetooth_adapter_, _1, _2));
   REGISTER_ASYNC("BluetoothAdapter_getDevice",
-      std::bind(&BluetoothAdapter::GetDevice, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::GetDevice, &bluetooth_adapter_, _1, _2));
   REGISTER_ASYNC("BluetoothAdapter_createBonding",
-      std::bind(&BluetoothAdapter::CreateBonding, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::CreateBonding, &bluetooth_adapter_, _1, _2));
   REGISTER_ASYNC("BluetoothAdapter_destroyBonding",
-      std::bind(&BluetoothAdapter::DestroyBonding, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::DestroyBonding, &bluetooth_adapter_, _1, _2));
   REGISTER_ASYNC("BluetoothAdapter_registerRFCOMMServiceByUUID",
-      std::bind(&BluetoothAdapter::RegisterRFCOMMServiceByUUID, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::RegisterRFCOMMServiceByUUID, &bluetooth_adapter_, _1, _2));
   REGISTER_SYNC("BluetoothAdapter_getBluetoothProfileHandler",
-      std::bind(&BluetoothAdapter::GetBluetoothProfileHandler, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::GetBluetoothProfileHandler, &bluetooth_adapter_, _1, _2));
   REGISTER_SYNC("BluetoothAdapter_getName",
-      std::bind(&BluetoothAdapter::GetName, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::GetName, &bluetooth_adapter_, _1, _2));
   REGISTER_SYNC("BluetoothAdapter_getAddress",
-      std::bind(&BluetoothAdapter::GetAddress, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::GetAddress, &bluetooth_adapter_, _1, _2));
   REGISTER_SYNC("BluetoothAdapter_getPowered",
-      std::bind(&BluetoothAdapter::GetPowered, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::GetPowered, &bluetooth_adapter_, _1, _2));
   REGISTER_SYNC("BluetoothAdapter_getVisible",
-      std::bind(&BluetoothAdapter::GetVisible, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::GetVisible, &bluetooth_adapter_, _1, _2));
   REGISTER_SYNC("BluetoothAdapter_isServiceConnected",
-      std::bind(&BluetoothAdapter::IsServiceConnected, bluetooth_adapter, _1, _2));
+      std::bind(&BluetoothAdapter::IsServiceConnected, &bluetooth_adapter_, _1, _2));
 
   // BluetoothDevice
   REGISTER_ASYNC("BluetoothDevice_connectToServiceByUUID",
-      std::bind(&BluetoothDevice::ConnectToServiceByUUID, &bluetooth_device, _1, _2));
+      std::bind(&BluetoothDevice::ConnectToServiceByUUID, &bluetooth_device_, _1, _2));
   REGISTER_SYNC("BluetoothDevice_getBoolValue",
-      std::bind(&BluetoothDevice::GetBoolValue, &bluetooth_device, _1, _2));
+      std::bind(&BluetoothDevice::GetBoolValue, &bluetooth_device_, _1, _2));
 
   // BluetoothHealthApplication
   REGISTER_ASYNC("BluetoothHealthApplication_unregister",
-      std::bind(&BluetoothHealthApplication::Unregister, &bluetooth_health_application, _1, _2));
+      std::bind(&BluetoothHealthApplication::Unregister, &bluetooth_health_application_, _1, _2));
 
   // BluetoothHealthChannel
   REGISTER_SYNC("BluetoothHealthChannel_close",
-      std::bind(&BluetoothHealthChannel::Close, &bluetooth_health_channel, _1, _2));
+      std::bind(&BluetoothHealthChannel::Close, &bluetooth_health_channel_, _1, _2));
   REGISTER_SYNC("BluetoothHealthChannel_sendData",
-      std::bind(&BluetoothHealthChannel::SendData, &bluetooth_health_channel, _1, _2));
+      std::bind(&BluetoothHealthChannel::SendData, &bluetooth_health_channel_, _1, _2));
 
   // BluetoothHealthProfileHandler
   REGISTER_ASYNC("BluetoothHealthProfileHandler_registerSinkApp",
-      std::bind(&BluetoothHealthProfileHandler::RegisterSinkApp, bluetooth_health_profile_handler, _1, _2));
+      std::bind(&BluetoothHealthProfileHandler::RegisterSinkApp, &bluetooth_health_profile_handler_, _1, _2));
   REGISTER_ASYNC("BluetoothHealthProfileHandler_connectToSource",
-      std::bind(&BluetoothHealthProfileHandler::ConnectToSource, bluetooth_health_profile_handler, _1, _2));
+      std::bind(&BluetoothHealthProfileHandler::ConnectToSource, &bluetooth_health_profile_handler_, _1, _2));
 
   // BluetoothServiceHandler
   REGISTER_ASYNC("BluetoothServiceHandler_unregister",
-      std::bind(&BluetoothServiceHandler::Unregister, &bluetooth_service_handler, _1, _2));
+      std::bind(&BluetoothServiceHandler::Unregister, &bluetooth_service_handler_, _1, _2));
 
   // BluetoothSocket
   REGISTER_SYNC("BluetoothSocket_writeData",
-      std::bind(&BluetoothSocket::WriteData, &bluetooth_socket, _1, _2));
+      std::bind(&BluetoothSocket::WriteData, &bluetooth_socket_, _1, _2));
   REGISTER_SYNC("BluetoothSocket_readData",
-      std::bind(&BluetoothSocket::ReadData, &bluetooth_socket, _1, _2));
+      std::bind(&BluetoothSocket::ReadData, &bluetooth_socket_, _1, _2));
   REGISTER_SYNC("BluetoothSocket_close",
-      std::bind(&BluetoothSocket::Close, &bluetooth_socket, _1, _2));
+      std::bind(&BluetoothSocket::Close, &bluetooth_socket_, _1, _2));
 
   #undef REGISTER_ASYNC
   #undef REGISTER_SYNC
@@ -128,6 +108,59 @@ BluetoothInstance::~BluetoothInstance()
   LoggerD("Entered");
 }
 
+namespace {
+const char* JSON_CALLBACK_ID = "callbackId";
+const char* JSON_LISTENER_ID = "listenerId";
+const char* JSON_STATUS = "status";
+const char* JSON_RESULT = "result";
+const char* JSON_CALLBACK_SUCCCESS = "success";
+const char* JSON_CALLBACK_ERROR = "error";
+const char* JSON_DATA = "args";
+} // namespace
+
+void BluetoothInstance::AsyncResponse(double callback_handle, const std::shared_ptr<picojson::value>& response) {
+  common::TaskQueue::GetInstance().Async<picojson::value>([this, callback_handle](const std::shared_ptr<picojson::value>& response) {
+    SyncResponse(callback_handle, response);
+  }, response);
+}
+
+void BluetoothInstance::AsyncResponse(double callback_handle, const PlatformResult& result) {
+  std::shared_ptr<picojson::value> response =
+      std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
+
+  if (result.IsError()) {
+    tools::ReportError(result, &response->get<picojson::object>());
+  } else {
+    tools::ReportSuccess(response->get<picojson::object>());
+  }
+
+  TaskQueue::GetInstance().Async<picojson::value>([this, callback_handle](const std::shared_ptr<picojson::value>& response) {
+    SyncResponse(callback_handle, response);
+  }, response);
+}
+
+void BluetoothInstance::SyncResponse(double callback_handle, const std::shared_ptr<picojson::value>& response) {
+  auto& obj = response->get<picojson::object>();
+  obj[JSON_CALLBACK_ID] = picojson::value(callback_handle);
+  PostMessage(response->serialize().c_str());
+}
+
+void BluetoothInstance::FireEvent(const std::string& event, picojson::value& value) {
+  auto& obj = value.get<picojson::object>();
+  obj[JSON_LISTENER_ID] = picojson::value(event);
+  PostMessage(value.serialize().c_str());
+}
+
+void BluetoothInstance::FireEvent(const std::string& event, const picojson::value& value) {
+  picojson::value v{value};
+  FireEvent(event, v);
+}
+
+void BluetoothInstance::FireEvent(const std::string& event, const std::shared_ptr<picojson::value>& value) {
+  FireEvent(event, *value.get());
+}
+
+
 } // namespace bluetooth
 } // namespace extension
 
index dc245c66267932b4867b618282daef7bb9414294..afbac548eacef22ea3c62471d8b89c97c4e4ac7a 100644 (file)
@@ -7,15 +7,39 @@
 
 #include "common/extension.h"
 
+#include "bluetooth/bluetooth_adapter.h"
+#include "bluetooth/bluetooth_device.h"
+#include "bluetooth/bluetooth_health_application.h"
+#include "bluetooth/bluetooth_health_channel.h"
+#include "bluetooth/bluetooth_health_profile_handler.h"
+#include "bluetooth/bluetooth_service_handler.h"
+#include "bluetooth/bluetooth_socket.h"
+#include "bluetooth/bluetooth_util.h"
+
 namespace extension {
 namespace bluetooth {
 
 class BluetoothInstance: public common::ParsedInstance {
  public:
-  static BluetoothInstance& GetInstance();
- private:
   BluetoothInstance();
   virtual ~BluetoothInstance();
+
+  void AsyncResponse(double callback_handle, const std::shared_ptr<picojson::value>& response);
+  void AsyncResponse(double callback_handle, const common::PlatformResult& result);
+  void SyncResponse(double callback_handle, const std::shared_ptr<picojson::value>& response);
+
+  void FireEvent(const std::string& event, picojson::value& value);
+  void FireEvent(const std::string& event, const picojson::value& value);
+  void FireEvent(const std::string& event, const std::shared_ptr<picojson::value>& value);
+
+ private:
+  BluetoothAdapter bluetooth_adapter_;
+  BluetoothDevice bluetooth_device_;
+  BluetoothHealthApplication bluetooth_health_application_;
+  BluetoothHealthChannel bluetooth_health_channel_;
+  BluetoothHealthProfileHandler bluetooth_health_profile_handler_;
+  BluetoothServiceHandler bluetooth_service_handler_;
+  BluetoothSocket bluetooth_socket_;
 };
 
 } // namespace bluetooth
index 369a40f27df2e989ae745b939a88f2737e10676f..0f04c6a36e371a4337cc39e36941e343634f39fa 100644 (file)
@@ -29,6 +29,10 @@ namespace bluetooth {
 
 using namespace common;
 
+BluetoothServiceHandler::BluetoothServiceHandler(BluetoothAdapter& adapter)
+    : adapter_(adapter) {
+}
+
 void BluetoothServiceHandler::Unregister(const picojson::value& data, picojson::object& out) {
   LoggerD("Entered");
 
@@ -36,9 +40,8 @@ void BluetoothServiceHandler::Unregister(const picojson::value& data, picojson::
 
   const auto& args = util::GetArguments(data);
 
-  BluetoothAdapter::GetInstance().UnregisterUUID(
-      FromJson<std::string>(args, "uuid"),
-      util::GetAsyncCallbackHandle(data));
+  adapter_.UnregisterUUID(FromJson<std::string>(args, "uuid"),
+                          util::GetAsyncCallbackHandle(data));
 
   tools::ReportSuccess(out);
 }
index f2df01eba41f3983430b47935e4841e9e1fc5da2..4007714605490c92947f3f753f73c640ffd4ae51 100644 (file)
 namespace extension {
 namespace bluetooth {
 
+class BluetoothAdapter;
+
 class BluetoothServiceHandler {
  public:
+  explicit BluetoothServiceHandler(BluetoothAdapter& adapter);
+
   /**
    * Signature: @code void unregister(successCallback, errorCallback); @endcode
    * JSON: @code data: {method: 'BluetoothServiceHandler_unregister', args: {}} @endcode
@@ -40,6 +44,9 @@ class BluetoothServiceHandler {
    * @endcode
    */
   void Unregister(const picojson::value& data, picojson::object& out);
+
+ private:
+  BluetoothAdapter& adapter_;
 };
 
 } // namespace bluetooth
index 9b3af6eefcce5f5cfcf7183d9f71c18b9faee26e..3771f1e4dfeeafc7b94311c30166b515e5157764 100644 (file)
@@ -43,6 +43,10 @@ const int kBluetoothError = -1;
 using namespace common;
 using namespace common::tools;
 
+BluetoothSocket::BluetoothSocket(BluetoothAdapter& adapter)
+    : adapter_(adapter) {
+}
+
 void BluetoothSocket::WriteData(const picojson::value& data, picojson::object& out) {
   LoggerD("Enter");
 
@@ -78,7 +82,7 @@ void BluetoothSocket::ReadData(const picojson::value& data, picojson::object& ou
 
   int socket = common::stol(FromJson<std::string>(args, "id"));
 
-  auto binary_data = BluetoothAdapter::GetInstance().ReadSocketData(socket);
+  auto binary_data = adapter_.ReadSocketData(socket);
   picojson::value ret = picojson::value(picojson::array());
   picojson::array& array = ret.get<picojson::array>();
 
@@ -86,7 +90,7 @@ void BluetoothSocket::ReadData(const picojson::value& data, picojson::object& ou
     array.push_back(picojson::value(static_cast<double>(val)));
   }
 
-  BluetoothAdapter::GetInstance().ClearSocketData(socket);
+  adapter_.ClearSocketData(socket);
 
   ReportSuccess(ret, out);
 }
index 8192888eaa44596d2483a7ac3b97559d96b3cb0b..b123b34a007e5741b31c70ff189d91b5763165f3 100644 (file)
 namespace extension {
 namespace bluetooth {
 
+class BluetoothAdapter;
+
 class BluetoothSocket {
  public:
+  explicit BluetoothSocket(BluetoothAdapter& adapter);
+
   /**
    * Signature: @code unsigned long writeData(data[]); @endcode
    * JSON: @code data: {method: 'BluetoothSocket_writeData', args: {data: data}} @endcode
@@ -63,6 +67,9 @@ class BluetoothSocket {
   void Close(const picojson::value& data, picojson::object& out);
 
   static picojson::value ToJson(bt_socket_connection_s* connection);
+
+ private:
+  BluetoothAdapter& adapter_;
 };
 
 } // namespace bluetooth
index 70d5146fbbd367407889902e087b27ac0ff8ea82..78a1618bbad1dfd76532977d1d636f3126374ee9 100644 (file)
@@ -4,12 +4,6 @@
 
 #include "bluetooth_util.h"
 
-#include "common/logger.h"
-#include "common/task-queue.h"
-#include "common/extension.h"
-
-#include "bluetooth_instance.h"
-
 using namespace common;
 
 namespace extension {
@@ -18,56 +12,8 @@ namespace util {
 
 namespace {
 const char* JSON_CALLBACK_ID = "callbackId";
-const char* JSON_LISTENER_ID = "listenerId";
-const char* JSON_STATUS = "status";
-const char* JSON_RESULT = "result";
-const char* JSON_CALLBACK_SUCCCESS = "success";
-const char* JSON_CALLBACK_ERROR = "error";
-const char* JSON_DATA = "args";
 } // namespace
 
-void AsyncResponse(double callback_handle, const std::shared_ptr<picojson::value>& response) {
-  common::TaskQueue::GetInstance().Async<picojson::value>([callback_handle](const std::shared_ptr<picojson::value>& response) {
-    SyncResponse(callback_handle, response);
-  }, response);
-}
-
-void AsyncResponse(double callback_handle, const PlatformResult& result) {
-  std::shared_ptr<picojson::value> response =
-      std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
-
-  if (result.IsError()) {
-    tools::ReportError(result, &response->get<picojson::object>());
-  } else {
-    tools::ReportSuccess(response->get<picojson::object>());
-  }
-
-  TaskQueue::GetInstance().Async<picojson::value>([callback_handle](const std::shared_ptr<picojson::value>& response) {
-    SyncResponse(callback_handle, response);
-  }, response);
-}
-
-void SyncResponse(double callback_handle, const std::shared_ptr<picojson::value>& response) {
-  auto& obj = response->get<picojson::object>();
-  obj[JSON_CALLBACK_ID] = picojson::value(callback_handle);
-  BluetoothInstance::GetInstance().PostMessage(response->serialize().c_str());
-}
-
-void FireEvent(const std::string& event, picojson::value& value) {
-  auto& obj = value.get<picojson::object>();
-  obj[JSON_LISTENER_ID] = picojson::value(event);
-  BluetoothInstance::GetInstance().PostMessage(value.serialize().c_str());
-}
-
-void FireEvent(const std::string& event, const picojson::value& value) {
-  picojson::value v{value};
-  FireEvent(event, v);
-}
-
-void FireEvent(const std::string& event, const std::shared_ptr<picojson::value>& value) {
-  FireEvent(event, *value.get());
-}
-
 double GetAsyncCallbackHandle(const picojson::value& data) {
   return data.get(JSON_CALLBACK_ID).get<double>();
 }
index 271be1f383e3af55375d672c759df288333df2f2..198be84219b0bf82b0e32e46d8b94e59e6351907 100644 (file)
@@ -15,14 +15,6 @@ namespace extension {
 namespace bluetooth {
 namespace util {
 
-void AsyncResponse(double callback_handle, const std::shared_ptr<picojson::value>& response);
-void AsyncResponse(double callback_handle, const common::PlatformResult& result);
-void SyncResponse(double callback_handle, const std::shared_ptr<picojson::value>& response);
-
-void FireEvent(const std::string& event, picojson::value& value);
-void FireEvent(const std::string& event, const picojson::value& value);
-void FireEvent(const std::string& event, const std::shared_ptr<picojson::value>& value);
-
 double GetAsyncCallbackHandle(const picojson::value& data);
 
 const picojson::object& GetArguments(const picojson::value& data);