ThingPlug Test
Dependents: WizFi310_ThingPlug_Test WizFi310_ThingPlug_Test_P
Fork of WizFi310Interface by
WizFi310/WizFi310.cpp@0:df571f8f8c03, 2016-10-05 (annotated)
- Committer:
- jehoon
- Date:
- Wed Oct 05 09:40:30 2016 +0000
- Revision:
- 0:df571f8f8c03
- Child:
- 1:16e57103a7dd
This is a WizFi310 Library. It supports new mbed5 features including NetworkInterfaceAPI.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jehoon | 0:df571f8f8c03 | 1 | /* |
jehoon | 0:df571f8f8c03 | 2 | * Copyright (C) 2013 gsfan, MIT License |
jehoon | 0:df571f8f8c03 | 3 | * |
jehoon | 0:df571f8f8c03 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
jehoon | 0:df571f8f8c03 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
jehoon | 0:df571f8f8c03 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
jehoon | 0:df571f8f8c03 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
jehoon | 0:df571f8f8c03 | 8 | * furnished to do so, subject to the following conditions: |
jehoon | 0:df571f8f8c03 | 9 | * |
jehoon | 0:df571f8f8c03 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
jehoon | 0:df571f8f8c03 | 11 | * substantial portions of the Software. |
jehoon | 0:df571f8f8c03 | 12 | * |
jehoon | 0:df571f8f8c03 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
jehoon | 0:df571f8f8c03 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
jehoon | 0:df571f8f8c03 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
jehoon | 0:df571f8f8c03 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jehoon | 0:df571f8f8c03 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
jehoon | 0:df571f8f8c03 | 18 | */ |
jehoon | 0:df571f8f8c03 | 19 | /* Copyright (C) 2014 Wiznet, MIT License |
jehoon | 0:df571f8f8c03 | 20 | * port to the Wiznet Module WizFi310 |
jehoon | 0:df571f8f8c03 | 21 | */ |
jehoon | 0:df571f8f8c03 | 22 | |
jehoon | 0:df571f8f8c03 | 23 | #include "mbed.h" |
jehoon | 0:df571f8f8c03 | 24 | #include "WizFi310.h" |
jehoon | 0:df571f8f8c03 | 25 | |
jehoon | 0:df571f8f8c03 | 26 | WizFi310 * WizFi310::_inst; |
jehoon | 0:df571f8f8c03 | 27 | |
jehoon | 0:df571f8f8c03 | 28 | |
jehoon | 0:df571f8f8c03 | 29 | WizFi310::WizFi310(PinName tx,PinName rx,PinName cts, PinName rts,PinName reset, PinName alarm, int baud): |
jehoon | 0:df571f8f8c03 | 30 | _wizfi(tx,rx), _reset(reset) |
jehoon | 0:df571f8f8c03 | 31 | { |
jehoon | 0:df571f8f8c03 | 32 | _inst = this; |
jehoon | 0:df571f8f8c03 | 33 | memset(&_state, 0, sizeof(_state)); |
jehoon | 0:df571f8f8c03 | 34 | memset(&_con, 0, sizeof(_con)); |
jehoon | 0:df571f8f8c03 | 35 | _state.initialized = false; |
jehoon | 0:df571f8f8c03 | 36 | _state.status = STAT_READY; |
jehoon | 0:df571f8f8c03 | 37 | _state.cid = -1; |
jehoon | 0:df571f8f8c03 | 38 | _state.buf = new CircBuffer<char>(CFG_DATA_SIZE); |
jehoon | 0:df571f8f8c03 | 39 | |
jehoon | 0:df571f8f8c03 | 40 | initUart(cts, rts, alarm, baud); |
jehoon | 0:df571f8f8c03 | 41 | _reset.output(); |
jehoon | 0:df571f8f8c03 | 42 | |
jehoon | 0:df571f8f8c03 | 43 | setRts(true); // release |
jehoon | 0:df571f8f8c03 | 44 | /* |
jehoon | 0:df571f8f8c03 | 45 | wait_ms(500); |
jehoon | 0:df571f8f8c03 | 46 | cmdAT(); |
jehoon | 0:df571f8f8c03 | 47 | cmdMECHO(false); |
jehoon | 0:df571f8f8c03 | 48 | if(cts != NC && rts != NC) |
jehoon | 0:df571f8f8c03 | 49 | cmdUSET(baud,"HW"); |
jehoon | 0:df571f8f8c03 | 50 | else |
jehoon | 0:df571f8f8c03 | 51 | cmdUSET(baud,"N"); |
jehoon | 0:df571f8f8c03 | 52 | |
jehoon | 0:df571f8f8c03 | 53 | // WizFi310 will restart by cmdUSET command. |
jehoon | 0:df571f8f8c03 | 54 | wait_ms(1000); |
jehoon | 0:df571f8f8c03 | 55 | cmdAT(); |
jehoon | 0:df571f8f8c03 | 56 | */ |
jehoon | 0:df571f8f8c03 | 57 | } |
jehoon | 0:df571f8f8c03 | 58 | |
jehoon | 0:df571f8f8c03 | 59 | int WizFi310::join(WiFiMode mode) |
jehoon | 0:df571f8f8c03 | 60 | { |
jehoon | 0:df571f8f8c03 | 61 | char sec[10]; |
jehoon | 0:df571f8f8c03 | 62 | |
jehoon | 0:df571f8f8c03 | 63 | if( cmdMMAC() ) return -1; |
jehoon | 0:df571f8f8c03 | 64 | |
jehoon | 0:df571f8f8c03 | 65 | if(mode == WM_AP) |
jehoon | 0:df571f8f8c03 | 66 | _state.wm = WM_AP; |
jehoon | 0:df571f8f8c03 | 67 | else |
jehoon | 0:df571f8f8c03 | 68 | _state.wm = WM_STATION; |
jehoon | 0:df571f8f8c03 | 69 | |
jehoon | 0:df571f8f8c03 | 70 | if ( cmdWNET(_state.dhcp) ) return -1; |
jehoon | 0:df571f8f8c03 | 71 | if ( cmdWSET(_state.wm, _state.ssid) ) return -1; |
jehoon | 0:df571f8f8c03 | 72 | |
jehoon | 0:df571f8f8c03 | 73 | //NSAPI_SECURITY_NONE = 0, /*!< open access point */ |
jehoon | 0:df571f8f8c03 | 74 | // NSAPI_SECURITY_WEP, /*!< phrase conforms to WEP */ |
jehoon | 0:df571f8f8c03 | 75 | // NSAPI_SECURITY_WPA, /*!< phrase conforms to WPA */ |
jehoon | 0:df571f8f8c03 | 76 | // NSAPI_SECURITY_WPA2, /*!< phrase conforms to WPA2 */ |
jehoon | 0:df571f8f8c03 | 77 | |
jehoon | 0:df571f8f8c03 | 78 | switch (_state.sec) |
jehoon | 0:df571f8f8c03 | 79 | { |
jehoon | 0:df571f8f8c03 | 80 | case NSAPI_SECURITY_NONE: |
jehoon | 0:df571f8f8c03 | 81 | strcpy(sec,"OPEN"); |
jehoon | 0:df571f8f8c03 | 82 | break; |
jehoon | 0:df571f8f8c03 | 83 | case NSAPI_SECURITY_WEP: |
jehoon | 0:df571f8f8c03 | 84 | strcpy(sec,"WEP"); |
jehoon | 0:df571f8f8c03 | 85 | break; |
jehoon | 0:df571f8f8c03 | 86 | case NSAPI_SECURITY_WPA: |
jehoon | 0:df571f8f8c03 | 87 | strcpy(sec,"WPA"); |
jehoon | 0:df571f8f8c03 | 88 | break; |
jehoon | 0:df571f8f8c03 | 89 | case NSAPI_SECURITY_WPA2: |
jehoon | 0:df571f8f8c03 | 90 | strcpy(sec,"WPA2"); |
jehoon | 0:df571f8f8c03 | 91 | break; |
jehoon | 0:df571f8f8c03 | 92 | default: |
jehoon | 0:df571f8f8c03 | 93 | strcpy(sec,""); |
jehoon | 0:df571f8f8c03 | 94 | break; |
jehoon | 0:df571f8f8c03 | 95 | } |
jehoon | 0:df571f8f8c03 | 96 | |
jehoon | 0:df571f8f8c03 | 97 | |
jehoon | 0:df571f8f8c03 | 98 | /* daniel |
jehoon | 0:df571f8f8c03 | 99 | switch (_state.sec) |
jehoon | 0:df571f8f8c03 | 100 | { |
jehoon | 0:df571f8f8c03 | 101 | case SEC_AUTO: |
jehoon | 0:df571f8f8c03 | 102 | strcpy(sec,""); |
jehoon | 0:df571f8f8c03 | 103 | break; |
jehoon | 0:df571f8f8c03 | 104 | case SEC_OPEN: |
jehoon | 0:df571f8f8c03 | 105 | strcpy(sec,"OPEN"); |
jehoon | 0:df571f8f8c03 | 106 | break; |
jehoon | 0:df571f8f8c03 | 107 | case SEC_WEP: |
jehoon | 0:df571f8f8c03 | 108 | strcpy(sec,"WEP"); |
jehoon | 0:df571f8f8c03 | 109 | break; |
jehoon | 0:df571f8f8c03 | 110 | case SEC_WPA_TKIP: |
jehoon | 0:df571f8f8c03 | 111 | strcpy(sec,"WPA"); |
jehoon | 0:df571f8f8c03 | 112 | break; |
jehoon | 0:df571f8f8c03 | 113 | case SEC_WPA_AES: |
jehoon | 0:df571f8f8c03 | 114 | strcpy(sec,"WPAAES"); |
jehoon | 0:df571f8f8c03 | 115 | break; |
jehoon | 0:df571f8f8c03 | 116 | case SEC_WPA2_AES: |
jehoon | 0:df571f8f8c03 | 117 | strcpy(sec,"WPA2AES"); |
jehoon | 0:df571f8f8c03 | 118 | break; |
jehoon | 0:df571f8f8c03 | 119 | case SEC_WPA2_TKIP: |
jehoon | 0:df571f8f8c03 | 120 | strcpy(sec,"WPA2TKIP"); |
jehoon | 0:df571f8f8c03 | 121 | break; |
jehoon | 0:df571f8f8c03 | 122 | case SEC_WPA2_MIXED: |
jehoon | 0:df571f8f8c03 | 123 | strcpy(sec,"WPA2"); |
jehoon | 0:df571f8f8c03 | 124 | break; |
jehoon | 0:df571f8f8c03 | 125 | } |
jehoon | 0:df571f8f8c03 | 126 | */ |
jehoon | 0:df571f8f8c03 | 127 | |
jehoon | 0:df571f8f8c03 | 128 | if ( cmdWSEC(_state.wm, _state.pass, sec) ) return -1; |
jehoon | 0:df571f8f8c03 | 129 | if ( cmdWJOIN() ) return -1;; |
jehoon | 0:df571f8f8c03 | 130 | _state.associated = true; |
jehoon | 0:df571f8f8c03 | 131 | |
jehoon | 0:df571f8f8c03 | 132 | return 0; |
jehoon | 0:df571f8f8c03 | 133 | } |
jehoon | 0:df571f8f8c03 | 134 | |
jehoon | 0:df571f8f8c03 | 135 | bool WizFi310::isAssociated() |
jehoon | 0:df571f8f8c03 | 136 | { |
jehoon | 0:df571f8f8c03 | 137 | return _state.associated; |
jehoon | 0:df571f8f8c03 | 138 | } |
jehoon | 0:df571f8f8c03 | 139 | |
jehoon | 0:df571f8f8c03 | 140 | int WizFi310::setMacAddress (const char *mac) |
jehoon | 0:df571f8f8c03 | 141 | { |
jehoon | 0:df571f8f8c03 | 142 | if (cmdMMAC(mac)) return -1; |
jehoon | 0:df571f8f8c03 | 143 | strncpy(_state.mac, mac, sizeof(_state.mac)); |
jehoon | 0:df571f8f8c03 | 144 | return 0; |
jehoon | 0:df571f8f8c03 | 145 | } |
jehoon | 0:df571f8f8c03 | 146 | |
jehoon | 0:df571f8f8c03 | 147 | int WizFi310::getMacAddress (char *mac) |
jehoon | 0:df571f8f8c03 | 148 | { |
jehoon | 0:df571f8f8c03 | 149 | if (cmdMMAC()) return -1; |
jehoon | 0:df571f8f8c03 | 150 | strcpy(mac, _state.mac); |
jehoon | 0:df571f8f8c03 | 151 | return 0; |
jehoon | 0:df571f8f8c03 | 152 | } |
jehoon | 0:df571f8f8c03 | 153 | |
jehoon | 0:df571f8f8c03 | 154 | int WizFi310::setAddress (const char *name) |
jehoon | 0:df571f8f8c03 | 155 | { |
jehoon | 0:df571f8f8c03 | 156 | _state.dhcp = true; |
jehoon | 0:df571f8f8c03 | 157 | strncpy(_state.name, name, sizeof(_state.name)); |
jehoon | 0:df571f8f8c03 | 158 | return 0; |
jehoon | 0:df571f8f8c03 | 159 | } |
jehoon | 0:df571f8f8c03 | 160 | |
jehoon | 0:df571f8f8c03 | 161 | int WizFi310::setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns, const char *name) |
jehoon | 0:df571f8f8c03 | 162 | { |
jehoon | 0:df571f8f8c03 | 163 | _state.dhcp = false; |
jehoon | 0:df571f8f8c03 | 164 | strncpy(_state.ip, ip, sizeof(_state.ip)); |
jehoon | 0:df571f8f8c03 | 165 | strncpy(_state.netmask, netmask, sizeof(_state.netmask)); |
jehoon | 0:df571f8f8c03 | 166 | strncpy(_state.gateway, gateway, sizeof(_state.gateway)); |
jehoon | 0:df571f8f8c03 | 167 | strncpy(_state.nameserver, dns, sizeof(_state.nameserver)); |
jehoon | 0:df571f8f8c03 | 168 | strncpy(_state.name, name, sizeof(_state.name)); |
jehoon | 0:df571f8f8c03 | 169 | return 0; |
jehoon | 0:df571f8f8c03 | 170 | } |
jehoon | 0:df571f8f8c03 | 171 | |
jehoon | 0:df571f8f8c03 | 172 | int WizFi310::getAddress (char *ip, char *netmask, char *gateway) |
jehoon | 0:df571f8f8c03 | 173 | { |
jehoon | 0:df571f8f8c03 | 174 | strcpy(ip, _state.ip); |
jehoon | 0:df571f8f8c03 | 175 | strcpy(netmask, _state.netmask); |
jehoon | 0:df571f8f8c03 | 176 | strcpy(gateway, _state.gateway); |
jehoon | 0:df571f8f8c03 | 177 | return 0; |
jehoon | 0:df571f8f8c03 | 178 | } |
jehoon | 0:df571f8f8c03 | 179 | |
jehoon | 0:df571f8f8c03 | 180 | int WizFi310::setSsid (const char *ssid) |
jehoon | 0:df571f8f8c03 | 181 | { |
jehoon | 0:df571f8f8c03 | 182 | strncpy(_state.ssid, ssid, sizeof(_state.ssid)); |
jehoon | 0:df571f8f8c03 | 183 | return 0; |
jehoon | 0:df571f8f8c03 | 184 | } |
jehoon | 0:df571f8f8c03 | 185 | |
jehoon | 0:df571f8f8c03 | 186 | //daniel |
jehoon | 0:df571f8f8c03 | 187 | //int WizFi310::setSec ( Security sec, const char *phrase ) |
jehoon | 0:df571f8f8c03 | 188 | int WizFi310::setSec ( nsapi_security_t sec, const char *phrase ) |
jehoon | 0:df571f8f8c03 | 189 | { |
jehoon | 0:df571f8f8c03 | 190 | _state.sec = sec; |
jehoon | 0:df571f8f8c03 | 191 | strncpy(_state.pass, phrase, strlen(phrase)); |
jehoon | 0:df571f8f8c03 | 192 | return 0; |
jehoon | 0:df571f8f8c03 | 193 | } |
jehoon | 0:df571f8f8c03 | 194 | |
jehoon | 0:df571f8f8c03 | 195 | const char *WizFi310::getIPAddress(void) |
jehoon | 0:df571f8f8c03 | 196 | { |
jehoon | 0:df571f8f8c03 | 197 | return _state.ip; |
jehoon | 0:df571f8f8c03 | 198 | } |
jehoon | 0:df571f8f8c03 | 199 | |
jehoon | 0:df571f8f8c03 | 200 | const char *WizFi310::getMACAddress(void) |
jehoon | 0:df571f8f8c03 | 201 | { |
jehoon | 0:df571f8f8c03 | 202 | return _state.mac; |
jehoon | 0:df571f8f8c03 | 203 | } |
jehoon | 0:df571f8f8c03 | 204 |