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
|
/*
* 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-2010 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 <string.h>
#include <errno.h>
#include "log.h"
#include "device.h"
#include "queue.h"
#include "io_bluetooth.h"
#include "bluetooth_internal.h"
static Queue *bluetoothConnectErrors = NULL;
typedef struct {
uint64_t bda;
int value;
} BluetoothConnectError;
static void
bthDeallocateConnectError (void *item, void *data) {
BluetoothConnectError *error = item;
free(error);
}
static int
bthInitializeConnectErrors (void) {
if (!bluetoothConnectErrors) {
if (!(bluetoothConnectErrors = newQueue(bthDeallocateConnectError, NULL))) {
return 0;
}
}
return 1;
}
static int
bthTestConnectError (const void *item, const void *data) {
const BluetoothConnectError *error = item;
const uint64_t *bda = data;
return error->bda == *bda;
}
static BluetoothConnectError *
bthGetConnectError (uint64_t bda, int add) {
BluetoothConnectError *error = findItem(bluetoothConnectErrors, bthTestConnectError, &bda);
if (error) return error;
if (add) {
if ((error = malloc(sizeof(*error)))) {
error->bda = bda;
error->value = 0;
if (enqueueItem(bluetoothConnectErrors, error)) return error;
free(error);
}
}
return NULL;
}
void
bthForgetConnectErrors (void) {
if (bthInitializeConnectErrors()) deleteElements(bluetoothConnectErrors);
}
static int
bthRememberConnectError (uint64_t bda, int value) {
if (bthInitializeConnectErrors()) {
BluetoothConnectError *error = bthGetConnectError(bda, 1);
if (error) {
error->value = value;
return 1;
}
}
return 0;
}
static int
bthRecallConnectError (uint64_t bda, int *value) {
if (bthInitializeConnectErrors()) {
BluetoothConnectError *error = bthGetConnectError(bda, 0);
if (error) {
*value = error->value;
return 1;
}
}
return 0;
}
static void
bthForgetConnectError (uint64_t bda) {
if (bthInitializeConnectErrors()) {
Element *element = findElement(bluetoothConnectErrors, bthTestConnectError, &bda);
if (element) deleteElement(element);
}
}
static int
bthParseAddress (uint64_t *bda, const char *address) {
const unsigned int width = 8;
const unsigned long mask = (1L << width) - 1L;
int counter = BDA_SIZE;
*bda = 0;
do {
*bda <<= width;
if (*address) {
char *end;
long int value = strtol(address, &end, 0X10);
if (end == address) return 0;
if (value < 0) return 0;
if (value > mask) return 0;
*bda |= value;
if (!*(address = end)) continue;
if (*address != ':') return 0;
if (!*++address) return 0;
}
} while (--counter);
return !*address;
}
BluetoothConnection *
bthOpenConnection (const char *address, uint8_t channel, int force) {
BluetoothConnection *connection;
if ((connection = malloc(sizeof(*connection)))) {
memset(connection, 0, sizeof(*connection));
connection->channel = channel;
if (bthParseAddress(&connection->address, address)) {
int alreadyTried = 0;
if (force) {
bthForgetConnectError(connection->address);
} else {
int value;
if (bthRecallConnectError(connection->address, &value)) {
errno = value;
alreadyTried = 1;
}
}
if (!alreadyTried) {
if ((connection->extension = bthConnect(connection->address, connection->channel))) return connection;
bthRememberConnectError(connection->address, errno);
}
} else {
LogPrint(LOG_ERR, "invalid Bluetooth device address: %s", address);
errno = EINVAL;
}
free(connection);
} else {
LogError("malloc");
}
return NULL;
}
void
bthCloseConnection (BluetoothConnection *connection) {
bthDisconnect(connection->extension);
free(connection);
}
int
isBluetoothDevice (const char **path) {
if (isQualifiedDevice(path, "bluetooth:")) return 1;
if (isQualifiedDevice(path, "bt:")) return 1;
if (isQualifiedDevice(path, "bluez:")) return 1;
return 0;
}
|