Merge branch 'timesync'

This commit is contained in:
Jef Driesen 2017-08-18 23:23:36 +02:00
commit 838f730fb2
45 changed files with 227 additions and 27 deletions

View File

@ -17,6 +17,7 @@ dctool_SOURCES = \
dctool_parse.c \
dctool_read.c \
dctool_write.c \
dctool_timesync.c \
dctool_fwupdate.c \
output.h \
output-private.h \

View File

@ -63,6 +63,7 @@ static const dctool_command_t *g_commands[] = {
&dctool_parse,
&dctool_read,
&dctool_write,
&dctool_timesync,
&dctool_fwupdate,
NULL
};

View File

@ -50,6 +50,7 @@ extern const dctool_command_t dctool_dump;
extern const dctool_command_t dctool_parse;
extern const dctool_command_t dctool_read;
extern const dctool_command_t dctool_write;
extern const dctool_command_t dctool_timesync;
extern const dctool_command_t dctool_fwupdate;
const dctool_command_t *

162
examples/dctool_timesync.c Normal file
View File

@ -0,0 +1,162 @@
/*
* libdivecomputer
*
* Copyright (C) 2017 Jef Driesen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <libdivecomputer/context.h>
#include <libdivecomputer/descriptor.h>
#include <libdivecomputer/device.h>
#include "dctool.h"
#include "common.h"
#include "utils.h"
static dc_status_t
do_timesync (dc_context_t *context, dc_descriptor_t *descriptor, const char *devname, const dc_datetime_t *datetime)
{
dc_status_t rc = DC_STATUS_SUCCESS;
dc_device_t *device = NULL;
// Open the device.
message ("Opening the device (%s %s, %s).\n",
dc_descriptor_get_vendor (descriptor),
dc_descriptor_get_product (descriptor),
devname ? devname : "null");
rc = dc_device_open (&device, context, descriptor, devname);
if (rc != DC_STATUS_SUCCESS) {
ERROR ("Error opening the device.");
goto cleanup;
}
// Register the event handler.
message ("Registering the event handler.\n");
int events = DC_EVENT_WAITING | DC_EVENT_PROGRESS | DC_EVENT_DEVINFO | DC_EVENT_CLOCK | DC_EVENT_VENDOR;
rc = dc_device_set_events (device, events, dctool_event_cb, NULL);
if (rc != DC_STATUS_SUCCESS) {
ERROR ("Error registering the event handler.");
goto cleanup;
}
// Register the cancellation handler.
message ("Registering the cancellation handler.\n");
rc = dc_device_set_cancel (device, dctool_cancel_cb, NULL);
if (rc != DC_STATUS_SUCCESS) {
ERROR ("Error registering the cancellation handler.");
goto cleanup;
}
// Syncronize the device clock.
message ("Syncronize the device clock.\n");
rc = dc_device_timesync (device, datetime);
if (rc != DC_STATUS_SUCCESS) {
ERROR ("Error syncronizing the device clock.");
goto cleanup;
}
cleanup:
dc_device_close (device);
return rc;
}
static int
dctool_timesync_run (int argc, char *argv[], dc_context_t *context, dc_descriptor_t *descriptor)
{
int exitcode = EXIT_SUCCESS;
dc_status_t status = DC_STATUS_SUCCESS;
// Default option values.
unsigned int help = 0;
// Parse the command-line options.
int opt = 0;
const char *optstring = "h";
#ifdef HAVE_GETOPT_LONG
struct option options[] = {
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0 }
};
while ((opt = getopt_long (argc, argv, optstring, options, NULL)) != -1) {
#else
while ((opt = getopt (argc, argv, optstring)) != -1) {
#endif
switch (opt) {
case 'h':
help = 1;
break;
default:
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
// Show help message.
if (help) {
dctool_command_showhelp (&dctool_timesync);
return EXIT_SUCCESS;
}
// Get the system time.
dc_datetime_t datetime = {0};
dc_ticks_t now = dc_datetime_now ();
if (!dc_datetime_localtime(&datetime, now)) {
message ("ERROR: Failed to get the system time.\n");
exitcode = EXIT_FAILURE;
goto cleanup;
}
// Synchronize the device clock.
status = do_timesync (context, descriptor, argv[0], &datetime);
if (status != DC_STATUS_SUCCESS) {
message ("ERROR: %s\n", dctool_errmsg (status));
exitcode = EXIT_FAILURE;
goto cleanup;
}
cleanup:
return exitcode;
}
const dctool_command_t dctool_timesync = {
dctool_timesync_run,
DCTOOL_CONFIG_DESCRIPTOR,
"timesync",
"Synchronize the device clock",
"Usage:\n"
" dctool timesync [options]\n"
"\n"
"Options:\n"
#ifdef HAVE_GETOPT_LONG
" -h, --help Show help message\n"
#else
" -h Show help message\n"
#endif
};

View File

@ -96,6 +96,9 @@ dc_device_dump (dc_device_t *device, dc_buffer_t *buffer);
dc_status_t
dc_device_foreach (dc_device_t *device, dc_dive_callback_t callback, void *userdata);
dc_status_t
dc_device_timesync (dc_device_t *device, const dc_datetime_t *datetime);
dc_status_t
dc_device_close (dc_device_t *device);

View File

@ -36,9 +36,6 @@ extern "C" {
dc_status_t
hw_frog_device_version (dc_device_t *device, unsigned char data[], unsigned int size);
dc_status_t
hw_frog_device_clock (dc_device_t *device, const dc_datetime_t *datetime);
dc_status_t
hw_frog_device_display (dc_device_t *device, const char *text);

View File

@ -43,9 +43,6 @@ typedef enum hw_ostc_format_t {
dc_status_t
hw_ostc_device_md2hash (dc_device_t *device, unsigned char data[], unsigned int size);
dc_status_t
hw_ostc_device_clock (dc_device_t *device, const dc_datetime_t *datetime);
dc_status_t
hw_ostc_device_eeprom_read (dc_device_t *device, unsigned int bank, unsigned char data[], unsigned int size);

View File

@ -39,9 +39,6 @@ hw_ostc3_device_version (dc_device_t *device, unsigned char data[], unsigned int
dc_status_t
hw_ostc3_device_hardware (dc_device_t *device, unsigned char data[], unsigned int size);
dc_status_t
hw_ostc3_device_clock (dc_device_t *device, const dc_datetime_t *datetime);
dc_status_t
hw_ostc3_device_display (dc_device_t *device, const char *text);

View File

@ -75,6 +75,7 @@ static const dc_device_vtable_t atomics_cobalt_device_vtable = {
NULL, /* write */
NULL, /* dump */
atomics_cobalt_device_foreach, /* foreach */
NULL, /* timesync */
atomics_cobalt_device_close /* close */
};

