Wed Oct 28 13:33:10 2009

Asterisk developer's documentation


misdn_config.c File Reference

chan_misdn configuration management More...

#include "asterisk.h"
#include "chan_misdn_config.h"
#include "asterisk/config.h"
#include "asterisk/channel.h"
#include "asterisk/lock.h"
#include "asterisk/pbx.h"
#include "asterisk/strings.h"
#include "asterisk/utils.h"

Include dependency graph for misdn_config.c:

Go to the source code of this file.

Data Structures

union  misdn_cfg_pt
struct  misdn_cfg_spec
struct  msn_list

Defines

#define CLI_ERROR(name, value, section)
#define GEN_CFG   1
#define NO_DEFAULT   "<>"
#define NONE   0
#define NUM_GEN_ELEMENTS   (sizeof(gen_spec) / sizeof(struct misdn_cfg_spec))
#define NUM_PORT_ELEMENTS   (sizeof(port_spec) / sizeof(struct misdn_cfg_spec))
#define PORT_CFG   2

Enumerations

enum  misdn_cfg_type {
  MISDN_CTYPE_STR, MISDN_CTYPE_INT, MISDN_CTYPE_BOOL, MISDN_CTYPE_BOOLINT,
  MISDN_CTYPE_MSNLIST, MISDN_CTYPE_ASTGROUP
}

Functions

static void _build_general_config (struct ast_variable *v)
static void _build_port_config (struct ast_variable *v, char *cat)
static int _enum_array_map (void)
static void _fill_defaults (void)
static void _free_general_cfg (void)
static void _free_msn_list (struct msn_list *iter)
static void _free_port_cfg (void)
static int _parse (union misdn_cfg_pt *dest, const char *value, enum misdn_cfg_type type, int boolint_def)
static int get_cfg_position (const char *name, int type)
void misdn_cfg_destroy (void)
void misdn_cfg_get (int port, enum misdn_cfg_elements elem, void *buf, int bufsize)
void misdn_cfg_get_config_string (int port, enum misdn_cfg_elements elem, char *buf, int bufsize)
void misdn_cfg_get_desc (enum misdn_cfg_elements elem, void *buf, int bufsize, void *buf_default, int bufsize_default)
enum misdn_cfg_elements misdn_cfg_get_elem (const char *name)
void misdn_cfg_get_name (enum misdn_cfg_elements elem, void *buf, int bufsize)
int misdn_cfg_get_next_port (int port)
int misdn_cfg_get_next_port_spin (int port)
void misdn_cfg_get_ports_string (char *ports)
 Generate a comma separated list of all active ports.
int misdn_cfg_init (int this_max_ports, int reload)
int misdn_cfg_is_group_method (char *group, enum misdn_cfg_method meth)
int misdn_cfg_is_msn_valid (int port, char *msn)
int misdn_cfg_is_port_valid (int port)
static void misdn_cfg_lock (void)
void misdn_cfg_reload (void)
static void misdn_cfg_unlock (void)
void misdn_cfg_update_ptp (void)
struct ast_jb_confmisdn_get_global_jbconf ()

Variables

static ast_mutex_t config_mutex
static struct ast_jb_conf default_jbconf
static struct misdn_cfg_spec gen_spec []
static union misdn_cfg_ptgeneral_cfg
static struct ast_jb_conf global_jbconf
static int * map
static int max_ports
static union misdn_cfg_pt ** port_cfg
static struct misdn_cfg_spec port_spec []
static const char ports_description []
static int * ptp


Detailed Description

chan_misdn configuration management

Author:
Christian Richter <crich@beronet.com>

Definition in file misdn_config.c.


Define Documentation

#define CLI_ERROR ( name,
value,
section   ) 

Value:

({ \
   ast_log(LOG_WARNING, "misdn.conf: \"%s=%s\" (section: %s) invalid or out of range. " \
      "Please edit your misdn.conf and then do a \"misdn reload\".\n", name, value, section); \
})

Definition at line 425 of file misdn_config.c.

Referenced by _build_general_config(), and _build_port_config().

#define GEN_CFG   1

Definition at line 45 of file misdn_config.c.

Referenced by _build_general_config(), get_cfg_position(), and misdn_cfg_get_elem().

#define NO_DEFAULT   "<>"

Definition at line 42 of file misdn_config.c.

Referenced by _fill_defaults(), and misdn_cfg_get_desc().

#define NONE   0

#define NUM_GEN_ELEMENTS   (sizeof(gen_spec) / sizeof(struct misdn_cfg_spec))

#define NUM_PORT_ELEMENTS   (sizeof(port_spec) / sizeof(struct misdn_cfg_spec))

#define PORT_CFG   2

Definition at line 46 of file misdn_config.c.

Referenced by _build_port_config(), get_cfg_position(), and misdn_cfg_get_elem().


Enumeration Type Documentation

Enumerator:
MISDN_CTYPE_STR 
MISDN_CTYPE_INT 
MISDN_CTYPE_BOOL 
MISDN_CTYPE_BOOLINT 
MISDN_CTYPE_MSNLIST 
MISDN_CTYPE_ASTGROUP 

Definition at line 61 of file misdn_config.c.


Function Documentation

static void _build_general_config ( struct ast_variable v  )  [static]

Definition at line 979 of file misdn_config.c.

References _parse(), ast_jb_read_conf(), misdn_cfg_spec::boolint_def, CLI_ERROR, GEN_CFG, get_cfg_position(), global_jbconf, ast_variable::name, ast_variable::next, misdn_cfg_spec::type, and ast_variable::value.

