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 
15 #include <string.h>
16 #include "unity.h"
17 #include "test_utils.h"
18 #include "freertos/FreeRTOS.h"
19 #include "freertos/task.h"
20 #include "esp_netif.h"
21 #include "lwip/sockets.h"
22 #include "sdkconfig.h"
23 #if !CONFIG_FREERTOS_UNICORE
24 #include "esp_ipc.h"
25 #include "esp_freertos_hooks.h"
26 #endif
27 
get_test_data_partition(void)28 const esp_partition_t *get_test_data_partition(void)
29 {
30     /* This finds "flash_test" partition defined in partition_table_unit_test_app.csv */
31     const esp_partition_t *result = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
32             ESP_PARTITION_SUBTYPE_ANY, "flash_test");
33     TEST_ASSERT_NOT_NULL(result); /* means partition table set wrong */
34     return result;
35 }
36 
test_case_uses_tcpip(void)37 void test_case_uses_tcpip(void)
38 {
39     // Can be called more than once, does nothing on subsequent calls
40     esp_netif_init();
41 
42     // Allocate all sockets then free them
43     // (First time each socket is allocated some one-time allocations happen.)
44     int sockets[CONFIG_LWIP_MAX_SOCKETS];
45     for (int i = 0; i < CONFIG_LWIP_MAX_SOCKETS; i++) {
46         int type = (i % 2 == 0) ? SOCK_DGRAM : SOCK_STREAM;
47         int family = (i % 3 == 0) ? PF_INET6 : PF_INET;
48         sockets[i] = socket(family, type, IPPROTO_IP);
49     }
50     for (int i = 0; i < CONFIG_LWIP_MAX_SOCKETS; i++) {
51         close(sockets[i]);
52     }
53 
54     // Allow LWIP tasks to finish initialising themselves
55     vTaskDelay(25 / portTICK_RATE_MS);
56 
57     printf("Note: esp_netif_init() has been called. Until next reset, TCP/IP task will periodicially allocate memory and consume CPU time.\n");
58 
59     // Reset the leak checker as LWIP allocates a lot of memory on first run
60     unity_reset_leak_checks();
61     test_utils_set_leak_level(0, TYPE_LEAK_CRITICAL, COMP_LEAK_GENERAL);
62     test_utils_set_leak_level(CONFIG_UNITY_CRITICAL_LEAK_LEVEL_LWIP, TYPE_LEAK_CRITICAL, COMP_LEAK_LWIP);
63 }
64 
65 // wait user to send "Enter" key or input parameter
wait_user_control(char * parameter_buf,uint8_t buf_len)66 static void wait_user_control(char* parameter_buf, uint8_t buf_len)
67 {
68     char *buffer = parameter_buf;
69     char sign[5];
70     uint8_t buffer_len = buf_len - 1;
71 
72     if (parameter_buf == NULL) {
73         buffer = sign;
74         buffer_len = sizeof(sign) - 1;
75     }
76     // workaround that unity_gets (esp_rom_uart_rx_string) will not set '\0' correctly
77     bzero(buffer, buffer_len);
78 
79     unity_gets(buffer, buffer_len);
80 }
81 
82 // signal functions, used for sync between unity DUTs for multiple devices cases
unity_wait_for_signal_param(const char * signal_name,char * parameter_buf,uint8_t buf_len)83 void unity_wait_for_signal_param(const char* signal_name, char* parameter_buf, uint8_t buf_len)
84 {
85     printf("Waiting for signal: [%s]!\n", signal_name);
86     if (parameter_buf == NULL) {
87         printf("Please press \"Enter\" key once any board send this signal.\n");
88     } else {
89         printf("Please input parameter value from any board send this signal and press \"Enter\" key.\n");
90     }
91     wait_user_control(parameter_buf, buf_len);
92 }
93 
unity_send_signal_param(const char * signal_name,const char * parameter)94 void unity_send_signal_param(const char* signal_name, const char *parameter)
95 {
96     if (parameter == NULL) {
97         printf("Send signal: [%s]!\n", signal_name);
98     } else {
99         printf("Send signal: [%s][%s]!\n", signal_name, parameter);
100     }
101 }
102 
unity_util_convert_mac_from_string(const char * mac_str,uint8_t * mac_addr)103 bool unity_util_convert_mac_from_string(const char* mac_str, uint8_t *mac_addr)
104 {
105     uint8_t loop = 0;
106     uint8_t tmp = 0;
107     const char *start;
108     char *stop;
109 
110     for (loop = 0; loop < 6; loop++) {
111         start = mac_str + loop * 3;
112         tmp = strtol(start, &stop, 16);
113         if (stop - start == 2 && (*stop == ':' || (*stop == 0 && loop == 5))) {
114             mac_addr[loop] = tmp;
115         } else {
116             return false;
117         }
118     }
119     return true;
120 }
121 
122 static size_t test_unity_leak_level[TYPE_LEAK_MAX][COMP_LEAK_ALL] = { 0 };
123 
test_utils_set_leak_level(size_t leak_level,esp_type_leak_t type_of_leak,esp_comp_leak_t component)124 esp_err_t test_utils_set_leak_level(size_t leak_level, esp_type_leak_t type_of_leak, esp_comp_leak_t component)
125 {
126     if (type_of_leak >= TYPE_LEAK_MAX || component >= COMP_LEAK_ALL) {
127         return ESP_ERR_INVALID_ARG;
128     }
129     test_unity_leak_level[type_of_leak][component] = leak_level;
130     return ESP_OK;
131 }
132 
test_utils_get_leak_level(esp_type_leak_t type_of_leak,esp_comp_leak_t component)133 size_t test_utils_get_leak_level(esp_type_leak_t type_of_leak, esp_comp_leak_t component)
134 {
135     size_t leak_level = 0;
136     if (type_of_leak >= TYPE_LEAK_MAX || component > COMP_LEAK_ALL) {
137         leak_level = 0;
138     } else {
139         if (component == COMP_LEAK_ALL) {
140             for (int comp = 0; comp < COMP_LEAK_ALL; ++comp) {
141                 leak_level += test_unity_leak_level[type_of_leak][comp];
142             }
143         } else {
144             leak_level = test_unity_leak_level[type_of_leak][component];
145         }
146     }
147     return leak_level;
148 }
149 
150 #define EXHAUST_MEMORY_ENTRIES 100
151 
152 struct test_utils_exhaust_memory_record_s {
153     int *entries[EXHAUST_MEMORY_ENTRIES];
154 };
155 
test_utils_exhaust_memory(uint32_t caps,size_t limit)156 test_utils_exhaust_memory_rec test_utils_exhaust_memory(uint32_t caps, size_t limit)
157 {
158     int idx = 0;
159     test_utils_exhaust_memory_rec rec = calloc(1, sizeof(struct test_utils_exhaust_memory_record_s));
160     TEST_ASSERT_NOT_NULL_MESSAGE(rec, "test_utils_exhaust_memory: not enough free memory to allocate record structure!");
161 
162     while (idx < EXHAUST_MEMORY_ENTRIES) {
163         size_t free_caps = heap_caps_get_largest_free_block(caps);
164         if (free_caps <= limit) {
165             return rec; // done!
166         }
167         rec->entries[idx] = heap_caps_malloc(free_caps - limit, caps);
168         TEST_ASSERT_NOT_NULL_MESSAGE(rec->entries[idx],
169                                      "test_utils_exhaust_memory: something went wrong while freeing up memory, is another task using heap?");
170         heap_caps_check_integrity_all(true);
171         idx++;
172     }
173 
174     TEST_FAIL_MESSAGE("test_utils_exhaust_memory: The heap with the requested caps is too fragmented, increase EXHAUST_MEMORY_ENTRIES or defrag the heap!");
175     abort();
176 }
177 
test_utils_free_exhausted_memory(test_utils_exhaust_memory_rec rec)178 void test_utils_free_exhausted_memory(test_utils_exhaust_memory_rec rec)
179 {
180     for (int i = 0; i < EXHAUST_MEMORY_ENTRIES; i++) {
181         free(rec->entries[i]);
182     }
183     free(rec);
184 }
185 
186 #if !CONFIG_FREERTOS_UNICORE
187 static SemaphoreHandle_t test_sem;
188 
test_idle_hook_func(void)189 static bool test_idle_hook_func(void)
190 {
191     if (test_sem) {
192         xSemaphoreGive(test_sem);
193     }
194     return true;
195 }
196 
test_task_delete_func(void * arg)197 static void test_task_delete_func(void *arg)
198 {
199     vTaskDelete(arg);
200 }
201 #endif // !CONFIG_FREERTOS_UNICORE
202 
test_utils_task_delete(TaskHandle_t thandle)203 void test_utils_task_delete(TaskHandle_t thandle)
204 {
205     /* Self deletion can not free up associated task dynamic memory immediately,
206      * hence not recommended for test scenarios */
207     TEST_ASSERT_NOT_NULL_MESSAGE(thandle, "test_utils_task_delete: handle is NULL");
208     TEST_ASSERT_NOT_EQUAL_MESSAGE(thandle, xTaskGetCurrentTaskHandle(), "test_utils_task_delete: handle is of currently executing task");
209 
210 #if CONFIG_FREERTOS_UNICORE
211     vTaskDelete(thandle);
212 #else // CONFIG_FREERTOS_UNICORE
213     const BaseType_t tsk_affinity = xTaskGetAffinity(thandle);
214     const BaseType_t core_id = xPortGetCoreID();
215 
216     printf("Task_affinity: 0x%x, current_core: %d\n", tsk_affinity, core_id);
217 
218     if (tsk_affinity == tskNO_AFFINITY) {
219         /* For no affinity case, we wait for idle hook to trigger on different core */
220         esp_err_t ret = esp_register_freertos_idle_hook_for_cpu(test_idle_hook_func, !core_id);
221         TEST_ASSERT_EQUAL_MESSAGE(ret, ESP_OK, "test_utils_task_delete: failed to register idle hook");
222         vTaskDelete(thandle);
223         test_sem = xSemaphoreCreateBinary();
224         TEST_ASSERT_NOT_NULL_MESSAGE(test_sem, "test_utils_task_delete: failed to create semaphore");
225         xSemaphoreTake(test_sem, portMAX_DELAY);
226         esp_deregister_freertos_idle_hook_for_cpu(test_idle_hook_func, !core_id);
227         vSemaphoreDelete(test_sem);
228         test_sem = NULL;
229     } else if (tsk_affinity != core_id) {
230         /* Task affinity and current core are differnt, schedule IPC call (to delete task)
231          * on core where task is pinned to */
232         esp_ipc_call_blocking(tsk_affinity, test_task_delete_func, thandle);
233     } else {
234         /* Task affinity and current core are same, so we can safely proceed for deletion */
235         vTaskDelete(thandle);
236     }
237 #endif // !CONFIG_FREERTOS_UNICORE
238 }
239