Fri Jul 3 20:38:43 2009

Asterisk developer's documentation


misdn_config.c File Reference

chan_misdn configuration management More...

#include "asterisk.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "chan_misdn_config.h"
#include "asterisk/config.h"
#include "asterisk/channel.h"
#include "asterisk/logger.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 AST_DESTROY_CFG   ast_config_destroy
#define AST_LOAD_CFG   ast_config_load
#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, char *value, enum misdn_cfg_type type, int boolint_def)
static int get_cfg_position (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 (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 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)

Variables

static ast_mutex_t config_mutex
static struct misdn_cfg_spec gen_spec []
static union misdn_cfg_pt * general_cfg
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 AST_DESTROY_CFG   ast_config_destroy

Definition at line 49 of file misdn_config.c.

Referenced by misdn_cfg_init().

#define AST_LOAD_CFG   ast_config_load

Definition at line 48 of file misdn_config.c.

Referenced by misdn_cfg_init().

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

Referenced by _build_general_config(), and _build_port_config().

#define GEN_CFG   1

Definition at line 54 of file misdn_config.c.

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

#define NO_DEFAULT   "<>"

Definition at line 51 of file misdn_config.c.

Referenced by _fill_defaults(), and misdn_cfg_get_desc().

#define NONE   0

Definition at line 52 of file misdn_config.c.

#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 55 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 59 of file misdn_config.c.


Function Documentation

static void _build_general_config ( struct ast_variable *  v  )  [static]

Definition at line 932 of file misdn_config.c.

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

Referenced by misdn_cfg_init().

00933 {
00934    int pos;
00935 
00936    for (; v; v = v->next) {
00937       if (((pos = get_cfg_position(v->name, GEN_CFG)) < 0) || 
00938          (_parse(&general_cfg[pos], v->value, gen_spec[pos].type, gen_spec[pos].boolint_def) < 0))
00939          CLI_ERROR(v->name, v->value, "general");
00940    }
00941 }

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

Definition at line 943 of file misdn_config.c.

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

Referenced by misdn_cfg_init().

00944 {
00945    int pos, i;
00946    union misdn_cfg_pt cfg_tmp[NUM_PORT_ELEMENTS];
00947    int cfg_for_ports[max_ports + 1];
00948 
00949    if (!v || !cat)
00950       return;
00951 
00952    memset(cfg_tmp, 0, sizeof(cfg_tmp));
00953    memset(cfg_for_ports, 0, sizeof(cfg_for_ports));
00954 
00955    if (!strcasecmp(cat, "default")) {
00956       cfg_for_ports[0] = 1;
00957    }
00958 
00959    if (((pos = get_cfg_position("name", PORT_CFG)) < 0) || 
00960       (_parse(&cfg_tmp[pos], cat, port_spec[pos].type, port_spec[pos].boolint_def) < 0)) {
00961       CLI_ERROR(v->name, v->value, cat);
00962       return;
00963    }
00964 
00965    for (; v; v = v->next) {
00966       if (!strcasecmp(v->name, "ports")) {
00967          char *token;
00968          char ptpbuf[BUFFERSIZE] = "";
00969          int start, end;
00970          for (token = strsep(&v->value, ","); token; token = strsep(&v->value, ","), *ptpbuf = 0) { 
00971             if (!*token)
00972                continue;
00973             if (sscanf(token, "%d-%d%s", &start, &end, ptpbuf) >= 2) {
00974                for (; start <= end; start++) {
00975                   if (start <= max_ports && start > 0) {
00976                      cfg_for_ports[start] = 1;
00977                      ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
00978                   } else
00979                      CLI_ERROR(v->name, v->value, cat);
00980                }
00981             } else {
00982                if (sscanf(token, "%d%s", &start, ptpbuf)) {
00983                   if (start <= max_ports && start > 0) {
00984                      cfg_for_ports[start] = 1;
00985                      ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
00986                   } else
00987                      CLI_ERROR(v->name, v->value, cat);
00988                } else
00989                   CLI_ERROR(v->name, v->value, cat);
00990             }
00991          }
00992       } else {
00993          if (((pos = get_cfg_position(v->name, PORT_CFG)) < 0) || 
00994             (_parse(&cfg_tmp[pos], v->value, port_spec[pos].type, port_spec[pos].boolint_def) < 0))
00995             CLI_ERROR(v->name, v->value, cat);
00996       }
00997    }
00998 
00999    for (i = 0; i < (max_ports + 1); ++i) {
01000       if (cfg_for_ports[i]) {
01001          memcpy(port_cfg[i], cfg_tmp, sizeof(cfg_tmp));
01002       }
01003    }
01004 }

static int _enum_array_map ( void   )  [static]

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

00395 {
00396    int i, j, ok;
00397 
00398    for (i = MISDN_CFG_FIRST + 1; i < MISDN_CFG_LAST; ++i) {
00399       if (i == MISDN_CFG_PTP)
00400          continue;
00401       ok = 0;
00402       for (j = 0; j < NUM_PORT_ELEMENTS; ++j) {
00403          if (port_spec[j].elem == i) {
00404             map[i] = j;
00405             ok = 1;
00406             break;
00407          }
00408       }
00409       if (!ok) {
00410          ast_log(LOG_WARNING, "Enum element %d in misdn_cfg_elements (port section) has no corresponding element in the config struct!\n", i);
00411          return -1;
00412       }
00413    }
00414    for (i = MISDN_GEN_FIRST + 1; i < MISDN_GEN_LAST; ++i) {
00415       ok = 0;
00416       for (j = 0; j < NUM_GEN_ELEMENTS; ++j) {
00417          if (gen_spec[j].elem == i) {
00418             map[i] = j;
00419             ok = 1;
00420             break;
00421          }
00422       }
00423       if (!ok) {
00424          ast_log(LOG_WARNING, "Enum element %d in misdn_cfg_elements (general section) has no corresponding element in the config struct!\n", i);
00425          return -1;
00426       }
00427    }
00428    return 0;
00429 }

static void _fill_defaults ( void   )  [static]

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

01062 {
01063    int i;
01064 
01065    for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
01066       if (!port_cfg[0][i].any && strcasecmp(port_spec[i].def, NO_DEFAULT))
01067          _parse(&(port_cfg[0][i]), (char *)port_spec[i].def, port_spec[i].type, port_spec[i].boolint_def);
01068    }
01069    for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
01070       if (!general_cfg[i].any && strcasecmp(gen_spec[i].def, NO_DEFAULT))
01071          _parse(&(general_cfg[i]), (char *)gen_spec[i].def, gen_spec[i].type, gen_spec[i].boolint_def);
01072    }
01073 }

