~singpolyma/asterisk

ref: 51af79b42e4ae7d1831603633d73bf0df179720e asterisk/main/channel_internal_api.c -rw-r--r-- 46.6 KiB
51af79b4Christopher Vollick Content Created By Initiator 2: Electric Boogaloo 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2012, Digium, Inc.
 *
 * Mark Spencer <markster@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Channel Accessor API
 *
 * This file is intended to be the only file that ever accesses the
 * internals of an ast_channel. All other files should use the
 * accessor functions defined here.
 *
 * \author Terry Wilson
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include <unistd.h>
#include <fcntl.h>

#include "asterisk/alertpipe.h"
#include "asterisk/paths.h"
#include "asterisk/channel.h"
#include "asterisk/channel_internal.h"
#include "asterisk/endpoints.h"
#include "asterisk/indications.h"
#include "asterisk/stasis_cache_pattern.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/stasis_endpoints.h"
#include "asterisk/stringfields.h"
#include "asterisk/stream.h"
#include "asterisk/test.h"
#include "asterisk/vector.h"

/*!
 * \brief Channel UniqueId structure
 * \note channel creation time used for determining LinkedId Propagation
 */
struct ast_channel_id {
	time_t creation_time;				/*!< Creation time */
	int creation_unique;				/*!< sub-second unique value */
	char unique_id[AST_MAX_UNIQUEID];	/*!< Unique Identifier */
};

/*!
 * \brief Main Channel structure associated with a channel.
 *
 * \note When adding fields to this structure, it is important to add the field
 *       'in position' with like-aligned fields, so as to keep the compiler from
 *       having to add padding to align fields. The structure's fields are sorted
 *       in this order: pointers, structures, long, int/enum, short, char. This
 *       is especially important on 64-bit architectures, where mixing 4-byte
 *       and 8-byte fields causes 4 bytes of padding to be added before many
 *       8-byte fields.
 */
struct ast_channel {
	const struct ast_channel_tech *tech;		/*!< Technology (point to channel driver) */
	void *tech_pvt;					/*!< Private data used by the technology driver */
	void *music_state;				/*!< Music State*/
	void *generatordata;				/*!< Current generator data if there is any */
	struct ast_generator *generator;		/*!< Current active data generator */
	struct ast_channel *masq;			/*!< Channel that will masquerade as us */
	struct ast_channel *masqr;			/*!< Who we are masquerading as */
	const char *blockproc;				/*!< Procedure causing blocking */
	const char *appl;				/*!< Current application */
	const char *data;				/*!< Data passed to current application */
	struct ast_sched_context *sched;                /*!< Schedule context */
	struct ast_filestream *stream;			/*!< Stream itself. */
	struct ast_filestream *vstream;			/*!< Video Stream itself. */
	ast_timing_func_t timingfunc;
	void *timingdata;
	struct ast_pbx *pbx;				/*!< PBX private structure for this channel */
	struct ast_trans_pvt *writetrans;		/*!< Write translation path */
	struct ast_trans_pvt *readtrans;		/*!< Read translation path */
	struct ast_audiohook_list *audiohooks;
	struct ast_framehook_list *framehooks;
	struct ast_cdr *cdr;				/*!< Call Detail Record */
	struct ast_tone_zone *zone;			/*!< Tone zone as set in indications.conf or
							 *   in the CHANNEL dialplan function */
	struct ast_channel_monitor *monitor;		/*!< Channel monitoring */
	ast_callid callid;			/*!< Bound call identifier pointer */
	struct ao2_container *dialed_causes;		/*!< Contains tech-specific and Asterisk cause data from dialed channels */

	AST_DECLARE_STRING_FIELDS(
		AST_STRING_FIELD(name);         /*!< ASCII unique channel name */
		AST_STRING_FIELD(language);     /*!< Language requested for voice prompts */
		AST_STRING_FIELD(musicclass);   /*!< Default music class */
		AST_STRING_FIELD(latest_musicclass);   /*!< Latest active music class */
		AST_STRING_FIELD(accountcode);  /*!< Account code for billing */
		AST_STRING_FIELD(peeraccount);  /*!< Peer account code for billing */
		AST_STRING_FIELD(userfield);    /*!< Userfield for CEL billing */
		AST_STRING_FIELD(call_forward); /*!< Where to forward to if asked to dial on this interface */
		AST_STRING_FIELD(parkinglot);   /*! Default parking lot, if empty, default parking lot  */
		AST_STRING_FIELD(hangupsource); /*! Who is responsible for hanging up this channel */
		AST_STRING_FIELD(dialcontext);  /*!< Dial: Extension context that we were called from */
	);

	struct ast_channel_id uniqueid;		/*!< Unique Channel Identifier - can be specified on creation */
	struct ast_channel_id linkedid;		/*!< Linked Channel Identifier - oldest propagated when bridged */

	struct timeval whentohangup; /*!< Non-zero, set to actual time when channel is to be hung up */
	pthread_t blocker;           /*!< If anyone is blocking, this is them */

	/*!
	 * \brief Dialed/Called information.
	 * \note Set on incoming channels to indicate the originally dialed party.
	 * \note Dialed Number Identifier (DNID)
	 */
	struct ast_party_dialed dialed;

	/*!
	 * \brief Channel Caller ID information.
	 * \note The caller id information is the caller id of this
	 * channel when it is used to initiate a call.
	 */
	struct ast_party_caller caller;

	/*!
	 * \brief Channel Connected Line ID information.
	 * \note The connected line information identifies the channel
	 * connected/bridged to this channel.
	 */
	struct ast_party_connected_line connected;

	/*!
	 * \brief Channel Connected Line ID information that was last indicated.
	 */
	struct ast_party_connected_line connected_indicated;

	/*! \brief Redirecting/Diversion information */
	struct ast_party_redirecting redirecting;

	struct ast_frame dtmff;				/*!< DTMF frame */
	struct varshead varshead;			/*!< A linked list for channel variables. See \ref AstChanVar */
	ast_group_t callgroup;				/*!< Call group for call pickups */
	ast_group_t pickupgroup;			/*!< Pickup group - which calls groups can be picked up? */
	struct ast_namedgroups *named_callgroups;	/*!< Named call group for call pickups */
	struct ast_namedgroups *named_pickupgroups;	/*!< Named pickup group - which call groups can be picked up? */
	struct timeval creationtime;			/*!< The time of channel creation */
	struct timeval answertime;				/*!< The time the channel was answered */
	struct ast_readq_list readq;
	struct ast_jb jb;				/*!< The jitterbuffer state */
	struct timeval dtmf_tv;				/*!< The time that an in process digit began, or the last digit ended */
	struct ast_hangup_handler_list hangup_handlers;/*!< Hangup handlers on the channel. */
	struct ast_datastore_list datastores; /*!< Data stores on the channel */
	struct ast_autochan_list autochans; /*!< Autochans on the channel */
	unsigned long insmpl;				/*!< Track the read/written samples for monitor use */
	unsigned long outsmpl;				/*!< Track the read/written samples for monitor use */

