Skip to content

Commit ee94571

Browse files
committed
Basic USB Host code samples
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
1 parent 93f601c commit ee94571

File tree

7 files changed

+104
-1
lines changed

7 files changed

+104
-1
lines changed

src/Tizen.System.Usb/Usb/UsbBulkEndpoint.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ internal UsbBulkEndpoint(UsbInterface parent, Interop.UsbEndpointHandle handle)
4545
/// <exception cref="InvalidOperationException">Throws an exception if the device is disconnected or not opened for an operation.</exception>
4646
/// <exception cref="TimeoutException">Throws an exception if the transfer is timed out.</exception>
4747
/// <since_tizen> 4 </since_tizen>
48+
/// <example>
49+
/// <code>
50+
/// byte[] buffer = new byte[123];
51+
/// int bytesTransferred = endpoint.Transfer(buffer, buffer.Length, 1000);
52+
/// </code>
53+
/// </example>
4854
public int Transfer(byte[] buffer, int length, uint timeout)
4955
{
5056
return TransferImpl(buffer, length, timeout);

src/Tizen.System.Usb/Usb/UsbConfiguration.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ public int MaximumPowerRequired
8787
/// <feature>http://tizen.org/feature/usb.host</feature>
8888
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
8989
/// <since_tizen> 4 </since_tizen>
90+
/// <example>
91+
/// <code>
92+
/// var specificIface = device.ActiveConfiguration.Interfaces[123];
93+
/// foreach (var iface in device.ActiveConfiguration.Interfaces.Where(...)) ...
94+
/// </code>
95+
/// </example>
9096
public IReadOnlyDictionary<int, UsbInterface> Interfaces
9197
{
9298
get
@@ -136,6 +142,12 @@ public string ConfigurationString
136142
/// Throws an exception if the device is disconnected, or not opened for an operation, or busy as its interfaces are currently claimed.
137143
/// </exception>
138144
/// <since_tizen> 4 </since_tizen>
145+
/// <example>
146+
/// <code>
147+
/// device.Configurations[123].SetAsActive();
148+
/// DoSomeWorkNow(device.ActiveConfiguration);
149+
/// </code>
150+
/// </example>
139151
public void SetAsActive()
140152
{
141153
ThrowIfDisposed();
@@ -190,4 +202,4 @@ public void Dispose()
190202
}
191203
#endregion
192204
}
193-
}
205+
}

