usb_moded-devicelock.c 13.3 KB
Newer Older
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
 * @file: usb_moded-devicelock.c
 *
 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
 * Copyright (C) 2013-2018 Jolla Ltd.
 *
 * @author: Philippe De Swert <philippe.de-swert@nokia.com>
 * @author: Philippe De Swert <philippe.deswert@jollamobile.com>
 * @author: Jonni Rainisto <jonni.rainisto@jollamobile.com>
 * @author: Philippe De Swert <philippedeswert@gmail.com>
 * @author: Vesa Halttunen <vesa.halttunen@jollamobile.com>
 * @author: Jonni Rainisto <jonni.rainisto@jollamobile.com>
 * @author: Philippe De Swert <philippe.deswert@jollamobile.com>
 * @author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Lesser GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the Lesser GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */
30

31
/*
32 33
 * Interacts with the devicelock to know if we can expose the system contents or not
 */
34 35

#include "usb_moded-devicelock.h"
36

37
#include "usb_moded-control.h"
38 39 40 41
#include "usb_moded-dbus-private.h"
#include "usb_moded-log.h"

#include <string.h>
42 43

/* ========================================================================= *
44
 * Types
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
 * ========================================================================= */

/** Devicelock states  */
typedef enum
{
    /** Devicelock is not active */
    DEVICE_LOCK_UNLOCKED  = 0,

    /** Devicelock is active */
    DEVICE_LOCK_LOCKED    = 1,

    /** Initial startup value; from usb-moded p.o.v. equals locked */
    DEVICE_LOCK_UNDEFINED = 2,
}  devicelock_state_t;

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
/* ========================================================================= *
 * Prototypes
 * ========================================================================= */

/* -- devicelock -- */

static const char        *devicelock_state_repr            (devicelock_state_t state);
bool                      devicelock_have_export_permission(void);
static void               devicelock_state_changed         (devicelock_state_t state);
static void               devicelock_state_cancel          (void);
static void               devicelock_state_query_cb        (DBusPendingCall *pending, void *aptr);
static void               devicelock_state_query           (void);
static void               devicelock_state_signal          (DBusMessage *msg);
static void               devicelock_available_changed     (const char *owner);
static void               devicelock_available_cb          (const char *owner);
static void               devicelock_available_cancel      (void);
static void               devicelock_available_query       (void);
static void               devicelock_name_owner_signal     (DBusMessage *msg);
static DBusHandlerResult  devicelock_dbus_filter_cb        (DBusConnection *con, DBusMessage *msg, void *aptr);
bool                      devicelock_start_listener        (void);
void                      devicelock_stop_listener         (void);

/* ========================================================================= *
 * module state data
 * ========================================================================= */

/* SystemBus connection ref used for devicelock ipc */
static DBusConnection *devicelock_con = NULL;

/* Cached devicelock state */
static devicelock_state_t device_lock_state = DEVICE_LOCK_UNDEFINED;

/* Flag for: devicelock is available on system bus */
static gboolean devicelock_is_available = FALSE;

/* ========================================================================= *
 * devicelock state type
 * ========================================================================= */

99 100 101 102 103 104
/** Return human readable representation of devicelock state enum
 */
static const char *
devicelock_state_repr(devicelock_state_t state)
{
    const char *repr = "DEVICE_LOCK_<INVALID>";
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119
    switch( state )
    {
    case DEVICE_LOCK_UNLOCKED:  repr = "DEVICE_LOCK_UNLOCKED";  break;
    case DEVICE_LOCK_LOCKED:    repr = "DEVICE_LOCK_LOCKED";    break;
    case DEVICE_LOCK_UNDEFINED: repr = "DEVICE_LOCK_UNDEFINED"; break;
    default: break;
    }

    return repr;
}

/* ========================================================================= *
 * functionality provided to the other modules
 * ========================================================================= */
120

121
/** Checks if the device is locked.
122
 *
123
 * @return TRUE for unlocked, FALSE for locked
124 125
 *
 */
126
bool devicelock_have_export_permission(void)
127
{
128
    bool unlocked = (device_lock_state == DEVICE_LOCK_UNLOCKED);
129

130
    return unlocked;
131 132
}

133 134 135 136 137
/* ========================================================================= *
 * devicelock state queries
 * ========================================================================= */

