Skip to content

Commit

Permalink
chan_simpleusb / chan_usbradio: Refactor time functions to res_usbrad…
Browse files Browse the repository at this point in the history
…io (#354)

This refactors chan_simpleusb and chan_usbradio time functions into res_usbradio.
  • Loading branch information
KB4MDD committed Jul 16, 2024
1 parent d7ffe11 commit 82b503b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 91 deletions.
60 changes: 14 additions & 46 deletions channels/chan_simpleusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,38 +396,6 @@ static struct ast_channel_tech simpleusb_tech = {
.setoption = simpleusb_setoption,
};

/*!
* \brief Get system monotonic
* This returns the CLOCK_MONOTONIC time
* \param second Pointer to time_t to receive the time.
*/
static void rpt_time(time_t *second)
{
struct timespec ts;

clock_gettime(CLOCK_MONOTONIC, &ts);

*second = ts.tv_sec;
}

/*!
* \brief Get system monotonic timeval
* This returns the CLOCK_MONOTONIC time as a timeval
* \retval timval structure with the current monotonic time.
*/
static struct timeval rpt_tvnow(void)
{
struct timeval tv;
struct timespec ts;

clock_gettime(CLOCK_MONOTONIC, &ts);

tv.tv_sec = ts.tv_sec;
tv.tv_usec = ts.tv_nsec / 1000;

return tv;
}

/*!
* \brief FIR Low pass filter.
* 2900 Hz passband with 0.5 db ripple, 6300 Hz stopband at 60db.
Expand Down Expand Up @@ -756,12 +724,12 @@ static void *pulserthread(void *arg)
ast_mutex_lock(&pp_lock);
ast_radio_ppwrite(haspp, ppfd, pbase, pport, pp_val);
ast_mutex_unlock(&pp_lock);
then = rpt_tvnow();
then = ast_radio_tvnow();

while (!stoppulser) {
usleep(50000);
ast_mutex_lock(&pp_lock);
now = rpt_tvnow();
now = ast_radio_tvnow();
j = ast_tvdiff_ms(now, then);
then = now;
/* make output inversion mask (for pulseage) */
Expand Down Expand Up @@ -904,7 +872,7 @@ static void *hidthread(void *arg)
* with the usb hid device
*/
while (!o->stophid) {
rpt_time(&o->lasthidtime);
ast_radio_time(&o->lasthidtime);
ast_mutex_lock(&usb_dev_lock);
o->hasusb = 0;
o->usbass = 0;
Expand All @@ -923,7 +891,7 @@ static void *hidthread(void *arg)
* If no device string is specified, attempt to assign the first
* found device.
*/
rpt_time(&o->lasthidtime);
ast_radio_time(&o->lasthidtime);

/* Automatically assign a devstr if one was not specified in the configuration. */
if (ast_strlen_zero(o->devstr)) {
Expand Down Expand Up @@ -1029,7 +997,7 @@ static void *hidthread(void *arg)
}
#endif
o->device_error = 0;
rpt_time(&o->lasthidtime);
ast_radio_time(&o->lasthidtime);
o->usbass = 1;
ast_mutex_unlock(&usb_dev_lock);
/* set the audio mixer values */
Expand Down Expand Up @@ -1115,15 +1083,15 @@ static void *hidthread(void *arg)
rfds[0].fd = o->pttkick[1];
rfds[0].events = POLLIN;

rpt_time(&o->lasthidtime);
ast_radio_time(&o->lasthidtime);
/* Main processing loop for GPIO
* This loop process every 50 milliseconds.
* The timer can be interupted by writing to
* the pttkick pipe.
*/
while ((!o->stophid) && o->hasusb) {

then = rpt_tvnow();
then = ast_radio_tvnow();
/* poll the pttkick pipe - timeout after 50 milliseconds */
res = ast_poll(rfds, 1, 50);
if (res < 0) {
Expand Down Expand Up @@ -1205,7 +1173,7 @@ static void *hidthread(void *arg)
}
lasttxtmp = o->lasttx;
o->lasttx = txreq;
rpt_time(&o->lasthidtime);
ast_radio_time(&o->lasthidtime);
/* Get the GPIO information */
j = buf[o->hid_gpio_loc];
/* If this device is a CM108AH, map the "HOOK" bit (which used to
Expand Down Expand Up @@ -1323,7 +1291,7 @@ static void *hidthread(void *arg)
}
}
}
j = ast_tvdiff_ms(rpt_tvnow(), then);
j = ast_tvdiff_ms(ast_radio_tvnow(), then);
/* make output inversion mask (for pulseage) */
o->hid_gpio_lastmask = o->hid_gpio_pulsemask;
o->hid_gpio_pulsemask = 0;
Expand Down Expand Up @@ -1396,7 +1364,7 @@ static void *hidthread(void *arg)
memcpy(bufsave, buf, sizeof(buf));
ast_radio_hid_set_outputs(usb_handle, buf);
}
rpt_time(&o->lasthidtime);
ast_radio_time(&o->lasthidtime);
ast_mutex_unlock(&o->usblock);
}
o->lasttx = 0;
Expand Down Expand Up @@ -1879,7 +1847,7 @@ static int simpleusb_call(struct ast_channel *c, const char *dest, int timeout)
struct chan_simpleusb_pvt *o = ast_channel_tech_pvt(c);

o->stophid = 0;
rpt_time(&o->lasthidtime);
ast_radio_time(&o->lasthidtime);
ast_pthread_create_background(&o->hidthread, NULL, hidthread, o);
ast_setstate(c, AST_STATE_UP);
return 0;
Expand Down Expand Up @@ -1995,7 +1963,7 @@ static struct ast_frame *simpleusb_read(struct ast_channel *c)

/* check to the if the hid thread is still processing */
if (o->lasthidtime) {
rpt_time(&now);
ast_radio_time(&now);
if ((now - o->lasthidtime) > 3) {
ast_log(LOG_ERROR, "Channel %s: HID process has died or is not responding.\n", o->name);
return NULL;
Expand Down Expand Up @@ -2372,7 +2340,7 @@ static struct ast_frame *simpleusb_read(struct ast_channel *c)
return (f1);
}
if (f1->frametype == AST_FRAME_DTMF_END) {
f1->len = ast_tvdiff_ms(rpt_tvnow(), o->tonetime);
f1->len = ast_tvdiff_ms(ast_radio_tvnow(), o->tonetime);
if (option_verbose) {
ast_log(LOG_NOTICE, "Channel %s: Got DTMF char %c duration %ld ms\n", o->name, f1->subclass.integer, f1->len);
}
Expand All @@ -2382,7 +2350,7 @@ static struct ast_frame *simpleusb_read(struct ast_channel *c)
ast_frfree(f1);
f1 = NULL;
} else {
o->tonetime = rpt_tvnow();
o->tonetime = ast_radio_tvnow();
o->toneflag = 1;
}
}
Expand Down
57 changes: 13 additions & 44 deletions channels/chan_usbradio.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,37 +492,6 @@ static struct ast_channel_tech usbradio_tech = {
.setoption = usbradio_setoption,
};

/*!
* \brief Get system monotonic
* This returns the CLOCK_MONOTONIC time
* \param second Pointer to time_t to receive the time.
*/
static void rpt_time(time_t *second)
{
struct timespec ts;

clock_gettime(CLOCK_MONOTONIC, &ts);

*second = ts.tv_sec;
}

/*!
* \brief Get system monotonic timeval
* This returns the CLOCK_MONOTONIC time as a timeval
* \retval timval structure with the current monotonic time.
*/
static struct timeval rpt_tvnow(void)
{
struct timeval tv;
struct timespec ts;

clock_gettime(CLOCK_MONOTONIC, &ts);

tv.tv_sec = ts.tv_sec;
tv.tv_usec = ts.tv_nsec / 1000;

return tv;
}

/*!
* \brief Configure our private structure based on the
Expand Down Expand Up @@ -711,12 +680,12 @@ static void *pulserthread(void *arg)
ast_mutex_lock(&pp_lock);
ast_radio_ppwrite(haspp, ppfd, pbase, pport, pp_val);
ast_mutex_unlock(&pp_lock);
then = rpt_tvnow();
then = ast_radio_tvnow();

while (!stoppulser) {
usleep(50000);
ast_mutex_lock(&pp_lock);
now = rpt_tvnow();
now = ast_radio_tvnow();
j = ast_tvdiff_ms(now, then);
then = now;
/* make output inversion mask (for pulseage) */
Expand Down Expand Up @@ -868,7 +837,7 @@ static void *hidthread(void *arg)
* with the usb hid device
*/
while (!o->stophid) {
rpt_time(&o->lasthidtime);
ast_radio_time(&o->lasthidtime);
ast_mutex_lock(&usb_dev_lock);
o->hasusb = 0;
o->usbass = 0;
Expand All @@ -887,7 +856,7 @@ static void *hidthread(void *arg)
* If no device string is specified, attempt to assign the first
* found device.
*/
rpt_time(&o->lasthidtime);
ast_radio_time(&o->lasthidtime);

/* Automatically assign a devstr if one was not specified in the configuration. */
if (ast_strlen_zero(o->devstr)) {
Expand Down Expand Up @@ -993,7 +962,7 @@ static void *hidthread(void *arg)
}
#endif
o->device_error = 0;
rpt_time(&o->lasthidtime);
ast_radio_time(&o->lasthidtime);
o->usbass = 1;
ast_mutex_unlock(&usb_dev_lock);
/* set the audio mixer values */
Expand Down Expand Up @@ -1186,15 +1155,15 @@ static void *hidthread(void *arg)
rfds[0].fd = o->pttkick[1];
rfds[0].events = POLLIN;

rpt_time(&o->lasthidtime);
ast_radio_time(&o->lasthidtime);
/* Main processing loop for GPIO
* This loop process every 50 milliseconds.
* The timer can be interupted by writing to
* the pttkick pipe.
*/
while ((!o->stophid) && o->hasusb) {

then = rpt_tvnow();
then = ast_radio_tvnow();
/* poll the pttkick pipe - timeout after 50 milliseconds */
res = ast_poll(rfds, 1, 50);
if (res < 0) {
Expand Down Expand Up @@ -1376,7 +1345,7 @@ static void *hidthread(void *arg)
}
}
}
j = ast_tvdiff_ms(rpt_tvnow(), then);
j = ast_tvdiff_ms(ast_radio_tvnow(), then);
/* make output inversion mask (for pulseage) */
o->hid_gpio_lastmask = o->hid_gpio_pulsemask;
o->hid_gpio_pulsemask = 0;
Expand Down Expand Up @@ -1453,7 +1422,7 @@ static void *hidthread(void *arg)
memcpy(bufsave, buf, sizeof(buf));
ast_radio_hid_set_outputs(usb_handle, buf);
}
rpt_time(&o->lasthidtime);
ast_radio_time(&o->lasthidtime);
ast_mutex_unlock(&o->usblock);
}
lasttxtmp = o->pmrChan->txPttOut = 0;
Expand Down Expand Up @@ -1865,7 +1834,7 @@ static int usbradio_call(struct ast_channel *c, const char *dest, int timeout)
struct chan_usbradio_pvt *o = ast_channel_tech_pvt(c);

