Skip to content

Commit ba4395e

Browse files
ptr1337spandruvada
authored andcommitted
Add GetState D-Bus method to query current LPM control mode
Expose the current daemon state (OFF/ON/AUTO/FREEZE/TERMINATE) via a new GetState D-Bus method and a STATUS command in intel_lpmd_control, so clients can query the active mode without having to track it externally. Signed-off-by: Peter Jung <ptr1337@archlinux.org>
1 parent ba2ad54 commit ba4395e

3 files changed

Lines changed: 54 additions & 5 deletions

File tree

src/intel_lpmd_dbus_interface.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@
1919
<method name="LPM_AUTO">
2020
</method>
2121

22+
<method name="GetState">
23+
<arg type="s" name="state" direction="out"/>
24+
</method>
25+
2226
</interface>
2327
</node>

src/lpmd_dbus_server.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,23 @@ lpmd_dbus_handle_method_call(GDBusConnection *connection,
155155
return;
156156
}
157157

158+
if (g_strcmp0(method_name, "GetState") == 0) {
159+
const char *state_names[] = {
160+
[LPMD_OFF] = "OFF",
161+
[LPMD_ON] = "ON",
162+
[LPMD_AUTO] = "AUTO",
163+
[LPMD_FREEZE] = "FREEZE",
164+
[LPMD_RESTORE] = "RESTORE",
165+
[LPMD_TERMINATE] = "TERMINATE",
166+
};
167+
int state = get_lpmd_state();
168+
const char *name = (state >= 0 && state <= LPMD_TERMINATE) ? state_names[state] : "UNKNOWN";
169+
170+
g_dbus_method_invocation_return_value(invocation,
171+
g_variant_new("(s)", name));
172+
return;
173+
}
174+
158175
g_set_error(&error,
159176
G_DBUS_ERROR,
160177
G_DBUS_ERROR_UNKNOWN_METHOD,

tools/intel_lpmd_control.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,42 @@ main(int argc, char **argv)
5151
if (argc < 2) {
5252
fprintf (stderr, "intel_lpmd_control: missing control command\n");
5353
fprintf (stderr, "syntax:\n");
54-
fprintf (stderr, "intel_lpmd_control ON|OFF|AUTO\n");
54+
fprintf (stderr, "intel_lpmd_control ON|OFF|AUTO|STATUS\n");
5555
exit (0);
5656
}
5757

58+
connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
59+
if (connection == NULL)
60+
return FALSE;
61+
62+
if (!strncmp (argv[1], "STATUS", 6)) {
63+
g_autoptr(GVariant) result = NULL;
64+
65+
result = g_dbus_connection_call_sync (connection,
66+
INTEL_LPMD_SERVICE_NAME,
67+
INTEL_LPMD_SERVICE_OBJECT_PATH,
68+
INTEL_LPMD_SERVICE_INTERFACE,
69+
"GetState",
70+
NULL,
71+
G_VARIANT_TYPE ("(s)"),
72+
G_DBUS_CALL_FLAGS_NONE,
73+
-1,
74+
NULL,
75+
&error);
76+
77+
if (error != NULL) {
78+
g_warning ("Fail on connecting lpmd: %s", error->message);
79+
exit (1);
80+
}
81+
82+
const gchar *state;
83+
84+
g_variant_get (result, "(&s)", &state);
85+
g_print ("%s\n", state);
86+
87+
return 0;
88+
}
89+
5890
if (!strncmp (argv[1], "ON", 2))
5991
command = g_string_new ("LPM_FORCE_ON");
6092
else if (!strncmp (argv[1], "OFF", 3))
@@ -66,10 +98,6 @@ main(int argc, char **argv)
6698
exit (1);
6799
}
68100

69-
connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
70-
if (connection == NULL)
71-
return FALSE;
72-
73101
g_dbus_connection_call_sync (connection,
74102
INTEL_LPMD_SERVICE_NAME,
75103
INTEL_LPMD_SERVICE_OBJECT_PATH,

0 commit comments

Comments
 (0)