📄 misdn_config.c
字号:
snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[port][place].num ? "yes" : "no"); else if (port_cfg[0][place].num) snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[0][place].num ? "yes" : "no"); else snprintf(buf, bufsize, " -> %s:", port_spec[place].name); break; case MISDN_CTYPE_ASTGROUP: if (port_cfg[port][place].grp) snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[port][place].grp)); else if (port_cfg[0][place].grp) snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[0][place].grp)); else snprintf(buf, bufsize, " -> %s:", port_spec[place].name); break; case MISDN_CTYPE_MSNLIST: if (port_cfg[port][place].ml) iter = port_cfg[port][place].ml; else iter = port_cfg[0][place].ml; if (iter) { for (; iter; iter = iter->next) sprintf(tempbuf, "%s%s, ", tempbuf, iter->msn); tempbuf[strlen(tempbuf)-2] = 0; } snprintf(buf, bufsize, " -> msns: %s", *tempbuf ? tempbuf : "none"); break; case MISDN_CTYPE_STR: if ( port_cfg[port][place].str) { snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[port][place].str); } else if (port_cfg[0][place].str) { snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[0][place].str); } else { snprintf(buf, bufsize, " -> %s:", port_spec[place].name); } break; } } else if (elem > MISDN_GEN_FIRST && elem < MISDN_GEN_LAST) { switch (gen_spec[place].type) { case MISDN_CTYPE_INT: case MISDN_CTYPE_BOOLINT: if (general_cfg[place].num) snprintf(buf, bufsize, " -> %s: %d", gen_spec[place].name, *general_cfg[place].num); else snprintf(buf, bufsize, " -> %s:", gen_spec[place].name); break; case MISDN_CTYPE_BOOL: if (general_cfg[place].num) snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, *general_cfg[place].num ? "yes" : "no"); else snprintf(buf, bufsize, " -> %s:", gen_spec[place].name); break; case MISDN_CTYPE_STR: if ( general_cfg[place].str) { snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, general_cfg[place].str); } else { snprintf(buf, bufsize, " -> %s:", gen_spec[place].name); } break; default: snprintf(buf, bufsize, " -> type of %s not handled yet", gen_spec[place].name); break; } } else { *buf = 0; ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Invalid config element (%d) requested.\n", elem); } misdn_cfg_unlock();}int misdn_cfg_get_next_port (int port){ int p = -1; int gn = map[MISDN_CFG_GROUPNAME]; misdn_cfg_lock(); for (port++; port <= max_ports; port++) { if (port_cfg[port][gn].str) { p = port; break; } } misdn_cfg_unlock(); return p;}int misdn_cfg_get_next_port_spin (int port){ int p = misdn_cfg_get_next_port(port); return (p > 0) ? p : misdn_cfg_get_next_port(0);}static int _parse (union misdn_cfg_pt *dest, char *value, enum misdn_cfg_type type, int boolint_def){ int re = 0; int len, tmp; char *valtmp; switch (type) { case MISDN_CTYPE_STR: if ((len = strlen(value))) { dest->str = (char *)malloc((len + 1) * sizeof(char)); strncpy(dest->str, value, len); dest->str[len] = 0; } else { dest->str = (char *)malloc( sizeof(char)); dest->str[0] = 0; } break; case MISDN_CTYPE_INT: { char *pat; if (strchr(value,'x')) pat="%x"; else pat="%d"; if (sscanf(value, pat, &tmp)) { dest->num = (int *)malloc(sizeof(int)); memcpy(dest->num, &tmp, sizeof(int)); } else re = -1; } break; case MISDN_CTYPE_BOOL: dest->num = (int *)malloc(sizeof(int)); *(dest->num) = (ast_true(value) ? 1 : 0); break; case MISDN_CTYPE_BOOLINT: dest->num = (int *)malloc(sizeof(int)); if (sscanf(value, "%d", &tmp)) { memcpy(dest->num, &tmp, sizeof(int)); } else { *(dest->num) = (ast_true(value) ? boolint_def : 0); } break; case MISDN_CTYPE_MSNLIST: for (valtmp = strsep(&value, ","); valtmp; valtmp = strsep(&value, ",")) { if ((len = strlen(valtmp))) { struct msn_list *ml = (struct msn_list *)malloc(sizeof(struct msn_list)); ml->msn = (char *)calloc(len+1, sizeof(char)); strncpy(ml->msn, valtmp, len); ml->next = dest->ml; dest->ml = ml; } } break; case MISDN_CTYPE_ASTGROUP: dest->grp = (ast_group_t *)malloc(sizeof(ast_group_t)); *(dest->grp) = ast_get_group(value); break; } return re;}static void _build_general_config (struct ast_variable *v){ int pos; for (; v; v = v->next) { if (((pos = get_cfg_position(v->name, GEN_CFG)) < 0) || (_parse(&general_cfg[pos], v->value, gen_spec[pos].type, gen_spec[pos].boolint_def) < 0)) CLI_ERROR(v->name, v->value, "general"); }}static void _build_port_config (struct ast_variable *v, char *cat){ int pos, i; union misdn_cfg_pt cfg_tmp[NUM_PORT_ELEMENTS]; int cfg_for_ports[max_ports + 1]; if (!v || !cat) return; memset(cfg_tmp, 0, sizeof(cfg_tmp)); memset(cfg_for_ports, 0, sizeof(cfg_for_ports)); if (!strcasecmp(cat, "default")) { cfg_for_ports[0] = 1; } if (((pos = get_cfg_position("name", PORT_CFG)) < 0) || (_parse(&cfg_tmp[pos], cat, port_spec[pos].type, port_spec[pos].boolint_def) < 0)) { CLI_ERROR(v->name, v->value, cat); return; } for (; v; v = v->next) { if (!strcasecmp(v->name, "ports")) { char *token; char ptpbuf[BUFFERSIZE] = ""; int start, end; for (token = strsep(&v->value, ","); token; token = strsep(&v->value, ","), *ptpbuf = 0) { if (!*token) continue; if (sscanf(token, "%d-%d%s", &start, &end, ptpbuf) >= 2) { for (; start <= end; start++) { if (start <= max_ports && start > 0) { cfg_for_ports[start] = 1; ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0; } else CLI_ERROR(v->name, v->value, cat); } } else { if (sscanf(token, "%d%s", &start, ptpbuf)) { if (start <= max_ports && start > 0) { cfg_for_ports[start] = 1; ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0; } else CLI_ERROR(v->name, v->value, cat); } else CLI_ERROR(v->name, v->value, cat); } } } else { if (((pos = get_cfg_position(v->name, PORT_CFG)) < 0) || (_parse(&cfg_tmp[pos], v->value, port_spec[pos].type, port_spec[pos].boolint_def) < 0)) CLI_ERROR(v->name, v->value, cat); } } for (i = 0; i < (max_ports + 1); ++i) { if (cfg_for_ports[i]) { memcpy(port_cfg[i], cfg_tmp, sizeof(cfg_tmp)); } }}void misdn_cfg_update_ptp (void){#ifndef MISDN_1_2 char misdn_init[BUFFERSIZE]; char line[BUFFERSIZE]; FILE *fp; char *tok, *p, *end; int port; misdn_cfg_get(0, MISDN_GEN_MISDN_INIT, &misdn_init, sizeof(misdn_init)); if (!ast_strlen_zero(misdn_init)) { fp = fopen(misdn_init, "r"); if (fp) { while(fgets(line, sizeof(line), fp)) { if (!strncmp(line, "nt_ptp", 6)) { for (tok = strtok_r(line,",=", &p); tok; tok = strtok_r(NULL,",=", &p)) { port = strtol(tok, &end, 10); if (end != tok && misdn_cfg_is_port_valid(port)) { misdn_cfg_lock(); ptp[port] = 1; misdn_cfg_unlock(); } } } } fclose(fp); } else { ast_log(LOG_WARNING,"Couldn't open %s: %s\n", misdn_init, strerror(errno)); } }#else int i; int proto; char filename[128]; FILE *fp; for (i = 1; i <= max_ports; ++i) { snprintf(filename, sizeof(filename), "/sys/class/mISDN-stacks/st-%08x/protocol", i << 8); fp = fopen(filename, "r"); if (!fp) { ast_log(LOG_WARNING, "Could not open %s: %s\n", filename, strerror(errno)); continue; } if (fscanf(fp, "0x%08x", &proto) != 1) ast_log(LOG_WARNING, "Could not parse contents of %s!\n", filename); else ptp[i] = proto & 1<<5 ? 1 : 0; fclose(fp); }#endif}static void _fill_defaults (void){ int i; for (i = 0; i < NUM_PORT_ELEMENTS; ++i) { if (!port_cfg[0][i].any && strcasecmp(port_spec[i].def, NO_DEFAULT)) _parse(&(port_cfg[0][i]), (char *)port_spec[i].def, port_spec[i].type, port_spec[i].boolint_def); } for (i = 0; i < NUM_GEN_ELEMENTS; ++i) { if (!general_cfg[i].any && strcasecmp(gen_spec[i].def, NO_DEFAULT)) _parse(&(general_cfg[i]), (char *)gen_spec[i].def, gen_spec[i].type, gen_spec[i].boolint_def); }}void misdn_cfg_reload (void){ misdn_cfg_init (0);}void misdn_cfg_destroy (void){ misdn_cfg_lock(); _free_port_cfg(); _free_general_cfg(); free(port_cfg); free(general_cfg); free(ptp); free(map); misdn_cfg_unlock(); ast_mutex_destroy(&config_mutex);}int misdn_cfg_init (int this_max_ports){ char config[] = "misdn.conf"; char *cat, *p; int i; struct ast_config *cfg; struct ast_variable *v; if (!(cfg = AST_LOAD_CFG(config))) { ast_log(LOG_WARNING, "missing file: misdn.conf\n"); return -1; } ast_mutex_init(&config_mutex); misdn_cfg_lock(); if (this_max_ports) { /* this is the first run */ max_ports = this_max_ports; map = (int *)calloc(MISDN_GEN_LAST + 1, sizeof(int)); if (_enum_array_map()) return -1; p = (char *)calloc(1, (max_ports + 1) * sizeof(union misdn_cfg_pt *) + (max_ports + 1) * NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt)); port_cfg = (union misdn_cfg_pt **)p; p += (max_ports + 1) * sizeof(union misdn_cfg_pt *); for (i = 0; i <= max_ports; ++i) { port_cfg[i] = (union misdn_cfg_pt *)p; p += NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt); } general_cfg = (union misdn_cfg_pt *)calloc(1, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS); ptp = (int *)calloc(max_ports + 1, sizeof(int)); } else { /* misdn reload */ _free_port_cfg(); _free_general_cfg(); memset(port_cfg[0], 0, NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt) * (max_ports + 1)); memset(general_cfg, 0, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS); memset(ptp, 0, sizeof(int) * (max_ports + 1)); } cat = ast_category_browse(cfg, NULL); while(cat) { v = ast_variable_browse(cfg, cat); if (!strcasecmp(cat, "general")) { _build_general_config(v); } else { _build_port_config(v, cat); } cat = ast_category_browse(cfg, cat); } _fill_defaults(); misdn_cfg_unlock(); AST_DESTROY_CFG(cfg); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -