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/network/NetworkJoin.cpp@16:7f1d6d359787, 2014-08-13 (annotated)
- Committer:
- dan_ackme
- Date:
- Wed Aug 13 04:41:04 2014 -0700
- Revision:
- 16:7f1d6d359787
- Parent:
- 13:2b51f5267c92
- Child:
- 17:7268f365676b
updated documentation and copyright
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dan_ackme | 16:7f1d6d359787 | 1 | /** |
dan_ackme | 16:7f1d6d359787 | 2 | * ACKme WiConnect Host Library is licensed under the BSD licence: |
dan_ackme | 16:7f1d6d359787 | 3 | * |
dan_ackme | 16:7f1d6d359787 | 4 | * Copyright (c)2014 ACKme Networks. |
dan_ackme | 16:7f1d6d359787 | 5 | * All rights reserved. |
dan_ackme | 16:7f1d6d359787 | 6 | * |
dan_ackme | 16:7f1d6d359787 | 7 | * Redistribution and use in source and binary forms, with or without modification, |
dan_ackme | 16:7f1d6d359787 | 8 | * are permitted provided that the following conditions are met: |
dan_ackme | 16:7f1d6d359787 | 9 | * |
dan_ackme | 16:7f1d6d359787 | 10 | * 1. Redistributions of source code must retain the above copyright notice, |
dan_ackme | 16:7f1d6d359787 | 11 | * this list of conditions and the following disclaimer. |
dan_ackme | 16:7f1d6d359787 | 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
dan_ackme | 16:7f1d6d359787 | 13 | * this list of conditions and the following disclaimer in the documentation |
dan_ackme | 16:7f1d6d359787 | 14 | * and/or other materials provided with the distribution. |
dan_ackme | 16:7f1d6d359787 | 15 | * 3. The name of the author may not be used to endorse or promote products |
dan_ackme | 16:7f1d6d359787 | 16 | * derived from this software without specific prior written permission. |
dan_ackme | 16:7f1d6d359787 | 17 | * |
dan_ackme | 16:7f1d6d359787 | 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED |
dan_ackme | 16:7f1d6d359787 | 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
dan_ackme | 16:7f1d6d359787 | 20 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
dan_ackme | 16:7f1d6d359787 | 21 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
dan_ackme | 16:7f1d6d359787 | 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
dan_ackme | 16:7f1d6d359787 | 23 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
dan_ackme | 16:7f1d6d359787 | 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
dan_ackme | 16:7f1d6d359787 | 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
dan_ackme | 16:7f1d6d359787 | 26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
dan_ackme | 16:7f1d6d359787 | 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 | 0:ea85c4bb5e1f | 41 | FS_NETWORK_UP |
dan_ackme | 0:ea85c4bb5e1f | 42 | }; |
dan_ackme | 0:ea85c4bb5e1f | 43 | |
dan_ackme | 0:ea85c4bb5e1f | 44 | CHECK_CALLBACK_AVAILABLE(completeHandler_); |
dan_ackme | 0:ea85c4bb5e1f | 45 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 0:ea85c4bb5e1f | 46 | |
dan_ackme | 0:ea85c4bb5e1f | 47 | if(wiconnect->internalProcessingState == FS_SET_SSID) |
dan_ackme | 0:ea85c4bb5e1f | 48 | { |
dan_ackme | 0:ea85c4bb5e1f | 49 | if(ssid == NULL || |
dan_ackme | 0:ea85c4bb5e1f | 50 | WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("set wlan.ssid %s", ssid))) |
dan_ackme | 0:ea85c4bb5e1f | 51 | { |
dan_ackme | 0:ea85c4bb5e1f | 52 | wiconnect->internalProcessingState = FS_SET_PASSWORD; |
dan_ackme | 0:ea85c4bb5e1f | 53 | } |
dan_ackme | 0:ea85c4bb5e1f | 54 | } |
dan_ackme | 0:ea85c4bb5e1f | 55 | |
dan_ackme | 0:ea85c4bb5e1f | 56 | if(wiconnect->internalProcessingState == FS_SET_PASSWORD) |
dan_ackme | 0:ea85c4bb5e1f | 57 | { |
dan_ackme | 0:ea85c4bb5e1f | 58 | if(password == NULL || |
dan_ackme | 0:ea85c4bb5e1f | 59 | WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("set wlan.passkey %s", password))) |
dan_ackme | 0:ea85c4bb5e1f | 60 | { |
dan_ackme | 0:ea85c4bb5e1f | 61 | wiconnect->internalProcessingState = FS_NETWORK_UP; |
dan_ackme | 0:ea85c4bb5e1f | 62 | } |
dan_ackme | 0:ea85c4bb5e1f | 63 | } |
dan_ackme | 0:ea85c4bb5e1f | 64 | |
dan_ackme | 0:ea85c4bb5e1f | 65 | if(wiconnect->internalProcessingState == FS_NETWORK_UP) |
dan_ackme | 0:ea85c4bb5e1f | 66 | { |
dan_ackme | 0:ea85c4bb5e1f | 67 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("network_up"))) |
dan_ackme | 0:ea85c4bb5e1f | 68 | { |
dan_ackme | 0:ea85c4bb5e1f | 69 | #ifdef WICONNECT_ASYNC_TIMER_ENABLED |
dan_ackme | 0:ea85c4bb5e1f | 70 | if(completeHandler_.isValid()) |
dan_ackme | 0:ea85c4bb5e1f | 71 | { |
dan_ackme | 11:ea484e1b7fc4 | 72 | #ifdef WICONNECT_ASYNC_TIMER_ENABLED |
dan_ackme | 11:ea484e1b7fc4 | 73 | monitorTimer.stop(); |
dan_ackme | 11:ea484e1b7fc4 | 74 | #endif |
dan_ackme | 11:ea484e1b7fc4 | 75 | completeHandler = completeHandler_; |
dan_ackme | 11:ea484e1b7fc4 | 76 | monitorTimer.start(this, &NetworkInterface::joinStatusMonitor, 1000); |
dan_ackme | 0:ea85c4bb5e1f | 77 | } |
dan_ackme | 0:ea85c4bb5e1f | 78 | #endif |
dan_ackme | 0:ea85c4bb5e1f | 79 | } |
dan_ackme | 0:ea85c4bb5e1f | 80 | } |
dan_ackme | 0:ea85c4bb5e1f | 81 | |
dan_ackme | 0:ea85c4bb5e1f | 82 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 0:ea85c4bb5e1f | 83 | |
dan_ackme | 0:ea85c4bb5e1f | 84 | return result; |
dan_ackme | 0:ea85c4bb5e1f | 85 | } |
dan_ackme | 0:ea85c4bb5e1f | 86 | |
dan_ackme | 0:ea85c4bb5e1f | 87 | |
dan_ackme | 0:ea85c4bb5e1f | 88 | |
dan_ackme | 0:ea85c4bb5e1f | 89 | /*************************************************************************************************/ |
dan_ackme | 0:ea85c4bb5e1f | 90 | WiconnectResult NetworkInterface::leave() |
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("network_down"); |
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::getNetworkStatus(NetworkStatus *statusPtr) |
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("get network.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 != NETWORK_STATUS_DOWN) |
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 | *statusPtr = (NetworkStatus)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 | 7:41d456a65f14 | 137 | |
dan_ackme | 0:ea85c4bb5e1f | 138 | /*************************************************************************************************/ |
dan_ackme | 0:ea85c4bb5e1f | 139 | // this is called every 1s by the monitorTimer |
dan_ackme | 0:ea85c4bb5e1f | 140 | void NetworkInterface::joinStatusMonitor() |
dan_ackme | 0:ea85c4bb5e1f | 141 | { |
dan_ackme | 8:1fad4ca6c6a4 | 142 | static char responseBuffer[4]; |
dan_ackme | 8:1fad4ca6c6a4 | 143 | static uint8_t cmdBuffer[sizeof(QueuedCommand)]; |
dan_ackme | 8:1fad4ca6c6a4 | 144 | QueuedCommand *cmd = (QueuedCommand*)cmdBuffer; |
dan_ackme | 6:8a87a59d0d21 | 145 | |
dan_ackme | 0:ea85c4bb5e1f | 146 | monitorTimer.stop(); |
dan_ackme | 6:8a87a59d0d21 | 147 | |
dan_ackme | 8:1fad4ca6c6a4 | 148 | *cmd = QueuedCommand(sizeof(responseBuffer), responseBuffer, "get network.status"); |
dan_ackme | 6:8a87a59d0d21 | 149 | |
dan_ackme | 6:8a87a59d0d21 | 150 | wiconnect->enqueueCommand(cmd, Callback(this, &NetworkInterface::joinStatusCheckCallback)); |
dan_ackme | 0:ea85c4bb5e1f | 151 | } |
dan_ackme | 0:ea85c4bb5e1f | 152 | |
dan_ackme | 0:ea85c4bb5e1f | 153 | /*************************************************************************************************/ |
dan_ackme | 0:ea85c4bb5e1f | 154 | // this is called on the completion of the 'get'network.status' command above |
dan_ackme | 0:ea85c4bb5e1f | 155 | void NetworkInterface::joinStatusCheckCallback(WiconnectResult result, void *arg1, void *arg2) |
dan_ackme | 0:ea85c4bb5e1f | 156 | { |
dan_ackme | 0:ea85c4bb5e1f | 157 | bool isComplete = true; |
dan_ackme | 0:ea85c4bb5e1f | 158 | |
dan_ackme | 0:ea85c4bb5e1f | 159 | QueuedCommand *cmd = (QueuedCommand*)arg1; |
dan_ackme | 0:ea85c4bb5e1f | 160 | |
dan_ackme | 0:ea85c4bb5e1f | 161 | if(result == WICONNECT_SUCCESS) |
dan_ackme | 0:ea85c4bb5e1f | 162 | { |
dan_ackme | 0:ea85c4bb5e1f | 163 | int32_t status; |
dan_ackme | 0:ea85c4bb5e1f | 164 | if(!StringUtil::strToInt32(cmd->responseBuffer, &status)) |
dan_ackme | 0:ea85c4bb5e1f | 165 | { |
dan_ackme | 0:ea85c4bb5e1f | 166 | result = WICONNECT_RESPONSE_PARSE_ERROR; |
dan_ackme | 0:ea85c4bb5e1f | 167 | } |
dan_ackme | 0:ea85c4bb5e1f | 168 | else if(status == 0) |
dan_ackme | 0:ea85c4bb5e1f | 169 | { |
dan_ackme | 0:ea85c4bb5e1f | 170 | isComplete = false; |
dan_ackme | 0:ea85c4bb5e1f | 171 | } |
dan_ackme | 0:ea85c4bb5e1f | 172 | } |
dan_ackme | 0:ea85c4bb5e1f | 173 | |
dan_ackme | 0:ea85c4bb5e1f | 174 | if(isComplete) |
dan_ackme | 0:ea85c4bb5e1f | 175 | { |
dan_ackme | 0:ea85c4bb5e1f | 176 | completeHandler.call(result, NULL, NULL); |
dan_ackme | 0:ea85c4bb5e1f | 177 | } |
dan_ackme | 0:ea85c4bb5e1f | 178 | else |
dan_ackme | 0:ea85c4bb5e1f | 179 | { |
dan_ackme | 0:ea85c4bb5e1f | 180 | monitorTimer.start(this, &NetworkInterface::joinStatusMonitor, 1000); |
dan_ackme | 0:ea85c4bb5e1f | 181 | } |
dan_ackme | 0:ea85c4bb5e1f | 182 | } |
dan_ackme | 0:ea85c4bb5e1f | 183 | |
dan_ackme | 0:ea85c4bb5e1f | 184 | #endif |