Hamlib 4.7~git
Loading...
Searching...
No Matches
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/* SPDX-License-Identifier: LGPL-2.1-or-later */
22
23#ifndef _MISC_H
24#define _MISC_H 1
25
26#include "hamlib/config.h"
27#include "hamlib/rig.h"
28
29
30/*
31 */
32#include <pthread.h>
33#define set_transaction_active(rig) {pthread_mutex_lock(&STATE(rig)->mutex_set_transaction);STATE(rig)->transaction_active = 1;}
34#define set_transaction_inactive(rig) {STATE(rig)->transaction_active = 0;pthread_mutex_unlock(&STATE(rig)->mutex_set_transaction);}
35
36__BEGIN_DECLS
37
38// a function to return just a string of spaces for indenting rig debug lines
39HAMLIB_EXPORT (const char *) spaces(int len);
40/*
41 * Do a hex dump of the unsigned char array.
42 */
43
44void dump_hex(const unsigned char ptr[], size_t size);
45
46/*
47 * BCD conversion routines.
48 *
49 * to_bcd() converts a long long int to a little endian BCD array,
50 * and return a pointer to this array.
51 *
52 * from_bcd() converts a little endian BCD array to long long int
53 * representation, and return it.
54 *
55 * bcd_len is the number of digits in the BCD array.
56 */
57extern HAMLIB_EXPORT(unsigned char *) to_bcd(unsigned char bcd_data[],
58 unsigned long long freq,
59 unsigned bcd_len);
60
61extern HAMLIB_EXPORT(unsigned long long) from_bcd(const unsigned char
62 bcd_data[],
63 unsigned bcd_len);
64
65/*
66 * same as to_bcd() and from_bcd(), but in Big Endian mode
67 */
68extern HAMLIB_EXPORT(unsigned char *) to_bcd_be(unsigned char bcd_data[],
69 unsigned long long freq,
70 unsigned bcd_len);
71
72extern HAMLIB_EXPORT(unsigned long long) from_bcd_be(const unsigned char
73 bcd_data[],
74 unsigned bcd_len);
75
76extern HAMLIB_EXPORT(size_t) to_hex(size_t source_length,
77 const unsigned char *source_data,
78 size_t dest_length,
79 char *dest_data);
80
81extern HAMLIB_EXPORT(double) morse_code_dot_to_millis(int wpm);
82extern HAMLIB_EXPORT(int) dot10ths_to_millis(int dot10ths, int wpm);
83extern HAMLIB_EXPORT(int) millis_to_dot10ths(int millis, int wpm);
84
85extern HAMLIB_EXPORT(int) sprintf_freq(char *str, int str_len, freq_t);
86
87/* flag that determines if AI mode should be restored on exit on
88 applicable rigs - See rig_no_restore_ai() */
89extern int no_restore_ai;
90
91/* check if it's any of CR or LF */
92#define isreturn(c) ((c) == 10 || (c) == 13)
93
94
95/* needs config.h included beforehand in .c file */
96#ifdef HAVE_INTTYPES_H
97# include <inttypes.h>
98#endif
99
100#ifdef HAVE_SYS_TIME_H
101# include <sys/time.h>
102#endif
103
104extern HAMLIB_EXPORT(int) rig_check_cache_timeout(const struct timeval *tv,
105 int timeout);
106
107extern HAMLIB_EXPORT(void) rig_force_cache_timeout(struct timeval *tv);
108
109extern HAMLIB_EXPORT(setting_t) rig_idx2setting(int i);
110
111extern HAMLIB_EXPORT(int) hl_usleep(rig_useconds_t usec);
112
113extern HAMLIB_EXPORT(double) elapsed_ms(struct timespec *start, int start_flag);
114
115extern HAMLIB_EXPORT(vfo_t) vfo_fixup(RIG *rig, vfo_t vfo, split_t split);
116extern HAMLIB_EXPORT(vfo_t) vfo_fixup2a(RIG *rig, vfo_t vfo, split_t split, const char *func, const int line);
117#define vfo_fixup(r,v,s) vfo_fixup2a(r,v,s,__func__,__LINE__)
118
119extern HAMLIB_EXPORT(int) parse_hoststr(char *hoststr, int hoststr_len, char host[256], char port[6]);
120
121extern HAMLIB_EXPORT(uint32_t) CRC32_function(const uint8_t *buf, uint32_t len);
122
123extern HAMLIB_EXPORT(char *)date_strget(char *buf, int buflen, int localtime);
124
125#ifdef PRId64
127# define PRIll PRId64
128# define PRXll PRIx64
129#else
130# ifdef FBSD4
131# define PRIll "qd"
132# define PRXll "qx"
133# else
134# define PRIll "lld"
135# define PRXll "lld"
136# endif
137#endif
138
139#ifdef SCNd64
141# define SCNll SCNd64
142# define SCNXll SCNx64
143#else
144# ifdef FBSD4
145# define SCNll "qd"
146# define SCNXll "qx"
147# else
148# define SCNll "lld"
149# define SCNXll "llx"
150# endif
151#endif
152
153#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
154void errmsg(int err, char *s, const char *func, const char *file, int line);
155#define ERRMSG(err, s) errmsg(err, s, __func__, __FILENAME__, __LINE__)
156#define ENTERFUNC { ++STATE(rig)->depth; \
157 rig_debug(RIG_DEBUG_VERBOSE, "%s%d:%s(%d):%s entered\n", spaces(STATE(rig)->depth), STATE(rig)->depth, __FILENAME__, __LINE__, __func__); \
158 }
159#define ENTERFUNC2 { rig_debug(RIG_DEBUG_VERBOSE, "%s(%d):%s entered\n", __FILENAME__, __LINE__, __func__); \
160 }
161// we need to refer to rc just once as it
162// could be a function call
163#define RETURNFUNC(rc) {do { \
164 int rctmp = rc; \
165 rig_debug(RIG_DEBUG_VERBOSE, "%s%d:%s(%d):%s returning(%ld) %s\n", spaces(STATE(rig)->depth), STATE(rig)->depth, __FILENAME__, __LINE__, __func__, (long int) (rctmp), rctmp<0?rigerror2(rctmp):""); \
166 --STATE(rig)->depth; \
167 return (rctmp); \
168 } while(0);}
169#define RETURNFUNC2(rc) {do { \
170 int rctmp = rc; \
171 rig_debug(RIG_DEBUG_VERBOSE, "%s(%d):%s returning2(%ld) %s\n", __FILENAME__, __LINE__, __func__, (long int) (rctmp), rctmp<0?rigerror2(rctmp):""); \
172 return (rctmp); \
173 } while(0);}
174
175#define CACHE_RESET {\
176 elapsed_ms(&CACHE(rig)->time_freqMainA, HAMLIB_ELAPSED_INVALIDATE);\
177 elapsed_ms(&CACHE(rig)->time_freqMainB, HAMLIB_ELAPSED_INVALIDATE);\
178 elapsed_ms(&CACHE(rig)->time_freqSubA, HAMLIB_ELAPSED_INVALIDATE);\
179 elapsed_ms(&CACHE(rig)->time_freqSubB, HAMLIB_ELAPSED_INVALIDATE);\
180 elapsed_ms(&CACHE(rig)->time_vfo, HAMLIB_ELAPSED_INVALIDATE);\
181 elapsed_ms(&CACHE(rig)->time_modeMainA, HAMLIB_ELAPSED_INVALIDATE);\
182 elapsed_ms(&CACHE(rig)->time_modeMainB, HAMLIB_ELAPSED_INVALIDATE);\
183 elapsed_ms(&CACHE(rig)->time_modeMainC, HAMLIB_ELAPSED_INVALIDATE);\
184 elapsed_ms(&CACHE(rig)->time_modeSubA, HAMLIB_ELAPSED_INVALIDATE);\
185 elapsed_ms(&CACHE(rig)->time_modeSubB, HAMLIB_ELAPSED_INVALIDATE);\
186 elapsed_ms(&CACHE(rig)->time_modeSubC, HAMLIB_ELAPSED_INVALIDATE);\
187 elapsed_ms(&CACHE(rig)->time_widthMainA, HAMLIB_ELAPSED_INVALIDATE);\
188 elapsed_ms(&CACHE(rig)->time_widthMainB, HAMLIB_ELAPSED_INVALIDATE);\
189 elapsed_ms(&CACHE(rig)->time_widthMainC, HAMLIB_ELAPSED_INVALIDATE);\
190 elapsed_ms(&CACHE(rig)->time_widthSubA, HAMLIB_ELAPSED_INVALIDATE);\
191 elapsed_ms(&CACHE(rig)->time_widthSubB, HAMLIB_ELAPSED_INVALIDATE);\
192 elapsed_ms(&CACHE(rig)->time_widthSubC, HAMLIB_ELAPSED_INVALIDATE);\
193 elapsed_ms(&CACHE(rig)->time_ptt, HAMLIB_ELAPSED_INVALIDATE);\
194 elapsed_ms(&CACHE(rig)->time_split, HAMLIB_ELAPSED_INVALIDATE);\
195 }
196
197
198typedef enum settings_value_e
199{
200 e_CHAR, e_INT, e_LONG, e_FLOAT, e_DOUBLE
201} settings_value_t;
202
203
204extern HAMLIB_EXPORT(int) rig_settings_save(const char *setting, void *value, settings_value_t valuet);
205extern HAMLIB_EXPORT(int) rig_settings_load(char *setting, void *value, settings_value_t valuet);
206extern HAMLIB_EXPORT(int) rig_settings_load_all(char *settings_file);
207
208extern int check_level_param(RIG *rig, setting_t level, value_t val, gran_t **gran);
209
210extern int queue_deferred_config(deferred_config_header_t *head, hamlib_token_t token, const char *val);
211
212// Takes rig-specific band result and maps it the bandlist int the rig's backend
213extern HAMLIB_EXPORT(hamlib_band_t) rig_get_band(RIG *rig, freq_t freq, int band);
214extern HAMLIB_EXPORT(const char*) rig_get_band_str(RIG *rig, hamlib_band_t band, int which);
215extern HAMLIB_EXPORT(int) rig_get_band_rig(RIG *rig, freq_t freq, const char *band);
216
217extern HAMLIB_EXPORT(int) rig_test_2038(RIG *rig);
218
219__END_DECLS
220
221#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:363
int rig_check_cache_timeout(const struct timeval *tv, int timeout)
Helper for checking cache timeout.
Definition misc.c:1851
int check_level_param(RIG *rig, setting_t level, value_t val, gran_t **gran)
check input to set_level
Definition misc.c:1042
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:229
void rig_force_cache_timeout(struct timeval *tv)
Helper for forcing cache timeout next call.
Definition misc.c:1905
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:145
setting_t rig_idx2setting(int i)
Definition misc.c:801
void dump_hex(const unsigned char ptr[], size_t size)
Do a hex dump of the unsigned char array.
Definition debug.c:82
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:192
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:272
int queue_deferred_config(deferred_config_header_t *head, hamlib_token_t token, const char *val)
Definition misc.c:3138
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:337
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:350
int sprintf_freq(char *str, int str_len, freq_t)
Pretty print a frequency.
Definition misc.c:384
unsigned int vfo_t
VFO definition.
Definition rig.h:544
hamlib_band_t
Band enumeration.
Definition rig.h:808
long hamlib_token_t
configuration token
Definition rig.h:893
split_t
Split mode.
Definition rig.h:473
uint64_t setting_t
Setting bit mask.
Definition rig.h:1234
double freq_t
Frequency type.
Definition rig.h:484
int rig_settings_save(const char *setting, void *value, settings_value_t valuet)
Save setting parameter.
Definition settings.c:1094
struct s_rig RIG
Rig structure definition (see rig for details).
Definition rig.h:290
struct gran gran_t
gran_t type
Definition rig.h:1771
Hamlib rig data structures.
level/parm granularity definition
Definition rig.h:1762
Universal approach for passing values.
Definition rig.h:1058