/*
 * Copyright (c) 2015, Texas Instruments Incorporated
 * All rights reserved.
 *
 * 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.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 OR
 * CONTRIBUTORS 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.
 */

/*
 *    ======== udpEcho.c ========
 *    Contains BSD sockets code.
 */

#include <string.h>

#include <xdc/std.h>
#include <xdc/cfg/global.h>
#include <xdc/runtime/System.h>
#include <xdc/runtime/Error.h>

#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/drivers/GPIO.h>

/* NDK BSD support */
#include <sys/socket.h>

/* Example/Board Header file */
#include "Board.h"
#include "ethcmd.h"
#include "vdsutils.h"
#include "pstatus.h"

#define UDPPORT 4000
#define UDPPACKETSIZE 256
    char *tbuff;
 /* prototype definition */

    Void ethcmdxmit(UArg arg0, UArg arg1);
    Void initEthXmitter(void);
    Void testtask(UArg arg0, UArg arg1);
    Void ethstsSend(UArg arg0, UArg arg1);

    int                server;
    fd_set             readSet;
    fd_set             wrSet;
    struct sockaddr_in localAddr;
    struct sockaddr_in clientAddr;
    socklen_t          addrlen;
    Bool sockcreated=FALSE;
/*
 *  ======== echoFxn ========
 *  Echoes UDP messages.
 *
 */
Void echoFxn(UArg arg0, UArg arg1)
{
    int                bytesRcvd;
    int                bytesSent;
    int                bytesSent1;
    int                status;
//    int                server;
//    fd_set             readSet;
//    struct sockaddr_in localAddr;
//    struct sockaddr_in clientAddr;
//    socklen_t          addrlen;
    char               buffer[UDPPACKETSIZE];
    char               custombuff[9]={1,2,3,4,5,6,7,8,9};
    char               tbf[5]= {0x23,0x45,0x67,0x89,0x90};

    ethMsgObj ethrecvmsg;
    Event_Handle tEvt;
    Mailbox_Handle tMbx;

    server = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (server == -1) {
        System_printf("Error: socket not created.\n");
        goto shutdown;
    }

    memset(&localAddr, 0, sizeof(localAddr));
    localAddr.sin_family = AF_INET;
    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    localAddr.sin_port = htons(arg0);

    status = bind(server, (struct sockaddr *)&localAddr, sizeof(localAddr));
    if (status == -1) {
        System_printf("Error: bind failed.\n");
        goto shutdown;
    }
    else sockcreated=TRUE;
    System_printf("socket creation finished.\n");
    System_flush();
    do {
        /*
         *  readSet and addrlen are value-result arguments, which must be reset
         *  in between each select() and recvfrom() call
         */
        FD_ZERO(&readSet);
        FD_SET(server, &readSet);
        addrlen = sizeof(clientAddr);

        /* Wait forever for the reply */
        status = select(server + 1, &readSet, NULL, NULL, NULL);
        if (status > 0) {
            if (FD_ISSET(server, &readSet)) {
                bytesRcvd = recvfrom(server, buffer, UDPPACKETSIZE, 0,
                        (struct sockaddr *)&clientAddr, &addrlen);

                if (bytesRcvd > 0) {
                    bytesSent = sendto(server, buffer, bytesRcvd, 0,
                            (struct sockaddr *)&clientAddr, addrlen);
                    bytesSent1=sendto(server, custombuff, 9, 0,
                                      (struct sockaddr *)&clientAddr, addrlen);
  //                copyCharArry(buffer,ethrecvmsg.msgbuf,30 );
  //                tMbx=getCmdMailboxHndle();
  //             Mailbox_post(tMbx, &ethrecvmsg, TIMEOUT);
                   postCmdMBoxMsg(buffer,30 );
                    if (bytesSent < 0 || bytesSent != bytesRcvd) {
                        System_printf("Error: sendto failed.\n");
                        goto shutdown;
                    }
                }
            }
        }
       if(!isXmitQEmpty())
        {


           tbuff=retrieveXmitQ();
  //    copyCharArry(tbuff, tbf,5 );
            bytesSent1=sendto(server, tbuff, 5, 0,
                                                  (struct sockaddr *)&clientAddr, addrlen);
        }

    } while (status > 0);

shutdown:
    if (server > 0) {
        close(server);
    }
}

