2017-04-27 18:24:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-04-03 23:07:59 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-08-05 17:41:15 +00:00
|
|
|
#include "errorhelper.h"
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "subsurface-string.h"
|
2015-04-03 23:07:59 +00:00
|
|
|
#include "gettext.h"
|
2020-05-01 11:43:52 +00:00
|
|
|
#include "dive.h"
|
2020-10-25 17:29:15 +00:00
|
|
|
#include "divelist.h"
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
#include "divelog.h"
|
2020-10-25 12:28:55 +00:00
|
|
|
#include "extradata.h"
|
2019-08-05 18:07:10 +00:00
|
|
|
#include "file.h"
|
2024-05-02 07:36:00 +00:00
|
|
|
#include "format.h"
|
2015-04-03 23:07:59 +00:00
|
|
|
#include "libdivecomputer.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OSTCTools stores the raw dive data in heavily padded files, one dive
|
2015-11-15 18:15:40 +00:00
|
|
|
* each file. So it's not necessary to iterate once and again on a parsing
|
2015-04-03 23:07:59 +00:00
|
|
|
* function. Actually there's only one kind of archive for every DC model.
|
|
|
|
*/
|
2024-11-11 02:41:31 +00:00
|
|
|
int ostctools_import(const std::unique_ptr<std::vector<unsigned char>> &buffer, struct divelog *log)
|
2015-04-03 23:07:59 +00:00
|
|
|
{
|
2024-11-11 02:41:31 +00:00
|
|
|
if (buffer->size() < 456)
|
|
|
|
return report_error("%s", translate("gettextFromC", "Invalid OSTCTools file"));
|
2015-04-03 23:07:59 +00:00
|
|
|
|
|
|
|
// Read dive number from the log
|
2024-11-11 02:41:31 +00:00
|
|
|
auto ostcdive = std::make_unique<dive>();
|
|
|
|
ostcdive->number = (*buffer)[258] + ((*buffer)[259] << 8);
|
|
|
|
|
2015-04-03 23:07:59 +00:00
|
|
|
|
|
|
|
// Read device's serial number
|
2024-11-11 02:41:31 +00:00
|
|
|
unsigned int serial = (*buffer)[265] + ((*buffer)[266] << 8);
|
2015-04-03 23:07:59 +00:00
|
|
|
|
2024-11-11 02:41:31 +00:00
|
|
|
// Trim the buffer to the actual dive data
|
|
|
|
buffer->erase(buffer->begin(), buffer->begin() + 456);
|
|
|
|
unsigned int i = 0;
|
|
|
|
bool end_marker = false;
|
|
|
|
for (auto c: *buffer) {
|
2015-04-03 23:07:59 +00:00
|
|
|
i++;
|
2024-11-11 02:41:31 +00:00
|
|
|
if (c == 0xFD) {
|
|
|
|
if (end_marker)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
end_marker = true;
|
|
|
|
} else {
|
|
|
|
end_marker = false;
|
|
|
|
}
|
2015-04-03 23:07:59 +00:00
|
|
|
}
|
2024-11-11 02:41:31 +00:00
|
|
|
if (end_marker)
|
|
|
|
buffer->erase(buffer->begin() + i, buffer->end());
|
2015-04-03 23:07:59 +00:00
|
|
|
|
|
|
|
// Try to determine the dc family based on the header type
|
2024-11-11 02:41:31 +00:00
|
|
|
dc_family_t dc_fam;
|
|
|
|
if ((*buffer)[2] == 0x20 || (*buffer)[2] == 0x21) {
|
2015-06-22 04:58:10 +00:00
|
|
|
dc_fam = DC_FAMILY_HW_OSTC;
|
2015-09-10 00:26:54 +00:00
|
|
|
} else {
|
2024-11-11 02:41:31 +00:00
|
|
|
switch ((*buffer)[8]) {
|
2015-09-10 00:26:54 +00:00
|
|
|
case 0x22:
|
|
|
|
dc_fam = DC_FAMILY_HW_FROG;
|
|
|
|
break;
|
|
|
|
case 0x23:
|
2016-12-29 15:05:05 +00:00
|
|
|
case 0x24:
|
2015-09-10 00:26:54 +00:00
|
|
|
dc_fam = DC_FAMILY_HW_OSTC3;
|
|
|
|
break;
|
|
|
|
default:
|
2024-11-11 02:41:31 +00:00
|
|
|
return report_error(translate("gettextFromC", "Unknown DC in dive %d"), ostcdive->number);
|
2015-07-12 18:30:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to determine the model based on serial number
|
2024-11-11 02:41:31 +00:00
|
|
|
int model;
|
2015-07-12 18:30:06 +00:00
|
|
|
switch (dc_fam) {
|
2015-09-10 00:26:54 +00:00
|
|
|
case DC_FAMILY_HW_OSTC:
|
|
|
|
if (serial > 7000)
|
|
|
|
model = 3; //2C
|
|
|
|
else if (serial > 2048)
|
|
|
|
model = 2; //2N
|
|
|
|
else if (serial > 300)
|
|
|
|
model = 1; //MK2
|
|
|
|
else
|
|
|
|
model = 0; //OSTC
|
|
|
|
break;
|
|
|
|
case DC_FAMILY_HW_FROG:
|
|
|
|
model = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (serial > 10000)
|
|
|
|
model = 0x12; //Sport
|
|
|
|
else
|
|
|
|
model = 0x0A; //OSTC3
|
2015-04-03 23:07:59 +00:00
|
|
|
}
|
|
|
|
|
2015-07-12 18:29:06 +00:00
|
|
|
// Prepare data to pass to libdivecomputer.
|
2024-11-11 02:41:31 +00:00
|
|
|
device_data_t devdata;
|
|
|
|
int ret = prepare_device_descriptor(model, dc_fam, devdata);
|
|
|
|
if (ret == 0)
|
|
|
|
return report_error(translate("gettextFromC", "Unknown DC in dive %d"), ostcdive->number);
|
2024-08-14 07:40:55 +00:00
|
|
|
ostcdive->dcs[0].model = devdata.vendor + " " + devdata.model + " (Imported from OSTCTools)";
|
2015-04-03 23:07:59 +00:00
|
|
|
|
|
|
|
// Parse the dive data
|
2024-11-11 02:41:31 +00:00
|
|
|
dc_status_t rc = libdc_buffer_parser(ostcdive.get(), &devdata, buffer->data(), buffer->size());
|
2015-04-03 23:07:59 +00:00
|
|
|
if (rc != DC_STATUS_SUCCESS)
|
2024-11-11 02:41:31 +00:00
|
|
|
return report_error(translate("gettextFromC", "Error - %s - parsing dive %d"), errmsg(rc), ostcdive->number);
|
2015-04-03 23:07:59 +00:00
|
|
|
|
|
|
|
// Serial number is not part of the header nor the profile, so libdc won't
|
2024-05-18 19:04:58 +00:00
|
|
|
// catch it. If Serial is part of the extra_data, and set to zero, replace it.
|
2024-05-27 15:09:48 +00:00
|
|
|
ostcdive->dcs[0].serial = std::to_string(serial);
|
2015-04-03 23:07:59 +00:00
|
|
|
|
2024-05-27 15:09:48 +00:00
|
|
|
auto it = find_if(ostcdive->dcs[0].extra_data.begin(), ostcdive->dcs[0].extra_data.end(),
|
2024-05-18 19:04:58 +00:00
|
|
|
[](auto &ed) { return ed.key == "Serial"; });
|
2024-05-27 15:09:48 +00:00
|
|
|
if (it != ostcdive->dcs[0].extra_data.end() && it->value == "0")
|
|
|
|
it->value = ostcdive->dcs[0].serial.c_str();
|
|
|
|
else if (it == ostcdive->dcs[0].extra_data.end())
|
|
|
|
add_extra_data(&ostcdive->dcs[0], "Serial", ostcdive->dcs[0].serial);
|
2024-05-18 19:04:58 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
log->dives.record_dive(std::move(ostcdive));
|
2024-11-11 02:41:31 +00:00
|
|
|
|
|
|
|
return 1;
|
2015-04-03 23:07:59 +00:00
|
|
|
}
|