	int blocker_tid;					/*!< If anyone is blocking, this is their thread id */
	AST_VECTOR(, int) fds;				/*!< File descriptors for channel -- Drivers will poll on
							 *   these file descriptors, so at least one must be non -1.
							 *   See \arg \ref AstFileDesc */
	int softhangup;				/*!< Whether or not we have been hung up...  Do not set this value
							 *   directly, use ast_softhangup() */
	int fdno;					/*!< Which fd had an event detected on */
	int streamid;					/*!< For streaming playback, the schedule ID */
	int vstreamid;					/*!< For streaming video playback, the schedule ID */
	struct ast_format *oldwriteformat;  /*!< Original writer format */
	int timingfd;					/*!< Timing fd */
	enum ast_channel_state state;			/*!< State of line -- Don't write directly, use ast_setstate() */
	int rings;					/*!< Number of rings so far */
	int priority;					/*!< Dialplan: Current extension priority */
	int macropriority;				/*!< Macro: Current non-macro priority. See app_macro.c */
	int amaflags;					/*!< Set BEFORE PBX is started to determine AMA flags */
	enum ast_channel_adsicpe adsicpe;		/*!< Whether or not ADSI is detected on CPE */
	unsigned int fin;				/*!< Frames in counters. The high bit is a debug mask, so
							 *   the counter is only in the remaining bits */
	unsigned int fout;				/*!< Frames out counters. The high bit is a debug mask, so
							 *   the counter is only in the remaining bits */
	int hangupcause;				/*!< Why is the channel hanged up. See causes.h */
	unsigned int finalized:1;       /*!< Whether or not the channel has been successfully allocated */
	struct ast_flags flags;				/*!< channel flags of AST_FLAG_ type */
	int alertpipe[2];
	struct ast_format_cap *nativeformats;         /*!< Kinds of data this channel can natively handle */
	struct ast_format *readformat;            /*!< Requested read format (after translation) */
	struct ast_format *writeformat;           /*!< Requested write format (before translation) */
	struct ast_format *rawreadformat;         /*!< Raw read format (before translation) */
	struct ast_format *rawwriteformat;        /*!< Raw write format (after translation) */
	unsigned int emulate_dtmf_duration;		/*!< Number of ms left to emulate DTMF for */
	int visible_indication;                         /*!< Indication currently playing on the channel */
	int hold_state;							/*!< Current Hold/Unhold state */

	unsigned short transfercapability;		/*!< ISDN Transfer Capability - AST_FLAG_DIGITAL is not enough */

	struct ast_bridge *bridge;                      /*!< Bridge this channel is participating in */
	struct ast_bridge_channel *bridge_channel;/*!< The bridge_channel this channel is linked with. */
	struct ast_timer *timer;			/*!< timer object that provided timingfd */

	char context[AST_MAX_CONTEXT];			/*!< Dialplan: Current extension context */
	char exten[AST_MAX_EXTENSION];			/*!< Dialplan: Current extension number */
	char macrocontext[AST_MAX_CONTEXT];		/*!< Macro: Current non-macro context. See app_macro.c */
	char macroexten[AST_MAX_EXTENSION];		/*!< Macro: Current non-macro extension. See app_macro.c */
	char unbridged;							/*!< non-zero if the bridge core needs to re-evaluate the current
											 bridging technology which is in use by this channel's bridge. */
	char is_t38_active;						/*!< non-zero if T.38 is active on this channel. */
	char dtmf_digit_to_emulate;			/*!< Digit being emulated */
	char sending_dtmf_digit;			/*!< Digit this channel is currently sending out. (zero if not sending) */
	struct timeval sending_dtmf_tv;		/*!< The time this channel started sending the current digit. (Invalid if sending_dtmf_digit is zero.) */
	struct stasis_cp_single *topics;		/*!< Topic for all channel's events */
	struct stasis_forward *endpoint_forward;	/*!< Subscription for event forwarding to endpoint's topic */
	struct stasis_forward *endpoint_cache_forward; /*!< Subscription for cache updates to endpoint's topic */
	struct ast_stream_topology *stream_topology; /*!< Stream topology */
	void *stream_topology_change_source; /*!< Source that initiated a stream topology change */
	struct ast_stream *default_streams[AST_MEDIA_TYPE_END]; /*!< Default streams indexed by media type */
};

/*! \brief The monotonically increasing integer counter for channel uniqueids */
static int uniqueint;

/* ACCESSORS */

#define DEFINE_STRINGFIELD_SETTERS_FOR(field, publish, assert_on_null) \
void ast_channel_##field##_set(struct ast_channel *chan, const char *value) \
{ \
	if ((assert_on_null)) ast_assert(!ast_strlen_zero(value)); \
	if (!strcmp(value, chan->field)) return; \
	ast_string_field_set(chan, field, value); \
	if (publish && ast_channel_internal_is_finalized(chan)) ast_channel_publish_snapshot(chan); \
} \
  \
void ast_channel_##field##_build_va(struct ast_channel *chan, const char *fmt, va_list ap) \
{ \
	ast_string_field_build_va(chan, field, fmt, ap); \
	if (publish && ast_channel_internal_is_finalized(chan)) ast_channel_publish_snapshot(chan); \
} \
void ast_channel_##field##_build(struct ast_channel *chan, const char *fmt, ...) \
{ \
	va_list ap; \
	va_start(ap, fmt); \
	ast_channel_##field##_build_va(chan, fmt, ap); \
	va_end(ap); \
}

DEFINE_STRINGFIELD_SETTERS_FOR(name, 0, 1);
DEFINE_STRINGFIELD_SETTERS_FOR(language, 1, 0);
DEFINE_STRINGFIELD_SETTERS_FOR(musicclass, 0, 0);
DEFINE_STRINGFIELD_SETTERS_FOR(latest_musicclass, 0, 0);
DEFINE_STRINGFIELD_SETTERS_FOR(accountcode, 1, 0);
DEFINE_STRINGFIELD_SETTERS_FOR(peeraccount, 1, 0);
DEFINE_STRINGFIELD_SETTERS_FOR(userfield, 0, 0);
DEFINE_STRINGFIELD_SETTERS_FOR(call_forward, 0, 0);
DEFINE_STRINGFIELD_SETTERS_FOR(parkinglot, 0, 0);
DEFINE_STRINGFIELD_SETTERS_FOR(hangupsource, 0, 0);
DEFINE_STRINGFIELD_SETTERS_FOR(dialcontext, 0, 0);

