local fork

Dependencies:   Socket USBHostWANDongle_bleedingedge lwip-sys lwip

Dependents:   Encrypted

Fork of VodafoneUSBModem_bleedingedge by Donatien Garnier

Committer:
donatien
Date:
Wed Oct 24 14:15:53 2012 +0000
Revision:
58:763eefc845b1
Parent:
57:593fb493172f
Child:
59:0bcb8c5216d4
(removed useless forgotten debug led)

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