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:
Mon Feb 11 06:01:46 2013 +0000
Revision:
25:f6e5622d2930
Parent:
21:1270827d431a
Child:
29:1c4419512941
split the file

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 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 18:4b97804c37d1 54
gsfan 18:4b97804c37d1 55 if (user && pwd) {
gsfan 18:4b97804c37d1 56 // smtp auth
gsfan 18:4b97804c37d1 57 char tmp[80], buf[100];
gsfan 18:4b97804c37d1 58 int len;
gsfan 18:4b97804c37d1 59 snprintf(tmp, sizeof(tmp), "%s%c%s%c%s", user, 0, user, 0, pwd);
gsfan 18:4b97804c37d1 60 len = strlen(user) * 2 + strlen(pwd) + 2;
gsfan 18:4b97804c37d1 61 base64encode(tmp, len, buf, sizeof(buf));
gsfan 18:4b97804c37d1 62 send(cid, "AUTH PLAIN ", 11);
gsfan 18:4b97804c37d1 63 send(cid, buf, strlen(buf));
gsfan 18:4b97804c37d1 64 send(cid, "\r\n", 2);
gsfan 18:4b97804c37d1 65 if (wait_smtp(cid ,235)) goto quit;
gsfan 18:4b97804c37d1 66 }
gsfan 18:4b97804c37d1 67
gsfan 18:4b97804c37d1 68 send(cid, "MAIL FROM: ", 11);
gsfan 18:4b97804c37d1 69 send(cid, from, strlen(from));
gsfan 18:4b97804c37d1 70 send(cid, "\r\n", 2);
gsfan 18:4b97804c37d1 71 if (wait_smtp(cid ,250)) goto quit;
gsfan 18:4b97804c37d1 72
gsfan 18:4b97804c37d1 73 send(cid, "RCPT TO: ", 9);
gsfan 18:4b97804c37d1 74 send(cid, to, strlen(to));
gsfan 18:4b97804c37d1 75 send(cid, "\r\n", 2);
gsfan 18:4b97804c37d1 76 if (wait_smtp(cid ,250)) goto quit;
gsfan 18:4b97804c37d1 77
gsfan 18:4b97804c37d1 78 send(cid, "DATA\r\n", 6);
gsfan 18:4b97804c37d1 79 if (wait_smtp(cid ,354)) goto quit;
gsfan 18:4b97804c37d1 80
gsfan 18:4b97804c37d1 81 // mail data
gsfan 18:4b97804c37d1 82 send(cid, mesg, strlen(mesg));
gsfan 18:4b97804c37d1 83 send(cid, "\r\n.\r\n", 5);
gsfan 18:4b97804c37d1 84 if (wait_smtp(cid ,250)) goto quit;
gsfan 18:4b97804c37d1 85 ret = 0;
gsfan 18:4b97804c37d1 86
gsfan 21:1270827d431a 87 LOG("From: %s To: %s %d\r\n", from, to, strlen(mesg));
gsfan 21:1270827d431a 88
gsfan 18:4b97804c37d1 89 quit:
gsfan 18:4b97804c37d1 90 send(cid, "QUIT\r\n", 6);
gsfan 18:4b97804c37d1 91 if (wait_smtp(cid ,221)) goto exit;
gsfan 18:4b97804c37d1 92
gsfan 18:4b97804c37d1 93 exit:
gsfan 18:4b97804c37d1 94 close(cid);
gsfan 18:4b97804c37d1 95 return ret;
gsfan 18:4b97804c37d1 96 }
gsfan 18:4b97804c37d1 97
gsfan 18:4b97804c37d1 98 int GSwifi::wait_smtp (int cid, int code) {
gsfan 18:4b97804c37d1 99 Timer timeout;
gsfan 18:4b97804c37d1 100 int i, n, len = 0;
gsfan 20:151b5a4fdd29 101 char buf[200], data[100];
gsfan 18:4b97804c37d1 102
gsfan 18:4b97804c37d1 103 // wait responce
gsfan 18:4b97804c37d1 104 timeout.start();
gsfan 18:4b97804c37d1 105 while (timeout.read_ms() < SMTP_TIMEOUT) {
gsfan 18:4b97804c37d1 106 wait_ms(100);
gsfan 18:4b97804c37d1 107 poll();
gsfan 18:4b97804c37d1 108 n = recv(cid, buf, sizeof(buf));
gsfan 18:4b97804c37d1 109 for (i = 0; i < n; i ++) {
gsfan 18:4b97804c37d1 110 if (buf[i] == '\r') continue;
gsfan 18:4b97804c37d1 111 if (buf[i] == '\n') {
gsfan 18:4b97804c37d1 112 if (len == 0) continue;
gsfan 18:4b97804c37d1 113 goto next;
gsfan 18:4b97804c37d1 114 } else
gsfan 18:4b97804c37d1 115 if (len < sizeof(data) - 1) {
gsfan 18:4b97804c37d1 116 data[len] = buf[i];
gsfan 18:4b97804c37d1 117 len ++;
gsfan 18:4b97804c37d1 118 }
gsfan 18:4b97804c37d1 119 }
gsfan 18:4b97804c37d1 120 }
gsfan 18:4b97804c37d1 121 next:
gsfan 18:4b97804c37d1 122 data[len] = 0;
gsfan 18:4b97804c37d1 123 len = 0;
gsfan 18:4b97804c37d1 124 DBG("smtp: %s\r\n", data);
gsfan 18:4b97804c37d1 125 timeout.stop();
gsfan 18:4b97804c37d1 126
gsfan 18:4b97804c37d1 127 // check return code
gsfan 18:4b97804c37d1 128 i = atoi(data);
gsfan 18:4b97804c37d1 129 DBG("smtp status %d\r\n", i);
gsfan 18:4b97804c37d1 130 if (i == code) return 0;
gsfan 18:4b97804c37d1 131
gsfan 18:4b97804c37d1 132 return -1;
gsfan 18:4b97804c37d1 133 }
gsfan 20:151b5a4fdd29 134
gsfan 20:151b5a4fdd29 135 #endif