/*
 ******************************************************************************
 *  Copyright 1994 DeltaLink bv. All Rights Reserved.
 ******************************************************************************
 *
 * DeltaLink Mailbox Communication Interface
 *
 * File         :   format.h
 *
 * Version      :   2.00
 *
 * Developed by :   Leon Westervoort.
 *
 *                  DeltaLink b.v.
 *                  Saffierborch 6c
 *                  5241LN Rosmalen
 *                  The Netherlands.
 *                  Tel. (31) 4192-20025
 *                  Fax. (31) 4192-15251
 *
 * This file contains data- and prototypes of the DeltaLink Mailbox
 * Communication Interface.
 *
 */
#ifndef _FORMAT_DEFS_INCLUDED /* { */
#define _FORMAT_DEFS_INCLUDED 1

/*
 * define all CDE codes
 */
#define CDE_B0_15               0x0100
#define CDE_B16_31              0x0110
#define CDE_NIBBLE              0x0300
#define CDE_BYTE                0x0400
#define CDE_UBYTE               0x0500
#define CDE_SIGNED              0x0600
#define CDE_UNSIGNED            0x0700
#define CDE_UBCD                0x0780

#define CDE_LONG                0x0800
#define CDE_ULONG               0x0900
#define CDE_LBCD                0x0980

#define CDE_SIEMENS_FLT         0x0A00
#define CDE_SINGLE_IEEE_FLT     0x0A80
#define CDE_DOUBLE_IEEE_FLT     0x0B00
#define CDE_BLOCK               0x0C00
#define CDE_STRING              0x0C80

/*#define E_USER_CMD            0x0cff */
/*#define E_RESET               0x0f00 */
/*#define E_DIP                 0x0f01 */
/*#define E_PEAK                0x0f02 */
/*#define E_FILTER              0x0f03 */
/*#define E_TOTALIZER           0x0f04 */
/*#define E_AVERAGE             0x0f05 */
/*#define E_STANDARD_DEV        0x0f06 */
/*#define E_MSG                 0x0f10 */

/* define the data manipulation routines for coneversions based on direction of data */

#ifdef HOST_LOW_2_HIGH                   /* data direction of this host is LOW to HIGH */

/*
 * operation on the MCI header which resides before the data. The header is always in the
 * format LOW to HIGH 
 */
#define Mci_Get_Version( m)           ( ((MCIHDR *)(m)->buffer)->i.version)
#define Mci_Set_Version( m, v)        ( ((MCIHDR *)(m)->buffer)->i.version = (v))

#define Mci_Get_Handling( m)          ( ((MCIHDR *)(m)->buffer)->i.handling)
#define Mci_Set_Handling( m, v)       ( ((MCIHDR *)(m)->buffer)->i.handling = (v))

#define Mci_Set_Bound_Dir( m, v)      ( (( MCIHDR *)((m)->buffer))->i.bound_dir = (v))
#define Mci_Get_Bound_Dir( m)         ( (( MCIHDR *)((m)->buffer))->i.bound_dir)

#define Mci_Set_Bit_Dir( m, v)        ( (( MCIHDR *)((m)->buffer))->i.bit_dir = (v))
#define Mci_Get_Bit_Dir( m)           ( (( MCIHDR *)((m)->buffer))->i.bit_dir)

#define Mci_Set_Bound_Start( m, v)    ( (( MCIHDR *)((m)->buffer))->i.bound_start = (v))
#define Mci_Get_Bound_Start( m)       ( (( MCIHDR *)((m)->buffer))->i.bound_start)

#define Mci_Set_Bit_Start( m, v)      ( (( MCIHDR *)((m)->buffer))->i.bit_start = (v))
#define Mci_Get_Bit_Start( m)         ( (( MCIHDR *)((m)->buffer))->i.bit_start)

#define Mci_Set_Boundary( m, v)       ( (( MCIHDR *)((m)->buffer))->i.boundary = (v))
#define Mci_Get_Boundary( m)          ( (( MCIHDR *)((m)->buffer))->i.boundary)