Referenced by misdn_cfg_init().

00980 {
00981    int pos;
00982 
00983    for (; v; v = v->next) {
00984       if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
00985          continue;
00986       if (((pos = get_cfg_position(v->name, GEN_CFG)) < 0) ||
00987          (_parse(&general_cfg[pos], v->value, gen_spec[pos].type, gen_spec[pos].boolint_def) < 0))
00988          CLI_ERROR(v->name, v->value, "general");
00989    }
00990 }

static void _build_port_config ( struct ast_variable v,
char *  cat 
) [static]

Definition at line 992 of file misdn_config.c.

References _parse(), ast_strdupa, misdn_cfg_spec::boolint_def, BUFFERSIZE, CLI_ERROR, get_cfg_position(), ast_variable::name, ast_variable::next, PORT_CFG, strsep(), misdn_cfg_spec::type, type, and ast_variable::value.

Referenced by misdn_cfg_init().

00993 {
00994    int pos, i;
00995    union misdn_cfg_pt cfg_tmp[NUM_PORT_ELEMENTS];
00996    int cfg_for_ports[max_ports + 1];
00997 
00998    if (!v || !cat)
00999       return;
01000 
01001    memset(cfg_tmp, 0, sizeof(cfg_tmp));
01002    memset(cfg_for_ports, 0, sizeof(cfg_for_ports));
01003 
01004    if (!strcasecmp(cat, "default")) {
01005       cfg_for_ports[0] = 1;
01006    }
01007 
01008    if (((pos = get_cfg_position("name", PORT_CFG)) < 0) ||
01009       (_parse(&cfg_tmp[pos], cat, port_spec[pos].type, port_spec[pos].boolint_def) < 0)) {
01010       CLI_ERROR(v->name, v->value, cat);
01011       return;
01012    }
01013 
01014    for (; v; v = v->next) {
01015       if (!strcasecmp(v->name, "ports")) {
01016          char *token, *tmp = ast_strdupa(v->value);
01017          char ptpbuf[BUFFERSIZE] = "";
01018          int start, end;
01019          for (token = strsep(&tmp, ","); token; token = strsep(&tmp, ","), *ptpbuf = 0) {
01020             if (!*token)
01021                continue;
01022             if (sscanf(token, "%30d-%30d%511s", &start, &end, ptpbuf) >= 2) {
01023                for (; start <= end; start++) {
01024                   if (start <= max_ports && start > 0) {
01025                      cfg_for_ports[start] = 1;
01026                      ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
01027                   } else
01028                      CLI_ERROR(v->name, v->value, cat);
01029                }
01030             } else {
01031                if (sscanf(token, "%30d%511s", &start, ptpbuf)) {
01032                   if (start <= max_ports && start > 0) {
01033                      cfg_for_ports[start] = 1;
01034                      ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
01035                   } else
01036                      CLI_ERROR(v->name, v->value, cat);
01037                } else
01038                   CLI_ERROR(v->name, v->value, cat);
01039             }
01040          }
01041       } else {
01042          if (((pos = get_cfg_position(v->name, PORT_CFG)) < 0) ||
01043             (_parse(&cfg_tmp[pos], v->value, port_spec[pos].type, port_spec[pos].boolint_def) < 0))
01044             CLI_ERROR(v->name, v->value, cat);
01045       }
01046    }
01047 
01048    for (i = 0; i < (max_ports + 1); ++i) {
01049       if (i > 0 && cfg_for_ports[0]) {
01050          /* default category, will populate the port_cfg with additional port
01051          categories in subsequent calls to this function */
01052          memset(cfg_tmp, 0, sizeof(cfg_tmp));
01053       }
01054       if (cfg_for_ports[i]) {
01055          memcpy(port_cfg[i], cfg_tmp, sizeof(cfg_tmp));
01056       }
01057    }
01058 }

static int _enum_array_map ( void   )  [static]

Definition at line 430 of file misdn_config.c.

References ast_log(), LOG_WARNING, MISDN_CFG_FIRST, MISDN_CFG_LAST, MISDN_CFG_PTP, MISDN_GEN_FIRST, MISDN_GEN_LAST, NUM_GEN_ELEMENTS, and NUM_PORT_ELEMENTS.

Referenced by misdn_cfg_init().

00431 {
00432    int i, j, ok;
00433 
00434    for (i = MISDN_CFG_FIRST + 1; i < MISDN_CFG_LAST; ++i) {
00435       if (i == MISDN_CFG_PTP)
00436          continue;
00437       ok = 0;
00438       for (j = 0; j < NUM_PORT_ELEMENTS; ++j) {
00439          if (port_spec[j].elem == i) {
00440             map[i] = j;
00441             ok = 1;
00442             break;
00443          }
00444       }
00445       if (!ok) {
00446          ast_log(LOG_WARNING, "Enum element %d in misdn_cfg_elements (port section) has no corresponding element in the config struct!\n", i);
00447          return -1;
00448       }
00449    }
00450    for (i = MISDN_GEN_FIRST + 1; i < MISDN_GEN_LAST; ++i) {
00451       ok = 0;
00452       for (j = 0; j < NUM_GEN_ELEMENTS; ++j) {
00453          if (gen_spec[j].elem == i) {
00454             map[i] = j;
00455             ok = 1;
00456             break;
00457          }
00458       }
00459       if (!ok) {
00460          ast_log(LOG_WARNING, "Enum element %d in misdn_cfg_elements (general section) has no corresponding element in the config struct!\n", i);
00461          return -1;
00462       }
00463    }
00464    return 0;
00465 }

