test_emac.c 22 KB

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