/**********************************************************************
 * Function Block Factory Files Generator V 1.0                       *
 * This file was generated automatically!                             *
 * ==============================================                     *
 * DO NEVER MAKE CHANGES IN THIS FILE MANUALLY!!!                     *
 * ==============================================                     *
 * Date/Time: 31.07.00 12:54:37                                       *
 *                                                                    *
 *                                                                    *
 * Copyright 2000, Borst Automation, Fachingen                        *
 * All Rights Reserved                                                *
 *                                                                    *
 *         File Name: HrtSrvIn.c                                      *
 *            Device: MyDevice                                        *
 *           Profile: HART                                            *
 *         Author(s):                                                 *
 *                    Walter Borst                                    *
 *                                                                    *
 * Revision History                                                   *
 * ================                                                   *
 * 7.11.1999, W. Borst:                                               *
 *   Initial Version                                                  *
 * 29.7.2000                                                          *
 *   Some Improvals for HART conformance testing                      *
 *                                                                    *
 **********************************************************************/

/**********************************************************************
 * The standard include                                               *
 **********************************************************************/
#define __HRTSRVIN_H__
#include "..\HrtSrvIn\HrtSrvIn.h"

/**********************************************************************
 * User Includes and Definitions                                      *
 **********************************************************************/
//No user additions so far

/**********************************************************************
 * Global Variables                                                   *
 **********************************************************************/
T_BOOL g_bNoData;
T_BOOL g_bConfChange;

/**********************************************************************
 * The Service Interpreter                                            *
 **********************************************************************/
   /*******************/
void hrtSrvInterpreter(
   /*******************/
     void
  )
/*
 * The HART Command Interpreter
 */
{
  g_bNoData = T_FALSE;

  if (strHrtAppIf.byStatus != HRT_APP_BUSY)
    return;
  switch(strHrtAppIf.bySrvID)
  {
     case 0:
       hrtSrvFnc000();
       break;
     case 1:
       hrtSrvFnc001();
       break;
     case 2:
       hrtSrvFnc002();
       break;
     case 3:
       hrtSrvFnc003();
       break;
     case 6:
       hrtSrvFnc006();
       break;
     case 11:
       hrtSrvFnc011();
       break;
     case 12:
       hrtSrvFnc012();
       break;
     case 13:
       hrtSrvFnc013();
       break;
     case 14:
       hrtSrvFnc014();
       break;
     case 15:
       hrtSrvFnc015();
       break;
     case 16:
       hrtSrvFnc016();
       break;
     case 17:
       hrtSrvFnc017();
       break;
     case 18:
       hrtSrvFnc018();
       break;
     case 19:
       hrtSrvFnc019();
       break;
     default:
       strHrtAppIf.byResponse1 = HRT_CMD_NOT_IMPL;
       strHrtAppIf.byDataLen = 0;
  }
  if (g_bNoData)
    strHrtAppIf.byDataLen = 0;
  strHrtAppIf.byResponse2 = FbHartGetResponse2();
  strHrtAppIf.byStatus = HRT_APP_DONE;
}


/**********************************************************************
 * HART Service Handling Procedures                                   *
 **********************************************************************/

          /**************/
static void hrtSrvFnc000(
          /**************/
     void
  )
/*
 * HART Command 0 Handling Function
 */
{
  strHrtAppIf.byResponse1 = 0;
  strHrtAppIf.byData[0] = (BYTE) co_ui8FbHartCmd0ID;
  strHrtAppIf.byData[1] = (BYTE) co_ui8FbHartMfrID;
  strHrtAppIf.byData[2] = (BYTE) co_ui8FbHartMfrDevType;
  strHrtAppIf.byData[3] = (BYTE) co_ui8FbHartNoReqPreAmbs;
  strHrtAppIf.byData[4] = (BYTE) co_ui8FbHartUnivCmdRev;
  strHrtAppIf.byData[5] = (BYTE) co_ui8FbHartTransmSpecRev;
  strHrtAppIf.byData[6] = (BYTE) co_ui8FbHartSoftwRev;
  strHrtAppIf.byData[7] = (BYTE) co_ui8FbHartHardwRev;
  strHrtAppIf.byData[8] = (BYTE) co_ui8FbHartFlags;
  M_API_MEMCPY(&strHrtAppIf.byData[9],&co_ui24FbHartDeviceIDnum,3);
#ifdef VC_STUDIO
  M_API_SWAP3(&strHrtAppIf.byData[9]);
#endif
  strHrtAppIf.byDataLen = 12;
}

          /**************/
