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
- Committer:
- gsfan
- Date:
- 2013-02-26
- Revision:
- 32:e19538c1f13d
- Parent:
- 29:1c4419512941
- Child:
- 35:515ec79792d3
File content as of revision 32:e19538c1f13d:
/* Copyright (C) 2013 gsfan, MIT License * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** @file * @brief Gainspan wi-fi module library for mbed * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc. */ #include "dbg.h" #include "mbed.h" #include "GSwifi.h" #ifdef GS_USE_SMTP int GSwifi::mail (Host &host, const char *to, const char *from, const char *subject, const char *mesg, const char *user, const char *pwd) { int ret = -1; int cid; if (! _connect || _status != GSSTAT_READY) return -1; if (host.getIp().isNull()) { if (getHostByName(host)) { if (getHostByName(host)) return -1; } } if (host.getPort() == 0) { host.setPort(25); } cid = open(host, GSPROT_TCP); if (cid < 0) return -1; DBG("cid %d\r\n", cid); if (wait_smtp(cid ,220)) goto exit; // send request send(cid, "EHLO gainspan\r\n", 15); wait_ms(100); if (wait_smtp(cid ,250)) goto exit; wait_smtp(cid ,0); if (user && pwd) { // smtp auth char tmp[80], buf[100]; int len; snprintf(tmp, sizeof(tmp), "%s%c%s%c%s", user, 0, user, 0, pwd); len = strlen(user) * 2 + strlen(pwd) + 2; base64encode(tmp, len, buf, sizeof(buf)); send(cid, "AUTH PLAIN ", 11); send(cid, buf, strlen(buf)); send(cid, "\r\n", 2); if (wait_smtp(cid ,235)) goto quit; } send(cid, "MAIL FROM: ", 11); send(cid, from, strlen(from)); send(cid, "\r\n", 2); if (wait_smtp(cid ,250)) goto quit; send(cid, "RCPT TO: ", 9); send(cid, to, strlen(to)); send(cid, "\r\n", 2); if (wait_smtp(cid ,250)) goto quit; send(cid, "DATA\r\n", 6); if (wait_smtp(cid ,354)) goto quit; // mail data send(cid, "From: ", 6); send(cid, from, strlen(from)); send(cid, "\r\n", 2); send(cid, "To: ", 9); send(cid, to, strlen(to)); send(cid, "\r\n", 2); send(cid, "Subject: ", 9); send(cid, subject, strlen(subject)); send(cid, "\r\n\r\n", 4); send(cid, mesg, strlen(mesg)); send(cid, "\r\n.\r\n", 5); if (wait_smtp(cid ,250)) goto quit; ret = 0; LOG("Mail, from: %s, to: %s %d\r\n", from, to, strlen(mesg)); quit: send(cid, "QUIT\r\n", 6); if (wait_smtp(cid ,221)) goto exit; exit: close(cid); return ret; } int GSwifi::wait_smtp (int cid, int code) { Timer timeout; int i, n, len = 0; char buf[200], data[100]; if (code == 0) { // dummy read timeout.start(); while (timeout.read_ms() < GS_TIMEOUT) { wait_ms(10); if (_gs_sock[cid].data->isEmpty()) break; poll(); n = recv(cid, buf, sizeof(buf)); if (n <= 0) break; } timeout.stop(); return 0; } // wait responce len = 0; timeout.start(); while (timeout.read_ms() < SMTP_TIMEOUT) { wait_ms(10); poll(); n = recv(cid, buf, sizeof(buf)); for (i = 0; i < n; i ++) { if (buf[i] == '\r') continue; if (buf[i] == '\n') { if (len == 0) continue; goto next; } else if (len < sizeof(data) - 1) { data[len] = buf[i]; len ++; } } } next: data[len] = 0; DBG("smtp: %s\r\n", data); timeout.stop(); // check return code i = atoi(data); DBG("smtp status %d\r\n", i); if (i == code) return 0; return -1; } #endif