static void _free_general_cfg ( void   )  [static]

Definition at line 504 of file misdn_config.c.

References misdn_cfg_pt::any, free, and NUM_GEN_ELEMENTS.

Referenced by misdn_cfg_destroy(), and misdn_cfg_init().

00505 {
00506    int i;
00507 
00508    for (i = 0; i < NUM_GEN_ELEMENTS; i++) 
00509       if (general_cfg[i].any)
00510          free(general_cfg[i].any);
00511 }

static void _free_msn_list ( struct msn_list *  iter  )  [static]

Definition at line 462 of file misdn_config.c.

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

Referenced by _free_port_cfg().

00463 {
00464    if (iter->next)
00465       _free_msn_list(iter->next);
00466    if (iter->msn)
00467       free(iter->msn);
00468    free(iter);
00469 }

static void _free_port_cfg ( void   )  [static]

Definition at line 471 of file misdn_config.c.

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

Referenced by misdn_cfg_destroy(), and misdn_cfg_init().

00472 {
00473    int i, j;
00474    int gn = map[MISDN_CFG_GROUPNAME];
00475    union misdn_cfg_pt* free_list[max_ports + 2];
00476    
00477    memset(free_list, 0, sizeof(free_list));
00478    free_list[0] = port_cfg[0];
00479    for (i = 1; i <= max_ports; ++i) {
00480       if (port_cfg[i][gn].str) {
00481          /* we always have a groupname in the non-default case, so this is fine */
00482          for (j = 1; j <= max_ports; ++j) {
00483             if (free_list[j] && free_list[j][gn].str == port_cfg[i][gn].str)
00484                break;
00485             else if (!free_list[j]) {
00486                free_list[j] = port_cfg[i];
00487                break;
00488             }
00489          }
00490       }
00491    }
00492    for (j = 0; free_list[j]; ++j) {
00493       for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
00494          if (free_list[j][i].any) {
00495             if (port_spec[i].type == MISDN_CTYPE_MSNLIST)
00496                _free_msn_list(free_list[j][i].ml);
00497             else
00498                free(free_list[j][i].any);
00499          }
00500       }
00501    }
00502 }

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

