summaryrefslogtreecommitdiffstats
path: root/src/telephone/telephone.c
blob: d4cc5409c7fd608b9a75246d12afff30279a22cc (plain)
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
/* console handling
 *
 * (C) 2020 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stddef.h>
#include "../libdebug/debug.h"
#include "../libsample/sample.h"
#include "../libg711/g711.h"
#include "../libsound/sound.h"
#include "telephone.h"
#include "../libosmocc/helper.h"

static struct osmo_cc_helper_audio_codecs codecs[] = {
	{ "PCMA", 8000, 1, g711_encode_alaw, g711_decode_alaw },
	{ "PCMU", 8000, 1, g711_encode_ulaw, g711_decode_ulaw },
	{ NULL, 0, 0, NULL, NULL},
};

const char *cause_name(int cause)
{
	static char cause_str[16];

	switch (cause) {
	case OSMO_CC_ISDN_CAUSE_NORM_CALL_CLEAR:
		return "hangup";
	case OSMO_CC_ISDN_CAUSE_USER_BUSY:
		return "busy";
	case OSMO_CC_ISDN_CAUSE_USER_ALERTING_NA:
		return "no-answer";
	case OSMO_CC_ISDN_CAUSE_DEST_OOO:
	case OSMO_CC_ISDN_CAUSE_NETWORK_OOO:
		return "out-of-order";
	case OSMO_CC_ISDN_CAUSE_INV_NR_FORMAT:
		return "invalid-number";
	case OSMO_CC_ISDN_CAUSE_NO_CIRCUIT_CHAN:
		return "no-channel";
	case OSMO_CC_ISDN_CAUSE_TEMP_FAILURE:
		return "link-failure";
	case OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL:
		return "resource-unavail";
	default:
		sprintf(cause_str, "cause=%d", cause);
		return cause_str;
	}

}

static char ui_text[256];
static char ui_clear[256];
static int ui_autoalert;
static int ui_autoanswer;
static int ui_len = 0;
#define UI_MAX_DIGITS 33
static char ui_remote_id[256] = ""; /* what we dial or who called us */
static char ui_remote_dialing[256] = ""; /* what remote called us with */
static char ui_local_id[256] = ""; /* our ID */
static const char ui_digits[] = "0123456789*#ABCDE";
static uint8_t ui_cause = 0;

static void append_string(char *string, size_t size, char c)
{
	size_t len = strlen(string);
	int i;

	/* string full */
	if (len == size - 1)
		return;

	for (i = 0; i < (int)strlen(ui_digits); i++) {
		if (c == ui_digits[i]) {
			string[len] = c;
			string[len + 1] = '\0';
			break;
		}
	}
}

/*
 * Endpoint instance
 */

/* create interface instance */
telephone_t *telephone_create(void)
{
	telephone_t *telephone_ep;

	telephone_ep = calloc(1, sizeof(*telephone_ep));
	if (!telephone_ep) {
		PDEBUG(DTEL, DEBUG_ERROR, "No memory!\n");
		abort();
	}

	PDEBUG(DTEL, DEBUG_DEBUG, "Telephone instance created\n");

	return telephone_ep;
}

static void telephone_close(telephone_t *telephone_ep);
static void call_destroy(call_t *call);

/* destroy interface instance and free all resource */
void telephone_destroy(telephone_t *telephone_ep)
{
	/* remove stack instance */
	telephone_close(telephone_ep);

	/* destroy all calls */
	while (telephone_ep->call_list)
		call_destroy(telephone_ep->call_list);

	free((char *)telephone_ep->name);

	free(telephone_ep);

	PDEBUG(DTEL, DEBUG_DEBUG, "Telephone instance destroyed\n");
}

