2
0

core.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/osdep.h"
  20. #include "qemu/error-report.h"
  21. #include "sysemu/bt.h"
  22. #include "hw/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. error_report("%s: stray LMP_not_accepted received, fixme", __func__);
  32. else
  33. error_report("%s: stray LMP_accepted received, fixme", __func__);
  34. exit(-1);
  35. }
  36. static void bt_dummy_lmp_disconnect_master(struct bt_link_s *link)
  37. {
  38. error_report("%s: stray LMP_detach received, fixme", __func__);
  39. exit(-1);
  40. }
  41. static void bt_dummy_lmp_acl_resp(struct bt_link_s *link,
  42. const uint8_t *data, int start, int len)
  43. {
  44. error_report("%s: stray ACL response PDU, fixme", __func__);
  45. exit(-1);
  46. }
  47. /* Slaves that don't hold any additional per link state can use these */
  48. static void bt_dummy_lmp_connection_request(struct bt_link_s *req)
  49. {
  50. struct bt_link_s *link = g_malloc0(sizeof(struct bt_link_s));
  51. link->slave = req->slave;
  52. link->host = req->host;
  53. req->host->reject_reason = 0;
  54. req->host->lmp_connection_complete(link);
  55. }
  56. static void bt_dummy_lmp_disconnect_slave(struct bt_link_s *link)
  57. {
  58. g_free(link);
  59. }
  60. static void bt_dummy_destroy(struct bt_device_s *device)
  61. {
  62. bt_device_done(device);
  63. g_free(device);
  64. }
  65. static int bt_dev_idx = 0;
  66. void bt_device_init(struct bt_device_s *dev, struct bt_scatternet_s *net)
  67. {
  68. memset(dev, 0, sizeof(*dev));
  69. dev->inquiry_scan = 1;
  70. dev->page_scan = 1;
  71. dev->bd_addr.b[0] = bt_dev_idx & 0xff;
  72. dev->bd_addr.b[1] = bt_dev_idx >> 8;
  73. dev->bd_addr.b[2] = 0xd0;
  74. dev->bd_addr.b[3] = 0xba;
  75. dev->bd_addr.b[4] = 0xbe;
  76. dev->bd_addr.b[5] = 0xba;
  77. bt_dev_idx ++;
  78. /* Simple slave-only devices need to implement only .lmp_acl_data */
  79. dev->lmp_connection_complete = bt_dummy_lmp_connection_complete;
  80. dev->lmp_disconnect_master = bt_dummy_lmp_disconnect_master;
  81. dev->lmp_acl_resp = bt_dummy_lmp_acl_resp;
  82. dev->lmp_mode_change = bt_dummy_lmp_mode_change;
  83. dev->lmp_connection_request = bt_dummy_lmp_connection_request;
  84. dev->lmp_disconnect_slave = bt_dummy_lmp_disconnect_slave;
  85. dev->handle_destroy = bt_dummy_destroy;
  86. dev->net = net;
  87. dev->next = net->slave;
  88. net->slave = dev;
  89. }
  90. void bt_device_done(struct bt_device_s *dev)
  91. {
  92. struct bt_device_s **p = &dev->net->slave;
  93. while (*p && *p != dev)
  94. p = &(*p)->next;
  95. if (*p != dev) {
  96. error_report("%s: bad bt device \"%s\"", __func__,
  97. dev->lmp_name ?: "(null)");
  98. exit(-1);
  99. }
  100. *p = dev->next;
  101. }
  102. static struct bt_vlan_s {
  103. struct bt_scatternet_s net;
  104. int id;
  105. struct bt_vlan_s *next;
  106. } *first_bt_vlan;
  107. /* find or alloc a new bluetooth "VLAN" */
  108. struct bt_scatternet_s *qemu_find_bt_vlan(int id)
  109. {
  110. struct bt_vlan_s **pvlan, *vlan;
  111. for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
  112. if (vlan->id == id)
  113. return &vlan->net;
  114. }
  115. vlan = g_malloc0(sizeof(struct bt_vlan_s));
  116. vlan->id = id;
  117. pvlan = &first_bt_vlan;
  118. while (*pvlan != NULL)
  119. pvlan = &(*pvlan)->next;
  120. *pvlan = vlan;
  121. return &vlan->net;
  122. }