• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2022 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 "uart_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 "securec.h"
16 #include "uart_if.h"
17 
18 #define HDF_LOG_TAG uart_test
19 
UartTestGetConfig(struct UartTestConfig * config)20 static int32_t UartTestGetConfig(struct UartTestConfig *config)
21 {
22     int32_t ret;
23     struct HdfSBuf *reply = NULL;
24     struct HdfIoService *service = NULL;
25     struct UartTestConfig *cfg;
26     const void *buf = NULL;
27     uint32_t len;
28 
29     service = HdfIoServiceBind("UART_TEST");
30     if (service == NULL) {
31         HDF_LOGE("%s: failed to bind service", __func__);
32         return HDF_ERR_NOT_SUPPORT;
33     }
34 
35     reply = HdfSbufObtainDefaultSize();
36     if (reply == NULL) {
37         HDF_LOGE("%s: Failed to obtain reply", __func__);
38         return HDF_ERR_MALLOC_FAIL;
39     }
40 
41     ret = service->dispatcher->Dispatch(&service->object, 0, NULL, reply);
42     if (ret != HDF_SUCCESS) {
43         HDF_LOGE("%s: Remote dispatch failed", __func__);
44         HdfSbufRecycle(reply);
45         return ret;
46     }
47 
48     if (!HdfSbufReadBuffer(reply, (const void **)&cfg, &len)) {
49         HDF_LOGE("%s: Read buf failed", __func__);
50         HdfSbufRecycle(reply);
51         return HDF_ERR_IO;
52     }
53     if (len != sizeof(*cfg)) {
54         HDF_LOGE("%s: cfg size:%zu, read size:%u", __func__, sizeof(*cfg), len);
55         HdfSbufRecycle(reply);
56         return HDF_ERR_IO;
57     }
58     if (memcpy_s(config, sizeof(*config), cfg, sizeof(*cfg)) != EOK) {
59         HDF_LOGE("%s: Memcpy buf failed", __func__);
60         HdfSbufRecycle(reply);
61         return HDF_ERR_IO;
62     }
63 
64     if (!HdfSbufReadBuffer(reply, (const void **)&buf, &len)) {
65         HDF_LOGE("%s: Read buf failed", __func__);
66         HdfSbufRecycle(reply);
67         return HDF_ERR_IO;
68     }
69 
70     if (len != config->len) {
71         HDF_LOGE("%s: cfg size:%zu, read size:%u", __func__, sizeof(*cfg), len);
72         HdfSbufRecycle(reply);
73         return HDF_ERR_IO;
74     }
75     config->wbuf = NULL;
76     config->wbuf = (uint8_t *)OsalMemCalloc(len);
77     if (config->wbuf == NULL) {
78         HDF_LOGE("%s: malloc wbuf failed", __func__);
79         HdfSbufRecycle(reply);
80         return HDF_ERR_MALLOC_FAIL;
81     }
82 
83     if (memcpy_s(config->wbuf, config->len, buf, len) != EOK) {
84         HDF_LOGE("%s: Memcpy wbuf failed", __func__);
85         HdfSbufRecycle(reply);
86         return HDF_ERR_IO;
87     }
88 
89     HdfSbufRecycle(reply);
90     HDF_LOGD("%s: Done", __func__);
91     HdfIoServiceRecycle(service);
92     return HDF_SUCCESS;
93 }
94 
UartTesterGet(void)95 struct UartTester *UartTesterGet(void)
96 {
97     int32_t ret;
98     static struct UartTester tester;
99 
100     ret = UartTestGetConfig(&tester.config);
101     if (ret != HDF_SUCCESS) {
102         HDF_LOGE("%s: read config failed:%d", __func__, ret);
103         return NULL;
104     }
105     tester.handle = UartOpen(tester.config.port);
106     if (tester.handle == NULL) {
107         HDF_LOGE("%s: open uart port:%u fail!", __func__, tester.config.port);
108         return NULL;
109     }
110     return &tester;
111 }
112 
UartTesterPut(struct UartTester * tester)113 static void UartTesterPut(struct UartTester *tester)
114 {
115     if (tester == NULL || tester->handle == NULL) {
116         HDF_LOGE("%s: uart handle is null", __func__);
117         return;
118     }
119     UartClose(tester->handle);
120     tester->handle = NULL;
121 }
122 
UartWriteTest(struct UartTester * tester)123 static int32_t UartWriteTest(struct UartTester *tester)
124 {
125     int32_t ret;
126 
127     ret = UartWrite(tester->handle, tester->config.wbuf, tester->config.len);
128     HDF_LOGE("%s: len is %d", __func__, tester->config.len);
129     if (ret != HDF_SUCCESS) {
130         HDF_LOGE("%s: write failed", __func__);
131         return HDF_FAILURE;
132     }
133     HDF_LOGD("%s: success", __func__);
134     return HDF_SUCCESS;
135 }
136 
UartReadTest(struct UartTester * tester)137 static int32_t UartReadTest(struct UartTester *tester)
138 {
139     int32_t ret;
140 
141     ret = UartSetTransMode(tester->handle, UART_MODE_RD_NONBLOCK);
142     if (ret != HDF_SUCCESS) {
143         HDF_LOGE("%s: transmode error.", __func__);
144         return HDF_FAILURE;
145     }
146     ret = UartRead(tester->handle, tester->config.rbuf, tester->config.len);
147     if (ret != HDF_SUCCESS) {
148         HDF_LOGE("%s: read failed", __func__);
149         return HDF_FAILURE;
150     }
151     HDF_LOGD("%s: success", __func__);
152     return HDF_SUCCESS;
153 }
154 
155 #define BAUD_921600 921600
UartSetBaudTest(struct UartTester * tester)156 static int32_t UartSetBaudTest(struct UartTester *tester)
157 {
158     int32_t ret;
159 
160     ret = UartSetBaud(tester->handle, BAUD_921600);
161     if (ret != HDF_SUCCESS) {
162         HDF_LOGE("%s: set baud failed", __func__);
163         return HDF_FAILURE;
164     }
165     HDF_LOGD("%s: success", __func__);
166     return HDF_SUCCESS;
167 }
168 
UartGetBaudTest(struct UartTester * tester)169 static int32_t UartGetBaudTest(struct UartTester *tester)
170 {
171     int32_t ret;
172     uint32_t baud;
173 
174     ret = UartGetBaud(tester->handle, &baud);
175     if (ret != HDF_SUCCESS) {
176         HDF_LOGE("%s: get baud failed", __func__);
177         return HDF_FAILURE;
178     }
179     HDF_LOGD("%s: baud %u success", __func__, baud);
180     return HDF_SUCCESS;
181 }
182 
UartSetAttributeTest(struct UartTester * tester)183 static int32_t UartSetAttributeTest(struct UartTester *tester)
184 {
185     struct UartAttribute attribute;
186     int32_t ret;
187 
188     attribute.dataBits = UART_ATTR_DATABIT_7;
189     attribute.parity = UART_ATTR_PARITY_NONE;
190     attribute.stopBits = UART_ATTR_STOPBIT_1;
191     attribute.rts = UART_ATTR_RTS_DIS;
192     attribute.cts = UART_ATTR_CTS_DIS;
193     attribute.fifoRxEn = UART_ATTR_RX_FIFO_EN;
194     attribute.fifoTxEn = UART_ATTR_TX_FIFO_EN;
195     ret = UartSetAttribute(tester->handle, &attribute);
196     if (ret != HDF_SUCCESS) {
197         HDF_LOGE("%s: set attribute failed", __func__);
198         return HDF_FAILURE;
199     }
200     HDF_LOGD("%s: success", __func__);
201     return HDF_SUCCESS;
202 }
203 
UartGetAttributeTest(struct UartTester * tester)204 static int32_t UartGetAttributeTest(struct UartTester *tester)
205 {
206     struct UartAttribute attribute;
207     int32_t ret;
208 
209     ret = UartGetAttribute(tester->handle, &attribute);
210     if (ret != HDF_SUCCESS) {
211         HDF_LOGE("%s: get attribute failed", __func__);
212         return HDF_FAILURE;
213     }
214     HDF_LOGD("dataBits %u", attribute.dataBits);
215     HDF_LOGD("parity %u", attribute.parity);
216     HDF_LOGD("stopBits %u", attribute.stopBits);
217     HDF_LOGD("rts %u", attribute.rts);
218     HDF_LOGD("cts %u", attribute.cts);
219     HDF_LOGD("fifoRxEn %u", attribute.fifoRxEn);
220     HDF_LOGD("fifoTxEn %u", attribute.fifoTxEn);
221     HDF_LOGD("%s: success", __func__);
222     return HDF_SUCCESS;
223 }
224 
UartSetTransModeTest(struct UartTester * tester)225 static int32_t UartSetTransModeTest(struct UartTester *tester)
226 {
227     int32_t ret;
228 
229     ret = UartSetTransMode(tester->handle, UART_MODE_RD_NONBLOCK);
230     if (ret != HDF_SUCCESS) {
231         HDF_LOGE("%s: set transmode failed", __func__);
232         return HDF_FAILURE;
233     }
234     HDF_LOGD("%s: success", __func__);
235     return HDF_SUCCESS;
236 }
237 
UartReliabilityTest(struct UartTester * tester)238 static int32_t UartReliabilityTest(struct UartTester *tester)
239 {
240     uint32_t baud;
241     struct UartAttribute attribute = {0};
242 
243     (void)UartSetTransMode(tester->handle, UART_MODE_RD_NONBLOCK);
244     (void)UartSetTransMode(tester->handle, -1);
245     (void)UartWrite(tester->handle, tester->config.wbuf, tester->config.len);
246     (void)UartWrite(tester->handle, NULL, -1);
247     (void)UartRead(tester->handle, tester->config.rbuf, tester->config.len);
248     (void)UartRead(tester->handle, NULL, -1);
249     (void)UartSetBaud(tester->handle, BAUD_921600);
250     (void)UartSetBaud(tester->handle, -1);
251     (void)UartGetBaud(tester->handle, &baud);
252     (void)UartGetBaud(tester->handle, NULL);
253     (void)UartSetAttribute(tester->handle, &attribute);
254     (void)UartSetAttribute(tester->handle, NULL);
255     (void)UartGetAttribute(tester->handle, &attribute);
256     (void)UartGetAttribute(tester->handle, NULL);
257     HDF_LOGD("%s: success", __func__);
258     return HDF_SUCCESS;
259 }
260 
UartIfPerformanceTest(struct UartTester * tester)261 static int32_t UartIfPerformanceTest(struct UartTester *tester)
262 {
263 #ifdef __LITEOS__
264     if (tester == NULL) {
265         return HDF_FAILURE;
266     }
267     return HDF_SUCCESS;
268 #endif
269     uint32_t baudRate;
270     uint64_t startMs;
271     uint64_t endMs;
272     uint64_t useTime;    // ms
273 
274     startMs = OsalGetSysTimeMs();
275     UartGetBaud(tester->handle, &baudRate);
276     endMs = OsalGetSysTimeMs();
277 
278     useTime = endMs - startMs;
279     HDF_LOGI("----->interface performance test:[start:%lld(ms) - end:%lld(ms) = %lld (ms)] < 1ms[%d]\r\n",
280         startMs, endMs, useTime, useTime < 1 ? true : false);
281     return HDF_SUCCESS;
282 }
283 
284 struct UartTestEntry {
285     int cmd;
286     int32_t (*func)(struct UartTester *tester);
287     const char *name;
288 };
289 
290 static struct UartTestEntry g_entry[] = {
291     { UART_TEST_CMD_WRITE, UartWriteTest, "UartWriteTest" },
292     { UART_TEST_CMD_READ, UartReadTest, "UartReadTest" },
293     { UART_TEST_CMD_SET_BAUD, UartSetBaudTest, "UartSetBaudTest" },
294     { UART_TEST_CMD_GET_BAUD, UartGetBaudTest, "UartGetBaudTest" },
295     { UART_TEST_CMD_SET_ATTRIBUTE, UartSetAttributeTest, "UartSetAttributeTest" },
296     { UART_TEST_CMD_GET_ATTRIBUTE, UartGetAttributeTest, "UartGetAttributeTest" },
297     { UART_TEST_CMD_SET_TRANSMODE, UartSetTransModeTest, "UartSetTransModeTest" },
298     { UART_TEST_CMD_RELIABILITY, UartReliabilityTest, "UartReliabilityTest" },
299     { UART_TEST_CMD_PERFORMANCE, UartIfPerformanceTest, "UartIfPerformanceTest" },
300 };
301 
UartTestExecute(int cmd)302 int32_t UartTestExecute(int cmd)
303 {
304     uint32_t i;
305     int32_t ret = HDF_ERR_NOT_SUPPORT;
306     struct UartTester *tester = NULL;
307 
308     tester = UartTesterGet();
309     if (tester == NULL) {
310         HDF_LOGE("%s: tester is null", __func__);
311         return HDF_ERR_INVALID_OBJECT;
312     }
313 
314     if (cmd > UART_TEST_CMD_MAX) {
315         HDF_LOGE("%s: invalid cmd:%d", __func__, cmd);
316         ret = HDF_ERR_NOT_SUPPORT;
317         HDF_LOGE("[%s][======cmd:%d====ret:%d======]", __func__, cmd, ret);
318         UartTesterPut(tester);
319         return ret;
320     }
321 
322     for (i = 0; i < sizeof(g_entry) / sizeof(g_entry[0]); i++) {
323         if (g_entry[i].cmd != cmd || g_entry[i].func == NULL) {
324             continue;
325         }
326         ret = g_entry[i].func(tester);
327         break;
328     }
329 
330     HDF_LOGE("[%s][======cmd:%d====ret:%d======]", __func__, cmd, ret);
331     UartTesterPut(tester);
332     return ret;
333 }
334