1 /* ATCommandsInterface.h */
2 /* Copyright (C) 2012 mbed.org, MIT License
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in all copies or
11 * substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 #ifndef ATCOMMANDSINTERFACE_H_
21 #define ATCOMMANDSINTERFACE_H_
26 #define MAX_AT_EVENTS_HANDLERS 4
28 class ATCommandsInterface
;
30 /** Interface implemented by components handling AT events
33 class IATEventsHandler
36 virtual bool isATCodeHandled(const char* atCode
) = 0; //Is this AT code handled
37 virtual void onDispatchStart() = 0;
38 virtual void onDispatchStop() = 0;
39 virtual char* getEventsEnableCommand() = 0;
40 virtual char* getEventsDisableCommand() = 0;
41 virtual void onEvent(const char* atCode
, const char* evt
) = 0;
42 friend class ATCommandsInterface
;
45 /** Interface implemented by components executing complex AT commands
48 class IATCommandsProcessor
51 virtual int onNewATResponseLine(ATCommandsInterface
* pInst
, const char* line
) = 0;
52 virtual int onNewEntryPrompt(ATCommandsInterface
* pInst
) = 0;
53 friend class ATCommandsInterface
;
56 #define AT_INPUT_BUF_SIZE 192//64
58 //Signals to be sent to the processing thread
59 #define AT_SIG_PROCESSING_START 1
60 #define AT_SIG_PROCESSING_STOP 2
61 //Messages to be sent to the processing thread
62 #define AT_CMD_READY 1
65 //Messages to be sent from the processing thread
66 #define AT_RESULT_READY 1
68 /** AT Commands interface class
71 class ATCommandsInterface
: protected IATCommandsProcessor
74 ATCommandsInterface(IOStream
* pStream
);
76 //Open connection to AT Interface in order to execute command & register/unregister events
80 int init(bool reset
= true);
90 enum { AT_OK
, AT_ERROR
, AT_CONNECT
, AT_CMS_ERROR
, AT_CME_ERROR
} result
;
94 int executeSimple(const char* command
, ATResult
* pResult
, uint32_t timeout
=1000);
95 int execute(const char* command
, IATCommandsProcessor
* pProcessor
, ATResult
* pResult
, uint32_t timeout
=1000);
97 int registerEventsHandler(IATEventsHandler
* pHdlr
);
98 int deregisterEventsHandler(IATEventsHandler
* pHdlr
);
100 //Commands that can be called during onNewATResponseLine callback, additionally to close()
101 //Access to this method is protected (can ONLY be called on processing thread during IATCommandsProcessor::onNewATResponseLine execution)
102 int sendData(const char* data
);
104 static void staticCallback(void const* p
);
106 int executeInternal(const char* command
, IATCommandsProcessor
* pProcessor
, ATResult
* pResult
, uint32_t timeout
=1000);
109 int trySendCommand();
110 int processReadLine();
111 int processEntryPrompt();
114 void disableEvents();
116 int ATResultToReturnCode(ATResult result
); //Helper
118 virtual int onNewATResponseLine(ATCommandsInterface
* pInst
, const char* line
); //Default implementation for simple commands handling
119 virtual int onNewEntryPrompt(ATCommandsInterface
* pInst
); //Default implementation (just sends Ctrl+Z to exit the prompt)
121 void process(); //Processing thread
125 bool m_open
; //< TRUE when the AT interface is open, and FALSE when it is not.
127 const char* m_transactionCommand
;
128 const char* m_transactionData
;
130 IATCommandsProcessor
* m_pTransactionProcessor
;
131 ATResult m_transactionResult
;
133 enum { IDLE
, COMMAND_SENT
, READING_RESULT
, ABORTED
} m_transactionState
;
135 char m_inputBuf
[AT_INPUT_BUF_SIZE
]; // Stores characters received from the modem.
136 int m_inputPos
; // Current position of fill pointer in the input buffer.
138 Mutex m_transactionMtx
;
140 // These are RTOS queues, concurrent access protected. In this case both only contain an integer.
141 Mail
<int,1> m_env2AT
; // used by calling function to inform processing thread of events
142 Mail
<int,1> m_AT2Env
; // used by processing thread to inform calling function of events
144 IATEventsHandler
* m_eventsHandlers
[MAX_AT_EVENTS_HANDLERS
]; // all registered events handlers
146 Mutex m_processingMtx
;
147 Thread m_processingThread
;
149 Mutex m_eventsMgmtMtx
; //Lock events use within the calling thread
150 Mutex m_eventsProcessingMtx
; //Lock events use within the processing thread
153 #endif /* ATCOMMANDSINTERFACE_H_ */