Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DataStorage NetworkManager IR_Manager WheelManager RestAPI_Manager
main.cpp
00001 00002 #if !FEATURE_LWIP 00003 #error[NOT_SUPPORTED] LWIP not supported for this target 00004 #endif 00005 00006 #include "module_for_all.h" 00007 00008 #define PORT 8080 00009 00010 #define IP1 "192.168.20.101" 00011 #define IP2 "192.168.20.102" 00012 #define IP3 "192.168.20.103" 00013 #define IP4 "192.168.20.104" 00014 #define GATEWAY "192.168.20.1" 00015 #define MASK "255.255.255.0" 00016 00017 const char *ip = 0; 00018 const char *mac = 0; 00019 00020 DataStorage ds_manager(""); 00021 IR_Manager ir_manager(""); 00022 WheelManager wheel_manager(""); 00023 00024 NetworkManager *net_manager = new NetworkManager(IP1, GATEWAY, MASK, PORT); 00025 RestAPI_Manager *restAPI_manager = new RestAPI_Manager(&ds_manager, &ir_manager, &wheel_manager, IP1, "1"); 00026 00027 //NetworkManager net_manager(); 00028 //RestAPI_Manager restAPI_manager(); 00029 00030 void checkServer() 00031 { 00032 //TCPSocket socket; 00033 00034 printf("checkServer Start...\r\n"); 00035 00036 ir_manager.digitalTest(); 00037 ir_manager.analogTest(); 00038 00039 // Open a socket on the network interface, and create a TCP connection to www.arm.com 00040 net_manager->clt_sock.open(&(net_manager->eth)); 00041 net_manager->clt_sock.connect("192.168.20.1", 81); 00042 00043 // Send a simple http request 00044 //char sbuffer[] = "GET /iamok/ HTTP/1.1\r\nHost: 192.168.20.1\r\n\r\n"; 00045 std::string sbuffer_str = "GET /iamok/ HTTP/1.1\r\nHost: 192.168.20.1\r\n\r\n"; 00046 char *sbuffer = new char[sbuffer_str.length() + 1]; 00047 00048 //tmpString = stringToChar(tmpResponse); 00049 stringToChar(sbuffer, sbuffer_str); 00050 00051 //clt_sock.send(tmpString, strlen(tmpString)); 00052 00053 int scount = net_manager->clt_sock.send(sbuffer, strlen(sbuffer)); 00054 printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n") - sbuffer, sbuffer); 00055 00056 if (scount >= 0) 00057 { 00058 // Recieve a simple http response and print out the response line 00059 char *rbuffer = new char[1024]; 00060 //char* buf = new char[1024]; 00061 //int rcount = net_manager->clt_sock.recv(rbuffer, sizeof rbuffer); 00062 int rcount = net_manager->clt_sock.recv(rbuffer, 100); 00063 printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n") - rbuffer, rbuffer); 00064 delete[] rbuffer; 00065 DataStorage::isNoConnectServer = false; 00066 } 00067 else 00068 { 00069 DataStorage::set_PWM_status(1, 0, 0); 00070 DataStorage::set_PWM_status(2, 0, 0); 00071 DataStorage::set_PWM_status(3, 0, 0); 00072 DataStorage::set_PWM_status(4, 0, 0); 00073 00074 for (int ii = 0; ii < 5; ii++) 00075 { 00076 DataStorage::WM_PWM[ii][0] = 1; 00077 DataStorage::WM_PWM[ii][1] = 0; 00078 } 00079 00080 /*(DataStorage::_WM_PWM_out01).period(DataStorage::WM_PWM[0][0]); 00081 (DataStorage::_WM_PWM_out02).period(DataStorage::WM_PWM[1][0]); 00082 (DataStorage::_WM_PWM_out03).period(DataStorage::WM_PWM[2][0]); 00083 (DataStorage::_WM_PWM_out04).period(DataStorage::WM_PWM[3][0]); 00084 00085 (DataStorage::_WM_PWM_out01).write(DataStorage::WM_PWM[0][1]); 00086 (DataStorage::_WM_PWM_out02).write(DataStorage::WM_PWM[1][1]); 00087 (DataStorage::_WM_PWM_out03).write(DataStorage::WM_PWM[2][1]); 00088 (DataStorage::_WM_PWM_out04).write(DataStorage::WM_PWM[3][1]);*/ 00089 for (int ii = 0; ii < 4; ii++) 00090 ds_manager.set_PWM_period(ii, 1); 00091 for (int ii = 0; ii < 4; ii++) 00092 ds_manager.set_PWM_write(ii, 0); 00093 DataStorage::isNoConnectServer = true; 00094 } 00095 00096 // Close the socket to return its memory and bring down the network interface 00097 net_manager->clt_sock.close(); 00098 00099 delete[] sbuffer; 00100 printf("checkServer Over...\r\n"); 00101 } 00102 00103 void checkServerLoop() 00104 { 00105 while (true) 00106 { 00107 checkServer(); 00108 } 00109 } 00110 00111 void checkServerThread() 00112 { 00113 Thread thread_connect; 00114 thread_connect.start(checkServerLoop); 00115 } 00116 00117 bool EventLoop() 00118 { 00119 while (true) 00120 { 00121 printf("$$ EventLoop New\r\n"); 00122 00123 //Thread threadMain01; 00124 //restAPI_manager.changeThread(&threadMain01); 00125 00126 //printf("$$ EventLoop Change Thread\r\n"); 00127 00128 LED_NetWait(); 00129 00130 printf("$$ EventLoop Waiting\r\n"); 00131 00132 net_manager->AcceptEvent(); 00133 //printf("$$ EventLoop run Response\r\n"); 00134 if (DataStorage::isAccept) 00135 { 00136 LED_NetProcess(); 00137 DataStorage::http_method = net_manager->getHttpMethod(); 00138 DataStorage::api_command = net_manager->getApiCommand(); 00139 DataStorage::response = restAPI_manager->runCommand(DataStorage::http_method, DataStorage::api_command); 00140 net_manager->SendResponseEvent(DataStorage::response); 00141 DataStorage::noAcceptCount = 0; 00142 } 00143 else 00144 { 00145 DataStorage::noAcceptCount += 1; 00146 if (DataStorage::noAcceptCount == 20) 00147 { 00148 DataStorage::noAcceptCount = 0; 00149 if (!DataStorage::isTestMode) 00150 { 00151 LED_NetProcess_checkServer(); 00152 checkServer(); 00153 } 00154 } 00155 } 00156 00157 //DataStorage::http_method = net_manager->getHttpMethod(); 00158 //DataStorage::api_command = net_manager->getApiCommand(); 00159 00160 //DataStorage::response = restAPI_manager->runCommand(DataStorage::http_method, DataStorage::api_command); 00161 //net_manager->SendResponseEvent(DataStorage::response); 00162 00163 DataStorage::isAccept = false; 00164 printf("$$ EventLoop Finish\r\n"); 00165 } 00166 00167 //printf("$$ EventLoop ALL Finish\r\n"); 00168 return false; 00169 } 00170 00171 void checkIP_Mode() 00172 { 00173 DigitalIn IP_Select1(PD_7); 00174 DigitalIn IP_Select2(PD_6); 00175 00176 DigitalIn Test_Mode(PC_14); 00177 00178 DataStorage::ipSelect = 1; 00179 00180 if (IP_Select1.read() == 1) 00181 { 00182 DataStorage::ipSelect += 1; 00183 } 00184 00185 if (IP_Select2.read() == 1) 00186 { 00187 DataStorage::ipSelect += 2; 00188 } 00189 00190 switch (DataStorage::ipSelect) 00191 { 00192 case 1: 00193 DataStorage::ipChar = IP1; 00194 break; 00195 case 2: 00196 DataStorage::ipChar = IP2; 00197 break; 00198 case 3: 00199 DataStorage::ipChar = IP3; 00200 break; 00201 case 4: 00202 DataStorage::ipChar = IP4; 00203 break; 00204 } 00205 00206 DataStorage::ipStr = intToString(DataStorage::ipSelect); 00207 00208 net_manager->SetConfig(DataStorage::ipChar, GATEWAY, MASK, PORT); 00209 restAPI_manager->SetConfig(DataStorage::ipChar, DataStorage::ipStr); 00210 00211 printf("mbed %d Start\r\n", DataStorage::ipSelect); 00212 00213 if (Test_Mode.read() == 1) 00214 { 00215 DataStorage::isTestMode = true; 00216 printf("mbed %d mode Test\r\n", DataStorage::ipSelect); 00217 } 00218 else 00219 { 00220 DataStorage::isTestMode = false; 00221 printf("mbed %d mode Normal\r\n", DataStorage::ipSelect); 00222 } 00223 } 00224 00225 int netInit() 00226 { 00227 net_manager->ServerInit(); 00228 printf("ServerInit\r\n"); 00229 00230 if (net_manager->OpenServer()) 00231 return -1; 00232 printf("OpenServer\r\n"); 00233 00234 if (net_manager->BindPort()) 00235 return -1; 00236 printf("BindPort\r\n"); 00237 00238 if (net_manager->ListenConnections()) 00239 return -1; 00240 printf("ListenConnections\r\n"); 00241 00242 return 0; 00243 } 00244 00245 void start() 00246 { 00247 printf("mbed Power Start\r\n"); 00248 LED_PowerStart(); 00249 00250 checkIP_Mode(); 00251 wait(0.5); 00252 netInit(); 00253 } 00254 /* Main function */ 00255 int main() 00256 { 00257 start(); 00258 00259 if (EventLoop()) 00260 { 00261 return 0; 00262 } 00263 }
Generated on Fri Jul 15 2022 12:26:47 by
1.7.2