|
| 1 | +/* Copyright 2014 CyberTech Labs Ltd. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. */ |
| 14 | + |
| 15 | +#include "src/rangeSensorWorker.h" |
| 16 | + |
| 17 | +#include <QtCore/QDebug> |
| 18 | +#include <QtCore/QFileInfo> |
| 19 | +#include <QtCore/QStringList> |
| 20 | + |
| 21 | +#include <unistd.h> |
| 22 | +#include <fcntl.h> |
| 23 | +#include <errno.h> |
| 24 | +#include <linux/input.h> |
| 25 | + |
| 26 | +#include <QsLog.h> |
| 27 | + |
| 28 | +using namespace trikControl; |
| 29 | + |
| 30 | +RangeSensorWorker::RangeSensorWorker(QString const &eventFile) |
| 31 | + : mEventFile(eventFile) |
| 32 | +{ |
| 33 | +} |
| 34 | + |
| 35 | +RangeSensorWorker::~RangeSensorWorker() |
| 36 | +{ |
| 37 | + stop(); |
| 38 | +} |
| 39 | + |
| 40 | +void RangeSensorWorker::stop() |
| 41 | +{ |
| 42 | + if (mSocketNotifier) { |
| 43 | + disconnect(mSocketNotifier.data(), SIGNAL(activated(int)), this, SLOT(readFile())); |
| 44 | + mSocketNotifier->setEnabled(false); |
| 45 | + } |
| 46 | + |
| 47 | + if (::close(mEventFileDescriptor) != 0) { |
| 48 | + QString const message = QString("%1: close failed: %2").arg(mEventFile.fileName()).arg(errno); |
| 49 | + QLOG_ERROR() << message; |
| 50 | + qDebug() << message; |
| 51 | + } |
| 52 | + |
| 53 | + mEventFileDescriptor = -1; |
| 54 | +} |
| 55 | + |
| 56 | +void RangeSensorWorker::init() |
| 57 | +{ |
| 58 | + QLOG_INFO() << "Opening" << mEventFile.fileName(); |
| 59 | + qDebug() << "Opening" << mEventFile.fileName(); |
| 60 | + |
| 61 | + /// @todo Is it possible to use QFile (and QFile::handle()) here? |
| 62 | + mEventFileDescriptor = open(mEventFile.fileName().toStdString().c_str(), O_SYNC | O_NONBLOCK, O_RDONLY); |
| 63 | + |
| 64 | + if (mEventFileDescriptor == -1) { |
| 65 | + QLOG_ERROR() << "Cannot open range sensor output file" << mEventFile.fileName(); |
| 66 | + qDebug() << "Cannot open range sensor output file" << mEventFile.fileName(); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + mSocketNotifier.reset(new QSocketNotifier(mEventFileDescriptor, QSocketNotifier::Read)); |
| 71 | + |
| 72 | + connect(mSocketNotifier.data(), SIGNAL(activated(int)), this, SLOT(readFile())); |
| 73 | + mSocketNotifier->setEnabled(true); |
| 74 | +} |
| 75 | + |
| 76 | +void RangeSensorWorker::readFile() |
| 77 | +{ |
| 78 | + struct input_event event; |
| 79 | + int size = 0; |
| 80 | + |
| 81 | + mSocketNotifier->setEnabled(false); |
| 82 | + |
| 83 | + while ((size = ::read(mEventFileDescriptor, reinterpret_cast<char *>(&event), sizeof(event))) |
| 84 | + == static_cast<int>(sizeof(event))) |
| 85 | + { |
| 86 | + switch (event.type) { |
| 87 | + case EV_ABS: |
| 88 | + switch (event.code) { |
| 89 | + case ABS_DISTANCE: |
| 90 | + mLock.lockForWrite(); |
| 91 | + mDistance = event.value; |
| 92 | + mLock.unlock(); |
| 93 | + break; |
| 94 | + case ABS_MISC: |
| 95 | + mLock.lockForWrite(); |
| 96 | + mRawDistance = event.value; |
| 97 | + mLock.unlock(); |
| 98 | + break; |
| 99 | + } |
| 100 | + break; |
| 101 | + case EV_SYN: |
| 102 | + mLock.lockForRead(); |
| 103 | + emit newData(mDistance, mRawDistance); |
| 104 | + mLock.unlock(); |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (0 <= size && size < static_cast<int>(sizeof(event))) { |
| 110 | + QLOG_ERROR() << "incomplete data read"; |
| 111 | + qDebug() << "incomplete data read"; |
| 112 | + } |
| 113 | + |
| 114 | + mSocketNotifier->setEnabled(true); |
| 115 | +} |
| 116 | + |
| 117 | +int RangeSensorWorker::read() |
| 118 | +{ |
| 119 | + mLock.lockForRead(); |
| 120 | + int const result = mDistance; |
| 121 | + mLock.unlock(); |
| 122 | + return result; |
| 123 | +} |
| 124 | + |
| 125 | +int RangeSensorWorker::readRawData() |
| 126 | +{ |
| 127 | + mLock.lockForRead(); |
| 128 | + int const result = mRawDistance; |
| 129 | + mLock.unlock(); |
| 130 | + return result; |
| 131 | +} |
0 commit comments