TCP communication sample for mbed-os by using 3 GR-PEACHs.

For mbed classic sample

For mbed classic sample, please refer to the following.
mbed-os用サンプルについては以下を参照ください。

https://developer.mbed.org/users/1050186/code/TCP_Comm_sample_mbed_classic/

Warning! 注意!

When exporting and using it, increase the following stack size.
エクスポートして使用する際は、以下のスタックサイズを増やしてください。

mbed-os/features/FEATURE_LWIP/lwip-interface/lwipopts.h

#define TCPIP_THREAD_STACKSIZE      1200
->
#define TCPIP_THREAD_STACKSIZE      2400

Information

Japanese version is available in lower part of this page.
このページの後半に日本語版が用意されています.

What is this ?

TCP communication sample program by using three GR-PEACHs.


Structure

GR-PEACH, ethernet cable and router.
/media/uploads/1050186/structure2.png


The IP address uses static IP address in this sample program. DHCP function of router does not use.

TCPPacket.h (IP address config)

/* GR-PEACH No1 */
#define PEACH_1_IP_ADDRESS      "192.168.2.1"
#define PEACH_1_PORT            11000

/* GR-PEACH No2 */
#define PEACH_2_IP_ADDRESS      "192.168.2.2"
#define PEACH_2_PORT            12000

/* GR-PEACH No3 */
#define PEACH_3_IP_ADDRESS      "192.168.2.3"
#define PEACH_3_PORT            13000

/* Other */
#define SUB_NET_MASK            "255.255.255.0"
#define DEFAULT_GATEWAY         "192.168.2.200"


In order to specify each GR-PEACH's own MAC address, the following function is added to main.cpp.

GR-PEACH NoMAC address
10x74, 0x90, 0x50, 0x00, 0x56, 0xA1
20x74, 0x90, 0x50, 0x00, 0x56, 0xA2
30x74, 0x90, 0x50, 0x00, 0x56, 0xA3

Specify MAC address

// set mac address
void mbed_mac_address(char *mac) {
    mac[0] = 0x74;
    mac[1] = 0x90;
    mac[2] = 0x50;
    mac[3] = 0x00;
    mac[4] = 0x56;
    mac[5] = 0xA1;
}

// set mac address
void mbed_mac_address(char *mac) {
    mac[0] = 0x74;
    mac[1] = 0x90;
    mac[2] = 0x50;
    mac[3] = 0x00;
    mac[4] = 0x56;
    mac[5] = 0xA2;
}

// set mac address
void mbed_mac_address(char *mac) {
    mac[0] = 0x74;
    mac[1] = 0x90;
    mac[2] = 0x50;
    mac[3] = 0x00;
    mac[4] = 0x56;
    mac[5] = 0xA3;
}


In sample program, the communication among GR-PEACH do by Ethernet and use TCP. Therefore, in this program, it is defined send side is client and receive side is server side.
In addition, to send data in TCP communication, client side needs a port number that identifies the application of server side.
The port number of server side in TCP communication is shown below.

GR-PEACH NoRole (Client / Server)port number
1Client11000(unused)
2Client / Server12000
3Server13000



Code of three PEACHs

There is the code of three GR-PEACHs in main.cpp. You can specify GR-PEACH to use by changing the following macro.

Specify GR-PEACH

/**** Selection of PEACH *****/
#define USE_PEACH       (1)     /* 1(PEACH No1) or 2(PEACH No2) or 3(PEACH No3) */
/*****************************/



How to use

  1. Please write bin file to GR-PEACH No1 after build by setting USE_PEACH to 1.
  2. Please write bin file to GR-PEACH No2 after build by setting USE_PEACH to 2.
  3. Please write bin file to GR-PEACH No3 after build by setting USE_PEACH to 3.
  4. Please connect GR-PEACH, ethernet cable and router like as structure picture.
  5. Please start terminal software.
  6. Please press the reset button of GR-PEACH in order of GR-PEACH No3、GR-PEACH No2、GR-PEACH No1.
  7. An IP address is output on terminal.
    Please refer to the following link about how to use the terminal.
    The default value of baud rate in mbed is 9600, and this application uses baud rate 9600.
    https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication
    https://developer.mbed.org/handbook/SerialPC
  8. After the IP address,
    in case of GR-PEACH No1, transmitted data is output, in case of GR-PEACH No2, received data and transmitted data is output, and in case of GR-PEACH No3, received data is output on terminal.
  9. GR-PEACH No1 sends 1 byte to GR-PEACH No2 every 1 second while incrementing.
    GR-PEACH No2 sends received data as is to GR-PEACH No3.
