summaryrefslogtreecommitdiff
path: root/gusb/gusb-endpoint.c
blob: 765c299d0b8a16b233771f7d662567f943588f71 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2020 Emmanuel Pacaud <emmanuel@gnome.org>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

/**
 * SECTION:gusb-endpoint
 * @short_description: GLib wrapper around a USB endpoint.
 *
 * This object is a thin glib wrapper around a libusb_endpoint_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 "gusb-endpoint.h"
#include "gusb-endpoint-private.h"

struct _GUsbEndpoint
{
	GObject parent_instance;

	struct libusb_endpoint_descriptor endpoint_descriptor;
	GBytes *extra;
};

G_DEFINE_TYPE (GUsbEndpoint, g_usb_endpoint, G_TYPE_OBJECT)

static void
g_usb_endpoint_finalize (GObject *object)
{
	GUsbEndpoint *endpoint = G_USB_ENDPOINT (object);

	g_bytes_unref (endpoint->extra);

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

static void
g_usb_endpoint_class_init (GUsbEndpointClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = g_usb_endpoint_finalize;
}

static void
g_usb_endpoint_init (GUsbEndpoint *endpoint)
{
}

/**
 * _g_usb_endpoint_new:
 *
 * Return value: a new #GUsbEndpoint object.
 *
 * Since: 0.3.3
 **/
GUsbEndpoint *
_g_usb_endpoint_new (const struct libusb_endpoint_descriptor *endpoint_descriptor)
{
	GUsbEndpoint *endpoint;
	endpoint = g_object_new (G_USB_TYPE_ENDPOINT, NULL);

	/* copy the data */
	memcpy (&endpoint->endpoint_descriptor,
		endpoint_descriptor,
		sizeof (struct libusb_endpoint_descriptor));
	endpoint->extra = g_bytes_new (endpoint_descriptor->extra, endpoint_descriptor->extra_length);

	return G_USB_ENDPOINT (endpoint);
}

/**
 * g_usb_endpoint_get_kind:
 * @endpoint: a #GUsbEndpoint
 *
 * Gets the type of endpoint.
 *
 * Return value: The 8-bit type
 *
 * Since: 0.3.3
 **/
guint8
g_usb_endpoint_get_kind	(GUsbEndpoint *endpoint)
{
	g_return_val_if_fail (G_USB_IS_ENDPOINT (endpoint), 0);
	return endpoint->endpoint_descriptor.bDescriptorType;
}

/**
 * g_usb_endpoint_get_maximum_packet_size:
 * @endpoint: a #GUsbEndpoint
 *
 * Gets the maximum packet size this endpoint is capable of sending/receiving.
 *
 * Return value: The maximum packet size
 *
 * Since: 0.3.3
 **/
guint16
g_usb_endpoint_get_maximum_packet_size (GUsbEndpoint *endpoint)
{
	g_return_val_if_fail (G_USB_IS_ENDPOINT (endpoint), 0);
	return endpoint->endpoint_descriptor.wMaxPacketSize;
}

/**
 * g_usb_endpoint_get_polling_interval:
 * @endpoint: a #GUsbEndpoint
 *
 * Gets the endpoint polling interval.
 *
 * Return value: The endpoint polling interval
 *
 * Since: 0.3.3
 **/
guint8
g_usb_endpoint_get_polling_interval (GUsbEndpoint *endpoint)
{
	g_return_val_if_fail (G_USB_IS_ENDPOINT (endpoint), 0);
	return endpoint->endpoint_descriptor.bInterval;
}

/**
 * g_usb_endpoint_get_refresh:
 * @endpoint: a #GUsbEndpoint
 *
 * Gets the rate at which synchronization feedback is provided, for audio device only.
 *
 * Return value: The endpoint refresh
 *
 * Since: 0.3.3
 **/
guint8
g_usb_endpoint_get_refresh (GUsbEndpoint *endpoint)
{
	g_return_val_if_fail (G_USB_IS_ENDPOINT (endpoint), 0);
	return endpoint->endpoint_descriptor.bRefresh;
}

/**
 * g_usb_endpoint_get_synch_address:
 * @endpoint: a #GUsbEndpoint
 *
 * Gets the address if the synch endpoint, for audio device only.
 *
 * Return value: The synch endpoint address
 *
 * Since: 0.3.3
 **/
guint8
g_usb_endpoint_get_synch_address (GUsbEndpoint *endpoint)
{
	g_return_val_if_fail (G_USB_IS_ENDPOINT (endpoint), 0);
	return endpoint->endpoint_descriptor.bSynchAddress;
}

/**
 * g_usb_endpoint_get_address:
 * @endpoint: a #GUsbEndpoint
 *
 * Gets the address of the endpoint.
 *
 * Return value: The 4-bit endpoint address
 *
 * Since: 0.3.3
 **/
guint8
g_usb_endpoint_get_address (GUsbEndpoint *endpoint)
{
	g_return_val_if_fail (G_USB_IS_ENDPOINT (endpoint), 0);
	return endpoint->endpoint_descriptor.bEndpointAddress;
}

/**
 * g_usb_endpoint_get_number:
 * @endpoint: a #GUsbEndpoint
 *
 * Gets the number part of endpoint address.
 *
 * Return value: The lower 4-bit of endpoint address
 *
 * Since: 0.3.3
 **/
guint8
g_usb_endpoint_get_number (GUsbEndpoint *endpoint)
{
	g_return_val_if_fail (G_USB_IS_ENDPOINT (endpoint), 0);
	return (endpoint->endpoint_descriptor.bEndpointAddress) & 0xf;
}

/**
 * g_usb_endpoint_get_direction:
 * @endpoint: a #GUsbEndpoint
 *
 * Gets the direction of the endpoint.
 *
 * Return value: The endpoint direction
 *
 * Since: 0.3.3
 **/
GUsbDeviceDirection
g_usb_endpoint_get_direction (GUsbEndpoint *endpoint)
{
	g_return_val_if_fail (G_USB_IS_ENDPOINT (endpoint), 0);
	return (endpoint->endpoint_descriptor.bEndpointAddress & 0x80) ?
		G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST :
		G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE;
}

/**
 * g_usb_endpoint_get_extra:
 * @endpoint: a #GUsbEndpoint
 *
 * Gets any extra data from the endpoint.
 *
 * Return value: (transfer none): a #GBytes, or %NULL for failure
 *
 * Since: 0.3.3
 **/
GBytes *
g_usb_endpoint_get_extra (GUsbEndpoint *endpoint)
{
	g_return_val_if_fail (G_USB_IS_ENDPOINT (endpoint), NULL);
	return endpoint->extra;
}