Definition at line 869 of file misdn_config.c.

References ast_get_group(), ast_true(), calloc, misdn_cfg_pt::grp, len(), malloc, 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, and misdn_cfg_pt::str.

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

00870 {
00871    int re = 0;
00872    int len, tmp;
00873    char *valtmp;
00874 
00875    switch (type) {
00876    case MISDN_CTYPE_STR:
00877       if ((len = strlen(value))) {
00878          dest->str = (char *)malloc((len + 1) * sizeof(char));
00879          strncpy(dest->str, value, len);
00880          dest->str[len] = 0;
00881       } else {
00882          dest->str = (char *)malloc( sizeof(char));
00883          dest->str[0] = 0;
00884       }
00885       break;
00886    case MISDN_CTYPE_INT:
00887    {
00888       char *pat;
00889       if (strchr(value,'x')) 
00890          pat="%x";
00891       else
00892          pat="%d";
00893       if (sscanf(value, pat, &tmp)) {
00894          dest->num = (int *)malloc(sizeof(int));
00895          memcpy(dest->num, &tmp, sizeof(int));
00896       } else
00897          re = -1;
00898    }
00899       break;
00900    case MISDN_CTYPE_BOOL:
00901       dest->num = (int *)malloc(sizeof(int));
00902       *(dest->num) = (ast_true(value) ? 1 : 0);
00903       break;
00904    case MISDN_CTYPE_BOOLINT:
00905       dest->num = (int *)malloc(sizeof(int));
00906       if (sscanf(value, "%d", &tmp)) {
00907          memcpy(dest->num, &tmp, sizeof(int));
00908       } else {
00909          *(dest->num) = (ast_true(value) ? boolint_def : 0);
00910       }
00911       break;
00912    case MISDN_CTYPE_MSNLIST:
00913       for (valtmp = strsep(&value, ","); valtmp; valtmp = strsep(&value, ",")) {
00914          if ((len = strlen(valtmp))) {
00915             struct msn_list *ml = (struct msn_list *)malloc(sizeof(struct msn_list));
00916             ml->msn = (char *)calloc(len+1, sizeof(char));
00917             strncpy(ml->msn, valtmp, len);
00918             ml->next = dest->ml;
00919             dest->ml = ml;
00920          }
00921       }
00922       break;
00923    case MISDN_CTYPE_ASTGROUP:
00924       dest->grp = (ast_group_t *)malloc(sizeof(ast_group_t));
00925       *(dest->grp) = ast_get_group(value);
00926       break;
00927    }
00928 
00929    return re;
00930 }

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

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

00432 {
00433    int i;
00434 
00435    switch (type) {
00436    case PORT_CFG:
00437       for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
00438          if (!strcasecmp(name, port_spec[i].name))
00439             return i;
00440       }
00441       break;
00442    case GEN_CFG:
00443       for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
00444          if (!strcasecmp(name, gen_spec[i].name))
00445             return i;
00446       }
00447    }
00448 
00449    return -1;
00450 }

void misdn_cfg_destroy ( void   ) 

Definition at line 1080 of file misdn_config.c.

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

Referenced by unload_module().

