| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2021 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "transitions.h" |
| 10 |
#include "plugin.h" |
| 11 |
#include "reserve-device.h" |
| 12 |
#include "reserve-device-interface.h" |
| 13 |
|
| 14 |
struct _WpReserveDeviceAcquireTransition |
| 15 |
{ |
| 16 |
WpTransition parent; |
| 17 |
gint owner_state; |
| 18 |
WpOrgFreedesktopReserveDevice1 *proxy; |
| 19 |
}; |
| 20 |
|
| 21 |
enum { |
| 22 |
OWNER_STATE_NONE = 0, |
| 23 |
OWNER_STATE_ACQUIRED, |
| 24 |
OWNER_STATE_LOST, |
| 25 |
}; |
| 26 |
|
| 27 |
enum { |
| 28 |
STEP_EXPORT_OBJECT = WP_TRANSITION_STEP_CUSTOM_START, |
| 29 |
STEP_ACQUIRE_NO_FORCE, |
| 30 |
STEP_GET_PROXY, |
| 31 |
STEP_REQUEST_RELEASE, |
| 32 |
STEP_ACQUIRE_WITH_FORCE, |
| 33 |
STEP_UNEXPORT_OBJECT, |
| 34 |
}; |
| 35 |
|
| 36 |
G_DEFINE_TYPE (WpReserveDeviceAcquireTransition, |
| 37 |
wp_reserve_device_acquire_transition, WP_TYPE_TRANSITION) |
| 38 |
|
| 39 |
static void |
| 40 |
wp_reserve_device_acquire_transition_init ( |
| 41 |
WpReserveDeviceAcquireTransition * self) |
| 42 |
{ |
| 43 |
} |
| 44 |
|
| 45 |
static void |
| 46 |
wp_reserve_device_acquire_transition_finalize (GObject * object) |
| 47 |
{ |
| 48 |
WpReserveDeviceAcquireTransition *self = |
| 49 |
WP_RESERVE_DEVICE_ACQUIRE_TRANSITION (object); |
| 50 |
|
| 51 |
g_clear_object (&self->proxy); |
| 52 |
|
| 53 |
G_OBJECT_CLASS (wp_reserve_device_acquire_transition_parent_class)-> |
| 54 |
finalize (object); |
| 55 |
} |
| 56 |
|
| 57 |
static guint |
| 58 |
wp_reserve_device_acquire_transition_get_next_step ( |
| 59 |
WpTransition * transition, guint step) |
| 60 |
{ |
| 61 |
WpReserveDeviceAcquireTransition *self = |
| 62 |
WP_RESERVE_DEVICE_ACQUIRE_TRANSITION (transition); |
| 63 |
|
| 64 |
switch (step) { |
| 65 |
case WP_TRANSITION_STEP_NONE: |
| 66 |
return STEP_EXPORT_OBJECT; |
| 67 |
|
| 68 |
case STEP_EXPORT_OBJECT: |
| 69 |
return STEP_ACQUIRE_NO_FORCE; |
| 70 |
|
| 71 |
case STEP_ACQUIRE_NO_FORCE: |
| 72 |
switch (self->owner_state) { |
| 73 |
case OWNER_STATE_ACQUIRED: |
| 74 |
return WP_TRANSITION_STEP_NONE; |
| 75 |
|
| 76 |
case OWNER_STATE_LOST: |
| 77 |
return STEP_GET_PROXY; |
| 78 |
|
| 79 |
default: |
| 80 |
return WP_TRANSITION_STEP_ERROR; |
| 81 |
} |
| 82 |
|
| 83 |
case STEP_GET_PROXY: |
| 84 |
if (self->proxy) |
| 85 |
return STEP_REQUEST_RELEASE; |
| 86 |
else |
| 87 |
return STEP_ACQUIRE_WITH_FORCE; |
| 88 |
|
| 89 |
case STEP_REQUEST_RELEASE: |
| 90 |
switch (self->owner_state) { |
| 91 |
case OWNER_STATE_ACQUIRED: |
| 92 |
return STEP_ACQUIRE_WITH_FORCE; |
| 93 |
|
| 94 |
case OWNER_STATE_LOST: |
| 95 |
return STEP_UNEXPORT_OBJECT; |
| 96 |
|
| 97 |
default: |
| 98 |
return WP_TRANSITION_STEP_ERROR; |
| 99 |
} |
| 100 |
|
| 101 |
case STEP_ACQUIRE_WITH_FORCE: |
| 102 |
return WP_TRANSITION_STEP_NONE; |
| 103 |
|
| 104 |
case STEP_UNEXPORT_OBJECT: |
| 105 |
return WP_TRANSITION_STEP_NONE; |
| 106 |
|
| 107 |
default: |
| 108 |
return WP_TRANSITION_STEP_ERROR; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
static void |
| 113 |
on_got_proxy (GObject * src, GAsyncResult * res, WpTransition * transition) |
| 114 |
{ |
| 115 |
WpReserveDeviceAcquireTransition *self = |
| 116 |
WP_RESERVE_DEVICE_ACQUIRE_TRANSITION (transition); |
| 117 |
g_autoptr (GError) error = NULL; |
| 118 |
|
| 119 |
self->proxy = |
| 120 |
wp_org_freedesktop_reserve_device1_proxy_new_finish (res, &error); |
| 121 |
if (!self->proxy) { |
| 122 |
WpReserveDevice *rd = wp_transition_get_source_object (transition); |
| 123 |
wp_info_object (rd, "%s: Could not get proxy of remote reservation: %s", |
| 124 |
rd->name, error->message); |
| 125 |
} |
| 126 |
|
| 127 |
wp_transition_advance (transition); |
| 128 |
} |
| 129 |
|
| 130 |
static void |
| 131 |
on_request_release_done (GObject * src, GAsyncResult * res, |
| 132 |
WpTransition * transition) |
| 133 |
{ |
| 134 |
WpReserveDeviceAcquireTransition *self = |
| 135 |
WP_RESERVE_DEVICE_ACQUIRE_TRANSITION (transition); |
| 136 |
g_autoptr (GError) error = NULL; |
| 137 |
gboolean released = FALSE; |
| 138 |
|
| 139 |
if (!wp_org_freedesktop_reserve_device1_call_request_release_finish ( |
| 140 |
self->proxy, &released, res, &error)) { |
| 141 |
WpReserveDevice *rd = wp_transition_get_source_object (transition); |
| 142 |
wp_info_object (rd, "%s: Could not call RequestRelease: %s", |
| 143 |
rd->name, error->message); |
| 144 |
} |
| 145 |
|
| 146 |
self->owner_state = released ? OWNER_STATE_ACQUIRED : OWNER_STATE_LOST; |
| 147 |
wp_transition_advance (transition); |
| 148 |
} |
| 149 |
|
| 150 |
static void |
| 151 |
wp_reserve_device_acquire_transition_execute_step ( |
| 152 |
WpTransition * transition, guint step) |
| 153 |
{ |
| 154 |
WpReserveDeviceAcquireTransition *self = |
| 155 |
WP_RESERVE_DEVICE_ACQUIRE_TRANSITION (transition); |
| 156 |
WpReserveDevice *rd = wp_transition_get_source_object (transition); |
| 157 |
g_autoptr (WpReserveDevicePlugin) plugin = g_weak_ref_get (&rd->plugin); |
| 158 |
|
| 159 |
if (G_UNLIKELY (!plugin && step != WP_TRANSITION_STEP_ERROR)) { |
| 160 |
wp_transition_return_error (transition, g_error_new ( |
| 161 |
WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED, |
| 162 |
"plugin destroyed while Acquire was in progress")); |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
switch (step) { |
| 167 |
case STEP_EXPORT_OBJECT: |
| 168 |
wp_reserve_device_export_object (rd); |
| 169 |
wp_transition_advance (transition); |
| 170 |
break; |
| 171 |
|
| 172 |
case STEP_ACQUIRE_NO_FORCE: |
| 173 |
wp_reserve_device_own_name (rd, FALSE); |
| 174 |
break; |
| 175 |
|
| 176 |
case STEP_GET_PROXY: { |
| 177 |
g_autoptr (GDBusConnection) conn = NULL; |
| 178 |
g_object_get (plugin->dbus, "connection", &conn, NULL); |
| 179 |
wp_org_freedesktop_reserve_device1_proxy_new (conn, |
| 180 |
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 181 |
G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS | |
| 182 |
G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, |
| 183 |
rd->service_name, rd->object_path, NULL, |
| 184 |
(GAsyncReadyCallback) on_got_proxy, self); |
| 185 |
break; |
| 186 |
} |
| 187 |
|
| 188 |
case STEP_REQUEST_RELEASE: |
| 189 |
self->owner_state = OWNER_STATE_NONE; |
| 190 |
wp_org_freedesktop_reserve_device1_call_request_release ( |
| 191 |
self->proxy, rd->priority, NULL, |
| 192 |
(GAsyncReadyCallback) on_request_release_done, self); |
| 193 |
break; |
| 194 |
|
| 195 |
case STEP_ACQUIRE_WITH_FORCE: |
| 196 |
wp_reserve_device_unown_name (rd); |
| 197 |
self->owner_state = OWNER_STATE_NONE; |
| 198 |
wp_reserve_device_own_name (rd, TRUE); |
| 199 |
break; |
| 200 |
|
| 201 |
case STEP_UNEXPORT_OBJECT: |
| 202 |
wp_reserve_device_unown_name (rd); |
| 203 |
wp_reserve_device_unexport_object (rd); |
| 204 |
wp_transition_advance (transition); |
| 205 |
break; |
| 206 |
|
| 207 |
case WP_TRANSITION_STEP_ERROR: |
| 208 |
wp_reserve_device_unown_name (rd); |
| 209 |
break; |
| 210 |
|
| 211 |
default: |
| 212 |
g_return_if_reached (); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
static void |
| 217 |
wp_reserve_device_acquire_transition_class_init ( |
| 218 |
WpReserveDeviceAcquireTransitionClass * klass) |
| 219 |
{ |
| 220 |
GObjectClass *object_class = (GObjectClass *) klass; |
| 221 |
WpTransitionClass *transition_class = (WpTransitionClass *) klass; |
| 222 |
|
| 223 |
object_class->finalize = |
| 224 |
wp_reserve_device_acquire_transition_finalize; |
| 225 |
|
| 226 |
transition_class->get_next_step = |
| 227 |
wp_reserve_device_acquire_transition_get_next_step; |
| 228 |
transition_class->execute_step = |
| 229 |
wp_reserve_device_acquire_transition_execute_step; |
| 230 |
} |
| 231 |
|
| 232 |
WpTransition * |
| 233 |
wp_reserve_device_acquire_transition_new (WpReserveDevice *rd, |
| 234 |
GCancellable * cancellable, GAsyncReadyCallback callback, |
| 235 |
gpointer callback_data) |
| 236 |
{ |
| 237 |
return wp_transition_new (wp_reserve_device_acquire_transition_get_type (), |
| 238 |
rd, cancellable, callback, callback_data); |
| 239 |
} |
| 240 |
|
| 241 |
void |
| 242 |
wp_reserve_device_acquire_transition_name_acquired (WpTransition * tr) |
| 243 |
{ |
| 244 |
WpReserveDeviceAcquireTransition *self = |
| 245 |
WP_RESERVE_DEVICE_ACQUIRE_TRANSITION (tr); |
| 246 |
self->owner_state = OWNER_STATE_ACQUIRED; |
| 247 |
wp_transition_advance (tr); |
| 248 |
} |
| 249 |
|
| 250 |
void |
| 251 |
wp_reserve_device_acquire_transition_name_lost (WpTransition * tr) |
| 252 |
{ |
| 253 |
WpReserveDeviceAcquireTransition *self = |
| 254 |
WP_RESERVE_DEVICE_ACQUIRE_TRANSITION (tr); |
| 255 |
self->owner_state = OWNER_STATE_LOST; |
| 256 |
wp_transition_advance (tr); |
| 257 |
} |
| 258 |
|
| 259 |
gboolean |
| 260 |
wp_reserve_device_acquire_transition_finish (GAsyncResult * res, |
| 261 |
GError ** error) |
| 262 |
{ |
| 263 |
if (!wp_transition_finish (res, error)) |
| 264 |
return FALSE; |
| 265 |
|
| 266 |
WpReserveDeviceAcquireTransition *self = |
| 267 |
WP_RESERVE_DEVICE_ACQUIRE_TRANSITION (res); |
| 268 |
return (self->owner_state == OWNER_STATE_ACQUIRED) ? TRUE : FALSE; |
| 269 |
} |