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