Skip to content

Commit

Permalink
chan_simpleusb, chan_usbradio: Use tune sections from main config file.
Browse files Browse the repository at this point in the history
Instead of having separate tune files for each node, look for
a section for the node in the main config file.

Closes #276
  • Loading branch information
InterLinked1 committed Feb 10, 2024
1 parent b9cd7d9 commit d41854a
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 170 deletions.
177 changes: 102 additions & 75 deletions channels/chan_simpleusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ static struct ast_jb_conf global_jbconf;
#define QUEUE_SIZE 5 /* 100 milliseconds of sound card output buffer */

#define CONFIG "simpleusb.conf" /* default config file */
#define CONFIG_TUNE "simpleusb_tune_%s.conf" /* tune config file */

/* file handles for writing debug audio packets */
static FILE *frxcapraw = NULL;
Expand Down Expand Up @@ -753,6 +752,59 @@ static void *pulserthread(void *arg)
pthread_exit(0);
}

/*!
* \brief Load settings for a specific node
* \param o
* \param cfg If provided, will use the provided config. If NULL, cfg will be opened automatically.
* \param reload 0 for first load, 1 for reload
*/
static int load_tune_config(struct chan_simpleusb_pvt *o, const struct ast_config *cfg, int reload)
{
struct ast_variable *v;
struct ast_config *cfg2;
int opened = 0;
int configured = 0;
char devstr[sizeof(o->devstr)];

o->rxmixerset = 500;
o->txmixaset = 500;
o->txmixbset = 500;

if (!reload) {
o->devstr[0] = 0;
}

if (!cfg) {
struct ast_flags zeroflag = { 0 };
cfg2 = ast_config_load(CONFIG, zeroflag);
if (!cfg2) {
ast_log(LOG_WARNING, "Can't %sload settings for %s, using default parameters\n", reload ? "re": "", o->name);
return -1;
}
opened = 1;
cfg = cfg2;
}

for (v = ast_variable_browse(cfg, o->name); v; v = v->next) {
configured = 1;
CV_START(v->name, v->value);
CV_UINT("rxmixerset", o->rxmixerset);
CV_UINT("txmixaset", o->txmixaset);
CV_UINT("txmixbset", o->txmixbset);
CV_STR("devstr", (reload ? devstr : o->devstr)); /* Ignore on reload */
CV_END;
}
ast_debug(1, "Processed config for %s\n", reload ? devstr : o->devstr);
if (opened) {
ast_config_destroy(cfg2);
}
if (!configured) {
ast_log(LOG_WARNING, "Can't %sload settings for %s (no section available), using default parameters\n", reload ? "re": "", o->name);
return -1;
}
return 0;
}

/*!
* \brief USB sound device GPIO processing thread
* This thread is responsible for finding and associating the node with the
Expand Down Expand Up @@ -785,16 +837,13 @@ static void *pulserthread(void *arg)
static void *hidthread(void *arg)
{
unsigned char buf[4], bufsave[4], keyed, ctcssed, txreq;
char fname[200], *s, lasttxtmp;
char *s, lasttxtmp;
register int i, j, k;
int res;
struct usb_device *usb_dev;
struct usb_dev_handle *usb_handle;
struct chan_simpleusb_pvt *o = arg, *ao;
struct timeval then;
struct ast_config *cfg_tune;
struct ast_variable *v;
struct ast_flags zeroflag = { 0 };
struct pollfd rfds[1];

usb_dev = NULL;
Expand Down Expand Up @@ -866,7 +915,7 @@ static void *hidthread(void *arg)
continue;
}
ast_log(LOG_NOTICE, "Channel %s: Assigned USB device %s to simpleusb channel\n", o->name, s);
strcpy(o->devstr, s);
ast_copy_string(o->devstr, s, sizeof(o->devstr));
}
/* Double check to see if the device string is assigned to another usb channel */
for (ao = simpleusb_default.next; ao && ao->name; ao = ao->next) {
Expand Down Expand Up @@ -966,31 +1015,11 @@ static void *hidthread(void *arg)
}
ast_debug(5, "Channel %s: Starting normally.\n", o->name);
ast_debug(5, "Channel %s: Attached to usb device %s.\n", o->name, o->devstr);
mixer_write(o);

/* reload the settings from the tune file */
snprintf(fname, sizeof(fname) - 1, CONFIG_TUNE, o->name);
cfg_tune = ast_config_load(fname, zeroflag);
o->rxmixerset = 500;
o->txmixaset = 500;
o->txmixbset = 500;
if (cfg_tune) {
for (v = ast_variable_browse(cfg_tune, o->name); v; v = v->next) {

CV_START((char *) v->name, (char *) v->value);
CV_UINT("rxmixerset", o->rxmixerset);
CV_UINT("txmixaset", o->txmixaset);
CV_UINT("txmixbset", o->txmixbset);
CV_END;
}
ast_config_destroy(cfg_tune);
ast_log(LOG_NOTICE, "Channel %s: Loaded parameters from %s .\n", o->name, fname);
} else {
ast_log(LOG_WARNING, "Channel %s: File %s not found, using default parameters.\n", o->name, fname);
}

