Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of WizFi310Interface_Legacy 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 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 return cid; 00081 } 00082 00083 int WizFi310::listen (Protocol proto, int port, void(*func)(int)) 00084 { 00085 int cid; 00086 00087 if(!isAssociated() || _state.status != STAT_READY) return -1; 00088 00089 _state.cid = -1; 00090 00091 if(proto == PROTO_TCP) 00092 { 00093 if( sendCommand("AT+MEVTMSG=1") ) return -1; 00094 if( cmdSCON("O","TSN",port) ) return -1; 00095 } 00096 else 00097 { 00098 if( cmdSCON("O","USN",port) ) return -1; 00099 } 00100 00101 if (_state.cid < 0) return -1; 00102 cid = _state.cid; 00103 _con[cid].protocol = proto; 00104 _con[cid].type = TYPE_SERVER; 00105 _con[cid].func = func; 00106 00107 return cid; 00108 } 00109 00110 int WizFi310::close (int cid) 00111 { 00112 // if(!isConnected(cid)) return -1; 00113 00114 _con[cid].connected = false; 00115 return cmdCLOSE(cid); 00116 } 00117 00118 00119 void WizFi310::initCon ( int cid, bool connected ) 00120 { 00121 _con[cid].parent = -1; // It will be delete because It is not need 00122 _con[cid].func = NULL; 00123 _con[cid].accept = false; 00124 00125 //#ifndef CFG_ENABLE_RTOS 00126 if ( _con[cid].buf == NULL ) 00127 { 00128 _con[cid].buf = new CircBuffer<char>(CFG_DATA_SIZE); 00129 if ( _con[cid].buf == NULL ) error("Can't allocate memory"); 00130 } 00131 //#endif 00132 if ( _con[cid].buf != NULL ) 00133 { 00134 _con[cid].buf->flush(); 00135 } 00136 _con[cid].connected = connected; 00137 } 00138 00139 int WizFi310::send(int cid, const char *buf, int len) 00140 { 00141 if(!isConnected(cid)) return -1; 00142 00143 if((_con[cid].protocol == PROTO_TCP) || 00144 (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) ) 00145 { 00146 // if ( len > CFG_DATA_SIZE) len = CFG_DATA_SIZE; 00147 return cmdSSEND(buf,cid,len); 00148 } 00149 else 00150 { 00151 return -1; 00152 } 00153 } 00154 00155 int WizFi310::sendto (int cid, const char *buf, int len, const char *ip, int port) 00156 { 00157 if(!isConnected(cid)) return -1; 00158 00159 if((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER)) 00160 { 00161 if ( len > CFG_DATA_SIZE ) len = CFG_DATA_SIZE; 00162 return cmdSSEND(buf,cid,len,ip,port); 00163 } 00164 else 00165 { 00166 return -1; 00167 } 00168 } 00169 00170 int WizFi310::recv (int cid, char *buf, int len) 00171 { 00172 int i; 00173 00174 if (!isConnected(cid)) return -1; 00175 00176 if (_con[cid].buf == NULL ) return 0; 00177 00178 while (!_con[cid].received && _state.mode != MODE_COMMAND); 00179 _con[cid].received = false; 00180 00181 for(i=0; i<len; i++) 00182 { 00183 if(_con[cid].buf->dequeue(&buf[i]) == false) break; 00184 } 00185 00186 setRts(true); // release 00187 return i; 00188 } 00189 00190 int WizFi310::recvfrom (int cid, char *buf, int len, char *ip, int *port) 00191 { 00192 int i; 00193 00194 if (!isConnected(cid)) return -1; 00195 00196 if (_con[cid].buf == NULL) return 0; 00197 00198 while (!_con[cid].received && _state.mode != MODE_COMMAND); 00199 00200 _con[cid].received = false; 00201 for(i=0; i<len; i++) 00202 { 00203 if( _con[cid].buf->dequeue(&buf[i]) == false ) break; 00204 } 00205 //buf[i] = '\0'; 00206 strncpy(ip, _con[cid].ip, 16); 00207 *port = _con[cid].port; 00208 setRts(true); // release 00209 00210 return i; 00211 } 00212 00213 int WizFi310::readable (int cid) 00214 { 00215 if (!isConnected(cid)) return -1; 00216 00217 if(_con[cid].buf == NULL) return -1; 00218 return _con[cid].buf->available(); 00219 } 00220 00221 bool WizFi310::isConnected (int cid) 00222 { 00223 if ( cid < 0 || cid >=8 ) return false; 00224 //printf("%d %d\r\n", cid, _con[cid].connected); 00225 return _con[cid].connected; 00226 } 00227 00228 int WizFi310::accept (int cid) 00229 { 00230 if(!isConnected(cid)) return -1; 00231 00232 if(_con[cid].connected && _con[cid].accept) 00233 { 00234 _con[cid].accept = false; 00235 return cid; 00236 } 00237 00238 return -1; 00239 } 00240 00241 int WizFi310::getRemote(int cid, char **ip, int *port) 00242 { 00243 if (!isConnected(cid)) return -1; 00244 00245 *ip = _con[cid].ip; 00246 *port = _con[cid].port; 00247 return 0; 00248 }
Generated on Thu Jul 14 2022 16:40:12 by
