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 gs fan

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/

/media/uploads/gsfan/gs_im_002.jpg /media/uploads/gsfan/gs1011m_2.jpg

ゲインスパン Wi-Fi モジュール ライブラリ

ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011 シリーズ用のライブラリです。

解説: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Committer:
gsfan
Date:
Wed Dec 18 01:29:43 2013 +0000
Revision:
43:0b5e2727e020
Parent:
35:515ec79792d3
fix reconnect

Who changed what in which revision?

UserRevisionLine numberNew 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 35:515ec79792d3 23 #include "GSwifi_conf.h"
gsfan 35:515ec79792d3 24 #ifdef GS_ENABLE_SMTP
gsfan 35:515ec79792d3 25
gsfan 18:4b97804c37d1 26 #include "dbg.h"
gsfan 18:4b97804c37d1 27 #include "mbed.h"
gsfan 18:4b97804c37d1 28 #include "GSwifi.h"
gsfan 18:4b97804c37d1 29
gsfan 19:cad912f5a6ba 30 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 31 int ret = -1;
gsfan 18:4b97804c37d1 32 int cid;
gsfan 18:4b97804c37d1 33
gsfan 18:4b97804c37d1 34 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 18:4b97804c37d1 35
gsfan 18:4b97804c37d1 36 if (host.getIp().isNull()) {
gsfan 18:4b97804c37d1 37 if (getHostByName(host)) {
gsfan 18:4b97804c37d1 38 if (getHostByName(host)) return -1;
gsfan 18:4b97804c37d1 39 }
gsfan 18:4b97804c37d1 40 }
gsfan 18:4b97804c37d1 41 if (host.getPort() == 0) {
gsfan 18:4b97804c37d1 42 host.setPort(25);
gsfan 18:4b97804c37d1 43 }
gsfan 18:4b97804c37d1 44
gsfan 18:4b97804c37d1 45 cid = open(host, GSPROT_TCP);
gsfan 18:4b97804c37d1 46 if (cid < 0) return -1;
gsfan 18:4b97804c37d1 47 DBG("cid %d\r\n", cid);
gsfan 18:4b97804c37d1 48
gsfan 18:4b97804c37d1 49 if (wait_smtp(cid ,220)) goto exit;
gsfan 18:4b97804c37d1 50
gsfan 18:4b97804c37d1 51 // send request
gsfan 18:4b97804c37d1 52 send(cid, "EHLO gainspan\r\n", 15);
gsfan 18:4b97804c37d1 53 wait_ms(100);
gsfan 18:4b97804c37d1 54 if (wait_smtp(cid ,250)) goto exit;
gsfan 32:e19538c1f13d 55 wait_smtp(cid ,0);
gsfan 18:4b97804c37d1 56
gsfan 18:4b97804c37d1 57 if (user && pwd) {
gsfan 18:4b97804c37d1 58 // smtp auth
gsfan 18:4b97804c37d1 59 char tmp[80], buf[100];
gsfan 18:4b97804c37d1 60 int len;
gsfan 18:4b97804c37d1 61 snprintf(tmp, sizeof(tmp), "%s%c%s%c%s", user, 0, user, 0, pwd);
gsfan 18:4b97804c37d1 62 len = strlen(user) * 2 + strlen(pwd) + 2;
gsfan 18:4b97804c37d1 63 base64encode(tmp, len, buf, sizeof(buf));
gsfan 18:4b97804c37d1 64 send(cid, "AUTH PLAIN ", 11);
gsfan 18:4b97804c37d1 65 send(cid, buf, strlen(buf));
gsfan 18:4b97804c37d1 66 send(cid, "\r\n", 2);
gsfan 18:4b97804c37d1 67 if (wait_smtp(cid ,235)) goto quit;
gsfan 18:4b97804c37d1 68 }
gsfan 18:4b97804c37d1 69
gsfan 18:4b97804c37d1 70 send(cid, "MAIL FROM: ", 11);
gsfan 18:4b97804c37d1 71 send(cid, from, strlen(from));
gsfan 18:4b97804c37d1 72 send(cid, "\r\n", 2);
gsfan 18:4b97804c37d1 73 if (wait_smtp(cid ,250)) goto quit;
gsfan 18:4b97804c37d1 74
gsfan 18:4b97804c37d1 75 send(cid, "RCPT TO: ", 9);
gsfan 18:4b97804c37d1 76 send(cid, to, strlen(to));
gsfan 18:4b97804c37d1 77 send(cid, "\r\n", 2);
gsfan 18:4b97804c37d1 78 if (wait_smtp(cid ,250)) goto quit;
gsfan 18:4b97804c37d1 79
gsfan 18:4b97804c37d1 80 send(cid, "DATA\r\n", 6);
gsfan 18:4b97804c37d1 81 if (wait_smtp(cid ,354)) goto quit;
gsfan 18:4b97804c37d1 82
gsfan 18:4b97804c37d1 83 // mail data
gsfan 32:e19538c1f13d 84 send(cid, "From: ", 6);
gsfan 32:e19538c1f13d 85 send(cid, from, strlen(from));
gsfan 32:e19538c1f13d 86 send(cid, "\r\n", 2);
gsfan 32:e19538c1f13d 87 send(cid, "To: ", 9);
gsfan 32:e19538c1f13d 88 send(cid, to, strlen(to));
gsfan 32:e19538c1f13d 89 send(cid, "\r\n", 2);
gsfan 32:e19538c1f13d 90 send(cid, "Subject: ", 9);
gsfan 32:e19538c1f13d 91 send(cid, subject, strlen(subject));
gsfan 32:e19538c1f13d 92 send(cid, "\r\n\r\n", 4);
gsfan 32:e19538c1f13d 93
gsfan 18:4b97804c37d1 94 send(cid, mesg, strlen(mesg));
gsfan 18:4b97804c37d1 95 send(cid, "\r\n.\r\n", 5);
gsfan 18:4b97804c37d1 96 if (wait_smtp(cid ,250)) goto quit;
gsfan 18:4b97804c37d1 97 ret = 0;
gsfan 18:4b97804c37d1 98
gsfan 32:e19538c1f13d 99 LOG("Mail, from: %s, to: %s %d\r\n", from, to, strlen(mesg));
gsfan 21:1270827d431a 100
gsfan 18:4b97804c37d1 101 quit:
gsfan 18:4b97804c37d1 102 send(cid, "QUIT\r\n", 6);
gsfan 18:4b97804c37d1 103 if (wait_smtp(cid ,221)) goto exit;
gsfan 18:4b97804c37d1 104
gsfan 18:4b97804c37d1 105 exit:
gsfan 18:4b97804c37d1 106 close(cid);
gsfan 18:4b97804c37d1 107 return ret;
gsfan 18:4b97804c37d1 108 }
gsfan 18:4b97804c37d1 109
gsfan 18:4b97804c37d1 110 int GSwifi::wait_smtp (int cid, int code) {
gsfan 18:4b97804c37d1 111 Timer timeout;
gsfan 18:4b97804c37d1 112 int i, n, len = 0;
gsfan 20:151b5a4fdd29 113 char buf[200], data[100];
gsfan 18:4b97804c37d1 114
gsfan 32:e19538c1f13d 115 if (code == 0) {
gsfan 32:e19538c1f13d 116 // dummy read
gsfan 32:e19538c1f13d 117 timeout.start();
gsfan 32:e19538c1f13d 118 while (timeout.read_ms() < GS_TIMEOUT) {
gsfan 32:e19538c1f13d 119 wait_ms(10);
gsfan 32:e19538c1f13d 120 if (_gs_sock[cid].data->isEmpty()) break;
gsfan 32:e19538c1f13d 121 poll();
gsfan 32:e19538c1f13d 122 n = recv(cid, buf, sizeof(buf));
gsfan 32:e19538c1f13d 123 if (n <= 0) break;
gsfan 32:e19538c1f13d 124 }
gsfan 32:e19538c1f13d 125 timeout.stop();
gsfan 32:e19538c1f13d 126 return 0;
gsfan 32:e19538c1f13d 127 }
gsfan 32:e19538c1f13d 128
gsfan 18:4b97804c37d1 129 // wait responce
gsfan 32:e19538c1f13d 130 len = 0;
gsfan 18:4b97804c37d1 131 timeout.start();
gsfan 18:4b97804c37d1 132 while (timeout.read_ms() < SMTP_TIMEOUT) {
gsfan 29:1c4419512941 133 wait_ms(10);
gsfan 18:4b97804c37d1 134 poll();
gsfan 18:4b97804c37d1 135 n = recv(cid, buf, sizeof(buf));
gsfan 18:4b97804c37d1 136 for (i = 0; i < n; i ++) {
gsfan 18:4b97804c37d1 137 if (buf[i] == '\r') continue;
gsfan 18:4b97804c37d1 138 if (buf[i] == '\n') {
gsfan 18:4b97804c37d1 139 if (len == 0) continue;
gsfan 18:4b97804c37d1 140 goto next;
gsfan 18:4b97804c37d1 141 } else
gsfan 18:4b97804c37d1 142 if (len < sizeof(data) - 1) {
gsfan 18:4b97804c37d1 143 data[len] = buf[i];
gsfan 18:4b97804c37d1 144 len ++;
gsfan 18:4b97804c37d1 145 }
gsfan 18:4b97804c37d1 146 }
gsfan 18:4b97804c37d1 147 }
gsfan 18:4b97804c37d1 148 next:
gsfan 18:4b97804c37d1 149 data[len] = 0;
gsfan 18:4b97804c37d1 150 DBG("smtp: %s\r\n", data);
gsfan 18:4b97804c37d1 151 timeout.stop();
gsfan 18:4b97804c37d1 152
gsfan 18:4b97804c37d1 153 // check return code
gsfan 18:4b97804c37d1 154 i = atoi(data);
gsfan 18:4b97804c37d1 155 DBG("smtp status %d\r\n", i);
gsfan 18:4b97804c37d1 156 if (i == code) return 0;
gsfan 18:4b97804c37d1 157
gsfan 18:4b97804c37d1 158 return -1;
gsfan 18:4b97804c37d1 159 }
gsfan 20:151b5a4fdd29 160
gsfan 20:151b5a4fdd29 161 #endif