summaryrefslogtreecommitdiffstats
path: root/core/dive.c
AgeCommit message (Collapse)Author
2018-10-30Fix gasmix for OTU calculationGravatar Robert C. Helling
With the fixed sematics of get_gasmix to return the new gasmix for the time of a gas switch (added comments to make this clear), in the OTU calculation we need the previous gasmix for the interval up to the current time. Signed-off-by: Robert C. Helling <helling@atdotde.de>
2018-10-30Fixed get_gas_at_time for equal timesGravatar Robert C. Helling
This fixes a subtle bug introduced in 5c4569247a31c which unified two functions finding the gasmix at a given time during the dive. There was a slight difference, though: Does a gaschange exactly at that time count or not? For the planner to work, the answer has to be in the affirmative. Signed-off-by: Robert C. Helling <helling@atdotde.de>
2018-10-29Dive site: replace dive->dive_site_uuid by dive_siteGravatar Berthold Stoeger
Replace the UUID reference of struct dive by a pointer to dive_site. This commit is rather large in lines, but nevertheless quite simple since most of the UUID->pointer work was done in previous commits. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-29Dive site: return pointer to dive_site in create_dive_site_*()Gravatar Berthold Stoeger
This changes more of the dive-site interface to return pointers instead of UUIDs. Currently, most call sites directly extract UUIDs afterwards. Ultimately, the UUIDs will be generally replaced by pointers, which will then simplify these callers. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-23Core: export free_picture()Gravatar Berthold Stoeger
Export the free_picture() function from dive.c. The parser may need this in case of truncated files to free its temporary resources. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-21Add 'location_t' data structureGravatar Linus Torvalds
Instead of having people treat latitude and longitude as separate things, just add a 'location_t' data structure that contains both. Almost all cases want to always act on them together. This is really just prep-work for adding a few more locations that we track: I want to add a entry/exit location to each dive (independent of the dive site) because of how the Garmin Descent gives us the information (and hopefully, some day, other dive computers too). Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-10-16Core: use free_dc_contents() in delete_current_divecomputer()Gravatar Berthold Stoeger
delete_current_divecomputer() had some duplicate code to release dive-computer resources. Use the free_dc_contents() function instead. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-16Core: copy / free serial and fw_version items of struct divecomputerGravatar Berthold Stoeger
The serial and fw_version strings of struct divecomputer were copied by pointer. This worked because they were never freed or modified. Instead, do a deep copy of the strings and free them when appropriate. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-16Core: copy and free extra_data of dive computer structureGravatar Berthold Stoeger
On dive computer copy, the extra data (a list of key/value pairs) was simply copied as a pointer. This worked because the list was never freed nor modified. Copy and free the list on dive computer copy and free, respectively. This made it necessary to move the STRUCTURE_LIST_* macros up in the dive.c file. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-15Core: fix broken commit e5dca8228eGravatar Berthold Stoeger
In commit e5dca8228e6b60cac5957726700c66d0565c064e a fixed order of the arguments to merge_dives() was introduced: first dive old, second dive downloaded. This made the dl variable, which pointed to the downloaded dive useless. One instance was forgotten, which led to a null-dereference. Remove. Reported-by: Dirk Hohndel <dirk@hohndel.org> Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2018-10-15Cleanup: remove apparently dead code in sanitize_cylinder_type()Gravatar Berthold Stoeger
sanitize_cylinder_type(), which is indirectly called from fixup_dive(), had ft^3 -> mliter conversion code, which was executed on the condition "xml_parsing_units.volume == CUFT". But nowhere in the code base would xml_parsing_units.volume ever be set to non-metric. Moreover, xml_parsing_units reflects the units of the latest parsed XML file, but fixup_dive() is called in numerous contexts not related to XML parsing. Therefore, the whole piece of code seems highly questionable. Remove this code. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-15Core: copy whole list in copy_dc_renumber()Gravatar Berthold Stoeger
copy_dc_renumber() is an internal function to copy dive computers and renumber the cylinders. Since only the structure was copied, in the case of multi-dc dives, the merged dives shared the same computer. If one of them was freed, use-after-free crashes would happen. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-15Dive list: make deep copy of picture list on mergeGravatar Berthold Stoeger
On merging make a deep copy of the picture list, to avoid a use-after-free crash after the orginal dive is deleted. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-15Dive list: fix crash on dive importGravatar Berthold Stoeger
In commit 8c2383b4952fa22d41745d29484462ed6a67112b dive merging was changed to not modify the original dive. On import, dives were then merged and the original deleted. The merge_weightsystem_info() was not adapted accordingly (deep copy of string instead of pointer), leading to a use-after-free crash. Resolve this by doing a deep copy. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-13Dive site: un-globalize displayed_dive_siteGravatar Berthold Stoeger
The global object displayed_dive_site is used a a backing-store by the dive-site-edit widget. All external accesses were removed, therefore make the object local to the widget. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-11Undo: don't modify source-dives on mergeGravatar Berthold Stoeger
For undo, it is crucial that commands don't modify existing dives. Unfortunately, dive merging would write into the data-structures of the to-be-merged dives. To prevent it from doing so, make the input dives const-pointers. This led to a whole cascade of functions that had to take const and significant churn. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-11Undo: implement rudimentary support for undo of dive-mergingGravatar Berthold Stoeger
For this, an output-parameter was added to the backend merge_dives() function. When non-zero, instead of adding the merged dive to the preferred trip, the preferred trip is returned to the caller. Since the new UndoObject, just like the delete-dives UndoObject, needs to remove/readd a set of dives, the corresponding functionality was split-off in a helper function. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-11Undo: implement rudimentary support for undo of dive-splittingGravatar Berthold Stoeger
For this, the core functionality of the split_dive() and split_dive_at_time() functions were split out into new split_dive_dont_insert() and split_dive_at_time_dont_insert(), which do not add the new dives to the log. Thus, the undo-command can take ownership of these dives, without having to remove them first. The split-dive functionality is temporarily made desktop-only until mobile also supports "UndoObjects". Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-10-06Import: remove dive->downloaded logicGravatar Berthold Stoeger
Dive importing is now performed via a distinct table which is merged into the main dive table. Thus, it is known which of the dive is new and which is old. This information can now be implicitely encoded in the parameter-position of merge_dive() [i.e. pass old as first and new as second dive]. This makes marking of downloaded dives via a flag unnecessary. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-09-29Cleanup: split out free_dive() function from delete_single_dive()Gravatar Berthold Stoeger
Currently, we can only delete dives that are indexed in the main dive table. In the future, we will have to delete dives outside of this table (e.g. for undo). Therefore, split out the free_dive() function from delete_single_dive(), which takes an index into the main dive table. In the process, adopt the dive freeing-code from clear_dive(), which frees more data than the code in delete_single_dive(). This potentially fixes a memory-leak. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-09-19Dive media: on import read metadata only onceGravatar Berthold Stoeger
On import of dive media, the timestamp is read from the metadata to check if the image belongs to the selected dives. The pictures are then listed in a dialog. Currently, the metadata is read twice if images are outside of a dive: once in picture_check_valid() and if it turns out that the picture is not valid again in picture_get_time() to display the proper timestamp. Even though metadata-extraction is reasonably fast, this is a bit of an embarrassment. Instead, read the timestamps only once in the constructor of the dialog and from then on only used these timestamps. Keep the timestamps in a QVector. Rename the picture_check_valid() function to picture_check_valid_time() and pass a timestamp instead of a filename. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-09-19Cleanup: make surface sample in merge_one_sample() non-staticGravatar Berthold Stoeger
The merge_one_sample() function adds a sample to the destination dive if dives are merged. For long periods between samples at surface depths, it adds a surface interval. To decrease the number of global objects, make the sample structure non-static. Of course, initialization of an on-stack structure is slower. Therefore move it into the corresponding if. Thus, the structure will be initialized only once per surface-interval. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-09-11cleanup[2/6]: remove set_userid()Gravatar Jan Mulder
This code is not used any more. Signed-off-by: Jan Mlder <jlmulder@xs4all.nl>
2018-09-10Profile: fix SAC calculation for air divesGravatar Berthold Stoeger
Commit f5b11daffd6f240268ce78d72c64be43670988ea changed gasmix arguments and return values to be passed by value instead of using pointers. Notably, get_gasmix() is fed a default-value and returns a new value. In the old code, NULL was passed in in a first loop iteration and non-NULL was always returned in the first iteration. Thus, an equality comparison of passed-in an returned gasmix would always fail in the first loop iteration. The new code passed in air as default. Now if air was also returned, then the matching gases were not calculated in calculate_sac(). To revert to the old behavior, pass in an invalid gasmix. Moreover, give names to the invalid and air gasmixes. Reported-by: tormento <turment@gmail.com> Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-09-07Cleanup: constify get_units()Gravatar Berthold Stoeger
get_units() returns a pointer to the units struct in the preferences. Callers should not modify the preferences via this struct, therefore make the return value point to const. This is a small step in constifying the global preferences structure. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-25Merge branch 'filter8' of https://github.com/bstoeger/subsurfaceGravatar Dirk Hohndel
2018-08-23Cleanup: const-ify functions taking dives and divecomputersGravatar Berthold Stoeger
Another small step in making things const-clean. See also commit 605e1e19ed0c52a16580c95e36ae79e71b539351. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-23Cleanup: const-ify functions taking pointers to eventsGravatar Berthold Stoeger
This is another entry in the series to make more things "const-clean" with the ultimate goal of merge_dive() take const pointers. This concerns functions taking pointers to events and the fallout from making these const. The somewhat debatable part of this commit might be that get_next_event() is split in a two distinct (const and non-const) versions with different names, since C doesn't allow overloading. The linker should recognize that these functions are identical and remove one of them. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-23Cleanup: const-ify parameters to trivial accessor functionsGravatar Berthold Stoeger
Accessor-functions without noticeable logic, such as depth_to_bar() can trivially be made "const-clean". Moreover, let get_dive_location() return a "const char *". The non-const version must have been an oversight, as the caller must not free() or overwrite the string. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-23Cleanup: pass gasmix by valueGravatar Berthold Stoeger
In a previous commit, the get_gasmix_* functions were changed to return by value. For consistency, also pass gasmix by value. Note that on common 64-bit platforms struct gasmix is the size of a pointer [2 * 32 bit vs. 64 bit] and therefore uses the same space on the stack. On 32-bit platforms, the stack use is probably doubled, but in return a dereference is avoided. Supporting arbitrary gas-mixes (H2, Ar, ...) will be such an invasive change that going back to pointers is probably the least of our worries. This commit is a step in const-ifying input parameters (passing by value is the ultimate way of signaling that the input parameter will not be changed [unless there are references to said parameter]). Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-23Cleanup: unify get_gas_at_time() and get_gasmix()Gravatar Berthold Stoeger
There were two functions for getting gas-mixes at a certain timestamp: - get_gasmix() for repeated queries. - get_gas_at_time() for a single query. Since the latter is a special case of the former, simply call the former in the latter. Moreover, rename to get_gasmix_at_time() for consistency. Replace on get_gasmix() call, which was outside of a loop by the corresponding get_gasmix_at_time() call. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-23Cleanup: return gasmix by valueGravatar Berthold Stoeger
Currently, get_gasmix_from_event() and get_gasmix() return pointers to either static or to (possibly changing) dive data. This seems like a dangerous practice and the returned data should be used immediately. Instead, return the gasmix by value. This is in preparation of const-ifying input parameters of a number of core functions, which will ultimately let the merge() function take const-arguments in preparation of undo of dive-merging. On common 64-bit systems gasmix (two "int"s) is the size of a pointer and can be returned in a register. On 32-bit systems a pointer to the struct to be filled out will be passed. Since get_gasmix() now returns a value, the first invocation is tested by a NULL-initialized "struct event *". Document this in a comment. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-14Filter: constify doFilter() argumentGravatar Berthold Stoeger
Conceptually, the doFilter() functions shouldn't modify the dive they test. Therefore, make the argument const. To do this, constify the parameter of get_dive_location(), which likewise seems to be the right thing to do. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-21Uninline functions in dive.hGravatar Berthold Stoeger
There were numerous inlined functions in dive.h. For many of them inlining is dubious. Let's uninline most of them, with the exception of trivial accessors and interpolate(). On current master, this gave a size reduction of 5 pages: -rwxrwxr-x 1 bs bs 5863656 Jul 18 20:57 subsurface-inline -rwxrwxr-x 1 bs bs 5843176 Jul 18 20:48 subsurface-noinline ----------------------------------------------------------- 20480 Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-17Cleanup: simplify dive_getUniqID()Gravatar Berthold Stoeger
dive_getUniqID() is used to create unique dive ids, which are stable during application lifetime. It was passed a dive, checked that the id was not set (if it was that it is know to the application) and set a new id (in contradiction to its name!) if it hadn't any. There were three callers: alloc_dive(): called the function on a zeroed dive struct. fixup_dive(): called the function only if the dive had a 0 id. MainWindow::setupForAddAndPlan(): called the function on a zeroed dive struct. Thus, in all three callers the id is guaranteed to be zero and the whole keeping-track-of-ids logic is moot. Remove the logic, don't pass a dive struct to dive_getUniqID() and move the function to the C-backend. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-03Allow to split dives with more than one dive computerGravatar Robert C. Helling
Only the first computer is taken into account to find surface intervals. All further dive computers are split according to time. Signed-off-by: Robert C. Helling <helling@atdotde.de>
2018-07-03Profile context menu entry to split a diveGravatar Robert C. Helling
Allow the user to manually split a dive in two. Signed-off-by: Robert C. Helling <helling@atdotde.de>
2018-07-03Include cylinder pressures upon force_fixup()Gravatar Robert C. Helling
Try to recompute cylinder start and end pressures from sample data. Signed-off-by: Robert C. Helling <helling@atdotde.de>
2018-07-02Use (and update) dive computer times when merging and splitting divesGravatar Linus Torvalds
When we split a dive in two, we keep the dive computer ID for the dive, but we should update the actual _time_ of the split dive to match the split. And when we look for "are these the exact same dives", we should check not only that the dive computer dive ID matches, but also that the dive computer time matches, so that we don't consider two parts of a dive that has been split to be obviously the same dive. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2018-07-02Use proper sample pointer when deciding to split divesGravatar Linus Torvalds
The dive splitting was completely wrong, because we checked the time of the previous sample by doing sample[i - 1].time.seconds which is entirely wrong. The 'sample' variable is the *current* sample, so the time of the previous sample is simply sample[-1].time.seconds Alternatively, we could have started from the first sample, and done dc->sample[i - 1].time.seconds but mixing the two concepts up just gets you a random sample pointer that is likely not a valid sample at all, and obviously does not have the right time at all. As a result, dive splitting was pretty much random. Sometimes it worked purely by mistake, because the rest of the logic was right (ie we _had_ found the right point where we reached the surface in the dive etc, the "previous sample time" was simply used to decide if the surface interval was sufficient to split the dive up). Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2018-06-20core: add free_samples helperGravatar Dirk Hohndel
And use it in the UI and planner code. See #1411 Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2018-06-20core: use num in alloc_samplesGravatar jan Iversen
When num > dc->alloc_samples we whould allocate space for num Signed-off-by: Jan Iversen <jani@apache.org>
2018-06-17Fix an error around translation of dive modes in the UIGravatar Stefan Fuchs
This fixes an mistake introduced in 3d1072f8862e4c329dc2678df52a24137b8d4704 Signed-off-by: Stefan Fuchs <sfuchs@gmx.de>
2018-06-07Dive pictures: remove cache_picture() call in dive_add_picture()Gravatar Berthold Stoeger
When adding a picture to a dive, cache_picture() was called, which calculated the hash of the picture in a background-thread. This made tests occasionally fail, because the tests depended on the filename-to-localfilename being overwritten in a call running in a different thread. Depending on which thread finished first, the test succeeded or failed. The easiest way to circumvent this problem is to remove the cache_picture() call. The hash will be calculated anyway with the thumbnails. And the only function of the hash is the "find moved images" function. Which is not an issue here, because the user just loaded the images from disk. Reported-by: Jan Iversen <jani@apache.org> Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-06-06Bugfix: Load PSC dive logsGravatar Willem Ferguson
The code changes to standardise the named of divemodes and to separate internal divemode names and UI divemode names introduced a bug that caused non-backward compatability with existing dive logs. The reason for this is the definition of the divemode_text strings in dive.c This change reverses that definition and brings about correct loading of PSCR dive logs as well as correct parsing of bailout events involving PSCR. Signed-off-by: Willem Ferguson <willemferguson@zoology.up.ac.za>
2018-05-28Distinguish between user and internal divemode namesGravatar Robert C. Helling
The former should be translated but not those that go to xml/git. ... and fix capitalization of pSCR. Suggested-by: Stefan Fuchs <sfuchs@gmx.de> Signed-off-by: Robert C. Helling <helling@atdotde.de>
2018-05-14Rename enum dive_comp_type to divemode_tGravatar Robert C. Helling
...as the usuage is not anymore about a computer but a momentary dive mode. Rename the end indicator as well. Signed-off-by: Robert C. Helling <helling@atdotde.de>
2018-05-14Fix divemode detection in plannerGravatar Robert C. Helling
Signed-off-by: Robert C. Helling <helling@atdotde.de>
2018-05-14Fix bug for CCR dive bailoutGravatar Willem Ferguson
Under some conditions get_current_divemode() (in dive.c) returns an erroneous divemode. This happens when there are several events at the very beginning of the dive, as can happen in some CCR dive logs. This commit fixes that bug. Signed-off-by: Willem Ferguson <willemferguson@zoology.up.ac.za>
2018-05-14Implement get_divemode() to find the divemode at a particular timeGravatar Willem Ferguson
Replaced a rather cumbersome function that that did the above. Upon the suggestion of Robert Helling who proposed a much shorter way, this new function replaced the previous ones. This necessitated changes to divelist.c, profile.c and plannernotes.c, as well as dive.c/h. Signed-off-by: Willem Ferguson <willemferguson@zoology.up.ac.za>