#define DEFINE_STRINGFIELD_GETTER_FOR(field) const char *ast_channel_##field(const struct ast_channel *chan) \
{ \
	return chan->field; \
}

DEFINE_STRINGFIELD_GETTER_FOR(name);
DEFINE_STRINGFIELD_GETTER_FOR(language);
DEFINE_STRINGFIELD_GETTER_FOR(musicclass);
DEFINE_STRINGFIELD_GETTER_FOR(latest_musicclass);
DEFINE_STRINGFIELD_GETTER_FOR(accountcode);
DEFINE_STRINGFIELD_GETTER_FOR(peeraccount);
DEFINE_STRINGFIELD_GETTER_FOR(userfield);
DEFINE_STRINGFIELD_GETTER_FOR(call_forward);
DEFINE_STRINGFIELD_GETTER_FOR(parkinglot);
DEFINE_STRINGFIELD_GETTER_FOR(hangupsource);
DEFINE_STRINGFIELD_GETTER_FOR(dialcontext);

const char *ast_channel_uniqueid(const struct ast_channel *chan)
{
	ast_assert(chan->uniqueid.unique_id[0] != '\0');
	return chan->uniqueid.unique_id;
}

const char *ast_channel_linkedid(const struct ast_channel *chan)
{
	ast_assert(chan->linkedid.unique_id[0] != '\0');
	return chan->linkedid.unique_id;
}

const char *ast_channel_appl(const struct ast_channel *chan)
{
	return chan->appl;
}
void ast_channel_appl_set(struct ast_channel *chan, const char *value)
{
	chan->appl = value;
}
const char *ast_channel_blockproc(const struct ast_channel *chan)
{
	return chan->blockproc;
}
void ast_channel_blockproc_set(struct ast_channel *chan, const char *value)
{
	chan->blockproc = value;
}
const char *ast_channel_data(const struct ast_channel *chan)
{
	return chan->data;
}
void ast_channel_data_set(struct ast_channel *chan, const char *value)
{
	chan->data = value;
}

const char *ast_channel_context(const struct ast_channel *chan)
{
	return chan->context;
}
void ast_channel_context_set(struct ast_channel *chan, const char *value)
{
	ast_copy_string(chan->context, value, sizeof(chan->context));
}
const char *ast_channel_exten(const struct ast_channel *chan)
{
	return chan->exten;
}
void ast_channel_exten_set(struct ast_channel *chan, const char *value)
{
	ast_copy_string(chan->exten, value, sizeof(chan->exten));
}
const char *ast_channel_macrocontext(const struct ast_channel *chan)
{
	return chan->macrocontext;
}
void ast_channel_macrocontext_set(struct ast_channel *chan, const char *value)
{
	ast_copy_string(chan->macrocontext, value, sizeof(chan->macrocontext));
}
const char *ast_channel_macroexten(const struct ast_channel *chan)
{
	return chan->macroexten;
}
void ast_channel_macroexten_set(struct ast_channel *chan, const char *value)
{
	ast_copy_string(chan->macroexten, value, sizeof(chan->macroexten));
}

char ast_channel_dtmf_digit_to_emulate(const struct ast_channel *chan)
{
	return chan->dtmf_digit_to_emulate;
}
void ast_channel_dtmf_digit_to_emulate_set(struct ast_channel *chan, char value)
{
	chan->dtmf_digit_to_emulate = value;
}

char ast_channel_sending_dtmf_digit(const struct ast_channel *chan)
{
	return chan->sending_dtmf_digit;
}
void ast_channel_sending_dtmf_digit_set(struct ast_channel *chan, char value)
{
	chan->sending_dtmf_digit = value;
}

struct timeval ast_channel_sending_dtmf_tv(const struct ast_channel *chan)
{
	return chan->sending_dtmf_tv;
}
void ast_channel_sending_dtmf_tv_set(struct ast_channel *chan, struct timeval value)
{
	chan->sending_dtmf_tv = value;
}

enum ama_flags ast_channel_amaflags(const struct ast_channel *chan)
{
	return chan->amaflags;
}

