Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hendrikvincent 0:05ccbd4f84f1 1
hendrikvincent 0:05ccbd4f84f1 2 /*
hendrikvincent 0:05ccbd4f84f1 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) y Segundo Equipo
hendrikvincent 0:05ccbd4f84f1 4
hendrikvincent 0:05ccbd4f84f1 5 Permission is hereby granted, free of charge, to any person obtaining a copy
hendrikvincent 0:05ccbd4f84f1 6 of this software and associated documentation files (the "Software"), to deal
hendrikvincent 0:05ccbd4f84f1 7 in the Software without restriction, including without limitation the rights
hendrikvincent 0:05ccbd4f84f1 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hendrikvincent 0:05ccbd4f84f1 9 copies of the Software, and to permit persons to whom the Software is
hendrikvincent 0:05ccbd4f84f1 10 furnished to do so, subject to the following conditions:
hendrikvincent 0:05ccbd4f84f1 11
hendrikvincent 0:05ccbd4f84f1 12 The above copyright notice and this permission notice shall be included in
hendrikvincent 0:05ccbd4f84f1 13 all copies or substantial portions of the Software.
hendrikvincent 0:05ccbd4f84f1 14
hendrikvincent 0:05ccbd4f84f1 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hendrikvincent 0:05ccbd4f84f1 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hendrikvincent 0:05ccbd4f84f1 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hendrikvincent 0:05ccbd4f84f1 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hendrikvincent 0:05ccbd4f84f1 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hendrikvincent 0:05ccbd4f84f1 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hendrikvincent 0:05ccbd4f84f1 21 THE SOFTWARE.
hendrikvincent 0:05ccbd4f84f1 22 */
hendrikvincent 0:05ccbd4f84f1 23
hendrikvincent 0:05ccbd4f84f1 24 /** \file
hendrikvincent 0:05ccbd4f84f1 25 SMTP Client header file
hendrikvincent 0:05ccbd4f84f1 26 */
hendrikvincent 0:05ccbd4f84f1 27
hendrikvincent 0:05ccbd4f84f1 28 #ifndef SMTP_CLIENT_H
hendrikvincent 0:05ccbd4f84f1 29 #define SMTP_CLIENT_H
hendrikvincent 0:05ccbd4f84f1 30
hendrikvincent 0:05ccbd4f84f1 31 class EmailMessage;
hendrikvincent 0:05ccbd4f84f1 32
hendrikvincent 0:05ccbd4f84f1 33 #include "core/net.h"
hendrikvincent 0:05ccbd4f84f1 34 #include "api/TCPSocket.h"
hendrikvincent 0:05ccbd4f84f1 35 #include "api/DNSRequest.h"
hendrikvincent 0:05ccbd4f84f1 36 #include "EmailMessage.h"
hendrikvincent 0:05ccbd4f84f1 37 #include "mbed.h"
hendrikvincent 0:05ccbd4f84f1 38
hendrikvincent 0:05ccbd4f84f1 39 ///SMTP client results
hendrikvincent 0:05ccbd4f84f1 40 enum SMTPResult {
hendrikvincent 0:05ccbd4f84f1 41 SMTP_OK, ///<Success
hendrikvincent 0:05ccbd4f84f1 42 SMTP_PROCESSING, ///<Processing
hendrikvincent 0:05ccbd4f84f1 43 SMTP_DNS, ///<Could not resolve name
hendrikvincent 0:05ccbd4f84f1 44 SMTP_PRTCL, ///<Protocol error
hendrikvincent 0:05ccbd4f84f1 45 SMTP_TIMEOUT, ///<Connection timeout
hendrikvincent 0:05ccbd4f84f1 46 SMTP_DISC ///<Disconnected
hendrikvincent 0:05ccbd4f84f1 47 };
hendrikvincent 0:05ccbd4f84f1 48
hendrikvincent 0:05ccbd4f84f1 49 ///SMTP authentication
hendrikvincent 0:05ccbd4f84f1 50 enum SMTPAuth {
hendrikvincent 0:05ccbd4f84f1 51 SMTP_AUTH_NONE, ///<No authentication
hendrikvincent 0:05ccbd4f84f1 52 SMTP_AUTH_PLAIN ///<AUTH PLAIN authentication
hendrikvincent 0:05ccbd4f84f1 53 };
hendrikvincent 0:05ccbd4f84f1 54 #include "core/netservice.h"
hendrikvincent 0:05ccbd4f84f1 55
hendrikvincent 0:05ccbd4f84f1 56 ///A simple SMTP Client
hendrikvincent 0:05ccbd4f84f1 57 /**
hendrikvincent 0:05ccbd4f84f1 58 The SMTPClient is composed of:
hendrikvincent 0:05ccbd4f84f1 59 - The actual client (SMTPClient)
hendrikvincent 0:05ccbd4f84f1 60 - A class (EmailMessage) to hold the message addresses and content for sending
hendrikvincent 0:05ccbd4f84f1 61 */
hendrikvincent 0:05ccbd4f84f1 62 class SMTPClient : protected NetService {
hendrikvincent 0:05ccbd4f84f1 63 public:
hendrikvincent 0:05ccbd4f84f1 64 ///Instantiates the SMTP client
hendrikvincent 0:05ccbd4f84f1 65 SMTPClient();
hendrikvincent 0:05ccbd4f84f1 66
hendrikvincent 0:05ccbd4f84f1 67 ///Destructor for the SMTP client
hendrikvincent 0:05ccbd4f84f1 68 virtual ~SMTPClient();
hendrikvincent 0:05ccbd4f84f1 69
hendrikvincent 0:05ccbd4f84f1 70 ///Full constructor for the SMTP client
hendrikvincent 0:05ccbd4f84f1 71 /**
hendrikvincent 0:05ccbd4f84f1 72 @param host : SMTP server host
hendrikvincent 0:05ccbd4f84f1 73 @param heloDomain : domain name of client
hendrikvincent 0:05ccbd4f84f1 74 @param user : username
hendrikvincent 0:05ccbd4f84f1 75 @param password : password
hendrikvincent 0:05ccbd4f84f1 76 @param auth : authentication type
hendrikvincent 0:05ccbd4f84f1 77 */
hendrikvincent 0:05ccbd4f84f1 78 SMTPClient(const Host& host, const char* heloDomain, const char* user, const char* password, SMTPAuth auth);
hendrikvincent 0:05ccbd4f84f1 79
hendrikvincent 0:05ccbd4f84f1 80 ///Set server host
hendrikvincent 0:05ccbd4f84f1 81 /**
hendrikvincent 0:05ccbd4f84f1 82 @param host : SMTP server host
hendrikvincent 0:05ccbd4f84f1 83 */
hendrikvincent 0:05ccbd4f84f1 84 void setServer(const Host& host);
hendrikvincent 0:05ccbd4f84f1 85
hendrikvincent 0:05ccbd4f84f1 86 ///Provides a plain authentication feature (Base64 encoded username and password)
hendrikvincent 0:05ccbd4f84f1 87 /**
hendrikvincent 0:05ccbd4f84f1 88 @param user : username
hendrikvincent 0:05ccbd4f84f1 89 @param password : password
hendrikvincent 0:05ccbd4f84f1 90 */
hendrikvincent 0:05ccbd4f84f1 91 void setAuth(const char* user, const char* password); // Plain authentication
hendrikvincent 0:05ccbd4f84f1 92
hendrikvincent 0:05ccbd4f84f1 93 ///Turns off authentication
hendrikvincent 0:05ccbd4f84f1 94 void clearAuth(); // Clear authentication
hendrikvincent 0:05ccbd4f84f1 95
hendrikvincent 0:05ccbd4f84f1 96 ///Set HELO domain (defaults to localhost if not set)
hendrikvincent 0:05ccbd4f84f1 97 /**
hendrikvincent 0:05ccbd4f84f1 98 @param heloDomain : domain name of client (strictly should be fully qualified domain name)
hendrikvincent 0:05ccbd4f84f1 99 */
hendrikvincent 0:05ccbd4f84f1 100 void setHeloDomain(const char* heloDomain);
hendrikvincent 0:05ccbd4f84f1 101
hendrikvincent 0:05ccbd4f84f1 102 //High Level setup functions
hendrikvincent 0:05ccbd4f84f1 103 ///Sends the message (blocking)
hendrikvincent 0:05ccbd4f84f1 104 /**
hendrikvincent 0:05ccbd4f84f1 105 @param pMessage : pointer to a message
hendrikvincent 0:05ccbd4f84f1 106
hendrikvincent 0:05ccbd4f84f1 107 Blocks until completion
hendrikvincent 0:05ccbd4f84f1 108 */
hendrikvincent 0:05ccbd4f84f1 109 SMTPResult send(EmailMessage* pMessage); //Blocking
hendrikvincent 0:05ccbd4f84f1 110
hendrikvincent 0:05ccbd4f84f1 111 ///Sends the message (non blocking)
hendrikvincent 0:05ccbd4f84f1 112 /**
hendrikvincent 0:05ccbd4f84f1 113 @param pMessage : pointer to a message
hendrikvincent 0:05ccbd4f84f1 114 @param pMethod : callback function
hendrikvincent 0:05ccbd4f84f1 115
hendrikvincent 0:05ccbd4f84f1 116 The function returns immediately and calls the callback on completion or error
hendrikvincent 0:05ccbd4f84f1 117 */
hendrikvincent 0:05ccbd4f84f1 118 SMTPResult send(EmailMessage* pMessage, void (*pMethod)(SMTPResult)); //Non blocking
hendrikvincent 0:05ccbd4f84f1 119
hendrikvincent 0:05ccbd4f84f1 120 ///Sends the message (non blocking)
hendrikvincent 0:05ccbd4f84f1 121 /**
hendrikvincent 0:05ccbd4f84f1 122 @param pMessage : pointer to a message
hendrikvincent 0:05ccbd4f84f1 123 @param pItem : instance of class on which to execute the callback method
hendrikvincent 0:05ccbd4f84f1 124 @param pMethod : callback method
hendrikvincent 0:05ccbd4f84f1 125
hendrikvincent 0:05ccbd4f84f1 126 The function returns immediately and calls the callback on completion or error
hendrikvincent 0:05ccbd4f84f1 127 */
hendrikvincent 0:05ccbd4f84f1 128 template<class T>
hendrikvincent 0:05ccbd4f84f1 129 SMTPResult send(EmailMessage* pMessage, T* pItem, void (T::*pMethod)(SMTPResult)) { //Non blocking
hendrikvincent 0:05ccbd4f84f1 130 setOnResult(pItem, pMethod);
hendrikvincent 0:05ccbd4f84f1 131 doSend(pMessage);
hendrikvincent 0:05ccbd4f84f1 132 return SMTP_PROCESSING;
hendrikvincent 0:05ccbd4f84f1 133 }
hendrikvincent 0:05ccbd4f84f1 134
hendrikvincent 0:05ccbd4f84f1 135 ///Sends the message (non blocking)
hendrikvincent 0:05ccbd4f84f1 136 /**
hendrikvincent 0:05ccbd4f84f1 137 @param pMessage : pointer to a message
hendrikvincent 0:05ccbd4f84f1 138
hendrikvincent 0:05ccbd4f84f1 139 The function returns immediately and calls the previously set callback on completion or error
hendrikvincent 0:05ccbd4f84f1 140 */
hendrikvincent 0:05ccbd4f84f1 141 void doSend(EmailMessage* pMessage);
hendrikvincent 0:05ccbd4f84f1 142
hendrikvincent 0:05ccbd4f84f1 143 ///Setup the result callback
hendrikvincent 0:05ccbd4f84f1 144 /**
hendrikvincent 0:05ccbd4f84f1 145 @param pMethod : callback function
hendrikvincent 0:05ccbd4f84f1 146 */
hendrikvincent 0:05ccbd4f84f1 147 void setOnResult( void (*pMethod)(SMTPResult) );
hendrikvincent 0:05ccbd4f84f1 148
hendrikvincent 0:05ccbd4f84f1 149 ///Setup the result callback
hendrikvincent 0:05ccbd4f84f1 150 /**
hendrikvincent 0:05ccbd4f84f1 151 @param pItem : instance of class on which to execute the callback method
hendrikvincent 0:05ccbd4f84f1 152 @param pMethod : callback method
hendrikvincent 0:05ccbd4f84f1 153 */
hendrikvincent 0:05ccbd4f84f1 154 class CDummy;
hendrikvincent 0:05ccbd4f84f1 155 template<class T>
hendrikvincent 0:05ccbd4f84f1 156 void setOnResult( T* pItem, void (T::*pMethod)(SMTPResult) ) {
hendrikvincent 0:05ccbd4f84f1 157 m_pCb = NULL;
hendrikvincent 0:05ccbd4f84f1 158 m_pCbItem = (CDummy*) pItem;
hendrikvincent 0:05ccbd4f84f1 159 m_pCbMeth = (void (CDummy::*)(SMTPResult)) pMethod;
hendrikvincent 0:05ccbd4f84f1 160 }
hendrikvincent 0:05ccbd4f84f1 161
hendrikvincent 0:05ccbd4f84f1 162 ///Setup timeout
hendrikvincent 0:05ccbd4f84f1 163 /**
hendrikvincent 0:05ccbd4f84f1 164 @param ms : time of connection inactivity in ms after which the request should timeout
hendrikvincent 0:05ccbd4f84f1 165 */
hendrikvincent 0:05ccbd4f84f1 166 void setTimeout(int ms);
hendrikvincent 0:05ccbd4f84f1 167
hendrikvincent 0:05ccbd4f84f1 168 ///Gets the last response from the server
hendrikvincent 0:05ccbd4f84f1 169 string& getLastResponse();
hendrikvincent 0:05ccbd4f84f1 170
hendrikvincent 0:05ccbd4f84f1 171 virtual void poll(); //Called by NetServices
hendrikvincent 0:05ccbd4f84f1 172
hendrikvincent 0:05ccbd4f84f1 173 protected:
hendrikvincent 0:05ccbd4f84f1 174 int rc(char* buf); //Return code
hendrikvincent 0:05ccbd4f84f1 175 void process(bool writeable); //Main state-machine
hendrikvincent 0:05ccbd4f84f1 176
hendrikvincent 0:05ccbd4f84f1 177 void resetTimeout();
hendrikvincent 0:05ccbd4f84f1 178
hendrikvincent 0:05ccbd4f84f1 179 void init();
hendrikvincent 0:05ccbd4f84f1 180 void close();
hendrikvincent 0:05ccbd4f84f1 181
hendrikvincent 0:05ccbd4f84f1 182 void setup(EmailMessage* pMessage); //Setup request, make DNS Req if necessary
hendrikvincent 0:05ccbd4f84f1 183 void connect(); //Start Connection
hendrikvincent 0:05ccbd4f84f1 184
hendrikvincent 0:05ccbd4f84f1 185 int tryRead(); //Read data and try to feed output
hendrikvincent 0:05ccbd4f84f1 186 void readData(); //Data has been read
hendrikvincent 0:05ccbd4f84f1 187 void writeData(); //Data has been written & buf is free
hendrikvincent 0:05ccbd4f84f1 188
hendrikvincent 0:05ccbd4f84f1 189 void onTCPSocketEvent(TCPSocketEvent e);
hendrikvincent 0:05ccbd4f84f1 190 void onDNSReply(DNSReply r);
hendrikvincent 0:05ccbd4f84f1 191 void onResult(SMTPResult r); //Called when exchange completed or on failure
hendrikvincent 0:05ccbd4f84f1 192 void onTimeout(); //Connection has timed out
hendrikvincent 0:05ccbd4f84f1 193
hendrikvincent 0:05ccbd4f84f1 194 string encodePlainAuth(); // Encode plain authentication (username and password in based 64)
hendrikvincent 0:05ccbd4f84f1 195 bool okPostAuthentication(int code); // True if server response is ok following authentication
hendrikvincent 0:05ccbd4f84f1 196
hendrikvincent 0:05ccbd4f84f1 197 private:
hendrikvincent 0:05ccbd4f84f1 198 SMTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
hendrikvincent 0:05ccbd4f84f1 199
hendrikvincent 0:05ccbd4f84f1 200 CDummy* m_pCbItem;
hendrikvincent 0:05ccbd4f84f1 201 void (CDummy::*m_pCbMeth)(SMTPResult);
hendrikvincent 0:05ccbd4f84f1 202 void (*m_pCb)(SMTPResult);
hendrikvincent 0:05ccbd4f84f1 203
hendrikvincent 0:05ccbd4f84f1 204 TCPSocket* m_pTCPSocket;
hendrikvincent 0:05ccbd4f84f1 205
hendrikvincent 0:05ccbd4f84f1 206 Timer m_watchdog;
hendrikvincent 0:05ccbd4f84f1 207 int m_timeout;
hendrikvincent 0:05ccbd4f84f1 208
hendrikvincent 0:05ccbd4f84f1 209 DNSRequest* m_pDnsReq;
hendrikvincent 0:05ccbd4f84f1 210 Host m_server;
hendrikvincent 0:05ccbd4f84f1 211
hendrikvincent 0:05ccbd4f84f1 212 bool m_closed;
hendrikvincent 0:05ccbd4f84f1 213
hendrikvincent 0:05ccbd4f84f1 214 enum SMTPStep {
hendrikvincent 0:05ccbd4f84f1 215 SMTP_HELLO,
hendrikvincent 0:05ccbd4f84f1 216 SMTP_AUTH,
hendrikvincent 0:05ccbd4f84f1 217 SMTP_FROM,
hendrikvincent 0:05ccbd4f84f1 218 SMTP_TO,
hendrikvincent 0:05ccbd4f84f1 219 SMTP_DATA,
hendrikvincent 0:05ccbd4f84f1 220 SMTP_BODY,
hendrikvincent 0:05ccbd4f84f1 221 SMTP_BODYMORE,
hendrikvincent 0:05ccbd4f84f1 222 SMTP_EOF,
hendrikvincent 0:05ccbd4f84f1 223 SMTP_BYE,
hendrikvincent 0:05ccbd4f84f1 224 SMTP_CLOSED
hendrikvincent 0:05ccbd4f84f1 225 };
hendrikvincent 0:05ccbd4f84f1 226
hendrikvincent 0:05ccbd4f84f1 227 SMTPStep m_state;
hendrikvincent 0:05ccbd4f84f1 228
hendrikvincent 0:05ccbd4f84f1 229 EmailMessage* m_pMessage;
hendrikvincent 0:05ccbd4f84f1 230
hendrikvincent 0:05ccbd4f84f1 231 int m_posInMsg;
hendrikvincent 0:05ccbd4f84f1 232 int m_posInCRLF;
hendrikvincent 0:05ccbd4f84f1 233
hendrikvincent 0:05ccbd4f84f1 234 SMTPResult m_blockingResult; //Result if blocking mode
hendrikvincent 0:05ccbd4f84f1 235 string m_response;
hendrikvincent 0:05ccbd4f84f1 236 int m_responseCode;
hendrikvincent 0:05ccbd4f84f1 237 string m_username;
hendrikvincent 0:05ccbd4f84f1 238 string m_password;
hendrikvincent 0:05ccbd4f84f1 239 SMTPAuth m_auth;
hendrikvincent 0:05ccbd4f84f1 240 string m_heloDomain;
hendrikvincent 0:05ccbd4f84f1 241
hendrikvincent 0:05ccbd4f84f1 242 vector<string>::iterator m_To;
hendrikvincent 0:05ccbd4f84f1 243 };
hendrikvincent 0:05ccbd4f84f1 244
hendrikvincent 0:05ccbd4f84f1 245 #endif // SMTP_CLIENT_H