Statistics
| Branch: | Revision:

root / hw / bt.c @ 1ae26a18

History | View | Annotate | Download (3.5 kB)

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
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19
 * MA 02111-1307 USA
20
 */
21

    
22
#include "qemu-common.h"
23
#include "net.h"
24
#include "bt.h"
25

    
26
/* Slave implementations can ignore this */
27
static void bt_dummy_lmp_mode_change(struct bt_link_s *link)
28
{
29
}
30

    
31
/* Slaves should never receive these PDUs */
32
static void bt_dummy_lmp_connection_complete(struct bt_link_s *link)
33
{
34
    if (link->slave->reject_reason)
35
        fprintf(stderr, "%s: stray LMP_not_accepted received, fixme\n",
36
                        __FUNCTION__);
37
    else
38
        fprintf(stderr, "%s: stray LMP_accepted received, fixme\n",
39
                        __FUNCTION__);
40
    exit(-1);
41
}
42

    
43
static void bt_dummy_lmp_disconnect_master(struct bt_link_s *link)
44
{
45
    fprintf(stderr, "%s: stray LMP_detach received, fixme\n", __FUNCTION__);
46
    exit(-1);
47
}
48

    
49
static void bt_dummy_lmp_acl_resp(struct bt_link_s *link,
50
                const uint8_t *data, int start, int len)
51
{
52
    fprintf(stderr, "%s: stray ACL response PDU, fixme\n", __FUNCTION__);
53
    exit(-1);
54
}
55

    
56
/* Slaves that don't hold any additional per link state can use these */
57
static void bt_dummy_lmp_connection_request(struct bt_link_s *req)
58
{
59
    struct bt_link_s *link = qemu_mallocz(sizeof(struct bt_link_s));
60

    
61
    link->slave = req->slave;
62
    link->host = req->host;
63

    
64
    req->host->reject_reason = 0;
65
    req->host->lmp_connection_complete(link);
66
}
67

    
68
static void bt_dummy_lmp_disconnect_slave(struct bt_link_s *link)
69
{
70
    qemu_free(link);
71
}
72

    
73
static void bt_dummy_destroy(struct bt_device_s *device)
74
{
75
    bt_device_done(device);
76
    qemu_free(device);
77
}
78

    
79
static int bt_dev_idx = 0;
80

    
81
void bt_device_init(struct bt_device_s *dev, struct bt_scatternet_s *net)
82
{
83
    memset(dev, 0, sizeof(*dev));
84
    dev->inquiry_scan = 1;
85
    dev->page_scan = 1;
86

    
87
    dev->bd_addr.b[0] = bt_dev_idx & 0xff;
88
    dev->bd_addr.b[1] = bt_dev_idx >> 8;
89
    dev->bd_addr.b[2] = 0xd0;
90
    dev->bd_addr.b[3] = 0xba;
91
    dev->bd_addr.b[4] = 0xbe;
92
    dev->bd_addr.b[5] = 0xba;
93
    bt_dev_idx ++;
94

    
95
    /* Simple slave-only devices need to implement only .lmp_acl_data */
96
    dev->lmp_connection_complete = bt_dummy_lmp_connection_complete;
97
    dev->lmp_disconnect_master = bt_dummy_lmp_disconnect_master;
98
    dev->lmp_acl_resp = bt_dummy_lmp_acl_resp;
99
    dev->lmp_mode_change = bt_dummy_lmp_mode_change;
100
    dev->lmp_connection_request = bt_dummy_lmp_connection_request;
101
    dev->lmp_disconnect_slave = bt_dummy_lmp_disconnect_slave;
102

    
103
    dev->handle_destroy = bt_dummy_destroy;
104

    
105
    dev->net = net;
106
    dev->next = net->slave;
107
    net->slave = dev;
108
}
109

    
110
void bt_device_done(struct bt_device_s *dev)
111
{
112
    struct bt_device_s **p = &dev->net->slave;
113

    
114
    while (*p && *p != dev)
115
        p = &(*p)->next;
116
    if (*p != dev) {
117
        fprintf(stderr, "%s: bad bt device \"%s\"\n", __FUNCTION__,
118
                        dev->lmp_name ?: "(null)");
119
        exit(-1);
120
    }
121

    
122
    *p = dev->next;
123
}