esp_modem.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "esp_modem_dce.h"
  19. #include "esp_modem_dte.h"
  20. #include "esp_event.h"
  21. #include "driver/uart.h"
  22. #include "esp_modem_compat.h"
  23. /**
  24. * @brief Declare Event Base for ESP Modem
  25. *
  26. */
  27. ESP_EVENT_DECLARE_BASE(ESP_MODEM_EVENT);
  28. /**
  29. * @brief ESP Modem Event
  30. *
  31. */
  32. typedef enum {
  33. ESP_MODEM_EVENT_PPP_START = 0, /*!< ESP Modem Start PPP Session */
  34. ESP_MODEM_EVENT_PPP_STOP = 3, /*!< ESP Modem Stop PPP Session*/
  35. ESP_MODEM_EVENT_UNKNOWN = 4 /*!< ESP Modem Unknown Response */
  36. } esp_modem_event_t;
  37. /**
  38. * @brief ESP Modem DTE Configuration
  39. *
  40. */
  41. typedef struct {
  42. uart_port_t port_num; /*!< UART port number */
  43. uart_word_length_t data_bits; /*!< Data bits of UART */
  44. uart_stop_bits_t stop_bits; /*!< Stop bits of UART */
  45. uart_parity_t parity; /*!< Parity type */
  46. modem_flow_ctrl_t flow_control; /*!< Flow control type */
  47. uint32_t baud_rate; /*!< Communication baud rate */
  48. int tx_io_num; /*!< TXD Pin Number */
  49. int rx_io_num; /*!< RXD Pin Number */
  50. int rts_io_num; /*!< RTS Pin Number */
  51. int cts_io_num; /*!< CTS Pin Number */
  52. int rx_buffer_size; /*!< UART RX Buffer Size */
  53. int tx_buffer_size; /*!< UART TX Buffer Size */
  54. int event_queue_size; /*!< UART Event Queue Size */
  55. uint32_t event_task_stack_size; /*!< UART Event Task Stack size */
  56. int event_task_priority; /*!< UART Event Task Priority */
  57. union {
  58. int dte_buffer_size; /*!< Internal buffer size */
  59. int line_buffer_size; /*!< Compatible option for the internal buffer size */
  60. };
  61. } esp_modem_dte_config_t;
  62. /**
  63. * @brief Type used for reception callback
  64. *
  65. */
  66. typedef esp_err_t (*esp_modem_on_receive)(void *buffer, size_t len, void *context);
  67. /**
  68. * @brief ESP Modem DTE Default Configuration
  69. *
  70. */
  71. #define ESP_MODEM_DTE_DEFAULT_CONFIG() \
  72. { \
  73. .port_num = UART_NUM_1, \
  74. .data_bits = UART_DATA_8_BITS, \
  75. .stop_bits = UART_STOP_BITS_1, \
  76. .parity = UART_PARITY_DISABLE, \
  77. .baud_rate = 115200, \
  78. .flow_control = MODEM_FLOW_CONTROL_NONE,\
  79. .tx_io_num = 25, \
  80. .rx_io_num = 26, \
  81. .rts_io_num = 27, \
  82. .cts_io_num = 23, \
  83. .rx_buffer_size = 1024, \
  84. .tx_buffer_size = 512, \
  85. .event_queue_size = 30, \
  86. .event_task_stack_size = 2048, \
  87. .event_task_priority = 5, \
  88. .dte_buffer_size = 512 \
  89. }
  90. /**
  91. * @brief Create and initialize Modem DTE object
  92. *
  93. * @param config configuration of ESP Modem DTE object
  94. * @return modem_dte_t*
  95. * - Modem DTE object
  96. */
  97. modem_dte_t *esp_modem_dte_init(const esp_modem_dte_config_t *config);
  98. /**
  99. * @brief Register event handler for ESP Modem event loop
  100. *
  101. * @param dte modem_dte_t type object
  102. * @param handler event handler to register
  103. * @param handler_args arguments for registered handler
  104. * @return esp_err_t
  105. * - ESP_OK on success
  106. * - ESP_ERR_NO_MEM on allocating memory for the handler failed
  107. * - ESP_ERR_INVALID_ARG on invalid combination of event base and event id
  108. */
  109. esp_err_t esp_modem_set_event_handler(modem_dte_t *dte, esp_event_handler_t handler, int32_t event_id, void *handler_args);
  110. /**
  111. * @brief Unregister event handler for ESP Modem event loop
  112. *
  113. * @param dte modem_dte_t type object
  114. * @param handler event handler to unregister
  115. * @return esp_err_t
  116. * - ESP_OK on success
  117. * - ESP_ERR_INVALID_ARG on invalid combination of event base and event id
  118. */
  119. esp_err_t esp_modem_remove_event_handler(modem_dte_t *dte, esp_event_handler_t handler);
  120. /**
  121. * @brief Setup PPP Session
  122. *
  123. * @param dte Modem DTE object
  124. * @return esp_err_t
  125. * - ESP_OK on success
  126. * - ESP_FAIL on error
  127. */
  128. esp_err_t esp_modem_start_ppp(modem_dte_t *dte);
  129. /**
  130. * @brief Exit PPP Session
  131. *
  132. * @param dte Modem DTE Object
  133. * @return esp_err_t
  134. * - ESP_OK on success
  135. * - ESP_FAIL on error
  136. */
  137. esp_err_t esp_modem_stop_ppp(modem_dte_t *dte);
  138. /**
  139. * @brief Setup on reception callback
  140. *
  141. * @param dte ESP Modem DTE object
  142. * @param receive_cb Function pointer to the reception callback
  143. * @param receive_cb_ctx Contextual pointer to be passed to the reception callback
  144. *
  145. * @return ESP_OK on success
  146. */
  147. esp_err_t esp_modem_set_rx_cb(modem_dte_t *dte, esp_modem_on_receive receive_cb, void *receive_cb_ctx);
  148. /**
  149. * @brief Notify the modem, that ppp netif has closed
  150. *
  151. * @note This API should only be used internally by the modem-netif layer
  152. *
  153. * @param dte ESP Modem DTE object
  154. *
  155. * @return ESP_OK on success
  156. */
  157. esp_err_t esp_modem_notify_ppp_netif_closed(modem_dte_t *dte);
  158. #ifdef __cplusplus
  159. }
  160. #endif