/*====================================================================*
 *
 *   int WaitForStart (struct int6k *int6k, char string [], signed length);
 *
 *   int6k.h
 *
 *   poll the local device with VS_SW_VER messages until it responds  
 *   or the retry count expires; return 0 if the device responds in 
 *   time or -1 if it does not;
 *
 *   if the device responds then et the hardwareID field of the int6k
 *   structure to indicate the true hardware platform; special checks 
 *   are needed because the INT6300 bootloader incorrectly identifies 
 *   that chipset as an INT6000 and the AR7400 bootloader incorrectly 
 *   identifies that chipset as an INT6400; INT6400 and AR7400 return
 *   the IDENT and STEP_NUMBER fields to correctly identify chipsets;
 *
 *   the MVERSION field returned by the firmware is longer than that
 *   returned by the bootloader; we assume the short version in case
 *   we need to access the IDENT and STEP_NUMBER fields;
 *
 *   See Atheros HomePlug AV Firmware Technical Reference Manual 
 *   for more information;
 *   
 *   This software and documentation is the property of Atheros 
 *   Corporation, Ocala, Florida. It is provided 'as is' without 
 *   expressed or implied warranty of any kind to anyone for any 
 *   reason. Atheros assumes no responsibility or liability for 
 *   errors or omissions in the software or documentation and 
 *   reserves the right to make changes without notification. 
 *   
 *   Atheros customers may modify and distribute the software 
 *   without obligation to Atheros. Since use of this software 
 *   is optional, users shall bear sole responsibility and 
 *   liability for any consequences of it's use. 
 *   
 *.  Atheros Powerline Toolkit for HomePlug AV; 
 *:  Published 2007 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *   Contributor(s):
 *      Charles Maier <charles.maier@atheros.com>
 *
 *--------------------------------------------------------------------*/

#ifndef WAITFORSTART_SOURCE
#define WAITFORSTART_SOURCE

#include <stdint.h>
#include <unistd.h>
#include <memory.h>
#include <string.h>

#include "../int6k/int6k.h"
#include "../int6k/chipset.h"
#include "../tools/memory.h"
#include "../tools/endian.h"
#include "../tools/error.h"

int WaitForStart (struct int6k *int6k, char string [], signed length) 

{
	struct channel * channel = (struct channel *)(int6k->channel);
	struct message * message = (struct message *)(int6k->message);

#ifndef __GNUC__
#pragma pack (push,1)
#endif

	struct __packed vs_sw_ver_confirm 
	{
		struct header_eth ethernet;
		struct header_int intellon;
		uint8_t MSTATUS;
		uint8_t MDEVICEID;
		uint8_t MVERLENGTH;
		char MVERSION [64];
		uint8_t RESERVED;
		uint32_t IDENT;
		uint32_t STEP_NUMBER;
	}
	* confirm = (struct vs_sw_ver_confirm *) (message);

#ifndef __GNUC__
#pragma pack (pop)
#endif

	unsigned retry = int6k->retry;
	while (retry--) 
	{
		signed status;
		memset (message, 0, sizeof (struct message));
		EthernetHeader (&message->ethernet, channel->peer, channel->host);
		IntellonHeader (&message->intellon, (VS_SW_VER | MMTYPE_REQ));
		int6k->packetsize = ETHER_MIN_LEN;
		if (SendMME (int6k) <= 0) 
		{
			error ((int6k->flags & INT6K_BAILOUT), ECANCELED, INT6K_CANTSEND);
			return (-1);
		}
		status = ReadMME (int6k, (VS_SW_VER | MMTYPE_CNF));
		if (status < 0) 
		{
			error ((int6k->flags & INT6K_BAILOUT), ECANCELED, INT6K_CANTREAD);
			return (-1);
		}
		if (status > 0) 
		{
			if (confirm->MSTATUS) 
			{
				Failure (int6k, "Device will not start");
				return (-1);
			}
			int6k->hardwareID = confirm->MDEVICEID;
			if (int6k->hardwareID > CHIPSETS) 
			{
				int6k->hardwareID = 0x00;
			}
			strncpy (string, confirm->MVERSION, length);

#if 1

/*
 *   this code block is needed because the bootloader returns the wrong device identifier for
 *   both the INT6300 and the AR7400 chipsets; additionally, the IDENT and STEP_NUMBER FIELDS
 *   are not valid for the INT6000 and INT6300 chipsets;
 */

			if (!strcmp (string, "BootLoader")) 
			{
				if (int6k->hardwareID == INT6000A1) 
				{
					int6k->hardwareID = INT6300A0;
				}
#if 1
				if (int6k->hardwareID == INT6400A0) 
				{
					if (confirm->IDENT == LE32TOH (0x6400)) 
					{
						int6k->hardwareID = INT6400A0;
					}
					if (confirm->IDENT == LE32TOH (0x7400)) 
					{
						int6k->hardwareID = AR7400A0;
					}
				}
#endif
			}

#endif

			return (0);
		}
		if (retry) 
		{
			SLEEP (int6k->timer);
		}
	}
	return (-1);
}

#endif