cochran.c: coding style cleanup

- empty lines
- indentation
- { placement
- others...

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Lubomir I. Ivanov 2014-10-28 13:35:14 +02:00 committed by Dirk Hohndel
parent 4bc9b7748b
commit e64a25d715

112
cochran.c
View file

@ -45,11 +45,11 @@ struct config {
// Convert 4 bytes into an INT // Convert 4 bytes into an INT
#define array_uint16_le(p) ( (unsigned int) (p)[0] \ #define array_uint16_le(p) ((unsigned int) (p)[0] \
+ ((p)[1]<<8) ) + ((p)[1]<<8) )
#define array_uint32_le(p) ( (unsigned int) (p)[0] \ #define array_uint32_le(p) ((unsigned int) (p)[0] \
+ ((p)[1]<<8) + ((p)[2]<<16) \ + ((p)[1]<<8) + ((p)[2]<<16) \
+ ((p)[3]<<24) ) + ((p)[3]<<24))
/* /*
* The Cochran file format is designed to be annoying to read. It's roughly: * The Cochran file format is designed to be annoying to read. It's roughly:
@ -143,19 +143,15 @@ static void cochran_debug_write(const unsigned char *data, unsigned size)
return; return;
int show = 1, i; int show = 1, i;
for (i = 0; i < size; i += 16) for (i = 0; i < size; i += 16)
show = show_line(i, data + i, size - i, show); show = show_line(i, data + i, size - i, show);
} }
static void cochran_debug_sample(const char * s, unsigned int seconds) static void cochran_debug_sample(const char *s, unsigned int seconds)
{ {
switch (config.type) switch (config.type) {
{
case TYPE_GEMINI: case TYPE_GEMINI:
switch (seconds % 4) switch (seconds % 4) {
{
case 0: case 0:
printf("Hex: %02x %02x ", s[0], s[1]); printf("Hex: %02x %02x ", s[0], s[1]);
break; break;
@ -171,8 +167,7 @@ static void cochran_debug_sample(const char * s, unsigned int seconds)
} }
break; break;
case TYPE_COMMANDER: case TYPE_COMMANDER:
switch (seconds % 2) switch (seconds % 2) {
{
case 0: case 0:
printf("Hex: %02x %02x ", s[0], s[1]); printf("Hex: %02x %02x ", s[0], s[1]);
break; break;
@ -182,8 +177,7 @@ static void cochran_debug_sample(const char * s, unsigned int seconds)
} }
break; break;
case TYPE_EMC: case TYPE_EMC:
switch (seconds % 2) switch (seconds % 2) {
{
case 0: case 0:
printf("Hex: %02x %02x %02x ", s[0], s[1], s[2]); printf("Hex: %02x %02x %02x ", s[0], s[1], s[2]);
break; break;
@ -200,8 +194,7 @@ static void cochran_debug_sample(const char * s, unsigned int seconds)
#endif // COCHRAN_DEBUG #endif // COCHRAN_DEBUG
static void cochran_parse_header( static void cochran_parse_header(const unsigned char *decode, unsigned mod,
const unsigned char *decode, unsigned mod,
const unsigned char *in, unsigned size) const unsigned char *in, unsigned size)
{ {
unsigned char *buf = malloc(size); unsigned char *buf = malloc(size);
@ -223,8 +216,7 @@ static void cochran_parse_header(
partial_decode(0x5414, size, decode, 0, mod, in, size, buf); partial_decode(0x5414, size, decode, 0, mod, in, size, buf);
// Detect log type // Detect log type
switch (buf[0x133]) switch (buf[0x133]) {
{
case '2': // Cochran Commander, version II log format case '2': // Cochran Commander, version II log format
config.logbook_size = 256; config.logbook_size = 256;
if (buf[0x132] == 0x10) { if (buf[0x132] == 0x10) {
@ -254,70 +246,57 @@ static void cochran_parse_header(
free(buf); free(buf);
} }
/* /*
* Bytes expected after a pre-dive event code * Bytes expected after a pre-dive event code
*/ */
static int cochran_predive_event_bytes(unsigned char code)
static int cochran_predive_event_bytes (unsigned char code)
{ {
int x = 0; int x = 0;
int gem_event_bytes[15][2] = {{0x00, 10}, {0x02, 17}, {0x08, 18},
int gem_event_bytes[15][2] = { {0x00, 10}, {0x02, 17}, {0x08, 18},
{0x09, 18}, {0x0c, 18}, {0x0d, 18}, {0x09, 18}, {0x0c, 18}, {0x0d, 18},
{0x0e, 18}, {0x0e, 18},
{ -1, 0} }; {-1, 0}};
int cmdr_event_bytes[15][2] = { {0x00, 16}, {0x01, 20}, {0x02, 17}, int cmdr_event_bytes[15][2] = {{0x00, 16}, {0x01, 20}, {0x02, 17},
{0x03, 16}, {0x06, 18}, {0x07, 18}, {0x03, 16}, {0x06, 18}, {0x07, 18},
{0x08, 18}, {0x09, 18}, {0x0a, 18}, {0x08, 18}, {0x09, 18}, {0x0a, 18},
{0x0b, 20}, {0x0c, 18}, {0x0d, 18}, {0x0b, 20}, {0x0c, 18}, {0x0d, 18},
{0x0e, 18}, {0x10, 20}, {0x0e, 18}, {0x10, 20},
{ -1, 0} }; {-1, 0}};
int emc_event_bytes[15][2] = { {0x00, 18}, {0x01, 22}, {0x02, 19}, int emc_event_bytes[15][2] = {{0x00, 18}, {0x01, 22}, {0x02, 19},
{0x03, 18}, {0x06, 20}, {0x07, 20}, {0x03, 18}, {0x06, 20}, {0x07, 20},
{0x0a, 20}, {0x0b, 20}, {0x0f, 18}, {0x0a, 20}, {0x0b, 20}, {0x0f, 18},
{0x10, 20}, {0x10, 20},
{ -1, 0} }; {-1, 0}};
switch (config.type) switch (config.type) {
{
case TYPE_GEMINI: case TYPE_GEMINI:
while (gem_event_bytes[x][0] != code && gem_event_bytes[x][0] != -1) while (gem_event_bytes[x][0] != code && gem_event_bytes[x][0] != -1)
x++; x++;
return gem_event_bytes[x][1]; return gem_event_bytes[x][1];
break; break;
case TYPE_COMMANDER: case TYPE_COMMANDER:
while (cmdr_event_bytes[x][0] != code && cmdr_event_bytes[x][0] != -1) while (cmdr_event_bytes[x][0] != code && cmdr_event_bytes[x][0] != -1)
x++; x++;
return cmdr_event_bytes[x][1]; return cmdr_event_bytes[x][1];
break; break;
case TYPE_EMC: case TYPE_EMC:
while (emc_event_bytes[x][0] != code && emc_event_bytes[x][0] != -1) while (emc_event_bytes[x][0] != code && emc_event_bytes[x][0] != -1)
x++; x++;
return emc_event_bytes[x][1]; return emc_event_bytes[x][1];
break; break;
} }
} }
int cochran_dive_event_bytes(unsigned char event) int cochran_dive_event_bytes(unsigned char event)
{ {
if (event == 0xAD || event == 0xAB) return (event == 0xAD || event == 0xAB) ? 4 : 0;
return 4;
else
return 0;
} }
static void cochran_dive_event(struct divecomputer *dc, const unsigned char *s, static void cochran_dive_event(struct divecomputer *dc, const unsigned char *s,
unsigned int seconds, unsigned int *in_deco, unsigned int seconds, unsigned int *in_deco,
unsigned int *deco_ceiling, unsigned int *deco_time) unsigned int *deco_ceiling, unsigned int *deco_time)
{ {
switch (s[0]) switch (s[0]) {
{
case 0xC5: // Deco obligation begins case 0xC5: // Deco obligation begins
*in_deco = 1; *in_deco = 1;
add_event(dc, seconds, SAMPLE_EVENT_DECOSTOP, add_event(dc, seconds, SAMPLE_EVENT_DECOSTOP,
@ -446,11 +425,9 @@ static void cochran_dive_event(struct divecomputer *dc, const unsigned char *s,
} }
} }
/* /*
* Parse sample data, extract events and build a dive * Parse sample data, extract events and build a dive
*/ */
static void cochran_parse_samples(struct dive *dive, const unsigned char *log, static void cochran_parse_samples(struct dive *dive, const unsigned char *log,
const unsigned char *samples, int size, const unsigned char *samples, int size,
unsigned int *duration, double *max_depth, unsigned int *duration, double *max_depth,
@ -473,23 +450,21 @@ static void cochran_parse_samples(struct dive *dive, const unsigned char *log,
*max_depth = 0, *avg_depth = 0, *min_temp = 0xFF; *max_depth = 0, *avg_depth = 0, *min_temp = 0xFF;
// Get starting depth and temp (tank PSI???) // Get starting depth and temp (tank PSI???)
switch (config.type) switch (config.type) {
{
case TYPE_GEMINI: case TYPE_GEMINI:
depth = (float) (log_cmdr->start_depth[0] depth = (float)(log_cmdr->start_depth[0]
+ log_cmdr->start_depth[1] * 256) / 4; + log_cmdr->start_depth[1] * 256) / 4;
psi = log_cmdr->start_psi[0] + log_cmdr->start_psi[1] * 256; psi = log_cmdr->start_psi[0] + log_cmdr->start_psi[1] * 256;
sgc_rate = (float) (log_cmdr->start_sgc[0] sgc_rate = (float)(log_cmdr->start_sgc[0]
+ log_cmdr->start_sgc[1] * 256) / 2; + log_cmdr->start_sgc[1] * 256) / 2;
break; break;
case TYPE_COMMANDER: case TYPE_COMMANDER:
depth = (float) (log_cmdr->start_depth[0] depth = (float)(log_cmdr->start_depth[0]
+ log_cmdr->start_depth[1] * 256) / 4; + log_cmdr->start_depth[1] * 256) / 4;
break; break;
case TYPE_EMC: case TYPE_EMC:
depth = (float) log_emc->start_depth[0] / 256 depth = (float)log_emc->start_depth[0] / 256
+ log_emc->start_depth[1]; + log_emc->start_depth[1];
temp = log_emc->start_temperature; temp = log_emc->start_temperature;
break; break;
@ -499,11 +474,11 @@ static void cochran_parse_samples(struct dive *dive, const unsigned char *log,
unsigned int x = 0; unsigned int x = 0;
if (samples[x] != 0x40) { if (samples[x] != 0x40) {
unsigned int c; unsigned int c;
while ( (samples[x] & 0x80) == 0 && samples[x] != 0x40 && x < size) { while ((samples[x] & 0x80) == 0 && samples[x] != 0x40 && x < size) {
c = cochran_predive_event_bytes(samples[x]) + 1; c = cochran_predive_event_bytes(samples[x]) + 1;
#ifdef COCHRAN_DEBUG #ifdef COCHRAN_DEBUG
printf ("Predive event: ", samples[x]); printf("Predive event: ", samples[x]);
for (int y = 0; y < c; y++) printf ("%02x ", samples[x + y]); for (int y = 0; y < c; y++) printf("%02x ", samples[x + y]);
putchar('\n'); putchar('\n');
#endif #endif
x += c; x += c;
@ -527,18 +502,16 @@ static void cochran_parse_samples(struct dive *dive, const unsigned char *log,
} }
// Depth is in every sample // Depth is in every sample
depth_sample = (float) (s[0] & 0x3F) / 4 * (s[0] & 0x40 ? -1 : 1); depth_sample = (float)(s[0] & 0x3F) / 4 * (s[0] & 0x40 ? -1 : 1);
depth += depth_sample; depth += depth_sample;
#ifdef COCHRAN_DEBUG #ifdef COCHRAN_DEBUG
cochran_debug_sample(s, seconds); cochran_debug_sample(s, seconds);
#endif #endif
switch (config.type) switch (config.type) {
{
case TYPE_COMMANDER: case TYPE_COMMANDER:
switch (seconds % 2) switch (seconds % 2) {
{
case 0: // Ascent rate case 0: // Ascent rate
ascent_rate = (s[1] & 0x7f) * (s[1] & 0x80 ? 1: -1); ascent_rate = (s[1] & 0x7f) * (s[1] & 0x80 ? 1: -1);
break; break;
@ -549,35 +522,32 @@ static void cochran_parse_samples(struct dive *dive, const unsigned char *log,
break; break;
case TYPE_GEMINI: case TYPE_GEMINI:
// Gemini with tank pressure and SAC rate. // Gemini with tank pressure and SAC rate.
switch (seconds % 4) switch (seconds % 4) {
{
case 0: // Ascent rate case 0: // Ascent rate
ascent_rate = (s[1] & 0x7f) * (s[1] & 0x80 ? 1 : -1); ascent_rate = (s[1] & 0x7f) * (s[1] & 0x80 ? 1 : -1);
break; break;
case 2: // PSI change case 2: // PSI change
psi -= (float) (s[1] & 0x7f) * (s[1] & 0x80 ? 1 : -1) / 4; psi -= (float)(s[1] & 0x7f) * (s[1] & 0x80 ? 1 : -1) / 4;
break; break;
case 1: // SGC rate case 1: // SGC rate
sgc_rate -= (float) (s[1] & 0x7f) * (s[1] & 0x80 ? 1 : -1) / 2; sgc_rate -= (float)(s[1] & 0x7f) * (s[1] & 0x80 ? 1 : -1) / 2;
break; break;
case 3: // Temperature case 3: // Temperature
temp = (float) s[1] / 2 + 20; temp = (float)s[1] / 2 + 20;
break; break;
} }
break; break;
case TYPE_EMC: case TYPE_EMC:
switch (seconds % 2) switch (seconds % 2) {
{
case 0: // Ascent rate case 0: // Ascent rate
ascent_rate = (s[1] & 0x7f) * (s[1] & 0x80 ? 1: -1); ascent_rate = (s[1] & 0x7f) * (s[1] & 0x80 ? 1: -1);
break; break;
case 1: // Temperature case 1: // Temperature
temp = (float) s[1] / 2 + 20; temp = (float)s[1] / 2 + 20;
break; break;
} }
// Get NDL and deco information // Get NDL and deco information
switch (seconds % 24) switch (seconds % 24) {
{
case 20: case 20:
if (in_deco) { if (in_deco) {
// Fist stop time // Fist stop time
@ -623,7 +593,6 @@ static void cochran_parse_samples(struct dive *dive, const unsigned char *log,
*duration = seconds - 1; *duration = seconds - 1;
} }
static void cochran_parse_dive(const unsigned char *decode, unsigned mod, static void cochran_parse_dive(const unsigned char *decode, unsigned mod,
const unsigned char *in, unsigned size) const unsigned char *in, unsigned size)
{ {
@ -687,8 +656,7 @@ static void cochran_parse_dive(const unsigned char *decode, unsigned mod,
struct cochran_cmdr_log_t *cmdr_log = (struct cochran_cmdr_log_t *) (buf + 0x4914); struct cochran_cmdr_log_t *cmdr_log = (struct cochran_cmdr_log_t *) (buf + 0x4914);
struct cochran_emc_log_t *emc_log = (struct cochran_emc_log_t *) (buf + 0x4914); struct cochran_emc_log_t *emc_log = (struct cochran_emc_log_t *) (buf + 0x4914);
switch (config.type) switch (config.type) {
{
case TYPE_GEMINI: case TYPE_GEMINI:
case TYPE_COMMANDER: case TYPE_COMMANDER:
if (config.type == TYPE_GEMINI) { if (config.type == TYPE_GEMINI) {
@ -756,7 +724,6 @@ static void cochran_parse_dive(const unsigned char *decode, unsigned mod,
tm.tm_sec = emc_log->seconds; tm.tm_sec = emc_log->seconds;
tm.tm_isdst = -1; tm.tm_isdst = -1;
dive->when = dc->when = utc_mktime(&tm); dive->when = dc->when = utc_mktime(&tm);
dive->number = emc_log->number[0] + emc_log->number[1] * 256 + 1; dive->number = emc_log->number[0] + emc_log->number[1] * 256 + 1;
dc->duration.seconds = (emc_log->bt[0] + emc_log->bt[1] * 256) * 60; dc->duration.seconds = (emc_log->bt[0] + emc_log->bt[1] * 256) * 60;
@ -816,7 +783,6 @@ int try_to_open_cochran(const char *filename, struct memblock *mem)
return 0; return 0;
mod = decode[0x100] + 1; mod = decode[0x100] + 1;
cochran_parse_header(decode, mod, mem->buffer + 0x40000, dive1 - 0x40000); cochran_parse_header(decode, mod, mem->buffer + 0x40000, dive1 - 0x40000);
// Decode each dive // Decode each dive