mce-common.c 33.5 KB
Newer Older
1 2 3 4
/**
 * @file mce-common.c
 * Common state logic for Mode Control Entity
 * <p>
5 6
 * Copyright (c) 2017 - 2020 Jolla Ltd.
 * Copyright (c) 2019 - 2020 Open Mobile Platform LLC.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * <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-common.h"

#include "mce.h"
#include "mce-dbus.h"
26
#include "mce-lib.h"
27 28 29 30 31 32 33
#include "mce-log.h"

#include <string.h>

#include <mce/dbus-names.h>
#include <mce/mode-names.h>

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/* ========================================================================= *
 * TYPES
 * ========================================================================= */

/** Bookkeeping data for on-condition callback function */
typedef struct
{
    /** Source identification, for mass cancelation */
    gchar         *srce;

    /** Function to call */
    GDestroyNotify func;

    /** Parameter to give to the function */
    gpointer       aptr;
} on_condition_t;

51 52 53 54
/* ========================================================================= *
 * PROTOTYPES
 * ========================================================================= */

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
/* ------------------------------------------------------------------------- *
 * ON_CONDITION
 * ------------------------------------------------------------------------- */

static bool            on_condition_matches           (on_condition_t *self, const char *srce, GDestroyNotify func, gpointer aptr);
static on_condition_t *on_condition_create            (const char *srce, GDestroyNotify func, gpointer aptr);
static void            on_condition_delete            (on_condition_t *self);
static void            on_condition_exec              (on_condition_t *self);
static void            on_condition_exec_and_delete_cb(gpointer self);
static void            on_condition_delete_cb         (gpointer self);

/* ------------------------------------------------------------------------- *
 * COMMON_ON_PROXIMITY
 * ------------------------------------------------------------------------- */

static gboolean common_on_proximity_exec_cb (gpointer aptr);
static void     common_on_proximity_exec    (void);
void            common_on_proximity_schedule(const char *srce, GDestroyNotify func, gpointer aptr);
void            common_on_proximity_cancel  (const char *srce, GDestroyNotify func, gpointer aptr);
static void     common_on_proximity_quit    (void);

/* ------------------------------------------------------------------------- *
 * COMMON_DBUS
 * ------------------------------------------------------------------------- */

80 81
static void     common_dbus_send_usb_cable_state  (DBusMessage *const req);
static gboolean common_dbus_get_usb_cable_state_cb(DBusMessage *const req);
82 83
static void     common_dbus_send_charger_type     (DBusMessage *const req);
static gboolean common_dbus_get_charger_type_cb   (DBusMessage *const req);
84 85 86 87
static void     common_dbus_send_charger_state    (DBusMessage *const req);
static gboolean common_dbus_get_charger_state_cb  (DBusMessage *const req);
static void     common_dbus_send_battery_status   (DBusMessage *const req);
static gboolean common_dbus_get_battery_status_cb (DBusMessage *const req);
88 89
static void     common_dbus_send_battery_state    (DBusMessage *const req);
static gboolean common_dbus_get_battery_state_cb  (DBusMessage *const req);
90 91
static void     common_dbus_send_battery_level    (DBusMessage *const req);
static gboolean common_dbus_get_battery_level_cb  (DBusMessage *const req);
92
static gboolean common_dbus_initial_cb            (gpointer aptr);
93 94 95
static void     common_dbus_init                  (void);
static void     common_dbus_quit                  (void);

96 97 98 99 100
/* ------------------------------------------------------------------------- *
 * COMMON_DATAPIPE
 * ------------------------------------------------------------------------- */

static void common_datapipe_usb_cable_state_cb        (gconstpointer data);
101
static void common_datapipe_charger_type_cb           (gconstpointer data);
102 103
static void common_datapipe_charger_state_cb          (gconstpointer data);
static void common_datapipe_battery_status_cb         (gconstpointer data);
104
static void common_datapipe_battery_state_cb          (gconstpointer data);
105 106 107 108 109 110 111 112
static void common_datapipe_battery_level_cb          (gconstpointer data);
static void common_datapipe_proximity_sensor_actual_cb(gconstpointer data);
static void common_datapipe_init                      (void);
static void common_datapipe_quit                      (void);

/* ------------------------------------------------------------------------- *
 * MCE_COMMON
 * ------------------------------------------------------------------------- */
113 114 115 116 117 118 119 120 121 122 123

