diff options
author | Petre Eftime <[email protected]> | 2016-05-23 17:42:28 +0300 |
---|---|---|
committer | Petre Eftime <[email protected]> | 2016-05-23 18:17:45 +0300 |
commit | f475d6dbcc549022920090ac11d783f011ffc010 (patch) | |
tree | f32e3139295fe4d0e7031c1b106f8eac12e1b4ea /src | |
parent | b98d8d7ad75d748596ee0246b6f21a74f4b61ef3 (diff) |
c++, java: Add BluetoothException, throw it on DBus or BlueZ error
Signed-off-by: Petre Eftime <[email protected]>
Diffstat (limited to 'src')
-rw-r--r-- | src/BluetoothAdapter.cpp | 10 | ||||
-rw-r--r-- | src/BluetoothDevice.cpp | 21 | ||||
-rw-r--r-- | src/BluetoothGattCharacteristic.cpp | 15 | ||||
-rw-r--r-- | src/BluetoothGattDescriptor.cpp | 9 | ||||
-rw-r--r-- | src/BluetoothGattService.cpp | 3 | ||||
-rw-r--r-- | src/BluetoothManager.cpp | 3 | ||||
-rw-r--r-- | src/tinyb_utils.cpp | 10 |
7 files changed, 36 insertions, 35 deletions
diff --git a/src/BluetoothAdapter.cpp b/src/BluetoothAdapter.cpp index 86f1084e..49f69cb6 100644 --- a/src/BluetoothAdapter.cpp +++ b/src/BluetoothAdapter.cpp @@ -27,6 +27,7 @@ #include "BluetoothAdapter.hpp" #include "BluetoothDevice.hpp" #include "BluetoothManager.hpp" +#include "BluetoothException.hpp" using namespace tinyb; @@ -118,8 +119,7 @@ bool BluetoothAdapter::start_discovery () NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); return result; } @@ -133,8 +133,7 @@ bool BluetoothAdapter::stop_discovery () NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); return result; } @@ -148,8 +147,7 @@ bool BluetoothAdapter::remove_device ( NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); return result; } diff --git a/src/BluetoothDevice.cpp b/src/BluetoothDevice.cpp index 93fc86ed..1b76a849 100644 --- a/src/BluetoothDevice.cpp +++ b/src/BluetoothDevice.cpp @@ -27,6 +27,7 @@ #include "BluetoothDevice.hpp" #include "BluetoothGattService.hpp" #include "BluetoothManager.hpp" +#include "BluetoothException.hpp" using namespace tinyb; @@ -117,8 +118,7 @@ bool BluetoothDevice::disconnect () NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); return result; } @@ -131,8 +131,7 @@ bool BluetoothDevice::connect () NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); return result; } @@ -147,8 +146,7 @@ bool BluetoothDevice::connect_profile ( NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); return result; } @@ -163,8 +161,7 @@ bool BluetoothDevice::disconnect_profile ( NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); return result; } @@ -177,8 +174,7 @@ bool BluetoothDevice::pair () NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); return result; } @@ -191,8 +187,7 @@ bool BluetoothDevice::cancel_pairing () NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); return result; } @@ -314,7 +309,7 @@ BluetoothAdapter BluetoothDevice::get_adapter () std::string error_msg("Error occured while instantiating adapter: "); error_msg += error->message; g_error_free(error); - throw std::runtime_error(error_msg); + throw BluetoothException(error_msg); } return BluetoothAdapter(adapter); diff --git a/src/BluetoothGattCharacteristic.cpp b/src/BluetoothGattCharacteristic.cpp index 9fb4ca96..b8784883 100644 --- a/src/BluetoothGattCharacteristic.cpp +++ b/src/BluetoothGattCharacteristic.cpp @@ -27,6 +27,7 @@ #include "BluetoothGattCharacteristic.hpp" #include "BluetoothGattService.hpp" #include "BluetoothGattDescriptor.hpp" +#include "BluetoothException.hpp" using namespace tinyb; @@ -102,8 +103,7 @@ std::vector<unsigned char> BluetoothGattCharacteristic::read_value () NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); std::vector<unsigned char> result = from_gbytes_to_vector(result_gbytes); @@ -127,8 +127,7 @@ bool BluetoothGattCharacteristic::write_value ( NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); /* freeing the GBytes allocated inside from_vector_to_gbytes function */ g_bytes_unref(arg_value_gbytes); @@ -145,8 +144,7 @@ bool BluetoothGattCharacteristic::start_notify () NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); return result; } @@ -159,8 +157,7 @@ bool BluetoothGattCharacteristic::stop_notify () NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); return result; } @@ -188,7 +185,7 @@ BluetoothGattService BluetoothGattCharacteristic::get_service () std::string error_msg("Error occured while instantiating service: "); error_msg += error->message; g_error_free(error); - throw std::runtime_error(error_msg); + throw BluetoothException(error_msg); } return BluetoothGattService(service); diff --git a/src/BluetoothGattDescriptor.cpp b/src/BluetoothGattDescriptor.cpp index 969097a2..b0c5fd64 100644 --- a/src/BluetoothGattDescriptor.cpp +++ b/src/BluetoothGattDescriptor.cpp @@ -26,6 +26,7 @@ #include "tinyb_utils.hpp" #include "BluetoothGattDescriptor.hpp" #include "BluetoothGattCharacteristic.hpp" +#include "BluetoothException.hpp" using namespace tinyb; @@ -103,8 +104,7 @@ std::vector<unsigned char> BluetoothGattDescriptor::read_value () NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); std::vector<unsigned char> result = from_gbytes_to_vector(result_gbytes); @@ -128,8 +128,7 @@ bool BluetoothGattDescriptor::write_value ( NULL, &error ); - if (error) - g_printerr("Error: %s\n", error->message); + handle_error(error); /* unref the GBytes allocated inside from_vector_to_gbytes function */ g_bytes_unref(arg_value_gbytes); @@ -161,7 +160,7 @@ BluetoothGattCharacteristic BluetoothGattDescriptor::get_characteristic () std::string error_msg("Error occured while instantiating characteristic: "); error_msg += error->message; g_error_free(error); - throw std::runtime_error(error_msg); + throw BluetoothException(error_msg); } return BluetoothGattCharacteristic(characteristic); diff --git a/src/BluetoothGattService.cpp b/src/BluetoothGattService.cpp index 31757ac1..23cb4800 100644 --- a/src/BluetoothGattService.cpp +++ b/src/BluetoothGattService.cpp @@ -27,6 +27,7 @@ #include "BluetoothGattService.hpp" #include "BluetoothGattCharacteristic.hpp" #include "BluetoothDevice.hpp" +#include "BluetoothException.hpp" using namespace tinyb; @@ -113,7 +114,7 @@ BluetoothDevice BluetoothGattService::get_device () std::string error_msg("Error occured while instantiating device: "); error_msg += error->message; g_error_free(error); - throw std::runtime_error(error_msg); + throw BluetoothException(error_msg); } return BluetoothDevice(device); diff --git a/src/BluetoothManager.cpp b/src/BluetoothManager.cpp index 8de8003c..a7459f4a 100644 --- a/src/BluetoothManager.cpp +++ b/src/BluetoothManager.cpp @@ -30,6 +30,7 @@ #include "BluetoothGattCharacteristic.hpp" #include "BluetoothGattDescriptor.hpp" #include "BluetoothEvent.hpp" +#include "BluetoothException.hpp" #include "version.h" #include <pthread.h> @@ -293,7 +294,7 @@ BluetoothManager::BluetoothManager() : event_list() g_list_free_full(objects, g_object_unref); if (default_adapter == nullptr) { - throw std::runtime_error("No adapter installed or not recognized by system"); + throw BluetoothException("No adapter installed or not recognized by system"); } } diff --git a/src/tinyb_utils.cpp b/src/tinyb_utils.cpp index a1f2a5d6..449ef1ed 100644 --- a/src/tinyb_utils.cpp +++ b/src/tinyb_utils.cpp @@ -23,6 +23,7 @@ */ #include "tinyb_utils.hpp" +#include "BluetoothException.hpp" std::vector<unsigned char> tinyb::from_gbytes_to_vector(const GBytes *bytes) { @@ -50,3 +51,12 @@ GBytes *tinyb::from_vector_to_gbytes(const std::vector<unsigned char>& vector) return result; } + +void tinyb::handle_error(GError *error) +{ + if (error != nullptr) { + BluetoothException e(error->message); + g_error_free(error); + throw e; + } +} |