misdn_config.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005, Christian Richter
  5. *
  6. * Christian Richter <crich@beronet.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. *
  18. */
  19. /*!
  20. * \file
  21. *
  22. * \brief chan_misdn configuration management
  23. * \author Christian Richter <crich@beronet.com>
  24. *
  25. * \ingroup channel_drivers
  26. */
  27. /*** MODULEINFO
  28. <support_level>extended</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "chan_misdn_config.h"
  33. #include "asterisk/config.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/lock.h"
  36. #include "asterisk/pbx.h"
  37. #include "asterisk/strings.h"
  38. #include "asterisk/utils.h"
  39. #define NO_DEFAULT "<>"
  40. #define NONE 0
  41. #define GEN_CFG 1
  42. #define PORT_CFG 2
  43. #define NUM_GEN_ELEMENTS (sizeof(gen_spec) / sizeof(struct misdn_cfg_spec))
  44. #define NUM_PORT_ELEMENTS (sizeof(port_spec) / sizeof(struct misdn_cfg_spec))
  45. /*! Global jitterbuffer configuration - by default, jb is disabled
  46. * \note Values shown here match the defaults shown in misdn.conf.sample */
  47. static struct ast_jb_conf default_jbconf =
  48. {
  49. .flags = 0,
  50. .max_size = 200,
  51. .resync_threshold = 1000,
  52. .impl = "fixed",
  53. .target_extra = 40,
  54. };
  55. static struct ast_jb_conf global_jbconf;
  56. enum misdn_cfg_type {
  57. MISDN_CTYPE_STR,
  58. MISDN_CTYPE_INT,
  59. MISDN_CTYPE_BOOL,
  60. MISDN_CTYPE_BOOLINT,
  61. MISDN_CTYPE_MSNLIST,
  62. MISDN_CTYPE_ASTGROUP,
  63. MISDN_CTYPE_ASTNAMEDGROUP
  64. };
  65. struct msn_list {
  66. char *msn;
  67. struct msn_list *next;
  68. };
  69. union misdn_cfg_pt {
  70. char *str;
  71. int *num;
  72. struct msn_list *ml;
  73. ast_group_t *grp;
  74. struct ast_namedgroups *namgrp;
  75. void *any;
  76. };
  77. struct misdn_cfg_spec {
  78. char name[BUFFERSIZE];
  79. enum misdn_cfg_elements elem;
  80. enum misdn_cfg_type type;
  81. char def[BUFFERSIZE];
  82. int boolint_def;
  83. char desc[BUFFERSIZE];
  84. };
  85. static const char ports_description[] =
  86. "Define your ports, e.g. 1,2 (depends on mISDN-driver loading order).";
  87. static const struct misdn_cfg_spec port_spec[] = {
  88. { "name", MISDN_CFG_GROUPNAME, MISDN_CTYPE_STR, "default", NONE,
  89. "Name of the portgroup." },
  90. { "allowed_bearers", MISDN_CFG_ALLOWED_BEARERS, MISDN_CTYPE_STR, "all", NONE,
  91. "Here you can list which bearer capabilities should be allowed:\n"
  92. "\t all - allow any bearer capability\n"
  93. "\t speech - allow speech\n"
  94. "\t 3_1khz - allow 3.1KHz audio\n"
  95. "\t digital_unrestricted - allow unrestricted digital\n"
  96. "\t digital_restricted - allow restricted digital\n"
  97. "\t video - allow video" },
  98. { "rxgain", MISDN_CFG_RXGAIN, MISDN_CTYPE_INT, "0", NONE,
  99. "Set this between -8 and 8 to change the RX Gain." },
  100. { "txgain", MISDN_CFG_TXGAIN, MISDN_CTYPE_INT, "0", NONE,
  101. "Set this between -8 and 8 to change the TX Gain." },
  102. { "te_choose_channel", MISDN_CFG_TE_CHOOSE_CHANNEL, MISDN_CTYPE_BOOL, "no", NONE,
  103. "Some telcos especially in NL seem to need this set to yes,\n"
  104. "\talso in Switzerland this seems to be important." },
  105. { "far_alerting", MISDN_CFG_FAR_ALERTING, MISDN_CTYPE_BOOL, "no", NONE,
  106. "If we should generate ringing for chan_sip and others." },
  107. { "pmp_l1_check", MISDN_CFG_PMP_L1_CHECK, MISDN_CTYPE_BOOL, "no", NONE,
  108. "This option defines, if chan_misdn should check the L1 on a PMP\n"
  109. "\tbefore making a group call on it. The L1 may go down for PMP Ports\n"
  110. "\tso we might need this.\n"
  111. "\tBut be aware! a broken or plugged off cable might be used for a group call\n"
  112. "\tas well, since chan_misdn has no chance to distinguish if the L1 is down\n"
  113. "\tbecause of a lost Link or because the Provider shut it down..." },
  114. { "block_on_alarm", MISDN_CFG_ALARM_BLOCK, MISDN_CTYPE_BOOL, "no", NONE ,
  115. "Block this port if we have an alarm on it." },
  116. { "hdlc", MISDN_CFG_HDLC, MISDN_CTYPE_BOOL, "no", NONE,
  117. "Set this to yes, if you want to bridge a mISDN data channel to\n"
  118. "\tanother channel type or to an application." },
  119. { "context", MISDN_CFG_CONTEXT, MISDN_CTYPE_STR, "default", NONE,
  120. "Context to use for incoming calls." },
  121. { "language", MISDN_CFG_LANGUAGE, MISDN_CTYPE_STR, "en", NONE,
  122. "Language." },
  123. { "musicclass", MISDN_CFG_MUSICCLASS, MISDN_CTYPE_STR, "default", NONE,
  124. "Sets the musiconhold class." },
  125. { "callerid", MISDN_CFG_CALLERID, MISDN_CTYPE_STR, "", NONE,
  126. "Set the outgoing caller id to the value." },
  127. { "incoming_cid_tag", MISDN_CFG_INCOMING_CALLERID_TAG, MISDN_CTYPE_STR, "", NONE,
  128. "Set the incoming caller id string tag to the value." },
  129. { "append_msn_to_cid_tag", MISDN_CFG_APPEND_MSN_TO_CALLERID_TAG, MISDN_CTYPE_BOOL, "no", NONE,
  130. "Automatically appends incoming or outgoing MSN to the incoming caller\n"
  131. "\tid string tag. An underscore '_' is used as delimiter. Incoming calls\n"
  132. "\twill have the dialed number appended, and outgoing calls will have the\n"
  133. "\tcaller number appended to the tag." },
  134. { "method", MISDN_CFG_METHOD, MISDN_CTYPE_STR, "standard", NONE,
  135. "Set the method to use for channel selection:\n"
  136. "\t standard - Use the first free channel starting from the lowest number.\n"
  137. "\t standard_dec - Use the first free channel starting from the highest number.\n"
  138. "\t round_robin - Use the round robin algorithm to select a channel. Use this\n"
  139. "\t if you want to balance your load." },
  140. { "dialplan", MISDN_CFG_DIALPLAN, MISDN_CTYPE_INT, "0", NONE,
  141. "Dialplan means Type Of Number in ISDN Terms\n"
  142. "\tThere are different types of the dialplan:\n"
  143. "\n"
  144. "\tdialplan -> for outgoing call's dialed number\n"
  145. "\tlocaldialplan -> for outgoing call's callerid\n"
  146. "\t (if -1 is set use the value from the asterisk channel)\n"
  147. "\tcpndialplan -> for incoming call's connected party number sent to caller\n"
  148. "\t (if -1 is set use the value from the asterisk channel)\n"
  149. "\n"
  150. "\tdialplan options:\n"
  151. "\n"
  152. "\t0 - unknown\n"
  153. "\t1 - International\n"
  154. "\t2 - National\n"
  155. "\t4 - Subscriber" },
  156. { "localdialplan", MISDN_CFG_LOCALDIALPLAN, MISDN_CTYPE_INT, "0", NONE,
  157. "Dialplan means Type Of Number in ISDN Terms\n"
  158. "\tThere are different types of the dialplan:\n"
  159. "\n"
  160. "\tdialplan -> for outgoing call's dialed number\n"
  161. "\tlocaldialplan -> for outgoing call's callerid\n"
  162. "\t (if -1 is set use the value from the asterisk channel)\n"
  163. "\tcpndialplan -> for incoming call's connected party number sent to caller\n"
  164. "\t (if -1 is set use the value from the asterisk channel)\n"
  165. "\n"
  166. "\tdialplan options:\n"
  167. "\n"
  168. "\t0 - unknown\n"
  169. "\t1 - International\n"
  170. "\t2 - National\n"
  171. "\t4 - Subscriber" },
  172. { "cpndialplan", MISDN_CFG_CPNDIALPLAN, MISDN_CTYPE_INT, "0", NONE,
  173. "Dialplan means Type Of Number in ISDN Terms\n"
  174. "\tThere are different types of the dialplan:\n"
  175. "\n"
  176. "\tdialplan -> for outgoing call's dialed number\n"
  177. "\tlocaldialplan -> for outgoing call's callerid\n"
  178. "\t (if -1 is set use the value from the asterisk channel)\n"
  179. "\tcpndialplan -> for incoming call's connected party number sent to caller\n"
  180. "\t (if -1 is set use the value from the asterisk channel)\n"
  181. "\n"
  182. "\tdialplan options:\n"
  183. "\n"
  184. "\t0 - unknown\n"
  185. "\t1 - International\n"
  186. "\t2 - National\n"
  187. "\t4 - Subscriber" },
  188. { "unknownprefix", MISDN_CFG_TON_PREFIX_UNKNOWN, MISDN_CTYPE_STR, "", NONE,
  189. "Prefix for unknown numbers, this is put before an incoming number\n"
  190. "\tif its type-of-number is unknown." },
  191. { "internationalprefix", MISDN_CFG_TON_PREFIX_INTERNATIONAL, MISDN_CTYPE_STR, "00", NONE,
  192. "Prefix for international numbers, this is put before an incoming number\n"
  193. "\tif its type-of-number is international." },
  194. { "nationalprefix", MISDN_CFG_TON_PREFIX_NATIONAL, MISDN_CTYPE_STR, "0", NONE,
  195. "Prefix for national numbers, this is put before an incoming number\n"
  196. "\tif its type-of-number is national." },
  197. { "netspecificprefix", MISDN_CFG_TON_PREFIX_NETWORK_SPECIFIC, MISDN_CTYPE_STR, "", NONE,
  198. "Prefix for network-specific numbers, this is put before an incoming number\n"
  199. "\tif its type-of-number is network-specific." },
  200. { "subscriberprefix", MISDN_CFG_TON_PREFIX_SUBSCRIBER, MISDN_CTYPE_STR, "", NONE,
  201. "Prefix for subscriber numbers, this is put before an incoming number\n"
  202. "\tif its type-of-number is subscriber." },
  203. { "abbreviatedprefix", MISDN_CFG_TON_PREFIX_ABBREVIATED, MISDN_CTYPE_STR, "", NONE,
  204. "Prefix for abbreviated numbers, this is put before an incoming number\n"
  205. "\tif its type-of-number is abbreviated." },
  206. { "presentation", MISDN_CFG_PRES, MISDN_CTYPE_INT, "-1", NONE,
  207. "These (presentation and screen) are the exact isdn screening and presentation\n"
  208. "\tindicators.\n"
  209. "\tIf -1 is given for either value, the presentation indicators are used from\n"
  210. "\tAsterisk's CALLERPRES function.\n"
  211. "\n"
  212. "\tscreen=0, presentation=0 -> callerid presented\n"
  213. "\tscreen=1, presentation=1 -> callerid restricted (the remote end doesn't see it!)" },
  214. { "screen", MISDN_CFG_SCREEN, MISDN_CTYPE_INT, "-1", NONE,
  215. "These (presentation and screen) are the exact isdn screening and presentation\n"
  216. "\tindicators.\n"
  217. "\tIf -1 is given for either value, the presentation indicators are used from\n"
  218. "\tAsterisk's CALLERPRES function.\n"
  219. "\n"
  220. "\tscreen=0, presentation=0 -> callerid presented\n"
  221. "\tscreen=1, presentation=1 -> callerid restricted (the remote end doesn't see it!)" },
  222. { "outgoing_colp", MISDN_CFG_OUTGOING_COLP, MISDN_CTYPE_INT, "0", NONE,
  223. "Select what to do with outgoing COLP information on this port.\n"
  224. "\n"
  225. "\t0 - Send out COLP information unaltered.\n"
  226. "\t1 - Force COLP to restricted on all outgoing COLP information.\n"
  227. "\t2 - Do not send COLP information." },
  228. { "display_connected", MISDN_CFG_DISPLAY_CONNECTED, MISDN_CTYPE_INT, "0", NONE,
  229. "Put a display ie in the CONNECT message containing the following\n"
  230. "\tinformation if it is available (nt port only):\n"
  231. "\n"
  232. "\t0 - Do not put the connected line information in the display ie.\n"
  233. "\t1 - Put the available connected line name in the display ie.\n"
  234. "\t2 - Put the available connected line number in the display ie.\n"
  235. "\t3 - Put the available connected line name and number in the display ie." },
  236. { "display_setup", MISDN_CFG_DISPLAY_SETUP, MISDN_CTYPE_INT, "0", NONE,
  237. "Put a display ie in the SETUP message containing the following\n"
  238. "\tinformation if it is available (nt port only):\n"
  239. "\n"
  240. "\t0 - Do not put the caller information in the display ie.\n"
  241. "\t1 - Put the available caller name in the display ie.\n"
  242. "\t2 - Put the available caller number in the display ie.\n"
  243. "\t3 - Put the available caller name and number in the display ie." },
  244. { "always_immediate", MISDN_CFG_ALWAYS_IMMEDIATE, MISDN_CTYPE_BOOL, "no", NONE,
  245. "Enable this to get into the s dialplan-extension.\n"
  246. "\tThere you can use DigitTimeout if you can't or don't want to use\n"
  247. "\tisdn overlap dial.\n"
  248. "\tNOTE: This will jump into the s extension for every exten!" },
  249. { "nodialtone", MISDN_CFG_NODIALTONE, MISDN_CTYPE_BOOL, "no", NONE,
  250. "Enable this to prevent chan_misdn to generate the dialtone\n"
  251. "\tThis makes only sense together with the always_immediate=yes option\n"
  252. "\tto generate your own dialtone with Playtones or so." },
  253. { "immediate", MISDN_CFG_IMMEDIATE, MISDN_CTYPE_BOOL, "no", NONE,
  254. "Enable this if you want callers which called exactly the base\n"
  255. "\tnumber (so no extension is set) to jump into the s extension.\n"
  256. "\tIf the user dials something more, it jumps to the correct extension\n"
  257. "\tinstead." },
  258. { "senddtmf", MISDN_CFG_SENDDTMF, MISDN_CTYPE_BOOL, "no", NONE,
  259. "Enable this if we should produce DTMF Tones ourselves." },
  260. { "astdtmf", MISDN_CFG_ASTDTMF, MISDN_CTYPE_BOOL, "no", NONE,
  261. "Enable this if you want to use the Asterisk dtmf detector\n"
  262. "instead of the mISDN_dsp/hfcmulti one."
  263. },
  264. { "hold_allowed", MISDN_CFG_HOLD_ALLOWED, MISDN_CTYPE_BOOL, "no", NONE,
  265. "Enable this to have support for hold and retrieve." },
  266. { "early_bconnect", MISDN_CFG_EARLY_BCONNECT, MISDN_CTYPE_BOOL, "yes", NONE,
  267. "Disable this if you don't mind correct handling of Progress Indicators." },
  268. { "incoming_early_audio", MISDN_CFG_INCOMING_EARLY_AUDIO, MISDN_CTYPE_BOOL, "no", NONE,
  269. "Turn this on if you like to send Tone Indications to a Incoming\n"
  270. "\tisdn channel on a TE Port. Rarely used, only if the Telco allows\n"
  271. "\tyou to send indications by yourself, normally the Telco sends the\n"
  272. "\tindications to the remote party." },
  273. { "echocancel", MISDN_CFG_ECHOCANCEL, MISDN_CTYPE_BOOLINT, "0", 128,
  274. "This enables echo cancellation with the given number of taps.\n"
  275. "\tBe aware: Move this setting only to outgoing portgroups!\n"
  276. "\tA value of zero turns echo cancellation off.\n"
  277. "\n"
  278. "\tPossible values are: 0,32,64,128,256,yes(=128),no(=0)" },
  279. #ifdef MISDN_1_2
  280. { "pipeline", MISDN_CFG_PIPELINE, MISDN_CTYPE_STR, NO_DEFAULT, NONE,
  281. "Set the configuration string for the mISDN dsp pipeline.\n"
  282. "\n"
  283. "\tExample for enabling the mg2 echo cancellation module with deftaps\n"
  284. "\tset to 128:\n"
  285. "\t\tmg2ec(deftaps=128)" },
  286. #endif
  287. #ifdef WITH_BEROEC
  288. { "bnechocancel", MISDN_CFG_BNECHOCANCEL, MISDN_CTYPE_BOOLINT, "yes", 64,
  289. "echotail in ms (1-200)" },
  290. { "bnec_antihowl", MISDN_CFG_BNEC_ANTIHOWL, MISDN_CTYPE_INT, "0", NONE,
  291. "Use antihowl" },
  292. { "bnec_nlp", MISDN_CFG_BNEC_NLP, MISDN_CTYPE_BOOL, "yes", NONE,
  293. "Nonlinear Processing (much faster adaption)" },
  294. { "bnec_zerocoeff", MISDN_CFG_BNEC_ZEROCOEFF, MISDN_CTYPE_BOOL, "no", NONE,
  295. "ZeroCoeffeciens" },
  296. { "bnec_tonedisabler", MISDN_CFG_BNEC_TD, MISDN_CTYPE_BOOL, "no", NONE,
  297. "Disable Tone" },
  298. { "bnec_adaption", MISDN_CFG_BNEC_ADAPT, MISDN_CTYPE_INT, "1", NONE,
  299. "Adaption mode (0=no,1=full,2=fast)" },
  300. #endif
  301. { "need_more_infos", MISDN_CFG_NEED_MORE_INFOS, MISDN_CTYPE_BOOL, "0", NONE,
  302. "Send Setup_Acknowledge on incoming calls anyway (instead of PROCEEDING),\n"
  303. "\tthis requests additional Infos, so we can waitfordigits without much\n"
  304. "\tissues. This works only for PTP Ports" },
  305. { "noautorespond_on_setup", MISDN_CFG_NOAUTORESPOND_ON_SETUP, MISDN_CTYPE_BOOL, "0", NONE,
  306. "Do not send SETUP_ACKNOWLEDGE or PROCEEDING automatically to the calling Party.\n"
  307. "Instead we directly jump into the dialplan. This might be useful for fast call\n"
  308. "rejection, or for some broken switches, that need hangup causes like busy in the.\n"
  309. "RELEASE_COMPLETE Message, instead of the DISCONNECT Message."},
  310. { "jitterbuffer", MISDN_CFG_JITTERBUFFER, MISDN_CTYPE_INT, "4000", NONE,
  311. "The jitterbuffer." },
  312. { "jitterbuffer_upper_threshold", MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD, MISDN_CTYPE_INT, "0", NONE,
  313. "Change this threshold to enable dejitter functionality." },
  314. { "callgroup", MISDN_CFG_CALLGROUP, MISDN_CTYPE_ASTGROUP, NO_DEFAULT, NONE,
  315. "Callgroup." },
  316. { "pickupgroup", MISDN_CFG_PICKUPGROUP, MISDN_CTYPE_ASTGROUP, NO_DEFAULT, NONE,
  317. "Pickupgroup." },
  318. { "namedcallgroup", MISDN_CFG_NAMEDCALLGROUP, MISDN_CTYPE_ASTNAMEDGROUP, NO_DEFAULT, NONE,
  319. "Named callgroup." },
  320. { "namedpickupgroup", MISDN_CFG_NAMEDPICKUPGROUP, MISDN_CTYPE_ASTNAMEDGROUP, NO_DEFAULT, NONE,
  321. "Named pickupgroup." },
  322. { "max_incoming", MISDN_CFG_MAX_IN, MISDN_CTYPE_INT, "-1", NONE,
  323. "Defines the maximum amount of incoming calls per port for this group.\n"
  324. "\tCalls which exceed the maximum will be marked with the channel variable\n"
  325. "\tMAX_OVERFLOW. It will contain the amount of overflowed calls" },
  326. { "max_outgoing", MISDN_CFG_MAX_OUT, MISDN_CTYPE_INT, "-1", NONE,
  327. "Defines the maximum amount of outgoing calls per port for this group\n"
  328. "\texceeding calls will be rejected" },
  329. { "reject_cause", MISDN_CFG_REJECT_CAUSE, MISDN_CTYPE_INT, "21", NONE,
  330. "Defines the cause with which a 3. call is rejected on PTMP BRI."},
  331. { "faxdetect", MISDN_CFG_FAXDETECT, MISDN_CTYPE_STR, "no", NONE,
  332. "Setup fax detection:\n"
  333. "\t no - no fax detection\n"
  334. "\t incoming - fax detection for incoming calls\n"
  335. "\t outgoing - fax detection for outgoing calls\n"
  336. "\t both - fax detection for incoming and outgoing calls\n"
  337. "\tAdd +nojump to your value (i.e. faxdetect=both+nojump) if you don't want to jump into the\n"
  338. "\tfax-extension but still want to detect the fax and prepare the channel for fax transfer." },
  339. { "faxdetect_timeout", MISDN_CFG_FAXDETECT_TIMEOUT, MISDN_CTYPE_INT, "5", NONE,
  340. "Number of seconds the fax detection should do its job. After the given period of time,\n"
  341. "\twe assume that it's not a fax call and save some CPU time by turning off fax detection.\n"
  342. "\tSet this to 0 if you don't want a timeout (never stop detecting)." },
  343. { "faxdetect_context", MISDN_CFG_FAXDETECT_CONTEXT, MISDN_CTYPE_STR, NO_DEFAULT, NONE,
  344. "Context to jump into if we detect a fax. Don't set this if you want to stay in the current context." },
  345. { "l1watcher_timeout", MISDN_CFG_L1_TIMEOUT, MISDN_CTYPE_BOOLINT, "0", 4,
  346. "Monitors L1 of the port. If L1 is down it tries\n"
  347. "\tto bring it up. The polling timeout is given in seconds.\n"
  348. "\tSetting the value to 0 disables monitoring L1 of the port.\n"
  349. "\n"
  350. "\tThis option is only read at chan_misdn loading time.\n"
  351. "\tYou need to unload and load chan_misdn to change the\n"
  352. "\tvalue. An asterisk restart will also do the trick." },
  353. { "overlapdial", MISDN_CFG_OVERLAP_DIAL, MISDN_CTYPE_BOOLINT, "0", 4,
  354. "Enables overlap dial for the given amount of seconds.\n"
  355. "\tPossible values are positive integers or:\n"
  356. "\t yes (= 4 seconds)\n"
  357. "\t no (= 0 seconds = disabled)" },
  358. { "nttimeout", MISDN_CFG_NTTIMEOUT, MISDN_CTYPE_BOOL, "no", NONE ,
  359. "Set this to yes if you want calls disconnected in overlap mode\n"
  360. "\twhen a timeout happens." },
  361. { "bridging", MISDN_CFG_BRIDGING, MISDN_CTYPE_BOOL, "yes", NONE,
  362. "Set this to yes/no, default is yes.\n"
  363. "This can be used to have bridging enabled in general and to\n"
  364. "disable it for specific ports. It makes sense to disable\n"
  365. "bridging on NT Port where you plan to use the HOLD/RETRIEVE\n"
  366. "features with ISDN phones." },
  367. { "msns", MISDN_CFG_MSNS, MISDN_CTYPE_MSNLIST, "*", NONE,
  368. "MSN's for TE ports, listen on those numbers on the above ports, and\n"
  369. "\tindicate the incoming calls to Asterisk.\n"
  370. "\tHere you can give a comma separated list, or simply an '*' for any msn." },
  371. { "cc_request_retention", MISDN_CFG_CC_REQUEST_RETENTION, MISDN_CTYPE_BOOL, "yes", NONE,
  372. "Enable/Disable call-completion request retention support (ptp)." },
  373. };
  374. static const struct misdn_cfg_spec gen_spec[] = {
  375. { "debug", MISDN_GEN_DEBUG, MISDN_CTYPE_INT, "0", NONE,
  376. "Sets the debugging flag:\n"
  377. "\t0 - No Debug\n"
  378. "\t1 - mISDN Messages and * - Messages, and * - State changes\n"
  379. "\t2 - Messages + Message specific Informations (e.g. bearer capability)\n"
  380. "\t3 - very Verbose, the above + lots of Driver specific infos\n"
  381. "\t4 - even more Verbose than 3" },
  382. #ifndef MISDN_1_2
  383. { "misdn_init", MISDN_GEN_MISDN_INIT, MISDN_CTYPE_STR, "/etc/misdn-init.conf", NONE,
  384. "Set the path to the misdn-init.conf (for nt_ptp mode checking)." },
  385. #endif
  386. { "tracefile", MISDN_GEN_TRACEFILE, MISDN_CTYPE_STR, "/var/log/asterisk/misdn.log", NONE,
  387. "Set the path to the massively growing trace file, if you want that." },
  388. { "bridging", MISDN_GEN_BRIDGING, MISDN_CTYPE_BOOL, "yes", NONE,
  389. "Set this to yes if you want mISDN_dsp to bridge the calls in HW." },
  390. { "stop_tone_after_first_digit", MISDN_GEN_STOP_TONE, MISDN_CTYPE_BOOL, "yes", NONE,
  391. "Stops dialtone after getting first digit on NT Port." },
  392. { "append_digits2exten", MISDN_GEN_APPEND_DIGITS2EXTEN, MISDN_CTYPE_BOOL, "yes", NONE,
  393. "Whether to append overlapdialed Digits to Extension or not." },
  394. { "dynamic_crypt", MISDN_GEN_DYNAMIC_CRYPT, MISDN_CTYPE_BOOL, "no", NONE,
  395. "Whether to look out for dynamic crypting attempts." },
  396. { "crypt_prefix", MISDN_GEN_CRYPT_PREFIX, MISDN_CTYPE_STR, NO_DEFAULT, NONE,
  397. "What is used for crypting Protocol." },
  398. { "crypt_keys", MISDN_GEN_CRYPT_KEYS, MISDN_CTYPE_STR, NO_DEFAULT, NONE,
  399. "Keys for cryption, you reference them in the dialplan\n"
  400. "\tLater also in dynamic encr." },
  401. { "ntkeepcalls", MISDN_GEN_NTKEEPCALLS, MISDN_CTYPE_BOOL, "no", NONE,
  402. "avoid dropping calls if the L2 goes down. some Nortel pbx\n"
  403. "do put down the L2/L1 for some milliseconds even if there\n"
  404. "are running calls. with this option you can avoid dropping them" },
  405. { "ntdebugflags", MISDN_GEN_NTDEBUGFLAGS, MISDN_CTYPE_INT, "0", NONE,
  406. "No description yet."},
  407. { "ntdebugfile", MISDN_GEN_NTDEBUGFILE, MISDN_CTYPE_STR, "/var/log/misdn-nt.log", NONE,
  408. "No description yet." }
  409. };
  410. /* array of port configs, default is at position 0. */
  411. static union misdn_cfg_pt **port_cfg;
  412. /* max number of available ports, is set on init */
  413. static int max_ports;
  414. /* general config */
  415. static union misdn_cfg_pt *general_cfg;
  416. /* storing the ptp flag separated to save memory */
  417. static int *ptp;
  418. /* maps enum config elements to array positions */
  419. static int *map;
  420. static ast_mutex_t config_mutex;
  421. #define CLI_ERROR(name, value, section) ({ \
  422. ast_log(LOG_WARNING, "misdn.conf: \"%s=%s\" (section: %s) invalid or out of range. " \
  423. "Please edit your misdn.conf and then do a \"misdn reload\".\n", name, value, section); \
  424. })
  425. static int _enum_array_map (void)
  426. {
  427. int i, j, ok;
  428. for (i = MISDN_CFG_FIRST + 1; i < MISDN_CFG_LAST; ++i) {
  429. if (i == MISDN_CFG_PTP)
  430. continue;
  431. ok = 0;
  432. for (j = 0; j < NUM_PORT_ELEMENTS; ++j) {
  433. if (port_spec[j].elem == i) {
  434. map[i] = j;
  435. ok = 1;
  436. break;
  437. }
  438. }
  439. if (!ok) {
  440. ast_log(LOG_WARNING, "Enum element %d in misdn_cfg_elements (port section) has no corresponding element in the config struct!\n", i);
  441. return -1;
  442. }
  443. }
  444. for (i = MISDN_GEN_FIRST + 1; i < MISDN_GEN_LAST; ++i) {
  445. ok = 0;
  446. for (j = 0; j < NUM_GEN_ELEMENTS; ++j) {
  447. if (gen_spec[j].elem == i) {
  448. map[i] = j;
  449. ok = 1;
  450. break;
  451. }
  452. }
  453. if (!ok) {
  454. ast_log(LOG_WARNING, "Enum element %d in misdn_cfg_elements (general section) has no corresponding element in the config struct!\n", i);
  455. return -1;
  456. }
  457. }
  458. return 0;
  459. }
  460. static int get_cfg_position (const char *name, int type)
  461. {
  462. int i;
  463. switch (type) {
  464. case PORT_CFG:
  465. for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
  466. if (!strcasecmp(name, port_spec[i].name))
  467. return i;
  468. }
  469. break;
  470. case GEN_CFG:
  471. for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
  472. if (!strcasecmp(name, gen_spec[i].name))
  473. return i;
  474. }
  475. }
  476. return -1;
  477. }
  478. static inline void misdn_cfg_lock (void)
  479. {
  480. ast_mutex_lock(&config_mutex);
  481. }
  482. static inline void misdn_cfg_unlock (void)
  483. {
  484. ast_mutex_unlock(&config_mutex);
  485. }
  486. static void _free_msn_list (struct msn_list* iter)
  487. {
  488. if (iter->next)
  489. _free_msn_list(iter->next);
  490. if (iter->msn)
  491. ast_free(iter->msn);
  492. ast_free(iter);
  493. }
  494. static void _free_port_cfg (void)
  495. {
  496. int i, j;
  497. int gn = map[MISDN_CFG_GROUPNAME];
  498. union misdn_cfg_pt* free_list[max_ports + 2];
  499. memset(free_list, 0, sizeof(free_list));
  500. free_list[0] = port_cfg[0];
  501. for (i = 1; i <= max_ports; ++i) {
  502. if (port_cfg[i][gn].str) {
  503. /* we always have a groupname in the non-default case, so this is fine */
  504. for (j = 1; j <= max_ports; ++j) {
  505. if (free_list[j] && free_list[j][gn].str == port_cfg[i][gn].str)
  506. break;
  507. else if (!free_list[j]) {
  508. free_list[j] = port_cfg[i];
  509. break;
  510. }
  511. }
  512. }
  513. }
  514. for (j = 0; free_list[j]; ++j) {
  515. for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
  516. if (free_list[j][i].any) {
  517. if (port_spec[i].type == MISDN_CTYPE_MSNLIST) {
  518. _free_msn_list(free_list[j][i].ml);
  519. } else if (port_spec[i].type == MISDN_CTYPE_ASTNAMEDGROUP) {
  520. ast_unref_namedgroups(free_list[j][i].namgrp);
  521. } else {
  522. ast_free(free_list[j][i].any);
  523. }
  524. }
  525. }
  526. }
  527. }
  528. static void _free_general_cfg (void)
  529. {
  530. int i;
  531. for (i = 0; i < NUM_GEN_ELEMENTS; i++)
  532. if (general_cfg[i].any)
  533. ast_free(general_cfg[i].any);
  534. }
  535. void misdn_cfg_get(int port, enum misdn_cfg_elements elem, void *buf, int bufsize)
  536. {
  537. int place;
  538. if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
  539. memset(buf, 0, bufsize);
  540. ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Port number %d is not valid.\n", port);
  541. return;
  542. }
  543. misdn_cfg_lock();
  544. if (elem == MISDN_CFG_PTP) {
  545. if (!memcpy(buf, &ptp[port], (bufsize > ptp[port]) ? sizeof(ptp[port]) : bufsize))
  546. memset(buf, 0, bufsize);
  547. } else {
  548. if ((place = map[elem]) < 0) {
  549. memset(buf, 0, bufsize);
  550. ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Invalid element (%d) requested.\n", elem);
  551. } else {
  552. if (elem < MISDN_CFG_LAST) {
  553. switch (port_spec[place].type) {
  554. case MISDN_CTYPE_STR:
  555. if (port_cfg[port][place].str) {
  556. ast_copy_string(buf, port_cfg[port][place].str, bufsize);
  557. } else if (port_cfg[0][place].str) {
  558. ast_copy_string(buf, port_cfg[0][place].str, bufsize);
  559. } else
  560. memset(buf, 0, bufsize);
  561. break;
  562. case MISDN_CTYPE_ASTNAMEDGROUP:
  563. if (bufsize >= sizeof(struct ast_namedgroups *)) {
  564. if (port_cfg[port][place].namgrp) {
  565. *(struct ast_namedgroups **)buf = port_cfg[port][place].namgrp;
  566. } else if (port_cfg[0][place].namgrp) {
  567. *(struct ast_namedgroups **)buf = port_cfg[0][place].namgrp;
  568. } else {
  569. *(struct ast_namedgroups **)buf = NULL;
  570. }
  571. }
  572. break;
  573. default:
  574. if (port_cfg[port][place].any)
  575. memcpy(buf, port_cfg[port][place].any, bufsize);
  576. else if (port_cfg[0][place].any)
  577. memcpy(buf, port_cfg[0][place].any, bufsize);
  578. else
  579. memset(buf, 0, bufsize);
  580. }
  581. } else {
  582. switch (gen_spec[place].type) {
  583. case MISDN_CTYPE_STR:
  584. ast_copy_string(buf, S_OR(general_cfg[place].str, ""), bufsize);
  585. break;
  586. default:
  587. if (general_cfg[place].any)
  588. memcpy(buf, general_cfg[place].any, bufsize);
  589. else
  590. memset(buf, 0, bufsize);
  591. }
  592. }
  593. }
  594. }
  595. misdn_cfg_unlock();
  596. }
  597. enum misdn_cfg_elements misdn_cfg_get_elem(const char *name)
  598. {
  599. int pos;
  600. /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
  601. if (!strcmp(name, "ports"))
  602. return MISDN_CFG_GROUPNAME;
  603. if (!strcmp(name, "name"))
  604. return MISDN_CFG_FIRST;
  605. pos = get_cfg_position(name, PORT_CFG);
  606. if (pos >= 0)
  607. return port_spec[pos].elem;
  608. pos = get_cfg_position(name, GEN_CFG);
  609. if (pos >= 0)
  610. return gen_spec[pos].elem;
  611. return MISDN_CFG_FIRST;
  612. }
  613. void misdn_cfg_get_name(enum misdn_cfg_elements elem, void *buf, int bufsize)
  614. {
  615. struct misdn_cfg_spec *spec = NULL;
  616. int place = map[elem];
  617. /* the ptp hack */
  618. if (elem == MISDN_CFG_PTP) {
  619. memset(buf, 0, 1);
  620. return;
  621. }
  622. /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
  623. if (elem == MISDN_CFG_GROUPNAME) {
  624. if (!snprintf(buf, bufsize, "ports"))
  625. memset(buf, 0, 1);
  626. return;
  627. }
  628. if ((elem > MISDN_CFG_FIRST) && (elem < MISDN_CFG_LAST))
  629. spec = (struct misdn_cfg_spec *)port_spec;
  630. else if ((elem > MISDN_GEN_FIRST) && (elem < MISDN_GEN_LAST))
  631. spec = (struct misdn_cfg_spec *)gen_spec;
  632. ast_copy_string(buf, spec ? spec[place].name : "", bufsize);
  633. }
  634. void misdn_cfg_get_desc (enum misdn_cfg_elements elem, void *buf, int bufsize, void *buf_default, int bufsize_default)
  635. {
  636. int place = map[elem];
  637. struct misdn_cfg_spec *spec = NULL;
  638. /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
  639. if (elem == MISDN_CFG_GROUPNAME) {
  640. ast_copy_string(buf, ports_description, bufsize);
  641. if (buf_default && bufsize_default)
  642. memset(buf_default, 0, 1);
  643. return;
  644. }
  645. if ((elem > MISDN_CFG_FIRST) && (elem < MISDN_CFG_LAST))
  646. spec = (struct misdn_cfg_spec *)port_spec;
  647. else if ((elem > MISDN_GEN_FIRST) && (elem < MISDN_GEN_LAST))
  648. spec = (struct misdn_cfg_spec *)gen_spec;
  649. if (!spec)
  650. memset(buf, 0, 1);
  651. else {
  652. ast_copy_string(buf, spec[place].desc, bufsize);
  653. if (buf_default && bufsize) {
  654. if (!strcmp(spec[place].def, NO_DEFAULT))
  655. memset(buf_default, 0, 1);
  656. else
  657. ast_copy_string(buf_default, spec[place].def, bufsize_default);
  658. }
  659. }
  660. }
  661. int misdn_cfg_is_msn_valid (int port, char* msn)
  662. {
  663. int re = 0;
  664. struct msn_list *iter;
  665. if (!misdn_cfg_is_port_valid(port)) {
  666. ast_log(LOG_WARNING, "Invalid call to misdn_cfg_is_msn_valid! Port number %d is not valid.\n", port);
  667. return 0;
  668. }
  669. misdn_cfg_lock();
  670. if (port_cfg[port][map[MISDN_CFG_MSNS]].ml)
  671. iter = port_cfg[port][map[MISDN_CFG_MSNS]].ml;
  672. else
  673. iter = port_cfg[0][map[MISDN_CFG_MSNS]].ml;
  674. for (; iter; iter = iter->next)
  675. if (*(iter->msn) == '*' || ast_extension_match(iter->msn, msn)) {
  676. re = 1;
  677. break;
  678. }
  679. misdn_cfg_unlock();
  680. return re;
  681. }
  682. int misdn_cfg_is_port_valid (int port)
  683. {
  684. int gn = map[MISDN_CFG_GROUPNAME];
  685. return (port >= 1 && port <= max_ports && port_cfg[port][gn].str);
  686. }
  687. int misdn_cfg_is_group_method (char *group, enum misdn_cfg_method meth)
  688. {
  689. int i, re = 0;
  690. char *method ;
  691. misdn_cfg_lock();
  692. method = port_cfg[0][map[MISDN_CFG_METHOD]].str;
  693. for (i = 1; i <= max_ports; i++) {
  694. if (port_cfg[i] && port_cfg[i][map[MISDN_CFG_GROUPNAME]].str) {
  695. if (!strcasecmp(port_cfg[i][map[MISDN_CFG_GROUPNAME]].str, group))
  696. method = (port_cfg[i][map[MISDN_CFG_METHOD]].str ?
  697. port_cfg[i][map[MISDN_CFG_METHOD]].str : port_cfg[0][map[MISDN_CFG_METHOD]].str);
  698. }
  699. }
  700. if (method) {
  701. switch (meth) {
  702. case METHOD_STANDARD: re = !strcasecmp(method, "standard");
  703. break;
  704. case METHOD_ROUND_ROBIN: re = !strcasecmp(method, "round_robin");
  705. break;
  706. case METHOD_STANDARD_DEC: re = !strcasecmp(method, "standard_dec");
  707. break;
  708. }
  709. }
  710. misdn_cfg_unlock();
  711. return re;
  712. }
  713. /*!
  714. * \brief Generate a comma separated list of all active ports
  715. */
  716. void misdn_cfg_get_ports_string (char *ports)
  717. {
  718. char tmp[16];
  719. int l, i;
  720. int gn = map[MISDN_CFG_GROUPNAME];
  721. *ports = 0;
  722. misdn_cfg_lock();
  723. for (i = 1; i <= max_ports; i++) {
  724. if (port_cfg[i][gn].str) {
  725. if (ptp[i])
  726. sprintf(tmp, "%dptp,", i);
  727. else
  728. sprintf(tmp, "%d,", i);
  729. strcat(ports, tmp);
  730. }
  731. }
  732. misdn_cfg_unlock();
  733. if ((l = strlen(ports))) {
  734. /* Strip trailing ',' */
  735. ports[l-1] = 0;
  736. }
  737. }
  738. void misdn_cfg_get_config_string (int port, enum misdn_cfg_elements elem, char* buf, int bufsize)
  739. {
  740. int place;
  741. char tempbuf[BUFFERSIZE] = "";
  742. struct msn_list *iter;
  743. if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
  744. *buf = 0;
  745. ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Port number %d is not valid.\n", port);
  746. return;
  747. }
  748. place = map[elem];
  749. misdn_cfg_lock();
  750. if (elem == MISDN_CFG_PTP) {
  751. snprintf(buf, bufsize, " -> ptp: %s", ptp[port] ? "yes" : "no");
  752. }
  753. else if (elem > MISDN_CFG_FIRST && elem < MISDN_CFG_LAST) {
  754. switch (port_spec[place].type) {
  755. case MISDN_CTYPE_INT:
  756. case MISDN_CTYPE_BOOLINT:
  757. if (port_cfg[port][place].num)
  758. snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[port][place].num);
  759. else if (port_cfg[0][place].num)
  760. snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[0][place].num);
  761. else
  762. snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
  763. break;
  764. case MISDN_CTYPE_BOOL:
  765. if (port_cfg[port][place].num)
  766. snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[port][place].num ? "yes" : "no");
  767. else if (port_cfg[0][place].num)
  768. snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[0][place].num ? "yes" : "no");
  769. else
  770. snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
  771. break;
  772. case MISDN_CTYPE_ASTGROUP:
  773. if (port_cfg[port][place].grp)
  774. snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name,
  775. ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[port][place].grp));
  776. else if (port_cfg[0][place].grp)
  777. snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name,
  778. ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[0][place].grp));
  779. else
  780. snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
  781. break;
  782. case MISDN_CTYPE_ASTNAMEDGROUP:
  783. if (port_cfg[port][place].namgrp) {
  784. struct ast_str *tmp_str = ast_str_create(1024);
  785. if (tmp_str) {
  786. snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name,
  787. ast_print_namedgroups(&tmp_str, port_cfg[port][place].namgrp));
  788. ast_free(tmp_str);
  789. }
  790. } else if (port_cfg[0][place].namgrp) {
  791. struct ast_str *tmp_str = ast_str_create(1024);
  792. if (tmp_str) {
  793. snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name,
  794. ast_print_namedgroups(&tmp_str, port_cfg[0][place].namgrp));
  795. ast_free(tmp_str);
  796. }
  797. } else {
  798. snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
  799. }
  800. break;
  801. case MISDN_CTYPE_MSNLIST:
  802. if (port_cfg[port][place].ml)
  803. iter = port_cfg[port][place].ml;
  804. else
  805. iter = port_cfg[0][place].ml;
  806. if (iter) {
  807. for (; iter; iter = iter->next) {
  808. strncat(tempbuf, iter->msn, sizeof(tempbuf) - strlen(tempbuf) - 1);
  809. }
  810. if (strlen(tempbuf) > 1) {
  811. tempbuf[strlen(tempbuf)-2] = 0;
  812. }
  813. }
  814. snprintf(buf, bufsize, " -> msns: %s", *tempbuf ? tempbuf : "none");
  815. break;
  816. case MISDN_CTYPE_STR:
  817. if ( port_cfg[port][place].str) {
  818. snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[port][place].str);
  819. } else if (port_cfg[0][place].str) {
  820. snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[0][place].str);
  821. } else {
  822. snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
  823. }
  824. break;
  825. }
  826. } else if (elem > MISDN_GEN_FIRST && elem < MISDN_GEN_LAST) {
  827. switch (gen_spec[place].type) {
  828. case MISDN_CTYPE_INT:
  829. case MISDN_CTYPE_BOOLINT:
  830. if (general_cfg[place].num)
  831. snprintf(buf, bufsize, " -> %s: %d", gen_spec[place].name, *general_cfg[place].num);
  832. else
  833. snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
  834. break;
  835. case MISDN_CTYPE_BOOL:
  836. if (general_cfg[place].num)
  837. snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, *general_cfg[place].num ? "yes" : "no");
  838. else
  839. snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
  840. break;
  841. case MISDN_CTYPE_STR:
  842. if ( general_cfg[place].str) {
  843. snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, general_cfg[place].str);
  844. } else {
  845. snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
  846. }
  847. break;
  848. default:
  849. snprintf(buf, bufsize, " -> type of %s not handled yet", gen_spec[place].name);
  850. break;
  851. }
  852. } else {
  853. *buf = 0;
  854. ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Invalid config element (%d) requested.\n", elem);
  855. }
  856. misdn_cfg_unlock();
  857. }
  858. int misdn_cfg_get_next_port (int port)
  859. {
  860. int p = -1;
  861. int gn = map[MISDN_CFG_GROUPNAME];
  862. misdn_cfg_lock();
  863. for (port++; port <= max_ports; port++) {
  864. if (port_cfg[port][gn].str) {
  865. p = port;
  866. break;
  867. }
  868. }
  869. misdn_cfg_unlock();
  870. return p;
  871. }
  872. int misdn_cfg_get_next_port_spin (int port)
  873. {
  874. int p = misdn_cfg_get_next_port(port);
  875. return (p > 0) ? p : misdn_cfg_get_next_port(0);
  876. }
  877. static int _parse (union misdn_cfg_pt *dest, const char *value, enum misdn_cfg_type type, int boolint_def)
  878. {
  879. int re = 0;
  880. int len, tmp;
  881. char *valtmp;
  882. char *tmp2 = ast_strdupa(value);
  883. switch (type) {
  884. case MISDN_CTYPE_STR:
  885. if (dest->str) {
  886. ast_free(dest->str);
  887. }
  888. if ((len = strlen(value))) {
  889. dest->str = ast_malloc((len + 1) * sizeof(char));
  890. strncpy(dest->str, value, len);
  891. dest->str[len] = 0;
  892. } else {
  893. dest->str = ast_malloc(sizeof(char));
  894. dest->str[0] = 0;
  895. }
  896. break;
  897. case MISDN_CTYPE_INT:
  898. {
  899. int res;
  900. if (strchr(value,'x')) {
  901. res = sscanf(value, "%30x", &tmp);
  902. } else {
  903. res = sscanf(value, "%30d", &tmp);
  904. }
  905. if (res) {
  906. if (!dest->num) {
  907. dest->num = ast_malloc(sizeof(int));
  908. }
  909. memcpy(dest->num, &tmp, sizeof(int));
  910. } else
  911. re = -1;
  912. }
  913. break;
  914. case MISDN_CTYPE_BOOL:
  915. if (!dest->num) {
  916. dest->num = ast_malloc(sizeof(int));
  917. }
  918. *(dest->num) = (ast_true(value) ? 1 : 0);
  919. break;
  920. case MISDN_CTYPE_BOOLINT:
  921. if (!dest->num) {
  922. dest->num = ast_malloc(sizeof(int));
  923. }
  924. if (sscanf(value, "%30d", &tmp)) {
  925. memcpy(dest->num, &tmp, sizeof(int));
  926. } else {
  927. *(dest->num) = (ast_true(value) ? boolint_def : 0);
  928. }
  929. break;
  930. case MISDN_CTYPE_MSNLIST:
  931. for (valtmp = strsep(&tmp2, ","); valtmp; valtmp = strsep(&tmp2, ",")) {
  932. if ((len = strlen(valtmp))) {
  933. struct msn_list *ml = ast_malloc(sizeof(*ml));
  934. ml->msn = ast_calloc(len+1, sizeof(char));
  935. strncpy(ml->msn, valtmp, len);
  936. ml->next = dest->ml;
  937. dest->ml = ml;
  938. }
  939. }
  940. break;
  941. case MISDN_CTYPE_ASTGROUP:
  942. if (!dest->grp) {
  943. dest->grp = ast_malloc(sizeof(ast_group_t));
  944. }
  945. *(dest->grp) = ast_get_group(value);
  946. break;
  947. case MISDN_CTYPE_ASTNAMEDGROUP:
  948. dest->namgrp = ast_get_namedgroups(value);
  949. break;
  950. }
  951. return re;
  952. }
  953. static void _build_general_config (struct ast_variable *v)
  954. {
  955. int pos;
  956. for (; v; v = v->next) {
  957. if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
  958. continue;
  959. if (((pos = get_cfg_position(v->name, GEN_CFG)) < 0) ||
  960. (_parse(&general_cfg[pos], v->value, gen_spec[pos].type, gen_spec[pos].boolint_def) < 0))
  961. CLI_ERROR(v->name, v->value, "general");
  962. }
  963. }
  964. static void _build_port_config (struct ast_variable *v, char *cat)
  965. {
  966. int pos, i;
  967. union misdn_cfg_pt cfg_tmp[NUM_PORT_ELEMENTS];
  968. int cfg_for_ports[max_ports + 1];
  969. if (!v || !cat)
  970. return;
  971. memset(cfg_tmp, 0, sizeof(cfg_tmp));
  972. memset(cfg_for_ports, 0, sizeof(cfg_for_ports));
  973. if (!strcasecmp(cat, "default")) {
  974. cfg_for_ports[0] = 1;
  975. }
  976. if (((pos = get_cfg_position("name", PORT_CFG)) < 0) ||
  977. (_parse(&cfg_tmp[pos], cat, port_spec[pos].type, port_spec[pos].boolint_def) < 0)) {
  978. CLI_ERROR(v->name, v->value, cat);
  979. return;
  980. }
  981. for (; v; v = v->next) {
  982. if (!strcasecmp(v->name, "ports")) {
  983. char *token, *tmp = ast_strdupa(v->value);
  984. char ptpbuf[BUFFERSIZE] = "";
  985. int start, end;
  986. for (token = strsep(&tmp, ","); token; token = strsep(&tmp, ","), *ptpbuf = 0) {
  987. if (!*token)
  988. continue;
  989. if (sscanf(token, "%30d-%30d%511s", &start, &end, ptpbuf) >= 2) {
  990. for (; start <= end; start++) {
  991. if (start <= max_ports && start > 0) {
  992. cfg_for_ports[start] = 1;
  993. ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
  994. } else
  995. CLI_ERROR(v->name, v->value, cat);
  996. }
  997. } else {
  998. if (sscanf(token, "%30d%511s", &start, ptpbuf)) {
  999. if (start <= max_ports && start > 0) {
  1000. cfg_for_ports[start] = 1;
  1001. ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
  1002. } else
  1003. CLI_ERROR(v->name, v->value, cat);
  1004. } else
  1005. CLI_ERROR(v->name, v->value, cat);
  1006. }
  1007. }
  1008. } else {
  1009. if (((pos = get_cfg_position(v->name, PORT_CFG)) < 0) ||
  1010. (_parse(&cfg_tmp[pos], v->value, port_spec[pos].type, port_spec[pos].boolint_def) < 0))
  1011. CLI_ERROR(v->name, v->value, cat);
  1012. }
  1013. }
  1014. for (i = 0; i < (max_ports + 1); ++i) {
  1015. if (i > 0 && cfg_for_ports[0]) {
  1016. /* default category, will populate the port_cfg with additional port
  1017. categories in subsequent calls to this function */
  1018. memset(cfg_tmp, 0, sizeof(cfg_tmp));
  1019. }
  1020. if (cfg_for_ports[i]) {
  1021. memcpy(port_cfg[i], cfg_tmp, sizeof(cfg_tmp));
  1022. }
  1023. }
  1024. }
  1025. void misdn_cfg_update_ptp (void)
  1026. {
  1027. #ifndef MISDN_1_2
  1028. char misdn_init[BUFFERSIZE];
  1029. char line[BUFFERSIZE];
  1030. FILE *fp;
  1031. char *tok, *p, *end;
  1032. int port;
  1033. misdn_cfg_get(0, MISDN_GEN_MISDN_INIT, &misdn_init, sizeof(misdn_init));
  1034. if (!ast_strlen_zero(misdn_init)) {
  1035. fp = fopen(misdn_init, "r");
  1036. if (fp) {
  1037. while(fgets(line, sizeof(line), fp)) {
  1038. if (!strncmp(line, "nt_ptp", 6)) {
  1039. for (tok = strtok_r(line,",=", &p);
  1040. tok;
  1041. tok = strtok_r(NULL,",=", &p)) {
  1042. port = strtol(tok, &end, 10);
  1043. if (end != tok && misdn_cfg_is_port_valid(port)) {
  1044. misdn_cfg_lock();
  1045. ptp[port] = 1;
  1046. misdn_cfg_unlock();
  1047. }
  1048. }
  1049. }
  1050. }
  1051. fclose(fp);
  1052. } else {
  1053. ast_log(LOG_WARNING,"Couldn't open %s: %s\n", misdn_init, strerror(errno));
  1054. }
  1055. }
  1056. #else
  1057. int i;
  1058. int proto;
  1059. char filename[128];
  1060. FILE *fp;
  1061. for (i = 1; i <= max_ports; ++i) {
  1062. snprintf(filename, sizeof(filename), "/sys/class/mISDN-stacks/st-%08x/protocol", i << 8);
  1063. fp = fopen(filename, "r");
  1064. if (!fp) {
  1065. ast_log(LOG_WARNING, "Could not open %s: %s\n", filename, strerror(errno));
  1066. continue;
  1067. }
  1068. if (fscanf(fp, "0x%08x", &proto) != 1)
  1069. ast_log(LOG_WARNING, "Could not parse contents of %s!\n", filename);
  1070. else
  1071. ptp[i] = proto & 1<<5 ? 1 : 0;
  1072. fclose(fp);
  1073. }
  1074. #endif
  1075. }
  1076. static void _fill_defaults (void)
  1077. {
  1078. int i;
  1079. for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
  1080. if (!port_cfg[0][i].any && strcasecmp(port_spec[i].def, NO_DEFAULT))
  1081. _parse(&(port_cfg[0][i]), (char *)port_spec[i].def, port_spec[i].type, port_spec[i].boolint_def);
  1082. }
  1083. for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
  1084. if (!general_cfg[i].any && strcasecmp(gen_spec[i].def, NO_DEFAULT))
  1085. _parse(&(general_cfg[i]), (char *)gen_spec[i].def, gen_spec[i].type, gen_spec[i].boolint_def);
  1086. }
  1087. }
  1088. void misdn_cfg_reload (void)
  1089. {
  1090. misdn_cfg_init(0, 1);
  1091. }
  1092. void misdn_cfg_destroy (void)
  1093. {
  1094. misdn_cfg_lock();
  1095. _free_port_cfg();
  1096. _free_general_cfg();
  1097. ast_free(port_cfg);
  1098. ast_free(general_cfg);
  1099. ast_free(ptp);
  1100. ast_free(map);
  1101. misdn_cfg_unlock();
  1102. ast_mutex_destroy(&config_mutex);
  1103. }
  1104. int misdn_cfg_init(int this_max_ports, int reload)
  1105. {
  1106. char config[] = "misdn.conf";
  1107. char *cat, *p;
  1108. int i;
  1109. struct ast_config *cfg;
  1110. struct ast_variable *v;
  1111. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  1112. if (!(cfg = ast_config_load2(config, "chan_misdn", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
  1113. ast_log(LOG_WARNING, "missing or invalid file: misdn.conf\n");
  1114. return -1;
  1115. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
  1116. return 0;
  1117. ast_mutex_init(&config_mutex);
  1118. /* Copy the default jb config over global_jbconf */
  1119. memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
  1120. misdn_cfg_lock();
  1121. if (this_max_ports) {
  1122. /* this is the first run */
  1123. max_ports = this_max_ports;
  1124. map = ast_calloc(MISDN_GEN_LAST + 1, sizeof(int));
  1125. if (_enum_array_map())
  1126. return -1;
  1127. p = ast_calloc(1, (max_ports + 1) * sizeof(union misdn_cfg_pt *)
  1128. + (max_ports + 1) * NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt));
  1129. port_cfg = (union misdn_cfg_pt **)p;
  1130. p += (max_ports + 1) * sizeof(union misdn_cfg_pt *);
  1131. for (i = 0; i <= max_ports; ++i) {
  1132. port_cfg[i] = (union misdn_cfg_pt *)p;
  1133. p += NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt);
  1134. }
  1135. general_cfg = ast_calloc(1, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
  1136. ptp = ast_calloc(max_ports + 1, sizeof(int));
  1137. }
  1138. else {
  1139. /* misdn reload */
  1140. _free_port_cfg();
  1141. _free_general_cfg();
  1142. memset(port_cfg[0], 0, NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt) * (max_ports + 1));
  1143. memset(general_cfg, 0, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
  1144. memset(ptp, 0, sizeof(int) * (max_ports + 1));
  1145. }
  1146. cat = ast_category_browse(cfg, NULL);
  1147. while(cat) {
  1148. v = ast_variable_browse(cfg, cat);
  1149. if (!strcasecmp(cat, "general")) {
  1150. _build_general_config(v);
  1151. } else {
  1152. _build_port_config(v, cat);
  1153. }
  1154. cat = ast_category_browse(cfg, cat);
  1155. }
  1156. _fill_defaults();
  1157. misdn_cfg_unlock();
  1158. ast_config_destroy(cfg);
  1159. return 0;
  1160. }
  1161. struct ast_jb_conf *misdn_get_global_jbconf() {
  1162. return &global_jbconf;
  1163. }