View File

@ -51,6 +51,7 @@ static const dc_device_vtable_t citizen_aqualand_device_vtable = {
NULL, /* write */
citizen_aqualand_device_dump, /* dump */
citizen_aqualand_device_foreach, /* foreach */
NULL, /* timesync */
citizen_aqualand_device_close /* close */
};

View File

@ -114,6 +114,7 @@ static const dc_device_vtable_t cochran_commander_device_vtable = {
NULL, /* write */
cochran_commander_device_dump, /* dump */
cochran_commander_device_foreach, /* foreach */
NULL, /* timesync */
cochran_commander_device_close /* close */
};

View File

@ -75,6 +75,7 @@ static const dc_device_vtable_t cressi_edy_device_vtable = {
NULL, /* write */
cressi_edy_device_dump, /* dump */
cressi_edy_device_foreach, /* foreach */
NULL, /* timesync */
cressi_edy_device_close /* close */
};

View File

@ -67,6 +67,7 @@ static const dc_device_vtable_t cressi_leonardo_device_vtable = {
NULL, /* write */
cressi_leonardo_device_dump, /* dump */
cressi_leonardo_device_foreach, /* foreach */
NULL, /* timesync */
cressi_leonardo_device_close /* close */
};

View File

@ -71,6 +71,8 @@ struct dc_device_vtable_t {
dc_status_t (*foreach) (dc_device_t *device, dc_dive_callback_t callback, void *userdata);
dc_status_t (*timesync) (dc_device_t *device, const dc_datetime_t *datetime);
dc_status_t (*close) (dc_device_t *device);
};

View File

