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
ProcessCommand.cpp@42:8ffb253b09e7, 2015-09-28 (annotated)
- Committer:
- aymangrais
- Date:
- Mon Sep 28 03:38:43 2015 +0000
- Revision:
- 42:8ffb253b09e7
- Parent:
- 29:b6af04b77a56
increase ota timeout to be 5 seconds (instead of 1.5 sec)
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 | |
dan_ackme | 29:b6af04b77a56 | 30 | #include "internal/CommandCommon.h" |
dan_ackme | 29:b6af04b77a56 | 31 | |
dan_ackme | 29:b6af04b77a56 | 32 | |
dan_ackme | 29:b6af04b77a56 | 33 | |
dan_ackme | 29:b6af04b77a56 | 34 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 35 | WiconnectResult Wiconnect::checkCurrentCommand() |
dan_ackme | 29:b6af04b77a56 | 36 | { |
dan_ackme | 29:b6af04b77a56 | 37 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 38 | |
dan_ackme | 29:b6af04b77a56 | 39 | start: |
dan_ackme | 29:b6af04b77a56 | 40 | CHECK_INITIALIZED(); |
dan_ackme | 29:b6af04b77a56 | 41 | if(!commandExecuting) |
dan_ackme | 29:b6af04b77a56 | 42 | { |
dan_ackme | 29:b6af04b77a56 | 43 | return WICONNECT_IDLE; |
dan_ackme | 29:b6af04b77a56 | 44 | } |
dan_ackme | 29:b6af04b77a56 | 45 | CommandContext *context = (CommandContext*)commandContext; |
dan_ackme | 29:b6af04b77a56 | 46 | |
dan_ackme | 29:b6af04b77a56 | 47 | if(context->commandLen > 0) |
dan_ackme | 29:b6af04b77a56 | 48 | { |
dan_ackme | 29:b6af04b77a56 | 49 | const TimerTimeout timeout = context->nonBlocking ? 0 : timeoutTimer.remainingMs(context->timeoutMs); |
dan_ackme | 29:b6af04b77a56 | 50 | const int bytesToWrite = context->commandLen; |
dan_ackme | 29:b6af04b77a56 | 51 | const int bytesWritten = serial.write(context->commandPtr, bytesToWrite, timeout); |
dan_ackme | 29:b6af04b77a56 | 52 | context->commandPtr += bytesWritten; |
dan_ackme | 29:b6af04b77a56 | 53 | context->commandLen -= bytesWritten; |
dan_ackme | 29:b6af04b77a56 | 54 | if(bytesToWrite != bytesWritten) |
dan_ackme | 29:b6af04b77a56 | 55 | { |
dan_ackme | 29:b6af04b77a56 | 56 | if(timeoutTimer.timedOut(context->timeoutMs)) |
dan_ackme | 29:b6af04b77a56 | 57 | { |
dan_ackme | 29:b6af04b77a56 | 58 | issueCommandCallback(WICONNECT_TIMEOUT); |
dan_ackme | 29:b6af04b77a56 | 59 | return WICONNECT_TIMEOUT; |
dan_ackme | 29:b6af04b77a56 | 60 | } |
dan_ackme | 29:b6af04b77a56 | 61 | else |
dan_ackme | 29:b6af04b77a56 | 62 | { |
dan_ackme | 29:b6af04b77a56 | 63 | return WICONNECT_PROCESSING; |
dan_ackme | 29:b6af04b77a56 | 64 | } |
dan_ackme | 29:b6af04b77a56 | 65 | } |
dan_ackme | 29:b6af04b77a56 | 66 | } |
dan_ackme | 29:b6af04b77a56 | 67 | |
dan_ackme | 29:b6af04b77a56 | 68 | while(context->reader.isValid()) |
dan_ackme | 29:b6af04b77a56 | 69 | { |
dan_ackme | 29:b6af04b77a56 | 70 | if(context->bytesToWrite == 0) |
dan_ackme | 29:b6af04b77a56 | 71 | { |
dan_ackme | 29:b6af04b77a56 | 72 | context->responseBufferPtr = context->responseBuffer; |
dan_ackme | 29:b6af04b77a56 | 73 | if(WICONNECT_FAILED(result, context->reader.call(context->user, context->responseBuffer, context->responseBufferLen, &context->bytesToWrite))) |
dan_ackme | 29:b6af04b77a56 | 74 | { |
dan_ackme | 29:b6af04b77a56 | 75 | issueCommandCallback(result); |
dan_ackme | 29:b6af04b77a56 | 76 | return result; |
dan_ackme | 29:b6af04b77a56 | 77 | } |
dan_ackme | 29:b6af04b77a56 | 78 | else if(context->bytesToWrite == EOF) |
dan_ackme | 29:b6af04b77a56 | 79 | { |
dan_ackme | 29:b6af04b77a56 | 80 | context->reader.setInvalid(); |
dan_ackme | 29:b6af04b77a56 | 81 | context->bytesToWrite = 0; |
dan_ackme | 29:b6af04b77a56 | 82 | context->responseBufferPtr = context->responseBuffer; |
dan_ackme | 29:b6af04b77a56 | 83 | break; |
dan_ackme | 29:b6af04b77a56 | 84 | } |
dan_ackme | 29:b6af04b77a56 | 85 | else |
dan_ackme | 29:b6af04b77a56 | 86 | { |
dan_ackme | 29:b6af04b77a56 | 87 | timeoutTimer.reset(); |
dan_ackme | 29:b6af04b77a56 | 88 | } |
dan_ackme | 29:b6af04b77a56 | 89 | } |
dan_ackme | 29:b6af04b77a56 | 90 | if(context->bytesToWrite > 0) |
dan_ackme | 29:b6af04b77a56 | 91 | { |
dan_ackme | 29:b6af04b77a56 | 92 | const TimerTimeout timeout = context->nonBlocking ? 0 : timeoutTimer.remainingMs(context->timeoutMs); |
dan_ackme | 29:b6af04b77a56 | 93 | const int bytesToWrite = context->bytesToWrite; |
dan_ackme | 29:b6af04b77a56 | 94 | const int bytesWritten = serial.write(context->responseBufferPtr, bytesToWrite, timeout); |
dan_ackme | 29:b6af04b77a56 | 95 | context->responseBufferPtr += bytesWritten; |
dan_ackme | 29:b6af04b77a56 | 96 | context->bytesToWrite -= bytesWritten; |
dan_ackme | 29:b6af04b77a56 | 97 | if(bytesToWrite != bytesWritten) |
dan_ackme | 29:b6af04b77a56 | 98 | { |
dan_ackme | 29:b6af04b77a56 | 99 | if(timeoutTimer.timedOut(context->timeoutMs)) |
dan_ackme | 29:b6af04b77a56 | 100 | { |
dan_ackme | 29:b6af04b77a56 | 101 | issueCommandCallback(WICONNECT_TIMEOUT); |
dan_ackme | 29:b6af04b77a56 | 102 | return WICONNECT_TIMEOUT; |
dan_ackme | 29:b6af04b77a56 | 103 | } |
dan_ackme | 29:b6af04b77a56 | 104 | else |
dan_ackme | 29:b6af04b77a56 | 105 | { |
dan_ackme | 29:b6af04b77a56 | 106 | return WICONNECT_PROCESSING; |
dan_ackme | 29:b6af04b77a56 | 107 | } |
dan_ackme | 29:b6af04b77a56 | 108 | } |
dan_ackme | 29:b6af04b77a56 | 109 | } |
dan_ackme | 29:b6af04b77a56 | 110 | } |
dan_ackme | 29:b6af04b77a56 | 111 | |
dan_ackme | 29:b6af04b77a56 | 112 | result = receiveResponse(); |
dan_ackme | 29:b6af04b77a56 | 113 | if(result == WICONNECT_PROCESSING && !context->nonBlocking) |
dan_ackme | 29:b6af04b77a56 | 114 | { |
dan_ackme | 29:b6af04b77a56 | 115 | goto start; |
dan_ackme | 29:b6af04b77a56 | 116 | } |
dan_ackme | 29:b6af04b77a56 | 117 | return result; |
dan_ackme | 29:b6af04b77a56 | 118 | } |
dan_ackme | 29:b6af04b77a56 | 119 | |
dan_ackme | 29:b6af04b77a56 | 120 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 121 | WiconnectResult Wiconnect::receiveResponse() |
dan_ackme | 29:b6af04b77a56 | 122 | { |
dan_ackme | 29:b6af04b77a56 | 123 | loop: |
dan_ackme | 29:b6af04b77a56 | 124 | WiconnectResult result = receivePacket(); |
dan_ackme | 29:b6af04b77a56 | 125 | |
dan_ackme | 29:b6af04b77a56 | 126 | if(result == WICONNECT_PROCESSING) |
dan_ackme | 29:b6af04b77a56 | 127 | { |
dan_ackme | 29:b6af04b77a56 | 128 | } |
dan_ackme | 29:b6af04b77a56 | 129 | else if(result == WICONNECT_SUCCESS) |
dan_ackme | 29:b6af04b77a56 | 130 | { |
dan_ackme | 29:b6af04b77a56 | 131 | CommandHeader *header = (CommandHeader*)commandHeaderBuffer; |
dan_ackme | 29:b6af04b77a56 | 132 | CommandContext *context = (CommandContext*)commandContext; |
dan_ackme | 29:b6af04b77a56 | 133 | |
dan_ackme | 29:b6af04b77a56 | 134 | // TODO: need to notify safemode |
dan_ackme | 29:b6af04b77a56 | 135 | |
dan_ackme | 29:b6af04b77a56 | 136 | if(header->response_type == WICONNECT_CMD_TYPE_REPLY || header->response_type == WICONNECT_CMD_TYPE_SAFEMODE) |
dan_ackme | 29:b6af04b77a56 | 137 | { |
dan_ackme | 29:b6af04b77a56 | 138 | if(header->response_code != WICONNECT_CMD_SUCCESS) |
dan_ackme | 29:b6af04b77a56 | 139 | { |
dan_ackme | 29:b6af04b77a56 | 140 | DEBUG_CMD_ERROR(header->response_code); |
dan_ackme | 29:b6af04b77a56 | 141 | flush(); |
dan_ackme | 29:b6af04b77a56 | 142 | issueCommandCallback(WICONNECT_CMD_RESPONSE_ERROR); |
dan_ackme | 29:b6af04b77a56 | 143 | return WICONNECT_CMD_RESPONSE_ERROR; |
dan_ackme | 29:b6af04b77a56 | 144 | } |
dan_ackme | 29:b6af04b77a56 | 145 | else if(header->response_len > 0) |
dan_ackme | 29:b6af04b77a56 | 146 | { |
dan_ackme | 29:b6af04b77a56 | 147 | DEBUG_CMD_RESPONSE(context->responseBuffer); |
dan_ackme | 29:b6af04b77a56 | 148 | |
dan_ackme | 29:b6af04b77a56 | 149 | if(header->response_len < (uint16_t)context->responseBufferLen) |
dan_ackme | 29:b6af04b77a56 | 150 | { |
dan_ackme | 29:b6af04b77a56 | 151 | context->responseBuffer[header->response_len] = 0; |
dan_ackme | 29:b6af04b77a56 | 152 | } |
dan_ackme | 29:b6af04b77a56 | 153 | } |
dan_ackme | 29:b6af04b77a56 | 154 | else |
dan_ackme | 29:b6af04b77a56 | 155 | { |
dan_ackme | 29:b6af04b77a56 | 156 | *context->responseBuffer = 0; |
dan_ackme | 29:b6af04b77a56 | 157 | } |
dan_ackme | 29:b6af04b77a56 | 158 | |
dan_ackme | 29:b6af04b77a56 | 159 | issueCommandCallback(WICONNECT_SUCCESS); |
dan_ackme | 29:b6af04b77a56 | 160 | |
dan_ackme | 29:b6af04b77a56 | 161 | return WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 162 | } |
dan_ackme | 29:b6af04b77a56 | 163 | else |
dan_ackme | 29:b6af04b77a56 | 164 | { |
dan_ackme | 29:b6af04b77a56 | 165 | DEBUG_CMD_LOG(context->responseBuffer); |
dan_ackme | 29:b6af04b77a56 | 166 | RESET_CMD_HEADER(header); |
dan_ackme | 29:b6af04b77a56 | 167 | context->responseBufferPtr = context->responseBuffer; |
dan_ackme | 29:b6af04b77a56 | 168 | goto loop; |
dan_ackme | 29:b6af04b77a56 | 169 | } |
dan_ackme | 29:b6af04b77a56 | 170 | } |
dan_ackme | 29:b6af04b77a56 | 171 | else |
dan_ackme | 29:b6af04b77a56 | 172 | { |
dan_ackme | 29:b6af04b77a56 | 173 | issueCommandCallback(result); |
dan_ackme | 29:b6af04b77a56 | 174 | } |
dan_ackme | 29:b6af04b77a56 | 175 | |
dan_ackme | 29:b6af04b77a56 | 176 | return result; |
dan_ackme | 29:b6af04b77a56 | 177 | } |
dan_ackme | 29:b6af04b77a56 | 178 | |
dan_ackme | 29:b6af04b77a56 | 179 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 180 | WiconnectResult Wiconnect::receivePacket() |
dan_ackme | 29:b6af04b77a56 | 181 | { |
dan_ackme | 29:b6af04b77a56 | 182 | CommandHeader *header = (CommandHeader*)commandHeaderBuffer; |
dan_ackme | 29:b6af04b77a56 | 183 | CommandContext *context = (CommandContext*)commandContext; |
dan_ackme | 29:b6af04b77a56 | 184 | if(header->bytes_remaining > 0) |
dan_ackme | 29:b6af04b77a56 | 185 | { |
dan_ackme | 29:b6af04b77a56 | 186 | uint16_t bytesReceived; |
dan_ackme | 29:b6af04b77a56 | 187 | static uint8_t buffer[WICONNECT_HEADER_LENGTH]; |
dan_ackme | 29:b6af04b77a56 | 188 | |
dan_ackme | 29:b6af04b77a56 | 189 | while(header->bytes_remaining > 0) |
dan_ackme | 29:b6af04b77a56 | 190 | { |
dan_ackme | 29:b6af04b77a56 | 191 | const TimerTimeout timeout = context->nonBlocking ? 0 : timeoutTimer.remainingMs(context->timeoutMs); |
dan_ackme | 29:b6af04b77a56 | 192 | bytesReceived = serial.read((char*)buffer, header->bytes_remaining, timeout); |
dan_ackme | 29:b6af04b77a56 | 193 | if(bytesReceived == 0) |
dan_ackme | 29:b6af04b77a56 | 194 | { |
dan_ackme | 29:b6af04b77a56 | 195 | return timeoutTimer.timedOut(context->timeoutMs) ? WICONNECT_TIMEOUT : WICONNECT_PROCESSING; |
dan_ackme | 29:b6af04b77a56 | 196 | } |
dan_ackme | 29:b6af04b77a56 | 197 | |
dan_ackme | 29:b6af04b77a56 | 198 | for(uint8_t *ptr = buffer; bytesReceived > 0; ++ptr) |
dan_ackme | 29:b6af04b77a56 | 199 | { |
dan_ackme | 29:b6af04b77a56 | 200 | if(header->response_type == WICONNECT_CMD_TYPE_NULL) |
dan_ackme | 29:b6af04b77a56 | 201 | { |
dan_ackme | 29:b6af04b77a56 | 202 | if( *ptr == WICONNECT_CMD_TYPE_REPLY || |
dan_ackme | 29:b6af04b77a56 | 203 | *ptr == WICONNECT_CMD_TYPE_LOG || |
dan_ackme | 29:b6af04b77a56 | 204 | *ptr == WICONNECT_CMD_TYPE_SAFEMODE) |
dan_ackme | 29:b6af04b77a56 | 205 | { |
dan_ackme | 29:b6af04b77a56 | 206 | header->response_type = (ResponseType)*ptr; |
dan_ackme | 29:b6af04b77a56 | 207 | -- header->bytes_remaining; |
dan_ackme | 29:b6af04b77a56 | 208 | } |
dan_ackme | 29:b6af04b77a56 | 209 | --bytesReceived; |
dan_ackme | 29:b6af04b77a56 | 210 | } |
dan_ackme | 29:b6af04b77a56 | 211 | else if(header->response_code == WICONNECT_CMD_CODE_NULL) |
dan_ackme | 29:b6af04b77a56 | 212 | { |
dan_ackme | 29:b6af04b77a56 | 213 | if(*ptr >= '0' && *ptr <= '7') |
dan_ackme | 29:b6af04b77a56 | 214 | { |
dan_ackme | 29:b6af04b77a56 | 215 | header->response_code = (ResponseCode)(*ptr - '0' + 1); |
dan_ackme | 29:b6af04b77a56 | 216 | --header->bytes_remaining; |
dan_ackme | 29:b6af04b77a56 | 217 | header->len_buffer_ptr = header->len_buffer; |
dan_ackme | 29:b6af04b77a56 | 218 | } |
dan_ackme | 29:b6af04b77a56 | 219 | else |
dan_ackme | 29:b6af04b77a56 | 220 | { |
dan_ackme | 29:b6af04b77a56 | 221 | RESET_CMD_HEADER(header); |
dan_ackme | 29:b6af04b77a56 | 222 | } |
dan_ackme | 29:b6af04b77a56 | 223 | --bytesReceived; |
dan_ackme | 29:b6af04b77a56 | 224 | } |
dan_ackme | 29:b6af04b77a56 | 225 | else if(header->bytes_remaining > 2) |
dan_ackme | 29:b6af04b77a56 | 226 | { |
dan_ackme | 29:b6af04b77a56 | 227 | uint8_t len_chars = MIN((int)bytesReceived, (int)(header->bytes_remaining-2)); |
dan_ackme | 29:b6af04b77a56 | 228 | header->bytes_remaining -= len_chars; |
dan_ackme | 29:b6af04b77a56 | 229 | bytesReceived -= len_chars; |
dan_ackme | 29:b6af04b77a56 | 230 | while(len_chars-- > 0) |
dan_ackme | 29:b6af04b77a56 | 231 | { |
dan_ackme | 29:b6af04b77a56 | 232 | *header->len_buffer_ptr++ = *ptr++; |
dan_ackme | 29:b6af04b77a56 | 233 | } |
dan_ackme | 29:b6af04b77a56 | 234 | --ptr; // need to decrement since the for loop increments |
dan_ackme | 29:b6af04b77a56 | 235 | if(header->bytes_remaining == 2) |
dan_ackme | 29:b6af04b77a56 | 236 | { |
dan_ackme | 29:b6af04b77a56 | 237 | uint32_t packetLen; |
dan_ackme | 29:b6af04b77a56 | 238 | *header->len_buffer_ptr = 0; |
dan_ackme | 29:b6af04b77a56 | 239 | if(!StringUtil::strToUint32((const char*)header->len_buffer, &packetLen)) |
dan_ackme | 29:b6af04b77a56 | 240 | { |
dan_ackme | 29:b6af04b77a56 | 241 | RESET_CMD_HEADER(header); |
dan_ackme | 29:b6af04b77a56 | 242 | } |
dan_ackme | 29:b6af04b77a56 | 243 | else |
dan_ackme | 29:b6af04b77a56 | 244 | { |
dan_ackme | 29:b6af04b77a56 | 245 | if(packetLen > 0) |
dan_ackme | 29:b6af04b77a56 | 246 | { |
dan_ackme | 29:b6af04b77a56 | 247 | if(packetLen <= 2) |
dan_ackme | 29:b6af04b77a56 | 248 | { |
dan_ackme | 29:b6af04b77a56 | 249 | return WICONNECT_CMD_RESPONSE_ERROR; |
dan_ackme | 29:b6af04b77a56 | 250 | } |
dan_ackme | 29:b6af04b77a56 | 251 | packetLen -= 2; |
dan_ackme | 29:b6af04b77a56 | 252 | } |
dan_ackme | 29:b6af04b77a56 | 253 | if((int)packetLen > context->responseBufferLen) |
dan_ackme | 29:b6af04b77a56 | 254 | { |
dan_ackme | 29:b6af04b77a56 | 255 | DEBUG_ERROR("Packet larger than response buffer: %d > %d", packetLen, context->responseBufferLen); |
dan_ackme | 29:b6af04b77a56 | 256 | return WICONNECT_OVERFLOW; |
dan_ackme | 29:b6af04b77a56 | 257 | } |
dan_ackme | 29:b6af04b77a56 | 258 | header->response_len = (uint16_t)packetLen; |
dan_ackme | 29:b6af04b77a56 | 259 | context->bytesToRead = packetLen; |
dan_ackme | 29:b6af04b77a56 | 260 | } |
dan_ackme | 29:b6af04b77a56 | 261 | } |
dan_ackme | 29:b6af04b77a56 | 262 | } |
dan_ackme | 29:b6af04b77a56 | 263 | else if(header->bytes_remaining == 2) |
dan_ackme | 29:b6af04b77a56 | 264 | { |
dan_ackme | 29:b6af04b77a56 | 265 | --bytesReceived; |
dan_ackme | 29:b6af04b77a56 | 266 | if(*ptr == '\r') |
dan_ackme | 29:b6af04b77a56 | 267 | { |
dan_ackme | 29:b6af04b77a56 | 268 | header->bytes_remaining = 1; |
dan_ackme | 29:b6af04b77a56 | 269 | } |
dan_ackme | 29:b6af04b77a56 | 270 | else |
dan_ackme | 29:b6af04b77a56 | 271 | { |
dan_ackme | 29:b6af04b77a56 | 272 | RESET_CMD_HEADER(header); |
dan_ackme | 29:b6af04b77a56 | 273 | } |
dan_ackme | 29:b6af04b77a56 | 274 | } |
dan_ackme | 29:b6af04b77a56 | 275 | else |
dan_ackme | 29:b6af04b77a56 | 276 | { |
dan_ackme | 29:b6af04b77a56 | 277 | --bytesReceived; |
dan_ackme | 29:b6af04b77a56 | 278 | if(*ptr == '\n') |
dan_ackme | 29:b6af04b77a56 | 279 | { |
dan_ackme | 29:b6af04b77a56 | 280 | header->bytes_remaining = 0; |
dan_ackme | 29:b6af04b77a56 | 281 | break; |
dan_ackme | 29:b6af04b77a56 | 282 | } |
dan_ackme | 29:b6af04b77a56 | 283 | else |
dan_ackme | 29:b6af04b77a56 | 284 | { |
dan_ackme | 29:b6af04b77a56 | 285 | RESET_CMD_HEADER(header); |
dan_ackme | 29:b6af04b77a56 | 286 | } |
dan_ackme | 29:b6af04b77a56 | 287 | } |
dan_ackme | 29:b6af04b77a56 | 288 | } |
dan_ackme | 29:b6af04b77a56 | 289 | } |
dan_ackme | 29:b6af04b77a56 | 290 | } |
dan_ackme | 29:b6af04b77a56 | 291 | |
dan_ackme | 29:b6af04b77a56 | 292 | while(context->bytesToRead > 0) |
dan_ackme | 29:b6af04b77a56 | 293 | { |
dan_ackme | 29:b6af04b77a56 | 294 | const TimerTimeout timeout = context->nonBlocking ? 0 : timeoutTimer.remainingMs(context->timeoutMs); |
dan_ackme | 29:b6af04b77a56 | 295 | const int bytesToRead = context->bytesToRead; |
dan_ackme | 29:b6af04b77a56 | 296 | const int bytesReceived = serial.read(context->responseBufferPtr, bytesToRead, timeout); |
dan_ackme | 29:b6af04b77a56 | 297 | context->responseBufferPtr += bytesReceived; |
dan_ackme | 29:b6af04b77a56 | 298 | context->bytesToRead -= bytesReceived; |
dan_ackme | 29:b6af04b77a56 | 299 | |
dan_ackme | 29:b6af04b77a56 | 300 | if(bytesReceived != bytesToRead) |
dan_ackme | 29:b6af04b77a56 | 301 | { |
dan_ackme | 29:b6af04b77a56 | 302 | return timeoutTimer.timedOut(context->timeoutMs) ? WICONNECT_TIMEOUT : WICONNECT_PROCESSING; |
dan_ackme | 29:b6af04b77a56 | 303 | } |
dan_ackme | 29:b6af04b77a56 | 304 | else if(context->bytesToRead == 0) |
dan_ackme | 29:b6af04b77a56 | 305 | { |
dan_ackme | 29:b6af04b77a56 | 306 | char buf[2]; |
dan_ackme | 29:b6af04b77a56 | 307 | int bytesRemaining = 2; |
dan_ackme | 29:b6af04b77a56 | 308 | *context->responseBufferPtr = 0; |
dan_ackme | 29:b6af04b77a56 | 309 | |
dan_ackme | 29:b6af04b77a56 | 310 | // read the trailing \r\n |
dan_ackme | 29:b6af04b77a56 | 311 | while(bytesRemaining > 0) |
dan_ackme | 29:b6af04b77a56 | 312 | { |
dan_ackme | 29:b6af04b77a56 | 313 | const int bytesReceived = serial.read(buf, bytesRemaining, 0); |
dan_ackme | 29:b6af04b77a56 | 314 | bytesRemaining -= bytesReceived; |
dan_ackme | 29:b6af04b77a56 | 315 | |
dan_ackme | 29:b6af04b77a56 | 316 | if(bytesRemaining > 0 && timeoutTimer.timedOut(context->timeoutMs)) |
dan_ackme | 29:b6af04b77a56 | 317 | { |
dan_ackme | 29:b6af04b77a56 | 318 | return WICONNECT_TIMEOUT; |
dan_ackme | 29:b6af04b77a56 | 319 | } |
dan_ackme | 29:b6af04b77a56 | 320 | } |
dan_ackme | 29:b6af04b77a56 | 321 | } |
dan_ackme | 29:b6af04b77a56 | 322 | } |
dan_ackme | 29:b6af04b77a56 | 323 | |
dan_ackme | 29:b6af04b77a56 | 324 | return (header->response_code != WICONNECT_CMD_CODE_NULL && |
dan_ackme | 29:b6af04b77a56 | 325 | header->response_type != WICONNECT_CMD_TYPE_NULL && |
dan_ackme | 29:b6af04b77a56 | 326 | context->bytesToRead == 0) ? WICONNECT_SUCCESS : WICONNECT_PROCESSING; |
dan_ackme | 29:b6af04b77a56 | 327 | } |
dan_ackme | 29:b6af04b77a56 | 328 | |
dan_ackme | 29:b6af04b77a56 | 329 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 330 | void Wiconnect::issueCommandCallback(WiconnectResult result) |
dan_ackme | 29:b6af04b77a56 | 331 | { |
dan_ackme | 29:b6af04b77a56 | 332 | CommandHeader *header = (CommandHeader*)commandHeaderBuffer; |
dan_ackme | 29:b6af04b77a56 | 333 | CommandContext *context = (CommandContext*)commandContext; |
dan_ackme | 29:b6af04b77a56 | 334 | #ifdef WICONNECT_ASYNC_TIMER_ENABLED |
dan_ackme | 29:b6af04b77a56 | 335 | void *returnPtr = (currentQueuedCommand != NULL) ? (void*)currentQueuedCommand : (void*)context->responseBuffer; |
dan_ackme | 29:b6af04b77a56 | 336 | currentQueuedCommand = NULL; |
dan_ackme | 29:b6af04b77a56 | 337 | commandProcessorTimer.stop(); |
dan_ackme | 29:b6af04b77a56 | 338 | #else |
dan_ackme | 29:b6af04b77a56 | 339 | void *returnPtr = (void*)context->responseBuffer; |
dan_ackme | 29:b6af04b77a56 | 340 | #endif |
dan_ackme | 29:b6af04b77a56 | 341 | |
dan_ackme | 29:b6af04b77a56 | 342 | context->callback.call(result, returnPtr, (void*)(uint32_t)header->response_len); |
dan_ackme | 29:b6af04b77a56 | 343 | commandExecuting = false; |
dan_ackme | 29:b6af04b77a56 | 344 | |
dan_ackme | 29:b6af04b77a56 | 345 | #ifdef WICONNECT_ASYNC_TIMER_ENABLED |
dan_ackme | 29:b6af04b77a56 | 346 | processNextQueuedCommand(); |
dan_ackme | 29:b6af04b77a56 | 347 | #endif |
dan_ackme | 29:b6af04b77a56 | 348 | } |
dan_ackme | 29:b6af04b77a56 | 349 | |
dan_ackme | 29:b6af04b77a56 | 350 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 351 | void Wiconnect::stopCurrentCommand() |
dan_ackme | 29:b6af04b77a56 | 352 | { |
dan_ackme | 29:b6af04b77a56 | 353 | internalProcessingState = 0; |
dan_ackme | 29:b6af04b77a56 | 354 | issueCommandCallback(WICONNECT_ABORTED); |
dan_ackme | 29:b6af04b77a56 | 355 | } |
dan_ackme | 29:b6af04b77a56 | 356 |