/*---------------------------------------------------------------------------
 * File Name: adaptor.c
 * Author: ITEX C Code Generator
 *
 * Copyright 1991-2001 Telelogic AB, All rights reserved.
 * 
 * This Program is owned by Telelogic and is protected by national
 * copyright laws and international copyright treaties. Telelogic
 * grants you the right to use this Program on one computer or in one
 * local computer network at any one time.  Under this License you may
 * only modify the source code for the purpose of adapting it to your
 * environment. You must reproduce and include any copyright and
 * trademark notices on all copies of the source code.  You may not
 * use, copy, merge, modify or transfer the Program except as provided
 * in this License.  Telelogic does not warrant that the Program will
 * meet your requirements or that the operation of the Program will be
 * uninterrupted and error free. You are solely responsible that the
 * selection of the Program and the modification of the source code
 * will achieve your intended results and that the results are
 * actually obtained.
 *
 * Description:
 *  If this file has been generated, the user didn't have an adaptor file
 *  before. The file is generated for the user as a template to allow the
 *  compilation to go through, but also to show the user the functions that
 *  need to be implemented. Observe that the user only needs to implement
 *  the functions he/she knows he/she will use. If for example a test suite
 *  doesn't use timers, there is no need to implement the timer functions.
 *  If a function is called which the user has not implemented a message
 *  will be printed to inform the user about this.
 */

#include "gci.h"
#include "adaptor.h"

#ifdef ASN1ENDE
#include "asn1ende.h"
#endif

/*------------------------------------------------------------------------- 
 * Concurrency part
 *
 */

#ifdef TTCN_CONCURRENCY_ENABLED

void taskinit(void) 
{
}

/*********************************************************************
 * Name:   taskcreate
 * Author: ITEX C Code Generator
 *
 ********************************************************************/

void taskcreate(void *(*function)(void *), void *args)
{
}

/*********************************************************************
 * Name:   taskdelete
 * Author: ITEX C Code Generator
 *
 ********************************************************************/

void taskdelete(void)
{
}

/*********************************************************************
 * Name:   taskreschedule
 * Author: ITEX C Code Generator
 *
 ********************************************************************/

void taskreschedule(void)
{
}

/*********************************************************************
 * Name:   taskcleanup
 * Author: ITEX C Code Generator
 *
 ********************************************************************/

void taskcleanup(void)
{
}

#endif

/*------------------------------------------------------------------------- 
 * Name: logStream
 * Author: ITEX C Code Generator
 *
 * Description: 
 *  Used for logging by GciLog and GciLogPrint. Assigned a value in main().
 *  This declaration is not part of the Gci Interface. 
 */

FILE * logStream;

/*-------------------------------------------------------------------------
 * Name: GciReadTSPar
 * Author: ITEX C Code Generator
 *
 * Description:
 *  If the user is using test suite parameters, this function will be
 *  called once for each of those before running. The first argument to
 *  the function is the name of the test suite parameter and the second
 *  is a reference to where the value can be found. Mostly the second
 *  parameter is used to define a given file or other media from where
 *  the value of the parameter should be fetched.
 *
 * Return Value: GciValue
 *  Return a GciValue with the parameter that was read.
 */

GciValue* GciReadTSPar( char* par, char* pRef )
{
  fprintf( stderr, "GciReadTSPar in adaptor.c not implemented!!\n" );  

  return NULL;
}



/*-------------------------------------------------------------------------
 * Name: GciSend
 * Author: ITEX C Code Generator
 *
 * Description:
 *  This function is called from the TTCN runtime behaviour when something
 *  needs to be sent to the implementation under test. The first argument
 *  is a unique descriptor for the PCO that will be used and the second
 *  argument is the object that needs to be sent. Observe that some kind of
 *  encoding will probably be needed (implemented by the user) before the
 *  object/message is sent away.
 * 
 * Return Value: GciStatus 
 *  GciOk if the sending worked as it should.
 *  GciNotOk if the sending didn't work at all.
 */

GciStatus GciSend( int pcod, GciValue* object )
{
  fprintf( stderr, "GciSend in adaptor.c not implemented!!\n" );

  return GciOk;
}



/*-------------------------------------------------------------------------
 * Name: GciImplicitSend
 * Author: ITEX C Code Generator
 *
 * Description:
 *  This function does a simular task as GciSend, but the sending is not
 *  done through a PCO. An implicit send is typically used for example to
 *  send some message to the test operator to have him/her perform some 
 *  action on the implementation under test that can not be controlled
 *  through a PCO.
 *
 * Return Value: GciStatus
 *  GciOk if the sending worked as it should.
 *  GciNotOk if the sending didn't work at all.
 */

GciStatus GciImplicitSend( GciValue* msg )
{
  fprintf( stderr, "GciImplicitSend in adaptor.c not implemented!!\n" );

  return GciOk;  
}



