/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss of reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include <malloc.h>
#include <unistd.h>
#include <sys/file.h>

#include "lpc-internals.h"

/** @addtogroup lpc_prg_lib */
/** @{ */

/**
*	Opens the library.
*
*	The function prepares the library for use. It creates an instance of
*	the opaque state descriptor and returns a pointer to it.
*
*	It is possible to call this function multiple times and use the library
*	on multiple serial ports and targets simultaneously.
*
*	@return			A new, initialised LPC object on success or NULL
*					if the allocation of the state descriptor failed.
*/

LPC		LPC_Open( void )
{
LPC		lpc;

	if ( ( lpc = malloc( sizeof( struct s_lpc ) ) ) ) {

		lpc->log_file = NULL;
		lpc->log_func = NULL;
		lpc->log_mask = 1 << LPC_LOG_ERROR;

		lpc->lib_emsg = NULL;

		lpc->hex_list = NULL;

		lpc->tgt_chip = NULL;
		lpc->tgt_freq = 0;
		lpc->tgt_baud = 9600;
		lpc->tgt_fast = 0;
		lpc->tgt_user = NULL;
		lpc->tgt_pwrf = NULL;

		lpc->ser_port = -1;
		lpc->ser_baud = 9600;
		lpc->ser_lock = NULL;
		lpc->ser_resc = -1;
		lpc->ser_ispc = -1;
		lpc->ser_tlog.len = 0;
		lpc->ser_rlog.len = 0;

		gettimeofday( &lpc->log_time, NULL );
		LPC_FileType( lpc, &_lpc_srec );
		LPC_FileType( lpc, &_lpc_ihex );
	}

	return( lpc );
};

/**
*	Closes the library.
*
*	The function closes down the library. All open files are closed,
*	all allocated resources, including the <tt>lpc</tt> object itself
*	is freed.
*
*	@param	lpc		The state descriptor
*/

void	LPC_Close( LPC lpc )
{
	// If we hold a lock on the serial port, release it

	if ( lpc->ser_lock ) {

		if ( ! *lpc->ser_lock )

			flock( lpc->ser_port, LOCK_UN );
		else
			unlink( lpc->ser_lock );

		free( lpc->ser_lock );
	}

	// Close the logfile, if it's open. If the logfile is the stdout,
	// then we don't close it

	if ( lpc->log_file && lpc->log_file != stdout ) fclose( lpc->log_file );

	// Close the serial port, if it's open. We also restore its previous
	// attributes, because that's the nice thing to do.

	if ( lpc->ser_port ) {

		tcsetattr( lpc->ser_port, TCSAFLUSH, &lpc->ser_attr );
		close( lpc->ser_port );
	}

	// Release all descriptors.

	free( lpc );
}

/** @} */