01081 {
01082    misdn_cfg_lock();
01083 
01084    _free_port_cfg();
01085    _free_general_cfg();
01086 
01087    free(port_cfg);
01088    free(general_cfg);
01089    free(ptp);
01090    free(map);
01091 
01092    misdn_cfg_unlock();
01093    ast_mutex_destroy(&config_mutex);
01094 }

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

Definition at line 513 of file misdn_config.c.

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

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

00514 {
00515    int place;
00516 
00517    if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
00518       memset(buf, 0, bufsize);
00519       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Port number %d is not valid.\n", port);
00520       return;
00521    }
00522 
00523    misdn_cfg_lock();
00524    if (elem == MISDN_CFG_PTP) {
00525       if (!memcpy(buf, &ptp[port], (bufsize > ptp[port]) ? sizeof(ptp[port]) : bufsize))
00526          memset(buf, 0, bufsize);
00527    } else {
00528       if ((place = map[elem]) < 0) {
00529          memset (buf, 0, bufsize);
00530          ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Invalid element (%d) requested.\n", elem);
00531       } else {
00532          if (elem < MISDN_CFG_LAST) {
00533             switch (port_spec[place].type) {
00534             case MISDN_CTYPE_STR:
00535                if (port_cfg[port][place].str) {
00536                   if (!memccpy(buf, port_cfg[port][place].str, 0, bufsize))
00537                      memset(buf, 0, 1);
00538                } else if (port_cfg[0][place].str) {
00539                   if (!memccpy(buf, port_cfg[0][place].str, 0, bufsize))
00540                      memset(buf, 0, 1);
00541                } else
00542                   memset(buf, 0, bufsize);
00543                break;
00544             default:
00545                if (port_cfg[port][place].any)
00546                   memcpy(buf, port_cfg[port][place].any, bufsize);
00547                else if (port_cfg[0][place].any)
00548                   memcpy(buf, port_cfg[0][place].any, bufsize);
00549                else
00550                   memset(buf, 0, bufsize);
00551             }
00552          } else {
00553             switch (gen_spec[place].type) {
00554             case MISDN_CTYPE_STR:
00555                if (!general_cfg[place].str || !memccpy(buf, general_cfg[place].str, 0, bufsize))
00556                   memset(buf, 0, 1);
00557                break;
00558             default:
00559                if (general_cfg[place].any)
00560                   memcpy(buf, general_cfg[place].any, bufsize);
00561                else
00562                   memset(buf, 0, bufsize);
00563             }
00564          }
00565       }
00566    }
00567    misdn_cfg_unlock();
00568 }

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

Definition at line 744 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, and type.

Referenced by misdn_show_config().

