Commit graph

42 commits

Author SHA1 Message Date
Andreas Buhr
41fc822d56 Use QtBluetooth enums from their namespace
For increased type safety, some enums have been changed to
scoped enums in Qt 6.2, see
https://codereview.qt-project.org/c/qt/qtconnectivity/+/337069
https://codereview.qt-project.org/c/qt/qtconnectivity/+/336678
This patch adapts subsurface to this change.
Since C++11, enums inject their symbols in both their own
and their parent namespace, so this patch can be merged right
now.

Signed-off-by: Andreas Buhr <andreas.buhr@qt.io>
2021-03-12 08:41:31 -08:00
Berthold Stoeger
5a19437311 cleanup: remove dc_user_device_t
The same structure was defined as "struct dc_user_device_t"
and typedefed as "device_data_t". Unify this. Since there
are much more of the latter, remove the former.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-24 09:51:37 -07:00
Dirk Hohndel
ce1582581b core: fix compile issue with older g++
Having the full list of all members in the exact order should be enough to get
g++ to accept the named initializers.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2020-06-18 09:05:13 -07:00
Linus Torvalds
7f0ee3d8e2 core: fix libdivecomputer dc_custom callbacks structures
The last time those changed, we forgot to update serial_ftdi. In that change
set_latency had been removed from libdivecomputer and poll and ioctl had been
added. This caused the callbacks to no longer be aligned correctly and the
functions were called with the wrong arguments through the wrong function
pointers, leading to crashes.

Instead of the fragile assumptions about order and type of function pointers,
use named initializers. And while we are at it, fix that for the bluetooth
implementation as well.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2020-06-18 09:05:13 -07:00
Linus Torvalds
5e89d81d9d iostream: fix incorrect rfcomm error case when writing
This is the exact same case as the previous commit, just for the writing
side.

Once again, it's the subsurface rfcomm iostream code that can return
DC_STATUS_SUCCESS with a byte count of zero when something goes wrong
with the write.

And once again, our libdivecomputer iostream code didn't try to be
robust and protect itself from that case.

The fix is equivalent, although slightly simpler, since the write side
doesn't have the whole timeout issue.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-03-14 11:34:46 -07:00
Linus Torvalds
4ee4bbdb58 iostream: fix incorrect rfcomm error case when reading
We had two independent bugs here, both of which needed to fire for this
to cause a problem.  This fixes both of them.

The first bug was that our rfcomm code would return DC_STATUS_SUCCESS
with a zero-sized read when a timeout happened, or when the rfcomm
socket had disconnected.  That makes absolutely no sense.  We should
return DC_STATUS_TIMEOUT on timeout, and DC_STATUS_IO if the socket has
disconnected without any data.

The fix to this is to make the whole rfcomm iostream read logic much
simpler: there's no need to loop at all for partial results, because the
libdivecomputer iostream side will do the loop for us (and handle
partial results much better: it knows if the target backend can handle
those partial results or not).

The second bug was in our libdivecomputer iostream read() function,
which reacted very badly to this bad return value.  This updates our
libdivecomputer branch to one that is more careful about things.

Reported-by: linuxcrash <albin@mrty.ch>
Debugged-by: Jef Driesen <jef@libdivecomputer.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-03-14 11:34:46 -07:00
Lubomir I. Ivanov
e69f5c4e28 core/qtserialbluetooth.cpp: use QEventLoop for polling
The Qt docs here:
https://doc.qt.io/qt-5/qbluetoothsocket.html#details
and here:
https://doc.qt.io/qt-5/qabstractsocket.html#waitForReadyRead

say that waitForReadyRead() does not work for QBluetoothSocket
and that it's flaky on Windows for the underlying QAbstractSocket.

Use a QEventLoop and a QTimer to poll the readyRead() signal.

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
2020-01-27 09:51:31 -08:00
Linus Torvalds
aceb8a547f rfcomm: make Windows use QtBluetooth too
Windows had it's own direct socket implementation for rfcomm (ie legacy
BT), while all the other platforms used QtBluetooth.

This makes Windows do the same thing.  Hopefully modern Qt libraries now
work well enough on the Windows platform for this to work, but I can't
test it.

