test_emac.c 22 KB

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