test

Fork of WizFi250Interface by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi250.cpp Source File

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