Host library for controlling a WiConnect enabled Wi-Fi module.
Dependents: wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more
NetworkInterface.cpp@40:4b4306f3d829, 2015-02-23 (annotated)
- Committer:
- dan_ackme
- Date:
- Mon Feb 23 22:21:50 2015 -0800
- Revision:
- 40:4b4306f3d829
- Parent:
- 31:59fac148a8d5
minor bug fixes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dan_ackme | 29:b6af04b77a56 | 1 | /** |
dan_ackme | 29:b6af04b77a56 | 2 | * ACKme WiConnect Host Library is licensed under the BSD licence: |
dan_ackme | 29:b6af04b77a56 | 3 | * |
dan_ackme | 29:b6af04b77a56 | 4 | * Copyright (c)2014 ACKme Networks. |
dan_ackme | 29:b6af04b77a56 | 5 | * All rights reserved. |
dan_ackme | 29:b6af04b77a56 | 6 | * |
dan_ackme | 29:b6af04b77a56 | 7 | * Redistribution and use in source and binary forms, with or without modification, |
dan_ackme | 29:b6af04b77a56 | 8 | * are permitted provided that the following conditions are met: |
dan_ackme | 29:b6af04b77a56 | 9 | * |
dan_ackme | 29:b6af04b77a56 | 10 | * 1. Redistributions of source code must retain the above copyright notice, |
dan_ackme | 29:b6af04b77a56 | 11 | * this list of conditions and the following disclaimer. |
dan_ackme | 29:b6af04b77a56 | 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
dan_ackme | 29:b6af04b77a56 | 13 | * this list of conditions and the following disclaimer in the documentation |
dan_ackme | 29:b6af04b77a56 | 14 | * and/or other materials provided with the distribution. |
dan_ackme | 29:b6af04b77a56 | 15 | * 3. The name of the author may not be used to endorse or promote products |
dan_ackme | 29:b6af04b77a56 | 16 | * derived from this software without specific prior written permission. |
dan_ackme | 29:b6af04b77a56 | 17 | * |
dan_ackme | 29:b6af04b77a56 | 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED |
dan_ackme | 29:b6af04b77a56 | 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
dan_ackme | 29:b6af04b77a56 | 20 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
dan_ackme | 29:b6af04b77a56 | 21 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
dan_ackme | 29:b6af04b77a56 | 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
dan_ackme | 29:b6af04b77a56 | 23 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
dan_ackme | 29:b6af04b77a56 | 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
dan_ackme | 29:b6af04b77a56 | 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
dan_ackme | 29:b6af04b77a56 | 26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
dan_ackme | 29:b6af04b77a56 | 27 | * OF SUCH DAMAGE. |
dan_ackme | 29:b6af04b77a56 | 28 | */ |
dan_ackme | 29:b6af04b77a56 | 29 | |
dan_ackme | 29:b6af04b77a56 | 30 | #include "Wiconnect.h" |
dan_ackme | 29:b6af04b77a56 | 31 | #include "internal/common.h" |
dan_ackme | 29:b6af04b77a56 | 32 | #include "api/StringUtil.h" |
dan_ackme | 29:b6af04b77a56 | 33 | |
dan_ackme | 29:b6af04b77a56 | 34 | |
dan_ackme | 29:b6af04b77a56 | 35 | #define IPV4_FORMAT "%u.%u.%u.%u" |
dan_ackme | 29:b6af04b77a56 | 36 | #define IPV4_ARGS(ip) \ |
dan_ackme | 29:b6af04b77a56 | 37 | (int)(((ip) >> 0) & 0xff), \ |
dan_ackme | 29:b6af04b77a56 | 38 | (int)(((ip) >> 8) & 0xff), \ |
dan_ackme | 29:b6af04b77a56 | 39 | (int)(((ip) >> 16) & 0xff), \ |
dan_ackme | 29:b6af04b77a56 | 40 | (int)(((ip) >> 24) & 0xff) |
dan_ackme | 29:b6af04b77a56 | 41 | |
dan_ackme | 29:b6af04b77a56 | 42 | |
dan_ackme | 29:b6af04b77a56 | 43 | |
dan_ackme | 29:b6af04b77a56 | 44 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 45 | NetworkInterface::NetworkInterface(Wiconnect *wiconnect_) |
dan_ackme | 29:b6af04b77a56 | 46 | { |
dan_ackme | 29:b6af04b77a56 | 47 | wiconnect = wiconnect_; |
dan_ackme | 29:b6af04b77a56 | 48 | } |
dan_ackme | 29:b6af04b77a56 | 49 | |
dan_ackme | 29:b6af04b77a56 | 50 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 51 | WiconnectResult NetworkInterface::ping(const char *domain, uint32_t *timeMsPtr) |
dan_ackme | 29:b6af04b77a56 | 52 | { |
dan_ackme | 29:b6af04b77a56 | 53 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 54 | |
dan_ackme | 29:b6af04b77a56 | 55 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 56 | |
dan_ackme | 29:b6af04b77a56 | 57 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("ping %s", (domain == NULL) ? "-g" : domain)) && |
dan_ackme | 29:b6af04b77a56 | 58 | timeMsPtr != NULL) |
dan_ackme | 29:b6af04b77a56 | 59 | { |
dan_ackme | 29:b6af04b77a56 | 60 | if(sscanf(wiconnect->internalBuffer, "Ping reply in %ums", (unsigned int*)timeMsPtr) != 1) |
dan_ackme | 29:b6af04b77a56 | 61 | { |
dan_ackme | 29:b6af04b77a56 | 62 | result = WICONNECT_RESPONSE_PARSE_ERROR; |
dan_ackme | 29:b6af04b77a56 | 63 | } |
dan_ackme | 29:b6af04b77a56 | 64 | } |
dan_ackme | 29:b6af04b77a56 | 65 | |
dan_ackme | 29:b6af04b77a56 | 66 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 67 | |
dan_ackme | 29:b6af04b77a56 | 68 | return result; |
dan_ackme | 29:b6af04b77a56 | 69 | } |
dan_ackme | 29:b6af04b77a56 | 70 | |
dan_ackme | 29:b6af04b77a56 | 71 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 72 | WiconnectResult NetworkInterface::lookup(const char *domain, uint32_t *ipAddressPtr) |
dan_ackme | 29:b6af04b77a56 | 73 | { |
dan_ackme | 29:b6af04b77a56 | 74 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 75 | |
dan_ackme | 29:b6af04b77a56 | 76 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 77 | |
dan_ackme | 29:b6af04b77a56 | 78 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("nlo %s", domain))) |
dan_ackme | 29:b6af04b77a56 | 79 | { |
dan_ackme | 29:b6af04b77a56 | 80 | if(!NetworkInterface::strToIp(wiconnect->internalBuffer, ipAddressPtr)) |
dan_ackme | 29:b6af04b77a56 | 81 | { |
dan_ackme | 29:b6af04b77a56 | 82 | result = WICONNECT_RESPONSE_PARSE_ERROR; |
dan_ackme | 29:b6af04b77a56 | 83 | } |
dan_ackme | 29:b6af04b77a56 | 84 | } |
dan_ackme | 29:b6af04b77a56 | 85 | |
dan_ackme | 29:b6af04b77a56 | 86 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 87 | |
dan_ackme | 29:b6af04b77a56 | 88 | return result; |
dan_ackme | 29:b6af04b77a56 | 89 | } |
dan_ackme | 29:b6af04b77a56 | 90 | |
dan_ackme | 29:b6af04b77a56 | 91 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 92 | WiconnectResult NetworkInterface::setDhcpEnabled(bool enabled) |
dan_ackme | 29:b6af04b77a56 | 93 | { |
dan_ackme | 29:b6af04b77a56 | 94 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 95 | |
dan_ackme | 29:b6af04b77a56 | 96 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 97 | |
dan_ackme | 29:b6af04b77a56 | 98 | result = wiconnect->sendCommand(CMD_SET_NETWORK_DHCP, enabled); |
dan_ackme | 29:b6af04b77a56 | 99 | |
dan_ackme | 29:b6af04b77a56 | 100 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 101 | |
dan_ackme | 29:b6af04b77a56 | 102 | return result; |
dan_ackme | 29:b6af04b77a56 | 103 | } |
dan_ackme | 29:b6af04b77a56 | 104 | |
dan_ackme | 29:b6af04b77a56 | 105 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 106 | WiconnectResult NetworkInterface::getDhcpEnabled(bool *enabledPtr) |
dan_ackme | 29:b6af04b77a56 | 107 | { |
dan_ackme | 29:b6af04b77a56 | 108 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 109 | |
dan_ackme | 29:b6af04b77a56 | 110 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 111 | |
dan_ackme | 29:b6af04b77a56 | 112 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("get network.dhcp.enabled"))) |
dan_ackme | 29:b6af04b77a56 | 113 | { |
dan_ackme | 29:b6af04b77a56 | 114 | int32_t enabled; |
dan_ackme | 29:b6af04b77a56 | 115 | if(WICONNECT_SUCCEEDED(result, wiconnect->responseToInt32(&enabled))) |
dan_ackme | 29:b6af04b77a56 | 116 | { |
dan_ackme | 29:b6af04b77a56 | 117 | *enabledPtr = (bool)enabled; |
dan_ackme | 29:b6af04b77a56 | 118 | } |
dan_ackme | 29:b6af04b77a56 | 119 | } |
dan_ackme | 29:b6af04b77a56 | 120 | |
dan_ackme | 29:b6af04b77a56 | 121 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 122 | |
dan_ackme | 29:b6af04b77a56 | 123 | return result; |
dan_ackme | 29:b6af04b77a56 | 124 | } |
dan_ackme | 29:b6af04b77a56 | 125 | |
dan_ackme | 29:b6af04b77a56 | 126 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 127 | WiconnectResult NetworkInterface::setIpSettings(uint32_t ip, uint32_t netmask, uint32_t gateway) |
dan_ackme | 29:b6af04b77a56 | 128 | { |
dan_ackme | 29:b6af04b77a56 | 129 | WiconnectResult result = WICONNECT_ERROR; |
dan_ackme | 29:b6af04b77a56 | 130 | |
dan_ackme | 29:b6af04b77a56 | 131 | enum |
dan_ackme | 29:b6af04b77a56 | 132 | { |
dan_ackme | 29:b6af04b77a56 | 133 | FS_SET_IP, |
dan_ackme | 29:b6af04b77a56 | 134 | FS_SET_NETMASK, |
dan_ackme | 29:b6af04b77a56 | 135 | FS_SET_GATEWAY |
dan_ackme | 29:b6af04b77a56 | 136 | }; |
dan_ackme | 29:b6af04b77a56 | 137 | |
dan_ackme | 29:b6af04b77a56 | 138 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 139 | |
dan_ackme | 29:b6af04b77a56 | 140 | if(wiconnect->internalProcessingState == FS_SET_IP) |
dan_ackme | 29:b6af04b77a56 | 141 | { |
dan_ackme | 29:b6af04b77a56 | 142 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(CMD_SET_STATIC_IP, IPV4_ARGS(ip)))) |
dan_ackme | 29:b6af04b77a56 | 143 | { |
dan_ackme | 29:b6af04b77a56 | 144 | wiconnect->internalProcessingState = FS_SET_NETMASK; |
dan_ackme | 29:b6af04b77a56 | 145 | } |
dan_ackme | 29:b6af04b77a56 | 146 | } |
dan_ackme | 29:b6af04b77a56 | 147 | |
dan_ackme | 29:b6af04b77a56 | 148 | if(wiconnect->internalProcessingState == FS_SET_NETMASK) |
dan_ackme | 29:b6af04b77a56 | 149 | { |
dan_ackme | 29:b6af04b77a56 | 150 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(CMD_SET_STATIC_NETMASK, IPV4_ARGS(netmask)))) |
dan_ackme | 29:b6af04b77a56 | 151 | { |
dan_ackme | 29:b6af04b77a56 | 152 | wiconnect->internalProcessingState = FS_SET_GATEWAY; |
dan_ackme | 29:b6af04b77a56 | 153 | } |
dan_ackme | 29:b6af04b77a56 | 154 | } |
dan_ackme | 29:b6af04b77a56 | 155 | |
dan_ackme | 29:b6af04b77a56 | 156 | if(wiconnect->internalProcessingState == FS_SET_GATEWAY) |
dan_ackme | 29:b6af04b77a56 | 157 | { |
dan_ackme | 29:b6af04b77a56 | 158 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(CMD_SET_STATIC_GATEWAY, IPV4_ARGS(gateway)))) |
dan_ackme | 29:b6af04b77a56 | 159 | { |
dan_ackme | 29:b6af04b77a56 | 160 | } |
dan_ackme | 29:b6af04b77a56 | 161 | } |
dan_ackme | 29:b6af04b77a56 | 162 | |
dan_ackme | 29:b6af04b77a56 | 163 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 164 | |
dan_ackme | 29:b6af04b77a56 | 165 | return result; |
dan_ackme | 29:b6af04b77a56 | 166 | } |
dan_ackme | 29:b6af04b77a56 | 167 | |
dan_ackme | 29:b6af04b77a56 | 168 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 169 | WiconnectResult NetworkInterface::setIpSettings(const char* ipStr, const char* netmaskStr, const char* gatewayStr) |
dan_ackme | 29:b6af04b77a56 | 170 | { |
dan_ackme | 29:b6af04b77a56 | 171 | uint32_t ip, nm, gw; |
dan_ackme | 29:b6af04b77a56 | 172 | |
dan_ackme | 29:b6af04b77a56 | 173 | if( !NetworkInterface::strToIp(ipStr, &ip) || |
dan_ackme | 29:b6af04b77a56 | 174 | !NetworkInterface::strToIp(netmaskStr, &nm) || |
dan_ackme | 29:b6af04b77a56 | 175 | !NetworkInterface::strToIp(gatewayStr, &gw)) |
dan_ackme | 29:b6af04b77a56 | 176 | { |
dan_ackme | 29:b6af04b77a56 | 177 | return WICONNECT_ERROR; |
dan_ackme | 29:b6af04b77a56 | 178 | } |
dan_ackme | 29:b6af04b77a56 | 179 | return setIpSettings(ip, nm, gw); |
dan_ackme | 29:b6af04b77a56 | 180 | } |
dan_ackme | 29:b6af04b77a56 | 181 | |
dan_ackme | 29:b6af04b77a56 | 182 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 183 | WiconnectResult NetworkInterface::getIpSettings(uint32_t *ip, uint32_t *netmask, uint32_t *gateway) |
dan_ackme | 29:b6af04b77a56 | 184 | { |
dan_ackme | 29:b6af04b77a56 | 185 | WiconnectResult result = WICONNECT_ERROR; |
dan_ackme | 29:b6af04b77a56 | 186 | |
dan_ackme | 29:b6af04b77a56 | 187 | enum |
dan_ackme | 29:b6af04b77a56 | 188 | { |
dan_ackme | 29:b6af04b77a56 | 189 | FS_GET_IP, |
dan_ackme | 29:b6af04b77a56 | 190 | FS_GET_NETMASK, |
dan_ackme | 29:b6af04b77a56 | 191 | FS_GET_GATEWAY |
dan_ackme | 29:b6af04b77a56 | 192 | }; |
dan_ackme | 29:b6af04b77a56 | 193 | |
dan_ackme | 29:b6af04b77a56 | 194 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 195 | |
dan_ackme | 29:b6af04b77a56 | 196 | if(wiconnect->internalProcessingState == FS_GET_IP) |
dan_ackme | 29:b6af04b77a56 | 197 | { |
dan_ackme | 29:b6af04b77a56 | 198 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("get network.ip"))) |
dan_ackme | 29:b6af04b77a56 | 199 | { |
dan_ackme | 29:b6af04b77a56 | 200 | if(!NetworkInterface::strToIp(wiconnect->internalBuffer, ip)) |
dan_ackme | 29:b6af04b77a56 | 201 | { |
dan_ackme | 29:b6af04b77a56 | 202 | result = WICONNECT_RESPONSE_PARSE_ERROR; |
dan_ackme | 29:b6af04b77a56 | 203 | } |
dan_ackme | 29:b6af04b77a56 | 204 | else |
dan_ackme | 29:b6af04b77a56 | 205 | { |
dan_ackme | 29:b6af04b77a56 | 206 | wiconnect->internalProcessingState = FS_GET_NETMASK; |
dan_ackme | 29:b6af04b77a56 | 207 | } |
dan_ackme | 29:b6af04b77a56 | 208 | } |
dan_ackme | 29:b6af04b77a56 | 209 | } |
dan_ackme | 29:b6af04b77a56 | 210 | |
dan_ackme | 29:b6af04b77a56 | 211 | if(wiconnect->internalProcessingState == FS_GET_NETMASK) |
dan_ackme | 29:b6af04b77a56 | 212 | { |
dan_ackme | 29:b6af04b77a56 | 213 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("get network.netmask"))) |
dan_ackme | 29:b6af04b77a56 | 214 | { |
dan_ackme | 29:b6af04b77a56 | 215 | if(!NetworkInterface::strToIp(wiconnect->internalBuffer, netmask)) |
dan_ackme | 29:b6af04b77a56 | 216 | { |
dan_ackme | 29:b6af04b77a56 | 217 | result = WICONNECT_RESPONSE_PARSE_ERROR; |
dan_ackme | 29:b6af04b77a56 | 218 | } |
dan_ackme | 29:b6af04b77a56 | 219 | else |
dan_ackme | 29:b6af04b77a56 | 220 | { |
dan_ackme | 29:b6af04b77a56 | 221 | wiconnect->internalProcessingState = FS_GET_GATEWAY; |
dan_ackme | 29:b6af04b77a56 | 222 | } |
dan_ackme | 29:b6af04b77a56 | 223 | } |
dan_ackme | 29:b6af04b77a56 | 224 | } |
dan_ackme | 29:b6af04b77a56 | 225 | |
dan_ackme | 29:b6af04b77a56 | 226 | if(wiconnect->internalProcessingState == FS_GET_GATEWAY) |
dan_ackme | 29:b6af04b77a56 | 227 | { |
dan_ackme | 29:b6af04b77a56 | 228 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("get network.gateway"))) |
dan_ackme | 29:b6af04b77a56 | 229 | { |
dan_ackme | 29:b6af04b77a56 | 230 | if(!NetworkInterface::strToIp(wiconnect->internalBuffer, gateway)) |
dan_ackme | 29:b6af04b77a56 | 231 | { |
dan_ackme | 29:b6af04b77a56 | 232 | result = WICONNECT_RESPONSE_PARSE_ERROR; |
dan_ackme | 29:b6af04b77a56 | 233 | } |
dan_ackme | 29:b6af04b77a56 | 234 | } |
dan_ackme | 29:b6af04b77a56 | 235 | } |
dan_ackme | 29:b6af04b77a56 | 236 | |
dan_ackme | 29:b6af04b77a56 | 237 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 238 | |
dan_ackme | 29:b6af04b77a56 | 239 | return result; |
dan_ackme | 29:b6af04b77a56 | 240 | } |
dan_ackme | 29:b6af04b77a56 | 241 | |
dan_ackme | 29:b6af04b77a56 | 242 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 243 | WiconnectResult NetworkInterface::setDnsAddress(uint32_t dnsAddress) |
dan_ackme | 29:b6af04b77a56 | 244 | { |
dan_ackme | 29:b6af04b77a56 | 245 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 246 | |
dan_ackme | 29:b6af04b77a56 | 247 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 248 | |
dan_ackme | 29:b6af04b77a56 | 249 | result = wiconnect->sendCommand("set static.dns %d.%d.%d.%d", IPV4_ARGS(dnsAddress)); |
dan_ackme | 29:b6af04b77a56 | 250 | |
dan_ackme | 29:b6af04b77a56 | 251 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 252 | |
dan_ackme | 29:b6af04b77a56 | 253 | return result; |
dan_ackme | 29:b6af04b77a56 | 254 | } |
dan_ackme | 29:b6af04b77a56 | 255 | |
dan_ackme | 29:b6af04b77a56 | 256 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 257 | WiconnectResult NetworkInterface::getDnsAddress(uint32_t *dnsAddress) |
dan_ackme | 29:b6af04b77a56 | 258 | { |
dan_ackme | 29:b6af04b77a56 | 259 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 260 | |
dan_ackme | 29:b6af04b77a56 | 261 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 262 | |
dan_ackme | 29:b6af04b77a56 | 263 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("get static.dns"))) |
dan_ackme | 29:b6af04b77a56 | 264 | { |
dan_ackme | 29:b6af04b77a56 | 265 | if(!NetworkInterface::strToIp(wiconnect->internalBuffer, dnsAddress)) |
dan_ackme | 29:b6af04b77a56 | 266 | { |
dan_ackme | 29:b6af04b77a56 | 267 | result = WICONNECT_RESPONSE_PARSE_ERROR; |
dan_ackme | 29:b6af04b77a56 | 268 | } |
dan_ackme | 29:b6af04b77a56 | 269 | } |
dan_ackme | 29:b6af04b77a56 | 270 | |
dan_ackme | 29:b6af04b77a56 | 271 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 272 | |
dan_ackme | 29:b6af04b77a56 | 273 | return result; |
dan_ackme | 29:b6af04b77a56 | 274 | } |
dan_ackme | 29:b6af04b77a56 | 275 | |
dan_ackme | 29:b6af04b77a56 | 276 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 277 | WiconnectResult NetworkInterface::getRssi(int32_t *rssiPtr) |
dan_ackme | 29:b6af04b77a56 | 278 | { |
dan_ackme | 29:b6af04b77a56 | 279 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 280 | |
dan_ackme | 29:b6af04b77a56 | 281 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 282 | |
dan_ackme | 29:b6af04b77a56 | 283 | *rssiPtr = -999; |
dan_ackme | 29:b6af04b77a56 | 284 | |
dan_ackme | 29:b6af04b77a56 | 285 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("rssi"))) |
dan_ackme | 29:b6af04b77a56 | 286 | { |
dan_ackme | 29:b6af04b77a56 | 287 | result = wiconnect->responseToInt32(rssiPtr); |
dan_ackme | 29:b6af04b77a56 | 288 | } |
dan_ackme | 29:b6af04b77a56 | 289 | |
dan_ackme | 29:b6af04b77a56 | 290 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 291 | |
dan_ackme | 29:b6af04b77a56 | 292 | return result; |
dan_ackme | 29:b6af04b77a56 | 293 | } |
dan_ackme | 29:b6af04b77a56 | 294 | |
dan_ackme | 29:b6af04b77a56 | 295 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 296 | WiconnectResult NetworkInterface::getSignalStrength(NetworkSignalStrength *signalStrengthPtr) |
dan_ackme | 29:b6af04b77a56 | 297 | { |
dan_ackme | 29:b6af04b77a56 | 298 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 299 | int32_t rssiDbm; |
dan_ackme | 29:b6af04b77a56 | 300 | |
dan_ackme | 29:b6af04b77a56 | 301 | if(WICONNECT_SUCCEEDED(result, getRssi(&rssiDbm))) |
dan_ackme | 29:b6af04b77a56 | 302 | { |
dan_ackme | 29:b6af04b77a56 | 303 | *signalStrengthPtr = NetworkInterface::rssiToSignalStrength(rssiDbm); |
dan_ackme | 29:b6af04b77a56 | 304 | } |
dan_ackme | 29:b6af04b77a56 | 305 | else if(result == WICONNECT_CMD_RESPONSE_ERROR) |
dan_ackme | 29:b6af04b77a56 | 306 | { |
dan_ackme | 29:b6af04b77a56 | 307 | *signalStrengthPtr = NETWORK_RSSI_UNKNOWN; |
dan_ackme | 29:b6af04b77a56 | 308 | result = WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 309 | } |
dan_ackme | 29:b6af04b77a56 | 310 | |
dan_ackme | 29:b6af04b77a56 | 311 | return result; |
dan_ackme | 29:b6af04b77a56 | 312 | } |
dan_ackme | 29:b6af04b77a56 | 313 | |
dan_ackme | 29:b6af04b77a56 | 314 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 315 | WiconnectResult NetworkInterface::getMacAddress(MacAddress *macAddress) |
dan_ackme | 29:b6af04b77a56 | 316 | { |
dan_ackme | 29:b6af04b77a56 | 317 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 318 | |
dan_ackme | 29:b6af04b77a56 | 319 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 320 | |
dan_ackme | 29:b6af04b77a56 | 321 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("get wlan.mac"))) |
dan_ackme | 29:b6af04b77a56 | 322 | { |
dan_ackme | 29:b6af04b77a56 | 323 | if(!Wiconnect::strToMacAddress(wiconnect->internalBuffer, macAddress)) |
dan_ackme | 29:b6af04b77a56 | 324 | { |
dan_ackme | 29:b6af04b77a56 | 325 | result = WICONNECT_RESPONSE_PARSE_ERROR; |
dan_ackme | 29:b6af04b77a56 | 326 | } |
dan_ackme | 29:b6af04b77a56 | 327 | } |
dan_ackme | 29:b6af04b77a56 | 328 | |
dan_ackme | 29:b6af04b77a56 | 329 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 330 | |
dan_ackme | 29:b6af04b77a56 | 331 | return result; |
dan_ackme | 29:b6af04b77a56 | 332 | } |
dan_ackme | 29:b6af04b77a56 | 333 | |
dan_ackme | 29:b6af04b77a56 | 334 | |
dan_ackme | 29:b6af04b77a56 | 335 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 336 | const char* NetworkInterface::getIpAddress(char *ipStrBuffer) |
dan_ackme | 29:b6af04b77a56 | 337 | { |
dan_ackme | 29:b6af04b77a56 | 338 | SET_STR_BUFFER(ipStrBuffer, 17); |
dan_ackme | 29:b6af04b77a56 | 339 | |
dan_ackme | 29:b6af04b77a56 | 340 | if(wiconnect->nonBlocking) |
dan_ackme | 29:b6af04b77a56 | 341 | { |
dan_ackme | 29:b6af04b77a56 | 342 | return "Err"; |
dan_ackme | 29:b6af04b77a56 | 343 | } |
dan_ackme | 29:b6af04b77a56 | 344 | |
dan_ackme | 29:b6af04b77a56 | 345 | if(wiconnect->sendCommand(ptr, 17, "get network.ip") != WICONNECT_SUCCESS) |
dan_ackme | 29:b6af04b77a56 | 346 | { |
dan_ackme | 29:b6af04b77a56 | 347 | strcpy(ptr, "0.0.0.0"); |
dan_ackme | 29:b6af04b77a56 | 348 | } |
dan_ackme | 29:b6af04b77a56 | 349 | |
dan_ackme | 29:b6af04b77a56 | 350 | return ptr; |
dan_ackme | 29:b6af04b77a56 | 351 | } |
dan_ackme | 29:b6af04b77a56 | 352 | |
dan_ackme | 29:b6af04b77a56 | 353 | |
dan_ackme | 29:b6af04b77a56 | 354 | //----------------------------------------------------------------------------------------------- |
dan_ackme | 29:b6af04b77a56 | 355 | |
dan_ackme | 29:b6af04b77a56 | 356 | |
dan_ackme | 40:4b4306f3d829 | 357 | /*************************************************************************************************/ |
dan_ackme | 40:4b4306f3d829 | 358 | bool NetworkInterface::strToIp(const char *str, uint32_t *intPtr) |
dan_ackme | 40:4b4306f3d829 | 359 | { |
dan_ackme | 40:4b4306f3d829 | 360 | if (!intPtr) |
dan_ackme | 40:4b4306f3d829 | 361 | { |
dan_ackme | 40:4b4306f3d829 | 362 | return false; |
dan_ackme | 40:4b4306f3d829 | 363 | } |
dan_ackme | 40:4b4306f3d829 | 364 | uint32_t ip = 0; |
dan_ackme | 29:b6af04b77a56 | 365 | int i; |
dan_ackme | 31:59fac148a8d5 | 366 | const char *tok; |
dan_ackme | 29:b6af04b77a56 | 367 | |
dan_ackme | 29:b6af04b77a56 | 368 | for(i = 0; i < 4; ++i, str = tok) |
dan_ackme | 29:b6af04b77a56 | 369 | { |
dan_ackme | 29:b6af04b77a56 | 370 | uint8_t nibble; |
dan_ackme | 31:59fac148a8d5 | 371 | char buf[4]; |
dan_ackme | 29:b6af04b77a56 | 372 | |
dan_ackme | 29:b6af04b77a56 | 373 | tok = strchr(str, '.'); |
dan_ackme | 40:4b4306f3d829 | 374 | |
dan_ackme | 40:4b4306f3d829 | 375 | const int len = (int)(tok - str); |
dan_ackme | 29:b6af04b77a56 | 376 | if(tok == NULL) |
dan_ackme | 29:b6af04b77a56 | 377 | { |
dan_ackme | 40:4b4306f3d829 | 378 | if(i < 3 || strlen(str) > 3) |
dan_ackme | 29:b6af04b77a56 | 379 | { |
dan_ackme | 29:b6af04b77a56 | 380 | return false; |
dan_ackme | 29:b6af04b77a56 | 381 | } |
dan_ackme | 31:59fac148a8d5 | 382 | strcpy(buf, str); |
dan_ackme | 29:b6af04b77a56 | 383 | } |
dan_ackme | 40:4b4306f3d829 | 384 | else if(len > 3) |
dan_ackme | 40:4b4306f3d829 | 385 | { |
dan_ackme | 40:4b4306f3d829 | 386 | return false; |
dan_ackme | 40:4b4306f3d829 | 387 | } |
dan_ackme | 29:b6af04b77a56 | 388 | else |
dan_ackme | 29:b6af04b77a56 | 389 | { |
dan_ackme | 40:4b4306f3d829 | 390 | strncpy(buf, str, len); |
dan_ackme | 40:4b4306f3d829 | 391 | buf[len] = 0; |
dan_ackme | 31:59fac148a8d5 | 392 | ++tok; |
dan_ackme | 29:b6af04b77a56 | 393 | } |
dan_ackme | 29:b6af04b77a56 | 394 | |
dan_ackme | 31:59fac148a8d5 | 395 | if(!StringUtil::strToUint8(buf, &nibble)) |
dan_ackme | 29:b6af04b77a56 | 396 | { |
dan_ackme | 29:b6af04b77a56 | 397 | return false; |
dan_ackme | 29:b6af04b77a56 | 398 | } |
dan_ackme | 29:b6af04b77a56 | 399 | ip |= (((uint32_t)nibble) << 24); |
dan_ackme | 29:b6af04b77a56 | 400 | if(i < 3) |
dan_ackme | 29:b6af04b77a56 | 401 | { |
dan_ackme | 29:b6af04b77a56 | 402 | ip = (ip >> 8UL); |
dan_ackme | 29:b6af04b77a56 | 403 | } |
dan_ackme | 29:b6af04b77a56 | 404 | } |
dan_ackme | 29:b6af04b77a56 | 405 | |
dan_ackme | 29:b6af04b77a56 | 406 | if(i != 4) |
dan_ackme | 29:b6af04b77a56 | 407 | { |
dan_ackme | 29:b6af04b77a56 | 408 | return false; |
dan_ackme | 40:4b4306f3d829 | 409 | } |
dan_ackme | 40:4b4306f3d829 | 410 | |
dan_ackme | 40:4b4306f3d829 | 411 | *intPtr = ip; |
dan_ackme | 40:4b4306f3d829 | 412 | |
dan_ackme | 40:4b4306f3d829 | 413 | return true; |
dan_ackme | 29:b6af04b77a56 | 414 | } |
dan_ackme | 29:b6af04b77a56 | 415 | |
dan_ackme | 29:b6af04b77a56 | 416 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 417 | const char* NetworkInterface::ipToStr(uint32_t ip, char *ipStrBuffer) |
dan_ackme | 29:b6af04b77a56 | 418 | { |
dan_ackme | 29:b6af04b77a56 | 419 | SET_STR_BUFFER(ipStrBuffer, 17); |
dan_ackme | 29:b6af04b77a56 | 420 | |
dan_ackme | 29:b6af04b77a56 | 421 | sprintf(ptr, IPV4_FORMAT, IPV4_ARGS(ip)); |
dan_ackme | 29:b6af04b77a56 | 422 | return ptr; |
dan_ackme | 29:b6af04b77a56 | 423 | } |
dan_ackme | 29:b6af04b77a56 | 424 | |
dan_ackme | 29:b6af04b77a56 | 425 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 426 | const char* NetworkInterface::networkStatusToStr(NetworkStatus status) |
dan_ackme | 29:b6af04b77a56 | 427 | { |
dan_ackme | 29:b6af04b77a56 | 428 | switch(status) |
dan_ackme | 29:b6af04b77a56 | 429 | { |
dan_ackme | 29:b6af04b77a56 | 430 | case NETWORK_STATUS_DOWN: |
dan_ackme | 29:b6af04b77a56 | 431 | return "Down"; |
dan_ackme | 29:b6af04b77a56 | 432 | case NETWORK_STATUS_WIFI_ONLY: |
dan_ackme | 29:b6af04b77a56 | 433 | return "WiFi Only"; |
dan_ackme | 29:b6af04b77a56 | 434 | case NETWORK_STATUS_UP: |
dan_ackme | 29:b6af04b77a56 | 435 | return "Up"; |
dan_ackme | 29:b6af04b77a56 | 436 | default: |
dan_ackme | 29:b6af04b77a56 | 437 | return "Unknown"; |
dan_ackme | 29:b6af04b77a56 | 438 | } |
dan_ackme | 29:b6af04b77a56 | 439 | } |
dan_ackme | 29:b6af04b77a56 | 440 | |
dan_ackme | 29:b6af04b77a56 | 441 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 442 | const char* NetworkInterface::networkJoinResultToStr(NetworkJoinResult joinResult) |
dan_ackme | 29:b6af04b77a56 | 443 | { |
dan_ackme | 29:b6af04b77a56 | 444 | switch(joinResult) |
dan_ackme | 29:b6af04b77a56 | 445 | { |
dan_ackme | 29:b6af04b77a56 | 446 | case NETWORK_JOIN_RESULT_NONE: |
dan_ackme | 29:b6af04b77a56 | 447 | return "None"; |
dan_ackme | 29:b6af04b77a56 | 448 | case NETWORK_JOIN_RESULT_SUCCESS: |
dan_ackme | 29:b6af04b77a56 | 449 | return "Success"; |
dan_ackme | 29:b6af04b77a56 | 450 | case NETWORK_JOIN_RESULT_JOINING: |
dan_ackme | 29:b6af04b77a56 | 451 | return "Joining"; |
dan_ackme | 29:b6af04b77a56 | 452 | case NETWORK_JOIN_RESULT_NO_SSID: |
dan_ackme | 29:b6af04b77a56 | 453 | return "No SSID"; |
dan_ackme | 29:b6af04b77a56 | 454 | case NETWORK_JOIN_RESULT_NO_PASSWORD: |
dan_ackme | 29:b6af04b77a56 | 455 | return "No Password"; |
dan_ackme | 29:b6af04b77a56 | 456 | case NETWORK_JOIN_RESULT_BAD_SECURITY: |
dan_ackme | 29:b6af04b77a56 | 457 | return "Bad Security Setting"; |
dan_ackme | 29:b6af04b77a56 | 458 | case NETWORK_JOIN_RESULT_NOT_FOUND: |
dan_ackme | 29:b6af04b77a56 | 459 | return "Network Not Found"; |
dan_ackme | 29:b6af04b77a56 | 460 | case NETWORK_JOIN_RESULT_FAILED: |
dan_ackme | 29:b6af04b77a56 | 461 | return "Failed"; |
dan_ackme | 29:b6af04b77a56 | 462 | case NETWORK_JOIN_RESULT_ABORTED: |
dan_ackme | 29:b6af04b77a56 | 463 | return "Aborted"; |
dan_ackme | 29:b6af04b77a56 | 464 | default: |
dan_ackme | 29:b6af04b77a56 | 465 | return "Unknown"; |
dan_ackme | 29:b6af04b77a56 | 466 | } |
dan_ackme | 29:b6af04b77a56 | 467 | } |
dan_ackme | 29:b6af04b77a56 | 468 | |
dan_ackme | 29:b6af04b77a56 | 469 | |
dan_ackme | 29:b6af04b77a56 | 470 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 471 | const char* NetworkInterface::signalStrengthToStr(NetworkSignalStrength signalStrenth) |
dan_ackme | 29:b6af04b77a56 | 472 | { |
dan_ackme | 29:b6af04b77a56 | 473 | switch(signalStrenth) |
dan_ackme | 29:b6af04b77a56 | 474 | { |
dan_ackme | 29:b6af04b77a56 | 475 | case NETWORK_RSSI_EXCELLENT: |
dan_ackme | 29:b6af04b77a56 | 476 | return "Excellent"; |
dan_ackme | 29:b6af04b77a56 | 477 | case NETWORK_RSSI_VERY_GOOD: |
dan_ackme | 29:b6af04b77a56 | 478 | return "Very Good"; |
dan_ackme | 29:b6af04b77a56 | 479 | case NETWORK_RSSI_GOOD: |
dan_ackme | 29:b6af04b77a56 | 480 | return "Good"; |
dan_ackme | 29:b6af04b77a56 | 481 | case NETWORK_RSSI_POOR: |
dan_ackme | 29:b6af04b77a56 | 482 | return "Poor"; |
dan_ackme | 29:b6af04b77a56 | 483 | case NETWORK_RSSI_VERY_POOR: |
dan_ackme | 29:b6af04b77a56 | 484 | return "Very Poor"; |
dan_ackme | 29:b6af04b77a56 | 485 | case NETWORK_RSSI_UNKNOWN: |
dan_ackme | 29:b6af04b77a56 | 486 | default: |
dan_ackme | 29:b6af04b77a56 | 487 | return "Unknown"; |
dan_ackme | 29:b6af04b77a56 | 488 | } |
dan_ackme | 29:b6af04b77a56 | 489 | } |
dan_ackme | 29:b6af04b77a56 | 490 | |
dan_ackme | 29:b6af04b77a56 | 491 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 492 | NetworkSignalStrength NetworkInterface::rssiToSignalStrength(int rssi_dbm) |
dan_ackme | 29:b6af04b77a56 | 493 | { |
dan_ackme | 29:b6af04b77a56 | 494 | if(rssi_dbm > -20) |
dan_ackme | 29:b6af04b77a56 | 495 | { |
dan_ackme | 29:b6af04b77a56 | 496 | return NETWORK_RSSI_EXCELLENT; |
dan_ackme | 29:b6af04b77a56 | 497 | } |
dan_ackme | 29:b6af04b77a56 | 498 | else if(rssi_dbm > -35) |
dan_ackme | 29:b6af04b77a56 | 499 | { |
dan_ackme | 29:b6af04b77a56 | 500 | return NETWORK_RSSI_VERY_GOOD; |
dan_ackme | 29:b6af04b77a56 | 501 | } |
dan_ackme | 29:b6af04b77a56 | 502 | else if(rssi_dbm > -50) |
dan_ackme | 29:b6af04b77a56 | 503 | { |
dan_ackme | 29:b6af04b77a56 | 504 | return NETWORK_RSSI_GOOD; |
dan_ackme | 29:b6af04b77a56 | 505 | } |
dan_ackme | 29:b6af04b77a56 | 506 | else if(rssi_dbm > -70) |
dan_ackme | 29:b6af04b77a56 | 507 | { |
dan_ackme | 29:b6af04b77a56 | 508 | return NETWORK_RSSI_POOR; |
dan_ackme | 29:b6af04b77a56 | 509 | } |
dan_ackme | 29:b6af04b77a56 | 510 | else |
dan_ackme | 29:b6af04b77a56 | 511 | { |
dan_ackme | 29:b6af04b77a56 | 512 | return NETWORK_RSSI_VERY_POOR; |
dan_ackme | 29:b6af04b77a56 | 513 | } |
dan_ackme | 29:b6af04b77a56 | 514 | } |
dan_ackme | 29:b6af04b77a56 | 515 | |
dan_ackme | 29:b6af04b77a56 | 516 | |
dan_ackme | 29:b6af04b77a56 | 517 | typedef struct |
dan_ackme | 29:b6af04b77a56 | 518 | { |
dan_ackme | 29:b6af04b77a56 | 519 | const char* key; |
dan_ackme | 29:b6af04b77a56 | 520 | NetworkSecurity value; |
dan_ackme | 29:b6af04b77a56 | 521 | } NetworkSecurityTableEntry; |
dan_ackme | 29:b6af04b77a56 | 522 | |
dan_ackme | 29:b6af04b77a56 | 523 | static const NetworkSecurityTableEntry networkSecurityTable[] = { |
dan_ackme | 29:b6af04b77a56 | 524 | {"Auto", NETWORK_SECURITY_UNKNOWN}, |
dan_ackme | 29:b6af04b77a56 | 525 | {"Open", NETWORK_SECURITY_OPEN}, |
dan_ackme | 29:b6af04b77a56 | 526 | {"Unknown", NETWORK_SECURITY_UNKNOWN}, |
dan_ackme | 29:b6af04b77a56 | 527 | {"WEP", NETWORK_SECURITY_WEP_PSK}, |
dan_ackme | 29:b6af04b77a56 | 528 | {"WPA-AES", NETWORK_SECURITY_WPA_AES_PSK}, |
dan_ackme | 29:b6af04b77a56 | 529 | {"WPA-TKIP", NETWORK_SECURITY_WPA_TKIP_PSK}, |
dan_ackme | 29:b6af04b77a56 | 530 | {"WPA2-AES", NETWORK_SECURITY_WPA2_AES_PSK}, |
dan_ackme | 29:b6af04b77a56 | 531 | {"WPA2-Mixed", NETWORK_SECURITY_WPA2_MIXED_PSK}, |
dan_ackme | 29:b6af04b77a56 | 532 | {"WPA2-TKIP", NETWORK_SECURITY_WPA2_TKIP_PSK}, |
dan_ackme | 29:b6af04b77a56 | 533 | }; |
dan_ackme | 29:b6af04b77a56 | 534 | |
dan_ackme | 29:b6af04b77a56 | 535 | |
dan_ackme | 29:b6af04b77a56 | 536 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 537 | NetworkSecurity NetworkInterface::strToNetworkSecurity(const char *str) |
dan_ackme | 29:b6af04b77a56 | 538 | { |
dan_ackme | 29:b6af04b77a56 | 539 | const NetworkSecurityTableEntry *end = &networkSecurityTable[ARRAY_COUNT(networkSecurityTable)]; |
dan_ackme | 29:b6af04b77a56 | 540 | |
dan_ackme | 29:b6af04b77a56 | 541 | for(const NetworkSecurityTableEntry *e = networkSecurityTable; e < end; ++e) |
dan_ackme | 29:b6af04b77a56 | 542 | { |
dan_ackme | 29:b6af04b77a56 | 543 | if(StringUtil::strcasecmp(e->key, str) == 0) |
dan_ackme | 29:b6af04b77a56 | 544 | { |
dan_ackme | 29:b6af04b77a56 | 545 | return e->value; |
dan_ackme | 29:b6af04b77a56 | 546 | } |
dan_ackme | 29:b6af04b77a56 | 547 | } |
dan_ackme | 29:b6af04b77a56 | 548 | return NETWORK_SECURITY_UNKNOWN; |
dan_ackme | 29:b6af04b77a56 | 549 | } |
dan_ackme | 29:b6af04b77a56 | 550 | |
dan_ackme | 29:b6af04b77a56 | 551 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 552 | const char* NetworkInterface::networkSecurityToStr(NetworkSecurity security) |
dan_ackme | 29:b6af04b77a56 | 553 | { |
dan_ackme | 29:b6af04b77a56 | 554 | const NetworkSecurityTableEntry *end = &networkSecurityTable[ARRAY_COUNT(networkSecurityTable)]; |
dan_ackme | 29:b6af04b77a56 | 555 | |
dan_ackme | 29:b6af04b77a56 | 556 | for(const NetworkSecurityTableEntry *e = networkSecurityTable; e < end; ++e) |
dan_ackme | 29:b6af04b77a56 | 557 | { |
dan_ackme | 29:b6af04b77a56 | 558 | if(e->value == security) |
dan_ackme | 29:b6af04b77a56 | 559 | { |
dan_ackme | 29:b6af04b77a56 | 560 | return e->key; |
dan_ackme | 29:b6af04b77a56 | 561 | } |
dan_ackme | 29:b6af04b77a56 | 562 | } |
dan_ackme | 29:b6af04b77a56 | 563 | return "Unknown"; |
dan_ackme | 29:b6af04b77a56 | 564 | } |
dan_ackme | 29:b6af04b77a56 | 565 | |
dan_ackme | 29:b6af04b77a56 | 566 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 567 | bool NetworkInterface::strToSsid(const char *str, Ssid *ssid) |
dan_ackme | 29:b6af04b77a56 | 568 | { |
dan_ackme | 29:b6af04b77a56 | 569 | #define ESCAPE_CHARACTER_DELIMITER '\\' |
dan_ackme | 29:b6af04b77a56 | 570 | #define HEX_ESCAPE_CHARACTER 'x' |
dan_ackme | 29:b6af04b77a56 | 571 | int c; |
dan_ackme | 29:b6af04b77a56 | 572 | uint8_t *ssidPtr = ssid->val; |
dan_ackme | 29:b6af04b77a56 | 573 | uint32_t ssidLen = 0; |
dan_ackme | 29:b6af04b77a56 | 574 | |
dan_ackme | 29:b6af04b77a56 | 575 | while((c = (int)(*str++)) != 0) |
dan_ackme | 29:b6af04b77a56 | 576 | { |
dan_ackme | 29:b6af04b77a56 | 577 | if(c == ESCAPE_CHARACTER_DELIMITER) |
dan_ackme | 29:b6af04b77a56 | 578 | { |
dan_ackme | 29:b6af04b77a56 | 579 | if(*str == HEX_ESCAPE_CHARACTER) |
dan_ackme | 29:b6af04b77a56 | 580 | { |
dan_ackme | 29:b6af04b77a56 | 581 | c = StringUtil::hexToInt(str+1); |
dan_ackme | 29:b6af04b77a56 | 582 | if(c == -1) |
dan_ackme | 29:b6af04b77a56 | 583 | return false; |
dan_ackme | 29:b6af04b77a56 | 584 | str += 3; |
dan_ackme | 29:b6af04b77a56 | 585 | } |
dan_ackme | 29:b6af04b77a56 | 586 | else |
dan_ackme | 29:b6af04b77a56 | 587 | { |
dan_ackme | 29:b6af04b77a56 | 588 | return false; |
dan_ackme | 29:b6af04b77a56 | 589 | } |
dan_ackme | 29:b6af04b77a56 | 590 | } |
dan_ackme | 29:b6af04b77a56 | 591 | if(ssidLen >= sizeof(ssid->val)) |
dan_ackme | 29:b6af04b77a56 | 592 | return false; |
dan_ackme | 29:b6af04b77a56 | 593 | ++ssidLen; |
dan_ackme | 29:b6af04b77a56 | 594 | *ssidPtr++ = (uint8_t)c; |
dan_ackme | 29:b6af04b77a56 | 595 | } |
dan_ackme | 29:b6af04b77a56 | 596 | |
dan_ackme | 29:b6af04b77a56 | 597 | ssid->len = ssidLen; |
dan_ackme | 29:b6af04b77a56 | 598 | |
dan_ackme | 29:b6af04b77a56 | 599 | return true; |
dan_ackme | 29:b6af04b77a56 | 600 | } |
dan_ackme | 29:b6af04b77a56 | 601 | |
dan_ackme | 29:b6af04b77a56 | 602 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 603 | const char* NetworkInterface::ssidToStr(const Ssid *ssid, char *ssidStrBuffer) |
dan_ackme | 29:b6af04b77a56 | 604 | { |
dan_ackme | 29:b6af04b77a56 | 605 | SET_STR_BUFFER(ssidStrBuffer, sizeof(SsidStrBuffer)); |
dan_ackme | 29:b6af04b77a56 | 606 | const char *src = (const char*)ssid->val; |
dan_ackme | 29:b6af04b77a56 | 607 | int len = ssid->len; |
dan_ackme | 29:b6af04b77a56 | 608 | char *buf = ptr; |
dan_ackme | 29:b6af04b77a56 | 609 | |
dan_ackme | 29:b6af04b77a56 | 610 | while(len--) |
dan_ackme | 29:b6af04b77a56 | 611 | { |
dan_ackme | 29:b6af04b77a56 | 612 | if(*src >= 0x20 && *src <= 0x7E) |
dan_ackme | 29:b6af04b77a56 | 613 | { |
dan_ackme | 29:b6af04b77a56 | 614 | *ptr++ = *src; |
dan_ackme | 29:b6af04b77a56 | 615 | } |
dan_ackme | 29:b6af04b77a56 | 616 | else |
dan_ackme | 29:b6af04b77a56 | 617 | { |
dan_ackme | 29:b6af04b77a56 | 618 | ptr += sprintf(ptr, "\\x%02X", (*src) & 0xff); |
dan_ackme | 29:b6af04b77a56 | 619 | } |
dan_ackme | 29:b6af04b77a56 | 620 | ++src; |
dan_ackme | 29:b6af04b77a56 | 621 | } |
dan_ackme | 29:b6af04b77a56 | 622 | *ptr = 0; |
dan_ackme | 29:b6af04b77a56 | 623 | return buf; |
dan_ackme | 29:b6af04b77a56 | 624 | } |
dan_ackme | 29:b6af04b77a56 | 625 | |
dan_ackme | 29:b6af04b77a56 | 626 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 627 | bool NetworkInterface::strToMacAddress(const char *str, MacAddress *macAddress) |
dan_ackme | 29:b6af04b77a56 | 628 | { |
dan_ackme | 29:b6af04b77a56 | 629 | const char* strPtr = str; |
dan_ackme | 29:b6af04b77a56 | 630 | uint8_t *macPtr = (uint8_t*)macAddress->octet; |
dan_ackme | 29:b6af04b77a56 | 631 | |
dan_ackme | 29:b6af04b77a56 | 632 | for(int count = 0; count < 6; ++count) |
dan_ackme | 29:b6af04b77a56 | 633 | { |
dan_ackme | 29:b6af04b77a56 | 634 | if(count < 5) |
dan_ackme | 29:b6af04b77a56 | 635 | { |
dan_ackme | 29:b6af04b77a56 | 636 | const char *idx = strchr(strPtr, ':'); |
dan_ackme | 29:b6af04b77a56 | 637 | if(idx == NULL) |
dan_ackme | 29:b6af04b77a56 | 638 | { |
dan_ackme | 29:b6af04b77a56 | 639 | return false; |
dan_ackme | 29:b6af04b77a56 | 640 | } |
dan_ackme | 29:b6af04b77a56 | 641 | } |
dan_ackme | 29:b6af04b77a56 | 642 | int num = StringUtil::hexToInt(strPtr); |
dan_ackme | 29:b6af04b77a56 | 643 | if(num == -1) |
dan_ackme | 29:b6af04b77a56 | 644 | { |
dan_ackme | 29:b6af04b77a56 | 645 | return false; |
dan_ackme | 29:b6af04b77a56 | 646 | } |
dan_ackme | 29:b6af04b77a56 | 647 | *macPtr++ = (uint8_t)num; |
dan_ackme | 29:b6af04b77a56 | 648 | strPtr += 3; |
dan_ackme | 29:b6af04b77a56 | 649 | } |
dan_ackme | 29:b6af04b77a56 | 650 | |
dan_ackme | 29:b6af04b77a56 | 651 | return true; |
dan_ackme | 29:b6af04b77a56 | 652 | } |
dan_ackme | 29:b6af04b77a56 | 653 | |
dan_ackme | 29:b6af04b77a56 | 654 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 655 | const char* NetworkInterface::macAddressToStr(const MacAddress *macAddress, char *macStrBuffer) |
dan_ackme | 29:b6af04b77a56 | 656 | { |
dan_ackme | 29:b6af04b77a56 | 657 | SET_STR_BUFFER(macStrBuffer, sizeof(MacAddressStrBuffer)); |
dan_ackme | 29:b6af04b77a56 | 658 | const uint8_t *mac = macAddress->octet; |
dan_ackme | 29:b6af04b77a56 | 659 | |
dan_ackme | 29:b6af04b77a56 | 660 | sprintf(ptr, "%02X:%02X:%02X:%02X:%02X:%02X", |
dan_ackme | 29:b6af04b77a56 | 661 | (unsigned int)mac[0], |
dan_ackme | 29:b6af04b77a56 | 662 | (unsigned int)mac[1], |
dan_ackme | 29:b6af04b77a56 | 663 | (unsigned int)mac[2], |
dan_ackme | 29:b6af04b77a56 | 664 | (unsigned int)mac[3], |
dan_ackme | 29:b6af04b77a56 | 665 | (unsigned int)mac[4], |
dan_ackme | 29:b6af04b77a56 | 666 | (unsigned int)mac[5]); |
dan_ackme | 29:b6af04b77a56 | 667 | |
dan_ackme | 29:b6af04b77a56 | 668 | return ptr; |
dan_ackme | 29:b6af04b77a56 | 669 | } |