PHS module SMA-01 library. see: https://developer.mbed.org/users/phsfan/notebook/abitusbmodem/

Dependencies:   Socket lwip-sys lwip

Dependents:   AbitUSBModem_HTTPTest AbitUSBModem_MQTTTest AbitUSBModem_WebsocketTest AbitUSBModem_SMSTest

Fork of VodafoneUSBModem by mbed official

/media/uploads/phsfan/sma01_003.png

Committer:
phsfan
Date:
Wed Feb 25 14:34:13 2015 +0000
Revision:
99:514e67a69ad6
Parent:
97:7d9cc95e2ea7
supported SMS

Who changed what in which revision?

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