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, MISDN_CTYPE_ASTNAMEDGROUP
}

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 444 of file misdn_config.c.

Referenced by _build_general_config(), and _build_port_config().

#define GEN_CFG   1

Definition at line 49 of file misdn_config.c.

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

#define NO_DEFAULT   "<>"

Definition at line 46 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 50 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 
MISDN_CTYPE_ASTNAMEDGROUP 

Definition at line 67 of file misdn_config.c.


Function Documentation

static void _build_general_config ( struct ast_variable v  )  [static]

Definition at line 1034 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().

01035 {
01036    int pos;
01037 
01038    for (; v; v = v->next) {
01039       if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
01040          continue;
01041       if (((pos = get_cfg_position(v->name, GEN_CFG)) < 0) ||
01042          (_parse(&general_cfg[pos], v->value, gen_spec[pos].type, gen_spec[pos].boolint_def) < 0))
01043          CLI_ERROR(v->name, v->value, "general");
01044    }
01045 }

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

Definition at line 1047 of file misdn_config.c.

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

Referenced by misdn_cfg_init().

01048 {
01049    int pos, i;
01050    union misdn_cfg_pt cfg_tmp[NUM_PORT_ELEMENTS];
01051    int cfg_for_ports[max_ports + 1];
01052 
01053    if (!v || !cat)
01054       return;
01055 
01056    memset(cfg_tmp, 0, sizeof(cfg_tmp));
01057    memset(cfg_for_ports, 0, sizeof(cfg_for_ports));
01058 
01059    if (!strcasecmp(cat, "default")) {
01060       cfg_for_ports[0] = 1;
01061    }
01062 
01063    if (((pos = get_cfg_position("name", PORT_CFG)) < 0) ||
01064       (_parse(&cfg_tmp[pos], cat, port_spec[pos].type, port_spec[pos].boolint_def) < 0)) {
01065       CLI_ERROR(v->name, v->value, cat);
01066       return;
01067    }
01068 
01069    for (; v; v = v->next) {
01070       if (!strcasecmp(v->name, "ports")) {
01071          char *token, *tmp = ast_strdupa(v->value);
01072          char ptpbuf[BUFFERSIZE] = "";
01073          int start, end;
01074          for (token = strsep(&tmp, ","); token; token = strsep(&tmp, ","), *ptpbuf = 0) {
01075             if (!*token)
01076                continue;
01077             if (sscanf(token, "%30d-%30d%511s", &start, &end, ptpbuf) >= 2) {
01078                for (; start <= end; start++) {
01079                   if (start <= max_ports && start > 0) {
01080                      cfg_for_ports[start] = 1;
01081                      ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
01082                   } else
01083                      CLI_ERROR(v->name, v->value, cat);
01084                }
01085             } else {
01086                if (sscanf(token, "%30d%511s", &start, ptpbuf)) {
01087                   if (start <= max_ports && start > 0) {
01088                      cfg_for_ports[start] = 1;
01089                      ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
01090                   } else
01091                      CLI_ERROR(v->name, v->value, cat);
01092                } else
01093                   CLI_ERROR(v->name, v->value, cat);
01094             }
01095          }
01096       } else {
01097          if (((pos = get_cfg_position(v->name, PORT_CFG)) < 0) ||
01098             (_parse(&cfg_tmp[pos], v->value, port_spec[pos].type, port_spec[pos].boolint_def) < 0))
01099             CLI_ERROR(v->name, v->value, cat);
01100       }
01101    }
01102 
01103    for (i = 0; i < (max_ports + 1); ++i) {
01104       if (i > 0 && cfg_for_ports[0]) {
01105          /* default category, will populate the port_cfg with additional port
01106          categories in subsequent calls to this function */
01107          memset(cfg_tmp, 0, sizeof(cfg_tmp));
01108       }
01109       if (cfg_for_ports[i]) {
01110          memcpy(port_cfg[i], cfg_tmp, sizeof(cfg_tmp));
01111       }
01112    }
01113 }

static int _enum_array_map ( void   )  [static]

Definition at line 449 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().

