Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qqnxnavigatorpps.cpp
Go to the documentation of this file.
1// Copyright (C) 2012 Research In Motion
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qqnxnavigatorpps.h"
5
6#include <QDebug>
7#include <QHash>
8#include <QByteArray>
9#include <private/qcore_unix_p.h>
10
12
13const char *QQnxNavigatorPps::navigatorControlPath = "/pps/services/navigator/control";
14const size_t QQnxNavigatorPps::ppsBufferSize = 4096;
15
17 : QQnxAbstractNavigator(parent)
18 , m_fd(-1)
19{
20}
21
23{
24 // close connection to navigator
25 if (m_fd != -1)
26 qt_safe_close(m_fd);
27}
28
29bool QQnxNavigatorPps::openPpsConnection()
30{
31 if (m_fd != -1)
32 return true;
33
34 // open connection to navigator
35 errno = 0;
36 m_fd = qt_safe_open(navigatorControlPath, O_RDWR);
37 if (m_fd == -1) {
38 qWarning("QQNX: failed to open navigator pps, errno=%d", errno);
39 return false;
40 }
41
42 qCDebug(lcQpaQnxNavigator) << "successfully connected to Navigator. fd=" << m_fd;
43
44 return true;
45}
46
48{
49 if (!openPpsConnection())
50 return false;
51
52 return sendPpsMessage("invoke", encodedUrl);
53}
54
55bool QQnxNavigatorPps::sendPpsMessage(const QByteArray &message, const QByteArray &data)
56{
57 QByteArray ppsMessage = "msg::" + message;
58
59 if (!data.isEmpty())
60 ppsMessage += "\ndat::" + data;
61
62 ppsMessage += "\n";
63
64 qCDebug(lcQpaQnxNavigator) << "sending PPS message:\n" << ppsMessage;
65
66 // send pps message to navigator
67 errno = 0;
68 int bytes = qt_safe_write(m_fd, ppsMessage.constData(), ppsMessage.size());
69 if (Q_UNLIKELY(bytes == -1))
70 qFatal("QQNX: failed to write navigator pps, errno=%d", errno);
71
72 // allocate buffer for pps data
73 char buffer[ppsBufferSize];
74
75 // attempt to read pps data
76 do {
77 errno = 0;
78 bytes = qt_safe_read(m_fd, buffer, ppsBufferSize - 1);
79 if (Q_UNLIKELY(bytes == -1))
80 qFatal("QQNX: failed to read navigator pps, errno=%d", errno);
81 } while (bytes == 0);
82
83 // ensure data is null terminated
84 buffer[bytes] = '\0';
85
86 qCDebug(lcQpaQnxNavigator) << "received PPS message:\n" << buffer;
87
88 // process received message
89 QByteArray ppsData(buffer);
90 QHash<QByteArray, QByteArray> responseFields;
91 parsePPS(ppsData, responseFields);
92
93 if (responseFields.contains("res") && responseFields.value("res") == message) {
94 if (Q_UNLIKELY(responseFields.contains("err"))) {
95 qCritical() << "navigator responded with error: " << responseFields.value("err");
96 return false;
97 }
98 }
99
100 return true;
101}
102
103void QQnxNavigatorPps::parsePPS(const QByteArray &ppsData, QHash<QByteArray, QByteArray> &messageFields)
104{
105 qCDebug(lcQpaQnxNavigator) << "data=" << ppsData;
106
107 // tokenize pps data into lines
108 QList<QByteArray> lines = ppsData.split('\n');
109
110 // validate pps object
111 if (Q_UNLIKELY(lines.empty() || lines.at(0) != "@control"))
112 qFatal("QQNX: 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 qCDebug(lcQpaQnxNavigator) << "attr=" << attr;
121
122 int firstColon = attr.indexOf(':');
123 if (firstColon == -1) {
124 // abort - malformed attribute
125 continue;
126 }
127
128 int secondColon = attr.indexOf(':', firstColon + 1);
129 if (secondColon == -1) {
130 // abort - malformed attribute
131 continue;
132 }
133
134 QByteArray key = attr.left(firstColon);
135 QByteArray value = attr.mid(secondColon + 1);
136
137 qCDebug(lcQpaQnxNavigator) << "key=" << key << "value=" << value;
138 messageFields[key] = value;
139 }
140}
141
\inmodule QtCore
Definition qbytearray.h:57
QByteArray left(qsizetype n) const &
Definition qbytearray.h:169
qsizetype indexOf(char c, qsizetype from=0) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
QByteArray mid(qsizetype index, qsizetype len=-1) const &
\inmodule QtCore
Definition qobject.h:103
bool requestInvokeUrl(const QByteArray &encodedUrl) override
QQnxNavigatorPps(QObject *parent=nullptr)
Combined button and popup list for selecting options.
#define Q_UNLIKELY(x)
static qint64 qt_safe_write(int fd, const void *data, qint64 len)
static int qt_safe_open(const char *pathname, int flags, mode_t mode=0777)
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
static int qt_safe_close(int fd)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qCritical
Definition qlogging.h:167
#define qWarning
Definition qlogging.h:166
#define qFatal
Definition qlogging.h:168
#define qCDebug(category,...)
GLuint64 key
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint buffer
GLuint GLsizei const GLchar * message