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

#ifndef _LPC_INTERNALS_H
#define _LPC_INTERNALS_H

#include <stdio.h>
#include <termios.h>
#include <sys/time.h>
#include LPC_HEADER

#define	BLEN	128

typedef struct {

	int		len;
	char	buf[ BLEN ];

} LOGBUF;

/*
*	This structure holds the entire state of the library.
*	It is allocated and initialised when TCL calls the
*	library init function and then it gets passed to all
*	functions when TCL calls them. No static data is used,
*	apart from const data tables.
*/

struct s_lpc {

	char			*lib_emsg;			// Last error message
	LOGBUF			ser_tlog;			// Worker thread Tx log buffer
	LOGBUF			ser_rlog;			// Worker thread Rx log buffer

	LPC_CHIP		*tgt_chip;			// Target chip type
	int				tgt_freq;			// Target chip CPU clock frequency
	int				tgt_fast;			// Fast comms with the target is OK
	int				tgt_baud;			// Target actual baud rate
	int				(*tgt_user)(LPC_CONTROL,void*); // Target control callback
	void			(*tgt_pwrf)(int,void*);			// Target power callback
	void			*tgt_parm;			// Target control callback parameter
	void			*tgt_pwru;			// Target power callback parameter
	int				ser_port;			// Serial port, <0 if closed
	int				ser_baud;			// Baud rate
	char			*ser_lock;			// Lock file location
	int				ser_resc;			// Reset control signal
	int				ser_ispc;			// ISP control signal
	struct termios	ser_attr;			// Initial terminal attributes
	char			ser_sbuf[ BLEN ];	// Serial comms buffer

	FILE			*log_file;			// The logfile
	int				log_mask;			// What to log
	void			(*log_func)( void *, const char *);	// Log function
	void			*log_parm;			// Log parameter
	struct timeval	log_time;			// Timestamp of the last log

	LPC_IO_FUNC		*hex_list;			// List of know hexfile formats
};

/*
*	Internally used common functions
*/

void	_lpc_Log( LPC lcp, unsigned type, const char *fmt, ... );
void	_lpc_Error( LPC lcp, const char *fmt, ... );

int		_lpc_SerialPuts( LPC lpc, const char *str );
int		_lpc_SerialBaud( int baud );

int		_lpc_UuEncode( char *buff, char *data, int dlen );
int		_lpc_UuDecode( char *data, char *buff, int blen );

extern const LPC_CHIP *chips[];

extern const LPC_IO_FUNC	_lpc_srec;
extern const LPC_IO_FUNC	_lpc_ihex;

#endif

