/*------------------------------------------------------------------------*/
/* 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: This module contains handling of data sets (without I&M). */
/*                                                                        */
/*------------------------------------------------------------------------*/
/* 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 _DS_H
#define _DS_H

#include <pniousrd.h>

#define DS_NUM_REC_INP_DATA_OBJ_ELEM  0x8028
#define DS_NUM_REC_OUTP_DATA_OBJ_ELEM 0x8029

/*  structure of IEC dataset header for dataset ReadInputDataObjectElement 0x8028 */
typedef struct
{
	PNIO_UINT16 dsBlockType;
	PNIO_UINT16 dsBlockLength;
	PNIO_UINT8  dsBlockVersionHi;
	PNIO_UINT8  dsBlockVersionLo;
	PNIO_UINT8  lengthIocs;
	PNIO_UINT8  iocs;
	PNIO_UINT8  lengthIops;
	PNIO_UINT8  iops;
	PNIO_UINT16 lengthData;
	PNIO_UINT8*  pData;
} IEC_DS_READ_INP_DATA_OBJ_ELEM;

/*  structure of IEC dataset header for dataset ReadOutputDataObjectElement 0x8029 */
typedef struct
{
	PNIO_UINT16 dsBlockType;
	PNIO_UINT16 dsBlockLength;
	PNIO_UINT8  dsBlockVersionHi;
	PNIO_UINT8  dsBlockVersionLo;
	PNIO_UINT16 substActiveFlag;       /*  0x0000 */
	PNIO_UINT8  lengthIocs;
	PNIO_UINT8  lengthIops;
	PNIO_UINT16 lengthData;
	PNIO_UINT8  iocs;
	PNIO_UINT8*  pData;
	PNIO_UINT8  iops;

	PNIO_UINT16 dsBlockTypeSubst;      /*  0x0014 */
	PNIO_UINT16 dsBlockLengthSubst;    /*  7 */
	PNIO_UINT8  dsBlockVersionHiSubst; /*  1 */
	PNIO_UINT8  dsBlockVersionLoSubst; /*  0 */
	PNIO_UINT16 substMode;             /*  always 0 here */
	PNIO_UINT8 substIocs;              /*  same as iocs */
	PNIO_UINT8*  pSubstData;
	PNIO_UINT8 substDataValid;         /* always bad (0x40) */
} IEC_DS_READ_OUTP_DATA_OBJ_ELEM;

void DsInit_InputDataObject(IEC_DS_READ_INP_DATA_OBJ_ELEM* pContent, PNIO_DEV_ADDR* pAddr);
void DsInit_OutputDataObject(IEC_DS_READ_OUTP_DATA_OBJ_ELEM* pContent, PNIO_DEV_ADDR* pAddr);
void DsFill_InputDataObject(IEC_DS_READ_INP_DATA_OBJ_ELEM* pContent, PNIO_UINT32* pBufLen, PNIO_UINT8* pBuf, PNIO_ERR_STAT* pPnioState);
void DsFill_OutputDataObject(IEC_DS_READ_OUTP_DATA_OBJ_ELEM* pContent, PNIO_UINT32* pBufLen, PNIO_UINT8* pBuf, PNIO_ERR_STAT* pPnioState);


#endif /* _DS_H */
