ping_sock.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2019 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 <stdint.h>
  19. #include "esp_err.h"
  20. #include "lwip/ip_addr.h"
  21. /**
  22. * @brief Type of "ping" session handle
  23. *
  24. */
  25. typedef void *esp_ping_handle_t;
  26. /**
  27. * @brief Type of "ping" callback functions
  28. *
  29. */
  30. typedef struct {
  31. /**
  32. * @brief arguments for callback functions
  33. *
  34. */
  35. void *cb_args;
  36. /**
  37. * @brief Invoked by internal ping thread when received ICMP echo reply packet
  38. *
  39. */
  40. void (*on_ping_success)(esp_ping_handle_t hdl, void *args);
  41. /**
  42. * @brief Invoked by internal ping thread when receive ICMP echo reply packet timeout
  43. *
  44. */
  45. void (*on_ping_timeout)(esp_ping_handle_t hdl, void *args);
  46. /**
  47. * @brief Invoked by internal ping thread when a ping session is finished
  48. *
  49. */
  50. void (*on_ping_end)(esp_ping_handle_t hdl, void *args);
  51. } esp_ping_callbacks_t;
  52. /**
  53. * @brief Type of "ping" configuration
  54. *
  55. */
  56. typedef struct {
  57. uint32_t count; /*!< A "ping" session contains count procedures */
  58. uint32_t interval_ms; /*!< Milliseconds between each ping procedure */
  59. uint32_t timeout_ms; /*!< Timeout value (in milliseconds) of each ping procedure */
  60. uint32_t data_size; /*!< Size of the data next to ICMP packet header */
  61. uint8_t tos; /*!< Type of Service, a field specified in the IP header */
  62. ip_addr_t target_addr; /*!< Target IP address, either IPv4 or IPv6 */
  63. uint32_t task_stack_size; /*!< Stack size of internal ping task */
  64. uint32_t task_prio; /*!< Priority of internal ping task */
  65. uint32_t interface; /*!< Netif index, interface=0 means NETIF_NO_INDEX*/
  66. } esp_ping_config_t;
  67. /**
  68. * @brief Default ping configuration
  69. *
  70. */
  71. #define ESP_PING_DEFAULT_CONFIG() \
  72. { \
  73. .count = 5, \
  74. .interval_ms = 1000, \
  75. .timeout_ms = 1000, \
  76. .data_size = 64, \
  77. .tos = 0, \
  78. .target_addr = *(IP_ANY_TYPE), \
  79. .task_stack_size = 2048, \
  80. .task_prio = 2, \
  81. .interface = 0,\
  82. }
  83. #define ESP_PING_COUNT_INFINITE (0) /*!< Set ping count to zero will ping target infinitely */
  84. /**
  85. * @brief Profile of ping session
  86. *
  87. */
  88. typedef enum {
  89. ESP_PING_PROF_SEQNO, /*!< Sequence number of a ping procedure */
  90. ESP_PING_PROF_TTL, /*!< Time to live of a ping procedure */
  91. ESP_PING_PROF_REQUEST, /*!< Number of request packets sent out */
  92. ESP_PING_PROF_REPLY, /*!< Number of reply packets received */
  93. ESP_PING_PROF_IPADDR, /*!< IP address of replied target */
  94. ESP_PING_PROF_SIZE, /*!< Size of received packet */
  95. ESP_PING_PROF_TIMEGAP, /*!< Elapsed time between request and reply packet */
  96. ESP_PING_PROF_DURATION /*!< Elapsed time of the whole ping session */
  97. } esp_ping_profile_t;
  98. /**
  99. * @brief Create a ping session
  100. *
  101. * @param config ping configuration
  102. * @param cbs a bunch of callback functions invoked by internal ping task
  103. * @param hdl_out handle of ping session
  104. * @return
  105. * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. configuration is null, etc)
  106. * - ESP_ERR_NO_MEM: out of memory
  107. * - ESP_FAIL: other internal error (e.g. socket error)
  108. * - ESP_OK: create ping session successfully, user can take the ping handle to do follow-on jobs
  109. */
  110. esp_err_t esp_ping_new_session(const esp_ping_config_t *config, const esp_ping_callbacks_t *cbs, esp_ping_handle_t *hdl_out);
  111. /**
  112. * @brief Delete a ping session
  113. *
  114. * @param hdl handle of ping session
  115. * @return
  116. * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. ping handle is null, etc)
  117. * - ESP_OK: delete ping session successfully
  118. */
  119. esp_err_t esp_ping_delete_session(esp_ping_handle_t hdl);
  120. /**
  121. * @brief Start the ping session
  122. *
  123. * @param hdl handle of ping session
  124. * @return
  125. * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. ping handle is null, etc)
  126. * - ESP_OK: start ping session successfully
  127. */
  128. esp_err_t esp_ping_start(esp_ping_handle_t hdl);
  129. /**
  130. * @brief Stop the ping session
  131. *
  132. * @param hdl handle of ping session
  133. * @return
  134. * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. ping handle is null, etc)
  135. * - ESP_OK: stop ping session successfully
  136. */
  137. esp_err_t esp_ping_stop(esp_ping_handle_t hdl);
  138. /**
  139. * @brief Get runtime profile of ping session
  140. *
  141. * @param hdl handle of ping session
  142. * @param profile type of profile
  143. * @param data profile data
  144. * @param size profile data size
  145. * @return
  146. * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. ping handle is null, etc)
  147. * - ESP_ERR_INVALID_SIZE: the actual profile data size doesn't match the "size" parameter
  148. * - ESP_OK: get profile successfully
  149. */
  150. esp_err_t esp_ping_get_profile(esp_ping_handle_t hdl, esp_ping_profile_t profile, void *data, uint32_t size);
  151. #ifdef __cplusplus
  152. }
  153. #endif