Skip to content

Commit

Permalink
Added simplified example plugins.
Browse files Browse the repository at this point in the history
There is now a sample plugin for sensor, chain, filter and adaptor.
They make use of each other to demonstrate how logical sensors are
created.
  • Loading branch information
Timo Rongas committed Oct 26, 2010
1 parent f12aa97 commit aa39c4a
Show file tree
Hide file tree
Showing 19 changed files with 985 additions and 5 deletions.
14 changes: 12 additions & 2 deletions examples/EXAMPLES
Expand Up @@ -10,8 +10,18 @@ This folder contains examples for extending sensord.

Current examples:

adaptorplugin: Instructions for writing a new adaptor plugin based on
SysfsAdaptor.
adaptorplugin: Adaptor plugin based on SysfsAdaptor.
filterplugin : Simple filter.
chainplugin : Simple chain, one input, one output, one filter
sensorplugin : Sample sensor providing data from chain

The examples make use of each other, so that SampleSensor gets data from
SampleChain, which filters data from SampleAdaptor with SampleFilter.

The code has been commented in order to explain the structure. Not all
comments are replicated to all plugins, so reading through the whole set
is encouraged. Please see the documentation in doc/ for further
description of logic.

Examples on how to use the sensor framework from client applications
might be here someday as well. For now, the fastest way to get started
Expand Down
11 changes: 8 additions & 3 deletions examples/adaptorplugin/sampleadaptor.cpp
Expand Up @@ -25,9 +25,14 @@

#include "sampleadaptor.h"

#include "logging.h" // Logging entries
#include "config.h" // Configuration file access
#include "datatypes/utils.h" // Provides timestamp utility
// Logging entries
#include "logging.h"

// Configuration file access
#include "config.h"

// Provides timestamp utility
#include "datatypes/utils.h"

// Constructor should initialise SysfsAdaptor parent.
// id should be passed directly. See #SysfsAdaptor for details on
Expand Down
121 changes: 121 additions & 0 deletions examples/chainplugin/samplechain.cpp
@@ -0,0 +1,121 @@
/**
@file samplechain.cpp
@brief SampleChain
<p>
Copyright (C) 2009-2010 Nokia Corporation
@author Timo Rongas <ext-timo.2.rongas@nokia.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 "samplechain.h"
#include "sensord/sensormanager.h"
#include "sensord/bin.h"
#include "sensord/bufferreader.h"
#include "sensord/config.h"
#include "sensord/logging.h"

SampleChain::SampleChain(const QString& id) :
AbstractChain(id)
{
SensorManager& sm = SensorManager::instance();

// Get a pointer (refcounted) to the adaptor from sensor manager
sampleAdaptor_ = sm.requestDeviceAdaptor("sampleadaptor");

// Create a reader for the adaptor
adaptorReader_ = new BufferReader<TimedUnsigned>(16);

// Get an instance of filter from sensor manager
sampleFilter_ = sm.instantiateFilter("samplefilter");

// Create and name output buffer. This name is used by other nodes
// to find the buffer.
outputBuffer_ = new RingBuffer<TimedUnsigned>(16);
nameOutputBuffer("sampledata", outputBuffer_);

// Create a new bin and add elements into it with names.
filterBin_ = new Bin;
filterBin_->add(adaptorReader_, "adaptor");
filterBin_->add(sampleFilter_, "filter");
filterBin_->add(outputBuffer_, "output");

// Make connections between the elements in the Bin. By default,
// readers, ringbuffers and simple filters use source and sink
// as buffer names.
filterBin_->join("adaptor", "source", "filter", "sink");
filterBin_->join("filter", "source", "output", "sink");

// Connect the reader to the adaptor, specifically to buffer named
// 'sample'
connectToSource(sampleAdaptor_, "sample", adaptorReader_);

// Setup metadata. A description is for each component, but as this
// chain just modifies each sample coming from the adaptor, we can
// push the responsibility for interval, datarange and such to the
// adaptor.
setDescription("Sample filtering with a chain");
setRangeSource(sampleAdaptor_);
setIntervalSource(sampleAdaptor_);

// Adaptors are normally stopped when device screen blanks. This
// function is used to signify which sources should get the request
// for overriding this behavior.
addStandbyOverrideSource(sampleAdaptor_);
}

SampleChain::~SampleChain()
{
SensorManager& sm = SensorManager::instance();

// Disconnect the reader from the adaptor
disconnectFromSource(sampleAdaptor_, "sample", adaptorReader_);

// Release the adaptor (must not delete, these are shared)
sm.releaseDeviceAdaptor("sampleadaptor");

// ..and delete all locally owned stuff.
delete adaptorReader_;
delete sampleFilter_;
delete outputBuffer_;
delete filterBin_;
}

// In chains one must start the sources and internal bins.
bool SampleChain::start()
{
if (AbstractSensorChannel::start()) {
sensordLogD() << "Starting SampleChain";
filterBin_->start();

// Adaptors are started on buffer basis, thus the buffer name
sampleAdaptor_->startSensor("sample");
}
return true;
}

// Stopping is pretty much start() in reverse.
bool SampleChain::stop()
{
if (AbstractSensorChannel::stop()) {
sensordLogD() << "Stopping SampleChain";
sampleAdaptor_->stopSensor("sample");
filterBin_->stop();
}
return true;
}
83 changes: 83 additions & 0 deletions examples/chainplugin/samplechain.h
@@ -0,0 +1,83 @@
/**
@file samplechain.h
@brief SampleChain
<p>
Copyright (C) 2009-2010 Nokia Corporation
@author Timo Rongas <ext-timo.2.rongas@nokia.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 SAMPLECHAIN_H
#define SAMPLECHAIN_H

// Include basetypes that we need to be aware of
#include "deviceadaptor.h"
#include "abstractsensor.h"
#include "abstractchain.h"

// Include any used datatypes
#include "datatypes/timedunsigned.h"

// Definition tricks, included from .cpp
class Bin;
template <class TYPE> class BufferReader;
class FilterBase;


class SampleChain : public AbstractChain
{
Q_OBJECT;

public:
/**
* Factory method for SampleChain.
* @return Pointer to new SampleChain instance as AbstractChain*
*/
static AbstractChain* factoryMethod(const QString& id)
{
SampleChain* sc = new SampleChain(id);
return sc;
}

