Exemple #1
0
/*
Caller must hold pmlmepriv->lock first.
*/
static void update_scanned_network(struct _adapter *adapter,
			    struct ndis_wlan_bssid_ex *target)
{
	struct list_head *plist, *phead;

	u32 bssid_ex_sz;
	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
	struct  __queue *queue = &pmlmepriv->scanned_queue;
	struct wlan_network *pnetwork = NULL;
	struct wlan_network *oldest = NULL;

	phead = get_list_head(queue);
	plist = get_next(phead);

	while (1) {
		if (end_of_queue_search(phead, plist) == true)
			break;

		pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list);
		if (is_same_network(&pnetwork->network, target))
			break;
		if ((oldest == ((struct wlan_network *)0)) ||
		    time_after((unsigned long)oldest->last_scanned,
				(unsigned long)pnetwork->last_scanned))
			oldest = pnetwork;

		plist = get_next(plist);
	}


	/* If we didn't find a match, then get a new network slot to initialize
	 * with this beacon's information */
	if (end_of_queue_search(phead, plist) == true) {
		if (_queue_empty(&pmlmepriv->free_bss_pool) == true) {
			/* If there are no more slots, expire the oldest */
			pnetwork = oldest;
			target->Rssi = (pnetwork->network.Rssi +
					target->Rssi) / 2;
			memcpy(&pnetwork->network, target,
				r8712_get_ndis_wlan_bssid_ex_sz(target));
			pnetwork->last_scanned = jiffies;
		} else {
			/* Otherwise just pull from the free list */
			/* update scan_time */
			pnetwork = alloc_network(pmlmepriv);
			if (pnetwork == NULL)
				return;
			bssid_ex_sz = r8712_get_ndis_wlan_bssid_ex_sz(target);
			target->Length = bssid_ex_sz;
			memcpy(&pnetwork->network, target, bssid_ex_sz);
			list_insert_tail(&pnetwork->list, &queue->queue);
		}
	} else {
		/* we have an entry and we are going to update it. But
		 * this entry may be already expired. In this case we
		 * do the same as we found a new net and call the new_net
		 * handler
		 */
		update_network(&pnetwork->network, target, adapter);
		pnetwork->last_scanned = jiffies;
	}
}
Exemple #2
0
int
read_network (const char *chem_file, net_t * network, const int verbose)
{
  FILE *f;
  char line[MAX_LINE];

  /*   Find the input file. We first look in the current directory, and
       then in the PKGDATADIR directory. Exit if we can't find it. */

  char chem_file1[MAX_LINE];
  strcpy (chem_file1, chem_file);
  f = fopen (chem_file1, "r");
  if (!f)
    {
      strncpy (chem_file1, PKGDATADIR, sizeof (chem_file1) - 1);
      strncat (chem_file1, "/",
               sizeof (chem_file1) - strlen (chem_file1) - 1);
      strncat (chem_file1, chem_file,
               sizeof (chem_file1) - strlen (chem_file1) - 1);
      f = fopen (chem_file1, "r");
      if (!f)
        {
          fprintf (stderr, "astrochem: error: can't find %s.\n", chem_file);
          return EXIT_FAILURE;
        }
    }
  fclose (f);

  // Get the number of reaction and estimates number of species to alloc
  int n_reactions = get_nb_active_line (chem_file1);
  if( n_reactions == -1 )
    {
      return EXIT_FAILURE;
    }
  if (n_reactions == 0)
    {
      fprintf (stderr,
               "astrochem: error: the number of reactions is zero.\n");
      return EXIT_FAILURE;
    }
  int n_alloc_species = n_reactions / 10;
  if (n_alloc_species == 0)
    {
      n_alloc_species = 1;
    }

  //Allocate network dynamic array
  if( alloc_network (network, n_alloc_species, n_reactions) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  network->n_species = 0;
  if (verbose >= 1)
    {
      fprintf (stdout, "Reading reactions network from %s... ", chem_file1);
      fflush (stdout);
    }


  f = fopen (chem_file1, "r");
  /* Loop over the lines, and look for the reactants and products, and
     the parameters of the reactions. */
  int n = 0;
  while (fgets (line, MAX_LINE, f) != NULL)
    {
      if (line[0] == '#')
        continue;               /* Skip comments. */

      if (n >= network->n_reactions)
        {
          fprintf (stderr,
                   "astrochem: error: incorect number of reactions exceed %i,"
                   "file %s may be corrupt.\n", network->n_reactions,
                   chem_file);
          return EXIT_FAILURE;
        }

      char *tmpLine = line;

      // Separating reaction line ( reactants ) -> ( products params )
      char* reaction_arrow = strchr( tmpLine, '>' );
      *(reaction_arrow-2) = '\0';
      char* reactants = tmpLine;
      char* products = reaction_arrow+1;

      // Parsing reactants
      char* specie;
      int nreactants = 0;

      // First non-whitespace char
      specie = strtok( reactants, " " );
      while( specie != NULL )
        {
          // Not a '+', it is a specie
          if( strcmp( specie, "+" ) != 0 )
            {
              // Check number of reactant
              if( nreactants == MAX_REACTANTS )
                {
                  fprintf (stderr,
                           "astrochem: error: number of reactant %i, is greater than %i,"
                           "file %s may be corrupt.\n", nreactants+1, MAX_REACTANTS ,
                           chem_file1);
                  return EXIT_FAILURE;
                }

              // Add specie in network and in reaction
              // Not storing some elements
              if ((strcmp ( specie, "cosmic-ray") != 0) &&
                  (strcmp ( specie, "uv-photon") != 0) &&
                  (strcmp ( specie, "photon") != 0))
                {
                  network->reactions[n].reactants[ nreactants ] =  add_species (specie, network);
                  if( network->reactions[n].reactants[ nreactants ] == -1 )
                    {
                      return EXIT_FAILURE;
                    }

                  // Increment the number of reactants
                  nreactants++;
                }
            }
          specie = strtok( NULL, " " );
        }

      int nproducts = 0;
      bool specie_ready = true;
      // First non-whitespace char
      specie = strtok( products, " " );
      while( specie != NULL )
        {
          // Found a '+' , be ready for next specie
          if( strcmp( specie, "+" ) == 0 )
            {
              specie_ready = true;
            }
          // Found a specie
          else if( specie_ready )
            {
              specie_ready = false;
              // Check number of products
              if( nproducts == MAX_PRODUCTS )
                {
                  fprintf (stderr,
                           "astrochem: error: number of products %i, is greater than %i,"
                           "file %s may be corrupt.\n", nproducts+1, MAX_PRODUCTS ,
                           chem_file1);
                  return EXIT_FAILURE;
                }

              // Add specie in network and in reaction
              // Not storing some elements
              if ((strcmp ( specie, "cosmic-ray") != 0) &&
                  (strcmp ( specie, "uv-photon") != 0) &&
                  (strcmp ( specie, "photon") != 0))
                {

                  network->reactions[n].products[ nproducts ] =  add_species (specie, network);
                  if( network->reactions[n].products[ nproducts ] == -1 )
                    {
                      return EXIT_FAILURE;
                    }

                  // Increment the number of reactants
                  nproducts++;
                }
            }
          // Found a char, wich is not a '+', without being ready for a specie : end of products
          else
            {
              break;
            }
          // Point to next non-whitespace char
          specie = strtok( NULL, " " );
        }

      // After the products, there is reaction params, wich need to be stored also

      // Alpha has already been read as the last products
      if (sscanf ( specie, "%lf",
                   &network->reactions[n].alpha) != 1)
        {
          fprintf (stderr, "astrochem: error: incorrect network file in %s line %i.\n",
                   chem_file1, n+1 );
          return EXIT_FAILURE;
        }

      // The remaining params
      char* params =  strtok( NULL, "" );
      if (sscanf ( params, "%lf %lf %d %d",
                   &network->reactions[n].beta,
                   &network->reactions[n].gamma,
                   &network->reactions[n].reaction_type,
                   &network->reactions[n].reaction_no) != 4)
        {
          fprintf (stderr, "astrochem: error: incorrect network file in %s line %i.\n",
                   chem_file1, n+1 );
          return EXIT_FAILURE;
        }
      // Next reaction
      n++;
    }
  // Check number of reactions
  if (n != network->n_reactions)
    {
      fprintf (stderr,
               "astrochem: error: incorect number of reactions %i, different from %i,"
               "file %s may be corrupt.\n", n, network->n_reactions,
               chem_file1);
      return EXIT_FAILURE;
    }
  /* Free non used memory space */
  if( realloc_network_species (network, network->n_species) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }
  if (verbose >= 1)
    {
      fprintf (stdout, "Found %d reactions involving %d species.\n",
               network->n_reactions, network->n_species);
    }
  /* Close the file. */
  fclose (f);
  return EXIT_SUCCESS;
}