/*******************************************************************************
********************************************************************************
** COPYRIGHT NOTIFICATION (c) 2013 HMS Industrial Networks AB                 **
**                                                                            **
** This code is the property of HMS Industrial Networks AB.                   **
** The source code may not be reproduced, distributed, or used without        **
** permission. When used together with a product from HMS, this code can be   **
** modified, reproduced and distributed in binary form without any            **
** restrictions.                                                              **
**                                                                            **
** THE CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. HMS DOES NOT    **
** WARRANT THAT THE FUNCTIONS OF THE CODE WILL MEET YOUR REQUIREMENTS, OR     **
** THAT THE OPERATION OF THE CODE WILL BE UNINTERRUPTED OR ERROR-FREE, OR     **
** THAT DEFECTS IN IT CAN BE CORRECTED.                                       **
********************************************************************************
********************************************************************************
** File Description:
********************************************************************************
********************************************************************************
*/
#include "user_adi.h"
#include "abcc_user_def.h"
#include "abcc.h"


/*******************************************************************************
** Constants
********************************************************************************
*/


/*******************************************************************************
** Typedefs
********************************************************************************
*/


/*******************************************************************************
** Public Globals
********************************************************************************
*/

/*-------------------------------------------------------------------------------------------------------
** ADI ENUM strings allocations
** Note: Optional, to be used with ADI property allocations for ENUM, if enum
** strings are to be used.
**
** 1. eValue            - Enum value
** 2. abEnumStr         - Cooresponding enum string value
**-------------------------------------------------------------------------------------------------------
** 1. eValue   |  2. xMinabEnumStrValue
**-------------------------------------------------------------------------------------------------------
*/
/*
AD_ENUMStrType ad_ENUMPropsString[] = { { 0, "Process OK" },
                                        { 1, "Process failed: Water level to high" },
                                        { 2, "Process failed: Valve 1 open problem" }
                                      };
*/

/*------------------------------------------------------------------------------
** ADI value properties allocation (optional)
** Note: Optional, if defined the corresponding entry can be used for ADI instance
**       allocations in psValueProps in AD_asADIEntryList definition below.
**
** 1. xMinValue         - Minimum value allowed.
** 2. xMaxValue         - Maximum value allowed.
** 3. xDefaultValue     - Default value.
** Note: The two files below only apply to the AD_ENUMType type.
** 4. bNumOfEnumStrings - Number of enum string entries in (5) below.
** 5. psEnumStrings     - Pointer to an array of AD_ENUMStrType defining the
**                        enum value and corresponding string.
**------------------------------------------------------------------------------
** 1. xMinValue | 2. xMaxValue | 3. xDefaultValue | ( 4. bNumOfEnumStrings |
** 5. psEnumStrings )
**------------------------------------------------------------------------------
*/
/*
AD_BOOL8Type   ad_BOOL8Props_0_1_0             = { 0, 1, 0 };
AD_SINT8Type   ad_SINT8Props_n100_100_0        = { -100, 100, 0};
AD_UINT8Type   ad_UINT8Props_1_254_0           = { 1,  254, 0 };
AD_SINT16Type  ad_SINT16Props_n32760_32760_0   = { -32760, 32760, 0 };
AD_UINT16Type  ad_UINT16Props_1_65534_0        = { 1, 65534, 0 };
AD_SINT32Type  ad_SINT32Props_n900K_900K_0     = { -900000, 900000, 0 };
AD_UINT32Type  ad_UINT32Props_1_0xFFFFFFFE_0   = { 1, 0xFFFFFFFE, 0 };
AD_FLOAT32Type ad_FLOAT32Props_n90p45_90p45_0  = { -90.45F, 90.45F, 0 };
AD_ENUMType    ad_ENUMProps_0_2_0_3STR = { 0, 2, 0, 3, ad_ENUMPropsString };

#ifdef AB_ENABLE_64BIT_ADI_SUPPORT
AD_SINT64Type  ad_SINT64Props_n900K_900K_0            = { -900000, 900000, 0 };
AD_UINT64Type  ad_UINT64Props_0_0xFFFFFFFFFFFFFFFE_0  = { 0, 0xFFFFFFFFFFFFFFFE, 0 };
#endif
*/