/* initialization and configuration of interface instance */
int telephone_init(telephone_t *telephone_ep, const char *name, const char *callerid, uint8_t serving_location, int early_audio, const char *audiodev, int samplerate, int __attribute__((unused)) latspl)
{
	telephone_ep->name = strdup(name);
	telephone_ep->serving_location = serving_location;
	telephone_ep->early_audio = early_audio;
	telephone_ep->samplerate = samplerate;
	telephone_ep->latspl = latspl;
	telephone_ep->loopback = 0;
	strcpy(ui_local_id, callerid);

	if (audiodev) {
#ifdef HAVE_ALSA
		/* open sound device for call control */
		/* use factor 1.4 of speech level for complete range of sound card */
		telephone_ep->sound = sound_open(audiodev, NULL, NULL, NULL, 1, 0.0, samplerate, latspl, 1 / (SPEECH_LEVEL * 0.7079), 4000.0, 2.0);
		if (!telephone_ep->sound) {
			PDEBUG(DTEL, DEBUG_ERROR, "No sound device!\n");
			return -EIO;
		}
		sound_start(telephone_ep->sound);
#else
		PDEBUG(DTEL, DEBUG_ERROR, "No sound card support compiled in!\n");
		return -ENOTSUP;
#endif
	}

	return 0;
}

static void telephone_close(telephone_t *telephone_ep)
{
#ifdef HAVE_ALSA
	if (telephone_ep->sound) {
		sound_close(telephone_ep->sound);
		telephone_ep->sound = NULL;
	}
#endif
}

/*
 * call instance
 */

static const char *call_state_name[] = {
	"on hook",
	"incoming setup",
	"outgoing setup",
	"incoming overlap",
	"outgoing overlap",
	"incoming proceeding",
	"outgoing proceeding",
	"incoming alerting",
	"outgoing alerting",
	"connected",
	"incoming disconect",
	"outgoing disconect",
};

static void call_new_state(call_t *call, enum call_state state)
{
	PDEBUG(DTEL, DEBUG_DEBUG, "Call state '%s' -> '%s'\n", call_state_name[call->state], call_state_name[state]);
	call->state = state;
}

static call_t *call_create(telephone_t *telephone_ep)
{
	call_t *call, **call_p;
	int rc;

	call = calloc(1, sizeof(*call));
	if (!call) {
		PDEBUG(DTEL, DEBUG_ERROR, "No memory!\n");
		abort();
	}

	call_p = &telephone_ep->call_list;
	while (*call_p)
		call_p = &((*call_p)->next);
	*call_p = call;

	call->telephone_ep = telephone_ep;

	/* init sample rate conversion */ 
	rc = init_samplerate(&call->srstate, 8000.0, (double)telephone_ep->samplerate, 3300.0);
	if (rc < 0)
		abort();

	/* allocate jitter buffer */
	rc = jitter_create(&call->dejitter, telephone_ep->samplerate / 10); // FIXME: size
	if (rc < 0)
		abort();

	PDEBUG(DTEL, DEBUG_DEBUG, "Created new call instance\n");

	return call;
}

static void call_destroy(call_t *call)
{
	call_t **call_p;

	/* free sdp */
	free((char *)call->sdp);

	/* free jitter buffer */
	jitter_destroy(&call->dejitter);

	/* free session description */
	if (call->cc_session)
		osmo_cc_free_session(call->cc_session);

	/* detach */
	call_p = &call->telephone_ep->call_list;
	while (*call_p) {
		if (*call_p == call)
			break;
		call_p = &((*call_p)->next);
	}
	*call_p = call->next;

	free(call);

	PDEBUG(DTEL, DEBUG_DEBUG, "destroyed call instance\n");
}

/*
 * audio handling
 */

/* take audio from CC and store in jitter buffer */
void down_audio(struct osmo_cc_session_codec *codec, uint16_t __attribute__((unused)) sequence_number, uint32_t __attribute__((unused)) timestamp, uint8_t *data, int len)
{
	call_t *call = codec->media->session->priv;
	int count = len / 2;
	sample_t samples[count];

	if (call->telephone_ep->loopback != 3) {
		sample_t up[(int)((double)count * call->srstate.factor + 0.5) + 10];
		int16_to_samples(samples, (int16_t *)data, count);
		count = samplerate_upsample(&call->srstate, samples, count, up);
		jitter_save(&call->dejitter, up, count);
	}
}

