/*
    XDemo70: Access Domain-name address

    Compiler: BC++ 3.1
    Compile mode: large
    Project: user.c
             v7000.c
             vModbus.c           [after vcom3002.lib]
             ..\Lib\7186EL.Lib
             ..\Lib\tcp_dm32.lib
             ..\Lib\VcomNNNN.Lib, with NNNN being the lib file's version.

    Choose either method below to access Domain-name address.
    1. Add Domain Name Server or
    2. Through DHCP

    Hardware: uPAC-7186EX * 1 (client)

    [Dec 18, 2008] by Liam 
*/

#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "..\lib\7186e.h"
#include "..\lib\tcpip32.h"
#include "..\lib\vxcomm.h"

extern int bUseDhcp;    //Define Vcomnnn.lib

int DN_to_IP(unsigned char *sDN,unsigned char *sIP);
 
void UserCount(void)
{       
    /*
    User's timer trigger function.
    Please refer to XDemo04 for detail description.
    Please refer to XDemo09 for example code.
    */
}

void UserInit(void)
{
        
    /*
    In this function, user CAN:
    1. initialize user's program.
    2. set time interval for calling UserCount().
    3. set initial value of I/O or variables for UserLoopFun().
    4. set initial value of I/O or variables for another functions.
    5. change the default TCP PORT 10000/9999/502 to others value. [after vcom3004.lib]
        Syntax:
        Port10000=newport_10000;  for calling UserCmd       (user.c)
        Port9999=newport_9999;    for calling VcomCmd7000   (v7000.c)
        Port502=newport_502;      for calling VcomCmdModbus (vModbus.c) [after vcom3002.lib]
        PortUser=newport_User;    for calling VcomCmdUser   (user.c)    [after vcom3005.lib]

        Default port value:
        Port10000=10000;
        Port9999=9999;
        Port502=502;
        PortUser=0;
        If the port value is 0, Xserver will not 
        listen that port. That means the port will 
        be disable.

    Please refer to Xdemo9 & Xdemo11 for example code
    */
    SetBaudrate1(115200);   //Set Debug COM port baud rate
    SetDataFormat1(8, 0, 1);  //Set Debug COM port data format
    
    /*
    Access Domain-name address.
    Choose either way below
    1.Add Domain Name Server
      AddDomainNameServer("10.0.0.10");
    2.Through DHCP
      Enable DHCP function
    */
    
    bUseDhcp=1;
     
    /*
    Note:
        1.The DHCP request can get the DNS address, so don't need call AddDomainNameServer
        2.bUseDhcp can only be set in the Userinit().
        3.The IP address obtained from DHCP server doesn't save on EEPROM of i7188E 
    */
}

void UserLoopFun(void)
{
    /*
    VxComm.exe will call this function every scan time
    refer to demo11 for scan-time evaluation     
    */
    
    char IP_Address[20];
    
    DN_to_IP("www.icpdas.com", IP_Address);
    printCom1("www.icpdas.com=%s\n\r", IP_Address);
    
    Delay(500);
    RefreshWDT();
    Delay(500);
    RefreshWDT();
    
}

int UserCmd(unsigned char *Cmd,unsigned char *Response)
{
    /*
    user's command interpreter
    Maximum length of Cmd is 1458 bytes.
    Maximum length of Response is 1024 bytes.
    */
    
    strcpy(Response, Cmd);
    return 1;       /* return error */
}

int VcomUserBinaryCmd(TCPREADDATA *p)
{
    /* 
    VXCOMM.EXE 2.6.12(04,Sep,2001) or later will support this function.

    Xserver executes this function when received a package 
    form TCP port 10000 and the first two bytes are "23".
    
    Please refer to XDemo04 for detail description.
    Please refer to XDemo23 for example code.
    */
    return 1;       /* any value will be accept */
}

int VcomCmdUser(TCPREADDATA *p)  
{ 
    /*
    VCOM3005 (Feb,22,2002) or later will call this function for PortUser.
    
    When packets received by TCP PORT PortUser(user defined) 
    of 7188E/8000E, Xserver will call this function.
    user can get the following message:
    p->ReadUartChar : the buffer store the command data.
                      Maximum length of p->ReadUartChar is 32767 bytes.
    p->Length : the command data length.
    p->Socket : the socket number that receive the command, that is when the user function wants 
                return message to the client, just use the socket to send data.
                usage:  VcomSendSocket(p->Socket,pdata,datalength);
    */
  
    /* here just send back the command to the client. */
    VcomSendSocket(p->Socket, p->ReadUartChar, p->Length);
    return 1;       /* any value will be accept */  
}

void PortUserStart(int skt)
{
    /* XS8_3200.Lib Version 3.2.00 (20,Apr,2004) 
       or later version supports this function.
    
    When a TCP/IP client connects to the 7188E/8000E via the user's defined port(PortUser), 
    the Xserver calls the function once.
    
    You can use function VcomSendSocket to send a message to the client 
    when a connection is established.
    For example:
        VcomSendSocket(skt,"Connection is established.",26); //return 26 bytes.
 
    skt: socket number assigned to the TCP/IP client.
    */
    
    skt=skt; //do nothing
}
 
 
void Port9999Start(int skt)
{
    /* 
    XS8_3200.Lib Version 3.2.00 (20,Apr,2004) 
    or later version supports this function.
    
    When a TCP/IP client connects to the 7188E/8000E TCP port 9999, 
    the Xserver calls the function once.
    
    You can use function VcomSendSocket to send a message to the client 
    when a connection is established.
    For example:
        VcomSendSocket(skt,"Connection is established.",26); //return 26 bytes.
 
    skt: socket number assigned to the TCP/IP client.
    */
    
    skt=skt; //do nothing
}
 
void Port502Start(int skt)
{
    /* XS8_3200.Lib Version 3.2.00 (20,Apr,2004) 
       or later version supports this function.
    
    When a TCP/IP client connects to the 7188E/8000E TCP port 502, 
    the Xserver calls the function once.
    
    You can use function VcomSendSocket to send a message to the client 
    when a connection is established.
    For example:
        VcomSendSocket(skt,"Connection is established.",26); //return 26 bytes.
 
    skt: socket number assigned to the TCP/IP client.
    */
    
    skt=skt; //do nothing
}

//unsigned char *CheckIpAddress(unsigned char *src,Iid *IidDest) 
int DN_to_IP(unsigned char *sDN, unsigned char *sIP)
{  
    unsigned char *tmp;
    struct hostent *hptr;

    if((hptr=gethostbyname(sDN)))
    {
        tmp=*(hptr->h_addr_list);
        sprintf(sIP,"%d.%d.%d.%d", tmp[0], tmp[1], tmp[2], tmp[3]);
        return 0;   //DN -> IP OK
    }
    else
        return -1;  //DN -> IP Faliure
}