GainSpan Wi-Fi library see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
Dependents: GSwifi_httpd GSwifi_websocket GSwifi_tcpclient GSwifi_tcpserver ... more
Fork of GSwifi by
GainSpan Wi-Fi library
The GS1011 is an ultra low power 802.11b wireless module from GainSpan.
see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
ゲインスパン Wi-Fi モジュール ライブラリ
ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011 シリーズ用のライブラリです。
解説: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
GSwifi_smtp.cpp@32:e19538c1f13d, 2013-02-26 (annotated)
- Committer:
- gsfan
- Date:
- Tue Feb 26 07:39:58 2013 +0000
- Revision:
- 32:e19538c1f13d
- Parent:
- 29:1c4419512941
- Child:
- 35:515ec79792d3
fix sendmail
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsfan | 25:f6e5622d2930 | 1 | /* Copyright (C) 2013 gsfan, MIT License |
gsfan | 25:f6e5622d2930 | 2 | * |
gsfan | 25:f6e5622d2930 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
gsfan | 25:f6e5622d2930 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
gsfan | 25:f6e5622d2930 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
gsfan | 25:f6e5622d2930 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
gsfan | 25:f6e5622d2930 | 7 | * furnished to do so, subject to the following conditions: |
gsfan | 25:f6e5622d2930 | 8 | * |
gsfan | 25:f6e5622d2930 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
gsfan | 25:f6e5622d2930 | 10 | * substantial portions of the Software. |
gsfan | 25:f6e5622d2930 | 11 | * |
gsfan | 25:f6e5622d2930 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
gsfan | 25:f6e5622d2930 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
gsfan | 25:f6e5622d2930 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
gsfan | 25:f6e5622d2930 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
gsfan | 25:f6e5622d2930 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
gsfan | 25:f6e5622d2930 | 17 | */ |
gsfan | 25:f6e5622d2930 | 18 | /** @file |
gsfan | 25:f6e5622d2930 | 19 | * @brief Gainspan wi-fi module library for mbed |
gsfan | 25:f6e5622d2930 | 20 | * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc. |
gsfan | 25:f6e5622d2930 | 21 | */ |
gsfan | 25:f6e5622d2930 | 22 | |
gsfan | 18:4b97804c37d1 | 23 | #include "dbg.h" |
gsfan | 18:4b97804c37d1 | 24 | #include "mbed.h" |
gsfan | 18:4b97804c37d1 | 25 | #include "GSwifi.h" |
gsfan | 18:4b97804c37d1 | 26 | |
gsfan | 20:151b5a4fdd29 | 27 | #ifdef GS_USE_SMTP |
gsfan | 20:151b5a4fdd29 | 28 | |
gsfan | 19:cad912f5a6ba | 29 | int GSwifi::mail (Host &host, const char *to, const char *from, const char *subject, const char *mesg, const char *user, const char *pwd) { |
gsfan | 18:4b97804c37d1 | 30 | int ret = -1; |
gsfan | 18:4b97804c37d1 | 31 | int cid; |
gsfan | 18:4b97804c37d1 | 32 | |
gsfan | 18:4b97804c37d1 | 33 | if (! _connect || _status != GSSTAT_READY) return -1; |
gsfan | 18:4b97804c37d1 | 34 | |
gsfan | 18:4b97804c37d1 | 35 | if (host.getIp().isNull()) { |
gsfan | 18:4b97804c37d1 | 36 | if (getHostByName(host)) { |
gsfan | 18:4b97804c37d1 | 37 | if (getHostByName(host)) return -1; |
gsfan | 18:4b97804c37d1 | 38 | } |
gsfan | 18:4b97804c37d1 | 39 | } |
gsfan | 18:4b97804c37d1 | 40 | if (host.getPort() == 0) { |
gsfan | 18:4b97804c37d1 | 41 | host.setPort(25); |
gsfan | 18:4b97804c37d1 | 42 | } |
gsfan | 18:4b97804c37d1 | 43 | |
gsfan | 18:4b97804c37d1 | 44 | cid = open(host, GSPROT_TCP); |
gsfan | 18:4b97804c37d1 | 45 | if (cid < 0) return -1; |
gsfan | 18:4b97804c37d1 | 46 | DBG("cid %d\r\n", cid); |
gsfan | 18:4b97804c37d1 | 47 | |
gsfan | 18:4b97804c37d1 | 48 | if (wait_smtp(cid ,220)) goto exit; |
gsfan | 18:4b97804c37d1 | 49 | |
gsfan | 18:4b97804c37d1 | 50 | // send request |
gsfan | 18:4b97804c37d1 | 51 | send(cid, "EHLO gainspan\r\n", 15); |
gsfan | 18:4b97804c37d1 | 52 | wait_ms(100); |
gsfan | 18:4b97804c37d1 | 53 | if (wait_smtp(cid ,250)) goto exit; |
gsfan | 32:e19538c1f13d | 54 | wait_smtp(cid ,0); |
gsfan | 18:4b97804c37d1 | 55 | |
gsfan | 18:4b97804c37d1 | 56 | if (user && pwd) { |
gsfan | 18:4b97804c37d1 | 57 | // smtp auth |
gsfan | 18:4b97804c37d1 | 58 | char tmp[80], buf[100]; |
gsfan | 18:4b97804c37d1 | 59 | int len; |
gsfan | 18:4b97804c37d1 | 60 | snprintf(tmp, sizeof(tmp), "%s%c%s%c%s", user, 0, user, 0, pwd); |
gsfan | 18:4b97804c37d1 | 61 | len = strlen(user) * 2 + strlen(pwd) + 2; |
gsfan | 18:4b97804c37d1 | 62 | base64encode(tmp, len, buf, sizeof(buf)); |
gsfan | 18:4b97804c37d1 | 63 | send(cid, "AUTH PLAIN ", 11); |
gsfan | 18:4b97804c37d1 | 64 | send(cid, buf, strlen(buf)); |
gsfan | 18:4b97804c37d1 | 65 | send(cid, "\r\n", 2); |
gsfan | 18:4b97804c37d1 | 66 | if (wait_smtp(cid ,235)) goto quit; |
gsfan | 18:4b97804c37d1 | 67 | } |
gsfan | 18:4b97804c37d1 | 68 | |
gsfan | 18:4b97804c37d1 | 69 | send(cid, "MAIL FROM: ", 11); |
gsfan | 18:4b97804c37d1 | 70 | send(cid, from, strlen(from)); |
gsfan | 18:4b97804c37d1 | 71 | send(cid, "\r\n", 2); |
gsfan | 18:4b97804c37d1 | 72 | if (wait_smtp(cid ,250)) goto quit; |
gsfan | 18:4b97804c37d1 | 73 | |
gsfan | 18:4b97804c37d1 | 74 | send(cid, "RCPT TO: ", 9); |
gsfan | 18:4b97804c37d1 | 75 | send(cid, to, strlen(to)); |
gsfan | 18:4b97804c37d1 | 76 | send(cid, "\r\n", 2); |
gsfan | 18:4b97804c37d1 | 77 | if (wait_smtp(cid ,250)) goto quit; |
gsfan | 18:4b97804c37d1 | 78 | |
gsfan | 18:4b97804c37d1 | 79 | send(cid, "DATA\r\n", 6); |
gsfan | 18:4b97804c37d1 | 80 | if (wait_smtp(cid ,354)) goto quit; |
gsfan | 18:4b97804c37d1 | 81 | |
gsfan | 18:4b97804c37d1 | 82 | // mail data |
gsfan | 32:e19538c1f13d | 83 | send(cid, "From: ", 6); |
gsfan | 32:e19538c1f13d | 84 | send(cid, from, strlen(from)); |
gsfan | 32:e19538c1f13d | 85 | send(cid, "\r\n", 2); |
gsfan | 32:e19538c1f13d | 86 | send(cid, "To: ", 9); |
gsfan | 32:e19538c1f13d | 87 | send(cid, to, strlen(to)); |
gsfan | 32:e19538c1f13d | 88 | send(cid, "\r\n", 2); |
gsfan | 32:e19538c1f13d | 89 | send(cid, "Subject: ", 9); |
gsfan | 32:e19538c1f13d | 90 | send(cid, subject, strlen(subject)); |
gsfan | 32:e19538c1f13d | 91 | send(cid, "\r\n\r\n", 4); |
gsfan | 32:e19538c1f13d | 92 | |
gsfan | 18:4b97804c37d1 | 93 | send(cid, mesg, strlen(mesg)); |
gsfan | 18:4b97804c37d1 | 94 | send(cid, "\r\n.\r\n", 5); |
gsfan | 18:4b97804c37d1 | 95 | if (wait_smtp(cid ,250)) goto quit; |
gsfan | 18:4b97804c37d1 | 96 | ret = 0; |
gsfan | 18:4b97804c37d1 | 97 | |
gsfan | 32:e19538c1f13d | 98 | LOG("Mail, from: %s, to: %s %d\r\n", from, to, strlen(mesg)); |
gsfan | 21:1270827d431a | 99 | |
gsfan | 18:4b97804c37d1 | 100 | quit: |
gsfan | 18:4b97804c37d1 | 101 | send(cid, "QUIT\r\n", 6); |
gsfan | 18:4b97804c37d1 | 102 | if (wait_smtp(cid ,221)) goto exit; |
gsfan | 18:4b97804c37d1 | 103 | |
gsfan | 18:4b97804c37d1 | 104 | exit: |
gsfan | 18:4b97804c37d1 | 105 | close(cid); |
gsfan | 18:4b97804c37d1 | 106 | return ret; |
gsfan | 18:4b97804c37d1 | 107 | } |
gsfan | 18:4b97804c37d1 | 108 | |
gsfan | 18:4b97804c37d1 | 109 | int GSwifi::wait_smtp (int cid, int code) { |
gsfan | 18:4b97804c37d1 | 110 | Timer timeout; |
gsfan | 18:4b97804c37d1 | 111 | int i, n, len = 0; |
gsfan | 20:151b5a4fdd29 | 112 | char buf[200], data[100]; |
gsfan | 18:4b97804c37d1 | 113 | |
gsfan | 32:e19538c1f13d | 114 | if (code == 0) { |
gsfan | 32:e19538c1f13d | 115 | // dummy read |
gsfan | 32:e19538c1f13d | 116 | timeout.start(); |
gsfan | 32:e19538c1f13d | 117 | while (timeout.read_ms() < GS_TIMEOUT) { |
gsfan | 32:e19538c1f13d | 118 | wait_ms(10); |
gsfan | 32:e19538c1f13d | 119 | if (_gs_sock[cid].data->isEmpty()) break; |
gsfan | 32:e19538c1f13d | 120 | poll(); |
gsfan | 32:e19538c1f13d | 121 | n = recv(cid, buf, sizeof(buf)); |
gsfan | 32:e19538c1f13d | 122 | if (n <= 0) break; |
gsfan | 32:e19538c1f13d | 123 | } |
gsfan | 32:e19538c1f13d | 124 | timeout.stop(); |
gsfan | 32:e19538c1f13d | 125 | return 0; |
gsfan | 32:e19538c1f13d | 126 | } |
gsfan | 32:e19538c1f13d | 127 | |
gsfan | 18:4b97804c37d1 | 128 | // wait responce |
gsfan | 32:e19538c1f13d | 129 | len = 0; |
gsfan | 18:4b97804c37d1 | 130 | timeout.start(); |
gsfan | 18:4b97804c37d1 | 131 | while (timeout.read_ms() < SMTP_TIMEOUT) { |
gsfan | 29:1c4419512941 | 132 | wait_ms(10); |
gsfan | 18:4b97804c37d1 | 133 | poll(); |
gsfan | 18:4b97804c37d1 | 134 | n = recv(cid, buf, sizeof(buf)); |
gsfan | 18:4b97804c37d1 | 135 | for (i = 0; i < n; i ++) { |
gsfan | 18:4b97804c37d1 | 136 | if (buf[i] == '\r') continue; |
gsfan | 18:4b97804c37d1 | 137 | if (buf[i] == '\n') { |
gsfan | 18:4b97804c37d1 | 138 | if (len == 0) continue; |
gsfan | 18:4b97804c37d1 | 139 | goto next; |
gsfan | 18:4b97804c37d1 | 140 | } else |
gsfan | 18:4b97804c37d1 | 141 | if (len < sizeof(data) - 1) { |
gsfan | 18:4b97804c37d1 | 142 | data[len] = buf[i]; |
gsfan | 18:4b97804c37d1 | 143 | len ++; |
gsfan | 18:4b97804c37d1 | 144 | } |
gsfan | 18:4b97804c37d1 | 145 | } |
gsfan | 18:4b97804c37d1 | 146 | } |
gsfan | 18:4b97804c37d1 | 147 | next: |
gsfan | 18:4b97804c37d1 | 148 | data[len] = 0; |
gsfan | 18:4b97804c37d1 | 149 | DBG("smtp: %s\r\n", data); |
gsfan | 18:4b97804c37d1 | 150 | timeout.stop(); |
gsfan | 18:4b97804c37d1 | 151 | |
gsfan | 18:4b97804c37d1 | 152 | // check return code |
gsfan | 18:4b97804c37d1 | 153 | i = atoi(data); |
gsfan | 18:4b97804c37d1 | 154 | DBG("smtp status %d\r\n", i); |
gsfan | 18:4b97804c37d1 | 155 | if (i == code) return 0; |
gsfan | 18:4b97804c37d1 | 156 | |
gsfan | 18:4b97804c37d1 | 157 | return -1; |
gsfan | 18:4b97804c37d1 | 158 | } |
gsfan | 20:151b5a4fdd29 | 159 | |
gsfan | 20:151b5a4fdd29 | 160 | #endif |