Hamlib  4.5.4
misc.h
1 /*
2  * Hamlib Interface - toolbox header
3  * Copyright (c) 2000-2004 by Stephane Fillod
4  *
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  */
21 
22 #ifndef _MISC_H
23 #define _MISC_H 1
24 
25 #include <hamlib/rig.h>
26 #include <hamlib/config.h>
27 
28 
29 /*
30  */
31 #ifdef HAVE_PTHREAD
32 #include <pthread.h>
33 #define set_transaction_active(rig) {pthread_mutex_lock(&rig->state.mutex_set_transaction);(rig)->state.transaction_active = 1;}
34 #define set_transaction_inactive(rig) {(rig)->state.transaction_active = 0;pthread_mutex_unlock(&rig->state.mutex_set_transaction);}
35 #else
36 #define set_transaction_active(rig) {(rig)->state.transaction_active = 1;}
37 #define set_transaction_inactive(rig) {(rig)->state.transaction_active = 0;}
38 #endif
39 
40 __BEGIN_DECLS
41 
42 // a function to return just a string of spaces for indenting rig debug lines
43 HAMLIB_EXPORT (const char *) spaces();
44 /*
45  * Do a hex dump of the unsigned char array.
46  */
47 
48 void dump_hex(const unsigned char ptr[], size_t size);
49 
50 /*
51  * BCD conversion routines.
52  *
53  * to_bcd() converts a long long int to a little endian BCD array,
54  * and return a pointer to this array.
55  *
56  * from_bcd() converts a little endian BCD array to long long int
57  * representation, and return it.
58  *
59  * bcd_len is the number of digits in the BCD array.
60  */
61 extern HAMLIB_EXPORT(unsigned char *) to_bcd(unsigned char bcd_data[],
62  unsigned long long freq,
63  unsigned bcd_len);
64 
65 extern HAMLIB_EXPORT(unsigned long long) from_bcd(const unsigned char
66  bcd_data[],
67  unsigned bcd_len);
68 
69 /*
70  * same as to_bcd() and from_bcd(), but in Big Endian mode
71  */
72 extern HAMLIB_EXPORT(unsigned char *) to_bcd_be(unsigned char bcd_data[],
73  unsigned long long freq,
74  unsigned bcd_len);
75 
76 extern HAMLIB_EXPORT(unsigned long long) from_bcd_be(const unsigned char
77  bcd_data[],
78  unsigned bcd_len);
79 
80 extern HAMLIB_EXPORT(size_t) to_hex(size_t source_length,
81  const unsigned char *source_data,
82  size_t dest_length,
83  char *dest_data);
84 
85 extern HAMLIB_EXPORT(double) morse_code_dot_to_millis(int wpm);
86 extern HAMLIB_EXPORT(int) dot10ths_to_millis(int dot10ths, int wpm);
87 extern HAMLIB_EXPORT(int) millis_to_dot10ths(int millis, int wpm);
88 
89 extern HAMLIB_EXPORT(int) sprintf_freq(char *str, int str_len, freq_t);
90 
91 /* flag that determines if AI mode should be restored on exit on
92  applicable rigs - See rig_no_restore_ai() */
93 extern int no_restore_ai;
94 
95 /* check if it's any of CR or LF */
96 #define isreturn(c) ((c) == 10 || (c) == 13)
97 
98 
99 /* needs config.h included beforehand in .c file */
100 #ifdef HAVE_INTTYPES_H
101 # include <inttypes.h>
102 #endif
103 
104 #ifdef HAVE_SYS_TIME_H
105 # include <sys/time.h>
106 #endif
107 
108 extern HAMLIB_EXPORT(int) rig_check_cache_timeout(const struct timeval *tv,
109  int timeout);
110 
111 extern HAMLIB_EXPORT(void) rig_force_cache_timeout(struct timeval *tv);
112 
113 extern HAMLIB_EXPORT(setting_t) rig_idx2setting(int i);
114 
115 extern HAMLIB_EXPORT(int) hl_usleep(rig_useconds_t usec);
116 
117 extern HAMLIB_EXPORT(double) elapsed_ms(struct timespec *start, int start_flag);
118 
119 extern HAMLIB_EXPORT(vfo_t) vfo_fixup(RIG *rig, vfo_t vfo, split_t split);
120 extern HAMLIB_EXPORT(vfo_t) vfo_fixup2a(RIG *rig, vfo_t vfo, split_t split, const char *func, const int line);
121 #define vfo_fixup(r,v,s) vfo_fixup2a(r,v,s,__func__,__LINE__)
122 
123 extern HAMLIB_EXPORT(int) parse_hoststr(char *hoststr, int hoststr_len, char host[256], char port[6]);
124 
125 extern HAMLIB_EXPORT(uint32_t) CRC32_function(uint8_t *buf, uint32_t len);
126 
127 extern HAMLIB_EXPORT(char *)date_strget(char *buf, int buflen, int localtime);
128 
129 #ifdef PRId64
131 # define PRIll PRId64
132 # define PRXll PRIx64
133 #else
134 # ifdef FBSD4
135 # define PRIll "qd"
136 # define PRXll "qx"
137 # else
138 # define PRIll "lld"
139 # define PRXll "lld"
140 # endif
141 #endif
142 
143 #ifdef SCNd64
145 # define SCNll SCNd64
146 # define SCNXll SCNx64
147 #else
148 # ifdef FBSD4
149 # define SCNll "qd"
150 # define SCNXll "qx"
151 # else
152 # define SCNll "lld"
153 # define SCNXll "llx"
154 # endif
155 #endif
156 
157 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
158 void errmsg(int err, char *s, const char *func, const char *file, int line);
159 #define ERRMSG(err, s) errmsg(err, s, __func__, __FILENAME__, __LINE__)
160 #define ENTERFUNC { ++rig->state.depth; \
161  rig_debug(RIG_DEBUG_VERBOSE, "%.*s%d:%s(%d):%s entered\n", rig->state.depth, spaces(), rig->state.depth, __FILENAME__, __LINE__, __func__); \
162  }
163 #define ENTERFUNC2 { rig_debug(RIG_DEBUG_VERBOSE, "%s(%d):%s entered\n", __FILENAME__, __LINE__, __func__); \
164  }
165 // we need to refer to rc just once as it
166 // could be a function call
167 #define RETURNFUNC(rc) {do { \
168  int rctmp = rc; \
169  rig_debug(RIG_DEBUG_VERBOSE, "%.*s%d:%s(%d):%s returning(%ld) %s\n", rig->state.depth, spaces(), rig->state.depth, __FILENAME__, __LINE__, __func__, (long int) (rctmp), rctmp<0?rigerror2(rctmp):""); \
170  --rig->state.depth; \
171  return (rctmp); \
172  } while(0);}
173 #define RETURNFUNC2(rc) {do { \
174  int rctmp = rc; \
175  rig_debug(RIG_DEBUG_VERBOSE, "%s(%d):%s returning2(%ld) %s\n", __FILENAME__, __LINE__, __func__, (long int) (rctmp), rctmp<0?rigerror2(rctmp):""); \
176  return (rctmp); \
177  } while(0);}
178 
179 #define CACHE_RESET {\
180  elapsed_ms(&rig->state.cache.time_freqMainA, HAMLIB_ELAPSED_INVALIDATE);\
181  elapsed_ms(&rig->state.cache.time_freqMainB, HAMLIB_ELAPSED_INVALIDATE);\
182  elapsed_ms(&rig->state.cache.time_freqSubA, HAMLIB_ELAPSED_INVALIDATE);\
183  elapsed_ms(&rig->state.cache.time_freqSubB, HAMLIB_ELAPSED_INVALIDATE);\
184  elapsed_ms(&rig->state.cache.time_vfo, HAMLIB_ELAPSED_INVALIDATE);\
185  elapsed_ms(&rig->state.cache.time_modeMainA, HAMLIB_ELAPSED_INVALIDATE);\
186  elapsed_ms(&rig->state.cache.time_modeMainB, HAMLIB_ELAPSED_INVALIDATE);\
187  elapsed_ms(&rig->state.cache.time_modeMainC, HAMLIB_ELAPSED_INVALIDATE);\
188  elapsed_ms(&rig->state.cache.time_modeSubA, HAMLIB_ELAPSED_INVALIDATE);\
189  elapsed_ms(&rig->state.cache.time_modeSubB, HAMLIB_ELAPSED_INVALIDATE);\
190  elapsed_ms(&rig->state.cache.time_modeSubC, HAMLIB_ELAPSED_INVALIDATE);\
191  elapsed_ms(&rig->state.cache.time_widthMainA, HAMLIB_ELAPSED_INVALIDATE);\
192  elapsed_ms(&rig->state.cache.time_widthMainB, HAMLIB_ELAPSED_INVALIDATE);\
193  elapsed_ms(&rig->state.cache.time_widthMainC, HAMLIB_ELAPSED_INVALIDATE);\
194  elapsed_ms(&rig->state.cache.time_widthSubA, HAMLIB_ELAPSED_INVALIDATE);\
195  elapsed_ms(&rig->state.cache.time_widthSubB, HAMLIB_ELAPSED_INVALIDATE);\
196  elapsed_ms(&rig->state.cache.time_widthSubC, HAMLIB_ELAPSED_INVALIDATE);\
197  elapsed_ms(&rig->state.cache.time_ptt, HAMLIB_ELAPSED_INVALIDATE);\
198  elapsed_ms(&rig->state.cache.time_split, HAMLIB_ELAPSED_INVALIDATE);\
199  }
200 
201 
202 typedef enum settings_value_e
203 {
204  e_CHAR, e_INT, e_LONG, e_FLOAT, e_DOUBLE
205 } settings_value_t;
206 
207 
208 extern HAMLIB_EXPORT(int) rig_settings_save(char *setting, void *value, settings_value_t valuet);
209 extern HAMLIB_EXPORT(int) rig_settings_load(char *setting, void *value, settings_value_t valuet);
210 extern HAMLIB_EXPORT(int) rig_settings_load_all(char *settings_file);
211 
212 __END_DECLS
213 
214 #endif /* _MISC_H */
int millis_to_dot10ths(int millis, int wpm)
Convert duration in milliseconds to tenths of morse code dots at the given speed.
Definition: misc.c:361
int rig_check_cache_timeout(const struct timeval *tv, int timeout)
Helper for checking cache timeout.
Definition: misc.c:1712
unsigned char * to_bcd(unsigned char bcd_data[], unsigned long long freq, unsigned bcd_len)
Convert from binary to 4-bit BCD digits, little-endian.
Definition: misc.c:143
void rig_force_cache_timeout(struct timeval *tv)
Helper for forcing cache timeout next call.
Definition: misc.c:1766
setting_t rig_idx2setting(int i)
Definition: misc.c:758
void dump_hex(const unsigned char ptr[], size_t size)
Do a hex dump of the unsigned char array.
Definition: debug.c:84
unsigned long long from_bcd(const unsigned char bcd_data[], unsigned bcd_len)
Convert BCD digits, little-endian, to a long long (e.g. frequency in Hz)
Definition: misc.c:190
unsigned long long from_bcd_be(const unsigned char bcd_data[], unsigned bcd_len)
Convert 4-bit BCD digits to binary, big-endian.
Definition: misc.c:270
unsigned char * to_bcd_be(unsigned char bcd_data[], unsigned long long freq, unsigned bcd_len)
Convert from binary to 4-bit BCD digits, big-endian.
Definition: misc.c:227
double morse_code_dot_to_millis(int wpm)
Convert duration of one morse code dot (element) to milliseconds at the given speed.
Definition: misc.c:335
int dot10ths_to_millis(int dot10ths, int wpm)
Convert duration of tenths of morse code dots to milliseconds at the given speed.
Definition: misc.c:348
int sprintf_freq(char *str, int str_len, freq_t)
Pretty print a frequency.
Definition: misc.c:382
unsigned int vfo_t
VFO definition.
Definition: rig.h:470
split_t
Split mode.
Definition: rig.h:399
int hl_usleep(rig_useconds_t usec)
provide sleep and usleep replacements
Definition: sleep.c:52
uint64_t setting_t
Setting.
Definition: rig.h:1092
double freq_t
Frequency type,.
Definition: rig.h:410
int rig_settings_save(char *setting, void *value, settings_value_t valuet)
Save setting parameter.
Definition: settings.c:1036
Hamlib rig data structures.
The Rig structure.
Definition: rig.h:2662