/*-------------------------------------------------------------------------
 * Name: GciStartTimer
 * Author: ITEX C Code Generator
 *
 * Description:
 *  This function is called when a timer needs to be started. The first
 *  argument is a unique descriptor for the timer, the second argument is
 *  the duration value and the last argument is the unit.
 *
 * Return Value: GciStatus
 *  GciOk if the timer was successfully started.
 *  GciNotOk if the timer couldn't be started at all.
 */

GciStatus GciStartTimer( GciTimerID timerd, long timerVal, int unit )
{
  fprintf( stderr, "GciStartTimer in adaptor.c not implemented!!\n" );

  return GciOk;
}



/*-------------------------------------------------------------------------
 * Name: GciCancelTimer
 * Author: ITEX C Code Generator
 *
 * Description:
 *  This function is called when a timer needs to be stopped. The only
 *  argument the function needs is the unique descriptor for the timer.
 *
 * Return Value: GciStatus
 *  GciOk if the timer could be stopped.
 *  GciNotOk if the timer couldn't be stopped at all.
 */

GciStatus GciCancelTimer( GciTimerID timerd )
{
  fprintf( stderr, "GciCancelTimer in adaptor.c not implemented!!\n" );

  return GciOk;
}



/*-------------------------------------------------------------------------
 * Name: GciReadTimer
 * Author: ITEX C Code Generator
 *
 * Description:
 *  This function is called when a timer needs to be read. The function
 *  needs only one argument which is the unique descriptor for the timer
 *  to be read and returns the timer value.
 *
 * Return Value: long
 *  A long representing the value of the timer.
 */

long GciReadTimer( GciTimerID timerd )
{
  fprintf( stderr, "GciReadTimer in adaptor.c not implemented!!\n" );

  return 0L;
}



/*-------------------------------------------------------------------------
 * Name: GciSnapshot
 * Author: ITEX C Code Generator
 *
 * Description:
 *  This is a very important function for the execution process. In this
 *  function the user must check all PCOs and timers in the system. If the
 *  user is running concurrent TTCN the termination of different test
 *  components must be detected and handled here too. The functions used
 *  to report nack to the TTCN runtime behaviour are:
 * 
 *  GciReceive     - Report that something has been received on a PCO
 *  GciTimeout     - Report that a timer has timed out
 *
 * Return Value: GciStatus
 *  GciOk if everything in snapshot went fine.
 *  GciNotOk if something catastrofic occured while doing the snapshot.
 */

GciStatus GciSnapshot( )
{
  fprintf( stderr, "GciSnapshot in adaptor.c not implemented!!\n" );
  return GciOk;
}



/*-------------------------------------------------------------------------
 * Name: GciConfig
 * Author: ITEX C Code Generator
 *
 * Description:
 *  This function is only called when concurrent TTCN is used. Some TTCN
 *  tables with dynamic behaviour allow the user to specify a configuration
 *  to use for a given test. The information in the configuration can be
 *  traversed using the following managment functions:
 *  
 *  GciGetNoOfComponents         - Number of components in the configuration
 *  GciGetComponent              - Get a given component in the configuration
 *  GciGetComponentName          - Get the name of a component
 *  GciGetComponentDescriptor    - Get the descriptor of a component
 *  GciGetComponentType          - Get the type of a given component
 *  GciGetComponentCPs           - Get the CPs the component uses
 *  GciGetComponentPCOs          - Get the PCOs the component uses
 *
 * Return Value: GciStatus
 *  GciOk if the configuration could be created.
 *  GciNotOk if the configuration could not be successfully created.
 */

GciStatus GciConfig(GciConf config)
{
  fprintf( stderr, "GciConfig in adaptor.c not implemented!!\n" );
  return GciOk;
}



/*-------------------------------------------------------------------------
 * Name: GciCreate
 * Author: ITEX C Code Generator
 *
 * Description:
 *  This function is called when running concurrent TTCN when a test
 *  component needs to be created. The first argument is a unique descriptor
 *  for the component, the second is the behaviour tree to execute and
 *  the last argument is a list of arguments for the test component.
 *
 * Return Value: GciStatus
 *  GciOk if the component could be created
 *  GciNotOk if the component could not be created at all.
 */

GciStatus GciCreate( int comp, char* tree, GciValue* args )
{
  fprintf( stderr, "GciCreate in adaptor.c not implemented!!\n" );  

  return GciOk;
}



/*-------------------------------------------------------------------------
 * Name: GciLogPrint
 * Author: ITEX C Code Generator
 *
 * Description:
 *  This is simply a help function for GciLog.
 *
 * Return Value: void
 *  No return value.
 *
 */

void GciLogPrint( const char* string, const char* str, int isRow )
{
  int maxWithCol1 = 30;
  int maxWithCol2 = 5;
  
  if( isRow )
    fprintf( logStream, "%-*sRow: %s\n", maxWithCol1, string, str );
  else
    fprintf( logStream, "%-*s%s\n", maxWithCol1 + maxWithCol2, string, str );
}



