Пример #1
0
void libdc_parser_close(parser_handle_t abstract)
{
	libdc_parser_t parser = (libdc_parser_t)(abstract);
	if (parser == NULL)
		return;

	if (parser->parser != NULL)
		dc_parser_destroy(parser->parser);

	parser->parser = NULL;

	free(parser);
}
Пример #2
0
static int
dive_cb (const unsigned char *data, unsigned int size, const unsigned char *fingerprint, unsigned int fsize, void *userdata)
{
	dive_data_t *divedata = (dive_data_t *) userdata;
	dc_status_t rc = DC_STATUS_SUCCESS;
	dc_parser_t *parser = NULL;

	divedata->number++;

	message ("Dive: number=%u, size=%u, fingerprint=", divedata->number, size);
	for (unsigned int i = 0; i < fsize; ++i)
		message ("%02X", fingerprint[i]);
	message ("\n");

	// Keep a copy of the most recent fingerprint. Because dives are
	// guaranteed to be downloaded in reverse order, the most recent
	// dive is always the first dive.
	if (divedata->number == 1) {
		dc_buffer_t *fp = dc_buffer_new (fsize);
		dc_buffer_append (fp, fingerprint, fsize);
		*divedata->fingerprint = fp;
	}

	// Create the parser.
	message ("Creating the parser.\n");
	rc = dc_parser_new (&parser, divedata->device);
	if (rc != DC_STATUS_SUCCESS) {
		ERROR ("Error creating the parser.");
		goto cleanup;
	}

	// Register the data.
	message ("Registering the data.\n");
	rc = dc_parser_set_data (parser, data, size);
	if (rc != DC_STATUS_SUCCESS) {
		ERROR ("Error registering the data.");
		goto cleanup;
	}

	// Parse the dive data.
	message ("Parsing the dive data.\n");
	rc = dctool_output_write (divedata->output, parser, data, size, fingerprint, fsize);
	if (rc != DC_STATUS_SUCCESS) {
		ERROR ("Error parsing the dive data.");
		goto cleanup;
	}

cleanup:
	dc_parser_destroy (parser);
	return 1;
}
Пример #3
0
static int dive_cb(const unsigned char *data, unsigned int size,
	const unsigned char *fingerprint, unsigned int fsize,
	void *userdata)
{
	int rc;
	dc_parser_t *parser = NULL;
	device_data_t *devdata = userdata;
	dc_datetime_t dt = {0};
	struct tm tm;
	struct dive *dive;

	rc = create_parser(devdata, &parser);
	if (rc != DC_STATUS_SUCCESS) {
		dev_info(devdata, _("Unable to create parser for %s %s"), devdata->vendor, devdata->product);
		return rc;
	}

	rc = dc_parser_set_data(parser, data, size);
	if (rc != DC_STATUS_SUCCESS) {
		dev_info(devdata, _("Error registering the data"));
		dc_parser_destroy(parser);
		return rc;
	}

	import_dive_number++;
	dive = alloc_dive();
	rc = dc_parser_get_datetime(parser, &dt);
	if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
		dev_info(devdata, _("Error parsing the datetime"));
		dc_parser_destroy(parser);
		return rc;
	}

	tm.tm_year = dt.year;
	tm.tm_mon = dt.month-1;
	tm.tm_mday = dt.day;
	tm.tm_hour = dt.hour;
	tm.tm_min = dt.minute;
	tm.tm_sec = dt.second;
	dive->when = utc_mktime(&tm);

	// Parse the divetime.
	dev_info(devdata, _("Dive %d: %s %d %04d"), import_dive_number,
		monthname(tm.tm_mon), tm.tm_mday, year(tm.tm_year));
	unsigned int divetime = 0;
	rc = dc_parser_get_field (parser, DC_FIELD_DIVETIME, 0, &divetime);
	if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
		dev_info(devdata, _("Error parsing the divetime"));
		dc_parser_destroy(parser);
		return rc;
	}
	dive->duration.seconds = divetime;

	// Parse the maxdepth.
	double maxdepth = 0.0;
	rc = dc_parser_get_field(parser, DC_FIELD_MAXDEPTH, 0, &maxdepth);
	if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
		dev_info(devdata, _("Error parsing the maxdepth"));
		dc_parser_destroy(parser);
		return rc;
	}
	dive->maxdepth.mm = maxdepth * 1000 + 0.5;

	// Parse the gas mixes.
	unsigned int ngases = 0;
	rc = dc_parser_get_field(parser, DC_FIELD_GASMIX_COUNT, 0, &ngases);
	if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
		dev_info(devdata, _("Error parsing the gas mix count"));
		dc_parser_destroy(parser);
		return rc;
	}

	// Check if the libdivecomputer version already supports salinity
	double salinity = 1.03;
#ifdef DC_FIELD_SALINITY
	rc = dc_parser_get_field(parser, DC_FIELD_SALINITY, 0, &salinity);
	if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
		dev_info(devdata, _("Error obtaining water salinity"));
		dc_parser_destroy(parser);
		return rc;
	}
