RC

Dependents:   WizFi250_AP_HelloWorld

Fork of WizFi250Interface by DongEun Koak

Committer:
kaizen
Date:
Mon Jun 02 23:34:10 2014 +0000
Revision:
0:f2039204c8f6
Child:
9:12ecb377f63f
WizFi250 Library First Release

Who changed what in which revision?

UserRevisionLine numberNew 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;
kaizen 0:f2039204c8f6 72 cid = _state.cid;
kaizen 0:f2039204c8f6 73 _con[cid].protocol = proto;
kaizen 0:f2039204c8f6 74 _con[cid].type = TYPE_CLIENT;
kaizen 0:f2039204c8f6 75 _con[cid].func = func;
kaizen 0:f2039204c8f6 76 return cid;
kaizen 0:f2039204c8f6 77 }
kaizen 0:f2039204c8f6 78
kaizen 0:f2039204c8f6 79 int WizFi250::listen (Protocol proto, int port, void(*func)(int))
kaizen 0:f2039204c8f6 80 {
kaizen 0:f2039204c8f6 81 int cid;
kaizen 0:f2039204c8f6 82
kaizen 0:f2039204c8f6 83 if(!isAssociated() || _state.status != STAT_READY) return -1;
kaizen 0:f2039204c8f6 84
kaizen 0:f2039204c8f6 85 _state.cid = -1;
kaizen 0:f2039204c8f6 86
kaizen 0:f2039204c8f6 87 if(proto == PROTO_TCP)
kaizen 0:f2039204c8f6 88 {
kaizen 0:f2039204c8f6 89 if( sendCommand("AT+MEVTMSG=1") ) return -1;
kaizen 0:f2039204c8f6 90 if( cmdSCON("O","TSN",port) ) return -1;
kaizen 0:f2039204c8f6 91 }
kaizen 0:f2039204c8f6 92 else
kaizen 0:f2039204c8f6 93 {
kaizen 0:f2039204c8f6 94 if( cmdSCON("O","USN",port) ) return -1;
kaizen 0:f2039204c8f6 95 }
kaizen 0:f2039204c8f6 96
kaizen 0:f2039204c8f6 97 if (_state.cid < 0) return -1;
kaizen 0:f2039204c8f6 98 cid = _state.cid;
kaizen 0:f2039204c8f6 99 _con[cid].protocol = proto;
kaizen 0:f2039204c8f6 100 _con[cid].type = TYPE_SERVER;
kaizen 0:f2039204c8f6 101 _con[cid].func = func;
kaizen 0:f2039204c8f6 102
kaizen 0:f2039204c8f6 103 return cid;
kaizen 0:f2039204c8f6 104 }
kaizen 0:f2039204c8f6 105
kaizen 0:f2039204c8f6 106 int WizFi250::close (int cid)
kaizen 0:f2039204c8f6 107 {
kaizen 0:f2039204c8f6 108 if(!isConnected(cid)) return -1;
kaizen 0:f2039204c8f6 109
kaizen 0:f2039204c8f6 110 _con[cid].connected = false;
kaizen 0:f2039204c8f6 111 return cmdCLOSE(cid);
kaizen 0:f2039204c8f6 112 }
kaizen 0:f2039204c8f6 113
kaizen 0:f2039204c8f6 114
kaizen 0:f2039204c8f6 115 void WizFi250::initCon ( int cid, bool connected )
kaizen 0:f2039204c8f6 116 {
kaizen 0:f2039204c8f6 117 _con[cid].parent = -1; // It will be delete because It is not need
kaizen 0:f2039204c8f6 118 _con[cid].func = NULL;
kaizen 0:f2039204c8f6 119 _con[cid].accept = false;
kaizen 0:f2039204c8f6 120
kaizen 0:f2039204c8f6 121 //#ifndef CFG_ENABLE_RTOS
kaizen 0:f2039204c8f6 122 if ( _con[cid].buf == NULL )
kaizen 0:f2039204c8f6 123 {
kaizen 0:f2039204c8f6 124 _con[cid].buf = new CircBuffer<char>(CFG_DATA_SIZE);
kaizen 0:f2039204c8f6 125 if ( _con[cid].buf == NULL ) error("Can't allocate memory");
kaizen 0:f2039204c8f6 126 }
kaizen 0:f2039204c8f6 127 //#endif
kaizen 0:f2039204c8f6 128 if ( _con[cid].buf != NULL )
kaizen 0:f2039204c8f6 129 {
kaizen 0:f2039204c8f6 130 _con[cid].buf->flush();
kaizen 0:f2039204c8f6 131 }
kaizen 0:f2039204c8f6 132 _con[cid].connected = connected;
kaizen 0:f2039204c8f6 133 }
kaizen 0:f2039204c8f6 134
kaizen 0:f2039204c8f6 135 int WizFi250::send(int cid, const char *buf, int len)
kaizen 0:f2039204c8f6 136 {
kaizen 0:f2039204c8f6 137 if(!isConnected(cid)) return -1;
kaizen 0:f2039204c8f6 138
kaizen 0:f2039204c8f6 139 if((_con[cid].protocol == PROTO_TCP) ||
kaizen 0:f2039204c8f6 140 (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) )
kaizen 0:f2039204c8f6 141 {
kaizen 0:f2039204c8f6 142 // if ( len > CFG_DATA_SIZE) len = CFG_DATA_SIZE;
kaizen 0:f2039204c8f6 143 return cmdSSEND(buf,cid,len);
kaizen 0:f2039204c8f6 144 }
kaizen 0:f2039204c8f6 145 else
kaizen 0:f2039204c8f6 146 {
kaizen 0:f2039204c8f6 147 return -1;
kaizen 0:f2039204c8f6 148 }
kaizen 0:f2039204c8f6 149 }
kaizen 0:f2039204c8f6 150
kaizen 0:f2039204c8f6 151 int WizFi250::sendto (int cid, const char *buf, int len, const char *ip, int port)
kaizen 0:f2039204c8f6 152 {
kaizen 0:f2039204c8f6 153 if(!isConnected(cid)) return -1;
kaizen 0:f2039204c8f6 154
kaizen 0:f2039204c8f6 155 if((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER))
kaizen 0:f2039204c8f6 156 {
kaizen 0:f2039204c8f6 157 if ( len > CFG_DATA_SIZE ) len = CFG_DATA_SIZE;
kaizen 0:f2039204c8f6 158 return cmdSSEND(buf,cid,len,ip,port);
kaizen 0:f2039204c8f6 159 }
kaizen 0:f2039204c8f6 160 else
kaizen 0:f2039204c8f6 161 {
kaizen 0:f2039204c8f6 162 return -1;
kaizen 0:f2039204c8f6 163 }
kaizen 0:f2039204c8f6 164 }
kaizen 0:f2039204c8f6 165
kaizen 0:f2039204c8f6 166 int WizFi250::recv (int cid, char *buf, int len)
kaizen 0:f2039204c8f6 167 {
kaizen 0:f2039204c8f6 168 int i;
kaizen 0:f2039204c8f6 169
kaizen 0:f2039204c8f6 170 if (!isConnected(cid)) return -1;
kaizen 0:f2039204c8f6 171
kaizen 0:f2039204c8f6 172 if (_con[cid].buf == NULL ) return 0;
kaizen 0:f2039204c8f6 173 while (!_con[cid].received && _state.mode != MODE_COMMAND);
kaizen 0:f2039204c8f6 174 _con[cid].received = false;
kaizen 0:f2039204c8f6 175 for(i=0; i<len; i++)
kaizen 0:f2039204c8f6 176 {
kaizen 0:f2039204c8f6 177 if(_con[cid].buf->dequeue(&buf[i]) == false) break;
kaizen 0:f2039204c8f6 178 }
kaizen 0:f2039204c8f6 179 setRts(true); // release
kaizen 0:f2039204c8f6 180 return i;
kaizen 0:f2039204c8f6 181 }
kaizen 0:f2039204c8f6 182
kaizen 0:f2039204c8f6 183 int WizFi250::recvfrom (int cid, char *buf, int len, char *ip, int *port)
kaizen 0:f2039204c8f6 184 {
kaizen 0:f2039204c8f6 185 int i;
kaizen 0:f2039204c8f6 186
kaizen 0:f2039204c8f6 187 if (!isConnected(cid)) return -1;
kaizen 0:f2039204c8f6 188
kaizen 0:f2039204c8f6 189 if (_con[cid].buf == NULL) return 0;
kaizen 0:f2039204c8f6 190
kaizen 0:f2039204c8f6 191 while (!_con[cid].received && _state.mode != MODE_COMMAND);
kaizen 0:f2039204c8f6 192
kaizen 0:f2039204c8f6 193 _con[cid].received = false;
kaizen 0:f2039204c8f6 194 for(i=0; i<len; i++)
kaizen 0:f2039204c8f6 195 {
kaizen 0:f2039204c8f6 196 if( _con[cid].buf->dequeue(&buf[i]) == false ) break;
kaizen 0:f2039204c8f6 197 }
kaizen 0:f2039204c8f6 198 //buf[i] = '\0';
kaizen 0:f2039204c8f6 199 strncpy(ip, _con[cid].ip, 16);
kaizen 0:f2039204c8f6 200 *port = _con[cid].port;
kaizen 0:f2039204c8f6 201 setRts(true); // release
kaizen 0:f2039204c8f6 202
kaizen 0:f2039204c8f6 203 return i;
kaizen 0:f2039204c8f6 204 }
kaizen 0:f2039204c8f6 205
kaizen 0:f2039204c8f6 206 int WizFi250::readable (int cid)
kaizen 0:f2039204c8f6 207 {
kaizen 0:f2039204c8f6 208 if (!isConnected(cid)) return -1;
kaizen 0:f2039204c8f6 209
kaizen 0:f2039204c8f6 210 if(_con[cid].buf == NULL) return -1;
kaizen 0:f2039204c8f6 211 return _con[cid].buf->available();
kaizen 0:f2039204c8f6 212 }
kaizen 0:f2039204c8f6 213
kaizen 0:f2039204c8f6 214 bool WizFi250::isConnected (int cid)
kaizen 0:f2039204c8f6 215 {
kaizen 0:f2039204c8f6 216 if ( cid < 0 || cid >=8 ) return false;
kaizen 0:f2039204c8f6 217
kaizen 0:f2039204c8f6 218 return _con[cid].connected;
kaizen 0:f2039204c8f6 219 }
kaizen 0:f2039204c8f6 220
kaizen 0:f2039204c8f6 221 int WizFi250::accept (int cid)
kaizen 0:f2039204c8f6 222 {
kaizen 0:f2039204c8f6 223 if(!isConnected(cid)) return -1;
kaizen 0:f2039204c8f6 224
kaizen 0:f2039204c8f6 225 if(_con[cid].connected && _con[cid].accept)
kaizen 0:f2039204c8f6 226 {
kaizen 0:f2039204c8f6 227 _con[cid].accept = false;
kaizen 0:f2039204c8f6 228 return cid;
kaizen 0:f2039204c8f6 229 }
kaizen 0:f2039204c8f6 230
kaizen 0:f2039204c8f6 231 return -1;
kaizen 0:f2039204c8f6 232 }
kaizen 0:f2039204c8f6 233
kaizen 0:f2039204c8f6 234 int WizFi250::getRemote(int cid, char **ip, int *port)
kaizen 0:f2039204c8f6 235 {
kaizen 0:f2039204c8f6 236 if (!isConnected(cid)) return -1;
kaizen 0:f2039204c8f6 237
kaizen 0:f2039204c8f6 238 *ip = _con[cid].ip;
kaizen 0:f2039204c8f6 239 *port = _con[cid].port;
kaizen 0:f2039204c8f6 240 return 0;
kaizen 0:f2039204c8f6 241 }