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:
aymangrais
Date:
Mon Sep 28 03:38:43 2015 +0000
Revision:
42:8ffb253b09e7
Parent:
29:b6af04b77a56
Child:
40:4b4306f3d829
increase ota timeout to be 5 seconds (instead of 1.5 sec)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dan_ackme 29:b6af04b77a56 1 /**
dan_ackme 29:b6af04b77a56 2 * ACKme WiConnect Host Library is licensed under the BSD licence:
dan_ackme 29:b6af04b77a56 3 *
dan_ackme 29:b6af04b77a56 4 * Copyright (c)2014 ACKme Networks.
dan_ackme 29:b6af04b77a56 5 * All rights reserved.
dan_ackme 29:b6af04b77a56 6 *
dan_ackme 29:b6af04b77a56 7 * Redistribution and use in source and binary forms, with or without modification,
dan_ackme 29:b6af04b77a56 8 * are permitted provided that the following conditions are met:
dan_ackme 29:b6af04b77a56 9 *
dan_ackme 29:b6af04b77a56 10 * 1. Redistributions of source code must retain the above copyright notice,
dan_ackme 29:b6af04b77a56 11 * this list of conditions and the following disclaimer.
dan_ackme 29:b6af04b77a56 12 * 2. Redistributions in binary form must reproduce the above copyright notice,
dan_ackme 29:b6af04b77a56 13 * this list of conditions and the following disclaimer in the documentation
dan_ackme 29:b6af04b77a56 14 * and/or other materials provided with the distribution.
dan_ackme 29:b6af04b77a56 15 * 3. The name of the author may not be used to endorse or promote products
dan_ackme 29:b6af04b77a56 16 * derived from this software without specific prior written permission.
dan_ackme 29:b6af04b77a56 17 *
dan_ackme 29:b6af04b77a56 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED
dan_ackme 29:b6af04b77a56 19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
dan_ackme 29:b6af04b77a56 20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
dan_ackme 29:b6af04b77a56 21 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
dan_ackme 29:b6af04b77a56 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
dan_ackme 29:b6af04b77a56 23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dan_ackme 29:b6af04b77a56 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dan_ackme 29:b6af04b77a56 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
dan_ackme 29:b6af04b77a56 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
dan_ackme 29:b6af04b77a56 27 * OF SUCH DAMAGE.
dan_ackme 29:b6af04b77a56 28 */
dan_ackme 29:b6af04b77a56 29 #include <stdarg.h>
dan_ackme 29:b6af04b77a56 30 #include "Wiconnect.h"
dan_ackme 29:b6af04b77a56 31 #include "internal/common.h"
dan_ackme 29:b6af04b77a56 32 #include "api/StringUtil.h"
dan_ackme 29:b6af04b77a56 33
dan_ackme 29:b6af04b77a56 34
dan_ackme 29:b6af04b77a56 35 #define CHECK_CONNECTED() if(!isConnected()) return WICONNECT_NOT_CONNECTED
dan_ackme 29:b6af04b77a56 36
dan_ackme 29:b6af04b77a56 37
dan_ackme 29:b6af04b77a56 38 WiconnectResult readerCallback(void *user, void *data, int maxReadSize, int *bytesRead);
dan_ackme 29:b6af04b77a56 39
dan_ackme 29:b6af04b77a56 40
dan_ackme 29:b6af04b77a56 41
dan_ackme 29:b6af04b77a56 42 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 43 WiconnectSocket::WiconnectSocket(int rxBufferLen_, void *rxBuffer_, int txBufferLen_, void *txBuffer_)
dan_ackme 29:b6af04b77a56 44 {
dan_ackme 29:b6af04b77a56 45 wiconnect = Wiconnect::getInstance();
dan_ackme 29:b6af04b77a56 46
dan_ackme 29:b6af04b77a56 47 memset((void*)&txBuffer, 0, sizeof(Buffer));
dan_ackme 29:b6af04b77a56 48 memset((void*)&rxBuffer, 0, sizeof(Buffer));
dan_ackme 29:b6af04b77a56 49
dan_ackme 29:b6af04b77a56 50 txBuffer.size = txBufferLen_;
dan_ackme 29:b6af04b77a56 51 txBuffer.buffer = (uint8_t*)txBuffer_;
dan_ackme 29:b6af04b77a56 52
dan_ackme 29:b6af04b77a56 53 rxBuffer.size = rxBufferLen_;
dan_ackme 29:b6af04b77a56 54 rxBuffer.buffer = (uint8_t*)rxBuffer_;
dan_ackme 29:b6af04b77a56 55
dan_ackme 29:b6af04b77a56 56 if(txBuffer.size > 0)
dan_ackme 29:b6af04b77a56 57 {
dan_ackme 29:b6af04b77a56 58 if(txBuffer_ == NULL)
dan_ackme 29:b6af04b77a56 59 {
dan_ackme 29:b6af04b77a56 60 #ifdef WICONNECT_ENABLE_MALLOC
dan_ackme 29:b6af04b77a56 61 wiconnect_assert(wiconnect, "Socket(), malloc not defined", wiconnect->_malloc != NULL);
dan_ackme 29:b6af04b77a56 62 txBuffer.buffer = (uint8_t*)wiconnect->_malloc(txBufferLen_);
dan_ackme 29:b6af04b77a56 63 wiconnect_assert(wiconnect, "Socket(), txBuffer malloc failed", txBuffer.buffer != NULL);
dan_ackme 29:b6af04b77a56 64 txBuffer.allocated = true;
dan_ackme 29:b6af04b77a56 65 #else
dan_ackme 29:b6af04b77a56 66 wiconnect_assert(wiconnect, "must specify buffer", 0);
dan_ackme 29:b6af04b77a56 67 #endif
dan_ackme 29:b6af04b77a56 68 }
dan_ackme 29:b6af04b77a56 69 txBuffer.size -= 4;
dan_ackme 29:b6af04b77a56 70 }
dan_ackme 29:b6af04b77a56 71
dan_ackme 29:b6af04b77a56 72 if(rxBuffer.size > 0)
dan_ackme 29:b6af04b77a56 73 {
dan_ackme 29:b6af04b77a56 74 if(rxBuffer_ == NULL)
dan_ackme 29:b6af04b77a56 75 {
dan_ackme 29:b6af04b77a56 76 #ifdef WICONNECT_ENABLE_MALLOC
dan_ackme 29:b6af04b77a56 77 wiconnect_assert(wiconnect, "Socket(), malloc not defined", wiconnect->_malloc != NULL);
dan_ackme 29:b6af04b77a56 78 rxBuffer.buffer = (uint8_t*)wiconnect->_malloc(rxBufferLen_);
dan_ackme 29:b6af04b77a56 79 wiconnect_assert(wiconnect, "Socket(), rxBuffer malloc failed", rxBuffer.buffer != NULL);
dan_ackme 29:b6af04b77a56 80 rxBuffer.allocated = true;
dan_ackme 29:b6af04b77a56 81 #else
dan_ackme 29:b6af04b77a56 82 wiconnect_assert(wiconnect, "must specify buffer", 0);
dan_ackme 29:b6af04b77a56 83 #endif
dan_ackme 29:b6af04b77a56 84 }
dan_ackme 29:b6af04b77a56 85 rxBuffer.size -= 4;
dan_ackme 29:b6af04b77a56 86 }
dan_ackme 29:b6af04b77a56 87
dan_ackme 29:b6af04b77a56 88 handle = SOCKET_INVALID_HANDLE;
dan_ackme 29:b6af04b77a56 89 type = SOCKET_TYPE_UNKNOWN;
dan_ackme 29:b6af04b77a56 90 remotePort = 0;
dan_ackme 29:b6af04b77a56 91 localPort = 0;
dan_ackme 29:b6af04b77a56 92 connected = false;
dan_ackme 29:b6af04b77a56 93 enableAutoClose = false;
dan_ackme 29:b6af04b77a56 94 host[0] = 0;
dan_ackme 29:b6af04b77a56 95 }
dan_ackme 29:b6af04b77a56 96
dan_ackme 29:b6af04b77a56 97
dan_ackme 29:b6af04b77a56 98 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 99 WiconnectResult WiconnectSocket::init(uint8_t handle_, SocketType type_, const char *host_, uint16_t remotePort_, uint16_t localPort_)
dan_ackme 29:b6af04b77a56 100 {
dan_ackme 29:b6af04b77a56 101 handle = handle_;
dan_ackme 29:b6af04b77a56 102 type = type_;
dan_ackme 29:b6af04b77a56 103 remotePort = remotePort_;
dan_ackme 29:b6af04b77a56 104 localPort = localPort_;
dan_ackme 29:b6af04b77a56 105 connected = true;
dan_ackme 29:b6af04b77a56 106 enableAutoClose = false;
dan_ackme 29:b6af04b77a56 107
dan_ackme 29:b6af04b77a56 108 rxBuffer.ptr = rxBuffer.buffer;
dan_ackme 29:b6af04b77a56 109 rxBuffer.bytesPending = 0;
dan_ackme 29:b6af04b77a56 110 txBuffer.ptr = txBuffer.buffer;
dan_ackme 29:b6af04b77a56 111 txBuffer.bytesPending = 0;
dan_ackme 29:b6af04b77a56 112
dan_ackme 29:b6af04b77a56 113 strncpy(host, host_, sizeof(host)-1);
dan_ackme 29:b6af04b77a56 114
dan_ackme 29:b6af04b77a56 115 return WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 116 }
dan_ackme 29:b6af04b77a56 117
dan_ackme 29:b6af04b77a56 118 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 119 WiconnectSocket::~WiconnectSocket()
dan_ackme 29:b6af04b77a56 120 {
dan_ackme 29:b6af04b77a56 121 while((handle != SOCKET_INVALID_HANDLE) && (close() == WICONNECT_PROCESSING))
dan_ackme 29:b6af04b77a56 122 {
dan_ackme 29:b6af04b77a56 123 }
dan_ackme 29:b6af04b77a56 124
dan_ackme 29:b6af04b77a56 125 #ifdef WICONNECT_ENABLE_MALLOC
dan_ackme 29:b6af04b77a56 126 if(txBuffer.allocated && txBuffer.size > 0)
dan_ackme 29:b6af04b77a56 127 {
dan_ackme 29:b6af04b77a56 128 wiconnect_assert(wiconnect, "~Socket(), free not defined", wiconnect->_free != NULL);
dan_ackme 29:b6af04b77a56 129 wiconnect->_free(txBuffer.buffer);
dan_ackme 29:b6af04b77a56 130 }
dan_ackme 29:b6af04b77a56 131 if(rxBuffer.allocated && rxBuffer.size > 0)
dan_ackme 29:b6af04b77a56 132 {
dan_ackme 29:b6af04b77a56 133 wiconnect_assert(wiconnect, "~Socket(), free not defined", wiconnect->_free != NULL);
dan_ackme 29:b6af04b77a56 134 wiconnect->_free(rxBuffer.buffer);
dan_ackme 29:b6af04b77a56 135 }
dan_ackme 29:b6af04b77a56 136 #endif
dan_ackme 29:b6af04b77a56 137 }
dan_ackme 29:b6af04b77a56 138
dan_ackme 29:b6af04b77a56 139 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 140 bool WiconnectSocket::isConnected()
dan_ackme 29:b6af04b77a56 141 {
dan_ackme 29:b6af04b77a56 142 return connected;
dan_ackme 29:b6af04b77a56 143 }
dan_ackme 29:b6af04b77a56 144
dan_ackme 29:b6af04b77a56 145 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 146 SocketType WiconnectSocket::getType()
dan_ackme 29:b6af04b77a56 147 {
dan_ackme 29:b6af04b77a56 148 return type;
dan_ackme 29:b6af04b77a56 149 }
dan_ackme 29:b6af04b77a56 150
dan_ackme 29:b6af04b77a56 151 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 152 const char* WiconnectSocket::getHost()
dan_ackme 29:b6af04b77a56 153 {
dan_ackme 29:b6af04b77a56 154 return host;
dan_ackme 29:b6af04b77a56 155 }
dan_ackme 29:b6af04b77a56 156
dan_ackme 29:b6af04b77a56 157 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 158 uint16_t WiconnectSocket::getLocalPort()
dan_ackme 29:b6af04b77a56 159 {
dan_ackme 29:b6af04b77a56 160 return localPort;
dan_ackme 29:b6af04b77a56 161 }
dan_ackme 29:b6af04b77a56 162
dan_ackme 29:b6af04b77a56 163 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 164 uint16_t WiconnectSocket::getRemotePort()
dan_ackme 29:b6af04b77a56 165 {
dan_ackme 29:b6af04b77a56 166 return remotePort;
dan_ackme 29:b6af04b77a56 167 }
dan_ackme 29:b6af04b77a56 168
dan_ackme 29:b6af04b77a56 169 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 170 uint8_t WiconnectSocket::getHandle()
dan_ackme 29:b6af04b77a56 171 {
dan_ackme 29:b6af04b77a56 172 return handle;
dan_ackme 29:b6af04b77a56 173 }
dan_ackme 29:b6af04b77a56 174
dan_ackme 29:b6af04b77a56 175
dan_ackme 29:b6af04b77a56 176 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 177 WiconnectResult WiconnectSocket::close()
dan_ackme 29:b6af04b77a56 178 {
dan_ackme 29:b6af04b77a56 179 WiconnectResult result;
dan_ackme 29:b6af04b77a56 180 CHECK_CONNECTED();
dan_ackme 29:b6af04b77a56 181 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 29:b6af04b77a56 182
dan_ackme 29:b6af04b77a56 183 result = wiconnect->sendCommand("close %d", handle);
dan_ackme 29:b6af04b77a56 184
dan_ackme 29:b6af04b77a56 185 if(result != WICONNECT_PROCESSING)
dan_ackme 29:b6af04b77a56 186 {
dan_ackme 29:b6af04b77a56 187 connected = false;
dan_ackme 29:b6af04b77a56 188 wiconnect->socketClosedCallback(this);
dan_ackme 29:b6af04b77a56 189 }
dan_ackme 29:b6af04b77a56 190
dan_ackme 29:b6af04b77a56 191 CHECK_CLEANUP_COMMAND();
dan_ackme 29:b6af04b77a56 192
dan_ackme 29:b6af04b77a56 193 return result;
dan_ackme 29:b6af04b77a56 194 }
dan_ackme 29:b6af04b77a56 195
dan_ackme 29:b6af04b77a56 196 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 197 WiconnectResult WiconnectSocket::poll(bool *rxDataAvailablePtr, bool autoClose)
dan_ackme 29:b6af04b77a56 198 {
dan_ackme 29:b6af04b77a56 199 WiconnectResult result;
dan_ackme 29:b6af04b77a56 200 int32_t status;
dan_ackme 29:b6af04b77a56 201 bool autoClosed = false;
dan_ackme 29:b6af04b77a56 202
dan_ackme 29:b6af04b77a56 203 *rxDataAvailablePtr = false;
dan_ackme 29:b6af04b77a56 204
dan_ackme 29:b6af04b77a56 205 CHECK_CONNECTED();
dan_ackme 29:b6af04b77a56 206
dan_ackme 29:b6af04b77a56 207 if(rxBuffer.size > 0 && WICONNECT_IS_IDLE())
dan_ackme 29:b6af04b77a56 208 {
dan_ackme 29:b6af04b77a56 209 if(rxBuffer.ptr < &rxBuffer.buffer[rxBuffer.bytesPending])
dan_ackme 29:b6af04b77a56 210 {
dan_ackme 29:b6af04b77a56 211 *rxDataAvailablePtr = true;
dan_ackme 29:b6af04b77a56 212 return WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 213 }
dan_ackme 29:b6af04b77a56 214 else if(read() == WICONNECT_SUCCESS && (rxBuffer.ptr < &rxBuffer.buffer[rxBuffer.bytesPending]))
dan_ackme 29:b6af04b77a56 215 {
dan_ackme 29:b6af04b77a56 216 *rxDataAvailablePtr = true;
dan_ackme 29:b6af04b77a56 217 return WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 218 }
dan_ackme 29:b6af04b77a56 219 }
dan_ackme 29:b6af04b77a56 220
dan_ackme 29:b6af04b77a56 221 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 29:b6af04b77a56 222
dan_ackme 29:b6af04b77a56 223 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("poll %d", handle)))
dan_ackme 29:b6af04b77a56 224 {
dan_ackme 29:b6af04b77a56 225 if(!WICONNECT_FAILED(result, wiconnect->responseToInt32(&status)))
dan_ackme 29:b6af04b77a56 226 {
dan_ackme 29:b6af04b77a56 227 if(status > 0)
dan_ackme 29:b6af04b77a56 228 {
dan_ackme 29:b6af04b77a56 229 if(status == 2 && enableAutoClose)
dan_ackme 29:b6af04b77a56 230 {
dan_ackme 29:b6af04b77a56 231 autoClosed = autoClose;
dan_ackme 29:b6af04b77a56 232 *rxDataAvailablePtr = false;
dan_ackme 29:b6af04b77a56 233 }
dan_ackme 29:b6af04b77a56 234 else
dan_ackme 29:b6af04b77a56 235 {
dan_ackme 29:b6af04b77a56 236 *rxDataAvailablePtr = true;
dan_ackme 29:b6af04b77a56 237 }
dan_ackme 29:b6af04b77a56 238 }
dan_ackme 29:b6af04b77a56 239 }
dan_ackme 29:b6af04b77a56 240 }
dan_ackme 29:b6af04b77a56 241
dan_ackme 29:b6af04b77a56 242 CHECK_CLEANUP_COMMAND();
dan_ackme 29:b6af04b77a56 243
dan_ackme 29:b6af04b77a56 244 // if we auto-closed, then block until everything is cleaned-up
dan_ackme 29:b6af04b77a56 245 if(autoClosed)
dan_ackme 29:b6af04b77a56 246 {
dan_ackme 29:b6af04b77a56 247 while(WICONNECT_IS_PROCESSING(result, close()))
dan_ackme 29:b6af04b77a56 248 {
dan_ackme 29:b6af04b77a56 249 }
dan_ackme 29:b6af04b77a56 250 }
dan_ackme 29:b6af04b77a56 251
dan_ackme 29:b6af04b77a56 252 return result;
dan_ackme 29:b6af04b77a56 253 }
dan_ackme 29:b6af04b77a56 254
dan_ackme 29:b6af04b77a56 255 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 256 WiconnectResult WiconnectSocket::write(int length, bool flush)
dan_ackme 29:b6af04b77a56 257 {
dan_ackme 29:b6af04b77a56 258 CHECK_CONNECTED();
dan_ackme 29:b6af04b77a56 259
dan_ackme 29:b6af04b77a56 260 if( txBuffer.size == 0)
dan_ackme 29:b6af04b77a56 261 {
dan_ackme 29:b6af04b77a56 262 return WICONNECT_UNSUPPORTED;
dan_ackme 29:b6af04b77a56 263 }
dan_ackme 29:b6af04b77a56 264 else if(length > txBuffer.size)
dan_ackme 29:b6af04b77a56 265 {
dan_ackme 29:b6af04b77a56 266 return WICONNECT_OVERFLOW;
dan_ackme 29:b6af04b77a56 267 }
dan_ackme 29:b6af04b77a56 268 txBuffer.bytesPending = length;
dan_ackme 29:b6af04b77a56 269
dan_ackme 29:b6af04b77a56 270 return flush ? flushTxBuffer() : WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 271 }
dan_ackme 29:b6af04b77a56 272
dan_ackme 29:b6af04b77a56 273 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 274 WiconnectResult WiconnectSocket::write(const void* buffer, int length, bool flush)
dan_ackme 29:b6af04b77a56 275 {
dan_ackme 29:b6af04b77a56 276 WiconnectResult result = WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 277 CHECK_CONNECTED();
dan_ackme 29:b6af04b77a56 278
dan_ackme 29:b6af04b77a56 279 if(txBuffer.size > 0)
dan_ackme 29:b6af04b77a56 280 {
dan_ackme 29:b6af04b77a56 281 const uint8_t *src = (const uint8_t *)buffer;
dan_ackme 29:b6af04b77a56 282
dan_ackme 29:b6af04b77a56 283 while(length > 0)
dan_ackme 29:b6af04b77a56 284 {
dan_ackme 29:b6af04b77a56 285 int bytesToWrite = MIN(length, txBuffer.size - txBuffer.bytesPending);
dan_ackme 29:b6af04b77a56 286 uint8_t *dst = (uint8_t*)&txBuffer.buffer[txBuffer.bytesPending];
dan_ackme 29:b6af04b77a56 287 memcpy(dst, src, bytesToWrite);
dan_ackme 29:b6af04b77a56 288 txBuffer.bytesPending += bytesToWrite;
dan_ackme 29:b6af04b77a56 289 length -= bytesToWrite;
dan_ackme 29:b6af04b77a56 290 src += bytesToWrite;
dan_ackme 29:b6af04b77a56 291
dan_ackme 29:b6af04b77a56 292 if((txBuffer.bytesPending >= txBuffer.size) &&
dan_ackme 29:b6af04b77a56 293 WICONNECT_FAILED(result, flushTxBuffer()))
dan_ackme 29:b6af04b77a56 294 {
dan_ackme 29:b6af04b77a56 295 break;
dan_ackme 29:b6af04b77a56 296 }
dan_ackme 29:b6af04b77a56 297 }
dan_ackme 29:b6af04b77a56 298
dan_ackme 29:b6af04b77a56 299 if(flush && txBuffer.bytesPending > 0)
dan_ackme 29:b6af04b77a56 300 {
dan_ackme 29:b6af04b77a56 301 result = flushTxBuffer();
dan_ackme 29:b6af04b77a56 302 }
dan_ackme 29:b6af04b77a56 303 }
dan_ackme 29:b6af04b77a56 304 else
dan_ackme 29:b6af04b77a56 305 {
dan_ackme 29:b6af04b77a56 306 if(WICONNECT_IS_IDLE())
dan_ackme 29:b6af04b77a56 307 {
dan_ackme 29:b6af04b77a56 308 txBuffer.ptr = (uint8_t*)buffer;
dan_ackme 29:b6af04b77a56 309 txBuffer.bytesPending = length;
dan_ackme 29:b6af04b77a56 310 }
dan_ackme 29:b6af04b77a56 311
dan_ackme 29:b6af04b77a56 312 result = flushTxBuffer();
dan_ackme 29:b6af04b77a56 313 }
dan_ackme 29:b6af04b77a56 314
dan_ackme 29:b6af04b77a56 315
dan_ackme 29:b6af04b77a56 316 return result;
dan_ackme 29:b6af04b77a56 317 }
dan_ackme 29:b6af04b77a56 318
dan_ackme 29:b6af04b77a56 319 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 320 WiconnectResult WiconnectSocket::read(void* buffer, uint16_t maxLength, uint16_t *bytesReadPtr)
dan_ackme 29:b6af04b77a56 321 {
dan_ackme 29:b6af04b77a56 322 WiconnectResult result;
dan_ackme 29:b6af04b77a56 323
dan_ackme 29:b6af04b77a56 324 CHECK_CONNECTED();
dan_ackme 29:b6af04b77a56 325
dan_ackme 29:b6af04b77a56 326 if(rxBuffer.size > 0)
dan_ackme 29:b6af04b77a56 327 {
dan_ackme 29:b6af04b77a56 328 uint16_t bytesToRead = 0;
dan_ackme 29:b6af04b77a56 329 const uint16_t bufferedBytes = (&rxBuffer.buffer[rxBuffer.bytesPending] - rxBuffer.ptr);
dan_ackme 29:b6af04b77a56 330 if(bufferedBytes > 0)
dan_ackme 29:b6af04b77a56 331 {
dan_ackme 29:b6af04b77a56 332 bytesToRead = MIN(bufferedBytes, maxLength);
dan_ackme 29:b6af04b77a56 333 memcpy(buffer, rxBuffer.ptr, bytesToRead);
dan_ackme 29:b6af04b77a56 334 rxBuffer.ptr += bytesToRead;
dan_ackme 29:b6af04b77a56 335 *bytesReadPtr = bytesToRead;
dan_ackme 29:b6af04b77a56 336 }
dan_ackme 29:b6af04b77a56 337 if(rxBuffer.ptr >= &rxBuffer.buffer[rxBuffer.bytesPending])
dan_ackme 29:b6af04b77a56 338 {
dan_ackme 29:b6af04b77a56 339 clearRxBuffer();
dan_ackme 29:b6af04b77a56 340 }
dan_ackme 29:b6af04b77a56 341 if(bytesToRead > 0)
dan_ackme 29:b6af04b77a56 342 {
dan_ackme 29:b6af04b77a56 343 return WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 344 }
dan_ackme 29:b6af04b77a56 345 }
dan_ackme 29:b6af04b77a56 346
dan_ackme 29:b6af04b77a56 347 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 29:b6af04b77a56 348
dan_ackme 29:b6af04b77a56 349 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand((char*)buffer, maxLength, "read %d %d", handle, maxLength)))
dan_ackme 29:b6af04b77a56 350 {
dan_ackme 29:b6af04b77a56 351 const uint16_t bytesRead = wiconnect->getLastCommandResponseLength();
dan_ackme 29:b6af04b77a56 352 enableAutoClose = (bytesRead == 0);
dan_ackme 29:b6af04b77a56 353 *bytesReadPtr = bytesRead;
dan_ackme 29:b6af04b77a56 354 }
dan_ackme 29:b6af04b77a56 355
dan_ackme 29:b6af04b77a56 356 CHECK_CLEANUP_COMMAND();
dan_ackme 29:b6af04b77a56 357
dan_ackme 29:b6af04b77a56 358 return result;
dan_ackme 29:b6af04b77a56 359 }
dan_ackme 29:b6af04b77a56 360
dan_ackme 29:b6af04b77a56 361 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 362 WiconnectResult WiconnectSocket::read(uint8_t **bufferPtr, uint16_t *bytesReadPtr)
dan_ackme 29:b6af04b77a56 363 {
dan_ackme 29:b6af04b77a56 364 WiconnectResult result = WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 365
dan_ackme 29:b6af04b77a56 366 CHECK_CONNECTED();
dan_ackme 29:b6af04b77a56 367
dan_ackme 29:b6af04b77a56 368 if(rxBuffer.size == 0)
dan_ackme 29:b6af04b77a56 369 {
dan_ackme 29:b6af04b77a56 370 return WICONNECT_UNSUPPORTED;
dan_ackme 29:b6af04b77a56 371 }
dan_ackme 29:b6af04b77a56 372 else if(bufferPtr != NULL && bytesReadPtr == NULL)
dan_ackme 29:b6af04b77a56 373 {
dan_ackme 29:b6af04b77a56 374 return WICONNECT_BAD_ARG;
dan_ackme 29:b6af04b77a56 375 }
dan_ackme 29:b6af04b77a56 376
dan_ackme 29:b6af04b77a56 377 if(rxBuffer.ptr >= &rxBuffer.buffer[rxBuffer.bytesPending])
dan_ackme 29:b6af04b77a56 378 {
dan_ackme 29:b6af04b77a56 379 clearRxBuffer();
dan_ackme 29:b6af04b77a56 380 }
dan_ackme 29:b6af04b77a56 381
dan_ackme 29:b6af04b77a56 382 if(rxBuffer.bytesPending < rxBuffer.size)
dan_ackme 29:b6af04b77a56 383 {
dan_ackme 29:b6af04b77a56 384 const int bytesToRead = rxBuffer.size - rxBuffer.bytesPending;
dan_ackme 29:b6af04b77a56 385 char* ptr = (char*)&rxBuffer.buffer[rxBuffer.bytesPending];
dan_ackme 29:b6af04b77a56 386
dan_ackme 29:b6af04b77a56 387 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 29:b6af04b77a56 388
dan_ackme 29:b6af04b77a56 389 loop:
dan_ackme 29:b6af04b77a56 390 if(bytesToRead > 0 && WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(ptr, bytesToRead, "read %d %d", handle, bytesToRead)))
dan_ackme 29:b6af04b77a56 391 {
dan_ackme 29:b6af04b77a56 392 const uint16_t bytesRead = wiconnect->getLastCommandResponseLength();
dan_ackme 29:b6af04b77a56 393 enableAutoClose = (bytesRead == 0);
dan_ackme 29:b6af04b77a56 394 rxBuffer.bytesPending += bytesRead;
dan_ackme 29:b6af04b77a56 395 }
dan_ackme 29:b6af04b77a56 396
dan_ackme 29:b6af04b77a56 397 // if still processing and in non-blocking mode,
dan_ackme 29:b6af04b77a56 398 // then this api call must block until the command completes
dan_ackme 29:b6af04b77a56 399 if(result == WICONNECT_PROCESSING && wiconnect->nonBlocking)
dan_ackme 29:b6af04b77a56 400 {
dan_ackme 29:b6af04b77a56 401 goto loop;
dan_ackme 29:b6af04b77a56 402 }
dan_ackme 29:b6af04b77a56 403
dan_ackme 29:b6af04b77a56 404 CHECK_CLEANUP_COMMAND();
dan_ackme 29:b6af04b77a56 405 }
dan_ackme 29:b6af04b77a56 406
dan_ackme 29:b6af04b77a56 407 if(bufferPtr != NULL)
dan_ackme 29:b6af04b77a56 408 {
dan_ackme 29:b6af04b77a56 409 *bufferPtr = rxBuffer.buffer;
dan_ackme 29:b6af04b77a56 410 *bytesReadPtr = rxBuffer.bytesPending;
dan_ackme 29:b6af04b77a56 411 clearRxBuffer();
dan_ackme 29:b6af04b77a56 412 }
dan_ackme 29:b6af04b77a56 413
dan_ackme 29:b6af04b77a56 414 return result;
dan_ackme 29:b6af04b77a56 415 }
dan_ackme 29:b6af04b77a56 416
dan_ackme 29:b6af04b77a56 417 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 418 WiconnectResult WiconnectSocket::getc(uint8_t *c)
dan_ackme 29:b6af04b77a56 419 {
dan_ackme 29:b6af04b77a56 420 WiconnectResult result;
dan_ackme 29:b6af04b77a56 421
dan_ackme 29:b6af04b77a56 422 if(rxBuffer.size == 0)
dan_ackme 29:b6af04b77a56 423 {
dan_ackme 29:b6af04b77a56 424 return WICONNECT_UNSUPPORTED;
dan_ackme 29:b6af04b77a56 425 }
dan_ackme 29:b6af04b77a56 426
dan_ackme 29:b6af04b77a56 427 if(rxBuffer.bytesPending == 0 &&
dan_ackme 29:b6af04b77a56 428 WICONNECT_FAILED(result, read()))
dan_ackme 29:b6af04b77a56 429 {
dan_ackme 29:b6af04b77a56 430 return result;
dan_ackme 29:b6af04b77a56 431 }
dan_ackme 29:b6af04b77a56 432 else if(rxBuffer.ptr < &rxBuffer.buffer[rxBuffer.bytesPending])
dan_ackme 29:b6af04b77a56 433 {
dan_ackme 29:b6af04b77a56 434 *c = *rxBuffer.ptr;
dan_ackme 29:b6af04b77a56 435 ++rxBuffer.ptr;
dan_ackme 29:b6af04b77a56 436 return WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 437 }
dan_ackme 29:b6af04b77a56 438 else
dan_ackme 29:b6af04b77a56 439 {
dan_ackme 29:b6af04b77a56 440 clearRxBuffer();
dan_ackme 29:b6af04b77a56 441 return WICONNECT_ERROR;
dan_ackme 29:b6af04b77a56 442 }
dan_ackme 29:b6af04b77a56 443 }
dan_ackme 29:b6af04b77a56 444
dan_ackme 29:b6af04b77a56 445 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 446 WiconnectResult WiconnectSocket::putc(uint8_t c, bool flush)
dan_ackme 29:b6af04b77a56 447 {
dan_ackme 29:b6af04b77a56 448 WiconnectResult result = WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 449 CHECK_CONNECTED();
dan_ackme 29:b6af04b77a56 450
dan_ackme 29:b6af04b77a56 451 if(txBuffer.size == 0)
dan_ackme 29:b6af04b77a56 452 {
dan_ackme 29:b6af04b77a56 453 return WICONNECT_UNSUPPORTED;
dan_ackme 29:b6af04b77a56 454 }
dan_ackme 29:b6af04b77a56 455 else if(txBuffer.bytesPending < txBuffer.size)
dan_ackme 29:b6af04b77a56 456 {
dan_ackme 29:b6af04b77a56 457 uint8_t *ptr = (uint8_t*)&txBuffer.buffer[txBuffer.bytesPending];
dan_ackme 29:b6af04b77a56 458 *ptr = c;
dan_ackme 29:b6af04b77a56 459 ++txBuffer.bytesPending;
dan_ackme 29:b6af04b77a56 460
dan_ackme 29:b6af04b77a56 461 if(flush || txBuffer.bytesPending >= txBuffer.size)
dan_ackme 29:b6af04b77a56 462 {
dan_ackme 29:b6af04b77a56 463 result = flushTxBuffer();
dan_ackme 29:b6af04b77a56 464 }
dan_ackme 29:b6af04b77a56 465 }
dan_ackme 29:b6af04b77a56 466 else
dan_ackme 29:b6af04b77a56 467 {
dan_ackme 29:b6af04b77a56 468 result = WICONNECT_OVERFLOW;
dan_ackme 29:b6af04b77a56 469 }
dan_ackme 29:b6af04b77a56 470
dan_ackme 29:b6af04b77a56 471 return result;
dan_ackme 29:b6af04b77a56 472 }
dan_ackme 29:b6af04b77a56 473
dan_ackme 29:b6af04b77a56 474 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 475 WiconnectResult WiconnectSocket::puts(const char *s, bool flush)
dan_ackme 29:b6af04b77a56 476 {
dan_ackme 29:b6af04b77a56 477 const int len = strlen(s);
dan_ackme 29:b6af04b77a56 478 return write(s, len, flush);
dan_ackme 29:b6af04b77a56 479 }
dan_ackme 29:b6af04b77a56 480
dan_ackme 29:b6af04b77a56 481 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 482 WiconnectResult WiconnectSocket::printf(const char* format, ...)
dan_ackme 29:b6af04b77a56 483 {
dan_ackme 29:b6af04b77a56 484 WiconnectResult result = WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 485
dan_ackme 29:b6af04b77a56 486 CHECK_CONNECTED();
dan_ackme 29:b6af04b77a56 487 if(txBuffer.size == 0)
dan_ackme 29:b6af04b77a56 488 {
dan_ackme 29:b6af04b77a56 489 return WICONNECT_UNSUPPORTED;
dan_ackme 29:b6af04b77a56 490 }
dan_ackme 29:b6af04b77a56 491
dan_ackme 29:b6af04b77a56 492 const int available = txBuffer.size - txBuffer.bytesPending;
dan_ackme 29:b6af04b77a56 493 char *ptr = (char*)&txBuffer.buffer[txBuffer.bytesPending];
dan_ackme 29:b6af04b77a56 494 va_list args;
dan_ackme 29:b6af04b77a56 495 va_start(args, format);
dan_ackme 29:b6af04b77a56 496 const int len = vsnprintf(ptr, available, format, args);
dan_ackme 29:b6af04b77a56 497 if(len > available)
dan_ackme 29:b6af04b77a56 498 {
dan_ackme 29:b6af04b77a56 499 return WICONNECT_OVERFLOW;
dan_ackme 29:b6af04b77a56 500 }
dan_ackme 29:b6af04b77a56 501 else
dan_ackme 29:b6af04b77a56 502 {
dan_ackme 29:b6af04b77a56 503 txBuffer.bytesPending += len;
dan_ackme 29:b6af04b77a56 504 }
dan_ackme 29:b6af04b77a56 505
dan_ackme 29:b6af04b77a56 506 if(txBuffer.bytesPending >= txBuffer.size)
dan_ackme 29:b6af04b77a56 507 {
dan_ackme 29:b6af04b77a56 508 result = flushTxBuffer();
dan_ackme 29:b6af04b77a56 509 }
dan_ackme 29:b6af04b77a56 510
dan_ackme 29:b6af04b77a56 511 return result;
dan_ackme 29:b6af04b77a56 512 }
dan_ackme 29:b6af04b77a56 513
dan_ackme 29:b6af04b77a56 514
dan_ackme 29:b6af04b77a56 515 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 516 WiconnectResult WiconnectSocket::flushTxBuffer()
dan_ackme 29:b6af04b77a56 517 {
dan_ackme 29:b6af04b77a56 518 WiconnectResult result = WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 519
dan_ackme 29:b6af04b77a56 520 CHECK_CONNECTED();
dan_ackme 29:b6af04b77a56 521
dan_ackme 29:b6af04b77a56 522 if(txBuffer.size == 0)
dan_ackme 29:b6af04b77a56 523 {
dan_ackme 29:b6af04b77a56 524 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 29:b6af04b77a56 525 }
dan_ackme 29:b6af04b77a56 526
dan_ackme 29:b6af04b77a56 527 if(txBuffer.bytesPending > 0)
dan_ackme 29:b6af04b77a56 528 {
dan_ackme 29:b6af04b77a56 529 loop:
dan_ackme 29:b6af04b77a56 530 result = wiconnect->sendCommand(ReaderFunc(readerCallback), &this->txBuffer, "write %u %u", handle, txBuffer.bytesPending);
dan_ackme 29:b6af04b77a56 531
dan_ackme 29:b6af04b77a56 532 // if still processing and in non-blocking mode and using a txtBuffer,
dan_ackme 29:b6af04b77a56 533 // then this api call must block until the command completes
dan_ackme 29:b6af04b77a56 534 if(result == WICONNECT_PROCESSING && wiconnect->nonBlocking && txBuffer.size > 0)
dan_ackme 29:b6af04b77a56 535 {
dan_ackme 29:b6af04b77a56 536 goto loop;
dan_ackme 29:b6af04b77a56 537 }
dan_ackme 29:b6af04b77a56 538 }
dan_ackme 29:b6af04b77a56 539
dan_ackme 29:b6af04b77a56 540 if(txBuffer.size == 0)
dan_ackme 29:b6af04b77a56 541 {
dan_ackme 29:b6af04b77a56 542 CHECK_CLEANUP_COMMAND();
dan_ackme 29:b6af04b77a56 543 }
dan_ackme 29:b6af04b77a56 544
dan_ackme 29:b6af04b77a56 545 if(result != WICONNECT_PROCESSING)
dan_ackme 29:b6af04b77a56 546 {
dan_ackme 29:b6af04b77a56 547 txBuffer.ptr = txBuffer.buffer;
dan_ackme 29:b6af04b77a56 548 txBuffer.bytesPending = 0;
dan_ackme 29:b6af04b77a56 549 }
dan_ackme 29:b6af04b77a56 550
dan_ackme 29:b6af04b77a56 551 return result;
dan_ackme 29:b6af04b77a56 552 }
dan_ackme 29:b6af04b77a56 553
dan_ackme 29:b6af04b77a56 554
dan_ackme 29:b6af04b77a56 555
dan_ackme 29:b6af04b77a56 556 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 557 void WiconnectSocket::clearRxBuffer()
dan_ackme 29:b6af04b77a56 558 {
dan_ackme 29:b6af04b77a56 559 rxBuffer.bytesPending = 0;
dan_ackme 29:b6af04b77a56 560 rxBuffer.ptr = rxBuffer.buffer;
dan_ackme 29:b6af04b77a56 561 }
dan_ackme 29:b6af04b77a56 562
dan_ackme 29:b6af04b77a56 563 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 564 uint8_t* WiconnectSocket::getTxBuffer()
dan_ackme 29:b6af04b77a56 565 {
dan_ackme 29:b6af04b77a56 566 return txBuffer.buffer;
dan_ackme 29:b6af04b77a56 567 }
dan_ackme 29:b6af04b77a56 568 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 569 int WiconnectSocket::getTxBufferSize()
dan_ackme 29:b6af04b77a56 570 {
dan_ackme 29:b6af04b77a56 571 return txBuffer.size;
dan_ackme 29:b6af04b77a56 572 }
dan_ackme 29:b6af04b77a56 573 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 574 int WiconnectSocket::getTxBufferBytesPending()
dan_ackme 29:b6af04b77a56 575 {
dan_ackme 29:b6af04b77a56 576 return txBuffer.bytesPending;
dan_ackme 29:b6af04b77a56 577 }
dan_ackme 29:b6af04b77a56 578 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 579 uint8_t* WiconnectSocket::getRxBuffer()
dan_ackme 29:b6af04b77a56 580 {
dan_ackme 29:b6af04b77a56 581 return rxBuffer.buffer;
dan_ackme 29:b6af04b77a56 582 }
dan_ackme 29:b6af04b77a56 583 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 584 int WiconnectSocket::getRxBufferSize()
dan_ackme 29:b6af04b77a56 585 {
dan_ackme 29:b6af04b77a56 586 return rxBuffer.size;
dan_ackme 29:b6af04b77a56 587 }
dan_ackme 29:b6af04b77a56 588 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 589 int WiconnectSocket::getRxBufferBytesPending()
dan_ackme 29:b6af04b77a56 590 {
dan_ackme 29:b6af04b77a56 591 return rxBuffer.bytesPending;
dan_ackme 29:b6af04b77a56 592 }
dan_ackme 29:b6af04b77a56 593
dan_ackme 29:b6af04b77a56 594
dan_ackme 29:b6af04b77a56 595 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 596 WiconnectResult readerCallback(void *user, void *data, int maxReadSize, int *bytesRead)
dan_ackme 29:b6af04b77a56 597 {
dan_ackme 29:b6af04b77a56 598 Buffer *txBuffer = (Buffer*)user;
dan_ackme 29:b6af04b77a56 599
dan_ackme 29:b6af04b77a56 600 if(txBuffer->bytesPending == 0)
dan_ackme 29:b6af04b77a56 601 {
dan_ackme 29:b6af04b77a56 602 *bytesRead = EOF;
dan_ackme 29:b6af04b77a56 603 }
dan_ackme 29:b6af04b77a56 604 else
dan_ackme 29:b6af04b77a56 605 {
dan_ackme 29:b6af04b77a56 606 const int bytesToWrite = MIN(maxReadSize, txBuffer->bytesPending);
dan_ackme 29:b6af04b77a56 607 memcpy(data, txBuffer->ptr, bytesToWrite);
dan_ackme 29:b6af04b77a56 608 txBuffer->ptr += bytesToWrite;
dan_ackme 29:b6af04b77a56 609 txBuffer->bytesPending -= bytesToWrite;
dan_ackme 29:b6af04b77a56 610 *bytesRead = bytesToWrite;
dan_ackme 29:b6af04b77a56 611 }
dan_ackme 29:b6af04b77a56 612
dan_ackme 29:b6af04b77a56 613 return WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 614 }
dan_ackme 29:b6af04b77a56 615