void ast_channel_amaflags_set(struct ast_channel *chan, enum ama_flags value)
{
	if (chan->amaflags == value) {
		return;
	}
	chan->amaflags = value;
	ast_channel_publish_snapshot(chan);
}
int ast_channel_fdno(const struct ast_channel *chan)
{
	return chan->fdno;
}
void ast_channel_fdno_set(struct ast_channel *chan, int value)
{
	chan->fdno = value;
}
int ast_channel_hangupcause(const struct ast_channel *chan)
{
	return chan->hangupcause;
}
void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
{
	chan->hangupcause = value;
}
int ast_channel_macropriority(const struct ast_channel *chan)
{
	return chan->macropriority;
}
void ast_channel_macropriority_set(struct ast_channel *chan, int value)
{
	chan->macropriority = value;
}
int ast_channel_priority(const struct ast_channel *chan)
{
	return chan->priority;
}
void ast_channel_priority_set(struct ast_channel *chan, int value)
{
	chan->priority = value;
}
int ast_channel_rings(const struct ast_channel *chan)
{
	return chan->rings;
}
void ast_channel_rings_set(struct ast_channel *chan, int value)
{
	chan->rings = value;
}
int ast_channel_streamid(const struct ast_channel *chan)
{
	return chan->streamid;
}
void ast_channel_streamid_set(struct ast_channel *chan, int value)
{
	chan->streamid = value;
}
int ast_channel_timingfd(const struct ast_channel *chan)
{
	return chan->timingfd;
}
void ast_channel_timingfd_set(struct ast_channel *chan, int value)
{
	chan->timingfd = value;
}
int ast_channel_visible_indication(const struct ast_channel *chan)
{
	return chan->visible_indication;
}
void ast_channel_visible_indication_set(struct ast_channel *chan, int value)
{
	chan->visible_indication = value;
}
int ast_channel_hold_state(const struct ast_channel *chan)
{
	return chan->hold_state;
}
void ast_channel_hold_state_set(struct ast_channel *chan, int value)
{
	chan->hold_state = value;
}
int ast_channel_vstreamid(const struct ast_channel *chan)
{
	return chan->vstreamid;
}
void ast_channel_vstreamid_set(struct ast_channel *chan, int value)
{
	chan->vstreamid = value;
}
unsigned short ast_channel_transfercapability(const struct ast_channel *chan)
{
	return chan->transfercapability;
}
void ast_channel_transfercapability_set(struct ast_channel *chan, unsigned short value)
{
	chan->transfercapability = value;
}
unsigned int ast_channel_emulate_dtmf_duration(const struct ast_channel *chan)
{
	return chan->emulate_dtmf_duration;
}
void ast_channel_emulate_dtmf_duration_set(struct ast_channel *chan, unsigned int value)
{
	chan->emulate_dtmf_duration = value;
}
unsigned int ast_channel_fin(const struct ast_channel *chan)
{
	return chan->fin;
}
void ast_channel_fin_set(struct ast_channel *chan, unsigned int value)
{
	chan->fin = value;
}
unsigned int ast_channel_fout(const struct ast_channel *chan)
{
	return chan->fout;
}
void ast_channel_fout_set(struct ast_channel *chan, unsigned int value)
{
	chan->fout = value;
}
unsigned long ast_channel_insmpl(const struct ast_channel *chan)
{
	return chan->insmpl;
}
void ast_channel_insmpl_set(struct ast_channel *chan, unsigned long value)
{
	chan->insmpl = value;
}
unsigned long ast_channel_outsmpl(const struct ast_channel *chan)
{
	return chan->outsmpl;
}
void ast_channel_outsmpl_set(struct ast_channel *chan, unsigned long value)
{
	chan->outsmpl = value;
}
void *ast_channel_generatordata(const struct ast_channel *chan)
{
	return chan->generatordata;
}
void ast_channel_generatordata_set(struct ast_channel *chan, void *value)
{
	chan->generatordata = value;
}
void *ast_channel_music_state(const struct ast_channel *chan)
{
	return chan->music_state;
}
void ast_channel_music_state_set(struct ast_channel *chan, void *value)
{
	chan->music_state = value;
}
void *ast_channel_tech_pvt(const struct ast_channel *chan)
{
	return chan->tech_pvt;
}
void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
{
	chan->tech_pvt = value;
}
void *ast_channel_timingdata(const struct ast_channel *chan)
{
	return chan->timingdata;
}
void ast_channel_timingdata_set(struct ast_channel *chan, void *value)
{
	chan->timingdata = value;
}
struct ast_audiohook_list *ast_channel_audiohooks(const struct ast_channel *chan)
{
	return chan->audiohooks;
}
void ast_channel_audiohooks_set(struct ast_channel *chan, struct ast_audiohook_list *value)
{
	chan->audiohooks = value;
}
struct ast_cdr *ast_channel_cdr(const struct ast_channel *chan)
{
	return chan->cdr;
}
void ast_channel_cdr_set(struct ast_channel *chan, struct ast_cdr *value)
{
	chan->cdr = value;
}
struct ast_channel *ast_channel_masq(const struct ast_channel *chan)
{
	return chan->masq;
}
void ast_channel_masq_set(struct ast_channel *chan, struct ast_channel *value)
{
	chan->masq = value;
}
struct ast_channel *ast_channel_masqr(const struct ast_channel *chan)
{
	return chan->masqr;
}
void ast_channel_masqr_set(struct ast_channel *chan, struct ast_channel *value)
{
	chan->masqr = value;
}
struct ast_channel_monitor *ast_channel_monitor(const struct ast_channel *chan)
{
	return chan->monitor;
}
void ast_channel_monitor_set(struct ast_channel *chan, struct ast_channel_monitor *value)
{
	chan->monitor = value;
}
struct ast_filestream *ast_channel_stream(const struct ast_channel *chan)
{
	return chan->stream;
}
void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
{
	chan->stream = value;
}
struct ast_filestream *ast_channel_vstream(const struct ast_channel *chan)
{
	return chan->vstream;
}
void ast_channel_vstream_set(struct ast_channel *chan, struct ast_filestream *value)
{
	chan->vstream = value;
}
struct ast_format_cap *ast_channel_nativeformats(const struct ast_channel *chan)
{
	return chan->nativeformats;
}

static void channel_set_default_streams(struct ast_channel *chan)
{
	enum ast_media_type type;

	ast_assert(chan != NULL);

	for (type = AST_MEDIA_TYPE_UNKNOWN; type < AST_MEDIA_TYPE_END; type++) {
		if (chan->stream_topology) {
			chan->default_streams[type] =
				ast_stream_topology_get_first_stream_by_type(chan->stream_topology, type);
		} else {
			chan->default_streams[type] = NULL;
		}
	}
}

void ast_channel_internal_set_stream_topology(struct ast_channel *chan,
	struct ast_stream_topology *topology)
{
	ast_stream_topology_free(chan->stream_topology);
	chan->stream_topology = topology;
	channel_set_default_streams(chan);
}

void ast_channel_internal_set_stream_topology_change_source(
	struct ast_channel *chan, void *change_source)
{
	chan->stream_topology_change_source = change_source;
}

void *ast_channel_get_stream_topology_change_source(struct ast_channel *chan)
{
	return chan->stream_topology_change_source;
}

void ast_channel_nativeformats_set(struct ast_channel *chan,
	struct ast_format_cap *value)
{
	ast_assert(chan != NULL);

	ao2_replace(chan->nativeformats, value);

	/* If chan->stream_topology is NULL, the channel is being destroyed
	 * and topology is destroyed.
	 */
	if (!chan->stream_topology) {
		return;
	}

	if (!ast_channel_is_multistream(chan) || !value) {
		struct ast_stream_topology *new_topology;

		new_topology = ast_stream_topology_create_from_format_cap(value);
		ast_channel_internal_set_stream_topology(chan, new_topology);
	}
}

