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