00450 {
00451    int i, j, ok;
00452 
00453    for (i = MISDN_CFG_FIRST + 1; i < MISDN_CFG_LAST; ++i) {
00454       if (i == MISDN_CFG_PTP)
00455          continue;
00456       ok = 0;
00457       for (j = 0; j < NUM_PORT_ELEMENTS; ++j) {
00458          if (port_spec[j].elem == i) {
00459             map[i] = j;
00460             ok = 1;
00461             break;
00462          }
00463       }
00464       if (!ok) {
00465          ast_log(LOG_WARNING, "Enum element %d in misdn_cfg_elements (port section) has no corresponding element in the config struct!\n", i);
00466          return -1;
00467       }
00468    }
00469    for (i = MISDN_GEN_FIRST + 1; i < MISDN_GEN_LAST; ++i) {
00470       ok = 0;
00471       for (j = 0; j < NUM_GEN_ELEMENTS; ++j) {
00472          if (gen_spec[j].elem == i) {
00473             map[i] = j;
00474             ok = 1;
00475             break;
00476          }
00477       }
00478       if (!ok) {
00479          ast_log(LOG_WARNING, "Enum element %d in misdn_cfg_elements (general section) has no corresponding element in the config struct!\n", i);
00480          return -1;
00481       }
00482    }
00483    return 0;
00484 }

static void _fill_defaults ( void   )  [static]

Definition at line 1170 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().

01171 {
01172    int i;
01173 
01174    for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
01175       if (!port_cfg[0][i].any && strcasecmp(port_spec[i].def, NO_DEFAULT))
01176          _parse(&(port_cfg[0][i]), (char *)port_spec[i].def, port_spec[i].type, port_spec[i].boolint_def);
01177    }
01178    for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
01179       if (!general_cfg[i].any && strcasecmp(gen_spec[i].def, NO_DEFAULT))
01180          _parse(&(general_cfg[i]), (char *)gen_spec[i].def, gen_spec[i].type, gen_spec[i].boolint_def);
01181    }
01182 }

static void _free_general_cfg ( void   )  [static]

Definition at line 562 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().

00563 {
00564    int i;
00565 
00566    for (i = 0; i < NUM_GEN_ELEMENTS; i++)
00567       if (general_cfg[i].any)
00568          ast_free(general_cfg[i].any);
00569 }

static void _free_msn_list ( struct msn_list iter  )  [static]

Definition at line 517 of file misdn_config.c.

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

Referenced by _free_port_cfg().

00518 {
00519    if (iter->next)
00520       _free_msn_list(iter->next);
00521    if (iter->msn)
00522       ast_free(iter->msn);
00523    ast_free(iter);
00524 }

static void _free_port_cfg ( void   )  [static]

Definition at line 526 of file misdn_config.c.

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

Referenced by misdn_cfg_destroy(), and misdn_cfg_init().

00527 {
00528    int i, j;
00529    int gn = map[MISDN_CFG_GROUPNAME];
00530    union misdn_cfg_pt* free_list[max_ports + 2];
00531 
00532    memset(free_list, 0, sizeof(free_list));
00533    free_list[0] = port_cfg[0];
00534    for (i = 1; i <= max_ports; ++i) {
00535       if (port_cfg[i][gn].str) {
00536          /* we always have a groupname in the non-default case, so this is fine */
00537          for (j = 1; j <= max_ports; ++j) {
00538             if (free_list[j] && free_list[j][gn].str == port_cfg[i][gn].str)
00539                break;
00540             else if (!free_list[j]) {
00541                free_list[j] = port_cfg[i];
00542                break;
00543             }
00544          }
00545       }
00546    }
00547    for (j = 0; free_list[j]; ++j) {
00548       for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
00549          if (free_list[j][i].any) {
00550             if (port_spec[i].type == MISDN_CTYPE_MSNLIST) {
00551                _free_msn_list(free_list[j][i].ml);
00552             } else if (port_spec[i].type == MISDN_CTYPE_ASTNAMEDGROUP) {
00553                ast_unref_namedgroups(free_list[j][i].namgrp);
00554             } else {
00555                ast_free(free_list[j][i].any);
00556             }
00557          }
00558       }
00559    }
00560 }

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

Definition at line 954 of file misdn_config.c.

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

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

