/src/fwupd/libfwupdplugin/fu-usb-config-descriptor.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2024 Richard Hughes <richard@hughsie.com> |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | */ |
6 | | |
7 | | /** |
8 | | * FuUsbConfigDescriptor: |
9 | | * |
10 | | * This object is a thin glib wrapper around a `libusb_config_dev_capability_descriptor`. |
11 | | * |
12 | | * All the data is copied when the object is created and the original descriptor can be destroyed |
13 | | * at any point. |
14 | | */ |
15 | | |
16 | | #include "config.h" |
17 | | |
18 | | #include "fu-usb-config-descriptor-private.h" |
19 | | |
20 | | struct _FuUsbConfigDescriptor { |
21 | | FuUsbDescriptor parent_instance; |
22 | | guint8 configuration; |
23 | | guint8 configuration_value; |
24 | | }; |
25 | | |
26 | | static void |
27 | | fu_usb_config_descriptor_codec_iface_init(FwupdCodecInterface *iface); |
28 | | |
29 | 397k | G_DEFINE_TYPE_EXTENDED(FuUsbConfigDescriptor, |
30 | 397k | fu_usb_config_descriptor, |
31 | 397k | FU_TYPE_USB_DESCRIPTOR, |
32 | 397k | 0, |
33 | 397k | G_IMPLEMENT_INTERFACE(FWUPD_TYPE_CODEC, |
34 | 397k | fu_usb_config_descriptor_codec_iface_init)); |
35 | 397k | |
36 | 397k | static gboolean |
37 | 397k | fu_usb_config_descriptor_from_json(FwupdCodec *codec, FwupdJsonObject *json_obj, GError **error) |
38 | 397k | { |
39 | 0 | FuUsbConfigDescriptor *self = FU_USB_CONFIG_DESCRIPTOR(codec); |
40 | 0 | gint64 tmpi = 0; |
41 | | |
42 | | /* optional properties */ |
43 | 0 | if (!fwupd_json_object_get_integer_with_default(json_obj, |
44 | 0 | "Configuration", |
45 | 0 | &tmpi, |
46 | 0 | 0x0, |
47 | 0 | error)) |
48 | 0 | return FALSE; |
49 | 0 | self->configuration = tmpi; |
50 | 0 | if (!fwupd_json_object_get_integer_with_default(json_obj, |
51 | 0 | "ConfigurationValue", |
52 | 0 | &tmpi, |
53 | 0 | 0x0, |
54 | 0 | error)) |
55 | 0 | return FALSE; |
56 | 0 | self->configuration_value = tmpi; |
57 | | |
58 | | /* success */ |
59 | 0 | return TRUE; |
60 | 0 | } |
61 | | |
62 | | static void |
63 | | fu_usb_config_descriptor_add_json(FwupdCodec *codec, |
64 | | FwupdJsonObject *json_obj, |
65 | | FwupdCodecFlags flags) |
66 | 0 | { |
67 | 0 | FuUsbConfigDescriptor *self = FU_USB_CONFIG_DESCRIPTOR(codec); |
68 | | |
69 | | /* optional properties */ |
70 | 0 | if (self->configuration != 0) |
71 | 0 | fwupd_json_object_add_integer(json_obj, "Configuration", self->configuration); |
72 | 0 | if (self->configuration_value != 0) { |
73 | 0 | fwupd_json_object_add_integer(json_obj, |
74 | 0 | "ConfigurationValue", |
75 | 0 | self->configuration_value); |
76 | 0 | } |
77 | 0 | } |
78 | | |
79 | | /** |
80 | | * fu_usb_config_descriptor_get_configuration: |
81 | | * @self: a #FuUsbConfigDescriptor |
82 | | * |
83 | | * Gets the config descriptor configuration. |
84 | | * |
85 | | * Return value: integer |
86 | | * |
87 | | * Since: 2.0.0 |
88 | | **/ |
89 | | guint8 |
90 | | fu_usb_config_descriptor_get_configuration(FuUsbConfigDescriptor *self) |
91 | 0 | { |
92 | 0 | g_return_val_if_fail(FU_IS_USB_CONFIG_DESCRIPTOR(self), 0); |
93 | 0 | return self->configuration; |
94 | 0 | } |
95 | | |
96 | | /** |
97 | | * fu_usb_config_descriptor_get_configuration_value: |
98 | | * @self: a #FuUsbConfigDescriptor |
99 | | * |
100 | | * Gets the CONFIG descriptor configuration value. |
101 | | * |
102 | | * Return value: integer |
103 | | * |
104 | | * Since: 2.0.0 |
105 | | **/ |
106 | | guint8 |
107 | | fu_usb_config_descriptor_get_configuration_value(FuUsbConfigDescriptor *self) |
108 | 0 | { |
109 | 0 | g_return_val_if_fail(FU_IS_USB_CONFIG_DESCRIPTOR(self), 0); |
110 | 0 | return self->configuration_value; |
111 | 0 | } |
112 | | |
113 | | static gboolean |
114 | | fu_usb_config_descriptor_parse(FuFirmware *firmware, |
115 | | GInputStream *stream, |
116 | | FuFirmwareParseFlags flags, |
117 | | GError **error) |
118 | 132k | { |
119 | 132k | FuUsbConfigDescriptor *self = FU_USB_CONFIG_DESCRIPTOR(firmware); |
120 | 132k | g_autoptr(FuUsbDescriptorHdr) st = NULL; |
121 | | |
122 | | /* parse */ |
123 | 132k | st = fu_usb_descriptor_hdr_parse_stream(stream, 0x0, error); |
124 | 132k | if (st == NULL) |
125 | 45 | return FALSE; |
126 | 132k | self->configuration = fu_usb_descriptor_hdr_get_configuration(st); |
127 | 132k | self->configuration_value = fu_usb_descriptor_hdr_get_configuration_value(st); |
128 | | |
129 | | /* success */ |
130 | 132k | return TRUE; |
131 | 132k | } |
132 | | |
133 | | static void |
134 | | fu_usb_config_descriptor_codec_iface_init(FwupdCodecInterface *iface) |
135 | 1 | { |
136 | 1 | iface->add_json = fu_usb_config_descriptor_add_json; |
137 | 1 | iface->from_json = fu_usb_config_descriptor_from_json; |
138 | 1 | } |
139 | | |
140 | | static void |
141 | | fu_usb_config_descriptor_class_init(FuUsbConfigDescriptorClass *klass) |
142 | 1 | { |
143 | 1 | FuFirmwareClass *firmware_class = FU_FIRMWARE_CLASS(klass); |
144 | 1 | firmware_class->parse = fu_usb_config_descriptor_parse; |
145 | 1 | } |
146 | | |
147 | | static void |
148 | | fu_usb_config_descriptor_init(FuUsbConfigDescriptor *self) |
149 | 132k | { |
150 | 132k | } |
151 | | |
152 | | /** |
153 | | * fu_usb_config_descriptor_new: |
154 | | * |
155 | | * Return value: a new #FuUsbConfigDescriptor object. |
156 | | * |
157 | | * Since: 2.0.0 |
158 | | **/ |
159 | | FuUsbConfigDescriptor * |
160 | | fu_usb_config_descriptor_new(void) |
161 | 132k | { |
162 | 132k | return FU_USB_CONFIG_DESCRIPTOR(g_object_new(FU_TYPE_USB_CONFIG_DESCRIPTOR, NULL)); |
163 | 132k | } |