1 /*
2 * Copyright (c) 2021 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 if (msg == NULL) {
29 return HDF_ERR_INVALID_PARAM;
30 }
31
32 if (queue == NULL) {
33 tmsg->status = HDF_ERR_INVALID_OBJECT;
34 PLAT_LOGE("%s: queue object is NULL", __func__);
35 return HDF_ERR_INVALID_OBJECT;
36 }
37
38 if (msg->data != queue) {
39 tmsg->status = HDF_ERR_INVALID_OBJECT;
40 PLAT_LOGE("%s: queue object is wrong", __func__);
41 return HDF_ERR_INVALID_OBJECT;
42 }
43
44 if (msg->code != TEST_CODE_A) {
45 tmsg->status = HDF_ERR_INVALID_PARAM;
46 PLAT_LOGE("%s: code is wrong:%d", __func__, msg->code);
47 return HDF_ERR_INVALID_PARAM;
48 }
49
50 (void)OsalSemPost(&tmsg->sem);
51 tmsg->status = HDF_SUCCESS;
52 return HDF_SUCCESS;
53 }
54
PlatformQueueTestAddAndWait(struct PlatformQueue * pq)55 static int32_t PlatformQueueTestAddAndWait(struct PlatformQueue *pq)
56 {
57 int32_t ret;
58 struct PlatformQueueTestMsg tmsg;
59
60 PLAT_LOGD("%s: enter", __func__);
61 tmsg.msg.code = TEST_CODE_A;
62 tmsg.msg.data = pq;
63 tmsg.status = -1;
64
65 (void)OsalSemInit(&tmsg.sem, 0);
66 // should add msg success
67 ret = PlatformQueueAddMsg(pq, &tmsg.msg);
68 if (!CHECK_EQ(ret, HDF_SUCCESS)) {
69 (void)OsalSemDestroy(&tmsg.sem);
70 return ret;
71 }
72
73 // should wait msg success
74 ret = OsalSemWait(&tmsg.sem, PLAT_QUEUE_TEST_TIMEOUT);
75 if (!CHECK_EQ(ret, HDF_SUCCESS)) {
76 (void)OsalSemDestroy(&tmsg.sem);
77 return ret;
78 }
79 if (!CHECK_EQ(tmsg.status, HDF_SUCCESS)) {
80 (void)OsalSemDestroy(&tmsg.sem);
81 return tmsg.status;
82 }
83
84 (void)OsalSemDestroy(&tmsg.sem);
85 PLAT_LOGD("%s: exit", __func__);
86 return HDF_SUCCESS;
87 }
88
PlatformQueueTestReliability(struct PlatformQueue * pq)89 static int32_t PlatformQueueTestReliability(struct PlatformQueue *pq)
90 {
91 int32_t ret;
92 struct PlatformMsg msg;
93
94 PLAT_LOGD("%s: enter", __func__);
95 ret = PlatformQueueAddMsg(NULL, &msg);
96 CHECK_NE_RETURN(ret, HDF_SUCCESS, ret);
97
98 ret = PlatformQueueAddMsg(pq, NULL);
99 CHECK_NE_RETURN(ret, HDF_SUCCESS, ret);
100
101 PLAT_LOGD("%s: exit", __func__);
102 return HDF_SUCCESS;
103 }
104
105 struct PlatformQueueTestEntry {
106 int cmd;
107 int32_t (*func)(struct PlatformQueue *pq);
108 const char *name;
109 };
110
111 static struct PlatformQueueTestEntry g_entry[] = {
112 { PLAT_QUEUE_TEST_ADD_AND_WAIT, PlatformQueueTestAddAndWait, "PlatformQueueTestAddAndWait" },
113 { PLAT_QUEUE_TEST_RELIABILITY, PlatformQueueTestReliability, "PlatformQueueTestReliability" },
114 };
115
PlatformQueueTestExecute(int cmd)116 int PlatformQueueTestExecute(int cmd)
117 {
118 uint32_t i;
119 int32_t ret = HDF_ERR_NOT_SUPPORT;
120 struct PlatformQueue *pq = NULL;
121 struct PlatformQueueTestEntry *entry = NULL;
122
123 if (cmd > PLAT_QUEUE_TEST_CMD_MAX) {
124 PLAT_LOGE("PlatformQueueTestExecute: invalid cmd:%d", cmd);
125 ret = HDF_ERR_NOT_SUPPORT;
126 PLAT_LOGE("[PlatformQueueTestExecute][======cmd:%d====ret:%d======]", cmd, ret);
127 return ret;
128 }
129
130 for (i = 0; i < sizeof(g_entry) / sizeof(g_entry[0]); i++) {
131 if (g_entry[i].cmd != cmd || g_entry[i].func == NULL) {
132 continue;
133 }
134 entry = &g_entry[i];
135 break;
136 }
137
138 if (entry == NULL) {
139 PLAT_LOGE("%s: no entry matched, cmd = %d", __func__, cmd);
140 return HDF_ERR_NOT_SUPPORT;
141 }
142
143 pq = PlatformQueueCreate(PlatformQueueTestHandle, "platform_queue_test", NULL);
144 if (pq == NULL) {
145 PLAT_LOGE("%s: create queue failed", __func__);
146 return HDF_FAILURE;
147 }
148
149 ret = PlatformQueueStart(pq);
150 if (ret != HDF_SUCCESS) {
151 PLAT_LOGE("%s: start queue failed, ret = %d", __func__, ret);
152 PlatformQueueDestroy(pq);
153 return ret;
154 }
155
156 ret = entry->func(pq);
157 PlatformQueueDestroy(pq);
158
159 PLAT_LOGE("[PlatformQueueTestExecute][======cmd:%d====ret:%d======]", cmd, ret);
160 return ret;
161 }
162
PlatformQueueTestExecuteAll(void)163 void PlatformQueueTestExecuteAll(void)
164 {
165 int32_t i;
166 int32_t ret;
167 int32_t fails = 0;
168
169 for (i = 0; i < PLAT_QUEUE_TEST_CMD_MAX; i++) {
170 ret = PlatformQueueTestExecute(i);
171 fails += (ret != HDF_SUCCESS) ? 1 : 0;
172 }
173
174 PLAT_LOGE("PlatformQueueTestExecuteALL: **********PASS:%d FAIL:%d************\n\n",
175 PLAT_QUEUE_TEST_CMD_MAX - fails, fails);
176 }
177