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:
Sat Sep 06 20:34:01 2014 -0700
Revision:
24:e27e23297f02
Parent:
16:7f1d6d359787
add api to get/set module settings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dan_ackme 24:e27e23297f02 1 /**
dan_ackme 24:e27e23297f02 2 * ACKme WiConnect Host Library is licensed under the BSD licence:
dan_ackme 24:e27e23297f02 3 *
dan_ackme 24:e27e23297f02 4 * Copyright (c)2014 ACKme Networks.
dan_ackme 24:e27e23297f02 5 * All rights reserved.
dan_ackme 24:e27e23297f02 6 *
dan_ackme 24:e27e23297f02 7 * Redistribution and use in source and binary forms, with or without modification,
dan_ackme 24:e27e23297f02 8 * are permitted provided that the following conditions are met:
dan_ackme 24:e27e23297f02 9 *
dan_ackme 24:e27e23297f02 10 * 1. Redistributions of source code must retain the above copyright notice,
dan_ackme 24:e27e23297f02 11 * this list of conditions and the following disclaimer.
dan_ackme 24:e27e23297f02 12 * 2. Redistributions in binary form must reproduce the above copyright notice,
dan_ackme 24:e27e23297f02 13 * this list of conditions and the following disclaimer in the documentation
dan_ackme 24:e27e23297f02 14 * and/or other materials provided with the distribution.
dan_ackme 24:e27e23297f02 15 * 3. The name of the author may not be used to endorse or promote products
dan_ackme 24:e27e23297f02 16 * derived from this software without specific prior written permission.
dan_ackme 24:e27e23297f02 17 *
dan_ackme 24:e27e23297f02 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED
dan_ackme 24:e27e23297f02 19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
dan_ackme 24:e27e23297f02 20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
dan_ackme 24:e27e23297f02 21 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
dan_ackme 24:e27e23297f02 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
dan_ackme 24:e27e23297f02 23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dan_ackme 24:e27e23297f02 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dan_ackme 24:e27e23297f02 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
dan_ackme 24:e27e23297f02 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
dan_ackme 24:e27e23297f02 27 * OF SUCH DAMAGE.
dan_ackme 0:ea85c4bb5e1f 28 */
dan_ackme 0:ea85c4bb5e1f 29 #include "Wiconnect.h"
dan_ackme 0:ea85c4bb5e1f 30 #include "types/ScanResult.h"
dan_ackme 0:ea85c4bb5e1f 31 #include "NetworkInterface.h"
dan_ackme 0:ea85c4bb5e1f 32 #include "internal/common.h"
dan_ackme 0:ea85c4bb5e1f 33
dan_ackme 0:ea85c4bb5e1f 34
dan_ackme 0:ea85c4bb5e1f 35 static inline bool floatToFixedPointInt(const char *str, uint32_t *res);
dan_ackme 0:ea85c4bb5e1f 36
dan_ackme 0:ea85c4bb5e1f 37
dan_ackme 0:ea85c4bb5e1f 38
dan_ackme 0:ea85c4bb5e1f 39 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 40 ScanResult::ScanResult()
dan_ackme 0:ea85c4bb5e1f 41 {
dan_ackme 0:ea85c4bb5e1f 42 next = NULL;
dan_ackme 0:ea85c4bb5e1f 43 previous = NULL;
dan_ackme 0:ea85c4bb5e1f 44 channel = 0xff;
dan_ackme 0:ea85c4bb5e1f 45 rssi = -9999;
dan_ackme 0:ea85c4bb5e1f 46 rate = 0;
dan_ackme 0:ea85c4bb5e1f 47 security = NETWORK_SECURITY_UNKNOWN;
dan_ackme 0:ea85c4bb5e1f 48 memset(&mac, 0, (uint32_t)sizeof(mac));
dan_ackme 0:ea85c4bb5e1f 49 memset(&ssid, 0, (uint32_t)sizeof(ssid));
dan_ackme 0:ea85c4bb5e1f 50 }
dan_ackme 0:ea85c4bb5e1f 51
dan_ackme 0:ea85c4bb5e1f 52 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 53 WiconnectResult ScanResult::init(const char *channelStr, const char *rssiStr, const char* macStr, const char *rateStr, const char *secStr, const char *ssidStr)
dan_ackme 0:ea85c4bb5e1f 54 {
dan_ackme 0:ea85c4bb5e1f 55 intmax_t r;
dan_ackme 0:ea85c4bb5e1f 56 if(!StringUtil::parseInt(channelStr, &r, 0, 15))
dan_ackme 0:ea85c4bb5e1f 57 {
dan_ackme 0:ea85c4bb5e1f 58 return WICONNECT_RESPONSE_PARSE_ERROR;
dan_ackme 0:ea85c4bb5e1f 59 }
dan_ackme 0:ea85c4bb5e1f 60 channel = (int)r;
dan_ackme 0:ea85c4bb5e1f 61 if(!StringUtil::parseInt(rssiStr, &r, -200, 100))
dan_ackme 0:ea85c4bb5e1f 62 {
dan_ackme 0:ea85c4bb5e1f 63 return WICONNECT_RESPONSE_PARSE_ERROR;
dan_ackme 0:ea85c4bb5e1f 64 }
dan_ackme 0:ea85c4bb5e1f 65 rssi = (int)r;
dan_ackme 0:ea85c4bb5e1f 66
dan_ackme 0:ea85c4bb5e1f 67 if(!Wiconnect::strToMacAddress(macStr, &mac))
dan_ackme 0:ea85c4bb5e1f 68 {
dan_ackme 0:ea85c4bb5e1f 69 return WICONNECT_RESPONSE_PARSE_ERROR;
dan_ackme 0:ea85c4bb5e1f 70 }
dan_ackme 0:ea85c4bb5e1f 71
dan_ackme 0:ea85c4bb5e1f 72 if(!floatToFixedPointInt(rateStr, &rate))
dan_ackme 0:ea85c4bb5e1f 73 {
dan_ackme 0:ea85c4bb5e1f 74 return WICONNECT_RESPONSE_PARSE_ERROR;
dan_ackme 0:ea85c4bb5e1f 75 }
dan_ackme 0:ea85c4bb5e1f 76 security = Wiconnect::strToNetworkSecurity(secStr);
dan_ackme 0:ea85c4bb5e1f 77
dan_ackme 0:ea85c4bb5e1f 78 if(!Wiconnect::strToSsid(ssidStr, &ssid))
dan_ackme 0:ea85c4bb5e1f 79 {
dan_ackme 0:ea85c4bb5e1f 80 return WICONNECT_RESPONSE_PARSE_ERROR;
dan_ackme 0:ea85c4bb5e1f 81 }
dan_ackme 0:ea85c4bb5e1f 82
dan_ackme 0:ea85c4bb5e1f 83 return WICONNECT_SUCCESS;
dan_ackme 0:ea85c4bb5e1f 84 }
dan_ackme 0:ea85c4bb5e1f 85
dan_ackme 0:ea85c4bb5e1f 86 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 87 void* ScanResult::operator new(size_t size)
dan_ackme 0:ea85c4bb5e1f 88 {
dan_ackme 6:8a87a59d0d21 89 Wiconnect *wiconnect = Wiconnect::getInstance();
dan_ackme 6:8a87a59d0d21 90 wiconnect_assert(wiconnect, "ScanResult:new, malloc not defined", wiconnect->_malloc != NULL);
dan_ackme 0:ea85c4bb5e1f 91 return Wiconnect::getInstance()->_malloc(size);
dan_ackme 0:ea85c4bb5e1f 92 }
dan_ackme 0:ea85c4bb5e1f 93
dan_ackme 0:ea85c4bb5e1f 94 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 95 void ScanResult::operator delete(void* ptr)
dan_ackme 0:ea85c4bb5e1f 96 {
dan_ackme 6:8a87a59d0d21 97 Wiconnect *wiconnect = Wiconnect::getInstance();
dan_ackme 6:8a87a59d0d21 98 wiconnect_assert(wiconnect, "ScanResult:delete, free not defined", wiconnect->_free != NULL);
dan_ackme 0:ea85c4bb5e1f 99 Wiconnect::getInstance()->_free(ptr);
dan_ackme 0:ea85c4bb5e1f 100 }
dan_ackme 0:ea85c4bb5e1f 101
dan_ackme 0:ea85c4bb5e1f 102 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 103 uint8_t ScanResult::getChannel() const
dan_ackme 0:ea85c4bb5e1f 104 {
dan_ackme 0:ea85c4bb5e1f 105 return channel;
dan_ackme 0:ea85c4bb5e1f 106 }
dan_ackme 0:ea85c4bb5e1f 107 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 108 NetworkSignalStrength ScanResult::getSignalStrength() const
dan_ackme 0:ea85c4bb5e1f 109 {
dan_ackme 0:ea85c4bb5e1f 110 return Wiconnect::rssiToSignalStrength(rssi);
dan_ackme 0:ea85c4bb5e1f 111 }
dan_ackme 0:ea85c4bb5e1f 112 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 113 const MacAddress* ScanResult::getMacAddress() const
dan_ackme 0:ea85c4bb5e1f 114 {
dan_ackme 0:ea85c4bb5e1f 115 return &mac;
dan_ackme 0:ea85c4bb5e1f 116 }
dan_ackme 0:ea85c4bb5e1f 117 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 118 uint32_t ScanResult::getRate() const
dan_ackme 0:ea85c4bb5e1f 119 {
dan_ackme 0:ea85c4bb5e1f 120 return rate;
dan_ackme 0:ea85c4bb5e1f 121 }
dan_ackme 0:ea85c4bb5e1f 122 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 123 const char* ScanResult::getRateStr(char *buffer) const
dan_ackme 0:ea85c4bb5e1f 124 {
dan_ackme 0:ea85c4bb5e1f 125 SET_STR_BUFFER(buffer, 16);
dan_ackme 0:ea85c4bb5e1f 126 uint32_t i = rate / 10;
dan_ackme 0:ea85c4bb5e1f 127 uint32_t f = rate % 10;
dan_ackme 0:ea85c4bb5e1f 128 sprintf(ptr, "%u.%u", i, f);
dan_ackme 0:ea85c4bb5e1f 129 return ptr;
dan_ackme 0:ea85c4bb5e1f 130 }
dan_ackme 0:ea85c4bb5e1f 131 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 132 NetworkSecurity ScanResult::getSecurityType() const
dan_ackme 0:ea85c4bb5e1f 133 {
dan_ackme 0:ea85c4bb5e1f 134 return security;
dan_ackme 0:ea85c4bb5e1f 135 }
dan_ackme 0:ea85c4bb5e1f 136 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 137 const Ssid* ScanResult::getSsid() const
dan_ackme 0:ea85c4bb5e1f 138 {
dan_ackme 0:ea85c4bb5e1f 139 return &ssid;
dan_ackme 0:ea85c4bb5e1f 140 }
dan_ackme 0:ea85c4bb5e1f 141
dan_ackme 0:ea85c4bb5e1f 142 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 143 const ScanResult* ScanResult::getNext() const
dan_ackme 0:ea85c4bb5e1f 144 {
dan_ackme 0:ea85c4bb5e1f 145 return next;
dan_ackme 0:ea85c4bb5e1f 146 }
dan_ackme 0:ea85c4bb5e1f 147
dan_ackme 0:ea85c4bb5e1f 148 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 149 const ScanResult* ScanResult::getPrevious() const
dan_ackme 0:ea85c4bb5e1f 150 {
dan_ackme 0:ea85c4bb5e1f 151 return previous;
dan_ackme 0:ea85c4bb5e1f 152 }
dan_ackme 0:ea85c4bb5e1f 153
dan_ackme 0:ea85c4bb5e1f 154
dan_ackme 0:ea85c4bb5e1f 155 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 156 static inline bool floatToFixedPointInt(const char *str, uint32_t *res)
dan_ackme 0:ea85c4bb5e1f 157 {
dan_ackme 0:ea85c4bb5e1f 158 intmax_t i;
dan_ackme 0:ea85c4bb5e1f 159 intmax_t f = 0;
dan_ackme 0:ea85c4bb5e1f 160 char buffer[32];
dan_ackme 0:ea85c4bb5e1f 161
dan_ackme 0:ea85c4bb5e1f 162 strcpy(buffer, str);
dan_ackme 0:ea85c4bb5e1f 163
dan_ackme 0:ea85c4bb5e1f 164 char* frac = strchr(buffer, '.');
dan_ackme 0:ea85c4bb5e1f 165 if(frac != NULL)
dan_ackme 0:ea85c4bb5e1f 166 {
dan_ackme 0:ea85c4bb5e1f 167 *frac = 0;
dan_ackme 0:ea85c4bb5e1f 168 ++frac;
dan_ackme 0:ea85c4bb5e1f 169 }
dan_ackme 0:ea85c4bb5e1f 170
dan_ackme 0:ea85c4bb5e1f 171 if(!StringUtil::parseInt(buffer, &i, 0, 1000))
dan_ackme 0:ea85c4bb5e1f 172 {
dan_ackme 0:ea85c4bb5e1f 173 return false;
dan_ackme 0:ea85c4bb5e1f 174 }
dan_ackme 0:ea85c4bb5e1f 175 if(frac != NULL && !StringUtil::parseInt(frac, &f, 0, 9))
dan_ackme 0:ea85c4bb5e1f 176 {
dan_ackme 0:ea85c4bb5e1f 177 return false;
dan_ackme 0:ea85c4bb5e1f 178 }
dan_ackme 0:ea85c4bb5e1f 179
dan_ackme 0:ea85c4bb5e1f 180 *res = (((uint32_t)i) * 10) + (uint32_t)f;
dan_ackme 0:ea85c4bb5e1f 181
dan_ackme 0:ea85c4bb5e1f 182 return true;
dan_ackme 0:ea85c4bb5e1f 183 }
dan_ackme 0:ea85c4bb5e1f 184