local fork

Dependencies:   Socket USBHostWANDongle_bleedingedge lwip-sys lwip

Dependents:   Encrypted

Fork of VodafoneUSBModem_bleedingedge by Donatien Garnier

Committer:
donatien
Date:
Mon Oct 22 10:12:27 2012 +0000
Revision:
54:f9b1f5444963
Parent:
51:54ca82a7644c
Parent:
53:9c0f1d4c1df9
Child:
55:07b9b48cfe09
Reverting to rev 52

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