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

Committer:
dan_ackme
Date:
Sat Sep 06 20:34:01 2014 -0700
Revision:
24:e27e23297f02
Parent:
16:7f1d6d359787
add api to get/set module settings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dan_ackme 24:e27e23297f02 1 /**
dan_ackme 24:e27e23297f02 2 * ACKme WiConnect Host Library is licensed under the BSD licence:
dan_ackme 24:e27e23297f02 3 *
dan_ackme 24:e27e23297f02 4 * Copyright (c)2014 ACKme Networks.
dan_ackme 24:e27e23297f02 5 * All rights reserved.
dan_ackme 24:e27e23297f02 6 *
dan_ackme 24:e27e23297f02 7 * Redistribution and use in source and binary forms, with or without modification,
dan_ackme 24:e27e23297f02 8 * are permitted provided that the following conditions are met:
dan_ackme 24:e27e23297f02 9 *
dan_ackme 24:e27e23297f02 10 * 1. Redistributions of source code must retain the above copyright notice,
dan_ackme 24:e27e23297f02 11 * this list of conditions and the following disclaimer.
dan_ackme 24:e27e23297f02 12 * 2. Redistributions in binary form must reproduce the above copyright notice,
dan_ackme 24:e27e23297f02 13 * this list of conditions and the following disclaimer in the documentation
dan_ackme 24:e27e23297f02 14 * and/or other materials provided with the distribution.
dan_ackme 24:e27e23297f02 15 * 3. The name of the author may not be used to endorse or promote products
dan_ackme 24:e27e23297f02 16 * derived from this software without specific prior written permission.
dan_ackme 24:e27e23297f02 17 *
dan_ackme 24:e27e23297f02 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED
dan_ackme 24:e27e23297f02 19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
dan_ackme 24:e27e23297f02 20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
dan_ackme 24:e27e23297f02 21 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
dan_ackme 24:e27e23297f02 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
dan_ackme 24:e27e23297f02 23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dan_ackme 24:e27e23297f02 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dan_ackme 24:e27e23297f02 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
dan_ackme 24:e27e23297f02 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
dan_ackme 24:e27e23297f02 27 * OF SUCH DAMAGE.
dan_ackme 0:ea85c4bb5e1f 28 */
dan_ackme 0:ea85c4bb5e1f 29 #include "Wiconnect.h"
dan_ackme 0:ea85c4bb5e1f 30 #include "internal/common.h"
dan_ackme 0:ea85c4bb5e1f 31
dan_ackme 0:ea85c4bb5e1f 32
dan_ackme 0:ea85c4bb5e1f 33
dan_ackme 0:ea85c4bb5e1f 34 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 35 WiconnectResult NetworkInterface::startWebSetup(const char *ssid, const char *password, const Callback &completeHandler_)
dan_ackme 0:ea85c4bb5e1f 36 {
dan_ackme 5:8d91a87ebba2 37 WiconnectResult result = WICONNECT_ERROR;;
dan_ackme 0:ea85c4bb5e1f 38
dan_ackme 0:ea85c4bb5e1f 39 enum
dan_ackme 0:ea85c4bb5e1f 40 {
dan_ackme 0:ea85c4bb5e1f 41 FS_SET_SSID,
dan_ackme 0:ea85c4bb5e1f 42 FS_SET_PASSWORD,
dan_ackme 0:ea85c4bb5e1f 43 FS_NETWORK_UP
dan_ackme 0:ea85c4bb5e1f 44 };
dan_ackme 0:ea85c4bb5e1f 45
dan_ackme 0:ea85c4bb5e1f 46 CHECK_CALLBACK_AVAILABLE(completeHandler_);
dan_ackme 0:ea85c4bb5e1f 47 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 0:ea85c4bb5e1f 48
dan_ackme 0:ea85c4bb5e1f 49 if(wiconnect->internalProcessingState == FS_SET_SSID)
dan_ackme 0:ea85c4bb5e1f 50 {
dan_ackme 0:ea85c4bb5e1f 51 if(ssid == NULL ||
dan_ackme 0:ea85c4bb5e1f 52 WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("set setup.web.ssid %s", ssid)))
dan_ackme 0:ea85c4bb5e1f 53 {
dan_ackme 0:ea85c4bb5e1f 54 wiconnect->internalProcessingState = FS_SET_PASSWORD;
dan_ackme 0:ea85c4bb5e1f 55 }
dan_ackme 0:ea85c4bb5e1f 56 }
dan_ackme 0:ea85c4bb5e1f 57
dan_ackme 0:ea85c4bb5e1f 58 if(wiconnect->internalProcessingState == FS_SET_PASSWORD)
dan_ackme 0:ea85c4bb5e1f 59 {
dan_ackme 0:ea85c4bb5e1f 60 if(password == NULL ||
dan_ackme 0:ea85c4bb5e1f 61 WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("set setup.web.passkey %s", password)))
dan_ackme 0:ea85c4bb5e1f 62 {
dan_ackme 0:ea85c4bb5e1f 63 wiconnect->internalProcessingState = FS_NETWORK_UP;
dan_ackme 0:ea85c4bb5e1f 64 }
dan_ackme 0:ea85c4bb5e1f 65 }
dan_ackme 0:ea85c4bb5e1f 66
dan_ackme 0:ea85c4bb5e1f 67 if(wiconnect->internalProcessingState == FS_NETWORK_UP)
dan_ackme 0:ea85c4bb5e1f 68 {
dan_ackme 0:ea85c4bb5e1f 69 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("setup web")))
dan_ackme 0:ea85c4bb5e1f 70 {
dan_ackme 0:ea85c4bb5e1f 71 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
dan_ackme 0:ea85c4bb5e1f 72 if(completeHandler_.isValid())
dan_ackme 0:ea85c4bb5e1f 73 {
dan_ackme 11:ea484e1b7fc4 74 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
dan_ackme 11:ea484e1b7fc4 75 monitorTimer.stop();
dan_ackme 11:ea484e1b7fc4 76 #endif
dan_ackme 11:ea484e1b7fc4 77 completeHandler = completeHandler_;
dan_ackme 11:ea484e1b7fc4 78 monitorTimer.start(this, &NetworkInterface::webSetupStatusMonitor, 1000);
dan_ackme 0:ea85c4bb5e1f 79 }
dan_ackme 0:ea85c4bb5e1f 80 #endif
dan_ackme 0:ea85c4bb5e1f 81 }
dan_ackme 0:ea85c4bb5e1f 82 }
dan_ackme 0:ea85c4bb5e1f 83
dan_ackme 0:ea85c4bb5e1f 84 CHECK_CLEANUP_COMMAND();
dan_ackme 0:ea85c4bb5e1f 85
dan_ackme 0:ea85c4bb5e1f 86 return result;
dan_ackme 0:ea85c4bb5e1f 87 }
dan_ackme 0:ea85c4bb5e1f 88
dan_ackme 0:ea85c4bb5e1f 89 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 90 WiconnectResult NetworkInterface::stopWebSetup()
dan_ackme 0:ea85c4bb5e1f 91 {
dan_ackme 0:ea85c4bb5e1f 92 WiconnectResult result;
dan_ackme 0:ea85c4bb5e1f 93
dan_ackme 0:ea85c4bb5e1f 94 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 0:ea85c4bb5e1f 95
dan_ackme 0:ea85c4bb5e1f 96 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
dan_ackme 0:ea85c4bb5e1f 97 monitorTimer.stop();
dan_ackme 0:ea85c4bb5e1f 98 #endif
dan_ackme 0:ea85c4bb5e1f 99 result = wiconnect->sendCommand("setup stop");
dan_ackme 0:ea85c4bb5e1f 100
dan_ackme 0:ea85c4bb5e1f 101 CHECK_CLEANUP_COMMAND();
dan_ackme 0:ea85c4bb5e1f 102
dan_ackme 0:ea85c4bb5e1f 103 return result;
dan_ackme 0:ea85c4bb5e1f 104 }
dan_ackme 0:ea85c4bb5e1f 105
dan_ackme 0:ea85c4bb5e1f 106
dan_ackme 0:ea85c4bb5e1f 107 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 108 WiconnectResult NetworkInterface::isWebSetupRunning(bool *isRunningPtr)
dan_ackme 0:ea85c4bb5e1f 109 {
dan_ackme 0:ea85c4bb5e1f 110 WiconnectResult result;
dan_ackme 0:ea85c4bb5e1f 111
dan_ackme 0:ea85c4bb5e1f 112 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 0:ea85c4bb5e1f 113
dan_ackme 0:ea85c4bb5e1f 114 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("setup status")))
dan_ackme 0:ea85c4bb5e1f 115 {
dan_ackme 0:ea85c4bb5e1f 116 int32_t status;
dan_ackme 0:ea85c4bb5e1f 117 if(!WICONNECT_FAILED(result, wiconnect->responseToInt32(&status)))
dan_ackme 0:ea85c4bb5e1f 118 {
dan_ackme 13:2b51f5267c92 119 if(status)
dan_ackme 13:2b51f5267c92 120 {
dan_ackme 13:2b51f5267c92 121 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
dan_ackme 13:2b51f5267c92 122 monitorTimer.stop();
dan_ackme 13:2b51f5267c92 123 #endif
dan_ackme 13:2b51f5267c92 124 }
dan_ackme 0:ea85c4bb5e1f 125 *isRunningPtr = (bool)status;
dan_ackme 0:ea85c4bb5e1f 126 }
dan_ackme 0:ea85c4bb5e1f 127 }
dan_ackme 0:ea85c4bb5e1f 128
dan_ackme 0:ea85c4bb5e1f 129 CHECK_CLEANUP_COMMAND();
dan_ackme 0:ea85c4bb5e1f 130
dan_ackme 0:ea85c4bb5e1f 131 return result;
dan_ackme 0:ea85c4bb5e1f 132 }
dan_ackme 0:ea85c4bb5e1f 133
dan_ackme 0:ea85c4bb5e1f 134
dan_ackme 0:ea85c4bb5e1f 135 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
dan_ackme 0:ea85c4bb5e1f 136
dan_ackme 0:ea85c4bb5e1f 137 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 138 void NetworkInterface::webSetupStatusMonitor()
dan_ackme 0:ea85c4bb5e1f 139 {
dan_ackme 8:1fad4ca6c6a4 140 static char responseBuffer[4];
dan_ackme 8:1fad4ca6c6a4 141 static uint8_t cmdBuffer[sizeof(QueuedCommand)];
dan_ackme 8:1fad4ca6c6a4 142 QueuedCommand *cmd = (QueuedCommand*)cmdBuffer;
dan_ackme 6:8a87a59d0d21 143
dan_ackme 0:ea85c4bb5e1f 144 monitorTimer.stop();
dan_ackme 6:8a87a59d0d21 145
dan_ackme 8:1fad4ca6c6a4 146 *cmd = QueuedCommand(sizeof(responseBuffer), responseBuffer, "setup status");
dan_ackme 6:8a87a59d0d21 147
dan_ackme 6:8a87a59d0d21 148 wiconnect->enqueueCommand(cmd, Callback(this, &NetworkInterface::webSetupStatusCheckCallback));
dan_ackme 0:ea85c4bb5e1f 149 }
dan_ackme 0:ea85c4bb5e1f 150
dan_ackme 0:ea85c4bb5e1f 151 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 152 void NetworkInterface::webSetupStatusCheckCallback(WiconnectResult result, void *arg1, void *arg2)
dan_ackme 0:ea85c4bb5e1f 153 {
dan_ackme 0:ea85c4bb5e1f 154 bool isComplete = true;
dan_ackme 0:ea85c4bb5e1f 155
dan_ackme 0:ea85c4bb5e1f 156 QueuedCommand *cmd = (QueuedCommand*)arg1;
dan_ackme 0:ea85c4bb5e1f 157
dan_ackme 0:ea85c4bb5e1f 158 if(result == WICONNECT_SUCCESS)
dan_ackme 0:ea85c4bb5e1f 159 {
dan_ackme 0:ea85c4bb5e1f 160 int32_t status;
dan_ackme 0:ea85c4bb5e1f 161 if(!StringUtil::strToInt32(cmd->responseBuffer, &status))
dan_ackme 0:ea85c4bb5e1f 162 {
dan_ackme 0:ea85c4bb5e1f 163 result = WICONNECT_RESPONSE_PARSE_ERROR;
dan_ackme 0:ea85c4bb5e1f 164 }
dan_ackme 0:ea85c4bb5e1f 165 else if(status > 0)
dan_ackme 0:ea85c4bb5e1f 166 {
dan_ackme 0:ea85c4bb5e1f 167 isComplete = false;
dan_ackme 0:ea85c4bb5e1f 168 }
dan_ackme 0:ea85c4bb5e1f 169 }
dan_ackme 0:ea85c4bb5e1f 170
dan_ackme 0:ea85c4bb5e1f 171 if(isComplete)
dan_ackme 0:ea85c4bb5e1f 172 {
dan_ackme 0:ea85c4bb5e1f 173 completeHandler.call(result, NULL, NULL);
dan_ackme 0:ea85c4bb5e1f 174 }
dan_ackme 0:ea85c4bb5e1f 175 else
dan_ackme 0:ea85c4bb5e1f 176 {
dan_ackme 0:ea85c4bb5e1f 177 monitorTimer.start(this, &NetworkInterface::webSetupStatusMonitor, 1000);
dan_ackme 0:ea85c4bb5e1f 178 }
dan_ackme 0:ea85c4bb5e1f 179 }
dan_ackme 0:ea85c4bb5e1f 180
dan_ackme 0:ea85c4bb5e1f 181 #endif