Skip to content

Commit

Permalink
* Added support for NCDK driver to ALSAdaptor.
Browse files Browse the repository at this point in the history
 * New helper functions to SysfsAdaptor for sysfs access.
 * Fix bug from SysfsAdaptor::writeToFile(): size for given buffer was wrong.
 * New configurable options to SysfsAdaptor::init().
  • Loading branch information
Antti Virtanen committed Feb 9, 2011
1 parent ba5dfad commit 7ef0e67
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 42 deletions.
56 changes: 49 additions & 7 deletions adaptors/alsadaptor/alsadaptor.cpp
Expand Up @@ -38,11 +38,11 @@

/* Device name: /dev/apds990x0 */
struct apds990x_data {
__u32 lux; /* 10x scale */
__u32 lux_raw; /* 10x scale */
__u16 ps;
__u16 ps_raw;
__u16 status;
__u32 lux; /* 10x scale */
__u32 lux_raw; /* 10x scale */
__u16 ps;
__u16 ps_raw;
__u16 status;
} __attribute__((packed));

struct bh1770glc_als {
Expand All @@ -57,13 +57,32 @@ ALSAdaptor::ALSAdaptor(const QString& id):
setAdaptedSensor("als", "Internal ambient light sensor lux values", alsBuffer_);
setDescription("Ambient light");
deviceType_ = (DeviceType)Config::configuration()->value("als/driver_type", "0").toInt();
powerStatePath_ = Config::configuration()->value("als/powerstate_path").toByteArray();
}

ALSAdaptor::~ALSAdaptor()
{
delete alsBuffer_;
}

bool ALSAdaptor::startSensor()
{
if(deviceType_ == NCDK && !powerStatePath_.isEmpty())
{
writeToFile(powerStatePath_, "1");
}
return SysfsAdaptor::startSensor();
}

void ALSAdaptor::stopSensor()
{
if(deviceType_ == NCDK && !powerStatePath_.isEmpty())
{
writeToFile(powerStatePath_, "0");
}
SysfsAdaptor::stopSensor();
}

void ALSAdaptor::processSample(int pathId, int fd)
{
Q_UNUSED(pathId);
Expand All @@ -76,7 +95,7 @@ void ALSAdaptor::processSample(int pathId, int fd)
int bytesRead = read(fd, &als_data, sizeof(als_data));

if (bytesRead <= 0) {
sensordLogW() << "read():" << strerror(errno);
sensordLogW() << "read(): " << strerror(errno);
return;
}
sensordLogT() << "Ambient light value: " << als_data.lux;
Expand All @@ -93,7 +112,7 @@ void ALSAdaptor::processSample(int pathId, int fd)
int bytesRead = read(fd, &als_data, sizeof(als_data));

if (bytesRead <= 0) {
sensordLogW() << "read():" << strerror(errno);
sensordLogW() << "read(): " << strerror(errno);
return;
}
sensordLogT() << "Ambient light value: " << als_data.lux;
Expand All @@ -102,6 +121,29 @@ void ALSAdaptor::processSample(int pathId, int fd)
lux->value_ = als_data.lux;
lux->timestamp_ = Utils::getTimeStamp();
}
else if (deviceType_ == NCDK)
{
char buffer[32];
memset(buffer, 0, sizeof(buffer));
int bytesRead = read(fd, &buffer, sizeof(buffer));
if (bytesRead <= 0) {
sensordLogW() << "read(): " << strerror(errno);
return;
}
QVariant value(buffer);
bool ok;
double fValue(value.toDouble(&ok));
if(!ok) {
sensordLogW() << "read(): failed to parse float from: " << buffer;
return;
}
TimedUnsigned* lux = alsBuffer_->nextSlot();
lux->value_ = fValue * 10;
lux->timestamp_ = Utils::getTimeStamp();
sensordLogT() << "Ambient light value: " << lux->value_;
}
else
return;
alsBuffer_->commit();
alsBuffer_->wakeUpReaders();
}
9 changes: 8 additions & 1 deletion adaptors/alsadaptor/alsadaptor.h
Expand Up @@ -53,7 +53,8 @@ class ALSAdaptor : public SysfsAdaptor
{
DeviceUnknown = 0,
RM680,
RM696
RM696,
NCDK
};

