1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "platform_queue_test.h"
10 #include "platform_assert.h"
11 #include "platform_queue.h"
12
13 #define HDF_LOG_TAG platform_queue_test
14
15 #define PLAT_QUEUE_TEST_TIMEOUT 1000
16
17 #define TEST_CODE_A 0x5A
18
19 struct PlatformQueueTestMsg {
20 struct PlatformMsg msg;
21 struct OsalSem sem;
22 int32_t status;
23 };
24
PlatformQueueTestHandle(struct PlatformQueue * queue,struct PlatformMsg * msg)25 static int32_t PlatformQueueTestHandle(struct PlatformQueue *queue, struct PlatformMsg *msg)
26 {
27 struct PlatformQueueTestMsg *tmsg = (struct PlatformQueueTestMsg *)msg;
28
29 if (msg == NULL) {
30 PLAT_LOGE("PlatformQueueTestHandle: msg is null!");
31 return HDF_ERR_INVALID_PARAM;
32 }
33
34 if (queue == NULL) {
35 tmsg->status = HDF_ERR_INVALID_OBJECT;
36 PLAT_LOGE("PlatformQueueTestHandle: queue object is null!");
37 return HDF_ERR_INVALID_OBJECT;
38 }
39
40 if (msg->data != queue) {
41 tmsg->status = HDF_ERR_INVALID_OBJECT;
42 PLAT_LOGE("PlatformQueueTestHandle: queue object is wrong!");
43 return HDF_ERR_INVALID_OBJECT;
44 }
45
46 if (msg->code != TEST_CODE_A) {
47 tmsg->status = HDF_ERR_INVALID_PARAM;
48 PLAT_LOGE("PlatformQueueTestHandle: code is wrong:%d!", msg->code);
49 return HDF_ERR_INVALID_PARAM;
50 }
51
52 (void)OsalSemPost(&tmsg->sem);
53 tmsg->status = HDF_SUCCESS;
54 return HDF_SUCCESS;
55 }
56
PlatformQueueTestAddAndWait(struct PlatformQueue * pq)57 static int32_t PlatformQueueTestAddAndWait(struct PlatformQueue *pq)
58 {
59 int32_t ret;
60 struct PlatformQueueTestMsg tmsg;
61
62 PLAT_LOGD("PlatformQueueTestAddAndWait: enter!");
63 tmsg.msg.code = TEST_CODE_A;
64 tmsg.msg.data = pq;
65 tmsg.status = -1;
66
67 (void)OsalSemInit(&tmsg.sem, 0);
68 // should add msg success
69 ret = PlatformQueueAddMsg(pq, &tmsg.msg);
70 if (!CHECK_EQ(ret, HDF_SUCCESS)) {
71 (void)OsalSemDestroy(&tmsg.sem);
72 return ret;
73 }
74
75 // should wait msg success
76 ret = OsalSemWait(&tmsg.sem, PLAT_QUEUE_TEST_TIMEOUT);
77 if (!CHECK_EQ(ret, HDF_SUCCESS)) {
78 (void)OsalSemDestroy(&tmsg.sem);
79 return ret;
80 }
81 if (!CHECK_EQ(tmsg.status, HDF_SUCCESS)) {
82 (void)OsalSemDestroy(&tmsg.sem);
83 return tmsg.status;
84 }
85
86 (void)OsalSemDestroy(&tmsg.sem);
87 PLAT_LOGD("PlatformQueueTestAddAndWait: exit!");
88 return HDF_SUCCESS;
89 }
90
PlatformQueueTestReliability(struct PlatformQueue * pq)91 static int32_t PlatformQueueTestReliability(struct PlatformQueue *pq)
92 {
93 int32_t ret;
94 struct PlatformMsg msg;
95
96 PLAT_LOGD("PlatformQueueTestReliability: enter");
97 ret = PlatformQueueAddMsg(NULL, &msg);
98 CHECK_NE_RETURN(ret, HDF_SUCCESS, ret);
99
100 ret = PlatformQueueAddMsg(pq, NULL);
101 CHECK_NE_RETURN(ret, HDF_SUCCESS, ret);
102
103 PLAT_LOGD("PlatformQueueTestReliability: exit!");
104 return HDF_SUCCESS;
105 }
106
107 struct PlatformQueueTestEntry {
108 int cmd;
109 int32_t (*func)(struct PlatformQueue *pq);
110 const char *name;
111 };
112
113 static struct PlatformQueueTestEntry g_entry[] = {
114 { PLAT_QUEUE_TEST_ADD_AND_WAIT, PlatformQueueTestAddAndWait, "PlatformQueueTestAddAndWait" },
115 { PLAT_QUEUE_TEST_RELIABILITY, PlatformQueueTestReliability, "PlatformQueueTestReliability" },
116 };
117
PlatformQueueTestExecute(int cmd)118 int PlatformQueueTestExecute(int cmd)
119 {
120 uint32_t i;
121 int32_t ret = HDF_ERR_NOT_SUPPORT;
122 struct PlatformQueue *pq = NULL;
123 struct PlatformQueueTestEntry *entry = NULL;
124
125 if (cmd > PLAT_QUEUE_TEST_CMD_MAX) {
126 PLAT_LOGE("PlatformQueueTestExecute: invalid cmd:%d", cmd);
127 ret = HDF_ERR_NOT_SUPPORT;
128 PLAT_LOGE("[PlatformQueueTestExecute][======cmd:%d====ret:%d======]", cmd, ret);
129 return ret;
130 }
131
132 for (i = 0; i < sizeof(g_entry) / sizeof(g_entry[0]); i++) {
133 if (g_entry[i].cmd != cmd || g_entry[i].func == NULL) {
134 continue;
135 }
136 entry = &g_entry[i];
137 break;
138 }
139
140 if (entry == NULL) {
141 PLAT_LOGE("PlatformQueueTestExecute: no entry matched, cmd = %d!", cmd);
142 return HDF_ERR_NOT_SUPPORT;
143 }
144
145 pq = PlatformQueueCreate(PlatformQueueTestHandle, "platform_queue_test", NULL);
146 if (pq == NULL) {
147 PLAT_LOGE("PlatformQueueTestExecute: create queue fail!");
148 return HDF_FAILURE;
149 }
150
151 ret = PlatformQueueStart(pq);
152 if (ret != HDF_SUCCESS) {
153 PLAT_LOGE("PlatformQueueTestExecute: start queue fail, ret = %d", ret);
154 PlatformQueueDestroy(pq);
155 return ret;
156 }
157
158 ret = entry->func(pq);
159 PlatformQueueDestroy(pq);
160
161 PLAT_LOGE("[PlatformQueueTestExecute][======cmd:%d====ret:%d======]", cmd, ret);
162 return ret;
163 }
164
PlatformQueueTestExecuteAll(void)165 void PlatformQueueTestExecuteAll(void)
166 {
167 int32_t i;
168 int32_t ret;
169 int32_t fails = 0;
170
171 for (i = 0; i < PLAT_QUEUE_TEST_CMD_MAX; i++) {
172 ret = PlatformQueueTestExecute(i);
173 fails += (ret != HDF_SUCCESS) ? 1 : 0;
174 }
175
176 PLAT_LOGE("PlatformQueueTestExecuteALL: **********PASS:%d FAIL:%d************\n\n",
177 PLAT_QUEUE_TEST_CMD_MAX - fails, fails);
178 }
179