1 /*
2 * libpri: An implementation of Primary Rate ISDN
3 *
4 * Written by Mark Spencer <markster@digium.com>
5 *
6 * Copyright (C) 2001, Digium, Inc.
7 * All Rights Reserved.
8 */
9
10 /*
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.
16 *
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.
21 *
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
27 * terms granted here.
28 */
29
30 #ifndef _PRI_INTERNAL_H
31 #define _PRI_INTERNAL_H
32
33 #include <stddef.h>
34 #include <sys/time.h>
35 #include "pri_q921.h"
36 #include "pri_q931.h"
37
38 #define ARRAY_LEN(arr) (sizeof(arr) / sizeof((arr)[0]))
39
40 #define DBGHEAD __FILE__ ":%d %s: "
41 #define DBGINFO __LINE__,__PRETTY_FUNCTION__
42
43 /* Forward declare some structs */
44 struct apdu_event;
45 struct pri_cc_record;
46
47 struct pri_sched {
48 struct timeval when;
49 void (*callback)(void *data);
50 void *data;
51 };
52
53 /*
54 * libpri needs to be able to allocate B channels to support Q.SIG path reservation.
55 * Until that happens, path reservation is not possible. Fortunately,
56 * path reservation is optional with a fallback to what we can implement.
57 */
58 //#define QSIG_PATH_RESERVATION_SUPPORT 1
59
60 /*! Maximum number of facility ie's to handle per incoming message. */
61 #define MAX_FACILITY_IES 8
62
63 /*! Maximum length of sent display text string. (No null terminator.) */
64 #define MAX_DISPLAY_TEXT 80
65
66 /*! Accumulated pri_message() line until a '\n' is seen on the end. */
67 struct pri_msg_line {
68 /*! Accumulated buffer used. */
69 unsigned length;
70 /*! Accumulated pri_message() contents. */
71 char str[2048];
72 };
73
74 /*! \brief D channel controller structure */
75 struct pri {
76 int fd; /* File descriptor for D-Channel */
77 pri_io_cb read_func; /* Read data callback */
78 pri_io_cb write_func; /* Write data callback */
79 void *userdata;
80 /*! Accumulated pri_message() line. (Valid in master record only) */
81 struct pri_msg_line *msg_line;
82 /*! NFAS master/primary channel if appropriate */
83 struct pri *master;
84 /*! Next NFAS slaved D channel if appropriate */
85 struct pri *slave;
86 struct {
87 /*! Dynamically allocated array of timers that can grow as needed. */
88 struct pri_sched *timer;
89 /*! Numer of timer slots in the allocated array of timers. */
90 unsigned num_slots;
91 /*! Maximum timer slots currently needed. */
92 unsigned max_used;
93 /*! First timer id in this timer pool. */
94 unsigned first_id;
95 } sched;
96 int debug; /* Debug stuff */
97 int state; /* State of D-channel */
98 int switchtype; /* Switch type */
99 int nsf; /* Network-Specific Facility (if any) */
100 int localtype; /* Local network type (unknown, network, cpe) */
101 int remotetype; /* Remote network type (unknown, network, cpe) */
102
103 int protodisc; /* Layer 3 protocol discriminator */
104
105 unsigned int nfas:1;/* TRUE if this D channel is involved with an NFAS group */
106 unsigned int bri:1;
107 unsigned int acceptinbanddisconnect:1; /* Should we allow inband progress after DISCONNECT? */
108 unsigned int sendfacility:1;
109 unsigned int overlapdial:1;/* TRUE if we do overlap dialing */
110 unsigned int chan_mapping_logical:1;/* TRUE if do not skip channel 16 (Q.SIG) */
111 unsigned int service_message_support:1;/* TRUE if upper layer supports SERVICE messages */
112 unsigned int hold_support:1;/* TRUE if upper layer supports call hold. */
113 unsigned int deflection_support:1;/* TRUE if upper layer supports call deflection/rerouting. */
114 unsigned int hangup_fix_enabled:1;/* TRUE if should follow Q.931 Section 5.3.2 instead of blindly sending RELEASE_COMPLETE for certain causes */
115 unsigned int cc_support:1;/* TRUE if upper layer supports call completion. */
116 unsigned int transfer_support:1;/* TRUE if the upper layer supports ECT */
117 unsigned int aoc_support:1;/* TRUE if can send AOC events to the upper layer. */
118 unsigned int manual_connect_ack:1;/* TRUE if the CONNECT_ACKNOWLEDGE is sent with API call */
119 unsigned int mcid_support:1;/* TRUE if the upper layer supports MCID */
120
121 /*! Layer 2 link control for D channel. */
122 struct q921_link link;
123 /*! Layer 2 persistence option. */
124 enum pri_layer2_persistence l2_persistence;
125 /*! T201 TEI Identity Check timer. */
126 int t201_timer;
127 /*! Number of times T201 has expired. */
128 int t201_expirycnt;
129
130 int cref; /* Next call reference value */
131
132 /* All ISDN Timer values */
133 int timers[PRI_MAX_TIMERS];
134
135 /* Used by scheduler */
136 int schedev;
137 pri_event ev; /* Static event thingy */
138 /*! Subcommands for static event thingy. */
139 struct pri_subcommands subcmds;
140
141 /* Q.931 calls */
142 struct q931_call **callpool;
143 struct q931_call *localpool;
144
145 /* q921/q931 packet counters */
146 unsigned int q921_txcount;
147 unsigned int q921_rxcount;
148 unsigned int q931_txcount;
149 unsigned int q931_rxcount;
150
151 short last_invoke; /* Last ROSE invoke ID (Valid in master record only) */
152
153 /*! Call completion (Valid in master record only) */
154 struct {
155 /*! Active CC records */
156 struct pri_cc_record *pool;
157 /*! Last CC record id allocated. */
158 unsigned short last_record_id;
159 /*! Last CC PTMP reference id allocated. (0-127) */
160 unsigned char last_reference_id;
161 /*! Last CC PTMP linkage id allocated. (0-127) */
162 unsigned char last_linkage_id;
163 /*! Configured CC options. */
164 struct {
165 /*! PTMP recall mode: globalRecall(0), specificRecall(1) */
166 unsigned char recall_mode;
167 /*! Q.SIG Request signaling link retention: release(0), retain(1), do-not-care(2) */
168 unsigned char signaling_retention_req;
169 /*! Q.SIG Response request signaling link retention: release(0), retain(1) */
170 unsigned char signaling_retention_rsp;
171 #if defined(QSIG_PATH_RESERVATION_SUPPORT)
172 /*! Q.SIG TRUE if response request can support path reservation. */
173 unsigned char allow_path_reservation;
174 #endif /* defined(QSIG_PATH_RESERVATION_SUPPORT) */
175 } option;
176 } cc;
177
178 /*! For delayed processing of facility ie's. */
179 struct {
180 /*! Array of facility ie locations in the current received message. */
181 q931_ie *ie[MAX_FACILITY_IES];
182 /*! Codeset facility ie found within. */
183 unsigned char codeset[MAX_FACILITY_IES];
184 /*! Number of facility ie's in the array from the current received message. */
185 unsigned char count;
186 } facility;
187 /*! Display text policy handling options. */
188 struct {
189 /*! Send display text policy option flags. */
190 unsigned long send;
191 /*! Receive display text policy option flags. */
192 unsigned long receive;
193 } display_flags;
194 /*! Configured date/time ie send policy option. */
195 int date_time_send;
196 };
197
198 /*! \brief Maximum name length plus null terminator (From ECMA-164) */
199 #define PRI_MAX_NAME_LEN (50 + 1)
200
201 /*! \brief Q.SIG name information. */
202 struct q931_party_name {
203 /*! \brief TRUE if name data is valid */
204 unsigned char valid;
205 /*!
206 * \brief Q.931 presentation-indicator encoded field
207 * \note Must tollerate the Q.931 screening-indicator field values being present.
208 */
209 unsigned char presentation;
210 /*!
211 * \brief Character set the name is using.
212 * \details
213 * unknown(0),
214 * iso8859-1(1),
215 * enum-value-withdrawn-by-ITU-T(2)
216 * iso8859-2(3),
217 * iso8859-3(4),
218 * iso8859-4(5),
219 * iso8859-5(6),
220 * iso8859-7(7),
221 * iso10646-BmpString(8),
222 * iso10646-utf-8String(9)
223 */
224 unsigned char char_set;
225 /*! \brief Name data with null terminator. */
226 char str[PRI_MAX_NAME_LEN];
227 };
228
229 /*! \brief Maximum phone number (address) length plus null terminator */
230 #define PRI_MAX_NUMBER_LEN (31 + 1)
231
232 struct q931_party_number {
233 /*! \brief TRUE if number data is valid */
234 unsigned char valid;
235 /*! \brief Q.931 presentation-indicator and screening-indicator encoded fields */
236 unsigned char presentation;
237 /*! \brief Q.931 Type-Of-Number and numbering-plan encoded fields */
238 unsigned char plan;
239 /*! \brief Number data with terminator. */
240 char str[PRI_MAX_NUMBER_LEN];
241 };
242
243 /*! \brief Maximum subaddress length plus null terminator */
244 #define PRI_MAX_SUBADDRESS_LEN (20 + 1)
245
246 struct q931_party_subaddress {
247 /*! \brief TRUE if the subaddress information is valid/present */
248 unsigned char valid;
249 /*!
250 * \brief Subaddress type.
251 * \details
252 * nsap(0),
253 * user_specified(2)
254 */
255 unsigned char type;
256 /*!
257 * \brief TRUE if odd number of address signals
258 * \note The odd/even indicator is used when the type of subaddress is
259 * user_specified and the coding is BCD.
260 */
261 unsigned char odd_even_indicator;
262 /*! \brief Length of the subaddress data */
263 unsigned char length;
264 /*!
265 * \brief Subaddress data with null terminator.
266 * \note The null terminator is a convenience only since the data could be
267 * BCD/binary and thus have a null byte as part of the contents.
268 */
269 unsigned char data[PRI_MAX_SUBADDRESS_LEN];
270 };
271
272 struct q931_party_address {
273 /*! \brief Subscriber phone number */
274 struct q931_party_number number;
275 /*! \brief Subscriber subaddress */
276 struct q931_party_subaddress subaddress;
277 };
278
279 /*! \brief Information needed to identify an endpoint in a call. */
280 struct q931_party_id {
281 /*! \brief Subscriber name */
282 struct q931_party_name name;
283 /*! \brief Subscriber phone number */
284 struct q931_party_number number;
285 /*! \brief Subscriber subaddress */
286 struct q931_party_subaddress subaddress;
287 };
288
289 enum Q931_REDIRECTING_STATE {
290 /*!
291 * \details
292 * CDO-Idle/CDF-Inv-Idle
293 */
294 Q931_REDIRECTING_STATE_IDLE,
295 /*!
296 * \details
297 * CDF-Inv-Wait - A DivLeg2 has been received and
298 * we are waiting for valid presentation restriction information to send.
299 */
300 Q931_REDIRECTING_STATE_PENDING_TX_DIV_LEG_3,
301 /*!
302 * \details
303 * CDO-Divert - A DivLeg1 has been received and
304 * we are waiting for the presentation restriction information to come in.
305 */
306 Q931_REDIRECTING_STATE_EXPECTING_RX_DIV_LEG_3,
307 };
308
309 /*!
310 * \brief Do not increment above this count.
311 * \details
312 * It is not our responsibility to enforce the maximum number of redirects.
313 * However, we cannot allow an increment past this number without breaking things.
314 * Besides, more than 255 redirects is probably not a good thing.
315 */
316 #define PRI_MAX_REDIRECTS 0xFF
317
318 /*! \brief Redirecting information struct */
319 struct q931_party_redirecting {
320 enum Q931_REDIRECTING_STATE state;
321 /*! \brief Who is redirecting the call (Sent to the party the call is redirected toward) */
322 struct q931_party_id from;
323 /*! \brief Call is redirecting to a new party (Sent to the caller) */
324 struct q931_party_id to;
325 /*! Originally called party (in cases of multiple redirects) */
326 struct q931_party_id orig_called;
327 /*!
328 * \brief Number of times the call was redirected
329 * \note The call is being redirected if the count is non-zero.
330 */
331 unsigned char count;
332 /*! Original reason for redirect (in cases of multiple redirects) */
333 unsigned char orig_reason;
334 /*! \brief Redirection reasons */
335 unsigned char reason;
336 };
337
338 /*! \brief New call setup parameter structure */
339 struct pri_sr {
340 int transmode;
341 int channel;
342 int exclusive;
343 int nonisdn;
344 struct q931_party_redirecting redirecting;
345 struct q931_party_id caller;
346 struct q931_party_address called;
347 int userl1;
348 int numcomplete;
349 int cis_call;
350 int cis_auto_disconnect;
351 const char *useruserinfo;
352 const char *keypad_digits;
353 int transferable;
354 int reversecharge;
355 int aoc_charging_request;
356 };
357
358 #define Q931_MAX_TEI 8
359
360 /*! \brief Incoming call transfer states. */
361 enum INCOMING_CT_STATE {
362 /*!
363 * \details
364 * Incoming call transfer is not active.
365 */
366 INCOMING_CT_STATE_IDLE,
367 /*!
368 * \details
369 * We have seen an incoming CallTransferComplete(alerting)
370 * so we are waiting for the expected CallTransferActive
371 * before updating the connected line about the remote party id.
372 */
373 INCOMING_CT_STATE_EXPECT_CT_ACTIVE,
374 /*!
375 * \details
376 * A call transfer message came in that updated the remote party id
377 * that we need to post a connected line update.
378 */
379 INCOMING_CT_STATE_POST_CONNECTED_LINE
380 };
381
382 /*! Call hold supplementary states. */
383 enum Q931_HOLD_STATE {
384 /*! \brief No call hold activity. */
385 Q931_HOLD_STATE_IDLE,
386 /*! \brief Request made to hold call. */
387 Q931_HOLD_STATE_HOLD_REQ,
388 /*! \brief Request received to hold call. */
389 Q931_HOLD_STATE_HOLD_IND,
390 /*! \brief Call is held. */
391 Q931_HOLD_STATE_CALL_HELD,
392 /*! \brief Request made to retrieve call. */
393 Q931_HOLD_STATE_RETRIEVE_REQ,
394 /*! \brief Request received to retrieve call. */
395 Q931_HOLD_STATE_RETRIEVE_IND,
396 };
397
398 /* Only save the first of each BC, HLC, and LLC from the initial SETUP. */
399 #define CC_SAVED_IE_BC (1 << 0) /*!< BC has already been saved. */
400 #define CC_SAVED_IE_HLC (1 << 1) /*!< HLC has already been saved. */
401 #define CC_SAVED_IE_LLC (1 << 2) /*!< LLC has already been saved. */
402
403 /*! Saved ie contents for BC, HLC, and LLC. (Only the first of each is saved.) */
404 struct q931_saved_ie_contents {
405 /*! Length of saved ie contents. */
406 unsigned char length;
407 /*! Saved ie contents data. */
408 unsigned char data[
409 /* Bearer Capability has a max length of 12. */
410 12
411 /* High Layer Compatibility has a max length of 5. */
412 + 5
413 /* Low Layer Compatibility has a max length of 18. */
414 + 18
415 /* Room for null terminator just in case. */
416 + 1];
417 };
418
419 /*! Digested BC parameters. */
420 struct decoded_bc {
421 int transcapability;
422 int transmoderate;
423 int transmultiple;
424 int userl1;
425 int userl2;
426 int userl3;
427 int rateadaption;
428 };
429
430 /* q931_call datastructure */
431 struct q931_call {
432 struct pri *pri; /* D channel controller (master) */
433 struct q921_link *link; /* Q.921 link associated with this call. */
434 struct q931_call *next;
435 int cr; /* Call Reference */
436 /* Slotmap specified (bitmap of channels 31/24-1) (Channel Identifier IE) (-1 means not specified) */
437 int slotmap;
438 /* An explicit channel (Channel Identifier IE) (-1 means not specified) */
439 int channelno;
440 /* An explicit DS1 (-1 means not specified) */
441 int ds1no;
442 /* Whether or not the ds1 is explicitly identified or implicit. If implicit
443 the bchan is on the same span as the current active dchan (NFAS) */
444 int ds1explicit;
445 /* Channel flags (0 means none retrieved) */
446 int chanflags;
447
448 int alive; /* Whether or not the call is alive */
449 int acked; /* Whether setup has been acked or not */
450 int sendhangupack; /* Whether or not to send a hangup ack */
451 int proc; /* Whether we've sent a call proceeding / alerting */
452
453 int ri; /* Restart Indicator (Restart Indicator IE) */
454
455 /*! Bearer Capability */
456 struct decoded_bc bc;
457
458 /*!
459 * \brief TRUE if the call is a Call Independent Signalling connection.
460 * \note The call has no B channel associated with it. (Just signalling)
461 */
462 int cis_call;
463 /*!
464 * \brief TRUE if we have recognized a use for this CIS call.
465 * \note An incoming CIS call will be immediately disconnected if not set.
466 * This is a safeguard against unhandled incoming CIS calls to protect the
467 * call reference pool.
468 */
469 int cis_recognized;
470 /*! \brief TRUE if we will auto disconnect the cis_call we originated. */
471 int cis_auto_disconnect;
472
473 int progcode; /* Progress coding */
474 int progloc; /* Progress Location */
475 int progress; /* Progress indicator */
476 int progressmask; /* Progress Indicator bitmask */
477
478 int notify; /* Notification indicator. */
479
480 int causecode; /* Cause Coding */
481 int causeloc; /* Cause Location */
482 int cause; /* Cause of clearing */
483
484 enum Q931_CALL_STATE peercallstate; /* Call state of peer as reported */
485 enum Q931_CALL_STATE ourcallstate; /* Our call state */
486 enum Q931_CALL_STATE sugcallstate; /* Status call state */
487
488 int ani2; /* ANI II */
489
490 /*! Buffer for digits that come in KEYPAD_FACILITY */
491 char keypad_digits[32 + 1];
492
493 /*! Current dialed digits to be sent or just received. */
494 char overlap_digits[PRI_MAX_NUMBER_LEN];
495
496 /*!
497 * \brief Local party ID
498 * \details
499 * The Caller-ID and connected-line ID are just roles the local and remote party
500 * play while a call is being established. Which roll depends upon the direction
501 * of the call.
502 * Outgoing party info is to identify the local party to the other end.
503 * (Caller-ID for originated or connected-line for answered calls.)
504 * Incoming party info is to identify the remote party to us.
505 * (Caller-ID for answered or connected-line for originated calls.)
506 */
507 struct q931_party_id local_id;
508 /*!
509 * \brief Remote party ID
510 * \details
511 * The Caller-ID and connected-line ID are just roles the local and remote party
512 * play while a call is being established. Which roll depends upon the direction
513 * of the call.
514 * Outgoing party info is to identify the local party to the other end.
515 * (Caller-ID for originated or connected-line for answered calls.)
516 * Incoming party info is to identify the remote party to us.
517 * (Caller-ID for answered or connected-line for originated calls.)
518 */
519 struct q931_party_id remote_id;
520 /*! \brief Automatic Number Identification (ANI) */
521 struct q931_party_number ani;
522
523 /*!
524 * \brief Staging place for the Q.931 redirection number ie.
525 * \note
526 * The number could be the remote_id.number or redirecting.to.number
527 * depending upon the notification indicator.
528 */
529 struct q931_party_number redirection_number;
530
531 /*!
532 * \brief Called party address.
533 * \note The called.number.str is the accumulated overlap dial digits
534 * and enbloc digits.
535 * \note The called.number.presentation value is not used.
536 */
537 struct q931_party_address called;
538 int nonisdn;
539 int complete; /* no more digits coming */
540 int newcall; /* if the received message has a new call reference value */
541
542 int retranstimer; /* Timer for retransmitting DISC */
543 int t308_timedout; /* Whether t308 timed out once */
544
545 struct q931_party_redirecting redirecting;
546
547 /*! \brief Incoming call transfer state. */
548 enum INCOMING_CT_STATE incoming_ct_state;
549 /*! Call hold supplementary state. Valid on master call record only. */
550 enum Q931_HOLD_STATE hold_state;
551 /*! Call hold event timer. Valid on master call record only. */
552 int hold_timer;
553
554 int deflection_in_progress; /*!< CallDeflection for NT PTMP in progress. */
555 /*! TRUE if the connected number ie was in the current received message. */
556 int connected_number_in_message;
557 /*! TRUE if the redirecting number ie was in the current received message. */
558 int redirecting_number_in_message;
559
560 int useruserprotocoldisc;
561 char useruserinfo[256];
562
563 long aoc_units; /* Advice of Charge Units */
564
565 struct apdu_event *apdus; /* APDU queue for call */
566
567 int transferable; /* RLT call is transferable */
568 unsigned int rlt_call_id; /* RLT call id */
569
570 /*! ETSI Explicit Call Transfer link id. */
571 int link_id;
572 /*! TRUE if link_id is valid. */
573 int is_link_id_valid;
574
575 /* Bridged call info */
576 struct q931_call *bridged_call; /* Pointer to other leg of bridged call (Used by Q.SIG when eliminating tromboned calls) */
577
578 int changestatus; /* SERVICE message changestatus */
579 int reversecharge; /* Reverse charging indication:
580 -1 - No reverse charging
581 1 - Reverse charging
582 0,2-7 - Reserved for future use */
583 int t303_timer;
584 int t303_expirycnt;
585 int t312_timer;
586 int fake_clearing_timer;
587
588 int hangupinitiated;
589 /*! \brief TRUE if we broadcast this call's SETUP message. */
590 int outboundbroadcast;
591 /*! TRUE if the master call is processing a hangup. Don't destroy it now. */
592 int master_hanging_up;
593 /*!
594 * \brief Master call controlling this call.
595 * \note Always valid. Master and normal calls point to self.
596 */
597 struct q931_call *master_call;
598
599 /* These valid in master call only */
600 struct q931_call *subcalls[Q931_MAX_TEI];
601 int pri_winner;
602
603 /* Call completion */
604 struct {
605 /*!
606 * \brief CC record associated with this call.
607 * \note
608 * CC signaling link or original call when cc-available indicated.
609 */
610 struct pri_cc_record *record;
611 /*! Original calling party. */
612 struct q931_party_id party_a;
613 /*! Saved BC, HLC, and LLC from initial SETUP */
614 struct q931_saved_ie_contents saved_ie_contents;
615 /*! Only save the first of each BC, HLC, and LLC from the initial SETUP. */
616 unsigned char saved_ie_flags;
617 /*! TRUE if call needs to be hung up. */
618 unsigned char hangup_call;
619 /*! TRUE if we originated this call. */
620 unsigned char originated;
621 /*! TRUE if outgoing call was already redirected. */
622 unsigned char initially_redirected;
623 } cc;
624
625 /*! Display text ie contents. */
626 struct {
627 /*! Display ie text. NULL if not present or consumed as remote name. */
628 const unsigned char *text;
629 /*! Full IE code of received display text */
630 int full_ie;
631 /*! Length of display text. */
632 unsigned char length;
633 /*!
634 * \brief Character set the text is using.
635 * \details
636 * unknown(0),
637 * iso8859-1(1),
638 * enum-value-withdrawn-by-ITU-T(2)
639 * iso8859-2(3),
640 * iso8859-3(4),
641 * iso8859-4(5),
642 * iso8859-5(6),
643 * iso8859-7(7),
644 * iso10646-BmpString(8),
645 * iso10646-utf-8String(9)
646 */
647 unsigned char char_set;
648 } display;
649
650 /*! AOC charge requesting on Setup */
651 int aoc_charging_request;
652
653 /*! TRUE if the slotmap is E1 (32 bits). */
654 unsigned int slotmap_size:1;
655 /*! TRUE if need to see the channel id ie in first response to SETUP. */
656 unsigned int channel_id_ie_mandatory:1;
657
658 /*! Control the RESTART reception to the upper layer. */
659 struct {
660 /*! Timer ID of RESTART notification events to upper layer. */
661 int timer;
662 /*! Current RESTART notification index. */
663 int idx;
664 /*! Number of channels in the channel ID list. */
665 int count;
666 /*! Channel ID list */
667 char chan_no[32];
668 } restart;
669 /*! Control the RESTART retransmissions. */
670 struct {
671 /*! T316 RESTART retransmit timer. */
672 int t316_timer;
673 /*! Number of times remaining that RESTART can be transmitted. */
674 int remain;
675 /*! Encoded RESTART channel id. */
676 int channel;
677 } restart_tx;
678 };
679
680 enum CC_STATES {
681 /*! CC is not active. */
682 CC_STATE_IDLE,
683 // /*! CC has recorded call information in anticipation of CC availability. */
684 // CC_STATE_RECORD_RETENTION,
685 /*! CC is available and waiting on ALERTING or DISCONNECT to go out. */
686 CC_STATE_PENDING_AVAILABLE,
687 /*! CC is available and waiting on possible CC request. */
688 CC_STATE_AVAILABLE,
689 /*! CC is requested to be activated and waiting on party B to acknowledge. */
690 CC_STATE_REQUESTED,
691 /*! CC is activated and waiting for party B to become available. */
692 CC_STATE_ACTIVATED,
693 /*! CC party B is available and waiting for status of party A. */
694 CC_STATE_B_AVAILABLE,
695 /*! CC is suspended because party A is not available. (Monitor party A.) */
696 CC_STATE_SUSPENDED,
697 /*! CC is waiting for party A to initiate CC callback. */
698 CC_STATE_WAIT_CALLBACK,
699 /*! CC callback in progress. */
700 CC_STATE_CALLBACK,
701 /*! CC is waiting for signaling link to be cleared before destruction. */
702 CC_STATE_WAIT_DESTRUCTION,
703
704 /*! Number of CC states. Must be last in enum. */
705 CC_STATE_NUM
706 };
707
708 enum CC_EVENTS {
709 /*! CC is available for the current call. */
710 CC_EVENT_AVAILABLE,
711 /*! Requesting CC activation. */
712 CC_EVENT_CC_REQUEST,
713 /*! Requesting CC activation accepted. */
714 CC_EVENT_CC_REQUEST_ACCEPT,
715 /*! Requesting CC activation failed (error/reject received). */
716 CC_EVENT_CC_REQUEST_FAIL,
717 /*! CC party B is available, party A is considered free. */
718 CC_EVENT_REMOTE_USER_FREE,
719 /*! CC party B is available, party A is busy or CCBS busy. */
720 CC_EVENT_B_FREE,
721 /*! Someone else responded to the CC recall. */
722 CC_EVENT_STOP_ALERTING,
723 /*! CC poll/prompt for party A status. */
724 CC_EVENT_A_STATUS,
725 /*! CC party A is free/available for recall. */
726 CC_EVENT_A_FREE,
727 /*! CC party A is busy/not-available for recall. */
728 CC_EVENT_A_BUSY,
729 /*! Suspend monitoring party B because party A is busy. */
730 CC_EVENT_SUSPEND,
731 /*! Resume monitoring party B because party A is now available. */
732 CC_EVENT_RESUME,
733 /*! This is the CC recall call attempt. */
734 CC_EVENT_RECALL,
735 /*! Link request to cancel/deactivate CC received. */
736 CC_EVENT_LINK_CANCEL,
737 /*! Tear down CC request from upper layer. */
738 CC_EVENT_CANCEL,
739 /*! Abnormal clearing of original call. (T309 processing/T309 timeout/TEI removal) */
740 CC_EVENT_INTERNAL_CLEARING,
741 /*! Received message indicating tear down of CC signaling link completed. */
742 CC_EVENT_SIGNALING_GONE,
743 /*! Delayed hangup request for the signaling link to allow subcmd events to be passed up. */
744 CC_EVENT_HANGUP_SIGNALING,
745 /*! Sent ALERTING message. */
746 CC_EVENT_MSG_ALERTING,
747 /*! Sent DISCONNECT message. */
748 CC_EVENT_MSG_DISCONNECT,
749 /*! Sent RELEASE message. */
750 CC_EVENT_MSG_RELEASE,
751 /*! Sent RELEASE_COMPLETE message. */
752 CC_EVENT_MSG_RELEASE_COMPLETE,
753 /*! T_ACTIVATE timer timed out. */
754 CC_EVENT_TIMEOUT_T_ACTIVATE,
755 #if 0
756 /*! T_DEACTIVATE timer timed out. */
757 CC_EVENT_TIMEOUT_T_DEACTIVATE,
758 /*! T_INTERROGATE timer timed out. */
759 CC_EVENT_TIMEOUT_T_INTERROGATE,
760 #endif
761 /*! T_RETENTION timer timed out. */
762 CC_EVENT_TIMEOUT_T_RETENTION,
763 /*! T-STATUS timer equivalent for CC user A status timed out. */
764 CC_EVENT_TIMEOUT_T_CCBS1,
765 /*! Timeout for valid party A status. */
766 CC_EVENT_TIMEOUT_EXTENDED_T_CCBS1,
767 /*! Max time the CCBS/CCNR service will be active. */
768 CC_EVENT_TIMEOUT_T_SUPERVISION,
769 /*! Max time to wait for user A to respond to user B availability. */
770 CC_EVENT_TIMEOUT_T_RECALL,
771 };
772
773 enum CC_PARTY_A_AVAILABILITY {
774 CC_PARTY_A_AVAILABILITY_INVALID,
775 CC_PARTY_A_AVAILABILITY_BUSY,
776 CC_PARTY_A_AVAILABILITY_FREE,
777 };
778
779 /* Invalid PTMP call completion reference and linkage id value. */
780 #define CC_PTMP_INVALID_ID 0xFF
781
782 /*! \brief Call-completion record */
783 struct pri_cc_record {
784 /*! Next call-completion record in the list */
785 struct pri_cc_record *next;
786 /*! D channel control structure. */
787 struct pri *ctrl;
788 /*! Original call that is offered CC availability. (NULL if no longer exists.) */
789 struct q931_call *original_call;
790 /*!
791 * \brief Associated signaling link. (NULL if not established.)
792 * \note
793 * PTMP - Broadcast dummy call reference call.
794 * (If needed, the TE side could use this pointer to locate its specific
795 * dummy call reference call.)
796 * \note
797 * PTP - REGISTER signaling link.
798 * \note
799 * Q.SIG - SETUP signaling link.
800 */
801 struct q931_call *signaling;
802 /*! Call-completion record id (0 - 65535) */
803 long record_id;
804 /*! Call-completion state */
805 enum CC_STATES state;
806 /*! Original calling party. */
807 struct q931_party_id party_a;
808 /*! Original called party. */
809 struct q931_party_address party_b;
810 /*! Saved BC, HLC, and LLC from initial SETUP */
811 struct q931_saved_ie_contents saved_ie_contents;
812 /*! Saved decoded BC */
813 struct decoded_bc bc;
814
815 /*! FSM parameters. */
816 union {
817 /*! PTMP FSM parameters. */
818 struct {
819 /*! Extended T_CCBS1 timer id for CCBSStatusRequest handling. */
820 int extended_t_ccbs1;
821 /*! Invoke id for the CCBSStatusRequest message to find if T_CCBS1 still running. */
822 int t_ccbs1_invoke_id;
823 /*! Number of times party A status request got no responses. */
824 int party_a_status_count;
825 /*! Accumulating party A availability status */
826 enum CC_PARTY_A_AVAILABILITY party_a_status_acc;
827 } ptmp;
828 /*! PTP FSM parameters. */
829 struct {
830 } ptp;
831 struct {
832 /*! Q.931 message type the current message event came in on. */
833 int msgtype;
834 } qsig;
835 } fsm;
836 /*! Received message parameters of interest. */
837 union {
838 /*! cc-request error/reject response */
839 struct {
840 /*! enum APDU_CALLBACK_REASON reason */
841 int reason;
842 /*! MSG_ERROR/MSG_REJECT fail code. */
843 int code;
844 } cc_req_rsp;
845 } msg;
846 /*! Party A availability status */
847 enum CC_PARTY_A_AVAILABILITY party_a_status;
848 /*! Indirect timer id to abort indirect action events. */
849 int t_indirect;
850 /*!
851 * \brief PTMP T_RETENTION timer id.
852 * \note
853 * This timer is used by all CC agents to implement
854 * the Asterisk CC core offer timer.
855 */
856 int t_retention;
857 /*!
858 * \brief CC service supervision timer.
859 *
860 * \details
861 * This timer is one of the following timer id's depending upon
862 * switch type and CC mode:
863 * PTMP - T_CCBS2/T_CCNR2,
864 * PTP - T_CCBS5/T_CCNR5/T_CCBS6/T_CCNR6,
865 * Q.SIG - QSIG_CCBS_T2/QSIG_CCNR_T2
866 */
867 int t_supervision;
868 /*!
869 * \brief Party A response to B availability for recall timer.
870 * \details
871 * This timer is one of the following timer id's:
872 * PTMP - T_CCBS3
873 * Q.SIG - QSIG_CC_T3
874 */
875 int t_recall;
876 /*! Invoke id for the cc-request message to find if T_ACTIVATE/QSIG_CC_T1 still running. */
877 int t_activate_invoke_id;
878 /*! Pending response information. */
879 struct {
880 /*!
881 * \brief Send response on this signaling link.
882 * \note Used by PTMP for CCBSRequest/CCNRRequest/CCBSCall responses.
883 * \note Used by Q.SIG for ccRingout responses.
884 */
885 struct q931_call *signaling;
886 /*! Invoke operation code */
887 int invoke_operation;
888 /*! Invoke id to use in the pending response. */
889 short invoke_id;
890 } response;
891
892 /*! TRUE if the call-completion FSM has completed and this record needs to be destroyed. */
893 unsigned char fsm_complete;
894 /*! TRUE if we are a call completion agent. */
895 unsigned char is_agent;
896 /*! TRUE if active cc mode is CCNR. */
897 unsigned char is_ccnr;
898 /*! PTMP pre-activation reference id. (0-127) */
899 unsigned char call_linkage_id;
900 /*! PTMP active CCBS reference id. (0-127) */
901 unsigned char ccbs_reference_id;
902 /*! Negotiated options */
903 struct {
904 /*! PTMP recall mode: globalRecall(0), specificRecall(1) */
905 unsigned char recall_mode;
906 /*! TRUE if negotiated for Q.SIG signaling link to be retained. */
907 unsigned char retain_signaling_link;
908 #if defined(QSIG_PATH_RESERVATION_SUPPORT)
909 /*! Q.SIG TRUE if can do path reservation. */
910 unsigned char do_path_reservation;
911 #endif /* defined(QSIG_PATH_RESERVATION_SUPPORT) */
912 } option;
913 };
914
915 /*! D channel control structure with associated dummy call reference record. */
916 struct d_ctrl_dummy {
917 /*! D channel control structure. Must be first in the structure. */
918 struct pri ctrl;
919 /*! Dummy call reference call record. */
920 struct q931_call dummy_call;
921 };
922
923 /*! Layer 2 link control structure with associated dummy call reference record. */
924 struct link_dummy {
925 /*! Layer 2 control structure. Must be first in the structure. */
926 struct q921_link link;
927 /*! Dummy call reference call record. */
928 struct q931_call dummy_call;
929 };
930
931 /*!
932 * \brief Check if the given call ptr is valid and gripe if not.
933 *
934 * \param ctrl D channel controller.
935 * \param call Q.931 call leg.
936 *
937 * \retval TRUE if call ptr is valid.
938 * \retval FALSE if call ptr is invalid.
939 */
940 #define pri_is_call_valid(ctrl, call) \
941 q931_is_call_valid_gripe(ctrl, call, __PRETTY_FUNCTION__, __LINE__)
942
943 int q931_is_call_valid(struct pri *ctrl, struct q931_call *call);
944 int q931_is_call_valid_gripe(struct pri *ctrl, struct q931_call *call, const char *func_name, unsigned long func_line);
945
946 unsigned pri_schedule_event(struct pri *ctrl, int ms, void (*function)(void *data), void *data);
947
948 extern pri_event *pri_schedule_run(struct pri *pri);
949
950 void pri_schedule_del(struct pri *ctrl, unsigned id);
951 int pri_schedule_check(struct pri *ctrl, unsigned id, void (*function)(void *data), void *data);
952
953 extern pri_event *pri_mkerror(struct pri *pri, char *errstr);
954
955 void pri_message(struct pri *ctrl, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
956 void pri_error(struct pri *ctrl, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
957
958 void libpri_copy_string(char *dst, const char *src, size_t size);
959
960 void pri_link_destroy(struct q921_link *link);
961 struct q921_link *pri_link_new(struct pri *ctrl, int sapi, int tei);
962
963 void q931_init_call_record(struct q921_link *link, struct q931_call *call, int cr);
964
965 void pri_sr_init(struct pri_sr *req);
966
967 void q931_party_name_init(struct q931_party_name *name);
968 void q931_party_number_init(struct q931_party_number *number);
969 void q931_party_subaddress_init(struct q931_party_subaddress *subaddr);
970 void q931_party_address_init(struct q931_party_address *address);
971 void q931_party_id_init(struct q931_party_id *id);
972 void q931_party_redirecting_init(struct q931_party_redirecting *redirecting);
973
q931_party_address_to_id(struct q931_party_id * id,const struct q931_party_address * address)974 static inline void q931_party_address_to_id(struct q931_party_id *id, const struct q931_party_address *address)
975 {
976 id->number = address->number;
977 id->subaddress = address->subaddress;
978 }
979
q931_party_id_to_address(struct q931_party_address * address,const struct q931_party_id * id)980 static inline void q931_party_id_to_address(struct q931_party_address *address, const struct q931_party_id *id)
981 {
982 address->number = id->number;
983 address->subaddress = id->subaddress;
984 }
985
986 int q931_party_name_cmp(const struct q931_party_name *left, const struct q931_party_name *right);
987 int q931_party_number_cmp(const struct q931_party_number *left, const struct q931_party_number *right);
988 int q931_party_subaddress_cmp(const struct q931_party_subaddress *left, const struct q931_party_subaddress *right);
989 int q931_party_address_cmp(const struct q931_party_address *left, const struct q931_party_address *right);
990 int q931_party_id_cmp(const struct q931_party_id *left, const struct q931_party_id *right);
991 int q931_party_id_cmp_address(const struct q931_party_id *left, const struct q931_party_id *right);
992
993 int q931_cmp_party_id_to_address(const struct q931_party_id *id, const struct q931_party_address *address);
994 void q931_party_id_copy_to_address(struct q931_party_address *address, const struct q931_party_id *id);
995
996 void q931_party_name_copy_to_pri(struct pri_party_name *pri_name, const struct q931_party_name *q931_name);
997 void q931_party_number_copy_to_pri(struct pri_party_number *pri_number, const struct q931_party_number *q931_number);
998 void q931_party_subaddress_copy_to_pri(struct pri_party_subaddress *pri_subaddress, const struct q931_party_subaddress *q931_subaddress);
999 void q931_party_address_copy_to_pri(struct pri_party_address *pri_address, const struct q931_party_address *q931_address);
1000 void q931_party_id_copy_to_pri(struct pri_party_id *pri_id, const struct q931_party_id *q931_id);
1001 void q931_party_redirecting_copy_to_pri(struct pri_party_redirecting *pri_redirecting, const struct q931_party_redirecting *q931_redirecting);
1002
1003 void pri_copy_party_name_to_q931(struct q931_party_name *q931_name, const struct pri_party_name *pri_name);
1004 void pri_copy_party_number_to_q931(struct q931_party_number *q931_number, const struct pri_party_number *pri_number);
1005 void pri_copy_party_subaddress_to_q931(struct q931_party_subaddress *q931_subaddress, const struct pri_party_subaddress *pri_subaddress);
1006 void pri_copy_party_id_to_q931(struct q931_party_id *q931_id, const struct pri_party_id *pri_id);
1007
1008 void q931_party_id_fixup(const struct pri *ctrl, struct q931_party_id *id);
1009 int q931_party_id_presentation(const struct q931_party_id *id);
1010
1011 int q931_display_name_get(struct q931_call *call, struct q931_party_name *name);
1012 int q931_display_text(struct pri *ctrl, struct q931_call *call, const struct pri_subcmd_display_txt *display);
1013
1014 int q931_facility_display_name(struct pri *ctrl, struct q931_call *call, const struct q931_party_name *name);
1015 int q931_facility_called(struct pri *ctrl, struct q931_call *call, const struct q931_party_id *called);
1016
1017 const char *q931_call_state_str(enum Q931_CALL_STATE callstate);
1018 const char *msg2str(int msg);
1019
1020 int q931_get_subcall_count(struct q931_call *master);
1021 struct q931_call *q931_find_winning_call(struct q931_call *call);
1022 int q931_master_pass_event(struct pri *ctrl, struct q931_call *subcall, int msg_type);
1023 struct pri_subcommand *q931_alloc_subcommand(struct pri *ctrl);
1024
1025 struct q931_call *q931_find_link_id_call(struct pri *ctrl, int link_id);
1026 struct q931_call *q931_find_held_active_call(struct pri *ctrl, struct q931_call *held_call);
1027
1028 int q931_request_subaddress(struct pri *ctrl, struct q931_call *call, int notify, const struct q931_party_name *name, const struct q931_party_number *number);
1029 int q931_subaddress_transfer(struct pri *ctrl, struct q931_call *call);
1030 int q931_notify_redirection(struct pri *ctrl, struct q931_call *call, int notify, const struct q931_party_name *name, const struct q931_party_number *number);
1031
1032 struct pri_cc_record *pri_cc_find_by_reference(struct pri *ctrl, unsigned reference_id);
1033 struct pri_cc_record *pri_cc_find_by_linkage(struct pri *ctrl, unsigned linkage_id);
1034 struct pri_cc_record *pri_cc_find_by_addressing(struct pri *ctrl, const struct q931_party_address *party_a, const struct q931_party_address *party_b, unsigned length, const unsigned char *q931_ies);
1035 struct pri_cc_record *pri_cc_new_record(struct pri *ctrl, q931_call *call);
1036 void pri_cc_qsig_determine_available(struct pri *ctrl, q931_call *call);
1037 const char *pri_cc_fsm_state_str(enum CC_STATES state);
1038 const char *pri_cc_fsm_event_str(enum CC_EVENTS event);
1039 int pri_cc_event(struct pri *ctrl, q931_call *call, struct pri_cc_record *cc_record, enum CC_EVENTS event);
1040 int q931_cc_timeout(struct pri *ctrl, struct pri_cc_record *cc_record, enum CC_EVENTS event);
1041 void q931_cc_indirect(struct pri *ctrl, struct pri_cc_record *cc_record, void (*func)(struct pri *ctrl, q931_call *call, struct pri_cc_record *cc_record));
1042
1043 /*!
1044 * \brief Get the NFAS master PRI control structure.
1045 *
1046 * \param ctrl D channel controller.
1047 *
1048 * \return NFAS master PRI control structure.
1049 */
PRI_NFAS_MASTER(struct pri * ctrl)1050 static inline struct pri *PRI_NFAS_MASTER(struct pri *ctrl)
1051 {
1052 if (ctrl->master) {
1053 ctrl = ctrl->master;
1054 }
1055 return ctrl;
1056 }
1057
1058 /*!
1059 * \brief Determine if layer 2 is in BRI NT PTMP mode.
1060 *
1061 * \param ctrl D channel controller.
1062 *
1063 * \retval TRUE if in BRI NT PTMP mode.
1064 * \retval FALSE otherwise.
1065 */
BRI_NT_PTMP(const struct pri * ctrl)1066 static inline int BRI_NT_PTMP(const struct pri *ctrl)
1067 {
1068 struct pri *my_ctrl = (struct pri *) ctrl;
1069
1070 return my_ctrl->bri && my_ctrl->localtype == PRI_NETWORK
1071 && my_ctrl->link.tei == Q921_TEI_GROUP;
1072 }
1073
1074 /*!
1075 * \brief Determine if layer 2 is in BRI TE PTMP mode.
1076 *
1077 * \param ctrl D channel controller.
1078 *
1079 * \retval TRUE if in BRI TE PTMP mode.
1080 * \retval FALSE otherwise.
1081 */
BRI_TE_PTMP(const struct pri * ctrl)1082 static inline int BRI_TE_PTMP(const struct pri *ctrl)
1083 {
1084 struct pri *my_ctrl = (struct pri *) ctrl;
1085
1086 return my_ctrl->bri && my_ctrl->localtype == PRI_CPE
1087 && my_ctrl->link.tei == Q921_TEI_GROUP;
1088 }
1089
1090 /*!
1091 * \brief Determine if layer 2 is in NT mode.
1092 *
1093 * \param ctrl D channel controller.
1094 *
1095 * \retval TRUE if in NT mode.
1096 * \retval FALSE otherwise.
1097 */
NT_MODE(const struct pri * ctrl)1098 static inline int NT_MODE(const struct pri *ctrl)
1099 {
1100 struct pri *my_ctrl = (struct pri *) ctrl;
1101
1102 return my_ctrl->localtype == PRI_NETWORK;
1103 }
1104
1105 /*!
1106 * \brief Determine if layer 2 is in TE mode.
1107 *
1108 * \param ctrl D channel controller.
1109 *
1110 * \retval TRUE if in TE mode.
1111 * \retval FALSE otherwise.
1112 */
TE_MODE(const struct pri * ctrl)1113 static inline int TE_MODE(const struct pri *ctrl)
1114 {
1115 struct pri *my_ctrl = (struct pri *) ctrl;
1116
1117 return my_ctrl->localtype == PRI_CPE;
1118 }
1119
1120 /*!
1121 * \brief Determine if layer 2 is in PTP mode.
1122 *
1123 * \param ctrl D channel controller.
1124 *
1125 * \retval TRUE if in PTP mode.
1126 * \retval FALSE otherwise.
1127 */
PTP_MODE(const struct pri * ctrl)1128 static inline int PTP_MODE(const struct pri *ctrl)
1129 {
1130 struct pri *my_ctrl = (struct pri *) ctrl;
1131
1132 return my_ctrl->link.tei == Q921_TEI_PRI;
1133 }
1134
1135 /*!
1136 * \brief Determine if layer 2 is in PTMP mode.
1137 *
1138 * \param ctrl D channel controller.
1139 *
1140 * \retval TRUE if in PTMP mode.
1141 * \retval FALSE otherwise.
1142 */
PTMP_MODE(const struct pri * ctrl)1143 static inline int PTMP_MODE(const struct pri *ctrl)
1144 {
1145 struct pri *my_ctrl = (struct pri *) ctrl;
1146
1147 return my_ctrl->link.tei == Q921_TEI_GROUP;
1148 }
1149
1150 #define Q931_DUMMY_CALL_REFERENCE -1
1151 #define Q931_CALL_REFERENCE_FLAG 0x8000 /* Identify which end allocted the CR. */
1152
1153 /*!
1154 * \brief Deterimine if the given call control pointer is a dummy call.
1155 *
1156 * \retval TRUE if given call is a dummy call.
1157 * \retval FALSE otherwise.
1158 */
q931_is_dummy_call(const struct q931_call * call)1159 static inline int q931_is_dummy_call(const struct q931_call *call)
1160 {
1161 return (call->cr == Q931_DUMMY_CALL_REFERENCE) ? 1 : 0;
1162 }
1163
get_invokeid(struct pri * ctrl)1164 static inline short get_invokeid(struct pri *ctrl)
1165 {
1166 return ++ctrl->last_invoke;
1167 }
1168
1169 #endif
1170