Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support URIx eeprom Serial Numbers #394

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion channels/chan_simpleusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ struct chan_simpleusb_pvt {
int boost; /* input boost, scaled by BOOST_SCALE */
char devicenum;
char devstr[128];
char serial[14];
int spkrmax;
int micmax;
int micplaymax;
Expand Down Expand Up @@ -347,6 +348,7 @@ static struct chan_simpleusb_pvt simpleusb_default = {
.rxondelay = 0,
.txoffdelay = 0,
.pager = PAGER_NONE,
.serial = "",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this since the variable is "char serial[14]". Also, with the default struct being zero'd out then is this needed (kinda the same way that .devstr is not being initialized)?

.clipledgpio = 0,
.rxaudiostats.index = 0
};
Expand Down Expand Up @@ -860,7 +862,7 @@ static int load_tune_config(struct chan_simpleusb_pvt *o, const struct ast_confi
static void *hidthread(void *arg)
{
unsigned char buf[4], bufsave[4], keyed, ctcssed, txreq;
char *s, lasttxtmp;
char *s, lasttxtmp, serial[14];
register int i, j, k;
int res;
struct usb_device *usb_dev;
Expand Down Expand Up @@ -906,6 +908,35 @@ static void *hidthread(void *arg)
* found device.
*/
ast_radio_time(&o->lasthidtime);
/* If configuration has a serial number defined, find the device */
if (strlen(o->serial) > 0)
{
ast_log(LOG_NOTICE, "Checking for USB device with serial %s\n", o->serial);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ast_log() ... and the others below ... really feel like debug messages. Maybe switch them to ast_debug() ?

int index = 0;
char *index_devstr = NULL;
for(;;)
{
index_devstr = ast_radio_usb_get_devstr(index);
if (ast_strlen_zero(index_devstr)) {
/* No more devices, so break out of the loop */
ast_log(LOG_NOTICE, "USB Device with serial %s not found.\n", o->serial);
break;
}
/* Go through the list of usb devices, and get the serial numbers */
if (ast_radio_get_usb_serial(index_devstr, serial) == 0) continue;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The app_rpt project follows the asterisk coding standards where single/standalone statements are not allowed. This should be :

if (ast_radio_get_usb_serial(index_devstr, serial) == 0) {
    continue;
}

ast_log(LOG_NOTICE, "Device Serial %s vs %s\n", o->serial, serial);
if (strcmp(o->serial, serial) == 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull the brace ('{') up to this line.

{
/* We found a device with the matching serial number
* Set the devstr to the matching device
*/
ast_log(LOG_NOTICE, "Found device serial %s at %s for %s\n",o->serial, index_devstr, o->name);
ast_copy_string(o->devstr, index_devstr, sizeof(o->devstr));
break;
}
index++;
}
}

/* Automatically assign a devstr if one was not specified in the configuration. */
if (ast_strlen_zero(o->devstr)) {
Expand Down Expand Up @@ -936,6 +967,11 @@ static void *hidthread(void *arg)
/* We found an unused device assign it to our node */
ast_copy_string(o->devstr, index_devstr, sizeof(o->devstr));
ast_log(LOG_NOTICE, "Channel %s: Automatically assigned USB device %s to SimpleUSB channel\n", o->name, o->devstr);
/* Check if the device has a serial number, and add it to the config file */
if (ast_radio_get_usb_serial(o->devstr, serial) > 0) {
ast_copy_string(o->serial, serial, sizeof(o->serial));
ast_log(LOG_NOTICE, "Channel %s: Automatically assigned USB device has serial %s\n", o->name, o->serial);
}
break;
}
if (ast_strlen_zero(o->devstr)) {
Expand Down Expand Up @@ -3087,6 +3123,7 @@ static void _menu_print(int fd, struct chan_simpleusb_pvt *o)
ast_cli(fd, "Active radio interface is [%s]\n", simpleusb_active);
ast_mutex_lock(&usb_dev_lock);
ast_cli(fd, "Device String is %s\n", o->devstr);
ast_cli(fd, "Device Serial is %s\n", o->serial);
Copy link
Collaborator

@Allan-N Allan-N Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have mixed thoughts about reporting the device serial if there is no serial number present.

ast_mutex_unlock(&usb_dev_lock);
ast_cli(fd, "Card is %i\n", ast_radio_usb_get_usbdev(o->devstr));
ast_cli(fd, "Rx Level currently set to %d\n", o->rxmixerset);
Expand Down Expand Up @@ -3245,6 +3282,7 @@ static void tune_write(struct chan_simpleusb_pvt *o)
ast_log(LOG_ERROR, "No category '%s' exists?\n", o->name);
} else {
CONFIG_UPDATE_STR(devstr);
CONFIG_UPDATE_STR(serial);
CONFIG_UPDATE_INT(rxmixerset);
CONFIG_UPDATE_INT(txmixaset);
CONFIG_UPDATE_INT(txmixbset);
Expand Down Expand Up @@ -3698,6 +3736,7 @@ static struct chan_simpleusb_pvt *store_config(const struct ast_config *cfg, con
CV_BOOL("rxboost", o->rxboost);
CV_UINT("hdwtype", o->hdwtype);
CV_UINT("eeprom", o->wanteeprom);
CV_STR("serial", o->serial);
CV_UINT("rxondelay", o->rxondelay);
CV_UINT("txoffdelay", o->txoffdelay);
CV_F("pager", store_pager(o, (char *) v->value));
Expand Down
41 changes: 40 additions & 1 deletion channels/chan_usbradio.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ struct chan_usbradio_pvt {
int boost; /* input boost, scaled by BOOST_SCALE */
char devicenum;
char devstr[128];
char serial[14];
int spkrmax;
int micmax;
int micplaymax;
Expand Down Expand Up @@ -428,6 +429,7 @@ static struct chan_usbradio_pvt usbradio_default = {
.usedtmf = 1,
.rxondelay = 0,
.txoffdelay = 0,
.serial = "",
.area = 0,
.rptnum = 0,
.clipledgpio = 0,
Expand Down Expand Up @@ -825,7 +827,7 @@ static int load_tune_config(struct chan_usbradio_pvt *o, const struct ast_config
static void *hidthread(void *arg)
{
unsigned char buf[4], bufsave[4], keyed, ctcssed;
char *s, lasttxtmp;
char *s, lasttxtmp, serial[14];
register int i, j, k;
int res;
struct usb_device *usb_dev;
Expand Down Expand Up @@ -872,6 +874,35 @@ static void *hidthread(void *arg)
*/
ast_radio_time(&o->lasthidtime);

/* If configuration has a serial number defined, find the device */
if (strlen(o->serial) > 0)
{
ast_log(LOG_NOTICE, "Checking for USB device with serial %s\n", o->serial);
int index = 0;
char *index_devstr = NULL;
for(;;)
{
index_devstr = ast_radio_usb_get_devstr(index);
if (ast_strlen_zero(index_devstr)) {
/* No more devices, so break out of the loop */
ast_log(LOG_NOTICE, "USB Device with serial %s not found.\n", o->serial);
break;
}
/* Go through the list of usb devices, and get the serial numbers */
if (ast_radio_get_usb_serial(index_devstr, serial) == 0) continue;
ast_log(LOG_NOTICE, "Device Serial %s vs %s\n", o->serial, serial);
if (strcmp(o->serial, serial) == 0)
{
/* We found a device with the matching serial number
* Set the devstr to the matching device
*/
ast_log(LOG_NOTICE, "Found device serial %s at %s for %s\n",o->serial, index_devstr, o->name);
ast_copy_string(o->devstr, index_devstr, sizeof(o->devstr));
break;
}
index++;
}
}
/* Automatically assign a devstr if one was not specified in the configuration. */
if (ast_strlen_zero(o->devstr)) {
int index = 0;
Expand Down Expand Up @@ -901,6 +932,11 @@ static void *hidthread(void *arg)
/* We found an unused device assign it to our node */
ast_copy_string(o->devstr, index_devstr, sizeof(o->devstr));
ast_log(LOG_NOTICE, "Channel %s: Automatically assigned USB device %s to USBRadio channel\n", o->name, o->devstr);
/* Check if the device has a serial number, and add it to the config file */
if (ast_radio_get_usb_serial(o->devstr, serial) > 0) {
ast_copy_string(o->serial, serial, sizeof(o->serial));
ast_log(LOG_NOTICE, "Channel %s: Automatically assigned USB device has serial %s\n", o->name, o->serial);
}
break;
}
if (ast_strlen_zero(o->devstr)) {
Expand Down Expand Up @@ -3670,6 +3706,7 @@ static void _menu_print(int fd, struct chan_usbradio_pvt *o)
ast_cli(fd, "Active radio interface is [%s]\n", usbradio_active);
ast_mutex_lock(&usb_dev_lock);
ast_cli(fd, "Device String is %s\n", o->devstr);
ast_cli(fd, "Device Serial is %s\n", o->serial);
ast_mutex_unlock(&usb_dev_lock);
ast_cli(fd, "Card is %i\n", ast_radio_usb_get_usbdev(o->devstr));
ast_cli(fd, "Output A is currently set to ");
Expand Down Expand Up @@ -4445,6 +4482,7 @@ static void tune_write(struct chan_usbradio_pvt *o)
ast_log(LOG_ERROR, "No category '%s' exists?\n", o->name);
} else {
CONFIG_UPDATE_STR(devstr);
CONFIG_UPDATE_STR(serial);
CONFIG_UPDATE_INT(rxmixerset);
CONFIG_UPDATE_INT(txmixaset);
CONFIG_UPDATE_INT(txmixbset);
Expand Down Expand Up @@ -4932,6 +4970,7 @@ static struct chan_usbradio_pvt *store_config(const struct ast_config *cfg, cons
CV_UINT("txlpf", o->txlpf);
CV_UINT("txhpf", o->txhpf);
CV_UINT("sendvoter", o->sendvoter);
CV_STR("serial", o->serial);
CV_UINT("clipledgpio", o->clipledgpio);
CV_END;

Expand Down
1 change: 1 addition & 0 deletions configs/rpt/simpleusb.conf
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ clipledgpio = 1 ; Enable ADC Clip Detect feature to use a GP

;;;;; ASL3 Tune settings ;;;;;
devstr=
serial=
rxmixerset=500
txmixaset=500
txmixbset=500
1 change: 1 addition & 0 deletions configs/rpt/usbradio.conf
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ clipledgpio = 0 ; Enable ADC Clip Detect feature to use a GPIO outpu

;;;;; ASL3 Tune settings ;;;;;
devstr=
serial=
rxmixerset=500
txmixaset=500
txmixbset=500
Expand Down
1 change: 1 addition & 0 deletions configs/samples/simpleusb.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@

;;;;; Tune settings ;;;;;
;devstr=
;serial=
;rxmixerset=500
;txmixaset=500
;txmixbset=500
Expand Down
1 change: 1 addition & 0 deletions configs/samples/usbradio.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@

;;;;; Tune settings ;;;;;
;devstr=
;serial=
;rxmixerset=500
;txmixaset=500
;txmixbset=500
Expand Down
11 changes: 11 additions & 0 deletions include/asterisk/res_usbradio.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,16 @@ void ast_radio_time(time_t *second);
*/
struct timeval ast_radio_tvnow(void);

/*!
* \brief Get serial number from device if available
* This function will attempt to get the serial number from a media device
*
* \param devstr The USB device string
* \param serial Buffer to return the serial number
*
* \retval Length of found serial number
*/
int ast_radio_get_usb_serial(char *devstr, char *serial);
/*!
* \brief Detect ADC clipping, collect Rx audio statistics.
*
Expand Down Expand Up @@ -509,3 +519,4 @@ int ast_radio_check_rx_audio(short *sbuf, struct rxaudiostatistics *o, short len
* \return None
*/
void ast_radio_print_rx_audio_stats(int fd, struct rxaudiostatistics *o);

27 changes: 27 additions & 0 deletions res/res_usbradio.c
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,33 @@ void ast_radio_time(time_t *second)
*second = ts.tv_sec;
}

/*!
* \brief Get serial number from device if available
* This function will attempt to get the serial number from a media device
*
* \param devstr The USB device string
* \param serial Buffer to return the serial number
*
* \retval Length of found serial number
*/

int ast_radio_get_usb_serial(char *devstr, char *serial)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function really needs to have the size of the "char *serial" buffer passed in as an argument.

{
struct usb_device *usb_dev;
struct usb_dev_handle *usb_handle;
int length;

usb_dev = ast_radio_hid_device_init(devstr);
usb_handle = usb_open(usb_dev);
length = usb_get_string_simple(usb_handle, usb_dev->descriptor.iSerialNumber, serial, sizeof(serial));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sizeof(serial) is NOT what you want here. You want the caller to pass in the size of the provided buffer and pass that along to usb_get_string_simple()

usb_close(usb_handle);
usb_handle = NULL;
usb_dev = NULL;

return length;
}


struct timeval ast_radio_tvnow(void)
{
struct timeval tv;
Expand Down