/*====================================================================*
 *
 *   void SessionTask ();
 *
 *   session.h 
 *
 *   inspect session.action and perform indicated action only if the
 *   action is valid; clear session.action on exit;
 *
 *   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 2006-2009 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *   Contributor(s):
 *      Charles Maier <charles.maier@atheros.com>
 *
 *--------------------------------------------------------------------*/

#ifndef SESSIONTASK_SOURCE
#define SESSIONTASK_SOURCE

#include <errno.h>

#include "../cgi/session.h"
#include "../cgi/page.h"

void SessionTask () 

{
	extern struct session session;
	extern struct channel channel;
	extern struct fileinfo pib;
	extern struct fileinfo nvm;
	CGILocalPLD ();
	if (!strcmp (session.action, ACTION_SOURCE)) 
	{
		closechannel (&channel);
		channel.name = session.source;
		openchannel (&channel);
		CGILocalPLD ();
	}
	else if (!strcmp (session.action, ACTION_DEVICE)) 
	{
	}
	else if (!strcmp (session.action, ACTION_NETWORK)) 
	{
		CGISetKey ();
	}
	else if (!strcmp (session.action, ACTION_PRIORITIZE)) 
	{
		status (1, ENOSYS, "This is demonstration software.");
	}
	else if (!strcmp (session.action, ACTION_CONFIGURE)) 
	{
		if ((!pib.data) || (!pib.size)) 
		{
			status (1, ECANCELED, "No PIB file selected");
		}
		CGIWriteMOD (VS_MODULE_PIB, pib.data, pib.size);
		CGIFlashMOD (VS_MODULE_PIB);
		memset (&pib, 0, sizeof (pib));
		memset (&nvm, 0, sizeof (nvm));
	}
	else if (!strcmp (session.action, ACTION_UPGRADE)) 
	{
		if ((!pib.data) && (!pib.size)) 
		{
			status (1, ECANCELED, "No PIB file selected");
		}
		if ((!nvm.data) && (!nvm.size)) 
		{
			status (1, ECANCELED, "No NVM file selected");
		}
		CGIWriteMOD (VS_MODULE_PIB, pib.data, pib.size);
		CGIWriteMOD (VS_MODULE_MAC, nvm.data, nvm.size);
		CGIFlashMOD (VS_MODULE_MAC|VS_MODULE_PIB);
		memset (&pib, 0, sizeof (pib));
		memset (&nvm, 0, sizeof (nvm));
	}
	else if (!strcmp (session.action, ACTION_DEFAULT)) 
	{
		CGIRestore ();
	}
	memset (session.action, 0, sizeof (session.action));
	return;
}

#endif