/**
Expand Down Expand Up @@ -83,6 +84,11 @@ class ALSAdaptor : public SysfsAdaptor
* @return Always false.
*/
virtual bool setStandbyOverride(const bool override) { Q_UNUSED(override); return false; }

virtual bool startSensor();

virtual void stopSensor();

private:

/**
Expand All @@ -96,6 +102,7 @@ class ALSAdaptor : public SysfsAdaptor

DeviceAdaptorRingBuffer<TimedUnsigned>* alsBuffer_;
DeviceType deviceType_;
QByteArray powerStatePath_;
};

#endif
10 changes: 7 additions & 3 deletions config/sensord-ncdk.conf
Expand Up @@ -8,10 +8,14 @@ intervals = "0,10=>1000"
transformation_matrix = "-1,0,0,0,-1,0,0,0,-1"

[als]
path = /sys/bus/i2c/drivers/apds990x/0-0039/lux_output
driver_type = 3
mode = 1
seek = 1
path = /sys/devices/pci0000:00/0000:00:00.3/i2c-0/0-0039/lux0_input
powerstate_path = /sys/devices/pci0000:00/0000:00:00.3/i2c-0/0-0039/power_state
dataranges = "0=>65535"
intervals = "50=>2000"
default_interval = 1000
intervals = "0"
default_interval = 0

[keyboardslider]
input_match = gpio-keys
Expand Down
7 changes: 7 additions & 0 deletions core/logging.h
Expand Up @@ -61,6 +61,13 @@ class SensordLogger
return *this;
}

SensordLogger& operator<<(const QByteArray& item)
{
if(initialized && isLoggable(currentLevel))
*oss << item.constData();
return *this;
}

template <typename T>
SensordLogger& operator<<(const T& item)
{
Expand Down
65 changes: 43 additions & 22 deletions core/sysfsadaptor.cpp
Expand Up @@ -40,16 +40,16 @@ SysfsAdaptor::SysfsAdaptor(const QString& id,
bool seek,
const QString& path,
const int pathId) :
DeviceAdaptor(id),
reader_(this),
mode_(mode),
epollDescriptor_(-1),
interval_(0),
initNotDone(true),
inStandbyMode_(false),
running_(false),
shouldBeRunning_(false),
doSeek_(seek)
DeviceAdaptor(id),
reader_(this),
mode_(mode),
epollDescriptor_(-1),
interval_(0),
initNotDone(true),
inStandbyMode_(false),
running_(false),
shouldBeRunning_(false),
doSeek_(seek)
{
if (!path.isEmpty()) {
addPath(path, pathId);
Expand Down Expand Up @@ -328,25 +328,44 @@ bool SysfsAdaptor::startReaderThread()
return true;
}

bool SysfsAdaptor::writeToFile(QString path, QString content)
bool SysfsAdaptor::writeToFile(const QByteArray& path, const QByteArray& content)
{
if (!QFile::exists(path)) {
sensordLogT() << "Writing to '" << path << ": " << content;
if (!QFile::exists(path))
{
sensordLogW() << "Path does not exists: " << path;
return false;
}

int fd = open(path.toLocal8Bit().constData(), O_WRONLY);
if (fd == -1) {
int fd = open(path.constData(), O_WRONLY);
if (fd == -1)
{
sensordLogW() << "Failed to open '" << path << "': " << strerror(errno);
return false;
}

if (write(fd, content.toLocal8Bit().constData(), path.size()*sizeof(char)) == -1) {
close(fd);
return false;
bool ret = false;
if (write(fd, content.constData(), content.size() * sizeof(char)) == -1)
{
sensordLogW() << "Failed to write to '" << path << "': " << strerror(errno);
}
else
{
ret = true;
}

close(fd);
return ret;
}

return true;
QByteArray SysfsAdaptor::readFromFile(const QByteArray& path)
{
QFile file(path);
if (!file.exists(path))
{
sensordLogW() << "Path does not exists: " << path;
return QByteArray();
}
QByteArray data(file.readAll());
sensordLogT() << "Read from '" << path << ": " << data;
return data;
}

void SysfsAdaptor::dataAvailable(int pathId, int fd)
Expand All @@ -361,7 +380,7 @@ bool SysfsAdaptor::checkIntervalUsage() const
const QList<DataRange>& list = getAvailableIntervals();
if (list.size() > 1 || (list.size() == 1 && list.first().min != list.first().max))
{
sensordLogW() << "Attempting to use PollMode interval() function for adaptor in SelectMode. Must reimplement!";
sensordLogW() << "Attempting to use IntervalMode interval() function for adaptor in SelectMode. Must reimplement!";
return false;
}
}
Expand Down Expand Up @@ -463,6 +482,8 @@ void SysfsAdaptor::init()
{
sensordLogW() << "No sysfs path defined for: " << name();
}
mode_ = (PollMode)Config::configuration()->value(name() + "/mode", mode_).toInt();
doSeek_ = Config::configuration()->value(name() + "/seek", doSeek_).toBool();

introduceAvailableDataRanges(name());
introduceAvailableIntervals(name());
Expand Down
21 changes: 14 additions & 7 deletions core/sysfsadaptor.h
Expand Up @@ -143,20 +143,27 @@ class SysfsAdaptor : public DeviceAdaptor, public PropertyTracker
*/
virtual void processSample(int pathId, int fd) = 0;

