From: Andrzej Popowski Date: Tue, 24 Nov 2015 15:20:17 +0000 (+0100) Subject: [Bluetooth] - refactoring logs X-Git-Tag: submit/tizen_mobile/20151215.080542^2~49^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=765da74bd4a4c60f98a372ecc48ed7dcb0c1ad60;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Bluetooth] - refactoring logs Change-Id: I6d196dd5c4c98fbb9b77a5c7e8d5ab2945074286 Signed-off-by: Andrzej Popowski --- diff --git a/src/bluetooth/bluetooth_adapter.cc b/src/bluetooth/bluetooth_adapter.cc index 6ff5fe8f..29bdaf60 100755 --- a/src/bluetooth/bluetooth_adapter.cc +++ b/src/bluetooth/bluetooth_adapter.cc @@ -148,10 +148,14 @@ void BluetoothAdapter::StateChangedCB(int result, bt_adapter_state_e state, void //do nothing break; case BT_ERROR_NOW_IN_PROGRESS: - ret = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is busy"); + ret = LogAndCreateResult( + ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is busy", + ("StateChangedCB result: %d (%s)", result, get_error_message(result))); break; default: - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("StateChangedCB result: %d (%s)", result, get_error_message(result))); } adapter->instance_.AsyncResponse(adapter->user_request_callback_[SET_POWERED], ret); @@ -211,7 +215,9 @@ void BluetoothAdapter::VisibilityChangedCB(int result, bt_adapter_visibility_mod PlatformResult ret = PlatformResult(ErrorCode::NO_ERROR); if (BT_ERROR_NONE != result) { - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("VisibilityChangedCB error: %d (%s)", result, get_error_message(result))); } adapter->instance_.AsyncResponse(adapter->user_request_callback_[SET_VISIBLE], ret); @@ -276,7 +282,8 @@ void BluetoothAdapter::DiscoveryStateChangedCB( data_obj->insert(std::make_pair(kAction, picojson::value(kOnDiscoverStarted))); adapter->instance_.FireEvent(kAdapterDiscoverSuccessEvent, value); } else { - ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"), data_obj); + LogAndReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"), data_obj, + ("Wrong discovery state: %d", discovery_state)); adapter->instance_.FireEvent(kAdapterDiscoverErrorEvent, value); adapter->user_request_list_[DISCOVER_DEVICES] = false; } @@ -455,7 +462,7 @@ 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."); + result = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); instance_.AsyncResponse(callback_handle, result); return; } @@ -467,7 +474,7 @@ void BluetoothAdapter::SetName(const picojson::value& data, picojson::object& ou } if (this->user_request_list_[SET_NAME]) { - result = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested"); + result = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested"); instance_.AsyncResponse(callback_handle, result); return; } @@ -483,13 +490,17 @@ void BluetoothAdapter::SetName(const picojson::value& data, picojson::object& ou this->requested_name_ = name; break; case BT_ERROR_INVALID_PARAMETER: - result = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Invalid value"); + result = LogAndCreateResult( + ErrorCode::INVALID_VALUES_ERR, "Invalid value", + ("bt_adapter_set_name error: %d (%s)", ret, get_error_message(ret))); break; default: - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + result = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("bt_adapter_set_name error: %d (%s)", ret, get_error_message(ret))); } } else { - result = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); + result = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); } if (result.IsError()) { @@ -509,11 +520,11 @@ void BluetoothAdapter::SetPowered(const picojson::value& data, picojson::object& PlatformResult ret = PlatformResult(ErrorCode::NO_ERROR); if (!this->is_initialized()) { - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); + ret = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); } if (ret.IsSuccess() && this->user_request_list_[SET_POWERED]) { - ret = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested"); + ret = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested"); } bool cur_powered = this->get_powered(); @@ -524,23 +535,26 @@ void BluetoothAdapter::SetPowered(const picojson::value& data, picojson::object& int err = 0; if ((err = app_control_create(&service)) != APP_CONTROL_ERROR_NONE) { - LoggerE("app control create failed: %d", err); - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "app control create failed"); + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "app control create failed", + ("app control create failed: %d", err)); } if (ret.IsSuccess()) { err = app_control_set_operation(service, "http://tizen.org/appcontrol/operation/edit"); if (err != APP_CONTROL_ERROR_NONE) { - LoggerE("app control set operation failed: %d", err); - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "app control set operation failed"); + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "app control set operation failed", + ("app control set operation failed: %d", err)); } } if (ret.IsSuccess()) { err = app_control_set_mime(service, "application/x-bluetooth-on-off"); if (err != APP_CONTROL_ERROR_NONE) { - LoggerE("app control set mime failed: %d", err); - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "app control set mime failed"); + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "app control set mime failed", + ("app control set mime failed: %d", err)); } } @@ -567,15 +581,18 @@ void BluetoothAdapter::SetPowered(const picojson::value& data, picojson::object& free(result); } else { LoggerE("app control setPowered failed"); - data->adapter->instance_.AsyncResponse(data->callback_handle, PlatformResult(ErrorCode::UNKNOWN_ERR, "app control setPowered failed")); + data->adapter->instance_.AsyncResponse( + data->callback_handle, + LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "app control setPowered failed")); } delete data; }, user_data); if (err != APP_CONTROL_ERROR_NONE) { - LoggerE("app control set launch request failed: %d", err); - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "app control set launch request failed"); + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "app control set launch request failed", + ("app control set launch request failed: %d", err)); } else { this->requested_powered_ = new_powered; this->user_request_list_[SET_POWERED] = true; @@ -585,8 +602,9 @@ void BluetoothAdapter::SetPowered(const picojson::value& data, picojson::object& err = app_control_destroy(service); if (err != APP_CONTROL_ERROR_NONE) { - LoggerE("app control destroy failed: %d", err); - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "app control destroy failed"); + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "app control destroy failed", + ("app control destroy failed: %d", err)); } if (!ret) { @@ -623,11 +641,11 @@ void BluetoothAdapter::SetVisible(const picojson::value& data, picojson::object& PlatformResult ret = PlatformResult(ErrorCode::NO_ERROR); if (!this->is_initialized()) { - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); + ret = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); } if (ret.IsSuccess() && this->user_request_list_[SET_VISIBLE]) { - ret = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested"); + ret = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested"); } if (ret.IsSuccess() && this->get_powered()) { @@ -643,8 +661,11 @@ void BluetoothAdapter::SetVisible(const picojson::value& data, picojson::object& bt_adapter_visibility_mode_e cur_mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE; int cur_timeout = 0; - if (BT_ERROR_NONE != bt_adapter_get_visibility(&cur_mode , &cur_timeout)) { - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + int ntv_ret = bt_adapter_get_visibility(&cur_mode , &cur_timeout); + if (BT_ERROR_NONE != ntv_ret) { + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("bt_adapter_get_visibility error: %d (%s)", ntv_ret, get_error_message(ntv_ret))); } if (ret.IsSuccess() && new_mode == cur_mode) { @@ -661,23 +682,23 @@ void BluetoothAdapter::SetVisible(const picojson::value& data, picojson::object& int err = 0; if ((err = app_control_create(&service)) != APP_CONTROL_ERROR_NONE) { - LoggerE("app control create failed: %d", err); - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "app control create failed"); + ret = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "app control create failed", + ("app control create failed: %d (%s)", err, get_error_message(err))); } if (ret.IsSuccess()) { err = app_control_set_operation(service, "http://tizen.org/appcontrol/operation/edit"); if (err != APP_CONTROL_ERROR_NONE) { - LoggerE("app control set operation failed: %d", err); - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "app control set operation failed"); + ret = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "app control set operation failed", + ("app control set operation failed: %d (%s)", err, get_error_message(err))); } } if (ret.IsSuccess()) { err = app_control_set_mime(service, "application/x-bluetooth-visibility"); if (err != APP_CONTROL_ERROR_NONE) { - LoggerE("app control set mime failed: %d", err); - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "app control set mime failed"); + ret = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "app control set mime failed", + ("app control set mime failed: %d (%s)", err, get_error_message(err))); } } @@ -701,43 +722,47 @@ void BluetoothAdapter::SetVisible(const picojson::value& data, picojson::object& self->user_request_list_[SET_VISIBLE] = true; self->user_request_callback_[SET_VISIBLE] = *p_callback_handle; } else { - LoggerE("app control setVisible failed"); - *p_ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "app control setVisible failed"); + *p_ret = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "app control setVisible failed"); } }, t_param); if (err != APP_CONTROL_ERROR_NONE) { - LoggerE("app control set launch request failed: %d", err); - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "app control set launch request failed"); + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "app control set launch request failed", + ("app control set launch request failed: %d (%s)", err, get_error_message(err))); } } err = app_control_destroy(service); if (err != APP_CONTROL_ERROR_NONE) { - LoggerE("app control destroy failed: %d", err); - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "app control destroy failed"); + ret = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "app control destroy failed", + ("app control destroy failed: %d (%s)", err, get_error_message(err))); } #else this->requested_visibility_ = new_mode; this->user_request_list_[SET_VISIBLE] = true; this->user_request_callback_[SET_VISIBLE] = callback_handle; - int ret = bt_adapter_set_visibility(new_mode, new_timeout); + int ntv_ret = bt_adapter_set_visibility(new_mode, new_timeout); - switch(ret) { + switch(ntv_ret) { case BT_ERROR_NONE: //bt_adapter_visibility_mode_changed_cb() will be invoked //if this function returns #BT_ERROR_NONE break; case BT_ERROR_INVALID_PARAMETER: - ret = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Invalid value"); + ret = LogAndCreateResult( + ErrorCode::INVALID_VALUES_ERR, "Invalid value", + ("bt_adapter_set_visibility error: %d (%s)", ntv_ret, get_error_message(ntv_ret))); break; default: - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("bt_adapter_set_visibility error: %d (%s)", ntv_ret, get_error_message(ntv_ret))); } #endif } } else if (ret.IsSuccess()){ - ret = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); + ret = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); } instance_.AsyncResponse(callback_handle, ret); @@ -749,15 +774,15 @@ void BluetoothAdapter::DiscoverDevices(const picojson::value& /* data */, picojs PlatformResult result = PlatformResult(ErrorCode::NO_ERROR); if (!is_initialized_) { - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); + result = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); } if (result.IsSuccess() && this->user_request_list_[DISCOVER_DEVICES]) { - result = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested"); + result = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested"); } if (result.IsSuccess() && !get_powered()) { - result = PlatformResult( + result = LogAndCreateResult( ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); } @@ -768,7 +793,7 @@ void BluetoothAdapter::DiscoverDevices(const picojson::value& /* data */, picojs std::shared_ptr response = std::shared_ptr(new picojson::value(picojson::object())); - ReportError(result, &response->get()); + LogAndReportError(result, &response->get()); TaskQueue::GetInstance().Async([this](const std::shared_ptr& result) { instance_.FireEvent(kAdapterDiscoverErrorEvent, result); }, response); @@ -783,11 +808,11 @@ void BluetoothAdapter::StopDiscovery(const picojson::value& data, picojson::obje PlatformResult result = PlatformResult(ErrorCode::NO_ERROR); if (!this->is_initialized()) { - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); + result = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); } if (result.IsSuccess() && this->user_request_list_[STOP_DISCOVERY]) { - result = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested"); + result = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested"); } if (result.IsSuccess() && this->get_powered()) { @@ -809,11 +834,13 @@ void BluetoothAdapter::StopDiscovery(const picojson::value& data, picojson::obje } default: { this->user_request_list_[STOP_DISCOVERY] = false; - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + result = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("bt_adapter_stop_device_discovery error: %d (%s)", ret, get_error_message(ret))); } } } else if (result.IsSuccess()) { - result = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); + result = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); } if (result.IsError()) { @@ -830,7 +857,7 @@ void BluetoothAdapter::GetKnownDevices(const picojson::value& data, picojson::ob PlatformResult ret = PlatformResult(ErrorCode::NO_ERROR); if (!this->is_initialized()) { - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); + ret = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); } if (ret.IsSuccess() && this->get_powered()) { @@ -842,17 +869,20 @@ void BluetoothAdapter::GetKnownDevices(const picojson::value& data, picojson::ob array = discovered_devices_; - if (BT_ERROR_NONE == bt_adapter_foreach_bonded_device(ForeachBondedDevicesCB, &array)) { + int ntv_ret = bt_adapter_foreach_bonded_device(ForeachBondedDevicesCB, &array); + if (BT_ERROR_NONE == ntv_ret) { ReportSuccess(result, response_obj); } else { - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("bt_adapter_foreach_bonded_device error %d (%s)", ntv_ret, get_error_message(ntv_ret))); } } else if (ret.IsSuccess()) { - ret = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); + ret = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); } if (ret.IsError()) { - ReportError(ret, &response->get()); + LogAndReportError(ret, &response->get()); } }; auto get_known_devices_response = [this, callback_handle]( @@ -881,11 +911,11 @@ void BluetoothAdapter::GetDevice(const picojson::value& data, picojson::object& auto get_device = [this, address](const std::shared_ptr& response) -> void { PlatformResult ret = PlatformResult(ErrorCode::NO_ERROR); if (!IsValidAddress(address)) { - ret = PlatformResult(ErrorCode::NOT_FOUND_ERR, "Wrong address"); + ret = LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, "Wrong address"); } if (ret.IsSuccess() && !this->is_initialized()) { - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); + ret = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); } if (ret.IsSuccess() && this->get_powered()) { picojson::object& response_obj = response->get(); @@ -913,14 +943,16 @@ void BluetoothAdapter::GetDevice(const picojson::value& data, picojson::object& } } } else { - ret = PlatformResult(ErrorCode::NOT_FOUND_ERR, "There is no device with the given address"); + ret = LogAndCreateResult( + ErrorCode::NOT_FOUND_ERR, "There is no device with the given address", + ("There is no device with the given address: %s", address.c_str())); } } else if (ret.IsSuccess()) { - ret = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); + ret = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); } if (ret.IsError()) { - ReportError(ret, &response->get()); + LogAndReportError(ret, &response->get()); } }; @@ -956,7 +988,7 @@ class BondingHandler { LoggerD("Entered"); if (result.IsError()) { - ReportError(result, &response->get()); + LogAndReportError(result, &response->get()); } else { ReportSuccess(response->get()); } @@ -981,11 +1013,13 @@ void BluetoothAdapter::CreateBonding(const picojson::value& data, picojson::obje auto create_bonding = [address, callback_handle, this]() -> void { PlatformResult result = PlatformResult(ErrorCode::NO_ERROR); if(!IsValidAddress(address)) { - result = PlatformResult(ErrorCode::NOT_FOUND_ERR, "Wrong address"); + result = LogAndCreateResult( + ErrorCode::NOT_FOUND_ERR, "Wrong address", + ("Wrong address: %s", address.c_str())); } if (result.IsSuccess() && !this->is_initialized()) { - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); + result = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); } if (result.IsSuccess() && this->get_powered()) { @@ -1029,11 +1063,15 @@ void BluetoothAdapter::CreateBonding(const picojson::value& data, picojson::obje result_obj["address"] = picojson::value(handler->address()); ReportSuccess(result, response_obj); } else if (BT_ERROR_REMOTE_DEVICE_NOT_FOUND == callback_result) { - LoggerE("Not found"); - ret = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Not found"); + ret = LogAndCreateResult( + ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Not found", + ("bond_create_callback result: %d (%s)", callback_result, + get_error_message(callback_result))); } else { - LoggerE("Unknown exception"); - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("bond_create_callback result: %d (%s)", callback_result, + get_error_message(callback_result))); } handler->Invoke(ret, response); @@ -1056,24 +1094,25 @@ void BluetoothAdapter::CreateBonding(const picojson::value& data, picojson::obje } case BT_ERROR_INVALID_PARAMETER: { - LoggerE("Not found"); bt_device_unset_bond_created_cb(); delete handler; - result = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Invalid value"); + result = LogAndCreateResult( + ErrorCode::INVALID_VALUES_ERR, "Invalid value", + ("bt_device_create_bond error: %d (%s)", ret, get_error_message(ret))); break; } default: { - LoggerE("Unknown exception"); bt_device_unset_bond_created_cb(); delete handler; - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + result = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("bt_device_create_bond error: %d (%s)", ret, get_error_message(ret))); } } } else if (result.IsSuccess()) { - LoggerE("Bluetooth device is turned off"); - result = PlatformResult( - ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); + result = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, + "Bluetooth device is turned off"); } if (result.IsError()) { @@ -1096,10 +1135,12 @@ void BluetoothAdapter::DestroyBonding(const picojson::value& data, picojson::obj auto destroy_bonding = [address, callback_handle, this]() -> void { PlatformResult result = PlatformResult(ErrorCode::NO_ERROR); if(!IsValidAddress(address)) { - result = PlatformResult(ErrorCode::NOT_FOUND_ERR, "Wrong address"); + result = LogAndCreateResult( + ErrorCode::NOT_FOUND_ERR, "Wrong address", + ("Wrong address: %s", address.c_str())); } if (result.IsSuccess() && !this->is_initialized()) { - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); + result = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); } if (result.IsSuccess() && this->get_powered()) { @@ -1107,8 +1148,9 @@ void BluetoothAdapter::DestroyBonding(const picojson::value& data, picojson::obj int ret = bt_adapter_get_bonded_device_info(address.c_str(), &device_info); if (BT_ERROR_NONE != ret || nullptr == device_info) { - LoggerD("There is no bonding"); - result = PlatformResult(ErrorCode::NOT_FOUND_ERR, "Not found"); + result = LogAndCreateResult( + ErrorCode::NOT_FOUND_ERR, "Not found", + ("There is no bonding %d (%s)", ret, get_error_message(ret))); } else { bt_adapter_free_device_info(device_info); @@ -1139,8 +1181,11 @@ void BluetoothAdapter::DestroyBonding(const picojson::value& data, picojson::obj if (!strcmp(address.c_str(), r_address.c_str())) { // requested event if (BT_ERROR_NONE != callback_result) { - LoggerE("Unknown exception"); - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("bond_destroy_callback result error: %d (%s)", + callback_result, + get_error_message(callback_result))); } handler->Invoke(ret, response); @@ -1164,24 +1209,25 @@ void BluetoothAdapter::DestroyBonding(const picojson::value& data, picojson::obj } case BT_ERROR_INVALID_PARAMETER: { - LoggerE("Not found"); bt_device_unset_bond_destroyed_cb(); delete handler; - result = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Invalid value"); + result = LogAndCreateResult( + ErrorCode::INVALID_VALUES_ERR, "Invalid value", + ("bt_device_destroy_bond error: %d (%s)", ret, get_error_message(ret))); break; } default: { - LoggerE("Unknown exception"); bt_device_unset_bond_destroyed_cb(); delete handler; - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + result = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("bt_device_destroy_bond error: %d (%s)", ret, get_error_message(ret))); } } } } else if (result.IsSuccess()) { - LoggerE("Bluetooth device is turned off"); - result = PlatformResult( + result = LogAndCreateResult( ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); } @@ -1205,11 +1251,13 @@ void BluetoothAdapter::RegisterRFCOMMServiceByUUID(const picojson::value& data, auto rfcomm = [this, uuid, name](const std::shared_ptr& response) -> void { PlatformResult result = PlatformResult(ErrorCode::NO_ERROR); if (!this->is_initialized()) { - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); + result = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."); } if (result.IsSuccess() && !IsValidUUID(uuid)) { - result = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Wrong UUID"); + result = LogAndCreateResult( + ErrorCode::INVALID_VALUES_ERR, "Wrong UUID", + ("Wrong UUID: %s", uuid.c_str())); } if (result.IsSuccess() && this->get_powered()) { @@ -1217,7 +1265,9 @@ void BluetoothAdapter::RegisterRFCOMMServiceByUUID(const picojson::value& data, int ret = bt_adapter_is_service_used(uuid.c_str(), &is_registered); if (BT_ERROR_NONE == ret && is_registered) { - result = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested"); + result = LogAndCreateResult( + ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Already requested", + ("bt_adapter_is_service_used error: %d (%s)", ret, get_error_message(ret))); } else { int socket = -1; ret = bt_socket_create_rfcomm(uuid.c_str(), &socket); @@ -1238,32 +1288,40 @@ void BluetoothAdapter::RegisterRFCOMMServiceByUUID(const picojson::value& data, } case BT_ERROR_INVALID_PARAMETER: { - result = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Invalid value"); + result = LogAndCreateResult( + ErrorCode::INVALID_VALUES_ERR, "Invalid value", + ("bt_socket_listen_and_accept_rfcomm error: %d (%s)", ret_in, get_error_message(ret_in))); break; } default: { - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"); + result = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown error", + ("bt_socket_listen_and_accept_rfcomm error: %d (%s)", ret_in, get_error_message(ret_in))); break; } } break; } case BT_ERROR_INVALID_PARAMETER: - result = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Invalid value"); + result = LogAndCreateResult( + ErrorCode::INVALID_VALUES_ERR, "Invalid value", + ("bt_socket_create_rfcomm error: %d (%s)", ret, get_error_message(ret))); break; default: - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"); + result = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown error", + ("bt_socket_create_rfcomm error: %d (%s)", ret, get_error_message(ret))); break; } } } else if (result.IsSuccess()) { - result = PlatformResult( + result = LogAndCreateResult( ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); } if (result.IsError()) { - ReportError(result, &response->get()); + LogAndReportError(result, &response->get()); } }; @@ -1286,16 +1344,21 @@ void BluetoothAdapter::UnregisterUUID(const std::string& uuid, int callback_hand PlatformResult result = PlatformResult(ErrorCode::NO_ERROR); if (!IsValidUUID(uuid)) { - result = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Wrong UUID"); + result = LogAndCreateResult( + ErrorCode::INVALID_VALUES_ERR, "Wrong UUID", + ("Wrong UUID: %s", uuid.c_str())); } if (result.IsSuccess() && is_powered_) { auto iter = registered_uuids_.find(uuid); if (iter != registered_uuids_.end()) { - if (BT_ERROR_NONE == bt_socket_destroy_rfcomm(iter->second.first)) { + int ntv_ret = bt_socket_destroy_rfcomm(iter->second.first); + if (BT_ERROR_NONE == ntv_ret) { registered_uuids_.erase(iter); } else { - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + result = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("bt_socket_destroy_rfcomm error: %d (%s)", ntv_ret, get_error_message(ntv_ret))); } } @@ -1305,7 +1368,7 @@ void BluetoothAdapter::UnregisterUUID(const std::string& uuid, int callback_hand bt_socket_unset_connection_state_changed_cb(); } } else if (result.IsSuccess()){ - result = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); + result = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); } instance_.AsyncResponse(callback_handle, result); @@ -1328,19 +1391,19 @@ void BluetoothAdapter::GetBluetoothProfileHandler(const picojson::value& data, } if (!supported) { - result = PlatformResult( + result = LogAndCreateResult( ErrorCode::NOT_SUPPORTED_ERR, "Bluetooth health profile is not supported"); } else { LoggerD("BT health profile is supported"); } } else { - result = PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "Wrong profile type."); + result = LogAndCreateResult(ErrorCode::TYPE_MISMATCH_ERR, "Wrong profile type."); } if (result.IsSuccess()) { ReportSuccess(out); } else { - ReportError(result, &out); + LogAndReportError(result, &out); } } @@ -1354,7 +1417,7 @@ void BluetoothAdapter::GetAddress(const picojson::value& /* data */, picojson::o LoggerD("Entered"); if (!is_initialized_) { - ReportError(PlatformResult( + LogAndReportError(PlatformResult( ErrorCode::UNKNOWN_ERR, "Bluetooth service is not initialized."), &out); return; } @@ -1465,7 +1528,7 @@ void BluetoothAdapter::OnSocketConnected( ReportSuccess(BluetoothSocket::ToJson(connection), response->get()); } else { - ReportError(PlatformResult( + LogAndReportError(PlatformResult( ErrorCode::NOT_FOUND_ERR, "Not found"), &response->get()); } @@ -1535,7 +1598,9 @@ void BluetoothAdapter::ConnectToServiceByUUID( PlatformResult result = PlatformResult(ErrorCode::NO_ERROR); if (!IsValidUUID(uuid)) { - result = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Wrong UUID"); + result = LogAndCreateResult( + ErrorCode::INVALID_VALUES_ERR, "Wrong UUID", + ("Wrong UUID: %s", uuid.c_str())); } if (result.IsSuccess() && is_powered_) { @@ -1561,14 +1626,19 @@ void BluetoothAdapter::ConnectToServiceByUUID( case BT_ERROR_INVALID_PARAMETER: case BT_ERROR_REMOTE_DEVICE_NOT_BONDED: - result = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Invalid value"); + result = LogAndCreateResult( + ErrorCode::INVALID_VALUES_ERR, "Invalid value", + ("bt_socket_connect_rfcomm error: %d (%s)", ret, get_error_message(ret))); break; default: - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"); + result = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown error", + ("bt_socket_connect_rfcomm error: %d (%s)", ret, get_error_message(ret))); break; } } else if (result.IsSuccess()) { - result = PlatformResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); + result = LogAndCreateResult(ErrorCode::SERVICE_NOT_AVAILABLE_ERR, + "Bluetooth device is turned off"); } if (result.IsError()) { @@ -1648,8 +1718,9 @@ void BluetoothAdapter::IsServiceConnected(const picojson::value& data, picojson: auto iter = registered_uuids_.find(uuid); if (iter == registered_uuids_.end()) { - ReportError(PlatformResult( - ErrorCode::INVALID_VALUES_ERR, "Invalid parameter was passed."), &out); + LogAndReportError(PlatformResult( + ErrorCode::INVALID_VALUES_ERR, "Invalid parameter was passed."), &out, + ("Invalid uuid was passed: %s", uuid.c_str())); return; } diff --git a/src/bluetooth/bluetooth_device.cc b/src/bluetooth/bluetooth_device.cc index 5315d39c..0cf8c1b0 100755 --- a/src/bluetooth/bluetooth_device.cc +++ b/src/bluetooth/bluetooth_device.cc @@ -123,8 +123,8 @@ void BluetoothDevice::GetBoolValue(const picojson::value& data, picojson::object PlatformResult result = PlatformResult(ErrorCode::NO_ERROR); bool value = false; bt_device_info_s *info = nullptr; - if (bt_adapter_get_bonded_device_info(address.c_str(), &info) == BT_ERROR_NONE && - info != nullptr) { + int ntv_ret = bt_adapter_get_bonded_device_info(address.c_str(), &info); + if (BT_ERROR_NONE == ntv_ret && info != nullptr) { if (kDeviceIsBonded == field) { value = info->is_bonded; } else if (kDeviceIsTrusted == field) { @@ -132,17 +132,21 @@ void BluetoothDevice::GetBoolValue(const picojson::value& data, picojson::object } else if (kDeviceIsConnected == field) { value = info->is_connected; } else { - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Wrong field passed."); + result = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Wrong field passed.", + ("Wrong field passed: %s", field.c_str())); } bt_adapter_free_device_info(info); } else { - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"); + result = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown error", + ("bt_adapter_get_bonded_device_info error: %d (%s)", ntv_ret, get_error_message(ntv_ret))); } if (result.IsSuccess()) { ReportSuccess(picojson::value(value), out); } else { - ReportError(result, &out); + LogAndReportError(result, &out); } } diff --git a/src/bluetooth/bluetooth_gatt_service.cc b/src/bluetooth/bluetooth_gatt_service.cc index 293c0f07..a6c95b10 100755 --- a/src/bluetooth/bluetooth_gatt_service.cc +++ b/src/bluetooth/bluetooth_gatt_service.cc @@ -125,7 +125,8 @@ PlatformResult BluetoothGATTService::GetSpecifiedGATTService(const std::string & bt_gatt_client_h client = GetGattClient(address); if (nullptr == client) { - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to create the GATT client's handle"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, + "Failed to create the GATT client's handle"); } bt_gatt_h service = nullptr; @@ -134,13 +135,19 @@ PlatformResult BluetoothGATTService::GetSpecifiedGATTService(const std::string & LoggerE("bt_gatt_client_get_service() error: %d", ret); switch (ret) { case BT_ERROR_NO_DATA: - return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Service not found"); + return LogAndCreateResult( + ErrorCode::NOT_FOUND_ERR, "Service not found", + ("bt_gatt_client_get_service error: %d (%s)", ret, get_error_message(ret))); case BT_ERROR_INVALID_PARAMETER: - return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Service UUID is invalid"); + return LogAndCreateResult( + ErrorCode::NOT_FOUND_ERR, "Service UUID is invalid", + ("bt_gatt_client_get_service error: %d (%s)", ret, get_error_message(ret))); default: - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to get a service's GATT handle"); + return LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Failed to get a service's GATT handle", + ("bt_gatt_client_get_service error: %d (%s)", ret, get_error_message(ret))); } } @@ -163,8 +170,7 @@ void BluetoothGATTService::GetServices(const picojson::value& args, picojson::array array; PlatformResult ret = GetServicesHelper(handle, address, &array); if (ret.IsError()) { - LoggerE("Error while getting services"); - ReportError(ret, &out); + LogAndReportError(ret, &out, ("Error while getting services")); } else { ReportSuccess(picojson::value(array), out); } @@ -176,9 +182,9 @@ PlatformResult BluetoothGATTService::GetServicesHelper(bt_gatt_h handle, LoggerD("Entered"); if (!IsStillConnected(address)) { - LoggerE("Device with address %s is no longer connected", address.c_str()); - return PlatformResult(ErrorCode::INVALID_STATE_ERR, - "Device is not connected"); + return LogAndCreateResult( + ErrorCode::INVALID_STATE_ERR, "Device is not connected", + ("Device with address %s is no longer connected", address.c_str())); } int ret = bt_gatt_service_foreach_included_services( @@ -222,8 +228,7 @@ void BluetoothGATTService::GetCharacteristics(const picojson::value& args, picojson::array array; PlatformResult ret = GetCharacteristicsHelper(handle, address, uuid, &array); if (ret.IsError()) { - LoggerE("Error while getting characteristics"); - ReportError(ret, &out); + LogAndReportError(ret, &out, ("Error while getting characteristics")); } else { ReportSuccess(picojson::value(array), out); } @@ -236,9 +241,9 @@ PlatformResult BluetoothGATTService::GetCharacteristicsHelper(bt_gatt_h handle, LoggerD("Entered"); if (!IsStillConnected(address)) { - LoggerE("Device with address %s is no longer connected", address.c_str()); - return PlatformResult(ErrorCode::INVALID_STATE_ERR, - "Device is not connected"); + return LogAndCreateResult( + ErrorCode::INVALID_STATE_ERR, "Device is not connected", + ("Device with address %s is no longer connected", address.c_str())); } struct Data { @@ -331,9 +336,9 @@ void BluetoothGATTService::ReadValue(const picojson::value& args, const std::string& address = args.get("address").get(); if (!IsStillConnected(address)) { - LoggerE("Device with address %s is no longer connected", address.c_str()); - ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR, - "Device is not connected"), &out); + LogAndReportError( + PlatformResult(ErrorCode::INVALID_STATE_ERR, "Device is not connected"), &out, + ("Device with address %s is no longer connected", address.c_str())); return; } @@ -381,7 +386,7 @@ void BluetoothGATTService::ReadValue(const picojson::value& args, if (plarform_res.IsSuccess()) { ReportSuccess(byte_array, response->get()); } else { - ReportError(plarform_res, &response->get()); + LogAndReportError(plarform_res, &response->get()); } TaskQueue::GetInstance().Async( [service, callback_handle](const std::shared_ptr& response) { @@ -390,7 +395,8 @@ void BluetoothGATTService::ReadValue(const picojson::value& args, }; int ret = bt_gatt_client_read_value(handle, read_value, (void*)user_data); if (BT_ERROR_NONE != ret) { - LOGE("Couldn't register callback for read value"); + LoggerE("Couldn't register callback for read value %d (%s)", + ret, get_error_message(ret)); } ReportSuccess(out); } @@ -402,9 +408,9 @@ void BluetoothGATTService::WriteValue(const picojson::value& args, const std::string& address = args.get("address").get(); if (!IsStillConnected(address)) { - LoggerE("Device with address %s is no longer connected", address.c_str()); - ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR, - "Device is not connected"), &out); + LogAndReportError( + PlatformResult(ErrorCode::INVALID_STATE_ERR, "Device is not connected"), &out, + ("Device with address %s is no longer connected", address.c_str())); return; } @@ -440,7 +446,7 @@ void BluetoothGATTService::WriteValue(const picojson::value& args, if (ret.IsSuccess()) { ReportSuccess(response->get()); } else { - ReportError(ret, &response->get()); + LogAndReportError(ret, &response->get()); } TaskQueue::GetInstance().Async( [service, callback_handle](const std::shared_ptr& response) { @@ -451,11 +457,12 @@ void BluetoothGATTService::WriteValue(const picojson::value& args, int ret = bt_gatt_set_value(handle, value_data.get(), value_size); if (BT_ERROR_NONE != ret) { - LoggerE("Couldn't set value"); std::shared_ptr response = std::shared_ptr(new picojson::value(picojson::object())); - ReportError(util::GetBluetoothError(ret, "Failed to set value"), - &response->get()); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to set value"), + &response->get(), + ("bt_gatt_set_value error: %d (%s)", ret, get_error_message(ret))); TaskQueue::GetInstance().Async( [this, callback_handle](const std::shared_ptr& response) { instance_.SyncResponse(callback_handle, response); @@ -465,7 +472,8 @@ void BluetoothGATTService::WriteValue(const picojson::value& args, ret = bt_gatt_client_write_value(handle, write_value, user_data); if (BT_ERROR_NONE != ret) { delete user_data; - LoggerE("Couldn't register callback for write value"); + LoggerE("Couldn't register callback for write value %d (%s)", + ret, get_error_message(ret)); } } ReportSuccess(out); @@ -476,9 +484,9 @@ void BluetoothGATTService::AddValueChangeListener(const picojson::value& args, LoggerD("Entered"); const auto& address = args.get("address").get(); if (!IsStillConnected(address)) { - LoggerE("Device with address %s is no longer connected", address.c_str()); - ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR, - "Device is not connected"), &out); + LogAndReportError( + PlatformResult(ErrorCode::INVALID_STATE_ERR, "Device is not connected"), &out, + ("Device with address %s is no longer connected", address.c_str())); return; } @@ -486,8 +494,10 @@ void BluetoothGATTService::AddValueChangeListener(const picojson::value& args, int ret = bt_gatt_client_set_characteristic_value_changed_cb(handle, OnCharacteristicValueChanged, this); if (BT_ERROR_NONE != ret) { - LoggerE("bt_gatt_client_set_characteristic_value_changed_cb() failed with: %d", ret); - ReportError(util::GetBluetoothError(ret, "Failed to register listener"), &out); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to register listener"), &out, + ("bt_gatt_client_set_characteristic_value_changed_cb() failed with: %d (%s)", + ret, get_error_message(ret))); } else { gatt_characteristic_.push_back(handle); ReportSuccess(out); @@ -499,9 +509,9 @@ void BluetoothGATTService::RemoveValueChangeListener( LoggerD("Entered"); const auto& address = args.get("address").get(); if (!IsStillConnected(address)) { - LoggerE("Device with address %s is no longer connected", address.c_str()); - ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR, - "Device is not connected"), &out); + LogAndReportError( + PlatformResult(ErrorCode::INVALID_STATE_ERR, "Device is not connected"), &out, + ("Device with address %s is no longer connected", address.c_str())); return; } @@ -510,8 +520,10 @@ void BluetoothGATTService::RemoveValueChangeListener( int ret = bt_gatt_client_unset_characteristic_value_changed_cb(handle); if (BT_ERROR_NONE != ret) { - LoggerE("bt_gatt_client_unset_characteristic_value_changed_cb() failed with: %d", ret); - ReportError(util::GetBluetoothError(ret, "Failed to unregister listener"), &out); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to unregister listener"), &out, + ("bt_gatt_client_unset_characteristic_value_changed_cb() failed with: %d (%s)", + ret, get_error_message(ret))); } else { gatt_characteristic_.erase(std::remove(gatt_characteristic_.begin(), gatt_characteristic_.end(), handle), gatt_characteristic_.end()); ReportSuccess(out); @@ -525,7 +537,7 @@ common::PlatformResult BluetoothGATTService::GetServiceUuids( bt_gatt_client_h client = GetGattClient(address); if (nullptr == client) { - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Unable to create client"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Unable to create client"); } auto foreach_callback = [](int total, int index, bt_gatt_h gatt_handle, void* user_data) -> bool { @@ -553,7 +565,6 @@ common::PlatformResult BluetoothGATTService::GetServiceUuids( if (BT_ERROR_NONE == ret) { return PlatformResult(ErrorCode::NO_ERROR); } else { - LoggerE("Failed to get UUIDS: %d", ret); return util::GetBluetoothError(ret, "Failed to get UUIDS"); } } diff --git a/src/bluetooth/bluetooth_health_channel.cc b/src/bluetooth/bluetooth_health_channel.cc index 1209021e..f6c7f65d 100755 --- a/src/bluetooth/bluetooth_health_channel.cc +++ b/src/bluetooth/bluetooth_health_channel.cc @@ -48,8 +48,11 @@ void BluetoothHealthChannel::Close(const picojson::value& data , picojson::objec unsigned int channel = common::stol(FromJson(args, "channel")); const auto& address = FromJson(args, "address"); - if (BT_ERROR_NONE != bt_hdp_disconnect(address.c_str(), channel)) { - ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"), &out); + int ntv_ret = bt_hdp_disconnect(address.c_str(), channel); + if (BT_ERROR_NONE != ntv_ret) { + LogAndReportError( + PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"), &out, + ("bt_hdp_disconnect error: %d (%s)", ntv_ret, get_error_message(ntv_ret))); return; } @@ -71,9 +74,11 @@ void BluetoothHealthChannel::SendData(const picojson::value& data, picojson::obj data_ptr[i] = static_cast(binary_data[i].get()); } - if (BT_ERROR_NONE != bt_hdp_send_data(channel, data_ptr.get(), data_size)) { - LoggerE("bt_hdp_send_data() failed"); - ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"), &out); + int ntv_ret = bt_hdp_send_data(channel, data_ptr.get(), data_size); + if (BT_ERROR_NONE != ntv_ret) { + LogAndReportError( + PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"), &out, + ("bt_hdp_send_data() failed: %d (%s)", ntv_ret, get_error_message(ntv_ret))); return; } diff --git a/src/bluetooth/bluetooth_health_profile_handler.cc b/src/bluetooth/bluetooth_health_profile_handler.cc index 1f0432d0..eb2be4fb 100755 --- a/src/bluetooth/bluetooth_health_profile_handler.cc +++ b/src/bluetooth/bluetooth_health_profile_handler.cc @@ -152,10 +152,10 @@ void BluetoothHealthProfileHandler::OnConnected(int result, ReportSuccess(result, response->get()); } else { - LoggerE("Failed to establish a connection with health profile"); - ReportError(PlatformResult( - ErrorCode::UNKNOWN_ERR, "Failed to establish a connection with health profile"), - &response->get()); + LogAndReportError( + PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to establish a connection with health profile"), + &response->get(), + ("OnConnected result: %d (%s)", result, get_error_message(result))); } object->instance_.AsyncResponse(request->second, response); @@ -258,18 +258,19 @@ void BluetoothHealthProfileHandler::RegisterSinkApp(const picojson::value& data, } case BT_ERROR_NOT_ENABLED: - LoggerE("Bluetooth device is turned off"); - platform_result = PlatformResult( - ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); + platform_result = LogAndCreateResult( + ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off", + ("bt_hdp_register_sink_app error %d (%s)", ret, get_error_message(ret))); break; default: - LoggerE("bt_hdp_register_sink_app() failed: %d", ret); - platform_result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"); + platform_result = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown error", + ("bt_hdp_register_sink_app() failed: %d (%s)", ret, get_error_message(ret))); break; } - ReportError(platform_result, &response->get()); + LogAndReportError(platform_result, &response->get()); }; auto register_app_response = [this, callback_handle](const std::shared_ptr& response) -> void { @@ -310,17 +311,22 @@ void BluetoothHealthProfileHandler::ConnectToSource(const picojson::value& data, } case BT_ERROR_NOT_ENABLED: - result = PlatformResult( - ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); + result = LogAndCreateResult( + ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off", + ("bt_hdp_connect_to_source error: %d (%s)", ret, get_error_message(ret))); break; case BT_ERROR_INVALID_PARAMETER: case BT_ERROR_REMOTE_DEVICE_NOT_BONDED: - result = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Invalid value"); + result = LogAndCreateResult( + ErrorCode::INVALID_VALUES_ERR, "Invalid value", + ("bt_hdp_connect_to_source error: %d (%s)", ret, get_error_message(ret))); break; default: - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + result = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("bt_hdp_connect_to_source error: %d (%s)", ret, get_error_message(ret))); break; } @@ -352,14 +358,16 @@ void BluetoothHealthProfileHandler::UnregisterSinkAppAsync(const std::string& ap break; case BT_ERROR_NOT_ENABLED: - LoggerE("Bluetooth device is turned off"); - result = PlatformResult( - ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off"); + result = LogAndCreateResult( + ErrorCode::SERVICE_NOT_AVAILABLE_ERR, "Bluetooth device is turned off", + ("Bluetooth device is turned off %d (%s)", ret, get_error_message(ret))); break; default: LoggerE("bt_hdp_unregister_sink_app() failed: %d", ret); - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown exception"); + result = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Unknown exception", + ("Bluetooth device is turned off %d (%s)", ret, get_error_message(ret))); break; } } else { @@ -369,7 +377,7 @@ void BluetoothHealthProfileHandler::UnregisterSinkAppAsync(const std::string& ap if (result.IsSuccess()) { ReportSuccess(response->get()); } else { - ReportError(result, &response->get()); + LogAndReportError(result, &response->get()); } }; diff --git a/src/bluetooth/bluetooth_instance.cc b/src/bluetooth/bluetooth_instance.cc index c919693a..a0c5de92 100755 --- a/src/bluetooth/bluetooth_instance.cc +++ b/src/bluetooth/bluetooth_instance.cc @@ -192,9 +192,9 @@ void BluetoothInstance::AsyncResponse(double callback_handle, const PlatformResu std::shared_ptr(new picojson::value(picojson::object())); if (result.IsError()) { - tools::ReportError(result, &response->get()); + LogAndReportError(result, &response->get()); } else { - tools::ReportSuccess(response->get()); + ReportSuccess(response->get()); } TaskQueue::GetInstance().Async([this, callback_handle](const std::shared_ptr& response) { diff --git a/src/bluetooth/bluetooth_le_adapter.cc b/src/bluetooth/bluetooth_le_adapter.cc index f2c232d1..2691cf45 100755 --- a/src/bluetooth/bluetooth_le_adapter.cc +++ b/src/bluetooth/bluetooth_le_adapter.cc @@ -427,16 +427,18 @@ void BluetoothLEAdapter::StartScan(const picojson::value& data, picojson::object if (BT_ERROR_NONE != ret) { if (BT_ERROR_NOW_IN_PROGRESS == ret) { - LoggerE("Scan in progress"); - ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR, "Scan already in progress"), &out); + LogAndReportError( + PlatformResult(ErrorCode::INVALID_STATE_ERR, "Scan already in progress"), &out, + ("Scan in progress %d (%s)", ret, get_error_message(ret))); } else { - LoggerE("Failed to start scan: %d", ret); // other errors are reported asynchronously picojson::value value = picojson::value(picojson::object()); picojson::object* data_obj = &value.get(); data_obj->insert(std::make_pair(kAction, picojson::value(kOnScanError))); - ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to start scan"), data_obj); + LogAndReportError( + PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to start scan"), data_obj, + ("Failed to start scan: %d (%s)", ret, get_error_message(ret))); instance_.FireEvent(kScanEvent, value); } } else { @@ -451,8 +453,9 @@ void BluetoothLEAdapter::StopScan(const picojson::value& data, picojson::object& int ret = bt_adapter_le_stop_scan(); if (BT_ERROR_NONE != ret && BT_ERROR_NOT_IN_PROGRESS != ret) { - LoggerE("Failed to stop scan: %d", ret); - ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to stop scan"), &out); + LogAndReportError( + PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to stop scan"), &out, + ("Failed to stop scan: %d (%s)", ret, get_error_message(ret))); } else { scanning_ = false; ReportSuccess(out); @@ -471,13 +474,15 @@ void BluetoothLEAdapter::StartAdvertise(const picojson::value& data, picojson::o !json_packet_type.is() || !json_mode.is() || !json_connectable.is()) { - ReportError(PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "Unexpected parameter type"), &out); + LogAndReportError( + PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "Unexpected parameter type"), &out); return; } BluetoothLEAdvertiseData advertise_data; if (!BluetoothLEAdvertiseData::Construct(json_advertise_data, &advertise_data)) { - ReportError(PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "Unexpected value of advertise data"), &out); + LogAndReportError( + PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "Unexpected value of advertise data"), &out); return; } @@ -489,8 +494,9 @@ void BluetoothLEAdapter::StartAdvertise(const picojson::value& data, picojson::o } else if ("SCAN_RESPONSE" == str_packet_type) { packet_type = BT_ADAPTER_LE_PACKET_SCAN_RESPONSE; } else { - LoggerE("Fail: json_packet_type.get"); - ReportError(PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "Unexpected value of packet type"), &out); + LogAndReportError( + PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "Unexpected value of packet type"), &out, + ("Wrong packet_type: %s", str_packet_type.c_str())); return; } } @@ -505,14 +511,16 @@ void BluetoothLEAdapter::StartAdvertise(const picojson::value& data, picojson::o } else if ("LOW_ENERGY" == str_mode) { mode = BT_ADAPTER_LE_ADVERTISING_MODE_LOW_ENERGY; } else { - ReportError(PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "Unexpected value of mode"), &out); + LogAndReportError( + PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "Unexpected value of mode"), &out, + ("Wrong mode: %s", str_mode.c_str())); return; } } if (nullptr != bt_advertiser_) { - LoggerE("Advertise in progress"); - ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR, "Advertise already in progress"), &out); + LogAndReportError( + PlatformResult(ErrorCode::INVALID_STATE_ERR, "Advertise already in progress"), &out); return; } @@ -520,8 +528,9 @@ void BluetoothLEAdapter::StartAdvertise(const picojson::value& data, picojson::o int ret = bt_adapter_le_create_advertiser(&advertiser); if (BT_ERROR_NONE != ret) { - LoggerE("bt_adapter_le_create_advertiser() failed with: %d", ret); - ReportError(util::GetBluetoothError(ret, "Failed to create advertiser"), &out); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to create advertiser"), &out, + ("bt_adapter_le_create_advertiser() failed with: %d, (%s)", ret, get_error_message(ret))); return; } @@ -533,8 +542,9 @@ void BluetoothLEAdapter::StartAdvertise(const picojson::value& data, picojson::o ret = bt_adapter_le_set_advertising_device_name(advertiser, packet_type, advertise_data.include_name()); if (BT_ERROR_NONE != ret) { - LoggerE("bt_adapter_le_set_advertising_device_name() failed with: %d", ret); - ReportError(util::GetBluetoothError(ret, "Failed to create advertiser"), &out); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to create advertiser"), &out, + ("bt_adapter_le_set_advertising_device_name() failed with: %d (%s)", ret, get_error_message(ret))); return; } } @@ -543,8 +553,9 @@ void BluetoothLEAdapter::StartAdvertise(const picojson::value& data, picojson::o ret = bt_adapter_le_add_advertising_service_uuid(advertiser, packet_type, i.c_str()); if (BT_ERROR_NONE != ret) { - LoggerE("bt_adapter_le_add_advertising_service_uuid() failed with: %d", ret); - ReportError(util::GetBluetoothError(ret, "Failed to create advertiser"), &out); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to create advertiser"), &out, + ("bt_adapter_le_add_advertising_service_uuid() failed with: %d (%s)", ret, get_error_message(ret))); return; } } @@ -554,8 +565,10 @@ void BluetoothLEAdapter::StartAdvertise(const picojson::value& data, picojson::o packet_type, i.c_str()); if (BT_ERROR_NONE != ret) { - LoggerE("bt_adapter_le_add_advertising_service_solicitation_uuid() failed with: %d", ret); - ReportError(util::GetBluetoothError(ret, "Failed to create advertiser"), &out); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to create advertiser"), &out, + ("bt_adapter_le_add_advertising_service_solicitation_uuid() failed with: %d (%s)", + ret, get_error_message(ret))); return; } } @@ -563,8 +576,10 @@ void BluetoothLEAdapter::StartAdvertise(const picojson::value& data, picojson::o ret = bt_adapter_le_set_advertising_appearance(advertiser, packet_type, advertise_data.appearance()); if (BT_ERROR_NONE != ret) { - LoggerE("bt_adapter_le_set_advertising_appearance() failed with: %d", ret); - ReportError(util::GetBluetoothError(ret, "Failed to create advertiser"), &out); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to create advertiser"), &out, + ("bt_adapter_le_set_advertising_appearance() failed with: %d (%s)", + ret, get_error_message(ret))); return; } @@ -572,8 +587,10 @@ void BluetoothLEAdapter::StartAdvertise(const picojson::value& data, picojson::o ret = bt_adapter_le_set_advertising_tx_power_level(advertiser, packet_type, advertise_data.include_tx_power_level()); if (BT_ERROR_NONE != ret) { - LoggerE("bt_adapter_le_set_advertising_tx_power_level() failed with: %d", ret); - ReportError(util::GetBluetoothError(ret, "Failed to create advertiser"), &out); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to create advertiser"), &out, + ("bt_adapter_le_set_advertising_tx_power_level() failed with: %d (%s)", + ret, get_error_message(ret))); return; } } @@ -587,8 +604,10 @@ void BluetoothLEAdapter::StartAdvertise(const picojson::value& data, picojson::o service_data.data().c_str(), service_data.data().length()); if (BT_ERROR_NONE != ret) { - LoggerE("bt_adapter_le_add_advertising_service_data() failed with: %d", ret); - ReportError(util::GetBluetoothError(ret, "Failed to create advertiser"), &out); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to create advertiser"), &out, + ("bt_adapter_le_add_advertising_service_data() failed with: %d (%s)", + ret, get_error_message(ret))); return; } } @@ -604,8 +623,10 @@ void BluetoothLEAdapter::StartAdvertise(const picojson::value& data, picojson::o (const char*)manufacturer_data.data(), manufacturer_data.data_length()); if (BT_ERROR_NONE != ret) { - LoggerE("bt_adapter_le_add_advertising_manufacturer_data() failed with: %d", ret); - ReportError(util::GetBluetoothError(ret, "Failed to create advertiser"), &out); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to create advertiser"), &out, + ("bt_adapter_le_add_advertising_manufacturer_data() failed with: %d (%s)", + ret, get_error_message(ret))); return; } } @@ -613,15 +634,19 @@ void BluetoothLEAdapter::StartAdvertise(const picojson::value& data, picojson::o ret = bt_adapter_le_set_advertising_mode(advertiser, mode); if (BT_ERROR_NONE != ret) { - LoggerE("bt_adapter_le_set_advertising_mode() failed with: %d", ret); - ReportError(util::GetBluetoothError(ret, "Failed to create advertiser"), &out); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to create advertiser"), &out, + ("bt_adapter_le_set_advertising_mode() failed with: %d (%s)", + ret, get_error_message(ret))); return; } ret = bt_adapter_le_set_advertising_connectable(advertiser, json_connectable.get()); if (BT_ERROR_NONE != ret) { - LoggerE("bt_adapter_le_set_advertising_connectable() failed with: %d", ret); - ReportError(util::GetBluetoothError(ret, "Failed to create advertiser"), &out); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to create advertiser"), &out, + ("bt_adapter_le_set_advertising_connectable() failed with: %d (%s)", + ret, get_error_message(ret))); return; } @@ -629,12 +654,16 @@ void BluetoothLEAdapter::StartAdvertise(const picojson::value& data, picojson::o ret = bt_adapter_le_start_advertising_new(advertiser, OnAdvertiseResult, this); if (BT_ERROR_NONE != ret) { if (BT_ERROR_NOW_IN_PROGRESS == ret) { - ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR, "Advertise already in progress"), &out); + LogAndReportError( + PlatformResult(ErrorCode::INVALID_STATE_ERR, "Advertise already in progress"), &out, + ("bt_adapter_le_start_advertising_new error: %d (%s)", ret, get_error_message(ret))); return; } - LoggerE("bt_adapter_le_start_advertising_new() failed with: %d", ret); - ReportError(util::GetBluetoothError(ret, "Failed to start advertising"), &out); + LogAndReportError( + util::GetBluetoothError(ret, "Failed to start advertising"), &out, + ("bt_adapter_le_start_advertising_new() failed with: %d (%s)", + ret, get_error_message(ret))); return; } @@ -649,15 +678,19 @@ void BluetoothLEAdapter::StopAdvertise(const picojson::value& data, picojson::ob if (nullptr != bt_advertiser_) { int ret = bt_adapter_le_stop_advertising(bt_advertiser_); if (BT_ERROR_NONE != ret && BT_ERROR_NOT_IN_PROGRESS != ret) { - LoggerE("bt_adapter_le_stop_advertising() failed with: %d", ret); - ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to stop advertising"), &out); + LogAndReportError( + PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to stop advertising"), &out, + ("bt_adapter_le_stop_advertising() failed with: %d (%s)", + ret, get_error_message(ret))); return; } ret = bt_adapter_le_destroy_advertiser(bt_advertiser_); if (BT_ERROR_NONE != ret && BT_ERROR_NOT_IN_PROGRESS != ret) { - LoggerE("bt_adapter_le_destroy_advertiser() failed with: %d", ret); - ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to destroy advertiser"), &out); + LogAndReportError( + PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to destroy advertiser"), &out, + ("bt_adapter_le_destroy_advertiser() failed with: %d (%s)", + ret, get_error_message(ret))); return; } @@ -700,8 +733,9 @@ void BluetoothLEAdapter::OnScanResult( picojson::object* data_obj = &value.get(); if (BT_ERROR_NONE != result) { - LoggerE("Error during scanning: %d", result); - ReportError(util::GetBluetoothError(result, "Error during scanning"), data_obj); + LogAndReportError( + util::GetBluetoothError(result, "Error during scanning"), data_obj, + ("Error during scanning: %d (%s)", result, get_error_message(result))); data_obj->insert(std::make_pair(kAction, picojson::value(kOnScanError))); } else { // this is probably capi-network-bluetooth error: when scan is stopped info has 0x1 value @@ -714,8 +748,7 @@ void BluetoothLEAdapter::OnScanResult( data_obj->insert(std::make_pair(kAction, picojson::value(kOnScanSuccess))); data_obj->insert(std::make_pair(kData, data)); } else { - LoggerE("Failed to parse Bluetooth LE device"); - ReportError(r, data_obj); + LogAndReportError(r, data_obj, ("Failed to parse Bluetooth LE device")); data_obj->insert(std::make_pair(kAction, picojson::value(kOnScanError))); } } @@ -740,8 +773,9 @@ void BluetoothLEAdapter::OnAdvertiseResult( picojson::object* data_obj = &value.get(); if (BT_ERROR_NONE != result) { - LoggerE("Error during advertising: %d", result); - ReportError(util::GetBluetoothError(result, "Error during advertising"), data_obj); + LogAndReportError( + util::GetBluetoothError(result, "Error during advertising"), data_obj, + ("Error during advertising: %d (%s)", result, get_error_message(result))); data_obj->insert(std::make_pair(kAction, picojson::value(kOnAdvertiseError))); } else { const char* state = (BT_ADAPTER_LE_ADVERTISING_STARTED == adv_state) ? "STARTED" : "STOPPED"; diff --git a/src/bluetooth/bluetooth_le_device.cc b/src/bluetooth/bluetooth_le_device.cc index dcb429bb..6d44b0da 100755 --- a/src/bluetooth/bluetooth_le_device.cc +++ b/src/bluetooth/bluetooth_le_device.cc @@ -311,20 +311,26 @@ void BluetoothLEDevice::Connect(const picojson::value& data, bool connected = false; int ret = bt_device_is_profile_connected(address.c_str(), BT_PROFILE_GATT, &connected); if (BT_ERROR_NONE != ret) { - instance_.AsyncResponse(callback_handle, - PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to disconnect.")); + instance_.AsyncResponse( + callback_handle, + LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Failed to disconnect.", + ("bt_device_is_profile_connected error: %d (%s)", ret, get_error_message(ret)))); return; } if (connected) { - instance_.AsyncResponse(callback_handle, - PlatformResult(ErrorCode::NO_ERROR)); + instance_.AsyncResponse( + callback_handle, + PlatformResult(ErrorCode::NO_ERROR)); } else { // not connected yet ret = bt_gatt_connect(address.c_str(), false); if (BT_ERROR_NONE != ret) { instance_.AsyncResponse( callback_handle, - PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to connect.")); + LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Failed to connect.", + ("bt_gatt_connect error: %d (%s)", ret, get_error_message(ret)))); return; } connecting_[address] = callback_handle; @@ -348,13 +354,17 @@ void BluetoothLEDevice::Disconnect(const picojson::value& data, if (BT_ERROR_NONE != ret) { instance_.AsyncResponse( callback_handle, - PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to disconnect.")); + LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Failed to disconnect.", + ("bt_device_is_profile_connected error: %d (%s)", ret, get_error_message(ret)))); return; } if (!connected) { - ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR, - "Bluetooth low energy device is not connected"), - &out); + LogAndReportError( + PlatformResult(ErrorCode::INVALID_STATE_ERR, + "Bluetooth low energy device is not connected"), + &out, + ("bt_device_is_profile_connected error: %d (%s)", ret, get_error_message(ret))); return; } @@ -362,7 +372,9 @@ void BluetoothLEDevice::Disconnect(const picojson::value& data, if (BT_ERROR_NONE != ret) { instance_.AsyncResponse( callback_handle, - PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to disconnect.")); + LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Failed to disconnect.", + ("bt_gatt_disconnect error: %d (%s)", ret, get_error_message(ret)))); return; } @@ -382,8 +394,7 @@ void BluetoothLEDevice::GetService(const picojson::value& data, auto it = is_connected_.find(address); if (it == is_connected_.end()) { - LoggerE("Bluetooth low energy device is not connected"); - ReportError( + LogAndReportError( PlatformResult(ErrorCode::INVALID_STATE_ERR, "Bluetooth low energy device is not connected"), &out); @@ -397,7 +408,7 @@ void BluetoothLEDevice::GetService(const picojson::value& data, data_obj); if (result.IsError()) { - ReportError(result, &out); + LogAndReportError(result, &out); } else { ReportSuccess(response, out); } @@ -436,7 +447,7 @@ void BluetoothLEDevice::GetServiceUuids(const picojson::value& data, if (result) { ReportSuccess(response, out); } else { - ReportError(result, &out); + LogAndReportError(result, &out); } } @@ -486,8 +497,9 @@ void BluetoothLEDevice::GattConnectionState(int result, bool connected, PlatformResult ret = PlatformResult(ErrorCode::NO_ERROR); if (BT_ERROR_NONE != result) { - ret = PlatformResult(ErrorCode::UNKNOWN_ERR, - "Failed to get connection state"); + ret = LogAndCreateResult( + ErrorCode::UNKNOWN_ERR, "Failed to get connection state", + ("GattConnectionState error: %d (%s)", result, get_error_message(result))); } le_device->instance_.AsyncResponse(it->second, ret); diff --git a/src/bluetooth/bluetooth_socket.cc b/src/bluetooth/bluetooth_socket.cc index 8f858bc1..5647db7c 100755 --- a/src/bluetooth/bluetooth_socket.cc +++ b/src/bluetooth/bluetooth_socket.cc @@ -63,8 +63,9 @@ void BluetoothSocket::WriteData(const picojson::value& data, picojson::object& o } if (kBluetoothError == bt_socket_send_data(socket, data_ptr.get(), data_size)) { - LoggerE("bt_socket_send_data() failed"); - ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"), &out); + LogAndReportError( + PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"), &out, + ("bt_socket_send_data() failed")); return; } @@ -99,8 +100,9 @@ void BluetoothSocket::Close(const picojson::value& data, picojson::object& out) int socket = common::stol(FromJson(args, "id")); if (BT_ERROR_NONE != bt_socket_disconnect_rfcomm(socket)) { - LoggerE("bt_socket_disconnect_rfcomm() failed"); - ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"), &out); + LogAndReportError( + PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown error"), &out, + ("bt_socket_disconnect_rfcomm() failed")); return; } diff --git a/src/bluetooth/bluetooth_util.cc b/src/bluetooth/bluetooth_util.cc index 0e4ff882..a449b397 100755 --- a/src/bluetooth/bluetooth_util.cc +++ b/src/bluetooth/bluetooth_util.cc @@ -64,8 +64,11 @@ PlatformResult GetBluetoothError(int error_code, break; } - return PlatformResult(error, - hint + " : " + GetBluetoothErrorMessage(error_code)); + std::string message = hint + " : " + GetBluetoothErrorMessage(error_code); + + return LogAndCreateResult( + error, message.c_str(), + ("%s %d (%s)", message.c_str(), error_code, get_error_message(error_code))); } std::string GetBluetoothErrorMessage(int error_code) {