GCC Code Coverage Report


Directory: ./
File: src/ppd-action-trickle-charge.c
Date: 2024-09-13 00:56:02
Exec Total Coverage
Lines: 49 58 84.5%
Functions: 8 9 88.9%
Branches: 16 25 64.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2020 Bastien Nocera <hadess@hadess.net>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3 as published by
6 * the Free Software Foundation.
7 *
8 */
9
10 #define G_LOG_DOMAIN "TrickleChargeAction"
11
12 #include <gudev/gudev.h>
13
14 #include "ppd-action-trickle-charge.h"
15 #include "ppd-profile.h"
16 #include "ppd-utils.h"
17
18 #define CHARGE_TYPE_SYSFS_NAME "charge_type"
19
20 struct _PpdActionTrickleCharge
21 {
22 PpdAction parent_instance;
23
24 GUdevClient *client;
25 gboolean active;
26 };
27
28
4/5
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 130 times.
✓ Branch 3 taken 130 times.
✗ Branch 4 not taken.
788 G_DEFINE_TYPE (PpdActionTrickleCharge, ppd_action_trickle_charge, PPD_TYPE_ACTION)
29
30 static GObject*
31 134 ppd_action_trickle_charge_constructor (GType type,
32 guint n_construct_params,
33 GObjectConstructParam *construct_params)
34 {
35 134 GObject *object;
36
37 134 object = G_OBJECT_CLASS (ppd_action_trickle_charge_parent_class)->constructor (type,
38 n_construct_params,
39 construct_params);
40 134 g_object_set (object,
41 "action-name", "trickle_charge",
42 NULL);
43
44 134 return object;
45 }
46
47 static void
48 270 set_charge_type (PpdActionTrickleCharge *action,
49 const char *charge_type)
50 {
51 270 GList *devices, *l;
52
53 270 devices = g_udev_client_query_by_subsystem (action->client, "power_supply");
54
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 260 times.
270 if (devices == NULL)
55 return;
56
57
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 6 times.
16 for (l = devices; l != NULL; l = l->next) {
58 10 GUdevDevice *dev = l->data;
59 10 const char *value;
60
61
2/2
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 6 times.
10 if (g_strcmp0 (g_udev_device_get_sysfs_attr (dev, "scope"), "Device") != 0)
62 4 continue;
63
64 6 value = g_udev_device_get_sysfs_attr_uncached (dev, CHARGE_TYPE_SYSFS_NAME);
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!value)
66 continue;
67
68
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
6 if (g_strcmp0 (charge_type, value) == 0)
69 2 continue;
70
71 4 ppd_utils_write_sysfs (dev, CHARGE_TYPE_SYSFS_NAME, charge_type, NULL);
72
73 4 break;
74 }
75
76 10 g_list_free_full (devices, g_object_unref);
77 }
78
79 static gboolean
80 270 ppd_action_trickle_charge_activate_profile (PpdAction *action,
81 PpdProfile profile,
82 GError **error)
83 {
84 270 PpdActionTrickleCharge *self = PPD_ACTION_TRICKLE_CHARGE (action);
85
86
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 210 times.
270 if (profile == PPD_PROFILE_POWER_SAVER) {
87 60 set_charge_type (self, "Trickle");
88 60 self->active = TRUE;
89 } else {
90 210 set_charge_type (self, "Fast");
91 210 self->active = FALSE;
92 }
93
94 270 return TRUE;
95 }
96
97 static void
98 uevent_cb (GUdevClient *client,
99 gchar *action,
100 GUdevDevice *device,
101 gpointer user_data)
102 {
103 PpdActionTrickleCharge *self = user_data;
104 const char *charge_type;
105
106 if (g_strcmp0 (action, "add") != 0)
107 return;
108
109 if (!g_udev_device_has_sysfs_attr (device, CHARGE_TYPE_SYSFS_NAME))
110 return;
111
112 charge_type = self->active ? "Trickle" : "Fast";
113 g_debug ("Updating charge type for '%s' to '%s'",
114 g_udev_device_get_sysfs_path (device),
115 charge_type);
116 ppd_utils_write_sysfs (device, CHARGE_TYPE_SYSFS_NAME, charge_type, NULL);
117 }
118
119 static void
120 134 ppd_action_trickle_charge_finalize (GObject *object)
121 {
122 134 PpdActionTrickleCharge *driver;
123
124 134 driver = PPD_ACTION_TRICKLE_CHARGE (object);
125
1/2
✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
134 g_clear_object (&driver->client);
126 134 G_OBJECT_CLASS (ppd_action_trickle_charge_parent_class)->finalize (object);
127 134 }
128
129 static void
130 130 ppd_action_trickle_charge_class_init (PpdActionTrickleChargeClass *klass)
131 {
132 130 GObjectClass *object_class;
133 130 PpdActionClass *driver_class;
134
135 130 object_class = G_OBJECT_CLASS (klass);
136 130 object_class->constructor = ppd_action_trickle_charge_constructor;
137 130 object_class->finalize = ppd_action_trickle_charge_finalize;
138
139 130 driver_class = PPD_ACTION_CLASS (klass);
140 130 driver_class->activate_profile = ppd_action_trickle_charge_activate_profile;
141 }
142
143 static void
144 134 ppd_action_trickle_charge_init (PpdActionTrickleCharge *self)
145 {
146 134 const gchar * const subsystem[] = { "power_supply", NULL };
147
148 134 self->client = g_udev_client_new (subsystem);
149 134 g_signal_connect (G_OBJECT (self->client), "uevent",
150 G_CALLBACK (uevent_cb), self);
151 134 }
152