Reading from character device with Qt - qt

I'm not very good at character devices, so I need your help. A have a char device(let's call it /dev/my_light) which is a light sensor. I have to read the data from this file and transform it to the brightness value and then pass it to the brightness manager that changes the brightness of my screen. The problem is that when I read the value for some period of time I get old values from the file.I assume there is a buffer(again not sure how character devices exactly work). Whereas when I use cat /dev/my_light I see new data! Is it possible to get rid off the buffer and read new values that were written to the file just right now. Here is my code in Qt:
void MySensor::updateMySensor()
{
Packet packet;
packet.startByte = 0;
packet.mantissa = 0;
packet.exp = 0;
d->device = ::open(d->path.toStdString().c_str(), O_RDONLY);
if (d->device == -1)
{
qDebug() << Q_FUNC_INFO << "can't open the sensor";
return;
}
ssize_t size = ::read(d->device, &packet, sizeof(packet));
close(d->device);
if (size == -1)
{
qDebug() << errno;
return;
}
packet.exp &= 0x0F;
float illumination = pow(2, packet.exp) * packet.mantissa * 0.045;
if(d->singleShot) emit lightSensorIsRunning(true);
emit illuminationRead(illumination);
}
The mySensor function is called every second. I tried to call it each 200 msec but it didn't help. The value of illumination stays old for about 7 seconds(!) whereas the value that I get from cat is new just immediately.
Thank you in advance!

I can't test with your specific device, however, I'm using the keyboard as a read only device.
The program attempts to connect to keyboard and read all keys pressed inside and outside the window. It's a broad solution you'll have to adapt to meet your demands.
Note that I'm opening the file with O_RDONLY | O_NONBLOCK which means open in read only mode and no wait for the event be triggered(some notifier needed to know when data is ready!) respectively.
You'll need super user privilege to run this example!
#include <QtCore>
#include <fcntl.h>
#include <linux/input.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
const char *device_name = "/dev/input/by-path/platform-i8042-serio-0-event-kbd";
int descriptor = open(device_name, O_RDONLY | O_NONBLOCK);
if (descriptor < 0)
{
qDebug() << "Error" << strerror(errno);
return a.exec();
}
QFile device;
if (!device.open(descriptor, QFile::ReadOnly))
{
qDebug() << "Error" << qPrintable(device.errorString());
return a.exec();
}
QSocketNotifier notifier(device.handle(), QSocketNotifier::Read);
QObject::connect(&notifier, &QSocketNotifier::activated, &notifier, [&](int socket){
Q_UNUSED(socket)
struct input_event ev;
QByteArray data = device.readAll();
qDebug() << "Event caught:"
<< "\n\nDATA SIZE" << data.size()
<< "\nSTRUCT COUNT" << data.size() / int(sizeof(input_event))
<< "\nSTRUCT SIZE" << sizeof(input_event);
qDebug() << ""; //New line
while (data.size() >= int(sizeof(input_event)))
{
memcpy(&ev, data.data(), sizeof(input_event));
data.remove(0, int(sizeof(input_event)));
qDebug() << "TYPE" << ev.type << "CODE" << ev.code << "VALUE" << ev.value << "TIME" << ev.time.tv_sec;
}
qDebug() << ""; //New line
});
return a.exec();
}

Related

Qt5: compile error while QSharedPointer<const T>::create()

