NuMaker Ethernet TCP

main.cpp

Committer:
cyliang
Date:
2016-09-19
Revision:
0:45a173aa6aa3
Child:
1:621d4620acd2

File content as of revision 0:45a173aa6aa3:

#if !FEATURE_IPV4
    #error [NOT_SUPPORTED] IPV4 not supported for this target
#endif

#include <algorithm>
#include "mbed.h"
#include "EthernetInterface.h"
#include "TCPSocket.h"
#include "greentea-client/test_env.h"
#include "unity/unity.h"

#ifndef __CC_ARM 
#ifdef __GNUC__
extern "C" caddr_t _sbrk(int incr);
#endif
#endif

//#define DEMO_STATIC_IP
#define LOCAL_LAN

namespace {
    // Test connection information
#ifndef DEMO_STATIC_IP
    #ifndef LOCAL_LAN
const char *HTTP_SERVER_NAME = "developer.mbed.org";
    #else
const char *HTTP_SERVER_NAME = "pt22_winserver2.nuvoton.com";
    #endif
#else
const char *HTTP_SERVER_NAME = "N/A";
const char *HTTP_SERVER_IP = "10.28.10.54";
const char *STATIC_IP = "10.28.10.99";
const char *STATIC_MASK = "255.255.255.0";
const char *DEFAULT_GATEWAY = "10.28.10.254";
#endif
    #ifndef LOCAL_LAN
const char *HTTP_SERVER_FILE_PATH = "/media/uploads/mbed_official/hello.txt";
const int HTTP_SERVER_PORT = 80;
    #else
const char *HTTP_SERVER_FILE_PATH = "/examples/arm_mbed/hello.txt";
const int HTTP_SERVER_PORT = 8080;
    #endif

#if defined(TARGET_VK_RZ_A1H)
    const int RECV_BUFFER_SIZE = 300;
#else
    const int RECV_BUFFER_SIZE = 512;
#endif

    // Test related data
    const char *HTTP_OK_STR = "200 OK";
    const char *HTTP_HELLO_STR = "Hello world!";

    // Test buffers
    char buffer[RECV_BUFFER_SIZE] = {0};
}

bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) {
    const char *f = std::search(first, last, s_first, s_last);
    return (f != last);
}

int main() {
    printf(" 100 \r\n");
//    GREENTEA_SETUP(20, "default_auto");
    printf(" 200 \r\n");
    bool result = true;
     int rc = 0;
#if 1    
    EthernetInterface eth;
#ifndef __CC_ARM 
#ifdef __GNUC__
    printf("sbrk=%x:\r\n", (unsigned int)_sbrk(0));
#endif
#endif 
    printf(" 300 \r\n");
#ifndef DEMO_STATIC_IP    
    eth.connect(); //eth.init(); //Use DHCP
#else
    ; //rc = eth.init(STATIC_IP, STATIC_MASK, DEFAULT_GATEWAY); 
#endif
    printf(" 400 \r\n");
    if (rc != 0) {
        printf("EthernetInterface::init() fails: %d\n", rc);
        return -1;
    }    
        
    printf("TCP client IP Address is %s\r\n", eth.get_ip_address());

    TCPSocket sock(&eth);
    printf(" 500 \r\n");
    if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) {
        printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT);

        // We are constructing GET command like this:
        // GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n
        strcpy(buffer, "GET http://");
        strcat(buffer, HTTP_SERVER_NAME);
        strcat(buffer, HTTP_SERVER_FILE_PATH);
        strcat(buffer, " HTTP/1.0\n\n");
        // Send GET command
        sock.send(buffer, strlen(buffer));

        // Server will respond with HTTP GET's success code
        const int ret = sock.recv(buffer, sizeof(buffer) - 1);
        buffer[ret] = '\0';

        // Find 200 OK HTTP status in reply
        bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
        // Find "Hello World!" string in reply
        bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));

        TEST_ASSERT_TRUE(found_200_ok);
        TEST_ASSERT_TRUE(found_hello);

        if (!found_200_ok) result = false;
        if (!found_hello) result = false;

        printf("HTTP: Received %d chars from server\r\n", ret);
        printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]");
        printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]");
        printf("HTTP: Received massage:\r\n\r\n");
        printf("%s", buffer);
    }
    printf(" 600 \r\n");
#ifndef __CC_ARM 
#ifdef __GNUC__
    printf("sbrk=%x:\r\n", (unsigned int)_sbrk(0));
#endif
#endif 
    sock.close();
    eth.disconnect();
//    GREENTEA_TESTSUITE_RESULT(result);
#endif
}