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.
Fork of httpServer by
FsHandler.cpp
00001 /* FsHandler.cpp */ 00002 #include "mbed.h" 00003 #include "FsHandler.h" 00004 //#define DEBUG 00005 #include "hl_debug.h" 00006 #include "stdlib.h" 00007 00008 DigitalOut led_red(LED1); 00009 DigitalOut led_green(LED2); 00010 DigitalOut led_blue(LED3); 00011 00012 void SNTP_Connect(void); 00013 void time_set(void); 00014 void time_update(char buffer[]); 00015 00016 extern int hour_r, min_r,hm_r, hm_n; 00017 extern int flag_t; 00018 extern double duty; 00019 00020 PwmOut myservo(P10); 00021 datetime ntptime; 00022 struct tm timeinfo; 00023 00024 uint8_t update_mode = 0; 00025 uint8_t Set_update[3] = {18, 00, 00}; //Hour,Minute,Second 00026 00027 //ymd_buffer[0]~[3] : Year 00028 //ymd_buffer[4]~[5] : Month 00029 //ymd_buffer[6]~[7] : Day 00030 extern char ymd_buffer[8]; 00031 //hms_buffer[0]~[1] : Hour 00032 //hms_buffer[2]~[3] : Minute 00033 //hms_buffer[4]~[5] : Second 00034 extern char hms_buffer[6]; 00035 00036 static int matchstrings(const char* one, const char* two) 00037 { 00038 int m = 0; 00039 00040 for (m = 0; m < min(strlen(one), strlen(two)) ; m++) { 00041 if (one[m] != two[m]) 00042 return m; 00043 } 00044 return m; 00045 } 00046 00047 std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap; 00048 00049 HTTPFsRequestHandler::HTTPFsRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp) 00050 : HTTPRequestHandler(Msg, Tcp) 00051 { 00052 m_rootPath = rootPath; 00053 m_localPath = localPath; 00054 00055 string myPath = m_rootPath + m_localPath; 00056 00057 // Now replace the virtual root path with a mounted device path 00058 std::map<const char*, const char*>::iterator it; 00059 const char *bestMatch = NULL; 00060 const char *bestMatchExchange = NULL; 00061 int match_ind = -1; 00062 for (it = m_fsMap.begin() ; it != m_fsMap.end() ; it++) { 00063 // find best match (if the given logical path is containted in the root 00064 int s = matchstrings(myPath.c_str(), it->second); 00065 INFO("Matching Root %s with handler %s results in %d identical characters\n", myPath.c_str(), it->second, s); 00066 if ((s == strlen(it->second)) && (s > match_ind)) { 00067 match_ind = s; 00068 bestMatch = it->first; 00069 bestMatchExchange = it->second; 00070 } 00071 } 00072 00073 if (bestMatch != NULL) { 00074 m_rootPath = bestMatch; 00075 m_localPath = string(myPath).substr(strlen(bestMatchExchange)); 00076 } 00077 00078 handleRequest(); 00079 } 00080 00081 HTTPFsRequestHandler::~HTTPFsRequestHandler() 00082 { 00083 } 00084 00085 std::map<int, EthernetInterface*> HTTPFsRequestHandler::m_eth_list; 00086 00087 int HTTPFsRequestHandler::handleGetRequest() 00088 { 00089 HTTPHeaders headers; 00090 EthernetInterface test_eth; 00091 int retval = 0; //success 00092 00093 if( std::string::npos != msg.uri.find("get_netinfo.cgi") ) 00094 {}/* 00095 char buf[256]; 00096 00097 sprintf(buf, "NetinfoCallback({\"mac\":\"%s\",\"ip\":\"%s\",\"sn\":\"%s\",\"gw\":\"%s\",\"temp\":\"%s\"});" 00098 ,m_eth_list[0]->getMACAddress(),m_eth_list[0]->getIPAddress(),m_eth_list[0]->getNetworkMask(),m_eth_list[0]->getGateway()); 00099 00100 tcp.send(buf, strlen(buf)); 00101 } 00102 00103 else if( std::string::npos != msg.uri.find("get_ain.cgi") ) 00104 { 00105 char buf[256]; 00106 00107 //sprintf(buf, "AinCallback({\"ain_v0\":\"%.3f\",\"ain_v1\":\"%.3f\"});",ain0.read()*100, ain1.read()*100); 00108 00109 tcp.send(buf, strlen(buf)); 00110 }*/ 00111 00112 else //read html pages 00113 { 00114 if (m_localPath.length() > 4) 00115 getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str()); 00116 else 00117 getStandardHeaders(headers); 00118 00119 INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str()); 00120 00121 std::string reqPath; 00122 00123 // Check if we received a directory with the local bath 00124 if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) { 00125 // yes, we shall append the default page name 00126 m_localPath += "index.html"; 00127 } 00128 00129 reqPath = m_rootPath + m_localPath; 00130 00131 INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str()); 00132 00133 FILE *fp = fopen(reqPath.c_str(), "r"); 00134 if (fp != NULL) { 00135 char * pBuffer = NULL; 00136 int sz = 8192; 00137 while( pBuffer == NULL) { 00138 sz /= 2; 00139 pBuffer = (char*)malloc(sz); 00140 if (sz < 128) 00141 error ("OutOfMemory"); 00142 } 00143 00144 // File was found and can be returned 00145 00146 // first determine the size 00147 fseek(fp, 0, SEEK_END); 00148 long size = ftell(fp); 00149 fseek(fp, 0, SEEK_SET); 00150 00151 startResponse(200, size); 00152 while(!feof(fp) && !ferror(fp)) { 00153 int cnt = fread(pBuffer, 1, sz , fp); 00154 if (cnt < 0) 00155 cnt = 0; 00156 processResponse(cnt, pBuffer); 00157 } 00158 delete pBuffer; 00159 endResponse(); 00160 fclose(fp); 00161 } 00162 else { 00163 retval = 404; 00164 ERR("Requested file was not found !"); 00165 } 00166 } 00167 00168 return retval; 00169 } 00170 00171 int HTTPFsRequestHandler::handlePostRequest() 00172 { 00173 char ch; 00174 myservo.period_ms(20); 00175 int pin = 0; 00176 00177 if( std::string::npos != msg.uri.find("set_dio.cgi") ) 00178 { 00179 pin = get_http_param_value("pin"); 00180 if(pin == 8) 00181 { 00182 led_red = get_http_param_value("val"); 00183 } 00184 else if(pin == 9) 00185 { 00186 led_green = get_http_param_value("val"); 00187 } 00188 else if(pin == 5) 00189 { 00190 led_blue = get_http_param_value("val"); 00191 } 00192 else 00193 { 00194 WARN("Wrong pin number"); 00195 } 00196 00197 return 0; 00198 } 00199 else if( std::string::npos != msg.uri.find("set_dio2.cgi") ) 00200 { 00201 ch = get_http_param_value("pin"); 00202 printf("ch : %d \r\n",ch); 00203 00204 if(ch == 0) 00205 { 00206 duty = 0.05; 00207 myservo = duty; 00208 printf("duty : %lf\r\n", duty); 00209 } 00210 else if(ch == 1) 00211 { 00212 duty = 1; 00213 myservo = duty; 00214 printf("duty : %lf\r\n", duty); 00215 //myservo.pulsewidth_ms(1.0); 00216 } 00217 else if(ch == 2) 00218 { 00219 duty = 0.2; 00220 myservo = duty; 00221 printf("duty : %lf\r\n", duty); 00222 //myservo.pulsewidth_ms(1.0); 00223 } 00224 else if(ch == 3) 00225 { 00226 duty = 0.2; 00227 myservo = duty; 00228 printf("duty : %lf\r\n", duty); 00229 wait(5); 00230 duty = 1; 00231 myservo = duty; 00232 printf("duty : %lf\r\n", duty); 00233 } 00234 } 00235 else if( std::string::npos != msg.uri.find("set_time.cgi") ) 00236 { 00237 hour_r = get_http_param_value("hour"); 00238 min_r = get_http_param_value("min"); 00239 00240 printf("hour : %d, min : %d\r\n", hour_r, min_r); 00241 00242 SNTP_Connect(); 00243 time_set(); 00244 00245 time_t seconds = time(NULL); 00246 00247 00248 strftime(ymd_buffer, 8, "%Y%m%d%\n\r", localtime(&seconds)); 00249 00250 if(hms_buffer[4] == '0' && hms_buffer[5] == '0') 00251 { 00252 time_update(hms_buffer); 00253 printf("update HMS : %s\r\n",hms_buffer); 00254 } 00255 00256 hm_r = (hour_r*100) + min_r; 00257 hm_n = atoi(hms_buffer); 00258 hm_n = hm_n / 100; 00259 printf("HMr : %d\r\n",hm_r); 00260 printf("HMn : %d\r\n",hm_n); 00261 00262 flag_t = 1; 00263 } 00264 else 00265 { 00266 return 404; 00267 } 00268 } 00269 00270 int HTTPFsRequestHandler::handlePutRequest() 00271 { 00272 return 404; 00273 } 00274 00275 uint32_t HTTPFsRequestHandler::get_http_param_value(char* param_name) 00276 { 00277 uint8_t * name = 0; 00278 uint8_t * pos2; 00279 uint16_t len = 0; 00280 char value[10]; 00281 uint32_t ret = 0; 00282 00283 //msg.attri 00284 if((name = (uint8_t *)strstr(msg.attri, param_name))) 00285 { 00286 name += strlen(param_name) + 1; 00287 pos2 = (uint8_t*)strstr((char*)name, "&"); 00288 if(!pos2) 00289 { 00290 pos2 = name + strlen((char*)name); 00291 } 00292 len = pos2 - name; 00293 00294 if(len) 00295 { 00296 strncpy(value, (char*)name, len); 00297 ret = atoi(value); 00298 } 00299 } 00300 return ret; 00301 } 00302 00303 void SNTP_Connect(void) { 00304 printf("Getting time information by using NTP...\r\n"); 00305 00306 SNTPClient sntp("time.nist.gov", 40); // timezone: Korea, Republic of 00307 sntp.connect(); 00308 if(sntp.getTime(&ntptime) == true) { 00309 printf("%d-%d-%d, %02d:%02d:%02d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss); 00310 printf("Completed Get and Set Time\r\n\r\n"); 00311 } 00312 else { 00313 while(sntp.getTime(&ntptime) == true) { 00314 printf("%d-%d-%d, %02d:%02d:%02d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss); 00315 printf("Completed Get and Set Time\r\n\r\n"); 00316 break; 00317 } 00318 } 00319 sntp.close(); 00320 } 00321 00322 void time_set(void) { 00323 timeinfo.tm_mon = ntptime.mo-1; 00324 timeinfo.tm_mday = ntptime.dd; 00325 timeinfo.tm_hour = ntptime.hh; 00326 timeinfo.tm_min = ntptime.mm; 00327 timeinfo.tm_sec = ntptime.ss; 00328 timeinfo.tm_year = ntptime.yy-1900; 00329 time_t t =mktime(&timeinfo); 00330 set_time(t); 00331 t = time(NULL); 00332 } 00333 00334 void time_update(char buffer[]) { 00335 uint8_t h_buffer = ((buffer[0]-48)*10) + (buffer[1]-48); 00336 uint8_t m_buffer = ((buffer[2]-48)*10) + (buffer[3]-48); 00337 uint8_t s_buffer = ((buffer[4]-48)*10) + (buffer[5]-48); 00338 00339 if(update_mode == 0){ 00340 if(h_buffer == Set_update[0] && m_buffer == Set_update[1] && s_buffer == Set_update[2]){ 00341 SNTP_Connect(); 00342 time_set(); 00343 update_mode = 1; 00344 printf("Time Update Completed.\n\r\n\r"); 00345 } 00346 } 00347 else if(update_mode == 1){ 00348 if(!(h_buffer == Set_update[0] && m_buffer == Set_update[1] && s_buffer == Set_update[2])){ 00349 update_mode = 0; 00350 } 00351 } 00352 }
Generated on Wed Jul 13 2022 00:31:04 by
1.7.2