static void _fill_defaults ( void   )  [static]

Definition at line 1115 of file misdn_config.c.

References _parse(), misdn_cfg_pt::any, NO_DEFAULT, NUM_GEN_ELEMENTS, NUM_PORT_ELEMENTS, and type.

Referenced by misdn_cfg_init().

01116 {
01117    int i;
01118 
01119    for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
01120       if (!port_cfg[0][i].any && strcasecmp(port_spec[i].def, NO_DEFAULT))
01121          _parse(&(port_cfg[0][i]), (char *)port_spec[i].def, port_spec[i].type, port_spec[i].boolint_def);
01122    }
01123    for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
01124       if (!general_cfg[i].any && strcasecmp(gen_spec[i].def, NO_DEFAULT))
01125          _parse(&(general_cfg[i]), (char *)gen_spec[i].def, gen_spec[i].type, gen_spec[i].boolint_def);
01126    }
01127 }

static void _free_general_cfg ( void   )  [static]

Definition at line 540 of file misdn_config.c.

References misdn_cfg_pt::any, ast_free, and NUM_GEN_ELEMENTS.

Referenced by misdn_cfg_destroy(), and misdn_cfg_init().

00541 {
00542    int i;
00543 
00544    for (i = 0; i < NUM_GEN_ELEMENTS; i++)
00545       if (general_cfg[i].any)
00546          ast_free(general_cfg[i].any);
00547 }

static void _free_msn_list ( struct msn_list iter  )  [static]

Definition at line 498 of file misdn_config.c.

References ast_free, msn_list::msn, and msn_list::next.

Referenced by _free_port_cfg().

00499 {
00500    if (iter->next)
00501       _free_msn_list(iter->next);
00502    if (iter->msn)
00503       ast_free(iter->msn);
00504    ast_free(iter);
00505 }

static void _free_port_cfg ( void   )  [static]

Definition at line 507 of file misdn_config.c.

References _free_msn_list(), misdn_cfg_pt::any, ast_free, MISDN_CFG_GROUPNAME, MISDN_CTYPE_MSNLIST, misdn_cfg_pt::ml, NUM_PORT_ELEMENTS, str, and type.

Referenced by misdn_cfg_destroy(), and misdn_cfg_init().

00508 {
00509    int i, j;
00510    int gn = map[MISDN_CFG_GROUPNAME];
00511    union misdn_cfg_pt* free_list[max_ports + 2];
00512 
00513    memset(free_list, 0, sizeof(free_list));
00514    free_list[0] = port_cfg[0];
00515    for (i = 1; i <= max_ports; ++i) {
00516       if (port_cfg[i][gn].str) {
00517          /* we always have a groupname in the non-default case, so this is fine */
00518          for (j = 1; j <= max_ports; ++j) {
00519             if (free_list[j] && free_list[j][gn].str == port_cfg[i][gn].str)
00520                break;
00521             else if (!free_list[j]) {
00522                free_list[j] = port_cfg[i];
00523                break;
00524             }
00525          }
00526       }
00527    }
00528    for (j = 0; free_list[j]; ++j) {
00529       for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
00530          if (free_list[j][i].any) {
00531             if (port_spec[i].type == MISDN_CTYPE_MSNLIST)
00532                _free_msn_list(free_list[j][i].ml);
00533             else
00534                ast_free(free_list[j][i].any);
00535          }
00536       }
00537    }
00538 }

static int _parse ( union misdn_cfg_pt dest,
const char *  value,
enum misdn_cfg_type  type,
int  boolint_def 
) [static]

Definition at line 902 of file misdn_config.c.

References ast_calloc, ast_free, ast_get_group(), ast_malloc, ast_strdupa, ast_true(), misdn_cfg_pt::grp, len(), MISDN_CTYPE_ASTGROUP, MISDN_CTYPE_BOOL, MISDN_CTYPE_BOOLINT, MISDN_CTYPE_INT, MISDN_CTYPE_MSNLIST, MISDN_CTYPE_STR, misdn_cfg_pt::ml, msn_list::msn, msn_list::next, misdn_cfg_pt::num, misdn_cfg_pt::str, and strsep().

Referenced by _build_general_config(), _build_port_config(), and _fill_defaults().

