app应用过程与reload

转自http://hi.baidu.com/ln5336993/blog/item/74f3be25b6dce622d50742d0.html

 

编写一个app模块:对照app_ivrdemo.c和其它的一些共同特征,如下:
#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision: 40722 $")

#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <limits.h>

#include "asterisk/lock.h"
#include "asterisk/cli.h"
#include "asterisk/pbx.h"
#include "asterisk/channel.h"
#include "asterisk/options.h"
#include "asterisk/logger.h"
#include "asterisk/file.h"
#include "asterisk/callerid.h"
#include "asterisk/cdr.h"
#include "asterisk/config.h"
#include "asterisk/term.h"
#include "asterisk/manager.h"
#include "asterisk/ast_expr.h"
#include "asterisk/linkedlists.h"
#include "asterisk/say.h"
#include "asterisk/utils.h"
#include "asterisk/causes.h"
#include "asterisk/musiconhold.h"
#include "asterisk/app.h"
#include "asterisk/devicestate.h"
#include "asterisk/stringfields.h"
#include "asterisk/module.h"


static char *app = "searchscore";
static char *synopsis = " data test ";
static char *descrip = " test";

static char testfor[24] = "";

#define MAXLONG 10

static void load_config(void);

static int data_test(struct ast_channel *chan, void *data)
{
    ast_verbose("test ok/n");
//    load_config();       
    return 0;
}


static void load_config(void)
{
    struct ast_config *cfg = NULL;
    char *cat = NULL;
    struct ast_variable *var = NULL;

    if (!(cfg = ast_config_load("call_test.conf"))) {
        ast_log(LOG_ERROR, "Configuration file amd.conf missing./n");
        return;
    }

    cat = ast_category_browse(cfg, NULL);

    while (cat)
    {
        if (!strcasecmp(cat, "general") )
        {
            var = ast_variable_browse(cfg, cat);
            while (var)
            {
                if (!strcasecmp(var->name, "maxlong"))
                {
                    strncpy(testfor, var->value, strlen(var->value));
                    ast_verbose("_________%s_______/n", testfor);
                }
                else if(!strcasecmp(var->name, "cluetime"))
                {
                    strncpy(testfor, var->value, strlen(var->value));
                }
                else if(!strcasecmp(var->name, "concallid"))
                {
                    strncpy(testfor, var->value, strlen(var->value));
                }
                else
                {
                    ast_log(LOG_WARNING, " CatUnknown keyword/n");
                }
                var = var->next;
            }
        }
        cat = ast_category_browse(cfg, cat);
    }

    ast_config_destroy(cfg);
   
    return;
}


static int unload_module(void)
{
    int res;
   
    res = ast_unregister_application(app);

    ast_module_user_hangup_all();
   
    return res;
}

static int load_module(void)
{
    return ast_register_application(app, data_test, synopsis, descrip);
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "IVR Application");

如何来使用一个app:
<1>直接使用系统本身的makefile来编译
    直接将些文件放到文件的apps/目录下,make即可,要apps/目录下会产生.so文件,将.so文件放到/usr/lib/asterisk/modules/下(默认情况下对新加的app文件会编译, 可以在/etc/asterisk/modules.conf中配置是否加载些.so )
<2>使用这个Makefile来做(下面)
这个应用在系统内加载的名字如:app = "searchscore";
而我们的app名为app_searchscore.c
1.先更改我们的makefile文件
    a.将MODS=?改为我们要生成的.so文件的名字,如MODS=app_searchscore.so
    b.在endif后面将app_cbmysql.o: app_cbmysql.c改为我们要生成的.o文件如app_searchscore.o:app_searchscore.c,在这句下面一样来替换我们app的名字:$(CC) -pipe -I/usr/include/mysql -L/usr/lib/mysql $(CFLAGS) -c -o app_cbmysql.o app_cbmysql.c,将app_cbmysql.o app_cbmysql.c改为app_searchscore.o app_searchscore.c
    c.同样地我们将app_cbmysql.so: app_cbmysql.o改为app_searchscore.so:app_searchscore.o

2.我们还需要在自己的app应用内加一个宏定义
    #define AST_MODULE "app的名字",在此我们就添加#define AST_MODULE "searchscore"
