WIZnet / WizFi310Interface

Dependents:   KT_IoTMakers_WizFi310_Example WizFi310_STATION_HelloWorld WizFi310_DNS_TCP_HelloWorld WizFi310_Ubidots ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi310_sock.cpp Source File

WizFi310_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 WizFi310
00021  */
00022 
00023 #include "WizFi310.h"
00024 
00025 int WizFi310::getHostByName(const char * host, char *ip)
00026 {
00027     int i, flg = 0;
00028 
00029     if(!isAssociated() || _state.status != STAT_READY)  return -1;
00030 
00031     for(i=0; i<strlen(host); i++)
00032     {
00033         if( (host[i] < '0' || host[i] > '9') && host[i] != '.')
00034         {
00035             flg = 1;
00036             break;
00037         }
00038     }
00039     if (!flg)
00040     {
00041         strncpy(ip, host, 16);
00042         return 0;
00043     }
00044 
00045     if ( cmdFDNS(host) )
00046     {
00047         wait_ms(1000);
00048         if( cmdFDNS(host) ) return -1;
00049     }
00050     strncpy(ip, _state.resolv, 16);
00051     return 0;
00052 }
00053 
00054 int WizFi310::open(Protocol proto, const char *ip, int remotePort, int localPort, void(*func)(int))
00055 {
00056     int cid;
00057 
00058     if (!isAssociated() || _state.status != STAT_READY)     return -1;
00059 
00060     _state.cid = -1;
00061 
00062     if (proto == PROTO_TCP)
00063     {
00064         if( cmdSCON( "O","TCN",ip, remotePort, localPort, "0" ) )   return -1;
00065     }
00066     else if(proto == PROTO_UDP)
00067     {
00068         if( cmdSCON( "O","UCN",ip, remotePort, localPort, "0" ) )   return -1;
00069     }
00070     
00071     if(_state.cid < 0) return -1;
00072     
00073     initCon(_state.cid, true);
00074     
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 WizFi310::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 WizFi310::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 WizFi310::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 WizFi310::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 WizFi310::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 WizFi310::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 
00177     while (!_con[cid].received && _state.mode != MODE_COMMAND);
00178     _con[cid].received = false;
00179     
00180     for(i=0; i<len; i++)
00181     {
00182         if(_con[cid].buf->dequeue(&buf[i]) == false)    break;
00183     }
00184     
00185     setRts(true);       // release
00186     return i;
00187 }
00188 
00189 int WizFi310::recvfrom (int cid, char *buf, int len, char *ip, int *port)
00190 {
00191     int i;
00192 
00193     if (!isConnected(cid))  return -1;
00194 
00195     if (_con[cid].buf == NULL)  return 0;
00196 
00197     while (!_con[cid].received && _state.mode != MODE_COMMAND);
00198 
00199     _con[cid].received = false;
00200     for(i=0; i<len; i++)
00201     {
00202         if( _con[cid].buf->dequeue(&buf[i]) == false )  break;
00203     }
00204     //buf[i] = '\0';
00205     strncpy(ip, _con[cid].ip, 16);
00206     *port = _con[cid].port;
00207     setRts(true);       // release
00208 
00209     return i;
00210 }
00211 
00212 int WizFi310::readable (int cid)
00213 {
00214     if (!isConnected(cid))  return -1;
00215 
00216     if(_con[cid].buf == NULL)   return -1;
00217     return _con[cid].buf->available();
00218 }
00219 
00220 bool WizFi310::isConnected (int cid)
00221 {
00222     if ( cid < 0 || cid >=8 ) return false;
00223 
00224     return _con[cid].connected;
00225 }
00226 
00227 int WizFi310::accept (int cid)
00228 {
00229     if(!isConnected(cid))   return -1;
00230     
00231     if(_con[cid].connected && _con[cid].accept)
00232     {
00233         _con[cid].accept = false;
00234         return cid;
00235     }
00236 
00237     return -1;
00238 }
00239 
00240 int WizFi310::getRemote(int cid, char **ip, int *port)
00241 {
00242     if (!isConnected(cid))  return -1;
00243 
00244     *ip = _con[cid].ip;
00245     *port = _con[cid].port;
00246     return 0;
00247 }