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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
/*
* BRLTTY - A background process providing access to the console screen (when in
* text mode) for a blind person using a refreshable braille display.
*
* Copyright (C) 1995-2014 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU General Public License, as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any
* later version. Please see the file LICENSE-GPL for details.
*
* Web Page: http://mielke.cc/brltty/
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
#include "prologue.h"
#include <errno.h>
#include "io_bluetooth.h"
#include "bluetooth_internal.h"
#include "log.h"
#include "io_misc.h"
#include "system_java.h"
struct BluetoothConnectionExtensionStruct {
JNIEnv *env;
jobject connection;
AsyncHandle inputMonitor;
int inputPipe[2];
};
static jclass connectionClass = NULL;
static jmethodID connectionConstructor = 0;
static jmethodID closeMethod = 0;
static jmethodID openMethod = 0;
static jmethodID writeMethod = 0;
BluetoothConnectionExtension *
bthNewConnectionExtension (uint64_t bda) {
BluetoothConnectionExtension *bcx;
if ((bcx = malloc(sizeof(*bcx)))) {
memset(bcx, 0, sizeof(*bcx));
bcx->inputPipe[0] = INVALID_FILE_DESCRIPTOR;
bcx->inputPipe[1] = INVALID_FILE_DESCRIPTOR;
if ((bcx->env = getJavaNativeInterface())) {
if (findJavaClass(bcx->env, &connectionClass, "org/a11y/brltty/android/BluetoothConnection")) {
if (findJavaConstructor(bcx->env, &connectionConstructor, connectionClass,
JAVA_SIG_CONSTRUCTOR(
JAVA_SIG_LONG // deviceAddress
))) {
jobject localReference = (*bcx->env)->NewObject(bcx->env, connectionClass, connectionConstructor, bda);
if (!clearJavaException(bcx->env, 0)) {
jobject globalReference = (*bcx->env)->NewGlobalRef(bcx->env, localReference);
(*bcx->env)->DeleteLocalRef(bcx->env, localReference);
localReference = NULL;
if ((globalReference)) {
bcx->connection = globalReference;
return bcx;
} else {
logMallocError();
clearJavaException(bcx->env, 0);
}
}
}
}
}
free(bcx);
} else {
logMallocError();
}
return NULL;
}
static void
bthCancelInputMonitor (BluetoothConnectionExtension *bcx) {
if (bcx->inputMonitor) {
asyncCancelRequest(bcx->inputMonitor);
bcx->inputMonitor = NULL;
}
}
void
bthReleaseConnectionExtension (BluetoothConnectionExtension *bcx) {
bthCancelInputMonitor(bcx);
if (bcx->connection) {
if (findJavaInstanceMethod(bcx->env, &closeMethod, connectionClass, "close",
JAVA_SIG_METHOD(JAVA_SIG_VOID, ))) {
(*bcx->env)->CallVoidMethod(bcx->env, bcx->connection, closeMethod);
}
(*bcx->env)->DeleteGlobalRef(bcx->env, bcx->connection);
clearJavaException(bcx->env, 1);
}
closeFile(&bcx->inputPipe[0]);
closeFile(&bcx->inputPipe[1]);
free(bcx);
}
int
bthOpenChannel (BluetoothConnectionExtension *bcx, uint8_t channel, int timeout) {
if (pipe(bcx->inputPipe) != -1) {
if (setBlockingIo(bcx->inputPipe[0], 0)) {
if (findJavaInstanceMethod(bcx->env, &openMethod, connectionClass, "open",
JAVA_SIG_METHOD(JAVA_SIG_BOOLEAN,
JAVA_SIG_INT // inputPipe
JAVA_SIG_INT // channel
JAVA_SIG_BOOLEAN // secure
))) {
jboolean result = (*bcx->env)->CallBooleanMethod(bcx->env, bcx->connection, openMethod,
bcx->inputPipe[1], channel, JNI_FALSE);
if (!clearJavaException(bcx->env, 1)) {
if (result == JNI_TRUE) {
return 1;
}
}
errno = EIO;
}
}
close(bcx->inputPipe[0]);
close(bcx->inputPipe[1]);
} else {
logSystemError("pipe");
}
return 0;
}
int
bthDiscoverChannel (
uint8_t *channel, BluetoothConnectionExtension *bcx,
const void *uuidBytes, size_t uuidLength,
int timeout
) {
*channel = 0;
return 1;
}
int
bthMonitorInput (BluetoothConnection *connection, AsyncMonitorCallback *callback, void *data) {
BluetoothConnectionExtension *bcx = connection->extension;
bthCancelInputMonitor(bcx);
if (!callback) return 1;
return asyncMonitorFileInput(&bcx->inputMonitor, bcx->inputPipe[0], callback, data);
}
int
bthAwaitInput (BluetoothConnection *connection, int milliseconds) {
BluetoothConnectionExtension *bcx = connection->extension;
return awaitFileInput(bcx->inputPipe[0], milliseconds);
}
ssize_t
bthReadData (
BluetoothConnection *connection, void *buffer, size_t size,
int initialTimeout, int subsequentTimeout
) {
BluetoothConnectionExtension *bcx = connection->extension;
return readFile(bcx->inputPipe[0], buffer, size, initialTimeout, subsequentTimeout);
}
ssize_t
bthWriteData (BluetoothConnection *connection, const void *buffer, size_t size) {
BluetoothConnectionExtension *bcx = connection->extension;
if (findJavaInstanceMethod(bcx->env, &writeMethod, connectionClass, "write",
JAVA_SIG_METHOD(JAVA_SIG_BOOLEAN,
JAVA_SIG_ARRAY(JAVA_SIG_BYTE)) // bytes
)) {
jbyteArray bytes = (*bcx->env)->NewByteArray(bcx->env, size);
if (bytes) {
jboolean result;
(*bcx->env)->SetByteArrayRegion(bcx->env, bytes, 0, size, buffer);
result = (*bcx->env)->CallBooleanMethod(bcx->env, bcx->connection, writeMethod, bytes);
(*bcx->env)->DeleteLocalRef(bcx->env, bytes);
if (!clearJavaException(bcx->env, 1)) {
if (result == JNI_TRUE) {
return size;
}
}
errno = EIO;
} else {
errno = ENOMEM;
}
} else {
errno = ENOSYS;
}
logSystemError("Bluetooth write");
return -1;
}
char *
bthObtainDeviceName (uint64_t bda, int timeout) {
char *name = NULL;
JNIEnv *env = getJavaNativeInterface();
if (env) {
static jclass class = NULL;
if (findJavaClass(env, &class, "org/a11y/brltty/android/BluetoothConnection")) {
static jmethodID method = 0;
if (findJavaStaticMethod(env, &method, class, "getName",
JAVA_SIG_METHOD(JAVA_SIG_OBJECT(java/lang/String),
JAVA_SIG_LONG // deviceAddress
))) {
jstring jName = (*env)->CallStaticObjectMethod(env, class, method, bda);
if (jName) {
const char *cName = (*env)->GetStringUTFChars(env, jName, NULL);
if (cName) {
if (!(name = strdup(cName))) logMallocError();
(*env)->ReleaseStringUTFChars(env, jName, cName);
} else {
logMallocError();
clearJavaException(env, 0);
}
(*env)->DeleteLocalRef(env, jName);
} else {
logMallocError();
clearJavaException(env, 0);
}
}
}
}
return name;
}
|