protected slots:
void dataAvailable(int pathId, int fd);

protected:
/**
* Utility function for writing to files. Can be used to control sensor driver
* parameters (setting to powersave mode etc.)
* Utility function for writing to sysfs entries.
*
* @param path Path of the file to write to
* @param content What to write
* @return True on success, false on failure.
*/
bool writeToFile(QString path, QString content);
static bool writeToFile(const QByteArray& path, const QByteArray& content);

/**
* Utility function for reading from sysfs entries.
*
* @param path Path of the file to read from
* @return Content of the file
*/
static QByteArray readFromFile(const QByteArray& path);

protected slots:
void dataAvailable(int pathId, int fd);

protected:
/**
* Returns the current interval (see setInterval()). Valid for PollMode.
* Reimplementation for adaptors using SelectMode is a must.
Expand Down
6 changes: 4 additions & 2 deletions debian/changelog
Expand Up @@ -3,7 +3,7 @@ sensord (0.7.0) unstable; urgency=low
[Antti Virtanen]
* Removed possibility of having multiple sensors inside single DeviceAdaptor. This feature was never used.
* DeviceAdaptor::init() added. Subclasses can implement this to initialize themselves.
* SysfsAdaptor and InputDevAdaptor init() implemented. They try to find adaptor configuration from section which is named after the adaptor type.
* SysfsAdaptor and InputDevAdaptor init() implemented. They try to find adaptor configuration from section which is named after the adaptor type. Both also add bunch of new configuration options.
* Removed hardcoded metadata definitions from adaptors. This will now come from config file.
* Renamed all configuration file entries.
* Removed logic from Loader to handle deviceId based hierarchy. This is replaced by having different config file for each device type.
Expand All @@ -12,6 +12,9 @@ sensord (0.7.0) unstable; urgency=low
* Changed init script to use correct config for ncdk.
* Fixed makefiles to copy correct config files.
* Modified unit tests to use different configs in different deployments.
* Added support for NCDK driver to ALSAdaptor.
* New helper functions to SysfsAdaptor for sysfs access.
* Fix bug from SysfsAdaptor::writeToFile(): size for given buffer was wrong.

[Timo Rongas]
* Prevent reading values from empty list in SysfsAdaptor
Expand All @@ -23,7 +26,6 @@ sensord (0.6.38) unstable; urgency=low
[Shenghua Liu]
* Orientation rotation improvement. Fixes: NB#223261
* Fixes: NB#223772
*

-- shenghua <ext-shenghua.1.liu@nokia.com> Thu, 03 Feb 2011 11:17:28 +0200

Expand Down

0 comments on commit 7ef0e67

Please sign in to comment.