hci_misc.c revision 1.1.2.3 1 1.1.2.3 yamt /* $NetBSD: hci_misc.c,v 1.1.2.3 2007/10/27 11:36:06 yamt Exp $ */
2 1.1.2.2 yamt
3 1.1.2.2 yamt /*-
4 1.1.2.2 yamt * Copyright (c) 2005 Iain Hibbert.
5 1.1.2.2 yamt * Copyright (c) 2006 Itronix Inc.
6 1.1.2.2 yamt * All rights reserved.
7 1.1.2.2 yamt *
8 1.1.2.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.1.2.2 yamt * modification, are permitted provided that the following conditions
10 1.1.2.2 yamt * are met:
11 1.1.2.2 yamt * 1. Redistributions of source code must retain the above copyright
12 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer.
13 1.1.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer in the
15 1.1.2.2 yamt * documentation and/or other materials provided with the distribution.
16 1.1.2.2 yamt * 3. The name of Itronix Inc. may not be used to endorse
17 1.1.2.2 yamt * or promote products derived from this software without specific
18 1.1.2.2 yamt * prior written permission.
19 1.1.2.2 yamt *
20 1.1.2.2 yamt * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21 1.1.2.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24 1.1.2.2 yamt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 1.1.2.2 yamt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.1.2.2 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 1.1.2.2 yamt * ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
31 1.1.2.2 yamt */
32 1.1.2.2 yamt
33 1.1.2.2 yamt #include <sys/cdefs.h>
34 1.1.2.3 yamt __KERNEL_RCSID(0, "$NetBSD: hci_misc.c,v 1.1.2.3 2007/10/27 11:36:06 yamt Exp $");
35 1.1.2.2 yamt
36 1.1.2.2 yamt #include <sys/param.h>
37 1.1.2.2 yamt #include <sys/kernel.h>
38 1.1.2.2 yamt #include <sys/malloc.h>
39 1.1.2.2 yamt #include <sys/mbuf.h>
40 1.1.2.2 yamt #include <sys/proc.h>
41 1.1.2.2 yamt #include <sys/queue.h>
42 1.1.2.2 yamt #include <sys/systm.h>
43 1.1.2.2 yamt
44 1.1.2.2 yamt #include <netbt/bluetooth.h>
45 1.1.2.2 yamt #include <netbt/hci.h>
46 1.1.2.2 yamt
47 1.1.2.2 yamt /*
48 1.1.2.2 yamt * cache Inquiry Responses for this number of seconds for routing
49 1.1.2.2 yamt * purposes [sysctl]
50 1.1.2.2 yamt */
51 1.1.2.2 yamt int hci_memo_expiry = 600;
52 1.1.2.2 yamt
53 1.1.2.2 yamt /*
54 1.1.2.2 yamt * set 'src' address for routing to 'dest'
55 1.1.2.2 yamt */
56 1.1.2.2 yamt int
57 1.1.2.2 yamt hci_route_lookup(bdaddr_t *src, bdaddr_t *dest)
58 1.1.2.2 yamt {
59 1.1.2.2 yamt struct hci_unit *unit;
60 1.1.2.2 yamt struct hci_link *link;
61 1.1.2.2 yamt struct hci_memo *memo;
62 1.1.2.2 yamt
63 1.1.2.2 yamt /*
64 1.1.2.2 yamt * Walk the ACL connections, if we have a connection
65 1.1.2.2 yamt * to 'dest' already then thats best..
66 1.1.2.2 yamt */
67 1.1.2.2 yamt SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) {
68 1.1.2.2 yamt if ((unit->hci_flags & BTF_UP) == 0)
69 1.1.2.2 yamt continue;
70 1.1.2.2 yamt
71 1.1.2.2 yamt TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
72 1.1.2.2 yamt if (link->hl_type != HCI_LINK_ACL)
73 1.1.2.2 yamt continue;
74 1.1.2.2 yamt
75 1.1.2.2 yamt if (bdaddr_same(&link->hl_bdaddr, dest))
76 1.1.2.2 yamt goto found;
77 1.1.2.2 yamt }
78 1.1.2.2 yamt }
79 1.1.2.2 yamt
80 1.1.2.2 yamt /*
81 1.1.2.2 yamt * Now check all the memos to see if there has been an
82 1.1.2.2 yamt * inquiry repsonse..
83 1.1.2.2 yamt */
84 1.1.2.2 yamt SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) {
85 1.1.2.2 yamt if ((unit->hci_flags & BTF_UP) == 0)
86 1.1.2.2 yamt continue;
87 1.1.2.2 yamt
88 1.1.2.2 yamt memo = hci_memo_find(unit, dest);
89 1.1.2.2 yamt if (memo)
90 1.1.2.2 yamt goto found;
91 1.1.2.2 yamt }
92 1.1.2.2 yamt
93 1.1.2.2 yamt /*
94 1.1.2.2 yamt * Last ditch effort, lets use the first unit we find
95 1.1.2.2 yamt * thats up and running. (XXX settable default route?)
96 1.1.2.2 yamt */
97 1.1.2.2 yamt SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) {
98 1.1.2.2 yamt if ((unit->hci_flags & BTF_UP) == 0)
99 1.1.2.2 yamt continue;
100 1.1.2.2 yamt
101 1.1.2.2 yamt goto found;
102 1.1.2.2 yamt }
103 1.1.2.2 yamt
104 1.1.2.2 yamt return EHOSTUNREACH;
105 1.1.2.2 yamt
106 1.1.2.2 yamt found:
107 1.1.2.2 yamt bdaddr_copy(src, &unit->hci_bdaddr);
108 1.1.2.2 yamt return 0;
109 1.1.2.2 yamt }
110 1.1.2.2 yamt
111 1.1.2.2 yamt /*
112 1.1.2.2 yamt * find unit memo from bdaddr
113 1.1.2.2 yamt */
114 1.1.2.2 yamt struct hci_memo *
115 1.1.2.2 yamt hci_memo_find(struct hci_unit *unit, bdaddr_t *bdaddr)
116 1.1.2.2 yamt {
117 1.1.2.2 yamt struct hci_memo *memo, *m0;
118 1.1.2.2 yamt struct timeval now;
119 1.1.2.2 yamt
120 1.1.2.2 yamt microtime(&now);
121 1.1.2.2 yamt
122 1.1.2.2 yamt m0 = LIST_FIRST(&unit->hci_memos);
123 1.1.2.2 yamt while ((memo = m0) != NULL) {
124 1.1.2.2 yamt m0 = LIST_NEXT(memo, next);
125 1.1.2.2 yamt
126 1.1.2.2 yamt if (now.tv_sec > memo->time.tv_sec + hci_memo_expiry) {
127 1.1.2.2 yamt DPRINTF("memo %p too old (expiring)\n", memo);
128 1.1.2.2 yamt hci_memo_free(memo);
129 1.1.2.2 yamt continue;
130 1.1.2.2 yamt }
131 1.1.2.2 yamt
132 1.1.2.3 yamt if (bdaddr_same(bdaddr, &memo->bdaddr)) {
133 1.1.2.2 yamt DPRINTF("memo %p found\n", memo);
134 1.1.2.2 yamt return memo;
135 1.1.2.2 yamt }
136 1.1.2.2 yamt }
137 1.1.2.2 yamt
138 1.1.2.2 yamt DPRINTF("no memo found\n");
139 1.1.2.2 yamt return NULL;
140 1.1.2.2 yamt }
141 1.1.2.2 yamt
142 1.1.2.3 yamt /*
143 1.1.2.3 yamt * Make a new memo on unit for bdaddr. If a memo exists, just
144 1.1.2.3 yamt * update the timestamp.
145 1.1.2.3 yamt */
146 1.1.2.3 yamt struct hci_memo *
147 1.1.2.3 yamt hci_memo_new(struct hci_unit *unit, bdaddr_t *bdaddr)
148 1.1.2.3 yamt {
149 1.1.2.3 yamt struct hci_memo *memo;
150 1.1.2.3 yamt
151 1.1.2.3 yamt memo = hci_memo_find(unit, bdaddr);
152 1.1.2.3 yamt if (memo == NULL) {
153 1.1.2.3 yamt memo = malloc(sizeof(struct hci_memo),
154 1.1.2.3 yamt M_BLUETOOTH, M_NOWAIT | M_ZERO);
155 1.1.2.3 yamt
156 1.1.2.3 yamt if (memo == NULL) {
157 1.1.2.3 yamt DPRINTFN(0, "no memory for memo!\n");
158 1.1.2.3 yamt return NULL;
159 1.1.2.3 yamt }
160 1.1.2.3 yamt
161 1.1.2.3 yamt DPRINTF("memo created for %02x:%02x:%02x:%02x:%02x:%02x\n",
162 1.1.2.3 yamt bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
163 1.1.2.3 yamt bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
164 1.1.2.3 yamt
165 1.1.2.3 yamt bdaddr_copy(&memo->bdaddr, bdaddr);
166 1.1.2.3 yamt LIST_INSERT_HEAD(&unit->hci_memos, memo, next);
167 1.1.2.3 yamt }
168 1.1.2.3 yamt else
169 1.1.2.3 yamt DPRINTF("memo updated for %02x:%02x:%02x:%02x:%02x:%02x\n",
170 1.1.2.3 yamt bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
171 1.1.2.3 yamt bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
172 1.1.2.3 yamt
173 1.1.2.3 yamt microtime(&memo->time);
174 1.1.2.3 yamt return memo;
175 1.1.2.3 yamt }
176 1.1.2.3 yamt
177 1.1.2.2 yamt void
178 1.1.2.2 yamt hci_memo_free(struct hci_memo *memo)
179 1.1.2.2 yamt {
180 1.1.2.2 yamt
181 1.1.2.2 yamt LIST_REMOVE(memo, next);
182 1.1.2.2 yamt free(memo, M_BLUETOOTH);
183 1.1.2.2 yamt }
184