Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | /* * Copyright (c) 2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include "test_msgq.h" /**TESTPOINT: init via K_MSGQ_DEFINE*/ K_MSGQ_DEFINE(kmsgq, MSG_SIZE, MSGQ_LEN, 4); K_MSGQ_DEFINE(kmsgq_test_alloc, MSG_SIZE, MSGQ_LEN, 4); struct k_msgq msgq; struct k_msgq msgq1; K_THREAD_STACK_DEFINE(tstack, STACK_SIZE); K_THREAD_STACK_DEFINE(tstack1, STACK_SIZE); K_THREAD_STACK_DEFINE(tstack2, STACK_SIZE); struct k_thread tdata; struct k_thread tdata1; struct k_thread tdata2; static ZTEST_BMEM char __aligned(4) tbuffer[MSG_SIZE * MSGQ_LEN]; static ZTEST_DMEM char __aligned(4) tbuffer1[MSG_SIZE]; static ZTEST_DMEM u32_t data[MSGQ_LEN] = { MSG0, MSG1 }; struct k_sem end_sema; static void put_msgq(struct k_msgq *pmsgq) { int ret; u32_t read_data; for (int i = 0; i < MSGQ_LEN; i++) { ret = k_msgq_put(pmsgq, (void *)&data[i], K_NO_WAIT); zassert_equal(ret, 0, NULL); /**TESTPOINT: Check if k_msgq_peek reads msgq * in FIFO manner. * Everytime msg is enqueued, msg read should * always be the first message */ zassert_equal(k_msgq_peek(pmsgq, &read_data), 0, NULL); zassert_equal(read_data, data[0], NULL); /**TESTPOINT: msgq free get*/ zassert_equal(k_msgq_num_free_get(pmsgq), MSGQ_LEN - 1 - i, NULL); /**TESTPOINT: msgq used get*/ zassert_equal(k_msgq_num_used_get(pmsgq), i + 1, NULL); } } static void get_msgq(struct k_msgq *pmsgq) { u32_t rx_data, read_data; int ret; for (int i = 0; i < MSGQ_LEN; i++) { zassert_equal(k_msgq_peek(pmsgq, &read_data), 0, NULL); ret = k_msgq_get(pmsgq, &rx_data, K_FOREVER); zassert_equal(ret, 0, NULL); zassert_equal(rx_data, data[i], NULL); /**TESTPOINT: Check if msg read is the msg deleted*/ zassert_equal(read_data, rx_data, NULL); /**TESTPOINT: msgq free get*/ zassert_equal(k_msgq_num_free_get(pmsgq), i + 1, NULL); /**TESTPOINT: msgq used get*/ zassert_equal(k_msgq_num_used_get(pmsgq), MSGQ_LEN - 1 - i, NULL); } } static void purge_msgq(struct k_msgq *pmsgq) { u32_t read_data; k_msgq_purge(pmsgq); zassert_equal(k_msgq_num_free_get(pmsgq), MSGQ_LEN, NULL); zassert_equal(k_msgq_num_used_get(pmsgq), 0, NULL); zassert_equal(k_msgq_peek(pmsgq, &read_data), -ENOMSG, NULL); } static void tisr_entry(void *p) { put_msgq((struct k_msgq *)p); } static void thread_entry(void *p1, void *p2, void *p3) { get_msgq((struct k_msgq *)p1); k_sem_give(&end_sema); } static void msgq_thread(struct k_msgq *pmsgq) { /**TESTPOINT: thread-thread data passing via message queue*/ k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, pmsgq, NULL, NULL, K_PRIO_PREEMPT(0), K_USER | K_INHERIT_PERMS, K_NO_WAIT); put_msgq(pmsgq); k_sem_take(&end_sema, K_FOREVER); k_thread_abort(tid); /**TESTPOINT: msgq purge*/ purge_msgq(pmsgq); } static void thread_entry_overflow(void *p1, void *p2, void *p3) { int ret; u32_t rx_buf[MSGQ_LEN]; ret = k_msgq_get(p1, &rx_buf[0], K_FOREVER); zassert_equal(ret, 0, NULL); ret = k_msgq_get(p1, &rx_buf[1], K_FOREVER); zassert_equal(ret, 0, NULL); k_sem_give(&end_sema); } static void msgq_thread_overflow(struct k_msgq *pmsgq) { int ret; ret = k_msgq_put(pmsgq, (void *)&data[0], K_FOREVER); zassert_equal(ret, 0, NULL); /**TESTPOINT: thread-thread data passing via message queue*/ k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry_overflow, pmsgq, NULL, NULL, K_PRIO_PREEMPT(0), K_USER | K_INHERIT_PERMS, K_NO_WAIT); ret = k_msgq_put(pmsgq, (void *)&data[1], K_FOREVER); zassert_equal(ret, 0, NULL); k_sem_take(&end_sema, K_FOREVER); k_thread_abort(tid); /**TESTPOINT: msgq purge*/ k_msgq_purge(pmsgq); } static void msgq_isr(struct k_msgq *pmsgq) { /**TESTPOINT: thread-isr data passing via message queue*/ irq_offload(tisr_entry, pmsgq); get_msgq(pmsgq); /**TESTPOINT: msgq purge*/ purge_msgq(pmsgq); } static void thread_entry_get_data(void *p1, void *p2, void *p3) { u32_t rx_buf[MSGQ_LEN]; int i = 0; while (k_msgq_get(p1, &rx_buf[i], K_NO_WAIT) != 0) { ++i; } k_sem_give(&end_sema); } static void pend_thread_entry(void *p1, void *p2, void *p3) { int ret; ret = k_msgq_put(p1, &data[1], TIMEOUT); zassert_equal(ret, 0, NULL); } static void msgq_thread_data_passing(struct k_msgq *pmsgq) { while (k_msgq_put(pmsgq, &data[0], K_NO_WAIT) != 0) { } k_tid_t tid = k_thread_create(&tdata2, tstack2, STACK_SIZE, pend_thread_entry, pmsgq, NULL, NULL, K_PRIO_PREEMPT(0), 0, K_NO_WAIT); k_tid_t tid1 = k_thread_create(&tdata1, tstack1, STACK_SIZE, thread_entry_get_data, pmsgq, NULL, NULL, K_PRIO_PREEMPT(1), 0, K_NO_WAIT); k_sem_take(&end_sema, K_FOREVER); k_thread_abort(tid); k_thread_abort(tid1); /**TESTPOINT: msgq purge*/ k_msgq_purge(pmsgq); } /** * @addtogroup kernel_message_queue_tests * @{ */ /** * @brief Test thread to thread data passing via message queue * @see k_msgq_init(), k_msgq_get(), k_msgq_put(), k_msgq_purge() */ void test_msgq_thread(void) { /**TESTPOINT: init via k_msgq_init*/ k_msgq_init(&msgq, tbuffer, MSG_SIZE, MSGQ_LEN); k_sem_init(&end_sema, 0, 1); msgq_thread(&msgq); msgq_thread(&kmsgq); } /** * @brief Test thread to thread data passing via message queue * @see k_msgq_init(), k_msgq_get(), k_msgq_put(), k_msgq_purge() */ void test_msgq_thread_overflow(void) { /**TESTPOINT: init via k_msgq_init*/ k_msgq_init(&msgq, tbuffer, MSG_SIZE, 1); k_sem_init(&end_sema, 0, 1); msgq_thread_overflow(&msgq); msgq_thread_overflow(&kmsgq); } #ifdef CONFIG_USERSPACE /** * @brief Test user thread to kernel thread data passing via message queue * @see k_msgq_alloc_init(), k_msgq_get(), k_msgq_put(), k_msgq_purge() */ void test_msgq_user_thread(void) { struct k_msgq *q; q = k_object_alloc(K_OBJ_MSGQ); zassert_not_null(q, "couldn't alloc message queue"); zassert_false(k_msgq_alloc_init(q, MSG_SIZE, MSGQ_LEN), NULL); k_sem_init(&end_sema, 0, 1); msgq_thread(q); } /** * @brief Test thread to thread data passing via message queue * @see k_msgq_alloc_init(), k_msgq_get(), k_msgq_put(), k_msgq_purge() */ void test_msgq_user_thread_overflow(void) { struct k_msgq *q; q = k_object_alloc(K_OBJ_MSGQ); zassert_not_null(q, "couldn't alloc message queue"); zassert_false(k_msgq_alloc_init(q, MSG_SIZE, 1), NULL); k_sem_init(&end_sema, 0, 1); msgq_thread_overflow(q); } #endif /* CONFIG_USERSPACE */ /** * @brief Test thread to isr data passing via message queue * @see k_msgq_init(), k_msgq_get(), k_msgq_put(), k_msgq_purge() */ void test_msgq_isr(void) { struct k_msgq stack_msgq; /**TESTPOINT: init via k_msgq_init*/ k_msgq_init(&stack_msgq, tbuffer, MSG_SIZE, MSGQ_LEN); msgq_isr(&stack_msgq); msgq_isr(&kmsgq); } /** * @brief Test pending writer in msgq * @see k_msgq_init(), k_msgq_get(), k_msgq_put(), k_msgq_purge() */ void test_msgq_pend_thread(void) { k_msgq_init(&msgq1, tbuffer1, MSG_SIZE, 1); k_sem_init(&end_sema, 0, 1); msgq_thread_data_passing(&msgq1); } /** * @brief Test k_msgq_alloc_init() * @details Initialization and buffer allocation for msgq from resource * pool with various parameters * @see k_msgq_alloc_init(), k_msgq_cleanup() */ void test_msgq_alloc(void) { int ret; k_msgq_alloc_init(&kmsgq_test_alloc, MSG_SIZE, MSGQ_LEN); msgq_isr(&kmsgq_test_alloc); k_msgq_cleanup(&kmsgq_test_alloc); /** Requesting buffer allocation from the test pool.*/ ret = k_msgq_alloc_init(&kmsgq_test_alloc, MSG_SIZE * 64, MSGQ_LEN); zassert_true(ret == -ENOMEM, "resource pool is smaller then requested buffer"); /* Requesting a huge size of MSG to validate overflow*/ ret = k_msgq_alloc_init(&kmsgq_test_alloc, OVERFLOW_SIZE_MSG, MSGQ_LEN); zassert_true(ret == -EINVAL, "Invalid request"); } /** * @} */ |