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
WiconnectSocket.cpp@40:4b4306f3d829, 2015-02-23 (annotated)
- Committer:
- dan_ackme
- Date:
- Mon Feb 23 22:21:50 2015 -0800
- Revision:
- 40:4b4306f3d829
- Parent:
- 29:b6af04b77a56
- Child:
- 41:66beaca0fd1a
minor bug fixes
Who changed what in which revision?
User | Revision | Line number | New 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 | 40:4b4306f3d829 | 100 | { |
dan_ackme | 40:4b4306f3d829 | 101 | do |
dan_ackme | 40:4b4306f3d829 | 102 | { |
dan_ackme | 40:4b4306f3d829 | 103 | result = close(); |
dan_ackme | 40:4b4306f3d829 | 104 | } while(result == WICONNECT_PROCESSING); |
dan_ackme | 40:4b4306f3d829 | 105 | |
dan_ackme | 29:b6af04b77a56 | 106 | handle = handle_; |
dan_ackme | 29:b6af04b77a56 | 107 | type = type_; |
dan_ackme | 29:b6af04b77a56 | 108 | remotePort = remotePort_; |
dan_ackme | 29:b6af04b77a56 | 109 | localPort = localPort_; |
dan_ackme | 29:b6af04b77a56 | 110 | connected = true; |
dan_ackme | 29:b6af04b77a56 | 111 | enableAutoClose = false; |
dan_ackme | 29:b6af04b77a56 | 112 | |
dan_ackme | 29:b6af04b77a56 | 113 | rxBuffer.ptr = rxBuffer.buffer; |
dan_ackme | 29:b6af04b77a56 | 114 | rxBuffer.bytesPending = 0; |
dan_ackme | 29:b6af04b77a56 | 115 | txBuffer.ptr = txBuffer.buffer; |
dan_ackme | 29:b6af04b77a56 | 116 | txBuffer.bytesPending = 0; |
dan_ackme | 29:b6af04b77a56 | 117 | |
dan_ackme | 29:b6af04b77a56 | 118 | strncpy(host, host_, sizeof(host)-1); |
dan_ackme | 29:b6af04b77a56 | 119 | |
dan_ackme | 29:b6af04b77a56 | 120 | return WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 121 | } |
dan_ackme | 29:b6af04b77a56 | 122 | |
dan_ackme | 29:b6af04b77a56 | 123 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 124 | WiconnectSocket::~WiconnectSocket() |
dan_ackme | 29:b6af04b77a56 | 125 | { |
dan_ackme | 29:b6af04b77a56 | 126 | while((handle != SOCKET_INVALID_HANDLE) && (close() == WICONNECT_PROCESSING)) |
dan_ackme | 29:b6af04b77a56 | 127 | { |
dan_ackme | 29:b6af04b77a56 | 128 | } |
dan_ackme | 29:b6af04b77a56 | 129 | |
dan_ackme | 29:b6af04b77a56 | 130 | #ifdef WICONNECT_ENABLE_MALLOC |
dan_ackme | 29:b6af04b77a56 | 131 | if(txBuffer.allocated && txBuffer.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(txBuffer.buffer); |
dan_ackme | 29:b6af04b77a56 | 135 | } |
dan_ackme | 29:b6af04b77a56 | 136 | if(rxBuffer.allocated && rxBuffer.size > 0) |
dan_ackme | 29:b6af04b77a56 | 137 | { |
dan_ackme | 29:b6af04b77a56 | 138 | wiconnect_assert(wiconnect, "~Socket(), free not defined", wiconnect->_free != NULL); |
dan_ackme | 29:b6af04b77a56 | 139 | wiconnect->_free(rxBuffer.buffer); |
dan_ackme | 29:b6af04b77a56 | 140 | } |
dan_ackme | 29:b6af04b77a56 | 141 | #endif |
dan_ackme | 29:b6af04b77a56 | 142 | } |
dan_ackme | 29:b6af04b77a56 | 143 | |
dan_ackme | 29:b6af04b77a56 | 144 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 145 | bool WiconnectSocket::isConnected() |
dan_ackme | 29:b6af04b77a56 | 146 | { |
dan_ackme | 29:b6af04b77a56 | 147 | return connected; |
dan_ackme | 29:b6af04b77a56 | 148 | } |
dan_ackme | 29:b6af04b77a56 | 149 | |
dan_ackme | 29:b6af04b77a56 | 150 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 151 | SocketType WiconnectSocket::getType() |
dan_ackme | 29:b6af04b77a56 | 152 | { |
dan_ackme | 29:b6af04b77a56 | 153 | return type; |
dan_ackme | 29:b6af04b77a56 | 154 | } |
dan_ackme | 29:b6af04b77a56 | 155 | |
dan_ackme | 29:b6af04b77a56 | 156 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 157 | const char* WiconnectSocket::getHost() |
dan_ackme | 29:b6af04b77a56 | 158 | { |
dan_ackme | 29:b6af04b77a56 | 159 | return host; |
dan_ackme | 29:b6af04b77a56 | 160 | } |
dan_ackme | 29:b6af04b77a56 | 161 | |
dan_ackme | 29:b6af04b77a56 | 162 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 163 | uint16_t WiconnectSocket::getLocalPort() |
dan_ackme | 29:b6af04b77a56 | 164 | { |
dan_ackme | 29:b6af04b77a56 | 165 | return localPort; |
dan_ackme | 29:b6af04b77a56 | 166 | } |
dan_ackme | 29:b6af04b77a56 | 167 | |
dan_ackme | 29:b6af04b77a56 | 168 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 169 | uint16_t WiconnectSocket::getRemotePort() |
dan_ackme | 29:b6af04b77a56 | 170 | { |
dan_ackme | 29:b6af04b77a56 | 171 | return remotePort; |
dan_ackme | 29:b6af04b77a56 | 172 | } |
dan_ackme | 29:b6af04b77a56 | 173 | |
dan_ackme | 29:b6af04b77a56 | 174 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 175 | uint8_t WiconnectSocket::getHandle() |
dan_ackme | 29:b6af04b77a56 | 176 | { |
dan_ackme | 29:b6af04b77a56 | 177 | return handle; |
dan_ackme | 29:b6af04b77a56 | 178 | } |
dan_ackme | 29:b6af04b77a56 | 179 | |
dan_ackme | 29:b6af04b77a56 | 180 | |
dan_ackme | 29:b6af04b77a56 | 181 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 182 | WiconnectResult WiconnectSocket::close() |
dan_ackme | 29:b6af04b77a56 | 183 | { |
dan_ackme | 29:b6af04b77a56 | 184 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 185 | CHECK_CONNECTED(); |
dan_ackme | 29:b6af04b77a56 | 186 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 187 | |
dan_ackme | 29:b6af04b77a56 | 188 | result = wiconnect->sendCommand("close %d", handle); |
dan_ackme | 29:b6af04b77a56 | 189 | |
dan_ackme | 29:b6af04b77a56 | 190 | if(result != WICONNECT_PROCESSING) |
dan_ackme | 29:b6af04b77a56 | 191 | { |
dan_ackme | 29:b6af04b77a56 | 192 | connected = false; |
dan_ackme | 29:b6af04b77a56 | 193 | wiconnect->socketClosedCallback(this); |
dan_ackme | 29:b6af04b77a56 | 194 | } |
dan_ackme | 29:b6af04b77a56 | 195 | |
dan_ackme | 29:b6af04b77a56 | 196 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 197 | |
dan_ackme | 29:b6af04b77a56 | 198 | return result; |
dan_ackme | 29:b6af04b77a56 | 199 | } |
dan_ackme | 29:b6af04b77a56 | 200 | |
dan_ackme | 29:b6af04b77a56 | 201 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 202 | WiconnectResult WiconnectSocket::poll(bool *rxDataAvailablePtr, bool autoClose) |
dan_ackme | 29:b6af04b77a56 | 203 | { |
dan_ackme | 29:b6af04b77a56 | 204 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 205 | int32_t status; |
dan_ackme | 29:b6af04b77a56 | 206 | bool autoClosed = false; |
dan_ackme | 29:b6af04b77a56 | 207 | |
dan_ackme | 29:b6af04b77a56 | 208 | *rxDataAvailablePtr = false; |
dan_ackme | 29:b6af04b77a56 | 209 | |
dan_ackme | 29:b6af04b77a56 | 210 | CHECK_CONNECTED(); |
dan_ackme | 29:b6af04b77a56 | 211 | |
dan_ackme | 29:b6af04b77a56 | 212 | if(rxBuffer.size > 0 && WICONNECT_IS_IDLE()) |
dan_ackme | 29:b6af04b77a56 | 213 | { |
dan_ackme | 29:b6af04b77a56 | 214 | if(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 | else if(read() == WICONNECT_SUCCESS && (rxBuffer.ptr < &rxBuffer.buffer[rxBuffer.bytesPending])) |
dan_ackme | 29:b6af04b77a56 | 220 | { |
dan_ackme | 29:b6af04b77a56 | 221 | *rxDataAvailablePtr = true; |
dan_ackme | 29:b6af04b77a56 | 222 | return WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 223 | } |
dan_ackme | 29:b6af04b77a56 | 224 | } |
dan_ackme | 29:b6af04b77a56 | 225 | |
dan_ackme | 29:b6af04b77a56 | 226 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 227 | |
dan_ackme | 29:b6af04b77a56 | 228 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("poll %d", handle))) |
dan_ackme | 29:b6af04b77a56 | 229 | { |
dan_ackme | 29:b6af04b77a56 | 230 | if(!WICONNECT_FAILED(result, wiconnect->responseToInt32(&status))) |
dan_ackme | 29:b6af04b77a56 | 231 | { |
dan_ackme | 29:b6af04b77a56 | 232 | if(status > 0) |
dan_ackme | 29:b6af04b77a56 | 233 | { |
dan_ackme | 29:b6af04b77a56 | 234 | if(status == 2 && enableAutoClose) |
dan_ackme | 29:b6af04b77a56 | 235 | { |
dan_ackme | 29:b6af04b77a56 | 236 | autoClosed = autoClose; |
dan_ackme | 29:b6af04b77a56 | 237 | *rxDataAvailablePtr = false; |
dan_ackme | 29:b6af04b77a56 | 238 | } |
dan_ackme | 29:b6af04b77a56 | 239 | else |
dan_ackme | 29:b6af04b77a56 | 240 | { |
dan_ackme | 29:b6af04b77a56 | 241 | *rxDataAvailablePtr = true; |
dan_ackme | 29:b6af04b77a56 | 242 | } |
dan_ackme | 29:b6af04b77a56 | 243 | } |
dan_ackme | 29:b6af04b77a56 | 244 | } |
dan_ackme | 29:b6af04b77a56 | 245 | } |
dan_ackme | 29:b6af04b77a56 | 246 | |
dan_ackme | 29:b6af04b77a56 | 247 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 248 | |
dan_ackme | 29:b6af04b77a56 | 249 | // if we auto-closed, then block until everything is cleaned-up |
dan_ackme | 29:b6af04b77a56 | 250 | if(autoClosed) |
dan_ackme | 29:b6af04b77a56 | 251 | { |
dan_ackme | 29:b6af04b77a56 | 252 | while(WICONNECT_IS_PROCESSING(result, close())) |
dan_ackme | 29:b6af04b77a56 | 253 | { |
dan_ackme | 29:b6af04b77a56 | 254 | } |
dan_ackme | 29:b6af04b77a56 | 255 | } |
dan_ackme | 29:b6af04b77a56 | 256 | |
dan_ackme | 29:b6af04b77a56 | 257 | return result; |
dan_ackme | 29:b6af04b77a56 | 258 | } |
dan_ackme | 29:b6af04b77a56 | 259 | |
dan_ackme | 29:b6af04b77a56 | 260 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 261 | WiconnectResult WiconnectSocket::write(int length, bool flush) |
dan_ackme | 29:b6af04b77a56 | 262 | { |
dan_ackme | 29:b6af04b77a56 | 263 | CHECK_CONNECTED(); |
dan_ackme | 29:b6af04b77a56 | 264 | |
dan_ackme | 29:b6af04b77a56 | 265 | if( txBuffer.size == 0) |
dan_ackme | 29:b6af04b77a56 | 266 | { |
dan_ackme | 29:b6af04b77a56 | 267 | return WICONNECT_UNSUPPORTED; |
dan_ackme | 29:b6af04b77a56 | 268 | } |
dan_ackme | 29:b6af04b77a56 | 269 | else if(length > txBuffer.size) |
dan_ackme | 29:b6af04b77a56 | 270 | { |
dan_ackme | 29:b6af04b77a56 | 271 | return WICONNECT_OVERFLOW; |
dan_ackme | 29:b6af04b77a56 | 272 | } |
dan_ackme | 29:b6af04b77a56 | 273 | txBuffer.bytesPending = length; |
dan_ackme | 29:b6af04b77a56 | 274 | |
dan_ackme | 29:b6af04b77a56 | 275 | return flush ? flushTxBuffer() : WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 276 | } |
dan_ackme | 29:b6af04b77a56 | 277 | |
dan_ackme | 29:b6af04b77a56 | 278 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 279 | WiconnectResult WiconnectSocket::write(const void* buffer, int length, bool flush) |
dan_ackme | 29:b6af04b77a56 | 280 | { |
dan_ackme | 29:b6af04b77a56 | 281 | WiconnectResult result = WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 282 | CHECK_CONNECTED(); |
dan_ackme | 29:b6af04b77a56 | 283 | |
dan_ackme | 29:b6af04b77a56 | 284 | if(txBuffer.size > 0) |
dan_ackme | 29:b6af04b77a56 | 285 | { |
dan_ackme | 29:b6af04b77a56 | 286 | const uint8_t *src = (const uint8_t *)buffer; |
dan_ackme | 29:b6af04b77a56 | 287 | |
dan_ackme | 29:b6af04b77a56 | 288 | while(length > 0) |
dan_ackme | 29:b6af04b77a56 | 289 | { |
dan_ackme | 29:b6af04b77a56 | 290 | int bytesToWrite = MIN(length, txBuffer.size - txBuffer.bytesPending); |
dan_ackme | 29:b6af04b77a56 | 291 | uint8_t *dst = (uint8_t*)&txBuffer.buffer[txBuffer.bytesPending]; |
dan_ackme | 29:b6af04b77a56 | 292 | memcpy(dst, src, bytesToWrite); |
dan_ackme | 29:b6af04b77a56 | 293 | txBuffer.bytesPending += bytesToWrite; |
dan_ackme | 29:b6af04b77a56 | 294 | length -= bytesToWrite; |
dan_ackme | 29:b6af04b77a56 | 295 | src += bytesToWrite; |
dan_ackme | 29:b6af04b77a56 | 296 | |
dan_ackme | 29:b6af04b77a56 | 297 | if((txBuffer.bytesPending >= txBuffer.size) && |
dan_ackme | 29:b6af04b77a56 | 298 | WICONNECT_FAILED(result, flushTxBuffer())) |
dan_ackme | 29:b6af04b77a56 | 299 | { |
dan_ackme | 29:b6af04b77a56 | 300 | break; |
dan_ackme | 29:b6af04b77a56 | 301 | } |
dan_ackme | 29:b6af04b77a56 | 302 | } |
dan_ackme | 29:b6af04b77a56 | 303 | |
dan_ackme | 29:b6af04b77a56 | 304 | if(flush && txBuffer.bytesPending > 0) |
dan_ackme | 29:b6af04b77a56 | 305 | { |
dan_ackme | 29:b6af04b77a56 | 306 | result = flushTxBuffer(); |
dan_ackme | 29:b6af04b77a56 | 307 | } |
dan_ackme | 29:b6af04b77a56 | 308 | } |
dan_ackme | 29:b6af04b77a56 | 309 | else |
dan_ackme | 29:b6af04b77a56 | 310 | { |
dan_ackme | 29:b6af04b77a56 | 311 | if(WICONNECT_IS_IDLE()) |
dan_ackme | 29:b6af04b77a56 | 312 | { |
dan_ackme | 29:b6af04b77a56 | 313 | txBuffer.ptr = (uint8_t*)buffer; |
dan_ackme | 29:b6af04b77a56 | 314 | txBuffer.bytesPending = length; |
dan_ackme | 29:b6af04b77a56 | 315 | } |
dan_ackme | 29:b6af04b77a56 | 316 | |
dan_ackme | 29:b6af04b77a56 | 317 | result = flushTxBuffer(); |
dan_ackme | 29:b6af04b77a56 | 318 | } |
dan_ackme | 29:b6af04b77a56 | 319 | |
dan_ackme | 29:b6af04b77a56 | 320 | |
dan_ackme | 29:b6af04b77a56 | 321 | return result; |
dan_ackme | 29:b6af04b77a56 | 322 | } |
dan_ackme | 29:b6af04b77a56 | 323 | |
dan_ackme | 29:b6af04b77a56 | 324 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 325 | WiconnectResult WiconnectSocket::read(void* buffer, uint16_t maxLength, uint16_t *bytesReadPtr) |
dan_ackme | 29:b6af04b77a56 | 326 | { |
dan_ackme | 29:b6af04b77a56 | 327 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 328 | |
dan_ackme | 29:b6af04b77a56 | 329 | CHECK_CONNECTED(); |
dan_ackme | 29:b6af04b77a56 | 330 | |
dan_ackme | 29:b6af04b77a56 | 331 | if(rxBuffer.size > 0) |
dan_ackme | 29:b6af04b77a56 | 332 | { |
dan_ackme | 29:b6af04b77a56 | 333 | uint16_t bytesToRead = 0; |
dan_ackme | 29:b6af04b77a56 | 334 | const uint16_t bufferedBytes = (&rxBuffer.buffer[rxBuffer.bytesPending] - rxBuffer.ptr); |
dan_ackme | 29:b6af04b77a56 | 335 | if(bufferedBytes > 0) |
dan_ackme | 29:b6af04b77a56 | 336 | { |
dan_ackme | 29:b6af04b77a56 | 337 | bytesToRead = MIN(bufferedBytes, maxLength); |
dan_ackme | 29:b6af04b77a56 | 338 | memcpy(buffer, rxBuffer.ptr, bytesToRead); |
dan_ackme | 29:b6af04b77a56 | 339 | rxBuffer.ptr += bytesToRead; |
dan_ackme | 29:b6af04b77a56 | 340 | *bytesReadPtr = bytesToRead; |
dan_ackme | 29:b6af04b77a56 | 341 | } |
dan_ackme | 29:b6af04b77a56 | 342 | if(rxBuffer.ptr >= &rxBuffer.buffer[rxBuffer.bytesPending]) |
dan_ackme | 29:b6af04b77a56 | 343 | { |
dan_ackme | 29:b6af04b77a56 | 344 | clearRxBuffer(); |
dan_ackme | 29:b6af04b77a56 | 345 | } |
dan_ackme | 29:b6af04b77a56 | 346 | if(bytesToRead > 0) |
dan_ackme | 29:b6af04b77a56 | 347 | { |
dan_ackme | 29:b6af04b77a56 | 348 | return WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 349 | } |
dan_ackme | 29:b6af04b77a56 | 350 | } |
dan_ackme | 29:b6af04b77a56 | 351 | |
dan_ackme | 29:b6af04b77a56 | 352 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 353 | |
dan_ackme | 29:b6af04b77a56 | 354 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand((char*)buffer, maxLength, "read %d %d", handle, maxLength))) |
dan_ackme | 29:b6af04b77a56 | 355 | { |
dan_ackme | 29:b6af04b77a56 | 356 | const uint16_t bytesRead = wiconnect->getLastCommandResponseLength(); |
dan_ackme | 29:b6af04b77a56 | 357 | enableAutoClose = (bytesRead == 0); |
dan_ackme | 29:b6af04b77a56 | 358 | *bytesReadPtr = bytesRead; |
dan_ackme | 29:b6af04b77a56 | 359 | } |
dan_ackme | 29:b6af04b77a56 | 360 | |
dan_ackme | 29:b6af04b77a56 | 361 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 362 | |
dan_ackme | 29:b6af04b77a56 | 363 | return result; |
dan_ackme | 29:b6af04b77a56 | 364 | } |
dan_ackme | 29:b6af04b77a56 | 365 | |
dan_ackme | 29:b6af04b77a56 | 366 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 367 | WiconnectResult WiconnectSocket::read(uint8_t **bufferPtr, uint16_t *bytesReadPtr) |
dan_ackme | 29:b6af04b77a56 | 368 | { |
dan_ackme | 29:b6af04b77a56 | 369 | WiconnectResult result = WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 370 | |
dan_ackme | 29:b6af04b77a56 | 371 | CHECK_CONNECTED(); |
dan_ackme | 29:b6af04b77a56 | 372 | |
dan_ackme | 29:b6af04b77a56 | 373 | if(rxBuffer.size == 0) |
dan_ackme | 29:b6af04b77a56 | 374 | { |
dan_ackme | 29:b6af04b77a56 | 375 | return WICONNECT_UNSUPPORTED; |
dan_ackme | 29:b6af04b77a56 | 376 | } |
dan_ackme | 29:b6af04b77a56 | 377 | else if(bufferPtr != NULL && bytesReadPtr == NULL) |
dan_ackme | 29:b6af04b77a56 | 378 | { |
dan_ackme | 29:b6af04b77a56 | 379 | return WICONNECT_BAD_ARG; |
dan_ackme | 29:b6af04b77a56 | 380 | } |
dan_ackme | 29:b6af04b77a56 | 381 | |
dan_ackme | 29:b6af04b77a56 | 382 | if(rxBuffer.ptr >= &rxBuffer.buffer[rxBuffer.bytesPending]) |
dan_ackme | 29:b6af04b77a56 | 383 | { |
dan_ackme | 29:b6af04b77a56 | 384 | clearRxBuffer(); |
dan_ackme | 29:b6af04b77a56 | 385 | } |
dan_ackme | 29:b6af04b77a56 | 386 | |
dan_ackme | 29:b6af04b77a56 | 387 | if(rxBuffer.bytesPending < rxBuffer.size) |
dan_ackme | 29:b6af04b77a56 | 388 | { |
dan_ackme | 29:b6af04b77a56 | 389 | const int bytesToRead = rxBuffer.size - rxBuffer.bytesPending; |
dan_ackme | 29:b6af04b77a56 | 390 | char* ptr = (char*)&rxBuffer.buffer[rxBuffer.bytesPending]; |
dan_ackme | 29:b6af04b77a56 | 391 | |
dan_ackme | 29:b6af04b77a56 | 392 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 393 | |
dan_ackme | 29:b6af04b77a56 | 394 | loop: |
dan_ackme | 29:b6af04b77a56 | 395 | if(bytesToRead > 0 && WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(ptr, bytesToRead, "read %d %d", handle, bytesToRead))) |
dan_ackme | 29:b6af04b77a56 | 396 | { |
dan_ackme | 29:b6af04b77a56 | 397 | const uint16_t bytesRead = wiconnect->getLastCommandResponseLength(); |
dan_ackme | 29:b6af04b77a56 | 398 | enableAutoClose = (bytesRead == 0); |
dan_ackme | 29:b6af04b77a56 | 399 | rxBuffer.bytesPending += bytesRead; |
dan_ackme | 29:b6af04b77a56 | 400 | } |
dan_ackme | 29:b6af04b77a56 | 401 | |
dan_ackme | 29:b6af04b77a56 | 402 | // if still processing and in non-blocking mode, |
dan_ackme | 29:b6af04b77a56 | 403 | // then this api call must block until the command completes |
dan_ackme | 29:b6af04b77a56 | 404 | if(result == WICONNECT_PROCESSING && wiconnect->nonBlocking) |
dan_ackme | 29:b6af04b77a56 | 405 | { |
dan_ackme | 29:b6af04b77a56 | 406 | goto loop; |
dan_ackme | 29:b6af04b77a56 | 407 | } |
dan_ackme | 29:b6af04b77a56 | 408 | |
dan_ackme | 29:b6af04b77a56 | 409 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 410 | } |
dan_ackme | 29:b6af04b77a56 | 411 | |
dan_ackme | 29:b6af04b77a56 | 412 | if(bufferPtr != NULL) |
dan_ackme | 29:b6af04b77a56 | 413 | { |
dan_ackme | 29:b6af04b77a56 | 414 | *bufferPtr = rxBuffer.buffer; |
dan_ackme | 29:b6af04b77a56 | 415 | *bytesReadPtr = rxBuffer.bytesPending; |
dan_ackme | 29:b6af04b77a56 | 416 | clearRxBuffer(); |
dan_ackme | 29:b6af04b77a56 | 417 | } |
dan_ackme | 29:b6af04b77a56 | 418 | |
dan_ackme | 29:b6af04b77a56 | 419 | return result; |
dan_ackme | 29:b6af04b77a56 | 420 | } |
dan_ackme | 29:b6af04b77a56 | 421 | |
dan_ackme | 29:b6af04b77a56 | 422 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 423 | WiconnectResult WiconnectSocket::getc(uint8_t *c) |
dan_ackme | 29:b6af04b77a56 | 424 | { |
dan_ackme | 29:b6af04b77a56 | 425 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 426 | |
dan_ackme | 29:b6af04b77a56 | 427 | if(rxBuffer.size == 0) |
dan_ackme | 29:b6af04b77a56 | 428 | { |
dan_ackme | 29:b6af04b77a56 | 429 | return WICONNECT_UNSUPPORTED; |
dan_ackme | 29:b6af04b77a56 | 430 | } |
dan_ackme | 29:b6af04b77a56 | 431 | |
dan_ackme | 29:b6af04b77a56 | 432 | if(rxBuffer.bytesPending == 0 && |
dan_ackme | 29:b6af04b77a56 | 433 | WICONNECT_FAILED(result, read())) |
dan_ackme | 29:b6af04b77a56 | 434 | { |
dan_ackme | 29:b6af04b77a56 | 435 | return result; |
dan_ackme | 29:b6af04b77a56 | 436 | } |
dan_ackme | 29:b6af04b77a56 | 437 | else if(rxBuffer.ptr < &rxBuffer.buffer[rxBuffer.bytesPending]) |
dan_ackme | 29:b6af04b77a56 | 438 | { |
dan_ackme | 29:b6af04b77a56 | 439 | *c = *rxBuffer.ptr; |
dan_ackme | 29:b6af04b77a56 | 440 | ++rxBuffer.ptr; |
dan_ackme | 29:b6af04b77a56 | 441 | return WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 442 | } |
dan_ackme | 29:b6af04b77a56 | 443 | else |
dan_ackme | 29:b6af04b77a56 | 444 | { |
dan_ackme | 29:b6af04b77a56 | 445 | clearRxBuffer(); |
dan_ackme | 29:b6af04b77a56 | 446 | return WICONNECT_ERROR; |
dan_ackme | 29:b6af04b77a56 | 447 | } |
dan_ackme | 29:b6af04b77a56 | 448 | } |
dan_ackme | 29:b6af04b77a56 | 449 | |
dan_ackme | 29:b6af04b77a56 | 450 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 451 | WiconnectResult WiconnectSocket::putc(uint8_t c, bool flush) |
dan_ackme | 29:b6af04b77a56 | 452 | { |
dan_ackme | 29:b6af04b77a56 | 453 | WiconnectResult result = WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 454 | CHECK_CONNECTED(); |
dan_ackme | 29:b6af04b77a56 | 455 | |
dan_ackme | 29:b6af04b77a56 | 456 | if(txBuffer.size == 0) |
dan_ackme | 29:b6af04b77a56 | 457 | { |
dan_ackme | 29:b6af04b77a56 | 458 | return WICONNECT_UNSUPPORTED; |
dan_ackme | 29:b6af04b77a56 | 459 | } |
dan_ackme | 29:b6af04b77a56 | 460 | else if(txBuffer.bytesPending < txBuffer.size) |
dan_ackme | 29:b6af04b77a56 | 461 | { |
dan_ackme | 29:b6af04b77a56 | 462 | uint8_t *ptr = (uint8_t*)&txBuffer.buffer[txBuffer.bytesPending]; |
dan_ackme | 29:b6af04b77a56 | 463 | *ptr = c; |
dan_ackme | 29:b6af04b77a56 | 464 | ++txBuffer.bytesPending; |
dan_ackme | 29:b6af04b77a56 | 465 | |
dan_ackme | 29:b6af04b77a56 | 466 | if(flush || txBuffer.bytesPending >= txBuffer.size) |
dan_ackme | 29:b6af04b77a56 | 467 | { |
dan_ackme | 29:b6af04b77a56 | 468 | result = flushTxBuffer(); |
dan_ackme | 29:b6af04b77a56 | 469 | } |
dan_ackme | 29:b6af04b77a56 | 470 | } |
dan_ackme | 29:b6af04b77a56 | 471 | else |
dan_ackme | 29:b6af04b77a56 | 472 | { |
dan_ackme | 29:b6af04b77a56 | 473 | result = WICONNECT_OVERFLOW; |
dan_ackme | 29:b6af04b77a56 | 474 | } |
dan_ackme | 29:b6af04b77a56 | 475 | |
dan_ackme | 29:b6af04b77a56 | 476 | return result; |
dan_ackme | 29:b6af04b77a56 | 477 | } |
dan_ackme | 29:b6af04b77a56 | 478 | |
dan_ackme | 29:b6af04b77a56 | 479 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 480 | WiconnectResult WiconnectSocket::puts(const char *s, bool flush) |
dan_ackme | 29:b6af04b77a56 | 481 | { |
dan_ackme | 29:b6af04b77a56 | 482 | const int len = strlen(s); |
dan_ackme | 29:b6af04b77a56 | 483 | return write(s, len, flush); |
dan_ackme | 29:b6af04b77a56 | 484 | } |
dan_ackme | 29:b6af04b77a56 | 485 | |
dan_ackme | 29:b6af04b77a56 | 486 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 487 | WiconnectResult WiconnectSocket::printf(const char* format, ...) |
dan_ackme | 29:b6af04b77a56 | 488 | { |
dan_ackme | 29:b6af04b77a56 | 489 | WiconnectResult result = WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 490 | |
dan_ackme | 29:b6af04b77a56 | 491 | CHECK_CONNECTED(); |
dan_ackme | 29:b6af04b77a56 | 492 | if(txBuffer.size == 0) |
dan_ackme | 29:b6af04b77a56 | 493 | { |
dan_ackme | 29:b6af04b77a56 | 494 | return WICONNECT_UNSUPPORTED; |
dan_ackme | 29:b6af04b77a56 | 495 | } |
dan_ackme | 29:b6af04b77a56 | 496 | |
dan_ackme | 29:b6af04b77a56 | 497 | const int available = txBuffer.size - txBuffer.bytesPending; |
dan_ackme | 29:b6af04b77a56 | 498 | char *ptr = (char*)&txBuffer.buffer[txBuffer.bytesPending]; |
dan_ackme | 29:b6af04b77a56 | 499 | va_list args; |
dan_ackme | 29:b6af04b77a56 | 500 | va_start(args, format); |
dan_ackme | 29:b6af04b77a56 | 501 | const int len = vsnprintf(ptr, available, format, args); |
dan_ackme | 29:b6af04b77a56 | 502 | if(len > available) |
dan_ackme | 29:b6af04b77a56 | 503 | { |
dan_ackme | 29:b6af04b77a56 | 504 | return WICONNECT_OVERFLOW; |
dan_ackme | 29:b6af04b77a56 | 505 | } |
dan_ackme | 29:b6af04b77a56 | 506 | else |
dan_ackme | 29:b6af04b77a56 | 507 | { |
dan_ackme | 29:b6af04b77a56 | 508 | txBuffer.bytesPending += len; |
dan_ackme | 29:b6af04b77a56 | 509 | } |
dan_ackme | 29:b6af04b77a56 | 510 | |
dan_ackme | 29:b6af04b77a56 | 511 | if(txBuffer.bytesPending >= txBuffer.size) |
dan_ackme | 29:b6af04b77a56 | 512 | { |
dan_ackme | 29:b6af04b77a56 | 513 | result = flushTxBuffer(); |
dan_ackme | 29:b6af04b77a56 | 514 | } |
dan_ackme | 29:b6af04b77a56 | 515 | |
dan_ackme | 29:b6af04b77a56 | 516 | return result; |
dan_ackme | 29:b6af04b77a56 | 517 | } |
dan_ackme | 29:b6af04b77a56 | 518 | |
dan_ackme | 29:b6af04b77a56 | 519 | |
dan_ackme | 29:b6af04b77a56 | 520 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 521 | WiconnectResult WiconnectSocket::flushTxBuffer() |
dan_ackme | 29:b6af04b77a56 | 522 | { |
dan_ackme | 29:b6af04b77a56 | 523 | WiconnectResult result = WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 524 | |
dan_ackme | 29:b6af04b77a56 | 525 | CHECK_CONNECTED(); |
dan_ackme | 29:b6af04b77a56 | 526 | |
dan_ackme | 29:b6af04b77a56 | 527 | if(txBuffer.size == 0) |
dan_ackme | 29:b6af04b77a56 | 528 | { |
dan_ackme | 29:b6af04b77a56 | 529 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 530 | } |
dan_ackme | 29:b6af04b77a56 | 531 | |
dan_ackme | 29:b6af04b77a56 | 532 | if(txBuffer.bytesPending > 0) |
dan_ackme | 29:b6af04b77a56 | 533 | { |
dan_ackme | 29:b6af04b77a56 | 534 | loop: |
dan_ackme | 29:b6af04b77a56 | 535 | result = wiconnect->sendCommand(ReaderFunc(readerCallback), &this->txBuffer, "write %u %u", handle, txBuffer.bytesPending); |
dan_ackme | 29:b6af04b77a56 | 536 | |
dan_ackme | 29:b6af04b77a56 | 537 | // if still processing and in non-blocking mode and using a txtBuffer, |
dan_ackme | 29:b6af04b77a56 | 538 | // then this api call must block until the command completes |
dan_ackme | 29:b6af04b77a56 | 539 | if(result == WICONNECT_PROCESSING && wiconnect->nonBlocking && txBuffer.size > 0) |
dan_ackme | 29:b6af04b77a56 | 540 | { |
dan_ackme | 29:b6af04b77a56 | 541 | goto loop; |
dan_ackme | 29:b6af04b77a56 | 542 | } |
dan_ackme | 29:b6af04b77a56 | 543 | } |
dan_ackme | 29:b6af04b77a56 | 544 | |
dan_ackme | 29:b6af04b77a56 | 545 | if(txBuffer.size == 0) |
dan_ackme | 29:b6af04b77a56 | 546 | { |
dan_ackme | 29:b6af04b77a56 | 547 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 548 | } |
dan_ackme | 29:b6af04b77a56 | 549 | |
dan_ackme | 29:b6af04b77a56 | 550 | if(result != WICONNECT_PROCESSING) |
dan_ackme | 29:b6af04b77a56 | 551 | { |
dan_ackme | 29:b6af04b77a56 | 552 | txBuffer.ptr = txBuffer.buffer; |
dan_ackme | 29:b6af04b77a56 | 553 | txBuffer.bytesPending = 0; |
dan_ackme | 29:b6af04b77a56 | 554 | } |
dan_ackme | 29:b6af04b77a56 | 555 | |
dan_ackme | 29:b6af04b77a56 | 556 | return result; |
dan_ackme | 29:b6af04b77a56 | 557 | } |
dan_ackme | 29:b6af04b77a56 | 558 | |
dan_ackme | 29:b6af04b77a56 | 559 | |
dan_ackme | 29:b6af04b77a56 | 560 | |
dan_ackme | 29:b6af04b77a56 | 561 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 562 | void WiconnectSocket::clearRxBuffer() |
dan_ackme | 29:b6af04b77a56 | 563 | { |
dan_ackme | 29:b6af04b77a56 | 564 | rxBuffer.bytesPending = 0; |
dan_ackme | 29:b6af04b77a56 | 565 | rxBuffer.ptr = rxBuffer.buffer; |
dan_ackme | 29:b6af04b77a56 | 566 | } |
dan_ackme | 29:b6af04b77a56 | 567 | |
dan_ackme | 29:b6af04b77a56 | 568 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 569 | uint8_t* WiconnectSocket::getTxBuffer() |
dan_ackme | 29:b6af04b77a56 | 570 | { |
dan_ackme | 29:b6af04b77a56 | 571 | return txBuffer.buffer; |
dan_ackme | 29:b6af04b77a56 | 572 | } |
dan_ackme | 29:b6af04b77a56 | 573 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 574 | int WiconnectSocket::getTxBufferSize() |
dan_ackme | 29:b6af04b77a56 | 575 | { |
dan_ackme | 29:b6af04b77a56 | 576 | return txBuffer.size; |
dan_ackme | 29:b6af04b77a56 | 577 | } |
dan_ackme | 29:b6af04b77a56 | 578 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 579 | int WiconnectSocket::getTxBufferBytesPending() |
dan_ackme | 29:b6af04b77a56 | 580 | { |
dan_ackme | 29:b6af04b77a56 | 581 | return txBuffer.bytesPending; |
dan_ackme | 29:b6af04b77a56 | 582 | } |
dan_ackme | 29:b6af04b77a56 | 583 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 584 | uint8_t* WiconnectSocket::getRxBuffer() |
dan_ackme | 29:b6af04b77a56 | 585 | { |
dan_ackme | 29:b6af04b77a56 | 586 | return rxBuffer.buffer; |
dan_ackme | 29:b6af04b77a56 | 587 | } |
dan_ackme | 29:b6af04b77a56 | 588 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 589 | int WiconnectSocket::getRxBufferSize() |
dan_ackme | 29:b6af04b77a56 | 590 | { |
dan_ackme | 29:b6af04b77a56 | 591 | return rxBuffer.size; |
dan_ackme | 29:b6af04b77a56 | 592 | } |
dan_ackme | 29:b6af04b77a56 | 593 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 594 | int WiconnectSocket::getRxBufferBytesPending() |
dan_ackme | 29:b6af04b77a56 | 595 | { |
dan_ackme | 29:b6af04b77a56 | 596 | return rxBuffer.bytesPending; |
dan_ackme | 29:b6af04b77a56 | 597 | } |
dan_ackme | 29:b6af04b77a56 | 598 | |
dan_ackme | 29:b6af04b77a56 | 599 | |
dan_ackme | 29:b6af04b77a56 | 600 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 601 | WiconnectResult readerCallback(void *user, void *data, int maxReadSize, int *bytesRead) |
dan_ackme | 29:b6af04b77a56 | 602 | { |
dan_ackme | 29:b6af04b77a56 | 603 | Buffer *txBuffer = (Buffer*)user; |
dan_ackme | 29:b6af04b77a56 | 604 | |
dan_ackme | 29:b6af04b77a56 | 605 | if(txBuffer->bytesPending == 0) |
dan_ackme | 29:b6af04b77a56 | 606 | { |
dan_ackme | 29:b6af04b77a56 | 607 | *bytesRead = EOF; |
dan_ackme | 29:b6af04b77a56 | 608 | } |
dan_ackme | 29:b6af04b77a56 | 609 | else |
dan_ackme | 29:b6af04b77a56 | 610 | { |
dan_ackme | 29:b6af04b77a56 | 611 | const int bytesToWrite = MIN(maxReadSize, txBuffer->bytesPending); |
dan_ackme | 29:b6af04b77a56 | 612 | memcpy(data, txBuffer->ptr, bytesToWrite); |
dan_ackme | 29:b6af04b77a56 | 613 | txBuffer->ptr += bytesToWrite; |
dan_ackme | 29:b6af04b77a56 | 614 | txBuffer->bytesPending -= bytesToWrite; |
dan_ackme | 29:b6af04b77a56 | 615 | *bytesRead = bytesToWrite; |
dan_ackme | 29:b6af04b77a56 | 616 | } |
dan_ackme | 29:b6af04b77a56 | 617 | |
dan_ackme | 29:b6af04b77a56 | 618 | return WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 619 | } |
dan_ackme | 29:b6af04b77a56 | 620 |