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