local fork

Dependencies:   Socket USBHostWANDongle_bleedingedge lwip-sys lwip

Dependents:   Encrypted

Fork of VodafoneUSBModem_bleedingedge by Donatien Garnier

Committer:
donatien
Date:
Thu Nov 01 13:01:03 2012 +0000
Revision:
59:0bcb8c5216d4
Parent:
58:763eefc845b1
Child:
60:6f42a974eea6
Fixed race condition within the timeout mechanism of the ATCommandsInterface.cpp module; Added retries for setting the PDP context

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:3b2f052c333b 1 /* ATCommandsInterface.cpp */
donatien 22:06fb2a93a1f6 2 /* Copyright (C) 2012 mbed.org, MIT License
donatien 22:06fb2a93a1f6 3 *
donatien 22:06fb2a93a1f6 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
donatien 22:06fb2a93a1f6 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
donatien 22:06fb2a93a1f6 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
donatien 22:06fb2a93a1f6 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
donatien 22:06fb2a93a1f6 8 * furnished to do so, subject to the following conditions:
donatien 22:06fb2a93a1f6 9 *
donatien 22:06fb2a93a1f6 10 * The above copyright notice and this permission notice shall be included in all copies or
donatien 22:06fb2a93a1f6 11 * substantial portions of the Software.
donatien 22:06fb2a93a1f6 12 *
donatien 22:06fb2a93a1f6 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
donatien 22:06fb2a93a1f6 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
donatien 22:06fb2a93a1f6 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
donatien 22:06fb2a93a1f6 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
donatien 22:06fb2a93a1f6 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
donatien 22:06fb2a93a1f6 18 */
donatien 0:3b2f052c333b 19
donatien 55:07b9b48cfe09 20 #define __DEBUG__ 2 //ERR+WARN
donatien 0:3b2f052c333b 21 #ifndef __MODULE__
donatien 0:3b2f052c333b 22 #define __MODULE__ "ATCommandsInterface.cpp"
donatien 0:3b2f052c333b 23 #endif
donatien 0:3b2f052c333b 24
donatien 0:3b2f052c333b 25 #include "core/fwk.h"
donatien 0:3b2f052c333b 26
donatien 0:3b2f052c333b 27 #include <cstdio>
donatien 0:3b2f052c333b 28 //#include <cstring> //For memset, strstr...
donatien 0:3b2f052c333b 29
donatien 0:3b2f052c333b 30 using std::memmove;
donatien 0:3b2f052c333b 31
donatien 0:3b2f052c333b 32 #include "ATCommandsInterface.h"
donatien 0:3b2f052c333b 33
donatien 0:3b2f052c333b 34 ATCommandsInterface::ATCommandsInterface(IOStream* pStream) :
donatien 0:3b2f052c333b 35 m_pStream(pStream), m_open(false), m_env2AT(), m_AT2Env(), m_processingMtx(),
donatien 0:3b2f052c333b 36 m_processingThread(&ATCommandsInterface::staticCallback, this, (osPriority)AT_THREAD_PRIORITY, 4*192),
donatien 57:593fb493172f 37 m_eventsMgmtMtx(), m_eventsProcessingMtx()
donatien 0:3b2f052c333b 38 {
donatien 0:3b2f052c333b 39 memset(m_eventsHandlers, 0, MAX_AT_EVENTS_HANDLERS * sizeof(IATEventsHandler*));
donatien 0:3b2f052c333b 40
donatien 0:3b2f052c333b 41 m_processingMtx.lock();
donatien 0:3b2f052c333b 42 }
donatien 0:3b2f052c333b 43
donatien 0:3b2f052c333b 44 //Open connection to AT Interface in order to execute command & register/unregister events
donatien 0:3b2f052c333b 45 int ATCommandsInterface::open()
donatien 0:3b2f052c333b 46 {
donatien 0:3b2f052c333b 47 if( m_open )
donatien 0:3b2f052c333b 48 {
donatien 0:3b2f052c333b 49 WARN("AT interface is already open");
donatien 0:3b2f052c333b 50 return OK;
donatien 0:3b2f052c333b 51 }
donatien 0:3b2f052c333b 52 DBG("Opening AT interface");
donatien 0:3b2f052c333b 53 //Start processing
donatien 0:3b2f052c333b 54 m_processingThread.signal_set(AT_SIG_PROCESSING_START);
donatien 0:3b2f052c333b 55
donatien 0:3b2f052c333b 56 m_processingMtx.unlock();
donatien 0:3b2f052c333b 57
donatien 0:3b2f052c333b 58 m_open = true;
donatien 0:3b2f052c333b 59
donatien 0:3b2f052c333b 60 DBG("AT interface opened");
donatien 57:593fb493172f 61
donatien 0:3b2f052c333b 62 return OK;
donatien 0:3b2f052c333b 63 }
donatien 0:3b2f052c333b 64
donatien 57:593fb493172f 65 //Initialize AT link & start events processing
donatien 0:3b2f052c333b 66 int ATCommandsInterface::init()
donatien 0:3b2f052c333b 67 {
donatien 0:3b2f052c333b 68 DBG("Sending ATZ E1 V1");
donatien 57:593fb493172f 69
donatien 57:593fb493172f 70 //Lock transaction mutex
donatien 57:593fb493172f 71 m_transactionMtx.lock();
donatien 57:593fb493172f 72
donatien 0:3b2f052c333b 73 //Should we flush m_pStream at this point ???
donatien 0:3b2f052c333b 74 int err;
donatien 0:3b2f052c333b 75 int tries = 5;
donatien 0:3b2f052c333b 76 do
donatien 0:3b2f052c333b 77 {
donatien 57:593fb493172f 78 err = executeInternal("ATZ E1 V1", this, NULL, 3000); //Enable echo and verbosity
donatien 0:3b2f052c333b 79 if(err && tries)
donatien 0:3b2f052c333b 80 {
donatien 0:3b2f052c333b 81 WARN("No response, trying again");
donatien 0:3b2f052c333b 82 Thread::wait(1000); //Give dongle time to recover
donatien 0:3b2f052c333b 83 }
donatien 0:3b2f052c333b 84 } while(err && tries--);
donatien 0:3b2f052c333b 85 if( err )
donatien 0:3b2f052c333b 86 {
donatien 0:3b2f052c333b 87 ERR("Sending ATZ E1 V1 returned with err code %d", err);
donatien 57:593fb493172f 88 m_transactionMtx.unlock();
donatien 0:3b2f052c333b 89 return err;
donatien 0:3b2f052c333b 90 }
donatien 57:593fb493172f 91
donatien 57:593fb493172f 92 //Enable events handling and execute events enabling commands
donatien 57:593fb493172f 93 enableEvents();
donatien 0:3b2f052c333b 94
donatien 0:3b2f052c333b 95 DBG("AT interface initialized");
donatien 57:593fb493172f 96
donatien 57:593fb493172f 97 //Unlock transaction mutex
donatien 57:593fb493172f 98 m_transactionMtx.unlock();
donatien 0:3b2f052c333b 99
donatien 0:3b2f052c333b 100 return OK;
donatien 0:3b2f052c333b 101 }
donatien 0:3b2f052c333b 102
donatien 0:3b2f052c333b 103 //Close connection
donatien 0:3b2f052c333b 104 int ATCommandsInterface::close()
donatien 0:3b2f052c333b 105 {
donatien 0:3b2f052c333b 106 if( !m_open )
donatien 0:3b2f052c333b 107 {
donatien 0:3b2f052c333b 108 WARN("AT interface is already closed");
donatien 0:3b2f052c333b 109 return OK;
donatien 0:3b2f052c333b 110 }
donatien 0:3b2f052c333b 111
donatien 0:3b2f052c333b 112 DBG("Closing AT interface");
donatien 59:0bcb8c5216d4 113
donatien 59:0bcb8c5216d4 114 //Lock transaction mutex
donatien 59:0bcb8c5216d4 115 m_transactionMtx.lock();
donatien 59:0bcb8c5216d4 116
donatien 59:0bcb8c5216d4 117 //Disable events handling and advertize this to the events handlers
donatien 59:0bcb8c5216d4 118 disableEvents();
donatien 0:3b2f052c333b 119
donatien 0:3b2f052c333b 120 //Stop processing
donatien 0:3b2f052c333b 121 m_processingThread.signal_set(AT_SIG_PROCESSING_STOP);
donatien 0:3b2f052c333b 122 //m_stopSphre.release();
donatien 0:3b2f052c333b 123
donatien 0:3b2f052c333b 124 int* msg = m_env2AT.alloc(osWaitForever);
donatien 0:3b2f052c333b 125 *msg = AT_STOP;
donatien 0:3b2f052c333b 126 m_env2AT.put(msg); //Used to unstall the process if needed
donatien 0:3b2f052c333b 127
donatien 0:3b2f052c333b 128 //Unlock process routine (abort read)
donatien 0:3b2f052c333b 129 m_pStream->abortRead(); //This is thread-safe
donatien 0:3b2f052c333b 130 m_processingMtx.lock();
donatien 0:3b2f052c333b 131 m_open = false;
donatien 59:0bcb8c5216d4 132
donatien 59:0bcb8c5216d4 133 //Unlock transaction mutex
donatien 59:0bcb8c5216d4 134 m_transactionMtx.unlock();
donatien 0:3b2f052c333b 135
donatien 0:3b2f052c333b 136 DBG("AT interface closed");
donatien 0:3b2f052c333b 137 return OK;
donatien 0:3b2f052c333b 138 }
donatien 0:3b2f052c333b 139
donatien 0:3b2f052c333b 140 bool ATCommandsInterface::isOpen()
donatien 0:3b2f052c333b 141 {
donatien 0:3b2f052c333b 142 return m_open;
donatien 0:3b2f052c333b 143 }
donatien 0:3b2f052c333b 144
donatien 0:3b2f052c333b 145 int ATCommandsInterface::executeSimple(const char* command, ATResult* pResult, uint32_t timeout/*=1000*/)
donatien 0:3b2f052c333b 146 {
donatien 0:3b2f052c333b 147 return execute(command, this, pResult, timeout);
donatien 0:3b2f052c333b 148 }
donatien 0:3b2f052c333b 149
donatien 0:3b2f052c333b 150 int ATCommandsInterface::execute(const char* command, IATCommandsProcessor* pProcessor, ATResult* pResult, uint32_t timeout/*=1000*/)
donatien 0:3b2f052c333b 151 {
donatien 0:3b2f052c333b 152 if(!m_open)
donatien 0:3b2f052c333b 153 {
donatien 0:3b2f052c333b 154 WARN("Interface is not open!");
donatien 0:3b2f052c333b 155 return NET_INVALID;
donatien 0:3b2f052c333b 156 }
donatien 0:3b2f052c333b 157
donatien 0:3b2f052c333b 158 //Lock transaction mutex
donatien 0:3b2f052c333b 159 m_transactionMtx.lock();
donatien 57:593fb493172f 160
donatien 57:593fb493172f 161 disableEvents(); //Disable unsollicited result codes
donatien 57:593fb493172f 162 int ret = executeInternal(command, pProcessor, pResult, timeout);
donatien 57:593fb493172f 163 enableEvents(); //Re-enable unsollicited result codes whatever the result of the command is
donatien 57:593fb493172f 164
donatien 57:593fb493172f 165 //Unlock transaction mutex
donatien 57:593fb493172f 166 m_transactionMtx.unlock();
donatien 57:593fb493172f 167
donatien 57:593fb493172f 168 return ret;
donatien 57:593fb493172f 169 }
donatien 57:593fb493172f 170
donatien 57:593fb493172f 171 int ATCommandsInterface::registerEventsHandler(IATEventsHandler* pHdlr)
donatien 57:593fb493172f 172 {
donatien 57:593fb493172f 173 m_eventsMgmtMtx.lock();
donatien 57:593fb493172f 174 m_eventsProcessingMtx.lock();
donatien 57:593fb493172f 175 for(int i = 0; i < MAX_AT_EVENTS_HANDLERS; i++) //Find a free slot
donatien 57:593fb493172f 176 {
donatien 57:593fb493172f 177 if( m_eventsHandlers[i] == NULL )
donatien 57:593fb493172f 178 {
donatien 57:593fb493172f 179 m_eventsHandlers[i] = pHdlr;
donatien 57:593fb493172f 180 m_eventsProcessingMtx.unlock();
donatien 57:593fb493172f 181 m_eventsMgmtMtx.unlock();
donatien 57:593fb493172f 182 return OK;
donatien 57:593fb493172f 183 }
donatien 57:593fb493172f 184 }
donatien 57:593fb493172f 185 m_eventsProcessingMtx.unlock();
donatien 57:593fb493172f 186 m_eventsMgmtMtx.unlock();
donatien 57:593fb493172f 187 return NET_OOM; //No room left
donatien 57:593fb493172f 188 }
donatien 57:593fb493172f 189
donatien 57:593fb493172f 190 int ATCommandsInterface::deregisterEventsHandler(IATEventsHandler* pHdlr)
donatien 57:593fb493172f 191 {
donatien 57:593fb493172f 192 m_eventsMgmtMtx.lock();
donatien 57:593fb493172f 193 m_eventsProcessingMtx.lock();
donatien 57:593fb493172f 194 for(int i = 0; i < MAX_AT_EVENTS_HANDLERS; i++) //Find handler in list
donatien 57:593fb493172f 195 {
donatien 57:593fb493172f 196 if( m_eventsHandlers[i] == pHdlr )
donatien 57:593fb493172f 197 {
donatien 57:593fb493172f 198 m_eventsHandlers[i] = NULL;
donatien 57:593fb493172f 199 m_eventsProcessingMtx.unlock();
donatien 57:593fb493172f 200 m_eventsMgmtMtx.unlock();
donatien 57:593fb493172f 201 return OK;
donatien 57:593fb493172f 202 }
donatien 57:593fb493172f 203 }
donatien 57:593fb493172f 204 m_eventsProcessingMtx.unlock();
donatien 57:593fb493172f 205 m_eventsMgmtMtx.unlock();
donatien 57:593fb493172f 206 return NET_NOTFOUND; //Not found
donatien 57:593fb493172f 207 }
donatien 57:593fb493172f 208
donatien 57:593fb493172f 209 //Private methods
donatien 57:593fb493172f 210
donatien 57:593fb493172f 211 int ATCommandsInterface::executeInternal(const char* command, IATCommandsProcessor* pProcessor, ATResult* pResult, uint32_t timeout/*=1000*/)
donatien 57:593fb493172f 212 {
donatien 57:593fb493172f 213 DBG("Executing command %s", command);
donatien 0:3b2f052c333b 214
donatien 0:3b2f052c333b 215 //Discard previous result if it arrived too late
donatien 0:3b2f052c333b 216 osEvent evt = m_AT2Env.get(0);
donatien 0:3b2f052c333b 217
donatien 0:3b2f052c333b 218 if(evt.status == osEventMail)
donatien 0:3b2f052c333b 219 {
donatien 0:3b2f052c333b 220 m_AT2Env.free((int*)evt.value.p);
donatien 0:3b2f052c333b 221 WARN("Previous result discarded");
donatien 0:3b2f052c333b 222 }
donatien 0:3b2f052c333b 223
donatien 0:3b2f052c333b 224 //Send params to the process routine
donatien 0:3b2f052c333b 225 m_transactionCommand = command;
donatien 0:3b2f052c333b 226 if(pProcessor != NULL)
donatien 0:3b2f052c333b 227 {
donatien 0:3b2f052c333b 228 m_pTransactionProcessor = pProcessor;
donatien 0:3b2f052c333b 229 }
donatien 0:3b2f052c333b 230 else
donatien 0:3b2f052c333b 231 {
donatien 0:3b2f052c333b 232 m_pTransactionProcessor = this; //Use default behaviour
donatien 0:3b2f052c333b 233 }
donatien 0:3b2f052c333b 234
donatien 0:3b2f052c333b 235 DBG("Sending command ready signal to AT thread & aborting current blocking read operation");
donatien 0:3b2f052c333b 236
donatien 0:3b2f052c333b 237 //Produce command ready signal
donatien 0:3b2f052c333b 238 int* msg = m_env2AT.alloc(osWaitForever);
donatien 0:3b2f052c333b 239 *msg = AT_CMD_READY;
donatien 0:3b2f052c333b 240 m_env2AT.put(msg);
donatien 0:3b2f052c333b 241
donatien 0:3b2f052c333b 242 DBG("Trying to enter abortRead()");
donatien 0:3b2f052c333b 243 //Unlock process routine (abort read)
donatien 0:3b2f052c333b 244 m_pStream->abortRead(); //This is thread-safe
donatien 0:3b2f052c333b 245
donatien 0:3b2f052c333b 246 //Wait for a result (get result message)
donatien 0:3b2f052c333b 247 evt = m_AT2Env.get(timeout);
donatien 0:3b2f052c333b 248
donatien 0:3b2f052c333b 249 if(evt.status != osEventMail)
donatien 0:3b2f052c333b 250 {
donatien 0:3b2f052c333b 251 //Cancel request
donatien 0:3b2f052c333b 252 msg = m_env2AT.alloc(osWaitForever);
donatien 0:3b2f052c333b 253 *msg = AT_TIMEOUT;
donatien 0:3b2f052c333b 254 m_env2AT.put(msg);
donatien 0:3b2f052c333b 255
donatien 0:3b2f052c333b 256 DBG("Trying to enter abortRead()");
donatien 0:3b2f052c333b 257 //Unlock process routine (abort read)
donatien 0:3b2f052c333b 258 m_pStream->abortRead(); //This is thread-safe
donatien 59:0bcb8c5216d4 259
donatien 59:0bcb8c5216d4 260 //Wait for acknowledge
donatien 59:0bcb8c5216d4 261 int msgResult;
donatien 59:0bcb8c5216d4 262 do
donatien 59:0bcb8c5216d4 263 {
donatien 59:0bcb8c5216d4 264 evt = m_AT2Env.get(osWaitForever);
donatien 59:0bcb8c5216d4 265 msgResult = *((int*) evt.value.p);
donatien 59:0bcb8c5216d4 266 m_AT2Env.free((int*)evt.value.p);
donatien 59:0bcb8c5216d4 267 } while(msgResult != AT_TIMEOUT);
donatien 0:3b2f052c333b 268
donatien 0:3b2f052c333b 269 WARN("Command returned no message");
donatien 0:3b2f052c333b 270 return NET_TIMEOUT;
donatien 0:3b2f052c333b 271 }
donatien 0:3b2f052c333b 272 DBG("Command returned with message %d", *msg);
donatien 0:3b2f052c333b 273
donatien 0:3b2f052c333b 274 m_AT2Env.free((int*)evt.value.p);
donatien 0:3b2f052c333b 275
donatien 0:3b2f052c333b 276 if(pResult != NULL)
donatien 0:3b2f052c333b 277 {
donatien 0:3b2f052c333b 278 *pResult = m_transactionResult;
donatien 0:3b2f052c333b 279 }
donatien 0:3b2f052c333b 280
donatien 0:3b2f052c333b 281 int ret = ATResultToReturnCode(m_transactionResult);
donatien 0:3b2f052c333b 282 if(ret != OK)
donatien 0:3b2f052c333b 283 {
donatien 0:3b2f052c333b 284 WARN("Command returned AT result %d with code %d", m_transactionResult.result, m_transactionResult.code);
donatien 0:3b2f052c333b 285 }
donatien 0:3b2f052c333b 286
donatien 0:3b2f052c333b 287 DBG("Command returned successfully");
donatien 0:3b2f052c333b 288
donatien 0:3b2f052c333b 289 return ret;
donatien 0:3b2f052c333b 290 }
donatien 0:3b2f052c333b 291
donatien 0:3b2f052c333b 292 int ATCommandsInterface::tryReadLine()
donatien 0:3b2f052c333b 293 {
donatien 0:3b2f052c333b 294 static bool lineDetected = false;
donatien 0:3b2f052c333b 295
donatien 0:3b2f052c333b 296 //Block on serial read or incoming command
donatien 0:3b2f052c333b 297 DBG("Trying to read a new line from stream");
donatien 0:3b2f052c333b 298 int ret = m_pStream->waitAvailable(); //This can be aborted
donatien 0:3b2f052c333b 299 size_t readLen = 0;
donatien 0:3b2f052c333b 300 if(ret == OK)
donatien 0:3b2f052c333b 301 {
donatien 0:3b2f052c333b 302 ret = m_pStream->read((uint8_t*)m_inputBuf + m_inputPos, &readLen, AT_INPUT_BUF_SIZE - 1 - m_inputPos, 0); //Do NOT wait at this point
donatien 0:3b2f052c333b 303 }
donatien 0:3b2f052c333b 304 if(ret == OK)
donatien 0:3b2f052c333b 305 {
donatien 0:3b2f052c333b 306 m_inputPos+=readLen;
donatien 0:3b2f052c333b 307 m_inputBuf[m_inputPos] = '\0'; //Add null terminating character to ease the use of str* functions
donatien 0:3b2f052c333b 308 DBG("In buffer: [%s]", m_inputBuf);
donatien 0:3b2f052c333b 309 }
donatien 0:3b2f052c333b 310
donatien 0:3b2f052c333b 311 if( ret == NET_INTERRUPTED ) //It is worth checking readLen as data might have been read even though the read was interrupted
donatien 0:3b2f052c333b 312 {
donatien 0:3b2f052c333b 313 DBG("Read was interrupted");
donatien 0:3b2f052c333b 314 return NET_INTERRUPTED; //0 chars were read
donatien 0:3b2f052c333b 315 }
donatien 0:3b2f052c333b 316 else if(readLen == 0)
donatien 0:3b2f052c333b 317 {
donatien 0:3b2f052c333b 318 DBG("Nothing read");
donatien 0:3b2f052c333b 319 return OK; //0 chars were read
donatien 0:3b2f052c333b 320 }
donatien 0:3b2f052c333b 321
donatien 0:3b2f052c333b 322 DBG("Trying to process incoming line");
donatien 0:3b2f052c333b 323 bool lineProcessed = false;
donatien 0:3b2f052c333b 324
donatien 0:3b2f052c333b 325 do
donatien 0:3b2f052c333b 326 {
donatien 0:3b2f052c333b 327 lineProcessed = false; //Reset flag
donatien 0:3b2f052c333b 328
donatien 0:3b2f052c333b 329 DBG("New iteration");
donatien 0:3b2f052c333b 330
donatien 0:3b2f052c333b 331 //Look for a new line
donatien 0:3b2f052c333b 332 if(!lineDetected)
donatien 0:3b2f052c333b 333 {
donatien 0:3b2f052c333b 334 DBG("No line detected yet");
donatien 0:3b2f052c333b 335 //Try to look for a starting CRLF
donatien 0:3b2f052c333b 336 char* crPtr = strchr(m_inputBuf, CR);
donatien 0:3b2f052c333b 337 /*
donatien 0:3b2f052c333b 338 Different cases at this point:
donatien 0:3b2f052c333b 339 - CRLF%c sequence: this is the start of a line
donatien 0:3b2f052c333b 340 - CRLFCR(LF) sequence: this is the end of a line (followed by the beginning of the next one)
donatien 0:3b2f052c333b 341 - LF: this is the trailing LF char of the previous line, discard
donatien 0:3b2f052c333b 342 - CR / CRLF incomplete sequence: more data is needed to determine which action to take
donatien 0:3b2f052c333b 343 - %c ... CR sequence: this should be the echo of the previous sequence
donatien 0:3b2f052c333b 344 - %c sequence: This might be the echo of the previous command; more data is needed to determine which action to take
donatien 0:3b2f052c333b 345
donatien 0:3b2f052c333b 346 In every case, move mem at the beginning
donatien 0:3b2f052c333b 347 */
donatien 0:3b2f052c333b 348 if(crPtr != NULL)
donatien 0:3b2f052c333b 349 {
donatien 0:3b2f052c333b 350 DBG("CR char found");
donatien 0:3b2f052c333b 351
donatien 0:3b2f052c333b 352 #if 0
donatien 0:3b2f052c333b 353 //Discard all preceding characters (can do nothing if m_inputBuf == crPtr)
donatien 0:3b2f052c333b 354 memmove(m_inputBuf, crPtr, (m_inputPos + 1) - (crPtr-m_inputBuf)); //Move null-terminating char as well
donatien 0:3b2f052c333b 355 m_inputPos = m_inputPos - (crPtr-m_inputBuf); //Adjust m_inputPos
donatien 0:3b2f052c333b 356 #endif
donatien 0:3b2f052c333b 357
donatien 0:3b2f052c333b 358 //If the line starts with CR, this should be a result code
donatien 0:3b2f052c333b 359 if( crPtr == m_inputBuf )
donatien 0:3b2f052c333b 360 {
donatien 0:3b2f052c333b 361 //To determine the sequence we need at least 3 chars
donatien 0:3b2f052c333b 362 if(m_inputPos >= 3)
donatien 0:3b2f052c333b 363 {
donatien 0:3b2f052c333b 364 //Look for a LF char next to the CR char
donatien 0:3b2f052c333b 365 if(m_inputBuf[1] == LF)
donatien 0:3b2f052c333b 366 {
donatien 0:3b2f052c333b 367 //At this point we can check whether this is the end of a preceding line or the beginning of a new one
donatien 0:3b2f052c333b 368 if(m_inputBuf[2] != CR)
donatien 0:3b2f052c333b 369 {
donatien 0:3b2f052c333b 370 DBG("Beginning of new line found");
donatien 0:3b2f052c333b 371 //Beginning of a line
donatien 0:3b2f052c333b 372 lineDetected = true; //Move to next state-machine step
donatien 0:3b2f052c333b 373 }
donatien 0:3b2f052c333b 374 else
donatien 0:3b2f052c333b 375 {
donatien 0:3b2f052c333b 376 //End of an unprocessed line
donatien 0:3b2f052c333b 377 WARN("End of unprocessed line");
donatien 0:3b2f052c333b 378 }
donatien 0:3b2f052c333b 379 //In both cases discard CRLF
donatien 0:3b2f052c333b 380 DBG("Discarding CRLF");
donatien 0:3b2f052c333b 381 memmove(m_inputBuf, m_inputBuf + 2, (m_inputPos + 1) - 2); //Move null-terminating char as well
donatien 0:3b2f052c333b 382 m_inputPos = m_inputPos - 2; //Adjust m_inputPos
donatien 0:3b2f052c333b 383 }
donatien 0:3b2f052c333b 384 else
donatien 0:3b2f052c333b 385 {
donatien 0:3b2f052c333b 386 //This is completely unexpected, discard the CR char to try to recover good state
donatien 0:3b2f052c333b 387 WARN("Unexpected %c char (%02d code) found after CR char", m_inputBuf[1]);
donatien 0:3b2f052c333b 388 memmove(m_inputBuf, m_inputBuf + 1, (m_inputPos + 1) - 1); //Move null-terminating char as well
donatien 0:3b2f052c333b 389 m_inputPos = m_inputPos - 1; //Adjust m_inputPos
donatien 0:3b2f052c333b 390 }
donatien 0:3b2f052c333b 391 }
donatien 0:3b2f052c333b 392 }
donatien 0:3b2f052c333b 393 //if the line does NOT begin with CR, this can be an echo of the previous command, process it
donatien 0:3b2f052c333b 394 else
donatien 0:3b2f052c333b 395 {
donatien 0:3b2f052c333b 396 int crPos = crPtr - m_inputBuf;
donatien 0:3b2f052c333b 397 int lfOff = 0; //Offset for LF if present
donatien 0:3b2f052c333b 398 DBG("New line found (possible echo of command)");
donatien 0:3b2f052c333b 399 //This is the end of line
donatien 0:3b2f052c333b 400 //Replace m_inputBuf[crPos] with null-terminating char
donatien 0:3b2f052c333b 401 m_inputBuf[crPos] = '\0';
donatien 0:3b2f052c333b 402 //Check if there is a LF char afterwards
donatien 0:3b2f052c333b 403 if(m_inputPos - crPos >= 1)
donatien 0:3b2f052c333b 404 {
donatien 0:3b2f052c333b 405 if(m_inputBuf[crPos+1] == LF)
donatien 0:3b2f052c333b 406 {
donatien 0:3b2f052c333b 407 lfOff++; //We will discard LF char as well
donatien 0:3b2f052c333b 408 }
donatien 0:3b2f052c333b 409 }
donatien 0:3b2f052c333b 410 //Process line
donatien 51:54ca82a7644c 411 int ret = processReadLine();
donatien 51:54ca82a7644c 412 if(ret)
donatien 51:54ca82a7644c 413 {
donatien 51:54ca82a7644c 414 m_inputPos = 0;
donatien 51:54ca82a7644c 415 m_inputBuf[0] = '\0'; //Always have a null-terminating char at start of buffer
donatien 51:54ca82a7644c 416 lineDetected = false;
donatien 51:54ca82a7644c 417 return ret;
donatien 51:54ca82a7644c 418 }
donatien 51:54ca82a7644c 419
donatien 51:54ca82a7644c 420 //If sendData has been called, all incoming data has been discarded
donatien 51:54ca82a7644c 421 if(m_inputPos > 0)
donatien 51:54ca82a7644c 422 {
donatien 51:54ca82a7644c 423 memmove(m_inputBuf, m_inputBuf + crPos + lfOff + 1, (m_inputPos + 1) - (crPos + lfOff + 1)); //Move null-terminating char as well
donatien 51:54ca82a7644c 424 m_inputPos = m_inputPos - (crPos + lfOff + 1); //Adjust m_inputPos
donatien 51:54ca82a7644c 425 }
donatien 0:3b2f052c333b 426 DBG("One line was successfully processed");
donatien 0:3b2f052c333b 427 lineProcessed = true; //Line was processed with success
donatien 0:3b2f052c333b 428 lineDetected = false; //Search now for a new line
donatien 0:3b2f052c333b 429 }
donatien 0:3b2f052c333b 430 }
donatien 0:3b2f052c333b 431 else if(m_inputBuf[0] == LF) //If there is a remaining LF char from the previous line, discard it
donatien 0:3b2f052c333b 432 {
donatien 0:3b2f052c333b 433 DBG("Discarding single LF char");
donatien 0:3b2f052c333b 434 memmove(m_inputBuf, m_inputBuf + 1, (m_inputPos + 1) - 1); //Move null-terminating char as well
donatien 0:3b2f052c333b 435 m_inputPos = m_inputPos - 1; //Adjust m_inputPos
donatien 0:3b2f052c333b 436 }
donatien 0:3b2f052c333b 437 }
donatien 0:3b2f052c333b 438
donatien 0:3b2f052c333b 439 //Look for the end of line
donatien 0:3b2f052c333b 440 if(lineDetected)
donatien 0:3b2f052c333b 441 {
donatien 0:3b2f052c333b 442 DBG("Looking for end of line");
donatien 0:3b2f052c333b 443 //Try to look for a terminating CRLF
donatien 0:3b2f052c333b 444 char* crPtr = strchr(m_inputBuf, CR);
donatien 0:3b2f052c333b 445 /*
donatien 0:3b2f052c333b 446 Different cases at this point:
donatien 0:3b2f052c333b 447 - CRLF sequence: this is the end of the line
donatien 0:3b2f052c333b 448 - CR%c sequence : unexpected
donatien 0:3b2f052c333b 449 - CR incomplete sequence: more data is needed to determine which action to take
donatien 0:3b2f052c333b 450 */
donatien 0:3b2f052c333b 451
donatien 0:3b2f052c333b 452 //Try to look for a '>' (greater than character) that marks an entry prompt
donatien 0:3b2f052c333b 453 char* greaterThanPtr = strchr(m_inputBuf, GD);
donatien 0:3b2f052c333b 454 /*
donatien 0:3b2f052c333b 455 This character must be detected as there is no CRLF sequence at the end of an entry prompt
donatien 0:3b2f052c333b 456 */
donatien 0:3b2f052c333b 457
donatien 0:3b2f052c333b 458 if(crPtr != NULL)
donatien 0:3b2f052c333b 459 {
donatien 0:3b2f052c333b 460 DBG("CR char found");
donatien 0:3b2f052c333b 461 int crPos = crPtr - m_inputBuf;
donatien 0:3b2f052c333b 462 //To determine the sequence we need at least 2 chars
donatien 0:3b2f052c333b 463 if(m_inputPos - crPos >= 2)
donatien 0:3b2f052c333b 464 {
donatien 0:3b2f052c333b 465 //Look for a LF char next to the CR char
donatien 0:3b2f052c333b 466 if(m_inputBuf[crPos + 1] == LF)
donatien 0:3b2f052c333b 467 {
donatien 0:3b2f052c333b 468 DBG("End of new line found");
donatien 0:3b2f052c333b 469 //This is the end of line
donatien 0:3b2f052c333b 470 //Replace m_inputBuf[crPos] with null-terminating char
donatien 0:3b2f052c333b 471 m_inputBuf[crPos] = '\0';
donatien 0:3b2f052c333b 472 //Process line
donatien 0:3b2f052c333b 473 int ret = processReadLine();
donatien 0:3b2f052c333b 474 if(ret)
donatien 0:3b2f052c333b 475 {
donatien 0:3b2f052c333b 476 m_inputPos = 0;
donatien 51:54ca82a7644c 477 m_inputBuf[0] = '\0'; //Always have a null-terminating char at start of buffer
donatien 0:3b2f052c333b 478 lineDetected = false;
donatien 0:3b2f052c333b 479 return ret;
donatien 0:3b2f052c333b 480 }
donatien 0:3b2f052c333b 481
donatien 0:3b2f052c333b 482 //If sendData has been called, all incoming data has been discarded
donatien 0:3b2f052c333b 483 if(m_inputPos > 0)
donatien 0:3b2f052c333b 484 {
donatien 0:3b2f052c333b 485 //Shift remaining data to beginning of buffer
donatien 0:3b2f052c333b 486 memmove(m_inputBuf, m_inputBuf + crPos + 2, (m_inputPos + 1) - (crPos + 2)); //Move null-terminating char as well
donatien 0:3b2f052c333b 487 m_inputPos = m_inputPos - (crPos + 2); //Adjust m_inputPos
donatien 0:3b2f052c333b 488 }
donatien 0:3b2f052c333b 489
donatien 0:3b2f052c333b 490 DBG("One line was successfully processed");
donatien 0:3b2f052c333b 491 lineProcessed = true; //Line was processed with success
donatien 0:3b2f052c333b 492 }
donatien 0:3b2f052c333b 493 else
donatien 0:3b2f052c333b 494 {
donatien 0:3b2f052c333b 495 //This is completely unexpected, discard all chars till the CR char to try to recover good state
donatien 0:3b2f052c333b 496 WARN("Unexpected %c char (%02d code) found in incoming line", m_inputBuf[crPos + 1]);
donatien 0:3b2f052c333b 497 memmove(m_inputBuf, m_inputBuf + crPos + 1, (m_inputPos + 1) - (crPos + 1)); //Move null-terminating char as well
donatien 0:3b2f052c333b 498 m_inputPos = m_inputPos - (crPos + 1); //Adjust m_inputPos
donatien 0:3b2f052c333b 499 }
donatien 0:3b2f052c333b 500 lineDetected = false; //In both case search now for a new line
donatien 0:3b2f052c333b 501 }
donatien 0:3b2f052c333b 502 }
donatien 0:3b2f052c333b 503 else if(greaterThanPtr != NULL)
donatien 0:3b2f052c333b 504 {
donatien 0:3b2f052c333b 505 DBG("> char found");
donatien 0:3b2f052c333b 506 int gdPos = greaterThanPtr - m_inputBuf;
donatien 0:3b2f052c333b 507 //To determine the sequence we need at least 2 chars
donatien 0:3b2f052c333b 508 if(m_inputPos - gdPos >= 2)
donatien 0:3b2f052c333b 509 {
donatien 0:3b2f052c333b 510 //Look for a space char next to the GD char
donatien 0:3b2f052c333b 511 if(m_inputBuf[gdPos + 1] == ' ')
donatien 0:3b2f052c333b 512 {
donatien 0:3b2f052c333b 513 //This is an entry prompt
donatien 0:3b2f052c333b 514 //Replace m_inputBuf[gdPos] with null-terminating char
donatien 0:3b2f052c333b 515 m_inputBuf[gdPos] = '\0';
donatien 0:3b2f052c333b 516
donatien 0:3b2f052c333b 517 //Shift remaining data to beginning of buffer
donatien 0:3b2f052c333b 518 memmove(m_inputBuf, m_inputBuf + gdPos + 1, (m_inputPos + 1) - (gdPos + 1)); //Move null-terminating char as well
donatien 0:3b2f052c333b 519 m_inputPos = m_inputPos - (gdPos + 1); //Adjust m_inputPos
donatien 0:3b2f052c333b 520
donatien 0:3b2f052c333b 521 //Process prompt
donatien 0:3b2f052c333b 522 ret = processEntryPrompt();
donatien 0:3b2f052c333b 523 if(ret)
donatien 0:3b2f052c333b 524 {
donatien 0:3b2f052c333b 525 m_inputPos = 0;
donatien 51:54ca82a7644c 526 m_inputBuf[0] = '\0'; //Always have a null-terminating char at start of buffer
donatien 0:3b2f052c333b 527 lineDetected = false;
donatien 0:3b2f052c333b 528 return ret;
donatien 0:3b2f052c333b 529 }
donatien 0:3b2f052c333b 530
donatien 0:3b2f052c333b 531 DBG("One line was successfully processed");
donatien 0:3b2f052c333b 532 lineProcessed = true; //Line was processed with success
donatien 0:3b2f052c333b 533 }
donatien 0:3b2f052c333b 534 else
donatien 0:3b2f052c333b 535 {
donatien 0:3b2f052c333b 536 //This is completely unexpected, discard all chars till the GD char to try to recover good state
donatien 0:3b2f052c333b 537 WARN("Unexpected %c char (%02d code) found in incoming line", m_inputBuf[gdPos + 1]);
donatien 0:3b2f052c333b 538 memmove(m_inputBuf, m_inputBuf + gdPos + 1, (m_inputPos + 1) - (gdPos + 1)); //Move null-terminating char as well
donatien 0:3b2f052c333b 539 m_inputPos = m_inputPos - (gdPos + 1); //Adjust m_inputPos
donatien 0:3b2f052c333b 540 }
donatien 0:3b2f052c333b 541 lineDetected = false; //In both case search now for a new line
donatien 0:3b2f052c333b 542 }
donatien 0:3b2f052c333b 543 }
donatien 0:3b2f052c333b 544 }
donatien 0:3b2f052c333b 545 } while(lineProcessed); //If one complete line was processed there might be other incoming lines that can also be processed without reading the buffer again
donatien 0:3b2f052c333b 546
donatien 0:3b2f052c333b 547 //If the line could not be processed AND buffer is full, it means that we won't ever be able to process it (buffer too short)
donatien 0:3b2f052c333b 548 if(m_inputPos == AT_INPUT_BUF_SIZE - 1)
donatien 0:3b2f052c333b 549 {
donatien 0:3b2f052c333b 550 //Discard everything
donatien 0:3b2f052c333b 551 m_inputPos = 0;
donatien 51:54ca82a7644c 552 m_inputBuf[0] = '\0'; //Always have a null-terminating char at start of buffer
donatien 0:3b2f052c333b 553 WARN("Incoming buffer is too short to process incoming line");
donatien 0:3b2f052c333b 554 //Look for a new line
donatien 0:3b2f052c333b 555 lineDetected = false;
donatien 0:3b2f052c333b 556 }
donatien 0:3b2f052c333b 557
donatien 0:3b2f052c333b 558 DBG("Processed every full incoming lines");
donatien 0:3b2f052c333b 559
donatien 0:3b2f052c333b 560 return OK;
donatien 0:3b2f052c333b 561 }
donatien 0:3b2f052c333b 562
donatien 0:3b2f052c333b 563 int ATCommandsInterface::trySendCommand()
donatien 0:3b2f052c333b 564 {
donatien 0:3b2f052c333b 565 osEvent evt = m_env2AT.get(0);
donatien 0:3b2f052c333b 566 DBG("status = %d, msg = %d", evt.status, evt.value.p);
donatien 0:3b2f052c333b 567 if(evt.status == osEventMail)
donatien 0:3b2f052c333b 568 {
donatien 0:3b2f052c333b 569 int* msg = (int*) evt.value.p;
donatien 0:3b2f052c333b 570 if( *msg == AT_CMD_READY ) //Command pending
donatien 0:3b2f052c333b 571 {
donatien 0:3b2f052c333b 572 if(m_transactionState != IDLE)
donatien 0:3b2f052c333b 573 {
donatien 0:3b2f052c333b 574 WARN("Previous command not processed!");
donatien 0:3b2f052c333b 575 }
donatien 0:3b2f052c333b 576 DBG("Sending pending command");
donatien 0:3b2f052c333b 577 m_pStream->write((uint8_t*)m_transactionCommand, strlen(m_transactionCommand), osWaitForever);
donatien 0:3b2f052c333b 578 char cr = CR;
donatien 0:3b2f052c333b 579 m_pStream->write((uint8_t*)&cr, 1, osWaitForever); //Carriage return line terminator
donatien 0:3b2f052c333b 580 m_transactionState = COMMAND_SENT;
donatien 0:3b2f052c333b 581 }
donatien 59:0bcb8c5216d4 582 else //Timeout
donatien 0:3b2f052c333b 583 {
donatien 59:0bcb8c5216d4 584 //Acknowledge
donatien 59:0bcb8c5216d4 585 int* msg = m_AT2Env.alloc(osWaitForever);
donatien 59:0bcb8c5216d4 586 *msg = AT_TIMEOUT;
donatien 59:0bcb8c5216d4 587 m_AT2Env.put(msg); //Command has timed out
donatien 0:3b2f052c333b 588 m_transactionState = IDLE; //State-machine reset
donatien 0:3b2f052c333b 589 }
donatien 0:3b2f052c333b 590 m_env2AT.free(msg);
donatien 0:3b2f052c333b 591 }
donatien 0:3b2f052c333b 592 return OK;
donatien 0:3b2f052c333b 593 }
donatien 0:3b2f052c333b 594
donatien 0:3b2f052c333b 595 int ATCommandsInterface::processReadLine()
donatien 0:3b2f052c333b 596 {
donatien 0:3b2f052c333b 597 DBG("Processing read line [%s]", m_inputBuf);
donatien 0:3b2f052c333b 598 //The line is stored in m_inputBuf
donatien 0:3b2f052c333b 599 if(m_transactionState == COMMAND_SENT)
donatien 0:3b2f052c333b 600 {
donatien 0:3b2f052c333b 601 //If the command has been sent, checks echo to see if it has been received properly
donatien 0:3b2f052c333b 602 if( strcmp(m_transactionCommand, m_inputBuf) == 0 )
donatien 0:3b2f052c333b 603 {
donatien 0:3b2f052c333b 604 DBG("Command echo received");
donatien 0:3b2f052c333b 605 //If so, it means that the following lines will only be solicited results
donatien 0:3b2f052c333b 606 m_transactionState = READING_RESULT;
donatien 0:3b2f052c333b 607 return OK;
donatien 0:3b2f052c333b 608 }
donatien 0:3b2f052c333b 609 }
donatien 0:3b2f052c333b 610 if(m_transactionState == IDLE || m_transactionState == COMMAND_SENT)
donatien 0:3b2f052c333b 611 {
donatien 0:3b2f052c333b 612 bool found = false;
donatien 0:3b2f052c333b 613 char* pSemicol = strchr(m_inputBuf, ':');
donatien 0:3b2f052c333b 614 char* pData = NULL;
donatien 0:3b2f052c333b 615 if( pSemicol != NULL ) //Split the identifier & the result code (if it exists)
donatien 0:3b2f052c333b 616 {
donatien 0:3b2f052c333b 617 *pSemicol = '\0';
donatien 0:3b2f052c333b 618 pData = pSemicol + 1;
donatien 0:3b2f052c333b 619 if(pData[0]==' ')
donatien 0:3b2f052c333b 620 {
donatien 0:3b2f052c333b 621 pData++; //Suppress whitespace
donatien 0:3b2f052c333b 622 }
donatien 0:3b2f052c333b 623 }
donatien 0:3b2f052c333b 624 //Looks for a unsolicited result code; we can have m_transactionState == COMMAND_SENT as the code may have arrived just before we sent the command
donatien 57:593fb493172f 625 m_eventsProcessingMtx.lock();
donatien 0:3b2f052c333b 626 //Go through the list
donatien 0:3b2f052c333b 627 for(int i = 0; i < MAX_AT_EVENTS_HANDLERS; i++) //Find a free slot
donatien 0:3b2f052c333b 628 {
donatien 0:3b2f052c333b 629 if( m_eventsHandlers[i] != NULL )
donatien 0:3b2f052c333b 630 {
donatien 0:3b2f052c333b 631 if( m_eventsHandlers[i]->isATCodeHandled(m_inputBuf) )
donatien 0:3b2f052c333b 632 {
donatien 0:3b2f052c333b 633 m_eventsHandlers[i]->onEvent(m_inputBuf, pData);
donatien 0:3b2f052c333b 634 found = true; //Do not break here as there might be multiple handlers for one event type
donatien 0:3b2f052c333b 635 }
donatien 0:3b2f052c333b 636 }
donatien 0:3b2f052c333b 637 }
donatien 57:593fb493172f 638 m_eventsProcessingMtx.unlock();
donatien 0:3b2f052c333b 639 if(found)
donatien 0:3b2f052c333b 640 {
donatien 0:3b2f052c333b 641 return OK;
donatien 0:3b2f052c333b 642 }
donatien 0:3b2f052c333b 643 }
donatien 0:3b2f052c333b 644 if(m_transactionState == READING_RESULT)
donatien 0:3b2f052c333b 645 {
donatien 0:3b2f052c333b 646 //The following lines can either be a command response or a result code (OK / ERROR / CONNECT / +CME ERROR: %s / +CMS ERROR: %s)
donatien 0:3b2f052c333b 647 if(strcmp("OK", m_inputBuf) == 0)
donatien 0:3b2f052c333b 648 {
donatien 0:3b2f052c333b 649 DBG("OK result received");
donatien 0:3b2f052c333b 650 m_transactionResult.code = 0;
donatien 0:3b2f052c333b 651 m_transactionResult.result = ATResult::AT_OK;
donatien 0:3b2f052c333b 652 m_transactionState = IDLE;
donatien 0:3b2f052c333b 653 int* msg = m_AT2Env.alloc(osWaitForever);
donatien 0:3b2f052c333b 654 *msg = AT_RESULT_READY;
donatien 0:3b2f052c333b 655 m_AT2Env.put(msg); //Command has been processed
donatien 0:3b2f052c333b 656 return OK;
donatien 0:3b2f052c333b 657 }
donatien 0:3b2f052c333b 658 else if(strcmp("ERROR", m_inputBuf) == 0)
donatien 0:3b2f052c333b 659 {
donatien 0:3b2f052c333b 660 DBG("ERROR result received");
donatien 0:3b2f052c333b 661 m_transactionResult.code = 0;
donatien 0:3b2f052c333b 662 m_transactionResult.result = ATResult::AT_ERROR;
donatien 0:3b2f052c333b 663 m_transactionState = IDLE;
donatien 0:3b2f052c333b 664 int* msg = m_AT2Env.alloc(osWaitForever);
donatien 0:3b2f052c333b 665 *msg = AT_RESULT_READY;
donatien 0:3b2f052c333b 666 m_AT2Env.put(msg); //Command has been processed
donatien 0:3b2f052c333b 667 return OK;
donatien 0:3b2f052c333b 668 }
donatien 0:3b2f052c333b 669 else if(strncmp("CONNECT", m_inputBuf, 7 /*=strlen("CONNECT")*/) == 0) //Result can be "CONNECT" or "CONNECT %d", indicating baudrate
donatien 0:3b2f052c333b 670 {
donatien 0:3b2f052c333b 671 DBG("CONNECT result received");
donatien 0:3b2f052c333b 672 m_transactionResult.code = 0;
donatien 0:3b2f052c333b 673 m_transactionResult.result = ATResult::AT_CONNECT;
donatien 0:3b2f052c333b 674 m_transactionState = IDLE;
donatien 0:3b2f052c333b 675 int* msg = m_AT2Env.alloc(osWaitForever);
donatien 0:3b2f052c333b 676 *msg = AT_RESULT_READY;
donatien 0:3b2f052c333b 677 m_AT2Env.put(msg); //Command has been processed
donatien 0:3b2f052c333b 678 return OK;
donatien 0:3b2f052c333b 679 }
donatien 0:3b2f052c333b 680 else if(strcmp("COMMAND NOT SUPPORT", m_inputBuf) == 0) //Huawei-specific, not normalized
donatien 0:3b2f052c333b 681 {
donatien 0:3b2f052c333b 682 DBG("COMMAND NOT SUPPORT result received");
donatien 0:3b2f052c333b 683 m_transactionResult.code = 0;
donatien 0:3b2f052c333b 684 m_transactionResult.result = ATResult::AT_ERROR;
donatien 0:3b2f052c333b 685 m_transactionState = IDLE;
donatien 0:3b2f052c333b 686 int* msg = m_AT2Env.alloc(osWaitForever);
donatien 0:3b2f052c333b 687 *msg = AT_RESULT_READY;
donatien 0:3b2f052c333b 688 m_AT2Env.put(msg); //Command has been processed
donatien 0:3b2f052c333b 689 return OK;
donatien 0:3b2f052c333b 690 }
donatien 0:3b2f052c333b 691 else if(strstr(m_inputBuf, "+CME ERROR:") == m_inputBuf) //Mobile Equipment Error
donatien 0:3b2f052c333b 692 {
donatien 0:3b2f052c333b 693 std::sscanf(m_inputBuf + 12 /* =strlen("+CME ERROR: ") */, "%d", &m_transactionResult.code);
donatien 0:3b2f052c333b 694 DBG("+CME ERROR: %d result received", m_transactionResult.code);
donatien 0:3b2f052c333b 695 m_transactionResult.result = ATResult::AT_CME_ERROR;
donatien 0:3b2f052c333b 696 m_transactionState = IDLE;
donatien 0:3b2f052c333b 697 int* msg = m_AT2Env.alloc(osWaitForever);
donatien 0:3b2f052c333b 698 *msg = AT_RESULT_READY;
donatien 0:3b2f052c333b 699 m_AT2Env.put(msg); //Command has been processed
donatien 0:3b2f052c333b 700 return OK;
donatien 0:3b2f052c333b 701 }
donatien 0:3b2f052c333b 702 else if(strstr(m_inputBuf, "+CMS ERROR:") == m_inputBuf) //SIM Error
donatien 0:3b2f052c333b 703 {
donatien 0:3b2f052c333b 704 std::sscanf(m_inputBuf + 13 /* =strlen("+CME ERROR: ") */, "%d", &m_transactionResult.code);
donatien 0:3b2f052c333b 705 DBG("+CMS ERROR: %d result received", m_transactionResult.code);
donatien 0:3b2f052c333b 706 m_transactionResult.result = ATResult::AT_CMS_ERROR;
donatien 0:3b2f052c333b 707 m_transactionState = IDLE;
donatien 0:3b2f052c333b 708 int* msg = m_AT2Env.alloc(osWaitForever);
donatien 0:3b2f052c333b 709 *msg = AT_RESULT_READY;
donatien 0:3b2f052c333b 710 m_AT2Env.put(msg); //Command has been processed
donatien 0:3b2f052c333b 711 return OK;
donatien 0:3b2f052c333b 712 }
donatien 0:3b2f052c333b 713 else
donatien 0:3b2f052c333b 714 {
donatien 0:3b2f052c333b 715 DBG("Unprocessed result received: '%s'", m_inputBuf);
donatien 0:3b2f052c333b 716 //Must call transaction processor to complete line processing
donatien 0:3b2f052c333b 717 int ret = m_pTransactionProcessor->onNewATResponseLine(this, m_inputBuf); //Here sendData can be called
donatien 0:3b2f052c333b 718 return ret;
donatien 0:3b2f052c333b 719 }
donatien 0:3b2f052c333b 720 }
donatien 0:3b2f052c333b 721
donatien 0:3b2f052c333b 722 return OK;
donatien 0:3b2f052c333b 723 }
donatien 0:3b2f052c333b 724
donatien 0:3b2f052c333b 725 int ATCommandsInterface::processEntryPrompt()
donatien 0:3b2f052c333b 726 {
donatien 0:3b2f052c333b 727 DBG("Calling prompt handler");
donatien 0:3b2f052c333b 728 int ret = m_pTransactionProcessor->onNewEntryPrompt(this); //Here sendData can be called
donatien 0:3b2f052c333b 729
donatien 0:3b2f052c333b 730 if( ret != NET_MOREINFO ) //A new prompt is expected
donatien 0:3b2f052c333b 731 {
donatien 0:3b2f052c333b 732 DBG("Sending break character");
donatien 0:3b2f052c333b 733 //Send CTRL+Z (break sequence) to exit prompt
donatien 0:3b2f052c333b 734 char seq[2] = {BRK, 0x00};
donatien 0:3b2f052c333b 735 sendData(seq);
donatien 0:3b2f052c333b 736 }
donatien 0:3b2f052c333b 737 return OK;
donatien 0:3b2f052c333b 738 }
donatien 0:3b2f052c333b 739
donatien 57:593fb493172f 740 //This will be called on initialization & after the execution of a command
donatien 57:593fb493172f 741 void ATCommandsInterface::enableEvents()
donatien 57:593fb493172f 742 {
donatien 57:593fb493172f 743 //Advertize this to events handlers
donatien 57:593fb493172f 744 m_eventsMgmtMtx.lock();
donatien 57:593fb493172f 745 for(int i = 0; i < MAX_AT_EVENTS_HANDLERS; i++) //Find a free slot
donatien 57:593fb493172f 746 {
donatien 57:593fb493172f 747 if( m_eventsHandlers[i] != NULL )
donatien 57:593fb493172f 748 {
donatien 57:593fb493172f 749 m_eventsHandlers[i]->onDispatchStart();
donatien 57:593fb493172f 750 //Enable this kind of events
donatien 57:593fb493172f 751 if(m_eventsHandlers[i]->getEventsEnableCommand() != NULL)
donatien 57:593fb493172f 752 {
donatien 57:593fb493172f 753 int ret = executeInternal(m_eventsHandlers[i]->getEventsEnableCommand(), this, NULL); //Execute enable command
donatien 57:593fb493172f 754 if(ret)
donatien 57:593fb493172f 755 {
donatien 57:593fb493172f 756 WARN("Events enabling command failed");
donatien 57:593fb493172f 757 }
donatien 57:593fb493172f 758 }
donatien 57:593fb493172f 759 }
donatien 57:593fb493172f 760 }
donatien 57:593fb493172f 761 m_eventsMgmtMtx.unlock();
donatien 57:593fb493172f 762 }
donatien 57:593fb493172f 763
donatien 57:593fb493172f 764 //This will be called on de-initialization & before the execution of a command to prevent unsollicited result codes from polluting the results
donatien 57:593fb493172f 765 void ATCommandsInterface::disableEvents()
donatien 57:593fb493172f 766 {
donatien 57:593fb493172f 767 //Advertize this to events handlers
donatien 57:593fb493172f 768 m_eventsMgmtMtx.lock();
donatien 57:593fb493172f 769 for(int i = 0; i < MAX_AT_EVENTS_HANDLERS; i++) //Find a free slot
donatien 57:593fb493172f 770 {
donatien 57:593fb493172f 771 if( m_eventsHandlers[i] != NULL )
donatien 57:593fb493172f 772 {
donatien 57:593fb493172f 773 m_eventsHandlers[i]->onDispatchStart();
donatien 57:593fb493172f 774 //Disable this kind of events
donatien 57:593fb493172f 775 if(m_eventsHandlers[i]->getEventsDisableCommand() != NULL)
donatien 57:593fb493172f 776 {
donatien 57:593fb493172f 777 int ret = executeInternal(m_eventsHandlers[i]->getEventsDisableCommand(), this, NULL); //Execute disable command
donatien 57:593fb493172f 778 if(ret)
donatien 57:593fb493172f 779 {
donatien 57:593fb493172f 780 WARN("Events disabling command failed");
donatien 57:593fb493172f 781 }
donatien 57:593fb493172f 782 }
donatien 57:593fb493172f 783 }
donatien 57:593fb493172f 784 }
donatien 57:593fb493172f 785 m_eventsMgmtMtx.unlock();
donatien 57:593fb493172f 786 }
donatien 57:593fb493172f 787
donatien 0:3b2f052c333b 788 //Commands that can be called during onNewATResponseLine callback, additionally to close()
donatien 0:3b2f052c333b 789 //Access to this method is protected (can ONLY be called on processing thread during IATCommandsProcessor::onNewATResponseLine execution)
donatien 0:3b2f052c333b 790 int ATCommandsInterface::sendData(const char* data)
donatien 0:3b2f052c333b 791 {
donatien 0:3b2f052c333b 792 //m_inputBuf is cleared at this point (and MUST therefore be empty)
donatien 0:3b2f052c333b 793 int dataLen = strlen(data);
donatien 0:3b2f052c333b 794 DBG("Sending raw string of length %d", dataLen);
donatien 0:3b2f052c333b 795 int ret = m_pStream->write((uint8_t*)data, dataLen, osWaitForever);
donatien 0:3b2f052c333b 796 if(ret)
donatien 0:3b2f052c333b 797 {
donatien 0:3b2f052c333b 798 WARN("Could not write to stream (returned %d)", ret);
donatien 0:3b2f052c333b 799 return ret;
donatien 0:3b2f052c333b 800 }
donatien 0:3b2f052c333b 801
donatien 0:3b2f052c333b 802 int dataPos = 0;
donatien 0:3b2f052c333b 803 do
donatien 0:3b2f052c333b 804 {
donatien 0:3b2f052c333b 805 //Read echo
donatien 0:3b2f052c333b 806 size_t readLen;
donatien 0:3b2f052c333b 807 int ret = m_pStream->read((uint8_t*)m_inputBuf, &readLen, MIN(dataLen - dataPos, AT_INPUT_BUF_SIZE - 1), osWaitForever); //Make sure we do not read more than needed otherwise it could break the parser
donatien 0:3b2f052c333b 808 if(ret)
donatien 0:3b2f052c333b 809 {
donatien 0:3b2f052c333b 810 WARN("Could not read from stream (returned %d)", ret);
donatien 51:54ca82a7644c 811 m_inputPos = 0; //Reset input buffer state
donatien 51:54ca82a7644c 812 m_inputBuf[0] = '\0'; //Always have a null-terminating char at start of buffer
donatien 0:3b2f052c333b 813 return ret;
donatien 51:54ca82a7644c 814 }
donatien 0:3b2f052c333b 815
donatien 0:3b2f052c333b 816 if( memcmp(m_inputBuf, data + dataPos, readLen) != 0 )
donatien 0:3b2f052c333b 817 {
donatien 0:3b2f052c333b 818 //Echo does not match output
donatien 57:593fb493172f 819 m_inputBuf[readLen] = '\0';
donatien 57:593fb493172f 820 WARN("Echo does not match output, got '%s' instead", m_inputBuf);
donatien 51:54ca82a7644c 821 m_inputPos = 0; //Reset input buffer state
donatien 51:54ca82a7644c 822 m_inputBuf[0] = '\0'; //Always have a null-terminating char at start of buffer
donatien 0:3b2f052c333b 823 return NET_DIFF;
donatien 0:3b2f052c333b 824 }
donatien 0:3b2f052c333b 825
donatien 0:3b2f052c333b 826 dataPos += readLen;
donatien 0:3b2f052c333b 827 //If all characters have not been read yet
donatien 0:3b2f052c333b 828
donatien 0:3b2f052c333b 829 } while(dataPos < dataLen);
donatien 0:3b2f052c333b 830
donatien 0:3b2f052c333b 831 DBG("String sent successfully");
donatien 0:3b2f052c333b 832
donatien 0:3b2f052c333b 833 m_inputPos = 0; //Reset input buffer state
donatien 51:54ca82a7644c 834 m_inputBuf[0] = '\0'; //Always have a null-terminating char at start of buffer
donatien 0:3b2f052c333b 835
donatien 0:3b2f052c333b 836 return OK;
donatien 0:3b2f052c333b 837 }
donatien 0:3b2f052c333b 838
donatien 0:3b2f052c333b 839 /*static*/ void ATCommandsInterface::staticCallback(void const* p)
donatien 0:3b2f052c333b 840 {
donatien 0:3b2f052c333b 841 ((ATCommandsInterface*)p)->process();
donatien 0:3b2f052c333b 842 }
donatien 0:3b2f052c333b 843
donatien 0:3b2f052c333b 844 int ATCommandsInterface::ATResultToReturnCode(ATResult result) //Helper
donatien 0:3b2f052c333b 845 {
donatien 0:3b2f052c333b 846 if(result.result == ATResult::AT_OK)
donatien 0:3b2f052c333b 847 {
donatien 0:3b2f052c333b 848 return OK;
donatien 0:3b2f052c333b 849 }
donatien 0:3b2f052c333b 850 else
donatien 0:3b2f052c333b 851 {
donatien 0:3b2f052c333b 852 return NET_MOREINFO;
donatien 0:3b2f052c333b 853 }
donatien 0:3b2f052c333b 854 }
donatien 0:3b2f052c333b 855
donatien 0:3b2f052c333b 856 /*virtual*/ int ATCommandsInterface::onNewATResponseLine(ATCommandsInterface* pInst, const char* line) //Default implementation for simple commands handling
donatien 0:3b2f052c333b 857 {
donatien 0:3b2f052c333b 858 return OK;
donatien 0:3b2f052c333b 859 }
donatien 0:3b2f052c333b 860
donatien 0:3b2f052c333b 861 /*virtual*/ int ATCommandsInterface::onNewEntryPrompt(ATCommandsInterface* pInst) //Default implementation (just sends Ctrl+Z to exit the prompt by returning OK right-away)
donatien 0:3b2f052c333b 862 {
donatien 0:3b2f052c333b 863 return OK;
donatien 0:3b2f052c333b 864 }
donatien 0:3b2f052c333b 865
donatien 0:3b2f052c333b 866 void ATCommandsInterface::process() //Processing thread
donatien 0:3b2f052c333b 867 {
donatien 0:3b2f052c333b 868 DBG("AT Thread started");
donatien 0:3b2f052c333b 869 while(true)
donatien 0:3b2f052c333b 870 {
donatien 0:3b2f052c333b 871 DBG("AT Processing on hold");
donatien 0:3b2f052c333b 872 m_processingThread.signal_wait(AT_SIG_PROCESSING_START); //Block until the process is started
donatien 0:3b2f052c333b 873
donatien 0:3b2f052c333b 874 m_processingMtx.lock();
donatien 0:3b2f052c333b 875 DBG("AT Processing started");
donatien 0:3b2f052c333b 876 //First of all discard buffer
donatien 0:3b2f052c333b 877 int ret;
donatien 0:3b2f052c333b 878 size_t readLen;
donatien 0:3b2f052c333b 879 do //Drop everything
donatien 0:3b2f052c333b 880 {
donatien 0:3b2f052c333b 881 ret = m_pStream->read((uint8_t*)m_inputBuf, &readLen, AT_INPUT_BUF_SIZE - 1, 0); //Do NOT wait at this point
donatien 0:3b2f052c333b 882 } while(ret == OK);
donatien 0:3b2f052c333b 883 m_inputPos = 0; //Clear input buffer
donatien 0:3b2f052c333b 884 do
donatien 0:3b2f052c333b 885 {
donatien 57:593fb493172f 886 DBG("Trying to send a pending command");
donatien 57:593fb493172f 887 trySendCommand(); //This must be tried first as we discarded the buffer before and therefore would be blocking though there is a pending command
donatien 0:3b2f052c333b 888 DBG("Trying to read a new line");
donatien 0:3b2f052c333b 889 tryReadLine();
donatien 0:3b2f052c333b 890 } while( m_processingThread.signal_wait(AT_SIG_PROCESSING_STOP, 0).status != osEventSignal ); //Loop until the process is interrupted
donatien 0:3b2f052c333b 891 m_processingMtx.unlock();
donatien 0:3b2f052c333b 892 DBG("AT Processing stopped");
donatien 0:3b2f052c333b 893 }
donatien 0:3b2f052c333b 894 }
donatien 0:3b2f052c333b 895