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

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SMTPClient.h Source File

SMTPClient.h

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #ifndef SMTP_CLIENT_H
00025 #define SMTP_CLIENT_H
00026 
00027 class EmailMessage;
00028 
00029 #include "core/net.h"
00030 #include "api/TCPSocket.h"
00031 #include "../emailMessage.h"
00032 
00033 #include "mbed.h"
00034 
00035 #define SMTP_REQUEST_TIMEOUT 5000
00036 
00037 enum SMTPResult
00038 {
00039   SMTP_OK,
00040   SMTP_PRTCL, //Protocol error
00041   SMTP_TIMEOUT, //Connection timeout
00042   SMTP_DISC //Disconnected 
00043 };
00044 
00045 class SMTPClient /*: public NetService*/
00046 {
00047 public:
00048   SMTPClient();
00049   virtual ~SMTPClient();
00050   
00051   void setHost(const Host& host);
00052   void send(EmailMessage* pMessage);
00053   
00054   class CDummy;
00055   template<class T> 
00056   //Linker bug : Must be defined here :(
00057   void setOnResult( T* pItem, void (T::*pMethod)(SMTPResult) )
00058   {
00059     m_pCbItem = (CDummy*) pItem;
00060     m_pCbMeth = (void (CDummy::*)(SMTPResult)) pMethod;
00061   }
00062   
00063   void init(); //Create and setup socket if needed
00064   void close();
00065   
00066 private:
00067   int rc(char* buf); //Return code
00068   void process(bool moreData); //Main state-machine
00069 
00070   void setTimeout(int ms);
00071   void resetTimeout();
00072   
00073   void onTimeout(); //Connection has timed out
00074   void onTCPSocketEvent(TCPSocketEvent e);
00075   void onResult(SMTPResult r); //Called when exchange completed or on failure
00076   
00077   EmailMessage* m_pMessage;
00078 
00079   TCPSocket* m_pTCPSocket;
00080 
00081   enum SMTPStep
00082   {
00083     SMTP_HELLO,
00084     SMTP_FROM,
00085     SMTP_TO,
00086     SMTP_DATA,
00087     SMTP_BODY,
00088     SMTP_BODYMORE,
00089     SMTP_EOF,
00090     SMTP_BYE
00091   };
00092   
00093   SMTPStep m_nextState;
00094   
00095   CDummy* m_pCbItem;
00096   void (CDummy::*m_pCbMeth)(SMTPResult);
00097   
00098   Timeout m_watchdog;
00099   int m_timeout;
00100   
00101   int m_posInMsg;
00102   
00103   bool m_closed;
00104   
00105   Host m_host;
00106 
00107 };
00108 
00109 #endif