RC
Dependents: WizFi250_AP_HelloWorld
Fork of WizFi250Interface by
WizFi250/WizFi250_sock.cpp@9:12ecb377f63f, 2015-06-19 (annotated)
- Committer:
- kaizen8501
- Date:
- Fri Jun 19 15:34:40 2015 +0900
- Revision:
- 9:12ecb377f63f
- Parent:
- 0:f2039204c8f6
- Child:
- 12:bb8e979df6df
Fixed Bug when using TCP Server, TCP Client
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kaizen | 0:f2039204c8f6 | 1 | /* |
kaizen | 0:f2039204c8f6 | 2 | /* Copyright (C) 2013 gsfan, MIT License |
kaizen | 0:f2039204c8f6 | 3 | * |
kaizen | 0:f2039204c8f6 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
kaizen | 0:f2039204c8f6 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
kaizen | 0:f2039204c8f6 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
kaizen | 0:f2039204c8f6 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
kaizen | 0:f2039204c8f6 | 8 | * furnished to do so, subject to the following conditions: |
kaizen | 0:f2039204c8f6 | 9 | * |
kaizen | 0:f2039204c8f6 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
kaizen | 0:f2039204c8f6 | 11 | * substantial portions of the Software. |
kaizen | 0:f2039204c8f6 | 12 | * |
kaizen | 0:f2039204c8f6 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
kaizen | 0:f2039204c8f6 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
kaizen | 0:f2039204c8f6 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kaizen | 0:f2039204c8f6 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kaizen | 0:f2039204c8f6 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kaizen | 0:f2039204c8f6 | 18 | */ |
kaizen | 0:f2039204c8f6 | 19 | /* Copyright (C) 2014 Wiznet, MIT License |
kaizen | 0:f2039204c8f6 | 20 | * port to the Wiznet Module WizFi250 |
kaizen | 0:f2039204c8f6 | 21 | */ |
kaizen | 0:f2039204c8f6 | 22 | |
kaizen | 0:f2039204c8f6 | 23 | #include "WizFi250.h" |
kaizen | 0:f2039204c8f6 | 24 | |
kaizen | 0:f2039204c8f6 | 25 | |
kaizen | 0:f2039204c8f6 | 26 | int WizFi250::getHostByName(const char * host, char *ip) |
kaizen | 0:f2039204c8f6 | 27 | { |
kaizen | 0:f2039204c8f6 | 28 | int i, flg = 0; |
kaizen | 0:f2039204c8f6 | 29 | |
kaizen | 0:f2039204c8f6 | 30 | if(!isAssociated() || _state.status != STAT_READY) return -1; |
kaizen | 0:f2039204c8f6 | 31 | |
kaizen | 0:f2039204c8f6 | 32 | for(i=0; i<strlen(host); i++) |
kaizen | 0:f2039204c8f6 | 33 | { |
kaizen | 0:f2039204c8f6 | 34 | if( (host[i] < '0' || host[i] > '9') && host[i] != '.') |
kaizen | 0:f2039204c8f6 | 35 | { |
kaizen | 0:f2039204c8f6 | 36 | flg = 1; |
kaizen | 0:f2039204c8f6 | 37 | break; |
kaizen | 0:f2039204c8f6 | 38 | } |
kaizen | 0:f2039204c8f6 | 39 | } |
kaizen | 0:f2039204c8f6 | 40 | if (!flg) |
kaizen | 0:f2039204c8f6 | 41 | { |
kaizen | 0:f2039204c8f6 | 42 | strncpy(ip, host, 16); |
kaizen | 0:f2039204c8f6 | 43 | return 0; |
kaizen | 0:f2039204c8f6 | 44 | } |
kaizen | 0:f2039204c8f6 | 45 | |
kaizen | 0:f2039204c8f6 | 46 | if ( cmdFDNS(host) ) |
kaizen | 0:f2039204c8f6 | 47 | { |
kaizen | 0:f2039204c8f6 | 48 | wait_ms(1000); |
kaizen | 0:f2039204c8f6 | 49 | if( cmdFDNS(host) ) return -1; |
kaizen | 0:f2039204c8f6 | 50 | } |
kaizen | 0:f2039204c8f6 | 51 | strncpy(ip, _state.resolv, 16); |
kaizen | 0:f2039204c8f6 | 52 | return 0; |
kaizen | 0:f2039204c8f6 | 53 | } |
kaizen | 0:f2039204c8f6 | 54 | |
kaizen | 0:f2039204c8f6 | 55 | int WizFi250::open(Protocol proto, const char *ip, int remotePort, int localPort, void(*func)(int)) |
kaizen | 0:f2039204c8f6 | 56 | { |
kaizen | 0:f2039204c8f6 | 57 | int cid; |
kaizen | 0:f2039204c8f6 | 58 | |
kaizen | 0:f2039204c8f6 | 59 | if (!isAssociated() || _state.status != STAT_READY) return -1; |
kaizen | 0:f2039204c8f6 | 60 | |
kaizen | 0:f2039204c8f6 | 61 | _state.cid = -1; |
kaizen | 0:f2039204c8f6 | 62 | |
kaizen | 0:f2039204c8f6 | 63 | if (proto == PROTO_TCP) |
kaizen | 0:f2039204c8f6 | 64 | { |
kaizen | 0:f2039204c8f6 | 65 | if( cmdSCON( "O","TCN",ip, remotePort, localPort, "0" ) ) return -1; |
kaizen | 0:f2039204c8f6 | 66 | } |
kaizen | 0:f2039204c8f6 | 67 | else if(proto == PROTO_UDP) |
kaizen | 0:f2039204c8f6 | 68 | { |
kaizen | 0:f2039204c8f6 | 69 | if( cmdSCON( "O","UCN",ip, remotePort, localPort, "0" ) ) return -1; |
kaizen | 0:f2039204c8f6 | 70 | } |
kaizen | 0:f2039204c8f6 | 71 | if(_state.cid < 0) return -1; |
kaizen8501 |
9:12ecb377f63f | 72 | |
kaizen8501 |
9:12ecb377f63f | 73 | initCon(_state.cid, true); |
kaizen | 0:f2039204c8f6 | 74 | cid = _state.cid; |
kaizen | 0:f2039204c8f6 | 75 | _con[cid].protocol = proto; |
kaizen | 0:f2039204c8f6 | 76 | _con[cid].type = TYPE_CLIENT; |
kaizen | 0:f2039204c8f6 | 77 | _con[cid].func = func; |
kaizen | 0:f2039204c8f6 | 78 | return cid; |
kaizen | 0:f2039204c8f6 | 79 | } |
kaizen | 0:f2039204c8f6 | 80 | |
kaizen | 0:f2039204c8f6 | 81 | int WizFi250::listen (Protocol proto, int port, void(*func)(int)) |
kaizen | 0:f2039204c8f6 | 82 | { |
kaizen | 0:f2039204c8f6 | 83 | int cid; |
kaizen | 0:f2039204c8f6 | 84 | |
kaizen | 0:f2039204c8f6 | 85 | if(!isAssociated() || _state.status != STAT_READY) return -1; |
kaizen | 0:f2039204c8f6 | 86 | |
kaizen | 0:f2039204c8f6 | 87 | _state.cid = -1; |
kaizen | 0:f2039204c8f6 | 88 | |
kaizen | 0:f2039204c8f6 | 89 | if(proto == PROTO_TCP) |
kaizen | 0:f2039204c8f6 | 90 | { |
kaizen | 0:f2039204c8f6 | 91 | if( sendCommand("AT+MEVTMSG=1") ) return -1; |
kaizen | 0:f2039204c8f6 | 92 | if( cmdSCON("O","TSN",port) ) return -1; |
kaizen | 0:f2039204c8f6 | 93 | } |
kaizen | 0:f2039204c8f6 | 94 | else |
kaizen | 0:f2039204c8f6 | 95 | { |
kaizen | 0:f2039204c8f6 | 96 | if( cmdSCON("O","USN",port) ) return -1; |
kaizen | 0:f2039204c8f6 | 97 | } |
kaizen | 0:f2039204c8f6 | 98 | |
kaizen | 0:f2039204c8f6 | 99 | if (_state.cid < 0) return -1; |
kaizen | 0:f2039204c8f6 | 100 | cid = _state.cid; |
kaizen | 0:f2039204c8f6 | 101 | _con[cid].protocol = proto; |
kaizen | 0:f2039204c8f6 | 102 | _con[cid].type = TYPE_SERVER; |
kaizen | 0:f2039204c8f6 | 103 | _con[cid].func = func; |
kaizen | 0:f2039204c8f6 | 104 | |
kaizen | 0:f2039204c8f6 | 105 | return cid; |
kaizen | 0:f2039204c8f6 | 106 | } |
kaizen | 0:f2039204c8f6 | 107 | |
kaizen | 0:f2039204c8f6 | 108 | int WizFi250::close (int cid) |
kaizen | 0:f2039204c8f6 | 109 | { |
kaizen | 0:f2039204c8f6 | 110 | if(!isConnected(cid)) return -1; |
kaizen | 0:f2039204c8f6 | 111 | |
kaizen | 0:f2039204c8f6 | 112 | _con[cid].connected = false; |
kaizen | 0:f2039204c8f6 | 113 | return cmdCLOSE(cid); |
kaizen | 0:f2039204c8f6 | 114 | } |
kaizen | 0:f2039204c8f6 | 115 | |
kaizen | 0:f2039204c8f6 | 116 | |
kaizen | 0:f2039204c8f6 | 117 | void WizFi250::initCon ( int cid, bool connected ) |
kaizen | 0:f2039204c8f6 | 118 | { |
kaizen | 0:f2039204c8f6 | 119 | _con[cid].parent = -1; // It will be delete because It is not need |
kaizen | 0:f2039204c8f6 | 120 | _con[cid].func = NULL; |
kaizen | 0:f2039204c8f6 | 121 | _con[cid].accept = false; |
kaizen | 0:f2039204c8f6 | 122 | |
kaizen | 0:f2039204c8f6 | 123 | //#ifndef CFG_ENABLE_RTOS |
kaizen | 0:f2039204c8f6 | 124 | if ( _con[cid].buf == NULL ) |
kaizen | 0:f2039204c8f6 | 125 | { |
kaizen | 0:f2039204c8f6 | 126 | _con[cid].buf = new CircBuffer<char>(CFG_DATA_SIZE); |
kaizen | 0:f2039204c8f6 | 127 | if ( _con[cid].buf == NULL ) error("Can't allocate memory"); |
kaizen | 0:f2039204c8f6 | 128 | } |
kaizen | 0:f2039204c8f6 | 129 | //#endif |
kaizen | 0:f2039204c8f6 | 130 | if ( _con[cid].buf != NULL ) |
kaizen | 0:f2039204c8f6 | 131 | { |
kaizen | 0:f2039204c8f6 | 132 | _con[cid].buf->flush(); |
kaizen | 0:f2039204c8f6 | 133 | } |
kaizen | 0:f2039204c8f6 | 134 | _con[cid].connected = connected; |
kaizen | 0:f2039204c8f6 | 135 | } |
kaizen | 0:f2039204c8f6 | 136 | |
kaizen | 0:f2039204c8f6 | 137 | int WizFi250::send(int cid, const char *buf, int len) |
kaizen | 0:f2039204c8f6 | 138 | { |
kaizen | 0:f2039204c8f6 | 139 | if(!isConnected(cid)) return -1; |
kaizen | 0:f2039204c8f6 | 140 | |
kaizen | 0:f2039204c8f6 | 141 | if((_con[cid].protocol == PROTO_TCP) || |
kaizen | 0:f2039204c8f6 | 142 | (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) ) |
kaizen | 0:f2039204c8f6 | 143 | { |
kaizen | 0:f2039204c8f6 | 144 | // if ( len > CFG_DATA_SIZE) len = CFG_DATA_SIZE; |
kaizen | 0:f2039204c8f6 | 145 | return cmdSSEND(buf,cid,len); |
kaizen | 0:f2039204c8f6 | 146 | } |
kaizen | 0:f2039204c8f6 | 147 | else |
kaizen | 0:f2039204c8f6 | 148 | { |
kaizen | 0:f2039204c8f6 | 149 | return -1; |
kaizen | 0:f2039204c8f6 | 150 | } |
kaizen | 0:f2039204c8f6 | 151 | } |
kaizen | 0:f2039204c8f6 | 152 | |
kaizen | 0:f2039204c8f6 | 153 | int WizFi250::sendto (int cid, const char *buf, int len, const char *ip, int port) |
kaizen | 0:f2039204c8f6 | 154 | { |
kaizen | 0:f2039204c8f6 | 155 | if(!isConnected(cid)) return -1; |
kaizen | 0:f2039204c8f6 | 156 | |
kaizen | 0:f2039204c8f6 | 157 | if((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER)) |
kaizen | 0:f2039204c8f6 | 158 | { |
kaizen | 0:f2039204c8f6 | 159 | if ( len > CFG_DATA_SIZE ) len = CFG_DATA_SIZE; |
kaizen | 0:f2039204c8f6 | 160 | return cmdSSEND(buf,cid,len,ip,port); |
kaizen | 0:f2039204c8f6 | 161 | } |
kaizen | 0:f2039204c8f6 | 162 | else |
kaizen | 0:f2039204c8f6 | 163 | { |
kaizen | 0:f2039204c8f6 | 164 | return -1; |
kaizen | 0:f2039204c8f6 | 165 | } |
kaizen | 0:f2039204c8f6 | 166 | } |
kaizen | 0:f2039204c8f6 | 167 | |
kaizen | 0:f2039204c8f6 | 168 | int WizFi250::recv (int cid, char *buf, int len) |
kaizen | 0:f2039204c8f6 | 169 | { |
kaizen | 0:f2039204c8f6 | 170 | int i; |
kaizen | 0:f2039204c8f6 | 171 | |
kaizen | 0:f2039204c8f6 | 172 | if (!isConnected(cid)) return -1; |
kaizen | 0:f2039204c8f6 | 173 | |
kaizen | 0:f2039204c8f6 | 174 | if (_con[cid].buf == NULL ) return 0; |
kaizen | 0:f2039204c8f6 | 175 | while (!_con[cid].received && _state.mode != MODE_COMMAND); |
kaizen | 0:f2039204c8f6 | 176 | _con[cid].received = false; |
kaizen | 0:f2039204c8f6 | 177 | for(i=0; i<len; i++) |
kaizen | 0:f2039204c8f6 | 178 | { |
kaizen | 0:f2039204c8f6 | 179 | if(_con[cid].buf->dequeue(&buf[i]) == false) break; |
kaizen | 0:f2039204c8f6 | 180 | } |
kaizen | 0:f2039204c8f6 | 181 | setRts(true); // release |
kaizen | 0:f2039204c8f6 | 182 | return i; |
kaizen | 0:f2039204c8f6 | 183 | } |
kaizen | 0:f2039204c8f6 | 184 | |
kaizen | 0:f2039204c8f6 | 185 | int WizFi250::recvfrom (int cid, char *buf, int len, char *ip, int *port) |
kaizen | 0:f2039204c8f6 | 186 | { |
kaizen | 0:f2039204c8f6 | 187 | int i; |
kaizen | 0:f2039204c8f6 | 188 | |
kaizen | 0:f2039204c8f6 | 189 | if (!isConnected(cid)) return -1; |
kaizen | 0:f2039204c8f6 | 190 | |
kaizen | 0:f2039204c8f6 | 191 | if (_con[cid].buf == NULL) return 0; |
kaizen | 0:f2039204c8f6 | 192 | |
kaizen | 0:f2039204c8f6 | 193 | while (!_con[cid].received && _state.mode != MODE_COMMAND); |
kaizen | 0:f2039204c8f6 | 194 | |
kaizen | 0:f2039204c8f6 | 195 | _con[cid].received = false; |
kaizen | 0:f2039204c8f6 | 196 | for(i=0; i<len; i++) |
kaizen | 0:f2039204c8f6 | 197 | { |
kaizen | 0:f2039204c8f6 | 198 | if( _con[cid].buf->dequeue(&buf[i]) == false ) break; |
kaizen | 0:f2039204c8f6 | 199 | } |
kaizen | 0:f2039204c8f6 | 200 | //buf[i] = '\0'; |
kaizen | 0:f2039204c8f6 | 201 | strncpy(ip, _con[cid].ip, 16); |
kaizen | 0:f2039204c8f6 | 202 | *port = _con[cid].port; |
kaizen | 0:f2039204c8f6 | 203 | setRts(true); // release |
kaizen | 0:f2039204c8f6 | 204 | |
kaizen | 0:f2039204c8f6 | 205 | return i; |
kaizen | 0:f2039204c8f6 | 206 | } |
kaizen | 0:f2039204c8f6 | 207 | |
kaizen | 0:f2039204c8f6 | 208 | int WizFi250::readable (int cid) |
kaizen | 0:f2039204c8f6 | 209 | { |
kaizen | 0:f2039204c8f6 | 210 | if (!isConnected(cid)) return -1; |
kaizen | 0:f2039204c8f6 | 211 | |
kaizen | 0:f2039204c8f6 | 212 | if(_con[cid].buf == NULL) return -1; |
kaizen | 0:f2039204c8f6 | 213 | return _con[cid].buf->available(); |
kaizen | 0:f2039204c8f6 | 214 | } |
kaizen | 0:f2039204c8f6 | 215 | |
kaizen | 0:f2039204c8f6 | 216 | bool WizFi250::isConnected (int cid) |
kaizen | 0:f2039204c8f6 | 217 | { |
kaizen | 0:f2039204c8f6 | 218 | if ( cid < 0 || cid >=8 ) return false; |
kaizen | 0:f2039204c8f6 | 219 | |
kaizen | 0:f2039204c8f6 | 220 | return _con[cid].connected; |
kaizen | 0:f2039204c8f6 | 221 | } |
kaizen | 0:f2039204c8f6 | 222 | |
kaizen | 0:f2039204c8f6 | 223 | int WizFi250::accept (int cid) |
kaizen | 0:f2039204c8f6 | 224 | { |
kaizen | 0:f2039204c8f6 | 225 | if(!isConnected(cid)) return -1; |
kaizen | 0:f2039204c8f6 | 226 | |
kaizen | 0:f2039204c8f6 | 227 | if(_con[cid].connected && _con[cid].accept) |
kaizen | 0:f2039204c8f6 | 228 | { |
kaizen | 0:f2039204c8f6 | 229 | _con[cid].accept = false; |
kaizen | 0:f2039204c8f6 | 230 | return cid; |
kaizen | 0:f2039204c8f6 | 231 | } |
kaizen | 0:f2039204c8f6 | 232 | |
kaizen | 0:f2039204c8f6 | 233 | return -1; |
kaizen | 0:f2039204c8f6 | 234 | } |
kaizen | 0:f2039204c8f6 | 235 | |
kaizen | 0:f2039204c8f6 | 236 | int WizFi250::getRemote(int cid, char **ip, int *port) |
kaizen | 0:f2039204c8f6 | 237 | { |
kaizen | 0:f2039204c8f6 | 238 | if (!isConnected(cid)) return -1; |
kaizen | 0:f2039204c8f6 | 239 | |
kaizen | 0:f2039204c8f6 | 240 | *ip = _con[cid].ip; |
kaizen | 0:f2039204c8f6 | 241 | *port = _con[cid].port; |
kaizen | 0:f2039204c8f6 | 242 | return 0; |
kaizen | 0:f2039204c8f6 | 243 | } |