GCC Code Coverage Report


Directory: ./
File: panels/privacy/bolt/bolt-enums.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 140 0.0%
Functions: 0 25 0.0%
Branches: 0 59 0.0%

Line Branch Exec Source
1 /*
2 * Copyright © 2017 Red Hat, Inc
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors:
18 * Christian J. Kellner <christian@kellner.me>
19 */
20
21 #include "config.h"
22
23 #include "bolt-enums.h"
24 #include "bolt-error.h"
25
26 #include <gio/gio.h>
27
28 #if !GLIB_CHECK_VERSION(2, 57, 0)
29 G_DEFINE_AUTOPTR_CLEANUP_FUNC (GEnumClass, g_type_class_unref);
30 G_DEFINE_AUTOPTR_CLEANUP_FUNC (GFlagsClass, g_type_class_unref);
31 #endif
32
33 gboolean
34 bolt_enum_class_validate (GEnumClass *enum_class,
35 gint value,
36 GError **error)
37 {
38 const char *name;
39 gboolean oob;
40
41 if (enum_class == NULL)
42 {
43 name = g_type_name_from_class ((GTypeClass *) enum_class);
44 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
45 "could not determine enum class for '%s'",
46 name);
47
48 return FALSE;
49 }
50
51 oob = value < enum_class->minimum || value > enum_class->maximum;
52
53 if (oob)
54 {
55 name = g_type_name_from_class ((GTypeClass *) enum_class);
56 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
57 "enum value '%d' is out of bounds for '%s'",
58 value, name);
59 return FALSE;
60 }
61
62 return TRUE;
63 }
64
65 gboolean
66 bolt_enum_validate (GType enum_type,
67 gint value,
68 GError **error)
69 {
70 g_autoptr(GEnumClass) klass = g_type_class_ref (enum_type);
71 return bolt_enum_class_validate (klass, value, error);
72 }
73
74 const char *
75 bolt_enum_to_string (GType enum_type,
76 gint value,
77 GError **error)
78 {
79 g_autoptr(GEnumClass) klass = NULL;
80 GEnumValue *ev;
81
82 klass = g_type_class_ref (enum_type);
83
84 if (!bolt_enum_class_validate (klass, value, error))
85 return NULL;
86
87 ev = g_enum_get_value (klass, value);
88 return ev->value_nick;
89 }
90
91 gint
92 bolt_enum_from_string (GType enum_type,
93 const char *string,
94 GError **error)
95 {
96 g_autoptr(GEnumClass) klass = NULL;
97 const char *name;
98 GEnumValue *ev;
99
100 klass = g_type_class_ref (enum_type);
101
102 if (klass == NULL)
103 {
104 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
105 "could not determine enum class");
106 return -1;
107 }
108
109 if (string == NULL)
110 {
111 name = g_type_name_from_class ((GTypeClass *) klass);
112 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
113 "empty string passed for enum class for '%s'",
114 name);
115 return -1;
116 }
117
118 ev = g_enum_get_value_by_nick (klass, string);
119
120 if (ev == NULL)
121 {
122 name = g_type_name (enum_type);
123 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
124 "invalid string '%s' for enum '%s'", string, name);
125 return -1;
126 }
127
128 return ev->value;
129 }
130
131 char *
132 bolt_flags_class_to_string (GFlagsClass *flags_class,
133 guint value,
134 GError **error)
135 {
136 g_autoptr(GString) str = NULL;
137 const char *name;
138 GFlagsValue *fv;
139
140 if (flags_class == NULL)
141 {
142 name = g_type_name_from_class ((GTypeClass *) flags_class);
143 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
144 "could not determine flags class for '%s'",
145 name);
146
147 return FALSE;
148 }
149
150 fv = g_flags_get_first_value (flags_class, value);
151 if (fv == NULL)
152 {
153 if (value == 0)
154 return g_strdup ("");
155
156 name = g_type_name_from_class ((GTypeClass *) flags_class);
157
158 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
159 "invalid value '%u' for flags '%s'", value, name);
160 return NULL;
161 }
162
163 value &= ~fv->value;
164 str = g_string_new (fv->value_nick);
165
166 while (value != 0 &&
167 (fv = g_flags_get_first_value (flags_class, value)) != NULL)
168 {
169 g_string_append (str, " | ");
170 g_string_append (str, fv->value_nick);
171
172 value &= ~fv->value;
173 }
174
175 if (value != 0)
176 {
177 name = g_type_name_from_class ((GTypeClass *) flags_class);
178
179 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
180 "unhandled value '%u' for flags '%s'", value, name);
181 return NULL;
182 }
183
184 return g_string_free (g_steal_pointer (&str), FALSE);
185 }
186
187 gboolean
188 bolt_flags_class_from_string (GFlagsClass *flags_class,
189 const char *string,
190 guint *flags_out,
191 GError **error)
192 {
193 g_auto(GStrv) vals = NULL;
194 const char *name;
195 guint flags = 0;
196
197 if (flags_class == NULL)
198 {
199 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
200 "could not determine flags class");
201
202 return FALSE;
203 }
204
205 if (string == NULL)
206 {
207 name = g_type_name_from_class ((GTypeClass *) flags_class);
208 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
209 "empty string passed for flags class for '%s'",
210 name);
211 return FALSE;
212 }
213
214 vals = g_strsplit (string, "|", -1);
215
216 for (guint i = 0; vals[i]; i++)
217 {
218 GFlagsValue *fv;
219 char *nick;
220
221 nick = g_strstrip (vals[i]);
222 fv = g_flags_get_value_by_nick (flags_class, nick);
223
224 if (fv == NULL)
225 {
226 name = g_type_name_from_class ((GTypeClass *) flags_class);
227 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
228 "invalid flag '%s' for flags '%s'", string, name);
229
230 return FALSE;
231 }
232
233 flags |= fv->value;
234 }
235
236 if (flags_out != NULL)
237 *flags_out = flags;
238
239 return TRUE;
240 }
241
242 char *
243 bolt_flags_to_string (GType flags_type,
244 guint value,
245 GError **error)
246 {
247 g_autoptr(GFlagsClass) klass = NULL;
248
249 klass = g_type_class_ref (flags_type);
250 return bolt_flags_class_to_string (klass, value, error);
251 }
252
253 gboolean
254 bolt_flags_from_string (GType flags_type,
255 const char *string,
256 guint *flags_out,
257 GError **error)
258 {
259 g_autoptr(GFlagsClass) klass = NULL;
260
261 klass = g_type_class_ref (flags_type);
262 return bolt_flags_class_from_string (klass, string, flags_out, error);
263 }
264
265 gboolean
266 bolt_flags_update (guint from,
267 guint *to,
268 guint mask)
269 {
270 guint val;
271 gboolean chg;
272
273 g_return_val_if_fail (to != NULL, FALSE);
274
275 val = *to & ~mask; /* clear all bits in mask */
276 val = val | (from & mask); /* set all bits in from and mask */
277 chg = *to != val;
278 *to = val;
279
280 return chg;
281 }
282
283 const char *
284 bolt_status_to_string (BoltStatus status)
285 {
286 return bolt_enum_to_string (BOLT_TYPE_STATUS, status, NULL);
287 }
288
289 gboolean
290 bolt_status_is_authorized (BoltStatus status)
291 {
292 return status == BOLT_STATUS_AUTHORIZED ||
293 status == BOLT_STATUS_AUTHORIZED_SECURE ||
294 status == BOLT_STATUS_AUTHORIZED_NEWKEY;
295 }
296
297 gboolean
298 bolt_status_is_pending (BoltStatus status)
299 {
300 return status == BOLT_STATUS_AUTH_ERROR ||
301 status == BOLT_STATUS_CONNECTED;
302 }
303
304 gboolean
305 bolt_status_validate (BoltStatus status)
306 {
307 return bolt_enum_validate (BOLT_TYPE_STATUS, status, NULL);
308 }
309
310 gboolean
311 bolt_status_is_connected (BoltStatus status)
312 {
313 return status > BOLT_STATUS_DISCONNECTED;
314 }
315
316 BoltSecurity
317 bolt_security_from_string (const char *str)
318 {
319 return bolt_enum_from_string (BOLT_TYPE_SECURITY, str, NULL);
320 }
321
322 const char *
323 bolt_security_to_string (BoltSecurity security)
324 {
325 return bolt_enum_to_string (BOLT_TYPE_SECURITY, security, NULL);
326 }
327
328 gboolean
329 bolt_security_validate (BoltSecurity security)
330 {
331 return bolt_enum_validate (BOLT_TYPE_SECURITY, security, NULL);
332 }
333
334 gboolean
335 bolt_security_allows_pcie (BoltSecurity security)
336 {
337 gboolean pcie = FALSE;
338
339 switch (security)
340 {
341 case BOLT_SECURITY_NONE:
342 case BOLT_SECURITY_USER:
343 case BOLT_SECURITY_SECURE:
344 pcie = TRUE;
345 break;
346
347 case BOLT_SECURITY_DPONLY:
348 case BOLT_SECURITY_USBONLY:
349 case BOLT_SECURITY_UNKNOWN:
350 pcie = FALSE;
351 break;
352 }
353
354 return pcie;
355 }
356
357 BoltPolicy
358 bolt_policy_from_string (const char *str)
359 {
360 return bolt_enum_from_string (BOLT_TYPE_POLICY, str, NULL);
361 }
362
363 const char *
364 bolt_policy_to_string (BoltPolicy policy)
365 {
366 return bolt_enum_to_string (BOLT_TYPE_POLICY, policy, NULL);
367 }
368
369 gboolean
370 bolt_policy_validate (BoltPolicy policy)
371 {
372 return bolt_enum_validate (BOLT_TYPE_POLICY, policy, NULL);
373 }
374
375 BoltDeviceType
376 bolt_device_type_from_string (const char *str)
377 {
378 return bolt_enum_from_string (BOLT_TYPE_DEVICE_TYPE, str, NULL);
379 }
380
381 const char *
382 bolt_device_type_to_string (BoltDeviceType type)
383 {
384 return bolt_enum_to_string (BOLT_TYPE_DEVICE_TYPE, type, NULL);
385 }
386
387 gboolean
388 bolt_device_type_validate (BoltDeviceType type)
389 {
390 return bolt_enum_validate (BOLT_TYPE_DEVICE_TYPE, type, NULL);
391 }
392
393 gboolean
394 bolt_device_type_is_host (BoltDeviceType type)
395 {
396 return type == BOLT_DEVICE_HOST;
397 }
398