@ -370,6 +370,19 @@ dc_device_foreach (dc_device_t *device, dc_dive_callback_t callback, void *userd
}
dc_status_t
dc_device_timesync (dc_device_t *device, const dc_datetime_t *datetime)
{
if (device == NULL)
return DC_STATUS_UNSUPPORTED;
if (device->vtable->timesync == NULL)
return DC_STATUS_UNSUPPORTED;
return device->vtable->timesync (device, datetime);
}
dc_status_t
dc_device_close (dc_device_t *device)
{

View File

@ -69,6 +69,7 @@ static const dc_device_vtable_t diverite_nitekq_device_vtable = {
NULL, /* write */
diverite_nitekq_device_dump, /* dump */
diverite_nitekq_device_foreach, /* foreach */
NULL, /* timesync */
diverite_nitekq_device_close /* close */
};

View File

@ -86,6 +86,7 @@ static const dc_device_vtable_t divesystem_idive_device_vtable = {
NULL, /* write */
NULL, /* dump */
divesystem_idive_device_foreach, /* foreach */
NULL, /* timesync */
divesystem_idive_device_close /* close */
};

View File

@ -61,6 +61,7 @@ typedef struct hw_frog_device_t {
static dc_status_t hw_frog_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
static dc_status_t hw_frog_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
static dc_status_t hw_frog_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime);
static dc_status_t hw_frog_device_close (dc_device_t *abstract);
static const dc_device_vtable_t hw_frog_device_vtable = {
@ -71,6 +72,7 @@ static const dc_device_vtable_t hw_frog_device_vtable = {
NULL, /* write */
NULL, /* dump */
hw_frog_device_foreach, /* foreach */
hw_frog_device_timesync, /* timesync */
hw_frog_device_close /* close */
};
@ -482,14 +484,11 @@ hw_frog_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void
}
dc_status_t
hw_frog_device_clock (dc_device_t *abstract, const dc_datetime_t *datetime)
static dc_status_t
hw_frog_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime)
{
hw_frog_device_t *device = (hw_frog_device_t *) abstract;
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
if (datetime == NULL) {
ERROR (abstract->context, "Invalid parameter specified.");
return DC_STATUS_INVALIDARGS;

View File

@ -70,6 +70,7 @@ typedef struct hw_ostc_firmware_t {
static dc_status_t hw_ostc_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
static dc_status_t hw_ostc_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
static dc_status_t hw_ostc_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
static dc_status_t hw_ostc_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime);
static dc_status_t hw_ostc_device_close (dc_device_t *abstract);
static const dc_device_vtable_t hw_ostc_device_vtable = {
@ -80,6 +81,7 @@ static const dc_device_vtable_t hw_ostc_device_vtable = {
NULL, /* write */
hw_ostc_device_dump, /* dump */
hw_ostc_device_foreach, /* foreach */
hw_ostc_device_timesync, /* timesync */
hw_ostc_device_close /* close */
};
@ -377,15 +379,12 @@ hw_ostc_device_md2hash (dc_device_t *abstract, unsigned char data[], unsigned in
}
dc_status_t
hw_ostc_device_clock (dc_device_t *abstract, const dc_datetime_t *datetime)
static dc_status_t
hw_ostc_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime)
{
dc_status_t status = DC_STATUS_SUCCESS;
hw_ostc_device_t *device = (hw_ostc_device_t *) abstract;
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
if (datetime == NULL) {
ERROR (abstract->context, "Invalid parameter specified.");
return DC_STATUS_INVALIDARGS;

View File

@ -129,6 +129,7 @@ static dc_status_t hw_ostc3_device_read (dc_device_t *abstract, unsigned int add
static dc_status_t hw_ostc3_device_write (dc_device_t *abstract, unsigned int address, const unsigned char data[], unsigned int size);
static dc_status_t hw_ostc3_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
static dc_status_t hw_ostc3_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
static dc_status_t hw_ostc3_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime);
static dc_status_t hw_ostc3_device_close (dc_device_t *abstract);
static const dc_device_vtable_t hw_ostc3_device_vtable = {
@ -139,6 +140,7 @@ static const dc_device_vtable_t hw_ostc3_device_vtable = {
hw_ostc3_device_write, /* write */
hw_ostc3_device_dump, /* dump */
hw_ostc3_device_foreach, /* foreach */
hw_ostc3_device_timesync, /* timesync */
hw_ostc3_device_close /* close */
};
@ -801,14 +803,11 @@ hw_ostc3_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, voi
}
dc_status_t
hw_ostc3_device_clock (dc_device_t *abstract, const dc_datetime_t *datetime)
static dc_status_t
hw_ostc3_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime)
{
hw_ostc3_device_t *device = (hw_ostc3_device_t *) abstract;
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
if (datetime == NULL) {
ERROR (abstract->context, "Invalid parameter specified.");
return DC_STATUS_INVALIDARGS;

View File

@ -56,6 +56,7 @@ dc_device_read
dc_device_set_cancel
dc_device_set_events
dc_device_set_fingerprint
dc_device_timesync
dc_device_write
oceanic_atom2_device_version
@ -79,19 +80,16 @@ suunto_eon_device_write_name
suunto_vyper2_device_version
suunto_vyper2_device_reset_maxdepth
hw_ostc_device_md2hash
hw_ostc_device_clock
hw_ostc_device_eeprom_read
hw_ostc_device_eeprom_write
hw_ostc_device_reset
hw_ostc_device_screenshot
hw_ostc_device_fwupdate
hw_frog_device_version
hw_frog_device_clock
hw_frog_device_display
hw_frog_device_customtext
hw_ostc3_device_version
hw_ostc3_device_hardware
hw_ostc3_device_clock
hw_ostc3_device_display
hw_ostc3_device_customtext
hw_ostc3_device_config_read

View File

@ -70,6 +70,7 @@ static const dc_device_vtable_t mares_darwin_device_vtable = {
NULL, /* write */
mares_darwin_device_dump, /* dump */
mares_darwin_device_foreach, /* foreach */
NULL, /* timesync */
mares_darwin_device_close /* close */
};

View File

@ -87,6 +87,7 @@ static const dc_device_vtable_t mares_iconhd_device_vtable = {
NULL, /* write */
mares_iconhd_device_dump, /* dump */
mares_iconhd_device_foreach, /* foreach */
NULL, /* timesync */
mares_iconhd_device_close /* close */
};

View File

@ -62,6 +62,7 @@ static const dc_device_vtable_t mares_nemo_device_vtable = {
NULL, /* write */
mares_nemo_device_dump, /* dump */
mares_nemo_device_foreach, /* foreach */
NULL, /* timesync */
mares_nemo_device_close /* close */
};

View File

@ -57,6 +57,7 @@ static const dc_device_vtable_t mares_puck_device_vtable = {
NULL, /* write */
mares_puck_device_dump, /* dump */
mares_puck_device_foreach, /* foreach */
NULL, /* timesync */
mares_puck_device_close /* close */
};

View File

@ -74,6 +74,7 @@ static const oceanic_common_device_vtable_t oceanic_atom2_device_vtable = {
oceanic_atom2_device_write, /* write */
oceanic_common_device_dump, /* dump */
oceanic_common_device_foreach, /* foreach */
NULL, /* timesync */
oceanic_atom2_device_close /* close */
},
oceanic_common_device_logbook,

View File

@ -56,6 +56,7 @@ static const oceanic_common_device_vtable_t oceanic_veo250_device_vtable = {
NULL, /* write */
oceanic_common_device_dump, /* dump */
oceanic_common_device_foreach, /* foreach */
NULL, /* timesync */
oceanic_veo250_device_close /* close */
},
oceanic_common_device_logbook,

View File

@ -68,6 +68,7 @@ static const oceanic_common_device_vtable_t oceanic_vtpro_device_vtable = {
NULL, /* write */
oceanic_common_device_dump, /* dump */
oceanic_common_device_foreach, /* foreach */
NULL, /* timesync */
oceanic_vtpro_device_close /* close */
},
oceanic_vtpro_device_logbook,

View File

@ -57,6 +57,7 @@ static const dc_device_vtable_t reefnet_sensus_device_vtable = {
NULL, /* write */
reefnet_sensus_device_dump, /* dump */
reefnet_sensus_device_foreach, /* foreach */
NULL, /* timesync */
reefnet_sensus_device_close /* close */
};

View File

@ -56,6 +56,7 @@ static const dc_device_vtable_t reefnet_sensuspro_device_vtable = {
NULL, /* write */
reefnet_sensuspro_device_dump, /* dump */
reefnet_sensuspro_device_foreach, /* foreach */
NULL, /* timesync */
reefnet_sensuspro_device_close /* close */
};

View File

@ -65,6 +65,7 @@ static const dc_device_vtable_t reefnet_sensusultra_device_vtable = {
NULL, /* write */
reefnet_sensusultra_device_dump, /* dump */
reefnet_sensusultra_device_foreach, /* foreach */
NULL, /* timesync */
reefnet_sensusultra_device_close /* close */
};

View File

@ -56,6 +56,7 @@ static const dc_device_vtable_t shearwater_petrel_device_vtable = {
NULL, /* write */
NULL, /* dump */
shearwater_petrel_device_foreach, /* foreach */
NULL, /* timesync */
shearwater_petrel_device_close /* close */
};

View File

@ -57,6 +57,7 @@ static const dc_device_vtable_t shearwater_predator_device_vtable = {
NULL, /* write */
shearwater_predator_device_dump, /* dump */
shearwater_predator_device_foreach, /* foreach */
NULL, /* timesync */
shearwater_predator_device_close /* close */
};

View File

@ -58,6 +58,7 @@ static const suunto_common2_device_vtable_t suunto_d9_device_vtable = {
suunto_common2_device_write, /* write */
suunto_common2_device_dump, /* dump */
suunto_common2_device_foreach, /* foreach */
NULL, /* timesync */
suunto_d9_device_close /* close */
},
suunto_d9_device_packet

View File

@ -51,6 +51,7 @@ static const dc_device_vtable_t suunto_eon_device_vtable = {
NULL, /* write */
suunto_eon_device_dump, /* dump */
suunto_eon_device_foreach, /* foreach */
NULL, /* timesync */
suunto_eon_device_close /* close */
};

View File

@ -81,6 +81,7 @@ static const dc_device_vtable_t suunto_eonsteel_device_vtable = {
NULL, /* write */
NULL, /* dump */
suunto_eonsteel_device_foreach, /* foreach */
NULL, /* timesync */
suunto_eonsteel_device_close /* close */
};

View File

@ -54,6 +54,7 @@ static const dc_device_vtable_t suunto_solution_device_vtable = {
NULL, /* write */
suunto_solution_device_dump, /* dump */
suunto_solution_device_foreach, /* foreach */
NULL, /* timesync */
suunto_solution_device_close /* close */
};

View File

@ -63,6 +63,7 @@ static const dc_device_vtable_t suunto_vyper_device_vtable = {
suunto_vyper_device_write, /* write */
suunto_vyper_device_dump, /* dump */
suunto_vyper_device_foreach, /* foreach */
NULL, /* timesync */
suunto_vyper_device_close /* close */
};

View File

@ -50,6 +50,7 @@ static const suunto_common2_device_vtable_t suunto_vyper2_device_vtable = {
suunto_common2_device_write, /* write */
suunto_common2_device_dump, /* dump */
suunto_common2_device_foreach, /* foreach */
NULL, /* timesync */
suunto_vyper2_device_close /* close */
},
suunto_vyper2_device_packet

View File

@ -62,6 +62,7 @@ static const dc_device_vtable_t uwatec_aladin_device_vtable = {
NULL, /* write */
uwatec_aladin_device_dump, /* dump */
uwatec_aladin_device_foreach, /* foreach */
NULL, /* timesync */
uwatec_aladin_device_close /* close */
};

View File

@ -54,6 +54,7 @@ static const dc_device_vtable_t uwatec_g2_device_vtable = {
NULL, /* write */
uwatec_g2_device_dump, /* dump */
uwatec_g2_device_foreach, /* foreach */
NULL, /* timesync */
uwatec_g2_device_close /* close */
};

View File

@ -58,6 +58,7 @@ static const dc_device_vtable_t uwatec_memomouse_device_vtable = {
NULL, /* write */
uwatec_memomouse_device_dump, /* dump */
uwatec_memomouse_device_foreach, /* foreach */
NULL, /* timesync */
uwatec_memomouse_device_close /* close */
};

View File

@ -56,6 +56,7 @@ static const dc_device_vtable_t uwatec_meridian_device_vtable = {
NULL, /* write */
uwatec_meridian_device_dump, /* dump */
uwatec_meridian_device_foreach, /* foreach */
NULL, /* timesync */
uwatec_meridian_device_close /* close */
};

View File

@ -52,6 +52,7 @@ static const dc_device_vtable_t uwatec_smart_device_vtable = {
NULL, /* write */
uwatec_smart_device_dump, /* dump */
uwatec_smart_device_foreach, /* foreach */
NULL, /* timesync */
uwatec_smart_device_close /* close */
};

View File

@ -64,6 +64,7 @@ static const dc_device_vtable_t zeagle_n2ition3_device_vtable = {
NULL, /* write */
zeagle_n2ition3_device_dump, /* dump */
zeagle_n2ition3_device_foreach, /* foreach */
NULL, /* timesync */
zeagle_n2ition3_device_close /* close */
};