示例#1
0
static void libdc_event_cb(dc_device_t * device, dc_event_type_t event, const void * data, void * userdata)
{
	libdc_device_t dev = (libdc_device_t)(userdata);
	if (dev == NULL)
		return;

	const dc_event_progress_t * progress = (dc_event_progress_t *)data;
	const dc_event_devinfo_t * devinfo = (dc_event_devinfo_t *)data;
	const dc_event_clock_t * clock = (dc_event_clock_t *)data;

	switch (event)
	{
	case DC_EVENT_PROGRESS:
		if (dev->pcb)
			dev->pcb(dev->cb_data, progress->current, progress->maximum, & dev->cancel);
		break;

	case DC_EVENT_DEVINFO:
		if (dev->dcb)
		{
			// Get the Token
			char * token = NULL;
			int free_token = 0;
			dev->dcb(dev->cb_data, devinfo->model, devinfo->serial, dev->devtime, & token, & free_token);

			if (token != NULL)
			{
				// Base64 Decode the Token
				unsigned char * token_data = NULL;
				size_t token_size = 0;
				token_data = base64_decode(token, strlen(token), & token_size);

				// Set the Token
				if (token_data != NULL)
					dc_device_set_fingerprint(dev->device, token_data, token_size);

				// Free Token Data
				if (free_token)
					free(token);
			}
		}
		break;

	case DC_EVENT_CLOCK:
		dev->devtime = clock->devtime;
		dev->systime = clock->systime;

	default:
		break;
	}
}
static void
event_cb (dc_device_t *device, dc_event_type_t event, const void *data, void *userdata)
{
	const dc_event_devinfo_t *devinfo = (const dc_event_devinfo_t *) data;

	event_data_t *eventdata = (event_data_t *) userdata;

	// Forward to the default event handler.
	dctool_event_cb (device, event, data, userdata);

	switch (event) {
	case DC_EVENT_DEVINFO:
		// Load the fingerprint from the cache. If there is no
		// fingerprint present in the cache, a NULL buffer is returned,
		// and the registered fingerprint will be cleared.
		if (eventdata->cachedir) {
			char filename[1024] = {0};
			dc_family_t family = DC_FAMILY_NULL;
			dc_buffer_t *fingerprint = NULL;

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

			// Read the fingerprint file.
			fingerprint = dctool_file_read (filename);

			// Register the fingerprint data.
			dc_device_set_fingerprint (device,
				dc_buffer_get_data (fingerprint),
				dc_buffer_get_size (fingerprint));

			// Free the buffer again.
			dc_buffer_free (fingerprint);
		}

		// Keep a copy of the event data. It will be used for generating
		// the fingerprint filename again after a (successful) download.
		eventdata->devinfo = *devinfo;
		break;
	default:
		break;
	}
}
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;
}
示例#4
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;
}