bool mce_common_init(void);
void mce_common_quit(void);

/* ========================================================================= *
 * STATE_DATA
 * ========================================================================= */

/** USB cable status; assume undefined */
static usb_cable_state_t usb_cable_state = USB_CABLE_UNDEF;

124 125 126
/** Charger type; assume none */
static charger_type_t charger_type = CHARGER_TYPE_NONE;

127 128 129 130 131 132
/** Charger state; assume undefined */
static charger_state_t charger_state = CHARGER_STATE_UNDEF;

/** Battery status; assume undefined */
static battery_status_t battery_status = BATTERY_STATUS_UNDEF;

133 134 135
/** Battery state; assume unknown */
static battery_state_t battery_state = BATTERY_STATE_UNKNOWN;

136 137
/** Battery charge level: assume unknown */
static gint battery_level = MCE_BATTERY_LEVEL_UNKNOWN;
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
/** Cached (raw) proximity sensor state */
static cover_state_t proximity_sensor_actual = COVER_UNDEF;

/* ========================================================================= *
 * ON_CONDITION
 * ========================================================================= */

static bool
on_condition_matches(on_condition_t *self,
                     const char *srce, GDestroyNotify func, gpointer aptr)
{
    bool matches = false;

    if( !srce || !self || !self->srce )
        goto EXIT;

    if( strcmp(self->srce, srce) )
        goto EXIT;

    if( self->func == func && self->aptr == aptr )
        matches = true;
    else
        matches = !func && !srce;

EXIT:
    return matches;
}

static on_condition_t *
on_condition_create(const char *srce, GDestroyNotify func, gpointer aptr)
{
    on_condition_t *self = g_slice_alloc0(sizeof *self);
    self->srce = g_strdup(srce);
    self->func = func;
    self->aptr = aptr;
    return self;
}

static void
on_condition_delete(on_condition_t *self)
{
    if( self ) {
        g_free(self->srce);
        g_slice_free1(sizeof *self, self);
    }
}

static void
on_condition_exec(on_condition_t *self)
{
    if( self )
        self->func(self->aptr);
}

static void
on_condition_exec_and_delete_cb(gpointer self)
{
    on_condition_exec(self);
    on_condition_delete(self);
}

static void
on_condition_delete_cb(gpointer self)
{
    on_condition_delete(self);
}

/* ========================================================================= *
 * COMMON_ON_PROXIMITY
 * ========================================================================= */

#define COMMON_ON_DEMAND_TAG "common_on_proximity"

static GSList *common_on_proximity_actions = 0;
static guint   common_on_proximity_exec_id = 0;

static gboolean
common_on_proximity_exec_cb(gpointer aptr)
{
    (void)aptr;

    gboolean result = G_SOURCE_REMOVE;

    GSList *todo;

    /* Execute queued actions */
    if( (todo = common_on_proximity_actions) ) {
        common_on_proximity_actions = 0;
        todo = g_slist_reverse(todo);
        g_slist_free_full(todo, on_condition_exec_and_delete_cb);
    }

    /* Check if executed actions queued more actions */
    if( common_on_proximity_actions ) {
        /* Repeat to handle freshly added actions */
        result = G_SOURCE_CONTINUE;
    }
    else {
        /* Queue exchausted - sensor no longer needed */
        datapipe_exec_full(&proximity_sensor_required_pipe,
                           PROXIMITY_SENSOR_REQUIRED_REM
                           COMMON_ON_DEMAND_TAG);
    }

    if( result == G_SOURCE_REMOVE )
        common_on_proximity_exec_id = 0;

    return result;
}

static void
common_on_proximity_exec(void)
{
    /* Execute via idle to make sure all proximity
     * datapipe listeners have had a chance to
     * register sensor state before callbacks
     * get triggered. */
    if( !common_on_proximity_exec_id ) {
        common_on_proximity_exec_id =
            mce_wakelocked_idle_add(common_on_proximity_exec_cb,
                                    0);
    }
}

/** Execute callback function when actual proximity sensor state is available
 *
 * @param srce  Call site identification
 * @param func  Callback function pointer
 * @param aptr  Parameter for the callback function
 */
