channel_internal_api.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Channel Accessor API
  21. *
  22. * This file is intended to be the only file that ever accesses the
  23. * internals of an ast_channel. All other files should use the
  24. * accessor functions defined here.
  25. *
  26. * \author Terry Wilson
  27. */
  28. /*** MODULEINFO
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include <unistd.h>
  34. #include <fcntl.h>
  35. #include "asterisk/paths.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/channel_internal.h"
  38. #include "asterisk/data.h"
  39. #include "asterisk/endpoints.h"
  40. #include "asterisk/indications.h"
  41. #include "asterisk/stasis_cache_pattern.h"
  42. #include "asterisk/stasis_channels.h"
  43. #include "asterisk/stasis_endpoints.h"
  44. #include "asterisk/stringfields.h"
  45. #include "asterisk/test.h"
  46. /*!
  47. * \brief Channel UniqueId structure
  48. * \note channel creation time used for determining LinkedId Propagation
  49. */
  50. struct ast_channel_id {
  51. time_t creation_time; /*!< Creation time */
  52. int creation_unique; /*!< sub-second unique value */
  53. char unique_id[AST_MAX_UNIQUEID]; /*!< Unique Identifier */
  54. };
  55. /*!
  56. * \brief Main Channel structure associated with a channel.
  57. *
  58. * \note When adding fields to this structure, it is important to add the field
  59. * 'in position' with like-aligned fields, so as to keep the compiler from
  60. * having to add padding to align fields. The structure's fields are sorted
  61. * in this order: pointers, structures, long, int/enum, short, char. This
  62. * is especially important on 64-bit architectures, where mixing 4-byte
  63. * and 8-byte fields causes 4 bytes of padding to be added before many
  64. * 8-byte fields.
  65. */
  66. struct ast_channel {
  67. const struct ast_channel_tech *tech; /*!< Technology (point to channel driver) */
  68. void *tech_pvt; /*!< Private data used by the technology driver */
  69. void *music_state; /*!< Music State*/
  70. void *generatordata; /*!< Current generator data if there is any */
  71. struct ast_generator *generator; /*!< Current active data generator */
  72. struct ast_channel *masq; /*!< Channel that will masquerade as us */
  73. struct ast_channel *masqr; /*!< Who we are masquerading as */
  74. const char *blockproc; /*!< Procedure causing blocking */
  75. const char *appl; /*!< Current application */
  76. const char *data; /*!< Data passed to current application */
  77. struct ast_sched_context *sched; /*!< Schedule context */
  78. struct ast_filestream *stream; /*!< Stream itself. */
  79. struct ast_filestream *vstream; /*!< Video Stream itself. */
  80. ast_timing_func_t timingfunc;
  81. void *timingdata;
  82. struct ast_pbx *pbx; /*!< PBX private structure for this channel */
  83. struct ast_trans_pvt *writetrans; /*!< Write translation path */
  84. struct ast_trans_pvt *readtrans; /*!< Read translation path */
  85. struct ast_audiohook_list *audiohooks;
  86. struct ast_framehook_list *framehooks;
  87. struct ast_cdr *cdr; /*!< Call Detail Record */
  88. struct ast_tone_zone *zone; /*!< Tone zone as set in indications.conf or
  89. * in the CHANNEL dialplan function */
  90. struct ast_channel_monitor *monitor; /*!< Channel monitoring */
  91. struct ast_callid *callid; /*!< Bound call identifier pointer */
  92. #ifdef HAVE_EPOLL
  93. struct ast_epoll_data *epfd_data[AST_MAX_FDS];
  94. #endif
  95. struct ao2_container *dialed_causes; /*!< Contains tech-specific and Asterisk cause data from dialed channels */
  96. AST_DECLARE_STRING_FIELDS(
  97. AST_STRING_FIELD(name); /*!< ASCII unique channel name */
  98. AST_STRING_FIELD(language); /*!< Language requested for voice prompts */
  99. AST_STRING_FIELD(musicclass); /*!< Default music class */
  100. AST_STRING_FIELD(latest_musicclass); /*!< Latest active music class */
  101. AST_STRING_FIELD(accountcode); /*!< Account code for billing */
  102. AST_STRING_FIELD(peeraccount); /*!< Peer account code for billing */
  103. AST_STRING_FIELD(userfield); /*!< Userfield for CEL billing */
  104. AST_STRING_FIELD(call_forward); /*!< Where to forward to if asked to dial on this interface */
  105. AST_STRING_FIELD(parkinglot); /*! Default parking lot, if empty, default parking lot */
  106. AST_STRING_FIELD(hangupsource); /*! Who is responsible for hanging up this channel */
  107. AST_STRING_FIELD(dialcontext); /*!< Dial: Extension context that we were called from */
  108. );
  109. struct ast_channel_id uniqueid; /*!< Unique Channel Identifier - can be specified on creation */
  110. struct ast_channel_id linkedid; /*!< Linked Channel Identifier - oldest propagated when bridged */
  111. struct timeval whentohangup; /*!< Non-zero, set to actual time when channel is to be hung up */
  112. pthread_t blocker; /*!< If anyone is blocking, this is them */
  113. /*!
  114. * \brief Dialed/Called information.
  115. * \note Set on incoming channels to indicate the originally dialed party.
  116. * \note Dialed Number Identifier (DNID)
  117. */
  118. struct ast_party_dialed dialed;
  119. /*!
  120. * \brief Channel Caller ID information.
  121. * \note The caller id information is the caller id of this
  122. * channel when it is used to initiate a call.
  123. */
  124. struct ast_party_caller caller;
  125. /*!
  126. * \brief Channel Connected Line ID information.
  127. * \note The connected line information identifies the channel
  128. * connected/bridged to this channel.
  129. */
  130. struct ast_party_connected_line connected;
  131. /*!
  132. * \brief Channel Connected Line ID information that was last indicated.
  133. */
  134. struct ast_party_connected_line connected_indicated;
  135. /*! \brief Redirecting/Diversion information */
  136. struct ast_party_redirecting redirecting;
  137. struct ast_frame dtmff; /*!< DTMF frame */
  138. struct varshead varshead; /*!< A linked list for channel variables. See \ref AstChanVar */
  139. ast_group_t callgroup; /*!< Call group for call pickups */
  140. ast_group_t pickupgroup; /*!< Pickup group - which calls groups can be picked up? */
  141. struct ast_namedgroups *named_callgroups; /*!< Named call group for call pickups */
  142. struct ast_namedgroups *named_pickupgroups; /*!< Named pickup group - which call groups can be picked up? */
  143. struct timeval creationtime; /*!< The time of channel creation */
  144. struct timeval answertime; /*!< The time the channel was answered */
  145. struct ast_readq_list readq;
  146. struct ast_jb jb; /*!< The jitterbuffer state */
  147. struct timeval dtmf_tv; /*!< The time that an in process digit began, or the last digit ended */
  148. struct ast_hangup_handler_list hangup_handlers;/*!< Hangup handlers on the channel. */
  149. struct ast_datastore_list datastores; /*!< Data stores on the channel */
  150. struct ast_autochan_list autochans; /*!< Autochans on the channel */
  151. unsigned long insmpl; /*!< Track the read/written samples for monitor use */
  152. unsigned long outsmpl; /*!< Track the read/written samples for monitor use */
  153. int fds[AST_MAX_FDS]; /*!< File descriptors for channel -- Drivers will poll on
  154. * these file descriptors, so at least one must be non -1.
  155. * See \arg \ref AstFileDesc */
  156. int softhangup; /*!< Whether or not we have been hung up... Do not set this value
  157. * directly, use ast_softhangup() */
  158. int unbridged; /*!< If non-zero, the bridge core needs to re-evaluate the current
  159. bridging technology which is in use by this channel's bridge. */
  160. int fdno; /*!< Which fd had an event detected on */
  161. int streamid; /*!< For streaming playback, the schedule ID */
  162. int vstreamid; /*!< For streaming video playback, the schedule ID */
  163. struct ast_format oldwriteformat; /*!< Original writer format */
  164. int timingfd; /*!< Timing fd */
  165. enum ast_channel_state state; /*!< State of line -- Don't write directly, use ast_setstate() */
  166. int rings; /*!< Number of rings so far */
  167. int priority; /*!< Dialplan: Current extension priority */
  168. int macropriority; /*!< Macro: Current non-macro priority. See app_macro.c */
  169. int amaflags; /*!< Set BEFORE PBX is started to determine AMA flags */
  170. enum ast_channel_adsicpe adsicpe; /*!< Whether or not ADSI is detected on CPE */
  171. unsigned int fin; /*!< Frames in counters. The high bit is a debug mask, so
  172. * the counter is only in the remaining bits */
  173. unsigned int fout; /*!< Frames out counters. The high bit is a debug mask, so
  174. * the counter is only in the remaining bits */
  175. int hangupcause; /*!< Why is the channel hanged up. See causes.h */
  176. unsigned int finalized:1; /*!< Whether or not the channel has been successfully allocated */
  177. struct ast_flags flags; /*!< channel flags of AST_FLAG_ type */
  178. int alertpipe[2];
  179. struct ast_format_cap *nativeformats; /*!< Kinds of data this channel can natively handle */
  180. struct ast_format readformat; /*!< Requested read format (after translation) */
  181. struct ast_format writeformat; /*!< Requested write format (after translation) */
  182. struct ast_format rawreadformat; /*!< Raw read format (before translation) */
  183. struct ast_format rawwriteformat; /*!< Raw write format (before translation) */
  184. unsigned int emulate_dtmf_duration; /*!< Number of ms left to emulate DTMF for */
  185. #ifdef HAVE_EPOLL
  186. int epfd;
  187. #endif
  188. int visible_indication; /*!< Indication currently playing on the channel */
  189. int hold_state; /*!< Current Hold/Unhold state */
  190. unsigned short transfercapability; /*!< ISDN Transfer Capability - AST_FLAG_DIGITAL is not enough */
  191. struct ast_bridge *bridge; /*!< Bridge this channel is participating in */
  192. struct ast_bridge_channel *bridge_channel;/*!< The bridge_channel this channel is linked with. */
  193. struct ast_timer *timer; /*!< timer object that provided timingfd */
  194. char context[AST_MAX_CONTEXT]; /*!< Dialplan: Current extension context */
  195. char exten[AST_MAX_EXTENSION]; /*!< Dialplan: Current extension number */
  196. char macrocontext[AST_MAX_CONTEXT]; /*!< Macro: Current non-macro context. See app_macro.c */
  197. char macroexten[AST_MAX_EXTENSION]; /*!< Macro: Current non-macro extension. See app_macro.c */
  198. char dtmf_digit_to_emulate; /*!< Digit being emulated */
  199. char sending_dtmf_digit; /*!< Digit this channel is currently sending out. (zero if not sending) */
  200. struct timeval sending_dtmf_tv; /*!< The time this channel started sending the current digit. (Invalid if sending_dtmf_digit is zero.) */
  201. struct stasis_cp_single *topics; /*!< Topic for all channel's events */
  202. struct stasis_forward *endpoint_forward; /*!< Subscription for event forwarding to endpoint's topic */
  203. struct stasis_forward *endpoint_cache_forward; /*!< Subscription for cache updates to endpoint's topic */
  204. };
  205. /*! \brief The monotonically increasing integer counter for channel uniqueids */
  206. static int uniqueint;
  207. /* AST_DATA definitions, which will probably have to be re-thought since the channel will be opaque */
  208. #if 0 /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
  209. #define DATA_EXPORT_CALLERID(MEMBER) \
  210. MEMBER(ast_callerid, cid_dnid, AST_DATA_STRING) \
  211. MEMBER(ast_callerid, cid_num, AST_DATA_STRING) \
  212. MEMBER(ast_callerid, cid_name, AST_DATA_STRING) \
  213. MEMBER(ast_callerid, cid_ani, AST_DATA_STRING) \
  214. MEMBER(ast_callerid, cid_pres, AST_DATA_INTEGER) \
  215. MEMBER(ast_callerid, cid_ani2, AST_DATA_INTEGER) \
  216. MEMBER(ast_callerid, cid_tag, AST_DATA_STRING)
  217. AST_DATA_STRUCTURE(ast_callerid, DATA_EXPORT_CALLERID);
  218. #endif
  219. #define DATA_EXPORT_CHANNEL(MEMBER) \
  220. MEMBER(ast_channel, blockproc, AST_DATA_STRING) \
  221. MEMBER(ast_channel, appl, AST_DATA_STRING) \
  222. MEMBER(ast_channel, data, AST_DATA_STRING) \
  223. MEMBER(ast_channel, name, AST_DATA_STRING) \
  224. MEMBER(ast_channel, language, AST_DATA_STRING) \
  225. MEMBER(ast_channel, musicclass, AST_DATA_STRING) \
  226. MEMBER(ast_channel, accountcode, AST_DATA_STRING) \
  227. MEMBER(ast_channel, peeraccount, AST_DATA_STRING) \
  228. MEMBER(ast_channel, userfield, AST_DATA_STRING) \
  229. MEMBER(ast_channel, call_forward, AST_DATA_STRING) \
  230. MEMBER(ast_channel, parkinglot, AST_DATA_STRING) \
  231. MEMBER(ast_channel, hangupsource, AST_DATA_STRING) \
  232. MEMBER(ast_channel, dialcontext, AST_DATA_STRING) \
  233. MEMBER(ast_channel, rings, AST_DATA_INTEGER) \
  234. MEMBER(ast_channel, priority, AST_DATA_INTEGER) \
  235. MEMBER(ast_channel, macropriority, AST_DATA_INTEGER) \
  236. MEMBER(ast_channel, adsicpe, AST_DATA_INTEGER) \
  237. MEMBER(ast_channel, fin, AST_DATA_UNSIGNED_INTEGER) \
  238. MEMBER(ast_channel, fout, AST_DATA_UNSIGNED_INTEGER) \
  239. MEMBER(ast_channel, emulate_dtmf_duration, AST_DATA_UNSIGNED_INTEGER) \
  240. MEMBER(ast_channel, visible_indication, AST_DATA_INTEGER) \
  241. MEMBER(ast_channel, context, AST_DATA_STRING) \
  242. MEMBER(ast_channel, exten, AST_DATA_STRING) \
  243. MEMBER(ast_channel, macrocontext, AST_DATA_STRING) \
  244. MEMBER(ast_channel, macroexten, AST_DATA_STRING)
  245. AST_DATA_STRUCTURE(ast_channel, DATA_EXPORT_CHANNEL);
  246. static void channel_data_add_flags(struct ast_data *tree,
  247. struct ast_channel *chan)
  248. {
  249. ast_data_add_bool(tree, "DEFER_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF));
  250. ast_data_add_bool(tree, "WRITE_INT", ast_test_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT));
  251. ast_data_add_bool(tree, "BLOCKING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING));
  252. ast_data_add_bool(tree, "ZOMBIE", ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE));
  253. ast_data_add_bool(tree, "EXCEPTION", ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION));
  254. ast_data_add_bool(tree, "MOH", ast_test_flag(ast_channel_flags(chan), AST_FLAG_MOH));
  255. ast_data_add_bool(tree, "SPYING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_SPYING));
  256. ast_data_add_bool(tree, "IN_AUTOLOOP", ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP));
  257. ast_data_add_bool(tree, "OUTGOING", ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING));
  258. ast_data_add_bool(tree, "IN_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF));
  259. ast_data_add_bool(tree, "EMULATE_DTMF", ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF));
  260. ast_data_add_bool(tree, "END_DTMF_ONLY", ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY));
  261. ast_data_add_bool(tree, "MASQ_NOSTREAM", ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM));
  262. ast_data_add_bool(tree, "BRIDGE_HANGUP_RUN", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_RUN));
  263. ast_data_add_bool(tree, "DISABLE_WORKAROUNDS", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_WORKAROUNDS));
  264. ast_data_add_bool(tree, "DISABLE_DEVSTATE_CACHE", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE));
  265. ast_data_add_bool(tree, "BRIDGE_DUAL_REDIRECT_WAIT", ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT));
  266. ast_data_add_bool(tree, "ORIGINATED", ast_test_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED));
  267. ast_data_add_bool(tree, "DEAD", ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEAD));
  268. }
  269. int ast_channel_data_add_structure(struct ast_data *tree,
  270. struct ast_channel *chan, int add_bridged)
  271. {
  272. struct ast_data *data_bridged;
  273. struct ast_data *data_cdr;
  274. struct ast_data *data_flags;
  275. struct ast_data *data_zones;
  276. struct ast_data *enum_node;
  277. struct ast_data *data_softhangup;
  278. #if 0 /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
  279. struct ast_data *data_callerid;
  280. char value_str[100];
  281. #endif
  282. if (!tree) {
  283. return -1;
  284. }
  285. ast_data_add_structure(ast_channel, tree, chan);
  286. if (add_bridged) {
  287. RAII_VAR(struct ast_channel *, bc, ast_channel_bridge_peer(chan), ast_channel_cleanup);
  288. if (bc) {
  289. data_bridged = ast_data_add_node(tree, "bridged");
  290. if (!data_bridged) {
  291. return -1;
  292. }
  293. ast_channel_data_add_structure(data_bridged, bc, 0);
  294. }
  295. }
  296. ast_data_add_str(tree, "uniqueid", ast_channel_uniqueid(chan));
  297. ast_data_add_str(tree, "linkedid", ast_channel_linkedid(chan));
  298. ast_data_add_codec(tree, "oldwriteformat", ast_channel_oldwriteformat(chan));
  299. ast_data_add_codec(tree, "readformat", ast_channel_readformat(chan));
  300. ast_data_add_codec(tree, "writeformat", ast_channel_writeformat(chan));
  301. ast_data_add_codec(tree, "rawreadformat", ast_channel_rawreadformat(chan));
  302. ast_data_add_codec(tree, "rawwriteformat", ast_channel_rawwriteformat(chan));
  303. ast_data_add_codecs(tree, "nativeformats", ast_channel_nativeformats(chan));
  304. /* state */
  305. enum_node = ast_data_add_node(tree, "state");
  306. if (!enum_node) {
  307. return -1;
  308. }
  309. ast_data_add_str(enum_node, "text", ast_state2str(ast_channel_state(chan)));
  310. ast_data_add_int(enum_node, "value", ast_channel_state(chan));
  311. /* hangupcause */
  312. enum_node = ast_data_add_node(tree, "hangupcause");
  313. if (!enum_node) {
  314. return -1;
  315. }
  316. ast_data_add_str(enum_node, "text", ast_cause2str(ast_channel_hangupcause(chan)));
  317. ast_data_add_int(enum_node, "value", ast_channel_hangupcause(chan));
  318. /* amaflags */
  319. enum_node = ast_data_add_node(tree, "amaflags");
  320. if (!enum_node) {
  321. return -1;
  322. }
  323. ast_data_add_str(enum_node, "text", ast_channel_amaflags2string(ast_channel_amaflags(chan)));
  324. ast_data_add_int(enum_node, "value", ast_channel_amaflags(chan));
  325. /* transfercapability */
  326. enum_node = ast_data_add_node(tree, "transfercapability");
  327. if (!enum_node) {
  328. return -1;
  329. }
  330. ast_data_add_str(enum_node, "text", ast_transfercapability2str(ast_channel_transfercapability(chan)));
  331. ast_data_add_int(enum_node, "value", ast_channel_transfercapability(chan));
  332. /* _softphangup */
  333. data_softhangup = ast_data_add_node(tree, "softhangup");
  334. if (!data_softhangup) {
  335. return -1;
  336. }
  337. ast_data_add_bool(data_softhangup, "dev", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_DEV);
  338. ast_data_add_bool(data_softhangup, "asyncgoto", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO);
  339. ast_data_add_bool(data_softhangup, "shutdown", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_SHUTDOWN);
  340. ast_data_add_bool(data_softhangup, "timeout", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_TIMEOUT);
  341. ast_data_add_bool(data_softhangup, "appunload", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_APPUNLOAD);
  342. ast_data_add_bool(data_softhangup, "explicit", ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_EXPLICIT);
  343. /* channel flags */
  344. data_flags = ast_data_add_node(tree, "flags");
  345. if (!data_flags) {
  346. return -1;
  347. }
  348. channel_data_add_flags(data_flags, chan);
  349. ast_data_add_uint(tree, "timetohangup", ast_channel_whentohangup(chan)->tv_sec);
  350. #if 0 /* XXX AstData: ast_callerid no longer exists. (Equivalent code not readily apparent.) */
  351. /* callerid */
  352. data_callerid = ast_data_add_node(tree, "callerid");
  353. if (!data_callerid) {
  354. return -1;
  355. }
  356. ast_data_add_structure(ast_callerid, data_callerid, &(chan->cid));
  357. /* insert the callerid ton */
  358. enum_node = ast_data_add_node(data_callerid, "cid_ton");
  359. if (!enum_node) {
  360. return -1;
  361. }
  362. ast_data_add_int(enum_node, "value", chan->cid.cid_ton);
  363. snprintf(value_str, sizeof(value_str), "TON: %s/Plan: %s",
  364. party_number_ton2str(chan->cid.cid_ton),
  365. party_number_plan2str(chan->cid.cid_ton));
  366. ast_data_add_str(enum_node, "text", value_str);
  367. #endif
  368. /* tone zone */
  369. if (ast_channel_zone(chan)) {
  370. data_zones = ast_data_add_node(tree, "zone");
  371. if (!data_zones) {
  372. return -1;
  373. }
  374. ast_tone_zone_data_add_structure(data_zones, ast_channel_zone(chan));
  375. }
  376. /* insert cdr */
  377. data_cdr = ast_data_add_node(tree, "cdr");
  378. if (!data_cdr) {
  379. return -1;
  380. }
  381. return 0;
  382. }
  383. int ast_channel_data_cmp_structure(const struct ast_data_search *tree,
  384. struct ast_channel *chan, const char *structure_name)
  385. {
  386. return ast_data_search_cmp_structure(tree, ast_channel, chan, structure_name);
  387. }
  388. /* ACCESSORS */
  389. #define DEFINE_STRINGFIELD_SETTERS_FOR(field, publish, assert_on_null) \
  390. void ast_channel_##field##_set(struct ast_channel *chan, const char *value) \
  391. { \
  392. if ((assert_on_null)) ast_assert(!ast_strlen_zero(value)); \
  393. if (!strcmp(value, chan->field)) return; \
  394. ast_string_field_set(chan, field, value); \
  395. if (publish && ast_channel_internal_is_finalized(chan)) ast_channel_publish_snapshot(chan); \
  396. } \
  397. \
  398. void ast_channel_##field##_build_va(struct ast_channel *chan, const char *fmt, va_list ap) \
  399. { \
  400. ast_string_field_build_va(chan, field, fmt, ap); \
  401. if (publish && ast_channel_internal_is_finalized(chan)) ast_channel_publish_snapshot(chan); \
  402. } \
  403. void ast_channel_##field##_build(struct ast_channel *chan, const char *fmt, ...) \
  404. { \
  405. va_list ap; \
  406. va_start(ap, fmt); \
  407. ast_channel_##field##_build_va(chan, fmt, ap); \
  408. va_end(ap); \
  409. }
  410. DEFINE_STRINGFIELD_SETTERS_FOR(name, 0, 1);
  411. DEFINE_STRINGFIELD_SETTERS_FOR(language, 1, 0);
  412. DEFINE_STRINGFIELD_SETTERS_FOR(musicclass, 0, 0);
  413. DEFINE_STRINGFIELD_SETTERS_FOR(latest_musicclass, 0, 0);
  414. DEFINE_STRINGFIELD_SETTERS_FOR(accountcode, 1, 0);
  415. DEFINE_STRINGFIELD_SETTERS_FOR(peeraccount, 1, 0);
  416. DEFINE_STRINGFIELD_SETTERS_FOR(userfield, 0, 0);
  417. DEFINE_STRINGFIELD_SETTERS_FOR(call_forward, 0, 0);
  418. DEFINE_STRINGFIELD_SETTERS_FOR(parkinglot, 0, 0);
  419. DEFINE_STRINGFIELD_SETTERS_FOR(hangupsource, 0, 0);
  420. DEFINE_STRINGFIELD_SETTERS_FOR(dialcontext, 0, 0);
  421. #define DEFINE_STRINGFIELD_GETTER_FOR(field) const char *ast_channel_##field(const struct ast_channel *chan) \
  422. { \
  423. return chan->field; \
  424. }
  425. DEFINE_STRINGFIELD_GETTER_FOR(name);
  426. DEFINE_STRINGFIELD_GETTER_FOR(language);
  427. DEFINE_STRINGFIELD_GETTER_FOR(musicclass);
  428. DEFINE_STRINGFIELD_GETTER_FOR(latest_musicclass);
  429. DEFINE_STRINGFIELD_GETTER_FOR(accountcode);
  430. DEFINE_STRINGFIELD_GETTER_FOR(peeraccount);
  431. DEFINE_STRINGFIELD_GETTER_FOR(userfield);
  432. DEFINE_STRINGFIELD_GETTER_FOR(call_forward);
  433. DEFINE_STRINGFIELD_GETTER_FOR(parkinglot);
  434. DEFINE_STRINGFIELD_GETTER_FOR(hangupsource);
  435. DEFINE_STRINGFIELD_GETTER_FOR(dialcontext);
  436. const char *ast_channel_uniqueid(const struct ast_channel *chan)
  437. {
  438. ast_assert(chan->uniqueid.unique_id[0] != '\0');
  439. return chan->uniqueid.unique_id;
  440. }
  441. const char *ast_channel_linkedid(const struct ast_channel *chan)
  442. {
  443. ast_assert(chan->linkedid.unique_id[0] != '\0');
  444. return chan->linkedid.unique_id;
  445. }
  446. const char *ast_channel_appl(const struct ast_channel *chan)
  447. {
  448. return chan->appl;
  449. }
  450. void ast_channel_appl_set(struct ast_channel *chan, const char *value)
  451. {
  452. chan->appl = value;
  453. }
  454. const char *ast_channel_blockproc(const struct ast_channel *chan)
  455. {
  456. return chan->blockproc;
  457. }
  458. void ast_channel_blockproc_set(struct ast_channel *chan, const char *value)
  459. {
  460. chan->blockproc = value;
  461. }
  462. const char *ast_channel_data(const struct ast_channel *chan)
  463. {
  464. return chan->data;
  465. }
  466. void ast_channel_data_set(struct ast_channel *chan, const char *value)
  467. {
  468. chan->data = value;
  469. }
  470. const char *ast_channel_context(const struct ast_channel *chan)
  471. {
  472. return chan->context;
  473. }
  474. void ast_channel_context_set(struct ast_channel *chan, const char *value)
  475. {
  476. ast_copy_string(chan->context, value, sizeof(chan->context));
  477. }
  478. const char *ast_channel_exten(const struct ast_channel *chan)
  479. {
  480. return chan->exten;
  481. }
  482. void ast_channel_exten_set(struct ast_channel *chan, const char *value)
  483. {
  484. ast_copy_string(chan->exten, value, sizeof(chan->exten));
  485. }
  486. const char *ast_channel_macrocontext(const struct ast_channel *chan)
  487. {
  488. return chan->macrocontext;
  489. }
  490. void ast_channel_macrocontext_set(struct ast_channel *chan, const char *value)
  491. {
  492. ast_copy_string(chan->macrocontext, value, sizeof(chan->macrocontext));
  493. }
  494. const char *ast_channel_macroexten(const struct ast_channel *chan)
  495. {
  496. return chan->macroexten;
  497. }
  498. void ast_channel_macroexten_set(struct ast_channel *chan, const char *value)
  499. {
  500. ast_copy_string(chan->macroexten, value, sizeof(chan->macroexten));
  501. }
  502. char ast_channel_dtmf_digit_to_emulate(const struct ast_channel *chan)
  503. {
  504. return chan->dtmf_digit_to_emulate;
  505. }
  506. void ast_channel_dtmf_digit_to_emulate_set(struct ast_channel *chan, char value)
  507. {
  508. chan->dtmf_digit_to_emulate = value;
  509. }
  510. char ast_channel_sending_dtmf_digit(const struct ast_channel *chan)
  511. {
  512. return chan->sending_dtmf_digit;
  513. }
  514. void ast_channel_sending_dtmf_digit_set(struct ast_channel *chan, char value)
  515. {
  516. chan->sending_dtmf_digit = value;
  517. }
  518. struct timeval ast_channel_sending_dtmf_tv(const struct ast_channel *chan)
  519. {
  520. return chan->sending_dtmf_tv;
  521. }
  522. void ast_channel_sending_dtmf_tv_set(struct ast_channel *chan, struct timeval value)
  523. {
  524. chan->sending_dtmf_tv = value;
  525. }
  526. enum ama_flags ast_channel_amaflags(const struct ast_channel *chan)
  527. {
  528. return chan->amaflags;
  529. }
  530. void ast_channel_amaflags_set(struct ast_channel *chan, enum ama_flags value)
  531. {
  532. if (chan->amaflags == value) {
  533. return;
  534. }
  535. chan->amaflags = value;
  536. ast_channel_publish_snapshot(chan);
  537. }
  538. #ifdef HAVE_EPOLL
  539. int ast_channel_epfd(const struct ast_channel *chan)
  540. {
  541. return chan->epfd;
  542. }
  543. void ast_channel_epfd_set(struct ast_channel *chan, int value)
  544. {
  545. chan->epfd = value;
  546. }
  547. #endif
  548. int ast_channel_fdno(const struct ast_channel *chan)
  549. {
  550. return chan->fdno;
  551. }
  552. void ast_channel_fdno_set(struct ast_channel *chan, int value)
  553. {
  554. chan->fdno = value;
  555. }
  556. int ast_channel_hangupcause(const struct ast_channel *chan)
  557. {
  558. return chan->hangupcause;
  559. }
  560. void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
  561. {
  562. chan->hangupcause = value;
  563. }
  564. int ast_channel_macropriority(const struct ast_channel *chan)
  565. {
  566. return chan->macropriority;
  567. }
  568. void ast_channel_macropriority_set(struct ast_channel *chan, int value)
  569. {
  570. chan->macropriority = value;
  571. }
  572. int ast_channel_priority(const struct ast_channel *chan)
  573. {
  574. return chan->priority;
  575. }
  576. void ast_channel_priority_set(struct ast_channel *chan, int value)
  577. {
  578. chan->priority = value;
  579. }
  580. int ast_channel_rings(const struct ast_channel *chan)
  581. {
  582. return chan->rings;
  583. }
  584. void ast_channel_rings_set(struct ast_channel *chan, int value)
  585. {
  586. chan->rings = value;
  587. }
  588. int ast_channel_streamid(const struct ast_channel *chan)
  589. {
  590. return chan->streamid;
  591. }
  592. void ast_channel_streamid_set(struct ast_channel *chan, int value)
  593. {
  594. chan->streamid = value;
  595. }
  596. int ast_channel_timingfd(const struct ast_channel *chan)
  597. {
  598. return chan->timingfd;
  599. }
  600. void ast_channel_timingfd_set(struct ast_channel *chan, int value)
  601. {
  602. chan->timingfd = value;
  603. }
  604. int ast_channel_visible_indication(const struct ast_channel *chan)
  605. {
  606. return chan->visible_indication;
  607. }
  608. void ast_channel_visible_indication_set(struct ast_channel *chan, int value)
  609. {
  610. chan->visible_indication = value;
  611. }
  612. int ast_channel_hold_state(const struct ast_channel *chan)
  613. {
  614. return chan->hold_state;
  615. }
  616. void ast_channel_hold_state_set(struct ast_channel *chan, int value)
  617. {
  618. chan->hold_state = value;
  619. }
  620. int ast_channel_vstreamid(const struct ast_channel *chan)
  621. {
  622. return chan->vstreamid;
  623. }
  624. void ast_channel_vstreamid_set(struct ast_channel *chan, int value)
  625. {
  626. chan->vstreamid = value;
  627. }
  628. unsigned short ast_channel_transfercapability(const struct ast_channel *chan)
  629. {
  630. return chan->transfercapability;
  631. }
  632. void ast_channel_transfercapability_set(struct ast_channel *chan, unsigned short value)
  633. {
  634. chan->transfercapability = value;
  635. }
  636. unsigned int ast_channel_emulate_dtmf_duration(const struct ast_channel *chan)
  637. {
  638. return chan->emulate_dtmf_duration;
  639. }
  640. void ast_channel_emulate_dtmf_duration_set(struct ast_channel *chan, unsigned int value)
  641. {
  642. chan->emulate_dtmf_duration = value;
  643. }
  644. unsigned int ast_channel_fin(const struct ast_channel *chan)
  645. {
  646. return chan->fin;
  647. }
  648. void ast_channel_fin_set(struct ast_channel *chan, unsigned int value)
  649. {
  650. chan->fin = value;
  651. }
  652. unsigned int ast_channel_fout(const struct ast_channel *chan)
  653. {
  654. return chan->fout;
  655. }
  656. void ast_channel_fout_set(struct ast_channel *chan, unsigned int value)
  657. {
  658. chan->fout = value;
  659. }
  660. unsigned long ast_channel_insmpl(const struct ast_channel *chan)
  661. {
  662. return chan->insmpl;
  663. }
  664. void ast_channel_insmpl_set(struct ast_channel *chan, unsigned long value)
  665. {
  666. chan->insmpl = value;
  667. }
  668. unsigned long ast_channel_outsmpl(const struct ast_channel *chan)
  669. {
  670. return chan->outsmpl;
  671. }
  672. void ast_channel_outsmpl_set(struct ast_channel *chan, unsigned long value)
  673. {
  674. chan->outsmpl = value;
  675. }
  676. void *ast_channel_generatordata(const struct ast_channel *chan)
  677. {
  678. return chan->generatordata;
  679. }
  680. void ast_channel_generatordata_set(struct ast_channel *chan, void *value)
  681. {
  682. chan->generatordata = value;
  683. }
  684. void *ast_channel_music_state(const struct ast_channel *chan)
  685. {
  686. return chan->music_state;
  687. }
  688. void ast_channel_music_state_set(struct ast_channel *chan, void *value)
  689. {
  690. chan->music_state = value;
  691. }
  692. void *ast_channel_tech_pvt(const struct ast_channel *chan)
  693. {
  694. return chan->tech_pvt;
  695. }
  696. void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
  697. {
  698. chan->tech_pvt = value;
  699. }
  700. void *ast_channel_timingdata(const struct ast_channel *chan)
  701. {
  702. return chan->timingdata;
  703. }
  704. void ast_channel_timingdata_set(struct ast_channel *chan, void *value)
  705. {
  706. chan->timingdata = value;
  707. }
  708. struct ast_audiohook_list *ast_channel_audiohooks(const struct ast_channel *chan)
  709. {
  710. return chan->audiohooks;
  711. }
  712. void ast_channel_audiohooks_set(struct ast_channel *chan, struct ast_audiohook_list *value)
  713. {
  714. chan->audiohooks = value;
  715. }
  716. struct ast_cdr *ast_channel_cdr(const struct ast_channel *chan)
  717. {
  718. return chan->cdr;
  719. }
  720. void ast_channel_cdr_set(struct ast_channel *chan, struct ast_cdr *value)
  721. {
  722. chan->cdr = value;
  723. }
  724. struct ast_channel *ast_channel_masq(const struct ast_channel *chan)
  725. {
  726. return chan->masq;
  727. }
  728. void ast_channel_masq_set(struct ast_channel *chan, struct ast_channel *value)
  729. {
  730. chan->masq = value;
  731. }
  732. struct ast_channel *ast_channel_masqr(const struct ast_channel *chan)
  733. {
  734. return chan->masqr;
  735. }
  736. void ast_channel_masqr_set(struct ast_channel *chan, struct ast_channel *value)
  737. {
  738. chan->masqr = value;
  739. }
  740. struct ast_channel_monitor *ast_channel_monitor(const struct ast_channel *chan)
  741. {
  742. return chan->monitor;
  743. }
  744. void ast_channel_monitor_set(struct ast_channel *chan, struct ast_channel_monitor *value)
  745. {
  746. chan->monitor = value;
  747. }
  748. struct ast_filestream *ast_channel_stream(const struct ast_channel *chan)
  749. {
  750. return chan->stream;
  751. }
  752. void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
  753. {
  754. chan->stream = value;
  755. }
  756. struct ast_filestream *ast_channel_vstream(const struct ast_channel *chan)
  757. {
  758. return chan->vstream;
  759. }
  760. void ast_channel_vstream_set(struct ast_channel *chan, struct ast_filestream *value)
  761. {
  762. chan->vstream = value;
  763. }
  764. struct ast_format_cap *ast_channel_nativeformats(const struct ast_channel *chan)
  765. {
  766. return chan->nativeformats;
  767. }
  768. void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
  769. {
  770. chan->nativeformats = value;
  771. }
  772. struct ast_framehook_list *ast_channel_framehooks(const struct ast_channel *chan)
  773. {
  774. return chan->framehooks;
  775. }
  776. void ast_channel_framehooks_set(struct ast_channel *chan, struct ast_framehook_list *value)
  777. {
  778. chan->framehooks = value;
  779. }
  780. struct ast_generator *ast_channel_generator(const struct ast_channel *chan)
  781. {
  782. return chan->generator;
  783. }
  784. void ast_channel_generator_set(struct ast_channel *chan, struct ast_generator *value)
  785. {
  786. chan->generator = value;
  787. }
  788. struct ast_pbx *ast_channel_pbx(const struct ast_channel *chan)
  789. {
  790. return chan->pbx;
  791. }
  792. void ast_channel_pbx_set(struct ast_channel *chan, struct ast_pbx *value)
  793. {
  794. chan->pbx = value;
  795. }
  796. struct ast_sched_context *ast_channel_sched(const struct ast_channel *chan)
  797. {
  798. return chan->sched;
  799. }
  800. void ast_channel_sched_set(struct ast_channel *chan, struct ast_sched_context *value)
  801. {
  802. chan->sched = value;
  803. }
  804. struct ast_timer *ast_channel_timer(const struct ast_channel *chan)
  805. {
  806. return chan->timer;
  807. }
  808. void ast_channel_timer_set(struct ast_channel *chan, struct ast_timer *value)
  809. {
  810. chan->timer = value;
  811. }
  812. struct ast_tone_zone *ast_channel_zone(const struct ast_channel *chan)
  813. {
  814. return chan->zone;
  815. }
  816. void ast_channel_zone_set(struct ast_channel *chan, struct ast_tone_zone *value)
  817. {
  818. chan->zone = value;
  819. }
  820. struct ast_trans_pvt *ast_channel_readtrans(const struct ast_channel *chan)
  821. {
  822. return chan->readtrans;
  823. }
  824. void ast_channel_readtrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
  825. {
  826. chan->readtrans = value;
  827. }
  828. struct ast_trans_pvt *ast_channel_writetrans(const struct ast_channel *chan)
  829. {
  830. return chan->writetrans;
  831. }
  832. void ast_channel_writetrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
  833. {
  834. chan->writetrans = value;
  835. }
  836. const struct ast_channel_tech *ast_channel_tech(const struct ast_channel *chan)
  837. {
  838. return chan->tech;
  839. }
  840. void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
  841. {
  842. chan->tech = value;
  843. }
  844. enum ast_channel_adsicpe ast_channel_adsicpe(const struct ast_channel *chan)
  845. {
  846. return chan->adsicpe;
  847. }
  848. void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
  849. {
  850. chan->adsicpe = value;
  851. }
  852. enum ast_channel_state ast_channel_state(const struct ast_channel *chan)
  853. {
  854. return chan->state;
  855. }
  856. struct ast_callid *ast_channel_callid(const struct ast_channel *chan)
  857. {
  858. if (chan->callid) {
  859. ast_callid_ref(chan->callid);
  860. return chan->callid;
  861. }
  862. return NULL;
  863. }
  864. void ast_channel_callid_set(struct ast_channel *chan, struct ast_callid *callid)
  865. {
  866. char call_identifier_from[AST_CALLID_BUFFER_LENGTH];
  867. char call_identifier_to[AST_CALLID_BUFFER_LENGTH];
  868. call_identifier_from[0] = '\0';
  869. ast_callid_strnprint(call_identifier_to, sizeof(call_identifier_to), callid);
  870. if (chan->callid) {
  871. ast_callid_strnprint(call_identifier_from, sizeof(call_identifier_from), chan->callid);
  872. ast_debug(3, "Channel Call ID changing from %s to %s\n", call_identifier_from, call_identifier_to);
  873. /* unbind if already set */
  874. ast_callid_unref(chan->callid);
  875. }
  876. chan->callid = ast_callid_ref(callid);
  877. ast_test_suite_event_notify("CallIDChange",
  878. "State: CallIDChange\r\n"
  879. "Channel: %s\r\n"
  880. "CallID: %s\r\n"
  881. "PriorCallID: %s",
  882. ast_channel_name(chan),
  883. call_identifier_to,
  884. call_identifier_from);
  885. }
  886. void ast_channel_state_set(struct ast_channel *chan, enum ast_channel_state value)
  887. {
  888. chan->state = value;
  889. }
  890. struct ast_format *ast_channel_oldwriteformat(struct ast_channel *chan)
  891. {
  892. return &chan->oldwriteformat;
  893. }
  894. struct ast_format *ast_channel_rawreadformat(struct ast_channel *chan)
  895. {
  896. return &chan->rawreadformat;
  897. }
  898. struct ast_format *ast_channel_rawwriteformat(struct ast_channel *chan)
  899. {
  900. return &chan->rawwriteformat;
  901. }
  902. struct ast_format *ast_channel_readformat(struct ast_channel *chan)
  903. {
  904. return &chan->readformat;
  905. }
  906. struct ast_format *ast_channel_writeformat(struct ast_channel *chan)
  907. {
  908. return &chan->writeformat;
  909. }
  910. struct ast_hangup_handler_list *ast_channel_hangup_handlers(struct ast_channel *chan)
  911. {
  912. return &chan->hangup_handlers;
  913. }
  914. struct ast_datastore_list *ast_channel_datastores(struct ast_channel *chan)
  915. {
  916. return &chan->datastores;
  917. }
  918. struct ast_autochan_list *ast_channel_autochans(struct ast_channel *chan)
  919. {
  920. return &chan->autochans;
  921. }
  922. struct ast_readq_list *ast_channel_readq(struct ast_channel *chan)
  923. {
  924. return &chan->readq;
  925. }
  926. struct ast_frame *ast_channel_dtmff(struct ast_channel *chan)
  927. {
  928. return &chan->dtmff;
  929. }
  930. struct ast_jb *ast_channel_jb(struct ast_channel *chan)
  931. {
  932. return &chan->jb;
  933. }
  934. struct ast_party_caller *ast_channel_caller(struct ast_channel *chan)
  935. {
  936. return &chan->caller;
  937. }
  938. struct ast_party_connected_line *ast_channel_connected(struct ast_channel *chan)
  939. {
  940. return &chan->connected;
  941. }
  942. struct ast_party_connected_line *ast_channel_connected_indicated(struct ast_channel *chan)
  943. {
  944. return &chan->connected_indicated;
  945. }
  946. struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
  947. {
  948. return ast_party_id_merge(&chan->connected.id, &chan->connected.priv);
  949. }
  950. struct ast_party_dialed *ast_channel_dialed(struct ast_channel *chan)
  951. {
  952. return &chan->dialed;
  953. }
  954. struct ast_party_redirecting *ast_channel_redirecting(struct ast_channel *chan)
  955. {
  956. return &chan->redirecting;
  957. }
  958. struct ast_party_id ast_channel_redirecting_effective_orig(struct ast_channel *chan)
  959. {
  960. return ast_party_id_merge(&chan->redirecting.orig, &chan->redirecting.priv_orig);
  961. }
  962. struct ast_party_id ast_channel_redirecting_effective_from(struct ast_channel *chan)
  963. {
  964. return ast_party_id_merge(&chan->redirecting.from, &chan->redirecting.priv_from);
  965. }
  966. struct ast_party_id ast_channel_redirecting_effective_to(struct ast_channel *chan)
  967. {
  968. return ast_party_id_merge(&chan->redirecting.to, &chan->redirecting.priv_to);
  969. }
  970. struct timeval *ast_channel_dtmf_tv(struct ast_channel *chan)
  971. {
  972. return &chan->dtmf_tv;
  973. }
  974. struct timeval *ast_channel_whentohangup(struct ast_channel *chan)
  975. {
  976. return &chan->whentohangup;
  977. }
  978. struct varshead *ast_channel_varshead(struct ast_channel *chan)
  979. {
  980. return &chan->varshead;
  981. }
  982. void ast_channel_dtmff_set(struct ast_channel *chan, struct ast_frame *value)
  983. {
  984. chan->dtmff = *value;
  985. }
  986. void ast_channel_jb_set(struct ast_channel *chan, struct ast_jb *value)
  987. {
  988. chan->jb = *value;
  989. }
  990. void ast_channel_caller_set(struct ast_channel *chan, struct ast_party_caller *value)
  991. {
  992. chan->caller = *value;
  993. }
  994. void ast_channel_connected_set(struct ast_channel *chan, struct ast_party_connected_line *value)
  995. {
  996. chan->connected = *value;
  997. }
  998. void ast_channel_dialed_set(struct ast_channel *chan, struct ast_party_dialed *value)
  999. {
  1000. chan->dialed = *value;
  1001. }
  1002. void ast_channel_redirecting_set(struct ast_channel *chan, struct ast_party_redirecting *value)
  1003. {
  1004. chan->redirecting = *value;
  1005. }
  1006. void ast_channel_dtmf_tv_set(struct ast_channel *chan, struct timeval *value)
  1007. {
  1008. chan->dtmf_tv = *value;
  1009. }
  1010. void ast_channel_whentohangup_set(struct ast_channel *chan, struct timeval *value)
  1011. {
  1012. chan->whentohangup = *value;
  1013. }
  1014. void ast_channel_varshead_set(struct ast_channel *chan, struct varshead *value)
  1015. {
  1016. chan->varshead = *value;
  1017. }
  1018. struct timeval ast_channel_creationtime(struct ast_channel *chan)
  1019. {
  1020. return chan->creationtime;
  1021. }
  1022. void ast_channel_creationtime_set(struct ast_channel *chan, struct timeval *value)
  1023. {
  1024. chan->creationtime = *value;
  1025. }
  1026. struct timeval ast_channel_answertime(struct ast_channel *chan)
  1027. {
  1028. return chan->answertime;
  1029. }
  1030. void ast_channel_answertime_set(struct ast_channel *chan, struct timeval *value)
  1031. {
  1032. chan->answertime = *value;
  1033. }
  1034. /* Evil softhangup accessors */
  1035. int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
  1036. {
  1037. return chan->softhangup;
  1038. }
  1039. void ast_channel_softhangup_internal_flag_set(struct ast_channel *chan, int value)
  1040. {
  1041. chan->softhangup = value;
  1042. }
  1043. void ast_channel_softhangup_internal_flag_add(struct ast_channel *chan, int value)
  1044. {
  1045. chan->softhangup |= value;
  1046. }
  1047. void ast_channel_softhangup_internal_flag_clear(struct ast_channel *chan, int value)
  1048. {
  1049. chan ->softhangup &= ~value;
  1050. }
  1051. int ast_channel_unbridged_nolock(struct ast_channel *chan)
  1052. {
  1053. return chan->unbridged;
  1054. }
  1055. int ast_channel_unbridged(struct ast_channel *chan)
  1056. {
  1057. int res;
  1058. ast_channel_lock(chan);
  1059. res = ast_channel_unbridged_nolock(chan);
  1060. ast_channel_unlock(chan);
  1061. return res;
  1062. }
  1063. void ast_channel_set_unbridged_nolock(struct ast_channel *chan, int value)
  1064. {
  1065. chan->unbridged = value;
  1066. ast_queue_frame(chan, &ast_null_frame);
  1067. }
  1068. void ast_channel_set_unbridged(struct ast_channel *chan, int value)
  1069. {
  1070. ast_channel_lock(chan);
  1071. ast_channel_set_unbridged_nolock(chan, value);
  1072. ast_channel_unlock(chan);
  1073. }
  1074. void ast_channel_callid_cleanup(struct ast_channel *chan)
  1075. {
  1076. if (chan->callid) {
  1077. chan->callid = ast_callid_unref(chan->callid);
  1078. }
  1079. }
  1080. /* Typedef accessors */
  1081. ast_group_t ast_channel_callgroup(const struct ast_channel *chan)
  1082. {
  1083. return chan->callgroup;
  1084. }
  1085. void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
  1086. {
  1087. chan->callgroup = value;
  1088. }
  1089. ast_group_t ast_channel_pickupgroup(const struct ast_channel *chan)
  1090. {
  1091. return chan->pickupgroup;
  1092. }
  1093. void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
  1094. {
  1095. chan->pickupgroup = value;
  1096. }
  1097. struct ast_namedgroups *ast_channel_named_callgroups(const struct ast_channel *chan)
  1098. {
  1099. return chan->named_callgroups;
  1100. }
  1101. void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
  1102. {
  1103. ast_unref_namedgroups(chan->named_callgroups);
  1104. chan->named_callgroups = ast_ref_namedgroups(value);
  1105. }
  1106. struct ast_namedgroups *ast_channel_named_pickupgroups(const struct ast_channel *chan)
  1107. {
  1108. return chan->named_pickupgroups;
  1109. }
  1110. void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
  1111. {
  1112. ast_unref_namedgroups(chan->named_pickupgroups);
  1113. chan->named_pickupgroups = ast_ref_namedgroups(value);
  1114. }
  1115. /* Alertpipe functions */
  1116. int ast_channel_alert_write(struct ast_channel *chan)
  1117. {
  1118. char blah = 0x7F;
  1119. return ast_channel_alert_writable(chan) && write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah);
  1120. }
  1121. ast_alert_status_t ast_channel_internal_alert_read(struct ast_channel *chan)
  1122. {
  1123. int flags;
  1124. char blah;
  1125. if (!ast_channel_internal_alert_readable(chan)) {
  1126. return AST_ALERT_NOT_READABLE;
  1127. }
  1128. flags = fcntl(chan->alertpipe[0], F_GETFL);
  1129. /* For some odd reason, the alertpipe occasionally loses nonblocking status,
  1130. * which immediately causes a deadlock scenario. Detect and prevent this. */
  1131. if ((flags & O_NONBLOCK) == 0) {
  1132. ast_log(LOG_ERROR, "Alertpipe on channel %s lost O_NONBLOCK?!!\n", ast_channel_name(chan));
  1133. if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
  1134. ast_log(LOG_WARNING, "Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
  1135. return AST_ALERT_READ_FATAL;
  1136. }
  1137. }
  1138. if (read(chan->alertpipe[0], &blah, sizeof(blah)) < 0) {
  1139. if (errno != EINTR && errno != EAGAIN) {
  1140. ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
  1141. return AST_ALERT_READ_FAIL;
  1142. }
  1143. }
  1144. return AST_ALERT_READ_SUCCESS;
  1145. }
  1146. int ast_channel_alert_writable(struct ast_channel *chan)
  1147. {
  1148. return chan->alertpipe[1] > -1;
  1149. }
  1150. int ast_channel_internal_alert_readable(struct ast_channel *chan)
  1151. {
  1152. return chan->alertpipe[0] > -1;
  1153. }
  1154. void ast_channel_internal_alertpipe_clear(struct ast_channel *chan)
  1155. {
  1156. chan->alertpipe[0] = chan->alertpipe[1] = -1;
  1157. }
  1158. void ast_channel_internal_alertpipe_close(struct ast_channel *chan)
  1159. {
  1160. if (ast_channel_internal_alert_readable(chan)) {
  1161. close(chan->alertpipe[0]);
  1162. }
  1163. if (ast_channel_alert_writable(chan)) {
  1164. close(chan->alertpipe[1]);
  1165. }
  1166. }
  1167. int ast_channel_internal_alertpipe_init(struct ast_channel *chan)
  1168. {
  1169. if (pipe(chan->alertpipe)) {
  1170. ast_log(LOG_WARNING, "Channel allocation failed: Can't create alert pipe! Try increasing max file descriptors with ulimit -n\n");
  1171. return -1;
  1172. } else {
  1173. int flags = fcntl(chan->alertpipe[0], F_GETFL);
  1174. if (fcntl(chan->alertpipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
  1175. ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
  1176. return -1;
  1177. }
  1178. flags = fcntl(chan->alertpipe[1], F_GETFL);
  1179. if (fcntl(chan->alertpipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
  1180. ast_log(LOG_WARNING, "Channel allocation failed: Unable to set alertpipe nonblocking! (%d: %s)\n", errno, strerror(errno));
  1181. return -1;
  1182. }
  1183. }
  1184. return 0;
  1185. }
  1186. int ast_channel_internal_alert_readfd(struct ast_channel *chan)
  1187. {
  1188. return chan->alertpipe[0];
  1189. }
  1190. void ast_channel_internal_alertpipe_swap(struct ast_channel *chan1, struct ast_channel *chan2)
  1191. {
  1192. int i;
  1193. for (i = 0; i < ARRAY_LEN(chan1->alertpipe); i++) {
  1194. SWAP(chan1->alertpipe[i], chan2->alertpipe[i]);
  1195. }
  1196. }
  1197. /* file descriptor array accessors */
  1198. void ast_channel_internal_fd_set(struct ast_channel *chan, int which, int value)
  1199. {
  1200. chan->fds[which] = value;
  1201. }
  1202. void ast_channel_internal_fd_clear(struct ast_channel *chan, int which)
  1203. {
  1204. ast_channel_internal_fd_set(chan, which, -1);
  1205. }
  1206. void ast_channel_internal_fd_clear_all(struct ast_channel *chan)
  1207. {
  1208. int i;
  1209. for (i = 0; i < AST_MAX_FDS; i++) {
  1210. ast_channel_internal_fd_clear(chan, i);
  1211. }
  1212. }
  1213. int ast_channel_fd(const struct ast_channel *chan, int which)
  1214. {
  1215. return chan->fds[which];
  1216. }
  1217. int ast_channel_fd_isset(const struct ast_channel *chan, int which)
  1218. {
  1219. return ast_channel_fd(chan, which) > -1;
  1220. }
  1221. #ifdef HAVE_EPOLL
  1222. struct ast_epoll_data *ast_channel_internal_epfd_data(const struct ast_channel *chan, int which)
  1223. {
  1224. return chan->epfd_data[which];
  1225. }
  1226. void ast_channel_internal_epfd_data_set(struct ast_channel *chan, int which , struct ast_epoll_data *value)
  1227. {
  1228. chan->epfd_data[which] = value;
  1229. }
  1230. #endif
  1231. pthread_t ast_channel_blocker(const struct ast_channel *chan)
  1232. {
  1233. return chan->blocker;
  1234. }
  1235. void ast_channel_blocker_set(struct ast_channel *chan, pthread_t value)
  1236. {
  1237. chan->blocker = value;
  1238. }
  1239. ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)
  1240. {
  1241. return chan->timingfunc;
  1242. }
  1243. void ast_channel_timingfunc_set(struct ast_channel *chan, ast_timing_func_t value)
  1244. {
  1245. chan->timingfunc = value;
  1246. }
  1247. struct ast_bridge *ast_channel_internal_bridge(const struct ast_channel *chan)
  1248. {
  1249. return chan->bridge;
  1250. }
  1251. void ast_channel_internal_bridge_set(struct ast_channel *chan, struct ast_bridge *value)
  1252. {
  1253. chan->bridge = value;
  1254. ast_channel_publish_snapshot(chan);
  1255. }
  1256. struct ast_bridge_channel *ast_channel_internal_bridge_channel(const struct ast_channel *chan)
  1257. {
  1258. return chan->bridge_channel;
  1259. }
  1260. void ast_channel_internal_bridge_channel_set(struct ast_channel *chan, struct ast_bridge_channel *value)
  1261. {
  1262. chan->bridge_channel = value;
  1263. }
  1264. struct ast_flags *ast_channel_flags(struct ast_channel *chan)
  1265. {
  1266. return &chan->flags;
  1267. }
  1268. static int collect_names_cb(void *obj, void *arg, int flags) {
  1269. struct ast_control_pvt_cause_code *cause_code = obj;
  1270. struct ast_str **str = arg;
  1271. ast_str_append(str, 0, "%s%s", (ast_str_strlen(*str) ? "," : ""), cause_code->chan_name);
  1272. return 0;
  1273. }
  1274. struct ast_str *ast_channel_dialed_causes_channels(const struct ast_channel *chan)
  1275. {
  1276. struct ast_str *chanlist = ast_str_create(128);
  1277. if (!chanlist) {
  1278. return NULL;
  1279. }
  1280. ao2_callback(chan->dialed_causes, 0, collect_names_cb, &chanlist);
  1281. return chanlist;
  1282. }
  1283. struct ast_control_pvt_cause_code *ast_channel_dialed_causes_find(const struct ast_channel *chan, const char *chan_name)
  1284. {
  1285. return ao2_find(chan->dialed_causes, chan_name, OBJ_KEY);
  1286. }
  1287. int ast_channel_dialed_causes_add(const struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
  1288. {
  1289. struct ast_control_pvt_cause_code *ao2_cause_code;
  1290. ao2_find(chan->dialed_causes, cause_code->chan_name, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA);
  1291. ao2_cause_code = ao2_alloc(datalen, NULL);
  1292. if (ao2_cause_code) {
  1293. memcpy(ao2_cause_code, cause_code, datalen);
  1294. ao2_link(chan->dialed_causes, ao2_cause_code);
  1295. ao2_ref(ao2_cause_code, -1);
  1296. return 0;
  1297. } else {
  1298. return -1;
  1299. }
  1300. }
  1301. void ast_channel_dialed_causes_clear(const struct ast_channel *chan)
  1302. {
  1303. ao2_callback(chan->dialed_causes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
  1304. }
  1305. /* \brief Hash function for pvt cause code frames */
  1306. static int pvt_cause_hash_fn(const void *vpc, const int flags)
  1307. {
  1308. const struct ast_control_pvt_cause_code *pc = vpc;
  1309. return ast_str_hash(ast_tech_to_upper(ast_strdupa(pc->chan_name)));
  1310. }
  1311. /* \brief Comparison function for pvt cause code frames */
  1312. static int pvt_cause_cmp_fn(void *obj, void *vstr, int flags)
  1313. {
  1314. struct ast_control_pvt_cause_code *pc = obj;
  1315. char *str = ast_tech_to_upper(ast_strdupa(vstr));
  1316. char *pc_str = ast_tech_to_upper(ast_strdupa(pc->chan_name));
  1317. return !strcmp(pc_str, str) ? CMP_MATCH | CMP_STOP : 0;
  1318. }
  1319. #define DIALED_CAUSES_BUCKETS 37
  1320. struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj), const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *file, int line, const char *function)
  1321. {
  1322. struct ast_channel *tmp;
  1323. #if defined(REF_DEBUG)
  1324. tmp = __ao2_alloc_debug(sizeof(*tmp), destructor,
  1325. AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function, 1);
  1326. #elif defined(__AST_DEBUG_MALLOC)
  1327. tmp = __ao2_alloc_debug(sizeof(*tmp), destructor,
  1328. AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function, 0);
  1329. #else
  1330. tmp = ao2_alloc(sizeof(*tmp), destructor);
  1331. #endif
  1332. if ((ast_string_field_init(tmp, 128))) {
  1333. return ast_channel_unref(tmp);
  1334. }
  1335. if (!(tmp->dialed_causes = ao2_container_alloc(DIALED_CAUSES_BUCKETS, pvt_cause_hash_fn, pvt_cause_cmp_fn))) {
  1336. return ast_channel_unref(tmp);
  1337. }
  1338. /* set the creation time in the uniqueid */
  1339. tmp->uniqueid.creation_time = time(NULL);
  1340. tmp->uniqueid.creation_unique = ast_atomic_fetchadd_int(&uniqueint, 1);
  1341. /* use provided id or default to historical {system-}time.# format */
  1342. if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
  1343. ast_copy_string(tmp->uniqueid.unique_id, assignedids->uniqueid, sizeof(tmp->uniqueid.unique_id));
  1344. } else if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
  1345. snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%li.%d",
  1346. (long)(tmp->uniqueid.creation_time),
  1347. tmp->uniqueid.creation_unique);
  1348. } else {
  1349. snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%s-%li.%d",
  1350. ast_config_AST_SYSTEM_NAME,
  1351. (long)(tmp->uniqueid.creation_time),
  1352. tmp->uniqueid.creation_unique);
  1353. }
  1354. /* copy linked id from parent channel if known */
  1355. if (requestor) {
  1356. tmp->linkedid = requestor->linkedid;
  1357. } else {
  1358. tmp->linkedid = tmp->uniqueid;
  1359. }
  1360. return tmp;
  1361. }
  1362. struct ast_channel *ast_channel_internal_oldest_linkedid(struct ast_channel *a, struct ast_channel *b)
  1363. {
  1364. ast_assert(a->linkedid.creation_time != 0);
  1365. ast_assert(b->linkedid.creation_time != 0);
  1366. if (a->linkedid.creation_time < b->linkedid.creation_time) {
  1367. return a;
  1368. }
  1369. if (b->linkedid.creation_time < a->linkedid.creation_time) {
  1370. return b;
  1371. }
  1372. if (a->linkedid.creation_unique < b->linkedid.creation_unique) {
  1373. return a;
  1374. }
  1375. return b;
  1376. }
  1377. void ast_channel_internal_copy_linkedid(struct ast_channel *dest, struct ast_channel *source)
  1378. {
  1379. if (dest->linkedid.creation_time == source->linkedid.creation_time
  1380. && dest->linkedid.creation_unique == source->linkedid.creation_unique
  1381. && !strcmp(dest->linkedid.unique_id, source->linkedid.unique_id)) {
  1382. return;
  1383. }
  1384. dest->linkedid = source->linkedid;
  1385. ast_channel_publish_snapshot(dest);
  1386. }
  1387. void ast_channel_internal_swap_uniqueid_and_linkedid(struct ast_channel *a, struct ast_channel *b)
  1388. {
  1389. struct ast_channel_id temp;
  1390. temp = a->uniqueid;
  1391. a->uniqueid = b->uniqueid;
  1392. b->uniqueid = temp;
  1393. temp = a->linkedid;
  1394. a->linkedid = b->linkedid;
  1395. b->linkedid = temp;
  1396. }
  1397. void ast_channel_internal_swap_topics(struct ast_channel *a, struct ast_channel *b)
  1398. {
  1399. struct stasis_cp_single *temp;
  1400. temp = a->topics;
  1401. a->topics = b->topics;
  1402. b->topics = temp;
  1403. }
  1404. void ast_channel_internal_set_fake_ids(struct ast_channel *chan, const char *uniqueid, const char *linkedid)
  1405. {
  1406. ast_copy_string(chan->uniqueid.unique_id, uniqueid, sizeof(chan->uniqueid.unique_id));
  1407. ast_copy_string(chan->linkedid.unique_id, linkedid, sizeof(chan->linkedid.unique_id));
  1408. }
  1409. void ast_channel_internal_cleanup(struct ast_channel *chan)
  1410. {
  1411. if (chan->dialed_causes) {
  1412. ao2_t_ref(chan->dialed_causes, -1,
  1413. "done with dialed causes since the channel is going away");
  1414. chan->dialed_causes = NULL;
  1415. }
  1416. ast_string_field_free_memory(chan);
  1417. chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
  1418. chan->endpoint_cache_forward = stasis_forward_cancel(chan->endpoint_cache_forward);
  1419. stasis_cp_single_unsubscribe(chan->topics);
  1420. chan->topics = NULL;
  1421. }
  1422. void ast_channel_internal_finalize(struct ast_channel *chan)
  1423. {
  1424. chan->finalized = 1;
  1425. }
  1426. int ast_channel_internal_is_finalized(struct ast_channel *chan)
  1427. {
  1428. return chan->finalized;
  1429. }
  1430. struct stasis_topic *ast_channel_topic(struct ast_channel *chan)
  1431. {
  1432. if (!chan) {
  1433. return ast_channel_topic_all();
  1434. }
  1435. return stasis_cp_single_topic(chan->topics);
  1436. }
  1437. struct stasis_topic *ast_channel_topic_cached(struct ast_channel *chan)
  1438. {
  1439. if (!chan) {
  1440. return ast_channel_topic_all_cached();
  1441. }
  1442. return stasis_cp_single_topic_cached(chan->topics);
  1443. }
  1444. int ast_channel_forward_endpoint(struct ast_channel *chan,
  1445. struct ast_endpoint *endpoint)
  1446. {
  1447. ast_assert(chan != NULL);
  1448. ast_assert(endpoint != NULL);
  1449. chan->endpoint_forward =
  1450. stasis_forward_all(ast_channel_topic(chan),
  1451. ast_endpoint_topic(endpoint));
  1452. if (!chan->endpoint_forward) {
  1453. return -1;
  1454. }
  1455. chan->endpoint_cache_forward = stasis_forward_all(ast_channel_topic_cached(chan),
  1456. ast_endpoint_topic(endpoint));
  1457. if (!chan->endpoint_cache_forward) {
  1458. chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
  1459. return -1;
  1460. }
  1461. return 0;
  1462. }
  1463. int ast_channel_internal_setup_topics(struct ast_channel *chan)
  1464. {
  1465. const char *topic_name = chan->uniqueid.unique_id;
  1466. ast_assert(chan->topics == NULL);
  1467. if (ast_strlen_zero(topic_name)) {
  1468. topic_name = "<dummy-channel>";
  1469. }
  1470. chan->topics = stasis_cp_single_create(
  1471. ast_channel_cache_all(), topic_name);
  1472. if (!chan->topics) {
  1473. return -1;
  1474. }
  1475. return 0;
  1476. }