From 8e1a8c14a3222301fb873ed78008444266951dae Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sat, 27 Jul 2013 06:54:14 +0200 Subject: [PATCH] Add a small example application. --- examples/Makefile.am | 3 + examples/hw_ostc_fwupdate.c | 126 ++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 examples/hw_ostc_fwupdate.c diff --git a/examples/Makefile.am b/examples/Makefile.am index fc0664d..2283028 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -21,6 +21,7 @@ bin_PROGRAMS = \ darwin \ iconhd \ ostc \ + ostc-fwupdate \ frog \ edy \ leonardo \ @@ -72,6 +73,8 @@ iconhd_SOURCES = mares_iconhd_test.c $(COMMON) ostc_SOURCES = hw_ostc_test.c $(COMMON) +ostc_fwupdate_SOURCES = hw_ostc_fwupdate.c $(COMMON) + frog_SOURCES = hw_frog_test.c $(COMMON) edy_SOURCES = cressi_edy_test.c $(COMMON) diff --git a/examples/hw_ostc_fwupdate.c b/examples/hw_ostc_fwupdate.c new file mode 100644 index 0000000..a9e7fde --- /dev/null +++ b/examples/hw_ostc_fwupdate.c @@ -0,0 +1,126 @@ +/* + * libdivecomputer + * + * Copyright (C) 2013 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 + */ + +#include // fopen, fwrite, fclose +#include + +#include + +#include "utils.h" +#include "common.h" + +static void +event_cb (dc_device_t *device, dc_event_type_t event, const void *data, void *userdata) +{ + const dc_event_progress_t *progress = (dc_event_progress_t *) data; + + switch (event) { + case DC_EVENT_PROGRESS: + message ("Event: progress %3.2f%% (%u/%u)\n", + 100.0 * (double) progress->current / (double) progress->maximum, + progress->current, progress->maximum); + break; + default: + break; + } +} + +static dc_status_t +fwupdate (const char *name, const char *hexfile) +{ + dc_context_t *context = NULL; + dc_device_t *device = NULL; + + dc_context_new (&context); + dc_context_set_loglevel (context, DC_LOGLEVEL_ALL); + dc_context_set_logfunc (context, logfunc, NULL); + + message ("hw_ostc_device_open\n"); + dc_status_t rc = hw_ostc_device_open (&device, context, name); + if (rc != DC_STATUS_SUCCESS) { + WARNING ("Error opening serial port."); + dc_context_free (context); + return rc; + } + + message ("dc_device_set_events.\n"); + rc = dc_device_set_events (device, DC_EVENT_PROGRESS, event_cb, NULL); + if (rc != DC_STATUS_SUCCESS) { + WARNING ("Error registering the event handler."); + dc_device_close (device); + dc_context_free (context); + return rc; + } + + message ("hw_ostc_device_fwupdate\n"); + rc = hw_ostc_device_fwupdate (device, hexfile); + if (rc != DC_STATUS_SUCCESS) { + WARNING ("Error flashing firmware."); + dc_device_close (device); + dc_context_free (context); + return rc; + } + + message ("dc_device_close\n"); + rc = dc_device_close (device); + if (rc != DC_STATUS_SUCCESS) { + WARNING ("Cannot close device."); + dc_context_free (context); + return rc; + } + + dc_context_free (context); + + return DC_STATUS_SUCCESS; +} + + +int main(int argc, char *argv[]) +{ + message_set_logfile ("OSTC-FWUPDATE.LOG"); + +#ifdef _WIN32 + const char* name = "COM1"; +#else + const char* name = "/dev/ttyUSB0"; +#endif + const char *hexfile = NULL; + + if (argc > 1) { + name = argv[1]; + } + if (argc > 2) { + hexfile = argv[2]; + } + + message ("DEVICE=%s\n", name); + message ("HEXFILE=%s\n", hexfile); + + dc_status_t a = fwupdate (name, hexfile); + + message ("SUMMARY\n"); + message ("-------\n"); + message ("fwupdate: %s\n", errmsg (a)); + + message_set_logfile (NULL); + + return 0; +}