/*******************************************************************************
* PMC825 UDP/IP Socket Interface Test Program                                  *
*                                                                              *
* (C) 2010 Stock Flight Systems. All rights reserved.                          *
*                                                                              *
* Filename: pmc825cpm.c                                                        *
*                                                                              *
* Redistribution and use in source and binary forms, with or without           *
* modification, are permitted provided that the following conditions are met:  *
* Redistributions of source code must retain the above copyright notice, this  *
* list of conditions and the following disclaimer.                             *
*                                                                              *
* Redistributions in binary form must reproduce the above copyright notice,    *
* this list of conditions and the following disclaimer in the documentation    *
* and/or other materials provided with the distribution.                       *
*                                                                              *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY EXPRESS    *
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN   *
* NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY DIRECT, INDIRECT,       *
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  *
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         *
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, *
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           *
*                                                                              *
* This file contains a test program which interfaces to the PMC825 via the     *
* UDP/IP socket interface.                                                     *
*                                                                              *
* Function names                                                               *
* ____________________________________________________________________________ *
*                                                                              *
* int main(void)                                                               *
* void ExceptionHandler(void)                                                  *
*                                                                              *
* MODIFICATIONS:                                                               *
*                                                                              *
* When          Version      What                                  Who         *
* ____________________________________________________________________________ *
*                                                                              *
* 26.02.2010    1.0          Initial Version                       M. Stock    *
* 21.04.2010    1.1          Command line interface added          M. Stock    *
* 11.06.2010    1.2          Some minor bugs fixed                 M. Stock    *
*                                                                              *
*******************************************************************************/

/*
 * Includes.
 */

#include <math.h>
#include "pmc825socket.h"
#include "can_as.h"

/*
 * Local definitions.
 */

#define	LPORT_BASE	34567           /* Local UDP/IP port number base */
#define	RPORT_BASE      34568           /* PMC825 UDP/IP port number base */

/*
 * Globals.
 */

PMC825_IF Pmc825;

/*
 * ExceptionHandler() frees all resources then terminates.
 */

void ExceptionHandler(void)
{
CTRL_MSG tx_ctrl;
int ret;

/*
 * Stop the CPM.
 */

tx_ctrl.opcode = CAN_CTRL;
tx_ctrl.svc_rsp_code = INIT_CAN_CHIP;
tx_ctrl.arg[0] = CAN_1M;
tx_ctrl.arg[1] = 0;
tx_ctrl.arg[2] = 0;
tx_ctrl.arg[3] = 0;
tx_ctrl.arg[4] = 0;

Pmc825CtrlWrite(&Pmc825, &tx_ctrl);

Pmc825StopInterface(&Pmc825);

printf("\nProgram terminated, all resources released.\n");
exit(0);
}

/*
 * main() starts here.
 */

int main (int argc, char *argv[])
{
#ifndef WIN32
struct timespec t2,t1 = {0,1000000*5};	/* 10ms frame time */
#endif
float *sine, angle;
unsigned short tmr_res;
unsigned int *iptr, ts, tsl, tsh, *test, host_ip, pmc825_ip, lport, rport;
unsigned char hip[32], pip[32], ch[8];
int loops, ret, tx_seq, statseq, chan, ip[4];
CAN_AS_MSG tx_buf[100];
CTRL_MSG rx_ctrl, tx_ctrl;
CAN_STAT_REGS *stat;

/*
 * First of all, get IP addresses and CAN channel number.
 */

if (argc != 4)
  {
  printf("usage: %s host_ip pmc825_ip can_channel\n", argv[0]);
  exit(1);
  }

strcpy(hip, argv[1]);
strcpy(pip, argv[2]);
strcpy(ch, argv[3]);

sscanf(hip, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]);
host_ip = (ip[0] << 24) | (ip[1] << 16) | (ip[2] << 8) | ip[3];

sscanf(pip, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]);
pmc825_ip = (ip[0] << 24) | (ip[1] << 16) | (ip[2] << 8) | ip[3];

sscanf(ch, "%d", &chan);

lport = LPORT_BASE + chan * 2;
rport = RPORT_BASE + chan * 2;

tmr_res = 1 << chan;

/*
 * Set up the exception handler. We want to make sure that we perform
 * cleanup in case we are terminated by some unexpected event.
 */

#ifndef WIN32
signal(SIGQUIT, (void *) ExceptionHandler);
signal(SIGINT,  (void *) ExceptionHandler);
signal(SIGTERM, (void *) ExceptionHandler);
signal(SIGKILL, (void *) ExceptionHandler);
signal(SIGPIPE, (void *) ExceptionHandler);
#endif

/*
 * Initialize the interface for the PMC825.
 */

ret = Pmc825StartInterface(&Pmc825, pmc825_ip, host_ip, rport, lport, chan);

if (ret == PMC825_MEM_ALLOC_ERR)
  {
  printf("Memory allocation error, exiting ...\n");
  Pmc825StopInterface(&Pmc825);
  exit(1);
  }
else if (ret == PMC825_SOCKET_ERR)
  {
  printf("Socket error, exiting ...\n");
  Pmc825StopInterface(&Pmc825);
  exit(1);
  }

/*
 * Setup the CANaerospace sine wave messages.
 */

angle = 0.0;

