[PATCH 4/6] Remote cochran_download example program.
John Van Ostrand
john at vanostrand.com
Mon Jan 11 07:22:56 PST 2016
---
examples/Makefile.am | 5 +-
examples/cochran_download.c | 372 --------------------------------------------
2 files changed, 1 insertion(+), 376 deletions(-)
delete mode 100644 examples/cochran_download.c
diff --git a/examples/Makefile.am b/examples/Makefile.am
index aaa7e4f..12bca6f 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -3,8 +3,7 @@ LDADD = $(top_builddir)/src/libdivecomputer.la
AM_LDFLAGS = -lm
bin_PROGRAMS = \
- dctool \
- cochran_download
+ dctool
dctool_SOURCES = \
common.h \
@@ -21,5 +20,3 @@ dctool_SOURCES = \
dctool_fwupdate.c \
utils.h \
utils.c
-
-cochran_download_SOURCES = cochran_download.c
diff --git a/examples/cochran_download.c b/examples/cochran_download.c
deleted file mode 100644
index 01df25d..0000000
--- a/examples/cochran_download.c
+++ /dev/null
@@ -1,372 +0,0 @@
-/*
- * cochran_download
- *
- * Copyright (C) 2014 John Van Ostrand
- *
- * 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
- */
-
-#include <stdio.h> // fopen, fwrite, fclose
-#include <stdlib.h>
-#include <string.h>
-#include <signal.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <fcntl.h>
-
-
-#include <libdivecomputer/context.h>
-#include <libdivecomputer/device.h>
-#include <libdivecomputer/parser.h>
-#include <libdivecomputer/cochran.h>
-
-
-volatile sig_atomic_t g_cancel = 0;
-
-void
-sighandler (int signum)
-{
- // Restore the default signal handler.
- signal (signum, SIG_DFL);
-
- g_cancel = 1;
-}
-
-static int
-cancel_cb (void *userdata)
-{
- return g_cancel;
-}
-
-static void
-usage (const char *filename)
-{
- fprintf (stderr, "Usage:\n\n");
- fprintf (stderr, " %s [options] devname\n\n", filename);
- fprintf (stderr, "Options:\n\n");
- fprintf (stderr, " -d dirname Dump data to dirname.\n");
- fprintf (stderr, " -f Force dump despite download errors\n");
- fprintf (stderr, " -h Show this help message.\n\n");
- fprintf (stderr, "\n\n");
-}
-
-static dc_status_t
-search (dc_descriptor_t **out, const char *name, dc_family_t backend, unsigned int model)
-{
- dc_status_t rc = DC_STATUS_SUCCESS;
-
- dc_iterator_t *iterator = NULL;
- rc = dc_descriptor_iterator (&iterator);
- if (rc != DC_STATUS_SUCCESS) {
- printf ("Error creating the device descriptor iterator.\n");
- return rc;
- }
-
- dc_descriptor_t *descriptor = NULL, *current = NULL;
- while ((rc = dc_iterator_next (iterator, &descriptor)) == DC_STATUS_SUCCESS)
- {
- if (backend == dc_descriptor_get_type (descriptor)) {
- dc_descriptor_free (current);
- current = descriptor;
- break;
- }
-
- dc_descriptor_free (descriptor);
- }
-
- if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_DONE) {
- dc_descriptor_free (current);
- dc_iterator_free (iterator);
- printf ("Error iterating the device descriptors.\n");
- return rc;
- }
-
- dc_iterator_free (iterator);
-
- *out = current;
-
- return DC_STATUS_SUCCESS;
-}
-
-static dc_status_t
-write_dump(const char *dump, unsigned int size, const char *fname)
-{
- int fd, written;
-
- if (size > 0) {
- fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH );
- if (fd == -1) {
- printf("Unable to open %s for writing\n", fname);
- exit;
- }
- written = write(fd, dump, size);
- if (written != size) {
- printf("Error writing %s. Wrote %d expected %d.\n", fname, written, size);
- return (DC_STATUS_UNSUPPORTED);
- }
- }
- return (DC_STATUS_SUCCESS);
-}
-
-#define hexchar(n) ("0123456789abcdef"[(n) & 15])
-
-static int show_line(unsigned offset, const unsigned char *data,
- unsigned size, int show_empty)
-{
- unsigned char bits;
- int i, off;
- char buffer[120];
-
- if (size > 16)
- size = 16;
-
- bits = 0;
- memset(buffer, ' ', sizeof(buffer));
- off = sprintf(buffer, "%06x ", offset);
- for (i = 0; i < size; i++) {
- char *hex = buffer + off + 3 * i;
- char *asc = buffer + off + 50 + i;
- unsigned char byte = data[i];
-
- hex[0] = hexchar(byte >> 4);
- hex[1] = hexchar(byte);
- bits |= byte;
- if (byte < 32 || byte > 126)
- byte = '.';
- asc[0] = byte;
- asc[1] = 0;
- }
-
- if (bits) {
- puts(buffer);
- return 1;
- }
- if (show_empty)
- puts("...");
- return 0;
-}
-
-static void cochran_debug_write(const unsigned char *data, unsigned size)
-{
- return;
-
- int show = 1, i;
-
-
- for (i = 0; i < size; i += 16)
- show = show_line(i, data + i, size - i, show);
-}
-
-
-#define array_uint32_le(p) ( (unsigned char) (p)[0] + ((unsigned char) (p)[1] << 8) \
- + ((unsigned char) (p)[2] << 16) + ((unsigned char) (p)[3] << 24) )
-
-static dc_status_t
-dowork (dc_context_t *context, dc_descriptor_t *descriptor, const char *devname, const char *dirname, unsigned int force_flag)
-{
- dc_status_t rc = DC_STATUS_SUCCESS;
-
- struct stat dstat;
-
- // Open the device.
- dc_device_t *device = NULL;
- rc = dc_device_open (&device, context, descriptor, devname);
- if (rc != DC_STATUS_SUCCESS) {
- printf ("Error opening %s.\n", devname);
- return rc;
- }
-
- // Register the cancellation handler.
- rc = dc_device_set_cancel (device, cancel_cb, NULL);
- if (rc != DC_STATUS_SUCCESS) {
- printf ("Error registering the cancellation handler.\n");
- dc_device_close (device);
- return rc;
- }
-
- // Create directory if needed
- int src;
- src = stat(dirname, &dstat);
-
- if ( src == -1 ) {
- if (mkdir(dirname, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
- printf("Unable to create directory\n");
- exit;
- }
- }
-
- // TODO Run dump and write files
- dc_buffer_t *dump = dc_buffer_new(0);
-
- rc = dc_device_dump (device, dump);
-
- cochran_debug_write(dc_buffer_get_data(dump), dc_buffer_get_size(dump));
- exit;
-
- if (rc == DC_STATUS_SUCCESS || force_flag) {
- char fname[128];
- char *d = dc_buffer_get_data(dump);
- unsigned int ptr = 0, start, size;
-
- sprintf(fname, "%s/info-x05x9dxffx00x43x00.bin", dirname);
- start = array_uint32_le(d + ptr);
- size = array_uint32_le(d + ptr + 5) - start;
-printf("%s, start = %d, size = %d\n", fname, start, size);
- write_dump(d + start, size, fname);
-
- ptr += 5;
- start = array_uint32_le(d + ptr);
- size = array_uint32_le(d + ptr + 5) - start;
- if (*(d + ptr - 1) == 1)
- sprintf(fname, "%s/info-xdbx7fxffx00x43x00.bin", dirname);
- else
- sprintf(fname, "%s/info-x05x9dxffx00x43x00.bin", dirname);
- write_dump(d + start, size, fname);
-
- ptr += 5;
- start = array_uint32_le(d + ptr);
- size = array_uint32_le(d + ptr + 5) - start;
- sprintf(fname, "%s/config-x96x00.bin", dirname);
-printf("%s, start = %d, size = %d\n", fname, start, size);
- write_dump(d + start, size, fname);
-
- ptr += 5;
- start = array_uint32_le(d + ptr);
- size = array_uint32_le(d + ptr + 5) - start;
- sprintf(fname, "%s/config-x96x01.bin", dirname);
-printf("%s, start = %d, size = %d\n", fname, start, size);
- write_dump(d + start, size, fname);
-
- ptr += 5;
- start = array_uint32_le(d + ptr);
- size = array_uint32_le(d + ptr + 5) - start;
- sprintf(fname, "%s/config-x96x02.bin", dirname);
-printf("%s, start = %d, size = %d\n", fname, start, size);
- write_dump(d + start, size, fname);
-
- ptr += 5;
- start = array_uint32_le(d + ptr);
- size = array_uint32_le(d + ptr + 5) - start;
- sprintf(fname, "%s/config-x96x03.bin", dirname);
-printf("%s, start = %d, size = %d\n", fname, start, size);
- write_dump(d + start, size, fname);
-
- ptr += 5;
- start = array_uint32_le(d + ptr);
- size = array_uint32_le(d + ptr + 5) - start;
- sprintf(fname, "%s/misc-x89x05x00x00x00xdcx05.bin", dirname);
-printf("%s, start = %d, size = %d\n", fname, start, size);
- write_dump(d + start, size, fname);
-
- ptr += 5;
- start = array_uint32_le(d + ptr);
- size = array_uint32_le(d + ptr + 5) - start;
- sprintf(fname, "%s/logbook.bin", dirname);
-printf("%s, start = %d, size = %d\n", fname, start, size);
- write_dump(d + start, size, fname);
-
- ptr += 5;
- start = array_uint32_le(d + ptr);
- size = array_uint32_le(d + ptr + 5) - start;
- sprintf(fname, "%s/sample.bin", dirname);
-printf("%s, start = %d, size = %d\n", fname, start, size);
- write_dump(d + start, size, fname);
-
- dc_buffer_free (dump);
- } else {
- printf("Dive computer read failure\n");
- }
-
- // Close the device.
- rc = dc_device_close (device);
- if (rc != DC_STATUS_SUCCESS) {
- printf ("Error closing the device.\n");
- return rc;
- }
-
- return DC_STATUS_SUCCESS;
-}
-
-
-int
-main (int argc, char *argv[])
-{
- extern char *optarg;
- extern int optind, opterr, optopt;
-
- // Default values.
- dc_family_t backend = DC_FAMILY_COCHRAN_COMMANDER;
-
- // Command line arguments
- const char *devname = NULL, *dirname;
- unsigned int force_flag = 0;
-
-
- // Parse command-line options.
- int opt = 0;
- while ((opt = getopt (argc, argv, "d:ls:h")) != -1) {
- switch (opt) {
- case 'd': // dump data to directory
- dirname = optarg;
- break;
- case 'f': // Force dump despite download error
- force_flag = 1;
- break;
- case '?':
- case 'h':
- default:
- usage (argv[0]);
- return EXIT_FAILURE;
- }
- }
-
- if (optind < argc)
- devname = argv[optind];
-
-
- signal (SIGINT, sighandler);
-
- dc_context_t *context = NULL;
- dc_status_t rc = dc_context_new (&context);
- if (rc != DC_STATUS_SUCCESS) {
- return EXIT_FAILURE;
- }
-
- dc_context_set_loglevel (context, DC_LOGLEVEL_NONE);
- //dc_context_set_logfunc (context, logfunc, NULL);
-
- /* Search for a matching device descriptor. */
- dc_descriptor_t *descriptor = NULL;
- rc = search (&descriptor, NULL, backend, 0);
- if (rc != DC_STATUS_SUCCESS) {
- return EXIT_FAILURE;
- }
-
- /* Fail if no device descriptor found. */
- if (descriptor == NULL) {
- printf ("No matching device found.\n");
- usage (argv[0]);
- return EXIT_FAILURE;
- }
-
- rc = dowork (context, descriptor, devname, dirname, force_flag);
-
- dc_descriptor_free (descriptor);
- dc_context_free (context);
-
- return rc != DC_STATUS_SUCCESS ? EXIT_FAILURE : EXIT_SUCCESS;
-}
--
2.4.3
More information about the devel
mailing list