00903 {
00904    int re = 0;
00905    int len, tmp;
00906    char *valtmp;
00907    char *tmp2 = ast_strdupa(value);
00908 
00909    switch (type) {
00910    case MISDN_CTYPE_STR:
00911       if (dest->str) {
00912          ast_free(dest->str);
00913       }
00914       if ((len = strlen(value))) {
00915          dest->str = ast_malloc((len + 1) * sizeof(char));
00916          strncpy(dest->str, value, len);
00917          dest->str[len] = 0;
00918       } else {
00919          dest->str = ast_malloc(sizeof(char));
00920          dest->str[0] = 0;
00921       }
00922       break;
00923    case MISDN_CTYPE_INT:
00924    {
00925       int res;
00926 
00927       if (strchr(value,'x')) {
00928          res = sscanf(value, "%30x", &tmp);
00929       } else {
00930          res = sscanf(value, "%30d", &tmp);
00931       }
00932       if (res) {
00933          if (!dest->num) {
00934             dest->num = ast_malloc(sizeof(int));
00935          }
00936          memcpy(dest->num, &tmp, sizeof(int));
00937       } else
00938          re = -1;
00939    }
00940       break;
00941    case MISDN_CTYPE_BOOL:
00942       if (!dest->num) {
00943          dest->num = ast_malloc(sizeof(int));
00944       }
00945       *(dest->num) = (ast_true(value) ? 1 : 0);
00946       break;
00947    case MISDN_CTYPE_BOOLINT:
00948       if (!dest->num) {
00949          dest->num = ast_malloc(sizeof(int));
00950       }
00951       if (sscanf(value, "%30d", &tmp)) {
00952          memcpy(dest->num, &tmp, sizeof(int));
00953       } else {
00954          *(dest->num) = (ast_true(value) ? boolint_def : 0);
00955       }
00956       break;
00957    case MISDN_CTYPE_MSNLIST:
00958       for (valtmp = strsep(&tmp2, ","); valtmp; valtmp = strsep(&tmp2, ",")) {
00959          if ((len = strlen(valtmp))) {
00960             struct msn_list *ml = ast_malloc(sizeof(*ml));
00961             ml->msn = ast_calloc(len+1, sizeof(char));
00962             strncpy(ml->msn, valtmp, len);
00963             ml->next = dest->ml;
00964             dest->ml = ml;
00965          }
00966       }
00967       break;
00968    case MISDN_CTYPE_ASTGROUP:
00969       if (!dest->grp) {
00970          dest->grp = ast_malloc(sizeof(ast_group_t));
00971       }
00972       *(dest->grp) = ast_get_group(value);
00973       break;
00974    }
00975 
00976    return re;
00977 }

static int get_cfg_position ( const char *  name,
int  type 
) [static]

Definition at line 467 of file misdn_config.c.

References GEN_CFG, NUM_GEN_ELEMENTS, NUM_PORT_ELEMENTS, and PORT_CFG.

Referenced by _build_general_config(), _build_port_config(), and misdn_cfg_get_elem().

00468 {
00469    int i;
00470 
00471    switch (type) {
00472    case PORT_CFG:
00473       for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
00474          if (!strcasecmp(name, port_spec[i].name))
00475             return i;
00476       }
00477       break;
00478    case GEN_CFG:
00479       for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
00480          if (!strcasecmp(name, gen_spec[i].name))
00481             return i;
00482       }
00483    }
00484 
00485    return -1;
00486 }

void misdn_cfg_destroy ( void   ) 

Definition at line 1134 of file misdn_config.c.

References _free_general_cfg(), _free_port_cfg(), ast_free, ast_mutex_destroy(), misdn_cfg_lock(), and misdn_cfg_unlock().

Referenced by unload_module().

01135 {
01136    misdn_cfg_lock();
01137 
01138    _free_port_cfg();
01139    _free_general_cfg();
01140 
01141    ast_free(port_cfg);
01142    ast_free(general_cfg);
01143    ast_free(ptp);
01144    ast_free(map);
01145 
01146    misdn_cfg_unlock();
01147    ast_mutex_destroy(&config_mutex);
01148 }

void misdn_cfg_get ( int  port,
enum misdn_cfg_elements  elem,
void *  buf,
int  bufsize 
)

Definition at line 549 of file misdn_config.c.

References misdn_cfg_pt::any, ast_copy_string(), ast_log(), LOG_WARNING, misdn_cfg_is_port_valid(), MISDN_CFG_LAST, misdn_cfg_lock(), MISDN_CFG_PTP, misdn_cfg_unlock(), MISDN_CTYPE_STR, S_OR, str, and type.

Referenced by add_in_calls(), add_out_calls(), cb_events(), dialtone_indicate(), load_module(), misdn_add_number_prefix(), misdn_bridge(), misdn_call(), misdn_cfg_update_ptp(), misdn_check_l2l1(), misdn_get_connected_line(), misdn_new(), misdn_request(), misdn_set_opt_exec(), process_ast_dsp(), read_config(), reload_config(), update_config(), and update_ec_config().

00550 {
00551    int place;
00552 
00553    if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
00554       memset(buf, 0, bufsize);
00555       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Port number %d is not valid.\n", port);
00556       return;
00557    }
00558 
00559    misdn_cfg_lock();
00560    if (elem == MISDN_CFG_PTP) {
00561       if (!memcpy(buf, &ptp[port], (bufsize > ptp[port]) ? sizeof(ptp[port]) : bufsize))
00562          memset(buf, 0, bufsize);
00563    } else {
00564       if ((place = map[elem]) < 0) {
00565          memset(buf, 0, bufsize);
00566          ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Invalid element (%d) requested.\n", elem);
00567       } else {
00568          if (elem < MISDN_CFG_LAST) {
00569             switch (port_spec[place].type) {
00570             case MISDN_CTYPE_STR:
00571                if (port_cfg[port][place].str) {
00572                   ast_copy_string(buf, port_cfg[port][place].str, bufsize);
00573                } else if (port_cfg[0][place].str) {
00574                   ast_copy_string(buf, port_cfg[0][place].str, bufsize);
00575                } else
00576                   memset(buf, 0, bufsize);
00577                break;
00578             default:
00579                if (port_cfg[port][place].any)
00580                   memcpy(buf, port_cfg[port][place].any, bufsize);
00581                else if (port_cfg[0][place].any)
00582                   memcpy(buf, port_cfg[0][place].any, bufsize);
00583                else
00584                   memset(buf, 0, bufsize);
00585             }
00586          } else {
00587             switch (gen_spec[place].type) {
00588             case MISDN_CTYPE_STR:
00589                ast_copy_string(buf, S_OR(general_cfg[place].str, ""), bufsize);
00590                break;
00591             default:
00592                if (general_cfg[place].any)
00593                   memcpy(buf, general_cfg[place].any, bufsize);
00594                else
00595                   memset(buf, 0, bufsize);
00596             }
00597          }
00598       }
00599    }
00600    misdn_cfg_unlock();
00601 }