void alsa_work(telephone_t *telephone_ep)
{
	if (!telephone_ep->sound)
		return;

#ifdef HAVE_ALSA
	/* handle audio, if sound device is used */
	call_t *call;
	sample_t samples[telephone_ep->latspl + 10], *samples_list[1];
	uint8_t *power_list[1];
	int count;
	int rc;

	/* hunt for call */
	for (call = telephone_ep->call_list; call; call = call->next)
		break; // just any call

	count = sound_get_tosend(telephone_ep->sound, telephone_ep->latspl);
	if (count < 0) {
		PDEBUG(DTEL, DEBUG_ERROR, "Failed to get samples in buffer (rc = %d)!\n", count);
		if (count == -EPIPE)
			PDEBUG(DTEL, DEBUG_ERROR, "Trying to recover.\n");
		return;
	}
	if (count > 0) {
		if (call)
			jitter_load(&call->dejitter, samples, count);
		else
			memset(samples, 0, sizeof(*samples) * count);
		samples_list[0] = samples;
		power_list[0] = NULL;
		rc = sound_write(telephone_ep->sound, samples_list, power_list, count, NULL, NULL, 1);
		if (rc < 0) {
			PDEBUG(DTEL, DEBUG_ERROR, "Failed to write TX data to sound device (rc = %d)\n", rc);
			if (rc == -EPIPE)
				PDEBUG(DTEL, DEBUG_ERROR, "Trying to recover.\n");
			return;
		}
	}
	samples_list[0] = samples;
	count = sound_read(telephone_ep->sound, samples_list, telephone_ep->latspl, 1, NULL);
	if (count < 0) {
		PDEBUG(DTEL, DEBUG_ERROR, "Failed to read from sound device (rc = %d)!\n", count);
		if (count == -EPIPE)
			PDEBUG(DTEL, DEBUG_ERROR, "Trying to recover.\n");
		return;
	}
	if (call && count) {
		int i;

		if (telephone_ep->loopback == 3)
			jitter_save(&call->dejitter, samples, count);
		count = samplerate_downsample(&call->srstate, samples, count);
		/* put samples into ring buffer */
		for (i = 0; i < count; i++) {
			call->tx_buffer[call->tx_buffer_pos] = samples[i];
			/* if ring buffer wrapps, deliver data down to call process */
			if (++call->tx_buffer_pos == 160) {
				call->tx_buffer_pos = 0;
				/* only if we have a call */
				if (call->cc_callref && call->codec) {
					int16_t data[160];
					samples_to_int16(data, call->tx_buffer, 160);
					osmo_cc_rtp_send(call->codec, (uint8_t *)data, 160 * 2, 1, 160);
				}
			}
		}
	}

//printf("%p, %p\n", call, call ? call->codec : NULL);
//	if (call && call->codec)
//		while (osmo_cc_rtp_receive(call->codec->media) >= 0);
#endif
}

void rtp_work(telephone_t *telephone_ep)
{
	call_t *call;

	call = telephone_ep->call_list;
	while (call) {
		if (call->cc_session)
			osmo_cc_session_handle(call->cc_session);
		call = call->next;
	}
}


/*
 * handle message from CC
 */

static void release_reject_ind(call_t *call, uint8_t msg_type, uint8_t isdn_cause)
{
	osmo_cc_msg_t *new_msg;

	/* create osmo-cc message */
	new_msg = osmo_cc_new_msg(msg_type);
	/* cause */
	osmo_cc_add_ie_cause(new_msg, call->telephone_ep->serving_location, isdn_cause, 0, 0);
	/* send message to osmo-cc */
	osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, new_msg);
	/* change state */
	call_new_state(call, CALL_STATE_IDLE);
	/* destroy call */
	call_destroy(call);
}