mixer_write(o);

load_tune_config(o, NULL, 1);
mixer_write(o);

ast_mutex_lock(&o->eepromlock);
if (o->wanteeprom) {
o->eepromctl = 1;
Expand Down Expand Up @@ -2717,7 +2746,7 @@ static int usb_device_swap(int fd, const char *other)
d = p->devicenum;
strcpy(p->devstr, o->devstr);
p->devicenum = o->devicenum;
strcpy(o->devstr, tmp);
ast_copy_string(o->devstr, tmp, sizeof(o->devstr));
o->devicenum = d;
o->hasusb = 0;
o->usbass = 0;
Expand Down Expand Up @@ -3041,30 +3070,52 @@ static void _menu_txb(int fd, struct chan_simpleusb_pvt *o, const char *str)
}

/*!
* \brief Write tune settings to the configuration file.
* File file name is "simpleusb_tune_xxx.conf", where
* xxx is the node number.
* If the device EEPROM is enabled, the settings are
* saved to EEPROM.
* \param o Private struct.
* \brief Write tune settings to the configuration file. If the device EEPROM is enabled, the settings are saved to EEPROM.
* \param o Channel private.
*/
static void tune_write(struct chan_simpleusb_pvt *o)
{
FILE *fp;
char fname[200];
struct ast_config *cfg;
struct ast_category *category = NULL;
struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS | CONFIG_FLAG_NOCACHE };

snprintf(fname, sizeof(fname) - 1, "/etc/asterisk/simpleusb_tune_%s.conf", o->name);
fp = fopen(fname, "w");
if (!(cfg = ast_config_load2(CONFIG, "chan_simpleusb", config_flags))) {
ast_log(LOG_ERROR, "Config file not found: %s\n", CONFIG);
return;
} else if (cfg == CONFIG_STATUS_FILEINVALID) {
ast_log(LOG_ERROR, "Config file has invalid format: %s\n", CONFIG);
return;
}

#define CONFIG_UPDATE_STR(field) \
if (ast_variable_update(category, #field, o->field, NULL, 0)) { \
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)) { \
ast_log(LOG_WARNING, "Failed to update %s\n", #field); \
} \
}

fprintf(fp, "[%s]\n", o->name);
category = ast_category_get(cfg, o->name, NULL);
if (!category) {
ast_log(LOG_ERROR, "No category '%s' exists?\n", o->name);
} else {
CONFIG_UPDATE_STR(devstr);
CONFIG_UPDATE_INT(rxmixerset);
CONFIG_UPDATE_INT(txmixaset);
CONFIG_UPDATE_INT(txmixbset);
if (ast_config_text_file_save2(CONFIG, cfg, "chan_simpleusb", 0)) {
ast_log(LOG_WARNING, "Failed to save config\n");
}
}

fprintf(fp, "; name=%s\n", o->name);
fprintf(fp, "; devicenum=%i\n", o->devicenum);
fprintf(fp, "devstr=%s\n", o->devstr);
fprintf(fp, "rxmixerset=%i\n", o->rxmixerset);
fprintf(fp, "txmixaset=%i\n", o->txmixaset);
fprintf(fp, "txmixbset=%i\n", o->txmixbset);
fclose(fp);
ast_config_destroy(cfg);
#undef CONFIG_UPDATE_STR
#undef CONFIG_UPDATE_INT

if (o->wanteeprom) {
ast_mutex_lock(&o->eepromlock);
Expand Down Expand Up @@ -3303,11 +3354,9 @@ static struct chan_simpleusb_pvt *store_config(const struct ast_config *cfg, con
{
const struct ast_variable *v;
struct chan_simpleusb_pvt *o;
struct ast_config *cfg_tune;
char fname[200], buf[100];
char buf[100];
int i;
struct ast_flags zeroflag = { 0 };


if (ctg == NULL) {
o = &simpleusb_default;
ctg = "general";
Expand Down Expand Up @@ -3398,29 +3447,7 @@ static struct chan_simpleusb_pvt *store_config(const struct ast_config *cfg, con
hasout = 1;
}

snprintf(fname, sizeof(fname) - 1, CONFIG_TUNE, o->name);
cfg_tune = ast_config_load(fname, zeroflag);

o->rxmixerset = 500;
o->txmixaset = 500;
o->txmixbset = 500;
o->devstr[0] = 0;

/* load the tune file settings */
if (cfg_tune) {
for (v = ast_variable_browse(cfg_tune, o->name); v; v = v->next) {

CV_START((char *) v->name, (char *) v->value);
CV_UINT("rxmixerset", o->rxmixerset);
CV_UINT("txmixaset", o->txmixaset);
CV_UINT("txmixbset", o->txmixbset);
CV_STR("devstr", o->devstr);
CV_END;
}
ast_config_destroy(cfg_tune);
} else {
ast_log(LOG_WARNING, "Channel %s: File %s not found, using default parameters.\n", o->name, fname);
}
load_tune_config(o, cfg, 0);

/* if we are using the EEPROM, request hidthread load the EEPROM */
if (o->wanteeprom) {
Expand Down
Loading

0 comments on commit d41854a

Please sign in to comment.