diff options
author | Emmanuel Pacaud <emmanuel@gnome.org> | 2020-01-17 16:46:54 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2020-01-20 14:06:14 +0000 |
commit | 8e1b23f5f6acfe18ef2500cfcd7def68603dcd21 (patch) | |
tree | 934ec215dffc0c038d0b6828bce60f5c9fbe1a9b | |
parent | 17f9cda073459fc673a99bd281e929a999643374 (diff) | |
download | gusb-8e1b23f5f6acfe18ef2500cfcd7def68603dcd21.tar.gz |
Add a thin glib wrapper around libusb_endpoint_descriptor
-rw-r--r-- | gusb/gusb-endpoint-private.h | 34 | ||||
-rw-r--r-- | gusb/gusb-endpoint.c | 244 | ||||
-rw-r--r-- | gusb/gusb-endpoint.h | 45 | ||||
-rw-r--r-- | gusb/gusb-interface.c | 25 | ||||
-rw-r--r-- | gusb/gusb-interface.h | 1 | ||||
-rw-r--r-- | gusb/gusb.h | 1 | ||||
-rw-r--r-- | gusb/libgusb.ver | 11 | ||||
-rw-r--r-- | gusb/meson.build | 7 |
8 files changed, 368 insertions, 0 deletions
diff --git a/gusb/gusb-endpoint-private.h b/gusb/gusb-endpoint-private.h new file mode 100644 index 0000000..3e6c501 --- /dev/null +++ b/gusb/gusb-endpoint-private.h @@ -0,0 +1,34 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2015 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU Lesser General Public License Version 2.1 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef __GUSB_ENDPOINT_PRIVATE_H__ +#define __GUSB_ENDPOINT_PRIVATE_H__ + +#include <libusb.h> + +#include <gusb/gusb-endpoint.h> + +G_BEGIN_DECLS + +GUsbEndpoint *_g_usb_endpoint_new (const struct libusb_endpoint_descriptor *endpoint); + +G_END_DECLS + +#endif /* __GUSB_ENDPOINT_PRIVATE_H__ */ diff --git a/gusb/gusb-endpoint.c b/gusb/gusb-endpoint.c new file mode 100644 index 0000000..82c18ad --- /dev/null +++ b/gusb/gusb-endpoint.c @@ -0,0 +1,244 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2015 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU Lesser General Public License Version 2.1 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see <http://www.gnu.org/licenses/>. + */ + +/** + * SECTION:gusb-endponit + * @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 destoyed 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; +} diff --git a/gusb/gusb-endpoint.h b/gusb/gusb-endpoint.h new file mode 100644 index 0000000..b40fa87 --- /dev/null +++ b/gusb/gusb-endpoint.h @@ -0,0 +1,45 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2015 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU Lesser General Public License Version 2.1 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef __GUSB_ENDPOINT_H__ +#define __GUSB_ENDPOINT_H__ + +#include <glib-object.h> +#include <gio/gio.h> +#include <gusb-device.h> + +G_BEGIN_DECLS + +#define G_USB_TYPE_ENDPOINT (g_usb_endpoint_get_type ()) +G_DECLARE_FINAL_TYPE (GUsbEndpoint, g_usb_endpoint, G_USB, ENDPOINT, GObject) + +guint8 g_usb_endpoint_get_kind (GUsbEndpoint *endpoint); +guint16 g_usb_endpoint_get_maximum_packet_size (GUsbEndpoint *endpoint); +guint8 g_usb_endpoint_get_polling_interval (GUsbEndpoint *endpoint); +guint8 g_usb_endpoint_get_refresh (GUsbEndpoint *endpoint); +guint8 g_usb_endpoint_get_synch_address (GUsbEndpoint *endpoint); +guint8 g_usb_endpoint_get_address (GUsbEndpoint *endpoint); +guint8 g_usb_endpoint_get_number (GUsbEndpoint *endpoint); +GUsbDeviceDirection g_usb_endpoint_get_direction (GUsbEndpoint *endpoint); +GBytes * g_usb_endpoint_get_extra (GUsbEndpoint *endpoint); + +G_END_DECLS + +#endif /* __GUSB_ENDPOINT_H__ */ diff --git a/gusb/gusb-interface.c b/gusb/gusb-interface.c index b7b0238..723557f 100644 --- a/gusb/gusb-interface.c +++ b/gusb/gusb-interface.c @@ -34,6 +34,7 @@ #include "gusb-interface.h" #include "gusb-interface-private.h" +#include "gusb-endpoint-private.h" struct _GUsbInterface { @@ -41,6 +42,8 @@ struct _GUsbInterface struct libusb_interface_descriptor iface; GBytes *extra; + + GPtrArray *endpoints; }; G_DEFINE_TYPE (GUsbInterface, g_usb_interface, G_TYPE_OBJECT) @@ -51,6 +54,7 @@ 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); } @@ -87,6 +91,10 @@ _g_usb_interface_new (const struct libusb_interface_descriptor *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); } @@ -244,3 +252,20 @@ 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); +} diff --git a/gusb/gusb-interface.h b/gusb/gusb-interface.h index 74905c6..4e19cac 100644 --- a/gusb/gusb-interface.h +++ b/gusb/gusb-interface.h @@ -38,6 +38,7 @@ guint8 g_usb_interface_get_subclass (GUsbInterface *interface); guint8 g_usb_interface_get_protocol (GUsbInterface *interface); guint8 g_usb_interface_get_index (GUsbInterface *interface); GBytes *g_usb_interface_get_extra (GUsbInterface *interface); +GPtrArray *g_usb_interface_get_endpoints (GUsbInterface *interface); G_END_DECLS diff --git a/gusb/gusb.h b/gusb/gusb.h index 56d08e1..8102ef4 100644 --- a/gusb/gusb.h +++ b/gusb/gusb.h @@ -27,6 +27,7 @@ #include <gusb/gusb-autocleanups.h> #include <gusb/gusb-context.h> #include <gusb/gusb-interface.h> +#include <gusb/gusb-endpoint.h> #include <gusb/gusb-source.h> #include <gusb/gusb-device.h> #include <gusb/gusb-device-list.h> diff --git a/gusb/libgusb.ver b/gusb/libgusb.ver index a2e07eb..4afc230 100644 --- a/gusb/libgusb.ver +++ b/gusb/libgusb.ver @@ -73,3 +73,14 @@ LIBGUSB_0.3.1 { global: g_usb_device_get_spec; } LIBGUSB_0.2.11; + +LIBGUSB_0.3.3 { + global: + g_usb_interface_get_endpoints; + g_usb_endpoint_get_kind; + g_usb_endpoint_get_maximum_packet_size; + g_usb_endpoint_get_polling_interval; + g_usb_endpoint_get_address; + g_usb_endpoint_get_direction; + g_usb_endpoint_get_extra; +} LIBGUSB_0.3.1; diff --git a/gusb/meson.build b/gusb/meson.build index b721ef4..931399d 100644 --- a/gusb/meson.build +++ b/gusb/meson.build @@ -31,6 +31,8 @@ install_headers([ 'gusb-device-list.h', 'gusb-interface.h', 'gusb-interface-private.h', + 'gusb-endpoint.h', + 'gusb-endpoint-private.h', 'gusb-source.h', 'gusb-util.h', ], @@ -49,6 +51,7 @@ gusb = library( 'gusb-device.c', 'gusb-device-list.c', 'gusb-interface.c', + 'gusb-endpoint.c', 'gusb-source.c', 'gusb-util.c', 'gusb-version.c', @@ -107,6 +110,9 @@ libgusb_girtarget = gnome.generate_gir(gusb, 'gusb-interface.c', 'gusb-interface.h', 'gusb-interface-private.h', + 'gusb-endpoint.c', + 'gusb-endpoint.h', + 'gusb-endpoint-private.h', 'gusb-source.c', 'gusb-source.h', 'gusb-util.c', @@ -152,6 +158,7 @@ if get_option('tests') 'gusb-device.c', 'gusb-device-list.c', 'gusb-interface.c', + 'gusb-endpoint.c', 'gusb-self-test.c', 'gusb-source.c', 'gusb-util.c', |