RC
Dependents: WizFi250_AP_HelloWorld
Fork of WizFi250Interface by
WizFi250/WizFi250.cpp@7:ba28fe711055, 2014-11-14 (annotated)
- Committer:
- DongEun Koak
- Date:
- Fri Nov 14 15:27:47 2014 +0900
- Revision:
- 7:ba28fe711055
- Parent:
- 6:e149bf87a1bd
- Child:
- 10:187e3fd24123
Fixed bug in init function of WizFi250.
Removed sendCommand for operating polling in order to wait response.
Fixed some bug.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kaizen | 0:f2039204c8f6 | 1 | /* |
DongEun Koak | 4:085e7efeae47 | 2 | * Copyright (C) 2013 gsfan, MIT License |
kaizen | 0:f2039204c8f6 | 3 | * |
kaizen | 0:f2039204c8f6 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
kaizen | 0:f2039204c8f6 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
kaizen | 0:f2039204c8f6 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
kaizen | 0:f2039204c8f6 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
kaizen | 0:f2039204c8f6 | 8 | * furnished to do so, subject to the following conditions: |
kaizen | 0:f2039204c8f6 | 9 | * |
kaizen | 0:f2039204c8f6 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
kaizen | 0:f2039204c8f6 | 11 | * substantial portions of the Software. |
kaizen | 0:f2039204c8f6 | 12 | * |
kaizen | 0:f2039204c8f6 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
kaizen | 0:f2039204c8f6 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
kaizen | 0:f2039204c8f6 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kaizen | 0:f2039204c8f6 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kaizen | 0:f2039204c8f6 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kaizen | 0:f2039204c8f6 | 18 | */ |
kaizen | 0:f2039204c8f6 | 19 | /* Copyright (C) 2014 Wiznet, MIT License |
kaizen | 0:f2039204c8f6 | 20 | * port to the Wiznet Module WizFi250 |
kaizen | 0:f2039204c8f6 | 21 | */ |
kaizen | 0:f2039204c8f6 | 22 | |
kaizen | 0:f2039204c8f6 | 23 | #include "mbed.h" |
kaizen | 0:f2039204c8f6 | 24 | #include "WizFi250.h" |
kaizen | 0:f2039204c8f6 | 25 | |
kaizen | 0:f2039204c8f6 | 26 | WizFi250 * WizFi250::_inst; |
kaizen | 0:f2039204c8f6 | 27 | |
kaizen | 0:f2039204c8f6 | 28 | |
DongEun Koak | 6:e149bf87a1bd | 29 | WizFi250::WizFi250(PinName tx,PinName rx,PinName cts, PinName rts,PinName reset, PinName alarm, int baud, mbed::Serial *debug): |
DongEun Koak | 6:e149bf87a1bd | 30 | _wizfi(tx,rx), _reset(reset) |
kaizen | 0:f2039204c8f6 | 31 | { |
kaizen | 0:f2039204c8f6 | 32 | _inst = this; |
kaizen | 0:f2039204c8f6 | 33 | memset(&_state, 0, sizeof(_state)); |
kaizen | 0:f2039204c8f6 | 34 | _state.initialized = false; |
kaizen | 0:f2039204c8f6 | 35 | _state.status = STAT_READY; |
kaizen | 0:f2039204c8f6 | 36 | _state.cid = -1; |
kaizen | 0:f2039204c8f6 | 37 | _state.buf = new CircBuffer<char>(CFG_DATA_SIZE); |
kaizen | 0:f2039204c8f6 | 38 | |
DongEun Koak | 6:e149bf87a1bd | 39 | if(debug == NULL) |
DongEun Koak | 6:e149bf87a1bd | 40 | { |
DongEun Koak | 6:e149bf87a1bd | 41 | _debug = new Serial(USBTX,USBRX); |
DongEun Koak | 6:e149bf87a1bd | 42 | _debug->baud(baud); |
DongEun Koak | 6:e149bf87a1bd | 43 | } |
DongEun Koak | 6:e149bf87a1bd | 44 | else |
DongEun Koak | 6:e149bf87a1bd | 45 | { |
DongEun Koak | 6:e149bf87a1bd | 46 | _debug = debug; |
DongEun Koak | 6:e149bf87a1bd | 47 | _debug->baud(baud); |
DongEun Koak | 6:e149bf87a1bd | 48 | } |
DongEun Koak | 6:e149bf87a1bd | 49 | |
kaizen | 0:f2039204c8f6 | 50 | initUart(cts, rts, alarm, baud); |
DongEun Koak | 7:ba28fe711055 | 51 | wait_ms(500); |
kaizen | 0:f2039204c8f6 | 52 | |
DongEun Koak | 7:ba28fe711055 | 53 | cmdAT(); |
kaizen | 0:f2039204c8f6 | 54 | cmdMECHO(false); |
DongEun Koak | 7:ba28fe711055 | 55 | if(cts != NC && rts != NC) |
DongEun Koak | 7:ba28fe711055 | 56 | cmdUSET(baud,"HW"); |
DongEun Koak | 7:ba28fe711055 | 57 | else |
DongEun Koak | 7:ba28fe711055 | 58 | cmdUSET(baud,"N"); |
DongEun Koak | 7:ba28fe711055 | 59 | |
DongEun Koak | 7:ba28fe711055 | 60 | // WizFi250 will restart by cmdUSET command. |
DongEun Koak | 7:ba28fe711055 | 61 | wait_ms(1000); |
DongEun Koak | 7:ba28fe711055 | 62 | cmdAT(); |
kaizen | 0:f2039204c8f6 | 63 | } |
kaizen | 0:f2039204c8f6 | 64 | |
kaizen | 0:f2039204c8f6 | 65 | int WizFi250::join() |
kaizen | 0:f2039204c8f6 | 66 | { |
kaizen | 0:f2039204c8f6 | 67 | char sec[10]; |
kaizen | 0:f2039204c8f6 | 68 | |
kaizen | 0:f2039204c8f6 | 69 | _state.wm = WM_STATION; |
kaizen | 0:f2039204c8f6 | 70 | if( cmdMMAC() ) return -1; |
kaizen | 0:f2039204c8f6 | 71 | if ( cmdWSET(_state.wm, _state.ssid) ) return -1; |
kaizen | 0:f2039204c8f6 | 72 | |
kaizen | 0:f2039204c8f6 | 73 | switch (_state.sec) |
kaizen | 0:f2039204c8f6 | 74 | { |
kaizen | 0:f2039204c8f6 | 75 | case SEC_AUTO: |
kaizen | 0:f2039204c8f6 | 76 | strcpy(sec,""); |
kaizen | 0:f2039204c8f6 | 77 | break; |
kaizen | 0:f2039204c8f6 | 78 | case SEC_OPEN: |
kaizen | 0:f2039204c8f6 | 79 | strcpy(sec,"OPEN"); |
kaizen | 0:f2039204c8f6 | 80 | break; |
kaizen | 0:f2039204c8f6 | 81 | case SEC_WEP: |
kaizen | 0:f2039204c8f6 | 82 | strcpy(sec,"WEP"); |
kaizen | 0:f2039204c8f6 | 83 | break; |
kaizen | 0:f2039204c8f6 | 84 | case SEC_WPA_TKIP: |
kaizen | 0:f2039204c8f6 | 85 | strcpy(sec,"WPA"); |
kaizen | 0:f2039204c8f6 | 86 | break; |
kaizen | 0:f2039204c8f6 | 87 | case SEC_WPA_AES: |
kaizen | 0:f2039204c8f6 | 88 | strcpy(sec,"WPAAES"); |
kaizen | 0:f2039204c8f6 | 89 | break; |
kaizen | 0:f2039204c8f6 | 90 | case SEC_WPA2_AES: |
kaizen | 0:f2039204c8f6 | 91 | strcpy(sec,"WPA2AES"); |
kaizen | 0:f2039204c8f6 | 92 | break; |
kaizen | 0:f2039204c8f6 | 93 | case SEC_WPA2_TKIP: |
kaizen | 0:f2039204c8f6 | 94 | strcpy(sec,"WPA2TKIP"); |
kaizen | 0:f2039204c8f6 | 95 | break; |
kaizen | 0:f2039204c8f6 | 96 | case SEC_WPA2_MIXED: |
kaizen | 0:f2039204c8f6 | 97 | strcpy(sec,"WPA2"); |
kaizen | 0:f2039204c8f6 | 98 | break; |
kaizen | 0:f2039204c8f6 | 99 | } |
kaizen | 0:f2039204c8f6 | 100 | if ( cmdWSEC(_state.wm, _state.pass, sec) ) return -1; |
kaizen | 0:f2039204c8f6 | 101 | if ( cmdWJOIN() ) return -1;; |
kaizen | 0:f2039204c8f6 | 102 | _state.associated = true; |
kaizen | 0:f2039204c8f6 | 103 | |
kaizen | 0:f2039204c8f6 | 104 | return 0; |
kaizen | 0:f2039204c8f6 | 105 | } |
kaizen | 0:f2039204c8f6 | 106 | |
kaizen | 0:f2039204c8f6 | 107 | bool WizFi250::isAssociated() |
kaizen | 0:f2039204c8f6 | 108 | { |
kaizen | 0:f2039204c8f6 | 109 | return _state.associated; |
kaizen | 0:f2039204c8f6 | 110 | } |
kaizen | 0:f2039204c8f6 | 111 | |
kaizen | 0:f2039204c8f6 | 112 | int WizFi250::setMacAddress (const char *mac) |
kaizen | 0:f2039204c8f6 | 113 | { |
kaizen | 0:f2039204c8f6 | 114 | if (cmdMMAC(mac)) return -1; |
kaizen | 0:f2039204c8f6 | 115 | strncpy(_state.mac, mac, sizeof(_state.mac)); |
kaizen | 0:f2039204c8f6 | 116 | return 0; |
kaizen | 0:f2039204c8f6 | 117 | } |
kaizen | 0:f2039204c8f6 | 118 | |
kaizen | 0:f2039204c8f6 | 119 | int WizFi250::getMacAddress (char *mac) |
kaizen | 0:f2039204c8f6 | 120 | { |
kaizen | 0:f2039204c8f6 | 121 | if (cmdMMAC()) return -1; |
kaizen | 0:f2039204c8f6 | 122 | strcpy(mac, _state.mac); |
kaizen | 0:f2039204c8f6 | 123 | return 0; |
kaizen | 0:f2039204c8f6 | 124 | } |
kaizen | 0:f2039204c8f6 | 125 | |
kaizen | 0:f2039204c8f6 | 126 | int WizFi250::setAddress (const char *name) |
kaizen | 0:f2039204c8f6 | 127 | { |
kaizen | 0:f2039204c8f6 | 128 | _state.dhcp = true; |
kaizen | 0:f2039204c8f6 | 129 | strncpy(_state.name, name, sizeof(_state.name)); |
kaizen | 0:f2039204c8f6 | 130 | return 0; |
kaizen | 0:f2039204c8f6 | 131 | } |
kaizen | 0:f2039204c8f6 | 132 | |
kaizen | 0:f2039204c8f6 | 133 | int WizFi250::setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns, const char *name) |
kaizen | 0:f2039204c8f6 | 134 | { |
kaizen | 0:f2039204c8f6 | 135 | _state.dhcp = false; |
kaizen | 0:f2039204c8f6 | 136 | strncpy(_state.ip, ip, sizeof(_state.ip)); |
kaizen | 0:f2039204c8f6 | 137 | strncpy(_state.netmask, netmask, sizeof(_state.netmask)); |
kaizen | 0:f2039204c8f6 | 138 | strncpy(_state.gateway, gateway, sizeof(_state.gateway)); |
kaizen | 0:f2039204c8f6 | 139 | strncpy(_state.nameserver, dns, sizeof(_state.nameserver)); |
kaizen | 0:f2039204c8f6 | 140 | strncpy(_state.name, name, sizeof(_state.name)); |
kaizen | 0:f2039204c8f6 | 141 | return 0; |
kaizen | 0:f2039204c8f6 | 142 | } |
kaizen | 0:f2039204c8f6 | 143 | |
kaizen | 0:f2039204c8f6 | 144 | int WizFi250::getAddress (char *ip, char *netmask, char *gateway) |
kaizen | 0:f2039204c8f6 | 145 | { |
kaizen | 0:f2039204c8f6 | 146 | strcpy(ip, _state.ip); |
kaizen | 0:f2039204c8f6 | 147 | strcpy(netmask, _state.netmask); |
kaizen | 0:f2039204c8f6 | 148 | strcpy(gateway, _state.gateway); |
kaizen | 0:f2039204c8f6 | 149 | return 0; |
kaizen | 0:f2039204c8f6 | 150 | } |
kaizen | 0:f2039204c8f6 | 151 | |
kaizen | 0:f2039204c8f6 | 152 | int WizFi250::setSsid (const char *ssid) |
kaizen | 0:f2039204c8f6 | 153 | { |
kaizen | 0:f2039204c8f6 | 154 | strncpy(_state.ssid, ssid, sizeof(_state.ssid)); |
kaizen | 0:f2039204c8f6 | 155 | return 0; |
kaizen | 0:f2039204c8f6 | 156 | } |
kaizen | 0:f2039204c8f6 | 157 | |
kaizen | 0:f2039204c8f6 | 158 | int WizFi250::setSec ( Security sec, const char *phrase ) |
kaizen | 0:f2039204c8f6 | 159 | { |
kaizen | 0:f2039204c8f6 | 160 | _state.sec = sec; |
kaizen | 0:f2039204c8f6 | 161 | strncpy(_state.pass, phrase, strlen(phrase)); |
kaizen | 0:f2039204c8f6 | 162 | return 0; |
kaizen | 0:f2039204c8f6 | 163 | } |
kaizen | 0:f2039204c8f6 | 164 |