GR-PEACH No1 sample start!!
IP Address is 192.168.2.1
Now connecting...
Send message : 1
Now connecting...
Send message : 2
Now connecting...
Send message : 3
GR-PEACH No2 sample start!!
IP Address is 192.168.2.2
Received message : 1
Now connecting...
Send message : 1
Received message : 2
Now connecting...
Send message : 2
Received message : 3
Now connecting...
Send message : 3
GR-PEACH No3 sample start!!
IP Address is 192.168.2.3
Received message : 1
Received message : 2
Received message : 3



概要

GR-PEACHを3台使ったTCP通信のサンプルプログラムです。


構成

GR-PEACH、イーサーネットケーブル、ルータ。
/media/uploads/1050186/structure2.png


IPアドレスは固定アドレスを使います。ルータのDHCP機能は使用しません。

TCPPacket.h (IP address config)

/* GR-PEACH No1 */
#define PEACH_1_IP_ADDRESS      "192.168.2.1"
#define PEACH_1_PORT            11000

/* GR-PEACH No2 */
#define PEACH_2_IP_ADDRESS      "192.168.2.2"
#define PEACH_2_PORT            12000

/* GR-PEACH No3 */
#define PEACH_3_IP_ADDRESS      "192.168.2.3"
#define PEACH_3_PORT            13000

/* Other */
#define SUB_NET_MASK            "255.255.255.0"
#define DEFAULT_GATEWAY         "192.168.2.200"


GR-PEACHそれぞれのMACアドレスを指定するために、以下の関数をmain.cppに追加しています。

GR-PEACH NoMAC address
10x74, 0x90, 0x50, 0x00, 0x56, 0xA1
20x74, 0x90, 0x50, 0x00, 0x56, 0xA2
30x74, 0x90, 0x50, 0x00, 0x56, 0xA3

Specify MAC address

// set mac address
void mbed_mac_address(char *mac) {
    mac[0] = 0x74;
    mac[1] = 0x90;
    mac[2] = 0x50;
    mac[3] = 0x00;
    mac[4] = 0x56;
    mac[5] = 0xA1;
}

// set mac address
void mbed_mac_address(char *mac) {
    mac[0] = 0x74;
    mac[1] = 0x90;
    mac[2] = 0x50;
    mac[3] = 0x00;
    mac[4] = 0x56;
    mac[5] = 0xA2;
}

// set mac address
void mbed_mac_address(char *mac) {
    mac[0] = 0x74;
    mac[1] = 0x90;
    mac[2] = 0x50;
    mac[3] = 0x00;
    mac[4] = 0x56;
    mac[5] = 0xA3;
}


サンプルプログラムでは、GR-PEACH 間の通信は全てEtherで行いTCPを用います。 そのため、予めTCP通信におる送信側をクライアント、受信側をサーバと定義します。
またTCP通信では、クライアント側はデータ送信するためにサーバ側のアプリケーションを特定する番号、つまり、ポート番号が必要です。
そこで、TCP通信でサーバ側となるGR-PEACH のポート番号を以下に示します。

GR-PEACH NoTCP通信の役割(クライアント/サーバ)ポート番号
1クライアント11000(未使用)
2クライアント / サーバ12000
3サーバ13000



3台のコードについて

GR-PEACH3台のコードはmain.cppにあります。
以下のマクロを変更することで、使用するGR-PEACHを指定できます。

Specify GR-PEACH

/**** Selection of PEACH *****/
#define USE_PEACH       (1)     /* 1(PEACH No1) or 2(PEACH No2) or 3(PEACH No3) */
/*****************************/