struct ast_framehook_list *ast_channel_framehooks(const struct ast_channel *chan)
{
	return chan->framehooks;
}
void ast_channel_framehooks_set(struct ast_channel *chan, struct ast_framehook_list *value)
{
	chan->framehooks = value;
}
struct ast_generator *ast_channel_generator(const struct ast_channel *chan)
{
	return chan->generator;
}
void ast_channel_generator_set(struct ast_channel *chan, struct ast_generator *value)
{
	chan->generator = value;
}
struct ast_pbx *ast_channel_pbx(const struct ast_channel *chan)
{
	return chan->pbx;
}
void ast_channel_pbx_set(struct ast_channel *chan, struct ast_pbx *value)
{
	chan->pbx = value;
}
struct ast_sched_context *ast_channel_sched(const struct ast_channel *chan)
{
	return chan->sched;
}
void ast_channel_sched_set(struct ast_channel *chan, struct ast_sched_context *value)
{
	chan->sched = value;
}
struct ast_timer *ast_channel_timer(const struct ast_channel *chan)
{
	return chan->timer;
}
void ast_channel_timer_set(struct ast_channel *chan, struct ast_timer *value)
{
	chan->timer = value;
}
struct ast_tone_zone *ast_channel_zone(const struct ast_channel *chan)
{
	return chan->zone;
}
void ast_channel_zone_set(struct ast_channel *chan, struct ast_tone_zone *value)
{
	chan->zone = value;
}
struct ast_trans_pvt *ast_channel_readtrans(const struct ast_channel *chan)
{
	return chan->readtrans;
}
void ast_channel_readtrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
{
	chan->readtrans = value;
}
struct ast_trans_pvt *ast_channel_writetrans(const struct ast_channel *chan)
{
	return chan->writetrans;
}
void ast_channel_writetrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
{
	chan->writetrans = value;
}
const struct ast_channel_tech *ast_channel_tech(const struct ast_channel *chan)
{
	return chan->tech;
}
void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
{
	if (value->read_stream || value->write_stream) {
		ast_assert(value->read_stream && value->write_stream);
	}

	chan->tech = value;
}
enum ast_channel_adsicpe ast_channel_adsicpe(const struct ast_channel *chan)
{
	return chan->adsicpe;
}
void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
{
	chan->adsicpe = value;
}
enum ast_channel_state ast_channel_state(const struct ast_channel *chan)
{
	return chan->state;
}
ast_callid ast_channel_callid(const struct ast_channel *chan)
{
	return chan->callid;
}
void ast_channel_callid_set(struct ast_channel *chan, ast_callid callid)
{
	char call_identifier_from[AST_CALLID_BUFFER_LENGTH];
	char call_identifier_to[AST_CALLID_BUFFER_LENGTH];
	call_identifier_from[0] = '\0';
	ast_callid_strnprint(call_identifier_to, sizeof(call_identifier_to), callid);
	if (chan->callid) {
		ast_callid_strnprint(call_identifier_from, sizeof(call_identifier_from), chan->callid);
		ast_debug(3, "Channel Call ID changing from %s to %s\n", call_identifier_from, call_identifier_to);
	}

	chan->callid = callid;

	ast_test_suite_event_notify("CallIDChange",
		"State: CallIDChange\r\n"
		"Channel: %s\r\n"
		"CallID: %s\r\n"
		"PriorCallID: %s",
		ast_channel_name(chan),
		call_identifier_to,
		call_identifier_from);
}

void ast_channel_state_set(struct ast_channel *chan, enum ast_channel_state value)
{
	chan->state = value;
}
void ast_channel_set_oldwriteformat(struct ast_channel *chan, struct ast_format *format)
{
	ao2_replace(chan->oldwriteformat, format);
}
void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
{
	ao2_replace(chan->rawreadformat, format);
}
void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
{
	ao2_replace(chan->rawwriteformat, format);
}
void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
{
	ao2_replace(chan->readformat, format);
}
void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
{
	ao2_replace(chan->writeformat, format);
}
struct ast_format *ast_channel_oldwriteformat(struct ast_channel *chan)
{
	return chan->oldwriteformat;
}
struct ast_format *ast_channel_rawreadformat(struct ast_channel *chan)
{
	return chan->rawreadformat;
}
struct ast_format *ast_channel_rawwriteformat(struct ast_channel *chan)
{
	return chan->rawwriteformat;
}
struct ast_format *ast_channel_readformat(struct ast_channel *chan)
{
	return chan->readformat;
}
struct ast_format *ast_channel_writeformat(struct ast_channel *chan)
{
	return chan->writeformat;
}
struct ast_hangup_handler_list *ast_channel_hangup_handlers(struct ast_channel *chan)
{
	return &chan->hangup_handlers;
}
struct ast_datastore_list *ast_channel_datastores(struct ast_channel *chan)
{
	return &chan->datastores;
}
struct ast_autochan_list *ast_channel_autochans(struct ast_channel *chan)
{
	return &chan->autochans;
}
struct ast_readq_list *ast_channel_readq(struct ast_channel *chan)
{
	return &chan->readq;
}
struct ast_frame *ast_channel_dtmff(struct ast_channel *chan)
{
	return &chan->dtmff;
}
struct ast_jb *ast_channel_jb(struct ast_channel *chan)
{
	return &chan->jb;
}
struct ast_party_caller *ast_channel_caller(struct ast_channel *chan)
{
	return &chan->caller;
}
struct ast_party_connected_line *ast_channel_connected(struct ast_channel *chan)
{
	return &chan->connected;
}
struct ast_party_connected_line *ast_channel_connected_indicated(struct ast_channel *chan)
{
	return &chan->connected_indicated;
}
struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
{
	return ast_party_id_merge(&chan->connected.id, &chan->connected.priv);
}
struct ast_party_dialed *ast_channel_dialed(struct ast_channel *chan)
{
	return &chan->dialed;
}
struct ast_party_redirecting *ast_channel_redirecting(struct ast_channel *chan)
{
	return &chan->redirecting;
}
struct ast_party_id ast_channel_redirecting_effective_orig(struct ast_channel *chan)
{
	return ast_party_id_merge(&chan->redirecting.orig, &chan->redirecting.priv_orig);
}
struct ast_party_id ast_channel_redirecting_effective_from(struct ast_channel *chan)
{
	return ast_party_id_merge(&chan->redirecting.from, &chan->redirecting.priv_from);
}
struct ast_party_id ast_channel_redirecting_effective_to(struct ast_channel *chan)
{
	return ast_party_id_merge(&chan->redirecting.to, &chan->redirecting.priv_to);
}
struct timeval *ast_channel_dtmf_tv(struct ast_channel *chan)
{
	return &chan->dtmf_tv;
}
struct timeval *ast_channel_whentohangup(struct ast_channel *chan)
{
	return &chan->whentohangup;
}
struct varshead *ast_channel_varshead(struct ast_channel *chan)
{
	return &chan->varshead;
}
void ast_channel_dtmff_set(struct ast_channel *chan, struct ast_frame *value)
{
	chan->dtmff = *value;
}
void ast_channel_jb_set(struct ast_channel *chan, struct ast_jb *value)
{
	chan->jb = *value;
}
void ast_channel_caller_set(struct ast_channel *chan, struct ast_party_caller *value)
{
	chan->caller = *value;
}
void ast_channel_connected_set(struct ast_channel *chan, struct ast_party_connected_line *value)
{
	chan->connected = *value;
}
void ast_channel_dialed_set(struct ast_channel *chan, struct ast_party_dialed *value)
{
	chan->dialed = *value;
}
void ast_channel_redirecting_set(struct ast_channel *chan, struct ast_party_redirecting *value)
{
	chan->redirecting = *value;
}
void ast_channel_dtmf_tv_set(struct ast_channel *chan, struct timeval *value)
{
	chan->dtmf_tv = *value;
}
void ast_channel_whentohangup_set(struct ast_channel *chan, struct timeval *value)
{
	chan->whentohangup = *value;
}
void ast_channel_varshead_set(struct ast_channel *chan, struct varshead *value)
{
	chan->varshead = *value;
}
struct timeval ast_channel_creationtime(struct ast_channel *chan)
{
	return chan->creationtime;
}
void ast_channel_creationtime_set(struct ast_channel *chan, struct timeval *value)
{
	chan->creationtime = *value;
}

