ATCommandsInterface.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifndef ATCOMMANDSINTERFACE_H_
  20. #define ATCOMMANDSINTERFACE_H_
  21. #include "core/fwk.h"
  22. #include "rtos.h"
  23. #define MAX_AT_EVENTS_HANDLERS 4
  24. class ATCommandsInterface;
  25. /** Interface implemented by components handling AT events
  26. *
  27. */
  28. class IATEventsHandler
  29. {
  30. protected:
  31. virtual bool isATCodeHandled(const char* atCode) = 0; //Is this AT code handled
  32. virtual void onDispatchStart() = 0;
  33. virtual void onDispatchStop() = 0;
  34. virtual char* getEventsEnableCommand() = 0;
  35. virtual char* getEventsDisableCommand() = 0;
  36. virtual void onEvent(const char* atCode, const char* evt) = 0;
  37. friend class ATCommandsInterface;
  38. };
  39. /** Interface implemented by components executing complex AT commands
  40. *
  41. */
  42. class IATCommandsProcessor
  43. {
  44. protected:
  45. virtual int onNewATResponseLine(ATCommandsInterface* pInst, const char* line) = 0;
  46. virtual int onNewEntryPrompt(ATCommandsInterface* pInst) = 0;
  47. friend class ATCommandsInterface;
  48. };
  49. #define AT_INPUT_BUF_SIZE 192//64
  50. //Signals to be sent to the processing thread
  51. #define AT_SIG_PROCESSING_START 1
  52. #define AT_SIG_PROCESSING_STOP 2
  53. //Messages to be sent to the processing thread
  54. #define AT_CMD_READY 1
  55. #define AT_TIMEOUT 2
  56. #define AT_STOP 3
  57. //Messages to be sent from the processing thread
  58. #define AT_RESULT_READY 1
  59. /** AT Commands interface class
  60. *
  61. */
  62. class ATCommandsInterface : protected IATCommandsProcessor
  63. {
  64. public:
  65. ATCommandsInterface(IOStream* pStream);
  66. //Open connection to AT Interface in order to execute command & register/unregister events
  67. int open();
  68. //Initialize AT link
  69. int init(bool reset = true);
  70. //Close connection
  71. int close();
  72. bool isOpen();
  73. class ATResult
  74. {
  75. public:
  76. enum { AT_OK, AT_ERROR, AT_CONNECT, AT_CMS_ERROR, AT_CME_ERROR } result;
  77. int code;
  78. };
  79. int executeSimple(const char* command, ATResult* pResult, uint32_t timeout=1000);
  80. int execute(const char* command, IATCommandsProcessor* pProcessor, ATResult* pResult, uint32_t timeout=1000);
  81. int registerEventsHandler(IATEventsHandler* pHdlr);
  82. int deregisterEventsHandler(IATEventsHandler* pHdlr);
  83. //Commands that can be called during onNewATResponseLine callback, additionally to close()
  84. //Access to this method is protected (can ONLY be called on processing thread during IATCommandsProcessor::onNewATResponseLine execution)
  85. int sendData(const char* data);
  86. static void staticCallback(void const* p);
  87. private:
  88. int executeInternal(const char* command, IATCommandsProcessor* pProcessor, ATResult* pResult, uint32_t timeout=1000);
  89. int tryReadLine();
  90. int trySendCommand();
  91. int processReadLine();
  92. int processEntryPrompt();
  93. void enableEvents();
  94. void disableEvents();
  95. int ATResultToReturnCode(ATResult result); //Helper
  96. virtual int onNewATResponseLine(ATCommandsInterface* pInst, const char* line); //Default implementation for simple commands handling
  97. virtual int onNewEntryPrompt(ATCommandsInterface* pInst); //Default implementation (just sends Ctrl+Z to exit the prompt)
  98. void process(); //Processing thread
  99. IOStream* m_pStream;
  100. bool m_open; //< TRUE when the AT interface is open, and FALSE when it is not.
  101. const char* m_transactionCommand;
  102. const char* m_transactionData;
  103. IATCommandsProcessor* m_pTransactionProcessor;
  104. ATResult m_transactionResult;
  105. enum { IDLE, COMMAND_SENT, READING_RESULT, ABORTED } m_transactionState;
  106. char m_inputBuf[AT_INPUT_BUF_SIZE]; // Stores characters received from the modem.
  107. int m_inputPos; // Current position of fill pointer in the input buffer.
  108. Mutex m_transactionMtx;
  109. // These are RTOS queues, concurrent access protected. In this case both only contain an integer.
  110. Mail<int,1> m_env2AT; // used by calling function to inform processing thread of events
  111. Mail<int,1> m_AT2Env; // used by processing thread to inform calling function of events
  112. IATEventsHandler* m_eventsHandlers[MAX_AT_EVENTS_HANDLERS]; // all registered events handlers
  113. Mutex m_processingMtx;
  114. Thread m_processingThread;
  115. Mutex m_eventsMgmtMtx; //Lock events use within the calling thread
  116. Mutex m_eventsProcessingMtx; //Lock events use within the processing thread
  117. };
  118. #endif /* ATCOMMANDSINTERFACE_H_ */