1 /*
2  * Copyright (c) 2022-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_dumper_test.h"
10 #include "osal_io.h"
11 #include "osal_time.h"
12 #include "platform_assert.h"
13 #include "platform_dumper.h"
14 #include "securec.h"
15 
16 #define HDF_LOG_TAG platform_dumper_test
17 
18 static const char *g_dumperTestName = "dumperTestName";
19 struct PlatformDumper *g_dumperTest = NULL;
20 struct PlatformDumperTestEntry {
21     int cmd;
22     int32_t (*func)(void);
23 };
24 
PlatformDumperTestAdd(const char * name,enum PlatformDumperDataType type,void * paddr)25 static int32_t PlatformDumperTestAdd(const char *name, enum PlatformDumperDataType type, void *paddr)
26 {
27     int32_t ret;
28     struct PlatformDumperData data;
29     if (memset_s(&data, sizeof(struct PlatformDumperData), 0, sizeof(struct PlatformDumperData)) != EOK) {
30         HDF_LOGE("PlatformDumperTestAdd: memset_s fail!");
31         return HDF_FAILURE;
32     }
33 
34     data.name = name;
35     data.type = type;
36     data.paddr = paddr;
37     ret = PlatformDumperAddData(g_dumperTest, &data);
38     if (ret != HDF_SUCCESS) {
39         HDF_LOGE("PlatformDumperTestAdd: add data fail, ret: %d!", ret);
40         return ret;
41     }
42 
43     ret = PlatformDumperDump(g_dumperTest);
44     if (ret != HDF_SUCCESS) {
45         HDF_LOGE("PlatformDumperTestAdd: get info fail, ret: %d!", ret);
46         return ret;
47     }
48 
49     return HDF_SUCCESS;
50 }
51 
52 static uint8_t g_uint8_test = 80;
53 static uint16_t g_uint16_test = 1600;
54 static uint32_t g_uint32_test = 3200;
55 static uint64_t g_uint64_test = 6400;
56 
57 static int8_t g_int8_test = 81;
58 static int16_t g_int16_test = 1601;
59 static int32_t g_int32_test = 3201;
60 static int64_t g_int64_test = 6401;
61 
62 static float g_floatTest = 3.13;
63 static double g_doubleTest = 3.1314;
64 static char g_charTest = 'a';
65 static char *g_stringTest = "abcdefg";
66 static volatile unsigned char *g_registerLTest = NULL;
67 static volatile unsigned char *g_registerWTest = NULL;
68 static volatile unsigned char *g_registerBTest = NULL;
69 
70 static struct PlatformDumperData g_datas[] = {
71     {"arrayDumperTestUint8",  PLATFORM_DUMPER_UINT8,  &g_uint8_test },
72     {"arrayDumperTestUint16", PLATFORM_DUMPER_UINT16, &g_uint16_test},
73     {"arrayDumperTestUint32", PLATFORM_DUMPER_UINT32, &g_uint32_test},
74     {"arrayDumperTestUint64", PLATFORM_DUMPER_UINT64, &g_uint64_test},
75     {"arrayDumperTestint64",  PLATFORM_DUMPER_INT64,  &g_int64_test },
76     {"arrayDumperTestchar",   PLATFORM_DUMPER_CHAR,   &g_charTest  }
77 };
78 
PlatformDumperTestAddatas(void)79 static int32_t PlatformDumperTestAddatas(void)
80 {
81     int32_t ret;
82     ret = PlatformDumperAddDatas(g_dumperTest, g_datas, sizeof(g_datas) / sizeof(g_datas[0]));
83     if (ret != HDF_SUCCESS) {
84         HDF_LOGE("PlatformDumperTestAddatas: add data fail, ret: %d!", ret);
85         return ret;
86     }
87 
88     HDF_LOGD("PlatformDumperTestAddatas: add data ok, then print!");
89 
90     ret = PlatformDumperDump(g_dumperTest);
91     if (ret != HDF_SUCCESS) {
92         HDF_LOGE("PlatformDumperTestAddatas: dumper info fail, ret: %d!", ret);
93         return ret;
94     }
95 
96     return HDF_SUCCESS;
97 }
98 
PlatformDumperTestAddUint8(void)99 static int32_t PlatformDumperTestAddUint8(void)
100 {
101     int32_t ret;
102     ret = PlatformDumperTestAdd("dumperTestUint8", PLATFORM_DUMPER_UINT8, &g_uint8_test);
103     if (ret != HDF_SUCCESS) {
104         HDF_LOGE("PlatformDumperTestAddUint8: add data fail, ret: %d!", ret);
105         return ret;
106     }
107 
108     return HDF_SUCCESS;
109 }
110 
PlatformDumperTestAddUint16(void)111 static int32_t PlatformDumperTestAddUint16(void)
112 {
113     int32_t ret;
114     ret = PlatformDumperTestAdd("dumperTestUint16", PLATFORM_DUMPER_UINT16, &g_uint16_test);
115     if (ret != HDF_SUCCESS) {
116         HDF_LOGE("PlatformDumperTestAddUint16: add data fail, ret: %d!", ret);
117         return ret;
118     }
119 
120     return HDF_SUCCESS;
121 }
122 
PlatformDumperTestAddUint32(void)123 static int32_t PlatformDumperTestAddUint32(void)
124 {
125     int32_t ret;
126     ret = PlatformDumperTestAdd("dumperTestUint32", PLATFORM_DUMPER_UINT32, &g_uint32_test);
127     if (ret != HDF_SUCCESS) {
128         HDF_LOGE("PlatformDumperTestAddUint32: add data fail, ret: %d!", ret);
129         return ret;
130     }
131 
132     return HDF_SUCCESS;
133 }
134 
PlatformDumperTestAddUint64(void)135 static int32_t PlatformDumperTestAddUint64(void)
136 {
137     int32_t ret;
138     ret = PlatformDumperTestAdd("dumperTestUint64", PLATFORM_DUMPER_UINT64, &g_uint64_test);
139     if (ret != HDF_SUCCESS) {
140         HDF_LOGE("PlatformDumperTestAddUint64: add data fail, ret: %d!", ret);
141         return ret;
142     }
143 
144     return HDF_SUCCESS;
145 }
146 
PlatformDumperTestAddInt8(void)147 static int32_t PlatformDumperTestAddInt8(void)
148 {
149     int32_t ret;
150     ret = PlatformDumperTestAdd("dumperTestInt8", PLATFORM_DUMPER_INT8, &g_int8_test);
151     if (ret != HDF_SUCCESS) {
152         HDF_LOGE("PlatformDumperTestAddInt8: add data fail, ret: %d!", ret);
153         return ret;
154     }
155 
156     return HDF_SUCCESS;
157 }
158 
PlatformDumperTestAddInt16(void)159 static int32_t PlatformDumperTestAddInt16(void)
160 {
161     int32_t ret;
162     ret = PlatformDumperTestAdd("dumperTestInt16", PLATFORM_DUMPER_INT16, &g_int16_test);
163     if (ret != HDF_SUCCESS) {
164         HDF_LOGE("PlatformDumperTestAddInt16: add data fail, ret: %d!", ret);
165         return ret;
166     }
167 
168     return HDF_SUCCESS;
169 }
170 
PlatformDumperTestAddInt32(void)171 static int32_t PlatformDumperTestAddInt32(void)
172 {
173     int32_t ret;
174     ret = PlatformDumperTestAdd("dumperTestInt32", PLATFORM_DUMPER_INT32, &g_int32_test);
175     if (ret != HDF_SUCCESS) {
176         HDF_LOGE("PlatformDumperTestAddInt32: add data fail, ret: %d!", ret);
177         return ret;
178     }
179 
180     return HDF_SUCCESS;
181 }
182 
PlatformDumperTestAddInt64(void)183 static int32_t PlatformDumperTestAddInt64(void)
184 {
185     int32_t ret;
186     ret = PlatformDumperTestAdd("dumperTestInt64", PLATFORM_DUMPER_INT64, &g_int64_test);
187     if (ret != HDF_SUCCESS) {
188         HDF_LOGE("PlatformDumperTestAddInt64: add data fail, ret: %d!", ret);
189         return ret;
190     }
191 
192     return HDF_SUCCESS;
193 }
194 
PlatformDumperTestAddFloat(void)195 static int32_t PlatformDumperTestAddFloat(void)
196 {
197     int32_t ret;
198     ret = PlatformDumperTestAdd("dumperTestFloat", PLATFORM_DUMPER_FLOAT, &g_floatTest);
199     if (ret != HDF_SUCCESS) {
200         HDF_LOGE("PlatformDumperTestAddFloat: add data fail!");
201         return ret;
202     }
203 
204     return HDF_SUCCESS;
205 }
206 
PlatformDumperTestAddDouble(void)207 static int32_t PlatformDumperTestAddDouble(void)
208 {
209     int32_t ret;
210     ret = PlatformDumperTestAdd("dumperTestDouble", PLATFORM_DUMPER_DOUBLE, &g_doubleTest);
211     if (ret != HDF_SUCCESS) {
212         HDF_LOGE("PlatformDumperTestAddDouble: add data fail, ret: %d!", ret);
213         return ret;
214     }
215 
216     return HDF_SUCCESS;
217 }
218 
PlatformDumperTestAddChar(void)219 static int32_t PlatformDumperTestAddChar(void)
220 {
221     int32_t ret;
222     ret = PlatformDumperTestAdd("dumperTestChar", PLATFORM_DUMPER_CHAR, &g_charTest);
223     if (ret != HDF_SUCCESS) {
224         HDF_LOGE("PlatformDumperTestAddChar: add data fail, ret: %d!", ret);
225         return ret;
226     }
227 
228     return HDF_SUCCESS;
229 }
230 
PlatformDumperTestAddString(void)231 static int32_t PlatformDumperTestAddString(void)
232 {
233     int32_t ret;
234     ret = PlatformDumperTestAdd("dumperTestString", PLATFORM_DUMPER_STRING, g_stringTest);
235     if (ret != HDF_SUCCESS) {
236         HDF_LOGE("PlatformDumperTestAddString: add data fail, ret: %d!", ret);
237         return ret;
238     }
239 
240     return HDF_SUCCESS;
241 }
242 
243 #define TIMER0_VALUE 0x12000004
244 #define GPIO0_VALUE  0x120d0400
245 #define ADC_VALUE    0x120e0024
PlatformDumperTestAddRegister(void)246 static int32_t PlatformDumperTestAddRegister(void)
247 {
248     int32_t ret;
249     if (g_registerLTest == NULL) {
250         g_registerLTest = OsalIoRemap(TIMER0_VALUE, 0x1000);
251     }
252     if (g_registerWTest == NULL) {
253         g_registerWTest = OsalIoRemap(GPIO0_VALUE, 0x1000);
254     }
255     if (g_registerBTest == NULL) {
256         g_registerBTest = OsalIoRemap(ADC_VALUE, 0x1000);
257     }
258     ret = PlatformDumperTestAdd("dumperTestRegisterL", PLATFORM_DUMPER_REGISTERL, (void *)g_registerLTest);
259     if (ret != HDF_SUCCESS) {
260         HDF_LOGE("PlatformDumperTestAddRegister: add registerL data fail, ret: %d!", ret);
261         return ret;
262     }
263     ret = PlatformDumperTestAdd("dumperTestRegisterW", PLATFORM_DUMPER_REGISTERW, (void *)g_registerWTest);
264     if (ret != HDF_SUCCESS) {
265         HDF_LOGE("PlatformDumperTestAddRegister: add registerW data fail, ret: %d!", ret);
266         return ret;
267     }
268     ret = PlatformDumperTestAdd("dumperTestRegisterB", PLATFORM_DUMPER_REGISTERB, (void *)g_registerBTest);
269     if (ret != HDF_SUCCESS) {
270         HDF_LOGE("PlatformDumperTestAddRegister: add registerB data fail, ret: %d!", ret);
271         return ret;
272     }
273 
274     return HDF_SUCCESS;
275 }
276 
TestDumperCfgInfo(void)277 static void TestDumperCfgInfo(void)
278 {
279 #ifdef __LITEOS__
280     dprintf("TestDumperCfgInfo\r\n");
281 #else
282     printk("TestDumperCfgInfo\r\n");
283 #endif
284 }
285 
TestDumperStatusInfo(void)286 static void TestDumperStatusInfo(void)
287 {
288 #ifdef __LITEOS__
289     dprintf("TestDumperStatusInfo\r\n");
290 #else
291     printk("TestDumperStatusInfo\r\n");
292 #endif
293 }
294 
TestDumperStatisInfo(void)295 static void TestDumperStatisInfo(void)
296 {
297 #ifdef __LITEOS__
298     dprintf("TestDumperStatisInfo\r\n");
299 #else
300     printk("TestDumperStatisInfo\r\n");
301 #endif
302 }
303 
TestDumperRegisterInfo(void)304 static void TestDumperRegisterInfo(void)
305 {
306 #ifdef __LITEOS__
307     dprintf("TestDumperRegisterInfo\r\n");
308 #else
309     printk("TestDumperRegisterInfo\r\n");
310 #endif
311 }
312 
313 struct PlatformDumperMethod g_ops;
PlatformDumperTestSetOps(void)314 static int32_t PlatformDumperTestSetOps(void)
315 {
316     int32_t ret;
317     g_ops.dumperCfgInfo = TestDumperCfgInfo;
318     g_ops.dumperStatusInfo = TestDumperStatusInfo;
319     g_ops.dumperStatisInfo = TestDumperStatisInfo;
320     g_ops.dumperRegisterInfo = TestDumperRegisterInfo;
321     ret = PlatformDumperSetMethod(g_dumperTest, &g_ops);
322     if (ret != HDF_SUCCESS) {
323         HDF_LOGE("PlatformDumperTestSetOps: set method fail!");
324         return HDF_FAILURE;
325     }
326 
327     return HDF_SUCCESS;
328 }
329 
PlatformDumperTestThreadFunc(void * param)330 static int PlatformDumperTestThreadFunc(void *param)
331 {
332     PlatformDumperTestAddUint8();
333     *((int32_t *)param) = 1;
334     return HDF_SUCCESS;
335 }
336 
PlatformDumperTestStartThread(struct OsalThread * thread1,struct OsalThread * thread2,const int32_t * count1,const int32_t * count2)337 static int32_t PlatformDumperTestStartThread(struct OsalThread *thread1, struct OsalThread *thread2,
338     const int32_t *count1, const int32_t *count2)
339 {
340     int32_t ret;
341     uint32_t time = 0;
342     struct OsalThreadParam cfg1;
343     struct OsalThreadParam cfg2;
344 
345     if (memset_s(&cfg1, sizeof(cfg1), 0, sizeof(cfg1)) != EOK ||
346         memset_s(&cfg2, sizeof(cfg2), 0, sizeof(cfg2)) != EOK) {
347         HDF_LOGE("PlatformDumperTestStartThread: memset_s fail!");
348         return HDF_ERR_IO;
349     }
350     cfg1.name = "DumperTestThread-1";
351     cfg2.name = "DumperTestThread-2";
352     cfg1.priority = cfg2.priority = OSAL_THREAD_PRI_DEFAULT;
353     cfg1.stackSize = cfg2.stackSize = PLAT_DUMPER_TEST_STACK_SIZE;
354 
355     ret = OsalThreadStart(thread1, &cfg1);
356     if (ret != HDF_SUCCESS) {
357         HDF_LOGE("PlatformDumperTestStartThread: start test thread1 fail, ret: %d!", ret);
358         return ret;
359     }
360 
361     ret = OsalThreadStart(thread2, &cfg2);
362     if (ret != HDF_SUCCESS) {
363         HDF_LOGE("PlatformDumperTestStartThread: start test thread2 fail, ret: %d!", ret);
364     }
365 
366     while (*count1 == 0 || *count2 == 0) {
367         HDF_LOGV("PlatformDumperTestStartThread: waitting testing dumper thread finish...");
368         OsalMSleep(PLAT_DUMPER_TEST_WAIT_TIMES);
369         time++;
370         if (time > PLAT_DUMPER_TEST_WAIT_TIMEOUT) {
371             break;
372         }
373     }
374     return ret;
375 }
376 
PlatformDumperTestMultiThread(void)377 static int32_t PlatformDumperTestMultiThread(void)
378 {
379     int32_t ret;
380     struct OsalThread thread1;
381     struct OsalThread thread2;
382     int32_t count1 = 0;
383     int32_t count2 = 0;
384 
385     ret = OsalThreadCreate(&thread1, (OsalThreadEntry)PlatformDumperTestThreadFunc, (void *)&count1);
386     if (ret != HDF_SUCCESS) {
387         HDF_LOGE("PlatformDumperTestMultiThread: create test thread1 fail, ret: %d!", ret);
388         return ret;
389     }
390 
391     ret = OsalThreadCreate(&thread2, (OsalThreadEntry)PlatformDumperTestThreadFunc, (void *)&count2);
392     if (ret != HDF_SUCCESS) {
393         (void)OsalThreadDestroy(&thread1);
394         HDF_LOGE("PlatformDumperTestMultiThread: create test thread2 fail, ret: %d!", ret);
395         return ret;
396     }
397 
398     ret = PlatformDumperTestStartThread(&thread1, &thread2, &count1, &count2);
399     if (ret != HDF_SUCCESS) {
400         HDF_LOGE("PlatformDumperTestMultiThread: test start thread fail, ret: %d!", ret);
401     }
402 
403     (void)OsalThreadDestroy(&thread1);
404     (void)OsalThreadDestroy(&thread2);
405     return ret;
406 }
407 
PlatformDumperTestDelData(void)408 static int32_t PlatformDumperTestDelData(void)
409 {
410     int32_t ret;
411 
412     ret = PlatformDumperDelData(g_dumperTest, "dumperTestUint8", PLATFORM_DUMPER_UINT8);
413     if (ret != HDF_SUCCESS) {
414         HDF_LOGE("PlatformDumperTestDelData: PlatformDumperDelData fail, ret: %d!", ret);
415         return ret;
416     }
417 
418     ret = PlatformDumperDelData(g_dumperTest, "dumperTestUint8", PLATFORM_DUMPER_UINT8);
419     if (ret != HDF_SUCCESS) {
420         HDF_LOGE("PlatformDumperTestDelData: PlatformDumperDelData fail, ret: %d!", ret);
421         return ret;
422     }
423     PlatformDumperDump(g_dumperTest);
424 
425     ret = PlatformDumperClearDatas(g_dumperTest);
426     if (ret != HDF_SUCCESS) {
427         HDF_LOGE("PlatformDumperTestDelData: PlatformDumperClearDatas fail, ret: %d!", ret);
428         return ret;
429     }
430     PlatformDumperDump(g_dumperTest);
431 
432     return HDF_SUCCESS;
433 }
434 
PlatformDumperTestReliability(void)435 static int32_t PlatformDumperTestReliability(void)
436 {
437     PlatformDumperAddData(g_dumperTest, NULL);
438     PlatformDumperAddData(NULL, NULL);
439     PlatformDumperDump(NULL);
440 
441     return HDF_SUCCESS;
442 }
443 
444 static struct PlatformDumperTestEntry g_entry[] = {
445     {PLAT_DUMPER_TEST_ADD_UINT8,      PlatformDumperTestAddUint8   },
446     {PLAT_DUMPER_TEST_ADD_UINT16,     PlatformDumperTestAddUint16  },
447     {PLAT_DUMPER_TEST_ADD_UINT32,     PlatformDumperTestAddUint32  },
448     {PLAT_DUMPER_TEST_ADD_UINT64,     PlatformDumperTestAddUint64  },
449     {PLAT_DUMPER_TEST_ADD_INT8,       PlatformDumperTestAddInt8    },
450     {PLAT_DUMPER_TEST_ADD_INT16,      PlatformDumperTestAddInt16   },
451     {PLAT_DUMPER_TEST_ADD_INT32,      PlatformDumperTestAddInt32   },
452     {PLAT_DUMPER_TEST_ADD_INT64,      PlatformDumperTestAddInt64   },
453     {PLAT_DUMPER_TEST_ADD_FLOAT,      PlatformDumperTestAddFloat   },
454     {PLAT_DUMPER_TEST_ADD_DOUBLE,     PlatformDumperTestAddDouble  },
455     {PLAT_DUMPER_TEST_ADD_CHAR,       PlatformDumperTestAddChar    },
456     {PLAT_DUMPER_TEST_ADD_STRING,     PlatformDumperTestAddString  },
457     {PLAT_DUMPER_TEST_ADD_REGISTER,   PlatformDumperTestAddRegister},
458     {PLAT_DUMPER_TEST_ADD_ARRAY_DATA, PlatformDumperTestAddatas    },
459     {PLAT_DUMPER_TEST_SET_OPS,        PlatformDumperTestSetOps     },
460     {PLAT_DUMPER_TEST_MULTI_THREAD,   PlatformDumperTestMultiThread},
461     {PLAT_DUMPER_TEST_DEL_DATA,       PlatformDumperTestDelData    },
462     {PLAT_DUMPER_TEST_RELIABILITY,    PlatformDumperTestReliability},
463 };
464 
PlatformDumperTestClear(void)465 static void PlatformDumperTestClear(void)
466 {
467     PlatformDumperDestroy(g_dumperTest);
468     g_dumperTest = NULL;
469     if (g_registerLTest != NULL) {
470         OsalIoUnmap((void *)g_registerLTest);
471         g_registerLTest = NULL;
472     }
473     if (g_registerWTest != NULL) {
474         OsalIoUnmap((void *)g_registerWTest);
475         g_registerWTest = NULL;
476     }
477     if (g_registerBTest != NULL) {
478         OsalIoUnmap((void *)g_registerBTest);
479         g_registerBTest = NULL;
480     }
481 }
482 
PlatformDumperTestExecute(int cmd)483 int PlatformDumperTestExecute(int cmd)
484 {
485     uint32_t i;
486     int32_t ret = HDF_ERR_NOT_SUPPORT;
487     struct PlatformDumperTestEntry *entry = NULL;
488 
489     if (cmd > PLAT_DUMPER_TEST_CMD_MAX) {
490         HDF_LOGE("PlatformDumperTestExecute: invalid cmd:%d!", cmd);
491         ret = HDF_ERR_NOT_SUPPORT;
492         HDF_LOGE("[PlatformDumperTestExecute][======cmd:%d====ret:%d======]", cmd, ret);
493         return ret;
494     }
495 
496     if (g_dumperTest == NULL) {
497         g_dumperTest = PlatformDumperCreate(g_dumperTestName);
498         if (g_dumperTest == NULL) {
499             HDF_LOGE("PlatformDumperTestExecute: PlatformDumperCreate fail!");
500             return HDF_FAILURE;
501         }
502     }
503 
504     for (i = 0; i < sizeof(g_entry) / sizeof(g_entry[0]); i++) {
505         if (g_entry[i].cmd != cmd || g_entry[i].func == NULL) {
506             continue;
507         }
508         entry = &g_entry[i];
509         break;
510     }
511 
512     if (entry == NULL) {
513         HDF_LOGE("PlatformDumperTestExecute: no entry matched, cmd = %d!", cmd);
514         return HDF_ERR_NOT_SUPPORT;
515     }
516 
517     ret = entry->func();
518     if (cmd == PLAT_DUMPER_TEST_RELIABILITY) {
519         PlatformDumperTestClear();
520     }
521     HDF_LOGE("[PlatformDumperTestExecute][======cmd:%d====ret:%d======]", cmd, ret);
522     return ret;
523 }
524