Qt 6.x
The Qt SDK
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
11#if defined(QQNXNAVIGATOR_DEBUG)
12#define qNavigatorDebug qDebug
13#else
14#define qNavigatorDebug QT_NO_QDEBUG_MACRO
15#endif
16
18
19const char *QQnxNavigatorPps::navigatorControlPath = "/pps/services/navigator/control";
20const size_t QQnxNavigatorPps::ppsBufferSize = 4096;
21
24 , m_fd(-1)
25{
26}
27
29{
30 // close connection to navigator
31 if (m_fd != -1)
32 qt_safe_close(m_fd);
33}
34
35bool QQnxNavigatorPps::openPpsConnection()
36{
37 if (m_fd != -1)
38 return true;
39
40 // open connection to navigator
41 errno = 0;
42 m_fd = qt_safe_open(navigatorControlPath, O_RDWR);
43 if (m_fd == -1) {
44 qWarning("QQNX: failed to open navigator pps, errno=%d", errno);
45 return false;
46 }
47
48 qNavigatorDebug("successfully connected to Navigator. fd=%d", m_fd);
49
50 return true;
51}
52
54{
55 if (!openPpsConnection())
56 return false;
57
58 return sendPpsMessage("invoke", encodedUrl);
59}
60
61bool QQnxNavigatorPps::sendPpsMessage(const QByteArray &message, const QByteArray &data)
62{
63 QByteArray ppsMessage = "msg::" + message;
64
65 if (!data.isEmpty())
66 ppsMessage += "\ndat::" + data;
67
68 ppsMessage += "\n";
69
70 qNavigatorDebug() << "sending PPS message:\n" << ppsMessage;
71
72 // send pps message to navigator
73 errno = 0;
74 int bytes = qt_safe_write(m_fd, ppsMessage.constData(), ppsMessage.size());
75 if (Q_UNLIKELY(bytes == -1))
76 qFatal("QQNX: failed to write navigator pps, errno=%d", errno);
77
78 // allocate buffer for pps data
79 char buffer[ppsBufferSize];
80
81 // attempt to read pps data
82 do {
83 errno = 0;
84 bytes = qt_safe_read(m_fd, buffer, ppsBufferSize - 1);
85 if (Q_UNLIKELY(bytes == -1))
86 qFatal("QQNX: failed to read navigator pps, errno=%d", errno);
87 } while (bytes == 0);
88
89 // ensure data is null terminated
90 buffer[bytes] = '\0';
91
92 qNavigatorDebug() << "received PPS message:\n" << buffer;
93
94 // process received message
95 QByteArray ppsData(buffer);
96 QHash<QByteArray, QByteArray> responseFields;
97 parsePPS(ppsData, responseFields);
98
99 if (responseFields.contains("res") && responseFields.value("res") == message) {
100 if (Q_UNLIKELY(responseFields.contains("err"))) {
101 qCritical() << "navigator responded with error: " << responseFields.value("err");
102 return false;
103 }
104 }
105
106 return true;
107}
108
109void QQnxNavigatorPps::parsePPS(const QByteArray &ppsData, QHash<QByteArray, QByteArray> &messageFields)
110{
111 qNavigatorDebug() << "data=" << ppsData;
112
113 // tokenize pps data into lines
114 QList<QByteArray> lines = ppsData.split('\n');
115
116 // validate pps object
117 if (Q_UNLIKELY(lines.empty() || lines.at(0) != "@control"))
118 qFatal("QQNX: unrecognized pps object, data=%s", ppsData.constData());
119
120 // parse pps object attributes and extract values
121 for (int i = 1; i < lines.size(); i++) {
122
123 // tokenize current attribute
124 const QByteArray &attr = lines.at(i);
125
126 qNavigatorDebug() << "attr=" << attr;
127
128 int firstColon = attr.indexOf(':');
129 if (firstColon == -1) {
130 // abort - malformed attribute
131 continue;
132 }
133
134 int secondColon = attr.indexOf(':', firstColon + 1);
135 if (secondColon == -1) {
136 // abort - malformed attribute
137 continue;
138 }
139
140 QByteArray key = attr.left(firstColon);
141 QByteArray value = attr.mid(secondColon + 1);
142
143 qNavigatorDebug() << "key=" << key;
144 qNavigatorDebug() << "val=" << value;
145 messageFields[key] = value;
146 }
147}
148
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:474
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:122
QList< QByteArray > split(char sep) const
Splits the byte array into subarrays wherever sep occurs, and returns the list of those arrays.
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 left(qsizetype len) const
Returns a byte array that contains the first len bytes of this byte array.
QByteArray mid(qsizetype index, qsizetype len=-1) const
Returns a byte array containing len bytes from this byte array, starting at position pos.
\inmodule QtCore
Definition qhash.h:818
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
Definition qhash.h:991
T value(const Key &key) const noexcept
Definition qhash.h:1044
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
bool empty() const noexcept
Definition qlist.h:682
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
\inmodule QtCore
Definition qobject.h:90
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:163
#define qWarning
Definition qlogging.h:162
#define qFatal
Definition qlogging.h:164
GLuint64 key
GLenum GLuint buffer
GLuint GLsizei const GLchar * message
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
#define qNavigatorDebug
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent