Skip to content

Commit

Permalink
[PATCH] Implement luna-service integration
Browse files Browse the repository at this point in the history
Used by LuneOS, made optional so shouldn't have any influence on Mer
code.

Signed-off-by: Nikolay Nizov <nizovn@gmail.com>
Signed-off-by: Herman van Hazendonk <github.com@herrie.org>

Fixups based on feedback

Final fix based on comments & review

Another cleanup
  • Loading branch information
Herrie82 committed Feb 22, 2018
1 parent bcdff5b commit c7009ad
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 3 deletions.
14 changes: 14 additions & 0 deletions LuneOS/sysbus/com.nokia.SensorService.json.prv
@@ -0,0 +1,14 @@
{
"role": {
"exeName":"/usr/sbin/sensorfwd",
"type": "privileged",
"allowedNames": ["com.nokia.SensorService"]
},
"permissions": [
{
"service":"com.nokia.SensorService",
"inbound":["*"],
"outbound":["*"]
}
]
}
4 changes: 4 additions & 0 deletions LuneOS/sysbus/com.nokia.SensorService.service.prv
@@ -0,0 +1,4 @@
[D-BUS Service]
Name=com.nokia.SensorService
Exec=/usr/sbin/sensrofwd
Type=static
17 changes: 17 additions & 0 deletions LuneOS/systemd/sensorfwd.service
@@ -0,0 +1,17 @@
[Unit]
Description=Sensor daemon for sensor framework
After=dbus.socket ls-hubd_private.service
Requires=dbus.service ls-hubd_private.service
Conflicts=actdead.target

[Service]
Type=forking
BusName=com.nokia.SensorService
ExecStartPre=/bin/sh /usr/bin/sensord-daemon-conf-setup
ExecStart=/usr/sbin/sensorfwd -c=/etc/sensorfw/primaryuse.conf -d --log-level=warning --no-magnetometer-bg-calibration
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=1

[Install]
WantedBy=multi-user.target
8 changes: 8 additions & 0 deletions core/core.pro
Expand Up @@ -80,6 +80,14 @@ mce {
DEFINES += SENSORFW_MCE_WATCHER
}

lunaservice {
SOURCES += lsclient.cpp
HEADERS += lsclient.h
DEFINES += SENSORFW_LUNA_SERVICE_CLIENT
PKGCONFIG += Qt5Gui json-c
PKGCONFIG += luna-service2 LunaSysMgrCommon LunaSysMgrIpcMessages
}

