/*
 * Ethernet_Test.c
 *
 *  Created on: 2020�~4��21��
 *      Author: Wendell
 */

#include "Ethernet_Test.h"
#include "IO_Test.h"

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>                 //write, close, usleep, read
#include <string.h>

void InitEthernetResetIO(void)
{
    /*MCASP0_ACLKX  =>  GPIO3_14*//*Ethernet PHY reset*/
    gpio_set_direction(PIN_ETHERNET_RESET, GPIO_DIR_OUTPUT);
    gpio_write(PIN_ETHERNET_RESET, 0);
    usleep(100000);

    gpio_write(PIN_ETHERNET_RESET, 1);
    usleep(100000);
}

void EthernetDown(int channel)
{
    char tmpbuf[256];

    memset(tmpbuf, 0, 256);
    sprintf(tmpbuf, "/sbin/ifconfig eth%d down", channel > 0 ? 1 : 0);
    system(tmpbuf);
}

void InitEthernet0(void)
{
    char tmpbuf[256];

    //Init Eth0 for internet
    memset(tmpbuf, 0, 256);
    sprintf(tmpbuf, "/sbin/ifconfig eth0 %s netmask %s up", "192.168.1.10", "255.255.255.0");
    system(tmpbuf);
    //memset(tmpbuf,0,256);
    //sprintf(tmpbuf,"route add default gw %s eth0 ", "192.168.1.254");
    //system(tmpbuf);
}

void InitEthernet1(void)
{
    char tmpbuf[256];

    //Init Eth1 for administrator tool
    memset(tmpbuf, 0, 256);
    sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", "192.168.1.20", "255.255.255.0");
    system(tmpbuf);
    //memset(tmpbuf,0,256);
    //sprintf(tmpbuf,"route add default gw %s eth1 ", "192.168.1.254");
    //system(tmpbuf);
}

void DoEthernet0Test(void)
{
    InitEthernetResetIO();

    EthernetDown(0);
    EthernetDown(1);

    InitEthernet0();

    printf("\r\nEthernet 0 Test Done!");
    printf("\r\nSuccess!\r\n");
}

void DoEthernet1Test(void)
{
    InitEthernetResetIO();

    EthernetDown(0);
    EthernetDown(1);

    InitEthernet1();

    printf("\r\nEthernet 1 Test Done!");
    printf("\r\nSuccess!\r\n");
}