• 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 #ifndef UART_CORE_H
10 #define UART_CORE_H
11 
12 #include "hdf_base.h"
13 #include "hdf_device_desc.h"
14 #include "hdf_sbuf.h"
15 #include "osal_atomic.h"
16 #include "uart_if.h"
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif /* __cplusplus */
21 
22 /**
23  * @brief uart device operations.
24  */
25 struct UartHost {
26     struct IDeviceIoService service;
27     struct HdfDeviceObject *device;
28     uint32_t num;
29     OsalAtomic atom;
30     void *priv;
31     struct UartHostMethod *method;
32 };
33 
34 struct UartHostMethod {
35     int32_t (*Init)(struct UartHost *host);
36     int32_t (*Deinit)(struct UartHost *host);
37     int32_t (*Read)(struct UartHost *host, uint8_t *data, uint32_t size);
38     int32_t (*Write)(struct UartHost *host, uint8_t *data, uint32_t size);
39     int32_t (*GetBaud)(struct UartHost *host, uint32_t *baudRate);
40     int32_t (*SetBaud)(struct UartHost *host, uint32_t baudRate);
41     int32_t (*GetAttribute)(struct UartHost *host, struct UartAttribute *attribute);
42     int32_t (*SetAttribute)(struct UartHost *host, struct UartAttribute *attribute);
43     int32_t (*SetTransMode)(struct UartHost *host, enum UartTransMode mode);
44     int32_t (*pollEvent)(struct UartHost *host, void *filep, void *table);
45 };
46 
47 struct UartHost *UartHostCreate(struct HdfDeviceObject *device);
48 void UartHostDestroy(struct UartHost *host);
49 
50 /**
51  * @brief Turn UartHost to a HdfDeviceObject.
52  *
53  * @param host Indicates the Uart host device.
54  *
55  * @return Retrns the pointer of the HdfDeviceObject on success; returns NULL otherwise.
56  * @since 1.0
57  */
UartHostToDevice(const struct UartHost * host)58 static inline struct HdfDeviceObject *UartHostToDevice(const struct UartHost *host)
59 {
60     return (host == NULL) ? NULL : host->device;
61 }
62 
63 /**
64  * @brief Turn HdfDeviceObject to an UartHost.
65  *
66  * @param device Indicates a HdfDeviceObject.
67  *
68  * @return Retrns the pointer of the UartHost on success; returns NULL otherwise.
69  * @since 1.0
70  */
UartHostFromDevice(const struct HdfDeviceObject * device)71 static inline struct UartHost *UartHostFromDevice(const struct HdfDeviceObject *device)
72 {
73     return (device == NULL) ? NULL : (struct UartHost *)device->service;
74 }
75 
76 int32_t UartHostRequest(struct UartHost *host);
77 
78 int32_t UartHostRelease(struct UartHost *host);
79 
UartHostRead(struct UartHost * host,uint8_t * data,uint32_t size)80 static inline int32_t UartHostRead(struct UartHost *host, uint8_t *data, uint32_t size)
81 {
82     if (host == NULL || host->method == NULL || host->method->Read == NULL) {
83         return HDF_ERR_NOT_SUPPORT;
84     }
85     return host->method->Read(host, data, size);
86 }
87 
UartHostWrite(struct UartHost * host,uint8_t * data,uint32_t size)88 static inline int32_t UartHostWrite(struct UartHost *host, uint8_t *data, uint32_t size)
89 {
90     if (host == NULL || host->method == NULL || host->method->Write == NULL) {
91         return HDF_ERR_NOT_SUPPORT;
92     }
93     return host->method->Write(host, data, size);
94 }
95 
UartHostGetBaud(struct UartHost * host,uint32_t * baudRate)96 static inline int32_t UartHostGetBaud(struct UartHost *host, uint32_t *baudRate)
97 {
98     if (host == NULL || host->method == NULL || host->method->GetBaud == NULL) {
99         return HDF_ERR_NOT_SUPPORT;
100     }
101     return host->method->GetBaud(host, baudRate);
102 }
103 
UartHostSetBaud(struct UartHost * host,uint32_t baudRate)104 static inline int32_t UartHostSetBaud(struct UartHost *host, uint32_t baudRate)
105 {
106     if (host == NULL || host->method == NULL || host->method->SetBaud == NULL) {
107         return HDF_ERR_NOT_SUPPORT;
108     }
109     return host->method->SetBaud(host, baudRate);
110 }
111 
UartHostGetAttribute(struct UartHost * host,struct UartAttribute * attribute)112 static inline int32_t UartHostGetAttribute(struct UartHost *host, struct UartAttribute *attribute)
113 {
114     if (host == NULL || host->method == NULL || host->method->GetAttribute == NULL) {
115         return HDF_ERR_NOT_SUPPORT;
116     }
117     return host->method->GetAttribute(host, attribute);
118 }
119 
UartHostSetAttribute(struct UartHost * host,struct UartAttribute * attribute)120 static inline int32_t UartHostSetAttribute(struct UartHost *host, struct UartAttribute *attribute)
121 {
122     if (host == NULL || host->method == NULL || host->method->SetAttribute == NULL) {
123         return HDF_ERR_NOT_SUPPORT;
124     }
125     return host->method->SetAttribute(host, attribute);
126 }
127 
UartHostSetTransMode(struct UartHost * host,enum UartTransMode mode)128 static inline int32_t UartHostSetTransMode(struct UartHost *host, enum UartTransMode mode)
129 {
130     if (host == NULL || host->method == NULL || host->method->SetTransMode == NULL) {
131         return HDF_ERR_NOT_SUPPORT;
132     }
133     return host->method->SetTransMode(host, mode);
134 }
135 
UartHostPollEvent(struct UartHost * host,void * filep,void * table)136 static inline int32_t UartHostPollEvent(struct UartHost *host, void *filep, void *table)
137 {
138     if (host == NULL || host->method == NULL || host->method->pollEvent == NULL) {
139         return HDF_ERR_NOT_SUPPORT;
140     }
141     return host->method->pollEvent(host, filep, table);
142 }
143 
144 int32_t UartIoDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply);
145 
146 #ifdef __cplusplus
147 }
148 #endif /* __cplusplus */
149 
150 #endif /* UART_CORE_H */
151