~davewalker/ubuntu/maverick/asterisk/lp_705014

« back to all changes in this revision

Viewing changes to channels/chan_vpb.c

  • Committer: Bazaar Package Importer
  • Author(s): Kilian Krause
  • Date: 2005-03-09 22:09:05 UTC
  • mto: (1.2.1 upstream) (8.2.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050309220905-9afy6hcpw96xbr6j
Tags: upstream-1.0.6
ImportĀ upstreamĀ versionĀ 1.0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 * VoiceTronix Interface driver
5
5
 * 
6
6
 * Copyright (C) 2003, Paul Bagyenda
7
 
 *
8
7
 * Paul Bagyenda <bagyenda@dsmagic.com>
 
8
 * Copyright (C) 2004, Ben Kramer
 
9
 * Ben Kramer <ben@voicetronix.com.au>
 
10
 *
 
11
 * Daniel Bichara <daniel@bichara.com.br> - Brazilian CallerID detection (c)2004 
 
12
 *
 
13
 * Welber Silveira - welberms@magiclink.com.br - (c)2004
 
14
 * Copying CLID string to propper structure after detection
9
15
 *
10
16
 * This program is free software, distributed under the terms of
11
17
 * the GNU General Public License
12
18
 */
13
19
 
 
20
 
14
21
#include <stdio.h>
15
 
#include <pthread.h>
16
22
#include <string.h>
17
 
#include <asterisk/lock.h>
 
23
//#include <asterisk/lock.h>
 
24
#include <asterisk/utils.h>
18
25
#include <asterisk/channel.h>
19
26
#include <asterisk/channel_pvt.h>
20
27
#include <asterisk/config.h>
30
37
#include <arpa/inet.h>
31
38
#include <fcntl.h>
32
39
#include <sys/ioctl.h>
 
40
#include <ctype.h>
33
41
 
34
42
#include <vpbapi.h>
35
 
 
36
 
 
37
 
#define DEFAULT_GAIN 1.0
38
 
#define VPB_SAMPLES 240 
 
43
#include <assert.h>
 
44
 
 
45
#ifdef pthread_create
 
46
#undef pthread_create
 
47
#endif
 
48
 
 
49
#define DEFAULT_GAIN 0
 
50
#define DEFAULT_ECHO_CANCEL 1
 
51
  
 
52
#define VPB_SAMPLES 160 
39
53
#define VPB_MAX_BUF VPB_SAMPLES*4 + AST_FRIENDLY_OFFSET
40
54
 
41
55
#define VPB_NULL_EVENT 200
42
56
 
43
 
#define VPB_DIALTONE_WAIT 2000
44
 
#define VPB_RINGWAIT 2000
45
 
#define VPB_WAIT_TIMEOUT 40
46
 
 
 
57
#define VPB_WAIT_TIMEOUT 4000
 
58
 
 
59
#define MAX_VPB_GAIN 12.0
 
60
 
 
61
/*
47
62
#if defined(__cplusplus) || defined(c_plusplus)
48
63
 extern "C" {
49
64
#endif
 
65
*/
50
66
 
51
 
static char *desc = "VoiceTronix V6PCI/V12PCI  API Support";
 
67
static char *desc = "VoiceTronix V6PCI/V12PCI/V4PCI  API Support";
52
68
static char *type = "vpb";
53
69
static char *tdesc = "Standard VoiceTronix API Driver";
54
70
static char *config = "vpb.conf";
60
76
static char language[MAX_LANGUAGE] = "";
61
77
static int usecnt =0;
62
78
 
63
 
static int echocancel = 1; 
64
 
static int setrxgain = 0, settxgain = 0;
65
 
 
66
 
static int tcounter  = 0;
67
 
 
68
 
static int gruntdetect_timeout = 5000; /* Grunt detect timeout is 5 seconds. */
69
 
 
70
 
static int silencesupression = 0;
71
 
 
72
 
static const int prefformat = AST_FORMAT_ALAW | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ADPCM;
73
 
 
74
 
static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
 
79
static int gruntdetect_timeout = 3600000; /* Grunt detect timeout is 1hr. */
 
80
 
 
81
static const int prefformat = AST_FORMAT_SLINEAR;
 
82
 
 
83
AST_MUTEX_DEFINE_STATIC(usecnt_lock);
75
84
 
76
85
/* Protect the interface list (of vpb_pvt's) */
77
 
static ast_mutex_t iflock = AST_MUTEX_INITIALIZER;
 
86
AST_MUTEX_DEFINE_STATIC(iflock);
78
87
 
79
88
/* Protect the monitoring thread, so only one process can kill or start it, and not
80
89
   when it's doing something critical. */
81
 
static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
 
90
AST_MUTEX_DEFINE_STATIC(monlock);
82
91
 
83
92
/* This is the thread for the monitor which checks for input on the channels
84
93
   which are not currently in use.  */
89
98
 
90
99
static int restart_monitor(void);
91
100
 
92
 
/* The private structures of the VPB channels are linked for
93
 
   selecting outgoing channels */
 
101
/* The private structures of the VPB channels are 
 
102
   linked for selecting outgoing channels */
94
103
   
95
104
#define MODE_DIALTONE   1
96
105
#define MODE_IMMEDIATE  2
97
106
#define MODE_FXO        3
98
107
 
99
 
 
100
 
static VPB_TONE Dialtone     = {440, 440, 440, 0,  0, 0, 5000, 0   };
101
 
static VPB_TONE Busytone     = {440,   0,   0, 0,  -100, -100,   500, 500};
102
 
static VPB_TONE Ringbacktone = {440,   0,   0, 0,  -100, -100,  100, 100};
103
 
 
104
 
 
105
 
#define VPB_MAX_BRIDGES 128 
106
 
static struct vpb_bridge_t {
107
 
     int inuse;
108
 
     struct ast_channel *c0, *c1, **rc;
109
 
     struct ast_frame **fo;
110
 
     int flags;
111
 
     
112
 
     ast_mutex_t lock;
113
 
     pthread_cond_t cond;
114
 
} bridges[VPB_MAX_BRIDGES]; /* Bridges...*/
115
 
 
116
 
static ast_mutex_t bridge_lock = AST_MUTEX_INITIALIZER;
 
108
/* Pick a country or add your own! */
 
109
/* These are the tones that are played to the user */
 
110
#define TONES_AU
 
111
#ifdef TONES_AU
 
112
static VPB_TONE Dialtone     = {440,    440,    440,    -10,    -10,    -10,    5000,   0   };
 
113
static VPB_TONE Busytone     = {470,    0,      0,      -10,    -100,   -100,   5000,   0 };
 
114
static VPB_TONE Ringbacktone = {400,    50,     440,    -10,    -10,    -10,    1400,   800 };
 
115
#endif
 
116
/*
 
117
#define TONES_USA
 
118
#ifdef TONES_USA
 
119
static VPB_TONE Dialtone     = {425,   0,   0, -16,  -100, -100, 10000,    0};
 
120
static VPB_TONE Busytone     = {425,   0,   0, -10,  -100, -100,   500,  500};
 
121
static VPB_TONE Ringbacktone = {400,   425,   450, -20,  -20, -20,  1000, 1000};
 
122
#endif
 
123
*/
 
124
 
 
125
/* grunt tone defn's */
 
126
static VPB_DETECT toned_grunt = { 3, VPB_GRUNT, 1, 2000, 3000, 0, 0, -40, 0, 0, 0, 40, { { VPB_DELAY, 1000, 0, 0 }, { VPB_RISING, 0, 40, 0 }, { 0, 100, 0, 0 } } };
 
127
static VPB_DETECT toned_ungrunt = { 2, VPB_GRUNT, 1, 2000, 1, 0, 0, -40, 0, 0, 30, 40, { { 0, 0, 0, 0 } } };
 
128
 
 
129
/* Use loop drop detection */
 
130
static int UseLoopDrop=1;
 
131
 
 
132
/* To use or not to use Native bridging */
 
133
static int UseNativeBridge=1;
 
134
 
 
135
#define TIMER_PERIOD_RINGBACK 2000
 
136
#define TIMER_PERIOD_BUSY 700
 
137
          
 
138
#define VPB_EVENTS_ALL (VPB_MRING|VPB_MDIGIT|VPB_MDTMF|VPB_MTONEDETECT|VPB_MTIMEREXP|VPB_MPLAY_UNDERFLOW \
 
139
                        |VPB_MRECORD_OVERFLOW|VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
 
140
                        |VPB_MRING_OFF|VPB_MDROP|VPB_MSTATION_FLASH)
 
141
#define VPB_EVENTS_NODROP (VPB_MRING|VPB_MDIGIT|VPB_MDTMF|VPB_MTONEDETECT|VPB_MTIMEREXP|VPB_MPLAY_UNDERFLOW \
 
142
                        |VPB_MRECORD_OVERFLOW|VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
 
143
                        |VPB_MRING_OFF|VPB_MSTATION_FLASH)
 
144
#define VPB_EVENTS_NODTMF (VPB_MRING|VPB_MDIGIT|VPB_MTONEDETECT|VPB_MTIMEREXP|VPB_MPLAY_UNDERFLOW \
 
145
                        |VPB_MRECORD_OVERFLOW|VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
 
146
                        |VPB_MRING_OFF|VPB_MDROP|VPB_MSTATION_FLASH)
 
147
#define VPB_EVENTS_STAT (VPB_MRING|VPB_MDIGIT|VPB_MDTMF|VPB_MTONEDETECT|VPB_MTIMEREXP|VPB_MPLAY_UNDERFLOW \
 
148
                        |VPB_MRECORD_OVERFLOW|VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
 
149
                        |VPB_MRING_OFF|VPB_MSTATION_FLASH)
 
150
 
 
151
 
 
152
// Dialing parameters for Australia
 
153
//#define DIAL_WITH_CALL_PROGRESS
 
154
VPB_TONE_MAP DialToneMap[] = {  { VPB_BUSY_AUST, VPB_CALL_DISCONNECT, 0 },
 
155
                                { VPB_DIAL, VPB_CALL_DIALTONE, 0 },
 
156
                                { VPB_RINGBACK_308, VPB_CALL_RINGBACK, 0 },
 
157
                                { VPB_BUSY_AUST, VPB_CALL_BUSY, 0 },
 
158
                                { VPB_GRUNT, VPB_CALL_GRUNT, 0 },
 
159
                                { 0, 0, 1 } };
 
160
#define VPB_DIALTONE_WAIT 2000 /* Wait up to 2s for a dialtone */
 
161
#define VPB_RINGWAIT 4000 /* Wait up to 4s for ring tone after dialing */
 
162
#define VPB_CONNECTED_WAIT 4000 /* If no ring tone detected for 4s then consider call connected */
 
163
#define TIMER_PERIOD_NOANSWER 120000 /* Let it ring for 120s before deciding theres noone there */
 
164
 
 
165
#define MAX_BRIDGES_V4PCI 2
 
166
#define MAX_BRIDGES_V12PCI 128
 
167
 
 
168
/* port states */
 
169
#define VPB_STATE_ONHOOK        0
 
170
#define VPB_STATE_OFFHOOK       1
 
171
#define VPB_STATE_DIALLING      2
 
172
#define VPB_STATE_JOINED        3
 
173
#define VPB_STATE_GETDTMF       4
 
174
#define VPB_STATE_PLAYDIAL      5
 
175
#define VPB_STATE_PLAYBUSY      6
 
176
#define VPB_STATE_PLAYRING      7
 
177
 
 
178
 
 
179
typedef struct  {
 
180
        int inuse;
 
181
        struct ast_channel *c0, *c1, **rc;
 
182
        struct ast_frame **fo;
 
183
        int flags;
 
184
        ast_mutex_t lock;
 
185
        pthread_cond_t cond;
 
186
        int endbridge;
 
187
} vpb_bridge_t;
 
188
 
 
189
static vpb_bridge_t * bridges;
 
190
static int max_bridges = MAX_BRIDGES_V4PCI;
 
191
 
 
192
AST_MUTEX_DEFINE_STATIC(bridge_lock);
 
193
 
 
194
typedef enum {
 
195
        vpb_model_unknown = 0, 
 
196
        vpb_model_v4pci,
 
197
        vpb_model_v12pci
 
198
} vpb_model_t;
117
199
 
118
200
static struct vpb_pvt {
119
201
 
120
 
     struct ast_channel *owner;         /* Channel we belong to, possibly NULL */
121
 
     int mode;                                          /* Is this in the  */
122
 
     int handle;  /* Handle for vpb interface */
123
 
 
124
 
     char dev[256];
125
 
 
126
 
     struct ast_frame f, fr;
127
 
     char buf[VPB_MAX_BUF];                     /* Static buffer for reading frames */
128
 
     char obuf[VPB_MAX_BUF];
129
 
     
130
 
     int dialtone;
131
 
     float txgain, rxgain;             /* gain control for playing, recording  */
132
 
     
133
 
     int wantdtmf; /* Waiting for DTMF. */
134
 
     int silencesupression;
135
 
     char context[AST_MAX_EXTENSION];
136
 
 
137
 
     char ext[AST_MAX_EXTENSION];
138
 
     char language[MAX_LANGUAGE];
139
 
     char callerid[AST_MAX_EXTENSION];
140
 
 
141
 
     int lastinput;
142
 
     int lastoutput;
143
 
 
144
 
     struct vpb_bridge_t *bridge;
145
 
     void *timer; /* For call timeout. */
146
 
     int calling;
147
 
    
148
 
     int lastgrunt;
149
 
 
150
 
     ast_mutex_t lock;
151
 
 
152
 
    int stopreads; /* Stop reading...*/
153
 
     pthread_t readthread;    /* For monitoring read channel. One per owned channel. */
154
 
 
155
 
     struct vpb_pvt *next;                      /* Next channel in list */
 
202
        ast_mutex_t owner_lock;                 /* Protect blocks that expect ownership to remain the same */
 
203
        struct ast_channel *owner;              /* Channel who owns us, possibly NULL */
 
204
 
 
205
        int golock;                             /* Got owner lock ? */
 
206
 
 
207
        int mode;                               /* fxo/imediate/dialtone*/
 
208
        int handle;                             /* Handle for vpb interface */
 
209
 
 
210
        int state;                              /* used to keep port state (internal to driver) */
 
211
 
 
212
        int group;                              /* Which group this port belongs to */
 
213
 
 
214
        char dev[256];                          /* Device name, eg vpb/1-1 */
 
215
        vpb_model_t vpb_model;                  /* card model */
 
216
 
 
217
        struct ast_frame f, fr;                 /* Asterisk frame interface */
 
218
        char buf[VPB_MAX_BUF];                  /* Static buffer for reading frames */
 
219
 
 
220
        int dialtone;                           /* NOT USED */
 
221
        float txgain, rxgain;                   /* Hardware gain control */
 
222
        float txswgain, rxswgain;               /* Software gain control */
 
223
 
 
224
        int wantdtmf;                           /* Waiting for DTMF. */
 
225
        char context[AST_MAX_EXTENSION];        /* The context for this channel */
 
226
 
 
227
        char ext[AST_MAX_EXTENSION];            /* DTMF buffer for the ext[ens] */
 
228
        char language[MAX_LANGUAGE];            /* language being used */
 
229
        char callerid[AST_MAX_EXTENSION];       /* CallerId used for directly connected phone */
 
230
 
 
231
        int brcallerpos;                        /* Brazilian CallerID detection */
 
232
 
 
233
        int lastoutput;                         /* Holds the last Audio format output'ed */
 
234
        int lastinput;                          /* Holds the last Audio format input'ed */
 
235
        int last_ignore_dtmf;
 
236
 
 
237
        void *busy_timer;                       /* Void pointer for busy vpb_timer */
 
238
        int busy_timer_id;                      /* unique timer ID for busy timer */
 
239
 
 
240
        void *ringback_timer;                   /* Void pointer for ringback vpb_timer */
 
241
        int ringback_timer_id;                  /* unique timer ID for ringback timer */
 
242
 
 
243
        double lastgrunt;                       /* time stamp (secs since epoc) of last grunt event */
 
244
 
 
245
        ast_mutex_t lock;                       /* This one just protects bridge ptr below */
 
246
        vpb_bridge_t *bridge;
 
247
 
 
248
        int stopreads;                          /* Stop reading...*/
 
249
        int read_state;                         /* Read state */
 
250
        int chuck_count;                        /* a count of packets weve chucked away!*/
 
251
        pthread_t readthread;                   /* For monitoring read channel. One per owned channel. */
 
252
 
 
253
        ast_mutex_t record_lock;                /* This one prevents reentering a record_buf block */
 
254
        ast_mutex_t play_lock;                  /* This one prevents reentering a play_buf block */
 
255
        int  play_buf_time;                     /* How long the last play_buf took */
 
256
 
 
257
        ast_mutex_t play_dtmf_lock;
 
258
        char play_dtmf[16];
 
259
 
 
260
        struct vpb_pvt *next;                   /* Next channel in list */
 
261
 
156
262
} *iflist = NULL;
157
263
 
158
 
static char callerid[AST_MAX_EXTENSION];
159
 
 
 
264
static struct ast_channel *vpb_new(struct vpb_pvt *i, int state, char *context);
 
265
static void *do_chanreads(void *pvt);
 
266
 
 
267
// Can't get vpb_bridge() working on v4pci without either a horrible
 
268
// high pitched feedback noise or bad hiss noise depending on gain settings
 
269
// Get asterisk to do the bridging
 
270
#define BAD_V4PCI_BRIDGE
 
271
 
 
272
// This one enables a half duplex bridge which may be required to prevent high pitched
 
273
// feedback when getting asterisk to do the bridging and when using certain gain settings.
 
274
//#define HALF_DUPLEX_BRIDGE
 
275
 
 
276
/* This is the Native bridge code, which Asterisk will try before using its own bridging code */
160
277
static int vpb_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
161
278
{
162
 
     struct vpb_pvt *p0 = (struct vpb_pvt *)c0->pvt->pvt;
163
 
     struct vpb_pvt *p1 = (struct vpb_pvt *)c1->pvt->pvt;
164
 
     int i, len = sizeof bridges/sizeof bridges[0], res;
165
 
     
166
 
     /* Bridge channels, check if we can.  I believe we always can, so find a slot.*/
167
 
     
168
 
     ast_mutex_lock(&bridge_lock); {
169
 
          for (i = 0; i < len; i++) 
170
 
               if (!bridges[i].inuse)
171
 
                    break;
172
 
          if (i < len) {
173
 
               bridges[i].inuse = 1;
174
 
               bridges[i].flags = flags;
175
 
               bridges[i].rc = rc;
176
 
               bridges[i].fo = fo;
177
 
               bridges[i].c0 = c0;
178
 
               bridges[i].c1 = c1;
179
 
               ast_mutex_init(&bridges[i].lock);
180
 
               pthread_cond_init(&bridges[i].cond, NULL);
181
 
          }            
182
 
     } ast_mutex_unlock(&bridge_lock); 
183
 
 
184
 
     if (i == len) {
185
 
          ast_log(LOG_WARNING, "Failed to bridge %s and %s!\n", c0->name, c1->name);
186
 
          return -2;
187
 
     } else {
188
 
 
189
 
          /* Set bridge pointers. You don't want to take these locks while holding bridge lock.*/
190
 
          ast_mutex_lock(&p0->lock); {
191
 
               p0->bridge = &bridges[i];
192
 
          } ast_mutex_unlock(&p0->lock);
193
 
 
194
 
          ast_mutex_lock(&p1->lock); {
195
 
               p1->bridge = &bridges[i];
196
 
          } ast_mutex_unlock(&p1->lock);
197
 
 
198
 
          if (option_verbose > 4) 
199
 
               ast_verbose(VERBOSE_PREFIX_3 
200
 
                           " Bridging call entered with [%s, %s]\n", c0->name, c1->name);
201
 
     }
202
 
     res = vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_ON, 0);
203
 
 
204
 
     if (res != VPB_OK) 
205
 
          goto done;
206
 
 
207
 
     res = pthread_cond_wait(&bridges[i].cond, &bridges[i].lock); /* Wait for condition signal. */