static void hrtSrvFnc001(
          /**************/
     void
  )
/*
 * HART Command 1 Handling Function
 */
{
  strHrtAppIf.byResponse1 = (BYTE) FbHartGetRspCmd1();
  strHrtAppIf.byData[0] = (BYTE) co_ui8MeasPv1unit;
  bLowLevelLocked = TRUE;
  M_API_MEMCPY(&strHrtAppIf.byData[1],&rl_flMeasPv1,4);
#ifdef VC_STUDIO
  M_API_MSfloatToIEEE(&strHrtAppIf.byData[1]);
#endif
  bLowLevelLocked = FALSE;
  strHrtAppIf.byDataLen = 5;
}

          /**************/
static void hrtSrvFnc002(
          /**************/
     void
  )
/*
 * HART Command 2 Handling Function
 */
{
  strHrtAppIf.byResponse1 = 0;
  bLowLevelLocked = TRUE;
  M_API_MEMCPY(&strHrtAppIf.byData[0],&rl_flIoutActCurrent,4);
#ifdef VC_STUDIO
  M_API_MSfloatToIEEE(&strHrtAppIf.byData[0]);
#endif
  M_API_MEMCPY(&strHrtAppIf.byData[4],&rl_flIoutActPercent,4);
#ifdef VC_STUDIO
  M_API_MSfloatToIEEE(&strHrtAppIf.byData[4]);
#endif
  bLowLevelLocked = FALSE;
  strHrtAppIf.byDataLen = 8;
}

          /**************/
static void hrtSrvFnc003(
          /**************/
     void
  )
/*
 * HART Command 3 Handling Function
 */
{
  strHrtAppIf.byResponse1 = 0;
  bLowLevelLocked = TRUE;
  M_API_MEMCPY(&strHrtAppIf.byData[0],&rl_flIoutActCurrent,4);
#ifdef VC_STUDIO
  M_API_MSfloatToIEEE(&strHrtAppIf.byData[0]);
#endif
  strHrtAppIf.byData[4] = (BYTE) co_ui8MeasPv1unit;
  M_API_MEMCPY(&strHrtAppIf.byData[5],&rl_flMeasPv1,4);
#ifdef VC_STUDIO
  M_API_MSfloatToIEEE(&strHrtAppIf.byData[5]);
#endif
  strHrtAppIf.byData[9] = (BYTE) co_ui8MeasPv2unit;
  M_API_MEMCPY(&strHrtAppIf.byData[10],&rl_flMeasPv2,4);
#ifdef VC_STUDIO
  M_API_MSfloatToIEEE(&strHrtAppIf.byData[10]);
#endif
  bLowLevelLocked = FALSE;
  strHrtAppIf.byDataLen = 14;
}

          /**************/
static void hrtSrvFnc006(
          /**************/
     void
  )
/*
 * HART Command 6 Handling Function
 */
{
  /* Write Part of Service */
  if (M_API_IS_WR_PROTECTED)
  { strHrtAppIf.byResponse1 = HRT_IN_WR_PROT_MODE;
    g_bNoData = T_TRUE;
  }
  else
  { if (strHrtAppIf.byDataLen < 1)
    { strHrtAppIf.byResponse1 = HRT_TOO_FEW_DATA_BYTES;
      g_bNoData = T_TRUE;
    }
    else
    { strHrtAppIf.byResponse1 = (BYTE) FbHartGetRspCmd6();
      if (strHrtAppIf.byResponse1 == 0)
      { if(rl_ui8FbHartAddress != strHrtAppIf.byData[0])
        { rl_ui8FbHartAddress = (BYTE) strHrtAppIf.byData[0];
          M_API_EE_WRITE(&ee_ui8FbHartAddress,&rl_ui8FbHartAddress,1);
          se_bFbHartParamChanged = TRUE;
          g_bConfChange = TRUE;
        }
      }
    }
  }

  /* Get the response data */
  strHrtAppIf.byData[0] = (BYTE) rl_ui8FbHartAddress;
  strHrtAppIf.byDataLen = 1;
}

          /**************/
