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