Is it "expected" for QSharedPointer::create() not to work or is it a bug? I get an error:
/usr/include/qt5/QtCore/qsharedpointer_impl.h:439:9: error:
invalid conversion from ‘const void*’ to ‘void*’ [-fpermissive]
new (result.data()) T(std::forward<Args>(arguments)...);
casting from nonconst shared pointer and constructor from raw const pointer work.
I got this for Qt5.7.0 and Qt5.10.0.
Here is a minimal example:
#include <QSharedPointer>
struct A {};
int main(int argc, char *argv[])
{
auto ca = QSharedPointer<const A>::create();
return 0;
}
Here is one file (not minimal) example but with few working cases, 2 not working and a debug. Commented defines are for "not compiling" parts.
#include <QSharedPointer>
#include <QDebug>
#define FROM_PTR
//#define CONST_CREATE
#define FROM_RAW_PTR
#define PERFECT_FORWARD_CREATE
//#define PERFECT_FORWARD_CREATE_CONST
#define BUILTIN_CAST
class A
{
public:
A() = default;
A(int i) : _i{i} {}
void foo() const { qDebug() << "const foo" << _i; }
void foo() { qDebug() << "foo" << ++_i; }
private:
int _i{0};
};
using ASPtr = QSharedPointer<A>;
using ASCPtr = QSharedPointer<const A>;
int main(int argc, char *argv[])
{
Q_UNUSED(argc)
Q_UNUSED(argv)
#ifdef FROM_PTR
qDebug() << "FROM_PTR";
auto a1 = ASPtr::create();
a1->foo();
auto ca1 = static_cast<ASCPtr>(a1);
ca1->foo();
qDebug() << "\n";
#endif // FROM_PTR
#ifdef CONST_CREATE
qDebug() << "CONST_CREATE";
auto ca2 = ASCPtr::create();
ca2->foo();
qDebug() << "\n";
#endif // CONST_CREATE
#ifdef FROM_RAW_PTR
qDebug() << "FROM_RAW_PTR";
auto ca3 = ASCPtr(new const A);
ca3->foo();
qDebug() << "\n";
#endif // FROM_RAW_PTR
#ifdef PERFECT_FORWARD_CREATE
qDebug() << "PERFECT_FORWARD_CREATE";
auto a2 = ASPtr::create(10);
a2->foo();
qDebug() << "\n";
#endif // PERFECT_FORWARD_CREATE
#ifdef PERFECT_FORWARD_CREATE_CONST
qDebug() << "PERFECT_FORWARD_CREATE_CONST";
auto ca4 = ASCPtr::create(20);
ca4->foo();
qDebug() << "\n";
#endif // PERFECT_FORWARD_CREATE
#ifdef BUILTIN_CAST
qDebug() << "BUILTIN_CAST";
QSharedPointer<A> a3 = ASPtr::create();
a3->foo();
auto ca4 = a3.constCast<const A>();
ca4->foo();
qDebug() << "\n";
#endif // BUILTIN_CAST
return 0;
}
That is a known Qt bug (QTBUG-49748). Although it is marked as resolved in Qt 5.6.0, the bug is still present as pointed out in the comments.
Why is this happening?
Look at the implmentation of the class QSharedPointer qsharedpointer_impl.h.
In particular the line:
new (result.data()) T(std::forward<Args>(arguments)...);
uses the result.data() as the new expression placement params. Unfortunately, one can not use a const pointer as a placement param (have a look at this question here on SO for more details).
Hence, there's not much you can do except reporting this to Qt developers via the official bug tracker.
You may have a look at the smart pointers provided by the standard library (e.g. std::shared_ptr) if you are not forced to use Qt ones.
UPDATE
As reported in Qt bug tracker, this bug was fixed in version 5.11 (here is the related commit). Basically, they used std::remove_cv to remove the topmost const from the type specified.

Blackberry 10 scan gallery

