test_emac.c 24 KB

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