~ubuntu-branches/ubuntu/trusty/libpri/trusty

« back to all changes in this revision

Viewing changes to .pc/fix-inband-bri/pri.c

  • Committer: Bazaar Package Importer
  • Author(s): Tzafrir Cohen
  • Date: 2010-06-08 21:56:54 UTC
  • mfrom: (1.3.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100608215654-nbofb34003evshln
Tags: 1.4.11.2-1
* New upstream bugfix release.
- Patch fix-inband-bri removed: merged upstream.
* Update my address to @debian.org.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * libpri: An implementation of Primary Rate ISDN
3
 
 *
4
 
 * Written by Mark Spencer <markster@digium.com>
5
 
 *
6
 
 * Copyright (C) 2001-2005, Digium, Inc.
7
 
 * All Rights Reserved.
8
 
 */
9
 
 
10
 
/*
11
 
 * See http://www.asterisk.org for more information about
12
 
 * the Asterisk project. Please do not directly contact
13
 
 * any of the maintainers of this project for assistance;
14
 
 * the project provides a web site, mailing lists and IRC
15
 
 * channels for your use.
16
 
 *
17
 
 * This program is free software, distributed under the terms of
18
 
 * the GNU General Public License Version 2 as published by the
19
 
 * Free Software Foundation. See the LICENSE file included with
20
 
 * this program for more details.
21
 
 *
22
 
 * In addition, when this program is distributed with Asterisk in
23
 
 * any form that would qualify as a 'combined work' or as a
24
 
 * 'derivative work' (but not mere aggregation), you can redistribute
25
 
 * and/or modify the combination under the terms of the license
26
 
 * provided with that copy of Asterisk, instead of the license
27
 
 * terms granted here.
28
 
 */
29
 
 
30
 
#include <unistd.h>
31
 
#include <stdlib.h>
32
 
#include <string.h>
33
 
#include <errno.h>
34
 
#include <stdio.h>
35
 
#include <unistd.h>
36
 
#include <stdlib.h>
37
 
#include <sys/select.h>
38
 
#include <stdarg.h>
39
 
#include "compat.h"
40
 
#include "libpri.h"
41
 
#include "pri_internal.h"
42
 
#include "pri_facility.h"
43
 
#include "pri_q921.h"
44
 
#include "pri_q931.h"
45
 
 
46
 
#define PRI_BIT(a_bit)          (1UL << (a_bit))
47
 
#define PRI_ALL_SWITCHES        0xFFFFFFFF
48
 
 
49
 
struct pri_timer_table {
50
 
        const char *name;
51
 
        enum PRI_TIMERS_AND_COUNTERS number;
52
 
        unsigned long used_by;
53
 
};
54
 
 
55
 
/*!
56
 
 * \note Sort the timer table entries in the order of the timer name so
57
 
 * pri_dump_info_str() can display them in a consistent order.
58
 
 */
59
 
static const struct pri_timer_table pri_timer[] = {
60
 
/* *INDENT-OFF* */
61
 
        /* timer name       timer number                used by switches */
62
 
        { "N200",           PRI_TIMER_N200,             PRI_ALL_SWITCHES },
63
 
        { "N201",           PRI_TIMER_N201,             PRI_ALL_SWITCHES },
64
 
        { "N202",           PRI_TIMER_N202,             PRI_ALL_SWITCHES },
65
 
        { "K",              PRI_TIMER_K,                PRI_ALL_SWITCHES },
66
 
        { "T200",           PRI_TIMER_T200,             PRI_ALL_SWITCHES },
67
 
        { "T202",           PRI_TIMER_T202,             PRI_ALL_SWITCHES },
68
 
        { "T203",           PRI_TIMER_T203,             PRI_ALL_SWITCHES },
69
 
        { "T300",           PRI_TIMER_T300,             PRI_ALL_SWITCHES },
70
 
        { "T301",           PRI_TIMER_T301,             PRI_ALL_SWITCHES },
71
 
        { "T302",           PRI_TIMER_T302,             PRI_ALL_SWITCHES },
72
 
        { "T303",           PRI_TIMER_T303,             PRI_ALL_SWITCHES },
73
 
        { "T304",           PRI_TIMER_T304,             PRI_ALL_SWITCHES },
74
 
        { "T305",           PRI_TIMER_T305,             PRI_ALL_SWITCHES },
75
 
        { "T306",           PRI_TIMER_T306,             PRI_ALL_SWITCHES },
76
 
        { "T307",           PRI_TIMER_T307,             PRI_ALL_SWITCHES },
77
 
        { "T308",           PRI_TIMER_T308,             PRI_ALL_SWITCHES },
78
 
        { "T309",           PRI_TIMER_T309,             PRI_ALL_SWITCHES },
79
 
        { "T310",           PRI_TIMER_T310,             PRI_ALL_SWITCHES },
80
 
        { "T313",           PRI_TIMER_T313,             PRI_ALL_SWITCHES },
81
 
        { "T314",           PRI_TIMER_T314,             PRI_ALL_SWITCHES },
82
 
        { "T316",           PRI_TIMER_T316,             PRI_ALL_SWITCHES },
83
 
        { "T317",           PRI_TIMER_T317,             PRI_ALL_SWITCHES },
84
 
        { "T318",           PRI_TIMER_T318,             PRI_ALL_SWITCHES },
85
 
        { "T319",           PRI_TIMER_T319,             PRI_ALL_SWITCHES },
86
 
        { "T320",           PRI_TIMER_T320,             PRI_ALL_SWITCHES },
87
 
        { "T321",           PRI_TIMER_T321,             PRI_ALL_SWITCHES },
88
 
        { "T322",           PRI_TIMER_T322,             PRI_ALL_SWITCHES },
89
 
        { "T-HOLD",         PRI_TIMER_T_HOLD,           PRI_ALL_SWITCHES },
90
 
        { "T-RETRIEVE",     PRI_TIMER_T_RETRIEVE,       PRI_ALL_SWITCHES },
91
 
        { "T-RESPONSE",     PRI_TIMER_T_RESPONSE,       PRI_ALL_SWITCHES },
92
 
/* *INDENT-ON* */
93
 
};
94
 
 
95
 
char *pri_node2str(int node)
96
 
{
97
 
        switch(node) {
98
 
        case PRI_UNKNOWN:
99
 
                return "Unknown node type";
100
 
        case PRI_NETWORK:
101
 
                return "Network";
102
 
        case PRI_CPE:
103
 
                return "CPE";
104
 
        default:
105
 
                return "Invalid value";
106
 
        }
107
 
}
108
 
 
109
 
char *pri_switch2str(int sw)
110
 
{
111
 
        switch(sw) {
112
 
        case PRI_SWITCH_NI2:
113
 
                return "National ISDN";
114
 
        case PRI_SWITCH_DMS100:
115
 
                return "Nortel DMS100";
116
 
        case PRI_SWITCH_LUCENT5E:
117
 
                return "Lucent 5E";
118
 
        case PRI_SWITCH_ATT4ESS:
119
 
                return "AT&T 4ESS";
120
 
        case PRI_SWITCH_NI1:
121
 
                return "National ISDN 1";
122
 
        case PRI_SWITCH_EUROISDN_E1:
123
 
                return "EuroISDN";
124
 
        case PRI_SWITCH_GR303_EOC:
125
 
                return "GR303 EOC";
126
 
        case PRI_SWITCH_GR303_TMC:
127
 
                return "GR303 TMC";
128
 
        case PRI_SWITCH_QSIG:
129
 
                return "Q.SIG switch";
130
 
        default:
131
 
                return "Unknown switchtype";
132
 
        }
133
 
}
134
 
 
135
 
static void pri_default_timers(struct pri *ctrl, int switchtype)
136
 
{
137
 
        unsigned idx;
138
 
 
139
 
        /* Initialize all timers/counters to unsupported/disabled. */
140
 
        for (idx = 0; idx < PRI_MAX_TIMERS; ++idx) {
141
 
                ctrl->timers[idx] = -1;
142
 
        }
143
 
 
144
 
        /* Set timer values to standard defaults.  Time is in ms. */
145
 
        ctrl->timers[PRI_TIMER_N200] = 3;                       /* Max numer of Q.921 retransmissions */
146
 
        ctrl->timers[PRI_TIMER_N202] = 3;                       /* Max numer of transmissions of the TEI identity request message */
147
 
 
148
 
        if (ctrl->bri == 1)
149
 
                ctrl->timers[PRI_TIMER_K] = 1;                          /* Max number of outstanding I-frames */
150
 
        else
151
 
                ctrl->timers[PRI_TIMER_K] = 7;                          /* Max number of outstanding I-frames */
152
 
 
153
 
        ctrl->timers[PRI_TIMER_T200] = 1000;            /* Time between SABME's */
154
 
        ctrl->timers[PRI_TIMER_T202] = 10 * 1000;       /* Min time between transmission of TEI Identity request messages */
155
 
        ctrl->timers[PRI_TIMER_T203] = 10 * 1000;       /* Max time without exchanging packets */
156
 
        ctrl->timers[PRI_TIMER_T305] = 30 * 1000;       /* Wait for DISCONNECT acknowledge */
157
 
        ctrl->timers[PRI_TIMER_T308] = 4 * 1000;        /* Wait for RELEASE acknowledge */
158
 
        ctrl->timers[PRI_TIMER_T313] = 4 * 1000;        /* Wait for CONNECT acknowledge, CPE side only */
159
 
        ctrl->timers[PRI_TIMER_TM20] = 2500;            /* Max time awaiting XID response - Q.921 Appendix IV */
160
 
        ctrl->timers[PRI_TIMER_NM20] = 3;                       /* Number of XID retransmits - Q.921 Appendix IV */
161
 
        ctrl->timers[PRI_TIMER_T303] = 4 * 1000;                        /* Length between SETUP retransmissions and timeout */
162
 
        ctrl->timers[PRI_TIMER_T309] = 6000;            /* Time to wait before clearing calls in case of D-channel transient event.  Q.931 specifies 6-90 seconds */
163
 
 
164
 
        ctrl->timers[PRI_TIMER_T_HOLD] = 4 * 1000;      /* Wait for HOLD request response. */
165
 
        ctrl->timers[PRI_TIMER_T_RETRIEVE] = 4 * 1000;/* Wait for RETRIEVE request response. */
166
 
 
167
 
        ctrl->timers[PRI_TIMER_T_RESPONSE] = 4 * 1000;  /* Maximum time to wait for a typical APDU response. */
168
 
 
169
 
        /* Set any switch specific override default values */
170
 
        switch (switchtype) {
171
 
        default:
172
 
                break;
173
 
        }
174
 
}
175
 
 
176
 
int pri_set_timer(struct pri *pri, int timer, int value)
177
 
{
178
 
        if (timer < 0 || timer > PRI_MAX_TIMERS || value < 0)
179
 
                return -1;
180
 
 
181
 
        pri->timers[timer] = value;
182
 
        return 0;
183
 
}
184
 
 
185
 
int pri_get_timer(struct pri *pri, int timer)
186
 
{
187
 
        if (timer < 0 || timer > PRI_MAX_TIMERS)
188
 
                return -1;
189
 
        return pri->timers[timer];
190
 
}
191
 
 
192
 
int pri_set_service_message_support(struct pri *pri, int supportflag)
193
 
{
194
 
        if (!pri) {
195
 
                return -1;
196
 
        }
197
 
        pri->service_message_support = supportflag ? 1 : 0;
198
 
        return 0;
199
 
}
200
 
 
201
 
int pri_timer2idx(const char *timer_name)
202
 
{
203
 
        unsigned idx;
204
 
        enum PRI_TIMERS_AND_COUNTERS timer_number;
205
 
 
206
 
        timer_number = -1;
207
 
        for (idx = 0; idx < ARRAY_LEN(pri_timer); ++idx) {
208
 
                if (!strcasecmp(timer_name, pri_timer[idx].name)) {
209
 
                        timer_number = pri_timer[idx].number;
210
 
                        break;
211
 
                }
212
 
        }
213
 
        return timer_number;
214
 
}
215
 
 
216
 
static int __pri_read(struct pri *pri, void *buf, int buflen)
217
 
{
218
 
        int res = read(pri->fd, buf, buflen);
219
 
        if (res < 0) {
220
 
                if (errno != EAGAIN)
221
 
                        pri_error(pri, "Read on %d failed: %s\n", pri->fd, strerror(errno));
222
 
                return 0;
223
 
        }
224
 
        return res;
225
 
}
226
 
 
227
 
static int __pri_write(struct pri *pri, void *buf, int buflen)
228
 
{
229
 
        int res = write(pri->fd, buf, buflen);
230
 
        if (res < 0) {
231
 
                if (errno != EAGAIN)
232
 
                        pri_error(pri, "Write to %d failed: %s\n", pri->fd, strerror(errno));
233
 
                return 0;
234
 
        }
235
 
        return res;
236
 
}
237
 
 
238
 
void __pri_free_tei(struct pri * p)
239
 
{
240
 
        if (p) {
241
 
                struct q931_call *call;
242
 
 
243
 
                call = p->dummy_call;
244
 
                if (call) {
245
 
                        pri_schedule_del(call->pri, call->retranstimer);
246
 
                        pri_call_apdu_queue_cleanup(call);
247
 
                }
248
 
                free(p->msg_line);
249
 
                free(p);
250
 
        }
251
 
}
252
 
 
253
 
struct pri *__pri_new_tei(int fd, int node, int switchtype, struct pri *master, pri_io_cb rd, pri_io_cb wr, void *userdata, int tei, int bri)
254
 
{
255
 
        struct d_ctrl_dummy *dummy_ctrl;
256
 
        struct pri *p;
257
 
 
258
 
        switch (switchtype) {
259
 
        case PRI_SWITCH_GR303_EOC:
260
 
        case PRI_SWITCH_GR303_TMC:
261
 
        case PRI_SWITCH_GR303_TMC_SWITCHING:
262
 
        case PRI_SWITCH_GR303_EOC_PATH:
263
 
                p = calloc(1, sizeof(*p));
264
 
                if (!p) {
265
 
                        return NULL;
266
 
                }
267
 
                dummy_ctrl = NULL;
268
 
                break;
269
 
        default:
270
 
                dummy_ctrl = calloc(1, sizeof(*dummy_ctrl));
271
 
                if (!dummy_ctrl) {
272
 
                        return NULL;
273
 
                }
274
 
                p = &dummy_ctrl->ctrl;
275
 
                break;
276
 
        }
277
 
        if (!master) {
278
 
                /* This is the master record. */
279
 
                p->msg_line = calloc(1, sizeof(*p->msg_line));
280
 
                if (!p->msg_line) {
281
 
                        free(p);
282
 
                        return NULL;
283
 
                }
284
 
        }
285
 
 
286
 
        p->bri = bri;
287
 
        p->fd = fd;
288
 
        p->read_func = rd;
289
 
        p->write_func = wr;
290
 
        p->userdata = userdata;
291
 
        p->localtype = node;
292
 
        p->switchtype = switchtype;
293
 
        p->cref = 1;
294
 
        p->sapi = (tei == Q921_TEI_GROUP) ? Q921_SAPI_LAYER2_MANAGEMENT : Q921_SAPI_CALL_CTRL;
295
 
        p->tei = tei;
296
 
        p->nsf = PRI_NSF_NONE;
297
 
        p->protodisc = Q931_PROTOCOL_DISCRIMINATOR;
298
 
        p->master = master;
299
 
        p->callpool = &p->localpool;
300
 
        pri_default_timers(p, switchtype);
301
 
        if (master) {
302
 
                pri_set_debug(p, master->debug);
303
 
                if (master->sendfacility)
304
 
                        pri_facility_enable(p);
305
 
        }
306
 
#ifdef LIBPRI_COUNTERS
307
 
        p->q921_rxcount = 0;
308
 
        p->q921_txcount = 0;
309
 
        p->q931_rxcount = 0;
310
 
        p->q931_txcount = 0;
311
 
#endif
312
 
        if (dummy_ctrl) {
313
 
                /* Initialize the dummy call reference call record. */
314
 
                dummy_ctrl->ctrl.dummy_call = &dummy_ctrl->dummy_call;
315
 
                q931_init_call_record(&dummy_ctrl->ctrl, dummy_ctrl->ctrl.dummy_call,
316
 
                        Q931_DUMMY_CALL_REFERENCE);
317
 
        }
318
 
        switch (switchtype) {
319
 
        case PRI_SWITCH_GR303_EOC:
320
 
                p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
321
 
                p->sapi = Q921_SAPI_GR303_EOC;
322
 
                p->tei = Q921_TEI_GR303_EOC_OPS;
323
 
                p->subchannel = __pri_new_tei(-1, node, PRI_SWITCH_GR303_EOC_PATH, p, NULL, NULL, NULL, Q921_TEI_GR303_EOC_PATH, 0);
324
 
                if (!p->subchannel) {
325
 
                        free(p);
326
 
                        p = NULL;
327
 
                }
328
 
                break;
329
 
        case PRI_SWITCH_GR303_TMC:
330
 
                p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
331
 
                p->sapi = Q921_SAPI_GR303_TMC_CALLPROC;
332
 
                p->tei = Q921_TEI_GR303_TMC_CALLPROC;
333
 
                p->subchannel = __pri_new_tei(-1, node, PRI_SWITCH_GR303_TMC_SWITCHING, p, NULL, NULL, NULL, Q921_TEI_GR303_TMC_SWITCHING, 0);
334
 
                if (!p->subchannel) {
335
 
                        free(p);
336
 
                        p = NULL;
337
 
                }
338
 
                break;
339
 
        case PRI_SWITCH_GR303_TMC_SWITCHING:
340
 
                p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
341
 
                p->sapi = Q921_SAPI_GR303_TMC_SWITCHING;
342
 
                p->tei = Q921_TEI_GR303_TMC_SWITCHING;
343
 
                break;
344
 
        case PRI_SWITCH_GR303_EOC_PATH:
345
 
                p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
346
 
                p->sapi = Q921_SAPI_GR303_EOC;
347
 
                p->tei = Q921_TEI_GR303_EOC_PATH;
348
 
                break;
349
 
        default:
350
 
                break;
351
 
        }
352
 
 
353
 
        if (p->tei == Q921_TEI_GROUP && p->sapi == Q921_SAPI_LAYER2_MANAGEMENT && p->localtype == PRI_CPE) {
354
 
                p->subchannel = __pri_new_tei(-1, p->localtype, p->switchtype, p, NULL, NULL, NULL, Q921_TEI_PRI, 1);
355
 
                if (!p->subchannel) {
356
 
                        free(p);
357
 
                        return NULL;
358
 
                }
359
 
        } else
360
 
                q921_start(p);
361
 
        
362
 
        return p;
363
 
}
364
 
 
365
 
void pri_call_set_useruser(q931_call *c, const char *userchars)
366
 
{
367
 
        if (userchars)
368
 
                libpri_copy_string(c->useruserinfo, userchars, sizeof(c->useruserinfo));
369
 
}
370
 
 
371
 
void pri_sr_set_useruser(struct pri_sr *sr, const char *userchars)
372
 
{
373
 
        sr->useruserinfo = userchars;
374
 
}
375
 
 
376
 
int pri_restart(struct pri *pri)
377
 
{
378
 
        /* pri_restart() is no longer needed since the Q.921 rewrite. */
379
 
#if 0
380
 
        /* Restart Q.921 layer */
381
 
        if (pri) {
382
 
                q921_reset(pri, 1);
383
 
                q921_start(pri, pri->localtype == PRI_CPE);     
384
 
        }
385
 
#endif
386
 
        return 0;
387
 
}
388
 
 
389
 
struct pri *pri_new(int fd, int nodetype, int switchtype)
390
 
{
391
 
        return __pri_new_tei(fd, nodetype, switchtype, NULL, __pri_read, __pri_write, NULL, Q921_TEI_PRI, 0);
392
 
}
393
 
 
394
 
struct pri *pri_new_bri(int fd, int ptpmode, int nodetype, int switchtype)
395
 
{
396
 
        if (ptpmode)
397
 
                return __pri_new_tei(fd, nodetype, switchtype, NULL, __pri_read, __pri_write, NULL, Q921_TEI_PRI, 1);
398
 
        else
399
 
                return __pri_new_tei(fd, nodetype, switchtype, NULL, __pri_read, __pri_write, NULL, Q921_TEI_GROUP, 1);
400
 
}
401
 
 
402
 
struct pri *pri_new_cb(int fd, int nodetype, int switchtype, pri_io_cb io_read, pri_io_cb io_write, void *userdata)
403
 
{
404
 
        if (!io_read)
405
 
                io_read = __pri_read;
406
 
        if (!io_write)
407
 
                io_write = __pri_write;
408
 
        return __pri_new_tei(fd, nodetype, switchtype, NULL, io_read, io_write, userdata, Q921_TEI_PRI, 0);
409
 
}
410
 
 
411
 
void *pri_get_userdata(struct pri *pri)
412
 
{
413
 
        return pri ? pri->userdata : NULL;
414
 
}
415
 
 
416
 
void pri_set_userdata(struct pri *pri, void *userdata)
417
 
{
418
 
        if (pri)
419
 
                pri->userdata = userdata;
420
 
}
421
 
 
422
 
void pri_set_nsf(struct pri *pri, int nsf)
423
 
{
424
 
        if (pri)
425
 
                pri->nsf = nsf;
426
 
}
427
 
 
428
 
char *pri_event2str(int id)
429
 
{
430
 
        unsigned idx;
431
 
        struct {
432
 
                int id;
433
 
                char *name;
434
 
        } events[] = {
435
 
/* *INDENT-OFF* */
436
 
                { PRI_EVENT_DCHAN_UP,       "D-Channel Up" },
437
 
                { PRI_EVENT_DCHAN_DOWN,     "D-channel Down" },
438
 
                { PRI_EVENT_RESTART,        "Restart channel" },
439
 
                { PRI_EVENT_CONFIG_ERR,     "Configuration Error" },
440
 
                { PRI_EVENT_RING,           "Ring" },
441
 
                { PRI_EVENT_HANGUP,         "Hangup" },
442
 
                { PRI_EVENT_RINGING,        "Ringing" },
443
 
                { PRI_EVENT_ANSWER,         "Answer" },
444
 
                { PRI_EVENT_HANGUP_ACK,     "Hangup ACK" },
445
 
                { PRI_EVENT_RESTART_ACK,    "Restart ACK" },
446
 
                { PRI_EVENT_FACILITY,       "Facility" },
447
 
                { PRI_EVENT_INFO_RECEIVED,  "Info Received" },
448
 
                { PRI_EVENT_PROCEEDING,     "Proceeding" },
449
 
                { PRI_EVENT_SETUP_ACK,      "Setup ACK" },
450
 
                { PRI_EVENT_HANGUP_REQ,     "Hangup Req" },
451
 
                { PRI_EVENT_NOTIFY,         "Notify" },
452
 
                { PRI_EVENT_PROGRESS,       "Progress" },
453
 
                { PRI_EVENT_KEYPAD_DIGIT,   "Keypad Digit" },
454
 
                { PRI_EVENT_SERVICE,        "Service" },
455
 
                { PRI_EVENT_SERVICE_ACK,    "Service ACK" },
456
 
                { PRI_EVENT_HOLD,           "Hold" },
457
 
                { PRI_EVENT_HOLD_ACK,       "Hold Ack" },
458
 
                { PRI_EVENT_HOLD_REJ,       "Hold Rej" },
459
 
                { PRI_EVENT_RETRIEVE,       "Retrieve" },
460
 
                { PRI_EVENT_RETRIEVE_ACK,   "Retrieve ACK" },
461
 
                { PRI_EVENT_RETRIEVE_REJ,   "Retrieve Rej" },
462
 
/* *INDENT-ON* */
463
 
        };
464
 
 
465
 
        for (idx = 0; idx < ARRAY_LEN(events); ++idx) {
466
 
                if (events[idx].id == id) {
467
 
                        return events[idx].name;
468
 
                }
469
 
        }
470
 
        return "Unknown Event";
471
 
}
472
 
 
473
 
pri_event *pri_check_event(struct pri *pri)
474
 
{
475
 
        char buf[1024];
476
 
        int res;
477
 
        pri_event *e;
478
 
        res = pri->read_func ? pri->read_func(pri, buf, sizeof(buf)) : 0;
479
 
        if (!res)
480
 
                return NULL;
481
 
        /* Receive the q921 packet */
482
 
        e = q921_receive(pri, (q921_h *)buf, res);
483
 
        return e;
484
 
}
485
 
 
486
 
static int wait_pri(struct pri *pri)
487
 
{       
488
 
        struct timeval *tv, real;
489
 
        fd_set fds;
490
 
        int res;
491
 
        FD_ZERO(&fds);
492
 
        FD_SET(pri->fd, &fds);
493
 
        tv = pri_schedule_next(pri);
494
 
        if (tv) {
495
 
                gettimeofday(&real, NULL);
496
 
                real.tv_sec = tv->tv_sec - real.tv_sec;
497
 
                real.tv_usec = tv->tv_usec - real.tv_usec;
498
 
                if (real.tv_usec < 0) {
499
 
                        real.tv_usec += 1000000;
500
 
                        real.tv_sec -= 1;
501
 
                }
502
 
                if (real.tv_sec < 0) {
503
 
                        real.tv_sec = 0;
504
 
                        real.tv_usec = 0;
505
 
                }
506
 
        }
507
 
        res = select(pri->fd + 1, &fds, NULL, NULL, tv ? &real : tv);
508
 
        if (res < 0) 
509
 
                return -1;
510
 
        return res;
511
 
}
512
 
 
513
 
pri_event *pri_mkerror(struct pri *pri, char *errstr)
514
 
{
515
 
        /* Return a configuration error */
516
 
        pri->ev.err.e = PRI_EVENT_CONFIG_ERR;
517
 
        libpri_copy_string(pri->ev.err.err, errstr, sizeof(pri->ev.err.err));
518
 
        return &pri->ev;
519
 
}
520
 
 
521
 
 
522
 
pri_event *pri_dchannel_run(struct pri *pri, int block)
523
 
{
524
 
        pri_event *e;
525
 
        int res;
526
 
        if (!pri)
527
 
                return NULL;
528
 
        if (block) {
529
 
                do {
530
 
                        e =  NULL;
531
 
                        res = wait_pri(pri);
532
 
                        /* Check for error / interruption */
533
 
                        if (res < 0)
534
 
                                return NULL;
535
 
                        if (!res)
536
 
                                e = pri_schedule_run(pri);
537
 
                        else
538
 
                                e = pri_check_event(pri);
539
 
                } while(!e);
540
 
        } else {
541
 
                e = pri_check_event(pri);
542
 
                return e;
543
 
        }
544
 
        return e;
545
 
}
546
 
 
547
 
void pri_set_debug(struct pri *pri, int debug)
548
 
{
549
 
        if (!pri)
550
 
                return;
551
 
        pri->debug = debug;
552
 
        if (pri->subchannel)
553
 
                pri_set_debug(pri->subchannel, debug);
554
 
}
555
 
 
556
 
int pri_get_debug(struct pri *pri)
557
 
{
558
 
        if (!pri)
559
 
                return -1;
560
 
        if (pri->subchannel)
561
 
                return pri_get_debug(pri->subchannel);
562
 
        return pri->debug;
563
 
}
564
 
 
565
 
void pri_facility_enable(struct pri *pri)
566
 
{
567
 
        if (!pri)
568
 
                return;
569
 
        pri->sendfacility = 1;
570
 
        if (pri->subchannel)
571
 
                pri_facility_enable(pri->subchannel);
572
 
        return;
573
 
}
574
 
 
575
 
int pri_acknowledge(struct pri *pri, q931_call *call, int channel, int info)
576
 
{
577
 
        if (!pri || !call)
578
 
                return -1;
579
 
        return q931_alerting(pri, call, channel, info);
580
 
}
581
 
 
582
 
int pri_proceeding(struct pri *pri, q931_call *call, int channel, int info)
583
 
{
584
 
        if (!pri || !call)
585
 
                return -1;
586
 
        return q931_call_proceeding(pri, call, channel, info);
587
 
}
588
 
 
589
 
int pri_progress_with_cause(struct pri *pri, q931_call *call, int channel, int info, int cause)
590
 
{
591
 
        if (!pri || !call)
592
 
                return -1;
593
 
 
594
 
        return q931_call_progress_with_cause(pri, call, channel, info, cause);
595
 
}
596
 
 
597
 
int pri_progress(struct pri *pri, q931_call *call, int channel, int info)
598
 
{
599
 
        if (!pri || !call)
600
 
                return -1;
601
 
 
602
 
        return q931_call_progress(pri, call, channel, info);
603
 
}
604
 
 
605
 
int pri_information(struct pri *pri, q931_call *call, char digit)
606
 
{
607
 
        if (!pri || !call)
608
 
                return -1;
609
 
        return q931_information(pri, call, digit);
610
 
}
611
 
 
612
 
int pri_keypad_facility(struct pri *pri, q931_call *call, const char *digits)
613
 
{
614
 
        if (!pri || !call || !digits || !digits[0])
615
 
                return -1;
616
 
 
617
 
        return q931_keypad_facility(pri, call, digits);
618
 
}
619
 
 
620
 
int pri_notify(struct pri *pri, q931_call *call, int channel, int info)
621
 
{
622
 
        if (!pri || !call)
623
 
                return -1;
624
 
        return q931_notify(pri, call, channel, info);
625
 
}
626
 
 
627
 
void pri_destroycall(struct pri *pri, q931_call *call)
628
 
{
629
 
        if (pri && call)
630
 
                q931_destroycall(pri, call);
631
 
        return;
632
 
}
633
 
 
634
 
int pri_need_more_info(struct pri *pri, q931_call *call, int channel, int nonisdn)
635
 
{
636
 
        if (!pri || !call)
637
 
                return -1;
638
 
        return q931_setup_ack(pri, call, channel, nonisdn);
639
 
}
640
 
 
641
 
int pri_answer(struct pri *pri, q931_call *call, int channel, int nonisdn)
642
 
{
643
 
        if (!pri || !call)
644
 
                return -1;
645
 
        return q931_connect(pri, call, channel, nonisdn);
646
 
}
647
 
 
648
 
/*!
649
 
 * \brief Copy the PRI party name to the Q.931 party name structure.
650
 
 *
651
 
 * \param q931_name Q.931 party name structure
652
 
 * \param pri_name PRI party name structure
653
 
 *
654
 
 * \return Nothing
655
 
 */
656
 
void pri_copy_party_name_to_q931(struct q931_party_name *q931_name, const struct pri_party_name *pri_name)
657
 
{
658
 
        q931_party_name_init(q931_name);
659
 
        if (pri_name->valid) {
660
 
                q931_name->valid = 1;
661
 
                q931_name->presentation = pri_name->presentation;
662
 
                q931_name->char_set = pri_name->char_set;
663
 
                libpri_copy_string(q931_name->str, pri_name->str, sizeof(q931_name->str));
664
 
        }
665
 
}
666
 
 
667
 
/*!
668
 
 * \brief Copy the PRI party number to the Q.931 party number structure.
669
 
 *
670
 
 * \param q931_number Q.931 party number structure
671
 
 * \param pri_number PRI party number structure
672
 
 *
673
 
 * \return Nothing
674
 
 */
675
 
void pri_copy_party_number_to_q931(struct q931_party_number *q931_number, const struct pri_party_number *pri_number)
676
 
{
677
 
        q931_party_number_init(q931_number);
678
 
        if (pri_number->valid) {
679
 
                q931_number->valid = 1;
680
 
                q931_number->presentation = pri_number->presentation;
681
 
                q931_number->plan = pri_number->plan;
682
 
                libpri_copy_string(q931_number->str, pri_number->str, sizeof(q931_number->str));
683
 
        }
684
 
}
685
 
 
686
 
/*!
687
 
 * \brief Copy the PRI party subaddress to the Q.931 party subaddress structure.
688
 
 *
689
 
 * \param q931_subaddress Q.931 party subaddress structure
690
 
 * \param pri_subaddress PRI party subaddress structure
691
 
 *
692
 
 * \return Nothing
693
 
 */
694
 
void pri_copy_party_subaddress_to_q931(struct q931_party_subaddress *q931_subaddress, const struct pri_party_subaddress *pri_subaddress)
695
 
{
696
 
        int length;
697
 
        int maxlen = sizeof(q931_subaddress->data) - 1;
698
 
 
699
 
        q931_party_subaddress_init(q931_subaddress);
700
 
 
701
 
        if (!pri_subaddress->valid) {
702
 
                return;
703
 
        }
704
 
 
705
 
        q931_subaddress->valid = 1;
706
 
        q931_subaddress->type = pri_subaddress->type;
707
 
 
708
 
        length = pri_subaddress->length;
709
 
        if (length > maxlen){
710
 
                length = maxlen;
711
 
        } else {
712
 
                q931_subaddress->odd_even_indicator = pri_subaddress->odd_even_indicator;
713
 
        }
714
 
        q931_subaddress->length = length;
715
 
        memcpy(q931_subaddress->data, pri_subaddress->data, length);
716
 
        q931_subaddress->data[length] = '\0';
717
 
}
718
 
 
719
 
/*!
720
 
 * \brief Copy the PRI party id to the Q.931 party id structure.
721
 
 *
722
 
 * \param q931_id Q.931 party id structure
723
 
 * \param pri_id PRI party id structure
724
 
 *
725
 
 * \return Nothing
726
 
 */
727
 
void pri_copy_party_id_to_q931(struct q931_party_id *q931_id, const struct pri_party_id *pri_id)
728
 
{
729
 
        pri_copy_party_name_to_q931(&q931_id->name, &pri_id->name);
730
 
        pri_copy_party_number_to_q931(&q931_id->number, &pri_id->number);
731
 
        pri_copy_party_subaddress_to_q931(&q931_id->subaddress, &pri_id->subaddress);
732
 
}
733
 
 
734
 
int pri_connected_line_update(struct pri *ctrl, q931_call *call, const struct pri_party_connected_line *connected)
735
 
{
736
 
        struct q931_party_id party_id;
737
 
        unsigned idx;
738
 
        struct q931_call *subcall;
739
 
 
740
 
        if (!ctrl || !call) {
741
 
                return -1;
742
 
        }
743
 
 
744
 
        pri_copy_party_id_to_q931(&party_id, &connected->id);
745
 
        q931_party_id_fixup(ctrl, &party_id);
746
 
        if (!q931_party_id_cmp(&party_id, &call->local_id)) {
747
 
                /* The local party information did not change so do nothing. */
748
 
                return 0;
749
 
        }
750
 
        call->local_id = party_id;
751
 
 
752
 
        /* Update all subcalls with new local_id. */
753
 
        if (call->outboundbroadcast && call->master_call == call) {
754
 
                for (idx = 0; idx < Q931_MAX_TEI; ++idx) {
755
 
                        subcall = call->subcalls[idx];
756
 
                        if (subcall) {
757
 
                                subcall->local_id = party_id;
758
 
                        }
759
 
                }
760
 
        }
761
 
 
762
 
        switch (call->ourcallstate) {
763
 
        case Q931_CALL_STATE_CALL_INITIATED:
764
 
        case Q931_CALL_STATE_OVERLAP_SENDING:
765
 
        case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
766
 
        case Q931_CALL_STATE_CALL_DELIVERED:
767
 
                /*
768
 
                 * The local party transferred to someone else before
769
 
                 * the remote end answered.
770
 
                 */
771
 
        case Q931_CALL_STATE_ACTIVE:
772
 
                switch (ctrl->switchtype) {
773
 
                case PRI_SWITCH_EUROISDN_E1:
774
 
                case PRI_SWITCH_EUROISDN_T1:
775
 
                        if (PTMP_MODE(ctrl)) {
776
 
                                /* PTMP mode */
777
 
                                q931_notify_redirection(ctrl, call, PRI_NOTIFY_TRANSFER_ACTIVE,
778
 
                                        &call->local_id.number);
779
 
                        } else {
780
 
                                /* PTP mode */
781
 
                                /* Immediately send EctInform APDU, callStatus=answered(0) */
782
 
                                send_call_transfer_complete(ctrl, call, 0);
783
 
                        }
784
 
                        break;
785
 
                case PRI_SWITCH_QSIG:
786
 
                        /* Immediately send CallTransferComplete APDU, callStatus=answered(0) */
787
 
                        send_call_transfer_complete(ctrl, call, 0);
788
 
                        break;
789
 
                default:
790
 
                        break;
791
 
                }
792
 
                break;
793
 
        default:
794
 
                /* Just save the data for further developments. */
795
 
                break;
796
 
        }
797
 
 
798
 
        return 0;
799
 
}
800
 
 
801
 
int pri_redirecting_update(struct pri *ctrl, q931_call *call, const struct pri_party_redirecting *redirecting)
802
 
{
803
 
        unsigned idx;
804
 
        struct q931_call *subcall;
805
 
 
806
 
        if (!ctrl || !call) {
807
 
                return -1;
808
 
        }
809
 
 
810
 
        /* Save redirecting.to information and reason. */
811
 
        pri_copy_party_id_to_q931(&call->redirecting.to, &redirecting->to);
812
 
        q931_party_id_fixup(ctrl, &call->redirecting.to);
813
 
        call->redirecting.reason = redirecting->reason;
814
 
 
815
 
        /*
816
 
         * Update all subcalls with new redirecting.to information and reason.
817
 
         * I do not think we will ever have any subcalls when this data is relevant,
818
 
         * but update it just in case.
819
 
         */
820
 
        if (call->outboundbroadcast && call->master_call == call) {
821
 
                for (idx = 0; idx < Q931_MAX_TEI; ++idx) {
822
 
                        subcall = call->subcalls[idx];
823
 
                        if (subcall) {
824
 
                                subcall->redirecting.to = call->redirecting.to;
825
 
                                subcall->redirecting.reason = redirecting->reason;
826
 
                        }
827
 
                }
828
 
        }
829
 
 
830
 
        switch (call->ourcallstate) {
831
 
        case Q931_CALL_STATE_NULL:
832
 
                /* Save the remaining redirecting information before we place a call. */
833
 
                pri_copy_party_id_to_q931(&call->redirecting.from, &redirecting->from);
834
 
                q931_party_id_fixup(ctrl, &call->redirecting.from);
835
 
                pri_copy_party_id_to_q931(&call->redirecting.orig_called, &redirecting->orig_called);
836
 
                q931_party_id_fixup(ctrl, &call->redirecting.orig_called);
837
 
                call->redirecting.orig_reason = redirecting->orig_reason;
838
 
                if (redirecting->count <= 0) {
839
 
                        if (call->redirecting.from.number.valid) {
840
 
                                /*
841
 
                                 * We are redirecting with an unknown count
842
 
                                 * so assume the count is one.
843
 
                                 */
844
 
                                call->redirecting.count = 1;
845
 
                        } else {
846
 
                                call->redirecting.count = 0;
847
 
                        }
848
 
                } else if (redirecting->count < PRI_MAX_REDIRECTS) {
849
 
                        call->redirecting.count = redirecting->count;
850
 
                } else {
851
 
                        call->redirecting.count = PRI_MAX_REDIRECTS;
852
 
                }
853
 
                break;
854
 
        case Q931_CALL_STATE_OVERLAP_RECEIVING:
855
 
        case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
856
 
        case Q931_CALL_STATE_CALL_RECEIVED:
857
 
                /* This is an incoming call that has not connected yet. */
858
 
                if (!call->redirecting.to.number.valid) {
859
 
                        /* Not being redirected toward valid number data. Ignore. */
860
 
                        break;
861
 
                }
862
 
 
863
 
                switch (ctrl->switchtype) {
864
 
                case PRI_SWITCH_EUROISDN_E1:
865
 
                case PRI_SWITCH_EUROISDN_T1:
866
 
                        if (PTMP_MODE(ctrl)) {
867
 
                                /* PTMP mode */
868
 
                                q931_notify_redirection(ctrl, call, PRI_NOTIFY_CALL_DIVERTING,
869
 
                                        &call->redirecting.to.number);
870
 
                                break;
871
 
                        }
872
 
                        /* PTP mode - same behaviour as Q.SIG */
873
 
                        /* fall through */
874
 
                case PRI_SWITCH_QSIG:
875
 
                        if (call->redirecting.state != Q931_REDIRECTING_STATE_PENDING_TX_DIV_LEG_3
876
 
                                || strcmp(call->redirecting.to.number.str, call->called.number.str) != 0) {
877
 
                                /* immediately send divertingLegInformation1 APDU */
878
 
                                if (rose_diverting_leg_information1_encode(ctrl, call)
879
 
                                        || q931_facility(ctrl, call)) {
880
 
                                        pri_message(ctrl,
881
 
                                                "Could not schedule facility message for divertingLegInfo1\n");
882
 
                                }
883
 
                        }
884
 
                        call->redirecting.state = Q931_REDIRECTING_STATE_IDLE;
885
 
 
886
 
                        /* immediately send divertingLegInformation3 APDU */
887
 
                        if (rose_diverting_leg_information3_encode(ctrl, call, Q931_FACILITY)
888
 
                                || q931_facility(ctrl, call)) {
889
 
                                pri_message(ctrl,
890
 
                                        "Could not schedule facility message for divertingLegInfo3\n");
891
 
                        }
892
 
                        break;
893
 
                default:
894
 
                        break;
895
 
                }
896
 
                break;
897
 
        default:
898
 
                pri_message(ctrl, "Ignored redirecting update because call in state %s(%d).\n",
899
 
                        q931_call_state_str(call->ourcallstate), call->ourcallstate);
900
 
                break;
901
 
        }
902
 
 
903
 
        return 0;
904
 
}
905
 
 
906
 
#if 0
907
 
/* deprecated routines, use pri_hangup */
908
 
int pri_release(struct pri *pri, q931_call *call, int cause)
909
 
{
910
 
        if (!pri || !call)
911
 
                return -1;
912
 
        return q931_release(pri, call, cause);
913
 
}
914
 
 
915
 
int pri_disconnect(struct pri *pri, q931_call *call, int cause)
916
 
{
917
 
        if (!pri || !call)
918
 
                return -1;
919
 
        return q931_disconnect(pri, call, cause);
920
 
}
921
 
#endif
922
 
 
923
 
int pri_channel_bridge(q931_call *call1, q931_call *call2)
924
 
{
925
 
        if (!call1 || !call2)
926
 
                return -1;
927
 
 
928
 
        /* Make sure we have compatible switchtypes */
929
 
        if (call1->pri->switchtype != call2->pri->switchtype)
930
 
                return -1;
931
 
 
932
 
        /* Check for bearer capability */
933
 
        if (call1->transcapability != call2->transcapability)
934
 
                return -1;
935
 
 
936
 
        /* Check to see if we're on the same PRI */
937
 
        if (call1->pri != call2->pri)
938
 
                return -1;
939
 
        
940
 
        switch (call1->pri->switchtype) {
941
 
                case PRI_SWITCH_NI2:
942
 
                case PRI_SWITCH_LUCENT5E:
943
 
                case PRI_SWITCH_ATT4ESS:
944
 
                        if (eect_initiate_transfer(call1->pri, call1, call2))
945
 
                                return -1;
946
 
                        else
947
 
                                return 0;
948
 
                        break;
949
 
                case PRI_SWITCH_DMS100:
950
 
                        if (rlt_initiate_transfer(call1->pri, call1, call2))
951
 
                                return -1;
952
 
                        else
953
 
                                return 0;
954
 
                        break;
955
 
                case PRI_SWITCH_QSIG:
956
 
                        call1->bridged_call = call2;
957
 
                        call2->bridged_call = call1;
958
 
                        if (anfpr_initiate_transfer(call1->pri, call1, call2))
959
 
                                return -1;
960
 
                        else
961
 
                                return 0;
962
 
                        break;
963
 
                default:
964
 
                        return -1;
965
 
        }
966
 
}
967
 
 
968
 
void pri_hangup_fix_enable(struct pri *ctrl, int enable)
969
 
{
970
 
        if (ctrl) {
971
 
                ctrl = PRI_MASTER(ctrl);
972
 
                ctrl->hangup_fix_enabled = enable ? 1 : 0;
973
 
        }
974
 
}
975
 
 
976
 
int pri_hangup(struct pri *pri, q931_call *call, int cause)
977
 
{
978
 
        if (!pri || !call)
979
 
                return -1;
980
 
        if (cause == -1)
981
 
                /* normal clear cause */
982
 
                cause = PRI_CAUSE_NORMAL_CLEARING;
983
 
        return q931_hangup(pri, call, cause);
984
 
}
985
 
 
986
 
int pri_reset(struct pri *pri, int channel)
987
 
{
988
 
        if (!pri)
989
 
                return -1;
990
 
        return q931_restart(pri, channel);
991
 
}
992
 
 
993
 
int pri_maintenance_service(struct pri *pri, int span, int channel, int changestatus)
994
 
{
995
 
        if (!pri) {
996
 
                return -1;
997
 
        }
998
 
        return maintenance_service(pri, span, channel, changestatus);
999
 
}
1000
 
 
1001
 
q931_call *pri_new_call(struct pri *pri)
1002
 
{
1003
 
        if (!pri)
1004
 
                return NULL;
1005
 
        return q931_new_call(pri);
1006
 
}
1007
 
 
1008
 
int pri_is_dummy_call(q931_call *call)
1009
 
{
1010
 
        if (!call) {
1011
 
                return 0;
1012
 
        }
1013
 
        return q931_is_dummy_call(call);
1014
 
}
1015
 
 
1016
 
void pri_dump_event(struct pri *pri, pri_event *e)
1017
 
{
1018
 
        if (!pri || !e)
1019
 
                return;
1020
 
        pri_message(pri, "Event type: %s (%d)\n", pri_event2str(e->gen.e), e->gen.e);
1021
 
        switch(e->gen.e) {
1022
 
        case PRI_EVENT_DCHAN_UP:
1023
 
        case PRI_EVENT_DCHAN_DOWN:
1024
 
                break;
1025
 
        case PRI_EVENT_CONFIG_ERR:
1026
 
                pri_message(pri, "Error: %s", e->err.err);
1027
 
                break;
1028
 
        case PRI_EVENT_RESTART:
1029
 
                pri_message(pri, "Restart on channel %d\n", e->restart.channel);
1030
 
        case PRI_EVENT_RING:
1031
 
                pri_message(pri, "Calling number: %s (%s, %s)\n", e->ring.callingnum, pri_plan2str(e->ring.callingplan), pri_pres2str(e->ring.callingpres));
1032
 
                pri_message(pri, "Called number: %s (%s)\n", e->ring.callednum, pri_plan2str(e->ring.calledplan));
1033
 
                pri_message(pri, "Channel: %d (%s) Reference number: %d\n", e->ring.channel, e->ring.flexible ? "Flexible" : "Not Flexible", e->ring.cref);
1034
 
                break;
1035
 
        case PRI_EVENT_HANGUP:
1036
 
                pri_message(pri, "Hangup, reference number: %d, reason: %s\n", e->hangup.cref, pri_cause2str(e->hangup.cause));
1037
 
                break;
1038
 
        default:
1039
 
                pri_message(pri, "Don't know how to dump events of type %d\n", e->gen.e);
1040
 
        }
1041
 
}
1042
 
 
1043
 
static void pri_sr_init(struct pri_sr *req)
1044
 
{
1045
 
        memset(req, 0, sizeof(struct pri_sr));
1046
 
        q931_party_redirecting_init(&req->redirecting);
1047
 
        q931_party_id_init(&req->caller);
1048
 
        q931_party_address_init(&req->called);
1049
 
        req->reversecharge = PRI_REVERSECHARGE_NONE;
1050
 
}
1051
 
 
1052
 
int pri_sr_set_connection_call_independent(struct pri_sr *req)
1053
 
{
1054
 
        if (!req)
1055
 
                return -1;
1056
 
 
1057
 
        req->cis_call = 1; /* have to set cis_call for all those pesky IEs we need to setup */
1058
 
        req->cis_auto_disconnect = 1;
1059
 
        return 0;
1060
 
}
1061
 
 
1062
 
int pri_sr_set_no_channel_call(struct pri_sr *req)
1063
 
{
1064
 
        if (!req) {
1065
 
                return -1;
1066
 
        }
1067
 
 
1068
 
        req->cis_call = 1;
1069
 
        return 0;
1070
 
}
1071
 
 
1072
 
/* Don't call any other pri functions on this */
1073
 
int pri_mwi_activate(struct pri *pri, q931_call *c, char *caller, int callerplan, char *callername, int callerpres, char *called,
1074
 
                                        int calledplan)
1075
 
{
1076
 
        struct pri_sr req;
1077
 
        if (!pri || !c)
1078
 
                return -1;
1079
 
 
1080
 
        pri_sr_init(&req);
1081
 
        pri_sr_set_connection_call_independent(&req);
1082
 
        pri_sr_set_caller(&req, caller, callername, callerplan, callerpres);
1083
 
        pri_sr_set_called(&req, called, calledplan, 0);
1084
 
 
1085
 
        if (mwi_message_send(pri, c, &req, 1) < 0) {
1086
 
                pri_message(pri, "Unable to send MWI activate message\n");
1087
 
                return -1;
1088
 
        }
1089
 
        /* Do more stuff when we figure out that the CISC stuff works */
1090
 
        return q931_setup(pri, c, &req);
1091
 
}
1092
 
 
1093
 
int pri_mwi_deactivate(struct pri *pri, q931_call *c, char *caller, int callerplan, char *callername, int callerpres, char *called,
1094
 
                                        int calledplan)
1095
 
{
1096
 
        struct pri_sr req;
1097
 
        if (!pri || !c)
1098
 
                return -1;
1099
 
 
1100
 
        pri_sr_init(&req);
1101
 
        pri_sr_set_connection_call_independent(&req);
1102
 
        pri_sr_set_caller(&req, caller, callername, callerplan, callerpres);
1103
 
        pri_sr_set_called(&req, called, calledplan, 0);
1104
 
 
1105
 
        if(mwi_message_send(pri, c, &req, 0) < 0) {
1106
 
                pri_message(pri, "Unable to send MWI deactivate message\n");
1107
 
                return -1;
1108
 
        }
1109
 
 
1110
 
        return q931_setup(pri, c, &req);
1111
 
}
1112
 
        
1113
 
int pri_setup(struct pri *pri, q931_call *c, struct pri_sr *req)
1114
 
{
1115
 
        if (!pri || !c)
1116
 
                return -1;
1117
 
 
1118
 
        return q931_setup(pri, c, req);
1119
 
}
1120
 
 
1121
 
int pri_call(struct pri *pri, q931_call *c, int transmode, int channel, int exclusive, 
1122
 
                                        int nonisdn, char *caller, int callerplan, char *callername, int callerpres, char *called,
1123
 
                                        int calledplan, int ulayer1)
1124
 
{
1125
 
        struct pri_sr req;
1126
 
        if (!pri || !c)
1127
 
                return -1;
1128
 
        pri_sr_init(&req);
1129
 
        pri_sr_set_caller(&req, caller, callername, callerplan, callerpres);
1130
 
        pri_sr_set_called(&req, called, calledplan, 0);
1131
 
        req.transmode = transmode;
1132
 
        req.channel = channel;
1133
 
        req.exclusive = exclusive;
1134
 
        req.nonisdn =  nonisdn;
1135
 
        req.userl1 = ulayer1;
1136
 
        return q931_setup(pri, c, &req);
1137
 
}       
1138
 
 
1139
 
static void (*__pri_error)(struct pri *pri, char *stuff);
1140
 
static void (*__pri_message)(struct pri *pri, char *stuff);
1141
 
 
1142
 
void pri_set_message(void (*func)(struct pri *pri, char *stuff))
1143
 
{
1144
 
        __pri_message = func;
1145
 
}
1146
 
 
1147
 
void pri_set_error(void (*func)(struct pri *pri, char *stuff))
1148
 
{
1149
 
        __pri_error = func;
1150
 
}
1151
 
 
1152
 
static void pri_old_message(struct pri *ctrl, const char *fmt, va_list *ap)
1153
 
{
1154
 
        char tmp[1024];
1155
 
 
1156
 
        vsnprintf(tmp, sizeof(tmp), fmt, *ap);
1157
 
        if (__pri_message)
1158
 
                __pri_message(ctrl, tmp);
1159
 
        else
1160
 
                fputs(tmp, stdout);
1161
 
}
1162
 
 
1163
 
void pri_message(struct pri *ctrl, const char *fmt, ...)
1164
 
{
1165
 
        int added_length;
1166
 
        va_list ap;
1167
 
 
1168
 
        if (ctrl) {
1169
 
                ctrl = PRI_MASTER(ctrl);
1170
 
        }
1171
 
        if (!ctrl || !ctrl->msg_line) {
1172
 
                /* Just have to do it the old way. */
1173
 
                va_start(ap, fmt);
1174
 
                pri_old_message(ctrl, fmt, &ap);
1175
 
                va_end(ap);
1176
 
                return;
1177
 
        }
1178
 
 
1179
 
        va_start(ap, fmt);
1180
 
        added_length = vsnprintf(ctrl->msg_line->str + ctrl->msg_line->length,
1181
 
                sizeof(ctrl->msg_line->str) - ctrl->msg_line->length, fmt, ap);
1182
 
        va_end(ap);
1183
 
        if (added_length < 0
1184
 
                || sizeof(ctrl->msg_line->str) <= ctrl->msg_line->length + added_length) {
1185
 
                static char truncated_output[] =
1186
 
                        "v-- Error building output or output was truncated. (Next line) --v\n";
1187
 
 
1188
 
                /*
1189
 
                 * This clause should never need to run because the
1190
 
                 * output line accumulation buffer is quite large.
1191
 
                 */
1192
 
 
1193
 
                /* vsnprintf() error or output string was truncated. */
1194
 
                if (__pri_message) {
1195
 
                        __pri_message(ctrl, truncated_output);
1196
 
                } else {
1197
 
                        fputs(truncated_output, stdout);
1198
 
                }
1199
 
 
1200
 
                /* Add a terminating '\n' to force a flush of the line. */
1201
 
                ctrl->msg_line->length = strlen(ctrl->msg_line->str);
1202
 
                if (ctrl->msg_line->length) {
1203
 
                        ctrl->msg_line->str[ctrl->msg_line->length - 1] = '\n';
1204
 
                } else {
1205
 
                        ctrl->msg_line->str[0] = '\n';
1206
 
                        ctrl->msg_line->str[1] = '\0';
1207
 
                }
1208
 
        } else {
1209
 
                ctrl->msg_line->length += added_length;
1210
 
        }
1211
 
 
1212
 
        if (ctrl->msg_line->length
1213
 
                && ctrl->msg_line->str[ctrl->msg_line->length - 1] == '\n') {
1214
 
                /* The accumulated output line was terminated so send it out. */
1215
 
                ctrl->msg_line->length = 0;
1216
 
                if (__pri_message) {
1217
 
                        __pri_message(ctrl, ctrl->msg_line->str);
1218
 
                } else {
1219
 
                        fputs(ctrl->msg_line->str, stdout);
1220
 
                }
1221
 
        }
1222
 
}
1223
 
 
1224
 
void pri_error(struct pri *pri, const char *fmt, ...)
1225
 
{
1226
 
        char tmp[1024];
1227
 
        va_list ap;
1228
 
        va_start(ap, fmt);
1229
 
        vsnprintf(tmp, sizeof(tmp), fmt, ap);
1230
 
        va_end(ap);
1231
 
        if (__pri_error)
1232
 
                __pri_error(pri ? PRI_MASTER(pri) : NULL, tmp);
1233
 
        else
1234
 
                fputs(tmp, stderr);
1235
 
}
1236
 
 
1237
 
/* Set overlap mode */
1238
 
void pri_set_overlapdial(struct pri *pri,int state)
1239
 
{
1240
 
        if (pri) {
1241
 
                pri->overlapdial = state ? 1 : 0;
1242
 
        }
1243
 
}
1244
 
 
1245
 
void pri_set_chan_mapping_logical(struct pri *pri, int state)
1246
 
{
1247
 
        if (pri && pri->switchtype == PRI_SWITCH_QSIG) {
1248
 
                pri->chan_mapping_logical = state ? 1 : 0;
1249
 
        }
1250
 
}
1251
 
 
1252
 
void pri_set_inbanddisconnect(struct pri *pri, unsigned int enable)
1253
 
{
1254
 
        if (pri) {
1255
 
                pri->acceptinbanddisconnect = (enable != 0);
1256
 
        }
1257
 
}
1258
 
 
1259
 
int pri_fd(struct pri *pri)
1260
 
{
1261
 
        return pri->fd;
1262
 
}
1263
 
 
1264
 
/*!
1265
 
 * \internal
1266
 
 * \brief Append snprintf output to the given buffer.
1267
 
 *
1268
 
 * \param buf Buffer currently filling.
1269
 
 * \param buf_used Offset into buffer where to put new stuff.
1270
 
 * \param buf_size Actual buffer size of buf.
1271
 
 * \param format printf format string.
1272
 
 *
1273
 
 * \return Total buffer space used.
1274
 
 */
1275
 
static size_t pri_snprintf(char *buf, size_t buf_used, size_t buf_size, const char *format, ...) __attribute__((format(printf, 4, 5)));
1276
 
static size_t pri_snprintf(char *buf, size_t buf_used, size_t buf_size, const char *format, ...)
1277
 
{
1278
 
        va_list args;
1279
 
 
1280
 
        if (buf_used < buf_size) {
1281
 
                va_start(args, format);
1282
 
                buf_used += vsnprintf(buf + buf_used, buf_size - buf_used, format, args);
1283
 
                va_end(args);
1284
 
        }
1285
 
        if (buf_size < buf_used) {
1286
 
                buf_used = buf_size + 1;
1287
 
        }
1288
 
        return buf_used;
1289
 
}
1290
 
 
1291
 
char *pri_dump_info_str(struct pri *ctrl)
1292
 
{
1293
 
        char *buf;
1294
 
        size_t buf_size;
1295
 
        size_t used;
1296
 
#ifdef LIBPRI_COUNTERS
1297
 
        struct q921_frame *f;
1298
 
        unsigned q921outstanding;
1299
 
#endif
1300
 
        unsigned idx;
1301
 
        unsigned long switch_bit;
1302
 
 
1303
 
        if (!ctrl) {
1304
 
                return NULL;
1305
 
        }
1306
 
 
1307
 
        buf_size = 4096;        /* This should be bigger than we will ever need. */
1308
 
        buf = malloc(buf_size);
1309
 
        if (!buf) {
1310
 
                return NULL;
1311
 
        }
1312
 
 
1313
 
        /* Might be nice to format these a little better */
1314
 
        used = 0;
1315
 
        used = pri_snprintf(buf, used, buf_size, "Switchtype: %s\n",
1316
 
                pri_switch2str(ctrl->switchtype));
1317
 
        used = pri_snprintf(buf, used, buf_size, "Type: %s\n", pri_node2str(ctrl->localtype));
1318
 
#ifdef LIBPRI_COUNTERS
1319
 
        /* Remember that Q921 Counters include Q931 packets (and any retransmissions) */
1320
 
        used = pri_snprintf(buf, used, buf_size, "Q931 RX: %d\n", ctrl->q931_rxcount);
1321
 
        used = pri_snprintf(buf, used, buf_size, "Q931 TX: %d\n", ctrl->q931_txcount);
1322
 
        used = pri_snprintf(buf, used, buf_size, "Q921 RX: %d\n", ctrl->q921_rxcount);
1323
 
        used = pri_snprintf(buf, used, buf_size, "Q921 TX: %d\n", ctrl->q921_txcount);
1324
 
        q921outstanding = 0;
1325
 
        f = ctrl->txqueue;
1326
 
        while (f) {
1327
 
                q921outstanding++;
1328
 
                f = f->next;
1329
 
        }
1330
 
        used = pri_snprintf(buf, used, buf_size, "Q921 Outstanding: %u\n", q921outstanding);
1331
 
#endif
1332
 
#if 0
1333
 
        used = pri_snprintf(buf, used, buf_size, "Window Length: %d/%d\n",
1334
 
                ctrl->timers[PRI_TIMER_K], ctrl->window);
1335
 
        used = pri_snprintf(buf, used, buf_size, "Sentrej: %d\n", ctrl->sentrej);
1336
 
        used = pri_snprintf(buf, used, buf_size, "SolicitFbit: %d\n", ctrl->solicitfbit);
1337
 
        used = pri_snprintf(buf, used, buf_size, "Retrans: %d\n", ctrl->retrans);
1338
 
        used = pri_snprintf(buf, used, buf_size, "Busy: %d\n", ctrl->busy);
1339
 
#endif
1340
 
        used = pri_snprintf(buf, used, buf_size, "Overlap Dial: %d\n", ctrl->overlapdial);
1341
 
        used = pri_snprintf(buf, used, buf_size, "Logical Channel Mapping: %d\n",
1342
 
                ctrl->chan_mapping_logical);
1343
 
        used = pri_snprintf(buf, used, buf_size, "Timer and counter settings:\n");
1344
 
        switch_bit = PRI_BIT(ctrl->switchtype);
1345
 
        for (idx = 0; idx < ARRAY_LEN(pri_timer); ++idx) {
1346
 
                if (pri_timer[idx].used_by & switch_bit) {
1347
 
                        enum PRI_TIMERS_AND_COUNTERS tmr;
1348
 
 
1349
 
                        tmr = pri_timer[idx].number;
1350
 
                        if (0 <= ctrl->timers[tmr] || tmr == PRI_TIMER_T309) {
1351
 
                                used = pri_snprintf(buf, used, buf_size, "  %s: %d\n",
1352
 
                                        pri_timer[idx].name, ctrl->timers[tmr]);
1353
 
                        }
1354
 
                }
1355
 
        }
1356
 
 
1357
 
        if (buf_size < used) {
1358
 
                pri_message(ctrl,
1359
 
                        "pri_dump_info_str(): Produced output exceeded buffer capacity. (Truncated)\n");
1360
 
        }
1361
 
        return buf;
1362
 
}
1363
 
 
1364
 
int pri_get_crv(struct pri *pri, q931_call *call, int *callmode)
1365
 
{
1366
 
        return q931_call_getcrv(pri, call, callmode);
1367
 
}
1368
 
 
1369
 
int pri_set_crv(struct pri *pri, q931_call *call, int crv, int callmode)
1370
 
{
1371
 
        return q931_call_setcrv(pri, call, crv, callmode);
1372
 
}
1373
 
 
1374
 
void pri_enslave(struct pri *master, struct pri *slave)
1375
 
{
1376
 
        if (master && slave)
1377
 
                slave->callpool = &master->localpool;
1378
 
}
1379
 
 
1380
 
struct pri_sr *pri_sr_new(void)
1381
 
{
1382
 
        struct pri_sr *req;
1383
 
        req = malloc(sizeof(*req));
1384
 
        if (req) 
1385
 
                pri_sr_init(req);
1386
 
        return req;
1387
 
}
1388
 
 
1389
 
void pri_sr_free(struct pri_sr *sr)
1390
 
{
1391
 
        free(sr);
1392
 
}
1393
 
 
1394
 
int pri_sr_set_channel(struct pri_sr *sr, int channel, int exclusive, int nonisdn)
1395
 
{
1396
 
        sr->channel = channel;
1397
 
        sr->exclusive = exclusive;
1398
 
        sr->nonisdn = nonisdn;
1399
 
        return 0;
1400
 
}
1401
 
 
1402
 
int pri_sr_set_bearer(struct pri_sr *sr, int transmode, int userl1)
1403
 
{
1404
 
        sr->transmode = transmode;
1405
 
        sr->userl1 = userl1;
1406
 
        return 0;
1407
 
}
1408
 
 
1409
 
int pri_sr_set_called(struct pri_sr *sr, char *called, int calledplan, int numcomplete)
1410
 
{
1411
 
        q931_party_address_init(&sr->called);
1412
 
        if (called) {
1413
 
                sr->called.number.valid = 1;
1414
 
                sr->called.number.plan = calledplan;
1415
 
                libpri_copy_string(sr->called.number.str, called, sizeof(sr->called.number.str));
1416
 
        }
1417
 
        sr->numcomplete = numcomplete;
1418
 
        return 0;
1419
 
}
1420
 
 
1421
 
void pri_sr_set_called_subaddress(struct pri_sr *sr, const struct pri_party_subaddress *subaddress)
1422
 
{
1423
 
        pri_copy_party_subaddress_to_q931(&sr->called.subaddress, subaddress);
1424
 
}
1425
 
 
1426
 
int pri_sr_set_caller(struct pri_sr *sr, char *caller, char *callername, int callerplan, int callerpres)
1427
 
{
1428
 
        q931_party_id_init(&sr->caller);
1429
 
        if (caller) {
1430
 
                sr->caller.number.valid = 1;
1431
 
                sr->caller.number.presentation = callerpres;
1432
 
                sr->caller.number.plan = callerplan;
1433
 
                libpri_copy_string(sr->caller.number.str, caller, sizeof(sr->caller.number.str));
1434
 
 
1435
 
                if (callername) {
1436
 
                        sr->caller.name.valid = 1;
1437
 
                        sr->caller.name.presentation = callerpres;
1438
 
                        sr->caller.name.char_set = PRI_CHAR_SET_ISO8859_1;
1439
 
                        libpri_copy_string(sr->caller.name.str, callername,
1440
 
                                sizeof(sr->caller.name.str));
1441
 
                }
1442
 
        }
1443
 
        return 0;
1444
 
}
1445
 
 
1446
 
void pri_sr_set_caller_subaddress(struct pri_sr *sr, const struct pri_party_subaddress *subaddress)
1447
 
{
1448
 
        pri_copy_party_subaddress_to_q931(&sr->caller.subaddress, subaddress);
1449
 
}
1450
 
 
1451
 
void pri_sr_set_caller_party(struct pri_sr *sr, const struct pri_party_id *caller)
1452
 
{
1453
 
        pri_copy_party_id_to_q931(&sr->caller, caller);
1454
 
}
1455
 
 
1456
 
int pri_sr_set_redirecting(struct pri_sr *sr, char *num, int plan, int pres, int reason)
1457
 
{
1458
 
        q931_party_redirecting_init(&sr->redirecting);
1459
 
        if (num && num[0]) {
1460
 
                sr->redirecting.from.number.valid = 1;
1461
 
                sr->redirecting.from.number.presentation = pres;
1462
 
                sr->redirecting.from.number.plan = plan;
1463
 
                libpri_copy_string(sr->redirecting.from.number.str, num,
1464
 
                        sizeof(sr->redirecting.from.number.str));
1465
 
 
1466
 
                sr->redirecting.count = 1;
1467
 
                sr->redirecting.reason = reason;
1468
 
        }
1469
 
        return 0;
1470
 
}
1471
 
 
1472
 
void pri_sr_set_redirecting_parties(struct pri_sr *sr, const struct pri_party_redirecting *redirecting)
1473
 
{
1474
 
        pri_copy_party_id_to_q931(&sr->redirecting.from, &redirecting->from);
1475
 
        pri_copy_party_id_to_q931(&sr->redirecting.to, &redirecting->to);
1476
 
        pri_copy_party_id_to_q931(&sr->redirecting.orig_called, &redirecting->orig_called);
1477
 
        sr->redirecting.orig_reason = redirecting->orig_reason;
1478
 
        sr->redirecting.reason = redirecting->reason;
1479
 
        if (redirecting->count <= 0) {
1480
 
                if (sr->redirecting.from.number.valid) {
1481
 
                        /*
1482
 
                         * We are redirecting with an unknown count
1483
 
                         * so assume the count is one.
1484
 
                         */
1485
 
                        sr->redirecting.count = 1;
1486
 
                } else {
1487
 
                        sr->redirecting.count = 0;
1488
 
                }
1489
 
        } else if (redirecting->count < PRI_MAX_REDIRECTS) {
1490
 
                sr->redirecting.count = redirecting->count;
1491
 
        } else {
1492
 
                sr->redirecting.count = PRI_MAX_REDIRECTS;
1493
 
        }
1494
 
}
1495
 
 
1496
 
void pri_sr_set_reversecharge(struct pri_sr *sr, int requested)
1497
 
{
1498
 
        sr->reversecharge = requested;
1499
 
}
1500
 
 
1501
 
void pri_sr_set_keypad_digits(struct pri_sr *sr, const char *keypad_digits)
1502
 
{
1503
 
        sr->keypad_digits = keypad_digits;
1504
 
}
1505
 
 
1506
 
void pri_hold_enable(struct pri *ctrl, int enable)
1507
 
{
1508
 
        if (ctrl) {
1509
 
                ctrl = PRI_MASTER(ctrl);
1510
 
                ctrl->hold_support = enable ? 1 : 0;
1511
 
        }
1512
 
}
1513
 
 
1514
 
int pri_hold(struct pri *ctrl, q931_call *call)
1515
 
{
1516
 
        if (!ctrl || !call) {
1517
 
                return -1;
1518
 
        }
1519
 
        return q931_send_hold(ctrl, call);
1520
 
}
1521
 
 
1522
 
int pri_hold_ack(struct pri *ctrl, q931_call *call)
1523
 
{
1524
 
        if (!ctrl || !call) {
1525
 
                return -1;
1526
 
        }
1527
 
        return q931_send_hold_ack(ctrl, call);
1528
 
}
1529
 
 
1530
 
int pri_hold_rej(struct pri *ctrl, q931_call *call, int cause)
1531
 
{
1532
 
        if (!ctrl || !call) {
1533
 
                return -1;
1534
 
        }
1535
 
        return q931_send_hold_rej(ctrl, call, cause);
1536
 
}
1537
 
 
1538
 
int pri_retrieve(struct pri *ctrl, q931_call *call, int channel)
1539
 
{
1540
 
        if (!ctrl || !call) {
1541
 
                return -1;
1542
 
        }
1543
 
        return q931_send_retrieve(ctrl, call, channel);
1544
 
}
1545
 
 
1546
 
int pri_retrieve_ack(struct pri *ctrl, q931_call *call, int channel)
1547
 
{
1548
 
        if (!ctrl || !call) {
1549
 
                return -1;
1550
 
        }
1551
 
        return q931_send_retrieve_ack(ctrl, call, channel);
1552
 
}
1553
 
 
1554
 
int pri_retrieve_rej(struct pri *ctrl, q931_call *call, int cause)
1555
 
{
1556
 
        if (!ctrl || !call) {
1557
 
                return -1;
1558
 
        }
1559
 
        return q931_send_retrieve_rej(ctrl, call, cause);
1560
 
}
1561
 
 
1562
 
int pri_callrerouting_facility(struct pri *pri, q931_call *call, const char *dest, const char* original, const char* reason)
1563
 
{
1564
 
        if (!pri || !call || !dest)
1565
 
                return -1;
1566
 
 
1567
 
        return qsig_cf_callrerouting(pri, call, dest, original, reason);
1568
 
}
1569
 
 
1570
 
void pri_reroute_enable(struct pri *ctrl, int enable)
1571
 
{
1572
 
        if (ctrl) {
1573
 
                ctrl = PRI_MASTER(ctrl);
1574
 
                ctrl->deflection_support = enable ? 1 : 0;
1575
 
        }
1576
 
}
1577
 
 
1578
 
int pri_reroute_call(struct pri *ctrl, q931_call *call, const struct pri_party_id *caller, const struct pri_party_redirecting *deflection, int subscription_option)
1579
 
{
1580
 
        const struct q931_party_id *caller_id;
1581
 
        struct q931_party_id local_caller;
1582
 
        struct q931_party_redirecting reroute;
1583
 
 
1584
 
        if (!ctrl || !call || !deflection) {
1585
 
                return -1;
1586
 
        }
1587
 
 
1588
 
        if (caller) {
1589
 
                /* Convert the caller update information. */
1590
 
                pri_copy_party_id_to_q931(&local_caller, caller);
1591
 
                q931_party_id_fixup(ctrl, &local_caller);
1592
 
                caller_id = &local_caller;
1593
 
        } else {
1594
 
                caller_id = NULL;
1595
 
        }
1596
 
 
1597
 
        /* Convert the deflection information. */
1598
 
        q931_party_redirecting_init(&reroute);
1599
 
        pri_copy_party_id_to_q931(&reroute.from, &deflection->from);
1600
 
        q931_party_id_fixup(ctrl, &reroute.from);
1601
 
        pri_copy_party_id_to_q931(&reroute.to, &deflection->to);
1602
 
        q931_party_id_fixup(ctrl, &reroute.to);
1603
 
        pri_copy_party_id_to_q931(&reroute.orig_called, &deflection->orig_called);
1604
 
        q931_party_id_fixup(ctrl, &reroute.orig_called);
1605
 
        reroute.reason = deflection->reason;
1606
 
        reroute.orig_reason = deflection->orig_reason;
1607
 
        if (deflection->count <= 0) {
1608
 
                /*
1609
 
                 * We are deflecting with an unknown count
1610
 
                 * so assume the count is one.
1611
 
                 */
1612
 
                reroute.count = 1;
1613
 
        } else if (deflection->count < PRI_MAX_REDIRECTS) {
1614
 
                reroute.count = deflection->count;
1615
 
        } else {
1616
 
                reroute.count = PRI_MAX_REDIRECTS;
1617
 
        }
1618
 
 
1619
 
        return send_reroute_request(ctrl, call, caller_id, &reroute, subscription_option);
1620
 
}