mce-sensorfw.c 153 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
/**
 * @file mce-sensorfw.c
 *
 * Mode Control Entity - Interprocess communication with sensord
 *
 * <p>
 *
 * Copyright (C) 2013-2014 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/>.
 */

27
#include "mce-sensorfw.h"
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
#include "mce.h"
#include "mce-log.h"
#include "mce-dbus.h"
#include "libwakelock.h"

#include <linux/input.h>

#include <sys/socket.h>
#include <sys/un.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <inttypes.h>

/* ========================================================================= *
 *
 * Internal Layering - Top-Down
 *
 * ========================================================================= *
 *
 * SENSORFW_MODULE:
 * - implements internal mce interface defined in mce-sensorfw.h
 *
 * SENSORFW_SERVICE:
 * - state machine that tracks availablity of sensord on system bus
 * - availalibility changes trigger activity in SENSORFW_PLUGIN
 *
 * SENSORFW_PLUGIN:
 * - state machine that takes care of sensord plugin loading ipc
 * - one instance / sensor
 * - finishing plugin loading triggers activity in SENSORFW_SESSION
 *
 * SENSORFW_SESSION:
 * - state machine that takes care of data session start ipc
 * - one instance / sensor
 * - acquiring a session id triggers activity in SENSORFW_CONNECTION
 *
 * SENSORFW_CONNECTION:
 * - state machine that takes care of data connection with sensord
 * - one instance / sensor
 * - establishing a connection triggers activity in SENSORFW_OVERRIDE
 *   and SENSORFW_REPORTING
 *
 * SENSORFW_OVERRIDE:
 * - state machine that takes care of standby override ipc
 * - one instance / sensor
 *
 *
 * SENSORFW_REPORTING:
 * - state machine that takes care of sensor start/stop ipc
 * - one instance / sensor
 * - value changes reported via sensor specific SENSORFW_BACKEND
 *
 * SENSORFW_BACKEND:
 * - contains all sensor specific constants and logic
 *
 * SENSORFW_NOTIFY:
 * - manages cached sensor state
 * - provides default values used when sensord is not available
 * - feeds sensor data to upper level logic when state changes
 *   and/or listening callbacks are registered
 *
 * Error handling:
 * - The layer where error occurred makes transition to ERROR state
 *   and all layers below that are reset to IDLE state and sensor
 *   states are reverted to sensord-is-not-available defaults.
98 99 100
 * - The failed operation is reattempted after brief delay until it
 *   succeeds
 * - Sensord restart might still be needed for full recovery.
101
 *
102 103 104 105 106
 * State transitions:
 * - state transition diagram can be generated from mce-sensorfw.dot
 *   via installing graphviz package and running from command line
 *     dot -Tpng mce-sensorfw.dot -o mce-sensorfw.png
 *
107 108 109 110 111 112 113 114 115 116 117 118 119
 * ========================================================================= *
 *
 * Rough Data/Control Flow Diagram - Error Handling and EVDEV Input Excluded
 *
 * ========================================================================= *
 *
 *             .--------------------.  .--------------------.
 *             | MCE INIT/EXIT      |  | MCE SENSOR LOGIC   |
 *             `--------------------'  `--------------------'
 *                 |                                     |
 *                 | init/quit            enable/disable |
 *                 |                                     |
 *                 v                                     v
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
 * .--------------------------------------------------------.
 * | SENSORFW_MODULE                                        |
 * `--------------------------------------------------------'
 *   |             |                                  |
 *   | start       | probe/reset       enable/disable |
 *   |             |                                  |
 *   |             v                                  v
 *   |  .---------------------------------------------------.
 *   |  | SENSORFW_SERVICE                                  |
 *   |  `---------------------------------------------------'
 *   |    |        |                              |      |
 *   |    | start  | load/reset         set/unset |      |
 *   |    |        |                              |      | start/stop
 *   |    |        v                              |      |
 *   |    |    .------------------------------.   |      |
 *   |    |    | SENSORFW_PLUGIN              |   |      |
 *   |    |    `------------------------------'   |      |
 *   |    |        |                              |      |
 *   |    |        | start/reset                  |      |
 *   |    |        |                              |      |
 *   |    |        v                              |      |
 *   |    |    .------------------------------.   |      |
 *   |    |    | SENSORFW_SESSION             |   |      |
 *   |    |    `------------------------------'   |      |
 *   |    |        |                              |      |
 *   |    |        | connect/reset                |      |
 *   |    |        |                              |      |
 *   |    |        v                              |      |
 *   |    |    .------------------------------.   |      |
 *   |    |    | SENSORFW_CONNECTION          |   |      |
 *   |    |    `------------------------------'   |      |
 *   |    |      |                   |            |      |
 *   |    |      |     rethink/reset |            |      |
 *   |    |      |                   |            |      |
 *   |    |      | rethink/reset     |            |      |
 *   |    |      |                   |            |      |
 *   |    |      |                   v            v      |
 *   |    |      | .--------------------------------.    |
 *   |    |      | | SENSORFW_OVERRIDE              |    |
 *   |    |      | `--------------------------------'    |
 *   |    |      v                                       v
 *   |    |    .--------------------------------------------.
 *   |    |    | SENSORFW_REPORTING                         |
 *   |    |    `--------------------------------------------'
 *   |    |                   |
 *   |    |                   | initial/change/reset
 *   |    |                   |
 *   |    |                   v
 *   |    |    .--------------------------------------------.
 *   |    |    | SENSORFW_BACKEND                           |
 *   |    |    `--------------------------------------------'
 *   v    v                |
 * .--------------------.  | sensor state
 * | SENSORFW_EXCEPTION |  |
 * `--------------------'  |
 *               |         |
 *        active |         |
 *               v         v
178 179 180 181 182 183 184 185 186 187 188 189 190
 *             .--------------------------------------------.
 *             | SENSORFW_NOTIFY                            |
 *             `--------------------------------------------'
 *                            |
 *                            | sensor state
 *                            |
 *                            v
 *             .--------------------------------------------.
 *             | MCE SENSOR LOGIC                           |
 *             `--------------------------------------------'
 *
 * ========================================================================= */

191 192 193 194 195 196 197
/** ID numbers for sensors supported by mce-sensorfwd module
 */
typedef enum
{
    SFW_SENSOR_ID_PS,
    SFW_SENSOR_ID_ALS,
    SFW_SENSOR_ID_ORIENT,
198 199 200 201 202 203 204 205 206 207 208
    SFW_SENSOR_ID_ACCELEROMETER,
    SFW_SENSOR_ID_COMPASS,
    SFW_SENSOR_ID_GYROSCOPE,
    SFW_SENSOR_ID_LID,
    SFW_SENSOR_ID_HUMIDITY,
    SFW_SENSOR_ID_MAGNETOMETER,
    SFW_SENSOR_ID_PRESSURE,
    SFW_SENSOR_ID_ROTATION,
    SFW_SENSOR_ID_STEPCOUNTER,
    SFW_SENSOR_ID_TAP,
    SFW_SENSOR_ID_TEMPERATURE,
209 210 211 212

    SFW_SENSOR_ID_COUNT
} sensor_id_t;

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
static bool
sensor_id_available(sensor_id_t id)
{
    bool available = true;
    switch( id ) {
    case SFW_SENSOR_ID_PS:
    case SFW_SENSOR_ID_ALS:
    case SFW_SENSOR_ID_ORIENT:
        break;
    default:
        available = mce_in_sensortest_mode();
        break;
    }
    return available;
}

229 230 231 232 233 234 235 236 237 238 239 240 241
/* ========================================================================= *
 * D-BUS CONSTANTS FOR D-BUS DAEMON
 * ========================================================================= */

/** org.freedesktop.DBus.NameOwnerChanged D-Bus signal */
#define DBUS_SIGNAL_NAME_OWNER_CHANGED         "NameOwnerChanged"

#define DBUS_METHOD_GET_NAME_OWNER             "GetNameOwner"

/* ========================================================================= *
 * D-BUS CONSTANTS FOR SENSORD SERVICE
 * ========================================================================= */

242
/** How long to wait before retrying failed sensorfw dbus requests */
243
#define SENSORFW_RETRY_DELAY_MS                10000
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
/** D-Bus name of the sensord service */
#define SENSORFW_SERVICE                       "com.nokia.SensorService"

// ----------------------------------------------------------------

/** D-Bus object path for sensord sensor manager */
#define SENSORFW_MANAGER_OBJECT                "/SensorManager"

/** D-Bus interface used by sensor manager */
#define SENSORFW_MANAGER_INTEFCACE             "local.SensorManager"

/** D-Bus method for loading sensor plugin */
#define SENSORFW_MANAGER_METHOD_LOAD_PLUGIN    "loadPlugin"

/** D-Bus method for starting sensor session */
#define SENSORFW_MANAGER_METHOD_START_SESSION  "requestSensor"

// ----------------------------------------------------------------

264 265 266
/* D-Bus interfaces for accessing sensor specific data
 *
 * Note: Sensorfwd uses separate interface for each sensor. */
267

268 269 270
#define SFW_SENSOR_INTERFACE_PS             "local.ProximitySensor"
#define SFW_SENSOR_INTERFACE_ALS            "local.ALSSensor"
#define SFW_SENSOR_INTERFACE_ORIENT         "local.OrientationSensor"
271 272 273 274 275 276 277 278 279 280 281
#define SFW_SENSOR_INTERFACE_ACCELEROMETER  "local.AccelerometerSensor"
#define SFW_SENSOR_INTERFACE_COMPASS        "local.CompassSensor"
#define SFW_SENSOR_INTERFACE_GYROSCOPE      "local.GyroscopeSensor"
#define SFW_SENSOR_INTERFACE_LID            "local.LidSensor"
#define SFW_SENSOR_INTERFACE_HUMIDITY       "local.HumiditySensor"
#define SFW_SENSOR_INTERFACE_MAGNETOMETER   "local.MagnetometerSensor"
#define SFW_SENSOR_INTERFACE_PRESSURE       "local.PressureSensor"
#define SFW_SENSOR_INTERFACE_ROTATION       "local.RotationSensor"
#define SFW_SENSOR_INTERFACE_STEPCOUNTER    "local.StepcounterSensor"
#define SFW_SENSOR_INTERFACE_TAP            "local.TapSensor"
#define SFW_SENSOR_INTERFACE_TEMPERATURE    "local.TemperatureSensor"
282