208
 
     
209
 
     
210
 
 done: /* Out of wait. */
211
 
 
212
 
     vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_OFF, 0); 
213
 
 
214
 
     
215
 
     ast_mutex_lock(&bridge_lock); {
216
 
          bridges[i].inuse = 0;
217
 
          ast_mutex_destroy(&bridges[i].lock);
218
 
          pthread_cond_destroy(&bridges[i].cond);       
219
 
     } ast_mutex_unlock(&bridge_lock); 
220
 
     
221
 
     ast_mutex_lock(&p0->lock); {
222
 
          p0->bridge = NULL;
223
 
     } ast_mutex_unlock(&p0->lock);
224
 
     
225
 
     ast_mutex_lock(&p1->lock); {
226
 
          p1->bridge = NULL;
227
 
     } ast_mutex_unlock(&p1->lock);
228
 
 
229
 
     
230
 
     if (option_verbose > 4) 
231
 
          ast_verbose(VERBOSE_PREFIX_3 
232
 
                      " Bridging call done with [%s, %s] => %d\n", c0->name, c1->name, res);
233
 
    
234
 
     if (res != 0 && res != VPB_OK) /* Don't assume VPB_OK is zero! */
235
 
          return -1;
236
 
     else 
237
 
          return 0;
 
279
        struct vpb_pvt *p0 = (struct vpb_pvt *)c0->pvt->pvt;
 
280
        struct vpb_pvt *p1 = (struct vpb_pvt *)c1->pvt->pvt;
 
281
        int i, res;
 
282
 
 
283
        #ifdef BAD_V4PCI_BRIDGE
 
284
        if(p0->vpb_model==vpb_model_v4pci)
 
285
                return -2;
 
286
        #endif
 
287
        if ( UseNativeBridge != 1){
 
288
                return -2;
 
289
        }
 
290
 
 
291
/*
 
292
        ast_mutex_lock(&p0->lock);
 
293
        ast_mutex_lock(&p1->lock);
 
294
*/
 
295
 
 
296
        /* Bridge channels, check if we can.  I believe we always can, so find a slot.*/
 
297
 
 
298
        ast_mutex_lock(&bridge_lock); {
 
299
                for (i = 0; i < max_bridges; i++) 
 
300
                        if (!bridges[i].inuse)
 
301
                                break;
 
302
                if (i < max_bridges) {
 
303
                        bridges[i].inuse = 1;
 
304
                        bridges[i].endbridge = 0;
 
305
                        bridges[i].flags = flags;
 
306
                        bridges[i].rc = rc;
 
307
                        bridges[i].fo = fo;
 
308
                        bridges[i].c0 = c0;
 
309
                        bridges[i].c1 = c1;
 
310
                }              
 
311
        } ast_mutex_unlock(&bridge_lock); 
 
312
 
 
313
        if (i == max_bridges) {
 
314
                ast_log(LOG_WARNING, "Failed to bridge %s and %s!\n", c0->name, c1->name);
 
315
                ast_mutex_unlock(&p0->lock);
 
316
                ast_mutex_unlock(&p1->lock);
 
317
                return -2;
 
318
        } else {
 
319
                /* Set bridge pointers. You don't want to take these locks while holding bridge lock.*/
 
320
                ast_mutex_lock(&p0->lock); {
 
321
                        p0->bridge = &bridges[i];
 
322
                } ast_mutex_unlock(&p0->lock);
 
323
 
 
324
                ast_mutex_lock(&p1->lock); {
 
325
                        p1->bridge = &bridges[i];
 
326
                } ast_mutex_unlock(&p1->lock);
 
327
 
 
328
                if (option_verbose>1) 
 
329
                        ast_verbose(VERBOSE_PREFIX_2 "Bridging call entered with [%s, %s]\n", c0->name, c1->name);
 
330
        }
 
331
 
 
332
        #ifdef HALF_DUPLEX_BRIDGE
 
333
 
 
334
        if (option_verbose>1) 
 
335
                ast_verbose(VERBOSE_PREFIX_2 "Starting half-duplex bridge [%s, %s]\n", c0->name, c1->name);
 
336
 
 
337
        int dir = 0;
 
338
 
 
339
        memset(p0->buf, 0, sizeof p0->buf);
 
340
        memset(p1->buf, 0, sizeof p1->buf);
 
341
 
 
342
        vpb_record_buf_start(p0->handle, VPB_ALAW);
 
343
        vpb_record_buf_start(p1->handle, VPB_ALAW);
 
344
 
 
345
        vpb_play_buf_start(p0->handle, VPB_ALAW);
 
346
        vpb_play_buf_start(p1->handle, VPB_ALAW);
 
347
 
 
348
        while( !bridges[i].endbridge ) {
 
349
                struct vpb_pvt *from, *to;
 
350
                if(++dir%2) {
 
351
                        from = p0;
 
352
                        to = p1;
 
353
                } else {
 
354
                        from = p1;
 
355
                        to = p0;
 
356
                }
 
357
                vpb_record_buf_sync(from->handle, from->buf, VPB_SAMPLES);
 
358
                vpb_play_buf_sync(to->handle, from->buf, VPB_SAMPLES);
 
359
        }
 
360
 
 
361
        vpb_record_buf_finish(p0->handle);
 
362
        vpb_record_buf_finish(p1->handle);
 
363
 
 
364
        vpb_play_buf_finish(p0->handle);
 
365
        vpb_play_buf_finish(p1->handle);
 
366
 
 
367
        if (option_verbose>1) 
 
368
                ast_verbose(VERBOSE_PREFIX_2 "Finished half-duplex bridge [%s, %s]\n", c0->name, c1->name);
 
369
 
 
370
        res = VPB_OK;
 
371
 
 
372
        #else
 
373
 
 
374
        res = vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_ON, i+1 /* resource 1 & 2 only for V4PCI*/ );
 
375
        if (res == VPB_OK) {
 
376
                //pthread_cond_wait(&bridges[i].cond, &bridges[i].lock); /* Wait for condition signal. */
 
377
                while( !bridges[i].endbridge ) {};
 
378
                vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_OFF, i+1 /* resource 1 & 2 only for V4PCI*/ ); 
 
379
        }
 
380
 
 
381
        #endif
 
382
 
 
383
        ast_mutex_lock(&bridge_lock); {
 
384
                bridges[i].inuse = 0;
 
385
        } ast_mutex_unlock(&bridge_lock); 
 
386
 
 
387
        p0->bridge = NULL;
 
388
        p1->bridge = NULL;
 
389
 
 
390
 
 
391
        if (option_verbose>1) 
 
392
                ast_verbose(VERBOSE_PREFIX_2 "Bridging call done with [%s, %s] => %d\n", c0->name, c1->name, res);
 
393
 
 
394
/*
 
395
        ast_mutex_unlock(&p0->lock);
 
396
        ast_mutex_unlock(&p1->lock);
 
397
*/
 
398
        return (res==VPB_OK)?0:-1;
 
399
}
 
400
 
 
401
static double get_time_in_ms()
 
402
{
 
403
        struct timeval tv;
 
404
        gettimeofday(&tv, NULL);
 
405
        return ((double)tv.tv_sec*1000)+((double)tv.tv_usec/1000);
 
406
}
 
407
 
 
408
// Caller ID can be located in different positions between the rings depending on your Telco
 
409
// Australian (Telstra) callerid starts 700ms after 1st ring and finishes 1.5s after first ring
 
410
// Use ANALYSE_CID to record rings and determine location of callerid
 
411
//#define ANALYSE_CID
 
412
#define RING_SKIP 600
 
413
#define CID_MSECS 1700
 
414
 
 
415
static void get_callerid(struct vpb_pvt *p)
 
416
{
 
417
        short buf[CID_MSECS*8]; // 8kHz sampling rate
 
418
        double cid_record_time;
 
419
        int rc;
 
420
 
 
421
        if(strcasecmp(p->callerid, "on")) {
 
422
                p->owner->callerid = strdup("unknown");
 
423
                return;
 
424
        }
 
425
 
 
426
        if( ast_mutex_trylock(&p->record_lock) == 0 ) {
 
427
 
 
428
                char callerid[AST_MAX_EXTENSION] = ""; 
 
429
 
 
430
                cid_record_time = get_time_in_ms();
 
431
                if (option_verbose>3) 
 
432
                        ast_verbose(VERBOSE_PREFIX_4 "CID record - start\n");
 
433
 
 
434
                #ifdef ANALYSE_CID
 
435
                VPB_RECORD record_parms = { "", 8 * 1000 }; // record a couple of rings
 
436
                vpb_record_set(p->handle,&record_parms);
 
437
                ast_verbose(VERBOSE_PREFIX_4 "Recording debug CID sample to /tmp/cid.raw\n");
 
438
                vpb_record_file_sync(p->handle,"/tmp/cid.raw",VPB_LINEAR);
 
439
                rc = 1; // don't try to decode_cid
 
440
                #else   
 
441
                // Skip any trailing ringtone
 
442
                vpb_sleep(RING_SKIP);
 
443
 
 
444
                if (option_verbose>3) 
 
445
                        ast_verbose(VERBOSE_PREFIX_4 "CID record - skipped %fms trailing ring\n",
 
446
                                 get_time_in_ms() - cid_record_time);
 
447
                cid_record_time = get_time_in_ms();
 
448
 
 
449
                // Record bit between the rings which contains the callerid
 
450
                vpb_record_buf_start(p->handle, VPB_LINEAR);
 
451
                rc = vpb_record_buf_sync(p->handle, (char*)buf, sizeof(buf));
 
452
                vpb_record_buf_finish(p->handle);
 
453
                #endif
 
454
 
 
455
                if (option_verbose>3) 
 
456
                        ast_verbose(VERBOSE_PREFIX_4 "CID record - recorded %fms between rings\n", 
 
457
                                get_time_in_ms() - cid_record_time);
 
458
 
 
459
                ast_mutex_unlock(&p->record_lock);
 
460
 
 
461
                if( rc != VPB_OK ) {
 
462
                        ast_log(LOG_ERROR, "Failed to record caller id sample on %s\n", p->dev );
 
463
                        return;
 
464
                }
 
465
 
 
466
                // This decodes FSK 1200baud type callerid
 
467
                if ((rc=vpb_cid_decode(callerid, buf, CID_MSECS*8)) == VPB_OK ) {
 
468
                        if(!*callerid) 
 
469
                                strncpy(callerid,"undisclosed", sizeof(callerid) - 1); // blocked CID (eg caller used 1831)
 
470
                } else {
 
471
                        ast_log(LOG_ERROR, "Failed to decode caller id on %s - %s\n", p->dev, vpb_strerror(rc) );
 
472
                        strncpy(callerid,"unknown", sizeof(callerid) - 1);
 
473
                }
 
474
                p->owner->callerid = strdup(callerid);
 
475
 
 
476
        } else 
 
477
                ast_log(LOG_ERROR, "Failed to set record mode for caller id on %s\n", p->dev );
 
478
}
 
479
 
 
480
// Terminate any tones we are presently playing
 
481
static void stoptone( int handle)
 
482
{
 
483
        int ret;
 
484
        VPB_EVENT je;
 
485
        while(vpb_playtone_state(handle)!=VPB_OK){
 
486
                vpb_tone_terminate(handle);
 
487
                ret = vpb_get_event_ch_async(handle,&je);
 
488
                if ((ret == VPB_OK)&&(je.type != VPB_DIALEND)){
 
489
                        if (option_verbose > 3){
 
490
                                        ast_verbose(VERBOSE_PREFIX_4 "Stop tone collected a wrong event!![%d]\n",je.type);
 
491
                        }
 
492
//                      vpb_put_event(&je);
 
493
                }
 
494
                vpb_sleep(10);
 
495
        }
 
496
}
 
497
 
 
498
/* Safe vpb_playtone_async */
 
499
static int playtone( int handle, VPB_TONE *tone)
 
500
{
 
501
        int ret=VPB_OK;
 
502
        stoptone(handle);
 
503
        if (option_verbose > 3) 
 
504
                ast_verbose(VERBOSE_PREFIX_4 "[%02d]: Playing tone\n", handle);
 
505
        ret = vpb_playtone_async(handle, tone);
 
506
        return ret;
238
507
}
239
508
 
240
509
static inline int monitor_handle_owned(struct vpb_pvt *p, VPB_EVENT *e)
241
510
{
242
 
     struct ast_frame f = {AST_FRAME_CONTROL}; /* default is control, Clear rest. */
243
 
     int endbridge = 0;
244
 
 
245
 
     if (option_verbose > 4) 
246
 
          ast_verbose(VERBOSE_PREFIX_3 " %s handle_owned got event: [%d=>%d]\n",
247
 
                      p->dev, e->type, e->data);
248
 
     
249
 
     f.src = type;
250
 
     switch (e->type) {
251
 
     case VPB_RING:
252
 
          if (p->mode == MODE_FXO) 
253
 
               f.subclass = AST_CONTROL_RING;
254
 
          else
255
 
               f.frametype = -1; /* ignore ring on station port. */
256
 
          break;
257
 
     case VPB_TIMEREXP:
258
 
          if (p->calling) { /* This means time to stop calling. */
259
 
               f.subclass = AST_CONTROL_BUSY;
260
 
               vpb_timer_close(p->timer);
261
 
          } else
262
 
               f.frametype = -1; /* Ignore. */
263
 
          break;
264
 
     case VPB_DTMF:
265
 
          if (p->owner->_state == AST_STATE_UP) {
266
 
               f.frametype = AST_FRAME_DTMF;
267
 
               f.subclass = e->data;
268
 
          } else
269
 
               f.frametype = -1;
270
 
          break;
271
 
 
272
 
     case VPB_TONEDETECT:
273
 
          if (e->data == VPB_BUSY || e->data == VPB_BUSY_308)
274
 
               f.subclass = AST_CONTROL_BUSY;
275
 
          else if (e->data == VPB_GRUNT) {
276
 
               p->lastgrunt = tcounter;
277
 
               f.frametype = -1;
278
 
          } else
279
 
               f.frametype = -1;
280
 
          break;
281
 
 
282
 
     case VPB_CALLEND:
283
 
          if (e->data == VPB_CALL_CONNECTED)
284
 
               f.subclass = AST_CONTROL_ANSWER;
285
 
          else if (e->data == VPB_CALL_NO_DIAL_TONE ||
286
 
                   e->data == VPB_CALL_NO_RING_BACK)
287
 
               f.subclass =  AST_CONTROL_CONGESTION;
288
 
          else if (e->data == VPB_CALL_NO_ANSWER ||
289
 
                   e->data == VPB_CALL_BUSY)
290
 
               f.subclass = AST_CONTROL_BUSY;
291
 
          else if (e->data  == VPB_CALL_DISCONNECTED) 
292
 
               f.subclass = AST_CONTROL_HANGUP;
293
 
          break;
294
 
 
295
 
     case VPB_STATION_OFFHOOK:
296
 
           f.subclass = AST_CONTROL_ANSWER;
297
 
          break;
298
 
          
299
 
     case VPB_STATION_ONHOOK:
300
 
          f.subclass = AST_CONTROL_HANGUP;
301
 
          break;
302
 
          
303
 
     case VPB_STATION_FLASH:
304
 
          f.subclass = AST_CONTROL_FLASH;
305
 
          break;
306
 
        
307
 
     case VPB_DIALEND:
308
 
          f.subclass = AST_CONTROL_ANSWER;
309
 
          break;
310
 
          
311
 
     default:
312
 
          f.frametype = -1;
313
 
          break;
314
 
     }
315
 
 
316
 
     if (option_verbose > 4) 
317
 
          ast_verbose(VERBOSE_PREFIX_3 " handle_owned: putting frame: [%d=>%d], bridge=%p\n",
318
 
                      f.frametype, f.subclass, (void *)p->bridge);
319
 
 
320
 
     ast_mutex_lock(&p->lock); {
321
 
          if (p->bridge) { /* Check what happened, see if we need to report it. */
322
 
               switch (f.frametype) { 
323
 
               case AST_FRAME_DTMF:
324
 
                    if (!(p->bridge->c0 == p->owner && 
325
 
                          (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_0) ) &&
326
 
                        !(p->bridge->c1 == p->owner && 
327
 
                          (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_1) )) 
328
 
                         /* Kill bridge, this is interesting. */
329
 
                         endbridge = 1;
330
 
                    break;
331
 
                    
332
 
               case AST_FRAME_CONTROL:
333
 
                    if (!(p->bridge->flags & AST_BRIDGE_IGNORE_SIGS)) 
334
 
#if 0
335
 
                         if (f.subclass == AST_CONTROL_BUSY ||
336
 
                             f.subclass == AST_CONTROL_CONGESTION ||
337
 
                             f.subclass == AST_CONTROL_HANGUP ||
338
 
                             f.subclass == AST_CONTROL_FLASH)
339
 
#endif
340
 
                              endbridge = 1;
341
 
                    break;
342
 
               default:
343
 
                    
344
 
                    break;
345
 
               }
346
 
               if (endbridge) {
347
 
                    if (p->bridge->fo)
348
 
                         *p->bridge->fo = ast_frisolate(&f);
349
 
                    if (p->bridge->rc)
350
 
                         *p->bridge->rc = p->owner;
351
 
                    
352
 
                    ast_mutex_lock(&p->bridge->lock); {
353
 
                         pthread_cond_signal(&p->bridge->cond);
354
 
                    } ast_mutex_unlock(&p->bridge->lock);                          
355
 
               }          
356
 
          }
357
 
     } ast_mutex_unlock(&p->lock);
358
 
     
359
 
     if (endbridge) return 0;
360
 
     
361
 
     if (f.frametype >= 0 && f.frametype != AST_FRAME_NULL) 
362
 
          ast_queue_frame(p->owner, &f, 0);
363
 
     return 0;
 
511
        struct ast_frame f = {AST_FRAME_CONTROL}; /* default is control, Clear rest. */
 
512
        int endbridge = 0;
 
513
        int res=0;
 
514
 
 
515
        if (option_verbose > 3) 
 
516
                ast_verbose(VERBOSE_PREFIX_4 "%s: handle_owned: got event: [%d=>%d]\n",
 
517
                        p->dev, e->type, e->data);
 
518
 
 
519
        f.src = type;
 
520
        switch (e->type) {
 
521
                case VPB_RING:
 
522
                        if (p->mode == MODE_FXO) {
 
523
                                f.subclass = AST_CONTROL_RING;
 
524
                        } else
 
525
                                f.frametype = -1; /* ignore ring on station port. */
 
526
                        break;
 
527
 
 
528
                case VPB_RING_OFF:
 
529
                        f.frametype = -1;
 
530
                        break;
 
531
 
 
532
                case VPB_TIMEREXP:
 
533
                        if (e->data == p->busy_timer_id) {
 
534
                                playtone(p->handle,&Busytone);
 
535
                                p->state = VPB_STATE_PLAYBUSY;
 
536
                                vpb_timer_stop(p->busy_timer);
 
537
                                vpb_timer_start(p->busy_timer);
 
538
                                f.frametype = -1;
 
539
                        } else if (e->data == p->ringback_timer_id) {
 
540
                                playtone(p->handle, &Ringbacktone);
 
541
                                vpb_timer_stop(p->ringback_timer);
 
542
                                vpb_timer_start(p->ringback_timer);
 
543
                                f.frametype = -1;
 
544
                        } else {
 
545
                                f.frametype = -1; /* Ignore. */
 
546
                        }
 
547
                        break;
 
548
 
 
549
                case VPB_DTMF:
 
550
                        if (p->owner->_state == AST_STATE_UP) {
 
551
                                f.frametype = AST_FRAME_DTMF;
 
552
                                f.subclass = e->data;
 
553
                        } else
 
554
                                f.frametype = -1;
 
555
                        break;
 
556
 
 
557
                case VPB_TONEDETECT:
 
558
                        if (e->data == VPB_BUSY || e->data == VPB_BUSY_308 || e->data == VPB_BUSY_AUST ) {
 
559
                                if (option_verbose > 3) 
 
560
                                        ast_verbose(VERBOSE_PREFIX_4 "%s: handle_owned: got event: BUSY\n", p->dev);
 
561
                                if (p->owner->_state == AST_STATE_UP) {
 
562
                                        f.subclass = AST_CONTROL_HANGUP;
 
563
                                }
 
564
                                else {
 
565
                                        f.subclass = AST_CONTROL_BUSY;
 
566
                                }
 
567
                        } else if (e->data == VPB_GRUNT) {
 
568
                                if( ( get_time_in_ms() - p->lastgrunt ) > gruntdetect_timeout ) {
 
569
                                        // Nothing heard on line for a very long time
 
570
                                        // Timeout connection
 
571
                                        if (option_verbose > 2) 
 
572
                                                ast_verbose(VERBOSE_PREFIX_3 "grunt timeout\n");
 
573
                                        ast_log(LOG_NOTICE,"%s: Line hangup due of lack of conversation\n",p->dev); 
 
574
                                        f.subclass = AST_CONTROL_HANGUP;
 
575
                                } else {
 
576
                                        p->lastgrunt = get_time_in_ms();
 
577
                                        f.frametype = -1;
 
578
                                }
 
579
                        } else
 
580
                                f.frametype = -1;
 
581
                        break;
 
582
 
 
583
                case VPB_CALLEND:
 
584
                        #ifdef DIAL_WITH_CALL_PROGRESS
 
585
                        if (e->data == VPB_CALL_CONNECTED) 
 
586
                                f.subclass = AST_CONTROL_ANSWER;
 
587
                        else if (e->data == VPB_CALL_NO_DIAL_TONE || e->data == VPB_CALL_NO_RING_BACK)
 
588
                                f.subclass =  AST_CONTROL_CONGESTION;
 
589
                        else if (e->data == VPB_CALL_NO_ANSWER || e->data == VPB_CALL_BUSY)
 
590
                                f.subclass = AST_CONTROL_BUSY;
 
591
                        else if (e->data  == VPB_CALL_DISCONNECTED) 
 
592
                                f.subclass = AST_CONTROL_HANGUP;
 
593
                        #else
 
594
                        ast_log(LOG_NOTICE,"%s: Got call progress callback but blind dialing \n", p->dev); 
 
595
                        f.frametype = -1;
 
596
                        #endif
 
597
                        break;
 
598
 
 
599
                case VPB_STATION_OFFHOOK:
 
600
                        f.subclass = AST_CONTROL_ANSWER;
 
601
                        break;
 
602
 
 
603
                case VPB_DROP:
 
604
                        if ((p->mode == MODE_FXO)&&(UseLoopDrop)){ /* ignore loop drop on stations */
 
605
                                if (p->owner->_state == AST_STATE_UP) 
 
606
                                        f.subclass = AST_CONTROL_HANGUP;
 
607
                                else
 
608
                                        f.frametype = -1;
 
609
                        }
 
610
                        break;
 
611
                case VPB_STATION_ONHOOK:
 
612
                        f.subclass = AST_CONTROL_HANGUP;
 
613
                        break;
 
614
 
 
615
                case VPB_STATION_FLASH:
 
616
                        f.subclass = AST_CONTROL_FLASH;
 
617
                        break;
 
618
 
 
619
                // Called when dialing has finished and ringing starts
 
620
                // No indication that call has really been answered when using blind dialing
 
621
                case VPB_DIALEND:
 
622
                        if (p->state < 5){
 
623
                                f.subclass = AST_CONTROL_ANSWER;
 
624
                                if (option_verbose > 1) 
 
625
                                        ast_verbose(VERBOSE_PREFIX_2 "%s: Dialend\n", p->dev);
 
626
                        } else {
 
627
                                f.frametype = -1;
 
628
                        }
 
629
                        break;
 
630
 
 
631
                case VPB_PLAY_UNDERFLOW:
 
632
                        f.frametype = -1;
 
633
                        vpb_reset_play_fifo_alarm(p->handle);
 
634
                        break;
 
635
 
 
636
                case VPB_RECORD_OVERFLOW:
 
637
                        f.frametype = -1;
 
638
                        vpb_reset_record_fifo_alarm(p->handle);
 
639
                        break;
 
640
 
 
641
                default:
 
642
                        f.frametype = -1;
 
643
                        break;
 
644
        }
 
645
 
 
646
        if (option_verbose > 3) 
 
647
                ast_verbose(VERBOSE_PREFIX_4 "%s: handle_owned: putting frame type[%d]subclass[%d], bridge=%p\n",
 
648
                        p->dev, f.frametype, f.subclass, (void *)p->bridge);
 
649
 
 
650
/*
 
651
        if (option_verbose > 3) ast_verbose("%s: LOCKING in handle_owned [%d]\n", p->dev,res);
 
652
        res = ast_mutex_lock(&p->lock); 
 
653
        if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
 
654
*/
 
655
        {
 
656
                if (p->bridge) { /* Check what happened, see if we need to report it. */
 
657
                        switch (f.frametype) {
 
658
                                case AST_FRAME_DTMF:
 
659
                                        if (    !(p->bridge->c0 == p->owner && 
 
660
                                                        (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_0) ) &&
 
661
                                                !(p->bridge->c1 == p->owner && 
 
662
                                                        (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_1) )) 
 
663
                                                /* Kill bridge, this is interesting. */
 
664
                                                endbridge = 1;
 
665
                                        break;
 
666
 
 
667
                                case AST_FRAME_CONTROL:
 
668
                                        if (!(p->bridge->flags & AST_BRIDGE_IGNORE_SIGS)) 
 
669
                                        #if 0
 
670
                                        if (f.subclass == AST_CONTROL_BUSY ||
 
671
                                        f.subclass == AST_CONTROL_CONGESTION ||
 
672
                                        f.subclass == AST_CONTROL_HANGUP ||
 
673
                                        f.subclass == AST_CONTROL_FLASH)
 
674
                                        #endif
 
675
                                                endbridge = 1;
 
676
                                        break;
 
677
 
 
678
                                default:
 
679
                                        break;
 
680
                        }
 
681
                        if (endbridge) {
 
682
                                if (p->bridge->fo)
 
683
                                        *p->bridge->fo = ast_frisolate(&f);
 
684
                                if (p->bridge->rc)
 
685
                                        *p->bridge->rc = p->owner;
 
686
 
 
687
                                ast_mutex_lock(&p->bridge->lock); {
 
688
                                        p->bridge->endbridge = 1;
 
689
                                        pthread_cond_signal(&p->bridge->cond);
 
690
                                } ast_mutex_unlock(&p->bridge->lock);                      
 
691
                        }         
 
692
                }
 
693
        } 
 
694
 
 
695
        if (endbridge){
 
696
                res = ast_mutex_unlock(&p->lock);
 
697
/*
 
698
                if (option_verbose > 3) ast_verbose("%s: unLOCKING in handle_owned [%d]\n", p->dev,res);
 
699
*/
 
700
                return 0;
 
701
        }
 
702
 
 
703
        // Trylock used here to avoid deadlock that can occur if we
 
704
        // happen to be in here handling an event when hangup is called
 
705
        // Problem is that hangup holds p->owner->lock
 
706
        if ((f.frametype >= 0)&& (f.frametype != AST_FRAME_NULL)&&(p->owner)) {
 
707
                if (ast_mutex_trylock(&p->owner->lock)==0)  {
 
708
                        ast_queue_frame(p->owner, &f);
 
709
                        ast_mutex_unlock(&p->owner->lock);
 
710
                } else {
 
711
                        ast_verbose("%s: handled_owned: Missed event %d/%d \n",
 
712
                                p->dev,f.frametype, f.subclass);
 
713
                }
 
714
        }
 
715
        res = ast_mutex_unlock(&p->lock);
 
716
/*
 
717
        if (option_verbose > 3) ast_verbose("%s: unLOCKING in handle_owned [%d]\n", p->dev,res);
 
718
*/
 
719
 
 
720
        return 0;
364
721
}
365
722
 