void
common_on_proximity_schedule(const char *srce, GDestroyNotify func, gpointer aptr)
{
    /* In order to execute actions in the requested order,
     * immediate execution can be allowed only when proximity
     * sensor state is known and the already queued actions
     * have been executed.
     */
    if( proximity_sensor_actual == COVER_UNDEF ||
        common_on_proximity_actions ||
        common_on_proximity_exec_id ) {
        // TODO: all failures to communicate sensor power up with
        //       sensorfwd should lead to mce-sensorfw module
        //       declaring proximity=not-covered, but having an
        //       explicit timeout here would not hurt ...
        if( !common_on_proximity_actions )
            datapipe_exec_full(&proximity_sensor_required_pipe,
                               PROXIMITY_SENSOR_REQUIRED_ADD
                               COMMON_ON_DEMAND_TAG);

        common_on_proximity_actions =
            g_slist_prepend(common_on_proximity_actions,
                            on_condition_create(srce, func, aptr));
    }
    else {
        func(aptr);
    }
}

/** Cancel pending on-proximity callback
 *
 * @param srce  Call site identification
 * @param func  Callback function pointer, or NULL for all
 * @param aptr  Parameter for the callback function, or NULL for all
 */
void
common_on_proximity_cancel(const char *srce, GDestroyNotify func, gpointer aptr)
{
    for( GSList *iter = common_on_proximity_actions; iter; iter = iter->next ) {
        on_condition_t *item = iter->data;

        if( !item )
            continue;

        if( !on_condition_matches(item, srce, func, aptr) )
            continue;

        /* Detach from list and delete.
         *
         * Note that the list itself is not garbage collected in order not
         * to disturb possibly pending execute etc logic -> all iterators
         * must be prepared to bump into empty links.
         */
        iter->data = 0;
        on_condition_delete(item);
    }
}

static void
common_on_proximity_quit(void)
{
    /* Cancel pending "on_condition" actions */
    g_slist_free_full(common_on_proximity_actions,
                      on_condition_delete_cb),
        common_on_proximity_actions = 0;

    /* Do not leave active timers behind */
    if( common_on_proximity_exec_id ) {
        g_source_remove(common_on_proximity_exec_id),
            common_on_proximity_exec_id = 0;
    }
}

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

/* ------------------------------------------------------------------------- *
 * usb_cable_state
 * ------------------------------------------------------------------------- */

/** Send usb_cable_state D-Bus signal / method call reply
 *
 * @param req  method call message to reply, or NULL to send signal
 */
static void
common_dbus_send_usb_cable_state(DBusMessage *const req)
{
    static const char *last = 0;

    DBusMessage *msg = NULL;

    const char *value = usb_cable_state_to_dbus(usb_cable_state);

    if( req ) {
        msg = dbus_new_method_reply(req);
    }
    else if( last == value ) {
        goto EXIT;
    }
    else {
        last = value;
        msg = dbus_new_signal(MCE_SIGNAL_PATH,
                              MCE_SIGNAL_IF,
                              MCE_USB_CABLE_STATE_SIG);
    }

    if( !dbus_message_append_args(msg,
                                  DBUS_TYPE_STRING, &value,
                                  DBUS_TYPE_INVALID) )
        goto EXIT;

    mce_log(LL_DEBUG, "%s: %s = %s",
            req ? "reply" : "broadcast",
            "usb_cable_state", value);

    dbus_send_message(msg), msg = 0;

EXIT:

    if( msg )
        dbus_message_unref(msg);
}

/** Callback for handling usb_cable_state D-Bus queries
 *
 * @param req  method call message to reply
 */
static gboolean
common_dbus_get_usb_cable_state_cb(DBusMessage *const req)
{
    mce_log(LL_DEBUG, "usb_cable_state query from: %s",
            mce_dbus_get_message_sender_ident(req));

    if( !dbus_message_get_no_reply(req) )
        common_dbus_send_usb_cable_state(req);

    return TRUE;
}

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
/* ------------------------------------------------------------------------- *
 * charger_type
 * ------------------------------------------------------------------------- */

/** Send charger_type D-Bus signal / method call reply
 *
 * @param req  method call message to reply, or NULL to send signal
 */
