-
Notifications
You must be signed in to change notification settings - Fork 410
/
fu-device.c
6670 lines (6114 loc) · 186 KB
/
fu-device.c
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2015 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#define G_LOG_DOMAIN "FuDevice"
#include "config.h"
#include <gio/gio.h>
#include <string.h>
#include "fwupd-common.h"
#include "fwupd-device-private.h"
#include "fu-bytes.h"
#include "fu-common.h"
#include "fu-device-private.h"
#include "fu-input-stream.h"
#include "fu-quirks.h"
#include "fu-security-attr.h"
#include "fu-string.h"
#include "fu-version-common.h"
#define FU_DEVICE_RETRY_OPEN_COUNT 5
#define FU_DEVICE_RETRY_OPEN_DELAY 500 /* ms */
/**
* FuDevice:
*
* A physical or logical device that is exported to the daemon.
*
* See also: [class@FuDeviceLocker], [class@Fwupd.Device]
*/
static void
fu_device_finalize(GObject *object);
static void
fu_device_inhibit_full(FuDevice *self,
FwupdDeviceProblem problem,
const gchar *inhibit_id,
const gchar *reason);
typedef struct {
gchar *equivalent_id;
gchar *physical_id;
gchar *logical_id;
gchar *backend_id;
gchar *update_request_id;
gchar *proxy_guid;
FuDevice *proxy; /* noref */
FuContext *ctx;
GHashTable *inhibits; /* (nullable) */
GHashTable *metadata; /* (nullable) */
GPtrArray *parent_guids;
GPtrArray *parent_physical_ids; /* (nullable) */
GPtrArray *parent_backend_ids; /* (nullable) */
guint remove_delay; /* ms */
guint acquiesce_delay; /* ms */
guint request_cnts[FWUPD_REQUEST_KIND_LAST];
gint order;
guint priority;
guint poll_id;
gint poll_locker_cnt;
gboolean done_probe;
gboolean done_setup;
gboolean device_id_valid;
guint64 size_min;
guint64 size_max;
gint open_refcount; /* atomic */
GType specialized_gtype;
GType proxy_gtype;
GType firmware_gtype;
GPtrArray *possible_plugins;
GPtrArray *instance_id_quirks; /* of utf-8 */
GPtrArray *retry_recs; /* of FuDeviceRetryRecovery */
guint retry_delay;
FuDeviceInternalFlags internal_flags;
guint64 private_flags;
GPtrArray *private_flag_items; /* (nullable) */
gchar *custom_flags;
gulong notify_flags_handler_id;
GHashTable *instance_hash;
FuProgress *progress; /* provided for FuDevice notify callbacks */
} FuDevicePrivate;
typedef struct {
GQuark domain;
gint code;
FuDeviceRetryFunc recovery_func;
} FuDeviceRetryRecovery;
typedef struct {
FwupdDeviceProblem problem;
gchar *inhibit_id;
gchar *reason;
} FuDeviceInhibit;
typedef struct {
guint64 value;
gchar *value_str;
} FuDevicePrivateFlagItem;
enum {
PROP_0,
PROP_PHYSICAL_ID,
PROP_LOGICAL_ID,
PROP_BACKEND_ID,
PROP_CONTEXT,
PROP_PROXY,
PROP_PARENT,
PROP_INTERNAL_FLAGS,
PROP_PRIVATE_FLAGS,
PROP_LAST
};
enum { SIGNAL_CHILD_ADDED, SIGNAL_CHILD_REMOVED, SIGNAL_REQUEST, SIGNAL_LAST };
static guint signals[SIGNAL_LAST] = {0};
G_DEFINE_TYPE_WITH_PRIVATE(FuDevice, fu_device, FWUPD_TYPE_DEVICE)
#define GET_PRIVATE(o) (fu_device_get_instance_private(o))
static FuDevicePrivateFlagItem *
fu_device_private_flag_item_find_by_val(FuDevice *self, guint64 value);
static void
fu_device_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
FuDevice *self = FU_DEVICE(object);
FuDevicePrivate *priv = GET_PRIVATE(self);
switch (prop_id) {
case PROP_PHYSICAL_ID:
g_value_set_string(value, priv->physical_id);
break;
case PROP_LOGICAL_ID:
g_value_set_string(value, priv->logical_id);
break;
case PROP_BACKEND_ID:
g_value_set_string(value, priv->backend_id);
break;
case PROP_CONTEXT:
g_value_set_object(value, priv->ctx);
break;
case PROP_PROXY:
g_value_set_object(value, priv->proxy);
break;
case PROP_PARENT:
g_value_set_object(value, fu_device_get_parent(self));
break;
case PROP_INTERNAL_FLAGS:
g_value_set_uint64(value, fu_device_get_internal_flags(self));
break;
case PROP_PRIVATE_FLAGS:
g_value_set_uint64(value, fu_device_get_private_flags(self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
fu_device_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
FuDevice *self = FU_DEVICE(object);
switch (prop_id) {
case PROP_PHYSICAL_ID:
fu_device_set_physical_id(self, g_value_get_string(value));
break;
case PROP_LOGICAL_ID:
fu_device_set_logical_id(self, g_value_get_string(value));
break;
case PROP_BACKEND_ID:
fu_device_set_backend_id(self, g_value_get_string(value));
break;
case PROP_CONTEXT:
fu_device_set_context(self, g_value_get_object(value));
break;
case PROP_PROXY:
fu_device_set_proxy(self, g_value_get_object(value));
break;
case PROP_PARENT:
fu_device_set_parent(self, g_value_get_object(value));
break;
case PROP_INTERNAL_FLAGS:
fu_device_set_internal_flags(self, g_value_get_uint64(value));
break;
case PROP_PRIVATE_FLAGS:
fu_device_set_private_flags(self, g_value_get_uint64(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
/**
* fu_device_internal_flag_to_string:
* @flag: an internal device flag, e.g. %FU_DEVICE_INTERNAL_FLAG_MD_SET_ICON
*
* Converts an internal device flag to a string.
*
* Returns: identifier string
*
* Since: 1.5.5
**/
const gchar *
fu_device_internal_flag_to_string(FuDeviceInternalFlags flag)
{
if (flag == FU_DEVICE_INTERNAL_FLAG_MD_SET_ICON)
return "md-set-icon";
if (flag == FU_DEVICE_INTERNAL_FLAG_MD_SET_NAME)
return "md-set-name";
if (flag == FU_DEVICE_INTERNAL_FLAG_MD_SET_NAME_CATEGORY)
return "md-set-name-category";
if (flag == FU_DEVICE_INTERNAL_FLAG_MD_SET_VERFMT)
return "md-set-verfmt";
if (flag == FU_DEVICE_INTERNAL_FLAG_ONLY_SUPPORTED)
return "only-supported";
if (flag == FU_DEVICE_INTERNAL_FLAG_NO_AUTO_INSTANCE_IDS)
return "no-auto-instance-ids";
if (flag == FU_DEVICE_INTERNAL_FLAG_ENSURE_SEMVER)
return "ensure-semver";
if (flag == FU_DEVICE_INTERNAL_FLAG_RETRY_OPEN)
return "retry-open";
if (flag == FU_DEVICE_INTERNAL_FLAG_REPLUG_MATCH_GUID)
return "replug-match-guid";
if (flag == FU_DEVICE_INTERNAL_FLAG_INHERIT_ACTIVATION)
return "inherit-activation";
if (flag == FU_DEVICE_INTERNAL_FLAG_IS_OPEN)
return "is-open";
if (flag == FU_DEVICE_INTERNAL_FLAG_NO_SERIAL_NUMBER)
return "no-serial-number";
if (flag == FU_DEVICE_INTERNAL_FLAG_AUTO_PARENT_CHILDREN)
return "auto-parent-children";
if (flag == FU_DEVICE_INTERNAL_FLAG_ATTACH_EXTRA_RESET)
return "attach-extra-reset";
if (flag == FU_DEVICE_INTERNAL_FLAG_INHIBIT_CHILDREN)
return "inhibit-children";
if (flag == FU_DEVICE_INTERNAL_FLAG_NO_AUTO_REMOVE_CHILDREN)
return "no-auto-remove-children";
if (flag == FU_DEVICE_INTERNAL_FLAG_USE_PARENT_FOR_OPEN)
return "use-parent-for-open";
if (flag == FU_DEVICE_INTERNAL_FLAG_USE_PROXY_FOR_OPEN)
return "use-proxy-for-open";
if (flag == FU_DEVICE_INTERNAL_FLAG_USE_PARENT_FOR_BATTERY)
return "use-parent-for-battery";
if (flag == FU_DEVICE_INTERNAL_FLAG_USE_PROXY_FALLBACK)
return "use-proxy-fallback";
if (flag == FU_DEVICE_INTERNAL_FLAG_NO_AUTO_REMOVE)
return "no-auto-remove";
if (flag == FU_DEVICE_INTERNAL_FLAG_MD_SET_VENDOR)
return "md-set-vendor";
if (flag == FU_DEVICE_INTERNAL_FLAG_NO_LID_CLOSED)
return "no-lid-closed";
if (flag == FU_DEVICE_INTERNAL_FLAG_NO_PROBE)
return "no-probe";
if (flag == FU_DEVICE_INTERNAL_FLAG_MD_SET_SIGNED)
return "md-set-signed";
if (flag == FU_DEVICE_INTERNAL_AUTO_PAUSE_POLLING)
return "auto-pause-polling";
if (flag == FU_DEVICE_INTERNAL_FLAG_ONLY_WAIT_FOR_REPLUG)
return "only-wait-for-replug";
if (flag == FU_DEVICE_INTERNAL_FLAG_IGNORE_SYSTEM_POWER)
return "ignore-system-power";
if (flag == FU_DEVICE_INTERNAL_FLAG_NO_PROBE_COMPLETE)
return "no-probe-complete";
if (flag == FU_DEVICE_INTERNAL_FLAG_SAVE_INTO_BACKUP_REMOTE)
return "save-into-backup-remote";
if (flag == FU_DEVICE_INTERNAL_FLAG_MD_SET_FLAGS)
return "md-set-flags";
if (flag == FU_DEVICE_INTERNAL_FLAG_MD_SET_VERSION)
return "md-set-version";
if (flag == FU_DEVICE_INTERNAL_FLAG_MD_ONLY_CHECKSUM)
return "md-only-checksum";
if (flag == FU_DEVICE_INTERNAL_FLAG_ADD_INSTANCE_ID_REV)
return "add-instance-id-rev";
if (flag == FU_DEVICE_INTERNAL_FLAG_UNCONNECTED)
return "unconnected";
if (flag == FU_DEVICE_INTERNAL_FLAG_DISPLAY_REQUIRED)
return "display-required";
if (flag == FU_DEVICE_INTERNAL_FLAG_UPDATE_PENDING)
return "update-pending";
if (flag == FU_DEVICE_INTERNAL_FLAG_NO_GENERIC_GUIDS)
return "no-generic-guids";
if (flag == FU_DEVICE_INTERNAL_FLAG_ENFORCE_REQUIRES)
return "enforce-requires";
if (flag == FU_DEVICE_INTERNAL_FLAG_HOST_FIRMWARE)
return "host-firmware";
if (flag == FU_DEVICE_INTERNAL_FLAG_HOST_FIRMWARE_CHILD)
return "host-firmware-child";
if (flag == FU_DEVICE_INTERNAL_FLAG_HOST_CPU)
return "host-cpu";
if (flag == FU_DEVICE_INTERNAL_FLAG_HOST_CPU_CHILD)
return "host-cpu-child";
if (flag == FU_DEVICE_INTERNAL_FLAG_EXPLICIT_ORDER)
return "explicit-order";
if (flag == FU_DEVICE_INTERNAL_FLAG_REFCOUNTED_PROXY)
return "refcounted-proxy";
return NULL;
}
/**
* fu_device_internal_flag_from_string:
* @flag: a string, e.g. `md-set-icon`
*
* Converts a string to an internal device flag.
*
* Returns: enumerated value
*
* Since: 1.5.5
**/
FuDeviceInternalFlags
fu_device_internal_flag_from_string(const gchar *flag)
{
if (g_strcmp0(flag, "md-set-icon") == 0)
return FU_DEVICE_INTERNAL_FLAG_MD_SET_ICON;
if (g_strcmp0(flag, "md-set-name") == 0)
return FU_DEVICE_INTERNAL_FLAG_MD_SET_NAME;
if (g_strcmp0(flag, "md-set-name-category") == 0)
return FU_DEVICE_INTERNAL_FLAG_MD_SET_NAME_CATEGORY;
if (g_strcmp0(flag, "md-set-verfmt") == 0)
return FU_DEVICE_INTERNAL_FLAG_MD_SET_VERFMT;
if (g_strcmp0(flag, "only-supported") == 0)
return FU_DEVICE_INTERNAL_FLAG_ONLY_SUPPORTED;
if (g_strcmp0(flag, "no-auto-instance-ids") == 0)
return FU_DEVICE_INTERNAL_FLAG_NO_AUTO_INSTANCE_IDS;
if (g_strcmp0(flag, "ensure-semver") == 0)
return FU_DEVICE_INTERNAL_FLAG_ENSURE_SEMVER;
if (g_strcmp0(flag, "retry-open") == 0)
return FU_DEVICE_INTERNAL_FLAG_RETRY_OPEN;
if (g_strcmp0(flag, "replug-match-guid") == 0)
return FU_DEVICE_INTERNAL_FLAG_REPLUG_MATCH_GUID;
if (g_strcmp0(flag, "inherit-activation") == 0)
return FU_DEVICE_INTERNAL_FLAG_INHERIT_ACTIVATION;
if (g_strcmp0(flag, "is-open") == 0)
return FU_DEVICE_INTERNAL_FLAG_IS_OPEN;
if (g_strcmp0(flag, "no-serial-number") == 0)
return FU_DEVICE_INTERNAL_FLAG_NO_SERIAL_NUMBER;
if (g_strcmp0(flag, "auto-parent-children") == 0)
return FU_DEVICE_INTERNAL_FLAG_AUTO_PARENT_CHILDREN;
if (g_strcmp0(flag, "attach-extra-reset") == 0)
return FU_DEVICE_INTERNAL_FLAG_ATTACH_EXTRA_RESET;
if (g_strcmp0(flag, "inhibit-children") == 0)
return FU_DEVICE_INTERNAL_FLAG_INHIBIT_CHILDREN;
if (g_strcmp0(flag, "no-auto-remove-children") == 0)
return FU_DEVICE_INTERNAL_FLAG_NO_AUTO_REMOVE_CHILDREN;
if (g_strcmp0(flag, "use-parent-for-open") == 0)
return FU_DEVICE_INTERNAL_FLAG_USE_PARENT_FOR_OPEN;
if (g_strcmp0(flag, "use-proxy-for-open") == 0)
return FU_DEVICE_INTERNAL_FLAG_USE_PROXY_FOR_OPEN;
if (g_strcmp0(flag, "use-parent-for-battery") == 0)
return FU_DEVICE_INTERNAL_FLAG_USE_PARENT_FOR_BATTERY;
if (g_strcmp0(flag, "use-proxy-fallback") == 0)
return FU_DEVICE_INTERNAL_FLAG_USE_PROXY_FALLBACK;
if (g_strcmp0(flag, "no-auto-remove") == 0)
return FU_DEVICE_INTERNAL_FLAG_NO_AUTO_REMOVE;
if (g_strcmp0(flag, "md-set-vendor") == 0)
return FU_DEVICE_INTERNAL_FLAG_MD_SET_VENDOR;
if (g_strcmp0(flag, "no-lid-closed") == 0)
return FU_DEVICE_INTERNAL_FLAG_NO_LID_CLOSED;
if (g_strcmp0(flag, "no-probe") == 0)
return FU_DEVICE_INTERNAL_FLAG_NO_PROBE;
if (g_strcmp0(flag, "md-set-signed") == 0)
return FU_DEVICE_INTERNAL_FLAG_MD_SET_SIGNED;
if (g_strcmp0(flag, "auto-pause-polling") == 0)
return FU_DEVICE_INTERNAL_AUTO_PAUSE_POLLING;
if (g_strcmp0(flag, "only-wait-for-replug") == 0)
return FU_DEVICE_INTERNAL_FLAG_ONLY_WAIT_FOR_REPLUG;
if (g_strcmp0(flag, "ignore-system-power") == 0)
return FU_DEVICE_INTERNAL_FLAG_IGNORE_SYSTEM_POWER;
if (g_strcmp0(flag, "no-probe-complete") == 0)
return FU_DEVICE_INTERNAL_FLAG_NO_PROBE_COMPLETE;
if (g_strcmp0(flag, "save-into-backup-remote") == 0)
return FU_DEVICE_INTERNAL_FLAG_SAVE_INTO_BACKUP_REMOTE;
if (g_strcmp0(flag, "md-set-flags") == 0)
return FU_DEVICE_INTERNAL_FLAG_MD_SET_FLAGS;
if (g_strcmp0(flag, "md-set-version") == 0)
return FU_DEVICE_INTERNAL_FLAG_MD_SET_VERSION;
if (g_strcmp0(flag, "md-only-checksum") == 0)
return FU_DEVICE_INTERNAL_FLAG_MD_ONLY_CHECKSUM;
if (g_strcmp0(flag, "add-instance-id-rev") == 0)
return FU_DEVICE_INTERNAL_FLAG_ADD_INSTANCE_ID_REV;
if (g_strcmp0(flag, "unconnected") == 0)
return FU_DEVICE_INTERNAL_FLAG_UNCONNECTED;
if (g_strcmp0(flag, "display-required") == 0)
return FU_DEVICE_INTERNAL_FLAG_DISPLAY_REQUIRED;
if (g_strcmp0(flag, "update-pending") == 0)
return FU_DEVICE_INTERNAL_FLAG_UPDATE_PENDING;
if (g_strcmp0(flag, "no-generic-guids") == 0)
return FU_DEVICE_INTERNAL_FLAG_NO_GENERIC_GUIDS;
if (g_strcmp0(flag, "enforce-requires") == 0)
return FU_DEVICE_INTERNAL_FLAG_ENFORCE_REQUIRES;
if (g_strcmp0(flag, "host-firmware") == 0)
return FU_DEVICE_INTERNAL_FLAG_HOST_FIRMWARE;
if (g_strcmp0(flag, "host-firmware-child") == 0)
return FU_DEVICE_INTERNAL_FLAG_HOST_FIRMWARE_CHILD;
if (g_strcmp0(flag, "host-cpu") == 0)
return FU_DEVICE_INTERNAL_FLAG_HOST_CPU;
if (g_strcmp0(flag, "host-cpu-child") == 0)
return FU_DEVICE_INTERNAL_FLAG_HOST_CPU_CHILD;
if (g_strcmp0(flag, "explicit-order") == 0)
return FU_DEVICE_INTERNAL_FLAG_EXPLICIT_ORDER;
if (g_strcmp0(flag, "refcounted-proxy") == 0)
return FU_DEVICE_INTERNAL_FLAG_REFCOUNTED_PROXY;
return FU_DEVICE_INTERNAL_FLAG_UNKNOWN;
}
/**
* fu_device_add_internal_flag:
* @self: a #FuDevice
* @flag: an internal device flag, e.g. %FU_DEVICE_INTERNAL_FLAG_MD_SET_ICON
*
* Adds a private flag that stays internal to the engine and is not leaked to the client.
*
* Since: 1.5.5
**/
void
fu_device_add_internal_flag(FuDevice *self, FuDeviceInternalFlags flag)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_DEVICE(self));
/* do not let devices be updated until re-connected */
if (flag & FU_DEVICE_INTERNAL_FLAG_UNCONNECTED)
fu_device_inhibit(self, "unconnected", "Device has been removed");
/* reset this back to the default */
if (flag & FU_DEVICE_INTERNAL_FLAG_EXPLICIT_ORDER) {
GPtrArray *children = fu_device_get_children(self);
for (guint i = 0; i < children->len; i++) {
FuDevice *child_tmp = g_ptr_array_index(children, i);
fu_device_add_internal_flag(child_tmp,
FU_DEVICE_INTERNAL_FLAG_EXPLICIT_ORDER);
}
fu_device_set_order(self, G_MAXINT);
}
priv->internal_flags |= flag;
g_object_notify(G_OBJECT(self), "internal-flags");
}
/**
* fu_device_remove_internal_flag:
* @self: a #FuDevice
* @flag: an internal device flag, e.g. %FU_DEVICE_INTERNAL_FLAG_MD_SET_ICON
*
* Removes a private flag that stays internal to the engine and is not leaked to the client.
*
* Since: 1.5.5
**/
void
fu_device_remove_internal_flag(FuDevice *self, FuDeviceInternalFlags flag)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_DEVICE(self));
if (flag & FU_DEVICE_INTERNAL_FLAG_UNCONNECTED)
fu_device_uninhibit(self, "unconnected");
priv->internal_flags &= ~flag;
g_object_notify(G_OBJECT(self), "internal-flags");
}
/**
* fu_device_has_internal_flag:
* @self: a #FuDevice
* @flag: an internal device flag, e.g. %FU_DEVICE_INTERNAL_FLAG_MD_SET_ICON
*
* Tests for a private flag that stays internal to the engine and is not leaked to the client.
*
* Since: 1.5.5
**/
gboolean
fu_device_has_internal_flag(FuDevice *self, FuDeviceInternalFlags flag)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_DEVICE(self), FALSE);
return (priv->internal_flags & flag) > 0;
}
/**
* fu_device_get_internal_flags:
* @self: a #FuDevice
*
* Gets all the internal flags.
*
* Returns: flags, e.g. %FU_DEVICE_INTERNAL_FLAG_MD_SET_ICON
*
* Since: 1.7.1
**/
FuDeviceInternalFlags
fu_device_get_internal_flags(FuDevice *self)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_DEVICE(self), FU_DEVICE_INTERNAL_FLAG_UNKNOWN);
return priv->internal_flags;
}
/**
* fu_device_set_internal_flags:
* @self: a #FuDevice
* @flags: internal device flags, e.g. %FU_DEVICE_INTERNAL_FLAG_MD_SET_ICON
*
* Sets the internal flags.
*
* Since: 1.7.1
**/
void
fu_device_set_internal_flags(FuDevice *self, FuDeviceInternalFlags flags)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_DEVICE(self));
priv->internal_flags = flags;
g_object_notify(G_OBJECT(self), "internal-flags");
}
/**
* fu_device_add_private_flag:
* @self: a #FuDevice
* @flag: a device flag
*
* Adds a private flag that can be used by the plugin for any purpose.
*
* Since: 1.6.2
**/
void
fu_device_add_private_flag(FuDevice *self, guint64 flag)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_DEVICE(self));
#ifndef SUPPORTED_BUILD
if (fu_device_private_flag_item_find_by_val(self, flag) == NULL) {
g_critical("%s flag 0x%x is unknown -- use fu_device_register_private_flag()",
G_OBJECT_TYPE_NAME(self),
(guint)flag);
}
#endif
priv->private_flags |= flag;
g_object_notify(G_OBJECT(self), "private-flags");
}
/**
* fu_device_remove_private_flag:
* @self: a #FuDevice
* @flag: a device flag
*
* Removes a private flag that can be used by the plugin for any purpose.
*
* Since: 1.6.2
**/
void
fu_device_remove_private_flag(FuDevice *self, guint64 flag)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_DEVICE(self));
#ifndef SUPPORTED_BUILD
if (fu_device_private_flag_item_find_by_val(self, flag) == NULL) {
g_critical("%s flag 0x%x is unknown -- use fu_device_register_private_flag()",
G_OBJECT_TYPE_NAME(self),
(guint)flag);
}
#endif
priv->private_flags &= ~flag;
g_object_notify(G_OBJECT(self), "private-flags");
}
/**
* fu_device_has_private_flag:
* @self: a #FuDevice
* @flag: a device flag
*
* Tests for a private flag that can be used by the plugin for any purpose.
*
* Since: 1.6.2
**/
gboolean
fu_device_has_private_flag(FuDevice *self, guint64 flag)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_DEVICE(self), FALSE);
#ifndef SUPPORTED_BUILD
if (fu_device_private_flag_item_find_by_val(self, flag) == NULL) {
g_critical("%s flag 0x%x is unknown -- use fu_device_register_private_flag()",
G_OBJECT_TYPE_NAME(self),
(guint)flag);
}
#endif
return (priv->private_flags & flag) > 0;
}
/**
* fu_device_get_private_flags:
* @self: a #FuDevice
*
* Returns all the private flags that can be used by the plugin for any purpose.
*
* Returns: flags
*
* Since: 1.6.2
**/
guint64
fu_device_get_private_flags(FuDevice *self)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_DEVICE(self), G_MAXUINT64);
return priv->private_flags;
}
/**
* fu_device_get_request_cnt:
* @self: a #FuDevice
* @request_kind: the type of request
*
* Returns the number of requests of a specific kind. This function is only
* useful to the daemon, which uses it to synthesize artificial events for
* plugins not yet ported to [class@Fwupd.Request].
*
* Returns: integer, usually 0
*
* Since: 1.6.2
**/
guint
fu_device_get_request_cnt(FuDevice *self, FwupdRequestKind request_kind)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_DEVICE(self), G_MAXUINT);
g_return_val_if_fail(request_kind < FWUPD_REQUEST_KIND_LAST, G_MAXUINT);
return priv->request_cnts[request_kind];
}
/**
* fu_device_set_private_flags:
* @self: a #FuDevice
* @flag: flags
*
* Sets private flags that can be used by the plugin for any purpose.
*
* Since: 1.6.2
**/
void
fu_device_set_private_flags(FuDevice *self, guint64 flag)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_DEVICE(self));
priv->private_flags = flag;
g_object_notify(G_OBJECT(self), "private-flags");
}
/**
* fu_device_get_possible_plugins:
* @self: a #FuDevice
*
* Gets the list of possible plugin names, typically added from quirk files.
*
* Returns: (element-type utf8) (transfer container): plugin names
*
* Since: 1.3.3
**/
GPtrArray *
fu_device_get_possible_plugins(FuDevice *self)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
return g_ptr_array_ref(priv->possible_plugins);
}
/**
* fu_device_add_possible_plugin:
* @self: a #FuDevice
* @plugin: a plugin name, e.g. `dfu`
*
* Adds a plugin name to the list of plugins that *might* be able to handle this
* device. This is typically called from a quirk handler.
*
* Duplicate plugin names are ignored.
*
* Since: 1.5.1
**/
void
fu_device_add_possible_plugin(FuDevice *self, const gchar *plugin)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_DEVICE(self));
g_return_if_fail(plugin != NULL);
/* add if it does not already exist */
if (g_ptr_array_find_with_equal_func(priv->possible_plugins, plugin, g_str_equal, NULL))
return;
g_ptr_array_add(priv->possible_plugins, g_strdup(plugin));
}
/**
* fu_device_retry_add_recovery:
* @self: a #FuDevice
* @domain: a #GQuark, or %0 for all domains
* @code: a #GError code
* @func: (scope async) (nullable): a function to recover the device
*
* Sets the optional function to be called when fu_device_retry() fails, which
* is possibly a device reset.
*
* If @func is %NULL then recovery is not possible and an error is returned
* straight away.
*
* Since: 1.4.0
**/
void
fu_device_retry_add_recovery(FuDevice *self, GQuark domain, gint code, FuDeviceRetryFunc func)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
FuDeviceRetryRecovery *rec;
g_return_if_fail(FU_IS_DEVICE(self));
g_return_if_fail(domain != 0);
rec = g_new(FuDeviceRetryRecovery, 1);
rec->domain = domain;
rec->code = code;
rec->recovery_func = func;
g_ptr_array_add(priv->retry_recs, rec);
}
/**
* fu_device_retry_set_delay:
* @self: a #FuDevice
* @delay: delay in ms
*
* Sets the recovery delay between failed retries.
*
* Since: 1.4.0
**/
void
fu_device_retry_set_delay(FuDevice *self, guint delay)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_DEVICE(self));
priv->retry_delay = delay;
}
/**
* fu_device_retry_full:
* @self: a #FuDevice
* @func: (scope async) (closure user_data): a function to execute
* @count: the number of tries to try the function
* @delay: the delay between each try in ms
* @user_data: (nullable): a helper to pass to @func
* @error: (nullable): optional return location for an error
*
* Calls a specific function a number of times, optionally handling the error
* with a reset action.
*
* If fu_device_retry_add_recovery() has not been used then all errors are
* considered non-fatal until the last try.
*
* If the reset function returns %FALSE, then the function returns straight away
* without processing any pending retries.
*
* Since: 1.5.5
**/
gboolean
fu_device_retry_full(FuDevice *self,
FuDeviceRetryFunc func,
guint count,
guint delay,
gpointer user_data,
GError **error)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_DEVICE(self), FALSE);
g_return_val_if_fail(func != NULL, FALSE);
g_return_val_if_fail(count >= 1, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
for (guint i = 0;; i++) {
g_autoptr(GError) error_local = NULL;
/* delay */
if (i > 0)
fu_device_sleep(self, delay);
/* run function, if success return success */
if (func(self, user_data, &error_local))
break;
/* sanity check */
if (error_local == NULL) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"exec failed but no error set!");
return FALSE;
}
/* too many retries */
if (i >= count - 1) {
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"failed after %u retries: ",
count);
return FALSE;
}
/* show recoverable error on the console */
if (priv->retry_recs->len == 0) {
g_info("failed on try %u of %u: %s", i + 1, count, error_local->message);
continue;
}
/* find the condition that matches */
for (guint j = 0; j < priv->retry_recs->len; j++) {
FuDeviceRetryRecovery *rec = g_ptr_array_index(priv->retry_recs, j);
if (g_error_matches(error_local, rec->domain, rec->code)) {
if (rec->recovery_func != NULL) {
if (!rec->recovery_func(self, user_data, error))
return FALSE;
} else {
g_propagate_prefixed_error(
error,
g_steal_pointer(&error_local),
"device recovery not possible: ");
return FALSE;
}
}
}
}
/* success */
return TRUE;
}
/**
* fu_device_retry:
* @self: a #FuDevice
* @func: (scope async) (closure user_data): a function to execute
* @count: the number of tries to try the function
* @user_data: (nullable): a helper to pass to @func
* @error: (nullable): optional return location for an error
*
* Calls a specific function a number of times, optionally handling the error
* with a reset action.
*
* If fu_device_retry_add_recovery() has not been used then all errors are
* considered non-fatal until the last try.
*
* If the reset function returns %FALSE, then the function returns straight away
* without processing any pending retries.
*
* Since: 1.4.0
**/
gboolean
fu_device_retry(FuDevice *self,
FuDeviceRetryFunc func,
guint count,
gpointer user_data,
GError **error)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
return fu_device_retry_full(self, func, count, priv->retry_delay, user_data, error);
}
/**
* fu_device_sleep:
* @self: a #FuDevice
* @delay_ms: delay in milliseconds
*
* Delays program execution up to 100 seconds, unless the device is emulated where no delays is
* performed.
*
* Long unavoidable delays (more than 1 second) should really use `fu_device_sleep_full()` so that
* the percentage progress bar is updated.
*
* Since: 1.8.11
**/
void
fu_device_sleep(FuDevice *self, guint delay_ms)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_DEVICE(self));
g_return_if_fail(delay_ms < 100000);
if (fu_device_has_flag(self, FWUPD_DEVICE_FLAG_EMULATED))
return;
if (priv->proxy != NULL && fu_device_has_flag(priv->proxy, FWUPD_DEVICE_FLAG_EMULATED))
return;
if (delay_ms > 0)
g_usleep(delay_ms * 1000);
}
/**
* fu_device_sleep_full:
* @self: a #FuDevice
* @delay_ms: delay in milliseconds
* @progress: a #FuProgress
*
* Delays program execution up to 1000 seconds, unless the device is emulated where no delays is
* performed.
*
* Since: 1.8.11
**/
void
fu_device_sleep_full(FuDevice *self, guint delay_ms, FuProgress *progress)
{
FuDevicePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_DEVICE(self));
g_return_if_fail(delay_ms < 1000000);
g_return_if_fail(FU_IS_PROGRESS(progress));
if (fu_device_has_flag(self, FWUPD_DEVICE_FLAG_EMULATED))
return;
if (priv->proxy != NULL && fu_device_has_flag(priv->proxy, FWUPD_DEVICE_FLAG_EMULATED))
return;
if (delay_ms > 0)
fu_progress_sleep(progress, delay_ms);
}
static gboolean
fu_device_poll_locker_open_cb(GObject *device, GError **error)
{
FuDevice *self = FU_DEVICE(device);
FuDevicePrivate *priv = GET_PRIVATE(self);
g_atomic_int_inc(&priv->poll_locker_cnt);
return TRUE;
}
static gboolean
fu_device_poll_locker_close_cb(GObject *device, GError **error)
{
FuDevice *self = FU_DEVICE(device);
FuDevicePrivate *priv = GET_PRIVATE(self);
g_atomic_int_dec_and_test(&priv->poll_locker_cnt);
return TRUE;
}
/**
* fu_device_poll_locker_new:
* @self: a #FuDevice
* @error: (nullable): optional return location for an error
*
* Returns a device locker that prevents polling on the device. If there are no open poll lockers
* then the poll callback will be called.
*
* Use %FU_DEVICE_INTERNAL_AUTO_PAUSE_POLLING to opt into this functionality.
*
* Returns: (transfer full): a #FuDeviceLocker
*
* Since: 1.8.1
**/
FuDeviceLocker *
fu_device_poll_locker_new(FuDevice *self, GError **error)
{
g_return_val_if_fail(FU_IS_DEVICE(self), NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
return fu_device_locker_new_full(self,
fu_device_poll_locker_open_cb,
fu_device_poll_locker_close_cb,
error);
}
/**
* fu_device_poll:
* @self: a #FuDevice
* @error: (nullable): optional return location for an error
*
* Polls a device, typically querying the hardware for status.
*
* Returns: %TRUE for success
*
* Since: 1.1.2
**/
gboolean
fu_device_poll(FuDevice *self, GError **error)
{
FuDeviceClass *device_class = FU_DEVICE_GET_CLASS(self);
g_return_val_if_fail(FU_IS_DEVICE(self), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* subclassed */
if (device_class->poll != NULL) {
if (!device_class->poll(self, error))
return FALSE;
}
return TRUE;
}
static gboolean
fu_device_poll_cb(gpointer user_data)
{
FuDevice *self = FU_DEVICE(user_data);
FuDevicePrivate *priv = GET_PRIVATE(self);
g_autoptr(GError) error_local = NULL;
/* device is being detached, written, read, or attached */
if (fu_device_has_internal_flag(self, FU_DEVICE_INTERNAL_AUTO_PAUSE_POLLING) &&