Tomo Yamanaka / Mbed OS TCP_Comm_sample
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "EthernetInterface.h"
00004 #include "TCPPacket.h"
00005 
00006 /**** Selection of PEACH *****/
00007 #define USE_PEACH       (1)     /* 1(PEACH No1) or 2(PEACH No2) or 3(PEACH No3) */
00008 /*****************************/
00009 
00010 #if USE_PEACH == 1      /* in case of PEACH No1 */
00011 Thread            TCPTask;
00012 EthernetInterface eth;
00013 DigitalOut        led1(LED1);
00014 
00015 static tcp_packet send_packet;
00016 
00017 int TCP_send(tcp_packet* send_data, int length, const char* adress, int port) {
00018     int ret;
00019     int retry_cnt = 0;
00020     TCPSocket socket;
00021 
00022     ret = socket.open(&eth);
00023     if (ret == 0) {
00024         printf("Now connecting...\n");
00025         while (socket.connect(adress, port) < 0) {
00026             printf("Unable to connect to (%s) on port (%d)\n", adress, port);
00027             retry_cnt++;
00028             if (retry_cnt >= TCPSEND_CONN_RETRY_MAX) {
00029                 socket.close();
00030                 return -1;
00031             }
00032         }
00033         ret = socket.send((char*)send_data, length);
00034         socket.close();
00035     }
00036 
00037     return ret;
00038 }
00039 
00040 void tcp_comm_task(void) {
00041     int ret;
00042 
00043     memset(&send_packet, 0, sizeof(send_packet));
00044     eth.set_network(PEACH_1_IP_ADDRESS, SUB_NET_MASK, DEFAULT_GATEWAY);
00045     ret = eth.connect();
00046     if (ret == 0) {
00047         printf("IP Address is %s\n", eth.get_ip_address());
00048 
00049         send_packet.data = 0;
00050         while (1) {
00051             // create data
00052             if (send_packet.data < 0xFF) {
00053                 send_packet.data++;
00054             } else {
00055                 send_packet.data = 0;
00056             }
00057             // Send message
00058             ret = TCP_send(&send_packet, sizeof(send_packet), PEACH_2_IP_ADDRESS, PEACH_2_PORT);
00059             if (ret >= 0) {
00060                 printf("Send  message : %d\n", send_packet.data);
00061                 led1 = !led1;
00062             } else {
00063                 printf("send failed!!\n");
00064             }
00065             Thread::wait(1000);
00066         }
00067     } else {
00068         printf("Network Connect Error \n");
00069     }
00070 }
00071 
00072 /****** main ******/
00073 int main(void) {
00074     printf("GR-PEACH No1 sample start!!\n");
00075 
00076     /* Start TCP communicationl processing */
00077     TCPTask.start(callback(tcp_comm_task));
00078 
00079     /* Wait for the threads to finish */
00080     TCPTask.join();
00081 }
00082 
00083 // set mac address
00084 void mbed_mac_address(char *mac) {
00085     mac[0] = 0x74;
00086     mac[1] = 0x90;
00087     mac[2] = 0x50;
00088     mac[3] = 0x00;
00089     mac[4] = 0x56;
00090     mac[5] = 0xA1;
00091 }
00092 
00093 #elif USE_PEACH == 2    /* in case of PEACH No2 */
00094 Thread            TCPTask;
00095 EthernetInterface eth;
00096 TCPServer         server;
00097 DigitalOut        led2(LED2);
00098 
00099 #define CONNECT_PENDING_NUM         (2)
00100 static tcp_packet send_packet;
00101 static tcp_packet recv_packet;
00102 
00103 int TCP_send(tcp_packet* send_data, int length, const char* adress, int port) {
00104     int ret;
00105     int retry_cnt = 0;
00106     TCPSocket socket;
00107 
00108     ret = socket.open(&eth);
00109     if (ret == 0) {
00110         printf("Now connecting...\n");
00111         while (socket.connect(adress, port) < 0) {
00112             printf("Unable to connect to (%s) on port (%d)\n", adress, port);
00113             retry_cnt++;
00114             if (retry_cnt >= TCPSEND_CONN_RETRY_MAX) {
00115                 socket.close();
00116                 return -1;
00117             }
00118         }
00119         ret = socket.send((char*)send_data, length);
00120         socket.close();
00121     }
00122 
00123     return ret;
00124 }
00125 
00126 static int TCP_receive(tcp_packet* recv_data, int length) {
00127     int ret;
00128     TCPSocket client;
00129 
00130     ret = server.accept(&client);
00131     if (ret == 0) {
00132         client.set_blocking(false);
00133         client.set_timeout(1500); // Timeout after 1.5s(default)
00134 
00135         ret = client.recv((char*)recv_data, length);
00136         client.close();
00137     }
00138 
00139     return ret;
00140 }
00141 
00142 void tcp_comm_task(void) {
00143     int ret;
00144 
00145     memset(&send_packet, 0, sizeof(send_packet));
00146     memset(&recv_packet, 0, sizeof(recv_packet));
00147     eth.set_network(PEACH_2_IP_ADDRESS, SUB_NET_MASK, DEFAULT_GATEWAY);
00148     ret = eth.connect();
00149     if (ret == 0) {
00150         printf("IP Address is %s\n", eth.get_ip_address());
00151 
00152         server.open(&eth);
00153         server.bind(PEACH_2_PORT);
00154         server.listen(CONNECT_PENDING_NUM);   // number of pending connections
00155         while (1) {
00156             // Receive message
00157             ret = TCP_receive(&recv_packet, sizeof(recv_packet));
00158             if (ret >= 0) {
00159                 printf("Received message : %d\n", recv_packet.data);
00160                 // Send message
00161                 memcpy(&send_packet, &recv_packet, sizeof(send_packet));
00162                 ret = TCP_send(&send_packet, sizeof(send_packet), PEACH_3_IP_ADDRESS, PEACH_3_PORT);
00163                 if (ret >= 0) {
00164                     printf("Send  message : %d\n", send_packet.data);
00165                     led2 = !led2;
00166                 } else {
00167                     printf("send failed!!\n");
00168                 }
00169             } else {
00170                 printf("receive failed!!\n");
00171             }
00172             Thread::wait(100);
00173         }
00174     } else {
00175         printf("Network Connect Error \n");
00176     }
00177 }
00178 
00179 /****** main ******/
00180 int main(void) {
00181     printf("GR-PEACH No2 sample start!!\n");
00182 
00183     /* Start TCP communicationl processing */
00184     TCPTask.start(callback(tcp_comm_task));
00185 
00186     /* Wait for the threads to finish */
00187     TCPTask.join();
00188 }
00189 
00190 // set mac address
00191 void mbed_mac_address(char *mac) {
00192     mac[0] = 0x74;
00193     mac[1] = 0x90;
00194     mac[2] = 0x50;
00195     mac[3] = 0x00;
00196     mac[4] = 0x56;
00197     mac[5] = 0xA2;
00198 }
00199 
00200 #elif USE_PEACH == 3    /* in case of PEACH No3 */
00201 Thread            TCPTask;
00202 EthernetInterface eth;
00203 TCPServer         server;
00204 DigitalOut        led3(LED3);
00205 
00206 #define CONNECT_PENDING_NUM         (2)
00207 static tcp_packet recv_packet;
00208 
00209 static int TCP_receive(tcp_packet* recv_data, int length) {
00210     int ret;
00211     TCPSocket client;
00212 
00213     ret = server.accept(&client);
00214     if (ret == 0) {
00215         client.set_blocking(false);
00216         client.set_timeout(1500); // Timeout after 1.5s(default)
00217 
00218         ret = client.recv((char*)recv_data, length);
00219         client.close();
00220     }
00221 
00222     return ret;
00223 }
00224 
00225 void tcp_comm_task(void) {
00226     int ret;
00227 
00228     memset(&recv_packet, 0, sizeof(recv_packet));
00229     eth.set_network(PEACH_3_IP_ADDRESS, SUB_NET_MASK, DEFAULT_GATEWAY);
00230     ret = eth.connect();
00231     if (ret == 0) {
00232         printf("IP Address is %s\n", eth.get_ip_address());
00233 
00234         server.open(&eth);
00235         server.bind(PEACH_3_PORT);
00236         server.listen(CONNECT_PENDING_NUM);   // number of pending connections
00237         while (1) {
00238             // Receive message
00239             ret = TCP_receive(&recv_packet, sizeof(recv_packet));
00240             if (ret >= 0) {
00241                 printf("Received message : %d\n", recv_packet.data);
00242                 led3 = !led3;
00243             } else {
00244                 printf("receive failed!!\n");
00245             }
00246             Thread::wait(100);
00247         }
00248     } else {
00249         printf("Network Connect Error \n");
00250     }
00251 }
00252 
00253 /****** main ******/
00254 int main(void) {
00255     printf("GR-PEACH No3 sample start!!\n");
00256 
00257     /* Start TCP communicationl processing */
00258     TCPTask.start(callback(tcp_comm_task));
00259 
00260     /* Wait for the threads to finish */
00261     TCPTask.join();
00262 }
00263 
00264 // set mac address
00265 void mbed_mac_address(char *mac) {
00266     mac[0] = 0x74;
00267     mac[1] = 0x90;
00268     mac[2] = 0x50;
00269     mac[3] = 0x00;
00270     mac[4] = 0x56;
00271     mac[5] = 0xA3;
00272 }
00273 
00274 #endif