/****************************************************************************

Module: Test6.cpp

Purpose:

   This module sets the description in a logger using Phase 2 communication.

****************************************************************************/

/////////////////////////////////////////////////////////////////////////////
// Include files.
/////////////////////////////////////////////////////////////////////////////

#include "afx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "spectrum.h"

/////////////////////////////////////////////////////////////////////////////
// Local definitions.
/////////////////////////////////////////////////////////////////////////////

// Define the API version and the API date.
#define API_VERSION "V3.5-Beta3"
#define API_DATE    "April 10, 2001"

// Define a macro for checking the status code returned from the 
// Spectrum API routines.
#define CHECK_STATUS_CODE(StatusCode)                     \
{                                                         \
   if ((StatusCode) != SPC_SC_Success)                    \
   {                                                      \
      char szStatusText[512];                             \
      SPUTL_FormatStatusCode((StatusCode), szStatusText); \
      printf("ERROR: %s\n", szStatusText);                \
      return(StatusCode);                                 \
   }                                                      \
}

/////////////////////////////////////////////////////////////////////////////
// This routine sets the description in a logger using Phase 2 communication.
/////////////////////////////////////////////////////////////////////////////

SPT_lStatusCode main(int argc, char* argv[])
{
   SPT_lStatusCode     lStatusCode;
   SPT_lComPortNumber  lComPortNumber;
   SPT_ulLoggerAddress ulLoggerAddress;

   // Ensure the user passed the correct number of parameters.
   if (argc != 4)
   {
      printf("\n");
      printf("PURPOSE:\n");
      printf("\n");
      printf("   This program sets the description in a logger using Phase 2 communication.\n");
      printf("\n");
      printf("USAGE:\n");
      printf("\n");
      printf("   Test6 ComPortNumber SerialNumber Description\n");
      printf("\n");
      printf("EXAMPLE:\n");
      printf("\n");
      printf("   Test6 1 99112034 \"Main Water Tank\"\n");
      return(SPC_SC_Success);
   }

   // Check the API version.
   char szApiVersion[256];

   lStatusCode = SPAPI_GetApiVersion(szApiVersion);
   CHECK_STATUS_CODE(lStatusCode);

   if (strcmp(szApiVersion, API_VERSION) != 0)
   {
      printf("ERROR: Invalid API version detected");
      return(SPC_SC_Success);
   }

   // Check the API date.
   char szApiDate[256];

   lStatusCode = SPAPI_GetApiDate(szApiDate);
   CHECK_STATUS_CODE(lStatusCode);

   if (strcmp(szApiDate, API_DATE) != 0)
   {
      printf("ERROR: Invalid API date detected");
      return(SPC_SC_Success);
   }

   // Get the COM port number.
   lComPortNumber = atoi(argv[1]);

   // Convert the serial number to the logger address.
   lStatusCode = SPUTL_SerialNumToLoggerAddr(argv[2], &ulLoggerAddress);
   CHECK_STATUS_CODE(lStatusCode);

   // Enable Phase 2 communication.
   lStatusCode = SPAPI_SetProtocol(TRUE);
   CHECK_STATUS_CODE(lStatusCode);

   // Create a logger object.
   SPT_ulLoggerHandle ulLoggerHandle;    
   lStatusCode = SPLGR_CreateLoggerObject(&ulLoggerHandle);
   CHECK_STATUS_CODE(lStatusCode);

   // Read the logger.
   lStatusCode = SPLGR_ReadLogger(ulLoggerHandle, 
                                  lComPortNumber,
                                  ulLoggerAddress,
                                  "");
   CHECK_STATUS_CODE(lStatusCode);

   // Set the description in the logger.
   lStatusCode = SPLGR_SetDescription(ulLoggerHandle, 
                                      lComPortNumber,
                                      argv[3]);
   CHECK_STATUS_CODE(lStatusCode);

   // Delete the logger object.
   lStatusCode = SPLGR_DeleteLoggerObject(ulLoggerHandle);
   CHECK_STATUS_CODE(lStatusCode);

   return(SPC_SC_Success);
}
