Base library for cellular modem implementations

Dependencies:   Socket lwip-sys lwip

Dependents:   CellularUSBModem CellularUSBModem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ATCommandsInterface.h Source File

ATCommandsInterface.h

00001 /* ATCommandsInterface.h */
00002 /* Copyright (C) 2012 mbed.org, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 
00020 #ifndef ATCOMMANDSINTERFACE_H_
00021 #define ATCOMMANDSINTERFACE_H_
00022 
00023 #include "core/fwk.h"
00024 #include "rtos.h"
00025 
00026 #define MAX_AT_EVENTS_HANDLERS 4
00027 
00028 class ATCommandsInterface;
00029 
00030 /** Interface implemented by components handling AT events
00031  *
00032  */
00033 class IATEventsHandler
00034 {
00035 protected:
00036   virtual bool isATCodeHandled(const char* atCode) = 0; //Is this AT code handled
00037   virtual void onDispatchStart() = 0;
00038   virtual void onDispatchStop() = 0;
00039   virtual char* getEventsEnableCommand() = 0;
00040   virtual char* getEventsDisableCommand() = 0;
00041   virtual void onEvent(const char* atCode, const char* evt) = 0;
00042   friend class ATCommandsInterface;
00043 };
00044 
00045 /** Interface implemented by components executing complex AT commands
00046  *
00047  */
00048 class IATCommandsProcessor
00049 {
00050 protected:
00051   virtual int onNewATResponseLine(ATCommandsInterface* pInst, const char* line) = 0;
00052   virtual int onNewEntryPrompt(ATCommandsInterface* pInst) = 0;
00053   friend class ATCommandsInterface;
00054 };
00055 
00056 #define AT_INPUT_BUF_SIZE 192//64
00057 
00058 //Signals to be sent to the processing thread
00059 #define AT_SIG_PROCESSING_START 1
00060 #define AT_SIG_PROCESSING_STOP 2
00061 //Messages to be sent to the processing thread
00062 #define AT_CMD_READY 1
00063 #define AT_TIMEOUT 2
00064 #define AT_STOP 3
00065 //Messages to be sent from the processing thread
00066 #define AT_RESULT_READY 1
00067 
00068 /** AT Commands interface class
00069  *
00070  */
00071 class ATCommandsInterface : protected IATCommandsProcessor
00072 {
00073 public:
00074   ATCommandsInterface(IOStream* pStream);
00075 
00076   //Open connection to AT Interface in order to execute command & register/unregister events
00077   int open();
00078 
00079   //Initialize AT link
00080   int init(bool reset = true);
00081 
00082   //Close connection
00083   int close();
00084   
00085   bool isOpen();
00086 
00087   class ATResult
00088   {
00089   public:
00090     enum { AT_OK, AT_ERROR, AT_CONNECT, AT_CMS_ERROR, AT_CME_ERROR } result;
00091     int code;
00092   };
00093 
00094   int executeSimple(const char* command, ATResult* pResult, uint32_t timeout=1000);
00095   int execute(const char* command, IATCommandsProcessor* pProcessor, ATResult* pResult, uint32_t timeout=1000);
00096   
00097   int registerEventsHandler(IATEventsHandler* pHdlr);
00098   int deregisterEventsHandler(IATEventsHandler* pHdlr);
00099 
00100   //Commands that can be called during onNewATResponseLine callback, additionally to close()
00101   //Access to this method is protected (can ONLY be called on processing thread during IATCommandsProcessor::onNewATResponseLine execution)
00102   int sendData(const char* data);
00103 
00104   static void staticCallback(void const* p);
00105 private:
00106   int executeInternal(const char* command, IATCommandsProcessor* pProcessor, ATResult* pResult, uint32_t timeout=1000);
00107   
00108   int tryReadLine();
00109   int trySendCommand();
00110   int processReadLine();
00111   int processEntryPrompt();
00112   
00113   void enableEvents();
00114   void disableEvents();
00115 
00116   int ATResultToReturnCode(ATResult result); //Helper
00117 
00118   virtual int onNewATResponseLine(ATCommandsInterface* pInst, const char* line); //Default implementation for simple commands handling
00119   virtual int onNewEntryPrompt(ATCommandsInterface* pInst); //Default implementation (just sends Ctrl+Z to exit the prompt)
00120 
00121   void process(); //Processing thread
00122 
00123   IOStream* m_pStream;
00124   
00125   bool m_open; //< TRUE when the AT interface is open, and FALSE when it is not.
00126 
00127   const char* m_transactionCommand;
00128   const char* m_transactionData;
00129 
00130   IATCommandsProcessor* m_pTransactionProcessor;
00131   ATResult m_transactionResult;
00132 
00133   enum { IDLE, COMMAND_SENT, READING_RESULT, ABORTED } m_transactionState;
00134 
00135   char m_inputBuf[AT_INPUT_BUF_SIZE]; // Stores characters received from the modem.
00136   int m_inputPos; // Current position of fill pointer in the input buffer.
00137 
00138   Mutex m_transactionMtx;
00139 
00140   // These are RTOS queues, concurrent access protected. In this case both only contain an integer.
00141   Mail<int,1> m_env2AT; // used by calling function to inform processing thread of events
00142   Mail<int,1> m_AT2Env; // used by processing thread to inform calling function of events
00143 
00144   IATEventsHandler* m_eventsHandlers[MAX_AT_EVENTS_HANDLERS]; // all registered events handlers
00145 
00146   Mutex m_processingMtx;
00147   Thread m_processingThread;
00148 
00149   Mutex m_eventsMgmtMtx; //Lock events use within the calling thread
00150   Mutex m_eventsProcessingMtx; //Lock events use within the processing thread
00151 };
00152 
00153 #endif /* ATCOMMANDSINTERFACE_H_ */