283 284 285 286 287
/** D-Bus method for enabling sensor
 *
 * @Note All sensor specific interfaces have start method.
 */
#define SFW_SENSOR_METHOD_START             "start"
288

289 290 291 292 293
/** D-Bus method for disabling sensor
 *
 * @Note All sensor specific interfaces have stop method.
 */
#define SFW_SENSOR_METHOD_STOP              "stop"
294

295 296 297 298 299 300 301 302
/** D-Bus method for changing sensor standby override
 *
 * @Note All sensor specific interfaces have have this method,
 *       but unless it is actually explicitly implemented, a
 *       virtual dummy method call handler gets invoked and
 *       it will just return "false".
 */
#define SFW_SENSOR_METHOD_SET_OVERRIDE      "setStandbyOverride"
303

304 305 306 307 308 309
/* D-Bus methods for getting current sensor value
 *
 * The method name and returned data varies from one sensor to
 * another, and in some cases there is no "current state" to
 * query (such as tap detection sensor which emits events).
 */
310

311 312 313
#define SFW_SENSOR_METHOD_READ_PS             "proximity"
#define SFW_SENSOR_METHOD_READ_ALS            "lux"
#define SFW_SENSOR_METHOD_READ_ORIENT         "orientation"
314 315 316 317 318 319 320 321 322 323 324
#define SFW_SENSOR_METHOD_READ_ACCELEROMETER  "xyz"
#define SFW_SENSOR_METHOD_READ_COMPASS        "value" // or "declinationvalue"?
#define SFW_SENSOR_METHOD_READ_GYROSCOPE      "value"
#define SFW_SENSOR_METHOD_READ_LID            "closed"
#define SFW_SENSOR_METHOD_READ_HUMIDITY       "relativeHumidity"
#define SFW_SENSOR_METHOD_READ_MAGNETOMETER   "magneticField"
#define SFW_SENSOR_METHOD_READ_PRESSURE       "pressure"
#define SFW_SENSOR_METHOD_READ_ROTATION       "rotation"
#define SFW_SENSOR_METHOD_READ_STEPCOUNTER    "steps"
#define SFW_SENSOR_METHOD_READ_TAP            NULL // event, not state
#define SFW_SENSOR_METHOD_READ_TEMPERATURE    "temperature"
325 326 327 328

// ----------------------------------------------------------------

/** Connect path to sensord data unix domain socket  */
329
#define SENSORFW_DATA_SOCKET                   "/run/sensord.sock"
330 331 332

// ----------------------------------------------------------------

333
/* Sensor names used by senseofwd */
334

335 336 337
#define SFW_SENSOR_NAME_PS                "proximitysensor"
#define SFW_SENSOR_NAME_ALS               "alssensor"
#define SFW_SENSOR_NAME_ORIENT            "orientationsensor"
338 339 340 341 342 343 344 345 346 347 348
#define SFW_SENSOR_NAME_ACCELEROMETER     "accelerometersensor"
#define SFW_SENSOR_NAME_COMPASS           "compasssensor"
#define SFW_SENSOR_NAME_GYROSCOPE         "gyroscopesensor"
#define SFW_SENSOR_NAME_LID               "lidsensor"
#define SFW_SENSOR_NAME_HUMIDITY          "humiditysensor"
#define SFW_SENSOR_NAME_MAGNETOMETER      "magnetometersensor"
#define SFW_SENSOR_NAME_PRESSURE          "pressuresensor"
#define SFW_SENSOR_NAME_ROTATION          "rotationsensor"
#define SFW_SENSOR_NAME_STEPCOUNTER       "stepcountersensor"
#define SFW_SENSOR_NAME_TAP               "tapsensor"
#define SFW_SENSOR_NAME_TEMPERATURE       "temperaturesensor"
349

350
// ----------------------------------------------------------------
351 352 353 354 355

/* ========================================================================= *
 * FORWARD_DECLARATIONS
 * ========================================================================= */

356
typedef struct sfw_service_t              sfw_service_t;
357

358 359 360 361 362 363
typedef struct sfw_plugin_t               sfw_plugin_t;
typedef struct sfw_session_t              sfw_session_t;
typedef struct sfw_connection_t           sfw_connection_t;
typedef struct sfw_override_t             sfw_override_t;
typedef struct sfw_reporting_t            sfw_reporting_t;
typedef struct sfw_backend_t              sfw_backend_t;
364

365 366 367
typedef struct sfw_sample_als_t           sfw_sample_als_t;
typedef struct sfw_sample_ps_t            sfw_sample_ps_t;
typedef struct sfw_sample_orient_t        sfw_sample_orient_t;
368 369 370 371 372 373 374 375 376 377 378
typedef struct sfw_sample_accelerometer_t sfw_sample_accelerometer_t;
typedef struct sfw_sample_compass_t       sfw_sample_compass_t;
typedef struct sfw_sample_gyroscope_t     sfw_sample_gyroscope_t;
typedef struct sfw_sample_lid_t           sfw_sample_lid_t;
typedef struct sfw_sample_humidity_t      sfw_sample_humidity_t;
typedef struct sfw_sample_magnetometer_t  sfw_sample_magnetometer_t;
typedef struct sfw_sample_pressure_t      sfw_sample_pressure_t;
typedef struct sfw_sample_rotation_t      sfw_sample_rotation_t;
typedef struct sfw_sample_stepcounter_t   sfw_sample_stepcounter_t;
typedef struct sfw_sample_tap_t           sfw_sample_tap_t;
typedef struct sfw_sample_temperature_t   sfw_sample_temperature_t;
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

/* ========================================================================= *
 * SENSORFW_NOTIFY
 * ========================================================================= */

/** Proximity state to use when sensor can't be enabled
 *
 * This must be "uncovered" so that absense of or failures within
 * sensord do not make the device unusable due to proximity values
 * blocking display power up and touch input.
 */
#define SFW_NOTIFY_DEFAULT_PS false

/** Proximity state to use when waiting for sensord startup
 *
 * This must be "covered" so that we do not prematurely unblock
 * touch input / allow display power up during bootup and during
 * sensord restarts.
 */
#define SFW_NOTIFY_EXCEPTION_PS true

/** Ambient light level [lux] to use when sensor can't be enabled */
#define SFW_NOTIFY_DEFAULT_ALS 400

/** Orientation state to use when sensor can't be enabled */
#define SFW_NOTIFY_DEFAULT_ORIENT MCE_ORIENTATION_UNDEFINED

/** Dummy sensor value to use when re-sending cached state data */
#define SFW_NOTIFY_DUMMY 0

typedef enum
{
    /** Cached sensor state should be reset to default value */
    NOTIFY_RESET,

    /** Cached sensor state should be restored from last-known value */
    NOTIFY_RESTORE,

    /** Cached sensor state should be sent again */
    NOTIFY_REPEAT,

    /** Sensor state was received from evdev source */
    NOTIFY_EVDEV,

    /** Sensor state was received from sensord */
    NOTIFY_SENSORD,

    /** Flush cached state */
    NOTIFY_FORGET,

    NOTIFY_NUMTYPES
} sfw_notify_t;

static const char       *sfw_notify_name                (sfw_notify_t type);
static gboolean          sfw_name_owner_changed_cb      (DBusMessage *const msg);
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

/* ========================================================================= *
 * SENSORD_DATA_TYPES
 * ========================================================================= */

/** ALS data block as sensord sends them */
struct sfw_sample_als_t
{
    /** microseconds, monotonic */
    uint64_t als_timestamp;

    /** amount of light [lux] */
    uint32_t als_value;
};

449 450
static const char *sfw_sample_als_repr(const sfw_sample_als_t *self);

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
/** PS data block as sensord sends them */
struct sfw_sample_ps_t
{
    /** microseconds, monotonic */
    uint64_t ps_timestamp;

    /** distance of blocking object [cm] */
    uint32_t ps_value;

    /** sensor covered [bool]
     *
     * This should be the size of a C++ bool on the same platform.
     * Unfortunately there's no way to find out in a C program
     */
    uint8_t  ps_withinProximity;
};

468 469
static const char *sfw_sample_ps_repr(const sfw_sample_ps_t *self);

470 471 472 473 474 475 476 477 478 479
/** Orientation data block as sensord sends them */
struct sfw_sample_orient_t
{
    /* microseconds, monotonic */
    uint64_t orient_timestamp;

    /* orientation [enum orientation_state_t] */
    int32_t  orient_state;
};

480 481
static const char *sfw_sample_orient_repr(const sfw_sample_orient_t *self);

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 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 686
typedef enum sfw_lid_type_t
{
    SFW_LID_TYPE_UNKNOWN = -1, // UnknownLid
    SFW_LID_TYPE_FRONT   =  0, // FrontLid
    SFW_LID_TYPE_BACK    =  1, // BackLid
} sfw_lid_type_t;

static const char *sfw_lid_type_repr(sfw_lid_type_t type);

