Skip to content

Commit

Permalink
use DebugLog in dg100
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteven4 committed Nov 2, 2024
1 parent 382323d commit 106cdd3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 41 deletions.
1 change: 1 addition & 0 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@ enum ff_cap {
class DebugLog
{
public:
int vlog(const char* fmt, va_list args1);
[[gnu::format(printf, 2, 3)]] int log(const char* fmt, ...);
int flush();

Expand Down
44 changes: 22 additions & 22 deletions dg-100.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <cassert> // for assert
#include <cstdarg> // for va_end, va_list, va_start
#include <cstdint> // for uint8_t, uint16_t, int16_t
#include <cstdio> // for fprintf, stderr, size_t, vfprintf
#include <cstdio> // for size_t, va_list
#include <cstdlib> // for abs
#include <cstring> // for memcpy, memcmp, strcmp

Expand Down Expand Up @@ -79,7 +79,7 @@ const Dg100Format::dg100_command Dg100Format::dg200_commands[] = {

/* helper functions */
const Dg100Format::dg100_command*
Dg100Format::dg100_findcmd(int id) const
Dg100Format::dg100_findcmd(int id)
{
/* linear search should be OK as long as dg100_numcommands is small */
for (unsigned int i = 0; i < model->numcommands; i++) {
Expand Down Expand Up @@ -118,14 +118,14 @@ Dg100Format::dg100_debug(const char* hdr, int include_nl, size_t sz, unsigned ch
return;
}

fprintf(stderr, "%s", hdr);
db.log("%s", hdr);

for (unsigned int i = 0; i < sz; i++) {
fprintf(stderr, "%02x ", buf[i]);
db.log("%02x ", buf[i]);
}

if (include_nl) {
fprintf(stderr, "\n");
db.log("\n");
}
}

Expand All @@ -135,7 +135,7 @@ Dg100Format::dg100_log(const char* fmt, ...)
va_list ap;
va_start(ap, fmt);
if (global_opts.debug_level > 0) {
vfprintf(stderr, fmt, ap);
db.log(fmt, ap);
}
va_end(ap);
}
Expand Down Expand Up @@ -166,15 +166,15 @@ Dg100Format::bin2deg(int val)
}

void
Dg100Format::process_gpsfile(uint8_t data[], route_head** track) const
Dg100Format::process_gpsfile(uint8_t data[], route_head** track)
{
const int recordsizes[3] = {8, 20, 32};

/* the first record of each file is always full-sized; its style field
* determines the format of all subsequent records in the file */
int style = be_read32(data + 28);
if (style > 2) {
fprintf(stderr, "unknown GPS record style %d", style);
warning("unknown GPS record style %d", style);
return;
}
int recsize = recordsizes[style];
Expand Down Expand Up @@ -256,7 +256,7 @@ Dg100Format::dg100_checksum(const uint8_t buf[], int count)

/* communication functions */
size_t
Dg100Format::dg100_send(uint8_t cmd, const void* payload, size_t param_len) const
Dg100Format::dg100_send(uint8_t cmd, const void* payload, size_t param_len)
{
uint8_t frame[FRAME_MAXLEN];

Expand Down Expand Up @@ -320,7 +320,7 @@ Dg100Format::dg100_send(uint8_t cmd, const void* payload, size_t param_len) cons
}

int
Dg100Format::dg100_recv_byte() const
Dg100Format::dg100_recv_byte()
{
int result;
if (isfile) {
Expand All @@ -344,7 +344,7 @@ Dg100Format::dg100_recv_byte() const
}

int
Dg100Format::dg100_read_wait(void* handle, void* buf, unsigned len, unsigned ms) const
Dg100Format::dg100_read_wait(void* handle, void* buf, unsigned len, unsigned ms)
{
if (isfile) {
return gbfread(buf, 1, len, fin);
Expand All @@ -357,7 +357,7 @@ Dg100Format::dg100_read_wait(void* handle, void* buf, unsigned len, unsigned ms)
* framing around the data), so the caller must copy the data before calling
* this function again */
int
Dg100Format::dg100_recv_frame(const dg100_command** cmdinfo_result, uint8_t** payload) const
Dg100Format::dg100_recv_frame(const dg100_command** cmdinfo_result, uint8_t** payload)
{
static uint8_t buf[FRAME_MAXLEN];
uint16_t payload_end_seq;
Expand Down Expand Up @@ -487,7 +487,7 @@ Dg100Format::dg100_recv_frame(const dg100_command** cmdinfo_result, uint8_t** pa

/* return value: number of bytes copied into buf, -1 on error */
int
Dg100Format::dg100_recv(uint8_t expected_id, void* buf, unsigned int len) const
Dg100Format::dg100_recv(uint8_t expected_id, void* buf, unsigned int len)
{
const dg100_command* cmdinfo;
uint8_t* data;
Expand All @@ -496,7 +496,7 @@ Dg100Format::dg100_recv(uint8_t expected_id, void* buf, unsigned int len) const

/* check whether the received frame matches the expected answer type */
if (cmdinfo->id != expected_id) {
fprintf(stderr, "ERROR: answer type %02x, expecting %02x", cmdinfo->id, expected_id);
warning("ERROR: answer type %02x, expecting %02x", cmdinfo->id, expected_id);
return -1;
}

Expand All @@ -505,7 +505,7 @@ Dg100Format::dg100_recv(uint8_t expected_id, void* buf, unsigned int len) const

/* check for buffer overflow */
if (len < copysize) {
fprintf(stderr, "ERROR: buffer too small, size=%u, need=%u", len, copysize);
warning("ERROR: buffer too small, size=%u, need=%u", len, copysize);
return -1;
}

Expand All @@ -516,7 +516,7 @@ Dg100Format::dg100_recv(uint8_t expected_id, void* buf, unsigned int len) const
/* the number of bytes to be sent is determined by cmd,
* count is the size of recvbuf */
int
Dg100Format::dg100_request(uint8_t cmd, const void* sendbuf, void* recvbuf, size_t count) const
Dg100Format::dg100_request(uint8_t cmd, const void* sendbuf, void* recvbuf, size_t count)
{
const dg100_command* cmdinfo = dg100_findcmd(cmd);
assert(cmdinfo != nullptr);
Expand All @@ -539,7 +539,7 @@ Dg100Format::dg100_request(uint8_t cmd, const void* sendbuf, void* recvbuf, size

/* higher level communication functions */
QList<int>
Dg100Format::dg100_getfileheaders() const
Dg100Format::dg100_getfileheaders()
{
QList<int> headers;
uint8_t request[2];
Expand Down Expand Up @@ -580,15 +580,15 @@ Dg100Format::dg100_getfileheaders() const
}

void
Dg100Format::dg100_getconfig() const
Dg100Format::dg100_getconfig()
{
uint8_t answer[45];

dg100_request(dg100cmd_getconfig, nullptr, answer, sizeof(answer));
}

void
Dg100Format::dg100_getfile(int16_t num, route_head** track) const
Dg100Format::dg100_getfile(int16_t num, route_head** track)
{
uint8_t request[2];
uint8_t answer[2048];
Expand All @@ -599,7 +599,7 @@ Dg100Format::dg100_getfile(int16_t num, route_head** track) const
}

void
Dg100Format::dg100_getfiles() const
Dg100Format::dg100_getfiles()
{
route_head* track = nullptr;

Expand All @@ -622,14 +622,14 @@ Dg100Format::dg100_getfiles() const
}

int
Dg100Format::dg100_erase() const
Dg100Format::dg100_erase()
{
uint8_t request[2] = { 0xFF, 0xFF };
uint8_t answer[4];

dg100_request(dg100cmd_erase, request, answer, sizeof(answer));
if (be_read32(answer) != 1) {
fprintf(stderr, "dg100_erase() FAILED\n");
warning("dg100_erase() FAILED\n");
return(-1);
}
return(0);
Expand Down
31 changes: 16 additions & 15 deletions dg-100.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,30 +108,31 @@ class Dg100Format : public Format

/* Member Functions */

const dg100_command* dg100_findcmd(int id) const;
const dg100_command* dg100_findcmd(int id);
static QDateTime bintime2utc(int date, int time);
static void dg100_debug(const char* hdr, int include_nl, size_t sz, unsigned char* buf);
[[gnu::format(printf, 1, 2)]] static void dg100_log(const char* fmt, ...);
void dg100_debug(const char* hdr, int include_nl, size_t sz, unsigned char* buf);
[[gnu::format(printf, 2, 3)]] void dg100_log(const char* fmt, ...);
static float bin2deg(int val);
void process_gpsfile(uint8_t* data, route_head** track) const;
void process_gpsfile(uint8_t* data, route_head** track);
static uint16_t dg100_checksum(const uint8_t* buf, int count);
size_t dg100_send(uint8_t cmd, const void* payload, size_t param_len) const;
int dg100_recv_byte() const;
int dg100_read_wait(void* handle, void* buf, unsigned int len, unsigned int ms) const;
int dg100_recv_frame(const dg100_command** cmdinfo_result, uint8_t** payload) const;
int dg100_recv(uint8_t expected_id, void* buf, unsigned int len) const;
int dg100_request(uint8_t cmd, const void* sendbuf, void* recvbuf, size_t count) const;
QList<int> dg100_getfileheaders() const;
void dg100_getconfig() const;
void dg100_getfile(int16_t num, route_head** track) const;
void dg100_getfiles() const;
int dg100_erase() const;
size_t dg100_send(uint8_t cmd, const void* payload, size_t param_len);
int dg100_recv_byte();
int dg100_read_wait(void* handle, void* buf, unsigned int len, unsigned int ms);
int dg100_recv_frame(const dg100_command** cmdinfo_result, uint8_t** payload);
int dg100_recv(uint8_t expected_id, void* buf, unsigned int len);
int dg100_request(uint8_t cmd, const void* sendbuf, void* recvbuf, size_t count);
QList<int> dg100_getfileheaders();
void dg100_getconfig();
void dg100_getfile(int16_t num, route_head** track);
void dg100_getfiles();
int dg100_erase();
void common_rd_init(const QString& fname);
void dg100_rd_init(const QString& fname, bool is_file);
void dg200_rd_init(const QString& fname, bool is_file);

/* Data Members */

DebugLog db;
const model_t* model{nullptr};
void* serial_handle{nullptr};
gbfile* fin{nullptr};
Expand Down
14 changes: 10 additions & 4 deletions fatal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,11 @@ debug(const char* fmt, ...)
qDebug().noquote() << msg;
}

int DebugLog::log(const char* fmt, ...)
int DebugLog::vlog(const char* fmt, va_list args1)
{
va_list args1;
va_start(args1, fmt);
va_list args2;
va_copy(args2, args1);
char cbuf[1 + vsnprintf(nullptr, 0, fmt, args1)];
va_end(args1);
vsnprintf(cbuf, sizeof cbuf, fmt, args2);
va_end(args2);

Expand All @@ -108,6 +105,15 @@ int DebugLog::log(const char* fmt, ...)
return rc;
}

int DebugLog::log(const char* fmt, ...)
{
va_list args1;
va_start(args1, fmt);
int rc = vlog(fmt, args1);
va_end(args1);
return rc;
}

int DebugLog::flush()
{
int rc = 0;
Expand Down

0 comments on commit 106cdd3

Please sign in to comment.