update
Fork of WizFi310Interface_Legacynew by
WizFi310/WizFi310.cpp@4:81d7f0d73aa5, 2018-09-20 (annotated)
- Committer:
- HarshaDRAGNEEL
- Date:
- Thu Sep 20 11:06:02 2018 +0000
- Revision:
- 4:81d7f0d73aa5
- Parent:
- 1:e08ea0ee2788
update
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 | /* |
kaizen | 0:774ff1e8b26b | 49 | wait_ms(500); |
kaizen | 0:774ff1e8b26b | 50 | cmdAT(); |
kaizen | 0:774ff1e8b26b | 51 | cmdMECHO(false); |
kaizen | 0:774ff1e8b26b | 52 | if(cts != NC && rts != NC) |
kaizen | 0:774ff1e8b26b | 53 | cmdUSET(baud,"HW"); |
kaizen | 0:774ff1e8b26b | 54 | else |
kaizen | 0:774ff1e8b26b | 55 | cmdUSET(baud,"N"); |
kaizen | 0:774ff1e8b26b | 56 | |
kaizen | 0:774ff1e8b26b | 57 | // WizFi310 will restart by cmdUSET command. |
kaizen | 0:774ff1e8b26b | 58 | wait_ms(1000); |
kaizen | 0:774ff1e8b26b | 59 | cmdAT(); |
kaizen | 0:774ff1e8b26b | 60 | */ |
kaizen | 0:774ff1e8b26b | 61 | } |
kaizen | 0:774ff1e8b26b | 62 | |
kaizen | 0:774ff1e8b26b | 63 | int WizFi310::join(WiFiMode mode) |
kaizen | 0:774ff1e8b26b | 64 | { |
kaizen | 0:774ff1e8b26b | 65 | char sec[10]; |
kaizen | 0:774ff1e8b26b | 66 | |
kaizen | 0:774ff1e8b26b | 67 | if( cmdMMAC() ) return -1; |
kaizen | 0:774ff1e8b26b | 68 | |
kaizen | 0:774ff1e8b26b | 69 | if(mode == WM_AP) |
kaizen | 0:774ff1e8b26b | 70 | _state.wm = WM_AP; |
kaizen | 0:774ff1e8b26b | 71 | else |
kaizen | 0:774ff1e8b26b | 72 | _state.wm = WM_STATION; |
kaizen | 0:774ff1e8b26b | 73 | |
kaizen | 0:774ff1e8b26b | 74 | if ( cmdWNET(_state.dhcp) ) return -1; |
kaizen | 0:774ff1e8b26b | 75 | if ( cmdWSET(_state.wm, _state.ssid) ) return -1; |
kaizen | 0:774ff1e8b26b | 76 | |
kaizen | 0:774ff1e8b26b | 77 | |
kaizen | 0:774ff1e8b26b | 78 | switch (_state.sec) |
kaizen | 0:774ff1e8b26b | 79 | { |
kaizen | 0:774ff1e8b26b | 80 | case SEC_OPEN: |
kaizen | 0:774ff1e8b26b | 81 | strcpy(sec,"OPEN"); |
kaizen | 0:774ff1e8b26b | 82 | break; |
kaizen | 0:774ff1e8b26b | 83 | case SEC_WEP: |
kaizen | 0:774ff1e8b26b | 84 | strcpy(sec,"WEP"); |
kaizen | 0:774ff1e8b26b | 85 | break; |
kaizen | 0:774ff1e8b26b | 86 | case SEC_WPA_TKIP: |
kaizen | 0:774ff1e8b26b | 87 | strcpy(sec,"WPA"); |
kaizen | 0:774ff1e8b26b | 88 | break; |
kaizen | 0:774ff1e8b26b | 89 | case SEC_WPA2_MIXED: |
kaizen | 0:774ff1e8b26b | 90 | strcpy(sec,"WPA2"); |
kaizen | 0:774ff1e8b26b | 91 | break; |
kaizen | 0:774ff1e8b26b | 92 | default: |
kaizen | 0:774ff1e8b26b | 93 | strcpy(sec,""); |
kaizen | 0:774ff1e8b26b | 94 | break; |
kaizen | 0:774ff1e8b26b | 95 | } |
kaizen | 0:774ff1e8b26b | 96 | |
kaizen | 0:774ff1e8b26b | 97 | if ( cmdWSEC(_state.wm, _state.pass, sec) ) return -1; |
kaizen | 0:774ff1e8b26b | 98 | if ( cmdWJOIN() ) return -1;; |
kaizen | 0:774ff1e8b26b | 99 | _state.associated = true; |
kaizen | 0:774ff1e8b26b | 100 | |
kaizen | 0:774ff1e8b26b | 101 | return 0; |
kaizen | 0:774ff1e8b26b | 102 | } |
kaizen | 0:774ff1e8b26b | 103 | |
kaizen | 0:774ff1e8b26b | 104 | bool WizFi310::isAssociated() |
kaizen | 0:774ff1e8b26b | 105 | { |
kaizen | 0:774ff1e8b26b | 106 | return _state.associated; |
kaizen | 0:774ff1e8b26b | 107 | } |
kaizen | 0:774ff1e8b26b | 108 | |
kaizen | 0:774ff1e8b26b | 109 | int WizFi310::setMacAddress (const char *mac) |
kaizen | 0:774ff1e8b26b | 110 | { |
kaizen | 0:774ff1e8b26b | 111 | if (cmdMMAC(mac)) return -1; |
kaizen | 0:774ff1e8b26b | 112 | strncpy(_state.mac, mac, sizeof(_state.mac)); |
kaizen | 0:774ff1e8b26b | 113 | return 0; |
kaizen | 0:774ff1e8b26b | 114 | } |
kaizen | 0:774ff1e8b26b | 115 | |
kaizen | 0:774ff1e8b26b | 116 | int WizFi310::getMacAddress (char *mac) |
kaizen | 0:774ff1e8b26b | 117 | { |
kaizen | 0:774ff1e8b26b | 118 | if (cmdMMAC()) return -1; |
kaizen | 0:774ff1e8b26b | 119 | strcpy(mac, _state.mac); |
kaizen | 0:774ff1e8b26b | 120 | return 0; |
kaizen | 0:774ff1e8b26b | 121 | } |
kaizen | 0:774ff1e8b26b | 122 | |
kaizen | 0:774ff1e8b26b | 123 | int WizFi310::setAddress (const char *name) |
kaizen | 0:774ff1e8b26b | 124 | { |
kaizen | 0:774ff1e8b26b | 125 | _state.dhcp = true; |
kaizen | 0:774ff1e8b26b | 126 | strncpy(_state.name, name, sizeof(_state.name)); |
kaizen | 0:774ff1e8b26b | 127 | return 0; |
kaizen | 0:774ff1e8b26b | 128 | } |
kaizen | 0:774ff1e8b26b | 129 | |
kaizen | 0:774ff1e8b26b | 130 | int WizFi310::setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns, const char *name) |
kaizen | 0:774ff1e8b26b | 131 | { |
kaizen | 0:774ff1e8b26b | 132 | _state.dhcp = false; |
kaizen | 0:774ff1e8b26b | 133 | strncpy(_state.ip, ip, sizeof(_state.ip)); |
kaizen | 0:774ff1e8b26b | 134 | strncpy(_state.netmask, netmask, sizeof(_state.netmask)); |
kaizen | 0:774ff1e8b26b | 135 | strncpy(_state.gateway, gateway, sizeof(_state.gateway)); |
kaizen | 0:774ff1e8b26b | 136 | strncpy(_state.nameserver, dns, sizeof(_state.nameserver)); |
kaizen | 0:774ff1e8b26b | 137 | strncpy(_state.name, name, sizeof(_state.name)); |
kaizen | 0:774ff1e8b26b | 138 | return 0; |
kaizen | 0:774ff1e8b26b | 139 | } |
kaizen | 0:774ff1e8b26b | 140 | |
kaizen | 0:774ff1e8b26b | 141 | int WizFi310::getAddress (char *ip, char *netmask, char *gateway) |
kaizen | 0:774ff1e8b26b | 142 | { |
kaizen | 0:774ff1e8b26b | 143 | strcpy(ip, _state.ip); |
kaizen | 0:774ff1e8b26b | 144 | strcpy(netmask, _state.netmask); |
kaizen | 0:774ff1e8b26b | 145 | strcpy(gateway, _state.gateway); |
kaizen | 0:774ff1e8b26b | 146 | return 0; |
kaizen | 0:774ff1e8b26b | 147 | } |
kaizen | 0:774ff1e8b26b | 148 | |
kaizen | 0:774ff1e8b26b | 149 | int WizFi310::setSsid (const char *ssid) |
kaizen | 0:774ff1e8b26b | 150 | { |
kaizen | 0:774ff1e8b26b | 151 | strncpy(_state.ssid, ssid, sizeof(_state.ssid)); |
kaizen | 0:774ff1e8b26b | 152 | return 0; |
kaizen | 0:774ff1e8b26b | 153 | } |
kaizen | 0:774ff1e8b26b | 154 | |
kaizen | 0:774ff1e8b26b | 155 | int WizFi310::setSec ( Security sec, const char *phrase ) |
kaizen | 0:774ff1e8b26b | 156 | { |
kaizen | 0:774ff1e8b26b | 157 | _state.sec = sec; |
kaizen | 0:774ff1e8b26b | 158 | strncpy(_state.pass, phrase, strlen(phrase)); |
kaizen | 0:774ff1e8b26b | 159 | return 0; |
kaizen | 0:774ff1e8b26b | 160 | } |
kaizen | 0:774ff1e8b26b | 161 | |
kaizen | 0:774ff1e8b26b | 162 | const char *WizFi310::getIPAddress(void) |
kaizen | 0:774ff1e8b26b | 163 | { |
kaizen | 0:774ff1e8b26b | 164 | return _state.ip; |
kaizen | 0:774ff1e8b26b | 165 | } |
kaizen | 0:774ff1e8b26b | 166 | |
kaizen | 0:774ff1e8b26b | 167 | const char *WizFi310::getMACAddress(void) |
kaizen | 0:774ff1e8b26b | 168 | { |
kaizen | 0:774ff1e8b26b | 169 | return _state.mac; |
kaizen | 0:774ff1e8b26b | 170 | } |
kaizen | 0:774ff1e8b26b | 171 |