mce-hbtimer.c 26.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
/**
 * @file mce-hbtimer.c
 *
 * Mode Control Entity - Suspend proof timer functionality
 *
 * <p>
 *
 * Copyright (C) 2014-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-hbtimer.h"

#include "mce.h"
#include "mce-log.h"
31
#include "mce-lib.h"
32 33 34 35 36

#ifdef ENABLE_WAKELOCKS
# include "libwakelock.h"
#endif

37 38
#include <sys/socket.h>

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
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>

#include <iphbd/libiphb.h>

/* ========================================================================= *
 * Types and functions
 * ========================================================================= */

/* ------------------------------------------------------------------------- *
 * TIMER_METHODS
 * ------------------------------------------------------------------------- */

/** State data for mce heartbeat timers */
struct mce_hbtimer_t
{
    /** Timer name, used for debug logging purposes */
    char       *hbt_name;

    /** Trigger time, milliseconds in CLOCK_BOOTTIME base */
    int64_t     hbt_trigger;

    /** Timer callback function */
    GSourceFunc hbt_notify;

    /** Timer delay in milliseconds */
    int         hbt_period;

    /** Flag for: control within hbt_notify() */
    bool        hbt_in_notify;

    /** User data to pass to hbt_notify() */
    void       *hbt_user_data;
};

mce_hbtimer_t * mce_hbtimer_create         (const char *name, int period, GSourceFunc notify, void *user_data);
void            mce_hbtimer_delete         (mce_hbtimer_t *self);

bool            mce_hbtimer_is_active      (const mce_hbtimer_t *self);
const char     *mce_hbtimer_get_name       (const mce_hbtimer_t *self);
void            mce_hbtimer_set_period     (mce_hbtimer_t *self, int period);
void            mce_hbtimer_start          (mce_hbtimer_t *self);
void            mce_hbtimer_stop           (mce_hbtimer_t *self);

static void     mce_hbtimer_notify         (mce_hbtimer_t *self);
static void     mce_hbtimer_set_trigger    (mce_hbtimer_t *self, int64_t trigger);

/* ------------------------------------------------------------------------- *
 * GENERIC_UTILITIES
 * ------------------------------------------------------------------------- */

/** Monotonic tick value used to signify "not-set" */
#define NO_TICK INT64_MAX

static guint    mht_add_iowatch            (int fd, bool close_on_unref, GIOCondition cnd, GIOFunc io_cb, gpointer aptr);

/* ------------------------------------------------------------------------- *
 * QUEUE_MANAGEMENT
 * ------------------------------------------------------------------------- */

/** List of registered timers */
static GSList *mht_queue_timer_list = 0;

void            mht_queue_dispatch_timers  (void);
static void     mht_queue_schedule_wakeups (void);
static void     mht_queue_garbage_collect  (void);
static void     mht_queue_add_timer        (mce_hbtimer_t *self);
static void     mht_queue_remove_timer     (mce_hbtimer_t *self);
static bool     mht_queue_has_timer        (const mce_hbtimer_t *self);

/* ------------------------------------------------------------------------- *
 * GLIB_WAKEUPS
 * ------------------------------------------------------------------------- */

/** Source ID for currently active glib timer wakeup */
static guint mce_hbtimer_glib_wait_id = 0;

static gboolean mht_glib_wakeup_cb         (gpointer aptr);
static void     mht_glib_set_wakeup        (int64_t trigger, int64_t now);

/* ------------------------------------------------------------------------- *
 * IPHB_WAKEUPS
 * ------------------------------------------------------------------------- */

/** How much wakeups from suspend can be delayed [s]
 *
 * To increase changes of aligning with other iphb wakeups this
 * should be >= heartbeat period (=12 seconds) */
#define MHT_IPHB_WAKEUP_MAX_DELAY_S     12

/** Cached timestamp of last requested iphb wakeup */
static int64_t  mht_iphb_wakeup_tick = NO_TICK;

/** Source id for iphb wakeup input watch */
static guint   mht_iphb_wakeup_watch_id = 0;