void misdn_cfg_get_config_string ( int  port,
enum misdn_cfg_elements  elem,
char *  buf,
int  bufsize 
)

Definition at line 774 of file misdn_config.c.

References ast_log(), ast_print_group(), BUFFERSIZE, LOG_WARNING, MISDN_CFG_FIRST, misdn_cfg_is_port_valid(), MISDN_CFG_LAST, misdn_cfg_lock(), MISDN_CFG_PTP, misdn_cfg_unlock(), MISDN_CTYPE_ASTGROUP, MISDN_CTYPE_BOOL, MISDN_CTYPE_BOOLINT, MISDN_CTYPE_INT, MISDN_CTYPE_MSNLIST, MISDN_CTYPE_STR, MISDN_GEN_FIRST, MISDN_GEN_LAST, misdn_cfg_pt::ml, msn_list::msn, name, msn_list::next, num, str, and type.

Referenced by handle_cli_misdn_show_config().

00775 {
00776    int place;
00777    char tempbuf[BUFFERSIZE] = "";
00778    struct msn_list *iter;
00779 
00780    if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
00781       *buf = 0;
00782       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Port number %d is not valid.\n", port);
00783       return;
00784    }
00785 
00786    place = map[elem];
00787 
00788    misdn_cfg_lock();
00789    if (elem == MISDN_CFG_PTP) {
00790       snprintf(buf, bufsize, " -> ptp: %s", ptp[port] ? "yes" : "no");
00791    }
00792    else if (elem > MISDN_CFG_FIRST && elem < MISDN_CFG_LAST) {
00793       switch (port_spec[place].type) {
00794       case MISDN_CTYPE_INT:
00795       case MISDN_CTYPE_BOOLINT:
00796          if (port_cfg[port][place].num)
00797             snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[port][place].num);
00798          else if (port_cfg[0][place].num)
00799             snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[0][place].num);
00800          else
00801             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00802          break;
00803       case MISDN_CTYPE_BOOL:
00804          if (port_cfg[port][place].num)
00805             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[port][place].num ? "yes" : "no");
00806          else if (port_cfg[0][place].num)
00807             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[0][place].num ? "yes" : "no");
00808          else
00809             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00810          break;
00811       case MISDN_CTYPE_ASTGROUP:
00812          if (port_cfg[port][place].grp)
00813             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name,
00814                    ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[port][place].grp));
00815          else if (port_cfg[0][place].grp)
00816             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name,
00817                    ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[0][place].grp));
00818          else
00819             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00820          break;
00821       case MISDN_CTYPE_MSNLIST:
00822          if (port_cfg[port][place].ml)
00823             iter = port_cfg[port][place].ml;
00824          else
00825             iter = port_cfg[0][place].ml;
00826          if (iter) {
00827             for (; iter; iter = iter->next) {
00828                strncat(tempbuf, iter->msn, sizeof(tempbuf) - strlen(tempbuf) - 1);
00829             }
00830             if (strlen(tempbuf) > 1) {
00831                tempbuf[strlen(tempbuf)-2] = 0;
00832             }
00833          }
00834          snprintf(buf, bufsize, " -> msns: %s", *tempbuf ? tempbuf : "none");
00835          break;
00836       case MISDN_CTYPE_STR:
00837          if ( port_cfg[port][place].str) {
00838             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[port][place].str);
00839          } else if (port_cfg[0][place].str) {
00840             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[0][place].str);
00841          } else {
00842             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00843          }
00844          break;
00845       }
00846    } else if (elem > MISDN_GEN_FIRST && elem < MISDN_GEN_LAST) {
00847       switch (gen_spec[place].type) {
00848       case MISDN_CTYPE_INT:
00849       case MISDN_CTYPE_BOOLINT:
00850          if (general_cfg[place].num)
00851             snprintf(buf, bufsize, " -> %s: %d", gen_spec[place].name, *general_cfg[place].num);
00852          else
00853             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00854          break;
00855       case MISDN_CTYPE_BOOL:
00856          if (general_cfg[place].num)
00857             snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, *general_cfg[place].num ? "yes" : "no");
00858          else
00859             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00860          break;
00861       case MISDN_CTYPE_STR:
00862          if ( general_cfg[place].str) {
00863             snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, general_cfg[place].str);
00864          } else {
00865             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00866          }
00867          break;
00868       default:
00869          snprintf(buf, bufsize, " -> type of %s not handled yet", gen_spec[place].name);
00870          break;
00871       }
00872    } else {
00873       *buf = 0;
00874       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Invalid config element (%d) requested.\n", elem);
00875    }
00876    misdn_cfg_unlock();
00877 }

void misdn_cfg_get_desc ( enum misdn_cfg_elements  elem,
void *  buf,
int  bufsize,
void *  buf_default,
int  bufsize_default 
)

Definition at line 650 of file misdn_config.c.

References ast_copy_string(), misdn_cfg_spec::def, desc, MISDN_CFG_FIRST, MISDN_CFG_GROUPNAME, MISDN_CFG_LAST, MISDN_GEN_FIRST, MISDN_GEN_LAST, and NO_DEFAULT.

Referenced by show_config_description().

