Add Tizen.Peripheral API (#1939)
[platform/core/csapi/tizenfx.git] / internals / src / Tizen.Peripheral / Tizen.Peripheral / Uart.cs
1 /*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 using System;
18 using NativeUart = Interop.Peripheral.Uart;
19
20 namespace Tizen.Peripheral.Uart
21 {
22     /// <summary>
23     /// Enumeration for baud rate.
24     /// </summary>
25     public enum BaudRate
26     {
27         /// <summary>The number of signal in one second is 0.</summary>
28         Rate0 = 0,
29
30         /// <summary>The number of signal in one second is 50.</summary>
31         Rate50,
32
33         /// <summary>The number of signal in one second is 75.</summary>
34         Rate75,
35
36         /// <summary>The number of signal in one second is 110.</summary>
37         Rate110,
38
39         /// <summary>The number of signal in one second is 134.</summary>
40         Rate134,
41
42         /// <summary>The number of signal in one second is 150.</summary>
43         Rate150,
44
45         /// <summary>The number of signal in one second is 200.</summary>
46         Rate200,
47
48         /// <summary>The number of signal in one second is 300.</summary>
49         Rate300,
50
51         /// <summary>The number of signal in one second is 600.</summary>
52         Rate600,
53
54         /// <summary>The number of signal in one second is 1200.</summary>
55         Rate1200,
56
57         /// <summary>The number of signal in one second is 1800.</summary>
58         Rate1800,
59
60         /// <summary>The number of signal in one second is 2400.</summary>
61         Rate2400,
62
63         /// <summary>The number of signal in one second is 4800.</summary>
64         Rate4800,
65
66         /// <summary>The number of signal in one second is 9600.</summary>
67         Rate9600,
68
69         /// <summary>The number of signal in one second is 19200.</summary>
70         Rate19200,
71
72         /// <summary>The number of signal in one second is 38400.</summary>
73         Rate38400,
74
75         /// <summary>The number of signal in one second is 57600.</summary>
76         Rate57600,
77
78         /// <summary>The number of signal in one second is 115200.</summary>
79         Rate115200,
80
81         /// <summary>The number of signal in one second is 230400.</summary>
82         Rate230400
83     }
84
85     /// <summary>
86     /// Enumeration for byte size.
87     /// </summary>
88     public enum DataBits
89     {
90         /// <summary>5 data bits.</summary>
91         Size5Bit = 0,
92
93         /// <summary>6 data bits.</summary>
94         Size6Bit,
95
96         /// <summary>7 data bits.</summary>
97         Size7Bit,
98
99         /// <summary>8 data bits.</summary>
100         Size8Bit
101     }
102
103     /// <summary>
104     /// Enumeration for parity bit.
105     /// </summary>
106     public enum Parity
107     {
108         /// <summary>No parity is used.</summary>
109         None = 0,
110
111         /// <summary>Even parity is used.</summary>
112         Even,
113
114         /// <summary>Odd parity is used.</summary>
115         Odd
116     }
117
118     /// <summary>
119     /// Enumeration for stop bits.
120     /// </summary>
121     public enum StopBits
122     {
123         /// <summary>One stop bit.</summary>
124         One = 0,
125
126         /// <summary>Two stop bits.</summary>
127         Two
128     }
129
130     /// <summary>
131     /// Enumeration for hardware flow control.
132     /// </summary>
133     public enum HardwareFlowControl
134     {
135         /// <summary>No hardware flow control.</summary>
136         None = 0,
137
138         /// <summary>Automatic RTS/CTS hardware flow control.</summary>
139         AutoRtsCts
140     }
141
142     /// <summary>
143     /// Enumeration for software flow control.
144     /// </summary>
145     public enum SoftwareFlowControl
146     {
147         /// <summary>No software flow control.</summary>
148         None = 0,
149
150         /// <summary>XON/XOFF software flow control.</summary>
151         XonXoff,
152     }
153
154     /// <summary>
155     /// The class allows applications to communicate via UART platform's bus.
156     /// </summary>
157     /// <privilege>http://tizen.org/privilege/peripheralio</privilege>
158     public class Uart : IDisposable
159     {
160         private BaudRate _baudRate;
161         private DataBits _dataBits;
162         private Parity _parity;
163         private StopBits _stopBits;
164         private HardwareFlowControl _hardwareFlowControl;
165         private SoftwareFlowControl _softwareFlowControl;
166         private bool _disposed = false;
167
168         /// <summary>
169         /// Native handle to I2c.
170         /// </summary>
171         private IntPtr _handle;
172
173         /// <summary>
174         /// Opens the UART slave device.
175         /// </summary>
176         /// <param name="port">The UART port number that the slave device is connected.</param>
177         public Uart(int port)
178         {
179             var ret = NativeUart.Open(port, out IntPtr handle);
180             if (ret != Internals.Errors.ErrorCode.None)
181                 throw ExceptionFactory.CreateException(ret);
182
183             _handle = handle;
184         }
185
186         /// <summary>
187         /// Closes the UART slave device.
188         /// </summary>
189         ~Uart()
190         {
191             Dispose(false);
192         }
193
194         /// <summary>
195         /// Closes the UART slave device.
196         /// </summary>
197         public void Close() => Dispose();
198
199         /// <summary>
200         /// Disposes Uart object.
201         /// </summary>
202         public void Dispose()
203         {
204             Dispose(true);
205             GC.SuppressFinalize(this);
206         }
207
208         /// <summary>
209         /// Disposes Uart object.
210         /// </summary>
211         protected virtual void Dispose(bool disposing)
212         {
213             if (_disposed)
214             {
215                 return;
216             }
217
218             NativeUart.Close(_handle);
219             _disposed = true;
220         }
221
222         /// <summary>
223         /// Reads the bytes data from the UART slave device.
224         /// </summary>
225         /// <param name="buffer">The output byte array.</param>
226         /// <param name="offset">The offset in buffer at which to write the bytes.</param>
227         /// <param name="count">The number of bytes to read.</param>
228         /// <returns>The number of bytes read.</returns>
229         public int Read(byte[] buffer, int offset, int count)
230         {
231             if (count > buffer.Length - offset)
232                 throw new Exception("Can not read more bytes than the buffer can hold.");
233             byte[] tmpBuffer = new byte[count];
234             var length = Convert.ToUInt32(count);
235             var ret = NativeUart.Read(_handle, tmpBuffer, length);
236             if (ret < Internals.Errors.ErrorCode.None)
237                 throw ExceptionFactory.CreateException(ret);
238             Array.Copy(tmpBuffer, 0, buffer, offset, (int)ret);
239             return (int)ret;
240         }
241
242         /// <summary>
243         /// Writes the bytes data to the UART slave device.
244         /// </summary>
245         /// <param name="buffer">The byte array that contains the data to write to the device.</param>
246         /// <param name="offset">The offset in buffer at which to begin copying bytes.</param>
247         /// <param name="count">The number of bytes to write</param>
248         public int Write(byte[] buffer, int offset, int count)
249         {
250             if (count > buffer.Length - offset)
251                 throw new Exception("Can not write more bytes than the buffer holds.");
252             byte[] tmpBuffer = new byte[count];
253             Array.Copy(buffer, offset, tmpBuffer, 0, count);
254             var length = Convert.ToUInt32(count);
255             var ret = NativeUart.Write(_handle, tmpBuffer, length);
256             if (ret < Internals.Errors.ErrorCode.None)
257                 throw ExceptionFactory.CreateException(ret);
258             return (int)ret;
259         }
260
261         /// <summary>
262         /// Sets or gets baud rate of the UART slave device.
263         /// </summary>
264         /// <remarks>Get value is initialized after successful Set call.</remarks>
265         public BaudRate BaudRate
266         {
267             get => _baudRate;
268             set
269             {
270                 var ret = NativeUart.SetBaudRate(_handle, (NativeUart.BaudRate)value);
271                 if (ret != Internals.Errors.ErrorCode.None)
272                     throw ExceptionFactory.CreateException(ret);
273
274                 _baudRate = value;
275             }
276         }
277
278         /// <summary>
279         /// Sets or gets byte size of the UART slave device.
280         /// </summary>
281         /// <remarks>Get value is initialized after successful Set call.</remarks>
282         public DataBits DataBits
283         {
284             get => _dataBits;
285             set
286             {
287                 var ret = NativeUart.SetByteSize(_handle, (NativeUart.ByteSize)value);
288                 if (ret != Internals.Errors.ErrorCode.None)
289                     throw ExceptionFactory.CreateException(ret);
290
291                 _dataBits = value;
292             }
293         }
294
295         /// <summary>
296         /// Sets or gets parity bit of the UART slave device.
297         /// </summary>
298         /// <remarks>Get value is initialized after successful Set call.</remarks>
299         public Parity Parity
300         {
301             get => _parity;
302             set
303             {
304                 var ret = NativeUart.SetParity(_handle, (NativeUart.Parity)value);
305                 if (ret != Internals.Errors.ErrorCode.None)
306                     throw ExceptionFactory.CreateException(ret);
307
308                 _parity = value;
309             }
310         }
311
312         /// <summary>
313         /// Sets or gets stop bits of the UART slave device.
314         /// </summary>
315         /// <remarks>Get value is initialized after successful Set call.</remarks>
316         public StopBits StopBits
317         {
318             get => _stopBits;
319             set
320             {
321                 var ret = NativeUart.SetStopBits(_handle, (NativeUart.StopBits)value);
322                 if (ret != Internals.Errors.ErrorCode.None)
323                     throw ExceptionFactory.CreateException(ret);
324
325                 _stopBits = value;
326             }
327         }
328
329         /// <summary>
330         /// Sets or gets hardware flow control of the UART slave device.
331         /// </summary>
332         /// <remarks>Get value is initialized after successful Set call.</remarks>
333         public HardwareFlowControl HardwareFlowControl
334         {
335             get => _hardwareFlowControl;
336             set
337             {
338                 var ret = NativeUart.SetFlowControl(_handle, (NativeUart.SoftwareFlowControl)_softwareFlowControl, (NativeUart.HardwareFlowControl)value);
339                 if (ret != Internals.Errors.ErrorCode.None)
340                     throw ExceptionFactory.CreateException(ret);
341
342                 _hardwareFlowControl = value;
343             }
344         }
345
346         /// <summary>
347         /// Sets or gets software flow control of the UART slave device.
348         /// </summary>
349         /// <remarks>Get value is initialized after successful Set call.</remarks>
350         public SoftwareFlowControl SoftwareFlowControl
351         {
352             get => _softwareFlowControl;
353             set
354             {
355                 var ret = NativeUart.SetFlowControl(_handle, (NativeUart.SoftwareFlowControl)value, (NativeUart.HardwareFlowControl)_hardwareFlowControl);
356                 if (ret != Internals.Errors.ErrorCode.None)
357                     throw ExceptionFactory.CreateException(ret);
358
359                 _softwareFlowControl = value;
360             }
361         }
362     }
363 }