static gboolean mht_iphb_wakeup_cb         (GIOChannel *chn, GIOCondition cnd, gpointer data);
static void     mht_iphb_set_wakeup        (int64_t trigger, int64_t now);

/* ------------------------------------------------------------------------- *
 * IPHB_CONNECTION
 * ------------------------------------------------------------------------- */

/** Number of times iphb connect is attempted after dsme startup */
#define MHT_CONNECTION_MAX_RETRIES    5

/** Delay between iphb connect attempts [ms] */
#define MHT_CONNECTION_RETRY_DELAY_MS 5000

/** Timer ID for: iphb connection attempts */
static guint mht_connection_timer_id = 0;

/** Number of connection attempts so far */
static gint  mht_connection_retry_no = 0;

/** Handle for iphb connection */
static iphb_t   mht_connection_handle = 0;

static bool     mht_connection_try_to_open (void);

static gboolean mht_connection_timer_cb    (gpointer aptr);
static bool     mht_connection_is_pending  (void);
static void     mht_connection_start_timer (void);
static void     mht_connection_stop_timer  (void);

static void     mht_connection_open        (void);
static void     mht_connection_close       (void);

/* ------------------------------------------------------------------------- *
 * DATAPIPE_HANDLERS
 * ------------------------------------------------------------------------- */

174 175
/** Availability of dsme; from dsme_service_state_pipe */
static service_state_t dsme_service_state = SERVICE_STATE_UNDEF;
176 177 178 179

/** Device is shutting down; assume false */
static bool shutting_down = false;

180 181
static void mht_datapipe_dsme_service_state_cb (gconstpointer data);
static void mht_datapipe_resume_detected_event_cb (gconstpointer data);
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
static void mht_datapipe_shutting_down_cb  (gconstpointer data);

static void mht_datapipe_init(void);
static void mht_datapipe_quit(void);

/* ------------------------------------------------------------------------- *
 * MODULE_INIT
 * ------------------------------------------------------------------------- */

/** Flag for: mce_hbtimer_init() has been called */
static bool     mce_hbtimer_initialized = false;

void            mce_hbtimer_init          (void);
void            mce_hbtimer_quit          (void);

/* ========================================================================= *
 * GENERIC_UTILITIES
 * ========================================================================= */

/** Helper for creating I/O watch for file descriptor
 */
static guint
mht_add_iowatch(int fd, bool close_on_unref,
                      GIOCondition cnd, GIOFunc io_cb, gpointer aptr)
{
    guint         wid = 0;
    GIOChannel   *chn = 0;

    if( !(chn = g_io_channel_unix_new(fd)) )
        goto cleanup;

    g_io_channel_set_close_on_unref(chn, close_on_unref);

    cnd |= G_IO_ERR | G_IO_HUP | G_IO_NVAL;

    if( !(wid = g_io_add_watch(chn, cnd, io_cb, aptr)) )
        goto cleanup;

cleanup:
    if( chn != 0 ) g_io_channel_unref(chn);

    return wid;

}

/* ========================================================================= *
 * TIMER_METHODS
 * ========================================================================= */

/** Create heatbeat timer
 *
 * @param name      timer name
 * @param period    timer delay [ms]
 * @param notify    function to be called when triggered
 * @param user_data pointer to be passed to notify function
 *
 * @return heartbeat timer object
 */
mce_hbtimer_t *
mce_hbtimer_create(const char  *name,
                   int          period,
                   GSourceFunc  notify,
                   void        *user_data)
{
    mce_hbtimer_t *self = calloc(1, sizeof *self);

    self->hbt_name      = name ? strdup(name) : 0;
    self->hbt_notify    = notify;
    self->hbt_period    = period;
    self->hbt_user_data = user_data;
    self->hbt_trigger   = NO_TICK;
    self->hbt_in_notify = false;

    mht_queue_add_timer(self);

    return self;
}

/** Delete heatbeat timer
 *
 * @param self heartbeat timer object, or NULL
 */
void
mce_hbtimer_delete(mce_hbtimer_t *self)
{
    if( !self )
        goto EXIT;

    mht_queue_remove_timer(self);
    mht_queue_schedule_wakeups();

    free(self->hbt_name),
        self->hbt_name = 0;

    free(self);

EXIT:
    return;
}