typedef enum sfw_tap_direction_t
{
    /** Left or right side tapped */
    SFW_TAP_DIRECTION_X          = 0,

    /** Top or down side tapped */
    SFW_TAP_DIRECTION_Y          = 1,

    /** Face or bottom tapped */
    SFW_TAP_DIRECTION_Z          = 2,

    /** Tapped from left to right */
    SFW_TAP_DIRECTION_LEFT_RIGHT = 3,

    /** Tapped from right to left */
    SFW_TAP_DIRECTION_RIGHT_LEFT = 4,

    /** Tapped from top to bottom */
    SFW_TAP_DIRECTION_TOP_BOTTOM = 5,

    /** Tapped from bottom to top */
    SFW_TAP_DIRECTION_BOTTOM_TOP = 6,

    /** Tapped from face to back */
    SFW_TAP_DIRECTION_FACE_BACK  = 7,

    /** Tapped from back to face */
    SFW_TAP_DIRECTION_BACK_FACE  = 8,
} sfw_tap_direction_t;

static const char *sfw_tap_direction_repr(sfw_tap_direction_t direction);

typedef enum sfw_tap_type_t
{
    /** placeholder */
    SFW_TAP_TYPE_NONE       = -1,
    /** Double tap. */
    SFW_TAP_TYPE_DOUBLE_TAP = 0,
    /**< Single tap. */
    SFW_TAP_TYPE_SINGLE_TAP = 1,
} sfw_tap_type_t;

static const char * sfw_tap_type_repr(sfw_tap_type_t type);

typedef struct sfw_xyz_t
{
    int32_t x,y,z;
} sfw_xyz_t;

struct sfw_sample_accelerometer_t
{
    /** microseconds, monotonic */
    uint64_t accelerometer_timestamp;

    /** amount of TBD */
    sfw_xyz_t accelerometer_xyz;
};

static const char *sfw_sample_accelerometer_repr(const sfw_sample_accelerometer_t *self);

struct sfw_sample_compass_t
{
    /** microseconds, monotonic */
    uint64_t compass_timestamp;

    /** Angle to north which may be declination corrected or not. This is the value apps should use */
    int32_t compass_degrees;
    /** Angle to north without declination correction */
    int32_t compass_raw_degrees;
    /** Declination corrected angle to north */
    int32_t compass_corrected_degrees;
    /** Magnetometer calibration level. Higher value means better calibration. */
    int32_t compass_level;
};

static const char *sfw_sample_compass_repr(const sfw_sample_compass_t *self);

struct sfw_sample_gyroscope_t
{
    /** microseconds, monotonic */
    uint64_t gyroscope_timestamp;

    /** amount of TBD */
    sfw_xyz_t gyroscope_xyz;
};

static const char *sfw_sample_gyroscope_repr(const sfw_sample_gyroscope_t *self);

struct sfw_sample_lid_t
{
    /** microseconds, monotonic */
    uint64_t lid_timestamp;

    /** amount of TBD */
    int32_t  lid_type; // sfw_lid_type_t
    uint32_t lid_value;
};

static const char *sfw_sample_lid_repr(const sfw_sample_lid_t *self);

struct sfw_sample_humidity_t
{
    /** microseconds, monotonic */
    uint64_t humidity_timestamp;

    /** amount of TBD */
    uint32_t humidity_value;
};

static const char *sfw_sample_humidity_repr(const sfw_sample_humidity_t *self);

struct sfw_sample_magnetometer_t
{
    /** microseconds, monotonic */
    uint64_t magnetometer_timestamp;

    /** X coordinate value */
    int32_t magnetometer_x;

    /** Y coordinate value */
    int32_t magnetometer_y;

    /** Z coordinate value */
    int32_t magnetometer_z;

    /** raw X coordinate value */
    int32_t magnetometer_rx;

    /** raw Y coordinate value */
    int32_t magnetometer_ry;

    /** raw Z coordinate value */
    int32_t magnetometer_rz;

    /** Magnetometer calibration level. Higher value means better calibration. */
    int32_t magnetometer_level;
};

static const char *sfw_sample_magnetometer_repr(const sfw_sample_magnetometer_t *self);

struct sfw_sample_pressure_t
{
    /** microseconds, monotonic */
    uint64_t pressure_timestamp;

    /** amount of TBD */
    uint32_t pressure_value;
};

static const char *sfw_sample_pressure_repr(const sfw_sample_pressure_t *self);

struct sfw_sample_rotation_t
{
    /** microseconds, monotonic */
    uint64_t rotation_timestamp;

    /** amount of TBD */
    sfw_xyz_t rotation_xyz;
};

static const char *sfw_sample_rotation_repr(const sfw_sample_rotation_t *self);

struct sfw_sample_stepcounter_t
{
    /** microseconds, monotonic */
    uint64_t stepcounter_timestamp;

    /** amount of TBD */
    uint32_t stepcounter_value;
};

static const char *sfw_sample_stepcounter_repr(const sfw_sample_stepcounter_t *self);

struct sfw_sample_tap_t
{
    /** microseconds, monotonic */
    uint64_t tap_timestamp;

    /** amount of TBD */
    uint32_t tap_direction; // sfw_tap_direction_t
    int32_t  tap_type;      // sfw_tap_type_t
};

static const char *sfw_sample_tap_repr(const sfw_sample_tap_t *self);

struct sfw_sample_temperature_t
{
    /** microseconds, monotonic */
    uint64_t temperature_timestamp;

    /** amount of TBD */
    uint32_t temperature_value;
};

static const char *sfw_sample_temperature_repr(const sfw_sample_temperature_t *self);

687 688 689 690
/* ========================================================================= *
 * SENSORFW_BACKEND
 * ========================================================================= */

691 692
/** Callback function type: parsing initial value reply */
typedef bool (*sfw_value_fn)(sfw_plugin_t *plugin, DBusMessageIter *data);
693 694

