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

NetworkWebSetup.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 
00034 /*************************************************************************************************/
00035 WiconnectResult NetworkInterface::startWebSetup(const char *ssid, const char *password, const Callback &completeHandler_)
00036 {
00037     WiconnectResult result = WICONNECT_ERROR;;
00038 
00039     enum
00040     {
00041         FS_SET_SSID,
00042         FS_SET_PASSWORD,
00043         FS_NETWORK_UP
00044     };
00045 
00046     CHECK_CALLBACK_AVAILABLE(completeHandler_);
00047     CHECK_OTHER_COMMAND_EXECUTING();
00048 
00049     if(wiconnect->internalProcessingState == FS_SET_SSID)
00050     {
00051         if(ssid == NULL ||
00052            WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("set setup.web.ssid %s", ssid)))
00053         {
00054             wiconnect->internalProcessingState = FS_SET_PASSWORD;
00055         }
00056     }
00057 
00058     if(wiconnect->internalProcessingState == FS_SET_PASSWORD)
00059     {
00060         if(password == NULL ||
00061            WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("set setup.web.passkey %s", password)))
00062         {
00063             wiconnect->internalProcessingState = FS_NETWORK_UP;
00064         }
00065     }
00066 
00067     if(wiconnect->internalProcessingState == FS_NETWORK_UP)
00068     {
00069         if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("setup web")))
00070         {
00071 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
00072             if(completeHandler_.isValid())
00073             {
00074 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
00075                 monitorTimer.stop();
00076 #endif
00077                 completeHandler = completeHandler_;
00078                 monitorTimer.start(this, &NetworkInterface::webSetupStatusMonitor, 1000);
00079             }
00080 #endif
00081         }
00082     }
00083 
00084     CHECK_CLEANUP_COMMAND();
00085 
00086     return result;
00087 }
00088 
00089 /*************************************************************************************************/
00090 WiconnectResult NetworkInterface::stopWebSetup()
00091 {
00092     WiconnectResult result;
00093 
00094     CHECK_OTHER_COMMAND_EXECUTING();
00095 
00096     #ifdef WICONNECT_ASYNC_TIMER_ENABLED
00097         monitorTimer.stop();
00098     #endif
00099     result = wiconnect->sendCommand("setup stop");
00100 
00101     CHECK_CLEANUP_COMMAND();
00102 
00103     return result;
00104 }
00105 
00106 
00107 /*************************************************************************************************/
00108 WiconnectResult NetworkInterface::isWebSetupRunning(bool *isRunningPtr)
00109 {
00110     WiconnectResult result;
00111 
00112     CHECK_OTHER_COMMAND_EXECUTING();
00113 
00114     if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("setup status")))
00115     {
00116         int32_t status;
00117         if(!WICONNECT_FAILED(result, wiconnect->responseToInt32(&status)))
00118         {
00119             if(status)
00120             {
00121 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
00122                 monitorTimer.stop();
00123 #endif
00124             }
00125             *isRunningPtr = (bool)status;
00126         }
00127     }
00128 
00129     CHECK_CLEANUP_COMMAND();
00130 
00131     return result;
00132 }
00133 
00134 
00135 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
00136 
00137 /*************************************************************************************************/
00138 void NetworkInterface::webSetupStatusMonitor()
00139 {
00140     static char responseBuffer[4];
00141     static uint8_t cmdBuffer[sizeof(QueuedCommand)];
00142     QueuedCommand *cmd = (QueuedCommand*)cmdBuffer;
00143 
00144     monitorTimer.stop();
00145 
00146     *cmd = QueuedCommand(sizeof(responseBuffer), responseBuffer, "setup status");
00147 
00148     wiconnect->enqueueCommand(cmd, Callback(this, &NetworkInterface::webSetupStatusCheckCallback));
00149 }
00150 
00151 /*************************************************************************************************/
00152 void NetworkInterface::webSetupStatusCheckCallback(WiconnectResult result, void *arg1, void *arg2)
00153 {
00154     bool isComplete = true;
00155 
00156     QueuedCommand *cmd = (QueuedCommand*)arg1;
00157 
00158     if(result == WICONNECT_SUCCESS)
00159     {
00160         int32_t status;
00161         if(!StringUtil::strToInt32(cmd->responseBuffer, &status))
00162         {
00163             result = WICONNECT_RESPONSE_PARSE_ERROR;
00164         }
00165         else if(status > 0)
00166         {
00167             isComplete = false;
00168         }
00169     }
00170 
00171     if(isComplete)
00172     {
00173         completeHandler.call(result, NULL, NULL);
00174     }
00175     else
00176     {
00177         monitorTimer.start(this, &NetworkInterface::webSetupStatusMonitor, 1000);
00178     }
00179 }
00180 
00181 #endif