123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590 |
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "asterisk/astobj2.h"
- #include "asterisk/strings.h"
- #include "asterisk/ccss.h"
- #include "asterisk/channel.h"
- #include "asterisk/pbx.h"
- #include "asterisk/utils.h"
- #include "asterisk/taskprocessor.h"
- #include "asterisk/event.h"
- #include "asterisk/devicestate.h"
- #include "asterisk/module.h"
- #include "asterisk/app.h"
- #include "asterisk/cli.h"
- #include "asterisk/manager.h"
- #include "asterisk/causes.h"
- static struct ast_sched_context *cc_sched_context;
- static int core_id_counter;
- static struct ast_taskprocessor *cc_core_taskprocessor;
- static const char *CC_LOGGER_LEVEL_NAME = "CC";
- static int cc_logger_level;
- static unsigned int global_cc_max_requests;
- static int cc_request_count;
- static inline void *cc_ref(void *obj, const char *debug)
- {
- ao2_t_ref(obj, +1, debug);
- return obj;
- }
- static inline void *cc_unref(void *obj, const char *debug)
- {
- ao2_t_ref(obj, -1, debug);
- return NULL;
- }
- struct ast_cc_config_params {
- enum ast_cc_agent_policies cc_agent_policy;
- enum ast_cc_monitor_policies cc_monitor_policy;
- unsigned int cc_offer_timer;
- unsigned int ccnr_available_timer;
- unsigned int ccbs_available_timer;
- unsigned int cc_recall_timer;
- unsigned int cc_max_agents;
- unsigned int cc_max_monitors;
- char cc_callback_macro[AST_MAX_EXTENSION];
- char cc_callback_sub[AST_MAX_EXTENSION];
- char cc_agent_dialstring[AST_MAX_EXTENSION];
- };
- enum cc_state {
-
- CC_AVAILABLE,
-
- CC_CALLER_OFFERED,
-
- CC_CALLER_REQUESTED,
-
- CC_ACTIVE,
-
- CC_CALLEE_READY,
-
- CC_CALLER_BUSY,
-
- CC_RECALLING,
-
- CC_COMPLETE,
-
- CC_FAILED,
- };
- struct cc_control_payload {
-
- const char *monitor_type;
-
- void *private_data;
-
- enum ast_cc_service_type service;
-
- struct ast_cc_config_params config_params;
-
- int parent_interface_id;
-
- char device_name[AST_CHANNEL_NAME];
-
- char dialstring[AST_CHANNEL_NAME];
- };
- AST_LIST_HEAD(cc_monitor_tree, ast_cc_monitor);
- static const int CC_CORE_INSTANCES_BUCKETS = 17;
- static struct ao2_container *cc_core_instances;
- struct cc_core_instance {
-
- int core_id;
-
- enum cc_state current_state;
-
- struct ast_cc_agent *agent;
-
- struct cc_monitor_tree *monitors;
- };
- static int __attribute__((format(printf, 3, 0))) cc_request_state_change(enum cc_state state, const int core_id, const char *debug, va_list ap);
- static struct cc_core_instance *cc_core_init_instance(struct ast_channel *caller_chan,
- struct cc_monitor_tree *called_tree, const int core_id, struct cc_control_payload *cc_data);
- static const struct {
- enum ast_cc_service_type service;
- const char *service_string;
- } cc_service_to_string_map[] = {
- {AST_CC_NONE, "NONE"},
- {AST_CC_CCBS, "CCBS"},
- {AST_CC_CCNR, "CCNR"},
- {AST_CC_CCNL, "CCNL"},
- };
- static const struct {
- enum cc_state state;
- const char *state_string;
- } cc_state_to_string_map[] = {
- {CC_AVAILABLE, "CC is available"},
- {CC_CALLER_OFFERED, "CC offered to caller"},
- {CC_CALLER_REQUESTED, "CC requested by caller"},
- {CC_ACTIVE, "CC accepted by callee"},
- {CC_CALLEE_READY, "Callee has become available"},
- {CC_CALLER_BUSY, "Callee was ready, but caller is now unavailable"},
- {CC_RECALLING, "Caller is attempting to recall"},
- {CC_COMPLETE, "Recall complete"},
- {CC_FAILED, "CC has failed"},
- };
- static const char *cc_state_to_string(enum cc_state state)
- {
- return cc_state_to_string_map[state].state_string;
- }
- static const char *cc_service_to_string(enum ast_cc_service_type service)
- {
- return cc_service_to_string_map[service].service_string;
- }
- static int cc_core_instance_hash_fn(const void *obj, const int flags)
- {
- const struct cc_core_instance *core_instance = obj;
- return core_instance->core_id;
- }
- static int cc_core_instance_cmp_fn(void *obj, void *arg, int flags)
- {
- struct cc_core_instance *core_instance1 = obj;
- struct cc_core_instance *core_instance2 = arg;
- return core_instance1->core_id == core_instance2->core_id ? CMP_MATCH | CMP_STOP : 0;
- }
- static struct cc_core_instance *find_cc_core_instance(const int core_id)
- {
- struct cc_core_instance finder = {.core_id = core_id,};
- return ao2_t_find(cc_core_instances, &finder, OBJ_POINTER, "Finding a core_instance");
- }
- struct cc_callback_helper {
- ao2_callback_fn *function;
- void *args;
- const char *type;
- };
- static int cc_agent_callback_helper(void *obj, void *args, int flags)
- {
- struct cc_core_instance *core_instance = obj;
- struct cc_callback_helper *helper = args;
- if (strcmp(core_instance->agent->callbacks->type, helper->type)) {
- return 0;
- }
- return helper->function(core_instance->agent, helper->args, flags);
- }
- struct ast_cc_agent *ast_cc_agent_callback(int flags, ao2_callback_fn *function, void *args, const char * const type)
- {
- struct cc_callback_helper helper = {.function = function, .args = args, .type = type};
- struct cc_core_instance *core_instance;
- if ((core_instance = ao2_t_callback(cc_core_instances, flags, cc_agent_callback_helper, &helper,
- "Calling provided agent callback function"))) {
- struct ast_cc_agent *agent = cc_ref(core_instance->agent, "An outside entity needs the agent");
- cc_unref(core_instance, "agent callback done with the core_instance");
- return agent;
- }
- return NULL;
- }
- enum match_flags {
-
- MATCH_NO_REQUEST = (1 << 0),
-
- MATCH_REQUEST = (1 << 1),
- };
- static int match_agent(void *obj, void *arg, void *data, int flags)
- {
- struct cc_core_instance *core_instance = obj;
- const char *name = arg;
- unsigned long match_flags = *(unsigned long *)data;
- int possible_match = 0;
- if ((match_flags & MATCH_NO_REQUEST) && core_instance->current_state < CC_CALLER_REQUESTED) {
- possible_match = 1;
- }
- if ((match_flags & MATCH_REQUEST) && core_instance->current_state >= CC_CALLER_REQUESTED) {
- possible_match = 1;
- }
- if (!possible_match) {
- return 0;
- }
- if (!strcmp(core_instance->agent->device_name, name)) {
- return CMP_MATCH | CMP_STOP;
- }
- return 0;
- }
- struct count_agents_cb_data {
- int count;
- int core_id_exception;
- };
- static int count_agents_cb(void *obj, void *arg, void *data, int flags)
- {
- struct cc_core_instance *core_instance = obj;
- const char *name = arg;
- struct count_agents_cb_data *cb_data = data;
- if (cb_data->core_id_exception == core_instance->core_id) {
- ast_log_dynamic_level(cc_logger_level, "Found agent with core_id %d but not counting it toward total\n", core_instance->core_id);
- return 0;
- }
- if (core_instance->current_state >= CC_CALLER_REQUESTED && !strcmp(core_instance->agent->device_name, name)) {
- cb_data->count++;
- }
- return 0;
- }
- #define CC_AVAILABLE_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
- #define CC_CALLER_OFFERED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
- #define CC_CALLER_REQUESTED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
- #define CC_ACTIVE_DEVSTATE_DEFAULT AST_DEVICE_INUSE
- #define CC_CALLEE_READY_DEVSTATE_DEFAULT AST_DEVICE_RINGING
- #define CC_CALLER_BUSY_DEVSTATE_DEFAULT AST_DEVICE_ONHOLD
- #define CC_RECALLING_DEVSTATE_DEFAULT AST_DEVICE_RINGING
- #define CC_COMPLETE_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
- #define CC_FAILED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE
- static enum ast_device_state cc_state_to_devstate_map[] = {
- [CC_AVAILABLE] = CC_AVAILABLE_DEVSTATE_DEFAULT,
- [CC_CALLER_OFFERED] = CC_CALLER_OFFERED_DEVSTATE_DEFAULT,
- [CC_CALLER_REQUESTED] = CC_CALLER_REQUESTED_DEVSTATE_DEFAULT,
- [CC_ACTIVE] = CC_ACTIVE_DEVSTATE_DEFAULT,
- [CC_CALLEE_READY] = CC_CALLEE_READY_DEVSTATE_DEFAULT,
- [CC_CALLER_BUSY] = CC_CALLER_BUSY_DEVSTATE_DEFAULT,
- [CC_RECALLING] = CC_RECALLING_DEVSTATE_DEFAULT,
- [CC_COMPLETE] = CC_COMPLETE_DEVSTATE_DEFAULT,
- [CC_FAILED] = CC_FAILED_DEVSTATE_DEFAULT,
- };
- static enum ast_device_state cc_state_to_devstate(enum cc_state state)
- {
- return cc_state_to_devstate_map[state];
- }
- static enum ast_device_state ccss_device_state(const char *device_name)
- {
- struct cc_core_instance *core_instance;
- unsigned long match_flags;
- enum ast_device_state cc_current_state;
- match_flags = MATCH_NO_REQUEST;
- core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent,
- (char *) device_name, &match_flags,
- "Find Core Instance for ccss_device_state reqeust.");
- if (!core_instance) {
- ast_log_dynamic_level(cc_logger_level,
- "Couldn't find a core instance for caller %s\n", device_name);
- return cc_state_to_devstate(CC_FAILED);
- }
- ast_log_dynamic_level(cc_logger_level,
- "Core %d: Found core_instance for caller %s in state %s\n",
- core_instance->core_id, device_name, cc_state_to_string(core_instance->current_state));
- if (strcmp(core_instance->agent->callbacks->type, "generic")) {
- ast_log_dynamic_level(cc_logger_level,
- "Core %d: Device State is only for generic agent types.\n",
- core_instance->core_id);
- cc_unref(core_instance, "Unref core_instance since ccss_device_state was called with native agent");
- return cc_state_to_devstate(CC_FAILED);
- }
- cc_current_state = cc_state_to_devstate(core_instance->current_state);
- cc_unref(core_instance, "Unref core_instance done with ccss_device_state");
- return cc_current_state;
- }
- static void ccss_notify_device_state_change(const char *device, enum cc_state state)
- {
- enum ast_device_state devstate;
- devstate = cc_state_to_devstate(state);
- ast_log_dynamic_level(cc_logger_level,
- "Notification of CCSS state change to '%s', device state '%s' for device '%s'\n",
- cc_state_to_string(state), ast_devstate2str(devstate), device);
- ast_devstate_changed(devstate, AST_DEVSTATE_CACHABLE, "ccss:%s", device);
- }
- #define CC_OFFER_TIMER_DEFAULT 20
- #define CCNR_AVAILABLE_TIMER_DEFAULT 7200
- #define CCBS_AVAILABLE_TIMER_DEFAULT 4800
- #define CC_RECALL_TIMER_DEFAULT 20
- #define CC_MAX_AGENTS_DEFAULT 5
- #define CC_MAX_MONITORS_DEFAULT 5
- #define GLOBAL_CC_MAX_REQUESTS_DEFAULT 20
- static const struct ast_cc_config_params cc_default_params = {
- .cc_agent_policy = AST_CC_AGENT_NEVER,
- .cc_monitor_policy = AST_CC_MONITOR_NEVER,
- .cc_offer_timer = CC_OFFER_TIMER_DEFAULT,
- .ccnr_available_timer = CCNR_AVAILABLE_TIMER_DEFAULT,
- .ccbs_available_timer = CCBS_AVAILABLE_TIMER_DEFAULT,
- .cc_recall_timer = CC_RECALL_TIMER_DEFAULT,
- .cc_max_agents = CC_MAX_AGENTS_DEFAULT,
- .cc_max_monitors = CC_MAX_MONITORS_DEFAULT,
- .cc_callback_macro = "",
- .cc_callback_sub = "",
- .cc_agent_dialstring = "",
- };
- void ast_cc_default_config_params(struct ast_cc_config_params *params)
- {
- *params = cc_default_params;
- }
- struct ast_cc_config_params *__ast_cc_config_params_init(const char *file, int line, const char *function)
- {
- #if defined(__AST_DEBUG_MALLOC)
- struct ast_cc_config_params *params = __ast_malloc(sizeof(*params), file, line, function);
- #else
- struct ast_cc_config_params *params = ast_malloc(sizeof(*params));
- #endif
- if (!params) {
- return NULL;
- }
- ast_cc_default_config_params(params);
- return params;
- }
- void ast_cc_config_params_destroy(struct ast_cc_config_params *params)
- {
- ast_free(params);
- }
- static enum ast_cc_agent_policies str_to_agent_policy(const char * const value)
- {
- if (!strcasecmp(value, "never")) {
- return AST_CC_AGENT_NEVER;
- } else if (!strcasecmp(value, "native")) {
- return AST_CC_AGENT_NATIVE;
- } else if (!strcasecmp(value, "generic")) {
- return AST_CC_AGENT_GENERIC;
- } else {
- ast_log(LOG_WARNING, "%s is an invalid value for cc_agent_policy. Switching to 'never'\n", value);
- return AST_CC_AGENT_NEVER;
- }
- }
- static enum ast_cc_monitor_policies str_to_monitor_policy(const char * const value)
- {
- if (!strcasecmp(value, "never")) {
- return AST_CC_MONITOR_NEVER;
- } else if (!strcasecmp(value, "native")) {
- return AST_CC_MONITOR_NATIVE;
- } else if (!strcasecmp(value, "generic")) {
- return AST_CC_MONITOR_GENERIC;
- } else if (!strcasecmp(value, "always")) {
- return AST_CC_MONITOR_ALWAYS;
- } else {
- ast_log(LOG_WARNING, "%s is an invalid value for cc_monitor_policy. Switching to 'never'\n", value);
- return AST_CC_MONITOR_NEVER;
- }
- }
- static const char *agent_policy_to_str(enum ast_cc_agent_policies policy)
- {
- switch (policy) {
- case AST_CC_AGENT_NEVER:
- return "never";
- case AST_CC_AGENT_NATIVE:
- return "native";
- case AST_CC_AGENT_GENERIC:
- return "generic";
- default:
-
- return "";
- }
- }
- static const char *monitor_policy_to_str(enum ast_cc_monitor_policies policy)
- {
- switch (policy) {
- case AST_CC_MONITOR_NEVER:
- return "never";
- case AST_CC_MONITOR_NATIVE:
- return "native";
- case AST_CC_MONITOR_GENERIC:
- return "generic";
- case AST_CC_MONITOR_ALWAYS:
- return "always";
- default:
-
- return "";
- }
- }
- int ast_cc_get_param(struct ast_cc_config_params *params, const char * const name,
- char *buf, size_t buf_len)
- {
- const char *value = NULL;
- if (!strcasecmp(name, "cc_callback_macro")) {
- value = ast_get_cc_callback_macro(params);
- } else if (!strcasecmp(name, "cc_callback_sub")) {
- value = ast_get_cc_callback_sub(params);
- } else if (!strcasecmp(name, "cc_agent_policy")) {
- value = agent_policy_to_str(ast_get_cc_agent_policy(params));
- } else if (!strcasecmp(name, "cc_monitor_policy")) {
- value = monitor_policy_to_str(ast_get_cc_monitor_policy(params));
- } else if (!strcasecmp(name, "cc_agent_dialstring")) {
- value = ast_get_cc_agent_dialstring(params);
- }
- if (value) {
- ast_copy_string(buf, value, buf_len);
- return 0;
- }
-
- if (!strcasecmp(name, "cc_offer_timer")) {
- snprintf(buf, buf_len, "%u", ast_get_cc_offer_timer(params));
- } else if (!strcasecmp(name, "ccnr_available_timer")) {
- snprintf(buf, buf_len, "%u", ast_get_ccnr_available_timer(params));
- } else if (!strcasecmp(name, "ccbs_available_timer")) {
- snprintf(buf, buf_len, "%u", ast_get_ccbs_available_timer(params));
- } else if (!strcasecmp(name, "cc_max_agents")) {
- snprintf(buf, buf_len, "%u", ast_get_cc_max_agents(params));
- } else if (!strcasecmp(name, "cc_max_monitors")) {
- snprintf(buf, buf_len, "%u", ast_get_cc_max_monitors(params));
- } else if (!strcasecmp(name, "cc_recall_timer")) {
- snprintf(buf, buf_len, "%u", ast_get_cc_recall_timer(params));
- } else {
- ast_log(LOG_WARNING, "%s is not a valid CC parameter. Ignoring.\n", name);
- return -1;
- }
- return 0;
- }
- int ast_cc_set_param(struct ast_cc_config_params *params, const char * const name,
- const char * const value)
- {
- unsigned int value_as_uint;
- if (!strcasecmp(name, "cc_agent_policy")) {
- return ast_set_cc_agent_policy(params, str_to_agent_policy(value));
- } else if (!strcasecmp(name, "cc_monitor_policy")) {
- return ast_set_cc_monitor_policy(params, str_to_monitor_policy(value));
- } else if (!strcasecmp(name, "cc_agent_dialstring")) {
- ast_set_cc_agent_dialstring(params, value);
- } else if (!strcasecmp(name, "cc_callback_macro")) {
- ast_set_cc_callback_macro(params, value);
- return 0;
- } else if (!strcasecmp(name, "cc_callback_sub")) {
- ast_set_cc_callback_sub(params, value);
- return 0;
- }
- if (sscanf(value, "%30u", &value_as_uint) != 1) {
- return -1;
- }
- if (!strcasecmp(name, "cc_offer_timer")) {
- ast_set_cc_offer_timer(params, value_as_uint);
- } else if (!strcasecmp(name, "ccnr_available_timer")) {
- ast_set_ccnr_available_timer(params, value_as_uint);
- } else if (!strcasecmp(name, "ccbs_available_timer")) {
- ast_set_ccbs_available_timer(params, value_as_uint);
- } else if (!strcasecmp(name, "cc_max_agents")) {
- ast_set_cc_max_agents(params, value_as_uint);
- } else if (!strcasecmp(name, "cc_max_monitors")) {
- ast_set_cc_max_monitors(params, value_as_uint);
- } else if (!strcasecmp(name, "cc_recall_timer")) {
- ast_set_cc_recall_timer(params, value_as_uint);
- } else {
- ast_log(LOG_WARNING, "%s is not a valid CC parameter. Ignoring.\n", name);
- return -1;
- }
- return 0;
- }
- int ast_cc_is_config_param(const char * const name)
- {
- return (!strcasecmp(name, "cc_agent_policy") ||
- !strcasecmp(name, "cc_monitor_policy") ||
- !strcasecmp(name, "cc_offer_timer") ||
- !strcasecmp(name, "ccnr_available_timer") ||
- !strcasecmp(name, "ccbs_available_timer") ||
- !strcasecmp(name, "cc_max_agents") ||
- !strcasecmp(name, "cc_max_monitors") ||
- !strcasecmp(name, "cc_callback_macro") ||
- !strcasecmp(name, "cc_callback_sub") ||
- !strcasecmp(name, "cc_agent_dialstring") ||
- !strcasecmp(name, "cc_recall_timer"));
- }
- void ast_cc_copy_config_params(struct ast_cc_config_params *dest, const struct ast_cc_config_params *src)
- {
- *dest = *src;
- }
- enum ast_cc_agent_policies ast_get_cc_agent_policy(struct ast_cc_config_params *config)
- {
- return config->cc_agent_policy;
- }
- int ast_set_cc_agent_policy(struct ast_cc_config_params *config, enum ast_cc_agent_policies value)
- {
-
- if (value < AST_CC_AGENT_NEVER || value > AST_CC_AGENT_GENERIC) {
- return -1;
- }
- config->cc_agent_policy = value;
- return 0;
- }
- enum ast_cc_monitor_policies ast_get_cc_monitor_policy(struct ast_cc_config_params *config)
- {
- return config->cc_monitor_policy;
- }
- int ast_set_cc_monitor_policy(struct ast_cc_config_params *config, enum ast_cc_monitor_policies value)
- {
-
- if (value < AST_CC_MONITOR_NEVER || value > AST_CC_MONITOR_ALWAYS) {
- return -1;
- }
- config->cc_monitor_policy = value;
- return 0;
- }
- unsigned int ast_get_cc_offer_timer(struct ast_cc_config_params *config)
- {
- return config->cc_offer_timer;
- }
- void ast_set_cc_offer_timer(struct ast_cc_config_params *config, unsigned int value)
- {
-
- if (value == 0) {
- ast_log(LOG_WARNING, "0 is an invalid value for cc_offer_timer. Retaining value as %u\n", config->cc_offer_timer);
- return;
- }
- config->cc_offer_timer = value;
- }
- unsigned int ast_get_ccnr_available_timer(struct ast_cc_config_params *config)
- {
- return config->ccnr_available_timer;
- }
- void ast_set_ccnr_available_timer(struct ast_cc_config_params *config, unsigned int value)
- {
-
- if (value == 0) {
- ast_log(LOG_WARNING, "0 is an invalid value for ccnr_available_timer. Retaining value as %u\n", config->ccnr_available_timer);
- return;
- }
- config->ccnr_available_timer = value;
- }
- unsigned int ast_get_cc_recall_timer(struct ast_cc_config_params *config)
- {
- return config->cc_recall_timer;
- }
- void ast_set_cc_recall_timer(struct ast_cc_config_params *config, unsigned int value)
- {
-
- if (value == 0) {
- ast_log(LOG_WARNING, "0 is an invalid value for ccnr_available_timer. Retaining value as %u\n", config->cc_recall_timer);
- return;
- }
- config->cc_recall_timer = value;
- }
- unsigned int ast_get_ccbs_available_timer(struct ast_cc_config_params *config)
- {
- return config->ccbs_available_timer;
- }
- void ast_set_ccbs_available_timer(struct ast_cc_config_params *config, unsigned int value)
- {
-
- if (value == 0) {
- ast_log(LOG_WARNING, "0 is an invalid value for ccbs_available_timer. Retaining value as %u\n", config->ccbs_available_timer);
- return;
- }
- config->ccbs_available_timer = value;
- }
- const char *ast_get_cc_agent_dialstring(struct ast_cc_config_params *config)
- {
- return config->cc_agent_dialstring;
- }
- void ast_set_cc_agent_dialstring(struct ast_cc_config_params *config, const char *const value)
- {
- if (ast_strlen_zero(value)) {
- config->cc_agent_dialstring[0] = '\0';
- } else {
- ast_copy_string(config->cc_agent_dialstring, value, sizeof(config->cc_agent_dialstring));
- }
- }
- unsigned int ast_get_cc_max_agents(struct ast_cc_config_params *config)
- {
- return config->cc_max_agents;
- }
- void ast_set_cc_max_agents(struct ast_cc_config_params *config, unsigned int value)
- {
- config->cc_max_agents = value;
- }
- unsigned int ast_get_cc_max_monitors(struct ast_cc_config_params *config)
- {
- return config->cc_max_monitors;
- }
- void ast_set_cc_max_monitors(struct ast_cc_config_params *config, unsigned int value)
- {
- config->cc_max_monitors = value;
- }
- const char *ast_get_cc_callback_macro(struct ast_cc_config_params *config)
- {
- return config->cc_callback_macro;
- }
- const char *ast_get_cc_callback_sub(struct ast_cc_config_params *config)
- {
- return config->cc_callback_sub;
- }
- void ast_set_cc_callback_macro(struct ast_cc_config_params *config, const char * const value)
- {
- ast_log(LOG_WARNING, "Usage of cc_callback_macro is deprecated. Please use cc_callback_sub instead.\n");
- if (ast_strlen_zero(value)) {
- config->cc_callback_macro[0] = '\0';
- } else {
- ast_copy_string(config->cc_callback_macro, value, sizeof(config->cc_callback_macro));
- }
- }
- void ast_set_cc_callback_sub(struct ast_cc_config_params *config, const char * const value)
- {
- if (ast_strlen_zero(value)) {
- config->cc_callback_sub[0] = '\0';
- } else {
- ast_copy_string(config->cc_callback_sub, value, sizeof(config->cc_callback_sub));
- }
- }
- struct cc_monitor_backend {
- AST_LIST_ENTRY(cc_monitor_backend) next;
- const struct ast_cc_monitor_callbacks *callbacks;
- };
- AST_RWLIST_HEAD_STATIC(cc_monitor_backends, cc_monitor_backend);
- int ast_cc_monitor_register(const struct ast_cc_monitor_callbacks *callbacks)
- {
- struct cc_monitor_backend *backend = ast_calloc(1, sizeof(*backend));
- if (!backend) {
- return -1;
- }
- backend->callbacks = callbacks;
- AST_RWLIST_WRLOCK(&cc_monitor_backends);
- AST_RWLIST_INSERT_TAIL(&cc_monitor_backends, backend, next);
- AST_RWLIST_UNLOCK(&cc_monitor_backends);
- return 0;
- }
- static const struct ast_cc_monitor_callbacks *find_monitor_callbacks(const char * const type)
- {
- struct cc_monitor_backend *backend;
- const struct ast_cc_monitor_callbacks *callbacks = NULL;
- AST_RWLIST_RDLOCK(&cc_monitor_backends);
- AST_RWLIST_TRAVERSE(&cc_monitor_backends, backend, next) {
- if (!strcmp(backend->callbacks->type, type)) {
- ast_log_dynamic_level(cc_logger_level, "Returning monitor backend %s\n", backend->callbacks->type);
- callbacks = backend->callbacks;
- break;
- }
- }
- AST_RWLIST_UNLOCK(&cc_monitor_backends);
- return callbacks;
- }
- void ast_cc_monitor_unregister(const struct ast_cc_monitor_callbacks *callbacks)
- {
- struct cc_monitor_backend *backend;
- AST_RWLIST_WRLOCK(&cc_monitor_backends);
- AST_RWLIST_TRAVERSE_SAFE_BEGIN(&cc_monitor_backends, backend, next) {
- if (backend->callbacks == callbacks) {
- AST_RWLIST_REMOVE_CURRENT(next);
- ast_free(backend);
- break;
- }
- }
- AST_RWLIST_TRAVERSE_SAFE_END;
- AST_RWLIST_UNLOCK(&cc_monitor_backends);
- }
- struct cc_agent_backend {
- AST_LIST_ENTRY(cc_agent_backend) next;
- const struct ast_cc_agent_callbacks *callbacks;
- };
- AST_RWLIST_HEAD_STATIC(cc_agent_backends, cc_agent_backend);
- int ast_cc_agent_register(const struct ast_cc_agent_callbacks *callbacks)
- {
- struct cc_agent_backend *backend = ast_calloc(1, sizeof(*backend));
- if (!backend) {
- return -1;
- }
- backend->callbacks = callbacks;
- AST_RWLIST_WRLOCK(&cc_agent_backends);
- AST_RWLIST_INSERT_TAIL(&cc_agent_backends, backend, next);
- AST_RWLIST_UNLOCK(&cc_agent_backends);
- return 0;
- }
- void ast_cc_agent_unregister(const struct ast_cc_agent_callbacks *callbacks)
- {
- struct cc_agent_backend *backend;
- AST_RWLIST_WRLOCK(&cc_agent_backends);
- AST_RWLIST_TRAVERSE_SAFE_BEGIN(&cc_agent_backends, backend, next) {
- if (backend->callbacks == callbacks) {
- AST_RWLIST_REMOVE_CURRENT(next);
- ast_free(backend);
- break;
- }
- }
- AST_RWLIST_TRAVERSE_SAFE_END;
- AST_RWLIST_UNLOCK(&cc_agent_backends);
- }
- static const struct ast_cc_agent_callbacks *find_agent_callbacks(struct ast_channel *chan)
- {
- struct cc_agent_backend *backend;
- const struct ast_cc_agent_callbacks *callbacks = NULL;
- struct ast_cc_config_params *cc_params;
- char type[32];
- cc_params = ast_channel_get_cc_config_params(chan);
- if (!cc_params) {
- return NULL;
- }
- switch (ast_get_cc_agent_policy(cc_params)) {
- case AST_CC_AGENT_GENERIC:
- ast_copy_string(type, "generic", sizeof(type));
- break;
- case AST_CC_AGENT_NATIVE:
- ast_channel_get_cc_agent_type(chan, type, sizeof(type));
- break;
- default:
- ast_log_dynamic_level(cc_logger_level, "Not returning agent callbacks since this channel is configured not to have a CC agent\n");
- return NULL;
- }
- AST_RWLIST_RDLOCK(&cc_agent_backends);
- AST_RWLIST_TRAVERSE(&cc_agent_backends, backend, next) {
- if (!strcmp(backend->callbacks->type, type)) {
- ast_log_dynamic_level(cc_logger_level, "Returning agent backend %s\n", backend->callbacks->type);
- callbacks = backend->callbacks;
- break;
- }
- }
- AST_RWLIST_UNLOCK(&cc_agent_backends);
- return callbacks;
- }
- static int cc_generic_is_device_available(enum ast_device_state state)
- {
- return state == AST_DEVICE_NOT_INUSE || state == AST_DEVICE_UNKNOWN;
- }
- static int cc_generic_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id);
- static int cc_generic_monitor_suspend(struct ast_cc_monitor *monitor);
- static int cc_generic_monitor_unsuspend(struct ast_cc_monitor *monitor);
- static int cc_generic_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id);
- static void cc_generic_monitor_destructor(void *private_data);
- static struct ast_cc_monitor_callbacks generic_monitor_cbs = {
- .type = "generic",
- .request_cc = cc_generic_monitor_request_cc,
- .suspend = cc_generic_monitor_suspend,
- .unsuspend = cc_generic_monitor_unsuspend,
- .cancel_available_timer = cc_generic_monitor_cancel_available_timer,
- .destructor = cc_generic_monitor_destructor,
- };
- struct ao2_container *generic_monitors;
- struct generic_monitor_instance {
- int core_id;
- int is_suspended;
- int monitoring;
- AST_LIST_ENTRY(generic_monitor_instance) next;
- };
- struct generic_monitor_instance_list {
- const char *device_name;
- enum ast_device_state current_state;
-
- int fit_for_recall;
- struct ast_event_sub *sub;
- AST_LIST_HEAD_NOLOCK(, generic_monitor_instance) list;
- };
- struct generic_monitor_pvt {
-
- const char *device_name;
-
- int core_id;
- };
- static int generic_monitor_hash_fn(const void *obj, const int flags)
- {
- const struct generic_monitor_instance_list *generic_list = obj;
- return ast_str_hash(generic_list->device_name);
- }
- static int generic_monitor_cmp_fn(void *obj, void *arg, int flags)
- {
- const struct generic_monitor_instance_list *generic_list1 = obj;
- const struct generic_monitor_instance_list *generic_list2 = arg;
- return !strcmp(generic_list1->device_name, generic_list2->device_name) ? CMP_MATCH | CMP_STOP : 0;
- }
- static struct generic_monitor_instance_list *find_generic_monitor_instance_list(const char * const device_name)
- {
- struct generic_monitor_instance_list finder = {0};
- char *uppertech = ast_strdupa(device_name);
- ast_tech_to_upper(uppertech);
- finder.device_name = uppertech;
- return ao2_t_find(generic_monitors, &finder, OBJ_POINTER, "Finding generic monitor instance list");
- }
- static void generic_monitor_instance_list_destructor(void *obj)
- {
- struct generic_monitor_instance_list *generic_list = obj;
- struct generic_monitor_instance *generic_instance;
- generic_list->sub = ast_event_unsubscribe(generic_list->sub);
- while ((generic_instance = AST_LIST_REMOVE_HEAD(&generic_list->list, next))) {
- ast_free(generic_instance);
- }
- ast_free((char *)generic_list->device_name);
- }
- static void generic_monitor_devstate_cb(const struct ast_event *event, void *userdata);
- static struct generic_monitor_instance_list *create_new_generic_list(struct ast_cc_monitor *monitor)
- {
- struct generic_monitor_instance_list *generic_list = ao2_t_alloc(sizeof(*generic_list),
- generic_monitor_instance_list_destructor, "allocate generic monitor instance list");
- char * device_name;
- if (!generic_list) {
- return NULL;
- }
- if (!(device_name = ast_strdup(monitor->interface->device_name))) {
- cc_unref(generic_list, "Failed to strdup the monitor's device name");
- return NULL;
- }
- ast_tech_to_upper(device_name);
- generic_list->device_name = device_name;
- if (!(generic_list->sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE,
- generic_monitor_devstate_cb, "Requesting CC", NULL,
- AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, monitor->interface->device_name,
- AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_EXISTS,
- AST_EVENT_IE_END))) {
- cc_unref(generic_list, "Failed to subscribe to device state");
- return NULL;
- }
- generic_list->current_state = ast_device_state(monitor->interface->device_name);
- ao2_t_link(generic_monitors, generic_list, "linking new generic monitor instance list");
- return generic_list;
- }
- struct generic_tp_cb_data {
- const char *device_name;
- enum ast_device_state new_state;
- };
- static int generic_monitor_devstate_tp_cb(void *data)
- {
- struct generic_tp_cb_data *gtcd = data;
- enum ast_device_state new_state = gtcd->new_state;
- enum ast_device_state previous_state = gtcd->new_state;
- const char *monitor_name = gtcd->device_name;
- struct generic_monitor_instance_list *generic_list;
- struct generic_monitor_instance *generic_instance;
- if (!(generic_list = find_generic_monitor_instance_list(monitor_name))) {
-
- ast_free((char *) gtcd->device_name);
- ast_free(gtcd);
- return 0;
- }
- if (generic_list->current_state == new_state) {
-
- cc_unref(generic_list, "Kill reference of generic list in devstate taskprocessor callback");
- ast_free((char *) gtcd->device_name);
- ast_free(gtcd);
- return 0;
- }
- previous_state = generic_list->current_state;
- generic_list->current_state = new_state;
- if (cc_generic_is_device_available(new_state) &&
- (previous_state == AST_DEVICE_INUSE || previous_state == AST_DEVICE_UNAVAILABLE ||
- previous_state == AST_DEVICE_BUSY)) {
- AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
- if (!generic_instance->is_suspended && generic_instance->monitoring) {
- generic_instance->monitoring = 0;
- generic_list->fit_for_recall = 1;
- ast_cc_monitor_callee_available(generic_instance->core_id, "Generic monitored party has become available");
- break;
- }
- }
- }
- cc_unref(generic_list, "Kill reference of generic list in devstate taskprocessor callback");
- ast_free((char *) gtcd->device_name);
- ast_free(gtcd);
- return 0;
- }
- static void generic_monitor_devstate_cb(const struct ast_event *event, void *userdata)
- {
-
- struct generic_tp_cb_data *gtcd = ast_calloc(1, sizeof(*gtcd));
- if (!gtcd) {
- return;
- }
- if (!(gtcd->device_name = ast_strdup(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE)))) {
- ast_free(gtcd);
- return;
- }
- gtcd->new_state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
- if (ast_taskprocessor_push(cc_core_taskprocessor, generic_monitor_devstate_tp_cb, gtcd)) {
- ast_free((char *)gtcd->device_name);
- ast_free(gtcd);
- }
- }
- int ast_cc_available_timer_expire(const void *data)
- {
- struct ast_cc_monitor *monitor = (struct ast_cc_monitor *) data;
- int res;
- monitor->available_timer_id = -1;
- res = ast_cc_monitor_failed(monitor->core_id, monitor->interface->device_name, "Available timer expired for monitor");
- cc_unref(monitor, "Unref reference from scheduler\n");
- return res;
- }
- static int cc_generic_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id)
- {
- struct generic_monitor_instance_list *generic_list;
- struct generic_monitor_instance *generic_instance;
- struct generic_monitor_pvt *gen_mon_pvt;
- enum ast_cc_service_type service = monitor->service_offered;
- int when;
-
- if (!(gen_mon_pvt = ast_calloc(1, sizeof(*gen_mon_pvt)))) {
- return -1;
- }
- if (!(gen_mon_pvt->device_name = ast_strdup(monitor->interface->device_name))) {
- ast_free(gen_mon_pvt);
- return -1;
- }
- gen_mon_pvt->core_id = monitor->core_id;
- monitor->private_data = gen_mon_pvt;
- if (!(generic_list = find_generic_monitor_instance_list(monitor->interface->device_name))) {
- if (!(generic_list = create_new_generic_list(monitor))) {
- return -1;
- }
- }
- if (!(generic_instance = ast_calloc(1, sizeof(*generic_instance)))) {
-
- cc_unref(generic_list, "Generic monitor instance failed to allocate");
- return -1;
- }
- generic_instance->core_id = monitor->core_id;
- generic_instance->monitoring = 1;
- AST_LIST_INSERT_TAIL(&generic_list->list, generic_instance, next);
- when = service == AST_CC_CCBS ? ast_get_ccbs_available_timer(monitor->interface->config_params) :
- ast_get_ccnr_available_timer(monitor->interface->config_params);
- *available_timer_id = ast_sched_add(cc_sched_context, when * 1000,
- ast_cc_available_timer_expire, cc_ref(monitor, "Give the scheduler a monitor reference"));
- if (*available_timer_id == -1) {
- cc_unref(monitor, "Failed to schedule available timer. (monitor)");
- cc_unref(generic_list, "Failed to schedule available timer. (generic_list)");
- return -1;
- }
-
- if (service == AST_CC_CCNR || service == AST_CC_CCNL) {
- generic_list->fit_for_recall = 0;
- }
- ast_cc_monitor_request_acked(monitor->core_id, "Generic monitor for %s subscribed to device state.",
- monitor->interface->device_name);
- cc_unref(generic_list, "Finished with monitor instance reference in request cc callback");
- return 0;
- }
- static int cc_generic_monitor_suspend(struct ast_cc_monitor *monitor)
- {
- struct generic_monitor_instance_list *generic_list;
- struct generic_monitor_instance *generic_instance;
- enum ast_device_state state = ast_device_state(monitor->interface->device_name);
- if (!(generic_list = find_generic_monitor_instance_list(monitor->interface->device_name))) {
- return -1;
- }
-
- AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
- if (generic_instance->core_id == monitor->core_id) {
- generic_instance->is_suspended = 1;
- break;
- }
- }
-
- if (!cc_generic_is_device_available(state)) {
- cc_unref(generic_list, "Device is in use. Nothing to do. Unref generic list.");
- return 0;
- }
-
- AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
- if (!generic_instance->is_suspended) {
- ast_cc_monitor_callee_available(generic_instance->core_id, "Generic monitored party has become available");
- break;
- }
- }
- cc_unref(generic_list, "Done with generic list in suspend callback");
- return 0;
- }
- static int cc_generic_monitor_unsuspend(struct ast_cc_monitor *monitor)
- {
- struct generic_monitor_instance *generic_instance;
- struct generic_monitor_instance_list *generic_list = find_generic_monitor_instance_list(monitor->interface->device_name);
- enum ast_device_state state = ast_device_state(monitor->interface->device_name);
- if (!generic_list) {
- return -1;
- }
-
- if (cc_generic_is_device_available(state)) {
- ast_cc_monitor_callee_available(monitor->core_id, "Generic monitored party has become available");
- }
-
- AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
- if (generic_instance->core_id == monitor->core_id) {
- generic_instance->is_suspended = 0;
- generic_instance->monitoring = 1;
- break;
- }
- }
- cc_unref(generic_list, "Done with generic list in cc_generic_monitor_unsuspend");
- return 0;
- }
- static int cc_generic_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id)
- {
- ast_assert(sched_id != NULL);
- if (*sched_id == -1) {
- return 0;
- }
- ast_log_dynamic_level(cc_logger_level, "Core %d: Canceling generic monitor available timer for monitor %s\n",
- monitor->core_id, monitor->interface->device_name);
- if (!ast_sched_del(cc_sched_context, *sched_id)) {
- cc_unref(monitor, "Remove scheduler's reference to the monitor");
- }
- *sched_id = -1;
- return 0;
- }
- static void cc_generic_monitor_destructor(void *private_data)
- {
- struct generic_monitor_pvt *gen_mon_pvt = private_data;
- struct generic_monitor_instance_list *generic_list;
- struct generic_monitor_instance *generic_instance;
- if (!private_data) {
-
- return;
- }
- ast_log_dynamic_level(cc_logger_level, "Core %d: Destroying generic monitor %s\n",
- gen_mon_pvt->core_id, gen_mon_pvt->device_name);
- if (!(generic_list = find_generic_monitor_instance_list(gen_mon_pvt->device_name))) {
-
- ast_free((char *)gen_mon_pvt->device_name);
- ast_free(gen_mon_pvt);
- return;
- }
- AST_LIST_TRAVERSE_SAFE_BEGIN(&generic_list->list, generic_instance, next) {
- if (generic_instance->core_id == gen_mon_pvt->core_id) {
- AST_LIST_REMOVE_CURRENT(next);
- ast_free(generic_instance);
- break;
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
- if (AST_LIST_EMPTY(&generic_list->list)) {
-
- ao2_t_unlink(generic_monitors, generic_list, "Generic list is empty. Unlink it from the container");
- } else {
-
-
- if (generic_list->fit_for_recall
- && cc_generic_is_device_available(generic_list->current_state)) {
- AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) {
- if (!generic_instance->is_suspended && generic_instance->monitoring) {
- ast_cc_monitor_callee_available(generic_instance->core_id, "Signaling generic monitor "
- "availability due to other instance's failure.");
- break;
- }
- }
- }
- }
- cc_unref(generic_list, "Done with generic list in generic monitor destructor");
- ast_free((char *)gen_mon_pvt->device_name);
- ast_free(gen_mon_pvt);
- }
- static void cc_interface_destroy(void *data)
- {
- struct ast_cc_interface *interface = data;
- ast_log_dynamic_level(cc_logger_level, "Destroying cc interface %s\n", interface->device_name);
- ast_cc_config_params_destroy(interface->config_params);
- }
- struct extension_child_dialstring {
-
- char original_dialstring[AST_CHANNEL_NAME];
-
- char device_name[AST_CHANNEL_NAME];
-
- int is_valid;
- AST_LIST_ENTRY(extension_child_dialstring) next;
- };
- struct extension_monitor_pvt {
- AST_LIST_HEAD_NOLOCK(, extension_child_dialstring) child_dialstrings;
- };
- static void cc_extension_monitor_destructor(void *private_data)
- {
- struct extension_monitor_pvt *extension_pvt = private_data;
- struct extension_child_dialstring *child_dialstring;
-
- if (!extension_pvt) {
- return;
- }
- while ((child_dialstring = AST_LIST_REMOVE_HEAD(&extension_pvt->child_dialstrings, next))) {
- ast_free(child_dialstring);
- }
- ast_free(extension_pvt);
- }
- static void cc_monitor_destroy(void *data)
- {
- struct ast_cc_monitor *monitor = data;
-
- ast_log_dynamic_level(cc_logger_level, "Core %d: Calling destructor for monitor %s\n",
- monitor->core_id, monitor->interface->device_name);
- if (monitor->interface->monitor_class == AST_CC_EXTENSION_MONITOR) {
- cc_extension_monitor_destructor(monitor->private_data);
- }
- if (monitor->callbacks) {
- monitor->callbacks->destructor(monitor->private_data);
- }
- cc_unref(monitor->interface, "Unreffing tree's reference to interface");
- ast_free(monitor->dialstring);
- }
- static void cc_interface_tree_destroy(void *data)
- {
- struct cc_monitor_tree *cc_interface_tree = data;
- struct ast_cc_monitor *monitor;
- while ((monitor = AST_LIST_REMOVE_HEAD(cc_interface_tree, next))) {
- if (monitor->callbacks) {
- monitor->callbacks->cancel_available_timer(monitor, &monitor->available_timer_id);
- }
- cc_unref(monitor, "Destroying all monitors");
- }
- AST_LIST_HEAD_DESTROY(cc_interface_tree);
- }
- static int dialed_cc_interface_counter;
- struct dialed_cc_interfaces {
-
- unsigned int dial_parent_id;
-
- int core_id;
-
- char ignore;
-
- char is_original_caller;
-
- struct cc_monitor_tree *interface_tree;
- };
- static void dialed_cc_interfaces_destroy(void *data)
- {
- struct dialed_cc_interfaces *cc_interfaces = data;
- cc_unref(cc_interfaces->interface_tree, "Unref dial's ref to monitor tree");
- ast_free(cc_interfaces);
- }
- static void *dialed_cc_interfaces_duplicate(void *data)
- {
- struct dialed_cc_interfaces *old_cc_interfaces = data;
- struct dialed_cc_interfaces *new_cc_interfaces = ast_calloc(1, sizeof(*new_cc_interfaces));
- if (!new_cc_interfaces) {
- return NULL;
- }
- new_cc_interfaces->ignore = old_cc_interfaces->ignore;
- new_cc_interfaces->dial_parent_id = old_cc_interfaces->dial_parent_id;
- new_cc_interfaces->is_original_caller = 0;
- cc_ref(old_cc_interfaces->interface_tree, "New ref due to duplication of monitor tree");
- new_cc_interfaces->core_id = old_cc_interfaces->core_id;
- new_cc_interfaces->interface_tree = old_cc_interfaces->interface_tree;
- return new_cc_interfaces;
- }
- static const struct ast_datastore_info dialed_cc_interfaces_info = {
- .type = "Dial CC Interfaces",
- .duplicate = dialed_cc_interfaces_duplicate,
- .destroy = dialed_cc_interfaces_destroy,
- };
- static struct extension_monitor_pvt *extension_monitor_pvt_init(void)
- {
- struct extension_monitor_pvt *ext_pvt = ast_calloc(1, sizeof(*ext_pvt));
- if (!ext_pvt) {
- return NULL;
- }
- AST_LIST_HEAD_INIT_NOLOCK(&ext_pvt->child_dialstrings);
- return ext_pvt;
- }
- void ast_cc_extension_monitor_add_dialstring(struct ast_channel *incoming, const char * const dialstring, const char * const device_name)
- {
- struct ast_datastore *cc_datastore;
- struct dialed_cc_interfaces *cc_interfaces;
- struct ast_cc_monitor *monitor;
- struct extension_monitor_pvt *extension_pvt;
- struct extension_child_dialstring *child_dialstring;
- struct cc_monitor_tree *interface_tree;
- int id;
- ast_channel_lock(incoming);
- if (!(cc_datastore = ast_channel_datastore_find(incoming, &dialed_cc_interfaces_info, NULL))) {
- ast_channel_unlock(incoming);
- return;
- }
- cc_interfaces = cc_datastore->data;
- interface_tree = cc_interfaces->interface_tree;
- id = cc_interfaces->dial_parent_id;
- ast_channel_unlock(incoming);
- AST_LIST_LOCK(interface_tree);
- AST_LIST_TRAVERSE(interface_tree, monitor, next) {
- if (monitor->id == id) {
- break;
- }
- }
- if (!monitor) {
- AST_LIST_UNLOCK(interface_tree);
- return;
- }
- extension_pvt = monitor->private_data;
- if (!(child_dialstring = ast_calloc(1, sizeof(*child_dialstring)))) {
- AST_LIST_UNLOCK(interface_tree);
- return;
- }
- ast_copy_string(child_dialstring->original_dialstring, dialstring, sizeof(child_dialstring->original_dialstring));
- ast_copy_string(child_dialstring->device_name, device_name, sizeof(child_dialstring->device_name));
- child_dialstring->is_valid = 1;
- AST_LIST_INSERT_TAIL(&extension_pvt->child_dialstrings, child_dialstring, next);
- AST_LIST_UNLOCK(interface_tree);
- }
- static void cc_extension_monitor_change_is_valid(struct cc_core_instance *core_instance, unsigned int parent_id, const char * const device_name, int is_valid)
- {
- struct ast_cc_monitor *monitor_iter;
- struct extension_monitor_pvt *extension_pvt;
- struct extension_child_dialstring *child_dialstring;
- AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
- if (monitor_iter->id == parent_id) {
- break;
- }
- }
- if (!monitor_iter) {
- return;
- }
- extension_pvt = monitor_iter->private_data;
- AST_LIST_TRAVERSE(&extension_pvt->child_dialstrings, child_dialstring, next) {
- if (!strcmp(child_dialstring->device_name, device_name)) {
- child_dialstring->is_valid = is_valid;
- break;
- }
- }
- }
- static struct ast_cc_monitor *cc_extension_monitor_init(const char * const exten, const char * const context, const unsigned int parent_id)
- {
- struct ast_str *str = ast_str_alloca(2 * AST_MAX_EXTENSION);
- struct ast_cc_interface *cc_interface;
- struct ast_cc_monitor *monitor;
- ast_str_set(&str, 0, "%s@%s", exten, context);
- if (!(cc_interface = ao2_t_alloc(sizeof(*cc_interface) + ast_str_strlen(str), cc_interface_destroy,
- "Allocating new ast_cc_interface"))) {
- return NULL;
- }
- if (!(monitor = ao2_t_alloc(sizeof(*monitor), cc_monitor_destroy, "Allocating new ast_cc_monitor"))) {
- cc_unref(cc_interface, "failed to allocate the monitor, so unref the interface");
- return NULL;
- }
- if (!(monitor->private_data = extension_monitor_pvt_init())) {
- cc_unref(monitor, "Failed to initialize extension monitor private data. uref monitor");
- cc_unref(cc_interface, "Failed to initialize extension monitor private data. unref cc_interface");
- }
- monitor->id = ast_atomic_fetchadd_int(&dialed_cc_interface_counter, +1);
- monitor->parent_id = parent_id;
- cc_interface->monitor_type = "extension";
- cc_interface->monitor_class = AST_CC_EXTENSION_MONITOR;
- strcpy(cc_interface->device_name, ast_str_buffer(str));
- monitor->interface = cc_interface;
- ast_log_dynamic_level(cc_logger_level, "Created an extension cc interface for '%s' with id %u and parent %u\n", cc_interface->device_name, monitor->id, monitor->parent_id);
- return monitor;
- }
- static int cc_interfaces_datastore_init(struct ast_channel *chan) {
- struct dialed_cc_interfaces *interfaces;
- struct ast_cc_monitor *monitor;
- struct ast_datastore *dial_cc_datastore;
-
- if (!ast_cc_request_is_within_limits()) {
- return 0;
- }
- if (!(interfaces = ast_calloc(1, sizeof(*interfaces)))) {
- return -1;
- }
- if (!(monitor = cc_extension_monitor_init(S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)), S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)), 0))) {
- ast_free(interfaces);
- return -1;
- }
- if (!(dial_cc_datastore = ast_datastore_alloc(&dialed_cc_interfaces_info, NULL))) {
- cc_unref(monitor, "Could not allocate the dialed interfaces datastore. Unreffing monitor");
- ast_free(interfaces);
- return -1;
- }
- if (!(interfaces->interface_tree = ao2_t_alloc(sizeof(*interfaces->interface_tree), cc_interface_tree_destroy,
- "Allocate monitor tree"))) {
- ast_datastore_free(dial_cc_datastore);
- cc_unref(monitor, "Could not allocate monitor tree on dialed interfaces datastore. Unreffing monitor");
- ast_free(interfaces);
- return -1;
- }
-
- AST_LIST_HEAD_INIT(interfaces->interface_tree);
- AST_LIST_INSERT_TAIL(interfaces->interface_tree, monitor, next);
- cc_ref(monitor, "List's reference to extension monitor");
- dial_cc_datastore->data = interfaces;
- dial_cc_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
- interfaces->dial_parent_id = monitor->id;
- interfaces->core_id = monitor->core_id = ast_atomic_fetchadd_int(&core_id_counter, +1);
- interfaces->is_original_caller = 1;
- ast_channel_lock(chan);
- ast_channel_datastore_add(chan, dial_cc_datastore);
- ast_channel_unlock(chan);
- cc_unref(monitor, "Unreffing allocation's reference");
- return 0;
- }
- static void call_destructor_with_no_monitor(const char * const monitor_type, void *private_data)
- {
- const struct ast_cc_monitor_callbacks *monitor_callbacks = find_monitor_callbacks(monitor_type);
- if (!monitor_callbacks) {
- return;
- }
- monitor_callbacks->destructor(private_data);
- }
- static struct ast_cc_monitor *cc_device_monitor_init(const char * const device_name, const char * const dialstring, const struct cc_control_payload *cc_data, int core_id)
- {
- struct ast_cc_interface *cc_interface;
- struct ast_cc_monitor *monitor;
- size_t device_name_len = strlen(device_name);
- int parent_id = cc_data->parent_interface_id;
- if (!(cc_interface = ao2_t_alloc(sizeof(*cc_interface) + device_name_len, cc_interface_destroy,
- "Allocating new ast_cc_interface"))) {
- return NULL;
- }
- if (!(cc_interface->config_params = ast_cc_config_params_init())) {
- cc_unref(cc_interface, "Failed to allocate config params, unref interface");
- return NULL;
- }
- if (!(monitor = ao2_t_alloc(sizeof(*monitor), cc_monitor_destroy, "Allocating new ast_cc_monitor"))) {
- cc_unref(cc_interface, "Failed to allocate monitor, unref interface");
- return NULL;
- }
- if (!(monitor->dialstring = ast_strdup(dialstring))) {
- cc_unref(monitor, "Failed to copy dialable name. Unref monitor");
- cc_unref(cc_interface, "Failed to copy dialable name");
- return NULL;
- }
- if (!(monitor->callbacks = find_monitor_callbacks(cc_data->monitor_type))) {
- cc_unref(monitor, "Failed to find monitor callbacks. Unref monitor");
- cc_unref(cc_interface, "Failed to find monitor callbacks");
- return NULL;
- }
- strcpy(cc_interface->device_name, device_name);
- monitor->id = ast_atomic_fetchadd_int(&dialed_cc_interface_counter, +1);
- monitor->parent_id = parent_id;
- monitor->core_id = core_id;
- monitor->service_offered = cc_data->service;
- monitor->private_data = cc_data->private_data;
- cc_interface->monitor_type = cc_data->monitor_type;
- cc_interface->monitor_class = AST_CC_DEVICE_MONITOR;
- monitor->interface = cc_interface;
- monitor->available_timer_id = -1;
- ast_cc_copy_config_params(cc_interface->config_params, &cc_data->config_params);
- ast_log_dynamic_level(cc_logger_level, "Core %d: Created a device cc interface for '%s' with id %u and parent %u\n",
- monitor->core_id, cc_interface->device_name, monitor->id, monitor->parent_id);
- return monitor;
- }
- void ast_handle_cc_control_frame(struct ast_channel *inbound, struct ast_channel *outbound, void *frame_data)
- {
- char *device_name;
- char *dialstring;
- struct ast_cc_monitor *monitor;
- struct ast_datastore *cc_datastore;
- struct dialed_cc_interfaces *cc_interfaces;
- struct cc_control_payload *cc_data = frame_data;
- struct cc_core_instance *core_instance;
- device_name = cc_data->device_name;
- dialstring = cc_data->dialstring;
- ast_channel_lock(inbound);
- if (!(cc_datastore = ast_channel_datastore_find(inbound, &dialed_cc_interfaces_info, NULL))) {
- ast_log(LOG_WARNING, "Unable to retrieve CC datastore while processing CC frame from '%s'. CC services will be unavailable.\n", device_name);
- ast_channel_unlock(inbound);
- call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
- return;
- }
- cc_interfaces = cc_datastore->data;
- if (cc_interfaces->ignore) {
- ast_channel_unlock(inbound);
- call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
- return;
- }
- if (!cc_interfaces->is_original_caller) {
-
- ast_channel_unlock(inbound);
- ast_indicate_data(inbound, AST_CONTROL_CC, cc_data, sizeof(*cc_data));
- return;
- }
- core_instance = find_cc_core_instance(cc_interfaces->core_id);
- if (!core_instance) {
- core_instance = cc_core_init_instance(inbound, cc_interfaces->interface_tree,
- cc_interfaces->core_id, cc_data);
- if (!core_instance) {
- cc_interfaces->ignore = 1;
- ast_channel_unlock(inbound);
- call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
- return;
- }
- }
- ast_channel_unlock(inbound);
-
- AST_LIST_LOCK(cc_interfaces->interface_tree);
- AST_LIST_TRAVERSE(cc_interfaces->interface_tree, monitor, next) {
- if (!strcmp(monitor->interface->device_name, device_name)) {
- ast_log_dynamic_level(cc_logger_level, "Core %d: Device %s sent us multiple CC control frames. Ignoring those beyond the first.\n",
- core_instance->core_id, device_name);
- AST_LIST_UNLOCK(cc_interfaces->interface_tree);
- cc_unref(core_instance, "Returning early from ast_handle_cc_control_frame. Unref core_instance");
- call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
- return;
- }
- }
- AST_LIST_UNLOCK(cc_interfaces->interface_tree);
- if (!(monitor = cc_device_monitor_init(device_name, dialstring, cc_data, core_instance->core_id))) {
- ast_log(LOG_WARNING, "Unable to create CC device interface for '%s'. CC services will be unavailable on this interface.\n", device_name);
- cc_unref(core_instance, "Returning early from ast_handle_cc_control_frame. Unref core_instance");
- call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data);
- return;
- }
- AST_LIST_LOCK(cc_interfaces->interface_tree);
- cc_ref(monitor, "monitor tree's reference to the monitor");
- AST_LIST_INSERT_TAIL(cc_interfaces->interface_tree, monitor, next);
- AST_LIST_UNLOCK(cc_interfaces->interface_tree);
- cc_extension_monitor_change_is_valid(core_instance, monitor->parent_id, monitor->interface->device_name, 0);
- manager_event(EVENT_FLAG_CC, "CCAvailable",
- "CoreID: %d\r\n"
- "Callee: %s\r\n"
- "Service: %s\r\n",
- cc_interfaces->core_id, device_name, cc_service_to_string(cc_data->service)
- );
- cc_unref(core_instance, "Done with core_instance after handling CC control frame");
- cc_unref(monitor, "Unref reference from allocating monitor");
- }
- int ast_cc_call_init(struct ast_channel *chan, int *ignore_cc)
- {
-
- struct ast_datastore *cc_interfaces_datastore;
- struct dialed_cc_interfaces *interfaces;
- struct ast_cc_monitor *monitor;
- struct ast_cc_config_params *cc_params;
- ast_channel_lock(chan);
- cc_params = ast_channel_get_cc_config_params(chan);
- if (!cc_params) {
- ast_channel_unlock(chan);
- return -1;
- }
- if (ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_NEVER) {
-
- *ignore_cc = 1;
- ast_channel_unlock(chan);
- ast_log_dynamic_level(cc_logger_level, "Agent policy for %s is 'never'. CC not possible\n", ast_channel_name(chan));
- return 0;
- }
- if (!(cc_interfaces_datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
-
- ast_channel_unlock(chan);
- return cc_interfaces_datastore_init(chan);
- }
- interfaces = cc_interfaces_datastore->data;
- ast_channel_unlock(chan);
- if (interfaces->ignore) {
-
- *ignore_cc = 1;
- ast_log_dynamic_level(cc_logger_level, "Datastore is present with ignore flag set. Ignoring CC offers on this call\n");
- return 0;
- }
-
- if (!(monitor = cc_extension_monitor_init(S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)),
- S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)), interfaces->dial_parent_id))) {
- return -1;
- }
- monitor->core_id = interfaces->core_id;
- AST_LIST_LOCK(interfaces->interface_tree);
- cc_ref(monitor, "monitor tree's reference to the monitor");
- AST_LIST_INSERT_TAIL(interfaces->interface_tree, monitor, next);
- AST_LIST_UNLOCK(interfaces->interface_tree);
- interfaces->dial_parent_id = monitor->id;
- cc_unref(monitor, "Unref monitor's allocation reference");
- return 0;
- }
- int ast_cc_request_is_within_limits(void)
- {
- return cc_request_count < global_cc_max_requests;
- }
- int ast_cc_get_current_core_id(struct ast_channel *chan)
- {
- struct ast_datastore *datastore;
- struct dialed_cc_interfaces *cc_interfaces;
- int core_id_return;
- ast_channel_lock(chan);
- if (!(datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
- ast_channel_unlock(chan);
- return -1;
- }
- cc_interfaces = datastore->data;
- core_id_return = cc_interfaces->ignore ? -1 : cc_interfaces->core_id;
- ast_channel_unlock(chan);
- return core_id_return;
- }
- static long count_agents(const char * const caller, const int core_id_exception)
- {
- struct count_agents_cb_data data = {.core_id_exception = core_id_exception,};
- ao2_t_callback_data(cc_core_instances, OBJ_NODATA, count_agents_cb, (char *)caller, &data, "Counting agents");
- ast_log_dynamic_level(cc_logger_level, "Counted %d agents\n", data.count);
- return data.count;
- }
- static void kill_duplicate_offers(char *caller)
- {
- unsigned long match_flags = MATCH_NO_REQUEST;
- struct ao2_iterator *dups_iter;
-
- dups_iter = ao2_t_callback_data(cc_core_instances, OBJ_MULTIPLE | OBJ_UNLINK,
- match_agent, caller, &match_flags, "Killing duplicate offers");
- if (dups_iter) {
-
- ao2_iterator_destroy(dups_iter);
- }
- }
- static void check_callback_sanity(const struct ast_cc_agent_callbacks *callbacks)
- {
- ast_assert(callbacks->init != NULL);
- ast_assert(callbacks->start_offer_timer != NULL);
- ast_assert(callbacks->stop_offer_timer != NULL);
- ast_assert(callbacks->respond != NULL);
- ast_assert(callbacks->status_request != NULL);
- ast_assert(callbacks->start_monitoring != NULL);
- ast_assert(callbacks->callee_available != NULL);
- ast_assert(callbacks->destructor != NULL);
- }
- static void agent_destroy(void *data)
- {
- struct ast_cc_agent *agent = data;
- if (agent->callbacks) {
- agent->callbacks->destructor(agent);
- }
- ast_cc_config_params_destroy(agent->cc_params);
- }
- static struct ast_cc_agent *cc_agent_init(struct ast_channel *caller_chan,
- const char * const caller_name, const int core_id,
- struct cc_monitor_tree *interface_tree)
- {
- struct ast_cc_agent *agent;
- struct ast_cc_config_params *cc_params;
- if (!(agent = ao2_t_alloc(sizeof(*agent) + strlen(caller_name), agent_destroy,
- "Allocating new ast_cc_agent"))) {
- return NULL;
- }
- agent->core_id = core_id;
- strcpy(agent->device_name, caller_name);
- cc_params = ast_channel_get_cc_config_params(caller_chan);
- if (!cc_params) {
- cc_unref(agent, "Could not get channel config params.");
- return NULL;
- }
- if (!(agent->cc_params = ast_cc_config_params_init())) {
- cc_unref(agent, "Could not init agent config params.");
- return NULL;
- }
- ast_cc_copy_config_params(agent->cc_params, cc_params);
- if (!(agent->callbacks = find_agent_callbacks(caller_chan))) {
- cc_unref(agent, "Could not find agent callbacks.");
- return NULL;
- }
- check_callback_sanity(agent->callbacks);
- if (agent->callbacks->init(agent, caller_chan)) {
- cc_unref(agent, "Agent init callback failed.");
- return NULL;
- }
- ast_log_dynamic_level(cc_logger_level, "Core %u: Created an agent for caller %s\n",
- agent->core_id, agent->device_name);
- return agent;
- }
- static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan);
- static int cc_generic_agent_start_offer_timer(struct ast_cc_agent *agent);
- static int cc_generic_agent_stop_offer_timer(struct ast_cc_agent *agent);
- static void cc_generic_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason);
- static int cc_generic_agent_status_request(struct ast_cc_agent *agent);
- static int cc_generic_agent_stop_ringing(struct ast_cc_agent *agent);
- static int cc_generic_agent_start_monitoring(struct ast_cc_agent *agent);
- static int cc_generic_agent_recall(struct ast_cc_agent *agent);
- static void cc_generic_agent_destructor(struct ast_cc_agent *agent);
- static struct ast_cc_agent_callbacks generic_agent_callbacks = {
- .type = "generic",
- .init = cc_generic_agent_init,
- .start_offer_timer = cc_generic_agent_start_offer_timer,
- .stop_offer_timer = cc_generic_agent_stop_offer_timer,
- .respond = cc_generic_agent_respond,
- .status_request = cc_generic_agent_status_request,
- .stop_ringing = cc_generic_agent_stop_ringing,
- .start_monitoring = cc_generic_agent_start_monitoring,
- .callee_available = cc_generic_agent_recall,
- .destructor = cc_generic_agent_destructor,
- };
- struct cc_generic_agent_pvt {
-
- struct ast_event_sub *sub;
-
- int offer_timer_id;
-
- char cid_num[AST_CHANNEL_NAME];
-
- char cid_name[AST_CHANNEL_NAME];
-
- char exten[AST_CHANNEL_NAME];
-
- char context[AST_CHANNEL_NAME];
- };
- static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan)
- {
- struct cc_generic_agent_pvt *generic_pvt = ast_calloc(1, sizeof(*generic_pvt));
- if (!generic_pvt) {
- return -1;
- }
- generic_pvt->offer_timer_id = -1;
- if (ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str) {
- ast_copy_string(generic_pvt->cid_num, ast_channel_caller(chan)->id.number.str, sizeof(generic_pvt->cid_num));
- }
- if (ast_channel_caller(chan)->id.name.valid && ast_channel_caller(chan)->id.name.str) {
- ast_copy_string(generic_pvt->cid_name, ast_channel_caller(chan)->id.name.str, sizeof(generic_pvt->cid_name));
- }
- ast_copy_string(generic_pvt->exten, S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan)), sizeof(generic_pvt->exten));
- ast_copy_string(generic_pvt->context, S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan)), sizeof(generic_pvt->context));
- agent->private_data = generic_pvt;
- ast_set_flag(agent, AST_CC_AGENT_SKIP_OFFER);
- return 0;
- }
- static int offer_timer_expire(const void *data)
- {
- struct ast_cc_agent *agent = (struct ast_cc_agent *) data;
- struct cc_generic_agent_pvt *agent_pvt = agent->private_data;
- ast_log_dynamic_level(cc_logger_level, "Core %u: Queuing change request because offer timer has expired.\n",
- agent->core_id);
- agent_pvt->offer_timer_id = -1;
- ast_cc_failed(agent->core_id, "Generic agent %s offer timer expired", agent->device_name);
- cc_unref(agent, "Remove scheduler's reference to the agent");
- return 0;
- }
- static int cc_generic_agent_start_offer_timer(struct ast_cc_agent *agent)
- {
- int when;
- int sched_id;
- struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
- ast_assert(cc_sched_context != NULL);
- ast_assert(agent->cc_params != NULL);
- when = ast_get_cc_offer_timer(agent->cc_params) * 1000;
- ast_log_dynamic_level(cc_logger_level, "Core %u: About to schedule offer timer expiration for %d ms\n",
- agent->core_id, when);
- if ((sched_id = ast_sched_add(cc_sched_context, when, offer_timer_expire, cc_ref(agent, "Give scheduler an agent ref"))) == -1) {
- return -1;
- }
- generic_pvt->offer_timer_id = sched_id;
- return 0;
- }
- static int cc_generic_agent_stop_offer_timer(struct ast_cc_agent *agent)
- {
- struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
- if (generic_pvt->offer_timer_id != -1) {
- if (!ast_sched_del(cc_sched_context, generic_pvt->offer_timer_id)) {
- cc_unref(agent, "Remove scheduler's reference to the agent");
- }
- generic_pvt->offer_timer_id = -1;
- }
- return 0;
- }
- static void cc_generic_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason)
- {
-
- return;
- }
- static int cc_generic_agent_status_request(struct ast_cc_agent *agent)
- {
- ast_cc_agent_status_response(agent->core_id, ast_device_state(agent->device_name));
- return 0;
- }
- static int cc_generic_agent_stop_ringing(struct ast_cc_agent *agent)
- {
- struct ast_channel *recall_chan = ast_channel_get_by_name_prefix(agent->device_name, strlen(agent->device_name));
- if (!recall_chan) {
- return 0;
- }
- ast_softhangup(recall_chan, AST_SOFTHANGUP_EXPLICIT);
- return 0;
- }
- static int generic_agent_devstate_unsubscribe(void *data)
- {
- struct ast_cc_agent *agent = data;
- struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
- if (generic_pvt->sub != NULL) {
- generic_pvt->sub = ast_event_unsubscribe(generic_pvt->sub);
- }
- cc_unref(agent, "Done unsubscribing from devstate");
- return 0;
- }
- static void generic_agent_devstate_cb(const struct ast_event *event, void *userdata)
- {
- struct ast_cc_agent *agent = userdata;
- enum ast_device_state new_state;
- new_state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
- if (!cc_generic_is_device_available(new_state)) {
-
- return;
- }
-
- if (ast_taskprocessor_push(cc_core_taskprocessor, generic_agent_devstate_unsubscribe,
- cc_ref(agent, "ref agent for device state unsubscription"))) {
- cc_unref(agent, "Unref agent unsubscribing from devstate failed");
- }
- ast_cc_agent_caller_available(agent->core_id, "%s is no longer busy", agent->device_name);
- }
- static int cc_generic_agent_start_monitoring(struct ast_cc_agent *agent)
- {
- struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
- struct ast_str *str = ast_str_alloca(128);
- ast_assert(generic_pvt->sub == NULL);
- ast_str_set(&str, 0, "Agent monitoring %s device state since it is busy\n",
- agent->device_name);
- if (!(generic_pvt->sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE,
- generic_agent_devstate_cb, ast_str_buffer(str), agent,
- AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, agent->device_name,
- AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_EXISTS,
- AST_EVENT_IE_END))) {
- return -1;
- }
- return 0;
- }
- static void *generic_recall(void *data)
- {
- struct ast_cc_agent *agent = data;
- struct cc_generic_agent_pvt *generic_pvt = agent->private_data;
- const char *interface = S_OR(ast_get_cc_agent_dialstring(agent->cc_params), ast_strdupa(agent->device_name));
- const char *tech;
- char *target;
- int reason;
- struct ast_channel *chan;
- const char *callback_macro = ast_get_cc_callback_macro(agent->cc_params);
- const char *callback_sub = ast_get_cc_callback_sub(agent->cc_params);
- unsigned int recall_timer = ast_get_cc_recall_timer(agent->cc_params) * 1000;
- struct ast_format tmp_fmt;
- struct ast_format_cap *tmp_cap = ast_format_cap_alloc_nolock();
- if (!tmp_cap) {
- return NULL;
- }
- tech = interface;
- if ((target = strchr(interface, '/'))) {
- *target++ = '\0';
- }
- ast_format_cap_add(tmp_cap, ast_format_set(&tmp_fmt, AST_FORMAT_SLINEAR, 0));
- if (!(chan = ast_request_and_dial(tech, tmp_cap, NULL, target, recall_timer, &reason, generic_pvt->cid_num, generic_pvt->cid_name))) {
-
- ast_log_dynamic_level(cc_logger_level, "Core %u: Failed to call back %s for reason %d\n",
- agent->core_id, agent->device_name, reason);
- ast_cc_failed(agent->core_id, "Failed to call back device %s/%s", tech, target);
- ast_format_cap_destroy(tmp_cap);
- return NULL;
- }
- ast_format_cap_destroy(tmp_cap);
-
-
- ast_setup_cc_recall_datastore(chan, agent->core_id);
- ast_cc_agent_set_interfaces_chanvar(chan);
- ast_channel_exten_set(chan, generic_pvt->exten);
- ast_channel_context_set(chan, generic_pvt->context);
- ast_channel_priority_set(chan, 1);
- pbx_builtin_setvar_helper(chan, "CC_EXTEN", generic_pvt->exten);
- pbx_builtin_setvar_helper(chan, "CC_CONTEXT", generic_pvt->context);
- if (!ast_strlen_zero(callback_macro)) {
- ast_log_dynamic_level(cc_logger_level, "Core %u: There's a callback macro configured for agent %s\n",
- agent->core_id, agent->device_name);
- if (ast_app_exec_macro(NULL, chan, callback_macro)) {
- ast_cc_failed(agent->core_id, "Callback macro to %s failed. Maybe a hangup?", agent->device_name);
- ast_hangup(chan);
- return NULL;
- }
- }
- if (!ast_strlen_zero(callback_sub)) {
- ast_log_dynamic_level(cc_logger_level, "Core %u: There's a callback subroutine configured for agent %s\n",
- agent->core_id, agent->device_name);
- if (ast_app_exec_sub(NULL, chan, callback_sub, 0)) {
- ast_cc_failed(agent->core_id, "Callback subroutine to %s failed. Maybe a hangup?", agent->device_name);
- ast_hangup(chan);
- return NULL;
- }
- }
- if (ast_pbx_start(chan)) {
- ast_cc_failed(agent->core_id, "PBX failed to start for %s.", agent->device_name);
- ast_hangup(chan);
- return NULL;
- }
- ast_cc_agent_recalling(agent->core_id, "Generic agent %s is recalling",
- agent->device_name);
- return NULL;
- }
- static int cc_generic_agent_recall(struct ast_cc_agent *agent)
- {
- pthread_t clotho;
- enum ast_device_state current_state = ast_device_state(agent->device_name);
- if (!cc_generic_is_device_available(current_state)) {
-
- ast_cc_agent_caller_busy(agent->core_id, "Generic agent caller %s is busy", agent->device_name);
- return 0;
- }
- ast_pthread_create_detached_background(&clotho, NULL, generic_recall, agent);
- return 0;
- }
- static void cc_generic_agent_destructor(struct ast_cc_agent *agent)
- {
- struct cc_generic_agent_pvt *agent_pvt = agent->private_data;
- if (!agent_pvt) {
-
- return;
- }
- cc_generic_agent_stop_offer_timer(agent);
- if (agent_pvt->sub) {
- agent_pvt->sub = ast_event_unsubscribe(agent_pvt->sub);
- }
- ast_free(agent_pvt);
- }
- static void cc_core_instance_destructor(void *data)
- {
- struct cc_core_instance *core_instance = data;
- ast_log_dynamic_level(cc_logger_level, "Core %d: Destroying core instance\n", core_instance->core_id);
- if (core_instance->agent) {
- cc_unref(core_instance->agent, "Core instance is done with the agent now");
- }
- if (core_instance->monitors) {
- core_instance->monitors = cc_unref(core_instance->monitors, "Core instance is done with interface list");
- }
- }
- static struct cc_core_instance *cc_core_init_instance(struct ast_channel *caller_chan,
- struct cc_monitor_tree *called_tree, const int core_id, struct cc_control_payload *cc_data)
- {
- char caller[AST_CHANNEL_NAME];
- struct cc_core_instance *core_instance;
- struct ast_cc_config_params *cc_params;
- long agent_count;
- int recall_core_id;
- ast_channel_get_device_name(caller_chan, caller, sizeof(caller));
- cc_params = ast_channel_get_cc_config_params(caller_chan);
- if (!cc_params) {
- ast_log_dynamic_level(cc_logger_level, "Could not get CC parameters for %s\n",
- caller);
- return NULL;
- }
-
- if (ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_GENERIC) {
- kill_duplicate_offers(caller);
- }
- ast_cc_is_recall(caller_chan, &recall_core_id, NULL);
- agent_count = count_agents(caller, recall_core_id);
- if (agent_count >= ast_get_cc_max_agents(cc_params)) {
- ast_log_dynamic_level(cc_logger_level, "Caller %s already has the maximum number of agents configured\n", caller);
- return NULL;
- }
-
- if (agent_count > 0 && ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_GENERIC) {
- ast_log_dynamic_level(cc_logger_level, "Generic agents can only have a single outstanding request\n");
- return NULL;
- }
-
- if (!(core_instance = ao2_t_alloc(sizeof(*core_instance), cc_core_instance_destructor, "Creating core instance for CC"))) {
- return NULL;
- }
- core_instance->core_id = core_id;
- if (!(core_instance->agent = cc_agent_init(caller_chan, caller, core_instance->core_id, called_tree))) {
- cc_unref(core_instance, "Couldn't allocate agent, unref core_instance");
- return NULL;
- }
- core_instance->monitors = cc_ref(called_tree, "Core instance getting ref to monitor tree");
- ao2_t_link(cc_core_instances, core_instance, "Link core instance into container");
- return core_instance;
- }
- struct cc_state_change_args {
- struct cc_core_instance *core_instance;
- enum cc_state state;
- int core_id;
- char debug[1];
- };
- static int is_state_change_valid(enum cc_state current_state, const enum cc_state new_state, struct ast_cc_agent *agent)
- {
- int is_valid = 0;
- switch (new_state) {
- case CC_AVAILABLE:
- ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to state %u? That should never happen.\n",
- agent->core_id, new_state);
- break;
- case CC_CALLER_OFFERED:
- if (current_state == CC_AVAILABLE) {
- is_valid = 1;
- }
- break;
- case CC_CALLER_REQUESTED:
- if (current_state == CC_CALLER_OFFERED ||
- (current_state == CC_AVAILABLE && ast_test_flag(agent, AST_CC_AGENT_SKIP_OFFER))) {
- is_valid = 1;
- }
- break;
- case CC_ACTIVE:
- if (current_state == CC_CALLER_REQUESTED || current_state == CC_CALLER_BUSY) {
- is_valid = 1;
- }
- break;
- case CC_CALLEE_READY:
- if (current_state == CC_ACTIVE) {
- is_valid = 1;
- }
- break;
- case CC_CALLER_BUSY:
- if (current_state == CC_CALLEE_READY) {
- is_valid = 1;
- }
- break;
- case CC_RECALLING:
- if (current_state == CC_CALLEE_READY) {
- is_valid = 1;
- }
- break;
- case CC_COMPLETE:
- if (current_state == CC_RECALLING) {
- is_valid = 1;
- }
- break;
- case CC_FAILED:
- is_valid = 1;
- break;
- default:
- ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to unknown state %u\n",
- agent->core_id, new_state);
- break;
- }
- return is_valid;
- }
- static int cc_available(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
- {
-
- ast_log(LOG_WARNING, "Someone requested to change to CC_AVAILABLE? Ignoring.\n");
- return -1;
- }
- static int cc_caller_offered(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
- {
- if (core_instance->agent->callbacks->start_offer_timer(core_instance->agent)) {
- ast_cc_failed(core_instance->core_id, "Failed to start the offer timer for %s\n",
- core_instance->agent->device_name);
- return -1;
- }
- manager_event(EVENT_FLAG_CC, "CCOfferTimerStart",
- "CoreID: %d\r\n"
- "Caller: %s\r\n"
- "Expires: %u\r\n",
- core_instance->core_id, core_instance->agent->device_name, core_instance->agent->cc_params->cc_offer_timer);
- ast_log_dynamic_level(cc_logger_level, "Core %d: Started the offer timer for the agent %s!\n",
- core_instance->core_id, core_instance->agent->device_name);
- return 0;
- }
- static int has_device_monitors(struct cc_core_instance *core_instance)
- {
- struct ast_cc_monitor *iter;
- int res = 0;
- AST_LIST_TRAVERSE(core_instance->monitors, iter, next) {
- if (iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
- res = 1;
- break;
- }
- }
- return res;
- }
- static void request_cc(struct cc_core_instance *core_instance)
- {
- struct ast_cc_monitor *monitor_iter;
- AST_LIST_LOCK(core_instance->monitors);
- AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
- if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
- if (monitor_iter->callbacks->request_cc(monitor_iter, &monitor_iter->available_timer_id)) {
- AST_LIST_REMOVE_CURRENT(next);
- cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
- monitor_iter->interface->device_name, 1);
- cc_unref(monitor_iter, "request_cc failed. Unref list's reference to monitor");
- } else {
- manager_event(EVENT_FLAG_CC, "CCRequested",
- "CoreID: %d\r\n"
- "Caller: %s\r\n"
- "Callee: %s\r\n",
- core_instance->core_id, core_instance->agent->device_name, monitor_iter->interface->device_name);
- }
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
- if (!has_device_monitors(core_instance)) {
- ast_cc_failed(core_instance->core_id, "All device monitors failed to request CC");
- }
- AST_LIST_UNLOCK(core_instance->monitors);
- }
- static int cc_caller_requested(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
- {
- if (!ast_cc_request_is_within_limits()) {
- ast_log(LOG_WARNING, "Cannot request CC since there is no more room for requests\n");
- core_instance->agent->callbacks->respond(core_instance->agent,
- AST_CC_AGENT_RESPONSE_FAILURE_TOO_MANY);
- ast_cc_failed(core_instance->core_id, "Too many requests in the system");
- return -1;
- }
- core_instance->agent->callbacks->stop_offer_timer(core_instance->agent);
- request_cc(core_instance);
- return 0;
- }
- static void unsuspend(struct cc_core_instance *core_instance)
- {
- struct ast_cc_monitor *monitor_iter;
- AST_LIST_LOCK(core_instance->monitors);
- AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
- if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
- if (monitor_iter->callbacks->unsuspend(monitor_iter)) {
- AST_LIST_REMOVE_CURRENT(next);
- cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
- monitor_iter->interface->device_name, 1);
- cc_unref(monitor_iter, "unsuspend failed. Unref list's reference to monitor");
- }
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
- if (!has_device_monitors(core_instance)) {
- ast_cc_failed(core_instance->core_id, "All device monitors failed to unsuspend CC");
- }
- AST_LIST_UNLOCK(core_instance->monitors);
- }
- static int cc_active(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
- {
-
- if (previous_state == CC_CALLER_REQUESTED) {
- core_instance->agent->callbacks->respond(core_instance->agent,
- AST_CC_AGENT_RESPONSE_SUCCESS);
- manager_event(EVENT_FLAG_CC, "CCRequestAcknowledged",
- "CoreID: %d\r\n"
- "Caller: %s\r\n",
- core_instance->core_id, core_instance->agent->device_name);
- } else if (previous_state == CC_CALLER_BUSY) {
- manager_event(EVENT_FLAG_CC, "CCCallerStopMonitoring",
- "CoreID: %d\r\n"
- "Caller: %s\r\n",
- core_instance->core_id, core_instance->agent->device_name);
- unsuspend(core_instance);
- }
-
- return 0;
- }
- static int cc_callee_ready(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
- {
- core_instance->agent->callbacks->callee_available(core_instance->agent);
- return 0;
- }
- static void suspend(struct cc_core_instance *core_instance)
- {
- struct ast_cc_monitor *monitor_iter;
- AST_LIST_LOCK(core_instance->monitors);
- AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
- if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
- if (monitor_iter->callbacks->suspend(monitor_iter)) {
- AST_LIST_REMOVE_CURRENT(next);
- cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
- monitor_iter->interface->device_name, 1);
- cc_unref(monitor_iter, "suspend failed. Unref list's reference to monitor");
- }
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
- if (!has_device_monitors(core_instance)) {
- ast_cc_failed(core_instance->core_id, "All device monitors failed to suspend CC");
- }
- AST_LIST_UNLOCK(core_instance->monitors);
- }
- static int cc_caller_busy(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
- {
-
- suspend(core_instance);
- core_instance->agent->callbacks->start_monitoring(core_instance->agent);
- manager_event(EVENT_FLAG_CC, "CCCallerStartMonitoring",
- "CoreID: %d\r\n"
- "Caller: %s\r\n",
- core_instance->core_id, core_instance->agent->device_name);
- return 0;
- }
- static void cancel_available_timer(struct cc_core_instance *core_instance)
- {
- struct ast_cc_monitor *monitor_iter;
- AST_LIST_LOCK(core_instance->monitors);
- AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
- if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
- if (monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id)) {
- AST_LIST_REMOVE_CURRENT(next);
- cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
- monitor_iter->interface->device_name, 1);
- cc_unref(monitor_iter, "cancel_available_timer failed. Unref list's reference to monitor");
- }
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
- if (!has_device_monitors(core_instance)) {
- ast_cc_failed(core_instance->core_id, "All device monitors failed to cancel their available timers");
- }
- AST_LIST_UNLOCK(core_instance->monitors);
- }
- static int cc_recalling(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
- {
-
- cancel_available_timer(core_instance);
- manager_event(EVENT_FLAG_CC, "CCCallerRecalling",
- "CoreID: %d\r\n"
- "Caller: %s\r\n",
- core_instance->core_id, core_instance->agent->device_name);
- return 0;
- }
- static int cc_complete(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
- {
-
- manager_event(EVENT_FLAG_CC, "CCRecallComplete",
- "CoreID: %d\r\n"
- "Caller: %s\r\n",
- core_instance->core_id, core_instance->agent->device_name);
- ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC recall has completed");
- return 0;
- }
- static int cc_failed(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state)
- {
- manager_event(EVENT_FLAG_CC, "CCFailure",
- "CoreID: %d\r\n"
- "Caller: %s\r\n"
- "Reason: %s\r\n",
- core_instance->core_id, core_instance->agent->device_name, args->debug);
- ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC failed");
- return 0;
- }
- static int (* const state_change_funcs [])(struct cc_core_instance *, struct cc_state_change_args *, enum cc_state previous_state) = {
- [CC_AVAILABLE] = cc_available,
- [CC_CALLER_OFFERED] = cc_caller_offered,
- [CC_CALLER_REQUESTED] = cc_caller_requested,
- [CC_ACTIVE] = cc_active,
- [CC_CALLEE_READY] = cc_callee_ready,
- [CC_CALLER_BUSY] = cc_caller_busy,
- [CC_RECALLING] = cc_recalling,
- [CC_COMPLETE] = cc_complete,
- [CC_FAILED] = cc_failed,
- };
- static int cc_do_state_change(void *datap)
- {
- struct cc_state_change_args *args = datap;
- struct cc_core_instance *core_instance;
- enum cc_state previous_state;
- int res;
- ast_log_dynamic_level(cc_logger_level, "Core %d: State change to %u requested. Reason: %s\n",
- args->core_id, args->state, args->debug);
- core_instance = args->core_instance;
- if (!is_state_change_valid(core_instance->current_state, args->state, core_instance->agent)) {
- ast_log_dynamic_level(cc_logger_level, "Core %d: Invalid state change requested. Cannot go from %s to %s\n",
- args->core_id, cc_state_to_string(core_instance->current_state), cc_state_to_string(args->state));
- if (args->state == CC_CALLER_REQUESTED) {
-
- core_instance->agent->callbacks->respond(core_instance->agent,
- AST_CC_AGENT_RESPONSE_FAILURE_INVALID);
- }
- ast_free(args);
- cc_unref(core_instance, "Unref core instance from when it was found earlier");
- return -1;
- }
-
- previous_state = core_instance->current_state;
- core_instance->current_state = args->state;
- res = state_change_funcs[core_instance->current_state](core_instance, args, previous_state);
-
- if (!res && !strcmp(core_instance->agent->callbacks->type, "generic")) {
- ccss_notify_device_state_change(core_instance->agent->device_name, core_instance->current_state);
- }
- ast_free(args);
- cc_unref(core_instance, "Unref since state change has completed");
- return res;
- }
- static int cc_request_state_change(enum cc_state state, const int core_id, const char *debug, va_list ap)
- {
- int res;
- int debuglen;
- char dummy[1];
- va_list aq;
- struct cc_core_instance *core_instance;
- struct cc_state_change_args *args;
-
- va_copy(aq, ap);
-
- debuglen = vsnprintf(dummy, sizeof(dummy), debug, aq) + 1;
- va_end(aq);
- if (!(args = ast_calloc(1, sizeof(*args) + debuglen))) {
- return -1;
- }
- core_instance = find_cc_core_instance(core_id);
- if (!core_instance) {
- ast_log_dynamic_level(cc_logger_level, "Core %d: Unable to find core instance.\n",
- core_id);
- ast_free(args);
- return -1;
- }
- args->core_instance = core_instance;
- args->state = state;
- args->core_id = core_id;
- vsnprintf(args->debug, debuglen, debug, ap);
- res = ast_taskprocessor_push(cc_core_taskprocessor, cc_do_state_change, args);
- if (res) {
- cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
- ast_free(args);
- }
- return res;
- }
- struct cc_recall_ds_data {
- int core_id;
- char ignore;
- char nested;
- struct cc_monitor_tree *interface_tree;
- };
- static void *cc_recall_ds_duplicate(void *data)
- {
- struct cc_recall_ds_data *old_data = data;
- struct cc_recall_ds_data *new_data = ast_calloc(1, sizeof(*new_data));
- if (!new_data) {
- return NULL;
- }
- new_data->interface_tree = cc_ref(old_data->interface_tree, "Bump refcount of monitor tree for recall datastore duplicate");
- new_data->core_id = old_data->core_id;
- new_data->nested = 1;
- return new_data;
- }
- static void cc_recall_ds_destroy(void *data)
- {
- struct cc_recall_ds_data *recall_data = data;
- recall_data->interface_tree = cc_unref(recall_data->interface_tree, "Unref recall monitor tree");
- ast_free(recall_data);
- }
- static const struct ast_datastore_info recall_ds_info = {
- .type = "cc_recall",
- .duplicate = cc_recall_ds_duplicate,
- .destroy = cc_recall_ds_destroy,
- };
- int ast_setup_cc_recall_datastore(struct ast_channel *chan, const int core_id)
- {
- struct ast_datastore *recall_datastore = ast_datastore_alloc(&recall_ds_info, NULL);
- struct cc_recall_ds_data *recall_data;
- struct cc_core_instance *core_instance;
- if (!recall_datastore) {
- return -1;
- }
- if (!(recall_data = ast_calloc(1, sizeof(*recall_data)))) {
- ast_datastore_free(recall_datastore);
- return -1;
- }
- if (!(core_instance = find_cc_core_instance(core_id))) {
- ast_free(recall_data);
- ast_datastore_free(recall_datastore);
- return -1;
- }
- recall_data->interface_tree = cc_ref(core_instance->monitors,
- "Bump refcount for monitor tree for recall datastore");
- recall_data->core_id = core_id;
- recall_datastore->data = recall_data;
- recall_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
- ast_channel_lock(chan);
- ast_channel_datastore_add(chan, recall_datastore);
- ast_channel_unlock(chan);
- cc_unref(core_instance, "Recall datastore set up. No need for core_instance ref");
- return 0;
- }
- int ast_cc_is_recall(struct ast_channel *chan, int *core_id, const char * const monitor_type)
- {
- struct ast_datastore *recall_datastore;
- struct cc_recall_ds_data *recall_data;
- struct cc_monitor_tree *interface_tree;
- char device_name[AST_CHANNEL_NAME];
- struct ast_cc_monitor *device_monitor;
- int core_id_candidate;
- ast_assert(core_id != NULL);
- *core_id = -1;
- ast_channel_lock(chan);
- if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
-
- ast_channel_unlock(chan);
- return 0;
- }
- recall_data = recall_datastore->data;
- if (recall_data->ignore) {
-
- ast_channel_unlock(chan);
- return 0;
- }
- if (!recall_data->nested) {
-
- *core_id = recall_data->core_id;
- ast_channel_unlock(chan);
- return 1;
- }
- if (ast_strlen_zero(monitor_type)) {
-
- ast_channel_unlock(chan);
- return 0;
- }
- interface_tree = recall_data->interface_tree;
- ast_channel_get_device_name(chan, device_name, sizeof(device_name));
-
- core_id_candidate = recall_data->core_id;
- ast_channel_unlock(chan);
-
- AST_LIST_LOCK(interface_tree);
- AST_LIST_TRAVERSE(interface_tree, device_monitor, next) {
- if (!strcmp(device_monitor->interface->device_name, device_name) &&
- !strcmp(device_monitor->interface->monitor_type, monitor_type)) {
-
- *core_id = core_id_candidate;
- AST_LIST_UNLOCK(interface_tree);
- return 1;
- }
- }
- AST_LIST_UNLOCK(interface_tree);
- return 0;
- }
- struct ast_cc_monitor *ast_cc_get_monitor_by_recall_core_id(const int core_id, const char * const device_name)
- {
- struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
- struct ast_cc_monitor *monitor_iter;
- if (!core_instance) {
- return NULL;
- }
- AST_LIST_LOCK(core_instance->monitors);
- AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
- if (!strcmp(monitor_iter->interface->device_name, device_name)) {
-
- cc_ref(monitor_iter, "Hand the requester of the monitor a reference");
- break;
- }
- }
- AST_LIST_UNLOCK(core_instance->monitors);
- cc_unref(core_instance, "Done with core instance ref in ast_cc_get_monitor_by_recall_core_id");
- return monitor_iter;
- }
- static void cc_unique_append(struct ast_str **str, const char *dialstring)
- {
- char dialstring_search[AST_CHANNEL_NAME];
- if (ast_strlen_zero(dialstring)) {
-
- return;
- }
- snprintf(dialstring_search, sizeof(dialstring_search), "%s%c", dialstring, '&');
- if (strstr(ast_str_buffer(*str), dialstring_search)) {
- return;
- }
- ast_str_append(str, 0, "%s", dialstring_search);
- }
- static void build_cc_interfaces_chanvar(struct ast_cc_monitor *starting_point, struct ast_str **str)
- {
- struct extension_monitor_pvt *extension_pvt;
- struct extension_child_dialstring *child_dialstring;
- struct ast_cc_monitor *monitor_iter = starting_point;
- int top_level_id = starting_point->id;
- size_t length;
-
- ast_str_truncate(*str, 0);
-
- extension_pvt = starting_point->private_data;
- AST_LIST_TRAVERSE(&extension_pvt->child_dialstrings, child_dialstring, next) {
- if (child_dialstring->is_valid) {
- cc_unique_append(str, child_dialstring->original_dialstring);
- }
- }
-
- while ((monitor_iter = AST_LIST_NEXT(monitor_iter, next))) {
- if (monitor_iter->parent_id == top_level_id) {
- cc_unique_append(str, monitor_iter->dialstring);
- }
- }
-
- length = ast_str_strlen(*str);
- if (length) {
- ast_str_truncate(*str, length - 1);
- }
- if (length <= 1) {
-
- ast_log(LOG_ERROR, "CC_INTERFACES is empty. starting device_name:'%s'\n",
- starting_point->interface->device_name);
- }
- }
- int ast_cc_agent_set_interfaces_chanvar(struct ast_channel *chan)
- {
- struct ast_datastore *recall_datastore;
- struct cc_monitor_tree *interface_tree;
- struct ast_cc_monitor *monitor;
- struct cc_recall_ds_data *recall_data;
- struct ast_str *str = ast_str_create(64);
- int core_id;
- if (!str) {
- return -1;
- }
- ast_channel_lock(chan);
- if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
- ast_channel_unlock(chan);
- ast_free(str);
- return -1;
- }
- recall_data = recall_datastore->data;
- interface_tree = recall_data->interface_tree;
- core_id = recall_data->core_id;
- ast_channel_unlock(chan);
- AST_LIST_LOCK(interface_tree);
- monitor = AST_LIST_FIRST(interface_tree);
- build_cc_interfaces_chanvar(monitor, &str);
- AST_LIST_UNLOCK(interface_tree);
- pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str));
- ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n",
- core_id, ast_str_buffer(str));
- ast_free(str);
- return 0;
- }
- int ast_set_cc_interfaces_chanvar(struct ast_channel *chan, const char * const extension)
- {
- struct ast_datastore *recall_datastore;
- struct cc_monitor_tree *interface_tree;
- struct ast_cc_monitor *monitor_iter;
- struct cc_recall_ds_data *recall_data;
- struct ast_str *str = ast_str_create(64);
- int core_id;
- if (!str) {
- return -1;
- }
- ast_channel_lock(chan);
- if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
- ast_channel_unlock(chan);
- ast_free(str);
- return -1;
- }
- recall_data = recall_datastore->data;
- interface_tree = recall_data->interface_tree;
- core_id = recall_data->core_id;
- ast_channel_unlock(chan);
- AST_LIST_LOCK(interface_tree);
- AST_LIST_TRAVERSE(interface_tree, monitor_iter, next) {
- if (!strcmp(monitor_iter->interface->device_name, extension)) {
- break;
- }
- }
- if (!monitor_iter) {
-
- AST_LIST_UNLOCK(interface_tree);
- ast_free(str);
- return -1;
- }
- build_cc_interfaces_chanvar(monitor_iter, &str);
- AST_LIST_UNLOCK(interface_tree);
- pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str));
- ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n",
- core_id, ast_str_buffer(str));
- ast_free(str);
- return 0;
- }
- void ast_ignore_cc(struct ast_channel *chan)
- {
- struct ast_datastore *cc_datastore;
- struct ast_datastore *cc_recall_datastore;
- struct dialed_cc_interfaces *cc_interfaces;
- struct cc_recall_ds_data *recall_cc_data;
- ast_channel_lock(chan);
- if ((cc_datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) {
- cc_interfaces = cc_datastore->data;
- cc_interfaces->ignore = 1;
- }
- if ((cc_recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
- recall_cc_data = cc_recall_datastore->data;
- recall_cc_data->ignore = 1;
- }
- ast_channel_unlock(chan);
- }
- static __attribute__((format(printf, 2, 3))) int cc_offer(const int core_id, const char * const debug, ...)
- {
- va_list ap;
- int res;
- va_start(ap, debug);
- res = cc_request_state_change(CC_CALLER_OFFERED, core_id, debug, ap);
- va_end(ap);
- return res;
- }
- int ast_cc_offer(struct ast_channel *caller_chan)
- {
- int core_id;
- int res = -1;
- struct ast_datastore *datastore;
- struct dialed_cc_interfaces *cc_interfaces;
- char cc_is_offerable;
- ast_channel_lock(caller_chan);
- if (!(datastore = ast_channel_datastore_find(caller_chan, &dialed_cc_interfaces_info, NULL))) {
- ast_channel_unlock(caller_chan);
- return res;
- }
- cc_interfaces = datastore->data;
- cc_is_offerable = cc_interfaces->is_original_caller;
- core_id = cc_interfaces->core_id;
- ast_channel_unlock(caller_chan);
- if (cc_is_offerable) {
- res = cc_offer(core_id, "CC offered to caller %s", ast_channel_name(caller_chan));
- }
- return res;
- }
- int ast_cc_agent_accept_request(int core_id, const char * const debug, ...)
- {
- va_list ap;
- int res;
- va_start(ap, debug);
- res = cc_request_state_change(CC_CALLER_REQUESTED, core_id, debug, ap);
- va_end(ap);
- return res;
- }
- int ast_cc_monitor_request_acked(int core_id, const char * const debug, ...)
- {
- va_list ap;
- int res;
- va_start(ap, debug);
- res = cc_request_state_change(CC_ACTIVE, core_id, debug, ap);
- va_end(ap);
- return res;
- }
- int ast_cc_monitor_callee_available(const int core_id, const char * const debug, ...)
- {
- va_list ap;
- int res;
- va_start(ap, debug);
- res = cc_request_state_change(CC_CALLEE_READY, core_id, debug, ap);
- va_end(ap);
- return res;
- }
- int ast_cc_agent_caller_busy(int core_id, const char * debug, ...)
- {
- va_list ap;
- int res;
- va_start(ap, debug);
- res = cc_request_state_change(CC_CALLER_BUSY, core_id, debug, ap);
- va_end(ap);
- return res;
- }
- int ast_cc_agent_caller_available(int core_id, const char * const debug, ...)
- {
- va_list ap;
- int res;
- va_start(ap, debug);
- res = cc_request_state_change(CC_ACTIVE, core_id, debug, ap);
- va_end(ap);
- return res;
- }
- int ast_cc_agent_recalling(int core_id, const char * const debug, ...)
- {
- va_list ap;
- int res;
- va_start(ap, debug);
- res = cc_request_state_change(CC_RECALLING, core_id, debug, ap);
- va_end(ap);
- return res;
- }
- int ast_cc_completed(struct ast_channel *chan, const char * const debug, ...)
- {
- struct ast_datastore *recall_datastore;
- struct cc_recall_ds_data *recall_data;
- int core_id;
- va_list ap;
- int res;
- ast_channel_lock(chan);
- if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) {
-
- ast_channel_unlock(chan);
- return -1;
- }
- recall_data = recall_datastore->data;
- if (recall_data->nested || recall_data->ignore) {
-
- ast_channel_unlock(chan);
- return -1;
- }
- core_id = recall_data->core_id;
- ast_channel_unlock(chan);
- va_start(ap, debug);
- res = cc_request_state_change(CC_COMPLETE, core_id, debug, ap);
- va_end(ap);
- return res;
- }
- int ast_cc_failed(int core_id, const char * const debug, ...)
- {
- va_list ap;
- int res;
- va_start(ap, debug);
- res = cc_request_state_change(CC_FAILED, core_id, debug, ap);
- va_end(ap);
- return res;
- }
- struct ast_cc_monitor_failure_data {
- const char *device_name;
- char *debug;
- int core_id;
- };
- static int cc_monitor_failed(void *data)
- {
- struct ast_cc_monitor_failure_data *failure_data = data;
- struct cc_core_instance *core_instance;
- struct ast_cc_monitor *monitor_iter;
- core_instance = find_cc_core_instance(failure_data->core_id);
- if (!core_instance) {
-
- ast_log_dynamic_level(cc_logger_level,
- "Core %d: Could not find core instance for device %s '%s'\n",
- failure_data->core_id, failure_data->device_name, failure_data->debug);
- ast_free((char *) failure_data->device_name);
- ast_free((char *) failure_data->debug);
- ast_free(failure_data);
- return -1;
- }
- AST_LIST_LOCK(core_instance->monitors);
- AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) {
- if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
- if (!strcmp(monitor_iter->interface->device_name, failure_data->device_name)) {
- AST_LIST_REMOVE_CURRENT(next);
- cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id,
- monitor_iter->interface->device_name, 1);
- monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id);
- manager_event(EVENT_FLAG_CC, "CCMonitorFailed",
- "CoreID: %d\r\n"
- "Callee: %s\r\n",
- monitor_iter->core_id, monitor_iter->interface->device_name);
- cc_unref(monitor_iter, "Monitor reported failure. Unref list's reference.");
- }
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
- if (!has_device_monitors(core_instance)) {
- ast_cc_failed(core_instance->core_id, "All monitors have failed\n");
- }
- AST_LIST_UNLOCK(core_instance->monitors);
- cc_unref(core_instance, "Finished with core_instance in cc_monitor_failed\n");
- ast_free((char *) failure_data->device_name);
- ast_free((char *) failure_data->debug);
- ast_free(failure_data);
- return 0;
- }
- int ast_cc_monitor_failed(int core_id, const char *const monitor_name, const char * const debug, ...)
- {
- struct ast_cc_monitor_failure_data *failure_data;
- int res;
- va_list ap;
- if (!(failure_data = ast_calloc(1, sizeof(*failure_data)))) {
- return -1;
- }
- if (!(failure_data->device_name = ast_strdup(monitor_name))) {
- ast_free(failure_data);
- return -1;
- }
- va_start(ap, debug);
- if (ast_vasprintf(&failure_data->debug, debug, ap) == -1) {
- va_end(ap);
- ast_free((char *)failure_data->device_name);
- ast_free(failure_data);
- return -1;
- }
- va_end(ap);
- failure_data->core_id = core_id;
- res = ast_taskprocessor_push(cc_core_taskprocessor, cc_monitor_failed, failure_data);
- if (res) {
- ast_free((char *)failure_data->device_name);
- ast_free((char *)failure_data->debug);
- ast_free(failure_data);
- }
- return res;
- }
- static int cc_status_request(void *data)
- {
- struct cc_core_instance *core_instance= data;
- int res;
- res = core_instance->agent->callbacks->status_request(core_instance->agent);
- cc_unref(core_instance, "Status request finished. Unref core instance");
- return res;
- }
- int ast_cc_monitor_status_request(int core_id)
- {
- int res;
- struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
- if (!core_instance) {
- return -1;
- }
- res = ast_taskprocessor_push(cc_core_taskprocessor, cc_status_request, core_instance);
- if (res) {
- cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
- }
- return res;
- }
- static int cc_stop_ringing(void *data)
- {
- struct cc_core_instance *core_instance = data;
- int res = 0;
- if (core_instance->agent->callbacks->stop_ringing) {
- res = core_instance->agent->callbacks->stop_ringing(core_instance->agent);
- }
-
- ast_cc_monitor_request_acked(core_instance->core_id, "Agent %s asked to stop ringing. Be prepared to be recalled again.",
- core_instance->agent->device_name);
- cc_unref(core_instance, "Stop ringing finished. Unref core_instance");
- return res;
- }
- int ast_cc_monitor_stop_ringing(int core_id)
- {
- int res;
- struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
- if (!core_instance) {
- return -1;
- }
- res = ast_taskprocessor_push(cc_core_taskprocessor, cc_stop_ringing, core_instance);
- if (res) {
- cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
- }
- return res;
- }
- static int cc_party_b_free(void *data)
- {
- struct cc_core_instance *core_instance = data;
- int res = 0;
- if (core_instance->agent->callbacks->party_b_free) {
- res = core_instance->agent->callbacks->party_b_free(core_instance->agent);
- }
- cc_unref(core_instance, "Party B free finished. Unref core_instance");
- return res;
- }
- int ast_cc_monitor_party_b_free(int core_id)
- {
- int res;
- struct cc_core_instance *core_instance = find_cc_core_instance(core_id);
- if (!core_instance) {
- return -1;
- }
- res = ast_taskprocessor_push(cc_core_taskprocessor, cc_party_b_free, core_instance);
- if (res) {
- cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
- }
- return res;
- }
- struct cc_status_response_args {
- struct cc_core_instance *core_instance;
- enum ast_device_state devstate;
- };
- static int cc_status_response(void *data)
- {
- struct cc_status_response_args *args = data;
- struct cc_core_instance *core_instance = args->core_instance;
- struct ast_cc_monitor *monitor_iter;
- enum ast_device_state devstate = args->devstate;
- ast_free(args);
- AST_LIST_LOCK(core_instance->monitors);
- AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
- if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR &&
- monitor_iter->callbacks->status_response) {
- monitor_iter->callbacks->status_response(monitor_iter, devstate);
- }
- }
- AST_LIST_UNLOCK(core_instance->monitors);
- cc_unref(core_instance, "Status response finished. Unref core instance");
- return 0;
- }
- int ast_cc_agent_status_response(int core_id, enum ast_device_state devstate)
- {
- struct cc_status_response_args *args;
- struct cc_core_instance *core_instance;
- int res;
- args = ast_calloc(1, sizeof(*args));
- if (!args) {
- return -1;
- }
- core_instance = find_cc_core_instance(core_id);
- if (!core_instance) {
- ast_free(args);
- return -1;
- }
- args->core_instance = core_instance;
- args->devstate = devstate;
- res = ast_taskprocessor_push(cc_core_taskprocessor, cc_status_response, args);
- if (res) {
- cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed");
- ast_free(args);
- }
- return res;
- }
- static int cc_build_payload(struct ast_channel *chan, struct ast_cc_config_params *cc_params,
- const char *monitor_type, const char * const device_name, const char * dialstring,
- enum ast_cc_service_type service, void *private_data, struct cc_control_payload *payload)
- {
- struct ast_datastore *datastore;
- struct dialed_cc_interfaces *cc_interfaces;
- int dial_parent_id;
- ast_channel_lock(chan);
- datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL);
- if (!datastore) {
- ast_channel_unlock(chan);
- return -1;
- }
- cc_interfaces = datastore->data;
- dial_parent_id = cc_interfaces->dial_parent_id;
- ast_channel_unlock(chan);
- payload->monitor_type = monitor_type;
- payload->private_data = private_data;
- payload->service = service;
- ast_cc_copy_config_params(&payload->config_params, cc_params);
- payload->parent_interface_id = dial_parent_id;
- ast_copy_string(payload->device_name, device_name, sizeof(payload->device_name));
- ast_copy_string(payload->dialstring, dialstring, sizeof(payload->dialstring));
- return 0;
- }
- int ast_queue_cc_frame(struct ast_channel *chan, const char *monitor_type,
- const char * const dialstring, enum ast_cc_service_type service, void *private_data)
- {
- struct ast_frame frame = {0,};
- char device_name[AST_CHANNEL_NAME];
- int retval;
- struct ast_cc_config_params *cc_params;
- cc_params = ast_channel_get_cc_config_params(chan);
- if (!cc_params) {
- return -1;
- }
- ast_channel_get_device_name(chan, device_name, sizeof(device_name));
- if (ast_cc_monitor_count(device_name, monitor_type) >= ast_get_cc_max_monitors(cc_params)) {
- ast_log(LOG_NOTICE, "Not queuing a CC frame for device %s since it already has its maximum monitors allocated\n", device_name);
- return -1;
- }
- if (ast_cc_build_frame(chan, cc_params, monitor_type, device_name, dialstring, service, private_data, &frame)) {
-
- return -1;
- }
- retval = ast_queue_frame(chan, &frame);
- ast_frfree(&frame);
- return retval;
- }
- int ast_cc_build_frame(struct ast_channel *chan, struct ast_cc_config_params *cc_params,
- const char *monitor_type, const char * const device_name,
- const char * const dialstring, enum ast_cc_service_type service, void *private_data,
- struct ast_frame *frame)
- {
- struct cc_control_payload *payload = ast_calloc(1, sizeof(*payload));
- if (!payload) {
- return -1;
- }
- if (cc_build_payload(chan, cc_params, monitor_type, device_name, dialstring, service, private_data, payload)) {
-
- ast_free(payload);
- return -1;
- }
- frame->frametype = AST_FRAME_CONTROL;
- frame->subclass.integer = AST_CONTROL_CC;
- frame->data.ptr = payload;
- frame->datalen = sizeof(*payload);
- frame->mallocd = AST_MALLOCD_DATA;
- return 0;
- }
- void ast_cc_call_failed(struct ast_channel *incoming, struct ast_channel *outgoing, const char * const dialstring)
- {
- char device_name[AST_CHANNEL_NAME];
- struct cc_control_payload payload;
- struct ast_cc_config_params *cc_params;
- if (ast_channel_hangupcause(outgoing) != AST_CAUSE_BUSY && ast_channel_hangupcause(outgoing) != AST_CAUSE_CONGESTION) {
-
- return;
- }
- cc_params = ast_channel_get_cc_config_params(outgoing);
- if (!cc_params) {
- return;
- }
- if (ast_get_cc_monitor_policy(cc_params) != AST_CC_MONITOR_GENERIC) {
-
- return;
- }
- ast_channel_get_device_name(outgoing, device_name, sizeof(device_name));
- if (cc_build_payload(outgoing, cc_params, AST_CC_GENERIC_MONITOR_TYPE, device_name,
- dialstring, AST_CC_CCBS, NULL, &payload)) {
-
- return;
- }
- ast_handle_cc_control_frame(incoming, outgoing, &payload);
- }
- void ast_cc_busy_interface(struct ast_channel *inbound, struct ast_cc_config_params *cc_params,
- const char *monitor_type, const char * const device_name, const char * const dialstring, void *private_data)
- {
- struct cc_control_payload payload;
- if (cc_build_payload(inbound, cc_params, monitor_type, device_name, dialstring, AST_CC_CCBS, private_data, &payload)) {
-
- call_destructor_with_no_monitor(monitor_type, private_data);
- return;
- }
- ast_handle_cc_control_frame(inbound, NULL, &payload);
- }
- int ast_cc_callback(struct ast_channel *inbound, const char * const tech, const char * const dest, ast_cc_callback_fn callback)
- {
- const struct ast_channel_tech *chantech = ast_get_channel_tech(tech);
- if (chantech && chantech->cc_callback) {
- chantech->cc_callback(inbound, dest, callback);
- }
- return 0;
- }
- static const char *ccreq_app = "CallCompletionRequest";
- static int ccreq_exec(struct ast_channel *chan, const char *data)
- {
- struct cc_core_instance *core_instance;
- char device_name[AST_CHANNEL_NAME];
- unsigned long match_flags;
- int res;
- ast_channel_get_device_name(chan, device_name, sizeof(device_name));
- match_flags = MATCH_NO_REQUEST;
- if (!(core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent, device_name, &match_flags, "Find core instance for CallCompletionRequest"))) {
- ast_log_dynamic_level(cc_logger_level, "Couldn't find a core instance for caller %s\n", device_name);
- pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL");
- pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "NO_CORE_INSTANCE");
- return 0;
- }
- ast_log_dynamic_level(cc_logger_level, "Core %d: Found core_instance for caller %s\n",
- core_instance->core_id, device_name);
- if (strcmp(core_instance->agent->callbacks->type, "generic")) {
- ast_log_dynamic_level(cc_logger_level, "Core %d: CallCompletionRequest is only for generic agent types.\n",
- core_instance->core_id);
- pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL");
- pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "NOT_GENERIC");
- cc_unref(core_instance, "Unref core_instance since CallCompletionRequest was called with native agent");
- return 0;
- }
- if (!ast_cc_request_is_within_limits()) {
- ast_log_dynamic_level(cc_logger_level, "Core %d: CallCompletionRequest failed. Too many requests in the system\n",
- core_instance->core_id);
- ast_cc_failed(core_instance->core_id, "Too many CC requests\n");
- pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL");
- pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "TOO_MANY_REQUESTS");
- cc_unref(core_instance, "Unref core_instance since too many CC requests");
- return 0;
- }
- res = ast_cc_agent_accept_request(core_instance->core_id, "CallCompletionRequest called by caller %s for core_id %d", device_name, core_instance->core_id);
- pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", res ? "FAIL" : "SUCCESS");
- if (res) {
- pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "UNSPECIFIED");
- }
- cc_unref(core_instance, "Done with CallCompletionRequest");
- return 0;
- }
- static const char *cccancel_app = "CallCompletionCancel";
- static int cccancel_exec(struct ast_channel *chan, const char *data)
- {
- struct cc_core_instance *core_instance;
- char device_name[AST_CHANNEL_NAME];
- unsigned long match_flags;
- int res;
- ast_channel_get_device_name(chan, device_name, sizeof(device_name));
- match_flags = MATCH_REQUEST;
- if (!(core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent, device_name, &match_flags, "Find core instance for CallCompletionCancel"))) {
- ast_log_dynamic_level(cc_logger_level, "Cannot find CC transaction to cancel for caller %s\n", device_name);
- pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", "FAIL");
- pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "NO_CORE_INSTANCE");
- return 0;
- }
- if (strcmp(core_instance->agent->callbacks->type, "generic")) {
- ast_log(LOG_WARNING, "CallCompletionCancel may only be used for calles with a generic agent\n");
- cc_unref(core_instance, "Unref core instance found during CallCompletionCancel");
- pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", "FAIL");
- pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "NOT_GENERIC");
- return 0;
- }
- res = ast_cc_failed(core_instance->core_id, "Call completion request Cancelled for core ID %d by caller %s",
- core_instance->core_id, device_name);
- cc_unref(core_instance, "Unref core instance found during CallCompletionCancel");
- pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", res ? "FAIL" : "SUCCESS");
- if (res) {
- pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "UNSPECIFIED");
- }
- return 0;
- }
- struct count_monitors_cb_data {
- const char *device_name;
- const char *monitor_type;
- int count;
- };
- static int count_monitors_cb(void *obj, void *arg, int flags)
- {
- struct cc_core_instance *core_instance = obj;
- struct count_monitors_cb_data *cb_data = arg;
- const char *device_name = cb_data->device_name;
- const char *monitor_type = cb_data->monitor_type;
- struct ast_cc_monitor *monitor_iter;
- AST_LIST_LOCK(core_instance->monitors);
- AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) {
- if (!strcmp(monitor_iter->interface->device_name, device_name) &&
- !strcmp(monitor_iter->interface->monitor_type, monitor_type)) {
- cb_data->count++;
- break;
- }
- }
- AST_LIST_UNLOCK(core_instance->monitors);
- return 0;
- }
- int ast_cc_monitor_count(const char * const name, const char * const type)
- {
- struct count_monitors_cb_data data = {.device_name = name, .monitor_type = type,};
- ao2_t_callback(cc_core_instances, OBJ_NODATA, count_monitors_cb, &data, "Counting agents");
- ast_log_dynamic_level(cc_logger_level, "Counted %d monitors\n", data.count);
- return data.count;
- }
- static void initialize_cc_max_requests(void)
- {
- struct ast_config *cc_config;
- const char *cc_max_requests_str;
- struct ast_flags config_flags = {0,};
- char *endptr;
- cc_config = ast_config_load2("ccss.conf", "ccss", config_flags);
- if (!cc_config || cc_config == CONFIG_STATUS_FILEINVALID) {
- ast_log(LOG_WARNING, "Could not find valid ccss.conf file. Using cc_max_requests default\n");
- global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT;
- return;
- }
- if (!(cc_max_requests_str = ast_variable_retrieve(cc_config, "general", "cc_max_requests"))) {
- ast_config_destroy(cc_config);
- global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT;
- return;
- }
- global_cc_max_requests = strtol(cc_max_requests_str, &endptr, 10);
- if (!ast_strlen_zero(endptr)) {
- ast_log(LOG_WARNING, "Invalid input given for cc_max_requests. Using default\n");
- global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT;
- }
- ast_config_destroy(cc_config);
- return;
- }
- static void initialize_cc_devstate_map_helper(struct ast_config *cc_config, enum cc_state state, const char *cc_setting)
- {
- const char *cc_devstate_str;
- enum ast_device_state this_devstate;
- if ((cc_devstate_str = ast_variable_retrieve(cc_config, "general", cc_setting))) {
- this_devstate = ast_devstate_val(cc_devstate_str);
- if (this_devstate != AST_DEVICE_UNKNOWN) {
- cc_state_to_devstate_map[state] = this_devstate;
- }
- }
- }
- static void initialize_cc_devstate_map(void)
- {
- struct ast_config *cc_config;
- struct ast_flags config_flags = { 0, };
- cc_config = ast_config_load2("ccss.conf", "ccss", config_flags);
- if (!cc_config || cc_config == CONFIG_STATUS_FILEINVALID) {
- ast_log(LOG_WARNING,
- "Could not find valid ccss.conf file. Using cc_[state]_devstate defaults\n");
- return;
- }
- initialize_cc_devstate_map_helper(cc_config, CC_AVAILABLE, "cc_available_devstate");
- initialize_cc_devstate_map_helper(cc_config, CC_CALLER_OFFERED, "cc_caller_offered_devstate");
- initialize_cc_devstate_map_helper(cc_config, CC_CALLER_REQUESTED, "cc_caller_requested_devstate");
- initialize_cc_devstate_map_helper(cc_config, CC_ACTIVE, "cc_active_devstate");
- initialize_cc_devstate_map_helper(cc_config, CC_CALLEE_READY, "cc_callee_ready_devstate");
- initialize_cc_devstate_map_helper(cc_config, CC_CALLER_BUSY, "cc_caller_busy_devstate");
- initialize_cc_devstate_map_helper(cc_config, CC_RECALLING, "cc_recalling_devstate");
- initialize_cc_devstate_map_helper(cc_config, CC_COMPLETE, "cc_complete_devstate");
- initialize_cc_devstate_map_helper(cc_config, CC_FAILED, "cc_failed_devstate");
- ast_config_destroy(cc_config);
- }
- static void cc_cli_print_monitor_stats(struct ast_cc_monitor *monitor, int fd, int parent_id)
- {
- struct ast_cc_monitor *child_monitor_iter = monitor;
- if (!monitor) {
- return;
- }
- ast_cli(fd, "\t\t|-->%s", monitor->interface->device_name);
- if (monitor->interface->monitor_class == AST_CC_DEVICE_MONITOR) {
- ast_cli(fd, "(%s)", cc_service_to_string(monitor->service_offered));
- }
- ast_cli(fd, "\n");
- while ((child_monitor_iter = AST_LIST_NEXT(child_monitor_iter, next))) {
- if (child_monitor_iter->parent_id == monitor->id) {
- cc_cli_print_monitor_stats(child_monitor_iter, fd, child_monitor_iter->id);
- }
- }
- }
- static int print_stats_cb(void *obj, void *arg, int flags)
- {
- int *cli_fd = arg;
- struct cc_core_instance *core_instance = obj;
- ast_cli(*cli_fd, "%d\t\t%s\t\t%s\n", core_instance->core_id, core_instance->agent->device_name,
- cc_state_to_string(core_instance->current_state));
- AST_LIST_LOCK(core_instance->monitors);
- cc_cli_print_monitor_stats(AST_LIST_FIRST(core_instance->monitors), *cli_fd, 0);
- AST_LIST_UNLOCK(core_instance->monitors);
- return 0;
- }
- static int cc_cli_output_status(void *data)
- {
- int *cli_fd = data;
- int count = ao2_container_count(cc_core_instances);
- if (!count) {
- ast_cli(*cli_fd, "There are currently no active call completion transactions\n");
- } else {
- ast_cli(*cli_fd, "%d Call completion transactions\n", count);
- ast_cli(*cli_fd, "Core ID\t\tCaller\t\t\t\tStatus\n");
- ast_cli(*cli_fd, "----------------------------------------------------------------------------\n");
- ao2_t_callback(cc_core_instances, OBJ_NODATA, print_stats_cb, cli_fd, "Printing stats to CLI");
- }
- ast_free(cli_fd);
- return 0;
- }
- static char *handle_cc_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
- {
- int *cli_fd;
- switch (cmd) {
- case CLI_INIT:
- e->command = "cc report status";
- e->usage =
- "Usage: cc report status\n"
- " Report the current status of any ongoing CC transactions\n";
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- if (a->argc != 3) {
- return CLI_SHOWUSAGE;
- }
- cli_fd = ast_malloc(sizeof(*cli_fd));
- if (!cli_fd) {
- return CLI_FAILURE;
- }
- *cli_fd = a->fd;
- if (ast_taskprocessor_push(cc_core_taskprocessor, cc_cli_output_status, cli_fd)) {
- ast_free(cli_fd);
- return CLI_FAILURE;
- }
- return CLI_SUCCESS;
- }
- static int kill_cores(void *obj, void *arg, int flags)
- {
- int *core_id = arg;
- struct cc_core_instance *core_instance = obj;
- if (!core_id || (core_instance->core_id == *core_id)) {
- ast_cc_failed(core_instance->core_id, "CC transaction canceled administratively\n");
- }
- return 0;
- }
- static char *complete_core_id(const char *line, const char *word, int pos, int state)
- {
- int which = 0;
- int wordlen = strlen(word);
- char *ret = NULL;
- struct ao2_iterator core_iter = ao2_iterator_init(cc_core_instances, 0);
- struct cc_core_instance *core_instance;
- for (; (core_instance = ao2_t_iterator_next(&core_iter, "Next core instance"));
- cc_unref(core_instance, "CLI tab completion iteration")) {
- char core_id_str[20];
- snprintf(core_id_str, sizeof(core_id_str), "%d", core_instance->core_id);
- if (!strncmp(word, core_id_str, wordlen) && ++which > state) {
- ret = ast_strdup(core_id_str);
- cc_unref(core_instance, "Found a matching core ID for CLI tab-completion");
- break;
- }
- }
- ao2_iterator_destroy(&core_iter);
- return ret;
- }
- static char *handle_cc_kill(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
- {
- static const char * const option[] = { "core", "all", NULL };
- switch (cmd) {
- case CLI_INIT:
- e->command = "cc cancel";
- e->usage =
- "Usage: cc cancel can be used in two ways.\n"
- " 1. 'cc cancel core [core ID]' will cancel the CC transaction with\n"
- " core ID equal to the specified core ID.\n"
- " 2. 'cc cancel all' will cancel all active CC transactions.\n";
- return NULL;
- case CLI_GENERATE:
- if (a->pos == 2) {
- return ast_cli_complete(a->word, option, a->n);
- }
- if (a->pos == 3) {
- return complete_core_id(a->line, a->word, a->pos, a->n);
- }
- return NULL;
- }
- if (a->argc == 4) {
- int core_id;
- char *endptr;
- if (strcasecmp(a->argv[2], "core")) {
- return CLI_SHOWUSAGE;
- }
- core_id = strtol(a->argv[3], &endptr, 10);
- if ((errno != 0 && core_id == 0) || (endptr == a->argv[3])) {
- return CLI_SHOWUSAGE;
- }
- ao2_t_callback(cc_core_instances, OBJ_NODATA, kill_cores, &core_id, "CLI Killing Core Id");
- } else if (a->argc == 3) {
- if (strcasecmp(a->argv[2], "all")) {
- return CLI_SHOWUSAGE;
- }
- ao2_t_callback(cc_core_instances, OBJ_NODATA, kill_cores, NULL, "CLI Killing all CC cores");
- } else {
- return CLI_SHOWUSAGE;
- }
- return CLI_SUCCESS;
- }
- static struct ast_cli_entry cc_cli[] = {
- AST_CLI_DEFINE(handle_cc_status, "Reports CC stats"),
- AST_CLI_DEFINE(handle_cc_kill, "Kill a CC transaction"),
- };
- static void cc_shutdown(void)
- {
- ast_devstate_prov_del("ccss");
- ast_cc_agent_unregister(&generic_agent_callbacks);
- ast_cc_monitor_unregister(&generic_monitor_cbs);
- ast_unregister_application(cccancel_app);
- ast_unregister_application(ccreq_app);
- ast_logger_unregister_level(CC_LOGGER_LEVEL_NAME);
- ast_cli_unregister_multiple(cc_cli, ARRAY_LEN(cc_cli));
- if (cc_sched_context) {
- ast_sched_context_destroy(cc_sched_context);
- cc_sched_context = NULL;
- }
- if (cc_core_taskprocessor) {
- cc_core_taskprocessor = ast_taskprocessor_unreference(cc_core_taskprocessor);
- }
-
- if (cc_core_instances) {
- ao2_t_ref(cc_core_instances, -1, "Unref cc_core_instances container in cc_shutdown");
- cc_core_instances = NULL;
- }
- if (generic_monitors) {
- ao2_t_ref(generic_monitors, -1, "Unref generic_monitor container in cc_shutdown");
- generic_monitors = NULL;
- }
- }
- int ast_cc_init(void)
- {
- int res;
- if (!(cc_core_instances = ao2_t_container_alloc(CC_CORE_INSTANCES_BUCKETS,
- cc_core_instance_hash_fn, cc_core_instance_cmp_fn,
- "Create core instance container"))) {
- return -1;
- }
- if (!(generic_monitors = ao2_t_container_alloc(CC_CORE_INSTANCES_BUCKETS,
- generic_monitor_hash_fn, generic_monitor_cmp_fn,
- "Create generic monitor container"))) {
- return -1;
- }
- if (!(cc_core_taskprocessor = ast_taskprocessor_get("CCSS core", TPS_REF_DEFAULT))) {
- return -1;
- }
- if (!(cc_sched_context = ast_sched_context_create())) {
- return -1;
- }
- if (ast_sched_start_thread(cc_sched_context)) {
- return -1;
- }
- res = ast_register_application2(ccreq_app, ccreq_exec, NULL, NULL, NULL);
- res |= ast_register_application2(cccancel_app, cccancel_exec, NULL, NULL, NULL);
- res |= ast_cc_monitor_register(&generic_monitor_cbs);
- res |= ast_cc_agent_register(&generic_agent_callbacks);
- ast_cli_register_multiple(cc_cli, ARRAY_LEN(cc_cli));
- cc_logger_level = ast_logger_register_level(CC_LOGGER_LEVEL_NAME);
- dialed_cc_interface_counter = 1;
- initialize_cc_max_requests();
-
- initialize_cc_devstate_map();
- res |= ast_devstate_prov_add("ccss", ccss_device_state);
- ast_register_cleanup(cc_shutdown);
- return res;
- }
|