mce-wakelock.c 11.5 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 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
/**
 * @file mce-wakelock.c
 * Wakelock multiplexing code for the Mode Control Entity
 * <p>
 * Copyright (C) 2015 Jolla Ltd.
 * <p>
 * @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
 *
 * mce is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * mce 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with mce.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mce-wakelock.h"
#include "mce-log.h"

#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include <glib.h>

/* ========================================================================= *
 * FUNCTIONS & DATA
 * ========================================================================= */

/* ------------------------------------------------------------------------- *
 * SYSFS_API
 * ------------------------------------------------------------------------- */

/** Path to kernel wakelock obtain sysfs file */
static const char mwl_sysfs_lock_path[] = "/sys/power/wake_lock";

/** Path to kernel wakelock release sysfs file */
static const char mwl_sysfs_unlock_path[] = "/sys/power/wake_unlock";

static bool mwl_sysfs_write(const char *path, const char *data, int size);

/* ------------------------------------------------------------------------- *
 * RAWLOCK_API
 * ------------------------------------------------------------------------- */

/** Name of the multiplexed "real" wakelock */
static const char mwl_rawlock_name[] = "mce_mux";

/** Flag for: "real" wakelock is held */
static bool mce_rawlock_locked = false;

static bool mwl_rawlock_supported (void);
static bool mwl_rawlock_lock      (void);
static bool mwl_rawlock_unlock    (void);
static void mwl_rawlock_set       (bool lock);

/* ------------------------------------------------------------------------- *
 * mwl_wakelock_t
 * ------------------------------------------------------------------------- */

/** Virtual wakelock object */
typedef struct mwl_wakelock_t
{
    /** Name of the virtual wakelock */
    gchar *wl_name;

    /** Release timer id */
    guint  wl_timer_id;
} mwl_wakelock_t;

static gboolean        mwl_wakelock_timer_cb    (gpointer aptr);
static void            mwl_wakelock_start_timer (mwl_wakelock_t *self, int delay_ms);
static void            mwl_wakelock_stop_timer  (mwl_wakelock_t *self);

static mwl_wakelock_t *mwl_wakelock_create      (const char *name);
static void            mwl_wakelock_delete      (mwl_wakelock_t *self);
static void            mwl_wakelock_delete_cb   (void *self);

/* ------------------------------------------------------------------------- *
 * MODULE_API
 * ------------------------------------------------------------------------- */

/** Flag for: wakelock module is ready for use */
static bool mce_wakelock_ready  = false;

/** Lookup table for tracked wakelock objects */
static GHashTable *mce_wakelock_lut = 0; // [name] -> mwl_wakelock_t *

static mwl_wakelock_t *mce_wakelock_add_entry    (const char *name);
static void            mce_wakelock_rem_entry    (const char *name);
static bool            mce_wakelock_have_entries (void);

void                   mce_wakelock_obtain       (const char *name, int duration_ms);
void                   mce_wakelock_release      (const char *name);

void                   mce_wakelock_init         (void);
void                   mce_wakelock_quit         (void);
void                   mce_wakelock_abort        (void);

/* ========================================================================= *
 * SYSFS_API
 * ========================================================================= */

/** Helper for writing to sysfs files
 */
static bool
mwl_sysfs_write(const char *path, const char *data, int size)
{
    bool res = false;
    int  fd  = -1;

    if( !path || !data || size <= 0 )
        goto cleanup;

    if( (fd = open(path, O_WRONLY)) == -1 )
        goto cleanup;

    if( write(fd, data, size) == -1 )
        goto cleanup;

    res = true;

cleanup:
    if( fd != -1 ) close(fd);

    return res;
}

/* ========================================================================= *
 * RAWLOCK_API
 * ========================================================================= */

/** Predicate for: wakelock sysfs control files exist
 */
static bool
mwl_rawlock_supported(void)
{
    return (access(mwl_sysfs_lock_path, W_OK) == 0 &&
            access(mwl_sysfs_unlock_path, W_OK) == 0);
}