/*------------------------------------------------------------------------------
** ADI instance allocation
**
** 1. iInstance               - ADI instance number (1-65535); 0 is reserved for Class.
** 2. pabName                 - Name of the ADI as a character string (ADI instance attribute #1).
**                              If NULL, a 0 length name will be returned.
** 3. bDataType               - ADI data type format, can be one of the following:
**                               ABP_BOOL:    Boolean.
**                               ABP_SINT8:   8-bit signed integer.
**                               ABP_SINT16:  16-bit signed integer.
**                               ABP_SINT32:  32-bit signed integer.
**                               ABP_UINT8:   8-bit unsigned integer.
**                               ABP_UINT16:  16-bit unsigned integer.
**                               ABP_UINT32:  32-bit unsigned integer.
**                               ABP_CHAR:    Character.
**                               ABP_ENUM:    Enumeration.
**                               ABP_SINT64:  64-bit signed integer.
**                               ABP_UINT64:  64-bit unsigned integer.
**                               ABP_FLOAT:   Floating point value (32-bits).
** 4. bNumOfElements          - Number of elements of the specified data type in (3).
** 5. bDesc                   - Entry descriptor; bits filed with the following configurations:
**                               ABP_APPD_DESCR_GET_ACCESS:  Get service is allowed on value attribute.
**                               ABP_APPD_DESCR_SET_ACCESS:  Set service is allowed on value attribute.
** 6. pxValuePtr              - Pointer to local value variable.
** 6. psValueProps            - Pointer to local value properties struct, if NULL no properties are applied (max/min/default).
**------------------------------------------------------------------------------
** 1. iInstance | 2. pabName | 3. bDataType | 4. bNumOfElements | 5. bDesc  |
** 6. { { pxValuePtr, pxValuePropPtr } }
**------------------------------------------------------------------------------
*/

/*
** Example implementation of looping process data.
** Here we map the data variables to the ADI instances.
*/
UINT32 ADI_DebitInstan;
UINT32 ADI_PoidsInstan;
UINT32 ADI_PoidsMetre;
UINT32 ADI_Vitesse;
UINT32 ADI_TotalPartiel;
UINT32 ADI_TotalGlobal;
UINT32 ADI_Tare;
UINT32 ADI_EtatBascule;
UINT32 ADI_CmdsBascule;
UINT8 ADI_DigitalInput;
UINT8 ADI_DigitalOutput;
/*
** Each property instance contains:
** - Min value
** - Max value
** - Default value
*/
static AD_UINT32Type  ADI_DebitInstanProps   = { 0, 0xFFFFFFFE, 0 };
static AD_UINT32Type  ADI_PoidsInstanProps   = { 0, 0xFFFFFFFE, 0 };
static AD_UINT32Type  ADI_PoidsMetreProps    = { 0, 0xFFFFFFFE, 0 };
static AD_UINT32Type  ADI_VitesseProps      = { 0, 0xFFFFFFFE, 0 };
static AD_UINT32Type   ADI_TotalPartielProps  = { 0, 0xFFFFFFFE, 0 };
static AD_UINT32Type   ADI_TotalGlobalProps   = { 0, 0xFFFFFFFE, 0 };
static AD_UINT32Type   ADI_TareProps          = { 0, 0xFFFFFFFE, 0 };
static AD_UINT32Type   ADI_EtatBasculeProps   = { 0, 0xFFFFFFFE, 0 };
static AD_UINT32Type   ADI_CmdsBasculeProps   = { 0, 0xFFFFFFFE, 0 };
static AD_UINT8Type   ADI_DigitalInputProps   = { 0, 255, 0 };
static AD_UINT8Type   ADI_DigitalOutputProps   = { 0, 255, 0 };
/*
** ADI properties.
*/
static const AD_AdiEntryType AD_asADIEntryList[] =
{
   /* Idx:0 */ { 300,  "Débit instantané",  ABP_UINT32,   1, ABP_APPD_DESCR_GET_ACCESS | ABP_APPD_DESCR_MAPPABLE_WRITE_PD,  {{&ADI_DebitInstan, &ADI_DebitInstanProps}} },
   /* Idx:1 */ { 301,  "Poids instantané",  ABP_UINT32,  1, ABP_APPD_DESCR_GET_ACCESS | ABP_APPD_DESCR_MAPPABLE_WRITE_PD,   {{&ADI_PoidsInstan, &ADI_PoidsInstanProps}} },
   /* Idx:2 */ { 302,  "Poids au mètre",    ABP_UINT32,   1, ABP_APPD_DESCR_GET_ACCESS | ABP_APPD_DESCR_MAPPABLE_WRITE_PD,  {{&ADI_PoidsMetre, &ADI_PoidsMetreProps}} },
   /* Idx:3 */ { 303,  "Vitesse",           ABP_UINT32,   1, ABP_APPD_DESCR_GET_ACCESS | ABP_APPD_DESCR_MAPPABLE_WRITE_PD,  {{&ADI_Vitesse, &ADI_VitesseProps}} },
   /* Idx:4 */ { 304,  "Totalisateur Partiel", ABP_UINT32,   1, ABP_APPD_DESCR_GET_ACCESS | ABP_APPD_DESCR_MAPPABLE_WRITE_PD,{{&ADI_TotalPartiel, &ADI_TotalPartielProps}} },
   /* Idx:5 */ { 305,  "Totalisateur Global",  ABP_UINT32,  1, ABP_APPD_DESCR_GET_ACCESS | ABP_APPD_DESCR_MAPPABLE_WRITE_PD, {{&ADI_TotalGlobal, &ADI_TotalGlobalProps}} },
   /* Idx:6 */ { 306,  "Tare",                 ABP_UINT32,   1, ABP_APPD_DESCR_GET_ACCESS | ABP_APPD_DESCR_MAPPABLE_WRITE_PD,{{&ADI_Tare, &ADI_TareProps}} },
   /* Idx:7 */ { 400,  "Etat Bascule",         ABP_UINT32,  1, ABP_APPD_DESCR_GET_ACCESS | ABP_APPD_DESCR_MAPPABLE_WRITE_PD,  {{&ADI_EtatBascule, &ADI_EtatBasculeProps}} },
   /* Idx:8 */ { 500,  "Commandes Bascule",    ABP_UINT32,  1, ABP_APPD_DESCR_GET_ACCESS | ABP_APPD_DESCR_SET_ACCESS | ABP_APPD_DESCR_MAPPABLE_READ_PD | ABP_APPD_DESCR_MAPPABLE_WRITE_PD, {{&ADI_CmdsBascule, &ADI_CmdsBasculeProps}} },
   /* Idx:9 */ { 401,  "Digital Output",    ABP_UINT8,  1, ABP_APPD_DESCR_GET_ACCESS | ABP_APPD_DESCR_SET_ACCESS | ABP_APPD_DESCR_MAPPABLE_READ_PD | ABP_APPD_DESCR_MAPPABLE_WRITE_PD, {{&ADI_DigitalOutput, &ADI_DigitalOutputProps}} },
   /* Idx:10 */ { 402, "Digital Input",    ABP_UINT8,  1, ABP_APPD_DESCR_GET_ACCESS | ABP_APPD_DESCR_MAPPABLE_WRITE_PD, {{&ADI_DigitalInput, &ADI_DigitalInputProps}} },

};

