/*********************************************************
* Module Name:  test driver for messages code 
*
* File Name: testSimple.c
*
*********************************************************/
#include "messages.h"

//#define  TRACEME 0

/*********************************************************
* Program :  test driver 
*
*
* Summary:
*
*********************************************************/
int main(int argc, char *argv[]) 
{  
  int rc = FAILURE;

  struct MsgHdr *myMsgHdr = NULL;
  struct DataMsg *myDataMsg = NULL;
  char *myData = NULL;
  char *myNetworkBuffer = NULL;
  struct timeval TimeVal1;
  struct timeval *timeVal1Ptr = &TimeVal1;
  gettimeofday(timeVal1Ptr, NULL);
  

  //Init to acceptable values....
  uint32_t sequenceNum = 0x01020304;
  uint16_t mode  = 0;
  uint16_t msgType  = DATAMSG;
  int dataSize =0;
  int netBufSize =0;
  int dataCount =0;

  sequenceNum = 0x01020304;
  msgType  = DATAMSG;
  
  timeVal1Ptr->tv_sec = 0xffffffff;
  timeVal1Ptr->tv_usec = 0x55555555;

  dataSize =8;
  netBufSize =10000;
  myNetworkBuffer = malloc(netBufSize);

  if (myNetworkBuffer != NULL) {
    bzero(myNetworkBuffer,netBufSize);
    myData = malloc(dataSize);
    if (myData != NULL) {
      bzero(myData,dataSize);

      myMsgHdr = createMsgHdr(sequenceNum,mode,msgType,timeVal1Ptr->tv_sec, timeVal1Ptr->tv_usec);
      rc = dumpBufferToFile(myMsgHdr,sizeof(MsgHdrType), "TESTSIMPLEMSGHDR1.dat");

      //init data to a recognized pattern
      unsigned char i = 0;
      for (i=0;i<dataSize; i++) {
        myData[i]=i;
      }
      printf("testMsg:  init myData:  ");
      showBuf( (void *)myData,dataSize);

      myDataMsg =  createDataMsg(myMsgHdr, dataSize,myData); 
      rc = dumpBufferToFile(myDataMsg,sizeof(DataMsgType), "TESTSIMPLEDATAMSG1.dat");

      printf("TestSimple: (rc from dump:%d, msgSize:%d)  DataMsg  seqnum:%d, msgType:%d, timestamp:%d.%d \n", 
           rc, myDataMsg->msgSize,
           myDataMsg->myHdr->sequenceNum,  myDataMsg->myHdr->msgType,
           myDataMsg->myHdr->timeSentSeconds,  myDataMsg->myHdr->timeSentUSeconds);

      dataCount = packDataMSGToNetworkBuffer(myDataMsg, myNetworkBuffer, netBufSize);
      rc = dumpBufferToFile(myNetworkBuffer,dataCount, "TESTSIMPLENETBUFDATAMSG1.dat");


      struct DataMsg *myDataMsgNEW = NULL;
      dataCount = unpackNetworkBufferToDataMsg(&myDataMsgNEW, myNetworkBuffer, netBufSize);
      rc = dumpBufferToFile(myDataMsgNEW,sizeof(DataMsgType), "TESTSIMPLEDATAMSG2.dat");

      printf("TestSimple: (rc from dump:%d, msgSize:%d)  DataMsgNEW  seqnum:%d, msgType:%d, timestamp:%d.%d \n", 
           rc, myDataMsgNEW->msgSize,
           myDataMsgNEW->myHdr->sequenceNum,  myDataMsgNEW->myHdr->msgType,
           myDataMsgNEW->myHdr->timeSentSeconds,  myDataMsgNEW->myHdr->timeSentUSeconds);

      rc = compareDataMsgs(myDataMsg, myDataMsgNEW);
      if (rc == TRUE) {
        printf("TestSimple: compare is TRUE returns %d  \n", rc);
        rc = SUCCESS;
      }
      else {
        printf("TestSimple:  compare is FALSE returns %d  \n", rc);
        rc = FAILURE;
      }

      int rc1 = freeDataMsg(myDataMsg);
      int rc2 = freeDataMsg(myDataMsgNEW);

      printf ("TestSimple:  free both DataMsgs, rcs: %d, %d \n", rc1, rc2);
    }
    else 
      printf ("TestSimple: FAILED, bad data ptr ,dataSize:%d \n", dataSize);
  }
  else 
      printf ("TestSimple: FAILED, netBuffer failed netBufSize:%d \n", netBufSize);

  if (rc == SUCCESS) {
      printf ("TestSimple: SUCCEEDED !!  \n");
  } else {
      printf ("TestSimple: FAILED !! \n");
  }

  return rc;
}




