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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ScanResult.cpp Source File

ScanResult.cpp

00001 /**
00002  * ACKme WiConnect Host Library is licensed under the BSD licence: 
00003  * 
00004  * Copyright (c)2014 ACKme Networks.
00005  * All rights reserved. 
00006  * 
00007  * Redistribution and use in source and binary forms, with or without modification, 
00008  * are permitted provided that the following conditions are met: 
00009  * 
00010  * 1. Redistributions of source code must retain the above copyright notice, 
00011  * this list of conditions and the following disclaimer. 
00012  * 2. Redistributions in binary form must reproduce the above copyright notice, 
00013  * this list of conditions and the following disclaimer in the documentation 
00014  * and/or other materials provided with the distribution. 
00015  * 3. The name of the author may not be used to endorse or promote products 
00016  * derived from this software without specific prior written permission. 
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED 
00019  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00020  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00021  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00023  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00026  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00027  * OF SUCH DAMAGE.
00028  */
00029 #include "Wiconnect.h"
00030 #include "api/types/ScanResult.h"
00031 #include "api/NetworkInterface.h"
00032 #include "internal/common.h"
00033 
00034 
00035 static inline bool floatToFixedPointInt(const char *str, uint32_t *res);
00036 
00037 
00038 
00039 /*************************************************************************************************/
00040 ScanResult::ScanResult()
00041 {
00042     next = NULL;
00043     previous = NULL;
00044     channel = 0xff;
00045     rssi = -9999;
00046     rate = 0;
00047     security = NETWORK_SECURITY_UNKNOWN;
00048     memset(&mac, 0, (uint32_t)sizeof(mac));
00049     memset(&ssid, 0, (uint32_t)sizeof(ssid));
00050 }
00051 
00052 /*************************************************************************************************/
00053 WiconnectResult ScanResult::init(const char *channelStr, const char *rssiStr, const char* macStr, const char *rateStr, const char *secStr, const char *ssidStr)
00054 {
00055     intmax_t r;
00056     if(!StringUtil::parseInt(channelStr, &r, 0, 15))
00057     {
00058         return WICONNECT_RESPONSE_PARSE_ERROR;
00059     }
00060     channel = (int)r;
00061     if(!StringUtil::parseInt(rssiStr, &r, -200, 100))
00062     {
00063         return WICONNECT_RESPONSE_PARSE_ERROR;
00064     }
00065     rssi = (int)r;
00066 
00067     if(!Wiconnect::strToMacAddress(macStr, &mac))
00068     {
00069         return WICONNECT_RESPONSE_PARSE_ERROR;
00070     }
00071 
00072     if(!floatToFixedPointInt(rateStr, &rate))
00073     {
00074         return WICONNECT_RESPONSE_PARSE_ERROR;
00075     }
00076     security = Wiconnect::strToNetworkSecurity(secStr);
00077 
00078     if(!Wiconnect::strToSsid(ssidStr, &ssid))
00079     {
00080         return WICONNECT_RESPONSE_PARSE_ERROR;
00081     }
00082 
00083     return WICONNECT_SUCCESS;
00084 }
00085 
00086 #ifdef WICONNECT_ENABLE_MALLOC
00087 /*************************************************************************************************/
00088 void* ScanResult::operator new(size_t size)
00089 {
00090     Wiconnect *wiconnect = Wiconnect::getInstance();
00091     wiconnect_assert(wiconnect, "ScanResult:new, malloc not defined", wiconnect->_malloc != NULL);
00092     return Wiconnect::getInstance()->_malloc(size);
00093 }
00094 
00095 /*************************************************************************************************/
00096 void ScanResult::operator delete(void* ptr)
00097 {
00098     Wiconnect *wiconnect = Wiconnect::getInstance();
00099     wiconnect_assert(wiconnect, "ScanResult:delete, free not defined", wiconnect->_free != NULL);
00100     Wiconnect::getInstance()->_free(ptr);
00101 }
00102 #endif
00103 
00104 /*************************************************************************************************/
00105 uint8_t ScanResult::getChannel() const
00106 {
00107     return channel;
00108 }
00109 /*************************************************************************************************/
00110 NetworkSignalStrength ScanResult::getSignalStrength() const
00111 {
00112     return Wiconnect::rssiToSignalStrength(rssi);
00113 }
00114 /*************************************************************************************************/
00115 const MacAddress* ScanResult::getMacAddress() const
00116 {
00117     return &mac;
00118 }
00119 /*************************************************************************************************/
00120 uint32_t ScanResult::getRate() const
00121 {
00122     return rate;
00123 }
00124 /*************************************************************************************************/
00125 const char* ScanResult::getRateStr(char *buffer) const
00126 {
00127     SET_STR_BUFFER(buffer, 16);
00128     uint32_t i = rate / 10;
00129     uint32_t f = rate % 10;
00130     sprintf(ptr, "%u.%u", (unsigned int)i, (unsigned int)f);
00131     return ptr;
00132 }
00133 /*************************************************************************************************/
00134 NetworkSecurity ScanResult::getSecurityType() const
00135 {
00136     return security;
00137 }
00138 /*************************************************************************************************/
00139 const Ssid* ScanResult::getSsid() const
00140 {
00141     return &ssid;
00142 }
00143 
00144 /*************************************************************************************************/
00145 const ScanResult* ScanResult::getNext() const
00146 {
00147     return next;
00148 }
00149 
00150 /*************************************************************************************************/
00151 const ScanResult* ScanResult::getPrevious() const
00152 {
00153     return previous;
00154 }
00155 
00156 
00157 /*************************************************************************************************/
00158 static inline bool floatToFixedPointInt(const char *str, uint32_t *res)
00159 {
00160     intmax_t i;
00161     intmax_t f = 0;
00162     char buffer[32];
00163 
00164     strcpy(buffer, str);
00165 
00166     char* frac = strchr(buffer, '.');
00167     if(frac != NULL)
00168     {
00169         *frac = 0;
00170         ++frac;
00171     }
00172 
00173     if(!StringUtil::parseInt(buffer, &i, 0, 1000))
00174     {
00175         return false;
00176     }
00177     if(frac != NULL && !StringUtil::parseInt(frac, &f, 0, 9))
00178     {
00179         return false;
00180     }
00181 
00182     *res = (((uint32_t)i) * 10) + (uint32_t)f;
00183 
00184     return true;
00185 }
00186