/**
  Generated Main Source File

  Company:
    Microchip Technology Inc.

  File Name:
    main.c

  Summary:
    This is the main file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs

  Description:
    This header file provides implementations for driver APIs for all modules selected in the GUI.
    Generation Information :
        Product Revision  :  PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.81.4
        Device            :  PIC18F67J60
        Driver Version    :  2.00
*/

/*
    (c) 2018 Microchip Technology Inc. and its subsidiaries. 
    
    Subject to your compliance with these terms, you may use Microchip software and any 
    derivatives exclusively with Microchip products. It is your responsibility to comply with third party 
    license terms applicable to your use of third party software (including open source software) that 
    may accompany Microchip software.
    
    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY 
    IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS 
    FOR A PARTICULAR PURPOSE.
    
    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, 
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND 
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP 
    HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO 
    THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL 
    CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT 
    OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS 
    SOFTWARE.
*/

#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/TCPIPLibrary/tcpv4.h"
#include <stdint.h>
#include <string.h>

/*
                         Main application
 */
uint8_t TOUT;
#define PORT 7
#define BUFSIZE 100
void App_Manage(void){             // should be return ASAP
    static tcpTCB_t port7TCB;       // create the socket
    static uint8_t rxBuf[BUFSIZE], txBuf[BUFSIZE];
    
    switch(TCP_SocketPoll(&port7TCB))
    {
        case NOT_A_SOCKET:
            TCP_SocketInit(&port7TCB);  // initialize socket
            break;
        case SOCKET_CLOSED:
            TCP_Bind(&port7TCB, PORT);  //configure the local port
            TCP_InsertRxBuffer(&port7TCB, rxBuf, sizeof(rxBuf)); // add receive buffer            
            TCP_Listen(&port7TCB);      // start the server
            TOUT = 0;
            break;
        case SOCKET_CONNECTED:
            // check if the buffer was sent, if yes we can send another buffer
            if(TCP_SendDone(&port7TCB))
            {
                // check to see  if there are any received data
                uint16_t len = TCP_GetRxLength(&port7TCB);
                if(len > 0)
                {
                    TOUT=0;
                    len = TCP_GetReceivedData(&port7TCB);
                    memcpy(txBuf, rxBuf, len);
                    // reuse the RX buffer
                    TCP_InsertRxBuffer(&port7TCB, rxBuf, sizeof(rxBuf));
                    TCP_Send(&port7TCB, txBuf, len);
                }
                else
                    if(TOUT > 10)       // Timeout
                        TCP_Close(&port7TCB);
            }
            break;
        case SOCKET_CLOSING:
            TCP_SocketRemove(&port7TCB);
            break;
        default:
            TOUT = 0;
    }
}
void main(void)
{
    // Initialize the device
    SYSTEM_Initialize();

    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
    // Use the following macros to:

    // Enable the Global Interrupts
    INTERRUPT_GlobalInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Enable the Peripheral Interrupts
    INTERRUPT_PeripheralInterruptEnable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    //TMR1_StartTimer();
    LATE = 0xff;
    while (1)
    {
        Network_Manage();
        App_Manage();
    }
}
/**
 End of File
*/