ping_sock.h 5.2 KB

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