00745 {
00746    int place;
00747    char tempbuf[BUFFERSIZE] = "";
00748    struct msn_list *iter;
00749 
00750    if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
00751       *buf = 0;
00752       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Port number %d is not valid.\n", port);
00753       return;
00754    }
00755 
00756    place = map[elem];
00757 
00758    misdn_cfg_lock();
00759    if (elem == MISDN_CFG_PTP) {
00760       snprintf(buf, bufsize, " -> ptp: %s", ptp[port] ? "yes" : "no");
00761    }
00762    else if (elem > MISDN_CFG_FIRST && elem < MISDN_CFG_LAST) {
00763       switch (port_spec[place].type) {
00764       case MISDN_CTYPE_INT:
00765       case MISDN_CTYPE_BOOLINT:
00766          if (port_cfg[port][place].num)
00767             snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[port][place].num);
00768          else if (port_cfg[0][place].num)
00769             snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[0][place].num);
00770          else
00771             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00772          break;
00773       case MISDN_CTYPE_BOOL:
00774          if (port_cfg[port][place].num)
00775             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[port][place].num ? "yes" : "no");
00776          else if (port_cfg[0][place].num)
00777             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[0][place].num ? "yes" : "no");
00778          else
00779             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00780          break;
00781       case MISDN_CTYPE_ASTGROUP:
00782          if (port_cfg[port][place].grp)
00783             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, 
00784                    ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[port][place].grp));
00785          else if (port_cfg[0][place].grp)
00786             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, 
00787                    ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[0][place].grp));
00788          else
00789             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00790          break;
00791       case MISDN_CTYPE_MSNLIST:
00792          if (port_cfg[port][place].ml)
00793             iter = port_cfg[port][place].ml;
00794          else
00795             iter = port_cfg[0][place].ml;
00796          if (iter) {
00797             for (; iter; iter = iter->next)
00798                sprintf(tempbuf, "%s%s, ", tempbuf, iter->msn);
00799             tempbuf[strlen(tempbuf)-2] = 0;
00800          }
00801          snprintf(buf, bufsize, " -> msns: %s", *tempbuf ? tempbuf : "none");
00802          break;
00803       case MISDN_CTYPE_STR:
00804          if ( port_cfg[port][place].str) {
00805             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[port][place].str);
00806          } else if (port_cfg[0][place].str) {
00807             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[0][place].str);
00808          } else {
00809             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00810          }
00811          break;
00812       }
00813    } else if (elem > MISDN_GEN_FIRST && elem < MISDN_GEN_LAST) {
00814       switch (gen_spec[place].type) {
00815       case MISDN_CTYPE_INT:
00816       case MISDN_CTYPE_BOOLINT:
00817          if (general_cfg[place].num)
00818             snprintf(buf, bufsize, " -> %s: %d", gen_spec[place].name, *general_cfg[place].num);
00819          else
00820             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00821          break;
00822       case MISDN_CTYPE_BOOL:
00823          if (general_cfg[place].num)
00824             snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, *general_cfg[place].num ? "yes" : "no");
00825          else
00826             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00827          break;
00828       case MISDN_CTYPE_STR:
00829          if ( general_cfg[place].str) {
00830             snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, general_cfg[place].str);
00831          } else {
00832             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00833          }
00834          break;
00835       default:
00836          snprintf(buf, bufsize, " -> type of %s not handled yet", gen_spec[place].name);
00837          break;
00838       }
00839    } else {
00840       *buf = 0;
00841       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Invalid config element (%d) requested.\n", elem);
00842    }
00843    misdn_cfg_unlock();
00844 }

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

Definition at line 618 of file misdn_config.c.

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

00619 {
00620    int place = map[elem];
00621    struct misdn_cfg_spec *spec = NULL;
00622 
00623    /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
00624    if (elem == MISDN_CFG_GROUPNAME) {
00625       if (!memccpy(buf, ports_description, 0, bufsize))
00626          memset(buf, 0, 1);
00627       if (buf_default && bufsize_default)
00628          memset(buf_default, 0, 1);
00629       return;
00630    }
00631 
00632    if ((elem > MISDN_CFG_FIRST) && (elem < MISDN_CFG_LAST))
00633       spec = (struct misdn_cfg_spec *)port_spec;
00634    else if ((elem > MISDN_GEN_FIRST) && (elem < MISDN_GEN_LAST))
00635       spec = (struct misdn_cfg_spec *)gen_spec;
00636       
00637    if (!spec || !spec[place].desc)
00638       memset(buf, 0, 1);
00639    else {
00640       if (!memccpy(buf, spec[place].desc, 0, bufsize))
00641          memset(buf, 0, 1);
00642       if (buf_default && bufsize) {
00643          if (!strcmp(spec[place].def, NO_DEFAULT))
00644             memset(buf_default, 0, 1);
00645          else if (!memccpy(buf_default, spec[place].def, 0, bufsize_default))
00646             memset(buf_default, 0, 1);
00647       }
00648    }
00649 }

enum misdn_cfg_elements misdn_cfg_get_elem ( char *  name  ) 

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

00571 {
00572    int pos;
00573 
00574    /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
00575    if (!strcmp(name, "ports"))
00576       return MISDN_CFG_GROUPNAME;
00577    if (!strcmp(name, "name"))
00578       return MISDN_CFG_FIRST;
00579 
00580    pos = get_cfg_position (name, PORT_CFG);
00581    if (pos >= 0)
00582       return port_spec[pos].elem;
00583    
00584    pos = get_cfg_position (name, GEN_CFG);
00585    if (pos >= 0)
00586       return gen_spec[pos].elem;
00587    
00588    return MISDN_CFG_FIRST;
00589 }

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

