ThingPlug Test
Dependents: WizFi310_ThingPlug_Test WizFi310_ThingPlug_Test_P
Fork of WizFi310Interface by
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 if(_state.cid < 0) return -1; 00071 00072 initCon(_state.cid, true); 00073 cid = _state.cid; 00074 _con[cid].protocol = proto; 00075 _con[cid].type = TYPE_CLIENT; 00076 _con[cid].func = func; 00077 return cid; 00078 } 00079 00080 int WizFi310::listen (Protocol proto, int port, void(*func)(int)) 00081 { 00082 int cid; 00083 00084 if(!isAssociated() || _state.status != STAT_READY) return -1; 00085 00086 _state.cid = -1; 00087 00088 if(proto == PROTO_TCP) 00089 { 00090 if( sendCommand("AT+MEVTMSG=1") ) return -1; 00091 if( cmdSCON("O","TSN",port) ) return -1; 00092 } 00093 else 00094 { 00095 if( cmdSCON("O","USN",port) ) return -1; 00096 } 00097 00098 if (_state.cid < 0) return -1; 00099 cid = _state.cid; 00100 _con[cid].protocol = proto; 00101 _con[cid].type = TYPE_SERVER; 00102 _con[cid].func = func; 00103 00104 return cid; 00105 } 00106 00107 int WizFi310::close (int cid) 00108 { 00109 // if(!isConnected(cid)) return -1; 00110 00111 _con[cid].connected = false; 00112 return cmdCLOSE(cid); 00113 } 00114 00115 00116 void WizFi310::initCon ( int cid, bool connected ) 00117 { 00118 _con[cid].parent = -1; // It will be delete because It is not need 00119 _con[cid].func = NULL; 00120 _con[cid].accept = false; 00121 00122 //#ifndef CFG_ENABLE_RTOS 00123 if ( _con[cid].buf == NULL ) 00124 { 00125 _con[cid].buf = new CircBuffer<char>(CFG_DATA_SIZE); 00126 if ( _con[cid].buf == NULL ) error("Can't allocate memory"); 00127 } 00128 //#endif 00129 if ( _con[cid].buf != NULL ) 00130 { 00131 _con[cid].buf->flush(); 00132 } 00133 _con[cid].connected = connected; 00134 } 00135 00136 int WizFi310::send(int cid, const char *buf, int len) 00137 { 00138 if(!isConnected(cid)) return -1; 00139 00140 if((_con[cid].protocol == PROTO_TCP) || 00141 (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) ) 00142 { 00143 // if ( len > CFG_DATA_SIZE) len = CFG_DATA_SIZE; 00144 return cmdSSEND(buf,cid,len); 00145 } 00146 else 00147 { 00148 return -1; 00149 } 00150 } 00151 00152 int WizFi310::sendto (int cid, const char *buf, int len, const char *ip, int port) 00153 { 00154 if(!isConnected(cid)) return -1; 00155 00156 if((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER)) 00157 { 00158 if ( len > CFG_DATA_SIZE ) len = CFG_DATA_SIZE; 00159 return cmdSSEND(buf,cid,len,ip,port); 00160 } 00161 else 00162 { 00163 return -1; 00164 } 00165 } 00166 00167 int WizFi310::recv (int cid, char *buf, int len) 00168 { 00169 int i; 00170 00171 if (!isConnected(cid)) return -1; 00172 00173 if (_con[cid].buf == NULL ) return 0; 00174 00175 memset(buf, 0x00, sizeof(buf)); 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 //printf("%d %d\r\n", cid, _con[cid].connected); 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 }
Generated on Sat Jul 16 2022 03:26:38 by
1.7.2