for (loops = 0; loops < 100; loops++)
  {
  tx_buf[loops].identifier = 1000;
  tx_buf[loops].byte_count = 8;
  tx_buf[loops].frame_type = DATA;
  tx_buf[loops].node_id = 100;
  tx_buf[loops].data_type = AS_FLOAT;
  tx_buf[loops].service_code = 0;
  tx_buf[loops].msg_code = 0;
  tx_buf[loops].can_status = 0;
  tx_buf[loops].error_counter = 0;
  tx_buf[loops].time_stamp_lo = CPM_TIME_100MS + CPM_TIME_100MS * loops;
  tx_buf[loops].time_stamp_hi = 0;

  sine = (float *) &(tx_buf[loops].data[0]);
  *sine = (float) sin((double) angle);
  angle += 0.062832;

  printf("Sine Wave [%02d] T = 0x%08x : %.2f\n", loops,
                                                 tx_buf[loops].time_stamp_lo,
					         *sine);
  }

/*
 * Activate the CPM in playback mode.
 */

tx_ctrl.opcode = CAN_CTRL;
tx_ctrl.svc_rsp_code = INIT_CAN_CHIP;
tx_ctrl.arg[0] = CAN_1M;
tx_ctrl.arg[1] = 0;
tx_ctrl.arg[2] = 0;
tx_ctrl.arg[3] = 0;
tx_ctrl.arg[4] = CPM_ENABLE;

ret = Pmc825CtrlWrite(&Pmc825, &tx_ctrl);

if (ret < 0)
  printf("Pmc825CtrlWrite[INIT_CAN_CHIP]: %d\n", ret);

/*
 * Initialize other variables.
 */

tx_seq = 1100;
statseq = 0;

/*
 * Now go into an endless loop.
 */

printf("\nStart processing PMC825 data:\n");

for (;;)
  {
  /*
   * Get received CAN control messages and print them.
   */

  ret = Pmc825CtrlRead(&Pmc825, &rx_ctrl);

  while (ret != PMC825_NO_MSG)
    {
    switch (rx_ctrl.opcode)
      {
      case CAN_WRITE_RSP:
	printf("\nEthernet WRITE_RSP Packet\n");
	printf("-------------------------\n");
	printf("Response Code     = %d\n", (short) rx_ctrl.svc_rsp_code);
	printf("Messages written  = %d\n", rx_ctrl.arg[0]);
        break;

      case CAN_STATUS:
        if (statseq < 50)
          statseq++;
        else
          {
          statseq = 0;
          stat = (CAN_STAT_REGS *) &(rx_ctrl.arg[0]);

          printf("\nEthernet STATUS Packet\n");
          printf("----------------------\n");
          printf("PMC825 Name       = %s\n",stat->name);
          printf("PMC825 IP Address = %d.%d.%d.%d\n",stat->ip_addr[0],
                                                     stat->ip_addr[1],
                                                     stat->ip_addr[2],
                                                     stat->ip_addr[3]);
          printf("CAN Channel       = %d\n",chan);
          printf("Bus Timing Reg    = 0x%04x\n",stat->btr);
          printf("CAN Status        = 0x%04x\n",stat->can_status);
          printf("Error Counters    = 0x%04x\n",stat->error_counter);
          printf("CPM Setting       = 0x%04x\n",stat->cpm_mode);
          printf("TX Msgs (100ms)   = %d\n",    stat->tx_msgs);
          printf("RX Msgs (100ms)   = %d\n",    stat->rx_msgs);
          printf("TX Bits (100ms)   = %d\n",    stat->tx_bits);
          printf("RX Bits (100ms)   = %d\n",    stat->rx_bits);
          printf("Board Temperature = %d\n",    stat->t1);
          printf("FPGA Temperature  = %d\n",    stat->t2);
          sleep(1);
          }
        break;

      case CAN_CTRL_RSP:
	printf("\nEthernet CTRL_RSP Packet\n");
	printf("------------------------\n");
	printf("Response Code  = %d\n", (short) rx_ctrl.svc_rsp_code);
        break;
      }
    ret = Pmc825CtrlRead(&Pmc825, &rx_ctrl);
    }

  /*
   * Send a sine wave coded into CANaerospace messages every 12 seconds.
   */

  if (tx_seq == 1190)
    {
    tx_ctrl.opcode = CAN_CTRL;
    tx_ctrl.svc_rsp_code = RESET_TIME_STAMP;
    tx_ctrl.arg[0] = tmr_res;

    ret = Pmc825CtrlWrite(&Pmc825, &tx_ctrl);

    if (ret < 0)
      printf("Pmc825CtrlWrite[RESET_TIME_STAMP]: %d\n", ret);
    }

  if (tx_seq == 1195)
    {
    ret = Pmc825CanAerospaceWrite(&Pmc825, &(tx_buf[0]), 50);

    if (ret < 0)
      printf("Pmc825CanAerospaceWrite[A]: %d\n", ret);
    }

  if (tx_seq < 1200)
    tx_seq++;
  else
    {
    tx_seq = 0;

    ret = Pmc825CanAerospaceWrite(&Pmc825, &(tx_buf[50]), 50);

    if (ret < 0)
      printf("Pmc825CanAerospaceWrite[B]: %d\n", ret);

    for (loops = 0; loops < 100; loops++)
      tx_buf[loops].msg_code++;
    }

  /*
   * 10ms time frame.
   */

#ifndef WIN32
  nanosleep(&t1,&t2);
#else
  Sleep (10);
#endif
  }
exit(0);
}

/*
 * End of file.
 */
