From 291d9d1e12b30086f0f4b7418dfb9a39721ab6b8 Mon Sep 17 00:00:00 2001 From: Dawid Juszczak Date: Wed, 8 Apr 2020 10:53:15 +0200 Subject: [PATCH] [Bluetooth] Fix onnamechanged callback https://code.sec.samsung.net/jira/browse/XWALK-2065 [Description] + removed unnecessary calls of onnamechanged of BluetoothAdapterChangeCallback + refactor setName function [Verification] + tested manually on chrome console + tct-bluetooth-tizen-tests (auto) 100% PASS Change-Id: Iaca69d08d18cc95d1f624678873a3d6f9ae2c575 Signed-off-by: Dawid Juszczak --- src/bluetooth/bluetooth_adapter.cc | 83 ++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/src/bluetooth/bluetooth_adapter.cc b/src/bluetooth/bluetooth_adapter.cc index 8a4bed4..128defb 100644 --- a/src/bluetooth/bluetooth_adapter.cc +++ b/src/bluetooth/bluetooth_adapter.cc @@ -162,13 +162,15 @@ void BluetoothAdapter::NameChangedCB(char* name, void* user_data) { return; } - picojson::value value = picojson::value(picojson::object()); - picojson::object* data_obj = &value.get(); + if (adapter->is_callback_set_) { + picojson::value value = picojson::value(picojson::object()); + picojson::object* data_obj = &value.get(); - data_obj->insert(std::make_pair(kAction, picojson::value(kOnNameChanged))); - data_obj->insert(std::make_pair(kName, picojson::value(name))); + data_obj->insert(std::make_pair(kAction, picojson::value(kOnNameChanged))); + data_obj->insert(std::make_pair(kName, picojson::value(name))); - adapter->instance_.FireEvent(kAdapterChangeCallbackEvent, value); + adapter->instance_.FireEvent(kAdapterChangeCallbackEvent, value); + } if (adapter->user_request_list_[SET_NAME] && name == adapter->requested_name_) { std::shared_ptr response = @@ -466,48 +468,51 @@ void BluetoothAdapter::SetName(const picojson::value& data, picojson::object& ou return; } - if (this->get_powered()) { - if (get_name() == name) { - instance_.AsyncResponse(callback_handle, result); - return; - } + if (!this->get_powered()) { + result = + LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); + instance_.AsyncResponse(callback_handle, result); + return; + } - if (this->user_request_list_[SET_NAME]) { - result = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested"); - instance_.AsyncResponse(callback_handle, result); - return; - } + if (get_name() == name) { + LoggerD("Requested the same name, calling success callback"); + instance_.AsyncResponse(callback_handle, result); + return; + } + + if (this->user_request_list_[SET_NAME]) { + result = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested"); + instance_.AsyncResponse(callback_handle, result); + return; + } - this->user_request_list_[SET_NAME] = true; - this->user_request_callback_[SET_NAME] = callback_handle; + this->user_request_list_[SET_NAME] = true; + this->user_request_callback_[SET_NAME] = callback_handle; - int ret = bt_adapter_set_name(name.c_str()); - switch (ret) { - case BT_ERROR_NONE: - // bt_adapter_name_changed_cb() will be invoked - // if this function returns #BT_ERROR_NONE - this->requested_name_ = name; - break; - case BT_ERROR_INVALID_PARAMETER: - result = - LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Invalid value", - ("bt_adapter_set_name error: %d (%s)", ret, get_error_message(ret))); - break; - default: - result = - LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Unknown exception", - ("bt_adapter_set_name error: %d (%s)", ret, get_error_message(ret))); - } - } else { - result = - LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); + int ret = bt_adapter_set_name(name.c_str()); + switch (ret) { + case BT_ERROR_NONE: + // bt_adapter_name_changed_cb() will be invoked + // if this function returns #BT_ERROR_NONE + this->requested_name_ = name; + break; + case BT_ERROR_INVALID_PARAMETER: + result = + LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Invalid value", + ("bt_adapter_set_name error: %d (%s)", ret, get_error_message(ret))); + break; + default: + result = + LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("bt_adapter_set_name error: %d (%s)", ret, get_error_message(ret))); } if (result.IsError()) { this->user_request_list_[SET_NAME] = false; + instance_.AsyncResponse(callback_handle, result); + return; } - - instance_.AsyncResponse(callback_handle, result); } void BluetoothAdapter::SetPowered(const picojson::value& data, picojson::object& out) { -- 2.7.4