366
 
static struct ast_channel *vpb_new(struct vpb_pvt *i, int state, char *context);
367
 
 
368
723
static inline int monitor_handle_notowned(struct vpb_pvt *p, VPB_EVENT *e)
369
724
{
370
 
     char s[2] = {0};
371
 
 
372
 
     if (option_verbose > 4) 
373
 
          ast_verbose(VERBOSE_PREFIX_3 " %s: In not owned, mode=%d, [%d=>%d]\n",
374
 
                      p->dev, p->mode, e->type, e->data);
375
 
          
376
 
     switch(e->type) {
377
 
     case VPB_RING:
378
 
          if (p->mode == MODE_FXO) /* FXO port ring, start * */
379
 
               vpb_new(p, AST_STATE_RING, p->context);
380
 
          break;
381
 
     case VPB_STATION_OFFHOOK:
382
 
          if (p->mode == MODE_IMMEDIATE) 
383
 
               vpb_new(p,AST_STATE_RING, p->context);
384
 
          else {
385
 
               vpb_playtone_async(p->handle, &Dialtone);
386
 
               p->wantdtmf = 1;
387
 
               p->ext[0] = 0; /* Just to be sure & paranoid.*/
388
 
          }
389
 
          break;
390
 
 
391
 
     case VPB_STATION_ONHOOK: /*, clear ext */
392
 
           while (vpb_playtone_state(p->handle) != 0){
393
 
                 vpb_tone_terminate(p->handle);
394
 
                 vpb_sleep(10);
395
 
             }
396
 
          p->wantdtmf = 1;
397
 
          p->ext[0] = 0;
398
 
          break;
399
 
 
400
 
     case VPB_DTMF:
401
 
          if (p->wantdtmf == 1) {
402
 
           while (vpb_playtone_state(p->handle) != 0){
403
 
                 vpb_tone_terminate(p->handle);
404
 
                 vpb_sleep(10);
405
 
             }
406
 
 
407
 
               p->wantdtmf = 0;
408
 
          }
409
 
          s[0] = e->data;
410
 
          strcat(p->ext, s);
411
 
          
412
 
          if (ast_exists_extension(NULL, p->context, p->ext, 1, p->callerid)) 
413
 
               vpb_new(p,AST_STATE_RING, p->context);
414
 
          else if (!ast_canmatch_extension(NULL, p->context, p->ext, 1, p->callerid)) 
415
 
               if (ast_exists_extension(NULL, "default", p->ext, 1, p->callerid)) 
416
 
                    vpb_new(p,AST_STATE_RING, "default");             
417
 
               else if (!ast_canmatch_extension(NULL, "default", p->ext, 1, p->callerid)) {
418
 
                    if (option_debug)
419
 
                         ast_log(LOG_DEBUG, 
420
 
                                 "%s can't match anything in %s or default\n", 
421
 
                                 p->ext, p->context);
422
 
                    vpb_playtone_async(p->handle, &Busytone);
423
 
               }
424
 
          
425
 
          break;
426
 
          
427
 
     default:
428
 
          /* Ignore.*/
429
 
          break;
430
 
     }
431
 
     
432
 
     if (option_verbose > 4) 
433
 
         ast_verbose(VERBOSE_PREFIX_3 " %s: Done not owned, mode=%d, [%d=>%d]\n",
434
 
                      p->dev, p->mode, e->type, e->data);
435
 
     
436
 
     return 0;
437
 
 }
 
725
        char s[2] = {0};
 
726
 
 
727
        if (option_verbose > 3) {
 
728
                char str[VPB_MAX_STR];
 
729
                vpb_translate_event(e, str);
 
730
                ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: mode=%d, event[%d][%s]=[%d]\n",
 
731
                        p->dev, p->mode, e->type,str, e->data);
 
732
        }
 
733
 
 
734
        switch(e->type) {
 
735
                case VPB_RING:
 
736
                        if (p->mode == MODE_FXO) /* FXO port ring, start * */ {
 
737
                                vpb_new(p, AST_STATE_RING, p->context);
 
738
                                get_callerid(p);        /* Australian Caller ID only between 1st and 2nd ring */
 
739
                        }
 
740
                        break;
 
741
 
 
742
                case VPB_RING_OFF:
 
743
                        break;
 
744
 
 
745
                case VPB_STATION_OFFHOOK:
 
746
                        if (p->mode == MODE_IMMEDIATE) 
 
747
                                vpb_new(p,AST_STATE_RING, p->context);
 
748
                        else {
 
749
                                ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: playing dialtone\n",p->dev);
 
750
                                playtone(p->handle, &Dialtone);
 
751
                                p->wantdtmf = 1;
 
752
                                p->ext[0] = 0;  /* Just to be sure & paranoid.*/
 
753
                                p->state=VPB_STATE_PLAYDIAL;
 
754
                        }
 
755
                        break;
 
756
 
 
757
                case VPB_DIALEND:
 
758
                        if (p->mode == MODE_DIALTONE){
 
759
                                if (p->state == VPB_STATE_PLAYDIAL) {
 
760
                                        playtone(p->handle, &Dialtone);
 
761
                                        p->wantdtmf = 1;
 
762
                                        p->ext[0] = 0;  // Just to be sure & paranoid.
 
763
                                }
 
764
                                /* These are not needed as they have timers to restart them
 
765
                                else if (p->state == VPB_STATE_PLAYBUSY) {
 
766
                                        playtone(p->handle, &Busytone);
 
767
                                        p->wantdtmf = 1;
 
768
                                        p->ext[0] = 0;  // Just to be sure & paranoid.
 
769
                                }
 
770
                                else if (p->state == VPB_STATE_PLAYRING) {
 
771
                                        playtone(p->handle, &Ringbacktone);
 
772
                                        p->wantdtmf = 1;
 
773
                                        p->ext[0] = 0;  // Just to be sure & paranoid.
 
774
                                }
 
775
                                */
 
776
                        } else {
 
777
                                ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: Got a DIALEND when not really expected\n",p->dev);
 
778
                        }
 
779
                        break;
 
780
 
 
781
                case VPB_STATION_ONHOOK:        /* clear ext */
 
782
                        stoptone(p->handle);
 
783
                        p->wantdtmf = 1 ;
 
784
                        p->ext[0] = 0;
 
785
                        p->state=VPB_STATE_ONHOOK;
 
786
                        break;
 
787
 
 
788
                case VPB_DTMF:
 
789
                        if (p->state == VPB_STATE_ONHOOK){
 
790
                                /* DTMF's being passed while on-hook maybe Caller ID */
 
791
                                break;
 
792
                        }
 
793
                        if (p->wantdtmf == 1) {
 
794
                                stoptone(p->handle);
 
795
                                p->wantdtmf = 0;
 
796
                        }
 
797
                        p->state=VPB_STATE_GETDTMF;
 
798
                        s[0] = e->data;
 
799
                        strncat(p->ext, s, sizeof(p->ext) - strlen(p->ext) - 1);
 
800
                        if (ast_exists_extension(NULL, p->context, p->ext, 1, p->callerid)){
 
801
                                vpb_new(p,AST_STATE_RING, p->context);
 
802
                        } else if (!ast_canmatch_extension(NULL, p->context, p->ext, 1, p->callerid)){
 
803
                                if (ast_exists_extension(NULL, "default", p->ext, 1, p->callerid)) {
 
804
                                        vpb_new(p,AST_STATE_RING, "default");         
 
805
                                } else if (!ast_canmatch_extension(NULL, "default", p->ext, 1, p->callerid)) {
 
806
                                        if (option_verbose > 3) {
 
807
                                                ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: can't match anything in %s or default\n",
 
808
                                                         p->dev, p->context);
 
809
                                        }
 
810
                                        playtone(p->handle, &Busytone);
 
811
                                        vpb_timer_stop(p->busy_timer);
 
812
                                        vpb_timer_start(p->busy_timer);
 
813
                                        p->state = VPB_STATE_PLAYBUSY;
 
814
                                }
 
815
                        }
 
816
                        break;
 
817
 
 
818
                default:
 
819
                        /* Ignore.*/
 
820
                        break;
 
821
        }
 
822
 
 
823
        if (option_verbose > 3) 
 
824
                ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: mode=%d, [%d=>%d]\n",
 
825
                        p->dev, p->mode, e->type, e->data);
 
826
 
 
827
        return 0;
 
828
}
438
829
 
439
830
static void *do_monitor(void *unused)
440
831
{
441
 
    
442
 
     /* Monitor thread, doesn't die until explicitly killed. */
443
 
     
444
 
     if (option_verbose > 4) 
445
 
          ast_verbose(VERBOSE_PREFIX_3 "Starting vpb monitor thread[%ld]\n",
446
 
                      pthread_self());
447
 
     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
448
 
     
449
 
     do {
450
 
          VPB_EVENT e;
451
 
          char str[VPB_MAX_STR];
452
 
          
453
 
          int res = vpb_get_event_sync(&e, VPB_WAIT_TIMEOUT);
454
 
 
455
 
          if (res != VPB_OK)
456
 
               goto end_loop;
457
 
          
458
 
          str[0] = 0;
459
 
          ast_mutex_lock(&monlock),
460
 
               ast_mutex_lock(&iflock); {
461
 
               struct vpb_pvt *p = iflist; /* Find the pvt structure */       
462
 
                
463
 
               vpb_translate_event(&e, str);
464
 
               
465
 
               if (e.type == VPB_NULL_EVENT) 
466
 
                    goto done; /* Nothing to do, just a wakeup call.*/
467
 
               while (p && p->handle != e.handle)
468
 
                    p = p->next;
469
 
               
470
 
               if (option_verbose > 2)
471
 
                    ast_verbose(VERBOSE_PREFIX_3 " Event [%d=>%s] on %s\n", 
472
 
                                e.type, str, p ? p->dev : "null");
473
 
               
474
 
               if (!p) {
475
 
                           ast_log(LOG_WARNING, 
476
 
                                   "Got event %s, no matching iface!\n", str);    
477
 
                           goto done;
478
 
               } 
479
 
               
480
 
               
481
 
               /* Two scenarios: Are you owned or not. */
482
 
               
483
 
               if (p->owner) 
 
832
 
 
833
        /* Monitor thread, doesn't die until explicitly killed. */
 
834
 
 
835
        if (option_verbose > 1) 
 
836
                ast_verbose(VERBOSE_PREFIX_2 "Starting vpb monitor thread[%ld]\n",
 
837
        pthread_self());
 
838
 
 
839
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
840
 
 
841
        for(;;) {
 
842
                VPB_EVENT e;
 
843
                VPB_EVENT je;
 
844
                char str[VPB_MAX_STR];
 
845
                struct vpb_pvt *p;
 
846
 
 
847
                /*
 
848
                if (option_verbose > 3)
 
849
                     ast_verbose(VERBOSE_PREFIX_4 "Monitor waiting for event\n");
 
850
                */
 
851
 
 
852
                int res = vpb_get_event_sync(&e, VPB_WAIT_TIMEOUT);
 
853
                if( (res==VPB_NO_EVENTS) || (res==VPB_TIME_OUT) ){
 
854
                        /*
 
855
                        if (option_verbose > 3){
 
856
                                if (res ==  VPB_NO_EVENTS){
 
857
                                        ast_verbose(VERBOSE_PREFIX_4 "No events....\n");
 
858
                                } else {
 
859
                                        ast_verbose(VERBOSE_PREFIX_4 "No events, timed out....\n");
 
860
                                }
 
861
                        }
 
862
                        */
 
863
                        continue;
 
864
                }
 
865
 
 
866
                if (res != VPB_OK) {
 
867
                        ast_log(LOG_ERROR,"Monitor get event error %s\n", vpb_strerror(res) );
 
868
                        ast_verbose("Monitor get event error %s\n", vpb_strerror(res) );
 
869
                        continue;
 
870
                }
 
871
 
 
872
                str[0] = 0;
 
873
 
 
874
                p = NULL;
 
875
 
 
876
                ast_mutex_lock(&monlock); {
 
877
                        vpb_translate_event(&e, str);
 
878
 
 
879
                        if (e.type == VPB_NULL_EVENT) {
 
880
                                if (option_verbose > 3)
 
881
                                        ast_verbose(VERBOSE_PREFIX_4 "Monitor got null event\n");
 
882
                                goto done; /* Nothing to do, just a wakeup call.*/
 
883
                        }
 
884
                        if (strlen(str)>1){
 
885
                                str[(strlen(str)-1)]='\0';
 
886
                        }
 
887
 
 
888
                        ast_mutex_lock(&iflock); {
 
889
                                p = iflist;
 
890
                                while (p && p->handle != e.handle)
 
891
                                        p = p->next;
 
892
                        } ast_mutex_unlock(&iflock);
 
893
 
 
894
                        if (p && (option_verbose > 3))
 
895
                                ast_verbose(VERBOSE_PREFIX_4 "%s: Event [%d=>%s] \n", 
 
896
                                        p ? p->dev : "null", e.type, str );
 
897
                        done: (void)0;
 
898
 
 
899
                } ast_mutex_unlock(&monlock); 
 
900
 
 
901
                if (!p) {
 
902
                        if (e.type != VPB_NULL_EVENT){
 
903
                                ast_log(LOG_WARNING, "Got event [%s][%d], no matching iface!\n", str,e.type);    
 
904
                                if (option_verbose > 3){
 
905
                                        ast_verbose(VERBOSE_PREFIX_4 "vpb/ERR: No interface for Event [%d=>%s] \n",e.type,str );
 
906
                                }
 
907
                        }
 
908
                        continue;
 
909
                } 
 
910
 
 
911
                /* flush the event from the channel event Q */
 
912
                vpb_get_event_ch_async(e.handle,&je);
 
913
                if (option_verbose > 4){
 
914
                        vpb_translate_event(&je, str);
 
915
                        ast_verbose("%s: Flushing event [%d]=>%s\n",p->dev,je.type,str);
 
916
                }
 
917
 
 
918
                /* Check for ownership and locks */
 
919
                if ((p->owner)&&(!p->golock)){
 
920
                        /* Need to get owner lock */
 
921
                        /* Safely grab both p->lock and p->owner->lock so that there
 
922
                        cannot be a race with something from the other side */
 
923
                        /*
 
924
                        ast_mutex_lock(&p->lock);
 
925
                        while(ast_mutex_trylock(&p->owner->lock)) {
 
926
                                ast_mutex_unlock(&p->lock);
 
927
                                usleep(1);
 
928
                                ast_mutex_lock(&p->lock);
 
929
                                if (!p->owner)
 
930
                                        break;
 
931
                        }
 
932
                        if (p->owner)
 
933
                                p->golock=1;
 
934
                        */
 
935
                }
 
936
                /* Two scenarios: Are you owned or not. */
 
937
                if (p->owner) {
484
938
                        monitor_handle_owned(p, &e);
485
 
               else 
486
 
                    monitor_handle_notowned(p, &e);
487
 
               
488
 
               done: (void)0;
489
 
          } ast_mutex_unlock(&iflock);
490
 
          ast_mutex_unlock(&monlock);
491
 
          
492
 
     end_loop:
493
 
          tcounter += VPB_WAIT_TIMEOUT; /* Ok, not quite but will suffice. */
494
 
          
495
 
     } while(1);
496
 
     
497
 
     
498
 
     return NULL;
 
939
                } else {
 
940
                        monitor_handle_notowned(p, &e);
 
941
                }
 
942
                /* if ((!p->owner)&&(p->golock)){
 
943
                        ast_mutex_unlock(&p->owner->lock);
 
944
                        ast_mutex_unlock(&p->lock);
 
945
                }
 
946
                */
 
947
 
 
948
        }
 
949
 
 
950
        return NULL;
499
951
}
500
952
 
