static int
mktemplate_datetime (char *buffer, size_t size, dc_parser_t *parser)
{
	dc_status_t rc = DC_STATUS_SUCCESS;
	dc_datetime_t datetime = {0};
	int n = 0;

	rc = dc_parser_get_datetime (parser, &datetime);
	if (rc != DC_STATUS_SUCCESS)
		return -1;

	n = snprintf (buffer, size, "%04i%02i%02iT%02i%02i%02i",
		datetime.year, datetime.month, datetime.day,
		datetime.hour, datetime.minute, datetime.second);
	if (n < 0 || n >= size)
		return -1;

	return n;
}
Exemple #2
0
int libdc_parser_parse_header(parser_handle_t abstract, const void * buffer, uint32_t size, header_callback_fn_t cb, void * userdata)
{
	libdc_parser_t parser = (libdc_parser_t)(abstract);
	if (parser == NULL)
	{
		errno = EINVAL;
		return -1;
	}

	dc_status_t rc = dc_parser_set_data(parser->parser, buffer, size);
	if (rc != DC_STATUS_SUCCESS)
	{
		parser->dev->errcode = DRIVER_ERR_PARSER;
		parser->dev->errmsg = "Failed to set parser data";
		return -1;
	}

	// Parse Dive Date/Time
	dc_datetime_t dt = {0};
	rc = dc_parser_get_datetime(parser->parser, & dt);
	if ((rc != DC_STATUS_SUCCESS) && (rc != DC_STATUS_UNSUPPORTED))
	{
		parser->dev->errcode = DRIVER_ERR_PARSER;
		parser->dev->errmsg = "Failed to retrieve dive date/time";
		return -1;
	}

	if ((rc == DC_STATUS_SUCCESS) && (cb != NULL))
	{
		dc_ticks_t ts = dc_datetime_mktime(& dt);
		cb(userdata, DIVE_HEADER_START_TIME, (time_t)ts, 0, 0);
	}

	// Parse Dive Duration
	unsigned int duration = 0;
	rc = dc_parser_get_field (parser->parser, DC_FIELD_DIVETIME, 0, & duration);
	if ((rc != DC_STATUS_SUCCESS) && (rc != DC_STATUS_UNSUPPORTED))
	{
		parser->dev->errcode = DRIVER_ERR_PARSER;
		parser->dev->errmsg = "Failed to retrieve dive duration";
		return -1;
	}

	if ((rc == DC_STATUS_SUCCESS) && (cb != NULL))
		cb(userdata, DIVE_HEADER_DURATION, duration / 60, 0, 0);

	// Parse Maximum Depth
	double maxdepth = 0.0;
	rc = dc_parser_get_field (parser->parser, DC_FIELD_MAXDEPTH, 0, & maxdepth);
	if ((rc != DC_STATUS_SUCCESS) && (rc != DC_STATUS_UNSUPPORTED))
	{
		parser->dev->errcode = DRIVER_ERR_PARSER;
		parser->dev->errmsg = "Failed to retrieve maximum depth";
		return -1;
	}

	if ((rc == DC_STATUS_SUCCESS) && (cb != NULL))
		cb(userdata, DIVE_HEADER_MAX_DEPTH, (uint32_t)round(maxdepth * 100), 0, 0);

	// Parse Gas Mixes
	unsigned int ngasses = 0;
	rc = dc_parser_get_field(parser->parser, DC_FIELD_GASMIX_COUNT, 0, & ngasses);
	if ((rc != DC_STATUS_SUCCESS) && (rc != DC_STATUS_UNSUPPORTED))
	{
		parser->dev->errcode = DRIVER_ERR_PARSER;
		parser->dev->errmsg = "Failed to retrieve gas mix count";
		return -1;
	}

	unsigned int i;
	for (i = 0; i < ngasses; ++i)
	{
		dc_gasmix_t gasmix = {0};
		rc = dc_parser_get_field(parser->parser, DC_FIELD_GASMIX, i, & gasmix);
		if ((rc != DC_STATUS_SUCCESS) && (rc != DC_STATUS_UNSUPPORTED))
		{
			parser->dev->errcode = DRIVER_ERR_PARSER;
			parser->dev->errmsg = "Failed to retrieve gas mix data";
			return -1;
		}

		if ((rc == DC_STATUS_SUCCESS) && (cb != NULL))
		{
			uint32_t pmHe = (uint32_t)round(gasmix.helium * 1000.0);
			uint32_t pmO2 = (uint32_t)round(gasmix.oxygen * 1000.0);

			cb(userdata, DIVE_HEADER_PMO2, pmO2, i, 0);
			cb(userdata, DIVE_HEADER_PMHe, pmHe, i, 0);
		}
	}

	return 0;
}
static dc_status_t
dctool_xml_output_write (dctool_output_t *abstract, dc_parser_t *parser, const unsigned char data[], unsigned int size, const unsigned char fingerprint[], unsigned int fsize)
{
	dctool_xml_output_t *output = (dctool_xml_output_t *) abstract;
	dc_status_t status = DC_STATUS_SUCCESS;

	// Initialize the sample data.
	sample_data_t sampledata = {0};
	sampledata.nsamples = 0;
	sampledata.ostream = output->ostream;
	sampledata.units = output->units;

	fprintf (output->ostream, "<dive>\n<number>%u</number>\n<size>%u</size>\n", abstract->number, size);

	if (fingerprint) {
		fprintf (output->ostream, "<fingerprint>");
		for (unsigned int i = 0; i < fsize; ++i)
			fprintf (output->ostream, "%02X", fingerprint[i]);
		fprintf (output->ostream, "</fingerprint>\n");
	}

	// Parse the datetime.
	message ("Parsing the datetime.\n");
	dc_datetime_t dt = {0};
	status = dc_parser_get_datetime (parser, &dt);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the datetime.");
		goto cleanup;
	}

	if (dt.timezone == DC_TIMEZONE_NONE) {
		fprintf (output->ostream, "<datetime>%04i-%02i-%02i %02i:%02i:%02i</datetime>\n",
			dt.year, dt.month, dt.day,
			dt.hour, dt.minute, dt.second);
	} else {
		fprintf (output->ostream, "<datetime>%04i-%02i-%02i %02i:%02i:%02i %+03i:%02i</datetime>\n",
			dt.year, dt.month, dt.day,
			dt.hour, dt.minute, dt.second,
			dt.timezone / 3600, (dt.timezone % 3600) / 60);
	}

	// Parse the divetime.
	message ("Parsing the divetime.\n");
	unsigned int divetime = 0;
	status = dc_parser_get_field (parser, DC_FIELD_DIVETIME, 0, &divetime);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the divetime.");
		goto cleanup;
	}

	fprintf (output->ostream, "<divetime>%02u:%02u</divetime>\n",
		divetime / 60, divetime % 60);

	// Parse the maxdepth.
	message ("Parsing the maxdepth.\n");
	double maxdepth = 0.0;
	status = dc_parser_get_field (parser, DC_FIELD_MAXDEPTH, 0, &maxdepth);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the maxdepth.");
		goto cleanup;
	}

	fprintf (output->ostream, "<maxdepth>%.2f</maxdepth>\n",
		convert_depth(maxdepth, output->units));

	// Parse the temperature.
	message ("Parsing the temperature.\n");
	for (unsigned int i = 0; i < 3; ++i) {
		dc_field_type_t fields[] = {DC_FIELD_TEMPERATURE_SURFACE,
			DC_FIELD_TEMPERATURE_MINIMUM,
			DC_FIELD_TEMPERATURE_MAXIMUM};
		const char *names[] = {"surface", "minimum", "maximum"};

		double temperature = 0.0;
		status = dc_parser_get_field (parser, fields[i], 0, &temperature);
		if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
			ERROR ("Error parsing the temperature.");
			goto cleanup;
		}

		if (status != DC_STATUS_UNSUPPORTED) {
			fprintf (output->ostream, "<temperature type=\"%s\">%.1f</temperature>\n",
				names[i],
				convert_temperature(temperature, output->units));
		}
	}

	// Parse the gas mixes.
	message ("Parsing the gas mixes.\n");
	unsigned int ngases = 0;
	status = dc_parser_get_field (parser, DC_FIELD_GASMIX_COUNT, 0, &ngases);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the gas mix count.");
		goto cleanup;
	}

	for (unsigned int i = 0; i < ngases; ++i) {
		dc_gasmix_t gasmix = {0};
		status = dc_parser_get_field (parser, DC_FIELD_GASMIX, i, &gasmix);
		if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
			ERROR ("Error parsing the gas mix.");
			goto cleanup;
		}

		fprintf (output->ostream,
			"<gasmix>\n"
			"   <he>%.1f</he>\n"
			"   <o2>%.1f</o2>\n"
			"   <n2>%.1f</n2>\n"
			"</gasmix>\n",
			gasmix.helium * 100.0,
			gasmix.oxygen * 100.0,
			gasmix.nitrogen * 100.0);
	}

	// Parse the tanks.
	message ("Parsing the tanks.\n");
	unsigned int ntanks = 0;
	status = dc_parser_get_field (parser, DC_FIELD_TANK_COUNT, 0, &ntanks);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the tank count.");
		goto cleanup;
	}

	for (unsigned int i = 0; i < ntanks; ++i) {
		const char *names[] = {"none", "metric", "imperial"};

		dc_tank_t tank = {0};
		status = dc_parser_get_field (parser, DC_FIELD_TANK, i, &tank);
		if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
			ERROR ("Error parsing the tank.");
			goto cleanup;
		}

		fprintf (output->ostream, "<tank>\n");
		if (tank.gasmix != DC_GASMIX_UNKNOWN) {
			fprintf (output->ostream,
				"   <gasmix>%u</gasmix>\n",
				tank.gasmix);
		}
		if (tank.type != DC_TANKVOLUME_NONE) {
			fprintf (output->ostream,
				"   <type>%s</type>\n"
				"   <volume>%.1f</volume>\n"
				"   <workpressure>%.2f</workpressure>\n",
				names[tank.type],
				convert_volume(tank.volume, output->units),
				convert_pressure(tank.workpressure, output->units));
		}
		fprintf (output->ostream,
			"   <beginpressure>%.2f</beginpressure>\n"
			"   <endpressure>%.2f</endpressure>\n"
			"</tank>\n",
			convert_pressure(tank.beginpressure, output->units),
			convert_pressure(tank.endpressure, output->units));
	}

	// Parse the dive mode.
	message ("Parsing the dive mode.\n");
	dc_divemode_t divemode = DC_DIVEMODE_OC;
	status = dc_parser_get_field (parser, DC_FIELD_DIVEMODE, 0, &divemode);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the dive mode.");
		goto cleanup;
	}

	if (status != DC_STATUS_UNSUPPORTED) {
		const char *names[] = {"freedive", "gauge", "oc", "ccr", "scr"};
		fprintf (output->ostream, "<divemode>%s</divemode>\n",
			names[divemode]);
	}

	// Parse the salinity.
	message ("Parsing the salinity.\n");
	dc_salinity_t salinity = {DC_WATER_FRESH, 0.0};
	status = dc_parser_get_field (parser, DC_FIELD_SALINITY, 0, &salinity);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the salinity.");
		goto cleanup;
	}

	if (status != DC_STATUS_UNSUPPORTED) {
		fprintf (output->ostream, "<salinity type=\"%u\">%.1f</salinity>\n",
			salinity.type, salinity.density);
	}

	// Parse the atmospheric pressure.
	message ("Parsing the atmospheric pressure.\n");
	double atmospheric = 0.0;
	status = dc_parser_get_field (parser, DC_FIELD_ATMOSPHERIC, 0, &atmospheric);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the atmospheric pressure.");
		goto cleanup;
	}

	if (status != DC_STATUS_UNSUPPORTED) {
		fprintf (output->ostream, "<atmospheric>%.5f</atmospheric>\n",
			convert_pressure(atmospheric, output->units));
	}

	// Parse the sample data.
	message ("Parsing the sample data.\n");
	status = dc_parser_samples_foreach (parser, sample_cb, &sampledata);
	if (status != DC_STATUS_SUCCESS) {
		ERROR ("Error parsing the sample data.");
		goto cleanup;
	}

cleanup:

	if (sampledata.nsamples)
		fprintf (output->ostream, "</sample>\n");
	fprintf (output->ostream, "</dive>\n");

	return status;
}
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;
}
Exemple #5
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;
}