see http://mbed.org/users/okini3939/notebook/wattmeter-shield-on-mbed/

Dependencies:   mbed

Fork of GSwifi_xively by gs fan

Committer:
okini3939
Date:
Wed Nov 27 08:18:45 2013 +0000
Revision:
4:9a2415f2ab07
update GSwifiInterface library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 4:9a2415f2ab07 1 /* Copyright (C) 2013 gsfan, MIT License
okini3939 4:9a2415f2ab07 2 *
okini3939 4:9a2415f2ab07 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
okini3939 4:9a2415f2ab07 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
okini3939 4:9a2415f2ab07 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
okini3939 4:9a2415f2ab07 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
okini3939 4:9a2415f2ab07 7 * furnished to do so, subject to the following conditions:
okini3939 4:9a2415f2ab07 8 *
okini3939 4:9a2415f2ab07 9 * The above copyright notice and this permission notice shall be included in all copies or
okini3939 4:9a2415f2ab07 10 * substantial portions of the Software.
okini3939 4:9a2415f2ab07 11 *
okini3939 4:9a2415f2ab07 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
okini3939 4:9a2415f2ab07 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
okini3939 4:9a2415f2ab07 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
okini3939 4:9a2415f2ab07 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
okini3939 4:9a2415f2ab07 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
okini3939 4:9a2415f2ab07 17 */
okini3939 4:9a2415f2ab07 18
okini3939 4:9a2415f2ab07 19 #include "GSwifi.h"
okini3939 4:9a2415f2ab07 20
okini3939 4:9a2415f2ab07 21 #ifdef CFG_ENABLE_SMTP
okini3939 4:9a2415f2ab07 22
okini3939 4:9a2415f2ab07 23 int GSwifi::mail (const char *host, int port, const char *to, const char *from, const char *subject, const char *mesg, const char *user, const char *pwd) {
okini3939 4:9a2415f2ab07 24 int ret = -1;
okini3939 4:9a2415f2ab07 25 int cid;
okini3939 4:9a2415f2ab07 26 char cmd[CFG_DATA_SIZE];
okini3939 4:9a2415f2ab07 27 char ip[17];
okini3939 4:9a2415f2ab07 28
okini3939 4:9a2415f2ab07 29 if (!isAssociated() || _state.status != STAT_READY) return -1;
okini3939 4:9a2415f2ab07 30
okini3939 4:9a2415f2ab07 31 if (getHostByName(host, ip)) return -1;
okini3939 4:9a2415f2ab07 32 if (! port) {
okini3939 4:9a2415f2ab07 33 port = 25;
okini3939 4:9a2415f2ab07 34 }
okini3939 4:9a2415f2ab07 35
okini3939 4:9a2415f2ab07 36 cid = open(PROTO_TCP, ip, port);
okini3939 4:9a2415f2ab07 37 if (cid < 0) return -1;
okini3939 4:9a2415f2ab07 38 DBG("cid %d\r\n", cid);
okini3939 4:9a2415f2ab07 39
okini3939 4:9a2415f2ab07 40 if (smtpWait(cid ,220)) goto exit;
okini3939 4:9a2415f2ab07 41
okini3939 4:9a2415f2ab07 42 // send request
okini3939 4:9a2415f2ab07 43 snprintf(cmd, sizeof(cmd), "EHLO %s\r\n", _state.name);
okini3939 4:9a2415f2ab07 44 send(cid, cmd, strlen(cmd));
okini3939 4:9a2415f2ab07 45 wait_ms(100);
okini3939 4:9a2415f2ab07 46 if (smtpWait(cid ,250)) goto quit;
okini3939 4:9a2415f2ab07 47 smtpWait(cid ,0);
okini3939 4:9a2415f2ab07 48
okini3939 4:9a2415f2ab07 49 if (user && pwd) {
okini3939 4:9a2415f2ab07 50 // smtp auth
okini3939 4:9a2415f2ab07 51 int len;
okini3939 4:9a2415f2ab07 52 snprintf(cmd, sizeof(cmd), "%s%c%s%c%s", user, 0, user, 0, pwd);
okini3939 4:9a2415f2ab07 53 len = strlen(user) * 2 + strlen(pwd) + 2;
okini3939 4:9a2415f2ab07 54 char tmp[len + (len / 2)];
okini3939 4:9a2415f2ab07 55 base64encode(cmd, len, tmp, sizeof(tmp));
okini3939 4:9a2415f2ab07 56 snprintf(cmd, sizeof(cmd), "AUTH PLAIN %s\r\n", tmp);
okini3939 4:9a2415f2ab07 57 send(cid, cmd, strlen(cmd));
okini3939 4:9a2415f2ab07 58 if (smtpWait(cid ,235)) goto quit;
okini3939 4:9a2415f2ab07 59 }
okini3939 4:9a2415f2ab07 60
okini3939 4:9a2415f2ab07 61 snprintf(cmd, sizeof(cmd), "MAIL FROM: %s\r\n", from);
okini3939 4:9a2415f2ab07 62 send(cid, cmd, strlen(cmd));
okini3939 4:9a2415f2ab07 63 if (smtpWait(cid ,250)) goto quit;
okini3939 4:9a2415f2ab07 64
okini3939 4:9a2415f2ab07 65 snprintf(cmd, sizeof(cmd), "RCPT TO: %s\r\n", to);
okini3939 4:9a2415f2ab07 66 send(cid, cmd, strlen(cmd));
okini3939 4:9a2415f2ab07 67 if (smtpWait(cid ,250)) goto quit;
okini3939 4:9a2415f2ab07 68
okini3939 4:9a2415f2ab07 69 strcpy(cmd, "DATA\r\n");
okini3939 4:9a2415f2ab07 70 send(cid, cmd, strlen(cmd));
okini3939 4:9a2415f2ab07 71 if (smtpWait(cid ,354)) goto quit;
okini3939 4:9a2415f2ab07 72
okini3939 4:9a2415f2ab07 73 // mail data
okini3939 4:9a2415f2ab07 74 snprintf(cmd, sizeof(cmd), "From: %s\r\n", from);
okini3939 4:9a2415f2ab07 75 send(cid, cmd, strlen(cmd));
okini3939 4:9a2415f2ab07 76 snprintf(cmd, sizeof(cmd), "To: %s\r\n", to);
okini3939 4:9a2415f2ab07 77 send(cid, cmd, strlen(cmd));
okini3939 4:9a2415f2ab07 78 snprintf(cmd, sizeof(cmd), "Subject: %s\r\n\r\n", subject);
okini3939 4:9a2415f2ab07 79 send(cid, cmd, strlen(cmd));
okini3939 4:9a2415f2ab07 80
okini3939 4:9a2415f2ab07 81 send(cid, mesg, strlen(mesg));
okini3939 4:9a2415f2ab07 82 strcpy(cmd, "\r\n.\r\n");
okini3939 4:9a2415f2ab07 83 send(cid, cmd, strlen(cmd));
okini3939 4:9a2415f2ab07 84 if (smtpWait(cid ,250)) goto quit;
okini3939 4:9a2415f2ab07 85 ret = 0;
okini3939 4:9a2415f2ab07 86
okini3939 4:9a2415f2ab07 87 INFO("Mail, from: %s, to: %s %d\r\n", from, to, strlen(mesg));
okini3939 4:9a2415f2ab07 88
okini3939 4:9a2415f2ab07 89 quit:
okini3939 4:9a2415f2ab07 90 strcpy(cmd, "QUIT\r\n");
okini3939 4:9a2415f2ab07 91 send(cid, cmd, strlen(cmd));
okini3939 4:9a2415f2ab07 92 smtpWait(cid ,221);
okini3939 4:9a2415f2ab07 93 exit:
okini3939 4:9a2415f2ab07 94 close(cid);
okini3939 4:9a2415f2ab07 95 return ret;
okini3939 4:9a2415f2ab07 96 }
okini3939 4:9a2415f2ab07 97
okini3939 4:9a2415f2ab07 98 int GSwifi::smtpWait (int cid, int code) {
okini3939 4:9a2415f2ab07 99 Timer timeout;
okini3939 4:9a2415f2ab07 100 int i, n, len = 0;
okini3939 4:9a2415f2ab07 101 char buf[CFG_CMD_SIZE], data[CFG_CMD_SIZE];
okini3939 4:9a2415f2ab07 102
okini3939 4:9a2415f2ab07 103 if (code == 0) {
okini3939 4:9a2415f2ab07 104 // dummy read
okini3939 4:9a2415f2ab07 105 timeout.start();
okini3939 4:9a2415f2ab07 106 while (timeout.read_ms() < CFG_TIMEOUT) {
okini3939 4:9a2415f2ab07 107 wait_ms(10);
okini3939 4:9a2415f2ab07 108 if (!readable(cid)) break;
okini3939 4:9a2415f2ab07 109 n = recv(cid, buf, sizeof(buf));
okini3939 4:9a2415f2ab07 110 if (n <= 0) break;
okini3939 4:9a2415f2ab07 111 }
okini3939 4:9a2415f2ab07 112 timeout.stop();
okini3939 4:9a2415f2ab07 113 return 0;
okini3939 4:9a2415f2ab07 114 }
okini3939 4:9a2415f2ab07 115
okini3939 4:9a2415f2ab07 116 // wait responce
okini3939 4:9a2415f2ab07 117 len = 0;
okini3939 4:9a2415f2ab07 118 timeout.start();
okini3939 4:9a2415f2ab07 119 while (timeout.read_ms() < CFG_TIMEOUT) {
okini3939 4:9a2415f2ab07 120 wait_ms(10);
okini3939 4:9a2415f2ab07 121 n = recv(cid, buf, sizeof(buf));
okini3939 4:9a2415f2ab07 122 for (i = 0; i < n; i ++) {
okini3939 4:9a2415f2ab07 123 if (buf[i] == '\r') continue;
okini3939 4:9a2415f2ab07 124 if (buf[i] == '\n') {
okini3939 4:9a2415f2ab07 125 if (len == 0) continue;
okini3939 4:9a2415f2ab07 126 goto next;
okini3939 4:9a2415f2ab07 127 } else
okini3939 4:9a2415f2ab07 128 if (len < sizeof(data) - 1) {
okini3939 4:9a2415f2ab07 129 data[len] = buf[i];
okini3939 4:9a2415f2ab07 130 len ++;
okini3939 4:9a2415f2ab07 131 }
okini3939 4:9a2415f2ab07 132 }
okini3939 4:9a2415f2ab07 133 }
okini3939 4:9a2415f2ab07 134 next:
okini3939 4:9a2415f2ab07 135 data[len] = 0;
okini3939 4:9a2415f2ab07 136 DBG("smtp: %s\r\n", data);
okini3939 4:9a2415f2ab07 137 timeout.stop();
okini3939 4:9a2415f2ab07 138
okini3939 4:9a2415f2ab07 139 // check return code
okini3939 4:9a2415f2ab07 140 i = atoi(data);
okini3939 4:9a2415f2ab07 141 DBG("smtp status %d\r\n", i);
okini3939 4:9a2415f2ab07 142 return i == code ? 0 : -1;
okini3939 4:9a2415f2ab07 143 }
okini3939 4:9a2415f2ab07 144
okini3939 4:9a2415f2ab07 145 #endif