esp32_compat.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _ESP32_COMPAT_H_
  14. #define _ESP32_COMPAT_H_
  15. #ifdef MDNS_TEST_MODE
  16. // Not to include
  17. #define ESP_MDNS_NETWORKING_H_
  18. #define _TCPIP_ADAPTER_H_
  19. #ifdef USE_BSD_STRING
  20. #include <bsd/string.h>
  21. #endif
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <stdbool.h>
  26. #include <unistd.h>
  27. #include <signal.h>
  28. #include <sys/time.h>
  29. #define CONFIG_MDNS_MAX_SERVICES 25
  30. #define ERR_OK 0
  31. #define ESP_OK 0
  32. #define ESP_FAIL -1
  33. #define ESP_ERR_NO_MEM 0x101
  34. #define ESP_ERR_INVALID_ARG 0x102
  35. #define ESP_ERR_INVALID_STATE 0x103
  36. #define ESP_ERR_INVALID_SIZE 0x104
  37. #define ESP_ERR_NOT_FOUND 0x105
  38. #define ESP_ERR_NOT_SUPPORTED 0x106
  39. #define ESP_ERR_TIMEOUT 0x107
  40. #define ESP_ERR_INVALID_RESPONSE 0x108
  41. #define ESP_ERR_INVALID_CRC 0x109
  42. #define pdTRUE true
  43. #define pdFALSE false
  44. #define pdPASS ( pdTRUE )
  45. #define pdFAIL ( pdFALSE )
  46. #define portMAX_DELAY 0xFFFFFFFF
  47. #define portTICK_PERIOD_MS 1
  48. #define ESP_LOGD(a,b)
  49. #define xSemaphoreTake(s,d)
  50. #define xTaskDelete(a)
  51. #define vTaskDelete(a) free(a)
  52. #define xSemaphoreGive(s)
  53. #define _mdns_pcb_init(a,b) true
  54. #define _mdns_pcb_deinit(a,b) true
  55. #define xSemaphoreCreateMutex() malloc(1)
  56. #define xSemaphoreCreateBinary() malloc(1)
  57. #define vSemaphoreDelete(s) free(s)
  58. #define xTaskCreatePinnedToCore(a,b,c,d,e,f,g) *(f) = malloc(1)
  59. #define vTaskDelay(m) usleep((m)*0)
  60. #define pbuf_free(p) free(p)
  61. #define esp_random() (rand()%UINT32_MAX)
  62. #define tcpip_adapter_get_ip_info(i,d) true
  63. #define tcpip_adapter_dhcpc_get_status(a, b) TCPIP_ADAPTER_DHCP_STARTED
  64. #define tcpip_adapter_get_ip6_linklocal(i,d) (ESP_OK)
  65. #define tcpip_adapter_get_hostname(i, n) *(n) = "esp32-0123456789AB"
  66. #define IP4_ADDR(ipaddr, a,b,c,d) \
  67. (ipaddr)->addr = ((uint32_t)((d) & 0xff) << 24) | \
  68. ((uint32_t)((c) & 0xff) << 16) | \
  69. ((uint32_t)((b) & 0xff) << 8) | \
  70. (uint32_t)((a) & 0xff)
  71. #define ip_2_ip6(ipaddr) (&((ipaddr)->u_addr.ip6))
  72. #define ip_2_ip4(ipaddr) (&((ipaddr)->u_addr.ip4))
  73. #define IPADDR_TYPE_V4 0U
  74. #define IPADDR_TYPE_V6 6U
  75. #define IPADDR_TYPE_ANY 46U
  76. #define IP_SET_TYPE_VAL(ipaddr, iptype) do { (ipaddr).type = (iptype); }while(0)
  77. #define IP_ADDR4(ipaddr,a,b,c,d) do { IP4_ADDR(ip_2_ip4(ipaddr),a,b,c,d); \
  78. IP_SET_TYPE_VAL(*(ipaddr), IPADDR_TYPE_V4); } while(0)
  79. #define IP_ADDR6(ipaddr,i0,i1,i2,i3) do { IP6_ADDR(ip_2_ip6(ipaddr),i0,i1,i2,i3); \
  80. IP_SET_TYPE_VAL(*(ipaddr), IPADDR_TYPE_V6); } while(0)
  81. #define IPADDR6_INIT(a, b, c, d) { { { { a, b, c, d } } }, IPADDR_TYPE_V6 }
  82. typedef int32_t esp_err_t;
  83. typedef void * xSemaphoreHandle;
  84. typedef void * SemaphoreHandle_t;
  85. typedef void * xQueueHandle;
  86. typedef void * QueueHandle_t;
  87. typedef void * TaskHandle_t;
  88. typedef void * esp_timer_handle_t;
  89. typedef uint32_t TickType_t;
  90. typedef uint32_t portTickType;
  91. typedef enum {
  92. WIFI_MODE_NULL = 0, /**< null mode */
  93. WIFI_MODE_STA, /**< WiFi station mode */
  94. WIFI_MODE_AP, /**< WiFi soft-AP mode */
  95. WIFI_MODE_APSTA, /**< WiFi station + soft-AP mode */
  96. WIFI_MODE_MAX
  97. } wifi_mode_t;
  98. extern const char * WIFI_EVENT;
  99. extern const char * IP_EVENT;
  100. extern const char * ETH_EVENT;
  101. typedef enum {
  102. WIFI_EVENT_WIFI_READY = 0, /**< ESP32 WiFi ready */
  103. WIFI_EVENT_SCAN_DONE, /**< ESP32 finish scanning AP */
  104. WIFI_EVENT_STA_START, /**< ESP32 station start */
  105. WIFI_EVENT_STA_STOP, /**< ESP32 station stop */
  106. WIFI_EVENT_STA_CONNECTED, /**< ESP32 station connected to AP */
  107. WIFI_EVENT_STA_DISCONNECTED, /**< ESP32 station disconnected from AP */
  108. WIFI_EVENT_STA_AUTHMODE_CHANGE, /**< the auth mode of AP connected by ESP32 station changed */
  109. WIFI_EVENT_STA_WPS_ER_SUCCESS, /**< ESP32 station wps succeeds in enrollee mode */
  110. WIFI_EVENT_STA_WPS_ER_FAILED, /**< ESP32 station wps fails in enrollee mode */
  111. WIFI_EVENT_STA_WPS_ER_TIMEOUT, /**< ESP32 station wps timeout in enrollee mode */
  112. WIFI_EVENT_STA_WPS_ER_PIN, /**< ESP32 station wps pin code in enrollee mode */
  113. WIFI_EVENT_AP_START, /**< ESP32 soft-AP start */
  114. WIFI_EVENT_AP_STOP, /**< ESP32 soft-AP stop */
  115. WIFI_EVENT_AP_STACONNECTED, /**< a station connected to ESP32 soft-AP */
  116. WIFI_EVENT_AP_STADISCONNECTED, /**< a station disconnected from ESP32 soft-AP */
  117. WIFI_EVENT_AP_PROBEREQRECVED, /**< Receive probe request packet in soft-AP interface */
  118. } wifi_event_t;
  119. typedef enum {
  120. ETHERNET_EVENT_START, /**< ESP32 ethernet start */
  121. ETHERNET_EVENT_STOP, /**< ESP32 ethernet stop */
  122. ETHERNET_EVENT_CONNECTED, /**< ESP32 ethernet phy link up */
  123. ETHERNET_EVENT_DISCONNECTED, /**< ESP32 ethernet phy link down */
  124. } eth_event_t;
  125. typedef enum {
  126. IP_EVENT_STA_GOT_IP, /*!< ESP32 station got IP from connected AP */
  127. IP_EVENT_STA_LOST_IP, /*!< ESP32 station lost IP and the IP is reset to 0 */
  128. IP_EVENT_AP_STAIPASSIGNED, /*!< ESP32 soft-AP assign an IP to a connected station */
  129. IP_EVENT_GOT_IP6, /*!< ESP32 station or ap or ethernet interface v6IP addr is preferred */
  130. IP_EVENT_ETH_GOT_IP, /*!< ESP32 ethernet got IP from connected AP */
  131. } ip_event_t;
  132. /* status of DHCP client or DHCP server */
  133. typedef enum {
  134. TCPIP_ADAPTER_DHCP_INIT = 0, /**< DHCP client/server in initial state */
  135. TCPIP_ADAPTER_DHCP_STARTED, /**< DHCP client/server already been started */
  136. TCPIP_ADAPTER_DHCP_STOPPED, /**< DHCP client/server already been stopped */
  137. TCPIP_ADAPTER_DHCP_STATUS_MAX
  138. } tcpip_adapter_dhcp_status_t;
  139. struct udp_pcb {
  140. uint8_t dummy;
  141. };
  142. struct ip4_addr {
  143. uint32_t addr;
  144. };
  145. typedef struct ip4_addr ip4_addr_t;
  146. struct ip6_addr {
  147. uint32_t addr[4];
  148. };
  149. typedef struct ip6_addr ip6_addr_t;
  150. typedef struct _ip_addr {
  151. union {
  152. ip6_addr_t ip6;
  153. ip4_addr_t ip4;
  154. } u_addr;
  155. uint8_t type;
  156. } ip_addr_t;
  157. typedef struct {
  158. ip4_addr_t ip;
  159. ip4_addr_t netmask;
  160. ip4_addr_t gw;
  161. } tcpip_adapter_ip_info_t;
  162. typedef enum {
  163. TCPIP_ADAPTER_IF_STA = 0, /**< ESP32 station interface */
  164. TCPIP_ADAPTER_IF_AP, /**< ESP32 soft-AP interface */
  165. TCPIP_ADAPTER_IF_ETH, /**< ESP32 ethernet interface */
  166. TCPIP_ADAPTER_IF_MAX
  167. } tcpip_adapter_if_t;
  168. typedef struct {
  169. ip6_addr_t ip;
  170. } tcpip_adapter_ip6_info_t;
  171. typedef struct {
  172. tcpip_adapter_if_t if_index; /*!< Interface for which the event is received */
  173. tcpip_adapter_ip6_info_t ip6_info; /*!< IPv6 address of the interface */
  174. } ip_event_got_ip6_t;
  175. typedef void* system_event_t;
  176. inline esp_err_t esp_wifi_get_mode(wifi_mode_t * mode)
  177. {
  178. *mode = WIFI_MODE_APSTA;
  179. return ESP_OK;
  180. }
  181. struct pbuf {
  182. struct pbuf *next;
  183. void *payload;
  184. uint16_t tot_len;
  185. uint16_t len;
  186. uint8_t /*pbuf_type*/ type;
  187. uint8_t flags;
  188. uint16_t ref;
  189. };
  190. #define IP_GET_TYPE(ipaddr) ((ipaddr)->type)
  191. #define IP_IS_V6_VAL(ipaddr) (IP_GET_TYPE(&ipaddr) == IPADDR_TYPE_V6)
  192. #define ip4_addr_copy(dest, src) ((dest).addr = (src).addr)
  193. #define ip6_addr_copy(dest, src) do{(dest).addr[0] = (src).addr[0]; \
  194. (dest).addr[1] = (src).addr[1]; \
  195. (dest).addr[2] = (src).addr[2]; \
  196. (dest).addr[3] = (src).addr[3];}while(0)
  197. #define ip_addr_copy(dest, src) do{ IP_SET_TYPE_VAL(dest, IP_GET_TYPE(&src)); if(IP_IS_V6_VAL(src)){ \
  198. ip6_addr_copy(*ip_2_ip6(&(dest)), *ip_2_ip6(&(src))); }else{ \
  199. ip4_addr_copy(*ip_2_ip4(&(dest)), *ip_2_ip4(&(src))); }}while(0)
  200. #include "esp32_mock.h"
  201. uint32_t xTaskGetTickCount(void);
  202. #endif //MDNS_TEST_MODE
  203. #endif //_ESP32_COMPAT_H_