test_emac.c 22 KB

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