SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lorcansmith 0:2a53a4c3238c 1
lorcansmith 0:2a53a4c3238c 2 /*
lorcansmith 0:2a53a4c3238c 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
lorcansmith 0:2a53a4c3238c 4
lorcansmith 0:2a53a4c3238c 5 Permission is hereby granted, free of charge, to any person obtaining a copy
lorcansmith 0:2a53a4c3238c 6 of this software and associated documentation files (the "Software"), to deal
lorcansmith 0:2a53a4c3238c 7 in the Software without restriction, including without limitation the rights
lorcansmith 0:2a53a4c3238c 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
lorcansmith 0:2a53a4c3238c 9 copies of the Software, and to permit persons to whom the Software is
lorcansmith 0:2a53a4c3238c 10 furnished to do so, subject to the following conditions:
lorcansmith 0:2a53a4c3238c 11
lorcansmith 0:2a53a4c3238c 12 The above copyright notice and this permission notice shall be included in
lorcansmith 0:2a53a4c3238c 13 all copies or substantial portions of the Software.
lorcansmith 0:2a53a4c3238c 14
lorcansmith 0:2a53a4c3238c 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
lorcansmith 0:2a53a4c3238c 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
lorcansmith 0:2a53a4c3238c 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
lorcansmith 0:2a53a4c3238c 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
lorcansmith 0:2a53a4c3238c 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
lorcansmith 0:2a53a4c3238c 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
lorcansmith 0:2a53a4c3238c 21 THE SOFTWARE.
lorcansmith 0:2a53a4c3238c 22 */
lorcansmith 0:2a53a4c3238c 23
lorcansmith 0:2a53a4c3238c 24 #include "core/netservice.h"
lorcansmith 0:2a53a4c3238c 25 #include "HTTPRequestHandler.h"
lorcansmith 0:2a53a4c3238c 26
lorcansmith 0:2a53a4c3238c 27 #include <string.h>
lorcansmith 0:2a53a4c3238c 28
lorcansmith 0:2a53a4c3238c 29 //#define __DEBUG
lorcansmith 0:2a53a4c3238c 30 #include "dbg/dbg.h"
lorcansmith 0:2a53a4c3238c 31
lorcansmith 0:2a53a4c3238c 32 #define HTTP_REQUEST_TIMEOUT 5000
lorcansmith 0:2a53a4c3238c 33
lorcansmith 0:2a53a4c3238c 34 HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : NetService(),
lorcansmith 0:2a53a4c3238c 35 m_pTCPSocket(pTCPSocket), m_reqHeaders(), m_respHeaders(),
lorcansmith 0:2a53a4c3238c 36 m_rootPath(rootPath), m_path(path), m_errc(200),
lorcansmith 0:2a53a4c3238c 37 m_watchdog(), m_timeout(0), m_closed(false), m_headersSent(false) //OK
lorcansmith 0:2a53a4c3238c 38 {
lorcansmith 0:2a53a4c3238c 39 //Read & parse headers
lorcansmith 0:2a53a4c3238c 40 readHeaders();
lorcansmith 0:2a53a4c3238c 41 m_pTCPSocket->setOnEvent(this, &HTTPRequestHandler::onTCPSocketEvent);
lorcansmith 0:2a53a4c3238c 42 setTimeout(HTTP_REQUEST_TIMEOUT);
lorcansmith 0:2a53a4c3238c 43 }
lorcansmith 0:2a53a4c3238c 44
lorcansmith 0:2a53a4c3238c 45 HTTPRequestHandler::~HTTPRequestHandler()
lorcansmith 0:2a53a4c3238c 46 {
lorcansmith 0:2a53a4c3238c 47 close();
lorcansmith 0:2a53a4c3238c 48 }
lorcansmith 0:2a53a4c3238c 49
lorcansmith 0:2a53a4c3238c 50 void HTTPRequestHandler::onTimeout() //Connection has timed out
lorcansmith 0:2a53a4c3238c 51 {
lorcansmith 0:2a53a4c3238c 52 close();
lorcansmith 0:2a53a4c3238c 53 }
lorcansmith 0:2a53a4c3238c 54
lorcansmith 0:2a53a4c3238c 55 void HTTPRequestHandler::close() //Close socket and destroy data
lorcansmith 0:2a53a4c3238c 56 {
lorcansmith 0:2a53a4c3238c 57 if(m_closed)
lorcansmith 0:2a53a4c3238c 58 return;
lorcansmith 0:2a53a4c3238c 59 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
lorcansmith 0:2a53a4c3238c 60 m_watchdog.detach();
lorcansmith 0:2a53a4c3238c 61 onClose();
lorcansmith 0:2a53a4c3238c 62 m_pTCPSocket->resetOnEvent();
lorcansmith 0:2a53a4c3238c 63 m_pTCPSocket->close();
lorcansmith 0:2a53a4c3238c 64 delete m_pTCPSocket; //Can safely destroy socket
lorcansmith 0:2a53a4c3238c 65 NetService::close();
lorcansmith 0:2a53a4c3238c 66 }
lorcansmith 0:2a53a4c3238c 67
lorcansmith 0:2a53a4c3238c 68 map<string, string>& HTTPRequestHandler::reqHeaders() //const
lorcansmith 0:2a53a4c3238c 69 {
lorcansmith 0:2a53a4c3238c 70 return m_reqHeaders;
lorcansmith 0:2a53a4c3238c 71 }
lorcansmith 0:2a53a4c3238c 72
lorcansmith 0:2a53a4c3238c 73 string& HTTPRequestHandler::path() //const
lorcansmith 0:2a53a4c3238c 74 {
lorcansmith 0:2a53a4c3238c 75 return m_path;
lorcansmith 0:2a53a4c3238c 76 }
lorcansmith 0:2a53a4c3238c 77
lorcansmith 0:2a53a4c3238c 78 int HTTPRequestHandler::dataLen() const
lorcansmith 0:2a53a4c3238c 79 {
lorcansmith 0:2a53a4c3238c 80 map<string,string>::iterator it;
lorcansmith 0:2a53a4c3238c 81 it = m_reqHeaders.find("Content-Length");
lorcansmith 0:2a53a4c3238c 82 if( it == m_reqHeaders.end() )
lorcansmith 0:2a53a4c3238c 83 {
lorcansmith 0:2a53a4c3238c 84 return 0;
lorcansmith 0:2a53a4c3238c 85 }
lorcansmith 0:2a53a4c3238c 86 return atoi((*it).second.c_str()); //return 0 if parse fails, so that's fine
lorcansmith 0:2a53a4c3238c 87 }
lorcansmith 0:2a53a4c3238c 88
lorcansmith 0:2a53a4c3238c 89 int HTTPRequestHandler::readData(char* buf, int len)
lorcansmith 0:2a53a4c3238c 90 {
lorcansmith 0:2a53a4c3238c 91 return m_pTCPSocket->recv(buf, len);
lorcansmith 0:2a53a4c3238c 92 }
lorcansmith 0:2a53a4c3238c 93
lorcansmith 0:2a53a4c3238c 94 string& HTTPRequestHandler::rootPath() //const
lorcansmith 0:2a53a4c3238c 95 {
lorcansmith 0:2a53a4c3238c 96 return m_rootPath;
lorcansmith 0:2a53a4c3238c 97 }
lorcansmith 0:2a53a4c3238c 98
lorcansmith 0:2a53a4c3238c 99 void HTTPRequestHandler::setErrCode(int errc)
lorcansmith 0:2a53a4c3238c 100 {
lorcansmith 0:2a53a4c3238c 101 m_errc = errc;
lorcansmith 0:2a53a4c3238c 102 }
lorcansmith 0:2a53a4c3238c 103
lorcansmith 0:2a53a4c3238c 104 void HTTPRequestHandler::setContentLen(int len)
lorcansmith 0:2a53a4c3238c 105 {
lorcansmith 0:2a53a4c3238c 106 char len_str[6] = {0};
lorcansmith 0:2a53a4c3238c 107 sprintf(len_str, "%d", len);
lorcansmith 0:2a53a4c3238c 108 respHeaders()["Content-Length"] = len_str;
lorcansmith 0:2a53a4c3238c 109 }
lorcansmith 0:2a53a4c3238c 110
lorcansmith 0:2a53a4c3238c 111 map<string, string>& HTTPRequestHandler::respHeaders()
lorcansmith 0:2a53a4c3238c 112 {
lorcansmith 0:2a53a4c3238c 113 return m_respHeaders;
lorcansmith 0:2a53a4c3238c 114 }
lorcansmith 0:2a53a4c3238c 115
lorcansmith 0:2a53a4c3238c 116 int HTTPRequestHandler::writeData(const char* buf, int len)
lorcansmith 0:2a53a4c3238c 117 {
lorcansmith 0:2a53a4c3238c 118 if(!m_headersSent)
lorcansmith 0:2a53a4c3238c 119 {
lorcansmith 0:2a53a4c3238c 120 m_headersSent = true;
lorcansmith 0:2a53a4c3238c 121 writeHeaders();
lorcansmith 0:2a53a4c3238c 122 }
lorcansmith 0:2a53a4c3238c 123
lorcansmith 0:2a53a4c3238c 124 return m_pTCPSocket->send(buf, len);
lorcansmith 0:2a53a4c3238c 125 }
lorcansmith 0:2a53a4c3238c 126
lorcansmith 0:2a53a4c3238c 127 void HTTPRequestHandler::setTimeout(int ms)
lorcansmith 0:2a53a4c3238c 128 {
lorcansmith 0:2a53a4c3238c 129 m_timeout = 1000*ms;
lorcansmith 0:2a53a4c3238c 130 resetTimeout();
lorcansmith 0:2a53a4c3238c 131 }
lorcansmith 0:2a53a4c3238c 132
lorcansmith 0:2a53a4c3238c 133 void HTTPRequestHandler::resetTimeout()
lorcansmith 0:2a53a4c3238c 134 {
lorcansmith 0:2a53a4c3238c 135 m_watchdog.detach();
lorcansmith 0:2a53a4c3238c 136 m_watchdog.attach_us<HTTPRequestHandler>(this, &HTTPRequestHandler::onTimeout, m_timeout);
lorcansmith 0:2a53a4c3238c 137 }
lorcansmith 0:2a53a4c3238c 138
lorcansmith 0:2a53a4c3238c 139
lorcansmith 0:2a53a4c3238c 140 void HTTPRequestHandler::readHeaders()
lorcansmith 0:2a53a4c3238c 141 {
lorcansmith 0:2a53a4c3238c 142 static char line[128];
lorcansmith 0:2a53a4c3238c 143 static char key[128];
lorcansmith 0:2a53a4c3238c 144 static char value[128];
lorcansmith 0:2a53a4c3238c 145 while( readLine(line, 128) > 0) //if == 0, it is an empty line = end of headers
lorcansmith 0:2a53a4c3238c 146 {
lorcansmith 0:2a53a4c3238c 147 int n = sscanf(line, "%[^:]: %[^\n]", key, value);
lorcansmith 0:2a53a4c3238c 148 if ( n == 2 )
lorcansmith 0:2a53a4c3238c 149 {
lorcansmith 0:2a53a4c3238c 150 DBG("\r\nRead header : %s : %s\r\n", key, value);
lorcansmith 0:2a53a4c3238c 151 m_reqHeaders[key] = value;
lorcansmith 0:2a53a4c3238c 152 }
lorcansmith 0:2a53a4c3238c 153 //TODO: Impl n==1 case (part 2 of previous header)
lorcansmith 0:2a53a4c3238c 154 }
lorcansmith 0:2a53a4c3238c 155 }
lorcansmith 0:2a53a4c3238c 156
lorcansmith 0:2a53a4c3238c 157 void HTTPRequestHandler::writeHeaders() //Called at the first writeData call
lorcansmith 0:2a53a4c3238c 158 {
lorcansmith 0:2a53a4c3238c 159 static char line[128];
lorcansmith 0:2a53a4c3238c 160
lorcansmith 0:2a53a4c3238c 161 //Response line
lorcansmith 0:2a53a4c3238c 162 sprintf(line, "HTTP/1.1 %d MbedInfo\r\n", m_errc); //Not a violation of the standard not to include the descriptive text
lorcansmith 0:2a53a4c3238c 163 m_pTCPSocket->send(line, strlen(line));
lorcansmith 0:2a53a4c3238c 164
lorcansmith 0:2a53a4c3238c 165 map<string,string>::iterator it;
lorcansmith 0:2a53a4c3238c 166 while( !m_respHeaders.empty() )
lorcansmith 0:2a53a4c3238c 167 {
lorcansmith 0:2a53a4c3238c 168 it = m_respHeaders.begin();
lorcansmith 0:2a53a4c3238c 169 sprintf(line, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() );
lorcansmith 0:2a53a4c3238c 170 DBG("\r\n%s", line);
lorcansmith 0:2a53a4c3238c 171 m_pTCPSocket->send(line, strlen(line));
lorcansmith 0:2a53a4c3238c 172 m_respHeaders.erase(it);
lorcansmith 0:2a53a4c3238c 173 }
lorcansmith 0:2a53a4c3238c 174 m_pTCPSocket->send("\r\n",2); //End of head
lorcansmith 0:2a53a4c3238c 175 }
lorcansmith 0:2a53a4c3238c 176
lorcansmith 0:2a53a4c3238c 177 int HTTPRequestHandler::readLine(char* str, int maxLen)
lorcansmith 0:2a53a4c3238c 178 {
lorcansmith 0:2a53a4c3238c 179 int ret;
lorcansmith 0:2a53a4c3238c 180 int len = 0;
lorcansmith 0:2a53a4c3238c 181 for(int i = 0; i < maxLen - 1; i++)
lorcansmith 0:2a53a4c3238c 182 {
lorcansmith 0:2a53a4c3238c 183 ret = m_pTCPSocket->recv(str, 1);
lorcansmith 0:2a53a4c3238c 184 if(!ret)
lorcansmith 0:2a53a4c3238c 185 {
lorcansmith 0:2a53a4c3238c 186 break;
lorcansmith 0:2a53a4c3238c 187 }
lorcansmith 0:2a53a4c3238c 188 if( (len > 1) && *(str-1)=='\r' && *str=='\n' )
lorcansmith 0:2a53a4c3238c 189 {
lorcansmith 0:2a53a4c3238c 190 str--;
lorcansmith 0:2a53a4c3238c 191 len-=2;
lorcansmith 0:2a53a4c3238c 192 break;
lorcansmith 0:2a53a4c3238c 193 }
lorcansmith 0:2a53a4c3238c 194 else if( *str=='\n' )
lorcansmith 0:2a53a4c3238c 195 {
lorcansmith 0:2a53a4c3238c 196 len--;
lorcansmith 0:2a53a4c3238c 197 break;
lorcansmith 0:2a53a4c3238c 198 }
lorcansmith 0:2a53a4c3238c 199 str++;
lorcansmith 0:2a53a4c3238c 200 len++;
lorcansmith 0:2a53a4c3238c 201 }
lorcansmith 0:2a53a4c3238c 202 *str = 0;
lorcansmith 0:2a53a4c3238c 203 return len;
lorcansmith 0:2a53a4c3238c 204 }
lorcansmith 0:2a53a4c3238c 205
lorcansmith 0:2a53a4c3238c 206 void HTTPRequestHandler::onTCPSocketEvent(TCPSocketEvent e)
lorcansmith 0:2a53a4c3238c 207 {
lorcansmith 0:2a53a4c3238c 208
lorcansmith 0:2a53a4c3238c 209 DBG("\r\nEvent %d in HTTPRequestHandler\r\n", e);
lorcansmith 0:2a53a4c3238c 210
lorcansmith 0:2a53a4c3238c 211 if(m_closed)
lorcansmith 0:2a53a4c3238c 212 {
lorcansmith 0:2a53a4c3238c 213 DBG("\r\nWARN: Discarded\r\n");
lorcansmith 0:2a53a4c3238c 214 return;
lorcansmith 0:2a53a4c3238c 215 }
lorcansmith 0:2a53a4c3238c 216
lorcansmith 0:2a53a4c3238c 217 switch(e)
lorcansmith 0:2a53a4c3238c 218 {
lorcansmith 0:2a53a4c3238c 219 case TCPSOCKET_READABLE:
lorcansmith 0:2a53a4c3238c 220 resetTimeout();
lorcansmith 0:2a53a4c3238c 221 onReadable();
lorcansmith 0:2a53a4c3238c 222 break;
lorcansmith 0:2a53a4c3238c 223 case TCPSOCKET_WRITEABLE:
lorcansmith 0:2a53a4c3238c 224 resetTimeout();
lorcansmith 0:2a53a4c3238c 225 onWriteable();
lorcansmith 0:2a53a4c3238c 226 break;
lorcansmith 0:2a53a4c3238c 227 case TCPSOCKET_CONTIMEOUT:
lorcansmith 0:2a53a4c3238c 228 case TCPSOCKET_CONRST:
lorcansmith 0:2a53a4c3238c 229 case TCPSOCKET_CONABRT:
lorcansmith 0:2a53a4c3238c 230 case TCPSOCKET_ERROR:
lorcansmith 0:2a53a4c3238c 231 case TCPSOCKET_DISCONNECTED:
lorcansmith 0:2a53a4c3238c 232 DBG("\r\nConnection error in handler\r\n");
lorcansmith 0:2a53a4c3238c 233 close();
lorcansmith 0:2a53a4c3238c 234 break;
lorcansmith 0:2a53a4c3238c 235 }
lorcansmith 0:2a53a4c3238c 236
lorcansmith 0:2a53a4c3238c 237 }