src/Tizen.System.Usb/Usb/UsbControlEndpoint.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ internal UsbControlEndpoint(UsbDevice device) : base(null, null)
9393
/// <exception cref="InvalidOperationException">Throws an exception if the device is disconnected or not opened for an operation.</exception>
9494
/// <exception cref="TimeoutException">Throws an exception if the transfer is timed out.</exception>
9595
/// <since_tizen> 4 </since_tizen>
96+
/// <example>
97+
/// <code>
98+
/// byte[] buffer = new byte[123];
99+
/// int bytesTransferred = endpoint.Transfer(0x12, 0x34, 0x56, 7, buffer, buffer.Length, 1000);
100+
/// </code>
101+
/// </example>
96102
public int Transfer(byte requestType, byte request, ushort value, ushort index, byte[] data, ushort length, uint timeout)
97103
{
98104
_device.ThrowIfDisposed();

src/Tizen.System.Usb/Usb/UsbDevice.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ public UsbControlEndpoint ControlEndpoint
117117
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
118118
/// <exception cref="InvalidOperationException">Throws exception if device is disconnected or not opened for operation. </exception>
119119
/// <since_tizen> 4 </since_tizen>
120+
/// <example>
121+
/// <code>
122+
/// device.Configurations[123].SetAsActive();
123+
/// DoSomeWorkNow(device.ActiveConfiguration);
124+
/// </code>
125+
/// </example>
120126
public UsbConfiguration ActiveConfiguration
121127
{
122128
get
@@ -135,6 +141,12 @@ public UsbConfiguration ActiveConfiguration
135141
/// <feature>http://tizen.org/feature/usb.host</feature>
136142
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
137143
/// <since_tizen> 4 </since_tizen>
144+
/// <example>
145+
/// <code>
146+
/// device.Configurations[123].SetAsActive();
147+
/// DoSomeWorkNow(device.ActiveConfiguration);
148+
/// </code>
149+
/// </example>
138150
public IReadOnlyDictionary<int, UsbConfiguration> Configurations
139151
{
140152
get
@@ -158,6 +170,11 @@ public IReadOnlyDictionary<int, UsbConfiguration> Configurations
158170
/// <feature>http://tizen.org/feature/usb.host</feature>
159171
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
160172
/// <since_tizen> 4 </since_tizen>
173+
/// <example>
174+
/// <code>
175+
/// var specificDevice = manager.AvailableDevices.SingleOrDefault(dev => dev.DeviceInformation.ProductId == 0x123);
176+
/// </code>
177+
/// </example>
161178
public UsbDeviceInformation DeviceInformation
162179
{
163180
get
@@ -174,6 +191,12 @@ public UsbDeviceInformation DeviceInformation
174191
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
175192
/// <exception cref="InvalidOperationException"> Throws exception if device is disconnected or not opened for operation. </exception>
176193
/// <since_tizen> 4 </since_tizen>
194+
/// <example>
195+
/// <code>
196+
/// device.Open();
197+
/// if (device.Strings.Manufacturer == "Samsung") ...
198+
/// </code>
199+
/// </example>
177200
public UsbDeviceStrings Strings
178201
{
179202
get
@@ -193,6 +216,14 @@ public UsbDeviceStrings Strings
193216
/// <exception cref="InvalidOperationException">Throws an exception if the device is disconnected.</exception>
194217
/// <exception cref="UnauthorizedAccessException">Throws an exception if the user has insufficient permission on the device.</exception>
195218
/// <since_tizen> 4 </since_tizen>
219+
/// <example>
220+
/// <code>
221+
/// var device = manager.Devices.Single(...);
222+
/// dev.Open();
223+
/// ... do the needful ...
224+
/// dev.Close();
225+
/// </code>
226+
/// </example>
196227
public void Open()
197228
{
198229
ThrowIfDisposed();
@@ -205,6 +236,14 @@ public void Open()
205236
/// <feature>http://tizen.org/feature/usb.host</feature>
206237
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
207238
/// <since_tizen> 4 </since_tizen>
239+
/// <example>
240+
/// <code>
241+
/// var device = manager.Devices.Single(...);
242+
/// dev.Open();
243+
/// ... do the needful ...
244+
/// dev.Close();
245+
/// </code>
246+
/// </example>
208247
public void Close()
209248
{
210249
ThrowIfDisposed();

src/Tizen.System.Usb/Usb/UsbInterface.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public int AlternateSetting
7272
/// <feature>http://tizen.org/feature/usb.host</feature>
7373
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
7474
/// <since_tizen> 4 </since_tizen>
75+
/// <example>
76+
/// <code>
77+
/// var endpoint = iface.Endpoints[123];
78+
/// </code>
79+
/// </example>
7580
public IReadOnlyDictionary<int, UsbEndpoint> Endpoints
7681
{
7782
get
@@ -123,6 +128,14 @@ public string InterfaceString
123128
/// Throws an exception if device is disconnected or not opened for operation or another program or driver has claimed the interface.
124129
/// </exception>
125130
/// <since_tizen> 4 </since_tizen>
131+
/// <example>
132+
/// <code>
133+
/// var interface = device.Interfaces[123];
134+
/// interface.Claim();
135+
/// ... do the needful ...
136+
/// interface.Release();
137+
/// </code>
138+
/// </example>
126139
public void Claim(bool force)
127140
{
128141
ThrowIfDisposed();
@@ -146,6 +159,14 @@ public void Claim(bool force)
146159
/// <exception cref="InvalidOperationException">Throws exception if the device is disconnected or not opened for operation.</exception>
147160
/// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on the device.</exception>
148161
/// <since_tizen> 4 </since_tizen>
162+
/// <example>
163+
/// <code>
164+
/// var interface = device.Interfaces[123];
165+
/// interface.Claim();
166+
/// ... do the needful ...
167+
/// interface.Release();
168+
/// </code>
169+
/// </example>
149170
public void Release()
150171
{
151172
ThrowIfDisposed();

src/Tizen.System.Usb/Usb/UsbInterruptEndpoint.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public int PollingInterval
6161
/// <exception cref="InvalidOperationException">Throws exception if device is disconnected or not opened for operation.</exception>
6262
/// <exception cref="TimeoutException">Throws exception if transfer timed-out.</exception>
6363
/// <since_tizen> 4 </since_tizen>
64+
/// <example>
65+
/// <code>
66+
/// byte[] buffer = new byte[123];
67+
/// int bytesTransferred = endpoint.Transfer(buffer, buffer.Length, 1000);
68+
/// </code>
69+
/// </example>
6470
public int Transfer(byte[] buffer, int length, uint timeout)
6571
{
6672
return TransferImpl(buffer, length, timeout);

src/Tizen.System.Usb/Usb/UsbManager.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ public UsbManager()
7272
/// <exception cref="OutOfMemoryException">Throws exception in case of insufficient memory.</exception>
7373
/// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
7474
/// <since_tizen> 4 </since_tizen>
75+
/// <example>
76+
/// <code>
77+
/// var specificDevice = manager.AvailableDevices.SingleOrDefault(dev => dev.DeviceInformation.ProductId == 0x123);
78+
/// foreach (var dev in manager.AvailableDevices.Where(...)) { ... }
79+
/// </code>
80+
/// </example>
7581
public IEnumerable<UsbDevice> AvailableDevices
7682
{
7783
get
@@ -87,6 +93,13 @@ public IEnumerable<UsbDevice> AvailableDevices
8793
/// <feature>http://tizen.org/feature/usb.host</feature>
8894
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
8995
/// <since_tizen> 4 </since_tizen>
96+
/// <example>
97+
/// <code>
98+
/// manager.DeviceHotPlugged += (object sender, HotPluggedEventArgs args) => {
99+
/// Tizen.Log.Warn("EXAMPLE", $"product {args.Device.DeviceInformation.ProductId} was {0}", args.EventType == HotplugEventType.Attach ? "attached" : "detached");
100+
/// };
101+
/// </code>
102+
/// </example>
90103
public event EventHandler<HotPluggedEventArgs> DeviceHotPlugged;
91104

92105
internal void HostHotplugAttachCallback(IntPtr devHandle, IntPtr userData)

0 commit comments

Comments
 (0)