00955 {
00956    int re = 0;
00957    int len, tmp;
00958    char *valtmp;
00959    char *tmp2 = ast_strdupa(value);
00960 
00961    switch (type) {
00962    case MISDN_CTYPE_STR:
00963       if (dest->str) {
00964          ast_free(dest->str);
00965       }
00966       if ((len = strlen(value))) {
00967          dest->str = ast_malloc((len + 1) * sizeof(char));
00968          strncpy(dest->str, value, len);
00969          dest->str[len] = 0;
00970       } else {
00971          dest->str = ast_malloc(sizeof(char));
00972          dest->str[0] = 0;
00973       }
00974       break;
00975    case MISDN_CTYPE_INT:
00976    {
00977       int res;
00978 
00979       if (strchr(value,'x')) {
00980          res = sscanf(value, "%30x", &tmp);
00981       } else {
00982          res = sscanf(value, "%30d", &tmp);
00983       }
00984       if (res) {
00985          if (!dest->num) {
00986             dest->num = ast_malloc(sizeof(int));
00987          }
00988          memcpy(dest->num, &tmp, sizeof(int));
00989       } else
00990          re = -1;
00991    }
00992       break;
00993    case MISDN_CTYPE_BOOL:
00994       if (!dest->num) {
00995          dest->num = ast_malloc(sizeof(int));
00996       }
00997       *(dest->num) = (ast_true(value) ? 1 : 0);
00998       break;
00999    case MISDN_CTYPE_BOOLINT:
01000       if (!dest->num) {
01001          dest->num = ast_malloc(sizeof(int));
01002       }
01003       if (sscanf(value, "%30d", &tmp)) {
01004          memcpy(dest->num, &tmp, sizeof(int));
01005       } else {
01006          *(dest->num) = (ast_true(value) ? boolint_def : 0);
01007       }
01008       break;
01009    case MISDN_CTYPE_MSNLIST:
01010       for (valtmp = strsep(&tmp2, ","); valtmp; valtmp = strsep(&tmp2, ",")) {
01011          if ((len = strlen(valtmp))) {
01012             struct msn_list *ml = ast_malloc(sizeof(*ml));
01013             ml->msn = ast_calloc(len+1, sizeof(char));
01014             strncpy(ml->msn, valtmp, len);
01015             ml->next = dest->ml;
01016             dest->ml = ml;
01017          }
01018       }
01019       break;
01020    case MISDN_CTYPE_ASTGROUP:
01021       if (!dest->grp) {
01022          dest->grp = ast_malloc(sizeof(ast_group_t));
01023       }
01024       *(dest->grp) = ast_get_group(value);
01025       break;
01026    case MISDN_CTYPE_ASTNAMEDGROUP:
01027       dest->namgrp = ast_get_namedgroups(value);
01028       break;
01029    }
01030 
01031    return re;
01032 }

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

Definition at line 486 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().

00487 {
00488    int i;
00489 
00490    switch (type) {
00491    case PORT_CFG:
00492       for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
00493          if (!strcasecmp(name, port_spec[i].name))
00494             return i;
00495       }
00496       break;
00497    case GEN_CFG:
00498       for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
00499          if (!strcasecmp(name, gen_spec[i].name))
00500             return i;
00501       }
00502    }
00503 
00504    return -1;
00505 }

void misdn_cfg_destroy ( void   ) 

Definition at line 1189 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().

01190 {
01191    misdn_cfg_lock();
01192 
01193    _free_port_cfg();
01194    _free_general_cfg();
01195 
01196    ast_free(port_cfg);
01197    ast_free(general_cfg);
01198    ast_free(ptp);
01199    ast_free(map);
01200 
01201    misdn_cfg_unlock();
01202    ast_mutex_destroy(&config_mutex);
01203 }

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

Definition at line 571 of file misdn_config.c.

References 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_ASTNAMEDGROUP, MISDN_CTYPE_STR, misdn_cfg_pt::namgrp, NULL, S_OR, str, and type.

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

