[TCSACR-93] Add USB Host C# API
[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     public class UsbEndpoint
25     {
26         internal readonly Interop.UsbEndpointHandle _handle;
27         protected readonly UsbInterface _parent;
28
29         internal UsbEndpoint(UsbInterface parent, Interop.UsbEndpointHandle handle)
30         {
31             _parent = parent;
32             _handle = handle;
33         }
34
35         /// <summary>
36         /// Gets number of this endpoint.
37         /// </summary>
38         public int Id
39         {
40             get
41             {
42                 ThrowIfDisposed();
43                 return Interop.NativeGet<int>(_handle.GetNumber);
44             }
45         }
46
47         /// <summary>
48         /// Gets direction of this endpoint.
49         /// </summary>
50         public EndpointDirection Direction
51         {
52             get
53             {
54                 ThrowIfDisposed();
55                 return (EndpointDirection)Interop.NativeGet<Interop.EndpointDirection>(_handle.GetDirection);
56             }
57         }
58
59         /// <summary>
60         /// Gets max packet size of given endpoint.
61         /// </summary>
62         public int MaxPacketSize
63         {
64             get
65             {
66                 ThrowIfDisposed();
67                 return Interop.NativeGet<int>(_handle.GetMaxPacketSize);
68             }
69         }
70
71         internal void ThrowIfDisposed()
72         {
73             if (_handle == null) throw new InvalidOperationException("Incompatible endpoint handle");
74             _parent?.ThrowIfDisposed();
75         }
76
77         protected int TransferImpl(byte[] buffer, int length, uint timeout)
78         {
79             ThrowIfDisposed();
80             int transferred;
81             _handle.Transfer(buffer, length, out transferred, timeout).ThrowIfFailed("Transfer failed");
82             return transferred;
83         }
84
85         internal static UsbEndpoint EndpointFactory(UsbInterface parent, Interop.UsbEndpointHandle handle)
86         {
87             Interop.TransferType transferType;
88             handle.GetTransferType(out transferType).ThrowIfFailed("Failed to get transfer type from endpoint");
89             switch(transferType)
90             {
91                 case Interop.TransferType.Bulk: return new UsbBulkEndpoint(parent, handle);
92                 case Interop.TransferType.Interrupt: return new UsbInterruptEndpoint(parent, handle);
93                 case Interop.TransferType.Isochronous: return new UsbIsochronousEndpoint(parent, handle);
94                 default: return new UsbEndpoint(parent, handle);
95             }
96         }
97     }
98 }