3.编译这个app应用,产生app_serchscore.so的文件.
把这个app和makefile文件移到我们想放的地方
如:放在/home下,然后在此目录下输入命令:make即可
4.将生成的.so文件移到/usr/lib/asterisk/modules/下
5.编写拨号规则
在/etc/asterisk/extensions.conf内写,如:
exten => 88,1,searchscore
exten => 88,2,Hangup

解释:exten =>是固定的,88是我们执行searchscore时要拨打的号码(当然我们可以把它改成任意我们想要拨打的号码如:314等等),1是优先级
6.启动asterisk,输入命令asterisk -vvvvvvc,拨打号码88,就会执行我们想要执行的函数了(searchscore)

<3>reload应用
这里我们运用reload来对call_test.conf配置文件(放在/etc/asterisk/底下)重新加载,reload只能对全此文件内的全局变量才能起到作用,当启动asterisk后,修改配置文件之后我们可以直接reload就相当于刷新了,而不需要停掉asterisk再make再启动,当然后要是修改了.so文件,就不行了,它只针对配置文件。

makefile:
## Asterisk -- A telephony toolkit for Linux.
## Makefile for CDR backends (dynamically loaded)
## Copyright (C) 1999, Mark Spencer
## Mark Spencer <markster@linux-support.net>
## This program is free software, distributed under the terms of
# the GNU General Public License
#

.EXPORT_ALL_VARIABLES:

MODS=app_cbmysql.so

CFLAGS+=-fPIC
CFLAGS+=-I/usr/src/asterisk
CFLAGS+=-D_GNU_SOURCE

INSTALL=install
INSTALL_PREFIX=
ASTLIBDIR=$(INSTALL_PREFIX)/usr/lib/asterisk
MODULES_DIR=$(ASTLIBDIR)/modules

#
# MySQL stuff... Autoconf anyone??
#
CFLAGS+=$(shell if [ -d /usr/local/mysql/include ]; then echo "-I/usr/local/mysql/include"; fi)
CFLAGS+=$(shell if [ -d /usr/include/mysql ]; then echo "-I/usr/include/mysql"; fi)
CFLAGS+=$(shell if [ -d /usr/local/include/mysql ]; then echo "-I/usr/local/include/mysql"; fi)
CFLAGS+=$(shell if [ -d /opt/mysql/include/mysql ]; then echo "-I/opt/mysql/include/mysql"; fi)
MLFLAGS=
MLFLAGS+=$(shell if [ -d /usr/lib/mysql ]; then echo "-L/usr/lib/mysql"; fi)
MLFLAGS+=$(shell if [ -d /usr/lib64/mysql ]; then echo "-L/usr/lib64/mysql"; fi)
MLFLAGS+=$(shell if [ -d /usr/local/mysql/lib ]; then echo "-L/usr/local/mysql/lib"; fi)
MLFLAGS+=$(shell if [ -d /usr/local/lib/mysql ]; then echo "-L/usr/local/lib/mysql"; fi)
MLFLAGS+=$(shell if [ -d /opt/mysql/lib/mysql ]; then echo "-L/opt/mysql/lib/mysql"; fi)

OSARCH=$(shell uname -s)

ifeq (${OSARCH},Darwin)
SOLINK=-dynamic -bundle -undefined suppress -force_flat_namespace
else
SOLINK=-shared -Xlinker -x
endif
ifeq (${OSARCH},SunOS)
SOLINK=-shared -fpic -L$(CROSS_COMPILE_TARGET)/usr/local/ssl/lib
endif

all: $(MODS)

install: all
    for x in $(MODS); do $(INSTALL) -m 755 $$x $(DESTDIR)$(MODULES_DIR) ; done
    mkdir -p /var/lib/asterisk/sounds/conf-recordings

clean:
    rm -f *.so *.o .depend

%.so : %.o
    $(CC) $(SOLINK) -o $@ $<

ifneq ($(wildcard .depend),)
include .depend
endif


app_cbmysql.o: app_cbmysql.c
    $(CC) -pipe -I/usr/include/mysql -L/usr/lib/mysql $(CFLAGS) -c -o app_cbmysql.o app_cbmysql.c

app_cbmysql.so: app_cbmysql.o
    $(CC) -shared -Xlinker -x -o $@ $< -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient

depend: .depend

.depend:
    ./mkdep $(CFLAGS) `ls *.c`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值