00572 {
00573    int place;
00574 
00575    if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
00576       memset(buf, 0, bufsize);
00577       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Port number %d is not valid.\n", port);
00578       return;
00579    }
00580 
00581    misdn_cfg_lock();
00582    if (elem == MISDN_CFG_PTP) {
00583       if (!memcpy(buf, &ptp[port], (bufsize > ptp[port]) ? sizeof(ptp[port]) : bufsize))
00584          memset(buf, 0, bufsize);
00585    } else {
00586       if ((place = map[elem]) < 0) {
00587          memset(buf, 0, bufsize);
00588          ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Invalid element (%d) requested.\n", elem);
00589       } else {
00590          if (elem < MISDN_CFG_LAST) {
00591             switch (port_spec[place].type) {
00592             case MISDN_CTYPE_STR:
00593                if (port_cfg[port][place].str) {
00594                   ast_copy_string(buf, port_cfg[port][place].str, bufsize);
00595                } else if (port_cfg[0][place].str) {
00596                   ast_copy_string(buf, port_cfg[0][place].str, bufsize);
00597                } else
00598                   memset(buf, 0, bufsize);
00599                break;
00600             case MISDN_CTYPE_ASTNAMEDGROUP:
00601                if (bufsize >= sizeof(struct ast_namedgroups *)) {
00602                   if (port_cfg[port][place].namgrp) {
00603                      *(struct ast_namedgroups **)buf = port_cfg[port][place].namgrp;
00604                   } else if (port_cfg[0][place].namgrp) {
00605                      *(struct ast_namedgroups **)buf = port_cfg[0][place].namgrp;
00606                   } else {
00607                      *(struct ast_namedgroups **)buf = NULL;
00608                   }
00609                }
00610                break;
00611             default:
00612                if (port_cfg[port][place].any)
00613                   memcpy(buf, port_cfg[port][place].any, bufsize);
00614                else if (port_cfg[0][place].any)
00615                   memcpy(buf, port_cfg[0][place].any, bufsize);
00616                else
00617                   memset(buf, 0, bufsize);
00618             }
00619          } else {
00620             switch (gen_spec[place].type) {
00621             case MISDN_CTYPE_STR:
00622                ast_copy_string(buf, S_OR(general_cfg[place].str, ""), bufsize);
00623                break;
00624             default:
00625                if (general_cfg[place].any)
00626                   memcpy(buf, general_cfg[place].any, bufsize);
00627                else
00628                   memset(buf, 0, bufsize);
00629             }
00630          }
00631       }
00632    }
00633    misdn_cfg_unlock();
00634 }

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

Definition at line 807 of file misdn_config.c.

References ast_free, ast_log, ast_print_group(), ast_print_namedgroups(), ast_str_create(), 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_ASTNAMEDGROUP, 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, str, and type.

Referenced by handle_cli_misdn_show_config().

