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

Committer:
dan_ackme
Date:
Wed Aug 13 04:41:04 2014 -0700
Revision:
16:7f1d6d359787
Parent:
6:8a87a59d0d21
Child:
17:7268f365676b
updated documentation and copyright

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dan_ackme 16:7f1d6d359787 1 /**
dan_ackme 16:7f1d6d359787 2 * ACKme WiConnect Host Library is licensed under the BSD licence:
dan_ackme 16:7f1d6d359787 3 *
dan_ackme 16:7f1d6d359787 4 * Copyright (c)2014 ACKme Networks.
dan_ackme 16:7f1d6d359787 5 * All rights reserved.
dan_ackme 16:7f1d6d359787 6 *
dan_ackme 16:7f1d6d359787 7 * Redistribution and use in source and binary forms, with or without modification,
dan_ackme 16:7f1d6d359787 8 * are permitted provided that the following conditions are met:
dan_ackme 16:7f1d6d359787 9 *
dan_ackme 16:7f1d6d359787 10 * 1. Redistributions of source code must retain the above copyright notice,
dan_ackme 16:7f1d6d359787 11 * this list of conditions and the following disclaimer.
dan_ackme 16:7f1d6d359787 12 * 2. Redistributions in binary form must reproduce the above copyright notice,
dan_ackme 16:7f1d6d359787 13 * this list of conditions and the following disclaimer in the documentation
dan_ackme 16:7f1d6d359787 14 * and/or other materials provided with the distribution.
dan_ackme 16:7f1d6d359787 15 * 3. The name of the author may not be used to endorse or promote products
dan_ackme 16:7f1d6d359787 16 * derived from this software without specific prior written permission.
dan_ackme 16:7f1d6d359787 17 *
dan_ackme 16:7f1d6d359787 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED
dan_ackme 16:7f1d6d359787 19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
dan_ackme 16:7f1d6d359787 20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
dan_ackme 16:7f1d6d359787 21 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
dan_ackme 16:7f1d6d359787 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
dan_ackme 16:7f1d6d359787 23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dan_ackme 16:7f1d6d359787 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dan_ackme 16:7f1d6d359787 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
dan_ackme 16:7f1d6d359787 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
dan_ackme 16:7f1d6d359787 27 * OF SUCH DAMAGE.
dan_ackme 0:ea85c4bb5e1f 28 */
dan_ackme 0:ea85c4bb5e1f 29
dan_ackme 0:ea85c4bb5e1f 30 #include "Wiconnect.h"
dan_ackme 0:ea85c4bb5e1f 31 #include "internal/common.h"
dan_ackme 0:ea85c4bb5e1f 32 #include "StringUtil.h"
dan_ackme 0:ea85c4bb5e1f 33
dan_ackme 0:ea85c4bb5e1f 34
dan_ackme 0:ea85c4bb5e1f 35 #define IPV4_FORMAT "%d.%d.%d.%d"
dan_ackme 0:ea85c4bb5e1f 36 #define IPV4_ARGS(ip) \
dan_ackme 0:ea85c4bb5e1f 37 (int)( (ip) & 0xff), \
dan_ackme 0:ea85c4bb5e1f 38 (int)(((ip) >> 8) & 0xff), \
dan_ackme 0:ea85c4bb5e1f 39 (int)(((ip) >> 16) & 0xff), \
dan_ackme 0:ea85c4bb5e1f 40 (int)(((ip) >> 24) & 0xff)
dan_ackme 0:ea85c4bb5e1f 41
dan_ackme 0:ea85c4bb5e1f 42
dan_ackme 0:ea85c4bb5e1f 43
dan_ackme 0:ea85c4bb5e1f 44 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 45 NetworkInterface::NetworkInterface(Wiconnect *wiconnect_)
dan_ackme 0:ea85c4bb5e1f 46 {
dan_ackme 0:ea85c4bb5e1f 47 wiconnect = wiconnect_;
dan_ackme 0:ea85c4bb5e1f 48 }
dan_ackme 0:ea85c4bb5e1f 49
dan_ackme 0:ea85c4bb5e1f 50 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 51 WiconnectResult NetworkInterface::ping(const char *domain, uint32_t *timeMsPtr)
dan_ackme 0:ea85c4bb5e1f 52 {
dan_ackme 0:ea85c4bb5e1f 53 WiconnectResult result;
dan_ackme 0:ea85c4bb5e1f 54
dan_ackme 0:ea85c4bb5e1f 55 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 0:ea85c4bb5e1f 56
dan_ackme 0:ea85c4bb5e1f 57 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(completeHandler, "ping %s", (domain == NULL) ? "-g" : domain)) &&
dan_ackme 0:ea85c4bb5e1f 58 timeMsPtr != NULL)
dan_ackme 0:ea85c4bb5e1f 59 {
dan_ackme 0:ea85c4bb5e1f 60 if(sscanf(wiconnect->internalBuffer, "Ping reply in %ums", timeMsPtr) != 1)
dan_ackme 0:ea85c4bb5e1f 61 {
dan_ackme 0:ea85c4bb5e1f 62 result = WICONNECT_RESPONSE_PARSE_ERROR;
dan_ackme 0:ea85c4bb5e1f 63 }
dan_ackme 0:ea85c4bb5e1f 64 }
dan_ackme 0:ea85c4bb5e1f 65
dan_ackme 0:ea85c4bb5e1f 66 CHECK_CLEANUP_COMMAND();
dan_ackme 0:ea85c4bb5e1f 67
dan_ackme 0:ea85c4bb5e1f 68 return result;
dan_ackme 0:ea85c4bb5e1f 69 }
dan_ackme 0:ea85c4bb5e1f 70
dan_ackme 0:ea85c4bb5e1f 71 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 72 WiconnectResult NetworkInterface::lookup(const char *domain, uint32_t *ipAddressPtr)
dan_ackme 0:ea85c4bb5e1f 73 {
dan_ackme 0:ea85c4bb5e1f 74 WiconnectResult result;
dan_ackme 0:ea85c4bb5e1f 75
dan_ackme 0:ea85c4bb5e1f 76 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 0:ea85c4bb5e1f 77
dan_ackme 0:ea85c4bb5e1f 78 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("nlo %s", domain)))
dan_ackme 0:ea85c4bb5e1f 79 {
dan_ackme 0:ea85c4bb5e1f 80 if(!NetworkInterface::strToIp(wiconnect->internalBuffer, ipAddressPtr))
dan_ackme 0:ea85c4bb5e1f 81 {
dan_ackme 0:ea85c4bb5e1f 82 result = WICONNECT_RESPONSE_PARSE_ERROR;
dan_ackme 0:ea85c4bb5e1f 83 }
dan_ackme 0:ea85c4bb5e1f 84 }
dan_ackme 0:ea85c4bb5e1f 85
dan_ackme 0:ea85c4bb5e1f 86 CHECK_CLEANUP_COMMAND();
dan_ackme 0:ea85c4bb5e1f 87
dan_ackme 0:ea85c4bb5e1f 88 return result;
dan_ackme 0:ea85c4bb5e1f 89 }
dan_ackme 0:ea85c4bb5e1f 90
dan_ackme 0:ea85c4bb5e1f 91 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 92 WiconnectResult NetworkInterface::setDhcpEnabled(bool enabled)
dan_ackme 0:ea85c4bb5e1f 93 {
dan_ackme 0:ea85c4bb5e1f 94 WiconnectResult result;
dan_ackme 0:ea85c4bb5e1f 95
dan_ackme 0:ea85c4bb5e1f 96 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 0:ea85c4bb5e1f 97
dan_ackme 0:ea85c4bb5e1f 98 result = wiconnect->sendCommand(CMD_SET_NETWORK_DHCP, enabled);
dan_ackme 0:ea85c4bb5e1f 99
dan_ackme 0:ea85c4bb5e1f 100 CHECK_CLEANUP_COMMAND();
dan_ackme 0:ea85c4bb5e1f 101
dan_ackme 0:ea85c4bb5e1f 102 return result;
dan_ackme 0:ea85c4bb5e1f 103 }
dan_ackme 0:ea85c4bb5e1f 104
dan_ackme 0:ea85c4bb5e1f 105 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 106 WiconnectResult NetworkInterface::getDhcpEnabled(bool *enabledPtr)
dan_ackme 0:ea85c4bb5e1f 107 {
dan_ackme 0:ea85c4bb5e1f 108 WiconnectResult result;
dan_ackme 0:ea85c4bb5e1f 109
dan_ackme 0:ea85c4bb5e1f 110 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 0:ea85c4bb5e1f 111
dan_ackme 0:ea85c4bb5e1f 112 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("get network.dhcp")))
dan_ackme 0:ea85c4bb5e1f 113 {
dan_ackme 0:ea85c4bb5e1f 114 int32_t enabled;
dan_ackme 0:ea85c4bb5e1f 115 if(WICONNECT_SUCCEEDED(result, wiconnect->responseToInt32(&enabled)))
dan_ackme 0:ea85c4bb5e1f 116 {
dan_ackme 0:ea85c4bb5e1f 117 *enabledPtr = (bool)enabled;
dan_ackme 0:ea85c4bb5e1f 118 }
dan_ackme 0:ea85c4bb5e1f 119 }
dan_ackme 0:ea85c4bb5e1f 120
dan_ackme 0:ea85c4bb5e1f 121 CHECK_CLEANUP_COMMAND();
dan_ackme 0:ea85c4bb5e1f 122
dan_ackme 0:ea85c4bb5e1f 123 return result;
dan_ackme 0:ea85c4bb5e1f 124 }
dan_ackme 0:ea85c4bb5e1f 125
dan_ackme 0:ea85c4bb5e1f 126 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 127 WiconnectResult NetworkInterface::setIpSettings(uint32_t ip, uint32_t netmask, uint32_t gateway)
dan_ackme 0:ea85c4bb5e1f 128 {
dan_ackme 2:05e20e184e7e 129 WiconnectResult result = WICONNECT_ERROR;
dan_ackme 0:ea85c4bb5e1f 130
dan_ackme 0:ea85c4bb5e1f 131 enum
dan_ackme 0:ea85c4bb5e1f 132 {
dan_ackme 0:ea85c4bb5e1f 133 FS_SET_IP,
dan_ackme 0:ea85c4bb5e1f 134 FS_SET_NETMASK,
dan_ackme 0:ea85c4bb5e1f 135 FS_SET_GATEWAY
dan_ackme 0:ea85c4bb5e1f 136 };
dan_ackme 0:ea85c4bb5e1f 137
dan_ackme 0:ea85c4bb5e1f 138 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 0:ea85c4bb5e1f 139
dan_ackme 0:ea85c4bb5e1f 140 if(wiconnect->internalProcessingState == FS_SET_IP)
dan_ackme 0:ea85c4bb5e1f 141 {
dan_ackme 0:ea85c4bb5e1f 142 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(CMD_SET_STATIC_IP, IPV4_ARGS(ip))))
dan_ackme 0:ea85c4bb5e1f 143 {
dan_ackme 0:ea85c4bb5e1f 144 wiconnect->internalProcessingState = FS_SET_NETMASK;
dan_ackme 0:ea85c4bb5e1f 145 }
dan_ackme 0:ea85c4bb5e1f 146 }
dan_ackme 0:ea85c4bb5e1f 147
dan_ackme 0:ea85c4bb5e1f 148 if(wiconnect->internalProcessingState == FS_SET_NETMASK)
dan_ackme 0:ea85c4bb5e1f 149 {
dan_ackme 0:ea85c4bb5e1f 150 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(CMD_SET_STATIC_NETMASK, IPV4_ARGS(netmask))))
dan_ackme 0:ea85c4bb5e1f 151 {
dan_ackme 0:ea85c4bb5e1f 152 wiconnect->internalProcessingState = FS_SET_GATEWAY;
dan_ackme 0:ea85c4bb5e1f 153 }
dan_ackme 0:ea85c4bb5e1f 154 }
dan_ackme 0:ea85c4bb5e1f 155
dan_ackme 0:ea85c4bb5e1f 156 if(wiconnect->internalProcessingState == FS_SET_GATEWAY)
dan_ackme 0:ea85c4bb5e1f 157 {
dan_ackme 0:ea85c4bb5e1f 158 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(CMD_SET_STATIC_GATEWAY, IPV4_ARGS(gateway))))
dan_ackme 0:ea85c4bb5e1f 159 {
dan_ackme 0:ea85c4bb5e1f 160 }
dan_ackme 0:ea85c4bb5e1f 161 }
dan_ackme 0:ea85c4bb5e1f 162
dan_ackme 0:ea85c4bb5e1f 163 CHECK_CLEANUP_COMMAND();
dan_ackme 0:ea85c4bb5e1f 164
dan_ackme 0:ea85c4bb5e1f 165 return result;
dan_ackme 0:ea85c4bb5e1f 166 }
dan_ackme 0:ea85c4bb5e1f 167
dan_ackme 0:ea85c4bb5e1f 168 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 169 WiconnectResult NetworkInterface::setIpSettings(const char* ipStr, const char* netmaskStr, const char* gatewayStr)
dan_ackme 0:ea85c4bb5e1f 170 {
dan_ackme 0:ea85c4bb5e1f 171 uint32_t ip, nm, gw;
dan_ackme 0:ea85c4bb5e1f 172
dan_ackme 0:ea85c4bb5e1f 173 if( !NetworkInterface::strToIp(ipStr, &ip) ||
dan_ackme 0:ea85c4bb5e1f 174 !NetworkInterface::strToIp(netmaskStr, &nm) ||
dan_ackme 0:ea85c4bb5e1f 175 !NetworkInterface::strToIp(gatewayStr, &gw))
dan_ackme 0:ea85c4bb5e1f 176 {
dan_ackme 0:ea85c4bb5e1f 177 return WICONNECT_ERROR;
dan_ackme 0:ea85c4bb5e1f 178 }
dan_ackme 0:ea85c4bb5e1f 179 return setIpSettings(ip, nm, gw);
dan_ackme 0:ea85c4bb5e1f 180 }
dan_ackme 0:ea85c4bb5e1f 181
dan_ackme 0:ea85c4bb5e1f 182 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 183 WiconnectResult NetworkInterface::getIpSettings(uint32_t *ip, uint32_t *netmask, uint32_t *gateway)
dan_ackme 0:ea85c4bb5e1f 184 {
dan_ackme 2:05e20e184e7e 185 WiconnectResult result = WICONNECT_ERROR;
dan_ackme 0:ea85c4bb5e1f 186
dan_ackme 0:ea85c4bb5e1f 187 enum
dan_ackme 0:ea85c4bb5e1f 188 {
dan_ackme 0:ea85c4bb5e1f 189 FS_GET_IP,
dan_ackme 0:ea85c4bb5e1f 190 FS_GET_NETMASK,
dan_ackme 0:ea85c4bb5e1f 191 FS_GET_GATEWAY
dan_ackme 0:ea85c4bb5e1f 192 };
dan_ackme 0:ea85c4bb5e1f 193
dan_ackme 0:ea85c4bb5e1f 194 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 0:ea85c4bb5e1f 195
dan_ackme 0:ea85c4bb5e1f 196 if(wiconnect->internalProcessingState == FS_GET_IP)
dan_ackme 0:ea85c4bb5e1f 197 {
dan_ackme 0:ea85c4bb5e1f 198 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("get network.ip")))
dan_ackme 0:ea85c4bb5e1f 199 {
dan_ackme 0:ea85c4bb5e1f 200 if(!NetworkInterface::strToIp(wiconnect->internalBuffer, ip))
dan_ackme 0:ea85c4bb5e1f 201 {
dan_ackme 0:ea85c4bb5e1f 202 result = WICONNECT_RESPONSE_PARSE_ERROR;
dan_ackme 6:8a87a59d0d21 203 }
dan_ackme 6:8a87a59d0d21 204 else
dan_ackme 6:8a87a59d0d21 205 {
dan_ackme 0:ea85c4bb5e1f 206 wiconnect->internalProcessingState = FS_GET_NETMASK;
dan_ackme 0:ea85c4bb5e1f 207 }
dan_ackme 0:ea85c4bb5e1f 208 }
dan_ackme 0:ea85c4bb5e1f 209 }
dan_ackme 0:ea85c4bb5e1f 210
dan_ackme 0:ea85c4bb5e1f 211 if(wiconnect->internalProcessingState == FS_GET_NETMASK)
dan_ackme 0:ea85c4bb5e1f 212 {
dan_ackme 0:ea85c4bb5e1f 213 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("get network.netmask")))
dan_ackme 0:ea85c4bb5e1f 214 {
dan_ackme 0:ea85c4bb5e1f 215 if(!NetworkInterface::strToIp(wiconnect->internalBuffer, netmask))
dan_ackme 0:ea85c4bb5e1f 216 {
dan_ackme 0:ea85c4bb5e1f 217 result = WICONNECT_RESPONSE_PARSE_ERROR;
dan_ackme 6:8a87a59d0d21 218 }
dan_ackme 6:8a87a59d0d21 219 else
dan_ackme 6:8a87a59d0d21 220 {
dan_ackme 0:ea85c4bb5e1f 221 wiconnect->internalProcessingState = FS_GET_GATEWAY;
dan_ackme 0:ea85c4bb5e1f 222 }
dan_ackme 0:ea85c4bb5e1f 223 }
dan_ackme 0:ea85c4bb5e1f 224 }
dan_ackme 0:ea85c4bb5e1f 225
dan_ackme 0:ea85c4bb5e1f 226 if(wiconnect->internalProcessingState == FS_GET_GATEWAY)
dan_ackme 0:ea85c4bb5e1f 227 {
dan_ackme 0:ea85c4bb5e1f 228 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("get network.gateway")))
dan_ackme 0:ea85c4bb5e1f 229 {
dan_ackme 0:ea85c4bb5e1f 230 if(!NetworkInterface::strToIp(wiconnect->internalBuffer, gateway))
dan_ackme 0:ea85c4bb5e1f 231 {
dan_ackme 0:ea85c4bb5e1f 232 result = WICONNECT_RESPONSE_PARSE_ERROR;
dan_ackme 0:ea85c4bb5e1f 233 }
dan_ackme 0:ea85c4bb5e1f 234 }
dan_ackme 0:ea85c4bb5e1f 235 }
dan_ackme 0:ea85c4bb5e1f 236
dan_ackme 0:ea85c4bb5e1f 237 CHECK_CLEANUP_COMMAND();
dan_ackme 0:ea85c4bb5e1f 238
dan_ackme 0:ea85c4bb5e1f 239 return result;
dan_ackme 0:ea85c4bb5e1f 240 }
dan_ackme 0:ea85c4bb5e1f 241 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 242 WiconnectResult NetworkInterface::getSignalStrength(NetworkSignalStrength *signalStrengthPtr)
dan_ackme 0:ea85c4bb5e1f 243 {
dan_ackme 0:ea85c4bb5e1f 244 WiconnectResult result;
dan_ackme 0:ea85c4bb5e1f 245 int32_t rssi_dbm;
dan_ackme 0:ea85c4bb5e1f 246
dan_ackme 0:ea85c4bb5e1f 247 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 0:ea85c4bb5e1f 248
dan_ackme 0:ea85c4bb5e1f 249 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("rssi")))
dan_ackme 0:ea85c4bb5e1f 250 {
dan_ackme 0:ea85c4bb5e1f 251 if(!WICONNECT_FAILED(result, wiconnect->responseToInt32(&rssi_dbm)))
dan_ackme 0:ea85c4bb5e1f 252 {
dan_ackme 0:ea85c4bb5e1f 253 *signalStrengthPtr = NetworkInterface::rssiToSignalStrength(rssi_dbm);
dan_ackme 0:ea85c4bb5e1f 254 }
dan_ackme 0:ea85c4bb5e1f 255 }
dan_ackme 0:ea85c4bb5e1f 256
dan_ackme 0:ea85c4bb5e1f 257 if(result == WICONNECT_CMD_RESPONSE_ERROR)
dan_ackme 0:ea85c4bb5e1f 258 {
dan_ackme 0:ea85c4bb5e1f 259 *signalStrengthPtr = NETWORK_RSSI_UNKNOWN;
dan_ackme 0:ea85c4bb5e1f 260 result = WICONNECT_SUCCESS;
dan_ackme 0:ea85c4bb5e1f 261 }
dan_ackme 0:ea85c4bb5e1f 262
dan_ackme 0:ea85c4bb5e1f 263 CHECK_CLEANUP_COMMAND();
dan_ackme 0:ea85c4bb5e1f 264
dan_ackme 0:ea85c4bb5e1f 265 return result;
dan_ackme 0:ea85c4bb5e1f 266 }
dan_ackme 0:ea85c4bb5e1f 267
dan_ackme 0:ea85c4bb5e1f 268
dan_ackme 0:ea85c4bb5e1f 269 //-----------------------------------------------------------------------------------------------
dan_ackme 0:ea85c4bb5e1f 270
dan_ackme 0:ea85c4bb5e1f 271
dan_ackme 0:ea85c4bb5e1f 272 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 273 bool NetworkInterface::strToIp(const char *str, uint32_t *intPtr)
dan_ackme 0:ea85c4bb5e1f 274 {
dan_ackme 0:ea85c4bb5e1f 275 if (!intPtr)
dan_ackme 0:ea85c4bb5e1f 276 {
dan_ackme 0:ea85c4bb5e1f 277 return false;
dan_ackme 0:ea85c4bb5e1f 278 }
dan_ackme 0:ea85c4bb5e1f 279 int temp[4];
dan_ackme 0:ea85c4bb5e1f 280
dan_ackme 0:ea85c4bb5e1f 281 if(sscanf(str, "%d.%d.%d.%d", &temp[0], &temp[1], &temp[2], &temp[3] ) != 4)
dan_ackme 0:ea85c4bb5e1f 282 {
dan_ackme 0:ea85c4bb5e1f 283 return false;
dan_ackme 0:ea85c4bb5e1f 284 }
dan_ackme 0:ea85c4bb5e1f 285 else if(temp[0] > 255 || temp[1] > 255 || temp[2] > 255 || temp[3] > 255)
dan_ackme 0:ea85c4bb5e1f 286 {
dan_ackme 0:ea85c4bb5e1f 287 return false;
dan_ackme 0:ea85c4bb5e1f 288 }
dan_ackme 0:ea85c4bb5e1f 289 *intPtr = (uint32_t)temp[3] << 24 | temp[2] << 16 | temp[1] << 8 | temp[0];
dan_ackme 0:ea85c4bb5e1f 290
dan_ackme 0:ea85c4bb5e1f 291 return true;
dan_ackme 0:ea85c4bb5e1f 292 }
dan_ackme 0:ea85c4bb5e1f 293
dan_ackme 0:ea85c4bb5e1f 294 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 295 const char* NetworkInterface::ipToStr(uint32_t ip, char *ipStrBuffer)
dan_ackme 0:ea85c4bb5e1f 296 {
dan_ackme 0:ea85c4bb5e1f 297 SET_STR_BUFFER(ipStrBuffer, 16);
dan_ackme 0:ea85c4bb5e1f 298 sprintf(ptr, IPV4_FORMAT, IPV4_ARGS(ip));
dan_ackme 0:ea85c4bb5e1f 299 return ptr;
dan_ackme 0:ea85c4bb5e1f 300 }
dan_ackme 0:ea85c4bb5e1f 301
dan_ackme 0:ea85c4bb5e1f 302 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 303 const char* NetworkInterface::networkStatusToStr(NetworkStatus status)
dan_ackme 0:ea85c4bb5e1f 304 {
dan_ackme 0:ea85c4bb5e1f 305 switch(status)
dan_ackme 0:ea85c4bb5e1f 306 {
dan_ackme 0:ea85c4bb5e1f 307 case NETWORK_STATUS_DOWN:
dan_ackme 0:ea85c4bb5e1f 308 return "Down";
dan_ackme 0:ea85c4bb5e1f 309 case NETWORK_STATUS_WIFI_ONLY:
dan_ackme 0:ea85c4bb5e1f 310 return "WiFi Only";
dan_ackme 0:ea85c4bb5e1f 311 case NETWORK_STATUS_UP:
dan_ackme 0:ea85c4bb5e1f 312 return "Up";
dan_ackme 0:ea85c4bb5e1f 313 default:
dan_ackme 0:ea85c4bb5e1f 314 return "Unknown";
dan_ackme 0:ea85c4bb5e1f 315 }
dan_ackme 0:ea85c4bb5e1f 316 }
dan_ackme 0:ea85c4bb5e1f 317
dan_ackme 0:ea85c4bb5e1f 318 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 319 const char* NetworkInterface::signalStrengthToStr(NetworkSignalStrength signalStrenth)
dan_ackme 0:ea85c4bb5e1f 320 {
dan_ackme 0:ea85c4bb5e1f 321 switch(signalStrenth)
dan_ackme 0:ea85c4bb5e1f 322 {
dan_ackme 0:ea85c4bb5e1f 323 case NETWORK_RSSI_EXCELLENT:
dan_ackme 0:ea85c4bb5e1f 324 return "Excellent";
dan_ackme 0:ea85c4bb5e1f 325 case NETWORK_RSSI_VERY_GOOD:
dan_ackme 0:ea85c4bb5e1f 326 return "Very Good";
dan_ackme 0:ea85c4bb5e1f 327 case NETWORK_RSSI_GOOD:
dan_ackme 0:ea85c4bb5e1f 328 return "Good";
dan_ackme 0:ea85c4bb5e1f 329 case NETWORK_RSSI_POOR:
dan_ackme 0:ea85c4bb5e1f 330 return "Poor";
dan_ackme 0:ea85c4bb5e1f 331 case NETWORK_RSSI_VERY_POOR:
dan_ackme 0:ea85c4bb5e1f 332 return "Very Poor";
dan_ackme 0:ea85c4bb5e1f 333 case NETWORK_RSSI_UNKNOWN:
dan_ackme 0:ea85c4bb5e1f 334 default:
dan_ackme 0:ea85c4bb5e1f 335 return "Unknown";
dan_ackme 0:ea85c4bb5e1f 336 }
dan_ackme 0:ea85c4bb5e1f 337 }
dan_ackme 0:ea85c4bb5e1f 338
dan_ackme 0:ea85c4bb5e1f 339 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 340 NetworkSignalStrength NetworkInterface::rssiToSignalStrength(int rssi_dbm)
dan_ackme 0:ea85c4bb5e1f 341 {
dan_ackme 0:ea85c4bb5e1f 342 if(rssi_dbm > -20)
dan_ackme 0:ea85c4bb5e1f 343 {
dan_ackme 0:ea85c4bb5e1f 344 return NETWORK_RSSI_EXCELLENT;
dan_ackme 0:ea85c4bb5e1f 345 }
dan_ackme 0:ea85c4bb5e1f 346 else if(rssi_dbm > -35)
dan_ackme 0:ea85c4bb5e1f 347 {
dan_ackme 0:ea85c4bb5e1f 348 return NETWORK_RSSI_VERY_GOOD;
dan_ackme 0:ea85c4bb5e1f 349 }
dan_ackme 0:ea85c4bb5e1f 350 else if(rssi_dbm > -50)
dan_ackme 0:ea85c4bb5e1f 351 {
dan_ackme 0:ea85c4bb5e1f 352 return NETWORK_RSSI_GOOD;
dan_ackme 0:ea85c4bb5e1f 353 }
dan_ackme 0:ea85c4bb5e1f 354 else if(rssi_dbm > -70)
dan_ackme 0:ea85c4bb5e1f 355 {
dan_ackme 0:ea85c4bb5e1f 356 return NETWORK_RSSI_POOR;
dan_ackme 0:ea85c4bb5e1f 357 }
dan_ackme 0:ea85c4bb5e1f 358 else
dan_ackme 0:ea85c4bb5e1f 359 {
dan_ackme 0:ea85c4bb5e1f 360 return NETWORK_RSSI_VERY_POOR;
dan_ackme 0:ea85c4bb5e1f 361 }
dan_ackme 0:ea85c4bb5e1f 362 }
dan_ackme 0:ea85c4bb5e1f 363
dan_ackme 0:ea85c4bb5e1f 364
dan_ackme 0:ea85c4bb5e1f 365 typedef struct
dan_ackme 0:ea85c4bb5e1f 366 {
dan_ackme 0:ea85c4bb5e1f 367 const char* key;
dan_ackme 0:ea85c4bb5e1f 368 NetworkSecurity value;
dan_ackme 0:ea85c4bb5e1f 369 } NetworkSecurityTableEntry;
dan_ackme 0:ea85c4bb5e1f 370
dan_ackme 0:ea85c4bb5e1f 371 static const NetworkSecurityTableEntry networkSecurityTable[] = {
dan_ackme 0:ea85c4bb5e1f 372 {"Auto", NETWORK_SECURITY_UNKNOWN},
dan_ackme 0:ea85c4bb5e1f 373 {"Open", NETWORK_SECURITY_OPEN},
dan_ackme 0:ea85c4bb5e1f 374 {"Unknown", NETWORK_SECURITY_UNKNOWN},
dan_ackme 0:ea85c4bb5e1f 375 {"WEP", NETWORK_SECURITY_WEP_PSK},
dan_ackme 0:ea85c4bb5e1f 376 {"WPA-AES", NETWORK_SECURITY_WPA_AES_PSK},
dan_ackme 0:ea85c4bb5e1f 377 {"WPA-TKIP", NETWORK_SECURITY_WPA_TKIP_PSK},
dan_ackme 0:ea85c4bb5e1f 378 {"WPA2-AES", NETWORK_SECURITY_WPA2_AES_PSK},
dan_ackme 0:ea85c4bb5e1f 379 {"WPA2-Mixed", NETWORK_SECURITY_WPA2_MIXED_PSK},
dan_ackme 0:ea85c4bb5e1f 380 {"WPA2-TKIP", NETWORK_SECURITY_WPA2_TKIP_PSK},
dan_ackme 0:ea85c4bb5e1f 381 };
dan_ackme 0:ea85c4bb5e1f 382
dan_ackme 0:ea85c4bb5e1f 383
dan_ackme 0:ea85c4bb5e1f 384 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 385 NetworkSecurity NetworkInterface::strToNetworkSecurity(const char *str)
dan_ackme 0:ea85c4bb5e1f 386 {
dan_ackme 0:ea85c4bb5e1f 387 const NetworkSecurityTableEntry *end = &networkSecurityTable[ARRAY_COUNT(networkSecurityTable)];
dan_ackme 0:ea85c4bb5e1f 388
dan_ackme 0:ea85c4bb5e1f 389 for(const NetworkSecurityTableEntry *e = networkSecurityTable; e < end; ++e)
dan_ackme 0:ea85c4bb5e1f 390 {
dan_ackme 0:ea85c4bb5e1f 391 if(StringUtil::strcasecmp(e->key, str) == 0)
dan_ackme 0:ea85c4bb5e1f 392 {
dan_ackme 0:ea85c4bb5e1f 393 return e->value;
dan_ackme 0:ea85c4bb5e1f 394 }
dan_ackme 0:ea85c4bb5e1f 395 }
dan_ackme 0:ea85c4bb5e1f 396 return NETWORK_SECURITY_UNKNOWN;
dan_ackme 0:ea85c4bb5e1f 397 }
dan_ackme 0:ea85c4bb5e1f 398
dan_ackme 0:ea85c4bb5e1f 399 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 400 const char* NetworkInterface::networkSecurityToStr(NetworkSecurity security)
dan_ackme 0:ea85c4bb5e1f 401 {
dan_ackme 0:ea85c4bb5e1f 402 const NetworkSecurityTableEntry *end = &networkSecurityTable[ARRAY_COUNT(networkSecurityTable)];
dan_ackme 0:ea85c4bb5e1f 403
dan_ackme 0:ea85c4bb5e1f 404 for(const NetworkSecurityTableEntry *e = networkSecurityTable; e < end; ++e)
dan_ackme 0:ea85c4bb5e1f 405 {
dan_ackme 0:ea85c4bb5e1f 406 if(e->value == security)
dan_ackme 0:ea85c4bb5e1f 407 {
dan_ackme 0:ea85c4bb5e1f 408 return e->key;
dan_ackme 0:ea85c4bb5e1f 409 }
dan_ackme 0:ea85c4bb5e1f 410 }
dan_ackme 0:ea85c4bb5e1f 411 return "Unknown";
dan_ackme 0:ea85c4bb5e1f 412 }
dan_ackme 0:ea85c4bb5e1f 413
dan_ackme 0:ea85c4bb5e1f 414 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 415 bool NetworkInterface::strToSsid(const char *str, Ssid *ssid)
dan_ackme 0:ea85c4bb5e1f 416 {
dan_ackme 0:ea85c4bb5e1f 417 #define ESCAPE_CHARACTER_DELIMITER '\\'
dan_ackme 0:ea85c4bb5e1f 418 #define HEX_ESCAPE_CHARACTER 'x'
dan_ackme 0:ea85c4bb5e1f 419 int c;
dan_ackme 0:ea85c4bb5e1f 420 uint8_t *ssidPtr = ssid->val;
dan_ackme 0:ea85c4bb5e1f 421 int ssidLen = 0;
dan_ackme 0:ea85c4bb5e1f 422
dan_ackme 0:ea85c4bb5e1f 423 while((c = (int)(*str++)) != 0)
dan_ackme 0:ea85c4bb5e1f 424 {
dan_ackme 0:ea85c4bb5e1f 425 if(c == ESCAPE_CHARACTER_DELIMITER)
dan_ackme 0:ea85c4bb5e1f 426 {
dan_ackme 0:ea85c4bb5e1f 427 if(*str == HEX_ESCAPE_CHARACTER)
dan_ackme 0:ea85c4bb5e1f 428 {
dan_ackme 0:ea85c4bb5e1f 429 c = StringUtil::hexToInt(str+1);
dan_ackme 0:ea85c4bb5e1f 430 if(c == -1)
dan_ackme 0:ea85c4bb5e1f 431 return false;
dan_ackme 0:ea85c4bb5e1f 432 str += 3;
dan_ackme 0:ea85c4bb5e1f 433 }
dan_ackme 0:ea85c4bb5e1f 434 else
dan_ackme 0:ea85c4bb5e1f 435 {
dan_ackme 0:ea85c4bb5e1f 436 return false;
dan_ackme 0:ea85c4bb5e1f 437 }
dan_ackme 0:ea85c4bb5e1f 438 }
dan_ackme 0:ea85c4bb5e1f 439 if(ssidLen >= sizeof(ssid->val))
dan_ackme 0:ea85c4bb5e1f 440 return false;
dan_ackme 0:ea85c4bb5e1f 441 ++ssidLen;
dan_ackme 0:ea85c4bb5e1f 442 *ssidPtr++ = (uint8_t)c;
dan_ackme 0:ea85c4bb5e1f 443 }
dan_ackme 0:ea85c4bb5e1f 444
dan_ackme 0:ea85c4bb5e1f 445 ssid->len = ssidLen;
dan_ackme 0:ea85c4bb5e1f 446
dan_ackme 0:ea85c4bb5e1f 447 return true;
dan_ackme 0:ea85c4bb5e1f 448 }
dan_ackme 0:ea85c4bb5e1f 449
dan_ackme 0:ea85c4bb5e1f 450 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 451 const char* NetworkInterface::ssidToStr(const Ssid *ssid, char *ssidStrBuffer)
dan_ackme 0:ea85c4bb5e1f 452 {
dan_ackme 0:ea85c4bb5e1f 453 SET_STR_BUFFER(ssidStrBuffer, sizeof(SsidStrBuffer));
dan_ackme 0:ea85c4bb5e1f 454 const char *src = (const char*)ssid->val;
dan_ackme 0:ea85c4bb5e1f 455 int len = ssid->len;
dan_ackme 3:2dc2592bae5e 456 char *buf = ptr;
dan_ackme 0:ea85c4bb5e1f 457
dan_ackme 0:ea85c4bb5e1f 458 while(len--)
dan_ackme 0:ea85c4bb5e1f 459 {
dan_ackme 0:ea85c4bb5e1f 460 if(*src >= 0x20 && *src <= 0x7E)
dan_ackme 0:ea85c4bb5e1f 461 {
dan_ackme 0:ea85c4bb5e1f 462 *ptr++ = *src;
dan_ackme 0:ea85c4bb5e1f 463 }
dan_ackme 0:ea85c4bb5e1f 464 else
dan_ackme 0:ea85c4bb5e1f 465 {
dan_ackme 0:ea85c4bb5e1f 466 ptr += sprintf(ptr, "\\x%02X", (*src) & 0xff);
dan_ackme 0:ea85c4bb5e1f 467 }
dan_ackme 0:ea85c4bb5e1f 468 ++src;
dan_ackme 0:ea85c4bb5e1f 469 }
dan_ackme 0:ea85c4bb5e1f 470 *ptr = 0;
dan_ackme 0:ea85c4bb5e1f 471 return buf;
dan_ackme 0:ea85c4bb5e1f 472 }
dan_ackme 0:ea85c4bb5e1f 473
dan_ackme 0:ea85c4bb5e1f 474 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 475 bool NetworkInterface::strToMacAddress(const char *str, MacAddress *macAddress)
dan_ackme 0:ea85c4bb5e1f 476 {
dan_ackme 0:ea85c4bb5e1f 477 const char* strPtr = str;
dan_ackme 0:ea85c4bb5e1f 478 uint8_t *macPtr = (uint8_t*)macAddress->octet;
dan_ackme 0:ea85c4bb5e1f 479
dan_ackme 0:ea85c4bb5e1f 480 for(int count = 0; count < 6; ++count)
dan_ackme 0:ea85c4bb5e1f 481 {
dan_ackme 0:ea85c4bb5e1f 482 if(count < 5)
dan_ackme 0:ea85c4bb5e1f 483 {
dan_ackme 3:2dc2592bae5e 484 const char *idx = strchr(strPtr, ':');
dan_ackme 0:ea85c4bb5e1f 485 if(idx == NULL)
dan_ackme 0:ea85c4bb5e1f 486 {
dan_ackme 0:ea85c4bb5e1f 487 return false;
dan_ackme 0:ea85c4bb5e1f 488 }
dan_ackme 0:ea85c4bb5e1f 489 }
dan_ackme 0:ea85c4bb5e1f 490 int num = StringUtil::hexToInt(strPtr);
dan_ackme 0:ea85c4bb5e1f 491 if(num == -1)
dan_ackme 0:ea85c4bb5e1f 492 {
dan_ackme 0:ea85c4bb5e1f 493 return false;
dan_ackme 0:ea85c4bb5e1f 494 }
dan_ackme 0:ea85c4bb5e1f 495 *macPtr++ = (uint8_t)num;
dan_ackme 0:ea85c4bb5e1f 496 strPtr += 3;
dan_ackme 0:ea85c4bb5e1f 497 }
dan_ackme 0:ea85c4bb5e1f 498
dan_ackme 0:ea85c4bb5e1f 499 return true;
dan_ackme 0:ea85c4bb5e1f 500 }
dan_ackme 0:ea85c4bb5e1f 501
dan_ackme 0:ea85c4bb5e1f 502 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 503 const char* NetworkInterface::macAddressToStr(const MacAddress *macAddress, char *macStrBuffer)
dan_ackme 0:ea85c4bb5e1f 504 {
dan_ackme 0:ea85c4bb5e1f 505 SET_STR_BUFFER(macStrBuffer, sizeof(MacAddressStrBuffer));
dan_ackme 0:ea85c4bb5e1f 506 const uint8_t *mac = macAddress->octet;
dan_ackme 0:ea85c4bb5e1f 507
dan_ackme 0:ea85c4bb5e1f 508 sprintf(ptr, "%02X:%02X:%02X:%02X:%02X:%02X",
dan_ackme 0:ea85c4bb5e1f 509 (unsigned int)mac[0],
dan_ackme 0:ea85c4bb5e1f 510 (unsigned int)mac[1],
dan_ackme 0:ea85c4bb5e1f 511 (unsigned int)mac[2],
dan_ackme 0:ea85c4bb5e1f 512 (unsigned int)mac[3],
dan_ackme 0:ea85c4bb5e1f 513 (unsigned int)mac[4],
dan_ackme 0:ea85c4bb5e1f 514 (unsigned int)mac[5]);
dan_ackme 0:ea85c4bb5e1f 515
dan_ackme 0:ea85c4bb5e1f 516 return ptr;
dan_ackme 0:ea85c4bb5e1f 517 }