summaryrefslogtreecommitdiffstats
path: root/core/jni/android_bluetooth_Database.cpp
blob: 136c9a328d5593683a9df45c17685eb5497a9226 (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
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define DBUS_CLASS_NAME BLUEZ_DBUS_BASE_IFC ".Database"
#define LOG_TAG "bluetooth_Database.cpp"

#include "android_bluetooth_common.h"
#include "android_runtime/AndroidRuntime.h"
#include "JNIHelp.h"
#include "jni.h"
#include "utils/Log.h"

#ifdef HAVE_BLUETOOTH
#include <dbus/dbus.h>
#endif

namespace android {

#ifdef HAVE_BLUETOOTH
static DBusConnection* conn = NULL;   // Singleton thread-safe connection
#endif

static void classInitNative(JNIEnv* env, jclass clazz) {
    LOGV(__FUNCTION__);
#ifdef HAVE_BLUETOOTH
    conn = NULL;
#endif
}

static void initializeNativeDataNative(JNIEnv* env, jobject object) {
    LOGV(__FUNCTION__);

#ifdef HAVE_BLUETOOTH
    if (conn == NULL) {
        DBusError err;
        dbus_error_init(&err);
        dbus_threads_init_default();
        conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
        if (dbus_error_is_set(&err)) {
            LOGE("Could not get onto the system bus!");
            dbus_error_free(&err);
        }
    }
#endif
}

static void cleanupNativeDataNative(JNIEnv* env, jobject object) {
    LOGV(__FUNCTION__);
}

static jint addServiceRecordNative(JNIEnv *env, jobject object,
                                   jbyteArray record) {
    LOGV(__FUNCTION__);
#ifdef HAVE_BLUETOOTH
    if (conn != NULL) {
        jbyte* c_record = env->GetByteArrayElements(record, NULL);
        DBusMessage *reply = dbus_func_args(env,
                                            conn,
                                            BLUEZ_DBUS_BASE_PATH,
                                            DBUS_CLASS_NAME,
                                            "AddServiceRecord",
                                            DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
                                            &c_record,
                                            env->GetArrayLength(record),
                                            DBUS_TYPE_INVALID);
        env->ReleaseByteArrayElements(record, c_record, JNI_ABORT);
        return reply ? dbus_returns_uint32(env, reply) : -1;
    }
#endif
    return -1;
}

static jint addServiceRecordFromXmlNative(JNIEnv *env, jobject object,
                                          jstring record) {
    LOGV(__FUNCTION__);
#ifdef HAVE_BLUETOOTH
    if (conn != NULL) {
        const char *c_record = env->GetStringUTFChars(record, NULL);
        DBusMessage *reply = dbus_func_args(env,
                                            conn,
                                            BLUEZ_DBUS_BASE_PATH,
                                            DBUS_CLASS_NAME,
                                            "AddServiceRecordFromXML",
                                            DBUS_TYPE_STRING, &c_record,
                                            DBUS_TYPE_INVALID);
        env->ReleaseStringUTFChars(record, c_record);
        return reply ? dbus_returns_uint32(env, reply) : -1;
    }
#endif
    return -1;
}

static void updateServiceRecordNative(JNIEnv *env, jobject object,
                                      jint handle,
                                      jbyteArray record) {
    LOGV(__FUNCTION__);
#ifdef HAVE_BLUETOOTH
    if (conn != NULL) {
        jbyte* c_record = env->GetByteArrayElements(record, NULL);
        DBusMessage *reply = dbus_func_args(env,
                                            conn,
                                            BLUEZ_DBUS_BASE_PATH,
                                            DBUS_CLASS_NAME,
                                            "UpdateServiceRecord",
                                            DBUS_TYPE_UINT32, &handle,
                                            DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
                                            &c_record,
                                            env->GetArrayLength(record),
                                            DBUS_TYPE_INVALID);
        env->ReleaseByteArrayElements(record, c_record, JNI_ABORT);
    }
#endif
}

static void updateServiceRecordFromXmlNative(JNIEnv *env, jobject object,
                                             jint handle,
                                             jstring record) {
    LOGV(__FUNCTION__);
#ifdef HAVE_BLUETOOTH
    if (conn != NULL) {
        const char *c_record = env->GetStringUTFChars(record, NULL);
        DBusMessage *reply = dbus_func_args(env,
                                            conn,
                                            BLUEZ_DBUS_BASE_PATH,
                                            DBUS_CLASS_NAME,
                                            "UpdateServiceRecordFromXML",
                                            DBUS_TYPE_UINT32, &handle,
                                            DBUS_TYPE_STRING, &c_record,
                                            DBUS_TYPE_INVALID);
        env->ReleaseStringUTFChars(record, c_record);
    }
#endif
}

/* private static native void removeServiceRecordNative(int handle); */
static void removeServiceRecordNative(JNIEnv *env, jobject object,
                                      jint handle) {
    LOGV(__FUNCTION__);
#ifdef HAVE_BLUETOOTH
    if (conn != NULL) {
        DBusMessage *reply = dbus_func_args(env,
                                            conn,
                                            BLUEZ_DBUS_BASE_PATH,
                                            DBUS_CLASS_NAME,
                                            "RemoveServiceRecord",
                                            DBUS_TYPE_UINT32, &handle,
                                            DBUS_TYPE_INVALID);
    }
#endif
}


static JNINativeMethod sMethods[] = {
     /* name, signature, funcPtr */
    {"classInitNative", "()V", (void*)classInitNative},
    {"initializeNativeDataNative", "()V", (void *)initializeNativeDataNative},
    {"cleanupNativeDataNative", "()V", (void *)cleanupNativeDataNative},
    {"addServiceRecordNative", "([B)I", (void*)addServiceRecordNative},
    {"addServiceRecordFromXmlNative", "(Ljava/lang/String;)I", (void*)addServiceRecordFromXmlNative},
    {"updateServiceRecordNative", "(I[B)V", (void*)updateServiceRecordNative},
    {"updateServiceRecordFromXmlNative", "(ILjava/lang/String;)V", (void*)updateServiceRecordFromXmlNative},
    {"removeServiceRecordNative", "(I)V", (void*)removeServiceRecordNative},
};

int register_android_bluetooth_Database(JNIEnv *env) {
    return AndroidRuntime::registerNativeMethods(env,
            "android/bluetooth/Database", sMethods, NELEM(sMethods));
}

} /* namespace android */