static void
common_dbus_send_charger_type(DBusMessage *const req)
{
    static const char *last = 0;

    DBusMessage *msg = NULL;

    const char *value = charger_type_to_dbus(charger_type);

    if( req ) {
        msg = dbus_new_method_reply(req);
    }
    else if( last == value ) {
        goto EXIT;
    }
    else {
        last = value;
        msg = dbus_new_signal(MCE_SIGNAL_PATH,
                              MCE_SIGNAL_IF,
                              MCE_CHARGER_TYPE_SIG);
    }

    if( !dbus_message_append_args(msg,
                                  DBUS_TYPE_STRING, &value,
                                  DBUS_TYPE_INVALID) )
        goto EXIT;

    mce_log(LL_DEBUG, "%s: %s = %s",
            req ? "reply" : "broadcast",
            "charger_type", value);

    dbus_send_message(msg), msg = 0;

EXIT:

    if( msg )
        dbus_message_unref(msg);
}

/** Callback for handling charger_type D-Bus queries
 *
 * @param req  method call message to reply
 */
static gboolean
common_dbus_get_charger_type_cb(DBusMessage *const req)
{
    mce_log(LL_DEBUG, "charger_type query from: %s",
            mce_dbus_get_message_sender_ident(req));

    if( !dbus_message_get_no_reply(req) )
        common_dbus_send_charger_type(req);

    return TRUE;
}

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 537 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 571 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
/* ------------------------------------------------------------------------- *
 * charger_state
 * ------------------------------------------------------------------------- */

/** Send charger_state D-Bus signal / method call reply
 *
 * @param req  method call message to reply, or NULL to send signal
 */
static void
common_dbus_send_charger_state(DBusMessage *const req)
{
    static const char *last = 0;

    DBusMessage *msg = NULL;

    const char *value = charger_state_to_dbus(charger_state);

    if( req ) {
        msg = dbus_new_method_reply(req);
    }
    else if( last == value ) {
        goto EXIT;
    }
    else {
        last = value;
        msg = dbus_new_signal(MCE_SIGNAL_PATH,
                              MCE_SIGNAL_IF,
                              MCE_CHARGER_STATE_SIG);
    }

    if( !dbus_message_append_args(msg,
                                  DBUS_TYPE_STRING, &value,
                                  DBUS_TYPE_INVALID) )
        goto EXIT;

    mce_log(LL_DEBUG, "%s: %s = %s",
            req ? "reply" : "broadcast",
            "charger_state", value);

    dbus_send_message(msg), msg = 0;

EXIT:

    if( msg )
        dbus_message_unref(msg);
}

/** Callback for handling charger_state D-Bus queries
 *
 * @param req  method call message to reply
 */
static gboolean
common_dbus_get_charger_state_cb(DBusMessage *const req)
{
    mce_log(LL_DEBUG, "charger_state query from: %s",
            mce_dbus_get_message_sender_ident(req));

    if( !dbus_message_get_no_reply(req) )
        common_dbus_send_charger_state(req);

    return TRUE;
}

/* ------------------------------------------------------------------------- *
 * battery_status
 * ------------------------------------------------------------------------- */

/** Send battery_status D-Bus signal / method call reply
 *
 * @param req  method call message to reply, or NULL to send signal
 */
static void
common_dbus_send_battery_status(DBusMessage *const req)
{
    static const char *last = 0;

    DBusMessage *msg = NULL;

    const char *value = battery_status_to_dbus(battery_status);

    if( req ) {
        msg = dbus_new_method_reply(req);
    }
    else if( last == value ) {
        goto EXIT;
    }
    else {
        last = value;
        msg = dbus_new_signal(MCE_SIGNAL_PATH,
                              MCE_SIGNAL_IF,
                              MCE_BATTERY_STATUS_SIG);
    }

    if( !dbus_message_append_args(msg,
                                  DBUS_TYPE_STRING, &value,
                                  DBUS_TYPE_INVALID) )
        goto EXIT;

    mce_log(LL_DEBUG, "%s: %s = %s",
            req ? "reply" : "broadcast",
            "battery_status", value);

    dbus_send_message(msg), msg = 0;

EXIT:

    if( msg )
        dbus_message_unref(msg);
}

/** Callback for handling battery_status D-Bus queries
 *
 * @param req  method call message to reply
 */
static gboolean
common_dbus_get_battery_status_cb(DBusMessage *const req)
{
    mce_log(LL_DEBUG, "battery_status query from: %s",
            mce_dbus_get_message_sender_ident(req));

    if( !dbus_message_get_no_reply(req) )
        common_dbus_send_battery_status(req);

    return TRUE;
}

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
/* ------------------------------------------------------------------------- *
 * battery_state
 * ------------------------------------------------------------------------- */

