indications.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2002, Pauline Middelink
  5. * Copyright (C) 2009, Digium, Inc.
  6. *
  7. * See http://www.asterisk.org for more information about
  8. * the Asterisk project. Please do not directly contact
  9. * any of the maintainers of this project for assistance;
  10. * the project provides a web site, mailing lists and IRC
  11. * channels for your use.
  12. *
  13. * This program is free software, distributed under the terms of
  14. * the GNU General Public License Version 2. See the LICENSE file
  15. * at the top of the source tree.
  16. */
  17. /*!
  18. * \file
  19. * \brief Indication Tone Handling
  20. *
  21. * \author Pauline Middelink <middelink@polyware.nl>
  22. * \author Russell Bryant <russell@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include <math.h>
  30. #include "asterisk/lock.h"
  31. #include "asterisk/linkedlists.h"
  32. #include "asterisk/indications.h"
  33. #include "asterisk/frame.h"
  34. #include "asterisk/format_cache.h"
  35. #include "asterisk/channel.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/cli.h"
  38. #include "asterisk/module.h"
  39. #include "asterisk/astobj2.h"
  40. #include "asterisk/data.h"
  41. #include "asterisk/_private.h" /* _init(), _reload() */
  42. #define DATA_EXPORT_TONE_ZONE(MEMBER) \
  43. MEMBER(ast_tone_zone, country, AST_DATA_STRING) \
  44. MEMBER(ast_tone_zone, description, AST_DATA_STRING) \
  45. MEMBER(ast_tone_zone, nrringcadence, AST_DATA_UNSIGNED_INTEGER)
  46. AST_DATA_STRUCTURE(ast_tone_zone, DATA_EXPORT_TONE_ZONE);
  47. #define DATA_EXPORT_TONE_ZONE_SOUND(MEMBER) \
  48. MEMBER(ast_tone_zone_sound, name, AST_DATA_STRING) \
  49. MEMBER(ast_tone_zone_sound, data, AST_DATA_STRING)
  50. AST_DATA_STRUCTURE(ast_tone_zone_sound, DATA_EXPORT_TONE_ZONE_SOUND);
  51. /* Globals */
  52. static const char config[] = "indications.conf";
  53. static const int midi_tohz[128] = {
  54. 8, 8, 9, 9, 10, 10, 11, 12, 12, 13,
  55. 14, 15, 16, 17, 18, 19, 20, 21, 23, 24,
  56. 25, 27, 29, 30, 32, 34, 36, 38, 41, 43,
  57. 46, 48, 51, 55, 58, 61, 65, 69, 73, 77,
  58. 82, 87, 92, 97, 103, 110, 116, 123, 130, 138,
  59. 146, 155, 164, 174, 184, 195, 207, 220, 233, 246,
  60. 261, 277, 293, 311, 329, 349, 369, 391, 415, 440,
  61. 466, 493, 523, 554, 587, 622, 659, 698, 739, 783,
  62. 830, 880, 932, 987, 1046, 1108, 1174, 1244, 1318, 1396,
  63. 1479, 1567, 1661, 1760, 1864, 1975, 2093, 2217, 2349, 2489,
  64. 2637, 2793, 2959, 3135, 3322, 3520, 3729, 3951, 4186, 4434,
  65. 4698, 4978, 5274, 5587, 5919, 6271, 6644, 7040, 7458, 7902,
  66. 8372, 8869, 9397, 9956, 10548, 11175, 11839, 12543
  67. };
  68. static struct ao2_container *ast_tone_zones;
  69. #define NUM_TONE_ZONE_BUCKETS 53
  70. /*!
  71. * \note Access to this is protected by locking the ast_tone_zones container
  72. */
  73. static struct ast_tone_zone *default_tone_zone;
  74. struct playtones_item {
  75. int fac1;
  76. int init_v2_1;
  77. int init_v3_1;
  78. int fac2;
  79. int init_v2_2;
  80. int init_v3_2;
  81. int modulate;
  82. int duration;
  83. };
  84. struct playtones_def {
  85. int vol;
  86. int reppos;
  87. int nitems;
  88. int interruptible;
  89. struct playtones_item *items;
  90. };
  91. struct playtones_state {
  92. int vol;
  93. int v1_1;
  94. int v2_1;
  95. int v3_1;
  96. int v1_2;
  97. int v2_2;
  98. int v3_2;
  99. int reppos;
  100. int nitems;
  101. struct playtones_item *items;
  102. int npos;
  103. int oldnpos;
  104. int pos;
  105. struct ast_format *origwfmt;
  106. struct ast_frame f;
  107. unsigned char offset[AST_FRIENDLY_OFFSET];
  108. short data[4000];
  109. };
  110. static void playtones_release(struct ast_channel *chan, void *params)
  111. {
  112. struct playtones_state *ps = params;
  113. if (chan) {
  114. ast_set_write_format(chan, ps->origwfmt);
  115. }
  116. ao2_cleanup(ps->origwfmt);
  117. ast_free(ps->items);
  118. ast_free(ps);
  119. }
  120. static void *playtones_alloc(struct ast_channel *chan, void *params)
  121. {
  122. struct playtones_def *pd = params;
  123. struct playtones_state *ps = NULL;
  124. if (!(ps = ast_calloc(1, sizeof(*ps)))) {
  125. return NULL;
  126. }
  127. ps->origwfmt = ao2_bump(ast_channel_writeformat(chan));
  128. if (ast_set_write_format(chan, ast_format_slin)) {
  129. ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", ast_channel_name(chan));
  130. playtones_release(NULL, ps);
  131. ps = NULL;
  132. } else {
  133. ps->vol = pd->vol;
  134. ps->reppos = pd->reppos;
  135. ps->nitems = pd->nitems;
  136. ps->items = pd->items;
  137. ps->oldnpos = -1;
  138. }
  139. /* Let interrupts interrupt :) */
  140. if (pd->interruptible) {
  141. ast_set_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
  142. } else {
  143. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
  144. }
  145. return ps;
  146. }
  147. static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
  148. {
  149. struct playtones_state *ps = data;
  150. struct playtones_item *pi;
  151. int x;
  152. /* we need to prepare a frame with 16 * timelen samples as we're
  153. * generating SLIN audio */
  154. len = samples * 2;
  155. if (len > sizeof(ps->data) / 2 - 1) {
  156. ast_log(LOG_WARNING, "Can't generate that much data!\n");
  157. return -1;
  158. }
  159. memset(&ps->f, 0, sizeof(ps->f));
  160. pi = &ps->items[ps->npos];
  161. if (ps->oldnpos != ps->npos) {
  162. /* Load new parameters */
  163. ps->v1_1 = 0;
  164. ps->v2_1 = pi->init_v2_1;
  165. ps->v3_1 = pi->init_v3_1;
  166. ps->v1_2 = 0;
  167. ps->v2_2 = pi->init_v2_2;
  168. ps->v3_2 = pi->init_v3_2;
  169. ps->oldnpos = ps->npos;
  170. }
  171. for (x = 0; x < samples; x++) {
  172. ps->v1_1 = ps->v2_1;
  173. ps->v2_1 = ps->v3_1;
  174. ps->v3_1 = (pi->fac1 * ps->v2_1 >> 15) - ps->v1_1;
  175. ps->v1_2 = ps->v2_2;
  176. ps->v2_2 = ps->v3_2;
  177. ps->v3_2 = (pi->fac2 * ps->v2_2 >> 15) - ps->v1_2;
  178. if (pi->modulate) {
  179. int p;
  180. p = ps->v3_2 - 32768;
  181. if (p < 0) {
  182. p = -p;
  183. }
  184. p = ((p * 9) / 10) + 1;
  185. ps->data[x] = (ps->v3_1 * p) >> 15;
  186. } else {
  187. ps->data[x] = ps->v3_1 + ps->v3_2;
  188. }
  189. }
  190. ps->f.frametype = AST_FRAME_VOICE;
  191. ps->f.subclass.format = ast_format_slin;
  192. ps->f.datalen = len;
  193. ps->f.samples = samples;
  194. ps->f.offset = AST_FRIENDLY_OFFSET;
  195. ps->f.data.ptr = ps->data;
  196. if (ast_write(chan, &ps->f)) {
  197. return -1;
  198. }
  199. ps->pos += x;
  200. if (pi->duration && ps->pos >= pi->duration * 8) { /* item finished? */
  201. ps->pos = 0; /* start new item */
  202. ps->npos++;
  203. if (ps->npos >= ps->nitems) { /* last item? */
  204. if (ps->reppos == -1) { /* repeat set? */
  205. return -1;
  206. }
  207. ps->npos = ps->reppos; /* redo from top */
  208. }
  209. }
  210. return 0;
  211. }
  212. static struct ast_generator playtones = {
  213. .alloc = playtones_alloc,
  214. .release = playtones_release,
  215. .generate = playtones_generator,
  216. };
  217. int ast_tone_zone_part_parse(const char *s, struct ast_tone_zone_part *tone_data)
  218. {
  219. if (sscanf(s, "%30u+%30u/%30u", &tone_data->freq1, &tone_data->freq2,
  220. &tone_data->time) == 3) {
  221. /* f1+f2/time format */
  222. } else if (sscanf(s, "%30u+%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
  223. /* f1+f2 format */
  224. tone_data->time = 0;
  225. } else if (sscanf(s, "%30u*%30u/%30u", &tone_data->freq1, &tone_data->freq2,
  226. &tone_data->time) == 3) {
  227. /* f1*f2/time format */
  228. tone_data->modulate = 1;
  229. } else if (sscanf(s, "%30u*%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
  230. /* f1*f2 format */
  231. tone_data->time = 0;
  232. tone_data->modulate = 1;
  233. } else if (sscanf(s, "%30u/%30u", &tone_data->freq1, &tone_data->time) == 2) {
  234. /* f1/time format */
  235. tone_data->freq2 = 0;
  236. } else if (sscanf(s, "%30u", &tone_data->freq1) == 1) {
  237. /* f1 format */
  238. tone_data->freq2 = 0;
  239. tone_data->time = 0;
  240. } else if (sscanf(s, "M%30u+M%30u/%30u", &tone_data->freq1, &tone_data->freq2,
  241. &tone_data->time) == 3) {
  242. /* Mf1+Mf2/time format */
  243. tone_data->midinote = 1;
  244. } else if (sscanf(s, "M%30u+M%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
  245. /* Mf1+Mf2 format */
  246. tone_data->time = 0;
  247. tone_data->midinote = 1;
  248. } else if (sscanf(s, "M%30u*M%30u/%30u", &tone_data->freq1, &tone_data->freq2,
  249. &tone_data->time) == 3) {
  250. /* Mf1*Mf2/time format */
  251. tone_data->modulate = 1;
  252. tone_data->midinote = 1;
  253. } else if (sscanf(s, "M%30u*M%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
  254. /* Mf1*Mf2 format */
  255. tone_data->time = 0;
  256. tone_data->modulate = 1;
  257. tone_data->midinote = 1;
  258. } else if (sscanf(s, "M%30u/%30u", &tone_data->freq1, &tone_data->time) == 2) {
  259. /* Mf1/time format */
  260. tone_data->freq2 = -1;
  261. tone_data->midinote = 1;
  262. } else if (sscanf(s, "M%30u", &tone_data->freq1) == 1) {
  263. /* Mf1 format */
  264. tone_data->freq2 = -1;
  265. tone_data->time = 0;
  266. tone_data->midinote = 1;
  267. } else {
  268. return -1;
  269. }
  270. return 0;
  271. }
  272. int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
  273. {
  274. char *s, *data = ast_strdupa(playlst);
  275. struct playtones_def d = { vol, -1, 0, 1, NULL };
  276. char *stringp;
  277. char *separator;
  278. static const float sample_rate = 8000.0;
  279. static const float max_sample_val = 32768.0;
  280. if (vol < 1) {
  281. d.vol = 7219; /* Default to -8db */
  282. }
  283. d.interruptible = interruptible;
  284. stringp = data;
  285. /* check if the data is separated with '|' or with ',' by default */
  286. if (strchr(stringp,'|')) {
  287. separator = "|";
  288. } else {
  289. separator = ",";
  290. }
  291. while ((s = strsep(&stringp, separator)) && !ast_strlen_zero(s)) {
  292. struct playtones_item *new_items;
  293. struct ast_tone_zone_part tone_data = {
  294. .time = 0,
  295. };
  296. s = ast_strip(s);
  297. if (s[0]=='!') {
  298. s++;
  299. } else if (d.reppos == -1) {
  300. d.reppos = d.nitems;
  301. }
  302. if (ast_tone_zone_part_parse(s, &tone_data)) {
  303. ast_log(LOG_ERROR, "Failed to parse tone part '%s'\n", s);
  304. continue;
  305. }
  306. if (tone_data.midinote) {
  307. /* midi notes must be between 0 and 127 */
  308. if (tone_data.freq1 <= 127) {
  309. tone_data.freq1 = midi_tohz[tone_data.freq1];
  310. } else {
  311. tone_data.freq1 = 0;
  312. }
  313. if (tone_data.freq2 <= 127) {
  314. tone_data.freq2 = midi_tohz[tone_data.freq2];
  315. } else {
  316. tone_data.freq2 = 0;
  317. }
  318. }
  319. new_items = ast_realloc(d.items, (d.nitems + 1) * sizeof(*d.items));
  320. if (!new_items) {
  321. ast_free(d.items);
  322. return -1;
  323. }
  324. d.items = new_items;
  325. d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (tone_data.freq1 / sample_rate)) * max_sample_val;
  326. d.items[d.nitems].init_v2_1 = sin(-4.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
  327. d.items[d.nitems].init_v3_1 = sin(-2.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
  328. d.items[d.nitems].fac2 = 2.0 * cos(2.0 * M_PI * (tone_data.freq2 / sample_rate)) * max_sample_val;
  329. d.items[d.nitems].init_v2_2 = sin(-4.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
  330. d.items[d.nitems].init_v3_2 = sin(-2.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
  331. d.items[d.nitems].duration = tone_data.time;
  332. d.items[d.nitems].modulate = tone_data.modulate;
  333. d.nitems++;
  334. }
  335. if (!d.nitems) {
  336. ast_log(LOG_ERROR, "No valid tone parts\n");
  337. return -1;
  338. }
  339. if (ast_activate_generator(chan, &playtones, &d)) {
  340. ast_free(d.items);
  341. return -1;
  342. }
  343. return 0;
  344. }
  345. void ast_playtones_stop(struct ast_channel *chan)
  346. {
  347. ast_deactivate_generator(chan);
  348. }
  349. int ast_tone_zone_count(void)
  350. {
  351. return ao2_container_count(ast_tone_zones);
  352. }
  353. struct ao2_iterator ast_tone_zone_iterator_init(void)
  354. {
  355. return ao2_iterator_init(ast_tone_zones, 0);
  356. }
  357. /*! \brief Set global indication country
  358. If no country is specified or we are unable to find the zone, then return not found */
  359. static int ast_set_indication_country(const char *country)
  360. {
  361. struct ast_tone_zone *zone = NULL;
  362. if (ast_strlen_zero(country) || !(zone = ast_get_indication_zone(country))) {
  363. return -1;
  364. }
  365. ast_verb(3, "Setting default indication country to '%s'\n", country);
  366. ao2_lock(ast_tone_zones);
  367. if (default_tone_zone) {
  368. default_tone_zone = ast_tone_zone_unref(default_tone_zone);
  369. }
  370. default_tone_zone = ast_tone_zone_ref(zone);
  371. ao2_unlock(ast_tone_zones);
  372. zone = ast_tone_zone_unref(zone);
  373. return 0;
  374. }
  375. /*! \brief locate ast_tone_zone, given the country. if country == NULL, use the default country */
  376. struct ast_tone_zone *ast_get_indication_zone(const char *country)
  377. {
  378. struct ast_tone_zone *tz = NULL;
  379. struct ast_tone_zone zone_arg = {
  380. .nrringcadence = 0,
  381. };
  382. if (ast_strlen_zero(country)) {
  383. ao2_lock(ast_tone_zones);
  384. if (default_tone_zone) {
  385. tz = ast_tone_zone_ref(default_tone_zone);
  386. }
  387. ao2_unlock(ast_tone_zones);
  388. return tz;
  389. }
  390. ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
  391. return ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER);
  392. }
  393. struct ast_tone_zone_sound *ast_get_indication_tone(const struct ast_tone_zone *_zone, const char *indication)
  394. {
  395. struct ast_tone_zone_sound *ts = NULL;
  396. /* _zone is const to the users of the API */
  397. struct ast_tone_zone *zone = (struct ast_tone_zone *) _zone;
  398. /* If no zone is specified, use the default */
  399. if (!zone) {
  400. ao2_lock(ast_tone_zones);
  401. if (default_tone_zone) {
  402. zone = ast_tone_zone_ref(default_tone_zone);
  403. }
  404. ao2_unlock(ast_tone_zones);
  405. if (!zone) {
  406. return NULL;
  407. }
  408. }
  409. ast_tone_zone_lock(zone);
  410. /* Look through list of tones in the zone searching for the right one */
  411. AST_LIST_TRAVERSE(&zone->tones, ts, entry) {
  412. if (!strcasecmp(ts->name, indication)) {
  413. /* Increase ref count for the reference we will return */
  414. ts = ast_tone_zone_sound_ref(ts);
  415. break;
  416. }
  417. }
  418. ast_tone_zone_unlock(zone);
  419. if (!_zone)
  420. zone = ast_tone_zone_unref(zone);
  421. return ts;
  422. }
  423. static void ast_tone_zone_sound_destructor(void *obj)
  424. {
  425. struct ast_tone_zone_sound *ts = obj;
  426. /* Deconstify the 'const char *'s so the compiler doesn't complain. (but it's safe) */
  427. if (ts->name) {
  428. ast_free((char *) ts->name);
  429. ts->name = NULL;
  430. }
  431. if (ts->data) {
  432. ast_free((char *) ts->data);
  433. ts->data = NULL;
  434. }
  435. }
  436. /*! \brief deallocate the passed tone zone */
  437. static void ast_tone_zone_destructor(void *obj)
  438. {
  439. struct ast_tone_zone *zone = obj;
  440. struct ast_tone_zone_sound *current;
  441. while ((current = AST_LIST_REMOVE_HEAD(&zone->tones, entry))) {
  442. current = ast_tone_zone_sound_unref(current);
  443. }
  444. if (zone->ringcadence) {
  445. ast_free(zone->ringcadence);
  446. zone->ringcadence = NULL;
  447. }
  448. }
  449. /*! \brief add a new country, if country exists, it will be replaced. */
  450. static int ast_register_indication_country(struct ast_tone_zone *zone)
  451. {
  452. ao2_lock(ast_tone_zones);
  453. if (!default_tone_zone) {
  454. default_tone_zone = ast_tone_zone_ref(zone);
  455. }
  456. ao2_unlock(ast_tone_zones);
  457. ao2_link(ast_tone_zones, zone);
  458. ast_verb(3, "Registered indication country '%s'\n", zone->country);
  459. return 0;
  460. }
  461. /*! \brief remove an existing country and all its indications, country must exist. */
  462. static int ast_unregister_indication_country(const char *country)
  463. {
  464. struct ast_tone_zone *tz = NULL;
  465. struct ast_tone_zone zone_arg = {
  466. .nrringcadence = 0,
  467. };
  468. ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
  469. ao2_lock(ast_tone_zones);
  470. tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER | OBJ_UNLINK);
  471. if (!tz) {
  472. ao2_unlock(ast_tone_zones);
  473. return -1;
  474. }
  475. if (default_tone_zone == tz) {
  476. ast_tone_zone_unref(default_tone_zone);
  477. /* Get a new default, punt to the first one we find */
  478. default_tone_zone = ao2_callback(ast_tone_zones, 0, NULL, NULL);
  479. }
  480. ao2_unlock(ast_tone_zones);
  481. tz = ast_tone_zone_unref(tz);
  482. return 0;
  483. }
  484. /*!
  485. * \note called with the tone zone locked
  486. */
  487. static int ast_register_indication(struct ast_tone_zone *zone, const char *indication,
  488. const char *tonelist)
  489. {
  490. struct ast_tone_zone_sound *ts;
  491. if (ast_strlen_zero(indication) || ast_strlen_zero(tonelist)) {
  492. return -1;
  493. }
  494. AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
  495. if (!strcasecmp(indication, ts->name)) {
  496. AST_LIST_REMOVE_CURRENT(entry);
  497. ts = ast_tone_zone_sound_unref(ts);
  498. break;
  499. }
  500. }
  501. AST_LIST_TRAVERSE_SAFE_END;
  502. if (!(ts = ao2_alloc(sizeof(*ts), ast_tone_zone_sound_destructor))) {
  503. return -1;
  504. }
  505. if (!(ts->name = ast_strdup(indication)) || !(ts->data = ast_strdup(tonelist))) {
  506. ts = ast_tone_zone_sound_unref(ts);
  507. return -1;
  508. }
  509. AST_LIST_INSERT_TAIL(&zone->tones, ts, entry); /* Inherit reference */
  510. return 0;
  511. }
  512. /*! \brief remove an existing country's indication. Both country and indication must exist */
  513. static int ast_unregister_indication(struct ast_tone_zone *zone, const char *indication)
  514. {
  515. struct ast_tone_zone_sound *ts;
  516. int res = -1;
  517. ast_tone_zone_lock(zone);
  518. AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
  519. if (!strcasecmp(indication, ts->name)) {
  520. AST_LIST_REMOVE_CURRENT(entry);
  521. ts = ast_tone_zone_sound_unref(ts);
  522. res = 0;
  523. break;
  524. }
  525. }
  526. AST_LIST_TRAVERSE_SAFE_END;
  527. ast_tone_zone_unlock(zone);
  528. return res;
  529. }
  530. static struct ast_tone_zone *ast_tone_zone_alloc(void)
  531. {
  532. return ao2_alloc(sizeof(struct ast_tone_zone), ast_tone_zone_destructor);
  533. }
  534. static char *complete_country(struct ast_cli_args *a)
  535. {
  536. char *res = NULL;
  537. struct ao2_iterator i;
  538. int which = 0;
  539. size_t wordlen;
  540. struct ast_tone_zone *tz;
  541. wordlen = strlen(a->word);
  542. i = ao2_iterator_init(ast_tone_zones, 0);
  543. while ((tz = ao2_iterator_next(&i))) {
  544. if (!strncasecmp(a->word, tz->country, wordlen) && ++which > a->n) {
  545. res = ast_strdup(tz->country);
  546. }
  547. tz = ast_tone_zone_unref(tz);
  548. if (res) {
  549. break;
  550. }
  551. }
  552. ao2_iterator_destroy(&i);
  553. return res;
  554. }
  555. static char *handle_cli_indication_add(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  556. {
  557. struct ast_tone_zone *tz;
  558. int created_country = 0;
  559. char *res = CLI_SUCCESS;
  560. switch (cmd) {
  561. case CLI_INIT:
  562. e->command = "indication add";
  563. e->usage =
  564. "Usage: indication add <country> <indication> \"<tonelist>\"\n"
  565. " Add the given indication to the country.\n";
  566. return NULL;
  567. case CLI_GENERATE:
  568. if (a->pos == 2) {
  569. return complete_country(a);
  570. } else {
  571. return NULL;
  572. }
  573. }
  574. if (a->argc != 5) {
  575. return CLI_SHOWUSAGE;
  576. }
  577. if (!(tz = ast_get_indication_zone(a->argv[2]))) {
  578. /* country does not exist, create it */
  579. ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n", a->argv[2]);
  580. if (!(tz = ast_tone_zone_alloc())) {
  581. return CLI_FAILURE;
  582. }
  583. ast_copy_string(tz->country, a->argv[2], sizeof(tz->country));
  584. if (ast_register_indication_country(tz)) {
  585. ast_log(LOG_WARNING, "Unable to register new country\n");
  586. tz = ast_tone_zone_unref(tz);
  587. return CLI_FAILURE;
  588. }
  589. created_country = 1;
  590. }
  591. ast_tone_zone_lock(tz);
  592. if (ast_register_indication(tz, a->argv[3], a->argv[4])) {
  593. ast_log(LOG_WARNING, "Unable to register indication %s/%s\n", a->argv[2], a->argv[3]);
  594. if (created_country) {
  595. ast_unregister_indication_country(a->argv[2]);
  596. }
  597. res = CLI_FAILURE;
  598. }
  599. ast_tone_zone_unlock(tz);
  600. tz = ast_tone_zone_unref(tz);
  601. return res;
  602. }
  603. static char *complete_indications(struct ast_cli_args *a)
  604. {
  605. char *res = NULL;
  606. int which = 0;
  607. size_t wordlen;
  608. struct ast_tone_zone_sound *ts;
  609. struct ast_tone_zone *tz, tmp_tz = {
  610. .nrringcadence = 0,
  611. };
  612. ast_copy_string(tmp_tz.country, a->argv[a->pos - 1], sizeof(tmp_tz.country));
  613. if (!(tz = ao2_find(ast_tone_zones, &tmp_tz, OBJ_POINTER))) {
  614. return NULL;
  615. }
  616. wordlen = strlen(a->word);
  617. ast_tone_zone_lock(tz);
  618. AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
  619. if (!strncasecmp(a->word, ts->name, wordlen) && ++which > a->n) {
  620. res = ast_strdup(ts->name);
  621. break;
  622. }
  623. }
  624. ast_tone_zone_unlock(tz);
  625. tz = ast_tone_zone_unref(tz);
  626. return res;
  627. }
  628. static char *handle_cli_indication_remove(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  629. {
  630. struct ast_tone_zone *tz;
  631. char *res = CLI_SUCCESS;
  632. switch (cmd) {
  633. case CLI_INIT:
  634. e->command = "indication remove";
  635. e->usage =
  636. "Usage: indication remove <country> [indication]\n"
  637. " Remove the given indication from the country.\n";
  638. return NULL;
  639. case CLI_GENERATE:
  640. if (a->pos == 2) {
  641. return complete_country(a);
  642. } else if (a->pos == 3) {
  643. return complete_indications(a);
  644. }
  645. }
  646. if (a->argc != 3 && a->argc != 4) {
  647. return CLI_SHOWUSAGE;
  648. }
  649. if (a->argc == 3) {
  650. /* remove entire country */
  651. if (ast_unregister_indication_country(a->argv[2])) {
  652. ast_log(LOG_WARNING, "Unable to unregister indication country %s\n", a->argv[2]);
  653. return CLI_FAILURE;
  654. }
  655. return CLI_SUCCESS;
  656. }
  657. if (!(tz = ast_get_indication_zone(a->argv[2]))) {
  658. ast_log(LOG_WARNING, "Unable to unregister indication %s/%s, country does not exists\n", a->argv[2], a->argv[3]);
  659. return CLI_FAILURE;
  660. }
  661. if (ast_unregister_indication(tz, a->argv[3])) {
  662. ast_log(LOG_WARNING, "Unable to unregister indication %s/%s\n", a->argv[2], a->argv[3]);
  663. res = CLI_FAILURE;
  664. }
  665. tz = ast_tone_zone_unref(tz);
  666. return res;
  667. }
  668. static char *handle_cli_indication_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  669. {
  670. struct ast_tone_zone *tz = NULL;
  671. struct ast_str *buf;
  672. int found_country = 0;
  673. int i;
  674. switch (cmd) {
  675. case CLI_INIT:
  676. e->command = "indication show";
  677. e->usage =
  678. "Usage: indication show [<country> ...]\n"
  679. " Display either a condensed summary of all countries and indications, or a\n"
  680. " more verbose list of indications for the specified countries.\n";
  681. return NULL;
  682. case CLI_GENERATE:
  683. return complete_country(a);
  684. }
  685. if (a->argc == 2) {
  686. struct ao2_iterator iter;
  687. /* no arguments, show a list of countries */
  688. ast_cli(a->fd, "Country Description\n");
  689. ast_cli(a->fd, "===========================\n");
  690. iter = ast_tone_zone_iterator_init();
  691. while ((tz = ao2_iterator_next(&iter))) {
  692. ast_tone_zone_lock(tz);
  693. ast_cli(a->fd, "%-7.7s %s\n", tz->country, tz->description);
  694. ast_tone_zone_unlock(tz);
  695. tz = ast_tone_zone_unref(tz);
  696. }
  697. ao2_iterator_destroy(&iter);
  698. return CLI_SUCCESS;
  699. }
  700. buf = ast_str_alloca(256);
  701. for (i = 2; i < a->argc; i++) {
  702. struct ast_tone_zone zone_arg = {
  703. .nrringcadence = 0,
  704. };
  705. struct ast_tone_zone_sound *ts;
  706. int j;
  707. ast_copy_string(zone_arg.country, a->argv[i], sizeof(zone_arg.country));
  708. if (!(tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER))) {
  709. continue;
  710. }
  711. if (!found_country) {
  712. found_country = 1;
  713. ast_cli(a->fd, "Country Indication PlayList\n");
  714. ast_cli(a->fd, "=====================================\n");
  715. }
  716. ast_tone_zone_lock(tz);
  717. ast_str_set(&buf, 0, "%-7.7s %-15.15s ", tz->country, "<ringcadence>");
  718. for (j = 0; j < tz->nrringcadence; j++) {
  719. ast_str_append(&buf, 0, "%d%s", tz->ringcadence[j],
  720. (j == tz->nrringcadence - 1) ? "" : ",");
  721. }
  722. ast_str_append(&buf, 0, "\n");
  723. ast_cli(a->fd, "%s", ast_str_buffer(buf));
  724. AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
  725. ast_cli(a->fd, "%-7.7s %-15.15s %s\n", tz->country, ts->name, ts->data);
  726. }
  727. ast_tone_zone_unlock(tz);
  728. tz = ast_tone_zone_unref(tz);
  729. }
  730. if (!found_country) {
  731. ast_cli(a->fd, "No countries matched your criteria.\n");
  732. }
  733. return CLI_SUCCESS;
  734. }
  735. static int is_valid_tone_zone(struct ast_tone_zone *zone)
  736. {
  737. int res;
  738. ast_tone_zone_lock(zone);
  739. res = (!ast_strlen_zero(zone->description) && !AST_LIST_EMPTY(&zone->tones));
  740. ast_tone_zone_unlock(zone);
  741. return res;
  742. }
  743. /*!\brief
  744. *
  745. * \note This is called with the tone zone locked.
  746. */
  747. static void store_tone_zone_ring_cadence(struct ast_tone_zone *zone, const char *val)
  748. {
  749. char buf[1024];
  750. char *ring, *c = buf;
  751. ast_copy_string(buf, val, sizeof(buf));
  752. while ((ring = strsep(&c, ","))) {
  753. int *tmp, val;
  754. ring = ast_strip(ring);
  755. if (!isdigit(ring[0]) || (val = atoi(ring)) == -1) {
  756. ast_log(LOG_WARNING, "Invalid ringcadence given '%s'.\n", ring);
  757. continue;
  758. }
  759. if (!(tmp = ast_realloc(zone->ringcadence, (zone->nrringcadence + 1) * sizeof(int)))) {
  760. return;
  761. }
  762. zone->ringcadence = tmp;
  763. tmp[zone->nrringcadence] = val;
  764. zone->nrringcadence++;
  765. }
  766. }
  767. static void store_config_tone_zone(struct ast_tone_zone *zone, const char *var,
  768. const char *value)
  769. {
  770. CV_START(var, value);
  771. CV_STR("description", zone->description);
  772. CV_F("ringcadence", store_tone_zone_ring_cadence(zone, value));
  773. ast_register_indication(zone, var, value);
  774. CV_END;
  775. }
  776. static void reset_tone_zone(struct ast_tone_zone *zone)
  777. {
  778. ast_tone_zone_lock(zone);
  779. zone->killme = 0;
  780. if (zone->nrringcadence) {
  781. zone->nrringcadence = 0;
  782. ast_free(zone->ringcadence);
  783. zone->ringcadence = NULL;
  784. }
  785. ast_tone_zone_unlock(zone);
  786. }
  787. static int parse_tone_zone(struct ast_config *cfg, const char *country)
  788. {
  789. struct ast_variable *v;
  790. struct ast_tone_zone *zone;
  791. struct ast_tone_zone tmp_zone = {
  792. .nrringcadence = 0,
  793. };
  794. int allocd = 0;
  795. ast_copy_string(tmp_zone.country, country, sizeof(tmp_zone.country));
  796. if ((zone = ao2_find(ast_tone_zones, &tmp_zone, OBJ_POINTER))) {
  797. reset_tone_zone(zone);
  798. } else if ((zone = ast_tone_zone_alloc())) {
  799. allocd = 1;
  800. ast_copy_string(zone->country, country, sizeof(zone->country));
  801. } else {
  802. return -1;
  803. }
  804. ast_tone_zone_lock(zone);
  805. for (v = ast_variable_browse(cfg, country); v; v = v->next) {
  806. store_config_tone_zone(zone, v->name, v->value);
  807. }
  808. ast_tone_zone_unlock(zone);
  809. if (allocd) {
  810. if (!is_valid_tone_zone(zone)) {
  811. ast_log(LOG_WARNING, "Indication country '%s' is invalid\n", country);
  812. } else if (ast_register_indication_country(zone)) {
  813. ast_log(LOG_WARNING, "Unable to register indication country '%s'.\n",
  814. country);
  815. }
  816. }
  817. zone = ast_tone_zone_unref(zone);
  818. return 0;
  819. }
  820. /*! \brief
  821. * Mark the zone and its tones before parsing configuration. We will use this
  822. * to know what to remove after configuration is parsed.
  823. */
  824. static int tone_zone_mark(void *obj, void *arg, int flags)
  825. {
  826. struct ast_tone_zone *zone = obj;
  827. struct ast_tone_zone_sound *s;
  828. ast_tone_zone_lock(zone);
  829. zone->killme = 1;
  830. AST_LIST_TRAVERSE(&zone->tones, s, entry) {
  831. s->killme = 1;
  832. }
  833. ast_tone_zone_unlock(zone);
  834. return 0;
  835. }
  836. /*! \brief
  837. * Prune tones no longer in the configuration, and have the tone zone unlinked
  838. * if it is no longer in the configuration at all.
  839. */
  840. static int prune_tone_zone(void *obj, void *arg, int flags)
  841. {
  842. struct ast_tone_zone *zone = obj;
  843. struct ast_tone_zone_sound *s;
  844. ast_tone_zone_lock(zone);
  845. AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, s, entry) {
  846. if (s->killme) {
  847. AST_LIST_REMOVE_CURRENT(entry);
  848. s = ast_tone_zone_sound_unref(s);
  849. }
  850. }
  851. AST_LIST_TRAVERSE_SAFE_END;
  852. ast_tone_zone_unlock(zone);
  853. return zone->killme ? CMP_MATCH : 0;
  854. }
  855. /*! \brief load indications module */
  856. static int load_indications(int reload)
  857. {
  858. struct ast_config *cfg;
  859. const char *cxt = NULL;
  860. const char *country = NULL;
  861. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  862. int res = -1;
  863. cfg = ast_config_load2(config, "indications", config_flags);
  864. if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
  865. ast_log(LOG_WARNING, "Can't find indications config file %s.\n", config);
  866. return 0;
  867. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  868. return 0;
  869. }
  870. /* Lock the container to prevent multiple simultaneous reloads */
  871. ao2_lock(ast_tone_zones);
  872. ao2_callback(ast_tone_zones, OBJ_NODATA, tone_zone_mark, NULL);
  873. /* Use existing config to populate the Indication table */
  874. while ((cxt = ast_category_browse(cfg, cxt))) {
  875. /* All categories but "general" are considered countries */
  876. if (!strcasecmp(cxt, "general")) {
  877. continue;
  878. }
  879. if (parse_tone_zone(cfg, cxt)) {
  880. goto return_cleanup;
  881. }
  882. }
  883. ao2_callback(ast_tone_zones, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK,
  884. prune_tone_zone, NULL);
  885. /* determine which country is the default */
  886. country = ast_variable_retrieve(cfg, "general", "country");
  887. if (ast_strlen_zero(country) || ast_set_indication_country(country)) {
  888. ast_log(LOG_WARNING, "Unable to set the default country (for indication tones)\n");
  889. }
  890. res = 0;
  891. return_cleanup:
  892. ao2_unlock(ast_tone_zones);
  893. ast_config_destroy(cfg);
  894. return res;
  895. }
  896. /*! \brief CLI entries for commands provided by this module */
  897. static struct ast_cli_entry cli_indications[] = {
  898. AST_CLI_DEFINE(handle_cli_indication_add, "Add the given indication to the country"),
  899. AST_CLI_DEFINE(handle_cli_indication_remove, "Remove the given indication from the country"),
  900. AST_CLI_DEFINE(handle_cli_indication_show, "Display a list of all countries/indications")
  901. };
  902. static int ast_tone_zone_hash(const void *obj, const int flags)
  903. {
  904. const struct ast_tone_zone *zone = obj;
  905. return ast_str_case_hash(zone->country);
  906. }
  907. static int ast_tone_zone_cmp(void *obj, void *arg, int flags)
  908. {
  909. struct ast_tone_zone *zone = obj;
  910. struct ast_tone_zone *zone_arg = arg;
  911. return (!strcasecmp(zone->country, zone_arg->country)) ?
  912. CMP_MATCH | CMP_STOP : 0;
  913. }
  914. int ast_tone_zone_data_add_structure(struct ast_data *tree, struct ast_tone_zone *zone)
  915. {
  916. struct ast_data *data_zone_sound;
  917. struct ast_tone_zone_sound *s;
  918. ast_data_add_structure(ast_tone_zone, tree, zone);
  919. if (AST_LIST_EMPTY(&zone->tones)) {
  920. return 0;
  921. }
  922. data_zone_sound = ast_data_add_node(tree, "tones");
  923. if (!data_zone_sound) {
  924. return -1;
  925. }
  926. ast_tone_zone_lock(zone);
  927. AST_LIST_TRAVERSE(&zone->tones, s, entry) {
  928. ast_data_add_structure(ast_tone_zone_sound, data_zone_sound, s);
  929. }
  930. ast_tone_zone_unlock(zone);
  931. return 0;
  932. }
  933. /*!
  934. * \internal
  935. * \brief Clean up resources on Asterisk shutdown
  936. */
  937. static void indications_shutdown(void)
  938. {
  939. ast_cli_unregister_multiple(cli_indications, ARRAY_LEN(cli_indications));
  940. if (default_tone_zone) {
  941. ast_tone_zone_unref(default_tone_zone);
  942. default_tone_zone = NULL;
  943. }
  944. if (ast_tone_zones) {
  945. ao2_ref(ast_tone_zones, -1);
  946. ast_tone_zones = NULL;
  947. }
  948. }
  949. /*! \brief Load indications module */
  950. int ast_indications_init(void)
  951. {
  952. if (!(ast_tone_zones = ao2_container_alloc(NUM_TONE_ZONE_BUCKETS,
  953. ast_tone_zone_hash, ast_tone_zone_cmp))) {
  954. return -1;
  955. }
  956. if (load_indications(0)) {
  957. indications_shutdown();
  958. return -1;
  959. }
  960. ast_cli_register_multiple(cli_indications, ARRAY_LEN(cli_indications));
  961. ast_register_cleanup(indications_shutdown);
  962. return 0;
  963. }
  964. /*! \brief Reload indications module */
  965. int ast_indications_reload(void)
  966. {
  967. return load_indications(1);
  968. }