void cc_message(osmo_cc_endpoint_t *ep, uint32_t callref, osmo_cc_msg_t *msg)
{
	telephone_t *telephone_ep = ep->priv;
	call_t *call, *other;
	osmo_cc_msg_t *new_msg;
	uint8_t type, plan, present, screen;
	char callerid[128], dialing[128];
	uint8_t coding, location, progress, socket_cause;
	uint16_t sip_cause;
	int rc;
	int i;

	/* hunt for callref */
	call = telephone_ep->call_list;
	while (call) {
		if (call->cc_callref == callref)
			break;
		call = call->next;
	}

	/* process SETUP */
	if (!call) {
		if (msg->type != OSMO_CC_MSG_SETUP_REQ) {
			PDEBUG(DTEL, DEBUG_ERROR, "received message without call instance, please fix!\n");
			goto done;
		}
		/* creating call instance */
		call = call_create(telephone_ep);
		if (!call) {
			PDEBUG(DTEL, DEBUG_ERROR, "Cannot create calll instance.\n");
			abort();
		}
		/* link with cc */
		call->cc_callref = callref;
	}

	switch (msg->type) {
	case OSMO_CC_MSG_SETUP_REQ: /* dial-out command received from epoint */
		/* hunt for call */
		for (other = telephone_ep->call_list; other; other = other->next) {
			if (other != call)
				break;
		}
		if (other) {
			release_reject_ind(call, OSMO_CC_MSG_REJ_IND, OSMO_CC_ISDN_CAUSE_USER_BUSY);
			break;
		}
		/* calling */
		rc = osmo_cc_get_ie_calling(msg, 0, &type, &plan, &present, &screen, callerid, sizeof(callerid));
		if (rc >= 0)
			strncpy(ui_remote_id, callerid, sizeof(ui_remote_id) - 1);
		/* called */
		rc = osmo_cc_get_ie_called(msg, 0, &type, &plan, dialing, sizeof(dialing));
		if (rc >= 0)
			strncpy(ui_remote_dialing, dialing, sizeof(ui_remote_dialing) - 1);
		PDEBUG(DTEL, DEBUG_INFO, "Incoming call from '%s' to '%s'\n", ui_remote_id, ui_remote_dialing);
		/* sdp accept */
		call->sdp = osmo_cc_helper_audio_accept(call, codecs, down_audio, msg, &call->cc_session, &call->codec, 0);
		if (!call->sdp) {
			release_reject_ind(call, OSMO_CC_MSG_REJ_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
			break;
		}
		call->sdp = strdup(call->sdp);
		/* change state */
		call_new_state(call, CALL_STATE_IN_SETUP);
		if (ui_autoanswer) {
			/* create osmo-cc message */
			new_msg = osmo_cc_new_msg(OSMO_CC_MSG_SETUP_CNF);
			if (call->telephone_ep->early_audio) {
				/* progress */
				osmo_cc_add_ie_progress(msg, OSMO_CC_CODING_ITU_T, call->telephone_ep->serving_location, OSMO_CC_PROGRESS_INBAND_INFO_AVAILABLE);
				/* sdp */
				osmo_cc_add_ie_sdp(new_msg, call->sdp);
				free((char *)call->sdp);
				call->sdp = NULL;
			}
			/* send message to osmo-cc */
			osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, new_msg);
			/* change state */
			call_new_state(call, CALL_STATE_CONNECT);
		} else
		if (ui_autoalert) {
			/* create osmo-cc message */
			new_msg = osmo_cc_new_msg(OSMO_CC_MSG_ALERT_IND);
			if (call->telephone_ep->early_audio) {
				/* progress */
				osmo_cc_add_ie_progress(msg, OSMO_CC_CODING_ITU_T, call->telephone_ep->serving_location, OSMO_CC_PROGRESS_INBAND_INFO_AVAILABLE);
				/* sdp */
				osmo_cc_add_ie_sdp(new_msg, call->sdp);
				free((char *)call->sdp);
				call->sdp = NULL;
			}
			/* send message to osmo-cc */
			osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, new_msg);
			/* change state */
			call_new_state(call, CALL_STATE_IN_ALERTING);
		} else {
			/* create osmo-cc message */
			new_msg = osmo_cc_new_msg(OSMO_CC_MSG_SETUP_ACK_IND);
			if (call->telephone_ep->early_audio) {
				/* progress */
				osmo_cc_add_ie_progress(msg, OSMO_CC_CODING_ITU_T, call->telephone_ep->serving_location, OSMO_CC_PROGRESS_INBAND_INFO_AVAILABLE);
				/* sdp */
				osmo_cc_add_ie_sdp(new_msg, call->sdp);
				free((char *)call->sdp);
				call->sdp = NULL;
			}
			/* send message to osmo-cc */
			osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, new_msg);
			/* change state */
			call_new_state(call, CALL_STATE_IN_OVERLAP);
		}
		break;
	case OSMO_CC_MSG_SETUP_ACK_REQ: /* more information is needed */
		rc = osmo_cc_helper_audio_negotiate(msg, &call->cc_session, &call->codec);
		if (rc < 0) {
			release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
			break;
		}
		PDEBUG(DTEL, DEBUG_INFO, "Incoming call acknowledged\n");
		/* change state */
		call_new_state(call, CALL_STATE_OUT_OVERLAP);
		break;
	case OSMO_CC_MSG_PROC_REQ: /* call of endpoint is proceeding */
		rc = osmo_cc_helper_audio_negotiate(msg, &call->cc_session, &call->codec);
		if (rc < 0) {
			release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
			break;
		}
		PDEBUG(DTEL, DEBUG_INFO, "Incoming call proceeding\n");
		/* change state */
		call_new_state(call, CALL_STATE_OUT_PROCEEDING);
		break;
	case OSMO_CC_MSG_ALERT_REQ: /* call of endpoint is ringing */
		rc = osmo_cc_helper_audio_negotiate(msg, &call->cc_session, &call->codec);
		if (rc < 0) {
			release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
			break;
		}
		PDEBUG(DTEL, DEBUG_INFO, "Incoming call alerting\n");
		/* change state */
		call_new_state(call, CALL_STATE_OUT_ALERTING);
		break;
	case OSMO_CC_MSG_SETUP_RSP: /* call of endpoint is connected */
		rc = osmo_cc_helper_audio_negotiate(msg, &call->cc_session, &call->codec);
		if (rc < 0) {
			release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
			break;
		}
		PDEBUG(DTEL, DEBUG_INFO, "Incoming call acknowledged\n");
		/* create osmo-cc message */
		new_msg = osmo_cc_new_msg(OSMO_CC_MSG_SETUP_COMP_IND);
		/* send message to osmo-cc */
		osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, new_msg);
		/* change state */
		call_new_state(call, CALL_STATE_CONNECT);
		break;
	case OSMO_CC_MSG_SETUP_COMP_REQ: /* call of endpoint is connected */
		break;
	case OSMO_CC_MSG_INFO_REQ: /* overlap dialing */
		rc = osmo_cc_get_ie_called(msg, 0, &type, &plan, dialing, sizeof(dialing));
		if (rc < 0)
			dialing[0] = '\0';
		PDEBUG(DTEL, DEBUG_INFO, "Incoming call received additional dialing '%s'\n", dialing);
		for (i = 0; dialing[i]; i++) {
			/* add to dial string */
			append_string(ui_remote_dialing, sizeof(ui_remote_dialing), dialing[i]);
		}
		break;
	case OSMO_CC_MSG_PROGRESS_REQ: /* progress */
		rc = osmo_cc_helper_audio_negotiate(msg, &call->cc_session, &call->codec);
		if (rc < 0) {
			release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
			break;
		}
		PDEBUG(DTEL, DEBUG_INFO, "Incoming call received progress\n");
		break;
	case OSMO_CC_MSG_NOTIFY_REQ: /* display and notifications */
		break;
	case OSMO_CC_MSG_REJ_REQ: /* call has been rejected */
		/* get cause */
		rc = osmo_cc_get_ie_cause(msg, 0, &location, &ui_cause, &sip_cause, &socket_cause);
		if (rc < 0)
			ui_cause = OSMO_CC_ISDN_CAUSE_NORM_CALL_CLEAR;
		else
			PDEBUG(DTEL, DEBUG_INFO, "Incoming call rejected: ISDN cause = %d, SIP cause = %d, socket cause = %d\n", ui_cause, sip_cause, socket_cause);
		/* change state */
		call_new_state(call, CALL_STATE_IDLE);
		/* destroy call */
		call_destroy(call);
		break;
	case OSMO_CC_MSG_DISC_REQ: /* call has been disconnected */
		rc = osmo_cc_helper_audio_negotiate(msg, &call->cc_session, &call->codec);
		if (rc < 0) {
			release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
			break;
		}
		/* get cause */
		rc = osmo_cc_get_ie_cause(msg, 0, &location, &ui_cause, &sip_cause, &socket_cause);
		if (rc < 0)
			ui_cause = OSMO_CC_ISDN_CAUSE_NORM_CALL_CLEAR;
		else
			PDEBUG(DTEL, DEBUG_INFO, "Disconnect by remote: ISDN cause = %d, SIP cause = %d, socket cause = %d\n", ui_cause, sip_cause, socket_cause);
		/* change state */
		call_new_state(call, CALL_STATE_IN_DISCONNECT);
		/* progress indicator */
		rc = osmo_cc_get_ie_progress(msg, 0, &coding, &location, &progress);
		if (rc < 0 || coding != OSMO_CC_CODING_ITU_T || !(progress == 1 || progress == 8)) {
			PDEBUG(DTEL, DEBUG_INFO, "no audio after disconnect, releasing!\n");
			release_reject_ind(call, OSMO_CC_MSG_REL_IND, ui_cause);
		}

		break;
	case OSMO_CC_MSG_REL_REQ: /* release call */
		/* get cause */
		rc = osmo_cc_get_ie_cause(msg, 0, &location, &ui_cause, &sip_cause, &socket_cause);
		if (rc < 0)
			ui_cause = OSMO_CC_ISDN_CAUSE_NORM_CALL_CLEAR;
		else
			PDEBUG(DTEL, DEBUG_INFO, "Disconnect by remote: ISDN cause = %d, SIP cause = %d, socket cause = %d\n", ui_cause, sip_cause, socket_cause);
		/* create osmo-cc message */
		new_msg = osmo_cc_new_msg(OSMO_CC_MSG_REL_CNF);
		/* send message to osmo-cc */
		osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, new_msg);
		/* change state */
		call_new_state(call, CALL_STATE_IDLE);
		/* destroy call */
		call_destroy(call);
		break;
	default:
		PDEBUG(DTEL, DEBUG_ERROR, "received an unsupported CC message: %d\n", msg->type);
	}