00651 {
00652    int place = map[elem];
00653    struct misdn_cfg_spec *spec = NULL;
00654 
00655    /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
00656    if (elem == MISDN_CFG_GROUPNAME) {
00657       ast_copy_string(buf, ports_description, bufsize);
00658       if (buf_default && bufsize_default)
00659          memset(buf_default, 0, 1);
00660       return;
00661    }
00662 
00663    if ((elem > MISDN_CFG_FIRST) && (elem < MISDN_CFG_LAST))
00664       spec = (struct misdn_cfg_spec *)port_spec;
00665    else if ((elem > MISDN_GEN_FIRST) && (elem < MISDN_GEN_LAST))
00666       spec = (struct misdn_cfg_spec *)gen_spec;
00667 
00668    if (!spec || !spec[place].desc)
00669       memset(buf, 0, 1);
00670    else {
00671       ast_copy_string(buf, spec[place].desc, bufsize);
00672       if (buf_default && bufsize) {
00673          if (!strcmp(spec[place].def, NO_DEFAULT))
00674             memset(buf_default, 0, 1);
00675          else
00676             ast_copy_string(buf_default, spec[place].def, bufsize_default);
00677       }
00678    }
00679 }

enum misdn_cfg_elements misdn_cfg_get_elem ( const char *  name  ) 

Definition at line 603 of file misdn_config.c.

References misdn_cfg_spec::elem, GEN_CFG, get_cfg_position(), MISDN_CFG_FIRST, MISDN_CFG_GROUPNAME, and PORT_CFG.

Referenced by handle_cli_misdn_show_config().

00604 {
00605    int pos;
00606 
00607    /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
00608    if (!strcmp(name, "ports"))
00609       return MISDN_CFG_GROUPNAME;
00610    if (!strcmp(name, "name"))
00611       return MISDN_CFG_FIRST;
00612 
00613    pos = get_cfg_position(name, PORT_CFG);
00614    if (pos >= 0)
00615       return port_spec[pos].elem;
00616 
00617    pos = get_cfg_position(name, GEN_CFG);
00618    if (pos >= 0)
00619       return gen_spec[pos].elem;
00620 
00621    return MISDN_CFG_FIRST;
00622 }

void misdn_cfg_get_name ( enum misdn_cfg_elements  elem,
void *  buf,
int  bufsize 
)

Definition at line 624 of file misdn_config.c.

References ast_copy_string(), MISDN_CFG_FIRST, MISDN_CFG_GROUPNAME, MISDN_CFG_LAST, MISDN_CFG_PTP, MISDN_GEN_FIRST, MISDN_GEN_LAST, and name.

Referenced by complete_show_config(), and show_config_description().

00625 {
00626    struct misdn_cfg_spec *spec = NULL;
00627    int place = map[elem];
00628 
00629    /* the ptp hack */
00630    if (elem == MISDN_CFG_PTP) {
00631       memset(buf, 0, 1);
00632       return;
00633    }
00634 
00635    /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
00636    if (elem == MISDN_CFG_GROUPNAME) {
00637       if (!snprintf(buf, bufsize, "ports"))
00638          memset(buf, 0, 1);
00639       return;
00640    }
00641 
00642    if ((elem > MISDN_CFG_FIRST) && (elem < MISDN_CFG_LAST))
00643       spec = (struct misdn_cfg_spec *)port_spec;
00644    else if ((elem > MISDN_GEN_FIRST) && (elem < MISDN_GEN_LAST))
00645       spec = (struct misdn_cfg_spec *)gen_spec;
00646 
00647    ast_copy_string(buf, spec ? spec[place].name : "", bufsize);
00648 }

int misdn_cfg_get_next_port ( int  port  ) 

Definition at line 879 of file misdn_config.c.

References MISDN_CFG_GROUPNAME, misdn_cfg_lock(), misdn_cfg_unlock(), and str.

Referenced by complete_show_config(), handle_cli_misdn_show_config(), handle_cli_misdn_show_ports_stats(), handle_cli_misdn_show_stacks(), load_module(), misdn_cfg_get_next_port_spin(), misdn_check_l2l1(), misdn_new(), misdn_request(), and update_name().

00880 {
00881    int p = -1;
00882    int gn = map[MISDN_CFG_GROUPNAME];
00883 
00884    misdn_cfg_lock();
00885    for (port++; port <= max_ports; port++) {
00886       if (port_cfg[port][gn].str) {
00887          p = port;
00888          break;
00889       }
00890    }
00891    misdn_cfg_unlock();
00892 
00893    return p;
00894 }

int misdn_cfg_get_next_port_spin ( int  port  ) 

Definition at line 896 of file misdn_config.c.

References misdn_cfg_get_next_port().

Referenced by misdn_request().

00897 {
00898    int p = misdn_cfg_get_next_port(port);
00899    return (p > 0) ? p : misdn_cfg_get_next_port(0);
00900 }

void misdn_cfg_get_ports_string ( char *  ports  ) 

Generate a comma separated list of all active ports.

Definition at line 748 of file misdn_config.c.

References MISDN_CFG_GROUPNAME, misdn_cfg_lock(), misdn_cfg_unlock(), and str.

Referenced by load_module().

00749 {
00750    char tmp[16];
00751    int l, i;
00752    int gn = map[MISDN_CFG_GROUPNAME];
00753 
00754    *ports = 0;
00755 
00756    misdn_cfg_lock();
00757    for (i = 1; i <= max_ports; i++) {
00758       if (port_cfg[i][gn].str) {
00759          if (ptp[i])
00760             sprintf(tmp, "%dptp,", i);
00761          else
00762             sprintf(tmp, "%d,", i);
00763          strcat(ports, tmp);
00764       }
00765    }
00766    misdn_cfg_unlock();
00767 
00768    if ((l = strlen(ports))) {
00769       /* Strip trailing ',' */
00770       ports[l-1] = 0;
00771    }
00772 }