We can make a test build that Windows people can try, though.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-01-27 09:51:31 -08:00
Linus Torvalds
9543f53150 Update to new libdivecomputer version
Jef has changed the libdivecomputer iostream layer and extended it in
two different ways:

 - iostram's now have a 'poll()' method, which does what the name
   implies: waits for data to be available with a timeout.

 - iostreams now have a 'ioctl()' method, which can be used to implement
   miscellaneous operations. Right now the two ones that you can do are
   "set latency" (this replaces the old 'set_latency()' method) and "get
   BLE name" (this replaces our 'get_name()' method that was never part
   of the upstream libdivecomputer interfaces)

Neither of these is all that complicated, and the transition is fairly
obvious.

HOWEVER.

I have absolutely no idea how to do 'poll()' on Windows sockets, and I
have no intention of figuring it out.  We use a direct socket interface
to implement the (non-BLE) RFCOMM bluetooth serial protocol, and I'm not
sure why Windows is so special here.  I suspect - but cannot test - that
we should just switch the Windows RFCOMM implementation over to the use
the same QtBluetooth code that we use on other platforms.

I assume that the Windows Bluetooth support was originally not
sufficiently good for that, but these days we depend on Qt doing BLE for
us even on Windows, so presumably FRCOMM works too.

That would be a nice cleanup, and would make 'poll()' work on RFCOMM
under Windows too.  However, since I can't test it, I've not done that,
but instead just made the Windows RFCOMM 'poll()' method always return
success.  That may or may not get the thing limping along.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-01-27 09:51:31 -08:00
Rolf Eike Beer
5ad52db451 drop support for Qt 5.4 and before
The oldest version tested on TravisCI is Qt 5.5, which is also what is in
Ubuntu 16.04. Drop all the older cruft, noone should use that anymore.

Signed-off-by: Rolf Eike Beer <eike@sf-mail.de>
2019-03-27 07:36:33 -07:00
Jan Mulder
09a7736ae1 Core: fix BT on Linux, workaround Qt bug on 5.12.0
After upgrading to Qt 5.12.0, download over BT from a DC did not work
any more. On the console the message "Connecting to port is not
supported (Uuid required)". Linus noticed earlier that we do rather
strange processing in this part of the code related to selecting port 1
or port 5. This all seems not needed (any more), but broader testing is
advised. This being stripped from the code, the mentioned error from Qt
persisted. That is strange in itself, as we did not reference port
numbers any more.

Step 2 in this commit is actually using an uuid to the call to
connectToService. Choosing an uuid seems relatively straightforward as
we can use the same one we already use for Android. That is the default
BT RFCOMM Serial Port Profile uuid. Interestingly, when changing to this
uuid we run immediately in a Qt runtime error telling us "QDBusPendingReply:
type ManagedObjectList is not registered with QtDBus.". For these 2
unexpected Qt messages, QTBUG-72742 was made. Studying the Qt source
code at this point reveals a possible workaround. Simply create a local
QBluetoothLocalDevice object, which, behind the scenes registers the Qt
internal ManagedObjectList with QtDBus.

In the meantime, Qt agrees that QTBUG-72742 is valid, and that a fix is
to be expected in a future version. At that point in time, the
declaration of the QBluetoothLocalDevice can be deleted again.

In the end, interfacing over BT works again.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
2018-12-27 15:15:07 -08:00
Linus Torvalds
9e3a22c522 qt-ble: add 'get_name()' function to expose the BLE name to libdivecomputer
Some divecomputer backends (ok, right now really only the Aqualung i770R
and i300C) want to know the bluetooth name of the dive computer they
connect to, because the name contains identifying information like the
serial number.

This just adds the support for that to our Qt BLE code.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-10-08 00:10:29 +03:00
Linus Torvalds
cd55344410 qt-ble: add support for libdivecomputer 'set_timeout()' function
Because some BLE operations can be very slow (device and service
discovery etc), we have some rather excessive default timeout for BLE
(currently set to 12 seconds).

But once we actually have started doing IO, that long timeout can be a
big performance problem, when the libdivecomputer backend has support
for retry and packet loss.

For that reason, libdivecomputer has a 'set_timeout()' function that
allows the divecomputer backend to say how quickly it expects the dive
computer to answer before the backend will start resending packets.

Let's just implement that for the actual IO side of BLE too.  The
default timeout value remains the general BLE timeout, and this only
affects the actual IO phase, but it improves things enormously for the
case where there is packet loss at that point.

For example, on the Aqualung i770R, the timeout for packet loss ends up
now being just one second rather than the full 12 seconds of default BLE
timeout.  Which gets the retry going much faster.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-10-06 19:38:28 -07:00
Linus Torvalds
b34a4063be Make sure our libdivecomputer custom IO interfaces have sleep functions
When I switched over from our own custom IO implementation to the new
upstream custom IO model in libdivecomputer, I completely missed the
fact that the libdivecomputer custom IO model also does a custom _sleep_
function.

