/*====================================================================*
 *
 *   arpc2.c - Atheros Message Logger;
 *
 *   this program is a template and may fail to compile or execute;
 *   
 *.  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>
 *
 *--------------------------------------------------------------------*/

#define _GETOPT_H

/*====================================================================*
 *   system header files;
 *--------------------------------------------------------------------*/

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

/*====================================================================*
 *   custom header files;
 *--------------------------------------------------------------------*/

#include "../tools/getoptv.h"
#include "../tools/putoptv.h"
#include "../tools/memory.h"
#include "../tools/number.h"
#include "../tools/types.h"
#include "../tools/flags.h"
#include "../tools/files.h"
#include "../tools/error.h"
#include "../int6k/intellon.h"
#include "../int6k/homeplug.h"
#include "../int6k/message.h"
#include "../int6k/problem.h"
#include "../ether/channel.h"
#include "../mme/mme.h"

/*====================================================================*
 *   custom source files;
 *--------------------------------------------------------------------*/

#ifndef MAKEFILE
#include "../tools/getoptv.c"
#include "../tools/putoptv.c"
#include "../tools/version.c"
#include "../tools/hexdump.c"
#include "../tools/uintspec.c"
#include "../tools/todigit.c"
#include "../tools/error.c"
#endif

#ifndef MAKEFILE
#include "../ether/channel.c"
#include "../ether/openchannel.c"
#include "../ether/closechannel.c"
#include "../ether/sendpacket.c"
#include "../ether/readpacket.c"
#endif

/*====================================================================*
 *    program constants; 
 *--------------------------------------------------------------------*/

#define PLCDEVICE "PLC"
#define ARPC_ASCII_EACH '2'
#define ARPC_ASCII_SOME '1'
#define ARPC_ASCII_EXIT '0'

/*====================================================================*
 *
 *   void ARPCPrint2 (FILE * fp, void const * memory, size_t extent);
 *
 *   print formatted VS_ARPC payload on the specified output stream;
 *   return the value of the first character of the format string as
 *   the function value;
 *
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2009 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *--------------------------------------------------------------------*/

void ARPCPrint2 (FILE * fp, void const * memory, size_t extent) 

{

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

	struct __packed vs_arpc_data 
	{
		uint32_t BYPASS;
		uint16_t ARPCID;
		uint16_t DATALENGTH;
		uint8_t DATAOFFSET;
		uint8_t RESERVED [3];
		uint16_t ARGOFFSET;
		uint16_t STROFFSET;
		uint16_t ARGLENGTH;
		uint16_t STRLENGTH;
		uint8_t LIST [1];
	}
	* data = (struct vs_arpc_data *)(memory);

#ifndef __GNUC__
#pragma pack (pop)
#endif

#if 0

	fprintf (fp, "BYPASS=%d ", data->BYPASS);
	fprintf (fp, "ARPCID=%d ", data->ARPCID);
	fprintf (fp, "DATALENGTH=%d ", LE16TOH (data->DATALENGTH));
	fprintf (fp, "DATAOFFSET=%d ", data->DATAOFFSET);
	fprintf (fp, "ARGOFFSET=%d ", LE16TOH (data->ARGOFFSET));
	fprintf (fp, "STROFFSET=%d ", LE16TOH (data->STROFFSET));
	fprintf (fp, "ARGLENGTH=%d ", LE16TOH (data->ARGLENGTH));
	fprintf (fp, "STRLENGTH=%d ", LE16TOH (data->STRLENGTH));
	fprintf (fp, "FORMAT=[%s]\n", &data->LIST [data->STROFFSET]);

#endif

	vfprintf (fp, (char *)(&data->LIST [data->STROFFSET]), (void *)(&data->LIST [data->ARGOFFSET]));
	fprintf (fp, "\n");
	return;
}

/*====================================================================*
 *   
 *   void function (struct channel * channel);
 *   
 *   capture and record VS_ARPC messages from any Atheros powerline 
 *   device; devices must be programmed to send these messages;
 *   the first payload character determines message disposition;
 *
 *   this function has no practical use under operation conditions;
 *
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2009 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *--------------------------------------------------------------------*/

static void function (struct channel * channel) 

{
	struct message message;

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

	struct __packed vs_arpc_ind 
	{
		struct header_eth ethernet;
		struct header_int intellon;
		uint16_t RDATALENGTH;
		uint8_t RDATAOFFSET;
		uint8_t RDATA [1];
	}
	* indicate = (struct vs_arpc_ind *)(&message);

#ifndef __GNUC__
#pragma pack (pop)
#endif

	signed length;
	while ((length = readpacket (channel, &message, sizeof (message))) >= 0) 
	{
		if (!length) 
		{
			continue;
		}
		if (message.ethernet.MTYPE != htons (HOMEPLUG_MTYPE)) 
		{
			continue;
		}
		if (message.intellon.MMTYPE != HTOLE16 (VS_ARPC | MMTYPE_IND)) 
		{
			continue;
		}
		if (message.intellon.MMV != INTELLON_MMV) 
		{
			continue;
		}
		ARPCPrint2 (stdout, &indicate->RDATA [indicate->RDATAOFFSET], LE16TOH (indicate->RDATALENGTH));
	}
	return;
}

/*====================================================================*
 *   
 *   int main (int argc, char * argv[]);
 *   
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2009 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *--------------------------------------------------------------------*/

int main (int argc, const char * argv []) 

{
	extern struct channel channel;
	static const char *optv [] = 
	{
		"i:qt:u:v",
		PUTOPTV_S_DIVINE,
		"Atheros Message Logger",

#if defined (WINPCAP) || defined (LIBPCAP)

		"i s\tuse host interface s [" CHANNEL_ETHDEVICE "]",

#else

		"i n\tuse host interface n [2]",

#endif

		"q\tsuppress output on stderr",
		"t n\ttimeout is (n) milliseconds [50]",
		"u s\tlog unwanted frames to file (s)",
		"v\tverbose messages on stdout",
		(const char *) (0) 
	};
	signed c;
	if (getenv (PLCDEVICE)) 
	{

#if defined (WINPCAP) || defined (LIBPCAP)

		channel.id = atoi (getenv (PLCDEVICE));

#else

		channel.name = strdup (getenv (PLCDEVICE));

#endif

	}
	optind = 1;
	opterr = 1;
	optmin = 0;
	while ((c = getoptv (argc, argv, optv)) != -1) 
	{
		switch ((char) (c)) 
		{
		case 'i':

#if defined (WINPCAP) || defined (LIBPCAP)

			channel.id = atoi (optarg);

#else

			channel.name = optarg;

#endif

			break;
		case 'q':
			_setbits (channel.flags, CHANNEL_SILENCE);
			break;
		case 't':
			channel.timeout = (unsigned)(uintspec (optarg, 0, UINT_MAX));
			break;
		case 'v':
			_setbits (channel.flags, CHANNEL_VERBOSE);
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;

#ifndef WIN32

/*
 *      raw packets require root priviledges on linux; one does not have to be
 *      root when this program is installed setuid using 'chown root:root' and 
 *      'chmod 4555';
 */

	if (geteuid ()) 
	{
		error (1, EPERM, INT6K_NEEDROOT);
	}

#endif

	openchannel (&channel);
	function (&channel);
	closechannel (&channel);
	return (0);
}