Definition at line 591 of file misdn_config.c.

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

00592 {
00593    struct misdn_cfg_spec *spec = NULL;
00594    int place = map[elem];
00595 
00596    /* the ptp hack */
00597    if (elem == MISDN_CFG_PTP) {
00598       memset(buf, 0, 1);
00599       return;
00600    }
00601    
00602    /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
00603    if (elem == MISDN_CFG_GROUPNAME) {
00604       if (!snprintf(buf, bufsize, "ports"))
00605          memset(buf, 0, 1);
00606       return;
00607    }
00608    
00609    if ((elem > MISDN_CFG_FIRST) && (elem < MISDN_CFG_LAST))
00610       spec = (struct misdn_cfg_spec *)port_spec;
00611    else if ((elem > MISDN_GEN_FIRST) && (elem < MISDN_GEN_LAST))
00612       spec = (struct misdn_cfg_spec *)gen_spec;
00613 
00614    if (!spec || !memccpy(buf, spec[place].name, 0, bufsize))
00615       memset(buf, 0, 1);
00616 }

int misdn_cfg_get_next_port ( int  port  ) 

Definition at line 846 of file misdn_config.c.

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

Referenced by complete_show_config(), load_module(), misdn_cfg_get_next_port_spin(), misdn_check_l2l1(), misdn_new(), misdn_request(), misdn_show_config(), misdn_show_ports_stats(), misdn_show_stacks(), and update_name().

00847 {
00848    int p = -1;
00849    int gn = map[MISDN_CFG_GROUPNAME];
00850    
00851    misdn_cfg_lock();
00852    for (port++; port <= max_ports; port++) {
00853       if (port_cfg[port][gn].str) {
00854          p = port;
00855          break;
00856       }
00857    }
00858    misdn_cfg_unlock();
00859 
00860    return p;
00861 }

int misdn_cfg_get_next_port_spin ( int  port  ) 

Definition at line 863 of file misdn_config.c.

References misdn_cfg_get_next_port().

Referenced by misdn_request().

00864 {
00865    int p = misdn_cfg_get_next_port(port);
00866    return (p > 0) ? p : misdn_cfg_get_next_port(0);
00867 }

void misdn_cfg_get_ports_string ( char *  ports  ) 

Generate a comma separated list of all active ports.

Definition at line 718 of file misdn_config.c.

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

Referenced by load_module().

00719 {
00720    char tmp[16];
00721    int l, i;
00722    int gn = map[MISDN_CFG_GROUPNAME];
00723 
00724    *ports = 0;
00725 
00726    misdn_cfg_lock();
00727    for (i = 1; i <= max_ports; i++) {
00728       if (port_cfg[i][gn].str) {
00729          if (ptp[i])
00730             sprintf(tmp, "%dptp,", i);
00731          else
00732             sprintf(tmp, "%d,", i);
00733          strcat(ports, tmp);
00734       }
00735    }
00736    misdn_cfg_unlock();
00737 
00738    if ((l = strlen(ports))) {
00739       /* Strip trailing ',' */
00740       ports[l-1] = 0;
00741    }
00742 }

int misdn_cfg_init ( int  this_max_ports  ) 

Definition at line 1096 of file misdn_config.c.

References _build_general_config(), _build_port_config(), _enum_array_map(), _fill_defaults(), _free_general_cfg(), _free_port_cfg(), ast_category_browse(), AST_DESTROY_CFG, AST_LOAD_CFG, ast_log(), ast_mutex_init(), ast_variable_browse(), calloc, config, 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().