I am working on content migration application. I have to migrate contacts, calendars, media from Blackberry device to Android device. Contacts and Calendars I have done.
I used below snip of code for Contacts
ContactService contactService;
ContactListFilters filters;
filters.setLimit(0);
QList<Contact> contactList = contactService.contacts(filters);
And below for Calendars
CalendarService calendarService;
EventSearchParameters searchParams;
searchParams.setStart(QDateTime(QDate(1918, 01, 01), QTime(00,00,00)));
searchParams.setEnd(QDateTime(QDate(2118, 12, 31), QTime(00,00,00)));
QList<CalendarEvent> eventList = calendarService.events(searchParams);
Its working fine.
Now, I have to lookup media in device i.e get media path based on type
say all Image, all Audio and all Video present in device.
Then with those path have to create a output stream and send it to
destination.
I've heard you can query the media SQL database available on every device, but I've never done it myself so I can't help on that one. The db file is located at /db/mmlibrary.db for media files stored on device and at /db/mmlibrary_SD.db for media files stored on SD card.
Otherwise, you can recursively navigate through the device and keep a global list of file paths. Note that doing so can take a long time, for my personal device it took 25 seconds to recursively go through all folders and find 186 audio files, 5127 picture files and 28 video files. You might want to execute this code in a separate thread to avoid blocking UI.
#include "applicationui.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <QFileInfo>
#include <QDir>
using namespace bb::cascades;
const QStringList audioFileExtensions = QStringList() << "mp3" << "wav";
const QStringList pictureFileExtensions = QStringList() << "bmp" << "gif" << "ico" << "jpg" << "jpeg" << "png" << "tiff";
const QStringList videoFileExtensions = QStringList() << "avi" << "mkv" << "mp4" << "mpeg";
ApplicationUI::ApplicationUI() :
QObject()
{
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
qml->setContextProperty("_app", this);
AbstractPane *root = qml->createRootObject<AbstractPane>();
Application::instance()->setScene(root);
}
//Declared as public Q_INVOKABLE in hpp
void ApplicationUI::findMediaFiles(QString parentFolder) {
QDateTime start = QDateTime::currentDateTime();
qDebug() << "findMediaFiles() started in" << parentFolder;
//Those 3 QStringList are declared as private variables in hpp
audioFilePaths.clear();
pictureFilePaths.clear();
videoFilePaths.clear();
if (parentFolder.isEmpty()) {
parentFolder = QString(getenv("PERIMETER_HOME")) + "/shared";
}
findMediaFilesRecursively(parentFolder);
qDebug() << audioFilePaths.size() << audioFilePaths;
qDebug() << pictureFilePaths.size() << pictureFilePaths;
qDebug() << videoFilePaths.size() << videoFilePaths;
qDebug() << "Took" << start.secsTo(QDateTime::currentDateTime()) << "seconds";
}
//Declared as private in hpp
void ApplicationUI::findMediaFilesRecursively(QString parentFolder) {
QDir dir(parentFolder);
dir.setFilter(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::NoSymLinks);
dir.setSorting(QDir::DirsFirst);
QFileInfoList fileInfoList = dir.entryInfoList();
foreach(QFileInfo fileInfo, fileInfoList) {
if (fileInfo.isDir()) {
findMediaFilesRecursively(fileInfo.absoluteFilePath());
continue;
}
QString extension = fileInfo.fileName().split(".").last();
if (audioFileExtensions.contains(extension, Qt::CaseInsensitive)) {
audioFilePaths.append(fileInfo.absoluteFilePath());
}
else if (pictureFileExtensions.contains(extension, Qt::CaseInsensitive)) {
pictureFilePaths.append(fileInfo.absoluteFilePath());
}
else if (videoFileExtensions.contains(extension, Qt::CaseInsensitive)) {
videoFilePaths.append(fileInfo.absoluteFilePath());
}
}
}

Why does QSettings not store anything?

I want to use QSettings to save my window's dimensions so I came up with these two functions to save & load the settings:
void MainWindow::loadSettings()
{
settings = new QSettings("Nothing","KTerminal");
int MainWidth = settings->value("MainWidth").toInt();
int MainHeight = settings->value("MainHeight").toInt();
std::cout << "loadSettings " << MainWidth << "x" << MainHeight << std::endl;
std::cout << "file: " << settings->fileName().toLatin1().data() << std::endl;
if (MainWidth && MainHeight)
this->resize(MainWidth,MainHeight);
else
this->resize(1300, 840);
}
void MainWindow::saveSettings()
{
int MainHeight = this->size().height();
int MainWidth = this->size().width();
std::cout << "file: " << settings->fileName().toLatin1().data() << std::endl;
std::cout << "saveSettings " << MainWidth << "x" << MainHeight << std::endl;
settings->setValue("MainHeight",MainHeight);
settings->setValue("MainWidth",MainWidth);
}
Now, I can see the demensions being extracted in saveSettings as expected but no file gets created and hence loadSettings will always load 0 only. Why is this?
QSettings isn't normally instantiated on the heap. To achieve the desired effect that you are looking for, follow the Application Example and how it is shown in the QSettings documentation.
void MainWindow::readSettings()
{
QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
const QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray();
if (geometry.isEmpty()) {
const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
resize(availableGeometry.width() / 3, availableGeometry.height() / 2);
move((availableGeometry.width() - width()) / 2,
(availableGeometry.height() - height()) / 2);
} else {
restoreGeometry(geometry);
}
}
void MainWindow::writeSettings()
{
QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
settings.setValue("geometry", saveGeometry());
}
Also note the use of saveGeometry() and restoreGeometry(). Other similarly useful functions for QWidget based GUIs are saveState() and restoreState() (not shown in the above example).
I strongly recommend the zero parameter constructor of QSettings, and to setup the defaults in your main.cpp, like so:
QSettings::setDefaultFormat(QSettings::IniFormat); // personal preference
qApp->setOrganizationName("Moose Soft");
qApp->setApplicationName("Facturo-Pro");
Then when you want to use QSettings in any part of your application, you simply do:
QSettings settings;
settings.setValue("Category/name", value);
// or
QString name_str = settings.value("Category/name", default_value).toString();
QSettings in general is highly optimized, and works really well.
Hope that helps.
Some other places where I've talked up usage of QSettings:
Using QSettings in a global static class
https://stackoverflow.com/a/14365937/999943