00808 {
00809    int place;
00810    char tempbuf[BUFFERSIZE] = "";
00811    struct msn_list *iter;
00812 
00813    if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
00814       *buf = 0;
00815       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Port number %d is not valid.\n", port);
00816       return;
00817    }
00818 
00819    place = map[elem];
00820 
00821    misdn_cfg_lock();
00822    if (elem == MISDN_CFG_PTP) {
00823       snprintf(buf, bufsize, " -> ptp: %s", ptp[port] ? "yes" : "no");
00824    }
00825    else if (elem > MISDN_CFG_FIRST && elem < MISDN_CFG_LAST) {
00826       switch (port_spec[place].type) {
00827       case MISDN_CTYPE_INT:
00828       case MISDN_CTYPE_BOOLINT:
00829          if (port_cfg[port][place].num)
00830             snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[port][place].num);
00831          else if (port_cfg[0][place].num)
00832             snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[0][place].num);
00833          else
00834             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00835          break;
00836       case MISDN_CTYPE_BOOL:
00837          if (port_cfg[port][place].num)
00838             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[port][place].num ? "yes" : "no");
00839          else if (port_cfg[0][place].num)
00840             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[0][place].num ? "yes" : "no");
00841          else
00842             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00843          break;
00844       case MISDN_CTYPE_ASTGROUP:
00845          if (port_cfg[port][place].grp)
00846             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name,
00847                    ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[port][place].grp));
00848          else if (port_cfg[0][place].grp)
00849             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name,
00850                    ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[0][place].grp));
00851          else
00852             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00853          break;
00854       case MISDN_CTYPE_ASTNAMEDGROUP:
00855          if (port_cfg[port][place].namgrp) {
00856             struct ast_str *tmp_str = ast_str_create(1024);
00857             if (tmp_str) {
00858                snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name,
00859                      ast_print_namedgroups(&tmp_str, port_cfg[port][place].namgrp));
00860                ast_free(tmp_str);
00861             }
00862          } else if (port_cfg[0][place].namgrp) {
00863             struct ast_str *tmp_str = ast_str_create(1024);
00864             if (tmp_str) {
00865                snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name,
00866                      ast_print_namedgroups(&tmp_str, port_cfg[0][place].namgrp));
00867                ast_free(tmp_str);
00868             }
00869          } else {
00870             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00871          }
00872          break;
00873       case MISDN_CTYPE_MSNLIST:
00874          if (port_cfg[port][place].ml)
00875             iter = port_cfg[port][place].ml;
00876          else
00877             iter = port_cfg[0][place].ml;
00878          if (iter) {
00879             for (; iter; iter = iter->next) {
00880                strncat(tempbuf, iter->msn, sizeof(tempbuf) - strlen(tempbuf) - 1);
00881             }
00882             if (strlen(tempbuf) > 1) {
00883                tempbuf[strlen(tempbuf)-2] = 0;
00884             }
00885          }
00886          snprintf(buf, bufsize, " -> msns: %s", *tempbuf ? tempbuf : "none");
00887          break;
00888       case MISDN_CTYPE_STR:
00889          if ( port_cfg[port][place].str) {
00890             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[port][place].str);
00891          } else if (port_cfg[0][place].str) {
00892             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[0][place].str);
00893          } else {
00894             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00895          }
00896          break;
00897       }
00898    } else if (elem > MISDN_GEN_FIRST && elem < MISDN_GEN_LAST) {
00899       switch (gen_spec[place].type) {
00900       case MISDN_CTYPE_INT:
00901       case MISDN_CTYPE_BOOLINT:
00902          if (general_cfg[place].num)
00903             snprintf(buf, bufsize, " -> %s: %d", gen_spec[place].name, *general_cfg[place].num);
00904          else
00905             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00906          break;
00907       case MISDN_CTYPE_BOOL:
00908          if (general_cfg[place].num)
00909             snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, *general_cfg[place].num ? "yes" : "no");
00910          else
00911             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00912          break;
00913       case MISDN_CTYPE_STR:
00914          if ( general_cfg[place].str) {
00915             snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, general_cfg[place].str);
00916          } else {
00917             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00918          }
00919          break;
00920       default:
00921          snprintf(buf, bufsize, " -> type of %s not handled yet", gen_spec[place].name);
00922          break;
00923       }
00924    } else {
00925       *buf = 0;
00926       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Invalid config element (%d) requested.\n", elem);
00927    }
00928    misdn_cfg_unlock();
00929 }

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

Definition at line 683 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, NO_DEFAULT, and NULL.

Referenced by show_config_description().

00684 {
00685    int place = map[elem];
00686    struct misdn_cfg_spec *spec = NULL;
00687 
00688    /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
00689    if (elem == MISDN_CFG_GROUPNAME) {
00690       ast_copy_string(buf, ports_description, bufsize);
00691       if (buf_default && bufsize_default)
00692          memset(buf_default, 0, 1);
00693       return;
00694    }
00695 
00696    if ((elem > MISDN_CFG_FIRST) && (elem < MISDN_CFG_LAST))
00697       spec = (struct misdn_cfg_spec *)port_spec;
00698    else if ((elem > MISDN_GEN_FIRST) && (elem < MISDN_GEN_LAST))
00699       spec = (struct misdn_cfg_spec *)gen_spec;
00700 
00701    if (!spec || !spec[place].desc)
00702       memset(buf, 0, 1);
00703    else {
00704       ast_copy_string(buf, spec[place].desc, bufsize);
00705       if (buf_default && bufsize) {
00706          if (!strcmp(spec[place].def, NO_DEFAULT))
00707             memset(buf_default, 0, 1);
00708          else
00709             ast_copy_string(buf_default, spec[place].def, bufsize_default);
00710       }
00711    }
00712 }

enum misdn_cfg_elements misdn_cfg_get_elem ( const char *  name  ) 

Definition at line 636 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().

