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 "pin_test.h"
10 #include "hdf_base.h"
11 #include "hdf_io_service_if.h"
12 #include "hdf_log.h"
13 #include "osal_mem.h"
14 #include "osal_time.h"
15 #include "pin_if.h"
16 #include "securec.h"
17
18 #define HDF_LOG_TAG pin_test_c
19
20 #define PIN_FUNC_NAME_LENGTH 30
21
22 struct PinCfgs {
23 enum PinPullType pullTypeNum;
24 uint32_t strengthNum;
25 const char *funcName;
26 } g_oldPinCfg;
27
28 struct PinTestEntry {
29 int cmd;
30 int32_t (*func)(void);
31 const char *name;
32 };
33
PinTestGetTestConfig(struct PinTestConfig * config)34 static int32_t PinTestGetTestConfig(struct PinTestConfig *config)
35 {
36 int32_t ret;
37 struct HdfSBuf *reply = NULL;
38 struct HdfIoService *service = NULL;
39 const void *buf = NULL;
40 uint32_t len;
41
42 HDF_LOGD("%s: enter", __func__);
43 service = HdfIoServiceBind("PIN_TEST");
44 if (service == NULL) {
45 return HDF_ERR_NOT_SUPPORT;
46 }
47
48 reply = HdfSbufObtain(sizeof(*config) + sizeof(uint64_t));
49 if (reply == NULL) {
50 HDF_LOGE("%s: Failed to obtain reply", __func__);
51 return HDF_ERR_MALLOC_FAIL;
52 }
53
54 ret = service->dispatcher->Dispatch(&service->object, 0, NULL, reply);
55 if (ret != HDF_SUCCESS) {
56 HDF_LOGE("%s: Remote dispatch failed", __func__);
57 return ret;
58 }
59
60 if (!HdfSbufReadBuffer(reply, &buf, &len)) {
61 HDF_LOGE("%s: Read buf failed", __func__);
62 HdfSbufRecycle(reply);
63 return HDF_ERR_IO;
64 }
65
66 if (len != sizeof(*config)) {
67 HDF_LOGE("%s: Config size:%zu, read size:%u", __func__, sizeof(*config), len);
68 HdfSbufRecycle(reply);
69 return HDF_ERR_IO;
70 }
71
72 if (memcpy_s(config, sizeof(*config), buf, sizeof(*config)) != EOK) {
73 HDF_LOGE("%s: Memcpy buf failed", __func__);
74 HdfSbufRecycle(reply);
75 return HDF_ERR_IO;
76 }
77 HdfSbufRecycle(reply);
78 HDF_LOGD("%s: Done", __func__);
79 return HDF_SUCCESS;
80 }
81
PinTesterGet(void)82 struct PinTester *PinTesterGet(void)
83 {
84 int32_t ret;
85 static struct PinTester tester;
86 static bool hasInit = false;
87
88 if (hasInit) {
89 return &tester;
90 }
91 HDF_LOGI("%s: enter", __func__);
92 if (hasInit) {
93 return &tester;
94 }
95 ret = PinTestGetTestConfig(&tester.config);
96 if (ret != HDF_SUCCESS) {
97 HDF_LOGE("%s: read config failed:%d", __func__, ret);
98 return NULL;
99 }
100 tester.handle = PinGet(tester.config.pinName);
101 if (tester.handle == NULL) {
102 HDF_LOGE("%s: open pin:%s failed", __func__, tester.config.pinName);
103 return NULL;
104 }
105 hasInit = true;
106 HDF_LOGD("%s: Done", __func__);
107 return &tester;
108 }
109
PinSetGetPullTest(void)110 static int32_t PinSetGetPullTest(void)
111 {
112 struct PinTester *tester = NULL;
113 int32_t ret;
114 enum PinPullType getPullTypeNum;
115
116 getPullTypeNum = 0;
117 tester = PinTesterGet();
118 if (tester == NULL) {
119 HDF_LOGE("%s: Get tester failed!", __func__);
120 return HDF_ERR_INVALID_OBJECT;
121 }
122 ret = PinSetPull(tester->handle, tester->config.PullTypeNum);
123 if (ret != HDF_SUCCESS) {
124 HDF_LOGE("%s: Pin set pull failed!, pinName:%s", __func__, tester->config.pinName);
125 return HDF_FAILURE;
126 }
127 HDF_LOGI("%s: Pin set pull success!, PullTypeNum:%d", __func__, tester->config.PullTypeNum);
128 ret = PinGetPull(tester->handle, &getPullTypeNum);
129 if (ret != HDF_SUCCESS) {
130 HDF_LOGE("%s: Pin get pull failed!, pinName:%s", __func__, tester->config.pinName);
131 return HDF_FAILURE;
132 }
133 if (tester->config.PullTypeNum != getPullTypeNum) {
134 HDF_LOGE("%s: Pin set pull:%d, but Pin get pull:%u", __func__, tester->config.PullTypeNum, getPullTypeNum);
135 }
136 HDF_LOGD("%s: done", __func__);
137 return HDF_SUCCESS;
138 }
139
PinSetGetStrengthTest(void)140 static int32_t PinSetGetStrengthTest(void)
141 {
142 struct PinTester *tester = NULL;
143 int32_t ret;
144 uint32_t getStrengthNum;
145
146 getStrengthNum = 0;
147 tester = PinTesterGet();
148 if (tester == NULL) {
149 HDF_LOGE("%s: Get tester failed!", __func__);
150 return HDF_ERR_INVALID_OBJECT;
151 }
152 ret = PinSetStrength(tester->handle, tester->config.strengthNum);
153 if (ret != HDF_SUCCESS) {
154 HDF_LOGE("%s: Pin set strength failed!, pinName:%s", __func__, tester->config.pinName);
155 return HDF_FAILURE;
156 }
157 HDF_LOGI("%s: Pin set strength success!,strengthNum:%d", __func__, tester->config.strengthNum);
158 ret = PinGetStrength(tester->handle, &getStrengthNum);
159 if (ret != HDF_SUCCESS) {
160 HDF_LOGE("%s: Pin get pull failed!, pinName:%s", __func__, tester->config.pinName);
161 return HDF_FAILURE;
162 }
163 if (tester->config.strengthNum != getStrengthNum) {
164 HDF_LOGE("%s: Pin set strength:%d, but Pin get strength:%d", __func__,
165 tester->config.strengthNum, getStrengthNum);
166 }
167 HDF_LOGD("%s: done", __func__);
168 return HDF_SUCCESS;
169 }
170
PinSetGetFuncTest(void)171 static int32_t PinSetGetFuncTest(void)
172 {
173 struct PinTester *tester = NULL;
174 int32_t ret;
175
176 tester = PinTesterGet();
177 if (tester == NULL) {
178 HDF_LOGE("%s: Get tester failed!", __func__);
179 return HDF_ERR_INVALID_OBJECT;
180 }
181 ret = PinSetFunc(tester->handle, (const char *)tester->config.funcNameBuf);
182 if (ret != HDF_SUCCESS) {
183 HDF_LOGE("%s: Pin set function failed!, pinName:%s, functionName:%s", __func__,
184 tester->config.pinName, tester->config.funcNameBuf);
185 return HDF_FAILURE;
186 }
187 HDF_LOGI("%s: Pin set function success!, pinName:%s, functionName:%s", __func__,
188 tester->config.pinName, tester->config.funcNameBuf);
189 ret = PinGetFunc(tester->handle, &tester->config.funcName);
190 if (ret != HDF_SUCCESS) {
191 HDF_LOGE("%s: Pin get function failed!, pinName:%s, functionName:%s", __func__,
192 tester->config.pinName, tester->config.funcName);
193 return HDF_FAILURE;
194 }
195 HDF_LOGI("%s: Pin get function success!, pinName:%s, functionName:%s", __func__,
196 tester->config.pinName, tester->config.funcName);
197 if (strcmp((const char *)tester->config.funcNameBuf, tester->config.funcName) != 0) {
198 HDF_LOGE("%s: Pin set function:%s, but Pin get function:%s", __func__,
199 tester->config.funcNameBuf, tester->config.funcName);
200 }
201 HDF_LOGD("%s: done", __func__);
202 return HDF_SUCCESS;
203 }
204
PinTestSetUpAll(void)205 int32_t PinTestSetUpAll(void)
206 {
207 int32_t ret;
208 struct PinTester *tester = NULL;
209 struct PinTestConfig *cfg = NULL;
210
211 HDF_LOGD("%s: enter!", __func__);
212 tester = PinTesterGet();
213 if (tester == NULL) {
214 HDF_LOGE("%s: get tester fail!", __func__);
215 return HDF_ERR_INVALID_OBJECT;
216 }
217 tester->total = PIN_TEST_CMD_MAX;
218 tester->fails = 0;
219
220 cfg = &tester->config;
221 HDF_LOGD("%s: test on pinName:%s, PullTypeNum:%d, strengthNum:%d", __func__,
222 cfg->pinName, cfg->PullTypeNum, cfg->strengthNum);
223 ret = PinGetPull(tester->handle, &g_oldPinCfg.pullTypeNum);
224 if (ret != HDF_SUCCESS) {
225 HDF_LOGE("%s: get pullTypeNum failed!", __func__);
226 return ret;
227 }
228 ret = PinGetStrength(tester->handle, &g_oldPinCfg.strengthNum);
229 if (ret != HDF_SUCCESS) {
230 HDF_LOGE("%s: get strengthNum failed!", __func__);
231 return ret;
232 }
233 g_oldPinCfg.funcName = (char *)OsalMemCalloc(PIN_FUNC_NAME_LENGTH * sizeof(char));
234 if (g_oldPinCfg.funcName == NULL) {
235 HDF_LOGE("%s: alloc g_oldPinCfg.funcName failed", __func__);
236 return HDF_ERR_MALLOC_FAIL;
237 }
238 tester->config.funcName = (char *)OsalMemCalloc(PIN_FUNC_NAME_LENGTH * sizeof(char));
239 if (tester->config.funcName == NULL) {
240 HDF_LOGE("%s: alloc tester->config.funcName failed", __func__);
241 return HDF_ERR_MALLOC_FAIL;
242 }
243 ret = PinGetFunc(tester->handle, &g_oldPinCfg.funcName);
244 if (ret != HDF_SUCCESS) {
245 HDF_LOGE("%s: get funcName failed!", __func__);
246 return ret;
247 }
248 HDF_LOGI(":%s old funcName:%s", __func__, g_oldPinCfg.funcName);
249 HDF_LOGD("%s: exit!", __func__);
250
251 return HDF_SUCCESS;
252 }
253
PinTestTearDownAll(void)254 int32_t PinTestTearDownAll(void)
255 {
256 int32_t ret;
257 struct PinTester *tester = NULL;
258
259 HDF_LOGD("%s: enter!", __func__);
260 tester = PinTesterGet();
261 if (tester == NULL) {
262 HDF_LOGE("%s: get tester fail!", __func__);
263 return HDF_ERR_INVALID_OBJECT;
264 }
265 ret = PinSetPull(tester->handle, g_oldPinCfg.pullTypeNum);
266 if (ret != HDF_SUCCESS) {
267 HDF_LOGE("%s: set pullTypeNum failed!", __func__);
268 return ret;
269 }
270 ret = PinSetStrength(tester->handle, g_oldPinCfg.strengthNum);
271 if (ret != HDF_SUCCESS) {
272 HDF_LOGE("%s: set strengthNum failed!", __func__);
273 return ret;
274 }
275 ret = PinSetFunc(tester->handle, g_oldPinCfg.funcName);
276 if (ret != HDF_SUCCESS) {
277 HDF_LOGE("%s: set funcName failed!", __func__);
278 return ret;
279 }
280 HDF_LOGD("%s: exit!", __func__);
281
282 return HDF_SUCCESS;
283 }
284
PinTestSetUpSingle(void)285 int32_t PinTestSetUpSingle(void)
286 {
287 return HDF_SUCCESS;
288 }
289
PinTestTearDownSingle(void)290 int32_t PinTestTearDownSingle(void)
291 {
292 return HDF_SUCCESS;
293 }
294
PinTestReliability(void)295 int32_t PinTestReliability(void)
296 {
297 struct PinTester *tester = NULL;
298
299 HDF_LOGI("%s: enter", __func__);
300 tester = PinTesterGet();
301 if (tester == NULL || tester->handle == NULL) {
302 return HDF_ERR_INVALID_OBJECT;
303 }
304 HDF_LOGD("%s: test dfr for PinSetPull ...", __func__);
305 // invalid handle
306 (void)PinSetPull(NULL, tester->config.PullTypeNum);
307 (void)PinGetPull(NULL, &tester->config.PullTypeNum);
308 (void)PinSetStrength(NULL, tester->config.strengthNum);
309 (void)PinGetStrength(NULL, &tester->config.strengthNum);
310 (void)PinSetFunc(NULL, (const char *)tester->config.funcNameBuf);
311 (void)PinGetFunc(NULL, &tester->config.funcName);
312 // invalid strengthNum
313 (void)PinSetStrength(tester->handle, -1);
314 (void)PinGetStrength(tester->handle, NULL);
315 // invalid FuncName
316 (void)PinSetFunc(tester->handle, NULL);
317 HDF_LOGD("%s:test done.All completed!", __func__);
318
319 return HDF_SUCCESS;
320 }
321
322 static struct PinTestEntry g_entry[] = {
323 { PIN_TEST_CMD_SETGETPULL, PinSetGetPullTest, "PinSetGetPullTest" },
324 { PIN_TEST_CMD_SETGETSTRENGTH, PinSetGetStrengthTest, "PinSetGetStrengthTest" },
325 { PIN_TEST_CMD_SETGETFUNC, PinSetGetFuncTest, "PinSetGetFuncTest" },
326 { PIN_TEST_CMD_RELIABILITY, PinTestReliability, "PinTestReliability" },
327 { PIN_TEST_CMD_SETUP_ALL, PinTestSetUpAll, "PinTestSetUpAll" },
328 { PIN_TEST_CMD_TEARDOWN_ALL, PinTestTearDownAll, "PinTestTearDownAll" },
329 };
330
PinTestExecute(int cmd)331 int32_t PinTestExecute(int cmd)
332 {
333 uint32_t i;
334 int32_t ret = HDF_ERR_NOT_SUPPORT;
335
336 if (cmd > PIN_TEST_CMD_MAX) {
337 HDF_LOGE("%s: invalid cmd:%d", __func__, cmd);
338 ret = HDF_ERR_NOT_SUPPORT;
339 HDF_LOGE("[%s][======cmd:%d====ret:%d======]", __func__, cmd, ret);
340 return ret;
341 }
342
343 for (i = 0; i < sizeof(g_entry) / sizeof(g_entry[0]); i++) {
344 if (g_entry[i].cmd != cmd || g_entry[i].func == NULL) {
345 continue;
346 }
347 ret = g_entry[i].func();
348 break;
349 }
350
351 HDF_LOGE("[%s][======cmd:%d====ret:%d======]", __func__, cmd, ret);
352 return ret;
353 }
354
PinTestExecuteAll(void)355 void PinTestExecuteAll(void)
356 {
357 int32_t i;
358 int32_t ret;
359 int32_t fails = 0;
360
361 /* setup env for all test cases */
362 ret = PinTestExecute(PIN_TEST_CMD_SETUP_ALL);
363 if (ret != HDF_SUCCESS) {
364 HDF_LOGE("func:%s PinTestExecute SETUP failed", __func__);
365 }
366
367 for (i = 0; i < PIN_TEST_CMD_SETUP_ALL; i++) {
368 ret = PinTestExecute(i);
369 fails += (ret != HDF_SUCCESS) ? 1 : 0;
370 }
371
372 /* teardown env for all test cases */
373 ret = PinTestExecute(PIN_TEST_CMD_TEARDOWN_ALL);
374 if (ret != HDF_SUCCESS) {
375 HDF_LOGE("func:%s PinTestExecute TEARDOWN failed", __func__);
376 }
377
378 HDF_LOGI("%s: **********PASS:%d FAIL:%d************\n\n",
379 __func__, PIN_TEST_CMD_RELIABILITY + 1 - fails, fails);
380 }
381