You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.1 KiB

  1. /**
  2. * @file TinyGsmCalling.tpp
  3. * @author Volodymyr Shymanskyy
  4. * @license LGPL-3.0
  5. * @copyright Copyright (c) 2016 Volodymyr Shymanskyy
  6. * @date Nov 2016
  7. */
  8. #ifndef SRC_TINYGSMCALLING_H_
  9. #define SRC_TINYGSMCALLING_H_
  10. #include "TinyGsmCommon.h"
  11. #define TINY_GSM_MODEM_HAS_CALLING
  12. template <class modemType>
  13. class TinyGsmCalling {
  14. public:
  15. /*
  16. * Phone Call functions
  17. */
  18. bool callAnswer() {
  19. return thisModem().callAnswerImpl();
  20. }
  21. bool callNumber(const String& number) {
  22. return thisModem().callNumberImpl(number);
  23. }
  24. bool callHangup() {
  25. return thisModem().callHangupImpl();
  26. }
  27. bool dtmfSend(char cmd, int duration_ms = 100) {
  28. return thisModem().dtmfSendImpl(cmd, duration_ms);
  29. }
  30. /*
  31. * CRTP Helper
  32. */
  33. protected:
  34. inline const modemType& thisModem() const {
  35. return static_cast<const modemType&>(*this);
  36. }
  37. inline modemType& thisModem() {
  38. return static_cast<modemType&>(*this);
  39. }
  40. /*
  41. * Phone Call functions
  42. */
  43. protected:
  44. bool callAnswerImpl() {
  45. thisModem().sendAT(GF("A"));
  46. return thisModem().waitResponse() == 1;
  47. }
  48. // Returns true on pick-up, false on error/busy
  49. bool callNumberImpl(const String& number) {
  50. if (number == GF("last")) {
  51. thisModem().sendAT(GF("DL"));
  52. } else {
  53. thisModem().sendAT(GF("D"), number, ";");
  54. }
  55. int8_t status = thisModem().waitResponse(60000L, GF("OK"), GF("BUSY"),
  56. GF("NO ANSWER"), GF("NO CARRIER"));
  57. switch (status) {
  58. case 1: return true;
  59. case 2:
  60. case 3: return false;
  61. default: return false;
  62. }
  63. }
  64. bool callHangupImpl() {
  65. thisModem().sendAT(GF("H"));
  66. return thisModem().waitResponse() == 1;
  67. }
  68. // 0-9,*,#,A,B,C,D
  69. bool dtmfSendImpl(char cmd, int duration_ms = 100) {
  70. duration_ms = constrain(duration_ms, 100, 1000);
  71. thisModem().sendAT(GF("+VTD="),
  72. duration_ms / 100); // VTD accepts in 1/10 of a second
  73. thisModem().waitResponse();
  74. thisModem().sendAT(GF("+VTS="), cmd);
  75. return thisModem().waitResponse(10000L) == 1;
  76. }
  77. };
  78. #endif // SRC_TINYGSMCALLING_H_