petfeeder st ver
Fork of httpServer_ntpsetting by
Handler/FsHandler.cpp@6:b33c1a450937, 2016-07-07 (annotated)
- Committer:
- justinkim
- Date:
- Thu Jul 07 23:53:18 2016 +0000
- Revision:
- 6:b33c1a450937
- Parent:
- 5:3c24e937da61
- Child:
- 7:2a38bd4be00f
add function about timer alert
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hjjeon | 0:e59cc54df17c | 1 | /* FsHandler.cpp */ |
hjjeon | 0:e59cc54df17c | 2 | #include "mbed.h" |
hjjeon | 0:e59cc54df17c | 3 | #include "FsHandler.h" |
hjjeon | 0:e59cc54df17c | 4 | //#define DEBUG |
hjjeon | 0:e59cc54df17c | 5 | #include "hl_debug.h" |
justinkim | 6:b33c1a450937 | 6 | #include "stdlib.h" |
hjjeon | 0:e59cc54df17c | 7 | |
hjjeon | 0:e59cc54df17c | 8 | DigitalOut led_red(LED1); |
hjjeon | 0:e59cc54df17c | 9 | DigitalOut led_green(LED2); |
hjjeon | 0:e59cc54df17c | 10 | DigitalOut led_blue(LED3); |
hjjeon | 0:e59cc54df17c | 11 | |
justinkim | 6:b33c1a450937 | 12 | void SNTP_Connect(void); |
justinkim | 6:b33c1a450937 | 13 | void time_set(void); |
justinkim | 6:b33c1a450937 | 14 | void time_update(char buffer[]); |
justinkim | 6:b33c1a450937 | 15 | |
justinkim | 6:b33c1a450937 | 16 | extern int hour_r, min_r,hm_r, hm_n; |
justinkim | 6:b33c1a450937 | 17 | extern int flag_t; |
justinkim | 6:b33c1a450937 | 18 | extern double duty; |
justinkim | 6:b33c1a450937 | 19 | |
justinkim | 6:b33c1a450937 | 20 | PwmOut myservo(P10); |
justinkim | 6:b33c1a450937 | 21 | datetime ntptime; |
justinkim | 6:b33c1a450937 | 22 | struct tm timeinfo; |
justinkim | 6:b33c1a450937 | 23 | |
justinkim | 6:b33c1a450937 | 24 | uint8_t update_mode = 0; |
justinkim | 6:b33c1a450937 | 25 | uint8_t Set_update[3] = {18, 00, 00}; //Hour,Minute,Second |
justinkim | 6:b33c1a450937 | 26 | |
justinkim | 6:b33c1a450937 | 27 | //ymd_buffer[0]~[3] : Year |
justinkim | 6:b33c1a450937 | 28 | //ymd_buffer[4]~[5] : Month |
justinkim | 6:b33c1a450937 | 29 | //ymd_buffer[6]~[7] : Day |
justinkim | 6:b33c1a450937 | 30 | extern char ymd_buffer[8]; |
justinkim | 6:b33c1a450937 | 31 | //hms_buffer[0]~[1] : Hour |
justinkim | 6:b33c1a450937 | 32 | //hms_buffer[2]~[3] : Minute |
justinkim | 6:b33c1a450937 | 33 | //hms_buffer[4]~[5] : Second |
justinkim | 6:b33c1a450937 | 34 | extern char hms_buffer[6]; |
hjjeon | 0:e59cc54df17c | 35 | |
hjjeon | 0:e59cc54df17c | 36 | static int matchstrings(const char* one, const char* two) |
hjjeon | 0:e59cc54df17c | 37 | { |
hjjeon | 0:e59cc54df17c | 38 | int m = 0; |
hjjeon | 0:e59cc54df17c | 39 | |
hjjeon | 0:e59cc54df17c | 40 | for (m = 0; m < min(strlen(one), strlen(two)) ; m++) { |
hjjeon | 0:e59cc54df17c | 41 | if (one[m] != two[m]) |
hjjeon | 0:e59cc54df17c | 42 | return m; |
hjjeon | 0:e59cc54df17c | 43 | } |
hjjeon | 0:e59cc54df17c | 44 | return m; |
hjjeon | 0:e59cc54df17c | 45 | } |
hjjeon | 0:e59cc54df17c | 46 | |
hjjeon | 0:e59cc54df17c | 47 | std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap; |
hjjeon | 0:e59cc54df17c | 48 | |
hjjeon | 0:e59cc54df17c | 49 | HTTPFsRequestHandler::HTTPFsRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp) |
hjjeon | 0:e59cc54df17c | 50 | : HTTPRequestHandler(Msg, Tcp) |
hjjeon | 0:e59cc54df17c | 51 | { |
hjjeon | 0:e59cc54df17c | 52 | m_rootPath = rootPath; |
hjjeon | 0:e59cc54df17c | 53 | m_localPath = localPath; |
hjjeon | 0:e59cc54df17c | 54 | |
hjjeon | 0:e59cc54df17c | 55 | string myPath = m_rootPath + m_localPath; |
hjjeon | 0:e59cc54df17c | 56 | |
hjjeon | 0:e59cc54df17c | 57 | // Now replace the virtual root path with a mounted device path |
hjjeon | 0:e59cc54df17c | 58 | std::map<const char*, const char*>::iterator it; |
hjjeon | 0:e59cc54df17c | 59 | const char *bestMatch = NULL; |
hjjeon | 0:e59cc54df17c | 60 | const char *bestMatchExchange = NULL; |
hjjeon | 0:e59cc54df17c | 61 | int match_ind = -1; |
hjjeon | 0:e59cc54df17c | 62 | for (it = m_fsMap.begin() ; it != m_fsMap.end() ; it++) { |
hjjeon | 0:e59cc54df17c | 63 | // find best match (if the given logical path is containted in the root |
hjjeon | 0:e59cc54df17c | 64 | int s = matchstrings(myPath.c_str(), it->second); |
hjjeon | 0:e59cc54df17c | 65 | INFO("Matching Root %s with handler %s results in %d identical characters\n", myPath.c_str(), it->second, s); |
hjjeon | 0:e59cc54df17c | 66 | if ((s == strlen(it->second)) && (s > match_ind)) { |
hjjeon | 0:e59cc54df17c | 67 | match_ind = s; |
hjjeon | 0:e59cc54df17c | 68 | bestMatch = it->first; |
hjjeon | 0:e59cc54df17c | 69 | bestMatchExchange = it->second; |
hjjeon | 0:e59cc54df17c | 70 | } |
hjjeon | 0:e59cc54df17c | 71 | } |
hjjeon | 0:e59cc54df17c | 72 | |
hjjeon | 0:e59cc54df17c | 73 | if (bestMatch != NULL) { |
hjjeon | 0:e59cc54df17c | 74 | m_rootPath = bestMatch; |
hjjeon | 0:e59cc54df17c | 75 | m_localPath = string(myPath).substr(strlen(bestMatchExchange)); |
hjjeon | 0:e59cc54df17c | 76 | } |
hjjeon | 0:e59cc54df17c | 77 | |
hjjeon | 0:e59cc54df17c | 78 | handleRequest(); |
hjjeon | 0:e59cc54df17c | 79 | } |
hjjeon | 0:e59cc54df17c | 80 | |
hjjeon | 0:e59cc54df17c | 81 | HTTPFsRequestHandler::~HTTPFsRequestHandler() |
hjjeon | 0:e59cc54df17c | 82 | { |
hjjeon | 0:e59cc54df17c | 83 | } |
hjjeon | 0:e59cc54df17c | 84 | |
justinkim | 2:dd293a10a772 | 85 | std::map<int, EthernetInterface*> HTTPFsRequestHandler::m_eth_list; |
justinkim | 2:dd293a10a772 | 86 | |
hjjeon | 0:e59cc54df17c | 87 | int HTTPFsRequestHandler::handleGetRequest() |
hjjeon | 0:e59cc54df17c | 88 | { |
hjjeon | 0:e59cc54df17c | 89 | HTTPHeaders headers; |
justinkim | 2:dd293a10a772 | 90 | EthernetInterface test_eth; |
hjjeon | 0:e59cc54df17c | 91 | int retval = 0; //success |
hjjeon | 0:e59cc54df17c | 92 | |
justinkim | 2:dd293a10a772 | 93 | if( std::string::npos != msg.uri.find("get_netinfo.cgi") ) |
justinkim | 6:b33c1a450937 | 94 | {}/* |
justinkim | 2:dd293a10a772 | 95 | char buf[256]; |
hjjeon | 4:2903435e3811 | 96 | |
hjjeon | 4:2903435e3811 | 97 | sprintf(buf, "NetinfoCallback({\"mac\":\"%s\",\"ip\":\"%s\",\"sn\":\"%s\",\"gw\":\"%s\",\"temp\":\"%s\"});" |
justinkim | 2:dd293a10a772 | 98 | ,m_eth_list[0]->getMACAddress(),m_eth_list[0]->getIPAddress(),m_eth_list[0]->getNetworkMask(),m_eth_list[0]->getGateway()); |
justinkim | 2:dd293a10a772 | 99 | |
justinkim | 2:dd293a10a772 | 100 | tcp.send(buf, strlen(buf)); |
justinkim | 2:dd293a10a772 | 101 | } |
justinkim | 2:dd293a10a772 | 102 | |
justinkim | 2:dd293a10a772 | 103 | else if( std::string::npos != msg.uri.find("get_ain.cgi") ) |
justinkim | 2:dd293a10a772 | 104 | { |
justinkim | 2:dd293a10a772 | 105 | char buf[256]; |
hjjeon | 0:e59cc54df17c | 106 | |
justinkim | 5:3c24e937da61 | 107 | //sprintf(buf, "AinCallback({\"ain_v0\":\"%.3f\",\"ain_v1\":\"%.3f\"});",ain0.read()*100, ain1.read()*100); |
justinkim | 2:dd293a10a772 | 108 | |
justinkim | 2:dd293a10a772 | 109 | tcp.send(buf, strlen(buf)); |
justinkim | 6:b33c1a450937 | 110 | }*/ |
justinkim | 2:dd293a10a772 | 111 | |
hjjeon | 0:e59cc54df17c | 112 | else //read html pages |
hjjeon | 0:e59cc54df17c | 113 | { |
hjjeon | 0:e59cc54df17c | 114 | if (m_localPath.length() > 4) |
hjjeon | 0:e59cc54df17c | 115 | getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str()); |
hjjeon | 0:e59cc54df17c | 116 | else |
hjjeon | 0:e59cc54df17c | 117 | getStandardHeaders(headers); |
hjjeon | 0:e59cc54df17c | 118 | |
hjjeon | 0:e59cc54df17c | 119 | INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str()); |
hjjeon | 0:e59cc54df17c | 120 | |
hjjeon | 0:e59cc54df17c | 121 | std::string reqPath; |
hjjeon | 0:e59cc54df17c | 122 | |
hjjeon | 0:e59cc54df17c | 123 | // Check if we received a directory with the local bath |
hjjeon | 0:e59cc54df17c | 124 | if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) { |
hjjeon | 0:e59cc54df17c | 125 | // yes, we shall append the default page name |
hjjeon | 0:e59cc54df17c | 126 | m_localPath += "index.html"; |
hjjeon | 0:e59cc54df17c | 127 | } |
hjjeon | 0:e59cc54df17c | 128 | |
hjjeon | 0:e59cc54df17c | 129 | reqPath = m_rootPath + m_localPath; |
hjjeon | 0:e59cc54df17c | 130 | |
hjjeon | 0:e59cc54df17c | 131 | INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str()); |
hjjeon | 0:e59cc54df17c | 132 | |
hjjeon | 0:e59cc54df17c | 133 | FILE *fp = fopen(reqPath.c_str(), "r"); |
hjjeon | 0:e59cc54df17c | 134 | if (fp != NULL) { |
hjjeon | 0:e59cc54df17c | 135 | char * pBuffer = NULL; |
hjjeon | 0:e59cc54df17c | 136 | int sz = 8192; |
hjjeon | 0:e59cc54df17c | 137 | while( pBuffer == NULL) { |
hjjeon | 0:e59cc54df17c | 138 | sz /= 2; |
hjjeon | 0:e59cc54df17c | 139 | pBuffer = (char*)malloc(sz); |
hjjeon | 0:e59cc54df17c | 140 | if (sz < 128) |
hjjeon | 0:e59cc54df17c | 141 | error ("OutOfMemory"); |
hjjeon | 0:e59cc54df17c | 142 | } |
hjjeon | 0:e59cc54df17c | 143 | |
hjjeon | 0:e59cc54df17c | 144 | // File was found and can be returned |
hjjeon | 0:e59cc54df17c | 145 | |
hjjeon | 0:e59cc54df17c | 146 | // first determine the size |
hjjeon | 0:e59cc54df17c | 147 | fseek(fp, 0, SEEK_END); |
hjjeon | 0:e59cc54df17c | 148 | long size = ftell(fp); |
hjjeon | 0:e59cc54df17c | 149 | fseek(fp, 0, SEEK_SET); |
hjjeon | 0:e59cc54df17c | 150 | |
hjjeon | 0:e59cc54df17c | 151 | startResponse(200, size); |
hjjeon | 0:e59cc54df17c | 152 | while(!feof(fp) && !ferror(fp)) { |
hjjeon | 0:e59cc54df17c | 153 | int cnt = fread(pBuffer, 1, sz , fp); |
hjjeon | 0:e59cc54df17c | 154 | if (cnt < 0) |
hjjeon | 0:e59cc54df17c | 155 | cnt = 0; |
hjjeon | 0:e59cc54df17c | 156 | processResponse(cnt, pBuffer); |
hjjeon | 0:e59cc54df17c | 157 | } |
hjjeon | 0:e59cc54df17c | 158 | delete pBuffer; |
hjjeon | 0:e59cc54df17c | 159 | endResponse(); |
hjjeon | 0:e59cc54df17c | 160 | fclose(fp); |
hjjeon | 0:e59cc54df17c | 161 | } |
hjjeon | 0:e59cc54df17c | 162 | else { |
hjjeon | 0:e59cc54df17c | 163 | retval = 404; |
hjjeon | 0:e59cc54df17c | 164 | ERR("Requested file was not found !"); |
hjjeon | 0:e59cc54df17c | 165 | } |
hjjeon | 0:e59cc54df17c | 166 | } |
hjjeon | 0:e59cc54df17c | 167 | |
hjjeon | 0:e59cc54df17c | 168 | return retval; |
hjjeon | 0:e59cc54df17c | 169 | } |
hjjeon | 0:e59cc54df17c | 170 | |
hjjeon | 0:e59cc54df17c | 171 | int HTTPFsRequestHandler::handlePostRequest() |
hjjeon | 0:e59cc54df17c | 172 | { |
justinkim | 6:b33c1a450937 | 173 | char ch; |
justinkim | 5:3c24e937da61 | 174 | myservo.period_ms(20); |
hjjeon | 0:e59cc54df17c | 175 | int pin = 0; |
hjjeon | 0:e59cc54df17c | 176 | |
hjjeon | 0:e59cc54df17c | 177 | if( std::string::npos != msg.uri.find("set_dio.cgi") ) |
hjjeon | 0:e59cc54df17c | 178 | { |
hjjeon | 0:e59cc54df17c | 179 | pin = get_http_param_value("pin"); |
hjjeon | 0:e59cc54df17c | 180 | if(pin == 8) |
hjjeon | 0:e59cc54df17c | 181 | { |
hjjeon | 0:e59cc54df17c | 182 | led_red = get_http_param_value("val"); |
hjjeon | 0:e59cc54df17c | 183 | } |
hjjeon | 0:e59cc54df17c | 184 | else if(pin == 9) |
hjjeon | 0:e59cc54df17c | 185 | { |
hjjeon | 0:e59cc54df17c | 186 | led_green = get_http_param_value("val"); |
hjjeon | 0:e59cc54df17c | 187 | } |
hjjeon | 0:e59cc54df17c | 188 | else if(pin == 5) |
hjjeon | 0:e59cc54df17c | 189 | { |
hjjeon | 0:e59cc54df17c | 190 | led_blue = get_http_param_value("val"); |
hjjeon | 0:e59cc54df17c | 191 | } |
hjjeon | 0:e59cc54df17c | 192 | else |
hjjeon | 0:e59cc54df17c | 193 | { |
hjjeon | 0:e59cc54df17c | 194 | WARN("Wrong pin number"); |
hjjeon | 0:e59cc54df17c | 195 | } |
hjjeon | 0:e59cc54df17c | 196 | |
hjjeon | 0:e59cc54df17c | 197 | return 0; |
justinkim | 5:3c24e937da61 | 198 | } |
justinkim | 5:3c24e937da61 | 199 | else if( std::string::npos != msg.uri.find("set_dio2.cgi") ) |
justinkim | 5:3c24e937da61 | 200 | { |
justinkim | 5:3c24e937da61 | 201 | ch = get_http_param_value("pin"); |
justinkim | 6:b33c1a450937 | 202 | printf("ch : %d \r\n",ch); |
justinkim | 5:3c24e937da61 | 203 | |
justinkim | 5:3c24e937da61 | 204 | if(ch == 0) |
justinkim | 5:3c24e937da61 | 205 | { |
justinkim | 5:3c24e937da61 | 206 | duty = 0.05; |
justinkim | 5:3c24e937da61 | 207 | myservo = duty; |
justinkim | 5:3c24e937da61 | 208 | printf("duty : %lf\r\n", duty); |
justinkim | 5:3c24e937da61 | 209 | } |
justinkim | 5:3c24e937da61 | 210 | else if(ch == 1) |
justinkim | 5:3c24e937da61 | 211 | { |
justinkim | 5:3c24e937da61 | 212 | duty = 1; |
justinkim | 5:3c24e937da61 | 213 | myservo = duty; |
justinkim | 5:3c24e937da61 | 214 | printf("duty : %lf\r\n", duty); |
justinkim | 5:3c24e937da61 | 215 | //myservo.pulsewidth_ms(1.0); |
justinkim | 5:3c24e937da61 | 216 | } |
justinkim | 5:3c24e937da61 | 217 | else if(ch == 2) |
justinkim | 5:3c24e937da61 | 218 | { |
justinkim | 5:3c24e937da61 | 219 | duty = 0.2; |
justinkim | 5:3c24e937da61 | 220 | myservo = duty; |
justinkim | 5:3c24e937da61 | 221 | printf("duty : %lf\r\n", duty); |
justinkim | 5:3c24e937da61 | 222 | //myservo.pulsewidth_ms(1.0); |
justinkim | 5:3c24e937da61 | 223 | } |
justinkim | 6:b33c1a450937 | 224 | else if(ch == 3) |
justinkim | 6:b33c1a450937 | 225 | { |
justinkim | 6:b33c1a450937 | 226 | duty = 0.2; |
justinkim | 6:b33c1a450937 | 227 | myservo = duty; |
justinkim | 6:b33c1a450937 | 228 | printf("duty : %lf\r\n", duty); |
justinkim | 6:b33c1a450937 | 229 | wait(5); |
justinkim | 6:b33c1a450937 | 230 | duty = 1; |
justinkim | 6:b33c1a450937 | 231 | myservo = duty; |
justinkim | 6:b33c1a450937 | 232 | printf("duty : %lf\r\n", duty); |
justinkim | 6:b33c1a450937 | 233 | } |
justinkim | 6:b33c1a450937 | 234 | } |
justinkim | 6:b33c1a450937 | 235 | else if( std::string::npos != msg.uri.find("set_time.cgi") ) |
justinkim | 6:b33c1a450937 | 236 | { |
justinkim | 6:b33c1a450937 | 237 | hour_r = get_http_param_value("hour"); |
justinkim | 6:b33c1a450937 | 238 | min_r = get_http_param_value("min"); |
justinkim | 6:b33c1a450937 | 239 | |
justinkim | 6:b33c1a450937 | 240 | printf("hour : %d, min : %d\r\n", hour_r, min_r); |
justinkim | 6:b33c1a450937 | 241 | |
justinkim | 6:b33c1a450937 | 242 | SNTP_Connect(); |
justinkim | 6:b33c1a450937 | 243 | time_set(); |
justinkim | 6:b33c1a450937 | 244 | |
justinkim | 6:b33c1a450937 | 245 | time_t seconds = time(NULL); |
justinkim | 6:b33c1a450937 | 246 | |
justinkim | 6:b33c1a450937 | 247 | |
justinkim | 6:b33c1a450937 | 248 | strftime(ymd_buffer, 8, "%Y%m%d%\n\r", localtime(&seconds)); |
justinkim | 6:b33c1a450937 | 249 | |
justinkim | 6:b33c1a450937 | 250 | if(hms_buffer[4] == '0' && hms_buffer[5] == '0') |
justinkim | 6:b33c1a450937 | 251 | { |
justinkim | 6:b33c1a450937 | 252 | time_update(hms_buffer); |
justinkim | 6:b33c1a450937 | 253 | printf("update HMS : %s\r\n",hms_buffer); |
justinkim | 6:b33c1a450937 | 254 | } |
justinkim | 6:b33c1a450937 | 255 | |
justinkim | 6:b33c1a450937 | 256 | hm_r = (hour_r*100) + min_r; |
justinkim | 6:b33c1a450937 | 257 | hm_n = atoi(hms_buffer); |
justinkim | 6:b33c1a450937 | 258 | hm_n = hm_n / 100; |
justinkim | 6:b33c1a450937 | 259 | printf("HMr : %d\r\n",hm_r); |
justinkim | 6:b33c1a450937 | 260 | printf("HMn : %d\r\n",hm_n); |
justinkim | 6:b33c1a450937 | 261 | |
justinkim | 6:b33c1a450937 | 262 | flag_t = 1; |
justinkim | 5:3c24e937da61 | 263 | } |
hjjeon | 0:e59cc54df17c | 264 | else |
hjjeon | 0:e59cc54df17c | 265 | { |
hjjeon | 0:e59cc54df17c | 266 | return 404; |
hjjeon | 0:e59cc54df17c | 267 | } |
hjjeon | 0:e59cc54df17c | 268 | } |
hjjeon | 0:e59cc54df17c | 269 | |
hjjeon | 0:e59cc54df17c | 270 | int HTTPFsRequestHandler::handlePutRequest() |
hjjeon | 0:e59cc54df17c | 271 | { |
hjjeon | 0:e59cc54df17c | 272 | return 404; |
hjjeon | 0:e59cc54df17c | 273 | } |
hjjeon | 0:e59cc54df17c | 274 | |
hjjeon | 0:e59cc54df17c | 275 | uint32_t HTTPFsRequestHandler::get_http_param_value(char* param_name) |
hjjeon | 0:e59cc54df17c | 276 | { |
hjjeon | 0:e59cc54df17c | 277 | uint8_t * name = 0; |
hjjeon | 0:e59cc54df17c | 278 | uint8_t * pos2; |
hjjeon | 0:e59cc54df17c | 279 | uint16_t len = 0; |
hjjeon | 0:e59cc54df17c | 280 | char value[10]; |
hjjeon | 0:e59cc54df17c | 281 | uint32_t ret = 0; |
hjjeon | 0:e59cc54df17c | 282 | |
hjjeon | 0:e59cc54df17c | 283 | //msg.attri |
hjjeon | 0:e59cc54df17c | 284 | if((name = (uint8_t *)strstr(msg.attri, param_name))) |
hjjeon | 0:e59cc54df17c | 285 | { |
hjjeon | 0:e59cc54df17c | 286 | name += strlen(param_name) + 1; |
hjjeon | 0:e59cc54df17c | 287 | pos2 = (uint8_t*)strstr((char*)name, "&"); |
hjjeon | 0:e59cc54df17c | 288 | if(!pos2) |
hjjeon | 0:e59cc54df17c | 289 | { |
hjjeon | 0:e59cc54df17c | 290 | pos2 = name + strlen((char*)name); |
hjjeon | 0:e59cc54df17c | 291 | } |
hjjeon | 0:e59cc54df17c | 292 | len = pos2 - name; |
hjjeon | 0:e59cc54df17c | 293 | |
hjjeon | 0:e59cc54df17c | 294 | if(len) |
hjjeon | 0:e59cc54df17c | 295 | { |
hjjeon | 0:e59cc54df17c | 296 | strncpy(value, (char*)name, len); |
hjjeon | 0:e59cc54df17c | 297 | ret = atoi(value); |
hjjeon | 0:e59cc54df17c | 298 | } |
hjjeon | 0:e59cc54df17c | 299 | } |
hjjeon | 0:e59cc54df17c | 300 | return ret; |
hjjeon | 0:e59cc54df17c | 301 | } |
hjjeon | 0:e59cc54df17c | 302 | |
justinkim | 6:b33c1a450937 | 303 | void SNTP_Connect(void) { |
justinkim | 6:b33c1a450937 | 304 | printf("Getting time information by using NTP...\r\n"); |
justinkim | 6:b33c1a450937 | 305 | |
justinkim | 6:b33c1a450937 | 306 | SNTPClient sntp("time.nist.gov", 40); // timezone: Korea, Republic of |
justinkim | 6:b33c1a450937 | 307 | sntp.connect(); |
justinkim | 6:b33c1a450937 | 308 | if(sntp.getTime(&ntptime) == true) { |
justinkim | 6:b33c1a450937 | 309 | printf("%d-%d-%d, %02d:%02d:%02d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss); |
justinkim | 6:b33c1a450937 | 310 | printf("Completed Get and Set Time\r\n\r\n"); |
justinkim | 6:b33c1a450937 | 311 | } |
justinkim | 6:b33c1a450937 | 312 | else { |
justinkim | 6:b33c1a450937 | 313 | while(sntp.getTime(&ntptime) == true) { |
justinkim | 6:b33c1a450937 | 314 | printf("%d-%d-%d, %02d:%02d:%02d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss); |
justinkim | 6:b33c1a450937 | 315 | printf("Completed Get and Set Time\r\n\r\n"); |
justinkim | 6:b33c1a450937 | 316 | break; |
justinkim | 6:b33c1a450937 | 317 | } |
justinkim | 6:b33c1a450937 | 318 | } |
justinkim | 6:b33c1a450937 | 319 | sntp.close(); |
justinkim | 6:b33c1a450937 | 320 | } |
justinkim | 2:dd293a10a772 | 321 | |
justinkim | 6:b33c1a450937 | 322 | void time_set(void) { |
justinkim | 6:b33c1a450937 | 323 | timeinfo.tm_mon = ntptime.mo-1; |
justinkim | 6:b33c1a450937 | 324 | timeinfo.tm_mday = ntptime.dd; |
justinkim | 6:b33c1a450937 | 325 | timeinfo.tm_hour = ntptime.hh; |
justinkim | 6:b33c1a450937 | 326 | timeinfo.tm_min = ntptime.mm; |
justinkim | 6:b33c1a450937 | 327 | timeinfo.tm_sec = ntptime.ss; |
justinkim | 6:b33c1a450937 | 328 | timeinfo.tm_year = ntptime.yy-1900; |
justinkim | 6:b33c1a450937 | 329 | time_t t =mktime(&timeinfo); |
justinkim | 6:b33c1a450937 | 330 | set_time(t); |
justinkim | 6:b33c1a450937 | 331 | t = time(NULL); |
justinkim | 6:b33c1a450937 | 332 | } |
justinkim | 6:b33c1a450937 | 333 | |
justinkim | 6:b33c1a450937 | 334 | void time_update(char buffer[]) { |
justinkim | 6:b33c1a450937 | 335 | uint8_t h_buffer = ((buffer[0]-48)*10) + (buffer[1]-48); |
justinkim | 6:b33c1a450937 | 336 | uint8_t m_buffer = ((buffer[2]-48)*10) + (buffer[3]-48); |
justinkim | 6:b33c1a450937 | 337 | uint8_t s_buffer = ((buffer[4]-48)*10) + (buffer[5]-48); |
justinkim | 6:b33c1a450937 | 338 | |
justinkim | 6:b33c1a450937 | 339 | if(update_mode == 0){ |
justinkim | 6:b33c1a450937 | 340 | if(h_buffer == Set_update[0] && m_buffer == Set_update[1] && s_buffer == Set_update[2]){ |
justinkim | 6:b33c1a450937 | 341 | SNTP_Connect(); |
justinkim | 6:b33c1a450937 | 342 | time_set(); |
justinkim | 6:b33c1a450937 | 343 | update_mode = 1; |
justinkim | 6:b33c1a450937 | 344 | printf("Time Update Completed.\n\r\n\r"); |
justinkim | 6:b33c1a450937 | 345 | } |
justinkim | 6:b33c1a450937 | 346 | } |
justinkim | 6:b33c1a450937 | 347 | else if(update_mode == 1){ |
justinkim | 6:b33c1a450937 | 348 | if(!(h_buffer == Set_update[0] && m_buffer == Set_update[1] && s_buffer == Set_update[2])){ |
justinkim | 6:b33c1a450937 | 349 | update_mode = 0; |
justinkim | 6:b33c1a450937 | 350 | } |
justinkim | 6:b33c1a450937 | 351 | } |
justinkim | 6:b33c1a450937 | 352 | } |