/*------------------------------------------------------------------------*/
/* Copyright (C) SIEMENS CORP., 2018 All rights reserved.*/
/* All rights reserved.                                                   */
/* Redistribution and use in source and binary forms, with or without     */
/* modification, are permitted provided that the following conditions     */
/* are met:                                                               */
/* 1. Redistributions of source code must retain the above copyright      */
/* notice, this list of conditions and the following disclaimer.          */
/* 2. 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.   */
/* 3. Neither the name of the copyright holder 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS    */
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      */
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS      */
/* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE         */
/* COPYRIGHT HOLDER 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.                                            */
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
/* Description: Main module: Device init, deinit and callbacks.           */
/*                                                                        */
/*------------------------------------------------------------------------*/
/* Attention : Callbacks are running concurrent in other threads so all   */
/*             printf statements should be synchronized. But this sample  */
/*             application doesn't synchronize for simplicity  !          */
/*------------------------------------------------------------------------*/

#ifndef _DEV_CERTIFY_ENERGY_H
#define _DEV_CERTIFY_ENERGY_H

#include "dev_cfg.h"
#include "pniousrd.h"

#define PROFI_ENERGY_ENABLE /*  enable PROFIENERGY extensions */
#define PE_PRINTF printf
/* #define PE_PRINTF */


/******************************************************************************/
/* SWAPPING of 16 and 32 Bit values depending of endianess of the Host-system */
/* endian.                                                                    */
/******************************************************************************/
#define SWAP_16(var)                  \
	(  ((( (var)) & 0xFF00L) >>  8)   \
	+ ((( (var)) & 0x00FFL) << 8))

#define SWAP_32(var)                     \
	(  ((( (var)) & 0xFF000000L) >> 24)  \
	+ ((( (var)) & 0x00FF0000L) >>  8)   \
	+ ((( (var)) & 0x0000FF00L) <<  8)   \
	+ ((( (var)) & 0x000000FFL) << 24))


#ifdef _DPR_LINUX
#if __BYTE_ORDER == __BIG_ENDIAN
#define NO_SWAP
#else
#define SWAP
#endif
#else
#define SWAP
#endif


#ifdef NOSWAP /* Linux Big Eandian */
#define CPU_TO_LE16(c) SWAP_16(c)
#define LE_TO_CPU16(c) SWAP_16(c)
#define CPU_TO_LE(c) SWAP_32(c)
#define LE_TO_CPU(c) SWAP_32(c)

#define CPU_TO_BE16(c) (c)
#define BE_TO_CPU16(c) (c)
#define CPU_TO_BE(c) (c)
#define BE_TO_CPU(c) (c)
#define SWAP_FLOAT(a,b) 	b = a

#else /* Linux Little Endian or Windows */
#define CPU_TO_LE16(c) (c)
#define LE_TO_CPU16(c) (c)
#define CPU_TO_LE(c) (c)
#define LE_TO_CPU(c) (c)

#define CPU_TO_BE16(c) SWAP_16(c)
#define BE_TO_CPU16(c) SWAP_16(c)
#define CPU_TO_BE(c) SWAP_32(c)
#define BE_TO_CPU(c) SWAP_32(c)
#define SWAP_FLOAT(b,a) 		{	PNIO_UINT32 dw_Help = BE_TO_CPU(*((PNIO_UINT32*)&a)); \
	b = (float)*((float*)&dw_Help); \
							}

#endif


/****************************************************/
/* Known Diagnostic alarms for this device          */
/****************************************************/

#define CH_ERR_INVAL_LINKUP     0x0100
#define CH_ERR_INVAL_LINKDOWN   0x0101
#define CH_ERR_NO_REDUND_PS     0x0200
#define CH_ERR_NO_CPLUG         0x0201
#define CH_ERR_CPLUG_ERROR      0x0202

/*****************************************/
/* help data for modules and sub modules */
/*****************************************/
typedef struct
{
	PNIO_DEV_ADDR Addr;
	PNIO_UINT32 modId;
	PNIO_UINT32 subslotId;
	PNIO_UINT32 api;
	PNIO_UINT32 InDatLength;
	PNIO_UINT32 OutDatLength;
	PNIO_UINT8* OutData;
	PNIO_IOXS OutDatIocs;
	PNIO_IOXS OutDatIops;
	PNIO_UINT8* InData;
	PNIO_IOXS InDatIops;
	PNIO_IOXS InDatIocs;
} device_data_t;

typedef struct
{
	unsigned short VendorId;
	int nrOfSubmodules;
	device_data_t* pDeviceData;
} device_data_set_t;

extern device_data_set_t g_device_data;

int addrToId(PNIO_DEV_ADDR addr);
int InitCard(PNIOD_CBF_FUNCTIONS* pCallbacks);
int UnInitCard();
PNIO_UINT32 Trigger_data_read(PNIO_DEV_ADDR*     pAddr);
PNIO_UINT32 Trigger_data_write(PNIO_DEV_ADDR*     pAddr);


#endif /* _DEV_CERTIFY_ENERGY_H */