/** Send battery_state D-Bus signal / method call reply
 *
 * @param req  method call message to reply, or NULL to send signal
 */
static void
common_dbus_send_battery_state(DBusMessage *const req)
{
    static const char *last = 0;

    DBusMessage *msg = NULL;

    const char *value = battery_state_to_dbus(battery_state);

    if( req ) {
        msg = dbus_new_method_reply(req);
    }
    else if( last == value ) {
        goto EXIT;
    }
    else {
        last = value;
        msg = dbus_new_signal(MCE_SIGNAL_PATH,
                              MCE_SIGNAL_IF,
                              MCE_BATTERY_STATE_SIG);
    }

    if( !dbus_message_append_args(msg,
                                  DBUS_TYPE_STRING, &value,
                                  DBUS_TYPE_INVALID) )
        goto EXIT;

    mce_log(LL_DEBUG, "%s: %s = %s",
            req ? "reply" : "broadcast",
            "battery_state", value);

    dbus_send_message(msg), msg = 0;

EXIT:

    if( msg )
        dbus_message_unref(msg);
}

/** Callback for handling battery_state D-Bus queries
 *
 * @param req  method call message to reply
 */
static gboolean
common_dbus_get_battery_state_cb(DBusMessage *const req)
{
    mce_log(LL_DEBUG, "battery_state query from: %s",
            mce_dbus_get_message_sender_ident(req));

    if( !dbus_message_get_no_reply(req) )
        common_dbus_send_battery_state(req);

    return TRUE;
}

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 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
/* ------------------------------------------------------------------------- *
 * battery_level
 * ------------------------------------------------------------------------- */

/** Send battery_level D-Bus signal / method call reply
 *
 * @param req  method call message to reply, or NULL to send signal
 */
static void
common_dbus_send_battery_level(DBusMessage *const req)
{
    static dbus_int32_t last = MCE_BATTERY_LEVEL_UNKNOWN - 1;

    DBusMessage *msg = NULL;

    dbus_int32_t value = battery_level;

    /* Normalize to values allowed by MCE D-Bus api documentation */
    if( value < 0 )
        value = MCE_BATTERY_LEVEL_UNKNOWN;
    else if( value > 100 )
        value = 100;

    if( req ) {
        msg = dbus_new_method_reply(req);
    }
    else if( last == value ) {
        goto EXIT;
    }
    else {
        last = value;
        msg = dbus_new_signal(MCE_SIGNAL_PATH,
                              MCE_SIGNAL_IF,
                              MCE_BATTERY_LEVEL_SIG);
    }

    if( !dbus_message_append_args(msg,
                                  DBUS_TYPE_INT32, &value,
                                  DBUS_TYPE_INVALID) )
        goto EXIT;

    mce_log(LL_DEBUG, "%s: %s = %d",
            req ? "reply" : "broadcast",
704
            "battery_level", value);
705 706 707 708 709 710 711 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

    dbus_send_message(msg), msg = 0;

EXIT:

    if( msg )
        dbus_message_unref(msg);
}

/** Callback for handling battery_level D-Bus queries
 *
 * @param req  method call message to reply
 */
static gboolean
common_dbus_get_battery_level_cb(DBusMessage *const req)
{
    mce_log(LL_DEBUG, "battery_level query from: %s",
            mce_dbus_get_message_sender_ident(req));

    if( !dbus_message_get_no_reply(req) )
        common_dbus_send_battery_level(req);

    return TRUE;
}

/* ------------------------------------------------------------------------- *
 * init/quit
 * ------------------------------------------------------------------------- */

