It is the project that wifi run on the rtos

Fork of WiflyInterface by Mbed

Committer:
shiyilei
Date:
Fri Dec 05 02:10:34 2014 +0000
Revision:
10:8a57e2ed20a4
Parent:
1:fb4494783863
It is the wifi project run on the rtos

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:fb4494783863 1 /* Copyright (C) 2012 mbed.org, MIT License
samux 1:fb4494783863 2 *
samux 1:fb4494783863 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 1:fb4494783863 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
samux 1:fb4494783863 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
samux 1:fb4494783863 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
samux 1:fb4494783863 7 * furnished to do so, subject to the following conditions:
samux 1:fb4494783863 8 *
samux 1:fb4494783863 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 1:fb4494783863 10 * substantial portions of the Software.
samux 1:fb4494783863 11 *
samux 1:fb4494783863 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 1:fb4494783863 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 1:fb4494783863 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 1:fb4494783863 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 1:fb4494783863 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 1:fb4494783863 17 */
samux 1:fb4494783863 18
samux 1:fb4494783863 19 #include "TCPSocketConnection.h"
shiyilei 10:8a57e2ed20a4 20 #include "rtos.h"
samux 1:fb4494783863 21 #include <algorithm>
samux 1:fb4494783863 22
samux 1:fb4494783863 23 TCPSocketConnection::TCPSocketConnection() {}
samux 1:fb4494783863 24
samux 1:fb4494783863 25 int TCPSocketConnection::connect(const char* host, const int port)
samux 1:fb4494783863 26 {
shiyilei 10:8a57e2ed20a4 27 return (base_level_connect(host,port));
samux 1:fb4494783863 28 }
samux 1:fb4494783863 29
samux 1:fb4494783863 30 bool TCPSocketConnection::is_connected(void)
samux 1:fb4494783863 31 {
shiyilei 10:8a57e2ed20a4 32 return base_level_is_connected();
shiyilei 10:8a57e2ed20a4 33
samux 1:fb4494783863 34 }
samux 1:fb4494783863 35
samux 1:fb4494783863 36 int TCPSocketConnection::send(char* data, int length)
samux 1:fb4494783863 37 {
shiyilei 10:8a57e2ed20a4 38 // Timer tmr;
samux 1:fb4494783863 39
shiyilei 10:8a57e2ed20a4 40 // if (!_blocking) {
shiyilei 10:8a57e2ed20a4 41 // tmr.start();
shiyilei 10:8a57e2ed20a4 42 // while (tmr.read_ms() < _timeout) {
shiyilei 10:8a57e2ed20a4 43 // if (wifi->writeable())
shiyilei 10:8a57e2ed20a4 44 // break;
shiyilei 10:8a57e2ed20a4 45 // }
shiyilei 10:8a57e2ed20a4 46 // if (tmr.read_ms() >= _timeout) {
shiyilei 10:8a57e2ed20a4 47 // return -1;
shiyilei 10:8a57e2ed20a4 48 // }
shiyilei 10:8a57e2ed20a4 49 // }
shiyilei 10:8a57e2ed20a4 50 // return wifi->send(data, length);
shiyilei 10:8a57e2ed20a4 51
samux 1:fb4494783863 52 }
samux 1:fb4494783863 53
samux 1:fb4494783863 54 // -1 if unsuccessful, else number of bytes written
samux 1:fb4494783863 55 int TCPSocketConnection::send_all(char* data, int length)
samux 1:fb4494783863 56 {
shiyilei 10:8a57e2ed20a4 57
samux 1:fb4494783863 58 Timer tmr;
samux 1:fb4494783863 59 int idx = 0;
shiyilei 10:8a57e2ed20a4 60 globaldata.send_length=length;
shiyilei 10:8a57e2ed20a4 61 memcpy(globaldata.send_buffer,data,length);
shiyilei 10:8a57e2ed20a4 62 globaldata.send_ready=1;
samux 1:fb4494783863 63 tmr.start();
samux 1:fb4494783863 64
samux 1:fb4494783863 65 while ((tmr.read_ms() < _timeout) || _blocking) {
samux 1:fb4494783863 66
shiyilei 10:8a57e2ed20a4 67 //idx += wifi->send(data, length);
samux 1:fb4494783863 68
shiyilei 10:8a57e2ed20a4 69 // idx+=baselevel_send_data(data,length);
shiyilei 10:8a57e2ed20a4 70 // if (idx == length)
shiyilei 10:8a57e2ed20a4 71 // return idx;
shiyilei 10:8a57e2ed20a4 72 if(globaldata.send_completed==1)
shiyilei 10:8a57e2ed20a4 73 {
shiyilei 10:8a57e2ed20a4 74 globaldata.send_completed=0;
shiyilei 10:8a57e2ed20a4 75 memset(globaldata.send_buffer,0,600*sizeof(char));
shiyilei 10:8a57e2ed20a4 76 if(globaldata.send_length_over==length)
shiyilei 10:8a57e2ed20a4 77 return length;
shiyilei 10:8a57e2ed20a4 78
shiyilei 10:8a57e2ed20a4 79 }
shiyilei 10:8a57e2ed20a4 80 //return (idx == 0) ? -1 : idx;
shiyilei 10:8a57e2ed20a4 81 }
shiyilei 10:8a57e2ed20a4 82 return (globaldata.send_length_over==0) ? -1 : globaldata.send_length_over;
shiyilei 10:8a57e2ed20a4 83
samux 1:fb4494783863 84 }
samux 1:fb4494783863 85
samux 1:fb4494783863 86 // -1 if unsuccessful, else number of bytes received
samux 1:fb4494783863 87 int TCPSocketConnection::receive(char* data, int length)
samux 1:fb4494783863 88 {
samux 1:fb4494783863 89 Timer tmr;
samux 1:fb4494783863 90 int time = -1;
shiyilei 10:8a57e2ed20a4 91 int idx=0;
samux 1:fb4494783863 92
samux 1:fb4494783863 93 if (!_blocking) {
shiyilei 10:8a57e2ed20a4 94 globaldata.need_receive=length;
shiyilei 10:8a57e2ed20a4 95 globaldata.receive_start=1;
samux 1:fb4494783863 96 tmr.start();
shiyilei 10:8a57e2ed20a4 97 while (time < _timeout + 30) {
shiyilei 10:8a57e2ed20a4 98 if (globaldata.receive_completed==1) {
shiyilei 10:8a57e2ed20a4 99 globaldata.receive_completed=0;
shiyilei 10:8a57e2ed20a4 100 globaldata.receive_start=0;
shiyilei 10:8a57e2ed20a4 101 break;
samux 1:fb4494783863 102 }
shiyilei 10:8a57e2ed20a4 103
samux 1:fb4494783863 104 time = tmr.read_ms();
samux 1:fb4494783863 105 }
shiyilei 10:8a57e2ed20a4 106 if (time >= _timeout + 30) {
samux 1:fb4494783863 107 return -1;
samux 1:fb4494783863 108 }
samux 1:fb4494783863 109 }
samux 1:fb4494783863 110
samux 1:fb4494783863 111
shiyilei 10:8a57e2ed20a4 112 // while(!wifi->readable());
shiyilei 10:8a57e2ed20a4 113 // int nb_available = wifi->readable();
shiyilei 10:8a57e2ed20a4 114 // for (int i = 0; i < min(nb_available, length); i++) {
shiyilei 10:8a57e2ed20a4 115 // data[i] = wifi->getc();
shiyilei 10:8a57e2ed20a4 116 //}
shiyilei 10:8a57e2ed20a4 117 //int nb_available=baselevel_receive_data(data, length);
shiyilei 10:8a57e2ed20a4 118
samux 1:fb4494783863 119
shiyilei 10:8a57e2ed20a4 120 // return min(nb_available, length);
shiyilei 10:8a57e2ed20a4 121 for(int i=0;i<globaldata.receive_length;i++)
shiyilei 10:8a57e2ed20a4 122 if(globaldata.receive_buffer[i]!=0)
shiyilei 10:8a57e2ed20a4 123 data[idx++]=globaldata.receive_buffer[i];
shiyilei 10:8a57e2ed20a4 124 globaldata.receive_completed=0;
shiyilei 10:8a57e2ed20a4 125
shiyilei 10:8a57e2ed20a4 126 return min(globaldata.receive_length, length);
shiyilei 10:8a57e2ed20a4 127
samux 1:fb4494783863 128 }
samux 1:fb4494783863 129
samux 1:fb4494783863 130
samux 1:fb4494783863 131 // -1 if unsuccessful, else number of bytes received
samux 1:fb4494783863 132 int TCPSocketConnection::receive_all(char* data, int length)
samux 1:fb4494783863 133 {
shiyilei 10:8a57e2ed20a4 134
shiyilei 10:8a57e2ed20a4 135 Timer tmr;
samux 1:fb4494783863 136 int idx = 0;
samux 1:fb4494783863 137 int time = -1;
shiyilei 10:8a57e2ed20a4 138 globaldata.receive_start=1;
shiyilei 10:8a57e2ed20a4 139 globaldata.need_receive=length;
samux 1:fb4494783863 140 tmr.start();
shiyilei 10:8a57e2ed20a4 141
samux 1:fb4494783863 142 while (time < _timeout || _blocking) {
samux 1:fb4494783863 143
shiyilei 10:8a57e2ed20a4 144 // int nb_available = wifi->readable();
shiyilei 10:8a57e2ed20a4 145 // for (int i = 0; i < min(nb_available, length); i++) {
shiyilei 10:8a57e2ed20a4 146 // data[idx++] = wifi->getc();
shiyilei 10:8a57e2ed20a4 147 // }
shiyilei 10:8a57e2ed20a4 148 //
shiyilei 10:8a57e2ed20a4 149 // my code:
shiyilei 10:8a57e2ed20a4 150 //idx=baselevel_receive_data(data,length);
shiyilei 10:8a57e2ed20a4 151
shiyilei 10:8a57e2ed20a4 152 // if (idx == length)
shiyilei 10:8a57e2ed20a4 153 // break;
shiyilei 10:8a57e2ed20a4 154
shiyilei 10:8a57e2ed20a4 155 if(globaldata.receive_completed==1)
shiyilei 10:8a57e2ed20a4 156 {
shiyilei 10:8a57e2ed20a4 157 for(int i=0;i<globaldata.receive_length;i++)
shiyilei 10:8a57e2ed20a4 158 if(globaldata.receive_buffer[i]!=0)
shiyilei 10:8a57e2ed20a4 159 data[idx++]=globaldata.receive_buffer[i];
shiyilei 10:8a57e2ed20a4 160 globaldata.receive_completed=0;
samux 1:fb4494783863 161 }
shiyilei 10:8a57e2ed20a4 162 if(idx==length)
samux 1:fb4494783863 163 break;
samux 1:fb4494783863 164
samux 1:fb4494783863 165 time = tmr.read_ms();
samux 1:fb4494783863 166 }
samux 1:fb4494783863 167
shiyilei 10:8a57e2ed20a4 168 //return (idx == 0) ? -1 : idx;
shiyilei 10:8a57e2ed20a4 169 return (idx==0) ? -1 : idx;
shiyilei 10:8a57e2ed20a4 170
shiyilei 10:8a57e2ed20a4 171
shiyilei 10:8a57e2ed20a4 172
samux 1:fb4494783863 173 }