public Q_SLOTS:
bool start();
bool stop();

protected:
SampleChain(const QString& id);
~SampleChain();

private:
// Bins contain a set of filters
Bin* filterBin_;

// We have one source
DeviceAdaptor* sampleAdaptor_;

// We need a reader for the source
BufferReader<TimedUnsigned>* adaptorReader_;

// We have one filter
FilterBase* sampleFilter_;

// ...and one output buffer.
RingBuffer<TimedUnsigned>* outputBuffer_;
};

#endif
30 changes: 30 additions & 0 deletions examples/chainplugin/samplechain.pro
@@ -0,0 +1,30 @@
TEMPLATE = lib
CONFIG += plugin

QT += dbus

TARGET = samplechain

include( ../../common-config.pri )

HEADERS += samplechain.h \
samplechainplugin.h

SOURCES += samplechain.cpp \
samplechainplugin.cpp

SENSORFW_INCLUDEPATHS = ../.. \
../../include \
../../sensord \
../../datatypes \
../../filters \
../../adaptors

DEPENDPATH += $$SENSORFW_INCLUDEPATHS
INCLUDEPATH += $$SENSORFW_INCLUDEPATHS

include(../../common-install.pri)
publicheaders.files += $$HEADERS
target.path = $$PLUGINPATH

INSTALLS += target
47 changes: 47 additions & 0 deletions examples/chainplugin/samplechainplugin.cpp
@@ -0,0 +1,47 @@
/**
@file samplechainplugin.cpp
@brief Sample plugin for chains
<p>
Copyright (C) 2009-2010 Nokia Corporation
@author Timo Rongas <ext-timo.2.rongas@nokia.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 "samplechainplugin.h"
#include "samplechain.h"
#include "sensormanager.h"
#include <QtDebug>

void SampleChainPlugin::Register(class Loader&)
{
qDebug() << "registering samplechain";
SensorManager& sm = SensorManager::instance();
sm.registerChain<SampleChain>("samplechain");
}

// Specify other required plugins in this function. Dependencies are
// anything that is required for this plugin to get data and filter it
// (adaptors, filters, other chains).
//
// Here we have one adaptor and one filter that we need.
QStringList SampleChainPlugin::Dependencies() {
return QString("sampleadaptor:samplefilter").split(":", QString::SkipEmptyParts);
}

Q_EXPORT_PLUGIN2(samplechain, SampleChainPlugin)
38 changes: 38 additions & 0 deletions examples/chainplugin/samplechainplugin.h
@@ -0,0 +1,38 @@
/**
@file samplechainplugin.h
@brief Sample plugin for chains
<p>
Copyright (C) 2009-2010 Nokia Corporation
@author Timo Rongas <ext-timo.2.rongas@nokia.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 SAMPLECHAINPLUGIN_H
#define SAMPLECHAINPLUGIN_H

#include "plugin.h"

class SampleChainPlugin : public Plugin
{
private:
void Register(class Loader& l);
QStringList Dependencies();
};

#endif

0 comments on commit aa39c4a

Please sign in to comment.