diff --git a/src/misc.c b/src/misc.c new file mode 100644 index 000000000..3ec0dda02 --- /dev/null +++ b/src/misc.c @@ -0,0 +1,214 @@ +/* + * hamlib - (C) Frank Singleton 2000 (vk3fcs@ix.netcom.com) + * + * misc.c - (C) Frank Singleton 2000 (vk3fcs@ix.netcom.com), + * Stephane Fillod 2000 + * Provides useful routines for data handling, used by backend + * as well as by the frontend. + * + * $Id: misc.c,v 1.1 2000-10-01 12:25:49 f4cfe Exp $ + * + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include +#include +#include /* Standard input/output definitions */ +#include /* String function definitions */ +#include /* UNIX standard function definitions */ +#include /* File control definitions */ +#include /* Error number definitions */ +#include +#include + +#include +#include "misc.h" + + + +static int rig_debug_level = RIG_DEBUG_TRACE; + +/* + * Do a hex dump of the unsigned char array. + */ + +#define DUMP_HEX_WIDTH 16 + +void dump_hex(const unsigned char ptr[], size_t size) +{ + int i; + char buf[DUMP_HEX_WIDTH+1]; + + if (!rig_need_debug(RIG_DEBUG_TRACE)) + return; + + buf[DUMP_HEX_WIDTH] = '\0'; + + for(i=0; i= ' ' && ptr[i] < 0x7f) + buf[i%DUMP_HEX_WIDTH] = ptr[i]; + else + buf[i%DUMP_HEX_WIDTH] = '.'; + + if (i % DUMP_HEX_WIDTH == DUMP_HEX_WIDTH-1) + rig_debug(RIG_DEBUG_TRACE,"\t%s\n",buf); + } + /* + * add some spaces in this case in order to align right ASCII dump column + */ + if (i % DUMP_HEX_WIDTH != DUMP_HEX_WIDTH-1) { + buf[i % DUMP_HEX_WIDTH] = '\0'; + rig_debug(RIG_DEBUG_TRACE,"\t%s\n",buf); + } + +} + + +/* + * Convert char to packed decimal + * eg: 33 (0x21) => 0x33 + * + */ + +char calc_packed_from_char(unsigned char dec ) { + + char d1,d2,pkd; + + d1 = dec/10; + d2 = dec - (d1 * 10); + pkd = (d1*16)+d2; + + return pkd; +} + + +/* + * Convert packed decimal to decimal + * eg: 0x33 (51) => 33 decimal + * + */ + +char calc_char_from_packed(unsigned char pkd ) { + + char d1,d2,dec; + + d1 = pkd/16; + d2 = pkd - (d1 * 16); + dec = (d1*10)+d2; + + return dec; +} + + + +/* + * Convert a long long (eg. frequency in Hz) to 4-bit BCD digits, + * packed two digits per octet, in little-endian order. + * bcd_len is the number of BCD digits, usually 10 or 8 in 1-Hz units, + * and 6 digits in 100-Hz units for Tx offset data. + * + * Hope the compiler will do a good job optimizing it (esp. w/ the 64bit freq) + */ +unsigned char * +to_bcd(unsigned char bcd_data[], unsigned long long freq, int bcd_len) +{ + int i; + unsigned char a; + + /* '450'-> 0,5;4,0 */ + + for (i=0; i < bcd_len/2; i++) { + a = freq%10; + freq /= 10; + a |= (freq%10)<<4; + freq /= 10; + bcd_data[i] = a; + } + if (bcd_len&1) + bcd_data[i] |= freq%10; /* NB: high nibble is left uncleared */ + + return bcd_data; +} + +/* + * Convert BCD digits to a long long (eg. frequency in Hz) + * bcd_len is the number of BCD digits. + * + * Hope the compiler will do a good job optimizing it (esp. w/ the 64bit freq) + */ +unsigned long long from_bcd(const unsigned char bcd_data[], int bcd_len) +{ + int i; + freq_t f = 0; + + if (bcd_len&1) + f = bcd_data[bcd_len/2] & 0x0f; + + for (i=(bcd_len/2)-1; i >= 0; i--) { + f *= 10; + f += bcd_data[i]>>4; + f *= 10; + f += bcd_data[i] & 0x0f; + } + + return f; +} + + +/* + * rig_set_debug + * Change the current debug level + */ +void rig_set_debug(enum rig_debug_level_e debug_level) +{ + rig_debug_level = debug_level; +} + +/* + * rig_need_debug + * Usefull for dump_hex, etc. + */ +int rig_need_debug(enum rig_debug_level_e debug_level) +{ + return (debug_level <= rig_debug_level); +} + +/* + * rig_debug + * Debugging messages are done through stderr + * TODO: add syslog suport if needed + */ +void rig_debug(enum rig_debug_level_e debug_level, const char *fmt, ...) +{ + va_list ap; + + if (debug_level <= rig_debug_level) { + va_start(ap, fmt); + /* + * Who cares about return code? + */ + vfprintf (stderr, fmt, ap); + va_end(ap); + } +} + + diff --git a/src/misc.h b/src/misc.h new file mode 100644 index 000000000..1b607f366 --- /dev/null +++ b/src/misc.h @@ -0,0 +1,66 @@ +/* + * hamlib - (C) Frank Singleton 2000 (vk3fcs@ix.netcom.com) + * + * misc.h - (C) Frank Singleton 2000 (vk3fcs@ix.netcom.com), + * Stephane Fillod 2000 + * Provides useful routines for data handling, used by backends + * as well as by the frontend. + * + * $Id: misc.h,v 1.1 2000-10-01 12:25:49 f4cfe Exp $ + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + + +#ifndef _MISC_H +#define _MISC_H 1 + +/* + * Convert char to packed decimal + * eg: 33 (0x21) => 0x33 + * + */ + +char calc_packed_from_char(unsigned char dec ); + + +/* + * Convert packed decimal to decimal + * eg: 0x33 (51) => 33 decimal + * + */ + +char calc_char_from_packed(unsigned char pkd ); + +/* + * Do a hex dump of the unsigned char array. + */ + +void dump_hex(const unsigned char ptr[], size_t size); + +/* + * BCD conversion routines. + * to_bcd converts a long long int to a little endian BCD array, + * and return a pointer to this array. + * from_bcd converts a little endian BCD array to long long int + * reprensentation, and return it. + * bcd_len is the number of digits in the BCD array. + */ +unsigned char *to_bcd(unsigned char bcd_data[], unsigned long long freq, int bcd_len); +unsigned long long from_bcd(const unsigned char bcd_data[], int bcd_len); + +#endif /* _MISC_H */ +