discovery.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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. #include <linux/string.h>
  33. #include <linux/socket.h>
  34. #include <net/irda/irda.h>
  35. #include <net/irda/irlmp.h>
  36. #include <net/irda/discovery.h>
  37. /*
  38.  * Function irlmp_add_discovery (cachelog, discovery)
  39.  *
  40.  *    Add a new discovery to the cachelog, and remove any old discoveries
  41.  *    from the same device
  42.  *
  43.  * Note : we try to preserve the time this device was *first* discovered
  44.  * (as opposed to the time of last discovery used for cleanup). This is
  45.  * used by clients waiting for discovery events to tell if the device
  46.  * discovered is "new" or just the same old one. They can't rely there
  47.  * on a binary flag (new/old), because not all discovery events are
  48.  * propagated to them, and they might not always listen, so they would
  49.  * miss some new devices popping up...
  50.  * Jean II
  51.  */
  52. void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new)
  53. {
  54. discovery_t *discovery, *node;
  55. unsigned long flags;
  56. /* Set time of first discovery if node is new (see below) */
  57. new->first_timestamp = new->timestamp;
  58. spin_lock_irqsave(&irlmp->log_lock, flags);
  59. /* 
  60.  * Remove all discoveries of devices that has previously been 
  61.  * discovered on the same link with the same name (info), or the 
  62.  * same daddr. We do this since some devices (mostly PDAs) change
  63.  * their device address between every discovery.
  64.  */
  65. discovery = (discovery_t *) hashbin_get_first(cachelog);
  66. while (discovery != NULL ) {
  67. node = discovery;
  68. /* Be sure to stay one item ahead */
  69. discovery = (discovery_t *) hashbin_get_next(cachelog);
  70. if ((node->saddr == new->saddr) &&
  71.     ((node->daddr == new->daddr) || 
  72.      (strcmp(node->nickname, new->nickname) == 0)))
  73. {
  74. /* This discovery is a previous discovery 
  75.  * from the same device, so just remove it
  76.  */
  77. hashbin_remove_this(cachelog, (irda_queue_t *) node);
  78. /* Check if hints bits have changed */
  79. if(node->hints.word == new->hints.word)
  80. /* Set time of first discovery for this node */
  81. new->first_timestamp = node->first_timestamp;
  82. kfree(node);
  83. }
  84. }
  85. /* Insert the new and updated version */
  86. hashbin_insert(cachelog, (irda_queue_t *) new, new->daddr, NULL);
  87. spin_unlock_irqrestore(&irlmp->log_lock, flags);
  88. }
  89. /*
  90.  * Function irlmp_add_discovery_log (cachelog, log)
  91.  *
  92.  *    Merge a disovery log into the cachlog.
  93.  *
  94.  */
  95. void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log)
  96. {
  97. discovery_t *discovery;
  98. IRDA_DEBUG(4, "%s()n", __FUNCTION__);
  99. /*
  100.  *  If log is missing this means that IrLAP was unable to perform the
  101.  *  discovery, so restart discovery again with just the half timeout
  102.  *  of the normal one.
  103.  */
  104. if (log == NULL) {
  105. /* irlmp_start_discovery_timer(irlmp, 150); */
  106. return;
  107. }
  108. discovery = (discovery_t *) hashbin_remove_first(log);
  109. while (discovery != NULL) {
  110. irlmp_add_discovery(cachelog, discovery);
  111. discovery = (discovery_t *) hashbin_remove_first(log);
  112. }
  113. /* Delete the now empty log */
  114. hashbin_delete(log, (FREE_FUNC) kfree);
  115. }
  116. /*
  117.  * Function irlmp_expire_discoveries (log, saddr, force)
  118.  *
  119.  *    Go through all discoveries and expire all that has stayed to long
  120.  *
  121.  * Note : this assume that IrLAP won't change its saddr, which
  122.  * currently is a valid assumption...
  123.  */
  124. void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force)
  125. {
  126. discovery_t *discovery, *curr;
  127. unsigned long flags;
  128. IRDA_DEBUG(4, "%s()n", __FUNCTION__);
  129. spin_lock_irqsave(&irlmp->log_lock, flags);
  130. discovery = (discovery_t *) hashbin_get_first(log);
  131. while (discovery != NULL) {
  132. curr = discovery;
  133. /* Be sure to be one item ahead */
  134. discovery = (discovery_t *) hashbin_get_next(log);
  135. /* Test if it's time to expire this discovery */
  136. if ((curr->saddr == saddr) &&
  137.     (force ||
  138.      ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))
  139. {
  140. /* Tell IrLMP and registered clients about it */
  141. irlmp_discovery_expiry(curr);
  142. /* Remove it from the log */
  143. curr = hashbin_remove_this(log, (irda_queue_t *) curr);
  144. if (curr)
  145. kfree(curr);
  146. }
  147. }
  148. spin_unlock_irqrestore(&irlmp->log_lock, flags);
  149. }
  150. /*
  151.  * Function irlmp_dump_discoveries (log)
  152.  *
  153.  *    Print out all discoveries in log
  154.  *
  155.  */
  156. void irlmp_dump_discoveries(hashbin_t *log)
  157. {
  158. discovery_t *discovery;
  159. ASSERT(log != NULL, return;);
  160. discovery = (discovery_t *) hashbin_get_first(log);
  161. while (discovery != NULL) {
  162. IRDA_DEBUG(0, "Discovery:n");
  163. IRDA_DEBUG(0, "  daddr=%08xn", discovery->daddr);
  164. IRDA_DEBUG(0, "  saddr=%08xn", discovery->saddr); 
  165. IRDA_DEBUG(0, "  nickname=%sn", discovery->nickname);
  166. discovery = (discovery_t *) hashbin_get_next(log);
  167. }
  168. }
  169. /*
  170.  * Function irlmp_copy_discoveries (log, pn, mask)
  171.  *
  172.  *    Copy all discoveries in a buffer
  173.  *
  174.  * This function implement a safe way for lmp clients to access the
  175.  * discovery log. The basic problem is that we don't want the log
  176.  * to change (add/remove) while the client is reading it. If the
  177.  * lmp client manipulate directly the hashbin, he is sure to get
  178.  * into troubles...
  179.  * The idea is that we copy all the current discovery log in a buffer
  180.  * which is specific to the client and pass this copy to him. As we
  181.  * do this operation with the spinlock grabbed, we are safe...
  182.  * Note : we don't want those clients to grab the spinlock, because
  183.  * we have no control on how long they will hold it...
  184.  * Note : we choose to copy the log in "struct irda_device_info" to
  185.  * save space...
  186.  * Note : the client must kfree himself() the log...
  187.  * Jean II
  188.  */
  189. struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn, __u16 mask)
  190. {
  191. discovery_t * discovery;
  192. unsigned long flags;
  193. struct irda_device_info * buffer;
  194. int i = 0;
  195. int n;
  196. ASSERT(pn != NULL, return NULL;);
  197. /* Check if log is empty */
  198. if(log == NULL)
  199. return NULL;
  200. /* Save spin lock - spinlock should be discovery specific */
  201. spin_lock_irqsave(&irlmp->log_lock, flags);
  202. /* Create the client specific buffer */
  203. n = HASHBIN_GET_SIZE(log);
  204. buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
  205. if (buffer == NULL) {
  206. spin_unlock_irqrestore(&irlmp->log_lock, flags);
  207. return NULL;
  208. }
  209. discovery = (discovery_t *) hashbin_get_first(log);
  210. while ((discovery != NULL) && (i < n)) {
  211. /* Mask out the ones we don't want */
  212. if (discovery->hints.word & mask) {
  213. /* Copy discovery information */
  214. buffer[i].saddr = discovery->saddr;
  215. buffer[i].daddr = discovery->daddr;
  216. buffer[i].charset = discovery->charset;
  217. buffer[i].hints[0] = discovery->hints.byte[0];
  218. buffer[i].hints[1] = discovery->hints.byte[1];
  219. strncpy(buffer[i].info, discovery->nickname,
  220. NICKNAME_MAX_LEN);
  221. i++;
  222. }
  223. discovery = (discovery_t *) hashbin_get_next(log);
  224. }
  225. spin_unlock_irqrestore(&irlmp->log_lock, flags);
  226. /* Get the actual number of device in the buffer and return */
  227. *pn = i;
  228. return(buffer);
  229. }
  230. /*
  231.  * Function irlmp_find_device (name, saddr)
  232.  *
  233.  *    Look through the discovery log at each of the links and try to find 
  234.  *    the device with the given name. Return daddr and saddr. If saddr is
  235.  *    specified, that look at that particular link only (not impl).
  236.  */
  237. __u32 irlmp_find_device(hashbin_t *cachelog, char *name, __u32 *saddr)
  238. {
  239. unsigned long flags;
  240. discovery_t *d;
  241. spin_lock_irqsave(&irlmp->log_lock, flags);
  242. /* Look at all discoveries for that link */
  243. d = (discovery_t *) hashbin_get_first(cachelog);
  244. while (d != NULL) {
  245. IRDA_DEBUG(1, "Discovery:n");
  246. IRDA_DEBUG(1, "  daddr=%08xn", d->daddr);
  247. IRDA_DEBUG(1, "  nickname=%sn", d->nickname);
  248. if (strcmp(name, d->nickname) == 0) {
  249. *saddr = d->saddr;
  250. spin_unlock_irqrestore(&irlmp->log_lock, flags);
  251. return d->daddr;
  252. }
  253. d = (discovery_t *) hashbin_get_next(cachelog);
  254. }
  255. spin_unlock_irqrestore(&irlmp->log_lock, flags);
  256. return 0;
  257. }
  258. /*
  259.  * Function proc_discovery_read (buf, start, offset, len, unused)
  260.  *
  261.  *    Print discovery information in /proc file system
  262.  *
  263.  */
  264. int discovery_proc_read(char *buf, char **start, off_t offset, int length, 
  265. int unused)
  266. {
  267. discovery_t *discovery;
  268. unsigned long flags;
  269. hashbin_t *cachelog = irlmp_get_cachelog();
  270. int len = 0;
  271. if (!irlmp)
  272. return len;
  273. len = sprintf(buf, "IrLMP: Discovery log:nn");
  274. spin_lock_irqsave(&irlmp->log_lock, flags);
  275. discovery = (discovery_t *) hashbin_get_first(cachelog);
  276. while (( discovery != NULL) && (len < length)) {
  277. len += sprintf(buf+len, "nickname: %s,", discovery->nickname);
  278. len += sprintf(buf+len, " hint: 0x%02x%02x", 
  279.        discovery->hints.byte[0], 
  280.        discovery->hints.byte[1]);
  281. #if 0
  282. if ( discovery->hints.byte[0] & HINT_PNP)
  283. len += sprintf( buf+len, "PnP Compatible ");
  284. if ( discovery->hints.byte[0] & HINT_PDA)
  285. len += sprintf( buf+len, "PDA/Palmtop ");
  286. if ( discovery->hints.byte[0] & HINT_COMPUTER)
  287. len += sprintf( buf+len, "Computer ");
  288. if ( discovery->hints.byte[0] & HINT_PRINTER)
  289. len += sprintf( buf+len, "Printer ");
  290. if ( discovery->hints.byte[0] & HINT_MODEM)
  291. len += sprintf( buf+len, "Modem ");
  292. if ( discovery->hints.byte[0] & HINT_FAX)
  293. len += sprintf( buf+len, "Fax ");
  294. if ( discovery->hints.byte[0] & HINT_LAN)
  295. len += sprintf( buf+len, "LAN Access ");
  296. if ( discovery->hints.byte[1] & HINT_TELEPHONY)
  297. len += sprintf( buf+len, "Telephony ");
  298. if ( discovery->hints.byte[1] & HINT_FILE_SERVER)
  299. len += sprintf( buf+len, "File Server ");       
  300. if ( discovery->hints.byte[1] & HINT_COMM)
  301. len += sprintf( buf+len, "IrCOMM ");
  302. if ( discovery->hints.byte[1] & HINT_OBEX)
  303. len += sprintf( buf+len, "IrOBEX ");
  304. #endif
  305. len += sprintf(buf+len, ", saddr: 0x%08x", 
  306.        discovery->saddr);
  307. len += sprintf(buf+len, ", daddr: 0x%08xn", 
  308.        discovery->daddr);
  309. len += sprintf(buf+len, "n");
  310. discovery = (discovery_t *) hashbin_get_next(cachelog);
  311. }
  312. spin_unlock_irqrestore(&irlmp->log_lock, flags);
  313. return len;
  314. }