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