static void hrtSrvFnc011(
          /**************/
     void
  )
/*
 * HART Command 11 Handling Function
 */
{
  strHrtAppIf.byResponse1 = 0;
  strHrtAppIf.byData[0] = (BYTE) co_ui8FbHartCmd0ID;
  strHrtAppIf.byData[1] = (BYTE) co_ui8FbHartMfrID;
  strHrtAppIf.byData[2] = (BYTE) co_ui8FbHartMfrDevType;
  strHrtAppIf.byData[3] = (BYTE) co_ui8FbHartNoReqPreAmbs;
  strHrtAppIf.byData[4] = (BYTE) co_ui8FbHartUnivCmdRev;
  strHrtAppIf.byData[5] = (BYTE) co_ui8FbHartTransmSpecRev;
  strHrtAppIf.byData[6] = (BYTE) co_ui8FbHartSoftwRev;
  strHrtAppIf.byData[7] = (BYTE) co_ui8FbHartHardwRev;
  strHrtAppIf.byData[8] = (BYTE) co_ui8FbHartFlags;
  M_API_MEMCPY(&strHrtAppIf.byData[9],&co_ui24FbHartDeviceIDnum,3);
#ifdef VC_STUDIO
  M_API_SWAP3(&strHrtAppIf.byData[9]);
#endif
  strHrtAppIf.byDataLen = 12;
}

          /**************/
static void hrtSrvFnc012(
          /**************/
     void
  )
/*
 * HART Command 12 Handling Function
 */
{
  strHrtAppIf.byResponse1 = 0;
  M_API_MEMCPY(&strHrtAppIf.byData[0],&rs_paFbHartMessage,24);
  strHrtAppIf.byDataLen = 24;
}

          /**************/
static void hrtSrvFnc013(
          /**************/
     void
  )
/*
 * HART Command 13 Handling Function
 */
{
  strHrtAppIf.byResponse1 = 0;
  M_API_MEMCPY(&strHrtAppIf.byData[0],&rl_paFbHartTagName,6);
  M_API_MEMCPY(&strHrtAppIf.byData[6],&rs_paFbHartDescription,12);
  strHrtAppIf.byData[18] = (BYTE) rs_ui8FbHartDayOfMonth;
  strHrtAppIf.byData[19] = (BYTE) rs_ui8FbHartMonthOfYear;
  strHrtAppIf.byData[20] = (BYTE) rs_ui8FbHartYearSince1900;
  strHrtAppIf.byDataLen = 21;
}

          /**************/
static void hrtSrvFnc014(
          /**************/
     void
  )
/*
 * HART Command 14 Handling Function
 */
{
  strHrtAppIf.byResponse1 = 0;
  M_API_MEMCPY(&strHrtAppIf.byData[0],&co_ui24MeasSensSerNum,3);
#ifdef VC_STUDIO
  M_API_SWAP3(&strHrtAppIf.byData[0]);
#endif
  strHrtAppIf.byData[3] = (BYTE) co_ui8MeasSensLimUnit;
  M_API_MEMCPY(&strHrtAppIf.byData[4],&co_flMeasUpperSensLimit,4);
#ifdef VC_STUDIO
  M_API_MSfloatToIEEE(&strHrtAppIf.byData[4]);
#endif
  M_API_MEMCPY(&strHrtAppIf.byData[8],&co_flMeasLowerSensLimit,4);
#ifdef VC_STUDIO
  M_API_MSfloatToIEEE(&strHrtAppIf.byData[8]);
#endif
  M_API_MEMCPY(&strHrtAppIf.byData[12],&co_flMeasMinSpan,4);
#ifdef VC_STUDIO
  M_API_MSfloatToIEEE(&strHrtAppIf.byData[12]);
#endif
  strHrtAppIf.byDataLen = 16;
}

          /**************/