/** Predicate for: heatbeat timer has been started
 *
 * @param self heartbeat timer object, or NULL
 *
 * @return true if heartbeat timer has been started, false otherwise
 */
bool
mce_hbtimer_is_active(const mce_hbtimer_t *self)
{
    bool active = false;
    if( self )
        active = (self->hbt_trigger < NO_TICK);
    return active;
}

/** Get heatbeat timer name
 *
 * @param self heartbeat timer object, or NULL
 *
 * @return "invalid" if object is not valid,
 *         "unknown" if object has no name, or
 *         object name
 */

const char *
mce_hbtimer_get_name(const mce_hbtimer_t *self)
{
    const char *name = "invalid";
    if( self )
        name = self->hbt_name;
    return name ?: "unknown";
}

/** Set heatbeat timer period
 *
 * @param self   heartbeat timer object, or NULL
 * @param period timer delay [ms]
 */
void
mce_hbtimer_set_period(mce_hbtimer_t *self, int period)
{
    if( self )
        self->hbt_period = period;
}

/** Call heatbeat timer notification functiom
 *
 * @param self   heartbeat timer object, or NULL
 */
static void
mce_hbtimer_notify(mce_hbtimer_t *self)
{
    if( !self )
        goto EXIT;

    if( self->hbt_in_notify )
        goto EXIT;

    if( !self->hbt_notify )
        goto EXIT;

    self->hbt_in_notify = true;
    self->hbt_trigger   = NO_TICK;

    bool again = self->hbt_notify(self->hbt_user_data);

    /* Check that notify callback did not delete the timer */
    if( !mht_queue_has_timer(self) )
        goto EXIT;

    self->hbt_in_notify = false;

    if( again )
        mce_hbtimer_start(self);

EXIT:
    return;
}

/** Set heatbeat timer trigger time stamp
 *
 * @param self    heartbeat timer object, or NULL
 * @param trigger trigger time
 */

static void
mce_hbtimer_set_trigger(mce_hbtimer_t *self, int64_t trigger)
{
    if( !self )
        goto EXIT;

    if( self->hbt_trigger == trigger )
        goto EXIT;

    self->hbt_trigger = trigger;
    mht_queue_schedule_wakeups();

EXIT:
    return;
}

/** Start heatbeat timer
 *
 * @param self   heartbeat timer object, or NULL
 */
void
mce_hbtimer_start(mce_hbtimer_t *self)
{
    if( !self )
        goto EXIT;

    mce_log(LL_DEBUG, "start %s %d", mce_hbtimer_get_name(self),
            self->hbt_period);
395
    int64_t now = mce_lib_get_boot_tick();
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 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    int64_t trigger = now + self->hbt_period;
    mce_hbtimer_set_trigger(self, trigger);

EXIT:
    return;
}

/** Stop heatbeat timer
 *
 * @param self   heartbeat timer object, or NULL
 */
void
mce_hbtimer_stop(mce_hbtimer_t *self)
{
    if( !self )
        goto EXIT;

    mce_log(LL_DEBUG, "stop %s", mce_hbtimer_get_name(self));
    mce_hbtimer_set_trigger(self, NO_TICK);
    mht_queue_schedule_wakeups();

EXIT:
    return;
}

/* ========================================================================= *
 * QUEUE_MANAGEMENT
 * ========================================================================= */

/** Clean up unused timer list slots
 *
 * When timers are deleted, the associated node in mht_queue_timer_list
 * is left in place with data pointer set to zero.
 *
 * This function can be used to remove such stale entries in suitable
 * point outside the dispatch iteration loop.
 */
static void
mht_queue_garbage_collect(void)
{
    GSList **tail = &mht_queue_timer_list;

    GSList *item;

    while( (item = *tail) ) {
        if( item->data ) {
            tail = &item->next;
        }
        else {
            *tail = item->next;
            item->next = 0;
            g_slist_free(item);
        }
    }
}

/** Predicate for: heartbeat timer is registered
 *
 * @param self   heartbeat timer object, or NULL
 *
 * return true if timer is registered, false otherwise
 */
