This library can be used in mbed driver or mbed OS2. So If you want to use WizFi310 on mbed OS5, You have to use another WizFi310 library(wizfi310-driver). That is git repository for wizfi310-driver. - https://github.com/ARMmbed/wizfi310-driver

Dependents:   KT_IoTMakers_WizFi310_Example WizFi310_STATION_HelloWorld WizFi310_DNS_TCP_HelloWorld WizFi310_Ubidots ... more

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