diff options
author | Petre Eftime <[email protected]> | 2016-05-10 16:37:33 +0300 |
---|---|---|
committer | Petre Eftime <[email protected]> | 2016-05-10 16:37:33 +0300 |
commit | 44ea9ff1da0e10a7e0e8c14167f0fc7e8937bd3a (patch) | |
tree | 5b57dcc022a580241f81cfb7f75e896af50a5fb8 | |
parent | c07b823b88c35b2e93ca2588afa119c31faf17fe (diff) |
java: throw exceptions from JNI to Javav0.4.2
Signed-off-by: Petre Eftime <[email protected]>
-rw-r--r-- | java/jni/BluetoothAdapter.cxx | 465 | ||||
-rw-r--r-- | java/jni/BluetoothDevice.cxx | 546 | ||||
-rw-r--r-- | java/jni/BluetoothGattCharacteristic.cxx | 319 | ||||
-rw-r--r-- | java/jni/BluetoothGattDescriptor.cxx | 191 | ||||
-rw-r--r-- | java/jni/BluetoothGattService.cxx | 138 | ||||
-rw-r--r-- | java/jni/BluetoothManager.cxx | 444 | ||||
-rw-r--r-- | java/jni/helper.cxx | 29 | ||||
-rw-r--r-- | java/jni/helper.hpp | 5 | ||||
-rw-r--r-- | src/BluetoothDevice.cpp | 8 | ||||
-rw-r--r-- | src/BluetoothGattCharacteristic.cpp | 17 | ||||
-rw-r--r-- | src/BluetoothGattDescriptor.cpp | 14 | ||||
-rw-r--r-- | src/BluetoothGattService.cpp | 8 | ||||
-rw-r--r-- | src/tinyb_utils.cpp | 10 |
13 files changed, 1626 insertions, 568 deletions
diff --git a/java/jni/BluetoothAdapter.cxx b/java/jni/BluetoothAdapter.cxx index c7c38589..ee7aab9b 100644 --- a/java/jni/BluetoothAdapter.cxx +++ b/java/jni/BluetoothAdapter.cxx @@ -34,204 +34,461 @@ using namespace tinyb; jobject Java_tinyb_BluetoothAdapter_getBluetoothType(JNIEnv *env, jobject obj) { - (void)obj; - - return get_bluetooth_type(env, "ADAPTER"); + try { + (void)obj; + + return get_bluetooth_type(env, "ADAPTER"); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jobject Java_tinyb_BluetoothAdapter_clone(JNIEnv *env, jobject obj) { - return generic_clone<BluetoothAdapter>(env, obj); + try { + return generic_clone<BluetoothAdapter>(env, obj); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jboolean Java_tinyb_BluetoothAdapter_startDiscovery(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - return obj_adapter->start_discovery() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return obj_adapter->start_discovery() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jboolean Java_tinyb_BluetoothAdapter_stopDiscovery(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - return obj_adapter->stop_discovery() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return obj_adapter->stop_discovery() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jobject Java_tinyb_BluetoothAdapter_getDevices(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - std::vector<std::unique_ptr<BluetoothDevice>> array = obj_adapter->get_devices(); - jobject result = convert_vector_to_jobject<BluetoothDevice>(env, array, - "(J)V"); - - return result; + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + std::vector<std::unique_ptr<BluetoothDevice>> array = obj_adapter->get_devices(); + jobject result = convert_vector_to_jobject<BluetoothDevice>(env, array, + "(J)V"); + + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jstring Java_tinyb_BluetoothAdapter_getAddress(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - std::string address = obj_adapter->get_address(); - - return env->NewStringUTF((const char *)address.c_str()); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + std::string address = obj_adapter->get_address(); + + return env->NewStringUTF((const char *)address.c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jstring Java_tinyb_BluetoothAdapter_getName(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - std::string name = obj_adapter->get_name(); - - return env->NewStringUTF((const char *)name.c_str()); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + std::string name = obj_adapter->get_name(); + + return env->NewStringUTF((const char *)name.c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jstring Java_tinyb_BluetoothAdapter_getAlias(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - std::string alias = obj_adapter->get_alias(); - - return env->NewStringUTF((const char *)alias.c_str()); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + std::string alias = obj_adapter->get_alias(); + + return env->NewStringUTF((const char *)alias.c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } void Java_tinyb_BluetoothAdapter_setAlias(JNIEnv *env, jobject obj, jstring str) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - const std::string string_to_write = from_jstring_to_string(env, str); - - obj_adapter->set_alias(string_to_write); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + const std::string string_to_write = from_jstring_to_string(env, str); + + obj_adapter->set_alias(string_to_write); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } jlong Java_tinyb_BluetoothAdapter_getBluetoothClass(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - return (jlong)obj_adapter->get_class(); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return (jlong)obj_adapter->get_class(); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return 0; } jboolean Java_tinyb_BluetoothAdapter_getPowered(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - return obj_adapter->get_powered() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return obj_adapter->get_powered() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } void Java_tinyb_BluetoothAdapter_setPowered(JNIEnv *env, jobject obj, jboolean val) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - bool val_to_write = from_jboolean_to_bool(val); - obj_adapter->set_powered(val_to_write); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + bool val_to_write = from_jboolean_to_bool(val); + obj_adapter->set_powered(val_to_write); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } jboolean Java_tinyb_BluetoothAdapter_getDiscoverable(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - return obj_adapter->get_discoverable() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return obj_adapter->get_discoverable() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } void Java_tinyb_BluetoothAdapter_setDiscoverable(JNIEnv *env, jobject obj, jboolean val) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - bool val_to_write = from_jboolean_to_bool(val); - obj_adapter->set_discoverable(val_to_write); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + bool val_to_write = from_jboolean_to_bool(val); + obj_adapter->set_discoverable(val_to_write); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } jlong Java_tinyb_BluetoothAdapter_getDiscoverableTimeout(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - return (jlong)obj_adapter->get_discoverable_timeout(); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return (jlong)obj_adapter->get_discoverable_timeout(); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return 0; } void Java_tinyb_BluetoothAdapter_setDiscoverableTimout(JNIEnv *env, jobject obj, jlong timeout) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - if (timeout < 0) - { - throw std::invalid_argument("timeout argument is negative\n"); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + if (timeout < 0) + { + throw std::invalid_argument("timeout argument is negative\n"); + } + obj_adapter->set_discoverable_timeout((unsigned int)timeout); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } - obj_adapter->set_discoverable_timeout((unsigned int)timeout); } jboolean Java_tinyb_BluetoothAdapter_getPairable(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - return obj_adapter->get_pairable() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return obj_adapter->get_pairable() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } void Java_tinyb_BluetoothAdapter_setPairable(JNIEnv *env, jobject obj, jboolean val) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - bool val_to_write = from_jboolean_to_bool(val); - obj_adapter->set_pairable(val_to_write); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + bool val_to_write = from_jboolean_to_bool(val); + obj_adapter->set_pairable(val_to_write); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } jlong Java_tinyb_BluetoothAdapter_getPairableTimeout(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - return (jlong)obj_adapter->get_pairable_timeout(); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return (jlong)obj_adapter->get_pairable_timeout(); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return 0; } void Java_tinyb_BluetoothAdapter_setPairableTimeout(JNIEnv *env, jobject obj, jlong timeout) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - if (timeout < 0) - { - throw std::invalid_argument("timeout argument is negative\n"); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + if (timeout < 0) + { + throw std::invalid_argument("timeout argument is negative\n"); + } + obj_adapter->set_pairable_timeout((unsigned int)timeout); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } - obj_adapter->set_pairable_timeout((unsigned int)timeout); } jboolean Java_tinyb_BluetoothAdapter_getDiscovering(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - - return obj_adapter->get_discovering() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return obj_adapter->get_discovering() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jobjectArray Java_tinyb_BluetoothAdapter_getUuids(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - std::vector<std::string> uuids = obj_adapter->get_uuids(); - unsigned int uuids_size = uuids.size(); - - jclass string_class = search_class(env, "Ljava/lang/String;"); - jobjectArray result = env->NewObjectArray(uuids_size, string_class, 0); - if (!result) - { - throw std::runtime_error("NewObjectArray cannot create instance\n"); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + std::vector<std::string> uuids = obj_adapter->get_uuids(); + unsigned int uuids_size = uuids.size(); + + jclass string_class = search_class(env, "Ljava/lang/String;"); + jobjectArray result = env->NewObjectArray(uuids_size, string_class, 0); + if (!result) + { + throw std::bad_alloc(); + } + + for (unsigned int i = 0; i < uuids_size; ++i) + { + std::string str_elem = uuids.at(i); + jobject elem = env->NewStringUTF((const char *)str_elem.c_str()); + env->SetObjectArrayElement(result, i, elem); + } + + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } - - for (unsigned int i = 0; i < uuids_size; ++i) - { - std::string str_elem = uuids.at(i); - jobject elem = env->NewStringUTF((const char *)str_elem.c_str()); - env->SetObjectArrayElement(result, i, elem); - } - - return result; + return nullptr; } jstring Java_tinyb_BluetoothAdapter_getModalias(JNIEnv *env, jobject obj) { - BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - std::unique_ptr<std::string> modalias = obj_adapter->get_modalias(); - if(modalias == nullptr) - return nullptr; - - return env->NewStringUTF((const char *)modalias->c_str()); + try { + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + std::unique_ptr<std::string> modalias = obj_adapter->get_modalias(); + if(modalias == nullptr) + return nullptr; + + return env->NewStringUTF((const char *)modalias->c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } void Java_tinyb_BluetoothAdapter_delete(JNIEnv *env, jobject obj) { - BluetoothAdapter *adapter = getInstance<BluetoothAdapter>(env, obj); - delete adapter; + try { + BluetoothAdapter *adapter = getInstance<BluetoothAdapter>(env, obj); + delete adapter; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } diff --git a/java/jni/BluetoothDevice.cxx b/java/jni/BluetoothDevice.cxx index d0e70007..750ac5e2 100644 --- a/java/jni/BluetoothDevice.cxx +++ b/java/jni/BluetoothDevice.cxx @@ -35,236 +35,540 @@ using namespace tinyb; jobject Java_tinyb_BluetoothAdapter_getBluetoothType(JNIEnv *env, jobject obj) { - (void)obj; - - return get_bluetooth_type(env, "DEVICE"); + try { + (void)obj; + + return get_bluetooth_type(env, "DEVICE"); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jobject Java_tinyb_BluetoothDevice_clone(JNIEnv *env, jobject obj) { - return generic_clone<BluetoothDevice>(env, obj); + try { + return generic_clone<BluetoothDevice>(env, obj); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jboolean Java_tinyb_BluetoothDevice_disconnect(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - return obj_device->disconnect() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->disconnect() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jboolean Java_tinyb_BluetoothDevice_connect(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - return obj_device->connect() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->connect() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jboolean Java_tinyb_BluetoothDevice_connectProfile(JNIEnv *env, jobject obj, jstring str) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - const std::string string_to_write = from_jstring_to_string(env, str); - - return obj_device->connect_profile(string_to_write) ? JNI_TRUE : JNI_FALSE; + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + const std::string string_to_write = from_jstring_to_string(env, str); + + return obj_device->connect_profile(string_to_write) ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jboolean Java_tinyb_BluetoothDevice_disconnectProfile(JNIEnv *env, jobject obj, jstring str) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - const std::string string_to_write = from_jstring_to_string(env, str); - - return obj_device->disconnect_profile(string_to_write) ? JNI_TRUE : JNI_FALSE; + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + const std::string string_to_write = from_jstring_to_string(env, str); + + return obj_device->disconnect_profile(string_to_write) ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jboolean Java_tinyb_BluetoothDevice_pair(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - return obj_device->pair() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->pair() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jboolean Java_tinyb_BluetoothDevice_cancelPairing(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - return obj_device->cancel_pairing() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->cancel_pairing() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jobject Java_tinyb_BluetoothDevice_getServices(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - std::vector<std::unique_ptr<BluetoothGattService>> array = obj_device->get_services(); - jobject result = convert_vector_to_jobject<BluetoothGattService>(env, array, - "(J)V"); - - return result; + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::vector<std::unique_ptr<BluetoothGattService>> array = obj_device->get_services(); + jobject result = convert_vector_to_jobject<BluetoothGattService>(env, array, + "(J)V"); + + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jstring Java_tinyb_BluetoothDevice_getAddress(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - std::string address = obj_device->get_address(); - - return env->NewStringUTF((const char *)address.c_str()); + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::string address = obj_device->get_address(); + + return env->NewStringUTF((const char *)address.c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jstring Java_tinyb_BluetoothDevice_getName(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - std::string name = obj_device->get_name(); - - return env->NewStringUTF((const char *)name.c_str()); + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::string name = obj_device->get_name(); + + return env->NewStringUTF((const char *)name.c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jstring Java_tinyb_BluetoothDevice_getAlias(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - std::string alias = obj_device->get_alias(); - - return env->NewStringUTF((const char *)alias.c_str()); + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::string alias = obj_device->get_alias(); + + return env->NewStringUTF((const char *)alias.c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } void Java_tinyb_BluetoothDevice_setAlias(JNIEnv *env, jobject obj, jstring str) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - const std::string string_to_write = from_jstring_to_string(env, str); - - obj_device->set_alias(string_to_write); + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + const std::string string_to_write = from_jstring_to_string(env, str); + + obj_device->set_alias(string_to_write); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } jint Java_tinyb_BluetoothDevice_getBluetoothClass(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - return (jlong)obj_device->get_class(); + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return (jlong)obj_device->get_class(); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return 0; } jshort Java_tinyb_BluetoothDevice_getAppearance(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - return (jshort)obj_device->get_appearance(); + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return (jshort)obj_device->get_appearance(); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return 0; } jstring Java_tinyb_BluetoothDevice_getIcon(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - std::unique_ptr<std::string> icon = obj_device->get_icon(); - if (icon == nullptr) - return nullptr; - - return env->NewStringUTF((const char *)icon->c_str()); + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::unique_ptr<std::string> icon = obj_device->get_icon(); + if (icon == nullptr) + return nullptr; + + return env->NewStringUTF((const char *)icon->c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jboolean Java_tinyb_BluetoothDevice_getPaired(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - return obj_device->get_paired() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->get_paired() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jboolean Java_tinyb_BluetoothDevice_getTrusted(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - return obj_device->get_trusted() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->get_trusted() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } void Java_tinyb_BluetoothDevice_setTrusted(JNIEnv *env, jobject obj, jboolean val) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - bool val_to_write = from_jboolean_to_bool(val); - obj_device->set_trusted(val_to_write); + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + bool val_to_write = from_jboolean_to_bool(val); + obj_device->set_trusted(val_to_write); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } jboolean Java_tinyb_BluetoothDevice_getBlocked(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - return obj_device->get_blocked() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->get_blocked() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } void Java_tinyb_BluetoothDevice_setBlocked(JNIEnv *env, jobject obj, jboolean val) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - bool val_to_write = from_jboolean_to_bool(val); - obj_device->set_blocked(val_to_write); + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + bool val_to_write = from_jboolean_to_bool(val); + obj_device->set_blocked(val_to_write); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } jboolean Java_tinyb_BluetoothDevice_getLegacyPairing(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - return obj_device->get_legacy_pairing() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->get_legacy_pairing() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jshort Java_tinyb_BluetoothDevice_getRssi(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - return (jshort)obj_device->get_rssi(); + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return (jshort)obj_device->get_rssi(); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return 0; } jboolean Java_tinyb_BluetoothDevice_getConnected(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - - return obj_device->get_connected() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->get_connected() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jobjectArray Java_tinyb_BluetoothDevice_getUuids(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - std::vector<std::string> uuids = obj_device->get_uuids(); - unsigned int uuids_size = uuids.size(); - - jclass string_class = search_class(env, "Ljava/lang/String;"); - jobjectArray result = env->NewObjectArray(uuids_size, string_class, 0); - - for (unsigned int i = 0; i < uuids_size; ++i) - { - std::string str_elem = uuids.at(i); - jobject elem = env->NewStringUTF((const char *)str_elem.c_str()); - env->SetObjectArrayElement(result, i, elem); + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::vector<std::string> uuids = obj_device->get_uuids(); + unsigned int uuids_size = uuids.size(); + + jclass string_class = search_class(env, "Ljava/lang/String;"); + jobjectArray result = env->NewObjectArray(uuids_size, string_class, 0); + + for (unsigned int i = 0; i < uuids_size; ++i) + { + std::string str_elem = uuids.at(i); + jobject elem = env->NewStringUTF((const char *)str_elem.c_str()); + env->SetObjectArrayElement(result, i, elem); + } + + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } - - return result; + return nullptr; } jstring Java_tinyb_BluetoothDevice_getModalias(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - std::unique_ptr<std::string> modalias = obj_device->get_modalias(); - if (modalias == nullptr) - return nullptr; - - return env->NewStringUTF((const char *)modalias->c_str()); + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::unique_ptr<std::string> modalias = obj_device->get_modalias(); + if (modalias == nullptr) + return nullptr; + + return env->NewStringUTF((const char *)modalias->c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jobject Java_tinyb_BluetoothDevice_getAdapter(JNIEnv *env, jobject obj) { - BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - BluetoothAdapter *obj_adapter = obj_device->get_adapter().clone(); - - jclass b_adapter_class = search_class(env, *obj_adapter); - jmethodID b_adapter_ctor = search_method(env, b_adapter_class, "<init>", - "(J)V", false); - jobject result = env->NewObject(b_adapter_class, b_adapter_ctor, (jlong)obj_adapter); - if (!result) - { - throw std::runtime_error("cannot create instance of class\n"); + try { + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + BluetoothAdapter *obj_adapter = obj_device->get_adapter().clone(); + + jclass b_adapter_class = search_class(env, *obj_adapter); + jmethodID b_adapter_ctor = search_method(env, b_adapter_class, "<init>", + "(J)V", false); + jobject result = env->NewObject(b_adapter_class, b_adapter_ctor, (jlong)obj_adapter); + if (!result) + { + throw std::bad_alloc(); + } + + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } - - return result; + return nullptr; } void Java_tinyb_BluetoothDevice_delete(JNIEnv *env, jobject obj) { - BluetoothDevice *b_device = getInstance<BluetoothDevice>(env, obj); - delete b_device; + try { + BluetoothDevice *b_device = getInstance<BluetoothDevice>(env, obj); + delete b_device; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } diff --git a/java/jni/BluetoothGattCharacteristic.cxx b/java/jni/BluetoothGattCharacteristic.cxx index 61cceda4..aa2770a5 100644 --- a/java/jni/BluetoothGattCharacteristic.cxx +++ b/java/jni/BluetoothGattCharacteristic.cxx @@ -35,144 +35,287 @@ using namespace tinyb; jobject Java_tinyb_BluetoothGattCharacteristic_getBluetoothType(JNIEnv *env, jobject obj) { - (void)obj; - - return get_bluetooth_type(env, "GATT_CHARACTERISTIC"); + try { + (void)obj; + + return get_bluetooth_type(env, "GATT_CHARACTERISTIC"); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jobject Java_tinyb_BluetoothGattCharacteristic_clone(JNIEnv *env, jobject obj) { - return generic_clone<BluetoothGattCharacteristic>(env, obj); + try { + return generic_clone<BluetoothGattCharacteristic>(env, obj); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jbyteArray Java_tinyb_BluetoothGattCharacteristic_readValue(JNIEnv *env, jobject obj) { - BluetoothGattCharacteristic *obj_gatt_char = - getInstance<BluetoothGattCharacteristic>(env, obj); - std::vector<unsigned char> array = obj_gatt_char->read_value(); - unsigned int array_size = array.size(); - - jbyteArray result = env->NewByteArray((jsize)array_size); - env->SetByteArrayRegion(result, 0, (jsize)array_size, (const jbyte *)&array[0]); - - return result; + try { + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + std::vector<unsigned char> array = obj_gatt_char->read_value(); + unsigned int array_size = array.size(); + + jbyteArray result = env->NewByteArray((jsize)array_size); + env->SetByteArrayRegion(result, 0, (jsize)array_size, (const jbyte *)&array[0]); + + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jboolean Java_tinyb_BluetoothGattCharacteristic_writeValue(JNIEnv *env, jobject obj, jbyteArray argValue) { - if (!argValue) - { - throw std::invalid_argument("byte array argument is null\n"); + try { + if (!argValue) + { + throw std::invalid_argument("byte array is null"); + return JNI_FALSE; + } + + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + + jboolean is_copy = false; + jbyte *native_array = env->GetByteArrayElements(argValue, &is_copy); + jsize native_array_length = env->GetArrayLength(argValue); + + std::vector<unsigned char> array(native_array, native_array + native_array_length); + + return obj_gatt_char->write_value(array) ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } - - BluetoothGattCharacteristic *obj_gatt_char = - getInstance<BluetoothGattCharacteristic>(env, obj); - - jboolean is_copy = false; - jbyte *native_array = env->GetByteArrayElements(argValue, &is_copy); - jsize native_array_length = env->GetArrayLength(argValue); - - std::vector<unsigned char> array(native_array, native_array + native_array_length); - - return obj_gatt_char->write_value(array) ? JNI_TRUE : JNI_FALSE; + return JNI_FALSE; } jboolean Java_tinyb_BluetoothGattCharacteristic_startNotify(JNIEnv *env, jobject obj) { - BluetoothGattCharacteristic *obj_gatt_char = - getInstance<BluetoothGattCharacteristic>(env, obj); - return obj_gatt_char->start_notify() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + return obj_gatt_char->start_notify() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jboolean Java_tinyb_BluetoothGattCharacteristic_stopNotify(JNIEnv *env, jobject obj) { - BluetoothGattCharacteristic *obj_gatt_char = - getInstance<BluetoothGattCharacteristic>(env, obj); - return obj_gatt_char->stop_notify() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + return obj_gatt_char->stop_notify() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jstring Java_tinyb_BluetoothGattCharacteristic_getUuid(JNIEnv *env, jobject obj) { - BluetoothGattCharacteristic *obj_gatt_char = - getInstance<BluetoothGattCharacteristic>(env, obj); - std::string uuid = obj_gatt_char->get_uuid(); - - return env->NewStringUTF((const char *)uuid.c_str()); + try { + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + std::string uuid = obj_gatt_char->get_uuid(); + + return env->NewStringUTF((const char *)uuid.c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jobject Java_tinyb_BluetoothGattCharacteristic_getService(JNIEnv *env, jobject obj) { - BluetoothGattCharacteristic *obj_gatt_char = - getInstance<BluetoothGattCharacteristic>(env, obj); - BluetoothGattService *obj_gatt_serv = obj_gatt_char->get_service().clone(); - - jclass b_gatt_serv_class = search_class(env, *obj_gatt_serv); - jmethodID b_gatt_serv_ctor = search_method(env, b_gatt_serv_class, "<init>", - "(J)V", false); - jobject result = env->NewObject(b_gatt_serv_class, b_gatt_serv_ctor, (jlong)obj_gatt_serv); - if (result == NULL) - { - throw std::runtime_error("cannot create instance of class\n"); + try { + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + BluetoothGattService *obj_gatt_serv = obj_gatt_char->get_service().clone(); + + jclass b_gatt_serv_class = search_class(env, *obj_gatt_serv); + jmethodID b_gatt_serv_ctor = search_method(env, b_gatt_serv_class, "<init>", + "(J)V", false); + jobject result = env->NewObject(b_gatt_serv_class, b_gatt_serv_ctor, (jlong)obj_gatt_serv); + if (result == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } - - return result; + return nullptr; } jbyteArray Java_tinyb_BluetoothGattCharacteristic_getValue(JNIEnv *env, jobject obj) { - BluetoothGattCharacteristic *obj_gatt_char = - getInstance<BluetoothGattCharacteristic>(env, obj); - std::vector<unsigned char> array = obj_gatt_char->get_value(); - unsigned int array_size = array.size(); - - jbyteArray result = env->NewByteArray((jsize)array_size); - env->SetByteArrayRegion(result, 0, (jsize)array_size, (const jbyte *)&array[0]); - - return result; + try { + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + std::vector<unsigned char> array = obj_gatt_char->get_value(); + unsigned int array_size = array.size(); + + jbyteArray result = env->NewByteArray((jsize)array_size); + env->SetByteArrayRegion(result, 0, (jsize)array_size, (const jbyte *)&array[0]); + + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jboolean Java_tinyb_BluetoothGattCharacteristic_getNotifying(JNIEnv *env, jobject obj) { - BluetoothGattCharacteristic *obj_gatt_char = - getInstance<BluetoothGattCharacteristic>(env, obj); - return obj_gatt_char->get_notifying() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + return obj_gatt_char->get_notifying() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jobjectArray Java_tinyb_BluetoothGattCharacteristic_getFlags(JNIEnv *env, jobject obj) { - BluetoothGattCharacteristic *obj_gatt_char = - getInstance<BluetoothGattCharacteristic>(env, obj); - std::vector<std::string> flags = obj_gatt_char->get_flags(); - unsigned int flags_size = flags.size(); - - jclass string_class = search_class(env, "Ljava/lang/String;"); - jobjectArray result = env->NewObjectArray(flags_size, string_class, 0); - - for (unsigned int i = 0; i < flags_size; ++i) - { - std::string str_elem = flags.at(i); - jobject elem = env->NewStringUTF((const char *)str_elem.c_str()); - env->SetObjectArrayElement(result, i, elem); + try { + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + std::vector<std::string> flags = obj_gatt_char->get_flags(); + unsigned int flags_size = flags.size(); + + jclass string_class = search_class(env, "Ljava/lang/String;"); + jobjectArray result = env->NewObjectArray(flags_size, string_class, 0); + + for (unsigned int i = 0; i < flags_size; ++i) + { + std::string str_elem = flags.at(i); + jobject elem = env->NewStringUTF((const char *)str_elem.c_str()); + env->SetObjectArrayElement(result, i, elem); + } + + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } - - return result; + return nullptr; } jobject Java_tinyb_BluetoothGattCharacteristic_getDescriptors(JNIEnv *env, jobject obj) { - BluetoothGattCharacteristic *obj_gatt_char = - getInstance<BluetoothGattCharacteristic>(env, obj); - std::vector<std::unique_ptr<BluetoothGattDescriptor>> array = obj_gatt_char->get_descriptors(); - - jobject result = convert_vector_to_jobject<BluetoothGattDescriptor>(env, array, - "(J)V"); - return result; + try { + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + std::vector<std::unique_ptr<BluetoothGattDescriptor>> array = obj_gatt_char->get_descriptors(); + + jobject result = convert_vector_to_jobject<BluetoothGattDescriptor>(env, array, + "(J)V"); + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } void Java_tinyb_BluetoothGattCharacteristic_delete(JNIEnv *env, jobject obj) { - BluetoothGattCharacteristic *obj_gatt_char = - getInstance<BluetoothGattCharacteristic>(env, obj); - delete obj_gatt_char; + try { + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + delete obj_gatt_char; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } diff --git a/java/jni/BluetoothGattDescriptor.cxx b/java/jni/BluetoothGattDescriptor.cxx index 02b04dbe..e8288ffd 100644 --- a/java/jni/BluetoothGattDescriptor.cxx +++ b/java/jni/BluetoothGattDescriptor.cxx @@ -34,87 +34,174 @@ using namespace tinyb; jobject Java_tinyb_BluetoothGattDescriptor_getBluetoothType(JNIEnv *env, jobject obj) { - (void)obj; - - return get_bluetooth_type(env, "GATT_DESCRIPTOR"); + try { + (void)obj; + + return get_bluetooth_type(env, "GATT_DESCRIPTOR"); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jobject Java_tinyb_BluetoothGattDescriptor_clone(JNIEnv *env, jobject obj) { - return generic_clone<BluetoothGattDescriptor>(env, obj); + try { + return generic_clone<BluetoothGattDescriptor>(env, obj); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jbyteArray Java_tinyb_BluetoothGattDescriptor_readValue(JNIEnv *env, jobject obj) { - BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); - std::vector<unsigned char> array = obj_gatt_desc->read_value(); - unsigned int array_size = array.size(); - - jbyteArray result = env->NewByteArray((jsize)array_size); - env->SetByteArrayRegion(result, 0, (jsize)array_size, (const jbyte *)&array[0]); - - return result; + try { + BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); + std::vector<unsigned char> array = obj_gatt_desc->read_value(); + unsigned int array_size = array.size(); + + jbyteArray result = env->NewByteArray((jsize)array_size); + env->SetByteArrayRegion(result, 0, (jsize)array_size, (const jbyte *)&array[0]); + + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jboolean Java_tinyb_BluetoothGattDescriptor_writeValue(JNIEnv *env, jobject obj, jbyteArray argValue) { - if (!argValue) - { - throw std::invalid_argument("byte array argument is null\n"); + try { + if (!argValue) + { + throw std::invalid_argument("byte array is null"); + } + + BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); + + jboolean is_copy = false; + jbyte *native_array = env->GetByteArrayElements(argValue, &is_copy); + jsize native_array_length = env->GetArrayLength(argValue); + + std::vector<unsigned char> array(native_array, native_array + native_array_length); + + return obj_gatt_desc->write_value(array) ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } - - BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); - - jboolean is_copy = false; - jbyte *native_array = env->GetByteArrayElements(argValue, &is_copy); - jsize native_array_length = env->GetArrayLength(argValue); - - std::vector<unsigned char> array(native_array, native_array + native_array_length); - - return obj_gatt_desc->write_value(array) ? JNI_TRUE : JNI_FALSE; + return JNI_FALSE; } jstring Java_tinyb_BluetoothGattDescriptor_getUuid(JNIEnv *env, jobject obj) { - BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); - std::string uuid = obj_gatt_desc->get_uuid(); - - return env->NewStringUTF((const char *)uuid.c_str()); + try { + BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); + std::string uuid = obj_gatt_desc->get_uuid(); + + return env->NewStringUTF((const char *)uuid.c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jobject Java_tinyb_BluetoothGattDescriptor_getCharacteristic(JNIEnv *env, jobject obj) { - BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); - BluetoothGattCharacteristic *obj_gatt_char = obj_gatt_desc->get_characteristic().clone(); - - jclass b_gatt_char_class = search_class(env, *obj_gatt_char); - jmethodID b_gatt_char_ctor = search_method(env, b_gatt_char_class, "<init>", - "(J)V", false); - jobject result = env->NewObject(b_gatt_char_class, b_gatt_char_ctor, (jlong)obj_gatt_char); - if (result == NULL) - { - throw std::runtime_error("cannot create instance of class\n"); + try { + BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); + BluetoothGattCharacteristic *obj_gatt_char = obj_gatt_desc->get_characteristic().clone(); + + jclass b_gatt_char_class = search_class(env, *obj_gatt_char); + jmethodID b_gatt_char_ctor = search_method(env, b_gatt_char_class, "<init>", + "(J)V", false); + jobject result = env->NewObject(b_gatt_char_class, b_gatt_char_ctor, (jlong)obj_gatt_char); + if (result == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } - - return result; + return nullptr; } jbyteArray Java_tinyb_BluetoothGattDescriptor_getValue(JNIEnv *env, jobject obj) { - BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); - std::vector<unsigned char> array = obj_gatt_desc->get_value(); - unsigned int array_size = array.size(); - - jbyteArray result = env->NewByteArray((jsize)array_size); - env->SetByteArrayRegion(result, 0, (jsize)array_size, (const jbyte *)&array[0]); - - return result; - + try { + BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); + std::vector<unsigned char> array = obj_gatt_desc->get_value(); + unsigned int array_size = array.size(); + + jbyteArray result = env->NewByteArray((jsize)array_size); + env->SetByteArrayRegion(result, 0, (jsize)array_size, (const jbyte *)&array[0]); + + return result; + + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } void Java_tinyb_BluetoothGattDescriptor_delete(JNIEnv *env, jobject obj) { - BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); - delete obj_gatt_desc; + try { + BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); + delete obj_gatt_desc; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } diff --git a/java/jni/BluetoothGattService.cxx b/java/jni/BluetoothGattService.cxx index e7107b51..c7f149ef 100644 --- a/java/jni/BluetoothGattService.cxx +++ b/java/jni/BluetoothGattService.cxx @@ -35,61 +35,137 @@ using namespace tinyb; jobject Java_tinyb_BluetoothGattService_getBluetoothType(JNIEnv *env, jobject obj) { - (void)obj; - - return get_bluetooth_type(env, "GATT_SERVICE"); + try { + (void)obj; + + return get_bluetooth_type(env, "GATT_SERVICE"); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jobject Java_tinyb_BluetoothGattService_clone(JNIEnv *env, jobject obj) { - return generic_clone<BluetoothGattService>(env, obj); + try { + return generic_clone<BluetoothGattService>(env, obj); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jstring Java_tinyb_BluetoothGattService_getUuid(JNIEnv *env, jobject obj) { - BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); - std::string uuid = obj_gatt_serv->get_uuid(); - - return env->NewStringUTF((const char *)uuid.c_str()); + try { + BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); + std::string uuid = obj_gatt_serv->get_uuid(); + + return env->NewStringUTF((const char *)uuid.c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jobject Java_tinyb_BluetoothGattService_getDevice(JNIEnv *env, jobject obj) { - BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); - BluetoothDevice *obj_device = obj_gatt_serv->get_device().clone(); - - jclass b_device_class = search_class(env, *obj_device); - jmethodID b_device_ctor = search_method(env, b_device_class, "<init>", - "(J)V", false); - jobject result = env->NewObject(b_device_class, b_device_ctor, (jlong)obj_device); - if (result == NULL) - { - throw std::runtime_error("cannot create instance of class\n"); + try { + BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); + BluetoothDevice *obj_device = obj_gatt_serv->get_device().clone(); + + jclass b_device_class = search_class(env, *obj_device); + jmethodID b_device_ctor = search_method(env, b_device_class, "<init>", + "(J)V", false); + jobject result = env->NewObject(b_device_class, b_device_ctor, (jlong)obj_device); + if (result == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } - - return result; + return nullptr; } jboolean Java_tinyb_BluetoothGattService_getPrimary(JNIEnv *env, jobject obj) { - BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); - - return obj_gatt_serv->get_primary() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); + + return obj_gatt_serv->get_primary() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jobject Java_tinyb_BluetoothGattService_getCharacteristics(JNIEnv *env, jobject obj) { - BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); - std::vector<std::unique_ptr<BluetoothGattCharacteristic>> array = - obj_gatt_serv->get_characteristics(); - jobject result = convert_vector_to_jobject<BluetoothGattCharacteristic>(env, array, - "(J)V"); - return result; + try { + BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); + std::vector<std::unique_ptr<BluetoothGattCharacteristic>> array = + obj_gatt_serv->get_characteristics(); + jobject result = convert_vector_to_jobject<BluetoothGattCharacteristic>(env, array, + "(J)V"); + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } void Java_tinyb_BluetoothGattService_delete(JNIEnv *env, jobject obj) { - BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); - delete obj_gatt_serv; + try { + BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); + delete obj_gatt_serv; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } diff --git a/java/jni/BluetoothManager.cxx b/java/jni/BluetoothManager.cxx index 568a302c..311d483b 100644 --- a/java/jni/BluetoothManager.cxx +++ b/java/jni/BluetoothManager.cxx @@ -35,41 +35,62 @@ using namespace tinyb; jobject Java_tinyb_BluetoothManager_getBluetoothType(JNIEnv *env, jobject obj) { - (void)obj; - - return get_bluetooth_type(env, "NONE"); + try { + (void)obj; + + return get_bluetooth_type(env, "NONE"); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } static void getObject_setter(JNIEnv *env, - jstring name, std::string **name_to_write, - jstring identifier, std::string **identifier_to_write, - jobject parent, BluetoothObject **b_parent) + jstring name, std::string **name_to_write, + jstring identifier, std::string **identifier_to_write, + jobject parent, BluetoothObject **b_parent) { - if (!parent) - { - *b_parent = nullptr; - } - else - { - *b_parent = getInstance<BluetoothObject>(env, parent); - } - - if (!name) - { - *name_to_write = nullptr; - } - else - { - *name_to_write = new std::string(from_jstring_to_string(env, name)); - } - - if (!identifier) - { - *identifier_to_write = nullptr; - } - else - { - *identifier_to_write = new std::string(from_jstring_to_string(env, identifier)); + try { + if (!parent) + { + *b_parent = nullptr; + } + else + { + *b_parent = getInstance<BluetoothObject>(env, parent); + } + + if (!name) + { + *name_to_write = nullptr; + } + else + { + *name_to_write = new std::string(from_jstring_to_string(env, name)); + } + + if (!identifier) + { + *identifier_to_write = nullptr; + } + else + { + *identifier_to_write = new std::string(from_jstring_to_string(env, identifier)); + } + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } } @@ -83,168 +104,297 @@ static void getObject_cleaner(std::string *name_to_write, std::string *identifie } jobject Java_tinyb_BluetoothManager_find(JNIEnv *env, jobject obj, jint type, - jstring name, jstring identifier, jobject parent, - jlong milliseconds) + jstring name, jstring identifier, jobject parent, + jlong milliseconds) { - BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); - BluetoothObject *b_parent; - BluetoothType b_type; - std::string *name_to_write; - std::string *identifier_to_write; - - getObject_setter(env, - name, &name_to_write, - identifier, &identifier_to_write, - parent, &b_parent); - - b_type = from_int_to_btype((int)type); - std::unique_ptr<BluetoothObject> b_object = manager->find(b_type, name_to_write, - identifier_to_write, - b_parent, - std::chrono::milliseconds(milliseconds)); - getObject_cleaner(name_to_write, identifier_to_write); - - BluetoothObject *b_object_naked = b_object.release(); - if (!b_object_naked) - { - return nullptr; + try { + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + BluetoothObject *b_parent; + BluetoothType b_type; + std::string *name_to_write; + std::string *identifier_to_write; + + getObject_setter(env, + name, &name_to_write, + identifier, &identifier_to_write, + parent, &b_parent); + + b_type = from_int_to_btype((int)type); + std::unique_ptr<BluetoothObject> b_object = manager->find(b_type, name_to_write, + identifier_to_write, + b_parent, + std::chrono::milliseconds(milliseconds)); + getObject_cleaner(name_to_write, identifier_to_write); + + BluetoothObject *b_object_naked = b_object.release(); + if (!b_object_naked) + { + return nullptr; + } + jclass clazz = search_class(env, *b_object_naked); + jmethodID clazz_ctor = search_method(env, clazz, "<init>", "(J)V", false); + + jobject result = env->NewObject(clazz, clazz_ctor, (long)b_object_naked); + + return result; + + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } - jclass clazz = search_class(env, *b_object_naked); - jmethodID clazz_ctor = search_method(env, clazz, "<init>", "(J)V", false); - - jobject result = env->NewObject(clazz, clazz_ctor, (long)b_object_naked); - - return result; - + return nullptr; } jobject Java_tinyb_BluetoothManager_getObject(JNIEnv *env, jobject obj, jint type, - jstring name, jstring identifier, jobject parent) + jstring name, jstring identifier, jobject parent) { - BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); - BluetoothObject *b_parent; - BluetoothType b_type; - std::string *name_to_write; - std::string *identifier_to_write; - - getObject_setter(env, - name, &name_to_write, - identifier, &identifier_to_write, - parent, &b_parent); - - b_type = from_int_to_btype((int)type); - std::unique_ptr<BluetoothObject> b_object = manager->get_object(b_type, name_to_write, - identifier_to_write, - b_parent); - getObject_cleaner(name_to_write, identifier_to_write); - - BluetoothObject *b_object_naked = b_object.release(); - if (!b_object_naked) - { - return nullptr; + try { + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + BluetoothObject *b_parent; + BluetoothType b_type; + std::string *name_to_write; + std::string *identifier_to_write; + + getObject_setter(env, + name, &name_to_write, + identifier, &identifier_to_write, + parent, &b_parent); + + b_type = from_int_to_btype((int)type); + std::unique_ptr<BluetoothObject> b_object = manager->get_object(b_type, name_to_write, + identifier_to_write, + b_parent); + getObject_cleaner(name_to_write, identifier_to_write); + + BluetoothObject *b_object_naked = b_object.release(); + if (!b_object_naked) + { + return nullptr; + } + jclass clazz = search_class(env, *b_object_naked); + jmethodID clazz_ctor = search_method(env, clazz, "<init>", "(J)V", false); + + jobject result = env->NewObject(clazz, clazz_ctor, (long)b_object_naked); + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); } - jclass clazz = search_class(env, *b_object_naked); - jmethodID clazz_ctor = search_method(env, clazz, "<init>", "(J)V", false); - - jobject result = env->NewObject(clazz, clazz_ctor, (long)b_object_naked); - return result; + return nullptr; } jobject Java_tinyb_BluetoothManager_getObjects(JNIEnv *env, jobject obj, jint type, - jstring name, jstring identifier, jobject parent) + jstring name, jstring identifier, jobject parent) { - BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); - BluetoothObject *b_parent; - BluetoothType b_type; - std::string *name_to_write; - std::string *identifier_to_write; - - getObject_setter(env, - name, &name_to_write, - identifier, &identifier_to_write, - parent, &b_parent); - - b_type = from_int_to_btype((int)type); - std::vector<std::unique_ptr<BluetoothObject>> array = manager->get_objects(b_type, - name_to_write, - identifier_to_write, - b_parent); - getObject_cleaner(name_to_write, identifier_to_write); - jobject result = convert_vector_to_jobject<BluetoothObject>(env, array, "(J)V"); - return result; + try { + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + BluetoothObject *b_parent; + BluetoothType b_type; + std::string *name_to_write; + std::string *identifier_to_write; + + getObject_setter(env, + name, &name_to_write, + identifier, &identifier_to_write, + parent, &b_parent); + + b_type = from_int_to_btype((int)type); + std::vector<std::unique_ptr<BluetoothObject>> array = manager->get_objects(b_type, + name_to_write, + identifier_to_write, + b_parent); + getObject_cleaner(name_to_write, identifier_to_write); + jobject result = convert_vector_to_jobject<BluetoothObject>(env, array, "(J)V"); + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jobject Java_tinyb_BluetoothManager_getAdapters(JNIEnv *env, jobject obj) { - BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); - - std::vector<std::unique_ptr<BluetoothAdapter>> array = manager->get_adapters(); - jobject result = convert_vector_to_jobject<BluetoothAdapter>(env, array, - "(J)V"); - return result; + try { + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + + std::vector<std::unique_ptr<BluetoothAdapter>> array = manager->get_adapters(); + jobject result = convert_vector_to_jobject<BluetoothAdapter>(env, array, + "(J)V"); + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jobject Java_tinyb_BluetoothManager_getDevices(JNIEnv *env, jobject obj) { - BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); - - std::vector<std::unique_ptr<BluetoothDevice>> array = manager->get_devices(); - jobject result = convert_vector_to_jobject<BluetoothDevice>(env, array, - "(J)V"); - return result; - + try { + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + + std::vector<std::unique_ptr<BluetoothDevice>> array = manager->get_devices(); + jobject result = convert_vector_to_jobject<BluetoothDevice>(env, array, + "(J)V"); + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jobject Java_tinyb_BluetoothManager_getServices(JNIEnv *env, jobject obj) { - BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); - - std::vector<std::unique_ptr<BluetoothGattService>> array = manager->get_services(); - jobject result = convert_vector_to_jobject<BluetoothGattService>(env, array, - "(J)V"); - return result; + try { + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + + std::vector<std::unique_ptr<BluetoothGattService>> array = manager->get_services(); + jobject result = convert_vector_to_jobject<BluetoothGattService>(env, array, + "(J)V"); + return result; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } jboolean Java_tinyb_BluetoothManager_setDefaultAdapter(JNIEnv *env, jobject obj, jobject adapter) { - if (adapter == nullptr) - throw std::invalid_argument("adapter argument is null\n"); - - BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); - BluetoothAdapter *b_adapter = getInstance<BluetoothAdapter>(env, adapter); - - return manager->set_default_adapter(*b_adapter); + try { + if (adapter == nullptr) + throw std::invalid_argument("adapter argument is null\n"); + + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + BluetoothAdapter *b_adapter = getInstance<BluetoothAdapter>(env, adapter); + + return manager->set_default_adapter(*b_adapter); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jboolean Java_tinyb_BluetoothManager_startDiscovery(JNIEnv *env, jobject obj) { - BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); - return manager->start_discovery() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + return manager->start_discovery() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } jboolean Java_tinyb_BluetoothManager_stopDiscovery(JNIEnv *env, jobject obj) { - BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); - return manager->start_discovery() ? JNI_TRUE : JNI_FALSE; + try { + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + return manager->start_discovery() ? JNI_TRUE : JNI_FALSE; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return JNI_FALSE; } void Java_tinyb_BluetoothManager_init(JNIEnv *env, jobject obj) { - BluetoothManager *manager = BluetoothManager::get_bluetooth_manager(); - setInstance<BluetoothManager>(env, obj, manager); + try { + BluetoothManager *manager = BluetoothManager::get_bluetooth_manager(); + setInstance<BluetoothManager>(env, obj, manager); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } void Java_tinyb_BluetoothManager_delete(JNIEnv *env, jobject obj) { - BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); - delete manager; + try { + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + delete manager; + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } } jstring Java_tinyb_BluetoothManager_getNativeAPIVersion(JNIEnv *env, jclass clazz) { - (void) clazz; - - BluetoothManager *manager = BluetoothManager::get_bluetooth_manager(); - return env->NewStringUTF(manager->get_api_version().c_str()); + try { + (void) clazz; + + BluetoothManager *manager = BluetoothManager::get_bluetooth_manager(); + return env->NewStringUTF(manager->get_api_version().c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; } diff --git a/java/jni/helper.cxx b/java/jni/helper.cxx index c701b931..f23691bc 100644 --- a/java/jni/helper.cxx +++ b/java/jni/helper.cxx @@ -120,10 +120,12 @@ bool from_jboolean_to_bool(jboolean val) std::string from_jstring_to_string(JNIEnv *env, jstring str) { jboolean is_copy = JNI_TRUE; + if (!str) { + throw std::invalid_argument("String should not be null"); + } const char *str_chars = (char *)env->GetStringUTFChars(str, &is_copy); - if (!str_chars) - { - throw std::runtime_error("GetStringUTFChars returned NULL\n"); + if (!str_chars) { + throw std::bad_alloc(); } const std::string string_to_write = std::string(str_chars); @@ -196,3 +198,24 @@ jobject get_new_arraylist(JNIEnv *env, unsigned int size, jmethodID *add) return result; } + +void raise_java_exception(JNIEnv *env, std::exception &e) +{ + env->ThrowNew(env->FindClass("java/lang/Error"), e.what()); +} + +void raise_java_runtime_exception(JNIEnv *env, std::runtime_error &e) +{ + env->ThrowNew(env->FindClass("java/lang/RuntimeException"), e.what()); +} + +void raise_java_oom_exception(JNIEnv *env, std::bad_alloc &e) +{ + env->ThrowNew(env->FindClass("java/lang/OutOfMemoryException"), e.what()); +} + +void raise_java_invalid_arg_exception(JNIEnv *env, std::invalid_argument &e) +{ + env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"), e.what()); +} + diff --git a/java/jni/helper.hpp b/java/jni/helper.hpp index d3ed289a..1344e431 100644 --- a/java/jni/helper.hpp +++ b/java/jni/helper.hpp @@ -108,3 +108,8 @@ jobject convert_vector_to_jobject(JNIEnv *env, std::vector<std::unique_ptr<T>>& } return result; } + +void raise_java_exception(JNIEnv *env, std::exception &e); +void raise_java_runtime_exception(JNIEnv *env, std::runtime_error &e); +void raise_java_oom_exception(JNIEnv *env, std::bad_alloc &e); +void raise_java_invalid_arg_exception(JNIEnv *env, std::invalid_argument &e); diff --git a/src/BluetoothDevice.cpp b/src/BluetoothDevice.cpp index 3d37f862..93fc86ed 100644 --- a/src/BluetoothDevice.cpp +++ b/src/BluetoothDevice.cpp @@ -311,10 +311,10 @@ BluetoothAdapter BluetoothDevice::get_adapter () &error); if (adapter == NULL) { - g_printerr("Error instantiating adapter: %s", - error->message); - g_error_free(error); - throw std::exception(); + std::string error_msg("Error occured while instantiating adapter: "); + error_msg += error->message; + g_error_free(error); + throw std::runtime_error(error_msg); } return BluetoothAdapter(adapter); diff --git a/src/BluetoothGattCharacteristic.cpp b/src/BluetoothGattCharacteristic.cpp index 9f0867dc..9fb4ca96 100644 --- a/src/BluetoothGattCharacteristic.cpp +++ b/src/BluetoothGattCharacteristic.cpp @@ -184,11 +184,11 @@ BluetoothGattService BluetoothGattCharacteristic::get_service () NULL, &error); - if (service == NULL) { - g_printerr("Error instantiating: %s", - error->message); + if (service == nullptr) { + std::string error_msg("Error occured while instantiating service: "); + error_msg += error->message; g_error_free(error); - throw std::exception(); + throw std::runtime_error(error_msg); } return BluetoothGattService(service); @@ -197,7 +197,14 @@ BluetoothGattService BluetoothGattCharacteristic::get_service () std::vector<unsigned char> BluetoothGattCharacteristic::get_value () { GBytes *value_gbytes = const_cast<GBytes *>(gatt_characteristic1_get_value (object)); - std::vector<unsigned char> result = from_gbytes_to_vector(value_gbytes); + std::vector<unsigned char> result; + + try { + result = from_gbytes_to_vector(value_gbytes); + } catch (std::exception &e) { + g_bytes_unref(value_gbytes); + throw e; + } g_bytes_unref(value_gbytes); diff --git a/src/BluetoothGattDescriptor.cpp b/src/BluetoothGattDescriptor.cpp index b0edfcba..969097a2 100644 --- a/src/BluetoothGattDescriptor.cpp +++ b/src/BluetoothGattDescriptor.cpp @@ -158,10 +158,10 @@ BluetoothGattCharacteristic BluetoothGattDescriptor::get_characteristic () &error); if (characteristic == NULL) { - g_printerr("Error instantiating: %s", - error->message); + std::string error_msg("Error occured while instantiating characteristic: "); + error_msg += error->message; g_error_free(error); - throw std::exception(); + throw std::runtime_error(error_msg); } return BluetoothGattCharacteristic(characteristic); @@ -170,7 +170,13 @@ BluetoothGattCharacteristic BluetoothGattDescriptor::get_characteristic () std::vector<unsigned char> BluetoothGattDescriptor::get_value () { GBytes *value_gbytes = const_cast<GBytes *>(gatt_descriptor1_get_value (object)); - std::vector<unsigned char> result = from_gbytes_to_vector(value_gbytes); + std::vector<unsigned char> result; + try { + result = from_gbytes_to_vector(value_gbytes); + } catch (std::exception &e) { + g_bytes_unref(value_gbytes); + throw e; + } g_bytes_unref(value_gbytes); diff --git a/src/BluetoothGattService.cpp b/src/BluetoothGattService.cpp index c1d35be8..31757ac1 100644 --- a/src/BluetoothGattService.cpp +++ b/src/BluetoothGattService.cpp @@ -109,11 +109,11 @@ BluetoothDevice BluetoothGattService::get_device () NULL, &error); - if (device == NULL) { - g_printerr("Error instantiating: %s", - error->message); + if (device == nullptr) { + std::string error_msg("Error occured while instantiating device: "); + error_msg += error->message; g_error_free(error); - throw std::exception(); + throw std::runtime_error(error_msg); } return BluetoothDevice(device); diff --git a/src/tinyb_utils.cpp b/src/tinyb_utils.cpp index a5853abc..a1f2a5d6 100644 --- a/src/tinyb_utils.cpp +++ b/src/tinyb_utils.cpp @@ -29,6 +29,9 @@ std::vector<unsigned char> tinyb::from_gbytes_to_vector(const GBytes *bytes) gsize result_size; const unsigned char *aux_array = (const unsigned char *)g_bytes_get_data(const_cast<GBytes *>(bytes), &result_size); + if (aux_array == nullptr || result_size == 0) + throw std::runtime_error("Trying to read empty value"); + std::vector<unsigned char> result(result_size); std::copy(aux_array, aux_array + result_size, result.begin()); @@ -42,11 +45,8 @@ GBytes *tinyb::from_vector_to_gbytes(const std::vector<unsigned char>& vector) const unsigned char *vector_content = vector.data(); GBytes *result = g_bytes_new(vector_content, vector_size); - if (!result) - { - g_printerr("Error: cannot allocate\n"); - throw std::exception(); - } + if (result == nullptr) + throw std::bad_alloc(); return result; } |