indications.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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. ts = ao2_alloc_options(sizeof(*ts), ast_tone_zone_sound_destructor,
  503. AO2_ALLOC_OPT_LOCK_NOLOCK);
  504. if (!ts) {
  505. return -1;
  506. }
  507. if (!(ts->name = ast_strdup(indication)) || !(ts->data = ast_strdup(tonelist))) {
  508. ts = ast_tone_zone_sound_unref(ts);
  509. return -1;
  510. }
  511. AST_LIST_INSERT_TAIL(&zone->tones, ts, entry); /* Inherit reference */
  512. return 0;
  513. }
  514. /*! \brief remove an existing country's indication. Both country and indication must exist */
  515. static int ast_unregister_indication(struct ast_tone_zone *zone, const char *indication)
  516. {
  517. struct ast_tone_zone_sound *ts;
  518. int res = -1;
  519. ast_tone_zone_lock(zone);
  520. AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
  521. if (!strcasecmp(indication, ts->name)) {
  522. AST_LIST_REMOVE_CURRENT(entry);
  523. ts = ast_tone_zone_sound_unref(ts);
  524. res = 0;
  525. break;
  526. }
  527. }
  528. AST_LIST_TRAVERSE_SAFE_END;
  529. ast_tone_zone_unlock(zone);
  530. return res;
  531. }
  532. static struct ast_tone_zone *ast_tone_zone_alloc(void)
  533. {
  534. return ao2_alloc(sizeof(struct ast_tone_zone), ast_tone_zone_destructor);
  535. }
  536. static char *complete_country(struct ast_cli_args *a)
  537. {
  538. struct ao2_iterator i;
  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)) {
  545. if (ast_cli_completion_add(ast_strdup(tz->country))) {
  546. ast_tone_zone_unref(tz);
  547. break;
  548. }
  549. }
  550. ast_tone_zone_unref(tz);
  551. }
  552. ao2_iterator_destroy(&i);
  553. return NULL;
  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. size_t wordlen;
  606. struct ast_tone_zone_sound *ts;
  607. struct ast_tone_zone *tz;
  608. struct ast_tone_zone tmp_tz = {
  609. .nrringcadence = 0,
  610. };
  611. ast_copy_string(tmp_tz.country, a->argv[a->pos - 1], sizeof(tmp_tz.country));
  612. tz = ao2_find(ast_tone_zones, &tmp_tz, OBJ_POINTER);
  613. if (!tz) {
  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)) {
  620. if (ast_cli_completion_add(ast_strdup(ts->name))) {
  621. break;
  622. }
  623. }
  624. }
  625. ast_tone_zone_unlock(tz);
  626. tz = ast_tone_zone_unref(tz);
  627. return NULL;
  628. }
  629. static char *handle_cli_indication_remove(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  630. {
  631. struct ast_tone_zone *tz;
  632. char *res = CLI_SUCCESS;
  633. switch (cmd) {
  634. case CLI_INIT:
  635. e->command = "indication remove";
  636. e->usage =
  637. "Usage: indication remove <country> [indication]\n"
  638. " Remove the given indication from the country.\n";
  639. return NULL;
  640. case CLI_GENERATE:
  641. if (a->pos == 2) {
  642. return complete_country(a);
  643. } else if (a->pos == 3) {
  644. return complete_indications(a);
  645. }
  646. }
  647. if (a->argc != 3 && a->argc != 4) {
  648. return CLI_SHOWUSAGE;
  649. }
  650. if (a->argc == 3) {
  651. /* remove entire country */
  652. if (ast_unregister_indication_country(a->argv[2])) {
  653. ast_log(LOG_WARNING, "Unable to unregister indication country %s\n", a->argv[2]);
  654. return CLI_FAILURE;
  655. }
  656. return CLI_SUCCESS;
  657. }
  658. if (!(tz = ast_get_indication_zone(a->argv[2]))) {
  659. ast_log(LOG_WARNING, "Unable to unregister indication %s/%s, country does not exists\n", a->argv[2], a->argv[3]);
  660. return CLI_FAILURE;
  661. }
  662. if (ast_unregister_indication(tz, a->argv[3])) {
  663. ast_log(LOG_WARNING, "Unable to unregister indication %s/%s\n", a->argv[2], a->argv[3]);
  664. res = CLI_FAILURE;
  665. }
  666. tz = ast_tone_zone_unref(tz);
  667. return res;
  668. }
  669. static char *handle_cli_indication_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  670. {
  671. struct ast_tone_zone *tz = NULL;
  672. struct ast_str *buf;
  673. int found_country = 0;
  674. int i;
  675. switch (cmd) {
  676. case CLI_INIT:
  677. e->command = "indication show";
  678. e->usage =
  679. "Usage: indication show [<country> ...]\n"
  680. " Display either a condensed summary of all countries and indications, or a\n"
  681. " more verbose list of indications for the specified countries.\n";
  682. return NULL;
  683. case CLI_GENERATE:
  684. return complete_country(a);
  685. }
  686. if (a->argc == 2) {
  687. struct ao2_iterator iter;
  688. /* no arguments, show a list of countries */
  689. ast_cli(a->fd, "Country Description\n");
  690. ast_cli(a->fd, "===========================\n");
  691. iter = ast_tone_zone_iterator_init();
  692. while ((tz = ao2_iterator_next(&iter))) {
  693. ast_tone_zone_lock(tz);
  694. ast_cli(a->fd, "%-7.7s %s\n", tz->country, tz->description);
  695. ast_tone_zone_unlock(tz);
  696. tz = ast_tone_zone_unref(tz);
  697. }
  698. ao2_iterator_destroy(&iter);
  699. return CLI_SUCCESS;
  700. }
  701. buf = ast_str_alloca(256);
  702. for (i = 2; i < a->argc; i++) {
  703. struct ast_tone_zone zone_arg = {
  704. .nrringcadence = 0,
  705. };
  706. struct ast_tone_zone_sound *ts;
  707. int j;
  708. ast_copy_string(zone_arg.country, a->argv[i], sizeof(zone_arg.country));
  709. if (!(tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER))) {
  710. continue;
  711. }
  712. if (!found_country) {
  713. found_country = 1;
  714. ast_cli(a->fd, "Country Indication PlayList\n");
  715. ast_cli(a->fd, "=====================================\n");
  716. }
  717. ast_tone_zone_lock(tz);
  718. ast_str_set(&buf, 0, "%-7.7s %-15.15s ", tz->country, "<ringcadence>");
  719. for (j = 0; j < tz->nrringcadence; j++) {
  720. ast_str_append(&buf, 0, "%d%s", tz->ringcadence[j],
  721. (j == tz->nrringcadence - 1) ? "" : ",");
  722. }
  723. ast_str_append(&buf, 0, "\n");
  724. ast_cli(a->fd, "%s", ast_str_buffer(buf));
  725. AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
  726. ast_cli(a->fd, "%-7.7s %-15.15s %s\n", tz->country, ts->name, ts->data);
  727. }
  728. ast_tone_zone_unlock(tz);
  729. tz = ast_tone_zone_unref(tz);
  730. }
  731. if (!found_country) {
  732. ast_cli(a->fd, "No countries matched your criteria.\n");
  733. }
  734. return CLI_SUCCESS;
  735. }
  736. static int is_valid_tone_zone(struct ast_tone_zone *zone)
  737. {
  738. int res;
  739. ast_tone_zone_lock(zone);
  740. res = (!ast_strlen_zero(zone->description) && !AST_LIST_EMPTY(&zone->tones));
  741. ast_tone_zone_unlock(zone);
  742. return res;
  743. }
  744. /*!\brief
  745. *
  746. * \note This is called with the tone zone locked.
  747. */
  748. static void store_tone_zone_ring_cadence(struct ast_tone_zone *zone, const char *val)
  749. {
  750. char buf[1024];
  751. char *ring, *c = buf;
  752. ast_copy_string(buf, val, sizeof(buf));
  753. while ((ring = strsep(&c, ","))) {
  754. int *tmp, val;
  755. ring = ast_strip(ring);
  756. if (!isdigit(ring[0]) || (val = atoi(ring)) == -1) {
  757. ast_log(LOG_WARNING, "Invalid ringcadence given '%s'.\n", ring);
  758. continue;
  759. }
  760. if (!(tmp = ast_realloc(zone->ringcadence, (zone->nrringcadence + 1) * sizeof(int)))) {
  761. return;
  762. }
  763. zone->ringcadence = tmp;
  764. tmp[zone->nrringcadence] = val;
  765. zone->nrringcadence++;
  766. }
  767. }
  768. static void store_config_tone_zone(struct ast_tone_zone *zone, const char *var,
  769. const char *value)
  770. {
  771. CV_START(var, value);
  772. CV_STR("description", zone->description);
  773. CV_F("ringcadence", store_tone_zone_ring_cadence(zone, value));
  774. ast_register_indication(zone, var, value);
  775. CV_END;
  776. }
  777. static void reset_tone_zone(struct ast_tone_zone *zone)
  778. {
  779. ast_tone_zone_lock(zone);
  780. zone->killme = 0;
  781. if (zone->nrringcadence) {
  782. zone->nrringcadence = 0;
  783. ast_free(zone->ringcadence);
  784. zone->ringcadence = NULL;
  785. }
  786. ast_tone_zone_unlock(zone);
  787. }
  788. static int parse_tone_zone(struct ast_config *cfg, const char *country)
  789. {
  790. struct ast_variable *v;
  791. struct ast_tone_zone *zone;
  792. struct ast_tone_zone tmp_zone = {
  793. .nrringcadence = 0,
  794. };
  795. int allocd = 0;
  796. ast_copy_string(tmp_zone.country, country, sizeof(tmp_zone.country));
  797. if ((zone = ao2_find(ast_tone_zones, &tmp_zone, OBJ_POINTER))) {
  798. reset_tone_zone(zone);
  799. } else if ((zone = ast_tone_zone_alloc())) {
  800. allocd = 1;
  801. ast_copy_string(zone->country, country, sizeof(zone->country));
  802. } else {
  803. return -1;
  804. }
  805. ast_tone_zone_lock(zone);
  806. for (v = ast_variable_browse(cfg, country); v; v = v->next) {
  807. store_config_tone_zone(zone, v->name, v->value);
  808. }
  809. ast_tone_zone_unlock(zone);
  810. if (allocd) {
  811. if (!is_valid_tone_zone(zone)) {
  812. ast_log(LOG_WARNING, "Indication country '%s' is invalid\n", country);
  813. } else if (ast_register_indication_country(zone)) {
  814. ast_log(LOG_WARNING, "Unable to register indication country '%s'.\n",
  815. country);
  816. }
  817. }
  818. zone = ast_tone_zone_unref(zone);
  819. return 0;
  820. }
  821. /*! \brief
  822. * Mark the zone and its tones before parsing configuration. We will use this
  823. * to know what to remove after configuration is parsed.
  824. */
  825. static int tone_zone_mark(void *obj, void *arg, int flags)
  826. {
  827. struct ast_tone_zone *zone = obj;
  828. struct ast_tone_zone_sound *s;
  829. ast_tone_zone_lock(zone);
  830. zone->killme = 1;
  831. AST_LIST_TRAVERSE(&zone->tones, s, entry) {
  832. s->killme = 1;
  833. }
  834. ast_tone_zone_unlock(zone);
  835. return 0;
  836. }
  837. /*! \brief
  838. * Prune tones no longer in the configuration, and have the tone zone unlinked
  839. * if it is no longer in the configuration at all.
  840. */
  841. static int prune_tone_zone(void *obj, void *arg, int flags)
  842. {
  843. struct ast_tone_zone *zone = obj;
  844. struct ast_tone_zone_sound *s;
  845. ast_tone_zone_lock(zone);
  846. AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, s, entry) {
  847. if (s->killme) {
  848. AST_LIST_REMOVE_CURRENT(entry);
  849. s = ast_tone_zone_sound_unref(s);
  850. }
  851. }
  852. AST_LIST_TRAVERSE_SAFE_END;
  853. ast_tone_zone_unlock(zone);
  854. return zone->killme ? CMP_MATCH : 0;
  855. }
  856. /*! \brief load indications module */
  857. static int load_indications(int reload)
  858. {
  859. struct ast_config *cfg;
  860. const char *cxt = NULL;
  861. const char *country = NULL;
  862. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  863. int res = -1;
  864. cfg = ast_config_load2(config, "indications", config_flags);
  865. if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
  866. ast_log(LOG_WARNING, "Can't find indications config file %s.\n", config);
  867. return 0;
  868. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  869. return 0;
  870. }
  871. /* Lock the container to prevent multiple simultaneous reloads */
  872. ao2_lock(ast_tone_zones);
  873. ao2_callback(ast_tone_zones, OBJ_NODATA, tone_zone_mark, NULL);
  874. /* Use existing config to populate the Indication table */
  875. while ((cxt = ast_category_browse(cfg, cxt))) {
  876. /* All categories but "general" are considered countries */
  877. if (!strcasecmp(cxt, "general")) {
  878. continue;
  879. }
  880. if (parse_tone_zone(cfg, cxt)) {
  881. goto return_cleanup;
  882. }
  883. }
  884. ao2_callback(ast_tone_zones, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK,
  885. prune_tone_zone, NULL);
  886. /* determine which country is the default */
  887. country = ast_variable_retrieve(cfg, "general", "country");
  888. if (ast_strlen_zero(country) || ast_set_indication_country(country)) {
  889. ast_log(LOG_WARNING, "Unable to set the default country (for indication tones)\n");
  890. }
  891. res = 0;
  892. return_cleanup:
  893. ao2_unlock(ast_tone_zones);
  894. ast_config_destroy(cfg);
  895. return res;
  896. }
  897. /*! \brief CLI entries for commands provided by this module */
  898. static struct ast_cli_entry cli_indications[] = {
  899. AST_CLI_DEFINE(handle_cli_indication_add, "Add the given indication to the country"),
  900. AST_CLI_DEFINE(handle_cli_indication_remove, "Remove the given indication from the country"),
  901. AST_CLI_DEFINE(handle_cli_indication_show, "Display a list of all countries/indications")
  902. };
  903. static int ast_tone_zone_hash(const void *obj, const int flags)
  904. {
  905. const struct ast_tone_zone *zone = obj;
  906. return ast_str_case_hash(zone->country);
  907. }
  908. static int ast_tone_zone_cmp(void *obj, void *arg, int flags)
  909. {
  910. struct ast_tone_zone *zone = obj;
  911. struct ast_tone_zone *zone_arg = arg;
  912. return (!strcasecmp(zone->country, zone_arg->country)) ?
  913. CMP_MATCH | CMP_STOP : 0;
  914. }
  915. int ast_tone_zone_data_add_structure(struct ast_data *tree, struct ast_tone_zone *zone)
  916. {
  917. struct ast_data *data_zone_sound;
  918. struct ast_tone_zone_sound *s;
  919. ast_data_add_structure(ast_tone_zone, tree, zone);
  920. if (AST_LIST_EMPTY(&zone->tones)) {
  921. return 0;
  922. }
  923. data_zone_sound = ast_data_add_node(tree, "tones");
  924. if (!data_zone_sound) {
  925. return -1;
  926. }
  927. ast_tone_zone_lock(zone);
  928. AST_LIST_TRAVERSE(&zone->tones, s, entry) {
  929. ast_data_add_structure(ast_tone_zone_sound, data_zone_sound, s);
  930. }
  931. ast_tone_zone_unlock(zone);
  932. return 0;
  933. }
  934. /*!
  935. * \internal
  936. * \brief Clean up resources on Asterisk shutdown
  937. */
  938. static void indications_shutdown(void)
  939. {
  940. ast_cli_unregister_multiple(cli_indications, ARRAY_LEN(cli_indications));
  941. if (default_tone_zone) {
  942. ast_tone_zone_unref(default_tone_zone);
  943. default_tone_zone = NULL;
  944. }
  945. if (ast_tone_zones) {
  946. ao2_ref(ast_tone_zones, -1);
  947. ast_tone_zones = NULL;
  948. }
  949. }
  950. /*! \brief Load indications module */
  951. int ast_indications_init(void)
  952. {
  953. ast_tone_zones = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  954. NUM_TONE_ZONE_BUCKETS, ast_tone_zone_hash, NULL, ast_tone_zone_cmp);
  955. if (!ast_tone_zones) {
  956. return -1;
  957. }
  958. if (load_indications(0)) {
  959. return -1;
  960. }
  961. ast_cli_register_multiple(cli_indications, ARRAY_LEN(cli_indications));
  962. ast_register_cleanup(indications_shutdown);
  963. return 0;
  964. }
  965. /*! \brief Reload indications module */
  966. int ast_indications_reload(void)
  967. {
  968. return load_indications(1);
  969. }