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