#define Mci_Get_Start( m)             ( ((MCIHDR *)(m)->buffer)->start )
#define Mci_Set_Start( m, v)          ( ((MCIHDR *)(m)->buffer)->start = (v))

#define Mci_Get_Len( m)               ( ((MCIHDR *)(m)->buffer)->len )
#define Mci_Set_Len( m, v)            ( ((MCIHDR *)(m)->buffer)->len = (v))

#define Mci_Get_CDE( m)               ( ((MCIHDR *)(m)->buffer)->cde )
#define Mci_Set_CDE( m, v)            ( ((MCIHDR *)(m)->buffer)->cde = (v))
#define Mci_Get_Len( m)               ( ((MCIHDR *)(m)->buffer)->len )
#define Mci_Set_Len( m, v)            ( ((MCIHDR *)(m)->buffer)->len = (v))

#define Mci_Get_CDE( m)               ( ((MCIHDR *)(m)->buffer)->cde )
#define Mci_Set_CDE( m, v)            ( ((MCIHDR *)(m)->buffer)->cde = (v))

/*
 * define the functions to manipulate data depending on the Host format 
 */

#define Mci_Get_Byte_Bit_H2L( b, n)     (*( uchar *)(b) & ( 1 << (n)))
#define Mci_Set_Byte_Bit_H2L( b, n, v)  (v) == 0 ? (*( uchar *)(b) &= ~( 1 << (n))) \
                                                 : (*( uchar *)(b) |= ( 1 << (n)))

#define Mci_Get_Byte_Bit_L2H( b, n)     (*( uchar *)(b) & ( 0x80 >> (n)))
#define Mci_Set_Byte_Bit_L2H( b, n, v)  (v) == 0 ? (*( uchar *)(b) &= ~( 0x80 >> (n))) \
                                                 : (*( uchar *)(b) |= ( 0x80 >> (n)))

#define Mci_Get_Word_Bit_H2L( w, n)     (*( u16 *)(w) & ( 1 << (n)))
#define Mci_Set_Word_Bit_H2L( w, n, v)  (v) == 0 ? (*( u16 *)(w) &= ~( 1 << (n))) \
                                                 : (*( u16 *)(w) |= ( 1 << (n)))

#define Mci_Get_Word_Bit_L2H( w, n)     (*( u16 *)(w) & ( 0x8000 >> (n)))
#define Mci_Set_Word_Bit_L2H( w, n, v)  (v) == 0 ? (*( u16 *)(w) &= ~( 0x8000 >> (n))) \
                                                 : (*( u16 *)(w) |= ( 0x8000 >> (n)))

#define Mci_Get_Long_Bit_H2L( l, n)     (*( u32 *)(l) & ( 1 << (n)))
#define Mci_Set_Long_Bit_H2L( l, n, v)  (v) == 0 ? (*( u32 *)(l) &= ~( 1 << (n))) \
                                                 : (*( u32 *)(l) |= ( 1 << (n)))

#define Mci_Get_Long_Bit_L2H( l, n)     (*( u32 *)(l) & ( 0x80000000 >> (n)))
#define Mci_Set_Long_Bit_L2H( l, n, v)  (v) == 0 ? (*( u32 *)(l) &= ~( 0x80000000 >> (n))) \
                                                 :  (*( u32 *)(l) |= ( 0x80000000 >> (n)))

#define Mci_Get_Byte_Left_H2L( w)       (*( u16 *)(w) & 0x00ff)
#define Mci_Set_Byte_Left_H2L( w, v)    { (*( u16 *)(w) &= 0xff00); (*( u16 *)(w) |= (v) & 0x00ff);}

#define Mci_Get_Byte_Left_L2H( w)       ( ( *( u16 *)(w) >> 8) & 0x00ff)
#define Mci_Set_Byte_Left_L2H( w, v)	{ ( *( u16 *)(w) &= 0x00ff); (*( u16 *)(w) |= ((v) << 8) & 0xff00);}

