123

Fork of WizFi250Interface_1 by cliff Hong

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi250_sock.cpp Source File

WizFi250_sock.cpp

00001 /*
00002 /* Copyright (C) 2013 gsfan, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 /* Copyright (C) 2014 Wiznet, MIT License
00020  *  port to the Wiznet Module WizFi250
00021  */
00022 
00023 #include "WizFi250.h"
00024 
00025 
00026 int WizFi250::getHostByName(const char * host, char *ip)
00027 {
00028     int i, flg = 0;
00029 
00030     if(!isAssociated() || _state.status != STAT_READY)  return -1;
00031 
00032     for(i=0; i<strlen(host); i++)
00033     {
00034         if( (host[i] < '0' || host[i] > '9') && host[i] != '.')
00035         {
00036             flg = 1;
00037             break;
00038         }
00039     }
00040     if (!flg)
00041     {
00042         strncpy(ip, host, 16);
00043         return 0;
00044     }
00045 
00046     if ( cmdFDNS(host) )
00047     {
00048         wait_ms(1000);
00049         if( cmdFDNS(host) ) return -1;
00050     }
00051     strncpy(ip, _state.resolv, 16);
00052     return 0;
00053 }
00054 
00055 int WizFi250::open(Protocol proto, const char *ip, int remotePort, int localPort, void(*func)(int))
00056 {
00057     int cid;
00058 
00059     if (!isAssociated() || _state.status != STAT_READY)     return -1;
00060 
00061     _state.cid = -1;
00062 
00063     if (proto == PROTO_TCP)
00064     {
00065         if( cmdSCON( "O","TCN",ip, remotePort, localPort, "0" ) )   return -1;
00066     }
00067     else if(proto == PROTO_UDP)
00068     {
00069         if( cmdSCON( "O","UCN",ip, remotePort, localPort, "0" ) )   return -1;
00070     }
00071     if(_state.cid < 0) return -1;
00072     
00073 
00074     initCon(_state.cid, true);
00075     cid = _state.cid;
00076     _con[cid].protocol = proto;
00077     _con[cid].type = TYPE_CLIENT;
00078     _con[cid].func = func;
00079     return cid;
00080 }
00081 
00082 int WizFi250::listen (Protocol proto, int port, void(*func)(int))
00083 {
00084     int cid;
00085 
00086     if(!isAssociated() || _state.status != STAT_READY)  return -1;
00087 
00088     _state.cid = -1;
00089 
00090     if(proto == PROTO_TCP)
00091     {
00092         if( sendCommand("AT+MEVTMSG=1") )   return -1;
00093         if( cmdSCON("O","TSN",port) )       return -1;
00094     }
00095     else
00096     {
00097         if( cmdSCON("O","USN",port) )   return -1;
00098     }
00099 
00100     if (_state.cid < 0) return -1;
00101     cid = _state.cid;
00102     _con[cid].protocol = proto;
00103     _con[cid].type = TYPE_SERVER;
00104     _con[cid].func = func;
00105 
00106     return cid;
00107 }
00108 
00109 int WizFi250::close (int cid)
00110 {
00111 //    if(!isConnected(cid))   return -1;
00112 
00113     _con[cid].connected = false;
00114     return cmdCLOSE(cid);
00115 }
00116 
00117 
00118 void WizFi250::initCon ( int cid, bool connected )
00119 {
00120     _con[cid].parent = -1;      // It will be delete because It is not need
00121     _con[cid].func = NULL;
00122     _con[cid].accept = false;
00123 
00124 //#ifndef CFG_ENABLE_RTOS
00125     if ( _con[cid].buf == NULL )
00126     {
00127         _con[cid].buf = new CircBuffer<char>(CFG_DATA_SIZE);
00128         if ( _con[cid].buf == NULL )    error("Can't allocate memory");
00129     }
00130 //#endif
00131     if ( _con[cid].buf != NULL )
00132     {
00133         _con[cid].buf->flush();
00134     }
00135     _con[cid].connected = connected;
00136 }
00137 
00138 int WizFi250::send(int cid, const char *buf, int len)
00139 {
00140     if(!isConnected(cid)) return -1;
00141 
00142     if((_con[cid].protocol == PROTO_TCP) ||
00143             (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) )
00144     {
00145 //        if ( len > CFG_DATA_SIZE)   len = CFG_DATA_SIZE;
00146         return cmdSSEND(buf,cid,len);
00147     }
00148     else
00149     {
00150         return -1;
00151     }
00152 }
00153 
00154 int WizFi250::sendto (int cid, const char *buf, int len, const char *ip, int port)
00155 {
00156     if(!isConnected(cid))   return -1;
00157 
00158     if((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER))
00159     {
00160         if ( len > CFG_DATA_SIZE )  len = CFG_DATA_SIZE;
00161         return cmdSSEND(buf,cid,len,ip,port);
00162     }
00163     else
00164     {
00165         return -1;
00166     }
00167 }
00168 
00169 int WizFi250::recv (int cid, char *buf, int len)
00170 {
00171     int i;
00172 
00173     if (!isConnected(cid))  return -1;
00174 
00175     if (_con[cid].buf == NULL ) return 0;
00176     while (!_con[cid].received && _state.mode != MODE_COMMAND);
00177     _con[cid].received = false;
00178     for(i=0; i<len; i++)
00179     {
00180         if(_con[cid].buf->dequeue(&buf[i]) == false)    break;
00181     }
00182     setRts(true);       // release
00183     return i;
00184 }
00185 
00186 int WizFi250::recvfrom (int cid, char *buf, int len, char *ip, int *port)
00187 {
00188     int i;
00189 
00190     if (!isConnected(cid))  return -1;
00191 
00192     if (_con[cid].buf == NULL)  return 0;
00193 
00194     while (!_con[cid].received && _state.mode != MODE_COMMAND);
00195 
00196     _con[cid].received = false;
00197     for(i=0; i<len; i++)
00198     {
00199         if( _con[cid].buf->dequeue(&buf[i]) == false )  break;
00200     }
00201     //buf[i] = '\0';
00202     strncpy(ip, _con[cid].ip, 16);
00203     *port = _con[cid].port;
00204     setRts(true);       // release
00205 
00206     return i;
00207 }
00208 
00209 int WizFi250::readable (int cid)
00210 {
00211     if (!isConnected(cid))  return -1;
00212 
00213     if(_con[cid].buf == NULL)   return -1;
00214     return _con[cid].buf->available();
00215 }
00216 
00217 bool WizFi250::isConnected (int cid)
00218 {
00219     if ( cid < 0 || cid >=8 ) return false;
00220 
00221     return _con[cid].connected;
00222 }
00223 
00224 int WizFi250::accept (int cid)
00225 {
00226     if(!isConnected(cid))   return -1;
00227 
00228     if(_con[cid].connected && _con[cid].accept)
00229     {
00230         _con[cid].accept = false;
00231         return cid;
00232     }
00233 
00234     return -1;
00235 }
00236 
00237 int WizFi250::getRemote(int cid, char **ip, int *port)
00238 {
00239     if (!isConnected(cid))  return -1;
00240 
00241     *ip = _con[cid].ip;
00242     *port = _con[cid].port;
00243     return 0;
00244 }