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