I'm not entirely sure what the point was, and it broke things even in
libdivecopmputer itself when some of the new sleep functions were
broken.

Anyway, we didn't export any sleep functions at all for the bluetooth,
BLE and FTDI cases, the the libdivecomputer code didn't fall back to any
sane default sleep implementation either, so the end result was no
sleeping at all.

Which didn't matter for most divecomputers.

But it seems like at least some OSTC dive computers did care, at least
in certain situations, and both Miika and Anton had trouble downloading
with their OSTC Sport dive computers.  Using the serial line protocol
and the legacy /dev/rfcomm model worked fine, because then it used the
sleeping functions in the POSIX serial code inside libdivecomputer.

This just adds trivial sleeping functions for the affected download
protocols.  Maybe I should have just made libdivecomputer have a sane
default instead, but this wasn't hard either (the hard part was trying
to figure out why the downloads worked for some people and not for
others).

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-07-13 11:25:38 -07:00
jan Iversen
12b4f2235b core: replace (void) with no parameter name
Unused parameters in C++ are "silenced" by removing the name.

Signed-off-by: Jan Iversen <jani@apache.org>
2018-05-24 08:34:14 -07:00
jan Iversen
a312e53f0c core: Change Q_UNUSED to no parameter name
C++ permits use of parameters without name, which signals unused

Signed-off-by: Jan Iversen <jani@apache.org>
2018-05-21 12:48:04 -07:00
jan Iversen
d8227c4a70 core: marked unused parameter
Add Q_UNUSED to unused parameter

Signed-off-by: Jan Iversen <jani@apache.org>
2018-05-20 08:11:29 -07:00
Dirk Hohndel
b930c39ead Compile fix when compiling without BLE enabled
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2018-04-25 07:56:19 -07:00
Linus Torvalds
13f5c75ac4 Convert our custom IO model to new libdivecomputer IO model
This converts our old custom IO model to the new model that
libdivecomputer introduced.  This is partly based on Jef's rough patch
to make things build, with further work by me.

The FTDI code is temporarily disabled here, because it will need to be
integrated with the new way of opening devices.

The ble_serial code goes away entirely, since now libdivecomputer knows
about BLE transport natively, and doesn't need to have any serial
wrapper around it.

Signed-off-by: Jef Driesen <jef@libdivecomputer.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-04-24 17:54:08 -07:00
Dirk Hohndel
d15a0bc277 Fix typos
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2018-04-13 17:07:42 -07:00
Linus Torvalds
f5e2a17e7d Subsurface update for upstream libdivecomputer changes
So because I merged with upstream libdivecomputer, and it no longer does
the "halfduplex emulation" thing in the IO layer, and instead does it in
the only Suunto backend that needed it, that also affected our custom IO
layer in subsurface.

Sure, I could have left a dummy interface and left subsurface with some
ugly dead code, but it's really better to just get rid of the code.

So when Dirk pulls in the libdivecomputer updates from

    https://github.com/torvalds/libdc-for-dirk.git Subsurface-branch

this patch to remove the halfduplex code in subsurface is also needed.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Stefan Fuchs <sfuchs@gmx.de>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2018-03-13 22:34:57 -07:00
Berthold Stoeger
e762fd2d41 Fix typo: successfull -> successful and succesfully -> successfully
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2017-11-20 10:01:15 +01:00
Dirk Hohndel
27c49fe3ad Silence random warnings
None of these seem to point to actual issues, so let's quiet them.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-10-20 17:25:18 -04:00
Jan Mulder
f442031fdd BLE: big writes to connected DC (OSTC firmware)
Most writes to a connected DC are small, typically some
command bytes to get DC in download mode, or to set
some parameter. All this just worked over BLE,
however, sending a full firmware update (on an
OSTC device) failed, as the underlying BLE interface
can only handle small 20 byte BLE packets at once.

So, send max ble->packet_size chuncks at once.

Tested for the following cases (linux desktop with
OSTC3 over BLE):
1) normal download of dive data.
2) read and write settings from configure UI
3) update firmware (from 2.15 to 2.15)

