Skip to content

Commit

Permalink
Tune menus will not always save settings (#410)
Browse files Browse the repository at this point in the history
When the tune menus request that the current configuration settings be
saved there is an assumption that each variable will be represented in
the .conf file.  There is no reason that any variables missing from the
.conf files cannot be added.
  • Loading branch information
Allan-N authored Oct 29, 2024
1 parent 90dadaa commit d5c27f5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
41 changes: 36 additions & 5 deletions channels/chan_simpleusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3197,6 +3197,37 @@ static void _menu_txb(int fd, struct chan_simpleusb_pvt *o, const char *str)
return;
}

/*!
* \brief Update the tune settings to the configuration file.
* \param filename The configuration file being updated (e.g. "simpleusb.conf").
* \param category The category being updated (e.g. "12345").
* \param variable The variable being updated (e.g. "rxboost").
* \param value The value being updated (e.g. "yes").
* \retval 0 If successful.
* \retval -1 If unsuccessful.
*/
static int tune_variable_update(const char *filename, struct ast_category *category,
const char *variable, const char *value)
{
int res;
struct ast_variable *var;

res = ast_variable_update(category, variable, value, NULL, 0);
if (res == 0) {
return 0;
}

/* if we could not find/update the variable, create new */
var = ast_variable_new(variable, value, filename);
if (var == NULL) {
return -1;
}

/* and append */
ast_variable_append(category, var);
return 0;
}

/*!
* \brief Write tune settings to the configuration file. If the device EEPROM is enabled, the settings are saved to EEPROM.
* \param o Channel private.
Expand All @@ -3216,25 +3247,25 @@ static void tune_write(struct chan_simpleusb_pvt *o)
}

#define CONFIG_UPDATE_STR(field) \
if (ast_variable_update(category, #field, o->field, NULL, 0)) { \
if (tune_variable_update(CONFIG, category, #field, o->field)) { \
ast_log(LOG_WARNING, "Failed to update %s\n", #field); \
}

#define CONFIG_UPDATE_INT(field) { \
char _buf[15]; \
snprintf(_buf, sizeof(_buf), "%d", o->field); \
if (ast_variable_update(category, #field, _buf, NULL, 0)) { \
if (tune_variable_update(CONFIG, category, #field, _buf)) { \
ast_log(LOG_WARNING, "Failed to update %s\n", #field); \
} \
}

#define CONFIG_UPDATE_BOOL(field) \
if (ast_variable_update(category, #field, o->field ? "yes" : "no", NULL, 0)) { \
if (tune_variable_update(CONFIG, category, #field, o->field ? "yes" : "no")) { \
ast_log(LOG_WARNING, "Failed to update %s\n", #field); \
}

#define CONFIG_UPDATE_SIGNAL(key, field) \
if (ast_variable_update(category, #key, signal_type[o->field], NULL, 0)) { \
if (tune_variable_update(CONFIG, category, #key, signal_type[o->field])) { \
ast_log(LOG_WARNING, "Failed to update %s\n", #field); \
}

Expand All @@ -3258,7 +3289,7 @@ static void tune_write(struct chan_simpleusb_pvt *o)
CONFIG_UPDATE_INT(rxondelay);
CONFIG_UPDATE_INT(txoffdelay);
if (ast_config_text_file_save2(CONFIG, cfg, "chan_simpleusb", 0)) {
ast_log(LOG_WARNING, "Failed to save config\n");
ast_log(LOG_WARNING, "Failed to save config %s\n", CONFIG);
}
}

Expand Down
43 changes: 37 additions & 6 deletions channels/chan_usbradio.c
Original file line number Diff line number Diff line change
Expand Up @@ -4390,6 +4390,37 @@ static void tune_rxctcss(int fd, struct chan_usbradio_pvt *o, int intflag)
o->pmrChan->b.tuning = 0;
}

/*!
* \brief Update the tune settings to the configuration file.
* \param filename The configuration file being updated (e.g. "usbradio.conf").
* \param category The category being updated (e.g. "12345").
* \param variable The variable being updated (e.g. "rxboost").
* \param value The value being updated (e.g. "yes").
* \retval 0 If successful.
* \retval -1 If unsuccessful.
*/
static int tune_variable_update(const char *filename, struct ast_category *category,
const char *variable, const char *value)
{
int res;
struct ast_variable *var;

res = ast_variable_update(category, variable, value, NULL, 0);
if (res == 0) {
return 0;
}

/* if we could not find/update the variable, create new */
var = ast_variable_new(variable, value, filename);
if (var == NULL) {
return -1;
}

/* and append */
ast_variable_append(category, var);
return 0;
}

/*!
* \brief Write tune settings to the configuration file. If the device EEPROM is enabled, the settings are saved to EEPROM.
* \param o Channel private.
Expand All @@ -4409,33 +4440,33 @@ static void tune_write(struct chan_usbradio_pvt *o)
}

#define CONFIG_UPDATE_STR(field) \
if (ast_variable_update(category, #field, o->field, NULL, 0)) { \
if (tune_variable_update(CONFIG, category, #field, o->field)) { \
ast_log(LOG_WARNING, "Failed to update %s\n", #field); \
}

#define CONFIG_UPDATE_INT(field) { \
char _buf[15]; \
snprintf(_buf, sizeof(_buf), "%d", o->field); \
if (ast_variable_update(category, #field, _buf, NULL, 0)) { \
if (tune_variable_update(CONFIG, category, #field, _buf)) { \
ast_log(LOG_WARNING, "Failed to update %s\n", #field); \
} \
}

#define CONFIG_UPDATE_BOOL(field) \
if (ast_variable_update(category, #field, o->field ? "yes" : "no", NULL, 0)) { \
if (tune_variable_update(CONFIG, category, #field, o->field ? "yes" : "no")) { \
ast_log(LOG_WARNING, "Failed to update %s\n", #field); \
}

#define CONFIG_UPDATE_FLOAT(field) { \
char _buf[15]; \
snprintf(_buf, sizeof(_buf), "%f", o->field); \
if (ast_variable_update(category, #field, _buf, NULL, 0)) { \
if (tune_variable_update(CONFIG, category, #field, _buf)) { \
ast_log(LOG_WARNING, "Failed to update %s\n", #field); \
} \
}

#define CONFIG_UPDATE_SIGNAL(key, field, signal_type) \
if (ast_variable_update(category, #key, signal_type[o->field], NULL, 0)) { \
if (tune_variable_update(CONFIG, category, #key, signal_type[o->field])) { \
ast_log(LOG_WARNING, "Failed to update %s\n", #field); \
}

Expand Down Expand Up @@ -4465,7 +4496,7 @@ static void tune_write(struct chan_usbradio_pvt *o)
CONFIG_UPDATE_SIGNAL(txmixa, txmixa, mixer_type);
CONFIG_UPDATE_SIGNAL(txmixb, txmixb, mixer_type);
if (ast_config_text_file_save2(CONFIG, cfg, "chan_usbradio", 0)) {
ast_log(LOG_WARNING, "Failed to save config\n");
ast_log(LOG_WARNING, "Failed to save config %s\n", CONFIG);
}
}

Expand Down

0 comments on commit d5c27f5

Please sign in to comment.