00637 {
00638    int pos;
00639 
00640    /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
00641    if (!strcmp(name, "ports"))
00642       return MISDN_CFG_GROUPNAME;
00643    if (!strcmp(name, "name"))
00644       return MISDN_CFG_FIRST;
00645 
00646    pos = get_cfg_position(name, PORT_CFG);
00647    if (pos >= 0)
00648       return port_spec[pos].elem;
00649 
00650    pos = get_cfg_position(name, GEN_CFG);
00651    if (pos >= 0)
00652       return gen_spec[pos].elem;
00653 
00654    return MISDN_CFG_FIRST;
00655 }

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

Definition at line 657 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, name, and NULL.

Referenced by complete_show_config(), and show_config_description().

00658 {
00659    struct misdn_cfg_spec *spec = NULL;
00660    int place = map[elem];
00661 
00662    /* the ptp hack */
00663    if (elem == MISDN_CFG_PTP) {
00664       memset(buf, 0, 1);
00665       return;
00666    }
00667 
00668    /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
00669    if (elem == MISDN_CFG_GROUPNAME) {
00670       if (!snprintf(buf, bufsize, "ports"))
00671          memset(buf, 0, 1);
00672       return;
00673    }
00674 
00675    if ((elem > MISDN_CFG_FIRST) && (elem < MISDN_CFG_LAST))
00676       spec = (struct misdn_cfg_spec *)port_spec;
00677    else if ((elem > MISDN_GEN_FIRST) && (elem < MISDN_GEN_LAST))
00678       spec = (struct misdn_cfg_spec *)gen_spec;
00679 
00680    ast_copy_string(buf, spec ? spec[place].name : "", bufsize);
00681 }

int misdn_cfg_get_next_port ( int  port  ) 

Definition at line 931 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().

00932 {
00933    int p = -1;
00934    int gn = map[MISDN_CFG_GROUPNAME];
00935 
00936    misdn_cfg_lock();
00937    for (port++; port <= max_ports; port++) {
00938       if (port_cfg[port][gn].str) {
00939          p = port;
00940          break;
00941       }
00942    }
00943    misdn_cfg_unlock();
00944 
00945    return p;
00946 }

int misdn_cfg_get_next_port_spin ( int  port  ) 

Definition at line 948 of file misdn_config.c.

References misdn_cfg_get_next_port().

Referenced by misdn_request().

00949 {
00950    int p = misdn_cfg_get_next_port(port);
00951    return (p > 0) ? p : misdn_cfg_get_next_port(0);
00952 }

void misdn_cfg_get_ports_string ( char *  ports  ) 

Generate a comma separated list of all active ports.

Definition at line 781 of file misdn_config.c.

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

Referenced by load_module().

00782 {
00783    char tmp[16];
00784    int l, i;
00785    int gn = map[MISDN_CFG_GROUPNAME];
00786 
00787    *ports = 0;
00788 
00789    misdn_cfg_lock();
00790    for (i = 1; i <= max_ports; i++) {
00791       if (port_cfg[i][gn].str) {
00792          if (ptp[i])
00793             sprintf(tmp, "%dptp,", i);
00794          else
00795             sprintf(tmp, "%d,", i);
00796          strcat(ports, tmp);
00797       }
00798    }
00799    misdn_cfg_unlock();
00800 
00801    if ((l = strlen(ports))) {
00802       /* Strip trailing ',' */
00803       ports[l-1] = 0;
00804    }
00805 }

int misdn_cfg_init ( int  this_max_ports,
int  reload 
)

Definition at line 1205 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, NULL, NUM_GEN_ELEMENTS, and NUM_PORT_ELEMENTS.

Referenced by load_module(), and misdn_cfg_reload().