static bool
mht_queue_has_timer(const mce_hbtimer_t *self)
{
    bool has_timer = false;

    if( !self )
        goto EXIT;

    has_timer = g_slist_find(mht_queue_timer_list, self) != 0;

EXIT:
    return has_timer;
}

/** Register heartbeat timer
 *
 * @param self   heartbeat timer object, or NULL
 */
static void
mht_queue_add_timer(mce_hbtimer_t *self)
{
    if( !self )
        goto EXIT;

    /* Try to find recyclable vacated timer slot */
    GSList *item = g_slist_find(mht_queue_timer_list, 0);

    if( item )
        item->data = self;
    else
        mht_queue_timer_list = g_slist_prepend(mht_queue_timer_list, self);

EXIT:
    return;
}

/** Unregister heartbeat timer
 *
 * @param self   heartbeat timer object, or NULL
 */
static void
mht_queue_remove_timer(mce_hbtimer_t *self)
{
    if( !self )
        goto EXIT;

    GSList *item = g_slist_find(mht_queue_timer_list, self);

    /* Vacate the slot by clearing the data link */
    if( item )
        item->data = 0;

EXIT:
    return;
}

/** Scan registered heartbeat timers and schdule nearest wakeup
 */
static void
mht_queue_schedule_wakeups(void)
{
    if( !mce_hbtimer_initialized )
        goto EXIT;

    int64_t trigger = NO_TICK;
    bool    compact = false;

    for( GSList *item = mht_queue_timer_list; item; item = item->next ) {
        mce_hbtimer_t *timer = item->data;

        if( !timer )
            compact = true;
        else if( trigger > timer->hbt_trigger )
            trigger = timer->hbt_trigger;
    }

    if( compact )
        mht_queue_garbage_collect();

537
    int64_t now = mce_lib_get_boot_tick();
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

    if( trigger < now )
        trigger = now;

    mht_glib_set_wakeup(trigger, now);
    mht_iphb_set_wakeup(trigger, now);

EXIT:
    return;
}

/** Scan registered heartbeat timers and notify triggered ones
 */
void
mht_queue_dispatch_timers(void)
{
    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

    if( !mce_hbtimer_initialized )
        goto EXIT;

    /* We need to be sure that actions resulting from
     * timer notifications do not cause recursive dispatching
     * to take place. */

    if( pthread_mutex_trylock(&mutex) != 0 )
        goto EXIT;

    /* Block suspend during dispatching */
#ifdef ENABLE_WAKELOCKS
    wakelock_lock("mce_hbtimer_dispatch", -1);
#endif

571
    int64_t now = mce_lib_get_boot_tick();
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685

    for( GSList *item = mht_queue_timer_list; item; item = item->next ) {
        mce_hbtimer_t *timer = item->data;

        if( !timer )
            continue;

        if( timer->hbt_trigger == NO_TICK )
            continue;

        mce_log(LL_DEBUG, "%s T%+"PRId64" ms",
                mce_hbtimer_get_name(timer),
                now - timer->hbt_trigger);

        if( now < timer->hbt_trigger )
            continue;

        mce_hbtimer_notify(timer);
    }

    /* Check the next timer to trigger */
    mht_queue_schedule_wakeups();

#ifdef ENABLE_WAKELOCKS
    wakelock_unlock("mce_hbtimer_dispatch");
#endif

    pthread_mutex_unlock(&mutex);

EXIT:
    return;
}

/* ========================================================================= *
 * GLIB_WAKEUPS
 * ========================================================================= */

/** Glib timeout callback for dispatching heartbeat timers
 *
 * @param aptr (not used)
 *
 * @return FALSE to stop timeout from repeating
 */
static gboolean
mht_glib_wakeup_cb(gpointer aptr)
{
    (void) aptr;

    if( !mce_hbtimer_glib_wait_id )
        goto EXIT;

    mce_hbtimer_glib_wait_id = 0;

    mce_log(LL_DEBUG, "glib wakeup; dispatch hbtimers");
    mht_queue_dispatch_timers();

EXIT:
    return FALSE;
}