/*
 *  ======== main ========
 */
int main(void)
{
    /* Call board init functions */
    Board_initGeneral();
    Board_initGPIO();
    Board_initEMAC();
    initEthCmd();
    initEthXmitter();
 //   initTestTask();
    initPstatus();
    /* Turn on user LED */
    GPIO_write(Board_LED0, Board_LED_ON);

    System_printf("Starting the UDP Echo example\nSystem provider is set to "
                  "SysMin. Halt the target to view any SysMin contents in"
                  " ROV.\n");
    /* SysMin will only print to the console when you call flush or exit */
    System_flush();
    System_printf("Just testing\n");
    System_flush();
    /* Start BIOS */
    BIOS_start();

    return (0);
}

Void initEthXmitter(void)
{


    Task_Handle taskHandle;
    Task_Params taskParams;
    Error_Block eb;
    /* Make sure Error_Block is initialized */
    Error_init(&eb);

    Task_Params_init(&taskParams);
     taskParams.stackSize = 1024;
     taskParams.priority = 4;
     taskParams.arg0 = UDPPORT;
     taskHandle = Task_create((Task_FuncPtr)ethcmdxmit, &taskParams, &eb);
     if (taskHandle == NULL) {
         System_printf("main: Failed to create ethcmdxmit Task\n");
     }

}

Void initTestTask(void)
{


    Task_Handle taskHandle;
    Task_Params taskParams;
    Error_Block eb;
    /* Make sure Error_Block is initialized */
    Error_init(&eb);

    Task_Params_init(&taskParams);
     taskParams.stackSize = 1024;
     taskParams.priority = 3;
     taskParams.arg0 = UDPPORT;
     taskHandle = Task_create((Task_FuncPtr)testtask, &taskParams, &eb);
     if (taskHandle == NULL) {
         System_printf("main: Failed to create ethcmdxmit Task\n");
     }

}
Void testtask(UArg arg0, UArg arg1)
{
    for (;;) {
        System_printf("In test Task\n");
        System_flush();
    }

}
Void ethcmdxmit(UArg arg0, UArg arg1)
{
    int                bytesSent1;
    int                status;
    UInt posted;
    for (;;) {
        System_printf("In ethcmdxmit Task\n");
        System_flush();
        posted = Event_pend(getPstsEvtHandle(), Event_Id_NONE, Event_Id_01, BIOS_WAIT_FOREVER);

       if(sockcreated)
        {
       if(!isXmitQEmpty())
         {
           System_printf("Queue updated\n");


            tbuff=retrieveXmitQ();
            FD_ZERO(&readSet);
            FD_SET(server, &readSet);
            addrlen = sizeof(clientAddr);

            /* Wait forever for the reply */
            status = select(server + 1, &readSet, NULL, NULL, NULL);
            System_printf("status = %d\n", status);
            System_printf("error = %d\n", fdError());
 //           if (status > 0) {
                if (FD_ISSET(server, &readSet)) {
             bytesSent1=sendto(server, tbuff, 5, 0,  (struct sockaddr *)&clientAddr, addrlen);
             System_printf("bytesSent1 inside loop = %d\n", bytesSent1);
                }
                System_printf("bytesSent1 = %d\n", bytesSent1);

 //           }
            System_flush();
         }
     }
    } //for loop

}

Void initEthsend(void)
{


    Task_Handle taskHandle;
    Task_Params taskParams;
    Error_Block eb;
    /* Make sure Error_Block is initialized */
    Error_init(&eb);

    Task_Params_init(&taskParams);
     taskParams.stackSize = 1024;
     taskParams.priority = 4;
     taskParams.arg0 = UDPPORT;
     taskHandle = Task_create((Task_FuncPtr)ethstsSend, &taskParams, &eb);
     if (taskHandle == NULL) {
         System_printf("main: Failed to create ethcmdxmit Task\n");
     }

}
Void ethstsSend(UArg arg0, UArg arg1)
{

}
Void psidletask(UArg arg0, UArg arg1)
{

}
