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@42:8ffb253b09e7, 2015-09-28 (annotated)
- Committer:
- aymangrais
- Date:
- Mon Sep 28 03:38:43 2015 +0000
- Revision:
- 42:8ffb253b09e7
- Parent:
- 31:59fac148a8d5
- Child:
- 40:4b4306f3d829
increase ota timeout to be 5 seconds (instead of 1.5 sec)
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 | 29:b6af04b77a56 | 357 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 358 | bool NetworkInterface::strToIp(const char *str, uint32_t *intPtr) |
dan_ackme | 29:b6af04b77a56 | 359 | { |
dan_ackme | 29:b6af04b77a56 | 360 | if (!intPtr) |
dan_ackme | 29:b6af04b77a56 | 361 | { |
dan_ackme | 29:b6af04b77a56 | 362 | return false; |
dan_ackme | 29:b6af04b77a56 | 363 | } |
dan_ackme | 29:b6af04b77a56 | 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 | 29:b6af04b77a56 | 374 | if(tok == NULL) |
dan_ackme | 29:b6af04b77a56 | 375 | { |
dan_ackme | 29:b6af04b77a56 | 376 | if(i < 3) |
dan_ackme | 29:b6af04b77a56 | 377 | { |
dan_ackme | 29:b6af04b77a56 | 378 | return false; |
dan_ackme | 29:b6af04b77a56 | 379 | } |
dan_ackme | 31:59fac148a8d5 | 380 | strcpy(buf, str); |
dan_ackme | 29:b6af04b77a56 | 381 | } |
dan_ackme | 29:b6af04b77a56 | 382 | else |
dan_ackme | 29:b6af04b77a56 | 383 | { |
dan_ackme | 31:59fac148a8d5 | 384 | strncpy(buf, str, tok - str); |
dan_ackme | 31:59fac148a8d5 | 385 | ++tok; |
dan_ackme | 29:b6af04b77a56 | 386 | } |
dan_ackme | 29:b6af04b77a56 | 387 | |
dan_ackme | 31:59fac148a8d5 | 388 | if(!StringUtil::strToUint8(buf, &nibble)) |
dan_ackme | 29:b6af04b77a56 | 389 | { |
dan_ackme | 29:b6af04b77a56 | 390 | return false; |
dan_ackme | 29:b6af04b77a56 | 391 | } |
dan_ackme | 29:b6af04b77a56 | 392 | ip |= (((uint32_t)nibble) << 24); |
dan_ackme | 29:b6af04b77a56 | 393 | if(i < 3) |
dan_ackme | 29:b6af04b77a56 | 394 | { |
dan_ackme | 29:b6af04b77a56 | 395 | ip = (ip >> 8UL); |
dan_ackme | 29:b6af04b77a56 | 396 | } |
dan_ackme | 29:b6af04b77a56 | 397 | } |
dan_ackme | 29:b6af04b77a56 | 398 | |
dan_ackme | 29:b6af04b77a56 | 399 | if(i != 4) |
dan_ackme | 29:b6af04b77a56 | 400 | { |
dan_ackme | 29:b6af04b77a56 | 401 | return false; |
dan_ackme | 29:b6af04b77a56 | 402 | } |
dan_ackme | 29:b6af04b77a56 | 403 | |
dan_ackme | 29:b6af04b77a56 | 404 | *intPtr = ip; |
dan_ackme | 29:b6af04b77a56 | 405 | |
dan_ackme | 29:b6af04b77a56 | 406 | return true; |
dan_ackme | 29:b6af04b77a56 | 407 | } |
dan_ackme | 29:b6af04b77a56 | 408 | |
dan_ackme | 29:b6af04b77a56 | 409 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 410 | const char* NetworkInterface::ipToStr(uint32_t ip, char *ipStrBuffer) |
dan_ackme | 29:b6af04b77a56 | 411 | { |
dan_ackme | 29:b6af04b77a56 | 412 | SET_STR_BUFFER(ipStrBuffer, 17); |
dan_ackme | 29:b6af04b77a56 | 413 | |
dan_ackme | 29:b6af04b77a56 | 414 | sprintf(ptr, IPV4_FORMAT, IPV4_ARGS(ip)); |
dan_ackme | 29:b6af04b77a56 | 415 | return ptr; |
dan_ackme | 29:b6af04b77a56 | 416 | } |
dan_ackme | 29:b6af04b77a56 | 417 | |
dan_ackme | 29:b6af04b77a56 | 418 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 419 | const char* NetworkInterface::networkStatusToStr(NetworkStatus status) |
dan_ackme | 29:b6af04b77a56 | 420 | { |
dan_ackme | 29:b6af04b77a56 | 421 | switch(status) |
dan_ackme | 29:b6af04b77a56 | 422 | { |
dan_ackme | 29:b6af04b77a56 | 423 | case NETWORK_STATUS_DOWN: |
dan_ackme | 29:b6af04b77a56 | 424 | return "Down"; |
dan_ackme | 29:b6af04b77a56 | 425 | case NETWORK_STATUS_WIFI_ONLY: |
dan_ackme | 29:b6af04b77a56 | 426 | return "WiFi Only"; |
dan_ackme | 29:b6af04b77a56 | 427 | case NETWORK_STATUS_UP: |
dan_ackme | 29:b6af04b77a56 | 428 | return "Up"; |
dan_ackme | 29:b6af04b77a56 | 429 | default: |
dan_ackme | 29:b6af04b77a56 | 430 | return "Unknown"; |
dan_ackme | 29:b6af04b77a56 | 431 | } |
dan_ackme | 29:b6af04b77a56 | 432 | } |
dan_ackme | 29:b6af04b77a56 | 433 | |
dan_ackme | 29:b6af04b77a56 | 434 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 435 | const char* NetworkInterface::networkJoinResultToStr(NetworkJoinResult joinResult) |
dan_ackme | 29:b6af04b77a56 | 436 | { |
dan_ackme | 29:b6af04b77a56 | 437 | switch(joinResult) |
dan_ackme | 29:b6af04b77a56 | 438 | { |
dan_ackme | 29:b6af04b77a56 | 439 | case NETWORK_JOIN_RESULT_NONE: |
dan_ackme | 29:b6af04b77a56 | 440 | return "None"; |
dan_ackme | 29:b6af04b77a56 | 441 | case NETWORK_JOIN_RESULT_SUCCESS: |
dan_ackme | 29:b6af04b77a56 | 442 | return "Success"; |
dan_ackme | 29:b6af04b77a56 | 443 | case NETWORK_JOIN_RESULT_JOINING: |
dan_ackme | 29:b6af04b77a56 | 444 | return "Joining"; |
dan_ackme | 29:b6af04b77a56 | 445 | case NETWORK_JOIN_RESULT_NO_SSID: |
dan_ackme | 29:b6af04b77a56 | 446 | return "No SSID"; |
dan_ackme | 29:b6af04b77a56 | 447 | case NETWORK_JOIN_RESULT_NO_PASSWORD: |
dan_ackme | 29:b6af04b77a56 | 448 | return "No Password"; |
dan_ackme | 29:b6af04b77a56 | 449 | case NETWORK_JOIN_RESULT_BAD_SECURITY: |
dan_ackme | 29:b6af04b77a56 | 450 | return "Bad Security Setting"; |
dan_ackme | 29:b6af04b77a56 | 451 | case NETWORK_JOIN_RESULT_NOT_FOUND: |
dan_ackme | 29:b6af04b77a56 | 452 | return "Network Not Found"; |
dan_ackme | 29:b6af04b77a56 | 453 | case NETWORK_JOIN_RESULT_FAILED: |
dan_ackme | 29:b6af04b77a56 | 454 | return "Failed"; |
dan_ackme | 29:b6af04b77a56 | 455 | case NETWORK_JOIN_RESULT_ABORTED: |
dan_ackme | 29:b6af04b77a56 | 456 | return "Aborted"; |
dan_ackme | 29:b6af04b77a56 | 457 | default: |
dan_ackme | 29:b6af04b77a56 | 458 | return "Unknown"; |
dan_ackme | 29:b6af04b77a56 | 459 | } |
dan_ackme | 29:b6af04b77a56 | 460 | } |
dan_ackme | 29:b6af04b77a56 | 461 | |
dan_ackme | 29:b6af04b77a56 | 462 | |
dan_ackme | 29:b6af04b77a56 | 463 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 464 | const char* NetworkInterface::signalStrengthToStr(NetworkSignalStrength signalStrenth) |
dan_ackme | 29:b6af04b77a56 | 465 | { |
dan_ackme | 29:b6af04b77a56 | 466 | switch(signalStrenth) |
dan_ackme | 29:b6af04b77a56 | 467 | { |
dan_ackme | 29:b6af04b77a56 | 468 | case NETWORK_RSSI_EXCELLENT: |
dan_ackme | 29:b6af04b77a56 | 469 | return "Excellent"; |
dan_ackme | 29:b6af04b77a56 | 470 | case NETWORK_RSSI_VERY_GOOD: |
dan_ackme | 29:b6af04b77a56 | 471 | return "Very Good"; |
dan_ackme | 29:b6af04b77a56 | 472 | case NETWORK_RSSI_GOOD: |
dan_ackme | 29:b6af04b77a56 | 473 | return "Good"; |
dan_ackme | 29:b6af04b77a56 | 474 | case NETWORK_RSSI_POOR: |
dan_ackme | 29:b6af04b77a56 | 475 | return "Poor"; |
dan_ackme | 29:b6af04b77a56 | 476 | case NETWORK_RSSI_VERY_POOR: |
dan_ackme | 29:b6af04b77a56 | 477 | return "Very Poor"; |
dan_ackme | 29:b6af04b77a56 | 478 | case NETWORK_RSSI_UNKNOWN: |
dan_ackme | 29:b6af04b77a56 | 479 | default: |
dan_ackme | 29:b6af04b77a56 | 480 | return "Unknown"; |
dan_ackme | 29:b6af04b77a56 | 481 | } |
dan_ackme | 29:b6af04b77a56 | 482 | } |
dan_ackme | 29:b6af04b77a56 | 483 | |
dan_ackme | 29:b6af04b77a56 | 484 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 485 | NetworkSignalStrength NetworkInterface::rssiToSignalStrength(int rssi_dbm) |
dan_ackme | 29:b6af04b77a56 | 486 | { |
dan_ackme | 29:b6af04b77a56 | 487 | if(rssi_dbm > -20) |
dan_ackme | 29:b6af04b77a56 | 488 | { |
dan_ackme | 29:b6af04b77a56 | 489 | return NETWORK_RSSI_EXCELLENT; |
dan_ackme | 29:b6af04b77a56 | 490 | } |
dan_ackme | 29:b6af04b77a56 | 491 | else if(rssi_dbm > -35) |
dan_ackme | 29:b6af04b77a56 | 492 | { |
dan_ackme | 29:b6af04b77a56 | 493 | return NETWORK_RSSI_VERY_GOOD; |
dan_ackme | 29:b6af04b77a56 | 494 | } |
dan_ackme | 29:b6af04b77a56 | 495 | else if(rssi_dbm > -50) |
dan_ackme | 29:b6af04b77a56 | 496 | { |
dan_ackme | 29:b6af04b77a56 | 497 | return NETWORK_RSSI_GOOD; |
dan_ackme | 29:b6af04b77a56 | 498 | } |
dan_ackme | 29:b6af04b77a56 | 499 | else if(rssi_dbm > -70) |
dan_ackme | 29:b6af04b77a56 | 500 | { |
dan_ackme | 29:b6af04b77a56 | 501 | return NETWORK_RSSI_POOR; |
dan_ackme | 29:b6af04b77a56 | 502 | } |
dan_ackme | 29:b6af04b77a56 | 503 | else |
dan_ackme | 29:b6af04b77a56 | 504 | { |
dan_ackme | 29:b6af04b77a56 | 505 | return NETWORK_RSSI_VERY_POOR; |
dan_ackme | 29:b6af04b77a56 | 506 | } |
dan_ackme | 29:b6af04b77a56 | 507 | } |
dan_ackme | 29:b6af04b77a56 | 508 | |
dan_ackme | 29:b6af04b77a56 | 509 | |
dan_ackme | 29:b6af04b77a56 | 510 | typedef struct |
dan_ackme | 29:b6af04b77a56 | 511 | { |
dan_ackme | 29:b6af04b77a56 | 512 | const char* key; |
dan_ackme | 29:b6af04b77a56 | 513 | NetworkSecurity value; |
dan_ackme | 29:b6af04b77a56 | 514 | } NetworkSecurityTableEntry; |
dan_ackme | 29:b6af04b77a56 | 515 | |
dan_ackme | 29:b6af04b77a56 | 516 | static const NetworkSecurityTableEntry networkSecurityTable[] = { |
dan_ackme | 29:b6af04b77a56 | 517 | {"Auto", NETWORK_SECURITY_UNKNOWN}, |
dan_ackme | 29:b6af04b77a56 | 518 | {"Open", NETWORK_SECURITY_OPEN}, |
dan_ackme | 29:b6af04b77a56 | 519 | {"Unknown", NETWORK_SECURITY_UNKNOWN}, |
dan_ackme | 29:b6af04b77a56 | 520 | {"WEP", NETWORK_SECURITY_WEP_PSK}, |
dan_ackme | 29:b6af04b77a56 | 521 | {"WPA-AES", NETWORK_SECURITY_WPA_AES_PSK}, |
dan_ackme | 29:b6af04b77a56 | 522 | {"WPA-TKIP", NETWORK_SECURITY_WPA_TKIP_PSK}, |
dan_ackme | 29:b6af04b77a56 | 523 | {"WPA2-AES", NETWORK_SECURITY_WPA2_AES_PSK}, |
dan_ackme | 29:b6af04b77a56 | 524 | {"WPA2-Mixed", NETWORK_SECURITY_WPA2_MIXED_PSK}, |
dan_ackme | 29:b6af04b77a56 | 525 | {"WPA2-TKIP", NETWORK_SECURITY_WPA2_TKIP_PSK}, |
dan_ackme | 29:b6af04b77a56 | 526 | }; |
dan_ackme | 29:b6af04b77a56 | 527 | |
dan_ackme | 29:b6af04b77a56 | 528 | |
dan_ackme | 29:b6af04b77a56 | 529 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 530 | NetworkSecurity NetworkInterface::strToNetworkSecurity(const char *str) |
dan_ackme | 29:b6af04b77a56 | 531 | { |
dan_ackme | 29:b6af04b77a56 | 532 | const NetworkSecurityTableEntry *end = &networkSecurityTable[ARRAY_COUNT(networkSecurityTable)]; |
dan_ackme | 29:b6af04b77a56 | 533 | |
dan_ackme | 29:b6af04b77a56 | 534 | for(const NetworkSecurityTableEntry *e = networkSecurityTable; e < end; ++e) |
dan_ackme | 29:b6af04b77a56 | 535 | { |
dan_ackme | 29:b6af04b77a56 | 536 | if(StringUtil::strcasecmp(e->key, str) == 0) |
dan_ackme | 29:b6af04b77a56 | 537 | { |
dan_ackme | 29:b6af04b77a56 | 538 | return e->value; |
dan_ackme | 29:b6af04b77a56 | 539 | } |
dan_ackme | 29:b6af04b77a56 | 540 | } |
dan_ackme | 29:b6af04b77a56 | 541 | return NETWORK_SECURITY_UNKNOWN; |
dan_ackme | 29:b6af04b77a56 | 542 | } |
dan_ackme | 29:b6af04b77a56 | 543 | |
dan_ackme | 29:b6af04b77a56 | 544 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 545 | const char* NetworkInterface::networkSecurityToStr(NetworkSecurity security) |
dan_ackme | 29:b6af04b77a56 | 546 | { |
dan_ackme | 29:b6af04b77a56 | 547 | const NetworkSecurityTableEntry *end = &networkSecurityTable[ARRAY_COUNT(networkSecurityTable)]; |
dan_ackme | 29:b6af04b77a56 | 548 | |
dan_ackme | 29:b6af04b77a56 | 549 | for(const NetworkSecurityTableEntry *e = networkSecurityTable; e < end; ++e) |
dan_ackme | 29:b6af04b77a56 | 550 | { |
dan_ackme | 29:b6af04b77a56 | 551 | if(e->value == security) |
dan_ackme | 29:b6af04b77a56 | 552 | { |
dan_ackme | 29:b6af04b77a56 | 553 | return e->key; |
dan_ackme | 29:b6af04b77a56 | 554 | } |
dan_ackme | 29:b6af04b77a56 | 555 | } |
dan_ackme | 29:b6af04b77a56 | 556 | return "Unknown"; |
dan_ackme | 29:b6af04b77a56 | 557 | } |
dan_ackme | 29:b6af04b77a56 | 558 | |
dan_ackme | 29:b6af04b77a56 | 559 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 560 | bool NetworkInterface::strToSsid(const char *str, Ssid *ssid) |
dan_ackme | 29:b6af04b77a56 | 561 | { |
dan_ackme | 29:b6af04b77a56 | 562 | #define ESCAPE_CHARACTER_DELIMITER '\\' |
dan_ackme | 29:b6af04b77a56 | 563 | #define HEX_ESCAPE_CHARACTER 'x' |
dan_ackme | 29:b6af04b77a56 | 564 | int c; |
dan_ackme | 29:b6af04b77a56 | 565 | uint8_t *ssidPtr = ssid->val; |
dan_ackme | 29:b6af04b77a56 | 566 | uint32_t ssidLen = 0; |
dan_ackme | 29:b6af04b77a56 | 567 | |
dan_ackme | 29:b6af04b77a56 | 568 | while((c = (int)(*str++)) != 0) |
dan_ackme | 29:b6af04b77a56 | 569 | { |
dan_ackme | 29:b6af04b77a56 | 570 | if(c == ESCAPE_CHARACTER_DELIMITER) |
dan_ackme | 29:b6af04b77a56 | 571 | { |
dan_ackme | 29:b6af04b77a56 | 572 | if(*str == HEX_ESCAPE_CHARACTER) |
dan_ackme | 29:b6af04b77a56 | 573 | { |
dan_ackme | 29:b6af04b77a56 | 574 | c = StringUtil::hexToInt(str+1); |
dan_ackme | 29:b6af04b77a56 | 575 | if(c == -1) |
dan_ackme | 29:b6af04b77a56 | 576 | return false; |
dan_ackme | 29:b6af04b77a56 | 577 | str += 3; |
dan_ackme | 29:b6af04b77a56 | 578 | } |
dan_ackme | 29:b6af04b77a56 | 579 | else |
dan_ackme | 29:b6af04b77a56 | 580 | { |
dan_ackme | 29:b6af04b77a56 | 581 | return false; |
dan_ackme | 29:b6af04b77a56 | 582 | } |
dan_ackme | 29:b6af04b77a56 | 583 | } |
dan_ackme | 29:b6af04b77a56 | 584 | if(ssidLen >= sizeof(ssid->val)) |
dan_ackme | 29:b6af04b77a56 | 585 | return false; |
dan_ackme | 29:b6af04b77a56 | 586 | ++ssidLen; |
dan_ackme | 29:b6af04b77a56 | 587 | *ssidPtr++ = (uint8_t)c; |
dan_ackme | 29:b6af04b77a56 | 588 | } |
dan_ackme | 29:b6af04b77a56 | 589 | |
dan_ackme | 29:b6af04b77a56 | 590 | ssid->len = ssidLen; |
dan_ackme | 29:b6af04b77a56 | 591 | |
dan_ackme | 29:b6af04b77a56 | 592 | return true; |
dan_ackme | 29:b6af04b77a56 | 593 | } |
dan_ackme | 29:b6af04b77a56 | 594 | |
dan_ackme | 29:b6af04b77a56 | 595 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 596 | const char* NetworkInterface::ssidToStr(const Ssid *ssid, char *ssidStrBuffer) |
dan_ackme | 29:b6af04b77a56 | 597 | { |
dan_ackme | 29:b6af04b77a56 | 598 | SET_STR_BUFFER(ssidStrBuffer, sizeof(SsidStrBuffer)); |
dan_ackme | 29:b6af04b77a56 | 599 | const char *src = (const char*)ssid->val; |
dan_ackme | 29:b6af04b77a56 | 600 | int len = ssid->len; |
dan_ackme | 29:b6af04b77a56 | 601 | char *buf = ptr; |
dan_ackme | 29:b6af04b77a56 | 602 | |
dan_ackme | 29:b6af04b77a56 | 603 | while(len--) |
dan_ackme | 29:b6af04b77a56 | 604 | { |
dan_ackme | 29:b6af04b77a56 | 605 | if(*src >= 0x20 && *src <= 0x7E) |
dan_ackme | 29:b6af04b77a56 | 606 | { |
dan_ackme | 29:b6af04b77a56 | 607 | *ptr++ = *src; |
dan_ackme | 29:b6af04b77a56 | 608 | } |
dan_ackme | 29:b6af04b77a56 | 609 | else |
dan_ackme | 29:b6af04b77a56 | 610 | { |
dan_ackme | 29:b6af04b77a56 | 611 | ptr += sprintf(ptr, "\\x%02X", (*src) & 0xff); |
dan_ackme | 29:b6af04b77a56 | 612 | } |
dan_ackme | 29:b6af04b77a56 | 613 | ++src; |
dan_ackme | 29:b6af04b77a56 | 614 | } |
dan_ackme | 29:b6af04b77a56 | 615 | *ptr = 0; |
dan_ackme | 29:b6af04b77a56 | 616 | return buf; |
dan_ackme | 29:b6af04b77a56 | 617 | } |
dan_ackme | 29:b6af04b77a56 | 618 | |
dan_ackme | 29:b6af04b77a56 | 619 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 620 | bool NetworkInterface::strToMacAddress(const char *str, MacAddress *macAddress) |
dan_ackme | 29:b6af04b77a56 | 621 | { |
dan_ackme | 29:b6af04b77a56 | 622 | const char* strPtr = str; |
dan_ackme | 29:b6af04b77a56 | 623 | uint8_t *macPtr = (uint8_t*)macAddress->octet; |
dan_ackme | 29:b6af04b77a56 | 624 | |
dan_ackme | 29:b6af04b77a56 | 625 | for(int count = 0; count < 6; ++count) |
dan_ackme | 29:b6af04b77a56 | 626 | { |
dan_ackme | 29:b6af04b77a56 | 627 | if(count < 5) |
dan_ackme | 29:b6af04b77a56 | 628 | { |
dan_ackme | 29:b6af04b77a56 | 629 | const char *idx = strchr(strPtr, ':'); |
dan_ackme | 29:b6af04b77a56 | 630 | if(idx == NULL) |
dan_ackme | 29:b6af04b77a56 | 631 | { |
dan_ackme | 29:b6af04b77a56 | 632 | return false; |
dan_ackme | 29:b6af04b77a56 | 633 | } |
dan_ackme | 29:b6af04b77a56 | 634 | } |
dan_ackme | 29:b6af04b77a56 | 635 | int num = StringUtil::hexToInt(strPtr); |
dan_ackme | 29:b6af04b77a56 | 636 | if(num == -1) |
dan_ackme | 29:b6af04b77a56 | 637 | { |
dan_ackme | 29:b6af04b77a56 | 638 | return false; |
dan_ackme | 29:b6af04b77a56 | 639 | } |
dan_ackme | 29:b6af04b77a56 | 640 | *macPtr++ = (uint8_t)num; |
dan_ackme | 29:b6af04b77a56 | 641 | strPtr += 3; |
dan_ackme | 29:b6af04b77a56 | 642 | } |
dan_ackme | 29:b6af04b77a56 | 643 | |
dan_ackme | 29:b6af04b77a56 | 644 | return true; |
dan_ackme | 29:b6af04b77a56 | 645 | } |
dan_ackme | 29:b6af04b77a56 | 646 | |
dan_ackme | 29:b6af04b77a56 | 647 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 648 | const char* NetworkInterface::macAddressToStr(const MacAddress *macAddress, char *macStrBuffer) |
dan_ackme | 29:b6af04b77a56 | 649 | { |
dan_ackme | 29:b6af04b77a56 | 650 | SET_STR_BUFFER(macStrBuffer, sizeof(MacAddressStrBuffer)); |
dan_ackme | 29:b6af04b77a56 | 651 | const uint8_t *mac = macAddress->octet; |
dan_ackme | 29:b6af04b77a56 | 652 | |
dan_ackme | 29:b6af04b77a56 | 653 | sprintf(ptr, "%02X:%02X:%02X:%02X:%02X:%02X", |
dan_ackme | 29:b6af04b77a56 | 654 | (unsigned int)mac[0], |
dan_ackme | 29:b6af04b77a56 | 655 | (unsigned int)mac[1], |
dan_ackme | 29:b6af04b77a56 | 656 | (unsigned int)mac[2], |
dan_ackme | 29:b6af04b77a56 | 657 | (unsigned int)mac[3], |
dan_ackme | 29:b6af04b77a56 | 658 | (unsigned int)mac[4], |
dan_ackme | 29:b6af04b77a56 | 659 | (unsigned int)mac[5]); |
dan_ackme | 29:b6af04b77a56 | 660 | |
dan_ackme | 29:b6af04b77a56 | 661 | return ptr; |
dan_ackme | 29:b6af04b77a56 | 662 | } |