Qt 4.8
qbbnavigatoreventnotifier.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 - 2012 Research In Motion <blackberry-qt@qnx.com>
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 //#define QBBNAVIGATOREVENTNOTIFIER_DEBUG
43 
44 
47 
48 #include <QtCore/private/qcore_unix_p.h>
49 #include <QByteArray>
50 #include <QList>
51 #include <QDebug>
52 #include <QSocketNotifier>
53 
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <unistd.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 
60 #define NAV_CONTROL_PATH "/pps/services/navigator/control"
61 #define PPS_BUFFER_SIZE 4096
62 
64  : QObject(parent),
65  mEventHandler(eventHandler),
66  mFd(-1),
67  mReadNotifier(0)
68 {
69 }
70 
72 {
73  delete mReadNotifier;
74 
75  if (mFd != -1)
77 
78 #if defined(QBBNAVIGATOREVENTNOTIFIER_DEBUG)
79  qDebug() << "QBB: navigator event notifier stopped";
80 #endif
81 }
82 
84 {
85 #if defined(QBBNAVIGATOREVENTNOTIFIER_DEBUG)
86  qDebug() << "QBB: navigator event notifier started";
87 #endif
88 
89  // open connection to navigator
90  errno = 0;
92  if (mFd == -1) {
93  qWarning("QBB: failed to open navigator pps, errno=%d", errno);
94  return;
95  }
96 
98  connect(mReadNotifier, SIGNAL(activated(int)), this, SLOT(readData()));
99 }
100 
102 {
103 #if defined(QBBNAVIGATOREVENTNOTIFIER_DEBUG)
104  qDebug() << "PPS: data=" << ppsData;
105 #endif
106 
107  // tokenize pps data into lines
108  QList<QByteArray> lines = ppsData.split('\n');
109 
110  // validate pps object
111  if (lines.size() == 0 || lines.at(0) != "@control")
112  qFatal("QBB: unrecognized pps object, data=%s", ppsData.constData());
113 
114  // parse pps object attributes and extract values
115  for (int i = 1; i < lines.size(); i++) {
116 
117  // tokenize current attribute
118  const QByteArray &attr = lines.at(i);
119 
120 #if defined(QBBNAVIGATOREVENTNOTIFIER_DEBUG)
121  qDebug() << "PPS: attr=" << attr;
122 #endif
123 
124  int firstColon = attr.indexOf(':');
125  if (firstColon == -1) {
126  // abort - malformed attribute
127  continue;
128  }
129 
130  int secondColon = attr.indexOf(':', firstColon + 1);
131  if (secondColon == -1) {
132  // abort - malformed attribute
133  continue;
134  }
135 
136  QByteArray key = attr.left(firstColon);
137  QByteArray value = attr.mid(secondColon + 1);
138 
139 #if defined(QBBNAVIGATOREVENTNOTIFIER_DEBUG)
140  qDebug() << "PPS: key=" << key;
141  qDebug() << "PPS: val=" << value;
142 #endif
143 
144  // save attribute value
145  if (key == "msg")
146  msg = value;
147  else if (key == "dat")
148  dat = value;
149  else if (key == "id")
150  id = value;
151  else
152  qFatal("QBB: unrecognized pps attribute, attr=%s", key.constData());
153  }
154 }
155 
157 {
158  // construct pps message
159  QByteArray ppsData = "res::";
160  ppsData += res;
161  ppsData += "\nid::";
162  ppsData += id;
163  if (!dat.isEmpty()) {
164  ppsData += "\ndat::";
165  ppsData += dat;
166  }
167  ppsData += "\n";
168 
169 #if defined(QBBNAVIGATOREVENTNOTIFIER_DEBUG)
170  qDebug() << "PPS reply=" << ppsData;
171 #endif
172 
173  // send pps message to navigator
174  errno = 0;
175  int bytes = write(mFd, ppsData.constData(), ppsData.size());
176  if (bytes == -1)
177  qFatal("QBB: failed to write navigator pps, errno=%d", errno);
178 }
179 
181 {
182 #if defined(QBBNAVIGATOREVENTNOTIFIER_DEBUG)
183  qDebug() << "PPS: msg=" << msg << ", dat=" << dat << ", id=" << id;
184 #endif
185 
186  // check message type
187  if (msg == "orientationCheck") {
188  const bool result = mEventHandler->handleOrientationCheck(dat.toInt());
189  replyPPS(msg, id, result ? "true": "false");
190  } else if (msg == "orientation") {
192  replyPPS(msg, id, "");
193  } else if (msg == "SWIPE_DOWN") {
195  } else if (msg == "exit") {
197  } else if (msg == "windowActive") {
199  } else if (msg == "windowInactive") {
201  }
202 }
203 
205 {
206 #if defined(QBBNAVIGATOREVENTNOTIFIER_DEBUG)
207  qDebug() << "QBB: reading navigator data";
208 #endif
209 
210  // allocate buffer for pps data
211  char buffer[PPS_BUFFER_SIZE];
212 
213  // attempt to read pps data
214  errno = 0;
215  int bytes = qt_safe_read(mFd, buffer, PPS_BUFFER_SIZE - 1);
216  if (bytes == -1)
217  qFatal("QBB: failed to read navigator pps, errno=%d", errno);
218 
219  // check if pps data was received
220  if (bytes > 0) {
221 
222  // ensure data is null terminated
223  buffer[bytes] = '\0';
224 
225  // process received message
226  QByteArray ppsData(buffer);
227  QByteArray msg;
228  QByteArray dat;
229  QByteArray id;
230  parsePPS(ppsData, msg, dat, id);
231  handleMessage(msg, dat, id);
232  }
233 
234 }
void handleWindowGroupActivated(const QByteArray &id)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define SLOT(a)
Definition: qobjectdefs.h:226
QBBNavigatorEventHandler * mEventHandler
void handleWindowGroupDeactivated(const QByteArray &id)
static int qt_safe_close(int fd)
Definition: qcore_unix_p.h:297
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
The QSocketNotifier class provides support for monitoring activity on a file descriptor.
void parsePPS(const QByteArray &ppsData, QByteArray &msg, QByteArray &dat, QByteArray &id)
Q_CORE_EXPORT void qDebug(const char *,...)
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define PPS_BUFFER_SIZE
static int qt_safe_open(const char *pathname, int flags, mode_t mode=0777)
Definition: qcore_unix_p.h:171
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
Q_CORE_EXPORT void qWarning(const char *,...)
QByteArray left(int len) const
Returns a byte array that contains the leftmost len bytes of this byte array.
QByteArray mid(int index, int len=-1) const
Returns a byte array containing len bytes from this byte array, starting at position pos...
int indexOf(char c, int from=0) const
Returns the index position of the first occurrence of the character ch in the byte array...
QBBNavigatorEventNotifier(QBBNavigatorEventHandler *eventHandler, QObject *parent=0)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QList< QByteArray > split(char sep) const
Splits the byte array into subarrays wherever sep occurs, and returns the list of those arrays...
Q_CORE_EXPORT void qFatal(const char *,...)
void handleMessage(const QByteArray &msg, const QByteArray &dat, const QByteArray &id)
int toInt(bool *ok=0, int base=10) const
Returns the byte array converted to an int using base base, which is 10 by default and must be betwee...
int key
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
void replyPPS(const QByteArray &res, const QByteArray &id, const QByteArray &dat)
#define O_RDWR
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
Definition: qcore_unix_p.h:273
#define NAV_CONTROL_PATH
int errno