And to my surprise, no flow control credit administration
is required here.

Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
2017-09-22 02:30:58 -07:00
Dirk Hohndel
ea8de175a7 Remove unused function
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-08-26 12:19:48 -07:00
Jan Mulder
883063875e BLE serial read/write buffer
The adapted define was confusingly wrong. Apparently, the BUFSIZ
define was coming from some include file, and was dependent on
platform (Linux 8K, Andorid 1K). Simple rewrite to a new define
and a proper value for both Linux and Android. If 4K is big
enhough, is a little uncertain, as its depends on the read
behavior of all libdivecomputer parsers using this serial
BLE interface.

The buffer size needed (on read, as that is the most prominent
direction when interfacing with DCs) is (most likely) 2x the
maximum block the libdc parsers request at once. I did not
study all parsers, but the Shearwater parser request 20 bytes
at once (we know that from the 1 packet at the time read, we
had before). The OSTC parser request 1K blocks for data
that is longer than 1K (like profiles, header tables).

The 1K we had on Android was working for Shearwater,
Eon Steel, but not for OSTC,as its reads 1K at the time
at max, and overflowing the buffer.

So 32k or 64k seems way to big (as in, much bigger than
any libdc read).

Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
2017-07-19 11:12:35 +09:00
Jan Mulder
8911281593 OSTC over BLE: read a long as needed
See also b409e9fc91 and 709c1df2af. The OSTC parser
cannot handle reads of single 20 byte BLE packages in serial mode.
Instead of doing a deeper down agressive read, we can read on
the serial level more subtile. As the parser is requesting a
specific number of bytes, we just read that number of bytes and
return them. As the 20 byte BLE packets do (obviously) not
align with the reading requirement of the libdc parser, a little
housekeeing needs to be done in between individual reads.

CAVEAT 1: In contradiction to 709c1df2af, this is supposed to
work for all parsers that properly specify the needed bytes to fetch.

CAVEAT 2: All above tested on Linux Desktop with bluez stack.
Subsurface mobile is step 2.

Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
2017-07-11 13:41:21 +02:00
Dirk Hohndel
ea8e3006db Whitespace
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-27 20:53:21 -07:00
Dirk Hohndel
f98fa50c39 BLE code: address some compiler warnings
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-27 20:53:11 -07:00
Linus Torvalds
d01b7bf891 Switch over to SSRF_CUSTOM_IO v2
I hate changing the IO interfaces this often, but when I converted the
custom serial interface to the more generic custom IO interface, I
intentionally left the legacy serial operations alone, because I didn't
want to change something I didn't care about.

But it turns out that leaving them with the old calling convention
caused extra problems when converting the bluetooth serial code to have
the BLE GATT packet fall-back, which requires mixing two kinds of
operations.

Also, the packet_open() routine was passed a copy of the 'dc_context_t',
which makes it possible to update the 'dc_custom_io_t' field on the fly
at open time.  That makes a lot of chaining operations much simpler,
since now you can chain the 'custom_io_t' at open time and then
libdivecomputer will automatically call the new routines instead of the
old ones.

That dc_context_t availability gets rid of all the

	if (device && device->ops)
		return device->ops->serial_xyz(..);

hackery inside the rfcomm routines - now we can just at open time do a simple

	dc_context_set_custom_io(context, &ble_serial_ops);

to switch things over to the BLE version of the serial code instead.

Finally, SSRF_CUSTOM_IO v2 added an opaque "dc_user_device_t" pointer
argument to the custom_io descriptor, which gets filled in as the
custom_io is registered with the download context.  Note that unlike
most opaque pointers, this one is opaque to *libdivecomputer*, and the
type is supposed to be supplied by the user.

We define the "dc_user_device_t" as our old "struct device_data_t",
making it "struct user_device_t" instead.  That means that the IO
routines now get passed the device info showing what device they are
supposed to download for.

That, in turn, means that now our BLE GATT open code can take the device
type it opens for into account if it wants to.  And it will want to,
since the rules for Shearwater are different from the rules for Suunto,
for example.

NOTE! Because of the interface change with libdivecomputer, this will
need a flag-day again where libdivecomputer and subsurface are updated
together. It may not be the last time, either.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-06-27 13:58:15 -07:00
Linus Torvalds
4f3a9cdb35 BT serial: recognize LE-only devices, and fall back to emulated serial
This is somewhat hacky, but it allows at least the Shearwater
libdivecomputer backend to continue to treat even the BLE GATT model as
just a serial protocol.

What it does is create a special "emulate serial behavior over the
packetized BLE protocol" helper layer, that qtserialbluetooth falls back
on when rfcomm is not available.

