• 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_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("%s: snprintf_s failed", __func__);
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         return;
37     }
38 }
39 
UartOpen(uint32_t port)40 DevHandle UartOpen(uint32_t port)
41 {
42     int32_t ret;
43     void *handle = NULL;
44 
45     handle = UartGetObjGetByBusNum(port);
46     if (handle == NULL) {
47         HDF_LOGE("%s: get handle error", __func__);
48         return NULL;
49     }
50     ret = UartHostRequest((struct UartHost *)handle);
51     if (ret != HDF_SUCCESS) {
52         HDF_LOGE("%s: UartHostRequest error, ret %d", __func__, ret);
53         UartPutObjByPointer(handle);
54         return NULL;
55     }
56     return (DevHandle)handle;
57 }
58 
UartClose(DevHandle handle)59 void UartClose(DevHandle handle)
60 {
61     int32_t ret;
62     if (handle == NULL) {
63         HDF_LOGE("%s: handle is NULL", __func__);
64         return;
65     }
66     ret = UartHostRelease((struct UartHost *)handle);
67     if (ret != HDF_SUCCESS) {
68         HDF_LOGE("%s: UartHostRelease error, ret %d", __func__, ret);
69     }
70     UartPutObjByPointer(handle);
71 }
72 
UartRead(DevHandle handle,uint8_t * data,uint32_t size)73 int32_t UartRead(DevHandle handle, uint8_t *data, uint32_t size)
74 {
75     return UartHostRead((struct UartHost *)handle, data, size);
76 }
77 
UartWrite(DevHandle handle,uint8_t * data,uint32_t size)78 int32_t UartWrite(DevHandle handle, uint8_t *data, uint32_t size)
79 {
80     return UartHostWrite((struct UartHost *)handle, data, size);
81 }
82 
UartGetBaud(DevHandle handle,uint32_t * baudRate)83 int32_t UartGetBaud(DevHandle handle, uint32_t *baudRate)
84 {
85     return UartHostGetBaud((struct UartHost *)handle, baudRate);
86 }
87 
UartSetBaud(DevHandle handle,uint32_t baudRate)88 int32_t UartSetBaud(DevHandle handle, uint32_t baudRate)
89 {
90     return UartHostSetBaud((struct UartHost *)handle, baudRate);
91 }
92 
UartGetAttribute(DevHandle handle,struct UartAttribute * attribute)93 int32_t UartGetAttribute(DevHandle handle, struct UartAttribute *attribute)
94 {
95     return UartHostGetAttribute((struct UartHost *)handle, attribute);
96 }
97 
UartSetAttribute(DevHandle handle,struct UartAttribute * attribute)98 int32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute)
99 {
100     return UartHostSetAttribute((struct UartHost *)handle, attribute);
101 }
102 
UartSetTransMode(DevHandle handle,enum UartTransMode mode)103 int32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode)
104 {
105     return UartHostSetTransMode((struct UartHost *)handle, mode);
106 }
107