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 #ifndef OLD_SANGOMA_API
32 #include <linux/if_wanpipe.h>
33 #include <linux/wanpipe.h>
35 #include <sys/signal.h>
36 #include "adtranvofr.h"
38 /* #define VOFRDUMPER */
40 #define G723_MAX_BUF 2048
42 #define FR_API_MESS 16
44 static char *desc = "Adtran Voice over Frame Relay";
45 static char *type = "AdtranVoFR";
46 static char *tdesc = "Voice over Frame Relay/Adtran style";
47 static char *config = "adtranvofr.conf";
49 static char context[AST_MAX_EXTENSION] = "default";
51 static char language[MAX_LANGUAGE] = "";
54 static pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
56 /* Protect the interface list (of vofr_pvt's) */
57 static pthread_mutex_t iflock = PTHREAD_MUTEX_INITIALIZER;
59 /* Protect the monitoring thread, so only one process can kill or start it, and not
60 when it's doing something critical. */
61 static pthread_mutex_t monlock = PTHREAD_MUTEX_INITIALIZER;
63 /* This is the thread for the monitor which checks for input on the channels
64 which are not currently in use. */
65 static pthread_t monitor_thread = 0;
67 static int restart_monitor(void);
69 /* The private structures of the Adtran VoFR channels are linked for
70 selecting outgoing channels */
72 static struct vofr_pvt {
73 int s; /* Raw socket for this DLCI */
74 #ifdef OLD_SANGOMA_API
75 struct sockaddr_pkt sa; /* Sockaddr needed for sending, also has iface name */
77 struct wan_sockaddr_ll sa; /* Wanpipe sockaddr */
79 struct ast_channel *owner; /* Channel we belong to, possibly NULL */
80 int outgoing; /* Does this channel support outgoing calls? */
81 struct vofr_pvt *next; /* Next channel in list */
82 struct vofr_hdr *hdr; /* VOFR header version of buf */
83 struct vofr_hdr *ohdr;
84 u_int8_t dlcih; /* High two bits of DLCI */
85 u_int8_t dlcil; /* Bottom two bits of DLCI */
86 u_int8_t cid; /* Call ID */
87 char buf[G723_MAX_BUF]; /* Static buffer for reading frames */
88 char obuf[G723_MAX_BUF]; /* Output buffer */
89 char context[AST_MAX_EXTENSION];
90 char language[MAX_LANGUAGE];
91 int ringgothangup; /* Have we received exactly one hangup after a ring */
96 /* Some useful debugging routines */
98 static char *set(int val)
100 return (val ? "Set " : "Unset");
103 static char *controlstr(int control)
106 case VOFR_CONTROL_ADTRAN:
107 return "Adtran Proprietary";
108 case VOFR_CONTROL_VOICE:
110 case VOFR_CONTROL_RFC1490:
116 static char *dtypestr(int control)
119 case VOFR_TYPE_SIGNAL:
120 return "Signal Frame";
121 case VOFR_TYPE_VOICE:
122 return "Voice Frame";
123 case VOFR_TYPE_ANSWER:
124 return "Answer Tone";
133 static char *vflagsstr(int flags)
139 if (flags & VOFR_ROUTE_LOCAL)
140 strcat(buf, "Local ");
141 if (flags & VOFR_ROUTE_VOICE)
142 strcat(buf, "Voice ");
143 if (flags & VOFR_ROUTE_DTE)
145 else if (flags & VOFR_ROUTE_DTE1)
146 strcat(buf, "DTE1 ");
147 else if (flags & VOFR_ROUTE_DTE2)
148 strcat(buf, "DTE2 ");
152 static char *remidstr(int remid)
155 case VOFR_CARD_TYPE_UNSPEC:
156 return "Unspecified";
157 case VOFR_CARD_TYPE_FXS:
159 case VOFR_CARD_TYPE_FXO:
161 case VOFR_CARD_TYPE_ENM:
163 case VOFR_CARD_TYPE_VCOM:
169 static char *modulationstr(int modulation)
172 case VOFR_MODULATION_SINGLE:
173 return "Single Frequency";
174 case VOFR_MODULATION_V21:
176 case VOFR_MODULATION_V27ter_2:
177 return "V.27 (2400bps)";
178 case VOFR_MODULATION_V27ter_4:
179 return "V.27 (4800bps)";
180 case VOFR_MODULATION_V29_7:
181 return "V.29 (7200bps)";
182 case VOFR_MODULATION_V29_9:
183 return "V.29 (9600bps)";
184 case VOFR_MODULATION_V33_12:
185 return "V.33 (12000bps)";
186 case VOFR_MODULATION_V33_14:
187 return "V.33 (14400BPS)";
192 static char *signalstr(int signal)
195 case VOFR_SIGNAL_ON_HOOK:
197 case VOFR_SIGNAL_OFF_HOOK:
199 case VOFR_SIGNAL_RING:
201 case VOFR_SIGNAL_SWITCHED_DIAL:
202 return "Switched Dial";
203 case VOFR_SIGNAL_BUSY:
205 case VOFR_SIGNAL_TRUNK_BUSY:
211 static char *vofr_digitstr(int val)
215 snprintf(num, sizeof(num), "%d", val);
228 static void vofr_dump_packet(struct vofr_hdr *vh, int len)
230 printf("VoFR Packet Dump\n");
231 printf("================\n");
232 printf("EI: %s ", set(vh->control & VOFR_MASK_EI));
233 printf("LI: %s\n", set(vh->control & VOFR_MASK_LI));
234 printf("Control: %s (0x%02x)\n",
235 controlstr(vh->control & VOFR_MASK_CONTROL), vh->control & VOFR_MASK_CONTROL);
236 printf("Data Type: %s (0x%02x)\n", dtypestr(vh->dtype), vh->dtype);
237 if (vh->dtype == VOFR_TYPE_SIGNAL) {
238 printf(" \\--Signal: %s (0x%02x)\n", signalstr(vh->data[0]), vh->data[0]);
240 if (vh->dtype == VOFR_TYPE_DTMF) {
241 printf(" \\--Digit: %s (0x%02x)\n", vofr_digitstr(vh->data[0]), vh->data[0]);
243 printf("Connect Tag: 0x%02x\n", vh->ctag);
244 printf("Voice Rt Flags: %s\n", vflagsstr(vh->vflags));
245 printf("DLCI X-Ref: %d\n", (vh->dlcih << 8) | (vh->dlcil));
246 printf("Channel ID: %d\n", vh->cid);
247 printf("Remote ID: %s (0x%02x)\n", remidstr(vh->remid), vh->remid);
248 printf("Modulation: %s (0x%02x)\n", modulationstr(vh->mod), vh->mod);
255 static int vofr_xmit(struct vofr_pvt *p, char *data, int len)
258 #ifdef OLD_SANGOMA_API
259 res=sendto(p->s, data, len, 0, (struct sockaddr *)&p->sa, sizeof(struct sockaddr_pkt));
261 res=sendto(p->s, data, len, 0, (struct sockaddr *)&p->sa, sizeof(struct wan_sockaddr_ll));
264 ast_log(LOG_WARNING, "vofr_xmit returned %d\n", res);
269 static int vofr_digit(struct ast_channel *ast, char digit)
272 * 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
273 * A N D D O E S N O T S O U N D R I G H T
274 * XXX Figure out how to really send a decent digit XXX
280 vh->control = VOFR_CONTROL_VOICE;
281 vh->dtype = VOFR_TYPE_DTMF;
282 vh->vflags = VOFR_ROUTE_NONE;
283 vh->dlcih = p->dlcih;
284 vh->dlcil = p->dlcil;
286 vh->remid = VOFR_CARD_TYPE_ASTERISK;
287 vh->mod = VOFR_MODULATION_SINGLE;
288 if ((digit >= '0') && (digit <= '9'))
289 vh->data[0] = digit - '0';
290 else if (digit == '*')
292 else if (digit == '#')
295 ast_log(LOG_WARNING, "%s: tried to dial a non digit '%c'\n", ast->name, digit);
301 /* We sorta start the digit */
302 vofr_xmit(p, p->obuf, VOFR_HDR_SIZE + 4 + FR_API_MESS);
304 /* And terminate with an empty voice frame */
305 vh->control = VOFR_CONTROL_VOICE;
306 vh->dtype = VOFR_TYPE_VOICE;
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 vofr_xmit(p, p->obuf, VOFR_HDR_SIZE + FR_API_MESS);
317 static int vofr_xmit_signal(struct vofr_pvt *p, int signal, int pad)
319 /* Prepare and transmit outgoing buffer with given signal and
320 pad the end with *pad* bytes of data presumed to already
321 be in the buffer (like DTMF tones, etc) */
322 struct vofr_hdr *vh = p->ohdr;
324 vh->control = VOFR_CONTROL_VOICE;
325 vh->dtype = VOFR_TYPE_SIGNAL;
326 vh->vflags = VOFR_ROUTE_NONE;
327 vh->dlcih = p->dlcih;
328 vh->dlcil = p->dlcil;
330 vh->remid = VOFR_CARD_TYPE_ASTERISK;
331 vh->mod = VOFR_MODULATION_SINGLE;
332 vh->data[0] = signal;
334 memset(p->obuf, 0, FR_API_MESS);
335 res = vofr_xmit(p, p->obuf, VOFR_HDR_SIZE + pad + 1 + FR_API_MESS);
340 static int vofr_call(struct ast_channel *ast, char *dest, int timeout)
347 if ((ast->state != AST_STATE_DOWN) && (ast->state != AST_STATE_RESERVED)) {
348 ast_log(LOG_WARNING, "vofr_call called on %s, neither down nor reserved\n", ast->name);
351 /* Take the system off hook */
352 vofr_xmit_signal(p, VOFR_SIGNAL_OFFHOOK, 0);
353 /* Wait for an acknowledgement */
356 otimeout = ast_waitfor(ast, 1000);
358 ast_log(LOG_WARNING, "Unable to take line off hook\n");
359 /* Musta gotten hung up, or no ack on off hook */
365 if ((f->frametype == AST_FRAME_CONTROL) &&
366 (f->subclass == AST_CONTROL_OFFHOOK))
371 ast_log(LOG_WARNING, "Unable to take line off hook\n");
374 /* Send the digits */
376 ast->state = AST_STATE_DIALING;
377 vofr_digit(ast, *dest);
378 /* Wait .1 seconds before dialing next digit */
383 /* Wait for the ack that it's ringing */
386 otimeout = ast_waitfor(ast, 1000);
388 ast_log(LOG_WARNING, "No acknowledgement for ringing\n");
389 /* Musta gotten hung up, or no ack on off hook */
396 if (f->frametype == AST_FRAME_CONTROL) {
397 if (f->subclass == AST_CONTROL_RINGING) {
398 ast->state = AST_STATE_RINGING;
399 /* We're ringing -- good enough */
402 if (f->subclass == AST_CONTROL_BUSY)
411 /* Wait for an answer, up to timeout... */
412 res = ast_waitfor(ast, timeout);
414 /* Musta gotten hung up */
419 /* Ooh, read what's there. */
423 if ((f->frametype == AST_FRAME_CONTROL) &&
424 (f->subclass == AST_CONTROL_ANSWER))
425 /* Got an answer -- return the # of ms it took */
426 return otimeout - res;
433 static int send_hangup(struct vofr_pvt *p)
435 /* Just send the hangup sequence */
436 return vofr_xmit_signal(p, 0x80, 0);
439 static int vofr_hangup(struct ast_channel *ast)
443 ast_log(LOG_DEBUG, "vofr_hangup(%s)\n", ast->name);
444 if (!ast->pvt->pvt) {
445 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
448 res = send_hangup(ast->pvt->pvt);
450 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
453 ast->state = AST_STATE_DOWN;
454 ((struct vofr_pvt *)(ast->pvt->pvt))->owner = NULL;
455 ((struct vofr_pvt *)(ast->pvt->pvt))->ringgothangup = 0;
456 pthread_mutex_lock(&usecnt_lock);
459 ast_log(LOG_WARNING, "Usecnt < 0???\n");
460 pthread_mutex_unlock(&usecnt_lock);
461 ast_update_use_count();
462 if (option_verbose > 2)
463 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
464 ast->pvt->pvt = NULL;
465 ast->state = AST_STATE_DOWN;
470 static int vofr_answer(struct ast_channel *ast)
478 ast_log(LOG_DEBUG, "vofr_answer(%s)\n", ast->name);
479 res = vofr_xmit_signal(ast->pvt->pvt, VOFR_SIGNAL_OFFHOOK, 0);
481 ast_log(LOG_WARNING, "Unable to anaswer line %s\n", ast->name);
482 ast->state = AST_STATE_UP;
484 cnt = ast_waitfor(ast, cnt);
486 res = read(ast->fd, buf, sizeof(buf));
488 vofr_dump_packet((void *)(buf +FR_API_MESS), res - FR_API_MESS);
492 ast_log(LOG_WARNING, "Warning: read failed (%s) on %s\n", strerror(errno), ast->name);
494 /* We're looking for an answer */
495 vh = (struct vofr_hdr *)(buf + FR_API_MESS);
497 case VOFR_TYPE_SIGNAL:
498 switch(vh->data[0]) {
499 case VOFR_SIGNAL_UNKNOWN:
500 switch(vh->data[1]) {
503 ast_log(LOG_DEBUG, "Answered '%s'\n", ast->name);
504 else if (option_verbose > 2)
505 ast_verbose( VERBOSE_PREFIX_3 "Answered '%s'\n", ast->name);
506 ast->state = AST_STATE_UP;
510 ast_log(LOG_WARNING, "Unexpected 'unknown' frame type %d\n", vh->data[1]);
513 case VOFR_SIGNAL_ON_HOOK:
514 /* Ignore onhooks. */
517 ast_log(LOG_WARNING, "Unexpected signal type %d\n", vh->data[0]);
521 ast_log(LOG_WARNING, "Unexpected data type %d\n", vh->dtype);
526 ast_log(LOG_WARNING, "Did not get acknowledged answer\n");
530 static char vofr_2digit(char c)
536 else if ((c < 10) && (c >= 0))
542 static struct ast_frame *vofr_read(struct ast_channel *ast)
547 struct vofr_pvt *p = ast->pvt->pvt;
549 struct ast_frame *fr = (struct ast_frame *)(p->buf);
550 struct vofr_hdr *vh = (struct vofr_hdr *)(p->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET - sizeof(struct vofr_hdr));
551 /* Read into the right place in the buffer, in case we send this
554 res = read(p->s, ((char *)vh) - FR_API_MESS,
555 G723_MAX_BUF - AST_FRIENDLY_OFFSET - sizeof(struct ast_frame) + sizeof(struct vofr_hdr) + FR_API_MESS);
557 vofr_dump_packet((void *)(vh), res);
561 if (res < sizeof(struct vofr_hdr *)) {
562 ast_log(LOG_WARNING, "Nonsense frame on %s\n", ast->name);
566 /* Some nice norms */
574 /* Now, what we do depends on what we read */
576 case VOFR_TYPE_SIGNAL:
577 switch(vh->data[0]) {
578 case VOFR_SIGNAL_ON_HOOK:
579 /* Hang up this line */
580 if ((ast->state == AST_STATE_UP) || (p->ringgothangup)) {
583 fr->frametype = AST_FRAME_NULL;
588 case VOFR_SIGNAL_RING:
590 p->ringgothangup = 0;
592 case VOFR_SIGNAL_UNKNOWN:
593 switch(vh->data[1]) {
595 /* This is a little tricky, because it depends
596 on the context of what state we're in */
598 case AST_STATE_RINGING:
599 fr->frametype = AST_FRAME_CONTROL;
600 fr->subclass = AST_CONTROL_ANSWER;
601 ast->state = AST_STATE_UP;
605 fr->frametype = AST_FRAME_NULL;
611 /* Remote acknowledged off hook */
612 fr->frametype = AST_FRAME_CONTROL;
613 fr->subclass = AST_CONTROL_OFFHOOK;
614 ast->state = AST_STATE_OFFHOOK;
618 fr->frametype = AST_FRAME_CONTROL;
619 fr->subclass = AST_CONTROL_BUSY;
620 ast->state = AST_STATE_BUSY;
623 /* Ringing -- acknowledged */
624 fr->frametype = AST_FRAME_CONTROL;
625 fr->subclass = AST_CONTROL_RINGING;
626 ast->state = AST_STATE_RINGING;
629 /* Hang up detected. Return NULL */
632 ast_log(LOG_WARNING, "Don't know what to do with 'unknown' signal '%d'\n", vh->data[1]);
633 fr->frametype = AST_FRAME_NULL;
639 ast_log(LOG_WARNING, "Don't know what to do with signal '%d'\n", vh->data[0]);
643 /* If it's a DTMF tone, then we want to wait until we don't get any more dtmf tones or
644 the DTMF tone changes.
645 XXX Note: We will drop at least one frame here though XXX */
647 tone = vofr_2digit(vh->data[0]);
650 if ((timeout = ast_waitfor(ast, timeout)) < 1)
653 res = read(p->s, ((char *)vh) - FR_API_MESS,
654 G723_MAX_BUF - AST_FRIENDLY_OFFSET - sizeof(struct ast_frame) + sizeof(struct vofr_hdr) + FR_API_MESS);
657 if (res < sizeof(struct vofr_hdr *)) {
658 ast_log(LOG_WARNING, "Nonsense frame on %s\n", ast->name);
661 if (vh->dtype == VOFR_TYPE_DTMF) {
662 /* Reset the timeout */
664 if ((tone != vofr_2digit(vh->data[0])) )
665 /* Or not... Something else now.. Just send our first frame */
670 fr->frametype = AST_FRAME_DTMF;
676 case VOFR_TYPE_VOICE:
677 /* XXX Bug in the Adtran: Sometimes we don't know when calls are picked up, so if we
678 get voice frames, go ahead and consider it answered even though it probably has
679 not been answered XXX */
680 if ((ast->state == AST_STATE_RINGING) || (ast->state == AST_STATE_DIALING)) {
681 ast_log(LOG_DEBUG, "Adtran bug! (state = %d)\n", ast->state);
682 fr->frametype = AST_FRAME_CONTROL;
683 fr->subclass = AST_CONTROL_ANSWER;
684 ast->state = AST_STATE_UP;
686 } else if (ast->state != AST_STATE_UP) {
687 ast_log(LOG_WARNING, "Voice in weird state %d\n", ast->state);
689 fr->frametype = AST_FRAME_VOICE;
690 fr->subclass = AST_FORMAT_G723_1;
691 fr->datalen = res - sizeof(struct vofr_hdr);
692 fr->data = ((char *)vh) + sizeof(struct vofr_hdr);
694 /* XXX Byte swapping is a bug XXX */
696 for (x=0;x<fr->datalen/2;x++)
697 swapping[x] = ntohs(swapping[x]);
698 fr->offset = AST_FRIENDLY_OFFSET;
699 /* Thirty ms of sound per frame */
703 ast_log(LOG_WARNING, "Don't know what to do with data type %d frames\n", vh->dtype);
705 /* If we don't know what it is, send a NULL frame */
706 fr->frametype = AST_FRAME_NULL;
711 static int vofr_write(struct ast_channel *ast, struct ast_frame *frame)
714 struct vofr_pvt *p = ast->pvt->pvt;
719 /* Write a frame of (presumably voice) data */
720 if (frame->frametype != AST_FRAME_VOICE) {
721 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
724 if (frame->subclass != AST_FORMAT_G723_1) {
725 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
728 /* If we get here, we have a voice frame of G.723.1 data. First check to be
729 sure we have enough headroom for the vofr header. If there isn't enough
730 headroom, we're lazy and just error out rather than copying it into the
731 output buffer, because applications should always leave AST_FRIENDLY_OFFSET
732 bytes just for this reason. */
733 if (frame->offset < sizeof(struct vofr_hdr) + FR_API_MESS) {
734 ast_log(LOG_WARNING, "Frame source '%s' didn't provide a friendly enough offset\n", (frame->src ? frame->src : "**Unknown**"));
737 /* XXX Byte swapping is a bug XXX */
738 swapping = frame->data;
739 for (x=0;x<frame->datalen/2;x++)
740 swapping[x] = ntohs(swapping[x]);
741 vh = (struct vofr_hdr *)(frame->data - sizeof(struct vofr_hdr));
742 /* Some versions of the API have some header mess that needs to be
743 zero'd out and acounted for.. */
744 start = ((void *)vh) - FR_API_MESS;
746 memset(start, 0, FR_API_MESS);
747 /* Now we fill in the vofr header */
748 vh->control = VOFR_CONTROL_VOICE;
749 vh->dtype = VOFR_TYPE_VOICE;
750 vh->vflags = VOFR_ROUTE_NONE;
751 vh->dlcih = p->dlcih;
752 vh->dlcil = p->dlcil;
754 vh->remid = VOFR_CARD_TYPE_ASTERISK;
755 vh->mod = VOFR_MODULATION_SINGLE;
756 res = vofr_xmit(p, start,
757 VOFR_HDR_SIZE + frame->datalen + FR_API_MESS);
759 /* XXX Byte swapping is a bug, but get it back to the right format XXX */
760 swapping = frame->data;
761 for (x=0;x<frame->datalen/2;x++)
762 swapping[x] = htons(swapping[x]);
763 if (res != VOFR_HDR_SIZE + frame->datalen) {
764 ast_log(LOG_WARNING, "Unable to write frame correctly\n");
770 static struct ast_channel *vofr_new(struct vofr_pvt *i, int state)
772 struct ast_channel *tmp;
773 tmp = ast_channel_alloc();
775 #ifdef OLD_SANGOMA_API
776 snprintf(tmp->name, sizeof(tmp->name), "AdtranVoFR/%s", i->sa.spkt_device);
778 snprintf(tmp->name, sizeof(tmp->name), "AdtranVoFR/%s", i->sa.sll_device);
782 /* Adtran VoFR supports only G723.1 format data. G711 (ulaw) would be nice too */
783 tmp->format = AST_FORMAT_G723_1;
785 if (state == AST_STATE_RING)
788 tmp->pvt->send_digit = vofr_digit;
789 tmp->pvt->call = vofr_call;
790 tmp->pvt->hangup = vofr_hangup;
791 tmp->pvt->answer = vofr_answer;
792 tmp->pvt->read = vofr_read;
793 tmp->pvt->write = vofr_write;
794 if (strlen(i->language))
795 strncpy(tmp->language, i->language, sizeof(tmp->language));
797 pthread_mutex_lock(&usecnt_lock);
799 pthread_mutex_unlock(&usecnt_lock);
800 ast_update_use_count();
801 strncpy(tmp->context, i->context, sizeof(tmp->context));
802 if (state != AST_STATE_DOWN) {
803 if (ast_pbx_start(tmp)) {
804 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
810 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
814 static int vofr_mini_packet(struct vofr_pvt *i, struct vofr_hdr *pkt, int len)
816 /* Here, we're looking for rings or off hooks -- signals that
817 something is about to happen and we need to start the
820 case VOFR_TYPE_SIGNAL:
821 switch(pkt->data[0]) {
822 case VOFR_SIGNAL_RING:
823 /* If we get a RING, we definitely want to start a new thread */
825 i->ringgothangup = 0;
826 vofr_new(i, AST_STATE_RING);
828 ast_log(LOG_WARNING, "Got a ring, but there's an owner?\n");
830 case VOFR_SIGNAL_OFF_HOOK:
831 /* Network termination, go off hook */
833 ast_log(LOG_DEBUG, "Off hook\n");
835 vofr_xmit_signal(i, 0x10, 2);
837 vofr_new(i, AST_STATE_UP);
839 ast_log(LOG_WARNING, "Got an offhook, but there's an owner?\n");
841 case VOFR_SIGNAL_ON_HOOK:
843 case VOFR_SIGNAL_UNKNOWN:
844 switch(pkt->data[1]) {
849 /* A remote hangup request */
851 ast_log(LOG_DEBUG, "Sending hangup reply\n");
855 ast_log(LOG_WARNING, "Unexected 'unknown' signal '%d'\n", pkt->data[1]);
859 ast_log(LOG_DEBUG, "Unknown signal type '%d'\n", pkt->data[0]);
862 case VOFR_TYPE_VOICE:
865 ast_log(LOG_DEBUG, "Unknown packet type '%d'\n", pkt->dtype);
870 static void *do_monitor(void *data)
875 /* This thread monitors all the frame relay interfaces which are not yet in use
876 (and thus do not have a separate thread) indefinitely */
877 /* From here on out, we die whenever asked */
879 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
880 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
885 /* Don't let anybody kill us right away. Nobody should lock the interface list
886 and wait for the monitor list, but the other way around is okay. */
887 if (pthread_mutex_lock(&monlock)) {
888 ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
891 /* Lock the interface list */
892 if (pthread_mutex_lock(&iflock)) {
893 ast_log(LOG_ERROR, "Unable to grab interface lock\n");
894 pthread_mutex_unlock(&monlock);
897 /* Build the stuff we're going to select on, that is the socket of every
898 vofr_pvt that does not have an associated owner channel */
903 if (FD_ISSET(i->s, &rfds))
904 #ifdef OLD_SANGOMA_API
905 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->s, i->sa.spkt_device);
907 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->s, i->sa.sll_device);
910 /* This needs to be watched, as it lacks an owner */
917 /* Okay, now that we know what to do, release the interface lock */
918 pthread_mutex_unlock(&iflock);
920 /* And from now on, we're okay to be killed, so release the monitor lock as well */
921 pthread_mutex_unlock(&monlock);
922 pthread_testcancel();
923 /* Wait indefinitely for something to happen */
924 res = select(n + 1, &rfds, NULL, NULL, NULL);
925 pthread_testcancel();
926 /* Okay, select has finished. Let's see what happened. */
928 if ((errno != EAGAIN) && (errno != EINTR))
929 ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
932 /* Alright, lock the interface list again, and let's look and see what has
934 if (pthread_mutex_lock(&iflock)) {
935 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
940 if (FD_ISSET(i->s, &rfds)) {
942 #ifdef OLD_SANGOMA_API
943 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d, %s)...\n", i->s, i->sa.spkt_device);
945 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d, %s)...\n", i->s, i->sa.sll_device);
949 res = read(i->s, i->buf, sizeof(i->buf));
952 vofr_dump_packet(i->hdr, res);
954 vofr_mini_packet(i, i->hdr, res);
958 pthread_mutex_unlock(&iflock);
965 static int restart_monitor(void)
967 /* If we're supposed to be stopped -- stay stopped */
968 if (monitor_thread == -2)
970 if (pthread_mutex_lock(&monlock)) {
971 ast_log(LOG_WARNING, "Unable to lock monitor\n");
974 if (monitor_thread == pthread_self()) {
975 pthread_mutex_unlock(&monlock);
976 ast_log(LOG_WARNING, "Cannot kill myself\n");
979 if (monitor_thread) {
981 pthread_cancel(monitor_thread);
983 pthread_kill(monitor_thread, SIGURG);
985 pthread_join(monitor_thread, NULL);
989 /* Start a new monitor */
990 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
991 pthread_mutex_unlock(&monlock);
992 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
995 pthread_mutex_unlock(&monlock);
999 static struct vofr_pvt *mkif(char *type, char *iface)
1001 /* Make a vofr_pvt structure for this interface */
1002 struct vofr_pvt *tmp;
1005 tmp = malloc(sizeof(struct vofr_pvt));
1008 /* Allocate a packet socket */
1009 #ifdef OLD_SANGOMA_API
1010 tmp->s = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL));
1012 /* Why the HELL does Sangoma change their API every damn time
1013 they make a new driver release?!?!?! Leave it the hell
1015 tmp->s = socket(AF_WANPIPE, SOCK_RAW, 0);
1019 ast_log(LOG_ERROR, "Unable to create socket: %s\n", strerror(errno));
1024 #ifdef OLD_SANGOMA_API
1025 /* Prepare sockaddr for binding */
1026 memset(&tmp->sa, 0, sizeof(tmp->sa));
1027 strncpy(tmp->sa.spkt_device, iface, sizeof(tmp->sa.spkt_device));
1028 tmp->sa.spkt_protocol = htons(0x16);
1029 tmp->sa.spkt_family = AF_PACKET;
1030 if (bind(tmp->s, (struct sockaddr *)&tmp->sa, sizeof(struct sockaddr))) {
1032 /* Prepare sockaddr for binding */
1033 memset(&tmp->sa, 0, sizeof(tmp->sa));
1034 tmp->sa.sll_family = AF_WANPIPE;
1035 tmp->sa.sll_protocol = htons(ETH_P_IP);
1036 strncpy(tmp->sa.sll_device, iface, sizeof(tmp->sa.sll_device));
1037 strncpy(tmp->sa.sll_card, "wanpipe1", sizeof(tmp->sa.sll_card));
1038 tmp->sa.sll_ifindex = 0;
1039 if (bind(tmp->s, (struct sockaddr *)&tmp->sa, sizeof(struct wan_sockaddr_ll))) {
1041 /* Bind socket to specific interface */
1042 #ifdef OLD_SANGOMA_API
1043 ast_log(LOG_ERROR, "Unable to bind to '%s': %s\n", tmp->sa.spkt_device,
1045 ast_log(LOG_ERROR, "Unable to bind to '%s': %s\n", tmp->sa.sll_device,
1052 /* Set magic send buffer size */
1053 if (setsockopt(tmp->s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))) {
1054 ast_log(LOG_ERROR, "Unable to set send buffer size to %d: %s\n", sndbuf, strerror(errno));
1059 tmp->hdr = (struct vofr_hdr *)(tmp->buf + FR_API_MESS);
1060 tmp->ohdr = (struct vofr_hdr *)(tmp->obuf + FR_API_MESS);
1064 tmp->ringgothangup = 0;
1065 strncpy(tmp->language, language, sizeof(tmp->language));
1066 strncpy(tmp->context, context, sizeof(tmp->context));
1067 /* User terminations are game for outgoing connections */
1068 if (!strcasecmp(type, "user"))
1073 /* Hang it up to be sure it's good */
1080 static struct ast_channel *vofr_request(char *type, int format, void *data)
1084 struct ast_channel *tmp = NULL;
1085 /* We can only support G.723.1 formatted frames, but we should never
1086 be asked to support anything else anyway, since we've published
1087 our capabilities when we registered. */
1089 format &= AST_FORMAT_G723_1;
1091 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
1094 /* Search for an unowned channel */
1095 if (pthread_mutex_lock(&iflock)) {
1096 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1102 tmp = vofr_new(p, AST_STATE_DOWN);
1107 pthread_mutex_unlock(&iflock);
1114 struct ast_config *cfg;
1115 struct ast_variable *v;
1116 struct vofr_pvt *tmp;
1117 cfg = ast_load(config);
1119 /* We *must* have a config file otherwise stop immediately */
1121 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1124 if (pthread_mutex_lock(&iflock)) {
1125 /* It's a little silly to lock it, but we mind as well just to be sure */
1126 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1129 v = ast_variable_browse(cfg, "interfaces");
1131 /* Create the interface list */
1132 if (!strcasecmp(v->name, "user") ||
1133 !strcasecmp(v->name, "network")) {
1134 tmp = mkif(v->name, v->value);
1139 ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
1141 pthread_mutex_unlock(&iflock);
1145 } else if (!strcasecmp(v->name, "context")) {
1146 strncpy(context, v->value, sizeof(context));
1147 } else if (!strcasecmp(v->name, "language")) {
1148 strncpy(language, v->value, sizeof(language));
1152 pthread_mutex_unlock(&iflock);
1153 /* Make sure we can register our AdtranVoFR channel type */
1154 if (ast_channel_register(type, tdesc, AST_FORMAT_G723_1, vofr_request)) {
1155 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1161 /* And start the monitor for the first time */
1168 struct vofr_pvt *p, *pl;
1169 /* First, take us out of the channel loop */
1170 ast_channel_unregister(type);
1171 if (!pthread_mutex_lock(&iflock)) {
1172 /* Hangup all interfaces if they have an owner */
1176 ast_softhangup(p->owner);
1180 pthread_mutex_unlock(&iflock);
1182 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1185 if (!pthread_mutex_lock(&monlock)) {
1186 if (monitor_thread) {
1187 pthread_cancel(monitor_thread);
1188 pthread_kill(monitor_thread, SIGURG);
1189 pthread_join(monitor_thread, NULL);
1191 monitor_thread = -2;
1192 pthread_mutex_unlock(&monlock);
1194 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1198 if (!pthread_mutex_lock(&iflock)) {
1199 /* Destroy all the interfaces and free their memory */
1202 /* Close the socket, assuming it's real */
1207 /* Free associated memory */
1211 pthread_mutex_unlock(&iflock);
1213 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1223 pthread_mutex_lock(&usecnt_lock);
1225 pthread_mutex_unlock(&usecnt_lock);