/*-------------------------------------------------------------------------------------------------------
** ADI process data mappings ( Application -> Anybus )
**
**-------------------------------------------------------------------------------------------------------
** Array of indexes entries in AD_asADIEntryList (Idx:xx) in native mapping order
** Note: Mapping sequence is terminated by index value 0xFFFF and MUST be present at end of LIST
** NOTE!!! Use INDEX not instance number.
**-------------------------------------------------------------------------------------------------------
*/
static const AD_DefaultMapType AD_asDefaultMap[]= {  { 0, PD_WRITE },
                                                      { 1, PD_WRITE  },
                                                      { 2, PD_WRITE  },
                                                      { 3, PD_WRITE  },
                                                      { 4, PD_WRITE  },
                                                      { 5, PD_WRITE  },
                                                      { 6, PD_WRITE  },
                                                      { 7, PD_WRITE  },
                                                      { 8, PD_READ  },
                                                      { 9, PD_READ  },
                                                      { 10, PD_WRITE  },
                                                      { 0xFFFF} };

/*static const AD_DefaultMapType AD_asDefaultMap[]= { { 0, PD_WRITE },
                                                      { 1, PD_READ  },
                                                      { 0xFFFF} };*/

/*******************************************************************************
** Private Globals
********************************************************************************
*/


/*******************************************************************************
** Private Services
********************************************************************************
*/


/*******************************************************************************
** Public Services
********************************************************************************
*/

UINT16 AD_GetNumAdi(void  )
{
   return sizeof( AD_asADIEntryList ) / sizeof( AD_AdiEntryType );
}

const AD_DefaultMapType* ADI_GetDefaultMap(void  )
{
   return AD_asDefaultMap;
}

const AD_AdiEntryType* ADI_GetADIEntryList(void  )
{
   return AD_asADIEntryList;
}


/*******************************************************************************
** End of adi_default.c
********************************************************************************
*/
