bt.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu-common.h"
  20. #include "bt/bt.h"
  21. #include "bt.h"
  22. /* Slave implementations can ignore this */
  23. static void bt_dummy_lmp_mode_change(struct bt_link_s *link)
  24. {
  25. }
  26. /* Slaves should never receive these PDUs */
  27. static void bt_dummy_lmp_connection_complete(struct bt_link_s *link)
  28. {
  29. if (link->slave->reject_reason)
  30. fprintf(stderr, "%s: stray LMP_not_accepted received, fixme\n",
  31. __FUNCTION__);
  32. else
  33. fprintf(stderr, "%s: stray LMP_accepted received, fixme\n",
  34. __FUNCTION__);
  35. exit(-1);
  36. }
  37. static void bt_dummy_lmp_disconnect_master(struct bt_link_s *link)
  38. {
  39. fprintf(stderr, "%s: stray LMP_detach received, fixme\n", __FUNCTION__);
  40. exit(-1);
  41. }
  42. static void bt_dummy_lmp_acl_resp(struct bt_link_s *link,
  43. const uint8_t *data, int start, int len)
  44. {
  45. fprintf(stderr, "%s: stray ACL response PDU, fixme\n", __FUNCTION__);
  46. exit(-1);
  47. }
  48. /* Slaves that don't hold any additional per link state can use these */
  49. static void bt_dummy_lmp_connection_request(struct bt_link_s *req)
  50. {
  51. struct bt_link_s *link = g_malloc0(sizeof(struct bt_link_s));
  52. link->slave = req->slave;
  53. link->host = req->host;
  54. req->host->reject_reason = 0;
  55. req->host->lmp_connection_complete(link);
  56. }
  57. static void bt_dummy_lmp_disconnect_slave(struct bt_link_s *link)
  58. {
  59. g_free(link);
  60. }
  61. static void bt_dummy_destroy(struct bt_device_s *device)
  62. {
  63. bt_device_done(device);
  64. g_free(device);
  65. }
  66. static int bt_dev_idx = 0;
  67. void bt_device_init(struct bt_device_s *dev, struct bt_scatternet_s *net)
  68. {
  69. memset(dev, 0, sizeof(*dev));
  70. dev->inquiry_scan = 1;
  71. dev->page_scan = 1;
  72. dev->bd_addr.b[0] = bt_dev_idx & 0xff;
  73. dev->bd_addr.b[1] = bt_dev_idx >> 8;
  74. dev->bd_addr.b[2] = 0xd0;
  75. dev->bd_addr.b[3] = 0xba;
  76. dev->bd_addr.b[4] = 0xbe;
  77. dev->bd_addr.b[5] = 0xba;
  78. bt_dev_idx ++;
  79. /* Simple slave-only devices need to implement only .lmp_acl_data */
  80. dev->lmp_connection_complete = bt_dummy_lmp_connection_complete;
  81. dev->lmp_disconnect_master = bt_dummy_lmp_disconnect_master;
  82. dev->lmp_acl_resp = bt_dummy_lmp_acl_resp;
  83. dev->lmp_mode_change = bt_dummy_lmp_mode_change;
  84. dev->lmp_connection_request = bt_dummy_lmp_connection_request;
  85. dev->lmp_disconnect_slave = bt_dummy_lmp_disconnect_slave;
  86. dev->handle_destroy = bt_dummy_destroy;
  87. dev->net = net;
  88. dev->next = net->slave;
  89. net->slave = dev;
  90. }
  91. void bt_device_done(struct bt_device_s *dev)
  92. {
  93. struct bt_device_s **p = &dev->net->slave;
  94. while (*p && *p != dev)
  95. p = &(*p)->next;
  96. if (*p != dev) {
  97. fprintf(stderr, "%s: bad bt device \"%s\"\n", __FUNCTION__,
  98. dev->lmp_name ?: "(null)");
  99. exit(-1);
  100. }
  101. *p = dev->next;
  102. }