NOTE! This still requires some BLE packet code changes to work with the
odd way that Shearwater sets up their BLE GATT communication.  So note
that no further patches are necessary to *libdivecomputer*, but some
updates are needed for the subsurface qt-ble.cpp code.

I have those updates in my tree, and this code is all tested on my
Perdix AI, but those patches are currently too ugly to commit as-is.
I've cleaned up this "fake serial" code sufficiently, that cleanup comes
next.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-26 22:21:14 -07:00
Linus Torvalds
196adb591b Very early and likely quite broken BLE GATT code
This is some very early and hacky code to be able to access BLE-enabled
dive computers that use the GATT protocol to send packets back and forth
(which seems to be pretty much all of them: a vendor-specific GATT
service with a write characteristic and a notification characteristic
for reading).

For testing only.  But it does successfully let me download dives from
my EON Steel and my Scubapro G2.

NOTE! There are several very hacky pieces in here, including just
"knowing" that the write characteristic is the first one, and the
notification characteristic is second.  The code should actually check
the properties rather than have those kinds of hardcoded assumptions.

It also checks "vendor specific" by looking at the UUID string
representation, and knowing that the standard ones start with zero.
Crazily, there doesn't seem to be any normal way to test for this,
although I guess that maybe the uuid.minimumSize() function could be
used.

There are other nasty corners. Don't complain, send me patches.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-24 21:58:01 -07:00
Linus Torvalds
add253ca9e Convert to new libdivecomputer custom IO model
Instead of being "custom serial", it's a IO model that allows serial or
packet modes, independently of each other (ie you can have a bluetooth
device that does serial over BT rfcomm and packet-based communication
over BLE GATT with the same serial operations that describe both cases).

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-06-22 08:43:47 -07:00
Dirk Hohndel
02d3289bbf Android BT download: hardcode UUID
While it seemed logical to use the advertized service UUID that doesn't
appear to be working - instead using this hard coded UUID seems to do
the trick. I now did a successful download from my Shearwater Petrel.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-05-31 22:16:25 -07:00
Dirk Hohndel
4ed6ae0dc7 Android BT: use NoSecurity for connection
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-05-31 11:43:18 -07:00
Dirk Hohndel
286bac6d30 Android: connect to BT via uuid instead of port
We remember the offered service uuids as we detect the device and then
try the first one - likely this needs to be fixed / tuned to pick the
right one if multiple uuids are offered.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-05-31 10:15:40 -07:00
Dirk Hohndel
b368ecd5aa Add SPDX header to remaining core files
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-04-29 13:32:55 -07:00
Robert C. Helling
5c98cf35d7 Change the Bluetooth connect call signature
By copying a line from the Linux bluetooth code I can download
from OSTC dive computers on Mac. Don't ask me why this works.

Signed-off-by: Robert C. Helling <helling@atdotde.de>
2017-04-28 17:55:22 -07:00
Lubomir I. Ivanov
b2c13f4178 qtserialbluetooth.cpp: mark qt_serial_get_transmitted() as unused
The function is unused, to silence the warning add the "unused"
GCC attribute to the function declaration.

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-03-12 09:19:08 -07:00
Dirk Hohndel
3ea15febfa Fix compile error on Windows
We need to return a DC_STATUS here.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2016-09-18 07:41:55 -07:00
Anton Lundin
ffa3c48593 Rewrite libdivecomputer custom serial code
This rewrites the custom serial code to use the new api which I
implemented in the Subsurface-branch of libdivecomputer.

This is a bit to big patch but I haven't had the time to break it down
into more sensible patches.

This rewrite enables us to support more ftdi based divecomputer
communication and is tested with both a OSTC3, OSTC2N and a Suunto
Vyper, all over the libftdi driver.

The bluetooth code paths are tested to, and should work as before.

Signed-off-by: Anton Lundin <glance@acc.umu.se>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2016-09-17 15:47:37 -07:00
Dirk Hohndel
7be962bfc2 Move subsurface-core to core and qt-mobile to mobile-widgets
Having subsurface-core as a directory name really messes with
autocomplete and is obviously redundant. Simmilarly, qt-mobile caused an
autocomplete conflict and also was inconsistent with the desktop-widget
name for the directory containing the "other" UI.

And while cleaning up the resulting change in the path name for include
files, I decided to clean up those even more to make them consistent
overall.

This could have been handled in more commits, but since this requires a
make clean before the build, it seemed more sensible to do it all in one.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2016-04-04 22:33:58 -07:00
Renamed from subsurface-core/qtserialbluetooth.cpp (Browse further)