2 * Asterisk -- A telephony toolkit for Linux.
4 * Implementation of Voice over Frame Relay, Adtran Style
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
17 #include <asterisk/channel.h>
18 #include <asterisk/channel_pvt.h>
19 #include <asterisk/config.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/module.h>
22 #include <asterisk/pbx.h>
23 #include <asterisk/options.h>
24 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <linux/if_packet.h>
30 #include <linux/if_ether.h>
31 #include "adtranvofr.h"
33 #define G723_MAX_BUF 2048
35 #define FR_API_MESS 16
37 static char *desc = "Adtran Voice over Frame Relay";
38 static char *type = "AdtranVoFR";
39 static char *tdesc = "Voice over Frame Relay/Adtran style";
40 static char *config = "adtranvofr.conf";
42 static char context[AST_MAX_EXTENSION] = "default";
45 static pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
47 /* Protect the interface list (of vofr_pvt's) */
48 static pthread_mutex_t iflock = PTHREAD_MUTEX_INITIALIZER;
50 /* Protect the monitoring thread, so only one process can kill or start it, and not
51 when it's doing something critical. */
52 static pthread_mutex_t monlock = PTHREAD_MUTEX_INITIALIZER;
54 /* This is the thread for the monitor which checks for input on the channels
55 which are not currently in use. */
56 static pthread_t monitor_thread = -1;
58 static int restart_monitor(void);
60 /* The private structures of the Adtran VoFR channels are linked for
61 selecting outgoing channels */
63 static struct vofr_pvt {
64 int s; /* Raw socket for this DLCI */
65 struct sockaddr_pkt sa; /* Sockaddr needed for sending, also has iface name */
66 struct ast_channel *owner; /* Channel we belong to, possibly NULL */
67 int outgoing; /* Does this channel support outgoing calls? */
68 struct vofr_pvt *next; /* Next channel in list */
69 struct vofr_hdr *hdr; /* VOFR header version of buf */
70 struct vofr_hdr *ohdr;
71 u_int8_t dlcih; /* High two bits of DLCI */
72 u_int8_t dlcil; /* Bottom two bits of DLCI */
73 u_int8_t cid; /* Call ID */
74 char buf[G723_MAX_BUF]; /* Static buffer for reading frames */
75 char obuf[G723_MAX_BUF]; /* Output buffer */
76 char context[AST_MAX_EXTENSION];
81 /* Some useful debugging routines */
83 static char *set(int val)
85 return (val ? "Set " : "Unset");
88 static char *controlstr(int control)
91 case VOFR_CONTROL_ADTRAN:
92 return "Adtran Proprietary";
93 case VOFR_CONTROL_VOICE:
95 case VOFR_CONTROL_RFC1490:
101 static char *dtypestr(int control)
104 case VOFR_TYPE_SIGNAL:
105 return "Signal Frame";
106 case VOFR_TYPE_VOICE:
107 return "Voice Frame";
108 case VOFR_TYPE_ANSWER:
109 return "Answer Tone";
118 static char *vflagsstr(int flags)
124 if (flags & VOFR_ROUTE_LOCAL)
125 strcat(buf, "Local ");
126 if (flags & VOFR_ROUTE_VOICE)
127 strcat(buf, "Voice ");
128 if (flags & VOFR_ROUTE_DTE)
130 else if (flags & VOFR_ROUTE_DTE1)
131 strcat(buf, "DTE1 ");
132 else if (flags & VOFR_ROUTE_DTE2)
133 strcat(buf, "DTE2 ");
137 static char *remidstr(int remid)
140 case VOFR_CARD_TYPE_UNSPEC:
141 return "Unspecified";
142 case VOFR_CARD_TYPE_FXS:
144 case VOFR_CARD_TYPE_FXO:
146 case VOFR_CARD_TYPE_ENM:
148 case VOFR_CARD_TYPE_VCOM:
154 static char *modulationstr(int modulation)
157 case VOFR_MODULATION_SINGLE:
158 return "Single Frequency";
159 case VOFR_MODULATION_V21:
161 case VOFR_MODULATION_V27ter_2:
162 return "V.27 (2400bps)";
163 case VOFR_MODULATION_V27ter_4:
164 return "V.27 (4800bps)";
165 case VOFR_MODULATION_V29_7:
166 return "V.29 (7200bps)";
167 case VOFR_MODULATION_V29_9:
168 return "V.29 (9600bps)";
169 case VOFR_MODULATION_V33_12:
170 return "V.33 (12000bps)";
171 case VOFR_MODULATION_V33_14:
172 return "V.33 (14400BPS)";
177 static char *signalstr(int signal)
180 case VOFR_SIGNAL_ON_HOOK:
182 case VOFR_SIGNAL_OFF_HOOK:
184 case VOFR_SIGNAL_RING:
186 case VOFR_SIGNAL_SWITCHED_DIAL:
187 return "Switched Dial";
188 case VOFR_SIGNAL_BUSY:
190 case VOFR_SIGNAL_TRUNK_BUSY:
196 static char *vofr_digitstr(int val)
200 snprintf(num, sizeof(num), "%d", val);
213 static void vofr_dump_packet(struct vofr_hdr *vh, int len)
215 printf("VoFR Packet Dump\n");
216 printf("================\n");
217 printf("EI: %s ", set(vh->control & VOFR_MASK_EI));
218 printf("LI: %s\n", set(vh->control & VOFR_MASK_LI));
219 printf("Control: %s (0x%02x)\n",
220 controlstr(vh->control & VOFR_MASK_CONTROL), vh->control & VOFR_MASK_CONTROL);
221 printf("Data Type: %s (0x%02x)\n", dtypestr(vh->dtype), vh->dtype);
222 if (vh->dtype == VOFR_TYPE_SIGNAL) {
223 printf(" \\--Signal: %s (0x%02x)\n", signalstr(vh->data[0]), vh->data[0]);
225 if (vh->dtype == VOFR_TYPE_DTMF) {
226 printf(" \\--Digit: %s (0x%02x)\n", vofr_digitstr(vh->data[0]), vh->data[0]);
228 printf("Connect Tag: 0x%02x\n", vh->ctag);
229 printf("Voice Rt Flags: %s\n", vflagsstr(vh->vflags));
230 printf("DLCI X-Ref: %d\n", (vh->dlcih << 8) | (vh->dlcil));
231 printf("Channel ID: %d\n", vh->cid);
232 printf("Remote ID: %s (0x%02x)\n", remidstr(vh->remid), vh->remid);
233 printf("Modulation: %s (0x%02x)\n", modulationstr(vh->mod), vh->mod);
240 static int vofr_xmit(struct vofr_pvt *p, char *data, int len)
243 res=sendto(p->s, data, len, 0, (struct sockaddr *)&p->sa, sizeof(struct sockaddr_pkt));
245 ast_log(LOG_WARNING, "vofr_xmit returned %d\n", res);
250 static int vofr_digit(struct ast_channel *ast, char digit)
253 * T H I S I S T O T A L L Y U N D O C U M E N T E D
254 * A N D D O E S N O T S O U N D R I G H T
255 * XXX Figure out how to really send a decent digit XXX
261 vh->control = VOFR_CONTROL_VOICE;
262 vh->dtype = VOFR_TYPE_DTMF;
263 vh->vflags = VOFR_ROUTE_NONE;
264 vh->dlcih = p->dlcih;
265 vh->dlcil = p->dlcil;
267 vh->remid = VOFR_CARD_TYPE_ASTERISK;
268 vh->mod = VOFR_MODULATION_SINGLE;
269 if ((digit >= '0') && (digit <= '9'))
270 vh->data[0] = digit - '0';
271 else if (digit == '*')
273 else if (digit == '#')
276 ast_log(LOG_WARNING, "%s: tried to dial a non digit '%c'\n", ast->name, digit);
282 /* We sorta start the digit */
283 vofr_xmit(p, p->obuf, VOFR_HDR_SIZE + 4 + FR_API_MESS);
285 /* And terminate with an empty voice frame */
286 vh->control = VOFR_CONTROL_VOICE;
287 vh->dtype = VOFR_TYPE_VOICE;
288 vh->vflags = VOFR_ROUTE_NONE;
289 vh->dlcih = p->dlcih;
290 vh->dlcil = p->dlcil;
292 vh->remid = VOFR_CARD_TYPE_ASTERISK;
293 vh->mod = VOFR_MODULATION_SINGLE;
294 vofr_xmit(p, p->obuf, VOFR_HDR_SIZE + FR_API_MESS);
298 static int vofr_xmit_signal(struct vofr_pvt *p, int signal, int pad)
300 /* Prepare and transmit outgoing buffer with given signal and
301 pad the end with *pad* bytes of data presumed to already
302 be in the buffer (like DTMF tones, etc) */
303 struct vofr_hdr *vh = p->ohdr;
305 vh->control = VOFR_CONTROL_VOICE;
306 vh->dtype = VOFR_TYPE_SIGNAL;
307 vh->vflags = VOFR_ROUTE_NONE;
308 vh->dlcih = p->dlcih;
309 vh->dlcil = p->dlcil;
311 vh->remid = VOFR_CARD_TYPE_ASTERISK;
312 vh->mod = VOFR_MODULATION_SINGLE;
313 vh->data[0] = signal;
315 memset(p->obuf, 0, FR_API_MESS);
316 res = vofr_xmit(p, p->obuf, VOFR_HDR_SIZE + pad + 1 + FR_API_MESS);
321 static int vofr_call(struct ast_channel *ast, char *dest, int timeout)
328 if ((ast->state != AST_STATE_DOWN) && (ast->state != AST_STATE_RESERVED)) {
329 ast_log(LOG_WARNING, "vofr_call called on %s, neither down nor reserved\n", ast->name);
332 /* Take the system off hook */
333 vofr_xmit_signal(p, VOFR_SIGNAL_OFFHOOK, 0);
334 /* Wait for an acknowledgement */
337 otimeout = ast_waitfor(ast, 1000);
339 ast_log(LOG_WARNING, "Unable to take line off hook\n");
340 /* Musta gotten hung up, or no ack on off hook */
346 if ((f->frametype == AST_FRAME_CONTROL) &&
347 (f->subclass == AST_CONTROL_OFFHOOK))
352 ast_log(LOG_WARNING, "Unable to take line off hook\n");
355 /* Send the digits */
357 ast->state = AST_STATE_DIALING;
358 vofr_digit(ast, *dest);
359 /* Wait .1 seconds before dialing next digit */
364 /* Wait for the ack that it's ringing */
367 otimeout = ast_waitfor(ast, 1000);
369 ast_log(LOG_WARNING, "No acknowledgement for ringing\n");
370 /* Musta gotten hung up, or no ack on off hook */
377 if (f->frametype == AST_FRAME_CONTROL) {
378 if (f->subclass == AST_CONTROL_RINGING) {
379 ast->state = AST_STATE_RINGING;
380 /* We're ringing -- good enough */
383 if (f->subclass == AST_CONTROL_BUSY)
392 /* Wait for an answer, up to timeout... */
393 res = ast_waitfor(ast, timeout);
395 /* Musta gotten hung up */
400 /* Ooh, read what's there. */
404 if ((f->frametype == AST_FRAME_CONTROL) &&
405 (f->subclass == AST_CONTROL_ANSWER))
406 /* Got an answer -- return the # of ms it took */
407 return otimeout - res;
414 static int send_hangup(struct vofr_pvt *p)
416 /* Just send the hangup sequence */
417 return vofr_xmit_signal(p, 0x80, 0);
420 static int vofr_hangup(struct ast_channel *ast)
424 ast_log(LOG_DEBUG, "vofr_hangup(%s)\n", ast->name);
425 if (!ast->pvt->pvt) {
426 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
429 res = send_hangup(ast->pvt->pvt);
431 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
434 ast->state = AST_STATE_DOWN;
435 ((struct vofr_pvt *)(ast->pvt->pvt))->owner = NULL;
436 pthread_mutex_lock(&usecnt_lock);
439 ast_log(LOG_WARNING, "Usecnt < 0???\n");
440 pthread_mutex_unlock(&usecnt_lock);
441 ast_update_use_count();
442 if (option_verbose > 2)
443 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
444 ast->pvt->pvt = NULL;
445 ast->state = AST_STATE_DOWN;
450 static int vofr_answer(struct ast_channel *ast)
458 ast_log(LOG_DEBUG, "vofr_answer(%s)\n", ast->name);
459 res = vofr_xmit_signal(ast->pvt->pvt, VOFR_SIGNAL_OFFHOOK, 0);
461 ast_log(LOG_WARNING, "Unable to anaswer line %s\n", ast->name);
462 ast->state = AST_STATE_UP;
464 cnt = ast_waitfor(ast, cnt);
466 res = read(ast->fd, buf, sizeof(buf));
469 ast_log(LOG_WARNING, "Warning: read failed (%s) on %s\n", strerror(errno), ast->name);
471 /* We're looking for an answer */
472 vh = (struct vofr_hdr *)(buf + FR_API_MESS);
474 case VOFR_TYPE_SIGNAL:
475 switch(vh->data[0]) {
476 case VOFR_SIGNAL_UNKNOWN:
477 switch(vh->data[1]) {
480 ast_log(LOG_DEBUG, "Answered '%s'\n", ast->name);
481 else if (option_verbose > 2)
482 ast_verbose( VERBOSE_PREFIX_3 "Answered '%s'\n", ast->name);
483 ast->state = AST_STATE_UP;
487 ast_log(LOG_WARNING, "Unexpected 'unknown' frame type %d\n", vh->data[1]);
490 case VOFR_SIGNAL_ON_HOOK:
491 /* Ignore onhooks. */
494 ast_log(LOG_WARNING, "Unexpected signal type %d\n", vh->data[0]);
498 ast_log(LOG_WARNING, "Unexpected data type %d\n", vh->dtype);
503 ast_log(LOG_WARNING, "Did not get acknowledged answer\n");
507 static char vofr_2digit(char c)
513 else if ((c < 10) && (c >= 0))
519 static struct ast_frame *vofr_read(struct ast_channel *ast)
524 struct vofr_pvt *p = ast->pvt->pvt;
526 struct ast_frame *fr = (struct ast_frame *)(p->buf);
527 struct vofr_hdr *vh = (struct vofr_hdr *)(p->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET - sizeof(struct vofr_hdr));
528 /* Read into the right place in the buffer, in case we send this
531 res = read(p->s, ((char *)vh) - FR_API_MESS,
532 G723_MAX_BUF - AST_FRIENDLY_OFFSET - sizeof(struct ast_frame) + sizeof(struct vofr_hdr) + FR_API_MESS);
535 if (res < sizeof(struct vofr_hdr *)) {
536 ast_log(LOG_WARNING, "Nonsense frame on %s\n", ast->name);
540 /* Some nice norms */
548 /* Now, what we do depends on what we read */
550 case VOFR_TYPE_SIGNAL:
551 switch(vh->data[0]) {
552 case VOFR_SIGNAL_ON_HOOK:
553 /* Hang up this line */
554 if (ast->state == AST_STATE_UP)
557 fr->frametype = AST_FRAME_NULL;
561 case VOFR_SIGNAL_RING:
564 case VOFR_SIGNAL_UNKNOWN:
565 switch(vh->data[1]) {
567 /* This is a little tricky, because it depends
568 on the context of what state we're in */
570 case AST_STATE_RINGING:
571 fr->frametype = AST_FRAME_CONTROL;
572 fr->subclass = AST_CONTROL_ANSWER;
573 ast->state = AST_STATE_UP;
577 fr->frametype = AST_FRAME_NULL;
583 /* Remote acknowledged off hook */
584 fr->frametype = AST_FRAME_CONTROL;
585 fr->subclass = AST_CONTROL_OFFHOOK;
586 ast->state = AST_STATE_OFFHOOK;
590 fr->frametype = AST_FRAME_CONTROL;
591 fr->subclass = AST_CONTROL_BUSY;
592 ast->state = AST_STATE_BUSY;
595 /* Ringing -- acknowledged */
596 fr->frametype = AST_FRAME_CONTROL;
597 fr->subclass = AST_CONTROL_RINGING;
598 ast->state = AST_STATE_RINGING;
601 /* Hang up detected. Return NULL */
604 ast_log(LOG_WARNING, "Don't know what to do with 'unknown' signal '%d'\n", vh->data[1]);
605 fr->frametype = AST_FRAME_NULL;
611 ast_log(LOG_WARNING, "Don't know what to do with signal '%d'\n", vh->data[0]);
615 /* If it's a DTMF tone, then we want to wait until we don't get any more dtmf tones or
616 the DTMF tone changes.
617 XXX Note: We will drop at least one frame here though XXX */
619 tone = vofr_2digit(vh->data[0]);
622 if ((timeout = ast_waitfor(ast, timeout)) < 1)
625 res = read(p->s, ((char *)vh) - FR_API_MESS,
626 G723_MAX_BUF - AST_FRIENDLY_OFFSET - sizeof(struct ast_frame) + sizeof(struct vofr_hdr) + FR_API_MESS);
629 if (res < sizeof(struct vofr_hdr *)) {
630 ast_log(LOG_WARNING, "Nonsense frame on %s\n", ast->name);
633 if (vh->dtype == VOFR_TYPE_DTMF) {
634 /* Reset the timeout */
636 if ((tone != vofr_2digit(vh->data[0])) )
637 /* Or not... Something else now.. Just send our first frame */
642 fr->frametype = AST_FRAME_DTMF;
648 case VOFR_TYPE_VOICE:
649 /* XXX Bug in the Adtran: Sometimes we don't know when calls are picked up, so if we
650 get voice frames, go ahead and consider it answered even though it probably has
651 not been answered XXX */
652 if ((ast->state == AST_STATE_RINGING) || (ast->state == AST_STATE_DIALING)) {
653 ast_log(LOG_DEBUG, "Adtran bug! (state = %d)\n", ast->state);
654 fr->frametype = AST_FRAME_CONTROL;
655 fr->subclass = AST_CONTROL_ANSWER;
656 ast->state = AST_STATE_UP;
658 } else if (ast->state != AST_STATE_UP) {
659 ast_log(LOG_WARNING, "Voice in weird state %d\n", ast->state);
661 fr->frametype = AST_FRAME_VOICE;
662 fr->subclass = AST_FORMAT_G723_1;
663 fr->datalen = res - sizeof(struct vofr_hdr);
664 fr->data = ((char *)vh) + sizeof(struct vofr_hdr);
666 /* XXX Byte swapping is a bug XXX */
668 for (x=0;x<fr->datalen/2;x++)
669 swapping[x] = ntohs(swapping[x]);
670 fr->offset = AST_FRIENDLY_OFFSET;
671 /* Thirty ms of sound per frame */
675 ast_log(LOG_WARNING, "Don't know what to do with data type %d frames\n", vh->dtype);
677 /* If we don't know what it is, send a NULL frame */
678 fr->frametype = AST_FRAME_NULL;
683 static int vofr_write(struct ast_channel *ast, struct ast_frame *frame)
686 struct vofr_pvt *p = ast->pvt->pvt;
691 /* Write a frame of (presumably voice) data */
692 if (frame->frametype != AST_FRAME_VOICE) {
693 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
696 if (frame->subclass != AST_FORMAT_G723_1) {
697 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
700 /* If we get here, we have a voice frame of G.723.1 data. First check to be
701 sure we have enough headroom for the vofr header. If there isn't enough
702 headroom, we're lazy and just error out rather than copying it into the
703 output buffer, because applications should always leave AST_FRIENDLY_OFFSET
704 bytes just for this reason. */
705 if (frame->offset < sizeof(struct vofr_hdr) + FR_API_MESS) {
706 ast_log(LOG_WARNING, "Frame source '%s' didn't provide a friendly enough offset\n", (frame->src ? frame->src : "**Unknown**"));
709 /* XXX Byte swapping is a bug XXX */
710 swapping = frame->data;
711 for (x=0;x<frame->datalen/2;x++)
712 swapping[x] = ntohs(swapping[x]);
713 vh = (struct vofr_hdr *)(frame->data - sizeof(struct vofr_hdr));
714 /* Some versions of the API have some header mess that needs to be
715 zero'd out and acounted for.. */
716 start = ((void *)vh) - FR_API_MESS;
718 memset(start, 0, FR_API_MESS);
719 /* Now we fill in the vofr header */
720 vh->control = VOFR_CONTROL_VOICE;
721 vh->dtype = VOFR_TYPE_VOICE;
722 vh->vflags = VOFR_ROUTE_NONE;
723 vh->dlcih = p->dlcih;
724 vh->dlcil = p->dlcil;
726 vh->remid = VOFR_CARD_TYPE_ASTERISK;
727 vh->mod = VOFR_MODULATION_SINGLE;
728 res = vofr_xmit(p, start,
729 VOFR_HDR_SIZE + frame->datalen + FR_API_MESS);
731 /* XXX Byte swapping is a bug, but get it back to the right format XXX */
732 swapping = frame->data;
733 for (x=0;x<frame->datalen/2;x++)
734 swapping[x] = htons(swapping[x]);
735 if (res != VOFR_HDR_SIZE + frame->datalen) {
736 ast_log(LOG_WARNING, "Unable to write frame correctly\n");
742 static struct ast_channel *vofr_new(struct vofr_pvt *i, int state)
744 struct ast_channel *tmp;
745 tmp = ast_channel_alloc();
747 snprintf(tmp->name, sizeof(tmp->name), "AdtranVoFR/%s", i->sa.spkt_device);
750 /* Adtran VoFR supports only G723.1 format data. G711 (ulaw) would be nice too */
751 tmp->format = AST_FORMAT_G723_1;
753 if (state == AST_STATE_RING)
756 tmp->pvt->send_digit = vofr_digit;
757 tmp->pvt->call = vofr_call;
758 tmp->pvt->hangup = vofr_hangup;
759 tmp->pvt->answer = vofr_answer;
760 tmp->pvt->read = vofr_read;
761 tmp->pvt->write = vofr_write;
763 pthread_mutex_lock(&usecnt_lock);
765 pthread_mutex_unlock(&usecnt_lock);
766 ast_update_use_count();
767 strncpy(tmp->context, i->context, sizeof(tmp->context));
768 if (state != AST_STATE_DOWN) {
769 if (ast_pbx_start(tmp)) {
770 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
775 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
779 static int vofr_mini_packet(struct vofr_pvt *i, struct vofr_hdr *pkt, int len)
781 /* Here, we're looking for rings or off hooks -- signals that
782 something is about to happen and we need to start the
785 case VOFR_TYPE_SIGNAL:
786 switch(pkt->data[0]) {
787 case VOFR_SIGNAL_RING:
788 /* If we get a RING, we definitely want to start a new thread */
790 vofr_new(i, AST_STATE_RING);
792 ast_log(LOG_WARNING, "Got a ring, but there's an owner?\n");
794 case VOFR_SIGNAL_OFF_HOOK:
795 /* Network termination, go off hook */
797 ast_log(LOG_DEBUG, "Off hook\n");
799 vofr_xmit_signal(i, 0x10, 2);
801 vofr_new(i, AST_STATE_UP);
803 ast_log(LOG_WARNING, "Got an offhook, but there's an owner?\n");
805 case VOFR_SIGNAL_ON_HOOK:
807 case VOFR_SIGNAL_UNKNOWN:
808 switch(pkt->data[1]) {
813 /* A remote hangup request */
815 ast_log(LOG_DEBUG, "Sending hangup reply\n");
819 ast_log(LOG_WARNING, "Unexected 'unknown' signal '%d'\n", pkt->data[1]);
823 ast_log(LOG_DEBUG, "Unknown signal type '%d'\n", pkt->data[0]);
826 case VOFR_TYPE_VOICE:
829 ast_log(LOG_DEBUG, "Unknown packet type '%d'\n", pkt->dtype);
834 static void *do_monitor(void *data)
839 /* This thread monitors all the frame relay interfaces which are not yet in use
840 (and thus do not have a separate thread) indefinitely */
841 /* From here on out, we die whenever asked */
842 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
843 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
847 /* Don't let anybody kill us right away. Nobody should lock the interface list
848 and wait for the monitor list, but the other way around is okay. */
849 if (pthread_mutex_lock(&monlock)) {
850 ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
853 /* Lock the interface list */
854 if (pthread_mutex_lock(&iflock)) {
855 ast_log(LOG_ERROR, "Unable to grab interface lock\n");
856 pthread_mutex_unlock(&monlock);
859 /* Build the stuff we're going to select on, that is the socket of every
860 vofr_pvt that does not have an associated owner channel */
865 if (FD_ISSET(i->s, &rfds))
866 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->s, i->sa.spkt_device);
868 /* This needs to be watched, as it lacks an owner */
875 /* Okay, now that we know what to do, release the interface lock */
876 pthread_mutex_unlock(&iflock);
878 /* And from now on, we're okay to be killed, so release the monitor lock as well */
879 pthread_mutex_unlock(&monlock);
880 /* Wait indefinitely for something to happen */
881 res = select(n + 1, &rfds, NULL, NULL, NULL);
882 /* Okay, select has finished. Let's see what happened. */
884 ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
887 /* Alright, lock the interface list again, and let's look and see what has
889 if (pthread_mutex_lock(&iflock)) {
890 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
895 if (FD_ISSET(i->s, &rfds)) {
897 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d, %s)...\n", i->s, i->sa.spkt_device);
900 res = read(i->s, i->buf, sizeof(i->buf));
903 vofr_dump_packet(i->hdr, res);
905 vofr_mini_packet(i, i->hdr, res);
909 pthread_mutex_unlock(&iflock);
916 static int restart_monitor(void)
918 /* If we're supposed to be stopped -- stay stopped */
919 if (monitor_thread == -2)
921 if (pthread_mutex_lock(&monlock)) {
922 ast_log(LOG_WARNING, "Unable to lock monitor\n");
925 if (monitor_thread == pthread_self()) {
926 pthread_mutex_unlock(&monlock);
927 ast_log(LOG_WARNING, "Cannot kill myself\n");
930 if (monitor_thread != -1) {
931 pthread_cancel(monitor_thread);
933 pthread_join(monitor_thread, NULL);
936 /* Start a new monitor */
937 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
938 pthread_mutex_unlock(&monlock);
939 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
942 pthread_mutex_unlock(&monlock);
946 static struct vofr_pvt *mkif(char *type, char *iface)
948 /* Make a vofr_pvt structure for this interface */
949 struct vofr_pvt *tmp;
952 tmp = malloc(sizeof(struct vofr_pvt));
955 /* Allocate a packet socket */
956 tmp->s = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL));
958 ast_log(LOG_ERROR, "Unable to create socket: %s\n", strerror(errno));
963 /* Prepare sockaddr for binding */
964 memset(&tmp->sa, 0, sizeof(tmp->sa));
965 strncpy(tmp->sa.spkt_device, iface, sizeof(tmp->sa.spkt_device));
966 tmp->sa.spkt_protocol = htons(0x16);
967 tmp->sa.spkt_family = AF_PACKET;
969 /* Bind socket to specific interface */
970 if (bind(tmp->s, (struct sockaddr *)&tmp->sa, sizeof(struct sockaddr))) {
971 ast_log(LOG_ERROR, "Unable to bind to '%s': %s\n", tmp->sa.spkt_device,
977 /* Set magic send buffer size */
978 if (setsockopt(tmp->s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))) {
979 ast_log(LOG_ERROR, "Unable to set send buffer size to %d: %s\n", sndbuf, strerror(errno));
984 tmp->hdr = (struct vofr_hdr *)(tmp->buf + FR_API_MESS);
985 tmp->ohdr = (struct vofr_hdr *)(tmp->obuf + FR_API_MESS);
989 strncpy(tmp->context, context, sizeof(tmp->context));
990 /* User terminations are game for outgoing connections */
991 if (!strcasecmp(type, "user"))
996 /* Hang it up to be sure it's good */
1003 static struct ast_channel *vofr_request(char *type, int format, void *data)
1007 struct ast_channel *tmp = NULL;
1008 /* We can only support G.723.1 formatted frames, but we should never
1009 be asked to support anything else anyway, since we've published
1010 our capabilities when we registered. */
1012 format &= AST_FORMAT_G723_1;
1014 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
1017 /* Search for an unowned channel */
1018 if (pthread_mutex_lock(&iflock)) {
1019 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1025 tmp = vofr_new(p, AST_STATE_DOWN);
1030 pthread_mutex_unlock(&iflock);
1037 struct ast_config *cfg;
1038 struct ast_variable *v;
1039 struct vofr_pvt *tmp;
1040 cfg = ast_load(config);
1042 /* We *must* have a config file otherwise stop immediately */
1044 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1047 if (pthread_mutex_lock(&iflock)) {
1048 /* It's a little silly to lock it, but we mind as well just to be sure */
1049 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1052 v = ast_variable_browse(cfg, "interfaces");
1054 /* Create the interface list */
1055 if (!strcasecmp(v->name, "user") ||
1056 !strcasecmp(v->name, "network")) {
1057 tmp = mkif(v->name, v->value);
1062 ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
1064 pthread_mutex_unlock(&iflock);
1068 } else if (!strcasecmp(v->name, "context")) {
1069 strncpy(context, v->value, sizeof(context));
1073 pthread_mutex_unlock(&iflock);
1074 /* Make sure we can register our AdtranVoFR channel type */
1075 if (ast_channel_register(type, tdesc, AST_FORMAT_G723_1, vofr_request)) {
1076 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1082 /* And start the monitor for the first time */
1091 struct vofr_pvt *p, *pl;
1092 /* First, take us out of the channel loop */
1093 ast_channel_unregister(type);
1094 if (!pthread_mutex_lock(&iflock)) {
1095 /* Hangup all interfaces if they have an owner */
1099 ast_softhangup(p->owner);
1103 pthread_mutex_unlock(&iflock);
1105 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1108 if (!pthread_mutex_lock(&monlock)) {
1109 if (monitor_thread > -1) {
1110 pthread_cancel(monitor_thread);
1111 pthread_join(monitor_thread, NULL);
1113 monitor_thread = -2;
1114 pthread_mutex_unlock(&monlock);
1116 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1120 if (!pthread_mutex_lock(&iflock)) {
1121 /* Destroy all the interfaces and free their memory */
1124 /* Close the socket, assuming it's real */
1129 /* Free associated memory */
1133 pthread_mutex_unlock(&iflock);
1135 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1145 pthread_mutex_lock(&usecnt_lock);
1147 pthread_mutex_unlock(&usecnt_lock);