channel.c 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Channel Management
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/time.h>
  27. #include <signal.h>
  28. #include <errno.h>
  29. #include <unistd.h>
  30. #include <math.h> /* For PI */
  31. #ifdef ZAPTEL_OPTIMIZATIONS
  32. #include <sys/ioctl.h>
  33. #ifdef __linux__
  34. #include <linux/zaptel.h>
  35. #else
  36. #include <zaptel.h>
  37. #endif /* __linux__ */
  38. #ifndef ZT_TIMERPING
  39. #error "You need newer zaptel! Please cvs update zaptel"
  40. #endif
  41. #endif
  42. #include "asterisk.h"
  43. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  44. #include "asterisk/pbx.h"
  45. #include "asterisk/frame.h"
  46. #include "asterisk/sched.h"
  47. #include "asterisk/options.h"
  48. #include "asterisk/channel.h"
  49. #include "asterisk/chanspy.h"
  50. #include "asterisk/musiconhold.h"
  51. #include "asterisk/logger.h"
  52. #include "asterisk/say.h"
  53. #include "asterisk/file.h"
  54. #include "asterisk/cli.h"
  55. #include "asterisk/translate.h"
  56. #include "asterisk/manager.h"
  57. #include "asterisk/chanvars.h"
  58. #include "asterisk/linkedlists.h"
  59. #include "asterisk/indications.h"
  60. #include "asterisk/monitor.h"
  61. #include "asterisk/causes.h"
  62. #include "asterisk/callerid.h"
  63. #include "asterisk/utils.h"
  64. #include "asterisk/lock.h"
  65. #include "asterisk/app.h"
  66. #include "asterisk/transcap.h"
  67. #include "asterisk/devicestate.h"
  68. struct channel_spy_trans {
  69. int last_format;
  70. struct ast_trans_pvt *path;
  71. };
  72. struct ast_channel_spy_list {
  73. struct channel_spy_trans read_translator;
  74. struct channel_spy_trans write_translator;
  75. AST_LIST_HEAD_NOLOCK(, ast_channel_spy) list;
  76. };
  77. /* uncomment if you have problems with 'monitoring' synchronized files */
  78. #if 0
  79. #define MONITOR_CONSTANT_DELAY
  80. #define MONITOR_DELAY 150 * 8 /* 150 ms of MONITORING DELAY */
  81. #endif
  82. /*
  83. * Prevent new channel allocation if shutting down.
  84. */
  85. static int shutting_down = 0;
  86. static int uniqueint = 0;
  87. unsigned long global_fin = 0, global_fout = 0;
  88. /* XXX Lock appropriately in more functions XXX */
  89. struct chanlist {
  90. const struct ast_channel_tech *tech;
  91. struct chanlist *next;
  92. };
  93. static struct chanlist *backends = NULL;
  94. /*
  95. * the list of channels we have
  96. */
  97. static struct ast_channel *channels = NULL;
  98. /* Protect the channel list, both backends and channels.
  99. */
  100. AST_MUTEX_DEFINE_STATIC(chlock);
  101. const struct ast_cause {
  102. int cause;
  103. const char *desc;
  104. } causes[] = {
  105. { AST_CAUSE_UNALLOCATED, "Unallocated (unassigned) number" },
  106. { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "No route to specified transmit network" },
  107. { AST_CAUSE_NO_ROUTE_DESTINATION, "No route to destination" },
  108. { AST_CAUSE_CHANNEL_UNACCEPTABLE, "Channel unacceptable" },
  109. { AST_CAUSE_CALL_AWARDED_DELIVERED, "Call awarded and being delivered in an established channel" },
  110. { AST_CAUSE_NORMAL_CLEARING, "Normal Clearing" },
  111. { AST_CAUSE_USER_BUSY, "User busy" },
  112. { AST_CAUSE_NO_USER_RESPONSE, "No user responding" },
  113. { AST_CAUSE_NO_ANSWER, "User alerting, no answer" },
  114. { AST_CAUSE_CALL_REJECTED, "Call Rejected" },
  115. { AST_CAUSE_NUMBER_CHANGED, "Number changed" },
  116. { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "Destination out of order" },
  117. { AST_CAUSE_INVALID_NUMBER_FORMAT, "Invalid number format" },
  118. { AST_CAUSE_FACILITY_REJECTED, "Facility rejected" },
  119. { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "Response to STATus ENQuiry" },
  120. { AST_CAUSE_NORMAL_UNSPECIFIED, "Normal, unspecified" },
  121. { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "Circuit/channel congestion" },
  122. { AST_CAUSE_NETWORK_OUT_OF_ORDER, "Network out of order" },
  123. { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "Temporary failure" },
  124. { AST_CAUSE_SWITCH_CONGESTION, "Switching equipment congestion" },
  125. { AST_CAUSE_ACCESS_INFO_DISCARDED, "Access information discarded" },
  126. { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "Requested channel not available" },
  127. { AST_CAUSE_PRE_EMPTED, "Pre-empted" },
  128. { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "Facility not subscribed" },
  129. { AST_CAUSE_OUTGOING_CALL_BARRED, "Outgoing call barred" },
  130. { AST_CAUSE_INCOMING_CALL_BARRED, "Incoming call barred" },
  131. { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "Bearer capability not authorized" },
  132. { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "Bearer capability not available" },
  133. { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "Bearer capability not implemented" },
  134. { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "Channel not implemented" },
  135. { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "Facility not implemented" },
  136. { AST_CAUSE_INVALID_CALL_REFERENCE, "Invalid call reference value" },
  137. { AST_CAUSE_INCOMPATIBLE_DESTINATION, "Incompatible destination" },
  138. { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "Invalid message unspecified" },
  139. { AST_CAUSE_MANDATORY_IE_MISSING, "Mandatory information element is missing" },
  140. { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "Message type nonexist." },
  141. { AST_CAUSE_WRONG_MESSAGE, "Wrong message" },
  142. { AST_CAUSE_IE_NONEXIST, "Info. element nonexist or not implemented" },
  143. { AST_CAUSE_INVALID_IE_CONTENTS, "Invalid information element contents" },
  144. { AST_CAUSE_WRONG_CALL_STATE, "Message not compatible with call state" },
  145. { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "Recover on timer expiry" },
  146. { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "Mandatory IE length error" },
  147. { AST_CAUSE_PROTOCOL_ERROR, "Protocol error, unspecified" },
  148. { AST_CAUSE_INTERWORKING, "Interworking, unspecified" },
  149. };
  150. static int show_channeltypes(int fd, int argc, char *argv[])
  151. {
  152. #define FORMAT "%-10.10s %-30.30s %-12.12s %-12.12s %-12.12s\n"
  153. struct chanlist *cl = backends;
  154. ast_cli(fd, FORMAT, "Type", "Description", "Devicestate", "Indications", "Transfer");
  155. ast_cli(fd, FORMAT, "----------", "-----------", "-----------", "-----------", "--------");
  156. if (ast_mutex_lock(&chlock)) {
  157. ast_log(LOG_WARNING, "Unable to lock channel list\n");
  158. return -1;
  159. }
  160. while (cl) {
  161. ast_cli(fd, FORMAT, cl->tech->type, cl->tech->description,
  162. (cl->tech->devicestate) ? "yes" : "no",
  163. (cl->tech->indicate) ? "yes" : "no",
  164. (cl->tech->transfer) ? "yes" : "no");
  165. cl = cl->next;
  166. }
  167. ast_mutex_unlock(&chlock);
  168. return RESULT_SUCCESS;
  169. #undef FORMAT
  170. }
  171. static char show_channeltypes_usage[] =
  172. "Usage: show channeltypes\n"
  173. " Shows available channel types registered in your Asterisk server.\n";
  174. static struct ast_cli_entry cli_show_channeltypes =
  175. { { "show", "channeltypes", NULL }, show_channeltypes, "Show available channel types", show_channeltypes_usage };
  176. /*--- ast_check_hangup: Checks to see if a channel is needing hang up */
  177. int ast_check_hangup(struct ast_channel *chan)
  178. {
  179. time_t myt;
  180. /* if soft hangup flag, return true */
  181. if (chan->_softhangup)
  182. return 1;
  183. /* if no technology private data, return true */
  184. if (!chan->tech_pvt)
  185. return 1;
  186. /* if no hangup scheduled, just return here */
  187. if (!chan->whentohangup)
  188. return 0;
  189. time(&myt); /* get current time */
  190. /* return, if not yet */
  191. if (chan->whentohangup > myt)
  192. return 0;
  193. chan->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
  194. return 1;
  195. }
  196. static int ast_check_hangup_locked(struct ast_channel *chan)
  197. {
  198. int res;
  199. ast_mutex_lock(&chan->lock);
  200. res = ast_check_hangup(chan);
  201. ast_mutex_unlock(&chan->lock);
  202. return res;
  203. }
  204. /*--- ast_begin_shutdown: Initiate system shutdown */
  205. void ast_begin_shutdown(int hangup)
  206. {
  207. struct ast_channel *c;
  208. shutting_down = 1;
  209. if (hangup) {
  210. ast_mutex_lock(&chlock);
  211. c = channels;
  212. while(c) {
  213. ast_softhangup(c, AST_SOFTHANGUP_SHUTDOWN);
  214. c = c->next;
  215. }
  216. ast_mutex_unlock(&chlock);
  217. }
  218. }
  219. /*--- ast_active_channels: returns number of active/allocated channels */
  220. int ast_active_channels(void)
  221. {
  222. struct ast_channel *c;
  223. int cnt = 0;
  224. ast_mutex_lock(&chlock);
  225. c = channels;
  226. while(c) {
  227. cnt++;
  228. c = c->next;
  229. }
  230. ast_mutex_unlock(&chlock);
  231. return cnt;
  232. }
  233. /*--- ast_cancel_shutdown: Cancel a shutdown in progress */
  234. void ast_cancel_shutdown(void)
  235. {
  236. shutting_down = 0;
  237. }
  238. /*--- ast_shutting_down: Returns non-zero if Asterisk is being shut down */
  239. int ast_shutting_down(void)
  240. {
  241. return shutting_down;
  242. }
  243. /*--- ast_channel_setwhentohangup: Set when to hangup channel */
  244. void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
  245. {
  246. time_t myt;
  247. struct ast_frame fr = { AST_FRAME_NULL, };
  248. time(&myt);
  249. if (offset)
  250. chan->whentohangup = myt + offset;
  251. else
  252. chan->whentohangup = 0;
  253. ast_queue_frame(chan, &fr);
  254. return;
  255. }
  256. /*--- ast_channel_cmpwhentohangup: Compare a offset with when to hangup channel */
  257. int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
  258. {
  259. time_t whentohangup;
  260. if (chan->whentohangup == 0) {
  261. if (offset == 0)
  262. return (0);
  263. else
  264. return (-1);
  265. } else {
  266. if (offset == 0)
  267. return (1);
  268. else {
  269. whentohangup = offset + time (NULL);
  270. if (chan->whentohangup < whentohangup)
  271. return (1);
  272. else if (chan->whentohangup == whentohangup)
  273. return (0);
  274. else
  275. return (-1);
  276. }
  277. }
  278. }
  279. /*--- ast_channel_register: Register a new telephony channel in Asterisk */
  280. int ast_channel_register(const struct ast_channel_tech *tech)
  281. {
  282. struct chanlist *chan;
  283. ast_mutex_lock(&chlock);
  284. chan = backends;
  285. while (chan) {
  286. if (!strcasecmp(tech->type, chan->tech->type)) {
  287. ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
  288. ast_mutex_unlock(&chlock);
  289. return -1;
  290. }
  291. chan = chan->next;
  292. }
  293. chan = malloc(sizeof(*chan));
  294. if (!chan) {
  295. ast_log(LOG_WARNING, "Out of memory\n");
  296. ast_mutex_unlock(&chlock);
  297. return -1;
  298. }
  299. chan->tech = tech;
  300. chan->next = backends;
  301. backends = chan;
  302. if (option_debug)
  303. ast_log(LOG_DEBUG, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
  304. if (option_verbose > 1)
  305. ast_verbose(VERBOSE_PREFIX_2 "Registered channel type '%s' (%s)\n", chan->tech->type,
  306. chan->tech->description);
  307. ast_mutex_unlock(&chlock);
  308. return 0;
  309. }
  310. void ast_channel_unregister(const struct ast_channel_tech *tech)
  311. {
  312. struct chanlist *chan, *last=NULL;
  313. if (option_debug)
  314. ast_log(LOG_DEBUG, "Unregistering channel type '%s'\n", tech->type);
  315. ast_mutex_lock(&chlock);
  316. chan = backends;
  317. while (chan) {
  318. if (chan->tech == tech) {
  319. if (last)
  320. last->next = chan->next;
  321. else
  322. backends = backends->next;
  323. free(chan);
  324. ast_mutex_unlock(&chlock);
  325. if (option_verbose > 1)
  326. ast_verbose( VERBOSE_PREFIX_2 "Unregistered channel type '%s'\n", tech->type);
  327. return;
  328. }
  329. last = chan;
  330. chan = chan->next;
  331. }
  332. ast_mutex_unlock(&chlock);
  333. }
  334. const struct ast_channel_tech *ast_get_channel_tech(const char *name)
  335. {
  336. struct chanlist *chanls;
  337. if (ast_mutex_lock(&chlock)) {
  338. ast_log(LOG_WARNING, "Unable to lock channel tech list\n");
  339. return NULL;
  340. }
  341. for (chanls = backends; chanls; chanls = chanls->next) {
  342. if (strcasecmp(name, chanls->tech->type))
  343. continue;
  344. ast_mutex_unlock(&chlock);
  345. return chanls->tech;
  346. }
  347. ast_mutex_unlock(&chlock);
  348. return NULL;
  349. }
  350. /*--- ast_cause2str: Gives the string form of a given hangup cause */
  351. const char *ast_cause2str(int cause)
  352. {
  353. int x;
  354. for (x=0; x < sizeof(causes) / sizeof(causes[0]); x++)
  355. if (causes[x].cause == cause)
  356. return causes[x].desc;
  357. return "Unknown";
  358. }
  359. /*--- ast_state2str: Gives the string form of a given channel state */
  360. char *ast_state2str(int state)
  361. {
  362. /* XXX Not reentrant XXX */
  363. static char localtmp[256];
  364. switch(state) {
  365. case AST_STATE_DOWN:
  366. return "Down";
  367. case AST_STATE_RESERVED:
  368. return "Rsrvd";
  369. case AST_STATE_OFFHOOK:
  370. return "OffHook";
  371. case AST_STATE_DIALING:
  372. return "Dialing";
  373. case AST_STATE_RING:
  374. return "Ring";
  375. case AST_STATE_RINGING:
  376. return "Ringing";
  377. case AST_STATE_UP:
  378. return "Up";
  379. case AST_STATE_BUSY:
  380. return "Busy";
  381. default:
  382. snprintf(localtmp, sizeof(localtmp), "Unknown (%d)\n", state);
  383. return localtmp;
  384. }
  385. }
  386. /*--- ast_transfercapability2str: Gives the string form of a given transfer capability */
  387. char *ast_transfercapability2str(int transfercapability)
  388. {
  389. switch(transfercapability) {
  390. case AST_TRANS_CAP_SPEECH:
  391. return "SPEECH";
  392. case AST_TRANS_CAP_DIGITAL:
  393. return "DIGITAL";
  394. case AST_TRANS_CAP_RESTRICTED_DIGITAL:
  395. return "RESTRICTED_DIGITAL";
  396. case AST_TRANS_CAP_3_1K_AUDIO:
  397. return "3K1AUDIO";
  398. case AST_TRANS_CAP_DIGITAL_W_TONES:
  399. return "DIGITAL_W_TONES";
  400. case AST_TRANS_CAP_VIDEO:
  401. return "VIDEO";
  402. default:
  403. return "UNKNOWN";
  404. }
  405. }
  406. /*--- ast_best_codec: Pick the best codec */
  407. int ast_best_codec(int fmts)
  408. {
  409. /* This just our opinion, expressed in code. We are asked to choose
  410. the best codec to use, given no information */
  411. int x;
  412. static int prefs[] =
  413. {
  414. /* Okay, ulaw is used by all telephony equipment, so start with it */
  415. AST_FORMAT_ULAW,
  416. /* Unless of course, you're a silly European, so then prefer ALAW */
  417. AST_FORMAT_ALAW,
  418. /* Okay, well, signed linear is easy to translate into other stuff */
  419. AST_FORMAT_SLINEAR,
  420. /* G.726 is standard ADPCM */
  421. AST_FORMAT_G726,
  422. /* ADPCM has great sound quality and is still pretty easy to translate */
  423. AST_FORMAT_ADPCM,
  424. /* Okay, we're down to vocoders now, so pick GSM because it's small and easier to
  425. translate and sounds pretty good */
  426. AST_FORMAT_GSM,
  427. /* iLBC is not too bad */
  428. AST_FORMAT_ILBC,
  429. /* Speex is free, but computationally more expensive than GSM */
  430. AST_FORMAT_SPEEX,
  431. /* Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
  432. to use it */
  433. AST_FORMAT_LPC10,
  434. /* G.729a is faster than 723 and slightly less expensive */
  435. AST_FORMAT_G729A,
  436. /* Down to G.723.1 which is proprietary but at least designed for voice */
  437. AST_FORMAT_G723_1,
  438. };
  439. /* Find the first prefered codec in the format given */
  440. for (x=0; x < (sizeof(prefs) / sizeof(prefs[0]) ); x++)
  441. if (fmts & prefs[x])
  442. return prefs[x];
  443. ast_log(LOG_WARNING, "Don't know any of 0x%x formats\n", fmts);
  444. return 0;
  445. }
  446. static const struct ast_channel_tech null_tech = {
  447. .type = "NULL",
  448. .description = "Null channel (should not see this)",
  449. };
  450. /*--- ast_channel_alloc: Create a new channel structure */
  451. struct ast_channel *ast_channel_alloc(int needqueue)
  452. {
  453. struct ast_channel *tmp;
  454. int x;
  455. int flags;
  456. struct varshead *headp;
  457. /* If shutting down, don't allocate any new channels */
  458. if (shutting_down) {
  459. ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
  460. return NULL;
  461. }
  462. tmp = malloc(sizeof(struct ast_channel));
  463. if (!tmp) {
  464. ast_log(LOG_WARNING, "Channel allocation failed: Out of memory\n");
  465. return NULL;
  466. }
  467. memset(tmp, 0, sizeof(struct ast_channel));
  468. tmp->sched = sched_context_create();
  469. if (!tmp->sched) {
  470. ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
  471. free(tmp);
  472. return NULL;
  473. }
  474. for (x=0; x<AST_MAX_FDS - 1; x++)
  475. tmp->fds[x] = -1;
  476. #ifdef ZAPTEL_OPTIMIZATIONS
  477. tmp->timingfd = open("/dev/zap/timer", O_RDWR);
  478. if (tmp->timingfd > -1) {
  479. /* Check if timing interface supports new
  480. ping/pong scheme */
  481. flags = 1;
  482. if (!ioctl(tmp->timingfd, ZT_TIMERPONG, &flags))
  483. needqueue = 0;
  484. }
  485. #else
  486. tmp->timingfd = -1;
  487. #endif
  488. if (needqueue) {
  489. if (pipe(tmp->alertpipe)) {
  490. ast_log(LOG_WARNING, "Channel allocation failed: Can't create alert pipe!\n");
  491. free(tmp);
  492. return NULL;
  493. } else {
  494. flags = fcntl(tmp->alertpipe[0], F_GETFL);
  495. fcntl(tmp->alertpipe[0], F_SETFL, flags | O_NONBLOCK);
  496. flags = fcntl(tmp->alertpipe[1], F_GETFL);
  497. fcntl(tmp->alertpipe[1], F_SETFL, flags | O_NONBLOCK);
  498. }
  499. } else
  500. /* Make sure we've got it done right if they don't */
  501. tmp->alertpipe[0] = tmp->alertpipe[1] = -1;
  502. /* Always watch the alertpipe */
  503. tmp->fds[AST_MAX_FDS-1] = tmp->alertpipe[0];
  504. /* And timing pipe */
  505. tmp->fds[AST_MAX_FDS-2] = tmp->timingfd;
  506. strcpy(tmp->name, "**Unkown**");
  507. /* Initial state */
  508. tmp->_state = AST_STATE_DOWN;
  509. tmp->streamid = -1;
  510. tmp->appl = NULL;
  511. tmp->data = NULL;
  512. tmp->fin = global_fin;
  513. tmp->fout = global_fout;
  514. snprintf(tmp->uniqueid, sizeof(tmp->uniqueid), "%li.%d", (long) time(NULL), uniqueint++);
  515. headp = &tmp->varshead;
  516. ast_mutex_init(&tmp->lock);
  517. AST_LIST_HEAD_INIT_NOLOCK(headp);
  518. strcpy(tmp->context, "default");
  519. ast_copy_string(tmp->language, defaultlanguage, sizeof(tmp->language));
  520. strcpy(tmp->exten, "s");
  521. tmp->priority = 1;
  522. tmp->amaflags = ast_default_amaflags;
  523. ast_copy_string(tmp->accountcode, ast_default_accountcode, sizeof(tmp->accountcode));
  524. tmp->tech = &null_tech;
  525. ast_mutex_lock(&chlock);
  526. tmp->next = channels;
  527. channels = tmp;
  528. ast_mutex_unlock(&chlock);
  529. return tmp;
  530. }
  531. /*--- ast_queue_frame: Queue an outgoing media frame */
  532. int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
  533. {
  534. struct ast_frame *f;
  535. struct ast_frame *prev, *cur;
  536. int blah = 1;
  537. int qlen = 0;
  538. /* Build us a copy and free the original one */
  539. f = ast_frdup(fin);
  540. if (!f) {
  541. ast_log(LOG_WARNING, "Unable to duplicate frame\n");
  542. return -1;
  543. }
  544. ast_mutex_lock(&chan->lock);
  545. prev = NULL;
  546. cur = chan->readq;
  547. while(cur) {
  548. if ((cur->frametype == AST_FRAME_CONTROL) && (cur->subclass == AST_CONTROL_HANGUP)) {
  549. /* Don't bother actually queueing anything after a hangup */
  550. ast_frfree(f);
  551. ast_mutex_unlock(&chan->lock);
  552. return 0;
  553. }
  554. prev = cur;
  555. cur = cur->next;
  556. qlen++;
  557. }
  558. /* Allow up to 96 voice frames outstanding, and up to 128 total frames */
  559. if (((fin->frametype == AST_FRAME_VOICE) && (qlen > 96)) || (qlen > 128)) {
  560. if (fin->frametype != AST_FRAME_VOICE) {
  561. ast_log(LOG_WARNING, "Exceptionally long queue length queuing to %s\n", chan->name);
  562. CRASH;
  563. } else {
  564. ast_log(LOG_DEBUG, "Dropping voice to exceptionally long queue on %s\n", chan->name);
  565. ast_frfree(f);
  566. ast_mutex_unlock(&chan->lock);
  567. return 0;
  568. }
  569. }
  570. if (prev)
  571. prev->next = f;
  572. else
  573. chan->readq = f;
  574. if (chan->alertpipe[1] > -1) {
  575. if (write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah))
  576. ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d (qlen = %d): %s!\n",
  577. chan->name, f->frametype, f->subclass, qlen, strerror(errno));
  578. #ifdef ZAPTEL_OPTIMIZATIONS
  579. } else if (chan->timingfd > -1) {
  580. ioctl(chan->timingfd, ZT_TIMERPING, &blah);
  581. #endif
  582. } else if (ast_test_flag(chan, AST_FLAG_BLOCKING)) {
  583. pthread_kill(chan->blocker, SIGURG);
  584. }
  585. ast_mutex_unlock(&chan->lock);
  586. return 0;
  587. }
  588. /*--- ast_queue_hangup: Queue a hangup frame for channel */
  589. int ast_queue_hangup(struct ast_channel *chan)
  590. {
  591. struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
  592. /* Yeah, let's not change a lock-critical value without locking */
  593. if (!ast_mutex_trylock(&chan->lock)) {
  594. chan->_softhangup |= AST_SOFTHANGUP_DEV;
  595. ast_mutex_unlock(&chan->lock);
  596. }
  597. return ast_queue_frame(chan, &f);
  598. }
  599. /*--- ast_queue_control: Queue a control frame */
  600. int ast_queue_control(struct ast_channel *chan, int control)
  601. {
  602. struct ast_frame f = { AST_FRAME_CONTROL, };
  603. f.subclass = control;
  604. return ast_queue_frame(chan, &f);
  605. }
  606. /*--- ast_channel_defer_dtmf: Set defer DTMF flag on channel */
  607. int ast_channel_defer_dtmf(struct ast_channel *chan)
  608. {
  609. int pre = 0;
  610. if (chan) {
  611. pre = ast_test_flag(chan, AST_FLAG_DEFER_DTMF);
  612. ast_set_flag(chan, AST_FLAG_DEFER_DTMF);
  613. }
  614. return pre;
  615. }
  616. /*--- ast_channel_undefer_dtmf: Unset defer DTMF flag on channel */
  617. void ast_channel_undefer_dtmf(struct ast_channel *chan)
  618. {
  619. if (chan)
  620. ast_clear_flag(chan, AST_FLAG_DEFER_DTMF);
  621. }
  622. /*
  623. * Helper function to find channels. It supports these modes:
  624. *
  625. * prev != NULL : get channel next in list after prev
  626. * name != NULL : get channel with matching name
  627. * name != NULL && namelen != 0 : get channel whose name starts with prefix
  628. * exten != NULL : get channel whose exten or macroexten matches
  629. * context != NULL && exten != NULL : get channel whose context or macrocontext
  630. *
  631. * It returns with the channel's lock held. If getting the individual lock fails,
  632. * unlock and retry quickly up to 10 times, then give up.
  633. *
  634. * XXX Note that this code has cost O(N) because of the need to verify
  635. * that the object is still on the global list.
  636. *
  637. * XXX also note that accessing fields (e.g. c->name in ast_log())
  638. * can only be done with the lock held or someone could delete the
  639. * object while we work on it. This causes some ugliness in the code.
  640. * Note that removing the first ast_log() may be harmful, as it would
  641. * shorten the retry period and possibly cause failures.
  642. * We should definitely go for a better scheme that is deadlock-free.
  643. */
  644. static struct ast_channel *channel_find_locked(const struct ast_channel *prev,
  645. const char *name, const int namelen,
  646. const char *context, const char *exten)
  647. {
  648. const char *msg = prev ? "deadlock" : "initial deadlock";
  649. int retries, done;
  650. struct ast_channel *c;
  651. for (retries = 0; retries < 10; retries++) {
  652. ast_mutex_lock(&chlock);
  653. for (c = channels; c; c = c->next) {
  654. if (!prev) {
  655. /* want head of list */
  656. if (!name && !exten)
  657. break;
  658. if (name) {
  659. /* want match by full name */
  660. if (!namelen) {
  661. if (!strcasecmp(c->name, name))
  662. break;
  663. else
  664. continue;
  665. }
  666. /* want match by name prefix */
  667. if (!strncasecmp(c->name, name, namelen))
  668. break;
  669. } else if (exten) {
  670. /* want match by context and exten */
  671. if (context && (strcasecmp(c->context, context) &&
  672. strcasecmp(c->macrocontext, context)))
  673. continue;
  674. /* match by exten */
  675. if (strcasecmp(c->exten, exten) &&
  676. strcasecmp(c->macroexten, exten))
  677. continue;
  678. else
  679. break;
  680. }
  681. } else if (c == prev) { /* found, return c->next */
  682. c = c->next;
  683. break;
  684. }
  685. }
  686. /* exit if chan not found or mutex acquired successfully */
  687. done = (c == NULL) || (ast_mutex_trylock(&c->lock) == 0);
  688. /* this is slightly unsafe, as we _should_ hold the lock to access c->name */
  689. if (!done && c)
  690. ast_log(LOG_DEBUG, "Avoiding %s for '%s'\n", msg, c->name);
  691. ast_mutex_unlock(&chlock);
  692. if (done)
  693. return c;
  694. usleep(1);
  695. }
  696. /*
  697. * c is surely not null, but we don't have the lock so cannot
  698. * access c->name
  699. */
  700. ast_log(LOG_WARNING, "Avoided %s for '%p', %d retries!\n",
  701. msg, c, retries);
  702. return NULL;
  703. }
  704. /*--- ast_channel_walk_locked: Browse channels in use */
  705. struct ast_channel *ast_channel_walk_locked(const struct ast_channel *prev)
  706. {
  707. return channel_find_locked(prev, NULL, 0, NULL, NULL);
  708. }
  709. /*--- ast_get_channel_by_name_locked: Get channel by name and lock it */
  710. struct ast_channel *ast_get_channel_by_name_locked(const char *name)
  711. {
  712. return channel_find_locked(NULL, name, 0, NULL, NULL);
  713. }
  714. /*--- ast_get_channel_by_name_prefix_locked: Get channel by name prefix and lock it */
  715. struct ast_channel *ast_get_channel_by_name_prefix_locked(const char *name, const int namelen)
  716. {
  717. return channel_find_locked(NULL, name, namelen, NULL, NULL);
  718. }
  719. /*--- ast_walk_channel_by_name_prefix_locked: Get next channel by name prefix and lock it */
  720. struct ast_channel *ast_walk_channel_by_name_prefix_locked(struct ast_channel *chan, const char *name, const int namelen)
  721. {
  722. return channel_find_locked(chan, name, namelen, NULL, NULL);
  723. }
  724. /*--- ast_get_channel_by_exten_locked: Get channel by exten (and optionally context) and lock it */
  725. struct ast_channel *ast_get_channel_by_exten_locked(const char *exten, const char *context)
  726. {
  727. return channel_find_locked(NULL, NULL, 0, context, exten);
  728. }
  729. /*--- ast_safe_sleep_conditional: Wait, look for hangups and condition arg */
  730. int ast_safe_sleep_conditional( struct ast_channel *chan, int ms,
  731. int (*cond)(void*), void *data )
  732. {
  733. struct ast_frame *f;
  734. while(ms > 0) {
  735. if( cond && ((*cond)(data) == 0 ) )
  736. return 0;
  737. ms = ast_waitfor(chan, ms);
  738. if (ms <0)
  739. return -1;
  740. if (ms > 0) {
  741. f = ast_read(chan);
  742. if (!f)
  743. return -1;
  744. ast_frfree(f);
  745. }
  746. }
  747. return 0;
  748. }
  749. /*--- ast_safe_sleep: Wait, look for hangups */
  750. int ast_safe_sleep(struct ast_channel *chan, int ms)
  751. {
  752. struct ast_frame *f;
  753. while(ms > 0) {
  754. ms = ast_waitfor(chan, ms);
  755. if (ms <0)
  756. return -1;
  757. if (ms > 0) {
  758. f = ast_read(chan);
  759. if (!f)
  760. return -1;
  761. ast_frfree(f);
  762. }
  763. }
  764. return 0;
  765. }
  766. static void free_cid(struct ast_callerid *cid)
  767. {
  768. if (cid->cid_dnid)
  769. free(cid->cid_dnid);
  770. if (cid->cid_num)
  771. free(cid->cid_num);
  772. if (cid->cid_name)
  773. free(cid->cid_name);
  774. if (cid->cid_ani)
  775. free(cid->cid_ani);
  776. if (cid->cid_rdnis)
  777. free(cid->cid_rdnis);
  778. }
  779. /*--- ast_channel_free: Free a channel structure */
  780. void ast_channel_free(struct ast_channel *chan)
  781. {
  782. struct ast_channel *last=NULL, *cur;
  783. int fd;
  784. struct ast_var_t *vardata;
  785. struct ast_frame *f, *fp;
  786. struct varshead *headp;
  787. char name[AST_CHANNEL_NAME];
  788. headp=&chan->varshead;
  789. ast_mutex_lock(&chlock);
  790. cur = channels;
  791. while(cur) {
  792. if (cur == chan) {
  793. if (last)
  794. last->next = cur->next;
  795. else
  796. channels = cur->next;
  797. break;
  798. }
  799. last = cur;
  800. cur = cur->next;
  801. }
  802. if (!cur)
  803. ast_log(LOG_WARNING, "Unable to find channel in list\n");
  804. else {
  805. /* Lock and unlock the channel just to be sure nobody
  806. has it locked still */
  807. ast_mutex_lock(&cur->lock);
  808. ast_mutex_unlock(&cur->lock);
  809. }
  810. if (chan->tech_pvt) {
  811. ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name);
  812. free(chan->tech_pvt);
  813. }
  814. ast_copy_string(name, chan->name, sizeof(name));
  815. /* Stop monitoring */
  816. if (chan->monitor) {
  817. chan->monitor->stop( chan, 0 );
  818. }
  819. /* If there is native format music-on-hold state, free it */
  820. if(chan->music_state)
  821. ast_moh_cleanup(chan);
  822. /* Free translatosr */
  823. if (chan->readtrans)
  824. ast_translator_free_path(chan->readtrans);
  825. if (chan->writetrans)
  826. ast_translator_free_path(chan->writetrans);
  827. if (chan->pbx)
  828. ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name);
  829. free_cid(&chan->cid);
  830. ast_mutex_destroy(&chan->lock);
  831. /* Close pipes if appropriate */
  832. if ((fd = chan->alertpipe[0]) > -1)
  833. close(fd);
  834. if ((fd = chan->alertpipe[1]) > -1)
  835. close(fd);
  836. if ((fd = chan->timingfd) > -1)
  837. close(fd);
  838. f = chan->readq;
  839. chan->readq = NULL;
  840. while(f) {
  841. fp = f;
  842. f = f->next;
  843. ast_frfree(fp);
  844. }
  845. /* loop over the variables list, freeing all data and deleting list items */
  846. /* no need to lock the list, as the channel is already locked */
  847. while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
  848. ast_var_delete(vardata);
  849. free(chan);
  850. ast_mutex_unlock(&chlock);
  851. ast_device_state_changed_literal(name);
  852. }
  853. int ast_channel_spy_add(struct ast_channel *chan, struct ast_channel_spy *spy)
  854. {
  855. if (!ast_test_flag(spy, CHANSPY_FORMAT_AUDIO)) {
  856. ast_log(LOG_WARNING, "Could not add channel spy '%s' to channel '%s', only audio format spies are supported.\n",
  857. spy->type, chan->name);
  858. return -1;
  859. }
  860. if (ast_test_flag(spy, CHANSPY_READ_VOLADJUST) && (spy->read_queue.format != AST_FORMAT_SLINEAR)) {
  861. ast_log(LOG_WARNING, "Cannot provide volume adjustment on '%s' format spies\n",
  862. ast_getformatname(spy->read_queue.format));
  863. return -1;
  864. }
  865. if (ast_test_flag(spy, CHANSPY_WRITE_VOLADJUST) && (spy->write_queue.format != AST_FORMAT_SLINEAR)) {
  866. ast_log(LOG_WARNING, "Cannot provide volume adjustment on '%s' format spies\n",
  867. ast_getformatname(spy->write_queue.format));
  868. return -1;
  869. }
  870. if (ast_test_flag(spy, CHANSPY_MIXAUDIO) &&
  871. ((spy->read_queue.format != AST_FORMAT_SLINEAR) ||
  872. (spy->write_queue.format != AST_FORMAT_SLINEAR))) {
  873. ast_log(LOG_WARNING, "Cannot provide audio mixing on '%s'-'%s' format spies\n",
  874. ast_getformatname(spy->read_queue.format), ast_getformatname(spy->write_queue.format));
  875. return -1;
  876. }
  877. if (!chan->spies) {
  878. if (!(chan->spies = calloc(1, sizeof(*chan->spies)))) {
  879. ast_log(LOG_WARNING, "Memory allocation failure\n");
  880. return -1;
  881. }
  882. AST_LIST_HEAD_INIT_NOLOCK(&chan->spies->list);
  883. AST_LIST_INSERT_HEAD(&chan->spies->list, spy, list);
  884. } else {
  885. AST_LIST_INSERT_TAIL(&chan->spies->list, spy, list);
  886. }
  887. if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE) {
  888. ast_cond_init(&spy->trigger, NULL);
  889. ast_set_flag(spy, CHANSPY_TRIGGER_READ);
  890. ast_clear_flag(spy, CHANSPY_TRIGGER_WRITE);
  891. }
  892. ast_log(LOG_DEBUG, "Spy %s added to channel %s\n",
  893. spy->type, chan->name);
  894. return 0;
  895. }
  896. void ast_channel_spy_stop_by_type(struct ast_channel *chan, const char *type)
  897. {
  898. struct ast_channel_spy *spy;
  899. if (!chan->spies)
  900. return;
  901. AST_LIST_TRAVERSE(&chan->spies->list, spy, list) {
  902. if ((spy->type == type) && (spy->status == CHANSPY_RUNNING))
  903. spy->status = CHANSPY_DONE;
  904. }
  905. }
  906. void ast_channel_spy_trigger_wait(struct ast_channel_spy *spy)
  907. {
  908. ast_cond_wait(&spy->trigger, &spy->lock);
  909. }
  910. void ast_channel_spy_remove(struct ast_channel *chan, struct ast_channel_spy *spy)
  911. {
  912. struct ast_frame *f;
  913. if (!chan->spies)
  914. return;
  915. AST_LIST_REMOVE(&chan->spies->list, spy, list);
  916. ast_mutex_lock(&spy->lock);
  917. for (f = spy->read_queue.head; f; f = spy->read_queue.head) {
  918. spy->read_queue.head = f->next;
  919. ast_frfree(f);
  920. }
  921. for (f = spy->write_queue.head; f; f = spy->write_queue.head) {
  922. spy->write_queue.head = f->next;
  923. ast_frfree(f);
  924. }
  925. if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE)
  926. ast_cond_destroy(&spy->trigger);
  927. ast_mutex_unlock(&spy->lock);
  928. ast_log(LOG_DEBUG, "Spy %s removed from channel %s\n",
  929. spy->type, chan->name);
  930. if (AST_LIST_EMPTY(&chan->spies->list)) {
  931. if (chan->spies->read_translator.path)
  932. ast_translator_free_path(chan->spies->read_translator.path);
  933. if (chan->spies->write_translator.path)
  934. ast_translator_free_path(chan->spies->write_translator.path);
  935. free(chan->spies);
  936. chan->spies = NULL;
  937. }
  938. }
  939. static void detach_spies(struct ast_channel *chan)
  940. {
  941. struct ast_channel_spy *spy;
  942. if (!chan->spies)
  943. return;
  944. /* Marking the spies as done is sufficient. Chanspy or spy users will get the picture. */
  945. AST_LIST_TRAVERSE(&chan->spies->list, spy, list) {
  946. ast_mutex_lock(&spy->lock);
  947. if (spy->status == CHANSPY_RUNNING)
  948. spy->status = CHANSPY_DONE;
  949. if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE)
  950. ast_cond_signal(&spy->trigger);
  951. ast_mutex_unlock(&spy->lock);
  952. }
  953. AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->spies->list, spy, list)
  954. ast_channel_spy_remove(chan, spy);
  955. AST_LIST_TRAVERSE_SAFE_END;
  956. }
  957. /*--- ast_softhangup_nolock: Softly hangup a channel, don't lock */
  958. int ast_softhangup_nolock(struct ast_channel *chan, int cause)
  959. {
  960. int res = 0;
  961. struct ast_frame f = { AST_FRAME_NULL };
  962. if (option_debug)
  963. ast_log(LOG_DEBUG, "Soft-Hanging up channel '%s'\n", chan->name);
  964. /* Inform channel driver that we need to be hung up, if it cares */
  965. chan->_softhangup |= cause;
  966. ast_queue_frame(chan, &f);
  967. /* Interrupt any poll call or such */
  968. if (ast_test_flag(chan, AST_FLAG_BLOCKING))
  969. pthread_kill(chan->blocker, SIGURG);
  970. return res;
  971. }
  972. /*--- ast_softhangup_nolock: Softly hangup a channel, lock */
  973. int ast_softhangup(struct ast_channel *chan, int cause)
  974. {
  975. int res;
  976. ast_mutex_lock(&chan->lock);
  977. res = ast_softhangup_nolock(chan, cause);
  978. ast_mutex_unlock(&chan->lock);
  979. return res;
  980. }
  981. enum spy_direction {
  982. SPY_READ,
  983. SPY_WRITE,
  984. };
  985. #define SPY_QUEUE_SAMPLE_LIMIT 4000 /* half of one second */
  986. static void queue_frame_to_spies(struct ast_channel *chan, struct ast_frame *f, enum spy_direction dir)
  987. {
  988. struct ast_frame *translated_frame = NULL;
  989. struct ast_channel_spy *spy;
  990. struct ast_channel_spy_queue *queue;
  991. struct ast_channel_spy_queue *other_queue;
  992. struct channel_spy_trans *trans;
  993. struct ast_frame *last;
  994. trans = (dir == SPY_READ) ? &chan->spies->read_translator : &chan->spies->write_translator;
  995. AST_LIST_TRAVERSE(&chan->spies->list, spy, list) {
  996. ast_mutex_lock(&spy->lock);
  997. queue = (dir == SPY_READ) ? &spy->read_queue : &spy->write_queue;
  998. if ((queue->format == AST_FORMAT_SLINEAR) && (f->subclass != AST_FORMAT_SLINEAR)) {
  999. if (!translated_frame) {
  1000. if (trans->path && (trans->last_format != f->subclass)) {
  1001. ast_translator_free_path(trans->path);
  1002. trans->path = NULL;
  1003. }
  1004. if (!trans->path) {
  1005. ast_log(LOG_DEBUG, "Building translator from %s to SLINEAR for spies on channel %s\n",
  1006. ast_getformatname(f->subclass), chan->name);
  1007. if ((trans->path = ast_translator_build_path(AST_FORMAT_SLINEAR, f->subclass)) == NULL) {
  1008. ast_log(LOG_WARNING, "Cannot build a path from %s to %s\n",
  1009. ast_getformatname(f->subclass), ast_getformatname(AST_FORMAT_SLINEAR));
  1010. ast_mutex_unlock(&spy->lock);
  1011. continue;
  1012. } else {
  1013. trans->last_format = f->subclass;
  1014. }
  1015. }
  1016. translated_frame = ast_translate(trans->path, f, 0);
  1017. }
  1018. for (last = queue->head; last && last->next; last = last->next);
  1019. if (last)
  1020. last->next = ast_frdup(translated_frame);
  1021. else
  1022. queue->head = ast_frdup(translated_frame);
  1023. } else {
  1024. if (f->subclass != queue->format) {
  1025. ast_log(LOG_WARNING, "Spy '%s' on channel '%s' wants format '%s', but frame is '%s', dropping\n",
  1026. spy->type, chan->name,
  1027. ast_getformatname(queue->format), ast_getformatname(f->subclass));
  1028. ast_mutex_unlock(&spy->lock);
  1029. continue;
  1030. }
  1031. for (last = queue->head; last && last->next; last = last->next);
  1032. if (last)
  1033. last->next = ast_frdup(f);
  1034. else
  1035. queue->head = ast_frdup(f);
  1036. }
  1037. queue->samples += f->samples;
  1038. if (queue->samples > SPY_QUEUE_SAMPLE_LIMIT) {
  1039. if (ast_test_flag(spy, CHANSPY_TRIGGER_MODE) != CHANSPY_TRIGGER_NONE) {
  1040. other_queue = (dir == SPY_WRITE) ? &spy->read_queue : &spy->write_queue;
  1041. if (other_queue->samples == 0) {
  1042. switch (ast_test_flag(spy, CHANSPY_TRIGGER_MODE)) {
  1043. case CHANSPY_TRIGGER_READ:
  1044. if (dir == SPY_WRITE) {
  1045. ast_set_flag(spy, CHANSPY_TRIGGER_WRITE);
  1046. ast_clear_flag(spy, CHANSPY_TRIGGER_READ);
  1047. if (option_debug)
  1048. ast_log(LOG_DEBUG, "Switching spy '%s' on '%s' to write-trigger mode\n",
  1049. spy->type, chan->name);
  1050. }
  1051. break;
  1052. case CHANSPY_TRIGGER_WRITE:
  1053. if (dir == SPY_READ) {
  1054. ast_set_flag(spy, CHANSPY_TRIGGER_READ);
  1055. ast_clear_flag(spy, CHANSPY_TRIGGER_WRITE);
  1056. if (option_debug)
  1057. ast_log(LOG_DEBUG, "Switching spy '%s' on '%s' to read-trigger mode\n",
  1058. spy->type, chan->name);
  1059. }
  1060. break;
  1061. }
  1062. if (option_debug)
  1063. ast_log(LOG_DEBUG, "Triggering queue flush for spy '%s' on '%s'\n",
  1064. spy->type, chan->name);
  1065. ast_set_flag(spy, CHANSPY_TRIGGER_FLUSH);
  1066. ast_cond_signal(&spy->trigger);
  1067. ast_mutex_unlock(&spy->lock);
  1068. continue;
  1069. }
  1070. }
  1071. if (option_debug)
  1072. ast_log(LOG_DEBUG, "Spy '%s' on channel '%s' %s queue too long, dropping frames\n",
  1073. spy->type, chan->name, (dir == SPY_READ) ? "read" : "write");
  1074. while (queue->samples > SPY_QUEUE_SAMPLE_LIMIT) {
  1075. struct ast_frame *drop = queue->head;
  1076. queue->samples -= drop->samples;
  1077. queue->head = drop->next;
  1078. ast_frfree(drop);
  1079. }
  1080. } else {
  1081. switch (ast_test_flag(spy, CHANSPY_TRIGGER_MODE)) {
  1082. case CHANSPY_TRIGGER_READ:
  1083. if (dir == SPY_READ)
  1084. ast_cond_signal(&spy->trigger);
  1085. break;
  1086. case CHANSPY_TRIGGER_WRITE:
  1087. if (dir == SPY_WRITE)
  1088. ast_cond_signal(&spy->trigger);
  1089. break;
  1090. }
  1091. }
  1092. ast_mutex_unlock(&spy->lock);
  1093. }
  1094. if (translated_frame)
  1095. ast_frfree(translated_frame);
  1096. }
  1097. static void free_translation(struct ast_channel *clone)
  1098. {
  1099. if (clone->writetrans)
  1100. ast_translator_free_path(clone->writetrans);
  1101. if (clone->readtrans)
  1102. ast_translator_free_path(clone->readtrans);
  1103. clone->writetrans = NULL;
  1104. clone->readtrans = NULL;
  1105. clone->rawwriteformat = clone->nativeformats;
  1106. clone->rawreadformat = clone->nativeformats;
  1107. }
  1108. /*--- ast_hangup: Hangup a channel */
  1109. int ast_hangup(struct ast_channel *chan)
  1110. {
  1111. int res = 0;
  1112. /* Don't actually hang up a channel that will masquerade as someone else, or
  1113. if someone is going to masquerade as us */
  1114. ast_mutex_lock(&chan->lock);
  1115. detach_spies(chan); /* get rid of spies */
  1116. if (chan->masq) {
  1117. if (ast_do_masquerade(chan))
  1118. ast_log(LOG_WARNING, "Failed to perform masquerade\n");
  1119. }
  1120. if (chan->masq) {
  1121. ast_log(LOG_WARNING, "%s getting hung up, but someone is trying to masq into us?!?\n", chan->name);
  1122. ast_mutex_unlock(&chan->lock);
  1123. return 0;
  1124. }
  1125. /* If this channel is one which will be masqueraded into something,
  1126. mark it as a zombie already, so we know to free it later */
  1127. if (chan->masqr) {
  1128. ast_set_flag(chan, AST_FLAG_ZOMBIE);
  1129. ast_mutex_unlock(&chan->lock);
  1130. return 0;
  1131. }
  1132. free_translation(chan);
  1133. if (chan->stream) /* Close audio stream */
  1134. ast_closestream(chan->stream);
  1135. if (chan->vstream) /* Close video stream */
  1136. ast_closestream(chan->vstream);
  1137. if (chan->sched)
  1138. sched_context_destroy(chan->sched);
  1139. if (chan->generatordata) /* Clear any tone stuff remaining */
  1140. chan->generator->release(chan, chan->generatordata);
  1141. chan->generatordata = NULL;
  1142. chan->generator = NULL;
  1143. if (chan->cdr) { /* End the CDR if it hasn't already */
  1144. ast_cdr_end(chan->cdr);
  1145. ast_cdr_detach(chan->cdr); /* Post and Free the CDR */
  1146. chan->cdr = NULL;
  1147. }
  1148. if (ast_test_flag(chan, AST_FLAG_BLOCKING)) {
  1149. ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
  1150. "is blocked by thread %ld in procedure %s! Expect a failure\n",
  1151. (long)pthread_self(), chan->name, (long)chan->blocker, chan->blockproc);
  1152. CRASH;
  1153. }
  1154. if (!ast_test_flag(chan, AST_FLAG_ZOMBIE)) {
  1155. if (option_debug)
  1156. ast_log(LOG_DEBUG, "Hanging up channel '%s'\n", chan->name);
  1157. if (chan->tech->hangup)
  1158. res = chan->tech->hangup(chan);
  1159. } else {
  1160. if (option_debug)
  1161. ast_log(LOG_DEBUG, "Hanging up zombie '%s'\n", chan->name);
  1162. }
  1163. ast_mutex_unlock(&chan->lock);
  1164. manager_event(EVENT_FLAG_CALL, "Hangup",
  1165. "Channel: %s\r\n"
  1166. "Uniqueid: %s\r\n"
  1167. "Cause: %d\r\n"
  1168. "Cause-txt: %s\r\n",
  1169. chan->name,
  1170. chan->uniqueid,
  1171. chan->hangupcause,
  1172. ast_cause2str(chan->hangupcause)
  1173. );
  1174. ast_channel_free(chan);
  1175. return res;
  1176. }
  1177. int ast_answer(struct ast_channel *chan)
  1178. {
  1179. int res = 0;
  1180. ast_mutex_lock(&chan->lock);
  1181. /* Stop if we're a zombie or need a soft hangup */
  1182. if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
  1183. ast_mutex_unlock(&chan->lock);
  1184. return -1;
  1185. }
  1186. switch(chan->_state) {
  1187. case AST_STATE_RINGING:
  1188. case AST_STATE_RING:
  1189. if (chan->tech->answer)
  1190. res = chan->tech->answer(chan);
  1191. ast_setstate(chan, AST_STATE_UP);
  1192. if (chan->cdr)
  1193. ast_cdr_answer(chan->cdr);
  1194. ast_mutex_unlock(&chan->lock);
  1195. return res;
  1196. break;
  1197. case AST_STATE_UP:
  1198. if (chan->cdr)
  1199. ast_cdr_answer(chan->cdr);
  1200. break;
  1201. }
  1202. ast_mutex_unlock(&chan->lock);
  1203. return 0;
  1204. }
  1205. void ast_deactivate_generator(struct ast_channel *chan)
  1206. {
  1207. ast_mutex_lock(&chan->lock);
  1208. if (chan->generatordata) {
  1209. if (chan->generator && chan->generator->release)
  1210. chan->generator->release(chan, chan->generatordata);
  1211. chan->generatordata = NULL;
  1212. chan->generator = NULL;
  1213. ast_clear_flag(chan, AST_FLAG_WRITE_INT);
  1214. ast_settimeout(chan, 0, NULL, NULL);
  1215. }
  1216. ast_mutex_unlock(&chan->lock);
  1217. }
  1218. static int generator_force(void *data)
  1219. {
  1220. /* Called if generator doesn't have data */
  1221. void *tmp;
  1222. int res;
  1223. int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
  1224. struct ast_channel *chan = data;
  1225. tmp = chan->generatordata;
  1226. chan->generatordata = NULL;
  1227. generate = chan->generator->generate;
  1228. res = generate(chan, tmp, 0, 160);
  1229. chan->generatordata = tmp;
  1230. if (res) {
  1231. ast_log(LOG_DEBUG, "Auto-deactivating generator\n");
  1232. ast_deactivate_generator(chan);
  1233. }
  1234. return 0;
  1235. }
  1236. int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
  1237. {
  1238. int res = 0;
  1239. ast_mutex_lock(&chan->lock);
  1240. if (chan->generatordata) {
  1241. if (chan->generator && chan->generator->release)
  1242. chan->generator->release(chan, chan->generatordata);
  1243. chan->generatordata = NULL;
  1244. }
  1245. ast_prod(chan);
  1246. if (gen->alloc) {
  1247. if (!(chan->generatordata = gen->alloc(chan, params)))
  1248. res = -1;
  1249. }
  1250. if (!res) {
  1251. ast_settimeout(chan, 160, generator_force, chan);
  1252. chan->generator = gen;
  1253. }
  1254. ast_mutex_unlock(&chan->lock);
  1255. return res;
  1256. }
  1257. /*--- ast_waitfor_n_fd: Wait for x amount of time on a file descriptor to have input. */
  1258. int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
  1259. {
  1260. struct timeval start = { 0 , 0 };
  1261. int res;
  1262. int x, y;
  1263. int winner = -1;
  1264. int spoint;
  1265. struct pollfd *pfds;
  1266. pfds = alloca(sizeof(struct pollfd) * n);
  1267. if (!pfds) {
  1268. ast_log(LOG_ERROR, "Out of memory\n");
  1269. return -1;
  1270. }
  1271. if (*ms > 0)
  1272. start = ast_tvnow();
  1273. y = 0;
  1274. for (x=0; x < n; x++) {
  1275. if (fds[x] > -1) {
  1276. pfds[y].fd = fds[x];
  1277. pfds[y].events = POLLIN | POLLPRI;
  1278. y++;
  1279. }
  1280. }
  1281. res = poll(pfds, y, *ms);
  1282. if (res < 0) {
  1283. /* Simulate a timeout if we were interrupted */
  1284. if (errno != EINTR)
  1285. *ms = -1;
  1286. else
  1287. *ms = 0;
  1288. return -1;
  1289. }
  1290. spoint = 0;
  1291. for (x=0; x < n; x++) {
  1292. if (fds[x] > -1) {
  1293. if ((res = ast_fdisset(pfds, fds[x], y, &spoint))) {
  1294. winner = fds[x];
  1295. if (exception) {
  1296. if (res & POLLPRI)
  1297. *exception = -1;
  1298. else
  1299. *exception = 0;
  1300. }
  1301. }
  1302. }
  1303. }
  1304. if (*ms > 0) {
  1305. *ms -= ast_tvdiff_ms(ast_tvnow(), start);
  1306. if (*ms < 0)
  1307. *ms = 0;
  1308. }
  1309. return winner;
  1310. }
  1311. /*--- ast_waitfor_nanfds: Wait for x amount of time on a file descriptor to have input. */
  1312. struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
  1313. int *exception, int *outfd, int *ms)
  1314. {
  1315. struct timeval start = { 0 , 0 };
  1316. struct pollfd *pfds;
  1317. int res;
  1318. long rms;
  1319. int x, y, max;
  1320. int spoint;
  1321. time_t now = 0;
  1322. long whentohangup = 0, havewhen = 0, diff;
  1323. struct ast_channel *winner = NULL;
  1324. pfds = alloca(sizeof(struct pollfd) * (n * AST_MAX_FDS + nfds));
  1325. if (!pfds) {
  1326. ast_log(LOG_ERROR, "Out of memory\n");
  1327. *outfd = -1;
  1328. return NULL;
  1329. }
  1330. if (outfd)
  1331. *outfd = -99999;
  1332. if (exception)
  1333. *exception = 0;
  1334. /* Perform any pending masquerades */
  1335. for (x=0; x < n; x++) {
  1336. ast_mutex_lock(&c[x]->lock);
  1337. if (c[x]->whentohangup) {
  1338. if (!havewhen)
  1339. time(&now);
  1340. diff = c[x]->whentohangup - now;
  1341. if (!havewhen || (diff < whentohangup)) {
  1342. havewhen++;
  1343. whentohangup = diff;
  1344. }
  1345. }
  1346. if (c[x]->masq) {
  1347. if (ast_do_masquerade(c[x])) {
  1348. ast_log(LOG_WARNING, "Masquerade failed\n");
  1349. *ms = -1;
  1350. ast_mutex_unlock(&c[x]->lock);
  1351. return NULL;
  1352. }
  1353. }
  1354. ast_mutex_unlock(&c[x]->lock);
  1355. }
  1356. rms = *ms;
  1357. if (havewhen) {
  1358. if ((*ms < 0) || (whentohangup * 1000 < *ms)) {
  1359. rms = whentohangup * 1000;
  1360. }
  1361. }
  1362. max = 0;
  1363. for (x=0; x < n; x++) {
  1364. for (y=0; y< AST_MAX_FDS; y++) {
  1365. if (c[x]->fds[y] > -1) {
  1366. pfds[max].fd = c[x]->fds[y];
  1367. pfds[max].events = POLLIN | POLLPRI;
  1368. pfds[max].revents = 0;
  1369. max++;
  1370. }
  1371. }
  1372. CHECK_BLOCKING(c[x]);
  1373. }
  1374. for (x=0; x < nfds; x++) {
  1375. if (fds[x] > -1) {
  1376. pfds[max].fd = fds[x];
  1377. pfds[max].events = POLLIN | POLLPRI;
  1378. pfds[max].revents = 0;
  1379. max++;
  1380. }
  1381. }
  1382. if (*ms > 0)
  1383. start = ast_tvnow();
  1384. if (sizeof(int) == 4) {
  1385. do {
  1386. int kbrms = rms;
  1387. if (kbrms > 600000)
  1388. kbrms = 600000;
  1389. res = poll(pfds, max, kbrms);
  1390. if (!res)
  1391. rms -= kbrms;
  1392. } while (!res && (rms > 0));
  1393. } else {
  1394. res = poll(pfds, max, rms);
  1395. }
  1396. if (res < 0) {
  1397. for (x=0; x < n; x++)
  1398. ast_clear_flag(c[x], AST_FLAG_BLOCKING);
  1399. /* Simulate a timeout if we were interrupted */
  1400. if (errno != EINTR)
  1401. *ms = -1;
  1402. else {
  1403. /* Just an interrupt */
  1404. #if 0
  1405. *ms = 0;
  1406. #endif
  1407. }
  1408. return NULL;
  1409. } else {
  1410. /* If no fds signalled, then timeout. So set ms = 0
  1411. since we may not have an exact timeout.
  1412. */
  1413. if (res == 0)
  1414. *ms = 0;
  1415. }
  1416. if (havewhen)
  1417. time(&now);
  1418. spoint = 0;
  1419. for (x=0; x < n; x++) {
  1420. ast_clear_flag(c[x], AST_FLAG_BLOCKING);
  1421. if (havewhen && c[x]->whentohangup && (now > c[x]->whentohangup)) {
  1422. c[x]->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
  1423. if (!winner)
  1424. winner = c[x];
  1425. }
  1426. for (y=0; y < AST_MAX_FDS; y++) {
  1427. if (c[x]->fds[y] > -1) {
  1428. if ((res = ast_fdisset(pfds, c[x]->fds[y], max, &spoint))) {
  1429. if (res & POLLPRI)
  1430. ast_set_flag(c[x], AST_FLAG_EXCEPTION);
  1431. else
  1432. ast_clear_flag(c[x], AST_FLAG_EXCEPTION);
  1433. c[x]->fdno = y;
  1434. winner = c[x];
  1435. }
  1436. }
  1437. }
  1438. }
  1439. for (x=0; x < nfds; x++) {
  1440. if (fds[x] > -1) {
  1441. if ((res = ast_fdisset(pfds, fds[x], max, &spoint))) {
  1442. if (outfd)
  1443. *outfd = fds[x];
  1444. if (exception) {
  1445. if (res & POLLPRI)
  1446. *exception = -1;
  1447. else
  1448. *exception = 0;
  1449. }
  1450. winner = NULL;
  1451. }
  1452. }
  1453. }
  1454. if (*ms > 0) {
  1455. *ms -= ast_tvdiff_ms(ast_tvnow(), start);
  1456. if (*ms < 0)
  1457. *ms = 0;
  1458. }
  1459. return winner;
  1460. }
  1461. struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
  1462. {
  1463. return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
  1464. }
  1465. int ast_waitfor(struct ast_channel *c, int ms)
  1466. {
  1467. struct ast_channel *chan;
  1468. int oldms = ms;
  1469. chan = ast_waitfor_n(&c, 1, &ms);
  1470. if (ms < 0) {
  1471. if (oldms < 0)
  1472. return 0;
  1473. else
  1474. return -1;
  1475. }
  1476. return ms;
  1477. }
  1478. int ast_waitfordigit(struct ast_channel *c, int ms)
  1479. {
  1480. /* XXX Should I be merged with waitfordigit_full XXX */
  1481. struct ast_frame *f;
  1482. int result = 0;
  1483. /* Stop if we're a zombie or need a soft hangup */
  1484. if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
  1485. return -1;
  1486. /* Wait for a digit, no more than ms milliseconds total. */
  1487. while(ms && !result) {
  1488. ms = ast_waitfor(c, ms);
  1489. if (ms < 0) /* Error */
  1490. result = -1;
  1491. else if (ms > 0) {
  1492. /* Read something */
  1493. f = ast_read(c);
  1494. if (f) {
  1495. if (f->frametype == AST_FRAME_DTMF)
  1496. result = f->subclass;
  1497. ast_frfree(f);
  1498. } else
  1499. result = -1;
  1500. }
  1501. }
  1502. return result;
  1503. }
  1504. int ast_settimeout(struct ast_channel *c, int samples, int (*func)(void *data), void *data)
  1505. {
  1506. int res = -1;
  1507. #ifdef ZAPTEL_OPTIMIZATIONS
  1508. if (c->timingfd > -1) {
  1509. if (!func) {
  1510. samples = 0;
  1511. data = 0;
  1512. }
  1513. ast_log(LOG_DEBUG, "Scheduling timer at %d sample intervals\n", samples);
  1514. res = ioctl(c->timingfd, ZT_TIMERCONFIG, &samples);
  1515. c->timingfunc = func;
  1516. c->timingdata = data;
  1517. }
  1518. #endif
  1519. return res;
  1520. }
  1521. int ast_waitfordigit_full(struct ast_channel *c, int ms, int audiofd, int cmdfd)
  1522. {
  1523. struct ast_frame *f;
  1524. struct ast_channel *rchan;
  1525. int outfd;
  1526. int res;
  1527. /* Stop if we're a zombie or need a soft hangup */
  1528. if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
  1529. return -1;
  1530. /* Wait for a digit, no more than ms milliseconds total. */
  1531. while(ms) {
  1532. errno = 0;
  1533. rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
  1534. if ((!rchan) && (outfd < 0) && (ms)) {
  1535. if (errno == 0 || errno == EINTR)
  1536. continue;
  1537. ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
  1538. return -1;
  1539. } else if (outfd > -1) {
  1540. /* The FD we were watching has something waiting */
  1541. return 1;
  1542. } else if (rchan) {
  1543. f = ast_read(c);
  1544. if(!f) {
  1545. return -1;
  1546. }
  1547. switch(f->frametype) {
  1548. case AST_FRAME_DTMF:
  1549. res = f->subclass;
  1550. ast_frfree(f);
  1551. return res;
  1552. case AST_FRAME_CONTROL:
  1553. switch(f->subclass) {
  1554. case AST_CONTROL_HANGUP:
  1555. ast_frfree(f);
  1556. return -1;
  1557. case AST_CONTROL_RINGING:
  1558. case AST_CONTROL_ANSWER:
  1559. /* Unimportant */
  1560. break;
  1561. default:
  1562. ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass);
  1563. }
  1564. case AST_FRAME_VOICE:
  1565. /* Write audio if appropriate */
  1566. if (audiofd > -1)
  1567. write(audiofd, f->data, f->datalen);
  1568. }
  1569. /* Ignore */
  1570. ast_frfree(f);
  1571. }
  1572. }
  1573. return 0; /* Time is up */
  1574. }
  1575. struct ast_frame *ast_read(struct ast_channel *chan)
  1576. {
  1577. struct ast_frame *f = NULL;
  1578. int blah;
  1579. int prestate;
  1580. #ifdef ZAPTEL_OPTIMIZATIONS
  1581. int (*func)(void *);
  1582. void *data;
  1583. int res;
  1584. #endif
  1585. static struct ast_frame null_frame = {
  1586. AST_FRAME_NULL,
  1587. };
  1588. ast_mutex_lock(&chan->lock);
  1589. if (chan->masq) {
  1590. if (ast_do_masquerade(chan)) {
  1591. ast_log(LOG_WARNING, "Failed to perform masquerade\n");
  1592. f = NULL;
  1593. } else
  1594. f = &null_frame;
  1595. ast_mutex_unlock(&chan->lock);
  1596. return f;
  1597. }
  1598. /* Stop if we're a zombie or need a soft hangup */
  1599. if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
  1600. if (chan->generator)
  1601. ast_deactivate_generator(chan);
  1602. ast_mutex_unlock(&chan->lock);
  1603. return NULL;
  1604. }
  1605. prestate = chan->_state;
  1606. if (!ast_test_flag(chan, AST_FLAG_DEFER_DTMF) && !ast_strlen_zero(chan->dtmfq)) {
  1607. /* We have DTMF that has been deferred. Return it now */
  1608. chan->dtmff.frametype = AST_FRAME_DTMF;
  1609. chan->dtmff.subclass = chan->dtmfq[0];
  1610. /* Drop first digit */
  1611. memmove(chan->dtmfq, chan->dtmfq + 1, sizeof(chan->dtmfq) - 1);
  1612. ast_mutex_unlock(&chan->lock);
  1613. return &chan->dtmff;
  1614. }
  1615. /* Read and ignore anything on the alertpipe, but read only
  1616. one sizeof(blah) per frame that we send from it */
  1617. if (chan->alertpipe[0] > -1) {
  1618. read(chan->alertpipe[0], &blah, sizeof(blah));
  1619. }
  1620. #ifdef ZAPTEL_OPTIMIZATIONS
  1621. if ((chan->timingfd > -1) && (chan->fdno == AST_MAX_FDS - 2) && ast_test_flag(chan, AST_FLAG_EXCEPTION)) {
  1622. ast_clear_flag(chan, AST_FLAG_EXCEPTION);
  1623. blah = -1;
  1624. /* IF we can't get event, assume it's an expired as-per the old interface */
  1625. res = ioctl(chan->timingfd, ZT_GETEVENT, &blah);
  1626. if (res)
  1627. blah = ZT_EVENT_TIMER_EXPIRED;
  1628. if (blah == ZT_EVENT_TIMER_PING) {
  1629. #if 0
  1630. ast_log(LOG_NOTICE, "Oooh, there's a PING!\n");
  1631. #endif
  1632. if (!chan->readq || !chan->readq->next) {
  1633. /* Acknowledge PONG unless we need it again */
  1634. #if 0
  1635. ast_log(LOG_NOTICE, "Sending a PONG!\n");
  1636. #endif
  1637. if (ioctl(chan->timingfd, ZT_TIMERPONG, &blah)) {
  1638. ast_log(LOG_WARNING, "Failed to pong timer on '%s': %s\n", chan->name, strerror(errno));
  1639. }
  1640. }
  1641. } else if (blah == ZT_EVENT_TIMER_EXPIRED) {
  1642. ioctl(chan->timingfd, ZT_TIMERACK, &blah);
  1643. func = chan->timingfunc;
  1644. data = chan->timingdata;
  1645. ast_mutex_unlock(&chan->lock);
  1646. if (func) {
  1647. #if 0
  1648. ast_log(LOG_DEBUG, "Calling private function\n");
  1649. #endif
  1650. func(data);
  1651. } else {
  1652. blah = 0;
  1653. ast_mutex_lock(&chan->lock);
  1654. ioctl(chan->timingfd, ZT_TIMERCONFIG, &blah);
  1655. chan->timingdata = NULL;
  1656. ast_mutex_unlock(&chan->lock);
  1657. }
  1658. f = &null_frame;
  1659. return f;
  1660. } else
  1661. ast_log(LOG_NOTICE, "No/unknown event '%d' on timer for '%s'?\n", blah, chan->name);
  1662. }
  1663. #endif
  1664. /* Check for pending read queue */
  1665. if (chan->readq) {
  1666. f = chan->readq;
  1667. chan->readq = f->next;
  1668. /* Interpret hangup and return NULL */
  1669. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP)) {
  1670. ast_frfree(f);
  1671. f = NULL;
  1672. }
  1673. } else {
  1674. chan->blocker = pthread_self();
  1675. if (ast_test_flag(chan, AST_FLAG_EXCEPTION)) {
  1676. if (chan->tech->exception)
  1677. f = chan->tech->exception(chan);
  1678. else {
  1679. ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", chan->name);
  1680. f = &null_frame;
  1681. }
  1682. /* Clear the exception flag */
  1683. ast_clear_flag(chan, AST_FLAG_EXCEPTION);
  1684. } else {
  1685. if (chan->tech->read)
  1686. f = chan->tech->read(chan);
  1687. else
  1688. ast_log(LOG_WARNING, "No read routine on channel %s\n", chan->name);
  1689. }
  1690. }
  1691. if (f && (f->frametype == AST_FRAME_VOICE)) {
  1692. if (!(f->subclass & chan->nativeformats)) {
  1693. /* This frame can't be from the current native formats -- drop it on the
  1694. floor */
  1695. ast_log(LOG_NOTICE, "Dropping incompatible voice frame on %s of format %s since our native format has changed to %s\n", chan->name, ast_getformatname(f->subclass), ast_getformatname(chan->nativeformats));
  1696. ast_frfree(f);
  1697. f = &null_frame;
  1698. } else {
  1699. if (chan->spies)
  1700. queue_frame_to_spies(chan, f, SPY_READ);
  1701. if (chan->monitor && chan->monitor->read_stream ) {
  1702. #ifndef MONITOR_CONSTANT_DELAY
  1703. int jump = chan->outsmpl - chan->insmpl - 2 * f->samples;
  1704. if (jump >= 0) {
  1705. if (ast_seekstream(chan->monitor->read_stream, jump + f->samples, SEEK_FORCECUR) == -1)
  1706. ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
  1707. chan->insmpl += jump + 2 * f->samples;
  1708. } else
  1709. chan->insmpl+= f->samples;
  1710. #else
  1711. int jump = chan->outsmpl - chan->insmpl;
  1712. if (jump - MONITOR_DELAY >= 0) {
  1713. if (ast_seekstream(chan->monitor->read_stream, jump - f->samples, SEEK_FORCECUR) == -1)
  1714. ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
  1715. chan->insmpl += jump;
  1716. } else
  1717. chan->insmpl += f->samples;
  1718. #endif
  1719. if (ast_writestream(chan->monitor->read_stream, f) < 0)
  1720. ast_log(LOG_WARNING, "Failed to write data to channel monitor read stream\n");
  1721. }
  1722. if (chan->readtrans) {
  1723. f = ast_translate(chan->readtrans, f, 1);
  1724. if (!f)
  1725. f = &null_frame;
  1726. }
  1727. }
  1728. }
  1729. /* Make sure we always return NULL in the future */
  1730. if (!f) {
  1731. chan->_softhangup |= AST_SOFTHANGUP_DEV;
  1732. if (chan->generator)
  1733. ast_deactivate_generator(chan);
  1734. /* End the CDR if appropriate */
  1735. if (chan->cdr)
  1736. ast_cdr_end(chan->cdr);
  1737. } else if (ast_test_flag(chan, AST_FLAG_DEFER_DTMF) && f->frametype == AST_FRAME_DTMF) {
  1738. if (strlen(chan->dtmfq) < sizeof(chan->dtmfq) - 2)
  1739. chan->dtmfq[strlen(chan->dtmfq)] = f->subclass;
  1740. else
  1741. ast_log(LOG_WARNING, "Dropping deferred DTMF digits on %s\n", chan->name);
  1742. f = &null_frame;
  1743. } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_ANSWER)) {
  1744. if (prestate == AST_STATE_UP) {
  1745. ast_log(LOG_DEBUG, "Dropping duplicate answer!\n");
  1746. f = &null_frame;
  1747. }
  1748. /* Answer the CDR */
  1749. ast_setstate(chan, AST_STATE_UP);
  1750. ast_cdr_answer(chan->cdr);
  1751. }
  1752. /* Run any generator sitting on the line */
  1753. if (f && (f->frametype == AST_FRAME_VOICE) && chan->generatordata) {
  1754. /* Mask generator data temporarily and apply. If there is a timing function, it
  1755. will be calling the generator instead */
  1756. void *tmp;
  1757. int res;
  1758. int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
  1759. if (chan->timingfunc) {
  1760. ast_log(LOG_DEBUG, "Generator got voice, switching to phase locked mode\n");
  1761. ast_settimeout(chan, 0, NULL, NULL);
  1762. }
  1763. tmp = chan->generatordata;
  1764. chan->generatordata = NULL;
  1765. generate = chan->generator->generate;
  1766. res = generate(chan, tmp, f->datalen, f->samples);
  1767. chan->generatordata = tmp;
  1768. if (res) {
  1769. ast_log(LOG_DEBUG, "Auto-deactivating generator\n");
  1770. ast_deactivate_generator(chan);
  1771. }
  1772. } else if (f && (f->frametype == AST_FRAME_CNG)) {
  1773. if (chan->generator && !chan->timingfunc && (chan->timingfd > -1)) {
  1774. ast_log(LOG_DEBUG, "Generator got CNG, switching to zap timed mode\n");
  1775. ast_settimeout(chan, 160, generator_force, chan);
  1776. }
  1777. }
  1778. /* High bit prints debugging */
  1779. if (chan->fin & 0x80000000)
  1780. ast_frame_dump(chan->name, f, "<<");
  1781. if ((chan->fin & 0x7fffffff) == 0x7fffffff)
  1782. chan->fin &= 0x80000000;
  1783. else
  1784. chan->fin++;
  1785. ast_mutex_unlock(&chan->lock);
  1786. return f;
  1787. }
  1788. int ast_indicate(struct ast_channel *chan, int condition)
  1789. {
  1790. int res = -1;
  1791. /* Stop if we're a zombie or need a soft hangup */
  1792. if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan))
  1793. return -1;
  1794. ast_mutex_lock(&chan->lock);
  1795. if (chan->tech->indicate)
  1796. res = chan->tech->indicate(chan, condition);
  1797. ast_mutex_unlock(&chan->lock);
  1798. if (!chan->tech->indicate || res) {
  1799. /*
  1800. * Device does not support (that) indication, lets fake
  1801. * it by doing our own tone generation. (PM2002)
  1802. */
  1803. if (condition >= 0) {
  1804. const struct tone_zone_sound *ts = NULL;
  1805. switch (condition) {
  1806. case AST_CONTROL_RINGING:
  1807. ts = ast_get_indication_tone(chan->zone, "ring");
  1808. break;
  1809. case AST_CONTROL_BUSY:
  1810. ts = ast_get_indication_tone(chan->zone, "busy");
  1811. break;
  1812. case AST_CONTROL_CONGESTION:
  1813. ts = ast_get_indication_tone(chan->zone, "congestion");
  1814. break;
  1815. }
  1816. if (ts && ts->data[0]) {
  1817. ast_log(LOG_DEBUG, "Driver for channel '%s' does not support indication %d, emulating it\n", chan->name, condition);
  1818. ast_playtones_start(chan,0,ts->data, 1);
  1819. res = 0;
  1820. } else if (condition == AST_CONTROL_PROGRESS) {
  1821. /* ast_playtones_stop(chan); */
  1822. } else if (condition == AST_CONTROL_PROCEEDING) {
  1823. /* Do nothing, really */
  1824. } else if (condition == AST_CONTROL_HOLD) {
  1825. /* Do nothing.... */
  1826. } else if (condition == AST_CONTROL_UNHOLD) {
  1827. /* Do nothing.... */
  1828. } else if (condition == AST_CONTROL_VIDUPDATE) {
  1829. /* Do nothing.... */
  1830. } else {
  1831. /* not handled */
  1832. ast_log(LOG_WARNING, "Unable to handle indication %d for '%s'\n", condition, chan->name);
  1833. res = -1;
  1834. }
  1835. }
  1836. else ast_playtones_stop(chan);
  1837. }
  1838. return res;
  1839. }
  1840. int ast_recvchar(struct ast_channel *chan, int timeout)
  1841. {
  1842. int c;
  1843. char *buf = ast_recvtext(chan, timeout);
  1844. if (buf == NULL)
  1845. return -1; /* error or timeout */
  1846. c = *(unsigned char *)buf;
  1847. free(buf);
  1848. return c;
  1849. }
  1850. char *ast_recvtext(struct ast_channel *chan, int timeout)
  1851. {
  1852. int res, done = 0;
  1853. char *buf = NULL;
  1854. while (!done) {
  1855. struct ast_frame *f;
  1856. if (ast_check_hangup(chan))
  1857. break;
  1858. res = ast_waitfor(chan, timeout);
  1859. if (res <= 0) /* timeout or error */
  1860. break;
  1861. timeout = res; /* update timeout */
  1862. f = ast_read(chan);
  1863. if (f == NULL)
  1864. break; /* no frame */
  1865. if (f->frametype == AST_FRAME_CONTROL && f->subclass == AST_CONTROL_HANGUP)
  1866. done = 1; /* force a break */
  1867. else if (f->frametype == AST_FRAME_TEXT) { /* what we want */
  1868. buf = strndup((char *) f->data, f->datalen); /* dup and break */
  1869. done = 1;
  1870. }
  1871. ast_frfree(f);
  1872. }
  1873. return buf;
  1874. }
  1875. int ast_sendtext(struct ast_channel *chan, const char *text)
  1876. {
  1877. int res = 0;
  1878. /* Stop if we're a zombie or need a soft hangup */
  1879. if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan))
  1880. return -1;
  1881. CHECK_BLOCKING(chan);
  1882. if (chan->tech->send_text)
  1883. res = chan->tech->send_text(chan, text);
  1884. ast_clear_flag(chan, AST_FLAG_BLOCKING);
  1885. return res;
  1886. }
  1887. static int do_senddigit(struct ast_channel *chan, char digit)
  1888. {
  1889. int res = -1;
  1890. if (chan->tech->send_digit)
  1891. res = chan->tech->send_digit(chan, digit);
  1892. if (!chan->tech->send_digit || res) {
  1893. /*
  1894. * Device does not support DTMF tones, lets fake
  1895. * it by doing our own generation. (PM2002)
  1896. */
  1897. static const char* dtmf_tones[] = {
  1898. "!941+1336/100,!0/100", /* 0 */
  1899. "!697+1209/100,!0/100", /* 1 */
  1900. "!697+1336/100,!0/100", /* 2 */
  1901. "!697+1477/100,!0/100", /* 3 */
  1902. "!770+1209/100,!0/100", /* 4 */
  1903. "!770+1336/100,!0/100", /* 5 */
  1904. "!770+1477/100,!0/100", /* 6 */
  1905. "!852+1209/100,!0/100", /* 7 */
  1906. "!852+1336/100,!0/100", /* 8 */
  1907. "!852+1477/100,!0/100", /* 9 */
  1908. "!697+1633/100,!0/100", /* A */
  1909. "!770+1633/100,!0/100", /* B */
  1910. "!852+1633/100,!0/100", /* C */
  1911. "!941+1633/100,!0/100", /* D */
  1912. "!941+1209/100,!0/100", /* * */
  1913. "!941+1477/100,!0/100" }; /* # */
  1914. if (digit >= '0' && digit <='9')
  1915. ast_playtones_start(chan, 0, dtmf_tones[digit-'0'], 0);
  1916. else if (digit >= 'A' && digit <= 'D')
  1917. ast_playtones_start(chan, 0, dtmf_tones[digit-'A'+10], 0);
  1918. else if (digit == '*')
  1919. ast_playtones_start(chan, 0, dtmf_tones[14], 0);
  1920. else if (digit == '#')
  1921. ast_playtones_start(chan, 0, dtmf_tones[15], 0);
  1922. else {
  1923. /* not handled */
  1924. ast_log(LOG_DEBUG, "Unable to generate DTMF tone '%c' for '%s'\n", digit, chan->name);
  1925. }
  1926. }
  1927. return 0;
  1928. }
  1929. int ast_senddigit(struct ast_channel *chan, char digit)
  1930. {
  1931. return do_senddigit(chan, digit);
  1932. }
  1933. int ast_prod(struct ast_channel *chan)
  1934. {
  1935. struct ast_frame a = { AST_FRAME_VOICE };
  1936. char nothing[128];
  1937. /* Send an empty audio frame to get things moving */
  1938. if (chan->_state != AST_STATE_UP) {
  1939. ast_log(LOG_DEBUG, "Prodding channel '%s'\n", chan->name);
  1940. a.subclass = chan->rawwriteformat;
  1941. a.data = nothing + AST_FRIENDLY_OFFSET;
  1942. a.src = "ast_prod";
  1943. if (ast_write(chan, &a))
  1944. ast_log(LOG_WARNING, "Prodding channel '%s' failed\n", chan->name);
  1945. }
  1946. return 0;
  1947. }
  1948. int ast_write_video(struct ast_channel *chan, struct ast_frame *fr)
  1949. {
  1950. int res;
  1951. if (!chan->tech->write_video)
  1952. return 0;
  1953. res = ast_write(chan, fr);
  1954. if (!res)
  1955. res = 1;
  1956. return res;
  1957. }
  1958. int ast_write(struct ast_channel *chan, struct ast_frame *fr)
  1959. {
  1960. int res = -1;
  1961. struct ast_frame *f = NULL;
  1962. /* Stop if we're a zombie or need a soft hangup */
  1963. ast_mutex_lock(&chan->lock);
  1964. if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
  1965. ast_mutex_unlock(&chan->lock);
  1966. return -1;
  1967. }
  1968. /* Handle any pending masquerades */
  1969. if (chan->masq) {
  1970. if (ast_do_masquerade(chan)) {
  1971. ast_log(LOG_WARNING, "Failed to perform masquerade\n");
  1972. ast_mutex_unlock(&chan->lock);
  1973. return -1;
  1974. }
  1975. }
  1976. if (chan->masqr) {
  1977. ast_mutex_unlock(&chan->lock);
  1978. return 0;
  1979. }
  1980. if (chan->generatordata) {
  1981. if (ast_test_flag(chan, AST_FLAG_WRITE_INT))
  1982. ast_deactivate_generator(chan);
  1983. else {
  1984. ast_mutex_unlock(&chan->lock);
  1985. return 0;
  1986. }
  1987. }
  1988. /* High bit prints debugging */
  1989. if (chan->fout & 0x80000000)
  1990. ast_frame_dump(chan->name, fr, ">>");
  1991. CHECK_BLOCKING(chan);
  1992. switch(fr->frametype) {
  1993. case AST_FRAME_CONTROL:
  1994. /* XXX Interpret control frames XXX */
  1995. ast_log(LOG_WARNING, "Don't know how to handle control frames yet\n");
  1996. break;
  1997. case AST_FRAME_DTMF:
  1998. ast_clear_flag(chan, AST_FLAG_BLOCKING);
  1999. ast_mutex_unlock(&chan->lock);
  2000. res = do_senddigit(chan,fr->subclass);
  2001. ast_mutex_lock(&chan->lock);
  2002. CHECK_BLOCKING(chan);
  2003. break;
  2004. case AST_FRAME_TEXT:
  2005. if (chan->tech->send_text)
  2006. res = chan->tech->send_text(chan, (char *) fr->data);
  2007. else
  2008. res = 0;
  2009. break;
  2010. case AST_FRAME_HTML:
  2011. if (chan->tech->send_html)
  2012. res = chan->tech->send_html(chan, fr->subclass, (char *) fr->data, fr->datalen);
  2013. else
  2014. res = 0;
  2015. break;
  2016. case AST_FRAME_VIDEO:
  2017. /* XXX Handle translation of video codecs one day XXX */
  2018. if (chan->tech->write_video)
  2019. res = chan->tech->write_video(chan, fr);
  2020. else
  2021. res = 0;
  2022. break;
  2023. default:
  2024. if (chan->tech->write) {
  2025. f = (chan->writetrans) ? ast_translate(chan->writetrans, fr, 0) : fr;
  2026. if (f) {
  2027. if (f->frametype == AST_FRAME_VOICE && chan->spies)
  2028. queue_frame_to_spies(chan, f, SPY_WRITE);
  2029. if( chan->monitor && chan->monitor->write_stream &&
  2030. f && ( f->frametype == AST_FRAME_VOICE ) ) {
  2031. #ifndef MONITOR_CONSTANT_DELAY
  2032. int jump = chan->insmpl - chan->outsmpl - 2 * f->samples;
  2033. if (jump >= 0) {
  2034. if (ast_seekstream(chan->monitor->write_stream, jump + f->samples, SEEK_FORCECUR) == -1)
  2035. ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
  2036. chan->outsmpl += jump + 2 * f->samples;
  2037. } else
  2038. chan->outsmpl += f->samples;
  2039. #else
  2040. int jump = chan->insmpl - chan->outsmpl;
  2041. if (jump - MONITOR_DELAY >= 0) {
  2042. if (ast_seekstream(chan->monitor->write_stream, jump - f->samples, SEEK_FORCECUR) == -1)
  2043. ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
  2044. chan->outsmpl += jump;
  2045. } else
  2046. chan->outsmpl += f->samples;
  2047. #endif
  2048. if (ast_writestream(chan->monitor->write_stream, f) < 0)
  2049. ast_log(LOG_WARNING, "Failed to write data to channel monitor write stream\n");
  2050. }
  2051. res = chan->tech->write(chan, f);
  2052. } else
  2053. res = 0;
  2054. }
  2055. }
  2056. /* It's possible this is a translated frame */
  2057. if (f && f->frametype == AST_FRAME_DTMF) {
  2058. ast_log(LOG_DTMF, "%s : %c\n", chan->name, f->subclass);
  2059. } else if (fr->frametype == AST_FRAME_DTMF) {
  2060. ast_log(LOG_DTMF, "%s : %c\n", chan->name, fr->subclass);
  2061. }
  2062. if (f && (f != fr))
  2063. ast_frfree(f);
  2064. ast_clear_flag(chan, AST_FLAG_BLOCKING);
  2065. /* Consider a write failure to force a soft hangup */
  2066. if (res < 0)
  2067. chan->_softhangup |= AST_SOFTHANGUP_DEV;
  2068. else {
  2069. if ((chan->fout & 0x7fffffff) == 0x7fffffff)
  2070. chan->fout &= 0x80000000;
  2071. else
  2072. chan->fout++;
  2073. }
  2074. ast_mutex_unlock(&chan->lock);
  2075. return res;
  2076. }
  2077. static int set_format(struct ast_channel *chan, int fmt, int *rawformat, int *format,
  2078. struct ast_trans_pvt **trans, const int direction)
  2079. {
  2080. int native;
  2081. int res;
  2082. native = chan->nativeformats;
  2083. /* Find a translation path from the native format to one of the desired formats */
  2084. if (!direction)
  2085. /* reading */
  2086. res = ast_translator_best_choice(&fmt, &native);
  2087. else
  2088. /* writing */
  2089. res = ast_translator_best_choice(&native, &fmt);
  2090. if (res < 0) {
  2091. ast_log(LOG_WARNING, "Unable to find a codec translation path from %s to %s\n",
  2092. ast_getformatname(native), ast_getformatname(fmt));
  2093. return -1;
  2094. }
  2095. /* Now we have a good choice for both. */
  2096. ast_mutex_lock(&chan->lock);
  2097. *rawformat = native;
  2098. /* User perspective is fmt */
  2099. *format = fmt;
  2100. /* Free any read translation we have right now */
  2101. if (*trans)
  2102. ast_translator_free_path(*trans);
  2103. /* Build a translation path from the raw format to the desired format */
  2104. if (!direction)
  2105. /* reading */
  2106. *trans = ast_translator_build_path(*format, *rawformat);
  2107. else
  2108. /* writing */
  2109. *trans = ast_translator_build_path(*rawformat, *format);
  2110. ast_mutex_unlock(&chan->lock);
  2111. if (option_debug)
  2112. ast_log(LOG_DEBUG, "Set channel %s to %s format %s\n", chan->name,
  2113. direction ? "write" : "read", ast_getformatname(fmt));
  2114. return 0;
  2115. }
  2116. int ast_set_read_format(struct ast_channel *chan, int fmt)
  2117. {
  2118. return set_format(chan, fmt, &chan->rawreadformat, &chan->readformat,
  2119. &chan->readtrans, 0);
  2120. }
  2121. int ast_set_write_format(struct ast_channel *chan, int fmt)
  2122. {
  2123. return set_format(chan, fmt, &chan->rawwriteformat, &chan->writeformat,
  2124. &chan->writetrans, 1);
  2125. }
  2126. struct ast_channel *__ast_request_and_dial(const char *type, int format, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name, struct outgoing_helper *oh)
  2127. {
  2128. int state = 0;
  2129. int cause = 0;
  2130. struct ast_channel *chan;
  2131. struct ast_frame *f;
  2132. int res = 0;
  2133. chan = ast_request(type, format, data, &cause);
  2134. if (chan) {
  2135. if (oh) {
  2136. if (oh->vars)
  2137. ast_set_variables(chan, oh->vars);
  2138. if (oh->cid_num && *oh->cid_num && oh->cid_name && *oh->cid_name)
  2139. ast_set_callerid(chan, oh->cid_num, oh->cid_name, oh->cid_num);
  2140. if (oh->parent_channel)
  2141. ast_channel_inherit_variables(oh->parent_channel, chan);
  2142. }
  2143. ast_set_callerid(chan, cid_num, cid_name, cid_num);
  2144. if (!ast_call(chan, data, 0)) {
  2145. while(timeout && (chan->_state != AST_STATE_UP)) {
  2146. res = ast_waitfor(chan, timeout);
  2147. if (res < 0) {
  2148. /* Something not cool, or timed out */
  2149. break;
  2150. }
  2151. /* If done, break out */
  2152. if (!res)
  2153. break;
  2154. if (timeout > -1)
  2155. timeout = res;
  2156. f = ast_read(chan);
  2157. if (!f) {
  2158. state = AST_CONTROL_HANGUP;
  2159. res = 0;
  2160. break;
  2161. }
  2162. if (f->frametype == AST_FRAME_CONTROL) {
  2163. if (f->subclass == AST_CONTROL_RINGING)
  2164. state = AST_CONTROL_RINGING;
  2165. else if ((f->subclass == AST_CONTROL_BUSY) || (f->subclass == AST_CONTROL_CONGESTION)) {
  2166. state = f->subclass;
  2167. ast_frfree(f);
  2168. break;
  2169. } else if (f->subclass == AST_CONTROL_ANSWER) {
  2170. state = f->subclass;
  2171. ast_frfree(f);
  2172. break;
  2173. } else if (f->subclass == AST_CONTROL_PROGRESS) {
  2174. /* Ignore */
  2175. } else if (f->subclass == -1) {
  2176. /* Ignore -- just stopping indications */
  2177. } else {
  2178. ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass);
  2179. }
  2180. }
  2181. ast_frfree(f);
  2182. }
  2183. } else
  2184. ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
  2185. } else {
  2186. ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
  2187. switch(cause) {
  2188. case AST_CAUSE_BUSY:
  2189. state = AST_CONTROL_BUSY;
  2190. break;
  2191. case AST_CAUSE_CONGESTION:
  2192. state = AST_CONTROL_CONGESTION;
  2193. break;
  2194. }
  2195. }
  2196. if (chan) {
  2197. /* Final fixups */
  2198. if (oh) {
  2199. if (oh->context && *oh->context)
  2200. ast_copy_string(chan->context, oh->context, sizeof(chan->context));
  2201. if (oh->exten && *oh->exten)
  2202. ast_copy_string(chan->exten, oh->exten, sizeof(chan->exten));
  2203. if (oh->priority)
  2204. chan->priority = oh->priority;
  2205. }
  2206. if (chan->_state == AST_STATE_UP)
  2207. state = AST_CONTROL_ANSWER;
  2208. }
  2209. if (outstate)
  2210. *outstate = state;
  2211. if (chan && res <= 0) {
  2212. if (!chan->cdr) {
  2213. chan->cdr = ast_cdr_alloc();
  2214. if (chan->cdr)
  2215. ast_cdr_init(chan->cdr, chan);
  2216. }
  2217. if (chan->cdr) {
  2218. char tmp[256];
  2219. snprintf(tmp, 256, "%s/%s", type, (char *)data);
  2220. ast_cdr_setapp(chan->cdr,"Dial",tmp);
  2221. ast_cdr_update(chan);
  2222. ast_cdr_start(chan->cdr);
  2223. ast_cdr_end(chan->cdr);
  2224. /* If the cause wasn't handled properly */
  2225. if (ast_cdr_disposition(chan->cdr,chan->hangupcause))
  2226. ast_cdr_failed(chan->cdr);
  2227. } else
  2228. ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
  2229. ast_hangup(chan);
  2230. chan = NULL;
  2231. }
  2232. return chan;
  2233. }
  2234. struct ast_channel *ast_request_and_dial(const char *type, int format, void *data, int timeout, int *outstate, const char *cidnum, const char *cidname)
  2235. {
  2236. return __ast_request_and_dial(type, format, data, timeout, outstate, cidnum, cidname, NULL);
  2237. }
  2238. struct ast_channel *ast_request(const char *type, int format, void *data, int *cause)
  2239. {
  2240. struct chanlist *chan;
  2241. struct ast_channel *c;
  2242. int capabilities;
  2243. int fmt;
  2244. int res;
  2245. int foo;
  2246. if (!cause)
  2247. cause = &foo;
  2248. *cause = AST_CAUSE_NOTDEFINED;
  2249. if (ast_mutex_lock(&chlock)) {
  2250. ast_log(LOG_WARNING, "Unable to lock channel list\n");
  2251. return NULL;
  2252. }
  2253. for (chan = backends; chan; chan = chan->next) {
  2254. if (strcasecmp(type, chan->tech->type))
  2255. continue;
  2256. capabilities = chan->tech->capabilities;
  2257. fmt = format;
  2258. res = ast_translator_best_choice(&fmt, &capabilities);
  2259. if (res < 0) {
  2260. ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %d) to %d\n", type, chan->tech->capabilities, format);
  2261. ast_mutex_unlock(&chlock);
  2262. return NULL;
  2263. }
  2264. ast_mutex_unlock(&chlock);
  2265. if (!chan->tech->requester)
  2266. return NULL;
  2267. if (!(c = chan->tech->requester(type, capabilities, data, cause)))
  2268. return NULL;
  2269. if (c->_state == AST_STATE_DOWN) {
  2270. manager_event(EVENT_FLAG_CALL, "Newchannel",
  2271. "Channel: %s\r\n"
  2272. "State: %s\r\n"
  2273. "CallerID: %s\r\n"
  2274. "CallerIDName: %s\r\n"
  2275. "Uniqueid: %s\r\n",
  2276. c->name, ast_state2str(c->_state),
  2277. c->cid.cid_num ? c->cid.cid_num : "<unknown>",
  2278. c->cid.cid_name ? c->cid.cid_name : "<unknown>",
  2279. c->uniqueid);
  2280. }
  2281. return c;
  2282. }
  2283. ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
  2284. *cause = AST_CAUSE_NOSUCHDRIVER;
  2285. ast_mutex_unlock(&chlock);
  2286. return NULL;
  2287. }
  2288. int ast_call(struct ast_channel *chan, char *addr, int timeout)
  2289. {
  2290. /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
  2291. If the remote end does not answer within the timeout, then do NOT hang up, but
  2292. return anyway. */
  2293. int res = -1;
  2294. /* Stop if we're a zombie or need a soft hangup */
  2295. ast_mutex_lock(&chan->lock);
  2296. if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan))
  2297. if (chan->tech->call)
  2298. res = chan->tech->call(chan, addr, timeout);
  2299. ast_mutex_unlock(&chan->lock);
  2300. return res;
  2301. }
  2302. /*--- ast_transfer: Transfer a call to dest, if the channel supports transfer */
  2303. /* called by app_transfer or the manager interface */
  2304. int ast_transfer(struct ast_channel *chan, char *dest)
  2305. {
  2306. int res = -1;
  2307. /* Stop if we're a zombie or need a soft hangup */
  2308. ast_mutex_lock(&chan->lock);
  2309. if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan)) {
  2310. if (chan->tech->transfer) {
  2311. res = chan->tech->transfer(chan, dest);
  2312. if (!res)
  2313. res = 1;
  2314. } else
  2315. res = 0;
  2316. }
  2317. ast_mutex_unlock(&chan->lock);
  2318. return res;
  2319. }
  2320. int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
  2321. {
  2322. int pos=0;
  2323. int to = ftimeout;
  2324. int d;
  2325. /* XXX Merge with full version? XXX */
  2326. /* Stop if we're a zombie or need a soft hangup */
  2327. if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
  2328. return -1;
  2329. if (!len)
  2330. return -1;
  2331. do {
  2332. if (c->stream) {
  2333. d = ast_waitstream(c, AST_DIGIT_ANY);
  2334. ast_stopstream(c);
  2335. usleep(1000);
  2336. if (!d)
  2337. d = ast_waitfordigit(c, to);
  2338. } else {
  2339. d = ast_waitfordigit(c, to);
  2340. }
  2341. if (d < 0)
  2342. return -1;
  2343. if (d == 0) {
  2344. s[pos]='\0';
  2345. return 1;
  2346. }
  2347. if (!strchr(enders, d))
  2348. s[pos++] = d;
  2349. if (strchr(enders, d) || (pos >= len)) {
  2350. s[pos]='\0';
  2351. return 0;
  2352. }
  2353. to = timeout;
  2354. } while(1);
  2355. /* Never reached */
  2356. return 0;
  2357. }
  2358. int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
  2359. {
  2360. int pos=0;
  2361. int to = ftimeout;
  2362. int d;
  2363. /* Stop if we're a zombie or need a soft hangup */
  2364. if (ast_test_flag(c, AST_FLAG_ZOMBIE) || ast_check_hangup(c))
  2365. return -1;
  2366. if (!len)
  2367. return -1;
  2368. do {
  2369. if (c->stream) {
  2370. d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd);
  2371. ast_stopstream(c);
  2372. usleep(1000);
  2373. if (!d)
  2374. d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
  2375. } else {
  2376. d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
  2377. }
  2378. if (d < 0)
  2379. return -1;
  2380. if (d == 0) {
  2381. s[pos]='\0';
  2382. return 1;
  2383. }
  2384. if (d == 1) {
  2385. s[pos]='\0';
  2386. return 2;
  2387. }
  2388. if (!strchr(enders, d))
  2389. s[pos++] = d;
  2390. if (strchr(enders, d) || (pos >= len)) {
  2391. s[pos]='\0';
  2392. return 0;
  2393. }
  2394. to = timeout;
  2395. } while(1);
  2396. /* Never reached */
  2397. return 0;
  2398. }
  2399. int ast_channel_supports_html(struct ast_channel *chan)
  2400. {
  2401. if (chan->tech->send_html)
  2402. return 1;
  2403. return 0;
  2404. }
  2405. int ast_channel_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen)
  2406. {
  2407. if (chan->tech->send_html)
  2408. return chan->tech->send_html(chan, subclass, data, datalen);
  2409. return -1;
  2410. }
  2411. int ast_channel_sendurl(struct ast_channel *chan, const char *url)
  2412. {
  2413. if (chan->tech->send_html)
  2414. return chan->tech->send_html(chan, AST_HTML_URL, url, strlen(url) + 1);
  2415. return -1;
  2416. }
  2417. int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
  2418. {
  2419. int src;
  2420. int dst;
  2421. /* Set up translation from the chan to the peer */
  2422. src = chan->nativeformats;
  2423. dst = peer->nativeformats;
  2424. if (ast_translator_best_choice(&dst, &src) < 0) {
  2425. ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", chan->name, src, peer->name, dst);
  2426. return -1;
  2427. }
  2428. /* if the best path is not 'pass through', then
  2429. transcoding is needed; if desired, force transcode path
  2430. to use SLINEAR between channels */
  2431. if ((src != dst) && option_transcode_slin)
  2432. dst = AST_FORMAT_SLINEAR;
  2433. if (ast_set_read_format(chan, dst) < 0) {
  2434. ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", chan->name, dst);
  2435. return -1;
  2436. }
  2437. if (ast_set_write_format(peer, dst) < 0) {
  2438. ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", peer->name, dst);
  2439. return -1;
  2440. }
  2441. /* Set up translation from the peer to the chan */
  2442. src = peer->nativeformats;
  2443. dst = chan->nativeformats;
  2444. if (ast_translator_best_choice(&dst, &src) < 0) {
  2445. ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", peer->name, src, chan->name, dst);
  2446. return -1;
  2447. }
  2448. /* if the best path is not 'pass through', then
  2449. transcoding is needed; if desired, force transcode path
  2450. to use SLINEAR between channels */
  2451. if ((src != dst) && option_transcode_slin)
  2452. dst = AST_FORMAT_SLINEAR;
  2453. if (ast_set_read_format(peer, dst) < 0) {
  2454. ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", peer->name, dst);
  2455. return -1;
  2456. }
  2457. if (ast_set_write_format(chan, dst) < 0) {
  2458. ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", chan->name, dst);
  2459. return -1;
  2460. }
  2461. return 0;
  2462. }
  2463. int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clone)
  2464. {
  2465. struct ast_frame null = { AST_FRAME_NULL, };
  2466. int res = -1;
  2467. if (original == clone) {
  2468. ast_log(LOG_WARNING, "Can't masquerade channel '%s' into itself!\n", original->name);
  2469. return -1;
  2470. }
  2471. ast_mutex_lock(&original->lock);
  2472. while(ast_mutex_trylock(&clone->lock)) {
  2473. ast_mutex_unlock(&original->lock);
  2474. usleep(1);
  2475. ast_mutex_lock(&original->lock);
  2476. }
  2477. ast_log(LOG_DEBUG, "Planning to masquerade channel %s into the structure of %s\n",
  2478. clone->name, original->name);
  2479. if (original->masq) {
  2480. ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
  2481. original->masq->name, original->name);
  2482. } else if (clone->masqr) {
  2483. ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
  2484. clone->name, clone->masqr->name);
  2485. } else {
  2486. original->masq = clone;
  2487. clone->masqr = original;
  2488. ast_queue_frame(original, &null);
  2489. ast_queue_frame(clone, &null);
  2490. ast_log(LOG_DEBUG, "Done planning to masquerade channel %s into the structure of %s\n", clone->name, original->name);
  2491. res = 0;
  2492. }
  2493. ast_mutex_unlock(&clone->lock);
  2494. ast_mutex_unlock(&original->lock);
  2495. return res;
  2496. }
  2497. void ast_change_name(struct ast_channel *chan, char *newname)
  2498. {
  2499. char tmp[256];
  2500. ast_copy_string(tmp, chan->name, sizeof(tmp));
  2501. ast_copy_string(chan->name, newname, sizeof(chan->name));
  2502. manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", tmp, chan->name, chan->uniqueid);
  2503. }
  2504. void ast_channel_inherit_variables(const struct ast_channel *parent, struct ast_channel *child)
  2505. {
  2506. struct ast_var_t *current, *newvar;
  2507. char *varname;
  2508. AST_LIST_TRAVERSE(&parent->varshead, current, entries) {
  2509. int vartype = 0;
  2510. varname = ast_var_full_name(current);
  2511. if (!varname)
  2512. continue;
  2513. if (varname[0] == '_') {
  2514. vartype = 1;
  2515. if (varname[1] == '_')
  2516. vartype = 2;
  2517. }
  2518. switch (vartype) {
  2519. case 1:
  2520. newvar = ast_var_assign(&varname[1], ast_var_value(current));
  2521. if (newvar) {
  2522. AST_LIST_INSERT_TAIL(&child->varshead, newvar, entries);
  2523. if (option_debug)
  2524. ast_log(LOG_DEBUG, "Copying soft-transferable variable %s.\n", ast_var_name(newvar));
  2525. }
  2526. break;
  2527. case 2:
  2528. newvar = ast_var_assign(ast_var_full_name(current), ast_var_value(current));
  2529. if (newvar) {
  2530. AST_LIST_INSERT_TAIL(&child->varshead, newvar, entries);
  2531. if (option_debug)
  2532. ast_log(LOG_DEBUG, "Copying hard-transferable variable %s.\n", ast_var_name(newvar));
  2533. }
  2534. break;
  2535. default:
  2536. if (option_debug)
  2537. ast_log(LOG_DEBUG, "Not copying variable %s.\n", ast_var_name(current));
  2538. break;
  2539. }
  2540. }
  2541. }
  2542. /* Clone channel variables from 'clone' channel into 'original' channel
  2543. All variables except those related to app_groupcount are cloned
  2544. Variables are actually _removed_ from 'clone' channel, presumably
  2545. because it will subsequently be destroyed.
  2546. Assumes locks will be in place on both channels when called.
  2547. */
  2548. static void clone_variables(struct ast_channel *original, struct ast_channel *clone)
  2549. {
  2550. struct ast_var_t *varptr;
  2551. /* we need to remove all app_groupcount related variables from the original
  2552. channel before merging in the clone's variables; any groups assigned to the
  2553. original channel should be released, only those assigned to the clone
  2554. should remain
  2555. */
  2556. AST_LIST_TRAVERSE_SAFE_BEGIN(&original->varshead, varptr, entries) {
  2557. if (!strncmp(ast_var_name(varptr), GROUP_CATEGORY_PREFIX, strlen(GROUP_CATEGORY_PREFIX))) {
  2558. AST_LIST_REMOVE(&original->varshead, varptr, entries);
  2559. ast_var_delete(varptr);
  2560. }
  2561. }
  2562. AST_LIST_TRAVERSE_SAFE_END;
  2563. /* Append variables from clone channel into original channel */
  2564. /* XXX Is this always correct? We have to in order to keep MACROS working XXX */
  2565. if (AST_LIST_FIRST(&clone->varshead))
  2566. AST_LIST_INSERT_TAIL(&original->varshead, AST_LIST_FIRST(&clone->varshead), entries);
  2567. }
  2568. /*--- ast_do_masquerade: Masquerade a channel */
  2569. /* Assumes channel will be locked when called */
  2570. int ast_do_masquerade(struct ast_channel *original)
  2571. {
  2572. int x,i;
  2573. int res=0;
  2574. int origstate;
  2575. struct ast_frame *cur, *prev;
  2576. const struct ast_channel_tech *t;
  2577. void *t_pvt;
  2578. struct ast_callerid tmpcid;
  2579. struct ast_channel *clone = original->masq;
  2580. int rformat = original->readformat;
  2581. int wformat = original->writeformat;
  2582. char newn[100];
  2583. char orig[100];
  2584. char masqn[100];
  2585. char zombn[100];
  2586. if (option_debug > 3)
  2587. ast_log(LOG_DEBUG, "Actually Masquerading %s(%d) into the structure of %s(%d)\n",
  2588. clone->name, clone->_state, original->name, original->_state);
  2589. /* XXX This is a seriously wacked out operation. We're essentially putting the guts of
  2590. the clone channel into the original channel. Start by killing off the original
  2591. channel's backend. I'm not sure we're going to keep this function, because
  2592. while the features are nice, the cost is very high in terms of pure nastiness. XXX */
  2593. /* We need the clone's lock, too */
  2594. ast_mutex_lock(&clone->lock);
  2595. ast_log(LOG_DEBUG, "Got clone lock for masquerade on '%s' at %p\n", clone->name, &clone->lock);
  2596. /* Having remembered the original read/write formats, we turn off any translation on either
  2597. one */
  2598. free_translation(clone);
  2599. free_translation(original);
  2600. /* Unlink the masquerade */
  2601. original->masq = NULL;
  2602. clone->masqr = NULL;
  2603. /* Save the original name */
  2604. ast_copy_string(orig, original->name, sizeof(orig));
  2605. /* Save the new name */
  2606. ast_copy_string(newn, clone->name, sizeof(newn));
  2607. /* Create the masq name */
  2608. snprintf(masqn, sizeof(masqn), "%s<MASQ>", newn);
  2609. /* Copy the name from the clone channel */
  2610. ast_copy_string(original->name, newn, sizeof(original->name));
  2611. /* Mangle the name of the clone channel */
  2612. ast_copy_string(clone->name, masqn, sizeof(clone->name));
  2613. /* Notify any managers of the change, first the masq then the other */
  2614. manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", newn, masqn, clone->uniqueid);
  2615. manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", orig, newn, original->uniqueid);
  2616. /* Swap the technlogies */
  2617. t = original->tech;
  2618. original->tech = clone->tech;
  2619. clone->tech = t;
  2620. t_pvt = original->tech_pvt;
  2621. original->tech_pvt = clone->tech_pvt;
  2622. clone->tech_pvt = t_pvt;
  2623. /* Swap the readq's */
  2624. cur = original->readq;
  2625. original->readq = clone->readq;
  2626. clone->readq = cur;
  2627. /* Swap the alertpipes */
  2628. for (i = 0; i < 2; i++) {
  2629. x = original->alertpipe[i];
  2630. original->alertpipe[i] = clone->alertpipe[i];
  2631. clone->alertpipe[i] = x;
  2632. }
  2633. /* Swap the raw formats */
  2634. x = original->rawreadformat;
  2635. original->rawreadformat = clone->rawreadformat;
  2636. clone->rawreadformat = x;
  2637. x = original->rawwriteformat;
  2638. original->rawwriteformat = clone->rawwriteformat;
  2639. clone->rawwriteformat = x;
  2640. /* Save any pending frames on both sides. Start by counting
  2641. * how many we're going to need... */
  2642. prev = NULL;
  2643. cur = clone->readq;
  2644. x = 0;
  2645. while(cur) {
  2646. x++;
  2647. prev = cur;
  2648. cur = cur->next;
  2649. }
  2650. /* If we had any, prepend them to the ones already in the queue, and
  2651. * load up the alertpipe */
  2652. if (prev) {
  2653. prev->next = original->readq;
  2654. original->readq = clone->readq;
  2655. clone->readq = NULL;
  2656. if (original->alertpipe[1] > -1) {
  2657. for (i = 0; i < x; i++)
  2658. write(original->alertpipe[1], &x, sizeof(x));
  2659. }
  2660. }
  2661. clone->_softhangup = AST_SOFTHANGUP_DEV;
  2662. /* And of course, so does our current state. Note we need not
  2663. call ast_setstate since the event manager doesn't really consider
  2664. these separate. We do this early so that the clone has the proper
  2665. state of the original channel. */
  2666. origstate = original->_state;
  2667. original->_state = clone->_state;
  2668. clone->_state = origstate;
  2669. if (clone->tech->fixup){
  2670. res = clone->tech->fixup(original, clone);
  2671. if (res)
  2672. ast_log(LOG_WARNING, "Fixup failed on channel %s, strange things may happen.\n", clone->name);
  2673. }
  2674. /* Start by disconnecting the original's physical side */
  2675. if (clone->tech->hangup)
  2676. res = clone->tech->hangup(clone);
  2677. if (res) {
  2678. ast_log(LOG_WARNING, "Hangup failed! Strange things may happen!\n");
  2679. ast_mutex_unlock(&clone->lock);
  2680. return -1;
  2681. }
  2682. snprintf(zombn, sizeof(zombn), "%s<ZOMBIE>", orig);
  2683. /* Mangle the name of the clone channel */
  2684. ast_copy_string(clone->name, zombn, sizeof(clone->name));
  2685. manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", masqn, zombn, clone->uniqueid);
  2686. /* Update the type. */
  2687. original->type = clone->type;
  2688. t_pvt = original->monitor;
  2689. original->monitor = clone->monitor;
  2690. clone->monitor = t_pvt;
  2691. /* Keep the same language. */
  2692. ast_copy_string(original->language, clone->language, sizeof(original->language));
  2693. /* Copy the FD's */
  2694. for (x = 0; x < AST_MAX_FDS; x++) {
  2695. original->fds[x] = clone->fds[x];
  2696. }
  2697. clone_variables(original, clone);
  2698. AST_LIST_HEAD_INIT_NOLOCK(&clone->varshead);
  2699. /* Presense of ADSI capable CPE follows clone */
  2700. original->adsicpe = clone->adsicpe;
  2701. /* Bridge remains the same */
  2702. /* CDR fields remain the same */
  2703. /* XXX What about blocking, softhangup, blocker, and lock and blockproc? XXX */
  2704. /* Application and data remain the same */
  2705. /* Clone exception becomes real one, as with fdno */
  2706. ast_copy_flags(original, clone, AST_FLAG_EXCEPTION);
  2707. original->fdno = clone->fdno;
  2708. /* Schedule context remains the same */
  2709. /* Stream stuff stays the same */
  2710. /* Keep the original state. The fixup code will need to work with it most likely */
  2711. /* Just swap the whole structures, nevermind the allocations, they'll work themselves
  2712. out. */
  2713. tmpcid = original->cid;
  2714. original->cid = clone->cid;
  2715. clone->cid = tmpcid;
  2716. /* Restore original timing file descriptor */
  2717. original->fds[AST_MAX_FDS - 2] = original->timingfd;
  2718. /* Our native formats are different now */
  2719. original->nativeformats = clone->nativeformats;
  2720. /* Context, extension, priority, app data, jump table, remain the same */
  2721. /* pvt switches. pbx stays the same, as does next */
  2722. /* Set the write format */
  2723. ast_set_write_format(original, wformat);
  2724. /* Set the read format */
  2725. ast_set_read_format(original, rformat);
  2726. /* Copy the music class */
  2727. ast_copy_string(original->musicclass, clone->musicclass, sizeof(original->musicclass));
  2728. ast_log(LOG_DEBUG, "Putting channel %s in %d/%d formats\n", original->name, wformat, rformat);
  2729. /* Okay. Last thing is to let the channel driver know about all this mess, so he
  2730. can fix up everything as best as possible */
  2731. if (original->tech->fixup) {
  2732. res = original->tech->fixup(clone, original);
  2733. if (res) {
  2734. ast_log(LOG_WARNING, "Channel for type '%s' could not fixup channel %s\n",
  2735. original->type, original->name);
  2736. ast_mutex_unlock(&clone->lock);
  2737. return -1;
  2738. }
  2739. } else
  2740. ast_log(LOG_WARNING, "Channel type '%s' does not have a fixup routine (for %s)! Bad things may happen.\n",
  2741. original->type, original->name);
  2742. /* Now, at this point, the "clone" channel is totally F'd up. We mark it as
  2743. a zombie so nothing tries to touch it. If it's already been marked as a
  2744. zombie, then free it now (since it already is considered invalid). */
  2745. if (ast_test_flag(clone, AST_FLAG_ZOMBIE)) {
  2746. ast_log(LOG_DEBUG, "Destroying channel clone '%s'\n", clone->name);
  2747. ast_mutex_unlock(&clone->lock);
  2748. ast_channel_free(clone);
  2749. manager_event(EVENT_FLAG_CALL, "Hangup",
  2750. "Channel: %s\r\n"
  2751. "Uniqueid: %s\r\n"
  2752. "Cause: %d\r\n"
  2753. "Cause-txt: %s\r\n",
  2754. clone->name,
  2755. clone->uniqueid,
  2756. clone->hangupcause,
  2757. ast_cause2str(clone->hangupcause)
  2758. );
  2759. } else {
  2760. struct ast_frame null_frame = { AST_FRAME_NULL, };
  2761. ast_log(LOG_DEBUG, "Released clone lock on '%s'\n", clone->name);
  2762. ast_set_flag(clone, AST_FLAG_ZOMBIE);
  2763. ast_queue_frame(clone, &null_frame);
  2764. ast_mutex_unlock(&clone->lock);
  2765. }
  2766. /* Signal any blocker */
  2767. if (ast_test_flag(original, AST_FLAG_BLOCKING))
  2768. pthread_kill(original->blocker, SIGURG);
  2769. ast_log(LOG_DEBUG, "Done Masquerading %s (%d)\n", original->name, original->_state);
  2770. return 0;
  2771. }
  2772. void ast_set_callerid(struct ast_channel *chan, const char *callerid, const char *calleridname, const char *ani)
  2773. {
  2774. if (callerid) {
  2775. if (chan->cid.cid_num)
  2776. free(chan->cid.cid_num);
  2777. if (ast_strlen_zero(callerid))
  2778. chan->cid.cid_num = NULL;
  2779. else
  2780. chan->cid.cid_num = strdup(callerid);
  2781. }
  2782. if (calleridname) {
  2783. if (chan->cid.cid_name)
  2784. free(chan->cid.cid_name);
  2785. if (ast_strlen_zero(calleridname))
  2786. chan->cid.cid_name = NULL;
  2787. else
  2788. chan->cid.cid_name = strdup(calleridname);
  2789. }
  2790. if (ani) {
  2791. if (chan->cid.cid_ani)
  2792. free(chan->cid.cid_ani);
  2793. if (ast_strlen_zero(ani))
  2794. chan->cid.cid_ani = NULL;
  2795. else
  2796. chan->cid.cid_ani = strdup(ani);
  2797. }
  2798. if (chan->cdr)
  2799. ast_cdr_setcid(chan->cdr, chan);
  2800. manager_event(EVENT_FLAG_CALL, "Newcallerid",
  2801. "Channel: %s\r\n"
  2802. "CallerID: %s\r\n"
  2803. "CallerIDName: %s\r\n"
  2804. "Uniqueid: %s\r\n"
  2805. "CID-CallingPres: %d (%s)\r\n",
  2806. chan->name, chan->cid.cid_num ?
  2807. chan->cid.cid_num : "<Unknown>",
  2808. chan->cid.cid_name ?
  2809. chan->cid.cid_name : "<Unknown>",
  2810. chan->uniqueid,
  2811. chan->cid.cid_pres,
  2812. ast_describe_caller_presentation(chan->cid.cid_pres)
  2813. );
  2814. }
  2815. int ast_setstate(struct ast_channel *chan, int state)
  2816. {
  2817. int oldstate = chan->_state;
  2818. if (oldstate == state)
  2819. return 0;
  2820. chan->_state = state;
  2821. ast_device_state_changed_literal(chan->name);
  2822. manager_event(EVENT_FLAG_CALL,
  2823. (oldstate == AST_STATE_DOWN) ? "Newchannel" : "Newstate",
  2824. "Channel: %s\r\n"
  2825. "State: %s\r\n"
  2826. "CallerID: %s\r\n"
  2827. "CallerIDName: %s\r\n"
  2828. "Uniqueid: %s\r\n",
  2829. chan->name, ast_state2str(chan->_state),
  2830. chan->cid.cid_num ? chan->cid.cid_num : "<unknown>",
  2831. chan->cid.cid_name ? chan->cid.cid_name : "<unknown>",
  2832. chan->uniqueid);
  2833. return 0;
  2834. }
  2835. /*--- Find bridged channel */
  2836. struct ast_channel *ast_bridged_channel(struct ast_channel *chan)
  2837. {
  2838. struct ast_channel *bridged;
  2839. bridged = chan->_bridge;
  2840. if (bridged && bridged->tech->bridged_channel)
  2841. bridged = bridged->tech->bridged_channel(chan, bridged);
  2842. return bridged;
  2843. }
  2844. static void bridge_playfile(struct ast_channel *chan, struct ast_channel *peer, char *sound, int remain)
  2845. {
  2846. int res=0, min=0, sec=0,check=0;
  2847. check = ast_autoservice_start(peer);
  2848. if(check)
  2849. return;
  2850. if (remain > 0) {
  2851. if (remain / 60 > 1) {
  2852. min = remain / 60;
  2853. sec = remain % 60;
  2854. } else {
  2855. sec = remain;
  2856. }
  2857. }
  2858. if (!strcmp(sound,"timeleft")) { /* Queue support */
  2859. res = ast_streamfile(chan, "vm-youhave", chan->language);
  2860. res = ast_waitstream(chan, "");
  2861. if (min) {
  2862. res = ast_say_number(chan, min, AST_DIGIT_ANY, chan->language, (char *) NULL);
  2863. res = ast_streamfile(chan, "queue-minutes", chan->language);
  2864. res = ast_waitstream(chan, "");
  2865. }
  2866. if (sec) {
  2867. res = ast_say_number(chan, sec, AST_DIGIT_ANY, chan->language, (char *) NULL);
  2868. res = ast_streamfile(chan, "queue-seconds", chan->language);
  2869. res = ast_waitstream(chan, "");
  2870. }
  2871. } else {
  2872. res = ast_streamfile(chan, sound, chan->language);
  2873. res = ast_waitstream(chan, "");
  2874. }
  2875. check = ast_autoservice_stop(peer);
  2876. }
  2877. static enum ast_bridge_result ast_generic_bridge(struct ast_channel *c0, struct ast_channel *c1,
  2878. struct ast_bridge_config *config, struct ast_frame **fo, struct ast_channel **rc, int toms)
  2879. {
  2880. /* Copy voice back and forth between the two channels. */
  2881. struct ast_channel *cs[3];
  2882. struct ast_frame *f;
  2883. struct ast_channel *who = NULL;
  2884. enum ast_bridge_result res = AST_BRIDGE_COMPLETE;
  2885. int o0nativeformats;
  2886. int o1nativeformats;
  2887. int watch_c0_dtmf;
  2888. int watch_c1_dtmf;
  2889. void *pvt0, *pvt1;
  2890. cs[0] = c0;
  2891. cs[1] = c1;
  2892. pvt0 = c0->tech_pvt;
  2893. pvt1 = c1->tech_pvt;
  2894. o0nativeformats = c0->nativeformats;
  2895. o1nativeformats = c1->nativeformats;
  2896. watch_c0_dtmf = config->flags & AST_BRIDGE_DTMF_CHANNEL_0;
  2897. watch_c1_dtmf = config->flags & AST_BRIDGE_DTMF_CHANNEL_1;
  2898. for (;;) {
  2899. if ((c0->tech_pvt != pvt0) || (c1->tech_pvt != pvt1) ||
  2900. (o0nativeformats != c0->nativeformats) ||
  2901. (o1nativeformats != c1->nativeformats)) {
  2902. /* Check for Masquerade, codec changes, etc */
  2903. res = AST_BRIDGE_RETRY;
  2904. break;
  2905. }
  2906. who = ast_waitfor_n(cs, 2, &toms);
  2907. if (!who) {
  2908. if (!toms) {
  2909. res = AST_BRIDGE_RETRY;
  2910. break;
  2911. }
  2912. ast_log(LOG_DEBUG, "Nobody there, continuing...\n");
  2913. if (c0->_softhangup == AST_SOFTHANGUP_UNBRIDGE || c1->_softhangup == AST_SOFTHANGUP_UNBRIDGE) {
  2914. if (c0->_softhangup == AST_SOFTHANGUP_UNBRIDGE)
  2915. c0->_softhangup = 0;
  2916. if (c1->_softhangup == AST_SOFTHANGUP_UNBRIDGE)
  2917. c1->_softhangup = 0;
  2918. c0->_bridge = c1;
  2919. c1->_bridge = c0;
  2920. }
  2921. continue;
  2922. }
  2923. f = ast_read(who);
  2924. if (!f) {
  2925. *fo = NULL;
  2926. *rc = who;
  2927. res = AST_BRIDGE_COMPLETE;
  2928. ast_log(LOG_DEBUG, "Didn't get a frame from channel: %s\n",who->name);
  2929. break;
  2930. }
  2931. if ((f->frametype == AST_FRAME_CONTROL) && !(config->flags & AST_BRIDGE_IGNORE_SIGS)) {
  2932. if ((f->subclass == AST_CONTROL_HOLD) || (f->subclass == AST_CONTROL_UNHOLD) ||
  2933. (f->subclass == AST_CONTROL_VIDUPDATE)) {
  2934. ast_indicate(who == c0 ? c1 : c0, f->subclass);
  2935. } else {
  2936. *fo = f;
  2937. *rc = who;
  2938. res = AST_BRIDGE_COMPLETE;
  2939. ast_log(LOG_DEBUG, "Got a FRAME_CONTROL (%d) frame on channel %s\n", f->subclass, who->name);
  2940. break;
  2941. }
  2942. }
  2943. if ((f->frametype == AST_FRAME_VOICE) ||
  2944. (f->frametype == AST_FRAME_DTMF) ||
  2945. (f->frametype == AST_FRAME_VIDEO) ||
  2946. (f->frametype == AST_FRAME_IMAGE) ||
  2947. (f->frametype == AST_FRAME_HTML) ||
  2948. (f->frametype == AST_FRAME_TEXT)) {
  2949. if (f->frametype == AST_FRAME_DTMF) {
  2950. if (((who == c0) && watch_c0_dtmf) ||
  2951. ((who == c1) && watch_c1_dtmf)) {
  2952. *rc = who;
  2953. *fo = f;
  2954. res = AST_BRIDGE_COMPLETE;
  2955. ast_log(LOG_DEBUG, "Got DTMF on channel (%s)\n", who->name);
  2956. break;
  2957. } else {
  2958. goto tackygoto;
  2959. }
  2960. } else {
  2961. #if 0
  2962. ast_log(LOG_DEBUG, "Read from %s\n", who->name);
  2963. if (who == last)
  2964. ast_log(LOG_DEBUG, "Servicing channel %s twice in a row?\n", last->name);
  2965. last = who;
  2966. #endif
  2967. tackygoto:
  2968. ast_write((who == c0) ? c1 : c0, f);
  2969. }
  2970. }
  2971. ast_frfree(f);
  2972. /* Swap who gets priority */
  2973. cs[2] = cs[0];
  2974. cs[0] = cs[1];
  2975. cs[1] = cs[2];
  2976. }
  2977. return res;
  2978. }
  2979. /*--- ast_channel_bridge: Bridge two channels together */
  2980. enum ast_bridge_result ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1,
  2981. struct ast_bridge_config *config, struct ast_frame **fo, struct ast_channel **rc)
  2982. {
  2983. struct ast_channel *who = NULL;
  2984. enum ast_bridge_result res = AST_BRIDGE_COMPLETE;
  2985. int nativefailed=0;
  2986. int firstpass;
  2987. int o0nativeformats;
  2988. int o1nativeformats;
  2989. long time_left_ms=0;
  2990. struct timeval nexteventts = { 0, };
  2991. char caller_warning = 0;
  2992. char callee_warning = 0;
  2993. int to;
  2994. if (c0->_bridge) {
  2995. ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
  2996. c0->name, c0->_bridge->name);
  2997. return -1;
  2998. }
  2999. if (c1->_bridge) {
  3000. ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
  3001. c1->name, c1->_bridge->name);
  3002. return -1;
  3003. }
  3004. /* Stop if we're a zombie or need a soft hangup */
  3005. if (ast_test_flag(c0, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c0) ||
  3006. ast_test_flag(c1, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c1))
  3007. return -1;
  3008. *fo = NULL;
  3009. firstpass = config->firstpass;
  3010. config->firstpass = 0;
  3011. if (ast_tvzero(config->start_time))
  3012. config->start_time = ast_tvnow();
  3013. time_left_ms = config->timelimit;
  3014. caller_warning = ast_test_flag(&config->features_caller, AST_FEATURE_PLAY_WARNING);
  3015. callee_warning = ast_test_flag(&config->features_callee, AST_FEATURE_PLAY_WARNING);
  3016. if (config->start_sound && firstpass) {
  3017. if (caller_warning)
  3018. bridge_playfile(c0, c1, config->start_sound, time_left_ms / 1000);
  3019. if (callee_warning)
  3020. bridge_playfile(c1, c0, config->start_sound, time_left_ms / 1000);
  3021. }
  3022. /* Keep track of bridge */
  3023. c0->_bridge = c1;
  3024. c1->_bridge = c0;
  3025. manager_event(EVENT_FLAG_CALL, "Link",
  3026. "Channel1: %s\r\n"
  3027. "Channel2: %s\r\n"
  3028. "Uniqueid1: %s\r\n"
  3029. "Uniqueid2: %s\r\n"
  3030. "CallerID1: %s\r\n"
  3031. "CallerID2: %s\r\n",
  3032. c0->name, c1->name, c0->uniqueid, c1->uniqueid, c0->cid.cid_num, c1->cid.cid_num);
  3033. o0nativeformats = c0->nativeformats;
  3034. o1nativeformats = c1->nativeformats;
  3035. if (config->timelimit) {
  3036. nexteventts = ast_tvadd(config->start_time, ast_samp2tv(config->timelimit, 1000));
  3037. if (caller_warning || callee_warning)
  3038. nexteventts = ast_tvsub(nexteventts, ast_samp2tv(config->play_warning, 1000));
  3039. }
  3040. for (/* ever */;;) {
  3041. to = -1;
  3042. if (config->timelimit) {
  3043. struct timeval now;
  3044. now = ast_tvnow();
  3045. to = ast_tvdiff_ms(nexteventts, now);
  3046. if (to < 0)
  3047. to = 0;
  3048. time_left_ms = config->timelimit - ast_tvdiff_ms(now, config->start_time);
  3049. if (time_left_ms < to)
  3050. to = time_left_ms;
  3051. if (time_left_ms <= 0) {
  3052. if (caller_warning && config->end_sound)
  3053. bridge_playfile(c0, c1, config->end_sound, 0);
  3054. if (callee_warning && config->end_sound)
  3055. bridge_playfile(c1, c0, config->end_sound, 0);
  3056. *fo = NULL;
  3057. if (who)
  3058. *rc = who;
  3059. res = 0;
  3060. break;
  3061. }
  3062. if (!to) {
  3063. if (time_left_ms >= 5000) {
  3064. if (caller_warning && config->warning_sound && config->play_warning)
  3065. bridge_playfile(c0, c1, config->warning_sound, time_left_ms / 1000);
  3066. if (callee_warning && config->warning_sound && config->play_warning)
  3067. bridge_playfile(c1, c0, config->warning_sound, time_left_ms / 1000);
  3068. }
  3069. if (config->warning_freq) {
  3070. nexteventts = ast_tvadd(nexteventts, ast_samp2tv(config->warning_freq, 1000));
  3071. } else
  3072. nexteventts = ast_tvadd(config->start_time, ast_samp2tv(config->timelimit, 1000));
  3073. }
  3074. }
  3075. if (c0->_softhangup == AST_SOFTHANGUP_UNBRIDGE || c1->_softhangup == AST_SOFTHANGUP_UNBRIDGE) {
  3076. if (c0->_softhangup == AST_SOFTHANGUP_UNBRIDGE)
  3077. c0->_softhangup = 0;
  3078. if (c1->_softhangup == AST_SOFTHANGUP_UNBRIDGE)
  3079. c1->_softhangup = 0;
  3080. c0->_bridge = c1;
  3081. c1->_bridge = c0;
  3082. ast_log(LOG_DEBUG, "Unbridge signal received. Ending native bridge.\n");
  3083. continue;
  3084. }
  3085. /* Stop if we're a zombie or need a soft hangup */
  3086. if (ast_test_flag(c0, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c0) ||
  3087. ast_test_flag(c1, AST_FLAG_ZOMBIE) || ast_check_hangup_locked(c1)) {
  3088. *fo = NULL;
  3089. if (who)
  3090. *rc = who;
  3091. res = 0;
  3092. ast_log(LOG_DEBUG, "Bridge stops because we're zombie or need a soft hangup: c0=%s, c1=%s, flags: %s,%s,%s,%s\n",
  3093. c0->name, c1->name,
  3094. ast_test_flag(c0, AST_FLAG_ZOMBIE) ? "Yes" : "No",
  3095. ast_check_hangup(c0) ? "Yes" : "No",
  3096. ast_test_flag(c1, AST_FLAG_ZOMBIE) ? "Yes" : "No",
  3097. ast_check_hangup(c1) ? "Yes" : "No");
  3098. break;
  3099. }
  3100. if (c0->tech->bridge &&
  3101. (config->timelimit == 0) &&
  3102. (c0->tech->bridge == c1->tech->bridge) &&
  3103. !nativefailed && !c0->monitor && !c1->monitor &&
  3104. !c0->spies && !c1->spies) {
  3105. /* Looks like they share a bridge method and nothing else is in the way */
  3106. if (option_verbose > 2)
  3107. ast_verbose(VERBOSE_PREFIX_3 "Attempting native bridge of %s and %s\n", c0->name, c1->name);
  3108. ast_set_flag(c0, AST_FLAG_NBRIDGE);
  3109. ast_set_flag(c1, AST_FLAG_NBRIDGE);
  3110. if ((res = c0->tech->bridge(c0, c1, config->flags, fo, rc, to)) == AST_BRIDGE_COMPLETE) {
  3111. manager_event(EVENT_FLAG_CALL, "Unlink",
  3112. "Channel1: %s\r\n"
  3113. "Channel2: %s\r\n"
  3114. "Uniqueid1: %s\r\n"
  3115. "Uniqueid2: %s\r\n"
  3116. "CallerID1: %s\r\n"
  3117. "CallerID2: %s\r\n",
  3118. c0->name, c1->name, c0->uniqueid, c1->uniqueid, c0->cid.cid_num, c1->cid.cid_num);
  3119. ast_log(LOG_DEBUG, "Returning from native bridge, channels: %s, %s\n", c0->name, c1->name);
  3120. ast_clear_flag(c0, AST_FLAG_NBRIDGE);
  3121. ast_clear_flag(c1, AST_FLAG_NBRIDGE);
  3122. if (c0->_softhangup == AST_SOFTHANGUP_UNBRIDGE || c1->_softhangup == AST_SOFTHANGUP_UNBRIDGE)
  3123. continue;
  3124. c0->_bridge = NULL;
  3125. c1->_bridge = NULL;
  3126. return res;
  3127. } else {
  3128. ast_clear_flag(c0, AST_FLAG_NBRIDGE);
  3129. ast_clear_flag(c1, AST_FLAG_NBRIDGE);
  3130. }
  3131. switch (res) {
  3132. case AST_BRIDGE_RETRY:
  3133. continue;
  3134. default:
  3135. ast_log(LOG_WARNING, "Private bridge between %s and %s failed\n", c0->name, c1->name);
  3136. /* fallthrough */
  3137. case AST_BRIDGE_FAILED_NOWARN:
  3138. nativefailed++;
  3139. break;
  3140. }
  3141. }
  3142. if (((c0->writeformat != c1->readformat) || (c0->readformat != c1->writeformat) ||
  3143. (c0->nativeformats != o0nativeformats) || (c1->nativeformats != o1nativeformats)) &&
  3144. !(c0->generator || c1->generator)) {
  3145. if (ast_channel_make_compatible(c0, c1)) {
  3146. ast_log(LOG_WARNING, "Can't make %s and %s compatible\n", c0->name, c1->name);
  3147. manager_event(EVENT_FLAG_CALL, "Unlink",
  3148. "Channel1: %s\r\n"
  3149. "Channel2: %s\r\n"
  3150. "Uniqueid1: %s\r\n"
  3151. "Uniqueid2: %s\r\n"
  3152. "CallerID1: %s\r\n"
  3153. "CallerID2: %s\r\n",
  3154. c0->name, c1->name, c0->uniqueid, c1->uniqueid, c0->cid.cid_num, c1->cid.cid_num);
  3155. return AST_BRIDGE_FAILED;
  3156. }
  3157. o0nativeformats = c0->nativeformats;
  3158. o1nativeformats = c1->nativeformats;
  3159. }
  3160. res = ast_generic_bridge(c0, c1, config, fo, rc, to);
  3161. if (res != AST_BRIDGE_RETRY)
  3162. break;
  3163. }
  3164. c0->_bridge = NULL;
  3165. c1->_bridge = NULL;
  3166. manager_event(EVENT_FLAG_CALL, "Unlink",
  3167. "Channel1: %s\r\n"
  3168. "Channel2: %s\r\n"
  3169. "Uniqueid1: %s\r\n"
  3170. "Uniqueid2: %s\r\n"
  3171. "CallerID1: %s\r\n"
  3172. "CallerID2: %s\r\n",
  3173. c0->name, c1->name, c0->uniqueid, c1->uniqueid, c0->cid.cid_num, c1->cid.cid_num);
  3174. ast_log(LOG_DEBUG, "Bridge stops bridging channels %s and %s\n", c0->name, c1->name);
  3175. return res;
  3176. }
  3177. /*--- ast_channel_setoption: Sets an option on a channel */
  3178. int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
  3179. {
  3180. int res;
  3181. if (chan->tech->setoption) {
  3182. res = chan->tech->setoption(chan, option, data, datalen);
  3183. if (res < 0)
  3184. return res;
  3185. } else {
  3186. errno = ENOSYS;
  3187. return -1;
  3188. }
  3189. if (block) {
  3190. /* XXX Implement blocking -- just wait for our option frame reply, discarding
  3191. intermediate packets. XXX */
  3192. ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
  3193. return -1;
  3194. }
  3195. return 0;
  3196. }
  3197. struct tonepair_def {
  3198. int freq1;
  3199. int freq2;
  3200. int duration;
  3201. int vol;
  3202. };
  3203. struct tonepair_state {
  3204. float freq1;
  3205. float freq2;
  3206. float vol;
  3207. int duration;
  3208. int pos;
  3209. int origwfmt;
  3210. struct ast_frame f;
  3211. unsigned char offset[AST_FRIENDLY_OFFSET];
  3212. short data[4000];
  3213. };
  3214. static void tonepair_release(struct ast_channel *chan, void *params)
  3215. {
  3216. struct tonepair_state *ts = params;
  3217. if (chan) {
  3218. ast_set_write_format(chan, ts->origwfmt);
  3219. }
  3220. free(ts);
  3221. }
  3222. static void *tonepair_alloc(struct ast_channel *chan, void *params)
  3223. {
  3224. struct tonepair_state *ts;
  3225. struct tonepair_def *td = params;
  3226. ts = malloc(sizeof(struct tonepair_state));
  3227. if (!ts)
  3228. return NULL;
  3229. memset(ts, 0, sizeof(struct tonepair_state));
  3230. ts->origwfmt = chan->writeformat;
  3231. if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
  3232. ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
  3233. tonepair_release(NULL, ts);
  3234. ts = NULL;
  3235. } else {
  3236. ts->freq1 = td->freq1;
  3237. ts->freq2 = td->freq2;
  3238. ts->duration = td->duration;
  3239. ts->vol = td->vol;
  3240. }
  3241. /* Let interrupts interrupt :) */
  3242. ast_set_flag(chan, AST_FLAG_WRITE_INT);
  3243. return ts;
  3244. }
  3245. static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
  3246. {
  3247. struct tonepair_state *ts = data;
  3248. int x;
  3249. /* we need to prepare a frame with 16 * timelen samples as we're
  3250. * generating SLIN audio
  3251. */
  3252. len = samples * 2;
  3253. if (len > sizeof(ts->data) / 2 - 1) {
  3254. ast_log(LOG_WARNING, "Can't generate that much data!\n");
  3255. return -1;
  3256. }
  3257. memset(&ts->f, 0, sizeof(ts->f));
  3258. for (x = 0; x < (len / 2); x++) {
  3259. ts->data[x] = ts->vol * (
  3260. sin((ts->freq1 * 2.0 * M_PI / 8000.0) * (ts->pos + x)) +
  3261. sin((ts->freq2 * 2.0 * M_PI / 8000.0) * (ts->pos + x))
  3262. );
  3263. }
  3264. ts->f.frametype = AST_FRAME_VOICE;
  3265. ts->f.subclass = AST_FORMAT_SLINEAR;
  3266. ts->f.datalen = len;
  3267. ts->f.samples = samples;
  3268. ts->f.offset = AST_FRIENDLY_OFFSET;
  3269. ts->f.data = ts->data;
  3270. ast_write(chan, &ts->f);
  3271. ts->pos += x;
  3272. if (ts->duration > 0) {
  3273. if (ts->pos >= ts->duration * 8)
  3274. return -1;
  3275. }
  3276. return 0;
  3277. }
  3278. static struct ast_generator tonepair = {
  3279. alloc: tonepair_alloc,
  3280. release: tonepair_release,
  3281. generate: tonepair_generator,
  3282. };
  3283. int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
  3284. {
  3285. struct tonepair_def d = { 0, };
  3286. d.freq1 = freq1;
  3287. d.freq2 = freq2;
  3288. d.duration = duration;
  3289. if (vol < 1)
  3290. d.vol = 8192;
  3291. else
  3292. d.vol = vol;
  3293. if (ast_activate_generator(chan, &tonepair, &d))
  3294. return -1;
  3295. return 0;
  3296. }
  3297. void ast_tonepair_stop(struct ast_channel *chan)
  3298. {
  3299. ast_deactivate_generator(chan);
  3300. }
  3301. int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
  3302. {
  3303. struct ast_frame *f;
  3304. int res;
  3305. if ((res = ast_tonepair_start(chan, freq1, freq2, duration, vol)))
  3306. return res;
  3307. /* Give us some wiggle room */
  3308. while(chan->generatordata && (ast_waitfor(chan, 100) >= 0)) {
  3309. f = ast_read(chan);
  3310. if (f)
  3311. ast_frfree(f);
  3312. else
  3313. return -1;
  3314. }
  3315. return 0;
  3316. }
  3317. ast_group_t ast_get_group(char *s)
  3318. {
  3319. char *copy;
  3320. char *piece;
  3321. char *c=NULL;
  3322. int start=0, finish=0, x;
  3323. ast_group_t group = 0;
  3324. copy = ast_strdupa(s);
  3325. if (!copy) {
  3326. ast_log(LOG_ERROR, "Out of memory\n");
  3327. return 0;
  3328. }
  3329. c = copy;
  3330. while((piece = strsep(&c, ","))) {
  3331. if (sscanf(piece, "%d-%d", &start, &finish) == 2) {
  3332. /* Range */
  3333. } else if (sscanf(piece, "%d", &start)) {
  3334. /* Just one */
  3335. finish = start;
  3336. } else {
  3337. ast_log(LOG_ERROR, "Syntax error parsing group configuration '%s' at '%s'. Ignoring.\n", s, piece);
  3338. continue;
  3339. }
  3340. for (x = start; x <= finish; x++) {
  3341. if ((x > 63) || (x < 0)) {
  3342. ast_log(LOG_WARNING, "Ignoring invalid group %d (maximum group is 63)\n", x);
  3343. } else
  3344. group |= ((ast_group_t) 1 << x);
  3345. }
  3346. }
  3347. return group;
  3348. }
  3349. static int (*ast_moh_start_ptr)(struct ast_channel *, char *) = NULL;
  3350. static void (*ast_moh_stop_ptr)(struct ast_channel *) = NULL;
  3351. static void (*ast_moh_cleanup_ptr)(struct ast_channel *) = NULL;
  3352. void ast_install_music_functions(int (*start_ptr)(struct ast_channel *, char *),
  3353. void (*stop_ptr)(struct ast_channel *),
  3354. void (*cleanup_ptr)(struct ast_channel *)
  3355. )
  3356. {
  3357. ast_moh_start_ptr = start_ptr;
  3358. ast_moh_stop_ptr = stop_ptr;
  3359. ast_moh_cleanup_ptr = cleanup_ptr;
  3360. }
  3361. void ast_uninstall_music_functions(void)
  3362. {
  3363. ast_moh_start_ptr = NULL;
  3364. ast_moh_stop_ptr = NULL;
  3365. ast_moh_cleanup_ptr = NULL;
  3366. }
  3367. /*! Turn on music on hold on a given channel */
  3368. int ast_moh_start(struct ast_channel *chan, char *mclass)
  3369. {
  3370. if (ast_moh_start_ptr)
  3371. return ast_moh_start_ptr(chan, mclass);
  3372. if (option_verbose > 2)
  3373. ast_verbose(VERBOSE_PREFIX_3 "Music class %s requested but no musiconhold loaded.\n", mclass ? mclass : "default");
  3374. return 0;
  3375. }
  3376. /*! Turn off music on hold on a given channel */
  3377. void ast_moh_stop(struct ast_channel *chan)
  3378. {
  3379. if(ast_moh_stop_ptr)
  3380. ast_moh_stop_ptr(chan);
  3381. }
  3382. void ast_moh_cleanup(struct ast_channel *chan)
  3383. {
  3384. if(ast_moh_cleanup_ptr)
  3385. ast_moh_cleanup_ptr(chan);
  3386. }
  3387. void ast_channels_init(void)
  3388. {
  3389. ast_cli_register(&cli_show_channeltypes);
  3390. }
  3391. /*--- ast_print_group: Print call group and pickup group ---*/
  3392. char *ast_print_group(char *buf, int buflen, ast_group_t group)
  3393. {
  3394. unsigned int i;
  3395. int first=1;
  3396. char num[3];
  3397. buf[0] = '\0';
  3398. if (!group) /* Return empty string if no group */
  3399. return(buf);
  3400. for (i=0; i<=63; i++) { /* Max group is 63 */
  3401. if (group & ((ast_group_t) 1 << i)) {
  3402. if (!first) {
  3403. strncat(buf, ", ", buflen);
  3404. } else {
  3405. first=0;
  3406. }
  3407. snprintf(num, sizeof(num), "%u", i);
  3408. strncat(buf, num, buflen);
  3409. }
  3410. }
  3411. return(buf);
  3412. }
  3413. void ast_set_variables(struct ast_channel *chan, struct ast_variable *vars)
  3414. {
  3415. struct ast_variable *cur;
  3416. for (cur = vars; cur; cur = cur->next)
  3417. pbx_builtin_setvar_helper(chan, cur->name, cur->value);
  3418. }
  3419. static void copy_data_from_queue(struct ast_channel_spy_queue *queue, short *buf, unsigned int samples)
  3420. {
  3421. struct ast_frame *f;
  3422. int tocopy;
  3423. int bytestocopy;
  3424. while (samples) {
  3425. f = queue->head;
  3426. if (!f) {
  3427. ast_log(LOG_ERROR, "Ran out of frames before buffer filled!\n");
  3428. break;
  3429. }
  3430. tocopy = (f->samples > samples) ? samples : f->samples;
  3431. bytestocopy = ast_codec_get_len(queue->format, samples);
  3432. memcpy(buf, f->data, bytestocopy);
  3433. samples -= tocopy;
  3434. buf += tocopy;
  3435. f->samples -= tocopy;
  3436. f->data += bytestocopy;
  3437. f->datalen -= bytestocopy;
  3438. f->offset += bytestocopy;
  3439. queue->samples -= tocopy;
  3440. if (!f->samples) {
  3441. queue->head = f->next;
  3442. ast_frfree(f);
  3443. }
  3444. }
  3445. }
  3446. struct ast_frame *ast_channel_spy_read_frame(struct ast_channel_spy *spy, unsigned int samples)
  3447. {
  3448. struct ast_frame *result;
  3449. /* buffers are allocated to hold SLINEAR, which is the largest format */
  3450. short read_buf[samples];
  3451. short write_buf[samples];
  3452. struct ast_frame *read_frame;
  3453. struct ast_frame *write_frame;
  3454. int need_dup;
  3455. struct ast_frame stack_read_frame = { .frametype = AST_FRAME_VOICE,
  3456. .subclass = spy->read_queue.format,
  3457. .data = read_buf,
  3458. .samples = samples,
  3459. .datalen = ast_codec_get_len(spy->read_queue.format, samples),
  3460. };
  3461. struct ast_frame stack_write_frame = { .frametype = AST_FRAME_VOICE,
  3462. .subclass = spy->write_queue.format,
  3463. .data = write_buf,
  3464. .samples = samples,
  3465. .datalen = ast_codec_get_len(spy->write_queue.format, samples),
  3466. };
  3467. /* if a flush has been requested, dump everything in whichever queue is larger */
  3468. if (ast_test_flag(spy, CHANSPY_TRIGGER_FLUSH)) {
  3469. if (spy->read_queue.samples > spy->write_queue.samples) {
  3470. if (ast_test_flag(spy, CHANSPY_READ_VOLADJUST)) {
  3471. for (result = spy->read_queue.head; result; result = result->next)
  3472. ast_frame_adjust_volume(result, spy->read_vol_adjustment);
  3473. }
  3474. result = spy->read_queue.head;
  3475. spy->read_queue.head = NULL;
  3476. spy->read_queue.samples = 0;
  3477. ast_clear_flag(spy, CHANSPY_TRIGGER_FLUSH);
  3478. return result;
  3479. } else {
  3480. if (ast_test_flag(spy, CHANSPY_WRITE_VOLADJUST)) {
  3481. for (result = spy->write_queue.head; result; result = result->next)
  3482. ast_frame_adjust_volume(result, spy->write_vol_adjustment);
  3483. }
  3484. result = spy->write_queue.head;
  3485. spy->write_queue.head = NULL;
  3486. spy->write_queue.samples = 0;
  3487. ast_clear_flag(spy, CHANSPY_TRIGGER_FLUSH);
  3488. return result;
  3489. }
  3490. }
  3491. if ((spy->read_queue.samples < samples) || (spy->write_queue.samples < samples))
  3492. return NULL;
  3493. /* short-circuit if both head frames have exactly what we want */
  3494. if ((spy->read_queue.head->samples == samples) &&
  3495. (spy->write_queue.head->samples == samples)) {
  3496. read_frame = spy->read_queue.head;
  3497. spy->read_queue.head = read_frame->next;
  3498. read_frame->next = NULL;
  3499. write_frame = spy->write_queue.head;
  3500. spy->write_queue.head = write_frame->next;
  3501. write_frame->next = NULL;
  3502. spy->read_queue.samples -= samples;
  3503. spy->write_queue.samples -= samples;
  3504. need_dup = 0;
  3505. } else {
  3506. copy_data_from_queue(&spy->read_queue, read_buf, samples);
  3507. copy_data_from_queue(&spy->write_queue, write_buf, samples);
  3508. read_frame = &stack_read_frame;
  3509. write_frame = &stack_write_frame;
  3510. need_dup = 1;
  3511. }
  3512. if (ast_test_flag(spy, CHANSPY_READ_VOLADJUST))
  3513. ast_frame_adjust_volume(read_frame, spy->read_vol_adjustment);
  3514. if (ast_test_flag(spy, CHANSPY_WRITE_VOLADJUST))
  3515. ast_frame_adjust_volume(write_frame, spy->write_vol_adjustment);
  3516. if (ast_test_flag(spy, CHANSPY_MIXAUDIO)) {
  3517. ast_frame_slinear_sum(read_frame, write_frame);
  3518. if (need_dup)
  3519. result = ast_frdup(read_frame);
  3520. else
  3521. result = read_frame;
  3522. } else {
  3523. if (need_dup) {
  3524. result = ast_frdup(read_frame);
  3525. result->next = ast_frdup(write_frame);
  3526. } else {
  3527. result = read_frame;
  3528. result->next = write_frame;
  3529. }
  3530. }
  3531. return result;
  3532. }
  3533. static void *silence_generator_alloc(struct ast_channel *chan, void *data)
  3534. {
  3535. /* just store the data pointer in the channel structure */
  3536. return data;
  3537. }
  3538. static void silence_generator_release(struct ast_channel *chan, void *data)
  3539. {
  3540. /* nothing to do */
  3541. }
  3542. static int silence_generator_generate(struct ast_channel *chan, void *data, int len, int samples)
  3543. {
  3544. if (samples == 160) {
  3545. short buf[160] = { 0, };
  3546. struct ast_frame frame = {
  3547. .frametype = AST_FRAME_VOICE,
  3548. .subclass = AST_FORMAT_SLINEAR,
  3549. .data = buf,
  3550. .samples = 160,
  3551. .datalen = sizeof(buf),
  3552. };
  3553. if (ast_write(chan, &frame))
  3554. return -1;
  3555. } else {
  3556. short buf[samples];
  3557. int x;
  3558. struct ast_frame frame = {
  3559. .frametype = AST_FRAME_VOICE,
  3560. .subclass = AST_FORMAT_SLINEAR,
  3561. .data = buf,
  3562. .samples = samples,
  3563. .datalen = sizeof(buf),
  3564. };
  3565. for (x = 0; x < samples; x++)
  3566. buf[x] = 0;
  3567. if (ast_write(chan, &frame))
  3568. return -1;
  3569. }
  3570. return 0;
  3571. }
  3572. static struct ast_generator silence_generator = {
  3573. .alloc = silence_generator_alloc,
  3574. .release = silence_generator_release,
  3575. .generate = silence_generator_generate,
  3576. };
  3577. struct ast_silence_generator {
  3578. int old_write_format;
  3579. };
  3580. struct ast_silence_generator *ast_channel_start_silence_generator(struct ast_channel *chan)
  3581. {
  3582. struct ast_silence_generator *state;
  3583. if (!(state = calloc(1, sizeof(*state)))) {
  3584. ast_log(LOG_WARNING, "Could not allocate state structure\n");
  3585. return NULL;
  3586. }
  3587. state->old_write_format = chan->writeformat;
  3588. if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
  3589. ast_log(LOG_ERROR, "Could not set write format to SLINEAR\n");
  3590. free(state);
  3591. return NULL;
  3592. }
  3593. ast_activate_generator(chan, &silence_generator, state);
  3594. if (option_debug)
  3595. ast_log(LOG_DEBUG, "Started silence generator on '%s'\n", chan->name);
  3596. return state;
  3597. }
  3598. void ast_channel_stop_silence_generator(struct ast_channel *chan, struct ast_silence_generator *state)
  3599. {
  3600. if (!state)
  3601. return;
  3602. ast_deactivate_generator(chan);
  3603. if (option_debug)
  3604. ast_log(LOG_DEBUG, "Stopped silence generator on '%s'\n", chan->name);
  3605. if (ast_set_write_format(chan, state->old_write_format) < 0)
  3606. ast_log(LOG_ERROR, "Could not return write format to its original state\n");
  3607. free(state);
  3608. }