12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196 |
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include <math.h>
- #include "asterisk/lock.h"
- #include "asterisk/linkedlists.h"
- #include "asterisk/indications.h"
- #include "asterisk/frame.h"
- #include "asterisk/channel.h"
- #include "asterisk/utils.h"
- #include "asterisk/cli.h"
- #include "asterisk/module.h"
- #include "asterisk/astobj2.h"
- #include "asterisk/data.h"
- #include "asterisk/_private.h"
- #define DATA_EXPORT_TONE_ZONE(MEMBER) \
- MEMBER(ast_tone_zone, country, AST_DATA_STRING) \
- MEMBER(ast_tone_zone, description, AST_DATA_STRING) \
- MEMBER(ast_tone_zone, nrringcadence, AST_DATA_UNSIGNED_INTEGER)
- AST_DATA_STRUCTURE(ast_tone_zone, DATA_EXPORT_TONE_ZONE);
- #define DATA_EXPORT_TONE_ZONE_SOUND(MEMBER) \
- MEMBER(ast_tone_zone_sound, name, AST_DATA_STRING) \
- MEMBER(ast_tone_zone_sound, data, AST_DATA_STRING)
- AST_DATA_STRUCTURE(ast_tone_zone_sound, DATA_EXPORT_TONE_ZONE_SOUND);
- static const char config[] = "indications.conf";
- static const int midi_tohz[128] = {
- 8, 8, 9, 9, 10, 10, 11, 12, 12, 13,
- 14, 15, 16, 17, 18, 19, 20, 21, 23, 24,
- 25, 27, 29, 30, 32, 34, 36, 38, 41, 43,
- 46, 48, 51, 55, 58, 61, 65, 69, 73, 77,
- 82, 87, 92, 97, 103, 110, 116, 123, 130, 138,
- 146, 155, 164, 174, 184, 195, 207, 220, 233, 246,
- 261, 277, 293, 311, 329, 349, 369, 391, 415, 440,
- 466, 493, 523, 554, 587, 622, 659, 698, 739, 783,
- 830, 880, 932, 987, 1046, 1108, 1174, 1244, 1318, 1396,
- 1479, 1567, 1661, 1760, 1864, 1975, 2093, 2217, 2349, 2489,
- 2637, 2793, 2959, 3135, 3322, 3520, 3729, 3951, 4186, 4434,
- 4698, 4978, 5274, 5587, 5919, 6271, 6644, 7040, 7458, 7902,
- 8372, 8869, 9397, 9956, 10548, 11175, 11839, 12543
- };
- static struct ao2_container *ast_tone_zones;
- #define NUM_TONE_ZONE_BUCKETS 53
- static struct ast_tone_zone *default_tone_zone;
- struct playtones_item {
- int fac1;
- int init_v2_1;
- int init_v3_1;
- int fac2;
- int init_v2_2;
- int init_v3_2;
- int modulate;
- int duration;
- };
- struct playtones_def {
- int vol;
- int reppos;
- int nitems;
- int interruptible;
- struct playtones_item *items;
- };
- struct playtones_state {
- int vol;
- int v1_1;
- int v2_1;
- int v3_1;
- int v1_2;
- int v2_2;
- int v3_2;
- int reppos;
- int nitems;
- struct playtones_item *items;
- int npos;
- int oldnpos;
- int pos;
- struct ast_format origwfmt;
- struct ast_frame f;
- unsigned char offset[AST_FRIENDLY_OFFSET];
- short data[4000];
- };
- static void playtones_release(struct ast_channel *chan, void *params)
- {
- struct playtones_state *ps = params;
- if (chan) {
- ast_set_write_format(chan, &ps->origwfmt);
- }
- if (ps->items) {
- ast_free(ps->items);
- ps->items = NULL;
- }
- ast_free(ps);
- }
- static void *playtones_alloc(struct ast_channel *chan, void *params)
- {
- struct playtones_def *pd = params;
- struct playtones_state *ps = NULL;
- if (!(ps = ast_calloc(1, sizeof(*ps)))) {
- return NULL;
- }
- ast_format_copy(&ps->origwfmt, ast_channel_writeformat(chan));
- if (ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR)) {
- ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", ast_channel_name(chan));
- playtones_release(NULL, ps);
- ps = NULL;
- } else {
- ps->vol = pd->vol;
- ps->reppos = pd->reppos;
- ps->nitems = pd->nitems;
- ps->items = pd->items;
- ps->oldnpos = -1;
- }
-
- if (pd->interruptible) {
- ast_set_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
- } else {
- ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
- }
- return ps;
- }
- static int playtones_generator(struct ast_channel *chan, void *data, int len, int samples)
- {
- struct playtones_state *ps = data;
- struct playtones_item *pi;
- int x;
-
- len = samples * 2;
- if (len > sizeof(ps->data) / 2 - 1) {
- ast_log(LOG_WARNING, "Can't generate that much data!\n");
- return -1;
- }
- memset(&ps->f, 0, sizeof(ps->f));
- pi = &ps->items[ps->npos];
- if (ps->oldnpos != ps->npos) {
-
- ps->v1_1 = 0;
- ps->v2_1 = pi->init_v2_1;
- ps->v3_1 = pi->init_v3_1;
- ps->v1_2 = 0;
- ps->v2_2 = pi->init_v2_2;
- ps->v3_2 = pi->init_v3_2;
- ps->oldnpos = ps->npos;
- }
- for (x = 0; x < samples; x++) {
- ps->v1_1 = ps->v2_1;
- ps->v2_1 = ps->v3_1;
- ps->v3_1 = (pi->fac1 * ps->v2_1 >> 15) - ps->v1_1;
- ps->v1_2 = ps->v2_2;
- ps->v2_2 = ps->v3_2;
- ps->v3_2 = (pi->fac2 * ps->v2_2 >> 15) - ps->v1_2;
- if (pi->modulate) {
- int p;
- p = ps->v3_2 - 32768;
- if (p < 0) {
- p = -p;
- }
- p = ((p * 9) / 10) + 1;
- ps->data[x] = (ps->v3_1 * p) >> 15;
- } else {
- ps->data[x] = ps->v3_1 + ps->v3_2;
- }
- }
- ps->f.frametype = AST_FRAME_VOICE;
- ast_format_set(&ps->f.subclass.format, AST_FORMAT_SLINEAR, 0);
- ps->f.datalen = len;
- ps->f.samples = samples;
- ps->f.offset = AST_FRIENDLY_OFFSET;
- ps->f.data.ptr = ps->data;
- if (ast_write(chan, &ps->f)) {
- return -1;
- }
- ps->pos += x;
- if (pi->duration && ps->pos >= pi->duration * 8) {
- ps->pos = 0;
- ps->npos++;
- if (ps->npos >= ps->nitems) {
- if (ps->reppos == -1) {
- return -1;
- }
- ps->npos = ps->reppos;
- }
- }
- return 0;
- }
- static struct ast_generator playtones = {
- .alloc = playtones_alloc,
- .release = playtones_release,
- .generate = playtones_generator,
- };
- int ast_tone_zone_part_parse(const char *s, struct ast_tone_zone_part *tone_data)
- {
- if (sscanf(s, "%30u+%30u/%30u", &tone_data->freq1, &tone_data->freq2,
- &tone_data->time) == 3) {
-
- } else if (sscanf(s, "%30u+%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
-
- tone_data->time = 0;
- } else if (sscanf(s, "%30u*%30u/%30u", &tone_data->freq1, &tone_data->freq2,
- &tone_data->time) == 3) {
-
- tone_data->modulate = 1;
- } else if (sscanf(s, "%30u*%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
-
- tone_data->time = 0;
- tone_data->modulate = 1;
- } else if (sscanf(s, "%30u/%30u", &tone_data->freq1, &tone_data->time) == 2) {
-
- tone_data->freq2 = 0;
- } else if (sscanf(s, "%30u", &tone_data->freq1) == 1) {
-
- tone_data->freq2 = 0;
- tone_data->time = 0;
- } else if (sscanf(s, "M%30u+M%30u/%30u", &tone_data->freq1, &tone_data->freq2,
- &tone_data->time) == 3) {
-
- tone_data->midinote = 1;
- } else if (sscanf(s, "M%30u+M%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
-
- tone_data->time = 0;
- tone_data->midinote = 1;
- } else if (sscanf(s, "M%30u*M%30u/%30u", &tone_data->freq1, &tone_data->freq2,
- &tone_data->time) == 3) {
-
- tone_data->modulate = 1;
- tone_data->midinote = 1;
- } else if (sscanf(s, "M%30u*M%30u", &tone_data->freq1, &tone_data->freq2) == 2) {
-
- tone_data->time = 0;
- tone_data->modulate = 1;
- tone_data->midinote = 1;
- } else if (sscanf(s, "M%30u/%30u", &tone_data->freq1, &tone_data->time) == 2) {
-
- tone_data->freq2 = -1;
- tone_data->midinote = 1;
- } else if (sscanf(s, "M%30u", &tone_data->freq1) == 1) {
-
- tone_data->freq2 = -1;
- tone_data->time = 0;
- tone_data->midinote = 1;
- } else {
- return -1;
- }
- return 0;
- }
- int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
- {
- char *s, *data = ast_strdupa(playlst);
- struct playtones_def d = { vol, -1, 0, 1, NULL };
- char *stringp;
- char *separator;
- static const float sample_rate = 8000.0;
- static const float max_sample_val = 32768.0;
- if (vol < 1) {
- d.vol = 7219;
- }
- d.interruptible = interruptible;
- stringp = data;
-
- if (strchr(stringp,'|')) {
- separator = "|";
- } else {
- separator = ",";
- }
- while ((s = strsep(&stringp, separator)) && !ast_strlen_zero(s)) {
- struct playtones_item *new_items;
- struct ast_tone_zone_part tone_data = {
- .time = 0,
- };
- s = ast_strip(s);
- if (s[0]=='!') {
- s++;
- } else if (d.reppos == -1) {
- d.reppos = d.nitems;
- }
- if (ast_tone_zone_part_parse(s, &tone_data)) {
- ast_log(LOG_ERROR, "Failed to parse tone part '%s'\n", s);
- continue;
- }
- if (tone_data.midinote) {
-
- if (tone_data.freq1 <= 127) {
- tone_data.freq1 = midi_tohz[tone_data.freq1];
- } else {
- tone_data.freq1 = 0;
- }
- if (tone_data.freq2 <= 127) {
- tone_data.freq2 = midi_tohz[tone_data.freq2];
- } else {
- tone_data.freq2 = 0;
- }
- }
- new_items = ast_realloc(d.items, (d.nitems + 1) * sizeof(*d.items));
- if (!new_items) {
- ast_free(d.items);
- return -1;
- }
- d.items = new_items;
- d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (tone_data.freq1 / sample_rate)) * max_sample_val;
- d.items[d.nitems].init_v2_1 = sin(-4.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
- d.items[d.nitems].init_v3_1 = sin(-2.0 * M_PI * (tone_data.freq1 / sample_rate)) * d.vol;
- d.items[d.nitems].fac2 = 2.0 * cos(2.0 * M_PI * (tone_data.freq2 / sample_rate)) * max_sample_val;
- d.items[d.nitems].init_v2_2 = sin(-4.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
- d.items[d.nitems].init_v3_2 = sin(-2.0 * M_PI * (tone_data.freq2 / sample_rate)) * d.vol;
- d.items[d.nitems].duration = tone_data.time;
- d.items[d.nitems].modulate = tone_data.modulate;
- d.nitems++;
- }
- if (!d.nitems) {
- ast_log(LOG_ERROR, "No valid tone parts\n");
- return -1;
- }
- if (ast_activate_generator(chan, &playtones, &d)) {
- ast_free(d.items);
- return -1;
- }
- return 0;
- }
- void ast_playtones_stop(struct ast_channel *chan)
- {
- ast_deactivate_generator(chan);
- }
- int ast_tone_zone_count(void)
- {
- return ao2_container_count(ast_tone_zones);
- }
- struct ao2_iterator ast_tone_zone_iterator_init(void)
- {
- return ao2_iterator_init(ast_tone_zones, 0);
- }
- static int ast_set_indication_country(const char *country)
- {
- struct ast_tone_zone *zone = NULL;
- if (ast_strlen_zero(country) || !(zone = ast_get_indication_zone(country))) {
- return -1;
- }
- ast_verb(3, "Setting default indication country to '%s'\n", country);
- ao2_lock(ast_tone_zones);
- if (default_tone_zone) {
- default_tone_zone = ast_tone_zone_unref(default_tone_zone);
- }
- default_tone_zone = ast_tone_zone_ref(zone);
- ao2_unlock(ast_tone_zones);
- zone = ast_tone_zone_unref(zone);
- return 0;
- }
- struct ast_tone_zone *ast_get_indication_zone(const char *country)
- {
- struct ast_tone_zone *tz = NULL;
- struct ast_tone_zone zone_arg = {
- .nrringcadence = 0,
- };
- if (ast_strlen_zero(country)) {
- ao2_lock(ast_tone_zones);
- if (default_tone_zone) {
- tz = ast_tone_zone_ref(default_tone_zone);
- }
- ao2_unlock(ast_tone_zones);
- return tz;
- }
- ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
- return ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER);
- }
- struct ast_tone_zone_sound *ast_get_indication_tone(const struct ast_tone_zone *_zone, const char *indication)
- {
- struct ast_tone_zone_sound *ts = NULL;
-
- struct ast_tone_zone *zone = (struct ast_tone_zone *) _zone;
-
- if (!zone) {
- ao2_lock(ast_tone_zones);
- if (default_tone_zone) {
- zone = ast_tone_zone_ref(default_tone_zone);
- }
- ao2_unlock(ast_tone_zones);
- if (!zone) {
- return NULL;
- }
- }
- ast_tone_zone_lock(zone);
-
- AST_LIST_TRAVERSE(&zone->tones, ts, entry) {
- if (!strcasecmp(ts->name, indication)) {
-
- ts = ast_tone_zone_sound_ref(ts);
- break;
- }
- }
- ast_tone_zone_unlock(zone);
- if (!_zone)
- zone = ast_tone_zone_unref(zone);
- return ts;
- }
- static void ast_tone_zone_sound_destructor(void *obj)
- {
- struct ast_tone_zone_sound *ts = obj;
-
- if (ts->name) {
- ast_free((char *) ts->name);
- ts->name = NULL;
- }
- if (ts->data) {
- ast_free((char *) ts->data);
- ts->data = NULL;
- }
- }
- static void ast_tone_zone_destructor(void *obj)
- {
- struct ast_tone_zone *zone = obj;
- struct ast_tone_zone_sound *current;
- while ((current = AST_LIST_REMOVE_HEAD(&zone->tones, entry))) {
- current = ast_tone_zone_sound_unref(current);
- }
- if (zone->ringcadence) {
- ast_free(zone->ringcadence);
- zone->ringcadence = NULL;
- }
- }
- static int ast_register_indication_country(struct ast_tone_zone *zone)
- {
- ao2_lock(ast_tone_zones);
- if (!default_tone_zone) {
- default_tone_zone = ast_tone_zone_ref(zone);
- }
- ao2_unlock(ast_tone_zones);
- ao2_link(ast_tone_zones, zone);
- ast_verb(3, "Registered indication country '%s'\n", zone->country);
- return 0;
- }
- static int ast_unregister_indication_country(const char *country)
- {
- struct ast_tone_zone *tz = NULL;
- struct ast_tone_zone zone_arg = {
- .nrringcadence = 0,
- };
- ast_copy_string(zone_arg.country, country, sizeof(zone_arg.country));
- ao2_lock(ast_tone_zones);
- tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER | OBJ_UNLINK);
- if (!tz) {
- ao2_unlock(ast_tone_zones);
- return -1;
- }
- if (default_tone_zone == tz) {
- ast_tone_zone_unref(default_tone_zone);
-
- default_tone_zone = ao2_callback(ast_tone_zones, 0, NULL, NULL);
- }
- ao2_unlock(ast_tone_zones);
- tz = ast_tone_zone_unref(tz);
- return 0;
- }
- static int ast_register_indication(struct ast_tone_zone *zone, const char *indication,
- const char *tonelist)
- {
- struct ast_tone_zone_sound *ts;
- if (ast_strlen_zero(indication) || ast_strlen_zero(tonelist)) {
- return -1;
- }
- AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
- if (!strcasecmp(indication, ts->name)) {
- AST_LIST_REMOVE_CURRENT(entry);
- ts = ast_tone_zone_sound_unref(ts);
- break;
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
- if (!(ts = ao2_alloc(sizeof(*ts), ast_tone_zone_sound_destructor))) {
- return -1;
- }
- if (!(ts->name = ast_strdup(indication)) || !(ts->data = ast_strdup(tonelist))) {
- ts = ast_tone_zone_sound_unref(ts);
- return -1;
- }
- AST_LIST_INSERT_TAIL(&zone->tones, ts, entry);
- return 0;
- }
- static int ast_unregister_indication(struct ast_tone_zone *zone, const char *indication)
- {
- struct ast_tone_zone_sound *ts;
- int res = -1;
- ast_tone_zone_lock(zone);
- AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, ts, entry) {
- if (!strcasecmp(indication, ts->name)) {
- AST_LIST_REMOVE_CURRENT(entry);
- ts = ast_tone_zone_sound_unref(ts);
- res = 0;
- break;
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
- ast_tone_zone_unlock(zone);
- return res;
- }
- static struct ast_tone_zone *ast_tone_zone_alloc(void)
- {
- return ao2_alloc(sizeof(struct ast_tone_zone), ast_tone_zone_destructor);
- }
- static char *complete_country(struct ast_cli_args *a)
- {
- char *res = NULL;
- struct ao2_iterator i;
- int which = 0;
- size_t wordlen;
- struct ast_tone_zone *tz;
- wordlen = strlen(a->word);
- i = ao2_iterator_init(ast_tone_zones, 0);
- while ((tz = ao2_iterator_next(&i))) {
- if (!strncasecmp(a->word, tz->country, wordlen) && ++which > a->n) {
- res = ast_strdup(tz->country);
- }
- tz = ast_tone_zone_unref(tz);
- if (res) {
- break;
- }
- }
- ao2_iterator_destroy(&i);
- return res;
- }
- static char *handle_cli_indication_add(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
- {
- struct ast_tone_zone *tz;
- int created_country = 0;
- char *res = CLI_SUCCESS;
- switch (cmd) {
- case CLI_INIT:
- e->command = "indication add";
- e->usage =
- "Usage: indication add <country> <indication> \"<tonelist>\"\n"
- " Add the given indication to the country.\n";
- return NULL;
- case CLI_GENERATE:
- if (a->pos == 2) {
- return complete_country(a);
- } else {
- return NULL;
- }
- }
- if (a->argc != 5) {
- return CLI_SHOWUSAGE;
- }
- if (!(tz = ast_get_indication_zone(a->argv[2]))) {
-
- ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n", a->argv[2]);
- if (!(tz = ast_tone_zone_alloc())) {
- return CLI_FAILURE;
- }
- ast_copy_string(tz->country, a->argv[2], sizeof(tz->country));
- if (ast_register_indication_country(tz)) {
- ast_log(LOG_WARNING, "Unable to register new country\n");
- tz = ast_tone_zone_unref(tz);
- return CLI_FAILURE;
- }
- created_country = 1;
- }
- ast_tone_zone_lock(tz);
- if (ast_register_indication(tz, a->argv[3], a->argv[4])) {
- ast_log(LOG_WARNING, "Unable to register indication %s/%s\n", a->argv[2], a->argv[3]);
- if (created_country) {
- ast_unregister_indication_country(a->argv[2]);
- }
- res = CLI_FAILURE;
- }
- ast_tone_zone_unlock(tz);
- tz = ast_tone_zone_unref(tz);
- return res;
- }
- static char *complete_indications(struct ast_cli_args *a)
- {
- char *res = NULL;
- int which = 0;
- size_t wordlen;
- struct ast_tone_zone_sound *ts;
- struct ast_tone_zone *tz, tmp_tz = {
- .nrringcadence = 0,
- };
- ast_copy_string(tmp_tz.country, a->argv[a->pos - 1], sizeof(tmp_tz.country));
- if (!(tz = ao2_find(ast_tone_zones, &tmp_tz, OBJ_POINTER))) {
- return NULL;
- }
- wordlen = strlen(a->word);
- ast_tone_zone_lock(tz);
- AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
- if (!strncasecmp(a->word, ts->name, wordlen) && ++which > a->n) {
- res = ast_strdup(ts->name);
- break;
- }
- }
- ast_tone_zone_unlock(tz);
- tz = ast_tone_zone_unref(tz);
- return res;
- }
- static char *handle_cli_indication_remove(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
- {
- struct ast_tone_zone *tz;
- char *res = CLI_SUCCESS;
- switch (cmd) {
- case CLI_INIT:
- e->command = "indication remove";
- e->usage =
- "Usage: indication remove <country> [indication]\n"
- " Remove the given indication from the country.\n";
- return NULL;
- case CLI_GENERATE:
- if (a->pos == 2) {
- return complete_country(a);
- } else if (a->pos == 3) {
- return complete_indications(a);
- }
- }
- if (a->argc != 3 && a->argc != 4) {
- return CLI_SHOWUSAGE;
- }
- if (a->argc == 3) {
-
- if (ast_unregister_indication_country(a->argv[2])) {
- ast_log(LOG_WARNING, "Unable to unregister indication country %s\n", a->argv[2]);
- return CLI_FAILURE;
- }
- return CLI_SUCCESS;
- }
- if (!(tz = ast_get_indication_zone(a->argv[2]))) {
- ast_log(LOG_WARNING, "Unable to unregister indication %s/%s, country does not exists\n", a->argv[2], a->argv[3]);
- return CLI_FAILURE;
- }
- if (ast_unregister_indication(tz, a->argv[3])) {
- ast_log(LOG_WARNING, "Unable to unregister indication %s/%s\n", a->argv[2], a->argv[3]);
- res = CLI_FAILURE;
- }
- tz = ast_tone_zone_unref(tz);
- return res;
- }
- static char *handle_cli_indication_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
- {
- struct ast_tone_zone *tz = NULL;
- struct ast_str *buf;
- int found_country = 0;
- int i;
- switch (cmd) {
- case CLI_INIT:
- e->command = "indication show";
- e->usage =
- "Usage: indication show [<country> ...]\n"
- " Display either a condensed summary of all countries and indications, or a\n"
- " more verbose list of indications for the specified countries.\n";
- return NULL;
- case CLI_GENERATE:
- return complete_country(a);
- }
- if (a->argc == 2) {
- struct ao2_iterator iter;
-
- ast_cli(a->fd, "Country Description\n");
- ast_cli(a->fd, "===========================\n");
- iter = ast_tone_zone_iterator_init();
- while ((tz = ao2_iterator_next(&iter))) {
- ast_tone_zone_lock(tz);
- ast_cli(a->fd, "%-7.7s %s\n", tz->country, tz->description);
- ast_tone_zone_unlock(tz);
- tz = ast_tone_zone_unref(tz);
- }
- ao2_iterator_destroy(&iter);
- return CLI_SUCCESS;
- }
- buf = ast_str_alloca(256);
- for (i = 2; i < a->argc; i++) {
- struct ast_tone_zone zone_arg = {
- .nrringcadence = 0,
- };
- struct ast_tone_zone_sound *ts;
- int j;
- ast_copy_string(zone_arg.country, a->argv[i], sizeof(zone_arg.country));
- if (!(tz = ao2_find(ast_tone_zones, &zone_arg, OBJ_POINTER))) {
- continue;
- }
- if (!found_country) {
- found_country = 1;
- ast_cli(a->fd, "Country Indication PlayList\n");
- ast_cli(a->fd, "=====================================\n");
- }
- ast_tone_zone_lock(tz);
- ast_str_set(&buf, 0, "%-7.7s %-15.15s ", tz->country, "<ringcadence>");
- for (j = 0; j < tz->nrringcadence; j++) {
- ast_str_append(&buf, 0, "%d%s", tz->ringcadence[j],
- (j == tz->nrringcadence - 1) ? "" : ",");
- }
- ast_str_append(&buf, 0, "\n");
- ast_cli(a->fd, "%s", ast_str_buffer(buf));
- AST_LIST_TRAVERSE(&tz->tones, ts, entry) {
- ast_cli(a->fd, "%-7.7s %-15.15s %s\n", tz->country, ts->name, ts->data);
- }
- ast_tone_zone_unlock(tz);
- tz = ast_tone_zone_unref(tz);
- }
- if (!found_country) {
- ast_cli(a->fd, "No countries matched your criteria.\n");
- }
- return CLI_SUCCESS;
- }
- static int is_valid_tone_zone(struct ast_tone_zone *zone)
- {
- int res;
- ast_tone_zone_lock(zone);
- res = (!ast_strlen_zero(zone->description) && !AST_LIST_EMPTY(&zone->tones));
- ast_tone_zone_unlock(zone);
- return res;
- }
- static void store_tone_zone_ring_cadence(struct ast_tone_zone *zone, const char *val)
- {
- char buf[1024];
- char *ring, *c = buf;
- ast_copy_string(buf, val, sizeof(buf));
- while ((ring = strsep(&c, ","))) {
- int *tmp, val;
- ring = ast_strip(ring);
- if (!isdigit(ring[0]) || (val = atoi(ring)) == -1) {
- ast_log(LOG_WARNING, "Invalid ringcadence given '%s'.\n", ring);
- continue;
- }
- if (!(tmp = ast_realloc(zone->ringcadence, (zone->nrringcadence + 1) * sizeof(int)))) {
- return;
- }
- zone->ringcadence = tmp;
- tmp[zone->nrringcadence] = val;
- zone->nrringcadence++;
- }
- }
- static void store_config_tone_zone(struct ast_tone_zone *zone, const char *var,
- const char *value)
- {
- CV_START(var, value);
- CV_STR("description", zone->description);
- CV_F("ringcadence", store_tone_zone_ring_cadence(zone, value));
- ast_register_indication(zone, var, value);
- CV_END;
- }
- static void reset_tone_zone(struct ast_tone_zone *zone)
- {
- ast_tone_zone_lock(zone);
- zone->killme = 0;
- if (zone->nrringcadence) {
- zone->nrringcadence = 0;
- ast_free(zone->ringcadence);
- zone->ringcadence = NULL;
- }
- ast_tone_zone_unlock(zone);
- }
- static int parse_tone_zone(struct ast_config *cfg, const char *country)
- {
- struct ast_variable *v;
- struct ast_tone_zone *zone;
- struct ast_tone_zone tmp_zone = {
- .nrringcadence = 0,
- };
- int allocd = 0;
- ast_copy_string(tmp_zone.country, country, sizeof(tmp_zone.country));
- if ((zone = ao2_find(ast_tone_zones, &tmp_zone, OBJ_POINTER))) {
- reset_tone_zone(zone);
- } else if ((zone = ast_tone_zone_alloc())) {
- allocd = 1;
- ast_copy_string(zone->country, country, sizeof(zone->country));
- } else {
- return -1;
- }
- ast_tone_zone_lock(zone);
- for (v = ast_variable_browse(cfg, country); v; v = v->next) {
- store_config_tone_zone(zone, v->name, v->value);
- }
- ast_tone_zone_unlock(zone);
- if (allocd) {
- if (!is_valid_tone_zone(zone)) {
- ast_log(LOG_WARNING, "Indication country '%s' is invalid\n", country);
- } else if (ast_register_indication_country(zone)) {
- ast_log(LOG_WARNING, "Unable to register indication country '%s'.\n",
- country);
- }
- }
- zone = ast_tone_zone_unref(zone);
- return 0;
- }
- static int tone_zone_mark(void *obj, void *arg, int flags)
- {
- struct ast_tone_zone *zone = obj;
- struct ast_tone_zone_sound *s;
- ast_tone_zone_lock(zone);
- zone->killme = 1;
- AST_LIST_TRAVERSE(&zone->tones, s, entry) {
- s->killme = 1;
- }
- ast_tone_zone_unlock(zone);
- return 0;
- }
- static int prune_tone_zone(void *obj, void *arg, int flags)
- {
- struct ast_tone_zone *zone = obj;
- struct ast_tone_zone_sound *s;
- ast_tone_zone_lock(zone);
- AST_LIST_TRAVERSE_SAFE_BEGIN(&zone->tones, s, entry) {
- if (s->killme) {
- AST_LIST_REMOVE_CURRENT(entry);
- s = ast_tone_zone_sound_unref(s);
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
- ast_tone_zone_unlock(zone);
- return zone->killme ? CMP_MATCH : 0;
- }
- static int load_indications(int reload)
- {
- struct ast_config *cfg;
- const char *cxt = NULL;
- const char *country = NULL;
- struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
- int res = -1;
- cfg = ast_config_load2(config, "indications", config_flags);
- if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
- ast_log(LOG_WARNING, "Can't find indications config file %s.\n", config);
- return 0;
- } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
- return 0;
- }
-
- ao2_lock(ast_tone_zones);
- ao2_callback(ast_tone_zones, OBJ_NODATA, tone_zone_mark, NULL);
-
- while ((cxt = ast_category_browse(cfg, cxt))) {
-
- if (!strcasecmp(cxt, "general")) {
- continue;
- }
- if (parse_tone_zone(cfg, cxt)) {
- goto return_cleanup;
- }
- }
- ao2_callback(ast_tone_zones, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK,
- prune_tone_zone, NULL);
-
- country = ast_variable_retrieve(cfg, "general", "country");
- if (ast_strlen_zero(country) || ast_set_indication_country(country)) {
- ast_log(LOG_WARNING, "Unable to set the default country (for indication tones)\n");
- }
- res = 0;
- return_cleanup:
- ao2_unlock(ast_tone_zones);
- ast_config_destroy(cfg);
- return res;
- }
- static struct ast_cli_entry cli_indications[] = {
- AST_CLI_DEFINE(handle_cli_indication_add, "Add the given indication to the country"),
- AST_CLI_DEFINE(handle_cli_indication_remove, "Remove the given indication from the country"),
- AST_CLI_DEFINE(handle_cli_indication_show, "Display a list of all countries/indications")
- };
- static int ast_tone_zone_hash(const void *obj, const int flags)
- {
- const struct ast_tone_zone *zone = obj;
- return ast_str_case_hash(zone->country);
- }
- static int ast_tone_zone_cmp(void *obj, void *arg, int flags)
- {
- struct ast_tone_zone *zone = obj;
- struct ast_tone_zone *zone_arg = arg;
- return (!strcasecmp(zone->country, zone_arg->country)) ?
- CMP_MATCH | CMP_STOP : 0;
- }
- int ast_tone_zone_data_add_structure(struct ast_data *tree, struct ast_tone_zone *zone)
- {
- struct ast_data *data_zone_sound;
- struct ast_tone_zone_sound *s;
- ast_data_add_structure(ast_tone_zone, tree, zone);
- if (AST_LIST_EMPTY(&zone->tones)) {
- return 0;
- }
- data_zone_sound = ast_data_add_node(tree, "tones");
- if (!data_zone_sound) {
- return -1;
- }
- ast_tone_zone_lock(zone);
- AST_LIST_TRAVERSE(&zone->tones, s, entry) {
- ast_data_add_structure(ast_tone_zone_sound, data_zone_sound, s);
- }
- ast_tone_zone_unlock(zone);
- return 0;
- }
- static void indications_shutdown(void)
- {
- ast_cli_unregister_multiple(cli_indications, ARRAY_LEN(cli_indications));
- if (default_tone_zone) {
- ast_tone_zone_unref(default_tone_zone);
- default_tone_zone = NULL;
- }
- if (ast_tone_zones) {
- ao2_ref(ast_tone_zones, -1);
- ast_tone_zones = NULL;
- }
- }
- int ast_indications_init(void)
- {
- if (!(ast_tone_zones = ao2_container_alloc(NUM_TONE_ZONE_BUCKETS,
- ast_tone_zone_hash, ast_tone_zone_cmp))) {
- return -1;
- }
- if (load_indications(0)) {
- indications_shutdown();
- return -1;
- }
- ast_cli_register_multiple(cli_indications, ARRAY_LEN(cli_indications));
- ast_register_cleanup(indications_shutdown);
- return 0;
- }
- int ast_indications_reload(void)
- {
- return load_indications(1);
- }
|