/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  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.
 *
 *
 *                      ********************
 *
 *
 *  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 Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  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 "Fournisseur.h"
#include "Debug.h"
#include "Declaration_Constante.h"
#include "Macro.h"

#define FOURNISSEUR_INCONNU "???"

gchar    *Recherche_Fournisseur_Equipement (unsigned char *Mac_Adresse)
{
	gchar    *Fournisseur = NULL;

	Mac_Adresse[8] = 0;
	Fournisseur = g_hash_table_lookup(Hash_Fournisseur,Mac_Adresse);
	Mac_Adresse[8] = ':';
	if (Fournisseur)
		return Fournisseur;
	return FOURNISSEUR_INCONNU;
	
	/*struct struct_MacListe *Mac = NULL;
	gchar    *Fournisseur = NULL;
	gint      i, Nb_Objet_Liste;

	ERROR_if (Mac_Adresse == NULL);
	i = 0;
	Nb_Objet_Liste = g_list_length (GList_Fournisseur);
	while ((i < Nb_Objet_Liste) && (Fournisseur == NULL)) {
		Mac = g_list_nth_data (GList_Fournisseur, i);
		if (strncmp (Mac->MAC, Mac_Adresse, 8) == 0) {
			Fournisseur = Mac->Constructeur;
		}
		i++;
	}
	if (Fournisseur == NULL)
		Fournisseur = "???";
	return Fournisseur;*/
}


void Creer_Liste_Fournisseur (void)
{
	FILE     *Fichier_MacListe = NULL;
	unsigned char *MAC = NULL;
	gchar    *Constructeur = NULL;

	char      buf[512];

	if ((Fichier_MacListe = fopen (PATH_CONF "Mac", "r")) == NULL)
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "%s", strerror (errno));

	Hash_Fournisseur = g_hash_table_new((GHashFunc)g_str_hash,(GEqualFunc)g_str_equal);

	while (fgets (buf, sizeof (buf), Fichier_MacListe)) {
/*		MALLOC (Mac, sizeof (struct struct_MacListe));
		strncpy (Mac->MAC, buf, 8);
		Mac->MAC[8] = '\0';
		Mac->Constructeur = strdup (buf + 9);
		Mac->Constructeur[strlen (Mac->Constructeur) - 1] = 0;
		G_LIST_APPEND (GList_Fournisseur, Mac);*/
		MAC = NULL;
		MALLOC (MAC, sizeof (unsigned char) * (8+1));
		strncpy (MAC, buf, 8);
		MAC[8] = '\0';
		Constructeur = strdup (buf + 9);
		Constructeur[strlen (Constructeur) - 1] = 0;
		g_hash_table_insert(Hash_Fournisseur,MAC,Constructeur);
	}
	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "hash_table: Creation de la liste des Adresses MAC (%d adresses MAC trouvees)",g_hash_table_size(Hash_Fournisseur));
	fclose (Fichier_MacListe);
	return;

}