/** Array of dbus message handlers */
static mce_dbus_handler_t common_dbus_handlers[] =
{
    /* signals - outbound (for Introspect purposes only) */
    {
        .interface = MCE_SIGNAL_IF,
        .name      = MCE_USB_CABLE_STATE_SIG,
        .type      = DBUS_MESSAGE_TYPE_SIGNAL,
        .args      =
            "    <arg name=\"usb_cable_state\" type=\"s\"/>\n"
    },
745 746 747 748 749 750 751
    {
        .interface = MCE_SIGNAL_IF,
        .name      = MCE_CHARGER_TYPE_SIG,
        .type      = DBUS_MESSAGE_TYPE_SIGNAL,
        .args      =
            "    <arg name=\"charger_type\" type=\"s\"/>\n"
    },
752 753 754 755 756 757 758 759 760 761 762 763 764 765
    {
        .interface = MCE_SIGNAL_IF,
        .name      = MCE_CHARGER_STATE_SIG,
        .type      = DBUS_MESSAGE_TYPE_SIGNAL,
        .args      =
            "    <arg name=\"charger_state\" type=\"s\"/>\n"
    },
    {
        .interface = MCE_SIGNAL_IF,
        .name      = MCE_BATTERY_STATUS_SIG,
        .type      = DBUS_MESSAGE_TYPE_SIGNAL,
        .args      =
            "    <arg name=\"battery_status\" type=\"s\"/>\n"
    },
766 767 768 769 770 771 772
    {
        .interface = MCE_SIGNAL_IF,
        .name      = MCE_BATTERY_STATE_SIG,
        .type      = DBUS_MESSAGE_TYPE_SIGNAL,
        .args      =
            "    <arg name=\"battery_state\" type=\"s\"/>\n"
    },
773 774 775 776 777 778 779 780 781 782 783 784 785
    {
        .interface = MCE_SIGNAL_IF,
        .name      = MCE_BATTERY_LEVEL_SIG,
        .type      = DBUS_MESSAGE_TYPE_SIGNAL,
        .args      =
            "    <arg name=\"battery_level\" type=\"i\"/>\n"
    },
    /* method calls */
    {
        .interface = MCE_REQUEST_IF,
        .name      = MCE_USB_CABLE_STATE_GET,
        .type      = DBUS_MESSAGE_TYPE_METHOD_CALL,
        .callback  = common_dbus_get_usb_cable_state_cb,
786 787
        .args      =
            "    <arg direction=\"out\" name=\"usb_cable_state\" type=\"s\"/>\n"
788
    },
789 790 791 792 793 794 795 796
    {
        .interface = MCE_REQUEST_IF,
        .name      = MCE_CHARGER_TYPE_GET,
        .type      = DBUS_MESSAGE_TYPE_METHOD_CALL,
        .callback  = common_dbus_get_charger_type_cb,
        .args      =
            "    <arg direction=\"out\" name=\"charger_type\" type=\"s\"/>\n"
    },
797 798 799 800 801
    {
        .interface = MCE_REQUEST_IF,
        .name      = MCE_CHARGER_STATE_GET,
        .type      = DBUS_MESSAGE_TYPE_METHOD_CALL,
        .callback  = common_dbus_get_charger_state_cb,
802 803
        .args      =
            "    <arg direction=\"out\" name=\"charger_state\" type=\"s\"/>\n"
804 805 806 807 808 809
    },
    {
        .interface = MCE_REQUEST_IF,
        .name      = MCE_BATTERY_STATUS_GET,
        .type      = DBUS_MESSAGE_TYPE_METHOD_CALL,
        .callback  = common_dbus_get_battery_status_cb,
810 811
        .args      =
            "    <arg direction=\"out\" name=\"battery_status\" type=\"s\"/>\n"
812
    },
813 814 815 816 817 818 819 820
    {
        .interface = MCE_REQUEST_IF,
        .name      = MCE_BATTERY_STATE_GET,
        .type      = DBUS_MESSAGE_TYPE_METHOD_CALL,
        .callback  = common_dbus_get_battery_state_cb,
        .args      =
            "    <arg direction=\"out\" name=\"battery_state\" type=\"s\"/>\n"
    },
821 822 823 824 825
    {
        .interface = MCE_REQUEST_IF,
        .name      = MCE_BATTERY_LEVEL_GET,
        .type      = DBUS_MESSAGE_TYPE_METHOD_CALL,
        .callback  = common_dbus_get_battery_level_cb,
826 827
        .args      =
            "    <arg direction=\"out\" name=\"battery_level\" type=\"i\"/>\n"
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
    },
    /* sentinel */
    {
        .interface = 0
    }
};

/** Timer callback id for broadcasting initial states */
static guint common_dbus_initial_id = 0;

/** Timer callback function for broadcasting initial states
 *
 * @param aptr (not used)
 *
 * @return FALSE to stop idle callback from repeating
 */
