2017-04-27 18:18:03 +00:00
// SPDX-License-Identifier: GPL-2.0
2012-09-25 14:28:47 +00:00
/*
2024-03-02 08:12:21 +00:00
* uemis - downloader . cpp
2012-09-25 14:28:47 +00:00
*
* Copyright ( c ) Dirk Hohndel < dirk @ hohndel . org >
* released under GPL2
*
* very ( VERY ) loosely based on the algorithms found in Java code by Fabian Gast < fgast @ only640k . net >
* which was released under the BSD - STYLE BEER WARE LICENSE
* I believe that I only used the information about HOW to do this ( download data from the Uemis
* Zurich ) but did not actually use any of his copyrighted code , therefore the license under which
* he released his code does not apply to this new implementation in C
2015-09-05 17:29:43 +00:00
*
2015-09-14 08:31:29 +00:00
* Modified by Guido Lerch guido . lerch @ gmail . com in August 2015
2012-09-25 14:28:47 +00:00
*/
# include <fcntl.h>
2013-10-05 07:29:09 +00:00
# include <dirent.h>
2012-09-25 14:28:47 +00:00
# include <stdio.h>
2019-03-03 21:55:18 +00:00
# include <stdarg.h>
2012-09-25 14:28:47 +00:00
# include <unistd.h>
# include <string.h>
2015-04-30 22:33:16 +00:00
# include <errno.h>
2023-12-03 21:59:21 +00:00
# include <stdlib.h>
2024-04-29 05:02:54 +00:00
# include <array>
2024-05-06 12:36:51 +00:00
# include <string>
2024-04-29 05:02:54 +00:00
# include <charconv>
2012-10-11 00:42:59 +00:00
2013-10-07 23:13:13 +00:00
# include "gettext.h"
2013-06-24 22:14:07 +00:00
# include "libdivecomputer.h"
2012-09-25 14:28:47 +00:00
# include "uemis.h"
2024-04-23 14:09:52 +00:00
# include "dive.h"
2012-09-25 14:28:47 +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"
2019-03-04 22:20:29 +00:00
# include "divesite.h"
2019-08-05 17:41:15 +00:00
# include "errorhelper.h"
2019-08-05 18:07:10 +00:00
# include "file.h"
2024-04-29 05:02:54 +00:00
# include "format.h"
2020-05-01 12:07:59 +00:00
# include "subsurface-time.h"
2024-04-23 14:09:52 +00:00
# include "tag.h"
2024-03-02 08:12:21 +00:00
# include "core/qthelper.h"
2018-05-11 15:25:41 +00:00
# include "core/subsurface-string.h"
2013-10-07 23:13:13 +00:00
2022-02-06 21:05:43 +00:00
# define ACTION_RECONNECT QT_TRANSLATE_NOOP("gettextFromC", "Disconnect / reconnect the SDA")
2015-01-25 15:03:22 +00:00
# define ERR_FS_ALMOST_FULL QT_TRANSLATE_NOOP("gettextFromC", "Uemis Zurich: the file system is almost full.\nDisconnect / reconnect the dive computer\nand click \'Retry\'")
2015-09-09 09:53:30 +00:00
# define ERR_FS_FULL QT_TRANSLATE_NOOP("gettextFromC", "Uemis Zurich: the file system is full.\nDisconnect / reconnect the dive computer\nand click Retry")
2015-01-25 15:03:22 +00:00
# define ERR_FS_SHORT_WRITE QT_TRANSLATE_NOOP("gettextFromC", "Short write to req.txt file.\nIs the Uemis Zurich plugged in correctly?")
2015-09-24 17:26:35 +00:00
# define ERR_NO_FILES QT_TRANSLATE_NOOP("gettextFromC", "No dives to download.")
2024-04-29 05:02:54 +00:00
constexpr size_t num_param_bufs = 10 ;
2012-12-05 05:03:56 +00:00
2015-09-05 17:33:43 +00:00
// debugging setup
2024-03-02 08:12:21 +00:00
//#define UEMIS_DEBUG 1 + 2 + 4 + 8 + 16 + 32
2015-09-05 17:29:43 +00:00
2015-09-05 18:04:58 +00:00
# define UEMIS_MAX_FILES 4000
2015-09-15 14:46:20 +00:00
# define UEMIS_MEM_FULL 1
2015-09-05 18:15:05 +00:00
# define UEMIS_MEM_OK 0
2015-09-15 14:46:20 +00:00
# define UEMIS_SPOT_BLOCK_SIZE 1
# define UEMIS_DIVE_DETAILS_SIZE 2
# define UEMIS_LOG_BLOCK_SIZE 10
# define UEMIS_CHECK_LOG 1
# define UEMIS_CHECK_DETAILS 2
# define UEMIS_CHECK_SINGLE_DIVE 3
2015-09-05 18:04:58 +00:00
2015-09-15 08:09:24 +00:00
# if UEMIS_DEBUG
2024-03-02 08:12:21 +00:00
static std : : string home , user , d_time ;
2015-09-15 08:09:24 +00:00
static int debug_round = 0 ;
# endif
2015-09-05 17:29:43 +00:00
# if UEMIS_DEBUG & 64 /* we are reading from a copy of the filesystem, not the device - no need to wait */
# define UEMIS_TIMEOUT 50 /* 50ns */
# define UEMIS_LONG_TIMEOUT 500 /* 500ns */
# define UEMIS_MAX_TIMEOUT 2000 /* 2ms */
2012-12-05 05:03:56 +00:00
# else
2015-09-05 17:29:43 +00:00
# define UEMIS_TIMEOUT 50000 /* 50ms */
# define UEMIS_LONG_TIMEOUT 500000 /* 500ms */
# define UEMIS_MAX_TIMEOUT 2000000 /* 2s */
2012-12-05 05:03:56 +00:00
# endif
2012-11-19 22:09:21 +00:00
2024-04-23 14:09:52 +00:00
static uemis uemis_obj ;
2024-04-29 05:02:54 +00:00
static std : : array < std : : string , num_param_bufs > param_buff ;
2024-05-06 12:36:51 +00:00
static std : : string reqtxt_path ;
2012-09-25 14:28:47 +00:00
static int filenr ;
static int number_of_files ;
2012-11-29 00:11:19 +00:00
2015-09-05 18:24:10 +00:00
static int max_mem_used = - 1 ;
2015-09-17 14:46:02 +00:00
static int dive_to_read = 0 ;
2015-09-05 18:24:10 +00:00
2018-01-18 19:38:19 +00:00
/* Linked list to remember already executed divespot download requests */
struct divespot_mapping {
int divespot_id ;
2018-10-23 10:48:33 +00:00
struct dive_site * dive_site ;
2018-01-18 19:38:19 +00:00
struct divespot_mapping * next ;
} ;
static struct divespot_mapping * divespot_mapping = NULL ;
static void erase_divespot_mapping ( )
{
struct divespot_mapping * tmp ;
while ( divespot_mapping ! = NULL ) {
tmp = divespot_mapping ;
divespot_mapping = tmp - > next ;
free ( tmp ) ;
}
divespot_mapping = NULL ;
}
2018-10-23 10:48:33 +00:00
static void add_to_divespot_mapping ( int divespot_id , struct dive_site * ds )
2018-01-18 19:38:19 +00:00
{
struct divespot_mapping * ndm = ( struct divespot_mapping * ) calloc ( 1 , sizeof ( struct divespot_mapping ) ) ;
struct divespot_mapping * * pdm = & divespot_mapping ;
struct divespot_mapping * cdm = * pdm ;
2019-10-27 10:54:10 +00:00
2018-01-18 19:38:19 +00:00
while ( cdm & & cdm - > next )
cdm = cdm - > next ;
2019-10-27 10:54:10 +00:00
2018-01-18 19:38:19 +00:00
ndm - > divespot_id = divespot_id ;
2018-10-23 10:48:33 +00:00
ndm - > dive_site = ds ;
2018-01-18 19:38:19 +00:00
ndm - > next = NULL ;
if ( cdm )
cdm - > next = ndm ;
else
cdm = * pdm = ndm ;
}
static bool is_divespot_mappable ( int divespot_id )
{
struct divespot_mapping * dm = divespot_mapping ;
while ( dm ) {
if ( dm - > divespot_id = = divespot_id )
return true ;
dm = dm - > next ;
}
return false ;
}
2018-10-23 10:42:01 +00:00
static struct dive_site * get_dive_site_by_divespot_id ( int divespot_id )
2018-01-18 19:38:19 +00:00
{
struct divespot_mapping * dm = divespot_mapping ;
while ( dm ) {
if ( dm - > divespot_id = = divespot_id )
2018-10-23 10:48:33 +00:00
return dm - > dive_site ;
2018-01-18 19:38:19 +00:00
dm = dm - > next ;
}
2018-10-23 10:42:01 +00:00
return NULL ;
2018-01-18 19:38:19 +00:00
}
2012-11-29 00:11:19 +00:00
/* helper function to parse the Uemis data structures */
2024-04-29 05:02:54 +00:00
static timestamp_t uemis_ts ( std : : string_view buffer )
2012-11-29 00:11:19 +00:00
{
struct tm tm ;
memset ( & tm , 0 , sizeof ( tm ) ) ;
2024-04-29 05:02:54 +00:00
sscanf ( buffer . begin ( ) , " %d-%d-%dT%d:%d:%d " ,
2014-02-28 04:09:57 +00:00
& tm . tm_year , & tm . tm_mon , & tm . tm_mday ,
& tm . tm_hour , & tm . tm_min , & tm . tm_sec ) ;
tm . tm_mon - = 1 ;
2024-04-29 05:02:54 +00:00
return utc_mktime ( & tm ) ;
}
/* helper function to make the std::from_chars() interface more
* palatable .
* std : : from_chars ( s . begin ( ) , s . end ( ) , v )
* works for the compilers we use , but is not guaranteed to work
* as per standard .
* see : https : //www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2007r0.html
*/
template < typename T >
auto from_chars ( std : : string_view s , T & v )
{
return std : : from_chars ( s . data ( ) , s . data ( ) + s . size ( ) , v ) ;
}
/* Sadly, a number of supported compilers do not support from_chars()
* to double . Therefore , implement our own version .
* Frustrating , but oh well . TODO : Try to find the proper check for the
* existence of this function .
*/
static std : : from_chars_result from_chars ( const std : : string & s , double & v )
{
const char * end ;
double res = ascii_strtod ( s . c_str ( ) , & end ) ;
if ( end = = s . c_str ( ) )
return { end , std : : errc : : invalid_argument } ;
v = res ;
return { end , std : : errc ( ) } ;
}
template < >
auto from_chars < double > ( std : : string_view sv , double & v )
{
std : : string s ( sv ) ; // Generate a null-terminated string to work on.
return from_chars ( s , v ) ;
2012-11-29 00:11:19 +00:00
}
/* float minutes */
2024-04-29 05:02:54 +00:00
static void uemis_duration ( std : : string_view buffer , duration_t & duration )
2012-11-29 00:11:19 +00:00
{
2024-04-29 05:02:54 +00:00
from_chars ( buffer , duration . seconds ) ;
2012-11-29 00:11:19 +00:00
}
/* int cm */
2024-04-29 05:02:54 +00:00
static void uemis_depth ( std : : string_view buffer , depth_t & depth )
2012-11-29 00:11:19 +00:00
{
2024-04-29 05:02:54 +00:00
if ( from_chars ( buffer , depth . mm ) . ec ! = std : : errc : : invalid_argument )
depth . mm * = 10 ;
2012-11-29 00:11:19 +00:00
}
2024-04-29 05:02:54 +00:00
static void uemis_get_index ( std : : string_view buffer , int & idx )
2012-11-29 00:11:19 +00:00
{
2024-04-29 05:02:54 +00:00
from_chars ( buffer , idx ) ;
2012-11-29 00:11:19 +00:00
}
/* space separated */
2024-04-29 05:02:54 +00:00
static void uemis_add_string ( std : : string_view buffer , char * * text , const char * delimit )
2012-11-29 00:11:19 +00:00
{
2012-12-29 02:30:02 +00:00
/* do nothing if this is an empty buffer (Uemis sometimes returns a single
* space for empty buffers ) */
2024-04-29 05:02:54 +00:00
if ( buffer . empty ( ) | | buffer = = " " )
2012-11-29 00:11:19 +00:00
return ;
if ( ! * text ) {
2024-04-29 05:02:54 +00:00
* text = strdup ( std : : string ( buffer ) . c_str ( ) ) ;
2012-11-29 00:11:19 +00:00
} else {
2024-04-29 05:02:54 +00:00
std : : string res = std : : string ( * text ) + delimit ;
res + = buffer ;
2012-11-29 00:11:19 +00:00
free ( * text ) ;
2024-04-29 05:02:54 +00:00
* text = strdup ( res . c_str ( ) ) ;
2012-11-29 00:11:19 +00:00
}
}
/* still unclear if it ever reports lbs */
2024-04-29 05:02:54 +00:00
static void uemis_get_weight ( std : : string_view buffer , weightsystem_t & weight , int diveid )
2012-11-29 00:11:19 +00:00
{
2024-04-29 05:02:54 +00:00
double val ;
if ( from_chars ( buffer , val ) . ec ! = std : : errc : : invalid_argument ) {
weight . weight . grams = uemis_obj . get_weight_unit ( diveid ) ?
lbs_to_grams ( val ) : lrint ( val * 1000 ) ;
}
weight . description = translate ( " gettextFromC " , " unknown " ) ;
2012-11-29 00:11:19 +00:00
}
2013-01-18 20:56:54 +00:00
static struct dive * uemis_start_dive ( uint32_t deviceid )
2012-11-29 00:11:19 +00:00
{
struct dive * dive = alloc_dive ( ) ;
dive - > dc . model = strdup ( " Uemis Zurich " ) ;
dive - > dc . deviceid = deviceid ;
return dive ;
}
2015-09-08 17:04:41 +00:00
static struct dive * get_dive_by_uemis_diveid ( device_data_t * devdata , uint32_t object_id )
2015-09-05 18:00:54 +00:00
{
2022-11-12 11:44:29 +00:00
for ( int i = 0 ; i < devdata - > log - > dives - > nr ; i + + ) {
if ( object_id = = devdata - > log - > dives - > dives [ i ] - > dc . diveid )
return devdata - > log - > dives - > dives [ i ] ;
2015-09-05 18:00:54 +00:00
}
return NULL ;
}
2012-09-25 14:28:47 +00:00
/* send text to the importer progress bar */
static void uemis_info ( const char * fmt , . . . )
{
2013-03-04 01:53:43 +00:00
static char buffer [ 256 ] ;
2012-09-25 14:28:47 +00:00
va_list ap ;
va_start ( ap , fmt ) ;
vsnprintf ( buffer , sizeof ( buffer ) , fmt , ap ) ;
va_end ( ap ) ;
progress_bar_text = buffer ;
2015-11-08 22:55:46 +00:00
if ( verbose )
2024-03-24 20:03:08 +00:00
report_info ( " Uemis downloader: %s " , buffer ) ;
2012-09-25 14:28:47 +00:00
}
static long bytes_available ( int file )
{
2019-10-27 11:07:35 +00:00
long result , r2 ;
2012-09-25 14:28:47 +00:00
long now = lseek ( file , 0 , SEEK_CUR ) ;
2015-06-22 13:46:01 +00:00
if ( now = = - 1 )
return 0 ;
2012-09-25 14:28:47 +00:00
result = lseek ( file , 0 , SEEK_END ) ;
2019-10-27 11:07:35 +00:00
r2 = lseek ( file , now , SEEK_SET ) ;
if ( now = = - 1 | | result = = - 1 | | r2 = = - 1 )
2014-03-06 02:27:33 +00:00
return 0 ;
2012-09-25 14:28:47 +00:00
return result ;
}
2024-04-25 04:53:51 +00:00
static int number_of_file ( const std : : string & path )
2012-09-25 14:28:47 +00:00
{
int count = 0 ;
2013-12-19 13:00:56 +00:00
# ifdef WIN32
struct _wdirent * entry ;
2024-05-06 12:36:51 +00:00
_WDIR * dirp = ( _WDIR * ) subsurface_opendir ( path . c_str ( ) ) ;
2013-12-19 13:00:56 +00:00
# else
struct dirent * entry ;
2024-05-06 12:36:51 +00:00
DIR * dirp = ( DIR * ) subsurface_opendir ( path . c_str ( ) ) ;
2013-12-19 13:00:56 +00:00
# endif
2013-10-05 07:29:09 +00:00
2013-12-19 13:00:56 +00:00
while ( dirp ) {
# ifdef WIN32
entry = _wreaddir ( dirp ) ;
if ( ! entry )
break ;
# else
entry = readdir ( dirp ) ;
if ( ! entry )
break ;
2015-10-21 12:51:11 +00:00
if ( strstr ( entry - > d_name , " .TXT " ) | | strstr ( entry - > d_name , " .txt " ) ) /* If the entry is a regular file */
2013-10-07 04:04:25 +00:00
# endif
2014-02-28 04:09:57 +00:00
count + + ;
2013-10-05 07:29:09 +00:00
}
2013-12-19 13:00:56 +00:00
# ifdef WIN32
_wclosedir ( dirp ) ;
# else
2013-10-05 07:29:09 +00:00
closedir ( dirp ) ;
2013-12-19 13:00:56 +00:00
# endif
2012-09-25 14:28:47 +00:00
return count ;
}
2024-04-25 04:53:51 +00:00
static std : : string build_filename ( const std : : string & path , const std : : string & name )
2013-10-05 07:29:09 +00:00
{
# if WIN32
2024-04-25 04:53:51 +00:00
return path + " \\ " + name ;
2013-10-05 07:29:09 +00:00
# else
2024-04-25 04:53:51 +00:00
return path + " / " + name ;
2013-10-05 07:29:09 +00:00
# endif
}
2012-09-25 14:28:47 +00:00
/* Check if there's a req.txt file and get the starting filenr from it.
* Test for the maximum number of ANS files ( I believe this is always
* 4000 but in case there are differences depending on firmware , this
* code is easy enough */
2024-04-25 04:53:51 +00:00
static bool uemis_init ( const std : : string & path )
2012-09-25 14:28:47 +00:00
{
2024-04-25 04:53:51 +00:00
using namespace std : : string_literals ;
2018-01-18 19:38:19 +00:00
erase_divespot_mapping ( ) ;
2024-04-25 04:53:51 +00:00
if ( path . empty ( ) )
2014-01-15 18:54:41 +00:00
return false ;
2012-09-25 14:28:47 +00:00
/* let's check if this is indeed a Uemis DC */
2024-04-25 04:53:51 +00:00
reqtxt_path = build_filename ( path , " req.txt " s ) ;
2024-05-01 14:56:28 +00:00
int reqtxt_file = subsurface_open ( reqtxt_path . c_str ( ) , O_RDONLY | O_CREAT , 0666 ) ;
2015-06-22 05:16:23 +00:00
if ( reqtxt_file < 0 ) {
2012-12-05 05:03:56 +00:00
# if UEMIS_DEBUG & 1
2024-04-24 09:45:21 +00:00
report_info ( " :EE req.txt can't be opened \n " ) ;
2012-11-19 22:08:14 +00:00
# endif
2014-01-15 18:54:41 +00:00
return false ;
2012-11-19 22:08:14 +00:00
}
2012-09-25 14:28:47 +00:00
if ( bytes_available ( reqtxt_file ) > 5 ) {
char tmp [ 6 ] ;
2018-01-27 22:33:05 +00:00
if ( read ( reqtxt_file , tmp , 5 ) ! = 5 )
return false ;
2012-09-25 14:28:47 +00:00
tmp [ 5 ] = ' \0 ' ;
2012-12-05 05:03:56 +00:00
# if UEMIS_DEBUG & 2
2024-04-24 09:45:21 +00:00
report_info ( " ::r req.txt \" %s \" \n " , tmp ) ;
2012-09-25 14:28:47 +00:00
# endif
if ( sscanf ( tmp + 1 , " %d " , & filenr ) ! = 1 )
2014-01-15 18:54:41 +00:00
return false ;
2013-01-29 21:10:46 +00:00
} else {
2012-11-19 23:09:50 +00:00
filenr = 0 ;
2012-12-05 05:03:56 +00:00
# if UEMIS_DEBUG & 2
2024-04-24 09:45:21 +00:00
report_info ( " ::r req.txt skipped as there were fewer than 5 bytes \n " ) ;
2012-09-25 14:28:47 +00:00
# endif
2012-11-19 23:09:50 +00:00
}
2014-02-28 04:09:57 +00:00
close ( reqtxt_file ) ;
2012-09-25 14:28:47 +00:00
/* It would be nice if we could simply go back to the first set of
2012-11-29 00:11:19 +00:00
* ANS files . But with a FAT filesystem that isn ' t possible */
2024-04-25 04:53:51 +00:00
std : : string ans_path = build_filename ( path , " ANS " s ) ;
2012-09-25 14:28:47 +00:00
number_of_files = number_of_file ( ans_path ) ;
/* initialize the array in which we collect the answers */
2024-04-29 05:02:54 +00:00
for ( std : : string & s : param_buff )
s . clear ( ) ;
2014-01-15 18:54:41 +00:00
return true ;
2012-09-25 14:28:47 +00:00
}
2024-04-29 05:02:54 +00:00
static void str_append_with_delim ( std : : string & s , const std : : string & t )
2012-09-25 14:28:47 +00:00
{
2024-04-29 05:02:54 +00:00
s + = t ;
s + = ' { ' ;
2012-09-25 14:28:47 +00:00
}
2015-11-15 18:15:40 +00:00
/* The communication protocol with the DC is truly funky.
2012-09-25 14:28:47 +00:00
* After you write your request to the req . txt file you call this function .
* It writes the number of the next ANS file at the beginning of the req . txt
* file ( prefixed by ' n ' or ' r ' ) and then again at the very end of it , after
* the full request ( this time without the prefix ) .
* Then it syncs ( not needed on Windows ) and closes the file . */
2024-03-02 08:12:21 +00:00
static void trigger_response ( int file , const char * command , int nr , long tailpos )
2012-09-25 14:28:47 +00:00
{
char fl [ 10 ] ;
snprintf ( fl , 8 , " %s%04d " , command , nr ) ;
2012-12-05 05:03:56 +00:00
# if UEMIS_DEBUG & 4
2024-04-24 09:45:21 +00:00
report_info ( " :tr %s (after seeks) \n " , fl ) ;
2012-09-25 14:28:47 +00:00
# endif
2014-03-06 02:27:33 +00:00
if ( lseek ( file , 0 , SEEK_SET ) = = - 1 )
goto fs_error ;
if ( write ( file , fl , strlen ( fl ) ) = = - 1 )
goto fs_error ;
if ( lseek ( file , tailpos , SEEK_SET ) = = - 1 )
goto fs_error ;
if ( write ( file , fl + 1 , strlen ( fl + 1 ) ) = = - 1 )
goto fs_error ;
2012-09-25 14:28:47 +00:00
# ifndef WIN32
fsync ( file ) ;
# endif
2014-03-06 02:27:33 +00:00
fs_error :
2012-09-25 14:28:47 +00:00
close ( file ) ;
}
2024-04-29 05:02:54 +00:00
static std : : string_view next_token ( std : : string_view & buf )
2012-09-25 14:28:47 +00:00
{
2024-04-29 05:02:54 +00:00
size_t pos = buf . find ( ' { ' ) ;
std : : string_view res ;
if ( pos = = std : : string : : npos ) {
res = buf ;
buf = std : : string_view ( ) ;
} else {
res = buf . substr ( 0 , pos ) ;
buf = buf . substr ( pos + 1 ) ;
}
return res ;
2012-09-25 14:28:47 +00:00
}
2015-11-15 18:15:40 +00:00
/* poor man's tokenizer that understands a quoted delimiter ('{') */
2024-04-29 05:02:54 +00:00
static std : : string next_segment ( const std : : string & buf , int & offset )
2012-09-25 14:28:47 +00:00
{
2024-04-29 05:02:54 +00:00
size_t i = static_cast < size_t > ( offset ) ;
std : : string segment ;
2012-09-25 14:28:47 +00:00
2024-04-29 05:02:54 +00:00
while ( i < buf . size ( ) ) {
if ( i + 1 < buf . size ( ) & & buf [ i ] = = ' \\ ' & &
( buf [ i + 1 ] = = ' \\ ' | | buf [ i + 1 ] = = ' { ' ) ) {
2012-09-25 14:28:47 +00:00
i + + ;
2024-04-29 05:02:54 +00:00
} else if ( buf [ i ] = = ' { ' ) {
i + + ;
break ;
2012-09-25 14:28:47 +00:00
}
2024-04-29 05:02:54 +00:00
segment + = buf [ i + + ] ;
2012-09-25 14:28:47 +00:00
}
2024-04-29 05:02:54 +00:00
offset = i ;
2012-09-25 14:28:47 +00:00
return segment ;
}
/* are there more ANS files we can check? */
2013-10-05 07:29:09 +00:00
static bool next_file ( int max )
2012-09-25 14:28:47 +00:00
{
if ( filenr > = max )
2014-01-15 18:54:41 +00:00
return false ;
2012-09-25 14:28:47 +00:00
filenr + + ;
2014-01-15 18:54:41 +00:00
return true ;
2012-09-25 14:28:47 +00:00
}
2024-04-29 05:02:54 +00:00
/* try and do a quick decode - without trying to get too fancy in case the data
2014-08-17 21:44:43 +00:00
* straddles a block boundary . . .
* we are parsing something that looks like this :
* object_id { int { 2 { date { ts { 2011 - 04 - 05 T12 : 38 : 04 { duration { float { 12.000 . . .
*/
2024-04-29 05:02:54 +00:00
static std : : string first_object_id_val ( std : : string_view buf )
2012-09-25 14:28:47 +00:00
{
2024-04-29 05:02:54 +00:00
size_t object = buf . find ( " object_id " ) ;
if ( object ! = std : : string : : npos & & object + 14 < buf . size ( ) ) {
2012-12-05 20:32:16 +00:00
/* get the value */
2024-04-29 05:02:54 +00:00
size_t p = object + 14 ;
2012-09-25 14:28:47 +00:00
2015-09-05 17:33:43 +00:00
# if UEMIS_DEBUG & 4
2012-12-05 21:43:36 +00:00
char debugbuf [ 50 ] ;
2024-04-29 05:02:54 +00:00
strncpy ( debugbuf , buf . begin ( ) + object , 49 ) ;
2012-12-05 21:43:36 +00:00
debugbuf [ 49 ] = ' \0 ' ;
2024-04-24 09:45:21 +00:00
report_info ( " buf |%s| \n " , debugbuf ) ;
2012-12-05 21:43:36 +00:00
# endif
2024-04-29 05:02:54 +00:00
std : : string res ;
while ( p < buf . size ( ) & & buf [ p ] ! = ' { ' & & res . size ( ) < 9 )
res + = buf [ p + + ] ;
if ( buf [ p ] = = ' { ' ) {
2014-08-17 21:44:43 +00:00
/* found the object_id - let's quickly look for the date */
2024-04-29 05:02:54 +00:00
std : : string_view buf2 = buf . substr ( p ) ;
if ( starts_with ( buf2 , " {date{ts{ " ) & & buf2 . find ( " {duration{ " ) ! = std : : string : : npos ) {
2014-08-17 21:44:43 +00:00
/* cool - that was easy */
2024-04-29 05:02:54 +00:00
res + = ' , ' ;
res + = ' ' ;
2014-08-17 21:44:43 +00:00
/* skip the 9 characters we just found and take the date, ignoring the seconds
* and replace the silly ' T ' in the middle with a space */
2024-04-29 05:02:54 +00:00
res + = buf2 . substr ( 9 , 16 ) ;
size_t pos = res . size ( ) - 16 + 10 ;
if ( res [ pos ] = = ' T ' )
res [ pos ] = ' ' ;
2014-08-17 21:44:43 +00:00
}
2024-04-29 05:02:54 +00:00
return res ;
2012-09-25 14:28:47 +00:00
}
2012-12-05 20:32:16 +00:00
}
return NULL ;
}
/* ultra-simplistic; it doesn't deal with the case when the object_id is
* split across two chunks . It also doesn ' t deal with the discrepancy between
* object_id and dive number as understood by the dive computer */
2024-04-29 05:02:54 +00:00
static void show_progress ( const std : : string & buf , const char * what )
2012-12-05 20:32:16 +00:00
{
2024-04-29 05:02:54 +00:00
std : : string val = first_object_id_val ( buf ) ;
if ( ! val . empty ( ) ) {
2014-02-28 04:09:57 +00:00
/* let the user know what we are working on */
2015-09-17 14:46:02 +00:00
# if UEMIS_DEBUG & 8
2024-04-29 05:02:54 +00:00
report_info ( " reading %s \n %s \n %s \n " , what , val . c_str ( ) , buf . c_str ( ) ) ;
2012-12-05 21:43:36 +00:00
# endif
2024-04-29 05:02:54 +00:00
uemis_info ( translate ( " gettextFromC " , " %s %s " ) , what , val . c_str ( ) ) ;
2012-09-25 14:28:47 +00:00
}
}
2012-11-19 22:09:21 +00:00
static void uemis_increased_timeout ( int * timeout )
{
if ( * timeout < UEMIS_MAX_TIMEOUT )
* timeout + = UEMIS_LONG_TIMEOUT ;
usleep ( * timeout ) ;
}
2024-04-25 04:53:51 +00:00
static std : : string build_ans_path ( const std : : string & path , int filenumber )
2018-11-18 10:33:50 +00:00
{
2024-04-25 04:53:51 +00:00
using namespace std : : string_literals ;
/* Clamp filenumber into the 0..9999 range. This is never necessary,
* as filenumber can never go above UEMIS_MAX_FILES , but gcc doesn ' t
* recognize that and produces very noisy warnings . */
filenumber = filenumber < 0 ? 0 : filenumber % 10000 ;
2018-11-18 10:33:50 +00:00
2024-04-25 04:53:51 +00:00
std : : string fl = " ANS " s + std : : to_string ( filenumber ) + " .TXT " s ;
std : : string intermediate = build_filename ( path , " ANS " s ) ;
return build_filename ( intermediate , fl ) ;
2018-11-18 10:33:50 +00:00
}
2012-09-25 14:28:47 +00:00
/* send a request to the dive computer and collect the answer */
2024-05-01 13:44:35 +00:00
static std : : string uemis_get_answer ( const char * path , const std : : string & request , int n_param_in ,
int n_param_out , std : : string & error_text )
2012-09-25 14:28:47 +00:00
{
2024-04-29 05:02:54 +00:00
int i = 0 ;
2012-09-27 07:16:42 +00:00
char tmp [ 101 ] ;
2014-02-28 04:09:57 +00:00
const char * what = translate ( " gettextFromC " , " data " ) ;
2014-01-15 18:54:41 +00:00
bool searching = true ;
bool assembling_mbuf = false ;
bool ismulti = false ;
bool found_answer = false ;
bool more_files = true ;
bool answer_in_mbuf = false ;
2012-09-25 14:28:47 +00:00
int ans_file ;
2012-11-19 22:09:21 +00:00
int timeout = UEMIS_LONG_TIMEOUT ;
2012-09-25 14:28:47 +00:00
2024-05-01 14:56:28 +00:00
int reqtxt_file = subsurface_open ( reqtxt_path . c_str ( ) , O_RDWR | O_CREAT , 0666 ) ;
2017-12-30 17:32:12 +00:00
if ( reqtxt_file < 0 ) {
2024-04-29 05:02:54 +00:00
error_text = " can't open req.txt " ;
2015-09-05 17:33:43 +00:00
# ifdef UEMIS_DEBUG
2024-04-25 04:53:51 +00:00
report_info ( " open %s failed with errno %d \n " , reqtxt_path . c_str ( ) , errno ) ;
2015-09-05 17:33:43 +00:00
# endif
2024-05-01 13:44:35 +00:00
return std : : string ( ) ;
2015-04-30 22:33:16 +00:00
}
2024-04-29 05:02:54 +00:00
std : : string sb ;
2012-09-25 14:28:47 +00:00
str_append_with_delim ( sb , request ) ;
for ( i = 0 ; i < n_param_in ; i + + )
str_append_with_delim ( sb , param_buff [ i ] ) ;
2024-04-29 05:02:54 +00:00
if ( request = = " getDivelogs " | | request = = " getDeviceData " | | request = = " getDirectory " | |
request = = " getDivespot " | | request = = " getDive " ) {
2014-01-15 18:54:41 +00:00
answer_in_mbuf = true ;
2024-04-29 05:02:54 +00:00
str_append_with_delim ( sb , std : : string ( ) ) ;
if ( request = = " getDivelogs " )
2017-02-20 09:30:01 +00:00
what = translate ( " gettextFromC " , " dive log # " ) ;
2024-04-29 05:02:54 +00:00
else if ( request = = " getDivespot " )
2017-02-20 09:36:49 +00:00
what = translate ( " gettextFromC " , " dive spot # " ) ;
2024-04-29 05:02:54 +00:00
else if ( request = = " getDive " )
2014-08-17 21:44:43 +00:00
what = translate ( " gettextFromC " , " details for # " ) ;
2012-09-25 14:28:47 +00:00
}
2024-04-29 05:02:54 +00:00
str_append_with_delim ( sb , std : : string ( ) ) ;
size_t file_length = sb . size ( ) ;
std : : string header = format_string_std ( " n%04d%08lu " , filenr , ( unsigned long ) file_length ) ;
sb = header + sb ;
2015-09-05 17:33:43 +00:00
# if UEMIS_DEBUG & 4
2024-04-29 05:02:54 +00:00
report_info ( " ::w req.txt \" %s \" \n " , sb . c_str ( ) ) ;
2012-09-25 14:28:47 +00:00
# endif
2024-04-29 05:02:54 +00:00
int written = write ( reqtxt_file , sb . c_str ( ) , sb . size ( ) ) ;
if ( written = = - 1 | | ( size_t ) written ! = sb . size ( ) ) {
error_text = translate ( " gettextFromC " , ERR_FS_SHORT_WRITE ) ;
2024-05-01 13:44:35 +00:00
return std : : string ( ) ;
2012-09-25 14:28:47 +00:00
}
2014-02-28 04:09:57 +00:00
if ( ! next_file ( number_of_files ) ) {
2024-04-29 05:02:54 +00:00
error_text = translate ( " gettextFromC " , ERR_FS_FULL ) ;
2014-01-15 18:54:41 +00:00
more_files = false ;
2012-09-25 14:28:47 +00:00
}
trigger_response ( reqtxt_file , " n " , filenr , file_length ) ;
2012-11-19 22:09:21 +00:00
usleep ( timeout ) ;
2024-05-01 13:44:35 +00:00
std : : string mbuf ;
2012-09-25 14:28:47 +00:00
while ( searching | | assembling_mbuf ) {
2012-11-19 22:11:08 +00:00
if ( import_thread_cancelled )
2024-05-01 13:44:35 +00:00
return std : : string ( ) ;
2015-09-05 18:04:58 +00:00
progress_bar_fraction = filenr / ( double ) UEMIS_MAX_FILES ;
2024-04-25 04:53:51 +00:00
std : : string ans_path = build_ans_path ( std : : string ( path ) , filenr - 1 ) ;
2024-05-06 12:36:51 +00:00
ans_file = subsurface_open ( ans_path . c_str ( ) , O_RDONLY , 0666 ) ;
2017-12-30 17:32:12 +00:00
if ( ans_file < 0 ) {
2024-04-29 05:02:54 +00:00
error_text = " can't open Uemis response file " ;
2017-12-30 17:32:12 +00:00
# ifdef UEMIS_DEBUG
2024-04-25 04:53:51 +00:00
report_info ( " open %s failed with errno %d \n " , ans_path . c_str ( ) , errno ) ;
2017-12-30 17:32:12 +00:00
# endif
2024-05-01 13:44:35 +00:00
return std : : string ( ) ;
2017-12-30 17:32:12 +00:00
}
2019-11-15 10:14:34 +00:00
if ( read ( ans_file , tmp , 100 ) < 3 ) {
2019-10-27 10:54:10 +00:00
close ( ans_file ) ;
2024-05-01 13:44:35 +00:00
return std : : string ( ) ;
2018-11-19 11:35:45 +00:00
}
2012-09-25 14:28:47 +00:00
close ( ans_file ) ;
2012-12-05 05:03:56 +00:00
# if UEMIS_DEBUG & 8
2014-02-28 04:09:57 +00:00
tmp [ 100 ] = ' \0 ' ;
2024-04-25 04:53:51 +00:00
report_info ( " ::t %s \" %s \" \n " , ans_path . c_str ( ) , tmp ) ;
2012-12-05 20:32:16 +00:00
# elif UEMIS_DEBUG & 4
2012-11-19 22:08:14 +00:00
char pbuf [ 4 ] ;
pbuf [ 0 ] = tmp [ 0 ] ;
pbuf [ 1 ] = tmp [ 1 ] ;
pbuf [ 2 ] = tmp [ 2 ] ;
pbuf [ 3 ] = 0 ;
2024-04-25 04:53:51 +00:00
report_info ( " ::t %s \" %s... \" \n " , ans_path . c_str ( ) , pbuf ) ;
2012-09-25 14:28:47 +00:00
# endif
if ( tmp [ 0 ] = = ' 1 ' ) {
2014-01-15 18:54:41 +00:00
searching = false ;
2012-09-25 14:28:47 +00:00
if ( tmp [ 1 ] = = ' m ' ) {
2014-01-15 18:54:41 +00:00
assembling_mbuf = true ;
ismulti = true ;
2012-09-25 14:28:47 +00:00
}
if ( tmp [ 2 ] = = ' e ' )
2014-01-15 18:54:41 +00:00
assembling_mbuf = false ;
2012-09-25 14:28:47 +00:00
if ( assembling_mbuf ) {
2014-02-28 04:09:57 +00:00
if ( ! next_file ( number_of_files ) ) {
2024-04-29 05:02:54 +00:00
error_text = translate ( " gettextFromC " , ERR_FS_FULL ) ;
2014-01-15 18:54:41 +00:00
more_files = false ;
assembling_mbuf = false ;
2012-09-25 14:28:47 +00:00
}
2024-05-06 12:36:51 +00:00
reqtxt_file = subsurface_open ( reqtxt_path . c_str ( ) , O_RDWR | O_CREAT , 0666 ) ;
2017-12-30 17:32:12 +00:00
if ( reqtxt_file < 0 ) {
2024-04-29 05:02:54 +00:00
error_text = " can't open req.txt " ;
2024-05-06 12:36:51 +00:00
report_info ( " open %s failed with errno %d " , reqtxt_path . c_str ( ) , errno ) ;
2024-05-01 13:44:35 +00:00
return std : : string ( ) ;
2015-06-22 13:46:01 +00:00
}
2012-09-25 14:28:47 +00:00
trigger_response ( reqtxt_file , " n " , filenr , file_length ) ;
}
} else {
2014-02-28 04:09:57 +00:00
if ( ! next_file ( number_of_files - 1 ) ) {
2024-04-29 05:02:54 +00:00
error_text = translate ( " gettextFromC " , ERR_FS_FULL ) ;
2014-01-15 18:54:41 +00:00
more_files = false ;
assembling_mbuf = false ;
searching = false ;
2012-09-25 14:28:47 +00:00
}
2024-05-06 12:36:51 +00:00
reqtxt_file = subsurface_open ( reqtxt_path . c_str ( ) , O_RDWR | O_CREAT , 0666 ) ;
2017-12-30 17:32:12 +00:00
if ( reqtxt_file < 0 ) {
2024-04-29 05:02:54 +00:00
error_text = " can't open req.txt " ;
2024-05-06 12:36:51 +00:00
report_info ( " open %s failed with errno %d " , reqtxt_path . c_str ( ) , errno ) ;
2024-05-01 13:44:35 +00:00
return std : : string ( ) ;
2015-06-22 13:46:01 +00:00
}
2012-09-25 14:28:47 +00:00
trigger_response ( reqtxt_file , " r " , filenr , file_length ) ;
2012-11-19 22:09:21 +00:00
uemis_increased_timeout ( & timeout ) ;
2012-09-25 14:28:47 +00:00
}
if ( ismulti & & more_files & & tmp [ 0 ] = = ' 1 ' ) {
int size ;
2024-04-25 04:53:51 +00:00
std : : string ans_path = build_ans_path ( std : : string ( path ) , assembling_mbuf ? filenr - 2 : filenr - 1 ) ;
2024-05-06 12:36:51 +00:00
ans_file = subsurface_open ( ans_path . c_str ( ) , O_RDONLY , 0666 ) ;
2017-12-30 17:32:12 +00:00
if ( ans_file < 0 ) {
2024-04-29 05:02:54 +00:00
error_text = " can't open Uemis response file " ;
2017-12-30 17:32:12 +00:00
# ifdef UEMIS_DEBUG
2024-04-25 04:53:51 +00:00
report_info ( " open %s failed with errno %d \n " , ans_path . c_str ( ) , errno ) ;
2017-12-30 17:32:12 +00:00
# endif
2024-05-01 13:44:35 +00:00
return std : : string ( ) ;
2017-12-30 17:32:12 +00:00
}
2012-09-25 14:28:47 +00:00
size = bytes_available ( ans_file ) ;
if ( size > 3 ) {
2014-03-06 02:27:33 +00:00
int r ;
if ( lseek ( ans_file , 3 , SEEK_CUR ) = = - 1 )
goto fs_error ;
2024-04-29 05:02:54 +00:00
std : : string buf ( size - 3 , ' ' ) ;
if ( ( r = read ( ans_file , buf . data ( ) , size - 3 ) ) ! = size - 3 )
2014-03-06 02:27:33 +00:00
goto fs_error ;
2024-04-29 05:02:54 +00:00
mbuf + = buf ;
2012-11-29 00:11:19 +00:00
show_progress ( buf , what ) ;
2024-04-29 05:02:54 +00:00
if ( param_buff [ 3 ] . size ( ) > 1 )
param_buff [ 3 ] = param_buff [ 3 ] . substr ( 1 ) ;
2012-09-25 14:28:47 +00:00
}
close ( ans_file ) ;
2012-11-19 22:09:21 +00:00
timeout = UEMIS_TIMEOUT ;
2012-09-27 07:16:42 +00:00
usleep ( UEMIS_TIMEOUT ) ;
2012-09-25 14:28:47 +00:00
}
}
if ( more_files ) {
2024-04-29 05:02:54 +00:00
int j = 0 ;
std : : string buf ;
2012-09-25 14:28:47 +00:00
if ( ! ismulti ) {
2024-04-25 04:53:51 +00:00
std : : string ans_path = build_ans_path ( std : : string ( path ) , filenr - 1 ) ;
2024-05-06 12:36:51 +00:00
ans_file = subsurface_open ( ans_path . c_str ( ) , O_RDONLY , 0666 ) ;
2017-12-30 17:32:12 +00:00
if ( ans_file < 0 ) {
2024-04-29 05:02:54 +00:00
error_text = " can't open Uemis response file " ;
2017-12-30 17:32:12 +00:00
# ifdef UEMIS_DEBUG
2024-04-25 04:53:51 +00:00
report_info ( " open %s failed with errno %d \n " , ans_path . c_str ( ) , errno ) ;
2017-12-30 17:32:12 +00:00
# endif
2024-05-01 13:44:35 +00:00
return std : : string ( ) ;
2017-12-30 17:32:12 +00:00
}
2024-04-29 05:02:54 +00:00
int size = bytes_available ( ans_file ) ;
2012-09-25 14:28:47 +00:00
if ( size > 3 ) {
2014-03-06 02:27:33 +00:00
int r ;
if ( lseek ( ans_file , 3 , SEEK_CUR ) = = - 1 )
goto fs_error ;
2024-04-29 05:02:54 +00:00
buf = std : : string ( size - 3 , ' ' ) ;
if ( ( r = read ( ans_file , buf . data ( ) , size - 3 ) ) ! = size - 3 )
2014-03-06 02:27:33 +00:00
goto fs_error ;
2024-04-29 05:02:54 +00:00
mbuf + = buf ;
2012-11-29 00:11:19 +00:00
show_progress ( buf , what ) ;
2012-12-05 05:03:56 +00:00
# if UEMIS_DEBUG & 8
2024-04-29 05:02:54 +00:00
report_info ( " ::r %s \" %s \" \n " , ans_path . c_str ( ) , buf . c_str ( ) ) ;
2012-09-25 14:28:47 +00:00
# endif
}
close ( ans_file ) ;
} else {
2014-01-15 18:54:41 +00:00
ismulti = false ;
2012-09-25 14:28:47 +00:00
}
2012-12-05 05:03:56 +00:00
# if UEMIS_DEBUG & 8
2024-04-29 05:02:54 +00:00
report_info ( " :r: %s \n " , buf . c_str ( ) ) ;
2012-09-25 14:28:47 +00:00
# endif
if ( ! answer_in_mbuf )
2024-04-29 05:02:54 +00:00
for ( i = 0 ; i < n_param_out & & ( size_t ) j < buf . size ( ) ; i + + )
param_buff [ i ] = next_segment ( buf , j ) ;
2014-01-15 18:54:41 +00:00
found_answer = true ;
2012-09-25 14:28:47 +00:00
}
2012-12-05 05:03:56 +00:00
# if UEMIS_DEBUG & 1
2012-09-25 14:28:47 +00:00
for ( i = 0 ; i < n_param_out ; i + + )
2024-04-29 05:02:54 +00:00
report_info ( " ::: %d: %s \n " , i , param_buff [ i ] . c_str ( ) ) ;
2012-09-25 14:28:47 +00:00
# endif
2024-05-01 13:44:35 +00:00
return found_answer ? mbuf : std : : string ( ) ;
2014-03-06 02:27:33 +00:00
fs_error :
2015-09-05 17:29:43 +00:00
close ( ans_file ) ;
2024-05-01 13:44:35 +00:00
return std : : string ( ) ;
2012-09-25 14:28:47 +00:00
}
2024-04-29 05:02:54 +00:00
static bool parse_divespot ( const std : : string & buf )
2012-11-29 00:11:19 +00:00
{
2024-04-29 05:02:54 +00:00
std : : string_view bp = std : : string_view ( buf ) . substr ( 1 ) ;
std : : string_view tp = next_token ( bp ) ;
std : : string_view tag , type , val ;
std : : string locationstring ;
int divespot ;
2013-04-08 02:50:26 +00:00
double latitude = 0.0 , longitude = 0.0 ;
2012-11-29 00:11:19 +00:00
2015-09-05 18:09:10 +00:00
// dive spot got deleted, so fail here
2024-04-29 05:02:54 +00:00
if ( bp . find ( " deleted{bool{true " ) ! = std : : string : : npos )
2015-09-05 18:09:10 +00:00
return false ;
// not a dive spot, fail here
2024-04-29 05:02:54 +00:00
if ( tp ! = " divespot " )
2015-09-05 18:09:10 +00:00
return false ;
2012-11-29 00:11:19 +00:00
do
2024-04-29 05:02:54 +00:00
tag = next_token ( bp ) ;
while ( ! bp . empty ( ) & & tag ! = " object_id " ) ;
if ( bp . empty ( ) )
return false ;
next_token ( bp ) ;
val = next_token ( bp ) ;
if ( from_chars ( val , divespot ) . ec = = std : : errc : : invalid_argument )
2015-09-05 18:09:10 +00:00
return false ;
2012-11-29 00:11:19 +00:00
do {
2024-04-29 05:02:54 +00:00
tag = next_token ( bp ) ;
type = next_token ( bp ) ;
val = next_token ( bp ) ;
if ( type = = " string " & & ! val . empty ( ) & & val ! = " " ) {
if ( ! locationstring . empty ( ) )
locationstring + = " , " ;
locationstring + = val ;
} else if ( type = = " float " ) {
if ( tag = = " longitude " )
from_chars ( val , longitude ) ;
else if ( tag = = " latitude " )
from_chars ( val , latitude ) ;
2012-11-29 00:11:19 +00:00
}
2024-04-29 05:02:54 +00:00
} while ( ! tag . empty ( ) ) ;
2015-09-05 18:09:10 +00:00
2024-04-23 14:09:52 +00:00
uemis_obj . set_divelocation ( divespot , locationstring , longitude , latitude ) ;
2015-09-05 18:09:10 +00:00
return true ;
2012-11-29 00:11:19 +00:00
}
2024-03-02 08:12:21 +00:00
static const char * suit [ ] = { " " , QT_TRANSLATE_NOOP ( " gettextFromC " , " wetsuit " ) , QT_TRANSLATE_NOOP ( " gettextFromC " , " semidry " ) , QT_TRANSLATE_NOOP ( " gettextFromC " , " drysuit " ) } ;
static const char * suit_type [ ] = { " " , QT_TRANSLATE_NOOP ( " gettextFromC " , " shorty " ) , QT_TRANSLATE_NOOP ( " gettextFromC " , " vest " ) , QT_TRANSLATE_NOOP ( " gettextFromC " , " long john " ) , QT_TRANSLATE_NOOP ( " gettextFromC " , " jacket " ) , QT_TRANSLATE_NOOP ( " gettextFromC " , " full suit " ) , QT_TRANSLATE_NOOP ( " gettextFromC " , " 2 pcs full suit " ) } ;
static const char * suit_thickness [ ] = { " " , " 0.5-2mm " , " 2-3mm " , " 3-5mm " , " 5-7mm " , " 8mm+ " , QT_TRANSLATE_NOOP ( " gettextFromC " , " membrane " ) } ;
2012-11-29 00:11:19 +00:00
2024-04-29 05:02:54 +00:00
static void parse_tag ( struct dive * dive , std : : string_view tag , std : : string_view val )
2012-11-29 00:11:19 +00:00
{
/* we can ignore computer_id, water and gas as those are redundant
* with the binary data and would just get overwritten */
2015-09-05 17:33:43 +00:00
# if UEMIS_DEBUG & 4
2024-04-29 05:02:54 +00:00
if ( tag = = " file_content " )
report_info ( " Adding to dive %d : %s = %s \n " , dive - > dc . diveid , std : : string ( tag ) . c_str ( ) , std : : string ( val ) . c_str ( ) ) ;
2015-09-05 17:33:43 +00:00
# endif
2024-04-29 05:02:54 +00:00
if ( tag = = " date " ) {
dive - > when = uemis_ts ( val ) ;
} else if ( tag = = " duration " ) {
uemis_duration ( val , dive - > dc . duration ) ;
} else if ( tag = = " depth " ) {
uemis_depth ( val , dive - > dc . maxdepth ) ;
} else if ( tag = = " file_content " ) {
2024-04-23 14:09:52 +00:00
uemis_obj . parse_divelog_binary ( val , dive ) ;
2024-04-29 05:02:54 +00:00
} else if ( tag = = " altitude " ) {
uemis_get_index ( val , dive - > dc . surface_pressure . mbar ) ;
} else if ( tag = = " f32Weight " ) {
2020-10-25 21:17:30 +00:00
weightsystem_t ws = empty_weightsystem ;
2024-04-29 05:02:54 +00:00
uemis_get_weight ( val , ws , dive - > dc . diveid ) ;
2019-06-26 15:21:03 +00:00
add_cloned_weightsystem ( & dive - > weightsystems , ws ) ;
2024-04-29 05:02:54 +00:00
} else if ( tag = = " notes " ) {
2015-09-18 20:14:46 +00:00
uemis_add_string ( val , & dive - > notes , " " ) ;
2024-04-29 05:02:54 +00:00
} else if ( tag = = " u8DiveSuit " ) {
int idx = 0 ;
uemis_get_index ( val , idx ) ;
if ( idx > 0 & & idx < ( int ) std : : size ( suit ) )
uemis_add_string ( translate ( " gettextFromC " , suit [ idx ] ) , & dive - > suit , " " ) ;
} else if ( tag = = " u8DiveSuitType " ) {
int idx = 0 ;
uemis_get_index ( val , idx ) ;
if ( idx > 0 & & idx < ( int ) std : : size ( suit_type ) )
uemis_add_string ( translate ( " gettextFromC " , suit_type [ idx ] ) , & dive - > suit , " " ) ;
} else if ( tag = = " u8SuitThickness " ) {
int idx = 0 ;
uemis_get_index ( val , idx ) ;
if ( idx > 0 & & idx < ( int ) std : : size ( suit_thickness ) )
uemis_add_string ( translate ( " gettextFromC " , suit_thickness [ idx ] ) , & dive - > suit , " " ) ;
} else if ( tag = = " nickname " ) {
2015-09-07 18:48:45 +00:00
uemis_add_string ( val , & dive - > buddy , " , " ) ;
2013-02-04 04:50:14 +00:00
}
2012-11-29 00:11:19 +00:00
}
2015-09-05 18:21:14 +00:00
static bool uemis_delete_dive ( device_data_t * devdata , uint32_t diveid )
{
struct dive * dive = NULL ;
2022-11-12 11:44:29 +00:00
if ( devdata - > log - > dives - > dives [ devdata - > log - > dives - > nr - 1 ] - > dc . diveid = = diveid ) {
2015-09-05 18:21:14 +00:00
/* we hit the last one in the array */
2022-11-12 11:44:29 +00:00
dive = devdata - > log - > dives - > dives [ devdata - > log - > dives - > nr - 1 ] ;
2015-09-05 18:21:14 +00:00
} else {
2022-11-12 11:44:29 +00:00
for ( int i = 0 ; i < devdata - > log - > dives - > nr - 1 ; i + + ) {
if ( devdata - > log - > dives - > dives [ i ] - > dc . diveid = = diveid ) {
dive = devdata - > log - > dives - > dives [ i ] ;
for ( int x = i ; x < devdata - > log - > dives - > nr - 1 ; x + + )
devdata - > log - > dives - > dives [ i ] = devdata - > log - > dives - > dives [ x + 1 ] ;
2015-09-05 18:21:14 +00:00
}
}
}
if ( dive ) {
2022-11-12 11:44:29 +00:00
devdata - > log - > dives - > dives [ - - devdata - > log - > dives - > nr ] = NULL ;
2022-08-20 15:24:45 +00:00
free_dive ( dive ) ;
2015-09-05 18:21:14 +00:00
return true ;
}
return false ;
}
2017-02-20 09:30:01 +00:00
/* This function is called for both dive log and dive information that we get
* from the SDA ( what an insane design , btw ) . The object_id in the dive log
2012-11-29 00:11:19 +00:00
* matches the logfilenr in the dive information ( which has its own , often
* different object_id ) - we use this as the diveid .
2017-02-20 09:30:01 +00:00
* We create the dive when parsing the dive log and then later , when we parse
2012-11-29 00:11:19 +00:00
* the dive information we locate the already created dive via its diveid .
* Most things just get parsed and converted into our internal data structures ,
* but the dive location API is even more crazy . We just get an id that is an
* index into yet another data store that we read out later . In order to
* correctly populate the location and gps data from that we need to remember
2017-02-20 09:36:49 +00:00
* the addresses of those fields for every dive that references the dive spot . */
2024-04-29 05:02:54 +00:00
static bool process_raw_buffer ( device_data_t * devdata , uint32_t deviceid , std : : string_view buf , int & max_divenr , int * for_dive )
2012-09-25 14:28:47 +00:00
{
2014-01-15 18:54:41 +00:00
bool done = false ;
2015-09-10 04:19:07 +00:00
bool is_log = false , is_dive = false ;
2024-04-29 05:02:54 +00:00
std : : vector < std : : string_view > sections ;
2012-11-29 00:11:19 +00:00
struct dive * dive = NULL ;
2024-04-29 05:02:54 +00:00
int dive_no = 0 ;
2012-09-25 14:28:47 +00:00
2015-09-17 14:46:02 +00:00
# if UEMIS_DEBUG & 8
2024-04-29 05:02:54 +00:00
report_info ( " p_r_b %s \n " , std : : string ( buf ) . c_str ( ) ) ;
2014-05-05 21:36:55 +00:00
# endif
2013-02-08 01:53:32 +00:00
if ( for_dive )
* for_dive = - 1 ;
2024-04-29 05:02:54 +00:00
std : : string_view bp = buf . substr ( 1 ) ;
std : : string_view tp = next_token ( bp ) ;
if ( tp = = " divelog " ) {
2017-02-20 09:30:01 +00:00
/* this is a dive log */
2015-09-05 18:24:10 +00:00
is_log = true ;
2024-04-29 05:02:54 +00:00
tp = next_token ( bp ) ;
2014-05-05 21:36:55 +00:00
/* is it a valid entry or nothing ? */
2024-04-29 05:02:54 +00:00
if ( tp ! = " 1.0 " | | buf . find ( " divelog{1.0{{{{ " ) ! = std : : string : : npos )
2014-05-05 21:36:55 +00:00
return false ;
2024-04-29 05:02:54 +00:00
} else if ( tp = = " dive " ) {
2012-10-26 03:15:39 +00:00
/* this is dive detail */
2015-09-05 18:24:10 +00:00
is_dive = true ;
2024-04-29 05:02:54 +00:00
tp = next_token ( bp ) ;
if ( tp ! = " 1.0 " )
2014-05-05 21:36:55 +00:00
return false ;
2012-10-26 03:15:39 +00:00
} else {
/* don't understand the buffer */
2014-05-05 21:36:55 +00:00
return false ;
2012-10-26 03:15:39 +00:00
}
2015-09-05 18:24:10 +00:00
if ( is_log ) {
2012-11-29 00:11:19 +00:00
dive = uemis_start_dive ( deviceid ) ;
2014-05-05 21:36:55 +00:00
} else {
/* remember, we don't know if this is the right entry,
* so first test if this is even a valid entry */
2024-04-29 05:02:54 +00:00
if ( buf . find ( " deleted{bool{true " ) ! = std : : string : : npos ) {
2015-09-05 17:33:43 +00:00
# if UEMIS_DEBUG & 2
2024-04-24 09:45:21 +00:00
report_info ( " p_r_b entry deleted \n " ) ;
2014-05-05 21:36:55 +00:00
# endif
/* oops, this one isn't valid, suggest to try the previous one */
return false ;
}
2015-09-07 18:51:49 +00:00
/* quickhack and workaround to capture the original dive_no
2015-11-15 18:15:40 +00:00
* I am doing this so I don ' t have to change the original design
2015-09-07 18:51:49 +00:00
* but when parsing a dive we never parse the dive number because
2015-11-15 18:15:40 +00:00
* at the time it ' s being read the * dive variable is not set because
2015-09-07 18:51:49 +00:00
* the dive_no tag comes before the object_id in the uemis ans file
*/
2024-04-29 05:02:54 +00:00
size_t dive_no_pos = buf . find ( " dive_no{int{ " ) ;
if ( dive_no_pos ! = std : : string : : npos ) {
dive_no_pos + = 12 ;
size_t dive_no_end = buf . find ( ' { ' , dive_no_pos ) ;
if ( dive_no_end ! = std : : string : : npos ) {
std : : string_view dive_no_str = buf . substr ( dive_no_pos , dive_no_end - dive_no_pos ) ;
if ( from_chars ( dive_no_str , dive_no ) . ec = = std : : errc : : invalid_argument )
dive_no = 0 ;
2015-10-02 01:49:00 +00:00
}
}
2014-05-05 21:36:55 +00:00
}
2012-09-25 14:28:47 +00:00
while ( ! done ) {
2012-10-26 03:15:39 +00:00
/* the valid buffer ends with a series of delimiters */
2024-04-29 05:02:54 +00:00
if ( bp . size ( ) < 2 | | starts_with ( bp , " {{ " ) )
2012-10-26 03:15:39 +00:00
break ;
2024-04-29 05:02:54 +00:00
std : : string_view tag = next_token ( bp ) ;
2012-10-26 03:15:39 +00:00
/* we also end if we get an empty tag */
2024-04-29 05:02:54 +00:00
if ( tag . empty ( ) )
2012-10-26 03:15:39 +00:00
break ;
2024-04-29 05:02:54 +00:00
if ( std : : find ( sections . begin ( ) , sections . end ( ) , tag ) ! = sections . end ( ) )
tag = next_token ( bp ) ;
std : : string_view type = next_token ( bp ) ;
if ( type = = " 1.0 " ) {
2012-10-26 03:15:39 +00:00
/* this tells us the sections that will follow; the tag here
* is of the format dive - < section > */
2024-04-29 05:02:54 +00:00
size_t pos = tag . find ( ' - ' ) ;
if ( pos ! = std : : string : : npos )
sections . push_back ( tag . substr ( pos + 1 ) ) ;
2012-12-05 05:03:56 +00:00
# if UEMIS_DEBUG & 4
2024-04-29 05:02:54 +00:00
report_info ( " Expect to find section %s \n " , std : : string ( sections . back ( ) ) . c_str ( ) ) ;
2012-10-26 03:15:39 +00:00
# endif
continue ;
}
2024-04-29 05:02:54 +00:00
std : : string_view val = next_token ( bp ) ;
2015-09-05 17:33:43 +00:00
# if UEMIS_DEBUG & 8
2024-04-29 05:02:54 +00:00
if ( val . size ( ) < 20 )
report_info ( " Parsed %s, %s, %s \n ************************* \n " , std : : string ( tag ) . c_str ( ) ,
std : : string ( type ) . c_str ( ) ,
std : : string ( val ) . c_str ( ) ) ;
2015-09-05 17:33:43 +00:00
# endif
2024-04-29 05:02:54 +00:00
if ( is_log & & tag = = " object_id " ) {
from_chars ( val , max_divenr ) ;
dive - > dc . diveid = max_divenr ;
2015-09-05 17:33:43 +00:00
# if UEMIS_DEBUG % 2
2024-04-29 05:02:54 +00:00
report_info ( " Adding new dive from log with object_id %d. \n " , max_divenr ) ;
2015-09-05 17:33:43 +00:00
# endif
2024-04-29 05:02:54 +00:00
} else if ( is_dive & & tag = = " logfilenr " ) {
2012-11-29 00:11:19 +00:00
/* this one tells us which dive we are adding data to */
2024-04-29 05:02:54 +00:00
int diveid = 0 ;
from_chars ( val , diveid ) ;
dive = get_dive_by_uemis_diveid ( devdata , diveid ) ;
if ( dive_no ! = 0 )
dive - > number = dive_no ;
2013-02-08 01:53:32 +00:00
if ( for_dive )
2024-04-29 05:02:54 +00:00
* for_dive = diveid ;
} else if ( ! is_log & & dive & & tag = = " divespot_id " ) {
int divespot_id ;
if ( from_chars ( val , divespot_id ) . ec ! = std : : errc : : invalid_argument ) {
2022-11-12 11:44:29 +00:00
struct dive_site * ds = create_dive_site ( " from Uemis " , devdata - > log - > sites ) ;
2019-03-05 21:58:47 +00:00
unregister_dive_from_dive_site ( dive ) ;
add_dive_to_dive_site ( dive , ds ) ;
2024-04-23 14:09:52 +00:00
uemis_obj . mark_divelocation ( dive - > dc . diveid , divespot_id , ds ) ;
2015-09-10 15:29:19 +00:00
}
2015-09-05 17:33:43 +00:00
# if UEMIS_DEBUG & 2
2024-04-24 09:45:21 +00:00
report_info ( " Created divesite %d for diveid : %d \n " , dive - > dive_site - > uuid , dive - > dc . diveid ) ;
2015-09-05 17:33:43 +00:00
# endif
2012-11-29 00:11:19 +00:00
} else if ( dive ) {
parse_tag ( dive , tag , val ) ;
2012-09-25 14:28:47 +00:00
}
2024-04-29 05:02:54 +00:00
if ( is_log & & tag = = " file_content " )
2014-01-15 18:54:41 +00:00
done = true ;
2012-09-27 03:37:57 +00:00
/* done with one dive (got the file_content tag), but there could be more:
* a ' { ' indicates the end of the record - but we need to see another " {{ "
* later in the buffer to know that the next record is complete ( it could
* be a short read because of some error */
2024-04-29 05:02:54 +00:00
if ( done & & bp . size ( ) > 3 ) {
bp = bp . substr ( 1 ) ;
if ( bp [ 0 ] ! = ' { ' & & bp . find ( " {{ " ) ! = std : : string : : npos ) {
done = false ;
record_dive_to_table ( dive , devdata - > log - > dives ) ;
dive = uemis_start_dive ( deviceid ) ;
}
2012-09-25 14:28:47 +00:00
}
}
2015-09-05 18:24:10 +00:00
if ( is_log ) {
2012-11-29 00:11:19 +00:00
if ( dive - > dc . diveid ) {
2022-11-12 11:44:29 +00:00
record_dive_to_table ( dive , devdata - > log - > dives ) ;
2012-11-29 00:11:19 +00:00
} else { /* partial dive */
2022-08-20 15:24:45 +00:00
free_dive ( dive ) ;
2014-05-05 21:36:55 +00:00
return false ;
2012-11-29 00:11:19 +00:00
}
2012-10-26 03:15:39 +00:00
}
2014-05-05 21:36:55 +00:00
return true ;
2012-09-25 14:28:47 +00:00
}
2024-05-01 15:08:56 +00:00
// Returns (mindiveid, maxdiveid)
static std : : pair < uint32_t , uint32_t > uemis_get_divenr ( uint32_t deviceid , struct dive_table * table , int force )
2012-09-25 14:28:47 +00:00
{
2024-04-29 05:02:54 +00:00
uint32_t maxdiveid = 0 ;
2024-05-01 15:08:56 +00:00
uint32_t mindiveid = 0xFFFFFFFF ;
2013-01-18 20:56:54 +00:00
int i ;
2015-09-20 04:09:58 +00:00
/*
* If we are are retrying after a disconnect / reconnect , we
* will look up the highest dive number in the dives we
* already have .
*
* Also , if " force_download " is true , do this even if we
2018-10-04 09:48:40 +00:00
* don ' t have any dives ( maxdiveid will remain ~ 0 ) .
*
* Otherwise , use the global dive table .
2015-09-20 04:09:58 +00:00
*/
2018-10-04 09:48:40 +00:00
if ( ! force & & ! table - > nr )
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
table = divelog . dives ;
2015-09-20 04:09:58 +00:00
for ( i = 0 ; i < table - > nr ; i + + ) {
struct dive * d = table - > dives [ i ] ;
2014-05-13 22:32:45 +00:00
struct divecomputer * dc ;
2015-09-20 04:09:58 +00:00
if ( ! d )
continue ;
2015-09-05 18:24:10 +00:00
for_each_dc ( d , dc ) {
2012-12-29 19:28:35 +00:00
if ( dc - > model & & ! strcmp ( dc - > model , " Uemis Zurich " ) & &
2018-01-28 19:20:02 +00:00
( dc - > deviceid = = 0 | | dc - > deviceid = = 0x7fffffff | | dc - > deviceid = = deviceid ) ) {
if ( dc - > diveid > maxdiveid )
maxdiveid = dc - > diveid ;
if ( dc - > diveid < mindiveid )
mindiveid = dc - > diveid ;
}
2012-12-29 02:21:34 +00:00
}
2012-09-25 14:28:47 +00:00
}
2024-05-01 15:08:56 +00:00
return std : : make_pair ( mindiveid , maxdiveid ) ;
2012-09-25 14:28:47 +00:00
}
2015-09-15 08:09:24 +00:00
# if UEMIS_DEBUG
2024-04-29 05:02:54 +00:00
static bool do_dump_buffer_to_file ( std : : string_view buf , const char * prefix )
2015-09-05 18:22:22 +00:00
{
2024-04-29 05:02:54 +00:00
using namespace std : : string_literals ;
static int bufCnt = 0 ;
if ( buf . empty ( ) )
2015-09-05 18:22:22 +00:00
return false ;
2024-04-29 05:02:54 +00:00
size_t datepos = buf . find ( " date{ts{ " ) ;
std : : string pdate ;
if ( datepos ! = std : : string : : npos ) {
std : : string_view ptr1 = buf . substr ( datepos ) ;
next_token ( ptr1 ) ;
next_token ( ptr1 ) ;
pdate = next_token ( ptr1 ) ;
} else {
pdate = " date{ts{no-date{ " s ;
}
2015-09-05 18:22:22 +00:00
2024-04-29 05:02:54 +00:00
size_t obidpos = buf . find ( " object_id{int{ " ) ;
if ( obidpos = = std : : string : : npos )
2015-09-05 18:22:22 +00:00
return false ;
2024-04-29 05:02:54 +00:00
std : : string_view ptr2 = buf . substr ( obidpos ) ;
next_token ( ptr2 ) ;
next_token ( ptr2 ) ;
std : : string pobid = std : : string ( next_token ( ptr2 ) ) ;
std : : string path = format_string_std ( " /%s/%s/UEMIS Dump/%s_%s_Uemis_dump_%s_in_round_%d_%d.txt " , home . c_str ( ) , user . c_str ( ) , prefix , pdate . c_str ( ) , pobid . c_str ( ) , debug_round , bufCnt ) ;
int dumpFile = subsurface_open ( path . c_str ( ) , O_RDWR | O_CREAT , 0666 ) ;
2015-09-05 18:22:22 +00:00
if ( dumpFile = = - 1 )
return false ;
2024-04-29 05:02:54 +00:00
bool success = ( size_t ) write ( dumpFile , buf . data ( ) , buf . size ( ) ) = = buf . size ( ) ;
2015-09-05 18:22:22 +00:00
close ( dumpFile ) ;
bufCnt + + ;
2018-01-27 22:33:05 +00:00
return success ;
2015-09-05 18:22:22 +00:00
}
2015-09-15 08:09:24 +00:00
# endif
2015-09-05 18:22:22 +00:00
2015-09-05 18:15:05 +00:00
/* do some more sophisticated calculations here to try and predict if the next round of
* divelog / divedetail reads will fit into the UEMIS buffer ,
* filenr holds now the uemis filenr after having read several logs including the dive details ,
* fCapacity will five us the average number of files needed for all currently loaded data
* remember the maximum file usage per dive
2015-11-15 18:15:40 +00:00
* return : UEMIS_MEM_OK if there is enough memory for a full round
2015-09-05 18:15:05 +00:00
* UEMIS_MEM_CRITICAL if the memory is good for reading the dive logs
2015-11-15 18:15:40 +00:00
* UEMIS_MEM_FULL if the memory is exhausted
2015-09-05 18:15:05 +00:00
*/
2015-09-15 14:46:20 +00:00
static int get_memory ( struct dive_table * td , int checkpoint )
2015-09-05 18:15:05 +00:00
{
2015-09-15 14:46:20 +00:00
if ( td - > nr < = 0 )
2015-09-05 18:15:05 +00:00
return UEMIS_MEM_OK ;
2015-09-15 14:46:20 +00:00
switch ( checkpoint ) {
case UEMIS_CHECK_LOG :
if ( filenr / td - > nr > max_mem_used )
max_mem_used = filenr / td - > nr ;
/* check if a full block of dive logs + dive details and dive spot fit into the UEMIS buffer */
2015-11-10 02:00:16 +00:00
# if UEMIS_DEBUG & 4
2024-04-24 09:45:21 +00:00
report_info ( " max_mem_used %d (from td->nr %d) * block_size %d > max_files %d - filenr %d? \n " , max_mem_used , td - > nr , UEMIS_LOG_BLOCK_SIZE , UEMIS_MAX_FILES , filenr ) ;
2015-11-10 02:00:16 +00:00
# endif
2015-09-15 14:46:20 +00:00
if ( max_mem_used * UEMIS_LOG_BLOCK_SIZE > UEMIS_MAX_FILES - filenr )
return UEMIS_MEM_FULL ;
break ;
case UEMIS_CHECK_DETAILS :
/* check if the next set of dive details and dive spot fit into the UEMIS buffer */
if ( ( UEMIS_DIVE_DETAILS_SIZE + UEMIS_SPOT_BLOCK_SIZE ) * UEMIS_LOG_BLOCK_SIZE > UEMIS_MAX_FILES - filenr )
return UEMIS_MEM_FULL ;
break ;
case UEMIS_CHECK_SINGLE_DIVE :
if ( UEMIS_DIVE_DETAILS_SIZE + UEMIS_SPOT_BLOCK_SIZE > UEMIS_MAX_FILES - filenr )
return UEMIS_MEM_FULL ;
break ;
2015-09-05 18:15:05 +00:00
}
return UEMIS_MEM_OK ;
}
2018-10-03 20:03:39 +00:00
/* we misuse the hidden_by_filter flag to mark a dive as deleted.
* this will be picked up by some cleaning statement later . */
2015-09-05 18:24:10 +00:00
static void do_delete_dives ( struct dive_table * td , int idx )
{
for ( int x = idx ; x < td - > nr ; x + + )
2018-10-03 20:03:39 +00:00
td - > dives [ x ] - > hidden_by_filter = true ;
2015-09-05 18:24:10 +00:00
}
2015-09-10 14:12:57 +00:00
static bool load_uemis_divespot ( const char * mountpath , int divespot_id )
{
2024-04-29 05:02:54 +00:00
param_buff [ 2 ] = std : : to_string ( divespot_id ) ;
2015-09-10 14:12:57 +00:00
# if UEMIS_DEBUG & 2
2024-04-24 09:45:21 +00:00
report_info ( " getDivespot %d \n " , divespot_id ) ;
2015-09-10 14:12:57 +00:00
# endif
2024-04-29 05:02:54 +00:00
std : : string error_text ; // unused
2024-05-01 13:44:35 +00:00
std : : string mbuf = uemis_get_answer ( mountpath , " getDivespot " , 3 , 0 , error_text ) ;
if ( ! mbuf . empty ( ) ) {
2015-09-10 19:23:47 +00:00
# if UEMIS_DEBUG & 16
2015-09-15 08:09:24 +00:00
do_dump_buffer_to_file ( mbuf , " Spot " ) ;
2015-09-10 14:12:57 +00:00
# endif
return parse_divespot ( mbuf ) ;
}
return false ;
}
2019-02-28 21:45:17 +00:00
static void get_uemis_divespot ( device_data_t * devdata , const char * mountpath , int divespot_id , struct dive * dive )
2015-09-14 08:42:00 +00:00
{
2018-10-26 15:03:54 +00:00
struct dive_site * nds = dive - > dive_site ;
2019-10-27 10:54:10 +00:00
2018-01-18 19:38:19 +00:00
if ( is_divespot_mappable ( divespot_id ) ) {
2018-10-23 10:42:01 +00:00
struct dive_site * ds = get_dive_site_by_divespot_id ( divespot_id ) ;
2019-03-05 21:58:47 +00:00
unregister_dive_from_dive_site ( dive ) ;
add_dive_to_dive_site ( dive , ds ) ;
2018-01-18 19:38:19 +00:00
} else if ( nds & & nds - > name & & strstr ( nds - > name , " from Uemis " ) ) {
2015-09-18 19:22:26 +00:00
if ( load_uemis_divespot ( mountpath , divespot_id ) ) {
/* get the divesite based on the diveid, this should give us
* the newly created site
*/
2018-10-23 10:42:01 +00:00
struct dive_site * ods ;
2015-09-14 08:42:00 +00:00
/* with the divesite name we got from parse_dive, that is called on load_uemis_divespot
* we search all existing divesites if we have one with the same name already . The function
* returns the first found which is luckily not the newly created .
*/
2022-11-12 11:44:29 +00:00
ods = get_dive_site_by_name ( nds - > name , devdata - > log - > sites ) ;
2015-09-14 08:42:00 +00:00
if ( ods ) {
2015-11-15 18:15:40 +00:00
/* if the uuid's are the same, the new site is a duplicate and can be deleted */
2015-09-14 08:42:00 +00:00
if ( nds - > uuid ! = ods - > uuid ) {
2022-11-12 11:44:29 +00:00
delete_dive_site ( nds , devdata - > log - > sites ) ;
2019-03-05 21:58:47 +00:00
unregister_dive_from_dive_site ( dive ) ;
add_dive_to_dive_site ( dive , ods ) ;
2015-09-14 08:42:00 +00:00
}
}
2018-10-26 15:03:54 +00:00
add_to_divespot_mapping ( divespot_id , dive - > dive_site ) ;
2015-09-18 19:22:26 +00:00
} else {
2015-11-15 18:15:40 +00:00
/* if we can't load the dive site details, delete the site we
2015-09-18 19:22:26 +00:00
* created in process_raw_buffer
*/
2022-11-12 11:44:29 +00:00
delete_dive_site ( dive - > dive_site , devdata - > log - > sites ) ;
2015-09-14 08:42:00 +00:00
}
}
}
2024-04-25 04:25:36 +00:00
static bool get_matching_dive ( int idx , int & newmax , int * uemis_mem_status , device_data_t * data , const char * mountpath , const char deviceidnr )
2015-09-14 20:04:10 +00:00
{
2022-11-12 11:44:29 +00:00
struct dive * dive = data - > log - > dives - > dives [ idx ] ;
2015-09-14 20:04:10 +00:00
char log_file_no_to_find [ 20 ] ;
bool found = false ;
2015-11-10 01:58:36 +00:00
bool found_below = false ;
bool found_above = false ;
2015-09-17 14:46:02 +00:00
int deleted_files = 0 ;
2018-01-28 19:20:02 +00:00
int fail_count = 0 ;
2015-09-14 20:04:10 +00:00
snprintf ( log_file_no_to_find , sizeof ( log_file_no_to_find ) , " logfilenr{int{%d " , dive - > dc . diveid ) ;
2015-11-10 02:00:16 +00:00
# if UEMIS_DEBUG & 2
2024-04-24 09:45:21 +00:00
report_info ( " Looking for dive details to go with dive log id %d \n " , dive - > dc . diveid ) ;
2015-11-10 02:00:16 +00:00
# endif
2015-09-14 20:04:10 +00:00
while ( ! found ) {
2015-09-16 07:10:56 +00:00
if ( import_thread_cancelled )
break ;
2024-04-29 05:02:54 +00:00
param_buff [ 2 ] = std : : to_string ( dive_to_read ) ;
std : : string error_text ; // unused
2024-05-01 13:44:35 +00:00
std : : string mbuf = uemis_get_answer ( mountpath , " getDive " , 3 , 0 , error_text ) ;
2015-09-14 20:04:10 +00:00
# if UEMIS_DEBUG & 16
2015-09-15 08:09:24 +00:00
do_dump_buffer_to_file ( mbuf , " Dive " ) ;
2015-09-14 20:04:10 +00:00
# endif
2022-11-12 11:44:29 +00:00
* uemis_mem_status = get_memory ( data - > log - > dives , UEMIS_CHECK_SINGLE_DIVE ) ;
2015-09-15 14:46:20 +00:00
if ( * uemis_mem_status = = UEMIS_MEM_OK ) {
2017-02-20 09:30:01 +00:00
/* if the memory isn's completely full we can try to read more dive log vs. dive details
2015-09-14 20:04:10 +00:00
* UEMIS_MEM_CRITICAL means not enough space for a full round but the dive details
2017-02-20 09:36:49 +00:00
* and the dive spots should fit into the UEMIS memory
2015-09-14 20:04:10 +00:00
* The match we do here is to map the object_id to the logfilenr , we do this
2017-02-20 09:30:01 +00:00
* by iterating through the last set of loaded dive logs and then find the corresponding
2015-09-14 20:04:10 +00:00
* dive with the matching logfilenr */
2024-04-29 05:02:54 +00:00
if ( ! mbuf . empty ( ) ) {
if ( strstr ( mbuf . c_str ( ) , log_file_no_to_find ) ) {
2017-02-20 09:30:01 +00:00
/* we found the logfilenr that matches our object_id from the dive log we were looking for
2015-11-15 18:15:40 +00:00
* we mark the search successful even if the dive has been deleted . */
2015-09-14 20:04:10 +00:00
found = true ;
2024-04-29 05:02:54 +00:00
if ( strstr ( mbuf . c_str ( ) , " deleted{bool{true " ) = = NULL ) {
2024-04-25 04:25:36 +00:00
process_raw_buffer ( data , deviceidnr , mbuf , newmax , NULL ) ;
2015-09-14 20:04:10 +00:00
/* remember the last log file number as it is very likely that subsequent dives
* have the same or higher logfile number .
* UEMIS unfortunately deletes dives by deleting the dive details and not the logs . */
# if UEMIS_DEBUG & 2
2015-09-15 08:09:24 +00:00
d_time = get_dive_date_c_string ( dive - > when ) ;
2024-04-24 09:45:21 +00:00
report_info ( " Matching dive log id %d from %s with dive details %d \n " , dive - > dc . diveid , d_time . c_str ( ) , dive_to_read ) ;
2015-09-14 20:04:10 +00:00
# endif
2024-04-23 14:09:52 +00:00
int divespot_id = uemis_obj . get_divespot_id_by_diveid ( dive - > dc . diveid ) ;
2015-09-18 19:37:23 +00:00
if ( divespot_id > = 0 )
2019-02-28 21:45:17 +00:00
get_uemis_divespot ( data , mountpath , divespot_id , dive ) ;
2015-09-14 20:04:10 +00:00
} else {
/* in this case we found a deleted file, so let's increment */
# if UEMIS_DEBUG & 2
2015-09-15 08:09:24 +00:00
d_time = get_dive_date_c_string ( dive - > when ) ;
2024-04-24 09:45:21 +00:00
report_info ( " TRY matching dive log id %d from %s with dive details %d but details are deleted \n " , dive - > dc . diveid , d_time . c_str ( ) , dive_to_read ) ;
2015-09-14 20:04:10 +00:00
# endif
2015-09-17 14:46:02 +00:00
deleted_files + + ;
2015-09-14 20:04:10 +00:00
/* mark this log entry as deleted and cleanup later, otherwise we mess up our array */
2018-10-03 20:03:39 +00:00
dive - > hidden_by_filter = true ;
2015-09-14 20:04:10 +00:00
# if UEMIS_DEBUG & 2
2024-04-25 04:25:36 +00:00
report_info ( " Deleted dive from %s, with id %d from table -- newmax is %d \n " , d_time . c_str ( ) , dive - > dc . diveid , newmax ) ;
2015-09-14 20:04:10 +00:00
# endif
}
} else {
2015-09-17 14:46:02 +00:00
uint32_t nr_found = 0 ;
2024-04-29 05:02:54 +00:00
size_t pos = mbuf . find ( " logfilenr " ) ;
if ( pos ! = std : : string : : npos & & mbuf . find ( " act{ " ) ! = std : : string : : npos ) {
sscanf ( mbuf . c_str ( ) + pos , " logfilenr{int{%u " , & nr_found ) ;
2015-12-06 21:38:38 +00:00
if ( nr_found > = dive - > dc . diveid | | nr_found = = 0 ) {
2015-11-10 01:58:36 +00:00
found_above = true ;
2015-09-17 14:46:02 +00:00
dive_to_read = dive_to_read - 2 ;
2015-11-10 01:58:36 +00:00
} else {
found_below = true ;
}
2015-09-17 14:46:02 +00:00
if ( dive_to_read < - 1 )
dive_to_read = - 1 ;
2024-04-29 05:02:54 +00:00
} else if ( mbuf . find ( " act{ " ) = = std : : string : : npos & & + + fail_count = = 10 ) {
2018-01-28 19:20:02 +00:00
if ( verbose )
2024-03-24 20:03:08 +00:00
report_info ( " Uemis downloader: Cannot access dive details - searching from start " ) ;
2018-01-28 19:20:02 +00:00
dive_to_read = - 1 ;
2015-09-17 14:46:02 +00:00
}
2015-09-14 20:04:10 +00:00
}
}
2015-11-10 01:58:36 +00:00
if ( found_above & & found_below )
break ;
2015-09-17 14:46:02 +00:00
dive_to_read + + ;
2015-09-14 20:04:10 +00:00
} else {
2017-02-20 09:30:01 +00:00
/* At this point the memory of the UEMIS is full, let's cleanup all dive log files were
2015-09-14 20:04:10 +00:00
* we could not match the details to . */
2022-11-12 11:44:29 +00:00
do_delete_dives ( data - > log - > dives , idx ) ;
2015-09-14 20:04:10 +00:00
return false ;
}
}
/* decrement iDiveToRead by the amount of deleted entries found to assure
* we are not missing any valid matches when processing subsequent logs */
2015-09-17 14:46:02 +00:00
dive_to_read = ( dive_to_read - deleted_files > 0 ? dive_to_read - deleted_files : 0 ) ;
deleted_files = 0 ;
2015-09-14 20:04:10 +00:00
return true ;
}
2024-04-29 05:02:54 +00:00
std : : string do_uemis_import ( device_data_t * data )
2012-09-25 14:28:47 +00:00
{
2014-10-12 10:57:32 +00:00
const char * mountpath = data - > devname ;
short force_download = data - > force_download ;
2024-04-25 04:25:36 +00:00
int newmax = - 1 ;
2015-04-22 03:29:20 +00:00
int first , start , end = - 2 ;
2013-01-18 20:56:54 +00:00
uint32_t deviceidnr ;
2024-04-29 05:02:54 +00:00
std : : string deviceid ;
std : : string result ;
2024-05-01 13:44:35 +00:00
bool once = true ;
2015-09-05 18:24:10 +00:00
int match_dive_and_log = 0 ;
2022-02-06 21:05:43 +00:00
int dive_offset = 0 ;
2015-09-05 18:24:10 +00:00
int uemis_mem_status = UEMIS_MEM_OK ;
2022-02-06 21:05:43 +00:00
// To speed up sync you can skip downloading old dives by defining UEMIS_DIVE_OFFSET
if ( getenv ( " UEMIS_DIVE_OFFSET " ) ) {
dive_offset = atoi ( getenv ( " UEMIS_DIVE_OFFSET " ) ) ;
2024-04-24 09:45:21 +00:00
report_info ( " Uemis: Using dive # offset %d \n " , dive_offset ) ;
2022-02-06 21:05:43 +00:00
}
2015-09-15 08:09:24 +00:00
# if UEMIS_DEBUG
home = getenv ( " HOME " ) ;
user = getenv ( " LOGNAME " ) ;
# endif
2014-11-25 15:47:24 +00:00
uemis_info ( translate ( " gettextFromC " , " Initialise communication " ) ) ;
2015-03-23 18:22:55 +00:00
if ( ! uemis_init ( mountpath ) ) {
2014-02-28 04:09:57 +00:00
return translate ( " gettextFromC " , " Uemis init failed " ) ;
2015-03-23 18:22:55 +00:00
}
2015-09-05 18:24:10 +00:00
2024-05-01 13:44:35 +00:00
if ( uemis_get_answer ( mountpath , " getDeviceId " , 0 , 1 , result ) . empty ( ) )
2012-09-30 03:57:48 +00:00
goto bail ;
2024-04-29 05:02:54 +00:00
deviceid = param_buff [ 0 ] ;
deviceidnr = atoi ( deviceid . c_str ( ) ) ;
2015-09-05 18:24:10 +00:00
2012-09-25 14:28:47 +00:00
/* param_buff[0] is still valid */
2024-05-01 13:44:35 +00:00
if ( uemis_get_answer ( mountpath , " initSession " , 1 , 6 , result ) . empty ( ) )
2012-09-30 03:57:48 +00:00
goto bail ;
2015-09-05 18:24:10 +00:00
2014-02-28 04:09:57 +00:00
uemis_info ( translate ( " gettextFromC " , " Start download " ) ) ;
2024-05-01 13:44:35 +00:00
if ( uemis_get_answer ( mountpath , " processSync " , 0 , 2 , result ) . empty ( ) )
2012-09-30 03:57:48 +00:00
goto bail ;
2015-09-05 18:24:10 +00:00
2012-11-19 22:11:08 +00:00
/* before starting the long download, check if user pressed cancel */
if ( import_thread_cancelled )
goto bail ;
2015-09-05 18:24:10 +00:00
2012-09-25 14:28:47 +00:00
param_buff [ 1 ] = " notempty " ;
2024-05-01 15:08:56 +00:00
{
auto [ mindiveid , maxdiveid ] = uemis_get_divenr ( deviceidnr , data - > log - > dives , force_download ) ;
newmax = maxdiveid ;
if ( verbose )
report_info ( " Uemis downloader: start looking at dive nr %d " , newmax ) ;
first = start = newmax ;
dive_to_read = ( int ) mindiveid < first ? first - mindiveid : first ;
}
2022-02-06 21:05:43 +00:00
if ( dive_offset > 0 )
start + = dive_offset ;
2012-09-27 03:37:57 +00:00
for ( ; ; ) {
2015-09-05 18:24:10 +00:00
# if UEMIS_DEBUG & 2
2015-09-15 08:09:24 +00:00
debug_round + + ;
2015-09-05 18:24:10 +00:00
# endif
2014-05-05 21:36:55 +00:00
# if UEMIS_DEBUG & 4
2024-04-25 04:25:36 +00:00
report_info ( " d_u_i inner loop start %d end %d newmax %d \n " , start , end , newmax ) ;
2014-05-05 21:36:55 +00:00
# endif
2015-09-05 18:24:10 +00:00
/* start at the last filled download table index */
2022-11-12 11:44:29 +00:00
match_dive_and_log = data - > log - > dives - > nr ;
2024-04-25 04:25:36 +00:00
newmax = start ;
std : : string newmax_str = std : : to_string ( newmax ) ;
param_buff [ 2 ] = newmax_str . c_str ( ) ;
2024-04-29 05:02:54 +00:00
param_buff [ 3 ] . clear ( ) ;
2024-05-01 13:44:35 +00:00
std : : string mbuf = uemis_get_answer ( mountpath , " getDivelogs " , 3 , 0 , result ) ;
2022-11-12 11:44:29 +00:00
uemis_mem_status = get_memory ( data - > log - > dives , UEMIS_CHECK_DETAILS ) ;
2015-12-03 17:47:46 +00:00
/* first, remove any leading garbage... this needs to start with a '{' */
2024-04-29 05:02:54 +00:00
std : : string_view realmbuf = mbuf ;
size_t pos = realmbuf . find ( ' { ' ) ;
if ( pos ! = std : : string : : npos )
realmbuf = realmbuf . substr ( pos ) ;
2024-05-01 13:44:35 +00:00
if ( ! realmbuf . empty ( ) & & uemis_mem_status ! = UEMIS_MEM_FULL ) {
2015-09-10 19:23:47 +00:00
# if UEMIS_DEBUG & 16
2017-02-20 09:30:01 +00:00
do_dump_buffer_to_file ( realmbuf , " Dive logs " ) ;
2015-09-05 18:24:10 +00:00
# endif
2024-05-01 13:44:35 +00:00
bool success = true ;
2015-09-05 18:24:10 +00:00
/* process the buffer we have assembled */
2024-04-25 04:25:36 +00:00
if ( ! process_raw_buffer ( data , deviceidnr , realmbuf , newmax , NULL ) ) {
2014-05-05 21:36:55 +00:00
/* if no dives were downloaded, mark end appropriately */
if ( end = = - 2 )
end = start - 1 ;
success = false ;
}
2015-09-05 18:24:10 +00:00
if ( once ) {
2024-04-29 05:02:54 +00:00
std : : string t = first_object_id_val ( realmbuf ) ;
int val ;
if ( from_chars ( t , val ) . ec ! = std : : errc : : invalid_argument ) {
start = std : : max ( val , start ) ;
}
2015-09-05 18:24:10 +00:00
once = false ;
}
/* clean up mbuf */
2024-04-29 05:02:54 +00:00
/* reason unclear:
const char * endptr = strstr ( realmbuf , " {{{ " ) ;
2015-09-05 18:24:10 +00:00
if ( endptr )
* ( endptr + 2 ) = ' \0 ' ;
2024-04-29 05:02:54 +00:00
*/
2015-09-05 18:24:10 +00:00
/* last object_id we parsed */
2024-04-25 04:25:36 +00:00
end = newmax ;
2014-05-05 21:36:55 +00:00
# if UEMIS_DEBUG & 4
2024-04-25 04:25:36 +00:00
report_info ( " d_u_i after download and parse start %d end %d newmax %d progress %4.2f \n " , start , end , newmax , progress_bar_fraction ) ;
2015-09-05 18:24:10 +00:00
# endif
/* The way this works is that I am reading the current dive from what has been loaded during the getDiveLogs call to the UEMIS.
2017-02-20 09:30:01 +00:00
* As the object_id of the dive log entry and the object_id of the dive details are not necessarily the same , the match needs
2015-09-05 18:24:10 +00:00
* to happen based on the logfilenr .
* What the following part does is to optimize the mapping by using
2015-11-15 18:15:40 +00:00
* dive_to_read = the dive details entry that need to be read using the object_id
2015-09-05 18:24:10 +00:00
* logFileNoToFind = map the logfilenr of the dive details with the object_id = diveid from the get dive logs */
2022-11-12 11:44:29 +00:00
for ( int i = match_dive_and_log ; i < data - > log - > dives - > nr ; i + + ) {
2024-05-01 13:44:35 +00:00
if ( ! get_matching_dive ( i , newmax , & uemis_mem_status , data , mountpath , deviceidnr ) )
2015-09-05 18:24:10 +00:00
break ;
2015-09-16 07:10:56 +00:00
if ( import_thread_cancelled )
break ;
2015-04-22 03:29:20 +00:00
}
2015-09-05 18:24:10 +00:00
start = end ;
/* Do some memory checking here */
2022-11-12 11:44:29 +00:00
uemis_mem_status = get_memory ( data - > log - > dives , UEMIS_CHECK_LOG ) ;
2015-11-10 02:00:16 +00:00
if ( uemis_mem_status ! = UEMIS_MEM_OK ) {
# if UEMIS_DEBUG & 4
2024-04-24 09:45:21 +00:00
report_info ( " d_u_i out of memory, bailing \n " ) ;
2015-11-10 02:00:16 +00:00
# endif
2024-05-01 13:44:35 +00:00
mbuf = uemis_get_answer ( mountpath , " terminateSync " , 0 , 3 , result ) ;
2022-02-06 21:05:43 +00:00
const char * errormsg = translate ( " gettextFromC " , ACTION_RECONNECT ) ;
for ( int wait = 60 ; wait > = 0 ; wait - - ) {
uemis_info ( " %s %ds " , errormsg , wait ) ;
usleep ( 1000000 ) ;
}
// Resetting to original state
filenr = 0 ;
max_mem_used = - 1 ;
2022-11-12 11:44:29 +00:00
uemis_mem_status = get_memory ( data - > log - > dives , UEMIS_CHECK_DETAILS ) ;
2024-05-01 13:44:35 +00:00
if ( uemis_get_answer ( mountpath , " getDeviceId " , 0 , 1 , result ) . empty ( ) )
2022-02-06 21:05:43 +00:00
goto bail ;
2024-04-29 05:02:54 +00:00
if ( deviceid ! = param_buff [ 0 ] ) {
2024-03-24 20:03:08 +00:00
report_info ( " Uemis: Device id has changed after reconnect! " ) ;
2022-02-06 21:05:43 +00:00
goto bail ;
}
2024-04-29 05:02:54 +00:00
param_buff [ 0 ] = deviceid ;
2024-05-01 13:44:35 +00:00
if ( uemis_get_answer ( mountpath , " initSession " , 1 , 6 , result ) . empty ( ) )
2022-02-06 21:05:43 +00:00
goto bail ;
uemis_info ( translate ( " gettextFromC " , " Start download " ) ) ;
2024-05-01 13:44:35 +00:00
if ( uemis_get_answer ( mountpath , " processSync " , 0 , 2 , result ) . empty ( ) )
2022-02-06 21:05:43 +00:00
goto bail ;
param_buff [ 1 ] = " notempty " ;
2015-11-10 02:00:16 +00:00
}
2015-09-05 18:24:10 +00:00
/* if the user clicked cancel, exit gracefully */
2015-11-10 02:00:16 +00:00
if ( import_thread_cancelled ) {
# if UEMIS_DEBUG & 4
2024-04-24 09:45:21 +00:00
report_info ( " d_u_i thread canceled, bailing \n " ) ;
2015-11-10 02:00:16 +00:00
# endif
2015-09-05 18:24:10 +00:00
break ;
2015-11-10 02:00:16 +00:00
}
2015-09-05 18:24:10 +00:00
/* if we got an error or got nothing back, stop trying */
2024-04-29 05:02:54 +00:00
if ( ! success | | param_buff [ 3 ] . empty ( ) ) {
2015-11-10 02:00:16 +00:00
# if UEMIS_DEBUG & 4
2024-04-24 09:45:21 +00:00
report_info ( " d_u_i after download nothing found, giving up \n " ) ;
2015-11-10 02:00:16 +00:00
# endif
2015-09-05 18:24:10 +00:00
break ;
2015-11-10 02:00:16 +00:00
}
2015-09-05 18:24:10 +00:00
# if UEMIS_DEBUG & 2
2015-09-15 08:09:24 +00:00
if ( debug_round ! = - 1 )
2015-11-10 02:00:16 +00:00
if ( debug_round - - = = 0 ) {
2024-04-24 09:45:21 +00:00
report_info ( " d_u_i debug_round is now 0, bailing \n " ) ;
2015-09-05 18:24:10 +00:00
goto bail ;
2015-11-10 02:00:16 +00:00
}
2015-09-05 18:24:10 +00:00
# endif
} else {
2017-02-20 09:30:01 +00:00
/* some of the loading from the UEMIS failed at the dive log level
2017-02-20 09:36:49 +00:00
* if the memory status = full , we can ' t even load the dive spots and / or buddies .
2017-02-20 09:30:01 +00:00
* The loaded block of dive logs is useless and all new loaded dive logs need to
2015-09-05 18:24:10 +00:00
* be deleted from the download_table .
*/
if ( uemis_mem_status = = UEMIS_MEM_FULL )
2022-11-12 11:44:29 +00:00
do_delete_dives ( data - > log - > dives , match_dive_and_log ) ;
2015-11-10 02:00:16 +00:00
# if UEMIS_DEBUG & 4
2024-04-24 09:45:21 +00:00
report_info ( " d_u_i out of memory, bailing instead of processing \n " ) ;
2015-11-10 02:00:16 +00:00
# endif
2012-09-27 03:37:57 +00:00
break ;
}
2012-09-25 14:28:47 +00:00
}
2015-09-05 18:24:10 +00:00
2024-04-25 04:25:36 +00:00
if ( end = = - 2 )
end = newmax ;
2015-09-05 18:24:10 +00:00
2012-12-05 05:03:56 +00:00
# if UEMIS_DEBUG & 2
2024-04-24 09:45:21 +00:00
report_info ( " Done: read from object_id %d to %d \n " , first , end ) ;
2012-10-26 03:15:39 +00:00
# endif
2015-09-05 18:24:10 +00:00
/* Regardless on where we are with the memory situation, it's time now
* to see if we have to clean some dead bodies from our download table */
2024-05-01 14:59:22 +00:00
for ( int next_table_index = 0 ; next_table_index < data - > log - > dives - > nr ; ) {
2022-11-12 11:44:29 +00:00
if ( data - > log - > dives - > dives [ next_table_index ] - > hidden_by_filter )
uemis_delete_dive ( data , data - > log - > dives - > dives [ next_table_index ] - > dc . diveid ) ;
2015-09-05 18:24:10 +00:00
else
next_table_index + + ;
}
2015-09-15 14:46:20 +00:00
if ( uemis_mem_status ! = UEMIS_MEM_OK )
result = translate ( " gettextFromC " , ERR_FS_ALMOST_FULL ) ;
2023-04-02 06:28:33 +00:00
if ( data - > sync_time )
uemis_info ( translate ( " gettextFromC " , " Time sync not supported by dive computer " ) ) ;
2012-11-19 22:11:08 +00:00
bail :
2024-04-29 05:02:54 +00:00
( void ) uemis_get_answer ( mountpath , " terminateSync " , 0 , 3 , result ) ;
if ( param_buff [ 0 ] = = " error " ) {
if ( param_buff [ 2 ] = = " Out of Memory " )
2014-02-28 04:09:57 +00:00
result = translate ( " gettextFromC " , ERR_FS_FULL ) ;
2012-09-27 03:37:57 +00:00
else
result = param_buff [ 2 ] ;
}
2022-11-12 11:44:29 +00:00
if ( ! data - > log - > dives - > nr )
2015-09-24 17:26:35 +00:00
result = translate ( " gettextFromC " , ERR_NO_FILES ) ;
2012-09-27 03:37:57 +00:00
return result ;
2012-09-25 14:28:47 +00:00
}