Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
DBTNativeDownlink.cxx
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2020 ZAFENA AB
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#include "jau_direct_bt_DBTNativeDownlink.h"
27
28#include <jau/debug.hpp>
29
30#include "helper_base.hpp"
31#include "helper_dbt.hpp"
32
34
35using namespace direct_bt;
36using namespace jau::jni;
37
38void Java_jau_direct_1bt_DBTNativeDownlink_initNativeJavaObject(JNIEnv *env, jobject obj, jlong nativeInstance)
39{
40 try {
41 shared_ptr_ref<JavaUplink> javaUplink(nativeInstance); // hold copy until done
42 javaUplink.null_check2();
43 JNIGlobalRef global_obj(obj); // lock instance first (global reference), inserted below
44 jclass javaClazz = search_class(env, global_obj.getObject());
46 if( nullptr == javaClazz ) {
47 throw jau::InternalError("DBTNativeDownlink class not found", E_FILE_LINE);
48 }
49 jmethodID mNotifyDeleted = search_method(env, javaClazz, "notifyDeleted", "()V", false);
51 if( nullptr == mNotifyDeleted ) {
52 throw jau::InternalError("DBTNativeDownlink class has no notifyDeleted() method, for "+javaUplink->toString(), E_FILE_LINE);
53 }
54 javaUplink->setJavaObject( std::make_shared<JavaGlobalObj>( std::move(global_obj), mNotifyDeleted ) );
55 JavaGlobalObj::check(javaUplink->getJavaObject(), E_FILE_LINE);
56 DBG_JNI_PRINT("Java_jau_direct_1bt_DBTNativeDownlink_initNativeJavaObject %p -> %s", javaUplink.shared_ptr().get(), javaUplink->toString().c_str());
57 } catch(...) {
59 }
60}
61
62void Java_jau_direct_1bt_DBTNativeDownlink_deleteNativeJavaObject(JNIEnv *env, jobject obj, jlong nativeInstance)
63{
64 (void)obj;
65 try {
66 shared_ptr_ref<JavaUplink> javaUplink(nativeInstance); // hold copy until done
67 javaUplink.null_check2();
68 DBG_JNI_PRINT("Java_jau_direct_1bt_DBTNativeDownlink_deleteNativeJavaObject %p -> %s", javaUplink.shared_ptr().get(), javaUplink->toString().c_str());
69 javaUplink->setJavaObject();
70 } catch(...) {
72 }
73}
74
#define E_FILE_LINE
jobject getObject() const noexcept
A std::shared_ptr<T> storage instance to be copied from and released into a java object's long native...
Definition: helper_jni.hpp:393
void null_check2() const
Throws an exception if either this instances shared_ptr<T> storage or the managed object reference is...
Definition: helper_jni.hpp:508
const std::shared_ptr< T > & shared_ptr() const
Provides access to const reference of shared_ptr<T>, r-value.
Definition: helper_jni.hpp:630
std::string toString() const noexcept
Definition: helper_jni.hpp:657
#define DBG_JNI_PRINT(...)
Use for environment-variable environment::DEBUG_JNI conditional debug messages, prefix '[elapsed_time...
Definition: debug.hpp:58
void java_exception_check_and_throw(JNIEnv *env, const char *file, int line)
Throws a C++ exception if a java exception occurred, otherwise do nothing.
jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name, const char *prototype, bool is_static)
jclass search_class(JNIEnv *env, const char *clazz_name)
#define rethrow_and_raise_java_exception(E)
Re-throw current exception and raise respective java exception using any matching function above.
Definition: helper_base.hpp:52