Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Smart-WiFly-WebServer PUB_WiflyInterface_Demo
Fork of WiflyInterface by
TCPSocketServer.cpp
00001 /* Copyright (C) 2012 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "TCPSocketServer.h" 00020 #include <string> 00021 00022 TCPSocketServer::TCPSocketServer() 00023 { 00024 acceptIndex = 0; 00025 } 00026 00027 // Server initialization 00028 int TCPSocketServer::bind(int port) 00029 { 00030 char cmd[20]; 00031 00032 // set TCP protocol 00033 wifi->setProtocol(TCP); 00034 00035 // set ip local port # 00036 sprintf(cmd, "set i l %d\r", port); 00037 if (!wifi->sendCommand(cmd, "AOK")) 00038 return -1; 00039 00040 // save 00041 if (!wifi->sendCommand("save\r", "Stor", NULL, 5000)) 00042 return -1; 00043 00044 // reboot 00045 wifi->reboot(); 00046 00047 // connect the network 00048 if (wifi->isDHCP()) { 00049 if (!wifi->sendCommand("join\r", "DHCP=ON", NULL, 10000)) 00050 return -1; 00051 } else { 00052 if (!wifi->sendCommand("join\r", "Associated", NULL, 10000)) 00053 return -1; 00054 } 00055 00056 // exit 00057 wifi->exit(); 00058 00059 wait(0.2); 00060 wifi->flush(); 00061 return 0; 00062 } 00063 00064 int TCPSocketServer::listen(int backlog) 00065 { 00066 if (backlog != 1) 00067 return -1; 00068 return 0; 00069 } 00070 00071 // sometimes simply "*OPEN*" comes in, and sometimes 00072 // a prior connection closes too, so we get "*CLOS**OPEN*". 00073 // When accepting a connection from a browser, it can be 00074 // *CLOS**OPEN*GET / HTTP/1.1 00075 // The point here is to remove everything to the left 00076 // of, and including, "*OPEN*" in order to accept this 00077 // connection, and the "GET / HTTP/1.1" is available 00078 // for consumption. 00079 // 00080 int TCPSocketServer::accept(TCPSocketConnection& connection) 00081 { 00082 int nb_available = 0; 00083 char c; 00084 const char OPEN[] = "*OPEN*"; // seeking this to accept 00085 00086 acceptTimerStart(); 00087 while (1) { 00088 nb_available = wifi->readable(); 00089 if (nb_available == 0 && !_blocking && acceptTimeout()) 00090 return -1; 00091 for (int i = 0; i < nb_available; i++) { 00092 c = wifi->getc(); 00093 if (c != OPEN[acceptIndex]) 00094 acceptIndex = 0; 00095 if (c == OPEN[acceptIndex]) { 00096 acceptIndex++; 00097 if (acceptIndex == strlen(OPEN)) { 00098 wifi->setConnectionState(true); 00099 acceptIndex = 0; // for next pass 00100 return 0; 00101 } 00102 } 00103 } 00104 } 00105 } 00106 00107 void TCPSocketServer::acceptTimerStart() 00108 { 00109 acceptTimer.start(); 00110 } 00111 00112 bool TCPSocketServer::acceptTimeout() 00113 { 00114 if ((_timeout == 0) || (acceptTimer.read_ms() >= _timeout)) 00115 return true; 00116 else 00117 return false; 00118 }
Generated on Tue Jul 12 2022 16:14:58 by
1.7.2
