/*
 * This file is part of libtqtvd_astronav
 *
 * Copyright (C) 2008-2017 TQTVD Software
 * Copyright (C) 2017 Mirakulo Software (mirakulo@mirakulo.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */
#ifndef ATV_GENERAL_H_
#define ATV_GENERAL_H_

#include <stdio.h>
#include <unistd.h>         /* POSIX Standard: 2.10 Symbolic Constants */
#include <sys/types.h>      /* POSIX Standard: 2.6 Primitive System Data Types */
#include <sys/stat.h>
//#include <bits/types.h>   /* The machine-dependent typesizes */
#include <sys/mman.h>       /* Memnory management */
#include <fcntl.h>
//#define _REENTRANT        /* thread-safe libraries */
#include <pthread.h>
#include <signal.h>

#include <libgen.h>         /* basename() */

#include <time.h>
#include <stdio.h>

/**
 *  This tells a C++ compiler that a function has C linkage.
 *  @see http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html
 *
 */
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C
#endif

/**
 * Specific platform's declarations
 */
EXTERN_C time_t timegm(struct tm* tm);

// #undef atvLog_report
// #define atvLog_report(X, P, args...) _mtk_dbg_stmt (args)
#define fflush_unlocked(x)  fflush(x)
// The following items are defined as enum by atvLog.h,
// but this platform avoids atvlog.h inclusion by defining macro ATV_GENERAL_H_ directly in the toolchain file (argh!!!!),
// this "trick" is used to force the usage of function _mtk_dbg_stmt to print logs in the console,
// therefore, as atvLog.h is not included, this values neeed to be re-defined elsewhere (like here...)
// REMARK: This "trick" is not really necessary, it would suffice if the platform implements the atvLog API,
//			or just uses the reference implementation...
// #define	ATV_LOG_EMERG 0  //!< ATV_LOG_EMERG
// #define	ATV_LOG_USER  0  //!< ATV_LOG_USER

//#define TQTVD_NO_64BIT_SUPPORT

typedef unsigned char       _u8;
typedef unsigned short      _u16;
typedef int                 _i32;
typedef unsigned long       _u32;
typedef char             	_s8;
typedef short               _s16;
typedef long                _s32;
typedef unsigned char       _ubyte;
typedef char                _sbyte;
typedef time_t              _tqtime;
typedef _u8                 _bool;
typedef float               _float;

#ifdef TQTVD_NO_64BIT_SUPPORT
	typedef _u32                _u64;
	typedef _s32                _s64;
	#define TQ_U64_LL l
	#define TQ_U64_LL_F "l"
#else
	typedef unsigned long long  _u64;
	typedef long long           _s64;
	#define TQ_U64_LL ll
	#define TQ_U64_LL_F "ll"
#endif

#ifndef MASK_BIT
#define MASK_BIT(b) (1 << (b))
#endif

typedef _u32                _handle;
#define INVALID_HANDLE      0xFFFFFFFF

/**
 *  @brief handle type for Demux instances
 */
typedef _handle atvDemuxHandle;

/**
 *  @brief handle type for Section Filter instances
 */
typedef _handle atvSectionFilterHandle;

/**
 *  @brief  handle type for Audio Output instances
 */
typedef _handle atvAudioOutputHandle;

/*
 * @brief handle type for Screen instances
 */
typedef _handle atvScreenHandle;

/**
 *  @brief  Network Interface handle
 */
typedef _handle atvNetworkInterfaceHandle;

/*
 * @brief handle type for File instances
 */
typedef _handle atvFileHandle;

/*
 * @brief handle type for Stream instances
 */
typedef _handle atvStreamHandle;

/*
 * @brief handle type for FileHandler instances
 */
typedef _handle atvFileStreamHandle;

/*
 *  * @brief handle type for Screen instances
 *
 */
typedef _handle atvMediaPlayerHandle;

/*
 *  * @brief handle type for Return Channel
 *
 */
typedef _handle atvReturnChannelHandle;


/**
 *  @brief handle type for Devices (PDA, CellPhones, etc)
 */
typedef _handle atvDeviceHandle;

/**
 * @brief handle type for Dsmcc
 */
typedef _handle atvDsmccHandle;

/**
 *
 *  @brief  Connection types
 *
 */
typedef enum e_atvConnectionType {
    atvConnectionType_DIALUP=1 /**< */
    , atvConnectionType_SOCKET /**< */
    , atvConnectionType_INETADDR /**< */
    , atvConnectionType_UNKNOWN /**< */
} atvConnectionType;

/**
 *
 *  @brief  necessary data to establish a dialup connection
 *
 */
typedef struct s_atvConnDialup {
    /*@{*/
    _sbyte* number; /**< */
    _sbyte* username; /**< */
    _sbyte* password; /**< */
    /*@}*/
} atvConnDialup;

/**
 *
 *  @brief  necessary data to establish a socket connection
 *
 */
typedef struct s_atvConnSocket {
    /*@{*/
    _i32 fdHandle; /**< */
    /*@}*/
} atvConnSocket;

/**
 *
 *  @brief  necessary data to establish an internet connection
 *
 */
typedef struct s_atvConnInet {
    /*@{*/
    _sbyte* host; /**< */
    /*@}*/
} atvConnInet;

/**
 *
 *  @brief  contains data necessary to establish a dial up, or a socket, or a
 *          internet connection
 *
 */
typedef struct s_atvConnectionParameters {
    /** identifies if the data ahead about a dial up, or a socket, or a internet connection. */
    /*@{*/
    atvConnectionType type;

    union u_atvparams {
        /** Dial-Up data */
        atvConnDialup  connDialup;

        /** Socket data */
        atvConnSocket connSocket;

        /** Internet data */
        atvConnInet connInet;
    } params;
    /*@}*/
} atvConnectionParameters;

/**
 *  @brief handles for Graphic Manager
 */
typedef _handle atvDrawContextHandle;
typedef _handle atvSurfaceHandle;
typedef _handle atvLayerHandle;
typedef _handle atvWindowHandle;
typedef _handle atvBitmapHandle;
typedef _handle atvFontHandle;
typedef _handle atvGraphicCapabilitiesHandle;

/**
 *
 *  @brief  handle type for Input Manager instances
 *
 */
typedef _handle atvInputHandle;

/**
 *
 *  @brief  handle type for connection devices instances.
 *
 */
typedef _handle atvConnectionDeviceHandle;

#define atvJvm_init _atvJvm_init
#define atvJvm_terminate _atvJvm_terminate

/**
 * 
 * @ brief handles for OS abstraction/synchronization
 * 
 */
typedef _handle atvTaskHandle;
typedef _handle atvMonitorHandle;
/**
 * Time interval measurement in milliseconds
 */
#define TQ_PREPARE_TIME_MEASURE(I)					timespec _tsb##I={0,0}, _tse##I={0,0}
#define TQ_BEGIN_TIME_MEASURE(I)					clock_gettime(CLOCK_MONOTONIC, &_tsb##I)
#define TQ_END_TIME_MEASURE(I)						clock_gettime(CLOCK_MONOTONIC, &_tse##I)
#define TQ_GET_TIME_MEASURE(I)						((float)((_tse##I.tv_sec*1e3 + _tse##I.tv_nsec/1e6)-(_tsb##I.tv_sec*1e3 + _tsb##I.tv_nsec/1e6)))

#endif /* ATV_GENERAL_H_ */
