/*
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
*/

/**
 * @file		ScatterWeb.Cmd.Comm.c
 * @brief		Command Engine Radio I/O
 * @ingroup		cmd
 * @version		$Revision: 130 $
 *
 * <p>See <a href="group__cmd.html">Command Engine</a> for documentation.</p>
 *
 * $Id: ScatterWeb.Cmd.c 122 2007-10-30 11:56:54Z baar@PCPOOL.MI.FU-BERLIN.DE $
 */

/**
 * @addtogroup cmd
 * @{
 */
 
#include <string.h>
#include <ScatterWeb.System.h>
#include "ScatterWeb.Cmd.internal.h"

bool Cmd_radioHandler(struct netpacket_handler_args* args) {
	if( args->cpaResult & (ADRCOMP_BROADCAST|ADRCOMP_THIS) ) {
		switch(args->netheader->type) {
			case CMD_REQ_PACKET:
				// remote command request
				_Cmd_RadioRequest(args);
				return true;
				
			case CMD_RPL_PACKET:
				// remote command reply
				_Cmd_RadioReply(args);
				return true;
		}
	}
	return false;
}

///////////////////////////////////////////////////////////////////////////////
// private helper functions
///////////////////////////////////////////////////////////////////////////////


void _Cmd_Forward(
	const netaddr_t*			pDestination,
	const cmd_header_t (*const	pCmd),
	const void*					data,
	const uint8_t				dataLength
) {
	const uint8_t headerLength = _Cmd_GetHeaderSize(pCmd);
	netpacket_send_args_t npsargs;

	// fill structure with default values
	Net_sendArgsInit( &npsargs );

	// fill network header custom values
	npsargs.netheader.type = CMD_REQ_PACKET;		// application protocol
	npsargs.netheader.to = *pDestination;			// receiver's address
	npsargs.netheader.flags = NET_TLFLAG_ACKREQ;	// network layer config flags
	npsargs.payload[0].buffer = (void*)pCmd;
	npsargs.payload[0].size = headerLength;
	npsargs.payload[1].buffer = (void*)data;
	npsargs.payload[1].size = dataLength;

	#if CMD_ENABLE_LOGGING
	_Cmd_logCommand(
		'f', pDestination,
		pCmd, headerLength + dataLength);
	#endif

	Net_send(&npsargs);
}

void _Cmd_SendReply(
	const netheader_t (*const			netheader),
	char* rpl_buffer, uint8_t rpl_size
) {
	// reply
	if( rpl_size > 0 ) {
		netpacket_send_args_t npsargs;

		// fill structure with default values
		Net_sendArgsInit( &npsargs );

		// fill network header custom values
		npsargs.netheader.type = CMD_RPL_PACKET;		// application protocol
		npsargs.netheader.to = netheader->from;			// receiver's address
		npsargs.netheader.flags = NET_TLFLAG_ACKREQ;	// network layer config flags
		npsargs.payload[0].buffer = rpl_buffer;
		npsargs.payload[0].size = rpl_size;

		#if CMD_ENABLE_LOGGING
		_Cmd_logCommand(
			'r', &npsargs.netheader.to,
			(cmd_header_t*)rpl_buffer, rpl_size
		);
		#endif
		
		// queue packet for sending
		Net_send( &npsargs );
	}
}

void _Cmd_RadioRequest(struct netpacket_handler_args* args)
{
	cmd_header_t*	cmd_header = (cmd_header_t*)args->payload;
	char* buffer = _Cmd_GetBuffer();
	if( buffer == NULL ) {
		cmd_header_t	state_reply;
		state_reply.msgref = cmd_header->msgref;
		state_reply.flags = CMD_RPLFLAG_ERROR;
		_Cmd_StateReply(&state_reply, CMD_FUNC_STATE_BUSY);
		_Cmd_SendReply(args->netheader, (char*)&state_reply, sizeof(cmd_header_t));

		LOG_VERBOSE("(BINCMD) Busy (no buffer)", NULL);
	} else {
		uint16_t			rpl_size;

		#if CMD_ENABLE_LOGGING
		_Cmd_logCommand(
			'q', &args->netheader->from,
			cmd_header, args->payload_size
		);
		#endif

		rpl_size = Cmd_Run(
			cmd_header,
			NULL,
			args->payload_size,
			buffer,
			TERM_BUFSIZE,
			false
		);

		if( rpl_size > 0 ) {
			_Cmd_SendReply(args->netheader, buffer, rpl_size);
		}

		_Cmd_FreeBuffer(buffer);
	}
}

void _Cmd_RadioReply(struct netpacket_handler_args* args)
{
	cmd_header_t*	cmd_header = (cmd_header_t*)args->payload;

	#if CMD_ENABLE_LOGGING
	_Cmd_logCommand(
		'r', &args->netheader->from,
		cmd_header, args->payload_size
	);
	#endif
	
  #if CMD_ASCII_MODE
	if( CMD_IS_ASCII(cmd_header) ) {
		_Cmd_AsciiPrintReply(&args->netheader->from, args->payload_size, cmd_header);
	}
	else
  #endif
	{
		_Cmd_BinPrintReply(&args->netheader->from, args->payload_size, cmd_header);
	}
}

/** @} */
