pval.c should not be in svn, in the utils dir
[asterisk/asterisk.git] / channels / chan_nbs.c
old mode 100755 (executable)
new mode 100644 (file)
index 3c6583f..d2718b3
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2005, Digium, Inc.
+ * Copyright (C) 1999 - 2006, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  *
 
 /*! \file
  *
- * \brief Generic Linux Telephony Interface driver
+ * \brief Network broadcast sound support channel driver
  * 
+ * \author Mark Spencer <markster@digium.com>
+ *
+ * \ingroup channel_drivers
  */
 
+/*** MODULEINFO
+       <depend>nbs</depend>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <nbs.h>
 
-#include "asterisk.h"
-
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include "asterisk/lock.h"
 #include "asterisk/channel.h"
 #include "asterisk/config.h"
@@ -45,19 +52,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/pbx.h"
 #include "asterisk/options.h"
+#include "asterisk/utils.h"
 
-static const char desc[] = "Network Broadcast Sound Support";
-static const char type[] = "NBS";
 static const char tdesc[] = "Network Broadcast Sound Driver";
 
-static int usecnt =0;
-
 /* Only linear is allowed */
 static int prefformat = AST_FORMAT_SLINEAR;
 
-AST_MUTEX_DEFINE_STATIC(usecnt_lock);
-
 static char context[AST_MAX_EXTENSION] = "default";
+static char type[] = "NBS";
 
 /* NBS creates private structures on demand */
    
@@ -67,6 +70,7 @@ struct nbs_pvt {
        char app[16];                                   /* Our app */
        char stream[80];                                /* Our stream */
        struct ast_frame fr;                    /* "null" frame */
+       struct ast_module_user *u;              /*! for holding a reference to this module */
 };
 
 static struct ast_channel *nbs_request(const char *type, int format, void *data, int *cause);
@@ -98,8 +102,7 @@ static int nbs_call(struct ast_channel *ast, char *dest, int timeout)
        }
        /* When we call, it just works, really, there's no destination...  Just
           ring the phone and wait for someone to answer */
