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 | /* * Copyright (c) 2017 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <ztest.h> #include <kernel.h> #include <cmsis_os.h> struct sample_data { int data1; unsigned char data2; unsigned int data3; }; #define MAIL1_DATA1 75663 #define MAIL1_DATA2 156 #define MAIL1_DATA3 1000001 #define MAIL2_DATA1 93567 #define MAIL2_DATA2 255 #define MAIL2_DATA3 1234567 #define TIMEOUT 500 #define Q_LEN 5 osMailQDef(mail, Q_LEN, struct sample_data); osMailQId mail_id; void send_thread(void const *argument) { int i; struct sample_data *tx_ptr; struct sample_data zeroblock; osStatus status; /* This is used for comparison later in the function */ memset(&zeroblock, 0, sizeof(struct sample_data)); status = osMailPut(mail_id, NULL); zassert_true(status == osErrorValue, "Something's wrong with osMailPut. It is passing for NULL mail!"); /* Wait for mail_recv to complete initial checks */ osDelay(TIMEOUT); /* Prepare and send 1st mail */ tx_ptr = osMailAlloc(mail_id, osWaitForever); zassert_true(tx_ptr != NULL, "Mail1 alloc failed"); tx_ptr->data1 = MAIL1_DATA1; tx_ptr->data2 = MAIL1_DATA2; tx_ptr->data3 = MAIL1_DATA3; status = osMailPut(mail_id, tx_ptr); zassert_true(status == osOK, "osMailPut failure for mail1"); /* Fill the queue with blocks of mails */ for (i = 0; i < Q_LEN; i++) { /* Alternately use osMailAlloc and osMailCAlloc to ensure * both the APIs are tested. */ if (i & 1) { tx_ptr = osMailCAlloc(mail_id, osWaitForever); } else { tx_ptr = osMailAlloc(mail_id, osWaitForever); } zassert_true(tx_ptr != NULL, "Mail alloc failed"); tx_ptr->data1 = i; tx_ptr->data2 = i+1; tx_ptr->data3 = i+2; status = osMailPut(mail_id, tx_ptr); zassert_true(status == osOK, "osMailPut failure for mail!"); } /* Try allocating mail to a full queue immediately * before it is emptied out and assert failure */ tx_ptr = osMailAlloc(mail_id, 0); zassert_true(tx_ptr == NULL, "MailAlloc passed. Something's wrong"); tx_ptr = osMailCAlloc(mail_id, 0); zassert_true(tx_ptr == NULL, "MailCAlloc passed. Something's wrong"); /* Try allocating mail to a full queue within a duration * less than TIMEOUT, before the queue is emptied out */ tx_ptr = osMailAlloc(mail_id, TIMEOUT/3); zassert_true(tx_ptr == NULL, "MailAlloc passed. Something's wrong"); tx_ptr = osMailCAlloc(mail_id, TIMEOUT/3); zassert_true(tx_ptr == NULL, "MailCAlloc passed. Something's wrong"); /* Send another mail after the queue is emptied */ tx_ptr = osMailCAlloc(mail_id, TIMEOUT*2); zassert_true(tx_ptr != NULL, "Mail alloc failed"); zassert_equal(memcmp(tx_ptr, &zeroblock, sizeof(struct sample_data)), 0, "osMailCAlloc returned memory not initialized to 0"); tx_ptr->data1 = MAIL2_DATA1; tx_ptr->data2 = MAIL2_DATA2; tx_ptr->data3 = MAIL2_DATA3; status = osMailPut(mail_id, tx_ptr); zassert_true(status == osOK, "osMailPut failure for mail"); } void mail_recv(void) { int i; struct sample_data *rx_ptr; osEvent evt; osStatus status; /* Try getting mail immediately before the queue is populated */ evt = osMailGet(mail_id, 0); zassert_true(evt.status == osOK, "Something's wrong with osMailGet!"); /* Try receiving mail within a duration of TIMEOUT */ evt = osMailGet(mail_id, TIMEOUT); zassert_true(evt.status == osEventTimeout, "Something's wrong with osMailGet!"); /* Receive 1st mail */ evt = osMailGet(mail_id, osWaitForever); zassert_true(evt.status == osEventMail, "osMailGet failure"); rx_ptr = evt.value.p; zassert_equal(rx_ptr->data1, MAIL1_DATA1, NULL); zassert_equal(rx_ptr->data2, MAIL1_DATA2, NULL); zassert_equal(rx_ptr->data3, MAIL1_DATA3, NULL); status = osMailFree(mail_id, rx_ptr); zassert_true(status == osOK, "osMailFree failure"); /* Wait for queue to get filled */ osDelay(TIMEOUT); /* Empty the queue */ for (i = 0; i < Q_LEN; i++) { evt = osMailGet(mail_id, osWaitForever); zassert_true(evt.status == osEventMail, "osMailGet failure"); rx_ptr = evt.value.p; zassert_equal(rx_ptr->data1, i, NULL); zassert_equal(rx_ptr->data2, i + 1, NULL); zassert_equal(rx_ptr->data3, i + 2, NULL); status = osMailFree(mail_id, rx_ptr); zassert_true(status == osOK, "osMailFree failure"); } /* Receive the next mail */ evt = osMailGet(mail_id, osWaitForever); zassert_true(evt.status == osEventMail, "osMailGet failure"); rx_ptr = evt.value.p; zassert_equal(rx_ptr->data1, MAIL2_DATA1, NULL); zassert_equal(rx_ptr->data2, MAIL2_DATA2, NULL); zassert_equal(rx_ptr->data3, MAIL2_DATA3, NULL); status = osMailFree(mail_id, rx_ptr); zassert_true(status == osOK, "osMailFree failure"); } osThreadDef(send_thread, osPriorityNormal, 1, 0); void test_mailq(void) { osThreadId tid; mail_id = osMailCreate(osMailQ(mail), NULL); zassert_true(mail_id != NULL, "Mail creation failed"); tid = osThreadCreate(osThread(send_thread), NULL); zassert_true(tid != NULL, "Thread creation failed"); mail_recv(); } |