/** Async signal safe wakelock obtain
 */
static bool
mwl_rawlock_lock(void)
{
    return mwl_sysfs_write(mwl_sysfs_lock_path, mwl_rawlock_name,
                           sizeof mwl_rawlock_name - 1);
}

/** Async signal safe wakelock release
 */
static bool
mwl_rawlock_unlock(void)
{
    return mwl_sysfs_write(mwl_sysfs_unlock_path, mwl_rawlock_name,
                           sizeof mwl_rawlock_name - 1);
}

/** Set wakelock state
 *
 * @param lock  true to obtain real wakelock, false to release
 */
static void
mwl_rawlock_set(bool lock)
{
    if( mce_rawlock_locked == lock )
        goto EXIT;

    mce_log(LL_DEBUG, "wakelock %s", lock ? "obtain" : "release");

    errno = 0;

    if( (mce_rawlock_locked = lock) ) {
        if( !mwl_rawlock_lock() )
            mce_log(LL_ERR, "failed to obtain wakelock: %m");
    }
    else {
        if( !mwl_rawlock_unlock() )
            mce_log(LL_ERR, "failed to release wakelock: %m");
    }

EXIT:
    return;
}

/* ========================================================================= *
 * mwl_wakelock_t
 * ========================================================================= */

/** Timer callback for releasing virtual wakelock
 *
 * @param aptr wakelock object pointer
 */
static gboolean
mwl_wakelock_timer_cb(gpointer aptr)
{
    mwl_wakelock_t *self = aptr;

    if( !self )
        goto EXIT;

    if( !self->wl_timer_id )
        goto EXIT;

    self->wl_timer_id = 0;

    mce_wakelock_release(self->wl_name);

EXIT:
    return FALSE;
}

/** Start virtual wakelock object release timer
 *
 * @param self      wakelock object pointer, or NULL
 * @param delay_ms  delay before releasing wakelock
 */
static void
mwl_wakelock_start_timer(mwl_wakelock_t *self, int delay_ms)
{
    if( !self )
        goto EXIT;

    if( self->wl_timer_id ) {
        g_source_remove(self->wl_timer_id),
            self->wl_timer_id = 0;
    }

    if( delay_ms < 0 )
        goto EXIT;

    if( delay_ms > 0 ) {
        self->wl_timer_id = g_timeout_add(delay_ms,
                                          mwl_wakelock_timer_cb,
                                          self);
    }
    else {
        self->wl_timer_id = g_idle_add(mwl_wakelock_timer_cb, self);
    }

EXIT:
    return;
}

/** Stop virtual wakelock object release timer
 *
 * @param self wakelock object pointer, or NULL
 */
static void
mwl_wakelock_stop_timer(mwl_wakelock_t *self)
{
    if( !self )
        goto EXIT;

    if( self->wl_timer_id ) {
        g_source_remove(self->wl_timer_id),
            self->wl_timer_id = 0;
    }

EXIT:
    return;
}

/** Create virtual wakelock object
 *
 * @param name Name of wakelock object to create
 *
 * @return wakelock object pointer
 */
static mwl_wakelock_t *
mwl_wakelock_create(const char *name)
{
    mwl_wakelock_t *self = g_malloc0(sizeof *self);

    self->wl_name     = g_strdup(name);
    self->wl_timer_id = 0;

    mce_log(LL_DEBUG, "wakelock %s obtain (mux)", self->wl_name);

    return self;
}

/** Delete virtual wakelock object
 *
 * @param self wakelock object pointer, or NULL
 */
static void
mwl_wakelock_delete(mwl_wakelock_t *self)
{
    if( !self )
        goto EXIT;

    mwl_wakelock_stop_timer(self);

    mce_log(LL_DEBUG, "wakelock %s release (mux)", self->wl_name);

    g_free(self->wl_name);
    g_free(self);

EXIT:
    return;
}

/** GDestroyNotify compatible delete callback
 *
 * @param aptr wakelock object pointer (as void pointer), or NULL
 */
