test_utils.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include "unity.h"
  16. #include "test_utils.h"
  17. #include "rom/ets_sys.h"
  18. #include "rom/uart.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "tcpip_adapter.h"
  22. #include "lwip/sockets.h"
  23. const esp_partition_t *get_test_data_partition()
  24. {
  25. /* This finds "flash_test" partition defined in partition_table_unit_test_app.csv */
  26. const esp_partition_t *result = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
  27. ESP_PARTITION_SUBTYPE_ANY, "flash_test");
  28. TEST_ASSERT_NOT_NULL(result); /* means partition table set wrong */
  29. return result;
  30. }
  31. void test_case_uses_tcpip()
  32. {
  33. // Can be called more than once, does nothing on subsequent calls
  34. tcpip_adapter_init();
  35. // Allocate all sockets then free them
  36. // (First time each socket is allocated some one-time allocations happen.)
  37. int sockets[CONFIG_LWIP_MAX_SOCKETS];
  38. for (int i = 0; i < CONFIG_LWIP_MAX_SOCKETS; i++) {
  39. int type = (i % 2 == 0) ? SOCK_DGRAM : SOCK_STREAM;
  40. int family = (i % 3 == 0) ? PF_INET6 : PF_INET;
  41. sockets[i] = socket(family, type, IPPROTO_IP);
  42. }
  43. for (int i = 0; i < CONFIG_LWIP_MAX_SOCKETS; i++) {
  44. close(sockets[i]);
  45. }
  46. // Allow LWIP tasks to finish initialising themselves
  47. vTaskDelay(25 / portTICK_RATE_MS);
  48. printf("Note: tcpip_adapter_init() has been called. Until next reset, TCP/IP task will periodicially allocate memory and consume CPU time.\n");
  49. // Reset the leak checker as LWIP allocates a lot of memory on first run
  50. unity_reset_leak_checks();
  51. }
  52. // wait user to send "Enter" key or input parameter
  53. static void wait_user_control(char* parameter_buf, uint8_t buf_len)
  54. {
  55. char *buffer = parameter_buf;
  56. char sign[5];
  57. uint8_t buffer_len = buf_len - 1;
  58. if (parameter_buf == NULL) {
  59. buffer = sign;
  60. buffer_len = sizeof(sign) - 1;
  61. }
  62. // workaround that unity_gets (UartRxString) will not set '\0' correctly
  63. bzero(buffer, buffer_len);
  64. unity_gets(buffer, buffer_len);
  65. }
  66. // signal functions, used for sync between unity DUTs for multiple devices cases
  67. void unity_wait_for_signal_param(const char* signal_name, char* parameter_buf, uint8_t buf_len)
  68. {
  69. printf("Waiting for signal: [%s]!\n", signal_name);
  70. if (parameter_buf == NULL) {
  71. printf("Please press \"Enter\" key once any board send this signal.\n");
  72. } else {
  73. printf("Please input parameter value from any board send this signal and press \"Enter\" key.\n");
  74. }
  75. wait_user_control(parameter_buf, buf_len);
  76. }
  77. void unity_send_signal_param(const char* signal_name, const char *parameter)
  78. {
  79. if (parameter == NULL) {
  80. printf("Send signal: [%s]!\n", signal_name);
  81. } else {
  82. printf("Send signal: [%s][%s]!\n", signal_name, parameter);
  83. }
  84. }
  85. bool unity_util_convert_mac_from_string(const char* mac_str, uint8_t *mac_addr)
  86. {
  87. uint8_t loop = 0;
  88. uint8_t tmp = 0;
  89. const char *start;
  90. char *stop;
  91. for (loop = 0; loop < 6; loop++) {
  92. start = mac_str + loop * 3;
  93. tmp = strtol(start, &stop, 16);
  94. if (stop - start == 2 && (*stop == ':' || (*stop == 0 && loop == 5))) {
  95. mac_addr[loop] = tmp;
  96. } else {
  97. return false;
  98. }
  99. }
  100. return true;
  101. }