#define Mci_Get_Byte_Right_H2L( w)      ( (*( u16 *)(w) >> 8) & 0x00ff)
#define Mci_Set_Byte_Right_H2L( w, v)   { (*( u16 *)(w) &= 0x00ff); (*( u16 *)(w) |= ( (v)<< 8) & 0xff00);}

#define Mci_Get_Byte_Right_L2H( w)      (*( u16 *)(w) & 0x00ff)
#define Mci_Set_Byte_Right_L2H( w, v)   { (*( u16 *)(w) &= 0xff00); (*( u16 *)(w) |= (v) & 0x00ff);}

#define Mci_Get_Word_H2L( w)            ( ( (*( u16 *)(w) >> 8) & 0x00ff) | \
                                          ( (*( u16 *)(w) << 8) & 0xff00) \
																				)
#define Mci_Set_Word_H2L( w, v)         ( *( u16 *)(w) = ((( u16 )(v) >> 8) & 0x00ff) | \
                                                         ((( u16 )(v) << 8) & 0xff00))

#define Mci_Get_Word_L2H( w)            (*( u16 *)(w))
#define Mci_Set_Word_L2H( w, v)         (*( u16 *)(w) = ( u16)(v))

#define Mci_Get_Long_H2L( l)      ( u32)(( ( (*( u32 *)(l)) >> 24) & 0x000000ff) | \
                                         ( ( (*( u32 *)(l)) >> 8)  & 0x0000ff00) | \
                                         ( ( (*( u32 *)(l)) << 8 ) & 0x00ff0000) | \
                                         ( ( (*( u32 *)(l)) << 24) & 0xff000000)   \
                                        )

#define Mci_Set_Long_H2L( l,  v)        *( u32 *)(l) = (( (( u32)(v)) >> 24) & 0x000000ff) | \
                                                       (( (( u32)(v)) >> 8)  & 0x0000ff00) | \
                                                       (( (( u32)(v)) << 8 ) & 0x00ff0000) | \
                                                       (( (( u32)(v)) << 24) & 0xff000000)

#define Mci_Get_Long_L2H( l)            (*( u32 *)(l))
#define Mci_Get_Long_L2H( l)            (*( u32 *)(l))
#define Mci_Set_Long_L2H( l, v)         (*( u32 *)(l) = ( u32)(v))

/*
 * special inverted long functionality for Modicon
 */
#define Mci_Get_RLong_H2L( l)     ( u32)(( ( (*( u32 *)(l)) >> 8) & 0x000000ff) | \
                                         ( ( (*( u32 *)(l)) << 8) & 0x0000ff00) | \
                                         ( ( (*( u32 *)(l)) >> 8) & 0x00ff0000) | \
                                         ( ( (*( u32 *)(l)) << 8) & 0xff000000)   \
                                        )

#define Mci_Set_RLong_H2L( l,  v)       *( u32 *)(l) = (( (( u32)(v)) >> 8) & 0x000000ff) | \
                                                       (( (( u32)(v)) << 8) & 0x0000ff00) | \
                                                       (( (( u32)(v)) >> 8) & 0x00ff0000) | \
                                                       (( (( u32)(v)) << 8) & 0xff000000)

#define Mci_Get_RLong_L2H( l)     ( u32)(( ( (*( u32 *)(l)) >> 16) & 0x000000ff) | \
                                         ( ( (*( u32 *)(l)) >> 24) & 0x0000ff00) | \
                                         ( ( (*( u32 *)(l)) << 16) & 0x00ff0000) | \
                                         ( ( (*( u32 *)(l)) << 24) & 0xff000000)   \
                                        )


