/* This test driver program encodes a data record and writes the        */
/* encoded result to an output file..                                   */

#include <stdio.h>
#include <stdlib.h>
#include "CSTA-ROSE-PDU-types.h"
#include "CSTA-single-step-transfer.h"

#define MAXMSGLEN 1024

static int encodeSSTRequest (ASN1BEREncodeBuffer& encodeBuffer);

static int encodeROSERequestHeader (ASN1BEREncodeBuffer& encodeBuffer, 
                                   int msglen);

int main (int argc, char** argv)
{
   /* Run-time support variables */

   const OSOCTET* msgptr;
   OSOCTET	msgbuf[MAXMSGLEN];
   int		i, len;
   FILE*        fp;
   const char*        filename = "message.dat";
   OSBOOL     trace = TRUE;

   /* Process command line arguments */

   if (argc > 1) {
      for (i = 1; i < argc; i++) {
         if (!strcmp (argv[i], "-v")) trace = TRUE;
         else if (!strcmp (argv[i], "-o")) filename = argv[++i];
         else if (!strcmp (argv[i], "-notrace")) trace = FALSE;
         else {
            printf ("usage: writer [ -v ] [ -o <filename>\n");
            printf ("   -v  verbose mode: print trace info\n");
            printf ("   -o <filename>  write encoded msg to <filename>\n");
            printf ("   -notrace  do not display trace info\n");
            return 0;
         }
      }
   }


      /* Initialize the encoding context */

      ASN1BEREncodeBuffer encodeBuffer (msgbuf, sizeof(msgbuf));

      /* Encode operation argument */

      if ((len = encodeSSTRequest (encodeBuffer)) < 0) return len;

      /* Encode ROSE header on top of encoded argument */

      if ((len = encodeROSERequestHeader (encodeBuffer, len)) < 0) return len;

      msgptr = encodeBuffer.getMsgPtr();

      if (trace) {
         encodeBuffer.binDump();
      }

      /* Write the encoded message out to the output file */

      /* Note that g++ on Linux suggests the inner parentheses around the
         "assignment used as truth value".  I disagree, but here they are to
         eliminate the warning message.  */
      if ((fp = fopen (filename, "wb"))) {
         fwrite (msgptr, 1, len, fp);
         fclose (fp);
      }
      else {
         printf ("Error opening %s for write access\n", filename);
         return -1;
      }

   return 0;

}

/* Encode "singleStepTrans" operation Request */

static int encodeSSTRequest (ASN1BEREncodeBuffer& encodeBuffer)
{
   ASN1T_SingleStepTransArgument sstArgument;
   ASN1C_SingleStepTransArgument sstArgumentC (encodeBuffer, sstArgument);
   int len;

   /* To make it simple, we will omit all of the optional fields */

   /* SingleStepTransArgument.activeCall */
   ASN1T_ConnectionID_both both;
   both.callID.numocts = 1;
   OSOCTET data = 0x10;
   both.callID.data = &data;
   ASN1T_DeviceID staticID;
   staticID.deviceIdentifier.t = T_DeviceID_deviceIdentifier_dialingNumber;
   staticID.deviceIdentifier.u.dialingNumber = "22343";
   both.deviceID.t = T_LocalDeviceID_staticID;
   both.deviceID.u.staticID = &staticID;
   sstArgument.activeCall.t = T_ConnectionID_both;
   sstArgument.activeCall.u.both = &both;

   /* SingleStepTransArgument.transferredTo */
   sstArgument.transferredTo.deviceIdentifier.t = T_DeviceID_deviceIdentifier_dialingNumber;
   sstArgument.transferredTo.deviceIdentifier.u.dialingNumber = "333333";


   /* Encode */

   len = sstArgumentC.Encode();

   if (len < 0) {
      printf ("Encode of SingleStepTransArgument failed.\n");
      encodeBuffer.printErrorInfo();
   }

   return len;
}

/* Encode ROSE header */

static int encodeROSERequestHeader (ASN1BEREncodeBuffer& encodeBuffer, 
                                   int msglen)
{
   ASN1T_CSTA_ROSE_PDU pdu;
   ASN1C_CSTA_ROSE_PDU pduC (encodeBuffer, pdu);
   ASN1T_CSTA_ROSE_PDU_invoke invoke;
   int len;

   /* Populate header structure */

   invoke.m.argumentPresent = 1;
   invoke.invokeId.t = T_InvokeId_present;
   invoke.invokeId.u.present = 1; /* arbitrary number: should be unique */
   invoke.opcode.t = T_Code_local;
   invoke.opcode.u.local = 50; /* opeartion code for "singleStepTrans" operation from Table 1 */

   /* This is where we get the previously encoded message component..   */
   invoke.argument.numocts = msglen;
   invoke.argument.data = (OSOCTET*) encodeBuffer.getMsgPtr();

   pdu.t = T_CSTA_ROSE_PDU_invoke;
   pdu.u.invoke = &invoke;

   /* Encode */

   len = pduC.Encode();

   if (len < 0) {
      printf ("Encode ROSE PDU failed.\n");
      encodeBuffer.printErrorInfo();
   }

   return len;
}
