1 diff -urN libpri-1.2.3.orig/Makefile libpri-1.2.3/Makefile
2 --- libpri-1.2.3.orig/Makefile 2006-04-30 17:17:47.000000000 +0200
3 +++ libpri-1.2.3/Makefile 2006-07-27 17:45:09.000000000 +0200
5 # Uncomment if you want libpri to count number of Q921/Q931 sent/received
6 #LIBPRI_COUNTERS=-DLIBPRI_COUNTERS
8 +# Uncomment if you want libpri to always keep layer 2 up
9 +#LAYER2ALWAYSUP=-DLAYER2ALWAYSUP
11 +# Uncomment if you want libpri to hangup a call to an NT (p2mp) port if one
12 +# device sends a RELEASE COMPLETE with cause 17
13 +#FASTBUSYONBUSY=-DFASTBUSYONBUSY
17 OSARCH=$(shell uname -s)
19 DYNAMIC_LIBRARY=libpri.so.1.0
20 STATIC_OBJS=copy_string.o pri.o q921.o prisched.o q931.o pri_facility.o
21 DYNAMIC_OBJS=copy_string.lo pri.lo q921.lo prisched.lo q931.lo pri_facility.lo
22 -CFLAGS=-Wall -Werror -Wstrict-prototypes -Wmissing-prototypes -g $(ALERTING) $(LIBPRI_COUNTERS)
23 +CFLAGS=-Wall -Wstrict-prototypes -Wmissing-prototypes -g $(ALERTING) $(LIBPRI_COUNTERS) $(LAYER2ALWAYSUP) $(FASTBUSYONBUSY) -DRELAX_TRB
24 INSTALL_PREFIX=$(DESTDIR)
26 SOFLAGS = -Wl,-hlibpri.so.1.0
27 diff -urN libpri-1.2.3.orig/README libpri-1.2.3/README
28 --- libpri-1.2.3.orig/README 2006-02-15 18:59:38.000000000 +0100
29 +++ libpri-1.2.3/README 2006-01-18 12:28:07.000000000 +0100
31 -libpri: An implementation of Primary Rate ISDN
33 -Written by Mark Spencer <markster@digium.com>
34 +libpri: An implementation of Primate Rate ISDN (and BRI ISDN)
36 +Written by Mark Spencer <markster@linux-support.net>
37 +Modified for BRI support by Klaus-Peter Junghanns <kpj@junghanns.net>
42 based on the Bellcore specification SR-NWT-002343 for National ISDN. As of
43 May 12, 2001, it has been tested work with NI-2, Nortel DMS-100, and
44 Lucent 5E Custom protocols on switches from Nortel and Lucent.
45 +The BRI and euroISDN modifications are based on ETS 300 102-1.
47 What is the license for libpri?
48 ===============================
52 If you wish to use libpri in an application for which the GPL is not
53 -appropriate (e.g. a proprietary embedded system), licenses for libpri
54 -under more flexible terms can be readily obtained through Digium, Inc.
56 +appropriate (e.g. a proprietary embedded system), then you have to use
57 +a non-standard compliant version without BRI support.
60 How do I report bugs or contribute?
61 diff -urN libpri-1.2.3.orig/TODO libpri-1.2.3/TODO
62 --- libpri-1.2.3.orig/TODO 2005-11-29 19:39:18.000000000 +0100
63 +++ libpri-1.2.3/TODO 2006-01-18 12:28:07.000000000 +0100
69 --- Support unnumbered information frames
73 --- Implement the 11 missing Q.931 timers
74 +-- Locking Shift IE (you did that already, didnt you??)
75 +-- Implement the 10 missing Q.931 timers
77 diff -urN libpri-1.2.3.orig/libpri.h libpri-1.2.3/libpri.h
78 --- libpri-1.2.3.orig/libpri.h 2006-04-27 18:08:39.000000000 +0200
79 +++ libpri-1.2.3/libpri.h 2006-07-11 11:34:59.000000000 +0200
82 * Copyright (C) 2001, Linux Support Services, Inc.
83 * All Rights Reserved.
84 + * Copyright (C) 2003-2006 Junghanns.NET GmbH
85 + * Klaus-Peter Junghanns <kpj@junghanns.net>
87 * This program is free software; you can redistribute it and/or modify
88 * it under the terms of the GNU General Public License as published by
93 -#define PRI_NETWORK 1
94 +#define PRI_NETWORK 1 /* PTP modes, default for PRI */
96 +#define BRI_NETWORK_PTMP 3 /* PTMP modes, default for BRI */
97 +#define BRI_CPE_PTMP 4
98 +#define BRI_NETWORK 5 /* PTP modes */
102 #define PRI_DEBUG_Q921_RAW (1 << 0) /* Show raw HDLC frames */
104 #define PRI_EVENT_NOTIFY 16 /* Notification received */
105 #define PRI_EVENT_PROGRESS 17 /* When we get CALL_PROCEEDING or PROGRESS */
106 #define PRI_EVENT_KEYPAD_DIGIT 18 /* When we receive during ACTIVE state */
107 +#define PRI_EVENT_HOLD_REQ 19 /* R */
108 +#define PRI_EVENT_RETRIEVE_REQ 20
109 +#define PRI_EVENT_SUSPEND_REQ 21 /* park */
110 +#define PRI_EVENT_RESUME_REQ 22 /* unpark */
111 +#define PRI_EVENT_DISPLAY_RECEIVED 23
112 +#define PRI_EVENT_FACILITY 24 /* Facility */
115 #define PRI_STATE_DOWN 0
116 @@ -250,11 +262,17 @@
117 #define PRI_NSF_ATT_MULTIQUEST 0xF0
118 #define PRI_NSF_CALL_REDIRECTION_SERVICE 0xF7
121 +#define PRI_RELAX_TRB
124 +typedef struct q921_call q921_call;
125 typedef struct q931_call q931_call;
127 typedef struct pri_event_generic {
128 /* Events with no additional information fall in this category */
133 typedef struct pri_event_error {
134 @@ -273,18 +291,19 @@
139 char useruserinfo[260]; /* User->User info */
143 typedef struct pri_event_answer {
146 + int tei; /* belongs to this tei */
151 char useruserinfo[260]; /* User->User info */
155 typedef struct pri_event_facname {
156 @@ -302,32 +321,37 @@
158 int channel; /* Channel requested */
159 int callingpres; /* Presentation of Calling CallerID */
160 - int callingplanani; /* Dialing plan of Calling entity ANI */
161 + int callingpresuser; /* Presentation of Calling CallerID */
162 int callingplan; /* Dialing plan of Calling entity */
163 - char callingani[256]; /* Calling ANI */
164 - char callingnum[256]; /* Calling number */
165 + int callingplanuser; /* Dialing plan of Calling entity */
166 + int callingplanani; /* Dialing plan of Calling entity ANI */
167 + char callingnum[256]; /* Calling number, network provided */
168 + char callingani[256]; /* Calling number, user provided */
169 char callingname[256]; /* Calling name (if provided) */
170 int calledplan; /* Dialing plan of Called number */
171 int ani2; /* ANI II */
172 char callednum[256]; /* Called number */
173 - char redirectingnum[256]; /* Redirecting number */
174 - char redirectingname[256]; /* Redirecting name */
175 - int redirectingreason; /* Reason for redirect */
176 + char redirectingnum[256]; /* Redirecting number */
177 + char redirectingname[256]; /* Redirecting name */
178 + int redirectingreason; /* Reason for redirect */
179 int callingplanrdnis; /* Dialing plan of Redirecting Number */
180 - char useruserinfo[260]; /* User->User info */
181 + char useruserinfo[260]; /* User->User info */
182 int flexible; /* Are we flexible with our channel selection? */
183 int cref; /* Call Reference Number */
184 int ctype; /* Call type (see PRI_TRANS_CAP_* */
185 - int layer1; /* User layer 1 */
186 + int layer1; /* User layer 1 */
187 int complete; /* Have we seen "Complete" i.e. no more number? */
188 q931_call *call; /* Opaque call pointer */
189 - char callingsubaddr[256]; /* Calling parties subaddress */
190 + int tei; /* belongs to this tei */
191 + char callingsubaddr[256]; /* Calling parties subaddress */
194 char origcalledname[256];
195 char origcallednum[256];
196 int callingplanorigcalled; /* Dialing plan of Originally Called Number */
197 int origredirectingreason;
198 + char lowlayercompat[16];
199 + char highlayercompat[4];
202 typedef struct pri_event_hangup {
204 int channel; /* Channel requested */
208 + int inband_progress;
209 q931_call *call; /* Opaque call pointer */
210 long aoc_units; /* Advise of Charge number of charged units */
211 char useruserinfo[260]; /* User->User info */
212 @@ -375,20 +401,80 @@
214 } pri_event_keypad_digit;
216 +typedef struct pri_event_hold_req {
222 +} pri_event_hold_req;
224 +/* euroisdn faciltiy fun */
225 +typedef struct pri_event_facility_req {
231 + char forwardnum[256]; /* Redirection destination */
233 +} pri_event_facility_req;
235 +typedef struct pri_event_retrieve_req {
241 +} pri_event_retrieve_req;
243 +typedef struct pri_event_suspend_req {
250 +} pri_event_suspend_req;
252 +typedef struct pri_event_resume_req {
259 +} pri_event_resume_req;
261 +typedef struct pri_event_display {
267 +} pri_event_display;
272 pri_event_generic gen; /* Generic view */
273 pri_event_restart restart; /* Restart view */
274 pri_event_error err; /* Error view */
275 pri_event_facname facname; /* Caller*ID Name on Facility */
276 + pri_event_facility_req facility; /* sservices */
277 pri_event_ring ring; /* Ring */
278 pri_event_hangup hangup; /* Hang up */
279 pri_event_ringing ringing; /* Ringing */
280 - pri_event_ringing answer; /* Answer */
281 + pri_event_answer answer; /* Answer */
282 pri_event_restart_ack restartack; /* Restart Acknowledge */
283 pri_event_proceeding proceeding; /* Call proceeding & Progress */
284 pri_event_setup_ack setup_ack; /* SETUP_ACKNOWLEDGE structure */
285 pri_event_notify notify; /* Notification */
286 + pri_event_hold_req hold_req;
287 + pri_event_retrieve_req retrieve_req;
288 + pri_event_suspend_req suspend_req;
289 + pri_event_resume_req resume_req;
290 + pri_event_display display;
291 pri_event_keypad_digit digit; /* Digits that come during a call */
295 channel operating in HDLC mode with FCS computed by the fd's driver. Also it
296 must be NON-BLOCKING! Frames received on the fd should include FCS. Nodetype
297 must be one of PRI_NETWORK or PRI_CPE. switchtype should be PRI_SWITCH_* */
298 -extern struct pri *pri_new(int fd, int nodetype, int switchtype);
299 +extern struct pri *pri_new(int fd, int nodetype, int switchtype, int span);
301 +extern void pri_shutdown(struct pri *pri);
303 /* Create D-channel just as above with user defined I/O callbacks and data */
304 extern struct pri *pri_new_cb(int fd, int nodetype, int switchtype, pri_io_cb io_read, pri_io_cb io_write, void *userdata);
306 /* Enable transmission support of Facility IEs on the pri */
307 extern void pri_facility_enable(struct pri *pri);
309 +/* Set file descriptor for debugging to a file */
310 +extern void pri_set_debug_fd(struct pri *pri, int fd);
312 /* Run PRI on the given D-channel, taking care of any events that
313 need to be handled. If block is set, it will block until an event
314 occurs which needs to be handled */
316 /* Send a digit in overlap mode */
317 extern int pri_information(struct pri *pri, q931_call *call, char digit);
319 +/* Send a INFO msg with display ie */
320 +extern int pri_information_display(struct pri *pri, q931_call *call, char *display);
322 +/* add a display ie to a call, so it can be sent with the next message */
323 +extern int pri_add_display(struct pri *pri, q931_call *call, char *display);
325 /* Answer the incomplete(call without called number) call on the given channel.
326 Set non-isdn to non-zero if you are not connecting to ISDN equipment */
327 extern int pri_need_more_info(struct pri *pri, q931_call *call, int channel, int nonisdn);
329 Set non-isdn to non-zero if you are not connecting to ISDN equipment */
330 extern int pri_answer(struct pri *pri, q931_call *call, int channel, int nonisdn);
332 +extern int pri_deflect(struct pri *pri, q931_call *call, char *destination);
334 +/* Ack a HOLD_REQ */
335 +extern int pri_hold_acknowledge(struct pri *pri, q931_call *call);
337 +/* Reject a HOLD_REQ */
338 +extern int pri_hold_reject(struct pri *pri, q931_call *call);
340 +/* Ack a RETRIEVE_REQ */
341 +extern int pri_retrieve_acknowledge(struct pri *pri, q931_call *call, int channel);
343 +/* Reject a RETRIEVE_REQ */
344 +extern int pri_retrieve_reject(struct pri *pri, q931_call *call);
346 +/* Ack a SUSPEND_REQ */
347 +extern int pri_suspend_acknowledge(struct pri *pri, q931_call *call, char *display);
349 +/* Reject a SUSPEND_REQ */
350 +extern int pri_suspend_reject(struct pri *pri, q931_call *call, char *display);
352 +/* Reject a RESUME_REQ */
353 +extern int pri_resume_reject(struct pri *pri, q931_call *call, char *display);
355 +/* Ack a RESUME_REQ */
356 +extern int pri_resume_acknowledge(struct pri *pri, q931_call *call, int channel, char *display);
358 +/* Send a Facility Message */
359 +extern int pri_facility(struct pri *pri, q931_call *call, int operation, char *arguments);
361 /* Set CRV reference for GR-303 calls */
364 @@ -479,14 +605,14 @@
366 /* backwards compatibility for those who don't use asterisk with libpri */
367 #define pri_release(a,b,c) \
369 + pri_hangup(a,b,c, -1)
371 #define pri_disconnect(a,b,c) \
373 + pri_hangup(a,b,c, -1)
377 -extern int pri_hangup(struct pri *pri, q931_call *call, int cause);
378 +extern int pri_hangup(struct pri *pri, q931_call *call, int cause, int aocunits);
380 #define PRI_DESTROYCALL
381 extern void pri_destroycall(struct pri *pri, q931_call *call);
382 @@ -519,14 +645,13 @@
383 extern void pri_sr_free(struct pri_sr *sr);
385 extern int pri_sr_set_channel(struct pri_sr *sr, int channel, int exclusive, int nonisdn);
386 -extern int pri_sr_set_bearer(struct pri_sr *sr, int transmode, int userl1);
387 +extern int pri_sr_set_bearer(struct pri_sr *sr, int transmode, int userl1, char *llc);
388 extern int pri_sr_set_called(struct pri_sr *sr, char *called, int calledplan, int complete);
389 extern int pri_sr_set_caller(struct pri_sr *sr, char *caller, char *callername, int callerplan, int callerpres);
390 extern int pri_sr_set_redirecting(struct pri_sr *sr, char *num, int plan, int pres, int reason);
391 #define PRI_USER_USER_TX
392 /* Set the user user field. Warning! don't send binary data accross this field */
393 extern void pri_sr_set_useruser(struct pri_sr *sr, char *userchars);
395 extern void pri_call_set_useruser(q931_call *sr, char *userchars);
397 extern int pri_setup(struct pri *pri, q931_call *call, struct pri_sr *req);
400 /* Override message and error stuff */
401 #define PRI_NEW_SET_API
402 -extern void pri_set_message(void (*__pri_error)(struct pri *pri, char *));
403 -extern void pri_set_error(void (*__pri_error)(struct pri *pri, char *));
404 +extern void pri_set_message(void (*__pri_error)(char *, int span));
405 +extern void pri_set_error(void (*__pri_error)(char *, int span));
407 /* Set overlap mode */
408 #define PRI_SET_OVERLAPDIAL
409 diff -urN libpri-1.2.3.orig/pri.c libpri-1.2.3/pri.c
410 --- libpri-1.2.3.orig/pri.c 2005-11-29 19:39:18.000000000 +0100
411 +++ libpri-1.2.3/pri.c 2006-07-11 12:39:20.000000000 +0200
414 * libpri: An implementation of Primary Rate ISDN
416 - * Written by Mark Spencer <markster@digium.com>
417 + * Written by Mark Spencer <markster@linux-suppot.net>
419 - * Copyright (C) 2001-2005, Digium
420 - * All Rights Reserved.
421 + * This program is confidential ( <- I dont think so! )
423 - * This program is free software; you can redistribute it and/or modify
424 - * it under the terms of the GNU General Public License as published by
425 - * the Free Software Foundation; either version 2 of the License, or
426 - * (at your option) any later version.
428 - * This program is distributed in the hope that it will be useful,
429 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
430 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
431 - * GNU General Public License for more details.
433 - * You should have received a copy of the GNU General Public License
434 - * along with this program; if not, write to the Free Software
435 - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
436 + * Copyright (C) 2001, Linux Support Services, Inc.
437 + * All Rights Reserved.
438 + * Copyright (C) 2003-2006 Junghanns.NET GmbH
439 + * Klaus-Peter Junghanns <kpj@junghanns.net>
451 + case BRI_NETWORK_PTMP:
452 + return "Network (PtMP)";
454 + return "CPE (PtMP)";
456 return "Invalid value";
462 -static struct pri *__pri_new(int fd, int node, int switchtype, struct pri *master, pri_io_cb rd, pri_io_cb wr, void *userdata)
463 +static struct pri *__pri_new(int fd, int node, int switchtype, struct pri *master, pri_io_cb rd, pri_io_cb wr, void *userdata, int span)
466 p = malloc(sizeof(struct pri));
469 p->callpool = &p->localpool;
470 pri_default_timers(p, switchtype);
473 #ifdef LIBPRI_COUNTERS
477 p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
478 p->sapi = Q921_SAPI_GR303_EOC;
479 p->tei = Q921_TEI_GR303_EOC_OPS;
480 - p->subchannel = __pri_new(-1, node, PRI_SWITCH_GR303_EOC_PATH, p, NULL, NULL, NULL);
481 + p->subchannel = __pri_new(-1, node, PRI_SWITCH_GR303_EOC_PATH, p, NULL, NULL, NULL, span);
482 if (!p->subchannel) {
486 p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
487 p->sapi = Q921_SAPI_GR303_TMC_CALLPROC;
488 p->tei = Q921_TEI_GR303_TMC_CALLPROC;
489 - p->subchannel = __pri_new(-1, node, PRI_SWITCH_GR303_TMC_SWITCHING, p, NULL, NULL, NULL);
490 + p->subchannel = __pri_new(-1, node, PRI_SWITCH_GR303_TMC_SWITCHING, p, NULL, NULL, NULL, span);
491 if (!p->subchannel) {
496 /* Start Q.921 layer, Wait if we're the network */
498 - q921_start(p, p->localtype == PRI_CPE);
499 + q921_start(p, p->localtype == PRI_CPE, 0);
503 @@ -262,15 +262,16 @@
505 /* Restart Q.921 layer */
508 - q921_start(pri, pri->localtype == PRI_CPE);
509 +// XXX q921_reset(pri);
510 +// q921_start(pri, pri->localtype == PRI_CPE);
515 -struct pri *pri_new(int fd, int nodetype, int switchtype)
517 +struct pri *pri_new(int fd, int nodetype, int switchtype, int span)
519 - return __pri_new(fd, nodetype, switchtype, NULL, __pri_read, __pri_write, NULL);
520 + return __pri_new(fd, nodetype, switchtype, NULL, __pri_read, __pri_write, NULL, span);
523 struct pri *pri_new_cb(int fd, int nodetype, int switchtype, pri_io_cb io_read, pri_io_cb io_write, void *userdata)
525 io_read = __pri_read;
527 io_write = __pri_write;
528 - return __pri_new(fd, nodetype, switchtype, NULL, io_read, io_write, userdata);
529 + return __pri_new(fd, nodetype, switchtype, NULL, io_read, io_write, userdata, -1);
532 void *pri_get_userdata(struct pri *pri)
537 +void pri_set_debug_fd(struct pri *pri, int fd)
542 + if (pri->subchannel)
543 + pri_set_debug_fd(pri->subchannel, fd);
546 int pri_acknowledge(struct pri *pri, q931_call *call, int channel, int info)
550 return q931_notify(pri, call, channel, info);
553 +int pri_information_display(struct pri *pri, q931_call *call, char *display)
557 + return q931_information_display(pri, call, display);
560 +int pri_add_display(struct pri *pri, q931_call *call, char *display)
564 + return q931_add_display(pri, call, display);
568 void pri_destroycall(struct pri *pri, q931_call *call)
572 return q931_connect(pri, call, channel, nonisdn);
575 +int pri_hold_acknowledge(struct pri *pri, q931_call *call)
579 + return q931_hold_acknowledge(pri, call);
582 +int pri_hold_reject(struct pri *pri, q931_call *call)
586 + return q931_hold_reject(pri, call);
589 +int pri_retrieve_acknowledge(struct pri *pri, q931_call *call, int channel)
593 + return q931_retrieve_acknowledge(pri, call, channel);
596 +int pri_retrieve_reject(struct pri *pri, q931_call *call)
600 + return q931_retrieve_reject(pri, call);
603 +int pri_suspend_acknowledge(struct pri *pri, q931_call *call, char *display)
607 + return q931_suspend_acknowledge(pri, call, display);
610 +int pri_suspend_reject(struct pri *pri, q931_call *call, char *display)
614 + return q931_suspend_reject(pri, call, display);
617 +int pri_resume_reject(struct pri *pri, q931_call *call, char *display)
621 + return q931_resume_reject(pri, call, display);
624 +int pri_resume_acknowledge(struct pri *pri, q931_call *call, int channel, char *display)
628 + return q931_resume_acknowledge(pri, call, channel, display);
631 +int pri_facility(struct pri *pri, q931_call *call, int operation, char *arguments)
635 +// return q931_facility(pri, call, operation, arguments);
636 + return q931_facility(pri, call);
639 +int pri_deflect(struct pri *pri, q931_call *call, char *destination)
641 + add_call_deflection_facility_ie(pri, call, destination);
642 + return q931_facility(pri, call);
646 /* deprecated routines, use pri_hangup */
647 int pri_release(struct pri *pri, q931_call *call, int cause)
648 @@ -541,14 +636,35 @@
652 -int pri_hangup(struct pri *pri, q931_call *call, int cause)
653 +int pri_hangup(struct pri *pri, q931_call *call, int cause, int aocunits)
659 /* normal clear cause */
661 - return q931_hangup(pri, call, cause);
662 + if ((cause == 34 || cause == 44 || cause == 82 || cause == 1 || cause == 81 || cause == 17) && (call->ourcallstate == Q931_CALL_STATE_ACTIVE)) {
663 + pri_error(pri, "Cause code %d not allowed when disconnecting an active call. Changing to cause 16.\n", cause);
667 + if (aocunits > -1) {
668 + call->aoc_units = aocunits;
671 + if (pri->localtype == BRI_NETWORK_PTMP) {
672 + res = q921_hangup(pri, call, 127);
674 + // q921_setup might give a HANGUP_ACK, if nobody got the call
675 + q931_hangup(pri, call, cause);
678 + return q931_hangup(pri, call, cause);
681 + return q931_hangup(pri, call, cause);
685 int pri_reset(struct pri *pri, int channel)
686 @@ -688,15 +804,15 @@
687 return q931_setup(pri, c, &req);
690 -static void (*__pri_error)(struct pri *pri, char *stuff);
691 -static void (*__pri_message)(struct pri *pri, char *stuff);
692 +static void (*__pri_error)(char *stuff,int span);
693 +static void (*__pri_message)(char *stuff,int span);
695 -void pri_set_message(void (*func)(struct pri *pri, char *stuff))
696 +void pri_set_message(void (*func)(char *stuff,int span))
698 __pri_message = func;
701 -void pri_set_error(void (*func)(struct pri *pri, char *stuff))
702 +void pri_set_error(void (*func)(char *stuff,int span))
706 @@ -708,10 +824,14 @@
708 vsnprintf(tmp, sizeof(tmp), fmt, ap);
711 - __pri_message(pri, tmp);
713 - fputs(tmp, stdout);
714 + if (__pri_message && pri) {
715 + if (pri->debugfd >= 0)
716 + write(pri->debugfd, tmp, strlen(tmp));
718 + __pri_message(tmp, pri->span);
720 + fputs(tmp, stdout);
724 void pri_error(struct pri *pri, char *fmt, ...)
725 @@ -721,10 +841,14 @@
727 vsnprintf(tmp, sizeof(tmp), fmt, ap);
730 - __pri_error(pri, tmp);
732 - fputs(tmp, stderr);
733 + if (__pri_error && pri) {
734 + if (pri->debugfd >= 0)
735 + write(pri->debugfd, tmp, strlen(tmp));
737 + __pri_error(tmp, pri->span);
739 + fputs(tmp, stderr);
743 /* Set overlap mode */
744 @@ -765,11 +889,13 @@
746 len += sprintf(buf + len, "Q921 Outstanding: %d\n", q921outstanding);
748 - len += sprintf(buf + len, "Window Length: %d/%d\n", pri->windowlen, pri->window);
749 - len += sprintf(buf + len, "Sentrej: %d\n", pri->sentrej);
750 - len += sprintf(buf + len, "SolicitFbit: %d\n", pri->solicitfbit);
751 - len += sprintf(buf + len, "Retrans: %d\n", pri->retrans);
752 - len += sprintf(buf + len, "Busy: %d\n", pri->busy);
753 + if (pri->localtype != BRI_NETWORK_PTMP) {
754 + len += sprintf(buf + len, "Window Length: %d/%d\n", pri->windowlen[0], pri->window[0]);
755 + len += sprintf(buf + len, "Sentrej: %d\n", pri->sentrej[0]);
756 + len += sprintf(buf + len, "SolicitFbit: %d\n", pri->solicitfbit[0]);
757 + len += sprintf(buf + len, "Retrans: %d\n", pri->retrans[0]);
758 + len += sprintf(buf + len, "Busy: %d\n", pri->busy[0]);
760 len += sprintf(buf + len, "Overlap Dial: %d\n", pri->overlapdial);
761 len += sprintf(buf + len, "T200 Timer: %d\n", pri->timers[PRI_TIMER_T200]);
762 len += sprintf(buf + len, "T203 Timer: %d\n", pri->timers[PRI_TIMER_T203]);
764 len += sprintf(buf + len, "T313 Timer: %d\n", pri->timers[PRI_TIMER_T313]);
765 len += sprintf(buf + len, "N200 Counter: %d\n", pri->timers[PRI_TIMER_N200]);
771 @@ -819,10 +946,11 @@
775 -int pri_sr_set_bearer(struct pri_sr *sr, int transmode, int userl1)
776 +int pri_sr_set_bearer(struct pri_sr *sr, int transmode, int userl1, char *llc)
778 sr->transmode = transmode;
785 sr->redirectingreason = reason;
789 +void pri_shutdown(struct pri *pri)
791 +#ifndef LAYER2ALWAYSUP
793 + if ((pri->localtype == BRI_NETWORK) || (pri->localtype == BRI_CPE) || (pri->localtype == BRI_CPE_PTMP)) {
794 + q921_reset(pri, pri->tei, 1);
799 diff -urN libpri-1.2.3.orig/pri_facility.c libpri-1.2.3/pri_facility.c
800 --- libpri-1.2.3.orig/pri_facility.c 2006-02-14 00:06:02.000000000 +0100
801 +++ libpri-1.2.3/pri_facility.c 2006-06-06 14:26:01.000000000 +0200
804 - * libpri: An implementation of Primary Rate ISDN
806 - * Written by Matthew Fredrickson <creslin@digium.com>
808 - * Copyright (C) 2004-2005, Digium
809 - * All Rights Reserved.
811 - * This program is free software; you can redistribute it and/or modify
812 - * it under the terms of the GNU General Public License as published by
813 - * the Free Software Foundation; either version 2 of the License, or
814 - * (at your option) any later version.
816 - * This program is distributed in the hope that it will be useful,
817 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
818 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
819 - * GNU General Public License for more details.
821 - * You should have received a copy of the GNU General Public License
822 - * along with this program; if not, write to the Free Software
823 - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
827 + This file and it's contents are licensed under the terms and conditions
828 + of the GNU Public License. See http://www.gnu.org for details.
830 + Routines for dealing with facility messages and their respective
833 + by Matthew Fredrickson <creslin@digium.com>
834 + Copyright (C) 2004-2005 Digium, Inc
836 + Copyright (C) 2005-2006 Junghanns.NET GmbH
837 + Klaus-Peter Junghanns <kpj@junghanns.net>
844 if (datalen > buflen) {
847 + memcpy(namebuf, comp->data, datalen);
849 - memcpy(namebuf, comp->data, datalen);
854 int asn1_string_encode(unsigned char asn1_type, void *data, int len, int max_len, void *src, int src_len)
855 @@ -305,12 +296,55 @@
866 +static int rose_cd_destination_decode(struct pri *pri, q931_call *call, unsigned char *data, int len)
868 + unsigned char *vdata = data;
869 + struct rose_component *comp1 = NULL, *comp2 = NULL;
870 + int pos1 = 0, pos2, sublen2;
872 + if (pri->debug & PRI_DEBUG_AOC)
873 + dump_apdu (pri, data, len);
876 + GET_COMPONENT(comp1, pos1, vdata, len);
877 + CHECK_COMPONENT(comp1, ASN1_SEQUENCE, "!! Invalid CD destination argument. Expected Sequence (0x30) but Received 0x%02X\n");
878 + SUB_COMPONENT(comp1, pos1);
879 + GET_COMPONENT(comp1, pos1, vdata, len);
880 + switch (comp1->type) {
881 + case (ASN1_SEQUENCE | ASN1_CONSTRUCTOR):
882 + sublen2 = comp1->len;
885 + SUB_COMPONENT(comp2, pos2);
887 + GET_COMPONENT(comp2, pos2, vdata, len);
888 + switch (comp2->type) {
889 + case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0):
890 + memcpy(call->redirectingnum, comp2->data, comp2->len);
891 + call->redirectingnum[comp2->len] = '\0';
895 + pri_message(pri, "!! Don't know how to handle 0x%02X in CD destination argument\n", comp2->type);
897 + NEXT_COMPONENT(comp2, pos2);
898 + } while (pos2 < sublen2);
901 + pri_message(pri, "!! Invalid CD destination argument. Expected Sequence (0x30) or Object Identifier (0x81/0x01) but received 0x%02X\n", comp1->type);
903 + NEXT_COMPONENT(comp1, pos1);
904 + } while (pos1 < len);
909 static int rose_address_decode(struct pri *pri, q931_call *call, unsigned char *data, int len, struct addressingdataelements_presentednumberunscreened *value)
912 @@ -375,11 +409,10 @@
913 pri_message(pri, "!! Unknown Party number component received 0x%X\n", comp->type);
916 - ASN1_FIXUP_LEN(comp, res);
917 NEXT_COMPONENT(comp, i);
919 pri_message(pri, "!! not all information is handled from Address component\n");
926 static int rose_presented_number_unscreened_decode(struct pri *pri, q931_call *call, unsigned char *data, int len, struct addressingdataelements_presentednumberunscreened *value)
930 struct rose_component *comp = NULL;
931 unsigned char *vdata = data;
935 case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0): /* [0] presentationAllowedNumber */
936 value->pres = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
937 - size = rose_address_decode(pri, call, comp->data, comp->len, value);
938 - ASN1_FIXUP_LEN(comp, size);
940 + return rose_address_decode(pri, call, comp->data, comp->len, value) + 2;
941 case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_1): /* [1] IMPLICIT presentationRestricted */
942 if (comp->len != 0) { /* must be NULL */
943 pri_error(pri, "!! Invalid PresentationRestricted component received (len != 0)\n");
946 case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3): /* [3] presentationRestrictedNumber */
947 value->pres = PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
948 - size = rose_address_decode(pri, call, comp->data, comp->len, value) + 2;
949 - ASN1_FIXUP_LEN(comp, size);
951 + return rose_address_decode(pri, call, comp->data, comp->len, value) + 2;
953 pri_message(pri, "Invalid PresentedNumberUnscreened component 0x%X\n", comp->type);
959 -static int rose_diverting_leg_information2_decode(struct pri *pri, q931_call *call, struct rose_component *sequence, int len)
960 +static int rose_diverting_leg_information2_decode(struct pri *pri, q931_call *call, unsigned char *data, int len)
963 int diversion_counter;
965 struct addressingdataelements_presentednumberunscreened divertingnr;
966 struct addressingdataelements_presentednumberunscreened originalcallednr;
967 struct rose_component *comp = NULL;
968 - unsigned char *vdata = sequence->data;
969 + unsigned char *vdata = data;
973 - if (sequence->type != (ASN1_CONSTRUCTOR | ASN1_SEQUENCE)) { /* Constructed Sequence */
974 - pri_message(pri, "Invalid DivertingLegInformation2Type argument\n");
978 - if (sequence->len == ASN1_LEN_INDEF) {
979 - len -= 4; /* For the 2 extra characters at the end
980 - * and two characters of header */
985 /* diversionCounter stuff */
986 GET_COMPONENT(comp, i, vdata, len);
987 @@ -477,20 +493,18 @@
989 if(pri->debug & PRI_DEBUG_APDU)
990 pri_message(pri, " Redirection reason: %d, total diversions: %d\n", diversion_reason, diversion_counter);
991 - pri_message(NULL, "Length of message is %d\n", len);
993 for(; i < len; NEXT_COMPONENT(comp, i)) {
994 GET_COMPONENT(comp, i, vdata, len);
995 - switch(comp->type) {
996 - case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0):
997 + switch(comp->type & ASN1_TYPE_MASK) {
999 call->origredirectingreason = redirectingreason_for_q931(pri, comp->data[0]);
1000 if (pri->debug & PRI_DEBUG_APDU)
1001 pri_message(pri, " Received reason for original redirection %d\n", call->origredirectingreason);
1003 - case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1):
1004 + case ASN1_TAG_1: /* divertingnr: presentednumberunscreened */
1005 res = rose_presented_number_unscreened_decode(pri, call, comp->data, comp->len, &divertingnr);
1006 /* TODO: Fix indefinite length form hacks */
1007 - ASN1_FIXUP_LEN(comp, res);
1011 @@ -499,43 +513,33 @@
1012 pri_message(pri, " ton = %d, pres = %d, npi = %d\n", divertingnr.ton, divertingnr.pres, divertingnr.npi);
1015 - case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_2):
1016 + case ASN1_TAG_2: /* originalCalledNr: PresentedNumberUnscreened */
1017 res = rose_presented_number_unscreened_decode(pri, call, comp->data, comp->len, &originalcallednr);
1020 - ASN1_FIXUP_LEN(comp, res);
1022 if (pri->debug & PRI_DEBUG_APDU) {
1023 pri_message(pri, " Received originalcallednr '%s'\n", originalcallednr.partyaddress);
1024 pri_message(pri, " ton = %d, pres = %d, npi = %d\n", originalcallednr.ton, originalcallednr.pres, originalcallednr.npi);
1027 - case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3):
1028 - res = asn1_name_decode(comp->data, comp->len, redirectingname, sizeof(redirectingname));
1031 - ASN1_FIXUP_LEN(comp, res);
1034 + comp->len = asn1_name_decode(comp->data, comp->len, redirectingname, sizeof(redirectingname));
1035 if (pri->debug & PRI_DEBUG_APDU)
1036 pri_message(pri, " Received RedirectingName '%s'\n", redirectingname);
1038 - case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_4):
1039 - res = asn1_name_decode(comp->data, comp->len, origcalledname, sizeof(origcalledname));
1042 - ASN1_FIXUP_LEN(comp, res);
1045 + comp->len = asn1_name_decode(comp->data, comp->len, origcalledname, sizeof(origcalledname));
1046 if (pri->debug & PRI_DEBUG_APDU)
1047 pri_message(pri, " Received Originally Called Name '%s'\n", origcalledname);
1050 - if (comp->type == 0 && comp->len == 0) {
1051 - break; /* Found termination characters */
1053 pri_message(pri, "!! Invalid DivertingLegInformation2 component received 0x%X\n", comp->type);
1058 + return -1; /* Aborted before */
1060 if (divertingnr.pres >= 0) {
1061 call->redirectingplan = divertingnr.npi;
1062 @@ -548,15 +552,19 @@
1063 call->origcalledpres = originalcallednr.pres;
1064 libpri_copy_string(call->origcallednum, originalcallednr.partyaddress, sizeof(call->origcallednum));
1066 - libpri_copy_string(call->redirectingname, redirectingname, sizeof(call->redirectingname));
1067 - libpri_copy_string(call->origcalledname, origcalledname, sizeof(call->origcalledname));
1068 + if (strlen(redirectingname) > 0) {
1069 + libpri_copy_string(call->redirectingname, redirectingname, sizeof(call->redirectingname));
1071 + if (strlen(origcalledname) > 0) {
1072 + libpri_copy_string(call->origcalledname, origcalledname, sizeof(call->origcalledname));
1082 static int rose_diverting_leg_information2_encode(struct pri *pri, q931_call *call)
1084 int i = 0, j, compsp = 0;
1085 @@ -694,6 +702,64 @@
1089 +/* Call deflection */
1090 +int add_call_deflection_facility_ie(struct pri *pri, q931_call *c, char *destination) {
1091 + int i = 0, j, compsp = 0;
1092 + struct rose_component *comp, *compstk[10];
1093 + unsigned char buffer[256];
1095 + buffer[i++] = (ASN1_CONTEXT_SPECIFIC | Q932_PROTOCOL_ROSE);
1097 + ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, buffer, i);
1098 + ASN1_PUSH(compstk, compsp, comp);
1100 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, get_invokeid(pri));
1101 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, ROSE_CALLDEFLECTION);
1103 + /* Argument sequence */
1104 + ASN1_ADD_SIMPLE(comp, (ASN1_CONSTRUCTOR | ASN1_SEQUENCE), buffer, i);
1105 + ASN1_PUSH(compstk, compsp, comp);
1108 + ASN1_ADD_SIMPLE(comp, (ASN1_CONSTRUCTOR | ASN1_SEQUENCE), buffer, i);
1109 + ASN1_PUSH(compstk, compsp, comp);
1111 +#ifndef CD_UNLIKE_IN_CAPI
1112 + /* arg.address.PartyNumber */
1115 + j = asn1_string_encode((ASN1_CONTEXT_SPECIFIC|ASN1_TAG_0), &buffer[i], sizeof(buffer)-i, 20, destination, strlen(destination));
1116 + if (j<0) return -1;
1119 + /* using PublicPartyNumber instead */
1120 + ASN1_ADD_SIMPLE(comp, (ASN1_CONSTRUCTOR | ASN1_SEQUENCE | ASN1_CONTEXT_SPECIFIC| ASN1_TAG_1), buffer, i);
1121 + ASN1_PUSH(compstk, compsp, comp);
1122 + /* ToN: unknown */
1123 + ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, buffer, i, 0);
1124 + j = asn1_string_encode(0x80, &buffer[i], sizeof(buffer)-i, 20, destination, strlen(destination));
1125 + if(j<0) return -1;
1127 + /* close PublicPartyNumber */
1128 + ASN1_FIXUP(compstk, compsp, buffer, i);
1131 + /* close Address */
1132 + ASN1_FIXUP(compstk, compsp, buffer, i);
1135 + ASN1_ADD_BYTECOMP(comp, ASN1_BOOLEAN, buffer, i, 0);
1137 + /* Fix length of stacked components */
1138 + while(compsp > 0) {
1139 + ASN1_FIXUP(compstk, compsp, buffer, i);
1141 + if (pri_call_apdu_queue(c, Q931_FACILITY, buffer, i, NULL, NULL))
1147 /* Sending callername information functions */
1148 static int add_callername_facility_ies(struct pri *pri, q931_call *c, int cpe)
1150 @@ -936,7 +1002,7 @@
1151 CHECK_COMPONENT(comp, ASN1_ENUMERATED, "!! Invalid AOC Charging Request argument. Expected Enumerated (0x0A) but Received 0x%02X\n");
1152 ASN1_GET_INTEGER(comp, chargingcase);
1153 if (chargingcase >= 0 && chargingcase <= 2) {
1154 - if (pri->debug & PRI_DEBUG_APDU)
1155 +// if (pri->debug & PRI_DEBUG_APDU)
1156 pri_message(pri, "Channel %d/%d, Call %d - received AOC charging request - charging case: %i\n",
1157 call->ds1no, call->channelno, call->cr, chargingcase);
1159 @@ -1054,7 +1120,7 @@
1163 -static int aoc_aoce_charging_unit_encode(struct pri *pri, q931_call *c, long chargedunits)
1164 +int aoc_aoce_charging_unit_encode(struct pri *pri, q931_call *c, long chargedunits, int msgtype)
1166 /* sample data: [ 91 a1 12 02 02 3a 78 02 01 24 30 09 30 07 a1 05 30 03 02 01 01 ] */
1167 int i = 0, res = 0, compsp = 0;
1168 @@ -1108,18 +1174,20 @@
1169 dump_apdu (pri, buffer, i);
1171 /* code below is untested */
1172 - res = pri_call_apdu_queue(c, Q931_FACILITY, buffer, i, NULL, NULL);
1173 + res = pri_call_apdu_queue(c, msgtype, buffer, i, NULL, NULL);
1175 pri_message(pri, "Could not queue ADPU in facility message\n");
1179 - /* Remember that if we queue a facility IE for a facility message we
1180 - * have to explicitly send the facility message ourselves */
1181 - res = q931_facility(c->pri, c);
1183 + if (msgtype == Q931_FACILITY) {
1184 + /* Remember that if we queue a facility IE for a facility message we
1185 + * have to explicitly send the facility message ourselves */
1186 + res = q931_facility(c->pri, c);
1188 pri_message(pri, "Could not schedule facility message for call %d\n", c->cr);
1194 @@ -1152,13 +1220,15 @@
1195 NEXT_COMPONENT(comp, i);
1197 /* No argument - return with error */
1199 + if ((i >= len) && (operation_tag != ROSE_EXPLICIT_CALL_TRANSFER))
1202 - /* Arguement Tag */
1203 - GET_COMPONENT(comp, i, vdata, len);
1206 + if (operation_tag != ROSE_EXPLICIT_CALL_TRANSFER) {
1207 + /* Arguement Tag */
1208 + GET_COMPONENT(comp, i, vdata, len);
1213 if (pri->debug & PRI_DEBUG_APDU)
1214 pri_message(pri, " [ Handling operation %d ]\n", operation_tag);
1215 @@ -1182,7 +1252,11 @@
1216 case ROSE_DIVERTING_LEG_INFORMATION2:
1217 if (pri->debug & PRI_DEBUG_APDU)
1218 pri_message(pri, " Handle DivertingLegInformation2\n");
1219 - return rose_diverting_leg_information2_decode(pri, call, comp, len-i);
1220 + if (comp->type != (ASN1_CONSTRUCTOR | ASN1_SEQUENCE)) { /* Constructed Sequence */
1221 + pri_message(pri, "Invalid DivertingLegInformation2Type argument\n");
1224 + return rose_diverting_leg_information2_decode(pri, call, comp->data, comp->len);
1225 case ROSE_AOC_NO_CHARGING_INFO_AVAILABLE:
1226 if (pri->debug & PRI_DEBUG_APDU) {
1227 pri_message(pri, "ROSE %i: AOC No Charging Info Available - not handled!", operation_tag);
1228 @@ -1210,6 +1284,7 @@
1231 case ROSE_AOC_AOCD_CHARGING_UNIT:
1232 +// return aoc_aoce_charging_unit_decode(pri, call, (u_int8_t *)comp, comp->len + 2);
1233 if (pri->debug & PRI_DEBUG_APDU) {
1234 pri_message(pri, "ROSE %i: AOC-D Charging Unit - not handled!", operation_tag);
1235 dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
1236 @@ -1224,7 +1299,7 @@
1237 case ROSE_AOC_AOCE_CHARGING_UNIT:
1238 return aoc_aoce_charging_unit_decode(pri, call, (u_int8_t *)comp, comp->len + 2);
1239 if (0) { /* the following function is currently not used - just to make the compiler happy */
1240 - aoc_aoce_charging_unit_encode(pri, call, call->aoc_units); /* use this function to forward the aoc-e on a bridged channel */
1241 + aoc_aoce_charging_unit_encode(pri, call, call->aoc_units, 1); /* use this function to forward the aoc-e on a bridged channel */
1244 case ROSE_AOC_IDENTIFICATION_OF_CHARGE:
1245 @@ -1233,6 +1308,22 @@
1246 dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
1249 + case ROSE_CALLDEFLECTION:
1250 + call->facility = operation_tag;
1251 + return rose_cd_destination_decode(pri, call, (u_int8_t *)comp, comp->len + 2);
1253 + case ROSE_EXPLICIT_CALL_TRANSFER:
1254 + call->facility = operation_tag;
1255 + if (pri->debug & PRI_DEBUG_APDU) {
1256 + pri_message(pri, "ROSE %i: received ECT execute!", operation_tag);
1259 + case ROSE_MALICIOUS_CID:
1260 +// call->facility = operation_tag;
1261 +// if (pri->debug & PRI_DEBUG_APDU) {
1262 + pri_message(pri, "ROSE %i: received MALICIOUS CID!", operation_tag);
1266 if (pri->debug & PRI_DEBUG_APDU) {
1267 pri_message(pri, "!! Unable to handle ROSE operation %d", operation_tag);
1268 diff -urN libpri-1.2.3.orig/pri_facility.h libpri-1.2.3/pri_facility.h
1269 --- libpri-1.2.3.orig/pri_facility.h 2005-11-29 19:39:18.000000000 +0100
1270 +++ libpri-1.2.3/pri_facility.h 2006-04-15 21:35:05.000000000 +0200
1272 /* Operation ID values */
1273 /* Q.952 ROSE operations (Diverting) */
1274 #define ROSE_DIVERTING_LEG_INFORMATION1 18
1275 -#define ROSE_DIVERTING_LEG_INFORMATION2 0x15
1276 +#define ROSE_DIVERTING_LEG_INFORMATION2 15
1277 #define ROSE_DIVERTING_LEG_INFORMATION3 19
1278 /* Q.956 ROSE operations (Advice Of Charge) */
1279 #define ROSE_AOC_NO_CHARGING_INFO_AVAILABLE 26
1281 #define ROSE_AOC_IDENTIFICATION_OF_CHARGE 37
1282 /* Q.SIG operations */
1283 #define SS_CNID_CALLINGNAME 0
1284 -#define SS_DIVERTING_LEG_INFORMATION2 21
1285 +#define SS_DIVERTING_LEG_INFORMATION2 22
1286 #define SS_MWI_ACTIVATE 80
1287 #define SS_MWI_DEACTIVATE 81
1288 #define SS_MWI_INTERROGATE 82
1290 +#define ROSE_CALLDEFLECTION 0x0D
1291 +#define ROSE_EXPLICIT_CALL_TRANSFER 0x06
1292 +#define ROSE_MALICIOUS_CID 0x31
1294 /* ROSE definitions and data structures */
1295 #define INVOKE_IDENTIFIER 0x02
1296 #define INVOKE_LINKED_IDENTIFIER 0x80
1297 @@ -180,12 +184,6 @@
1298 (variable) = ((variable) << 8) | (component)->data[comp_idx]; \
1301 -#define ASN1_FIXUP_LEN(component, size) \
1303 - if ((component)->len == ASN1_LEN_INDEF) \
1307 #define ASN1_ADD_SIMPLE(component, comptype, ptr, idx) \
1309 (component) = (struct rose_component *)&((ptr)[(idx)]); \
1311 /* Adds the "standard" ADPUs to a call */
1312 extern int pri_call_add_standard_apdus(struct pri *pri, q931_call *call);
1314 +extern int add_call_deflection_facility_ie(struct pri *pri, q931_call *c, char *destination);
1316 +extern int aoc_aoce_charging_unit_encode(struct pri *pri, q931_call *c, long chargedunits, int send_facility_message);
1318 #endif /* _PRI_FACILITY_H */
1319 diff -urN libpri-1.2.3.orig/pri_internal.h libpri-1.2.3/pri_internal.h
1320 --- libpri-1.2.3.orig/pri_internal.h 2005-11-29 19:39:18.000000000 +0100
1321 +++ libpri-1.2.3/pri_internal.h 2006-04-15 21:57:19.000000000 +0200
1324 * Copyright (C) 2001, Linux Support Services, Inc.
1325 * All Rights Reserved.
1326 + * Copyright (C) 2003-2006 Junghanns.NET GmbH
1327 + * Klaus-Peter Junghanns <kpj@junghanns.net>
1329 * This program is free software; you can redistribute it and/or modify
1330 * it under the terms of the GNU General Public License as published by
1333 struct timeval when;
1334 void (*callback)(void *data);
1335 + void (*callback2)(void *data, int);
1345 /* No more than 128 scheduled events */
1346 +/* XXX is this sufficient for nfs ??? */
1347 #define MAX_SCHED 128
1349 +/* this can be freely configured to support more devices .... ok, 63 would be max! */
1350 +#define Q921_MAX_TEIS 16
1352 +/* dynamically allocated TEIs start here */
1353 +#define Q921_TEI_BASE 64
1355 #define MAX_TIMERS 32
1359 struct pri *master; /* Master channel if appropriate */
1360 struct pri_sched pri_sched[MAX_SCHED]; /* Scheduled events */
1361 int debug; /* Debug stuff */
1363 int state; /* State of D-channel */
1364 int switchtype; /* Switch type */
1365 int nsf; /* Network-Specific Facility (if any) */
1371 - int window; /* Max window size */
1372 - int windowlen; /* Fullness of window */
1373 - int v_s; /* Next N(S) for transmission */
1374 - int v_a; /* Last acknowledged frame */
1375 - int v_r; /* Next frame expected to be received */
1376 - int v_na; /* What we've told our peer we've acknowledged */
1377 - int solicitfbit; /* Have we sent an I or S frame with the F-bit set? */
1378 - int retrans; /* Retransmissions */
1379 - int sentrej; /* Are we in reject state */
1381 + int q921_state[Q921_MAX_TEIS];
1384 + /* TEI registry */
1385 + char q921_teis[Q921_MAX_TEIS];
1387 + char q921_tei_check[Q921_MAX_TEIS];
1388 + unsigned short q921_tei_check_ri[Q921_MAX_TEIS];
1392 + int busy[Q921_MAX_TEIS]; /* Peer is busy */
1394 + int window[Q921_MAX_TEIS]; /* Max window size */
1395 + int windowlen[Q921_MAX_TEIS]; /* Fullness of window */
1396 + int v_s[Q921_MAX_TEIS]; /* Next N(S) for transmission */
1397 + int v_a[Q921_MAX_TEIS]; /* Last acknowledged frame */
1398 + int v_r[Q921_MAX_TEIS]; /* Next frame expected to be received */
1399 + int v_na[Q921_MAX_TEIS]; /* What we've told our peer we've acknowledged */
1400 + int solicitfbit[Q921_MAX_TEIS]; /* Have we sent an I or S frame with the F-bit set? */
1401 + int retrans[Q921_MAX_TEIS]; /* Retransmissions */
1402 + int sabme_retrans[Q921_MAX_TEIS]; /* Retransmissions */
1404 + int sentrej[Q921_MAX_TEIS]; /* Are we in reject state */
1406 + /* Various timers */
1407 + int sabme_timer[Q921_MAX_TEIS];
1408 + int t203_timer[Q921_MAX_TEIS];
1409 + int t202_timer[Q921_MAX_TEIS];
1411 + int t201_timer[Q921_MAX_TEIS];
1412 + int t200_timer[Q921_MAX_TEIS];
1415 int cref; /* Next call reference value */
1417 - int busy; /* Peer is busy */
1419 - /* Various timers */
1420 - int sabme_timer; /* SABME retransmit */
1421 - int t203_timer; /* Max idle time */
1422 - int t200_timer; /* T-200 retransmission timer */
1423 /* All ISDN Timer values */
1424 int timers[MAX_TIMERS];
1428 pri_event ev; /* Static event thingy */
1430 - /* Q.921 Re-transmission queue */
1431 - struct q921_frame *txqueue;
1432 + /* Q.921 (Re)transmission queue */
1433 + struct q921_frame *txqueue[Q921_MAX_TEIS];
1436 q931_call **callpool;
1439 unsigned char last_invoke; /* Last ROSE invoke ID */
1440 unsigned char sendfacility;
1442 + int span; /* our fellow pri lives on this zaptel span */
1451 + int callerplanani;
1456 int redirectingreason;
1462 /* Internal switch types */
1463 @@ -167,8 +202,13 @@
1465 int alive; /* Whether or not the call is alive */
1466 int acked; /* Whether setup has been acked or not */
1467 + int con_acked; /* Whether CONNECT has been CONNECT_ACKNOWLEDGEd or not */
1468 int sendhangupack; /* Whether or not to send a hangup ack */
1469 int proc; /* Whether we've sent a call proceeding / alerting */
1470 + int alert; /* Whether we've sent an alerting */
1473 + q921_call *phones;
1475 int ri; /* Restart Indicator (Restart Indicator IE) */
1477 @@ -202,15 +242,18 @@
1480 int callerpres; /* Caller presentation */
1481 - char callerani[256]; /* Caller */
1482 - char callernum[256];
1483 + int callerplanuser;
1484 + int callerpresuser; /* Caller presentation */
1485 + char callernum[256]; /* Calling Number (network provided) */
1486 + char callerani[256]; /* Calling Number, (user provided) */
1488 char callername[256];
1490 - char digitbuf[64]; /* Buffer for digits that come in KEYPAD_FACILITY */
1491 + char digitbuf[64]; /* Buffer for digits that come in KEYPAD_FACILITY */
1493 int ani2; /* ANI II */
1498 char callednum[256]; /* Called Number */
1499 int complete; /* no more digits coming */
1500 @@ -225,23 +268,36 @@
1501 char redirectingnum[256]; /* Number of redirecting party */
1502 char redirectingname[256]; /* Name of redirecting party */
1507 /* Filled in cases of multiple diversions */
1510 - int origredirectingreason; /* Original reason for redirect (in cases of multiple redirects) */
1511 + int origredirectingreason; /* Original reason for redirect (in cases of multiple redirects) */
1512 char origcalledname[256]; /* Original name of person being called */
1513 - char origcallednum[256]; /* Orignal number of person being called */
1514 + char origcallednum[256]; /* Orignal number of person being called */
1516 - int useruserprotocoldisc;
1517 + int useruserprotocoldisc;
1518 char useruserinfo[256];
1519 char callingsubaddr[256]; /* Calling parties sub address */
1521 + char callid[10]; /* call identity for SUSPEND/RESUME */
1522 + char digits[256]; /* additional digits received via info msgs (cpn or keypad) */
1523 + char display[256]; /* display ie received in info msgs or for sending */
1525 + /* euroisdn facility fun */
1526 + int facility; /* FACILTIY received */
1529 long aoc_units; /* Advice of Charge Units */
1531 + char llc[16]; /* low layer compatibility */
1532 struct apdu_event *apdus; /* APDU queue for call */
1535 extern int pri_schedule_event(struct pri *pri, int ms, void (*function)(void *data), void *data);
1536 +extern int pri_schedule_event2(struct pri *pri, int ms, void (*function)(void *data, int data2), void *data, int data2);
1538 extern pri_event *pri_schedule_run(struct pri *pri);
1541 extern pri_event *pri_mkerror(struct pri *pri, char *errstr);
1543 extern void pri_message(struct pri *pri, char *fmt, ...);
1546 extern void pri_error(struct pri *pri, char *fmt, ...);
1548 void libpri_copy_string(char *dst, const char *src, size_t size);
1549 diff -urN libpri-1.2.3.orig/pri_q921.h libpri-1.2.3/pri_q921.h
1550 --- libpri-1.2.3.orig/pri_q921.h 2005-11-29 19:39:18.000000000 +0100
1551 +++ libpri-1.2.3/pri_q921.h 2006-04-15 21:57:24.000000000 +0200
1554 * Copyright (C) 2001, Linux Support Services, Inc.
1555 * All Rights Reserved.
1556 + * Copyright (C) 2003-2006 Junghanns.NET GmbH
1557 + * Klaus-Peter Junghanns <kpj@junghanns.net>
1559 * This program is free software; you can redistribute it and/or modify
1560 * it under the terms of the GNU General Public License as published by
1562 #define Q921_FRAMETYPE_S 0x1
1564 #define Q921_TEI_GROUP 127
1565 +#define Q921_TEI_ID_REQUEST 0x1
1566 +#define Q921_TEI_ID_ASSIGNED 0x2
1567 +#define Q921_TEI_ID_DENIED 0x3
1568 +#define Q921_TEI_ID_CHK_REQ 0x4
1569 +#define Q921_TEI_ID_CHK_RES 0x5
1570 +#define Q921_TEI_ID_REMOVE 0x6
1571 +#define Q921_TEI_ID_VERIFY 0x7
1572 #define Q921_TEI_GR303_EOC_PATH 0
1573 #define Q921_TEI_GR303_EOC_OPS 4
1574 #define Q921_TEI_GR303_TMC_SWITCHING 0
1575 @@ -164,12 +173,14 @@
1576 extern void q921_dump(struct pri *pri, q921_h *h, int len, int showraw, int txrx);
1578 /* Bring up the D-channel */
1579 -extern void q921_start(struct pri *pri, int now);
1580 +extern void q921_start(struct pri *pri, int now, int tei);
1582 -extern void q921_reset(struct pri *pri);
1583 +extern void q921_reset(struct pri *pri, int tei, int discard);
1585 extern pri_event *q921_receive(struct pri *pri, q921_h *h, int len);
1587 -extern int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr);
1588 +extern int q921_transmit_uframe(struct pri *pri, void *buf, int len, int cr, int tei);
1590 +extern int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr, int tei);
1593 diff -urN libpri-1.2.3.orig/pri_q931.h libpri-1.2.3/pri_q931.h
1594 --- libpri-1.2.3.orig/pri_q931.h 2005-11-29 19:39:18.000000000 +0100
1595 +++ libpri-1.2.3/pri_q931.h 2006-04-15 21:57:29.000000000 +0200
1598 * Copyright (C) 2001, Linux Support Services, Inc.
1599 * All Rights Reserved.
1600 + * Copyright (C) 2003-2006 Junghanns.NET GmbH
1601 + * Klaus-Peter Junghanns <kpj@junghanns.net>
1603 * This program is free software; you can redistribute it and/or modify
1604 * it under the terms of the GNU General Public License as published by
1605 @@ -190,6 +192,10 @@
1606 #define Q931_IE_CODESET(x) ((x) >> 8)
1607 #define Q931_IE_IE(x) ((x) & 0xff)
1608 #define Q931_FULL_IE(codeset, ie) (((codeset) << 8) | ((ie) & 0xff))
1609 +#define Q931_IE_MAX_LEN 257
1612 +#define Q931_COLP 0x4c
1614 #define Q931_DISPLAY 0x28
1615 #define Q931_IE_SEGMENTED_MSG 0x00
1617 #define Q931_IE_USER_USER 0x7E
1618 #define Q931_IE_ESCAPE_FOR_EXT 0x7F
1620 +#define Q931_IE_SPECIAL 0x02
1623 /* Call state stuff */
1624 #define Q931_CALL_STATE_NULL 0
1627 #define Q931_SENDING_COMPLETE 0xa1
1629 -extern int q931_receive(struct pri *pri, q931_h *h, int len);
1630 +extern int q931_receive(struct pri *pri, q931_h *h, int len, int tei);
1632 extern int q931_alerting(struct pri *pri, q931_call *call, int channel, int info);
1634 @@ -257,6 +265,10 @@
1636 extern int q931_information(struct pri *pri, q931_call *call, char digit);
1638 +extern int q931_information_display(struct pri *pri, q931_call *call, char *display);
1640 +extern int q931_add_display(struct pri *pri, q931_call *call, char *display);
1642 extern int q931_connect(struct pri *pri, q931_call *call, int channel, int nonisdn);
1644 extern int q931_release(struct pri *pri, q931_call *call, int cause);
1645 @@ -265,6 +277,10 @@
1647 extern int q931_hangup(struct pri *pri, q931_call *call, int cause);
1649 +extern int q921_hangup(struct pri *pri, q931_call *c, int tei);
1651 +extern int q921_handle_hangup(struct pri *pri, q931_call *c, int tei);
1653 extern int q931_restart(struct pri *pri, int channel);
1655 extern int q931_facility(struct pri *pri, q931_call *call);
1656 @@ -279,5 +295,23 @@
1657 extern void q931_dump(struct pri *pri, q931_h *h, int len, int txrx);
1659 extern void __q931_destroycall(struct pri *pri, q931_call *c);
1661 +extern int q931_hold_acknowledge(struct pri *pri, q931_call *c);
1663 +extern int q931_hold_reject(struct pri *pri, q931_call *c);
1665 +extern int q931_retrieve_acknowledge(struct pri *pri, q931_call *c, int channel);
1667 +extern int q931_retrieve_reject(struct pri *pri, q931_call *c);
1669 +extern int q931_suspend_acknowledge(struct pri *pri, q931_call *c, char *display);
1671 +extern int q931_suspend_reject(struct pri *pri, q931_call *c, char *display);
1673 +extern int q931_resume_reject(struct pri *pri, q931_call *c, char *display);
1675 +extern int q931_resume_acknowledge(struct pri *pri, q931_call *c, int channel, char *display);
1677 +//extern int q931_facility(struct pri *pri, q931_call *c, int operation, char *arguments);
1680 diff -urN libpri-1.2.3.orig/pri_timers.h libpri-1.2.3/pri_timers.h
1681 --- libpri-1.2.3.orig/pri_timers.h 2005-11-29 19:39:18.000000000 +0100
1682 +++ libpri-1.2.3/pri_timers.h 2006-01-18 12:28:07.000000000 +0100
1685 /* -1 means we dont currently support the timer/counter */
1686 #define PRI_TIMERS_DEFAULT { 3, /* N200 */ \
1695 + 2000, /* T201 */ \
1696 + 5000, /* T202 */ \
1702 + 4000, /* T303 */ \
1706 diff -urN libpri-1.2.3.orig/pridump.c libpri-1.2.3/pridump.c
1707 --- libpri-1.2.3.orig/pridump.c 2005-11-29 19:39:18.000000000 +0100
1708 +++ libpri-1.2.3/pridump.c 2006-01-18 12:28:07.000000000 +0100
1711 * libpri: An implementation of Primary Rate ISDN
1713 - * Written by Mark Spencer <markster@digium.com>
1714 + * Written by Mark Spencer <markster@linux-support.net>
1716 - * Copyright (C) 2001-2005, Digium
1717 + * Copyright (C) 2001, Linux Support Services, Inc.
1718 * All Rights Reserved.
1720 * This program is free software; you can redistribute it and/or modify
1721 diff -urN libpri-1.2.3.orig/prisched.c libpri-1.2.3/prisched.c
1722 --- libpri-1.2.3.orig/prisched.c 2005-11-29 19:39:18.000000000 +0100
1723 +++ libpri-1.2.3/prisched.c 2006-04-15 21:57:42.000000000 +0200
1726 * libpri: An implementation of Primary Rate ISDN
1728 - * Written by Mark Spencer <markster@digium.com>
1729 + * Written by Mark Spencer <markster@linux-support.net>
1731 - * Copyright (C) 2001-2005, Digium
1732 + * Copyright (C) 2001, Linux Support Services, Inc.
1733 * All Rights Reserved.
1734 + * Copyright (C) 2003-2006 Junghanns.NET GmbH
1735 + * Klaus-Peter Junghanns <kpj@junghanns.net>
1737 * This program is free software; you can redistribute it and/or modify
1738 * it under the terms of the GNU General Public License as published by
1746 #include "pri_internal.h"
1750 static int maxsched = 0;
1754 for (x=1;x<MAX_SCHED;x++)
1755 - if (!pri->pri_sched[x].callback)
1756 + if ((!pri->pri_sched[x].callback2) && (!pri->pri_sched[x].callback))
1758 if (x == MAX_SCHED) {
1759 pri_error(pri, "No more room in scheduler\n");
1762 pri->pri_sched[x].when = tv;
1763 pri->pri_sched[x].callback = function;
1764 + pri->pri_sched[x].callback2 = NULL;
1765 pri->pri_sched[x].data = data;
1766 + pri->pri_sched[x].hasdata2 = 0;
1767 + pri->pri_sched[x].data2 = 0;
1771 +int pri_schedule_event2(struct pri *pri, int ms, void (*function)(void *data, int data2), void *data, int data2)
1774 + struct timeval tv;
1775 + for (x=1;x<MAX_SCHED;x++)
1776 + if ((!pri->pri_sched[x].callback2) && (!pri->pri_sched[x].callback))
1778 + if (x == MAX_SCHED) {
1779 + pri_error(pri, "No more room in scheduler\n");
1784 + gettimeofday(&tv, NULL);
1785 + tv.tv_sec += ms / 1000;
1786 + tv.tv_usec += (ms % 1000) * 1000;
1787 + if (tv.tv_usec > 1000000) {
1788 + tv.tv_usec -= 1000000;
1791 + pri->pri_sched[x].when = tv;
1792 + pri->pri_sched[x].callback = NULL;
1793 + pri->pri_sched[x].callback2 = function;
1794 + pri->pri_sched[x].data = data;
1795 + pri->pri_sched[x].hasdata2 = 1;
1796 + pri->pri_sched[x].data2 = data2;
1801 if (pri->subchannel)
1802 closest = pri_schedule_next(pri->subchannel);
1803 for (x=1;x<MAX_SCHED;x++) {
1804 - if (pri->pri_sched[x].callback &&
1805 + if ((pri->pri_sched[x].callback || pri->pri_sched[x].callback2) &&
1806 (!closest || (closest->tv_sec > pri->pri_sched[x].when.tv_sec) ||
1807 ((closest->tv_sec == pri->pri_sched[x].when.tv_sec) &&
1808 (closest->tv_usec > pri->pri_sched[x].when.tv_usec))))
1809 @@ -76,26 +109,38 @@
1811 static pri_event *__pri_schedule_run(struct pri *pri, struct timeval *tv)
1814 - void (*callback)(void *);
1817 + void (*callback)(void *);
1818 + void (*callback2)(void *, int);
1823 if (pri->subchannel) {
1824 if ((e = __pri_schedule_run(pri->subchannel, tv))) {
1828 for (x=1;x<MAX_SCHED;x++) {
1829 - if (pri->pri_sched[x].callback &&
1830 + if ((pri->pri_sched[x].callback || pri->pri_sched[x].callback2) &&
1831 ((pri->pri_sched[x].when.tv_sec < tv->tv_sec) ||
1832 ((pri->pri_sched[x].when.tv_sec == tv->tv_sec) &&
1833 (pri->pri_sched[x].when.tv_usec <= tv->tv_usec)))) {
1835 callback = pri->pri_sched[x].callback;
1836 + callback2 = pri->pri_sched[x].callback2;
1837 data = pri->pri_sched[x].data;
1838 + data2 = pri->pri_sched[x].data2;
1839 pri->pri_sched[x].callback = NULL;
1840 + pri->pri_sched[x].callback2 = NULL;
1841 pri->pri_sched[x].data = NULL;
1843 + pri->pri_sched[x].data2 = 0;
1844 + if (pri->pri_sched[x].hasdata2 == 1) {
1845 + pri->pri_sched[x].hasdata2 = 0;
1846 + callback2(data, data2);
1854 if ((id >= MAX_SCHED) || (id < 0))
1855 pri_error(pri, "Asked to delete sched id %d???\n", id);
1856 pri->pri_sched[id].callback = NULL;
1857 + pri->pri_sched[id].callback2 = NULL;
1860 diff -urN libpri-1.2.3.orig/pritest.c libpri-1.2.3/pritest.c
1861 --- libpri-1.2.3.orig/pritest.c 2005-11-29 19:39:18.000000000 +0100
1862 +++ libpri-1.2.3/pritest.c 2006-01-18 12:28:07.000000000 +0100
1865 * libpri: An implementation of Primary Rate ISDN
1867 - * Written by Mark Spencer <markster@digium.com>
1868 + * Written by Mark Spencer <markster@linux-support.net>
1870 - * Copyright (C) 2001-2005, Digium
1871 + * Copyright (C) 2001, Linux Support Services, Inc.
1872 * All Rights Reserved.
1874 * This program is free software; you can redistribute it and/or modify
1876 #define PRI_DEF_NODETYPE PRI_CPE
1877 #define PRI_DEF_SWITCHTYPE PRI_SWITCH_NI2
1879 -#define MAX_CHAN 32
1880 -#define DCHANNEL_TIMESLOT 16
1882 +#define DCHANNEL_TIMESLOT 3
1885 static int offset = 0;
1887 static void do_channel(ZAP *z)
1889 /* This is the part that runs on a given channel */
1890 - zap_playf(z, "raw.ulaw", 0);
1891 + zap_playf(z, "raw.alaw", 0);
1897 if (!strcasecmp(node, "network"))
1899 + if (!strcasecmp(node, "bri_cpe_ptmp"))
1900 + return BRI_CPE_PTMP;
1901 + if (!strcasecmp(node, "bri_network_ptmp"))
1902 + return BRI_NETWORK_PTMP;
1903 + if (!strcasecmp(node, "bri_cpe"))
1905 + if (!strcasecmp(node, "bri_network"))
1906 + return BRI_NETWORK;
1910 @@ -285,6 +293,10 @@
1914 + case PRI_EVENT_HANGUP_REQ:
1915 + printf("-- Hanging up channel %d\n", e->hangup.channel);
1916 + hangup_channel(e->hangup.channel);
1919 fprintf(stderr, "--!! Unknown PRI event %d\n", e->e);
1921 diff -urN libpri-1.2.3.orig/q921.c libpri-1.2.3/q921.c
1922 --- libpri-1.2.3.orig/q921.c 2005-12-06 22:35:50.000000000 +0100
1923 +++ libpri-1.2.3/q921.c 2006-08-01 09:55:53.000000000 +0200
1926 * libpri: An implementation of Primary Rate ISDN
1928 - * Written by Mark Spencer <markster@digium.com>
1929 + * Written by Mark Spencer <markster@linux-support.net>
1931 - * Copyright (C) 2001-2005, Digium
1932 + * Copyright (C) 2001, Linux Support Services, Inc.
1933 * All Rights Reserved.
1934 + * Copyright (C) 2003-2006 Junghanns.NET GmbH
1935 + * Klaus-Peter Junghanns <kpj@junghanns.net>
1937 * This program is free software; you can redistribute it and/or modify
1938 * it under the terms of the GNU General Public License as published by
1940 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1949 (hf).h.tei = (pri)->tei; \
1952 -static void reschedule_t203(struct pri *pri);
1953 +static void reschedule_t203(struct pri *pri, int tei);
1954 +static void q921_flush_txqueue(struct pri *pri, int tei, int devnull);
1955 +static void q921_send_teiverify(struct pri *pri,int tei);
1957 -static void q921_discard_retransmissions(struct pri *pri)
1958 +static void q921_discard_retransmissions(struct pri *pri, int tei)
1960 struct q921_frame *f, *p;
1962 + int teio = tei - Q921_TEI_BASE;
1963 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
1965 + f = pri->txqueue[teio];
1976 - pri->txqueue = NULL;
1977 + pri->txqueue[teio] = NULL;
1980 static int q921_transmit(struct pri *pri, q921_h *h, int len)
1982 pri_error(pri, "Short write: %d/%d (%s)\n", res, len + 2, strerror(errno));
1985 - reschedule_t203(pri);
1986 + if (pri->localtype == BRI_CPE_PTMP) {
1987 + reschedule_t203(pri, pri->tei);
1988 + } else if (h->h.tei != Q921_TEI_GROUP) {
1989 + reschedule_t203(pri, h->h.tei);
1994 -static void q921_send_ua(struct pri *pri, int pfbit)
1995 +static void q921_send_ua(struct pri *pri, int pfbit, int tei)
2000 h.u.m2 = 0; /* M2 = 0 */
2001 h.u.p_f = pfbit; /* Final bit on */
2002 h.u.ft = Q921_FRAMETYPE_U;
2004 switch(pri->localtype) {
2007 @@ -107,6 +119,19 @@
2011 + case BRI_NETWORK_PTMP:
2014 + case BRI_CPE_PTMP:
2015 + h.h.tei = pri->tei;
2025 pri_error(pri, "Don't know how to U/A on a type %d node\n", pri->localtype);
2027 @@ -116,18 +141,364 @@
2028 q921_transmit(pri, &h, 3);
2031 -static void q921_send_sabme_now(void *vpri);
2033 +static void q921_send_disconnect(struct pri *pri, int pfbit, int tei) {
2035 + Q921_INIT(pri, h);
2039 + h.u.ft = Q921_FRAMETYPE_U;
2041 + switch(pri->localtype) {
2048 + case BRI_NETWORK_PTMP:
2051 + case BRI_CPE_PTMP:
2052 + h.h.tei = pri->tei;
2062 + pri_error(pri, "Don't know how to disconnect on a type %d node\n", pri->localtype);
2065 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2066 + pri_message(pri, "Sending Disconnect\n");
2067 + q921_transmit(pri, &h, 3);
2071 -static void q921_send_sabme(void *vpri, int now)
2072 +static void q921_send_dm(struct pri *pri, int pfbit, int tei)
2075 + Q921_INIT(pri, h);
2076 + h.u.m3 = 0; /* M3 = 0 */
2077 + h.u.m2 = 3; /* M2 = 3 */
2078 + h.u.p_f = pfbit; /* Final bit on */
2079 + h.u.ft = Q921_FRAMETYPE_U;
2081 + switch(pri->localtype) {
2088 + case BRI_NETWORK_PTMP:
2091 + case BRI_CPE_PTMP:
2092 + h.h.tei = pri->tei;
2102 + pri_error(pri, "Don't know how to DM on a type %d node\n", pri->localtype);
2105 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2106 + pri_message(pri, "Sending DM\n");
2107 + q921_transmit(pri, &h, 3);
2110 +static void q921_send_teireq(void *vpri) {
2111 + struct pri *pri = vpri;
2112 + unsigned short ri=0x6464;
2116 + if (pri->localtype != BRI_CPE_PTMP) {
2117 + pri_error(pri, "TEI req for non-ptmp???\n");
2120 + if (pri->t202_timer[0]) {
2121 + pri_schedule_del(pri, pri->t202_timer[0]);
2122 + pri->t202_timer[0] = 0;
2124 + if (pri->sabme_retrans[0]++ > (pri->timers[PRI_TIMER_N202])) {
2125 + /* delete txqueue */
2126 + q921_flush_txqueue(pri, 0, 1);
2127 + /* N202 retransmissions, activation of layer 2 failed, tell upper layer, start again */
2129 + pri->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
2130 + pri->ev.gen.tei = 0;
2131 + pri->sabme_retrans[0] = 0;
2132 + q921_send_teiverify(pri, 127);
2134 + pri->t202_timer[0] = pri_schedule_event(pri, pri->timers[PRI_TIMER_T202] + 3000, q921_send_teireq, pri);
2139 + pri->t202_timer[0] = pri_schedule_event(pri, pri->timers[PRI_TIMER_T202], q921_send_teireq, pri);
2142 + f = malloc(sizeof(q921_u) + 5 + 2);
2143 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
2145 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
2146 + f->h.tei = Q921_TEI_GROUP;
2152 + f->ft = Q921_FRAMETYPE_U;
2154 + f->data[1] = ri >> 8;
2155 + f->data[2] = ri & 0xff;
2156 + f->data[3] = Q921_TEI_ID_REQUEST;
2157 + f->data[4] = 0xff;
2158 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2159 + pri_message(pri, "Sending TEI Request ri=%d\n",ri);
2160 + q921_transmit(pri,(q921_h *)&(f->h),8);
2164 +static void q921_send_teiassign(struct pri *pri,int ri,int tei) {
2167 + if (pri->localtype != BRI_NETWORK_PTMP) {
2168 + pri_error(pri, "TEI assign for non-ptmp???\n");
2172 + f = malloc(sizeof(q921_u) + 5 + 2);
2173 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
2175 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
2176 + f->h.tei = Q921_TEI_GROUP;
2182 + f->ft = Q921_FRAMETYPE_U;
2184 + f->data[1] = ri >> 8;
2185 + f->data[2] = ri & 0xff;
2186 + f->data[3] = Q921_TEI_ID_ASSIGNED;
2187 + f->data[4] = (tei << 1) | 0x1;
2188 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2189 + pri_message(pri, "Sending TEI assign ri=%d tei=%d\n",ri,tei);
2190 + q921_transmit(pri,(q921_h *)&(f->h),8);
2193 + pri_error(pri, "q921_send_teiassign: failed to malloc f!\n");
2197 +static void q921_send_teichkresp(struct pri *pri,int tei) {
2199 + unsigned short ri=0x6464;
2202 + if (pri->localtype != BRI_CPE_PTMP) {
2203 + pri_error(pri, "TEI check response for non-ptmp???\n");
2207 + f = malloc(sizeof(q921_u) + 5 + 2);
2208 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
2210 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
2211 + f->h.tei = Q921_TEI_GROUP;
2212 + f->h.c_r = 0; // command u->n
2217 + f->ft = Q921_FRAMETYPE_U;
2219 + f->data[1] = ri >> 8;
2220 + f->data[2] = ri & 0xff;
2221 + f->data[3] = Q921_TEI_ID_CHK_RES;
2222 + f->data[4] = (tei << 1) | 0x1;
2223 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2224 + pri_message(pri, "Sending TEI check resp ri=%d tei=%d\n",ri,tei);
2225 + q921_transmit(pri,(q921_h *)&(f->h),8);
2230 +static void q921_send_teichkreq(struct pri *pri,int tei) {
2232 + unsigned short ri=0x6464;
2235 + if (pri->localtype != BRI_NETWORK_PTMP) {
2236 + pri_error(pri, "TEI check response for non-ptmp???\n");
2240 + f = malloc(sizeof(q921_u) + 5 + 2);
2241 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
2243 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
2244 + f->h.tei = Q921_TEI_GROUP;
2245 + f->h.c_r = 1; // command u->n
2250 + f->ft = Q921_FRAMETYPE_U;
2254 + f->data[3] = Q921_TEI_ID_CHK_REQ;
2255 + f->data[4] = (tei << 1) | 0x1;
2256 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2257 + pri_message(pri, "Sending TEI check request ri=%d tei=%d\n",ri,tei);
2258 + q921_transmit(pri,(q921_h *)&(f->h),8);
2263 +static void q921_send_teiverify(struct pri *pri,int tei) {
2266 + if ((pri->localtype != BRI_CPE) && (pri->localtype != BRI_CPE_PTMP)) {
2267 + pri_error(pri, "TEI verify for non-ptmp???\n");
2271 + f = malloc(sizeof(q921_u) + 5 + 2);
2272 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
2274 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
2275 + f->h.tei = Q921_TEI_GROUP;
2276 + f->h.c_r = 0; // command u->n
2281 + f->ft = Q921_FRAMETYPE_U;
2285 + f->data[3] = Q921_TEI_ID_VERIFY;
2286 + f->data[4] = (tei << 1) | 0x1;
2287 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2288 + pri_message(pri, "Sending TEI verify tei=%d\n", tei);
2289 + q921_transmit(pri,(q921_h *)&(f->h),8);
2294 + static void q921_send_teiremove(struct pri *pri, int tei) {
2296 + unsigned short ri=0x6464;
2299 + if (pri->localtype != BRI_NETWORK_PTMP) {
2300 + pri_error(pri, "TEI remove for non-ptmp???\n");
2304 + f = malloc(sizeof(q921_u) + 5 + 2);
2305 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
2307 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
2308 + f->h.tei = Q921_TEI_GROUP;
2314 + f->ft = Q921_FRAMETYPE_U;
2316 + f->data[1] = ri >> 8;
2317 + f->data[2] = ri & 0xff;
2318 + f->data[3] = Q921_TEI_ID_REMOVE;
2319 + f->data[4] = (tei << 1) | 0x1;
2320 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2321 + pri_message(pri, "Sending TEI remove tei=%d\n",tei);
2322 + q921_transmit(pri,(q921_h *)&(f->h),8);
2327 + static void q921_send_teidenied(struct pri *pri, int ri, int tei) {
2330 + if (pri->localtype != BRI_NETWORK_PTMP) {
2331 + pri_error(pri, "TEI ID denied for non-ptmp???\n");
2335 + f = malloc(sizeof(q921_u) + 5 + 2);
2336 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
2338 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
2339 + f->h.tei = Q921_TEI_GROUP;
2345 + f->ft = Q921_FRAMETYPE_U;
2347 + f->data[1] = ri >> 8;
2348 + f->data[2] = ri & 0xff;
2349 + f->data[3] = Q921_TEI_ID_DENIED;
2350 + f->data[4] = (tei << 1) | 0x1;
2351 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2352 + pri_message(pri, "Sending TEI ID denied tei=%d\n",tei);
2353 + q921_transmit(pri,(q921_h *)&(f->h),8);
2359 +static void q921_send_sabme_now(void *vpri, int tei);
2361 +static void q921_send_sabme(void *vpri, int now, int tei)
2363 struct pri *pri = vpri;
2365 - pri_schedule_del(pri, pri->sabme_timer);
2366 - pri->sabme_timer = 0;
2367 - pri->sabme_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T200], q921_send_sabme_now, pri);
2368 + int teio=tei - Q921_TEI_BASE;
2369 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2371 + if (pri->sabme_timer[teio]) {
2372 + pri_schedule_del(pri, pri->sabme_timer[teio]);
2373 + pri->sabme_timer[teio] = 0;
2375 + pri->sabme_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], q921_send_sabme_now, pri, tei);
2378 + if (pri->sabme_retrans[teio]++ > (pri->timers[PRI_TIMER_N202])) {
2379 + /* delete txqueue */
2380 + q921_flush_txqueue(pri, tei, 1);
2381 + /* N202 retransmissions, activation of layer 2 failed, tell upper layer, start again */
2383 + pri->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
2384 + pri->ev.gen.tei = tei;
2385 + pri->sabme_retrans[teio] = 0;
2387 + pri->sabme_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200] + 3000, q921_send_sabme_now, pri, tei);
2394 h.u.m3 = 3; /* M3 = 3 */
2395 h.u.m2 = 3; /* M2 = 3 */
2396 h.u.p_f = 1; /* Poll bit set */
2397 @@ -139,25 +510,42 @@
2401 + case BRI_NETWORK_PTMP:
2404 + case BRI_CPE_PTMP:
2406 + h.h.tei = pri->tei;
2415 - pri_error(pri, "Don't know how to U/A on a type %d node\n", pri->localtype);
2416 + pri_error(pri, "Don't know how to send SABME on a type %d node\n", pri->localtype);
2419 if (pri->debug & PRI_DEBUG_Q921_STATE)
2420 pri_message(pri, "Sending Set Asynchronous Balanced Mode Extended\n");
2421 q921_transmit(pri, &h, 3);
2422 - pri->q921_state = Q921_AWAITING_ESTABLISH;
2423 + pri->q921_state[teio] = Q921_AWAITING_ESTABLISH;
2426 -static void q921_send_sabme_now(void *vpri)
2427 +static void q921_send_sabme_now(void *vpri, int tei)
2429 - q921_send_sabme(vpri, 1);
2430 + q921_send_sabme(vpri, 1, tei);
2433 -static int q921_ack_packet(struct pri *pri, int num)
2436 +static int q921_ack_packet(struct pri *pri, int num, int tei)
2438 struct q921_frame *f, *prev = NULL;
2440 + int teio=tei - Q921_TEI_BASE;
2441 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2442 + f = pri->txqueue[teio];
2444 if (f->h.n_s == num) {
2445 /* Cancel each packet as necessary */
2446 @@ -165,26 +553,26 @@
2448 prev->next = f->next;
2450 - pri->txqueue = f->next;
2451 + pri->txqueue[teio] = f->next;
2452 if (pri->debug & PRI_DEBUG_Q921_STATE)
2453 - pri_message(pri, "-- ACKing packet %d, new txqueue is %d (-1 means empty)\n", f->h.n_s, pri->txqueue ? pri->txqueue->h.n_s : -1);
2454 + pri_message(pri, "-- ACKing packet %d, new txqueue is %d (-1 means empty)\n", f->h.n_s, pri->txqueue[teio] ? pri->txqueue[teio]->h.n_s : -1);
2457 + pri->v_a[teio] = num;
2459 /* Reset retransmission counter if we actually acked something */
2461 + pri->retrans[teio] = 0;
2462 /* Decrement window size */
2464 + pri->windowlen[teio]--;
2465 /* Search for something to send */
2467 + f = pri->txqueue[teio];
2469 if (!f->transmitted) {
2470 /* Send it now... */
2471 if (pri->debug & PRI_DEBUG_Q921_STATE)
2472 pri_message(pri, "-- Finally transmitting %d, since window opened up\n", f->h.n_s);
2475 - f->h.n_r = pri->v_r;
2476 + pri->windowlen[teio]++;
2477 + f->h.n_r = pri->v_r[teio];
2478 q921_transmit(pri, (q921_h *)(&f->h), f->len);
2481 @@ -198,77 +586,136 @@
2485 -static void t203_expire(void *);
2486 -static void t200_expire(void *);
2487 -static pri_event *q921_dchannel_down(struct pri *pri);
2488 +static void t203_expire(void *, int tei);
2489 +static void t200_expire(void *, int tei);
2490 +static pri_event *q921_dchannel_down(struct pri *pri, int tei);
2492 +static void reschedule_t203(struct pri *pri, int tei)
2494 + int teio=tei - Q921_TEI_BASE;
2495 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2496 + if (pri->t203_timer[teio]) {
2497 + pri_schedule_del(pri, pri->t203_timer[teio]);
2498 + pri->t203_timer[teio] = 0;
2499 + if (pri->q921_state[teio] != Q921_LINK_CONNECTION_RELEASED) {
2500 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2501 + pri_message(pri, "-- Restarting T203 counter\n");
2502 + /* Nothing to transmit, start the T203 counter instead */
2503 + pri->t203_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri, tei);
2508 -static void reschedule_t203(struct pri *pri)
2509 +static void q921_flush_txqueue(struct pri *pri, int tei, int devnull)
2511 - if (pri->t203_timer) {
2512 - pri_schedule_del(pri, pri->t203_timer);
2513 - if (pri->debug & PRI_DEBUG_Q921_STATE)
2514 - pri_message(pri, "-- Restarting T203 counter\n");
2515 - /* Nothing to transmit, start the T203 counter instead */
2516 - pri->t203_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri);
2517 + struct q921_frame *f, *tmp = NULL;
2518 + int teio=tei - Q921_TEI_BASE;
2519 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2520 + f = pri->txqueue[teio];
2522 + /* nothing to send */
2525 + /* transmit all i-frames that were queued up while we were waiting for layer 2 to rise */
2533 + if (pri->localtype == BRI_CPE_PTMP) {
2534 + /* update TEI, it might have changed */
2535 + f->h.h.tei = pri->tei;
2537 + q921_transmit(pri, (q921_h *)&f->h, f->len);
2544 + pri->txqueue[teio] = NULL;
2545 + pri->v_s[teio] = 0;
2547 + if (pri->t200_timer[teio]) {
2548 + pri_schedule_del(pri, pri->t200_timer[teio]);
2549 + pri->t200_timer[teio] = 0;
2551 + if (pri->t203_timer[teio]) {
2552 + pri_schedule_del(pri, pri->t203_timer[teio]);
2553 + pri->t203_timer[teio] = 0;
2555 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
2559 -static pri_event *q921_ack_rx(struct pri *pri, int ack)
2560 +static pri_event *q921_ack_rx(struct pri *pri, int ack, int tei)
2565 + int teio=tei - Q921_TEI_BASE;
2566 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2567 /* Make sure the ACK was within our window */
2568 - for (x=pri->v_a; (x != pri->v_s) && (x != ack); Q921_INC(x));
2569 + for (x=pri->v_a[teio]; (x != pri->v_s[teio]) && (x != ack); Q921_INC(x));
2571 /* ACK was outside of our window --- ignore */
2572 - pri_error(pri, "ACK received for '%d' outside of window of '%d' to '%d', restarting\n", ack, pri->v_a, pri->v_s);
2573 - ev = q921_dchannel_down(pri);
2574 - q921_start(pri, 1);
2575 + pri_error(pri, "ACK received for '%d' outside of window of '%d' to '%d', restarting\n", ack, pri->v_a[teio], pri->v_s[teio]);
2576 + ev = q921_dchannel_down(pri, tei);
2577 + q921_start(pri, 1, tei);
2581 /* Cancel each packet as necessary */
2582 if (pri->debug & PRI_DEBUG_Q921_STATE)
2583 - pri_message(pri, "-- ACKing all packets from %d to (but not including) %d\n", pri->v_a, ack);
2584 - for (x=pri->v_a; x != ack; Q921_INC(x))
2585 - cnt += q921_ack_packet(pri, x);
2586 - if (!pri->txqueue) {
2587 + pri_message(pri, "-- ACKing all packets from %d to (but not including) %d\n", pri->v_a[teio], ack);
2588 + for (x=pri->v_a[teio]; x != ack; Q921_INC(x))
2589 + cnt += q921_ack_packet(pri, x, tei);
2590 + if (!pri->txqueue[teio]) {
2591 + /* Something was ACK'd. Stop T200 counter */
2592 + if (pri->t200_timer[teio]) {
2593 + pri_schedule_del(pri, pri->t200_timer[teio]);
2594 + pri->t200_timer[teio] = 0;
2596 if (pri->debug & PRI_DEBUG_Q921_STATE)
2597 pri_message(pri, "-- Since there was nothing left, stopping T200 counter\n");
2598 - /* Something was ACK'd. Stop T200 counter */
2599 - pri_schedule_del(pri, pri->t200_timer);
2600 - pri->t200_timer = 0;
2602 - if (pri->t203_timer) {
2603 + if (pri->t203_timer[teio]) {
2604 + pri_schedule_del(pri, pri->t203_timer[teio]);
2605 + pri->t203_timer[teio] = 0;
2606 if (pri->debug & PRI_DEBUG_Q921_STATE)
2607 pri_message(pri, "-- Stopping T203 counter since we got an ACK\n");
2608 - pri_schedule_del(pri, pri->t203_timer);
2609 - pri->t203_timer = 0;
2611 - if (pri->txqueue) {
2612 + if (pri->txqueue[teio]) {
2613 /* Something left to transmit, Start the T200 counter again if we stopped it */
2614 + if (pri->t200_timer[teio]) {
2615 + pri_schedule_del(pri, pri->t200_timer[teio]);
2616 + pri->t200_timer[teio] = 0;
2618 if (pri->debug & PRI_DEBUG_Q921_STATE)
2619 - pri_message(pri, "-- Something left to transmit (%d), restarting T200 counter\n", pri->txqueue->h.n_s);
2620 - if (!pri->t200_timer)
2621 - pri->t200_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri);
2622 + pri_message(pri, "-- Something left to transmit (%d), restarting T200 counter\n", pri->txqueue[teio]->h.n_s);
2623 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
2626 if (pri->debug & PRI_DEBUG_Q921_STATE)
2627 pri_message(pri, "-- Nothing left, starting T203 counter\n");
2628 /* Nothing to transmit, start the T203 counter instead */
2629 - pri->t203_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri);
2630 + pri->t203_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri, tei);
2635 -static void q921_reject(struct pri *pri, int pf)
2636 +static void q921_reject(struct pri *pri, int pf, int tei)
2639 + int teio=tei - Q921_TEI_BASE;
2640 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2642 h.s.x0 = 0; /* Always 0 */
2643 h.s.ss = 2; /* Reject */
2644 h.s.ft = 1; /* Frametype (01) */
2645 - h.s.n_r = pri->v_r; /* Where to start retransmission */
2646 + h.s.n_r = pri->v_r[teio]; /* Where to start retransmission */
2648 switch(pri->localtype) {
2650 @@ -277,23 +724,38 @@
2654 + case BRI_NETWORK_PTMP:
2657 + case BRI_CPE_PTMP:
2668 - pri_error(pri, "Don't know how to U/A on a type %d node\n", pri->localtype);
2669 + pri_error(pri, "Don't know how to REJECT on a type %d node\n", pri->localtype);
2672 if (pri->debug & PRI_DEBUG_Q921_STATE)
2673 - pri_message(pri, "Sending Reject (%d)\n", pri->v_r);
2675 + pri_message(pri, "Sending Reject (%d)\n", pri->v_r[teio]);
2676 + pri->sentrej[teio] = 1;
2677 q921_transmit(pri, &h, 4);
2680 -static void q921_rr(struct pri *pri, int pbit, int cmd) {
2681 +static void q921_rr(struct pri *pri, int pbit, int cmd, int tei) {
2683 + int teio=tei - Q921_TEI_BASE;
2684 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2686 h.s.x0 = 0; /* Always 0 */
2687 h.s.ss = 0; /* Receive Ready */
2688 h.s.ft = 1; /* Frametype (01) */
2689 - h.s.n_r = pri->v_r; /* N/R */
2690 + h.s.n_r = pri->v_r[teio]; /* N/R */
2691 h.s.p_f = pbit; /* Poll/Final set appropriately */
2692 switch(pri->localtype) {
2694 @@ -308,81 +770,192 @@
2698 + case BRI_NETWORK_PTMP:
2705 + case BRI_CPE_PTMP:
2725 - pri_error(pri, "Don't know how to U/A on a type %d node\n", pri->localtype);
2726 + pri_error(pri, "Don't know how to RR on a type %d node\n", pri->localtype);
2729 - pri->v_na = pri->v_r; /* Make a note that we've already acked this */
2730 + pri->v_na[teio] = pri->v_r[teio]; /* Make a note that we've already acked this */
2731 if (pri->debug & PRI_DEBUG_Q921_STATE)
2732 - pri_message(pri, "Sending Receiver Ready (%d)\n", pri->v_r);
2733 + pri_message(pri, "Sending Receiver Ready (%d)\n", pri->v_r[teio]);
2734 q921_transmit(pri, &h, 4);
2737 -static void t200_expire(void *vpri)
2738 +static void t200_expire(void *vpri, int tei)
2740 struct pri *pri = vpri;
2741 - if (pri->txqueue) {
2742 + int teio=tei - Q921_TEI_BASE;
2743 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2744 + if (pri->txqueue[teio]) {
2745 /* Retransmit first packet in the queue, setting the poll bit */
2746 if (pri->debug & PRI_DEBUG_Q921_STATE)
2747 pri_message(pri, "-- T200 counter expired, What to do...\n");
2748 - /* Force Poll bit */
2749 - pri->txqueue->h.p_f = 1;
2751 - pri->txqueue->h.n_r = pri->v_r;
2752 - pri->v_na = pri->v_r;
2753 - pri->solicitfbit = 1;
2755 + if (pri->txqueue[teio]->transmitted) {
2756 + /* Force Poll bit, if this is a retransmission */
2757 + pri->txqueue[teio]->h.p_f = 1;
2758 + pri->solicitfbit[teio] = 1;
2760 + pri->txqueue[teio]->h.n_r = pri->v_r[teio];
2761 + pri->v_na[teio] = pri->v_r[teio];
2762 + pri->retrans[teio]++;
2764 /* Up to three retransmissions */
2765 - if (pri->retrans < pri->timers[PRI_TIMER_N200]) {
2766 + if ((pri->retrans[teio] + 1) <= pri->timers[PRI_TIMER_N200]) {
2767 /* Reschedule t200_timer */
2768 if (pri->debug & PRI_DEBUG_Q921_STATE)
2769 - pri_message(pri, "-- Retransmitting %d bytes\n", pri->txqueue->len);
2771 - q921_rr(pri, 1, 1);
2773 - if (!pri->txqueue->transmitted)
2774 + pri_message(pri, "-- Retransmitting %d bytes\n", pri->txqueue[teio]->len);
2775 + if (pri->busy[teio]) {
2776 + // pri_message(pri, "-- q921_rr(pri, 1, 1) \n", tei);
2777 + q921_rr(pri, 1, 1, tei);
2779 + if (!pri->txqueue[teio]->transmitted)
2780 pri_error(pri, "!! Not good - head of queue has not been transmitted yet\n");
2781 - q921_transmit(pri, (q921_h *)&pri->txqueue->h, pri->txqueue->len);
2782 + q921_transmit(pri, (q921_h *)&pri->txqueue[teio]->h, pri->txqueue[teio]->len);
2784 + pri->txqueue[teio]->transmitted++;
2786 if (pri->debug & PRI_DEBUG_Q921_STATE)
2787 - pri_message(pri, "-- Rescheduling retransmission (%d)\n", pri->retrans);
2788 - pri->t200_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri);
2789 + pri_message(pri, "-- Rescheduling retransmission (%d)\n", pri->retrans[teio]);
2790 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
2792 if (pri->debug & PRI_DEBUG_Q921_STATE)
2793 pri_message(pri, "-- Timeout occured, restarting PRI\n");
2794 - pri->q921_state = Q921_LINK_CONNECTION_RELEASED;
2795 - pri->t200_timer = 0;
2796 - q921_dchannel_down(pri);
2797 - q921_start(pri, 1);
2798 + pri->q921_state[teio] = Q921_LINK_CONNECTION_RELEASED;
2799 + pri->t200_timer[teio] = 0;
2800 + q921_dchannel_down(pri, tei);
2801 + q921_start(pri, 1, tei);
2804 - } else if (pri->solicitfbit) {
2805 + } else if (pri->solicitfbit[teio]) {
2806 if (pri->debug & PRI_DEBUG_Q921_STATE)
2807 pri_message(pri, "-- Retrying poll with f-bit\n");
2809 - if (pri->retrans < pri->timers[PRI_TIMER_N200]) {
2810 - pri->solicitfbit = 1;
2811 - q921_rr(pri, 1, 1);
2812 - pri->t200_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri);
2813 + pri->retrans[teio]++;
2814 + if (pri->retrans[teio] < pri->timers[PRI_TIMER_N200]) {
2815 + pri->solicitfbit[teio] = 1;
2816 + q921_rr(pri, 1, 1, tei);
2817 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
2819 if (pri->debug & PRI_DEBUG_Q921_STATE)
2820 pri_message(pri, "-- Timeout occured, restarting PRI\n");
2821 - pri->q921_state = Q921_LINK_CONNECTION_RELEASED;
2822 - pri->t200_timer = 0;
2823 - q921_dchannel_down(pri);
2824 - q921_start(pri, 1);
2825 + pri->q921_state[teio] = Q921_LINK_CONNECTION_RELEASED;
2826 + pri->t200_timer[teio] = 0;
2827 + q921_dchannel_down(pri, tei);
2828 + q921_start(pri, 1, tei);
2832 pri_error(pri, "T200 counter expired, nothing to send...\n");
2833 - pri->t200_timer = 0;
2834 + pri->t200_timer[teio] = 0;
2835 + if (pri->busy[teio]) {
2836 + if ((pri->retrans[teio] + 1) <= pri->timers[PRI_TIMER_N200]) {
2838 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
2839 + pri->retrans[teio]++;
2840 + q921_rr(pri, 1, 1, tei);
2842 + q921_reset(pri, tei, 1);
2843 + q921_start(pri, 1, tei);
2849 -int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr)
2850 +int q921_transmit_uframe(struct pri *pri, void *buf, int len, int cr, int tei)
2853 + uf = malloc(sizeof(q921_u) + len + 2);
2854 + memset(uf,0,sizeof(q921_u) + len + 2);
2862 + uf->ft = Q921_FRAMETYPE_U;
2863 + switch(pri->localtype) {
2870 + case BRI_NETWORK_PTMP:
2873 + case BRI_CPE_PTMP:
2883 + pri_error(pri, "Don't know how to send U frames on a type %d node\n", pri->localtype);
2886 + memcpy(uf->data,buf,len);
2887 + q921_transmit(pri, (q921_h*)&(uf->h), 3+len);
2893 +int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr, int tei)
2895 q921_frame *f, *prev=NULL;
2896 - for (f=pri->txqueue; f; f = f->next) prev = f;
2897 + int teio=tei - Q921_TEI_BASE;
2898 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2899 + if ((pri->q921_state[teio] == Q921_LINK_CONNECTION_RELEASED) && (!pri->sabme_timer[teio])) {
2900 + if (pri->localtype == BRI_CPE_PTMP) {
2901 + if (pri->tei > 0) {
2902 + /* p2p datalink is down */
2903 + pri->sabme_retrans[teio] = 0;
2904 + q921_send_sabme_now(pri, pri->tei);
2905 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2906 + pri_message(pri, "Reactivating layer 2\n");
2909 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2910 + pri_message(pri, "reactivating layer 2, sending tei req\n");
2911 + q921_send_teireq(pri);
2913 + } else if ((pri->localtype == BRI_CPE) || (pri->localtype == BRI_NETWORK)) {
2914 + /* p2p datalink is down */
2915 + pri->sabme_retrans[teio] = 0;
2916 + q921_send_sabme_now(pri, pri->tei);
2917 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2918 + pri_message(pri, "Reactivating layer 2\n");
2921 + for (f=pri->txqueue[teio]; f; f = f->next) prev = f;
2922 f = malloc(sizeof(q921_frame) + len + 2);
2924 memset(f,0,sizeof(q921_frame) + len + 2);
2925 @@ -400,47 +973,80 @@
2929 + case BRI_NETWORK_PTMP:
2936 + case BRI_CPE_PTMP:
2937 + f->h.h.tei = pri->tei;
2959 memcpy(f->h.data, buf, len);
2960 - f->h.n_s = pri->v_s;
2961 - f->h.n_r = pri->v_r;
2963 - pri->v_na = pri->v_r;
2964 + f->h.n_s = pri->v_s[teio];
2965 + f->h.n_r = pri->v_r[teio];
2967 + pri->v_na[teio] = pri->v_r[teio];
2974 - /* Immediately transmit unless we're in a recovery state, or the window
2975 - size is too big */
2976 - if (!pri->retrans && !pri->busy) {
2977 - if (pri->windowlen < pri->window) {
2979 + pri->txqueue[teio] = f;
2981 + if ((pri->q921_state[teio] != Q921_LINK_CONNECTION_ESTABLISHED) && ((pri->localtype == BRI_CPE_PTMP) || (pri->localtype == BRI_CPE) || (pri->localtype == BRI_NETWORK))){
2982 + /* no p2p datalink, yet. queue up the iframes... */
2983 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2984 + pri_message(pri, "Layer 3 transmit waiting for layer 2\n");
2986 + /* Immediately transmit unless we're in a recovery state, or the window
2987 + size is too big */
2988 + if (!pri->retrans[teio] && !pri->busy[teio]) {
2989 + if (pri->windowlen[teio] < pri->window[teio]) {
2990 + pri->windowlen[teio]++;
2991 q921_transmit(pri, (q921_h *)(&f->h), f->len);
2994 if (pri->debug & PRI_DEBUG_Q921_STATE)
2995 pri_message(pri, "Delaying transmission of %d, window is %d/%d long\n",
2996 - f->h.n_s, pri->windowlen, pri->window);
2997 + f->h.n_s, pri->windowlen[teio], pri->window[teio]);
3000 - if (pri->t203_timer) {
3002 + if (pri->t203_timer[teio]) {
3003 + pri_schedule_del(pri, pri->t203_timer[teio]);
3004 + pri->t203_timer[teio] = 0;
3005 if (pri->debug & PRI_DEBUG_Q921_STATE)
3006 pri_message(pri, "Stopping T_203 timer\n");
3007 - pri_schedule_del(pri, pri->t203_timer);
3008 - pri->t203_timer = 0;
3010 - if (!pri->t200_timer) {
3012 + if (!pri->t200_timer[teio]) {
3013 if (pri->debug & PRI_DEBUG_Q921_STATE)
3014 pri_message(pri, "Starting T_200 timer\n");
3015 - pri->t200_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri);
3017 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
3019 if (pri->debug & PRI_DEBUG_Q921_STATE)
3020 - pri_message(pri, "T_200 timer already going (%d)\n", pri->t200_timer);
3021 + pri_message(pri, "T_200 timer already going (%d)\n", pri->t200_timer[teio]);
3025 pri_error(pri, "!! Out of memory for Q.921 transmit\n");
3026 @@ -449,49 +1055,86 @@
3030 -static void t203_expire(void *vpri)
3031 +static void t203_expire(void *vpri, int tei)
3033 - struct pri *pri = vpri;
3034 - if (pri->q921_state == Q921_LINK_CONNECTION_ESTABLISHED) {
3035 - if (pri->debug & PRI_DEBUG_Q921_STATE)
3036 + struct pri *pri = vpri;
3037 + int teio=tei - Q921_TEI_BASE;
3038 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
3040 + if (pri->q921_state[teio] == Q921_LINK_CONNECTION_ESTABLISHED) {
3041 + if (pri->debug & PRI_DEBUG_Q921_STATE)
3042 pri_message(pri, "T203 counter expired, sending RR and scheduling T203 again\n");
3043 - /* Solicit an F-bit in the other's RR */
3044 - pri->solicitfbit = 1;
3046 - q921_rr(pri, 1, 1);
3047 - /* Start timer T200 to resend our RR if we don't get it */
3048 - pri->t203_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri);
3049 + /* Solicit an F-bit in the other's RR */
3050 + pri->solicitfbit[teio] = 1;
3051 + pri->retrans[teio] = 0;
3052 + q921_rr(pri, 1, 1, tei);
3053 + /* Start timer T200 to resend our RR if we don't get it */
3054 + pri->t203_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
3056 + if (pri->debug & PRI_DEBUG_Q921_STATE)
3057 + pri_message(pri, "T203 counter expired in weird state %d\n", pri->q921_state[teio]);
3058 + pri->t203_timer[teio] = 0;
3062 +static void q921_start_tei(struct pri *pri, int tei);
3065 +static void t201_expire(void *vpri, int tei)
3067 + struct pri *pri = vpri;
3068 + int teio=tei - Q921_TEI_BASE;
3070 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
3071 + if (tei == Q921_TEI_GROUP) {
3072 + for (i=0;i<Q921_MAX_TEIS;i++) {
3073 + if (pri->q921_tei_check[i] == 1) {
3074 + pri->q921_tei_check[i] = 0;
3075 + q921_start_tei(pri, Q921_TEI_BASE + i);
3079 - if (pri->debug & PRI_DEBUG_Q921_STATE)
3080 - pri_message(pri, "T203 counter expired in weird state %d\n", pri->q921_state);
3081 - pri->t203_timer = 0;
3082 + if (pri->q921_tei_check[teio] == 1) {
3083 + pri->q921_tei_check[teio] = 0;
3084 + q921_start_tei(pri, tei);
3087 + pri->t201_timer[teio] = 0;
3090 static pri_event *q921_handle_iframe(struct pri *pri, q921_i *i, int len)
3094 + int teio= i->h.tei - Q921_TEI_BASE;
3095 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
3096 +// pri_error(pri, "q921_handle_iframe: i->n_s %d pri->v_r[%d] %d \n", i->n_s, teio, pri->v_r[teio]);
3097 /* Make sure this is a valid packet */
3098 - if (i->n_s == pri->v_r) {
3099 + if (i->n_s == pri->v_r[teio]) {
3100 /* Increment next expected I-frame */
3101 - Q921_INC(pri->v_r);
3102 + Q921_INC(pri->v_r[teio]);
3103 /* Handle their ACK */
3105 - ev = q921_ack_rx(pri, i->n_r);
3106 + pri->sentrej[teio] = 0;
3107 + ev = q921_ack_rx(pri, i->n_r, i->h.tei);
3109 + pri_error(pri, "q921_handle_iframe: ev = %d \n", ev->e);
3114 /* If the Poll/Final bit is set, immediate send the RR */
3115 - q921_rr(pri, 1, 0);
3116 - } else if (pri->busy) {
3117 - q921_rr(pri, 0, 0);
3118 + q921_rr(pri, 1, 0, i->h.tei);
3119 + } else if (pri->busy[teio]) {
3120 + q921_rr(pri, 0, 0, i->h.tei);
3121 + } else if (pri->t200_timer[teio] && pri->retrans[teio]) {
3122 + pri_error(pri, "q921_handle_iframe: sending RR \n");
3123 + q921_rr(pri, 0, 0, i->h.tei);
3125 /* Receive Q.931 data */
3126 - res = q931_receive(pri, (q931_h *)i->data, len - 4);
3127 + res = q931_receive(pri, (q931_h *)i->data, len - 4, i->h.tei);
3128 /* Send an RR if one wasn't sent already */
3129 - if (pri->v_na != pri->v_r)
3130 - q921_rr(pri, 0, 0);
3131 + if (pri->v_na[teio] != pri->v_r[teio])
3132 + q921_rr(pri, 0, 0, i->h.tei);
3136 @@ -500,10 +1143,10 @@
3138 /* If we haven't already sent a reject, send it now, otherwise
3139 we are obliged to RR */
3140 - if (!pri->sentrej)
3141 - q921_reject(pri, i->p_f);
3142 + if (!pri->sentrej[teio])
3143 + q921_reject(pri, i->p_f, i->h.tei);
3145 - q921_rr(pri, 1, 0);
3146 + q921_rr(pri, 1, 0, i->h.tei);
3150 @@ -641,75 +1284,158 @@
3154 -static pri_event *q921_dchannel_up(struct pri *pri)
3155 +static void q921_tei_recovery(struct pri *pri, int tei) {
3157 + int teio=tei - Q921_TEI_BASE;
3158 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
3159 + for (i=0;i<Q921_MAX_TEIS;i++) {
3160 + if ((i + Q921_TEI_BASE == tei) || (tei == Q921_TEI_GROUP)) {
3161 + pri->q921_tei_check[i] = 1;
3164 + q921_send_teichkreq(pri, tei);
3165 + if (!pri->t201_timer[teio]) {
3166 + pri->t201_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T201], t201_expire, pri, tei);
3170 +static void q921_invoke_tei_recovery(void *vpri, int tei) {
3171 + struct pri *pri = vpri;
3172 + q921_tei_recovery(pri, tei);
3176 +static pri_event *q921_dchannel_up(struct pri *pri, int tei)
3178 - /* Reset counters, etc */
3180 + // we treat this as MFE
3181 + int teio=tei - Q921_TEI_BASE;
3182 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
3184 + if (((pri->localtype == BRI_CPE_PTMP) || (pri->localtype == BRI_CPE) || (pri->localtype == BRI_NETWORK)) && (pri->txqueue[teio])) {
3185 + /* Reset counters, etc */
3186 + q921_reset(pri, tei, 0);
3188 + /* Reset counters, discard frames, etc */
3189 + q921_reset(pri, tei, 1);
3192 /* Stop any SABME retransmissions */
3193 - pri_schedule_del(pri, pri->sabme_timer);
3194 - pri->sabme_timer = 0;
3195 + if (pri->sabme_timer[teio]) {
3196 + pri_schedule_del(pri, pri->sabme_timer[teio]);
3197 + pri->sabme_timer[teio] = 0;
3200 + if (pri->t202_timer[teio]) {
3201 + pri_schedule_del(pri, pri->t202_timer[teio]);
3202 + pri->t202_timer[teio] = 0;
3206 + if (pri->t203_timer[teio]) {
3207 + pri_schedule_del(pri, pri->t203_timer[teio]);
3208 + pri->t203_timer[teio] = 0;
3211 /* Reset any rejects */
3213 + pri->sentrej[teio] = 0;
3215 /* Go into connection established state */
3216 - pri->q921_state = Q921_LINK_CONNECTION_ESTABLISHED;
3218 - /* Start the T203 timer */
3219 - pri->t203_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri);
3220 + pri->q921_state[teio] = Q921_LINK_CONNECTION_ESTABLISHED;
3222 - /* Report event that D-Channel is now up */
3223 - pri->ev.gen.e = PRI_EVENT_DCHAN_UP;
3225 + if (((pri->localtype == BRI_CPE_PTMP) || (pri->localtype == BRI_CPE) || (pri->localtype == BRI_NETWORK)) && (pri->txqueue[teio])) {
3226 + /* transmit queued iframes ans start T200 (handled by flush_txqueue) */
3227 + q921_flush_txqueue(pri, tei, 0);
3228 + /* dont upset upper layers if we reactivate layer 2 */
3231 + /* Start the T203 timer */
3232 + pri->t203_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri, tei);
3234 + /* Report event that D-Channel is now up */
3235 + pri->ev.gen.e = PRI_EVENT_DCHAN_UP;
3236 + pri->ev.gen.tei = tei;
3241 -static pri_event *q921_dchannel_down(struct pri *pri)
3242 +static pri_event *q921_dchannel_down(struct pri *pri, int tei)
3244 + int teio=tei - Q921_TEI_BASE;
3245 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
3247 + pri->q921_state[teio] = Q921_LINK_CONNECTION_RELEASED;
3249 /* Reset counters, reset sabme timer etc */
3251 + if (pri->localtype == BRI_NETWORK_PTMP) {
3252 + if (pri->t203_timer[teio]) {
3253 + pri_schedule_del(pri, pri->t203_timer[teio]);
3254 + pri->t203_timer[teio] = 0;
3255 + if (pri->debug & PRI_DEBUG_Q921_STATE) {
3256 + pri_message(pri, "Stopping T_203 timer for TEI %d\n", tei);
3261 + q921_reset(pri, tei, 1);
3263 - /* Report event that D-Channel is now up */
3264 + /* Report event that D-Channel is now down */
3265 pri->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
3266 + pri->ev.gen.tei = tei;
3270 -void q921_reset(struct pri *pri)
3271 +void q921_reset(struct pri *pri, int tei, int discard)
3273 + int teio=tei - Q921_TEI_BASE;
3274 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
3275 /* Having gotten a SABME we MUST reset our entire state */
3280 - pri->window = pri->timers[PRI_TIMER_K];
3281 - pri->windowlen = 0;
3282 - pri_schedule_del(pri, pri->sabme_timer);
3283 - pri_schedule_del(pri, pri->t203_timer);
3284 - pri_schedule_del(pri, pri->t200_timer);
3285 - pri->sabme_timer = 0;
3286 - pri->t203_timer = 0;
3287 - pri->t200_timer = 0;
3289 - pri->solicitfbit = 0;
3290 - pri->q921_state = Q921_LINK_CONNECTION_RELEASED;
3294 + pri->v_s[teio] = 0;
3296 + pri->v_a[teio] = 0;
3297 + pri->v_r[teio] = 0;
3298 + pri->v_na[teio] = 0;
3299 + pri->window[teio] = pri->timers[PRI_TIMER_K];
3300 + pri->windowlen[teio] = 0;
3301 + pri_schedule_del(pri, pri->sabme_timer[teio]);
3302 + pri_schedule_del(pri, pri->t203_timer[teio]);
3303 + pri_schedule_del(pri, pri->t200_timer[teio]);
3304 + pri->sabme_timer[teio] = 0;
3305 + pri->t203_timer[teio] = 0;
3306 + pri->t200_timer[teio] = 0;
3307 + pri_schedule_del(pri, pri->t202_timer[teio]);
3308 + pri->t202_timer[teio] = 0;
3309 + pri_schedule_del(pri, pri->t201_timer[teio]);
3310 + pri->t201_timer[teio] = 0;
3311 + pri->busy[teio] = 0;
3312 + pri->solicitfbit[teio] = 0;
3313 + pri->q921_state[teio] = Q921_LINK_CONNECTION_RELEASED;
3314 + pri->retrans[teio] = 0;
3315 + pri->sabme_retrans[teio] = 0;
3316 + pri->sentrej[teio] = 0;
3318 - /* Discard anything waiting to go out */
3319 - q921_discard_retransmissions(pri);
3321 + /* Discard anything waiting to go out */
3322 + q921_discard_retransmissions(pri, tei);
3327 static pri_event *__q921_receive_qualified(struct pri *pri, q921_h *h, int len)
3334 + int teio=h->h.tei - Q921_TEI_BASE;
3335 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
3337 switch(h->h.data[0] & Q921_FRAMETYPE_MASK) {
3340 - if (pri->q921_state != Q921_LINK_CONNECTION_ESTABLISHED) {
3341 - pri_error(pri, "!! Got I-frame while link state %d\n", pri->q921_state);
3342 + if (pri->q921_state[teio] != Q921_LINK_CONNECTION_ESTABLISHED) {
3343 + pri_error(pri, "!! Got I-frame while link state %d\n", pri->q921_state[teio]);
3346 /* Informational frame */
3347 @@ -720,8 +1446,10 @@
3348 return q921_handle_iframe(pri, &h->i, len);
3351 - if (pri->q921_state != Q921_LINK_CONNECTION_ESTABLISHED) {
3352 + if (pri->q921_state[teio] != Q921_LINK_CONNECTION_ESTABLISHED) {
3353 pri_error(pri, "!! Got S-frame while link down\n");
3354 + q921_send_dm(pri, 1, h->h.tei);
3355 + q921_reset(pri, h->h.tei, 1);
3359 @@ -731,80 +1459,128 @@
3362 /* Receiver Ready */
3364 + pri->busy[teio] = 0;
3365 /* Acknowledge frames as necessary */
3366 - ev = q921_ack_rx(pri, h->s.n_r);
3367 + ev = q921_ack_rx(pri, h->s.n_r, h->h.tei);
3371 - /* If it's a p/f one then send back a RR in return with the p/f bit set */
3372 - if (pri->solicitfbit) {
3373 + /* If it's a p/f one then send back a RR in return with the p/f bit set */
3374 +// pri_error(pri, "-- h->s.pf %d pri->solicitfbit[teio] %d\n", h->s.p_f, pri->solicitfbit[teio]);
3375 + if (pri->solicitfbit[teio]) {
3376 if (pri->debug & PRI_DEBUG_Q921_STATE)
3377 pri_message(pri, "-- Got RR response to our frame\n");
3379 - if (pri->debug & PRI_DEBUG_Q921_STATE)
3380 + if (pri->txqueue[teio]) {
3381 + q921_flush_txqueue(pri, pri->tei, 0);
3383 + if (pri->debug & PRI_DEBUG_Q921_STATE)
3384 pri_message(pri, "-- Unsolicited RR with P/F bit, responding\n");
3385 - q921_rr(pri, 1, 0);
3386 + q921_rr(pri, 1, 0, h->h.tei);
3389 - pri->solicitfbit = 0;
3390 + pri->solicitfbit[teio] = 0;
3394 /* Receiver not ready */
3395 if (pri->debug & PRI_DEBUG_Q921_STATE)
3396 pri_message(pri, "-- Got receiver not ready\n");
3398 - /* Send RR if poll bit set */
3399 - q921_rr(pri, h->s.p_f, 0);
3400 + if ((pri->localtype != PRI_NETWORK) && (pri->localtype != BRI_NETWORK) && (pri->localtype != BRI_NETWORK_PTMP)) {
3401 + if (h->s.p_f && h->s.h.c_r) {
3402 +// if (!pri->t200_timer[teio]) {
3403 + /* Send RR if poll bit set */
3404 + q921_rr(pri, h->s.p_f, 0, h->h.tei);
3408 + if (h->s.p_f && (!h->s.h.c_r)) {
3409 +// if (!pri->t200_timer[teio]) {
3410 + /* Send RR if poll bit set */
3411 + q921_rr(pri, h->s.p_f, 0, h->h.tei);
3415 + pri->busy[teio] = 1;
3416 + if (pri->t200_timer[teio]) {
3417 + pri_schedule_del(pri, pri->t200_timer[teio]);
3418 + pri->t200_timer[teio] = 0;
3422 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, h->h.tei);
3425 /* Just retransmit */
3426 if (pri->debug & PRI_DEBUG_Q921_STATE)
3427 pri_message(pri, "-- Got reject requesting packet %d... Retransmitting.\n", h->s.n_r);
3429 - /* If it has the poll bit set, send an appropriate supervisory response */
3430 - q921_rr(pri, 1, 0);
3434 + q921_ack_rx(pri, h->s.n_r, h->h.tei);
3435 + /* Reset t200 timer if it was somehow going */
3436 + if (pri->t200_timer[teio]) {
3437 + pri_schedule_del(pri, pri->t200_timer[teio]);
3438 + pri->t200_timer[teio] = 0;
3440 /* Resend the proper I-frame */
3441 - for(f=pri->txqueue;f;f=f->next) {
3442 + for(f=pri->txqueue[teio];f;f=f->next) {
3443 +// pri_error(pri, "!! frame n_s %d transmitted %d (searching for %d)!\n", f->h.n_s, f->transmitted, h->s.n_r);
3444 if ((sendnow || (f->h.n_s == h->s.n_r)) && f->transmitted) {
3445 /* Matches the request, or follows in our window, and has
3446 already been transmitted. */
3448 - pri_error(pri, "!! Got reject for frame %d, retransmitting frame %d now, updating n_r!\n", h->s.n_r, f->h.n_s);
3449 - f->h.n_r = pri->v_r;
3450 - q921_transmit(pri, (q921_h *)(&f->h), f->len);
3452 + /* multiframe established */
3453 + f->transmitted = 0;
3454 + f->h.n_r = pri->v_r[teio];
3456 + if (pri->busy[teio]) {
3457 + pri->busy[teio] = 0;
3462 + pri_error(pri, "!! Got reject for frame %d, retransmitting frame %d now, updating n_r!\n", h->s.n_r, f->h.n_s);
3463 + q921_transmit(pri, (q921_h *)(&f->h), f->len);
3469 - if (pri->txqueue) {
3470 - /* This should never happen */
3471 - if (!h->s.p_f || h->s.n_r) {
3472 + if (pri->txqueue[teio]) {
3474 + q921_rr(pri, 1, 0, h->h.tei);
3475 + } else if (!h->s.p_f || h->s.n_r) {
3476 pri_error(pri, "!! Got reject for frame %d, but we only have others!\n", h->s.n_r);
3479 /* Hrm, we have nothing to send, but have been REJ'd. Reset v_a, v_s, etc */
3480 - pri_error(pri, "!! Got reject for frame %d, but we have nothing -- resetting!\n", h->s.n_r);
3481 - pri->v_a = h->s.n_r;
3482 - pri->v_s = h->s.n_r;
3483 - /* Reset t200 timer if it was somehow going */
3484 - if (pri->t200_timer) {
3485 - pri_schedule_del(pri, pri->t200_timer);
3486 - pri->t200_timer = 0;
3488 +/* pri_error(pri, "!! Got reject for frame %d, but we have nothing -- resetting!\n", h->s.n_r);
3489 + pri->v_a[teio] = h->s.n_r;
3490 + pri->v_s[teio] = h->s.n_r; */
3491 /* Reset and restart t203 timer */
3492 - if (pri->t203_timer)
3493 - pri_schedule_del(pri, pri->t203_timer);
3494 - pri->t203_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri);
3495 + if (pri->t203_timer[teio])
3496 + pri_schedule_del(pri, pri->t203_timer[teio]);
3497 + pri->t203_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri, tei);
3498 + pri->solicitfbit[teio] = 0;
3503 + /* If it has the poll bit set (and an iframe was retransmitted), send an appropriate supervisory response */
3504 +// q921_rr(pri, 1, 0, h->h.tei);
3505 + /* Reset t200 timer if it was somehow going */
3506 + if (pri->t200_timer[teio]) {
3507 + pri_schedule_del(pri, pri->t200_timer[teio]);
3508 + pri->t200_timer[teio] = 0;
3510 + /* Reset and restart t203 timer */
3511 + pri->solicitfbit[teio] = 0;
3512 + if (pri->t203_timer[teio])
3513 + pri_schedule_del(pri, pri->t203_timer[teio]);
3514 + pri->t203_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri, h->h.tei);
3519 pri_error(pri, "!! XXX Unknown Supervisory frame ss=0x%02x,pf=%02xnr=%02x vs=%02x, va=%02x XXX\n", h->s.ss, h->s.p_f, h->s.n_r,
3520 - pri->v_s, pri->v_a);
3521 + pri->v_s[teio], pri->v_a[teio]);
3525 @@ -821,8 +1597,16 @@
3526 if (pri->debug & PRI_DEBUG_Q921_STATE)
3527 pri_message(pri, "-- Got DM Mode from peer.\n");
3528 /* Disconnected mode, try again after T200 */
3529 - ev = q921_dchannel_down(pri);
3530 - q921_start(pri, 0);
3531 + ev = q921_dchannel_down(pri, h->h.tei);
3532 + if ((pri->localtype == BRI_CPE_PTMP) || (pri->localtype == BRI_CPE) || (pri->localtype == BRI_NETWORK)) {
3533 + /* release layer 2 */
3534 + // pri_error(pri, "got DM, restarting layer 2.\n");
3536 + q921_start(pri, 0, h->h.tei);
3538 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)){
3539 + q921_start(pri, 0, h->h.tei);
3544 @@ -830,21 +1614,153 @@
3545 pri_message(pri, "-- Ignoring unsolicited DM with p/f set to 0\n");
3547 /* Requesting that we start */
3548 - q921_start(pri, 0);
3549 + q921_start(pri, 0, h->h.tei);
3553 - } else if (!h->u.m2) {
3554 - pri_message(pri, "XXX Unnumbered Information not implemented XXX\n");
3555 + } else if (h->u.m2 == 0) {
3556 + if (h->u.ft == 3) {
3557 + switch (h->u.data[0]) { /* Management Entity Identifier */
3559 + /* TEI Procedure */
3560 + switch (h->u.data[3]) {
3561 + case Q921_TEI_ID_VERIFY:
3562 + if (pri->localtype != BRI_NETWORK_PTMP)
3564 + if (pri->debug & PRI_DEBUG_Q921_STATE)
3565 + pri_message(pri, "got TEI verify for TEI = %d\n",h->u.data[4] >> 1);
3567 + case Q921_TEI_ID_ASSIGNED:
3568 + if (pri->localtype != BRI_CPE_PTMP)
3570 + if (pri->tei == (h->u.data[4] >> 1)) {
3571 + // TEI already assgined, CHECK_TEI or REMOVE_TEI
3572 + pri_error(pri, "Double assgined TEI!\n");
3574 + if (pri->ri == ((unsigned short) (h->u.data[1] << 8) + h->u.data[2])) {
3575 + if (pri->t202_timer[0]) {
3576 + pri_schedule_del(pri, pri->t202_timer[0]);
3577 + pri->t202_timer[0] = 0;
3579 + pri->tei = h->u.data[4] >> 1;
3581 + if (pri->debug & PRI_DEBUG_Q921_STATE)
3582 + pri_message(pri, "received TEI assign TEI = %d ri=%d\n",pri->tei,(unsigned short) (h->u.data[1] << 8) + h->u.data[2]);
3583 + pri->sabme_retrans[teio] = 0;
3584 + q921_send_sabme_now(pri, pri->tei);
3587 + case Q921_TEI_ID_REMOVE:
3588 + if (pri->localtype != BRI_CPE_PTMP)
3590 + if (((h->u.data[4] >> 1) == Q921_TEI_GROUP) || (pri->tei == (h->u.data[4] >> 1))){
3591 + if (pri->debug & PRI_DEBUG_Q921_STATE)
3592 + pri_message(pri, "received TEI remove TEI = %d\n",pri->tei);
3596 + q921_reset(pri, 0, 1);
3597 + q921_send_teireq(pri);
3600 + case Q921_TEI_ID_REQUEST:
3601 + if (pri->localtype != BRI_NETWORK_PTMP)
3604 + tei = h->u.data[4] >> 1;
3605 + if (tei != Q921_TEI_GROUP) {
3606 + pri_message(pri, "got TEI request for unavailable TEI..\n");
3607 + q921_send_teidenied(pri,((unsigned int)(h->u.data[1] << 8) + h->u.data[2]),h->u.data[4] >> 1);
3611 + for (tei=0;tei<Q921_MAX_TEIS;tei++) {
3612 + if (pri->q921_teis[tei] == 0) {
3613 + pri->q921_teis[tei] = 1;
3617 + if (tei < Q921_MAX_TEIS) {
3618 + q921_send_teiassign(pri,((unsigned int)(h->u.data[1] << 8) + h->u.data[2]),tei + Q921_TEI_BASE);
3620 + pri_error(pri, "no TEI available. starting TEI recovery procedure. dont worry!\n");
3621 + q921_tei_recovery(pri, 127);
3625 + case Q921_TEI_ID_CHK_REQ:
3626 + if (pri->localtype != BRI_CPE_PTMP)
3628 + if ((((h->u.data[4] >> 1) == Q921_TEI_GROUP) || ((h->u.data[4] >> 1) == 0) || ((h->u.data[4] >> 1) == pri->tei)) && (pri->tei > 0)) {
3629 + pri_message(pri, "received TEI check request for TEI = %d\n",h->u.data[4] >> 1);
3630 + q921_send_teichkresp(pri, pri->tei);
3633 + case Q921_TEI_ID_CHK_RES:
3634 + if (pri->localtype != BRI_NETWORK_PTMP)
3636 + teio = (h->u.data[4] >> 1) - Q921_TEI_BASE;
3637 + if ((teio < 0) || (teio >= Q921_MAX_TEIS)) break;
3638 + if (pri->q921_tei_check[teio] == 1) {
3639 + pri->q921_tei_check_ri[teio] = (h->u.data[1] << 8) + h->u.data[2];
3640 + pri->q921_tei_check[teio] = 0;
3643 + pri_error(pri, "double assgined tei for tei %d teio %d\n", h->u.data[4] >> 1, teio);
3644 + pri->q921_tei_check[teio] = 0;
3645 + q921_start_tei(pri, h->u.data[4] >> 1);
3649 + pri_message(pri, "Ri = %d TEI msg = %x TEI = %x\n", (h->u.data[1] << 8) + h->u.data[2], h->u.data[3], h->u.data[4] >> 1);
3652 + case Q931_PROTOCOL_DISCRIMINATOR:
3653 + if ((pri->localtype == BRI_CPE_PTMP) || (pri->localtype == BRI_CPE)){
3654 + res = q931_receive(pri, (q931_h *)h->u.data, len-3, h->h.tei);
3655 + /* Send an RR if one wasn't sent already */
3656 + if (pri->v_na[teio] != pri->v_r[teio])
3657 + q921_rr(pri, 0, 0, pri->tei);
3661 + if (res & Q931_RES_HAVEEVENT)
3667 + pri_message(pri, "XXX Unnumbered Information not implemented XXX\n");
3672 if (pri->debug & PRI_DEBUG_Q921_STATE)
3673 pri_message(pri, "-- Got Disconnect from peer.\n");
3675 + if (pri->q921_state[teio] != Q921_LINK_CONNECTION_ESTABLISHED) {
3676 + q921_send_dm(pri, 1, h->h.tei);
3681 - q921_send_ua(pri, h->u.p_f);
3682 - ev = q921_dchannel_down(pri);
3683 - q921_start(pri, 0);
3684 + q921_send_ua(pri, h->u.p_f, h->h.tei);
3685 + ev = q921_dchannel_down(pri, h->h.tei);
3686 + if ((pri->localtype == BRI_CPE_PTMP) || (pri->localtype == BRI_CPE) || (pri->localtype == BRI_NETWORK)) {
3687 +#ifndef LAYER2ALWAYSUP
3688 + /* release layer 2 */
3691 + /* keep layer 2 up */
3692 + if (pri->t203_timer[teio])
3693 + pri_schedule_del(pri, pri->t203_timer[teio]);
3694 + pri->t203_timer[teio] = 0;
3695 + q921_send_sabme(pri, 1, pri->tei);
3698 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)){
3699 + q921_start(pri, 0, 0);
3704 @@ -866,17 +1782,28 @@
3707 /* Send Unnumbered Acknowledgement */
3708 - q921_send_ua(pri, h->u.p_f);
3709 - return q921_dchannel_up(pri);
3710 + q921_send_ua(pri, h->u.p_f, h->h.tei);
3711 + return q921_dchannel_up(pri, h->h.tei);
3712 } else if (h->u.m2 == 0) {
3714 - if (pri->q921_state == Q921_AWAITING_ESTABLISH) {
3715 + if (pri->q921_state[teio] == Q921_AWAITING_ESTABLISH) {
3716 if (pri->debug & PRI_DEBUG_Q921_STATE) {
3717 pri_message(pri, "-- Got UA from %s peer Link up.\n", h->h.c_r ? "cpe" : "network");
3719 - return q921_dchannel_up(pri);
3721 - pri_error(pri, "!! Got a UA, but i'm in state %d\n", pri->q921_state);
3722 + return q921_dchannel_up(pri, h->h.tei);
3724 + pri_error(pri, "!! Got a UA, but i'm in state %d\n", pri->q921_state[teio]);
3725 + if ((pri->localtype == BRI_CPE_PTMP) || (pri->localtype == BRI_CPE)) {
3726 + q921_reset(pri, h->h.tei, 1);
3727 + q921_send_teiverify(pri, h->h.tei);
3730 + // q921_send_dm(pri, 1, h->h.tei);
3732 + q921_reset(pri, h->h.tei, 1);
3737 pri_error(pri, "!! Weird frame received (m3=3, m2 = %d)\n", h->u.m2);
3739 @@ -901,19 +1828,42 @@
3743 - if (!pri->master && pri->debug & PRI_DEBUG_Q921_DUMP)
3744 - q921_dump(pri, h, len, pri->debug & PRI_DEBUG_Q921_RAW, 0);
3746 /* Check some reject conditions -- Start by rejecting improper ea's */
3747 if (h->h.ea1 || !(h->h.ea2))
3749 + if ((pri->localtype == PRI_CPE) || (pri->localtype == PRI_NETWORK)) {
3750 + /* Check for broadcasts - not yet handled (for PRI) */
3751 + if (h->h.tei == Q921_TEI_GROUP) {
3754 + } else if ((pri->localtype == BRI_CPE) || (pri->localtype == BRI_CPE_PTMP)) {
3755 + if ((h->h.tei != pri->tei) && (h->h.tei != Q921_TEI_GROUP)) {
3758 + } else if (pri->localtype == BRI_NETWORK_PTMP) {
3759 + /* discard anything from a strange TEI (strange == not assigned by us or the broadcast tei) */
3760 + if (((h->h.tei < Q921_TEI_BASE) || (h->h.tei > Q921_TEI_BASE + Q921_MAX_TEIS)) && (h->h.tei != Q921_TEI_GROUP)) {
3761 + if (pri->debug & PRI_DEBUG_Q921_DUMP)
3762 + pri_message(pri, "Received a Q.921 message from strange/unassigned TEI %d.\n");
3765 + if ((pri->q921_teis[h->h.tei - Q921_TEI_BASE] != 1) && (h->h.tei != Q921_TEI_GROUP)) {
3766 + if (pri->debug & PRI_DEBUG_Q921_DUMP)
3767 + pri_message(pri, "Received a Q.921 message from unassigned TEI %d.. Sending DM and assigning.\n", h->h.tei);
3769 + q921_send_dm(pri, 1, h->h.tei);
3770 + pri->q921_teis[h->h.tei - Q921_TEI_BASE] = 1;
3775 + if (!pri->master && pri->debug & PRI_DEBUG_Q921_DUMP)
3776 + q921_dump(pri, h, len, pri->debug & PRI_DEBUG_Q921_RAW, 0);
3778 - /* Check for broadcasts - not yet handled */
3779 - if (h->h.tei == Q921_TEI_GROUP)
3782 + if (pri->localtype != BRI_NETWORK_PTMP) {
3783 /* Check for SAPIs we don't yet handle */
3784 - if ((h->h.sapi != pri->sapi) || (h->h.tei != pri->tei)) {
3785 + if (((h->h.sapi != pri->sapi) && (h->h.sapi != Q921_SAPI_LAYER2_MANAGEMENT)) || ((h->h.tei != pri->tei) && (h->h.tei != Q921_TEI_GROUP) )) {
3786 #ifdef PROCESS_SUBCHANNELS
3787 /* If it's not us, try any subchannels we have */
3788 if (pri->subchannel)
3789 @@ -921,10 +1871,16 @@
3796 ev = __q921_receive_qualified(pri, h, len);
3797 - reschedule_t203(pri);
3800 + if (pri->localtype == BRI_CPE_PTMP) {
3801 + reschedule_t203(pri, pri->tei);
3803 + reschedule_t203(pri, h->h.tei);
3808 @@ -938,14 +1894,58 @@
3812 -void q921_start(struct pri *pri, int now)
3813 +static void q921_start_tei(struct pri *pri, int tei)
3815 - if (pri->q921_state != Q921_LINK_CONNECTION_RELEASED) {
3816 - pri_error(pri, "!! q921_start: Not in 'Link Connection Released' state\n");
3818 + int teio=tei - Q921_TEI_BASE;
3819 + if (pri->localtype != BRI_NETWORK_PTMP) { return; }
3820 + if (((teio < 0) || (teio > Q921_MAX_TEIS))) { teio=0; }
3821 + pri->q921_teis[teio] = 0;
3822 + q921_send_teiremove(pri, tei);
3823 + q921_reset(pri,tei,1);
3826 +void q921_start(struct pri *pri, int now, int tei)
3829 +/* if (pri->q921_state != Q921_LINK_CONNECTION_RELEASED) {
3830 + pri_error(pri, "!! q921_start: Not in 'Link Connection Released' state\n");
3833 + /* Reset our interface */
3834 + if (pri->localtype != BRI_NETWORK_PTMP) {
3835 + q921_reset(pri,0,1);
3837 + /* Do the SABME XXX Maybe we should implement T_WAIT? XXX */
3838 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE) || (pri->localtype == BRI_NETWORK) || (pri->localtype == BRI_CPE)) {
3839 + pri->sabme_retrans[0] = 0;
3840 + q921_send_sabme(pri, now, 0);
3843 + if (pri->localtype == BRI_NETWORK_PTMP) {
3845 + // initial start or complete restart
3846 + q921_send_teiremove(pri, 127);
3848 + for (i=0;i<Q921_MAX_TEIS;i++) {
3849 + q921_start_tei(pri,Q921_TEI_BASE+i);
3851 + /* clean up those TEIs */
3852 +// pri_schedule_event2(pri, 10000, q921_invoke_tei_recovery, pri, 127 );
3853 + } else if ((tei >= Q921_TEI_BASE) && (tei < Q921_TEI_BASE + Q921_MAX_TEIS)){
3854 + // restart of a single p2p datalink
3855 + q921_start_tei(pri,tei);
3858 + if (pri->localtype == BRI_CPE_PTMP){
3861 + /* let's get a TEI */
3862 + q921_send_teireq(pri);
3865 + /* save the planet by recycling */
3866 + pri->sabme_retrans[0] = 0;
3867 + q921_send_sabme(pri, now, tei);
3870 - /* Reset our interface */
3872 - /* Do the SABME XXX Maybe we should implement T_WAIT? XXX */
3873 - q921_send_sabme(pri, now);
3875 diff -urN libpri-1.2.3.orig/q931.c libpri-1.2.3/q931.c
3876 --- libpri-1.2.3.orig/q931.c 2006-04-27 18:08:39.000000000 +0200
3877 +++ libpri-1.2.3/q931.c 2006-08-01 10:55:05.000000000 +0200
3880 * libpri: An implementation of Primary Rate ISDN
3882 - * Written by Mark Spencer <markster@digium.com>
3883 + * Written by Mark Spencer <markster@linux-support.net>
3885 - * Copyright (C) 2001-2005, Digium
3886 + * Copyright (C) 2001, Linux Support Services, Inc.
3887 * All Rights Reserved.
3888 + * Copyright (C) 2003-2006 Junghanns.NET GmbH
3889 + * Klaus-Peter Junghanns <kpj@junghanns.net>
3891 * This program is free software; you can redistribute it and/or modify
3892 * it under the terms of the GNU General Public License as published by
3894 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
3901 #include "pri_internal.h"
3910 @@ -205,6 +208,14 @@
3911 #define LOC_INTERNATIONAL_NETWORK 0x7
3912 #define LOC_NETWORK_BEYOND_INTERWORKING 0xa
3922 static char *ie2str(int ie);
3923 static char *msg2str(int msg);
3926 static void call_init(struct q931_call *c)
3928 memset(c, 0, sizeof(*c));
3931 c->sendhangupack = 0;
3932 c->forceinvert = -1;
3933 @@ -253,8 +265,16 @@
3938 + c->t303running = 0;
3941 c->ourcallstate = Q931_CALL_STATE_NULL;
3942 c->peercallstate = Q931_CALL_STATE_NULL;
3945 + c->causecode = -1;
3949 static char *binary(int b, int len) {
3950 @@ -272,14 +292,17 @@
3954 -#ifdef NO_BRI_SUPPORT
3955 - if (!ie->data[0] & 0x20) {
3956 - pri_error(pri, "!! Not PRI type!?\n");
3958 + if ((pri->localtype != PRI_CPE) && (pri->localtype != PRI_NETWORK)) {
3959 + // pri_error(pri, "!! BRI type %d!?\n",ie->data[0] & 0x03);
3960 + call->channelno = ie->data[0] & 0x03;
3961 + if (call->channelno == 3) {
3962 + call->channelno = -1; // any channel
3967 #ifndef NOAUTO_CHANNEL_SELECTION_SUPPORT
3968 - switch (ie->data[0] & 3) {
3969 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
3970 + switch (ie->data[0] & 3) {
3972 call->justsignalling = 1;
3976 pri_error(pri, "!! Unexpected Channel selection %d\n", ie->data[0] & 3);
3981 if (ie->data[0] & 0x08)
3982 @@ -349,10 +373,16 @@
3985 /* Start with standard stuff */
3986 - if (pri->switchtype == PRI_SWITCH_GR303_TMC)
3987 + if (pri->switchtype == PRI_SWITCH_GR303_TMC) {
3988 ie->data[pos] = 0x69;
3991 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
3992 ie->data[pos] = 0xa1;
3995 + ie->data[pos] = 0x80;
3998 /* Add exclusive flag if necessary */
3999 if (call->chanflags & FLAG_EXCLUSIVE)
4000 ie->data[pos] |= 0x08;
4004 if ((call->channelno > -1) || (call->slotmap != -1)) {
4005 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
4006 /* We'll have the octet 8.2 and 8.3's present */
4007 ie->data[pos++] = 0x83;
4008 if (call->channelno > -1) {
4009 @@ -384,11 +415,43 @@
4010 ie->data[pos++] = (call->slotmap & 0xff);
4016 + // pri_error(pri, "channelno %d, ds1no %d data %d\n",call->channelno,call->ds1no,ie->data[pos-1]);
4017 + if (pri->localtype == BRI_CPE_PTMP) {
4018 + if (msgtype == Q931_SETUP) {
4019 + // network, you decide!
4020 + if (call->channelno > -1) {
4021 + // ie->data[pos-1] = 0x83;
4022 + ie->data[pos-1] = 0x80 | call->channelno;
4025 + if (call->channelno > -1) {
4026 + ie->data[pos-1] |= call->channelno;
4030 + if (call->channelno > -1) {
4031 + ie->data[pos-1] |= call->channelno;
4037 + if (pri->localtype == BRI_CPE) {
4038 + ie->data[pos++] = 0x80 | 3;
4043 if (call->ds1no > 0) {
4047 + if (msgtype == Q931_RESTART_ACKNOWLEDGE) {
4048 + /* restart complete interface! */
4051 pri_error(pri, "!! No channel map, no channel, and no ds1? What am I supposed to identify?\n");
4054 @@ -734,9 +797,13 @@
4055 return code2str(pres, press, sizeof(press) / sizeof(press[0]));
4058 -static void q931_get_number(unsigned char *num, int maxlen, unsigned char *src, int len)
4059 +static void q931_get_number(char *num, int maxlen, unsigned char *src, int len)
4061 - if ((len < 0) || (len > maxlen - 1)) {
4063 + pri_error(NULL, "q931_get_number received invalid len = %d\n", len);
4066 + if (len > maxlen - 1) {
4070 @@ -746,50 +813,75 @@
4072 static FUNC_DUMP(dump_called_party_number)
4074 - unsigned char cnum[256];
4076 - q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
4077 - pri_message(pri, "%c Called Number (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d) '%s' ]\n",
4078 - prefix, len, ie->data[0] >> 7, ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07, npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f, cnum);
4081 + q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
4082 + pri_message(pri, "%c Called Number (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d) '%s' ]\n",
4083 + prefix, len, ie->data[0] >> 7, ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07, npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f, cnum);
4085 + pri_error(pri, "Called Number (len=%2d) too short.\n", len);
4089 static FUNC_DUMP(dump_called_party_subaddr)
4091 - unsigned char cnum[256];
4092 - q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
4093 - pri_message(pri, "%c Called Sub-Address (len=%2d) [ Ext: %d Type: %s (%d) O: %d '%s' ]\n",
4094 - prefix, len, ie->data[0] >> 7,
4095 - subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
4096 - (ie->data[0] & 0x08) >> 3, cnum);
4099 + q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
4100 + pri_message(pri, "%c Called Sub-Address (len=%2d) [ Ext: %d Type: %s (%d) O: %d '%s' ]\n",
4101 + prefix, len, ie->data[0] >> 7,
4102 + subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
4103 + (ie->data[0] & 0x08) >> 3, cnum);
4105 + pri_error(pri, "Called Party Subaddress (len=%2d) too short.\n", len);
4109 static FUNC_DUMP(dump_calling_party_number)
4111 - unsigned char cnum[256];
4112 - if (ie->data[0] & 0x80)
4113 - q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
4115 - q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
4116 - pri_message(pri, "%c Calling Number (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d)\n", prefix, len, ie->data[0] >> 7, ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07, npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
4117 - if (ie->data[0] & 0x80)
4118 - pri_message(pri, "%c Presentation: %s (%d) '%s' ]\n", prefix, pri_pres2str(0), 0, cnum);
4120 - pri_message(pri, "%c Presentation: %s (%d) '%s' ]\n", prefix, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, cnum);
4122 + if ((ie->data[0] & 0x80) && (len >= 3)) {
4123 + q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
4124 + pri_message(pri, "%c Calling Number (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d)\n", prefix, len, ie->data[0] >> 7, ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07, npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
4125 + pri_message(pri, "%c Presentation: %s (%d) '%s' ]\n", prefix, pri_pres2str(0), 0, cnum);
4126 + } else if (len >= 4) {
4127 + q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
4128 + pri_message(pri, "%c Calling Number (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d)\n", prefix, len, ie->data[0] >> 7, ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07, npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
4129 + pri_message(pri, "%c Presentation: %s (%d) '%s' ]\n", prefix, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, cnum);
4131 + pri_error(pri, "Calling Party Number (len=%2d) too short.\n", len);
4135 static FUNC_DUMP(dump_calling_party_subaddr)
4137 - unsigned char cnum[256];
4138 - q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
4139 - pri_message(pri, "%c Calling Sub-Address (len=%2d) [ Ext: %d Type: %s (%d) O: %d '%s' ]\n",
4140 - prefix, len, ie->data[0] >> 7,
4141 - subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
4142 - (ie->data[0] & 0x08) >> 3, cnum);
4145 + q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
4146 + pri_message(pri, "%c Calling Sub-Address (len=%2d) [ Ext: %d Type: %s (%d) O: %d '%s' ]\n",
4147 + prefix, len, ie->data[0] >> 7,
4148 + subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
4149 + (ie->data[0] & 0x08) >> 3, cnum);
4151 + pri_error(pri, "Calling Party Subaddress (len=%2d) too short.\n", len);
4155 +static FUNC_DUMP(dump_colp)
4159 + q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
4160 + pri_message(pri, "%c COLP (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d)\n", prefix, len, ie->data[0] >> 7, ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07, npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
4161 + pri_message(pri, "%c Presentation: %s (%d) '%s' ]\n", prefix, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, cnum);
4163 + pri_error(pri, "COLP (len=%2d) too short.\n", len);
4167 static FUNC_DUMP(dump_redirecting_number)
4169 - unsigned char cnum[256];
4172 /* To follow Q.931 (4.5.1), we must search for start of octet 4 by
4173 walking through all bytes until one with ext bit (8) set to 1 */
4174 @@ -810,13 +902,17 @@
4177 while(!(ie->data[i++]& 0x80));
4178 - q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
4179 - pri_message(pri, " '%s' ]\n", cnum);
4180 + if ((ie->len - i) >= 0) {
4181 + q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
4182 + pri_message(pri, " '%s' ]\n", cnum);
4184 + pri_error(pri, "Redirecting Number (len=%2d) too short.\n", len);
4188 static FUNC_DUMP(dump_connected_number)
4190 - unsigned char cnum[256];
4193 /* To follow Q.931 (4.5.1), we must search for start of octet 4 by
4194 walking through all bytes until one with ext bit (8) set to 1 */
4195 @@ -833,8 +929,12 @@
4198 while(!(ie->data[i++]& 0x80));
4199 - q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
4200 - pri_message(pri, " '%s' ]\n", cnum);
4201 + if ((ie->len - i) >= 0) {
4202 + q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
4203 + pri_message(pri, " '%s' ]\n", cnum);
4205 + pri_error(pri, "Connected Number (len=%2d) too short.\n", len);
4213 while(!(ie->data[i++] & 0x80));
4214 - q931_get_number((unsigned char *) call->redirectingnum, sizeof(call->redirectingnum), ie->data + i, ie->len - i);
4215 + q931_get_number(call->redirectingnum, sizeof(call->redirectingnum), ie->data + i, ie->len - i);
4223 - if (call->redirectingnum && *call->redirectingnum) {
4224 + if (call->redirectingnum && strlen(call->redirectingnum)) {
4225 ie->data[0] = call->redirectingplan;
4226 ie->data[1] = call->redirectingpres;
4227 ie->data[2] = (call->redirectingreason & 0x0f) | 0x80;
4228 @@ -878,67 +978,90 @@
4230 static FUNC_DUMP(dump_redirecting_subaddr)
4232 - unsigned char cnum[256];
4233 - q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
4234 - pri_message(pri, "%c Redirecting Sub-Address (len=%2d) [ Ext: %d Type: %s (%d) O: %d '%s' ]\n",
4235 - prefix, len, ie->data[0] >> 7,
4236 - subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
4237 - (ie->data[0] & 0x08) >> 3, cnum);
4240 + q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
4241 + pri_message(pri, "%c Redirecting Sub-Address (len=%2d) [ Ext: %d Type: %s (%d) O: %d '%s' ]\n",
4242 + prefix, len, ie->data[0] >> 7,
4243 + subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
4244 + (ie->data[0] & 0x08) >> 3, cnum);
4246 + pri_error(pri, "Redirecting Subaddress (len=%2d) too short.\n", len);
4250 static FUNC_RECV(receive_calling_party_subaddr)
4252 /* copy digits to call->callingsubaddr */
4253 - q931_get_number((unsigned char *) call->callingsubaddr, sizeof(call->callingsubaddr), ie->data + 1, len - 3);
4255 + q931_get_number(call->callingsubaddr, sizeof(call->callingsubaddr), ie->data + 1, len - 3);
4257 + pri_error(call->pri, "Calling Party Subaddress (len=%2d) too short.\n", len);
4262 static FUNC_RECV(receive_called_party_number)
4264 - /* copy digits to call->callednum */
4265 - q931_get_number((unsigned char *) call->callednum, sizeof(call->callednum), ie->data + 1, len - 3);
4266 - call->calledplan = ie->data[0] & 0x7f;
4267 + /* copy digits to call->callednum or call->digits */
4269 + if (msgtype == Q931_INFORMATION) {
4270 + q931_get_number(call->digits, sizeof(call->digits), ie->data + 1, len - 3);
4272 + q931_get_number(call->callednum, sizeof(call->callednum), ie->data + 1, len - 3);
4274 + call->calledplan = ie->data[0] & 0x7f;
4276 + pri_error(call->pri, "Called Party Number (len=%2d) too short.\n", len);
4281 static FUNC_SEND(transmit_called_party_number)
4283 ie->data[0] = 0x80 | call->calledplan;
4284 - if (*call->callednum)
4285 + if (strlen(call->callednum))
4286 memcpy(ie->data + 1, call->callednum, strlen(call->callednum));
4287 return strlen(call->callednum) + 3;
4290 static FUNC_RECV(receive_calling_party_number)
4295 - if (ie->data[0] & 0x80) {
4296 - data = ie->data + 1;
4298 - call->callerpres = 0; /* PI presentation allowed SI user-provided, not screened */
4300 - data = ie->data + 2;
4302 - call->callerpres = ie->data[1] & 0x7f;
4305 - if (call->callerpres == PRES_ALLOWED_NETWORK_NUMBER ||
4306 - call->callerpres == PRES_PROHIB_NETWORK_NUMBER) {
4307 - q931_get_number((u_int8_t *)call->callerani, sizeof(call->callerani), data, length);
4308 - call->callerplanani = ie->data[0] & 0x7f;
4310 - if (!*call->callernum) { /*Copy ANI to CallerID if CallerID is not already set */
4311 - libpri_copy_string(call->callernum, call->callerani, sizeof(call->callernum));
4312 - call->callerplan = call->callerplanani;
4316 - q931_get_number((u_int8_t *)call->callernum, sizeof(call->callernum), data, length);
4317 - call->callerplan = ie->data[0] & 0x7f;
4320 + if (strlen(call->callernum)) {
4321 + call->callerplanuser = ie->data[0] & 0x7f;
4323 + call->callerplan = ie->data[0] & 0x7f;
4326 + if (ie->data[0] & 0x80) {
4328 + if (strlen(call->callernum)) {
4329 + // got A NUM already (this is not 100% correct, but the network should do it this way to protect bad implementations
4330 + q931_get_number(call->callerani, sizeof(call->callerani), ie->data + 1, len - 3);
4331 + call->callerpresuser = 0; /* PI presentation allowed
4332 + SI user-provided, not screened */
4334 + q931_get_number(call->callernum, sizeof(call->callernum), ie->data + 1, len - 3);
4335 + call->callerpres = 0; /* PI presentation allowed
4336 + SI user-provided, not screened */
4339 + pri_error(call->pri, "Calling Party Number (len=%2d) too short.\n", len);
4343 + if (strlen(call->callernum)) {
4344 + q931_get_number(call->callerani, sizeof(call->callerani), ie->data + 2, len - 4);
4345 + call->callerpresuser = ie->data[1] & 0x7f;
4347 + q931_get_number(call->callernum, sizeof(call->callernum), ie->data + 2, len - 4);
4348 + call->callerpres = ie->data[1] & 0x7f;
4351 + pri_error(call->pri, "Calling Party Number (len=%2d) too short.\n", len);
4357 @@ -946,7 +1069,7 @@
4359 ie->data[0] = call->callerplan;
4360 ie->data[1] = 0x80 | call->callerpres;
4361 - if (*call->callernum)
4362 + if (strlen(call->callernum))
4363 memcpy(ie->data + 2, call->callernum, strlen(call->callernum));
4364 return strlen(call->callernum) + 4;
4366 @@ -964,11 +1087,89 @@
4367 static FUNC_RECV(receive_user_user)
4369 call->useruserprotocoldisc = ie->data[0] & 0xff;
4370 - if (call->useruserprotocoldisc == 4) /* IA5 */
4371 - q931_get_number((unsigned char *) call->useruserinfo, sizeof(call->useruserinfo), ie->data + 1, len - 3);
4372 + if (call->useruserprotocoldisc == 4) { /* IA5 */
4374 + q931_get_number(call->useruserinfo, sizeof(call->useruserinfo), ie->data + 1, len - 3);
4376 + pri_error(call->pri, "User-User Information (len=%2d) too short.\n", len);
4382 +static FUNC_RECV(receive_call_identity)
4385 + q931_get_number(call->callid, sizeof(call->callid), ie->data, len - 2);
4387 + pri_error(call->pri, "Call Identity (len=%2d) too short.\n", len);
4392 +static FUNC_SEND(transmit_call_identity)
4394 + if (strlen(call->callid))
4395 + memcpy(ie->data , call->callid, strlen(call->callid));
4396 + return strlen(call->callednum) + 3;
4399 +static FUNC_RECV(receive_high_layer_compat)
4404 +static FUNC_SEND(transmit_high_layer_compat)
4406 + if (call->transcapability != PRI_TRANS_CAP_RESTRICTED_DIGITAL && call->transcapability != PRI_TRANS_CAP_DIGITAL && call->transcapability != PRI_TRANS_CAP_DIGITAL_W_TONES) {
4407 + ie->data[0] = 0x91;
4408 + ie->data[1] = 0x81;
4414 +static FUNC_DUMP(dump_high_layer_compat)
4417 + pri_message(pri, "%c High-layer compatibilty (len=%2d) [ ", prefix, len);
4418 + for (x=0;x<ie->len;x++)
4419 + pri_message(pri, "0x%02X ", ie->data[x]);
4420 + pri_message(pri, " ]\n");
4424 +static FUNC_RECV(receive_low_layer_compat)
4428 + pri_error(pri, "%d bytes LLC too long\n", len);
4431 + pri_error(pri, "copying %d bytes LLC \n", len);
4432 + call->llc[0] = len;
4433 + memcpy(call->llc+1, ie->data, len);
4439 +static FUNC_SEND(transmit_low_layer_compat)
4441 + if (call->llc[0] == 0) return 0;
4442 + memcpy(ie->data, call->llc + 1, call->llc[0]);
4443 + return call->llc[0] + 2;
4446 +static FUNC_DUMP(dump_low_layer_compat)
4449 + pri_message(pri, "%c Low-layer compatibilty (len=%2d) [ ", prefix, len);
4450 + for (x=0;x<ie->len;x++)
4451 + pri_message(pri, "0x%02X ", ie->data[x]);
4452 + pri_message(pri, " ]\n");
4455 static FUNC_SEND(transmit_user_user)
4457 int datalen = strlen(call->useruserinfo);
4458 @@ -1050,22 +1251,41 @@
4462 - q931_get_number((unsigned char *) call->callername, sizeof(call->callername), data, len - 2);
4463 + if (msgtype == Q931_SETUP) {
4464 + /* we treat display IEs in the SETUP msg as callername */
4466 + q931_get_number(call->callername, sizeof(call->callername), data, len - 2);
4468 + pri_error(call->pri, "Display (len=%2d) too short.\n", len);
4471 + /* in other msgs we will pass it as text to chan_zap */
4473 + q931_get_number(call->display, sizeof(call->display), data, len - 2);
4475 + pri_error(call->pri, "Display (len=%2d) too short.\n", len);
4481 static FUNC_SEND(transmit_display)
4484 - if ((pri->switchtype != PRI_SWITCH_NI1) && (pri->switchtype != PRI_SWITCH_QSIG)
4485 - && *call->callername) {
4486 + int cpe = pri->localtype == BRI_CPE || pri->localtype == BRI_CPE_PTMP || pri->localtype == PRI_CPE;
4487 + if ((pri->switchtype != PRI_SWITCH_NI1) && (pri->switchtype != PRI_SWITCH_QSIG) && strlen(call->callername) && !cpe) {
4489 if(pri->switchtype != PRI_SWITCH_EUROISDN_E1) {
4493 - memcpy(ie->data + i, call->callername, strlen(call->callername));
4494 - return 2 + i + strlen(call->callername);
4495 + if (msgtype == Q931_SETUP_ACKNOWLEDGE) {
4496 + memcpy(ie->data + i, call->display, strlen(call->display));
4497 + return 2 + i + strlen(call->display);
4499 + memcpy(ie->data + i, call->callername, strlen(call->callername));
4500 + return 2 + i + strlen(call->callername);
4505 @@ -1112,6 +1332,111 @@
4510 + static FUNC_RECV(receive_facility_kpj)
4512 + unsigned char cpt_tag, cp_len, invoke_id_tag, invoke_id_len, operation_value_len, operation_value_tag;
4513 + unsigned char arg_len = 0;
4514 + unsigned char pos = 0;
4515 + short invoke_id = 0, operation_value = 0;
4516 + if ((ie->data[pos++] & 0x1F) == 0x11) {
4517 + /* service discriminator == supplementary services */
4518 + cpt_tag = ie->data[pos++] & 0x1F;
4519 + cp_len = ie->data[pos++];
4520 + switch (cpt_tag) {
4521 + case 1: /* invoke */
4522 + invoke_id_tag = ie->data[pos++];
4523 + if (invoke_id_tag != 0x02) {
4524 + // pri_error(call->pri, "invoke id tag != 0x02\n");
4527 + invoke_id_len = ie->data[pos++]; // 4
4528 + while (invoke_id_len > 0) {
4529 + invoke_id = (invoke_id << 8) | (ie->data[pos++] & 0xFF);
4532 + operation_value_tag = ie->data[pos++];
4533 + if (operation_value_tag != 0x02) {
4534 + // pri_error(call->pri, "operation value tag != 0x02\n");
4537 + operation_value_len = ie->data[pos++];
4538 + while (operation_value_len > 0) {
4539 + operation_value = (operation_value << 8) | (ie->data[pos++] & 0xFF);
4540 + operation_value_len--;
4542 + arg_len = ie->len - pos;
4543 + switch (operation_value) {
4544 + case 0x06: /* ECT execute */
4545 + call->facility = operation_value;
4547 + case 0x0D: /* call deflection */
4548 + call->facility = operation_value;
4551 + if (arg_len > 0) {
4553 + q931_get_number(call->redirectingnum, sizeof(call->redirectingnum), ie->data + pos, arg_len);
4555 + /* now retrieve the number */
4557 + case 0x22: /* AOC-D */
4559 + case 0x24: /* AOC-E */
4563 + case 2: /* return result */
4565 + case 3: /* return error */
4567 + case 4: /* reject */
4571 + /* OLD DIRTY ULAW HACK */
4572 + if (ie->len < 14) {
4573 + pri_error(call->pri, "!! Facility message shorter than 14 bytes\n");
4576 + if (ie->data[13] + 14 == ie->len) {
4577 + q931_get_number(call->callername, sizeof(call->callername) - 1, ie->data + 14, ie->len - 14);
4579 + call->facility = 0x0;
4584 + static FUNC_SEND(transmit_facility_kpj)
4588 + if (call->aoc && (pri->switchtype == PRI_SWITCH_EUROISDN_E1) && ((pri->localtype == BRI_NETWORK) || (pri->localtype == BRI_NETWORK_PTMP) || (pri->localtype == PRI_NETWORK))) {
4589 + ie->data[0] = 0x90; /* PP remote operations */
4590 + ie->data[i++] = 0x00; /* component tag */
4591 + ie->data[i++] = 0x02; /* invoke id tag */
4592 + ie->data[i++] = 0x02; /* invoke id len */
4593 + ie->data[i++] = 0x34; /* invoke id */
4594 + ie->data[i++] = 0x56; /* invoke id */
4595 + ie->data[i++] = 0x02; /* operation value tag */
4596 + ie->data[i++] = 0x01; /* operation value len */
4597 + switch (msgtype) {
4599 + ie->data[i++] = 0x26; /* operation value AOC-S */
4601 + case Q931_DISCONNECT:
4602 + ie->data[i++] = 0x24; /* operation value AOC-E */
4605 + ie->data[i++] = 0x22; /* operation value AOC-D */
4614 static FUNC_SEND(transmit_facility)
4616 struct apdu_event *tmp;
4617 @@ -1138,6 +1463,182 @@
4622 +static FUNC_SEND(transmit_facility)
4624 + int i = 0, j, first_i, compsp = 0;
4625 + struct rose_component *comp, *compstk[10];
4626 + unsigned char namelen = strlen(call->callername);
4628 + if ((pri->switchtype == PRI_SWITCH_NI2) && (namelen > 15))
4629 + namelen = 15; /* According to GR-1367, for NI2 switches it can't be > 15 characters */
4630 + if ((namelen > 0) && ((pri->switchtype == PRI_SWITCH_QSIG) ||
4631 + ((pri->switchtype == PRI_SWITCH_NI2) && (pri->localtype == PRI_NETWORK)))) {
4634 + ie->data[i] = 0x80 | Q932_PROTOCOL_EXTENSIONS;
4636 + /* Interpretation component */
4637 + ASN1_ADD_BYTECOMP(comp, COMP_TYPE_INTERPRETATION, ie->data, i, 0x00 /* Discard unrecognized invokes */);
4640 + ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, ie->data, i);
4641 + ASN1_PUSH(compstk, compsp, comp);
4643 + /* Invoke component contents */
4645 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, ie->data, i, ++pri->last_invoke);
4647 + /* Operation Tag */
4648 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, ie->data, i, SS_CNID_CALLINGNAME);
4650 + /* Arugement Tag */
4651 + j = asn1_string_encode(ROSE_NAME_PRESENTATION_ALLOWED_SIMPLE, &ie->data[i], len - i, 15, call->callername, namelen);
4658 + /* Fix length of stacked components */
4659 + while(compsp > 0) {
4660 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4664 + if (/*(pri->switchtype == PRI_SWITCH_EUROISDN_E1) &&*/ call->redirectingnum && strlen(call->redirectingnum)) {
4665 + if (!(first_i = i)) {
4666 + /* Add protocol information header */
4667 + ie->data[i++] = 0x80 | Q932_PROTOCOL_ROSE;
4670 + /* ROSE invoke component */
4671 + ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, ie->data, i);
4672 + ASN1_PUSH(compstk, compsp, comp);
4674 + /* ROSE invokeId component */
4675 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, ie->data, i, ++pri->last_invoke);
4677 + /* ROSE operationId component */
4678 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, ie->data, i, ROSE_DIVERTING_LEG_INFORMATION2);
4680 + /* ROSE ARGUMENT component */
4681 + ASN1_ADD_SIMPLE(comp, 0x30, ie->data, i);
4682 + ASN1_PUSH(compstk, compsp, comp);
4684 + /* ROSE DivertingLegInformation2.diversionCounter component */
4685 + /* Always is 1 because other isn't available in the current design */
4686 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, ie->data, i, 1);
4688 + /* ROSE DivertingLegInformation2.diversionReason component */
4689 + ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, ie->data, i, redirectingreason_from_q931(pri, call->redirectingreason));
4691 + /* ROSE DivertingLegInformation2.divertingNr component */
4692 + ASN1_ADD_SIMPLE(comp, 0xA1, ie->data, i);
4693 + ASN1_PUSH(compstk, compsp, comp);
4695 + /* Redirecting information always not screened */
4696 + switch(call->redirectingpres) {
4697 + case PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
4698 + case PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
4699 + if (call->redirectingnum && strlen(call->redirectingnum)) {
4700 + ASN1_ADD_SIMPLE(comp, 0xA0, ie->data, i);
4701 + ASN1_PUSH(compstk, compsp, comp);
4703 + /* NPI of redirected number is not supported in the current design */
4704 + ASN1_ADD_SIMPLE(comp, 0xA1, ie->data, i);
4705 + ASN1_PUSH(compstk, compsp, comp);
4707 + ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, ie->data, i, typeofnumber_from_q931(pri, call->redirectingplan >> 4));
4709 + j = asn1_string_encode(ASN1_NUMERICSTRING, &ie->data[i], len - i, 20, call->redirectingnum, strlen(call->redirectingnum));
4715 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4716 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4719 + /* fall through */
4720 + case PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
4721 + case PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
4722 + ASN1_ADD_SIMPLE(comp, 0x81, ie->data, i);
4724 + /* Don't know how to handle this */
4725 + case PRES_ALLOWED_NETWORK_NUMBER:
4726 + case PRES_PROHIB_NETWORK_NUMBER:
4727 + case PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
4728 + case PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
4729 + ASN1_ADD_SIMPLE(comp, 0x81, ie->data, i);
4732 + pri_message(pri, "!! Undefined presentation value for redirecting number: %d\n", call->redirectingpres);
4733 + case PRES_NUMBER_NOT_AVAILABLE:
4734 + ASN1_ADD_SIMPLE(comp, 0x82, ie->data, i);
4737 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4739 + /* ROSE DivertingLegInformation2.originalCalledNr component */
4740 + /* This information isn't supported by current design - duplicate divertingNr */
4741 + ASN1_ADD_SIMPLE(comp, 0xA2, ie->data, i);
4742 + ASN1_PUSH(compstk, compsp, comp);
4744 + /* Redirecting information always not screened */
4745 + switch(call->redirectingpres) {
4746 + case PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
4747 + case PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
4748 + if (call->redirectingnum && strlen(call->redirectingnum)) {
4749 + ASN1_ADD_SIMPLE(comp, 0xA0, ie->data, i);
4750 + ASN1_PUSH(compstk, compsp, comp);
4752 + ASN1_ADD_SIMPLE(comp, 0xA1, ie->data, i);
4753 + ASN1_PUSH(compstk, compsp, comp);
4755 + ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, ie->data, i, typeofnumber_from_q931(pri, call->redirectingplan >> 4));
4757 + j = asn1_string_encode(ASN1_NUMERICSTRING, &ie->data[i], len - i, 20, call->redirectingnum, strlen(call->redirectingnum));
4763 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4764 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4767 + /* fall through */
4768 + case PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
4769 + case PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
4770 + ASN1_ADD_SIMPLE(comp, 0x81, ie->data, i);
4772 + /* Don't know how to handle this */
4773 + case PRES_ALLOWED_NETWORK_NUMBER:
4774 + case PRES_PROHIB_NETWORK_NUMBER:
4775 + case PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
4776 + case PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
4777 + ASN1_ADD_SIMPLE(comp, 0x81, ie->data, i);
4780 + pri_message(pri, "!! Undefined presentation value for redirecting number: %d\n", call->redirectingpres);
4781 + case PRES_NUMBER_NOT_AVAILABLE:
4782 + ASN1_ADD_SIMPLE(comp, 0x82, ie->data, i);
4785 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4787 + /* Fix length of stacked components */
4788 + while(compsp > 0) {
4789 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4793 + return (i ? i+2 : 0);
4797 static FUNC_RECV(receive_facility)
4800 @@ -1346,6 +1847,7 @@
4801 pri_message(pri, " ]\n");
4805 static FUNC_DUMP(dump_time_date)
4807 pri_message(pri, "%c Time Date (len=%2d) [ ", prefix, len);
4808 @@ -1366,7 +1868,7 @@
4810 static FUNC_DUMP(dump_keypad_facility)
4813 + char tmp[64] = "";
4815 if (ie->len == 0 || ie->len > sizeof(tmp))
4817 @@ -1377,28 +1879,49 @@
4819 static FUNC_RECV(receive_keypad_facility)
4827 if (ie->len > (sizeof(call->digitbuf) - 1))
4828 - mylen = (sizeof(call->digitbuf) - 1);
4829 + mylen = sizeof(call->digitbuf) - 1;
4833 - memcpy(call->digitbuf, ie->data, mylen);
4834 + strncpy(call->digitbuf, (char *) ie->data, mylen);
4836 - call->digitbuf[mylen] = 0;
4837 + /* I must be really neurotic */
4838 + call->digitbuf[sizeof(call->digitbuf)-1] = '\0';
4843 +static FUNC_RECV(receive_time_date)
4848 +static FUNC_SEND(transmit_time_date) {
4850 + struct tm *timedate;
4852 + timedate = localtime(&now);
4853 + ie->data[0] = timedate->tm_year - 100; // 1900+
4854 + ie->data[1] = timedate->tm_mon + 1;
4855 + ie->data[2] = timedate->tm_mday;
4856 + ie->data[3] = timedate->tm_hour;
4857 + ie->data[4] = timedate->tm_min;
4863 static FUNC_DUMP(dump_display)
4866 char *buf = malloc(len + 1);
4871 if ((x < ie->len) && (ie->data[x] & 0x80)) {
4872 @@ -1413,7 +1936,7 @@
4876 -static void dump_ie_data(unsigned char *c, int len)
4877 +static void dump_ie_data(struct pri *pri, unsigned char *c, int len)
4879 char tmp[1024] = "";
4881 @@ -1423,7 +1946,7 @@
4882 ((*c >= 'a') && (*c <= 'z')) ||
4883 ((*c >= '0') && (*c <= '9'))) {
4886 + if (strlen(tmp)) {
4890 @@ -1435,7 +1958,7 @@
4895 + if (strlen(tmp)) {
4899 @@ -1448,14 +1971,14 @@
4903 - pri_message(NULL, tmp);
4904 + pri_message(pri, tmp);
4907 static FUNC_DUMP(dump_facility)
4909 pri_message(pri, "%c Facility (len=%2d, codeset=%d) [ ", prefix, len, Q931_IE_CODESET(full_ie));
4910 - dump_ie_data(ie->data, ie->len);
4911 - pri_message(NULL, " ]\n");
4912 + dump_ie_data(pri, ie->data, ie->len);
4913 + pri_message(pri, " ]\n");
4916 static FUNC_DUMP(dump_network_spec_fac)
4917 @@ -1465,7 +1988,7 @@
4918 pri_message(pri, code2str(ie->data[1], facilities, sizeof(facilities) / sizeof(facilities[0])));
4921 - dump_ie_data(ie->data, ie->len);
4922 + dump_ie_data(pri, ie->data, ie->len);
4923 pri_message(pri, " ]\n");
4926 @@ -1932,8 +2455,8 @@
4927 { 1, Q931_REDIRECTING_SUBADDR, "Redirecting Subaddress", dump_redirecting_subaddr },
4928 { 0, Q931_TRANSIT_NET_SELECT, "Transit Network Selection" },
4929 { 1, Q931_RESTART_INDICATOR, "Restart Indicator", dump_restart_indicator, receive_restart_indicator, transmit_restart_indicator },
4930 - { 0, Q931_LOW_LAYER_COMPAT, "Low-layer Compatibility" },
4931 - { 0, Q931_HIGH_LAYER_COMPAT, "High-layer Compatibility" },
4932 + { 0, Q931_LOW_LAYER_COMPAT, "Low-layer Compatibility" , dump_low_layer_compat, receive_low_layer_compat, transmit_low_layer_compat },
4933 + { 1, Q931_HIGH_LAYER_COMPAT, "High-layer Compatibility" , dump_high_layer_compat, receive_high_layer_compat, transmit_high_layer_compat },
4934 { 1, Q931_PACKET_SIZE, "Packet Size" },
4935 { 1, Q931_IE_FACILITY, "Facility" , dump_facility, receive_facility, transmit_facility },
4936 { 1, Q931_IE_REDIRECTION_NUMBER, "Redirection Number" },
4937 @@ -1942,11 +2465,11 @@
4938 { 1, Q931_IE_INFO_REQUEST, "Feature Request" },
4939 { 1, Q931_IE_FEATURE_IND, "Feature Indication" },
4940 { 1, Q931_IE_SEGMENTED_MSG, "Segmented Message" },
4941 - { 1, Q931_IE_CALL_IDENTITY, "Call Identity", dump_call_identity },
4942 + { 1, Q931_IE_CALL_IDENTITY, "Call Identity", dump_call_identity, receive_call_identity, transmit_call_identity },
4943 { 1, Q931_IE_ENDPOINT_ID, "Endpoint Identification" },
4944 { 1, Q931_IE_NOTIFY_IND, "Notification Indicator", dump_notify, receive_notify, transmit_notify },
4945 { 1, Q931_DISPLAY, "Display", dump_display, receive_display, transmit_display },
4946 - { 1, Q931_IE_TIME_DATE, "Date/Time", dump_time_date },
4947 + { 1, Q931_IE_TIME_DATE, "Date/Time", dump_time_date, receive_time_date, transmit_time_date },
4948 { 1, Q931_IE_KEYPAD_FACILITY, "Keypad Facility", dump_keypad_facility, receive_keypad_facility },
4949 { 0, Q931_IE_SIGNAL, "Signal", dump_signal },
4950 { 1, Q931_IE_SWITCHHOOK, "Switch-hook" },
4951 @@ -1954,6 +2477,7 @@
4952 { 1, Q931_IE_ESCAPE_FOR_EXT, "Escape for Extension" },
4953 { 1, Q931_IE_CALL_STATUS, "Call Status" },
4954 { 1, Q931_IE_CHANGE_STATUS, "Change Status" },
4955 + { 1, Q931_COLP, "Connect Line ID Presentation", dump_colp},
4956 { 1, Q931_IE_CONNECTED_ADDR, "Connected Number", dump_connected_number },
4957 { 1, Q931_IE_CONNECTED_NUM, "Connected Number", dump_connected_number },
4958 { 1, Q931_IE_ORIGINAL_CALLED_NUMBER, "Original Called Number", dump_redirecting_number, receive_redirecting_number, transmit_redirecting_number },
4959 @@ -2024,7 +2548,7 @@
4961 if ((ie->ie & 0x80) != 0)
4968 @@ -2054,10 +2578,10 @@
4973 + /* if (cr & 0x80) {
4980 pri_error(NULL, "Call Reference Length not supported: %d\n", h->crlen);
4981 @@ -2071,14 +2595,14 @@
4982 int full_ie = Q931_FULL_IE(codeset, ie->ie);
4985 - pri_message(NULL, "%c [", prefix);
4986 - pri_message(NULL, "%02x", ie->ie);
4987 + pri_message(pri, "%c [", prefix);
4988 + pri_message(pri, "%02x", ie->ie);
4989 if (!(ie->ie & 0x80)) {
4990 - pri_message(NULL, " %02x", ielen(ie)-2);
4991 + pri_message(pri, " %02x", ielen(ie)-2);
4992 for (x = 0; x + 2 < ielen(ie); ++x)
4993 - pri_message(NULL, " %02x", ie->data[x]);
4994 + pri_message(pri, " %02x", ie->data[x]);
4996 - pri_message(NULL, "]\n");
4997 + pri_message(pri, "]\n");
4999 /* Special treatment for shifts */
5000 if((full_ie & 0xf0) == Q931_LOCKING_SHIFT)
5001 @@ -2098,14 +2622,46 @@
5002 pri_error(pri, "!! %c Unknown IE %d (len = %d)\n", prefix, base_ie, ielen(ie));
5005 -static q931_call *q931_getcall(struct pri *pri, int cr)
5006 +static q921_call *q921_getcall(struct pri *pri, struct q931_call *c, int tei)
5011 + if (cur->tei == tei) {
5016 + /* No call exists, make a new one */
5017 + if (pri->debug & PRI_DEBUG_Q921_STATE)
5018 + pri_message(pri, "-- Making new q921 call for cref %d tei %d\n", c->cr, tei);
5019 + cur = malloc(sizeof(struct q921_call));
5020 + memset(cur, 0, sizeof(cur));
5023 + cur->channel = -1;
5024 + cur->next = c->phones;
5029 +static q931_call *q931_getcall(struct pri *pri, int cr, int tei)
5031 q931_call *cur, *prev;
5032 cur = *pri->callpool;
5035 - if (cur->cr == cr)
5037 + if ((pri->localtype == BRI_NETWORK_PTMP) && (tei >= 0)) {
5038 + // hmm...ok, we might be the 1st responding to the setup
5039 + // or it is really our call
5040 + if ((cur->cr == cr) && ((cur->tei == tei) || (cur->tei == 127)))
5042 + // or we might not be the 1st responding, then we need to clone
5043 + // the call struct to hangup properly
5045 + if (cur->cr == cr)
5051 @@ -2118,6 +2674,7 @@
5052 /* Call reference */
5056 /* Append to end of list */
5059 @@ -2133,24 +2690,42 @@
5061 cur = *pri->callpool;
5063 - if (pri->cref > 32767)
5065 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
5066 + if (pri->cref > 32767)
5070 + if (pri->cref > 255)
5074 - if (cur->cr == (0x8000 | pri->cref))
5076 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
5077 + if (cur->cr == (0x8000 | pri->cref))
5080 + // BRIs have only 1 bye cref
5081 + if (cur->cr == (0x80 | pri->cref))
5087 - return q931_getcall(pri, pri->cref | 0x8000);
5088 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
5089 + return q931_getcall(pri, pri->cref | 0x8000, 0);
5092 + return q931_getcall(pri, pri->cref | 0x80, 0);
5096 -static void q931_destroy(struct pri *pri, int cr, q931_call *c)
5097 +static void q931_destroy(struct pri *pri, int cr, q931_call *c, int tei)
5099 q931_call *cur, *prev;
5101 cur = *pri->callpool;
5103 - if ((c && (cur == c)) || (!c && (cur->cr == cr))) {
5104 +// if ((c && (cur == c)) || (!c && (cur->cr == cr))) {
5105 + if ((c && (cur == c)) || (!c && ((cur->cr == cr) && ((pri->localtype != BRI_NETWORK_PTMP) || (cur->tei == tei))))) {
5107 prev->next = cur->next;
5109 @@ -2159,6 +2734,8 @@
5110 pri_message(pri, "NEW_HANGUP DEBUG: Destroying the call, ourstate %s, peerstate %s\n",callstate2str(cur->ourcallstate),callstate2str(cur->peercallstate));
5111 if (cur->retranstimer)
5112 pri_schedule_del(pri, cur->retranstimer);
5113 + if (cur->t303timer)
5114 + pri_schedule_del(pri, cur->t303timer);
5115 pri_call_apdu_queue_cleanup(cur);
5118 @@ -2169,16 +2746,16 @@
5119 pri_error(pri, "Can't destroy call %d!\n", cr);
5122 -static void q931_destroycall(struct pri *pri, int cr)
5123 +static void q931_destroycall(struct pri *pri, int cr, int tei)
5125 - return q931_destroy(pri, cr, NULL);
5126 + return q931_destroy(pri, cr, NULL, tei);
5130 void __q931_destroycall(struct pri *pri, q931_call *c)
5133 - q931_destroy(pri,0, c);
5134 + q931_destroy(pri,0, c, c->tei);
5138 @@ -2290,6 +2867,10 @@
5141 int full_ie = Q931_FULL_IE(codeset, ie->ie);
5142 + if (ielen(ie) > Q931_IE_MAX_LEN) {
5143 + pri_error(pri, "!! Invalid IE length %d (len = %d)\n", full_ie, ielen(ie));
5146 if (pri->debug & PRI_DEBUG_Q931_STATE)
5147 pri_message(pri, "-- Processing IE %d (cs%d, %s)\n", ie->ie, codeset, ie2str(full_ie));
5148 for (x=0;x<sizeof(ies) / sizeof(ies[0]);x++) {
5149 @@ -2307,21 +2888,36 @@
5153 -static void init_header(struct pri *pri, q931_call *call, unsigned char *buf, q931_h **hb, q931_mh **mhb, int *len)
5154 +static void init_header(struct pri *pri, q931_call *call, unsigned char *buf, q931_h **hb, q931_mh **mhb, int *len, int briflag)
5156 /* Returns header and message header and modifies length in place */
5157 q931_h *h = (q931_h *)buf;
5158 - q931_mh * mh = (q931_mh *)(h->contents + 2);
5160 h->pd = pri->protodisc;
5161 h->x0 = 0; /* Reserved 0 */
5162 - h->crlen = 2; /* Two bytes of Call Reference. Invert the top bit to make it from our sense */
5163 - if (call->cr || call->forceinvert) {
5165 + if (briflag == 1) {
5166 + mh = (q931_mh *)(h->contents + 1);
5167 + h->crlen = 1; /* One bytes of Call Reference. Invert the top bit to make it from our sense */
5168 + if (call->cr || call->forceinvert) {
5169 + h->crv[0] = (call->cr ^ 0x80);
5171 + /* Unless of course this has no call reference */
5176 + mh = (q931_mh *)(h->contents + 2);
5177 + h->crlen = 2; /* Two bytes of Call Reference. Invert the top bit to make it from our sense */
5178 + if (call->cr || call->forceinvert) {
5179 h->crv[0] = ((call->cr ^ 0x8000) & 0xff00) >> 8;
5180 h->crv[1] = (call->cr & 0xff);
5183 /* Unless of course this has no call reference */
5189 if (pri->subchannel) {
5190 /* On GR-303, top bit is always 0 */
5191 @@ -2330,13 +2926,23 @@
5199 -static int q931_xmit(struct pri *pri, q931_h *h, int len, int cr)
5200 +static int q931_xmit(struct pri *pri, q931_h *h, int len, int cr, int tei)
5202 - q921_transmit_iframe(pri, h, len, cr);
5204 + if (pri->localtype == BRI_NETWORK_PTMP) {
5205 + mh = (q931_mh *)(h->contents + 1);
5206 + if (mh->msg == Q931_SETUP) {
5207 + q921_transmit_uframe(pri, h, len, cr, tei);
5209 + q921_transmit_iframe(pri, h, len, cr, tei);
5211 + } else if (pri->localtype == BRI_CPE_PTMP) {
5212 + q921_transmit_iframe(pri, h, len, cr, pri->tei);
5214 + q921_transmit_iframe(pri, h, len, cr, tei);
5216 /* The transmit operation might dump the q921 header, so logging the q931
5217 message body after the transmit puts the sections of the message in the
5218 right order in the log */
5219 @@ -2362,7 +2968,11 @@
5221 memset(buf, 0, sizeof(buf));
5223 - init_header(pri, c, buf, &h, &mh, &len);
5224 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
5225 + init_header(pri, c, buf, &h, &mh, &len, 0);
5227 + init_header(pri, c, buf, &h, &mh, &len, 1);
5232 @@ -2397,11 +3007,34 @@
5234 /* Invert the logic */
5235 len = sizeof(buf) - len;
5236 - q931_xmit(pri, h, len, 1);
5237 + if (pri->localtype == BRI_CPE_PTMP) {
5238 + q931_xmit(pri, h, len, 1, pri->tei);
5240 + q931_xmit(pri, h, len, 1, c->tei);
5246 +static int facility_ies[] = { Q931_IE_FACILITY, -1 };
5249 +int q931_facility_kpj(struct pri *pri, q931_call *c, int operation, char *arguments)
5251 + switch (operation) {
5252 + case 0x26: c->aoc = 1;
5254 + case 0x24: c->aoc = 1;
5256 + case 0x22: c->aoc = 1;
5261 + return send_message(pri, c, Q931_FACILITY, facility_ies);
5265 static int status_ies[] = { Q931_CAUSE, Q931_CALL_STATE, -1 };
5267 static int q931_status(struct pri *pri, q931_call *c, int cause)
5268 @@ -2439,17 +3072,45 @@
5269 return send_message(pri, c, Q931_INFORMATION, information_ies);
5272 +static int information_display_ies[] = { Q931_DISPLAY, -1 };
5274 +int q931_information_display(struct pri *pri, q931_call *c, char *display)
5278 + if (!display) return -1;
5279 + strncpy(temp, c->callername, sizeof(temp));
5280 + strncpy(c->callername, display, sizeof(c->callername));
5281 + res = send_message(pri, c, Q931_INFORMATION, information_display_ies);
5282 + strncpy(c->callername, temp, sizeof(c->callername));
5286 +int q931_add_display(struct pri *pri, q931_call *c, char *display)
5288 + strncpy(c->display, display, sizeof(c->display));
5292 +/* static int information_special_ies[] = { Q931_IE_SPECIAL, -1 };
5293 +static int q931_information_special(struct pri *pri, q931_call *c)
5295 + return send_message(pri, c, Q931_FACILITY, information_special_ies);
5300 static int restart_ack_ies[] = { Q931_CHANNEL_IDENT, Q931_RESTART_INDICATOR, -1 };
5302 static int restart_ack(struct pri *pri, q931_call *c)
5304 c->ourcallstate = Q931_CALL_STATE_NULL;
5305 c->peercallstate = Q931_CALL_STATE_NULL;
5306 + c->chanflags &= ~FLAG_PREFERRED;
5307 + c->chanflags |= FLAG_EXCLUSIVE;
5308 return send_message(pri, c, Q931_RESTART_ACKNOWLEDGE, restart_ack_ies);
5311 -static int facility_ies[] = { Q931_IE_FACILITY, -1 };
5313 int q931_facility(struct pri*pri, q931_call *c)
5315 return send_message(pri, c, Q931_FACILITY, facility_ies);
5316 @@ -2463,7 +3124,6 @@
5317 if ((info > 0x2) || (info < 0x00))
5322 c->notify = info & 0x7F;
5324 @@ -2506,6 +3166,8 @@
5326 int q931_call_proceeding(struct pri *pri, q931_call *c, int channel, int info)
5328 + // never send two PROCEEDINGs!
5329 + if (c->proc > 0) return 0;
5331 c->ds1no = (channel & 0xff00) >> 8;
5332 c->ds1explicit = (channel & 0x10000) >> 16;
5333 @@ -2532,8 +3194,12 @@
5334 static int alerting_ies[] = { -1 };
5337 +static int alerting_BRI_ies[] = { -1 };
5339 int q931_alerting(struct pri *pri, q931_call *c, int channel, int info)
5341 + // never send two ALERTINGs!
5342 + if (c->alert > 0) return 0;
5344 q931_call_proceeding(pri, c, channel, 0);
5346 @@ -2544,14 +3210,130 @@
5347 c->progressmask = 0;
5348 c->ourcallstate = Q931_CALL_STATE_CALL_RECEIVED;
5349 c->peercallstate = Q931_CALL_STATE_CALL_DELIVERED;
5352 - return send_message(pri, c, Q931_ALERTING, alerting_ies);
5353 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
5354 + return send_message(pri, c, Q931_ALERTING, alerting_ies);
5356 + if ((pri->localtype == BRI_NETWORK) || (pri->localtype == BRI_NETWORK_PTMP)) {
5357 + return send_message(pri, c, Q931_ALERTING, alerting_ies);
5359 + /* no PROGRESS_INDICATOR for BRI please */
5360 + return send_message(pri, c, Q931_ALERTING, alerting_BRI_ies);
5365 +static int hold_acknowledge_ies[] = { -1 };
5367 +int q931_hold_acknowledge(struct pri *pri, q931_call *c)
5369 + return send_message(pri, c, Q931_HOLD_ACKNOWLEDGE, hold_acknowledge_ies);
5372 +static int hold_reject_ies[] = { Q931_CAUSE, -1 };
5374 +int q931_hold_reject(struct pri *pri, q931_call *c)
5377 + c->causecode = CODE_CCITT;
5378 + c->causeloc = LOC_PRIV_NET_LOCAL_USER;
5379 + return send_message(pri, c, Q931_HOLD_REJECT, hold_reject_ies);
5382 +static int retrieve_acknowledge_ies[] = { Q931_CHANNEL_IDENT, -1 };
5384 +int q931_retrieve_acknowledge(struct pri *pri, q931_call *c, int channel)
5387 + c->channelno = channel;
5388 + c->chanflags &= ~FLAG_PREFERRED;
5389 + c->chanflags |= FLAG_EXCLUSIVE;
5390 + return send_message(pri, c, Q931_RETRIEVE_ACKNOWLEDGE, retrieve_acknowledge_ies);
5393 +static int retrieve_reject_ies[] = { -1 };
5395 +int q931_retrieve_reject(struct pri *pri, q931_call *c)
5397 + return send_message(pri, c, Q931_RETRIEVE_REJECT, retrieve_reject_ies);
5400 -static int connect_ies[] = { Q931_CHANNEL_IDENT, Q931_PROGRESS_INDICATOR, -1 };
5401 +static int suspend_acknowledge_ies[] = { Q931_DISPLAY, -1 };
5403 +int q931_suspend_acknowledge(struct pri *pri, q931_call *c, char *display)
5405 + char tempcallername[256];
5407 + c->ourcallstate = Q931_CALL_STATE_NULL;
5408 + c->peercallstate = Q931_CALL_STATE_NULL;
5409 + strncpy(tempcallername,c->callername,sizeof(tempcallername));
5410 + strncpy(c->callername,display,sizeof(c->callername));
5411 + res = send_message(pri, c, Q931_SUSPEND_ACKNOWLEDGE, suspend_acknowledge_ies);
5412 + strncpy(c->callername,tempcallername,sizeof(c->callername));
5413 + __q931_destroycall(pri, c);
5417 +static int suspend_reject_ies[] = { Q931_DISPLAY, Q931_CAUSE, -1 };
5419 +int q931_suspend_reject(struct pri *pri, q931_call *c, char *display)
5421 + char tempcallername[256];
5423 + strncpy(tempcallername,c->callername,sizeof(tempcallername));
5424 + strncpy(c->callername,display,sizeof(c->callername));
5426 + c->causecode = CODE_CCITT;
5427 + c->causeloc = LOC_PRIV_NET_LOCAL_USER;
5428 + res = send_message(pri, c, Q931_SUSPEND_REJECT, suspend_reject_ies);
5429 + strncpy(c->callername,tempcallername,sizeof(c->callername));
5433 +static int resume_reject_ies[] = { Q931_CAUSE, Q931_DISPLAY, -1 };
5435 +int q931_resume_reject(struct pri *pri, q931_call *c, char *display)
5437 + char tempcallername[256];
5440 + c->causecode = CODE_CCITT;
5441 + c->causeloc = LOC_PRIV_NET_LOCAL_USER;
5442 + strncpy(tempcallername,c->callername,sizeof(tempcallername));
5443 + strncpy(c->callername,display,sizeof(c->callername));
5444 + res = send_message(pri, c, Q931_RESUME_REJECT, resume_reject_ies);
5445 + strncpy(c->callername,tempcallername,sizeof(c->callername));
5449 +static int resume_acknowledge_ies[] = { Q931_CHANNEL_IDENT, Q931_DISPLAY, -1 };
5451 +int q931_resume_acknowledge(struct pri *pri, q931_call *c, int channel, char *display)
5453 + char tempcallername[256];
5456 + c->channelno = channel;
5457 + c->chanflags &= ~FLAG_PREFERRED;
5458 + c->chanflags |= FLAG_EXCLUSIVE;
5460 + c->ourcallstate = Q931_CALL_STATE_ACTIVE;
5461 + c->peercallstate = Q931_CALL_STATE_ACTIVE;
5462 + strncpy(tempcallername,c->callername,sizeof(tempcallername));
5463 + strncpy(c->callername,display,sizeof(c->callername));
5464 + res = send_message(pri, c, Q931_RESUME_ACKNOWLEDGE, resume_acknowledge_ies);
5465 + strncpy(c->callername,tempcallername,sizeof(c->callername));
5470 +static int connect_ies[] = { Q931_CHANNEL_IDENT, Q931_PROGRESS_INDICATOR, Q931_DISPLAY, -1 };
5471 +static int connect_NET_ies[] = { Q931_CHANNEL_IDENT, Q931_PROGRESS_INDICATOR, Q931_IE_TIME_DATE, -1 };
5473 int q931_setup_ack(struct pri *pri, q931_call *c, int channel, int nonisdn)
5475 + int network = pri->localtype == PRI_NETWORK || pri->localtype == BRI_NETWORK || pri->localtype == BRI_NETWORK_PTMP;
5477 c->ds1no = (channel & 0xff00) >> 8;
5478 c->ds1explicit = (channel & 0x10000) >> 16;
5479 @@ -2569,9 +3351,39 @@
5480 c->ourcallstate = Q931_CALL_STATE_OVERLAP_RECEIVING;
5481 c->peercallstate = Q931_CALL_STATE_OVERLAP_SENDING;
5484 + c->progloc = LOC_PRIV_NET_LOCAL_USER;
5485 + c->progcode = CODE_CCITT;
5486 + c->progressmask = Q931_PROG_INBAND_AVAILABLE;
5488 return send_message(pri, c, Q931_SETUP_ACKNOWLEDGE, connect_ies);
5491 +static void pri_setup_response_timeout(void *data)
5493 + struct q931_call *c = data;
5494 + struct pri *pri = NULL;
5499 + c->cause = PRI_CAUSE_NO_USER_RESPONSE;
5500 + if (pri->debug & PRI_DEBUG_Q931_STATE)
5501 + pri_message(pri, "No response to SETUP message\n");
5503 + pri->ev.e = PRI_EVENT_HANGUP;
5504 + pri->ev.hangup.channel = c->channelno;
5505 + pri->ev.hangup.cref = c->cr;
5506 + pri->ev.hangup.aoc_units = -1;
5507 + if (c->cause == -1) {
5508 + pri->ev.hangup.cause = PRI_CAUSE_SWITCH_CONGESTION;
5510 + pri->ev.hangup.cause = c->cause;
5512 + pri->ev.hangup.call = c;
5513 + q931_hangup(pri, c, c->cause);
5516 static void pri_connect_timeout(void *data)
5518 struct q931_call *c = data;
5519 @@ -2624,6 +3436,7 @@
5521 int q931_connect(struct pri *pri, q931_call *c, int channel, int nonisdn)
5523 + int network = pri->localtype == PRI_NETWORK || pri->localtype == BRI_NETWORK || pri->localtype == BRI_NETWORK_PTMP;
5525 c->ds1no = (channel & 0xff00) >> 8;
5526 c->ds1explicit = (channel & 0x10000) >> 16;
5527 @@ -2638,22 +3451,37 @@
5528 c->progressmask = PRI_PROG_CALLED_NOT_ISDN;
5530 c->progressmask = 0;
5531 - c->ourcallstate = Q931_CALL_STATE_CONNECT_REQUEST;
5533 + /* WE decide when the call is up and active */
5534 + c->ourcallstate = Q931_CALL_STATE_ACTIVE;
5536 + c->ourcallstate = Q931_CALL_STATE_CONNECT_REQUEST;
5538 c->peercallstate = Q931_CALL_STATE_ACTIVE;
5542 if (c->retranstimer)
5543 pri_schedule_del(pri, c->retranstimer);
5544 c->retranstimer = 0;
5545 - if ((pri->localtype == PRI_CPE) && (!pri->subchannel))
5546 + if (!network && (!pri->subchannel)) {
5547 + /* make sure that CPEs get a CONNECT_ACKNOWLEDGE */
5548 c->retranstimer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T313], pri_connect_timeout, c);
5549 - return send_message(pri, c, Q931_CONNECT, connect_ies);
5551 + if ((pri->localtype != PRI_CPE) && (pri->localtype != BRI_CPE) && (pri->localtype != BRI_CPE_PTMP)) {
5552 + // networks may send datetime
5553 + return send_message(pri, c, Q931_CONNECT, connect_NET_ies);
5555 + return send_message(pri, c, Q931_CONNECT, connect_ies);
5559 +static int release_aoce_ies[] = { Q931_CAUSE, Q931_IE_USER_USER, Q931_IE_FACILITY, -1 };
5560 static int release_ies[] = { Q931_CAUSE, Q931_IE_USER_USER, -1 };
5562 int q931_release(struct pri *pri, q931_call *c, int cause)
5564 + int network = pri->localtype == PRI_NETWORK || pri->localtype == BRI_NETWORK || pri->localtype == BRI_NETWORK_PTMP;
5565 c->ourcallstate = Q931_CALL_STATE_RELEASE_REQUEST;
5566 /* c->peercallstate stays the same */
5568 @@ -2669,7 +3497,14 @@
5570 c->retranstimer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T308], pri_release_finaltimeout, c);
5572 - return send_message(pri, c, Q931_RELEASE, release_ies);
5573 + if (network && (c->aoc_units > -1)) {
5574 + /* include FACILITY IE for AOC-E */
5575 + aoc_aoce_charging_unit_encode(pri, c , c->aoc_units, Q931_RELEASE);
5576 + c->aoc_units = -1;
5577 + return send_message(pri, c, Q931_RELEASE, release_aoce_ies);
5579 + return send_message(pri, c, Q931_RELEASE, release_ies);
5582 return send_message(pri, c, Q931_RELEASE_COMPLETE, release_ies); /* Yes, release_ies, not release_complete_ies */
5584 @@ -2681,7 +3516,7 @@
5585 int q931_restart(struct pri *pri, int channel)
5587 struct q931_call *c;
5588 - c = q931_getcall(pri, 0 | 0x8000);
5589 + c = q931_getcall(pri, 0 | 0x8000, 0);
5593 @@ -2698,10 +3533,12 @@
5594 return send_message(pri, c, Q931_RESTART, restart_ies);
5597 +static int disconnect_aoce_ies[] = { Q931_CAUSE, Q931_IE_USER_USER, Q931_IE_FACILITY, -1 };
5598 static int disconnect_ies[] = { Q931_CAUSE, Q931_IE_USER_USER, -1 };
5600 int q931_disconnect(struct pri *pri, q931_call *c, int cause)
5602 + int network = pri->localtype == PRI_NETWORK || pri->localtype == BRI_NETWORK || pri->localtype == BRI_NETWORK_PTMP;
5603 c->ourcallstate = Q931_CALL_STATE_DISCONNECT_REQUEST;
5604 c->peercallstate = Q931_CALL_STATE_DISCONNECT_INDICATION;
5606 @@ -2713,14 +3550,27 @@
5607 if (c->retranstimer)
5608 pri_schedule_del(pri, c->retranstimer);
5609 c->retranstimer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T305], pri_disconnect_timeout, c);
5610 - return send_message(pri, c, Q931_DISCONNECT, disconnect_ies);
5611 + if (network && (c->aoc_units > -1)) {
5612 + /* include FACILITY IE for AOC-E */
5613 + aoc_aoce_charging_unit_encode(pri, c , c->aoc_units, Q931_DISCONNECT);
5614 + c->aoc_units = -1;
5615 + return send_message(pri, c, Q931_DISCONNECT, disconnect_aoce_ies);
5617 + return send_message(pri, c, Q931_DISCONNECT, disconnect_ies);
5623 +//static int setup_ies[] = { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT, Q931_DISPLAY, Q931_PROGRESS_INDICATOR,
5624 +// Q931_IE_SIGNAL, Q931_CALLING_PARTY_NUMBER, Q931_CALLED_PARTY_NUMBER, Q931_SENDING_COMPLETE, -1 };
5626 static int setup_ies[] = { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT, Q931_IE_FACILITY, Q931_PROGRESS_INDICATOR, Q931_NETWORK_SPEC_FAC, Q931_DISPLAY, Q931_IE_USER_USER,
5627 Q931_CALLING_PARTY_NUMBER, Q931_CALLED_PARTY_NUMBER, Q931_REDIRECTING_NUMBER, Q931_SENDING_COMPLETE, Q931_IE_ORIGINATING_LINE_INFO, Q931_IE_GENERIC_DIGITS, -1 };
5629 +static int setup_bri_ies[] = { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT, Q931_IE_FACILITY, Q931_PROGRESS_INDICATOR, Q931_NETWORK_SPEC_FAC, Q931_DISPLAY, Q931_IE_USER_USER,
5630 + Q931_CALLING_PARTY_NUMBER, Q931_CALLED_PARTY_NUMBER, Q931_REDIRECTING_NUMBER, Q931_SENDING_COMPLETE, Q931_IE_ORIGINATING_LINE_INFO, Q931_IE_GENERIC_DIGITS, Q931_HIGH_LAYER_COMPAT, Q931_LOW_LAYER_COMPAT, -1 };
5632 static int gr303_setup_ies[] = { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT, -1 };
5634 static int cis_setup_ies[] = { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT, Q931_IE_FACILITY, Q931_CALLED_PARTY_NUMBER, -1 };
5635 @@ -2728,7 +3578,12 @@
5636 int q931_setup(struct pri *pri, q931_call *c, struct pri_sr *req)
5641 + if (pri->localtype == BRI_NETWORK_PTMP) {
5647 c->transcapability = req->transmode;
5648 c->transmoderate = TRANS_MODE_64_CIRCUIT;
5649 @@ -2753,6 +3608,7 @@
5650 c->chanflags = FLAG_EXCLUSIVE;
5651 else if (c->channelno)
5652 c->chanflags = FLAG_PREFERRED;
5653 + memcpy(c->llc, req->llc, sizeof(c->llc));
5655 libpri_copy_string(c->callernum, req->caller, sizeof(c->callernum));
5656 c->callerplan = req->callerplan;
5657 @@ -2812,14 +3668,19 @@
5658 res = send_message(pri, c, Q931_SETUP, gr303_setup_ies);
5659 else if (c->justsignalling)
5660 res = send_message(pri, c, Q931_SETUP, cis_setup_ies);
5661 + else if (pri->localtype == BRI_NETWORK || pri->localtype == BRI_NETWORK_PTMP)
5662 + res = send_message(pri, c, Q931_SETUP, setup_bri_ies);
5664 res = send_message(pri, c, Q931_SETUP, setup_ies);
5668 /* make sure we call PRI_EVENT_HANGUP_ACK once we send/receive RELEASE_COMPLETE */
5669 c->sendhangupack = 1;
5670 c->ourcallstate = Q931_CALL_STATE_CALL_INITIATED;
5671 c->peercallstate = Q931_CALL_STATE_OVERLAP_SENDING;
5672 + c->t303timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T303], pri_setup_response_timeout, c);
5673 + c->t303running = 1;
5677 @@ -2835,7 +3696,11 @@
5680 c->causecode = CODE_CCITT;
5681 - c->causeloc = LOC_PRIV_NET_LOCAL_USER;
5682 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == BRI_NETWORK) || (pri->localtype == BRI_NETWORK_PTMP)) {
5683 + c->causeloc = LOC_PRIV_NET_LOCAL_USER;
5685 + c->causeloc = LOC_USER;
5687 /* release_ies has CAUSE in it */
5688 res = send_message(pri, c, Q931_RELEASE_COMPLETE, release_ies);
5690 @@ -2860,6 +3725,125 @@
5694 +/* here we cleanly hangup the phones that responded to our call but didnt get the call */
5695 +int q921_hangup(struct pri *pri, q931_call *c, int tei)
5697 + q921_call *cur,*prev;
5705 + if (pri->localtype != BRI_NETWORK_PTMP){
5708 +// pri_error(pri, "q921_hangup(%d, %d)\n", c->cr, tei);
5713 +// pri_error(pri, "tei %d\n", tei);
5720 + if (cur->tei != tei) {
5721 + c->cause = PRI_CAUSE_NORMAL_CLEARING;
5722 + c->tei = cur->tei;
5723 + if (pri->debug & PRI_DEBUG_Q921_STATE)
5724 + pri_message(pri, "sending RELEASE for TEI %d\n", cur->tei);
5725 + send_message(pri, c, Q931_RELEASE, release_ies);
5738 + if (c->tei == 127) {
5739 + q931_destroycall(pri, c->cr, c->tei);
5740 + // make sure * frees the channel
5741 +/* pri_error(pri, "returning PRI_EVENT_HANGUP_ACK\n");
5742 + res = Q931_RES_HAVEEVENT;
5743 + pri->ev.hangup.channel = c->channelno;
5744 + pri->ev.e = PRI_EVENT_HANGUP_ACK; */
5749 +/* here we handle release_completes from the phones
5750 + because some (elmeg) phones do not send a disconnect
5751 + message when the phone is busy */
5752 +int q921_handle_hangup(struct pri *pri, q931_call *c, int tei)
5754 + q921_call *cur,*match,*prev=NULL;
5761 + if (pri->localtype != BRI_NETWORK_PTMP){
5768 + if (cur->tei == tei) {
5771 + prev->next = cur->next;
5774 + c->phones = cur->next;
5779 + if (cur) cur = cur->next;
5789 + // if all phones have signalled busy AND the timer is not running anymore!
5790 +#ifdef FASTBUSYONBUSY
5791 + if ((c->cause == PRI_CAUSE_USER_BUSY) && (c->t303timer)) {
5792 + c->t303running = 0;
5793 + pri_schedule_del(pri, c->t303timer);
5797 + if ((left==0) && (c->cause == PRI_CAUSE_USER_BUSY) && (c->t303running == 0)) {
5798 + // pri_error(pri, "q921_handle_hangup(%d, %d, %d)\n", c->cr, tei, c->tei);
5799 + // make sure * frees the channel
5800 + res = Q931_RES_HAVEEVENT;
5801 + pri->ev.hangup.cause = PRI_CAUSE_USER_BUSY;
5802 + pri->ev.hangup.channel = c->channelno | (c->ds1no << 8);
5803 + pri->ev.hangup.cref = c->cr;
5804 + pri->ev.hangup.call = c;
5805 + pri->ev.hangup.aoc_units = 0;
5806 + pri->ev.e = PRI_EVENT_HANGUP;
5813 int q931_hangup(struct pri *pri, q931_call *c, int cause)
5816 @@ -2871,7 +3855,7 @@
5817 /* If mandatory IE was missing, insist upon that cause code */
5818 if (c->cause == PRI_CAUSE_MANDATORY_IE_MISSING)
5820 - if (cause == 34 || cause == 44 || cause == 82 || cause == 1 || cause == 81) {
5821 + if (cause == 34 || cause == 44 || cause == 82 || cause == 1 || cause == 81 || cause == 17) {
5822 /* We'll send RELEASE_COMPLETE with these causes */
5825 @@ -2885,7 +3869,7 @@
5826 case Q931_CALL_STATE_NULL:
5827 if (c->peercallstate == Q931_CALL_STATE_NULL)
5828 /* free the resources if we receive or send REL_COMPL */
5829 - q931_destroycall(pri, c->cr);
5830 + q931_destroycall(pri, c->cr, c->tei);
5831 else if (c->peercallstate == Q931_CALL_STATE_RELEASE_REQUEST)
5832 q931_release_complete(pri,c,cause);
5834 @@ -2911,6 +3895,11 @@
5835 /* received SETUP_ACKNOWLEDGE */
5836 /* send DISCONNECT in general */
5837 if (c->peercallstate != Q931_CALL_STATE_NULL && c->peercallstate != Q931_CALL_STATE_DISCONNECT_REQUEST && c->peercallstate != Q931_CALL_STATE_DISCONNECT_INDICATION && c->peercallstate != Q931_CALL_STATE_RELEASE_REQUEST && c->peercallstate != Q931_CALL_STATE_RESTART_REQUEST && c->peercallstate != Q931_CALL_STATE_RESTART) {
5838 + if (pri->localtype == BRI_NETWORK_PTMP) {
5839 + if (c->tei == 127) {
5844 q931_disconnect(pri,c,cause);
5845 else if (release_compl)
5846 @@ -2926,8 +3915,14 @@
5848 case Q931_CALL_STATE_DISCONNECT_INDICATION:
5849 /* received DISCONNECT */
5850 + if (pri->localtype == BRI_NETWORK_PTMP) {
5851 + if (c->tei == 127) {
5855 if (c->peercallstate == Q931_CALL_STATE_DISCONNECT_REQUEST) {
5857 +// pri_error(pri, "sending release to %d\n", c->tei);
5858 q931_release(pri,c,cause);
5861 @@ -2941,19 +3936,17 @@
5862 pri_error(pri, "q931_hangup shouldn't be called in this state, ourstate %s, peerstate %s\n",callstate2str(c->ourcallstate),callstate2str(c->peercallstate));
5865 - pri_error(pri, "We're not yet handling hanging up when our state is %d, contact support@digium.com, ourstate %s, peerstate %s\n",
5867 - callstate2str(c->ourcallstate),
5868 - callstate2str(c->peercallstate));
5869 + pri_error(pri, "We're not yet handling hanging up when our state is %d, contact support@digium.com, ourstate %s, peerstate %s\n",callstate2str(c->ourcallstate),callstate2str(c->peercallstate));
5872 /* we did handle hangup properly at this point */
5876 -int q931_receive(struct pri *pri, q931_h *h, int len)
5877 +int q931_receive(struct pri *pri, q931_h *h, int len, int tei)
5884 @@ -2965,6 +3958,7 @@
5885 int codeset, cur_codeset;
5887 struct apdu_event *cur = NULL;
5888 + int network = pri->localtype == PRI_NETWORK || pri->localtype == BRI_NETWORK || pri->localtype == BRI_NETWORK_PTMP;
5890 memset(last_ie, 0, sizeof(last_ie));
5891 if (pri->debug & PRI_DEBUG_Q931_DUMP)
5892 @@ -2978,13 +3972,13 @@
5893 KLUDGE this by changing byte 4 from a 0xf (SERVICE)
5894 to a 0x7 (SERVICE ACKNOWLEDGE) */
5895 h->raw[h->crlen + 2] -= 0x8;
5896 - q931_xmit(pri, h, len, 1);
5897 + q931_xmit(pri, h, len, 1, tei);
5899 } else if (h->pd != pri->protodisc) {
5900 pri_error(pri, "Warning: unknown/inappropriate protocol discriminator received (%02x/%d)\n", h->pd, h->pd);
5903 - c = q931_getcall(pri, q931_cr(h));
5904 + c = q931_getcall(pri, q931_cr(h), tei);
5906 pri_error(pri, "Unable to locate call %d\n", q931_cr(h));
5908 @@ -3007,6 +4001,7 @@
5910 if (pri->debug & PRI_DEBUG_Q931_STATE)
5911 pri_message(pri, "-- Processing Q.931 Call Setup\n");
5916 @@ -3027,28 +4022,44 @@
5917 c->callername[0] = '\0';
5918 c->callerani[0] = '\0';
5919 c->callerplanani = -1;
5920 - c->redirectingplan = -1;
5921 - c->redirectingpres = -1;
5922 - c->redirectingreason = -1;
5923 - c->origcalledplan = -1;
5924 - c->origcalledpres = -1;
5925 - c->origredirectingreason = -1;
5926 + c->redirectingplan = -1;
5927 + c->redirectingpres = -1;
5928 + c->redirectingreason = -1;
5929 + c->origcalledplan = -1;
5930 + c->origcalledpres = -1;
5931 + c->origredirectingreason = -1;
5932 c->redirectingnum[0] = '\0';
5933 c->origcallednum[0] = '\0';
5934 c->redirectingname[0] = '\0';
5935 c->origcalledname[0] = '\0';
5936 - c->useruserprotocoldisc = -1;
5937 + c->useruserprotocoldisc = -1;
5938 c->useruserinfo[0] = '\0';
5942 - /* Fall through */
5943 + c->digits[0] = '\0';
5944 + c->display[0] = '\0';
5946 + c->progressmask = 0;
5949 + if (c->t303timer) {
5950 + c->t303running = 0;
5951 + pri_schedule_del(pri, c->t303timer);
5953 + c->useruserinfo[0] = '\0';
5959 - c->useruserinfo[0] = '\0';
5961 + c->useruserinfo[0] = '\0';
5962 case Q931_CALL_PROCEEDING:
5963 + if (c->t303timer) {
5964 + c->t303running = 0;
5965 + pri_schedule_del(pri, c->t303timer);
5969 c->progressmask = 0;
5971 @@ -3059,20 +4070,22 @@
5974 case Q931_DISCONNECT:
5976 - c->causecode = -1;
5978 - c->aoc_units = -1;
5979 +// c->aoc_units = -1;
5980 + c->useruserinfo[0] = '\0';
5981 if (c->retranstimer)
5982 pri_schedule_del(pri, c->retranstimer);
5983 c->retranstimer = 0;
5984 - c->useruserinfo[0] = '\0';
5985 + if (c->t303timer) {
5986 + c->t303running = 0;
5987 + pri_schedule_del(pri, c->t303timer);
5991 case Q931_RELEASE_COMPLETE:
5992 if (c->retranstimer)
5993 - pri_schedule_del(pri, c->retranstimer);
5994 + pri_schedule_del(pri, c->retranstimer);
5995 c->retranstimer = 0;
5996 - c->useruserinfo[0] = '\0';
5997 + c->useruserinfo[0] = '\0';
6001 @@ -3089,22 +4102,32 @@
6002 case Q931_STATUS_ENQUIRY:
6004 case Q931_SETUP_ACKNOWLEDGE:
6005 + if (c->t303timer) {
6006 + c->t303running = 0;
6007 + pri_schedule_del(pri, c->t303timer);
6015 + case Q931_RETRIEVE:
6020 + case Q931_SUSPEND:
6022 case Q931_USER_INFORMATION:
6024 case Q931_CONGESTION_CONTROL:
6026 case Q931_HOLD_ACKNOWLEDGE:
6027 case Q931_HOLD_REJECT:
6028 - case Q931_RETRIEVE:
6029 case Q931_RETRIEVE_ACKNOWLEDGE:
6030 case Q931_RETRIEVE_REJECT:
6032 case Q931_RESUME_ACKNOWLEDGE:
6033 case Q931_RESUME_REJECT:
6034 - case Q931_SUSPEND:
6035 case Q931_SUSPEND_ACKNOWLEDGE:
6036 case Q931_SUSPEND_REJECT:
6037 pri_error(pri, "!! Not yet handling pre-handle message type %s (%d)\n", msg2str(mh->msg), mh->msg);
6038 @@ -3113,7 +4136,7 @@
6039 pri_error(pri, "!! Don't know how to post-handle message type %s (%d)\n", msg2str(mh->msg), mh->msg);
6040 q931_status(pri,c, PRI_CAUSE_MESSAGE_TYPE_NONEXIST);
6042 - q931_destroycall(pri,c->cr);
6043 + q931_destroycall(pri,c->cr,c->tei);
6046 memset(mandies, 0, sizeof(mandies));
6047 @@ -3193,12 +4216,19 @@
6049 for (x=0;x<MAX_MAND_IES;x++) {
6051 - /* check if there is no channel identification when we're configured as network -> that's not an error */
6052 - if (((pri->localtype != PRI_NETWORK) || (mh->msg != Q931_SETUP) || (mandies[x] != Q931_CHANNEL_IDENT)) &&
6053 - ((mh->msg != Q931_PROGRESS) || (mandies[x] != Q931_PROGRESS_INDICATOR))) {
6054 - pri_error(pri, "XXX Missing handling for mandatory IE %d (cs%d, %s) XXX\n", Q931_IE_IE(mandies[x]), Q931_IE_CODESET(mandies[x]), ie2str(mandies[x]));
6056 + /* check if there is no channel identification when we're configured as network -> that's not an error */
6058 + if (((mh->msg == Q931_SETUP) && (mandies[x] == Q931_CHANNEL_IDENT)) ||
6059 + ((mh->msg == Q931_PROGRESS) && (mandies[x] == Q931_PROGRESS_INDICATOR))) {
6060 + /* according to ets 300 102-1 a progress indicator is mandatory, but so what? ;-) */
6062 + pri_error(pri, "XXX Missing handling for mandatory IE %d (cs%d, %s) XXX\n", Q931_IE_IE(mandies[x]), Q931_IE_CODESET(mandies[x]), ie2str(mandies[x]));
6066 + pri_error(pri, "XXX Missing handling for mandatory IE %d (cs%d, %s) XXX\n", Q931_IE_IE(mandies[x]), Q931_IE_CODESET(mandies[x]), ie2str(mandies[x]));
6072 @@ -3207,7 +4237,7 @@
6075 q931_status(pri, c, PRI_CAUSE_MANDATORY_IE_MISSING);
6076 - q931_destroycall(pri, c->cr);
6077 + q931_destroycall(pri, c->cr, c->tei);
6080 c->ourcallstate = Q931_CALL_STATE_RESTART;
6081 @@ -3225,6 +4255,7 @@
6083 /* Must be new call */
6085 + pri_error(pri, "received SETUP message for call that is not a new call (retransmission). \n");
6088 if (c->progressmask & PRI_PROG_CALLER_NOT_ISDN)
6089 @@ -3242,16 +4273,20 @@
6090 pri->ev.ring.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6091 pri->ev.ring.callingpres = c->callerpres;
6092 pri->ev.ring.callingplan = c->callerplan;
6093 - pri->ev.ring.callingplanani = c->callerplanani;
6094 pri->ev.ring.callingplanrdnis = c->redirectingplan;
6095 pri->ev.ring.callingplanorigcalled = c->origcalledplan;
6096 pri->ev.ring.ani2 = c->ani2;
6097 libpri_copy_string(pri->ev.ring.callingani, c->callerani, sizeof(pri->ev.ring.callingani));
6098 + pri->ev.ring.callingplanani = c->callerplanani;
6099 libpri_copy_string(pri->ev.ring.callingnum, c->callernum, sizeof(pri->ev.ring.callingnum));
6100 libpri_copy_string(pri->ev.ring.callingname, c->callername, sizeof(pri->ev.ring.callingname));
6101 pri->ev.ring.calledplan = c->calledplan;
6102 libpri_copy_string(pri->ev.ring.callingsubaddr, c->callingsubaddr, sizeof(pri->ev.ring.callingsubaddr));
6103 - libpri_copy_string(pri->ev.ring.callednum, c->callednum, sizeof(pri->ev.ring.callednum));
6104 + if (!strlen(c->callednum) && strlen(c->digitbuf)) {
6105 + libpri_copy_string(pri->ev.ring.callednum, c->digitbuf, sizeof(pri->ev.ring.callednum));
6107 + libpri_copy_string(pri->ev.ring.callednum, c->callednum, sizeof(pri->ev.ring.callednum));
6109 libpri_copy_string(pri->ev.ring.origcalledname, c->origcalledname, sizeof(pri->ev.ring.origcalledname));
6110 libpri_copy_string(pri->ev.ring.origcallednum, c->origcallednum, sizeof(pri->ev.ring.origcallednum));
6111 libpri_copy_string(pri->ev.ring.redirectingnum, c->redirectingnum, sizeof(pri->ev.ring.redirectingnum));
6112 @@ -3261,11 +4296,13 @@
6113 pri->ev.ring.redirectingreason = c->redirectingreason;
6114 pri->ev.ring.origredirectingreason = c->origredirectingreason;
6115 pri->ev.ring.flexible = ! (c->chanflags & FLAG_EXCLUSIVE);
6116 - pri->ev.ring.cref = c->cr;
6117 + pri->ev.ring.tei = c->tei;
6118 pri->ev.ring.call = c;
6119 + pri->ev.ring.cref = c->cr;
6120 pri->ev.ring.layer1 = c->userl1;
6121 pri->ev.ring.complete = c->complete;
6122 pri->ev.ring.ctype = c->transcapability;
6123 + memcpy(pri->ev.ring.lowlayercompat, c->llc, sizeof(pri->ev.ring.lowlayercompat));
6124 pri->ev.ring.redirectingreason = c->redirectingreason;
6125 pri->ev.ring.progress = c->progress;
6126 pri->ev.ring.progressmask = c->progressmask;
6127 @@ -3275,6 +4312,9 @@
6128 q931_release_complete(pri,c,PRI_CAUSE_INVALID_CALL_REFERENCE);
6131 + if (pri->localtype == BRI_NETWORK_PTMP) {
6132 + l2c = q921_getcall(pri, c, tei);
6134 c->ourcallstate = Q931_CALL_STATE_CALL_DELIVERED;
6135 c->peercallstate = Q931_CALL_STATE_CALL_RECEIVED;
6136 pri->ev.e = PRI_EVENT_RINGING;
6137 @@ -3295,17 +4335,24 @@
6138 q931_status(pri, c, PRI_CAUSE_WRONG_MESSAGE);
6141 + /* TEI got the call */
6143 c->ourcallstate = Q931_CALL_STATE_ACTIVE;
6144 c->peercallstate = Q931_CALL_STATE_CONNECT_REQUEST;
6145 pri->ev.e = PRI_EVENT_ANSWER;
6146 pri->ev.answer.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6147 pri->ev.answer.cref = c->cr;
6148 pri->ev.answer.call = c;
6149 + pri->ev.answer.tei = c->tei;
6150 pri->ev.answer.progress = c->progress;
6151 pri->ev.answer.progressmask = c->progressmask;
6152 libpri_copy_string(pri->ev.answer.useruserinfo, c->useruserinfo, sizeof(pri->ev.ring.useruserinfo));
6153 c->useruserinfo[0] = '\0';
6154 q931_connect_acknowledge(pri, c);
6155 + if (pri->localtype == BRI_NETWORK_PTMP) {
6156 + /* Release all other TEIs */
6157 + q921_hangup(pri, c, tei);
6159 if (c->justsignalling) { /* Make sure WE release when we initiatie a signalling only connection */
6160 q931_release(pri, c, PRI_CAUSE_NORMAL_CLEARING);
6162 @@ -3313,23 +4360,43 @@
6163 return Q931_RES_HAVEEVENT;
6166 - q931_release_complete(pri,c,PRI_CAUSE_INVALID_CALL_REFERENCE);
6169 - pri->ev.e = PRI_EVENT_FACNAME;
6170 - libpri_copy_string(pri->ev.facname.callingname, c->callername, sizeof(pri->ev.facname.callingname));
6171 - libpri_copy_string(pri->ev.facname.callingnum, c->callernum, sizeof(pri->ev.facname.callingnum));
6172 - pri->ev.facname.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6173 - pri->ev.facname.cref = c->cr;
6174 - pri->ev.facname.call = c;
6176 - pri_message(pri, "Sending facility event (%s/%s)\n", pri->ev.facname.callingname, pri->ev.facname.callingnum);
6178 + if ((pri->localtype == PRI_CPE) || (pri->localtype == PRI_NETWORK)) {
6179 + q931_release_complete(pri,c,PRI_CAUSE_INVALID_CALL_REFERENCE);
6181 + // BRI uses the dummy cref for sservices like ccnr
6185 + if (c->facility > 0) {
6186 + pri->ev.e = PRI_EVENT_FACILITY;
6187 + pri->ev.facility.channel = c->channelno | (c->ds1no << 8);
6188 + pri->ev.facility.cref = c->cr;
6189 + pri->ev.facility.tei = c->tei;
6190 + pri->ev.facility.call = c;
6191 + switch (c->facility) {
6192 + case 0x06: /* ECT execute */
6193 + pri->ev.facility.operation = 0x06;
6195 + case 0x0D: /* CD */
6196 + pri->ev.facility.operation = 0x0D;
6197 + libpri_copy_string(pri->ev.facility.forwardnum, c->redirectingnum, sizeof(pri->ev.facility.forwardnum));
6200 + pri->ev.facility.operation = c->facility;
6203 + pri->ev.e = PRI_EVENT_FACNAME;
6204 + libpri_copy_string(pri->ev.facname.callingname, c->callername, sizeof(pri->ev.facname.callingname));
6205 + libpri_copy_string(pri->ev.facname.callingnum, c->callernum, sizeof(pri->ev.facname.callingname));
6206 + pri->ev.facname.channel = c->channelno | (c->ds1no << 8);
6207 + pri->ev.facname.cref = c->cr;
6208 + pri->ev.facname.call = c;
6210 return Q931_RES_HAVEEVENT;
6213 q931_status(pri, c, PRI_CAUSE_MANDATORY_IE_MISSING);
6214 - q931_destroycall(pri, c->cr);
6215 + q931_destroycall(pri, c->cr, c->tei);
6218 pri->ev.e = PRI_EVENT_PROGRESS;
6219 @@ -3347,6 +4414,11 @@
6220 q931_status(pri,c,PRI_CAUSE_WRONG_MESSAGE);
6223 + if (pri->localtype == BRI_NETWORK_PTMP) {
6224 + l2c = q921_getcall(pri, c, tei);
6226 + l2c->channel = c->channelno;
6228 pri->ev.proceeding.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6229 if (mh->msg == Q931_CALL_PROCEEDING) {
6230 pri->ev.e = PRI_EVENT_PROCEEDING;
6231 @@ -3364,16 +4436,21 @@
6234 if (c->ourcallstate != Q931_CALL_STATE_CONNECT_REQUEST) {
6235 + if ((c->ourcallstate == Q931_CALL_STATE_ACTIVE) && (c->con_acked == 0) && (network)) {
6236 + /* sending a CONNECT_ACKNOWLEDGE is optional for CPEs */
6238 q931_status(pri,c,PRI_CAUSE_WRONG_MESSAGE);
6243 c->ourcallstate = Q931_CALL_STATE_ACTIVE;
6244 c->peercallstate = Q931_CALL_STATE_ACTIVE;
6249 q931_status(pri, c, PRI_CAUSE_MANDATORY_IE_MISSING);
6250 - q931_destroycall(pri, c->cr);
6251 + q931_destroycall(pri, c->cr, c->tei);
6255 @@ -3410,31 +4487,70 @@
6260 + if (c->peercallstate != c->sugcallstate) {
6261 + pri_error(pri, "updating callstate, peercallstate %d to %d\n", c->peercallstate, c->sugcallstate);
6262 +// c->peercallstate = c->sugcallstate;
6263 + c->ourcallstate = c->sugcallstate;
6264 + if (c->sugcallstate != Q931_CALL_STATE_ACTIVE) {
6265 + /* pass hangup to upper layer! */
6267 + pri->ev.e = PRI_EVENT_HANGUP;
6268 + res = Q931_RES_HAVEEVENT;
6270 + } else if (c->sendhangupack) {
6271 + res = Q931_RES_HAVEEVENT;
6272 + pri->ev.e = PRI_EVENT_HANGUP_ACK;
6273 + q931_hangup(pri, c, c->cause);
6275 + q931_hangup(pri, c, c->cause);
6283 case Q931_RELEASE_COMPLETE:
6284 - c->ourcallstate = Q931_CALL_STATE_NULL;
6285 - c->peercallstate = Q931_CALL_STATE_NULL;
6286 - pri->ev.hangup.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6287 - pri->ev.hangup.cref = c->cr;
6288 - pri->ev.hangup.cause = c->cause;
6289 - pri->ev.hangup.call = c;
6290 - libpri_copy_string(pri->ev.hangup.useruserinfo, c->useruserinfo, sizeof(pri->ev.ring.useruserinfo));
6291 - c->useruserinfo[0] = '\0';
6292 - /* Free resources */
6294 - pri->ev.e = PRI_EVENT_HANGUP;
6295 - res = Q931_RES_HAVEEVENT;
6297 - } else if (c->sendhangupack) {
6298 - res = Q931_RES_HAVEEVENT;
6299 - pri->ev.e = PRI_EVENT_HANGUP_ACK;
6300 - pri_hangup(pri, c, c->cause);
6306 - q931_hangup(pri,c,c->cause);
6307 + if (pri->localtype != BRI_NETWORK_PTMP) {
6308 + /* only stop the T303 timer if WE are not a BRI PTMP network */
6309 + if (c->t303timer) {
6310 + c->t303running = 0;
6311 + pri_schedule_del(pri, c->t303timer);
6316 + if ((pri->localtype != BRI_NETWORK_PTMP) || (c->tei == tei)) {
6317 + c->ourcallstate = Q931_CALL_STATE_NULL;
6318 + c->peercallstate = Q931_CALL_STATE_NULL;
6319 + pri->ev.hangup.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6320 + pri->ev.hangup.cref = c->cr;
6321 + pri->ev.hangup.cause = c->cause;
6322 + pri->ev.hangup.call = c;
6323 + libpri_copy_string(pri->ev.hangup.useruserinfo, c->useruserinfo, sizeof(pri->ev.ring.useruserinfo));
6324 + c->useruserinfo[0] = '\0';
6325 + /* Free resources */
6327 + pri->ev.e = PRI_EVENT_HANGUP;
6328 + res = Q931_RES_HAVEEVENT;
6330 + } else if (c->sendhangupack) {
6331 + res = Q931_RES_HAVEEVENT;
6332 + pri->ev.e = PRI_EVENT_HANGUP_ACK;
6333 + pri_hangup(pri, c, c->cause, -1);
6339 + q931_hangup(pri,c,c->cause);
6342 + // ignoring relase_complete
6343 + res = q921_handle_hangup(pri,c,tei);
6350 @@ -3450,6 +4566,7 @@
6351 pri->ev.e = PRI_EVENT_HANGUP;
6352 pri->ev.hangup.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6353 pri->ev.hangup.cref = c->cr;
6354 + pri->ev.hangup.tei = c->tei;
6355 pri->ev.hangup.cause = c->cause;
6356 pri->ev.hangup.call = c;
6357 pri->ev.hangup.aoc_units = c->aoc_units;
6358 @@ -3478,9 +4595,16 @@
6359 pri->ev.e = PRI_EVENT_HANGUP_REQ;
6360 pri->ev.hangup.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6361 pri->ev.hangup.cref = c->cr;
6362 + pri->ev.hangup.tei = c->tei;
6363 pri->ev.hangup.cause = c->cause;
6364 pri->ev.hangup.call = c;
6365 pri->ev.hangup.aoc_units = c->aoc_units;
6366 + if (c->progressmask & PRI_PROG_INBAND_AVAILABLE) {
6367 + pri->ev.hangup.inband_progress = 1;
6369 + pri->ev.hangup.inband_progress = 0;
6371 + pri->ev.hangup.aoc_units = c->aoc_units;
6373 return Q931_RES_HAVEEVENT;
6375 @@ -3511,7 +4635,7 @@
6376 pri->ev.e = PRI_EVENT_INFO_RECEIVED;
6377 pri->ev.ring.call = c;
6378 pri->ev.ring.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6379 - libpri_copy_string(pri->ev.ring.callednum, c->callednum, sizeof(pri->ev.ring.callednum));
6380 + libpri_copy_string(pri->ev.ring.callednum, c->digits, sizeof(pri->ev.ring.callednum) );
6381 libpri_copy_string(pri->ev.ring.callingsubaddr, c->callingsubaddr, sizeof(pri->ev.ring.callingsubaddr));
6382 pri->ev.ring.complete = c->complete; /* this covers IE 33 (Sending Complete) */
6383 return Q931_RES_HAVEEVENT;
6384 @@ -3531,7 +4655,6 @@
6385 pri->ev.e = PRI_EVENT_SETUP_ACK;
6386 pri->ev.setup_ack.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6387 pri->ev.setup_ack.call = c;
6391 if (!cur->sent && cur->message == Q931_FACILITY) {
6392 @@ -3547,19 +4670,53 @@
6393 pri->ev.notify.channel = c->channelno;
6394 pri->ev.notify.info = c->notify;
6395 return Q931_RES_HAVEEVENT;
6397 + pri->ev.e = PRI_EVENT_HOLD_REQ;
6398 + pri->ev.hold_req.call = c;
6399 + pri->ev.hold_req.cref = c->cr;
6400 + pri->ev.hold_req.tei = c->tei;
6401 + pri->ev.hold_req.channel = c->channelno;
6402 + return Q931_RES_HAVEEVENT;
6404 + case Q931_RETRIEVE:
6405 + pri->ev.e = PRI_EVENT_RETRIEVE_REQ;
6406 + pri->ev.retrieve_req.call = c;
6407 + pri->ev.retrieve_req.cref = c->cr;
6408 + pri->ev.retrieve_req.tei = c->tei;
6409 + pri->ev.retrieve_req.channel = c->channelno;
6410 + return Q931_RES_HAVEEVENT;
6412 + case Q931_SUSPEND:
6413 + pri->ev.e = PRI_EVENT_SUSPEND_REQ;
6414 + pri->ev.suspend_req.call = c;
6415 + pri->ev.suspend_req.cref = c->cr;
6416 + pri->ev.suspend_req.tei = c->tei;
6417 + pri->ev.suspend_req.channel = c->channelno;
6418 + strncpy(pri->ev.suspend_req.callid, c->callid, sizeof(pri->ev.suspend_req.callid) - 1);
6419 + return Q931_RES_HAVEEVENT;
6422 + if (pri->localtype == BRI_NETWORK_PTMP) {
6423 + l2c = q921_getcall(pri, c, tei);
6426 + pri->ev.e = PRI_EVENT_RESUME_REQ;
6427 + pri->ev.resume_req.call = c;
6428 + pri->ev.resume_req.cref = c->cr;
6429 + pri->ev.resume_req.tei = c->tei;
6430 + pri->ev.resume_req.channel = c->channelno;
6431 + strncpy(pri->ev.resume_req.callid, c->callid, sizeof(pri->ev.resume_req.callid) - 1);
6432 + return Q931_RES_HAVEEVENT;
6434 case Q931_USER_INFORMATION:
6436 case Q931_CONGESTION_CONTROL:
6438 case Q931_HOLD_ACKNOWLEDGE:
6439 case Q931_HOLD_REJECT:
6440 - case Q931_RETRIEVE:
6441 case Q931_RETRIEVE_ACKNOWLEDGE:
6442 case Q931_RETRIEVE_REJECT:
6444 case Q931_RESUME_ACKNOWLEDGE:
6445 case Q931_RESUME_REJECT:
6446 - case Q931_SUSPEND:
6447 case Q931_SUSPEND_ACKNOWLEDGE:
6448 case Q931_SUSPEND_REJECT:
6449 pri_error(pri, "!! Not yet handling post-handle message type %s (%d)\n", msg2str(mh->msg), mh->msg);
6450 @@ -3569,7 +4726,7 @@
6451 pri_error(pri, "!! Don't know how to post-handle message type %s (%d)\n", msg2str(mh->msg), mh->msg);
6452 q931_status(pri,c, PRI_CAUSE_MESSAGE_TYPE_NONEXIST);
6454 - q931_destroycall(pri,c->cr);
6455 + q931_destroycall(pri,c->cr,c->tei);