1 /*
2 * Copyright (c) 2020-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_if.h"
10 #include "devsvc_manager_clnt.h"
11 #include "hdf_log.h"
12 #include "osal_mem.h"
13 #include "securec.h"
14 #include "uart_core.h"
15
16 #define HDF_LOG_TAG uart_if_c
17 #define UART_HOST_NAME_LEN 32
18
UartGetObjGetByBusNum(uint32_t num)19 static void *UartGetObjGetByBusNum(uint32_t num)
20 {
21 int ret;
22 char name[UART_HOST_NAME_LEN + 1] = {0};
23
24 ret = snprintf_s(name, UART_HOST_NAME_LEN + 1, UART_HOST_NAME_LEN, "HDF_PLATFORM_UART_%u", num);
25 if (ret < 0) {
26 HDF_LOGE("UartGetObjGetByBusNum: snprintf_s fail!");
27 return NULL;
28 }
29
30 return (void *)DevSvcManagerClntGetService(name);
31 }
32
UartPutObjByPointer(const void * obj)33 static void UartPutObjByPointer(const void *obj)
34 {
35 if (obj == NULL) {
36 HDF_LOGE("UartPutObjByPointer: obj is null!");
37 return;
38 }
39 }
40
UartOpen(uint32_t port)41 DevHandle UartOpen(uint32_t port)
42 {
43 int32_t ret;
44 void *handle = NULL;
45
46 handle = UartGetObjGetByBusNum(port);
47 if (handle == NULL) {
48 HDF_LOGE("UartOpen: get handle error!");
49 return NULL;
50 }
51 ret = UartHostRequest((struct UartHost *)handle);
52 if (ret != HDF_SUCCESS) {
53 HDF_LOGE("UartOpen: uart host request error, ret: %d!", ret);
54 UartPutObjByPointer(handle);
55 return NULL;
56 }
57 return (DevHandle)handle;
58 }
59
UartClose(DevHandle handle)60 void UartClose(DevHandle handle)
61 {
62 int32_t ret;
63 if (handle == NULL) {
64 HDF_LOGE("UartClose: handle is null!");
65 return;
66 }
67 ret = UartHostRelease((struct UartHost *)handle);
68 if (ret != HDF_SUCCESS) {
69 HDF_LOGE("UartClose: uart host release error, ret: %d!", ret);
70 }
71 UartPutObjByPointer(handle);
72 }
73
UartRead(DevHandle handle,uint8_t * data,uint32_t size)74 int32_t UartRead(DevHandle handle, uint8_t *data, uint32_t size)
75 {
76 return UartHostRead((struct UartHost *)handle, data, size);
77 }
78
UartWrite(DevHandle handle,uint8_t * data,uint32_t size)79 int32_t UartWrite(DevHandle handle, uint8_t *data, uint32_t size)
80 {
81 return UartHostWrite((struct UartHost *)handle, data, size);
82 }
83
UartGetBaud(DevHandle handle,uint32_t * baudRate)84 int32_t UartGetBaud(DevHandle handle, uint32_t *baudRate)
85 {
86 return UartHostGetBaud((struct UartHost *)handle, baudRate);
87 }
88
UartSetBaud(DevHandle handle,uint32_t baudRate)89 int32_t UartSetBaud(DevHandle handle, uint32_t baudRate)
90 {
91 return UartHostSetBaud((struct UartHost *)handle, baudRate);
92 }
93
UartGetAttribute(DevHandle handle,struct UartAttribute * attribute)94 int32_t UartGetAttribute(DevHandle handle, struct UartAttribute *attribute)
95 {
96 return UartHostGetAttribute((struct UartHost *)handle, attribute);
97 }
98
UartSetAttribute(DevHandle handle,struct UartAttribute * attribute)99 int32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute)
100 {
101 return UartHostSetAttribute((struct UartHost *)handle, attribute);
102 }
103
UartSetTransMode(DevHandle handle,enum UartTransMode mode)104 int32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode)
105 {
106 return UartHostSetTransMode((struct UartHost *)handle, mode);
107 }
108