Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:significant reason:default
4
6
7#include <QDebug>
8#include <QHash>
9#include <QByteArray>
10#include <private/qcore_unix_p.h>
11
13
14const char *QQnxNavigatorPps::navigatorControlPath = "/pps/services/navigator/control";
15const size_t QQnxNavigatorPps::ppsBufferSize = 4096;
16
17QQnxNavigatorPps::QQnxNavigatorPps(QObject *parent)
18 : QQnxAbstractNavigator(parent)
19 , m_fd(-1)
20{
21}
22
24{
25 // close connection to navigator
26 if (m_fd != -1)
27 qt_safe_close(m_fd);
28}
29
30bool QQnxNavigatorPps::openPpsConnection()
31{
32 if (m_fd != -1)
33 return true;
34
35 // open connection to navigator
36 errno = 0;
37 m_fd = qt_safe_open(navigatorControlPath, O_RDWR);
38 if (m_fd == -1) {
39 qWarning("QQNX: failed to open navigator pps, errno=%d", errno);
40 return false;
41 }
42
43 qCDebug(lcQpaQnxNavigator) << "successfully connected to Navigator. fd=" << m_fd;
44
45 return true;
46}
47
48bool QQnxNavigatorPps::requestInvokeUrl(const QByteArray &encodedUrl)
49{
50 if (!openPpsConnection())
51 return false;
52
53 return sendPpsMessage("invoke", encodedUrl);
54}
55
56bool QQnxNavigatorPps::sendPpsMessage(const QByteArray &message, const QByteArray &data)
57{
58 QByteArray ppsMessage = "msg::" + message;
59
60 if (!data.isEmpty())
61 ppsMessage += "\ndat::" + data;
62
63 ppsMessage += "\n";
64
65 qCDebug(lcQpaQnxNavigator) << "sending PPS message:\n" << ppsMessage;
66
67 // send pps message to navigator
68 errno = 0;
69 int bytes = qt_safe_write(m_fd, ppsMessage.constData(), ppsMessage.size());
70 if (Q_UNLIKELY(bytes == -1))
71 qFatal("QQNX: failed to write navigator pps, errno=%d", errno);
72
73 // allocate buffer for pps data
74 char buffer[ppsBufferSize];
75
76 // attempt to read pps data
77 do {
78 errno = 0;
79 bytes = qt_safe_read(m_fd, buffer, ppsBufferSize - 1);
80 if (Q_UNLIKELY(bytes == -1))
81 qFatal("QQNX: failed to read navigator pps, errno=%d", errno);
82 } while (bytes == 0);
83
84 // ensure data is null terminated
85 buffer[bytes] = '\0';
86
87 qCDebug(lcQpaQnxNavigator) << "received PPS message:\n" << buffer;
88
89 // process received message
90 QByteArray ppsData(buffer);
91 QHash<QByteArray, QByteArray> responseFields;
92 parsePPS(ppsData, responseFields);
93
94 if (responseFields.contains("res") && responseFields.value("res") == message) {
95 if (Q_UNLIKELY(responseFields.contains("err"))) {
96 qCritical() << "navigator responded with error: " << responseFields.value("err");
97 return false;
98 }
99 }
100
101 return true;
102}
103
104void QQnxNavigatorPps::parsePPS(const QByteArray &ppsData, QHash<QByteArray, QByteArray> &messageFields)
105{
106 qCDebug(lcQpaQnxNavigator) << "data=" << ppsData;
107
108 // tokenize pps data into lines
109 QList<QByteArray> lines = ppsData.split('\n');
110
111 // validate pps object
112 if (Q_UNLIKELY(lines.empty() || lines.at(0) != "@control"))
113 qFatal("QQNX: unrecognized pps object, data=%s", ppsData.constData());
114
115 // parse pps object attributes and extract values
116 for (int i = 1; i < lines.size(); i++) {
117
118 // tokenize current attribute
119 const QByteArray &attr = lines.at(i);
120
121 qCDebug(lcQpaQnxNavigator) << "attr=" << attr;
122
123 int firstColon = attr.indexOf(':');
124 if (firstColon == -1) {
125 // abort - malformed attribute
126 continue;
127 }
128
129 int secondColon = attr.indexOf(':', firstColon + 1);
130 if (secondColon == -1) {
131 // abort - malformed attribute
132 continue;
133 }
134
135 QByteArray key = attr.left(firstColon);
136 QByteArray value = attr.mid(secondColon + 1);
137
138 qCDebug(lcQpaQnxNavigator) << "key=" << key << "value=" << value;
139 messageFields[key] = value;
140 }
141}
142
143QT_END_NAMESPACE
bool requestInvokeUrl(const QByteArray &encodedUrl) override
Combined button and popup list for selecting options.