PIR sensor 2

Fork of PIRSensorWithTwitter by ajeet prajapati

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 WizFi250
00021  */
00022 /* Copyright (C) 2017 Wiznet, MIT License
00023  *  port to the Wiznet Module WizFi310
00024  */
00025 
00026 #include "WizFi310.h"
00027 
00028 int WizFi310::getHostByName(const char * host, char *ip)
00029 {
00030     int i, flg = 0;
00031 
00032     if(!isAssociated() || _state.status != STAT_READY)  return -1;
00033 
00034     for(i=0; i<strlen(host); i++)
00035     {
00036         if( (host[i] < '0' || host[i] > '9') && host[i] != '.')
00037         {
00038             flg = 1;
00039             break;
00040         }
00041     }
00042     if (!flg)
00043     {
00044         strncpy(ip, host, 16);
00045         return 0;
00046     }
00047 
00048     if ( cmdFDNS(host) )
00049     {
00050         wait_ms(1000);
00051         if( cmdFDNS(host) ) return -1;
00052     }
00053     strncpy(ip, _state.resolv, 16);
00054     return 0;
00055 }
00056 
00057 int WizFi310::open(Protocol proto, const char *ip, int remotePort, int localPort, void(*func)(int))
00058 {
00059     int cid;
00060 
00061     if (!isAssociated() || _state.status != STAT_READY)     return -1;
00062 
00063     _state.cid = -1;
00064 
00065     if (proto == PROTO_TCP)
00066     {
00067         if( cmdSCON( "O","TCN",ip, remotePort, localPort, "0" ) )   return -1;
00068     }
00069     else if(proto == PROTO_UDP)
00070     {
00071         if( cmdSCON( "O","UCN",ip, remotePort, localPort, "0" ) )   return -1;
00072     }
00073     if(_state.cid < 0) return -1;
00074 
00075     initCon(_state.cid, true);
00076     cid = _state.cid;
00077     _con[cid].protocol = proto;
00078     _con[cid].type = TYPE_CLIENT;
00079     _con[cid].func = func;
00080     printf("TYPE : %d\n",_con[cid].type);
00081 
00082     return cid;
00083 }
00084 
00085 int WizFi310::listen (Protocol proto, int port, void(*func)(int))
00086 {
00087     int cid;
00088 
00089     if(!isAssociated() || _state.status != STAT_READY)  return -1;
00090 
00091     _state.cid = -1;
00092 
00093     if(proto == PROTO_TCP)
00094     {
00095         if( sendCommand("AT+MEVTMSG=1") )   return -1;
00096         if( cmdSCON("O","TSN",port) )       return -1;
00097     }
00098     else
00099     {
00100         if( cmdSCON("O","USN",port) )   return -1;
00101     }
00102 
00103     if (_state.cid < 0) return -1;
00104     cid = _state.cid;
00105     _con[cid].protocol = proto;
00106     _con[cid].type = TYPE_SERVER;
00107     _con[cid].func = func;
00108 
00109     return cid;
00110 }
00111 
00112 int WizFi310::close (int cid)
00113 {
00114 //    if(!isConnected(cid))   return -1;
00115 
00116     _con[cid].connected = false;
00117     return cmdCLOSE(cid);
00118 }
00119 
00120 
00121 void WizFi310::initCon ( int cid, bool connected )
00122 {
00123     _con[cid].parent = -1;      // It will be delete because It is not need
00124     _con[cid].func = NULL;
00125     _con[cid].accept = false;
00126 
00127 //#ifndef CFG_ENABLE_RTOS
00128     if ( _con[cid].buf == NULL )
00129     {
00130         _con[cid].buf = new CircBuffer<char>(CFG_DATA_SIZE);
00131         if ( _con[cid].buf == NULL )    error("Can't allocate memory");
00132     }
00133 //#endif
00134     if ( _con[cid].buf != NULL )
00135     {
00136         _con[cid].buf->flush();
00137     }
00138     _con[cid].connected = connected;
00139 }
00140 
00141 int WizFi310::send(int cid, const char *buf, int len)
00142 {
00143     if(!isConnected(cid)) return -1;
00144 
00145     if((_con[cid].protocol == PROTO_TCP) ||
00146             (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) )
00147     {
00148 //        if ( len > CFG_DATA_SIZE)   len = CFG_DATA_SIZE;
00149         return cmdSSEND(buf,cid,len);
00150     }
00151     else
00152     {
00153         return -1;
00154     }
00155 }
00156 
00157 int WizFi310::sendto (int cid, const char *buf, int len, const char *ip, int port)
00158 {
00159     if(!isConnected(cid))   return -1;
00160 
00161     if((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER))
00162     {
00163         if ( len > CFG_DATA_SIZE )  len = CFG_DATA_SIZE;
00164         return cmdSSEND(buf,cid,len,ip,port);
00165     }
00166     else
00167     {
00168         return -1;
00169     }
00170 }
00171 
00172 int WizFi310::recv (int cid, char *buf, int len)
00173 {
00174     int i;
00175 
00176     if (!isConnected(cid))  return -1;
00177     
00178     if (_con[cid].buf == NULL ) return 0;
00179 
00180     while (!_con[cid].received && _state.mode != MODE_COMMAND);
00181     _con[cid].received = false;
00182     
00183     for(i=0; i<len; i++)
00184     {
00185         if(_con[cid].buf->dequeue(&buf[i]) == false)    break;
00186     }
00187     
00188     setRts(true);       // release
00189     return i;
00190 }
00191 
00192 int WizFi310::recvfrom (int cid, char *buf, int len, char *ip, int *port)
00193 {
00194     int i;
00195 
00196     if (!isConnected(cid))  return -1;
00197 
00198     if (_con[cid].buf == NULL)  return 0;
00199 
00200     while (!_con[cid].received && _state.mode != MODE_COMMAND);
00201 
00202     _con[cid].received = false;
00203     for(i=0; i<len; i++)
00204     {
00205         if( _con[cid].buf->dequeue(&buf[i]) == false )  break;
00206     }
00207     //buf[i] = '\0';
00208     strncpy(ip, _con[cid].ip, 16);
00209     *port = _con[cid].port;
00210     setRts(true);       // release
00211 
00212     return i;
00213 }
00214 
00215 int WizFi310::readable (int cid)
00216 {
00217     if (!isConnected(cid))  return -1;
00218 
00219     if(_con[cid].buf == NULL)   return -1;
00220     return _con[cid].buf->available();
00221 }
00222 
00223 bool WizFi310::isConnected (int cid)
00224 {
00225     if ( cid < 0 || cid >=8 ) return false;
00226     //printf("%d %d\r\n", cid, _con[cid].connected);
00227     return _con[cid].connected;
00228 }
00229 
00230 int WizFi310::accept (int cid)
00231 {
00232     if(!isConnected(cid))   return -1;
00233 
00234     if(_con[cid].connected && _con[cid].accept)
00235     {
00236         _con[cid].accept = false;
00237         return cid;
00238     }
00239 
00240     return -1;
00241 }
00242 
00243 int WizFi310::getRemote(int cid, char **ip, int *port)
00244 {
00245     if (!isConnected(cid))  return -1;
00246 
00247     *ip = _con[cid].ip;
00248     *port = _con[cid].port;
00249     return 0;
00250 }