fork of VodafoneUSBModem with updated USBHost library

Dependencies:   Socket USBHost lwip-sys lwip

Dependents:   VodafoneUSBModemSMSTest

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 void onEvent(const char* atCode, const char* evt) = 0;
00040   friend class ATCommandsInterface;
00041 };
00042 
00043 /** Interface implemented by components executing complex AT commands
00044  *
00045  */
00046 class IATCommandsProcessor
00047 {
00048 protected:
00049   virtual int onNewATResponseLine(ATCommandsInterface* pInst, const char* line) = 0;
00050   virtual int onNewEntryPrompt(ATCommandsInterface* pInst) = 0;
00051   friend class ATCommandsInterface;
00052 };
00053 
00054 #define AT_INPUT_BUF_SIZE 192//64
00055 
00056 //Signals to be sent to the processing thread
00057 #define AT_SIG_PROCESSING_START 1
00058 #define AT_SIG_PROCESSING_STOP 2
00059 //Messages to be sent to the processing thread
00060 #define AT_CMD_READY 1
00061 #define AT_TIMEOUT 2
00062 #define AT_STOP 3
00063 //Messages to be sent from the processing thread
00064 #define AT_RESULT_READY 1
00065 
00066 /** AT Commands interface class
00067  *
00068  */
00069 class ATCommandsInterface : protected IATCommandsProcessor
00070 {
00071 public:
00072   ATCommandsInterface(IOStream* pStream);
00073 
00074   //Open connection to AT Interface in order to execute command & register/unregister events
00075   int open();
00076 
00077   //Initialize AT link
00078   int init();
00079 
00080   //Close connection
00081   int close();
00082   
00083   bool isOpen();
00084 
00085   class ATResult
00086   {
00087   public:
00088     enum { AT_OK, AT_ERROR, AT_CONNECT, AT_CMS_ERROR, AT_CME_ERROR } result;
00089     int code;
00090   };
00091 
00092   int executeSimple(const char* command, ATResult* pResult, uint32_t timeout=1000);
00093   int execute(const char* command, IATCommandsProcessor* pProcessor, ATResult* pResult, uint32_t timeout=1000);
00094   int registerEventsHandler(IATEventsHandler* pHdlr);
00095   int deregisterEventsHandler(IATEventsHandler* pHdlr);
00096 
00097   //Commands that can be called during onNewATResponseLine callback, additionally to close()
00098   //Access to this method is protected (can ONLY be called on processing thread during IATCommandsProcessor::onNewATResponseLine execution)
00099   int sendData(const char* data);
00100 
00101   static void staticCallback(void const* p);
00102 private:
00103   int tryReadLine();
00104   int trySendCommand();
00105   int processReadLine();
00106   int processEntryPrompt();
00107 
00108   int ATResultToReturnCode(ATResult result); //Helper
00109 
00110   virtual int onNewATResponseLine(ATCommandsInterface* pInst, const char* line); //Default implementation for simple commands handling
00111   virtual int onNewEntryPrompt(ATCommandsInterface* pInst); //Default implementation (just sends Ctrl+Z to exit the prompt)
00112 
00113   void process(); //Processing thread
00114 
00115   IOStream* m_pStream;
00116   
00117   bool m_open; //< TRUE when the AT interface is open, and FALSE when it is not.
00118 
00119   const char* m_transactionCommand;
00120   const char* m_transactionData;
00121 
00122   IATCommandsProcessor* m_pTransactionProcessor;
00123   ATResult m_transactionResult;
00124 
00125   enum { IDLE, COMMAND_SENT, READING_RESULT, ABORTED } m_transactionState;
00126 
00127   char m_inputBuf[AT_INPUT_BUF_SIZE]; // Stores characters received from the modem.
00128   int m_inputPos; // Current position of fill pointer in the input buffer.
00129 
00130   Mutex m_transactionMtx;
00131 
00132   // These are RTOS queues, concurrent access protected. In this case both only contain an integer.
00133   Mail<int,1> m_env2AT; // used by calling function to inform processing thread of events
00134   Mail<int,1> m_AT2Env; // used by processing thread to inform calling function of events
00135 
00136   IATEventsHandler* m_eventsHandlers[MAX_AT_EVENTS_HANDLERS]; // all registered events handlers
00137 
00138   Mutex m_processingMtx;
00139   Thread m_processingThread;
00140 
00141   Mutex m_eventsMtx;
00142 };
00143 
00144 #endif /* ATCOMMANDSINTERFACE_H_ */