summaryrefslogtreecommitdiff
path: root/gusb/gusb-interface.c
blob: 5eb2c977dbed38d842d0173e94ae6764db08fc40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2015 Richard Hughes <richard@hughsie.com>
 * Copyright (C) 2020 Emmanuel Pacaud <emmanuel@gnome.org>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

/**
 * SECTION:gusb-interface
 * @short_description: GLib wrapper around a USB interface.
 *
 * This object is a thin glib wrapper around a libusb_interface_descriptor.
 *
 * All the data is copied when the object is created and the original
 * descriptor can be destroyed any at point.
 */

#include "config.h"

#include <string.h>

#include "gusb-interface.h"
#include "gusb-interface-private.h"
#include "gusb-endpoint-private.h"

struct _GUsbInterface
{
	GObject parent_instance;

	struct libusb_interface_descriptor iface;
	GBytes *extra;

	GPtrArray *endpoints;
};

G_DEFINE_TYPE (GUsbInterface, g_usb_interface, G_TYPE_OBJECT)

static void
g_usb_interface_finalize (GObject *object)
{
	GUsbInterface *interface = G_USB_INTERFACE (object);

	g_bytes_unref (interface->extra);
	g_ptr_array_unref (interface->endpoints);

	G_OBJECT_CLASS (g_usb_interface_parent_class)->finalize (object);
}

static void
g_usb_interface_class_init (GUsbInterfaceClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = g_usb_interface_finalize;
}

static void
g_usb_interface_init (GUsbInterface *interface)
{
}

/**
 * _g_usb_interface_new:
 *
 * Return value: a new #GUsbInterface object.
 *
 * Since: 0.2.8
 **/
GUsbInterface *
_g_usb_interface_new (const struct libusb_interface_descriptor *iface)
{
	GUsbInterface *interface;
	interface = g_object_new (G_USB_TYPE_INTERFACE, NULL);

	/* copy the data */
	memcpy (&interface->iface,
		iface,
		sizeof (struct libusb_interface_descriptor));
	interface->extra = g_bytes_new (iface->extra, iface->extra_length);

	interface->endpoints = g_ptr_array_new_with_free_func (g_object_unref);
	for (guint i = 0; i < iface->bNumEndpoints; i++)
		g_ptr_array_add (interface->endpoints, _g_usb_endpoint_new (&iface->endpoint[i]));

	return G_USB_INTERFACE (interface);
}

/**
 * g_usb_interface_get_length:
 * @interface: a #GUsbInterface
 *
 * Gets the USB bus number for the interface.
 *
 * Return value: The 8-bit bus number
 *
 * Since: 0.2.8
 **/
guint8
g_usb_interface_get_length (GUsbInterface *interface)
{
	g_return_val_if_fail (G_USB_IS_INTERFACE (interface), 0);
	return interface->iface.bLength;
}

/**
 * g_usb_interface_get_kind:
 * @interface: a #GUsbInterface
 *
 * Gets the type of interface.
 *
 * Return value: The 8-bit address
 *
 * Since: 0.2.8
 **/
guint8
g_usb_interface_get_kind (GUsbInterface *interface)
{
	g_return_val_if_fail (G_USB_IS_INTERFACE (interface), 0);
	return interface->iface.bDescriptorType;
}

/**
 * g_usb_interface_get_number:
 * @interface: a #GUsbInterface
 *
 * Gets the interface number.
 *
 * Return value: The interface ID
 *
 * Since: 0.2.8
 **/
guint8
g_usb_interface_get_number (GUsbInterface *interface)
{
	g_return_val_if_fail (G_USB_IS_INTERFACE (interface), 0);
	return interface->iface.bInterfaceNumber;
}

/**
 * g_usb_interface_get_alternate:
 * @interface: a #GUsbInterface
 *
 * Gets the alternate setting for the interface.
 *
 * Return value: alt setting, typically zero.
 *
 * Since: 0.2.8
 **/
guint8
g_usb_interface_get_alternate (GUsbInterface *interface)
{
	g_return_val_if_fail (G_USB_IS_INTERFACE (interface), 0);
	return interface->iface.bAlternateSetting;
}

/**
 * g_usb_interface_get_class:
 * @interface: a #GUsbInterface
 *
 * Gets the interface class, typically a #GUsbInterfaceClassCode.
 *
 * Return value: a interface class number, e.g. 0x09 is a USB hub.
 *
 * Since: 0.2.8
 **/
guint8
g_usb_interface_get_class (GUsbInterface *interface)
{
	g_return_val_if_fail (G_USB_IS_INTERFACE (interface), 0);
	return interface->iface.bInterfaceClass;
}

/**
 * g_usb_interface_get_subclass:
 * @interface: a #GUsbInterface
 *
 * Gets the interface subclass qualified by the class number.
 * See g_usb_interface_get_class().
 *
 * Return value: a interface subclass number.
 *
 * Since: 0.2.8
 **/
guint8
g_usb_interface_get_subclass (GUsbInterface *interface)
{
	g_return_val_if_fail (G_USB_IS_INTERFACE (interface), 0);
	return interface->iface.bInterfaceSubClass;
}

/**
 * g_usb_interface_get_protocol:
 * @interface: a #GUsbInterface
 *
 * Gets the interface protocol qualified by the class and subclass numbers.
 * See g_usb_interface_get_class() and g_usb_interface_get_subclass().
 *
 * Return value: a interface protocol number.
 *
 * Since: 0.2.8
 **/
guint8
g_usb_interface_get_protocol (GUsbInterface *interface)
{
	g_return_val_if_fail (G_USB_IS_INTERFACE (interface), 0);
	return interface->iface.bInterfaceProtocol;
}

/**
 * g_usb_interface_get_index:
 * @interface: a #GUsbInterface
 *
 * Gets the index for the string descriptor.
 *
 * Return value: a string descriptor index.
 *
 * Since: 0.2.8
 **/
guint8
g_usb_interface_get_index (GUsbInterface *interface)
{
	g_return_val_if_fail (G_USB_IS_INTERFACE (interface), 0);
	return interface->iface.iInterface;
}

/**
 * g_usb_interface_get_extra:
 * @interface: a #GUsbInterface
 *
 * Gets any extra data from the interface.
 *
 * Return value: (transfer none): a #GBytes, or %NULL for failure
 *
 * Since: 0.2.8
 **/
GBytes *
g_usb_interface_get_extra (GUsbInterface *interface)
{
	g_return_val_if_fail (G_USB_IS_INTERFACE (interface), NULL);
	return interface->extra;
}

/**
 * g_usb_interface_get_endpoints:
 * @interface: a #GUsbInterface
 *
 * Gets interface endpoints.
 *
 * Return value: (transfer container) (element-type GUsbEndpoint): an array of endpoints, or %NULL on failure
 *
 * Since: 0.3.3
 **/
GPtrArray *
g_usb_interface_get_endpoints (GUsbInterface	*interface)
{
	g_return_val_if_fail (G_USB_IS_INTERFACE (interface), NULL);
	return g_ptr_array_ref (interface->endpoints);
}