Changeset ceb54e6 in subsurface


Ignore:
Timestamp:
May 7, 2017, 7:48:12 AM (5 months ago)
Author:
Dirk Hohndel <dirk@…>
Branches:
master
Children:
618cd3f
Parents:
22f07f5
git-author:
Salvador Cuñat <salvador.cunat@…> (05/07/17 02:38:51)
git-committer:
Dirk Hohndel <dirk@…> (05/07/17 07:48:12)
Message:

Datatrak import rework: changes on helper funcs

Remove dtrak_profile() profile parsing func as this work is left to
libdivecomputer.
Simplifies read_file_header() to return the number of dives in the log
file as we don't use the rest of header data.
Add dtrak_prepare_data() to achieve a device_data_t structure and get
the correct libdc model number for the device.
Remove checking macro substituted with JUMP in header file.
Add dt_libdc_buffer() to get a buffer parseable with libdivecomputer.

Signed-off-by: Salvador Cuñat <salvador.cunat@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/datatrak.c

    rbf373f5 rceb54e6  
    99#include <string.h>
    1010#include <time.h>
    11 
     11#include "gettext.h"
    1212#include "datatrak.h"
    1313#include "dive.h"
    1414#include "units.h"
    1515#include "device.h"
    16 #include "gettext.h"
    17 
    18 extern struct sample *add_sample(struct sample *sample, int time, struct divecomputer *dc);
     16#include "file.h"
    1917
    2018unsigned char lector_bytes[2], lector_word[4], tmp_1byte, *byte;
    2119unsigned int tmp_2bytes;
    2220char is_nitrox, is_O2, is_SCR;
    23 unsigned long tmp_4bytes;
     21unsigned long tmp_4bytes, maxbuf;
    2422
    2523static unsigned int two_bytes_to_int(unsigned char x, unsigned char y)
     
    9088
    9189/*
    92  * Subsurface sample structure doesn't support the flags and alarms in the dt .log
    93  * so will treat them as dc events.
     90 * Reads the header of a datatrak  buffer and returns the number of
     91 * dives; zero on error (meaning this isn't a datatrak file).
     92 * All other info in the header is useless for Subsurface.
    9493 */
    95 static struct sample *dtrak_profile(struct dive *dt_dive, FILE *archivo)
    96 {
    97         int i, j = 1, interval, o2percent = dt_dive->cylinder[0].gasmix.o2.permille / 10;
    98         struct sample *sample = dt_dive->dc.sample;
    99         struct divecomputer *dc = &dt_dive->dc;
    100 
    101         for (i = 1; i <= dt_dive->dc.alloc_samples; i++) {
    102                 if (fread(&lector_bytes, 1, 2, archivo) != 2)
    103                         return sample;
    104                 interval= 20 * (i + 1);
    105                 sample = add_sample(sample, interval, dc);
    106                 sample->depth.mm = (two_bytes_to_int(lector_bytes[0], lector_bytes[1]) & 0xFFC0) * 1000 / 410;
    107                 byte = byte_to_bits(two_bytes_to_int(lector_bytes[0], lector_bytes[1]) & 0x003F);
    108                 if (byte[0] != 0)
    109                         sample->in_deco = true;
    110                 else
    111                         sample->in_deco = false;
    112                 if (byte[1] != 0)
    113                         add_event(dc, sample->time.seconds, 0, 0, 0, QT_TRANSLATE_NOOP("gettextFromC", "rbt"));
    114                 if (byte[2] != 0)
    115                         add_event(dc, sample->time.seconds, 0, 0, 0, QT_TRANSLATE_NOOP("gettextFromC", "ascent"));
    116                 if (byte[3] != 0)
    117                         add_event(dc, sample->time.seconds, 0, 0, 0, QT_TRANSLATE_NOOP("gettextFromC", "ceiling"));
    118                 if (byte[4] != 0)
    119                         add_event(dc, sample->time.seconds, 0, 0, 0, QT_TRANSLATE_NOOP("gettextFromC", "workload"));
    120                 if (byte[5] != 0)
    121                         add_event(dc, sample->time.seconds, 0, 0, 0, QT_TRANSLATE_NOOP("gettextFromC", "transmitter"));
    122                 if (j == 3) {
    123                         read_bytes(1);
    124                         if (is_O2) {
    125                                 read_bytes(1);
    126                                 o2percent = tmp_1byte;
    127                         }
    128                         j = 0;
    129                 }
    130                 free(byte);
    131 
    132                 // In commit 5f44fdd setpoint replaced po2, so although this is not necessarily CCR dive ...
    133                 if (is_O2)
    134                         sample->setpoint.mbar = calculate_depth_to_mbar(sample->depth.mm, dt_dive->surface_pressure, 0) * o2percent / 100;
    135                 j++;
    136         }
    137 bail:
    138         return sample;
     94static int read_file_header(unsigned char *buffer)
     95{
     96        int n = 0;
     97
     98        if (two_bytes_to_int(buffer[0], buffer[1]) == 0xA100)
     99                n = two_bytes_to_int(buffer[7], buffer[6]);
     100        return n;
    139101}
    140102
    141103/*
    142  * Reads the header of a file and returns the header struct
    143  * If it's not a DATATRAK file returns header zero initalized
     104 * Fills a device_data_t structure based on the info from g_models table, using
     105 * the dc's model number as start point.
     106 * Returns libdc's equivalent model number (also from g_models) or zero if
     107 * this a manual dive.
    144108 */
    145 static dtrakheader read_file_header(FILE *archivo)
    146 {
    147         dtrakheader fileheader = { 0 };
    148         const short headerbytes = 12;
    149         unsigned char *lector = (unsigned char *)malloc(headerbytes);
    150 
    151         if (fread(lector, 1, headerbytes, archivo) != headerbytes) {
    152                 free(lector);
    153                 return fileheader;
    154         }
    155         if (two_bytes_to_int(lector[0], lector[1]) != 0xA100) {
    156                 report_error(translate("gettextFromC", "Error: the file does not appear to be a DATATRAK dive log"));
    157                 free(lector);
    158                 return fileheader;
    159         }
    160         fileheader.header = (lector[0] << 8) + lector[1];
    161         fileheader.dc_serial_1 = two_bytes_to_int(lector[2], lector[3]);
    162         fileheader.dc_serial_2 = two_bytes_to_int(lector[4], lector[5]);
    163         fileheader.divesNum = two_bytes_to_int(lector[7], lector[6]);
    164         free(lector);
    165         return fileheader;
    166 }
    167 
    168 #define CHECK(_func, _val) if ((_func) != (_val)) goto bail
     109static int dtrak_prepare_data(int model, device_data_t *dev_data)
     110{
     111        dc_descriptor_t *d = NULL;
     112        int i = 0;
     113
     114        while (model != g_models[i].model_num && g_models[i].model_num != 0xEE)
     115                i++;
     116        dev_data->model = copy_string(g_models[i].name);
     117        sscanf(g_models[i].name,"%m[A-Za-z] ", &dev_data->vendor);
     118        dev_data->product = copy_string(strchr(g_models[i].name, ' ') + 1);
     119
     120        d = get_descriptor(g_models[i].type, g_models[i].libdc_num);
     121        if (d)
     122                dev_data->descriptor = d;
     123        else
     124                return 0;
     125        return g_models[i].libdc_num;
     126}
     127
     128/*
     129 * Reads the size of a datatrak profile from actual position in buffer *ptr,
     130 * zero padds it with a faked header and inserts the model number for
     131 * libdivecomputer parsing. Puts the completed buffer in a pre-allocated
     132 * compl_buffer, and returns status.
     133 */
     134static dc_status_t dt_libdc_buffer(unsigned char *ptr, int prf_length, int dc_model, unsigned char *compl_buffer)
     135{
     136        if (compl_buffer == NULL)
     137                return DC_STATUS_NOMEMORY;
     138        compl_buffer[3] = (unsigned char) dc_model;
     139        memcpy(compl_buffer + 18, ptr, prf_length);
     140        return DC_STATUS_SUCCESS;
     141}
    169142
    170143/*
Note: See TracChangeset for help on using the changeset viewer.