01206 {
01207    char config[] = "misdn.conf";
01208    char *cat, *p;
01209    int i;
01210    struct ast_config *cfg;
01211    struct ast_variable *v;
01212    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
01213 
01214    if (!(cfg = ast_config_load2(config, "chan_misdn", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
01215       ast_log(LOG_WARNING, "missing or invalid file: misdn.conf\n");
01216       return -1;
01217    } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
01218       return 0;
01219 
01220    ast_mutex_init(&config_mutex);
01221 
01222    /* Copy the default jb config over global_jbconf */
01223    memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
01224 
01225    misdn_cfg_lock();
01226 
01227    if (this_max_ports) {
01228       /* this is the first run */
01229       max_ports = this_max_ports;
01230       map = ast_calloc(MISDN_GEN_LAST + 1, sizeof(int));
01231       if (_enum_array_map())
01232          return -1;
01233       p = ast_calloc(1, (max_ports + 1) * sizeof(union misdn_cfg_pt *)
01234                      + (max_ports + 1) * NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt));
01235       port_cfg = (union misdn_cfg_pt **)p;
01236       p += (max_ports + 1) * sizeof(union misdn_cfg_pt *);
01237       for (i = 0; i <= max_ports; ++i) {
01238          port_cfg[i] = (union misdn_cfg_pt *)p;
01239          p += NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt);
01240       }
01241       general_cfg = ast_calloc(1, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
01242       ptp = ast_calloc(max_ports + 1, sizeof(int));
01243    }
01244    else {
01245       /* misdn reload */
01246       _free_port_cfg();
01247       _free_general_cfg();
01248       memset(port_cfg[0], 0, NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt) * (max_ports + 1));
01249       memset(general_cfg, 0, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
01250       memset(ptp, 0, sizeof(int) * (max_ports + 1));
01251    }
01252 
01253    cat = ast_category_browse(cfg, NULL);
01254 
01255    while(cat) {
01256       v = ast_variable_browse(cfg, cat);
01257       if (!strcasecmp(cat, "general")) {
01258          _build_general_config(v);
01259       } else {
01260          _build_port_config(v, cat);
01261       }
01262       cat = ast_category_browse(cfg, cat);
01263    }
01264 
01265    _fill_defaults();
01266 
01267    misdn_cfg_unlock();
01268    ast_config_destroy(cfg);
01269 
01270    return 0;
01271 }

int misdn_cfg_is_group_method ( char *  group,
enum misdn_cfg_method  meth 
)

Definition at line 746 of file misdn_config.c.

References method, 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().

00747 {
00748    int i, re = 0;
00749    char *method ;
00750 
00751    misdn_cfg_lock();
00752 
00753    method = port_cfg[0][map[MISDN_CFG_METHOD]].str;
00754 
00755    for (i = 1; i <= max_ports; i++) {
00756       if (port_cfg[i] && port_cfg[i][map[MISDN_CFG_GROUPNAME]].str) {
00757          if (!strcasecmp(port_cfg[i][map[MISDN_CFG_GROUPNAME]].str, group))
00758             method = (port_cfg[i][map[MISDN_CFG_METHOD]].str ?
00759                     port_cfg[i][map[MISDN_CFG_METHOD]].str : port_cfg[0][map[MISDN_CFG_METHOD]].str);
00760       }
00761    }
00762 
00763    if (method) {
00764       switch (meth) {
00765       case METHOD_STANDARD:      re = !strcasecmp(method, "standard");
00766                            break;
00767       case METHOD_ROUND_ROBIN:   re = !strcasecmp(method, "round_robin");
00768                            break;
00769       case METHOD_STANDARD_DEC:  re = !strcasecmp(method, "standard_dec");
00770                            break;
00771       }
00772    }
00773    misdn_cfg_unlock();
00774 
00775    return re;
00776 }

int misdn_cfg_is_msn_valid ( int  port,
char *  msn 
)

Definition at line 714 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().

00715 {
00716    int re = 0;
00717    struct msn_list *iter;
00718 
00719    if (!misdn_cfg_is_port_valid(port)) {
00720       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_is_msn_valid! Port number %d is not valid.\n", port);
00721       return 0;
00722    }
00723 
00724    misdn_cfg_lock();
00725    if (port_cfg[port][map[MISDN_CFG_MSNS]].ml)
00726       iter = port_cfg[port][map[MISDN_CFG_MSNS]].ml;
00727    else
00728       iter = port_cfg[0][map[MISDN_CFG_MSNS]].ml;
00729    for (; iter; iter = iter->next)
00730       if (*(iter->msn) == '*' || ast_extension_match(iter->msn, msn)) {
00731          re = 1;
00732          break;
00733       }
00734    misdn_cfg_unlock();
00735 
00736    return re;
00737 }

int misdn_cfg_is_port_valid ( int  port  ) 

Definition at line 739 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().

00740 {
00741    int gn = map[MISDN_CFG_GROUPNAME];
00742 
00743    return (port >= 1 && port <= max_ports && port_cfg[port][gn].str);
00744 }

static void misdn_cfg_lock ( void   )  [inline, static]

void misdn_cfg_reload ( void   ) 

Definition at line 1184 of file misdn_config.c.

References misdn_cfg_init().

Referenced by reload_config().

01185 {
01186    misdn_cfg_init(0, 1);
01187 }

static void misdn_cfg_unlock ( void   )  [inline, static]

void misdn_cfg_update_ptp ( void   ) 

Definition at line 1115 of file misdn_config.c.

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

Referenced by load_module(), and reload_config().

01116 {
01117 #ifndef MISDN_1_2
01118    char misdn_init[BUFFERSIZE];
01119    char line[BUFFERSIZE];
01120    FILE *fp;
01121    char *tok, *p, *end;
01122    int port;
01123 
01124    misdn_cfg_get(0, MISDN_GEN_MISDN_INIT, &misdn_init, sizeof(misdn_init));
01125 
01126    if (!ast_strlen_zero(misdn_init)) {
01127       fp = fopen(misdn_init, "r");
01128       if (fp) {
01129          while(fgets(line, sizeof(line), fp)) {
01130             if (!strncmp(line, "nt_ptp", 6)) {
01131                for (tok = strtok_r(line,",=", &p);
01132                    tok;
01133                    tok = strtok_r(NULL,",=", &p)) {
01134                   port = strtol(tok, &end, 10);
01135                   if (end != tok && misdn_cfg_is_port_valid(port)) {
01136                      misdn_cfg_lock();
01137                      ptp[port] = 1;
01138                      misdn_cfg_unlock();
01139                   }
01140                }
01141             }
01142          }
01143          fclose(fp);
01144       } else {
01145          ast_log(LOG_WARNING,"Couldn't open %s: %s\n", misdn_init, strerror(errno));
01146       }
01147    }
01148 #else
01149    int i;
01150    int proto;
01151    char filename[128];
01152    FILE *fp;
01153 
01154    for (i = 1; i <= max_ports; ++i) {
01155       snprintf(filename, sizeof(filename), "/sys/class/mISDN-stacks/st-%08x/protocol", i << 8);
01156       fp = fopen(filename, "r");
01157       if (!fp) {
01158          ast_log(LOG_WARNING, "Could not open %s: %s\n", filename, strerror(errno));
01159          continue;
01160       }
01161       if (fscanf(fp, "0x%08x", &proto) != 1)
01162          ast_log(LOG_WARNING, "Could not parse contents of %s!\n", filename);
01163       else
01164          ptp[i] = proto & 1<<5 ? 1 : 0;
01165       fclose(fp);
01166    }
01167 #endif
01168 }

struct ast_jb_conf* misdn_get_global_jbconf ( void   )  [read]

Definition at line 1273 of file misdn_config.c.

References global_jbconf.

Referenced by misdn_new().

01273                                               {
01274    return &global_jbconf;
01275 }


Variable Documentation

Definition at line 442 of file misdn_config.c.

struct ast_jb_conf default_jbconf [static]

Global jitterbuffer configuration - by default, jb is disabled

Note:
Values shown here match the defaults shown in misdn.conf.sample

Definition at line 56 of file misdn_config.c.

struct misdn_cfg_spec gen_spec[] [static]

Definition at line 393 of file misdn_config.c.

union misdn_cfg_pt* general_cfg [static]

Definition at line 436 of file misdn_config.c.

struct ast_jb_conf global_jbconf [static]

Definition at line 65 of file misdn_config.c.

int* map [static]

int max_ports [static]

Definition at line 434 of file misdn_config.c.

union misdn_cfg_pt** port_cfg [static]

Definition at line 432 of file misdn_config.c.

struct misdn_cfg_spec port_spec[] [static]

Definition at line 104 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 101 of file misdn_config.c.

int* ptp [static]

Definition at line 438 of file misdn_config.c.

Referenced by misdn_lib_init().


Generated on Thu Apr 16 06:32:01 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6