使い方

  1. USE_PEACHを1に設定してビルドして、GR-PEACH No1に書き込みます。
  2. USE_PEACHを2に設定してビルドして、GR-PEACH No2に書き込みます。
  3. USE_PEACHを3に設定してビルドして、GR-PEACH No3に書き込みます。
  4. 構成図のようにGR-PEACHとイーサーネットケーブルとルータを接続します。
  5. Terminalソフトを立ち上げます。。
  6. GR-PEACH No3、GR-PEACH No2、GR-PEACH No1の順にGR-PEACHをリセットします。
  7. 3.Terminal上にIPアドレスが出力されます。
    Terminalの使い方は以下のリンクを参照下さい。
    mbedでのボーレートのデフォルト値は9600で、このサンプルではボーレート9600を使います。
    https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication
    https://developer.mbed.org/handbook/SerialPC
    IPアドレスの後、GR-PEACH No1の場合は送信データ、GR-PEACH No2の場合は受信データと送信データ、GR-PEACH No3の場合は受信データがTerminalに出力されます。
  8. GR-PEACH No1は1秒間隔で1ByteのデータをインクリメントしながらGR-PEACH No2に送信します。
    GR-PEACH No2は受信したデータをそのままGR-PEACH No3に送信します。
GR-PEACH No1 sample start!!
IP Address is 192.168.2.1
Now connecting...
Send message : 1
Now connecting...
Send message : 2
Now connecting...
Send message : 3
GR-PEACH No2 sample start!!
IP Address is 192.168.2.2
Received message : 1
Now connecting...
Send message : 1
Received message : 2
Now connecting...
Send message : 2
Received message : 3
Now connecting...
Send message : 3
GR-PEACH No3 sample start!!
IP Address is 192.168.2.3
Received message : 1
Received message : 2
Received message : 3
Committer:
1050186
Date:
Thu Apr 13 05:34:08 2017 +0000
Revision:
0:b7b0e737acd6
Child:
1:cb7a74257b1e
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
1050186 0:b7b0e737acd6 1 #include "mbed.h"
1050186 0:b7b0e737acd6 2 #include "rtos.h"
1050186 0:b7b0e737acd6 3 #include "EthernetInterface.h"
1050186 0:b7b0e737acd6 4 #include "TCPPacket.h"
1050186 0:b7b0e737acd6 5
1050186 0:b7b0e737acd6 6 /**** Selection of PEACH *****/
1050186 0:b7b0e737acd6 7 #define USE_PEACH (1) /* 1(PEACH No1) or 2(PEACH No2) or 3(PEACH No3) */
1050186 0:b7b0e737acd6 8 /*****************************/
1050186 0:b7b0e737acd6 9
1050186 0:b7b0e737acd6 10 #if USE_PEACH == 1 /* in case of PEACH No1 */
1050186 0:b7b0e737acd6 11 Thread TCPTask;
1050186 0:b7b0e737acd6 12 EthernetInterface eth;
1050186 0:b7b0e737acd6 13 DigitalOut led1(LED1);
1050186 0:b7b0e737acd6 14
1050186 0:b7b0e737acd6 15 static tcp_packet send_packet;
1050186 0:b7b0e737acd6 16
1050186 0:b7b0e737acd6 17 int TCP_send(tcp_packet* send_data, int length, const char* adress, int port) {
1050186 0:b7b0e737acd6 18 int ret;
1050186 0:b7b0e737acd6 19 int retry_cnt = 0;
1050186 0:b7b0e737acd6 20 TCPSocket socket;
1050186 0:b7b0e737acd6 21
1050186 0:b7b0e737acd6 22 ret = socket.open(&eth);
1050186 0:b7b0e737acd6 23 if (ret == 0) {
1050186 0:b7b0e737acd6 24 printf("Now connecting...\n");
1050186 0:b7b0e737acd6 25 while (socket.connect(adress, port) < 0) {
1050186 0:b7b0e737acd6 26 printf("Unable to connect to (%s) on port (%d)\n", adress, port);
1050186 0:b7b0e737acd6 27 retry_cnt++;
1050186 0:b7b0e737acd6 28 if (retry_cnt >= TCPSEND_CONN_RETRY_MAX) {
1050186 0:b7b0e737acd6 29 socket.close();
1050186 0:b7b0e737acd6 30 return -1;
1050186 0:b7b0e737acd6 31 }
1050186 0:b7b0e737acd6 32 }
1050186 0:b7b0e737acd6 33 ret = socket.send((char*)send_data, length);
1050186 0:b7b0e737acd6 34 socket.close();
1050186 0:b7b0e737acd6 35 }
1050186 0:b7b0e737acd6 36
1050186 0:b7b0e737acd6 37 return ret;
1050186 0:b7b0e737acd6 38 }
1050186 0:b7b0e737acd6 39
1050186 0:b7b0e737acd6 40 void tcp_comm_task(void) {
1050186 0:b7b0e737acd6 41 int ret;
1050186 0:b7b0e737acd6 42
1050186 0:b7b0e737acd6 43 memset(&send_packet, 0, sizeof(send_packet));
1050186 0:b7b0e737acd6 44 eth.set_network(PEACH_1_IP_ADDRESS, SUB_NET_MASK, DEFAULT_GATEWAY);
1050186 0:b7b0e737acd6 45 ret = eth.connect();
1050186 0:b7b0e737acd6 46 if (ret == 0) {
1050186 0:b7b0e737acd6 47 printf("IP Address is %s\n", eth.get_ip_address());
1050186 0:b7b0e737acd6 48
1050186 0:b7b0e737acd6 49 send_packet.data = 0;
1050186 0:b7b0e737acd6 50 while (1) {
1050186 0:b7b0e737acd6 51 // create data
1050186 0:b7b0e737acd6 52 if (send_packet.data < 0xFF) {
1050186 0:b7b0e737acd6 53 send_packet.data++;
1050186 0:b7b0e737acd6 54 } else {
1050186 0:b7b0e737acd6 55 send_packet.data = 0;
1050186 0:b7b0e737acd6 56 }
1050186 0:b7b0e737acd6 57 // Send message
1050186 0:b7b0e737acd6 58 ret = TCP_send(&send_packet, sizeof(send_packet), PEACH_2_IP_ADDRESS, PEACH_2_PORT);
1050186 0:b7b0e737acd6 59 if (ret >= 0) {
1050186 0:b7b0e737acd6 60 printf("Send message : 0x%02X\n", send_packet.data);
1050186 0:b7b0e737acd6 61 led1 = !led1;
1050186 0:b7b0e737acd6 62 } else {
1050186 0:b7b0e737acd6 63 printf("send failed!!\n");
1050186 0:b7b0e737acd6 64 }
1050186 0:b7b0e737acd6 65 Thread::wait(1000);
1050186 0:b7b0e737acd6 66 }
1050186 0:b7b0e737acd6 67 } else {
1050186 0:b7b0e737acd6 68 printf("Network Connect Error \n");
1050186 0:b7b0e737acd6 69 }
1050186 0:b7b0e737acd6 70 }
1050186 0:b7b0e737acd6 71
1050186 0:b7b0e737acd6 72 /****** main ******/
1050186 0:b7b0e737acd6 73 int main(void) {
1050186 0:b7b0e737acd6 74 printf("GR-PEACH No1 sample start!!\n");
1050186 0:b7b0e737acd6 75
1050186 0:b7b0e737acd6 76 /* Start TCP communicationl processing */
1050186 0:b7b0e737acd6 77 TCPTask.start(callback(tcp_comm_task));
1050186 0:b7b0e737acd6 78
1050186 0:b7b0e737acd6 79 /* Wait for the threads to finish */
1050186 0:b7b0e737acd6 80 TCPTask.join();
1050186 0:b7b0e737acd6 81 }
1050186 0:b7b0e737acd6 82
1050186 0:b7b0e737acd6 83 // set mac address
1050186 0:b7b0e737acd6 84 void mbed_mac_address(char *mac) {
1050186 0:b7b0e737acd6 85 mac[0] = 0x74;
1050186 0:b7b0e737acd6 86 mac[1] = 0x90;
1050186 0:b7b0e737acd6 87 mac[2] = 0x50;
1050186 0:b7b0e737acd6 88 mac[3] = 0x00;
1050186 0:b7b0e737acd6 89 mac[4] = 0x56;
1050186 0:b7b0e737acd6 90 mac[5] = 0xA1;
1050186 0:b7b0e737acd6 91 }
1050186 0:b7b0e737acd6 92
1050186 0:b7b0e737acd6 93 #elif USE_PEACH == 2 /* in case of PEACH No2 */
1050186 0:b7b0e737acd6 94 Thread TCPTask;
1050186 0:b7b0e737acd6 95 EthernetInterface eth;
1050186 0:b7b0e737acd6 96 TCPServer server;
1050186 0:b7b0e737acd6 97 DigitalOut led2(LED2);
1050186 0:b7b0e737acd6 98
1050186 0:b7b0e737acd6 99 #define CONNECT_PENDING_NUM (2)
1050186 0:b7b0e737acd6 100 static tcp_packet send_packet;
1050186 0:b7b0e737acd6 101 static tcp_packet recv_packet;
1050186 0:b7b0e737acd6 102
1050186 0:b7b0e737acd6 103 int TCP_send(tcp_packet* send_data, int length, const char* adress, int port) {
1050186 0:b7b0e737acd6 104 int ret;
1050186 0:b7b0e737acd6 105 int retry_cnt = 0;
1050186 0:b7b0e737acd6 106 TCPSocket socket;
1050186 0:b7b0e737acd6 107
1050186 0:b7b0e737acd6 108 ret = socket.open(&eth);
1050186 0:b7b0e737acd6 109 if (ret == 0) {
1050186 0:b7b0e737acd6 110 printf("Now connecting...\n");
1050186 0:b7b0e737acd6 111 while (socket.connect(adress, port) < 0) {
1050186 0:b7b0e737acd6 112 printf("Unable to connect to (%s) on port (%d)\n", adress, port);
1050186 0:b7b0e737acd6 113 retry_cnt++;
1050186 0:b7b0e737acd6 114 if (retry_cnt >= TCPSEND_CONN_RETRY_MAX) {
1050186 0:b7b0e737acd6 115 socket.close();
1050186 0:b7b0e737acd6 116 return -1;
1050186 0:b7b0e737acd6 117 }
1050186 0:b7b0e737acd6 118 }
1050186 0:b7b0e737acd6 119 ret = socket.send((char*)send_data, length);
1050186 0:b7b0e737acd6 120 socket.close();
1050186 0:b7b0e737acd6 121 }
1050186 0:b7b0e737acd6 122
1050186 0:b7b0e737acd6 123 return ret;
1050186 0:b7b0e737acd6 124 }
1050186 0:b7b0e737acd6 125
1050186 0:b7b0e737acd6 126 static int TCP_receive(tcp_packet* recv_data, int length) {
1050186 0:b7b0e737acd6 127 int ret;
1050186 0:b7b0e737acd6 128 TCPSocket client;
1050186 0:b7b0e737acd6 129
1050186 0:b7b0e737acd6 130 ret = server.accept(&client);
1050186 0:b7b0e737acd6 131 if (ret == 0) {
1050186 0:b7b0e737acd6 132 client.set_blocking(false);
1050186 0:b7b0e737acd6 133 client.set_timeout(1500); // Timeout after 1.5s(default)
1050186 0:b7b0e737acd6 134
1050186 0:b7b0e737acd6 135 ret = client.recv((char*)recv_data, length);
1050186 0:b7b0e737acd6 136 client.close();
1050186 0:b7b0e737acd6 137 }
1050186 0:b7b0e737acd6 138
1050186 0:b7b0e737acd6 139 return ret;
1050186 0:b7b0e737acd6 140 }
1050186 0:b7b0e737acd6 141
1050186 0:b7b0e737acd6 142 void tcp_comm_task(void) {
1050186 0:b7b0e737acd6 143 int ret;
1050186 0:b7b0e737acd6 144
1050186 0:b7b0e737acd6 145 memset(&send_packet, 0, sizeof(send_packet));
1050186 0:b7b0e737acd6 146 memset(&recv_packet, 0, sizeof(recv_packet));
1050186 0:b7b0e737acd6 147 eth.set_network(PEACH_2_IP_ADDRESS, SUB_NET_MASK, DEFAULT_GATEWAY);
1050186 0:b7b0e737acd6 148 ret = eth.connect();
1050186 0:b7b0e737acd6 149 if (ret == 0) {
1050186 0:b7b0e737acd6 150 printf("IP Address is %s\n", eth.get_ip_address());
1050186 0:b7b0e737acd6 151
1050186 0:b7b0e737acd6 152 server.open(&eth);
1050186 0:b7b0e737acd6 153 server.bind(PEACH_2_PORT);
1050186 0:b7b0e737acd6 154 server.listen(CONNECT_PENDING_NUM); // number of pending connections
1050186 0:b7b0e737acd6 155 while (1) {
1050186 0:b7b0e737acd6 156 // Receive message
1050186 0:b7b0e737acd6 157 ret = TCP_receive(&recv_packet, sizeof(recv_packet));
1050186 0:b7b0e737acd6 158 if (ret >= 0) {
1050186 0:b7b0e737acd6 159 printf("Received message : %d\n", recv_packet.data);
1050186 0:b7b0e737acd6 160 // Send message
1050186 0:b7b0e737acd6 161 ret = TCP_send(&send_packet, sizeof(send_packet), PEACH_3_IP_ADDRESS, PEACH_3_PORT);
1050186 0:b7b0e737acd6 162 if (ret >= 0) {
1050186 0:b7b0e737acd6 163 printf("Send message : 0x%02X\n", send_packet.data);
1050186 0:b7b0e737acd6 164 led2 = !led2;
1050186 0:b7b0e737acd6 165 } else {
1050186 0:b7b0e737acd6 166 printf("send failed!!\n");
1050186 0:b7b0e737acd6 167 }
1050186 0:b7b0e737acd6 168 } else {
1050186 0:b7b0e737acd6 169 printf("receive failed!!\n");
1050186 0:b7b0e737acd6 170 }
1050186 0:b7b0e737acd6 171 Thread::wait(100);
1050186 0:b7b0e737acd6 172 }
1050186 0:b7b0e737acd6 173 } else {
1050186 0:b7b0e737acd6 174 printf("Network Connect Error \n");
1050186 0:b7b0e737acd6 175 }
1050186 0:b7b0e737acd6 176 }
1050186 0:b7b0e737acd6 177
1050186 0:b7b0e737acd6 178 /****** main ******/
1050186 0:b7b0e737acd6 179 int main(void) {
1050186 0:b7b0e737acd6 180 printf("GR-PEACH No2 sample start!!\n");
1050186 0:b7b0e737acd6 181
1050186 0:b7b0e737acd6 182 /* Start TCP communicationl processing */
1050186 0:b7b0e737acd6 183 TCPTask.start(callback(tcp_comm_task));
1050186 0:b7b0e737acd6 184
1050186 0:b7b0e737acd6 185 /* Wait for the threads to finish */
1050186 0:b7b0e737acd6 186 TCPTask.join();
1050186 0:b7b0e737acd6 187 }
1050186 0:b7b0e737acd6 188
1050186 0:b7b0e737acd6 189 // set mac address
1050186 0:b7b0e737acd6 190 void mbed_mac_address(char *mac) {
1050186 0:b7b0e737acd6 191 mac[0] = 0x74;
1050186 0:b7b0e737acd6 192 mac[1] = 0x90;
1050186 0:b7b0e737acd6 193 mac[2] = 0x50;
1050186 0:b7b0e737acd6 194 mac[3] = 0x00;
1050186 0:b7b0e737acd6 195 mac[4] = 0x56;
1050186 0:b7b0e737acd6 196 mac[5] = 0xA2;
1050186 0:b7b0e737acd6 197 }
1050186 0:b7b0e737acd6 198
1050186 0:b7b0e737acd6 199 #elif USE_PEACH == 3 /* in case of PEACH No3 */
1050186 0:b7b0e737acd6 200 Thread TCPTask;
1050186 0:b7b0e737acd6 201 EthernetInterface eth;
1050186 0:b7b0e737acd6 202 TCPServer server;
1050186 0:b7b0e737acd6 203 DigitalOut led3(LED3);
1050186 0:b7b0e737acd6 204
1050186 0:b7b0e737acd6 205 #define CONNECT_PENDING_NUM (2)
1050186 0:b7b0e737acd6 206 static tcp_packet recv_packet;
1050186 0:b7b0e737acd6 207
1050186 0:b7b0e737acd6 208 static int TCP_receive(tcp_packet* recv_data, int length) {
1050186 0:b7b0e737acd6 209 int ret;
1050186 0:b7b0e737acd6 210 TCPSocket client;
1050186 0:b7b0e737acd6 211
1050186 0:b7b0e737acd6 212 ret = server.accept(&client);
1050186 0:b7b0e737acd6 213 if (ret == 0) {
1050186 0:b7b0e737acd6 214 client.set_blocking(false);
1050186 0:b7b0e737acd6 215 client.set_timeout(1500); // Timeout after 1.5s(default)
1050186 0:b7b0e737acd6 216
1050186 0:b7b0e737acd6 217 ret = client.recv((char*)recv_data, length);
1050186 0:b7b0e737acd6 218 client.close();
1050186 0:b7b0e737acd6 219 }
1050186 0:b7b0e737acd6 220
1050186 0:b7b0e737acd6 221 return ret;
1050186 0:b7b0e737acd6 222 }
1050186 0:b7b0e737acd6 223
1050186 0:b7b0e737acd6 224 void tcp_comm_task(void) {
1050186 0:b7b0e737acd6 225 int ret;
1050186 0:b7b0e737acd6 226
1050186 0:b7b0e737acd6 227 memset(&recv_packet, 0, sizeof(recv_packet));
1050186 0:b7b0e737acd6 228 eth.set_network(PEACH_3_IP_ADDRESS, SUB_NET_MASK, DEFAULT_GATEWAY);
1050186 0:b7b0e737acd6 229 ret = eth.connect();
1050186 0:b7b0e737acd6 230 if (ret == 0) {
1050186 0:b7b0e737acd6 231 printf("IP Address is %s\n", eth.get_ip_address());
1050186 0:b7b0e737acd6 232
1050186 0:b7b0e737acd6 233 server.open(&eth);
1050186 0:b7b0e737acd6 234 server.bind(PEACH_3_PORT);
1050186 0:b7b0e737acd6 235 server.listen(CONNECT_PENDING_NUM); // number of pending connections
1050186 0:b7b0e737acd6 236 while (1) {
1050186 0:b7b0e737acd6 237 // Receive message
1050186 0:b7b0e737acd6 238 ret = TCP_receive(&recv_packet, sizeof(recv_packet));
1050186 0:b7b0e737acd6 239 if (ret >= 0) {
1050186 0:b7b0e737acd6 240 printf("Received message : %d\n", recv_packet.data);
1050186 0:b7b0e737acd6 241 led3 = !led3;
1050186 0:b7b0e737acd6 242 } else {
1050186 0:b7b0e737acd6 243 printf("receive failed!!\n");
1050186 0:b7b0e737acd6 244 }
1050186 0:b7b0e737acd6 245 Thread::wait(100);
1050186 0:b7b0e737acd6 246 }
1050186 0:b7b0e737acd6 247 } else {
1050186 0:b7b0e737acd6 248 printf("Network Connect Error \n");
1050186 0:b7b0e737acd6 249 }
1050186 0:b7b0e737acd6 250 }
1050186 0:b7b0e737acd6 251
1050186 0:b7b0e737acd6 252 /****** main ******/
1050186 0:b7b0e737acd6 253 int main(void) {
1050186 0:b7b0e737acd6 254 printf("GR-PEACH No3 sample start!!\n");
1050186 0:b7b0e737acd6 255
1050186 0:b7b0e737acd6 256 /* Start TCP communicationl processing */
1050186 0:b7b0e737acd6 257 TCPTask.start(callback(tcp_comm_task));
1050186 0:b7b0e737acd6 258
1050186 0:b7b0e737acd6 259 /* Wait for the threads to finish */
1050186 0:b7b0e737acd6 260 TCPTask.join();
1050186 0:b7b0e737acd6 261 }
1050186 0:b7b0e737acd6 262
1050186 0:b7b0e737acd6 263 // set mac address
1050186 0:b7b0e737acd6 264 void mbed_mac_address(char *mac) {
1050186 0:b7b0e737acd6 265 mac[0] = 0x74;
1050186 0:b7b0e737acd6 266 mac[1] = 0x90;
1050186 0:b7b0e737acd6 267 mac[2] = 0x50;
1050186 0:b7b0e737acd6 268 mac[3] = 0x00;
1050186 0:b7b0e737acd6 269 mac[4] = 0x56;
1050186 0:b7b0e737acd6 270 mac[5] = 0xA3;
1050186 0:b7b0e737acd6 271 }
1050186 0:b7b0e737acd6 272
1050186 0:b7b0e737acd6 273 #endif