/** Reprogram glib timeout for dispatching heartbeat timers
 *
 * @param trigger when to trigger
 * @param now     current time
 */
static void
mht_glib_set_wakeup(int64_t trigger, int64_t now)
{
    static int64_t prev = NO_TICK;

    int delay = -1;

    /* The glib timers use CLOCK_MONOTONIC while we need to evaluate
     * triggering in CLOCK_BOOTTIME.
     *
     * Assume glib reprogramming is cheap enough so that we do not need
     * to try to avoid it.
     */

    if( mce_hbtimer_glib_wait_id ) {
        g_source_remove(mce_hbtimer_glib_wait_id),
            mce_hbtimer_glib_wait_id = 0;
    }
    if( trigger != NO_TICK ) {
        delay = (int)(trigger - now);
        mce_hbtimer_glib_wait_id =
            g_timeout_add(delay, mht_glib_wakeup_cb, 0);
    }

    if( prev != trigger ) {
        prev = trigger;
        mce_log(LL_DEBUG, "glib wakeup in %d ms", delay);
    }
}

/* ========================================================================= *
 * IPHB_WAKEUPS
 * ========================================================================= */

/** iphb wakeup callback for dispatching heartbeat timers
 *
 * @param chn  io channel
 * @param cnd  io condition
 * @param data (unused)
 *
 * @return TRUE to keep io watch alive, or FALSE to disable it
 */
static gboolean
mht_iphb_wakeup_cb(GIOChannel *chn, GIOCondition cnd, gpointer data)
{
    (void)data;

    gboolean keep_going = FALSE;

686 687 688
    if( !mht_iphb_wakeup_watch_id )
        goto cleanup_nak;

689 690 691 692 693 694 695 696
    int fd = g_io_channel_unix_get_fd(chn);

    if( fd < 0 )
        goto cleanup_nak;

    if( cnd & ~G_IO_IN )
        goto cleanup_nak;

697 698 699
    if( !(cnd & G_IO_IN) )
        goto cleanup_ack;

700 701
    char buf[256];

702
    int rc = recv(fd, buf, sizeof buf, MSG_DONTWAIT);
703 704 705 706 707 708 709 710

    if( rc == 0 ) {
        if( !shutting_down )
            mce_log(LL_ERR, "unexpected eof");
        goto cleanup_nak;
    }

    if( rc == -1 ) {
711
        if( errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK )
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
            goto cleanup_ack;

        mce_log(LL_ERR, "read error: %m");
        goto cleanup_nak;
    }

    if( mht_iphb_wakeup_tick == NO_TICK )
        goto cleanup_ack;

    /* clear programmed state */
    mht_iphb_wakeup_tick = NO_TICK;

    /* notify */
    mce_log(LL_DEBUG, "iphb wakeup; dispatch hbtimers");
    mht_queue_dispatch_timers();

cleanup_ack:
    keep_going = TRUE;

cleanup_nak:

    if( !keep_going ) {
        mht_iphb_wakeup_watch_id = 0;
        mht_connection_close();
    }

    return keep_going;
}

/** Reprogram iphb timeout for dispatching heartbeat timers
 *
 * @param trigger when to trigger
 * @param now     current time
 */
static void
mht_iphb_set_wakeup(int64_t trigger, int64_t now)
{
    /* Assume: iphb timer should be stopped */
    int lo = 0;
    int hi = 0;
    int64_t tick = NO_TICK;

    if( mht_connection_handle && trigger != NO_TICK) {
        /* Calculate the iphb wakeup range to be used. */
        int64_t delay = (trigger - now + 999) / 1000;

        lo = (int)delay;
        hi = lo + MHT_IPHB_WAKEUP_MAX_DELAY_S;

        /* Calculate the next full BOOTTIME second after low bound
         * of iphb wakeup. This is used for avoiding constant iphb
         * ipc when wakeups get re-evaluated.
         *
         * There might be up to one second jitter in actual wakeup
         * vs cached time stamp, but it does not matter since:
         * 1) the wide iphb range means the wake ups from suspend
         *    are going to be off by several seconds on average
         *    anyway
         * 2) if the device does not get suspended, the higher
         *    resolution glib timeout gets triggered on time
         */

        tick = now + delay * 1000;
        tick += 999;
        tick -= tick % 1000;
    }

    if( mht_iphb_wakeup_tick != tick ) {
        mht_iphb_wakeup_tick = tick;

        if( mht_connection_handle )
            iphb_wait2(mht_connection_handle, lo, hi, 0, 1);

        mce_log(LL_DEBUG, "iphb wakeup in [%d, %d] s", lo, hi);
    }
}