done:
	osmo_cc_free_msg(msg);
}

/*
 * user interface
 */


static void _clear_ui_text(void)
{
	if (!ui_len)
		return;

	fwrite(ui_clear, ui_len, 1, stdout);
	// note: fflused by user of this function
	ui_len = 0;
}

static void _print_ui_text(void)
{
	if (!ui_len)
		return;

	printf("\033[1;37m");
	fwrite(ui_text, ui_len, 1, stdout);
	printf("\033[0;39m");
}

int ui_init(const char *remote_id, int autoalert, int autoanswer)
{
	clear_console_text = _clear_ui_text;
	print_console_text = _print_ui_text;
	ui_autoalert = autoalert;
	ui_autoanswer = autoanswer;

	strncpy(ui_remote_id, remote_id, sizeof(ui_remote_id) - 1);
	ui_remote_id[sizeof(ui_remote_id) - 1] = '\0';

	return 0;
}

int ui_work(telephone_t *telephone_ep, int c)
{
	int work = (c > 0) ? 1 : 0;
	call_t *call;
	osmo_cc_msg_t *msg;
	char text[1024] = "";
	char display[UI_MAX_DIGITS + 1];
	int len;

	/* hunt for call */
	for (call = telephone_ep->call_list; call; call = call->next)
		break; // just any call
	if (!call) {
		if (c > 0) {
			append_string(ui_remote_id, sizeof(ui_remote_id), c);
			if ((c == 8 || c == 127) && strlen(ui_remote_id))
				ui_remote_id[strlen(ui_remote_id) - 1] = '\0';
dial_after_hangup:
			if (c == 'd') {
				PDEBUG(DTEL, DEBUG_INFO, "Outgoing call from '%s' to '%s'\n", ui_local_id, ui_remote_id);
				ui_remote_dialing[0] = '\0';
				/* creating call instance */
				call = call_create(telephone_ep);
				if (!call) {
					PDEBUG(DTEL, DEBUG_ERROR, "Cannot create calll instance.\n");
					abort();
				}

				/* setup message */
				msg = osmo_cc_new_msg(OSMO_CC_MSG_SETUP_IND);
				/* network type */
				osmo_cc_add_ie_calling_network(msg, OSMO_CC_NETWORK_ALSA_NONE, "");
				/* calling number */
				osmo_cc_add_ie_calling(msg, OSMO_CC_TYPE_UNKNOWN, OSMO_CC_PLAN_TELEPHONY, OSMO_CC_PRESENT_ALLOWED, OSMO_CC_SCREEN_USER_UNSCREENED, ui_local_id);
				/* called number */
				osmo_cc_add_ie_called(msg, OSMO_CC_TYPE_UNKNOWN, OSMO_CC_PLAN_TELEPHONY, ui_remote_id);
				/* bearer capability */
				osmo_cc_add_ie_bearer(msg, OSMO_CC_CODING_ITU_T, OSMO_CC_CAPABILITY_AUDIO, OSMO_CC_MODE_CIRCUIT);
				/* sdp offer */
				call->cc_session = osmo_cc_helper_audio_offer(call, codecs, down_audio, msg, 1);
				if (!call->cc_session) {
					PDEBUG(DTEL, DEBUG_NOTICE, "Failed to offer audio, call aborted.\n");
					osmo_cc_free_msg(msg);
					call_destroy(call);
				} else {
					/* create new call */
					osmo_cc_call_t *cc_call = osmo_cc_call_new(&call->telephone_ep->cc_ep);
					call->cc_callref = cc_call->callref;
					/* send message to CC */
					osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, msg);
					/* change state */
					call_new_state(call, CALL_STATE_OUT_SETUP);
				}
			}
		}
		memset(display, '.', UI_MAX_DIGITS);
		memcpy(display, ui_remote_id, strlen(ui_remote_id));
		display[UI_MAX_DIGITS] = '\0';
		sprintf(text, "%s: %s (press digits 0..9 or d=dial)\r", call_state_name[(call) ? call->state : CALL_STATE_IDLE], display);
		goto done;
	}

	if (c == 'h' || (c == 'd' && call->state == CALL_STATE_IN_DISCONNECT)) {
		PDEBUG(DTEL, DEBUG_INFO, "Call hangup\n");
		/* create osmo-cc message */
		if (call->state == CALL_STATE_IN_SETUP)
			release_reject_ind(call, OSMO_CC_MSG_REJ_IND, OSMO_CC_ISDN_CAUSE_NORM_CALL_CLEAR);
		else
			release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_NORM_CALL_CLEAR);
		/* dial new number */
		if (c == 'd') {
			call = NULL;
			goto dial_after_hangup;
		}
		goto done;
	}
	if (c == 'o' && call->state == CALL_STATE_IN_SETUP) {
		PDEBUG(DTEL, DEBUG_INFO, "Acknowledge incoming call\n");
		/* create osmo-cc message */
		msg = osmo_cc_new_msg(OSMO_CC_MSG_SETUP_ACK_IND);
		if (call->telephone_ep->early_audio && call->sdp) {
			/* progress */
			osmo_cc_add_ie_progress(msg, OSMO_CC_CODING_ITU_T, call->telephone_ep->serving_location, OSMO_CC_PROGRESS_INBAND_INFO_AVAILABLE);
			/* sdp */
			osmo_cc_add_ie_sdp(msg, call->sdp);
			free((char *)call->sdp);
			call->sdp = NULL;
		}
		/* send message to osmo-cc */
		osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, msg);
		/* change state */
		call_new_state(call, CALL_STATE_IN_OVERLAP);
	}
	if (c == 'p' && (call->state == CALL_STATE_IN_SETUP || call->state == CALL_STATE_IN_OVERLAP)) {
		PDEBUG(DTEL, DEBUG_INFO, "Proceeding incoming call\n");
		/* create osmo-cc message */
		msg = osmo_cc_new_msg(OSMO_CC_MSG_PROC_IND);
		if (call->telephone_ep->early_audio && call->sdp) {
			/* progress */
			osmo_cc_add_ie_progress(msg, OSMO_CC_CODING_ITU_T, call->telephone_ep->serving_location, OSMO_CC_PROGRESS_INBAND_INFO_AVAILABLE);
			/* sdp */
			osmo_cc_add_ie_sdp(msg, call->sdp);
			free((char *)call->sdp);
			call->sdp = NULL;
		}
		/* send message to osmo-cc */
		osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, msg);
		/* change state */
		call_new_state(call, CALL_STATE_IN_PROCEEDING);
	}
	if (c == 'a' && (call->state == CALL_STATE_IN_SETUP || call->state == CALL_STATE_IN_OVERLAP || call->state == CALL_STATE_IN_PROCEEDING)) {
		PDEBUG(DTEL, DEBUG_INFO, "Alerting incoming call\n");
		/* create osmo-cc message */
		msg = osmo_cc_new_msg(OSMO_CC_MSG_ALERT_IND);
		if (call->telephone_ep->early_audio && call->sdp) {
			/* progress */
			osmo_cc_add_ie_progress(msg, OSMO_CC_CODING_ITU_T, call->telephone_ep->serving_location, OSMO_CC_PROGRESS_INBAND_INFO_AVAILABLE);
			/* sdp */
			osmo_cc_add_ie_sdp(msg, call->sdp);
			free((char *)call->sdp);
			call->sdp = NULL;
		}
		/* send message to osmo-cc */
		osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, msg);
		/* change state */
		call_new_state(call, CALL_STATE_IN_ALERTING);
	}
	if (c == 'c' && (call->state == CALL_STATE_IN_SETUP || call->state == CALL_STATE_IN_OVERLAP || call->state == CALL_STATE_IN_PROCEEDING || call->state == CALL_STATE_IN_ALERTING)) {
		PDEBUG(DTEL, DEBUG_INFO, "Answer incoming call\n");
		/* create osmo-cc message */
		msg = osmo_cc_new_msg(OSMO_CC_MSG_SETUP_CNF);
		/* sdp */
		if (call->sdp) {
			osmo_cc_add_ie_sdp(msg, call->sdp);
			free((char *)call->sdp);
			call->sdp = NULL;
		}
		/* send message to osmo-cc */
		osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, msg);
		/* change state */
		call_new_state(call, CALL_STATE_CONNECT);
	}
	if (c > 0 && call->state == CALL_STATE_OUT_OVERLAP && strchr(ui_digits, c)) {
		PDEBUG(DTEL, DEBUG_INFO, "Send digit %c\n", c);
		char called[] = { c, '\0' };
		/* create osmo-cc message */
		msg = osmo_cc_new_msg(OSMO_CC_MSG_INFO_IND);
		/* add dialing */
		osmo_cc_add_ie_called(msg, OSMO_CC_TYPE_UNKNOWN, OSMO_CC_PLAN_TELEPHONY, called);
		/* send message to osmo-cc */
		osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, msg);
		/* add to dial string */
		append_string(ui_remote_id, sizeof(ui_remote_id), c);
	}

	/* NOTE: the state is from console view: a call towards CC is OUT and from CC is IN */
	switch (call->state) {
	case CALL_STATE_OUT_SETUP:
	case CALL_STATE_OUT_PROCEEDING:
	case CALL_STATE_OUT_ALERTING:
		sprintf(text, "%s: %s (press h=hangup)\r", call_state_name[call->state], ui_remote_id);
		break;
	case CALL_STATE_OUT_OVERLAP:
		sprintf(text, "%s: %s (press digits 0..9 h=hangup)\r", call_state_name[call->state], ui_remote_id);
		break;
	case CALL_STATE_CONNECT:
		if (ui_remote_dialing[0])
			sprintf(text, "%s: %s->%s (press h=hangup)\r", call_state_name[call->state], ui_remote_id, ui_remote_dialing);
		else
			sprintf(text, "%s: %s (press h=hangup)\r", call_state_name[call->state], ui_remote_id);
		break;
	case CALL_STATE_IN_DISCONNECT:
		sprintf(text, "%s: %s (press h=hangup d=redial)\r", call_state_name[call->state], cause_name(ui_cause));
		break;
	case CALL_STATE_IN_SETUP:
		sprintf(text, "%s: %s->%s (press o=overlap p=proceeding a=alerting c=connect h=hangup)\r", call_state_name[call->state], ui_remote_id, ui_remote_dialing);
		break;
	case CALL_STATE_IN_OVERLAP:
		sprintf(text, "%s: %s->%s (press p=proceeding a=alerting c=connect h=hangup)\r", call_state_name[call->state], ui_remote_id, ui_remote_dialing);
		break;
	case CALL_STATE_IN_PROCEEDING:
		sprintf(text, "%s: %s->%s (press a=alerting c=connect h=hangup)\r", call_state_name[call->state], ui_remote_id, ui_remote_dialing);
		break;
	case CALL_STATE_IN_ALERTING:
		sprintf(text, "%s: %s->%s (press c=connect h=hangup)\r", call_state_name[call->state], ui_remote_id, ui_remote_dialing);
		break;
	default:
		break;
	}

done:
	/* skip if nothing has changed */
	len = strlen(text);
	if (ui_len == len && !memcmp(ui_text, text, len))
		return work;
	clear_console_text();
	ui_len = len;
	memcpy(ui_text, text, len);
	memset(ui_clear, ' ', len - 1);
	ui_clear[len - 1] = '\r';
	print_console_text();
	fflush(stdout);

	return work;
}