local fork

Dependencies:   Socket USBHostWANDongle_bleedingedge lwip-sys lwip

Dependents:   Encrypted

Fork of VodafoneUSBModem_bleedingedge by Donatien Garnier

Committer:
ashleymills
Date:
Tue Mar 05 14:54:15 2013 +0000
Revision:
79:897a0de9d668
Parent:
75:a6ac8206a58d
Added MU509 support

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