summaryrefslogtreecommitdiffstats
path: root/java/jni/direct_bt/helper_dbt.hpp
blob: f644d58c0311e7acab38a7abeebd3b284804d14f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef HELPER_DBT_HPP_
#define HELPER_DBT_HPP_

#include "JNIMem.hpp"
#include "helper_base.hpp"

#include "direct_bt/JavaUplink.hpp"
#include "direct_bt/BasicTypes.hpp"
#include "direct_bt/BTAddress.hpp"

namespace direct_bt {

    class DirectBTJNISettings {
        private:
            bool unifyUUID128Bit = true;

        public:
            /**
             * Enables or disables uuid128_t consolidation
             * for native uuid16_t and uuid32_t values before string conversion.
             * <p>
             * Default is {@code true}, as this represent compatibility with original TinyB D-Bus behavior.
             * </p>
             */
            bool getUnifyUUID128Bit() { return unifyUUID128Bit; }
            void setUnifyUUID128Bit(bool v) { unifyUUID128Bit = v; }
    };
    extern DirectBTJNISettings directBTJNISettings;

    /**
     * Implementation for JavaAnonObj,
     * by simply wrapping a JNIGlobalRef instance.
     */
    class JavaGlobalObj : public JavaAnonObj {
        private:
            JNIGlobalRef javaObjectRef;
            jmethodID  mNotifyDeleted;

        public:
            static inline void check(const std::shared_ptr<JavaAnonObj> & shref, const char* file, int line) {
                if( nullptr == shref ) {
                    throw direct_bt::RuntimeException("JavaGlobalObj::check: Null shared-JavaAnonObj", file, line);
                }
                const jobject obj = static_cast<const JavaGlobalObj*>(shref.get())->getObject();
                if( nullptr == obj ) {
                    throw direct_bt::RuntimeException("JavaGlobalObj::check: Null object", file, line);
                }
            }
            static bool isValid(const std::shared_ptr<JavaAnonObj> & shref) {
                if( nullptr == shref ) {
                    return false;
                }
                const jobject obj = static_cast<const JavaGlobalObj*>(shref.get())->getObject();
                if( nullptr == obj ) {
                    return false;
                }
                return true;
            }
            JavaGlobalObj(jobject obj, jmethodID mNotifyDeleted)
            : javaObjectRef(obj), mNotifyDeleted(mNotifyDeleted) { }

            JavaGlobalObj(const JavaGlobalObj &o) noexcept = default;
            JavaGlobalObj(JavaGlobalObj &&o) noexcept = default;
            JavaGlobalObj& operator=(const JavaGlobalObj &o) noexcept = default;
            JavaGlobalObj& operator=(JavaGlobalObj &&o) noexcept = default;

            virtual ~JavaGlobalObj();

            std::string toString() const override {
                const uint64_t ref = (uint64_t)(void*)javaObjectRef.getObject();
                return "JavaGlobalObj["+uint64HexString(ref, true)+"]";
            }

            /** Clears the java reference, i.e. nulling it, without deleting the global reference via JNI. */
            void clear() override { javaObjectRef.clear(); }

            JNIGlobalRef & getJavaObject() { return javaObjectRef; }

            /* Provides access to the stored GlobalRef as an jobject. */
            jobject getObject() const { return javaObjectRef.getObject(); }
            /* Provides access to the stored GlobalRef as a jclass. */
            jclass getClass() const { return javaObjectRef.getClass(); }

            /* Provides access to the stored GlobalRef as an getJavaObject. */
            static JNIGlobalRef GetJavaObject(const std::shared_ptr<JavaAnonObj> & shref) {
                return static_cast<JavaGlobalObj*>(shref.get())->getJavaObject();
            }
            /* Provides access to the stored GlobalRef as an jobject. */
            static jobject GetObject(const std::shared_ptr<JavaAnonObj> & shref) {
                return static_cast<JavaGlobalObj*>(shref.get())->getObject();
            }

            /* Provides access to the stored GlobalRef as a jclass. */
            static jclass GetClass(const std::shared_ptr<JavaAnonObj> & shref) {
                return static_cast<JavaGlobalObj*>(shref.get())->getClass();
            }
    };

    jclass search_class(JNIEnv *env, JavaUplink &object);

    template <typename T>
    jobject convert_vector_sharedptr_to_jarraylist(JNIEnv *env, std::vector<std::shared_ptr<T>>& array)
    {
        unsigned int array_size = array.size();

        jmethodID arraylist_add;
        jobject result = get_new_arraylist(env, array_size, &arraylist_add);

        if (0 == array_size) {
            return result;
        }

        for (unsigned int i = 0; i < array_size; ++i) {
            std::shared_ptr<T> elem = array[i];
            std::shared_ptr<JavaAnonObj> objref = elem->getJavaObject();
            if ( nullptr == objref ) {
                throw InternalError("JavaUplink element of array has no valid java-object: "+elem->toString(), E_FILE_LINE);
            }
            env->CallBooleanMethod(result, arraylist_add, JavaGlobalObj::GetObject(objref));
        }
        return result;
    }

    BDAddressType fromJavaAdressTypeToBDAddressType(JNIEnv *env, jstring jAddressType);
    jstring fromBDAddressTypeToJavaAddressType(JNIEnv *env, BDAddressType bdAddressType);

    template <typename T>
    T *getDBTObject(JNIEnv *env, jobject obj)
    {
        jlong instance = env->GetLongField(obj, getInstanceField(env, obj));
        T *t = reinterpret_cast<T *>(instance);
        if (t == nullptr) {
            throw std::runtime_error("Trying to acquire null DBTObject");
        }
        t->checkValid();
        return t;
    }

    template <typename T>
    T *getDBTObjectUnchecked(JNIEnv *env, jobject obj)
    {
        jlong instance = env->GetLongField(obj, getInstanceField(env, obj));
        return reinterpret_cast<T *>(instance);
    }

    template <typename T>
    void setDBTObject(JNIEnv *env, jobject obj, T *t)
    {
        if (t == nullptr) {
            throw std::runtime_error("Trying to create null DBTObject");
        }
        jlong instance = reinterpret_cast<jlong>(t);
        env->SetLongField(obj, getInstanceField(env, obj), instance);
    }



} // namespace direct_bt

#endif /* HELPER_DBT_HPP_ */