01097 {
01098    char config[] = "misdn.conf";
01099    char *cat, *p;
01100    int i;
01101    struct ast_config *cfg;
01102    struct ast_variable *v;
01103 
01104    if (!(cfg = AST_LOAD_CFG(config))) {
01105       ast_log(LOG_WARNING, "missing file: misdn.conf\n");
01106       return -1;
01107    }
01108 
01109    ast_mutex_init(&config_mutex);
01110 
01111    misdn_cfg_lock();
01112 
01113    if (this_max_ports) {
01114       /* this is the first run */
01115       max_ports = this_max_ports;
01116       map = (int *)calloc(MISDN_GEN_LAST + 1, sizeof(int));
01117       if (_enum_array_map())
01118          return -1;
01119       p = (char *)calloc(1, (max_ports + 1) * sizeof(union misdn_cfg_pt *)
01120                      + (max_ports + 1) * NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt));
01121       port_cfg = (union misdn_cfg_pt **)p;
01122       p += (max_ports + 1) * sizeof(union misdn_cfg_pt *);
01123       for (i = 0; i <= max_ports; ++i) {
01124          port_cfg[i] = (union misdn_cfg_pt *)p;
01125          p += NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt);
01126       }
01127       general_cfg = (union misdn_cfg_pt *)calloc(1, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
01128       ptp = (int *)calloc(max_ports + 1, sizeof(int));
01129    }
01130    else {
01131       /* misdn reload */
01132       _free_port_cfg();
01133       _free_general_cfg();
01134       memset(port_cfg[0], 0, NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt) * (max_ports + 1));
01135       memset(general_cfg, 0, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
01136       memset(ptp, 0, sizeof(int) * (max_ports + 1));
01137    }
01138 
01139    cat = ast_category_browse(cfg, NULL);
01140 
01141    while(cat) {
01142       v = ast_variable_browse(cfg, cat);
01143       if (!strcasecmp(cat, "general")) {
01144          _build_general_config(v);
01145       } else {
01146          _build_port_config(v, cat);
01147       }
01148       cat = ast_category_browse(cfg, cat);
01149    }
01150 
01151    _fill_defaults();
01152 
01153    misdn_cfg_unlock();
01154    AST_DESTROY_CFG(cfg);
01155 
01156    return 0;
01157 }

int misdn_cfg_is_group_method ( char *  group,
enum misdn_cfg_method  meth 
)

Definition at line 683 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(), and misdn_cfg_pt::str.

Referenced by misdn_request().

00684 {
00685    int i, re = 0;
00686    char *method ;
00687 
00688    misdn_cfg_lock();
00689 
00690    method = port_cfg[0][map[MISDN_CFG_METHOD]].str;
00691 
00692    for (i = 1; i <= max_ports; i++) {
00693       if (port_cfg[i] && port_cfg[i][map[MISDN_CFG_GROUPNAME]].str) {
00694          if (!strcasecmp(port_cfg[i][map[MISDN_CFG_GROUPNAME]].str, group))
00695             method = (port_cfg[i][map[MISDN_CFG_METHOD]].str ? 
00696                     port_cfg[i][map[MISDN_CFG_METHOD]].str : port_cfg[0][map[MISDN_CFG_METHOD]].str);
00697       }
00698    }
00699 
00700    if (method) {
00701       switch (meth) {
00702       case METHOD_STANDARD:      re = !strcasecmp(method, "standard");
00703                            break;
00704       case METHOD_ROUND_ROBIN:   re = !strcasecmp(method, "round_robin");
00705                            break;
00706       case METHOD_STANDARD_DEC:  re = !strcasecmp(method, "standard_dec");
00707                            break;
00708       }
00709    }
00710    misdn_cfg_unlock();
00711 
00712    return re;
00713 }

int misdn_cfg_is_msn_valid ( int  port,
char *  msn 
)

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

