Statistics
| Branch: | Revision:

root / hw / bt.c @ 8af7a3ab

History | View | Annotate | Download (3.5 kB)

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