501
953
static int restart_monitor(void)
502
954
{
503
 
     int error = 0;
504
 
     
505
 
     /* If we're supposed to be stopped -- stay stopped */
506
 
     if (mthreadactive == -2)
507
 
          return 0;
508
 
     ast_mutex_lock(&monlock); {
509
 
          if (monitor_thread == pthread_self()) {
510
 
               ast_log(LOG_WARNING, "Cannot kill myself\n");
511
 
               error = -1;
512
 
               goto done;
513
 
          }
514
 
          if (mthreadactive != -1) {
515
 
               /* Why do other drivers kill the thread? No need says I, simply awake thread with event. */
516
 
               VPB_EVENT e;
517
 
               e.handle = 0;
518
 
               e.type = VPB_NULL_EVENT;
519
 
               e.data = 0;
 
955
        int error = 0;
 
956
 
 
957
        /* If we're supposed to be stopped -- stay stopped */
 
958
        if (mthreadactive == -2)
 
959
                return 0;
 
960
 
 
961
        if (option_verbose > 3)
 
962
                ast_verbose(VERBOSE_PREFIX_4 "Restarting monitor\n");
 
963
 
 
964
        ast_mutex_lock(&monlock); {
 
965
                if (monitor_thread == pthread_self()) {
 
966
                        ast_log(LOG_WARNING, "Cannot kill myself\n");
 
967
                        error = -1;
 
968
                        if (option_verbose > 3)
 
969
                                ast_verbose(VERBOSE_PREFIX_4 "Monitor trying to kill monitor\n");
 
970
                        goto done;
 
971
                }
 
972
                if (mthreadactive != -1) {
 
973
                        /* Why do other drivers kill the thread? No need says I, simply awake thread with event. */
 
974
                        VPB_EVENT e;
 
975
                        e.handle = 0;
 
976
                        e.type = VPB_NULL_EVENT;
 
977
                        e.data = 0;
 
978
 
 
979
                        if (option_verbose > 3)
 
980
                                ast_verbose(VERBOSE_PREFIX_4 "Trying to reawake monitor\n");
 
981
 
 
982
                        vpb_put_event(&e);
 
983
                } else {
 
984
                        /* Start a new monitor */
 
985
                        int pid = ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL); 
 
986
                        if (option_verbose > 3)
 
987
                                ast_verbose(VERBOSE_PREFIX_4 "Created new monitor thread %d\n",pid);
 
988
                        if (pid < 0) {
 
989
                                ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
 
990
                                error = -1;
 
991
                                goto done;
 
992
                        } else
 
993
                                mthreadactive = 0; /* Started the thread!*/
 
994
                }
 
995
                done: (void)0;
 
996
        } ast_mutex_unlock(&monlock);
 
997
 
 
998
        if (option_verbose > 3)
 
999
                ast_verbose(VERBOSE_PREFIX_4 "Monitor restarted\n");
 
1000
 
 
1001
        return error;
 
1002
}
 
1003
 
 
1004
// Per board config that must be called after vpb_open()
 
1005
static void mkbrd(vpb_model_t model, int echo_cancel)
 
1006
{
 
1007
        if(!bridges) {
 
1008
                if(model==vpb_model_v4pci) 
 
1009
                        max_bridges = MAX_BRIDGES_V4PCI;
 
1010
                bridges = (vpb_bridge_t *)malloc(max_bridges * sizeof(vpb_bridge_t) );
 
1011
                if(!bridges) 
 
1012
                        ast_log(LOG_ERROR, "Failed to initialize bridges\n");
 
1013
                else {
 
1014
                        memset(bridges,0,max_bridges * sizeof(vpb_bridge_t));
 
1015
                        for(int i = 0; i < max_bridges; i++ ) {
 
1016
                                ast_mutex_init(&bridges[i].lock);
 
1017
                                pthread_cond_init(&bridges[i].cond, NULL);
 
1018
                        }
 
1019
                }
 
1020
        }
 
1021
        if(!echo_cancel) {
 
1022
                if (model==vpb_model_v4pci) {
 
1023
                        vpb_echo_canc_disable();
 
1024
                        ast_log(LOG_NOTICE, "Voicetronix echo cancellation OFF\n");
 
1025
                } 
 
1026
                else {
 
1027
                /* need to it port by port for OpenSwitch*/
 
1028
                }
 
1029
        } else {
 
1030
                if (model==vpb_model_v4pci) {
 
1031
                        vpb_echo_canc_enable();
 
1032
                        ast_log(LOG_NOTICE, "Voicetronix echo cancellation ON\n");
 
1033
                }
 
1034
                else {
 
1035
                /* need to it port by port for OpenSwitch*/
 
1036
                }
 
1037
        }
 
1038
}
 
1039
 
 
1040
struct vpb_pvt *mkif(int board, int channel, int mode, float txgain, float rxgain,
 
1041
                         float txswgain, float rxswgain, int bal1, int bal2, int bal3,
 
1042
                         char * callerid, int echo_cancel, int group )
 
1043
{
 
1044
        struct vpb_pvt *tmp;
 
1045
        char buf[64];
 
1046
 
 
1047
        tmp = (struct vpb_pvt *)calloc(1, sizeof *tmp);
 
1048
 
 
1049
        if (!tmp)
 
1050
                return NULL;
 
1051
 
 
1052
        tmp->handle = vpb_open(board, channel);
 
1053
 
 
1054
        if (tmp->handle < 0) {    
 
1055
                ast_log(LOG_WARNING, "Unable to create channel vpb/%d-%d: %s\n", 
 
1056
                                        board, channel, strerror(errno));
 
1057
                free(tmp);
 
1058
                return NULL;
 
1059
        }
520
1060
               
521
 
               vpb_put_event(&e);
522
 
          } else {
523
 
               /* Start a new monitor */
524
 
               int pid = pthread_create(&monitor_thread, NULL, do_monitor, NULL); 
525
 
               if (pid < 0) {
526
 
                    ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
527
 
                    error = -1;
528
 
                    goto done;
529
 
               } else
530
 
                    mthreadactive = 0; /* Started the thread!*/
531
 
 
532
 
#if 0          
533
 
               if (option_verbose > 4) 
534
 
                    ast_verbose(VERBOSE_PREFIX_3 
535
 
                                "Starting vpb restast: thread[%d]\n",
536
 
                                pid);
537
 
#endif
538
 
          }
539
 
     done: (void)0;
540
 
     } ast_mutex_unlock(&monlock);
541
 
     
542
 
     return error;
543
 
}
544
 
 
545
 
struct vpb_pvt *mkif(int board, int channel, int mode, float txgain, float rxgain)
546
 
{
547
 
     struct vpb_pvt *tmp;
548
 
 
549
 
 
550
 
     tmp = (struct vpb_pvt *)calloc(1, sizeof *tmp);
551
 
 
552
 
     if (!tmp)
553
 
          return NULL;
554
 
 
555
 
     tmp->handle = vpb_open(board, channel);
556
 
     
557
 
     if (tmp->handle < 0) {       
558
 
          ast_log(LOG_WARNING, "Unable to create channel vpb/%d-%d: %s\n",
559
 
                  board, channel, strerror(errno));
560
 
          free(tmp);
561
 
          return NULL;
562
 
     }
563
 
          
564
 
     if (echocancel) {
565
 
          if (option_verbose > 4)
566
 
               ast_verbose(VERBOSE_PREFIX_3 " vpb turned on echo cancel.\n");
567
 
          vpb_echo_canc_enable();
568
 
          vpb_echo_canc_force_adapt_on();
569
 
          echocancel = 0; /* So we do not initialise twice! */
570
 
     }
571
 
     
572
 
     if (option_verbose > 4) 
573
 
          ast_verbose(VERBOSE_PREFIX_3 " vpb created channel: [%d:%d]\n",
574
 
                      board, channel);
575
 
 
576
 
     sprintf(tmp->dev, "vpb/%d-%d", board, channel);
577
 
 
578
 
     tmp->mode = mode;
579
 
 
580
 
     strcpy(tmp->language, language);
581
 
     strcpy(tmp->context, context);
582
 
     
583
 
     strcpy(tmp->callerid, callerid);
584
 
     tmp->txgain = txgain;
585
 
 
586
 
     tmp->rxgain = rxgain;
587
 
 
588
 
     tmp->lastinput = -1;
589
 
     tmp->lastoutput = -1;
590
 
 
591
 
     tmp->bridge = NULL;
592
 
 
593
 
     tmp->readthread = 0;
594
 
 
595
 
     ast_mutex_init(&tmp->lock);
596
 
 
597
 
     if (setrxgain)      
598
 
          vpb_record_set_gain(tmp->handle, rxgain);
599
 
     if (settxgain)  
600
 
          vpb_play_set_gain(tmp->handle, txgain);
601
 
     
602
 
     return tmp;
603
 
}
604
 
 
 
1061
        snprintf(tmp->dev, sizeof(tmp->dev), "vpb/%d-%d", board, channel);
 
1062
 
 
1063
        tmp->mode = mode;
 
1064
 
 
1065
        tmp->group = group;
 
1066
 
 
1067
 
 
1068
        strncpy(tmp->language, language, sizeof(tmp->language) - 1);
 
1069
        strncpy(tmp->context, context, sizeof(tmp->context) - 1);
 
1070
 
 
1071
        if(callerid) { 
 
1072
                strncpy(tmp->callerid, callerid, sizeof(tmp->callerid) - 1);
 
1073
                free(callerid);
 
1074
        } else {
 
1075
                strncpy(tmp->callerid, "unknown", sizeof(tmp->callerid) - 1);
 
1076
        }
 
1077
 
 
1078
        /* check if codec balances have been set in the config file */
 
1079
        if (bal3>=0) {
 
1080
                if ((bal1>=0) && !(bal1 & 32)) bal1 |= 32;
 
1081
                        vpb_set_codec_reg(tmp->handle, 0x42, bal3);
 
1082
        }
 
1083
        if(bal1>=0) vpb_set_codec_reg(tmp->handle, 0x32, bal1);
 
1084
        if(bal2>=0) vpb_set_codec_reg(tmp->handle, 0x3a, bal2);
 
1085
 
 
1086
        tmp->txgain = txgain;
 
1087
        vpb_play_set_hw_gain(tmp->handle, (txgain > MAX_VPB_GAIN) ? MAX_VPB_GAIN : txgain);
 
1088
 
 
1089
        tmp->rxgain = rxgain;
 
1090
        vpb_record_set_hw_gain(tmp->handle, (rxgain > MAX_VPB_GAIN) ? MAX_VPB_GAIN : rxgain);
 
1091
 
 
1092
        tmp->txswgain = txswgain;
 
1093
        vpb_play_set_gain(tmp->handle, (txswgain > MAX_VPB_GAIN) ? MAX_VPB_GAIN : txswgain);
 
1094
 
 
1095
        tmp->rxswgain = rxswgain;
 
1096
        vpb_record_set_gain(tmp->handle, (rxswgain > MAX_VPB_GAIN) ? MAX_VPB_GAIN : rxswgain);
 
1097
 
 
1098
        tmp->vpb_model = vpb_model_unknown;
 
1099
        if( vpb_get_model(buf) == VPB_OK ) {
 
1100
                if(strcmp(buf,"V12PCI")==0) 
 
1101
                        tmp->vpb_model = vpb_model_v12pci;
 
1102
                else if(strcmp(buf,"VPB4")==0) 
 
1103
                        tmp->vpb_model = vpb_model_v4pci;
 
1104
        }
 
1105
 
 
1106
        ast_mutex_init(&tmp->owner_lock);
 
1107
        ast_mutex_init(&tmp->lock);
 
1108
        ast_mutex_init(&tmp->record_lock);
 
1109
        ast_mutex_init(&tmp->play_lock);
 
1110
        ast_mutex_init(&tmp->play_dtmf_lock);
 
1111
 
 
1112
        /* set default read state */
 
1113
        tmp->read_state = 0;
 
1114
        
 
1115
        tmp->golock=0;
 
1116
 
 
1117
        tmp->busy_timer_id = vpb_timer_get_unique_timer_id();
 
1118
        vpb_timer_open(&tmp->busy_timer, tmp->handle, tmp->busy_timer_id, TIMER_PERIOD_BUSY);
 
1119
 
 
1120
        tmp->ringback_timer_id = vpb_timer_get_unique_timer_id();
 
1121
        vpb_timer_open(&tmp->ringback_timer, tmp->handle, tmp->ringback_timer_id, TIMER_PERIOD_RINGBACK);
 
1122
              
 
1123
        if (mode == MODE_FXO){
 
1124
                vpb_set_event_mask(tmp->handle, VPB_EVENTS_ALL );
 
1125
        }
 
1126
        else {
 
1127
                vpb_set_event_mask(tmp->handle, VPB_EVENTS_STAT );
 
1128
        }
 
1129
 
 
1130
        if ((tmp->vpb_model == vpb_model_v12pci) && (echo_cancel)){
 
1131
                vpb_hostecho_on(tmp->handle);
 
1132
        }
 
1133
 
 
1134
        /* define grunt tone */
 
1135
        vpb_settonedet(tmp->handle,&toned_ungrunt);
 
1136
 
 
1137
        ast_log(LOG_NOTICE,"Voicetronix %s channel %s initialized (rxgain=%f txgain=%f) (%f/%f/0x%X/0x%X/0x%X)\n",
 
1138
                (tmp->vpb_model==vpb_model_v4pci)?"V4PCI":
 
1139
                (tmp->vpb_model==vpb_model_v12pci)?"V12PCI":"[Unknown model]",
 
1140
                tmp->dev, tmp->rxswgain, tmp->txswgain, tmp->rxgain, tmp->txgain, bal1, bal2, bal3 );
 
1141
 
 
1142
        return tmp;
 
1143
}
605
1144
 
606
1145
static int vpb_indicate(struct ast_channel *ast, int condition)
607
1146
{
608
 
    struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
609
 
    int res = 0;
610
 
 
611
 
    if (option_verbose > 4)
612
 
        ast_verbose(VERBOSE_PREFIX_3 " vpb indicate on %s with %d\n",
613
 
                    p->dev, condition);
614
 
 
615
 
    switch(condition) {
616
 
        case AST_CONTROL_BUSY:
617
 
        case AST_CONTROL_CONGESTION:
618
 
          while (vpb_playtone_state(p->handle) != 0){
619
 
                res = vpb_tone_terminate(p->handle);
620
 
                vpb_sleep(10);
621
 
            }
622
 
            res = vpb_playtone_async(p->handle, &Busytone);
623
 
            break;
624
 
        case AST_CONTROL_RINGING:
625
 
          while (vpb_playtone_state(p->handle) != 0){
626
 
                res = vpb_tone_terminate(p->handle);
627
 
                vpb_sleep(10);
628
 
            }
629
 
            res = vpb_playtone_async(p->handle, &Ringbacktone);
630
 
            break;          
631
 
        case AST_CONTROL_ANSWER:
632
 
        case -1: /* -1 means stop playing? */
633
 
           while (vpb_playtone_state(p->handle) != 0){
634
 
                 res = vpb_tone_terminate(p->handle);
635
 
                 vpb_sleep(10);
636
 
           }
637
 
 
638
 
            break;
639
 
        case AST_CONTROL_HANGUP:
640
 
          while (vpb_playtone_state(p->handle) != 0){
641
 
                res = vpb_tone_terminate(p->handle);
642
 
                vpb_sleep(10);
643
 
            }
644
 
            res = vpb_playtone_async(p->handle, &Busytone);
645
 
            break;
646
 
            
647
 
        default:
648
 
            res = 0;
649
 
            break;
650
 
    }
651
 
    return res;
 
1147
        struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
 
1148
        int res = 0;
 
1149
        int tmp = 0;
 
1150
 
 
1151
        if (option_verbose > 3)
 
1152
                ast_verbose(VERBOSE_PREFIX_4 "%s: vpb_indicate [%d] state[%d]\n", p->dev, condition,ast->_state);
 
1153
/*
 
1154
        if (ast->_state != AST_STATE_UP) {
 
1155
                ast_verbose(VERBOSE_PREFIX_4 "%s: vpb_indicate Not in AST_STATE_UP\n", p->dev, condition,ast->_state);
 
1156
                return res;
 
1157
        }
 
1158
*/
 
1159
 
 
1160
/*
 
1161
        if (option_verbose > 3) ast_verbose("%s: LOCKING in indicate \n", p->dev);
 
1162
        if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
 
1163
*/
 
1164
        ast_mutex_lock(&p->lock);
 
1165
        switch(condition) {
 
1166
                case AST_CONTROL_BUSY:
 
1167
                case AST_CONTROL_CONGESTION:
 
1168
                        if (ast->_state == AST_STATE_UP) {
 
1169
                                playtone(p->handle, &Busytone);
 
1170
                                p->state = VPB_STATE_PLAYBUSY;
 
1171
                                vpb_timer_stop(p->busy_timer); 
 
1172
                                vpb_timer_start(p->busy_timer); 
 
1173
                        }
 
1174
                        break;
 
1175
                case AST_CONTROL_RINGING:
 
1176
                        if (ast->_state == AST_STATE_UP) {
 
1177
                                playtone(p->handle, &Ringbacktone);
 
1178
                                p->state = VPB_STATE_PLAYRING;
 
1179
                                if (option_verbose > 3)
 
1180
                                        ast_verbose(VERBOSE_PREFIX_4 "%s: vpb indicate: setting ringback timer [%d]\n", p->dev,p->ringback_timer_id);
 
1181
                                
 
1182
                                vpb_timer_stop(p->ringback_timer);
 
1183
                                vpb_timer_start(p->ringback_timer);
 
1184
                        }
 
1185
                        break;      
 
1186
                case AST_CONTROL_ANSWER:
 
1187
                case -1: /* -1 means stop playing? */
 
1188
                        vpb_timer_stop(p->ringback_timer);
 
1189
                        vpb_timer_stop(p->busy_timer);
 
1190
                        stoptone(p->handle);
 
1191
                        break;
 
1192
                case AST_CONTROL_HANGUP:
 
1193
                        if (ast->_state == AST_STATE_UP) {
 
1194
                                playtone(p->handle, &Busytone);
 
1195
                                p->state = VPB_STATE_PLAYBUSY;
 
1196
                                vpb_timer_stop(p->busy_timer);
 
1197
                                vpb_timer_start(p->busy_timer);
 
1198
                        }
 
1199
                        break;
 
1200
 
 
1201
                default:
 
1202
                        res = 0;
 
1203
                        break;
 
1204
        }
 
1205
        tmp = ast_mutex_unlock(&p->lock);
 
1206
/*
 
1207
        if (option_verbose > 3) ast_verbose("%s: unLOCKING in indicate [%d]\n", p->dev,tmp);
 
1208
*/
 
1209
        return res;
652
1210
}
653
1211
 
