/**
 * \brief IQI's communication protocol via RS232
 * \copyright 2013-2014, IQ² Development GmbH, Neckartenzlingen. All rights reserved.
 */

#ifndef _iqi_comm_h
#define _iqi_comm_h

enum
{
  CH_ERR = 0x00,
  CH_IOL = 0x40,
  CH_IQI = 0xC0
};

typedef enum
{
  IQParReserved0 = 0,
  IQParIQIOperMode = 1,
  IQParBootVersion = 2,
  IQParApplVersion = 3,
  IQParTimeStamp = 4,
  IQParSerialNumber = 5,
  IQParFDSpeed = 6,
  IQParReserved7 = 7,

  IQParGadgetID = 8,
  IQParSWToolID = 9,
  IQParReserved10 = 10,
  IQParReserved11 = 11,
  IQParReserved12 = 12,
  IQParReserved13 = 13,
  IQParReserved14 = 14,
  IQParReserved15 = 15,

  IQParMstStackVersion = 16,
  IQParReserved17 = 17,
  IQParReserved18 = 18,
  IQParReserved19 = 19,

  IQParConfigCycleTimeStartup = 20,
  IQParConfigCycleTimePreoperate = 21,
  IQParConfigCycleTimeOperate = 22,
  IQParConfigRestOfCycleTime = 23,
  IQParConfigRevisionID = 24,
  IQParInspectionLevel = 25,
  IQParDeviceVendorID = 26,
  IQParDeviceID = 27,
  IQParDeviceFunctionID = 28,
  IQParDeviceSernum = 29,
  IQParReserved30 = 30,
  IQParRebootDevice = 38,
  IQParResetParameters = 39,

  IQParMasterCycleTime = 40,
  IQParMinCycleTime = 41,
  IQParIsSIOSupported = 42,
  IQParODLengthPreoperate = 43,
  IQParFrameTypeOperate = 44,
  IQParODLengthOperate = 45,
  IQParIsISDUSupported = 46,
  IQParRealRevisionID = 47,
  IQParPDInLength = 48,
  IQParPDOutLength = 49,
  IQParIsInterleaveMode = 50,
  IQParRealDeviceVendorID = 51,
  IQParRealDeviceID = 52,
  IQParRealDeviceFunctionID = 53,
  IQParRealDeviceSernum = 54,
  IQParRealConnectionBaudrate = 55,
  IQParReserved56 = 56,
  IQParReserved57 = 57,
  IQParReserved58 = 58,
  IQParReserved59 = 59,

  IQParDSActivState = 60,
  IQParDSDownloadEnable = 61,
  IQParDSUploadEnable = 62,
  IQParReserved63 = 63,
  IQParReserved64 = 64,
  IQParReserved65 = 65,
  IQParReserved66 = 66,
  IQParReserved67 = 67,
  IQParReserved68 = 68,
  IQParReserved69 = 69,

  IQParEMCErrCounter = 70,

  IQParReserved127 = 127,

  IQParDevStackVersion = 128,
  IQParGenDevDirPage = 129,
  IQParGenDevSupportedBaudrate = 130,
  IQParGenDevODRspType = 131,
  IQParGenDevUploadReqFlag = 132,
  IQParGenDevISDUSetLength = 133,
  IQParGenDevMstCycTime = 134,
  IQParReserved135 = 135,

} iqi_ParT;

typedef enum
{
  IQMODE_IOL_MASTER    = 1,
  IQMODE_IOL_DEVICE    = 2,  
  IQMODE_DIRECT_MODE   = 0xFD,
    
  IQMODE_RESERVED      = 0xFF
    
} IQOperModeT;

typedef int16 (*iqi_TryConnectFuncT)(uint16 uiPortNo, char08 *cpErrMsg);

/**
 * \brief Call an command by IQ Interface
 *
 * \param[in]       uiPortNo          COM or virtual ethernet port number of iqInterface
 * \param[in]       ucChannel         a communication channel
 * \param[in]       ucCommand         a required command
 * \param[in]       ucpSendBuf        pointer on send buffer
 * \param[in]       uiBytesCount      count of bytes to send
 * \param[out]      ucpRecvBuf        a pointer on receive buffer
 * \param[in]       uiRecvBufSize     a size of receive buffer
 * \param[out]      cpErrMsg          an error message (minimum 256 symbols!)
 *
 * \return the count of really readed bytes
 */
int16 comm_SendRecv(uint16 uiPortNo,
                    uchar08 ucChannel,
                    uchar08 ucCommand,
                    uchar08 *ucpSendBuf,
                    uint16 uiBytesCount,
                    uchar08 *ucpRecvBuf,
                    uint16 uiRecvBufSize,
                    char08 *cpErrMsg);

/**
 * \brief Search iqInterface on ports range
 *
 * \param[in]   ucFromPortNo        com port number search from
 * \param[in]   ucUntilPortNo       com port number until search
 * \param[out]  cpErrMsg            an error message (minimum 256 symbols!)
 *
 * \return the founded IQI'c com-port number
 */
int16 comm_SearchPortNo(uchar08 ucFromPortNo,
                        uchar08 ucUntilPortNo,
                        char08 *cpErrMsg);

/**
 * \brief Read an IQI's communication parameter
 *
 * \param[in]   uiPortNo      COM or virtual ethernet port number of iqInterface
 * \param[in]   ucParNo       a parameter number
 * \param[out]  ucpBuf        a data buffer
 * \param[in]   uiBufSize     a data buffer size
 * \param[out]  cpErrMsg      an error message (minimum 256 symbols!)
 *
 * \return the actual size of readed data
 */
int16 comm_ReadParameter(uint16 uiPortNo,
                         uchar08 ucParNo,
                         uchar08 *ucpBuf,
                         uint16 uiBufSize,
                         char08 *cpErrMsg);

/**
 * \brief Write an IQI's communication parameter
 *
 * \param[in]   uiPortNo      COM or virtual ethernet port number of iqInterface
 * \param[in]   ucParNo       a parameter number
 * \param[in]   ucpBuf        a data buffer
 * \param[in]   uiBytesCount  a count of bytes to send
 * \param[out]  cpErrMsg      an error message (minimum 256 symbols!)
 */
int16 comm_WriteParameter(uint16 uiPortNo,
                          uchar08 ucParNo,
                          uchar08 *ucpBuf,
                          uint16 uiBytesCount,
                          char08 *cpErrMsg);

DLL_API
int16 comm_ReadSerialNumber(uint16 uiPortNo, ulong64 * ul64pSerialNumber, char08 *cpErrMsg);

#endif
