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
Diff: internal/types/ScanResult.cpp
- Revision:
- 0:ea85c4bb5e1f
- Child:
- 1:6ec9998427ad
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/internal/types/ScanResult.cpp Mon Aug 11 09:58:24 2014 +0000 @@ -0,0 +1,167 @@ +/* + * Copyright 2014, ACKme Networks + * All Rights Reserved. + * + * This is UNPUBLISHED PROPRIETARY SOURCE CODE of ACKme Networks; + * the contents of this file may not be disclosed to third parties, copied + * or duplicated in any form, in whole or in part, without the prior + * written permission of ACKme Networks. + */ + +#include <assert.h> + +#include "Wiconnect.h" +#include "types/ScanResult.h" +#include "NetworkInterface.h" +#include "internal/common.h" + + +static inline bool floatToFixedPointInt(const char *str, uint32_t *res); + + + +/*************************************************************************************************/ +ScanResult::ScanResult() +{ + next = NULL; + previous = NULL; + channel = 0xff; + rssi = -9999; + rate = 0; + security = NETWORK_SECURITY_UNKNOWN; + memset(&mac, 0, (uint32_t)sizeof(mac)); + memset(&ssid, 0, (uint32_t)sizeof(ssid)); +} + +/*************************************************************************************************/ +WiconnectResult ScanResult::init(const char *channelStr, const char *rssiStr, const char* macStr, const char *rateStr, const char *secStr, const char *ssidStr) +{ + intmax_t r; + if(!StringUtil::parseInt(channelStr, &r, 0, 15)) + { + return WICONNECT_RESPONSE_PARSE_ERROR; + } + channel = (int)r; + if(!StringUtil::parseInt(rssiStr, &r, -200, 100)) + { + return WICONNECT_RESPONSE_PARSE_ERROR; + } + rssi = (int)r; + + if(!Wiconnect::strToMacAddress(macStr, &mac)) + { + return WICONNECT_RESPONSE_PARSE_ERROR; + } + + if(!floatToFixedPointInt(rateStr, &rate)) + { + return WICONNECT_RESPONSE_PARSE_ERROR; + } + security = Wiconnect::strToNetworkSecurity(secStr); + + if(!Wiconnect::strToSsid(ssidStr, &ssid)) + { + return WICONNECT_RESPONSE_PARSE_ERROR; + } + + return WICONNECT_SUCCESS; +} + +/*************************************************************************************************/ +void* ScanResult::operator new(size_t size) +{ + assert(Wiconnect::getInstance()->_malloc != NULL); + return Wiconnect::getInstance()->_malloc(size); +} + +/*************************************************************************************************/ +void ScanResult::operator delete(void* ptr) +{ + assert(Wiconnect::getInstance()->_free != NULL); + Wiconnect::getInstance()->_free(ptr); +} + +/*************************************************************************************************/ +uint8_t ScanResult::getChannel() const +{ + return channel; +} +/*************************************************************************************************/ +NetworkSignalStrength ScanResult::getSignalStrength() const +{ + return Wiconnect::rssiToSignalStrength(rssi); +} +/*************************************************************************************************/ +const MacAddress* ScanResult::getMacAddress() const +{ + return &mac; +} +/*************************************************************************************************/ +uint32_t ScanResult::getRate() const +{ + return rate; +} +/*************************************************************************************************/ +const char* ScanResult::getRateStr(char *buffer) const +{ + SET_STR_BUFFER(buffer, 16); + uint32_t i = rate / 10; + uint32_t f = rate % 10; + sprintf(ptr, "%u.%u", i, f); + return ptr; +} +/*************************************************************************************************/ +NetworkSecurity ScanResult::getSecurityType() const +{ + return security; +} +/*************************************************************************************************/ +const Ssid* ScanResult::getSsid() const +{ + return &ssid; +} + +/*************************************************************************************************/ +const ScanResult* ScanResult::getNext() const +{ + return next; +} + +/*************************************************************************************************/ +const ScanResult* ScanResult::getPrevious() const +{ + return previous; +} + + +/*************************************************************************************************/ +static inline bool floatToFixedPointInt(const char *str, uint32_t *res) +{ + intmax_t i; + intmax_t f = 0; + char buffer[32]; + + strcpy(buffer, str); + + char* frac = strchr(buffer, '.'); + if(frac != NULL) + { + *frac = 0; + ++frac; + } + + if(!StringUtil::parseInt(buffer, &i, 0, 1000)) + { + return false; + } + if(frac != NULL && !StringUtil::parseInt(frac, &f, 0, 9)) + { + return false; + } + + *res = (((uint32_t)i) * 10) + (uint32_t)f; + + return true; +} + +