WiflyInterface library (interface for Roving Networks Wifly modules)

Dependents:   IOT-Websocket_Wifly_HelloWorld WiFly_NSDL_HelloWorld IOT-Project-LED-ControlTelnet IOT-Project-Wifly-Xively ... more

Fork of WiflyInterface by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPSocketConnection.cpp Source File

TCPSocketConnection.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 "TCPSocketConnection.h"
00020 #include <algorithm>
00021 
00022 TCPSocketConnection::TCPSocketConnection() {}
00023 
00024 int TCPSocketConnection::connect(const char* host, const int port)
00025 {  
00026     if (!wifi->connect(host, port))
00027         return -1;
00028     wifi->flush();
00029     return 0;
00030 }
00031 
00032 bool TCPSocketConnection::is_connected(void)
00033 {
00034     return wifi->is_connected();
00035 }
00036 
00037 int TCPSocketConnection::send(char* data, int length)
00038 {
00039     Timer tmr;
00040 
00041     if (!_blocking) {
00042         tmr.start();
00043         while (tmr.read_ms() < _timeout) {
00044             if (wifi->writeable())
00045                 break;
00046         }
00047         if (tmr.read_ms() >= _timeout) {
00048             return -1;
00049         }
00050     }
00051     return wifi->send(data, length);
00052 }
00053 
00054 // -1 if unsuccessful, else number of bytes written
00055 int TCPSocketConnection::send_all(char* data, int length)
00056 {
00057     Timer tmr;
00058     int idx = 0;
00059     tmr.start();
00060 
00061     while ((tmr.read_ms() < _timeout) || _blocking) {
00062 
00063         idx += wifi->send(data, length);
00064 
00065         if (idx == length)
00066             return idx;
00067     }
00068     return (idx == 0) ? -1 : idx;
00069 }
00070 
00071 // -1 if unsuccessful, else number of bytes received
00072 int TCPSocketConnection::receive(char* data, int length)
00073 {
00074     Timer tmr;
00075     int time = -1;
00076     
00077     
00078     if (!_blocking) {
00079         tmr.start();
00080         while (time < _timeout + 20) {
00081             if (wifi->readable()) {
00082                 break;
00083             }
00084             time = tmr.read_ms();
00085         }
00086         if (time >= _timeout + 20) {
00087             return -1;
00088         }
00089     }
00090 
00091 
00092     while(!wifi->readable());
00093     int nb_available = wifi->readable();
00094     for (int i = 0; i < min(nb_available, length); i++) {
00095         data[i] = wifi->getc();
00096     }
00097 
00098     return min(nb_available, length);
00099 }
00100 
00101 
00102 // -1 if unsuccessful, else number of bytes received
00103 int TCPSocketConnection::receive_all(char* data, int length)
00104 {
00105     Timer tmr;
00106     int idx = 0;
00107     int time = -1;
00108 
00109     tmr.start();
00110     
00111     while (time < _timeout || _blocking) {
00112 
00113         int nb_available = wifi->readable();
00114         for (int i = 0; i < min(nb_available, length); i++) {
00115             data[idx++] = wifi->getc();
00116         }
00117 
00118         if (idx == length)
00119             break;
00120 
00121         time = tmr.read_ms();
00122     }
00123 
00124     return (idx == 0) ? -1 : idx;
00125 }