o->stophid = 0;
rpt_time(&o->lasthidtime);
ast_radio_time(&o->lasthidtime);
ast_pthread_create_background(&o->hidthread, NULL, hidthread, o);
ast_setstate(c, AST_STATE_UP);
return 0;
Expand Down Expand Up @@ -1969,7 +1938,7 @@ static struct ast_frame *usbradio_read(struct ast_channel *c)

/* check to the if the hid thread is still processing */
if (o->lasthidtime) {
rpt_time(&now);
ast_radio_time(&now);
if ((now - o->lasthidtime) > 3) {
ast_log(LOG_ERROR, "Channel %s: HID process has died or is not responding.\n", o->name);
return NULL;
Expand Down Expand Up @@ -2366,7 +2335,7 @@ static struct ast_frame *usbradio_read(struct ast_channel *c)
return (f1);
}
if (f1->frametype == AST_FRAME_DTMF_END) {
f1->len = ast_tvdiff_ms(rpt_tvnow(), o->tonetime);
f1->len = ast_tvdiff_ms(ast_radio_tvnow(), o->tonetime);
if (option_verbose) {
ast_log(LOG_NOTICE, "Channel %s: Got DTMF char %c duration %ld ms\n", o->name, f1->subclass.integer, f1->len);
}
Expand All @@ -2376,7 +2345,7 @@ static struct ast_frame *usbradio_read(struct ast_channel *c)
ast_frfree(f1);
f1 = NULL;
} else {
o->tonetime = rpt_tvnow();
o->tonetime = ast_radio_tvnow();
o->toneflag = 1;
}
}
Expand Down
17 changes: 16 additions & 1 deletion include/asterisk/res_usbradio.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,19 @@ int ast_radio_poll_input(int fd, int ms);
* \retval 0 Timer expired.
* \retval 1 Activity occurred on the fd.
*/
int ast_radio_wait_or_poll(int fd, int ms, int flag);
int ast_radio_wait_or_poll(int fd, int ms, int flag);

/*!
* \brief Get system monotonic
* This returns the CLOCK_MONOTONIC time
* \param second Pointer to time_t to receive the time.
*/
void ast_radio_time(time_t *second);

/*!
* \brief Get system monotonic timeval
* This returns the CLOCK_MONOTONIC time as a timeval
* \retval timval structure with the current monotonic time.
*/
struct timeval ast_radio_tvnow(void);

21 changes: 21 additions & 0 deletions res/res_usbradio.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,27 @@ int ast_radio_wait_or_poll(int fd, int ms, int flag)
return 0;
}

void ast_radio_time(time_t *second)
{
struct timespec ts;

clock_gettime(CLOCK_MONOTONIC, &ts);

*second = ts.tv_sec;
}

struct timeval ast_radio_tvnow(void)
{
struct timeval tv;
struct timespec ts;

clock_gettime(CLOCK_MONOTONIC, &ts);

tv.tv_sec = ts.tv_sec;
tv.tv_usec = ts.tv_nsec / 1000;

return tv;
}

static int load_module(void)
{
Expand Down

0 comments on commit 82b503b

Please sign in to comment.