indications.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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/channel.h"
  35. #include "asterisk/utils.h"
  36. #include "asterisk/cli.h"
  37. #include "asterisk/module.h"
  38. #include "asterisk/astobj2.h"
  39. #include "asterisk/data.h"
  40. #include "asterisk/_private.h" /* _init(), _reload() */
  41. #define DATA_EXPORT_TONE_ZONE(MEMBER) \
  42. MEMBER(ast_tone_zone, country, AST_DATA_STRING) \
  43. MEMBER(ast_tone_zone, description, AST_DATA_STRING) \
  44. MEMBER(ast_tone_zone, nrringcadence, AST_DATA_UNSIGNED_INTEGER)
  45. AST_DATA_STRUCTURE(ast_tone_zone, DATA_EXPORT_TONE_ZONE);
  46. #define DATA_EXPORT_TONE_ZONE_SOUND(MEMBER) \
  47. MEMBER(ast_tone_zone_sound, name, AST_DATA_STRING) \
  48. MEMBER(ast_tone_zone_sound, data, AST_DATA_STRING)
  49. AST_DATA_STRUCTURE(ast_tone_zone_sound, DATA_EXPORT_TONE_ZONE_SOUND);
  50. /* Globals */
  51. static const char config[] = "indications.conf";
  52. static const int midi_tohz[128] = {
  53. 8, 8, 9, 9, 10, 10, 11, 12, 12, 13,
  54. 14, 15, 16, 17, 18, 19, 20, 21, 23, 24,
  55. 25, 27, 29, 30, 32, 34, 36, 38, 41, 43,
  56. 46, 48, 51, 55, 58, 61, 65, 69, 73, 77,
  57. 82, 87, 92, 97, 103, 110, 116, 123, 130, 138,
  58. 146, 155, 164, 174, 184, 195, 207, 220, 233, 246,
  59. 261, 277, 293, 311, 329, 349, 369, 391, 415, 440,
  60. 466, 493, 523, 554, 587, 622, 659, 698, 739, 783,
  61. 830, 880, 932, 987, 1046, 1108, 1174, 1244, 1318, 1396,
  62. 1479, 1567, 1661, 1760, 1864, 1975, 2093, 2217, 2349, 2489,
  63. 2637, 2793, 2959, 3135, 3322, 3520, 3729, 3951, 4186, 4434,
  64. 4698, 4978, 5274, 5587, 5919, 6271, 6644, 7040, 7458, 7902,
  65. 8372, 8869, 9397, 9956, 10548, 11175, 11839, 12543
  66. };
  67. static struct ao2_container *ast_tone_zones;
  68. #define NUM_TONE_ZONE_BUCKETS 53
  69. /*!
  70. * \note Access to this is protected by locking the ast_tone_zones container
  71. */
  72. static struct ast_tone_zone *default_tone_zone;
  73. struct playtones_item {
  74. int fac1;
  75. int init_v2_1;
  76. int init_v3_1;
  77. int fac2;
  78. int init_v2_2;
  79. int init_v3_2;
  80. int modulate;
  81. int duration;
  82. };
  83. struct playtones_def {
  84. int vol;
  85. int reppos;
  86. int nitems;
  87. int interruptible;
  88. struct playtones_item *items;
  89. };
  90. struct playtones_state {
  91. int vol;
  92. int v1_1;
  93. int v2_1;
  94. int v3_1;
  95. int v1_2;
  96. int v2_2;
  97. int v3_2;
  98. int reppos;
  99. int nitems;
  100. struct playtones_item *items;
  101. int npos;
  102. int oldnpos;
  103. int pos;
  104. struct ast_format origwfmt;
  105. struct ast_frame f;
  106. unsigned char offset[AST_FRIENDLY_OFFSET];
  107. short data[4000];
  108. };
  109. static void playtones_release(struct ast_channel *chan, void *params)
  110. {
  111. struct playtones_state *ps = params;
  112. if (chan) {
  113. ast_set_write_format(chan, &ps->origwfmt);
  114. }
  115. if (ps->items) {
  116. ast_free(ps->items);
  117. ps->items = NULL;
  118. }
  119. ast_free(ps);
  120. }
  121. static void *playtones_alloc(struct ast_channel *chan, void *params)
  122. {
  123. struct playtones_def *pd = params;
  124. struct playtones_state *ps = NULL;
  125. if (!(ps = ast_calloc(1, sizeof(*ps)))) {
  126. return NULL;
  127. }
  128. ast_format_copy(&ps->origwfmt, ast_channel_writeformat(chan));
  129. if (ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR)) {
  130. ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", ast_channel_name(chan));
  131. playtones_release(NULL, ps);
  132. ps = NULL;
  133. } else {
  134. ps->vol = pd->vol;
  135. ps->reppos = pd->reppos;
  136. ps->nitems = pd->nitems;
  137. ps->items = pd->items;
  138. ps->oldnpos = -1;
  139. }
  140. /* Let interrupts interrupt :) */
  141. if (pd->interruptible) {
  142. ast_set_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
  143. } else {
  144. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
  145. }
  146. return ps;
  147. }
  148. static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
  149. {
  150. struct playtones_state *ps = data;
  151. struct playtones_item *pi;
  152. int x;
  153. /* we need to prepare a frame with 16 * timelen samples as we're
  154. * generating SLIN audio */
  155. len = samples * 2;
  156. if (len > sizeof(ps->data) / 2 - 1) {
  157. ast_log(LOG_WARNING, "Can't generate that much data!\n");
  158. return -1;
  159. }
  160. memset(&ps->f, 0, sizeof(ps->f));
  161. pi = &ps->items[ps->npos];
  162. if (ps->oldnpos != ps->npos) {
  163. /* Load new parameters */
  164. ps->v1_1 = 0;
  165. ps->v2_1 = pi->init_v2_1;
  166. ps->v3_1 = pi->init_v3_1;
  167. ps->v1_2 = 0;
  168. ps->v2_2 = pi->init_v2_2;
  169. ps->v3_2 = pi->init_v3_2;
  170. ps->oldnpos = ps->npos;
  171. }
  172. for (x = 0; x < samples; x++) {
  173. ps->v1_1 = ps->v2_1;
  174. ps->v2_1 = ps->v3_1;
  175. ps->v3_1 = (pi->fac1 * ps->v2_1 >> 15) - ps->v1_1;
  176. ps->v1_2 = ps->v2_2;
  177. ps->v2_2 = ps->v3_2;
  178. ps->v3_2 = (pi->fac2 * ps->v2_2 >> 15) - ps->v1_2;
  179. if (pi->modulate) {
  180. int p;
  181. p = ps->v3_2 - 32768;
  182. if (p < 0) {
  183. p = -p;
  184. }
  185. p = ((p * 9) / 10) + 1;
  186. ps->data[x] = (ps->v3_1 * p) >> 15;
  187. } else {
  188. ps->data[x] = ps->v3_1 + ps->v3_2;
  189. }
  190. }
  191. ps->f.frametype = AST_FRAME_VOICE;
  192. ast_format_set(&ps->f.subclass.format, AST_FORMAT_SLINEAR, 0);
  193. ps->f.datalen = len;
  194. ps->f.samples = samples;
  195. ps->f.offset = AST_FRIENDLY_OFFSET;
  196. ps->f.data.ptr = ps->data;
  197. if (ast_write(chan, &ps->f)) {
  198. return -1;
  199. }
  200. ps->pos += x;
  201. if (pi->duration && ps->pos >= pi->duration * 8) { /* item finished? */
  202. ps->pos = 0; /* start new item */
  203. ps->npos++;
  204. if (ps->npos >= ps->nitems) { /* last item? */
  205. if (ps->reppos == -1) { /* repeat set? */
  206. return -1;
  207. }
  208. ps->npos = ps->reppos; /* redo from top */
  209. }
  210. }
  211. return 0;
  212. }
  213. static struct ast_generator playtones = {
  214. .alloc = playtones_alloc,
  215. .release = playtones_release,
  216. .generate = playtones_generator,
  217. };
  218. int ast_tone_zone_part_parse(const char *s, struct ast_tone_zone_part *tone_data)
  219. {
  220. if (sscanf(s, "%30u+%30u/%30u", &tone_data->freq1, &tone_data->freq2,
  221. &tone_data->time) == 3) {
  222. /* f1+f2/time format */
  223. } else if (sscanf(s, "%30u+%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
  224. /* f1+f2 format */
  225. tone_data->time = 0;
  226. } else if (sscanf(s, "%30u*%30u/%30u", &tone_data->freq1, &tone_data->freq2,
  227. &tone_data->time) == 3) {
  228. /* f1*f2/time format */
  229. tone_data->modulate = 1;
  230. } else if (sscanf(s, "%30u*%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
  231. /* f1*f2 format */
  232. tone_data->time = 0;
  233. tone_data->modulate = 1;
  234. } else if (sscanf(s, "%30u/%30u", &tone_data->freq1, &tone_data->time) == 2) {
  235. /* f1/time format */
  236. tone_data->freq2 = 0;
  237. } else if (sscanf(s, "%30u", &tone_data->freq1) == 1) {
  238. /* f1 format */
  239. tone_data->freq2 = 0;
  240. tone_data->time = 0;
  241. } else if (sscanf(s, "M%30u+M%30u/%30u", &tone_data->freq1, &tone_data->freq2,
  242. &tone_data->time) == 3) {
  243. /* Mf1+Mf2/time format */
  244. tone_data->midinote = 1;
  245. } else if (sscanf(s, "M%30u+M%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
  246. /* Mf1+Mf2 format */
  247. tone_data->time = 0;
  248. tone_data->midinote = 1;
  249. } else if (sscanf(s, "M%30u*M%30u/%30u", &tone_data->freq1, &tone_data->freq2,
  250. &tone_data->time) == 3) {
  251. /* Mf1*Mf2/time format */
  252. tone_data->modulate = 1;
  253. tone_data->midinote = 1;
  254. } else if (sscanf(s, "M%30u*M%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
  255. /* Mf1*Mf2 format */
  256. tone_data->time = 0;
  257. tone_data->modulate = 1;
  258. tone_data->midinote = 1;
  259. } else if (sscanf(s, "M%30u/%30u", &tone_data->freq1, &tone_data->time) == 2) {
  260. /* Mf1/time format */
  261. tone_data->freq2 = -1;
  262. tone_data->midinote = 1;
  263. } else if (sscanf(s, "M%30u", &tone_data->freq1) == 1) {
  264. /* Mf1 format */
  265. tone_data->freq2 = -1;
  266. tone_data->time = 0;
  267. tone_data->midinote = 1;
  268. } else {
  269. return -1;
  270. }
  271. return 0;
  272. }
  273. int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
  274. {
  275. char *s, *data = ast_strdupa(playlst);
  276. struct playtones_def d = { vol, -1, 0, 1, NULL };
  277. char *stringp;
  278. char *separator;
  279. static const float sample_rate = 8000.0;
  280. static const float max_sample_val = 32768.0;
  281. if (vol < 1) {
  282. d.vol = 7219; /* Default to -8db */
  283. }
  284. d.interruptible = interruptible;
  285. stringp = data;
  286. /* check if the data is separated with '|' or with ',' by default */
  287. if (strchr(stringp,'|')) {
  288. separator = "|";
  289. } else {
  290. separator = ",";
  291. }
  292. while ((s = strsep(&stringp, separator)) && !ast_strlen_zero(s)) {
  293. struct playtones_item *new_items;
  294. struct ast_tone_zone_part tone_data = {
  295. .time = 0,
  296. };
  297. s = ast_strip(s);
  298. if (s[0]=='!') {
  299. s++;
  300. } else if (d.reppos == -1) {
  301. d.reppos = d.nitems;
  302. }
  303. if (ast_tone_zone_part_parse(s, &tone_data)) {
  304. ast_log(LOG_ERROR, "Failed to parse tone part '%s'\n", s);
  305. continue;
  306. }
  307. if (tone_data.midinote) {
  308. /* midi notes must be between 0 and 127 */
  309. if (tone_data.freq1 <= 127) {
  310. tone_data.freq1 = midi_tohz[tone_data.freq1];
  311. } else {
  312. tone_data.freq1 = 0;
  313. }
  314. if (tone_data.freq2 <= 127) {
  315. tone_data.freq2 = midi_tohz[tone_data.freq2];
  316. } else {
  317. tone_data.freq2 = 0;
  318. }
  319. }
  320. new_items = ast_realloc(d.items, (d.nitems + 1) * sizeof(*d.items));
  321. if (!new_items) {
  322. ast_free(d.items);
  323. return -1;
  324. }
  325. d.items = new_items;
  326. d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (tone_data.freq1 / sample_rate)) * max_sample_val;
  327. d.items[d.nitems].init_v2_1 = sin(-4.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
  328. d.items[d.nitems].init_v3_1 = sin(-2.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
  329. d.items[d.nitems].fac2 = 2.0 * cos(2.0 * M_PI * (tone_data.freq2 / sample_rate)) * max_sample_val;
  330. d.items[d.nitems].init_v2_2 = sin(-4.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
  331. d.items[d.nitems].init_v3_2 = sin(-2.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
  332. d.items[d.nitems].duration = tone_data.time;
  333. d.items[d.nitems].modulate = tone_data.modulate;
  334. d.nitems++;
  335. }
  336. if (!d.nitems) {
  337. ast_log(LOG_ERROR, "No valid tone parts\n");
  338. return -1;
  339. }
  340. if (ast_activate_generator(chan, &playtones, &d)) {
  341. ast_free(d.items);
  342. return -1;
  343. }
  344. return 0;
  345. }
  346. void ast_playtones_stop(struct ast_channel *chan)
  347. {
  348. ast_deactivate_generator(chan);
  349. }
  350. int ast_tone_zone_count(void)
  351. {
  352. return ao2_container_count(ast_tone_zones);
  353. }
  354. struct ao2_iterator ast_tone_zone_iterator_init(void)
  355. {
  356. return ao2_iterator_init(ast_tone_zones, 0);
  357. }
  358. /*! \brief Set global indication country
  359. If no country is specified or we are unable to find the zone, then return not found */
  360. static int ast_set_indication_country(const char *country)
  361. {
  362. struct ast_tone_zone *zone = NULL;
  363. if (ast_strlen_zero(country) || !(zone = ast_get_indication_zone(country))) {
  364. return -1;
  365. }
  366. ast_verb(3, "Setting default indication country to '%s'\n", country);
  367. ao2_lock(ast_tone_zones);
  368. if (default_tone_zone) {
  369. default_tone_zone = ast_tone_zone_unref(default_tone_zone);
  370. }
  371. default_tone_zone = ast_tone_zone_ref(zone);
  372. ao2_unlock(ast_tone_zones);
  373. zone = ast_tone_zone_unref(zone);
  374. return 0;
  375. }
  376. /*! \brief locate ast_tone_zone, given the country. if country == NULL, use the default country */
  377. struct ast_tone_zone *ast_get_indication_zone(const char *country)
  378. {
  379. struct ast_tone_zone *tz = NULL;
  380. struct ast_tone_zone zone_arg = {
  381. .nrringcadence = 0,
  382. };
  383. if (ast_strlen_zero(country)) {
  384. ao2_lock(ast_tone_zones);
  385. if (default_tone_zone) {
  386. tz = ast_tone_zone_ref(default_tone_zone);
  387. }
  388. ao2_unlock(ast_tone_zones);
  389. return tz;
  390. }
  391. ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
  392. return ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER);
  393. }
  394. struct ast_tone_zone_sound *ast_get_indication_tone(const struct ast_tone_zone *_zone, const char *indication)
  395. {
  396. struct ast_tone_zone_sound *ts = NULL;
  397. /* _zone is const to the users of the API */
  398. struct ast_tone_zone *zone = (struct ast_tone_zone *) _zone;
  399. /* If no zone is specified, use the default */
  400. if (!zone) {
  401. ao2_lock(ast_tone_zones);
  402. if (default_tone_zone) {
  403. zone = ast_tone_zone_ref(default_tone_zone);
  404. }
  405. ao2_unlock(ast_tone_zones);
  406. if (!zone) {
  407. return NULL;
  408. }
  409. }
  410. ast_tone_zone_lock(zone);
  411. /* Look through list of tones in the zone searching for the right one */
  412. AST_LIST_TRAVERSE(&zone->tones, ts, entry) {
  413. if (!strcasecmp(ts->name, indication)) {
  414. /* Increase ref count for the reference we will return */
  415. ts = ast_tone_zone_sound_ref(ts);
  416. break;
  417. }
  418. }
  419. ast_tone_zone_unlock(zone);
  420. if (!_zone)
  421. zone = ast_tone_zone_unref(zone);
  422. return ts;
  423. }
  424. static void ast_tone_zone_sound_destructor(void *obj)
  425. {
  426. struct ast_tone_zone_sound *ts = obj;
  427. /* Deconstify the 'const char *'s so the compiler doesn't complain. (but it's safe) */
  428. if (ts->name) {
  429. ast_free((char *) ts->name);
  430. ts->name = NULL;
  431. }
  432. if (ts->data) {
  433. ast_free((char *) ts->data);
  434. ts->data = NULL;
  435. }
  436. }
  437. /*! \brief deallocate the passed tone zone */
  438. static void ast_tone_zone_destructor(void *obj)
  439. {
  440. struct ast_tone_zone *zone = obj;
  441. struct ast_tone_zone_sound *current;
  442. while ((current = AST_LIST_REMOVE_HEAD(&zone->tones, entry))) {
  443. current = ast_tone_zone_sound_unref(current);
  444. }
  445. if (zone->ringcadence) {
  446. ast_free(zone->ringcadence);
  447. zone->ringcadence = NULL;
  448. }
  449. }
  450. /*! \brief add a new country, if country exists, it will be replaced. */
  451. static int ast_register_indication_country(struct ast_tone_zone *zone)
  452. {
  453. ao2_lock(ast_tone_zones);
  454. if (!default_tone_zone) {
  455. default_tone_zone = ast_tone_zone_ref(zone);
  456. }
  457. ao2_unlock(ast_tone_zones);
  458. ao2_link(ast_tone_zones, zone);
  459. ast_verb(3, "Registered indication country '%s'\n", zone->country);
  460. return 0;
  461. }
  462. /*! \brief remove an existing country and all its indications, country must exist. */
  463. static int ast_unregister_indication_country(const char *country)
  464. {
  465. struct ast_tone_zone *tz = NULL;
  466. struct ast_tone_zone zone_arg = {
  467. .nrringcadence = 0,
  468. };
  469. ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
  470. ao2_lock(ast_tone_zones);
  471. tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER | OBJ_UNLINK);
  472. if (!tz) {
  473. ao2_unlock(ast_tone_zones);
  474. return -1;
  475. }
  476. if (default_tone_zone == tz) {
  477. ast_tone_zone_unref(default_tone_zone);
  478. /* Get a new default, punt to the first one we find */
  479. default_tone_zone = ao2_callback(ast_tone_zones, 0, NULL, NULL);
  480. }
  481. ao2_unlock(ast_tone_zones);
  482. tz = ast_tone_zone_unref(tz);
  483. return 0;
  484. }
  485. /*!
  486. * \note called with the tone zone locked
  487. */
  488. static int ast_register_indication(struct ast_tone_zone *zone, const char *indication,
  489. const char *tonelist)
  490. {
  491. struct ast_tone_zone_sound *ts;
  492. if (ast_strlen_zero(indication) || ast_strlen_zero(tonelist)) {
  493. return -1;
  494. }
  495. AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
  496. if (!strcasecmp(indication, ts->name)) {
  497. AST_LIST_REMOVE_CURRENT(entry);
  498. ts = ast_tone_zone_sound_unref(ts);
  499. break;
  500. }
  501. }
  502. AST_LIST_TRAVERSE_SAFE_END;
  503. if (!(ts = ao2_alloc(sizeof(*ts), ast_tone_zone_sound_destructor))) {
  504. return -1;
  505. }
  506. if (!(ts->name = ast_strdup(indication)) || !(ts->data = ast_strdup(tonelist))) {
  507. ts = ast_tone_zone_sound_unref(ts);
  508. return -1;
  509. }
  510. AST_LIST_INSERT_TAIL(&zone->tones, ts, entry); /* Inherit reference */
  511. return 0;
  512. }
  513. /*! \brief remove an existing country's indication. Both country and indication must exist */
  514. static int ast_unregister_indication(struct ast_tone_zone *zone, const char *indication)
  515. {
  516. struct ast_tone_zone_sound *ts;
  517. int res = -1;
  518. ast_tone_zone_lock(zone);
  519. AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
  520. if (!strcasecmp(indication, ts->name)) {
  521. AST_LIST_REMOVE_CURRENT(entry);
  522. ts = ast_tone_zone_sound_unref(ts);
  523. res = 0;
  524. break;
  525. }
  526. }
  527. AST_LIST_TRAVERSE_SAFE_END;
  528. ast_tone_zone_unlock(zone);
  529. return res;
  530. }
  531. static struct ast_tone_zone *ast_tone_zone_alloc(void)
  532. {
  533. return ao2_alloc(sizeof(struct ast_tone_zone), ast_tone_zone_destructor);
  534. }
  535. static char *complete_country(struct ast_cli_args *a)
  536. {
  537. char *res = NULL;
  538. struct ao2_iterator i;
  539. int which = 0;
  540. size_t wordlen;
  541. struct ast_tone_zone *tz;
  542. wordlen = strlen(a->word);
  543. i = ao2_iterator_init(ast_tone_zones, 0);
  544. while ((tz = ao2_iterator_next(&i))) {
  545. if (!strncasecmp(a->word, tz->country, wordlen) && ++which > a->n) {
  546. res = ast_strdup(tz->country);
  547. }
  548. tz = ast_tone_zone_unref(tz);
  549. if (res) {
  550. break;
  551. }
  552. }
  553. ao2_iterator_destroy(&i);
  554. return res;
  555. }
  556. static char *handle_cli_indication_add(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  557. {
  558. struct ast_tone_zone *tz;
  559. int created_country = 0;
  560. char *res = CLI_SUCCESS;
  561. switch (cmd) {
  562. case CLI_INIT:
  563. e->command = "indication add";
  564. e->usage =
  565. "Usage: indication add <country> <indication> \"<tonelist>\"\n"
  566. " Add the given indication to the country.\n";
  567. return NULL;
  568. case CLI_GENERATE:
  569. if (a->pos == 2) {
  570. return complete_country(a);
  571. } else {
  572. return NULL;
  573. }
  574. }
  575. if (a->argc != 5) {
  576. return CLI_SHOWUSAGE;
  577. }
  578. if (!(tz = ast_get_indication_zone(a->argv[2]))) {
  579. /* country does not exist, create it */
  580. ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n", a->argv[2]);
  581. if (!(tz = ast_tone_zone_alloc())) {
  582. return CLI_FAILURE;
  583. }
  584. ast_copy_string(tz->country, a->argv[2], sizeof(tz->country));
  585. if (ast_register_indication_country(tz)) {
  586. ast_log(LOG_WARNING, "Unable to register new country\n");
  587. tz = ast_tone_zone_unref(tz);
  588. return CLI_FAILURE;
  589. }
  590. created_country = 1;
  591. }
  592. ast_tone_zone_lock(tz);
  593. if (ast_register_indication(tz, a->argv[3], a->argv[4])) {
  594. ast_log(LOG_WARNING, "Unable to register indication %s/%s\n", a->argv[2], a->argv[3]);
  595. if (created_country) {
  596. ast_unregister_indication_country(a->argv[2]);
  597. }
  598. res = CLI_FAILURE;
  599. }
  600. ast_tone_zone_unlock(tz);
  601. tz = ast_tone_zone_unref(tz);
  602. return res;
  603. }
  604. static char *complete_indications(struct ast_cli_args *a)
  605. {
  606. char *res = NULL;
  607. int which = 0;
  608. size_t wordlen;
  609. struct ast_tone_zone_sound *ts;
  610. struct ast_tone_zone *tz, tmp_tz = {
  611. .nrringcadence = 0,
  612. };
  613. ast_copy_string(tmp_tz.country, a->argv[a->pos - 1], sizeof(tmp_tz.country));
  614. if (!(tz = ao2_find(ast_tone_zones, &tmp_tz, OBJ_POINTER))) {
  615. return NULL;
  616. }
  617. wordlen = strlen(a->word);
  618. ast_tone_zone_lock(tz);
  619. AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
  620. if (!strncasecmp(a->word, ts->name, wordlen) && ++which > a->n) {
  621. res = ast_strdup(ts->name);
  622. break;
  623. }
  624. }
  625. ast_tone_zone_unlock(tz);
  626. tz = ast_tone_zone_unref(tz);
  627. return res;
  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. /*! \internal \brief Clean up resources on Asterisk shutdown */
  935. static void indications_shutdown(void)
  936. {
  937. ast_cli_unregister_multiple(cli_indications, ARRAY_LEN(cli_indications));
  938. if (default_tone_zone) {
  939. ast_tone_zone_unref(default_tone_zone);
  940. default_tone_zone = NULL;
  941. }
  942. if (ast_tone_zones) {
  943. ao2_ref(ast_tone_zones, -1);
  944. ast_tone_zones = NULL;
  945. }
  946. }
  947. /*! \brief Load indications module */
  948. int ast_indications_init(void)
  949. {
  950. if (!(ast_tone_zones = ao2_container_alloc(NUM_TONE_ZONE_BUCKETS,
  951. ast_tone_zone_hash, ast_tone_zone_cmp))) {
  952. return -1;
  953. }
  954. if (load_indications(0)) {
  955. indications_shutdown();
  956. return -1;
  957. }
  958. ast_cli_register_multiple(cli_indications, ARRAY_LEN(cli_indications));
  959. ast_register_cleanup(indications_shutdown);
  960. return 0;
  961. }
  962. /*! \brief Reload indications module */
  963. int ast_indications_reload(void)
  964. {
  965. return load_indications(1);
  966. }