QMediaPlayer : media stays in UnknownMediaStatus

I created a QMediaPlayer, passed video address to it and it won't play. I checked the mediaStatus and player state, they all stays 0 all the time. The basic idea is:
QMediaPlayer player = new QMediaPlayer();
cout << player.mediaStatus(); // should print 1: NoMedia but is 0: UnknownMediaStatus
player.setVideoOutput(some_constructed_video_widget);
cout << player.mediaStatus(); // should print 1: NoMedia but is 0: UnknownMediaStatus
player.setMedia(QUrl::fromLocalFile("path/to/test/video/test.mp4"));
cout << player.mediaStatus(); // should print 2: LoadingMedia but is 0: UnknownMediaStatus
player.play();
cout << player.mediaStatus(); // should print 3: LoadedMedia but is 0: UnknownMediaStatus
// and of course, no video gets played
The mediaStatus is simply a enum: MediaStatus { UnknownMediaStatus, NoMedia, LoadingMedia, LoadedMedia, ..., InvalidMedia }
The questions are:
What may be causing this problem and how to fix that?
What are all the cases that a QMediaPlayer::mediaStatus() will return an QMediaPlayer::UnknownMediaStatus (please be conclusive)?
Edit with more information: The following is the output I get for the following code. Anyone has any idea what the error message means and how to fix that?
code:
int main(int argc, char *argv[])
{
QMediaPlayer * temp = new QMediaPlayer(0, QMediaPlayer::VideoSurface);
std::cout << "Constructed: " << temp->mediaStatus() << std::endl;
temp->setMedia(QUrl::fromLocalFile("path/to/video/test.mp4"));
std::cout << "SetMedia: " << temp->mediaStatus() << std::endl;
temp->play();
std::cout << "Play: " << temp->mediaStatus() << std::endl;
-> debug breakpoint here
......
}
output:
defaultServiceProvider::requestService(): no service found for - "org.qt-project.qt.mediaplayer"
Constructed: 0
SetMedia: 0
Play: 0
I am using Mac 10.9 and Qt 5.3.0, but I do not think the mac/qt version matters for this problem.

Why my fread stops when reading some file?

//#include <QtCore/QCoreApplication>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include "shift.h"
using namespace::std;
int main(int argc, char *argv[])
{
unsigned char tmpBuf;
FILE* fp;
FILE* fp2;
char fname[50];
static unsigned int lSize, count, num;
cout << "Input the filename:" << endl;
cin >> fname;
fp = fopen(fname,"r");
if(fp == NULL) {
cout << "The file does not exist!" << endl;
exit(1);
}
// obtain file size:
fseek(fp , 0 , SEEK_END);
lSize = ftell(fp);
rewind(fp);
cout << "The intput file's size is: " << lSize << endl;
fp2 = fopen("myfile", "w");
while(1){
num = fread(&tmpBuf, 1, 1, fp);
count += num;
// putchar(tmpBuf);
// tmpBuf = cror(tmpBuf, 4);
// tmpBuf = crol(tmpBuf, 4);
fwrite(&tmpBuf, 1, num, fp2);
cout << tmpBuf << " " << num << " " << count << endl;
if (count == lSize){
printf("over\n");
break;
}
}
fclose(fp);
fclose(fp2);
while(1){}
return 0;
//return a.exec();
}
I made a Qt console program and disable QtCore, like above code. When read some file, e.g. 1.txt (which contains only 1234567890) it succeeds. But when reading some other file, e.g. 1.rar, it failed like below: Why?
Check the value of num coming back from
num = fread(&tmpBuf, 1, 1, fp);
If it comes back as 0, that would explain why
if (count == lSize){
never comes back as true to break you out of the loop.
As to why that could happen, you're opening fname in "r" mode but a rar file would be binary. For that, I'd suggest opening in "rb" mode. If fread expects txt format but hits the EOF indicator, it'll stop advancing so num = 0 and count won't increase.
I don't have windows available to test this, but other questions have come up on stackoverflow for this reason:
fread/ftell apparently broken under Windows, works fine under Linux

Resources