Merge "[NUI]Remove some unused APIs."
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Usb / Usb / UsbEndpoint.cs
1 /*
2  * Copyright (c) 2016 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
19 namespace Tizen.System.Usb
20 {
21     /// <summary>
22     /// USB Endpoint class.
23     /// </summary>
24     /// <since_tizen> 4 </since_tizen>
25     public class UsbEndpoint
26     {
27         internal readonly Interop.UsbEndpointHandle _handle;
28         internal readonly UsbInterface _parent;
29
30         internal UsbEndpoint(UsbInterface parent, Interop.UsbEndpointHandle handle)
31         {
32             _parent = parent;
33             _handle = handle;
34         }
35
36         /// <summary>
37         /// Gets number of this endpoint.
38         /// </summary>
39         /// <since_tizen> 4 </since_tizen>
40         public int Id
41         {
42             get
43             {
44                 ThrowIfDisposed();
45                 return Interop.NativeGet<int>(_handle.GetNumber);
46             }
47         }
48
49         /// <summary>
50         /// Gets direction of this endpoint.
51         /// </summary>
52         /// <since_tizen> 4 </since_tizen>
53         public EndpointDirection Direction
54         {
55             get
56             {
57                 ThrowIfDisposed();
58                 return (EndpointDirection)Interop.NativeGet<Interop.EndpointDirection>(_handle.GetDirection);
59             }
60         }
61
62         /// <summary>
63         /// Gets max packet size of given endpoint.
64         /// </summary>
65         /// <since_tizen> 4 </since_tizen>
66         public int MaxPacketSize
67         {
68             get
69             {
70                 ThrowIfDisposed();
71                 return Interop.NativeGet<int>(_handle.GetMaxPacketSize);
72             }
73         }
74
75         internal void ThrowIfDisposed()
76         {
77             if (_handle == null) throw new InvalidOperationException("Incompatible endpoint handle");
78             _parent?.ThrowIfDisposed();
79         }
80
81         internal int TransferImpl(byte[] buffer, int length, uint timeout)
82         {
83             ThrowIfDisposed();
84             int transferred;
85             _handle.Transfer(buffer, length, out transferred, timeout).ThrowIfFailed("Transfer failed");
86             return transferred;
87         }
88
89         internal static UsbEndpoint EndpointFactory(UsbInterface parent, Interop.UsbEndpointHandle handle)
90         {
91             Interop.TransferType transferType;
92             handle.GetTransferType(out transferType).ThrowIfFailed("Failed to get transfer type from endpoint");
93             switch(transferType)
94             {
95                 case Interop.TransferType.Bulk: return new UsbBulkEndpoint(parent, handle);
96                 case Interop.TransferType.Interrupt: return new UsbInterruptEndpoint(parent, handle);
97                 case Interop.TransferType.Isochronous: return new UsbIsochronousEndpoint(parent, handle);
98                 default: return new UsbEndpoint(parent, handle);
99             }
100         }
101     }
102 }