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