bt.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Convenience functions for bluetooth.
  3. *
  4. * Copyright (C) 2008 Andrzej Zaborowski <balrog@zabor.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 or
  9. * (at your option) version 3 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "qemu-common.h"
  21. #include "net.h"
  22. #include "bt.h"
  23. /* Slave implementations can ignore this */
  24. static void bt_dummy_lmp_mode_change(struct bt_link_s *link)
  25. {
  26. }
  27. /* Slaves should never receive these PDUs */
  28. static void bt_dummy_lmp_connection_complete(struct bt_link_s *link)
  29. {
  30. if (link->slave->reject_reason)
  31. fprintf(stderr, "%s: stray LMP_not_accepted received, fixme\n",
  32. __FUNCTION__);
  33. else
  34. fprintf(stderr, "%s: stray LMP_accepted received, fixme\n",
  35. __FUNCTION__);
  36. exit(-1);
  37. }
  38. static void bt_dummy_lmp_disconnect_master(struct bt_link_s *link)
  39. {
  40. fprintf(stderr, "%s: stray LMP_detach received, fixme\n", __FUNCTION__);
  41. exit(-1);
  42. }
  43. static void bt_dummy_lmp_acl_resp(struct bt_link_s *link,
  44. const uint8_t *data, int start, int len)
  45. {
  46. fprintf(stderr, "%s: stray ACL response PDU, fixme\n", __FUNCTION__);
  47. exit(-1);
  48. }
  49. /* Slaves that don't hold any additional per link state can use these */
  50. static void bt_dummy_lmp_connection_request(struct bt_link_s *req)
  51. {
  52. struct bt_link_s *link = qemu_mallocz(sizeof(struct bt_link_s));
  53. link->slave = req->slave;
  54. link->host = req->host;
  55. req->host->reject_reason = 0;
  56. req->host->lmp_connection_complete(link);
  57. }
  58. static void bt_dummy_lmp_disconnect_slave(struct bt_link_s *link)
  59. {
  60. qemu_free(link);
  61. }
  62. static void bt_dummy_destroy(struct bt_device_s *device)
  63. {
  64. bt_device_done(device);
  65. qemu_free(device);
  66. }
  67. static int bt_dev_idx = 0;
  68. void bt_device_init(struct bt_device_s *dev, struct bt_scatternet_s *net)
  69. {
  70. memset(dev, 0, sizeof(*dev));
  71. dev->inquiry_scan = 1;
  72. dev->page_scan = 1;
  73. dev->bd_addr.b[0] = bt_dev_idx & 0xff;
  74. dev->bd_addr.b[1] = bt_dev_idx >> 8;
  75. dev->bd_addr.b[2] = 0xd0;
  76. dev->bd_addr.b[3] = 0xba;
  77. dev->bd_addr.b[4] = 0xbe;
  78. dev->bd_addr.b[5] = 0xba;
  79. bt_dev_idx ++;
  80. /* Simple slave-only devices need to implement only .lmp_acl_data */
  81. dev->lmp_connection_complete = bt_dummy_lmp_connection_complete;
  82. dev->lmp_disconnect_master = bt_dummy_lmp_disconnect_master;
  83. dev->lmp_acl_resp = bt_dummy_lmp_acl_resp;
  84. dev->lmp_mode_change = bt_dummy_lmp_mode_change;
  85. dev->lmp_connection_request = bt_dummy_lmp_connection_request;
  86. dev->lmp_disconnect_slave = bt_dummy_lmp_disconnect_slave;
  87. dev->handle_destroy = bt_dummy_destroy;
  88. dev->net = net;
  89. dev->next = net->slave;
  90. net->slave = dev;
  91. }
  92. void bt_device_done(struct bt_device_s *dev)
  93. {
  94. struct bt_device_s **p = &dev->net->slave;
  95. while (*p && *p != dev)
  96. p = &(*p)->next;
  97. if (*p != dev) {
  98. fprintf(stderr, "%s: bad bt device \"%s\"\n", __FUNCTION__,
  99. dev->lmp_name ?: "(null)");
  100. exit(-1);
  101. }
  102. *p = dev->next;
  103. }