📄 chan_features.c
字号:
/* * Asterisk -- An open source telephony toolkit. * * Copyright (C) 1999 - 2005, Digium, Inc. * * Mark Spencer <markster@digium.com> * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * * This program is free software, distributed under the terms of * the GNU General Public License Version 2. See the LICENSE file * at the top of the source tree. *//*! \file * * \brief feature Proxy Channel * * \author Mark Spencer <markster@digium.com> * * \note *** Experimental code **** * * \ingroup channel_drivers *//*** MODULEINFO <defaultenabled>no</defaultenabled> ***/#include "asterisk.h"ASTERISK_FILE_VERSION(__FILE__, "$Revision: 68196 $")#include <stdio.h>#include <string.h>#include <unistd.h>#include <sys/socket.h>#include <errno.h>#include <stdlib.h>#include <fcntl.h>#include <netdb.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/signal.h>#include "asterisk/lock.h"#include "asterisk/channel.h"#include "asterisk/config.h"#include "asterisk/logger.h"#include "asterisk/module.h"#include "asterisk/pbx.h"#include "asterisk/options.h"#include "asterisk/lock.h"#include "asterisk/sched.h"#include "asterisk/io.h"#include "asterisk/rtp.h"#include "asterisk/acl.h"#include "asterisk/callerid.h"#include "asterisk/file.h"#include "asterisk/cli.h"#include "asterisk/app.h"#include "asterisk/musiconhold.h"#include "asterisk/manager.h"#include "asterisk/stringfields.h"static const char tdesc[] = "Feature Proxy Channel Driver";#define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)struct feature_sub { struct ast_channel *owner; int inthreeway; int pfd; int timingfdbackup; int alertpipebackup[2];};struct feature_pvt { ast_mutex_t lock; /* Channel private lock */ char tech[AST_MAX_EXTENSION]; /* Technology to abstract */ char dest[AST_MAX_EXTENSION]; /* Destination to abstract */ struct ast_channel *subchan; struct feature_sub subs[3]; /* Subs */ struct ast_channel *owner; /* Current Master Channel */ AST_LIST_ENTRY(feature_pvt) list; /* Next entity */};static AST_LIST_HEAD_STATIC(features, feature_pvt);#define SUB_REAL 0 /* Active call */#define SUB_CALLWAIT 1 /* Call-Waiting call on hold */#define SUB_THREEWAY 2 /* Three-way call */static struct ast_channel *features_request(const char *type, int format, void *data, int *cause);static int features_digit_begin(struct ast_channel *ast, char digit);static int features_digit_end(struct ast_channel *ast, char digit, unsigned int duration);static int features_call(struct ast_channel *ast, char *dest, int timeout);static int features_hangup(struct ast_channel *ast);static int features_answer(struct ast_channel *ast);static struct ast_frame *features_read(struct ast_channel *ast);static int features_write(struct ast_channel *ast, struct ast_frame *f);static int features_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);static const struct ast_channel_tech features_tech = { .type = "Feature", .description = tdesc, .capabilities = -1, .requester = features_request, .send_digit_begin = features_digit_begin, .send_digit_end = features_digit_end, .call = features_call, .hangup = features_hangup, .answer = features_answer, .read = features_read, .write = features_write, .exception = features_read, .indicate = features_indicate, .fixup = features_fixup,};static inline void init_sub(struct feature_sub *sub){ sub->inthreeway = 0; sub->pfd = -1; sub->timingfdbackup = -1; sub->alertpipebackup[0] = sub->alertpipebackup[1] = -1;}static inline int indexof(struct feature_pvt *p, struct ast_channel *owner, int nullok){ int x; if (!owner) { ast_log(LOG_WARNING, "indexof called on NULL owner??\n"); return -1; } for (x=0; x<3; x++) { if (owner == p->subs[x].owner) return x; } return -1;}#if 0static void wakeup_sub(struct feature_pvt *p, int a){ struct ast_frame null = { AST_FRAME_NULL, }; for (;;) { if (p->subs[a].owner) { if (ast_mutex_trylock(&p->subs[a].owner->lock)) { ast_mutex_unlock(&p->lock); usleep(1); ast_mutex_lock(&p->lock); } else { ast_queue_frame(p->subs[a].owner, &null); ast_mutex_unlock(&p->subs[a].owner->lock); break; } } else break; }}#endifstatic void restore_channel(struct feature_pvt *p, int index){ /* Restore timing/alertpipe */ p->subs[index].owner->timingfd = p->subs[index].timingfdbackup; p->subs[index].owner->alertpipe[0] = p->subs[index].alertpipebackup[0]; p->subs[index].owner->alertpipe[1] = p->subs[index].alertpipebackup[1]; p->subs[index].owner->fds[AST_ALERT_FD] = p->subs[index].alertpipebackup[0]; p->subs[index].owner->fds[AST_TIMING_FD] = p->subs[index].timingfdbackup;}static void update_features(struct feature_pvt *p, int index){ int x; if (p->subs[index].owner) { for (x=0; x<AST_MAX_FDS; x++) { if (index) p->subs[index].owner->fds[x] = -1; else p->subs[index].owner->fds[x] = p->subchan->fds[x]; } if (!index) { /* Copy timings from master channel */ p->subs[index].owner->timingfd = p->subchan->timingfd; p->subs[index].owner->alertpipe[0] = p->subchan->alertpipe[0]; p->subs[index].owner->alertpipe[1] = p->subchan->alertpipe[1]; if (p->subs[index].owner->nativeformats != p->subchan->readformat) { p->subs[index].owner->nativeformats = p->subchan->readformat; if (p->subs[index].owner->readformat) ast_set_read_format(p->subs[index].owner, p->subs[index].owner->readformat); if (p->subs[index].owner->writeformat) ast_set_write_format(p->subs[index].owner, p->subs[index].owner->writeformat); } } else{ restore_channel(p, index); } }}#if 0static void swap_subs(struct feature_pvt *p, int a, int b){ int tinthreeway; struct ast_channel *towner; ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b); towner = p->subs[a].owner; tinthreeway = p->subs[a].inthreeway; p->subs[a].owner = p->subs[b].owner; p->subs[a].inthreeway = p->subs[b].inthreeway; p->subs[b].owner = towner; p->subs[b].inthreeway = tinthreeway; update_features(p,a); update_features(p,b); wakeup_sub(p, a); wakeup_sub(p, b);}#endifstatic int features_answer(struct ast_channel *ast){ struct feature_pvt *p = ast->tech_pvt; int res = -1; int x; ast_mutex_lock(&p->lock); x = indexof(p, ast, 0); if (!x && p->subchan) res = ast_answer(p->subchan); ast_mutex_unlock(&p->lock); return res;}static struct ast_frame *features_read(struct ast_channel *ast){ struct feature_pvt *p = ast->tech_pvt; struct ast_frame *f; int x; f = &ast_null_frame; ast_mutex_lock(&p->lock); x = indexof(p, ast, 0); if (!x && p->subchan) { update_features(p, x); f = ast_read(p->subchan); } ast_mutex_unlock(&p->lock); return f;}static int features_write(struct ast_channel *ast, struct ast_frame *f){ struct feature_pvt *p = ast->tech_pvt; int res = -1; int x; ast_mutex_lock(&p->lock); x = indexof(p, ast, 0); if (!x && p->subchan) res = ast_write(p->subchan, f); ast_mutex_unlock(&p->lock); return res;}static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan){ struct feature_pvt *p = newchan->tech_pvt; int x; ast_mutex_lock(&p->lock); if (p->owner == oldchan) p->owner = newchan; for (x = 0; x < 3; x++) { if (p->subs[x].owner == oldchan) p->subs[x].owner = newchan; } ast_mutex_unlock(&p->lock); return 0;}static int features_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -