8f5d12ee4dc6ba40d5823b7b5b49c84462a6e2db
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Usb / Usb / UsbControlEndpoint.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 Control Endpoint class.
23     /// </summary>
24     /// <since_tizen> 4 </since_tizen>
25     public class UsbControlEndpoint : UsbEndpoint
26     {
27         private readonly UsbDevice _device;
28
29         internal UsbControlEndpoint(UsbDevice device) : base(null, null)
30         {
31             _device = device;
32         }
33
34         /// <summary>
35         /// Gets number of this endpoint.
36         /// </summary>
37         /// <feature>http://tizen.org/feature/usb.host</feature>
38         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
39         /// <since_tizen> 4 </since_tizen>
40         public new int Id
41         {
42             get
43             {
44                 return 0;
45             }
46         }
47
48         /// <summary>
49         /// Gets direction of this endpoint.
50         /// </summary>
51         /// <feature>http://tizen.org/feature/usb.host</feature>
52         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
53         /// <since_tizen> 4 </since_tizen>
54         public new EndpointDirection Direction
55         {
56             get
57             {
58                 return EndpointDirection.InOut;
59             }
60         }
61
62         /// <summary>
63         /// Gets max packet size of given endpoint.
64         /// </summary>
65         /// <feature>http://tizen.org/feature/usb.host</feature>
66         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
67         /// <since_tizen> 4 </since_tizen>
68         public new int MaxPacketSize
69         {
70             get
71             {
72                 _device.ThrowIfDisposed();
73                 return Interop.NativeGet<int>(_device._handle.GetMaxPacketSize0);
74             }
75         }
76
77         /// <summary>
78         /// Performs a control transaction on endpoint zero for this device.
79         /// </summary>
80         /// <param name="requestType">bmRequestType type field for the setup packet.</param>
81         /// <param name="request">bRequest field for the setup packet.</param>
82         /// <param name="value">wValue field for the setup packet.</param>
83         /// <param name="index">wIndex field for the setup packet.</param>
84         /// <param name="data">Suitably-sized data buffer for either input or output, (depending on direction bits within bmRequestType).</param>
85         /// <param name="length">wLength field for the setup packet. The data buffer should be at least this size.</param>
86         /// <param name="timeout">
87         /// Time (in milliseconds) that this function should wait for, before giving up due to no response being received
88         /// (for an unlimited timeout 0 value should be used).
89         /// </param>
90         /// <returns>Transferred Number of transferred bytes.</returns>
91         /// <feature>http://tizen.org/feature/usb.host</feature>
92         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
93         /// <exception cref="InvalidOperationException">Throws exception if device is disconnected or not opened for operation.</exception>
94         /// <exception cref="TimeoutException">Throws exception if transfer timed-out.</exception>
95         /// <since_tizen> 4 </since_tizen>
96         public int Transfer(byte requestType, byte request, ushort value, ushort index, byte[] data, ushort length, uint timeout)
97         {
98             _device.ThrowIfDisposed();
99             if (_device.IsOpened == false) throw new InvalidOperationException("Device must be opened for operation first");
100
101             int transferred;
102             var err = _device._handle.ControlTransfer(requestType, request, value, index, data, length, timeout, out transferred);
103             err.ThrowIfFailed("Failed during control transfer");
104             return transferred;
105         }
106     }
107 }