/* tel — terminal SIP phone * * Single-file curses front end for libpjsua (pjproject). Registers an * account from /etc/telephony.conf, shows incoming calls, dials numbers or * full sip: URIs, and handles mute/DTMF during a call. * * USAGE * tel [-c file] [--null-audio] [-v] * * CONFIG (/etc/telephony.conf, key = value) * uri = sip:user@example.org * registrar = sip:example.org * user = user * password = secret * realm = * * * KEYS * digits/letters append to dial string * Enter dial * a / d answer / decline incoming call * h hangup * m mute * 0-9 * # DTMF while in call * Esc clear input * Ctrl-Q quit * * BUILD * make * * AUTHOR * ====== * Written by 'telephony' contributors. */ #include #include #include #include #include #include #include #include #define CONFIG "/etc/telephony.conf" #define HISTN 5 #define HISTLEN 96 #define DIALMAX 191 static struct { char uri[160]; char registrar[160]; char user[64]; char pass[64]; char realm[64]; int nullaudio; int verbose; int port; int headless; int autoanswer; int seconds; int listdev; int snd; char callto[256]; pjsua_acc_id acc; int accok; char dial[DIALMAX + 1]; int diallen; int muted; int quit; volatile int dirty; char msg[160]; char hist[HISTN][HISTLEN]; int histn; } app; static void hist_add(const char *s) { int i; if (!s[0]) return; for (i = HISTN - 1; i > 0; i--) memcpy(app.hist[i], app.hist[i - 1], HISTLEN); snprintf(app.hist[0], HISTLEN, "%s", s); if (app.histn < HISTN) app.histn++; } static char *trim(char *s) { char *e; while (*s == ' ' || *s == '\t') s++; e = s + strlen(s); while (e > s && (e[-1] == ' ' || e[-1] == '\t' || e[-1] == '\n' || e[-1] == '\r')) *--e = 0; return s; } static void cfg_load(const char *path) { FILE *f; char line[512], *p, *k, *v; f = fopen(path, "r"); if (!f) return; while (fgets(line, sizeof(line), f)) { p = strchr(line, '='); if (!p) continue; *p = 0; k = trim(line); v = trim(p + 1); if (!strcmp(k, "uri")) snprintf(app.uri, sizeof(app.uri), "%s", v); else if (!strcmp(k, "registrar")) snprintf(app.registrar, sizeof(app.registrar), "%s", v); else if (!strcmp(k, "user")) snprintf(app.user, sizeof(app.user), "%s", v); else if (!strcmp(k, "password")) snprintf(app.pass, sizeof(app.pass), "%s", v); else if (!strcmp(k, "realm")) snprintf(app.realm, sizeof(app.realm), "%s", v); } fclose(f); } static const char *sip_host(void) { static char h[160]; const char *p = app.registrar; char *e; if (!strncmp(p, "sips:", 5)) p += 5; else if (!strncmp(p, "sip:", 4)) p += 4; snprintf(h, sizeof(h), "%s", p); e = strpbrk(h, ";/>"); if (e) *e = 0; return h; } static int sip_target(char *out, size_t n) { const char *d = app.dial; if (!d[0]) return 0; if (!strncmp(d, "sip:", 4) || !strncmp(d, "sips:", 5)) { snprintf(out, n, "%s", d); return 1; } if (strchr(d, '@')) { snprintf(out, n, "sip:%s", d); return 1; } if (!sip_host()[0]) return 0; snprintf(out, n, "sip:%s@%s", d, sip_host()); return 1; } static pjsua_call_id cur_call(void) { pjsua_call_id i, max = pjsua_call_get_max_count(); for (i = 0; i < max; i++) if (pjsua_call_is_active(i)) return i; return PJSUA_INVALID_ID; } static void on_incoming(pjsua_acc_id acc_id, pjsua_call_id call_id, pjsip_rx_data *rdata) { (void)acc_id; (void)rdata; snprintf(app.msg, sizeof(app.msg), "incoming call"); if (app.headless) printf("incoming\n"); fflush(stdout); if (app.autoanswer) pjsua_call_answer(call_id, 200, NULL, NULL); app.dirty = 1; } static void on_call_state(pjsua_call_id call_id, pjsip_event *e) { pjsua_call_info ci; char line[HISTLEN]; (void)e; if (pjsua_call_get_info(call_id, &ci) != PJ_SUCCESS) return; if (ci.state == PJSIP_INV_STATE_DISCONNECTED) { snprintf(line, sizeof(line), "%s %.*s", ci.role == PJSIP_ROLE_UAC ? "out" : "in", (int)ci.remote_info.slen, ci.remote_info.ptr); hist_add(line); } if (app.headless) { printf("call %d state=%.*s code=%d\n", call_id, (int)ci.state_text.slen, ci.state_text.ptr, ci.last_status); fflush(stdout); } app.dirty = 1; } static void on_media_state(pjsua_call_id call_id) { pjsua_call_info ci; if (pjsua_call_get_info(call_id, &ci) != PJ_SUCCESS) return; if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) { pjsua_conf_connect(ci.conf_slot, 0); pjsua_conf_connect(0, ci.conf_slot); pjsua_conf_adjust_tx_level(0, app.muted ? 0.0f : 1.0f); } if (app.headless) { printf("media %d status=%d\n", call_id, ci.media_status); fflush(stdout); } app.dirty = 1; } static void on_reg_state(pjsua_acc_id acc_id) { pjsua_acc_info ai; (void)acc_id; if (app.headless && pjsua_acc_get_info(app.acc, &ai) == PJ_SUCCESS) { printf("reg %d %.*s\n", ai.status, (int)ai.status_text.slen, ai.status_text.ptr); fflush(stdout); } app.dirty = 1; } static void on_signal(int sig) { (void)sig; app.quit = 1; } static int sip_init(void) { pjsua_config cfg; pjsua_logging_config lc; pjsua_media_config mc; pjsua_transport_config tc; pjsua_acc_config acfg; pj_status_t st; if (pjsua_create() != PJ_SUCCESS) return -1; pjsua_config_default(&cfg); cfg.cb.on_incoming_call = &on_incoming; cfg.cb.on_call_state = &on_call_state; cfg.cb.on_call_media_state = &on_media_state; cfg.cb.on_reg_state = &on_reg_state; pjsua_logging_config_default(&lc); lc.console_level = 0; lc.level = app.verbose ? 4 : 0; pjsua_media_config_default(&mc); mc.clock_rate = 8000; mc.snd_clock_rate = 8000; mc.ec_tail_len = 0; mc.enable_ice = PJ_FALSE; if (pjsua_init(&cfg, &lc, &mc) != PJ_SUCCESS) return -1; pjsua_transport_config_default(&tc); tc.port = app.port ? (unsigned)app.port : 5060; st = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &tc, NULL); if (st != PJ_SUCCESS) { tc.port = 0; st = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &tc, NULL); } if (st != PJ_SUCCESS) return -1; if (pjsua_start() != PJ_SUCCESS) return -1; if (app.nullaudio) { pjsua_set_null_snd_dev(); } else if (app.snd >= 0) { if (pjsua_set_snd_dev(app.snd, app.snd) != PJ_SUCCESS) { pjsua_set_null_snd_dev(); snprintf(app.msg, sizeof(app.msg), "sound device %d unavailable", app.snd); } } else if (pjsua_set_snd_dev(-1, -1) != PJ_SUCCESS) { pjsua_set_null_snd_dev(); snprintf(app.msg, sizeof(app.msg), "no audio device (null audio)"); } if (!app.uri[0]) { if (app.user[0]) snprintf(app.uri, sizeof(app.uri), "sip:%s@%s", app.user, sip_host()[0] ? sip_host() : "localhost"); else snprintf(app.uri, sizeof(app.uri), "sip:tel@localhost"); } pjsua_acc_config_default(&acfg); acfg.id = pj_str(app.uri); acfg.reg_uri = pj_str(app.registrar); if (app.user[0]) { acfg.cred_count = 1; acfg.cred_info[0].realm = pj_str(app.realm[0] ? app.realm : (char *)"*"); acfg.cred_info[0].scheme = pj_str((char *)"digest"); acfg.cred_info[0].username = pj_str(app.user); acfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; acfg.cred_info[0].data = pj_str(app.pass); } if (pjsua_acc_add(&acfg, PJ_TRUE, &app.acc) != PJ_SUCCESS) return -1; app.accok = 1; return 0; } static void draw(void) { pjsua_call_id id = cur_call(); pjsua_call_info ci; pjsua_acc_info ai; int row = 0, i; char dur[32]; erase(); attron(A_BOLD); mvprintw(row++, 0, "tel"); attroff(A_BOLD); row++; mvprintw(row++, 0, "account %s", app.uri); if (app.registrar[0] && app.accok && pjsua_acc_get_info(app.acc, &ai) == PJ_SUCCESS) mvprintw(row++, 0, "status %.*s", (int)ai.status_text.slen, ai.status_text.ptr); else mvprintw(row++, 0, "status direct only"); row++; if (id != PJSUA_INVALID_ID && pjsua_call_get_info(id, &ci) == PJ_SUCCESS) { attron(A_BOLD); mvprintw(row++, 0, ci.state == PJSIP_INV_STATE_INCOMING ? "INCOMING CALL" : "CALL"); attroff(A_BOLD); mvprintw(row++, 0, "peer %.*s", (int)ci.remote_info.slen, ci.remote_info.ptr); mvprintw(row++, 0, "state %.*s", (int)ci.state_text.slen, ci.state_text.ptr); snprintf(dur, sizeof(dur), "%ld:%02ld", (long)(ci.connect_duration.sec / 60), (long)(ci.connect_duration.sec % 60)); mvprintw(row++, 0, "time %s", dur); mvprintw(row++, 0, "mute %s", app.muted ? "on" : "off"); } else { mvprintw(row++, 0, "idle"); } row++; for (i = 0; i < app.histn; i++) mvprintw(row++, 0, "%.*s", COLS - 2, app.hist[i]); if (app.msg[0]) { row++; mvprintw(row++, 0, "%.*s", COLS - 2, app.msg); } if (LINES > 3) { if (id != PJSUA_INVALID_ID) mvprintw(LINES - 2, 0, "%.*s", COLS - 1, "digits=dtmf h=hangup m=mute ^Q=quit"); else mvprintw(LINES - 2, 0, "dial> %.*s", COLS - 8, app.dial); } move(LINES - 2, COLS > 8 ? 6 + app.diallen : 0); refresh(); app.dirty = 0; } static void place_call(const char *target) { pjsua_call_id id; pj_str_t dst; pj_status_t st; char err[128]; dst = pj_str((char *)target); st = pjsua_call_make_call(app.acc, &dst, NULL, NULL, NULL, &id); if (st != PJ_SUCCESS) { pj_strerror(st, err, sizeof(err)); snprintf(app.msg, sizeof(app.msg), "call failed: %s", err); if (app.headless) { printf("call failed %s: %s\n", target, err); fflush(stdout); } return; } snprintf(app.msg, sizeof(app.msg), "calling %s", target); if (app.headless) { printf("calling %s\n", target); fflush(stdout); } } static void do_dial(void) { char target[256]; if (!sip_target(target, sizeof(target))) { snprintf(app.msg, sizeof(app.msg), "need number or sip: URI"); return; } place_call(target); app.dial[0] = 0; app.diallen = 0; } static void handle_key(int ch) { pjsua_call_id id = cur_call(); pjsua_call_info ci; int incoming = 0; char b[2]; pj_str_t ds; if (ch == 3) { app.quit = 1; return; } if (id != PJSUA_INVALID_ID && pjsua_call_get_info(id, &ci) == PJ_SUCCESS && ci.state == PJSIP_INV_STATE_INCOMING) incoming = 1; if (incoming) { if (ch == 'a' || ch == '\n' || ch == '\r') pjsua_call_answer(id, 200, NULL, NULL); else if (ch == 'd') pjsua_call_hangup(id, 486, NULL, NULL); return; } if (id != PJSUA_INVALID_ID) { if (ch == 'h' || ch == 27) { pjsua_call_hangup(id, 0, NULL, NULL); } else if (ch == 'm') { app.muted = !app.muted; pjsua_conf_adjust_tx_level(0, app.muted ? 0.0f : 1.0f); } else if (ch && strchr("0123456789*#", ch)) { b[0] = (char)ch; b[1] = 0; ds = pj_str(b); pjsua_call_dial_dtmf(id, &ds); } app.dirty = 1; return; } if (ch == '\n' || ch == '\r' || ch == KEY_ENTER) { do_dial(); } else if (ch == 127 || ch == 8 || ch == KEY_BACKSPACE) { if (app.diallen > 0) app.dial[--app.diallen] = 0; } else if (ch == 27) { app.dial[0] = 0; app.diallen = 0; } else if (ch >= 32 && ch < 127 && app.diallen < DIALMAX) { app.dial[app.diallen++] = (char)ch; app.dial[app.diallen] = 0; } app.dirty = 1; } int main(int argc, char **argv) { const char *cfg = CONFIG; int i, ch, tick = 0; int efd = -1, nfd; app.snd = -1; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-c") && i + 1 < argc) { cfg = argv[++i]; } else if (!strcmp(argv[i], "--null-audio")) { app.nullaudio = 1; } else if (!strcmp(argv[i], "-v")) { app.verbose = 1; } else if (!strcmp(argv[i], "--port") && i + 1 < argc) { app.port = atoi(argv[++i]); } else if (!strcmp(argv[i], "--headless")) { app.headless = 1; } else if (!strcmp(argv[i], "--auto-answer")) { app.autoanswer = 1; } else if (!strcmp(argv[i], "--call") && i + 1 < argc) { snprintf(app.callto, sizeof(app.callto), "%s", argv[++i]); } else if (!strcmp(argv[i], "--seconds") && i + 1 < argc) { app.seconds = atoi(argv[++i]); } else if (!strcmp(argv[i], "--list-dev")) { app.listdev = 1; } else if (!strcmp(argv[i], "--snd") && i + 1 < argc) { app.snd = atoi(argv[++i]); } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { printf("usage: tel [-c file] [--null-audio] [-v] " "[--port N] [--headless] [--auto-answer] " "[--call URI] [--seconds N] [--snd N] " "[--list-dev]\n"); return 0; } else { fprintf(stderr, "tel: unknown option %s\n", argv[i]); return 2; } } cfg_load(cfg); signal(SIGINT, on_signal); signal(SIGTERM, on_signal); app.dirty = 1; if (!app.verbose) { efd = dup(2); nfd = open("/dev/null", O_WRONLY); if (nfd >= 0) { dup2(nfd, 2); close(nfd); } } if (sip_init() != 0) { if (efd >= 0) { dup2(efd, 2); close(efd); } fprintf(stderr, "tel: SIP init failed\n"); return 1; } if (efd >= 0) { dup2(efd, 2); close(efd); } if (app.listdev) { pjmedia_aud_dev_info di[32]; unsigned n = 32, k; pjsua_enum_aud_devs(di, &n); for (k = 0; k < n; k++) printf("%u\t%s\tin=%u\tout=%u\n", k, di[k].name, di[k].input_count, di[k].output_count); pjsua_destroy(); return 0; } if (app.headless) { printf("ready port=%d uri=%s\n", app.port ? app.port : 5060, app.uri); fflush(stdout); if (app.callto[0]) { sleep(1); place_call(app.callto); } for (i = 0; app.seconds <= 0 || i < app.seconds * 10; i++) usleep(100000); pjsua_call_hangup_all(); pjsua_destroy(); return 0; } initscr(); cbreak(); noecho(); nonl(); keypad(stdscr, TRUE); curs_set(1); nodelay(stdscr, TRUE); while (!app.quit) { if (app.dirty || ++tick >= 10) { tick = 0; draw(); } while ((ch = getch()) != ERR) handle_key(ch); napms(100); } pjsua_call_hangup_all(); pjsua_destroy(); endwin(); return 0; }