-       if (option_debug)
-               ast_log(LOG_DEBUG, "Calling %s on %s\n", dest, ast->name);
+       ast_debug(1, "Calling %s on %s\n", dest, ast->name);
 
        /* If we can't connect, return congestion */
        if (nbs_connect(p->nbs)) {
@@ -117,25 +120,26 @@ static void nbs_destroy(struct nbs_pvt *p)
 {
        if (p->nbs)
                nbs_delstream(p->nbs);
-       free(p);
+       ast_module_user_remove(p->u);
+       ast_free(p);
 }
 
 static struct nbs_pvt *nbs_alloc(void *data)
 {
        struct nbs_pvt *p;
        int flags = 0;
-       char stream[256] = "";
+       char stream[256];
        char *opts;
-       strncpy(stream, data, sizeof(stream) - 1);
+
+       ast_copy_string(stream, data, sizeof(stream));
        if ((opts = strchr(stream, ':'))) {
                *opts = '\0';
                opts++;
        } else
                opts = "";
-       p = malloc(sizeof(struct nbs_pvt));
+       p = ast_calloc(1, sizeof(*p));
        if (p) {
-               memset(p, 0, sizeof(struct nbs_pvt));
-               if (strlen(opts)) {
+               if (!ast_strlen_zero(opts)) {
                        if (strchr(opts, 'm'))
                                flags |= NBS_FLAG_MUTE;
                        if (strchr(opts, 'o'))
@@ -147,11 +151,11 @@ static struct nbs_pvt *nbs_alloc(void *data)
                } else
                        flags = NBS_FLAG_OVERSPEAK;
                
-               strncpy(p->stream, stream, sizeof(p->stream) - 1);
+               ast_copy_string(p->stream, stream, sizeof(p->stream));
                p->nbs = nbs_newstream("asterisk", stream, flags);
                if (!p->nbs) {
                        ast_log(LOG_WARNING, "Unable to allocate new NBS stream '%s' with flags %d\n", stream, flags);
-                       free(p);
+                       ast_free(p);
                        p = NULL;
                } else {
                        /* Set for 8000 hz mono, 640 samples */
@@ -168,8 +172,7 @@ static int nbs_hangup(struct ast_channel *ast)
 {
        struct nbs_pvt *p;
        p = ast->tech_pvt;
-       if (option_debug)
-               ast_log(LOG_DEBUG, "nbs_hangup(%s)\n", ast->name);
+       ast_debug(1, "nbs_hangup(%s)\n", ast->name);
        if (!ast->tech_pvt) {
                ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
                return 0;
@@ -195,7 +198,7 @@ static struct ast_frame  *nbs_xread(struct ast_channel *ast)
        p->fr.delivery.tv_sec = 0;
        p->fr.delivery.tv_usec = 0;
 
-       ast_log(LOG_DEBUG, "Returning null frame on %s\n", ast->name);
+       ast_debug(1, "Returning null frame on %s\n", ast->name);
 
        return &p->fr;
 }
@@ -226,29 +229,23 @@ static int nbs_xwrite(struct ast_channel *ast, struct ast_frame *frame)
 static struct ast_channel *nbs_new(struct nbs_pvt *i, int state)
 {
        struct ast_channel *tmp;
-       tmp = ast_channel_alloc(1);
+       tmp = ast_channel_alloc(1, state, 0, 0, "", "s", context, 0, "NBS/%s", i->stream);
        if (tmp) {
                tmp->tech = &nbs_tech;
-               snprintf(tmp->name, sizeof(tmp->name), "NBS/%s", i->stream);
-               tmp->type = type;
-               tmp->fds[0] = nbs_fd(i->nbs);
+               ast_channel_set_fd(tmp, 0, nbs_fd(i->nbs));
                tmp->nativeformats = prefformat;
                tmp->rawreadformat = prefformat;
                tmp->rawwriteformat = prefformat;
                tmp->writeformat = prefformat;
                tmp->readformat = prefformat;
-               ast_setstate(tmp, state);
                if (state == AST_STATE_RING)
                        tmp->rings = 1;
                tmp->tech_pvt = i;
-               strncpy(tmp->context, context, sizeof(tmp->context)-1);
-               strncpy(tmp->exten, "s",  sizeof(tmp->exten) - 1);
-               tmp->language[0] = '\0';
+               ast_copy_string(tmp->context, context, sizeof(tmp->context));
+               ast_copy_string(tmp->exten, "s",  sizeof(tmp->exten));
+               ast_string_field_set(tmp, language, "");
                i->owner = tmp;
-               ast_mutex_lock(&usecnt_lock);
-               usecnt++;
-               ast_mutex_unlock(&usecnt_lock);
-               ast_update_use_count();
+               i->u = ast_module_user_add(tmp);
                if (state != AST_STATE_DOWN) {
                        if (ast_pbx_start(tmp)) {
                                ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
@@ -282,40 +279,21 @@ static struct ast_channel *nbs_request(const char *type, int format, void *data,
        return tmp;
 }
 
-static int __unload_module(void)
+static int unload_module(void)
 {
        /* First, take us out of the channel loop */
        ast_channel_unregister(&nbs_tech);
        return 0;
 }
 
-int unload_module(void)
-{
-       return __unload_module();
-}
-
-int load_module()
+static int load_module(void)
 {
        /* Make sure we can register our channel type */
        if (ast_channel_register(&nbs_tech)) {
                ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
-               __unload_module();
                return -1;
        }
        return 0;
 }
 
-int usecount()
-{
-       return usecnt;
-}
-
-char *description()
-{
-       return (char *) desc;
-}
-
-char *key()
-{
-       return ASTERISK_GPL_KEY;
-}
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Network Broadcast Sound Support");