int misdn_cfg_init ( int  this_max_ports,
int  reload 
)

Definition at line 1150 of file misdn_config.c.

References _build_general_config(), _build_port_config(), _enum_array_map(), _fill_defaults(), _free_general_cfg(), _free_port_cfg(), ast_calloc, ast_category_browse(), ast_config_destroy(), ast_config_load2(), ast_log(), ast_mutex_init(), ast_variable_browse(), config, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, global_jbconf, LOG_WARNING, misdn_cfg_lock(), misdn_cfg_unlock(), MISDN_GEN_LAST, NUM_GEN_ELEMENTS, and NUM_PORT_ELEMENTS.

Referenced by load_module(), and misdn_cfg_reload().

01151 {
01152    char config[] = "misdn.conf";
01153    char *cat, *p;
01154    int i;
01155    struct ast_config *cfg;
01156    struct ast_variable *v;
01157    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
01158 
01159    if (!(cfg = ast_config_load2(config, "chan_misdn", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
01160       ast_log(LOG_WARNING, "missing or invalid file: misdn.conf\n");
01161       return -1;
01162    } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
01163       return 0;
01164 
01165    ast_mutex_init(&config_mutex);
01166 
01167    /* Copy the default jb config over global_jbconf */
01168    memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
01169 
01170    misdn_cfg_lock();
01171 
01172    if (this_max_ports) {
01173       /* this is the first run */
01174       max_ports = this_max_ports;
01175       map = ast_calloc(MISDN_GEN_LAST + 1, sizeof(int));
01176       if (_enum_array_map())
01177          return -1;
01178       p = ast_calloc(1, (max_ports + 1) * sizeof(union misdn_cfg_pt *)
01179                      + (max_ports + 1) * NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt));
01180       port_cfg = (union misdn_cfg_pt **)p;
01181       p += (max_ports + 1) * sizeof(union misdn_cfg_pt *);
01182       for (i = 0; i <= max_ports; ++i) {
01183          port_cfg[i] = (union misdn_cfg_pt *)p;
01184          p += NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt);
01185       }
01186       general_cfg = ast_calloc(1, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
01187       ptp = ast_calloc(max_ports + 1, sizeof(int));
01188    }
01189    else {
01190       /* misdn reload */
01191       _free_port_cfg();
01192       _free_general_cfg();
01193       memset(port_cfg[0], 0, NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt) * (max_ports + 1));
01194       memset(general_cfg, 0, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
01195       memset(ptp, 0, sizeof(int) * (max_ports + 1));
01196    }
01197 
01198    cat = ast_category_browse(cfg, NULL);
01199 
01200    while(cat) {
01201       v = ast_variable_browse(cfg, cat);
01202       if (!strcasecmp(cat, "general")) {
01203          _build_general_config(v);
01204       } else {
01205          _build_port_config(v, cat);
01206       }
01207       cat = ast_category_browse(cfg, cat);
01208    }
01209 
01210    _fill_defaults();
01211 
01212    misdn_cfg_unlock();
01213    ast_config_destroy(cfg);
01214 
01215    return 0;
01216 }

int misdn_cfg_is_group_method ( char *  group,
enum misdn_cfg_method  meth 
)

Definition at line 713 of file misdn_config.c.

References METHOD_ROUND_ROBIN, METHOD_STANDARD, METHOD_STANDARD_DEC, MISDN_CFG_GROUPNAME, misdn_cfg_lock(), MISDN_CFG_METHOD, misdn_cfg_unlock(), str, and misdn_cfg_pt::str.

Referenced by misdn_request().

00714 {
00715    int i, re = 0;
00716    char *method ;
00717 
00718    misdn_cfg_lock();
00719 
00720    method = port_cfg[0][map[MISDN_CFG_METHOD]].str;
00721 
00722    for (i = 1; i <= max_ports; i++) {
00723       if (port_cfg[i] && port_cfg[i][map[MISDN_CFG_GROUPNAME]].str) {
00724          if (!strcasecmp(port_cfg[i][map[MISDN_CFG_GROUPNAME]].str, group))
00725             method = (port_cfg[i][map[MISDN_CFG_METHOD]].str ?
00726                     port_cfg[i][map[MISDN_CFG_METHOD]].str : port_cfg[0][map[MISDN_CFG_METHOD]].str);
00727       }
00728    }
00729 
00730    if (method) {
00731       switch (meth) {
00732       case METHOD_STANDARD:      re = !strcasecmp(method, "standard");
00733                            break;
00734       case METHOD_ROUND_ROBIN:   re = !strcasecmp(method, "round_robin");
00735                            break;
00736       case METHOD_STANDARD_DEC:  re = !strcasecmp(method, "standard_dec");
00737                            break;
00738       }
00739    }
00740    misdn_cfg_unlock();
00741 
00742    return re;
00743 }

int misdn_cfg_is_msn_valid ( int  port,
char *  msn 
)

Definition at line 681 of file misdn_config.c.

References ast_extension_match(), ast_log(), LOG_WARNING, misdn_cfg_is_port_valid(), misdn_cfg_lock(), MISDN_CFG_MSNS, misdn_cfg_unlock(), misdn_cfg_pt::ml, msn_list::msn, and msn_list::next.

Referenced by misdn_is_msn_valid().

00682 {
00683    int re = 0;
00684    struct msn_list *iter;
00685 
00686    if (!misdn_cfg_is_port_valid(port)) {
00687       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_is_msn_valid! Port number %d is not valid.\n", port);
00688       return 0;
00689    }
00690 
00691    misdn_cfg_lock();
00692    if (port_cfg[port][map[MISDN_CFG_MSNS]].ml)
00693       iter = port_cfg[port][map[MISDN_CFG_MSNS]].ml;
00694    else
00695       iter = port_cfg[0][map[MISDN_CFG_MSNS]].ml;
00696    for (; iter; iter = iter->next)
00697       if (*(iter->msn) == '*' || ast_extension_match(iter->msn, msn)) {
00698          re = 1;
00699          break;
00700       }
00701    misdn_cfg_unlock();
00702 
00703    return re;
00704 }

int misdn_cfg_is_port_valid ( int  port  ) 

Definition at line 706 of file misdn_config.c.

References MISDN_CFG_GROUPNAME, and str.

Referenced by handle_cli_misdn_show_config(), misdn_cfg_get(), misdn_cfg_get_config_string(), misdn_cfg_is_msn_valid(), and misdn_cfg_update_ptp().

00707 {
00708    int gn = map[MISDN_CFG_GROUPNAME];
00709 
00710    return (port >= 1 && port <= max_ports && port_cfg[port][gn].str);
00711 }

static void misdn_cfg_lock ( void   )  [inline, static]

void misdn_cfg_reload ( void   ) 

Definition at line 1129 of file misdn_config.c.

References misdn_cfg_init().

Referenced by reload_config().

01130 {
01131    misdn_cfg_init(0, 1);
01132 }

static void misdn_cfg_unlock ( void   )  [inline, static]

void misdn_cfg_update_ptp ( void   ) 

Definition at line 1060 of file misdn_config.c.

References ast_log(), ast_strlen_zero(), BUFFERSIZE, errno, LOG_WARNING, misdn_cfg_get(), misdn_cfg_is_port_valid(), misdn_cfg_lock(), misdn_cfg_unlock(), and MISDN_GEN_MISDN_INIT.

Referenced by load_module(), and reload_config().

01061 {
01062 #ifndef MISDN_1_2
01063    char misdn_init[BUFFERSIZE];
01064    char line[BUFFERSIZE];
01065    FILE *fp;
01066    char *tok, *p, *end;
01067    int port;
01068 
01069    misdn_cfg_get(0, MISDN_GEN_MISDN_INIT, &misdn_init, sizeof(misdn_init));
01070 
01071    if (!ast_strlen_zero(misdn_init)) {
01072       fp = fopen(misdn_init, "r");
01073       if (fp) {
01074          while(fgets(line, sizeof(line), fp)) {
01075             if (!strncmp(line, "nt_ptp", 6)) {
01076                for (tok = strtok_r(line,",=", &p);
01077                    tok;
01078                    tok = strtok_r(NULL,",=", &p)) {
01079                   port = strtol(tok, &end, 10);
01080                   if (end != tok && misdn_cfg_is_port_valid(port)) {
01081                      misdn_cfg_lock();
01082                      ptp[port] = 1;
01083                      misdn_cfg_unlock();
01084                   }
01085                }
01086             }
01087          }
01088          fclose(fp);
01089       } else {
01090          ast_log(LOG_WARNING,"Couldn't open %s: %s\n", misdn_init, strerror(errno));
01091       }
01092    }
01093 #else
01094    int i;
01095    int proto;
01096    char filename[128];
01097    FILE *fp;
01098 
01099    for (i = 1; i <= max_ports; ++i) {
01100       snprintf(filename, sizeof(filename), "/sys/class/mISDN-stacks/st-%08x/protocol", i << 8);
01101       fp = fopen(filename, "r");
01102       if (!fp) {
01103          ast_log(LOG_WARNING, "Could not open %s: %s\n", filename, strerror(errno));
01104          continue;
01105       }
01106       if (fscanf(fp, "0x%08x", &proto) != 1)
01107          ast_log(LOG_WARNING, "Could not parse contents of %s!\n", filename);
01108       else
01109          ptp[i] = proto & 1<<5 ? 1 : 0;
01110       fclose(fp);
01111    }
01112 #endif
01113 }

struct ast_jb_conf* misdn_get_global_jbconf ( void   )  [read]

Definition at line 1218 of file misdn_config.c.

References global_jbconf.

Referenced by misdn_new().

01218                                               {
01219    return &global_jbconf;
01220 }


Variable Documentation

Definition at line 423 of file misdn_config.c.

struct ast_jb_conf default_jbconf [static]

Global jitterbuffer configuration - by default, jb is disabled

Definition at line 51 of file misdn_config.c.

struct misdn_cfg_spec gen_spec[] [static]

Definition at line 374 of file misdn_config.c.

union misdn_cfg_pt* general_cfg [static]

Definition at line 417 of file misdn_config.c.

struct ast_jb_conf global_jbconf [static]

Definition at line 59 of file misdn_config.c.

int* map [static]

int max_ports [static]

Definition at line 415 of file misdn_config.c.

union misdn_cfg_pt** port_cfg [static]

Definition at line 413 of file misdn_config.c.

struct misdn_cfg_spec port_spec[] [static]

Definition at line 96 of file misdn_config.c.

const char ports_description[] [static]

Initial value:

   "Define your ports, e.g. 1,2 (depends on mISDN-driver loading order)."

Definition at line 93 of file misdn_config.c.

int* ptp [static]

Definition at line 419 of file misdn_config.c.

Referenced by misdn_lib_init().


Generated on Wed Oct 28 13:33:10 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6