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 NetworkScan.cpp Source File

NetworkScan.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 "internal/common.h"
00031 
00032 
00033 #define SCAN_TIMEOUT 15000
00034 
00035 
00036 
00037 
00038 /*************************************************************************************************/
00039 WiconnectResult NetworkInterface::scan(ScanResultList &resultList, const uint8_t *channelList, const char* ssid)
00040 {
00041     WiconnectResult result;
00042     char *cmdBuffer = wiconnect->internalBuffer;
00043 
00044 
00045     if(WICONNECT_IS_IDLE())
00046     {
00047 #define SCAN_CMD "scan -v "
00048         char *cmdBufferPtr = cmdBuffer + sizeof(SCAN_CMD)-1;
00049 
00050         strcpy(cmdBuffer, SCAN_CMD);
00051 
00052         if(channelList != NULL)
00053         {
00054             for(const uint8_t *ch = (const uint8_t *)channelList; *ch != 0; ++ch)
00055             {
00056                 cmdBufferPtr += sprintf(cmdBufferPtr, "%d,", *ch);
00057             }
00058             *(cmdBufferPtr-1) = ' ';
00059         }
00060         else
00061         {
00062             strcat(cmdBufferPtr, "all ");
00063             cmdBufferPtr += 4;
00064         }
00065 
00066         if(ssid != NULL)
00067         {
00068             strcpy(cmdBufferPtr, ssid);
00069         }
00070 
00071         resultList.reset();
00072     }
00073 
00074     CHECK_OTHER_COMMAND_EXECUTING();
00075 
00076     //if(!completeHandler_.isValid())
00077     {
00078         if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(SCAN_TIMEOUT, cmdBuffer)))
00079         {
00080             result = processScanResults(wiconnect->internalBuffer, resultList);
00081         }
00082     }
00083 //#ifdef WICONNECT_ASYNC_TIMER_ENABLED
00084 //    else
00085 //    {
00086 //        QueuedCommand *cmd = new QueuedCommand(NULL, 4096, SCAN_TIMEOUT, cmdBuffer);
00087 //        cmd->userData = (void*)resultList;
00088 //        completeHandler = completeHandler_;
00089 //        if(WICONNECT_FAILED(result, wiconnect->enqueueCommand(cmd, Callback(this, &NetworkInterface::scanCompleteCallback))))
00090 //        {
00091 //            delete cmd;
00092 //        }
00093 //        else
00094 //        {
00095 //            result = WICONNECT_PROCESSING;
00096 //        }
00097 //    }
00098 //#endif
00099 
00100     CHECK_CLEANUP_COMMAND();
00101 
00102     return result;
00103 }
00104 
00105 
00106 
00107 
00108 
00109 /*************************************************************************************************/
00110 WiconnectResult NetworkInterface::processScanResults(char *resultStr, ScanResultList &resultList)
00111 {
00112     WiconnectResult result = WICONNECT_SUCCESS;
00113     char *line, *savedLine;
00114 
00115     for(savedLine = resultStr; (line = StringUtil::strtok_r(savedLine, "\r\n", &savedLine)) != NULL;)
00116     {
00117         char *toks[9], *savedTok;
00118 
00119         if(*line != '#')
00120         {
00121             continue;
00122         }
00123         savedTok = line + 2;
00124 
00125         for(int i = 0; i < 8 && (toks[i] = StringUtil::strtok_r(savedTok, " ", &savedTok)) != NULL; ++i)
00126         {
00127             if(toks[i] == NULL)
00128             {
00129                 result = WICONNECT_RESPONSE_PARSE_ERROR;
00130                 goto exit;
00131             }
00132         }
00133 
00134         if(WICONNECT_FAILED(result, resultList.add(toks[1], toks[2], toks[3], toks[4], toks[5], savedTok)))
00135         {
00136             goto exit;
00137         }
00138     }
00139 
00140     exit:
00141     return result;
00142 }
00143 
00144 //#ifdef WICONNECT_ASYNC_TIMER_ENABLED
00145 //
00146 /*************************************************************************************************/
00147 //void NetworkInterface::scanCompleteCallback(WiconnectResult result, void *arg1, void *arg2)
00148 //{
00149 //    QueuedCommand *cmd = (QueuedCommand*)arg1;
00150 //    ScanResultList *listPtr = (ScanResultList*)cmd->userData;
00151 //
00152 //    if(result == WICONNECT_SUCCESS)
00153 //    {
00154 //        result = processScanResults(cmd->responseBuffer, listPtr);
00155 //    }
00156 //    delete cmd;
00157 //
00158 //    completeHandler.call(result, listPtr, NULL);
00159 //}
00160 //
00161 //#endif