Base library for cellular modem implementations

Dependencies:   Socket lwip-sys lwip

Dependents:   CellularUSBModem CellularUSBModem

Deprecated

This is an mbed 2 networking library. For mbed 5, the networking libraries have been revised to better support additional network stacks and thread safety here.

Committer:
mbed_official
Date:
Mon Dec 16 09:00:24 2013 +0000
Revision:
4:3fc75e611736
Parent:
1:4a23efdf0da9
Synchronized with git revision 170ac6562b7b2b5bb43f8ecf82b2af18b37eeb9c

Full URL: https://github.com/mbedmicro/mbed/commit/170ac6562b7b2b5bb43f8ecf82b2af18b37eeb9c/

improve USB host library and cellular modem stack

Who changed what in which revision?

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