/*-------------------------------------------------------------------------
 * Name: GciLog
 * Author: ITEX C Code Generator
 *
 * Description:
 *  This function is called every time something needs to be logged. Normally
 *  the user would have to write this function too, but we provide here an
 *  implementation to give some extra help on the way. The user is of course
 *  free to totally rewrite this function.
 *
 * Return Value: GciStatus
 *  GciOk if the logging went fine.
 *  GciNotOk if the logging couldn't be performed.
 */

GciStatus GciLog( int logId, const char* logString )
{
  switch( logId )
    {
    case GciLogStartTC:
      GciLogPrint( "Start Test Case:", logString, 0 );
      break;
    case GciLogStopTC:
      GciLogPrint( "Test Case ended:", logString, 0 );
      break;
    case GciLogStartDEF:
      GciLogPrint( "Start Default:", logString, 0 );
      break;
    case GciLogStopDEF:
      GciLogPrint( "Default ended:", logString, 0 );
      break;
    case GciLogVerdict:
      GciLogPrint( "Final verdict:", logString, 0 );
      break;
    case GciLogPVerdict:
      GciLogPrint( "Preliminary verdict:", logString, 0 );
      break;
    case GciLogMatch:
      GciLogPrint( "Line matched:", logString, 0 );
      break;
    case GciLogNOMatch:
      GciLogPrint( "Line not matched:", logString, 0 );
      break;
    case GciLogSendE:
      GciLogPrint( "Send event:", logString, 1 );
      break;
    case GciLogRecE:
      GciLogPrint( "Receive event:", logString, 1 );
      break;
    case GciLogOtherE:
      GciLogPrint( "Otherwise event:", logString, 1 );
      break;
    case GciLogTimeoutE:
      GciLogPrint( "TimeOut:", logString, 1 );
      break;
    case GciLogAssign:
      GciLogPrint( "Assignment:", logString, 1 );
      break;
    case GciLogStartT:
      GciLogPrint( "StartTimer:", logString, 1 );
      break;
    case GciLogStopT:
      GciLogPrint( "StopTimer:", logString, 0 );
      break;
    case GciLogCancelT:
      GciLogPrint( "CancelTimer:", logString, 1 );
      break;
    case GciLogReadT:
      GciLogPrint( "ReadTimer:", logString, 1 );
      break;
    case GciLogAttach:
      GciLogPrint( "Attach:", logString, 1 );
      break;
    case GciLogImplSend:
      GciLogPrint( "ImplicitSend:", logString, 1 );
      break;
    case GciLogGoto:
      GciLogPrint( "Goto:", logString, 1 );
      break;
    case GciLogRec:
      GciLogPrint( "Received on PCO:", logString, 0 );
      break;
    case GciLogTimeout:
      GciLogPrint( "Timed out Timer:", logString, 0 );
      break;
    case GciLogError:
      GciLogPrint( "Error: ", logString, 0);
      break;
    case GciLogCreate:
      GciLogPrint("Create: ", logString, 0);
      break;
    case GciLogDone:
      GciLogPrint("Done: ", logString, 0);
      break;
    case GciLogActivate:
      GciLogPrint("Activate :", logString, 0);
      break;
    case GciLogMessage:
      GciLogPrint("Message: ", logString, 0);
      break;
    case GciLogMatchFailed:
      GciLogPrint("Match failed: ", logString, 0);
      break;
    default:
      fprintf( logStream, "Log descriptor %d string %s not implemented!\n"
	      , logId, logString );
      fflush( logStream );
      return GciNotOk;
      break;
    }

  fflush( logStream );  
  return GciOk;
}



/*-------------------------------------------------------------------------
 * Name:   main
 * Author: ITEX C Code Generator
 *
 * Description:
 *  This is a simple main function that shows how some managment functions
 *  can be used to run tests from a given test suite. First a list of test
 *  cases is fetched and these names are presented to the user. After that
 *  the test case the user wants to run is executed and the verdict is then
 *  presented back.
 *
 * Return Value: integer
 *  A value reporting the success or failure of the test run.
 */

int main(int argc, char **argv)
{
  int i, ver;
  char **list;
  char name[255];
  char *verdicts[] = { "FAIL", "INCONCLUSIVE", "PASS", "NONE" };

  logStream = stdout;

#ifdef TTCN_CONCURRENCY_ENABLED  
  TaskInit       = taskinit; 
  TaskCreate     = taskcreate;
  TaskDelete     = taskdelete;
  TaskReschedule = taskreschedule;
  TaskCleanup    = taskcleanup;
#endif

  if (GciInit() != GciOk) {
    fprintf(stderr, "ERROR: Could not initialize the GCI layer\n");
    exit(1);
  }


  list = GciGetTestCaseList();
  
  printf("Available Test Cases :\n\n");
  
  for (i = 0; list[i] != NULL; ++i)
    printf("%s\n", list[i]);
  
  printf("\nEnter test case name : ");
  gets(name);
  
  ver = GciStartTestCase(name);
     
  fprintf(stderr, "\nFINAL VERDICT = %s\n", verdicts[ver]);
  
#ifdef WIN32
  printf("\n         press Return to continue : ");
  gets(name);
#endif
  return 0;
}

/* End of File */
