2005-04-09 09:49:13 +00:00
|
|
|
/*
|
|
|
|
* Hamlib Interface - debug
|
2010-05-29 14:18:48 +00:00
|
|
|
* Copyright (c) 2000-2010 by Stephane Fillod
|
2005-04-09 09:49:13 +00:00
|
|
|
*
|
|
|
|
*
|
2011-08-22 01:07:57 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2005-04-09 09:49:13 +00:00
|
|
|
*
|
2011-08-22 01:07:57 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2005-04-09 09:49:13 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2011-08-22 01:07:57 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2005-04-09 09:49:13 +00:00
|
|
|
*
|
2011-08-22 01:07:57 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2005-04-09 09:49:13 +00:00
|
|
|
* License along with this library; if not, write to the Free Software
|
2011-08-22 01:07:57 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
2005-04-09 09:49:13 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-15 00:27:52 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup rig_internal
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file debug.c
|
|
|
|
* \brief control hamlib debugging functions
|
|
|
|
*/
|
|
|
|
|
2005-04-09 09:49:13 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2017-10-05 02:32:08 +00:00
|
|
|
# include "config.h"
|
2005-04-09 09:49:13 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
2017-08-05 14:09:12 +00:00
|
|
|
#include <stdio.h> /* Standard input/output definitions */
|
|
|
|
#include <string.h> /* String function definitions */
|
|
|
|
#include <unistd.h> /* UNIX standard function definitions */
|
|
|
|
#include <fcntl.h> /* File control definitions */
|
|
|
|
#include <errno.h> /* Error number definitions */
|
2005-04-09 09:49:13 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2018-04-14 22:20:28 +00:00
|
|
|
#include <time.h>
|
2005-04-09 09:49:13 +00:00
|
|
|
|
2012-12-19 11:59:59 +00:00
|
|
|
#ifdef ANDROID
|
2017-10-05 02:32:08 +00:00
|
|
|
# include <android/log.h>
|
2012-12-19 11:59:59 +00:00
|
|
|
#endif
|
|
|
|
|
2005-04-09 09:49:13 +00:00
|
|
|
#include <hamlib/rig.h>
|
2020-01-22 23:04:25 +00:00
|
|
|
#include <hamlib/rig_dll.h>
|
2005-04-09 09:49:13 +00:00
|
|
|
#include "misc.h"
|
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
#define DUMP_HEX_WIDTH 16
|
|
|
|
|
|
|
|
|
2005-04-09 09:49:13 +00:00
|
|
|
static int rig_debug_level = RIG_DEBUG_TRACE;
|
2018-04-14 22:20:28 +00:00
|
|
|
static int rig_debug_time_stamp = 0;
|
2005-04-09 09:49:13 +00:00
|
|
|
static FILE *rig_debug_stream;
|
|
|
|
static vprintf_cb_t rig_vprintf_cb;
|
|
|
|
static rig_ptr_t rig_vprintf_arg;
|
|
|
|
|
2020-01-23 23:20:41 +00:00
|
|
|
extern HAMLIB_EXPORT(void) dump_hex(const unsigned char ptr[], size_t size);
|
2005-04-09 09:49:13 +00:00
|
|
|
|
2006-10-15 00:27:52 +00:00
|
|
|
/**
|
|
|
|
* \param ptr Pointer to memory area
|
|
|
|
* \param size Number of chars to words to dump
|
|
|
|
* \brief Do a hex dump of the unsigned char array.
|
|
|
|
*/
|
2020-01-23 23:20:41 +00:00
|
|
|
void dump_hex(const unsigned char ptr[], size_t size)
|
2005-04-09 09:49:13 +00:00
|
|
|
{
|
2019-11-30 16:19:08 +00:00
|
|
|
/* example
|
|
|
|
* 0000 4b 30 30 31 34 35 30 30 30 30 30 30 30 35 30 32 K001450000000502
|
|
|
|
* 0010 30 30 0d 0a 00..
|
|
|
|
*/
|
|
|
|
char line[4 + 4 + 3 * DUMP_HEX_WIDTH + 4 + DUMP_HEX_WIDTH + 1];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!rig_need_debug(RIG_DEBUG_TRACE))
|
2017-10-05 02:32:08 +00:00
|
|
|
{
|
2019-11-30 16:19:08 +00:00
|
|
|
return;
|
2017-08-05 14:09:12 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
line[sizeof(line) - 1] = '\0';
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
for (i = 0; i < size; ++i)
|
2017-10-05 02:32:08 +00:00
|
|
|
{
|
2019-12-12 16:14:42 +00:00
|
|
|
unsigned char c;
|
2020-02-23 17:26:09 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
if (i % DUMP_HEX_WIDTH == 0)
|
|
|
|
{
|
|
|
|
/* new line */
|
|
|
|
sprintf(line + 0, "%04x", i);
|
|
|
|
memset(line + 4, ' ', sizeof(line) - 4 - 1);
|
|
|
|
}
|
|
|
|
|
2019-12-09 23:12:13 +00:00
|
|
|
c = ptr[i];
|
2019-11-30 16:19:08 +00:00
|
|
|
|
|
|
|
/* hex print */
|
|
|
|
sprintf(line + 8 + 3 * (i % DUMP_HEX_WIDTH), "%02x", c);
|
|
|
|
line[8 + 3 * (i % DUMP_HEX_WIDTH) + 2] = ' '; /* no \0 */
|
|
|
|
|
|
|
|
/* ascii print */
|
|
|
|
line[8 + 3 * DUMP_HEX_WIDTH + 4 + (i % DUMP_HEX_WIDTH)] = (c >= ' '
|
|
|
|
&& c < 0x7f) ? c : '.';
|
|
|
|
|
|
|
|
/* actually print the line */
|
|
|
|
if (i + 1 == size || (i && i % DUMP_HEX_WIDTH == DUMP_HEX_WIDTH - 1))
|
|
|
|
{
|
|
|
|
rig_debug(RIG_DEBUG_TRACE, "%s\n", line);
|
|
|
|
}
|
2017-08-05 14:09:12 +00:00
|
|
|
}
|
2011-06-19 23:43:53 +00:00
|
|
|
}
|
2005-04-09 09:49:13 +00:00
|
|
|
|
|
|
|
|
2006-10-15 00:27:52 +00:00
|
|
|
/**
|
|
|
|
* \param debug_level
|
|
|
|
* \brief Change the current debug level
|
2005-04-09 09:49:13 +00:00
|
|
|
*/
|
|
|
|
void HAMLIB_API rig_set_debug(enum rig_debug_level_e debug_level)
|
|
|
|
{
|
2019-11-30 16:19:08 +00:00
|
|
|
rig_debug_level = debug_level;
|
2005-04-09 09:49:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2006-10-15 00:27:52 +00:00
|
|
|
/**
|
|
|
|
* \param debug_level
|
|
|
|
* \brief Useful for dump_hex, etc.
|
2005-04-09 09:49:13 +00:00
|
|
|
*/
|
|
|
|
int HAMLIB_API rig_need_debug(enum rig_debug_level_e debug_level)
|
|
|
|
{
|
2019-11-30 16:19:08 +00:00
|
|
|
return (debug_level <= rig_debug_level);
|
2005-04-09 09:49:13 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 22:20:28 +00:00
|
|
|
/**
|
|
|
|
* \param debug_time_stamp
|
|
|
|
* \brief Enbable/disable time stamp on debug output
|
|
|
|
*/
|
|
|
|
void HAMLIB_API rig_set_debug_time_stamp(int flag)
|
|
|
|
{
|
2019-11-30 16:19:08 +00:00
|
|
|
rig_debug_time_stamp = flag;
|
2018-04-14 22:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-22 13:52:50 +00:00
|
|
|
char *date_strget(char *buf, int buflen)
|
2018-04-14 22:20:28 +00:00
|
|
|
{
|
2019-12-09 23:12:13 +00:00
|
|
|
char tmp[16];
|
2019-11-30 16:19:08 +00:00
|
|
|
time_t mytime;
|
|
|
|
struct tm *mytm;
|
|
|
|
struct timeval tv;
|
|
|
|
mytime = time(NULL);
|
|
|
|
mytm = gmtime(&mytime);
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
strftime(buf, buflen, "%Y-%m-%d:%H:%M:%S.", mytm);
|
|
|
|
sprintf(tmp, "%06ld", (long)tv.tv_usec);
|
|
|
|
strcat(buf, tmp);
|
|
|
|
return buf;
|
2018-04-14 22:20:28 +00:00
|
|
|
}
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2006-10-15 00:27:52 +00:00
|
|
|
/**
|
|
|
|
* \param debug_level
|
|
|
|
* \param fmt
|
|
|
|
* \brief Default is debugging messages are done through stderr
|
2005-04-09 09:49:13 +00:00
|
|
|
*/
|
2019-11-30 16:04:31 +00:00
|
|
|
#undef rig_debug
|
2017-08-05 14:09:12 +00:00
|
|
|
void HAMLIB_API rig_debug(enum rig_debug_level_e debug_level,
|
|
|
|
const char *fmt, ...)
|
2005-04-09 09:49:13 +00:00
|
|
|
{
|
2019-11-30 16:19:08 +00:00
|
|
|
va_list ap;
|
2005-04-09 09:49:13 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
if (!rig_need_debug(debug_level))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2005-04-09 09:49:13 +00:00
|
|
|
|
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
va_start(ap, fmt);
|
2005-04-09 09:49:13 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
if (rig_vprintf_cb)
|
2017-10-05 02:32:08 +00:00
|
|
|
{
|
2019-11-30 16:19:08 +00:00
|
|
|
rig_vprintf_cb(debug_level, rig_vprintf_arg, fmt, ap);
|
2017-10-05 02:32:08 +00:00
|
|
|
}
|
2019-11-30 16:19:08 +00:00
|
|
|
else
|
2017-10-05 02:32:08 +00:00
|
|
|
{
|
2019-11-30 16:19:08 +00:00
|
|
|
if (!rig_debug_stream)
|
|
|
|
{
|
|
|
|
rig_debug_stream = stderr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rig_debug_time_stamp)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
fprintf(rig_debug_stream, "%s: ", date_strget(buf, sizeof(buf)));
|
|
|
|
}
|
|
|
|
|
|
|
|
vfprintf(rig_debug_stream, fmt, ap);
|
|
|
|
fflush(rig_debug_stream);
|
2017-08-05 14:09:12 +00:00
|
|
|
}
|
2005-04-09 09:49:13 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
va_end(ap);
|
2012-12-19 11:59:59 +00:00
|
|
|
#ifdef ANDROID
|
2019-11-30 16:19:08 +00:00
|
|
|
int a;
|
|
|
|
va_start(ap, fmt);
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
switch (debug_level)
|
|
|
|
{
|
2012-12-19 11:59:59 +00:00
|
|
|
// case RIG_DEBUG_NONE:
|
2019-11-30 16:19:08 +00:00
|
|
|
case RIG_DEBUG_BUG:
|
|
|
|
a = ANDROID_LOG_FATAL;
|
|
|
|
break;
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
case RIG_DEBUG_ERR:
|
|
|
|
a = ANDROID_LOG_ERROR;
|
|
|
|
break;
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
case RIG_DEBUG_WARN:
|
|
|
|
a = ANDROID_LOG_WARN;
|
|
|
|
break;
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
case RIG_DEBUG_VERBOSE:
|
|
|
|
a = ANDROID_LOG_VERBOSE;
|
|
|
|
break;
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
case RIG_DEBUG_TRACE:
|
|
|
|
a = ANDROID_LOG_VERBOSE;
|
|
|
|
break;
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
default:
|
|
|
|
a = ANDROID_LOG_DEBUG;
|
|
|
|
break;
|
|
|
|
}
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
__android_log_vprint(a, PACKAGE_NAME, fmt, ap);
|
2012-12-19 11:59:59 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
va_end(ap);
|
2012-12-19 11:59:59 +00:00
|
|
|
#endif
|
2005-04-09 09:49:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2005-04-09 09:49:13 +00:00
|
|
|
/**
|
|
|
|
* \brief set callback to handle debug messages
|
2017-08-05 14:09:12 +00:00
|
|
|
* \param cb The callback to install
|
|
|
|
* \param arg A Pointer to some private data to pass later on to the callback
|
2005-04-09 09:49:13 +00:00
|
|
|
*
|
|
|
|
* Install a callback for \a rig_debug messages.
|
2006-01-16 21:00:25 +00:00
|
|
|
\code
|
|
|
|
int
|
2017-08-05 14:09:12 +00:00
|
|
|
rig_message_cb(enum rig_debug_level_e debug_level,
|
|
|
|
rig_ptr_t user_data,
|
|
|
|
const char *fmt,
|
|
|
|
va_list ap)
|
2006-01-16 21:00:25 +00:00
|
|
|
{
|
2017-08-05 14:09:12 +00:00
|
|
|
char buf[1024];
|
2006-01-16 21:00:25 +00:00
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
sprintf (buf, "Message(%s) ", (char*)user_data);
|
|
|
|
syslog (LOG_USER, buf);
|
|
|
|
vsprintf (buf, fmt, ap);
|
|
|
|
syslog (LOG_USER, buf);
|
2006-01-16 21:00:25 +00:00
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
return RIG_OK;
|
2006-01-16 21:00:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
. . .
|
2006-01-16 21:00:25 +00:00
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
char *cookie = "Foo";
|
|
|
|
rig_set_debug_callback (rig_message_cb, (rig_ptr_t)cookie);
|
2006-01-16 21:00:25 +00:00
|
|
|
\endcode
|
2005-04-09 09:49:13 +00:00
|
|
|
*
|
|
|
|
* \return RIG_OK if the operation has been sucessful, otherwise
|
|
|
|
* a negative value if an error occured (in which case, cause
|
|
|
|
* is set appropriately).
|
|
|
|
*
|
|
|
|
* \sa rig_debug()
|
|
|
|
*/
|
2006-02-26 23:28:13 +00:00
|
|
|
vprintf_cb_t HAMLIB_API rig_set_debug_callback(vprintf_cb_t cb, rig_ptr_t arg)
|
2005-04-09 09:49:13 +00:00
|
|
|
{
|
2019-11-30 16:19:08 +00:00
|
|
|
vprintf_cb_t prev_cb = rig_vprintf_cb;
|
2005-04-09 09:49:13 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
rig_vprintf_cb = cb;
|
|
|
|
rig_vprintf_arg = arg;
|
2005-04-09 09:49:13 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
return prev_cb;
|
2005-04-09 09:49:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2005-04-09 09:49:13 +00:00
|
|
|
/**
|
|
|
|
* \brief change stderr to some different output
|
|
|
|
* \param stream The stream to set output to
|
|
|
|
*/
|
2019-05-22 13:52:50 +00:00
|
|
|
FILE *HAMLIB_API rig_set_debug_file(FILE *stream)
|
2005-04-09 09:49:13 +00:00
|
|
|
{
|
2019-11-30 16:19:08 +00:00
|
|
|
FILE *prev_stream = rig_debug_stream;
|
2005-04-09 09:49:13 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
rig_debug_stream = stream;
|
2005-04-09 09:49:13 +00:00
|
|
|
|
2019-11-30 16:19:08 +00:00
|
|
|
return prev_stream;
|
2005-04-09 09:49:13 +00:00
|
|
|
}
|
|
|
|
|
2006-10-15 00:27:52 +00:00
|
|
|
/** @} */
|