- Added setBaud() function - Added CheckNetworkStatus() function - Improved messaging system

Dependents:   IoT_Ex BatteryModelTester BatteryModelTester

Fork of WiflyInterface by Components

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