struct timeval ast_channel_answertime(struct ast_channel *chan)
{
	return chan->answertime;
}

void ast_channel_answertime_set(struct ast_channel *chan, struct timeval *value)
{
	chan->answertime = *value;
}

/* Evil softhangup accessors */
int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
{
	return chan->softhangup;
}
void ast_channel_softhangup_internal_flag_set(struct ast_channel *chan, int value)
{
	chan->softhangup = value;
}
void ast_channel_softhangup_internal_flag_add(struct ast_channel *chan, int value)
{
	chan->softhangup |= value;
}
void ast_channel_softhangup_internal_flag_clear(struct ast_channel *chan, int value)
{
	chan ->softhangup &= ~value;
}

int ast_channel_unbridged_nolock(struct ast_channel *chan)
{
	return chan->unbridged;
}

int ast_channel_unbridged(struct ast_channel *chan)
{
	int res;
	ast_channel_lock(chan);
	res = ast_channel_unbridged_nolock(chan);
	ast_channel_unlock(chan);
	return res;
}

void ast_channel_set_unbridged_nolock(struct ast_channel *chan, int value)
{
	chan->unbridged = !!value;
	ast_queue_frame(chan, &ast_null_frame);
}

void ast_channel_set_unbridged(struct ast_channel *chan, int value)
{
	ast_channel_lock(chan);
	ast_channel_set_unbridged_nolock(chan, value);
	ast_channel_unlock(chan);
}

int ast_channel_is_t38_active_nolock(struct ast_channel *chan)
{
	return chan->is_t38_active;
}

int ast_channel_is_t38_active(struct ast_channel *chan)
{
	int res;

	ast_channel_lock(chan);
	res = ast_channel_is_t38_active_nolock(chan);
	ast_channel_unlock(chan);
	return res;
}

void ast_channel_set_is_t38_active_nolock(struct ast_channel *chan, int is_t38_active)
{
	chan->is_t38_active = !!is_t38_active;
}

void ast_channel_set_is_t38_active(struct ast_channel *chan, int is_t38_active)
{
	ast_channel_lock(chan);
	ast_channel_set_is_t38_active_nolock(chan, is_t38_active);
	ast_channel_unlock(chan);
}

void ast_channel_callid_cleanup(struct ast_channel *chan)
{
	chan->callid = 0;
}

/* Typedef accessors */
ast_group_t ast_channel_callgroup(const struct ast_channel *chan)
{
	return chan->callgroup;
}
void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
{
	chan->callgroup = value;
}
ast_group_t ast_channel_pickupgroup(const struct ast_channel *chan)
{
	return chan->pickupgroup;
}
void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
{
	chan->pickupgroup = value;
}
struct ast_namedgroups *ast_channel_named_callgroups(const struct ast_channel *chan)
{
	return chan->named_callgroups;
}
void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
{
	ast_unref_namedgroups(chan->named_callgroups);
	chan->named_callgroups = ast_ref_namedgroups(value);
}
struct ast_namedgroups *ast_channel_named_pickupgroups(const struct ast_channel *chan)
{
	return chan->named_pickupgroups;
}
void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
{
	ast_unref_namedgroups(chan->named_pickupgroups);
	chan->named_pickupgroups = ast_ref_namedgroups(value);
}

/* Alertpipe functions */
int ast_channel_alert_write(struct ast_channel *chan)
{
	return ast_alertpipe_write(chan->alertpipe);
}

ast_alert_status_t ast_channel_internal_alert_flush(struct ast_channel *chan)
{
	return ast_alertpipe_flush(chan->alertpipe);
}

ast_alert_status_t ast_channel_internal_alert_read(struct ast_channel *chan)
{
	return ast_alertpipe_read(chan->alertpipe);
}

int ast_channel_alert_writable(struct ast_channel *chan)
{
	return ast_alertpipe_writable(chan->alertpipe);
}

int ast_channel_internal_alert_readable(struct ast_channel *chan)
{
	return ast_alertpipe_readable(chan->alertpipe);
}

void ast_channel_internal_alertpipe_clear(struct ast_channel *chan)
{
	ast_alertpipe_clear(chan->alertpipe);
}

void ast_channel_internal_alertpipe_close(struct ast_channel *chan)
{
	ast_alertpipe_close(chan->alertpipe);
}

int ast_channel_internal_alertpipe_init(struct ast_channel *chan)
{
	return ast_alertpipe_init(chan->alertpipe);
}

int ast_channel_internal_alert_readfd(struct ast_channel *chan)
{
	return ast_alertpipe_readfd(chan->alertpipe);
}

void ast_channel_internal_alertpipe_swap(struct ast_channel *chan1, struct ast_channel *chan2)
{
	ast_alertpipe_swap(chan1->alertpipe, chan2->alertpipe);
}

/* file descriptor array accessors */
void ast_channel_internal_fd_set(struct ast_channel *chan, int which, int value)
{
	int pos;

	/* This ensures that if the vector has to grow with unused positions they will be
	 * initialized to -1.
	 */
	for (pos = AST_VECTOR_SIZE(&chan->fds); pos < which; pos++) {
		AST_VECTOR_REPLACE(&chan->fds, pos, -1);
	}

	AST_VECTOR_REPLACE(&chan->fds, which, value);
}
void ast_channel_internal_fd_clear(struct ast_channel *chan, int which)
{
	if (which >= AST_VECTOR_SIZE(&chan->fds)) {
		return;
	}

	AST_VECTOR_REPLACE(&chan->fds, which, -1);
}
void ast_channel_internal_fd_clear_all(struct ast_channel *chan)
{
	AST_VECTOR_RESET(&chan->fds, AST_VECTOR_ELEM_CLEANUP_NOOP);
}
int ast_channel_fd(const struct ast_channel *chan, int which)
{
	return (which >= AST_VECTOR_SIZE(&chan->fds)) ? -1 : AST_VECTOR_GET(&chan->fds, which);
}
int ast_channel_fd_isset(const struct ast_channel *chan, int which)
{
	return ast_channel_fd(chan, which) > -1;
}

