1 /*
2 * Copyright (c) 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 "device_resource_if.h"
11 #include "hdf_base.h"
12 #include "hdf_device_desc.h"
13 #include "hdf_log.h"
14 #include "osal_mem.h"
15
16 static struct UartTestConfig g_config;
17
UartTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)18 static int32_t UartTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
19 {
20 if (cmd == 0) {
21 if (reply == NULL) {
22 HDF_LOGE("%s: reply is null!", __func__);
23 return HDF_ERR_INVALID_PARAM;
24 }
25 if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
26 HDF_LOGE("%s: write reply failed", __func__);
27 return HDF_ERR_IO;
28 }
29 HDF_LOGE("%s: g_config.len is %d ", __func__, g_config.len);
30 if (!HdfSbufWriteBuffer(reply, g_config.wbuf, g_config.len)) {
31 HDF_LOGE("%s: write config wbuf failed", __func__);
32 return HDF_ERR_IO;
33 }
34 } else {
35 HDF_LOGE("%s: cmd is not support", __func__);
36 return HDF_ERR_NOT_SUPPORT;
37 }
38 return HDF_SUCCESS;
39 }
40
UartTestReadConfig(struct UartTestConfig * config,const struct DeviceResourceNode * node)41 static int32_t UartTestReadConfig(struct UartTestConfig *config, const struct DeviceResourceNode *node)
42 {
43 int32_t ret;
44 int32_t i;
45 uint32_t *tmp = NULL;
46 struct DeviceResourceIface *drsOps = NULL;
47
48 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
49 if (drsOps == NULL || drsOps->GetUint32 == NULL || drsOps->GetUint32Array == NULL) {
50 HDF_LOGE("%s: invalid drs ops fail!", __func__);
51 return HDF_FAILURE;
52 }
53 ret = drsOps->GetUint32(node, "port", &config->port, 0);
54 if (ret != HDF_SUCCESS) {
55 HDF_LOGE("%s: read port fail", __func__);
56 return HDF_FAILURE;
57 }
58 ret = drsOps->GetUint32(node, "len", &config->len, 0);
59 if (ret != HDF_SUCCESS) {
60 HDF_LOGE("%s: read len fail", __func__);
61 return HDF_FAILURE;
62 }
63 config->wbuf = (uint8_t *)OsalMemCalloc(config->len);
64 if (config->wbuf == NULL) {
65 HDF_LOGE("%s: wbuf OsalMemCalloc error\n", __func__);
66 return HDF_ERR_MALLOC_FAIL;
67 }
68 tmp = (uint32_t *)OsalMemCalloc(config->len * sizeof(uint32_t));
69 if (tmp == NULL) {
70 HDF_LOGE("%s: tmp OsalMemCalloc error\n", __func__);
71 OsalMemFree(config->wbuf);
72 return HDF_ERR_MALLOC_FAIL;
73 }
74 ret = drsOps->GetUint32Array(node, "wbuf", tmp, config->len, 0);
75 if (ret != HDF_SUCCESS) {
76 HDF_LOGE("%s: read wbuf fail\n", __func__);
77 OsalMemFree(config->wbuf);
78 OsalMemFree(tmp);
79 return HDF_FAILURE;
80 }
81 for (i = 0; i < config->len; i++) {
82 config->wbuf[i] = tmp[i];
83 }
84 OsalMemFree(tmp);
85 config->rbuf = (uint8_t *)OsalMemCalloc(config->len);
86 if (config->rbuf == NULL) {
87 HDF_LOGE("%s: rbuf OsalMemCalloc error\n", __func__);
88 OsalMemFree(config->wbuf);
89 return HDF_ERR_MALLOC_FAIL;
90 }
91 return HDF_SUCCESS;
92 }
93
UartTestBind(struct HdfDeviceObject * device)94 static int32_t UartTestBind(struct HdfDeviceObject *device)
95 {
96 int32_t ret;
97 static struct IDeviceIoService service;
98
99 if (device == NULL || device->property == NULL) {
100 HDF_LOGE("%s: device or config is null!", __func__);
101 return HDF_ERR_IO;
102 }
103
104 ret = UartTestReadConfig(&g_config, device->property);
105 if (ret != HDF_SUCCESS) {
106 HDF_LOGE("%s: read config failed", __func__);
107 return ret;
108 }
109
110 service.Dispatch = UartTestDispatch;
111 device->service = &service;
112
113 return HDF_SUCCESS;
114 }
115
UartTestInit(struct HdfDeviceObject * device)116 static int32_t UartTestInit(struct HdfDeviceObject *device)
117 {
118 (void)device;
119 return HDF_SUCCESS;
120 }
121
UartTestRelease(struct HdfDeviceObject * device)122 static void UartTestRelease(struct HdfDeviceObject *device)
123 {
124 if (device != NULL) {
125 device->service = NULL;
126 }
127 return;
128 }
129
130 struct HdfDriverEntry g_uartTestEntry = {
131 .moduleVersion = 1,
132 .Bind = UartTestBind,
133 .Init = UartTestInit,
134 .Release = UartTestRelease,
135 .moduleName = "PLATFORM_UART_TEST",
136 };
137 HDF_INIT(g_uartTestEntry);
138