/*
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.Debuglog.c
 * @brief	ScatterWeb logging functions for debugging purposes
 * @ingroup	logging 
 * 
 * @version 1.2
 * @date	Feb 2007
 * 
 * @author Michael Baar
 */

#include "ScatterWeb.System.h"
#include "ScatterWeb.Debuglog.h"

__attribute__((packed))
typedef struct debuglog_header {
	uint8_t msg_length;
} debuglog_header_t;

uint8_t Debuglog_level;

#if DEBUGLOG_LEVEL > 0
	const char Debuglog_level_strings[] =	"NONE\0"	"LOW\0"	"MEDIUM\0" "\0"	"HIGH\0"	"VERBOSE\0" "\3";

	int _Debuglog_writer(int c);
	void _Debuglog_queue_packet();
	void Debuglog_radio(uint8_t level, const char* fmt, ...);
	void _Debuglog_null(uint8_t level, const char* fmt, ...);
	
	fp_log_t Debuglog_print;
	
	// additional buffer is neccessary because termreply is use
	// as argument to print function
	debuglog_header_t	dlbuf_header;
	char				dlbuf[TERM_BUFSIZE];

	/**
	 * @brief Send log message packet
	 */
	void _Debuglog_queue_packet() {
		netpacket_send_args_t npsargs;
		
		Net_sendArgsInit(&npsargs);
		npsargs.netheader.type = DEBUGLOG_PACKET;
		npsargs.payload[0].buffer = (uint8_t*)&dlbuf_header;
		npsargs.payload[0].size = sizeof(debuglog_header_t);
		npsargs.payload[1].buffer = (uint8_t*)dlbuf;
		npsargs.payload[1].size = dlbuf_header.msg_length;
		npsargs.netheader.to = NETADDR_BROADCAST;
		
		Net_send(&npsargs);
	}
	
	int _Debuglog_writer(int c) {
		if( dlbuf_header.msg_length == TERM_BUFSIZE ) {
			// send part of message
			// if message can't be queued, discard it and reset buffer		
			_Debuglog_queue_packet();
			dlbuf_header.msg_length = 0;
		}
		
		dlbuf[dlbuf_header.msg_length] = c;
		dlbuf_header.msg_length++;
		return c;
	}

	void Debuglog_radio(uint8_t level, const char* fmt, ...) {
		va_list argp;
	
		va_start(argp, fmt);
		
		#if DEBUGLOG_SERIAL_CC
			vuprintf(Comm_writer, fmt, argp);
			//String_abstractWrite(Comm_writer, fmt, argp);  
			//Comm_writer('\r');
			Comm_writer('\n');
		#endif	
		
		if( DEBUGLOG_LEVEL_TEST(level) ) {
			// initialize buffer
			dlbuf_header.msg_length = 0;
			
			// generate message
			//String_abstractWrite(_Debuglog_writer, fmt, argp);
			vuprintf(_Debuglog_writer, fmt, argp);
			
			// send message
			// if message can't be queued, discard it and reset buffer
			_Debuglog_queue_packet();
			
			_NOP();
		}
		va_end(argp);	
	}
	
	void Debuglog_serial(uint8_t level, const char *fmt, ...) {
		uint8_t component = level & ~DEBUGLOG_LEVEL_MASK;
		level &= DEBUGLOG_LEVEL_MASK;

		if( !DEBUGLOG_LEVEL_TEST(level) ) {
			// requested level not reached
			return;
		} else if( (component > 0) && ((Debuglog_level & component) != component) ) {
			// requested component not active
			return;
		} else {
			va_list argp;
			if( uart_lock(UART_MODE_RS232) ) {
				va_start(argp, fmt);

				Comm_writer('#');
				vuprintf(Comm_writer, fmt, argp);
				//String_abstractWrite(Comm_writer, fmt, argp);
				Comm_writer('\r'); Comm_writer('\n');

				va_end(argp);
				uart_unlock(UART_MODE_RS232);
			}
		}
	}

	void Debuglog_null(uint8_t level, const char* fmt, ...) {
		return;
	}
#else
	const char Debuglog_level_strings[] =	"NONE\0" "\3";
#endif

void Debuglog_print_packet(netheader_t header, const void* payload, uint16_t size) {
	debuglog_header_t* dlh;
	dlh = (debuglog_header_t*)payload;

	printf("#%u:%*s\n",
			header.from,
			dlh->msg_length, (char*)(dlh + 1)
	);
}

void Debuglog_setHandler(fp_log_t handler) {
	#if DEBUGLOG_LEVEL > 0
		Debuglog_print = handler;
	#endif
}
