summaryrefslogtreecommitdiffstats
path: root/core/divelist.c
AgeCommit message (Collapse)Author
2021-08-19Planner: Correctly compute CNS and OTU for bailout segmentsGravatar Robert C. Helling
For dives with mixed divemode, one needs to check sample.setpoint to figure out if the segment is an OC segment and the po2 needs to be computed from the gasmix and ambient pressure. This fixes #3310 Signed-off-by: Robert C. Helling <helling@atdotde.de>
2021-08-18Clean up divecomputer 'device' handlingGravatar Linus Torvalds
We have this odd legacy notion of a divecomputer 'device', that was originally just basically the libdivecomputer 'EVENT_DEVINFO' report that was associated with each dive. So it had firmware version, deviceid, and serial number. It had also gotten extended to do 'nickname' handling, and it was all confusing, ugly and bad. It was particularly bad because it wasn't actually a 'per device' thing at all: due to the firmware field, a dive computer that got a firmware update forced a new 'device'. To make matters worse, the 'deviceid' was also almost random, because we've calculated it a couple of different ways, and libdivecomputer itself has changed how the legacy 32-bit 'serial number' is expressed. Finally, because of all these issues, we didn't even try to make the thing unique, so it really ended up being a random snapshot of the state of the dive computer at the time of a dive, and sometimes we'd pick one, and sometimes another, since they weren't really well-defined. So get rid of all this confusion. The new rules: - the actual random dive computer state at the time of a dive is kept in the dive data. So if you want to know the firmware version, it should be in the 'extra data' - the only serial number that matters is the string one in the extra data, because that's the one that actually matches what the dive computer reports, and isn't some random 32-bit integer with ambiguous formatting. - the 'device id' - the thing we match with (together with the model name, eg "Suunto EON Steel") is purely a hash of the real serial number. The device ID that libdivecomputer reports in EVENT_DEVINFO is ignored, as is the device ID we've saved in the XML or git files. If we have a serial number, the device ID will be uniquely associated with that serial number, and if we don't have one, the device ID will be zero (for 'match anything'). So now 'deviceid' is literally just a shorthand for the serial number string, and the two are joined at the hip. - the 'device' managament is _only_ used to track devices that have serial numbers _and_ nicknames. So no more different device structures just because one had a nickname and the other didn't etc. Without a serial number, the device is 'anonymous' and fundamentally cannot be distinguished from other devices of the same model, so a nickname is meaningless. And without a nickname, there is no point in creating a device data structure, since all the data is in the dive itself and the device structure wouldn't add any value.. These rules mean that we no longer have ambiguous 'device' structures, and we can never have duplicates that can confuse us. This does mean that you can't give a nickname to a device that cannot be uniquely identified with a serial number, but those are happily fairly rare (and mostly older ones). Dirk said he'd look at what it takes to give more dive computers proper serial numbers, and I already did it for the Garmin Descent family yesterday. (Honesty in advertizing: right now you can't add a nickname to a dive computer that doesn't already have one, because such a dive computer will not have a device structure. But that's a UI issue, and I'll sort that out separately) Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2021-03-31core: initialize dive selection after resetting the dataGravatar Berthold Stoeger
The dive selection was initialized during data-reset. However, this emitted a signal before all data-reset routines were run. Ultimately, this led to access-after-free in the statistics code. Instead, move the select_newest_visible_dive() signal from the divelist-model to the process_loaded_dives() function. There is no point in initializing the selection if the dive data is cleared after all. This change broke closing of the log, because the UI-selection was not reset. Therefore, when clearing the data, clear the selection before proceeding with clearing. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-03-12Use PSCR gas when computing O2 toxicityGravatar Robert C. Helling
Both the calculations for CNS and OTU did not take into account the pO2 drop when using a PSCR. Furthermore, there was some unit confusion due to not using internal units. Reported-by: arosl Signed-off-by: Robert C. Helling <helling@atdotde.de>
2021-02-26Get O2 right in bailout modeGravatar Robert C. Helling
When doing OC bailout from a CCR dive, there could still be pO2 sensor readings but those are not valid. This fixes a problem noticed by Justin Ashworth. Signed-off-by: Robert C. Helling <helling@atdotde.de>
2021-02-26Fix pO2 for CCR at shallow depthsGravatar Robert C. Helling
Even when diving a CCR, the pO2 cannot exceed ambient pressure. This only makes a difference at shallow depths. Fix this in the calculation of OTUs and CNS. This affects some tests that now have slightly different CNS and OTU values. Suggested-by: Justin Ashworth Signed-off-by: Robert C. Helling <helling@atdotde.de>
2021-02-17planner: pass in_planner argument to decoMode()Gravatar Berthold Stoeger
To remove reliance on global state, pass an "in_planner" argument to decoMode(). Thus, calls to in_planner() can be removed. This is a more-or-less automated change. Ultimately it would probably be better to pass the current deco-mode to the affected functions instead of calling decoMode() with an in_planner parameter. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-02-17planner: pass in_planner argument to clear_deco()Gravatar Berthold Stoeger
To remove reliance on global state, pass an "in_planner" argument to clear_deco(). Thus, calls to in_planner() can be removed. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-02-17planner: pass in_planner argument to add_segment()Gravatar Berthold Stoeger
To remove reliance on global state, pass an "in_planner" argument to add_segment(). Thus, calls to in_planner() can be removed. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-20cleanup: constify init_decompression()Gravatar Berthold Stoeger
This function initializes decompression data from a dive. The dive is not modified, therefore make it const. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-12-29filter: move shown_dive from divelist.c to divefilter.cppGravatar Berthold Stoeger
Arguably, the number of filtered dives is a matter of the divefilter. Let's move it there. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-12-29filter: reset shown_dives in filterGravatar Berthold Stoeger
The shown_dives variable was reset by the dive_list code. Arguably, the filter should keep track of the number of shown dives, so move the resetting there. This means adding a new "reset()" member function to the filter and call that instead of "updateAll()" when the core data is reset. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-12-13core: keep tank infos in a dynamic tableGravatar Berthold Stoeger
The list of known tank types were kept in a fixed size table. Instead, use a dynamic table with our horrendous table macros. This is more flexible and sensible. While doing this, clean up the TankInfoModel, which was leaking memory. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-31core: on import remove merged dives from trip/divesiteGravatar Berthold Stoeger
When dives were merged on import, they were not unregistered from their dive site and trip before being deleted. Thus, these tables had stale pointers, which would ultimate lead to crashes. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-25cleanup: move interpolate inline function to its own header fileGravatar Berthold Stoeger
This is not really related to struct dive, so let's move it. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-25cleanup: split out divecomputer functions from dive.cGravatar Berthold Stoeger
Since dive.c is so huge, split out divecomputer-related functions into divecomputer.[c|h], sample.[c|h] and extradata.[c|h]. This does not give huge compile time improvements, since struct dive contains a struct divecomputer and therefore dive.h has to include divecomputer.h. However, it make things distinctly more clear. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-25cleanup: break out event-related code into event.[c|h]Gravatar Berthold Stoeger
In an effort to reduce the size of dive.h and dive.c, break out the event related functions. Moreover event-names were handled by the profile-code, collect that also in the new source files. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-25cleanup: move mark_divelist_changed() to qmlmanager.cppGravatar Berthold Stoeger
Desktop does not use mark_divelist_changed() anymore - all is done via the undo machinery. Therefore move this function (and its counterpart unsaved_changes()) to qmlmanager.cpp. Ultimately, it probably should be removed from there as well, but currently I don't dare to touch all the cloud-logic! Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-24cleanup: rename set_dc_nickname() to add_devices_of_dive()Gravatar Berthold Stoeger
The function was misnamed in that it doesn't set the nickname of a device. Instead, it adds all (unknown) devices of a dive to the/a device-table. Let's call it appropriately. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-24devices: add devices in Command::importTable()Gravatar Berthold Stoeger
Add a device_table parameters to Command::importTable() and add_imported_dives(). The content of this table will be added to the global device list (respectively removed on undo). This is currently a no-op, as the parser doesn't yet fill out the device table, but adds devices directly to the global device table. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-17cleanup: rename clear_device_nodes() to clear_device_table()Gravatar Berthold Stoeger
For consistency with all the other clear_*_table functions (dive, trip, dive_site, ...) Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-16core: add device_table parameter to device table functionsGravatar Berthold Stoeger
Instead of accessing the global device table directly, add a parameter to all device-table accessing functions. This makes all places in the code that access the global device table grep-able, which is necessary to include the device-table code in the undo system. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-09-29filter: add primitive filter presetsGravatar Berthold Stoeger
Add a rudimentary list of filter presets to the core. The list is sorted by name. Access is provided via a C interface so that the presets can be written to the git and XML logs. Internally, the list is realized by a C++ vector for convenience (euphemism for laziness). Morover, a C++ interface is provided for the UI. Currently names of the presets cannot be edited, since this would mean that the order of the list changes. This may be implemented later if required. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-08-17core: fix dive renumbering logic on importGravatar Berthold Stoeger
0249e12 split up the dive import logic in multiple steps. Thereby, the one of the conditions for renumbering the imported dives (is the last old dive numbered) got messed up: The first number of the new dive was compared to the total number of old dives, which makes no sense. - Simply check for the number of the last existing dive (if any). - Don't remember the number of old dives - the original table is not modified anyway. Fixes #2731 Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-07cleanup: invert control-flow when resetting the core structuresGravatar Berthold Stoeger
To reset the core data structures, the mobile and desktop UIs were calling into the dive-list models, which then reset the core data structures, themselves and the unrelated locationinformation model. The UI code then reset various other things, such as the TankInformation model or the map. . This was unsatisfying from a control-flow perspective, as the models should display the core data, not act on it. Moreover, this meant lots of intricate intermodule-dependencies. Thus, straighten up the control flow: give the C core the possibility to send a "all data reset" event. And do that in those functions that reset the core data structures. Let each module react to this event by itself. This removes inter-module dependencies. For example, the MainWindow now doesn't have to reset the TankInfoModel or the MapWidget. Then, to reset the core data structures, let the UI code simply directly call the respective core functions. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-03selection: introduce clear_selection() functionGravatar Berthold Stoeger
The DiveListView would touch the selection-innards directly. Let's encapsulate that. Moreover, take care to reset the trip selection when resetting the core data. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-02cleanup: remove unused function append_dive()Gravatar Berthold Stoeger
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-01cleanup: move dive_table from dive.h to divelist.hGravatar Berthold Stoeger
This allows us to decouple dive.h and divelist.h, a small step in include disentangling. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-24cleanup: move clearing of event names to clear_dive_file_data()Gravatar Berthold Stoeger
Move this to the core so that desktop and mobile don't have to call this explicitly. Matter of fact, mobile didn't call this. It is unclear, whether that was even used on mobile, though. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-24cleanup: move clearing of device nodes to clear_dive_file_data()Gravatar Berthold Stoeger
It makes no sense to keep the device nodes if all the other data is cleared. Let's do this automatically and not explicitly. This ensures that the function is also called on mobile. Currently it was only called on desktop. Weirdly, the parser-tests were expecting that the device nodes were not reset by clear_dive_file_data() and therefore divecomputers were accumulating in the test results. Thus, the additional computers had to be removed from the expected test results. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-13divelist: when removing/adding a dive unregister/register fulltextGravatar Berthold Stoeger
This fixes a crash: when the undo commands removed a dive from the list, the fulltext cache was not cleared. If now the divelist is reset and then the undo-command deleted, deletion of the owned dive tries to remove it's fulltext cache, which doesn't exist anymore. For reasons of symmetry, when readding the dive, its fulltext has to be registered. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-04-13Remove extra argument and add a testGravatar Robert C. Helling
The compiler complained about this and it seems the function does not need it. Additional-test-suggested-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at> Signed-off-by: Robert C. Helling <helling@atdotde.de>
2020-04-08fulltext: rename fulltext_reload() to fulltext_populate()Gravatar Berthold Stoeger
This function was named improperly: it was only used on freshly loaded data. Indeed, attempts to use it to actually reload lead to crashes. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-03-20Core: consider invalid flag when adding divesGravatar Berthold Stoeger
Adding dives uses the number of the last dive to create a new dive number. Ignore invalid dives. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-03-01filter: compile fulltext index on mobileGravatar Berthold Stoeger
The code is not used yet. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-03-01filter: index/unindex dives on loading or clearing logsGravatar Berthold Stoeger
When loading dive data, populate the fulltext index. When clearing dive data, free the fulltext index. When deleting a dive, remove it from the fulltext index. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-01-10code cleanup: add empty table structuresGravatar Dirk Hohndel
It seemed to make sense to combine all three types in one commit. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2019-12-04Cleanup: remove superfluous includes from core/divelist.cGravatar Berthold Stoeger
Most of these haven't been used in a long time. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-12-04Selection: move selection functions from divelist.c to selection.cGravatar Berthold Stoeger
Since we now have a selection.c translation unit, put the selection- related functions there. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-11-19Filter: move num_shown logic from model to coreGravatar Berthold Stoeger
Since the number of shown dives is stored in the core, let's also keep it updated there. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-11-19Filter: move number of shown dives to coreGravatar Berthold Stoeger
We mark hidden/shown dives in the core but store the number of shown dives in the MultiFilterSortModel. Move this datum to the core for improved locality. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-11-16Cleanup: move clearing of current_dive into clear_dive_file_data()Gravatar Berthold Stoeger
It makes no sense to have a non-NULL current_dive once all dives have been deleted. Therefore, clear current_dive implicitly in clear_dive_file_data() and don't depend on the caller performing this. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-11-09Cylinders: access cylinders with get_cylinder()Gravatar Berthold Stoeger
Instead of accessing the cylinder table directly, use the get_cylinder() function. This gives less unwieldy expressions. But more importantly, the function does bound checking. This is crucial for now as the code hasn't be properly audited since the change to arbitrarily sized cylinder tables. Accesses of invalid cylinder indexes may lead to silent data-corruption that is sometimes not even noticed by valgrind. Returning NULL instead of an invalid pointer will make debugging much easier. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-11-09Core: remove MAX_CYLINDERS restrictionGravatar Berthold Stoeger
Instead of using fixed size arrays, use a new cylinder_table structure. The code copies the weightsystem code, but is significantly more complex because cylinders are such an integral part of the core. Two functions to access the cylinders were added: get_cylinder() and get_or_create_cylinder() The former does a simple array access and supposes that the cylinder exists. The latter is used by the parser(s) and if a cylinder with the given id does not exist, cylinders up to that id are generated. One point will make C programmers cringe: the cylinder structure is passed by value. This is due to the way the table-macros work. A refactoring of the table macros is planned. It has to be noted that the size of a cylinder_t is 64 bytes, i.e. 8 long words on a 64-bit architecture, so passing on the stack is probably not even significantly slower than passing as reference. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-10-28Cleanup: avoid dereferencing NULLGravatar Dirk Hohndel
We should call this function with two well defined dive_or_trip structures which means that exactly one of the two values is set in each argument. But in order to not have bugs elsewhere leed to crashes here, be more tolerant of malformed argumnts. Fixes CID 350100 Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2019-10-28Cleanup: prevent NULL dereferenceGravatar Dirk Hohndel
This should never happen based on the logic in the callers, but just to be on the safe side. Should fix CID 350128 Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2019-10-02Core: implement move functions for dive and dive_site tableGravatar Berthold Stoeger
To allow efficient moving of downloaded dives from the download thread to the model, implement a general move function that moves table data. Instantiate that function for the dive and dive_site tables. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-08-28Fix new CNS calculation - remove error with factor of 10Gravatar Stefan Fuchs
Fix a typo in the new CNS calculation which introduced a error of factor 10. Signed-off-by: Stefan Fuchs <sfuchs@gmx.de>
2019-08-20Replace table interpolation by two line fit for CNSGravatar Robert C. Helling
We used a table lookup for CNS equivalent times. Turns out the log of this table falls pretty much on a straight line for po2 <= 1.5bar. We now fit this tabel two two lines, one for <= 1.5 bar and one above. This four parameter fit has half the sum of errors squared than the five parameter fit using a fourth order polynomial. Fitting the log has the advantage that this never crosses 0, which would have the bad effect of resulting in negative CNS values as we divide by the table value. We don't adopt a maximum pO2 cut-off for the CNS calculation but rather live with the large values that the interpolation formula produces when extrapolating. Signed-off-by: Robert C. Helling <helling@atdotde.de>
2019-08-04Cleanup: use clear_git_id() instead of setting saved_git_idGravatar Berthold Stoeger
For better encapsulation, use clear_git_id() in clear_dive_file_data() instead of setting saved_git_id directly. Thus, memory management of the saved_git_id value is encapsulated and can be modified more easily. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>