ping_sock.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. } esp_ping_config_t;
  66. /**
  67. * @brief Default ping configuration
  68. *
  69. */
  70. #define ESP_PING_DEFAULT_CONFIG() \
  71. { \
  72. .count = 5, \
  73. .interval_ms = 1000, \
  74. .timeout_ms = 1000, \
  75. .data_size = 64, \
  76. .tos = 0, \
  77. .target_addr = ip_addr_any_type, \
  78. .task_stack_size = 2048, \
  79. .task_prio = 2, \
  80. }
  81. #define ESP_PING_COUNT_INFINITE (0) /*!< Set ping count to zero will ping target infinitely */
  82. /**
  83. * @brief Profile of ping session
  84. *
  85. */
  86. typedef enum {
  87. ESP_PING_PROF_SEQNO, /*!< Sequence number of a ping procedure */
  88. ESP_PING_PROF_TTL, /*!< Time to live of a ping procedure */
  89. ESP_PING_PROF_REQUEST, /*!< Number of request packets sent out */
  90. ESP_PING_PROF_REPLY, /*!< Number of reply packets received */
  91. ESP_PING_PROF_IPADDR, /*!< IP address of replied target */
  92. ESP_PING_PROF_SIZE, /*!< Size of received packet */
  93. ESP_PING_PROF_TIMEGAP, /*!< Elapsed time between request and reply packet */
  94. ESP_PING_PROF_DURATION /*!< Elapsed time of the whole ping session */
  95. } esp_ping_profile_t;
  96. /**
  97. * @brief Create a ping session
  98. *
  99. * @param config ping configuration
  100. * @param cbs a bunch of callback functions invoked by internal ping task
  101. * @param hdl_out handle of ping session
  102. * @return
  103. * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. configuration is null, etc)
  104. * - ESP_ERR_NO_MEM: out of memory
  105. * - ESP_FAIL: other internal error (e.g. socket error)
  106. * - ESP_OK: create ping session successfully, user can take the ping handle to do follow-on jobs
  107. */
  108. 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);
  109. /**
  110. * @brief Delete a ping session
  111. *
  112. * @param hdl handle of ping session
  113. * @return
  114. * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. ping handle is null, etc)
  115. * - ESP_OK: delete ping session successfully
  116. */
  117. esp_err_t esp_ping_delete_session(esp_ping_handle_t hdl);
  118. /**
  119. * @brief Start the ping session
  120. *
  121. * @param hdl handle of ping session
  122. * @return
  123. * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. ping handle is null, etc)
  124. * - ESP_OK: start ping session successfully
  125. */
  126. esp_err_t esp_ping_start(esp_ping_handle_t hdl);
  127. /**
  128. * @brief Stop the ping session
  129. *
  130. * @param hdl handle of ping session
  131. * @return
  132. * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. ping handle is null, etc)
  133. * - ESP_OK: stop ping session successfully
  134. */
  135. esp_err_t esp_ping_stop(esp_ping_handle_t hdl);
  136. /**
  137. * @brief Get runtime profile of ping session
  138. *
  139. * @param hdl handle of ping session
  140. * @param profile type of profile
  141. * @param data profile data
  142. * @param size profile data size
  143. * @return
  144. * - ESP_ERR_INVALID_ARG: invalid parameters (e.g. ping handle is null, etc)
  145. * - ESP_ERR_INVALID_SIZE: the actual profile data size doesn't match the "size" parameter
  146. * - ESP_OK: get profile successfully
  147. */
  148. esp_err_t esp_ping_get_profile(esp_ping_handle_t hdl, esp_ping_profile_t profile, void *data, uint32_t size);
  149. #ifdef __cplusplus
  150. }
  151. #endif