1 /***************************************************************************
2 **
3 ** Copyright (C) 2012 Research In Motion
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins 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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qqnxnavigatorpps.h"
41 
42 #include <QDebug>
43 #include <private/qcore_unix_p.h>
44 
45 #if defined(QQNXNAVIGATOR_DEBUG)
46 #define qNavigatorDebug qDebug
47 #else
48 #define qNavigatorDebug QT_NO_QDEBUG_MACRO
49 #endif
50 
51 static const char *navigatorControlPath = "/pps/services/navigator/control";
52 static const int ppsBufferSize = 4096;
53 
54 QT_BEGIN_NAMESPACE
55 
QQnxNavigatorPps(QObject * parent)56 QQnxNavigatorPps::QQnxNavigatorPps(QObject *parent)
57     : QQnxAbstractNavigator(parent)
58     , m_fd(-1)
59 {
60 }
61 
~QQnxNavigatorPps()62 QQnxNavigatorPps::~QQnxNavigatorPps()
63 {
64     // close connection to navigator
65     if (m_fd != -1)
66         qt_safe_close(m_fd);
67 }
68 
openPpsConnection()69 bool QQnxNavigatorPps::openPpsConnection()
70 {
71     if (m_fd != -1)
72         return true;
73 
74     // open connection to navigator
75     errno = 0;
76     m_fd = qt_safe_open(navigatorControlPath, O_RDWR);
77     if (m_fd == -1) {
78         qWarning("QQNX: failed to open navigator pps, errno=%d", errno);
79         return false;
80     }
81 
82     qNavigatorDebug("successfully connected to Navigator. fd=%d", m_fd);
83 
84     return true;
85 }
86 
requestInvokeUrl(const QByteArray & encodedUrl)87 bool QQnxNavigatorPps::requestInvokeUrl(const QByteArray &encodedUrl)
88 {
89     if (!openPpsConnection())
90         return false;
91 
92     return sendPpsMessage("invoke", encodedUrl);
93 }
94 
sendPpsMessage(const QByteArray & message,const QByteArray & data)95 bool QQnxNavigatorPps::sendPpsMessage(const QByteArray &message, const QByteArray &data)
96 {
97     QByteArray ppsMessage = "msg::" + message;
98 
99     if (!data.isEmpty())
100         ppsMessage += "\ndat::" + data;
101 
102     ppsMessage += "\n";
103 
104     qNavigatorDebug() << "sending PPS message:\n" << ppsMessage;
105 
106     // send pps message to navigator
107     errno = 0;
108     int bytes = qt_safe_write(m_fd, ppsMessage.constData(), ppsMessage.size());
109     if (Q_UNLIKELY(bytes == -1))
110         qFatal("QQNX: failed to write navigator pps, errno=%d", errno);
111 
112     // allocate buffer for pps data
113     char buffer[ppsBufferSize];
114 
115     // attempt to read pps data
116     do {
117         errno = 0;
118         bytes = qt_safe_read(m_fd, buffer, ppsBufferSize - 1);
119         if (Q_UNLIKELY(bytes == -1))
120             qFatal("QQNX: failed to read navigator pps, errno=%d", errno);
121     } while (bytes == 0);
122 
123     // ensure data is null terminated
124     buffer[bytes] = '\0';
125 
126     qNavigatorDebug() << "received PPS message:\n" << buffer;
127 
128     // process received message
129     QByteArray ppsData(buffer);
130     QHash<QByteArray, QByteArray> responseFields;
131     parsePPS(ppsData, responseFields);
132 
133     if (responseFields.contains("res") && responseFields.value("res") == message) {
134         if (Q_UNLIKELY(responseFields.contains("err"))) {
135             qCritical() << "navigator responded with error: " << responseFields.value("err");
136             return false;
137         }
138     }
139 
140     return true;
141 }
142 
parsePPS(const QByteArray & ppsData,QHash<QByteArray,QByteArray> & messageFields)143 void QQnxNavigatorPps::parsePPS(const QByteArray &ppsData, QHash<QByteArray, QByteArray> &messageFields)
144 {
145     qNavigatorDebug() << "data=" << ppsData;
146 
147     // tokenize pps data into lines
148     QList<QByteArray> lines = ppsData.split('\n');
149 
150     // validate pps object
151     if (Q_UNLIKELY(lines.empty() || lines.at(0) != "@control"))
152         qFatal("QQNX: unrecognized pps object, data=%s", ppsData.constData());
153 
154     // parse pps object attributes and extract values
155     for (int i = 1; i < lines.size(); i++) {
156 
157         // tokenize current attribute
158         const QByteArray &attr = lines.at(i);
159 
160         qNavigatorDebug() << "attr=" << attr;
161 
162         int firstColon = attr.indexOf(':');
163         if (firstColon == -1) {
164             // abort - malformed attribute
165             continue;
166         }
167 
168         int secondColon = attr.indexOf(':', firstColon + 1);
169         if (secondColon == -1) {
170             // abort - malformed attribute
171             continue;
172         }
173 
174         QByteArray key = attr.left(firstColon);
175         QByteArray value = attr.mid(secondColon + 1);
176 
177         qNavigatorDebug() << "key=" << key;
178         qNavigatorDebug() << "val=" << value;
179         messageFields[key] = value;
180     }
181 }
182 
183 QT_END_NAMESPACE
184