2
* libpri: An implementation of Primary Rate ISDN
4
* Written by Mark Spencer <markster@digium.com>
6
* Copyright (C) 2001-2005, Digium, Inc.
11
* See http://www.asterisk.org for more information about
12
* the Asterisk project. Please do not directly contact
13
* any of the maintainers of this project for assistance;
14
* the project provides a web site, mailing lists and IRC
15
* channels for your use.
17
* This program is free software, distributed under the terms of
18
* the GNU General Public License Version 2 as published by the
19
* Free Software Foundation. See the LICENSE file included with
20
* this program for more details.
22
* In addition, when this program is distributed with Asterisk in
23
* any form that would qualify as a 'combined work' or as a
24
* 'derivative work' (but not mere aggregation), you can redistribute
25
* and/or modify the combination under the terms of the license
26
* provided with that copy of Asterisk, instead of the license
37
#include <sys/select.h>
41
#include "pri_internal.h"
42
#include "pri_facility.h"
46
#define PRI_BIT(a_bit) (1UL << (a_bit))
47
#define PRI_ALL_SWITCHES 0xFFFFFFFF
49
struct pri_timer_table {
51
enum PRI_TIMERS_AND_COUNTERS number;
52
unsigned long used_by;
56
* \note Sort the timer table entries in the order of the timer name so
57
* pri_dump_info_str() can display them in a consistent order.
59
static const struct pri_timer_table pri_timer[] = {
61
/* timer name timer number used by switches */
62
{ "N200", PRI_TIMER_N200, PRI_ALL_SWITCHES },
63
{ "N201", PRI_TIMER_N201, PRI_ALL_SWITCHES },
64
{ "N202", PRI_TIMER_N202, PRI_ALL_SWITCHES },
65
{ "K", PRI_TIMER_K, PRI_ALL_SWITCHES },
66
{ "T200", PRI_TIMER_T200, PRI_ALL_SWITCHES },
67
{ "T202", PRI_TIMER_T202, PRI_ALL_SWITCHES },
68
{ "T203", PRI_TIMER_T203, PRI_ALL_SWITCHES },
69
{ "T300", PRI_TIMER_T300, PRI_ALL_SWITCHES },
70
{ "T301", PRI_TIMER_T301, PRI_ALL_SWITCHES },
71
{ "T302", PRI_TIMER_T302, PRI_ALL_SWITCHES },
72
{ "T303", PRI_TIMER_T303, PRI_ALL_SWITCHES },
73
{ "T304", PRI_TIMER_T304, PRI_ALL_SWITCHES },
74
{ "T305", PRI_TIMER_T305, PRI_ALL_SWITCHES },
75
{ "T306", PRI_TIMER_T306, PRI_ALL_SWITCHES },
76
{ "T307", PRI_TIMER_T307, PRI_ALL_SWITCHES },
77
{ "T308", PRI_TIMER_T308, PRI_ALL_SWITCHES },
78
{ "T309", PRI_TIMER_T309, PRI_ALL_SWITCHES },
79
{ "T310", PRI_TIMER_T310, PRI_ALL_SWITCHES },
80
{ "T313", PRI_TIMER_T313, PRI_ALL_SWITCHES },
81
{ "T314", PRI_TIMER_T314, PRI_ALL_SWITCHES },
82
{ "T316", PRI_TIMER_T316, PRI_ALL_SWITCHES },
83
{ "T317", PRI_TIMER_T317, PRI_ALL_SWITCHES },
84
{ "T318", PRI_TIMER_T318, PRI_ALL_SWITCHES },
85
{ "T319", PRI_TIMER_T319, PRI_ALL_SWITCHES },
86
{ "T320", PRI_TIMER_T320, PRI_ALL_SWITCHES },
87
{ "T321", PRI_TIMER_T321, PRI_ALL_SWITCHES },
88
{ "T322", PRI_TIMER_T322, PRI_ALL_SWITCHES },
89
{ "T-HOLD", PRI_TIMER_T_HOLD, PRI_ALL_SWITCHES },
90
{ "T-RETRIEVE", PRI_TIMER_T_RETRIEVE, PRI_ALL_SWITCHES },
91
{ "T-RESPONSE", PRI_TIMER_T_RESPONSE, PRI_ALL_SWITCHES },
95
char *pri_node2str(int node)
99
return "Unknown node type";
105
return "Invalid value";
109
char *pri_switch2str(int sw)
113
return "National ISDN";
114
case PRI_SWITCH_DMS100:
115
return "Nortel DMS100";
116
case PRI_SWITCH_LUCENT5E:
118
case PRI_SWITCH_ATT4ESS:
121
return "National ISDN 1";
122
case PRI_SWITCH_EUROISDN_E1:
124
case PRI_SWITCH_GR303_EOC:
126
case PRI_SWITCH_GR303_TMC:
128
case PRI_SWITCH_QSIG:
129
return "Q.SIG switch";
131
return "Unknown switchtype";
135
static void pri_default_timers(struct pri *ctrl, int switchtype)
139
/* Initialize all timers/counters to unsupported/disabled. */
140
for (idx = 0; idx < PRI_MAX_TIMERS; ++idx) {
141
ctrl->timers[idx] = -1;
144
/* Set timer values to standard defaults. Time is in ms. */
145
ctrl->timers[PRI_TIMER_N200] = 3; /* Max numer of Q.921 retransmissions */
146
ctrl->timers[PRI_TIMER_N202] = 3; /* Max numer of transmissions of the TEI identity request message */
149
ctrl->timers[PRI_TIMER_K] = 1; /* Max number of outstanding I-frames */
151
ctrl->timers[PRI_TIMER_K] = 7; /* Max number of outstanding I-frames */
153
ctrl->timers[PRI_TIMER_T200] = 1000; /* Time between SABME's */
154
ctrl->timers[PRI_TIMER_T202] = 10 * 1000; /* Min time between transmission of TEI Identity request messages */
155
ctrl->timers[PRI_TIMER_T203] = 10 * 1000; /* Max time without exchanging packets */
156
ctrl->timers[PRI_TIMER_T305] = 30 * 1000; /* Wait for DISCONNECT acknowledge */
157
ctrl->timers[PRI_TIMER_T308] = 4 * 1000; /* Wait for RELEASE acknowledge */
158
ctrl->timers[PRI_TIMER_T313] = 4 * 1000; /* Wait for CONNECT acknowledge, CPE side only */
159
ctrl->timers[PRI_TIMER_TM20] = 2500; /* Max time awaiting XID response - Q.921 Appendix IV */
160
ctrl->timers[PRI_TIMER_NM20] = 3; /* Number of XID retransmits - Q.921 Appendix IV */
161
ctrl->timers[PRI_TIMER_T303] = 4 * 1000; /* Length between SETUP retransmissions and timeout */
162
ctrl->timers[PRI_TIMER_T309] = 6000; /* Time to wait before clearing calls in case of D-channel transient event. Q.931 specifies 6-90 seconds */
164
ctrl->timers[PRI_TIMER_T_HOLD] = 4 * 1000; /* Wait for HOLD request response. */
165
ctrl->timers[PRI_TIMER_T_RETRIEVE] = 4 * 1000;/* Wait for RETRIEVE request response. */
167
ctrl->timers[PRI_TIMER_T_RESPONSE] = 4 * 1000; /* Maximum time to wait for a typical APDU response. */
169
/* Set any switch specific override default values */
170
switch (switchtype) {
176
int pri_set_timer(struct pri *pri, int timer, int value)
178
if (timer < 0 || timer > PRI_MAX_TIMERS || value < 0)
181
pri->timers[timer] = value;
185
int pri_get_timer(struct pri *pri, int timer)
187
if (timer < 0 || timer > PRI_MAX_TIMERS)
189
return pri->timers[timer];
192
int pri_set_service_message_support(struct pri *pri, int supportflag)
197
pri->service_message_support = supportflag ? 1 : 0;
201
int pri_timer2idx(const char *timer_name)
204
enum PRI_TIMERS_AND_COUNTERS timer_number;
207
for (idx = 0; idx < ARRAY_LEN(pri_timer); ++idx) {
208
if (!strcasecmp(timer_name, pri_timer[idx].name)) {
209
timer_number = pri_timer[idx].number;
216
static int __pri_read(struct pri *pri, void *buf, int buflen)
218
int res = read(pri->fd, buf, buflen);
221
pri_error(pri, "Read on %d failed: %s\n", pri->fd, strerror(errno));
227
static int __pri_write(struct pri *pri, void *buf, int buflen)
229
int res = write(pri->fd, buf, buflen);
232
pri_error(pri, "Write to %d failed: %s\n", pri->fd, strerror(errno));
238
void __pri_free_tei(struct pri * p)
241
struct q931_call *call;
243
call = p->dummy_call;
245
pri_schedule_del(call->pri, call->retranstimer);
246
pri_call_apdu_queue_cleanup(call);
253
struct pri *__pri_new_tei(int fd, int node, int switchtype, struct pri *master, pri_io_cb rd, pri_io_cb wr, void *userdata, int tei, int bri)
255
struct d_ctrl_dummy *dummy_ctrl;
258
switch (switchtype) {
259
case PRI_SWITCH_GR303_EOC:
260
case PRI_SWITCH_GR303_TMC:
261
case PRI_SWITCH_GR303_TMC_SWITCHING:
262
case PRI_SWITCH_GR303_EOC_PATH:
263
p = calloc(1, sizeof(*p));
270
dummy_ctrl = calloc(1, sizeof(*dummy_ctrl));
274
p = &dummy_ctrl->ctrl;
278
/* This is the master record. */
279
p->msg_line = calloc(1, sizeof(*p->msg_line));
290
p->userdata = userdata;
292
p->switchtype = switchtype;
294
p->sapi = (tei == Q921_TEI_GROUP) ? Q921_SAPI_LAYER2_MANAGEMENT : Q921_SAPI_CALL_CTRL;
296
p->nsf = PRI_NSF_NONE;
297
p->protodisc = Q931_PROTOCOL_DISCRIMINATOR;
299
p->callpool = &p->localpool;
300
pri_default_timers(p, switchtype);
302
pri_set_debug(p, master->debug);
303
if (master->sendfacility)
304
pri_facility_enable(p);
306
#ifdef LIBPRI_COUNTERS
313
/* Initialize the dummy call reference call record. */
314
dummy_ctrl->ctrl.dummy_call = &dummy_ctrl->dummy_call;
315
q931_init_call_record(&dummy_ctrl->ctrl, dummy_ctrl->ctrl.dummy_call,
316
Q931_DUMMY_CALL_REFERENCE);
318
switch (switchtype) {
319
case PRI_SWITCH_GR303_EOC:
320
p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
321
p->sapi = Q921_SAPI_GR303_EOC;
322
p->tei = Q921_TEI_GR303_EOC_OPS;
323
p->subchannel = __pri_new_tei(-1, node, PRI_SWITCH_GR303_EOC_PATH, p, NULL, NULL, NULL, Q921_TEI_GR303_EOC_PATH, 0);
324
if (!p->subchannel) {
329
case PRI_SWITCH_GR303_TMC:
330
p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
331
p->sapi = Q921_SAPI_GR303_TMC_CALLPROC;
332
p->tei = Q921_TEI_GR303_TMC_CALLPROC;
333
p->subchannel = __pri_new_tei(-1, node, PRI_SWITCH_GR303_TMC_SWITCHING, p, NULL, NULL, NULL, Q921_TEI_GR303_TMC_SWITCHING, 0);
334
if (!p->subchannel) {
339
case PRI_SWITCH_GR303_TMC_SWITCHING:
340
p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
341
p->sapi = Q921_SAPI_GR303_TMC_SWITCHING;
342
p->tei = Q921_TEI_GR303_TMC_SWITCHING;
344
case PRI_SWITCH_GR303_EOC_PATH:
345
p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
346
p->sapi = Q921_SAPI_GR303_EOC;
347
p->tei = Q921_TEI_GR303_EOC_PATH;
353
if (p->tei == Q921_TEI_GROUP && p->sapi == Q921_SAPI_LAYER2_MANAGEMENT && p->localtype == PRI_CPE) {
354
p->subchannel = __pri_new_tei(-1, p->localtype, p->switchtype, p, NULL, NULL, NULL, Q921_TEI_PRI, 1);
355
if (!p->subchannel) {
365
void pri_call_set_useruser(q931_call *c, const char *userchars)
368
libpri_copy_string(c->useruserinfo, userchars, sizeof(c->useruserinfo));
371
void pri_sr_set_useruser(struct pri_sr *sr, const char *userchars)
373
sr->useruserinfo = userchars;
376
int pri_restart(struct pri *pri)
378
/* pri_restart() is no longer needed since the Q.921 rewrite. */
380
/* Restart Q.921 layer */
383
q921_start(pri, pri->localtype == PRI_CPE);
389
struct pri *pri_new(int fd, int nodetype, int switchtype)
391
return __pri_new_tei(fd, nodetype, switchtype, NULL, __pri_read, __pri_write, NULL, Q921_TEI_PRI, 0);
394
struct pri *pri_new_bri(int fd, int ptpmode, int nodetype, int switchtype)
397
return __pri_new_tei(fd, nodetype, switchtype, NULL, __pri_read, __pri_write, NULL, Q921_TEI_PRI, 1);
399
return __pri_new_tei(fd, nodetype, switchtype, NULL, __pri_read, __pri_write, NULL, Q921_TEI_GROUP, 1);
402
struct pri *pri_new_cb(int fd, int nodetype, int switchtype, pri_io_cb io_read, pri_io_cb io_write, void *userdata)
405
io_read = __pri_read;
407
io_write = __pri_write;
408
return __pri_new_tei(fd, nodetype, switchtype, NULL, io_read, io_write, userdata, Q921_TEI_PRI, 0);
411
void *pri_get_userdata(struct pri *pri)
413
return pri ? pri->userdata : NULL;
416
void pri_set_userdata(struct pri *pri, void *userdata)
419
pri->userdata = userdata;
422
void pri_set_nsf(struct pri *pri, int nsf)
428
char *pri_event2str(int id)
436
{ PRI_EVENT_DCHAN_UP, "D-Channel Up" },
437
{ PRI_EVENT_DCHAN_DOWN, "D-channel Down" },
438
{ PRI_EVENT_RESTART, "Restart channel" },
439
{ PRI_EVENT_CONFIG_ERR, "Configuration Error" },
440
{ PRI_EVENT_RING, "Ring" },
441
{ PRI_EVENT_HANGUP, "Hangup" },
442
{ PRI_EVENT_RINGING, "Ringing" },
443
{ PRI_EVENT_ANSWER, "Answer" },
444
{ PRI_EVENT_HANGUP_ACK, "Hangup ACK" },
445
{ PRI_EVENT_RESTART_ACK, "Restart ACK" },
446
{ PRI_EVENT_FACILITY, "Facility" },
447
{ PRI_EVENT_INFO_RECEIVED, "Info Received" },
448
{ PRI_EVENT_PROCEEDING, "Proceeding" },
449
{ PRI_EVENT_SETUP_ACK, "Setup ACK" },
450
{ PRI_EVENT_HANGUP_REQ, "Hangup Req" },
451
{ PRI_EVENT_NOTIFY, "Notify" },
452
{ PRI_EVENT_PROGRESS, "Progress" },
453
{ PRI_EVENT_KEYPAD_DIGIT, "Keypad Digit" },
454
{ PRI_EVENT_SERVICE, "Service" },
455
{ PRI_EVENT_SERVICE_ACK, "Service ACK" },
456
{ PRI_EVENT_HOLD, "Hold" },
457
{ PRI_EVENT_HOLD_ACK, "Hold Ack" },
458
{ PRI_EVENT_HOLD_REJ, "Hold Rej" },
459
{ PRI_EVENT_RETRIEVE, "Retrieve" },
460
{ PRI_EVENT_RETRIEVE_ACK, "Retrieve ACK" },
461
{ PRI_EVENT_RETRIEVE_REJ, "Retrieve Rej" },
465
for (idx = 0; idx < ARRAY_LEN(events); ++idx) {
466
if (events[idx].id == id) {
467
return events[idx].name;
470
return "Unknown Event";
473
pri_event *pri_check_event(struct pri *pri)
478
res = pri->read_func ? pri->read_func(pri, buf, sizeof(buf)) : 0;
481
/* Receive the q921 packet */
482
e = q921_receive(pri, (q921_h *)buf, res);
486
static int wait_pri(struct pri *pri)
488
struct timeval *tv, real;
492
FD_SET(pri->fd, &fds);
493
tv = pri_schedule_next(pri);
495
gettimeofday(&real, NULL);
496
real.tv_sec = tv->tv_sec - real.tv_sec;
497
real.tv_usec = tv->tv_usec - real.tv_usec;
498
if (real.tv_usec < 0) {
499
real.tv_usec += 1000000;
502
if (real.tv_sec < 0) {
507
res = select(pri->fd + 1, &fds, NULL, NULL, tv ? &real : tv);
513
pri_event *pri_mkerror(struct pri *pri, char *errstr)
515
/* Return a configuration error */
516
pri->ev.err.e = PRI_EVENT_CONFIG_ERR;
517
libpri_copy_string(pri->ev.err.err, errstr, sizeof(pri->ev.err.err));
522
pri_event *pri_dchannel_run(struct pri *pri, int block)
532
/* Check for error / interruption */
536
e = pri_schedule_run(pri);
538
e = pri_check_event(pri);
541
e = pri_check_event(pri);
547
void pri_set_debug(struct pri *pri, int debug)
553
pri_set_debug(pri->subchannel, debug);
556
int pri_get_debug(struct pri *pri)
561
return pri_get_debug(pri->subchannel);
565
void pri_facility_enable(struct pri *pri)
569
pri->sendfacility = 1;
571
pri_facility_enable(pri->subchannel);
575
int pri_acknowledge(struct pri *pri, q931_call *call, int channel, int info)
579
return q931_alerting(pri, call, channel, info);
582
int pri_proceeding(struct pri *pri, q931_call *call, int channel, int info)
586
return q931_call_proceeding(pri, call, channel, info);
589
int pri_progress_with_cause(struct pri *pri, q931_call *call, int channel, int info, int cause)
594
return q931_call_progress_with_cause(pri, call, channel, info, cause);
597
int pri_progress(struct pri *pri, q931_call *call, int channel, int info)
602
return q931_call_progress(pri, call, channel, info);
605
int pri_information(struct pri *pri, q931_call *call, char digit)
609
return q931_information(pri, call, digit);
612
int pri_keypad_facility(struct pri *pri, q931_call *call, const char *digits)
614
if (!pri || !call || !digits || !digits[0])
617
return q931_keypad_facility(pri, call, digits);
620
int pri_notify(struct pri *pri, q931_call *call, int channel, int info)
624
return q931_notify(pri, call, channel, info);
627
void pri_destroycall(struct pri *pri, q931_call *call)
630
q931_destroycall(pri, call);
634
int pri_need_more_info(struct pri *pri, q931_call *call, int channel, int nonisdn)
638
return q931_setup_ack(pri, call, channel, nonisdn);
641
int pri_answer(struct pri *pri, q931_call *call, int channel, int nonisdn)
645
return q931_connect(pri, call, channel, nonisdn);
649
* \brief Copy the PRI party name to the Q.931 party name structure.
651
* \param q931_name Q.931 party name structure
652
* \param pri_name PRI party name structure
656
void pri_copy_party_name_to_q931(struct q931_party_name *q931_name, const struct pri_party_name *pri_name)
658
q931_party_name_init(q931_name);
659
if (pri_name->valid) {
660
q931_name->valid = 1;
661
q931_name->presentation = pri_name->presentation;
662
q931_name->char_set = pri_name->char_set;
663
libpri_copy_string(q931_name->str, pri_name->str, sizeof(q931_name->str));
668
* \brief Copy the PRI party number to the Q.931 party number structure.
670
* \param q931_number Q.931 party number structure
671
* \param pri_number PRI party number structure
675
void pri_copy_party_number_to_q931(struct q931_party_number *q931_number, const struct pri_party_number *pri_number)
677
q931_party_number_init(q931_number);
678
if (pri_number->valid) {
679
q931_number->valid = 1;
680
q931_number->presentation = pri_number->presentation;
681
q931_number->plan = pri_number->plan;
682
libpri_copy_string(q931_number->str, pri_number->str, sizeof(q931_number->str));
687
* \brief Copy the PRI party subaddress to the Q.931 party subaddress structure.
689
* \param q931_subaddress Q.931 party subaddress structure
690
* \param pri_subaddress PRI party subaddress structure
694
void pri_copy_party_subaddress_to_q931(struct q931_party_subaddress *q931_subaddress, const struct pri_party_subaddress *pri_subaddress)
697
int maxlen = sizeof(q931_subaddress->data) - 1;
699
q931_party_subaddress_init(q931_subaddress);
701
if (!pri_subaddress->valid) {
705
q931_subaddress->valid = 1;
706
q931_subaddress->type = pri_subaddress->type;
708
length = pri_subaddress->length;
709
if (length > maxlen){
712
q931_subaddress->odd_even_indicator = pri_subaddress->odd_even_indicator;
714
q931_subaddress->length = length;
715
memcpy(q931_subaddress->data, pri_subaddress->data, length);
716
q931_subaddress->data[length] = '\0';
720
* \brief Copy the PRI party id to the Q.931 party id structure.
722
* \param q931_id Q.931 party id structure
723
* \param pri_id PRI party id structure
727
void pri_copy_party_id_to_q931(struct q931_party_id *q931_id, const struct pri_party_id *pri_id)
729
pri_copy_party_name_to_q931(&q931_id->name, &pri_id->name);
730
pri_copy_party_number_to_q931(&q931_id->number, &pri_id->number);
731
pri_copy_party_subaddress_to_q931(&q931_id->subaddress, &pri_id->subaddress);
734
int pri_connected_line_update(struct pri *ctrl, q931_call *call, const struct pri_party_connected_line *connected)
736
struct q931_party_id party_id;
738
struct q931_call *subcall;
740
if (!ctrl || !call) {
744
pri_copy_party_id_to_q931(&party_id, &connected->id);
745
q931_party_id_fixup(ctrl, &party_id);
746
if (!q931_party_id_cmp(&party_id, &call->local_id)) {
747
/* The local party information did not change so do nothing. */
750
call->local_id = party_id;
752
/* Update all subcalls with new local_id. */
753
if (call->outboundbroadcast && call->master_call == call) {
754
for (idx = 0; idx < Q931_MAX_TEI; ++idx) {
755
subcall = call->subcalls[idx];
757
subcall->local_id = party_id;
762
switch (call->ourcallstate) {
763
case Q931_CALL_STATE_CALL_INITIATED:
764
case Q931_CALL_STATE_OVERLAP_SENDING:
765
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
766
case Q931_CALL_STATE_CALL_DELIVERED:
768
* The local party transferred to someone else before
769
* the remote end answered.
771
case Q931_CALL_STATE_ACTIVE:
772
switch (ctrl->switchtype) {
773
case PRI_SWITCH_EUROISDN_E1:
774
case PRI_SWITCH_EUROISDN_T1:
775
if (PTMP_MODE(ctrl)) {
777
q931_notify_redirection(ctrl, call, PRI_NOTIFY_TRANSFER_ACTIVE,
778
&call->local_id.number);
781
/* Immediately send EctInform APDU, callStatus=answered(0) */
782
send_call_transfer_complete(ctrl, call, 0);
785
case PRI_SWITCH_QSIG:
786
/* Immediately send CallTransferComplete APDU, callStatus=answered(0) */
787
send_call_transfer_complete(ctrl, call, 0);
794
/* Just save the data for further developments. */
801
int pri_redirecting_update(struct pri *ctrl, q931_call *call, const struct pri_party_redirecting *redirecting)
804
struct q931_call *subcall;
806
if (!ctrl || !call) {
810
/* Save redirecting.to information and reason. */
811
pri_copy_party_id_to_q931(&call->redirecting.to, &redirecting->to);
812
q931_party_id_fixup(ctrl, &call->redirecting.to);
813
call->redirecting.reason = redirecting->reason;
816
* Update all subcalls with new redirecting.to information and reason.
817
* I do not think we will ever have any subcalls when this data is relevant,
818
* but update it just in case.
820
if (call->outboundbroadcast && call->master_call == call) {
821
for (idx = 0; idx < Q931_MAX_TEI; ++idx) {
822
subcall = call->subcalls[idx];
824
subcall->redirecting.to = call->redirecting.to;
825
subcall->redirecting.reason = redirecting->reason;
830
switch (call->ourcallstate) {
831
case Q931_CALL_STATE_NULL:
832
/* Save the remaining redirecting information before we place a call. */
833
pri_copy_party_id_to_q931(&call->redirecting.from, &redirecting->from);
834
q931_party_id_fixup(ctrl, &call->redirecting.from);
835
pri_copy_party_id_to_q931(&call->redirecting.orig_called, &redirecting->orig_called);
836
q931_party_id_fixup(ctrl, &call->redirecting.orig_called);
837
call->redirecting.orig_reason = redirecting->orig_reason;
838
if (redirecting->count <= 0) {
839
if (call->redirecting.from.number.valid) {
841
* We are redirecting with an unknown count
842
* so assume the count is one.
844
call->redirecting.count = 1;
846
call->redirecting.count = 0;
848
} else if (redirecting->count < PRI_MAX_REDIRECTS) {
849
call->redirecting.count = redirecting->count;
851
call->redirecting.count = PRI_MAX_REDIRECTS;
854
case Q931_CALL_STATE_OVERLAP_RECEIVING:
855
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
856
case Q931_CALL_STATE_CALL_RECEIVED:
857
/* This is an incoming call that has not connected yet. */
858
if (!call->redirecting.to.number.valid) {
859
/* Not being redirected toward valid number data. Ignore. */
863
switch (ctrl->switchtype) {
864
case PRI_SWITCH_EUROISDN_E1:
865
case PRI_SWITCH_EUROISDN_T1:
866
if (PTMP_MODE(ctrl)) {
868
q931_notify_redirection(ctrl, call, PRI_NOTIFY_CALL_DIVERTING,
869
&call->redirecting.to.number);
872
/* PTP mode - same behaviour as Q.SIG */
874
case PRI_SWITCH_QSIG:
875
if (call->redirecting.state != Q931_REDIRECTING_STATE_PENDING_TX_DIV_LEG_3
876
|| strcmp(call->redirecting.to.number.str, call->called.number.str) != 0) {
877
/* immediately send divertingLegInformation1 APDU */
878
if (rose_diverting_leg_information1_encode(ctrl, call)
879
|| q931_facility(ctrl, call)) {
881
"Could not schedule facility message for divertingLegInfo1\n");
884
call->redirecting.state = Q931_REDIRECTING_STATE_IDLE;
886
/* immediately send divertingLegInformation3 APDU */
887
if (rose_diverting_leg_information3_encode(ctrl, call, Q931_FACILITY)
888
|| q931_facility(ctrl, call)) {
890
"Could not schedule facility message for divertingLegInfo3\n");
898
pri_message(ctrl, "Ignored redirecting update because call in state %s(%d).\n",
899
q931_call_state_str(call->ourcallstate), call->ourcallstate);
907
/* deprecated routines, use pri_hangup */
908
int pri_release(struct pri *pri, q931_call *call, int cause)
912
return q931_release(pri, call, cause);
915
int pri_disconnect(struct pri *pri, q931_call *call, int cause)
919
return q931_disconnect(pri, call, cause);
923
int pri_channel_bridge(q931_call *call1, q931_call *call2)
925
if (!call1 || !call2)
928
/* Make sure we have compatible switchtypes */
929
if (call1->pri->switchtype != call2->pri->switchtype)
932
/* Check for bearer capability */
933
if (call1->transcapability != call2->transcapability)
936
/* Check to see if we're on the same PRI */
937
if (call1->pri != call2->pri)
940
switch (call1->pri->switchtype) {
942
case PRI_SWITCH_LUCENT5E:
943
case PRI_SWITCH_ATT4ESS:
944
if (eect_initiate_transfer(call1->pri, call1, call2))
949
case PRI_SWITCH_DMS100:
950
if (rlt_initiate_transfer(call1->pri, call1, call2))
955
case PRI_SWITCH_QSIG:
956
call1->bridged_call = call2;
957
call2->bridged_call = call1;
958
if (anfpr_initiate_transfer(call1->pri, call1, call2))
968
void pri_hangup_fix_enable(struct pri *ctrl, int enable)
971
ctrl = PRI_MASTER(ctrl);
972
ctrl->hangup_fix_enabled = enable ? 1 : 0;
976
int pri_hangup(struct pri *pri, q931_call *call, int cause)
981
/* normal clear cause */
982
cause = PRI_CAUSE_NORMAL_CLEARING;
983
return q931_hangup(pri, call, cause);
986
int pri_reset(struct pri *pri, int channel)
990
return q931_restart(pri, channel);
993
int pri_maintenance_service(struct pri *pri, int span, int channel, int changestatus)
998
return maintenance_service(pri, span, channel, changestatus);
1001
q931_call *pri_new_call(struct pri *pri)
1005
return q931_new_call(pri);
1008
int pri_is_dummy_call(q931_call *call)
1013
return q931_is_dummy_call(call);
1016
void pri_dump_event(struct pri *pri, pri_event *e)
1020
pri_message(pri, "Event type: %s (%d)\n", pri_event2str(e->gen.e), e->gen.e);
1022
case PRI_EVENT_DCHAN_UP:
1023
case PRI_EVENT_DCHAN_DOWN:
1025
case PRI_EVENT_CONFIG_ERR:
1026
pri_message(pri, "Error: %s", e->err.err);
1028
case PRI_EVENT_RESTART:
1029
pri_message(pri, "Restart on channel %d\n", e->restart.channel);
1030
case PRI_EVENT_RING:
1031
pri_message(pri, "Calling number: %s (%s, %s)\n", e->ring.callingnum, pri_plan2str(e->ring.callingplan), pri_pres2str(e->ring.callingpres));
1032
pri_message(pri, "Called number: %s (%s)\n", e->ring.callednum, pri_plan2str(e->ring.calledplan));
1033
pri_message(pri, "Channel: %d (%s) Reference number: %d\n", e->ring.channel, e->ring.flexible ? "Flexible" : "Not Flexible", e->ring.cref);
1035
case PRI_EVENT_HANGUP:
1036
pri_message(pri, "Hangup, reference number: %d, reason: %s\n", e->hangup.cref, pri_cause2str(e->hangup.cause));
1039
pri_message(pri, "Don't know how to dump events of type %d\n", e->gen.e);
1043
static void pri_sr_init(struct pri_sr *req)
1045
memset(req, 0, sizeof(struct pri_sr));
1046
q931_party_redirecting_init(&req->redirecting);
1047
q931_party_id_init(&req->caller);
1048
q931_party_address_init(&req->called);
1049
req->reversecharge = PRI_REVERSECHARGE_NONE;
1052
int pri_sr_set_connection_call_independent(struct pri_sr *req)
1057
req->cis_call = 1; /* have to set cis_call for all those pesky IEs we need to setup */
1058
req->cis_auto_disconnect = 1;
1062
int pri_sr_set_no_channel_call(struct pri_sr *req)
1072
/* Don't call any other pri functions on this */
1073
int pri_mwi_activate(struct pri *pri, q931_call *c, char *caller, int callerplan, char *callername, int callerpres, char *called,
1081
pri_sr_set_connection_call_independent(&req);
1082
pri_sr_set_caller(&req, caller, callername, callerplan, callerpres);
1083
pri_sr_set_called(&req, called, calledplan, 0);
1085
if (mwi_message_send(pri, c, &req, 1) < 0) {
1086
pri_message(pri, "Unable to send MWI activate message\n");
1089
/* Do more stuff when we figure out that the CISC stuff works */
1090
return q931_setup(pri, c, &req);
1093
int pri_mwi_deactivate(struct pri *pri, q931_call *c, char *caller, int callerplan, char *callername, int callerpres, char *called,
1101
pri_sr_set_connection_call_independent(&req);
1102
pri_sr_set_caller(&req, caller, callername, callerplan, callerpres);
1103
pri_sr_set_called(&req, called, calledplan, 0);
1105
if(mwi_message_send(pri, c, &req, 0) < 0) {
1106
pri_message(pri, "Unable to send MWI deactivate message\n");
1110
return q931_setup(pri, c, &req);
1113
int pri_setup(struct pri *pri, q931_call *c, struct pri_sr *req)
1118
return q931_setup(pri, c, req);
1121
int pri_call(struct pri *pri, q931_call *c, int transmode, int channel, int exclusive,
1122
int nonisdn, char *caller, int callerplan, char *callername, int callerpres, char *called,
1123
int calledplan, int ulayer1)
1129
pri_sr_set_caller(&req, caller, callername, callerplan, callerpres);
1130
pri_sr_set_called(&req, called, calledplan, 0);
1131
req.transmode = transmode;
1132
req.channel = channel;
1133
req.exclusive = exclusive;
1134
req.nonisdn = nonisdn;
1135
req.userl1 = ulayer1;
1136
return q931_setup(pri, c, &req);
1139
static void (*__pri_error)(struct pri *pri, char *stuff);
1140
static void (*__pri_message)(struct pri *pri, char *stuff);
1142
void pri_set_message(void (*func)(struct pri *pri, char *stuff))
1144
__pri_message = func;
1147
void pri_set_error(void (*func)(struct pri *pri, char *stuff))
1152
static void pri_old_message(struct pri *ctrl, const char *fmt, va_list *ap)
1156
vsnprintf(tmp, sizeof(tmp), fmt, *ap);
1158
__pri_message(ctrl, tmp);
1163
void pri_message(struct pri *ctrl, const char *fmt, ...)
1169
ctrl = PRI_MASTER(ctrl);
1171
if (!ctrl || !ctrl->msg_line) {
1172
/* Just have to do it the old way. */
1174
pri_old_message(ctrl, fmt, &ap);
1180
added_length = vsnprintf(ctrl->msg_line->str + ctrl->msg_line->length,
1181
sizeof(ctrl->msg_line->str) - ctrl->msg_line->length, fmt, ap);
1183
if (added_length < 0
1184
|| sizeof(ctrl->msg_line->str) <= ctrl->msg_line->length + added_length) {
1185
static char truncated_output[] =
1186
"v-- Error building output or output was truncated. (Next line) --v\n";
1189
* This clause should never need to run because the
1190
* output line accumulation buffer is quite large.
1193
/* vsnprintf() error or output string was truncated. */
1194
if (__pri_message) {
1195
__pri_message(ctrl, truncated_output);
1197
fputs(truncated_output, stdout);
1200
/* Add a terminating '\n' to force a flush of the line. */
1201
ctrl->msg_line->length = strlen(ctrl->msg_line->str);
1202
if (ctrl->msg_line->length) {
1203
ctrl->msg_line->str[ctrl->msg_line->length - 1] = '\n';
1205
ctrl->msg_line->str[0] = '\n';
1206
ctrl->msg_line->str[1] = '\0';
1209
ctrl->msg_line->length += added_length;
1212
if (ctrl->msg_line->length
1213
&& ctrl->msg_line->str[ctrl->msg_line->length - 1] == '\n') {
1214
/* The accumulated output line was terminated so send it out. */
1215
ctrl->msg_line->length = 0;
1216
if (__pri_message) {
1217
__pri_message(ctrl, ctrl->msg_line->str);
1219
fputs(ctrl->msg_line->str, stdout);
1224
void pri_error(struct pri *pri, const char *fmt, ...)
1229
vsnprintf(tmp, sizeof(tmp), fmt, ap);
1232
__pri_error(pri ? PRI_MASTER(pri) : NULL, tmp);
1237
/* Set overlap mode */
1238
void pri_set_overlapdial(struct pri *pri,int state)
1241
pri->overlapdial = state ? 1 : 0;
1245
void pri_set_chan_mapping_logical(struct pri *pri, int state)
1247
if (pri && pri->switchtype == PRI_SWITCH_QSIG) {
1248
pri->chan_mapping_logical = state ? 1 : 0;
1252
void pri_set_inbanddisconnect(struct pri *pri, unsigned int enable)
1255
pri->acceptinbanddisconnect = (enable != 0);
1259
int pri_fd(struct pri *pri)
1266
* \brief Append snprintf output to the given buffer.
1268
* \param buf Buffer currently filling.
1269
* \param buf_used Offset into buffer where to put new stuff.
1270
* \param buf_size Actual buffer size of buf.
1271
* \param format printf format string.
1273
* \return Total buffer space used.
1275
static size_t pri_snprintf(char *buf, size_t buf_used, size_t buf_size, const char *format, ...) __attribute__((format(printf, 4, 5)));
1276
static size_t pri_snprintf(char *buf, size_t buf_used, size_t buf_size, const char *format, ...)
1280
if (buf_used < buf_size) {
1281
va_start(args, format);
1282
buf_used += vsnprintf(buf + buf_used, buf_size - buf_used, format, args);
1285
if (buf_size < buf_used) {
1286
buf_used = buf_size + 1;
1291
char *pri_dump_info_str(struct pri *ctrl)
1296
#ifdef LIBPRI_COUNTERS
1297
struct q921_frame *f;
1298
unsigned q921outstanding;
1301
unsigned long switch_bit;
1307
buf_size = 4096; /* This should be bigger than we will ever need. */
1308
buf = malloc(buf_size);
1313
/* Might be nice to format these a little better */
1315
used = pri_snprintf(buf, used, buf_size, "Switchtype: %s\n",
1316
pri_switch2str(ctrl->switchtype));
1317
used = pri_snprintf(buf, used, buf_size, "Type: %s\n", pri_node2str(ctrl->localtype));
1318
#ifdef LIBPRI_COUNTERS
1319
/* Remember that Q921 Counters include Q931 packets (and any retransmissions) */
1320
used = pri_snprintf(buf, used, buf_size, "Q931 RX: %d\n", ctrl->q931_rxcount);
1321
used = pri_snprintf(buf, used, buf_size, "Q931 TX: %d\n", ctrl->q931_txcount);
1322
used = pri_snprintf(buf, used, buf_size, "Q921 RX: %d\n", ctrl->q921_rxcount);
1323
used = pri_snprintf(buf, used, buf_size, "Q921 TX: %d\n", ctrl->q921_txcount);
1324
q921outstanding = 0;
1330
used = pri_snprintf(buf, used, buf_size, "Q921 Outstanding: %u\n", q921outstanding);
1333
used = pri_snprintf(buf, used, buf_size, "Window Length: %d/%d\n",
1334
ctrl->timers[PRI_TIMER_K], ctrl->window);
1335
used = pri_snprintf(buf, used, buf_size, "Sentrej: %d\n", ctrl->sentrej);
1336
used = pri_snprintf(buf, used, buf_size, "SolicitFbit: %d\n", ctrl->solicitfbit);
1337
used = pri_snprintf(buf, used, buf_size, "Retrans: %d\n", ctrl->retrans);
1338
used = pri_snprintf(buf, used, buf_size, "Busy: %d\n", ctrl->busy);
1340
used = pri_snprintf(buf, used, buf_size, "Overlap Dial: %d\n", ctrl->overlapdial);
1341
used = pri_snprintf(buf, used, buf_size, "Logical Channel Mapping: %d\n",
1342
ctrl->chan_mapping_logical);
1343
used = pri_snprintf(buf, used, buf_size, "Timer and counter settings:\n");
1344
switch_bit = PRI_BIT(ctrl->switchtype);
1345
for (idx = 0; idx < ARRAY_LEN(pri_timer); ++idx) {
1346
if (pri_timer[idx].used_by & switch_bit) {
1347
enum PRI_TIMERS_AND_COUNTERS tmr;
1349
tmr = pri_timer[idx].number;
1350
if (0 <= ctrl->timers[tmr] || tmr == PRI_TIMER_T309) {
1351
used = pri_snprintf(buf, used, buf_size, " %s: %d\n",
1352
pri_timer[idx].name, ctrl->timers[tmr]);
1357
if (buf_size < used) {
1359
"pri_dump_info_str(): Produced output exceeded buffer capacity. (Truncated)\n");
1364
int pri_get_crv(struct pri *pri, q931_call *call, int *callmode)
1366
return q931_call_getcrv(pri, call, callmode);
1369
int pri_set_crv(struct pri *pri, q931_call *call, int crv, int callmode)
1371
return q931_call_setcrv(pri, call, crv, callmode);
1374
void pri_enslave(struct pri *master, struct pri *slave)
1376
if (master && slave)
1377
slave->callpool = &master->localpool;
1380
struct pri_sr *pri_sr_new(void)
1383
req = malloc(sizeof(*req));
1389
void pri_sr_free(struct pri_sr *sr)
1394
int pri_sr_set_channel(struct pri_sr *sr, int channel, int exclusive, int nonisdn)
1396
sr->channel = channel;
1397
sr->exclusive = exclusive;
1398
sr->nonisdn = nonisdn;
1402
int pri_sr_set_bearer(struct pri_sr *sr, int transmode, int userl1)
1404
sr->transmode = transmode;
1405
sr->userl1 = userl1;
1409
int pri_sr_set_called(struct pri_sr *sr, char *called, int calledplan, int numcomplete)
1411
q931_party_address_init(&sr->called);
1413
sr->called.number.valid = 1;
1414
sr->called.number.plan = calledplan;
1415
libpri_copy_string(sr->called.number.str, called, sizeof(sr->called.number.str));
1417
sr->numcomplete = numcomplete;
1421
void pri_sr_set_called_subaddress(struct pri_sr *sr, const struct pri_party_subaddress *subaddress)
1423
pri_copy_party_subaddress_to_q931(&sr->called.subaddress, subaddress);
1426
int pri_sr_set_caller(struct pri_sr *sr, char *caller, char *callername, int callerplan, int callerpres)
1428
q931_party_id_init(&sr->caller);
1430
sr->caller.number.valid = 1;
1431
sr->caller.number.presentation = callerpres;
1432
sr->caller.number.plan = callerplan;
1433
libpri_copy_string(sr->caller.number.str, caller, sizeof(sr->caller.number.str));
1436
sr->caller.name.valid = 1;
1437
sr->caller.name.presentation = callerpres;
1438
sr->caller.name.char_set = PRI_CHAR_SET_ISO8859_1;
1439
libpri_copy_string(sr->caller.name.str, callername,
1440
sizeof(sr->caller.name.str));
1446
void pri_sr_set_caller_subaddress(struct pri_sr *sr, const struct pri_party_subaddress *subaddress)
1448
pri_copy_party_subaddress_to_q931(&sr->caller.subaddress, subaddress);
1451
void pri_sr_set_caller_party(struct pri_sr *sr, const struct pri_party_id *caller)
1453
pri_copy_party_id_to_q931(&sr->caller, caller);
1456
int pri_sr_set_redirecting(struct pri_sr *sr, char *num, int plan, int pres, int reason)
1458
q931_party_redirecting_init(&sr->redirecting);
1459
if (num && num[0]) {
1460
sr->redirecting.from.number.valid = 1;
1461
sr->redirecting.from.number.presentation = pres;
1462
sr->redirecting.from.number.plan = plan;
1463
libpri_copy_string(sr->redirecting.from.number.str, num,
1464
sizeof(sr->redirecting.from.number.str));
1466
sr->redirecting.count = 1;
1467
sr->redirecting.reason = reason;
1472
void pri_sr_set_redirecting_parties(struct pri_sr *sr, const struct pri_party_redirecting *redirecting)
1474
pri_copy_party_id_to_q931(&sr->redirecting.from, &redirecting->from);
1475
pri_copy_party_id_to_q931(&sr->redirecting.to, &redirecting->to);
1476
pri_copy_party_id_to_q931(&sr->redirecting.orig_called, &redirecting->orig_called);
1477
sr->redirecting.orig_reason = redirecting->orig_reason;
1478
sr->redirecting.reason = redirecting->reason;
1479
if (redirecting->count <= 0) {
1480
if (sr->redirecting.from.number.valid) {
1482
* We are redirecting with an unknown count
1483
* so assume the count is one.
1485
sr->redirecting.count = 1;
1487
sr->redirecting.count = 0;
1489
} else if (redirecting->count < PRI_MAX_REDIRECTS) {
1490
sr->redirecting.count = redirecting->count;
1492
sr->redirecting.count = PRI_MAX_REDIRECTS;
1496
void pri_sr_set_reversecharge(struct pri_sr *sr, int requested)
1498
sr->reversecharge = requested;
1501
void pri_sr_set_keypad_digits(struct pri_sr *sr, const char *keypad_digits)
1503
sr->keypad_digits = keypad_digits;
1506
void pri_hold_enable(struct pri *ctrl, int enable)
1509
ctrl = PRI_MASTER(ctrl);
1510
ctrl->hold_support = enable ? 1 : 0;
1514
int pri_hold(struct pri *ctrl, q931_call *call)
1516
if (!ctrl || !call) {
1519
return q931_send_hold(ctrl, call);
1522
int pri_hold_ack(struct pri *ctrl, q931_call *call)
1524
if (!ctrl || !call) {
1527
return q931_send_hold_ack(ctrl, call);
1530
int pri_hold_rej(struct pri *ctrl, q931_call *call, int cause)
1532
if (!ctrl || !call) {
1535
return q931_send_hold_rej(ctrl, call, cause);
1538
int pri_retrieve(struct pri *ctrl, q931_call *call, int channel)
1540
if (!ctrl || !call) {
1543
return q931_send_retrieve(ctrl, call, channel);
1546
int pri_retrieve_ack(struct pri *ctrl, q931_call *call, int channel)
1548
if (!ctrl || !call) {
1551
return q931_send_retrieve_ack(ctrl, call, channel);
1554
int pri_retrieve_rej(struct pri *ctrl, q931_call *call, int cause)
1556
if (!ctrl || !call) {
1559
return q931_send_retrieve_rej(ctrl, call, cause);
1562
int pri_callrerouting_facility(struct pri *pri, q931_call *call, const char *dest, const char* original, const char* reason)
1564
if (!pri || !call || !dest)
1567
return qsig_cf_callrerouting(pri, call, dest, original, reason);
1570
void pri_reroute_enable(struct pri *ctrl, int enable)
1573
ctrl = PRI_MASTER(ctrl);
1574
ctrl->deflection_support = enable ? 1 : 0;
1578
int pri_reroute_call(struct pri *ctrl, q931_call *call, const struct pri_party_id *caller, const struct pri_party_redirecting *deflection, int subscription_option)
1580
const struct q931_party_id *caller_id;
1581
struct q931_party_id local_caller;
1582
struct q931_party_redirecting reroute;
1584
if (!ctrl || !call || !deflection) {
1589
/* Convert the caller update information. */
1590
pri_copy_party_id_to_q931(&local_caller, caller);
1591
q931_party_id_fixup(ctrl, &local_caller);
1592
caller_id = &local_caller;
1597
/* Convert the deflection information. */
1598
q931_party_redirecting_init(&reroute);
1599
pri_copy_party_id_to_q931(&reroute.from, &deflection->from);
1600
q931_party_id_fixup(ctrl, &reroute.from);
1601
pri_copy_party_id_to_q931(&reroute.to, &deflection->to);
1602
q931_party_id_fixup(ctrl, &reroute.to);
1603
pri_copy_party_id_to_q931(&reroute.orig_called, &deflection->orig_called);
1604
q931_party_id_fixup(ctrl, &reroute.orig_called);
1605
reroute.reason = deflection->reason;
1606
reroute.orig_reason = deflection->orig_reason;
1607
if (deflection->count <= 0) {
1609
* We are deflecting with an unknown count
1610
* so assume the count is one.
1613
} else if (deflection->count < PRI_MAX_REDIRECTS) {
1614
reroute.count = deflection->count;
1616
reroute.count = PRI_MAX_REDIRECTS;
1619
return send_reroute_request(ctrl, call, caller_id, &reroute, subscription_option);