Test program with networking part in RTOS-thread

Dependencies:   mbed mbed-rtos EthernetInterface

main.cpp

Committer:
tuxic
Date:
2012-07-22
Revision:
0:7a6d4b87941e

File content as of revision 0:7a6d4b87941e:

#include "mbed.h"
#include "EthernetInterface.h"
#include "cmsis_os.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);

void led2_thread(void const *argument) {
    while (true) {
        led2 = !led2;
        osDelay(1000);
    }
}
void net_thread(void const *argument) {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());
    TCPSocket sock;
    while (true) {
        led3 = !led3;
        sock.connect("mbed.org", 80);

        char http_cmd[] = "GET /media/uploads/donatien/hello.txt HTTP/1.1\r\nHost: %s\r\n\r\n";
        sock.send(http_cmd, sizeof(http_cmd) - 1, 3000);

        char in_buf[256];
        bool firstIteration = true;
        int ret;
        do {
            ret = sock.receive(in_buf, 255, firstIteration?3000:0);
            in_buf[ret] = '\0';

            printf("Received %d chars from server: %s\n", ret, in_buf);
            firstIteration = false;
        } while ( ret > 0 );

        sock.close();

        //eth.disconnect();

        led3 = !led3;
        osDelay(500);
    }
}

osThreadDef(led2_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
osThreadDef(net_thread, osPriorityNormal, DEFAULT_STACK_SIZE);

int main() {
    osThreadCreate(osThread(led2_thread), NULL);

    osThreadCreate(osThread(net_thread), NULL);

    while (true) {
        led1 = !led1;
        osDelay(500);
    }
}