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:
23:8cfc6fdce6b0
Child:
27:b63f5a9cdefa
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 WiconnectResult NetworkInterface::join(const char* ssid, const char *password, const Callback &completeHandler_)
dan_ackme 0:ea85c4bb5e1f 34 {
dan_ackme 2:05e20e184e7e 35 WiconnectResult result = WICONNECT_ERROR;
dan_ackme 0:ea85c4bb5e1f 36
dan_ackme 0:ea85c4bb5e1f 37 enum
dan_ackme 0:ea85c4bb5e1f 38 {
dan_ackme 0:ea85c4bb5e1f 39 FS_SET_SSID,
dan_ackme 0:ea85c4bb5e1f 40 FS_SET_PASSWORD,
dan_ackme 24:e27e23297f02 41 FS_NETWORK_UP,
dan_ackme 17:7268f365676b 42 FS_GET_STATUS
dan_ackme 0:ea85c4bb5e1f 43 };
dan_ackme 0:ea85c4bb5e1f 44
dan_ackme 24:e27e23297f02 45 CHECK_CALLBACK_AVAILABLE(completeHandler_);
dan_ackme 17:7268f365676b 46
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 wlan.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 24:e27e23297f02 60 if(password == NULL || *password == 0 ||
dan_ackme 0:ea85c4bb5e1f 61 WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("set wlan.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("network_up")))
dan_ackme 24:e27e23297f02 70 {
dan_ackme 24:e27e23297f02 71 if(!completeHandler_.isValid())
dan_ackme 24:e27e23297f02 72 {
dan_ackme 24:e27e23297f02 73 wiconnect->internalProcessingState = FS_GET_STATUS;
dan_ackme 17:7268f365676b 74 }
dan_ackme 24:e27e23297f02 75 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
dan_ackme 17:7268f365676b 76 else
dan_ackme 0:ea85c4bb5e1f 77 {
dan_ackme 11:ea484e1b7fc4 78 monitorTimer.stop();
dan_ackme 11:ea484e1b7fc4 79 completeHandler = completeHandler_;
dan_ackme 11:ea484e1b7fc4 80 monitorTimer.start(this, &NetworkInterface::joinStatusMonitor, 1000);
dan_ackme 0:ea85c4bb5e1f 81 }
dan_ackme 0:ea85c4bb5e1f 82 #endif
dan_ackme 0:ea85c4bb5e1f 83 }
dan_ackme 24:e27e23297f02 84 }
dan_ackme 24:e27e23297f02 85
dan_ackme 24:e27e23297f02 86 if(wiconnect->internalProcessingState == FS_GET_STATUS)
dan_ackme 24:e27e23297f02 87 {
dan_ackme 24:e27e23297f02 88 #define MAX_JOIN_TIME 30000
dan_ackme 24:e27e23297f02 89 TimeoutTimer timeout;
dan_ackme 24:e27e23297f02 90
dan_ackme 24:e27e23297f02 91 status_loop:
dan_ackme 24:e27e23297f02 92 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("get network.status")))
dan_ackme 24:e27e23297f02 93 {
dan_ackme 24:e27e23297f02 94 int32_t status;
dan_ackme 24:e27e23297f02 95 if(!WICONNECT_FAILED(result, wiconnect->responseToInt32(&status)))
dan_ackme 24:e27e23297f02 96 {
dan_ackme 24:e27e23297f02 97 if(status == NETWORK_STATUS_DOWN)
dan_ackme 24:e27e23297f02 98 {
dan_ackme 24:e27e23297f02 99 if(timeout.timedOut(MAX_JOIN_TIME))
dan_ackme 24:e27e23297f02 100 {
dan_ackme 24:e27e23297f02 101 result = WICONNECT_TIMEOUT;
dan_ackme 24:e27e23297f02 102 }
dan_ackme 24:e27e23297f02 103 else if(!wiconnect->nonBlocking)
dan_ackme 24:e27e23297f02 104 {
dan_ackme 24:e27e23297f02 105 goto status_loop;
dan_ackme 24:e27e23297f02 106 }
dan_ackme 24:e27e23297f02 107 else
dan_ackme 24:e27e23297f02 108 {
dan_ackme 24:e27e23297f02 109 result = WICONNECT_PROCESSING;
dan_ackme 24:e27e23297f02 110 }
dan_ackme 24:e27e23297f02 111 }
dan_ackme 24:e27e23297f02 112 }
dan_ackme 24:e27e23297f02 113 }
dan_ackme 0:ea85c4bb5e1f 114 }
dan_ackme 0:ea85c4bb5e1f 115
dan_ackme 0:ea85c4bb5e1f 116 CHECK_CLEANUP_COMMAND();
dan_ackme 0:ea85c4bb5e1f 117
dan_ackme 0:ea85c4bb5e1f 118 return result;
dan_ackme 0:ea85c4bb5e1f 119 }
dan_ackme 0:ea85c4bb5e1f 120
dan_ackme 0:ea85c4bb5e1f 121
dan_ackme 0:ea85c4bb5e1f 122
dan_ackme 0:ea85c4bb5e1f 123 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 124 WiconnectResult NetworkInterface::leave()
dan_ackme 0:ea85c4bb5e1f 125 {
dan_ackme 0:ea85c4bb5e1f 126 WiconnectResult result;
dan_ackme 0:ea85c4bb5e1f 127
dan_ackme 0:ea85c4bb5e1f 128 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 0:ea85c4bb5e1f 129
dan_ackme 0:ea85c4bb5e1f 130 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
dan_ackme 0:ea85c4bb5e1f 131 monitorTimer.stop();
dan_ackme 0:ea85c4bb5e1f 132 #endif
dan_ackme 0:ea85c4bb5e1f 133 result = wiconnect->sendCommand("network_down");
dan_ackme 0:ea85c4bb5e1f 134
dan_ackme 0:ea85c4bb5e1f 135 CHECK_CLEANUP_COMMAND();
dan_ackme 0:ea85c4bb5e1f 136
dan_ackme 0:ea85c4bb5e1f 137 return result;
dan_ackme 0:ea85c4bb5e1f 138 }
dan_ackme 0:ea85c4bb5e1f 139
dan_ackme 0:ea85c4bb5e1f 140
dan_ackme 0:ea85c4bb5e1f 141 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 142 WiconnectResult NetworkInterface::getNetworkStatus(NetworkStatus *statusPtr)
dan_ackme 0:ea85c4bb5e1f 143 {
dan_ackme 0:ea85c4bb5e1f 144 WiconnectResult result;
dan_ackme 0:ea85c4bb5e1f 145
dan_ackme 0:ea85c4bb5e1f 146 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 0:ea85c4bb5e1f 147
dan_ackme 0:ea85c4bb5e1f 148 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("get network.status")))
dan_ackme 0:ea85c4bb5e1f 149 {
dan_ackme 0:ea85c4bb5e1f 150 int32_t status;
dan_ackme 0:ea85c4bb5e1f 151 if(!WICONNECT_FAILED(result, wiconnect->responseToInt32(&status)))
dan_ackme 0:ea85c4bb5e1f 152 {
dan_ackme 13:2b51f5267c92 153 if(status != NETWORK_STATUS_DOWN)
dan_ackme 13:2b51f5267c92 154 {
dan_ackme 13:2b51f5267c92 155 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
dan_ackme 13:2b51f5267c92 156 monitorTimer.stop();
dan_ackme 13:2b51f5267c92 157 #endif
dan_ackme 13:2b51f5267c92 158 }
dan_ackme 0:ea85c4bb5e1f 159 *statusPtr = (NetworkStatus)status;
dan_ackme 0:ea85c4bb5e1f 160 }
dan_ackme 0:ea85c4bb5e1f 161 }
dan_ackme 0:ea85c4bb5e1f 162
dan_ackme 0:ea85c4bb5e1f 163 CHECK_CLEANUP_COMMAND();
dan_ackme 0:ea85c4bb5e1f 164
dan_ackme 0:ea85c4bb5e1f 165 return result;
dan_ackme 0:ea85c4bb5e1f 166 }
dan_ackme 0:ea85c4bb5e1f 167
dan_ackme 0:ea85c4bb5e1f 168
dan_ackme 0:ea85c4bb5e1f 169 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
dan_ackme 0:ea85c4bb5e1f 170
dan_ackme 7:41d456a65f14 171
dan_ackme 0:ea85c4bb5e1f 172 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 173 // this is called every 1s by the monitorTimer
dan_ackme 0:ea85c4bb5e1f 174 void NetworkInterface::joinStatusMonitor()
dan_ackme 0:ea85c4bb5e1f 175 {
dan_ackme 8:1fad4ca6c6a4 176 static char responseBuffer[4];
dan_ackme 8:1fad4ca6c6a4 177 static uint8_t cmdBuffer[sizeof(QueuedCommand)];
dan_ackme 8:1fad4ca6c6a4 178 QueuedCommand *cmd = (QueuedCommand*)cmdBuffer;
dan_ackme 6:8a87a59d0d21 179
dan_ackme 0:ea85c4bb5e1f 180 monitorTimer.stop();
dan_ackme 6:8a87a59d0d21 181
dan_ackme 8:1fad4ca6c6a4 182 *cmd = QueuedCommand(sizeof(responseBuffer), responseBuffer, "get network.status");
dan_ackme 6:8a87a59d0d21 183
dan_ackme 6:8a87a59d0d21 184 wiconnect->enqueueCommand(cmd, Callback(this, &NetworkInterface::joinStatusCheckCallback));
dan_ackme 0:ea85c4bb5e1f 185 }
dan_ackme 0:ea85c4bb5e1f 186
dan_ackme 0:ea85c4bb5e1f 187 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 188 // this is called on the completion of the 'get'network.status' command above
dan_ackme 0:ea85c4bb5e1f 189 void NetworkInterface::joinStatusCheckCallback(WiconnectResult result, void *arg1, void *arg2)
dan_ackme 0:ea85c4bb5e1f 190 {
dan_ackme 0:ea85c4bb5e1f 191 bool isComplete = true;
dan_ackme 0:ea85c4bb5e1f 192
dan_ackme 0:ea85c4bb5e1f 193 QueuedCommand *cmd = (QueuedCommand*)arg1;
dan_ackme 0:ea85c4bb5e1f 194
dan_ackme 0:ea85c4bb5e1f 195 if(result == WICONNECT_SUCCESS)
dan_ackme 0:ea85c4bb5e1f 196 {
dan_ackme 0:ea85c4bb5e1f 197 int32_t status;
dan_ackme 0:ea85c4bb5e1f 198 if(!StringUtil::strToInt32(cmd->responseBuffer, &status))
dan_ackme 0:ea85c4bb5e1f 199 {
dan_ackme 0:ea85c4bb5e1f 200 result = WICONNECT_RESPONSE_PARSE_ERROR;
dan_ackme 0:ea85c4bb5e1f 201 }
dan_ackme 0:ea85c4bb5e1f 202 else if(status == 0)
dan_ackme 0:ea85c4bb5e1f 203 {
dan_ackme 0:ea85c4bb5e1f 204 isComplete = false;
dan_ackme 0:ea85c4bb5e1f 205 }
dan_ackme 0:ea85c4bb5e1f 206 }
dan_ackme 0:ea85c4bb5e1f 207
dan_ackme 0:ea85c4bb5e1f 208 if(isComplete)
dan_ackme 0:ea85c4bb5e1f 209 {
dan_ackme 0:ea85c4bb5e1f 210 completeHandler.call(result, NULL, NULL);
dan_ackme 0:ea85c4bb5e1f 211 }
dan_ackme 0:ea85c4bb5e1f 212 else
dan_ackme 0:ea85c4bb5e1f 213 {
dan_ackme 0:ea85c4bb5e1f 214 monitorTimer.start(this, &NetworkInterface::joinStatusMonitor, 1000);
dan_ackme 0:ea85c4bb5e1f 215 }
dan_ackme 0:ea85c4bb5e1f 216 }
dan_ackme 0:ea85c4bb5e1f 217
dan_ackme 0:ea85c4bb5e1f 218 #endif