Test program with networking part in RTOS-thread

Dependencies:   mbed mbed-rtos EthernetInterface

Committer:
tuxic
Date:
Sun Jul 22 09:29:28 2012 +0000
Revision:
0:7a6d4b87941e
first test with network routines in a rtos-thread

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tuxic 0:7a6d4b87941e 1 #include "mbed.h"
tuxic 0:7a6d4b87941e 2 #include "EthernetInterface.h"
tuxic 0:7a6d4b87941e 3 #include "cmsis_os.h"
tuxic 0:7a6d4b87941e 4
tuxic 0:7a6d4b87941e 5 DigitalOut led1(LED1);
tuxic 0:7a6d4b87941e 6 DigitalOut led2(LED2);
tuxic 0:7a6d4b87941e 7 DigitalOut led3(LED3);
tuxic 0:7a6d4b87941e 8
tuxic 0:7a6d4b87941e 9 void led2_thread(void const *argument) {
tuxic 0:7a6d4b87941e 10 while (true) {
tuxic 0:7a6d4b87941e 11 led2 = !led2;
tuxic 0:7a6d4b87941e 12 osDelay(1000);
tuxic 0:7a6d4b87941e 13 }
tuxic 0:7a6d4b87941e 14 }
tuxic 0:7a6d4b87941e 15 void net_thread(void const *argument) {
tuxic 0:7a6d4b87941e 16 EthernetInterface eth;
tuxic 0:7a6d4b87941e 17 eth.init(); //Use DHCP
tuxic 0:7a6d4b87941e 18 eth.connect();
tuxic 0:7a6d4b87941e 19 printf("IP Address is %s\n", eth.getIPAddress());
tuxic 0:7a6d4b87941e 20 TCPSocket sock;
tuxic 0:7a6d4b87941e 21 while (true) {
tuxic 0:7a6d4b87941e 22 led3 = !led3;
tuxic 0:7a6d4b87941e 23 sock.connect("mbed.org", 80);
tuxic 0:7a6d4b87941e 24
tuxic 0:7a6d4b87941e 25 char http_cmd[] = "GET /media/uploads/donatien/hello.txt HTTP/1.1\r\nHost: %s\r\n\r\n";
tuxic 0:7a6d4b87941e 26 sock.send(http_cmd, sizeof(http_cmd) - 1, 3000);
tuxic 0:7a6d4b87941e 27
tuxic 0:7a6d4b87941e 28 char in_buf[256];
tuxic 0:7a6d4b87941e 29 bool firstIteration = true;
tuxic 0:7a6d4b87941e 30 int ret;
tuxic 0:7a6d4b87941e 31 do {
tuxic 0:7a6d4b87941e 32 ret = sock.receive(in_buf, 255, firstIteration?3000:0);
tuxic 0:7a6d4b87941e 33 in_buf[ret] = '\0';
tuxic 0:7a6d4b87941e 34
tuxic 0:7a6d4b87941e 35 printf("Received %d chars from server: %s\n", ret, in_buf);
tuxic 0:7a6d4b87941e 36 firstIteration = false;
tuxic 0:7a6d4b87941e 37 } while ( ret > 0 );
tuxic 0:7a6d4b87941e 38
tuxic 0:7a6d4b87941e 39 sock.close();
tuxic 0:7a6d4b87941e 40
tuxic 0:7a6d4b87941e 41 //eth.disconnect();
tuxic 0:7a6d4b87941e 42
tuxic 0:7a6d4b87941e 43 led3 = !led3;
tuxic 0:7a6d4b87941e 44 osDelay(500);
tuxic 0:7a6d4b87941e 45 }
tuxic 0:7a6d4b87941e 46 }
tuxic 0:7a6d4b87941e 47
tuxic 0:7a6d4b87941e 48 osThreadDef(led2_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
tuxic 0:7a6d4b87941e 49 osThreadDef(net_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
tuxic 0:7a6d4b87941e 50
tuxic 0:7a6d4b87941e 51 int main() {
tuxic 0:7a6d4b87941e 52 osThreadCreate(osThread(led2_thread), NULL);
tuxic 0:7a6d4b87941e 53
tuxic 0:7a6d4b87941e 54 osThreadCreate(osThread(net_thread), NULL);
tuxic 0:7a6d4b87941e 55
tuxic 0:7a6d4b87941e 56 while (true) {
tuxic 0:7a6d4b87941e 57 led1 = !led1;
tuxic 0:7a6d4b87941e 58 osDelay(500);
tuxic 0:7a6d4b87941e 59 }
tuxic 0:7a6d4b87941e 60 }
tuxic 0:7a6d4b87941e 61