654
1212
static int vpb_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
655
1213
{
656
1214
        struct vpb_pvt *p = (struct vpb_pvt *)newchan->pvt->pvt;
657
 
 
658
 
        ast_log(LOG_DEBUG, 
659
 
                "New owner for channel %s is %s\n", p->dev, newchan->name);
660
 
        if (p->owner == oldchan)
 
1215
        int res = 0;
 
1216
 
 
1217
/*
 
1218
        if (option_verbose > 3) ast_verbose("%s: LOCKING in fixup \n", p->dev);
 
1219
        if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
 
1220
*/
 
1221
        ast_mutex_lock(&p->lock);
 
1222
        ast_log(LOG_DEBUG, "New owner for channel %s is %s\n", p->dev, newchan->name);
 
1223
 
 
1224
        if (p->owner == oldchan) {
661
1225
                p->owner = newchan;
 
1226
        }
662
1227
 
663
1228
        if (newchan->_state == AST_STATE_RINGING) 
664
1229
                vpb_indicate(newchan, AST_CONTROL_RINGING);
665
1230
 
 
1231
        res= ast_mutex_unlock(&p->lock);
 
1232
/*
 
1233
        if (option_verbose > 3) ast_verbose("%s: unLOCKING in fixup [%d]\n", p->dev,res);
 
1234
*/
666
1235
        return 0;
667
1236
}
668
1237
 
669
1238
static int vpb_digit(struct ast_channel *ast, char digit)
670
1239
{
671
 
    char s[2];
672
 
 
673
 
    s[0] = digit;
674
 
    s[1] = '\0';
675
 
   
676
 
    return vpb_dial_sync(((struct vpb_pvt *)ast->pvt->pvt)->handle, s);
 
1240
        struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
 
1241
        char s[2];
 
1242
        int res = 0;
 
1243
 
 
1244
/*
 
1245
        if (option_verbose > 3) ast_verbose("%s: LOCKING in digit \n", p->dev);
 
1246
        if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
 
1247
*/
 
1248
        ast_mutex_lock(&p->lock);
 
1249
 
 
1250
 
 
1251
        s[0] = digit;
 
1252
        s[1] = '\0';
 
1253
 
 
1254
        if (option_verbose > 3)
 
1255
                ast_verbose(VERBOSE_PREFIX_4 "%s: play digit[%s]\n", p->dev, s);
 
1256
 
 
1257
        ast_mutex_lock(&p->play_dtmf_lock);
 
1258
        strncat(p->play_dtmf,s,sizeof(*p->play_dtmf));
 
1259
        ast_mutex_unlock(&p->play_dtmf_lock);
 
1260
 
 
1261
        res = ast_mutex_unlock(&p->lock);
 
1262
/*
 
1263
        if (option_verbose > 3) ast_verbose("%s: unLOCKING in digit [%d]\n", p->dev,res);
 
1264
*/
 
1265
        return 0;
677
1266
}
678
1267
 
679
 
 
 
1268
/* Places a call out of a VPB channel */
680
1269
static int vpb_call(struct ast_channel *ast, char *dest, int timeout)
681
1270
{
682
 
    struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
683
 
    int res = 0;
684
 
    char *s = strrchr(dest, '/');
685
 
 
686
 
    if (s)
687
 
         s = s + 1;
688
 
    else
689
 
         s = dest;
690
 
 
691
 
    if (ast->_state != AST_STATE_DOWN && ast->_state != AST_STATE_RESERVED) {
692
 
        ast_log(LOG_WARNING, "vpb_call on %s neither down nor reserved!\n", 
693
 
                ast->name);
694
 
        return -1;
695
 
    }
696
 
    if (p->mode != MODE_FXO)  /* Station port, ring it. */
697
 
        res = vpb_ring_station_async(p->handle, VPB_RING_STATION_ON,'1');       
698
 
     else {
699
 
          VPB_CALL call;
700
 
 
701
 
          vpb_get_call(p->handle, &call);
702
 
          
703
 
          call.dialtone_timeout = VPB_DIALTONE_WAIT;
704
 
          call.answer_timeout = timeout;
705
 
          call.ringback_timeout = VPB_RINGWAIT;
706
 
 
707
 
          vpb_set_call(p->handle, &call);
708
 
 
709
 
          if (option_verbose > 2)
710
 
               ast_verbose(VERBOSE_PREFIX_3 " Calling %s on %s \n", 
711
 
                           dest, ast->name); 
712
 
 
713
 
          vpb_sethook_sync(p->handle,VPB_OFFHOOK);
714
 
          
715
 
          res = vpb_dial_async(p->handle, s);
716
 
 
717
 
          if (res != VPB_OK) {
718
 
            ast_log(LOG_DEBUG, "Call on %s to %s failed: %s\n", 
719
 
                    ast->name, dest, vpb_strerror(res));              
720
 
            res = -1;
721
 
          } else 
722
 
            res = 0;
723
 
    }
724
 
 
725
 
    if (option_verbose > 2)
726
 
        ast_verbose(VERBOSE_PREFIX_3 
727
 
                    " VPB Calling %s [t=%d] on %s returned %d\n", 
728
 
                    dest, timeout, ast->name, res); 
729
 
 
730
 
    if (res == 0) {
731
 
         if (timeout) {
732
 
              vpb_timer_open(&p->timer, p->handle, 0, 100*timeout);
733
 
              vpb_timer_start(p->timer);
734
 
         }
735
 
         p->calling = 1;
736
 
        ast_setstate(ast, AST_STATE_RINGING);
737
 
        ast_queue_control(ast,AST_CONTROL_RINGING, 0);          
738
 
    }
739
 
 
740
 
    return res;
 
1271
        struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
 
1272
        int res = 0,i;
 
1273
        char *s = strrchr(dest, '/');
 
1274
        char dialstring[254] = "";
 
1275
        int tmp = 0;
 
1276
 
 
1277
/*
 
1278
        if (option_verbose > 3) ast_verbose("%s: LOCKING in call \n", p->dev);
 
1279
        if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
 
1280
*/
 
1281
        ast_mutex_lock(&p->lock);
 
1282
 
 
1283
        if (s)
 
1284
                s = s + 1;
 
1285
        else
 
1286
                s = dest;
 
1287
        strncpy(dialstring, s, sizeof(dialstring) - 1);
 
1288
        for (i=0; dialstring[i] != '\0' ; i++) {
 
1289
                if ((dialstring[i] == 'w') || (dialstring[i] == 'W'))
 
1290
                        dialstring[i] = ',';
 
1291
                else if ((dialstring[i] == 'f') || (dialstring[i] == 'F'))
 
1292
                        dialstring[i] = '&';
 
1293
        }       
 
1294
        if (option_verbose > 3)
 
1295
                ast_verbose(VERBOSE_PREFIX_4 "%s: starting call\n", p->dev);
 
1296
 
 
1297
        if (ast->_state != AST_STATE_DOWN && ast->_state != AST_STATE_RESERVED) {
 
1298
                ast_log(LOG_WARNING, "vpb_call on %s neither down nor reserved!\n", ast->name);
 
1299
                tmp = ast_mutex_unlock(&p->lock);
 
1300
/*
 
1301
                if (option_verbose > 3) ast_verbose("%s: unLOCKING in call [%d]\n", p->dev,tmp);
 
1302
*/
 
1303
                return -1;
 
1304
        }
 
1305
        if (p->mode != MODE_FXO)  /* Station port, ring it. */
 
1306
                res = vpb_ring_station_async(p->handle, VPB_RING_STATION_ON,0);       
 
1307
        else {
 
1308
                VPB_CALL call;
 
1309
 
 
1310
                // Dial must timeout or it can leave channels unuseable
 
1311
                if( timeout == 0 )
 
1312
                        timeout = TIMER_PERIOD_NOANSWER;
 
1313
                else 
 
1314
                        timeout = timeout * 1000; //convert from secs to ms.
 
1315
 
 
1316
                // These timeouts are only used with call progress dialing
 
1317
                call.dialtones = 1; // Number of dialtones to get outside line
 
1318
                call.dialtone_timeout = VPB_DIALTONE_WAIT; // Wait this long for dialtone (ms)
 
1319
                call.ringback_timeout = VPB_RINGWAIT; // Wait this long for ringing after dialing (ms)
 
1320
                call.inter_ringback_timeout = VPB_CONNECTED_WAIT; // If ringing stops for this long consider it connected (ms)
 
1321
                call.answer_timeout = timeout; // Time to wait for answer after ringing starts (ms)
 
1322
                memcpy( &call.tone_map,  DialToneMap, sizeof(DialToneMap) );
 
1323
                vpb_set_call(p->handle, &call);
 
1324
 
 
1325
                if (option_verbose > 1)
 
1326
                        ast_verbose(VERBOSE_PREFIX_2 "%s: Calling %s on %s \n",p->dev, dialstring, ast->name); 
 
1327
 
 
1328
                if (option_verbose > 2) {
 
1329
                        int j;
 
1330
                        ast_verbose(VERBOSE_PREFIX_2 "%s: Dial parms for %s %d/%dms/%dms/%dms/%dms\n", p->dev
 
1331
                                , ast->name, call.dialtones, call.dialtone_timeout
 
1332
                                , call.ringback_timeout, call.inter_ringback_timeout
 
1333
                                , call.answer_timeout );
 
1334
                        for( j=0; !call.tone_map[j].terminate; j++ )
 
1335
                                ast_verbose(VERBOSE_PREFIX_2 "%s: Dial parms for %s tone %d->%d\n", p->dev,
 
1336
                                        ast->name, call.tone_map[j].tone_id, call.tone_map[j].call_id); 
 
1337
                }
 
1338
 
 
1339
                if (option_verbose > 4)
 
1340
                                ast_verbose("%s: Disabling Loop Drop detection\n",p->dev);
 
1341
                vpb_disable_event(p->handle, VPB_MDROP);
 
1342
                vpb_sethook_sync(p->handle,VPB_OFFHOOK);
 
1343
                p->state=VPB_STATE_OFFHOOK;
 
1344
 
 
1345
                #ifndef DIAL_WITH_CALL_PROGRESS
 
1346
                vpb_sleep(300);
 
1347
                if (option_verbose > 4)
 
1348
                                ast_verbose("%s: Disabling Loop Drop detection\n",p->dev);
 
1349
                vpb_enable_event(p->handle, VPB_MDROP);
 
1350
                res = vpb_dial_async(p->handle, dialstring);
 
1351
                #else
 
1352
                if (option_verbose > 4)
 
1353
                                ast_verbose("%s: Disabling Loop Drop detection\n",p->dev);
 
1354
                vpb_enable_event(p->handle, VPB_MDROP);
 
1355
                res = vpb_call_async(p->handle, dialstring);
 
1356
                #endif
 
1357
 
 
1358
                if (res != VPB_OK) {
 
1359
                        ast_log(LOG_DEBUG, "Call on %s to %s failed: %s\n", ast->name, s, vpb_strerror(res));         
 
1360
                        res = -1;
 
1361
                } else 
 
1362
                        res = 0;
 
1363
        }
 
1364
 
 
1365
        if (option_verbose > 2)
 
1366
                ast_verbose(VERBOSE_PREFIX_3 "%s: VPB Calling %s [t=%d] on %s returned %d\n",p->dev , s, timeout, ast->name, res); 
 
1367
        if (res == 0) {
 
1368
                ast_setstate(ast, AST_STATE_RINGING);
 
1369
                ast_queue_control(ast,AST_CONTROL_RINGING);             
 
1370
        }
 
1371
 
 
1372
        if (!p->readthread){
 
1373
                ast_pthread_create(&p->readthread, NULL, do_chanreads, (void *)p);
 
1374
        }
 
1375
 
 
1376
        tmp = ast_mutex_unlock(&p->lock);
 
1377
/*
 
1378
        if (option_verbose > 3) ast_verbose("%s: unLOCKING in call [%d]\n", p->dev,tmp);
 
1379
*/
 
1380
        return res;
741
1381
}
742
1382
 
743
1383
static int vpb_hangup(struct ast_channel *ast)
744
1384
{
745
 
    struct vpb_pvt *p;
746
 
 
747
 
    if (option_verbose > 2) 
748
 
        ast_verbose(VERBOSE_PREFIX_3 " hangup on vpb (%s)\n", ast->name);
749
 
    
750
 
    if (!ast->pvt || !ast->pvt->pvt) {
751
 
        ast_log(LOG_WARNING, "channel (%s) not connected?\n", ast->name);
 
1385
        struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
 
1386
        VPB_EVENT je;
 
1387
        char str[VPB_MAX_STR];
 
1388
        int res =0 ;
 
1389
 
 
1390
/*
 
1391
        if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup \n", p->dev);
 
1392
        if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
 
1393
        if (option_verbose > 3) ast_verbose("%s: LOCKING pthread_self(%d)\n", p->dev,pthread_self());
 
1394
        ast_mutex_lock(&p->lock);
 
1395
*/
 
1396
        if (option_verbose > 1) 
 
1397
                ast_verbose(VERBOSE_PREFIX_2 "%s: Hangup requested\n", ast->name);
 
1398
 
 
1399
        if (!ast->pvt || !ast->pvt->pvt) {
 
1400
                ast_log(LOG_WARNING, "%s: channel not connected?\n", ast->name);
 
1401
                res = ast_mutex_unlock(&p->lock);
 
1402
/*
 
1403
                if (option_verbose > 3) ast_verbose("%s: unLOCKING in hangup [%d]\n", p->dev,res);
 
1404
*/
 
1405
                return 0;
 
1406
        }
 
1407
 
 
1408
 
 
1409
 
 
1410
        /* Stop record */
 
1411
        p->stopreads = 1;
 
1412
        if( p->readthread ){
 
1413
                pthread_join(p->readthread, NULL); 
 
1414
                if(option_verbose>3) 
 
1415
                        ast_verbose( VERBOSE_PREFIX_4 "%s: stopped record thread on %s\n",ast->name,p->dev);
 
1416
        }
 
1417
 
 
1418
        /* Stop play */
 
1419
        if (p->lastoutput != -1) {
 
1420
                if(option_verbose>1) 
 
1421
                        ast_verbose( VERBOSE_PREFIX_2 "%s: Ending play mode on %s\n",ast->name,p->dev);
 
1422
                vpb_play_terminate(p->handle);
 
1423
                ast_mutex_lock(&p->play_lock); {
 
1424
                        vpb_play_buf_finish(p->handle);
 
1425
                } ast_mutex_unlock(&p->play_lock);
 
1426
        }
 
1427
 
 
1428
        if(option_verbose>3) 
 
1429
                ast_verbose( VERBOSE_PREFIX_4 "%s: Setting state down\n",ast->name);
 
1430
        ast_setstate(ast,AST_STATE_DOWN);
 
1431
 
 
1432
 
 
1433
/*
 
1434
        if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup \n", p->dev);
 
1435
        if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
 
1436
        if (option_verbose > 3) ast_verbose("%s: LOCKING pthread_self(%d)\n", p->dev,pthread_self());
 
1437
*/
 
1438
        ast_mutex_lock(&p->lock);
 
1439
 
 
1440
        if (p->mode != MODE_FXO) { 
 
1441
                /* station port. */
 
1442
                vpb_ring_station_async(p->handle, VPB_RING_STATION_OFF,0);      
 
1443
                if(p->state!=VPB_STATE_ONHOOK){
 
1444
                        /* This is causing a "dial end" "play tone" loop
 
1445
                        playtone(p->handle, &Busytone); 
 
1446
                        p->state = VPB_STATE_PLAYBUSY;
 
1447
                        if(option_verbose>4) 
 
1448
                                ast_verbose( VERBOSE_PREFIX_4 "%s: Station offhook[%d], playing busy tone\n",
 
1449
                                                                ast->name,p->state);
 
1450
                        */
 
1451
                }
 
1452
                else {
 
1453
                        stoptone(p->handle);
 
1454
                }
 
1455
        } else {
 
1456
                stoptone(p->handle); // Terminates any dialing
 
1457
                vpb_sethook_sync(p->handle, VPB_ONHOOK);
 
1458
                p->state=VPB_STATE_ONHOOK;
 
1459
        }
 
1460
        while (VPB_OK==vpb_get_event_ch_async(p->handle,&je)){
 
1461
                if(option_verbose>3) {
 
1462
                        vpb_translate_event(&je, str);
 
1463
                        ast_verbose( VERBOSE_PREFIX_4 "%s: Flushing event [%d]=>%s\n",ast->name,je.type,str);
 
1464
                }
 
1465
        }
 
1466
 
 
1467
        p->readthread = 0;
 
1468
        p->lastoutput = -1;
 
1469
        p->lastinput = -1;
 
1470
        p->last_ignore_dtmf = 1;
 
1471
        p->ext[0]  = 0;
 
1472
        p->dialtone = 0;
 
1473
 
 
1474
        p->owner = NULL;
 
1475
        ast->pvt->pvt=NULL;
 
1476
 
 
1477
        ast_mutex_lock(&usecnt_lock); {
 
1478
                usecnt--;
 
1479
        } ast_mutex_unlock(&usecnt_lock);
 
1480
        ast_update_use_count();
 
1481
 
 
1482
        if (option_verbose > 1)
 
1483
                ast_verbose(VERBOSE_PREFIX_2 "%s: Hangup complete\n", ast->name);
 
1484
 
 
1485
        restart_monitor();
 
1486
/*
 
1487
        if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
 
1488
*/
 
1489
        res = ast_mutex_unlock(&p->lock);
 
1490
/*
 
1491
        if (option_verbose > 3) ast_verbose("%s: unLOCKING in hangup [%d]\n", p->dev,res);
 
1492
        if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
 
1493
*/
752
1494
        return 0;
753
 
    }
754
 
 
755
 
    p = (struct vpb_pvt *)ast->pvt->pvt;
756
 
 
757
 
    vpb_play_terminate(p->handle);
758
 
    vpb_record_terminate(p->handle);
759
 
    
760
 
    if (p->mode != MODE_FXO) { /* station port. */
761
 
        vpb_ring_station_async(p->handle, VPB_RING_STATION_OFF,'1');    
762
 
        vpb_playtone_async(p->handle, &Busytone);
763
 
 
764
 
    } else
765
 
        vpb_sethook_sync(p->handle, VPB_ONHOOK);
766
 
 
767
 
    ast_setstate(ast,AST_STATE_DOWN);
768
 
    
769
 
    ast_mutex_lock(&p->lock); {    
770
 
         p->lastinput = p->lastoutput  = -1;
771
 
         p->ext[0]  = 0;
772
 
         p->owner = NULL;
773
 
         p->dialtone = 0;
774
 
         p->calling = 0;
775
 
         ast->pvt->pvt = NULL;   
776
 
    } ast_mutex_unlock(&p->lock);
777
 
         
778
 
    ast_mutex_lock(&usecnt_lock); {
779
 
        usecnt--;
780
 
    } ast_mutex_unlock(&usecnt_lock);
781
 
    ast_update_use_count();
782
 
 
783
 
    /* Stop thread doing reads. */
784
 
    p->stopreads = 1;
785
 
    pthread_join(p->readthread, NULL); 
786
 
 
787
 
    if (option_verbose > 2)
788
 
        ast_verbose(VERBOSE_PREFIX_3 " Hungup on %s complete\n", ast->name);
789
 
    
790
 
    restart_monitor();
791
 
    return 0;
792
1495
}
793
1496
 
