#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 {
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]) {
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;
}
}
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;
}
}
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) {
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;
}
}
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;
}
}
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;
}
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;
}
BluetoothDevice::ToJson(discovery_info, &data.get<picojson::object>());
adapter->discovered_devices_.push_back(data);
- util::FireEvent(kAdapterDiscoverSuccessEvent, value);
+ adapter->instance_.FireEvent(kAdapterDiscoverSuccessEvent, value);
}
}
break;
}
}
-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()) {
}
}
-BluetoothAdapter& BluetoothAdapter::GetInstance() {
- static BluetoothAdapter instance;
- return instance;
-}
-
std::string BluetoothAdapter::get_name() const {
char* name = nullptr;
std::string str_name = "";
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;
}
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) {
return;
}
- util::AsyncResponse(callback_handle, ret);
+ instance_.AsyncResponse(callback_handle, ret);
}
void BluetoothAdapter::SetVisible(const picojson::value& data, picojson::object& out) {
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;
}
}
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) {
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);
}
}
bt_adapter_is_discovering(&is_discovering);
if (!is_discovering) {
- util::AsyncResponse(callback_handle, result);
+ instance_.AsyncResponse(callback_handle, result);
return;
}
}
if (result.IsError()) {
- util::AsyncResponse(callback_handle, result);
+ instance_.AsyncResponse(callback_handle, result);
}
}
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>(
}
};
- 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>(
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;
ReportSuccess(response->get<picojson::object>());
}
- util::AsyncResponse(callback_handle_, response);
+ instance_.AsyncResponse(callback_handle_, response);
}
private:
+ BluetoothInstance& instance_;
double callback_handle_;
std::string address_;
};
}
};
- 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());
}
if (result.IsError()) {
- util::AsyncResponse(callback_handle, result);
+ instance_.AsyncResponse(callback_handle, result);
}
};
TaskQueue::GetInstance().Queue(create_bonding);
}
};
- 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());
}
if (result.IsError()) {
- util::AsyncResponse(callback_handle, result);
+ instance_.AsyncResponse(callback_handle, result);
}
};
TaskQueue::GetInstance().Queue(destroy_bonding);
}
};
- 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>(
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,
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);
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);
// Store received data
object->StoreSocketData(data);
- InvokeSocketOnMessageEvent(*it);
+ object->InvokeSocketOnMessageEvent(*it);
}
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) {
connected_sockets_.erase(it);
- BluetoothAdapter::InvokeSocketOnCloseEvent(*it);
+ InvokeSocketOnCloseEvent(*it);
}
void BluetoothAdapter::StoreSocketData(bt_socket_received_data_s* data) {
STOP_DISCOVERY
};
+class BluetoothInstance;
+
class BluetoothAdapter {
public:
/**
*/
void IsServiceConnected(const picojson::value& data, picojson::object& out);
- static BluetoothAdapter& GetInstance();
+ explicit BluetoothAdapter(BluetoothInstance& instance);
virtual ~BluetoothAdapter();
std::string get_name() const;
void UnregisterUUID(const std::string& uuid, int callback_handle);
private:
- BluetoothAdapter();
BluetoothAdapter(const BluetoothAdapter&) = delete;
BluetoothAdapter& operator=(const BluetoothAdapter&) = delete;
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_;
RegisteredUuidMap registered_uuids_;
std::unordered_map<int, std::list<char>> socket_data_;
+
+ BluetoothInstance& instance_;
};
} // namespace bluetooth
}
}
+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))));
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);
}
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
picojson::object* device);
static void ToJson(bt_adapter_device_discovery_info_s *info,
picojson::object* device);
+
+ private:
+ BluetoothAdapter& adapter_;
};
} // namespace bluetooth
BluetoothExtension::~BluetoothExtension() {}
common::Instance* BluetoothExtension::CreateInstance() {
- return &extension::bluetooth::BluetoothInstance::GetInstance();
+ return new extension::bluetooth::BluetoothInstance();
}
using namespace common;
+BluetoothHealthApplication::BluetoothHealthApplication(
+ BluetoothHealthProfileHandler& handler)
+ : handler_(handler) {
+}
+
void BluetoothHealthApplication::Unregister(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
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);
}
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
const std::string& name,
const char* id,
picojson::object* out);
+
+ private:
+ BluetoothHealthProfileHandler& handler_;
};
} // namespace bluetooth
#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 {
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");
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");
}
&response->get<picojson::object>());
}
- util::AsyncResponse(request->second, response);
+ object->instance_.AsyncResponse(request->second, response);
// request was handled, remove
object->connection_requests_.erase(request);
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);
}
}
array.push_back(picojson::value(static_cast<double>(data[i])));
}
- util::FireEvent(kChangeCallback, value);
+ object->instance_.FireEvent(kChangeCallback, value);
}
}
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>(
}
if (result.IsError()) {
- util::AsyncResponse(callback_handle, result);
+ instance_.AsyncResponse(callback_handle, result);
}
ReportSuccess(out);
}
};
- 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>(
namespace extension {
namespace bluetooth {
+class BluetoothInstance;
+
class BluetoothHealthProfileHandler {
public:
/**
*/
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;
std::set<std::string> registered_health_apps_;
std::map<std::string, double> connection_requests_;
std::set<unsigned int> connected_channels_;
+
+ BluetoothInstance& instance_;
};
} // namespace bluetooth
#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;
// 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
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
#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
using namespace common;
+BluetoothServiceHandler::BluetoothServiceHandler(BluetoothAdapter& adapter)
+ : adapter_(adapter) {
+}
+
void BluetoothServiceHandler::Unregister(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
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);
}
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
* @endcode
*/
void Unregister(const picojson::value& data, picojson::object& out);
+
+ private:
+ BluetoothAdapter& adapter_;
};
} // namespace bluetooth
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");
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>();
array.push_back(picojson::value(static_cast<double>(val)));
}
- BluetoothAdapter::GetInstance().ClearSocketData(socket);
+ adapter_.ClearSocketData(socket);
ReportSuccess(ret, out);
}
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
void Close(const picojson::value& data, picojson::object& out);
static picojson::value ToJson(bt_socket_connection_s* connection);
+
+ private:
+ BluetoothAdapter& adapter_;
};
} // namespace bluetooth
#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 {
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>();
}
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);