This is library for using WizFi250

Dependents:   WebSocket_WizFi250_HelloWorld IFTTT_WizFi250 AxedaGo-WizFi250 FANARM_AP_udp_server ... more

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     wait_ms(500);
00042 
00043     cmdAT();
00044     cmdMECHO(false);
00045     if(cts != NC && rts != NC)
00046         cmdUSET(baud,"HW");
00047     else
00048         cmdUSET(baud,"N");
00049 
00050     // WizFi250 will restart by cmdUSET command.
00051     wait_ms(1000);
00052     cmdAT();
00053 }
00054 
00055 int WizFi250::join(WiFiMode mode)
00056 {
00057     char sec[10];
00058 
00059     if( cmdMMAC() ) return -1;
00060 
00061     if(mode == WM_AP)
00062         _state.wm = WM_AP;
00063     else
00064         _state.wm = WM_STATION;
00065 
00066     if ( cmdWNET(_state.dhcp) ) return -1;
00067     if ( cmdWSET(_state.wm, _state.ssid) ) return -1;
00068 
00069     switch (_state.sec)
00070     {
00071     case SEC_AUTO:
00072         strcpy(sec,"");
00073         break;
00074     case SEC_OPEN:
00075         strcpy(sec,"OPEN");
00076         break;
00077     case SEC_WEP:
00078         strcpy(sec,"WEP");
00079         break;
00080     case SEC_WPA_TKIP:
00081         strcpy(sec,"WPA");
00082         break;
00083     case SEC_WPA_AES:
00084         strcpy(sec,"WPAAES");
00085         break;
00086     case SEC_WPA2_AES:
00087         strcpy(sec,"WPA2AES");
00088         break;
00089     case SEC_WPA2_TKIP:
00090         strcpy(sec,"WPA2TKIP");
00091         break;
00092     case SEC_WPA2_MIXED:
00093         strcpy(sec,"WPA2");
00094         break;
00095     }
00096     if ( cmdWSEC(_state.wm, _state.pass, sec) ) return -1;
00097     if ( cmdWJOIN() )   return -1;;
00098     _state.associated = true;
00099 
00100     return 0;
00101 }
00102 
00103 bool WizFi250::isAssociated()
00104 {
00105     return _state.associated;
00106 }
00107 
00108 int WizFi250::setMacAddress (const char *mac)
00109 {
00110     if (cmdMMAC(mac)) return -1;
00111     strncpy(_state.mac, mac, sizeof(_state.mac));
00112     return 0;
00113 }
00114 
00115 int WizFi250::getMacAddress (char *mac)
00116 {
00117     if (cmdMMAC())  return -1;
00118     strcpy(mac, _state.mac);
00119     return 0;
00120 }
00121 
00122 int WizFi250::setAddress (const char *name)
00123 {
00124     _state.dhcp = true;
00125     strncpy(_state.name, name, sizeof(_state.name));
00126     return 0;
00127 }
00128 
00129 int WizFi250::setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns, const char *name)
00130 {
00131     _state.dhcp = false;
00132     strncpy(_state.ip, ip, sizeof(_state.ip));
00133     strncpy(_state.netmask, netmask, sizeof(_state.netmask));
00134     strncpy(_state.gateway, gateway, sizeof(_state.gateway));
00135     strncpy(_state.nameserver, dns, sizeof(_state.nameserver));
00136     strncpy(_state.name, name, sizeof(_state.name));
00137     return 0;
00138 }
00139 
00140 int WizFi250::getAddress (char *ip, char *netmask, char *gateway)
00141 {
00142     strcpy(ip, _state.ip);
00143     strcpy(netmask, _state.netmask);
00144     strcpy(gateway, _state.gateway);
00145     return 0;
00146 }
00147 
00148 int WizFi250::setSsid (const char *ssid)
00149 {
00150     strncpy(_state.ssid, ssid, sizeof(_state.ssid));
00151     return 0;
00152 }
00153 
00154 int WizFi250::setSec ( Security sec, const char *phrase )
00155 {
00156     _state.sec = sec;
00157     strncpy(_state.pass, phrase, strlen(phrase));
00158     return 0;
00159 }
00160