int ast_channel_fd_count(const struct ast_channel *chan)
{
	return AST_VECTOR_SIZE(&chan->fds);
}

int ast_channel_fd_add(struct ast_channel *chan, int value)
{
	int pos = AST_EXTENDED_FDS;

	while (ast_channel_fd_isset(chan, pos)) {
		pos += 1;
	}

	AST_VECTOR_REPLACE(&chan->fds, pos, value);

	return pos;
}

pthread_t ast_channel_blocker(const struct ast_channel *chan)
{
	return chan->blocker;
}
void ast_channel_blocker_set(struct ast_channel *chan, pthread_t value)
{
	chan->blocker = value;
}

int ast_channel_blocker_tid(const struct ast_channel *chan)
{
	return chan->blocker_tid;
}
void ast_channel_blocker_tid_set(struct ast_channel *chan, int value)
{
	chan->blocker_tid = value;
}

ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)
{
	return chan->timingfunc;
}
void ast_channel_timingfunc_set(struct ast_channel *chan, ast_timing_func_t value)
{
	chan->timingfunc = value;
}

struct ast_bridge *ast_channel_internal_bridge(const struct ast_channel *chan)
{
	return chan->bridge;
}
void ast_channel_internal_bridge_set(struct ast_channel *chan, struct ast_bridge *value)
{
	chan->bridge = value;
	ast_channel_publish_snapshot(chan);
}

struct ast_bridge_channel *ast_channel_internal_bridge_channel(const struct ast_channel *chan)
{
	return chan->bridge_channel;
}
void ast_channel_internal_bridge_channel_set(struct ast_channel *chan, struct ast_bridge_channel *value)
{
	chan->bridge_channel = value;
}

struct ast_flags *ast_channel_flags(struct ast_channel *chan)
{
	return &chan->flags;
}

static int collect_names_cb(void *obj, void *arg, int flags) {
	struct ast_control_pvt_cause_code *cause_code = obj;
	struct ast_str **str = arg;

	ast_str_append(str, 0, "%s%s", (ast_str_strlen(*str) ? "," : ""), cause_code->chan_name);

	return 0;
}

struct ast_str *ast_channel_dialed_causes_channels(const struct ast_channel *chan)
{
	struct ast_str *chanlist = ast_str_create(128);

	if (!chanlist) {
		return NULL;
	}

	ao2_callback(chan->dialed_causes, 0, collect_names_cb, &chanlist);

	return chanlist;
}

struct ast_control_pvt_cause_code *ast_channel_dialed_causes_find(const struct ast_channel *chan, const char *chan_name)
{
	return ao2_find(chan->dialed_causes, chan_name, OBJ_KEY);
}

int ast_channel_dialed_causes_add(const struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
{
	struct ast_control_pvt_cause_code *ao2_cause_code;
	ao2_find(chan->dialed_causes, cause_code->chan_name, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA);
	ao2_cause_code = ao2_alloc(datalen, NULL);

	if (ao2_cause_code) {
		memcpy(ao2_cause_code, cause_code, datalen);
		ao2_link(chan->dialed_causes, ao2_cause_code);
		ao2_ref(ao2_cause_code, -1);
		return 0;
	} else {
		return -1;
	}
}

void ast_channel_dialed_causes_clear(const struct ast_channel *chan)
{
	ao2_callback(chan->dialed_causes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
}

/* \brief Hash function for pvt cause code frames */
static int pvt_cause_hash_fn(const void *vpc, const int flags)
{
	const struct ast_control_pvt_cause_code *pc = vpc;
	return ast_str_hash(ast_tech_to_upper(ast_strdupa(pc->chan_name)));
}

/* \brief Comparison function for pvt cause code frames */
static int pvt_cause_cmp_fn(void *obj, void *vstr, int flags)
{
	struct ast_control_pvt_cause_code *pc = obj;
	char *str = ast_tech_to_upper(ast_strdupa(vstr));
	char *pc_str = ast_tech_to_upper(ast_strdupa(pc->chan_name));
	return !strcmp(pc_str, str) ? CMP_MATCH | CMP_STOP : 0;
}

#define DIALED_CAUSES_BUCKETS 37

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)
{
	struct ast_channel *tmp;

	tmp = __ao2_alloc(sizeof(*tmp), destructor,
		AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function);

	if (!tmp) {
		return NULL;
	}

	if ((ast_string_field_init(tmp, 128))) {
		return ast_channel_unref(tmp);
	}

	tmp->dialed_causes = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
		DIALED_CAUSES_BUCKETS, pvt_cause_hash_fn, NULL, pvt_cause_cmp_fn);
	if (!tmp->dialed_causes) {
		return ast_channel_unref(tmp);
	}

	/* set the creation time in the uniqueid */
	tmp->uniqueid.creation_time = time(NULL);
	tmp->uniqueid.creation_unique = ast_atomic_fetchadd_int(&uniqueint, 1);

	/* use provided id or default to historical {system-}time.# format */
	if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
		ast_copy_string(tmp->uniqueid.unique_id, assignedids->uniqueid, sizeof(tmp->uniqueid.unique_id));
	} else if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
		snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%li.%d",
			(long)(tmp->uniqueid.creation_time),
			tmp->uniqueid.creation_unique);
	} else {
		snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%s-%li.%d",
			ast_config_AST_SYSTEM_NAME,
			(long)(tmp->uniqueid.creation_time),
			tmp->uniqueid.creation_unique);
	}

	/* copy linked id from parent channel if known */
	if (requestor) {
		tmp->linkedid = requestor->linkedid;
	} else {
		tmp->linkedid = tmp->uniqueid;
	}

	AST_VECTOR_INIT(&tmp->fds, AST_MAX_FDS);

	return tmp;
}

struct ast_channel *ast_channel_internal_oldest_linkedid(struct ast_channel *a, struct ast_channel *b)
{
	ast_assert(a->linkedid.creation_time != 0);
	ast_assert(b->linkedid.creation_time != 0);

	if (a->linkedid.creation_time < b->linkedid.creation_time) {
		return a;
	}
	if (b->linkedid.creation_time < a->linkedid.creation_time) {
		return b;
	}
	if (a->linkedid.creation_unique < b->linkedid.creation_unique) {
		return a;
	}
	return b;
}

