Warning: That file was not part of the compilation database. It may have many parsing errors.

1/****************************************************************************
2**
3** Copyright (C) 2016 Lauri Laanmets (Proekspert AS) <lauri.laanmets@eesti.ee>
4** Copyright (C) 2016 The Qt Company Ltd.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtBluetooth module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtCore/QLoggingCategory>
42#include <QtAndroidExtras/QAndroidJniEnvironment>
43
44#include "android/inputstreamthread_p.h"
45#include "qbluetoothsocket_android_p.h"
46
47QT_BEGIN_NAMESPACE
48
49Q_DECLARE_LOGGING_CATEGORY(QT_BT_ANDROID)
50
51InputStreamThread::InputStreamThread(QBluetoothSocketPrivateAndroid *socket)
52 : QObject(), m_socket_p(socket), expectClosure(false)
53{
54}
55
56bool InputStreamThread::run()
57{
58 QMutexLocker lock(&m_mutex);
59
60 javaInputStreamThread = QAndroidJniObject("org/qtproject/qt5/android/bluetooth/QtBluetoothInputStreamThread");
61 if (!javaInputStreamThread.isValid() || !m_socket_p->inputStream.isValid())
62 return false;
63
64 javaInputStreamThread.callMethod<void>("setInputStream", "(Ljava/io/InputStream;)V",
65 m_socket_p->inputStream.object<jobject>());
66 javaInputStreamThread.setField<jlong>("qtObject", reinterpret_cast<long>(this));
67 javaInputStreamThread.setField<jboolean>("logEnabled", QT_BT_ANDROID().isDebugEnabled());
68
69 javaInputStreamThread.callMethod<void>("start");
70
71 return true;
72}
73
74qint64 InputStreamThread::bytesAvailable() const
75{
76 QMutexLocker locker(&m_mutex);
77 return m_socket_p->buffer.size();
78}
79
80bool InputStreamThread::canReadLine() const
81{
82 QMutexLocker locker(&m_mutex);
83 return m_socket_p->buffer.canReadLine();
84}
85
86qint64 InputStreamThread::readData(char *data, qint64 maxSize)
87{
88 QMutexLocker locker(&m_mutex);
89
90 if (!m_socket_p->buffer.isEmpty())
91 return m_socket_p->buffer.read(data, maxSize);
92
93 return 0;
94}
95
96//inside the java thread
97void InputStreamThread::javaThreadErrorOccurred(int errorCode)
98{
99 QMutexLocker lock(&m_mutex);
100
101 if (!expectClosure)
102 emit error(errorCode);
103 else
104 emit error(-1); //magic error, -1 means error was expected due to expected close()
105}
106
107//inside the java thread
108void InputStreamThread::javaReadyRead(jbyteArray buffer, int bufferLength)
109{
110 QAndroidJniEnvironment env;
111
112 QMutexLocker lock(&m_mutex);
113 char *writePtr = m_socket_p->buffer.reserve(bufferLength);
114 env->GetByteArrayRegion(buffer, 0, bufferLength, reinterpret_cast<jbyte*>(writePtr));
115 emit dataAvailable();
116}
117
118void InputStreamThread::prepareForClosure()
119{
120 QMutexLocker lock(&m_mutex);
121 expectClosure = true;
122}
123
124QT_END_NAMESPACE
125

Warning: That file was not part of the compilation database. It may have many parsing errors.