📄 discovery.c
字号:
/*********************************************************************** Filename: discovery.c* Version: 0.1* Description: Routines for handling discoveries at the IrLMP layer* Status: Experimental.* Author: Dag Brattli <dagb@cs.uit.no>* Created at: Tue Apr 6 15:33:50 1999* Modified at: Sat Oct 9 17:11:31 1999* Modified by: Dag Brattli <dagb@cs.uit.no>* Modified at: Fri May 28 3:11 CST 1999* Modified by: Horst von Brand <vonbrand@sleipnir.valparaiso.cl>** Copyright (c) 1999 Dag Brattli, All Rights Reserved.** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU General Public License as* published by the Free Software Foundation; either version 2 of* the License, or (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place, Suite 330, Boston,* MA 02111-1307 USA*********************************************************************/#include <string.h>#include "irda.h"#include "irlmp.h"#include "irqueue.h"#include <stdlib.h>/** Function irlmp_add_discovery (cachelog, discovery)** Add a new discovery to the cachelog, and remove any old discoveries* from the same device** Note : we try to preserve the time this device was *first* discovered* (as opposed to the time of last discovery used for cleanup). This is* used by clients waiting for discovery events to tell if the device* discovered is "new" or just the same old one. They can't rely there* on a binary flag (new/old), because not all discovery events are* propagated to them, and they might not always listen, so they would* miss some new devices popping up...* Jean II*/void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new){ discovery_t *discovery, *node; IRDA_DEBUG(1, "%s()\n", __FUNCTION__); /* Set time of first discovery if node is new (see below) */ new->firststamp = new->timestamp; /* * Remove all discoveries of devices that has previously been * discovered on the same link with the same name (info), or the * same daddr. We do this since some devices (mostly PDAs) change * their device address between every discovery. */ discovery = (discovery_t *) hashbin_get_first(cachelog); while (discovery != NULL ) { node = discovery; /* Be sure to stay one item ahead */ discovery = (discovery_t *) hashbin_get_next(cachelog); if ((node->data.saddr == new->data.saddr) && ((node->data.daddr == new->data.daddr) || (strcmp(node->data.info, new->data.info) == 0))) { /* This discovery is a previous discovery * from the same device, so just remove it */ hashbin_remove_this(cachelog, (irda_queue_t *) node); /* Check if hints bits are unchanged */ //modify by xugan if(node->data.hints[0]== new->data.hints[0] && node->data.hints[1]== new->data.hints[1]) /* Set time of first discovery for this node */ new->firststamp = node->firststamp; free(node); } } /* Insert the new and updated version */ hashbin_insert(cachelog, (irda_queue_t *) new, new->data.daddr, NULL);}/** Function irlmp_add_discovery_log (cachelog, log)** Merge a disovery log into the cachelog.**/void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log){ discovery_t *discovery; IRDA_DEBUG(1, "%s()\n", __FUNCTION__); /* * If log is missing this means that IrLAP was unable to perform the * discovery, so restart discovery again with just the half timeout * of the normal one. */ /* Well... It means that there was nobody out there - Jean II */ if (log == NULL) { return; } /* * Locking : we are the only owner of this discovery log, so * no need to lock it. * We just need to lock the global log in irlmp_add_discovery(). */ discovery = (discovery_t *) hashbin_remove_first(log); while (discovery != NULL) { irlmp_add_discovery(cachelog, discovery); discovery = (discovery_t *) hashbin_remove_first(log); } /* Delete the now empty log */ hashbin_delete(log, (FREE_FUNC) free);}/** Function irlmp_expire_discoveries (log, saddr, force)** Go through all discoveries and expire all that has stayed too long** Note : this assume that IrLAP won't change its saddr, which* currently is a valid assumption...*/void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force){ discovery_t * discovery; discovery_t * curr; discinfo_t * buffer = NULL; int n; /* Size of the full log */ int i = 0; /* How many we expired */ IRDA_ASSERT(log != NULL, return;); IRDA_DEBUG(4, "%s()\n", __FUNCTION__); discovery = (discovery_t *) hashbin_get_first(log); while (discovery != NULL) { /* Be sure to be one item ahead */ curr = discovery; discovery = (discovery_t *) hashbin_get_next(log); /* Test if it's time to expire this discovery */ // modify by xugan time_t i_time = kern_time_get(); if ((curr->data.saddr == saddr) && (force || ((i_time - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT))) { /* Create buffer as needed. * As this function get called a lot and most time * we don't have anything to put in the log (we are * quite picky), we can save a lot of overhead * by not calling kmalloc. Jean II */ if(buffer == NULL) { /* Create the client specific buffer */ n = HASHBIN_GET_SIZE(log); buffer = (discinfo_t*)malloc(n * sizeof(struct irda_device_info)); if (buffer == NULL) { IRDA_ERROR("%s() can not allocate memory\n",__FUNCTION__); return; } memset(buffer,0,n*sizeof(struct irda_device_info)); } /* Copy discovery information */ memcpy(&(buffer[i]), &(curr->data), sizeof(discinfo_t)); i++; /* Remove it from the log */ curr = hashbin_remove_this(log, (irda_queue_t *) curr); free(curr); } } /* Drop the spinlock before calling the higher layers, as * we can't guarantee they won't call us back and create a * deadlock. We will work on our own private data, so we * don't care to be interupted. - Jean II */ //spin_unlock_irqrestore(&log->hb_spinlock, flags); if(buffer == NULL) return; /* Tell IrLMP and registered clients about it */ irlmp_discovery_expiry(buffer, i); /* Free up our buffer */ free(buffer);}/** Function irlmp_copy_discoveries (log, pn, mask)** Copy all discoveries in a buffer** This function implement a safe way for lmp clients to access the* discovery log. The basic problem is that we don't want the log* to change (add/remove) while the client is reading it. If the* lmp client manipulate directly the hashbin, he is sure to get* into troubles...* The idea is that we copy all the current discovery log in a buffer* which is specific to the client and pass this copy to him. As we* do this operation with the spinlock grabbed, we are safe...* Note : we don't want those clients to grab the spinlock, because* we have no control on how long they will hold it...* Note : we choose to copy the log in "struct irda_device_info" to* save space...* Note : the client must kfree himself() the log...* Jean II*/struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn,__u16 mask, int old_entries){ discovery_t * discovery; discinfo_t * buffer = NULL; int j_timeout = sysctl_discovery_timeout ; int n; /* Size of the full log */ int i = 0; /* How many we picked */ __u16 discovery_hint; IRDA_ASSERT(pn != NULL, return NULL;); IRDA_ASSERT(log != NULL, return NULL;); //modify by xugan time_t i_time = kern_time_get(); discovery = (discovery_t *) hashbin_get_first(log); IRDA_DEBUG(4, "%s()\n", __FUNCTION__); while (discovery != NULL) { IRDA_DEBUG(4, "have log%s()\n", __FUNCTION__); /* Mask out the ones we don't want : * We want to match the discovery mask, and to get only * the most recent one (unless we want old ones) */ //modify by xugangan discovery_hint = discovery->data.hints[0]<<8 |discovery->data.hints[1]; if ((discovery_hint & mask) &&((old_entries) || ((i_time - discovery->firststamp) < j_timeout)) ) { /* Create buffer as needed. * As this function get called a lot and most time * we don't have anything to put in the log (we are * quite picky), we can save a lot of overhead * by not calling kmalloc. Jean II */ if(buffer == NULL) { /* Create the client specific buffer */ n = HASHBIN_GET_SIZE(log); buffer = (struct irda_device_info*)malloc(n * sizeof(struct irda_device_info)); if (buffer == NULL) { IRDA_ERROR("%s() can not allocate memory\n",__FUNCTION__); return NULL; } memset(buffer,0,n*sizeof(struct irda_device_info)); } /* Copy discovery information */ memcpy(&(buffer[i]), &(discovery->data), sizeof(discinfo_t)); i++; } discovery = (discovery_t *) hashbin_get_next(log); } /* Get the actual number of device in the buffer and return */ *pn = i; return(buffer);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -