/* ***********************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 "Ip_Dynamic.h"
#include "Liste_Equipement.h"
#include "Debug.h"
#include "Thread.h"
#include "Semaphore.h"
#include "Macro.h"


void Traite_Equipement_Marquer_Ip_Change (gpointer * Donnee)
{
	struct Struct_Liste_Equipement *Equipement = NULL;
	struct struct_Ip_Modifier Message;
	unsigned char *ID_Equipement = (unsigned char *) Donnee;
	gint      Nb_Objet_Liste, i;
	gboolean  Equipement_Trouver;
	
#ifdef __LOG_ENABLE
	Create_Log(CATEG_IP_DYNAMIC,NULL,NULL,TRUE);
#endif
	ERROR_if (Donnee == NULL);
	Equipement_Trouver = FALSE;


	i = 0;
	do {
		Equipement = NULL;
		PROTECT_CODE(Semaphore_Equipement);
		Nb_Objet_Liste = g_list_length (GList_Equipement);
		if (i < Nb_Objet_Liste) {
			Equipement = g_list_nth_data (GList_Equipement, i);
		}
		DEPROTECT_CODE(Semaphore_Equipement);
		if (Equipement != NULL) {
		
			if (memcmp (Equipement->ID_Equipement, ID_Equipement, MAC_ADDR_LEN) == 0) {
				Equipement->Rafraichissement_Etat_En_Cours = TRUE;
				Equipement->Compteur_Notification = 0;
				Equipement->Equipement_Allumer = FALSE;
				Envoie_Arp_Equipement (Equipement, 4);
				sleep (2);
				Equipement->Rafraichissement_Etat_En_Cours = FALSE;
				if (!Equipement->Equipement_Allumer)
					Equipement_Trouver = TRUE;// si trouvé mais en ancienne ip ne répond pas
			}

		}
		i++;
	} while ((i < Nb_Objet_Liste) && (!Equipement_Trouver));

	if (Equipement_Trouver) { 
	
		bzero (&Message, sizeof (struct struct_Ip_Modifier));
		Message.ID_Reseau = Info_Demarrage.ID_Reseau;
		memcpy (Message.ID_Equipement, Equipement->ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
		memcpy (Message.Nouvelle_Adresse_Ip, &ID_Equipement[6], IP_ADDR_LEN + 1);
		Communication_Envoie_Donnee (IP_MODIFIER, (gpointer *) & Message, sizeof (Message));
		memcpy (Equipement->Adresse_Ip, &ID_Equipement[MAC_ADDR_LEN], IP_ADDR_LEN + 1);
		sprintf (Equipement->Adresse_Ip_Char, "%d.%d.%d.%d", Equipement->Adresse_Ip[0], Equipement->Adresse_Ip[1], Equipement->Adresse_Ip[2], Equipement->Adresse_Ip[3]);
		memcpy (&Equipement->ID_Equipement[6], &ID_Equipement[MAC_ADDR_LEN], IP_ADDR_LEN + 1);
		Equipement->Equipement_Allumer = TRUE;
	}


	PROTECT_CODE(Semaphore_Equipement);
	Nb_Objet_Liste = g_list_length (GList_Equipement);
	for (i = 0; i < Nb_Objet_Liste; i++) {
		Equipement = g_list_nth_data (GList_Equipement, i);
		if (memcmp (Equipement->ID_Equipement, ID_Equipement, MAC_ADDR_LEN) == 0) {
			if (Equipement->Marquage_Change_Ip) 
				Equipement->Marquage_Change_Ip = FALSE;
		}
	}
	DEPROTECT_CODE(Semaphore_Equipement);
	MYFREE (ID_Equipement);
	Stop_Thread (NULL);
}


gint Marque_Equipement_Avec_Meme_Adresse_MAC (unsigned char *ID_Equipement)
{
	struct Struct_Liste_Equipement *Equipement = NULL;
	gint      Nb_Objet_Liste, i;
	gboolean  Marquer;

	if (Info_Demarrage.Ip_Dynamic) {
		ERROR_if (ID_Equipement == NULL);
		Marquer = FALSE;
		PROTECT_CODE(Semaphore_Equipement);
		Nb_Objet_Liste = g_list_length (GList_Equipement);
		for (i = 0; i < Nb_Objet_Liste; i++) {
			Equipement = g_list_nth_data (GList_Equipement, i);
			if (memcmp (Equipement->ID_Equipement, ID_Equipement, MAC_ADDR_LEN) == 0) {
				if (Equipement->Marquage_Change_Ip) {
					DEPROTECT_CODE(Semaphore_Equipement);
					return DEJA_EN_COURS_CHANGE_IP;
				}
			
				Equipement->Marquage_Change_Ip = TRUE;
				Marquer = TRUE;
			}
		}
		DEPROTECT_CODE(Semaphore_Equipement);
		if (Marquer)
			return EQUIPEMENT_MARQUER_CHANGE_IP;
		else
			return AUCUN_EQUIPEMENT_MARQUER_CHANGE_IP;
	}
	else
		return AUCUN_EQUIPEMENT_MARQUER_CHANGE_IP;
}

void Recherche_Equipement_Perte_Ip (unsigned char *ID_Equipement)
{
	struct struct_Ip_Perdu Message;
	struct Struct_Liste_Equipement *Equipement = NULL;
	gint      Nb_Objet_Liste, i;

	ERROR_if (ID_Equipement == NULL);
	PROTECT_CODE(Semaphore_Equipement);
	Nb_Objet_Liste = g_list_length (GList_Equipement);
	for (i = 0; i < Nb_Objet_Liste; i++) {
		Equipement = g_list_nth_data (GList_Equipement, i);
		if (memcmp (&Equipement->ID_Equipement[6], &ID_Equipement[6], IP_ADDR_LEN) == 0) {
		
			bzero (&Message, sizeof (struct struct_Ip_Perdu));
			Message.ID_Reseau = Info_Demarrage.ID_Reseau;
			memcpy (Message.ID_Equipement, Equipement->ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
			Communication_Envoie_Donnee (IP_PERDU, (gpointer *) & Message, sizeof (Message));
		
			bzero (&Equipement->ID_Equipement[6], IP_ADDR_LEN);
			bzero (&Equipement->Adresse_Ip, IP_ADDR_LEN);
			strcpy (Equipement->Adresse_Ip_Char, "0.0.0.0");
			Equipement->Equipement_Allumer = FALSE;
			DEPROTECT_CODE(Semaphore_Equipement);
			return;
		}
	}
	DEPROTECT_CODE(Semaphore_Equipement);
	return;
}



void Ip_Equipement_Retrouver (unsigned char *ID_Equipement)
{
	struct struct_Ip_Retrouver Message;
	struct Struct_Liste_Equipement *Equipement = NULL;
	gint      Nb_Objet_Liste, i;

	ERROR_if (ID_Equipement == NULL);
	PROTECT_CODE(Semaphore_Equipement);
	Nb_Objet_Liste = g_list_length (GList_Equipement);
	for (i = 0; i < Nb_Objet_Liste; i++) {
		Equipement = g_list_nth_data (GList_Equipement, i);
		if ((memcmp (Equipement->ID_Equipement, ID_Equipement, MAC_ADDR_LEN) == 0) && (Equipement->ID_Equipement[6] == 0)) {
		
		
			bzero (&Message, sizeof (struct struct_Ip_Retrouver));
			Message.ID_Reseau = Info_Demarrage.ID_Reseau;
			memcpy (Message.ID_Equipement, ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
			Communication_Envoie_Donnee (IP_RETROUVER, (gpointer *) & Message, sizeof (Message));
			memcpy (Equipement->Adresse_Ip, &ID_Equipement[MAC_ADDR_LEN], IP_ADDR_LEN + 1);
			sprintf (Equipement->Adresse_Ip_Char, "%d.%d.%d.%d", Equipement->Adresse_Ip[0], Equipement->Adresse_Ip[1], Equipement->Adresse_Ip[2], Equipement->Adresse_Ip[3]);
			memcpy (&Equipement->ID_Equipement[6], &ID_Equipement[MAC_ADDR_LEN], IP_ADDR_LEN + 1);
			Equipement->Equipement_Allumer = TRUE;
		
		
			DEPROTECT_CODE(Semaphore_Equipement);
			return;
		}
	}
	DEPROTECT_CODE(Semaphore_Equipement);
	debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Failed unknown MAC");
}