/* ========================================================================= *
 * IPHB_CONNECTION
 * ========================================================================= */

/** Try to establish iphb socket connection
 *
 * Note: This is a helper function meant to be called only from
 *       mht_connection_open() and mht_connection_timer_cb()
 *       functions.
 *
 * @return true if connection is made, false otherwise
 */
static bool
mht_connection_try_to_open(void)
{
    iphb_t handle = 0;

    if( mht_connection_handle )
        goto cleanup;

    if( !(handle = iphb_open(0)) ) {
        mce_log(LL_WARN, "iphb_open: %m");
        goto cleanup;
    }

    int fd = iphb_get_fd(handle);
    if( fd == -1 ) {
        mce_log(LL_WARN, "iphb_get_fd: %m");
        goto cleanup;
    }

    /* set up io watch */
    mht_iphb_wakeup_watch_id =
        mht_add_iowatch(fd, false, G_IO_IN, mht_iphb_wakeup_cb, 0);

    if( !mht_iphb_wakeup_watch_id )
        goto cleanup;

    /* cache the handle */
    mht_connection_handle = handle, handle = 0;

    mce_log(LL_DEBUG, "iphb connected; dispatch hbtimers");
    mht_queue_dispatch_timers();

cleanup:

    if( handle ) iphb_close(handle);

    return mht_connection_handle != 0;
}

/** Callback for connect reattempt timer
 *
 * @param aptr  (unused)
 *
 * @return TRUE to keep timer repeating, or FALSE to stop it
 */
static gboolean
mht_connection_timer_cb(gpointer aptr)
{
    (void)aptr;

    if( !mht_connection_timer_id )
        goto cleanup;

    ++mht_connection_retry_no;

    if( !mht_connection_try_to_open() ) {
        if( mht_connection_retry_no < MHT_CONNECTION_MAX_RETRIES )
            goto cleanup;

        mce_log(LL_WARN, "connect failed %d times; giving up",
                mht_connection_retry_no);
    }
    else {
        mce_log(LL_DEBUG, "connected after %d retries",
                mht_connection_retry_no);
    }

    mht_connection_timer_id = 0;

cleanup:

    return mht_connection_timer_id != 0;
}

/** Start connect reattempt timer
 */
static void
mht_connection_start_timer(void)
{
    if( !mht_connection_timer_id && mce_hbtimer_initialized ) {
        mht_connection_retry_no = 0;
        mht_connection_timer_id =
            g_timeout_add(MHT_CONNECTION_RETRY_DELAY_MS,
                          mht_connection_timer_cb, 0);
    }
}

/** Cancel connect reattempt timer
 */
static void
mht_connection_stop_timer(void)
{
    if( mht_connection_timer_id ) {
        g_source_remove(mht_connection_timer_id),
            mht_connection_timer_id = 0;
    }
}

/** Predicate for: Connect reattempt timer is active
 */
static bool
mht_connection_is_pending(void)
{
    return mht_connection_timer_id != 0;
}

/** Start connecting to iphb socket
 *
 * If the connection cant be made immediately it
 * will be reattempted few times via timer callback.
 */
static void
mht_connection_open(void)
{
    if( mht_connection_is_pending() ) {
        // Retry timer already set up
    }
    else if( !mht_connection_try_to_open() ) {
        // Could not connect now - start retry timer
        mht_connection_start_timer();
    }
}

/** Close connection to iphb socket
 */
