channel.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Channel Management
  5. *
  6. * Copyright (C) 1999-2004, Digium, Inc.
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/time.h>
  17. #include <signal.h>
  18. #include <errno.h>
  19. #include <unistd.h>
  20. #include <math.h> /* For PI */
  21. #include <sys/poll.h>
  22. #include <asterisk/pbx.h>
  23. #include <asterisk/frame.h>
  24. #include <asterisk/sched.h>
  25. #include <asterisk/options.h>
  26. #include <asterisk/channel.h>
  27. #include <asterisk/channel_pvt.h>
  28. #include <asterisk/logger.h>
  29. #include <asterisk/say.h>
  30. #include <asterisk/file.h>
  31. #include <asterisk/translate.h>
  32. #include <asterisk/manager.h>
  33. #include <asterisk/chanvars.h>
  34. #include <asterisk/linkedlists.h>
  35. #include <asterisk/indications.h>
  36. #include <asterisk/monitor.h>
  37. #include <asterisk/causes.h>
  38. #include <asterisk/utils.h>
  39. #include <asterisk/lock.h>
  40. #ifdef ZAPTEL_OPTIMIZATIONS
  41. #include <sys/ioctl.h>
  42. #ifdef __linux__
  43. #include <linux/zaptel.h>
  44. #else
  45. #include <zaptel.h>
  46. #endif /* __linux__ */
  47. #ifndef ZT_TIMERPING
  48. #error "You need newer zaptel! Please cvs update zaptel"
  49. #endif
  50. #endif
  51. /* uncomment if you have problems with 'monitoring' synchronized files */
  52. #if 0
  53. #define MONITOR_CONSTANT_DELAY
  54. #define MONITOR_DELAY 150 * 8 /* 150 ms of MONITORING DELAY */
  55. #endif
  56. static int shutting_down = 0;
  57. static int uniqueint = 0;
  58. /* XXX Lock appropriately in more functions XXX */
  59. struct chanlist {
  60. char type[80];
  61. char description[80];
  62. int capabilities;
  63. struct ast_channel * (*requester)(char *type, int format, void *data);
  64. int (*devicestate)(void *data);
  65. struct chanlist *next;
  66. } *backends = NULL;
  67. struct ast_channel *channels = NULL;
  68. /* Protect the channel list (highly unlikely that two things would change
  69. it at the same time, but still! */
  70. AST_MUTEX_DEFINE_STATIC(chlock);
  71. int ast_check_hangup(struct ast_channel *chan)
  72. {
  73. time_t myt;
  74. /* if soft hangup flag, return true */
  75. if (chan->_softhangup) return 1;
  76. /* if no private structure, return true */
  77. if (!chan->pvt->pvt) return 1;
  78. /* if no hangup scheduled, just return here */
  79. if (!chan->whentohangup) return 0;
  80. time(&myt); /* get current time */
  81. /* return, if not yet */
  82. if (chan->whentohangup > myt) return 0;
  83. chan->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
  84. return 1;
  85. }
  86. static int ast_check_hangup_locked(struct ast_channel *chan)
  87. {
  88. int res;
  89. ast_mutex_lock(&chan->lock);
  90. res = ast_check_hangup(chan);
  91. ast_mutex_unlock(&chan->lock);
  92. return res;
  93. }
  94. void ast_begin_shutdown(int hangup)
  95. {
  96. struct ast_channel *c;
  97. shutting_down = 1;
  98. if (hangup) {
  99. ast_mutex_lock(&chlock);
  100. c = channels;
  101. while(c) {
  102. ast_softhangup(c, AST_SOFTHANGUP_SHUTDOWN);
  103. c = c->next;
  104. }
  105. ast_mutex_unlock(&chlock);
  106. }
  107. }
  108. int ast_active_channels(void)
  109. {
  110. struct ast_channel *c;
  111. int cnt = 0;
  112. ast_mutex_lock(&chlock);
  113. c = channels;
  114. while(c) {
  115. cnt++;
  116. c = c->next;
  117. }
  118. ast_mutex_unlock(&chlock);
  119. return cnt;
  120. }
  121. void ast_cancel_shutdown(void)
  122. {
  123. shutting_down = 0;
  124. }
  125. int ast_shutting_down(void)
  126. {
  127. return shutting_down;
  128. }
  129. void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
  130. {
  131. time_t myt;
  132. time(&myt);
  133. if (offset)
  134. chan->whentohangup = myt + offset;
  135. else
  136. chan->whentohangup = 0;
  137. return;
  138. }
  139. int ast_channel_register(char *type, char *description, int capabilities,
  140. struct ast_channel *(*requester)(char *type, int format, void *data))
  141. {
  142. return ast_channel_register_ex(type, description, capabilities, requester, NULL);
  143. }
  144. int ast_channel_register_ex(char *type, char *description, int capabilities,
  145. struct ast_channel *(*requester)(char *type, int format, void *data),
  146. int (*devicestate)(void *data))
  147. {
  148. struct chanlist *chan, *last=NULL;
  149. if (ast_mutex_lock(&chlock)) {
  150. ast_log(LOG_WARNING, "Unable to lock channel list\n");
  151. return -1;
  152. }
  153. chan = backends;
  154. while (chan) {
  155. if (!strcasecmp(type, chan->type)) {
  156. ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", type);
  157. ast_mutex_unlock(&chlock);
  158. return -1;
  159. }
  160. last = chan;
  161. chan = chan->next;
  162. }
  163. chan = malloc(sizeof(struct chanlist));
  164. if (!chan) {
  165. ast_log(LOG_WARNING, "Out of memory\n");
  166. ast_mutex_unlock(&chlock);
  167. return -1;
  168. }
  169. strncpy(chan->type, type, sizeof(chan->type)-1);
  170. strncpy(chan->description, description, sizeof(chan->description)-1);
  171. chan->capabilities = capabilities;
  172. chan->requester = requester;
  173. chan->devicestate = devicestate;
  174. chan->next = NULL;
  175. if (last)
  176. last->next = chan;
  177. else
  178. backends = chan;
  179. if (option_debug)
  180. ast_log(LOG_DEBUG, "Registered handler for '%s' (%s)\n", chan->type, chan->description);
  181. else if (option_verbose > 1)
  182. ast_verbose( VERBOSE_PREFIX_2 "Registered channel type '%s' (%s)\n", chan->type, chan->description);
  183. ast_mutex_unlock(&chlock);
  184. return 0;
  185. }
  186. char *ast_state2str(int state)
  187. {
  188. /* XXX Not reentrant XXX */
  189. static char localtmp[256];
  190. switch(state) {
  191. case AST_STATE_DOWN:
  192. return "Down";
  193. case AST_STATE_RESERVED:
  194. return "Rsrvd";
  195. case AST_STATE_OFFHOOK:
  196. return "OffHook";
  197. case AST_STATE_DIALING:
  198. return "Dialing";
  199. case AST_STATE_RING:
  200. return "Ring";
  201. case AST_STATE_RINGING:
  202. return "Ringing";
  203. case AST_STATE_UP:
  204. return "Up";
  205. case AST_STATE_BUSY:
  206. return "Busy";
  207. default:
  208. snprintf(localtmp, sizeof(localtmp), "Unknown (%d)\n", state);
  209. return localtmp;
  210. }
  211. }
  212. int ast_best_codec(int fmts)
  213. {
  214. /* This just our opinion, expressed in code. We are asked to choose
  215. the best codec to use, given no information */
  216. int x;
  217. static int prefs[] =
  218. {
  219. /* Okay, ulaw is used by all telephony equipment, so start with it */
  220. AST_FORMAT_ULAW,
  221. /* Unless of course, you're a silly European, so then prefer ALAW */
  222. AST_FORMAT_ALAW,
  223. /* Okay, well, signed linear is easy to translate into other stuff */
  224. AST_FORMAT_SLINEAR,
  225. /* G.726 is standard ADPCM */
  226. AST_FORMAT_G726,
  227. /* ADPCM has great sound quality and is still pretty easy to translate */
  228. AST_FORMAT_ADPCM,
  229. /* Okay, we're down to vocoders now, so pick GSM because it's small and easier to
  230. translate and sounds pretty good */
  231. AST_FORMAT_GSM,
  232. /* iLBC is not too bad */
  233. AST_FORMAT_ILBC,
  234. /* Speex is free, but computationally more expensive than GSM */
  235. AST_FORMAT_SPEEX,
  236. /* Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
  237. to use it */
  238. AST_FORMAT_LPC10,
  239. /* G.729a is faster than 723 and slightly less expensive */
  240. AST_FORMAT_G729A,
  241. /* Down to G.723.1 which is proprietary but at least designed for voice */
  242. AST_FORMAT_G723_1,
  243. };
  244. for (x=0;x<sizeof(prefs) / sizeof(prefs[0]); x++)
  245. if (fmts & prefs[x])
  246. return prefs[x];
  247. ast_log(LOG_WARNING, "Don't know any of 0x%x formats\n", fmts);
  248. return 0;
  249. }
  250. struct ast_channel *ast_channel_alloc(int needqueue)
  251. {
  252. struct ast_channel *tmp;
  253. struct ast_channel_pvt *pvt;
  254. int x;
  255. int flags;
  256. struct varshead *headp;
  257. /* If shutting down, don't allocate any new channels */
  258. if (shutting_down)
  259. return NULL;
  260. ast_mutex_lock(&chlock);
  261. tmp = malloc(sizeof(struct ast_channel));
  262. if (tmp) {
  263. memset(tmp, 0, sizeof(struct ast_channel));
  264. pvt = malloc(sizeof(struct ast_channel_pvt));
  265. if (pvt) {
  266. memset(pvt, 0, sizeof(struct ast_channel_pvt));
  267. tmp->sched = sched_context_create();
  268. if (tmp->sched) {
  269. for (x=0;x<AST_MAX_FDS - 1;x++)
  270. tmp->fds[x] = -1;
  271. #ifdef ZAPTEL_OPTIMIZATIONS
  272. tmp->timingfd = open("/dev/zap/timer", O_RDWR);
  273. if (tmp->timingfd > -1) {
  274. /* Check if timing interface supports new
  275. ping/pong scheme */
  276. flags = 1;
  277. if (!ioctl(tmp->timingfd, ZT_TIMERPONG, &flags))
  278. needqueue = 0;
  279. }
  280. #else
  281. tmp->timingfd = -1;
  282. #endif
  283. if (needqueue &&
  284. pipe(pvt->alertpipe)) {
  285. ast_log(LOG_WARNING, "Alert pipe creation failed!\n");
  286. free(pvt);
  287. free(tmp);
  288. tmp = NULL;
  289. pvt = NULL;
  290. } else {
  291. if (needqueue) {
  292. flags = fcntl(pvt->alertpipe[0], F_GETFL);
  293. fcntl(pvt->alertpipe[0], F_SETFL, flags | O_NONBLOCK);
  294. flags = fcntl(pvt->alertpipe[1], F_GETFL);
  295. fcntl(pvt->alertpipe[1], F_SETFL, flags | O_NONBLOCK);
  296. } else
  297. /* Make sure we've got it done right if they don't */
  298. pvt->alertpipe[0] = pvt->alertpipe[1] = -1;
  299. /* Always watch the alertpipe */
  300. tmp->fds[AST_MAX_FDS-1] = pvt->alertpipe[0];
  301. /* And timing pipe */
  302. tmp->fds[AST_MAX_FDS-2] = tmp->timingfd;
  303. strncpy(tmp->name, "**Unknown**", sizeof(tmp->name)-1);
  304. tmp->pvt = pvt;
  305. /* Initial state */
  306. tmp->_state = AST_STATE_DOWN;
  307. tmp->stack = -1;
  308. tmp->streamid = -1;
  309. tmp->appl = NULL;
  310. tmp->data = NULL;
  311. tmp->fin = 0;
  312. tmp->fout = 0;
  313. snprintf(tmp->uniqueid, sizeof(tmp->uniqueid), "%li.%d", (long)time(NULL), uniqueint++);
  314. headp=&tmp->varshead;
  315. ast_mutex_init(&tmp->lock);
  316. AST_LIST_HEAD_INIT(headp);
  317. tmp->vars=ast_var_assign("tempvar","tempval");
  318. AST_LIST_INSERT_HEAD(headp,tmp->vars,entries);
  319. strncpy(tmp->context, "default", sizeof(tmp->context)-1);
  320. strncpy(tmp->language, defaultlanguage, sizeof(tmp->language)-1);
  321. strncpy(tmp->exten, "s", sizeof(tmp->exten)-1);
  322. tmp->priority=1;
  323. tmp->amaflags = ast_default_amaflags;
  324. strncpy(tmp->accountcode, ast_default_accountcode, sizeof(tmp->accountcode)-1);
  325. tmp->next = channels;
  326. channels= tmp;
  327. }
  328. } else {
  329. ast_log(LOG_WARNING, "Unable to create schedule context\n");
  330. free(tmp);
  331. tmp = NULL;
  332. }
  333. } else {
  334. ast_log(LOG_WARNING, "Out of memory\n");
  335. free(tmp);
  336. tmp = NULL;
  337. }
  338. } else
  339. ast_log(LOG_WARNING, "Out of memory\n");
  340. ast_mutex_unlock(&chlock);
  341. return tmp;
  342. }
  343. int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
  344. {
  345. struct ast_frame *f;
  346. struct ast_frame *prev, *cur;
  347. int blah = 1;
  348. int qlen = 0;
  349. /* Build us a copy and free the original one */
  350. f = ast_frdup(fin);
  351. if (!f) {
  352. ast_log(LOG_WARNING, "Unable to duplicate frame\n");
  353. return -1;
  354. }
  355. ast_mutex_lock(&chan->lock);
  356. prev = NULL;
  357. cur = chan->pvt->readq;
  358. while(cur) {
  359. if ((cur->frametype == AST_FRAME_CONTROL) && (cur->subclass == AST_CONTROL_HANGUP)) {
  360. /* Don't bother actually queueing anything after a hangup */
  361. ast_frfree(f);
  362. ast_mutex_unlock(&chan->lock);
  363. return 0;
  364. }
  365. prev = cur;
  366. cur = cur->next;
  367. qlen++;
  368. }
  369. /* Allow up to 96 voice frames outstanding, and up to 128 total frames */
  370. if (((fin->frametype == AST_FRAME_VOICE) && (qlen > 96)) || (qlen > 128)) {
  371. if (fin->frametype != AST_FRAME_VOICE) {
  372. ast_log(LOG_WARNING, "Exceptionally long queue length queuing to %s\n", chan->name);
  373. CRASH;
  374. } else {
  375. ast_log(LOG_DEBUG, "Dropping voice to exceptionally long queue on %s\n", chan->name);
  376. ast_frfree(f);
  377. ast_mutex_unlock(&chan->lock);
  378. return 0;
  379. }
  380. }
  381. if (prev)
  382. prev->next = f;
  383. else
  384. chan->pvt->readq = f;
  385. if (chan->pvt->alertpipe[1] > -1) {
  386. if (write(chan->pvt->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah))
  387. ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d (qlen = %d): %s!\n",
  388. chan->name, f->frametype, f->subclass, qlen, strerror(errno));
  389. #ifdef ZAPTEL_OPTIMIZATIONS
  390. } else if (chan->timingfd > -1) {
  391. ioctl(chan->timingfd, ZT_TIMERPING, &blah);
  392. #endif
  393. } else if (chan->blocking) {
  394. pthread_kill(chan->blocker, SIGURG);
  395. }
  396. ast_mutex_unlock(&chan->lock);
  397. return 0;
  398. }
  399. int ast_queue_hangup(struct ast_channel *chan)
  400. {
  401. struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
  402. chan->_softhangup |= AST_SOFTHANGUP_DEV;
  403. return ast_queue_frame(chan, &f);
  404. }
  405. int ast_queue_control(struct ast_channel *chan, int control)
  406. {
  407. struct ast_frame f = { AST_FRAME_CONTROL, };
  408. f.subclass = control;
  409. return ast_queue_frame(chan, &f);
  410. }
  411. int ast_channel_defer_dtmf(struct ast_channel *chan)
  412. {
  413. int pre = 0;
  414. if (chan) {
  415. pre = chan->deferdtmf;
  416. chan->deferdtmf = 1;
  417. }
  418. return pre;
  419. }
  420. void ast_channel_undefer_dtmf(struct ast_channel *chan)
  421. {
  422. if (chan)
  423. chan->deferdtmf = 0;
  424. }
  425. struct ast_channel *ast_channel_walk_locked(struct ast_channel *prev)
  426. {
  427. /* Returns next channel (locked) */
  428. struct ast_channel *l, *ret;
  429. int retries = 0;
  430. retry:
  431. ret=NULL;
  432. ast_mutex_lock(&chlock);
  433. l = channels;
  434. if (!prev) {
  435. if (l) {
  436. if (ast_mutex_trylock(&l->lock)) {
  437. if (retries < 10)
  438. ast_log(LOG_DEBUG, "Avoiding initial deadlock for '%s'\n", l->name);
  439. else
  440. ast_log(LOG_WARNING, "Avoided initial deadlock for '%s', %d retries!\n", l->name, retries);
  441. ast_mutex_unlock(&chlock);
  442. if (retries < 10) {
  443. usleep(1);
  444. retries++;
  445. goto retry;
  446. } else
  447. return NULL;
  448. }
  449. }
  450. ast_mutex_unlock(&chlock);
  451. return l;
  452. }
  453. while(l) {
  454. if (l == prev)
  455. ret = l->next;
  456. l = l->next;
  457. }
  458. if (ret) {
  459. if (ast_mutex_trylock(&ret->lock)) {
  460. if (retries < 10)
  461. ast_log(LOG_DEBUG, "Avoiding deadlock for '%s'\n", ret->name);
  462. else
  463. ast_log(LOG_WARNING, "Avoided deadlock for '%s', %d retries!\n", ret->name, retries);
  464. ast_mutex_unlock(&chlock);
  465. if (retries < 10) {
  466. usleep(1);
  467. retries++;
  468. goto retry;
  469. } else
  470. return NULL;
  471. }
  472. }
  473. ast_mutex_unlock(&chlock);
  474. return ret;
  475. }
  476. struct ast_channel *ast_get_channel_by_name_locked(char *channame)
  477. {
  478. struct ast_channel *chan;
  479. chan = ast_channel_walk_locked(NULL);
  480. while(chan) {
  481. if (!strcasecmp(chan->name, channame))
  482. return chan;
  483. ast_mutex_unlock(&chan->lock);
  484. chan = ast_channel_walk_locked(chan);
  485. }
  486. return NULL;
  487. }
  488. int ast_safe_sleep_conditional( struct ast_channel *chan, int ms,
  489. int (*cond)(void*), void *data )
  490. {
  491. struct ast_frame *f;
  492. while(ms > 0) {
  493. if( cond && ((*cond)(data) == 0 ) )
  494. return 0;
  495. ms = ast_waitfor(chan, ms);
  496. if (ms <0)
  497. return -1;
  498. if (ms > 0) {
  499. f = ast_read(chan);
  500. if (!f)
  501. return -1;
  502. ast_frfree(f);
  503. }
  504. }
  505. return 0;
  506. }
  507. int ast_safe_sleep(struct ast_channel *chan, int ms)
  508. {
  509. struct ast_frame *f;
  510. while(ms > 0) {
  511. ms = ast_waitfor(chan, ms);
  512. if (ms <0)
  513. return -1;
  514. if (ms > 0) {
  515. f = ast_read(chan);
  516. if (!f)
  517. return -1;
  518. ast_frfree(f);
  519. }
  520. }
  521. return 0;
  522. }
  523. void ast_channel_free(struct ast_channel *chan)
  524. {
  525. struct ast_channel *last=NULL, *cur;
  526. int fd;
  527. struct ast_var_t *vardata;
  528. struct ast_frame *f, *fp;
  529. struct varshead *headp;
  530. char name[AST_CHANNEL_NAME];
  531. headp=&chan->varshead;
  532. ast_mutex_lock(&chlock);
  533. cur = channels;
  534. while(cur) {
  535. if (cur == chan) {
  536. if (last)
  537. last->next = cur->next;
  538. else
  539. channels = cur->next;
  540. break;
  541. }
  542. last = cur;
  543. cur = cur->next;
  544. }
  545. if (!cur)
  546. ast_log(LOG_WARNING, "Unable to find channel in list\n");
  547. else {
  548. /* Lock and unlock the channel just to be sure nobody
  549. has it locked still */
  550. ast_mutex_lock(&cur->lock);
  551. ast_mutex_unlock(&cur->lock);
  552. }
  553. if (chan->pvt->pvt)
  554. ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name);
  555. strncpy(name, chan->name, sizeof(name)-1);
  556. /* Stop monitoring */
  557. if (chan->monitor) {
  558. chan->monitor->stop( chan, 0 );
  559. }
  560. /* Free translatosr */
  561. if (chan->pvt->readtrans)
  562. ast_translator_free_path(chan->pvt->readtrans);
  563. if (chan->pvt->writetrans)
  564. ast_translator_free_path(chan->pvt->writetrans);
  565. if (chan->pbx)
  566. ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name);
  567. if (chan->dnid)
  568. free(chan->dnid);
  569. if (chan->callerid)
  570. free(chan->callerid);
  571. if (chan->ani)
  572. free(chan->ani);
  573. if (chan->rdnis)
  574. free(chan->rdnis);
  575. ast_mutex_destroy(&chan->lock);
  576. /* Close pipes if appropriate */
  577. if ((fd = chan->pvt->alertpipe[0]) > -1)
  578. close(fd);
  579. if ((fd = chan->pvt->alertpipe[1]) > -1)
  580. close(fd);
  581. if ((fd = chan->timingfd) > -1)
  582. close(fd);
  583. f = chan->pvt->readq;
  584. chan->pvt->readq = NULL;
  585. while(f) {
  586. fp = f;
  587. f = f->next;
  588. ast_frfree(fp);
  589. }
  590. /* loop over the variables list, freeing all data and deleting list items */
  591. /* no need to lock the list, as the channel is already locked */
  592. while (!AST_LIST_EMPTY(headp)) { /* List Deletion. */
  593. vardata = AST_LIST_FIRST(headp);
  594. AST_LIST_REMOVE_HEAD(headp, entries);
  595. // printf("deleting var %s=%s\n",ast_var_name(vardata),ast_var_value(vardata));
  596. ast_var_delete(vardata);
  597. }
  598. free(chan->pvt);
  599. chan->pvt = NULL;
  600. free(chan);
  601. ast_mutex_unlock(&chlock);
  602. ast_device_state_changed(name);
  603. }
  604. int ast_softhangup_nolock(struct ast_channel *chan, int cause)
  605. {
  606. int res = 0;
  607. struct ast_frame f = { AST_FRAME_NULL };
  608. if (option_debug)
  609. ast_log(LOG_DEBUG, "Soft-Hanging up channel '%s'\n", chan->name);
  610. /* Inform channel driver that we need to be hung up, if it cares */
  611. chan->_softhangup |= cause;
  612. ast_queue_frame(chan, &f);
  613. /* Interrupt any poll call or such */
  614. if (chan->blocking)
  615. pthread_kill(chan->blocker, SIGURG);
  616. return res;
  617. }
  618. int ast_softhangup(struct ast_channel *chan, int cause)
  619. {
  620. int res;
  621. ast_mutex_lock(&chan->lock);
  622. res = ast_softhangup_nolock(chan, cause);
  623. ast_mutex_unlock(&chan->lock);
  624. return res;
  625. }
  626. static void free_translation(struct ast_channel *clone)
  627. {
  628. if (clone->pvt->writetrans)
  629. ast_translator_free_path(clone->pvt->writetrans);
  630. if (clone->pvt->readtrans)
  631. ast_translator_free_path(clone->pvt->readtrans);
  632. clone->pvt->writetrans = NULL;
  633. clone->pvt->readtrans = NULL;
  634. clone->pvt->rawwriteformat = clone->nativeformats;
  635. clone->pvt->rawreadformat = clone->nativeformats;
  636. }
  637. int ast_hangup(struct ast_channel *chan)
  638. {
  639. int res = 0;
  640. /* Don't actually hang up a channel that will masquerade as someone else, or
  641. if someone is going to masquerade as us */
  642. ast_mutex_lock(&chan->lock);
  643. if (chan->masq) {
  644. if (ast_do_masquerade(chan))
  645. ast_log(LOG_WARNING, "Failed to perform masquerade\n");
  646. }
  647. if (chan->masq) {
  648. ast_log(LOG_WARNING, "%s getting hung up, but someone is trying to masq into us?!?\n", chan->name);
  649. ast_mutex_unlock(&chan->lock);
  650. return 0;
  651. }
  652. /* If this channel is one which will be masqueraded into something,
  653. mark it as a zombie already, so we know to free it later */
  654. if (chan->masqr) {
  655. chan->zombie=1;
  656. ast_mutex_unlock(&chan->lock);
  657. return 0;
  658. }
  659. free_translation(chan);
  660. if (chan->stream)
  661. ast_closestream(chan->stream);
  662. if (chan->vstream)
  663. ast_closestream(chan->vstream);
  664. if (chan->sched)
  665. sched_context_destroy(chan->sched);
  666. /* Clear any tone stuff remaining */
  667. if (chan->generatordata)
  668. chan->generator->release(chan, chan->generatordata);
  669. chan->generatordata = NULL;
  670. chan->generator = NULL;
  671. if (chan->cdr) {
  672. /* End the CDR if it hasn't already */
  673. ast_cdr_end(chan->cdr);
  674. /* Post and Free the CDR */
  675. ast_cdr_post(chan->cdr);
  676. ast_cdr_free(chan->cdr);
  677. }
  678. if (chan->blocking) {
  679. ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
  680. "is blocked by thread %ld in procedure %s! Expect a failure\n",
  681. (long)pthread_self(), chan->name, (long)chan->blocker, chan->blockproc);
  682. CRASH;
  683. }
  684. if (!chan->zombie) {
  685. if (option_debug)
  686. ast_log(LOG_DEBUG, "Hanging up channel '%s'\n", chan->name);
  687. if (chan->pvt->hangup)
  688. res = chan->pvt->hangup(chan);
  689. } else
  690. if (option_debug)
  691. ast_log(LOG_DEBUG, "Hanging up zombie '%s'\n", chan->name);
  692. ast_mutex_unlock(&chan->lock);
  693. manager_event(EVENT_FLAG_CALL, "Hangup",
  694. "Channel: %s\r\n"
  695. "Uniqueid: %s\r\n"
  696. "Cause: %i\r\n",
  697. chan->name, chan->uniqueid, chan->hangupcause);
  698. ast_channel_free(chan);
  699. return res;
  700. }
  701. void ast_channel_unregister(char *type)
  702. {
  703. struct chanlist *chan, *last=NULL;
  704. if (option_debug)
  705. ast_log(LOG_DEBUG, "Unregistering channel type '%s'\n", type);
  706. if (ast_mutex_lock(&chlock)) {
  707. ast_log(LOG_WARNING, "Unable to lock channel list\n");
  708. return;
  709. }
  710. if (option_verbose > 1)
  711. ast_verbose( VERBOSE_PREFIX_2 "Unregistered channel type '%s'\n", type);
  712. chan = backends;
  713. while(chan) {
  714. if (!strcasecmp(chan->type, type)) {
  715. if (last)
  716. last->next = chan->next;
  717. else
  718. backends = backends->next;
  719. free(chan);
  720. ast_mutex_unlock(&chlock);
  721. return;
  722. }
  723. last = chan;
  724. chan = chan->next;
  725. }
  726. ast_mutex_unlock(&chlock);
  727. }
  728. int ast_answer(struct ast_channel *chan)
  729. {
  730. int res = 0;
  731. ast_mutex_lock(&chan->lock);
  732. /* Stop if we're a zombie or need a soft hangup */
  733. if (chan->zombie || ast_check_hangup(chan)) {
  734. ast_mutex_unlock(&chan->lock);
  735. return -1;
  736. }
  737. switch(chan->_state) {
  738. case AST_STATE_RINGING:
  739. case AST_STATE_RING:
  740. if (chan->pvt->answer)
  741. res = chan->pvt->answer(chan);
  742. ast_setstate(chan, AST_STATE_UP);
  743. if (chan->cdr)
  744. ast_cdr_answer(chan->cdr);
  745. ast_mutex_unlock(&chan->lock);
  746. return res;
  747. break;
  748. case AST_STATE_UP:
  749. if (chan->cdr)
  750. ast_cdr_answer(chan->cdr);
  751. break;
  752. }
  753. ast_mutex_unlock(&chan->lock);
  754. return 0;
  755. }
  756. void ast_deactivate_generator(struct ast_channel *chan)
  757. {
  758. ast_mutex_lock(&chan->lock);
  759. if (chan->generatordata) {
  760. if (chan->generator && chan->generator->release)
  761. chan->generator->release(chan, chan->generatordata);
  762. chan->generatordata = NULL;
  763. chan->generator = NULL;
  764. chan->writeinterrupt = 0;
  765. ast_settimeout(chan, 0, NULL, NULL);
  766. }
  767. ast_mutex_unlock(&chan->lock);
  768. }
  769. static int generator_force(void *data)
  770. {
  771. /* Called if generator doesn't have data */
  772. void *tmp;
  773. int res;
  774. int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
  775. struct ast_channel *chan = data;
  776. tmp = chan->generatordata;
  777. chan->generatordata = NULL;
  778. generate = chan->generator->generate;
  779. res = generate(chan, tmp, 0, 160);
  780. chan->generatordata = tmp;
  781. if (res) {
  782. ast_log(LOG_DEBUG, "Auto-deactivating generator\n");
  783. ast_deactivate_generator(chan);
  784. }
  785. return 0;
  786. }
  787. int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
  788. {
  789. int res = 0;
  790. ast_mutex_lock(&chan->lock);
  791. if (chan->generatordata) {
  792. if (chan->generator && chan->generator->release)
  793. chan->generator->release(chan, chan->generatordata);
  794. chan->generatordata = NULL;
  795. }
  796. ast_prod(chan);
  797. if ((chan->generatordata = gen->alloc(chan, params))) {
  798. ast_settimeout(chan, 160, generator_force, chan);
  799. chan->generator = gen;
  800. } else {
  801. res = -1;
  802. }
  803. ast_mutex_unlock(&chan->lock);
  804. return res;
  805. }
  806. int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
  807. {
  808. /* Wait for x amount of time on a file descriptor to have input. */
  809. struct timeval start, now;
  810. int res;
  811. int x, y;
  812. int winner = -1;
  813. int spoint;
  814. struct pollfd *pfds;
  815. pfds = alloca(sizeof(struct pollfd) * n);
  816. if (!pfds) {
  817. ast_log(LOG_WARNING, "alloca failed! bad things will happen.\n");
  818. return -1;
  819. }
  820. if (*ms > 0)
  821. gettimeofday(&start, NULL);
  822. y = 0;
  823. for (x=0;x<n;x++) {
  824. if (fds[x] > -1) {
  825. pfds[y].fd = fds[x];
  826. pfds[y].events = POLLIN | POLLPRI;
  827. y++;
  828. }
  829. }
  830. res = poll(pfds, y, *ms);
  831. if (res < 0) {
  832. /* Simulate a timeout if we were interrupted */
  833. if (errno != EINTR)
  834. *ms = -1;
  835. else
  836. *ms = 0;
  837. return -1;
  838. }
  839. spoint = 0;
  840. for (x=0;x<n;x++) {
  841. if (fds[x] > -1) {
  842. if ((res = ast_fdisset(pfds, fds[x], y, &spoint))) {
  843. winner = fds[x];
  844. if (exception) {
  845. if (res & POLLPRI)
  846. *exception = -1;
  847. else
  848. *exception = 0;
  849. }
  850. }
  851. }
  852. }
  853. if (*ms > 0) {
  854. long passed;
  855. gettimeofday(&now, NULL);
  856. passed = (now.tv_sec - start.tv_sec) * 1000;
  857. passed += (now.tv_usec - start.tv_usec) / 1000;
  858. if (passed <= *ms)
  859. *ms -= passed;
  860. else
  861. *ms = 0;
  862. }
  863. return winner;
  864. }
  865. struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
  866. int *exception, int *outfd, int *ms)
  867. {
  868. /* Wait for x amount of time on a file descriptor to have input. */
  869. struct timeval start, end;
  870. struct pollfd *pfds;
  871. int res;
  872. long rms;
  873. int x, y, max;
  874. int spoint;
  875. time_t now = 0;
  876. long whentohangup = 0, havewhen = 0, diff;
  877. struct ast_channel *winner = NULL;
  878. pfds = alloca(sizeof(struct pollfd) * (n * AST_MAX_FDS + nfds));
  879. if (!pfds) {
  880. ast_log(LOG_WARNING, "alloca failed! bad things will happen.\n");
  881. *outfd = -1;
  882. return NULL;
  883. }
  884. if (outfd)
  885. *outfd = -99999;
  886. if (exception)
  887. *exception = 0;
  888. /* Perform any pending masquerades */
  889. for (x=0;x<n;x++) {
  890. ast_mutex_lock(&c[x]->lock);
  891. if (c[x]->whentohangup) {
  892. if (!havewhen)
  893. time(&now);
  894. diff = c[x]->whentohangup - now;
  895. if (!havewhen || (diff < whentohangup)) {
  896. havewhen++;
  897. whentohangup = diff;
  898. }
  899. }
  900. if (c[x]->masq) {
  901. if (ast_do_masquerade(c[x])) {
  902. ast_log(LOG_WARNING, "Masquerade failed\n");
  903. *ms = -1;
  904. ast_mutex_unlock(&c[x]->lock);
  905. return NULL;
  906. }
  907. }
  908. ast_mutex_unlock(&c[x]->lock);
  909. }
  910. rms = *ms;
  911. if (havewhen) {
  912. if ((*ms < 0) || (whentohangup * 1000 < *ms)) {
  913. rms = whentohangup * 1000;
  914. }
  915. }
  916. max = 0;
  917. for (x=0;x<n;x++) {
  918. for (y=0;y<AST_MAX_FDS;y++) {
  919. if (c[x]->fds[y] > -1) {
  920. pfds[max].fd = c[x]->fds[y];
  921. pfds[max].events = POLLIN | POLLPRI;
  922. max++;
  923. }
  924. }
  925. CHECK_BLOCKING(c[x]);
  926. }
  927. for (x=0;x<nfds; x++) {
  928. if (fds[x] > -1) {
  929. pfds[max].fd = fds[x];
  930. pfds[max].events = POLLIN | POLLPRI;
  931. max++;
  932. }
  933. }
  934. if (*ms > 0)
  935. gettimeofday(&start, NULL);
  936. res = poll(pfds, max, rms);
  937. if (res < 0) {
  938. for (x=0;x<n;x++)
  939. c[x]->blocking = 0;
  940. /* Simulate a timeout if we were interrupted */
  941. if (errno != EINTR)
  942. *ms = -1;
  943. else {
  944. /* Just an interrupt */
  945. #if 0
  946. *ms = 0;
  947. #endif
  948. }
  949. return NULL;
  950. }
  951. if (havewhen)
  952. time(&now);
  953. spoint = 0;
  954. for (x=0;x<n;x++) {
  955. c[x]->blocking = 0;
  956. if (havewhen && c[x]->whentohangup && (now > c[x]->whentohangup)) {
  957. c[x]->_softhangup |= AST_SOFTHANGUP_TIMEOUT;
  958. if (!winner)
  959. winner = c[x];
  960. }
  961. for (y=0;y<AST_MAX_FDS;y++) {
  962. if (c[x]->fds[y] > -1) {
  963. if ((res = ast_fdisset(pfds, c[x]->fds[y], max, &spoint))) {
  964. if (res & POLLPRI)
  965. c[x]->exception = -1;
  966. else
  967. c[x]->exception = 0;
  968. c[x]->fdno = y;
  969. winner = c[x];
  970. }
  971. }
  972. }
  973. }
  974. for (x=0;x<nfds;x++) {
  975. if (fds[x] > -1) {
  976. if ((res = ast_fdisset(pfds, fds[x], max, &spoint))) {
  977. if (outfd)
  978. *outfd = fds[x];
  979. if (exception) {
  980. if (res & POLLPRI)
  981. *exception = -1;
  982. else
  983. *exception = 0;
  984. }
  985. winner = NULL;
  986. }
  987. }
  988. }
  989. if (*ms > 0) {
  990. long diff;
  991. gettimeofday(&end, NULL);
  992. diff = (end.tv_sec - start.tv_sec) * 1000;
  993. diff += (end.tv_usec - start.tv_usec) / 1000;
  994. if (diff < *ms)
  995. *ms -= diff;
  996. else
  997. *ms = 0;
  998. }
  999. return winner;
  1000. }
  1001. struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
  1002. {
  1003. return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
  1004. }
  1005. int ast_waitfor(struct ast_channel *c, int ms)
  1006. {
  1007. struct ast_channel *chan;
  1008. int oldms = ms;
  1009. chan = ast_waitfor_n(&c, 1, &ms);
  1010. if (ms < 0) {
  1011. if (oldms < 0)
  1012. return 0;
  1013. else
  1014. return -1;
  1015. }
  1016. return ms;
  1017. }
  1018. int ast_waitfordigit(struct ast_channel *c, int ms)
  1019. {
  1020. /* XXX Should I be merged with waitfordigit_full XXX */
  1021. struct ast_frame *f;
  1022. int result = 0;
  1023. /* Stop if we're a zombie or need a soft hangup */
  1024. if (c->zombie || ast_check_hangup(c))
  1025. return -1;
  1026. /* Wait for a digit, no more than ms milliseconds total. */
  1027. while(ms && !result) {
  1028. ms = ast_waitfor(c, ms);
  1029. if (ms < 0) /* Error */
  1030. result = -1;
  1031. else if (ms > 0) {
  1032. /* Read something */
  1033. f = ast_read(c);
  1034. if (f) {
  1035. if (f->frametype == AST_FRAME_DTMF)
  1036. result = f->subclass;
  1037. ast_frfree(f);
  1038. } else
  1039. result = -1;
  1040. }
  1041. }
  1042. return result;
  1043. }
  1044. int ast_settimeout(struct ast_channel *c, int samples, int (*func)(void *data), void *data)
  1045. {
  1046. int res = -1;
  1047. #ifdef ZAPTEL_OPTIMIZATIONS
  1048. if (c->timingfd > -1) {
  1049. if (!func) {
  1050. samples = 0;
  1051. data = 0;
  1052. }
  1053. ast_log(LOG_DEBUG, "Scheduling timer at %d sample intervals\n", samples);
  1054. res = ioctl(c->timingfd, ZT_TIMERCONFIG, &samples);
  1055. c->timingfunc = func;
  1056. c->timingdata = data;
  1057. }
  1058. #endif
  1059. return res;
  1060. }
  1061. int ast_waitfordigit_full(struct ast_channel *c, int ms, int audiofd, int cmdfd)
  1062. {
  1063. struct ast_frame *f;
  1064. struct ast_channel *rchan;
  1065. int outfd;
  1066. int res;
  1067. /* Stop if we're a zombie or need a soft hangup */
  1068. if (c->zombie || ast_check_hangup(c))
  1069. return -1;
  1070. /* Wait for a digit, no more than ms milliseconds total. */
  1071. while(ms) {
  1072. errno = 0;
  1073. rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
  1074. if ((!rchan) && (outfd < 0) && (ms)) {
  1075. if (errno == 0 || errno == EINTR)
  1076. continue;
  1077. ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
  1078. return -1;
  1079. } else if (outfd > -1) {
  1080. /* The FD we were watching has something waiting */
  1081. return 1;
  1082. } else if (rchan) {
  1083. f = ast_read(c);
  1084. if(!f) {
  1085. return -1;
  1086. }
  1087. switch(f->frametype) {
  1088. case AST_FRAME_DTMF:
  1089. res = f->subclass;
  1090. ast_frfree(f);
  1091. return res;
  1092. case AST_FRAME_CONTROL:
  1093. switch(f->subclass) {
  1094. case AST_CONTROL_HANGUP:
  1095. ast_frfree(f);
  1096. return -1;
  1097. case AST_CONTROL_RINGING:
  1098. case AST_CONTROL_ANSWER:
  1099. /* Unimportant */
  1100. break;
  1101. default:
  1102. ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass);
  1103. }
  1104. case AST_FRAME_VOICE:
  1105. /* Write audio if appropriate */
  1106. if (audiofd > -1)
  1107. write(audiofd, f->data, f->datalen);
  1108. }
  1109. /* Ignore */
  1110. ast_frfree(f);
  1111. }
  1112. }
  1113. return 0; // Time is up
  1114. }
  1115. struct ast_frame *ast_read(struct ast_channel *chan)
  1116. {
  1117. struct ast_frame *f = NULL;
  1118. int blah;
  1119. #ifdef ZAPTEL_OPTIMIZATIONS
  1120. int (*func)(void *);
  1121. void *data;
  1122. int res;
  1123. #endif
  1124. static struct ast_frame null_frame =
  1125. {
  1126. AST_FRAME_NULL,
  1127. };
  1128. ast_mutex_lock(&chan->lock);
  1129. if (chan->masq) {
  1130. if (ast_do_masquerade(chan)) {
  1131. ast_log(LOG_WARNING, "Failed to perform masquerade\n");
  1132. f = NULL;
  1133. } else
  1134. f = &null_frame;
  1135. ast_mutex_unlock(&chan->lock);
  1136. return f;
  1137. }
  1138. /* Stop if we're a zombie or need a soft hangup */
  1139. if (chan->zombie || ast_check_hangup(chan)) {
  1140. if (chan->generator)
  1141. ast_deactivate_generator(chan);
  1142. ast_mutex_unlock(&chan->lock);
  1143. return NULL;
  1144. }
  1145. if (!chan->deferdtmf && !ast_strlen_zero(chan->dtmfq)) {
  1146. /* We have DTMF that has been deferred. Return it now */
  1147. chan->dtmff.frametype = AST_FRAME_DTMF;
  1148. chan->dtmff.subclass = chan->dtmfq[0];
  1149. /* Drop first digit */
  1150. memmove(chan->dtmfq, chan->dtmfq + 1, sizeof(chan->dtmfq) - 1);
  1151. ast_mutex_unlock(&chan->lock);
  1152. return &chan->dtmff;
  1153. }
  1154. /* Read and ignore anything on the alertpipe, but read only
  1155. one sizeof(blah) per frame that we send from it */
  1156. if (chan->pvt->alertpipe[0] > -1) {
  1157. read(chan->pvt->alertpipe[0], &blah, sizeof(blah));
  1158. }
  1159. #ifdef ZAPTEL_OPTIMIZATIONS
  1160. if ((chan->timingfd > -1) && (chan->fdno == AST_MAX_FDS - 2) && chan->exception) {
  1161. chan->exception = 0;
  1162. blah = -1;
  1163. /* IF we can't get event, assume it's an expired as-per the old interface */
  1164. res = ioctl(chan->timingfd, ZT_GETEVENT, &blah);
  1165. if (res)
  1166. blah = ZT_EVENT_TIMER_EXPIRED;
  1167. if (blah == ZT_EVENT_TIMER_PING) {
  1168. #if 0
  1169. ast_log(LOG_NOTICE, "Oooh, there's a PING!\n");
  1170. #endif
  1171. if (!chan->pvt->readq || !chan->pvt->readq->next) {
  1172. /* Acknowledge PONG unless we need it again */
  1173. #if 0
  1174. ast_log(LOG_NOTICE, "Sending a PONG!\n");
  1175. #endif
  1176. if (ioctl(chan->timingfd, ZT_TIMERPONG, &blah)) {
  1177. ast_log(LOG_WARNING, "Failed to pong timer on '%s': %s\n", chan->name, strerror(errno));
  1178. }
  1179. }
  1180. } else if (blah == ZT_EVENT_TIMER_EXPIRED) {
  1181. ioctl(chan->timingfd, ZT_TIMERACK, &blah);
  1182. func = chan->timingfunc;
  1183. data = chan->timingdata;
  1184. ast_mutex_unlock(&chan->lock);
  1185. if (func) {
  1186. #if 0
  1187. ast_log(LOG_DEBUG, "Calling private function\n");
  1188. #endif
  1189. func(data);
  1190. } else {
  1191. blah = 0;
  1192. ast_mutex_lock(&chan->lock);
  1193. ioctl(chan->timingfd, ZT_TIMERCONFIG, &blah);
  1194. chan->timingdata = NULL;
  1195. ast_mutex_unlock(&chan->lock);
  1196. }
  1197. f = &null_frame;
  1198. return f;
  1199. } else
  1200. ast_log(LOG_NOTICE, "No/unknown event '%d' on timer for '%s'?\n", blah, chan->name);
  1201. }
  1202. #endif
  1203. /* Check for pending read queue */
  1204. if (chan->pvt->readq) {
  1205. f = chan->pvt->readq;
  1206. chan->pvt->readq = f->next;
  1207. /* Interpret hangup and return NULL */
  1208. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP)) {
  1209. ast_frfree(f);
  1210. f = NULL;
  1211. }
  1212. } else {
  1213. chan->blocker = pthread_self();
  1214. if (chan->exception) {
  1215. if (chan->pvt->exception)
  1216. f = chan->pvt->exception(chan);
  1217. else {
  1218. ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", chan->name);
  1219. f = &null_frame;
  1220. }
  1221. /* Clear the exception flag */
  1222. chan->exception = 0;
  1223. } else
  1224. if (chan->pvt->read)
  1225. f = chan->pvt->read(chan);
  1226. else
  1227. ast_log(LOG_WARNING, "No read routine on channel %s\n", chan->name);
  1228. }
  1229. if (f && (f->frametype == AST_FRAME_VOICE)) {
  1230. if (!(f->subclass & chan->nativeformats)) {
  1231. /* This frame can't be from the current native formats -- drop it on the
  1232. floor */
  1233. 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));
  1234. ast_frfree(f);
  1235. f = &null_frame;
  1236. } else {
  1237. if (chan->monitor && chan->monitor->read_stream ) {
  1238. #ifndef MONITOR_CONSTANT_DELAY
  1239. int jump = chan->outsmpl - chan->insmpl - 2 * f->samples;
  1240. if (jump >= 0) {
  1241. if (ast_seekstream(chan->monitor->read_stream, jump + f->samples, SEEK_FORCECUR) == -1)
  1242. ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
  1243. chan->insmpl += jump + 2 * f->samples;
  1244. } else
  1245. chan->insmpl+= f->samples;
  1246. #else
  1247. int jump = chan->outsmpl - chan->insmpl;
  1248. if (jump - MONITOR_DELAY >= 0) {
  1249. if (ast_seekstream(chan->monitor->read_stream, jump - f->samples, SEEK_FORCECUR) == -1)
  1250. ast_log(LOG_WARNING, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
  1251. chan->insmpl += jump;
  1252. } else
  1253. chan->insmpl += f->samples;
  1254. #endif
  1255. if (ast_writestream(chan->monitor->read_stream, f) < 0)
  1256. ast_log(LOG_WARNING, "Failed to write data to channel monitor read stream\n");
  1257. }
  1258. if (chan->pvt->readtrans) {
  1259. f = ast_translate(chan->pvt->readtrans, f, 1);
  1260. if (!f)
  1261. f = &null_frame;
  1262. }
  1263. }
  1264. }
  1265. /* Make sure we always return NULL in the future */
  1266. if (!f) {
  1267. chan->_softhangup |= AST_SOFTHANGUP_DEV;
  1268. if (chan->generator)
  1269. ast_deactivate_generator(chan);
  1270. /* End the CDR if appropriate */
  1271. if (chan->cdr)
  1272. ast_cdr_end(chan->cdr);
  1273. } else if (chan->deferdtmf && f->frametype == AST_FRAME_DTMF) {
  1274. if (strlen(chan->dtmfq) < sizeof(chan->dtmfq) - 2)
  1275. chan->dtmfq[strlen(chan->dtmfq)] = f->subclass;
  1276. else
  1277. ast_log(LOG_WARNING, "Dropping deferred DTMF digits on %s\n", chan->name);
  1278. f = &null_frame;
  1279. } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_ANSWER)) {
  1280. if (chan->_state == AST_STATE_UP) {
  1281. ast_log(LOG_DEBUG, "Dropping duplicate answer!\n");
  1282. f = &null_frame;
  1283. }
  1284. /* Answer the CDR */
  1285. ast_setstate(chan, AST_STATE_UP);
  1286. ast_cdr_answer(chan->cdr);
  1287. }
  1288. /* Run any generator sitting on the line */
  1289. if (f && (f->frametype == AST_FRAME_VOICE) && chan->generatordata) {
  1290. /* Mask generator data temporarily and apply. If there is a timing function, it
  1291. will be calling the generator instead */
  1292. void *tmp;
  1293. int res;
  1294. int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
  1295. if (chan->timingfunc) {
  1296. ast_log(LOG_DEBUG, "Generator got voice, switching to phase locked mode\n");
  1297. ast_settimeout(chan, 0, NULL, NULL);
  1298. }
  1299. tmp = chan->generatordata;
  1300. chan->generatordata = NULL;
  1301. generate = chan->generator->generate;
  1302. res = generate(chan, tmp, f->datalen, f->samples);
  1303. chan->generatordata = tmp;
  1304. if (res) {
  1305. ast_log(LOG_DEBUG, "Auto-deactivating generator\n");
  1306. ast_deactivate_generator(chan);
  1307. }
  1308. } else if (f && (f->frametype == AST_FRAME_CNG)) {
  1309. if (chan->generator && !chan->timingfunc && (chan->timingfd > -1)) {
  1310. ast_log(LOG_DEBUG, "Generator got CNG, switching to zap timed mode\n");
  1311. ast_settimeout(chan, 160, generator_force, chan);
  1312. }
  1313. }
  1314. /* High bit prints debugging */
  1315. if (chan->fin & 0x80000000)
  1316. ast_frame_dump(chan->name, f, "<<");
  1317. if ((chan->fin & 0x7fffffff) == 0x7fffffff)
  1318. chan->fin &= 0x80000000;
  1319. else
  1320. chan->fin++;
  1321. ast_mutex_unlock(&chan->lock);
  1322. return f;
  1323. }
  1324. int ast_indicate(struct ast_channel *chan, int condition)
  1325. {
  1326. int res = -1;
  1327. /* Stop if we're a zombie or need a soft hangup */
  1328. if (chan->zombie || ast_check_hangup(chan))
  1329. return -1;
  1330. ast_mutex_lock(&chan->lock);
  1331. if (chan->pvt->indicate)
  1332. res = chan->pvt->indicate(chan, condition);
  1333. ast_mutex_unlock(&chan->lock);
  1334. if (!chan->pvt->indicate || res) {
  1335. /*
  1336. * Device does not support (that) indication, lets fake
  1337. * it by doing our own tone generation. (PM2002)
  1338. */
  1339. if (condition >= 0) {
  1340. const struct tone_zone_sound *ts = NULL;
  1341. switch (condition) {
  1342. case AST_CONTROL_RINGING:
  1343. ts = ast_get_indication_tone(chan->zone, "ring");
  1344. break;
  1345. case AST_CONTROL_BUSY:
  1346. ts = ast_get_indication_tone(chan->zone, "busy");
  1347. break;
  1348. case AST_CONTROL_CONGESTION:
  1349. ts = ast_get_indication_tone(chan->zone, "congestion");
  1350. break;
  1351. }
  1352. if (ts && ts->data[0]) {
  1353. ast_log(LOG_DEBUG, "Driver for channel '%s' does not support indication %d, emulating it\n", chan->name, condition);
  1354. ast_playtones_start(chan,0,ts->data, 1);
  1355. res = 0;
  1356. } else if (condition == AST_CONTROL_PROGRESS) {
  1357. /* ast_playtones_stop(chan); */
  1358. } else if (condition == AST_CONTROL_PROCEEDING) {
  1359. /* Do nothing, really */
  1360. } else {
  1361. /* not handled */
  1362. ast_log(LOG_WARNING, "Unable to handle indication %d for '%s'\n", condition, chan->name);
  1363. res = -1;
  1364. }
  1365. }
  1366. else ast_playtones_stop(chan);
  1367. }
  1368. return res;
  1369. }
  1370. int ast_recvchar(struct ast_channel *chan, int timeout)
  1371. {
  1372. int res,ourto,c;
  1373. struct ast_frame *f;
  1374. ourto = timeout;
  1375. for(;;)
  1376. {
  1377. if (ast_check_hangup(chan)) return -1;
  1378. res = ast_waitfor(chan,ourto);
  1379. if (res <= 0) /* if timeout */
  1380. {
  1381. return 0;
  1382. }
  1383. ourto = res;
  1384. f = ast_read(chan);
  1385. if (f == NULL) return -1; /* if hangup */
  1386. if ((f->frametype == AST_FRAME_CONTROL) &&
  1387. (f->subclass == AST_CONTROL_HANGUP)) return -1; /* if hangup */
  1388. if (f->frametype == AST_FRAME_TEXT) /* if a text frame */
  1389. {
  1390. c = *((char *)f->data); /* get the data */
  1391. ast_frfree(f);
  1392. return(c);
  1393. }
  1394. ast_frfree(f);
  1395. }
  1396. }
  1397. int ast_sendtext(struct ast_channel *chan, char *text)
  1398. {
  1399. int res = 0;
  1400. /* Stop if we're a zombie or need a soft hangup */
  1401. if (chan->zombie || ast_check_hangup(chan))
  1402. return -1;
  1403. CHECK_BLOCKING(chan);
  1404. if (chan->pvt->send_text)
  1405. res = chan->pvt->send_text(chan, text);
  1406. chan->blocking = 0;
  1407. return res;
  1408. }
  1409. static int do_senddigit(struct ast_channel *chan, char digit)
  1410. {
  1411. int res = -1;
  1412. if (chan->pvt->send_digit)
  1413. res = chan->pvt->send_digit(chan, digit);
  1414. if (!chan->pvt->send_digit || res) {
  1415. /*
  1416. * Device does not support DTMF tones, lets fake
  1417. * it by doing our own generation. (PM2002)
  1418. */
  1419. static const char* dtmf_tones[] = {
  1420. "!941+1336/100,!0/100", /* 0 */
  1421. "!697+1209/100,!0/100", /* 1 */
  1422. "!697+1336/100,!0/100", /* 2 */
  1423. "!697+1477/100,!0/100", /* 3 */
  1424. "!770+1209/100,!0/100", /* 4 */
  1425. "!770+1336/100,!0/100", /* 5 */
  1426. "!770+1477/100,!0/100", /* 6 */
  1427. "!852+1209/100,!0/100", /* 7 */
  1428. "!852+1336/100,!0/100", /* 8 */
  1429. "!852+1477/100,!0/100", /* 9 */
  1430. "!697+1633/100,!0/100", /* A */
  1431. "!770+1633/100,!0/100", /* B */
  1432. "!852+1633/100,!0/100", /* C */
  1433. "!941+1633/100,!0/100", /* D */
  1434. "!941+1209/100,!0/100", /* * */
  1435. "!941+1477/100,!0/100" }; /* # */
  1436. if (digit >= '0' && digit <='9')
  1437. ast_playtones_start(chan,0,dtmf_tones[digit-'0'], 0);
  1438. else if (digit >= 'A' && digit <= 'D')
  1439. ast_playtones_start(chan,0,dtmf_tones[digit-'A'+10], 0);
  1440. else if (digit == '*')
  1441. ast_playtones_start(chan,0,dtmf_tones[14], 0);
  1442. else if (digit == '#')
  1443. ast_playtones_start(chan,0,dtmf_tones[15], 0);
  1444. else {
  1445. /* not handled */
  1446. ast_log(LOG_DEBUG, "Unable to handle DTMF tone '%c' for '%s'\n", digit, chan->name);
  1447. }
  1448. }
  1449. return 0;
  1450. }
  1451. int ast_senddigit(struct ast_channel *chan, char digit)
  1452. {
  1453. return do_senddigit(chan, digit);
  1454. }
  1455. int ast_prod(struct ast_channel *chan)
  1456. {
  1457. struct ast_frame a = { AST_FRAME_VOICE };
  1458. char nothing[128];
  1459. /* Send an empty audio frame to get things moving */
  1460. if (chan->_state != AST_STATE_UP) {
  1461. ast_log(LOG_DEBUG, "Prodding channel '%s'\n", chan->name);
  1462. a.subclass = chan->pvt->rawwriteformat;
  1463. a.data = nothing + AST_FRIENDLY_OFFSET;
  1464. if (ast_write(chan, &a))
  1465. ast_log(LOG_WARNING, "Prodding channel '%s' failed\n", chan->name);
  1466. }
  1467. return 0;
  1468. }
  1469. int ast_write_video(struct ast_channel *chan, struct ast_frame *fr)
  1470. {
  1471. int res;
  1472. if (!chan->pvt->write_video)
  1473. return 0;
  1474. res = ast_write(chan, fr);
  1475. if (!res)
  1476. res = 1;
  1477. return res;
  1478. }
  1479. int ast_write(struct ast_channel *chan, struct ast_frame *fr)
  1480. {
  1481. int res = -1;
  1482. struct ast_frame *f = NULL;
  1483. /* Stop if we're a zombie or need a soft hangup */
  1484. ast_mutex_lock(&chan->lock);
  1485. if (chan->zombie || ast_check_hangup(chan)) {
  1486. ast_mutex_unlock(&chan->lock);
  1487. return -1;
  1488. }
  1489. /* Handle any pending masquerades */
  1490. if (chan->masq) {
  1491. if (ast_do_masquerade(chan)) {
  1492. ast_log(LOG_WARNING, "Failed to perform masquerade\n");
  1493. ast_mutex_unlock(&chan->lock);
  1494. return -1;
  1495. }
  1496. }
  1497. if (chan->masqr) {
  1498. ast_mutex_unlock(&chan->lock);
  1499. return 0;
  1500. }
  1501. if (chan->generatordata) {
  1502. if (chan->writeinterrupt)
  1503. ast_deactivate_generator(chan);
  1504. else {
  1505. ast_mutex_unlock(&chan->lock);
  1506. return 0;
  1507. }
  1508. }
  1509. /* High bit prints debugging */
  1510. if (chan->fout & 0x80000000)
  1511. ast_frame_dump(chan->name, fr, ">>");
  1512. CHECK_BLOCKING(chan);
  1513. switch(fr->frametype) {
  1514. case AST_FRAME_CONTROL:
  1515. /* XXX Interpret control frames XXX */
  1516. ast_log(LOG_WARNING, "Don't know how to handle control frames yet\n");
  1517. break;
  1518. case AST_FRAME_DTMF:
  1519. chan->blocking = 0;
  1520. ast_mutex_unlock(&chan->lock);
  1521. res = do_senddigit(chan,fr->subclass);
  1522. ast_mutex_lock(&chan->lock);
  1523. CHECK_BLOCKING(chan);
  1524. break;
  1525. case AST_FRAME_TEXT:
  1526. if (chan->pvt->send_text)
  1527. res = chan->pvt->send_text(chan, (char *) fr->data);
  1528. else
  1529. res = 0;
  1530. break;
  1531. case AST_FRAME_HTML:
  1532. if (chan->pvt->send_html)
  1533. res = chan->pvt->send_html(chan, fr->subclass, (char *) fr->data, fr->datalen);
  1534. else
  1535. res = 0;
  1536. break;
  1537. case AST_FRAME_VIDEO:
  1538. /* XXX Handle translation of video codecs one day XXX */
  1539. if (chan->pvt->write_video)
  1540. res = chan->pvt->write_video(chan, fr);
  1541. else
  1542. res = 0;
  1543. break;
  1544. default:
  1545. if (chan->pvt->write) {
  1546. if (chan->pvt->writetrans) {
  1547. f = ast_translate(chan->pvt->writetrans, fr, 0);
  1548. } else
  1549. f = fr;
  1550. if (f) {
  1551. res = chan->pvt->write(chan, f);
  1552. if( chan->monitor &&
  1553. chan->monitor->write_stream &&
  1554. f && ( f->frametype == AST_FRAME_VOICE ) ) {
  1555. #ifndef MONITOR_CONSTANT_DELAY
  1556. int jump = chan->insmpl - chan->outsmpl - 2 * f->samples;
  1557. if (jump >= 0) {
  1558. if (ast_seekstream(chan->monitor->write_stream, jump + f->samples, SEEK_FORCECUR) == -1)
  1559. ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
  1560. chan->outsmpl += jump + 2 * f->samples;
  1561. } else
  1562. chan->outsmpl += f->samples;
  1563. #else
  1564. int jump = chan->insmpl - chan->outsmpl;
  1565. if (jump - MONITOR_DELAY >= 0) {
  1566. if (ast_seekstream(chan->monitor->write_stream, jump - f->samples, SEEK_FORCECUR) == -1)
  1567. ast_log(LOG_WARNING, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
  1568. chan->outsmpl += jump;
  1569. } else
  1570. chan->outsmpl += f->samples;
  1571. #endif
  1572. if (ast_writestream(chan->monitor->write_stream, f) < 0)
  1573. ast_log(LOG_WARNING, "Failed to write data to channel monitor write stream\n");
  1574. }
  1575. } else
  1576. res = 0;
  1577. }
  1578. }
  1579. if (f && (f != fr))
  1580. ast_frfree(f);
  1581. chan->blocking = 0;
  1582. /* Consider a write failure to force a soft hangup */
  1583. if (res < 0)
  1584. chan->_softhangup |= AST_SOFTHANGUP_DEV;
  1585. else {
  1586. if ((chan->fout & 0x7fffffff) == 0x7fffffff)
  1587. chan->fout &= 0x80000000;
  1588. else
  1589. chan->fout++;
  1590. }
  1591. ast_mutex_unlock(&chan->lock);
  1592. return res;
  1593. }
  1594. int ast_set_write_format(struct ast_channel *chan, int fmts)
  1595. {
  1596. int fmt;
  1597. int native;
  1598. int res;
  1599. ast_mutex_lock(&chan->lock);
  1600. native = chan->nativeformats;
  1601. fmt = fmts;
  1602. res = ast_translator_best_choice(&native, &fmt);
  1603. if (res < 0) {
  1604. ast_log(LOG_NOTICE, "Unable to find a path from %s to %s\n",
  1605. ast_getformatname(fmts), ast_getformatname(chan->nativeformats));
  1606. ast_mutex_unlock(&chan->lock);
  1607. return -1;
  1608. }
  1609. /* Now we have a good choice for both. We'll write using our native format. */
  1610. chan->pvt->rawwriteformat = native;
  1611. /* User perspective is fmt */
  1612. chan->writeformat = fmt;
  1613. /* Free any write translation we have right now */
  1614. if (chan->pvt->writetrans)
  1615. ast_translator_free_path(chan->pvt->writetrans);
  1616. /* Build a translation path from the user write format to the raw writing format */
  1617. chan->pvt->writetrans = ast_translator_build_path(chan->pvt->rawwriteformat, chan->writeformat);
  1618. if (option_debug)
  1619. ast_log(LOG_DEBUG, "Set channel %s to write format %s\n", chan->name, ast_getformatname(chan->writeformat));
  1620. ast_mutex_unlock(&chan->lock);
  1621. return 0;
  1622. }
  1623. int ast_set_read_format(struct ast_channel *chan, int fmts)
  1624. {
  1625. int fmt;
  1626. int native;
  1627. int res;
  1628. ast_mutex_lock(&chan->lock);
  1629. native = chan->nativeformats;
  1630. fmt = fmts;
  1631. /* Find a translation path from the native read format to one of the user's read formats */
  1632. res = ast_translator_best_choice(&fmt, &native);
  1633. if (res < 0) {
  1634. ast_log(LOG_NOTICE, "Unable to find a path from %s to %s\n",
  1635. ast_getformatname(chan->nativeformats), ast_getformatname(fmts));
  1636. ast_mutex_unlock(&chan->lock);
  1637. return -1;
  1638. }
  1639. /* Now we have a good choice for both. We'll write using our native format. */
  1640. chan->pvt->rawreadformat = native;
  1641. /* User perspective is fmt */
  1642. chan->readformat = fmt;
  1643. /* Free any read translation we have right now */
  1644. if (chan->pvt->readtrans)
  1645. ast_translator_free_path(chan->pvt->readtrans);
  1646. /* Build a translation path from the raw read format to the user reading format */
  1647. chan->pvt->readtrans = ast_translator_build_path(chan->readformat, chan->pvt->rawreadformat);
  1648. if (option_debug)
  1649. ast_log(LOG_DEBUG, "Set channel %s to read format %s\n",
  1650. chan->name, ast_getformatname(chan->readformat));
  1651. ast_mutex_unlock(&chan->lock);
  1652. return 0;
  1653. }
  1654. struct ast_channel *__ast_request_and_dial(char *type, int format, void *data, int timeout, int *outstate, char *callerid, struct outgoing_helper *oh)
  1655. {
  1656. int state = 0;
  1657. struct ast_channel *chan;
  1658. struct ast_frame *f;
  1659. int res = 0;
  1660. char *variable;
  1661. chan = ast_request(type, format, data);
  1662. if (chan) {
  1663. if (oh) {
  1664. char *tmp, *var;
  1665. /* JDG chanvar */
  1666. if (oh->variable)
  1667. variable = ast_strdupa(oh->variable);
  1668. else
  1669. variable = NULL;
  1670. tmp = variable;
  1671. /* FIXME replace this call with strsep NOT*/
  1672. while( (var = strtok_r(NULL, "|", &tmp)) ) {
  1673. pbx_builtin_setvar( chan, var );
  1674. } /* /JDG */
  1675. if (oh->callerid && *oh->callerid)
  1676. ast_set_callerid(chan, oh->callerid, 1);
  1677. if (oh->account && *oh->account)
  1678. ast_cdr_setaccount(chan, oh->account);
  1679. }
  1680. if (callerid && !ast_strlen_zero(callerid))
  1681. ast_set_callerid(chan, callerid, 1);
  1682. if (!ast_call(chan, data, 0)) {
  1683. while(timeout && (chan->_state != AST_STATE_UP)) {
  1684. res = ast_waitfor(chan, timeout);
  1685. if (res < 0) {
  1686. /* Something not cool, or timed out */
  1687. break;
  1688. }
  1689. /* If done, break out */
  1690. if (!res)
  1691. break;
  1692. if (timeout > -1)
  1693. timeout = res;
  1694. f = ast_read(chan);
  1695. if (!f) {
  1696. state = AST_CONTROL_HANGUP;
  1697. res = 0;
  1698. break;
  1699. }
  1700. if (f->frametype == AST_FRAME_CONTROL) {
  1701. if (f->subclass == AST_CONTROL_RINGING)
  1702. state = AST_CONTROL_RINGING;
  1703. else if ((f->subclass == AST_CONTROL_BUSY) || (f->subclass == AST_CONTROL_CONGESTION)) {
  1704. state = f->subclass;
  1705. ast_frfree(f);
  1706. break;
  1707. } else if (f->subclass == AST_CONTROL_ANSWER) {
  1708. state = f->subclass;
  1709. ast_frfree(f);
  1710. break;
  1711. } else if (f->subclass == AST_CONTROL_PROGRESS) {
  1712. /* Ignore */
  1713. } else if (f->subclass == -1) {
  1714. /* Ignore -- just stopping indications */
  1715. } else {
  1716. ast_log(LOG_NOTICE, "Don't know what to do with control frame %d\n", f->subclass);
  1717. }
  1718. }
  1719. ast_frfree(f);
  1720. }
  1721. } else
  1722. ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
  1723. } else
  1724. ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
  1725. if (chan) {
  1726. /* Final fixups */
  1727. if (oh) {
  1728. if (oh->context && *oh->context)
  1729. strncpy(chan->context, oh->context, sizeof(chan->context) - 1);
  1730. if (oh->exten && *oh->exten)
  1731. strncpy(chan->exten, oh->exten, sizeof(chan->exten) - 1);
  1732. chan->priority = oh->priority;
  1733. }
  1734. if (chan->_state == AST_STATE_UP)
  1735. state = AST_CONTROL_ANSWER;
  1736. }
  1737. if (outstate)
  1738. *outstate = state;
  1739. if (chan && res <= 0) {
  1740. if (!chan->cdr) {
  1741. chan->cdr = ast_cdr_alloc();
  1742. if (chan->cdr)
  1743. ast_cdr_init(chan->cdr, chan);
  1744. }
  1745. if (chan->cdr) {
  1746. char tmp[256];
  1747. snprintf(tmp, 256, "%s/%s", type, (char *)data);
  1748. ast_cdr_setapp(chan->cdr,"Dial",tmp);
  1749. ast_cdr_update(chan);
  1750. ast_cdr_start(chan->cdr);
  1751. ast_cdr_end(chan->cdr);
  1752. /* If the cause wasn't handled properly */
  1753. if (ast_cdr_disposition(chan->cdr,chan->hangupcause))
  1754. ast_cdr_failed(chan->cdr);
  1755. } else
  1756. ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
  1757. ast_hangup(chan);
  1758. chan = NULL;
  1759. }
  1760. return chan;
  1761. }
  1762. struct ast_channel *ast_request_and_dial(char *type, int format, void *data, int timeout, int *outstate, char *callerid)
  1763. {
  1764. return __ast_request_and_dial(type, format, data, timeout, outstate, callerid, NULL);
  1765. }
  1766. struct ast_channel *ast_request(char *type, int format, void *data)
  1767. {
  1768. struct chanlist *chan;
  1769. struct ast_channel *c = NULL;
  1770. int capabilities;
  1771. int fmt;
  1772. int res;
  1773. if (ast_mutex_lock(&chlock)) {
  1774. ast_log(LOG_WARNING, "Unable to lock channel list\n");
  1775. return NULL;
  1776. }
  1777. chan = backends;
  1778. while(chan) {
  1779. if (!strcasecmp(type, chan->type)) {
  1780. capabilities = chan->capabilities;
  1781. fmt = format;
  1782. res = ast_translator_best_choice(&fmt, &capabilities);
  1783. if (res < 0) {
  1784. ast_log(LOG_WARNING, "No translator path exists for channel type %s (native %d) to %d\n", type, chan->capabilities, format);
  1785. ast_mutex_unlock(&chlock);
  1786. return NULL;
  1787. }
  1788. ast_mutex_unlock(&chlock);
  1789. if (chan->requester)
  1790. c = chan->requester(type, capabilities, data);
  1791. if (c) {
  1792. if (c->_state == AST_STATE_DOWN) {
  1793. manager_event(EVENT_FLAG_CALL, "Newchannel",
  1794. "Channel: %s\r\n"
  1795. "State: %s\r\n"
  1796. "CallerID: %s\r\n"
  1797. "Uniqueid: %s\r\n",
  1798. c->name, ast_state2str(c->_state), c->callerid ? c->callerid : "<unknown>", c->uniqueid);
  1799. }
  1800. }
  1801. return c;
  1802. }
  1803. chan = chan->next;
  1804. }
  1805. if (!chan)
  1806. ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
  1807. ast_mutex_unlock(&chlock);
  1808. return c;
  1809. }
  1810. int ast_parse_device_state(char *device)
  1811. {
  1812. char name[AST_CHANNEL_NAME] = "";
  1813. char *cut;
  1814. struct ast_channel *chan;
  1815. chan = ast_channel_walk_locked(NULL);
  1816. while (chan) {
  1817. strncpy(name, chan->name, sizeof(name)-1);
  1818. ast_mutex_unlock(&chan->lock);
  1819. cut = strchr(name,'-');
  1820. if (cut)
  1821. *cut = 0;
  1822. if (!strcmp(name, device))
  1823. return AST_DEVICE_INUSE;
  1824. chan = ast_channel_walk_locked(chan);
  1825. }
  1826. return AST_DEVICE_UNKNOWN;
  1827. }
  1828. int ast_device_state(char *device)
  1829. {
  1830. char tech[AST_MAX_EXTENSION] = "";
  1831. char *number;
  1832. struct chanlist *chanls;
  1833. int res = 0;
  1834. strncpy(tech, device, sizeof(tech)-1);
  1835. number = strchr(tech, '/');
  1836. if (!number) {
  1837. return AST_DEVICE_INVALID;
  1838. }
  1839. *number = 0;
  1840. number++;
  1841. if (ast_mutex_lock(&chlock)) {
  1842. ast_log(LOG_WARNING, "Unable to lock channel list\n");
  1843. return -1;
  1844. }
  1845. chanls = backends;
  1846. while(chanls) {
  1847. if (!strcasecmp(tech, chanls->type)) {
  1848. ast_mutex_unlock(&chlock);
  1849. if (!chanls->devicestate)
  1850. return ast_parse_device_state(device);
  1851. else {
  1852. res = chanls->devicestate(number);
  1853. if (res == AST_DEVICE_UNKNOWN)
  1854. return ast_parse_device_state(device);
  1855. else
  1856. return res;
  1857. }
  1858. }
  1859. chanls = chanls->next;
  1860. }
  1861. ast_mutex_unlock(&chlock);
  1862. return AST_DEVICE_INVALID;
  1863. }
  1864. int ast_call(struct ast_channel *chan, char *addr, int timeout)
  1865. {
  1866. /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
  1867. If the remote end does not answer within the timeout, then do NOT hang up, but
  1868. return anyway. */
  1869. int res = -1;
  1870. /* Stop if we're a zombie or need a soft hangup */
  1871. ast_mutex_lock(&chan->lock);
  1872. if (!chan->zombie && !ast_check_hangup(chan))
  1873. if (chan->pvt->call)
  1874. res = chan->pvt->call(chan, addr, timeout);
  1875. ast_mutex_unlock(&chan->lock);
  1876. return res;
  1877. }
  1878. int ast_transfer(struct ast_channel *chan, char *dest)
  1879. {
  1880. /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
  1881. If the remote end does not answer within the timeout, then do NOT hang up, but
  1882. return anyway. */
  1883. int res = -1;
  1884. /* Stop if we're a zombie or need a soft hangup */
  1885. ast_mutex_lock(&chan->lock);
  1886. if (!chan->zombie && !ast_check_hangup(chan)) {
  1887. if (chan->pvt->transfer) {
  1888. res = chan->pvt->transfer(chan, dest);
  1889. if (!res)
  1890. res = 1;
  1891. } else
  1892. res = 0;
  1893. }
  1894. ast_mutex_unlock(&chan->lock);
  1895. return res;
  1896. }
  1897. int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders)
  1898. {
  1899. int pos=0;
  1900. int to = ftimeout;
  1901. int d;
  1902. /* XXX Merge with full version? XXX */
  1903. /* Stop if we're a zombie or need a soft hangup */
  1904. if (c->zombie || ast_check_hangup(c))
  1905. return -1;
  1906. if (!len)
  1907. return -1;
  1908. do {
  1909. if (c->stream) {
  1910. d = ast_waitstream(c, AST_DIGIT_ANY);
  1911. ast_stopstream(c);
  1912. usleep(1000);
  1913. if (!d)
  1914. d = ast_waitfordigit(c, to);
  1915. } else {
  1916. d = ast_waitfordigit(c, to);
  1917. }
  1918. if (d < 0)
  1919. return -1;
  1920. if (d == 0) {
  1921. s[pos]='\0';
  1922. return 1;
  1923. }
  1924. if (!strchr(enders, d))
  1925. s[pos++] = d;
  1926. if (strchr(enders, d) || (pos >= len)) {
  1927. s[pos]='\0';
  1928. return 0;
  1929. }
  1930. to = timeout;
  1931. } while(1);
  1932. /* Never reached */
  1933. return 0;
  1934. }
  1935. int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int ftimeout, char *enders, int audiofd, int ctrlfd)
  1936. {
  1937. int pos=0;
  1938. int to = ftimeout;
  1939. int d;
  1940. /* Stop if we're a zombie or need a soft hangup */
  1941. if (c->zombie || ast_check_hangup(c))
  1942. return -1;
  1943. if (!len)
  1944. return -1;
  1945. do {
  1946. if (c->stream) {
  1947. d = ast_waitstream_full(c, AST_DIGIT_ANY, audiofd, ctrlfd);
  1948. ast_stopstream(c);
  1949. usleep(1000);
  1950. if (!d)
  1951. d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
  1952. } else {
  1953. d = ast_waitfordigit_full(c, to, audiofd, ctrlfd);
  1954. }
  1955. if (d < 0)
  1956. return -1;
  1957. if (d == 0) {
  1958. s[pos]='\0';
  1959. return 1;
  1960. }
  1961. if (d == 1) {
  1962. s[pos]='\0';
  1963. return 2;
  1964. }
  1965. if (!strchr(enders, d))
  1966. s[pos++] = d;
  1967. if (strchr(enders, d) || (pos >= len)) {
  1968. s[pos]='\0';
  1969. return 0;
  1970. }
  1971. to = timeout;
  1972. } while(1);
  1973. /* Never reached */
  1974. return 0;
  1975. }
  1976. int ast_channel_supports_html(struct ast_channel *chan)
  1977. {
  1978. if (chan->pvt->send_html)
  1979. return 1;
  1980. return 0;
  1981. }
  1982. int ast_channel_sendhtml(struct ast_channel *chan, int subclass, char *data, int datalen)
  1983. {
  1984. if (chan->pvt->send_html)
  1985. return chan->pvt->send_html(chan, subclass, data, datalen);
  1986. return -1;
  1987. }
  1988. int ast_channel_sendurl(struct ast_channel *chan, char *url)
  1989. {
  1990. if (chan->pvt->send_html)
  1991. return chan->pvt->send_html(chan, AST_HTML_URL, url, strlen(url) + 1);
  1992. return -1;
  1993. }
  1994. int ast_channel_make_compatible(struct ast_channel *chan, struct ast_channel *peer)
  1995. {
  1996. int peerf;
  1997. int chanf;
  1998. int res;
  1999. ast_mutex_lock(&peer->lock);
  2000. peerf = peer->nativeformats;
  2001. ast_mutex_unlock(&peer->lock);
  2002. ast_mutex_lock(&chan->lock);
  2003. chanf = chan->nativeformats;
  2004. ast_mutex_unlock(&chan->lock);
  2005. res = ast_translator_best_choice(&peerf, &chanf);
  2006. if (res < 0) {
  2007. ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", chan->name, chan->nativeformats, peer->name, peer->nativeformats);
  2008. return -1;
  2009. }
  2010. /* Set read format on channel */
  2011. res = ast_set_read_format(chan, peerf);
  2012. if (res < 0) {
  2013. ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", chan->name, chanf);
  2014. return -1;
  2015. }
  2016. /* Set write format on peer channel */
  2017. res = ast_set_write_format(peer, peerf);
  2018. if (res < 0) {
  2019. ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", peer->name, peerf);
  2020. return -1;
  2021. }
  2022. /* Now we go the other way */
  2023. peerf = peer->nativeformats;
  2024. chanf = chan->nativeformats;
  2025. res = ast_translator_best_choice(&chanf, &peerf);
  2026. if (res < 0) {
  2027. ast_log(LOG_WARNING, "No path to translate from %s(%d) to %s(%d)\n", peer->name, peer->nativeformats, chan->name, chan->nativeformats);
  2028. return -1;
  2029. }
  2030. /* Set writeformat on channel */
  2031. res = ast_set_write_format(chan, chanf);
  2032. if (res < 0) {
  2033. ast_log(LOG_WARNING, "Unable to set write format on channel %s to %d\n", chan->name, chanf);
  2034. return -1;
  2035. }
  2036. /* Set read format on peer channel */
  2037. res = ast_set_read_format(peer, chanf);
  2038. if (res < 0) {
  2039. ast_log(LOG_WARNING, "Unable to set read format on channel %s to %d\n", peer->name, peerf);
  2040. return -1;
  2041. }
  2042. return 0;
  2043. }
  2044. int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clone)
  2045. {
  2046. struct ast_frame null = { AST_FRAME_NULL, };
  2047. int res = -1;
  2048. ast_mutex_lock(&original->lock);
  2049. while(ast_mutex_trylock(&clone->lock)) {
  2050. ast_mutex_unlock(&original->lock);
  2051. usleep(1);
  2052. ast_mutex_lock(&original->lock);
  2053. }
  2054. ast_log(LOG_DEBUG, "Planning to masquerade %s into the structure of %s\n",
  2055. clone->name, original->name);
  2056. if (original->masq) {
  2057. ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
  2058. original->masq->name, original->name);
  2059. } else if (clone->masqr) {
  2060. ast_log(LOG_WARNING, "%s is already going to masquerade as %s\n",
  2061. clone->name, clone->masqr->name);
  2062. } else {
  2063. original->masq = clone;
  2064. clone->masqr = original;
  2065. ast_queue_frame(original, &null);
  2066. ast_queue_frame(clone, &null);
  2067. ast_log(LOG_DEBUG, "Done planning to masquerade %s into the structure of %s\n", original->name, clone->name);
  2068. res = 0;
  2069. }
  2070. ast_mutex_unlock(&clone->lock);
  2071. ast_mutex_unlock(&original->lock);
  2072. return res;
  2073. }
  2074. void ast_change_name(struct ast_channel *chan, char *newname)
  2075. {
  2076. char tmp[256];
  2077. strncpy(tmp, chan->name, sizeof(tmp) - 1);
  2078. strncpy(chan->name, newname, sizeof(chan->name) - 1);
  2079. manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", tmp, chan->name, chan->uniqueid);
  2080. }
  2081. int ast_do_masquerade(struct ast_channel *original)
  2082. {
  2083. int x,i;
  2084. int res=0;
  2085. int origstate;
  2086. char *tmp;
  2087. struct ast_var_t *varptr;
  2088. struct ast_frame *cur, *prev;
  2089. struct ast_channel_pvt *p;
  2090. struct ast_channel *clone = original->masq;
  2091. struct ast_channel_monitor *monitor;
  2092. int rformat = original->readformat;
  2093. int wformat = original->writeformat;
  2094. char newn[100];
  2095. char orig[100];
  2096. char masqn[100];
  2097. char zombn[100];
  2098. #if 1
  2099. ast_log(LOG_DEBUG, "Actually Masquerading %s(%d) into the structure of %s(%d)\n",
  2100. clone->name, clone->_state, original->name, original->_state);
  2101. #endif
  2102. /* XXX This is a seriously wacked out operation. We're essentially putting the guts of
  2103. the clone channel into the original channel. Start by killing off the original
  2104. channel's backend. I'm not sure we're going to keep this function, because
  2105. while the features are nice, the cost is very high in terms of pure nastiness. XXX */
  2106. /* We need the clone's lock, too */
  2107. ast_mutex_lock(&clone->lock);
  2108. ast_log(LOG_DEBUG, "Got clone lock on '%s' at %p\n", clone->name, &clone->lock);
  2109. /* Having remembered the original read/write formats, we turn off any translation on either
  2110. one */
  2111. free_translation(clone);
  2112. free_translation(original);
  2113. /* Unlink the masquerade */
  2114. original->masq = NULL;
  2115. clone->masqr = NULL;
  2116. /* Save the original name */
  2117. strncpy(orig, original->name, sizeof(orig) - 1);
  2118. /* Save the new name */
  2119. strncpy(newn, clone->name, sizeof(newn) - 1);
  2120. /* Create the masq name */
  2121. snprintf(masqn, sizeof(masqn), "%s<MASQ>", newn);
  2122. /* Copy the name from the clone channel */
  2123. strncpy(original->name, newn, sizeof(original->name)-1);
  2124. /* Mangle the name of the clone channel */
  2125. strncpy(clone->name, masqn, sizeof(clone->name) - 1);
  2126. /* Notify any managers of the change, first the masq then the other */
  2127. manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", newn, masqn, clone->uniqueid);
  2128. manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", orig, newn, original->uniqueid);
  2129. /* Swap the guts */
  2130. p = original->pvt;
  2131. original->pvt = clone->pvt;
  2132. clone->pvt = p;
  2133. /* Save any pending frames on both sides. Start by counting
  2134. * how many we're going to need... */
  2135. prev = NULL;
  2136. cur = clone->pvt->readq;
  2137. x = 0;
  2138. while(cur) {
  2139. x++;
  2140. prev = cur;
  2141. cur = cur->next;
  2142. }
  2143. /* If we had any, prepend them to the ones already in the queue, and
  2144. * load up the alertpipe */
  2145. if (prev) {
  2146. prev->next = original->pvt->readq;
  2147. original->pvt->readq = clone->pvt->readq;
  2148. clone->pvt->readq = NULL;
  2149. if (original->pvt->alertpipe[1] > -1) {
  2150. for (i=0;i<x;i++)
  2151. write(original->pvt->alertpipe[1], &x, sizeof(x));
  2152. }
  2153. }
  2154. clone->_softhangup = AST_SOFTHANGUP_DEV;
  2155. /* And of course, so does our current state. Note we need not
  2156. call ast_setstate since the event manager doesn't really consider
  2157. these separate. We do this early so that the clone has the proper
  2158. state of the original channel. */
  2159. origstate = original->_state;
  2160. original->_state = clone->_state;
  2161. clone->_state = origstate;
  2162. if (clone->pvt->fixup){
  2163. res = clone->pvt->fixup(original, clone);
  2164. if (res)
  2165. ast_log(LOG_WARNING, "Fixup failed on channel %s, strange things may happen.\n", clone->name);
  2166. }
  2167. /* Start by disconnecting the original's physical side */
  2168. if (clone->pvt->hangup)
  2169. res = clone->pvt->hangup(clone);
  2170. if (res) {
  2171. ast_log(LOG_WARNING, "Hangup failed! Strange things may happen!\n");
  2172. ast_mutex_unlock(&clone->lock);
  2173. return -1;
  2174. }
  2175. snprintf(zombn, sizeof(zombn), "%s<ZOMBIE>", orig);
  2176. /* Mangle the name of the clone channel */
  2177. strncpy(clone->name, zombn, sizeof(clone->name) - 1);
  2178. manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", masqn, zombn, clone->uniqueid);
  2179. /* Update the type. */
  2180. original->type = clone->type;
  2181. /* copy the monitor */
  2182. monitor = original->monitor;
  2183. original->monitor = clone->monitor;
  2184. clone->monitor = monitor;
  2185. /* Keep the same language. */
  2186. strncpy(original->language, clone->language, sizeof(original->language));
  2187. /* Copy the FD's */
  2188. for (x=0;x<AST_MAX_FDS;x++) {
  2189. original->fds[x] = clone->fds[x];
  2190. }
  2191. /* Append variables from clone channel into original channel */
  2192. /* XXX Is this always correct? We have to in order to keep MACROS working XXX */
  2193. varptr = original->varshead.first;
  2194. if (varptr) {
  2195. while(varptr->entries.next) {
  2196. varptr = varptr->entries.next;
  2197. }
  2198. varptr->entries.next = clone->varshead.first;
  2199. } else {
  2200. original->varshead.first = clone->varshead.first;
  2201. }
  2202. clone->varshead.first = NULL;
  2203. /* Presense of ADSI capable CPE follows clone */
  2204. original->adsicpe = clone->adsicpe;
  2205. /* Bridge remains the same */
  2206. /* CDR fields remain the same */
  2207. /* XXX What about blocking, softhangup, blocker, and lock and blockproc? XXX */
  2208. /* Application and data remain the same */
  2209. /* Clone exception becomes real one, as with fdno */
  2210. original->exception = clone->exception;
  2211. original->fdno = clone->fdno;
  2212. /* Schedule context remains the same */
  2213. /* Stream stuff stays the same */
  2214. /* Keep the original state. The fixup code will need to work with it most likely */
  2215. /* dnid and callerid change to become the new, HOWEVER, we also link the original's
  2216. fields back into the defunct 'clone' so that they will be freed when
  2217. ast_frfree is eventually called */
  2218. tmp = original->dnid;
  2219. original->dnid = clone->dnid;
  2220. clone->dnid = tmp;
  2221. tmp = original->callerid;
  2222. original->callerid = clone->callerid;
  2223. clone->callerid = tmp;
  2224. /* Restore original timing file descriptor */
  2225. original->fds[AST_MAX_FDS - 2] = original->timingfd;
  2226. /* Our native formats are different now */
  2227. original->nativeformats = clone->nativeformats;
  2228. /* Context, extension, priority, app data, jump table, remain the same */
  2229. /* pvt switches. pbx stays the same, as does next */
  2230. /* Set the write format */
  2231. ast_set_write_format(original, wformat);
  2232. /* Set the read format */
  2233. ast_set_read_format(original, rformat);
  2234. /* Copy the music class */
  2235. strncpy(original->musicclass, clone->musicclass, sizeof(original->musicclass) - 1);
  2236. ast_log(LOG_DEBUG, "Putting channel %s in %d/%d formats\n", original->name, wformat, rformat);
  2237. /* Okay. Last thing is to let the channel driver know about all this mess, so he
  2238. can fix up everything as best as possible */
  2239. if (original->pvt->fixup) {
  2240. res = original->pvt->fixup(clone, original);
  2241. if (res) {
  2242. ast_log(LOG_WARNING, "Driver for '%s' could not fixup channel %s\n",
  2243. original->type, original->name);
  2244. ast_mutex_unlock(&clone->lock);
  2245. return -1;
  2246. }
  2247. } else
  2248. ast_log(LOG_WARNING, "Driver '%s' does not have a fixup routine (for %s)! Bad things may happen.\n",
  2249. original->type, original->name);
  2250. /* Now, at this point, the "clone" channel is totally F'd up. We mark it as
  2251. a zombie so nothing tries to touch it. If it's already been marked as a
  2252. zombie, then free it now (since it already is considered invalid). */
  2253. if (clone->zombie) {
  2254. ast_log(LOG_DEBUG, "Destroying clone '%s'\n", clone->name);
  2255. ast_mutex_unlock(&clone->lock);
  2256. ast_channel_free(clone);
  2257. manager_event(EVENT_FLAG_CALL, "Hangup", "Channel: %s\r\n", zombn);
  2258. } else {
  2259. struct ast_frame null_frame = { AST_FRAME_NULL, };
  2260. ast_log(LOG_DEBUG, "Released clone lock on '%s'\n", clone->name);
  2261. clone->zombie=1;
  2262. ast_queue_frame(clone, &null_frame);
  2263. ast_mutex_unlock(&clone->lock);
  2264. }
  2265. /* Signal any blocker */
  2266. if (original->blocking)
  2267. pthread_kill(original->blocker, SIGURG);
  2268. ast_log(LOG_DEBUG, "Done Masquerading %s (%d)\n",
  2269. original->name, original->_state);
  2270. return 0;
  2271. }
  2272. void ast_set_callerid(struct ast_channel *chan, char *callerid, int anitoo)
  2273. {
  2274. if (chan->callerid)
  2275. free(chan->callerid);
  2276. if (anitoo && chan->ani)
  2277. free(chan->ani);
  2278. if (callerid) {
  2279. chan->callerid = strdup(callerid);
  2280. if (anitoo)
  2281. chan->ani = strdup(callerid);
  2282. } else {
  2283. chan->callerid = NULL;
  2284. if (anitoo)
  2285. chan->ani = NULL;
  2286. }
  2287. if (chan->cdr)
  2288. ast_cdr_setcid(chan->cdr, chan);
  2289. manager_event(EVENT_FLAG_CALL, "Newcallerid",
  2290. "Channel: %s\r\n"
  2291. "CallerID: %s\r\n"
  2292. "Uniqueid: %s\r\n",
  2293. chan->name, chan->callerid ?
  2294. chan->callerid : "<Unknown>",
  2295. chan->uniqueid);
  2296. }
  2297. int ast_setstate(struct ast_channel *chan, int state)
  2298. {
  2299. if (chan->_state != state) {
  2300. int oldstate = chan->_state;
  2301. chan->_state = state;
  2302. if (oldstate == AST_STATE_DOWN) {
  2303. ast_device_state_changed(chan->name);
  2304. manager_event(EVENT_FLAG_CALL, "Newchannel",
  2305. "Channel: %s\r\n"
  2306. "State: %s\r\n"
  2307. "CallerID: %s\r\n"
  2308. "Uniqueid: %s\r\n",
  2309. chan->name, ast_state2str(chan->_state), chan->callerid ? chan->callerid : "<unknown>", chan->uniqueid);
  2310. } else {
  2311. manager_event(EVENT_FLAG_CALL, "Newstate",
  2312. "Channel: %s\r\n"
  2313. "State: %s\r\n"
  2314. "CallerID: %s\r\n"
  2315. "Uniqueid: %s\r\n",
  2316. chan->name, ast_state2str(chan->_state), chan->callerid ? chan->callerid : "<unknown>", chan->uniqueid);
  2317. }
  2318. }
  2319. return 0;
  2320. }
  2321. static long tvdiff(struct timeval *now, struct timeval *then)
  2322. {
  2323. return (((now->tv_sec * 1000) + now->tv_usec / 1000) - ((then->tv_sec * 1000) + then->tv_usec / 1000));
  2324. }
  2325. static void bridge_playfile(struct ast_channel *chan, struct ast_channel *peer, char *sound, int remain)
  2326. {
  2327. int res=0, min=0, sec=0,check=0;
  2328. check = ast_autoservice_start(peer);
  2329. if(check)
  2330. return;
  2331. if (remain > 0) {
  2332. if (remain / 60 > 1) {
  2333. min = remain / 60;
  2334. sec = remain % 60;
  2335. } else {
  2336. sec = remain;
  2337. }
  2338. }
  2339. if (!strcmp(sound,"timeleft")) {
  2340. res = ast_streamfile(chan, "vm-youhave", chan->language);
  2341. res = ast_waitstream(chan, "");
  2342. if (min) {
  2343. res = ast_say_number(chan, min, AST_DIGIT_ANY, chan->language, (char *) NULL);
  2344. res = ast_streamfile(chan, "queue-minutes", chan->language);
  2345. res = ast_waitstream(chan, "");
  2346. }
  2347. if (sec) {
  2348. res = ast_say_number(chan, sec, AST_DIGIT_ANY, chan->language, (char *) NULL);
  2349. res = ast_streamfile(chan, "queue-seconds", chan->language);
  2350. res = ast_waitstream(chan, "");
  2351. }
  2352. } else {
  2353. res = ast_streamfile(chan, sound, chan->language);
  2354. res = ast_waitstream(chan, "");
  2355. }
  2356. check = ast_autoservice_stop(peer);
  2357. }
  2358. int ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1, struct ast_bridge_config *config, struct ast_frame **fo, struct ast_channel **rc)
  2359. {
  2360. /* Copy voice back and forth between the two channels. Give the peer
  2361. the ability to transfer calls with '#<extension' syntax. */
  2362. int flags;
  2363. struct ast_channel *cs[3];
  2364. int to = -1;
  2365. struct ast_frame *f;
  2366. struct ast_channel *who = NULL;
  2367. int res=0;
  2368. int nativefailed=0;
  2369. int firstpass;
  2370. int o0nativeformats;
  2371. int o1nativeformats;
  2372. struct timeval start_time,precise_now;
  2373. long elapsed_ms=0, time_left_ms=0;
  2374. int playit=0, playitagain=1, first_time=1;
  2375. flags = (config->allowdisconnect_out||config->allowredirect_out ? AST_BRIDGE_DTMF_CHANNEL_0 : 0) + (config->allowdisconnect_in||config->allowredirect_in ? AST_BRIDGE_DTMF_CHANNEL_1 : 0);
  2376. firstpass = config->firstpass;
  2377. config->firstpass = 0;
  2378. /* timestamp */
  2379. gettimeofday(&start_time,NULL);
  2380. time_left_ms = config->timelimit;
  2381. if (config->play_to_caller && config->start_sound && firstpass)
  2382. bridge_playfile(c0,c1,config->start_sound,time_left_ms / 1000);
  2383. if (config->play_to_callee && config->start_sound && firstpass)
  2384. bridge_playfile(c1,c0,config->start_sound,time_left_ms / 1000);
  2385. /* Stop if we're a zombie or need a soft hangup */
  2386. if (c0->zombie || ast_check_hangup_locked(c0) || c1->zombie || ast_check_hangup_locked(c1))
  2387. return -1;
  2388. if (c0->bridge) {
  2389. ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
  2390. c0->name, c0->bridge->name);
  2391. return -1;
  2392. }
  2393. if (c1->bridge) {
  2394. ast_log(LOG_WARNING, "%s is already in a bridge with %s\n",
  2395. c1->name, c1->bridge->name);
  2396. return -1;
  2397. }
  2398. /* Keep track of bridge */
  2399. c0->bridge = c1;
  2400. c1->bridge = c0;
  2401. cs[0] = c0;
  2402. cs[1] = c1;
  2403. manager_event(EVENT_FLAG_CALL, "Link",
  2404. "Channel1: %s\r\n"
  2405. "Channel2: %s\r\n"
  2406. "Uniqueid1: %s\r\n"
  2407. "Uniqueid2: %s\r\n",
  2408. c0->name, c1->name, c0->uniqueid, c1->uniqueid);
  2409. o1nativeformats = c1->nativeformats;
  2410. o0nativeformats = c0->nativeformats;
  2411. for (/* ever */;;) {
  2412. /* timestamp */
  2413. if (config->timelimit) {
  2414. gettimeofday(&precise_now,NULL);
  2415. elapsed_ms = tvdiff(&precise_now,&start_time);
  2416. time_left_ms = config->timelimit - elapsed_ms;
  2417. if (playitagain && (config->play_to_caller || config->play_to_callee) && (config->play_warning && time_left_ms <= config->play_warning)) {
  2418. /* narrowing down to the end */
  2419. if (config->warning_freq == 0) {
  2420. playit = 1;
  2421. first_time=0;
  2422. playitagain=0;
  2423. } else if (first_time) {
  2424. playit = 1;
  2425. first_time=0;
  2426. } else {
  2427. if ((time_left_ms % config->warning_freq) <= 50) {
  2428. playit = 1;
  2429. }
  2430. }
  2431. }
  2432. if (time_left_ms <= 0) {
  2433. if (config->play_to_caller && config->end_sound)
  2434. bridge_playfile(c0,c1,config->end_sound,0);
  2435. if (config->play_to_callee && config->end_sound)
  2436. bridge_playfile(c1,c0,config->end_sound,0);
  2437. *fo = NULL;
  2438. if (who) *rc = who;
  2439. res = 0;
  2440. break;
  2441. }
  2442. if (time_left_ms >= 5000 && playit) {
  2443. if (config->play_to_caller && config->warning_sound && config->play_warning)
  2444. bridge_playfile(c0,c1,config->warning_sound,time_left_ms / 1000);
  2445. if (config->play_to_callee && config->warning_sound && config->play_warning)
  2446. bridge_playfile(c1,c0,config->warning_sound,time_left_ms / 1000);
  2447. playit = 0;
  2448. }
  2449. }
  2450. /* Stop if we're a zombie or need a soft hangup */
  2451. if (c0->zombie || ast_check_hangup_locked(c0) || c1->zombie || ast_check_hangup_locked(c1)) {
  2452. *fo = NULL;
  2453. if (who) *rc = who;
  2454. res = 0;
  2455. 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",c0->name,c1->name,c0->zombie?"Yes":"No",ast_check_hangup(c0)?"Yes":"No",c1->zombie?"Yes":"No",ast_check_hangup(c1)?"Yes":"No");
  2456. break;
  2457. }
  2458. if (c0->pvt->bridge && config->timelimit==0 &&
  2459. (c0->pvt->bridge == c1->pvt->bridge) && !nativefailed && !c0->monitor && !c1->monitor) {
  2460. /* Looks like they share a bridge code */
  2461. if (option_verbose > 2)
  2462. ast_verbose(VERBOSE_PREFIX_3 "Attempting native bridge of %s and %s\n", c0->name, c1->name);
  2463. if (!(res = c0->pvt->bridge(c0, c1, flags, fo, rc))) {
  2464. c0->bridge = NULL;
  2465. c1->bridge = NULL;
  2466. manager_event(EVENT_FLAG_CALL, "Unlink",
  2467. "Channel1: %s\r\n"
  2468. "Channel2: %s\r\n"
  2469. "Uniqueid1: %s\r\n"
  2470. "Uniqueid2: %s\r\n",
  2471. c0->name, c1->name, c0->uniqueid, c1->uniqueid);
  2472. ast_log(LOG_DEBUG, "Returning from native bridge, channels: %s, %s\n",c0->name ,c1->name);
  2473. return 0;
  2474. }
  2475. /* If they return non-zero then continue on normally. Let "-2" mean don't worry about
  2476. my not wanting to bridge */
  2477. if ((res != -2) && (res != -3))
  2478. ast_log(LOG_WARNING, "Private bridge between %s and %s failed\n", c0->name, c1->name);
  2479. if (res != -3) nativefailed++;
  2480. }
  2481. if (((c0->writeformat != c1->readformat) || (c0->readformat != c1->writeformat) || (c0->nativeformats != o0nativeformats) || (c1->nativeformats != o1nativeformats)) &&
  2482. !(c0->generator || c1->generator)) {
  2483. if (ast_channel_make_compatible(c0, c1)) {
  2484. ast_log(LOG_WARNING, "Can't make %s and %s compatible\n", c0->name, c1->name);
  2485. manager_event(EVENT_FLAG_CALL, "Unlink",
  2486. "Channel1: %s\r\n"
  2487. "Channel2: %s\r\n"
  2488. "Uniqueid1: %s\r\n"
  2489. "Uniqueid2: %s\r\n",
  2490. c0->name, c1->name, c0->uniqueid, c1->uniqueid);
  2491. return -1;
  2492. }
  2493. o0nativeformats = c0->nativeformats;
  2494. o1nativeformats = c1->nativeformats;
  2495. }
  2496. who = ast_waitfor_n(cs, 2, &to);
  2497. if (!who) {
  2498. ast_log(LOG_DEBUG, "Nobody there, continuing...\n");
  2499. continue;
  2500. }
  2501. f = ast_read(who);
  2502. if (!f) {
  2503. *fo = NULL;
  2504. *rc = who;
  2505. res = 0;
  2506. ast_log(LOG_DEBUG, "Didn't get a frame from channel: %s\n",who->name);
  2507. break;
  2508. }
  2509. if ((f->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
  2510. *fo = f;
  2511. *rc = who;
  2512. res = 0;
  2513. ast_log(LOG_DEBUG, "Got a FRAME_CONTROL (%d) frame on channel %s\n", f->subclass, who->name);
  2514. break;
  2515. }
  2516. if ((f->frametype == AST_FRAME_VOICE) ||
  2517. (f->frametype == AST_FRAME_TEXT) ||
  2518. (f->frametype == AST_FRAME_VIDEO) ||
  2519. (f->frametype == AST_FRAME_IMAGE) ||
  2520. (f->frametype == AST_FRAME_HTML) ||
  2521. (f->frametype == AST_FRAME_DTMF)) {
  2522. if ((f->frametype == AST_FRAME_DTMF) &&
  2523. (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))) {
  2524. if ((who == c0)) {
  2525. if ((flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
  2526. *rc = c0;
  2527. *fo = f;
  2528. /* Take out of conference mode */
  2529. res = 0;
  2530. ast_log(LOG_DEBUG, "Got AST_BRIDGE_DTMF_CHANNEL_0 on c0 (%s)\n",c0->name);
  2531. break;
  2532. } else
  2533. goto tackygoto;
  2534. } else
  2535. if ((who == c1)) {
  2536. if (flags & AST_BRIDGE_DTMF_CHANNEL_1) {
  2537. *rc = c1;
  2538. *fo = f;
  2539. res = 0;
  2540. ast_log(LOG_DEBUG, "Got AST_BRIDGE_DTMF_CHANNEL_1 on c1 (%s)\n",c1->name);
  2541. break;
  2542. } else
  2543. goto tackygoto;
  2544. }
  2545. } else {
  2546. #if 0
  2547. ast_log(LOG_DEBUG, "Read from %s\n", who->name);
  2548. if (who == last)
  2549. ast_log(LOG_DEBUG, "Servicing channel %s twice in a row?\n", last->name);
  2550. last = who;
  2551. #endif
  2552. tackygoto:
  2553. /* Don't copy packets if there is a generator on either one, since they're
  2554. not supposed to be listening anyway */
  2555. if (who == c0)
  2556. ast_write(c1, f);
  2557. else
  2558. ast_write(c0, f);
  2559. }
  2560. ast_frfree(f);
  2561. } else
  2562. ast_frfree(f);
  2563. /* Swap who gets priority */
  2564. cs[2] = cs[0];
  2565. cs[0] = cs[1];
  2566. cs[1] = cs[2];
  2567. }
  2568. c0->bridge = NULL;
  2569. c1->bridge = NULL;
  2570. manager_event(EVENT_FLAG_CALL, "Unlink",
  2571. "Channel1: %s\r\n"
  2572. "Channel2: %s\r\n"
  2573. "Uniqueid1: %s\r\n"
  2574. "Uniqueid2: %s\r\n",
  2575. c0->name, c1->name, c0->uniqueid, c1->uniqueid);
  2576. ast_log(LOG_DEBUG, "Bridge stops bridging channels %s and %s\n",c0->name,c1->name);
  2577. return res;
  2578. }
  2579. int ast_channel_setoption(struct ast_channel *chan, int option, void *data, int datalen, int block)
  2580. {
  2581. int res;
  2582. if (chan->pvt->setoption) {
  2583. res = chan->pvt->setoption(chan, option, data, datalen);
  2584. if (res < 0)
  2585. return res;
  2586. } else {
  2587. errno = ENOSYS;
  2588. return -1;
  2589. }
  2590. if (block) {
  2591. /* XXX Implement blocking -- just wait for our option frame reply, discarding
  2592. intermediate packets. XXX */
  2593. ast_log(LOG_ERROR, "XXX Blocking not implemented yet XXX\n");
  2594. return -1;
  2595. }
  2596. return 0;
  2597. }
  2598. struct tonepair_def {
  2599. int freq1;
  2600. int freq2;
  2601. int duration;
  2602. int vol;
  2603. };
  2604. struct tonepair_state {
  2605. float freq1;
  2606. float freq2;
  2607. float vol;
  2608. int duration;
  2609. int pos;
  2610. int origwfmt;
  2611. struct ast_frame f;
  2612. unsigned char offset[AST_FRIENDLY_OFFSET];
  2613. short data[4000];
  2614. };
  2615. static void tonepair_release(struct ast_channel *chan, void *params)
  2616. {
  2617. struct tonepair_state *ts = params;
  2618. if (chan) {
  2619. ast_set_write_format(chan, ts->origwfmt);
  2620. }
  2621. free(ts);
  2622. }
  2623. static void * tonepair_alloc(struct ast_channel *chan, void *params)
  2624. {
  2625. struct tonepair_state *ts;
  2626. struct tonepair_def *td = params;
  2627. ts = malloc(sizeof(struct tonepair_state));
  2628. if (!ts)
  2629. return NULL;
  2630. memset(ts, 0, sizeof(struct tonepair_state));
  2631. ts->origwfmt = chan->writeformat;
  2632. if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
  2633. ast_log(LOG_WARNING, "Unable to set '%s' to signed linear format (write)\n", chan->name);
  2634. tonepair_release(NULL, ts);
  2635. ts = NULL;
  2636. } else {
  2637. ts->freq1 = td->freq1;
  2638. ts->freq2 = td->freq2;
  2639. ts->duration = td->duration;
  2640. ts->vol = td->vol;
  2641. }
  2642. /* Let interrupts interrupt :) */
  2643. chan->writeinterrupt = 1;
  2644. return ts;
  2645. }
  2646. static int tonepair_generator(struct ast_channel *chan, void *data, int len, int samples)
  2647. {
  2648. struct tonepair_state *ts = data;
  2649. int x;
  2650. /* we need to prepare a frame with 16 * timelen samples as we're
  2651. * generating SLIN audio
  2652. */
  2653. len = samples * 2;
  2654. if (len > sizeof(ts->data) / 2 - 1) {
  2655. ast_log(LOG_WARNING, "Can't generate that much data!\n");
  2656. return -1;
  2657. }
  2658. memset(&ts->f, 0, sizeof(ts->f));
  2659. for (x=0;x<len/2;x++) {
  2660. ts->data[x] = ts->vol * (
  2661. sin((ts->freq1 * 2.0 * M_PI / 8000.0) * (ts->pos + x)) +
  2662. sin((ts->freq2 * 2.0 * M_PI / 8000.0) * (ts->pos + x))
  2663. );
  2664. }
  2665. ts->f.frametype = AST_FRAME_VOICE;
  2666. ts->f.subclass = AST_FORMAT_SLINEAR;
  2667. ts->f.datalen = len;
  2668. ts->f.samples = samples;
  2669. ts->f.offset = AST_FRIENDLY_OFFSET;
  2670. ts->f.data = ts->data;
  2671. ast_write(chan, &ts->f);
  2672. ts->pos += x;
  2673. if (ts->duration > 0) {
  2674. if (ts->pos >= ts->duration * 8)
  2675. return -1;
  2676. }
  2677. return 0;
  2678. }
  2679. static struct ast_generator tonepair = {
  2680. alloc: tonepair_alloc,
  2681. release: tonepair_release,
  2682. generate: tonepair_generator,
  2683. };
  2684. int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
  2685. {
  2686. struct tonepair_def d = { 0, };
  2687. d.freq1 = freq1;
  2688. d.freq2 = freq2;
  2689. d.duration = duration;
  2690. if (vol < 1)
  2691. d.vol = 8192;
  2692. else
  2693. d.vol = vol;
  2694. if (ast_activate_generator(chan, &tonepair, &d))
  2695. return -1;
  2696. return 0;
  2697. }
  2698. void ast_tonepair_stop(struct ast_channel *chan)
  2699. {
  2700. ast_deactivate_generator(chan);
  2701. }
  2702. int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol)
  2703. {
  2704. struct ast_frame *f;
  2705. int res;
  2706. if ((res = ast_tonepair_start(chan, freq1, freq2, duration, vol)))
  2707. return res;
  2708. /* Give us some wiggle room */
  2709. while(chan->generatordata && (ast_waitfor(chan, 100) >= 0)) {
  2710. f = ast_read(chan);
  2711. if (f)
  2712. ast_frfree(f);
  2713. else
  2714. return -1;
  2715. }
  2716. return 0;
  2717. }
  2718. unsigned int ast_get_group(char *s)
  2719. {
  2720. char *copy;
  2721. char *piece;
  2722. char *c=NULL;
  2723. int start=0, finish=0,x;
  2724. unsigned int group = 0;
  2725. copy = ast_strdupa(s);
  2726. if (!copy) {
  2727. ast_log(LOG_ERROR, "Out of memory\n");
  2728. return 0;
  2729. }
  2730. c = copy;
  2731. while((piece = strsep(&c, ","))) {
  2732. if (sscanf(piece, "%d-%d", &start, &finish) == 2) {
  2733. /* Range */
  2734. } else if (sscanf(piece, "%d", &start)) {
  2735. /* Just one */
  2736. finish = start;
  2737. } else {
  2738. ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'.\n", s, piece);
  2739. continue;
  2740. }
  2741. for (x=start;x<=finish;x++) {
  2742. if ((x > 31) || (x < 0)) {
  2743. ast_log(LOG_WARNING, "Ignoring invalid group %d (maximum group is 31)\n", x);
  2744. } else
  2745. group |= (1 << x);
  2746. }
  2747. }
  2748. return group;
  2749. }