[USB] Add bulk, interrupt, isochronous Endpoint classes
[platform/core/csapi/usb.git] / 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
28         internal UsbEndpoint(Interop.UsbEndpointHandle handle)
29         {
30             _handle = handle;
31         }
32
33         /// <summary>
34         /// Gets number of this endpoint
35         /// </summary>
36         public int Id
37         {
38             get
39             {
40                 return Interop.NativeGet<int>(_handle.GetNumber);
41             }
42         }
43
44         /// <summary>
45         /// Gets direction of this endpoint
46         /// </summary>
47         public EndpointDirection Direction
48         {
49             get
50             {
51                 return (EndpointDirection)Interop.NativeGet<Interop.EndpointDirection>(_handle.GetDirection);
52             }
53         }
54
55         /// <summary>
56         /// Gets max packet size of given endpoint
57         /// </summary>
58         public int MaxPacketSize
59         {
60             get
61             {
62                 return Interop.NativeGet<int>(_handle.GetMaxPacketSize);
63             }
64         }
65
66         protected int TransferImpl(byte[] buffer, int length, uint timeout)
67         {
68             int transferred;
69             _handle.Transfer(buffer, length, out transferred, timeout).ThrowIfFailed("Transfer failed");
70             return transferred;
71         }
72
73         internal static UsbEndpoint EndpointFactory(Interop.UsbEndpointHandle handle)
74         {
75             Interop.TransferType transferType;
76             handle.GetTransferType(out transferType).ThrowIfFailed("Failed to get transfer type from endpoint");
77             switch(transferType)
78             {
79                 case Interop.TransferType.Bulk: return new UsbBulkEndpoint(handle);
80                 case Interop.TransferType.Interrupt: return new UsbInterruptEndpoint(handle);
81                 case Interop.TransferType.Isochronous: return new UsbIsochronousEndpoint(handle);
82                 default: return new UsbEndpoint(handle);
83             }
84         }
85     }
86 }