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