Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:350011bf8be7 1
simon 0:350011bf8be7 2 /*
simon 0:350011bf8be7 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
simon 0:350011bf8be7 4
simon 0:350011bf8be7 5 Permission is hereby granted, free of charge, to any person obtaining a copy
simon 0:350011bf8be7 6 of this software and associated documentation files (the "Software"), to deal
simon 0:350011bf8be7 7 in the Software without restriction, including without limitation the rights
simon 0:350011bf8be7 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 0:350011bf8be7 9 copies of the Software, and to permit persons to whom the Software is
simon 0:350011bf8be7 10 furnished to do so, subject to the following conditions:
simon 0:350011bf8be7 11
simon 0:350011bf8be7 12 The above copyright notice and this permission notice shall be included in
simon 0:350011bf8be7 13 all copies or substantial portions of the Software.
simon 0:350011bf8be7 14
simon 0:350011bf8be7 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 0:350011bf8be7 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 0:350011bf8be7 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 0:350011bf8be7 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 0:350011bf8be7 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 0:350011bf8be7 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 0:350011bf8be7 21 THE SOFTWARE.
simon 0:350011bf8be7 22 */
simon 0:350011bf8be7 23
simon 0:350011bf8be7 24 #include "SMTPClient.h"
simon 0:350011bf8be7 25
simon 0:350011bf8be7 26 /*
simon 0:350011bf8be7 27 Provided as reference only, this code has not been tested.
simon 0:350011bf8be7 28 */
simon 0:350011bf8be7 29 #if 0
simon 0:350011bf8be7 30
simon 0:350011bf8be7 31 #include <stdio.h>
simon 0:350011bf8be7 32
simon 0:350011bf8be7 33 #define __DEBUG
simon 0:350011bf8be7 34 #include "dbg.h"
simon 0:350011bf8be7 35
simon 0:350011bf8be7 36 #define BUF_SIZE 128
simon 0:350011bf8be7 37 #define CHUNK_SIZE 512
simon 0:350011bf8be7 38
simon 0:350011bf8be7 39 SMTPClient::SMTPClient() : m_pMessage(NULL), m_nextState(SMTP_HELLO),
simon 0:350011bf8be7 40 m_pCbItem(NULL), m_pCbMeth(NULL), m_watchdog(), m_timeout(0), m_posInMsg(0), m_closed(true), m_host()
simon 0:350011bf8be7 41 {
simon 0:350011bf8be7 42 setTimeout(SMTP_REQUEST_TIMEOUT);
simon 0:350011bf8be7 43 }
simon 0:350011bf8be7 44
simon 0:350011bf8be7 45 SMTPClient::~SMTPClient()
simon 0:350011bf8be7 46 {
simon 0:350011bf8be7 47 close();
simon 0:350011bf8be7 48 }
simon 0:350011bf8be7 49
simon 0:350011bf8be7 50 void SMTPClient::setHost(const Host& host)
simon 0:350011bf8be7 51 {
simon 0:350011bf8be7 52 m_host = host;
simon 0:350011bf8be7 53 }
simon 0:350011bf8be7 54
simon 0:350011bf8be7 55 void SMTPClient::send(EmailMessage* pMessage)
simon 0:350011bf8be7 56 {
simon 0:350011bf8be7 57 init();
simon 0:350011bf8be7 58 m_posInMsg = 0;
simon 0:350011bf8be7 59 m_nextState = SMTP_HELLO;
simon 0:350011bf8be7 60 if( !m_pTCPSocket->connect(m_host) )
simon 0:350011bf8be7 61 {
simon 0:350011bf8be7 62 close();
simon 0:350011bf8be7 63 onResult(SMTP_DISC);
simon 0:350011bf8be7 64 }
simon 0:350011bf8be7 65 }
simon 0:350011bf8be7 66
simon 0:350011bf8be7 67 void SMTPClient::init() //Create and setup socket if needed
simon 0:350011bf8be7 68 {
simon 0:350011bf8be7 69 close(); //Remove previous elements
simon 0:350011bf8be7 70 if(!m_closed) //Already opened
simon 0:350011bf8be7 71 return;
simon 0:350011bf8be7 72 m_nextState = SMTP_HELLO;
simon 0:350011bf8be7 73 m_pTCPSocket = new TCPSocket;
simon 0:350011bf8be7 74 m_pTCPSocket->setOnEvent(this, &SMTPClient::onTCPSocketEvent);
simon 0:350011bf8be7 75 m_closed = false;
simon 0:350011bf8be7 76 }
simon 0:350011bf8be7 77
simon 0:350011bf8be7 78 void SMTPClient::close()
simon 0:350011bf8be7 79 {
simon 0:350011bf8be7 80 if(m_closed)
simon 0:350011bf8be7 81 return;
simon 0:350011bf8be7 82 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
simon 0:350011bf8be7 83 m_watchdog.detach();
simon 0:350011bf8be7 84 m_pTCPSocket->resetOnEvent();
simon 0:350011bf8be7 85 m_pTCPSocket->close();
simon 0:350011bf8be7 86 delete m_pTCPSocket;
simon 0:350011bf8be7 87 m_pTCPSocket = NULL;
simon 0:350011bf8be7 88 }
simon 0:350011bf8be7 89
simon 0:350011bf8be7 90 int SMTPClient::rc(char* buf) //Parse return code
simon 0:350011bf8be7 91 {
simon 0:350011bf8be7 92 int rc;
simon 0:350011bf8be7 93 int len = sscanf(buf, "%d %*[^\r\n]\r\n", &rc);
simon 0:350011bf8be7 94 if(len != 1)
simon 0:350011bf8be7 95 return -1;
simon 0:350011bf8be7 96 return rc;
simon 0:350011bf8be7 97 }
simon 0:350011bf8be7 98
simon 0:350011bf8be7 99 #define MIN(a,b) ((a)<(b))?(a):(b)
simon 0:350011bf8be7 100 void SMTPClient::process(bool moreData) //Main state-machine
simon 0:350011bf8be7 101 {
simon 0:350011bf8be7 102 char buf[BUF_SIZE] = {0};
simon 0:350011bf8be7 103 if(moreData)
simon 0:350011bf8be7 104 {
simon 0:350011bf8be7 105 if( m_nextState != SMTP_BODYMORE )
simon 0:350011bf8be7 106 {
simon 0:350011bf8be7 107 return;
simon 0:350011bf8be7 108 }
simon 0:350011bf8be7 109 }
simon 0:350011bf8be7 110 if(!moreData) //Receive next frame
simon 0:350011bf8be7 111 {
simon 0:350011bf8be7 112 m_pTCPSocket->recv(buf, BUF_SIZE - 1);
simon 0:350011bf8be7 113 }
simon 0:350011bf8be7 114
simon 0:350011bf8be7 115 IpAddr myIp(0,0,0,0);
simon 0:350011bf8be7 116 string to;
simon 0:350011bf8be7 117 int sendLen;
simon 0:350011bf8be7 118
simon 0:350011bf8be7 119 DBG("In state %d", m_nextState);
simon 0:350011bf8be7 120
simon 0:350011bf8be7 121 switch(m_nextState)
simon 0:350011bf8be7 122 {
simon 0:350011bf8be7 123 case SMTP_HELLO:
simon 0:350011bf8be7 124 if( rc(buf) != 220 )
simon 0:350011bf8be7 125 { close(); onResult(SMTP_PRTCL); return; }
simon 0:350011bf8be7 126 myIp = Net::getDefaultIf()->getIp();
simon 0:350011bf8be7 127 sprintf(buf, "HELO %d.%d.%d.%d\r\n", myIp[0], myIp[1], myIp[2], myIp[3]);
simon 0:350011bf8be7 128 m_nextState = SMTP_FROM;
simon 0:350011bf8be7 129 break;
simon 0:350011bf8be7 130 case SMTP_FROM:
simon 0:350011bf8be7 131 if( rc(buf) != 250 )
simon 0:350011bf8be7 132 { close(); onResult(SMTP_PRTCL); return; }
simon 0:350011bf8be7 133 sprintf(buf, "MAIL FROM:<%s>\r\n", m_pMessage->m_from.c_str());
simon 0:350011bf8be7 134 break;
simon 0:350011bf8be7 135 case SMTP_TO:
simon 0:350011bf8be7 136 if( rc(buf) != 250 )
simon 0:350011bf8be7 137 { close(); onResult(SMTP_PRTCL); return; }
simon 0:350011bf8be7 138 to = m_pMessage->m_lTo.front();
simon 0:350011bf8be7 139 sprintf(buf, "RCPT TO:<%s>\r\n", to.c_str());
simon 0:350011bf8be7 140 m_pMessage->m_lTo.pop();
simon 0:350011bf8be7 141 if(m_pMessage->m_lTo.empty())
simon 0:350011bf8be7 142 {
simon 0:350011bf8be7 143 m_nextState = SMTP_DATA;
simon 0:350011bf8be7 144 }
simon 0:350011bf8be7 145 break;
simon 0:350011bf8be7 146 case SMTP_DATA:
simon 0:350011bf8be7 147 if( rc(buf) != 250 )
simon 0:350011bf8be7 148 { close(); onResult(SMTP_PRTCL); return; }
simon 0:350011bf8be7 149 sprintf(buf, "DATA\r\n");
simon 0:350011bf8be7 150 break;
simon 0:350011bf8be7 151 case SMTP_BODY:
simon 0:350011bf8be7 152 if( rc(buf) != 354 )
simon 0:350011bf8be7 153 { close(); onResult(SMTP_PRTCL); return; }
simon 0:350011bf8be7 154 m_nextState = SMTP_BODYMORE;
simon 0:350011bf8be7 155 case SMTP_BODYMORE:
simon 0:350011bf8be7 156 sendLen = 0;
simon 0:350011bf8be7 157 if( m_posInMsg < m_pMessage->m_content.length() )
simon 0:350011bf8be7 158 {
simon 0:350011bf8be7 159 sendLen = MIN( (m_pMessage->m_content.length() - m_posInMsg), CHUNK_SIZE );
simon 0:350011bf8be7 160 m_pTCPSocket->send( m_pMessage->m_content.c_str() + m_posInMsg, sendLen );
simon 0:350011bf8be7 161 m_posInMsg += sendLen;
simon 0:350011bf8be7 162 }
simon 0:350011bf8be7 163 if( m_posInMsg == m_pMessage->m_content.length() )
simon 0:350011bf8be7 164 {
simon 0:350011bf8be7 165 sprintf(buf, "\r\n.\r\n"); //EOF
simon 0:350011bf8be7 166 m_nextState = SMTP_EOF;
simon 0:350011bf8be7 167 }
simon 0:350011bf8be7 168 break;
simon 0:350011bf8be7 169 case SMTP_EOF:
simon 0:350011bf8be7 170 if( rc(buf) != 250 )
simon 0:350011bf8be7 171 { close(); onResult(SMTP_PRTCL); return; }
simon 0:350011bf8be7 172 sprintf(buf, "QUIT\r\n");
simon 0:350011bf8be7 173 m_nextState = SMTP_BYE;
simon 0:350011bf8be7 174 break;
simon 0:350011bf8be7 175 case SMTP_BYE:
simon 0:350011bf8be7 176 if( rc(buf) != 221 )
simon 0:350011bf8be7 177 { close(); onResult(SMTP_PRTCL); return; }
simon 0:350011bf8be7 178 close();
simon 0:350011bf8be7 179 onResult(SMTP_OK);
simon 0:350011bf8be7 180 break;
simon 0:350011bf8be7 181 }
simon 0:350011bf8be7 182
simon 0:350011bf8be7 183 if( m_nextState != SMTP_BODYMORE )
simon 0:350011bf8be7 184 {
simon 0:350011bf8be7 185 m_pTCPSocket->send( buf, strlen(buf) );
simon 0:350011bf8be7 186 }
simon 0:350011bf8be7 187 }
simon 0:350011bf8be7 188
simon 0:350011bf8be7 189 void SMTPClient::setTimeout(int ms)
simon 0:350011bf8be7 190 {
simon 0:350011bf8be7 191 m_timeout = 1000*ms;
simon 0:350011bf8be7 192 resetTimeout();
simon 0:350011bf8be7 193 }
simon 0:350011bf8be7 194
simon 0:350011bf8be7 195 void SMTPClient::resetTimeout()
simon 0:350011bf8be7 196 {
simon 0:350011bf8be7 197 m_watchdog.detach();
simon 0:350011bf8be7 198 m_watchdog.attach_us<SMTPClient>(this, &SMTPClient::onTimeout, m_timeout);
simon 0:350011bf8be7 199 }
simon 0:350011bf8be7 200
simon 0:350011bf8be7 201 void SMTPClient::onTimeout() //Connection has timed out
simon 0:350011bf8be7 202 {
simon 0:350011bf8be7 203 close();
simon 0:350011bf8be7 204 onResult(SMTP_TIMEOUT);
simon 0:350011bf8be7 205 }
simon 0:350011bf8be7 206
simon 0:350011bf8be7 207 void SMTPClient::onTCPSocketEvent(TCPSocketEvent e)
simon 0:350011bf8be7 208 {
simon 0:350011bf8be7 209 switch(e)
simon 0:350011bf8be7 210 {
simon 0:350011bf8be7 211 case TCPSOCKET_READABLE:
simon 0:350011bf8be7 212 resetTimeout();
simon 0:350011bf8be7 213 process(false);
simon 0:350011bf8be7 214 break;
simon 0:350011bf8be7 215 case TCPSOCKET_WRITEABLE:
simon 0:350011bf8be7 216 resetTimeout();
simon 0:350011bf8be7 217 process(true);
simon 0:350011bf8be7 218 break;
simon 0:350011bf8be7 219 case TCPSOCKET_CONTIMEOUT:
simon 0:350011bf8be7 220 case TCPSOCKET_CONRST:
simon 0:350011bf8be7 221 case TCPSOCKET_CONABRT:
simon 0:350011bf8be7 222 case TCPSOCKET_ERROR:
simon 0:350011bf8be7 223 onResult(SMTP_DISC);
simon 0:350011bf8be7 224 DBG("\r\nConnection error in SMTP Client.\r\n");
simon 0:350011bf8be7 225 close();
simon 0:350011bf8be7 226 break;
simon 0:350011bf8be7 227 case TCPSOCKET_DISCONNECTED:
simon 0:350011bf8be7 228 if(m_nextState != SMTP_BYE)
simon 0:350011bf8be7 229 {
simon 0:350011bf8be7 230 onResult(SMTP_DISC);
simon 0:350011bf8be7 231 DBG("\r\nConnection error in SMTP Client.\r\n");
simon 0:350011bf8be7 232 close();
simon 0:350011bf8be7 233 }
simon 0:350011bf8be7 234 break;
simon 0:350011bf8be7 235 }
simon 0:350011bf8be7 236 }
simon 0:350011bf8be7 237
simon 0:350011bf8be7 238 void SMTPClient::onResult(SMTPResult r) //Must be called by impl when the request completes
simon 0:350011bf8be7 239 {
simon 0:350011bf8be7 240 if(m_pCbItem && m_pCbMeth)
simon 0:350011bf8be7 241 (m_pCbItem->*m_pCbMeth)(r);
simon 0:350011bf8be7 242 }
simon 0:350011bf8be7 243
simon 0:350011bf8be7 244 #endif