contains(CONFIG,hybris) {
} else {
publicheaders.path = $${publicheaders.path}/core
Expand Down
6 changes: 5 additions & 1 deletion core/deviceadaptor.cpp
Expand Up @@ -83,7 +83,11 @@ DeviceAdaptor::DeviceAdaptor(const QString& id) :
#ifdef SENSORFW_MCE_WATCHER
screenBlanked_(!SensorManager::instance().MCEWatcher()->displayEnabled())
#else
screenBlanked_(false)
#ifdef SENSORFW_LUNA_SERVICE_CLIENT
screenBlanked_(!SensorManager::instance().LSClient_instance()->displayEnabled())
#else
screenBlanked_(false)
#endif
#endif
{
setValid(true);
Expand Down
191 changes: 191 additions & 0 deletions core/lsclient.cpp
@@ -0,0 +1,191 @@
/**
@file lsclient.cpp
@brief LunaService signal utility
<p>
Copyright (C) 2009-2010 Nokia Corporation
Copyright (C) 2015-2018 Nikolay Nizov
@author Timo Rongas <ext-timo.2.rongas@nokia.com>
@author Lihan Guo <ext-lihan.4.guo@nokia.com>
@author Nikolay Nizov <nizovn@gmail.com>
This file is part of Sensord.
Sensord 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.
Sensord 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 Sensord. If not, see <http://www.gnu.org/licenses/>.
</p>
*/

#include "lsclient.h"
#include "JSONUtils.h"
#include "HostBase.h"
#include <json.h>

#define URI_SIGNAL_ADDMATCH "palm://com.palm.lunabus/signal/addmatch"
#define JSON_CHARGER_SIGNAL_ADDMATCH "{\"category\":\"/com/palm/power\",\"method\":\"chargerStatus\"}"
#define URI_DISPLAY_STATUS "palm://com.palm.display/control/status"
#define JSON_DISPLAY_SUBSCRIBE "{\"subscribe\":true}"

LSClient::LSClient(QObject* parent) : QObject(parent),
displayState(true),
powerSave(true)
{
bool retVal;
LSError lserror;
LSErrorInit(&lserror);
LSHandle *serviceHandle;

retVal = LSRegister("com.nokia.SensorService", &serviceHandle, &lserror);
if (!retVal) {
goto error;
}

retVal = LSCall(serviceHandle, URI_SIGNAL_ADDMATCH, JSON_CHARGER_SIGNAL_ADDMATCH, LSClient::chargerCallback, this, NULL, &lserror);
if (!retVal) {
goto error;
}

retVal = LSCall(serviceHandle, URI_DISPLAY_STATUS, JSON_DISPLAY_SUBSCRIBE, LSClient::displayCallback, this, NULL, &lserror);
if (!retVal) {
goto error;
}

retVal = LSGmainAttach(serviceHandle, HostBase::instance()->mainLoop(), &lserror);
if (!retVal) {
goto error;
}

return;

error:
if (LSErrorIsSet(&lserror)) {
LSErrorPrint(&lserror, stderr);
LSErrorFree(&lserror);
}

qDebug() << "Unable to start service.";
}

bool LSClient::displayEnabled() const
{
return displayState;
}

bool LSClient::PSMEnabled() const
{
return powerSave;
}

bool LSClient::chargerCallback(LSHandle *sh, LSMessage *message, void *ctx)
{
LSError lserror;
LSErrorInit(&lserror);

// {"type": string, "connected": boolean}
VALIDATE_SCHEMA_AND_RETURN(sh,
message,
SCHEMA_2(REQUIRED(type, string), REQUIRED(connected, boolean)));

LSClient* lsclient = (LSClient*)ctx;

json_object* label = 0;
json_object* root = 0;
bool newState = true;
const char* str = LSMessageGetPayload(message);
if (!str)
goto error;

root = json_tokener_parse(str);
if (!root || is_error(root))
goto error;

label = json_object_object_get(root, "connected");
if (!label)
goto error;

newState = !json_object_get_boolean(label);

if (lsclient->powerSave != newState)
{
lsclient->powerSave = newState;
emit lsclient->devicePSMStateChanged(newState);
}

error:

if (root && !is_error(root))
json_object_put(root);

return true;
}

bool LSClient::displayCallback(LSHandle *sh, LSMessage *message, void *ctx)
{
LSError lserror;
LSErrorInit(&lserror);

VALIDATE_SCHEMA_AND_RETURN(sh,
message,
SCHEMA_7(
REQUIRED(returnValue, boolean),
REQUIRED(event, string),
OPTIONAL(state, string),
OPTIONAL(timeout, integer),
OPTIONAL(blockDisplay, string),
OPTIONAL(active, boolean),
OPTIONAL(subscribed, boolean)
));

LSClient* lsclient = (LSClient*)ctx;

json_object* root = 0;
const char* value = 0;
bool newState = true;
bool ret = false;
const char* str = LSMessageGetPayload(message);
if (!str)
goto error;

root = json_tokener_parse(str);
if (!root || is_error(root))
goto error;

ret = json_object_get_boolean(json_object_object_get(root, "returnValue"));
if (!ret)
goto error;

value = json_object_get_string(json_object_object_get(root, "event"));
if (!value || is_error(value))
goto error;

if (0 == strcmp (value, "displayOff"))
newState = false;

value = json_object_get_string(json_object_object_get(root, "state"));
if (value && !is_error(value))
if (0 == strcmp (value, "off"))
newState = false;

if (lsclient->displayState != newState)
{
lsclient->displayState = newState;
emit lsclient->displayStateChanged(newState);
}

error:

if (root && !is_error(root))
json_object_put(root);

return true;
}
103 changes: 103 additions & 0 deletions core/lsclient.h
@@ -0,0 +1,103 @@
/**
@file lsclient.h
@brief LunaService signal utility
<p>
Copyright (C) 2009-2010 Nokia Corporation
Copyright (C) 2015-2018 Nikolay Nizov
@author Timo Rongas <ext-timo.2.rongas@nokia.com>
@author Lihan Guo <ext-lihan.4.guo@nokia.com>
@author Antti Virtanen <antti.i.virtanen@nokia.com>
@author Nikolay Nizov <nizovn@gmail.com>
This file is part of Sensord.
Sensord 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.
Sensord 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 Sensord. If not, see <http://www.gnu.org/licenses/>.
</p>
*/

#ifndef SENSORD_LUNA_SERVICE_CLIENT_H
#define SENSORD_LUNA_SERVICE_CLIENT_H

#include <QObject>
#include <luna-service2/lunaservice.h>

/**
* Class for monitoring various LS signals.
*/
class LSClient : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(LSClient)

public:
/**
* Constructor.
*
* @param parent Parent object.
*/
LSClient(QObject* parent = 0);

/**
* Get display state.
*
* @return display state.
*/
bool displayEnabled() const;

/**
* Get powersave-mode state.
*
* @return powersave-mode state.
*/
bool PSMEnabled() const;

signals:
/**
* Emitted when display state has changed.
*
* @param displayOn \c true if display went to 'on' or 'dimmed',
* \c false if 'off'.
*/
void displayStateChanged(bool displayOn);

/**
* Emitted when powersave-mode has changed.
*
* @param PSM is powersave-mode enabled or not.
*/
void devicePSMStateChanged(bool PSM);

private slots:
/**
* Slot for LS display state change signals.
*
* @param state name of the state.
*/
static bool displayCallback(LSHandle *sh, LSMessage *message, void *ctx);

/**
* Slot for LS powersave-mode state change signals.
*
* @param mode is powersave-mode enabled or not.
*/
static bool chargerCallback(LSHandle *sh, LSMessage *message, void *ctx);

private:
bool displayState; /**< current display state */
bool powerSave; /**< current powersave-mode state */

};

#endif // SENSORD_LUNA_SERVICE_CLIENT_H

0 comments on commit c7009ad

Please sign in to comment.