static void
mht_connection_close(void)
{
    /* stop pending connect attempts */
    mht_connection_stop_timer();

    /* remove io watch */
    if( mht_iphb_wakeup_watch_id ) {
        g_source_remove(mht_iphb_wakeup_watch_id),
            mht_iphb_wakeup_watch_id = 0;
    }

    /* close handle */
    if( mht_connection_handle ) {
        iphb_close(mht_connection_handle),
            mht_connection_handle = 0;

        mce_log(LL_DEBUG, "iphb disconnected");

        /* reset last programmed wakeup */
        mht_iphb_set_wakeup(NO_TICK, NO_TICK);
    }
}

/* ========================================================================= *
 * DATAPIPE_HANDLERS
 * ========================================================================= */

/** Resumed from suspend notification */
955
static void mht_datapipe_resume_detected_event_cb(gconstpointer data)
956 957 958 959 960 961 962 963 964 965
{
    (void) data;

    /* Check triggers & update glib wakeup after resume */
    mce_log(LL_DEBUG, "resumed; dispatch hbtimers");
    mht_queue_dispatch_timers();
}

/** Datapipe trigger for dsme availability
 */
966
static void mht_datapipe_dsme_service_state_cb(gconstpointer const data)
967
{
968 969
    service_state_t prev = dsme_service_state;
    dsme_service_state = GPOINTER_TO_INT(data);
970

971
    if( dsme_service_state == prev )
972 973 974 975
        goto EXIT;

    mce_log(LL_DEBUG, "DSME dbus service: %s -> %s",
            service_state_repr(prev),
976
            service_state_repr(dsme_service_state));
977

978
    if( dsme_service_state == SERVICE_STATE_RUNNING )
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
        mht_connection_open();
    else
        mht_connection_close();

EXIT:
    return;
}

/** Change notifications for shutting_down
 */
static void mht_datapipe_shutting_down_cb(gconstpointer data)
{
    bool prev = shutting_down;
    shutting_down = GPOINTER_TO_INT(data);

    if( shutting_down == prev )
        goto EXIT;

997 998
    mce_log(LL_DEBUG, "shutting_down = %d -> %d",
            prev, shutting_down);
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

    /* Loss of iphb connection is expected during shutdown */

EXIT:
    return;
}

/** Array of datapipe handlers */
static datapipe_handler_t mht_datapipe_handlers[] =
{
    // output triggers
    {
1011 1012
        .datapipe  = &resume_detected_event_pipe,
        .output_cb = mht_datapipe_resume_detected_event_cb,
1013 1014
    },
    {
1015 1016
        .datapipe  = &dsme_service_state_pipe,
        .output_cb = mht_datapipe_dsme_service_state_cb,
1017 1018
    },
    {
1019
        .datapipe  = &shutting_down_pipe,
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
        .output_cb = mht_datapipe_shutting_down_cb,
    },

    // sentinel
    {
        .datapipe = 0,
    }
};

static datapipe_bindings_t mht_datapipe_bindings =
{
    .module   = "mce_hbtimer",
    .handlers = mht_datapipe_handlers,
};

/** Append triggers/filters to datapipes
 */
static void mht_datapipe_init(void)
{
1039
    mce_datapipe_init_bindings(&mht_datapipe_bindings);
1040 1041 1042 1043 1044 1045
}

/** Remove triggers/filters from datapipes
 */
static void mht_datapipe_quit(void)
{
1046
    mce_datapipe_quit_bindings(&mht_datapipe_bindings);
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
}

/* ========================================================================= *
 * MODULE_INIT
 * ========================================================================= */

void
mce_hbtimer_init(void)
{
    /* Connect to datapipes */
    mht_datapipe_init();

    /* Mark as initialized */
    mce_hbtimer_initialized = true;

    /* Schedule timers that have been created
     * before initialization took place */
    mht_queue_schedule_wakeups();
}

void
mce_hbtimer_quit(void)
{
    /* Mark as de-initialized */
    mce_hbtimer_initialized = false;

    /* Disconnect from datapipes */
    mht_datapipe_quit();

    /* Remove wakeups */
    mht_glib_set_wakeup(NO_TICK, NO_TICK);
    mht_iphb_set_wakeup(NO_TICK, NO_TICK);

    /* close iphb connection */
    mht_connection_close();
}