test_emac.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "freertos/event_groups.h"
  6. #include "unity.h"
  7. #include "test_utils.h"
  8. #include "esp_event.h"
  9. #include "esp_eth.h"
  10. #include "esp_log.h"
  11. #include "esp_http_client.h"
  12. #include "lwip/inet.h"
  13. #include "lwip/netdb.h"
  14. #include "lwip/sockets.h"
  15. #include "ping/ping_sock.h"
  16. #include "esp_rom_md5.h"
  17. #include "soc/soc_caps.h"
  18. #if SOC_EMAC_SUPPORTED
  19. static const char *TAG = "esp32_eth_test";
  20. #define ETH_START_BIT BIT(0)
  21. #define ETH_STOP_BIT BIT(1)
  22. #define ETH_CONNECT_BIT BIT(2)
  23. #define ETH_GOT_IP_BIT BIT(3)
  24. #define ETH_PING_END_BIT BIT(4)
  25. #define ETH_DOWNLOAD_END_BIT BIT(5)
  26. #define ETH_START_TIMEOUT_MS (10000)
  27. #define ETH_CONNECT_TIMEOUT_MS (40000)
  28. #define ETH_STOP_TIMEOUT_MS (10000)
  29. #define ETH_GET_IP_TIMEOUT_MS (60000)
  30. #define ETH_DOWNLOAD_END_TIMEOUT_MS (240000)
  31. #define ETH_PING_DURATION_MS (5000)
  32. #define ETH_PING_END_TIMEOUT_MS (ETH_PING_DURATION_MS * 2)
  33. #define TEST_ICMP_DESTINATION_DOMAIN_NAME "127.0.0.1"
  34. // compute md5 of download file
  35. static md5_context_t md5_context;
  36. static uint8_t digest[16];
  37. /** Event handler for Ethernet events */
  38. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  39. int32_t event_id, void *event_data)
  40. {
  41. EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg;
  42. switch (event_id) {
  43. case ETHERNET_EVENT_CONNECTED:
  44. xEventGroupSetBits(eth_event_group, ETH_CONNECT_BIT);
  45. ESP_LOGI(TAG, "Ethernet Link Up");
  46. break;
  47. case ETHERNET_EVENT_DISCONNECTED:
  48. ESP_LOGI(TAG, "Ethernet Link Down");
  49. break;
  50. case ETHERNET_EVENT_START:
  51. xEventGroupSetBits(eth_event_group, ETH_START_BIT);
  52. ESP_LOGI(TAG, "Ethernet Started");
  53. break;
  54. case ETHERNET_EVENT_STOP:
  55. xEventGroupSetBits(eth_event_group, ETH_STOP_BIT);
  56. ESP_LOGI(TAG, "Ethernet Stopped");
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. /** Event handler for IP_EVENT_ETH_GOT_IP */
  63. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  64. int32_t event_id, void *event_data)
  65. {
  66. EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg;
  67. ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  68. const esp_netif_ip_info_t *ip_info = &event->ip_info;
  69. ESP_LOGI(TAG, "Ethernet Got IP Address");
  70. ESP_LOGI(TAG, "~~~~~~~~~~~");
  71. ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
  72. ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
  73. ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
  74. ESP_LOGI(TAG, "~~~~~~~~~~~");
  75. xEventGroupSetBits(eth_event_group, ETH_GOT_IP_BIT);
  76. }
  77. static void test_on_ping_success(esp_ping_handle_t hdl, void *args)
  78. {
  79. uint8_t ttl;
  80. uint16_t seqno;
  81. uint32_t elapsed_time, recv_len;
  82. ip_addr_t target_addr;
  83. esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
  84. esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl));
  85. esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
  86. esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
  87. esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
  88. printf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n",
  89. recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time);
  90. }
  91. static void test_on_ping_timeout(esp_ping_handle_t hdl, void *args)
  92. {
  93. uint16_t seqno;
  94. ip_addr_t target_addr;
  95. esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
  96. esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
  97. printf("From %s icmp_seq=%d timeout\n", inet_ntoa(target_addr.u_addr.ip4), seqno);
  98. }
  99. static void test_on_ping_end(esp_ping_handle_t hdl, void *args)
  100. {
  101. EventGroupHandle_t eth_event_group = (EventGroupHandle_t)args;
  102. uint32_t transmitted;
  103. uint32_t received;
  104. uint32_t total_time_ms;
  105. esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted));
  106. esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received));
  107. esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms));
  108. printf("%d packets transmitted, %d received, time %dms\n", transmitted, received, total_time_ms);
  109. if (transmitted == received) {
  110. xEventGroupSetBits(eth_event_group, ETH_PING_END_BIT);
  111. }
  112. }
  113. static esp_err_t test_uninstall_driver(esp_eth_handle_t eth_hdl, uint32_t ms_to_wait)
  114. {
  115. int i = 0;
  116. ms_to_wait += 100;
  117. for (i = 0; i < ms_to_wait / 100; i++) {
  118. vTaskDelay(pdMS_TO_TICKS(100));
  119. if (esp_eth_driver_uninstall(eth_hdl) == ESP_OK) {
  120. break;
  121. }
  122. }
  123. if (i < ms_to_wait / 10) {
  124. return ESP_OK;
  125. } else {
  126. return ESP_FAIL;
  127. }
  128. }
  129. TEST_CASE("esp32 ethernet io test", "[ethernet][test_env=UT_T2_Ethernet]")
  130. {
  131. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  132. mac_config.flags = ETH_MAC_FLAG_PIN_TO_CORE; // pin to core
  133. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  134. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  135. // auto detect PHY address
  136. phy_config.phy_addr = ESP_ETH_PHY_ADDR_AUTO;
  137. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  138. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  139. esp_eth_handle_t eth_handle = NULL;
  140. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  141. /* get MAC address */
  142. uint8_t mac_addr[6];
  143. memset(mac_addr, 0, sizeof(mac_addr));
  144. TEST_ESP_OK(esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr));
  145. ESP_LOGI(TAG, "Ethernet MAC Address: %02x:%02x:%02x:%02x:%02x:%02x",
  146. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  147. TEST_ASSERT(mac_addr[0] != 0);
  148. /* get PHY address */
  149. int phy_addr = -1;
  150. TEST_ESP_OK(esp_eth_ioctl(eth_handle, ETH_CMD_G_PHY_ADDR, &phy_addr));
  151. ESP_LOGI(TAG, "Ethernet PHY Address: %d", phy_addr);
  152. TEST_ASSERT(phy_addr >= 0 && phy_addr <= 31);
  153. TEST_ESP_OK(esp_eth_driver_uninstall(eth_handle));
  154. TEST_ESP_OK(phy->del(phy));
  155. TEST_ESP_OK(mac->del(mac));
  156. }
  157. TEST_CASE("esp32 ethernet event test", "[ethernet][test_env=UT_T2_Ethernet]")
  158. {
  159. EventBits_t bits = 0;
  160. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  161. TEST_ASSERT(eth_event_group != NULL);
  162. TEST_ESP_OK(esp_event_loop_create_default());
  163. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  164. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  165. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  166. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  167. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  168. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  169. esp_eth_handle_t eth_handle = NULL;
  170. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  171. // this test only test layer2 event, so don't need to register input callback (i.e. esp_eth_update_input_path)
  172. TEST_ESP_OK(esp_eth_start(eth_handle));
  173. /* wait for connection start */
  174. bits = xEventGroupWaitBits(eth_event_group, ETH_START_BIT, true, true, pdMS_TO_TICKS(ETH_START_TIMEOUT_MS));
  175. TEST_ASSERT((bits & ETH_START_BIT) == ETH_START_BIT);
  176. /* wait for connection establish */
  177. bits = xEventGroupWaitBits(eth_event_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(ETH_CONNECT_TIMEOUT_MS));
  178. TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
  179. // stop Ethernet driver
  180. TEST_ESP_OK(esp_eth_stop(eth_handle));
  181. /* wait for connection stop */
  182. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS));
  183. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  184. /* driver should be uninstalled within 2 seconds */
  185. TEST_ESP_OK(test_uninstall_driver(eth_handle, 2000));
  186. TEST_ESP_OK(phy->del(phy));
  187. TEST_ESP_OK(mac->del(mac));
  188. TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler));
  189. TEST_ESP_OK(esp_event_loop_delete_default());
  190. vEventGroupDelete(eth_event_group);
  191. }
  192. TEST_CASE("esp32 ethernet dhcp test", "[ethernet][test_env=UT_T2_Ethernet]")
  193. {
  194. EventBits_t bits = 0;
  195. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  196. TEST_ASSERT(eth_event_group != NULL);
  197. test_case_uses_tcpip();
  198. TEST_ESP_OK(esp_event_loop_create_default());
  199. // create TCP/IP netif
  200. esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
  201. esp_netif_t *eth_netif = esp_netif_new(&netif_cfg);
  202. // set default handlers to do layer 3 (and up) stuffs
  203. TEST_ESP_OK(esp_eth_set_default_handlers(eth_netif));
  204. // register user defined event handers
  205. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  206. TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group));
  207. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  208. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  209. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  210. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  211. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  212. esp_eth_handle_t eth_handle = NULL;
  213. // install Ethernet driver
  214. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  215. // combine driver with netif
  216. void *glue = esp_eth_new_netif_glue(eth_handle);
  217. TEST_ESP_OK(esp_netif_attach(eth_netif, glue));
  218. // start Ethernet driver
  219. TEST_ESP_OK(esp_eth_start(eth_handle));
  220. /* wait for IP lease */
  221. bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS));
  222. TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT);
  223. // stop Ethernet driver
  224. TEST_ESP_OK(esp_eth_stop(eth_handle));
  225. /* wait for connection stop */
  226. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS));
  227. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  228. TEST_ESP_OK(esp_eth_del_netif_glue(glue));
  229. /* driver should be uninstalled within 2 seconds */
  230. TEST_ESP_OK(test_uninstall_driver(eth_handle, 2000));
  231. TEST_ESP_OK(phy->del(phy));
  232. TEST_ESP_OK(mac->del(mac));
  233. TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler));
  234. TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler));
  235. TEST_ESP_OK(esp_eth_clear_default_handlers(eth_netif));
  236. esp_netif_destroy(eth_netif);
  237. TEST_ESP_OK(esp_event_loop_delete_default());
  238. vEventGroupDelete(eth_event_group);
  239. }
  240. TEST_CASE("esp32 ethernet start/stop stress test", "[ethernet][test_env=UT_T2_Ethernet][timeout=240]")
  241. {
  242. EventBits_t bits = 0;
  243. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  244. TEST_ASSERT(eth_event_group != NULL);
  245. test_case_uses_tcpip();
  246. TEST_ESP_OK(esp_event_loop_create_default());
  247. // create TCP/IP netif
  248. esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
  249. esp_netif_t *eth_netif = esp_netif_new(&netif_cfg);
  250. // set default handlers to do layer 3 (and up) stuffs
  251. TEST_ESP_OK(esp_eth_set_default_handlers(eth_netif));
  252. // register user defined event handers
  253. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  254. TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group));
  255. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  256. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  257. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  258. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  259. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  260. esp_eth_handle_t eth_handle = NULL;
  261. // install Ethernet driver
  262. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  263. // combine driver with netif
  264. void *glue = esp_eth_new_netif_glue(eth_handle);
  265. TEST_ESP_OK(esp_netif_attach(eth_netif, glue));
  266. for (int i = 0; i < 10; i++) {
  267. // start Ethernet driver
  268. TEST_ESP_OK(esp_eth_start(eth_handle));
  269. /* wait for IP lease */
  270. bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS));
  271. TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT);
  272. // stop Ethernet driver
  273. TEST_ESP_OK(esp_eth_stop(eth_handle));
  274. /* wait for connection stop */
  275. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS));
  276. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  277. }
  278. TEST_ESP_OK(esp_eth_del_netif_glue(glue));
  279. /* driver should be uninstalled within 2 seconds */
  280. TEST_ESP_OK(test_uninstall_driver(eth_handle, 2000));
  281. TEST_ESP_OK(phy->del(phy));
  282. TEST_ESP_OK(mac->del(mac));
  283. TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler));
  284. TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler));
  285. TEST_ESP_OK(esp_eth_clear_default_handlers(eth_netif));
  286. esp_netif_destroy(eth_netif);
  287. TEST_ESP_OK(esp_event_loop_delete_default());
  288. vEventGroupDelete(eth_event_group);
  289. }
  290. TEST_CASE("esp32 ethernet icmp test", "[ethernet][test_env=UT_T2_Ethernet]")
  291. {
  292. EventBits_t bits = 0;
  293. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  294. TEST_ASSERT(eth_event_group != NULL);
  295. test_case_uses_tcpip();
  296. TEST_ESP_OK(esp_event_loop_create_default());
  297. // create TCP/IP netif
  298. esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
  299. esp_netif_t *eth_netif = esp_netif_new(&netif_cfg);
  300. // set default handlers to do layer 3 (and up) stuffs
  301. TEST_ESP_OK(esp_eth_set_default_handlers(eth_netif));
  302. // register user defined event handers
  303. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  304. TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group));
  305. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  306. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  307. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  308. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  309. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  310. esp_eth_handle_t eth_handle = NULL;
  311. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  312. // combine driver with netif
  313. void *glue = esp_eth_new_netif_glue(eth_handle);
  314. TEST_ESP_OK(esp_netif_attach(eth_netif, glue));
  315. // start Ethernet driver
  316. TEST_ESP_OK(esp_eth_start(eth_handle));
  317. /* wait for IP lease */
  318. bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS));
  319. TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT);
  320. // Parse IP address
  321. ip_addr_t target_addr;
  322. struct addrinfo hint;
  323. struct addrinfo *res = NULL;
  324. memset(&hint, 0, sizeof(hint));
  325. memset(&target_addr, 0, sizeof(target_addr));
  326. /* convert URL to IP */
  327. TEST_ASSERT(getaddrinfo(TEST_ICMP_DESTINATION_DOMAIN_NAME, NULL, &hint, &res) == 0);
  328. struct in_addr addr4 = ((struct sockaddr_in *)(res->ai_addr))->sin_addr;
  329. inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4);
  330. freeaddrinfo(res);
  331. esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG();
  332. ping_config.timeout_ms = 2000;
  333. ping_config.target_addr = target_addr;
  334. ping_config.count = 0; // ping in infinite mode
  335. /* set callback functions */
  336. esp_ping_callbacks_t cbs;
  337. cbs.on_ping_success = test_on_ping_success;
  338. cbs.on_ping_timeout = test_on_ping_timeout;
  339. cbs.on_ping_end = test_on_ping_end;
  340. cbs.cb_args = eth_event_group;
  341. esp_ping_handle_t ping;
  342. TEST_ESP_OK(esp_ping_new_session(&ping_config, &cbs, &ping));
  343. /* start ping */
  344. TEST_ESP_OK(esp_ping_start(ping));
  345. /* ping for a while */
  346. vTaskDelay(pdMS_TO_TICKS(ETH_PING_DURATION_MS));
  347. /* stop ping */
  348. TEST_ESP_OK(esp_ping_stop(ping));
  349. /* wait for end of ping */
  350. bits = xEventGroupWaitBits(eth_event_group, ETH_PING_END_BIT, true, true, pdMS_TO_TICKS(ETH_PING_END_TIMEOUT_MS));
  351. TEST_ASSERT((bits & ETH_PING_END_BIT) == ETH_PING_END_BIT);
  352. /* restart ping */
  353. TEST_ESP_OK(esp_ping_start(ping));
  354. vTaskDelay(pdMS_TO_TICKS(ETH_PING_DURATION_MS));
  355. TEST_ESP_OK(esp_ping_stop(ping));
  356. bits = xEventGroupWaitBits(eth_event_group, ETH_PING_END_BIT, true, true, pdMS_TO_TICKS(ETH_PING_END_TIMEOUT_MS));
  357. TEST_ASSERT((bits & ETH_PING_END_BIT) == ETH_PING_END_BIT);
  358. /* de-initialize ping process */
  359. TEST_ESP_OK(esp_ping_delete_session(ping));
  360. // stop Ethernet driver
  361. TEST_ESP_OK(esp_eth_stop(eth_handle));
  362. /* wait for connection stop */
  363. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS));
  364. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  365. TEST_ESP_OK(esp_eth_del_netif_glue(glue));
  366. /* driver should be uninstalled within 2 seconds */
  367. TEST_ESP_OK(test_uninstall_driver(eth_handle, 2000));
  368. TEST_ESP_OK(phy->del(phy));
  369. TEST_ESP_OK(mac->del(mac));
  370. TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler));
  371. TEST_ESP_OK(esp_eth_clear_default_handlers(eth_netif));
  372. esp_netif_destroy(eth_netif);
  373. TEST_ESP_OK(esp_event_loop_delete_default());
  374. vEventGroupDelete(eth_event_group);
  375. }
  376. esp_err_t http_event_handle(esp_http_client_event_t *evt)
  377. {
  378. switch (evt->event_id) {
  379. case HTTP_EVENT_ERROR:
  380. ESP_LOGE(TAG, "HTTP_EVENT_ERROR");
  381. break;
  382. case HTTP_EVENT_ON_CONNECTED:
  383. ESP_LOGI(TAG, "HTTP_EVENT_ON_CONNECTED");
  384. break;
  385. case HTTP_EVENT_HEADER_SENT:
  386. ESP_LOGI(TAG, "HTTP_EVENT_HEADER_SENT");
  387. break;
  388. case HTTP_EVENT_ON_HEADER:
  389. ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER");
  390. break;
  391. case HTTP_EVENT_ON_DATA:
  392. esp_rom_md5_update(&md5_context, evt->data, evt->data_len);
  393. break;
  394. case HTTP_EVENT_ON_FINISH:
  395. ESP_LOGI(TAG, "HTTP_EVENT_ON_FINISH");
  396. break;
  397. case HTTP_EVENT_DISCONNECTED:
  398. ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
  399. break;
  400. }
  401. return ESP_OK;
  402. }
  403. static void eth_download_task(void *param)
  404. {
  405. EventGroupHandle_t eth_event_group = (EventGroupHandle_t)param;
  406. esp_rom_md5_init(&md5_context);
  407. esp_http_client_config_t config = {
  408. .url = "https://dl.espressif.com/dl/misc/2MB.bin",
  409. .event_handler = http_event_handle,
  410. .buffer_size = 5120
  411. };
  412. esp_http_client_handle_t client = esp_http_client_init(&config);
  413. TEST_ASSERT_NOT_NULL(client);
  414. TEST_ESP_OK(esp_http_client_perform(client));
  415. TEST_ESP_OK(esp_http_client_cleanup(client));
  416. esp_rom_md5_final(digest, &md5_context);
  417. xEventGroupSetBits(eth_event_group, ETH_DOWNLOAD_END_BIT);
  418. vTaskDelete(NULL);
  419. }
  420. TEST_CASE("esp32 ethernet download test", "[ethernet][test_env=UT_T2_Ethernet][timeout=240]")
  421. {
  422. EventBits_t bits = 0;
  423. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  424. TEST_ASSERT(eth_event_group != NULL);
  425. test_case_uses_tcpip();
  426. TEST_ESP_OK(esp_event_loop_create_default());
  427. // create TCP/IP netif
  428. esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
  429. esp_netif_t *eth_netif = esp_netif_new(&netif_cfg);
  430. // set default handlers to do layer 3 (and up) stuffs
  431. TEST_ESP_OK(esp_eth_set_default_handlers(eth_netif));
  432. // register user defined event handers
  433. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  434. TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group));
  435. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  436. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  437. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  438. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  439. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  440. esp_eth_handle_t eth_handle = NULL;
  441. // install Ethernet driver
  442. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  443. // combine driver with netif
  444. void *glue = esp_eth_new_netif_glue(eth_handle);
  445. TEST_ESP_OK(esp_netif_attach(eth_netif, glue));
  446. // start Ethernet driver
  447. TEST_ESP_OK(esp_eth_start(eth_handle));
  448. /* wait for IP lease */
  449. bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS));
  450. TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT);
  451. xTaskCreate(eth_download_task, "eth_dl", 4096, eth_event_group, tskIDLE_PRIORITY + 2, NULL);
  452. /* wait for download end */
  453. bits = xEventGroupWaitBits(eth_event_group, ETH_DOWNLOAD_END_BIT, true, true, pdMS_TO_TICKS(ETH_DOWNLOAD_END_TIMEOUT_MS));
  454. TEST_ASSERT_EQUAL(ETH_DOWNLOAD_END_BIT, bits & ETH_DOWNLOAD_END_BIT);
  455. // check MD5 digest
  456. // MD5: df61db8564d145bbe67112aa8ecdccd8
  457. uint8_t expect_digest[16] = {223, 97, 219, 133, 100, 209, 69, 187, 230, 113, 18, 170, 142, 205, 204, 216};
  458. printf("MD5 Digest: ");
  459. for (int i = 0; i < 16; i++) {
  460. printf("%d ", digest[i]);
  461. }
  462. printf("\r\n");
  463. TEST_ASSERT(memcmp(expect_digest, digest, sizeof(digest)) == 0);
  464. // stop Ethernet driver
  465. TEST_ESP_OK(esp_eth_stop(eth_handle));
  466. /* wait for connection stop */
  467. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS));
  468. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  469. TEST_ESP_OK(esp_eth_del_netif_glue(glue));
  470. /* driver should be uninstalled within 2 seconds */
  471. TEST_ESP_OK(test_uninstall_driver(eth_handle, 2000));
  472. TEST_ESP_OK(phy->del(phy));
  473. TEST_ESP_OK(mac->del(mac));
  474. TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler));
  475. TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler));
  476. TEST_ESP_OK(esp_eth_clear_default_handlers(eth_netif));
  477. esp_netif_destroy(eth_netif);
  478. TEST_ESP_OK(esp_event_loop_delete_default());
  479. vEventGroupDelete(eth_event_group);
  480. }
  481. #endif // SOC_EMAC_SUPPORTED