From: Pawel Andruszkiewicz
Date: Wed, 8 Apr 2015 11:06:26 +0000 (+0200)
Subject: [Bluetooth] Allow to create multiple instances of BluetoothInstance class.
X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~176
X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ea0d7470df3cec17386f4ab2ddb5313a8a708442;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git
[Bluetooth] Allow to create multiple instances of BluetoothInstance class.
Change-Id: I0424009bd9cdb0d4a199b1b9601bb1518d1c7535
---
diff --git a/src/bluetooth/bluetooth_adapter.cc b/src/bluetooth/bluetooth_adapter.cc
index 52a55139..e8ff334a 100644
--- a/src/bluetooth/bluetooth_adapter.cc
+++ b/src/bluetooth/bluetooth_adapter.cc
@@ -29,11 +29,12 @@
#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 response =
std::shared_ptr(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(new picojson::value(picojson::object()));
ReportSuccess(response->get());
- 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());
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(¤t , &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(new picojson::value(picojson::object()));
ReportError(result, &response->get());
- TaskQueue::GetInstance().Async([](const std::shared_ptr& result) {
- util::FireEvent(kAdapterDiscoverErrorEvent, result);
+ TaskQueue::GetInstance().Async([this](const std::shared_ptr& 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());
}
};
- auto get_known_devices_response = [callback_handle](
+ auto get_known_devices_response = [this, callback_handle](
const std::shared_ptr& response) -> void {
- util::SyncResponse(callback_handle, response);
+ instance_.SyncResponse(callback_handle, response);
};
TaskQueue::GetInstance().Queue(
@@ -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& response) -> void {
- util::SyncResponse(callback_handle, response);
+ instance_.SyncResponse(callback_handle, response);
};
TaskQueue::GetInstance().Queue(
@@ -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());
}
- 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& response) -> void {
- util::SyncResponse(callback_handle, response);
+ auto rfcomm_response = [this, callback_handle](const std::shared_ptr& response) -> void {
+ instance_.SyncResponse(callback_handle, response);
};
TaskQueue::GetInstance().Queue(
@@ -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());
}
- 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();
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) {
diff --git a/src/bluetooth/bluetooth_adapter.h b/src/bluetooth/bluetooth_adapter.h
index b59acb29..a7857809 100644
--- a/src/bluetooth/bluetooth_adapter.h
+++ b/src/bluetooth/bluetooth_adapter.h
@@ -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> socket_data_;
+
+ BluetoothInstance& instance_;
};
} // namespace bluetooth
diff --git a/src/bluetooth/bluetooth_device.cc b/src/bluetooth/bluetooth_device.cc
index 4f392a23..f9166fff 100644
--- a/src/bluetooth/bluetooth_device.cc
+++ b/src/bluetooth/bluetooth_device.cc
@@ -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(args, "address"),
- FromJson(args, "uuid"),
- util::GetAsyncCallbackHandle(data));
+ adapter_.ConnectToServiceByUUID(FromJson(args, "address"),
+ FromJson(args, "uuid"),
+ util::GetAsyncCallbackHandle(data));
ReportSuccess(out);
}
diff --git a/src/bluetooth/bluetooth_device.h b/src/bluetooth/bluetooth_device.h
index ad697bd6..3ac06afa 100644
--- a/src/bluetooth/bluetooth_device.h
+++ b/src/bluetooth/bluetooth_device.h
@@ -24,8 +24,12 @@
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
diff --git a/src/bluetooth/bluetooth_extension.cc b/src/bluetooth/bluetooth_extension.cc
index 9bd9086e..4663f331 100644
--- a/src/bluetooth/bluetooth_extension.cc
+++ b/src/bluetooth/bluetooth_extension.cc
@@ -27,5 +27,5 @@ BluetoothExtension::BluetoothExtension() {
BluetoothExtension::~BluetoothExtension() {}
common::Instance* BluetoothExtension::CreateInstance() {
- return &extension::bluetooth::BluetoothInstance::GetInstance();
+ return new extension::bluetooth::BluetoothInstance();
}
diff --git a/src/bluetooth/bluetooth_health_application.cc b/src/bluetooth/bluetooth_health_application.cc
index fc8cc4c0..aec0358f 100644
--- a/src/bluetooth/bluetooth_health_application.cc
+++ b/src/bluetooth/bluetooth_health_application.cc
@@ -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(args, "id"),
- util::GetAsyncCallbackHandle(data));
+ handler_.UnregisterSinkAppAsync(FromJson(args, "id"),
+ util::GetAsyncCallbackHandle(data));
tools::ReportSuccess(out);
}
diff --git a/src/bluetooth/bluetooth_health_application.h b/src/bluetooth/bluetooth_health_application.h
index 831dcb91..5ee9a902 100644
--- a/src/bluetooth/bluetooth_health_application.h
+++ b/src/bluetooth/bluetooth_health_application.h
@@ -23,8 +23,12 @@
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
diff --git a/src/bluetooth/bluetooth_health_profile_handler.cc b/src/bluetooth/bluetooth_health_profile_handler.cc
index f0b4d0a0..e46d9c00 100644
--- a/src/bluetooth/bluetooth_health_profile_handler.cc
+++ b/src/bluetooth/bluetooth_health_profile_handler.cc
@@ -21,11 +21,12 @@
#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());
}
- 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(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());
};
- auto register_app_response = [callback_handle](const std::shared_ptr& response) -> void {
- util::SyncResponse(callback_handle, response);
+ auto register_app_response = [this, callback_handle](const std::shared_ptr& response) -> void {
+ instance_.SyncResponse(callback_handle, response);
};
TaskQueue::GetInstance().Queue(
@@ -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& response) -> void {
- util::SyncResponse(callback_handle, response);
+ auto unregister_app_response = [this, callback_handle](const std::shared_ptr& response) -> void {
+ instance_.SyncResponse(callback_handle, response);
};
TaskQueue::GetInstance().Queue(
diff --git a/src/bluetooth/bluetooth_health_profile_handler.h b/src/bluetooth/bluetooth_health_profile_handler.h
index 0ba22ddf..436d8efd 100644
--- a/src/bluetooth/bluetooth_health_profile_handler.h
+++ b/src/bluetooth/bluetooth_health_profile_handler.h
@@ -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 registered_health_apps_;
std::map connection_requests_;
std::set connected_channels_;
+
+ BluetoothInstance& instance_;
};
} // namespace bluetooth
diff --git a/src/bluetooth/bluetooth_instance.cc b/src/bluetooth/bluetooth_instance.cc
index 0327f179..76593478 100644
--- a/src/bluetooth/bluetooth_instance.cc
+++ b/src/bluetooth/bluetooth_instance.cc
@@ -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& response) {
+ common::TaskQueue::GetInstance().Async([this, callback_handle](const std::shared_ptr& response) {
+ SyncResponse(callback_handle, response);
+ }, response);
+}
+
+void BluetoothInstance::AsyncResponse(double callback_handle, const PlatformResult& result) {
+ std::shared_ptr response =
+ std::shared_ptr(new picojson::value(picojson::object()));
+
+ if (result.IsError()) {
+ tools::ReportError(result, &response->get());
+ } else {
+ tools::ReportSuccess(response->get());
+ }
+
+ TaskQueue::GetInstance().Async([this, callback_handle](const std::shared_ptr& response) {
+ SyncResponse(callback_handle, response);
+ }, response);
+}
+
+void BluetoothInstance::SyncResponse(double callback_handle, const std::shared_ptr& response) {
+ auto& obj = response->get();
+ 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();
+ 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& value) {
+ FireEvent(event, *value.get());
+}
+
+
} // namespace bluetooth
} // namespace extension
diff --git a/src/bluetooth/bluetooth_instance.h b/src/bluetooth/bluetooth_instance.h
index dc245c66..afbac548 100644
--- a/src/bluetooth/bluetooth_instance.h
+++ b/src/bluetooth/bluetooth_instance.h
@@ -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& response);
+ void AsyncResponse(double callback_handle, const common::PlatformResult& result);
+ void SyncResponse(double callback_handle, const std::shared_ptr& 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& 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
diff --git a/src/bluetooth/bluetooth_service_handler.cc b/src/bluetooth/bluetooth_service_handler.cc
index 369a40f2..0f04c6a3 100644
--- a/src/bluetooth/bluetooth_service_handler.cc
+++ b/src/bluetooth/bluetooth_service_handler.cc
@@ -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(args, "uuid"),
- util::GetAsyncCallbackHandle(data));
+ adapter_.UnregisterUUID(FromJson(args, "uuid"),
+ util::GetAsyncCallbackHandle(data));
tools::ReportSuccess(out);
}
diff --git a/src/bluetooth/bluetooth_service_handler.h b/src/bluetooth/bluetooth_service_handler.h
index f2df01eb..40077146 100644
--- a/src/bluetooth/bluetooth_service_handler.h
+++ b/src/bluetooth/bluetooth_service_handler.h
@@ -22,8 +22,12 @@
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
diff --git a/src/bluetooth/bluetooth_socket.cc b/src/bluetooth/bluetooth_socket.cc
index 9b3af6ee..3771f1e4 100644
--- a/src/bluetooth/bluetooth_socket.cc
+++ b/src/bluetooth/bluetooth_socket.cc
@@ -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(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();
@@ -86,7 +90,7 @@ void BluetoothSocket::ReadData(const picojson::value& data, picojson::object& ou
array.push_back(picojson::value(static_cast(val)));
}
- BluetoothAdapter::GetInstance().ClearSocketData(socket);
+ adapter_.ClearSocketData(socket);
ReportSuccess(ret, out);
}
diff --git a/src/bluetooth/bluetooth_socket.h b/src/bluetooth/bluetooth_socket.h
index 8192888e..b123b34a 100644
--- a/src/bluetooth/bluetooth_socket.h
+++ b/src/bluetooth/bluetooth_socket.h
@@ -24,8 +24,12 @@
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
diff --git a/src/bluetooth/bluetooth_util.cc b/src/bluetooth/bluetooth_util.cc
index 70d5146f..78a1618b 100644
--- a/src/bluetooth/bluetooth_util.cc
+++ b/src/bluetooth/bluetooth_util.cc
@@ -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& response) {
- common::TaskQueue::GetInstance().Async([callback_handle](const std::shared_ptr& response) {
- SyncResponse(callback_handle, response);
- }, response);
-}
-
-void AsyncResponse(double callback_handle, const PlatformResult& result) {
- std::shared_ptr response =
- std::shared_ptr(new picojson::value(picojson::object()));
-
- if (result.IsError()) {
- tools::ReportError(result, &response->get());
- } else {
- tools::ReportSuccess(response->get());
- }
-
- TaskQueue::GetInstance().Async([callback_handle](const std::shared_ptr& response) {
- SyncResponse(callback_handle, response);
- }, response);
-}
-
-void SyncResponse(double callback_handle, const std::shared_ptr& response) {
- auto& obj = response->get();
- 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();
- 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& value) {
- FireEvent(event, *value.get());
-}
-
double GetAsyncCallbackHandle(const picojson::value& data) {
return data.get(JSON_CALLBACK_ID).get();
}
diff --git a/src/bluetooth/bluetooth_util.h b/src/bluetooth/bluetooth_util.h
index 271be1f3..198be842 100644
--- a/src/bluetooth/bluetooth_util.h
+++ b/src/bluetooth/bluetooth_util.h
@@ -15,14 +15,6 @@ namespace extension {
namespace bluetooth {
namespace util {
-void AsyncResponse(double callback_handle, const std::shared_ptr& response);
-void AsyncResponse(double callback_handle, const common::PlatformResult& result);
-void SyncResponse(double callback_handle, const std::shared_ptr& 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& value);
-
double GetAsyncCallbackHandle(const picojson::value& data);
const picojson::object& GetArguments(const picojson::value& data);