00652 {
00653    int re = 0;
00654    struct msn_list *iter;
00655 
00656    if (!misdn_cfg_is_port_valid(port)) {
00657       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_is_msn_valid! Port number %d is not valid.\n", port);
00658       return 0;
00659    }
00660 
00661    misdn_cfg_lock();
00662    if (port_cfg[port][map[MISDN_CFG_MSNS]].ml)
00663       iter = port_cfg[port][map[MISDN_CFG_MSNS]].ml;
00664    else
00665       iter = port_cfg[0][map[MISDN_CFG_MSNS]].ml;
00666    for (; iter; iter = iter->next) 
00667       if (*(iter->msn) == '*' || ast_extension_match(iter->msn, msn)) {
00668          re = 1;
00669          break;
00670       }
00671    misdn_cfg_unlock();
00672 
00673    return re;
00674 }

int misdn_cfg_is_port_valid ( int  port  ) 

Definition at line 676 of file misdn_config.c.

References MISDN_CFG_GROUPNAME.

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

00677 {
00678    int gn = map[MISDN_CFG_GROUPNAME];
00679 
00680    return (port >= 1 && port <= max_ports && port_cfg[port][gn].str);
00681 }

static void misdn_cfg_lock ( void   )  [inline, static]

void misdn_cfg_reload ( void   ) 

Definition at line 1075 of file misdn_config.c.

References misdn_cfg_init().

Referenced by reload_config().

01076 {
01077    misdn_cfg_init (0);
01078 }

static void misdn_cfg_unlock ( void   )  [inline, static]

void misdn_cfg_update_ptp ( void   ) 

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

01007 {
01008 #ifndef MISDN_1_2
01009    char misdn_init[BUFFERSIZE];
01010    char line[BUFFERSIZE];
01011    FILE *fp;
01012    char *tok, *p, *end;
01013    int port;
01014 
01015    misdn_cfg_get(0, MISDN_GEN_MISDN_INIT, &misdn_init, sizeof(misdn_init));
01016 
01017    if (!ast_strlen_zero(misdn_init)) {
01018       fp = fopen(misdn_init, "r");
01019       if (fp) {
01020          while(fgets(line, sizeof(line), fp)) {
01021             if (!strncmp(line, "nt_ptp", 6)) {
01022                for (tok = strtok_r(line,",=", &p);
01023                    tok;
01024                    tok = strtok_r(NULL,",=", &p)) {
01025                   port = strtol(tok, &end, 10);
01026                   if (end != tok && misdn_cfg_is_port_valid(port)) {
01027                      misdn_cfg_lock();
01028                      ptp[port] = 1;
01029                      misdn_cfg_unlock();
01030                   }
01031                }
01032             }
01033          }
01034          fclose(fp);
01035       } else {
01036          ast_log(LOG_WARNING,"Couldn't open %s: %s\n", misdn_init, strerror(errno));
01037       }
01038    }
01039 #else
01040    int i;
01041    int proto;
01042    char filename[128];
01043    FILE *fp;
01044 
01045    for (i = 1; i <= max_ports; ++i) {
01046       snprintf(filename, sizeof(filename), "/sys/class/mISDN-stacks/st-%08x/protocol", i << 8);
01047       fp = fopen(filename, "r");
01048       if (!fp) {
01049          ast_log(LOG_WARNING, "Could not open %s: %s\n", filename, strerror(errno));
01050          continue;
01051       }
01052       if (fscanf(fp, "0x%08x", &proto) != 1)
01053          ast_log(LOG_WARNING, "Could not parse contents of %s!\n", filename);
01054       else
01055          ptp[i] = proto & 1<<5 ? 1 : 0;
01056       fclose(fp);
01057    }
01058 #endif
01059 }


Variable Documentation

Definition at line 387 of file misdn_config.c.

struct misdn_cfg_spec gen_spec[] [static]

Definition at line 338 of file misdn_config.c.

union misdn_cfg_pt* general_cfg [static]

Definition at line 381 of file misdn_config.c.

int* map [static]

int max_ports [static]

Definition at line 379 of file misdn_config.c.

union misdn_cfg_pt** port_cfg [static]

Definition at line 377 of file misdn_config.c.

struct misdn_cfg_spec port_spec[] [static]

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

int* ptp [static]

Definition at line 383 of file misdn_config.c.

Referenced by misdn_lib_init().


Generated on Fri Jul 3 20:38:44 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9