1 /*********************************************************************
3 * Filename: discovery.c
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>
14 * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
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.
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.
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,
31 ********************************************************************/
33 #include <linux/string.h>
34 #include <linux/socket.h>
36 #include <net/irda/irda.h>
37 #include <net/irda/irlmp.h>
39 #include <net/irda/discovery.h>
42 * Function irlmp_add_discovery (cachelog, discovery)
44 * Add a new discovery to the cachelog, and remove any old discoveries
45 * from the same device
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...
56 void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new)
58 discovery_t *discovery, *node;
61 /* Set time of first discovery if node is new (see below) */
62 new->first_timestamp = new->timestamp;
64 spin_lock_irqsave(&irlmp->log_lock, flags);
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.
72 discovery = (discovery_t *) hashbin_get_first(cachelog);
73 while (discovery != NULL ) {
76 /* Be sure to stay one item ahead */
77 discovery = (discovery_t *) hashbin_get_next(cachelog);
79 if ((node->saddr == new->saddr) &&
80 ((node->daddr == new->daddr) ||
81 (strcmp(node->nickname, new->nickname) == 0)))
83 /* This discovery is a previous discovery
84 * from the same device, so just remove it
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;
95 /* Insert the new and updated version */
96 hashbin_insert(cachelog, (irda_queue_t *) new, new->daddr, NULL);
98 spin_unlock_irqrestore(&irlmp->log_lock, flags);
102 * Function irlmp_add_discovery_log (cachelog, log)
104 * Merge a disovery log into the cachlog.
107 void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log)
109 discovery_t *discovery;
111 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
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
119 /* irlmp_start_discovery_timer(irlmp, 150); */
123 discovery = (discovery_t *) hashbin_remove_first(log);
124 while (discovery != NULL) {
125 irlmp_add_discovery(cachelog, discovery);
127 discovery = (discovery_t *) hashbin_remove_first(log);
130 /* Delete the now empty log */
131 hashbin_delete(log, (FREE_FUNC) kfree);
135 * Function irlmp_expire_discoveries (log, saddr, force)
137 * Go through all discoveries and expire all that has stayed to long
139 * Note : this assume that IrLAP won't change its saddr, which
140 * currently is a valid assumption...
142 void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force)
144 discovery_t *discovery, *curr;
147 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
149 spin_lock_irqsave(&irlmp->log_lock, flags);
151 discovery = (discovery_t *) hashbin_get_first(log);
152 while (discovery != NULL) {
155 /* Be sure to be one item ahead */
156 discovery = (discovery_t *) hashbin_get_next(log);
158 /* Test if it's time to expire this discovery */
159 if ((curr->saddr == saddr) &&
161 ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))
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);
172 spin_unlock_irqrestore(&irlmp->log_lock, flags);
176 * Function irlmp_dump_discoveries (log)
178 * Print out all discoveries in log
181 void irlmp_dump_discoveries(hashbin_t *log)
183 discovery_t *discovery;
185 ASSERT(log != NULL, return;);
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);
194 discovery = (discovery_t *) hashbin_get_next(log);
199 * Function irlmp_copy_discoveries (log, pn, mask)
201 * Copy all discoveries in a buffer
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
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
215 * Note : the client must kfree himself() the log...
218 struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn, __u16 mask)
220 discovery_t * discovery;
222 struct irda_device_info * buffer;
226 ASSERT(pn != NULL, return NULL;);
228 /* Check if log is empty */
232 /* Save spin lock - spinlock should be discovery specific */
233 spin_lock_irqsave(&irlmp->log_lock, flags);
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);
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,
257 discovery = (discovery_t *) hashbin_get_next(log);
260 spin_unlock_irqrestore(&irlmp->log_lock, flags);
262 /* Get the actual number of device in the buffer and return */
268 * Function irlmp_find_device (name, saddr)
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).
274 __u32 irlmp_find_device(hashbin_t *cachelog, char *name, __u32 *saddr)
279 spin_lock_irqsave(&irlmp->log_lock, flags);
281 /* Look at all discoveries for that link */
282 d = (discovery_t *) hashbin_get_first(cachelog);
284 IRDA_DEBUG(1, "Discovery:\n");
285 IRDA_DEBUG(1, " daddr=%08x\n", d->daddr);
286 IRDA_DEBUG(1, " nickname=%s\n", d->nickname);
288 if (strcmp(name, d->nickname) == 0) {
291 spin_unlock_irqrestore(&irlmp->log_lock, flags);
294 d = (discovery_t *) hashbin_get_next(cachelog);
297 spin_unlock_irqrestore(&irlmp->log_lock, flags);
303 * Function proc_discovery_read (buf, start, offset, len, unused)
305 * Print discovery information in /proc file system
308 int discovery_proc_read(char *buf, char **start, off_t offset, int length,
311 discovery_t *discovery;
313 hashbin_t *cachelog = irlmp_get_cachelog();
319 len = sprintf(buf, "IrLMP: Discovery log:\n\n");
321 spin_lock_irqsave(&irlmp->log_lock, flags);
323 discovery = (discovery_t *) hashbin_get_first(cachelog);
324 while (( discovery != NULL) && (len < length)) {
325 len += sprintf(buf+len, "nickname: %s,", discovery->nickname);
327 len += sprintf(buf+len, " hint: 0x%02x%02x",
328 discovery->hints.byte[0],
329 discovery->hints.byte[1]);
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 ");
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 ");
355 len += sprintf(buf+len, ", saddr: 0x%08x",
358 len += sprintf(buf+len, ", daddr: 0x%08x\n",
361 len += sprintf(buf+len, "\n");
363 discovery = (discovery_t *) hashbin_get_next(cachelog);
365 spin_unlock_irqrestore(&irlmp->log_lock, flags);