Skip to content

Commit

Permalink
add lid sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorn Potter committed Nov 11, 2016
1 parent 6e5acd5 commit 853844e
Show file tree
Hide file tree
Showing 31 changed files with 1,516 additions and 10 deletions.
4 changes: 3 additions & 1 deletion adaptors/adaptors.pro
Expand Up @@ -30,14 +30,16 @@ SUBDIRS = alsadaptor \
mrstaccelerometer \
gyroscopeadaptor \
gyroscopeadaptor-evdev

SUDBIRS += oemtabletmagnetometeradaptor
SUBDIRS += pegatronaccelerometeradaptor
SUBDIRS += pegatronaccelerometeradaptor
SUBDIRS += oemtabletalsadaptor-ascii
SUBDIRS += oaktrailaccelerometer
SUBDIRS += oemtabletaccelerometer
SUDBIRS += oemtabletgyroscopeadaptor
SUBDIRS += steaccelerometeradaptor
SUBDIRS += mpu6050accelerometer
SUBDIRS += lidsensoradaptor-evdev

config_hybris {
SUBDIRS += hybrisaccelerometer
Expand Down
172 changes: 172 additions & 0 deletions adaptors/lidsensoradaptor-evdev/lidsensoradaptor-evdev.cpp
@@ -0,0 +1,172 @@
/**
@file lidsensoradaptor-evdev.cpp
@brief LidSensorAdaptor utilizing evdev kernel interface
<p>
Copyright (C) 2016 Canonical, Ltd.
@author Lorn Potter <lorn.potter@canonical.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 <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <string.h>
#include <logging.h>
#include "datatypes/utils.h"
#include "config.h"

#include "lidsensoradaptor-evdev.h"

#define SELFDEF_SW_LID 0
#define SELFDEF_EV_SW 5
#define SELFDEF_EV_DISTANCE 25

LidSensorAdaptorEvdev::LidSensorAdaptorEvdev(const QString& id) :
InputDevAdaptor(id, 2),
currentType_(LidData::UnknownLid),
lastType(LidData::UnknownLid),
currentValue_(-1),
lastValue(-1),
usingFront(false)
{
lidBuffer_ = new DeviceAdaptorRingBuffer<LidData>(1);
setAdaptedSensor("lidsensor", "Lid state", lidBuffer_);
powerStatePath_ = Config::configuration()->value("lidsensor/powerstate_path").toByteArray();

}

LidSensorAdaptorEvdev::~LidSensorAdaptorEvdev()
{
delete lidBuffer_;
}

/* Dell event0 Lid Switch
* type EV_SW code EV_LID value 1 closed
* */

/* Dell event8 Virtual Button Driver (happens with front lid too)
* back closed:
* type (4) EV_MSC code (4) MSC_SCAN value cb
* EV_MSC MSC_SCAN value cc
*
* back open:
* EV_MSC MSC_SCAN value ca
* EV_MSC MSC_SCAN value cd
*
*
* */

// there can be two lid sensors, so try and use both
void LidSensorAdaptorEvdev::init()
{
qDebug() << Q_FUNC_INFO << name();
QStringList strList = Config::configuration()->value<QStringList>(name() + "/input_match");
qDebug() << strList;
for (const QString str : strList) {
if (!getInputDevices(str)) {
sensordLogW() << "Input device not found.";
SysfsAdaptor::init();
}
}
}

void LidSensorAdaptorEvdev::interpretEvent(int src, struct input_event *ev)
{
Q_UNUSED(src);

if (ev->type == SELFDEF_EV_SW && ev->code == SELFDEF_SW_LID) {
currentValue_ = ev->value;
currentType_ = LidData::FrontLid;
if (currentValue_ == 1)
usingFront = true;
else
usingFront = false;
}
if (ev->type == EV_MSC && ev->code == MSC_SCAN && !usingFront) {

switch (ev->value) {
case 0xCC: // closed
currentValue_ = 1;
break;
case 0xCD: // open
currentValue_ = 0;
break;
};
currentType_ = LidData::BackLid;
}
}

void LidSensorAdaptorEvdev::interpretSync(int /*src*/, struct input_event *ev)
{
commitOutput(ev);
}

void LidSensorAdaptorEvdev::commitOutput(struct input_event *ev)
{
if (lastValue != currentValue_
&& ((currentType_ == LidData::FrontLid)
|| (!usingFront && currentType_ == LidData::BackLid))) {

LidData *lidData = lidBuffer_->nextSlot();

lidData->timestamp_ = Utils::getTimeStamp(&(ev->time));
lidData->value_ = currentValue_;
lidData->type_ = currentType_;
sensordLogD() << "Lid state change detected: "
<< (currentType_ == 0 ? "front" : "back")
<< (currentValue_ == 0 ? "OPEN": "CLOSED");

lidBuffer_->commit();
lidBuffer_->wakeUpReaders();
lastValue = currentValue_;
lastType = currentType_;
}
}

bool LidSensorAdaptorEvdev::startSensor()
{
if (!powerStatePath_.isEmpty()) {
// writeToFile(powerStatePath_, "1");
}
if (SysfsAdaptor::startSensor()) {
return true;
}
return false;
}

void LidSensorAdaptorEvdev::stopSensor()
{
if (!powerStatePath_.isEmpty()) {
// writeToFile(powerStatePath_, "0");
}

SysfsAdaptor::stopSensor();
}

bool LidSensorAdaptorEvdev::standby()
{
stopSensor();
return true;
}

bool LidSensorAdaptorEvdev::resume()
{
startSensor();
return true;
}
90 changes: 90 additions & 0 deletions adaptors/lidsensoradaptor-evdev/lidsensoradaptor-evdev.h
@@ -0,0 +1,90 @@
/**
@file lidsensoradaptor-evdev.h
@brief LidSensorAdaptor utilizing evdev kernel interface
<p>
Copyright (C) 2016 Canonical, Ltd.
@author Lorn Potter <lorn.potter@canonical.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 LIDADAPTOR_EVDEV_H
#define LIDADAPTOR_EVDEV_H

#include "inputdevadaptor.h"
#include "deviceadaptorringbuffer.h"
#include "datatypes/liddata.h"

/**
* @brief Adaptor for lid sensor.
*
* Events are received through @e evdev kernel interface.
*
* Listening clients are notified whenever the state has changed.
*/
class LidSensorAdaptorEvdev : public InputDevAdaptor
{
Q_OBJECT
public:
// /* Enumerates the possible states for proximity */
// enum ProximityState {
// ProximityStateUnknown = -1, /**< Proximity state unknown */
// ProximityStateOpen = 0, /**< Proximity open (no target detected)*/
// ProximityStateClosed = 1 /**< Proximity closed (target nearby)*/
// };

/**
* Factory method for gaining a new instance of LidsensorAdaptorEvdev class.
* @param id Identifier for the adaptor.
*/
static DeviceAdaptor* factoryMethod(const QString& id)
{
return new LidSensorAdaptorEvdev(id);
}
virtual bool startSensor();

virtual void stopSensor();

virtual bool standby();

virtual bool resume();
virtual void init();
protected:
/**
* Constructor.
* @param id Identifier for the adaptor.
*/
LidSensorAdaptorEvdev(const QString& id);
~LidSensorAdaptorEvdev();

private:

DeviceAdaptorRingBuffer<LidData> *lidBuffer_;
LidData::Type currentType_;
LidData::Type lastType;
qreal currentValue_;
qreal lastValue;
bool usingFront;

void interpretEvent(int src, struct input_event *ev);
void commitOutput(struct input_event *ev);
void interpretSync(int src, struct input_event *ev);
QByteArray powerStatePath_;
};

#endif
9 changes: 9 additions & 0 deletions adaptors/lidsensoradaptor-evdev/lidsensoradaptor-evdev.pro
@@ -0,0 +1,9 @@
TARGET = lidsensoradaptor-evdev

HEADERS += lidsensoradaptor-evdev.h \
lidsensoradaptor-evdevplugin.h

SOURCES += lidsensoradaptor-evdev.cpp \
lidsensoradaptor-evdevplugin.cpp

include( ../adaptor-config.pri )

0 comments on commit 853844e

Please sign in to comment.