794
1497
static int vpb_answer(struct ast_channel *ast)
795
1498
{
796
 
    struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
797
 
 
798
 
    if (p->mode == MODE_FXO)
799
 
        vpb_sethook_sync(p->handle, VPB_OFFHOOK);
800
 
    
801
 
    if (option_debug)
802
 
        ast_log(LOG_DEBUG, "vpb answer on %s\n", ast->name);
803
 
    ast->rings = 0;
804
 
    ast_setstate(ast, AST_STATE_UP);
805
 
    
806
 
    return 0;
 
1499
        struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
 
1500
        VPB_EVENT je;
 
1501
        int ret;
 
1502
        int res = 0;
 
1503
/*
 
1504
        if (option_verbose > 3) ast_verbose("%s: LOCKING in answer \n", p->dev);
 
1505
        if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
 
1506
*/
 
1507
        ast_mutex_lock(&p->lock);
 
1508
 
 
1509
        if (option_verbose > 3)
 
1510
                        ast_verbose(VERBOSE_PREFIX_4 "%s: Answering channel\n",p->dev);
 
1511
 
 
1512
        if (p->mode == MODE_FXO){
 
1513
                if (option_verbose > 3)
 
1514
                                ast_verbose("%s: Disabling Loop Drop detection\n",p->dev);
 
1515
                vpb_disable_event(p->handle, VPB_MDROP);
 
1516
        }
 
1517
 
 
1518
        if (ast->_state != AST_STATE_UP) {
 
1519
                if (p->mode == MODE_FXO){
 
1520
                        vpb_sethook_sync(p->handle, VPB_OFFHOOK);
 
1521
                        p->state=VPB_STATE_OFFHOOK;
 
1522
/*                      vpb_sleep(500);
 
1523
                        ret = vpb_get_event_ch_async(p->handle,&je);
 
1524
                        if ((ret == VPB_OK)&&((je.type != VPB_DROP)&&(je.type != VPB_RING))){
 
1525
                                if (option_verbose > 3){
 
1526
                                                ast_verbose(VERBOSE_PREFIX_4 "%s: Answer collected a wrong event!!\n",p->dev);
 
1527
                                }
 
1528
                                vpb_put_event(&je);
 
1529
                        }
 
1530
*/
 
1531
                }
 
1532
                ast_setstate(ast, AST_STATE_UP);
 
1533
 
 
1534
                if(option_verbose>1) 
 
1535
                        ast_verbose( VERBOSE_PREFIX_2 "%s: Answered call from %s on %s [%s]\n", p->dev,
 
1536
                                        p->owner->callerid, ast->name,(p->mode == MODE_FXO)?"FXO":"FXS");
 
1537
 
 
1538
                ast->rings = 0;
 
1539
                if( !p->readthread ){
 
1540
        //              res = ast_mutex_unlock(&p->lock);
 
1541
        //              ast_verbose("%s: unLOCKING in answer [%d]\n", p->dev,res);
 
1542
                        ast_pthread_create(&p->readthread, NULL, do_chanreads, (void *)p);
 
1543
                } else {
 
1544
                        if(option_verbose>3) 
 
1545
                                ast_verbose(VERBOSE_PREFIX_4 "%s: Record thread already running!!\n",p->dev);
 
1546
                }
 
1547
        } else {
 
1548
                if(option_verbose>3) {
 
1549
                        ast_verbose(VERBOSE_PREFIX_4 "%s: Answered state is up\n",p->dev);
 
1550
                }
 
1551
        //      res = ast_mutex_unlock(&p->lock);
 
1552
        //      ast_verbose("%s: unLOCKING in answer [%d]\n", p->dev,res);
 
1553
        }
 
1554
        vpb_sleep(500);
 
1555
        if (p->mode == MODE_FXO){
 
1556
                if (option_verbose > 3)
 
1557
                                ast_verbose("%s: Re-enabling Loop Drop detection\n",p->dev);
 
1558
                vpb_enable_event(p->handle,VPB_MDROP);
 
1559
        }
 
1560
        res = ast_mutex_unlock(&p->lock);
 
1561
/*
 
1562
        if(option_verbose>3) ast_verbose("%s: unLOCKING in answer [%d]\n", p->dev,res);
 
1563
*/
 
1564
        return 0;
807
1565
}
808
1566
 
809
1567
static struct ast_frame  *vpb_read(struct ast_channel *ast)
810
1568
{
811
 
    struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt; 
812
 
    static struct ast_frame f = {AST_FRAME_NULL}; 
813
 
    
814
 
    f.src = type;
815
 
    ast_log(LOG_NOTICE, "vpb_read should never be called (chan=%s)!\n", p->dev);
816
 
        
817
 
    return &f;
 
1569
        struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt; 
 
1570
        static struct ast_frame f = {AST_FRAME_NULL}; 
 
1571
 
 
1572
        f.src = type;
 
1573
        ast_log(LOG_NOTICE, "%s: vpb_read: should never be called!\n", p->dev);
 
1574
        ast_verbose("%s: vpb_read: should never be called!\n", p->dev);
 
1575
 
 
1576
        return &f;
818
1577
}
819
1578
 
820
1579
static inline int ast2vpbformat(int ast_format)
821
1580
{
822
 
 
823
 
    switch(ast_format) {
824
 
        case AST_FORMAT_ALAW:
825
 
            return VPB_ALAW;
826
 
        case AST_FORMAT_SLINEAR:
827
 
            return VPB_LINEAR;
828
 
        case AST_FORMAT_ULAW:
829
 
            return VPB_MULAW;
830
 
        case AST_FORMAT_ADPCM:
831
 
 
832
 
            return VPB_OKIADPCM;
833
 
        default:
834
 
            return -1;
835
 
    }
 
1581
        switch(ast_format) {
 
1582
                case AST_FORMAT_ALAW:
 
1583
                        return VPB_ALAW;
 
1584
                case AST_FORMAT_SLINEAR:
 
1585
                        return VPB_LINEAR;
 
1586
                case AST_FORMAT_ULAW:
 
1587
                        return VPB_MULAW;
 
1588
                case AST_FORMAT_ADPCM:
 
1589
                        return VPB_OKIADPCM;
 
1590
                default:
 
1591
                        return -1;
 
1592
        }
 
1593
}
 
1594
 
 
1595
static inline char * ast2vpbformatname(int ast_format)
 
1596
{
 
1597
        switch(ast_format) {
 
1598
                case AST_FORMAT_ALAW:
 
1599
                        return "AST_FORMAT_ALAW:VPB_ALAW";
 
1600
                case AST_FORMAT_SLINEAR:
 
1601
                        return "AST_FORMAT_SLINEAR:VPB_LINEAR";
 
1602
                case AST_FORMAT_ULAW:
 
1603
                        return "AST_FORMAT_ULAW:VPB_MULAW";
 
1604
                case AST_FORMAT_ADPCM:
 
1605
                        return "AST_FORMAT_ADPCM:VPB_OKIADPCM";
 
1606
                default:
 
1607
                        return "UNKN:UNKN";
 
1608
        }
836
1609
}
837
1610
 
838
1611
static inline int astformatbits(int ast_format)
839
1612
{
840
 
 
841
 
    switch(ast_format) {
842
 
        case AST_FORMAT_ALAW:
843
 
        case AST_FORMAT_ULAW:
844
 
            return 8;
845
 
        case AST_FORMAT_SLINEAR:
846
 
            return 16;
847
 
        case AST_FORMAT_ADPCM:
848
 
            return 4;
849
 
        default:
850
 
            return 8;
851
 
    }   
852
 
}
853
 
 
 
1613
        switch(ast_format) {
 
1614
                case AST_FORMAT_ALAW:
 
1615
                case AST_FORMAT_ULAW:
 
1616
                        return 8;
 
1617
                case AST_FORMAT_SLINEAR:
 
1618
                        return 16;
 
1619
                case AST_FORMAT_ADPCM:
 
1620
                        return 4;
 
1621
                default:
 
1622
                        return 8;
 
1623
        }   
 
1624
}
 
1625
 
 
1626
int a_gain_vector(float g, short *v, int n) 
 
1627
{
 
1628
        int i;
 
1629
        float tmp;
 
1630
        for ( i = 0; i<n; i++) {
 
1631
                tmp = g*v[i];
 
1632
                if (tmp > 32767.0)
 
1633
                        tmp = 32767.0;
 
1634
                if (tmp < -32768.0)
 
1635
                        tmp = -32768.0;
 
1636
                v[i] = (short)tmp;      
 
1637
        }  
 
1638
        return(i);
 
1639
}
 
1640
 
 
1641
/* Writes a frame of voice data to a VPB channel */
854
1642
static int vpb_write(struct ast_channel *ast, struct ast_frame *frame)
855
1643
{
856
 
    struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt; 
857
 
    int res = 0, fmt = 0;
858
 
 
859
 
    if (frame->frametype != AST_FRAME_VOICE) {
860
 
        ast_log(LOG_WARNING, "Don't know how to handle from type %d\n",
861
 
                frame->frametype);
862
 
        return 0;
863
 
    } else if (ast->_state != AST_STATE_UP) {
864
 
        if (option_verbose  > 4)
865
 
            ast_log(LOG_WARNING, "Writing frame type [%d,%d] on chan %s not up\n",
866
 
                    frame->frametype, frame->subclass, ast->name);
867
 
        return 0;
868
 
        
869
 
    }
870
 
    
871
 
 
872
 
    fmt = ast2vpbformat(frame->subclass);
873
 
 
874
 
    if (option_verbose > 4)
875
 
        ast_verbose(VERBOSE_PREFIX_3 
876
 
                    " Write chan %s: got frame type = %d\n",
877
 
                    p->dev, frame->subclass);
878
 
       
879
 
    if (fmt < 0) {
880
 
        ast_log(LOG_WARNING, "vpb_write Cannot handle frames of %d format!\n", 
881
 
                frame->subclass);
882
 
        return -1;
883
 
    }
884
 
    
885
 
 
886
 
    if (p->lastoutput != fmt) {
887
 
         vpb_play_buf_start(p->handle, fmt);
888
 
         p->lastoutput = fmt;
889
 
    }
890
 
    
891
 
    memcpy(p->obuf, frame->data, 
892
 
           (frame->datalen > (int)sizeof p->obuf) ? sizeof p->obuf : frame->datalen);
893
 
    res = vpb_play_buf_sync(p->handle, p->obuf, frame->datalen);
894
 
    if (res != VPB_OK)
895
 
         return -1;
896
 
    else 
897
 
         return 0;
 
1644
        struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt; 
 
1645
        int res = 0, fmt = 0;
 
1646
        struct timeval play_buf_time_start,play_buf_time_finish;
 
1647
//      ast_mutex_lock(&p->lock);
 
1648
        if(option_verbose>5) 
 
1649
                ast_verbose("%s: vpb_write: Writing to channel\n", p->dev);
 
1650
 
 
1651
        if (frame->frametype != AST_FRAME_VOICE) {
 
1652
                if(option_verbose>3) 
 
1653
                        ast_verbose("%s: vpb_write: Don't know how to handle from type %d\n", ast->name, frame->frametype);
 
1654
//              ast_mutex_unlock(&p->lock);
 
1655
                return 0;
 
1656
        } else if (ast->_state != AST_STATE_UP) {
 
1657
                if(option_verbose>3) 
 
1658
                        ast_verbose("%s: vpb_write: Attempt to Write frame type[%d]subclass[%d] on not up chan\n",ast->name, frame->frametype, frame->subclass);
 
1659
                p->lastoutput = -1;
 
1660
//              ast_mutex_unlock(&p->lock);
 
1661
                return 0;
 
1662
        }
 
1663
        ast_log(LOG_DEBUG, "%s: vpb_write: Checked frame type..\n", p->dev);
 
1664
 
 
1665
        fmt = ast2vpbformat(frame->subclass);
 
1666
        if (fmt < 0) {
 
1667
                ast_log(LOG_WARNING, "%s: vpb_write: Cannot handle frames of %d format!\n",ast->name, frame->subclass);
 
1668
                return -1;
 
1669
        }
 
1670
        ast_log(LOG_DEBUG, "%s: vpb_write: Checked frame format..\n", p->dev);
 
1671
 
 
1672
        ast_mutex_lock(&p->play_lock);
 
1673
 
 
1674
        ast_log(LOG_DEBUG, "%s: vpb_write: Got play lock..\n", p->dev);
 
1675
 
 
1676
        /* Check if we have set up the play_buf */
 
1677
        if (p->lastoutput == -1) {
 
1678
                vpb_play_buf_start(p->handle, fmt);
 
1679
                if(option_verbose>1) {
 
1680
                        ast_verbose("%s: vpb_write: Starting play mode (codec=%d)[%s]\n",p->dev,fmt,ast2vpbformatname(frame->subclass));
 
1681
                }
 
1682
        } else if (p->lastoutput != fmt) {
 
1683
                vpb_play_buf_finish(p->handle);
 
1684
                vpb_play_buf_start(p->handle, fmt);
 
1685
                if(option_verbose>1) 
 
1686
                        ast_verbose("%s: vpb_write: Changed play format (%d=>%d)\n",p->dev,p->lastoutput,fmt);
 
1687
        }
 
1688
        p->lastoutput = fmt;
 
1689
 
 
1690
 
 
1691
 
 
1692
        // Apply extra gain !
 
1693
        if( p->txswgain > MAX_VPB_GAIN )
 
1694
                a_gain_vector(p->txswgain - MAX_VPB_GAIN , (short*)frame->data, frame->datalen/sizeof(short));
 
1695
 
 
1696
        ast_log(LOG_DEBUG, "%s: vpb_write: Applied gain..\n", p->dev);
 
1697
 
 
1698
//      gettimeofday(&tv, NULL);
 
1699
//      return ((double)tv.tv_sec*1000)+((double)tv.tv_usec/1000);
 
1700
 
 
1701
        if ((p->read_state == 1)&&(p->play_buf_time<5)){
 
1702
        gettimeofday(&play_buf_time_start,NULL);
 
1703
        res = vpb_play_buf_sync(p->handle, (char*)frame->data, frame->datalen);
 
1704
        if( (res == VPB_OK) && (option_verbose > 5) ) {
 
1705
                short * data = (short*)frame->data;
 
1706
                ast_verbose("%s: vpb_write: Wrote chan (codec=%d) %d %d\n", p->dev, fmt, data[0],data[1]);
 
1707
        }
 
1708
        gettimeofday(&play_buf_time_finish,NULL);
 
1709
        if (play_buf_time_finish.tv_sec == play_buf_time_start.tv_sec){
 
1710
                p->play_buf_time=(int)((play_buf_time_finish.tv_usec-play_buf_time_start.tv_usec)/1000);
 
1711
//              ast_log(LOG_DEBUG, "%s: vpb_write: Timing start(%d) finish(%d)\n", p->dev,play_buf_time_start.tv_usec,play_buf_time_finish.tv_usec);
 
1712
        }
 
1713
        else {
 
1714
                p->play_buf_time=(int)((play_buf_time_finish.tv_sec - play_buf_time_start.tv_sec)*100)+(int)((play_buf_time_finish.tv_usec-play_buf_time_start.tv_usec)/1000);
 
1715
        }
 
1716
//      ast_log(LOG_DEBUG, "%s: vpb_write: Wrote data [%d](%d=>%s) to play_buf in [%d]ms..\n", p->dev,frame->datalen,fmt,ast2vpbformatname(frame->subclass),p->play_buf_time);
 
1717
        }
 
1718
        else {
 
1719
                p->chuck_count++;
 
1720
                ast_log(LOG_DEBUG, "%s: vpb_write: Tossed data away, tooooo much data!![%d]\n", p->dev,p->chuck_count);
 
1721
                p->play_buf_time=0;
 
1722
        }
 
1723
 
 
1724
        ast_mutex_unlock(&p->play_lock);
 
1725
//      ast_mutex_unlock(&p->lock);
 
1726
        if(option_verbose>5) 
 
1727
                ast_verbose("%s: vpb_write: Done Writing to channel\n", p->dev);
 
1728
        return 0;
898
1729
}
899
1730
 
