channel_internal_api.c 52 KB

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