for pet feeder system

Dependents:   PetfeederWebServer

Fork of httpServer_with_Ethernt by IOP

Committer:
justinkim
Date:
Wed May 04 00:43:24 2016 +0000
Revision:
5:3c24e937da61
Parent:
4:2903435e3811
pet feeder system ver 0.1

Who changed what in which revision?

UserRevisionLine numberNew 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"
hjjeon 0:e59cc54df17c 6
hjjeon 0:e59cc54df17c 7 DigitalOut led_red(LED1);
hjjeon 0:e59cc54df17c 8 DigitalOut led_green(LED2);
hjjeon 0:e59cc54df17c 9 DigitalOut led_blue(LED3);
hjjeon 0:e59cc54df17c 10
justinkim 5:3c24e937da61 11 PwmOut myservo(PC_10);
hjjeon 0:e59cc54df17c 12
hjjeon 0:e59cc54df17c 13 static int matchstrings(const char* one, const char* two)
hjjeon 0:e59cc54df17c 14 {
hjjeon 0:e59cc54df17c 15 int m = 0;
hjjeon 0:e59cc54df17c 16
hjjeon 0:e59cc54df17c 17 for (m = 0; m < min(strlen(one), strlen(two)) ; m++) {
hjjeon 0:e59cc54df17c 18 if (one[m] != two[m])
hjjeon 0:e59cc54df17c 19 return m;
hjjeon 0:e59cc54df17c 20 }
hjjeon 0:e59cc54df17c 21 return m;
hjjeon 0:e59cc54df17c 22 }
hjjeon 0:e59cc54df17c 23
hjjeon 0:e59cc54df17c 24 std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap;
hjjeon 0:e59cc54df17c 25
hjjeon 0:e59cc54df17c 26 HTTPFsRequestHandler::HTTPFsRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp)
hjjeon 0:e59cc54df17c 27 : HTTPRequestHandler(Msg, Tcp)
hjjeon 0:e59cc54df17c 28 {
hjjeon 0:e59cc54df17c 29 m_rootPath = rootPath;
hjjeon 0:e59cc54df17c 30 m_localPath = localPath;
hjjeon 0:e59cc54df17c 31
hjjeon 0:e59cc54df17c 32 string myPath = m_rootPath + m_localPath;
hjjeon 0:e59cc54df17c 33
hjjeon 0:e59cc54df17c 34 // Now replace the virtual root path with a mounted device path
hjjeon 0:e59cc54df17c 35 std::map<const char*, const char*>::iterator it;
hjjeon 0:e59cc54df17c 36 const char *bestMatch = NULL;
hjjeon 0:e59cc54df17c 37 const char *bestMatchExchange = NULL;
hjjeon 0:e59cc54df17c 38 int match_ind = -1;
hjjeon 0:e59cc54df17c 39 for (it = m_fsMap.begin() ; it != m_fsMap.end() ; it++) {
hjjeon 0:e59cc54df17c 40 // find best match (if the given logical path is containted in the root
hjjeon 0:e59cc54df17c 41 int s = matchstrings(myPath.c_str(), it->second);
hjjeon 0:e59cc54df17c 42 INFO("Matching Root %s with handler %s results in %d identical characters\n", myPath.c_str(), it->second, s);
hjjeon 0:e59cc54df17c 43 if ((s == strlen(it->second)) && (s > match_ind)) {
hjjeon 0:e59cc54df17c 44 match_ind = s;
hjjeon 0:e59cc54df17c 45 bestMatch = it->first;
hjjeon 0:e59cc54df17c 46 bestMatchExchange = it->second;
hjjeon 0:e59cc54df17c 47 }
hjjeon 0:e59cc54df17c 48 }
hjjeon 0:e59cc54df17c 49
hjjeon 0:e59cc54df17c 50 if (bestMatch != NULL) {
hjjeon 0:e59cc54df17c 51 m_rootPath = bestMatch;
hjjeon 0:e59cc54df17c 52 m_localPath = string(myPath).substr(strlen(bestMatchExchange));
hjjeon 0:e59cc54df17c 53 }
hjjeon 0:e59cc54df17c 54
hjjeon 0:e59cc54df17c 55 handleRequest();
hjjeon 0:e59cc54df17c 56 }
hjjeon 0:e59cc54df17c 57
hjjeon 0:e59cc54df17c 58 HTTPFsRequestHandler::~HTTPFsRequestHandler()
hjjeon 0:e59cc54df17c 59 {
hjjeon 0:e59cc54df17c 60 }
hjjeon 0:e59cc54df17c 61
justinkim 2:dd293a10a772 62 std::map<int, EthernetInterface*> HTTPFsRequestHandler::m_eth_list;
justinkim 2:dd293a10a772 63
hjjeon 0:e59cc54df17c 64 int HTTPFsRequestHandler::handleGetRequest()
hjjeon 0:e59cc54df17c 65 {
hjjeon 0:e59cc54df17c 66 HTTPHeaders headers;
justinkim 2:dd293a10a772 67 EthernetInterface test_eth;
hjjeon 0:e59cc54df17c 68 int retval = 0; //success
hjjeon 0:e59cc54df17c 69
justinkim 2:dd293a10a772 70 if( std::string::npos != msg.uri.find("get_netinfo.cgi") )
hjjeon 0:e59cc54df17c 71 {
justinkim 2:dd293a10a772 72 char buf[256];
hjjeon 4:2903435e3811 73
hjjeon 4:2903435e3811 74 sprintf(buf, "NetinfoCallback({\"mac\":\"%s\",\"ip\":\"%s\",\"sn\":\"%s\",\"gw\":\"%s\",\"temp\":\"%s\"});"
justinkim 2:dd293a10a772 75 ,m_eth_list[0]->getMACAddress(),m_eth_list[0]->getIPAddress(),m_eth_list[0]->getNetworkMask(),m_eth_list[0]->getGateway());
justinkim 2:dd293a10a772 76
justinkim 2:dd293a10a772 77 tcp.send(buf, strlen(buf));
justinkim 2:dd293a10a772 78 }
justinkim 2:dd293a10a772 79
justinkim 2:dd293a10a772 80 else if( std::string::npos != msg.uri.find("get_ain.cgi") )
justinkim 2:dd293a10a772 81 {
justinkim 2:dd293a10a772 82 char buf[256];
hjjeon 0:e59cc54df17c 83
justinkim 5:3c24e937da61 84 //sprintf(buf, "AinCallback({\"ain_v0\":\"%.3f\",\"ain_v1\":\"%.3f\"});",ain0.read()*100, ain1.read()*100);
justinkim 2:dd293a10a772 85
justinkim 2:dd293a10a772 86 tcp.send(buf, strlen(buf));
hjjeon 0:e59cc54df17c 87 }
justinkim 2:dd293a10a772 88
hjjeon 0:e59cc54df17c 89 else //read html pages
hjjeon 0:e59cc54df17c 90 {
hjjeon 0:e59cc54df17c 91 if (m_localPath.length() > 4)
hjjeon 0:e59cc54df17c 92 getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str());
hjjeon 0:e59cc54df17c 93 else
hjjeon 0:e59cc54df17c 94 getStandardHeaders(headers);
hjjeon 0:e59cc54df17c 95
hjjeon 0:e59cc54df17c 96 INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str());
hjjeon 0:e59cc54df17c 97
hjjeon 0:e59cc54df17c 98 std::string reqPath;
hjjeon 0:e59cc54df17c 99
hjjeon 0:e59cc54df17c 100 // Check if we received a directory with the local bath
hjjeon 0:e59cc54df17c 101 if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) {
hjjeon 0:e59cc54df17c 102 // yes, we shall append the default page name
hjjeon 0:e59cc54df17c 103 m_localPath += "index.html";
hjjeon 0:e59cc54df17c 104 }
hjjeon 0:e59cc54df17c 105
hjjeon 0:e59cc54df17c 106 reqPath = m_rootPath + m_localPath;
hjjeon 0:e59cc54df17c 107
hjjeon 0:e59cc54df17c 108 INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str());
hjjeon 0:e59cc54df17c 109
hjjeon 0:e59cc54df17c 110 FILE *fp = fopen(reqPath.c_str(), "r");
hjjeon 0:e59cc54df17c 111 if (fp != NULL) {
hjjeon 0:e59cc54df17c 112 char * pBuffer = NULL;
hjjeon 0:e59cc54df17c 113 int sz = 8192;
hjjeon 0:e59cc54df17c 114 while( pBuffer == NULL) {
hjjeon 0:e59cc54df17c 115 sz /= 2;
hjjeon 0:e59cc54df17c 116 pBuffer = (char*)malloc(sz);
hjjeon 0:e59cc54df17c 117 if (sz < 128)
hjjeon 0:e59cc54df17c 118 error ("OutOfMemory");
hjjeon 0:e59cc54df17c 119 }
hjjeon 0:e59cc54df17c 120
hjjeon 0:e59cc54df17c 121 // File was found and can be returned
hjjeon 0:e59cc54df17c 122
hjjeon 0:e59cc54df17c 123 // first determine the size
hjjeon 0:e59cc54df17c 124 fseek(fp, 0, SEEK_END);
hjjeon 0:e59cc54df17c 125 long size = ftell(fp);
hjjeon 0:e59cc54df17c 126 fseek(fp, 0, SEEK_SET);
hjjeon 0:e59cc54df17c 127
hjjeon 0:e59cc54df17c 128 startResponse(200, size);
hjjeon 0:e59cc54df17c 129 while(!feof(fp) && !ferror(fp)) {
hjjeon 0:e59cc54df17c 130 int cnt = fread(pBuffer, 1, sz , fp);
hjjeon 0:e59cc54df17c 131 if (cnt < 0)
hjjeon 0:e59cc54df17c 132 cnt = 0;
hjjeon 0:e59cc54df17c 133 processResponse(cnt, pBuffer);
hjjeon 0:e59cc54df17c 134 }
hjjeon 0:e59cc54df17c 135 delete pBuffer;
hjjeon 0:e59cc54df17c 136 endResponse();
hjjeon 0:e59cc54df17c 137 fclose(fp);
hjjeon 0:e59cc54df17c 138 }
hjjeon 0:e59cc54df17c 139 else {
hjjeon 0:e59cc54df17c 140 retval = 404;
hjjeon 0:e59cc54df17c 141 ERR("Requested file was not found !");
hjjeon 0:e59cc54df17c 142 }
hjjeon 0:e59cc54df17c 143 }
hjjeon 0:e59cc54df17c 144
hjjeon 0:e59cc54df17c 145 return retval;
hjjeon 0:e59cc54df17c 146 }
hjjeon 0:e59cc54df17c 147
hjjeon 0:e59cc54df17c 148 int HTTPFsRequestHandler::handlePostRequest()
hjjeon 0:e59cc54df17c 149 {
justinkim 5:3c24e937da61 150 char ch;
justinkim 5:3c24e937da61 151 double duty;
justinkim 5:3c24e937da61 152 myservo.period_ms(20);
hjjeon 0:e59cc54df17c 153 int pin = 0;
hjjeon 0:e59cc54df17c 154
hjjeon 0:e59cc54df17c 155 if( std::string::npos != msg.uri.find("set_dio.cgi") )
hjjeon 0:e59cc54df17c 156 {
hjjeon 0:e59cc54df17c 157 pin = get_http_param_value("pin");
hjjeon 0:e59cc54df17c 158 if(pin == 8)
hjjeon 0:e59cc54df17c 159 {
hjjeon 0:e59cc54df17c 160 led_red = get_http_param_value("val");
hjjeon 0:e59cc54df17c 161 }
hjjeon 0:e59cc54df17c 162 else if(pin == 9)
hjjeon 0:e59cc54df17c 163 {
hjjeon 0:e59cc54df17c 164 led_green = get_http_param_value("val");
hjjeon 0:e59cc54df17c 165 }
hjjeon 0:e59cc54df17c 166 else if(pin == 5)
hjjeon 0:e59cc54df17c 167 {
hjjeon 0:e59cc54df17c 168 led_blue = get_http_param_value("val");
hjjeon 0:e59cc54df17c 169 }
hjjeon 0:e59cc54df17c 170 else
hjjeon 0:e59cc54df17c 171 {
hjjeon 0:e59cc54df17c 172 WARN("Wrong pin number");
hjjeon 0:e59cc54df17c 173 }
hjjeon 0:e59cc54df17c 174
hjjeon 0:e59cc54df17c 175 return 0;
justinkim 5:3c24e937da61 176 }
justinkim 5:3c24e937da61 177 else if( std::string::npos != msg.uri.find("set_dio2.cgi") )
justinkim 5:3c24e937da61 178 {
justinkim 5:3c24e937da61 179 ch = get_http_param_value("pin");
justinkim 5:3c24e937da61 180
justinkim 5:3c24e937da61 181 if(ch == 0)
justinkim 5:3c24e937da61 182 {
justinkim 5:3c24e937da61 183 duty = 0.05;
justinkim 5:3c24e937da61 184 myservo = duty;
justinkim 5:3c24e937da61 185 printf("duty : %lf\r\n", duty);
justinkim 5:3c24e937da61 186 }
justinkim 5:3c24e937da61 187 else if(ch == 1)
justinkim 5:3c24e937da61 188 {
justinkim 5:3c24e937da61 189 duty = 1;
justinkim 5:3c24e937da61 190 myservo = duty;
justinkim 5:3c24e937da61 191 printf("duty : %lf\r\n", duty);
justinkim 5:3c24e937da61 192 //myservo.pulsewidth_ms(1.0);
justinkim 5:3c24e937da61 193 }
justinkim 5:3c24e937da61 194 else if(ch == 2)
justinkim 5:3c24e937da61 195 {
justinkim 5:3c24e937da61 196 duty = 0.2;
justinkim 5:3c24e937da61 197 myservo = duty;
justinkim 5:3c24e937da61 198 printf("duty : %lf\r\n", duty);
justinkim 5:3c24e937da61 199 //myservo.pulsewidth_ms(1.0);
justinkim 5:3c24e937da61 200 }
justinkim 5:3c24e937da61 201 }
hjjeon 0:e59cc54df17c 202 else
hjjeon 0:e59cc54df17c 203 {
hjjeon 0:e59cc54df17c 204 return 404;
hjjeon 0:e59cc54df17c 205 }
hjjeon 0:e59cc54df17c 206 }
hjjeon 0:e59cc54df17c 207
hjjeon 0:e59cc54df17c 208 int HTTPFsRequestHandler::handlePutRequest()
hjjeon 0:e59cc54df17c 209 {
hjjeon 0:e59cc54df17c 210 return 404;
hjjeon 0:e59cc54df17c 211 }
hjjeon 0:e59cc54df17c 212
hjjeon 0:e59cc54df17c 213 uint32_t HTTPFsRequestHandler::get_http_param_value(char* param_name)
hjjeon 0:e59cc54df17c 214 {
hjjeon 0:e59cc54df17c 215 uint8_t * name = 0;
hjjeon 0:e59cc54df17c 216 uint8_t * pos2;
hjjeon 0:e59cc54df17c 217 uint16_t len = 0;
hjjeon 0:e59cc54df17c 218 char value[10];
hjjeon 0:e59cc54df17c 219 uint32_t ret = 0;
hjjeon 0:e59cc54df17c 220
hjjeon 0:e59cc54df17c 221 //msg.attri
hjjeon 0:e59cc54df17c 222 if((name = (uint8_t *)strstr(msg.attri, param_name)))
hjjeon 0:e59cc54df17c 223 {
hjjeon 0:e59cc54df17c 224 name += strlen(param_name) + 1;
hjjeon 0:e59cc54df17c 225 pos2 = (uint8_t*)strstr((char*)name, "&");
hjjeon 0:e59cc54df17c 226 if(!pos2)
hjjeon 0:e59cc54df17c 227 {
hjjeon 0:e59cc54df17c 228 pos2 = name + strlen((char*)name);
hjjeon 0:e59cc54df17c 229 }
hjjeon 0:e59cc54df17c 230 len = pos2 - name;
hjjeon 0:e59cc54df17c 231
hjjeon 0:e59cc54df17c 232 if(len)
hjjeon 0:e59cc54df17c 233 {
hjjeon 0:e59cc54df17c 234 strncpy(value, (char*)name, len);
hjjeon 0:e59cc54df17c 235 ret = atoi(value);
hjjeon 0:e59cc54df17c 236 }
hjjeon 0:e59cc54df17c 237 }
hjjeon 0:e59cc54df17c 238 return ret;
hjjeon 0:e59cc54df17c 239 }
hjjeon 0:e59cc54df17c 240
justinkim 2:dd293a10a772 241