ACKme / WiConnect

Dependents:   wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more

Committer:
dan_ackme
Date:
Thu Oct 23 15:16:06 2014 -0700
Revision:
26:8067e3d463d3
Parent:
17:7268f365676b
Child:
27:b63f5a9cdefa
Added 'updateFirmware' and 'networkGetJoinResult' api methods
Various bug fixes
Updated documentation

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 26:8067e3d463d3 112 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("get network.dhcp.enabled")))
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 17:7268f365676b 267
dan_ackme 17:7268f365676b 268 /*************************************************************************************************/
dan_ackme 17:7268f365676b 269 const char* NetworkInterface::getIpAddress(char *ipStrBuffer)
dan_ackme 17:7268f365676b 270 {
dan_ackme 17:7268f365676b 271 SET_STR_BUFFER(ipStrBuffer, 17);
dan_ackme 17:7268f365676b 272
dan_ackme 17:7268f365676b 273 if(wiconnect->nonBlocking)
dan_ackme 17:7268f365676b 274 {
dan_ackme 17:7268f365676b 275 return "Err";
dan_ackme 17:7268f365676b 276 }
dan_ackme 17:7268f365676b 277
dan_ackme 17:7268f365676b 278 if(wiconnect->sendCommand(ptr, 17, "get network.ip") != WICONNECT_SUCCESS)
dan_ackme 17:7268f365676b 279 {
dan_ackme 17:7268f365676b 280 strcpy(ptr, "0.0.0.0");
dan_ackme 17:7268f365676b 281 }
dan_ackme 17:7268f365676b 282
dan_ackme 17:7268f365676b 283 return ptr;
dan_ackme 17:7268f365676b 284 }
dan_ackme 0:ea85c4bb5e1f 285
dan_ackme 0:ea85c4bb5e1f 286
dan_ackme 0:ea85c4bb5e1f 287 //-----------------------------------------------------------------------------------------------
dan_ackme 0:ea85c4bb5e1f 288
dan_ackme 0:ea85c4bb5e1f 289
dan_ackme 0:ea85c4bb5e1f 290 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 291 bool NetworkInterface::strToIp(const char *str, uint32_t *intPtr)
dan_ackme 0:ea85c4bb5e1f 292 {
dan_ackme 0:ea85c4bb5e1f 293 if (!intPtr)
dan_ackme 0:ea85c4bb5e1f 294 {
dan_ackme 0:ea85c4bb5e1f 295 return false;
dan_ackme 0:ea85c4bb5e1f 296 }
dan_ackme 0:ea85c4bb5e1f 297 int temp[4];
dan_ackme 0:ea85c4bb5e1f 298
dan_ackme 0:ea85c4bb5e1f 299 if(sscanf(str, "%d.%d.%d.%d", &temp[0], &temp[1], &temp[2], &temp[3] ) != 4)
dan_ackme 0:ea85c4bb5e1f 300 {
dan_ackme 0:ea85c4bb5e1f 301 return false;
dan_ackme 0:ea85c4bb5e1f 302 }
dan_ackme 0:ea85c4bb5e1f 303 else if(temp[0] > 255 || temp[1] > 255 || temp[2] > 255 || temp[3] > 255)
dan_ackme 0:ea85c4bb5e1f 304 {
dan_ackme 0:ea85c4bb5e1f 305 return false;
dan_ackme 0:ea85c4bb5e1f 306 }
dan_ackme 0:ea85c4bb5e1f 307 *intPtr = (uint32_t)temp[3] << 24 | temp[2] << 16 | temp[1] << 8 | temp[0];
dan_ackme 0:ea85c4bb5e1f 308
dan_ackme 0:ea85c4bb5e1f 309 return true;
dan_ackme 0:ea85c4bb5e1f 310 }
dan_ackme 0:ea85c4bb5e1f 311
dan_ackme 0:ea85c4bb5e1f 312 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 313 const char* NetworkInterface::ipToStr(uint32_t ip, char *ipStrBuffer)
dan_ackme 0:ea85c4bb5e1f 314 {
dan_ackme 17:7268f365676b 315 SET_STR_BUFFER(ipStrBuffer, 17);
dan_ackme 0:ea85c4bb5e1f 316 sprintf(ptr, IPV4_FORMAT, IPV4_ARGS(ip));
dan_ackme 0:ea85c4bb5e1f 317 return ptr;
dan_ackme 0:ea85c4bb5e1f 318 }
dan_ackme 0:ea85c4bb5e1f 319
dan_ackme 0:ea85c4bb5e1f 320 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 321 const char* NetworkInterface::networkStatusToStr(NetworkStatus status)
dan_ackme 0:ea85c4bb5e1f 322 {
dan_ackme 0:ea85c4bb5e1f 323 switch(status)
dan_ackme 0:ea85c4bb5e1f 324 {
dan_ackme 0:ea85c4bb5e1f 325 case NETWORK_STATUS_DOWN:
dan_ackme 0:ea85c4bb5e1f 326 return "Down";
dan_ackme 0:ea85c4bb5e1f 327 case NETWORK_STATUS_WIFI_ONLY:
dan_ackme 0:ea85c4bb5e1f 328 return "WiFi Only";
dan_ackme 0:ea85c4bb5e1f 329 case NETWORK_STATUS_UP:
dan_ackme 0:ea85c4bb5e1f 330 return "Up";
dan_ackme 0:ea85c4bb5e1f 331 default:
dan_ackme 0:ea85c4bb5e1f 332 return "Unknown";
dan_ackme 0:ea85c4bb5e1f 333 }
dan_ackme 26:8067e3d463d3 334 }
dan_ackme 26:8067e3d463d3 335
dan_ackme 26:8067e3d463d3 336 /*************************************************************************************************/
dan_ackme 26:8067e3d463d3 337 const char* NetworkInterface::networkJoinResultToStr(NetworkJoinResult joinResult)
dan_ackme 26:8067e3d463d3 338 {
dan_ackme 26:8067e3d463d3 339 switch(joinResult)
dan_ackme 26:8067e3d463d3 340 {
dan_ackme 26:8067e3d463d3 341 case NETWORK_JOIN_RESULT_NONE:
dan_ackme 26:8067e3d463d3 342 return "None";
dan_ackme 26:8067e3d463d3 343 case NETWORK_JOIN_RESULT_SUCCESS:
dan_ackme 26:8067e3d463d3 344 return "Success";
dan_ackme 26:8067e3d463d3 345 case NETWORK_JOIN_RESULT_JOINING:
dan_ackme 26:8067e3d463d3 346 return "Joining";
dan_ackme 26:8067e3d463d3 347 case NETWORK_JOIN_RESULT_NO_SSID:
dan_ackme 26:8067e3d463d3 348 return "No SSID";
dan_ackme 26:8067e3d463d3 349 case NETWORK_JOIN_RESULT_NO_PASSWORD:
dan_ackme 26:8067e3d463d3 350 return "No Password";
dan_ackme 26:8067e3d463d3 351 case NETWORK_JOIN_RESULT_BAD_SECURITY:
dan_ackme 26:8067e3d463d3 352 return "Bad Security Setting";
dan_ackme 26:8067e3d463d3 353 case NETWORK_JOIN_RESULT_NOT_FOUND:
dan_ackme 26:8067e3d463d3 354 return "Network Not Found";
dan_ackme 26:8067e3d463d3 355 case NETWORK_JOIN_RESULT_FAILED:
dan_ackme 26:8067e3d463d3 356 return "Failed";
dan_ackme 26:8067e3d463d3 357 case NETWORK_JOIN_RESULT_ABORTED:
dan_ackme 26:8067e3d463d3 358 return "Aborted";
dan_ackme 26:8067e3d463d3 359 default:
dan_ackme 26:8067e3d463d3 360 return "Unknown";
dan_ackme 26:8067e3d463d3 361 }
dan_ackme 26:8067e3d463d3 362 }
dan_ackme 26:8067e3d463d3 363
dan_ackme 0:ea85c4bb5e1f 364
dan_ackme 0:ea85c4bb5e1f 365 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 366 const char* NetworkInterface::signalStrengthToStr(NetworkSignalStrength signalStrenth)
dan_ackme 0:ea85c4bb5e1f 367 {
dan_ackme 0:ea85c4bb5e1f 368 switch(signalStrenth)
dan_ackme 0:ea85c4bb5e1f 369 {
dan_ackme 0:ea85c4bb5e1f 370 case NETWORK_RSSI_EXCELLENT:
dan_ackme 0:ea85c4bb5e1f 371 return "Excellent";
dan_ackme 0:ea85c4bb5e1f 372 case NETWORK_RSSI_VERY_GOOD:
dan_ackme 0:ea85c4bb5e1f 373 return "Very Good";
dan_ackme 0:ea85c4bb5e1f 374 case NETWORK_RSSI_GOOD:
dan_ackme 0:ea85c4bb5e1f 375 return "Good";
dan_ackme 0:ea85c4bb5e1f 376 case NETWORK_RSSI_POOR:
dan_ackme 0:ea85c4bb5e1f 377 return "Poor";
dan_ackme 0:ea85c4bb5e1f 378 case NETWORK_RSSI_VERY_POOR:
dan_ackme 0:ea85c4bb5e1f 379 return "Very Poor";
dan_ackme 0:ea85c4bb5e1f 380 case NETWORK_RSSI_UNKNOWN:
dan_ackme 0:ea85c4bb5e1f 381 default:
dan_ackme 0:ea85c4bb5e1f 382 return "Unknown";
dan_ackme 0:ea85c4bb5e1f 383 }
dan_ackme 0:ea85c4bb5e1f 384 }
dan_ackme 0:ea85c4bb5e1f 385
dan_ackme 0:ea85c4bb5e1f 386 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 387 NetworkSignalStrength NetworkInterface::rssiToSignalStrength(int rssi_dbm)
dan_ackme 0:ea85c4bb5e1f 388 {
dan_ackme 0:ea85c4bb5e1f 389 if(rssi_dbm > -20)
dan_ackme 0:ea85c4bb5e1f 390 {
dan_ackme 0:ea85c4bb5e1f 391 return NETWORK_RSSI_EXCELLENT;
dan_ackme 0:ea85c4bb5e1f 392 }
dan_ackme 0:ea85c4bb5e1f 393 else if(rssi_dbm > -35)
dan_ackme 0:ea85c4bb5e1f 394 {
dan_ackme 0:ea85c4bb5e1f 395 return NETWORK_RSSI_VERY_GOOD;
dan_ackme 0:ea85c4bb5e1f 396 }
dan_ackme 0:ea85c4bb5e1f 397 else if(rssi_dbm > -50)
dan_ackme 0:ea85c4bb5e1f 398 {
dan_ackme 0:ea85c4bb5e1f 399 return NETWORK_RSSI_GOOD;
dan_ackme 0:ea85c4bb5e1f 400 }
dan_ackme 0:ea85c4bb5e1f 401 else if(rssi_dbm > -70)
dan_ackme 0:ea85c4bb5e1f 402 {
dan_ackme 0:ea85c4bb5e1f 403 return NETWORK_RSSI_POOR;
dan_ackme 0:ea85c4bb5e1f 404 }
dan_ackme 0:ea85c4bb5e1f 405 else
dan_ackme 0:ea85c4bb5e1f 406 {
dan_ackme 0:ea85c4bb5e1f 407 return NETWORK_RSSI_VERY_POOR;
dan_ackme 0:ea85c4bb5e1f 408 }
dan_ackme 0:ea85c4bb5e1f 409 }
dan_ackme 0:ea85c4bb5e1f 410
dan_ackme 0:ea85c4bb5e1f 411
dan_ackme 0:ea85c4bb5e1f 412 typedef struct
dan_ackme 0:ea85c4bb5e1f 413 {
dan_ackme 0:ea85c4bb5e1f 414 const char* key;
dan_ackme 0:ea85c4bb5e1f 415 NetworkSecurity value;
dan_ackme 0:ea85c4bb5e1f 416 } NetworkSecurityTableEntry;
dan_ackme 0:ea85c4bb5e1f 417
dan_ackme 0:ea85c4bb5e1f 418 static const NetworkSecurityTableEntry networkSecurityTable[] = {
dan_ackme 0:ea85c4bb5e1f 419 {"Auto", NETWORK_SECURITY_UNKNOWN},
dan_ackme 0:ea85c4bb5e1f 420 {"Open", NETWORK_SECURITY_OPEN},
dan_ackme 0:ea85c4bb5e1f 421 {"Unknown", NETWORK_SECURITY_UNKNOWN},
dan_ackme 0:ea85c4bb5e1f 422 {"WEP", NETWORK_SECURITY_WEP_PSK},
dan_ackme 0:ea85c4bb5e1f 423 {"WPA-AES", NETWORK_SECURITY_WPA_AES_PSK},
dan_ackme 0:ea85c4bb5e1f 424 {"WPA-TKIP", NETWORK_SECURITY_WPA_TKIP_PSK},
dan_ackme 0:ea85c4bb5e1f 425 {"WPA2-AES", NETWORK_SECURITY_WPA2_AES_PSK},
dan_ackme 0:ea85c4bb5e1f 426 {"WPA2-Mixed", NETWORK_SECURITY_WPA2_MIXED_PSK},
dan_ackme 0:ea85c4bb5e1f 427 {"WPA2-TKIP", NETWORK_SECURITY_WPA2_TKIP_PSK},
dan_ackme 0:ea85c4bb5e1f 428 };
dan_ackme 0:ea85c4bb5e1f 429
dan_ackme 0:ea85c4bb5e1f 430
dan_ackme 0:ea85c4bb5e1f 431 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 432 NetworkSecurity NetworkInterface::strToNetworkSecurity(const char *str)
dan_ackme 0:ea85c4bb5e1f 433 {
dan_ackme 0:ea85c4bb5e1f 434 const NetworkSecurityTableEntry *end = &networkSecurityTable[ARRAY_COUNT(networkSecurityTable)];
dan_ackme 0:ea85c4bb5e1f 435
dan_ackme 0:ea85c4bb5e1f 436 for(const NetworkSecurityTableEntry *e = networkSecurityTable; e < end; ++e)
dan_ackme 0:ea85c4bb5e1f 437 {
dan_ackme 0:ea85c4bb5e1f 438 if(StringUtil::strcasecmp(e->key, str) == 0)
dan_ackme 0:ea85c4bb5e1f 439 {
dan_ackme 0:ea85c4bb5e1f 440 return e->value;
dan_ackme 0:ea85c4bb5e1f 441 }
dan_ackme 0:ea85c4bb5e1f 442 }
dan_ackme 0:ea85c4bb5e1f 443 return NETWORK_SECURITY_UNKNOWN;
dan_ackme 0:ea85c4bb5e1f 444 }
dan_ackme 0:ea85c4bb5e1f 445
dan_ackme 0:ea85c4bb5e1f 446 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 447 const char* NetworkInterface::networkSecurityToStr(NetworkSecurity security)
dan_ackme 0:ea85c4bb5e1f 448 {
dan_ackme 0:ea85c4bb5e1f 449 const NetworkSecurityTableEntry *end = &networkSecurityTable[ARRAY_COUNT(networkSecurityTable)];
dan_ackme 0:ea85c4bb5e1f 450
dan_ackme 0:ea85c4bb5e1f 451 for(const NetworkSecurityTableEntry *e = networkSecurityTable; e < end; ++e)
dan_ackme 0:ea85c4bb5e1f 452 {
dan_ackme 0:ea85c4bb5e1f 453 if(e->value == security)
dan_ackme 0:ea85c4bb5e1f 454 {
dan_ackme 0:ea85c4bb5e1f 455 return e->key;
dan_ackme 0:ea85c4bb5e1f 456 }
dan_ackme 0:ea85c4bb5e1f 457 }
dan_ackme 0:ea85c4bb5e1f 458 return "Unknown";
dan_ackme 0:ea85c4bb5e1f 459 }
dan_ackme 0:ea85c4bb5e1f 460
dan_ackme 0:ea85c4bb5e1f 461 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 462 bool NetworkInterface::strToSsid(const char *str, Ssid *ssid)
dan_ackme 0:ea85c4bb5e1f 463 {
dan_ackme 0:ea85c4bb5e1f 464 #define ESCAPE_CHARACTER_DELIMITER '\\'
dan_ackme 0:ea85c4bb5e1f 465 #define HEX_ESCAPE_CHARACTER 'x'
dan_ackme 0:ea85c4bb5e1f 466 int c;
dan_ackme 0:ea85c4bb5e1f 467 uint8_t *ssidPtr = ssid->val;
dan_ackme 0:ea85c4bb5e1f 468 int ssidLen = 0;
dan_ackme 0:ea85c4bb5e1f 469
dan_ackme 0:ea85c4bb5e1f 470 while((c = (int)(*str++)) != 0)
dan_ackme 0:ea85c4bb5e1f 471 {
dan_ackme 0:ea85c4bb5e1f 472 if(c == ESCAPE_CHARACTER_DELIMITER)
dan_ackme 0:ea85c4bb5e1f 473 {
dan_ackme 0:ea85c4bb5e1f 474 if(*str == HEX_ESCAPE_CHARACTER)
dan_ackme 0:ea85c4bb5e1f 475 {
dan_ackme 0:ea85c4bb5e1f 476 c = StringUtil::hexToInt(str+1);
dan_ackme 0:ea85c4bb5e1f 477 if(c == -1)
dan_ackme 0:ea85c4bb5e1f 478 return false;
dan_ackme 0:ea85c4bb5e1f 479 str += 3;
dan_ackme 0:ea85c4bb5e1f 480 }
dan_ackme 0:ea85c4bb5e1f 481 else
dan_ackme 0:ea85c4bb5e1f 482 {
dan_ackme 0:ea85c4bb5e1f 483 return false;
dan_ackme 0:ea85c4bb5e1f 484 }
dan_ackme 0:ea85c4bb5e1f 485 }
dan_ackme 0:ea85c4bb5e1f 486 if(ssidLen >= sizeof(ssid->val))
dan_ackme 0:ea85c4bb5e1f 487 return false;
dan_ackme 0:ea85c4bb5e1f 488 ++ssidLen;
dan_ackme 0:ea85c4bb5e1f 489 *ssidPtr++ = (uint8_t)c;
dan_ackme 0:ea85c4bb5e1f 490 }
dan_ackme 0:ea85c4bb5e1f 491
dan_ackme 0:ea85c4bb5e1f 492 ssid->len = ssidLen;
dan_ackme 0:ea85c4bb5e1f 493
dan_ackme 0:ea85c4bb5e1f 494 return true;
dan_ackme 0:ea85c4bb5e1f 495 }
dan_ackme 0:ea85c4bb5e1f 496
dan_ackme 0:ea85c4bb5e1f 497 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 498 const char* NetworkInterface::ssidToStr(const Ssid *ssid, char *ssidStrBuffer)
dan_ackme 0:ea85c4bb5e1f 499 {
dan_ackme 0:ea85c4bb5e1f 500 SET_STR_BUFFER(ssidStrBuffer, sizeof(SsidStrBuffer));
dan_ackme 0:ea85c4bb5e1f 501 const char *src = (const char*)ssid->val;
dan_ackme 0:ea85c4bb5e1f 502 int len = ssid->len;
dan_ackme 3:2dc2592bae5e 503 char *buf = ptr;
dan_ackme 0:ea85c4bb5e1f 504
dan_ackme 0:ea85c4bb5e1f 505 while(len--)
dan_ackme 0:ea85c4bb5e1f 506 {
dan_ackme 0:ea85c4bb5e1f 507 if(*src >= 0x20 && *src <= 0x7E)
dan_ackme 0:ea85c4bb5e1f 508 {
dan_ackme 0:ea85c4bb5e1f 509 *ptr++ = *src;
dan_ackme 0:ea85c4bb5e1f 510 }
dan_ackme 0:ea85c4bb5e1f 511 else
dan_ackme 0:ea85c4bb5e1f 512 {
dan_ackme 0:ea85c4bb5e1f 513 ptr += sprintf(ptr, "\\x%02X", (*src) & 0xff);
dan_ackme 0:ea85c4bb5e1f 514 }
dan_ackme 0:ea85c4bb5e1f 515 ++src;
dan_ackme 0:ea85c4bb5e1f 516 }
dan_ackme 0:ea85c4bb5e1f 517 *ptr = 0;
dan_ackme 0:ea85c4bb5e1f 518 return buf;
dan_ackme 0:ea85c4bb5e1f 519 }
dan_ackme 0:ea85c4bb5e1f 520
dan_ackme 0:ea85c4bb5e1f 521 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 522 bool NetworkInterface::strToMacAddress(const char *str, MacAddress *macAddress)
dan_ackme 0:ea85c4bb5e1f 523 {
dan_ackme 0:ea85c4bb5e1f 524 const char* strPtr = str;
dan_ackme 0:ea85c4bb5e1f 525 uint8_t *macPtr = (uint8_t*)macAddress->octet;
dan_ackme 0:ea85c4bb5e1f 526
dan_ackme 0:ea85c4bb5e1f 527 for(int count = 0; count < 6; ++count)
dan_ackme 0:ea85c4bb5e1f 528 {
dan_ackme 0:ea85c4bb5e1f 529 if(count < 5)
dan_ackme 0:ea85c4bb5e1f 530 {
dan_ackme 3:2dc2592bae5e 531 const char *idx = strchr(strPtr, ':');
dan_ackme 0:ea85c4bb5e1f 532 if(idx == NULL)
dan_ackme 0:ea85c4bb5e1f 533 {
dan_ackme 0:ea85c4bb5e1f 534 return false;
dan_ackme 0:ea85c4bb5e1f 535 }
dan_ackme 0:ea85c4bb5e1f 536 }
dan_ackme 0:ea85c4bb5e1f 537 int num = StringUtil::hexToInt(strPtr);
dan_ackme 0:ea85c4bb5e1f 538 if(num == -1)
dan_ackme 0:ea85c4bb5e1f 539 {
dan_ackme 0:ea85c4bb5e1f 540 return false;
dan_ackme 0:ea85c4bb5e1f 541 }
dan_ackme 0:ea85c4bb5e1f 542 *macPtr++ = (uint8_t)num;
dan_ackme 0:ea85c4bb5e1f 543 strPtr += 3;
dan_ackme 0:ea85c4bb5e1f 544 }
dan_ackme 0:ea85c4bb5e1f 545
dan_ackme 0:ea85c4bb5e1f 546 return true;
dan_ackme 0:ea85c4bb5e1f 547 }
dan_ackme 0:ea85c4bb5e1f 548
dan_ackme 0:ea85c4bb5e1f 549 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 550 const char* NetworkInterface::macAddressToStr(const MacAddress *macAddress, char *macStrBuffer)
dan_ackme 0:ea85c4bb5e1f 551 {
dan_ackme 0:ea85c4bb5e1f 552 SET_STR_BUFFER(macStrBuffer, sizeof(MacAddressStrBuffer));
dan_ackme 0:ea85c4bb5e1f 553 const uint8_t *mac = macAddress->octet;
dan_ackme 0:ea85c4bb5e1f 554
dan_ackme 0:ea85c4bb5e1f 555 sprintf(ptr, "%02X:%02X:%02X:%02X:%02X:%02X",
dan_ackme 0:ea85c4bb5e1f 556 (unsigned int)mac[0],
dan_ackme 0:ea85c4bb5e1f 557 (unsigned int)mac[1],
dan_ackme 0:ea85c4bb5e1f 558 (unsigned int)mac[2],
dan_ackme 0:ea85c4bb5e1f 559 (unsigned int)mac[3],
dan_ackme 0:ea85c4bb5e1f 560 (unsigned int)mac[4],
dan_ackme 0:ea85c4bb5e1f 561 (unsigned int)mac[5]);
dan_ackme 0:ea85c4bb5e1f 562
dan_ackme 0:ea85c4bb5e1f 563 return ptr;
dan_ackme 0:ea85c4bb5e1f 564 }