Esempio n. 1
0
dc_status_t
test_dump_memory (const char* name, const char* filename)
{
	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 ("oceanic_atom2_device_open\n");
	dc_status_t rc = oceanic_atom2_device_open (&device, context, name);
	if (rc != DC_STATUS_SUCCESS) {
		WARNING ("Error opening serial port.");
		dc_context_free (context);
		return rc;
	}

	dc_buffer_t *buffer = dc_buffer_new (0);

	message ("dc_device_dump\n");
	rc = dc_device_dump (device, buffer);
	if (rc != DC_STATUS_SUCCESS) {
		WARNING ("Cannot read memory.");
		dc_buffer_free (buffer);
		dc_device_close (device);
		dc_context_free (context);
		return rc;
	}

	message ("Dumping data\n");
	FILE* fp = fopen (filename, "wb");
	if (fp != NULL) {
		fwrite (dc_buffer_get_data (buffer), sizeof (unsigned char), dc_buffer_get_size (buffer), fp);
		fclose (fp);
	}

	dc_buffer_free (buffer);

	message ("dc_device_foreach\n");
	rc = dc_device_foreach (device, NULL, NULL);
	if (rc != DC_STATUS_SUCCESS) {
		WARNING ("Cannot read dives.");
		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;
}
Esempio n. 2
0
int libdc_driver_transfer(dev_handle_t abstract, void ** buffer, uint32_t * size, device_callback_fn_t dcb, transfer_callback_fn_t pcb, void * userdata)
{
	libdc_device_t dev = (libdc_device_t)(abstract);
	if (dev == NULL)
	{
		errno = EINVAL;
		return -1;
	}

	// Set the Callback Data
	dev->cb_data = userdata;

	/*
	 * NB: libdc_dive_cb reverses the returned order of dives so that the last dive returned
	 *     holds the next transfer token.  This is per the universal.c program included with
	 *     libdivecomputer which takes the _first_ returned token as the next token.
	 */

	// Transfer and Store the Dives (calls device_callback and transfer_callback internally)
	dc_status_t rc = dc_device_foreach (dev->device, libdc_dive_cb, dev);
	if (rc != DC_STATUS_SUCCESS)
	{
		dev->errcode = DRIVER_ERR_READ;
		dev->errmsg = "Failed to read dive data from device";

		dive_list_t * c = dev->dives;
		while (c != NULL)
		{
			free(c->data);
			free(c->token);
			dive_list_t * n = c->next;
			free(c);
			c = n;
		}

		return -1;
	}

	// Flatten the Dive List (supports higher-level dump function)
	* buffer = join_dive_list(dev->dives, size);

	// Free Dive List Memory
	dive_list_t * c = dev->dives;
	while (c != NULL)
	{
		free(c->data);
		free(c->token);
		dive_list_t * n = c->next;
		free(c);
		c = n;
	}

	return DRIVER_ERR_SUCCESS;
}
Esempio n. 3
0
dc_status_t
test_dump_sdm (const char* name)
{
	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 ("suunto_vyper_device_open\n");
	dc_status_t rc = suunto_vyper_device_open (&device, context, name);
	if (rc != DC_STATUS_SUCCESS) {
		WARNING ("Error opening serial port.");
		dc_context_free (context);
		return rc;
	}

	message ("dc_device_foreach\n");
	rc = dc_device_foreach (device, NULL, NULL);
	if (rc != DC_STATUS_SUCCESS) {
		WARNING ("Cannot read dives.");
		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;
}
static dc_status_t
download (dc_context_t *context, dc_descriptor_t *descriptor, const char *devname, const char *cachedir, dc_buffer_t *fingerprint, dctool_output_t *output)
{
	dc_status_t rc = DC_STATUS_SUCCESS;
	dc_device_t *device = NULL;
	dc_buffer_t *ofingerprint = 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;
	}

	// Initialize the event data.
	event_data_t eventdata = {0};
	if (fingerprint) {
		eventdata.cachedir = NULL;
	} else {
		eventdata.cachedir = cachedir;
	}

	// 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, event_cb, &eventdata);
	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;
	}

	// Register the fingerprint data.
	if (fingerprint) {
		message ("Registering the fingerprint data.\n");
		rc = dc_device_set_fingerprint (device, dc_buffer_get_data (fingerprint), dc_buffer_get_size (fingerprint));
		if (rc != DC_STATUS_SUCCESS) {
			ERROR ("Error registering the fingerprint data.");
			goto cleanup;
		}
	}

	// Initialize the dive data.
	dive_data_t divedata = {0};
	divedata.device = device;
	divedata.fingerprint = &ofingerprint;
	divedata.number = 0;
	divedata.output = output;

	// Download the dives.
	message ("Downloading the dives.\n");
	rc = dc_device_foreach (device, dive_cb, &divedata);
	if (rc != DC_STATUS_SUCCESS) {
		ERROR ("Error downloading the dives.");
		goto cleanup;
	}

	// Store the fingerprint data.
	if (cachedir && ofingerprint) {
		char filename[1024] = {0};
		dc_family_t family = DC_FAMILY_NULL;

		// Generate the fingerprint filename.
		family = dc_device_get_type (device);
		snprintf (filename, sizeof (filename), "%s/%s-%08X.bin",
			cachedir, dctool_family_name (family), eventdata.devinfo.serial);

		// Write the fingerprint file.
		dctool_file_write (filename, ofingerprint);
	}

cleanup:
	dc_buffer_free (ofingerprint);
	dc_device_close (device);
	return rc;
}
Esempio n. 5
0
static dc_status_t import_device_data(dc_device_t *device, device_data_t *devicedata)
{
	return dc_device_foreach(device, dive_cb, devicedata);
}
Esempio n. 6
0
static dc_status_t
dowork(dc_context_t *context, dc_descriptor_t *descriptor, program_options_t *options, dc_buffer_t *fingerprint) {
    dc_status_t rc = DC_STATUS_SUCCESS;

    /* initialize the device data */
    device_data_t devdata = {{0}};

    /* open the device */
    message("Opening the device (%s, %s, %s.\n",
            dc_descriptor_get_vendor(descriptor),
            dc_descriptor_get_product(descriptor),
            options->devname ? options->devname : "null");
    dc_device_t *device = NULL;
    rc = dc_device_open(&device, context, descriptor, options->devname);
    if (rc != DC_STATUS_SUCCESS) {
        WARNING("Error opening device.");
        return rc;
    }

    /* register the event handler */
    message("Registering the event handler.\n");
    int events = DC_EVENT_WAITING | DC_EVENT_PROGRESS | DC_EVENT_DEVINFO | DC_EVENT_CLOCK;
    rc = dc_device_set_events(device, events, event_cb, &devdata);
    if (rc != DC_STATUS_SUCCESS) {
        WARNING("Error registering the event handler.");
        dc_device_close(device);
        return rc;
    }

    /* register the cancellation handler */
    message("Registering the cancellation handler.\n");
    rc = dc_device_set_cancel(device, cancel_cb, NULL);
    if (rc != DC_STATUS_SUCCESS) {
        WARNING("Error registering the cancellation handler.");
        dc_device_close(device);
        return rc;
    }

    /* register the fingerprint data */
    if (fingerprint) {
        message("Registering the fingerprint data.\n");
        rc = dc_device_set_fingerprint(device, dc_buffer_get_data(fingerprint), dc_buffer_get_size(fingerprint));
        if (rc != DC_STATUS_SUCCESS) {
            WARNING("Error registerting the fingerprint data");
            dc_device_close(device);
            return rc;
        }
    }

    /* dump the memory if requested */
    if (options->dumpMemory) {
        WARNING("Memory dump not enabled.");
    }

    /* dump the dives if requested */
    if (options->dumpDives) {
        /* initialize the dive data */
        dive_data_t divedata = {0};
        dif_dive_collection_t *dc = dif_dive_collection_alloc();

        divedata.device = device;
        divedata.fingerprint = NULL;
        divedata.number = 0;
        divedata.dc = dc;
        /* download the dives */
        message("Downloading the dives.\n");
        rc = dc_device_foreach(device, dive_cb, &divedata);
        if (rc != DC_STATUS_SUCCESS) {
            WARNING("Error downloading the dives.");
            dc_buffer_free(divedata.fingerprint);
            dc_device_close (device);
            return rc;
        }

        xml_options_t *xmlOptions = dif_xml_options_alloc();
        xmlOptions->filename = options->xmlfile;
        xmlOptions->useInvalidElements = options->useInvalidElements;

        if (options->truncateDives) {
            divedata.dc = dif_alg_dc_truncate_dives(divedata.dc);
        }

        if (options->initialPressureFix) {
            divedata.dc = dif_alg_dc_initial_pressure_fix(divedata.dc);
        }
        dif_save_dive_collection_uddf_options(divedata.dc, xmlOptions);

        /* free the fingerprint buffer */
        dc_buffer_free(divedata.fingerprint);
        dif_dive_collection_free(divedata.dc);
        dif_xml_options_free(xmlOptions);
    }

    /* close the device */
    message("Closing the device.\n");
    rc = dc_device_close(device);
    if (rc != DC_STATUS_SUCCESS) {
        WARNING("Error closing the device.");
        return rc;
    }

    return DC_STATUS_SUCCESS;
}