static void hrtSrvFnc015(
          /**************/
     void
  )
/*
 * HART Command 15 Handling Function
 */
{
  strHrtAppIf.byResponse1 = 0;
  strHrtAppIf.byData[0] = (BYTE) co_ui8FbHartAlmSelCode;
  strHrtAppIf.byData[1] = (BYTE) co_ui8FbHartXferFuncCode;
  strHrtAppIf.byData[2] = (BYTE) co_ui8IoutRangeUnits;
  M_API_MEMCPY(&strHrtAppIf.byData[3],&co_flIoutUpperRange,4);
#ifdef VC_STUDIO
  M_API_MSfloatToIEEE(&strHrtAppIf.byData[3]);
#endif
  M_API_MEMCPY(&strHrtAppIf.byData[7],&co_flIoutLowerRange,4);
#ifdef VC_STUDIO
  M_API_MSfloatToIEEE(&strHrtAppIf.byData[7]);
#endif
  M_API_MEMCPY(&strHrtAppIf.byData[11],&co_flIoutDamping,4);
#ifdef VC_STUDIO
  M_API_MSfloatToIEEE(&strHrtAppIf.byData[11]);
#endif
  strHrtAppIf.byData[15] = (BYTE) rs_ui8FbHartWrProtCode;
  strHrtAppIf.byData[16] = (BYTE) co_ui8FbHartPrivLabDistCode;
  strHrtAppIf.byDataLen = 17;
}

          /**************/
static void hrtSrvFnc016(
          /**************/
     void
  )
/*
 * HART Command 16 Handling Function
 */
{
  strHrtAppIf.byResponse1 = 0;
  M_API_MEMCPY(&strHrtAppIf.byData[0],&rs_ui24FbHartFinAssNum,3);
#ifdef VC_STUDIO
  M_API_SWAP3(&strHrtAppIf.byData[0]);
#endif
  strHrtAppIf.byDataLen = 3;
}

          /**************/
static void hrtSrvFnc017(
          /**************/
     void
  )
/*
 * HART Command 17 Handling Function
 */
{
  /* Write Part of Service */
  if (M_API_IS_WR_PROTECTED)
  { strHrtAppIf.byResponse1 = HRT_IN_WR_PROT_MODE;
    g_bNoData = T_TRUE;
  }
  else
  { if (strHrtAppIf.byDataLen < 24)
    { strHrtAppIf.byResponse1 = HRT_TOO_FEW_DATA_BYTES;
      g_bNoData = T_TRUE;
    }
    else
    { strHrtAppIf.byResponse1 = 0;
      if (strHrtAppIf.byResponse1 == 0)
      { if(memcmp(&rs_paFbHartMessage,&strHrtAppIf.byData[0],24)!=0)
        { M_API_MEMCPY(&rs_paFbHartMessage,&strHrtAppIf.byData[0],24);
          M_API_EE_WRITE(&ee_paFbHartMessage,&rs_paFbHartMessage,24);
          g_bConfChange = TRUE;
        }
      }
    }
  }

  /* Get the response data */
  M_API_MEMCPY(&strHrtAppIf.byData[0],&rs_paFbHartMessage,24);
  strHrtAppIf.byDataLen = 24;
}

          /**************/
static void hrtSrvFnc018(
          /**************/
     void
  )
