device.c revision 632a9432774ff3a0c6e556e8f32a565b38890767
1/* 2 * 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2000-2001 Qualcomm Incorporated 6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com> 7 * Copyright (C) 2002-2005 Marcel Holtmann <marcel@holtmann.org> 8 * 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 * 24 */ 25 26#ifdef HAVE_CONFIG_H 27#include <config.h> 28#endif 29 30#include <stdio.h> 31#include <errno.h> 32#include <fcntl.h> 33#include <unistd.h> 34#include <stdlib.h> 35#include <string.h> 36#include <signal.h> 37#include <syslog.h> 38#include <sys/time.h> 39#include <sys/stat.h> 40#include <sys/ioctl.h> 41#include <sys/socket.h> 42 43#include <bluetooth/bluetooth.h> 44#include <bluetooth/hci.h> 45#include <bluetooth/hci_lib.h> 46 47#include "glib-ectomy.h" 48 49#include "hcid.h" 50#include "lib.h" 51 52struct hcid_opts hcid; 53struct device_opts default_device; 54struct device_opts *parser_device; 55static struct device_list *device_list = NULL; 56 57static GMainLoop *event_loop; 58 59static void usage(void) 60{ 61 printf("hcid - HCI daemon ver %s\n", VERSION); 62 printf("Usage: \n"); 63 printf("\thcid [-n not_daemon] [-f config file]\n"); 64} 65 66static inline void init_device_defaults(struct device_opts *device_opts) 67{ 68 memset(device_opts, 0, sizeof(*device_opts)); 69 device_opts->scan = SCAN_PAGE | SCAN_INQUIRY; 70 device_opts->name = strdup("BlueZ"); 71} 72 73struct device_opts *alloc_device_opts(char *ref) 74{ 75 struct device_list *device; 76 77 device = malloc(sizeof(struct device_list)); 78 if (!device) { 79 syslog(LOG_INFO, "Can't allocate devlist opts buffer: %s (%d)", 80 strerror(errno), errno); 81 exit(1); 82 } 83 84 device->ref = ref; 85 device->next = device_list; 86 device_list = device; 87 88 memcpy(&device->opts, &default_device, sizeof(struct device_opts)); 89 device->opts.name = strdup(default_device.name); 90 91 return &device->opts; 92} 93 94static void free_device_opts(void) 95{ 96 struct device_list *device, *next; 97 98 if (default_device.name) { 99 free(default_device.name); 100 default_device.name = NULL; 101 } 102 103 for (device = device_list; device; device = next) { 104 free(device->ref); 105 if (device->opts.name) 106 free(device->opts.name); 107 next = device->next; 108 free(device); 109 } 110 111 device_list = NULL; 112} 113 114static inline struct device_opts *find_device_opts(char *ref) 115{ 116 struct device_list *device; 117 118 for (device = device_list; device; device = device->next) 119 if (!strcmp(ref, device->ref)) 120 return &device->opts; 121 122 return NULL; 123} 124 125static struct device_opts *get_device_opts(int sock, int hdev) 126{ 127 struct device_opts *device_opts = NULL; 128 struct hci_dev_info di; 129 130 /* First try to get BD_ADDR based settings ... */ 131 di.dev_id = hdev; 132 if (!ioctl(sock, HCIGETDEVINFO, (void *) &di)) { 133 char addr[18]; 134 ba2str(&di.bdaddr, addr); 135 device_opts = find_device_opts(addr); 136 } 137 138 /* ... then try HCI based settings ... */ 139 if (!device_opts) { 140 char ref[8]; 141 snprintf(ref, sizeof(ref) - 1, "hci%d", hdev); 142 device_opts = find_device_opts(ref); 143 } 144 145 /* ... and last use the default settings. */ 146 if (!device_opts) 147 device_opts = &default_device; 148 149 return device_opts; 150} 151 152static void configure_device(int hdev) 153{ 154 struct device_opts *device_opts; 155 struct hci_dev_req dr; 156 struct hci_dev_info di; 157 int s; 158 159 /* Do configuration in the separate process */ 160 switch (fork()) { 161 case 0: 162 break; 163 case -1: 164 syslog(LOG_ERR, "Fork failed. Can't init device hci%d: %s (%d)", 165 hdev, strerror(errno), errno); 166 default: 167 return; 168 } 169 170 set_title("hci%d config", hdev); 171 172 if ((s = hci_open_dev(hdev)) < 0) { 173 syslog(LOG_ERR, "Can't open device hci%d: %s (%d)", 174 hdev, strerror(errno), errno); 175 exit(1); 176 } 177 178 di.dev_id = hdev; 179 if (ioctl(s, HCIGETDEVINFO, (void *) &di) < 0) 180 exit(1); 181 182 if (hci_test_bit(HCI_RAW, &di.flags)) 183 exit(0); 184 185 dr.dev_id = hdev; 186 device_opts = get_device_opts(s, hdev); 187 188 /* Set scan mode */ 189 dr.dev_opt = device_opts->scan; 190 if (ioctl(s, HCISETSCAN, (unsigned long) &dr) < 0) { 191 syslog(LOG_ERR, "Can't set scan mode on hci%d: %s (%d)", 192 hdev, strerror(errno), errno); 193 } 194 195 /* Set authentication */ 196 if (device_opts->auth) 197 dr.dev_opt = AUTH_ENABLED; 198 else 199 dr.dev_opt = AUTH_DISABLED; 200 201 if (ioctl(s, HCISETAUTH, (unsigned long) &dr) < 0) { 202 syslog(LOG_ERR, "Can't set auth on hci%d: %s (%d)", 203 hdev, strerror(errno), errno); 204 } 205 206 /* Set encryption */ 207 if (device_opts->encrypt) 208 dr.dev_opt = ENCRYPT_P2P; 209 else 210 dr.dev_opt = ENCRYPT_DISABLED; 211 212 if (ioctl(s, HCISETENCRYPT, (unsigned long) &dr) < 0) { 213 syslog(LOG_ERR, "Can't set encrypt on hci%d: %s (%d)", 214 hdev, strerror(errno), errno); 215 } 216 217 /* Set device name */ 218 if ((device_opts->flags & (1 << HCID_SET_NAME)) && device_opts->name) { 219 change_local_name_cp cp; 220 write_ext_inquiry_response_cp ip; 221 uint8_t len; 222 223 memset(cp.name, 0, sizeof(cp.name)); 224 expand_name((char *) cp.name, sizeof(cp.name), device_opts->name, hdev); 225 226 ip.fec = 0x00; 227 memset(ip.data, 0, sizeof(ip.data)); 228 len = strlen((char *) cp.name); 229 if (len > 48) { 230 len = 48; 231 ip.data[1] = 0x08; 232 } else 233 ip.data[1] = 0x09; 234 ip.data[0] = len + 1; 235 memcpy(ip.data + 2, cp.name, len); 236 237 hci_send_cmd(s, OGF_HOST_CTL, OCF_CHANGE_LOCAL_NAME, 238 CHANGE_LOCAL_NAME_CP_SIZE, &cp); 239 240 if (di.features[6] & LMP_EXT_INQ) 241 hci_send_cmd(s, OGF_HOST_CTL, OCF_WRITE_EXT_INQUIRY_RESPONSE, 242 WRITE_EXT_INQUIRY_RESPONSE_CP_SIZE, &ip); 243 } 244 245 /* Set device class */ 246 if ((device_opts->flags & (1 << HCID_SET_CLASS))) { 247 uint32_t class = htobl(device_opts->class); 248 write_class_of_dev_cp cp; 249 250 memcpy(cp.dev_class, &class, 3); 251 hci_send_cmd(s, OGF_HOST_CTL, OCF_WRITE_CLASS_OF_DEV, 252 WRITE_CLASS_OF_DEV_CP_SIZE, &cp); 253 } 254 255 /* Set voice setting */ 256 if ((device_opts->flags & (1 << HCID_SET_VOICE))) { 257 write_voice_setting_cp cp; 258 259 cp.voice_setting = htobl(device_opts->voice); 260 hci_send_cmd(s, OGF_HOST_CTL, OCF_WRITE_VOICE_SETTING, 261 WRITE_VOICE_SETTING_CP_SIZE, &cp); 262 } 263 264 /* Set inquiry mode */ 265 if ((device_opts->flags & (1 << HCID_SET_INQMODE))) { 266 write_inquiry_mode_cp cp; 267 268 switch (device_opts->inqmode) { 269 case 2: 270 if (di.features[6] & LMP_EXT_INQ) { 271 cp.mode = 2; 272 break; 273 } 274 case 1: 275 if (di.features[3] & LMP_RSSI_INQ) { 276 cp.mode = 1; 277 break; 278 } 279 default: 280 cp.mode = 0; 281 break; 282 } 283 284 hci_send_cmd(s, OGF_HOST_CTL, OCF_WRITE_INQUIRY_MODE, 285 WRITE_INQUIRY_MODE_CP_SIZE, &cp); 286 } 287 288 /* Set page timeout */ 289 if ((device_opts->flags & (1 << HCID_SET_PAGETO))) { 290 write_page_timeout_cp cp; 291 292 cp.timeout = htobs(device_opts->pageto); 293 hci_send_cmd(s, OGF_HOST_CTL, OCF_WRITE_PAGE_TIMEOUT, 294 WRITE_PAGE_TIMEOUT_CP_SIZE, &cp); 295 } 296 297 exit(0); 298} 299 300static void init_device(int hdev) 301{ 302 struct device_opts *device_opts; 303 struct hci_dev_req dr; 304 struct hci_dev_info di; 305 int s; 306 307 /* Do initialization in the separate process */ 308 switch (fork()) { 309 case 0: 310 break; 311 case -1: 312 syslog(LOG_ERR, "Fork failed. Can't init device hci%d: %s (%d)", 313 hdev, strerror(errno), errno); 314 default: 315 return; 316 } 317 318 set_title("hci%d init", hdev); 319 320 if ((s = hci_open_dev(hdev)) < 0) { 321 syslog(LOG_ERR, "Can't open device hci%d: %s (%d)", 322 hdev, strerror(errno), errno); 323 exit(1); 324 } 325 326 /* Start HCI device */ 327 if (ioctl(s, HCIDEVUP, hdev) < 0 && errno != EALREADY) { 328 syslog(LOG_ERR, "Can't init device hci%d: %s (%d)", 329 hdev, strerror(errno), errno); 330 exit(1); 331 } 332 333 di.dev_id = hdev; 334 if (ioctl(s, HCIGETDEVINFO, (void *) &di) < 0) 335 exit(1); 336 337 if (hci_test_bit(HCI_RAW, &di.flags)) 338 exit(0); 339 340 dr.dev_id = hdev; 341 device_opts = get_device_opts(s, hdev); 342 343 /* Set packet type */ 344 if ((device_opts->flags & (1 << HCID_SET_PTYPE))) { 345 dr.dev_opt = device_opts->pkt_type; 346 if (ioctl(s, HCISETPTYPE, (unsigned long) &dr) < 0) { 347 syslog(LOG_ERR, "Can't set packet type on hci%d: %s (%d)", 348 hdev, strerror(errno), errno); 349 } 350 } 351 352 /* Set link mode */ 353 if ((device_opts->flags & (1 << HCID_SET_LM))) { 354 dr.dev_opt = device_opts->link_mode; 355 if (ioctl(s, HCISETLINKMODE, (unsigned long) &dr) < 0) { 356 syslog(LOG_ERR, "Can't set link mode on hci%d: %s (%d)", 357 hdev, strerror(errno), errno); 358 } 359 } 360 361 /* Set link policy */ 362 if ((device_opts->flags & (1 << HCID_SET_LP))) { 363 dr.dev_opt = device_opts->link_policy; 364 if (ioctl(s, HCISETLINKPOL, (unsigned long) &dr) < 0) { 365 syslog(LOG_ERR, "Can't set link policy on hci%d: %s (%d)", 366 hdev, strerror(errno), errno); 367 } 368 } 369 370 exit(0); 371} 372 373static void init_all_devices(int ctl) 374{ 375 struct hci_dev_list_req *dl; 376 struct hci_dev_req *dr; 377 int i; 378 379 if (!(dl = malloc(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t)))) { 380 syslog(LOG_INFO, "Can't allocate devlist buffer: %s (%d)", 381 strerror(errno), errno); 382 exit(1); 383 } 384 dl->dev_num = HCI_MAX_DEV; 385 dr = dl->dev_req; 386 387 if (ioctl(ctl, HCIGETDEVLIST, (void *) dl) < 0) { 388 syslog(LOG_INFO, "Can't get device list: %s (%d)", 389 strerror(errno), errno); 390 exit(1); 391 } 392 393 for (i = 0; i < dl->dev_num; i++, dr++) { 394 if (hcid.auto_init) 395 init_device(dr->dev_id); 396 397 if (hcid.auto_init && hci_test_bit(HCI_UP, &dr->dev_opt)) 398 configure_device(dr->dev_id); 399 400 if (hcid.security && hci_test_bit(HCI_UP, &dr->dev_opt)) 401 start_security_manager(dr->dev_id); 402 403#ifdef ENABLE_DBUS 404 if (hci_test_bit(HCI_UP, &dr->dev_opt)) 405 hcid_dbus_register_manager(dr->dev_id); 406 407 hcid_dbus_register_device(dr->dev_id); 408#endif 409 } 410 411 free(dl); 412} 413 414static void init_defaults(void) 415{ 416 hcid.auto_init = 0; 417 hcid.security = 0; 418 419 init_device_defaults(&default_device); 420} 421 422static void sig_usr1(int sig) 423{ 424 toggle_pairing(0); 425} 426 427static void sig_usr2(int sig) 428{ 429 toggle_pairing(1); 430} 431 432static void sig_term(int sig) 433{ 434 g_main_quit(event_loop); 435} 436 437static void sig_hup(int sig) 438{ 439 syslog(LOG_INFO, "Reloading config file"); 440 441 init_defaults(); 442 443 if (read_config(hcid.config_file) < 0) 444 syslog(LOG_ERR, "Config reload failed"); 445 446 init_security_data(); 447 448 init_all_devices(hcid.sock); 449} 450 451static inline void device_event(GIOChannel *chan, evt_stack_internal *si) 452{ 453 evt_si_device *sd = (void *) &si->data; 454 455 switch (sd->event) { 456 case HCI_DEV_REG: 457 syslog(LOG_INFO, "HCI dev %d registered", sd->dev_id); 458 if (hcid.auto_init) 459 init_device(sd->dev_id); 460#ifdef ENABLE_DBUS 461 hcid_dbus_register_device(sd->dev_id); 462#endif 463 break; 464 465 case HCI_DEV_UNREG: 466 syslog(LOG_INFO, "HCI dev %d unregistered", sd->dev_id); 467#ifdef ENABLE_DBUS 468 hcid_dbus_unregister_device(sd->dev_id); 469#endif 470 break; 471 472 case HCI_DEV_UP: 473 syslog(LOG_INFO, "HCI dev %d up", sd->dev_id); 474 if (hcid.auto_init) 475 configure_device(sd->dev_id); 476 if (hcid.security) 477 start_security_manager(sd->dev_id); 478#ifdef ENABLE_DBUS 479 hcid_dbus_register_manager(sd->dev_id); 480#endif 481 break; 482 483 case HCI_DEV_DOWN: 484 syslog(LOG_INFO, "HCI dev %d down", sd->dev_id); 485 if (hcid.security) 486 stop_security_manager(sd->dev_id); 487#ifdef ENABLE_DBUS 488 hcid_dbus_unregister_manager(sd->dev_id); 489#endif 490 break; 491 } 492} 493 494static gboolean io_stack_event(GIOChannel *chan, GIOCondition cond, gpointer data) 495{ 496 unsigned char buf[HCI_MAX_FRAME_SIZE], *ptr; 497 evt_stack_internal *si; 498 hci_event_hdr *eh; 499 int type; 500 size_t len; 501 GIOError err; 502 503 ptr = buf; 504 505 if ((err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len))) { 506 if (err == G_IO_ERROR_AGAIN) 507 return TRUE; 508 509 syslog(LOG_ERR, "Read from control socket failed: %s (%d)", 510 strerror(errno), errno); 511 g_main_quit(event_loop); 512 return FALSE; 513 } 514 515 type = *ptr++; 516 517 if (type != HCI_EVENT_PKT) 518 return TRUE; 519 520 eh = (hci_event_hdr *) ptr; 521 if (eh->evt != EVT_STACK_INTERNAL) 522 return TRUE; 523 524 ptr += HCI_EVENT_HDR_SIZE; 525 526 si = (evt_stack_internal *) ptr; 527 switch (si->type) { 528 case EVT_SI_DEVICE: 529 device_event(chan, si); 530 break; 531 } 532 533 return TRUE; 534} 535 536extern int optind, opterr, optopt; 537extern char *optarg; 538 539int main(int argc, char *argv[], char *env[]) 540{ 541 int daemon, dofork, opt, fd; 542 struct sockaddr_hci addr; 543 struct hci_filter flt; 544 struct sigaction sa; 545 GIOChannel *ctl_io; 546 547 daemon = 1; dofork = 1; 548 549 /* Default HCId settings */ 550 hcid.config_file = HCID_CONFIG_FILE; 551 hcid.host_name = get_host_name(); 552 hcid.security = HCID_SEC_AUTO; 553 hcid.pairing = HCID_PAIRING_MULTI; 554 555 hcid.pin_file = strdup(HCID_PIN_FILE); 556 hcid.pin_helper = strdup(HCID_PIN_HELPER); 557 hcid.key_file = strdup(HCID_KEY_FILE); 558 559 init_defaults(); 560 561 while ((opt = getopt(argc, argv, "f:n")) != EOF) { 562 switch (opt) { 563 case 'n': 564 daemon = 0; 565 break; 566 567 case 'f': 568 hcid.config_file = strdup(optarg); 569 break; 570 571 default: 572 usage(); 573 exit(1); 574 } 575 } 576 577 if (daemon) { 578 if (dofork && fork()) 579 exit(0); 580 581 /* Direct stdin,stdout,stderr to '/dev/null' */ 582 fd = open("/dev/null", O_RDWR); 583 dup2(fd, 0); dup2(fd, 1); dup2(fd, 2); 584 close(fd); 585 586 setsid(); 587 588 chdir("/"); 589 } 590 591 umask(0077); 592 593 init_title(argc, argv, env, "hcid: "); 594 set_title("initializing"); 595 596 /* Start logging to syslog and stderr */ 597 openlog("hcid", LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON); 598 syslog(LOG_INFO, "Bluetooth HCI daemon"); 599 600 memset(&sa, 0, sizeof(sa)); 601 sa.sa_flags = SA_NOCLDSTOP; 602 sa.sa_handler = sig_term; 603 sigaction(SIGTERM, &sa, NULL); 604 sigaction(SIGINT, &sa, NULL); 605 sa.sa_handler = sig_hup; 606 sigaction(SIGHUP, &sa, NULL); 607 sa.sa_handler = sig_usr1; 608 sigaction(SIGUSR1, &sa, NULL); 609 sa.sa_handler = sig_usr2; 610 sigaction(SIGUSR2, &sa, NULL); 611 612 sa.sa_handler = SIG_IGN; 613 sigaction(SIGCHLD, &sa, NULL); 614 sigaction(SIGPIPE, &sa, NULL); 615 616 /* Create and bind HCI socket */ 617 if ((hcid.sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) { 618 syslog(LOG_ERR, "Can't open HCI socket: %s (%d)", 619 strerror(errno), errno); 620 exit(1); 621 } 622 623 /* Set filter */ 624 hci_filter_clear(&flt); 625 hci_filter_set_ptype(HCI_EVENT_PKT, &flt); 626 hci_filter_set_event(EVT_STACK_INTERNAL, &flt); 627 if (setsockopt(hcid.sock, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) { 628 syslog(LOG_ERR, "Can't set filter: %s (%d)", 629 strerror(errno), errno); 630 exit(1); 631 } 632 633 addr.hci_family = AF_BLUETOOTH; 634 addr.hci_dev = HCI_DEV_NONE; 635 if (bind(hcid.sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 636 syslog(LOG_ERR, "Can't bind HCI socket: %s (%d)", 637 strerror(errno), errno); 638 exit(1); 639 } 640 641 if (read_config(hcid.config_file) < 0) 642 syslog(LOG_ERR, "Config load failed"); 643 644#ifdef ENABLE_DBUS 645 if (hcid_dbus_init() == FALSE && hcid.dbus_pin_helper) { 646 syslog(LOG_ERR, "Unable to get on D-BUS"); 647 exit(1); 648 } 649#else 650 if (hcid.dbus_pin_helper) { 651 syslog(LOG_ERR, "D-BUS not configured in this build of hcid"); 652 exit(1); 653 } 654#endif 655 656 init_security_data(); 657 658 /* Create event loop */ 659 event_loop = g_main_new(FALSE); 660 661 /* Initialize already connected devices */ 662 init_all_devices(hcid.sock); 663 664 set_title("processing events"); 665 666 ctl_io = g_io_channel_unix_new(hcid.sock); 667 g_io_add_watch(ctl_io, G_IO_IN, io_stack_event, NULL); 668 669 /* Start event processor */ 670 g_main_run(event_loop); 671 672 free_device_opts(); 673 674#ifdef ENABLE_DBUS 675 hcid_dbus_exit(); 676#endif 677 678 syslog(LOG_INFO, "Exit."); 679 return 0; 680} 681