Compare commits
44 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6a9ed870b | ||
|
|
0777515137 | ||
|
|
8f9296ef00 | ||
|
|
e1394704ad | ||
|
|
0961ecb4b0 | ||
|
|
a36186ce49 | ||
|
|
5f6e12c3e5 | ||
|
|
d80324dbdd | ||
|
|
e172ca2a03 | ||
|
|
e4f9ea7583 | ||
|
|
b5fa5986b2 | ||
|
|
f63cc76a94 | ||
|
|
cb01262a54 | ||
|
|
79e10c30a1 | ||
|
|
06d9c2aeb3 | ||
|
|
4e6c9e0410 | ||
|
|
88656d9497 | ||
|
|
fe0eba8175 | ||
|
|
24b85a379b | ||
|
|
ec99ad86dd | ||
|
|
ee9ef9ef3c | ||
|
|
f6966bc35b | ||
|
|
45efb97ebd | ||
|
|
110062aef6 | ||
|
|
de24dd76a6 | ||
|
|
e09654f0ed | ||
|
|
86fe59eacc | ||
|
|
2809056303 | ||
|
|
f8d3569b56 | ||
|
|
5f1aba027a | ||
|
|
a85d2c508b | ||
|
|
34f700b584 | ||
|
|
5137d733f9 | ||
|
|
5478d68b2a | ||
|
|
6e8e051464 | ||
|
|
80355d96c5 | ||
|
|
e4d3ca388b | ||
|
|
426336f4e6 | ||
|
|
af1366fcde | ||
|
|
96695361e4 | ||
|
|
cc7adcd328 | ||
|
|
2a1c974412 | ||
|
|
b00db5738c | ||
|
|
c0ce1c85ed |
19
Makefile
19
Makefile
@@ -20,22 +20,27 @@
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
#
|
||||
#
|
||||
# Uncomment if you want libpri not send PROGRESS_INDICATOR w/ALERTING
|
||||
#ALERTING=-DALERTING_NO_PROGRESS
|
||||
|
||||
TOBJS=testpri.o
|
||||
STATIC_LIBRARY=libpri.a
|
||||
DYNAMIC_LIBRARY=libpri.so.1.0
|
||||
STATIC_OBJS=pri.o q921.o prisched.o q931.o
|
||||
DYNAMIC_OBJS=pri.lo q921.lo prisched.lo q931.lo
|
||||
CFLAGS=-Wall -Werror -Wstrict-prototypes -Wmissing-prototypes -g
|
||||
CFLAGS=-Wall -Werror -Wstrict-prototypes -Wmissing-prototypes -g $(ALERTING)
|
||||
INSTALL_PREFIX=
|
||||
|
||||
all: $(STATIC_LIBRARY) $(DYNAMIC_LIBRARY)
|
||||
|
||||
install: $(STATIC_LIBRARY) $(DYNAMIC_LIBRARY)
|
||||
mkdir -p /usr/lib
|
||||
mkdir -p /usr/include
|
||||
install -m 644 libpri.h /usr/include
|
||||
install -m 755 $(DYNAMIC_LIBRARY) /usr/lib
|
||||
( cd /usr/lib ; ln -sf libpri.so.1 libpri.so )
|
||||
install -m 644 $(STATIC_LIBRARY) /usr/lib
|
||||
mkdir -p $(INSTALL_PREFIX)/usr/lib
|
||||
mkdir -p $(INSTALL_PREFIX)/usr/include
|
||||
install -m 644 libpri.h $(INSTALL_PREFIX)/usr/include
|
||||
install -m 755 $(DYNAMIC_LIBRARY) $(INSTALL_PREFIX)/usr/lib
|
||||
( cd $(INSTALL_PREFIX)/usr/lib ; ln -sf libpri.so.1 libpri.so )
|
||||
install -m 644 $(STATIC_LIBRARY) $(INSTALL_PREFIX)/usr/lib
|
||||
/sbin/ldconfig
|
||||
|
||||
pritest: pritest.o
|
||||
|
||||
71
libpri.h
71
libpri.h
@@ -48,6 +48,7 @@
|
||||
#define PRI_SWITCH_ATT4ESS 4 /* AT&T 4ESS */
|
||||
#define PRI_SWITCH_EUROISDN_E1 5 /* Standard EuroISDN (CTR4, ETSI 300-102) */
|
||||
#define PRI_SWITCH_EUROISDN_T1 6 /* T1 EuroISDN variant (ETSI 300-102) */
|
||||
#define PRI_SWITCH_NI1 7 /* National ISDN 1 */
|
||||
|
||||
/* PRI D-Channel Events */
|
||||
#define PRI_EVENT_DCHAN_UP 1 /* D-channel is up */
|
||||
@@ -60,6 +61,11 @@
|
||||
#define PRI_EVENT_ANSWER 8 /* Call has been answered */
|
||||
#define PRI_EVENT_HANGUP_ACK 9 /* Call hangup has been acknowledged */
|
||||
#define PRI_EVENT_RESTART_ACK 10 /* Restart complete on a given channel */
|
||||
#define PRI_EVENT_FACNAME 11 /* Caller*ID Name received on Facility */
|
||||
#define PRI_EVENT_INFO_RECEIVED 12 /* Additional info (keypad) received */
|
||||
#define PRI_EVENT_PROCEEDING 13 /* When we get CALL_PROCEEDING or PROGRESS */
|
||||
#define PRI_EVENT_SETUP_ACK 14 /* When we get SETUP_ACKNOWLEDGE */
|
||||
#define PRI_EVENT_HANGUP_REQ 15 /* Requesting the higher layer to hangup */
|
||||
|
||||
/* Simple states */
|
||||
#define PRI_STATE_DOWN 0
|
||||
@@ -174,6 +180,8 @@
|
||||
#define PRI_LAYER_1_V120_RATE_ADAPT 0x28
|
||||
#define PRI_LAYER_1_X31_RATE_ADAPT 0x29
|
||||
|
||||
#define PRI_COPY_DIGITS_CALLED_NUMBER
|
||||
|
||||
typedef struct q931_call q931_call;
|
||||
|
||||
typedef struct pri_event_generic {
|
||||
@@ -205,18 +213,31 @@ typedef struct pri_event_answer {
|
||||
q931_call *call;
|
||||
} pri_event_answer;
|
||||
|
||||
typedef struct pri_event_facname {
|
||||
int e;
|
||||
char callingname[256];
|
||||
char callingnum[256];
|
||||
int channel;
|
||||
int cref;
|
||||
q931_call *call;
|
||||
} pri_event_facname;
|
||||
|
||||
typedef struct pri_event_ring {
|
||||
int e;
|
||||
int channel; /* Channel requested */
|
||||
int callingpres; /* Presentation of Calling CallerID */
|
||||
int callingplan; /* Dialing plan of Calling entity */
|
||||
char callingnum[256]; /* Calling number */
|
||||
char callingname[256]; /* Calling name (if provided) */
|
||||
int calledplan; /* Dialing plan of Called number */
|
||||
char callednum[256]; /* Called number */
|
||||
char redirectingnum[256]; /* Redirecting number */
|
||||
char useruserinfo[256]; /* User->User info */
|
||||
int flexible; /* Are we flexible with our channel selection? */
|
||||
int cref; /* Call Reference Number */
|
||||
int ctype; /* Call type (see PRI_TRANS_CAP_* */
|
||||
int layer1; /* User layer 1 */
|
||||
int complete; /* Have we seen "Complete" i.e. no more number? */
|
||||
q931_call *call; /* Opaque call pointer */
|
||||
} pri_event_ring;
|
||||
|
||||
@@ -233,16 +254,29 @@ typedef struct pri_event_restart_ack {
|
||||
int channel;
|
||||
} pri_event_restart_ack;
|
||||
|
||||
typedef struct pri_event_proceeding {
|
||||
int e;
|
||||
int channel;
|
||||
} pri_event_proceeding;
|
||||
|
||||
typedef struct pri_event_setup_ack {
|
||||
int e;
|
||||
int channel;
|
||||
} pri_event_setup_ack;
|
||||
|
||||
typedef union {
|
||||
int e;
|
||||
pri_event_generic gen; /* Generic view */
|
||||
pri_event_restart restart; /* Restart view */
|
||||
pri_event_error err; /* Error view */
|
||||
pri_event_facname facname; /* Caller*ID Name on Facility */
|
||||
pri_event_ring ring; /* Ring */
|
||||
pri_event_hangup hangup; /* Hang up */
|
||||
pri_event_ringing ringing; /* Ringing */
|
||||
pri_event_ringing answer; /* Answer */
|
||||
pri_event_restart_ack restartack; /* Restart Acknowledge */
|
||||
pri_event_proceeding proceeding; /* Call proceeding & Progress */
|
||||
pri_event_setup_ack setup_ack; /* SETUP_ACKNOWLEDGE structure */
|
||||
} pri_event;
|
||||
|
||||
struct pri;
|
||||
@@ -293,15 +327,42 @@ extern char *pri_cause2str(int cause);
|
||||
is in-band data available on the channel */
|
||||
extern int pri_acknowledge(struct pri *pri, q931_call *call, int channel, int info);
|
||||
|
||||
/* Send a digit in overlap mode */
|
||||
extern int pri_information(struct pri *pri, q931_call *call, char digit);
|
||||
|
||||
/* Answer the incomplete(call without called number) call on the given channel.
|
||||
Set non-isdn to non-zero if you are not connecting to ISDN equipment */
|
||||
extern int pri_need_more_info(struct pri *pri, q931_call *call, int channel, int nonisdn);
|
||||
|
||||
/* Answer the call on the given channel (ignored if you called acknowledge already).
|
||||
Set non-isdn to non-zero if you are not connecting to ISDN equipment */
|
||||
extern int pri_answer(struct pri *pri, q931_call *call, int channel, int nonisdn);
|
||||
|
||||
#if 0
|
||||
/* deprecated routines, use pri_hangup */
|
||||
/* Release/Reject a call */
|
||||
extern int pri_release(struct pri *pri, q931_call *call, int cause);
|
||||
|
||||
/* Hangup / Disconnect a call */
|
||||
extern int pri_disconnect(struct pri *pri, q931_call *call, int cause);
|
||||
#endif
|
||||
|
||||
#undef pri_release
|
||||
#undef pri_disconnect
|
||||
|
||||
/* backwards compatibility for those who don't use asterisk with libpri */
|
||||
#define pri_release(a,b,c) \
|
||||
pri_hangup(a,b,c)
|
||||
|
||||
#define pri_disconnect(a,b,c) \
|
||||
pri_hangup(a,b,c)
|
||||
|
||||
/* Hangup a call */
|
||||
#define PRI_HANGUP
|
||||
extern int pri_hangup(struct pri *pri, q931_call *call, int cause);
|
||||
|
||||
#define PRI_DESTROYCALL
|
||||
extern void pri_destroycall(struct pri *pri, q931_call *call);
|
||||
|
||||
extern int pri_reset(struct pri *pri, int channel);
|
||||
|
||||
@@ -315,6 +376,14 @@ extern struct timeval *pri_schedule_next(struct pri *pri);
|
||||
extern pri_event *pri_schedule_run(struct pri *pri);
|
||||
|
||||
extern int pri_call(struct pri *pri, q931_call *c, int transmode, int channel,
|
||||
int exclusive, int nonisdn, char *caller, int callerplan, int callerpres,
|
||||
int exclusive, int nonisdn, char *caller, int callerplan, char *callername, int callerpres,
|
||||
char *called,int calledplan, int ulayer1);
|
||||
|
||||
/* Override message and error stuff */
|
||||
extern void pri_set_message(void (*__pri_error)(char *));
|
||||
extern void pri_set_error(void (*__pri_error)(char *));
|
||||
|
||||
/* Set overlap mode */
|
||||
#define PRI_SET_OVERLAPDIAL
|
||||
extern void pri_set_overlapdial(struct pri *pri,int state);
|
||||
#endif
|
||||
|
||||
108
pri.c
108
pri.c
@@ -18,6 +18,7 @@
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/select.h>
|
||||
#include <stdarg.h>
|
||||
#include "libpri.h"
|
||||
#include "pri_internal.h"
|
||||
#include "pri_q921.h"
|
||||
@@ -48,6 +49,8 @@ char *pri_switch2str(int sw)
|
||||
return "Lucent 5E";
|
||||
case PRI_SWITCH_ATT4ESS:
|
||||
return "AT&T 4ESS";
|
||||
case PRI_SWITCH_NI1:
|
||||
return "National ISDN 1";
|
||||
default:
|
||||
return "Unknown switchtype";
|
||||
}
|
||||
@@ -64,7 +67,7 @@ struct pri *pri_new(int fd, int node, int switchtype)
|
||||
p->switchtype = switchtype;
|
||||
p->cref = 1;
|
||||
/* Start Q.921 layer */
|
||||
q921_start(p);
|
||||
q921_start(p, 1);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
@@ -95,9 +98,11 @@ pri_event *pri_check_event(struct pri *pri)
|
||||
res = read(pri->fd, buf, sizeof(buf));
|
||||
if (res < 0) {
|
||||
if (errno != EAGAIN)
|
||||
fprintf(stderr, "Read on %d failed: %s\n", pri->fd, strerror(errno));
|
||||
pri_error("Read on %d failed: %s\n", pri->fd, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
if (!res)
|
||||
return NULL;
|
||||
/* Receive the q921 packet */
|
||||
e = q921_receive(pri, (q921_h *)buf, res);
|
||||
return e;
|
||||
@@ -178,6 +183,27 @@ int pri_acknowledge(struct pri *pri, q931_call *call, int channel, int info)
|
||||
return q931_alerting(pri, call, channel, info);
|
||||
}
|
||||
|
||||
int pri_information(struct pri *pri, q931_call *call, char digit)
|
||||
{
|
||||
if (!pri || !call)
|
||||
return -1;
|
||||
return q931_information(pri, call, digit);
|
||||
}
|
||||
|
||||
void pri_destroycall(struct pri *pri, q931_call *call)
|
||||
{
|
||||
if (pri && call)
|
||||
__q931_destroycall(pri, call);
|
||||
return;
|
||||
}
|
||||
|
||||
int pri_need_more_info(struct pri *pri, q931_call *call, int channel, int nonisdn)
|
||||
{
|
||||
if (!pri || !call)
|
||||
return -1;
|
||||
return q931_setup_ack(pri, call, channel, nonisdn);
|
||||
}
|
||||
|
||||
int pri_answer(struct pri *pri, q931_call *call, int channel, int nonisdn)
|
||||
{
|
||||
if (!pri || !call)
|
||||
@@ -185,6 +211,8 @@ int pri_answer(struct pri *pri, q931_call *call, int channel, int nonisdn)
|
||||
return q931_connect(pri, call, channel, nonisdn);
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* deprecated routines, use pri_hangup */
|
||||
int pri_release(struct pri *pri, q931_call *call, int cause)
|
||||
{
|
||||
if (!pri || !call)
|
||||
@@ -198,6 +226,17 @@ int pri_disconnect(struct pri *pri, q931_call *call, int cause)
|
||||
return -1;
|
||||
return q931_disconnect(pri, call, cause);
|
||||
}
|
||||
#endif
|
||||
|
||||
int pri_hangup(struct pri *pri, q931_call *call, int cause)
|
||||
{
|
||||
if (!pri || !call)
|
||||
return -1;
|
||||
if (cause == -1)
|
||||
/* normal clear cause */
|
||||
cause = 16;
|
||||
return q931_hangup(pri, call, cause);
|
||||
}
|
||||
|
||||
int pri_reset(struct pri *pri, int channel)
|
||||
{
|
||||
@@ -217,34 +256,79 @@ void pri_dump_event(struct pri *pri, pri_event *e)
|
||||
{
|
||||
if (!pri || !e)
|
||||
return;
|
||||
printf("Event type: %s (%d)\n", pri_event2str(e->gen.e), e->gen.e);
|
||||
pri_message("Event type: %s (%d)\n", pri_event2str(e->gen.e), e->gen.e);
|
||||
switch(e->gen.e) {
|
||||
case PRI_EVENT_DCHAN_UP:
|
||||
case PRI_EVENT_DCHAN_DOWN:
|
||||
break;
|
||||
case PRI_EVENT_CONFIG_ERR:
|
||||
printf("Error: %s", e->err.err);
|
||||
pri_message("Error: %s", e->err.err);
|
||||
break;
|
||||
case PRI_EVENT_RESTART:
|
||||
printf("Restart on channel %d\n", e->restart.channel);
|
||||
pri_message("Restart on channel %d\n", e->restart.channel);
|
||||
case PRI_EVENT_RING:
|
||||
printf("Calling number: %s (%s, %s)\n", e->ring.callingnum, pri_plan2str(e->ring.callingplan), pri_pres2str(e->ring.callingpres));
|
||||
printf("Called number: %s (%s)\n", e->ring.callednum, pri_plan2str(e->ring.calledplan));
|
||||
printf("Channel: %d (%s) Reference number: %d\n", e->ring.channel, e->ring.flexible ? "Flexible" : "Not Flexible", e->ring.cref);
|
||||
pri_message("Calling number: %s (%s, %s)\n", e->ring.callingnum, pri_plan2str(e->ring.callingplan), pri_pres2str(e->ring.callingpres));
|
||||
pri_message("Called number: %s (%s)\n", e->ring.callednum, pri_plan2str(e->ring.calledplan));
|
||||
pri_message("Channel: %d (%s) Reference number: %d\n", e->ring.channel, e->ring.flexible ? "Flexible" : "Not Flexible", e->ring.cref);
|
||||
break;
|
||||
case PRI_EVENT_HANGUP:
|
||||
printf("Hangup, reference number: %d, reason: %s\n", e->hangup.cref, pri_cause2str(e->hangup.cause));
|
||||
pri_message("Hangup, reference number: %d, reason: %s\n", e->hangup.cref, pri_cause2str(e->hangup.cause));
|
||||
break;
|
||||
default:
|
||||
printf("Don't know how to dump events of type %d\n", e->gen.e);
|
||||
pri_message("Don't know how to dump events of type %d\n", e->gen.e);
|
||||
}
|
||||
}
|
||||
|
||||
int pri_call(struct pri *pri, q931_call *c, int transmode, int channel, int exclusive,
|
||||
int nonisdn, char *caller, int callerplan, int callerpres, char *called,
|
||||
int nonisdn, char *caller, int callerplan, char *callername, int callerpres, char *called,
|
||||
int calledplan,int ulayer1)
|
||||
{
|
||||
if (!pri || !c)
|
||||
return -1;
|
||||
return q931_setup(pri, c, transmode, channel, exclusive, nonisdn, caller, callerplan, callerpres, called, calledplan, ulayer1);
|
||||
return q931_setup(pri, c, transmode, channel, exclusive, nonisdn, caller, callerplan, callername, callerpres, called, calledplan, ulayer1);
|
||||
}
|
||||
|
||||
static void (*__pri_error)(char *stuff);
|
||||
static void (*__pri_message)(char *stuff);
|
||||
|
||||
void pri_set_message(void (*func)(char *stuff))
|
||||
{
|
||||
__pri_message = func;
|
||||
}
|
||||
|
||||
void pri_set_error(void (*func)(char *stuff))
|
||||
{
|
||||
__pri_error = func;
|
||||
}
|
||||
|
||||
void pri_message(char *fmt, ...)
|
||||
{
|
||||
char tmp[1024];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(tmp, sizeof(tmp), fmt, ap);
|
||||
va_end(ap);
|
||||
if (__pri_message)
|
||||
__pri_message(tmp);
|
||||
else
|
||||
fprintf(stdout, tmp);
|
||||
}
|
||||
|
||||
void pri_error(char *fmt, ...)
|
||||
{
|
||||
char tmp[1024];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(tmp, sizeof(tmp), fmt, ap);
|
||||
va_end(ap);
|
||||
if (__pri_error)
|
||||
__pri_error(tmp);
|
||||
else
|
||||
fprintf(stderr, tmp);
|
||||
}
|
||||
/* Set overlap mode */
|
||||
void pri_set_overlapdial(struct pri *pri,int state)
|
||||
{
|
||||
pri->overlapdial = state;
|
||||
}
|
||||
|
||||
|
||||
@@ -58,6 +58,7 @@ struct pri {
|
||||
int v_na; /* What we've told our peer we've acknowledged */
|
||||
int solicitfbit; /* Have we sent an I or S frame with the F-bit set? */
|
||||
int retrans; /* Retransmissions */
|
||||
int sentrej; /* Are we in reject state */
|
||||
|
||||
int cref; /* Next call reference value */
|
||||
|
||||
@@ -78,6 +79,9 @@ struct pri {
|
||||
|
||||
/* Q.931 calls */
|
||||
q931_call *calls;
|
||||
|
||||
/* do we do overlap dialing */
|
||||
int overlapdial;
|
||||
};
|
||||
|
||||
extern int pri_schedule_event(struct pri *pri, int ms, void (*function)(void *data), void *data);
|
||||
@@ -88,4 +92,8 @@ extern void pri_schedule_del(struct pri *pri, int ev);
|
||||
|
||||
extern pri_event *pri_mkerror(struct pri *pri, char *errstr);
|
||||
|
||||
extern void pri_message(char *fmt, ...);
|
||||
|
||||
extern void pri_error(char *fmt, ...);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -137,7 +137,7 @@ typedef struct q921_frame {
|
||||
q921_i h;
|
||||
} q921_frame;
|
||||
|
||||
#define Q921_INC(j) (j) = ((j) + 1) % 128
|
||||
#define Q921_INC(j) (j) = (((j) + 1) % 128)
|
||||
|
||||
typedef enum q921_state {
|
||||
Q921_LINK_CONNECTION_RELEASED, /* Also known as TEI_ASSIGNED */
|
||||
@@ -150,7 +150,7 @@ typedef enum q921_state {
|
||||
extern void q921_dump(q921_h *h, int len, int showraw, int txrx);
|
||||
|
||||
/* Bring up the D-channel */
|
||||
extern void q921_start(struct pri *pri);
|
||||
extern void q921_start(struct pri *pri, int now);
|
||||
|
||||
extern void q921_reset(struct pri *pri);
|
||||
|
||||
|
||||
35
pri_q931.h
35
pri_q931.h
@@ -190,6 +190,7 @@ typedef struct q931_ie {
|
||||
#define Q931_LOW_LAYER_COMPAT 0x7c
|
||||
#define Q931_HIGH_LAYER_COMPAT 0x7d
|
||||
|
||||
#define Q931_DISPLAY 0x28
|
||||
#define Q931_IE_SEGMENTED_MSG 0x00
|
||||
#define Q931_IE_CHANGE_STATUS 0x01
|
||||
#define Q931_IE_CONNECTED_NUM 0x0C
|
||||
@@ -197,7 +198,6 @@ typedef struct q931_ie {
|
||||
#define Q931_IE_FACILITY 0x1c
|
||||
#define Q931_IE_ENDPOINT_ID 0x26
|
||||
#define Q931_IE_NOTIFY_IND 0x27
|
||||
#define Q931_IE_DISPLAY 0x28
|
||||
#define Q931_IE_TIME_DATE 0x29
|
||||
#define Q931_IE_KEYPAD_FACILITY 0x2c
|
||||
#define Q931_IE_CALL_STATUS 0x2d
|
||||
@@ -214,6 +214,28 @@ typedef struct q931_ie {
|
||||
#define Q931_IE_USER_USER 0x7E
|
||||
#define Q931_IE_ESCAPE_FOR_EXT 0x7F
|
||||
|
||||
|
||||
/* Call state stuff */
|
||||
#define Q931_CALL_STATE_NULL 0
|
||||
#define Q931_CALL_STATE_CALL_INITIATED 1
|
||||
#define Q931_CALL_STATE_OVERLAP_SENDING 2
|
||||
#define Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING 3
|
||||
#define Q931_CALL_STATE_CALL_DELIVERED 4
|
||||
#define Q931_CALL_STATE_CALL_PRESENT 6
|
||||
#define Q931_CALL_STATE_CALL_RECEIVED 7
|
||||
#define Q931_CALL_STATE_CONNECT_REQUEST 8
|
||||
#define Q931_CALL_STATE_INCOMING_CALL_PROCEEDING 9
|
||||
#define Q931_CALL_STATE_ACTIVE 10
|
||||
#define Q931_CALL_STATE_DISCONNECT_REQUEST 11
|
||||
#define Q931_CALL_STATE_DISCONNECT_INDICATION 12
|
||||
#define Q931_CALL_STATE_SUSPEND_REQUEST 15
|
||||
#define Q931_CALL_STATE_RESUME_REQUEST 17
|
||||
#define Q931_CALL_STATE_RELEASE_REQUEST 19
|
||||
#define Q931_CALL_STATE_OVERLAP_RECEIVING 25
|
||||
#define Q931_CALL_STATE_RESTART_REQUEST 61
|
||||
#define Q931_CALL_STATE_RESTART 62
|
||||
|
||||
|
||||
/* EuroISDN */
|
||||
#define Q931_SENDING_COMPLETE 0x21
|
||||
|
||||
@@ -223,18 +245,27 @@ extern int q931_alerting(struct pri *pri, q931_call *call, int channel, int info
|
||||
|
||||
extern int q931_call_proceeding(struct pri *pri, q931_call *call);
|
||||
|
||||
extern int q931_setup_ack(struct pri *pri, q931_call *call, int channel, int nonisdn);
|
||||
|
||||
extern int q931_information(struct pri *pri, q931_call *call, char digit);
|
||||
|
||||
extern int q931_connect(struct pri *pri, q931_call *call, int channel, int nonisdn);
|
||||
|
||||
extern int q931_release(struct pri *pri, q931_call *call, int cause);
|
||||
|
||||
extern int q931_disconnect(struct pri *pri, q931_call *call, int cause);
|
||||
|
||||
extern int q931_hangup(struct pri *pri, q931_call *call, int cause);
|
||||
|
||||
extern int q931_restart(struct pri *pri, int channel);
|
||||
|
||||
extern q931_call *q931_new_call(struct pri *pri);
|
||||
|
||||
extern int q931_setup(struct pri *pri, q931_call *c, int transmode, int channel, int exclusive,
|
||||
int nonisdn, char *caller, int callerplan, int callerpres, char *called,
|
||||
int nonisdn, char *caller, int callerplan, char *callername, int callerpres, char *called,
|
||||
int calledplan, int ulay1);
|
||||
extern void q931_dump(q931_h *h, int len, int txrx);
|
||||
|
||||
extern void __q931_destroycall(struct pri *pri, q931_call *c);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -38,7 +38,7 @@ int pri_schedule_event(struct pri *pri, int ms, void (*function)(void *data), vo
|
||||
if (!pri->pri_sched[x].callback)
|
||||
break;
|
||||
if (x == MAX_SCHED) {
|
||||
fprintf(stderr, "No more room in scheduler\n");
|
||||
pri_error("No more room in scheduler\n");
|
||||
return -1;
|
||||
}
|
||||
if (x > maxsched)
|
||||
@@ -98,6 +98,6 @@ pri_event *pri_schedule_run(struct pri *pri)
|
||||
void pri_schedule_del(struct pri *pri,int id)
|
||||
{
|
||||
if ((id >= MAX_SCHED) || (id < 0))
|
||||
fprintf(stderr, "Asked to delete sched id %d???\n", id);
|
||||
pri_error("Asked to delete sched id %d???\n", id);
|
||||
pri->pri_sched[id].callback = NULL;
|
||||
}
|
||||
|
||||
56
pritest.c
56
pritest.c
@@ -48,6 +48,8 @@
|
||||
#define PRI_DEF_SWITCHTYPE PRI_SWITCH_NI2
|
||||
|
||||
#define MAX_CHAN 32
|
||||
#define DCHANNEL_TIMESLOT 16
|
||||
|
||||
|
||||
static int offset = 0;
|
||||
|
||||
@@ -162,10 +164,50 @@ static void launch_channel(int channo)
|
||||
|
||||
}
|
||||
|
||||
static int get_free_channel(int channo)
|
||||
{
|
||||
channo--;
|
||||
if((channo>MAX_CHAN)||(channo<0)) {
|
||||
fprintf(stderr, "Invalid Bchannel RANGE <%d", channo);
|
||||
return 0;
|
||||
};
|
||||
|
||||
while(chans[channo].pid) {
|
||||
channo--;
|
||||
}
|
||||
|
||||
return channo;
|
||||
}
|
||||
|
||||
/* place here criteria for completion of destination number */
|
||||
static int number_incommplete(char *number)
|
||||
{
|
||||
return strlen(number) < 3;
|
||||
}
|
||||
|
||||
static void start_channel(struct pri *pri, pri_event *e)
|
||||
{
|
||||
int channo = e->ring.channel;
|
||||
int flag = 1;
|
||||
pri_event_ring *ring = &e->ring;
|
||||
|
||||
if(channo == -1) {
|
||||
channo = e->ring.channel = get_free_channel(MAX_CHAN);
|
||||
|
||||
if(channo == DCHANNEL_TIMESLOT)
|
||||
channo = e->ring.channel = get_free_channel(MAX_CHAN);
|
||||
|
||||
|
||||
fprintf(stdout, "Any channel selected: %d\n", channo);
|
||||
|
||||
if(!channo) {
|
||||
pri_release(pri, ring->call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
|
||||
fprintf(stdout, "Abort call due to no avl B channels\n");
|
||||
return;
|
||||
}
|
||||
|
||||
flag = 0;
|
||||
}
|
||||
/* Make sure it's a valid number */
|
||||
if ((channo >= MAX_CHAN) || (channo < 0)) {
|
||||
fprintf(stderr, "--!! Channel %d is out of range\n", channo);
|
||||
@@ -185,7 +227,11 @@ static void start_channel(struct pri *pri, pri_event *e)
|
||||
chans[channo].call = e->ring.call;
|
||||
|
||||
/* Answer the line */
|
||||
pri_answer(pri, chans[channo].call, channo, 1);
|
||||
if(flag) {
|
||||
pri_answer(pri, chans[channo].call, channo, 1);
|
||||
} else {
|
||||
pri_need_more_info(pri, chans[channo].call, channo, 1);
|
||||
}
|
||||
|
||||
/* Launch a process to handle it */
|
||||
launch_channel(channo);
|
||||
@@ -222,6 +268,14 @@ static void handle_pri_event(struct pri *pri, pri_event *e)
|
||||
break;
|
||||
case PRI_EVENT_HANGUP_ACK:
|
||||
/* Ignore */
|
||||
break;
|
||||
case PRI_EVENT_INFO_RECEIVED:
|
||||
fprintf(stdout, "number is: %s\n", e->ring.callednum);
|
||||
if(!number_incommplete(e->ring.callednum)) {
|
||||
fprintf(stdout, "final number is: %s\n", e->ring.callednum);
|
||||
pri_answer(pri, e->ring.call, 0, 1);
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "--!! Unknown PRI event %d\n", e->e);
|
||||
|
||||
310
q921.c
310
q921.c
@@ -42,12 +42,15 @@
|
||||
*/
|
||||
|
||||
#define Q921_INIT(hf) do { \
|
||||
memset(&(hf),0,sizeof(hf)); \
|
||||
(hf).h.sapi = 0; \
|
||||
(hf).h.ea1 = 0; \
|
||||
(hf).h.ea2 = 1; \
|
||||
(hf).h.tei = 0; \
|
||||
} while(0)
|
||||
|
||||
static void reschedule_t203(struct pri *pri);
|
||||
|
||||
static void q921_discard_retransmissions(struct pri *pri)
|
||||
{
|
||||
struct q921_frame *f, *p;
|
||||
@@ -61,11 +64,12 @@ static void q921_discard_retransmissions(struct pri *pri)
|
||||
pri->txqueue = NULL;
|
||||
}
|
||||
|
||||
static int q921_transmit(struct pri *pri, q921_h *h, int len) {
|
||||
static int q921_transmit(struct pri *pri, q921_h *h, int len)
|
||||
{
|
||||
int res;
|
||||
#ifdef RANDOM_DROPS
|
||||
if (!(random() % 3)) {
|
||||
printf(" === Dropping Packet ===\n");
|
||||
pri_message(" === Dropping Packet ===\n");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
@@ -76,9 +80,10 @@ static int q921_transmit(struct pri *pri, q921_h *h, int len) {
|
||||
/* Write an extra two bytes for the FCS */
|
||||
res = write(pri->fd, h, len + 2);
|
||||
if (res != (len + 2)) {
|
||||
fprintf(stderr, "Short write: %d/%d (%s)\n", res, len + 2, strerror(errno));
|
||||
pri_error("Short write: %d/%d (%s)\n", res, len + 2, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
reschedule_t203(pri);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -98,21 +103,25 @@ static void q921_send_ua(struct pri *pri, int pfbit)
|
||||
h.h.c_r = 1;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Don't know how to U/A on a type %d node\n", pri->localtype);
|
||||
pri_error("Don't know how to U/A on a type %d node\n", pri->localtype);
|
||||
return;
|
||||
}
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("Sending Unnumbered Acknowledgement\n");
|
||||
pri_message("Sending Unnumbered Acknowledgement\n");
|
||||
q921_transmit(pri, &h, 3);
|
||||
}
|
||||
|
||||
static void q921_send_sabme(void *vpri)
|
||||
static void q921_send_sabme_now(void *vpri);
|
||||
|
||||
static void q921_send_sabme(void *vpri, int now)
|
||||
{
|
||||
struct pri *pri = vpri;
|
||||
q921_h h;
|
||||
pri_schedule_del(pri, pri->sabme_timer);
|
||||
pri->sabme_timer = 0;
|
||||
pri->sabme_timer = pri_schedule_event(pri, T_200, q921_send_sabme, pri);
|
||||
pri->sabme_timer = pri_schedule_event(pri, T_200, q921_send_sabme_now, pri);
|
||||
if (!now)
|
||||
return;
|
||||
Q921_INIT(h);
|
||||
h.u.m3 = 3; /* M3 = 3 */
|
||||
h.u.m2 = 3; /* M2 = 3 */
|
||||
@@ -126,31 +135,42 @@ static void q921_send_sabme(void *vpri)
|
||||
h.h.c_r = 0;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Don't know how to U/A on a type %d node\n", pri->localtype);
|
||||
pri_error("Don't know how to U/A on a type %d node\n", pri->localtype);
|
||||
return;
|
||||
}
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("Sending Set Asynchronous Balanced Mode Extended\n");
|
||||
pri_message("Sending Set Asynchronous Balanced Mode Extended\n");
|
||||
q921_transmit(pri, &h, 3);
|
||||
pri->q921_state = Q921_AWAITING_ESTABLISH;
|
||||
}
|
||||
|
||||
static void q921_send_sabme_now(void *vpri)
|
||||
{
|
||||
q921_send_sabme(vpri, 1);
|
||||
}
|
||||
|
||||
static int q921_ack_packet(struct pri *pri, int num)
|
||||
{
|
||||
struct q921_frame *f, *prev = NULL;
|
||||
f = pri->txqueue;
|
||||
while(f) {
|
||||
if (f->h.n_s == num) {
|
||||
/* Cancel each packet as necessary */
|
||||
/* That's our packet */
|
||||
if (prev)
|
||||
prev->next = f->next;
|
||||
else
|
||||
pri->txqueue = f->next;
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
pri_message("-- ACKing packet %d, new txqueue is %d (-1 means empty)\n", f->h.n_s, pri->txqueue ? pri->txqueue->h.n_s : -1);
|
||||
/* Update v_a */
|
||||
pri->v_a = num;
|
||||
free(f);
|
||||
/* Reset retransmission counter if we actually acked something */
|
||||
pri->retrans = 0;
|
||||
return 1;
|
||||
}
|
||||
prev = f;
|
||||
f = f->next;
|
||||
}
|
||||
return 0;
|
||||
@@ -158,49 +178,68 @@ static int q921_ack_packet(struct pri *pri, int num)
|
||||
|
||||
static void t203_expire(void *);
|
||||
static void t200_expire(void *);
|
||||
static pri_event *q921_dchannel_down(struct pri *pri);
|
||||
|
||||
static void q921_ack_rx(struct pri *pri, int ack)
|
||||
static void reschedule_t203(struct pri *pri)
|
||||
{
|
||||
if (pri->t203_timer) {
|
||||
pri_schedule_del(pri, pri->t203_timer);
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
pri_message("-- Restarting T203 counter\n");
|
||||
/* Nothing to transmit, start the T203 counter instead */
|
||||
pri->t203_timer = pri_schedule_event(pri, T_203, t203_expire, pri);
|
||||
}
|
||||
}
|
||||
|
||||
static pri_event *q921_ack_rx(struct pri *pri, int ack)
|
||||
{
|
||||
int x;
|
||||
int cnt=0;
|
||||
pri_event *ev;
|
||||
/* Make sure the ACK was within our window */
|
||||
for (x=pri->v_a; (x != pri->v_s) && (x != ack); Q921_INC(x));
|
||||
if (x != ack) {
|
||||
/* ACK was outside of our window --- ignore */
|
||||
fprintf(stderr, "ACK received outside of window, ignoring\n");
|
||||
return;
|
||||
pri_error("ACK received outside of window, restarting\n");
|
||||
ev = q921_dchannel_down(pri);
|
||||
q921_start(pri, 1);
|
||||
pri->schedev = 1;
|
||||
return ev;
|
||||
}
|
||||
/* Cancel each packet as necessary */
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
pri_message("-- ACKing all packets from %d to (but not including) %d\n", pri->v_a, ack);
|
||||
for (x=pri->v_a; x != ack; Q921_INC(x))
|
||||
cnt += q921_ack_packet(pri, x);
|
||||
if (!pri->txqueue) {
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- Since there was nothing left, stopping T200 counter\n");
|
||||
pri_message("-- Since there was nothing left, stopping T200 counter\n");
|
||||
/* Something was ACK'd. Stop T200 counter */
|
||||
pri_schedule_del(pri, pri->t200_timer);
|
||||
pri->t200_timer = 0;
|
||||
}
|
||||
if (pri->t203_timer) {
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- Stopping T203 counter since we got an ACK\n");
|
||||
pri_message("-- Stopping T203 counter since we got an ACK\n");
|
||||
pri_schedule_del(pri, pri->t203_timer);
|
||||
pri->t203_timer = 0;
|
||||
}
|
||||
if (pri->txqueue) {
|
||||
/* Something left to transmit, Start the T200 counter again if we stopped it */
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- Something left to transmit, restarting T200 counter\n");
|
||||
pri_message("-- Something left to transmit (%d), restarting T200 counter\n", pri->txqueue->h.n_s);
|
||||
if (!pri->t200_timer)
|
||||
pri->t200_timer = pri_schedule_event(pri, T_200, t200_expire, pri);
|
||||
} else {
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- Nothing left, starting T203 counter\n");
|
||||
pri_message("-- Nothing left, starting T203 counter\n");
|
||||
/* Nothing to transmit, start the T203 counter instead */
|
||||
pri->t203_timer = pri_schedule_event(pri, T_203, t203_expire, pri);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void q921_reject(struct pri *pri)
|
||||
static void q921_reject(struct pri *pri, int pf)
|
||||
{
|
||||
q921_h h;
|
||||
Q921_INIT(h);
|
||||
@@ -208,7 +247,7 @@ static void q921_reject(struct pri *pri)
|
||||
h.s.ss = 2; /* Reject */
|
||||
h.s.ft = 1; /* Frametype (01) */
|
||||
h.s.n_r = pri->v_r; /* Where to start retransmission */
|
||||
h.s.p_f = 1; /* XXX Should it always be set to 1? XXX */
|
||||
h.s.p_f = pf;
|
||||
switch(pri->localtype) {
|
||||
case PRI_NETWORK:
|
||||
h.h.c_r = 0;
|
||||
@@ -217,15 +256,16 @@ static void q921_reject(struct pri *pri)
|
||||
h.h.c_r = 1;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Don't know how to U/A on a type %d node\n", pri->localtype);
|
||||
pri_error("Don't know how to U/A on a type %d node\n", pri->localtype);
|
||||
return;
|
||||
}
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("Sending Reject (%d)\n", pri->v_r);
|
||||
pri_message("Sending Reject (%d)\n", pri->v_r);
|
||||
pri->sentrej = 1;
|
||||
q921_transmit(pri, &h, 4);
|
||||
}
|
||||
|
||||
static void q921_rr(struct pri *pri, int pbit) {
|
||||
static void q921_rr(struct pri *pri, int pbit, int cmd) {
|
||||
q921_h h;
|
||||
Q921_INIT(h);
|
||||
h.s.x0 = 0; /* Always 0 */
|
||||
@@ -235,30 +275,34 @@ static void q921_rr(struct pri *pri, int pbit) {
|
||||
h.s.p_f = pbit; /* Poll/Final set appropriately */
|
||||
switch(pri->localtype) {
|
||||
case PRI_NETWORK:
|
||||
h.h.c_r = 0;
|
||||
if (cmd)
|
||||
h.h.c_r = 1;
|
||||
else
|
||||
h.h.c_r = 0;
|
||||
break;
|
||||
case PRI_CPE:
|
||||
h.h.c_r = 1;
|
||||
if (cmd)
|
||||
h.h.c_r = 0;
|
||||
else
|
||||
h.h.c_r = 1;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Don't know how to U/A on a type %d node\n", pri->localtype);
|
||||
pri_error("Don't know how to U/A on a type %d node\n", pri->localtype);
|
||||
return;
|
||||
}
|
||||
pri->v_na = pri->v_r; /* Make a note that we've already acked this */
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("Sending Receiver Ready (%d)\n", pri->v_r);
|
||||
pri_message("Sending Receiver Ready (%d)\n", pri->v_r);
|
||||
q921_transmit(pri, &h, 4);
|
||||
}
|
||||
|
||||
static pri_event *q921_dchannel_down(struct pri *pri);
|
||||
|
||||
static void t200_expire(void *vpri)
|
||||
{
|
||||
struct pri *pri = vpri;
|
||||
if (pri->txqueue) {
|
||||
/* Retransmit first packet in the queue, setting the poll bit */
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- T200 counter expired, What to do...\n");
|
||||
pri_message("-- T200 counter expired, What to do...\n");
|
||||
/* Force Poll bit */
|
||||
pri->txqueue->h.p_f = 1;
|
||||
/* Update nr */
|
||||
@@ -270,22 +314,25 @@ static void t200_expire(void *vpri)
|
||||
if (pri->retrans < N_200) {
|
||||
/* Reschedule t200_timer */
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- Retransmitting %d bytes\n", pri->txqueue->len);
|
||||
q921_transmit(pri, (q921_h *)&pri->txqueue->h, pri->txqueue->len);
|
||||
pri_message("-- Retransmitting %d bytes\n", pri->txqueue->len);
|
||||
if (pri->busy)
|
||||
q921_rr(pri, 1, 0);
|
||||
else
|
||||
q921_transmit(pri, (q921_h *)&pri->txqueue->h, pri->txqueue->len);
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- Rescheduling retransmission (%d)\n", pri->retrans);
|
||||
pri_message("-- Rescheduling retransmission (%d)\n", pri->retrans);
|
||||
pri->t200_timer = pri_schedule_event(pri, T_200, t200_expire, pri);
|
||||
} else {
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- Timeout occured, restarting PRI\n");
|
||||
pri->state = Q921_LINK_CONNECTION_RELEASED;
|
||||
pri_message("-- Timeout occured, restarting PRI\n");
|
||||
pri->q921_state = Q921_LINK_CONNECTION_RELEASED;
|
||||
pri->t200_timer = 0;
|
||||
q921_dchannel_down(pri);
|
||||
q921_start(pri);
|
||||
q921_start(pri, 1);
|
||||
pri->schedev = 1;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "T200 counter expired, nothing to send...\n");
|
||||
pri_error("T200 counter expired, nothing to send...\n");
|
||||
pri->t200_timer = 0;
|
||||
}
|
||||
}
|
||||
@@ -295,6 +342,7 @@ int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr)
|
||||
q921_frame *f, *prev=NULL;
|
||||
for (f=pri->txqueue; f; f = f->next) prev = f;
|
||||
f = malloc(sizeof(q921_frame) + len + 2);
|
||||
memset(f,0,sizeof(q921_frame) + len + 2);
|
||||
if (f) {
|
||||
Q921_INIT(f->h);
|
||||
switch(pri->localtype) {
|
||||
@@ -325,24 +373,25 @@ int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr)
|
||||
else
|
||||
pri->txqueue = f;
|
||||
/* Immediately transmit unless we're in a recovery state */
|
||||
if (!pri->retrans)
|
||||
if (!pri->retrans && !pri->busy) {
|
||||
q921_transmit(pri, (q921_h *)(&f->h), f->len);
|
||||
}
|
||||
if (pri->t203_timer) {
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("Stopping T_203 timer\n");
|
||||
pri_message("Stopping T_203 timer\n");
|
||||
pri_schedule_del(pri, pri->t203_timer);
|
||||
pri->t203_timer = 0;
|
||||
}
|
||||
if (!pri->t200_timer) {
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("Starting T_200 timer\n");
|
||||
pri_message("Starting T_200 timer\n");
|
||||
pri->t200_timer = pri_schedule_event(pri, T_200, t200_expire, pri);
|
||||
} else
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("T_200 timer already going (%d)\n", pri->t200_timer);
|
||||
pri_message("T_200 timer already going (%d)\n", pri->t200_timer);
|
||||
|
||||
} else {
|
||||
fprintf(stderr, "!! Out of memory for Q.921 transmit\n");
|
||||
pri_error("!! Out of memory for Q.921 transmit\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
@@ -351,47 +400,57 @@ int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr)
|
||||
static void t203_expire(void *vpri)
|
||||
{
|
||||
struct pri *pri = vpri;
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("T203 counter expired, sending RR and scheduling T203 again\n");
|
||||
/* Solicit an F-bit in the other's RR */
|
||||
pri->solicitfbit = 1;
|
||||
q921_rr(pri, 1);
|
||||
/* Restart ourselves */
|
||||
pri->t203_timer = pri_schedule_event(pri, T_203, t203_expire, pri);
|
||||
if (pri->q921_state == Q921_LINK_CONNECTION_ESTABLISHED) {
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
pri_message("T203 counter expired, sending RR and scheduling T203 again\n");
|
||||
/* Solicit an F-bit in the other's RR */
|
||||
pri->solicitfbit = 1;
|
||||
q921_rr(pri, 1, 1);
|
||||
/* Restart ourselves */
|
||||
pri->t203_timer = pri_schedule_event(pri, T_203, t203_expire, pri);
|
||||
} else {
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
pri_message("T203 counter expired in weird statd %d\n", pri->q921_state);
|
||||
pri->t203_timer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static pri_event *q921_handle_iframe(struct pri *pri, q921_i *i, int len)
|
||||
{
|
||||
int res;
|
||||
pri_event *ev;
|
||||
/* Make sure this is a valid packet */
|
||||
if (i->n_s == pri->v_r) {
|
||||
/* Increment next expected I-frame */
|
||||
Q921_INC(pri->v_r);
|
||||
/* Handle their ACK */
|
||||
q921_ack_rx(pri, i->n_r);
|
||||
pri->sentrej = 0;
|
||||
ev = q921_ack_rx(pri, i->n_r);
|
||||
if (ev)
|
||||
return ev;
|
||||
if (i->p_f) {
|
||||
/* If the Poll/Final bit is set, immediate send the RR */
|
||||
q921_rr(pri, 1);
|
||||
q921_rr(pri, 1, 0);
|
||||
} else if (pri->busy) {
|
||||
q921_rr(pri, 0, 0);
|
||||
}
|
||||
/* Receive Q.931 data */
|
||||
res = q931_receive(pri, (q931_h *)i->data, len - 4);
|
||||
/* Send an RR if one wasn't sent already */
|
||||
if (pri->v_na != pri->v_r)
|
||||
q921_rr(pri, 0);
|
||||
q921_rr(pri, 0, 0);
|
||||
if (res == -1) {
|
||||
return NULL;
|
||||
}
|
||||
if (res & Q931_RES_HAVEEVENT)
|
||||
return &pri->ev;
|
||||
} else {
|
||||
if (((pri->v_r - i->n_s) & 127) < pri->window) {
|
||||
/* It's within our window -- send back an RR */
|
||||
q921_rr(pri, 0);
|
||||
} else
|
||||
q921_reject(pri);
|
||||
#if 0
|
||||
q931_reject(pri);
|
||||
#endif
|
||||
/* If we haven't already sent a reject, send it now, otherwise
|
||||
we are obliged to RR */
|
||||
if (!pri->sentrej)
|
||||
q921_reject(pri, i->p_f);
|
||||
else if (i->p_f)
|
||||
q921_rr(pri, 1, 0);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -404,26 +463,26 @@ void q921_dump(q921_h *h, int len, int showraw, int txrx)
|
||||
|
||||
direction_tag = txrx ? '>' : '<';
|
||||
if (showraw) {
|
||||
printf("\n%c [", direction_tag);
|
||||
pri_message("\n%c [", direction_tag);
|
||||
for (x=0;x<len;x++)
|
||||
printf("%02x ",h->raw[x]);
|
||||
printf("]");
|
||||
pri_message("%02x ",h->raw[x]);
|
||||
pri_message("]");
|
||||
}
|
||||
|
||||
switch (h->h.data[0] & Q921_FRAMETYPE_MASK) {
|
||||
case 0:
|
||||
case 2:
|
||||
printf("\n%c Informational frame:\n", direction_tag);
|
||||
pri_message("\n%c Informational frame:\n", direction_tag);
|
||||
break;
|
||||
case 1:
|
||||
printf("\n%c Supervisory frame:\n", direction_tag);
|
||||
pri_message("\n%c Supervisory frame:\n", direction_tag);
|
||||
break;
|
||||
case 3:
|
||||
printf("\n%c Unnumbered frame:\n", direction_tag);
|
||||
pri_message("\n%c Unnumbered frame:\n", direction_tag);
|
||||
break;
|
||||
}
|
||||
|
||||
printf(
|
||||
pri_message(
|
||||
"%c SAPI: %02d C/R: %d EA: %d\n"
|
||||
"%c TEI: %03d EA: %d\n",
|
||||
direction_tag,
|
||||
@@ -437,7 +496,7 @@ void q921_dump(q921_h *h, int len, int showraw, int txrx)
|
||||
case 0:
|
||||
case 2:
|
||||
/* Informational frame */
|
||||
printf(
|
||||
pri_message(
|
||||
"%c N(S): %03d 0: %d\n"
|
||||
"%c N(R): %03d P: %d\n"
|
||||
"%c %d bytes of data\n",
|
||||
@@ -464,7 +523,7 @@ void q921_dump(q921_h *h, int len, int showraw, int txrx)
|
||||
type = "REJ (reject)";
|
||||
break;
|
||||
}
|
||||
printf(
|
||||
pri_message(
|
||||
"%c Zero: %d S: %d 01: %d [ %s ]\n"
|
||||
"%c N(R): %03d P/F: %d\n"
|
||||
"%c %d bytes of data\n",
|
||||
@@ -510,7 +569,7 @@ void q921_dump(q921_h *h, int len, int showraw, int txrx)
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf(
|
||||
pri_message(
|
||||
"%c M3: %d P/F: %d M2: %d 11: %d [ %s ]\n"
|
||||
"%c %d bytes of data\n",
|
||||
direction_tag,
|
||||
@@ -534,6 +593,9 @@ static pri_event *q921_dchannel_up(struct pri *pri)
|
||||
pri_schedule_del(pri, pri->sabme_timer);
|
||||
pri->sabme_timer = 0;
|
||||
|
||||
/* Reset any rejects */
|
||||
pri->sentrej = 0;
|
||||
|
||||
/* Go into connection established state */
|
||||
pri->q921_state = Q921_LINK_CONNECTION_ESTABLISHED;
|
||||
|
||||
@@ -573,13 +635,17 @@ void q921_reset(struct pri *pri)
|
||||
pri->solicitfbit = 0;
|
||||
pri->q921_state = Q921_LINK_CONNECTION_RELEASED;
|
||||
pri->retrans = 0;
|
||||
pri->sentrej = 0;
|
||||
|
||||
/* Discard anything waiting to go out */
|
||||
q921_discard_retransmissions(pri);
|
||||
}
|
||||
|
||||
pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
|
||||
static pri_event *__q921_receive(struct pri *pri, q921_h *h, int len)
|
||||
{
|
||||
q921_frame *f;
|
||||
pri_event *ev;
|
||||
int sendnow;
|
||||
/* Discard FCS */
|
||||
len -= 2;
|
||||
|
||||
@@ -602,23 +668,23 @@ pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
|
||||
case 0:
|
||||
case 2:
|
||||
if (pri->q921_state != Q921_LINK_CONNECTION_ESTABLISHED) {
|
||||
fprintf(stderr, "!! Got I-frame while link state %d\n", pri->q921_state);
|
||||
pri_error("!! Got I-frame while link state %d\n", pri->q921_state);
|
||||
return NULL;
|
||||
}
|
||||
/* Informational frame */
|
||||
if (len < 4) {
|
||||
fprintf(stderr, "!! Received short I-frame\n");
|
||||
pri_error("!! Received short I-frame (expected 4, got %d)\n", len);
|
||||
break;
|
||||
}
|
||||
return q921_handle_iframe(pri, &h->i, len);
|
||||
break;
|
||||
case 1:
|
||||
if (pri->q921_state != Q921_LINK_CONNECTION_ESTABLISHED) {
|
||||
fprintf(stderr, "!! Got S-frame while link down\n");
|
||||
pri_error("!! Got S-frame while link down\n");
|
||||
return NULL;
|
||||
}
|
||||
if (len < 4) {
|
||||
fprintf(stderr, "!! Received short S-frame\n");
|
||||
pri_error("!! Received short S-frame (expected 4, got %d)\n", len);
|
||||
break;
|
||||
}
|
||||
switch(h->s.ss) {
|
||||
@@ -626,54 +692,60 @@ pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
|
||||
/* Receiver Ready */
|
||||
pri->busy = 0;
|
||||
/* Acknowledge frames as necessary */
|
||||
q921_ack_rx(pri, h->s.n_r);
|
||||
ev = q921_ack_rx(pri, h->s.n_r);
|
||||
if (ev)
|
||||
return ev;
|
||||
if (h->s.p_f) {
|
||||
/* If it's a p/f one then send back a RR in return with the p/f bit set */
|
||||
if (pri->solicitfbit) {
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- Got RR response to our frame\n");
|
||||
pri_message("-- Got RR response to our frame\n");
|
||||
} else {
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- Unsolicited RR with P/F bit, responding\n");
|
||||
q921_rr(pri, 1);
|
||||
pri_message("-- Unsolicited RR with P/F bit, responding\n");
|
||||
q921_rr(pri, 1, 0);
|
||||
}
|
||||
pri->solicitfbit = 0;
|
||||
}
|
||||
break;
|
||||
#if 0
|
||||
case 1:
|
||||
/* Receiver not ready */
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- Got receiver not ready\n");
|
||||
pri_message("-- Got receiver not ready\n");
|
||||
if(h->s.p_f) {
|
||||
/* Send RR if poll bit set */
|
||||
q921_rr(pri, h->s.p_f, 0);
|
||||
}
|
||||
pri->busy = 1;
|
||||
break;
|
||||
#endif
|
||||
case 2:
|
||||
/* Just retransmit */
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- Got reject requesting packet %d... Retransmitting.\n", h->s.n_r);
|
||||
pri_message("-- Got reject requesting packet %d... Retransmitting.\n", h->s.n_r);
|
||||
if (h->s.p_f) {
|
||||
/* If it has the poll bit set, send an appropriate supervisory response */
|
||||
q921_rr(pri, 1);
|
||||
q921_rr(pri, 1, 0);
|
||||
}
|
||||
sendnow = 0;
|
||||
/* Resend the proper I-frame */
|
||||
for(f=pri->txqueue;f;f=f->next) {
|
||||
if (f->h.n_s == h->s.n_r) {
|
||||
/* Matches the request */
|
||||
break;
|
||||
if (sendnow || (f->h.n_s == h->s.n_r)) {
|
||||
/* Matches the request, or follows in our window */
|
||||
sendnow = 1;
|
||||
pri_error("!! Got reject for frame %d, retransmitting frame %d now, updating n_r!\n", h->s.n_r, f->h.n_s);
|
||||
f->h.n_r = pri->v_r;
|
||||
q921_transmit(pri, (q921_h *)(&f->h), f->len);
|
||||
}
|
||||
}
|
||||
if (f) {
|
||||
/* Retransmit the requested frame */
|
||||
q921_transmit(pri, (q921_h *)(&f->h), f->len);
|
||||
} else {
|
||||
if (!sendnow) {
|
||||
if (pri->txqueue) {
|
||||
/* This should never happen */
|
||||
if (!h->s.p_f || h->s.n_r) {
|
||||
fprintf(stderr, "!! Got reject for frame %d, but we only have others!\n", h->s.n_r);
|
||||
pri_error("!! Got reject for frame %d, but we only have others!\n", h->s.n_r);
|
||||
}
|
||||
} else {
|
||||
/* Hrm, we have nothing to send, but have been REJ'd. Reset v_a, v_s, etc */
|
||||
pri_error("!! Got reject for frame %d, but we have nothing -- resetting!\n", h->s.n_r);
|
||||
pri->v_a = h->s.n_r;
|
||||
pri->v_s = h->s.n_r;
|
||||
/* Reset t200 timer if it was somehow going */
|
||||
@@ -689,46 +761,54 @@ pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "!! 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,
|
||||
pri_error("!! 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,
|
||||
pri->v_s, pri->v_a);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (len < 3) {
|
||||
fprintf(stderr, "!! Received short unnumbered frame\n");
|
||||
pri_error("!! Received short unnumbered frame\n");
|
||||
break;
|
||||
}
|
||||
switch(h->u.m3) {
|
||||
case 0:
|
||||
if (h->u.m2 == 3) {
|
||||
if (h->u.p_f) {
|
||||
/* Section 5.7.1 says we should restart on receiving a DM response with the f-bit set to
|
||||
one, but we wait T200 first */
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- Got Unconnected Mode from peer.\n");
|
||||
/* Disconnected mode */
|
||||
if (pri->q921_state != Q921_LINK_CONNECTION_RELEASED)
|
||||
return q921_dchannel_down(pri);
|
||||
pri_message("-- Got DM Mode from peer.\n");
|
||||
/* Disconnected mode, try again after T200 */
|
||||
ev = q921_dchannel_down(pri);
|
||||
q921_start(pri, 0);
|
||||
return ev;
|
||||
|
||||
} else {
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- DM requesting SABME, starting.\n");
|
||||
pri_message("-- Ignoring unsolicited DM with p/f set to 0\n");
|
||||
#if 0
|
||||
/* Requesting that we start */
|
||||
q921_start(pri);
|
||||
q921_start(pri, 0);
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
} else if (!h->u.m2) {
|
||||
printf("XXX Unnumbered Information not implemented XXX\n");
|
||||
pri_message("XXX Unnumbered Information not implemented XXX\n");
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE)
|
||||
printf("-- Got Disconnect from peer.\n");
|
||||
pri_message("-- Got Disconnect from peer.\n");
|
||||
/* Acknowledge */
|
||||
q921_send_ua(pri, h->u.p_f);
|
||||
return q921_dchannel_down(pri);
|
||||
ev = q921_dchannel_down(pri);
|
||||
q921_start(pri, 0);
|
||||
return ev;
|
||||
case 3:
|
||||
if (h->u.m2 == 3) {
|
||||
/* SABME */
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE) {
|
||||
printf("-- Got SABME from %s peer.\n", h->h.c_r ? "network" : "cpe");
|
||||
pri_message("-- Got SABME from %s peer.\n", h->h.c_r ? "network" : "cpe");
|
||||
}
|
||||
if (h->h.c_r) {
|
||||
pri->remotetype = PRI_NETWORK;
|
||||
@@ -750,22 +830,22 @@ pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
|
||||
/* It's a UA */
|
||||
if (pri->q921_state == Q921_AWAITING_ESTABLISH) {
|
||||
if (pri->debug & PRI_DEBUG_Q921_STATE) {
|
||||
printf("-- Got UA from %s peer Link up.\n", h->h.c_r ? "cpe" : "network");
|
||||
pri_message("-- Got UA from %s peer Link up.\n", h->h.c_r ? "cpe" : "network");
|
||||
}
|
||||
return q921_dchannel_up(pri);
|
||||
} else
|
||||
fprintf(stderr, "!! Got a UA, but i'm in state %d\n", pri->q921_state);
|
||||
pri_error("!! Got a UA, but i'm in state %d\n", pri->q921_state);
|
||||
} else
|
||||
fprintf(stderr, "!! Weird frame received (m3=3, m2 = %d)\n", h->u.m2);
|
||||
pri_error("!! Weird frame received (m3=3, m2 = %d)\n", h->u.m2);
|
||||
break;
|
||||
case 4:
|
||||
fprintf(stderr, "!! Frame got rejected!\n");
|
||||
pri_error("!! Frame got rejected!\n");
|
||||
break;
|
||||
case 5:
|
||||
fprintf(stderr, "!! XID frames not supported\n");
|
||||
pri_error("!! XID frames not supported\n");
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "!! Don't know what to do with M3=%d u-frames\n", h->u.m3);
|
||||
pri_error("!! Don't know what to do with M3=%d u-frames\n", h->u.m3);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -773,14 +853,22 @@ pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void q921_start(struct pri *pri)
|
||||
pri_event *q921_receive(struct pri *pri, q921_h *h, int len)
|
||||
{
|
||||
pri_event *e;
|
||||
e = __q921_receive(pri, h, len);
|
||||
reschedule_t203(pri);
|
||||
return e;
|
||||
}
|
||||
|
||||
void q921_start(struct pri *pri, int now)
|
||||
{
|
||||
if (pri->q921_state != Q921_LINK_CONNECTION_RELEASED) {
|
||||
fprintf(stderr, "!! q921_start: Not in 'Link Connection Released' state\n");
|
||||
pri_error("!! q921_start: Not in 'Link Connection Released' state\n");
|
||||
return;
|
||||
}
|
||||
/* Reset our interface */
|
||||
q921_reset(pri);
|
||||
/* Do the SABME XXX Maybe we should implement T_WAIT? XXX */
|
||||
q921_send_sabme(pri);
|
||||
q921_send_sabme(pri, now);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user