/*
Copyright 2007, Freie Universitaet Berlin. All rights reserved.

These sources were developed at the Freie Universität Berlin, Computer
Systems and Telematics group.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

- Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

This software is provided by FUB and the contributors on an "as is"
basis, without any representations or warranties of any kind, express
or implied including, but not limited to, representations or
warranties of non-infringement, merchantability or fitness for a
particular purpose. In no event shall FUB or contributors be liable
for any direct, indirect, incidental, special, exemplary, or
consequential damages (including, but not limited to, procurement of
substitute goods or services; loss of use, data, or profits; or
business interruption) however caused and on any theory of liability,
whether in contract, strict liability, or tort (including negligence
or otherwise) arising in any way out of the use of this software, even
if advised of the possibility of such damage.

This implementation was developed by the CST group at the FUB.

For documentation and questions please use the web site
http://scatterweb.mi.fu-berlin.de and the mailinglist
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
Berlin, 2007
*/

#include <ScatterWeb.System.h>
#include <ScatterWeb.CC1020.h>
#include <string.h>

bool Process_radioHandler(struct netpacket_handler_args* args)
{
	/*
		Notes: * The received packet data becomes invalid when this handler is completed.
	*/

	// Pass packet through command interface
	if ( Cmd_radioHandler( args ) ) {
		// packet handled by messaging module
		return true;
	}

	if ( args->cpaResult == ADRCOMP_OTHER ) {
		// intercept packets for other nodes here
	} else if ( args->cpaResult == ADRCOMP_BROADCAST ) {
		// process broadcast packets here
	} else {
		// process packets for this node here
		switch ( args->netheader->type ) {
			case PING_REQUEST_PACKET:
				// send back to receiver's address
				Net_sendPong(args->netheader->from);
				break;

			case PING_REPLY_PACKET:
				LOG_LOW( "PONG from %u", args->netheader->from );
				break;



			/*#####################################*/
			/*#####################################*/
			/*#####################################*/
			case USERDEFINED_PACKET+10:
				printf("Received message for me\n");
				break;

			/*#####################################*/
			/*#####################################*/
			/*#####################################*/


			default:
				return false;		// packet not handled (unknown)
		}
		return true;				// packet handled
	}
	return false;					// packet not handled (other destination)
}







/*#####################################*/
/*#####################################*/
/*#####################################*/
COMMAND( message_send, CMDFLAG_SERIAL, cmdargs ) {
uint16_t count = Cmd_SplitArgs( cmdargs->args );
char* arg = (char*) cmdargs->args;
uint8_t to = 0, size = 0;
netpacket_send_args_t npsargs;
uint8_t packet[1000];
int i = 0;

	if (count < 2) {
	
		printf("USAGE: message_send <to> <size>\n");
		return;
	}

	/*Catch arguments*/
	to = atoi(arg);
	arg = ( char* )Cmd_GetNextArg( arg );
	size = atoi(arg);

	/*Init "payload"*/
	for (i=0; i<1000; i++) packet[i] = 0;
	
	/*Send*/
	Net_sendArgsInit(&npsargs);
	npsargs.netheader.to = to;
	npsargs.netheader.type = USERDEFINED_PACKET+10;
	npsargs.netheader.flags = 0;
	npsargs.payload[0].buffer = (uint8_t*) packet;
	npsargs.payload[0].size = size;
	Net_send(&npsargs);


}
/*#####################################*/
/*#####################################*/
/*#####################################*/










void Process_sensorHandler(void* sdata)
{

}

void Process_init()
{
	System_registerCallback( C_RADIO, ( fp_vp )Process_radioHandler );
	System_registerCallback( C_SENSOR, ( fp_vp )Process_sensorHandler );
}

/**
 * @brief	Ping request
 *
 * Send a ping request to another node
 * Example: ping 24
 */
COMMAND( ping, 0, cmdargs )
{
	uint16_t node;

	node = String_parseUint16( cmdargs->args, NULL );
	if ( node > 0 ) {
		Net_sendPing((netaddr_t)node);
	}
}

/**
 * @brief Echo Arguments
 *
 * Testcase 1: long command name
 * Testcase 2: argument splitting
 */
COMMAND( echoargs, CMDFLAG_SERIAL, cmdargs )
{
	uint16_t i;
	uint16_t count = Cmd_SplitArgs( cmdargs->args );
	char* arg = (char*) cmdargs->args;

	for ( i = 1; i <= count; i++ ) {
		printf("%u: %*s\n", i, strlen(arg), arg);
		arg = ( char* )Cmd_GetNextArg( arg );
	}
	snprintf(cmdargs->response_args, cmdargs->response_bufsize, "%u args found", count );
}

// Software header.
APP(
    APPH_STATIC_APP,	// Application Type
    __APP_NAME,			// Application Name (8 chars)
    1, 0				// Aplication Version (2 bytes)
);
