/*
 * etne20.h
 *
 * Copyright (c) 1997-2008 ETEL SA. All Rights Reserved.
 *
 * This software is the confidential and proprietary informatione of ETEL SA
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the
 * license agreement you entered into with ETEL.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ETEL AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL ETEL OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF ETEL HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 *
 */

/**
 * This header file contains public declaration of etne library.\n
 * This library allows enabling access to already connected hardware for other process
 * This library in conformed to POSIX 1003.1c
 * @file etne20.h
 */


#ifndef _ETNE20_H
#define _ETNE20_H


/*** libraries ***/

#ifdef __cplusplus
#include <typeinfo.h>
#endif


/*** litterals ***/

#ifdef __WIN32__		/* defined by Borland C++ Builder */
#ifndef WIN32
#define WIN32
#endif
#endif

#ifdef __cplusplus
#ifdef ETEL_OO_API		/* defined by the user when he need the Object Oriented interface */
#define ETNE_OO_API
#endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @defgroup ETNEAll ETNE All functions
 */
/*@{*/
/*@}*/

/**
 * @defgroup ETNEErrorsAndWarnings ETNE Errors and warnings
 */
/*@{*/
/*@}*/

/**
 * @defgroup ETNEVersion ETNE Library version access functions
 */
/*@{*/
/*@}*/

/**
 * @defgroup ETNELibrary ETNE Connection enabling functions
 */
/*@{*/
/*@}*/

#ifndef __BYTE_ORDER
#if defined WIN32 || defined QNX4
#define __LITTLE_ENDIAN 1234
#define __BIG_ENDIAN 4321
#define __BYTE_ORDER __LITTLE_ENDIAN	/* define byte order for INTEL processor */
#endif /*WIN32 || QNX4*/

#ifdef POSIX

#ifdef SOLARIS
#define __LITTLE_ENDIAN 1234
#define __BIG_ENDIAN 4321
#ifdef _BIG_ENDIAN
#define __BYTE_ORDER __BIG_ENDIAN		/* define byte order for SPARC processor */
#else
#define __BYTE_ORDER __LITTLE_ENDIAN		/* define byte order for SPARC processor */
#endif
#endif /*SOLARIS*/

#ifdef LINUX
#include <endian.h>
#endif /*LINUX*/

#ifdef QNX6
#define __LITTLE_ENDIAN 1234
#define __BIG_ENDIAN 4321
#ifdef __BIGENDIAN__
#define __BYTE_ORDER __BIG_ENDIAN		/* define byte order for SPARC processor */
#else
#define __BYTE_ORDER __LITTLE_ENDIAN		/* define byte order for SPARC processor */
#endif
#endif /*QNX6*/

#endif /*POSIX*/
#endif /*BYTE_ORDER*/


/*
 * error codes - c
 */
#ifndef ETNE_OO_API
#define ETNE_EPORTMAX                     -704        /**< maximal number of user port exceeded */
#define ETNE_EPRESENT                     -703        /**< an etne or a custom application is already running for this url */
#define ETNE_EINTERNAL                    -702        /**< some internal error in the etel software */
#define ETNE_ESYSTEM                      -701        /**< some system resource return an error */
#define ETNE_EBADPARAM                    -700        /**< one of the parameter is not valid */

#endif /* ETNE_OO_API */

/*** types ***/

/*
 * type modifiers
 */
#ifdef WIN32
#define _ETNE_EXPORT __cdecl                         /* function exported by static library */
#endif /* WIN32 */

#ifdef QNX4
#define _ETNE_EXPORT __cdecl                          /* function exported by library */
#endif /* QNX4 */

#ifdef POSIX
#define _ETNE_EXPORT									/* function exported by library */
#endif /* POSIX */

/*
 * hidden structure for library clients
 */
#ifndef ETB
#define ETB void
struct EtbBus { ETB *etb; };
#endif

#ifndef STA
#define STA void
#endif

/*** prototypes ***/
/**
 * @addtogroup ETNEAll
 * @{
 */
char_cp _ETNE_EXPORT etne_translate_error (int code);
void _ETNE_EXPORT etne_connection_clear(int sta_port_idx);
void _ETNE_EXPORT etne_connection_destroy(int sta_port_idx);
int _ETNE_EXPORT etne_connection_init(int sta_port_idx,
                                      ETB *bus,
									  char *name,
									  char *url,
									  int flags,
									  char *baudrate,
									  char *accept_driver_name,
									  int accept_driver_port,
									  int port_properties_driver_port,
									  STA *sta);
int _ETNE_EXPORT etne_accept_destroy(int port_idx);
int _ETNE_EXPORT etne_accept_init(int port_idx, char *accept_driver_name, int accept_driver_port);
int _ETNE_EXPORT etne_start_custom(ETB *bus, int accept_driver_port, char *name);
int _ETNE_EXPORT etne_stop_custom(ETB *bus, int accept_port);
int _ETNE_EXPORT etne_connection_spy(int sta_port_idx);
dword	_ETNE_EXPORT etne_get_version();
dword	_ETNE_EXPORT etne_get_edi_version();
time_t	_ETNE_EXPORT etne_get_build_time();
int _ETNE_EXPORT etne_wait_event(int port_idx, int timeout);

/*@}*/


#ifdef __cplusplus
} /* extern "C" */
#endif


/*
 * ETNE constants - c++
 */
#ifdef ETNE_OO_API

/*
 * generate exceptions from error codes
 */
#define ERRCHK(a) do { int _err = (a); if(_err) throw EtneException(_err); } while(0)

/*
 * ETNE exception - c++
 */
class EtneException {

	/* error codes - c++ */
public:
    enum {EBADPARAM = -700 };                       /* one of the parameter is not valid */
    enum {EINTERNAL = -702 };                       /* some internal error in the etel software */
    enum {EPORTMAX = -704 };                        /* maximal number of user port exceeded */
    enum {EPRESENT = -703 };                        /* an etne or a custom application is already running for this url */
    enum {ESYSTEM = -701 };                         /* some system resource return an error */


	/* exception code */
private:
    int code;

	/* constructor */
protected:
    EtneException(int e) { code = e; };

	/* translate error code */
public:
    static char_cp translate(int code) {
        return etne_translate_error(code);
    }

    /* get error description */
public:
    int getCode() {
		return code;
	}
    const char *getText() {
		return translate(code);
	}
};


/**
 * Etne class - C++
 */
class Etne {
	/* constructors */
public:
	Etne(void) {
	}
public:
	/* functions */
    void startCustom(EtbBus etb, int accept_port, char *name) {
        ERRCHK(etne_start_custom(etb, accept_port, name));
	}
    void stopCustom(EtbBus etb, int accept_port) {
        ERRCHK(etne_stop_custom(etb, accept_port));
	}
    static char_cp translateError(int code) {
        return etne_translate_error(code);
    }
	static dword getVersion() {
		return etne_get_version();
	}
	static dword getEdiVersion() {
		return etne_get_edi_version();
	}
	static time_t getBuildTime() {
		return etne_get_build_time();
	}
};

#undef ERRCHK
#endif /* ETNE_OO_API */


#endif /* _ETNE20_H */