#endif
	dive->salinity = salinity * 10000.0 + 0.5;

	rc = parse_gasmixes(devdata, dive, parser, ngases);
	if (rc != DC_STATUS_SUCCESS) {
		dev_info(devdata, _("Error parsing the gas mix"));
		dc_parser_destroy(parser);
		return rc;
	}

	// Initialize the sample data.
	rc = parse_samples(devdata, &dive, parser);
	if (rc != DC_STATUS_SUCCESS) {
		dev_info(devdata, _("Error parsing the samples"));
		dc_parser_destroy(parser);
		return rc;
	}

	dc_parser_destroy(parser);

	/* If we already saw this dive, abort. */
	if (!devdata->force_download && find_dive(dive, devdata))
		return 0;

	dive->downloaded = TRUE;
	record_dive(dive);
	mark_divelist_changed(TRUE);
	return 1;
}
Пример #4
0
static dc_status_t
doparse(dif_dive_collection_t *dc, dc_device_t *device, const unsigned char data[], unsigned int size) {
    dif_dive_t *dive = NULL;
    unsigned int i = 0;

    /* allocate the dive */
    message("allocating the dive\n");
    dive = dif_dive_alloc();
    if (dive == NULL || dc == NULL) {
        WARNING("Error creating the dive object");
        return DC_STATUS_NOMEMORY;
    }
    dc = dif_dive_collection_add_dive(dc, dive);

    /* create the parser */
    message("Creating the parser.\n");
    dc_parser_t *parser = NULL;
    dc_status_t rc = dc_parser_new(&parser, device);
    if (rc != DC_STATUS_SUCCESS) {
        WARNING("Error creating the parser.");
        return rc;
    }

    /* register the data with the parser */
    message("Registering the data.\n");
    rc = dc_parser_set_data(parser, data, size);
    if (rc != DC_STATUS_SUCCESS) {
        WARNING("Error registering the data.");
        dc_parser_destroy(parser);
        return rc;
    }

    /* parse the datetime of the dive*/
    message("Parsing the datetime.\n");
    dc_datetime_t dt = {0};
    rc = dc_parser_get_datetime(parser, &dt);
    if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
        WARNING("Error parsing the datetime.");
        dc_parser_destroy(parser);
        return rc;
    }

    dive = dif_dive_set_datetime(dive, dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second);

    /* parse the divetime - in seconds */
    message("Parsing the divetime.\n");
    unsigned int divetime = 0;
    rc = dc_parser_get_field(parser, DC_FIELD_DIVETIME, 0, &divetime);
    if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
        WARNING("Error parsing the divetime.");
        dc_parser_destroy(parser);
        return rc;
    }
    dive = dif_dive_set_duration(dive, divetime);

    /* parse the maximum depth */
    message("Parsing the maximum depth.\n");
    double maxdepth = 0.0;
    rc = dc_parser_get_field(parser, DC_FIELD_MAXDEPTH, 0, &maxdepth);
    if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
        WARNING("Error parsing the maximum depth.");
        dc_parser_destroy(parser);
        return rc;
    }
    dive = dif_dive_set_maxdepth(dive, maxdepth);

    /* parse the gas mixes */
    message("Parsing the gas mixes.\n");
    unsigned int ngases = 0;
    rc = dc_parser_get_field(parser, DC_FIELD_GASMIX_COUNT, 0, &ngases);
    if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
        WARNING("Error parsing the gas mix count.");
        dc_parser_destroy(parser);
        return rc;
    }

    for (i=0; i < ngases; ++i) {
        dc_gasmix_t gasmix = {0};
        rc = dc_parser_get_field(parser, DC_FIELD_GASMIX, i, &gasmix);
        if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
            WARNING("Error parsing the gas mix.");
            dc_parser_destroy(parser);
            return rc;
        }
        dif_gasmix_t *mix = dif_gasmix_alloc();
        if (mix == NULL) {
            WARNING("Error allocating gas mix object.");
            return DC_STATUS_NOMEMORY;
        }
        mix->id = i;
        mix->helium = gasmix.helium * 100;
        mix->oxygen = gasmix.oxygen * 100;
        mix->nitrogen = gasmix.nitrogen * 100;
        if (dif_gasmix_is_valid(mix)) {
            dive = dif_dive_add_gasmix(dive, mix);
        } else {
            dif_gasmix_free(mix);
        }
    }

    /* parse the sample data */
    message("Parsing the sample data.\n");
    sample_cb_data_t *cbdata = g_malloc(sizeof(sample_cb_data_t));
    cbdata->dive = dive;
    cbdata->sample = NULL;

    rc = dc_parser_samples_foreach(parser, sample_cb, cbdata);
    if (rc != DC_STATUS_SUCCESS) {
        WARNING("Error parsing the sample data.");
        dc_parser_destroy(parser);
        return rc;
    }

    g_free(cbdata);

    /* destroy the parser */
    message("Destroying the parser.\n");
    rc = dc_parser_destroy(parser);
    if (rc != DC_STATUS_SUCCESS) {
        WARNING("Error destroying the parser.");
        return rc;
    }

    return DC_STATUS_SUCCESS;
}