2003-12-04 23:15:02 +00:00
|
|
|
/*
|
2005-01-24 23:04:35 +00:00
|
|
|
* memcsv.c - (C) Stephane Fillod 2003-2005
|
2003-12-04 23:15:02 +00:00
|
|
|
*
|
|
|
|
* This program exercises the backup and restore of a radio
|
|
|
|
* using Hamlib. CSV primitives
|
|
|
|
*
|
2005-04-20 14:47:04 +00:00
|
|
|
* $Id: memcsv.c,v 1.8 2005-04-20 14:47:04 fillods Exp $
|
2003-12-04 23:15:02 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include <hamlib/rig.h>
|
|
|
|
#include "misc.h"
|
|
|
|
#include "sprintflst.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* external prototype
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern int all;
|
|
|
|
|
2005-04-20 14:47:04 +00:00
|
|
|
char csv_sep = ','; /* CSV separator */
|
|
|
|
|
2003-12-04 23:15:02 +00:00
|
|
|
/*
|
|
|
|
* Prototypes
|
|
|
|
*/
|
2005-04-20 14:47:04 +00:00
|
|
|
static int dump_csv_chan(RIG *rig, channel_t **chan, int channel_num, const chan_t *chan_list, rig_ptr_t arg);
|
2003-12-04 23:15:02 +00:00
|
|
|
static void dump_csv_name(const channel_cap_t *mem_caps, FILE *f);
|
|
|
|
|
|
|
|
int csv_save (RIG *rig, const char *outfilename);
|
|
|
|
int csv_load (RIG *rig, const char *infilename);
|
|
|
|
int csv_parm_save (RIG *rig, const char *outfilename);
|
|
|
|
int csv_parm_load (RIG *rig, const char *infilename);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int csv_save (RIG *rig, const char *outfilename)
|
|
|
|
{
|
2005-04-20 14:47:04 +00:00
|
|
|
int status;
|
2003-12-04 23:15:02 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
f = fopen(outfilename, "w");
|
|
|
|
if (!f)
|
|
|
|
return -1;
|
|
|
|
|
2005-04-20 14:47:04 +00:00
|
|
|
if (rig->caps->clone_combo_get)
|
|
|
|
printf("About to save data, enter cloning mode: %s\n",
|
|
|
|
rig->caps->clone_combo_get);
|
2003-12-04 23:15:02 +00:00
|
|
|
|
2005-04-20 14:47:04 +00:00
|
|
|
status = rig_get_chan_all_cb (rig, dump_csv_chan, f);
|
2003-12-04 23:15:02 +00:00
|
|
|
|
|
|
|
fclose(f);
|
2005-04-20 14:47:04 +00:00
|
|
|
return status;
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int csv_load (RIG *rig, const char *infilename)
|
|
|
|
{
|
|
|
|
return -RIG_ENIMPL;
|
|
|
|
/* for every channel, fscanf, parse, set_chan */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_parm_name(RIG *rig, const struct confparams *cfp, rig_ptr_t ptr)
|
|
|
|
{
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf((FILE*)ptr, "%s%c", cfp->name, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
|
|
|
|
return 1; /* process them all */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_parm_val(RIG *rig, const struct confparams *cfp, rig_ptr_t ptr)
|
|
|
|
{
|
|
|
|
value_t val;
|
|
|
|
FILE *f=(FILE*)ptr;
|
|
|
|
rig_get_ext_parm(rig, cfp->token, &val);
|
|
|
|
|
|
|
|
switch (cfp->type) {
|
2005-04-20 14:47:04 +00:00
|
|
|
case RIG_CONF_CHECKBUTTON:
|
|
|
|
case RIG_CONF_COMBO:
|
|
|
|
fprintf(f,"%d%c", val.i, csv_sep);
|
|
|
|
break;
|
|
|
|
case RIG_CONF_NUMERIC:
|
|
|
|
fprintf(f,"%f%c", val.f, csv_sep);
|
|
|
|
break;
|
|
|
|
case RIG_CONF_STRING:
|
|
|
|
fprintf(f,"%s%c", val.s, csv_sep);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(f,"unknown%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1; /* process them all */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int csv_parm_save (RIG *rig, const char *outfilename)
|
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
FILE *f;
|
|
|
|
setting_t parm, get_parm = all ? 0x7fffffff : rig->state.has_get_parm;
|
|
|
|
|
|
|
|
f = fopen(outfilename, "w");
|
|
|
|
if (!f)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i < RIG_SETTING_MAX; i++) {
|
2004-05-17 21:09:45 +00:00
|
|
|
const char *ms = rig_strparm(get_parm & rig_idx2setting(i));
|
2003-12-04 23:15:02 +00:00
|
|
|
if (!ms || !ms[0])
|
|
|
|
continue;
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "%s%c", ms, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rig_ext_parm_foreach(rig, print_parm_name, f);
|
|
|
|
fprintf(f, "\n");
|
|
|
|
|
|
|
|
for (i = 0; i < RIG_SETTING_MAX; i++) {
|
2003-12-08 08:46:11 +00:00
|
|
|
const char *ms;
|
2003-12-04 23:15:02 +00:00
|
|
|
value_t val;
|
|
|
|
|
2003-12-08 08:46:11 +00:00
|
|
|
parm = get_parm & rig_idx2setting(i);
|
2004-05-17 21:09:45 +00:00
|
|
|
ms = rig_strparm(parm);
|
2003-12-08 08:46:11 +00:00
|
|
|
|
2003-12-04 23:15:02 +00:00
|
|
|
if (!ms || !ms[0])
|
|
|
|
continue;
|
|
|
|
ret = rig_get_parm(rig, parm, &val);
|
|
|
|
if (RIG_PARM_IS_FLOAT(parm))
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "%f%c", val.f, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
else
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "%d%c", val.i, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
rig_ext_parm_foreach(rig, print_parm_val, f);
|
|
|
|
fprintf(f, "\n");
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int csv_parm_load (RIG *rig, const char *infilename)
|
|
|
|
{
|
|
|
|
return -RIG_ENIMPL;
|
|
|
|
/* for every parm/ext_parm, fscanf, parse, set_parm's */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* *********************** */
|
|
|
|
|
|
|
|
|
|
|
|
void dump_csv_name(const channel_cap_t *mem_caps, FILE *f)
|
|
|
|
{
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "num%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
|
|
|
|
if (mem_caps->bank_num) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "bank_num%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mem_caps->channel_desc) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "channel_desc%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->vfo) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "vfo%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->ant) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "ant%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->freq) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "freq%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->mode) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "mode%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->width) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "width%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->tx_freq) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "tx_freq%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->tx_mode) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "tx_mode%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->tx_width) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "tx_width%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->split) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "split%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->tx_vfo) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "tx_vfo%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->rptr_shift) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "rptr_shift%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->rptr_offs) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "rptr_offs%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->tuning_step) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "tuning_step%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->rit) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "rit%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->xit) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "xit%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->funcs) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "funcs%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->ctcss_tone) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "ctcss_tone%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->ctcss_sql) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "ctcss_sql%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->dcs_code) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "dcs_code%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->dcs_sql) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "dcs_sql%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->scan_group) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "scan_group%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->flags) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "flags%c", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
fprintf(f, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-20 14:47:04 +00:00
|
|
|
int dump_csv_chan(RIG *rig, channel_t **chan_pp, int channel_num, const chan_t *chan_list, rig_ptr_t arg)
|
2003-12-04 23:15:02 +00:00
|
|
|
{
|
2005-04-20 14:47:04 +00:00
|
|
|
FILE *f = arg;
|
|
|
|
static channel_t chan;
|
|
|
|
static int first_time = 1;
|
|
|
|
const channel_cap_t *mem_caps = &chan_list->mem_caps;
|
|
|
|
|
|
|
|
if (first_time) {
|
|
|
|
dump_csv_name(mem_caps, f);
|
|
|
|
first_time = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*chan_pp == NULL) {
|
|
|
|
/*
|
|
|
|
* Hamlib frontend demand application an allocated
|
|
|
|
* channel_t pointer for next round.
|
|
|
|
*/
|
|
|
|
*chan_pp = &chan;
|
|
|
|
|
|
|
|
return RIG_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f,"%d%c",chan.channel_num, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
|
|
|
|
if (mem_caps->bank_num) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%d%c",chan.bank_num, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mem_caps->channel_desc) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "%s%c", chan.channel_desc, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->vfo) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%s%c",rig_strvfo(chan.vfo), csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->ant) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%d%c",chan.ant, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->freq) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%.0"PRIfreq"%c",chan.freq, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->mode) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "%s%c", rig_strrmode(chan.mode), csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->width) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%d%c",(int)chan.width, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->tx_freq) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%.0"PRIfreq"%c",chan.tx_freq, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->tx_mode) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "%s%c", rig_strrmode(chan.tx_mode), csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->tx_width) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%d%c",(int)chan.tx_width, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->split) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "%s%c", chan.split==RIG_SPLIT_ON?"on":"off", csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->tx_vfo) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%s%c",rig_strvfo(chan.tx_vfo), csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->rptr_shift) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f, "%s%c", rig_strptrshift(chan.rptr_shift), csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->rptr_offs) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%d%c",(int)chan.rptr_offs, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->tuning_step) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%d%c",(int)chan.tuning_step, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->rit) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%d%c",(int)chan.rit, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->xit) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%d%c",(int)chan.xit, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->funcs) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%lx%c",chan.funcs, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->ctcss_tone) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%d%c",chan.ctcss_tone, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->ctcss_sql) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%d%c",chan.ctcss_sql, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->dcs_code) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%d%c",chan.dcs_code, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->dcs_sql) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%d%c",chan.dcs_sql, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->scan_group) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%d%c",chan.scan_group, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
if (mem_caps->flags) {
|
2005-04-20 14:47:04 +00:00
|
|
|
fprintf(f,"%x%c",chan.flags, csv_sep);
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
fprintf(f,"\n");
|
2005-04-20 14:47:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* keep the same *chan_pp for next round, thanks
|
|
|
|
* to chan being static
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
return RIG_OK;
|
2003-12-04 23:15:02 +00:00
|
|
|
}
|
|
|
|
|