void ast_channel_internal_copy_linkedid(struct ast_channel *dest, struct ast_channel *source)
{
	if (dest->linkedid.creation_time == source->linkedid.creation_time
		&& dest->linkedid.creation_unique == source->linkedid.creation_unique
		&& !strcmp(dest->linkedid.unique_id, source->linkedid.unique_id)) {
		return;
	}
	dest->linkedid = source->linkedid;
	ast_channel_publish_snapshot(dest);
}

void ast_channel_internal_swap_uniqueid_and_linkedid(struct ast_channel *a, struct ast_channel *b)
{
	struct ast_channel_id temp;

	temp = a->uniqueid;
	a->uniqueid = b->uniqueid;
	b->uniqueid = temp;

	temp = a->linkedid;
	a->linkedid = b->linkedid;
	b->linkedid = temp;
}

void ast_channel_internal_swap_topics(struct ast_channel *a, struct ast_channel *b)
{
	struct stasis_cp_single *temp;

	temp = a->topics;
	a->topics = b->topics;
	b->topics = temp;
}

void ast_channel_internal_swap_endpoint_forward_and_endpoint_cache_forward(struct ast_channel *a, struct ast_channel *b)
{
	struct stasis_forward *temp;
	temp = a->endpoint_forward;
	a->endpoint_forward = b->endpoint_forward;
	b->endpoint_forward = temp;

	temp = a->endpoint_cache_forward;
	a->endpoint_cache_forward = b->endpoint_cache_forward;
	b->endpoint_cache_forward = temp;
}

void ast_channel_internal_set_fake_ids(struct ast_channel *chan, const char *uniqueid, const char *linkedid)
{
	ast_copy_string(chan->uniqueid.unique_id, uniqueid, sizeof(chan->uniqueid.unique_id));
	ast_copy_string(chan->linkedid.unique_id, linkedid, sizeof(chan->linkedid.unique_id));
}

void ast_channel_internal_cleanup(struct ast_channel *chan)
{
	if (chan->dialed_causes) {
		ao2_t_ref(chan->dialed_causes, -1,
			"done with dialed causes since the channel is going away");
		chan->dialed_causes = NULL;
	}

	ast_string_field_free_memory(chan);

	chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
	chan->endpoint_cache_forward = stasis_forward_cancel(chan->endpoint_cache_forward);

	stasis_cp_single_unsubscribe(chan->topics);
	chan->topics = NULL;

	ast_channel_internal_set_stream_topology(chan, NULL);

	AST_VECTOR_FREE(&chan->fds);
}

void ast_channel_internal_finalize(struct ast_channel *chan)
{
	chan->finalized = 1;
}

int ast_channel_internal_is_finalized(struct ast_channel *chan)
{
	return chan->finalized;
}

struct stasis_topic *ast_channel_topic(struct ast_channel *chan)
{
	if (!chan) {
		return ast_channel_topic_all();
	}

	return stasis_cp_single_topic(chan->topics);
}

struct stasis_topic *ast_channel_topic_cached(struct ast_channel *chan)
{
	if (!chan) {
		return ast_channel_topic_all_cached();
	}

	return stasis_cp_single_topic_cached(chan->topics);
}

int ast_channel_forward_endpoint(struct ast_channel *chan,
	struct ast_endpoint *endpoint)
{
	ast_assert(chan != NULL);
	ast_assert(endpoint != NULL);

	chan->endpoint_forward =
		stasis_forward_all(ast_channel_topic(chan),
			ast_endpoint_topic(endpoint));
	if (!chan->endpoint_forward) {
		return -1;
	}

	chan->endpoint_cache_forward = stasis_forward_all(ast_channel_topic_cached(chan),
		ast_endpoint_topic(endpoint));
	if (!chan->endpoint_cache_forward) {
		chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
		return -1;
	}

	return 0;
}

int ast_channel_internal_setup_topics(struct ast_channel *chan)
{
	const char *topic_name = chan->uniqueid.unique_id;
	ast_assert(chan->topics == NULL);

	if (ast_strlen_zero(topic_name)) {
		topic_name = "<dummy-channel>";
	}

	chan->topics = stasis_cp_single_create(
		ast_channel_cache_all(), topic_name);
	if (!chan->topics) {
		return -1;
	}

	return 0;
}

AST_THREADSTORAGE(channel_errno);

void ast_channel_internal_errno_set(enum ast_channel_error error)
{
	enum ast_channel_error *error_code = ast_threadstorage_get(&channel_errno, sizeof(*error_code));
	if (!error_code) {
		return;
	}

	*error_code = error;
}

enum ast_channel_error ast_channel_internal_errno(void)
{
	enum ast_channel_error *error_code = ast_threadstorage_get(&channel_errno, sizeof(*error_code));
	if (!error_code) {
		return AST_CHANNEL_ERROR_UNKNOWN;
	}

	return *error_code;
}

struct ast_stream_topology *ast_channel_get_stream_topology(
	const struct ast_channel *chan)
{
	ast_assert(chan != NULL);

	return chan->stream_topology;
}

struct ast_stream_topology *ast_channel_set_stream_topology(struct ast_channel *chan,
	struct ast_stream_topology *topology)
{
	struct ast_stream_topology *new_topology;

	ast_assert(chan != NULL);

	/* A non-MULTISTREAM channel can't manipulate topology directly */
	ast_assert(ast_channel_is_multistream(chan));

	/* Unless the channel is being destroyed, we always want a topology on
	 * it even if its empty.
	 */
	if (!topology) {
		new_topology = ast_stream_topology_alloc();
	} else {
		new_topology = topology;
	}

	if (new_topology) {
		ast_channel_internal_set_stream_topology(chan, new_topology);
	}

	return new_topology;
}

struct ast_stream *ast_channel_get_default_stream(struct ast_channel *chan,
	enum ast_media_type type)
{
	ast_assert(chan != NULL);
	ast_assert(type < AST_MEDIA_TYPE_END);

	return chan->default_streams[type];
}

void ast_channel_internal_swap_stream_topology(struct ast_channel *chan1,
	struct ast_channel *chan2)
{
	struct ast_stream_topology *tmp_topology;

	ast_assert(chan1 != NULL && chan2 != NULL);

	tmp_topology = chan1->stream_topology;
	chan1->stream_topology = chan2->stream_topology;
	chan2->stream_topology = tmp_topology;

	channel_set_default_streams(chan1);
	channel_set_default_streams(chan2);
}

int ast_channel_is_multistream(struct ast_channel *chan)
{
	return (chan->tech && chan->tech->read_stream && chan->tech->write_stream);
}