900
1731
/* Read monitor thread function. */
901
1732
static void *do_chanreads(void *pvt)
902
1733
{
903
 
     struct vpb_pvt *p = (struct vpb_pvt *)pvt;
904
 
     struct ast_frame *fr = &p->fr;
905
 
     char *readbuf = ((char *)p->buf) + AST_FRIENDLY_OFFSET;
906
 
     int bridgerec = 0;
907
 
 
908
 
     fr->frametype = AST_FRAME_VOICE;
909
 
     fr->src = type;
910
 
     fr->mallocd = 0;
911
 
     
912
 
     memset(p->buf, 0, sizeof p->buf);
913
 
 
914
 
     while (!p->stopreads && p->owner) {         
915
 
         int res = -1, fmt;
916
 
         struct ast_channel *owner = p->owner;
917
 
         int afmt = (owner) ? owner->pvt->rawreadformat : AST_FORMAT_ALAW;
918
 
         int state = (owner) ? owner->_state : AST_STATE_DOWN;
919
 
         int readlen;   
920
 
         
921
 
         fmt = ast2vpbformat(afmt);
922
 
         
923
 
         if (fmt < 0) {
924
 
             p->stopreads = 1;
925
 
             goto done;
926
 
         }
927
 
 
928
 
         readlen = VPB_SAMPLES * astformatbits(afmt) / 8;
929
 
 
930
 
         if (p->lastinput != fmt) {
931
 
             if (option_verbose > 2) 
932
 
                 ast_verbose(" Read_channel ##  %s: Setting record mode, bridge = %d\n", 
933
 
                             p->dev, p->bridge ? 1 : 0);             
934
 
             vpb_record_buf_start(p->handle, fmt);
935
 
             p->lastinput = fmt;
936
 
         } 
937
 
 
938
 
         ast_mutex_lock(&p->lock); {
939
 
              if (p->bridge) 
940
 
                   if (p->bridge->c0 == p->owner && 
941
 
                       (p->bridge->flags & AST_BRIDGE_REC_CHANNEL_0))
942
 
                        bridgerec = 1;
943
 
                   else if (p->bridge->c1 == p->owner && 
944
 
                       (p->bridge->flags & AST_BRIDGE_REC_CHANNEL_1))
945
 
                        bridgerec = 1;
946
 
                   else 
947
 
                        bridgerec = 0;
948
 
              else
949
 
                   bridgerec = 1;
950
 
         } ast_mutex_unlock(&p->lock);
951
 
         
952
 
         if (state == AST_STATE_UP && bridgerec) {  
953
 
             /* Read only if up and not bridged, or a bridge for which we can read. */
954
 
              res = vpb_record_buf_sync(p->handle, readbuf, readlen);         
955
 
        }  else {
956
 
              res = 0;
957
 
              vpb_sleep(10);
958
 
         }
959
 
         if (res == VPB_OK) {
960
 
              fr->subclass = afmt;
961
 
              fr->samples = VPB_SAMPLES;
962
 
              fr->data = readbuf;
963
 
              fr->datalen = readlen;
964
 
              fr->offset = AST_FRIENDLY_OFFSET;
965
 
 
966
 
              ast_mutex_lock(&p->lock); {    
967
 
                   if (p->owner) ast_queue_frame(p->owner, fr, 0);
968
 
              } ast_mutex_unlock(&p->lock);
969
 
         } else 
970
 
              p->stopreads = 1;
971
 
                 
972
 
     done: (void)0;
973
 
         if (option_verbose > 4)
974
 
             ast_verbose(" Read_channel  %s (state=%d), res=%d, bridge=%d\n", 
975
 
                         p->dev, state, res, bridgerec);     
976
 
     }
977
 
     
978
 
     /* When stopreads seen, go away! */
979
 
     vpb_record_buf_finish(p->handle);
980
 
 
981
 
     if (option_verbose > 4)
982
 
         ast_verbose(" Read_channel  %s terminating, stopreads=%d, owner=%s\n", 
983
 
                             p->dev, p->stopreads, p->owner? "yes" : "no");     
984
 
 
985
 
     return NULL;
 
1734
        struct vpb_pvt *p = (struct vpb_pvt *)pvt;
 
1735
        struct ast_frame *fr = &p->fr;
 
1736
        char *readbuf = ((char *)p->buf) + AST_FRIENDLY_OFFSET;
 
1737
        int bridgerec = 0;
 
1738
        int afmt, readlen, res, fmt, trycnt=0;
 
1739
        int ignore_dtmf;
 
1740
        char * getdtmf_var = NULL;
 
1741
 
 
1742
        fr->frametype = AST_FRAME_VOICE;
 
1743
        fr->src = type;
 
1744
        fr->mallocd = 0;
 
1745
        fr->delivery.tv_sec = 0;
 
1746
        fr->delivery.tv_usec = 0;
 
1747
        fr->samples = VPB_SAMPLES;
 
1748
        fr->offset = AST_FRIENDLY_OFFSET;
 
1749
        memset(p->buf, 0, sizeof p->buf);
 
1750
 
 
1751
        if (option_verbose > 2) {
 
1752
                ast_verbose("%s: chanreads: starting thread\n", p->dev);
 
1753
        }  
 
1754
        ast_mutex_lock(&p->record_lock);
 
1755
 
 
1756
        p->stopreads = 0; 
 
1757
        p->read_state = 1;
 
1758
        while (!p->stopreads && p->owner) {
 
1759
 
 
1760
                if (option_verbose > 4)
 
1761
                        ast_verbose("%s: chanreads: Starting cycle ...\n", p->dev);
 
1762
                if (option_verbose > 4)
 
1763
                        ast_verbose("%s: chanreads: Checking bridge \n", p->dev);
 
1764
                if (p->bridge) {
 
1765
                        if (p->bridge->c0 == p->owner && (p->bridge->flags & AST_BRIDGE_REC_CHANNEL_0))
 
1766
                                bridgerec = 1;
 
1767
                        else if (p->bridge->c1 == p->owner && (p->bridge->flags & AST_BRIDGE_REC_CHANNEL_1))
 
1768
                                bridgerec = 1;
 
1769
                        else 
 
1770
                                bridgerec = 0;
 
1771
                } else {
 
1772
                        if (option_verbose > 4)
 
1773
                                ast_verbose("%s: chanreads: No native bridge.\n", p->dev);
 
1774
                        if (p->owner->bridge){
 
1775
                                if (option_verbose > 4){
 
1776
                                        ast_verbose("%s: chanreads: Got Asterisk bridge with [%s].\n", p->dev,p->owner->bridge->name);
 
1777
                                }
 
1778
                                bridgerec = 1;
 
1779
                        }
 
1780
                        else {
 
1781
                                bridgerec = 0;
 
1782
                        }
 
1783
                }
 
1784
 
 
1785
//              if ( (p->owner->_state != AST_STATE_UP) || !bridgerec) {
 
1786
                if ( (p->owner->_state != AST_STATE_UP) ) {
 
1787
                        if (option_verbose > 4) {
 
1788
                                if (p->owner->_state != AST_STATE_UP)
 
1789
                                        ast_verbose("%s: chanreads: Im not up[%d]\n", p->dev,p->owner->_state);
 
1790
                                else
 
1791
                                        ast_verbose("%s: chanreads: No bridgerec[%d]\n", p->dev,bridgerec);
 
1792
                        }  
 
1793
                        vpb_sleep(10);
 
1794
                        continue;
 
1795
                }
 
1796
 
 
1797
                // Voicetronix DTMF detection can be triggered off ordinary speech
 
1798
                // This leads to annoying beeps during the conversation
 
1799
                // Avoid this problem by just setting VPB_GETDTMF when you want to listen for DTMF
 
1800
                //ignore_dtmf = 1;
 
1801
                ignore_dtmf = 0; /* set this to 1 to turn this feature on */
 
1802
                getdtmf_var = pbx_builtin_getvar_helper(p->owner,"VPB_GETDTMF");
 
1803
                if( getdtmf_var && ( strcasecmp( getdtmf_var, "yes" ) == 0 ) )
 
1804
                        ignore_dtmf = 0;
 
1805
 
 
1806
                if( ignore_dtmf != p->last_ignore_dtmf ) {
 
1807
                        if(option_verbose>1) 
 
1808
                                ast_verbose( VERBOSE_PREFIX_2 "%s:Now %s DTMF \n",
 
1809
                                        p->dev, ignore_dtmf ? "ignoring" : "listening for");
 
1810
                        vpb_set_event_mask(p->handle, ignore_dtmf ? VPB_EVENTS_NODTMF : VPB_EVENTS_ALL );
 
1811
                }
 
1812
                p->last_ignore_dtmf = ignore_dtmf;
 
1813
 
 
1814
                // Play DTMF digits here to avoid problem you get if playing a digit during
 
1815
                // a record operation
 
1816
                if (option_verbose > 5) {
 
1817
                        ast_verbose("%s: chanreads: Checking dtmf's \n", p->dev);
 
1818
                }  
 
1819
                ast_mutex_lock(&p->play_dtmf_lock);
 
1820
                if( p->play_dtmf[0] ) {
 
1821
                        // Try to ignore DTMF event we get after playing digit
 
1822
                        // This DTMF is played by asterisk and leads to an annoying trailing beep on CISCO phones
 
1823
                        if( !ignore_dtmf) 
 
1824
                                vpb_set_event_mask(p->handle, VPB_EVENTS_NODTMF );
 
1825
                        vpb_dial_sync(p->handle,p->play_dtmf);
 
1826
                        if(option_verbose>1) 
 
1827
                                ast_verbose( VERBOSE_PREFIX_2 "%s: Played DTMF %s\n",p->dev,p->play_dtmf);
 
1828
                        p->play_dtmf[0] = '\0';
 
1829
                        ast_mutex_unlock(&p->play_dtmf_lock);
 
1830
                        vpb_sleep(700); // Long enough to miss echo and DTMF event
 
1831
                        if( !ignore_dtmf) 
 
1832
                                vpb_set_event_mask(p->handle, VPB_EVENTS_ALL );
 
1833
                        continue;
 
1834
                }
 
1835
                ast_mutex_unlock(&p->play_dtmf_lock);
 
1836
 
 
1837
//              afmt = (p->owner) ? p->owner->pvt->rawreadformat : AST_FORMAT_SLINEAR;
 
1838
                if (p->owner){
 
1839
                        afmt = p->owner->pvt->rawreadformat;
 
1840
                        ast_log(LOG_DEBUG,"%s: Record using owner format [%s]\n", p->dev, ast2vpbformatname(afmt));
 
1841
                }
 
1842
                else {
 
1843
                        afmt = AST_FORMAT_SLINEAR;
 
1844
                        ast_log(LOG_DEBUG,"%s: Record using default format [%s]\n", p->dev, ast2vpbformatname(afmt));
 
1845
                }
 
1846
                fmt = ast2vpbformat(afmt);
 
1847
                if (fmt < 0) {
 
1848
                        ast_log(LOG_WARNING,"%s: Record failure (unsupported format %d)\n", p->dev, afmt);
 
1849
                        return NULL;
 
1850
                }
 
1851
                readlen = VPB_SAMPLES * astformatbits(afmt) / 8;
 
1852
 
 
1853
                if (p->lastinput == -1) {
 
1854
                        vpb_record_buf_start(p->handle, fmt);
 
1855
                        vpb_reset_record_fifo_alarm(p->handle);
 
1856
                        if(option_verbose>1) 
 
1857
                                ast_verbose( VERBOSE_PREFIX_2 "%s: Starting record mode (codec=%d)[%s]\n",p->dev,fmt,ast2vpbformatname(afmt));
 
1858
                } else if (p->lastinput != fmt) {
 
1859
                        vpb_record_buf_finish(p->handle);
 
1860
                        vpb_record_buf_start(p->handle, fmt);
 
1861
                        if(option_verbose>1) 
 
1862
                                ast_verbose( VERBOSE_PREFIX_2 "%s: Changed record format (%d=>%d)\n",p->dev,p->lastinput,fmt);
 
1863
                }
 
1864
                p->lastinput = fmt;
 
1865
 
 
1866
                /* Read only if up and not bridged, or a bridge for which we can read. */
 
1867
                if (option_verbose > 5) {
 
1868
                        ast_verbose("%s: chanreads: getting buffer!\n", p->dev);
 
1869
                }  
 
1870
                if( (res = vpb_record_buf_sync(p->handle, readbuf, readlen) ) == VPB_OK ) {
 
1871
                        if (option_verbose > 5) {
 
1872
                                ast_verbose("%s: chanreads: got buffer!\n", p->dev);
 
1873
                        }  
 
1874
                        // Apply extra gain !
 
1875
                        if( p->rxswgain > MAX_VPB_GAIN )
 
1876
                                a_gain_vector(p->rxswgain - MAX_VPB_GAIN , (short*)readbuf, readlen/sizeof(short));
 
1877
                        if (option_verbose > 5) {
 
1878
                                ast_verbose("%s: chanreads: applied gain\n", p->dev);
 
1879
                        }  
 
1880
 
 
1881
                        fr->subclass = afmt;
 
1882
                        fr->data = readbuf;
 
1883
                        fr->datalen = readlen;
 
1884
 
 
1885
                        // Using trylock here to prevent deadlock when channel is hungup
 
1886
                        // (ast_hangup() immediately gets lock)
 
1887
                        if (p->owner && !p->stopreads ) {
 
1888
                                if (option_verbose > 5) {
 
1889
                                        ast_verbose("%s: chanreads: queueing buffer on read frame q (state[%d])\n", p->dev,p->owner->_state);
 
1890
                                }  
 
1891
                                do {
 
1892
                                        res = ast_mutex_trylock(&p->owner->lock);
 
1893
                                        trycnt++;
 
1894
                                } while((res !=0)&&(trycnt<300));
 
1895
                                if (res==0)  {
 
1896
                                        ast_queue_frame(p->owner, fr);
 
1897
                                        ast_mutex_unlock(&p->owner->lock);
 
1898
                                } else {
 
1899
                                        if (option_verbose > 4) 
 
1900
                                                ast_verbose("%s: chanreads: Couldnt get lock after %d tries!\n", p->dev,trycnt);
 
1901
                                }
 
1902
                                trycnt=0;
 
1903
                                
 
1904
/*
 
1905
                                res = ast_mutex_trylock(&p->owner->lock);
 
1906
//                              res=0;
 
1907
                                if (res==0)  {
 
1908
                                        ast_queue_frame(p->owner, fr);
 
1909
                                        ast_mutex_unlock(&p->owner->lock);
 
1910
                                } else {
 
1911
                                        if (res == EINVAL )
 
1912
                                                if (option_verbose > 4) ast_verbose("%s: chanreads: try owner->lock gave me EINVAL[%d]\n", p->dev,res);
 
1913
                                        else if (res == EBUSY )
 
1914
                                                if (option_verbose > 4) ast_verbose("%s: chanreads: try owner->lock gave me EBUSY[%d]\n", p->dev,res);
 
1915
                                        while(res !=0){
 
1916
                                        res = ast_mutex_trylock(&p->owner->lock);
 
1917
                                        }
 
1918
                                        if (res==0)  {
 
1919
                                                ast_queue_frame(p->owner, fr);
 
1920
                                                ast_mutex_unlock(&p->owner->lock);
 
1921
                                        }
 
1922
                                        else {
 
1923
                                                if (res == EINVAL )
 
1924
                                                        if (option_verbose > 4) ast_verbose("%s: chanreads: try owner->lock gave me EINVAL[%d]\n", p->dev,res);
 
1925
                                                else if (res == EBUSY )
 
1926
                                                        if (option_verbose > 4) ast_verbose("%s: chanreads: try owner->lock gave me EBUSY[%d]\n", p->dev,res);
 
1927
                                                if (option_verbose > 4) ast_verbose("%s: chanreads: Couldnt get lock on owner[%s][%d][%d] channel to send frame!\n", p->dev,p->owner->name,(int)p->owner->lock.__m_owner,(int)p->owner->lock.__m_count);
 
1928
                                                //assert(p->dev!=p->dev);
 
1929
                                        }
 
1930
                                }
 
1931
*/
 
1932
                                if (option_verbose > 6) {
 
1933
                                        short * data = (short*)readbuf;
 
1934
                                        ast_verbose("%s: Read channel (codec=%d) %d %d\n", p->dev, fmt, data[0], data[1] );
 
1935
                                }  
 
1936
                        }
 
1937
                        else {
 
1938
                                if (option_verbose > 4) {
 
1939
                                        ast_verbose("%s: p->stopreads[%d] p->owner[%p]\n", p->dev, p->stopreads,(void *)p->owner);
 
1940
                                }  
 
1941
                        }
 
1942
                } else {
 
1943
                        ast_log(LOG_WARNING,"%s: Record failure (%s)\n", p->dev, vpb_strerror(res));
 
1944
                        vpb_record_buf_finish(p->handle);
 
1945
                        vpb_record_buf_start(p->handle, fmt);
 
1946
                }
 
1947
                if (option_verbose > 4)
 
1948
                        ast_verbose("%s: chanreads: Finished cycle...\n", p->dev);
 
1949
        }
 
1950
        p->read_state=0;
 
1951
 
 
1952
        /* When stopreads seen, go away! */
 
1953
        vpb_record_buf_finish(p->handle);
 
1954
        p->read_state=0;
 
1955
        ast_mutex_unlock(&p->record_lock);
 
1956
 
 
1957
        if (option_verbose > 1)
 
1958
                ast_verbose(VERBOSE_PREFIX_2 "%s: Ending record mode (%d/%s)\n",
 
1959
                         p->dev, p->stopreads, p->owner? "yes" : "no");     
 
1960
        return NULL;
986
1961
}
987
1962
 
988
 
static struct ast_channel *vpb_new(struct vpb_pvt *i, int state, char *context)
 
1963
static struct ast_channel *vpb_new(struct vpb_pvt *me, int state, char *context)
989
1964
{
990
1965
        struct ast_channel *tmp; 
991
1966
 
992
 
        if (i->owner) {
993
 
            ast_log(LOG_WARNING, "Called vpb_new on owned channel (%s) ?!\n", i->dev);
 
1967
        if (me->owner) {
 
1968
            ast_log(LOG_WARNING, "Called vpb_new on owned channel (%s) ?!\n", me->dev);
994
1969
            return NULL;
995
1970
        }
 
1971
        if (option_verbose > 3)
 
1972
                ast_verbose("%s: New call for context [%s]\n",me->dev,context);
996
1973
            
997
1974
        tmp = ast_channel_alloc(1);
998
1975
        if (tmp) {
999
 
                strncpy(tmp->name, i->dev, sizeof(tmp->name));
 
1976
                strncpy(tmp->name, me->dev, sizeof(tmp->name) - 1);
1000
1977
                tmp->type = type;
1001
1978
               
 
1979
                // Linear is the preferred format. Although Voicetronix supports other formats
 
1980
                // they are all converted to/from linear in the vpb code. Best for us to use
 
1981
                // linear since we can then adjust volume in this modules.
1002
1982
                tmp->nativeformats = prefformat;
1003
 
                tmp->pvt->rawreadformat = AST_FORMAT_ALAW;
1004
 
                tmp->pvt->rawwriteformat =  AST_FORMAT_ALAW;
 
1983
                tmp->pvt->rawreadformat = AST_FORMAT_SLINEAR;
 
1984
                tmp->pvt->rawwriteformat =  AST_FORMAT_SLINEAR;
1005
1985
                ast_setstate(tmp, state);
1006
1986
                if (state == AST_STATE_RING)
1007
1987
                        tmp->rings = 1;
1008
 
                tmp->pvt->pvt = i;
 
1988
                tmp->pvt->pvt = me;
 
1989
                /* set call backs */
1009
1990
                tmp->pvt->send_digit = vpb_digit;
1010
1991
                tmp->pvt->call = vpb_call;
1011
1992
                tmp->pvt->hangup = vpb_hangup;
1017
1998
                tmp->pvt->fixup = vpb_fixup;
1018
1999
                
1019
2000
                strncpy(tmp->context, context, sizeof(tmp->context)-1);
1020
 
                if (strlen(i->ext))
1021
 
                        strncpy(tmp->exten, i->ext, sizeof(tmp->exten)-1);
 
2001
                if (strlen(me->ext))
 
2002
                        strncpy(tmp->exten, me->ext, sizeof(tmp->exten)-1);
1022
2003
                else
1023
2004
                        strncpy(tmp->exten, "s",  sizeof(tmp->exten) - 1);
1024
 
                if (strlen(i->language))
1025
 
                        strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
1026
 
                if (strlen(i->callerid))
1027
 
                        tmp->callerid = strdup(i->callerid);
1028
 
                i->owner = tmp;
 
2005
                if (strlen(me->language))
 
2006
                        strncpy(tmp->language, me->language, sizeof(tmp->language)-1);
1029
2007
 
 
2008
                me->owner = tmp;
 
2009
     
 
2010
                me->bridge = NULL;
 
2011
                me->lastoutput = -1;
 
2012
                me->lastinput = -1;
 
2013
                me->last_ignore_dtmf = 1;
 
2014
                me->readthread = 0;
 
2015
                me->play_dtmf[0] = '\0';
1030
2016
                
1031
 
                i->lastinput = i->lastoutput = -1;
1032
 
                i->lastgrunt  = tcounter; /* Assume at least one grunt tone seen now. */
 
2017
                me->lastgrunt  = get_time_in_ms(); /* Assume at least one grunt tone seen now. */
1033
2018
 
1034
2019
                ast_mutex_lock(&usecnt_lock);
1035
2020
                usecnt++;
1036
2021
                ast_mutex_unlock(&usecnt_lock);
1037
2022
                ast_update_use_count();
1038
 
                if (state != AST_STATE_DOWN) 
1039
 
                     if (ast_pbx_start(tmp)) {
1040
 
                          ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
1041
 
                          ast_hangup(tmp);
1042
 
                     }
1043
 
 
1044
 
                i->stopreads = 0; /* So read thread runs. */    
1045
 
                /* Finally start read monitoring thread. */
1046
 
                pthread_create(&i->readthread, NULL, do_chanreads, (void *)i);
1047
 
        } else
 
2023
                if (state != AST_STATE_DOWN) {
 
2024
                        if (ast_pbx_start(tmp)) {
 
2025
                                ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
 
2026
                                ast_hangup(tmp);
 
2027
                        }
 
2028
                }
 
2029
        } else {
1048
2030
                ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
 
2031
        }
1049
2032
        return tmp;
1050
2033
}
1051
2034
 
