]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/net/cellular/CellularModem/at/ATCommandsInterface.h
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / net / cellular / CellularModem / at / ATCommandsInterface.h
1 /* ATCommandsInterface.h */
2 /* Copyright (C) 2012 mbed.org, MIT License
3 *
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:
9 *
10 * The above copyright notice and this permission notice shall be included in all copies or
11 * substantial portions of the Software.
12 *
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.
18 */
19
20 #ifndef ATCOMMANDSINTERFACE_H_
21 #define ATCOMMANDSINTERFACE_H_
22
23 #include "core/fwk.h"
24 #include "rtos.h"
25
26 #define MAX_AT_EVENTS_HANDLERS 4
27
28 class ATCommandsInterface;
29
30 /** Interface implemented by components handling AT events
31 *
32 */
33 class IATEventsHandler
34 {
35 protected:
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;
43 };
44
45 /** Interface implemented by components executing complex AT commands
46 *
47 */
48 class IATCommandsProcessor
49 {
50 protected:
51 virtual int onNewATResponseLine(ATCommandsInterface* pInst, const char* line) = 0;
52 virtual int onNewEntryPrompt(ATCommandsInterface* pInst) = 0;
53 friend class ATCommandsInterface;
54 };
55
56 #define AT_INPUT_BUF_SIZE 192//64
57
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
63 #define AT_TIMEOUT 2
64 #define AT_STOP 3
65 //Messages to be sent from the processing thread
66 #define AT_RESULT_READY 1
67
68 /** AT Commands interface class
69 *
70 */
71 class ATCommandsInterface : protected IATCommandsProcessor
72 {
73 public:
74 ATCommandsInterface(IOStream* pStream);
75
76 //Open connection to AT Interface in order to execute command & register/unregister events
77 int open();
78
79 //Initialize AT link
80 int init(bool reset = true);
81
82 //Close connection
83 int close();
84
85 bool isOpen();
86
87 class ATResult
88 {
89 public:
90 enum { AT_OK, AT_ERROR, AT_CONNECT, AT_CMS_ERROR, AT_CME_ERROR } result;
91 int code;
92 };
93
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);
96
97 int registerEventsHandler(IATEventsHandler* pHdlr);
98 int deregisterEventsHandler(IATEventsHandler* pHdlr);
99
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);
103
104 static void staticCallback(void const* p);
105 private:
106 int executeInternal(const char* command, IATCommandsProcessor* pProcessor, ATResult* pResult, uint32_t timeout=1000);
107
108 int tryReadLine();
109 int trySendCommand();
110 int processReadLine();
111 int processEntryPrompt();
112
113 void enableEvents();
114 void disableEvents();
115
116 int ATResultToReturnCode(ATResult result); //Helper
117
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)
120
121 void process(); //Processing thread
122
123 IOStream* m_pStream;
124
125 bool m_open; //< TRUE when the AT interface is open, and FALSE when it is not.
126
127 const char* m_transactionCommand;
128 const char* m_transactionData;
129
130 IATCommandsProcessor* m_pTransactionProcessor;
131 ATResult m_transactionResult;
132
133 enum { IDLE, COMMAND_SENT, READING_RESULT, ABORTED } m_transactionState;
134
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.
137
138 Mutex m_transactionMtx;
139
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
143
144 IATEventsHandler* m_eventsHandlers[MAX_AT_EVENTS_HANDLERS]; // all registered events handlers
145
146 Mutex m_processingMtx;
147 Thread m_processingThread;
148
149 Mutex m_eventsMgmtMtx; //Lock events use within the calling thread
150 Mutex m_eventsProcessingMtx; //Lock events use within the processing thread
151 };
152
153 #endif /* ATCOMMANDSINTERFACE_H_ */
Imprint / Impressum