static void devicelock_state_changed(devicelock_state_t state)
138
{
139 140 141 142 143 144 145 146
    if( device_lock_state == state )
        goto EXIT;

    log_debug("devicelock state: %s -> %s",
              devicelock_state_repr(device_lock_state),
              devicelock_state_repr(state));
    device_lock_state = state;

147
    control_rethink_usb_charging_fallback();
148 149 150

EXIT:
    return;
151
}
152

153 154 155 156 157 158 159 160 161
static DBusPendingCall *devicelock_state_query_pc = 0;

static void devicelock_state_cancel(void)
{
    if( devicelock_state_query_pc ) {
        dbus_pending_call_cancel(devicelock_state_query_pc);
        dbus_pending_call_unref(devicelock_state_query_pc),
            devicelock_state_query_pc = 0;
    }
162 163
}

164
static void devicelock_state_query_cb(DBusPendingCall *pending, void *aptr)
165
{
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    DBusMessage *rsp = 0;
    dbus_int32_t dta = DEVICE_LOCK_UNDEFINED;
    DBusError    err = DBUS_ERROR_INIT;

    (void)aptr;

    if( !(rsp = dbus_pending_call_steal_reply(pending)) )
    {
        log_err("%s.%s: no reply",
                DEVICELOCK_INTERFACE, DEVICELOCK_GET_STATE_REQ);
        goto EXIT;
    }

    if( dbus_set_error_from_message(&err, rsp) )
    {
        log_err("%s.%s: error reply: %s: %s",
                DEVICELOCK_INTERFACE, DEVICELOCK_GET_STATE_REQ,
                err.name, err.message);
        goto EXIT;
    }
186

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    if( !dbus_message_get_args(rsp, &err, DBUS_TYPE_INT32, &dta,
                               DBUS_TYPE_INVALID) )
    {
        log_err("%s.%s: parse error: %s: %s",
                DEVICELOCK_INTERFACE, DEVICELOCK_GET_STATE_REQ,
                err.name, err.message);
        goto EXIT;
    }

EXIT:
    devicelock_state_changed(dta);

    if( rsp ) dbus_message_unref(rsp);

    dbus_error_free(&err);

    dbus_pending_call_unref(devicelock_state_query_pc),
        devicelock_state_query_pc = 0;
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
static void devicelock_state_query(void)
{
    DBusMessage     *req = NULL;
    DBusPendingCall *pc  = 0;

    devicelock_state_cancel();

    log_debug("querying device lock state");

    if( !devicelock_con ) {
        log_err("not connected to system bus; skip device state query");
        goto EXIT;
    }

    req = dbus_message_new_method_call(DEVICELOCK_SERVICE,
                                       DEVICELOCK_OBJECT,
                                       DEVICELOCK_INTERFACE,
                                       DEVICELOCK_GET_STATE_REQ);

    if( !req )
    {
        log_err("%s.%s: failed to construct request",
                DEVICELOCK_INTERFACE, DEVICELOCK_GET_STATE_REQ);
        goto EXIT;
    }

    if( !dbus_connection_send_with_reply(devicelock_con, req, &pc, -1) )
        goto EXIT;

    if( !pc )
        goto EXIT;

    if( !dbus_pending_call_set_notify(pc, devicelock_state_query_cb, 0, 0) )
        goto EXIT;

    devicelock_state_query_pc = pc, pc = 0;

EXIT:

    if( pc  ) dbus_pending_call_unref(pc);
    if( req ) dbus_message_unref(req);
}

static void devicelock_state_signal(DBusMessage *msg)
{
    DBusError    err = DBUS_ERROR_INIT;
    dbus_int32_t dta = DEVICE_LOCK_LOCKED;

    if( !dbus_message_get_args(msg, &err,
                               DBUS_TYPE_INT32, &dta,
                               DBUS_TYPE_INVALID) )
    {
        log_err("failed to parse  %s.%s signal: %s: %s",
                DEVICELOCK_INTERFACE, DEVICELOCK_STATE_CHANGED_SIG,
                err.name, err.message);
    }

    devicelock_state_changed(dta);

    dbus_error_free(&err);
}

/* ========================================================================= *
 * devicelock name owner tracking
 * ========================================================================= */

static void devicelock_available_changed(const char *owner)
{
    gboolean is_available = (owner && *owner);

    if( devicelock_is_available != is_available ) {
        devicelock_is_available = is_available;
        log_debug("devicelock is %s",
                  devicelock_is_available ? "running" : "stopped");

        /* Forget cached device state */
        devicelock_state_changed(DEVICE_LOCK_UNDEFINED);

        /* Query current state on devicelock startup */
        if( devicelock_is_available ) {
            devicelock_state_query();
        }
    }
}

static DBusPendingCall *devicelock_available_pc = 0;

static void devicelock_available_cb(const char *owner)
{
    devicelock_available_changed(owner);

    dbus_pending_call_unref(devicelock_available_pc),
        devicelock_available_pc = 0;
}

static void devicelock_available_cancel(void)
{
    if( devicelock_available_pc )
    {
306
        dbus_pending_call_cancel(devicelock_available_pc);
307 308 309 310 311 312 313 314 315 316 317
        dbus_pending_call_unref(devicelock_available_pc),
            devicelock_available_pc = 0;
    }
}

static void devicelock_available_query(void)
{
    devicelock_available_cancel();

    log_debug("querying %s name owner", DEVICELOCK_SERVICE);

318 319 320
    umdbus_get_name_owner_async(DEVICELOCK_SERVICE,
                                devicelock_available_cb,
                                &devicelock_available_pc);
321 322
}

323
static void devicelock_name_owner_signal(DBusMessage *msg)
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
{
    DBusError   err  = DBUS_ERROR_INIT;
    const char *name = 0;
    const char *prev = 0;
    const char *curr = 0;

    if( !dbus_message_get_args(msg, &err,
                               DBUS_TYPE_STRING, &name,
                               DBUS_TYPE_STRING, &prev,
                               DBUS_TYPE_STRING, &curr,
                               DBUS_TYPE_INVALID) )
    {
        log_err("failed to parse signal: %s: %s",
                err.name, err.message);
    }
    else if( !strcmp(name, DEVICELOCK_SERVICE) )
    {
        devicelock_available_changed(curr);
    }
    dbus_error_free(&err);
}

/* ========================================================================= *
 * dbus message filter
 * ========================================================================= */

static DBusHandlerResult
devicelock_dbus_filter_cb(DBusConnection *con, DBusMessage *msg, void *aptr)
{
    (void)con;
    (void)aptr;

    if( dbus_message_is_signal(msg,
                               DEVICELOCK_INTERFACE,
                               DEVICELOCK_STATE_CHANGED_SIG) )
    {
        devicelock_state_signal(msg);
    }
    else if( dbus_message_is_signal(msg,
                                    DBUS_INTERFACE_DBUS,
                                    DBUS_NAME_OWNER_CHANGED_SIG) )
    {
366
        devicelock_name_owner_signal(msg);
367 368 369 370 371 372 373 374 375
    }

    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

/* ========================================================================= *
 * start/stop devicelock state tracking
 * ========================================================================= */

376 377
bool
devicelock_start_listener(void)
378
{
379
    bool ack = false;
380 381 382 383

    log_debug("starting devicelock listener");

    /* Get connection ref */
384
    if( (devicelock_con = umdbus_get_connection()) == 0 )
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    {
        log_err("Could not connect to dbus for devicelock\n");
        goto cleanup;
    }

    /* Add filter callback */
    if( !dbus_connection_add_filter(devicelock_con,
                                    devicelock_dbus_filter_cb , 0, 0) )
    {
        log_err("adding system dbus filter for devicelock failed");
        goto cleanup;
    }

    /* Add match without blocking / error checking */
    dbus_bus_add_match(devicelock_con, DEVICELOCK_STATE_CHANGED_MATCH,0);
    dbus_bus_add_match(devicelock_con, DEVICELOCK_NAME_OWNER_CHANGED_MATCH,0);

    /* Initiate async devicelock name owner query */
    devicelock_available_query();

405
    ack = true;
406 407

cleanup:
408 409

    return ack;
410 411
}

412
void
413
devicelock_stop_listener(void)
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
{
    log_debug("stopping devicelock listener");

    /* Do note leave pending queries behind */
    devicelock_state_cancel();
    devicelock_available_cancel();

    if(devicelock_con)
    {
        /* Remove filter callback */
        dbus_connection_remove_filter(devicelock_con,
                                      devicelock_dbus_filter_cb, 0);

        if( dbus_connection_get_is_connected(devicelock_con) ) {
            /* Remove match without blocking / error checking */
            dbus_bus_remove_match(devicelock_con,
                                  DEVICELOCK_STATE_CHANGED_MATCH, 0);
            dbus_bus_remove_match(devicelock_con,
                                  DEVICELOCK_NAME_OWNER_CHANGED_MATCH, 0);
        }

        /* Let go of connection ref */
        dbus_connection_unref(devicelock_con),
            devicelock_con = 0;
    }
}