It is the project that wifi run on the rtos
Fork of WiflyInterface by
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 "rtos.h" 00021 #include <algorithm> 00022 00023 TCPSocketConnection::TCPSocketConnection() {} 00024 00025 int TCPSocketConnection::connect(const char* host, const int port) 00026 { 00027 return (base_level_connect(host,port)); 00028 } 00029 00030 bool TCPSocketConnection::is_connected(void) 00031 { 00032 return base_level_is_connected(); 00033 00034 } 00035 00036 int TCPSocketConnection::send(char* data, int length) 00037 { 00038 // Timer tmr; 00039 00040 // if (!_blocking) { 00041 // tmr.start(); 00042 // while (tmr.read_ms() < _timeout) { 00043 // if (wifi->writeable()) 00044 // break; 00045 // } 00046 // if (tmr.read_ms() >= _timeout) { 00047 // return -1; 00048 // } 00049 // } 00050 // return wifi->send(data, length); 00051 00052 } 00053 00054 // -1 if unsuccessful, else number of bytes written 00055 int TCPSocketConnection::send_all(char* data, int length) 00056 { 00057 00058 Timer tmr; 00059 int idx = 0; 00060 globaldata.send_length=length; 00061 memcpy(globaldata.send_buffer,data,length); 00062 globaldata.send_ready=1; 00063 tmr.start(); 00064 00065 while ((tmr.read_ms() < _timeout) || _blocking) { 00066 00067 //idx += wifi->send(data, length); 00068 00069 // idx+=baselevel_send_data(data,length); 00070 // if (idx == length) 00071 // return idx; 00072 if(globaldata.send_completed==1) 00073 { 00074 globaldata.send_completed=0; 00075 memset(globaldata.send_buffer,0,600*sizeof(char)); 00076 if(globaldata.send_length_over==length) 00077 return length; 00078 00079 } 00080 //return (idx == 0) ? -1 : idx; 00081 } 00082 return (globaldata.send_length_over==0) ? -1 : globaldata.send_length_over; 00083 00084 } 00085 00086 // -1 if unsuccessful, else number of bytes received 00087 int TCPSocketConnection::receive(char* data, int length) 00088 { 00089 Timer tmr; 00090 int time = -1; 00091 int idx=0; 00092 00093 if (!_blocking) { 00094 globaldata.need_receive=length; 00095 globaldata.receive_start=1; 00096 tmr.start(); 00097 while (time < _timeout + 30) { 00098 if (globaldata.receive_completed==1) { 00099 globaldata.receive_completed=0; 00100 globaldata.receive_start=0; 00101 break; 00102 } 00103 00104 time = tmr.read_ms(); 00105 } 00106 if (time >= _timeout + 30) { 00107 return -1; 00108 } 00109 } 00110 00111 00112 // while(!wifi->readable()); 00113 // int nb_available = wifi->readable(); 00114 // for (int i = 0; i < min(nb_available, length); i++) { 00115 // data[i] = wifi->getc(); 00116 //} 00117 //int nb_available=baselevel_receive_data(data, length); 00118 00119 00120 // return min(nb_available, length); 00121 for(int i=0;i<globaldata.receive_length;i++) 00122 if(globaldata.receive_buffer[i]!=0) 00123 data[idx++]=globaldata.receive_buffer[i]; 00124 globaldata.receive_completed=0; 00125 00126 return min(globaldata.receive_length, length); 00127 00128 } 00129 00130 00131 // -1 if unsuccessful, else number of bytes received 00132 int TCPSocketConnection::receive_all(char* data, int length) 00133 { 00134 00135 Timer tmr; 00136 int idx = 0; 00137 int time = -1; 00138 globaldata.receive_start=1; 00139 globaldata.need_receive=length; 00140 tmr.start(); 00141 00142 while (time < _timeout || _blocking) { 00143 00144 // int nb_available = wifi->readable(); 00145 // for (int i = 0; i < min(nb_available, length); i++) { 00146 // data[idx++] = wifi->getc(); 00147 // } 00148 // 00149 // my code: 00150 //idx=baselevel_receive_data(data,length); 00151 00152 // if (idx == length) 00153 // break; 00154 00155 if(globaldata.receive_completed==1) 00156 { 00157 for(int i=0;i<globaldata.receive_length;i++) 00158 if(globaldata.receive_buffer[i]!=0) 00159 data[idx++]=globaldata.receive_buffer[i]; 00160 globaldata.receive_completed=0; 00161 } 00162 if(idx==length) 00163 break; 00164 00165 time = tmr.read_ms(); 00166 } 00167 00168 //return (idx == 0) ? -1 : idx; 00169 return (idx==0) ? -1 : idx; 00170 00171 00172 00173 }
Generated on Fri Jul 15 2022 17:51:39 by 1.7.2