test_emac.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/event_groups.h"
  11. #include "unity.h"
  12. #include "test_utils.h"
  13. #include "esp_event.h"
  14. #include "esp_netif.h"
  15. #include "esp_eth.h"
  16. #include "esp_log.h"
  17. #include "esp_http_client.h"
  18. #include "esp_rom_md5.h"
  19. #include "soc/soc_caps.h"
  20. #if SOC_EMAC_SUPPORTED
  21. static const char *TAG = "esp32_eth_test";
  22. #define ETH_START_BIT BIT(0)
  23. #define ETH_STOP_BIT BIT(1)
  24. #define ETH_CONNECT_BIT BIT(2)
  25. #define ETH_GOT_IP_BIT BIT(3)
  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. extern const char dl_espressif_com_root_cert_pem_start[] asm("_binary_dl_espressif_com_root_cert_pem_start");
  32. extern const char dl_espressif_com_root_cert_pem_end[] asm("_binary_dl_espressif_com_root_cert_pem_end");
  33. // compute md5 of download file
  34. static md5_context_t 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 esp_err_t test_uninstall_driver(esp_eth_handle_t eth_hdl, uint32_t ms_to_wait)
  77. {
  78. int i = 0;
  79. ms_to_wait += 100;
  80. for (i = 0; i < ms_to_wait / 100; i++) {
  81. vTaskDelay(pdMS_TO_TICKS(100));
  82. if (esp_eth_driver_uninstall(eth_hdl) == ESP_OK) {
  83. break;
  84. }
  85. }
  86. if (i < ms_to_wait / 100) {
  87. return ESP_OK;
  88. } else {
  89. return ESP_FAIL;
  90. }
  91. }
  92. TEST_CASE("esp32 ethernet io test", "[ethernet][test_env=UT_T2_Ethernet]")
  93. {
  94. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  95. mac_config.flags = ETH_MAC_FLAG_PIN_TO_CORE; // pin to core
  96. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  97. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
  98. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  99. // auto detect PHY address
  100. phy_config.phy_addr = ESP_ETH_PHY_ADDR_AUTO;
  101. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  102. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  103. esp_eth_handle_t eth_handle = NULL;
  104. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  105. /* get MAC address */
  106. uint8_t mac_addr[6];
  107. memset(mac_addr, 0, sizeof(mac_addr));
  108. TEST_ESP_OK(esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr));
  109. ESP_LOGI(TAG, "Ethernet MAC Address: %02x:%02x:%02x:%02x:%02x:%02x",
  110. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  111. TEST_ASSERT(mac_addr[0] != 0);
  112. /* get PHY address */
  113. int phy_addr = -1;
  114. TEST_ESP_OK(esp_eth_ioctl(eth_handle, ETH_CMD_G_PHY_ADDR, &phy_addr));
  115. ESP_LOGI(TAG, "Ethernet PHY Address: %d", phy_addr);
  116. TEST_ASSERT(phy_addr >= 0 && phy_addr <= 31);
  117. TEST_ESP_OK(esp_eth_driver_uninstall(eth_handle));
  118. TEST_ESP_OK(phy->del(phy));
  119. TEST_ESP_OK(mac->del(mac));
  120. }
  121. TEST_CASE("esp32 ethernet speed/duplex/autonegotiation", "[ethernet][test_env=UT_T2_Ethernet]")
  122. {
  123. EventBits_t bits = 0;
  124. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  125. TEST_ASSERT(eth_event_group != NULL);
  126. TEST_ESP_OK(esp_event_loop_create_default());
  127. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  128. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  129. mac_config.flags = ETH_MAC_FLAG_PIN_TO_CORE; // pin to core
  130. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  131. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &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. // Set PHY to loopback mode so we do not have to take care about link configuration of the other node.
  140. // The reason behind is improbable, however, if the other node was configured to e.g. 100 Mbps and we
  141. // tried to change the speed at ESP node to 10 Mbps, we could get into trouble to establish a link.
  142. bool loopback_en = true;
  143. esp_eth_ioctl(eth_handle, ETH_CMD_S_PHY_LOOPBACK, &loopback_en);
  144. // this test only test layer2, so don't need to register input callback (i.e. esp_eth_update_input_path)
  145. TEST_ESP_OK(esp_eth_start(eth_handle));
  146. // wait for connection start
  147. bits = xEventGroupWaitBits(eth_event_group, ETH_START_BIT, true, true, pdMS_TO_TICKS(ETH_START_TIMEOUT_MS));
  148. TEST_ASSERT((bits & ETH_START_BIT) == ETH_START_BIT);
  149. // wait for connection establish
  150. bits = xEventGroupWaitBits(eth_event_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(ETH_CONNECT_TIMEOUT_MS));
  151. TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
  152. eth_duplex_t exp_duplex;
  153. esp_eth_ioctl(eth_handle, ETH_CMD_G_DUPLEX_MODE, &exp_duplex);
  154. eth_speed_t exp_speed;
  155. esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &exp_speed);
  156. // verify autonegotiation result (expecting the best link configuration)
  157. TEST_ASSERT_EQUAL(ETH_DUPLEX_FULL, exp_duplex);
  158. TEST_ASSERT_EQUAL(ETH_SPEED_100M, exp_speed);
  159. bool exp_autoneg_en;
  160. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_AUTONEGO, &exp_autoneg_en));
  161. TEST_ASSERT_EQUAL(true, exp_autoneg_en);
  162. ESP_LOGI(TAG, "try to change autonegotiation when driver is started...");
  163. bool auto_nego_en = false;
  164. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, esp_eth_ioctl(eth_handle, ETH_CMD_S_AUTONEGO, &auto_nego_en));
  165. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_AUTONEGO, &exp_autoneg_en));
  166. TEST_ASSERT_EQUAL(true, exp_autoneg_en);
  167. ESP_LOGI(TAG, "stop the Ethernet driver and...");
  168. esp_eth_stop(eth_handle);
  169. ESP_LOGI(TAG, "try to change speed/duplex prior disabling the autonegotiation...");
  170. eth_duplex_t duplex = ETH_DUPLEX_HALF;
  171. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, esp_eth_ioctl(eth_handle, ETH_CMD_S_DUPLEX_MODE, &duplex));
  172. eth_speed_t speed = ETH_SPEED_10M;
  173. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, esp_eth_ioctl(eth_handle, ETH_CMD_S_SPEED, &speed));
  174. // Disable autonegotiation and change speed to 10 Mbps and duplex to half
  175. ESP_LOGI(TAG, "disable the autonegotiation and change the speed/duplex...");
  176. auto_nego_en = false;
  177. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_S_AUTONEGO, &auto_nego_en));
  178. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_AUTONEGO, &exp_autoneg_en));
  179. TEST_ASSERT_EQUAL(false, exp_autoneg_en);
  180. // set new duplex mode
  181. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_S_DUPLEX_MODE, &duplex));
  182. // set new speed
  183. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_S_SPEED, &speed));
  184. // start the driver and wait for connection establish
  185. esp_eth_start(eth_handle);
  186. bits = xEventGroupWaitBits(eth_event_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(ETH_CONNECT_TIMEOUT_MS));
  187. TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
  188. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_DUPLEX_MODE, &exp_duplex));
  189. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &exp_speed));
  190. TEST_ASSERT_EQUAL(ETH_DUPLEX_HALF, exp_duplex);
  191. TEST_ASSERT_EQUAL(ETH_SPEED_10M, exp_speed);
  192. // Change speed back to 100 Mbps
  193. esp_eth_stop(eth_handle);
  194. ESP_LOGI(TAG, "change speed again...");
  195. speed = ETH_SPEED_100M;
  196. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_S_SPEED, &speed));
  197. // start the driver and wait for connection establish
  198. esp_eth_start(eth_handle);
  199. bits = xEventGroupWaitBits(eth_event_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(ETH_CONNECT_TIMEOUT_MS));
  200. TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
  201. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &exp_speed));
  202. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_DUPLEX_MODE, &exp_duplex));
  203. TEST_ASSERT_EQUAL(ETH_DUPLEX_HALF, exp_duplex);
  204. TEST_ASSERT_EQUAL(ETH_SPEED_100M, exp_speed);
  205. // Change duplex back to full
  206. esp_eth_stop(eth_handle);
  207. ESP_LOGI(TAG, "change duplex again...");
  208. duplex = ETH_DUPLEX_FULL;
  209. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_S_DUPLEX_MODE, &duplex));
  210. // start the driver and wait for connection establish
  211. esp_eth_start(eth_handle);
  212. bits = xEventGroupWaitBits(eth_event_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(ETH_CONNECT_TIMEOUT_MS));
  213. TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
  214. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_DUPLEX_MODE, &exp_duplex));
  215. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &exp_speed));
  216. TEST_ASSERT_EQUAL(ETH_DUPLEX_FULL, exp_duplex);
  217. TEST_ASSERT_EQUAL(ETH_SPEED_100M, exp_speed);
  218. ESP_LOGI(TAG, "try to change speed/duplex when driver is started and autonegotiation disabled...");
  219. speed = ETH_SPEED_10M;
  220. duplex = ETH_DUPLEX_HALF;
  221. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, esp_eth_ioctl(eth_handle, ETH_CMD_S_DUPLEX_MODE, &duplex));
  222. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, esp_eth_ioctl(eth_handle, ETH_CMD_S_SPEED, &speed));
  223. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_DUPLEX_MODE, &exp_duplex));
  224. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &exp_speed));
  225. TEST_ASSERT_EQUAL(ETH_DUPLEX_FULL, exp_duplex);
  226. TEST_ASSERT_EQUAL(ETH_SPEED_100M, exp_speed);
  227. ESP_LOGI(TAG, "change the speed/duplex to 10 Mbps/half and then enable autonegotiation...");
  228. esp_eth_stop(eth_handle);
  229. speed = ETH_SPEED_10M;
  230. duplex = ETH_DUPLEX_HALF;
  231. // set new duplex mode
  232. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_S_DUPLEX_MODE, &duplex));
  233. // set new speed
  234. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_S_SPEED, &speed));
  235. // start the driver and wait for connection establish
  236. esp_eth_start(eth_handle);
  237. bits = xEventGroupWaitBits(eth_event_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(ETH_CONNECT_TIMEOUT_MS));
  238. TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
  239. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_DUPLEX_MODE, &exp_duplex));
  240. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &exp_speed));
  241. TEST_ASSERT_EQUAL(ETH_DUPLEX_HALF, exp_duplex);
  242. TEST_ASSERT_EQUAL(ETH_SPEED_10M, exp_speed);
  243. esp_eth_stop(eth_handle);
  244. auto_nego_en = true;
  245. esp_eth_ioctl(eth_handle, ETH_CMD_S_AUTONEGO, &auto_nego_en);
  246. esp_eth_start(eth_handle);
  247. bits = xEventGroupWaitBits(eth_event_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(ETH_CONNECT_TIMEOUT_MS));
  248. TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
  249. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_AUTONEGO, &exp_autoneg_en));
  250. TEST_ASSERT_EQUAL(true, exp_autoneg_en);
  251. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_DUPLEX_MODE, &exp_duplex));
  252. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &exp_speed));
  253. // verify autonegotiation result (expecting the best link configuration)
  254. TEST_ASSERT_EQUAL(ETH_DUPLEX_FULL, exp_duplex);
  255. TEST_ASSERT_EQUAL(ETH_SPEED_100M, exp_speed);
  256. // stop Ethernet driver
  257. TEST_ESP_OK(esp_eth_stop(eth_handle));
  258. /* wait for connection stop */
  259. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS));
  260. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  261. TEST_ESP_OK(esp_eth_driver_uninstall(eth_handle));
  262. TEST_ESP_OK(phy->del(phy));
  263. TEST_ESP_OK(mac->del(mac));
  264. TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler));
  265. TEST_ESP_OK(esp_event_loop_delete_default());
  266. vEventGroupDelete(eth_event_group);
  267. }
  268. TEST_CASE("esp32 ethernet event test", "[ethernet][test_env=UT_T2_Ethernet]")
  269. {
  270. EventBits_t bits = 0;
  271. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  272. TEST_ASSERT(eth_event_group != NULL);
  273. TEST_ESP_OK(esp_event_loop_create_default());
  274. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  275. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  276. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  277. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
  278. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  279. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  280. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  281. esp_eth_handle_t eth_handle = NULL;
  282. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  283. // this test only test layer2 event, so don't need to register input callback (i.e. esp_eth_update_input_path)
  284. TEST_ESP_OK(esp_eth_start(eth_handle));
  285. /* wait for connection start */
  286. bits = xEventGroupWaitBits(eth_event_group, ETH_START_BIT, true, true, pdMS_TO_TICKS(ETH_START_TIMEOUT_MS));
  287. TEST_ASSERT((bits & ETH_START_BIT) == ETH_START_BIT);
  288. /* wait for connection establish */
  289. bits = xEventGroupWaitBits(eth_event_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(ETH_CONNECT_TIMEOUT_MS));
  290. TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
  291. // stop Ethernet driver
  292. TEST_ESP_OK(esp_eth_stop(eth_handle));
  293. /* wait for connection stop */
  294. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS));
  295. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  296. /* driver should be uninstalled within 2 seconds */
  297. TEST_ESP_OK(test_uninstall_driver(eth_handle, 2000));
  298. TEST_ESP_OK(phy->del(phy));
  299. TEST_ESP_OK(mac->del(mac));
  300. TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler));
  301. TEST_ESP_OK(esp_event_loop_delete_default());
  302. vEventGroupDelete(eth_event_group);
  303. }
  304. TEST_CASE("esp32 ethernet dhcp test", "[ethernet][test_env=UT_T2_Ethernet]")
  305. {
  306. EventBits_t bits = 0;
  307. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  308. TEST_ASSERT(eth_event_group != NULL);
  309. test_case_uses_tcpip();
  310. TEST_ESP_OK(esp_event_loop_create_default());
  311. // create TCP/IP netif
  312. esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
  313. esp_netif_t *eth_netif = esp_netif_new(&netif_cfg);
  314. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  315. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  316. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
  317. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  318. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  319. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  320. esp_eth_handle_t eth_handle = NULL;
  321. // install Ethernet driver
  322. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  323. // combine driver with netif
  324. esp_eth_netif_glue_handle_t glue = esp_eth_new_netif_glue(eth_handle);
  325. TEST_ESP_OK(esp_netif_attach(eth_netif, glue));
  326. // register user defined event handers
  327. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  328. TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group));
  329. // start Ethernet driver
  330. TEST_ESP_OK(esp_eth_start(eth_handle));
  331. /* wait for IP lease */
  332. bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS));
  333. TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT);
  334. // stop Ethernet driver
  335. TEST_ESP_OK(esp_eth_stop(eth_handle));
  336. /* wait for connection stop */
  337. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS));
  338. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  339. TEST_ESP_OK(esp_eth_del_netif_glue(glue));
  340. /* driver should be uninstalled within 2 seconds */
  341. TEST_ESP_OK(test_uninstall_driver(eth_handle, 2000));
  342. TEST_ESP_OK(phy->del(phy));
  343. TEST_ESP_OK(mac->del(mac));
  344. TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler));
  345. TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler));
  346. esp_netif_destroy(eth_netif);
  347. TEST_ESP_OK(esp_event_loop_delete_default());
  348. vEventGroupDelete(eth_event_group);
  349. }
  350. TEST_CASE("esp32 ethernet start/stop stress test", "[ethernet][test_env=UT_T2_Ethernet][timeout=240]")
  351. {
  352. EventBits_t bits = 0;
  353. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  354. TEST_ASSERT(eth_event_group != NULL);
  355. test_case_uses_tcpip();
  356. TEST_ESP_OK(esp_event_loop_create_default());
  357. // create TCP/IP netif
  358. esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
  359. esp_netif_t *eth_netif = esp_netif_new(&netif_cfg);
  360. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  361. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  362. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
  363. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  364. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  365. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  366. esp_eth_handle_t eth_handle = NULL;
  367. // install Ethernet driver
  368. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  369. // combine driver with netif
  370. esp_eth_netif_glue_handle_t glue = esp_eth_new_netif_glue(eth_handle);
  371. TEST_ESP_OK(esp_netif_attach(eth_netif, glue));
  372. // register user defined event handers
  373. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  374. TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group));
  375. for (int i = 0; i < 10; i++) {
  376. // start Ethernet driver
  377. TEST_ESP_OK(esp_eth_start(eth_handle));
  378. /* wait for IP lease */
  379. bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS));
  380. TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT);
  381. // stop Ethernet driver
  382. TEST_ESP_OK(esp_eth_stop(eth_handle));
  383. /* wait for connection stop */
  384. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS));
  385. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  386. }
  387. TEST_ESP_OK(esp_eth_del_netif_glue(glue));
  388. /* driver should be uninstalled within 2 seconds */
  389. TEST_ESP_OK(test_uninstall_driver(eth_handle, 2000));
  390. TEST_ESP_OK(phy->del(phy));
  391. TEST_ESP_OK(mac->del(mac));
  392. TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler));
  393. TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler));
  394. esp_netif_destroy(eth_netif);
  395. TEST_ESP_OK(esp_event_loop_delete_default());
  396. vEventGroupDelete(eth_event_group);
  397. }
  398. esp_err_t http_event_handle(esp_http_client_event_t *evt)
  399. {
  400. switch (evt->event_id) {
  401. case HTTP_EVENT_ERROR:
  402. ESP_LOGE(TAG, "HTTP_EVENT_ERROR");
  403. break;
  404. case HTTP_EVENT_ON_CONNECTED:
  405. ESP_LOGI(TAG, "HTTP_EVENT_ON_CONNECTED");
  406. break;
  407. case HTTP_EVENT_HEADER_SENT:
  408. ESP_LOGI(TAG, "HTTP_EVENT_HEADER_SENT");
  409. break;
  410. case HTTP_EVENT_ON_HEADER:
  411. ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER");
  412. break;
  413. case HTTP_EVENT_ON_DATA:
  414. esp_rom_md5_update(&md5_context, evt->data, evt->data_len);
  415. break;
  416. case HTTP_EVENT_ON_FINISH:
  417. ESP_LOGI(TAG, "HTTP_EVENT_ON_FINISH");
  418. break;
  419. case HTTP_EVENT_DISCONNECTED:
  420. ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
  421. break;
  422. case HTTP_EVENT_REDIRECT:
  423. ESP_LOGI(TAG, "HTTP_EVENT_REDIRECT");
  424. break;
  425. }
  426. return ESP_OK;
  427. }
  428. static void eth_start_download(void)
  429. {
  430. esp_rom_md5_init(&md5_context);
  431. esp_http_client_config_t config = {
  432. .url = "https://dl.espressif.com/dl/misc/2MB.bin",
  433. .cert_pem = dl_espressif_com_root_cert_pem_start,
  434. .event_handler = http_event_handle,
  435. .buffer_size = 5120
  436. };
  437. esp_http_client_handle_t client = esp_http_client_init(&config);
  438. TEST_ASSERT_NOT_NULL(client);
  439. TEST_ESP_OK(esp_http_client_perform(client));
  440. TEST_ESP_OK(esp_http_client_cleanup(client));
  441. esp_rom_md5_final(digest, &md5_context);
  442. }
  443. TEST_CASE("esp32 ethernet download test", "[ethernet][test_env=UT_T2_Ethernet][timeout=240]")
  444. {
  445. EventBits_t bits = 0;
  446. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  447. TEST_ASSERT(eth_event_group != NULL);
  448. test_case_uses_tcpip();
  449. TEST_ESP_OK(esp_event_loop_create_default());
  450. // create TCP/IP netif
  451. esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
  452. esp_netif_t *eth_netif = esp_netif_new(&netif_cfg);
  453. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  454. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  455. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
  456. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  457. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  458. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  459. esp_eth_handle_t eth_handle = NULL;
  460. // install Ethernet driver
  461. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  462. // combine driver with netif
  463. esp_eth_netif_glue_handle_t glue = esp_eth_new_netif_glue(eth_handle);
  464. TEST_ESP_OK(esp_netif_attach(eth_netif, glue));
  465. // register user defined event handers
  466. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  467. TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group));
  468. // start Ethernet driver
  469. TEST_ESP_OK(esp_eth_start(eth_handle));
  470. /* wait for IP lease */
  471. bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS));
  472. TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT);
  473. eth_start_download();
  474. // check MD5 digest
  475. // MD5: df61db8564d145bbe67112aa8ecdccd8
  476. uint8_t expect_digest[16] = {223, 97, 219, 133, 100, 209, 69, 187, 230, 113, 18, 170, 142, 205, 204, 216};
  477. printf("MD5 Digest: ");
  478. for (int i = 0; i < 16; i++) {
  479. printf("%d ", digest[i]);
  480. }
  481. printf("\r\n");
  482. TEST_ASSERT(memcmp(expect_digest, digest, sizeof(digest)) == 0);
  483. // stop Ethernet driver
  484. TEST_ESP_OK(esp_eth_stop(eth_handle));
  485. /* wait for connection stop */
  486. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS));
  487. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  488. TEST_ESP_OK(esp_eth_del_netif_glue(glue));
  489. /* driver should be uninstalled within 2 seconds */
  490. TEST_ESP_OK(test_uninstall_driver(eth_handle, 2000));
  491. TEST_ESP_OK(phy->del(phy));
  492. TEST_ESP_OK(mac->del(mac));
  493. TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler));
  494. TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler));
  495. esp_netif_destroy(eth_netif);
  496. TEST_ESP_OK(esp_event_loop_delete_default());
  497. vEventGroupDelete(eth_event_group);
  498. }
  499. #endif // SOC_EMAC_SUPPORTED