/*
 * HART Command 18 Handling Function
 */
{
  /* Write Part of Service */
  if (M_API_IS_WR_PROTECTED)
  { strHrtAppIf.byResponse1 = HRT_IN_WR_PROT_MODE;
    g_bNoData = T_TRUE;
  }
  else
  { if (strHrtAppIf.byDataLen < 21)
    { strHrtAppIf.byResponse1 = HRT_TOO_FEW_DATA_BYTES;
      g_bNoData = T_TRUE;
    }
    else
    { strHrtAppIf.byResponse1 = 0;
      if (strHrtAppIf.byResponse1 == 0)
      { if(memcmp(&rl_paFbHartTagName,&strHrtAppIf.byData[0],6)!=0)
        { M_API_MEMCPY(&rl_paFbHartTagName,&strHrtAppIf.byData[0],6);
          M_API_EE_WRITE(&ee_paFbHartTagName,&rl_paFbHartTagName,6);
          se_bFbHartParamChanged = TRUE;
          g_bConfChange = TRUE;
        }
        if(memcmp(&rs_paFbHartDescription,&strHrtAppIf.byData[6],12)!=0)
        { M_API_MEMCPY(&rs_paFbHartDescription,&strHrtAppIf.byData[6],12);
          M_API_EE_WRITE(&ee_paFbHartDescription,&rs_paFbHartDescription,12);
          g_bConfChange = TRUE;
        }
        if(rs_ui8FbHartDayOfMonth != strHrtAppIf.byData[18])
        { rs_ui8FbHartDayOfMonth = (BYTE) strHrtAppIf.byData[18];
          M_API_EE_WRITE(&ee_ui8FbHartDayOfMonth,&rs_ui8FbHartDayOfMonth,1);
          g_bConfChange = TRUE;
        }
        if(rs_ui8FbHartMonthOfYear != strHrtAppIf.byData[19])
        { rs_ui8FbHartMonthOfYear = (BYTE) strHrtAppIf.byData[19];
          M_API_EE_WRITE(&ee_ui8FbHartMonthOfYear,&rs_ui8FbHartMonthOfYear,1);
          g_bConfChange = TRUE;
        }
        if(rs_ui8FbHartYearSince1900 != strHrtAppIf.byData[20])
        { rs_ui8FbHartYearSince1900 = (BYTE) strHrtAppIf.byData[20];
          M_API_EE_WRITE(&ee_ui8FbHartYearSince1900,&rs_ui8FbHartYearSince1900,1);
          g_bConfChange = TRUE;
        }
      }
    }
  }

  /* Get the response data */
  M_API_MEMCPY(&strHrtAppIf.byData[0],&rl_paFbHartTagName,6);
  M_API_MEMCPY(&strHrtAppIf.byData[6],&rs_paFbHartDescription,12);
  strHrtAppIf.byData[18] = (BYTE) rs_ui8FbHartDayOfMonth;
  strHrtAppIf.byData[19] = (BYTE) rs_ui8FbHartMonthOfYear;
  strHrtAppIf.byData[20] = (BYTE) rs_ui8FbHartYearSince1900;
  strHrtAppIf.byDataLen = 21;
}

          /**************/
static void hrtSrvFnc019(
          /**************/
     void
  )
/*
 * HART Command 19 Handling Function
 */
{
  /* Write Part of Service */
  if (M_API_IS_WR_PROTECTED)
  { strHrtAppIf.byResponse1 = HRT_IN_WR_PROT_MODE;
    g_bNoData = T_TRUE;
  }
  else
  { if (strHrtAppIf.byDataLen < 3)
    { strHrtAppIf.byResponse1 = HRT_TOO_FEW_DATA_BYTES;
      g_bNoData = T_TRUE;
    }
    else
    { strHrtAppIf.byResponse1 = 0;
      if (strHrtAppIf.byResponse1 == 0)
      { 
#ifdef VC_STUDIO
        M_API_SWAP3(&strHrtAppIf.byData[0]);
#endif
        if(memcmp(&rs_ui24FbHartFinAssNum,&strHrtAppIf.byData[0],3)!=0)
        { M_API_MEMCPY(&rs_ui24FbHartFinAssNum,&strHrtAppIf.byData[0],3);
          M_API_EE_WRITE(&ee_ui24FbHartFinAssNum,&rs_ui24FbHartFinAssNum,3);
          g_bConfChange = TRUE;
        }
      }
    }
  }

  /* Get the response data */
  M_API_MEMCPY(&strHrtAppIf.byData[0],&rs_ui24FbHartFinAssNum,3);
#ifdef VC_STUDIO
  M_API_SWAP3(&strHrtAppIf.byData[0]);
#endif
  strHrtAppIf.byDataLen = 3;
}

