/*
DHCPClie.c: Demo for DHCP.
            Shows the IP on 5 digit LED
  
Compiler: BC++ 3.1, 
          Turbo C++ 1.01(3.01) (free from http://cc.codegear.com/free/cpp)

Compile mode: Large

Project: Client.c
         ..\lib\7186el.Lib
         ..\lib\tcp_dm32.lib
         ..\lib\FW_nnnnn.LIB
         
Hordware: uPAC-7186EX

[Nov 04, 2008] by Liam
*/

#include<conio.h>
#include<stdio.h>
#include "..\lib\7186e.h"
#include "..\lib\Tcpip32.h"
#include "..\lib\MFW.h"

void ShowIp(void);

void XS_UserInit(int argc,char *argv[])
{
    void UserLoop(void);

    char version[20];
    extern struct NETDATA netdata[];    //for use of DHCP
    InitLib();

    bUseDhcp=1;     /* use dhcp function, and the ip got is stored in
    netconf[nets[0].confix].Iaddr.c[0] ~netconf[nets[0].confix].Iaddr.c[3]
                    which is defined in tcpip.h*/

    XS_GetVersion(version);
    Print("[X-Server library]: version=%s ",version);
    XS_GetLibDate(version);
    Print("date=%s\r\n",version);
    GetTcpipLibDate(version);
    Print("Tcpip library version:%X, Library Date is %s\r\n",GetTcpipLibVer(),version);

    XS_AddSystemLoopFun(UserLoop);

    // for using DHCP, Set the IP address/MASK to 0 before call Ninit().
    *(long *)netdata[0].Iaddr.c=
    *(long *)netdata[0].Imask.c=
    *(long *)netdata[1].Iaddr.c=
    *(long *)netdata[1].Imask.c=0L;
    // DhcpLeaseTime=xxxx;  // by default DhcpLeaseTime=0,
    // DhcpLeaseTime=20;
    // that is the time is assigned by the DHCP server.
    // the time unit is second.
    Install_DHCP(); // must call Install_DHCP() for DHCP function to work.
 
    XS_AddSystemLoopFun(XS_SocketLoopFun);
    XS_StartSocket();
}

void XS_UserEnd(void)
{
    XS_StopSocket();
    DisableWDT();
}

void UserLoop(void)
{ 
    int data;
    if(Kbhit()){
        data=Getch();
    
        if(data=='q' || data=='Q'){
            QuitMain=1;
        }
    }

    ShowIp();
}    
 
void main(int argc,char *argv[])
{
    XS_main(argc,argv); /* call the XS library main function. */
}

int once=0;
static int idx=0;
void ShowIp(void)
{ 
    int data;
    
    data=netconf[nets[0].confix].Iaddr.c[idx];
    if(once ==0){
        Print("IP = %d.",data);
        once++;
    }else if(once<3){
        Print("%d.",data);
        once++;
    }else if(once==3){
        Print("%d\r\n",data);
        once++;
    }
    idx++;

    Show5DigitLedWithDot(1,idx);
    Show5DigitLed(5, data%10);
    data/=10;
    if(data){
        Show5DigitLed(4, data%10);
        data/=10;
        if(data) Show5DigitLed(3, data);
        else    Show5DigitLed(3, 16);
    }
    else {
        Show5DigitLed(4, 16);
        Show5DigitLed(3, 16);
    }
    Show5DigitLed(2, 16);

    if(idx>=4) idx=0;
    Delay(500);
}
