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

internal/types/ScanResultList.cpp

Committer:
dan_ackme
Date:
2014-08-11
Revision:
6:8a87a59d0d21
Parent:
1:6ec9998427ad
Child:
16:7f1d6d359787

File content as of revision 6:8a87a59d0d21:

/*
 * 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 "Wiconnect.h"
#include "types/ScanResult.h"
#include "internal/common.h"


/*************************************************************************************************/
ScanResultList::ScanResultList(int bufferLen_, void *buffer_)
{
    Wiconnect *wiconnect = Wiconnect::getInstance();
    wiconnect_assert(wiconnect, "ScanResultList(), bad buffer", (bufferLen_ == 0 && buffer_ == NULL) || (bufferLen_ != 0 && buffer_ != NULL));
    count = 0;
    listHead = listTail = NULL;
    buffer = (uint8_t*)buffer_;
    bufferPtr = buffer;
    bufferLen = bufferLen_;
}

/*************************************************************************************************/
ScanResultList::~ScanResultList()
{
    if(buffer == NULL)
    {
        ScanResult* result = listHead;
        while(result != NULL)
        {
            ScanResult* tmp = result;
            result = result->next;
            delete tmp;
        }
    }
}

/*************************************************************************************************/
WiconnectResult ScanResultList::add(const char *channelStr, const char *rssiStr, const char* macStr, const char *rateStr, const char *secStr, const char *ssidStr)
{
    WiconnectResult result;
    ScanResult *res;

    if(buffer == NULL)
    {
        res = new ScanResult();
        if(res == NULL)
        {
            return WICONNECT_NULL_BUFFER;
        }
    }
    else
    {
        if(bufferLen < sizeof(ScanResult))
        {
            return WICONNECT_OVERFLOW;
        }
        res = (ScanResult*)bufferPtr;
        memset(res, 0, sizeof(ScanResult));
        bufferLen -= sizeof(ScanResult);
        bufferPtr += sizeof(ScanResult);
    }

    if(WICONNECT_FAILED(result, res->init(channelStr, rssiStr, macStr, rateStr, secStr, ssidStr)))
    {
        if(buffer == NULL)
        {
            delete res;
        }
    }
    else
    {
        if(listHead == NULL)
        {
            listHead = listTail = res;
        }
        else
        {
            res->previous = listTail;
            listTail->next = res;
            listTail = res;
        }
        ++count;
    }

    return result;
}

/*************************************************************************************************/
const ScanResult* ScanResultList::getListHead() const
{
    return listHead;
}

/*************************************************************************************************/
int ScanResultList::getCount() const
{
    return count;
}

/*************************************************************************************************/
const ScanResult* ScanResultList::getResult(int i) const
{
    if(i >= count)
        return NULL;

    ScanResult* result = listHead;
    while(i-- != 0)
        result = result->next;

    return result;
}

/*************************************************************************************************/
const ScanResult* ScanResultList::operator [](int i) const
{
    return getResult(i);
}