| 1 |
/* |
| 2 |
* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License version 2 as |
| 5 |
* published by the Free Software Foundation; |
| 6 |
* |
| 7 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 8 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 10 |
* IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 11 |
* CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
* |
| 16 |
* ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 17 |
* COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 18 |
* SOFTWARE IS DISCLAIMED. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
#include <config.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <string.h> |
| 30 |
#include <signal.h> |
| 31 |
#include <fcntl.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <ctype.h> |
| 34 |
#include <syslog.h> |
| 35 |
#include <stdarg.h> |
| 36 |
|
| 37 |
#include <termios.h> |
| 38 |
#include <fcntl.h> |
| 39 |
#include <getopt.h> |
| 40 |
#include <sys/types.h> |
| 41 |
#include <sys/ioctl.h> |
| 42 |
|
| 43 |
#include <bluetooth/bluetooth.h> |
| 44 |
#include <bluetooth/hci.h> |
| 45 |
#include <bluetooth/hci_lib.h> |
| 46 |
|
| 47 |
#define DBUS_API_SUBJECT_TO_CHANGE |
| 48 |
#include <dbus/dbus.h> |
| 49 |
#include <stdbool.h> |
| 50 |
#include <unistd.h> |
| 51 |
#include <stdio.h> |
| 52 |
#include <stdlib.h> |
| 53 |
|
| 54 |
#define BTCONNECTTIMEOUT 5000 |
| 55 |
#define DEFAULTTHRESHOLD 200 |
| 56 |
#define DEFAULTINTERVAL 100 |
| 57 |
//#define DEBUG |
| 58 |
|
| 59 |
#define DBUS_INTROSPECTION_DATA "<!DOCTYPE node PUBLIC \ |
| 60 |
\"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" \ |
| 61 |
\"http://dbus.freedesktop.org/doc/introspect.dtd\"> \n\ |
| 62 |
<node name=\"/cx/ath/matthew/bluemon/Bluemon\">\n\ |
| 63 |
<interface name=\"cx.ath.matthew.bluemon.Bluemon\">\n\ |
| 64 |
<method name=\"Status\" >\n\ |
| 65 |
<arg name=\"address\" type=\"s\" direction=\"in\"/>\n\ |
| 66 |
<arg name=\"address\" type=\"s\" direction=\"out\"/>\n\ |
| 67 |
<arg name=\"status\" type=\"b\" direction=\"out\"/>\n\ |
| 68 |
<arg name=\"level\" type=\"u\" direction=\"out\"/>\n\ |
| 69 |
</method>\n\ |
| 70 |
</interface>\n\ |
| 71 |
<interface name=\"cx.ath.matthew.bluemon.ProximitySignal\">\n\ |
| 72 |
<signal name=\"Connect\" >\n\ |
| 73 |
<arg name=\"address\" type=\"s\" direction=\"out\"/>\n\ |
| 74 |
</signal>\n\ |
| 75 |
<signal name=\"Disconnect\" >\n\ |
| 76 |
<arg name=\"address\" type=\"s\" direction=\"out\"/>\n\ |
| 77 |
</signal>\n\ |
| 78 |
</interface>\n\ |
| 79 |
<interface name=\"org.freedesktop.DBus.Introspectable\">\n\ |
| 80 |
<method name=\"Introspect\">\n\ |
| 81 |
<arg type=\"s\" direction=\"out\"/>\n\ |
| 82 |
</method>\n\ |
| 83 |
</interface>\n\ |
| 84 |
</node>" |
| 85 |
|
| 86 |
|
| 87 |
struct devstruct; |
| 88 |
typedef struct devstruct { |
| 89 |
char* btid; |
| 90 |
bdaddr_t bdaddr; |
| 91 |
int dd; |
| 92 |
int track; |
| 93 |
bool connected; |
| 94 |
struct devstruct* next; |
| 95 |
} btdev_t; |
| 96 |
|
| 97 |
btdev_t* btdev_append_new(btdev_t* current, char* btid) |
| 98 |
{ |
| 99 |
btdev_t* temp; |
| 100 |
if (NULL == current) { |
| 101 |
temp = malloc(sizeof(btdev_t)); |
| 102 |
temp->btid = btid; |
| 103 |
temp->dd = 0; |
| 104 |
temp->connected = false; |
| 105 |
temp->next = NULL; |
| 106 |
temp->track = -1; |
| 107 |
return temp; |
| 108 |
} |
| 109 |
else { |
| 110 |
current->next = btdev_append_new(current->next, btid); |
| 111 |
return current; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
typedef struct { |
| 116 |
char* config; |
| 117 |
btdev_t* btdevroot; |
| 118 |
int threshold; |
| 119 |
int interval; |
| 120 |
bool stdout; |
| 121 |
bool fork; |
| 122 |
bool verbose; |
| 123 |
bool authenticate; |
| 124 |
int uid; |
| 125 |
int gid; |
| 126 |
bool disconnecthack; |
| 127 |
bool linkquality; |
| 128 |
} arg_t; |
| 129 |
arg_t opts; |
| 130 |
arg_t args; |
| 131 |
DBusConnection* conn = NULL; |
| 132 |
|
| 133 |
void message(bool verbose, char* msg, ...); |
| 134 |
void sendsignal(bool connected, btdev_t* btdev); |
| 135 |
|
| 136 |
#define assert(a, b) if(!(a)) real_assert(b) |
| 137 |
void real_assert (char* msg) |
| 138 |
{ |
| 139 |
if (0 != errno) { |
| 140 |
char* errstr = strerror(errno); |
| 141 |
message(false, "[Assert] failed with errno %d/%s: %s\n", errno, errstr, msg); |
| 142 |
} else |
| 143 |
message(false, "[Assert] failed: %s\n", msg); |
| 144 |
#ifdef DEBUG |
| 145 |
exit(1); |
| 146 |
#endif |
| 147 |
} |
| 148 |
|
| 149 |
static int find_conn(int s, int dev_id, long arg) |
| 150 |
{ |
| 151 |
struct hci_conn_list_req *cl; |
| 152 |
struct hci_conn_info *ci; |
| 153 |
int i; |
| 154 |
|
| 155 |
if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) { |
| 156 |
perror("Can't allocate memory"); |
| 157 |
exit(1); |
| 158 |
} |
| 159 |
cl->dev_id = dev_id; |
| 160 |
cl->conn_num = 10; |
| 161 |
ci = cl->conn_info; |
| 162 |
|
| 163 |
if (ioctl(s, HCIGETCONNLIST, (void*)cl)) { |
| 164 |
perror("Can't get connection list"); |
| 165 |
exit(1); |
| 166 |
} |
| 167 |
|
| 168 |
for (i=0; i < cl->conn_num; i++, ci++) |
| 169 |
if (!bacmp((bdaddr_t *)arg, &ci->bdaddr)) { |
| 170 |
free(cl); |
| 171 |
return 1; |
| 172 |
} |
| 173 |
free(cl); |
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
// open connection |
| 178 |
bool btconnect(btdev_t* btdev) |
| 179 |
{ |
| 180 |
int ptype; |
| 181 |
uint16_t handle = 0; |
| 182 |
uint8_t role; |
| 183 |
bool rval; |
| 184 |
struct hci_conn_info_req *cr; |
| 185 |
struct hci_request rq; |
| 186 |
auth_requested_cp acp; |
| 187 |
evt_auth_complete arp; |
| 188 |
evt_encrypt_change erp; |
| 189 |
set_conn_encrypt_cp ecp; |
| 190 |
|
| 191 |
role = 0x01; |
| 192 |
ptype = HCI_DM1 | HCI_DM3 | HCI_DM5 | HCI_DH1 | HCI_DH3 | HCI_DH5; |
| 193 |
|
| 194 |
int dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &(btdev->bdaddr)); |
| 195 |
if (dev_id < 0) { |
| 196 |
message(true, "Connecting to: %x:%x:%x:%x:%x:%x\n", btdev->bdaddr.b[5], btdev->bdaddr.b[4], btdev->bdaddr.b[3], btdev->bdaddr.b[2], btdev->bdaddr.b[1], btdev->bdaddr.b[0]); |
| 197 |
if (hci_create_connection(btdev->dd, &(btdev->bdaddr), htobs(ptype), 0, role, &handle, BTCONNECTTIMEOUT) < 0) { |
| 198 |
message(true, "Can't create connection\n"); |
| 199 |
return false; |
| 200 |
} |
| 201 |
btdev->connected = true; |
| 202 |
} |
| 203 |
else btdev->connected = false; |
| 204 |
|
| 205 |
if (0 == handle) { |
| 206 |
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info)); |
| 207 |
|
| 208 |
bacpy(&cr->bdaddr, &(btdev->bdaddr)); |
| 209 |
cr->type = ACL_LINK; |
| 210 |
if (ioctl(btdev->dd, HCIGETCONNINFO, (unsigned long) cr) < 0) { |
| 211 |
message(true, "Get connection info failed"); |
| 212 |
return false; |
| 213 |
} |
| 214 |
handle = htobs(cr->conn_info->handle); |
| 215 |
free(cr); |
| 216 |
} |
| 217 |
|
| 218 |
if (opts.authenticate || args.authenticate) { |
| 219 |
|
| 220 |
acp.handle = htobs(handle); |
| 221 |
|
| 222 |
memset(&rq, 0, sizeof(rq)); |
| 223 |
rq.ogf = OGF_LINK_CTL; |
| 224 |
rq.ocf = OCF_AUTH_REQUESTED; |
| 225 |
rq.cparam = &acp; |
| 226 |
rq.clen = AUTH_REQUESTED_CP_SIZE; |
| 227 |
rq.rparam = &arp; |
| 228 |
rq.rlen = EVT_AUTH_COMPLETE_SIZE; |
| 229 |
rq.event = EVT_AUTH_COMPLETE; |
| 230 |
|
| 231 |
if (hci_send_req(btdev->dd, &rq, 25000) < 0) { |
| 232 |
#ifdef DEBUG |
| 233 |
message(true, "Can't authenticate link\n"); |
| 234 |
#endif |
| 235 |
return false; |
| 236 |
} |
| 237 |
ecp.handle = htobs(handle); |
| 238 |
ecp.encrypt = 1; |
| 239 |
|
| 240 |
memset(&rq, 0, sizeof(rq)); |
| 241 |
rq.ogf = OGF_LINK_CTL; |
| 242 |
rq.ocf = OCF_SET_CONN_ENCRYPT; |
| 243 |
rq.cparam = &ecp; |
| 244 |
rq.clen = SET_CONN_ENCRYPT_CP_SIZE; |
| 245 |
rq.rparam = &erp; |
| 246 |
rq.rlen = EVT_ENCRYPT_CHANGE_SIZE; |
| 247 |
rq.event = EVT_ENCRYPT_CHANGE; |
| 248 |
|
| 249 |
if (hci_send_req(btdev->dd, &rq, 25000) < 0) { |
| 250 |
#ifdef DEBUG |
| 251 |
message(true, "Can't encrypt link\n"); |
| 252 |
#endif |
| 253 |
return false; |
| 254 |
} |
| 255 |
} |
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
// close connection |
| 261 |
void btdisconnect(btdev_t* btdev) |
| 262 |
{ |
| 263 |
struct hci_conn_info_req *cr; |
| 264 |
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info)); |
| 265 |
if (!cr) |
| 266 |
return; |
| 267 |
|
| 268 |
bacpy(&cr->bdaddr, &(btdev->bdaddr)); |
| 269 |
cr->type = ACL_LINK; |
| 270 |
if (ioctl(btdev->dd, HCIGETCONNINFO, (unsigned long) cr) < 0) { |
| 271 |
message(false, "Get connection info failed\n"); |
| 272 |
exit(1); |
| 273 |
} |
| 274 |
|
| 275 |
if (hci_disconnect(btdev->dd, htobs(cr->conn_info->handle), HCI_OE_USER_ENDED_CONNECTION, 100) < 0) |
| 276 |
message(false, "Disconnect failed\n"); |
| 277 |
free(cr); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
// query link quality |
| 283 |
int get_link_qual(btdev_t* btdev) |
| 284 |
{ |
| 285 |
struct hci_conn_info_req *cr; |
| 286 |
struct hci_request rq; |
| 287 |
#ifdef OCF_GET_LINK_QUALITY |
| 288 |
get_link_quality_rp rp; |
| 289 |
#else |
| 290 |
read_link_quality_rp rp; |
| 291 |
#endif |
| 292 |
uint16_t handle; |
| 293 |
int qual; |
| 294 |
|
| 295 |
// check the quality |
| 296 |
if (opts.linkquality || args.linkquality) { |
| 297 |
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info)); |
| 298 |
if (!cr) |
| 299 |
return; |
| 300 |
|
| 301 |
bacpy(&cr->bdaddr, &(btdev->bdaddr)); |
| 302 |
cr->type = ACL_LINK; |
| 303 |
if (ioctl(btdev->dd, HCIGETCONNINFO, (unsigned long) cr) < 0) { |
| 304 |
#ifdef DEBUG |
| 305 |
message(true, "Get connection info failed, trying reconnect\n"); |
| 306 |
#endif |
| 307 |
return -1; |
| 308 |
} |
| 309 |
|
| 310 |
handle = htobs(cr->conn_info->handle); |
| 311 |
|
| 312 |
memset(&rq, 0, sizeof(rq)); |
| 313 |
rq.ogf = OGF_STATUS_PARAM; |
| 314 |
#ifdef OCF_GET_LINK_QUALITY |
| 315 |
rq.ocf = OCF_GET_LINK_QUALITY; |
| 316 |
#else |
| 317 |
rq.ocf = OCF_READ_LINK_QUALITY; |
| 318 |
#endif |
| 319 |
rq.cparam = &handle; |
| 320 |
rq.clen = 2; |
| 321 |
rq.rparam = &rp; |
| 322 |
#ifdef OCF_GET_LINK_QUALITY_RP_SIZE |
| 323 |
rq.rlen = GET_LINK_QUALITY_RP_SIZE; |
| 324 |
#else |
| 325 |
rq.rlen = READ_LINK_QUALITY_RP_SIZE; |
| 326 |
#endif |
| 327 |
|
| 328 |
if (hci_send_req(btdev->dd, &rq, 100) < 0) { |
| 329 |
message(true, "HCI get_link_quality request failed, trying reconnect\n"); |
| 330 |
free(cr); |
| 331 |
return -1; |
| 332 |
} |
| 333 |
|
| 334 |
if (rp.status) { |
| 335 |
#ifdef DEBUG |
| 336 |
message(true, "HCI get_link_quality cmd failed (0x%2.2X), trying reconnect\n", rp.status); |
| 337 |
#endif |
| 338 |
free(cr); |
| 339 |
return -1; |
| 340 |
} |
| 341 |
|
| 342 |
qual = rp.link_quality; |
| 343 |
free(cr); |
| 344 |
return qual; |
| 345 |
} |
| 346 |
// just check for connection |
| 347 |
else { |
| 348 |
qual = hci_for_each_dev(HCI_UP, find_conn, (long) &(btdev->bdaddr)); |
| 349 |
return qual < 0 ? qual : 255; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
void readoptions(arg_t args, arg_t* opts) |
| 354 |
{ |
| 355 |
opts->config = NULL; |
| 356 |
opts->btdevroot = NULL; |
| 357 |
opts->threshold = DEFAULTTHRESHOLD; |
| 358 |
opts->interval = DEFAULTINTERVAL; |
| 359 |
opts->stdout = false; |
| 360 |
opts->fork = true; |
| 361 |
opts->verbose = false; |
| 362 |
opts->uid = 0; |
| 363 |
opts->gid = 0; |
| 364 |
opts->authenticate = false; |
| 365 |
opts->linkquality = false; |
| 366 |
} |
| 367 |
|
| 368 |
char* progname; |
| 369 |
|
| 370 |
void syntax() |
| 371 |
{ |
| 372 |
printf("Syntax: %s <options>\n", progname); |
| 373 |
//printf(" -c configfile --config=configfile\n"); |
| 374 |
printf(" -b aa:bb:cc:dd:ee:ff --btid=aa:bb:cc:dd:ee:ff\n"); |
| 375 |
printf(" -t threshold --threshold=threshold\n"); |
| 376 |
printf(" -i interval --interval=interval\n"); |
| 377 |
printf(" -s --stdout --no-syslog\n"); |
| 378 |
printf(" -n --no-fork\n"); |
| 379 |
printf(" -v --verbose\n"); |
| 380 |
printf(" -V --version\n"); |
| 381 |
printf(" -d --disconnect-hack\n"); |
| 382 |
printf(" -q --link-quality\n"); |
| 383 |
printf(" -a --authenticate\n"); |
| 384 |
printf(" -h --help\n"); |
| 385 |
exit(1); |
| 386 |
} |
| 387 |
|
| 388 |
/* |
| 389 |
* -c --config |
| 390 |
* -b --btid |
| 391 |
* -t --threshold |
| 392 |
* -i --interval |
| 393 |
* -s --stdout --no-syslog |
| 394 |
* -n --no-fork |
| 395 |
* -v --verbose |
| 396 |
* -a --authenticate |
| 397 |
* -h --help |
| 398 |
*/ |
| 399 |
void readargs(int argc, char** argv, arg_t* args) |
| 400 |
{ |
| 401 |
args->config = NULL; |
| 402 |
args->btdevroot = NULL; |
| 403 |
args->threshold = DEFAULTTHRESHOLD; |
| 404 |
args->interval = DEFAULTINTERVAL; |
| 405 |
args->stdout = false; |
| 406 |
args->fork = true; |
| 407 |
args->verbose = false; |
| 408 |
args->authenticate = false; |
| 409 |
args->disconnecthack = false; |
| 410 |
args->linkquality = false; |
| 411 |
args->uid = 0; |
| 412 |
args->gid = 0; |
| 413 |
|
| 414 |
int c; |
| 415 |
static struct option long_opts[] = { |
| 416 |
{"config", 1, 0, 'c'}, |
| 417 |
{"btid", 1, 0, 'b'}, |
| 418 |
{"threshold", 1, 0, 't'}, |
| 419 |
{"interval", 1, 0, 'i'}, |
| 420 |
{"stdout", 0, 0, 's'}, |
| 421 |
{"help", 0, 0, 'h'}, |
| 422 |
{"no-fork", 0, 0, 'n'}, |
| 423 |
{"verbose", 0, 0, 'v'}, |
| 424 |
{"version", 0, 0, 'V'}, |
| 425 |
{"authenticate", 0, 0, 'a'}, |
| 426 |
{"no-syslog", 0, 0, 's'}, |
| 427 |
{"uid", 1, 0, 'u'}, |
| 428 |
{"gid", 1, 0, 'u'}, |
| 429 |
{"disconnect-hack", 1, 0, 'd'}, |
| 430 |
{"link-quality", 1, 0, 'q'} |
| 431 |
}; |
| 432 |
int idx = 0; |
| 433 |
while (-1 != (c = getopt_long(argc, argv, "-c:b:t:i:o:sqhVndvag:u:",long_opts, &idx))) { |
| 434 |
switch (c) { |
| 435 |
case 'c': |
| 436 |
args->config = optarg; |
| 437 |
break; |
| 438 |
case 'b': |
| 439 |
args->btdevroot = btdev_append_new(args->btdevroot, optarg); |
| 440 |
break; |
| 441 |
case 't': |
| 442 |
args->threshold = atoi(optarg); |
| 443 |
break; |
| 444 |
case 'i': |
| 445 |
args->interval = atoi(optarg); |
| 446 |
break; |
| 447 |
case 'u': |
| 448 |
args->uid = atoi(optarg); |
| 449 |
break; |
| 450 |
case 'g': |
| 451 |
args->gid = atoi(optarg); |
| 452 |
break; |
| 453 |
case 's': |
| 454 |
args->stdout = true; |
| 455 |
break; |
| 456 |
case 'n': |
| 457 |
args->fork = false; |
| 458 |
break; |
| 459 |
case 'v': |
| 460 |
args->verbose = true; |
| 461 |
break; |
| 462 |
case 'd': |
| 463 |
args->disconnecthack = true; |
| 464 |
break; |
| 465 |
case 'a': |
| 466 |
args->authenticate = true; |
| 467 |
break; |
| 468 |
case 'q': |
| 469 |
args->linkquality = true; |
| 470 |
break; |
| 471 |
case 'V': |
| 472 |
printf("Bluemon version %s.\n", VERSION); |
| 473 |
break; |
| 474 |
case 'h': |
| 475 |
case ':': |
| 476 |
case '?': |
| 477 |
case 1: |
| 478 |
syntax(); |
| 479 |
break; |
| 480 |
} |
| 481 |
idx = 0; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
int testint(int a, int b, char* errormsg) |
| 486 |
{ |
| 487 |
if (0 != a) |
| 488 |
return a; |
| 489 |
if (0 != b) |
| 490 |
return b; |
| 491 |
message(false, "%s\n", errormsg); |
| 492 |
exit(1); |
| 493 |
} |
| 494 |
btdev_t* testdev(btdev_t* a, btdev_t* b, char* errormsg) |
| 495 |
{ |
| 496 |
if (NULL != a) |
| 497 |
return a; |
| 498 |
if (NULL != b) |
| 499 |
return b; |
| 500 |
message(false, "%s\n", errormsg); |
| 501 |
|
| 502 |
exit(1); |
| 503 |
} |
| 504 |
char* testchar(char* a, char* b, char* errormsg) |
| 505 |
{ |
| 506 |
if (NULL != a) |
| 507 |
return a; |
| 508 |
if (NULL != b) |
| 509 |
return b; |
| 510 |
message(false, "%s\n", errormsg); |
| 511 |
|
| 512 |
exit(1); |
| 513 |
} |
| 514 |
|
| 515 |
// write a message to syslog or stdout depending on setup |
| 516 |
void message(bool verbose, char* msg, ...) |
| 517 |
{ |
| 518 |
if (!args.verbose && !opts.verbose && verbose) return; |
| 519 |
va_list vargs; |
| 520 |
va_start(vargs, msg); |
| 521 |
|
| 522 |
if (args.stdout || opts.stdout) |
| 523 |
vprintf(msg, vargs); |
| 524 |
else |
| 525 |
vsyslog(LOG_NOTICE, msg, vargs); |
| 526 |
|
| 527 |
va_end(vargs); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
void sighandle(int signum) |
| 533 |
{ |
| 534 |
btdev_t* btdev; |
| 535 |
switch (signum) { |
| 536 |
case SIGINT: |
| 537 |
case SIGTERM: |
| 538 |
btdev = testdev(args.btdevroot, opts.btdevroot, "No Devices Specified"); |
| 539 |
while (NULL != btdev) { |
| 540 |
if (btdev->connected) { |
| 541 |
// disconnect bluetooth |
| 542 |
btdisconnect(btdev); |
| 543 |
message(true, "Disconnected %s\n", btdev->btid); |
| 544 |
} |
| 545 |
if (NULL != conn) |
| 546 |
sendsignal(false, btdev); |
| 547 |
hci_close_dev(btdev->dd); |
| 548 |
btdev_t* temp = btdev->next; |
| 549 |
free(btdev); |
| 550 |
btdev = temp; |
| 551 |
} |
| 552 |
message(false, "Caught SIGTERM, announcing our demise\n"); |
| 553 |
if (NULL != conn) |
| 554 |
dbus_connection_unref(conn); |
| 555 |
exit(0); |
| 556 |
case SIGHUP: |
| 557 |
readoptions(args, &opts); |
| 558 |
break; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
void writepidfile(void) |
| 563 |
{ |
| 564 |
FILE* fd; |
| 565 |
fd = fopen("/var/run/bluemon.pid", "w"); |
| 566 |
if (NULL != fd) { |
| 567 |
fprintf(fd, "%d", getpid()); |
| 568 |
fclose(fd); |
| 569 |
} else { |
| 570 |
message(true, "Could not write PID file, quitting\n"); |
| 571 |
exit(1); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
DBusConnection* register_dbus() |
| 576 |
{ |
| 577 |
DBusError err; |
| 578 |
DBusConnection* conn; |
| 579 |
int ret; |
| 580 |
|
| 581 |
dbus_error_init(&err); |
| 582 |
|
| 583 |
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err); |
| 584 |
if (dbus_error_is_set(&err)) { |
| 585 |
message(true, "DBUS Connection Error (%s)\n", err.message); |
| 586 |
dbus_error_free(&err); |
| 587 |
} |
| 588 |
if (NULL == conn) { |
| 589 |
exit(1); |
| 590 |
} |
| 591 |
|
| 592 |
ret = dbus_bus_request_name(conn, "cx.ath.matthew.bluemon.server", |
| 593 |
DBUS_NAME_FLAG_REPLACE_EXISTING, |
| 594 |
&err); |
| 595 |
if (dbus_error_is_set(&err)) { |
| 596 |
message(true, "DBUS Name Error (%s)\n", err.message); |
| 597 |
dbus_error_free(&err); |
| 598 |
} |
| 599 |
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { |
| 600 |
exit(1); |
| 601 |
} |
| 602 |
return conn; |
| 603 |
} |
| 604 |
|
| 605 |
void sendsignal(bool inrange, btdev_t* btdev) |
| 606 |
{ |
| 607 |
DBusMessage* msg; |
| 608 |
DBusMessageIter it; |
| 609 |
dbus_uint32_t serial; |
| 610 |
if (inrange) |
| 611 |
msg = dbus_message_new_signal("/cx/ath/matthew/bluemon/Bluemon", "cx.ath.matthew.bluemon.ProximitySignal", "Connect"); |
| 612 |
else |
| 613 |
msg = dbus_message_new_signal("/cx/ath/matthew/bluemon/Bluemon", "cx.ath.matthew.bluemon.ProximitySignal", "Disconnect"); |
| 614 |
if (NULL == msg) { message(true, "DBUS Message Null, ALIEN INVASION\n"); exit(1); } |
| 615 |
dbus_message_iter_init_append(msg, &it); |
| 616 |
if (!dbus_message_iter_append_basic(&it, DBUS_TYPE_STRING, &(btdev->btid))) { |
| 617 |
message(true, "Out Of Memory!"); |
| 618 |
exit(1); |
| 619 |
} |
| 620 |
if (!dbus_connection_send(conn, msg, &serial)) { |
| 621 |
message(true, "Out Of Memory!"); |
| 622 |
exit(1); |
| 623 |
} |
| 624 |
|
| 625 |
//dbus_connection_flush(conn); |
| 626 |
dbus_message_unref(msg); |
| 627 |
message(true, "Signal sent (%s, %s)\n", inrange?"true":"false", btdev->btid); |
| 628 |
} |
| 629 |
|
| 630 |
void handle_dbus_message(DBusMessage* msg, int threshold) |
| 631 |
{ |
| 632 |
DBusMessage* reply; |
| 633 |
DBusMessageIter it; |
| 634 |
dbus_uint32_t serial, level; |
| 635 |
dbus_bool_t stat; |
| 636 |
char* address; |
| 637 |
|
| 638 |
if (!dbus_message_iter_init(msg, &it)) { |
| 639 |
message(true, "Bad parameter count for Status message"); |
| 640 |
return; |
| 641 |
} |
| 642 |
if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&it)) { |
| 643 |
message(true, "Bad parameter type (%d) for Status message", dbus_message_iter_get_arg_type(&it)); |
| 644 |
return; |
| 645 |
} |
| 646 |
dbus_message_iter_get_basic(&it, &address); |
| 647 |
message(true, "Handling dbus status query for %d\n", address); |
| 648 |
reply = dbus_message_new_method_return(msg); |
| 649 |
if (NULL == reply) { |
| 650 |
message(true, "Reply is NULL!! You have been eaten by a GRUE!"); |
| 651 |
return; |
| 652 |
} |
| 653 |
dbus_message_iter_init_append(reply, &it); |
| 654 |
btdev_t* btdev = testdev(args.btdevroot, opts.btdevroot, "No Devices Specified"); |
| 655 |
for (; NULL != btdev; btdev = btdev->next) { |
| 656 |
if (0 == strncmp("", address, 1) || 0 == strncasecmp(btdev->btid, address, strlen(btdev->btid))) { |
| 657 |
if (!dbus_message_iter_append_basic(&it, DBUS_TYPE_STRING, &(btdev->btid))) { |
| 658 |
message(true, "Out Of Memory! Lock me in the cellar and feed me pins, PINS!!"); |
| 659 |
dbus_message_unref(reply); |
| 660 |
return; |
| 661 |
} |
| 662 |
if (btdev->track >= threshold) { |
| 663 |
stat = TRUE; |
| 664 |
level = btdev->track; |
| 665 |
} else { |
| 666 |
stat = FALSE; |
| 667 |
level = -1; |
| 668 |
} |
| 669 |
break; |
| 670 |
} else { |
| 671 |
//address = ""; |
| 672 |
stat = FALSE; |
| 673 |
level = -1; |
| 674 |
dbus_message_iter_append_basic(&it, DBUS_TYPE_STRING, &address); |
| 675 |
} |
| 676 |
} |
| 677 |
if (!dbus_message_iter_append_basic(&it, DBUS_TYPE_BOOLEAN, &stat)){ |
| 678 |
message(true, "Out Of Memory! The bells! the bells!"); |
| 679 |
dbus_message_unref(reply); |
| 680 |
return; |
| 681 |
} |
| 682 |
|
| 683 |
if (!dbus_message_iter_append_basic(&it, DBUS_TYPE_UINT32, &level)){ |
| 684 |
message(true, "Out Of Memory! Every night the steam part of my brain and feed it to their mum!"); |
| 685 |
dbus_message_unref(reply); |
| 686 |
return; |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
if (!dbus_connection_send(conn, reply, &serial)){ |
| 691 |
message(true, "Out Of Memory! Universe error, redo from start!"); |
| 692 |
dbus_message_unref(reply); |
| 693 |
return; |
| 694 |
} |
| 695 |
dbus_connection_flush(conn); |
| 696 |
dbus_message_unref(reply); |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
void handle_introspection_message(DBusMessage* msg) |
| 701 |
{ |
| 702 |
DBusMessage* reply; |
| 703 |
DBusMessageIter it; |
| 704 |
dbus_uint32_t serial; |
| 705 |
char* xml = DBUS_INTROSPECTION_DATA; |
| 706 |
|
| 707 |
message(true, "Handling dbus introspection query\n"); |
| 708 |
reply = dbus_message_new_method_return(msg); |
| 709 |
if (NULL == reply) { |
| 710 |
message(true, "Reply is NULL!! You have been eaten by a GRUE!"); |
| 711 |
return; |
| 712 |
} |
| 713 |
dbus_message_iter_init_append(reply, &it); |
| 714 |
if (!dbus_message_iter_append_basic(&it, DBUS_TYPE_STRING, &xml)){ |
| 715 |
message(true, "Out Of Memory! The bells! the bells!"); |
| 716 |
dbus_message_unref(reply); |
| 717 |
return; |
| 718 |
} |
| 719 |
|
| 720 |
if (!dbus_connection_send(conn, reply, &serial)){ |
| 721 |
message(true, "Out Of Memory! Universe error, redo from start!"); |
| 722 |
dbus_message_unref(reply); |
| 723 |
return; |
| 724 |
} |
| 725 |
dbus_connection_flush(conn); |
| 726 |
dbus_message_unref(reply); |
| 727 |
} |
| 728 |
|
| 729 |
int main(int argc, char ** argv) |
| 730 |
{ |
| 731 |
int dev_id, pid; |
| 732 |
int track, threshold, interval; |
| 733 |
char* btid; |
| 734 |
int ret; |
| 735 |
int lq = 0; |
| 736 |
bool run = true; |
| 737 |
bool foo = false; |
| 738 |
DBusMessage* msg; |
| 739 |
DBusMessage* err; |
| 740 |
dbus_uint32_t serial; |
| 741 |
|
| 742 |
progname = argv[0]; |
| 743 |
|
| 744 |
readargs(argc, argv, &args); |
| 745 |
readoptions(args, &opts); |
| 746 |
|
| 747 |
// fork |
| 748 |
if (args.fork && opts.fork) { |
| 749 |
pid = fork(); |
| 750 |
if (pid < 0) |
| 751 |
printf("Cannot fork"); |
| 752 |
if (pid > 0) |
| 753 |
exit(0); |
| 754 |
else { |
| 755 |
setsid(); |
| 756 |
chdir("/"); |
| 757 |
umask(0); |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
writepidfile(); |
| 762 |
|
| 763 |
// syslog |
| 764 |
if (!(args.stdout || opts.stdout)) |
| 765 |
openlog("bluetooth-monitor",0, LOG_DAEMON); |
| 766 |
|
| 767 |
message(false, "Bluetooth monitoring system startup, version %s\n", VERSION); |
| 768 |
|
| 769 |
// register cleanup code |
| 770 |
signal(SIGTERM, sighandle); |
| 771 |
signal(SIGINT, sighandle); |
| 772 |
signal(SIGHUP, sighandle); |
| 773 |
|
| 774 |
conn = register_dbus(); |
| 775 |
|
| 776 |
threshold = testint(args.threshold, opts.threshold, "No Link Quality Threshold Specified"); |
| 777 |
|
| 778 |
btdev_t* btdev = testdev(args.btdevroot, opts.btdevroot, "No Devices Specified"); |
| 779 |
for (; NULL != btdev; btdev = btdev->next) { |
| 780 |
str2ba(btdev->btid, &(btdev->bdaddr)); |
| 781 |
|
| 782 |
message(true, "Connecting to address %x:%x:%x:%x:%x:%x\n", btdev->bdaddr.b[5], btdev->bdaddr.b[4], btdev->bdaddr.b[3], btdev->bdaddr.b[2], btdev->bdaddr.b[1], btdev->bdaddr.b[0]); |
| 783 |
dev_id = hci_get_route(&(btdev->bdaddr)); |
| 784 |
|
| 785 |
if (dev_id < 0) { |
| 786 |
message(false, "Device is not available.\n"); |
| 787 |
exit(1); |
| 788 |
} |
| 789 |
|
| 790 |
btdev->dd = hci_open_dev(dev_id); |
| 791 |
if (btdev->dd < 0) { |
| 792 |
message(false, "HCI device open failed\n"); |
| 793 |
exit(1); |
| 794 |
} |
| 795 |
|
| 796 |
foo = btconnect(btdev); |
| 797 |
if (!btdev->connected) |
| 798 |
message(true, "Using existing connection\n"); |
| 799 |
if (!foo) lq = -1; |
| 800 |
|
| 801 |
message(true, "Monitoring link quality\n"); |
| 802 |
btdev->track = get_link_qual(btdev); |
| 803 |
if (btdev->track >= threshold) |
| 804 |
sendsignal(true, btdev); |
| 805 |
} |
| 806 |
|
| 807 |
// drop privs |
| 808 |
if ((args.uid || opts.uid) && (args.gid || opts.gid)) |
| 809 |
setreuid(args.uid?args.uid:opts.uid, args.gid?args.gid:opts.gid); |
| 810 |
|
| 811 |
while (run) { |
| 812 |
threshold = testint(args.threshold, opts.threshold, "No Link Quality Threshold Specified"); |
| 813 |
interval = testint(args.interval, opts.interval, "No Interval Specified"); |
| 814 |
|
| 815 |
btdev_t* btdev = testdev(args.btdevroot, opts.btdevroot, "No Devices Specified"); |
| 816 |
for (; NULL != btdev; btdev = btdev->next) { |
| 817 |
// if device disconnects every 54s check before reconnect statement |
| 818 |
if (args.disconnecthack || opts.disconnecthack) |
| 819 |
lq = get_link_qual(btdev); |
| 820 |
if (-1 == lq) |
| 821 |
btconnect(btdev); |
| 822 |
|
| 823 |
lq = get_link_qual(btdev); |
| 824 |
|
| 825 |
if (lq >= threshold && btdev->track < threshold) { |
| 826 |
// good quality, stop the screensaver |
| 827 |
sendsignal(true, btdev); |
| 828 |
message(false, "Device %s Connected\n", btdev->btid); |
| 829 |
} |
| 830 |
else if (lq < threshold && btdev->track >= threshold) { |
| 831 |
// bad quality, start the screensaver |
| 832 |
sendsignal(false, btdev); |
| 833 |
message(false, "Device %s Disconnected\n", btdev->btid); |
| 834 |
} |
| 835 |
btdev->track = lq; |
| 836 |
} |
| 837 |
|
| 838 |
dbus_connection_read_write(conn, interval); |
| 839 |
while (NULL != (msg = dbus_connection_pop_message(conn))) { |
| 840 |
if (dbus_message_is_method_call(msg, "cx.ath.matthew.bluemon.Bluemon", "Status")) |
| 841 |
handle_dbus_message(msg, threshold); |
| 842 |
else if (dbus_message_is_method_call(msg, "org.freedesktop.DBus.Introspectable", "Introspect")) |
| 843 |
handle_introspection_message(msg); |
| 844 |
else if (dbus_message_is_method_call(msg, "org.freedesktop.DBus.Peer", "Ping")) { |
| 845 |
err = dbus_message_new_method_return (msg); |
| 846 |
dbus_connection_send(conn, err, &serial); |
| 847 |
dbus_message_unref(err); |
| 848 |
} |
| 849 |
else if (DBUS_MESSAGE_TYPE_METHOD_CALL == dbus_message_get_type(msg)) { |
| 850 |
err = dbus_message_new_error (msg, "cx.ath.matthew.bluemon.error", "Unknown Method"); |
| 851 |
dbus_connection_send(conn, err, &serial); |
| 852 |
dbus_message_unref(err); |
| 853 |
} |
| 854 |
dbus_message_unref(msg); |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
btdev = testdev(args.btdevroot, opts.btdevroot, "No Devices Specified"); |
| 859 |
while (NULL != btdev) { |
| 860 |
if (btdev->connected) { |
| 861 |
// disconnect bluetooth |
| 862 |
btdisconnect(btdev); |
| 863 |
if (NULL != conn) |
| 864 |
sendsignal(false, btdev); |
| 865 |
message(true, "Disconnected %s\n", btdev->btid); |
| 866 |
} |
| 867 |
hci_close_dev(btdev->dd); |
| 868 |
btdev_t* temp = btdev->next; |
| 869 |
free(btdev); |
| 870 |
btdev = temp; |
| 871 |
} |
| 872 |
// disconnect socket |
| 873 |
if (NULL != conn) |
| 874 |
dbus_connection_unref(conn); |
| 875 |
} |
| 876 |
|