1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
|
/**
* @file menu.c Interactive menu
*
* Copyright (C) 2010 Alfred E. Heggestad
*/
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <re.h>
#include <baresip.h>
#include "menu.h"
/**
* @defgroup menu menu
*
* Interactive menu
*
* This module must be loaded if you want to use the interactive menu
* to control the Baresip application.
*/
static struct menu menu;
enum {
MIN_RINGTIME = 1000,
};
static int menu_set_incall(bool incall)
{
int err = 0;
/* Dynamic menus */
if (incall) {
dial_menu_unregister();
err = dynamic_menu_register();
}
else {
dynamic_menu_unregister();
err = dial_menu_register();
}
if (err) {
warning("menu: set_incall: cmd_register failed (%m)\n", err);
}
return err;
}
static void tmrstat_handler(void *arg)
{
struct call *call;
(void)arg;
/* the UI will only show the current active call */
call = menu_callcur();
if (!call)
return;
tmr_start(&menu.tmr_stat, 100, tmrstat_handler, 0);
if (ui_isediting(baresip_uis()))
return;
if (STATMODE_OFF != menu.statmode) {
(void)re_fprintf(stderr, "%H\r", call_status, call);
}
}
void menu_update_callstatus(bool incall)
{
/* if there are any active calls, enable the call status view */
if (incall)
tmr_start(&menu.tmr_stat, 100, tmrstat_handler, 0);
else
tmr_cancel(&menu.tmr_stat);
}
static void redial_reset(void)
{
tmr_cancel(&menu.tmr_redial);
menu.current_attempts = 0;
}
static char *errorcode_fb_aufile(uint16_t scode)
{
switch (scode) {
case 404: return "notfound.wav";
case 486: return "busy.wav";
case 487: return NULL; /* ignore */
default: return "error.wav";
}
}
static char *errorcode_key_aufile(uint16_t scode)
{
switch (scode) {
case 404: return "notfound_aufile";
case 486: return "busy_aufile";
case 487: return NULL; /* ignore */
default: return "error_aufile";
}
}
static bool active_call_test(const struct call* call)
{
return call_state(call) == CALL_STATE_ESTABLISHED &&
!call_is_onhold(call);
}
static void find_first_call(struct call *call, void *arg)
{
struct call **callp = arg;
if (!*callp)
*callp = call;
}
struct call *menu_find_call_state(enum call_state st)
{
struct le *le;
for (le = list_head(uag_list()); le; le = le->next) {
struct ua *ua = le->data;
struct call *call = ua_find_call_state(ua, st);
if (call)
return call;
}
return NULL;
}
/**
* Search all User-Agents for a call that matches
*
* @param matchh Optional match handler. If NULL, the last call of the first
* User-Agent is returned
*
* @return A call that matches
*/
struct call *menu_find_call(call_match_h *matchh)
{
struct call *call = NULL;
uag_filter_calls(find_first_call, matchh, &call);
return call;
}
static void menu_stop_play(void)
{
menu.play = mem_deref(menu.play);
menu.ringback = false;
}
static void menu_play(const char *ckey, const char *fname, int repeat)
{
struct config *cfg = conf_config();
struct player *player = baresip_player();
struct pl pl = PL_INIT;
char *file = NULL;
if (conf_get(conf_cur(), ckey, &pl))
pl_set_str(&pl, fname);
if (!pl_isset(&pl))
return;
pl_strdup(&file, &pl);
menu_stop_play();
(void)play_file(&menu.play, player, file, repeat,
cfg->audio.play_mod,
cfg->audio.play_dev);
mem_deref(file);
}
static void play_incoming(const struct call *call)
{
/* stop any ringtones */
menu_stop_play();
/* Only play the ringtones if answermode is "Manual".
* If the answermode is "auto" then be silent.
*/
if (ANSWERMODE_MANUAL == account_answermode(call_account(call))) {
if (menu_find_call(active_call_test)) {
menu_play("callwaiting_aufile", "callwaiting.wav", 3);
}
else {
/* Alert user */
menu_play("ring_aufile", "ring.wav", -1);
}
}
}
static void play_ringback(void)
{
/* stop any ringtones */
menu_stop_play();
if (menu.ringback_disabled) {
info("\nRingback disabled\n");
}
else {
menu_play("ringback_aufile", "ringback.wav", -1);
menu.ringback = true;
}
}
static void play_resume(void)
{
struct call *call = uag_call_find(menu.callid);
switch (call_state(call)) {
case CALL_STATE_INCOMING:
play_incoming(call);
break;
case CALL_STATE_RINGING:
if (!menu.ringback && !menu_find_call(active_call_test))
play_ringback();
break;
default:
break;
}
}
static void check_registrations(void)
{
static bool ual_ready = false;
struct le *le;
uint32_t n;
if (ual_ready)
return;
for (le = list_head(uag_list()); le; le = le->next) {
struct ua *ua = le->data;
if (!ua_isregistered(ua) && !account_prio(ua_account(ua)))
return;
}
n = list_count(uag_list());
/* We are ready */
ui_output(baresip_uis(),
"\x1b[32mAll %u useragent%s registered successfully!"
" (%u ms)\x1b[;m\n",
n, n==1 ? "" : "s",
(uint32_t)(tmr_jiffies() - menu.start_ticks));
ual_ready = true;
}
static void redial_handler(void *arg)
{
char *uri = NULL;
int err;
(void)arg;
info("now: redialing now. current_attempts=%u, max_attempts=%u\n",
menu.current_attempts,
menu.redial_attempts);
if (menu.current_attempts > menu.redial_attempts) {
info("menu: redial: too many attemptes -- giving up\n");
return;
}
if (menu.dialbuf->end == 0) {
warning("menu: redial: dialbuf is empty\n");
return;
}
menu.dialbuf->pos = 0;
err = mbuf_strdup(menu.dialbuf, &uri, menu.dialbuf->end);
if (err)
return;
err = ua_connect(uag_find_aor(menu.redial_aor), NULL, NULL,
uri, VIDMODE_ON);
if (err) {
warning("menu: redial: ua_connect failed (%m)\n", err);
}
mem_deref(uri);
}
static void menu_play_closed(struct call *call)
{
uint16_t scode;
const char *key;
const char *fb;
/* stop any ringtones */
menu_stop_play();
if (call_scode(call)) {
scode = call_scode(call);
key = errorcode_key_aufile(scode);
fb = errorcode_fb_aufile(scode);
menu_play(key, fb, 1);
}
}
static void auans_play_finished(struct play *play, void *arg)
{
struct call *call = arg;
int32_t adelay = call_answer_delay(call);
(void) play;
if (call_state(call) == CALL_STATE_INCOMING) {
call_start_answtmr(call, adelay);
if (adelay >= MIN_RINGTIME)
play_incoming(call);
}
}
static void start_sip_autoanswer(struct call *call)
{
int32_t adelay = call_answer_delay(call);
bool beep = true;
if (adelay == -1)
return;
conf_get_bool(conf_cur(), "sip_autoanswer_beep", &beep);
if (beep) {
menu_play("sip_autoanswer_aufile", "autoanswer.wav", 1);
play_set_finish_handler(menu.play, auans_play_finished, call);
}
else {
call_start_answtmr(call, adelay);
if (adelay >= MIN_RINGTIME)
play_incoming(call);
}
}
static void ua_event_handler(struct ua *ua, enum ua_event ev,
struct call *call, const char *prm, void *arg)
{
struct call *call2 = NULL;
struct account *acc = ua_account(ua);
int32_t adelay = -1;
bool incall;
enum sdp_dir ardir, vrdir;
uint32_t count;
int err;
(void)prm;
(void)arg;
#if 0
debug("menu: [ ua=%s call=%s ] event: %s (%s)\n",
account_aor(acc), call_id(call), uag_event_str(ev), prm);
#endif
count = uag_call_count();
switch (ev) {
case UA_EVENT_CALL_INCOMING:
/* set the current User-Agent to the one with the call */
menu_selcall(call);
menu_stop_play();
ardir =sdp_media_rdir(
stream_sdpmedia(audio_strm(call_audio(call))));
vrdir = sdp_media_rdir(
stream_sdpmedia(video_strm(call_video(call))));
if (!call_has_video(call))
vrdir = SDP_INACTIVE;
info("%s: Incoming call from: %s %s - audio-video: %s-%s -"
" (press 'a' to accept)\n",
account_aor(acc), call_peername(call), call_peeruri(call),
sdp_dir_name(ardir), sdp_dir_name(vrdir));
if (acc && account_sip_autoanswer(acc))
adelay = call_answer_delay(call);
if (adelay == -1)
play_incoming(call);
else
start_sip_autoanswer(call);
break;
case UA_EVENT_CALL_RINGING:
menu_selcall(call);
if (!menu.ringback && !menu_find_call(active_call_test))
play_ringback();
break;
case UA_EVENT_CALL_PROGRESS:
menu_selcall(call);
menu_stop_play();
break;
case UA_EVENT_CALL_ANSWERED:
menu_stop_play();
break;
case UA_EVENT_CALL_ESTABLISHED:
menu_selcall(call);
/* stop any ringtones */
menu_stop_play();
/* We must stop the re-dialing if the call was
established */
redial_reset();
uag_hold_others(call);
break;
case UA_EVENT_CALL_CLOSED:
/* Activate the re-dialing if:
*
* - redial_attempts must be enabled in config
* - the closed call must be of outgoing direction
* - the closed call must fail with special code 701
*/
if (menu.redial_attempts) {
if (menu.current_attempts
||
(call_is_outgoing(call) &&
call_scode(call) == 701)) {
info("menu: call closed"
" -- redialing in %u seconds\n",
menu.redial_delay);
++menu.current_attempts;
str_ncpy(menu.redial_aor, account_aor(acc),
sizeof(menu.redial_aor));
tmr_start(&menu.tmr_redial,
menu.redial_delay*1000,
redial_handler, NULL);
}
else {
info("menu: call closed -- not redialing\n");
}
}
if (!str_cmp(call_id(call), menu.callid)) {
menu_play_closed(call);
menu_selcall(NULL);
play_resume();
}
break;
case UA_EVENT_CALL_REMOTE_SDP:
if (!str_cmp(prm, "answer") &&
call_state(call) == CALL_STATE_ESTABLISHED)
menu_selcall(call);
break;
case UA_EVENT_CALL_TRANSFER:
/*
* Create a new call to transfer target.
*
* NOTE: we will automatically connect a new call to the
* transfer target
*/
info("menu: transferring call %s to '%s'\n",
call_id(call), prm);
err = ua_call_alloc(&call2, ua, VIDMODE_ON, NULL, call,
call_localuri(call), true);
if (!err) {
struct pl pl;
pl_set_str(&pl, prm);
err = call_connect(call2, &pl);
if (err) {
warning("ua: transfer: connect error: %m\n",
err);
}
}
if (err) {
(void)call_notify_sipfrag(call, 500, "Call Error");
mem_deref(call2);
}
break;
case UA_EVENT_CALL_TRANSFER_FAILED:
info("menu: transfer failure: %s\n", prm);
break;
case UA_EVENT_REGISTER_OK:
check_registrations();
break;
case UA_EVENT_UNREGISTERING:
return;
case UA_EVENT_MWI_NOTIFY:
info("----- MWI for %s -----\n", account_aor(acc));
info("%s\n", prm);
break;
case UA_EVENT_AUDIO_ERROR:
info("menu: audio error (%s)\n", prm);
break;
default:
break;
}
incall = ev == UA_EVENT_CALL_CLOSED ? count > 1 : count;
menu_set_incall(incall);
menu_update_callstatus(incall);
}
static void message_handler(struct ua *ua, const struct pl *peer,
const struct pl *ctype,
struct mbuf *body, void *arg)
{
struct config *cfg;
(void)ua;
(void)ctype;
(void)arg;
cfg = conf_config();
ui_output(baresip_uis(), "\r%r: \"%b\"\n",
peer, mbuf_buf(body), mbuf_get_left(body));
(void)play_file(NULL, baresip_player(), "message.wav", 0,
cfg->audio.alert_mod, cfg->audio.alert_dev);
}
/**
* Get the menu object
*
* @return ptr to menu object
*/
struct menu *menu_get(void)
{
return &menu;
}
/**
* Selects the active call.
*
* @param call The call
*/
void menu_selcall(struct call *call)
{
int i;
enum call_state state[] = {
CALL_STATE_INCOMING,
CALL_STATE_OUTGOING,
CALL_STATE_RINGING,
CALL_STATE_EARLY,
CALL_STATE_ESTABLISHED,
};
if (!call) {
/* select another call */
for (i = ARRAY_SIZE(state)-1; i >= 0; --i) {
call = menu_find_call_state(state[i]);
if (!str_cmp(call_id(call), menu.callid))
call = NULL;
if (call)
break;
}
}
menu.callid = mem_deref(menu.callid);
if (call) {
str_dup(&menu.callid, call_id(call));
call_set_current(ua_calls(call_get_ua(call)), call);
}
}
/**
* Gets the active call.
*
* @return The active call.
*/
struct call *menu_callcur(void)
{
return uag_call_find(menu.callid);
}
/**
* Get UA of active call
*
* @return ptr to UA object
*/
struct ua *menu_uacur(void)
{
return call_get_ua(menu_callcur());
}
/**
* Manual selection of the UA via command parameter
* - carg->data has highest priority
* - otherwise second word in carg->prm is checked for an UA index
*
* @param pf Print backend
* @param carg Command argument
* @param word1 First word
* @param word2 Second word
*
* @return The UA if found, NULL otherwise.
*/
struct ua *menu_ua_carg(struct re_printf *pf, const struct cmd_arg *carg,
struct pl *word1, struct pl *word2)
{
int err;
struct le *le;
uint32_t i;
struct ua *ua = carg->data;
if (ua)
return ua;
err = re_regex(carg->prm, str_len(carg->prm), "[^ ]+ [^ ]+", word1,
word2);
if (err)
return NULL;
i = pl_u32(word2);
le = uag_list()->head;
while (le && i--)
le = le->next;
if (le) {
ua = le->data;
info("%s: selected for request\n",
account_aor(ua_account(ua)));
}
else {
re_hprintf(pf, "no User-Agent at pos %r\n", word2);
}
return ua;
}
/**
* Decode a command parameter
*
* @param prm Command arguments parameter string
* @param name Parameter name
* @param val Returned parameter value
*
* @return 0 for success, otherwise errorcode
*/
int menu_param_decode(const char *prm, const char *name, struct pl *val)
{
char expr[128];
struct pl v;
if (!str_isset(prm) || !name || !val)
return EINVAL;
(void)re_snprintf(expr, sizeof(expr),
"[ \t\r\n]*%s[ \t\r\n]*=[ \t\r\n]*[~ \t\r\n;]+",
name);
if (re_regex(prm, strlen(prm), expr, NULL, NULL, NULL, &v))
return ENOENT;
*val = v;
return 0;
}
static int module_init(void)
{
struct pl val;
int err;
memset(&menu, 0, sizeof(menu));
menu.redial_attempts = 0;
menu.redial_delay = 5;
menu.ringback_disabled = false;
menu.statmode = STATMODE_CALL;
menu.clean_number = false;
menu.play = NULL;
menu.adelay = -1;
/*
* Read the config values
*/
conf_get_bool(conf_cur(), "ringback_disabled",
&menu.ringback_disabled);
conf_get_bool(conf_cur(), "menu_clean_number", &menu.clean_number);
if (0 == conf_get(conf_cur(), "redial_attempts", &val) &&
0 == pl_strcasecmp(&val, "inf")) {
menu.redial_attempts = (uint32_t)-1;
}
else {
conf_get_u32(conf_cur(), "redial_attempts",
&menu.redial_attempts);
}
conf_get_u32(conf_cur(), "redial_delay", &menu.redial_delay);
if (menu.redial_attempts) {
info("menu: redial enabled with %u attempts and"
" %u seconds delay\n",
menu.redial_attempts,
menu.redial_delay);
}
menu.dialbuf = mbuf_alloc(64);
if (!menu.dialbuf)
return ENOMEM;
menu.start_ticks = tmr_jiffies();
if (0 == conf_get(conf_cur(), "statmode_default", &val) &&
0 == pl_strcasecmp(&val, "off")) {
menu.statmode = STATMODE_OFF;
}
else {
menu.statmode = STATMODE_CALL;
}
err = static_menu_register();
err |= dial_menu_register();
if (err)
return err;
err = uag_event_register(ua_event_handler, NULL);
if (err)
return err;
err = message_listen(baresip_message(),
message_handler, NULL);
if (err)
return err;
return err;
}
static int module_close(void)
{
debug("menu: close (redial current_attempts=%d)\n",
menu.current_attempts);
message_unlisten(baresip_message(), message_handler);
uag_event_unregister(ua_event_handler);
static_menu_unregister();
dial_menu_unregister();
dynamic_menu_unregister();
tmr_cancel(&menu.tmr_stat);
menu.dialbuf = mem_deref(menu.dialbuf);
menu.callid = mem_deref(menu.callid);
menu_stop_play();
tmr_cancel(&menu.tmr_redial);
return 0;
}
const struct mod_export DECL_EXPORTS(menu) = {
"menu",
"application",
module_init,
module_close
};
|