static void
mwl_wakelock_delete_cb(void *aptr)
{
    mwl_wakelock_delete(aptr);
}

/* ========================================================================= *
 * MODULE_API
 * ========================================================================= */

/** Lookup or create a wakelock object by name
 *
 * @param name  Name of the virtual wakelock
 *
 * @return object pointer, or NULL on errors
 */
static mwl_wakelock_t *
mce_wakelock_add_entry(const char *name)
{
    mwl_wakelock_t *self = 0;

    if( !mce_wakelock_lut )
        goto EXIT;

    if( !(self = g_hash_table_lookup(mce_wakelock_lut, name)) ) {
        self = mwl_wakelock_create(name);
        g_hash_table_replace(mce_wakelock_lut, g_strdup(name), self);
    }

EXIT:
    return self;
}

/** Remove a wakelock object by name
 *
 * @param name  Name of the virtual wakelock
 */
static void
mce_wakelock_rem_entry(const char *name)
{
    if( !mce_wakelock_lut )
        goto EXIT;

    g_hash_table_remove(mce_wakelock_lut, name);

EXIT:
    return;
}

/** Predicate for: have virtual wakelocks
 *
 * @return true if there are active virtual wakelocks, false otherwise
 */
static bool
mce_wakelock_have_entries(void)
{
    bool have_entries = false;

    if( !mce_wakelock_lut )
        goto EXIT;

    if( g_hash_table_size(mce_wakelock_lut) > 0 )
        have_entries = true;

EXIT:
    return have_entries;
}

/** Obtain virtual wakelock
 *
 * @param name  Name of the virtual wakelock
 */
void
mce_wakelock_obtain(const char *name, int duration_ms)
{
    if( !mce_wakelock_ready )
        goto EXIT;

    /* Add entry & start release timer */
    mwl_wakelock_start_timer(mce_wakelock_add_entry(name),
                             duration_ms);

    /* Re-evaluate need for real wakelock */
    mwl_rawlock_set(mce_wakelock_have_entries());

EXIT:
    return;
}

/** Release virtual wakelock
 *
 * @param name  Name of the virtual wakelock
 */
void
mce_wakelock_release(const char *name)
{
    if( !mce_wakelock_ready )
        goto EXIT;

    /* Remove entry */
    mce_wakelock_rem_entry(name);

    /* Re-evaluate need for real wakelock */
    mwl_rawlock_set(mce_wakelock_have_entries());

EXIT:
    return;
}

/** Initialize mce wakelock subsystem
 */
void
mce_wakelock_init(void)
{
    /* Leave disabled if sysfs control files do not exist */
    if( !mwl_rawlock_supported() )
        goto EXIT;

    /* Setup entry look up table */
    if( !mce_wakelock_lut )
        mce_wakelock_lut = g_hash_table_new_full(g_str_hash, g_str_equal,
                                                 g_free, mwl_wakelock_delete_cb);

    /* In case previous mce instance managed to exit without
     * clearing wakelocks: unlock without error checking */
    mwl_rawlock_unlock();

    /* allow locking */
    mce_wakelock_ready = true;

EXIT:
    mce_log(LL_DEBUG, "wakelock usage %s",
            mce_wakelock_ready ? "enabled" : "disabled");

    return;
}

/** Cleanup mce wakelock subsystem
 */
void
mce_wakelock_quit(void)
{
    /* Deny further locking */
    mce_wakelock_ready = false;

    /* Flush entry look up table */
    if( mce_wakelock_lut )
        g_hash_table_unref(mce_wakelock_lut), mce_wakelock_lut = 0;

    /* If there were active internal wakelocks,
     * remove the real kernel wakelock too */
    mwl_rawlock_set(false);
}

/** Async signal safe wakelock cleanup
 */
void
mce_wakelock_abort(void)
{
    /* Deny further locking */
    mce_wakelock_ready = false;

    /* Uncondition unlock, using syscalls only */
    mwl_rawlock_unlock();
}