ghj

Fork of WizFi310Interface_Legacy by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi310.cpp Source File

WizFi310.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 "mbed.h"
00027 #include "WizFi310.h"
00028 
00029 WizFi310 * WizFi310::_inst;
00030 
00031 
00032 WizFi310::WizFi310(PinName tx,PinName rx,PinName cts, PinName rts,PinName reset, PinName alarm, int baud):
00033     _wizfi(tx,rx), _reset(reset)
00034 {
00035     _inst = this;
00036     memset(&_state, 0, sizeof(_state));
00037     memset(&_con, 0, sizeof(_con));
00038     _state.initialized = false;
00039     _state.status = STAT_READY;
00040     _state.cid = -1;
00041     _state.buf = new CircBuffer<char>(CFG_DATA_SIZE);
00042 
00043     initUart(cts, rts, alarm, baud);
00044     _reset.output();
00045 
00046     setRts(true); // release
00047 
00048     /*
00049     wait_ms(500);
00050     cmdAT();
00051     cmdMECHO(false);
00052     if(cts != NC && rts != NC)
00053         cmdUSET(baud,"HW");
00054     else
00055         cmdUSET(baud,"N");
00056 
00057     // WizFi310 will restart by cmdUSET command.
00058     wait_ms(1000);
00059     cmdAT();
00060     */
00061 }
00062 
00063 int WizFi310::join(WiFiMode mode)
00064 {
00065     char sec[10];
00066 
00067     if( cmdMMAC() ) return -1;
00068 
00069     if(mode == WM_AP)
00070         _state.wm = WM_AP;
00071     else
00072         _state.wm = WM_STATION;
00073 
00074     if ( cmdWNET(_state.dhcp) ) return -1;
00075     if ( cmdWSET(_state.wm, _state.ssid) ) return -1;
00076 
00077 
00078     switch (_state.sec)
00079     {
00080     case SEC_OPEN:
00081         strcpy(sec,"OPEN");
00082         break;
00083     case SEC_WEP:
00084         strcpy(sec,"WEP");
00085         break;
00086     case SEC_WPA_TKIP:
00087         strcpy(sec,"WPA");
00088         break;
00089     case SEC_WPA2_MIXED:
00090         strcpy(sec,"WPA2");
00091         break;
00092     default:
00093         strcpy(sec,"");
00094         break;
00095     }
00096         
00097     if ( cmdWSEC(_state.wm, _state.pass, sec) ) return -1;
00098     if ( cmdWJOIN() )   return -1;;
00099     _state.associated = true;
00100 
00101     return 0;
00102 }
00103 
00104 bool WizFi310::isAssociated()
00105 {
00106     return _state.associated;
00107 }
00108 
00109 int WizFi310::setMacAddress (const char *mac)
00110 {
00111     if (cmdMMAC(mac)) return -1;
00112     strncpy(_state.mac, mac, sizeof(_state.mac));
00113     return 0;
00114 }
00115 
00116 int WizFi310::getMacAddress (char *mac)
00117 {
00118     if (cmdMMAC())  return -1;
00119     strcpy(mac, _state.mac);
00120     return 0;
00121 }
00122 
00123 int WizFi310::setAddress (const char *name)
00124 {
00125     _state.dhcp = true;
00126     strncpy(_state.name, name, sizeof(_state.name));
00127     return 0;
00128 }
00129 
00130 int WizFi310::setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns, const char *name)
00131 {
00132     _state.dhcp = false;
00133     strncpy(_state.ip, ip, sizeof(_state.ip));
00134     strncpy(_state.netmask, netmask, sizeof(_state.netmask));
00135     strncpy(_state.gateway, gateway, sizeof(_state.gateway));
00136     strncpy(_state.nameserver, dns, sizeof(_state.nameserver));
00137     strncpy(_state.name, name, sizeof(_state.name));
00138     return 0;
00139 }
00140 
00141 int WizFi310::getAddress (char *ip, char *netmask, char *gateway)
00142 {
00143     strcpy(ip, _state.ip);
00144     strcpy(netmask, _state.netmask);
00145     strcpy(gateway, _state.gateway);
00146     return 0;
00147 }
00148 
00149 int WizFi310::setSsid (const char *ssid)
00150 {
00151     strncpy(_state.ssid, ssid, sizeof(_state.ssid));
00152     return 0;
00153 }
00154 
00155 int WizFi310::setSec ( Security sec, const char *phrase )
00156 {
00157     _state.sec = sec;
00158     strncpy(_state.pass, phrase, strlen(phrase));
00159     return 0;
00160 }
00161 
00162 const char *WizFi310::getIPAddress(void)
00163 {
00164     return _state.ip;
00165 }
00166 
00167 const char *WizFi310::getMACAddress(void)
00168 {
00169     return _state.mac;
00170 }
00171