1052
2035
static struct ast_channel *vpb_request(char *type, int format, void *data) 
1053
2036
{
1054
 
     int oldformat;
1055
 
     struct vpb_pvt *p;
1056
 
     struct ast_channel *tmp = NULL;
1057
 
     char *name = strdup(data ? (char *)data : "");
1058
 
     char *s, *sepstr;
1059
 
     
1060
 
     oldformat = format;
1061
 
     format &= prefformat;
1062
 
     if (!format) {
1063
 
          ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
1064
 
          return NULL;
1065
 
     }
1066
 
     
1067
 
     sepstr = name;
1068
 
     s = strsep(&sepstr, "/"); /* Handle / issues */
1069
 
     if (!s) 
1070
 
          s = "";
1071
 
     /* Search for an unowned channel */
1072
 
     ast_mutex_lock(&iflock); {
1073
 
          p = iflist;
1074
 
          while(p) {
1075
 
               if (strncmp(s, p->dev + 4, sizeof p->dev) == 0) 
1076
 
                    if (!p->owner) {
1077
 
                         tmp = vpb_new(p, AST_STATE_DOWN, p->context);
1078
 
                         break;
1079
 
                    }
1080
 
               p = p->next;
1081
 
          }
1082
 
     } ast_mutex_unlock(&iflock);
1083
 
 
1084
 
 
1085
 
     if (option_verbose > 2) 
1086
 
          ast_verbose(VERBOSE_PREFIX_3 " %s requested, got: [%s]\n",
1087
 
                      name, tmp ? tmp->name : "None");
1088
 
 
1089
 
     free(name);
1090
 
     
1091
 
     restart_monitor();
1092
 
     return tmp;
 
2037
        int oldformat;
 
2038
        struct vpb_pvt *p;
 
2039
        struct ast_channel *tmp = NULL;
 
2040
        char *name = strdup(data ? (char *)data : "");
 
2041
        char *s, *sepstr;
 
2042
        int group=-1;
 
2043
 
 
2044
        oldformat = format;
 
2045
        format &= prefformat;
 
2046
        if (!format) {
 
2047
                ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
 
2048
                return NULL;
 
2049
        }
 
2050
 
 
2051
        sepstr = name;
 
2052
        s = strsep(&sepstr, "/"); /* Handle / issues */
 
2053
        if (!s) 
 
2054
                s = "";
 
2055
        /* Check if we are looking for a group */
 
2056
        if (toupper(name[0]) == 'G' || toupper(name[0])=='R') {
 
2057
                group=atoi(name+1);     
 
2058
        }
 
2059
        /* Search for an unowned channel */
 
2060
        ast_mutex_lock(&iflock); {
 
2061
                p = iflist;
 
2062
                while(p) {
 
2063
                        if (group == -1){
 
2064
                                if (strncmp(s, p->dev + 4, sizeof p->dev) == 0) {
 
2065
                                        if (!p->owner) {
 
2066
                                                tmp = vpb_new(p, AST_STATE_DOWN, p->context);
 
2067
                                                break;
 
2068
                                        }
 
2069
                                }
 
2070
                        }
 
2071
                        else {
 
2072
                                if ((p->group == group) && (!p->owner)) {
 
2073
                                        tmp = vpb_new(p, AST_STATE_DOWN, p->context);
 
2074
                                        break;
 
2075
                                }
 
2076
                        }
 
2077
                        p = p->next;
 
2078
                }
 
2079
        } ast_mutex_unlock(&iflock);
 
2080
 
 
2081
 
 
2082
        if (option_verbose > 1) 
 
2083
                ast_verbose(VERBOSE_PREFIX_2 " %s requested, got: [%s]\n",
 
2084
                name, tmp ? tmp->name : "None");
 
2085
 
 
2086
        free(name);
 
2087
 
 
2088
        restart_monitor();
 
2089
        return tmp;
1093
2090
}
1094
2091
 
1095
2092
static float parse_gain_value(char *gain_type, char *value)
1099
2096
        /* try to scan number */
1100
2097
        if (sscanf(value, "%f", &gain) != 1)
1101
2098
        {
1102
 
                ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n",
1103
 
                        value, gain_type, config);
 
2099
                ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n", value, gain_type, config);
1104
2100
                return DEFAULT_GAIN;
1105
2101
        }
1106
2102
 
1107
2103
 
1108
2104
        /* percentage? */
1109
 
        if (value[strlen(value) - 1] == '%')
1110
 
                return gain / (float)100;
 
2105
        //if (value[strlen(value) - 1] == '%')
 
2106
        //      return gain / (float)100;
1111
2107
 
1112
2108
        return gain;
1113
2109
}
1114
2110
 
1115
 
static int __unload_module(void)
1116
 
{
1117
 
     struct vpb_pvt *p;
1118
 
        /* First, take us out of the channel loop */
1119
 
        ast_channel_unregister(type);
1120
 
 
1121
 
        ast_mutex_lock(&iflock); {
1122
 
             /* Hangup all interfaces if they have an owner */
1123
 
             p = iflist;
1124
 
             while(p) {
1125
 
                  if (p->owner)
1126
 
                       ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1127
 
                  p = p->next;
1128
 
             }
1129
 
             iflist = NULL;
1130
 
        } ast_mutex_unlock(&iflock);
1131
 
 
1132
 
        ast_mutex_lock(&monlock); {
1133
 
             if (mthreadactive > -1) {
1134
 
                  pthread_cancel(monitor_thread);
1135
 
                  pthread_join(monitor_thread, NULL);
1136
 
             }
1137
 
             mthreadactive = -2;
1138
 
        } ast_mutex_unlock(&monlock);
1139
 
 
1140
 
        ast_mutex_lock(&iflock); {
1141
 
                /* Destroy all the interfaces and free their memory */
1142
 
 
1143
 
                while(iflist) {
1144
 
                     p = iflist;                    
1145
 
                     ast_mutex_destroy(&p->lock);
1146
 
                     pthread_cancel(p->readthread);
1147
 
                     p->readthread = 0;
1148
 
                     
1149
 
                     iflist = iflist->next;
1150
 
                     
1151
 
                     free(p);
1152
 
                }
1153
 
                iflist = NULL;
1154
 
        } ast_mutex_unlock(&iflock);
1155
 
        
1156
 
        ast_mutex_lock(&bridge_lock); {
1157
 
             memset(bridges, 0, sizeof bridges);             
1158
 
        } ast_mutex_unlock(&bridge_lock);
1159
 
        ast_mutex_destroy(&bridge_lock);
1160
 
 
1161
 
        tcounter = 0;
1162
 
        
1163
 
        return 0;
1164
 
}
1165
 
 
1166
 
int unload_module(void)
1167
 
{
1168
 
        return __unload_module();
1169
 
}
1170
 
 
1171
2111
int load_module()
1172
2112
{
1173
2113
        struct ast_config *cfg;
1175
2115
        struct vpb_pvt *tmp;
1176
2116
        int board = 0, group = 0;
1177
2117
        int mode = MODE_IMMEDIATE;
1178
 
        float txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN; 
1179
 
        
 
2118
        float txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN; 
 
2119
        float txswgain = 0, rxswgain = 0; 
 
2120
        int first_channel = 1;
 
2121
        int echo_cancel = DEFAULT_ECHO_CANCEL;
1180
2122
        int error = 0; /* Error flag */
1181
 
 
1182
 
 
1183
 
        setrxgain = settxgain = 0;
 
2123
        int bal1 = -1; // Special value - means do not set
 
2124
        int bal2 = -1; 
 
2125
        int bal3 = -1;
 
2126
        char * callerid = NULL;
1184
2127
 
1185
2128
        cfg = ast_load(config);
1186
 
        
 
2129
 
1187
2130
        /* We *must* have a config file otherwise stop immediately */
1188
2131
        if (!cfg) {
1189
 
             ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1190
 
             return -1;
 
2132
                ast_log(LOG_ERROR, "Unable to load config %s\n", config);
 
2133
                return -1;
1191
2134
        }  
1192
 
        
1193
 
        vpb_seterrormode(VPB_DEVELOPMENT);
1194
 
        
 
2135
 
 
2136
        vpb_seterrormode(VPB_ERROR_CODE);
 
2137
 
1195
2138
        ast_mutex_lock(&iflock); {
1196
 
             v = ast_variable_browse(cfg, "interfaces");
1197
 
             while(v) {
1198
 
                  /* Create the interface list */
1199
 
                  if (strcasecmp(v->name, "board") == 0) 
1200
 
                       board = atoi(v->value);
1201
 
                  else  if (strcasecmp(v->name, "group") == 0)
1202
 
                       group = atoi(v->value);
1203
 
                  else if (strcasecmp(v->name, "channel") == 0) {
1204
 
                       int channel = atoi(v->value);
1205
 
                       tmp = mkif(board, channel, mode, txgain, rxgain);
1206
 
                       if (tmp) {
1207
 
                            tmp->next = iflist;
1208
 
                            iflist = tmp;
1209
 
                            
1210
 
                       } else {
1211
 
                            ast_log(LOG_ERROR, 
1212
 
                                    "Unable to register channel '%s'\n", v->value);
1213
 
                            error = -1;
1214
 
                            goto done;
1215
 
                            
1216
 
                       }
1217
 
                  } else if (strcasecmp(v->name, "silencesupression") == 0) 
1218
 
                       silencesupression = ast_true(v->value);
1219
 
                  else if (strcasecmp(v->name, "language") == 0) 
1220
 
                       strncpy(language, v->value, sizeof(language)-1);
1221
 
                  else if (strcasecmp(v->name, "callerid") == 0) 
1222
 
                       strncpy(callerid, v->value, sizeof(callerid)-1);
1223
 
                  else if (strcasecmp(v->name, "mode") == 0) {
1224
 
                       if (strncasecmp(v->value, "di", 2) == 0) 
1225
 
                            mode = MODE_DIALTONE;
1226
 
                       else if (strncasecmp(v->value, "im", 2) == 0)
1227
 
                            mode = MODE_IMMEDIATE;
1228
 
                       else if (strncasecmp(v->value, "fx", 2) == 0)
1229
 
                            mode = MODE_FXO;
1230
 
                       else
1231
 
                            ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
1232
 
                  } else if (!strcasecmp(v->name, "context")) 
1233
 
                       strncpy(context, v->value, sizeof(context)-1);
1234
 
                  else if (!strcasecmp(v->name, "echocancel")) {
1235
 
                       if (!strcasecmp(v->value, "off")) 
1236
 
                            echocancel = 0;
1237
 
                       else 
1238
 
                            echocancel = 1;
1239
 
                  } else if (strcasecmp(v->name, "txgain") == 0) {
1240
 
                       settxgain = 1;
1241
 
                       txgain = parse_gain_value(v->name, v->value);
1242
 
                  } else if (strcasecmp(v->name, "rxgain") == 0) {
1243
 
                       setrxgain = 1;
1244
 
                       rxgain = parse_gain_value(v->name, v->value);
1245
 
                  } 
1246
 
#if 0
1247
 
                  else if (strcasecmp(v->name, "grunttimeout") == 0) 
1248
 
                       gruntdetect_timeout = 1000*atoi(v->value);
1249
 
#endif    
1250
 
                  v = v->next;
1251
 
             }
1252
 
             
1253
 
             if (gruntdetect_timeout < 1000)
1254
 
                  gruntdetect_timeout = 1000;
1255
 
 
1256
 
        done: (void)0;
 
2139
                v = ast_variable_browse(cfg, "interfaces");
 
2140
                while(v) {
 
2141
                        /* Create the interface list */
 
2142
                        if (strcasecmp(v->name, "board") == 0) {
 
2143
                                board = atoi(v->value);
 
2144
                        } else  if (strcasecmp(v->name, "group") == 0){
 
2145
                                group = atoi(v->value);
 
2146
                        } else  if (strcasecmp(v->name, "useloopdrop") == 0){
 
2147
                                UseLoopDrop = atoi(v->value);
 
2148
                        } else  if (strcasecmp(v->name, "usenativebridge") == 0){
 
2149
                                UseNativeBridge = atoi(v->value);
 
2150
                        } else if (strcasecmp(v->name, "channel") == 0) {
 
2151
                                int channel = atoi(v->value);
 
2152
                                tmp = mkif(board, channel, mode, txgain, rxgain, txswgain, rxswgain, bal1, bal2, bal3, callerid, echo_cancel,group);
 
2153
                                if (tmp) {
 
2154
                                        if(first_channel) {
 
2155
                                                mkbrd( tmp->vpb_model, echo_cancel );
 
2156
                                                first_channel = 0;
 
2157
                                        }
 
2158
                                        tmp->next = iflist;
 
2159
                                        iflist = tmp;
 
2160
                                } else {
 
2161
                                        ast_log(LOG_ERROR, 
 
2162
                                        "Unable to register channel '%s'\n", v->value);
 
2163
                                        error = -1;
 
2164
                                        goto done;
 
2165
                                }
 
2166
                                callerid = NULL;
 
2167
                        } else if (strcasecmp(v->name, "language") == 0) {
 
2168
                                strncpy(language, v->value, sizeof(language)-1);
 
2169
                        } else if (strcasecmp(v->name, "callerid") == 0) {
 
2170
                                callerid = strdup(v->value);
 
2171
                        } else if (strcasecmp(v->name, "mode") == 0) {
 
2172
                                if (strncasecmp(v->value, "di", 2) == 0) 
 
2173
                                        mode = MODE_DIALTONE;
 
2174
                                else if (strncasecmp(v->value, "im", 2) == 0)
 
2175
                                        mode = MODE_IMMEDIATE;
 
2176
                                else if (strncasecmp(v->value, "fx", 2) == 0)
 
2177
                                        mode = MODE_FXO;
 
2178
                                else
 
2179
                                        ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
 
2180
                        } else if (!strcasecmp(v->name, "context")) {
 
2181
                                strncpy(context, v->value, sizeof(context)-1);
 
2182
                        } else if (!strcasecmp(v->name, "echocancel")) {
 
2183
                                if (!strcasecmp(v->value, "off")) 
 
2184
                                        echo_cancel = 0;
 
2185
                        } else if (strcasecmp(v->name, "txgain") == 0) {
 
2186
                                txswgain = parse_gain_value(v->name, v->value);
 
2187
                        } else if (strcasecmp(v->name, "rxgain") == 0) {
 
2188
                                rxswgain = parse_gain_value(v->name, v->value);
 
2189
                        } else if (strcasecmp(v->name, "txhwgain") == 0) {
 
2190
                                txgain = parse_gain_value(v->name, v->value);
 
2191
                        } else if (strcasecmp(v->name, "rxhwgain") == 0) {
 
2192
                                rxgain = parse_gain_value(v->name, v->value);
 
2193
                        } else if (strcasecmp(v->name, "bal1") == 0) {
 
2194
                                bal1 = strtol(v->value, NULL, 16);
 
2195
                                if(bal1<0 || bal1>255) {
 
2196
                                        ast_log(LOG_WARNING, "Bad bal1 value: %d\n", bal1);
 
2197
                                        bal1 = -1;
 
2198
                                }
 
2199
                        } else if (strcasecmp(v->name, "bal2") == 0) {
 
2200
                                bal2 = strtol(v->value, NULL, 16);
 
2201
                                if(bal2<0 || bal2>255) {
 
2202
                                        ast_log(LOG_WARNING, "Bad bal2 value: %d\n", bal2);
 
2203
                                        bal2 = -1;
 
2204
                                }
 
2205
                        } else if (strcasecmp(v->name, "bal3") == 0) {
 
2206
                                bal3 = strtol(v->value, NULL, 16);
 
2207
                                if(bal3<0 || bal3>255) {
 
2208
                                        ast_log(LOG_WARNING, "Bad bal3 value: %d\n", bal3);
 
2209
                                        bal3 = -1;
 
2210
                                }
 
2211
                        } else if (strcasecmp(v->name, "grunttimeout") == 0) {
 
2212
                                gruntdetect_timeout = 1000*atoi(v->value);
 
2213
                        }
 
2214
                        v = v->next;
 
2215
                }
 
2216
 
 
2217
                if (gruntdetect_timeout < 1000)
 
2218
                        gruntdetect_timeout = 1000;
 
2219
 
 
2220
                done: (void)0;
1257
2221
        } ast_mutex_unlock(&iflock);
1258
2222
 
1259
 
        
1260
2223
        ast_destroy(cfg);
1261
 
        
1262
 
        if (!error && 
1263
 
            ast_channel_register(type, tdesc, 
1264
 
                                prefformat, vpb_request) != 0) {
1265
 
             ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1266
 
             
1267
 
             error = -1;
 
2224
 
 
2225
        if (!error && ast_channel_register(type, tdesc, prefformat, vpb_request) != 0) {
 
2226
                ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
 
2227
                error = -1;
1268
2228
        }
1269
2229
 
1270
2230
 
1271
2231
        if (error)
1272
 
             __unload_module();
1273
 
        else         
1274
 
             restart_monitor(); /* And start the monitor for the first time */
1275
 
        
 
2232
                unload_module();
 
2233
        else 
 
2234
                restart_monitor(); /* And start the monitor for the first time */
 
2235
 
1276
2236
        return error;
1277
2237
}
1278
2238
 
 
2239
 
 
2240
int unload_module()
 
2241
{
 
2242
        struct vpb_pvt *p;
 
2243
        /* First, take us out of the channel loop */
 
2244
        ast_channel_unregister(type);
 
2245
 
 
2246
        ast_mutex_lock(&iflock); {
 
2247
                /* Hangup all interfaces if they have an owner */
 
2248
                p = iflist;
 
2249
                while(p) {
 
2250
                        if (p->owner)
 
2251
                                ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
 
2252
                        p = p->next;
 
2253
                }
 
2254
                iflist = NULL;
 
2255
        } ast_mutex_unlock(&iflock);
 
2256
 
 
2257
        ast_mutex_lock(&monlock); {
 
2258
                if (mthreadactive > -1) {
 
2259
                        pthread_cancel(monitor_thread);
 
2260
                        pthread_join(monitor_thread, NULL);
 
2261
                }
 
2262
                mthreadactive = -2;
 
2263
        } ast_mutex_unlock(&monlock);
 
2264
 
 
2265
        ast_mutex_lock(&iflock); {
 
2266
                /* Destroy all the interfaces and free their memory */
 
2267
 
 
2268
                while(iflist) {
 
2269
                        p = iflist;                 
 
2270
                        ast_mutex_destroy(&p->lock);
 
2271
                        pthread_cancel(p->readthread);
 
2272
                        ast_mutex_destroy(&p->owner_lock);
 
2273
                        ast_mutex_destroy(&p->record_lock);
 
2274
                        ast_mutex_destroy(&p->play_lock);
 
2275
                        ast_mutex_destroy(&p->play_dtmf_lock);
 
2276
                        p->readthread = 0;
 
2277
 
 
2278
                        vpb_close(p->handle);
 
2279
 
 
2280
                        iflist = iflist->next;
 
2281
 
 
2282
                        free(p);
 
2283
                }
 
2284
                iflist = NULL;
 
2285
        } ast_mutex_unlock(&iflock);
 
2286
 
 
2287
        ast_mutex_lock(&bridge_lock); {
 
2288
                memset(bridges, 0, sizeof bridges);          
 
2289
        } ast_mutex_unlock(&bridge_lock);
 
2290
        ast_mutex_destroy(&bridge_lock);
 
2291
        for(int i = 0; i < max_bridges; i++ ) {
 
2292
                ast_mutex_destroy(&bridges[i].lock);
 
2293
                pthread_cond_destroy(&bridges[i].cond);
 
2294
        }
 
2295
        free(bridges);
 
2296
 
 
2297
        return 0;
 
2298
}
 
2299
 
1279
2300
int usecount()
1280
2301
{
1281
2302
        int res;
1295
2316
        return ASTERISK_GPL_KEY;
1296
2317
}
1297
2318
 
 
2319
/*
1298
2320
#if defined(__cplusplus) || defined(c_plusplus)
1299
2321
 }
1300
2322
#endif
 
2323
*/