#define Mci_Set_RLong_L2H( l, v)  *( u32 *)(l) = (( (( u32)(v)) >> 16) & 0x000000ff) | \
                                                 (( (( u32)(v)) >> 24) & 0x0000ff00) | \
                                                 (( (( u32)(v)) << 16) & 0x00ff0000) | \
                                                 (( (( u32)(v)) << 24) & 0xff000000)

/*
 * -----------------------------------------------
 */

#define Mci_Get_Double_H2L( d, s)        reverse( (char *)(d), (char *)(s), 8)
#define Mci_Set_Double_H2L( d, s)        reverse( (char *)(d), (char *)(s), 8)

#define Mci_Get_Double_L2H( d, s)        ( *( double *)(d) = *( double *)(s))
#define Mci_Set_Double_L2H( d, s)        ( *( double *)(d) = *( double *)(s))


#else                           /* data direction of this host is HIGH to LOW */

/*
 * operation on the MCI header which resides before the data. The header is always in the
 * format LOW to HIGH 
 */
#define Mci_Get_Version( m)           ( ((MCIHDR *)(m)->buffer)->i.version)
#define Mci_Set_Version( m, v)        ( ((MCIHDR *)(m)->buffer)->i.version = (v))

#define Mci_Get_Handling( m)          ( ((MCIHDR *)(m)->buffer)->i.handling)
#define Mci_Set_Handling( m, v)       ( ((MCIHDR *)(m)->buffer)->i.handling = (v))

#define Mci_Set_Bound_Dir( m, v)      ( (( MCIHDR *)((m)->buffer))->i.bound_dir = (v))
#define Mci_Get_Bound_Dir( m)         ( (( MCIHDR *)((m)->buffer))->i.bound_dir)

#define Mci_Set_Bit_Dir( m, v)        ( (( MCIHDR *)((m)->buffer))->i.bit_dir = (v))
#define Mci_Get_Bit_Dir( m)           ( (( MCIHDR *)((m)->buffer))->i.bit_dir)

#define Mci_Set_Bound_Start( m, v)    ( (( MCIHDR *)((m)->buffer))->i.bound_start = (v))
#define Mci_Get_Bound_Start( m)       ( (( MCIHDR *)((m)->buffer))->i.bound_start)

#define Mci_Set_Bit_Start( m, v)      ( (( MCIHDR *)((m)->buffer))->i.bit_start = (v))
#define Mci_Get_Bit_Start( m)         ( (( MCIHDR *)((m)->buffer))->i.bit_start)

#define Mci_Set_Boundary( m, v)       ( (( MCIHDR *)((m)->buffer))->i.boundary = (v))
#define Mci_Get_Boundary( m)          ( (( MCIHDR *)((m)->buffer))->i.boundary)

#define Mci_Get_Start( m)             ( ((MCIHDR *)(m)->buffer)->start )
#define Mci_Set_Start( m, v)          ( ((MCIHDR *)(m)->buffer)->start = (v))

/*
#define Mci_Get_Start( m)             ( ((MCIHDR *)(m)->buffer)->start >> 8 | \
                                        ((MCIHDR *)(m)->buffer)->start << 8   \
                                      )
#define Mci_Set_Start( m, v)          ( ((MCIHDR *)(m)->buffer)->start = ((v) >> 8) | ((v) << 8))
*/

#define Mci_Get_Len( m)               ( ((MCIHDR *)(m)->buffer)->len )
#define Mci_Set_Len( m, v)            ( ((MCIHDR *)(m)->buffer)->len = (v))

/*
#define Mci_Get_Len( m)               ( ((MCIHDR *)(m)->buffer)->len >> 8 | \
                                        ((MCIHDR *)(m)->buffer)->len << 8   \
                                      )
#define Mci_Set_Len( m, v)            ( ((MCIHDR *)(m)->buffer)->len = ((v) >> 8) | ((v) << 8))
*/

#define Mci_Get_CDE( m)               ( ((MCIHDR *)(m)->buffer)->cde )
#define Mci_Set_CDE( m, v)            ( ((MCIHDR *)(m)->buffer)->cde = (v))

