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"
|
2024-05-02 07:36:00 +00:00
|
|
|
#include "owning_ptrs.h"
|
2015-04-03 23:07:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fills a device_data_t structure with known dc data and a descriptor.
|
|
|
|
*/
|
2024-05-02 07:36:00 +00:00
|
|
|
static int ostc_prepare_data(int data_model, dc_family_t dc_fam, device_data_t &dev_data)
|
2015-04-03 23:07:59 +00:00
|
|
|
{
|
|
|
|
dc_descriptor_t *data_descriptor;
|
|
|
|
|
2024-05-02 07:36:00 +00:00
|
|
|
dev_data.device = NULL;
|
|
|
|
dev_data.context = NULL;
|
2015-04-03 23:07:59 +00:00
|
|
|
|
2016-12-29 15:03:26 +00:00
|
|
|
data_descriptor = get_descriptor(dc_fam, data_model);
|
2015-04-03 23:07:59 +00:00
|
|
|
if (data_descriptor) {
|
2024-05-02 07:36:00 +00:00
|
|
|
dev_data.descriptor = data_descriptor;
|
2024-05-18 15:03:19 +00:00
|
|
|
dev_data.vendor = dc_descriptor_get_vendor(data_descriptor);
|
|
|
|
dev_data.model = dc_descriptor_get_product(data_descriptor);
|
2015-09-10 00:26:54 +00:00
|
|
|
} else {
|
2015-07-12 18:29:06 +00:00
|
|
|
return 0;
|
2015-09-10 00:26:54 +00:00
|
|
|
}
|
2015-07-12 18:29:06 +00:00
|
|
|
return 1;
|
2015-04-03 23:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-05-04 16:45:55 +00:00
|
|
|
void ostctools_import(const char *file, struct divelog *log)
|
2015-04-03 23:07:59 +00:00
|
|
|
{
|
|
|
|
FILE *archive;
|
2024-05-02 07:36:00 +00:00
|
|
|
device_data_t devdata;
|
2015-04-03 23:07:59 +00:00
|
|
|
dc_family_t dc_fam;
|
2024-05-02 07:36:00 +00:00
|
|
|
std::vector<unsigned char> buffer(65536, 0);
|
2019-10-26 18:59:26 +00:00
|
|
|
unsigned char uc_tmp[2];
|
2024-05-16 18:11:21 +00:00
|
|
|
auto ostcdive = std::make_unique<dive>();
|
2024-05-02 07:36:00 +00:00
|
|
|
dc_status_t rc = DC_STATUS_SUCCESS;
|
2017-12-19 22:34:32 +00:00
|
|
|
int model, ret, i = 0, c;
|
2015-04-03 23:07:59 +00:00
|
|
|
unsigned int serial;
|
2017-12-19 22:34:32 +00:00
|
|
|
const char *failed_to_read_msg = translate("gettextFromC", "Failed to read '%s'");
|
2015-04-03 23:07:59 +00:00
|
|
|
|
|
|
|
// Open the archive
|
|
|
|
if ((archive = subsurface_fopen(file, "rb")) == NULL) {
|
2017-12-19 22:34:32 +00:00
|
|
|
report_error(failed_to_read_msg, file);
|
2024-05-02 07:36:00 +00:00
|
|
|
return;
|
2015-04-03 23:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read dive number from the log
|
2017-12-28 21:01:16 +00:00
|
|
|
if (fseek(archive, 258, 0) == -1) {
|
|
|
|
report_error(failed_to_read_msg, file);
|
2024-05-02 07:36:00 +00:00
|
|
|
fclose(archive);
|
|
|
|
return;
|
2017-12-28 21:01:16 +00:00
|
|
|
}
|
2017-12-19 22:34:32 +00:00
|
|
|
if (fread(uc_tmp, 1, 2, archive) != 2) {
|
|
|
|
report_error(failed_to_read_msg, file);
|
2024-05-02 07:36:00 +00:00
|
|
|
fclose(archive);
|
|
|
|
return;
|
2017-12-19 22:34:32 +00:00
|
|
|
}
|
2015-07-06 20:02:03 +00:00
|
|
|
ostcdive->number = uc_tmp[0] + (uc_tmp[1] << 8);
|
2015-04-03 23:07:59 +00:00
|
|
|
|
|
|
|
// Read device's serial number
|
2017-12-28 21:01:16 +00:00
|
|
|
if (fseek(archive, 265, 0) == -1) {
|
|
|
|
report_error(failed_to_read_msg, file);
|
2024-05-02 07:36:00 +00:00
|
|
|
fclose(archive);
|
|
|
|
return;
|
2017-12-28 21:01:16 +00:00
|
|
|
}
|
2017-12-19 22:34:32 +00:00
|
|
|
if (fread(uc_tmp, 1, 2, archive) != 2) {
|
|
|
|
report_error(failed_to_read_msg, file);
|
2024-05-02 07:36:00 +00:00
|
|
|
fclose(archive);
|
|
|
|
return;
|
2017-12-19 22:34:32 +00:00
|
|
|
}
|
2015-07-06 20:02:03 +00:00
|
|
|
serial = uc_tmp[0] + (uc_tmp[1] << 8);
|
2015-04-03 23:07:59 +00:00
|
|
|
|
|
|
|
// Read dive's raw data, header + profile
|
2017-12-28 21:01:16 +00:00
|
|
|
if (fseek(archive, 456, 0) == -1) {
|
|
|
|
report_error(failed_to_read_msg, file);
|
2024-05-02 07:36:00 +00:00
|
|
|
fclose(archive);
|
|
|
|
return;
|
2017-12-28 21:01:16 +00:00
|
|
|
}
|
2017-12-19 22:34:32 +00:00
|
|
|
while ((c = getc(archive)) != EOF) {
|
|
|
|
buffer[i] = c;
|
2015-09-10 00:26:54 +00:00
|
|
|
if (buffer[i] == 0xFD && buffer[i - 1] == 0xFD)
|
2015-04-03 23:07:59 +00:00
|
|
|
break;
|
|
|
|
i++;
|
|
|
|
}
|
2017-12-19 22:34:32 +00:00
|
|
|
if (ferror(archive)) {
|
|
|
|
report_error(failed_to_read_msg, file);
|
2024-05-02 07:36:00 +00:00
|
|
|
fclose(archive);
|
|
|
|
return;
|
2017-12-19 22:34:32 +00:00
|
|
|
}
|
2024-05-02 07:36:00 +00:00
|
|
|
fclose(archive);
|
2015-04-03 23:07:59 +00:00
|
|
|
|
|
|
|
// Try to determine the dc family based on the header type
|
2015-09-10 00:26:54 +00:00
|
|
|
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 {
|
2015-07-12 18:30:06 +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:
|
|
|
|
report_error(translate("gettextFromC", "Unknown DC in dive %d"), ostcdive->number);
|
2024-05-02 07:36:00 +00:00
|
|
|
return;
|
2015-07-12 18:30:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to determine the model based on serial number
|
|
|
|
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.
|
|
|
|
ret = ostc_prepare_data(model, dc_fam, devdata);
|
2015-09-10 00:26:54 +00:00
|
|
|
if (ret == 0) {
|
2015-07-12 18:29:06 +00:00
|
|
|
report_error(translate("gettextFromC", "Unknown DC in dive %d"), ostcdive->number);
|
2024-05-02 07:36:00 +00:00
|
|
|
return;
|
2015-07-12 18:29:06 +00:00
|
|
|
}
|
2024-05-02 19:26:22 +00:00
|
|
|
std::string tmp = devdata.vendor + " " + devdata.model + " (Imported from OSTCTools)";
|
2024-05-18 15:03:19 +00:00
|
|
|
ostcdive->dc.model = tmp.c_str();
|
2015-04-03 23:07:59 +00:00
|
|
|
|
|
|
|
// Parse the dive data
|
2024-05-02 07:36:00 +00:00
|
|
|
rc = libdc_buffer_parser(ostcdive.get(), &devdata, buffer.data(), i + 1);
|
2015-04-03 23:07:59 +00:00
|
|
|
if (rc != DC_STATUS_SUCCESS)
|
2015-09-10 00:26:54 +00:00
|
|
|
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-18 15:03:19 +00:00
|
|
|
ostcdive->dc.serial = std::to_string(serial);
|
2015-04-03 23:07:59 +00:00
|
|
|
|
2024-05-18 19:04:58 +00:00
|
|
|
auto it = find_if(ostcdive->dc.extra_data.begin(), ostcdive->dc.extra_data.end(),
|
|
|
|
[](auto &ed) { return ed.key == "Serial"; });
|
|
|
|
if (it != ostcdive->dc.extra_data.end() && it->value == "0")
|
|
|
|
it->value = ostcdive->dc.serial.c_str();
|
|
|
|
else if (it == ostcdive->dc.extra_data.end())
|
|
|
|
add_extra_data(&ostcdive->dc, "Serial", ostcdive->dc.serial);
|
|
|
|
|
2024-05-13 17:34:43 +00:00
|
|
|
record_dive_to_table(ostcdive.release(), log->dives.get());
|
|
|
|
sort_dive_table(log->dives.get());
|
2015-04-03 23:07:59 +00:00
|
|
|
}
|