static gboolean common_dbus_initial_cb(gpointer aptr)
{
    (void)aptr;

    /* Do explicit broadcast of initial states.
     *
     * Note that we expect nothing to happen here, unless the
     * datapipe initialization for some reason ends up leaving
     * some values to undefined state.
     */
    common_dbus_send_usb_cable_state(0);
855
    common_dbus_send_charger_type(0);
856 857
    common_dbus_send_charger_state(0);
    common_dbus_send_battery_status(0);
858
    common_dbus_send_battery_state(0);
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
    common_dbus_send_battery_level(0);

    common_dbus_initial_id = 0;
    return FALSE;
}

/** Add dbus handlers
 */
static void common_dbus_init(void)
{
    mce_dbus_handler_register_array(common_dbus_handlers);

    /* To avoid unnecessary jitter on startup, allow dbus service tracking
     * and datapipe initialization some time to come up with proper initial
     * state values before forcing broadcasting to dbus */
    if( !common_dbus_initial_id )
        common_dbus_initial_id = g_timeout_add(1000, common_dbus_initial_cb, 0);
}

/** Remove dbus handlers
 */
static void common_dbus_quit(void)
{
    if( common_dbus_initial_id ) {
        g_source_remove(common_dbus_initial_id),
            common_dbus_initial_id = 0;
    }

    mce_dbus_handler_unregister_array(common_dbus_handlers);
}

/* ========================================================================= *
 * DATAPIPE_FUNCTIONS
 * ========================================================================= */

/* ------------------------------------------------------------------------- *
 * usb_cable_state
 * ------------------------------------------------------------------------- */

/** Callback for handling usb_cable_state_pipe state changes
 *
 * @param data usb_cable_state (as void pointer)
 */
static void common_datapipe_usb_cable_state_cb(gconstpointer data)
{
    usb_cable_state_t prev = usb_cable_state;
    usb_cable_state = GPOINTER_TO_INT(data);

    if( usb_cable_state == prev )
        goto EXIT;

    /* The enumerated states do not have 1:1 string mapping, so to
     * avoid sending duplicate signals also the representation
     * values need to be checked. */
    const char *value_old = usb_cable_state_to_dbus(prev);
    const char *value_new = usb_cable_state_to_dbus(usb_cable_state);

    if( !strcmp(value_old, value_new) )
        goto EXIT;

    mce_log(LL_DEBUG, "usb_cable_state = %s -> %s",
            value_old, value_new);

    common_dbus_send_usb_cable_state(0);

EXIT:
    return;
}

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
/* ------------------------------------------------------------------------- *
 * charger_type
 * ------------------------------------------------------------------------- */

/** Callback for handling charger_type_pipe state changes
 *
 * @param data charger_type (as void pointer)
 */
static void common_datapipe_charger_type_cb(gconstpointer data)
{
    charger_type_t prev = charger_type;
    charger_type = GPOINTER_TO_INT(data);

    if( charger_type == prev )
        goto EXIT;

    mce_log(LL_DEBUG, "charger_type = %s -> %s",
            charger_type_repr(prev),
            charger_type_repr(charger_type));

    common_dbus_send_charger_type(0);

EXIT:
    return;
}

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
/* ------------------------------------------------------------------------- *
 * charger_state
 * ------------------------------------------------------------------------- */

/** Callback for handling charger_state_pipe state changes
 *
 * @param data charger_state (as void pointer)
 */
static void common_datapipe_charger_state_cb(gconstpointer data)
{
    charger_state_t prev = charger_state;
    charger_state = GPOINTER_TO_INT(data);

    if( charger_state == prev )
        goto EXIT;

    mce_log(LL_DEBUG, "charger_state = %s -> %s",
            charger_state_repr(prev),
            charger_state_repr(charger_state));

    common_dbus_send_charger_state(0);

EXIT:
    return;
}

/* ------------------------------------------------------------------------- *
 * battery_status
 * ------------------------------------------------------------------------- */

/** Callback for handling battery_status_pipe state changes
 *
 * @param data battery_status (as void pointer)
 */
static void common_datapipe_battery_status_cb(gconstpointer data)
{
    battery_status_t prev = battery_status;
    battery_status = GPOINTER_TO_INT(data);

    if( battery_status == prev )
        goto EXIT;

    mce_log(LL_DEBUG, "battery_status = %s -> %s",
            battery_status_repr(prev),
            battery_status_repr(battery_status));

    common_dbus_send_battery_status(0);

EXIT:
    return;
}

1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
/* ------------------------------------------------------------------------- *
 * battery_state
 * ------------------------------------------------------------------------- */