/*
#define Mci_Get_CDE( m)               ( ((MCIHDR *)(m)->buffer)->cde >> 8 | \
                                        ((MCIHDR *)(m)->buffer)->cde << 8   \
                                      )
#define Mci_Set_CDE( m, v)            ( ((MCIHDR *)(m)->buffer)->cde = ((v) >> 8) | ((v) << 8) )
*/

/*
 * define the functions to manipulate data depending on the Host format 
 */
#define Mci_Get_Byte_Bit_L2H( b, n)         (*( u8 *)(b) & ( 0x80 >> (n)))
#define Mci_Set_Byte_Bit_L2H( b, n, v)      (v) == 0 ? (*( u8 *)(b) &= ~( 0x80 >> (n))) \
                                                     : (*( u8 *)(b) |= ( 0x80 >> (n)))

#define Mci_Get_Byte_Bit_H2L( b, n)         (*( u8 *)(b) & ( 1 << (n)))
#define Mci_Set_Byte_Bit_H2L( b, n, v)      (v) == 0 ? (*( u8 *)(b) &= ~( 1 << (n))) \
                                                     : (*( u8 *)(b) |= ( 1 << (n)))

#define Mci_Get_Word_Bit_L2H( w, n)         (*( u16 *)(w) & ( 0x8000 >> (n)))
#define Mci_Set_Word_Bit_L2H( w, n, v)      (v) == 0 ? (*( u16 *)(w) &= ~( 0x8000 >> (n))) \
                                                     : (*( u16 *)(w) |= ( 0x8000 >> (n)))

#define Mci_Get_Word_Bit_H2L( w, n)         (*( u16 *)(w) & ( 1 << (n)))
#define Mci_Set_Word_Bit_H2L( w, n, v)      (v) == 0 ? (*( u16 *)(w) &= ~( 1 << (n))) \
                                                     : (*( u16 *)(w) |= ( 1 << (n)))

#define Mci_Get_Long_Bit_L2H( l, n)         (*( u32 *)(l) & ( 0x80000000 >> (n)))
#define Mci_Set_Long_Bit_L2H( l, n, v)      (v) == 0 ? (*( u32 *)(l) &= ~( 0x80000000 >> (n))) \
                                                     :  (*( u32 *)(l) |= ( 0x80000000 >> (n)))

#define Mci_Get_Long_Bit_H2L( l, n)         (*( u32 *)(l) & ( 1 << (n)))
#define Mci_Set_Long_Bit_H2L( l, n, v)      (v) == 0 ? (*( u32 *)(l) &= ~( 1 << (n))) \
                                                     : (*( u32 *)(l) |= ( 1 << (n)))

#define Mci_Get_Byte_Left_L2H( w)           (*( u16 *)(w) & 0x00ff)
#define Mci_Set_Byte_Left_L2H( w, v)        { (*( u16 *)(w) &= 0xff00); (*( u16 *)(w) |= ( u16)(v) & 0x00ff);}

#define Mci_Get_Byte_Left_H2L( w)           ( ( *( u16 *)(w) >> 8) & 0x00ff)
#define Mci_Set_Byte_Left_H2L( w, v)        { ( *( u16 *)(w) &= 0x00ff); (*( u16 *)(w) |= ( ( u16)(v) << 8) & 0xff00);}

#define Mci_Get_Byte_Right_L2H( w)          ( (*( u16 *)(w) >> 8) & 0x00ff)
#define Mci_Set_Byte_Right_L2H( w, v)       { (*( u16 *)(w) &= 0x00ff); (*( u16 *)(w) |= ( ( u16)(v) << 8) & 0xff00);}

#define Mci_Get_Byte_Right_H2L( w)          (*( u16 *)(w) & 0x00ff)
#define Mci_Set_Byte_Right_H2L( w, v)       { (*( u16 *)(w) &= 0xff00); (*( u16 *)(w) |= ( u16)(v) & 0x00ff);}

