1 /*********************************************************************
2 *
3 * Filename: discovery.c
4 * Version: 0.1
5 * Description: Routines for handling discoveries at the IrLMP layer
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Tue Apr 6 15:33:50 1999
9 * Modified at: Sat Oct 9 17:11:31 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 * Modified at: Fri May 28 3:11 CST 1999
12 * Modified by: Horst von Brand <vonbrand@sleipnir.valparaiso.cl>
13 *
14 * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
30 *
31 ********************************************************************/
32
33 #include <linux/string.h>
34 #include <linux/socket.h>
35
36 #include <net/irda/irda.h>
37 #include <net/irda/irlmp.h>
38
39 #include <net/irda/discovery.h>
40
41 /*
42 * Function irlmp_add_discovery (cachelog, discovery)
43 *
44 * Add a new discovery to the cachelog, and remove any old discoveries
45 * from the same device
46 *
47 * Note : we try to preserve the time this device was *first* discovered
48 * (as opposed to the time of last discovery used for cleanup). This is
49 * used by clients waiting for discovery events to tell if the device
50 * discovered is "new" or just the same old one. They can't rely there
51 * on a binary flag (new/old), because not all discovery events are
52 * propagated to them, and they might not always listen, so they would
53 * miss some new devices popping up...
54 * Jean II
55 */
irlmp_add_discovery(hashbin_t * cachelog,discovery_t * new)56 void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new)
57 {
58 discovery_t *discovery, *node;
59 unsigned long flags;
60
61 /* Set time of first discovery if node is new (see below) */
62 new->first_timestamp = new->timestamp;
63
64 spin_lock_irqsave(&irlmp->log_lock, flags);
65
66 /*
67 * Remove all discoveries of devices that has previously been
68 * discovered on the same link with the same name (info), or the
69 * same daddr. We do this since some devices (mostly PDAs) change
70 * their device address between every discovery.
71 */
72 discovery = (discovery_t *) hashbin_get_first(cachelog);
73 while (discovery != NULL ) {
74 node = discovery;
75
76 /* Be sure to stay one item ahead */
77 discovery = (discovery_t *) hashbin_get_next(cachelog);
78
79 if ((node->saddr == new->saddr) &&
80 ((node->daddr == new->daddr) ||
81 (strcmp(node->nickname, new->nickname) == 0)))
82 {
83 /* This discovery is a previous discovery
84 * from the same device, so just remove it
85 */
86 hashbin_remove_this(cachelog, (irda_queue_t *) node);
87 /* Check if hints bits have changed */
88 if(node->hints.word == new->hints.word)
89 /* Set time of first discovery for this node */
90 new->first_timestamp = node->first_timestamp;
91 kfree(node);
92 }
93 }
94
95 /* Insert the new and updated version */
96 hashbin_insert(cachelog, (irda_queue_t *) new, new->daddr, NULL);
97
98 spin_unlock_irqrestore(&irlmp->log_lock, flags);
99 }
100
101 /*
102 * Function irlmp_add_discovery_log (cachelog, log)
103 *
104 * Merge a disovery log into the cachlog.
105 *
106 */
irlmp_add_discovery_log(hashbin_t * cachelog,hashbin_t * log)107 void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log)
108 {
109 discovery_t *discovery;
110
111 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
112
113 /*
114 * If log is missing this means that IrLAP was unable to perform the
115 * discovery, so restart discovery again with just the half timeout
116 * of the normal one.
117 */
118 if (log == NULL) {
119 /* irlmp_start_discovery_timer(irlmp, 150); */
120 return;
121 }
122
123 discovery = (discovery_t *) hashbin_remove_first(log);
124 while (discovery != NULL) {
125 irlmp_add_discovery(cachelog, discovery);
126
127 discovery = (discovery_t *) hashbin_remove_first(log);
128 }
129
130 /* Delete the now empty log */
131 hashbin_delete(log, (FREE_FUNC) kfree);
132 }
133
134 /*
135 * Function irlmp_expire_discoveries (log, saddr, force)
136 *
137 * Go through all discoveries and expire all that has stayed to long
138 *
139 * Note : this assume that IrLAP won't change its saddr, which
140 * currently is a valid assumption...
141 */
irlmp_expire_discoveries(hashbin_t * log,__u32 saddr,int force)142 void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force)
143 {
144 discovery_t *discovery, *curr;
145 unsigned long flags;
146
147 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
148
149 spin_lock_irqsave(&irlmp->log_lock, flags);
150
151 discovery = (discovery_t *) hashbin_get_first(log);
152 while (discovery != NULL) {
153 curr = discovery;
154
155 /* Be sure to be one item ahead */
156 discovery = (discovery_t *) hashbin_get_next(log);
157
158 /* Test if it's time to expire this discovery */
159 if ((curr->saddr == saddr) &&
160 (force ||
161 ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))
162 {
163 /* Tell IrLMP and registered clients about it */
164 irlmp_discovery_expiry(curr);
165 /* Remove it from the log */
166 curr = hashbin_remove_this(log, (irda_queue_t *) curr);
167 if (curr)
168 kfree(curr);
169 }
170 }
171
172 spin_unlock_irqrestore(&irlmp->log_lock, flags);
173 }
174
175 /*
176 * Function irlmp_dump_discoveries (log)
177 *
178 * Print out all discoveries in log
179 *
180 */
irlmp_dump_discoveries(hashbin_t * log)181 void irlmp_dump_discoveries(hashbin_t *log)
182 {
183 discovery_t *discovery;
184
185 ASSERT(log != NULL, return;);
186
187 discovery = (discovery_t *) hashbin_get_first(log);
188 while (discovery != NULL) {
189 IRDA_DEBUG(0, "Discovery:\n");
190 IRDA_DEBUG(0, " daddr=%08x\n", discovery->daddr);
191 IRDA_DEBUG(0, " saddr=%08x\n", discovery->saddr);
192 IRDA_DEBUG(0, " nickname=%s\n", discovery->nickname);
193
194 discovery = (discovery_t *) hashbin_get_next(log);
195 }
196 }
197
198 /*
199 * Function irlmp_copy_discoveries (log, pn, mask)
200 *
201 * Copy all discoveries in a buffer
202 *
203 * This function implement a safe way for lmp clients to access the
204 * discovery log. The basic problem is that we don't want the log
205 * to change (add/remove) while the client is reading it. If the
206 * lmp client manipulate directly the hashbin, he is sure to get
207 * into troubles...
208 * The idea is that we copy all the current discovery log in a buffer
209 * which is specific to the client and pass this copy to him. As we
210 * do this operation with the spinlock grabbed, we are safe...
211 * Note : we don't want those clients to grab the spinlock, because
212 * we have no control on how long they will hold it...
213 * Note : we choose to copy the log in "struct irda_device_info" to
214 * save space...
215 * Note : the client must kfree himself() the log...
216 * Jean II
217 */
irlmp_copy_discoveries(hashbin_t * log,int * pn,__u16 mask)218 struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn, __u16 mask)
219 {
220 discovery_t * discovery;
221 unsigned long flags;
222 struct irda_device_info * buffer;
223 int i = 0;
224 int n;
225
226 ASSERT(pn != NULL, return NULL;);
227
228 /* Check if log is empty */
229 if(log == NULL)
230 return NULL;
231
232 /* Save spin lock - spinlock should be discovery specific */
233 spin_lock_irqsave(&irlmp->log_lock, flags);
234
235 /* Create the client specific buffer */
236 n = HASHBIN_GET_SIZE(log);
237 buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
238 if (buffer == NULL) {
239 spin_unlock_irqrestore(&irlmp->log_lock, flags);
240 return NULL;
241 }
242
243 discovery = (discovery_t *) hashbin_get_first(log);
244 while ((discovery != NULL) && (i < n)) {
245 /* Mask out the ones we don't want */
246 if (discovery->hints.word & mask) {
247 /* Copy discovery information */
248 buffer[i].saddr = discovery->saddr;
249 buffer[i].daddr = discovery->daddr;
250 buffer[i].charset = discovery->charset;
251 buffer[i].hints[0] = discovery->hints.byte[0];
252 buffer[i].hints[1] = discovery->hints.byte[1];
253 strncpy(buffer[i].info, discovery->nickname,
254 NICKNAME_MAX_LEN);
255 i++;
256 }
257 discovery = (discovery_t *) hashbin_get_next(log);
258 }
259
260 spin_unlock_irqrestore(&irlmp->log_lock, flags);
261
262 /* Get the actual number of device in the buffer and return */
263 *pn = i;
264 return(buffer);
265 }
266
267 /*
268 * Function irlmp_find_device (name, saddr)
269 *
270 * Look through the discovery log at each of the links and try to find
271 * the device with the given name. Return daddr and saddr. If saddr is
272 * specified, that look at that particular link only (not impl).
273 */
irlmp_find_device(hashbin_t * cachelog,char * name,__u32 * saddr)274 __u32 irlmp_find_device(hashbin_t *cachelog, char *name, __u32 *saddr)
275 {
276 unsigned long flags;
277 discovery_t *d;
278
279 spin_lock_irqsave(&irlmp->log_lock, flags);
280
281 /* Look at all discoveries for that link */
282 d = (discovery_t *) hashbin_get_first(cachelog);
283 while (d != NULL) {
284 IRDA_DEBUG(1, "Discovery:\n");
285 IRDA_DEBUG(1, " daddr=%08x\n", d->daddr);
286 IRDA_DEBUG(1, " nickname=%s\n", d->nickname);
287
288 if (strcmp(name, d->nickname) == 0) {
289 *saddr = d->saddr;
290
291 spin_unlock_irqrestore(&irlmp->log_lock, flags);
292 return d->daddr;
293 }
294 d = (discovery_t *) hashbin_get_next(cachelog);
295 }
296
297 spin_unlock_irqrestore(&irlmp->log_lock, flags);
298
299 return 0;
300 }
301
302 /*
303 * Function proc_discovery_read (buf, start, offset, len, unused)
304 *
305 * Print discovery information in /proc file system
306 *
307 */
discovery_proc_read(char * buf,char ** start,off_t offset,int length,int unused)308 int discovery_proc_read(char *buf, char **start, off_t offset, int length,
309 int unused)
310 {
311 discovery_t *discovery;
312 unsigned long flags;
313 hashbin_t *cachelog = irlmp_get_cachelog();
314 int len = 0;
315
316 if (!irlmp)
317 return len;
318
319 len = sprintf(buf, "IrLMP: Discovery log:\n\n");
320
321 spin_lock_irqsave(&irlmp->log_lock, flags);
322
323 discovery = (discovery_t *) hashbin_get_first(cachelog);
324 while (( discovery != NULL) && (len < length)) {
325 len += sprintf(buf+len, "nickname: %s,", discovery->nickname);
326
327 len += sprintf(buf+len, " hint: 0x%02x%02x",
328 discovery->hints.byte[0],
329 discovery->hints.byte[1]);
330 #if 0
331 if ( discovery->hints.byte[0] & HINT_PNP)
332 len += sprintf( buf+len, "PnP Compatible ");
333 if ( discovery->hints.byte[0] & HINT_PDA)
334 len += sprintf( buf+len, "PDA/Palmtop ");
335 if ( discovery->hints.byte[0] & HINT_COMPUTER)
336 len += sprintf( buf+len, "Computer ");
337 if ( discovery->hints.byte[0] & HINT_PRINTER)
338 len += sprintf( buf+len, "Printer ");
339 if ( discovery->hints.byte[0] & HINT_MODEM)
340 len += sprintf( buf+len, "Modem ");
341 if ( discovery->hints.byte[0] & HINT_FAX)
342 len += sprintf( buf+len, "Fax ");
343 if ( discovery->hints.byte[0] & HINT_LAN)
344 len += sprintf( buf+len, "LAN Access ");
345
346 if ( discovery->hints.byte[1] & HINT_TELEPHONY)
347 len += sprintf( buf+len, "Telephony ");
348 if ( discovery->hints.byte[1] & HINT_FILE_SERVER)
349 len += sprintf( buf+len, "File Server ");
350 if ( discovery->hints.byte[1] & HINT_COMM)
351 len += sprintf( buf+len, "IrCOMM ");
352 if ( discovery->hints.byte[1] & HINT_OBEX)
353 len += sprintf( buf+len, "IrOBEX ");
354 #endif
355 len += sprintf(buf+len, ", saddr: 0x%08x",
356 discovery->saddr);
357
358 len += sprintf(buf+len, ", daddr: 0x%08x\n",
359 discovery->daddr);
360
361 len += sprintf(buf+len, "\n");
362
363 discovery = (discovery_t *) hashbin_get_next(cachelog);
364 }
365 spin_unlock_irqrestore(&irlmp->log_lock, flags);
366
367 return len;
368 }
369