/** Callback for handling battery_state_pipe state changes
 *
 * @param data battery_state (as void pointer)
 */
static void common_datapipe_battery_state_cb(gconstpointer data)
{
    battery_state_t prev = battery_state;
    battery_state = GPOINTER_TO_INT(data);

    if( battery_state == prev )
        goto EXIT;

    mce_log(LL_DEBUG, "battery_state = %s -> %s",
            battery_state_repr(prev),
            battery_state_repr(battery_state));

    common_dbus_send_battery_state(0);

EXIT:
    return;
}

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
/* ------------------------------------------------------------------------- *
 * battery_level
 * ------------------------------------------------------------------------- */

/** Callback for handling battery_level_pipe state changes
 *
 * @param data battery_level (as void pointer)
 */
static void common_datapipe_battery_level_cb(gconstpointer data)
{
    gint prev = battery_level;
    battery_level = GPOINTER_TO_INT(data);

    if( battery_level == prev )
        goto EXIT;

    mce_log(LL_DEBUG, "battery_level = %d -> %d",
            prev, battery_level);

    common_dbus_send_battery_level(0);

EXIT:
    return;
}

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
/* ------------------------------------------------------------------------- *
 * proximity_sensor
 * ------------------------------------------------------------------------- */

/** Change notifications for proximity_sensor_actual
 */
static void common_datapipe_proximity_sensor_actual_cb(gconstpointer data)
{
    cover_state_t prev = proximity_sensor_actual;
    proximity_sensor_actual = GPOINTER_TO_INT(data);

    if( proximity_sensor_actual == prev )
        goto EXIT;

    mce_log(LL_DEBUG, "proximity_sensor_actual = %s -> %s",
            proximity_state_repr(prev),
            proximity_state_repr(proximity_sensor_actual));

    if( proximity_sensor_actual != COVER_UNDEF )
        common_on_proximity_exec();

EXIT:
    return;
}

1082 1083 1084 1085 1086 1087 1088 1089 1090
/* ------------------------------------------------------------------------- *
 * init/quit
 * ------------------------------------------------------------------------- */

/** Array of datapipe handlers */
static datapipe_handler_t common_datapipe_handlers[] =
{
    // output triggers
    {
1091
        .datapipe  = &usb_cable_state_pipe,
1092 1093
        .output_cb = common_datapipe_usb_cable_state_cb,
    },
1094 1095 1096 1097
    {
        .datapipe  = &charger_type_pipe,
        .output_cb = common_datapipe_charger_type_cb,
    },
1098 1099 1100 1101 1102 1103 1104 1105
    {
        .datapipe  = &charger_state_pipe,
        .output_cb = common_datapipe_charger_state_cb,
    },
    {
        .datapipe  = &battery_status_pipe,
        .output_cb = common_datapipe_battery_status_cb,
    },
1106 1107 1108 1109
    {
        .datapipe  = &battery_state_pipe,
        .output_cb = common_datapipe_battery_state_cb,
    },
1110 1111 1112 1113
    {
        .datapipe  = &battery_level_pipe,
        .output_cb = common_datapipe_battery_level_cb,
    },
1114 1115 1116 1117
    {
        .datapipe  = &proximity_sensor_actual_pipe,
        .output_cb = common_datapipe_proximity_sensor_actual_cb,
    },
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
    // sentinel
    {
        .datapipe = 0,
    }
};

static datapipe_bindings_t common_datapipe_bindings =
{
    .module   = "common",
    .handlers = common_datapipe_handlers,
};

/** Append triggers/filters to datapipes
 */
static void common_datapipe_init(void)
{
1134
    mce_datapipe_init_bindings(&common_datapipe_bindings);
1135 1136 1137 1138 1139 1140
}

/** Remove triggers/filters from datapipes
 */
static void common_datapipe_quit(void)
{
1141
    mce_datapipe_quit_bindings(&common_datapipe_bindings);
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
}

/* ========================================================================= *
 * MODULE_INIT_QUIT
 * ========================================================================= */

/** Initialize common functionality
 */
bool
mce_common_init(void)
{
    /* attach to internal state variables */
    common_datapipe_init();

    /* set up dbus message handlers */
    common_dbus_init();

    return true;
}

/** De-initialize common functionality
 */
void
mce_common_quit(void)
{
    /* remove all handlers */
    common_dbus_quit();
    common_datapipe_quit();
1170
    common_on_proximity_quit();
1171
}