#define Mci_Get_Word_L2H( w)                ( ( (*( u16 *)(w) >> 8) & 0x00ff) | \
                                              ( (*( u16 *)(w) << 8) & 0xff00) \
																						)
#define Mci_Set_Word_L2H( w, v)             ( *( u16 *)(w) = ((( u16 )(v) >> 8) & 0x00ff) | \
                                                             ((( u16 )(v) << 8) & 0xff00))

#define Mci_Get_Word_H2L( w)                (*( u16 *)(w))
#define Mci_Set_Word_H2L( w, v)             (*( u16 *)(w) = ( u16)(v))

#define Mci_Get_Long_L2H( l)                ( u32)(( ( (*( u32 *)(l)) >> 24) & 0x000000ff) | \
                                                   ( ( (*( u32 *)(l)) >> 8)  & 0x0000ff00) | \
                                                   ( ( (*( u32 *)(l)) << 8 ) & 0x00ff0000) | \
                                                   ( ( (*( u32 *)(l)) << 24) & 0xff000000)   \
                                            )

#define Mci_Set_Long_L2H( l,  v)            *( u32 *)(l) = (( (( u32)(v)) >> 24) & 0x000000ff) | \
                                                           (( (( u32)(v)) >> 8)  & 0x0000ff00) | \
                                                           (( (( u32)(v)) << 8 ) & 0x00ff0000) | \
                                                           (( (( u32)(v)) << 24) & 0xff000000)

#define Mci_Get_Long_H2L( l)                (*(( u32 *)(l)))
#define Mci_Set_Long_H2L( l, v)             (*( u32 *)(l) = ( u32)(v))

/*
 * special inverted long functionality for Modicon
 */
#define Mci_Get_RLong_L2H( l)               ( u32)(( ( (*( u32 *)(l)) >> 8) & 0x000000ff) | \
                                                   ( ( (*( u32 *)(l)) << 8) & 0x0000ff00) | \
                                                   ( ( (*( u32 *)(l)) >> 8) & 0x00ff0000) | \
                                                   ( ( (*( u32 *)(l)) << 8) & 0xff000000)   \
                                            )

#define Mci_Set_RLong_L2H( l,  v)           *( u32 *)(l) = (( (( u32)(v)) >> 8) & 0x000000ff) | \
                                                           (( (( u32)(v)) << 8) & 0x0000ff00) | \
                                                           (( (( u32)(v)) >> 8) & 0x00ff0000) | \
                                                           (( (( u32)(v)) << 8) & 0xff000000)

#define Mci_Get_RLong_H2L( l)               ( u32)(( ( (*( u32 *)(l)) >> 16) & 0x000000ff) | \
                                                   ( ( (*( u32 *)(l)) >> 24) & 0x0000ff00) | \
                                                   ( ( (*( u32 *)(l)) << 16) & 0x00ff0000) | \
                                                   ( ( (*( u32 *)(l)) << 24) & 0xff000000)   \
                                            )


#define Mci_Set_RLong_H2L( l, v)            *( u32 *)(l) = (( (( u32)(v)) >> 16) & 0x000000ff) | \
                                                           (( (( u32)(v)) >> 24) & 0x0000ff00) | \
                                                           (( (( u32)(v)) << 16) & 0x00ff0000) | \
                                                           (( (( u32)(v)) << 24) & 0xff000000)

/*
 * -----------------------------------------------
 */

#define Mci_Get_Double_L2H( d, s)        reverse( (char *)(d), (char *)(s), 8)
#define Mci_Set_Double_L2H( d, s)        reverse( (char *)(d), (char *)(s), 8)

#define Mci_Get_Double_H2L( d, s)        ( *( double *)(d) = *( double *)(s))
#define Mci_Set_Double_H2L( d, s)        ( *( double *)(d) = *( double *)(s))

#endif


#endif /* _FORMAT_DEFS_INCLUDED */