/** Callback function type: value change reporting */
695
typedef void (*sfw_sample_fn)(sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711

/** Sensor specific data and callbacks */
struct sfw_backend_t
{
    /** Name of the sensor as expected by sensord */
    const char   *be_sensor_name;

    /** D-Bus object path for the sensor, or NULL to construct default one */
    const char   *be_sensor_object;

    /** D-Bus interface for the sensor */
    const char   *be_sensor_interface;

    /** Size of sensor data blobs sensord will be sending */
    size_t        be_sample_size;

712 713 714
    /** Callback for handling initial value reply */
    sfw_value_fn  be_value_cb;

715 716 717 718 719
    /** Callback for handling sensor data blob */
    sfw_sample_fn be_sample_cb;

    /** D-Bus method name for querying the initial sensor value */
    const char   *be_value_method;
720
};
721

722
static bool sfw_backend_parse_data              (DBusMessageIter *data, int arg_type, ...);
723

724 725 726
static bool sfw_backend_als_value_cb            (sfw_plugin_t *plugin, DBusMessageIter *data);
static bool sfw_backend_ps_value_cb             (sfw_plugin_t *plugin, DBusMessageIter *data);
static bool sfw_backend_orient_value_cb         (sfw_plugin_t *plugin, DBusMessageIter *data);
727 728 729 730 731 732 733 734 735 736 737
static bool sfw_backend_accelerometer_value_cb  (sfw_plugin_t *plugin, DBusMessageIter *data);
static bool sfw_backend_compass_value_cb        (sfw_plugin_t *plugin, DBusMessageIter *data);
static bool sfw_backend_gyroscope_value_cb      (sfw_plugin_t *plugin, DBusMessageIter *data);
static bool sfw_backend_lid_value_cb            (sfw_plugin_t *plugin, DBusMessageIter *data);
static bool sfw_backend_humidity_value_cb       (sfw_plugin_t *plugin, DBusMessageIter *data);
static bool sfw_backend_magnetometer_value_cb   (sfw_plugin_t *plugin, DBusMessageIter *data);
static bool sfw_backend_pressure_value_cb       (sfw_plugin_t *plugin, DBusMessageIter *data);
static bool sfw_backend_rotation_value_cb       (sfw_plugin_t *plugin, DBusMessageIter *data);
static bool sfw_backend_stepcounter_value_cb    (sfw_plugin_t *plugin, DBusMessageIter *data);
static bool sfw_backend_tap_value_cb            (sfw_plugin_t *plugin, DBusMessageIter *data);
static bool sfw_backend_temperature_value_cb    (sfw_plugin_t *plugin, DBusMessageIter *data);
738

739 740 741
static void sfw_backend_als_sample_cb           (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
static void sfw_backend_ps_sample_cb            (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
static void sfw_backend_orient_sample_cb        (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
742 743 744 745 746 747 748 749 750 751 752
static void sfw_backend_accelerometer_sample_cb (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
static void sfw_backend_compass_sample_cb       (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
static void sfw_backend_gyroscope_sample_cb     (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
static void sfw_backend_lid_sample_cb           (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
static void sfw_backend_humidity_sample_cb      (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
static void sfw_backend_magnetometer_sample_cb  (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
static void sfw_backend_pressure_sample_cb      (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
static void sfw_backend_rotation_sample_cb      (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
static void sfw_backend_stepcounter_sample_cb   (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
static void sfw_backend_tap_sample_cb           (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
static void sfw_backend_temperature_sample_cb   (sfw_plugin_t *plugin, sfw_notify_t type, const void *sample);
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767

/* ========================================================================= *
 * SENSORFW_HELPERS
 * ========================================================================= */

static bool              sfw_socket_set_blocking        (int fd, bool blocking);
static int               sfw_socket_open                (void);
static guint             sfw_socket_add_notify          (int fd, bool close_on_unref, GIOCondition cnd, GIOFunc io_cb, gpointer aptr);

/* ========================================================================= *
 * SENSORFW_REPORTING
 * ========================================================================= */

typedef enum
{
768 769 770
    /** Initial state used before availability of sensord is known */
    REPORTING_INITIAL,

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
    /** Sensord is not available */
    REPORTING_IDLE,

    /** Choose between start() and stop() */
    REPORTING_RETHINK,

    /** Waiting a reply to start() */
    REPORTING_ENABLING,

    /** Sensor started succesfully */
    REPORTING_ENABLED,

    /** Waiting a reply to stop() */
    REPORTING_DISABLING,

    /** Sensor stopped succesfully */
    REPORTING_DISABLED,

    /** Something went wrong */
    REPORTING_ERROR,

    REPORTING_NUMSTATES
} sfw_reporting_state_t;

/** State machine for handling sensor start/stop */
struct sfw_reporting_t
{
    /** Pointer to containing plugin object */
    sfw_plugin_t          *rep_plugin;

    /** Current reporting state */
    sfw_reporting_state_t  rep_state;

    /** Pending start/stop dbus method call */
    DBusPendingCall       *rep_change_pc;

    /** Pending initial value dbus method call */
    DBusPendingCall       *rep_value_pc;

    /** Flag for: MCE wants sensor to be enabled */
    bool                  rep_target;
812 813 814

    /** Timer for: Retry after ipc error */
    guint                 rep_retry_id;
815 816 817 818 819 820 821 822 823
};

static const char       *sfw_reporting_state_name         (sfw_reporting_state_t state);

static sfw_reporting_t  *sfw_reporting_create             (sfw_plugin_t *plugin);
static void              sfw_reporting_delete             (sfw_reporting_t *self);

static void              sfw_reporting_cancel_change      (sfw_reporting_t *self);
static void              sfw_reporting_cancel_value       (sfw_reporting_t *self);
824
static void              sfw_reporting_cancel_retry       (sfw_reporting_t *self);
825 826 827 828 829

static void              sfw_reporting_trans              (sfw_reporting_t *self, sfw_reporting_state_t state);

static void              sfw_reporting_change_cb          (DBusPendingCall *pc, void *aptr);
static void              sfw_reporting_value_cb           (DBusPendingCall *pc, void *aptr);
830
static gboolean          sfw_reporting_retry_cb           (gpointer aptr);
831 832 833 834 835 836 837 838 839 840 841 842

static void              sfw_reporting_do_rethink         (sfw_reporting_t *self);
static void              sfw_reporting_do_start           (sfw_reporting_t *self);
static void              sfw_reporting_do_reset           (sfw_reporting_t *self);

static void              sfw_reporting_set_target         (sfw_reporting_t *self, bool enable);

/* ========================================================================= *
 * SENSORFW_OVERRIDE
 * ========================================================================= */

typedef enum  {
843 844 845
    /** Initial state used before availability of sensord is known */
    OVERRIDE_INITIAL,

846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
    /** Sensord is not available */
    OVERRIDE_IDLE,

    /** Choose between standby override set/unset */
    OVERRIDE_RETHINK,

    /** Waiting for a reply to set standby override */
    OVERRIDE_ENABLING,

    /** Standby override succesfully set */
    OVERRIDE_ENABLED,

    /** Waiting for a reply to unset standby override */
    OVERRIDE_DISABLING,

    /** Standby override succesfully unset */
    OVERRIDE_DISABLED,

    /** Something went wrong */
    OVERRIDE_ERROR,

867 868 869
    /** Not supported */
    OVERRIDE_NA,

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
    OVERRIDE_NUMSTATES
} sfw_override_state_t;

/** State machine for handling sensor standby override */
struct sfw_override_t
{
    /** Pointer to containing plugin object */
    sfw_plugin_t         *ovr_plugin;

    /** Current override state */
    sfw_override_state_t  ovr_state;

    /** Pending standby override set/unset method call */
    DBusPendingCall      *ovr_start_pc;

    /** Flag for: MCE wants standby override to be set */
    bool                  ovr_target;
887 888 889

    /** Timer for: Retry after ipc error */
    guint                 ovr_retry_id;
890 891 892 893 894 895 896 897
};

static const char       *sfw_override_state_name         (sfw_override_state_t state);

static sfw_override_t    *sfw_override_create            (sfw_plugin_t *plugin);
static void              sfw_override_delete             (sfw_override_t *self);

static void              sfw_override_cancel_start       (sfw_override_t *self);
898
static void              sfw_override_cancel_retry       (sfw_override_t *self);
899 900 901 902

static void              sfw_override_trans              (sfw_override_t *self, sfw_override_state_t state);

static void              sfw_override_start_cb           (DBusPendingCall *pc, void *aptr);
903
static gboolean          sfw_override_retry_cb           (gpointer aptr);
904 905 906 907 908 909 910 911 912 913 914 915 916

static void              sfw_override_do_rethink         (sfw_override_t *self);
static void              sfw_override_do_start           (sfw_override_t *self);
static void              sfw_override_do_reset           (sfw_override_t *self);

static void              sfw_override_set_target         (sfw_override_t *self, bool enable);

/* ========================================================================= *
 * SENSORFW_CONNECTION
 * ========================================================================= */

typedef enum
{
917 918 919
    /** Initial state used before availability of sensord is known */
    CONNECTION_INITIAL,

920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
    /** Sensord is not available */
    CONNECTION_IDLE,

    /** Waiting for data socket to come writable */
    CONNECTION_CONNECTING,

    /** Waiting for handshake reply */
    CONNECTION_REGISTERING,

    /** Waiting for sensor data */
    CONNECTION_CONNECTED,

    /** Something went wrong */
    CONNECTION_ERROR,

    CONNECTION_NUMSTATES
} sfw_connection_state_t;

938 939
static const char       *sfw_connection_state_name      (sfw_connection_state_t state);

940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
/** State machine for handling sensor data connection */
struct sfw_connection_t
{
    /** Pointer to containing plugin object */
    sfw_plugin_t           *con_plugin;

    /** Current connection state */
    sfw_connection_state_t  con_state;

    /** File descriptor for data socket */
    int                     con_fd;

    /** I/O watch id id for: ready to write */
    guint                   con_rx_id;

    /** I/O watch id id for: data available */
    guint                   con_tx_id;
957 958 959

    /** Timer for: Retry after ipc error */
    guint                   con_retry_id;
960 961 962 963 964
};

static sfw_connection_t *sfw_connection_create          (sfw_plugin_t *plugin);
static void              sfw_connection_delete          (sfw_connection_t *self);

spiiroin's avatar
spiiroin committed
965
static bool              sfw_connection_handle_samples  (sfw_connection_t *self, char *data, size_t size);
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980

static int               sfw_connection_get_session_id  (const sfw_connection_t *self);

static bool              sfw_connection_tx_req          (sfw_connection_t *self);
static bool              sfw_connection_rx_ack          (sfw_connection_t *self);
static bool              sfw_connection_rx_dta          (sfw_connection_t *self);

static gboolean          sfw_connection_tx_cb           (GIOChannel *chn, GIOCondition cnd, gpointer aptr);
static gboolean          sfw_connection_rx_cb           (GIOChannel *chn, GIOCondition cnd, gpointer aptr);

static void              sfw_connection_remove_tx_notify(sfw_connection_t *self);
static void              sfw_connection_remove_rx_notify(sfw_connection_t *self);
static void              sfw_connection_close_socket    (sfw_connection_t *self);
static bool              sfw_connection_open_socket     (sfw_connection_t *self);

981 982 983
static gboolean          sfw_connection_retry_cb        (gpointer aptr);
static void              sfw_connection_cancel_retry    (sfw_connection_t *self);

984 985 986 987 988 989 990 991 992 993 994
static void              sfw_connection_trans           (sfw_connection_t *self, sfw_connection_state_t state);

static void              sfw_connection_do_start        (sfw_connection_t *self);
static void              sfw_connection_do_reset        (sfw_connection_t *self);

/* ========================================================================= *
 * SENSORFW_SESSION
 * ========================================================================= */

typedef enum
{
995 996 997
    /** Initial state used before availability of sensord is known */
    SESSION_INITIAL,

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
    /** Sensord is not available */
    SESSION_IDLE,

    /** Waiting for session id reply */
    SESSION_REQUESTING,

    /** Have a session id */
    SESSION_ACTIVE,

    /** Something went wrong */
    SESSION_ERROR,

1010 1011 1012
    /** Sensor is not supported */
    SESSION_INVALID,

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
    SESSION_NUMSTATES
} sfw_session_state_t;

/** State machine for handling sensor data session */
struct sfw_session_t
{
    /** Pointer to containing plugin object */
    sfw_plugin_t        *ses_plugin;

    /** Current session state */
    sfw_session_state_t  ses_state;

    /** Pending session start dbus method call */
    DBusPendingCall     *ses_start_pc;

    /** Session ID received from sensord */
    dbus_int32_t         ses_id;
1030 1031 1032

    /** Timer for: Retry after ipc error */
    guint                ses_retry_id;
1033 1034
};

1035 1036 1037 1038 1039 1040
/** Sensor not supported session id from sensorfwd */
#define SESSION_ID_INVALID (-1)

/** Placeholder value for session id not parsed yet */
#define SESSION_ID_UNKNOWN (-2)

1041 1042 1043 1044 1045 1046 1047 1048
static const char       *sfw_session_state_name         (sfw_session_state_t state);

static sfw_session_t    *sfw_session_create             (sfw_plugin_t *plugin);
static void              sfw_session_delete             (sfw_session_t *self);

static int               sfw_session_get_id             (const sfw_session_t *self);

static void              sfw_session_cancel_start       (sfw_session_t *self);
1049
static void              sfw_session_cancel_retry       (sfw_session_t *self);
1050 1051 1052 1053

static void              sfw_session_trans              (sfw_session_t *self, sfw_session_state_t state);

static void              sfw_session_start_cb           (DBusPendingCall *pc, void *aptr);
1054
static gboolean          sfw_session_retry_cb           (gpointer aptr);
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

static void              sfw_session_do_start           (sfw_session_t *self);
static void              sfw_session_do_reset           (sfw_session_t *self);

/* ========================================================================= *
 * SENSORFW_PLUGIN
 * ========================================================================= */

typedef enum
{
1065 1066 1067
    /** Initial state used before availability of sensord is known */
    PLUGIN_INITIAL,

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
    /** Sensord is not available */
    PLUGIN_IDLE,

    /** Waiting for a reply to plugin load method call */
    PLUGIN_LOADING,

    /** Sensor plugin is loaded  */
    PLUGIN_LOADED,

    /** Something went wrong */
    PLUGIN_ERROR,

1080 1081 1082
    /** Plugin not available */
    PLUGIN_NA,

1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
    PLUGIN_NUMSTATES
} sfw_plugin_state_t;

/** State machine for handling sensor plugin loading */
struct sfw_plugin_t
{
    /** Sensor specific backend data & callbacks */
    const sfw_backend_t  *plg_backend;

    /** Default sensor specific D-BUs object path
     *
     * Constructed if backend does not define one explicitly */
    char                 *plg_sensor_object;

    /** Current plugin state */
    sfw_plugin_state_t    plg_state;

    /** Pending plugin load dbus method call */
    DBusPendingCall      *plg_load_pc;

    /** Session state machine object */
    sfw_session_t        *plg_session;

    /** Connection state machine object */
    sfw_connection_t     *plg_connection;

    /** Standby override state machine object */
    sfw_override_t       *plg_override;

    /** Sensor reporting state machine object */
    sfw_reporting_t      *plg_reporting;
1114 1115 1116

    /** Timer for: Retry after ipc error */
    guint                 plg_retry_id;
1117 1118 1119 1120 1121 1122 1123
};

static const char       *sfw_plugin_state_name          (sfw_plugin_state_t state);

static sfw_plugin_t     *sfw_plugin_create              (const sfw_backend_t *backend);
static void              sfw_plugin_delete              (sfw_plugin_t *self);

1124
static void              sfw_plugin_enable_sensor       (sfw_plugin_t *self, bool enable);
1125 1126 1127 1128 1129 1130 1131
static const char       *sfw_plugin_get_sensor_name     (const sfw_plugin_t *self);
static const char       *sfw_plugin_get_sensor_object   (const sfw_plugin_t *self);
static const char       *sfw_plugin_get_sensor_interface(const sfw_plugin_t *self);
static int               sfw_plugin_get_session_id      (const sfw_plugin_t *self);
static const char       *sfw_plugin_get_value_method    (const sfw_plugin_t *self);
static size_t            sfw_plugin_get_sample_size     (const sfw_plugin_t *self);
static void              sfw_plugin_handle_sample       (sfw_plugin_t *self, const void *sample);
1132
static bool              sfw_plugin_handle_value        (sfw_plugin_t *self, DBusMessageIter *data);
1133
static void              sfw_plugin_reset_value         (sfw_plugin_t *self);
1134
static void              sfw_plugin_repeat_value        (sfw_plugin_t *self);
1135
static void              sfw_plugin_restore_value       (sfw_plugin_t *self);
1136
static void              sfw_plugin_forget_value        (sfw_plugin_t *self);
1137 1138

static void              sfw_plugin_cancel_load         (sfw_plugin_t *self);
1139
static void              sfw_plugin_cancel_retry        (sfw_plugin_t *self);
1140 1141 1142 1143

static void              sfw_plugin_trans               (sfw_plugin_t *self, sfw_plugin_state_t state);

static void              sfw_plugin_load_cb             (DBusPendingCall *pc, void *aptr);
1144 1145
static gboolean          sfw_plugin_retry_cb            (gpointer aptr);

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
static void              sfw_plugin_do_load             (sfw_plugin_t *self);
static void              sfw_plugin_do_reset            (sfw_plugin_t *self);

static void              sfw_plugin_do_session_start    (sfw_plugin_t *self);
static void              sfw_plugin_do_session_reset    (sfw_plugin_t *self);

static void              sfw_plugin_do_connection_start (sfw_plugin_t *self);
static void              sfw_plugin_do_connection_reset (sfw_plugin_t *self);

static void              sfw_plugin_do_override_start   (sfw_plugin_t *self);
static void              sfw_plugin_do_override_reset   (sfw_plugin_t *self);

static void              sfw_plugin_do_reporting_start  (sfw_plugin_t *self);
static void              sfw_plugin_do_reporting_reset  (sfw_plugin_t *self);

/* ========================================================================= *
 * SENSORFW_SERVICE
 * ========================================================================= */

typedef enum
{
    /** Sensord availability is not known */
1168
    SERVICE_UNKNOWN,
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190

    /** Waiting for a reply to name owner query */
    SERVICE_QUERYING,

    /** Sensord service is available */
    SERVICE_RUNNING,

    /** Sensord service is not available */
    SERVICE_STOPPED,

    SERVICE_NUMSTATES
} sfw_service_state_t;

/** State machine for handling sensord service availability tracking */
struct sfw_service_t
{
    /** Sensord service tracking state */
    sfw_service_state_t  srv_state;

    /** Pending name owner dbus method call */
    DBusPendingCall     *srv_query_pc;

1191 1192
    /** State machine objects for sensors */
    sfw_plugin_t        *srv_plugin[SFW_SENSOR_ID_COUNT];
1193 1194 1195 1196 1197 1198 1199 1200
};

static const char       *sfw_service_state_name         (sfw_service_state_t state);

static sfw_service_t    *sfw_service_create             (void);
static void              sfw_service_delete             (sfw_service_t *self);

static void              sfw_service_cancel_query       (sfw_service_t *self);
spiiroin's avatar
spiiroin committed
1201
static void              sfw_service_query_cb           (DBusPendingCall *pc, void *aptr);
1202 1203 1204 1205 1206 1207 1208

static void              sfw_service_trans              (sfw_service_t *self, sfw_service_state_t state);

static void              sfw_service_do_start           (sfw_service_t *self);
static void              sfw_service_do_stop            (sfw_service_t *self);
static void              sfw_service_do_query           (sfw_service_t *self);

1209 1210
static sfw_plugin_t     *sfw_service_plugin             (const sfw_service_t *self, sensor_id_t id);
static void              sfw_service_set_sensor         (const sfw_service_t *self, sensor_id_t id, bool enable);
1211

1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
/* ========================================================================= *
 * SENSORFW_EXCEPTION
 * ========================================================================= */

/** Durations for enforcing use of exceptional sensor values */
typedef enum {
    /** Expected time until sensord availability is known */
     SFW_EXCEPTION_LENGTH_MCE_STARTING_UP = 30 * 1000,

    /** Expected time until data from sensord is available */
     SFW_EXCEPTION_LENGTH_SENSORD_RUNNING =  2 * 1000,

    /** Expected time until sensord gets (re)started */
     SFW_EXCEPTION_LENGTH_SENSORD_STOPPED =  5 * 1000,
} sfw_exception_delay_t;

static gboolean          sfw_exception_timer_cb  (gpointer aptr);
static void              sfw_exception_cancel    (void);
static void              sfw_exception_start     (sfw_exception_delay_t delay_ms);
static bool              sfw_exception_is_active (void);

1233 1234 1235 1236
/* ========================================================================= *
 * SENSORFW_MODULE
 * ========================================================================= */

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
/** Sensord availability tracking state machine object */
static sfw_service_t *sfw_service = 0;

/** Proximity change callback used for notifying upper level logic */
static void (*sfw_notify_ps_cb)(bool covered) = 0;

/** Ambient light change callback used for notifying upper level logic */
static void (*sfw_notify_als_cb)(int lux) = 0;

/** Orientation change callback used for notifying upper level logic */
static void (*sfw_notify_orient_cb)(int state) = 0;

1249 1250
// (exported API defined in "mce-sensorfw.h")

1251 1252 1253 1254 1255 1256 1257 1258
static gboolean mce_sensorfw_evdev_cb    (GIOChannel *chn, GIOCondition cnd, gpointer aptr);

static void     mce_sensorfw_als_detach  (void);
static void     mce_sensorfw_ps_detach   (void);

static bool     als_from_evdev(void);
static bool     ps_from_evdev(void);

1259
/* ========================================================================= *
1260
 * SENSORD_DATA_TYPES
1261 1262
 * ========================================================================= */

1263 1264
static const char *
sfw_sample_als_repr(const sfw_sample_als_t *self)
1265
{
1266 1267 1268 1269 1270 1271 1272
    static char buf[64];
    snprintf(buf, sizeof buf,
             "time=%"PRIu64" lux=%"PRIu32,
             self->als_timestamp,
             self->als_value);
    return buf;
}
1273

1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
static const char *
sfw_sample_ps_repr(const sfw_sample_ps_t *self)
{
    static char buf[64];
    snprintf(buf, sizeof buf,
             "time=%"PRIu64" distance=%"PRIu32" proximity=%s",
             self->ps_timestamp,
             self->ps_value,
             self->ps_withinProximity ? "true" : "false");
    return buf;
1284 1285
}

1286 1287
static const char *
sfw_sample_orient_repr(const sfw_sample_orient_t *self)
1288
{
1289 1290 1291 1292 1293 1294 1295
    static char buf[64];
    snprintf(buf, sizeof buf,
             "time=%"PRIu64" state=%s",
             self->orient_timestamp,
             orientation_state_repr(self->orient_state));
    return buf;
}
1296

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
static const char *
sfw_sample_accelerometer_repr(const sfw_sample_accelerometer_t *self)
{
    static char buf[64];
    snprintf(buf, sizeof buf,
             "time=%"PRIu64" x=%"PRId32" y=%"PRId32" z=%"PRId32,
             self->accelerometer_timestamp,
             self->accelerometer_xyz.x,
             self->accelerometer_xyz.y,
             self->accelerometer_xyz.z);
    return buf;
}

static const char *
sfw_sample_compass_repr(const sfw_sample_compass_t *self)
{
    static char buf[64];
    snprintf(buf, sizeof buf,
             "time=%"PRIu64" deg=%"PRId32" raw=%"PRId32" cor=%"PRId32" lev=%"PRId32,
             self->compass_timestamp,
             self->compass_degrees,
             self->compass_raw_degrees,
             self->compass_corrected_degrees,
             self->compass_level);
    return buf;
}

static const char *
sfw_sample_gyroscope_repr(const sfw_sample_gyroscope_t *self)
{
    static char buf[64];
    snprintf(buf, sizeof buf,
             "time=%"PRIu64" x=%"PRId32" y=%"PRId32" z=%"PRId32,
             self->gyroscope_timestamp,
             self->gyroscope_xyz.x,
             self->gyroscope_xyz.y,
             self->gyroscope_xyz.z);
    return buf;
}

static const char *
sfw_lid_type_repr(sfw_lid_type_t type)
{
    const char *repr = "INVALID";
    switch( type ) {
    case SFW_LID_TYPE_UNKNOWN: repr = "UNKNOWN"; break;
    case SFW_LID_TYPE_FRONT:   repr = "FRONT";   break;
    case SFW_LID_TYPE_BACK:    repr = "BACK";    break;
    default: break;
    }
    return repr;
}

static const char *
sfw_sample_lid_repr(const sfw_sample_lid_t *self)
{
    static char buf[64];
    snprintf(buf, sizeof buf,
             "time=%"PRIu64" type=%s value=%"PRIu32,
             self->lid_timestamp,
             sfw_lid_type_repr(self->lid_type),
             self->lid_value);
    return buf;
}

static const char *
sfw_sample_humidity_repr(const sfw_sample_humidity_t *self)
{
    static char buf[64];
    snprintf(buf, sizeof buf,
             "time=%"PRIu64" humidity=%"PRIu32,
             self->humidity_timestamp,
             self->humidity_value);
    return buf;
}

static const char *
sfw_sample_magnetometer_repr(const sfw_sample_magnetometer_t *self)
{
    static char buf[64];
    snprintf(buf, sizeof buf,
             "time=%"PRIu64
             " x=%"PRId32" y=%"PRId32" z=%"PRId32
             " rx=%"PRId32" ry=%"PRId32" rz=%"PRId32
             " level=%"PRId32,
             self->magnetometer_timestamp,
             self->magnetometer_x,
             self->magnetometer_y,
             self->magnetometer_z,
             self->magnetometer_rx,
             self->magnetometer_ry,
             self->magnetometer_rz,
             self->magnetometer_level);
    return buf;
}

static const char *
sfw_sample_pressure_repr(const sfw_sample_pressure_t *self)
{
    static char buf[64];
    snprintf(buf, sizeof buf,
             "time=%"PRIu64" pressure=%"PRIu32,
             self->pressure_timestamp,
             self->pressure_value);
    return buf;
}

static const char *
sfw_sample_rotation_repr(const sfw_sample_rotation_t *self)
{
    static char buf[64];
    snprintf(buf, sizeof buf,
             "time=%"PRIu64" x=%"PRId32" y=%"PRId32" z=%"PRId32,
             self->rotation_timestamp,
             self->rotation_xyz.x,
             self->rotation_xyz.y,
             self->rotation_xyz.z);
    return buf;
}

static const char *
sfw_sample_stepcounter_repr(const sfw_sample_stepcounter_t *self)
{
    static char buf[64];
    snprintf(buf, sizeof buf,
             "time=%"PRIu64" stepcount=%"PRIu32,
             self->stepcounter_timestamp,
             self->stepcounter_value);
    return buf;
}

static const char *
sfw_tap_direction_repr(sfw_tap_direction_t direction)
{
    const char *repr = "INVALID";
    switch( direction ) {
    case SFW_TAP_DIRECTION_X:           repr = "X";          break;
    case SFW_TAP_DIRECTION_Y:           repr = "Y";          break;
    case SFW_TAP_DIRECTION_Z:           repr = "Z";          break;
    case SFW_TAP_DIRECTION_LEFT_RIGHT:  repr = "LEFT_RIGHT"; break;
    case SFW_TAP_DIRECTION_RIGHT_LEFT:  repr = "RIGHT_LEFT"; break;
    case SFW_TAP_DIRECTION_TOP_BOTTOM:  repr = "TOP_BOTTOM"; break;
    case SFW_TAP_DIRECTION_BOTTOM_TOP:  repr = "BOTTOM_TOP"; break;
    case SFW_TAP_DIRECTION_FACE_BACK:   repr = "FACE_BACK";  break;
    case SFW_TAP_DIRECTION_BACK_FACE:   repr = "BACK_FACE";  break;
    default: break;
    }
    return repr;
}

static const char *
sfw_tap_type_repr(sfw_tap_type_t type)
{
    const char *repr = "INVALID";
    switch( type ) {
    case SFW_TAP_TYPE_NONE:       repr = "NONE";       break;
    case SFW_TAP_TYPE_DOUBLE_TAP: repr = "DOUBLE_TAP"; break;
    case SFW_TAP_TYPE_SINGLE_TAP: repr = "SINGLE_TAP"; break;
    default: break;
    }
    return repr;
}

static const char *
sfw_sample_tap_repr(const sfw_sample_tap_t *self)
{
    static char buf[64];
    snprintf(buf, sizeof buf, "time=%"PRIu64" direction=%s type=%s",
             self->tap_timestamp,
             sfw_tap_direction_repr(self->tap_direction),
             sfw_tap_type_repr(self->tap_type));
    return buf;
}

static const char *
sfw_sample_temperature_repr(const sfw_sample_temperature_t *self)
{
    static char buf[64];
    snprintf(buf, sizeof buf,
             "time=%"PRIu64" temperature=%"PRIu32,
             self->temperature_timestamp,
             self->temperature_value);
    return buf;
}

1482 1483 1484
/* ========================================================================= *
 * SENSORFW_BACKEND
 * ========================================================================= */
1485

1486 1487 1488
/* ------------------------------------------------------------------------- *
 * callbacks for interpreting sensor specific current value replies
 * ------------------------------------------------------------------------- */
1489

1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
static bool
sfw_backend_parse_data(DBusMessageIter *data, int arg_type, ...)
{
    bool ack = false;
    va_list va;
    va_start(va, arg_type);

    while( !mce_dbus_iter_at_end(data) ) {
        if( !mce_dbus_iter_get_basic(data, va_arg(va, void *), arg_type) )
            goto EXIT;
        arg_type = va_arg(va, int);
    }

1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
    ack = true;
EXIT:
    va_end(va);
    return ack;
}

static bool
sfw_backend_als_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack = false;
    dbus_uint64_t tck  = 0;
    dbus_uint32_t val  = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_UINT32, &val,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_als_t sample = {
        .als_timestamp = tck,
        .als_value     = val,
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

static bool
sfw_backend_ps_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack = false;
    dbus_uint64_t tck  = 0;
    dbus_uint32_t val  = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_UINT32, &val,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_ps_t sample = {
        .ps_timestamp       = tck,
        .ps_value           = (val < 1) ?    0 :    10,
        .ps_withinProximity = (val < 1) ? true : false,
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

static bool
sfw_backend_orient_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack = false;
    dbus_uint64_t tck  = 0;
    dbus_uint32_t val  = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_UINT32, &val,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_orient_t sample = {
        .orient_timestamp = tck,
        .orient_state     = val,
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

static bool
sfw_backend_accelerometer_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack  = false;
    dbus_uint64_t tck  = 0;
    dbus_int32_t  xval = 0;
    dbus_int32_t  yval = 0;
    dbus_int32_t  zval = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_INT32,  &xval,
                                DBUS_TYPE_INT32,  &yval,
                                DBUS_TYPE_INT32,  &zval,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_accelerometer_t sample = {
        .accelerometer_timestamp = tck,
        .accelerometer_xyz = {
            .x = xval,
            .y = yval,
            .z = zval,
        },
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

static bool
sfw_backend_compass_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack  = false;
    dbus_uint64_t tck  = 0;
    dbus_int32_t  deg = 0;
    dbus_int32_t  raw = 0;
    dbus_int32_t  cor = 0;
    dbus_int32_t  lev = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_INT32,  &deg,
                                DBUS_TYPE_INT32,  &raw,
                                DBUS_TYPE_INT32,  &cor,
                                DBUS_TYPE_INT32,  &lev,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_compass_t sample = {
        .compass_timestamp         = tck,
        .compass_degrees           = deg,
        .compass_raw_degrees       = raw,
        .compass_corrected_degrees = cor,
        .compass_level             = lev,
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

static bool
sfw_backend_gyroscope_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack  = false;
    dbus_uint64_t tck  = 0;
    dbus_int32_t  xval = 0;
    dbus_int32_t  yval = 0;
    dbus_int32_t  zval = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_INT32,  &xval,
                                DBUS_TYPE_INT32,  &yval,
                                DBUS_TYPE_INT32,  &zval,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_gyroscope_t sample = {
        .gyroscope_timestamp = tck,
        .gyroscope_xyz = {
            .x = xval,
            .y = yval,
            .z = zval,
        },
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

static bool
sfw_backend_lid_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack  = false;
    dbus_uint64_t tck  = 0;
    dbus_int32_t  type = 0;
    dbus_uint32_t val  = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_INT32,  &type,
                                DBUS_TYPE_UINT32, &val,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_lid_t sample = {
        .lid_timestamp = tck,
        .lid_type      = type,
        .lid_value     = val,
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

static bool
sfw_backend_humidity_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack  = false;
    dbus_uint64_t tck  = 0;
    dbus_uint32_t  val = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_UINT32, &val,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_humidity_t sample = {
        .humidity_timestamp = tck,
        .humidity_value     = val,
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

static bool
sfw_backend_magnetometer_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack  = false;
    dbus_uint64_t tck  = 0;
    dbus_int32_t   x   = 0;
    dbus_int32_t   y   = 0;
    dbus_int32_t   z   = 0;
    dbus_int32_t   rx  = 0;
    dbus_int32_t   ry  = 0;
    dbus_int32_t   rz  = 0;
    dbus_int32_t   lev = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_INT32, &x,
                                DBUS_TYPE_INT32, &y,
                                DBUS_TYPE_INT32, &z,
                                DBUS_TYPE_INT32, &rx,
                                DBUS_TYPE_INT32, &ry,
                                DBUS_TYPE_INT32, &rz,
                                DBUS_TYPE_INT32, &lev,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_magnetometer_t sample = {
        .magnetometer_timestamp = tck,
        .magnetometer_x         = x,
        .magnetometer_y         = y,
        .magnetometer_z         = z,
        .magnetometer_rx        = rx,
        .magnetometer_ry        = ry,
        .magnetometer_rz        = rz,
        .magnetometer_level     = lev,
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

static bool
sfw_backend_pressure_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack  = false;
    dbus_uint64_t tck  = 0;
    dbus_uint32_t val  = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_UINT32, &val,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_pressure_t sample = {
        .pressure_timestamp = tck,
        .pressure_value     = val,
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

static bool
sfw_backend_rotation_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack  = false;
    dbus_uint64_t tck  = 0;
    dbus_int32_t  xval = 0;
    dbus_int32_t  yval = 0;
    dbus_int32_t  zval = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_INT32,  &xval,
                                DBUS_TYPE_INT32,  &yval,
                                DBUS_TYPE_INT32,  &zval,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_rotation_t sample = {
        .rotation_timestamp = tck,
        .rotation_xyz = {
            .x = xval,
            .y = yval,
            .z = zval,
        },
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

static bool
sfw_backend_stepcounter_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack = false;
    dbus_uint64_t tck = 0;
    dbus_uint32_t val = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_UINT32, &val,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_stepcounter_t sample = {
        .stepcounter_timestamp = tck,
        .stepcounter_value     = val,
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

static bool
sfw_backend_tap_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack = false;
    dbus_uint64_t tck = 0;
    dbus_uint32_t direction = 0;
    dbus_uint32_t type = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_UINT32, &direction,
                                DBUS_TYPE_INT32,  &type,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_tap_t sample = {
        .tap_timestamp = tck,
        .tap_direction = direction,
        .tap_type      = type,
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

static bool
sfw_backend_temperature_value_cb(sfw_plugin_t *plugin, DBusMessageIter *data)
{
    bool          ack  = false;
    dbus_uint64_t tck  = 0;
    dbus_uint32_t val  = 0;

    if( !sfw_backend_parse_data(data,
                                DBUS_TYPE_UINT64, &tck,
                                DBUS_TYPE_UINT32, &val,
                                DBUS_TYPE_INVALID) )
        goto EXIT;

    const sfw_sample_temperature_t sample = {
        .temperature_timestamp = tck,
        .temperature_value     = val,
    };

    sfw_plugin_handle_sample(plugin, &sample);

    ack = true;
EXIT:
    return ack;
}

/* ------------------------------------------------------------------------- *
 * Callbacks for interpreting sensor specific change notifications
 * ------------------------------------------------------------------------- */

/** Callback for handling ambient light events from sensord */
static void
sfw_backend_als_sample_cb(sfw_plugin_t *plugin, sfw_notify_t type, const void *sampledata)
{
    (void)plugin;
    const sfw_sample_als_t *sample  = sampledata;

    static const sfw_sample_als_t default_value = {
        .als_value = SFW_NOTIFY_DEFAULT_ALS,
    };

    static sfw_sample_als_t cached_value = {
        .als_value = SFW_NOTIFY_DEFAULT_ALS,
    };

    static bool tracking_active = false;

    if( sample ) {
        mce_log(LL_DEBUG, "ALS: UPDATE %s %s",
                sfw_notify_name(type),
                sfw_sample_als_repr(sample));
    }

    switch( type ) {
    default:
    case NOTIFY_REPEAT:
        break;

    case NOTIFY_FORGET:
    case NOTIFY_RESET:
        tracking_active = false;
        break;

    case NOTIFY_RESTORE:
        tracking_active = true;
        break;

    case NOTIFY_EVDEV:
        cached_value = *sample;
        break;

    case NOTIFY_SENSORD:
        if( als_from_evdev() )
            mce_log(LL_DEBUG, "ignoring sensord input: %s",
                    sfw_sample_als_repr(sample));
        else
            cached_value = *sample;
        break;
    }

    /* Default value is used unless we are in fully working state */
    sample = tracking_active ? &cached_value : &default_value ;

    if( sfw_notify_als_cb ) {
        mce_log(LL_DEBUG, "ALS: NOTIFY %s %s",
                sfw_notify_name(type),
                sfw_sample_als_repr(sample));
        sfw_notify_als_cb(sample->als_value);
    }

    return;
}

/** Callback for handling proximity events from sensord */
static void
sfw_backend_ps_sample_cb(sfw_plugin_t *plugin, sfw_notify_t type, const void *sampledata)
{
    (void)plugin;
    const sfw_sample_ps_t *sample = sampledata;

    static const sfw_sample_ps_t exception_value = {
        .ps_withinProximity = SFW_NOTIFY_EXCEPTION_PS,
    };

    static const sfw_sample_ps_t default_value = {
        .ps_withinProximity = SFW_NOTIFY_DEFAULT_PS,
    };

    static sfw_sample_ps_t cached_value = {
        .ps_value = 10,
        .ps_withinProximity = SFW_NOTIFY_DEFAULT_PS,
    };

    static bool tracking_active = false;

    if( sample ) {
        mce_log(LL_DEBUG, "PS: UPDATE %s %s",
                sfw_notify_name(type),
                sfw_sample_ps_repr(sample));
    }

    switch( type ) {
    default:
    case NOTIFY_FORGET:
    case NOTIFY_REPEAT:
        break;

    case NOTIFY_RESET:
        tracking_active = false;
        break;

    case NOTIFY_RESTORE:
        tracking_active = true;
        break;

    case NOTIFY_EVDEV:
        cached_value = *sample;
        break;

    case NOTIFY_SENSORD:
        if( ps_from_evdev() )
            mce_log(LL_DEBUG, "ignoring sensord input: %s",
                    sfw_sample_ps_repr(sample));
        else
            cached_value = *sample;
        break;
    }

    if( sfw_exception_is_active() ) {
        // ps=covered while waiting for sensorfwd to show up
        sample = &exception_value;
    }
    else if( !tracking_active ) {
        // ps=not-covered while sensorfwd is not running
        sample = &default_value;
    }
    else {
        // ps=as-reported by sensorfwd or kernel
        sample = &cached_value;
    }

    if( sfw_notify_ps_cb ) {
        mce_log(LL_DEBUG, "PS: NOTIFY %s %s",
                sfw_notify_name(type),
                sfw_sample_ps_repr(sample));
        sfw_notify_ps_cb(sample->ps_withinProximity);
    }

    return;
}

/** Callback for handling orientation events from sensord */
static void
sfw_backend_orient_sample_cb(sfw_plugin_t *plugin, sfw_notify_t type, const void *sampledata)
{
    static const sfw_sample_orient_t default_value = {
        .orient_state = SFW_NOTIFY_DEFAULT_ORIENT,
    };

    static sfw_sample_orient_t cached_value = {
        .orient_state = SFW_NOTIFY_DEFAULT_ORIENT,
    };

    static bool     tracking_active = false;

    (void)plugin;
    const sfw_sample_orient_t *sample = sampledata;

    if( sample ) {
        mce_log(LL_DEBUG, "ORIENT: UPDATE %s %s",
                sfw_notify_name(type),
                sfw_sample_orient_repr(sample));
    }

    switch( type ) {
    default:
    case NOTIFY_REPEAT:
        break;

    case NOTIFY_FORGET:
    case NOTIFY_RESET:
        tracking_active = false;
        cached_value = default_value;
        break;

    case NOTIFY_RESTORE:
        tracking_active = true;
        break;

    case NOTIFY_EVDEV:
    case NOTIFY_SENSORD:
        cached_value = *sample;
        break;
    }

    /* Default value is used unless we are in fully working state */
    sample = tracking_active ? &cached_value : &default_value ;

    if( sfw_notify_orient_cb ) {
        mce_log(LL_DEBUG, "ORIENT: NOTIFY %s %s",
                sfw_notify_name(type),
                sfw_sample_orient_repr(sample));
        sfw_notify_orient_cb(sample->orient_state);
    }

    return;
}

/** Callback for handling accelerometer events from sensord */
static void
sfw_backend_accelerometer_sample_cb(sfw_plugin_t *plugin, sfw_notify_t type, const void *sampledata)
{
    (void)plugin;
    const sfw_sample_accelerometer_t *sample = sampledata;

    static const sfw_sample_accelerometer_t default_value =
    {
        .accelerometer_xyz = {
            .x = 0,
            .y = 0,
            .z = 0,
        },
    };

    static sfw_sample_accelerometer_t cached_value = { };

    static bool tracking_active = false;

    switch( type ) {
    default:
    case NOTIFY_REPEAT:
        break;

    case NOTIFY_FORGET:
    case NOTIFY_RESET:
        tracking_active = false;
        cached_value = default_value;
        break;

    case NOTIFY_RESTORE:
        tracking_active = true;
        break;

    case NOTIFY_EVDEV:
    case NOTIFY_SENSORD:
        cached_value = *sample;
        break;
    }

    sample = tracking_active ? &cached_value : &default_value;

    mce_log(LL_DEBUG, "ACCELEROMETER: UPDATE %s %s",
            sfw_notify_name(type),
            sfw_sample_accelerometer_repr(sample));

    return;
}

static void
sfw_backend_compass_sample_cb(sfw_plugin_t *plugin, sfw_notify_t type, const void *sampledata)
{
    (void)plugin;
    const sfw_sample_compass_t *sample = sampledata;

    static const sfw_sample_compass_t default_value = { };
    static sfw_sample_compass_t cached_value = { };

    static bool tracking_active = false;

    switch( type ) {
    default:
    case NOTIFY_REPEAT:
        break;

    case NOTIFY_FORGET:
    case NOTIFY_RESET:
        tracking_active = false;
        cached_value = default_value;
        break;

    case NOTIFY_RESTORE:
        tracking_active = true;
        break;

    case NOTIFY_EVDEV:
    case NOTIFY_SENSORD:
        cached_value = *sample;
        break;
    }

    sample = tracking_active ? &cached_value : &default_value;

    mce_log(LL_DEBUG, "COMPASS: UPDATE %s %s",
            sfw_notify_name(type),
            sfw_sample_compass_repr(sample));

    return;
}

static void
sfw_backend_gyroscope_sample_cb(sfw_plugin_t *plugin, sfw_notify_t type, const void *sampledata)
{
    (void)plugin;
    const sfw_sample_gyroscope_t *sample = sampledata;

    static const sfw_sample_gyroscope_t default_value = { };
    static sfw_sample_gyroscope_t cached_value = { };

    static bool tracking_active = false;

    switch( type ) {
    default:
    case NOTIFY_REPEAT:
        break;

    case NOTIFY_FORGET:
    case NOTIFY_RESET:
        tracking_active = false;
        cached_value = default_value;
        break;

    case NOTIFY_RESTORE:
        tracking_active = true;
        break;

    case NOTIFY_EVDEV:
    case NOTIFY_SENSORD:
        cached_value = *sample;
        break;
    }

    sample = tracking_active ? &cached_value : &default_value;

    mce_log(LL_DEBUG, "GYROSCOPE: UPDATE %s %s",
            sfw_notify_name(type),
            sfw_sample_gyroscope_repr(sample));

    return;
}

static void
sfw_backend_lid_sample_cb(sfw_plugin_t *plugin, sfw_notify_t type, const void *sampledata)
{
    (void)plugin;
    const sfw_sample_lid_t *sample = sampledata;

    static const sfw_sample_lid_t default_value = { };
    static sfw_sample_lid_t cached_value = { };

    static bool tracking_active = false;

    switch( type ) {
    default:
    case NOTIFY_REPEAT:
        break;

    case NOTIFY_FORGET:
    case NOTIFY_RESET:
        tracking_active = false;
        cached_value = default_value;
        break;

    case NOTIFY_RESTORE:
        tracking_active = true;
        break;

    case NOTIFY_EVDEV:
    case NOTIFY_SENSORD:
        cached_value = *sample;
        break;
    }

    sample = tracking_active ? &cached_value : &default_value;

    mce_log(LL_DEBUG, "LID: UPDATE %s %s",
            sfw_notify_name(type),
            sfw_sample_lid_repr(sample));

    return;
}

static void
sfw_backend_humidity_sample_cb(sfw_plugin_t *plugin, sfw_notify_t type, const void *sampledata)
{
    (void)plugin;
    const sfw_sample_humidity_t *sample = sampledata;

    static const sfw_sample_humidity_t default_value = { };
    static sfw_sample_humidity_t cached_value = { };

    static bool tracking_active = false;

    switch( type ) {
    default:
    case NOTIFY_REPEAT:
        break;

    case NOTIFY_FORGET:
    case NOTIFY_RESET:
        tracking_active = false;
        cached_value = default_value;
        break;

    case NOTIFY_RESTORE:
        tracking_active = true;
        break;

    case NOTIFY_EVDEV:
    case NOTIFY_SENSORD:
        cached_value = *sample;
        break;
    }

    sample = tracking_active ? &cached_value : &default_value;

    mce_log(LL_DEBUG, "HUMIDITY: UPDATE %s %s",
            sfw_notify_name(type),
            sfw_sample_humidity_repr(sample));

    return;
}

static void
sfw_backend_magnetometer_sample_cb(sfw_plugin_t *plugin, sfw_notify_t type, const void *sampledata)
{
    (void)plugin;
    const sfw_sample_magnetometer_t *sample = sampledata;

    static const sfw_sample_magnetometer_t default_value = { };
    static sfw_sample_magnetometer_t cached_value = { };

    static bool tracking_active = false;

    switch( type ) {
    default:
    case NOTIFY_REPEAT:
        break;

    case NOTIFY_FORGET:
    case NOTIFY_RESET:
        tracking_active = false;
        cached_value = default_value;
        break;

    case NOTIFY_RESTORE:
        tracking_active = true;
        break;

    case NOTIFY_EVDEV:
    case NOTIFY_SENSORD:
        cached_value = *sample;
        break;
    }

    sample = tracking_active ? &cached_value : &default_value;

    mce_log(LL_DEBUG, "MAGNETOMETER: UPDATE %s %s",
            sfw_notify_name(type),
            sfw_sample_magnetometer_repr(sample));

    return;
2372 2373
}

2374 2375
static void
sfw_backend_pressure_sample_cb(sfw_plugin_t *plugin, sfw_notify_t type, const void *sampledata)
2376
{
2377 2378
    (void)plugin;
    const sfw_sample_pressure_t *sample = sampledata;
2379

2380 2381
    static const sfw_sample_pressure_t default_value = { };
    static sfw_sample_pressure_t cached_value = { };
2382

2383
    static bool tracking_active = false;
2384

2385 2386 2387 2388
    switch( type ) {
    default:
    case NOTIFY_REPEAT:
        break;
2389

2390 2391 2392 2393 2394
    case NOTIFY_FORGET:
    case NOTIFY_RESET:
        tracking_active = false;
        cached_value = default_value;
        break;
2395

2396 2397 2398
    case NOTIFY_RESTORE:
        tracking_active = true;
        break;
2399

2400 2401 2402 2403 2404
    case NOTIFY_EVDEV:
    case NOTIFY_SENSORD:
        cached_value = *sample;
        break;
    }
2405

2406
    sample = tracking_active ? &cached_value : &default_value;
2407

2408 2409 2410
    mce_log(LL_DEBUG, "PRESSURE: UPDATE %s %s",
            sfw_notify_name(type),
            sfw_sample_pressure_repr(sample));
2411

2412
    return;
2413 2414
}

2415 2416
static void
sfw_backend_rotation_sample_cb(sfw_plugin_t *plugin, sfw_notify_t type, const void *sampledata)
2417
{
2418 2419
    (void)plugin;
    const sfw_sample_rotation_t *sample = sampledata;
2420

2421 2422
    static const sfw_sample_rotation_t default_value = { };
    static sfw_sample_rotation_t cached_value = { };
2423

2424
    static bool tracking_active = false;
2425

2426 2427 2428 2429
    switch( type ) {
    default:
    case NOTIFY_REPEAT:
        break;
2430

2431 2432 2433 2434 2435
    case NOTIFY_FORGET:
    case NOTIFY_RESET:
        tracking_active = false;
        cached_value = default_value;
        break;
2436

2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
    case NOTIFY_RESTORE:
        tracking_active = true;
        break;

    case NOTIFY_EVDEV:
    case NOTIFY_SENSORD:
        cached_value = *sample;
        break;
    }

    sample = tracking_active ? &cached_value : &default_value;

    mce_log(LL_DEBUG, "ROTATION: UPDATE %s %s",
            sfw_notify_name(type),
            sfw_sample_rotation_repr(sample));

    return;
}
2455

2456
static void
2457
sfw_backend_stepcounter_sample_cb(sfw_plugin_t *plugin, sfw_notify_t type, const void *sampledata)
2458 2459
{
    (void)plugin;
2460
    const sfw_sample_stepcounter_t *sample = sampledata;
2461

2462 2463
    static const sfw_sample_stepcounter_t default_value = { };
    static sfw_sample_stepcounter_t cached_value = { };
2464

2465
    static bool tracking_active = false;
2466

2467 2468 2469 2470
    switch( type ) {
    default:
    case NOTIFY_REPEAT:
        break;
2471

2472 2473 2474
    case NOTIFY_FORGET:
    case NOTIFY_RESET:
        tracking_active = false;
2475
        cached_value = default_value;
2476
        break;
2477

2478 2479 2480
    case NOTIFY_RESTORE:
        tracking_active = true;
        break;
2481

2482 2483
    case NOTIFY_EVDEV:
    case NOTIFY_SENSORD:
2484
        cached_value = *sample;
2485 2486
        break;
    }
2487

2488
    sample = tracking_active ? &cached_value : &default_value;
2489

2490 2491 2492
    mce_log(LL_DEBUG, "STEPCOUNTER: UPDATE %s %s",
            sfw_notify_name(type),
            sfw_sample_stepcounter_repr(sample));
2493 2494

    return;
2495 2496 2497
}

static void
2498
sfw_backend_tap_sample_cb(sfw_plugin_t *plugin, sfw_notify_t type, const void *sampledata)
2499 2500
{
    (void)plugin;
2501
    const sfw_sample_tap_t *sample = sampledata;
2502

2503 2504
    static const sfw_sample_tap_t default_value = { };
    static sfw_sample_tap_t cached_value = { };
2505

2506
    static bool tracking_active = false;
2507

2508 2509 2510 2511
    switch( type ) {
    default:
    case NOTIFY_REPEAT:
        break;
2512

2513
    case NOTIFY_FORGET:
2514 2515
    case NOTIFY_RESET:
        tracking_active = false;