Review Board 1.7.16


DTMF emulation bad calculation that hurts RTP

Review Request #3546 - Created May 16, 2014 and updated

Olle E Johansson
1.8
ASTERISK-23747
Reviewers
asterisk-dev
Asterisk
This code in channel.c is wrong. It first checks if we have a length. If not, we set it to a measured time, which is fine.

If we have a length and it's under the minimum DTMF duration, we set it again to the measured time. In an RTP session, the duration can be under minimum, but has no relationship to the measured time between DTMF start and end. We should keep the given RTP DTMF time and use that for emulation. I have had DTMF that was extended by up to 60 ms because of this code and that really, really broke communication for these alarm panels that send many short DTMF tones.

I suggest that this fix goes into 1.8 and later revisions.
Hours and hours of reading DTMF logs. Countless cups of tea. A gazillion milliseconds wasted. All tested in 1.8.

Diff revision 1 (Latest)

  1. /trunk/main/channel.c: Loading...
/trunk/main/channel.c
Revision 414046 New Change
1
/*
1
/*
2
 * Asterisk -- An open source telephony toolkit.
2
 * Asterisk -- An open source telephony toolkit.
3
 *
3
 *
4
 * Copyright (C) 1999 - 2006, Digium, Inc.
4
 * Copyright (C) 1999 - 2006, Digium, Inc.
5
 *
5
 *
6
 * Mark Spencer <markster@digium.com>
6
 * Mark Spencer <markster@digium.com>
7
 *
7
 *
8
 * See http://www.asterisk.org for more information about
8
 * See http://www.asterisk.org for more information about
9
 * the Asterisk project. Please do not directly contact
9
 * the Asterisk project. Please do not directly contact
10
 * any of the maintainers of this project for assistance;
10
 * any of the maintainers of this project for assistance;
11
 * the project provides a web site, mailing lists and IRC
11
 * the project provides a web site, mailing lists and IRC
12
 * channels for your use.
12
 * channels for your use.
13
 *
13
 *
14
 * This program is free software, distributed under the terms of
14
 * This program is free software, distributed under the terms of
15
 * the GNU General Public License Version 2. See the LICENSE file
15
 * the GNU General Public License Version 2. See the LICENSE file
16
 * at the top of the source tree.
16
 * at the top of the source tree.
17
 */
17
 */
18

    
   
18

   
19
/*! \file
19
/*! \file
20
 *
20
 *
21
 * \brief Channel Management
21
 * \brief Channel Management
22
 *
22
 *
23
 * \author Mark Spencer <markster@digium.com>
23
 * \author Mark Spencer <markster@digium.com>
24
 */
24
 */
25

    
   
25

   
26
/*** MODULEINFO
26
/*** MODULEINFO
27
	<support_level>core</support_level>
27
	<support_level>core</support_level>
28
 ***/
28
 ***/
29

    
   
29

   
30
#include "asterisk.h"
30
#include "asterisk.h"
31

    
   
31

   
32
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33

    
   
33

   
34
#include "asterisk/_private.h"
34
#include "asterisk/_private.h"
35

    
   
35

   
36
#include <sys/time.h>
36
#include <sys/time.h>
37
#include <signal.h>
37
#include <signal.h>
38
#include <math.h>
38
#include <math.h>
39

    
   
39

   
40
#include "asterisk/paths.h"	/* use ast_config_AST_SYSTEM_NAME */
40
#include "asterisk/paths.h"	/* use ast_config_AST_SYSTEM_NAME */
41

    
   
41

   
42
#include "asterisk/pbx.h"
42
#include "asterisk/pbx.h"
43
#include "asterisk/frame.h"
43
#include "asterisk/frame.h"
44
#include "asterisk/mod_format.h"
44
#include "asterisk/mod_format.h"
45
#include "asterisk/sched.h"
45
#include "asterisk/sched.h"
46
#include "asterisk/channel.h"
46
#include "asterisk/channel.h"
47
#include "asterisk/musiconhold.h"
47
#include "asterisk/musiconhold.h"
48
#include "asterisk/say.h"
48
#include "asterisk/say.h"
49
#include "asterisk/file.h"
49
#include "asterisk/file.h"
50
#include "asterisk/cli.h"
50
#include "asterisk/cli.h"
51
#include "asterisk/translate.h"
51
#include "asterisk/translate.h"
52
#include "asterisk/manager.h"
52
#include "asterisk/manager.h"
53
#include "asterisk/chanvars.h"
53
#include "asterisk/chanvars.h"
54
#include "asterisk/linkedlists.h"
54
#include "asterisk/linkedlists.h"
55
#include "asterisk/indications.h"
55
#include "asterisk/indications.h"
56
#include "asterisk/monitor.h"
56
#include "asterisk/monitor.h"
57
#include "asterisk/causes.h"
57
#include "asterisk/causes.h"
58
#include "asterisk/callerid.h"
58
#include "asterisk/callerid.h"
59
#include "asterisk/utils.h"
59
#include "asterisk/utils.h"
60
#include "asterisk/lock.h"
60
#include "asterisk/lock.h"
61
#include "asterisk/app.h"
61
#include "asterisk/app.h"
62
#include "asterisk/transcap.h"
62
#include "asterisk/transcap.h"
63
#include "asterisk/devicestate.h"
63
#include "asterisk/devicestate.h"
64
#include "asterisk/threadstorage.h"
64
#include "asterisk/threadstorage.h"
65
#include "asterisk/slinfactory.h"
65
#include "asterisk/slinfactory.h"
66
#include "asterisk/audiohook.h"
66
#include "asterisk/audiohook.h"
67
#include "asterisk/framehook.h"
67
#include "asterisk/framehook.h"
68
#include "asterisk/timing.h"
68
#include "asterisk/timing.h"
69
#include "asterisk/autochan.h"
69
#include "asterisk/autochan.h"
70
#include "asterisk/stringfields.h"
70
#include "asterisk/stringfields.h"
71
#include "asterisk/global_datastores.h"
71
#include "asterisk/global_datastores.h"
72
#include "asterisk/data.h"
72
#include "asterisk/data.h"
73
#include "asterisk/channel_internal.h"
73
#include "asterisk/channel_internal.h"
74
#include "asterisk/features.h"
74
#include "asterisk/features.h"
75
#include "asterisk/bridge.h"
75
#include "asterisk/bridge.h"
76
#include "asterisk/test.h"
76
#include "asterisk/test.h"
77
#include "asterisk/stasis_channels.h"
77
#include "asterisk/stasis_channels.h"
78

    
   
78

   
79
/*** DOCUMENTATION
79
/*** DOCUMENTATION
80
 ***/
80
 ***/
81

    
   
81

   
82
#ifdef HAVE_EPOLL
82
#ifdef HAVE_EPOLL
83
#include <sys/epoll.h>
83
#include <sys/epoll.h>
84
#endif
84
#endif
85

    
   
85

   
86
#if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
86
#if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
87
#if defined(HAVE_PRI)
87
#if defined(HAVE_PRI)
88
#include "libpri.h"
88
#include "libpri.h"
89
#endif	/* defined(HAVE_PRI) */
89
#endif	/* defined(HAVE_PRI) */
90
#endif	/* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
90
#endif	/* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
91

    
   
91

   
92
struct ast_epoll_data {
92
struct ast_epoll_data {
93
	struct ast_channel *chan;
93
	struct ast_channel *chan;
94
	int which;
94
	int which;
95
};
95
};
96

    
   
96

   
97
/* uncomment if you have problems with 'monitoring' synchronized files */
97
/* uncomment if you have problems with 'monitoring' synchronized files */
98
#if 0
98
#if 0
99
#define MONITOR_CONSTANT_DELAY
99
#define MONITOR_CONSTANT_DELAY
100
#define MONITOR_DELAY	150 * 8		/*!< 150 ms of MONITORING DELAY */
100
#define MONITOR_DELAY	150 * 8		/*!< 150 ms of MONITORING DELAY */
101
#endif
101
#endif
102

    
   
102

   
103
/*! \brief Prevent new channel allocation if shutting down. */
103
/*! \brief Prevent new channel allocation if shutting down. */
104
static int shutting_down;
104
static int shutting_down;
105

    
   
105

   
106
static int chancount;
106
static int chancount;
107

    
   
107

   
108
unsigned long global_fin, global_fout;
108
unsigned long global_fin, global_fout;
109

    
   
109

   
110
AST_THREADSTORAGE(state2str_threadbuf);
110
AST_THREADSTORAGE(state2str_threadbuf);
111
#define STATE2STR_BUFSIZE   32
111
#define STATE2STR_BUFSIZE   32
112

    
   
112

   
113
/*! Default amount of time to use when emulating a digit as a begin and end
113
/*! Default amount of time to use when emulating a digit as a begin and end
114
 *  100ms */
114
 *  100ms */
115
#define AST_DEFAULT_EMULATE_DTMF_DURATION 100
115
#define AST_DEFAULT_EMULATE_DTMF_DURATION 100
116

    
   
116

   
117
#define DEFAULT_AMA_FLAGS AST_AMA_DOCUMENTATION
117
#define DEFAULT_AMA_FLAGS AST_AMA_DOCUMENTATION
118

    
   
118

   
119
/*! Minimum amount of time between the end of the last digit and the beginning
119
/*! Minimum amount of time between the end of the last digit and the beginning
120
 *  of a new one - 45ms */
120
 *  of a new one - 45ms */
121
#define AST_MIN_DTMF_GAP 45
121
#define AST_MIN_DTMF_GAP 45
122

    
   
122

   
123
/*! \brief List of channel drivers */
123
/*! \brief List of channel drivers */
124
struct chanlist {
124
struct chanlist {
125
	const struct ast_channel_tech *tech;
125
	const struct ast_channel_tech *tech;
126
	AST_LIST_ENTRY(chanlist) list;
126
	AST_LIST_ENTRY(chanlist) list;
127
};
127
};
128

    
   
128

   
129
/*! \brief the list of registered channel types */
129
/*! \brief the list of registered channel types */
130
static AST_RWLIST_HEAD_STATIC(backends, chanlist);
130
static AST_RWLIST_HEAD_STATIC(backends, chanlist);
131

    
   
131

   
132
#ifdef LOW_MEMORY
132
#ifdef LOW_MEMORY
133
#define NUM_CHANNEL_BUCKETS 61
133
#define NUM_CHANNEL_BUCKETS 61
134
#else
134
#else
135
#define NUM_CHANNEL_BUCKETS 1567
135
#define NUM_CHANNEL_BUCKETS 1567
136
#endif
136
#endif
137

    
   
137

   
138
/*! \brief All active channels on the system */
138
/*! \brief All active channels on the system */
139
static struct ao2_container *channels;
139
static struct ao2_container *channels;
140

    
   
140

   
141
/*! \brief map AST_CAUSE's to readable string representations
141
/*! \brief map AST_CAUSE's to readable string representations
142
 *
142
 *
143
 * \ref causes.h
143
 * \ref causes.h
144
*/
144
*/
145
struct causes_map {
145
struct causes_map {
146
	int cause;
146
	int cause;
147
	const char *name;
147
	const char *name;
148
	const char *desc;
148
	const char *desc;
149
};
149
};
150

    
   
150

   
151
static const struct causes_map causes[] = {
151
static const struct causes_map causes[] = {
152
	{ AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
152
	{ AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
153
	{ AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
153
	{ AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
154
	{ AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
154
	{ AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
155
	{ AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
155
	{ AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
156
	{ AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
156
	{ AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
157
	{ AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
157
	{ AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
158
	{ AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
158
	{ AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
159
	{ AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
159
	{ AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
160
	{ AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
160
	{ AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
161
	{ AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
161
	{ AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
162
	{ AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
162
	{ AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
163
	{ AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
163
	{ AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
164
	{ AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
164
	{ AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
165
	{ AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
165
	{ AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
166
	{ AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
166
	{ AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
167
	{ AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
167
	{ AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
168
	{ AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
168
	{ AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
169
	{ AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
169
	{ AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
170
	{ AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
170
	{ AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
171
	{ AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
171
	{ AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
172
	{ AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
172
	{ AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
173
	{ AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
173
	{ AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
174
	{ AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
174
	{ AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
175
	{ AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
175
	{ AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
176
	{ AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
176
	{ AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
177
	{ AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
177
	{ AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
178
	{ AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
178
	{ AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
179
	{ AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
179
	{ AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
180
	{ AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
180
	{ AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
181
	{ AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
181
	{ AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
182
	{ AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
182
	{ AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
183
	{ AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
183
	{ AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
184
	{ AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
184
	{ AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
185
	{ AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
185
	{ AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
186
	{ AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
186
	{ AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
187
	{ AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
187
	{ AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
188
	{ AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
188
	{ AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
189
	{ AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
189
	{ AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
190
	{ AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
190
	{ AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
191
	{ AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
191
	{ AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
192
	{ AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
192
	{ AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
193
	{ AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
193
	{ AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
194
	{ AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
194
	{ AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
195
	{ AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
195
	{ AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
196
	{ AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
196
	{ AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
197
	{ AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
197
	{ AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
198
	{ AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
198
	{ AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
199
	{ AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
199
	{ AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
200
	{ AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
200
	{ AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
201
};
201
};
202

    
   
202

   
203
struct ast_variable *ast_channeltype_list(void)
203
struct ast_variable *ast_channeltype_list(void)
204
{
204
{
205
	struct chanlist *cl;
205
	struct chanlist *cl;
206
	struct ast_variable *var = NULL, *prev = NULL;
206
	struct ast_variable *var = NULL, *prev = NULL;
207

    
   
207

   
208
	AST_RWLIST_RDLOCK(&backends);
208
	AST_RWLIST_RDLOCK(&backends);
209
	AST_RWLIST_TRAVERSE(&backends, cl, list) {
209
	AST_RWLIST_TRAVERSE(&backends, cl, list) {
210
		if (prev)  {
210
		if (prev)  {
211
			if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
211
			if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
212
				prev = prev->next;
212
				prev = prev->next;
213
		} else {
213
		} else {
214
			var = ast_variable_new(cl->tech->type, cl->tech->description, "");
214
			var = ast_variable_new(cl->tech->type, cl->tech->description, "");
215
			prev = var;
215
			prev = var;
216
		}
216
		}
217
	}
217
	}
218
	AST_RWLIST_UNLOCK(&backends);
218
	AST_RWLIST_UNLOCK(&backends);
219

    
   
219

   
220
	return var;
220
	return var;
221
}
221
}
222

    
   
222

   
223
#if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
223
#if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
224
static const char *party_number_ton2str(int ton)
224
static const char *party_number_ton2str(int ton)
225
{
225
{
226
#if defined(HAVE_PRI)
226
#if defined(HAVE_PRI)
227
	switch ((ton >> 4) & 0x07) {
227
	switch ((ton >> 4) & 0x07) {
228
	case PRI_TON_INTERNATIONAL:
228
	case PRI_TON_INTERNATIONAL:
229
		return "International";
229
		return "International";
230
	case PRI_TON_NATIONAL:
230
	case PRI_TON_NATIONAL:
231
		return "National";
231
		return "National";
232
	case PRI_TON_NET_SPECIFIC:
232
	case PRI_TON_NET_SPECIFIC:
233
		return "Network Specific";
233
		return "Network Specific";
234
	case PRI_TON_SUBSCRIBER:
234
	case PRI_TON_SUBSCRIBER:
235
		return "Subscriber";
235
		return "Subscriber";
236
	case PRI_TON_ABBREVIATED:
236
	case PRI_TON_ABBREVIATED:
237
		return "Abbreviated";
237
		return "Abbreviated";
238
	case PRI_TON_RESERVED:
238
	case PRI_TON_RESERVED:
239
		return "Reserved";
239
		return "Reserved";
240
	case PRI_TON_UNKNOWN:
240
	case PRI_TON_UNKNOWN:
241
	default:
241
	default:
242
		break;
242
		break;
243
	}
243
	}
244
#endif	/* defined(HAVE_PRI) */
244
#endif	/* defined(HAVE_PRI) */
245
	return "Unknown";
245
	return "Unknown";
246
}
246
}
247
#endif	/* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
247
#endif	/* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
248

    
   
248

   
249
#if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
249
#if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
250
static const char *party_number_plan2str(int plan)
250
static const char *party_number_plan2str(int plan)
251
{
251
{
252
#if defined(HAVE_PRI)
252
#if defined(HAVE_PRI)
253
	switch (plan & 0x0F) {
253
	switch (plan & 0x0F) {
254
	default:
254
	default:
255
	case PRI_NPI_UNKNOWN:
255
	case PRI_NPI_UNKNOWN:
256
		break;
256
		break;
257
	case PRI_NPI_E163_E164:
257
	case PRI_NPI_E163_E164:
258
		return "Public (E.163/E.164)";
258
		return "Public (E.163/E.164)";
259
	case PRI_NPI_X121:
259
	case PRI_NPI_X121:
260
		return "Data (X.121)";
260
		return "Data (X.121)";
261
	case PRI_NPI_F69:
261
	case PRI_NPI_F69:
262
		return "Telex (F.69)";
262
		return "Telex (F.69)";
263
	case PRI_NPI_NATIONAL:
263
	case PRI_NPI_NATIONAL:
264
		return "National Standard";
264
		return "National Standard";
265
	case PRI_NPI_PRIVATE:
265
	case PRI_NPI_PRIVATE:
266
		return "Private";
266
		return "Private";
267
	case PRI_NPI_RESERVED:
267
	case PRI_NPI_RESERVED:
268
		return "Reserved";
268
		return "Reserved";
269
	}
269
	}
270
#endif	/* defined(HAVE_PRI) */
270
#endif	/* defined(HAVE_PRI) */
271
	return "Unknown";
271
	return "Unknown";
272
}
272
}
273
#endif	/* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
273
#endif	/* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
274

    
   
274

   
275
/*! \brief Show channel types - CLI command */
275
/*! \brief Show channel types - CLI command */
276
static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
276
static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
277
{
277
{
278
#define FORMAT  "%-15.15s  %-40.40s %-12.12s %-12.12s %-12.12s\n"
278
#define FORMAT  "%-15.15s  %-40.40s %-12.12s %-12.12s %-12.12s\n"
279
	struct chanlist *cl;
279
	struct chanlist *cl;
280
	int count_chan = 0;
280
	int count_chan = 0;
281

    
   
281

   
282
	switch (cmd) {
282
	switch (cmd) {
283
	case CLI_INIT:
283
	case CLI_INIT:
284
		e->command = "core show channeltypes";
284
		e->command = "core show channeltypes";
285
		e->usage =
285
		e->usage =
286
			"Usage: core show channeltypes\n"
286
			"Usage: core show channeltypes\n"
287
			"       Lists available channel types registered in your\n"
287
			"       Lists available channel types registered in your\n"
288
			"       Asterisk server.\n";
288
			"       Asterisk server.\n";
289
		return NULL;
289
		return NULL;
290
	case CLI_GENERATE:
290
	case CLI_GENERATE:
291
		return NULL;
291
		return NULL;
292
	}
292
	}
293

    
   
293

   
294
	if (a->argc != 3)
294
	if (a->argc != 3)
295
		return CLI_SHOWUSAGE;
295
		return CLI_SHOWUSAGE;
296

    
   
296

   
297
	ast_cli(a->fd, FORMAT, "Type", "Description",       "Devicestate", "Indications", "Transfer");
297
	ast_cli(a->fd, FORMAT, "Type", "Description",       "Devicestate", "Indications", "Transfer");
298
	ast_cli(a->fd, FORMAT, "-----------", "-----------", "-----------", "-----------", "-----------");
298
	ast_cli(a->fd, FORMAT, "-----------", "-----------", "-----------", "-----------", "-----------");
299

    
   
299

   
300
	AST_RWLIST_RDLOCK(&backends);
300
	AST_RWLIST_RDLOCK(&backends);
301
	AST_RWLIST_TRAVERSE(&backends, cl, list) {
301
	AST_RWLIST_TRAVERSE(&backends, cl, list) {
302
		ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
302
		ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
303
			(cl->tech->devicestate) ? "yes" : "no",
303
			(cl->tech->devicestate) ? "yes" : "no",
304
			(cl->tech->indicate) ? "yes" : "no",
304
			(cl->tech->indicate) ? "yes" : "no",
305
			(cl->tech->transfer) ? "yes" : "no");
305
			(cl->tech->transfer) ? "yes" : "no");
306
		count_chan++;
306
		count_chan++;
307
	}
307
	}
308
	AST_RWLIST_UNLOCK(&backends);
308
	AST_RWLIST_UNLOCK(&backends);
309

    
   
309

   
310
	ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
310
	ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
311

    
   
311

   
312
	return CLI_SUCCESS;
312
	return CLI_SUCCESS;
313

    
   
313

   
314
#undef FORMAT
314
#undef FORMAT
315
}
315
}
316

    
   
316

   
317
static char *complete_channeltypes(struct ast_cli_args *a)
317
static char *complete_channeltypes(struct ast_cli_args *a)
318
{
318
{
319
	struct chanlist *cl;
319
	struct chanlist *cl;
320
	int which = 0;
320
	int which = 0;
321
	int wordlen;
321
	int wordlen;
322
	char *ret = NULL;
322
	char *ret = NULL;
323

    
   
323

   
324
	if (a->pos != 3)
324
	if (a->pos != 3)
325
		return NULL;
325
		return NULL;
326

    
   
326

   
327
	wordlen = strlen(a->word);
327
	wordlen = strlen(a->word);
328

    
   
328

   
329
	AST_RWLIST_RDLOCK(&backends);
329
	AST_RWLIST_RDLOCK(&backends);
330
	AST_RWLIST_TRAVERSE(&backends, cl, list) {
330
	AST_RWLIST_TRAVERSE(&backends, cl, list) {
331
		if (!strncasecmp(a->word, cl->tech->type, wordlen) && ++which > a->n) {
331
		if (!strncasecmp(a->word, cl->tech->type, wordlen) && ++which > a->n) {
332
			ret = ast_strdup(cl->tech->type);
332
			ret = ast_strdup(cl->tech->type);
333
			break;
333
			break;
334
		}
334
		}
335
	}
335
	}
336
	AST_RWLIST_UNLOCK(&backends);
336
	AST_RWLIST_UNLOCK(&backends);
337

    
   
337

   
338
	return ret;
338
	return ret;
339
}
339
}
340

    
   
340

   
341
/*! \brief Show details about a channel driver - CLI command */
341
/*! \brief Show details about a channel driver - CLI command */
342
static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
342
static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
343
{
343
{
344
	struct chanlist *cl = NULL;
344
	struct chanlist *cl = NULL;
345
	char buf[512];
345
	char buf[512];
346

    
   
346

   
347
	switch (cmd) {
347
	switch (cmd) {
348
	case CLI_INIT:
348
	case CLI_INIT:
349
		e->command = "core show channeltype";
349
		e->command = "core show channeltype";
350
		e->usage =
350
		e->usage =
351
			"Usage: core show channeltype <name>\n"
351
			"Usage: core show channeltype <name>\n"
352
			"	Show details about the specified channel type, <name>.\n";
352
			"	Show details about the specified channel type, <name>.\n";
353
		return NULL;
353
		return NULL;
354
	case CLI_GENERATE:
354
	case CLI_GENERATE:
355
		return complete_channeltypes(a);
355
		return complete_channeltypes(a);
356
	}
356
	}
357

    
   
357

   
358
	if (a->argc != 4)
358
	if (a->argc != 4)
359
		return CLI_SHOWUSAGE;
359
		return CLI_SHOWUSAGE;
360

    
   
360

   
361
	AST_RWLIST_RDLOCK(&backends);
361
	AST_RWLIST_RDLOCK(&backends);
362

    
   
362

   
363
	AST_RWLIST_TRAVERSE(&backends, cl, list) {
363
	AST_RWLIST_TRAVERSE(&backends, cl, list) {
364
		if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
364
		if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
365
			break;
365
			break;
366
	}
366
	}
367

    
   
367

   
368

    
   
368

   
369
	if (!cl) {
369
	if (!cl) {
370
		ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
370
		ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
371
		AST_RWLIST_UNLOCK(&backends);
371
		AST_RWLIST_UNLOCK(&backends);
372
		return CLI_FAILURE;
372
		return CLI_FAILURE;
373
	}
373
	}
374

    
   
374

   
375
	ast_cli(a->fd,
375
	ast_cli(a->fd,
376
		"-- Info about channel driver: %s --\n"
376
		"-- Info about channel driver: %s --\n"
377
		"  Device State: %s\n"
377
		"  Device State: %s\n"
378
		"    Indication: %s\n"
378
		"    Indication: %s\n"
379
		"     Transfer : %s\n"
379
		"     Transfer : %s\n"
380
		"  Capabilities: %s\n"
380
		"  Capabilities: %s\n"
381
		"   Digit Begin: %s\n"
381
		"   Digit Begin: %s\n"
382
		"     Digit End: %s\n"
382
		"     Digit End: %s\n"
383
		"    Send HTML : %s\n"
383
		"    Send HTML : %s\n"
384
		" Image Support: %s\n"
384
		" Image Support: %s\n"
385
		"  Text Support: %s\n",
385
		"  Text Support: %s\n",
386
		cl->tech->type,
386
		cl->tech->type,
387
		(cl->tech->devicestate) ? "yes" : "no",
387
		(cl->tech->devicestate) ? "yes" : "no",
388
		(cl->tech->indicate) ? "yes" : "no",
388
		(cl->tech->indicate) ? "yes" : "no",
389
		(cl->tech->transfer) ? "yes" : "no",
389
		(cl->tech->transfer) ? "yes" : "no",
390
		ast_getformatname_multiple(buf, sizeof(buf), cl->tech->capabilities),
390
		ast_getformatname_multiple(buf, sizeof(buf), cl->tech->capabilities),
391
		(cl->tech->send_digit_begin) ? "yes" : "no",
391
		(cl->tech->send_digit_begin) ? "yes" : "no",
392
		(cl->tech->send_digit_end) ? "yes" : "no",
392
		(cl->tech->send_digit_end) ? "yes" : "no",
393
		(cl->tech->send_html) ? "yes" : "no",
393
		(cl->tech->send_html) ? "yes" : "no",
394
		(cl->tech->send_image) ? "yes" : "no",
394
		(cl->tech->send_image) ? "yes" : "no",
395
		(cl->tech->send_text) ? "yes" : "no"
395
		(cl->tech->send_text) ? "yes" : "no"
396

    
   
396

   
397
	);
397
	);
398

    
   
398

   
399
	AST_RWLIST_UNLOCK(&backends);
399
	AST_RWLIST_UNLOCK(&backends);
400

    
   
400

   
401
	return CLI_SUCCESS;
401
	return CLI_SUCCESS;
402
}
402
}
403

    
   
403

   
404
static struct ast_cli_entry cli_channel[] = {
404
static struct ast_cli_entry cli_channel[] = {
405
	AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
405
	AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
406
	AST_CLI_DEFINE(handle_cli_core_show_channeltype,  "Give more details on that channel type")
406
	AST_CLI_DEFINE(handle_cli_core_show_channeltype,  "Give more details on that channel type")
407
};
407
};
408

    
   
408

   
409
static struct ast_frame *kill_read(struct ast_channel *chan)
409
static struct ast_frame *kill_read(struct ast_channel *chan)
410
{
410
{
411
	/* Hangup channel. */
411
	/* Hangup channel. */
412
	return NULL;
412
	return NULL;
413
}
413
}
414

    
   
414

   
415
static struct ast_frame *kill_exception(struct ast_channel *chan)
415
static struct ast_frame *kill_exception(struct ast_channel *chan)
416
{
416
{
417
	/* Hangup channel. */
417
	/* Hangup channel. */
418
	return NULL;
418
	return NULL;
419
}
419
}
420

    
   
420

   
421
static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
421
static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
422
{
422
{
423
	/* Hangup channel. */
423
	/* Hangup channel. */
424
	return -1;
424
	return -1;
425
}
425
}
426

    
   
426

   
427
static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
427
static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
428
{
428
{
429
	/* No problem fixing up the channel. */
429
	/* No problem fixing up the channel. */
430
	return 0;
430
	return 0;
431
}
431
}
432

    
   
432

   
433
static int kill_hangup(struct ast_channel *chan)
433
static int kill_hangup(struct ast_channel *chan)
434
{
434
{
435
	ast_channel_tech_pvt_set(chan, NULL);
435
	ast_channel_tech_pvt_set(chan, NULL);
436
	return 0;
436
	return 0;
437
}
437
}
438

    
   
438

   
439
/*!
439
/*!
440
 * \brief Kill the channel channel driver technology descriptor.
440
 * \brief Kill the channel channel driver technology descriptor.
441
 *
441
 *
442
 * \details
442
 * \details
443
 * The purpose of this channel technology is to encourage the
443
 * The purpose of this channel technology is to encourage the
444
 * channel to hangup as quickly as possible.
444
 * channel to hangup as quickly as possible.
445
 *
445
 *
446
 * \note Used by DTMF atxfer and zombie channels.
446
 * \note Used by DTMF atxfer and zombie channels.
447
 */
447
 */
448
const struct ast_channel_tech ast_kill_tech = {
448
const struct ast_channel_tech ast_kill_tech = {
449
	.type = "Kill",
449
	.type = "Kill",
450
	.description = "Kill channel (should not see this)",
450
	.description = "Kill channel (should not see this)",
451
	.read = kill_read,
451
	.read = kill_read,
452
	.exception = kill_exception,
452
	.exception = kill_exception,
453
	.write = kill_write,
453
	.write = kill_write,
454
	.fixup = kill_fixup,
454
	.fixup = kill_fixup,
455
	.hangup = kill_hangup,
455
	.hangup = kill_hangup,
456
};
456
};
457

    
   
457

   
458
/*! \brief Checks to see if a channel is needing hang up */
458
/*! \brief Checks to see if a channel is needing hang up */
459
int ast_check_hangup(struct ast_channel *chan)
459
int ast_check_hangup(struct ast_channel *chan)
460
{
460
{
461
	if (ast_channel_softhangup_internal_flag(chan))		/* yes if soft hangup flag set */
461
	if (ast_channel_softhangup_internal_flag(chan))		/* yes if soft hangup flag set */
462
		return 1;
462
		return 1;
463
	if (ast_tvzero(*ast_channel_whentohangup(chan)))	/* no if no hangup scheduled */
463
	if (ast_tvzero(*ast_channel_whentohangup(chan)))	/* no if no hangup scheduled */
464
		return 0;
464
		return 0;
465
	if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0)		/* no if hangup time has not come yet. */
465
	if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0)		/* no if hangup time has not come yet. */
466
		return 0;
466
		return 0;
467
	ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
467
	ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
468
	ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(chan));
468
	ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(chan));
469
	ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);	/* record event */
469
	ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);	/* record event */
470
	return 1;
470
	return 1;
471
}
471
}
472

    
   
472

   
473
int ast_check_hangup_locked(struct ast_channel *chan)
473
int ast_check_hangup_locked(struct ast_channel *chan)
474
{
474
{
475
	int res;
475
	int res;
476
	ast_channel_lock(chan);
476
	ast_channel_lock(chan);
477
	res = ast_check_hangup(chan);
477
	res = ast_check_hangup(chan);
478
	ast_channel_unlock(chan);
478
	ast_channel_unlock(chan);
479
	return res;
479
	return res;
480
}
480
}
481

    
   
481

   
482
void ast_channel_softhangup_withcause_locked(struct ast_channel *chan, int causecode)
482
void ast_channel_softhangup_withcause_locked(struct ast_channel *chan, int causecode)
483
{
483
{
484
	ast_channel_lock(chan);
484
	ast_channel_lock(chan);
485

    
   
485

   
486
	if (causecode > 0) {
486
	if (causecode > 0) {
487
		ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
487
		ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
488
			ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
488
			ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
489

    
   
489

   
490
		ast_channel_hangupcause_set(chan, causecode);
490
		ast_channel_hangupcause_set(chan, causecode);
491
	}
491
	}
492

    
   
492

   
493
	ast_softhangup_nolock(chan, AST_SOFTHANGUP_EXPLICIT);
493
	ast_softhangup_nolock(chan, AST_SOFTHANGUP_EXPLICIT);
494

    
   
494

   
495
	ast_channel_unlock(chan);
495
	ast_channel_unlock(chan);
496
}
496
}
497

    
   
497

   
498
static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
498
static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
499
{
499
{
500
	struct ast_channel *chan = obj;
500
	struct ast_channel *chan = obj;
501

    
   
501

   
502
	ast_softhangup(chan, AST_SOFTHANGUP_SHUTDOWN);
502
	ast_softhangup(chan, AST_SOFTHANGUP_SHUTDOWN);
503

    
   
503

   
504
	return 0;
504
	return 0;
505
}
505
}
506

    
   
506

   
507
void ast_begin_shutdown(int hangup)
507
void ast_begin_shutdown(int hangup)
508
{
508
{
509
	shutting_down = 1;
509
	shutting_down = 1;
510

    
   
510

   
511
	if (hangup) {
511
	if (hangup) {
512
		ao2_callback(channels, OBJ_NODATA | OBJ_MULTIPLE, ast_channel_softhangup_cb, NULL);
512
		ao2_callback(channels, OBJ_NODATA | OBJ_MULTIPLE, ast_channel_softhangup_cb, NULL);
513
	}
513
	}
514
}
514
}
515

    
   
515

   
516
/*! \brief returns number of active/allocated channels */
516
/*! \brief returns number of active/allocated channels */
517
int ast_active_channels(void)
517
int ast_active_channels(void)
518
{
518
{
519
	return channels ? ao2_container_count(channels) : 0;
519
	return channels ? ao2_container_count(channels) : 0;
520
}
520
}
521

    
   
521

   
522
int ast_undestroyed_channels(void)
522
int ast_undestroyed_channels(void)
523
{
523
{
524
	return ast_atomic_fetchadd_int(&chancount, 0);
524
	return ast_atomic_fetchadd_int(&chancount, 0);
525
}
525
}
526

    
   
526

   
527
/*! \brief Cancel a shutdown in progress */
527
/*! \brief Cancel a shutdown in progress */
528
void ast_cancel_shutdown(void)
528
void ast_cancel_shutdown(void)
529
{
529
{
530
	shutting_down = 0;
530
	shutting_down = 0;
531
}
531
}
532

    
   
532

   
533
/*! \brief Returns non-zero if Asterisk is being shut down */
533
/*! \brief Returns non-zero if Asterisk is being shut down */
534
int ast_shutting_down(void)
534
int ast_shutting_down(void)
535
{
535
{
536
	return shutting_down;
536
	return shutting_down;
537
}
537
}
538

    
   
538

   
539
/*! \brief Set when to hangup channel */
539
/*! \brief Set when to hangup channel */
540
void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
540
void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
541
{
541
{
542
	if (ast_tvzero(offset)) {
542
	if (ast_tvzero(offset)) {
543
		ast_channel_whentohangup_set(chan, &offset);
543
		ast_channel_whentohangup_set(chan, &offset);
544
	} else {
544
	} else {
545
		struct timeval tv = ast_tvadd(offset, ast_tvnow());
545
		struct timeval tv = ast_tvadd(offset, ast_tvnow());
546
		ast_channel_whentohangup_set(chan, &tv);
546
		ast_channel_whentohangup_set(chan, &tv);
547
	}
547
	}
548
	ast_queue_frame(chan, &ast_null_frame);
548
	ast_queue_frame(chan, &ast_null_frame);
549
	return;
549
	return;
550
}
550
}
551

    
   
551

   
552
void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
552
void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
553
{
553
{
554
	struct timeval when = { offset, };
554
	struct timeval when = { offset, };
555
	ast_channel_setwhentohangup_tv(chan, when);
555
	ast_channel_setwhentohangup_tv(chan, when);
556
}
556
}
557

    
   
557

   
558
/*! \brief Compare a offset with when to hangup channel */
558
/*! \brief Compare a offset with when to hangup channel */
559
int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
559
int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
560
{
560
{
561
	struct timeval whentohangup;
561
	struct timeval whentohangup;
562

    
   
562

   
563
	if (ast_tvzero(*ast_channel_whentohangup(chan)))
563
	if (ast_tvzero(*ast_channel_whentohangup(chan)))
564
		return ast_tvzero(offset) ? 0 : -1;
564
		return ast_tvzero(offset) ? 0 : -1;
565

    
   
565

   
566
	if (ast_tvzero(offset))
566
	if (ast_tvzero(offset))
567
		return 1;
567
		return 1;
568

    
   
568

   
569
	whentohangup = ast_tvadd(offset, ast_tvnow());
569
	whentohangup = ast_tvadd(offset, ast_tvnow());
570

    
   
570

   
571
	return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
571
	return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
572
}
572
}
573

    
   
573

   
574
int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
574
int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
575
{
575
{
576
	struct timeval when = { offset, };
576
	struct timeval when = { offset, };
577
	return ast_channel_cmpwhentohangup_tv(chan, when);
577
	return ast_channel_cmpwhentohangup_tv(chan, when);
578
}
578
}
579

    
   
579

   
580
/*! \brief Register a new telephony channel in Asterisk */
580
/*! \brief Register a new telephony channel in Asterisk */
581
int ast_channel_register(const struct ast_channel_tech *tech)
581
int ast_channel_register(const struct ast_channel_tech *tech)
582
{
582
{
583
	struct chanlist *chan;
583
	struct chanlist *chan;
584

    
   
584

   
585
	AST_RWLIST_WRLOCK(&backends);
585
	AST_RWLIST_WRLOCK(&backends);
586

    
   
586

   
587
	AST_RWLIST_TRAVERSE(&backends, chan, list) {
587
	AST_RWLIST_TRAVERSE(&backends, chan, list) {
588
		if (!strcasecmp(tech->type, chan->tech->type)) {
588
		if (!strcasecmp(tech->type, chan->tech->type)) {
589
			ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
589
			ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
590
			AST_RWLIST_UNLOCK(&backends);
590
			AST_RWLIST_UNLOCK(&backends);
591
			return -1;
591
			return -1;
592
		}
592
		}
593
	}
593
	}
594

    
   
594

   
595
	if (!(chan = ast_calloc(1, sizeof(*chan)))) {
595
	if (!(chan = ast_calloc(1, sizeof(*chan)))) {
596
		AST_RWLIST_UNLOCK(&backends);
596
		AST_RWLIST_UNLOCK(&backends);
597
		return -1;
597
		return -1;
598
	}
598
	}
599
	chan->tech = tech;
599
	chan->tech = tech;
600
	AST_RWLIST_INSERT_HEAD(&backends, chan, list);
600
	AST_RWLIST_INSERT_HEAD(&backends, chan, list);
601

    
   
601

   
602
	ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
602
	ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
603

    
   
603

   
604
	ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
604
	ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
605

    
   
605

   
606
	AST_RWLIST_UNLOCK(&backends);
606
	AST_RWLIST_UNLOCK(&backends);
607

    
   
607

   
608
	return 0;
608
	return 0;
609
}
609
}
610

    
   
610

   
611
/*! \brief Unregister channel driver */
611
/*! \brief Unregister channel driver */
612
void ast_channel_unregister(const struct ast_channel_tech *tech)
612
void ast_channel_unregister(const struct ast_channel_tech *tech)
613
{
613
{
614
	struct chanlist *chan;
614
	struct chanlist *chan;
615

    
   
615

   
616
	ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
616
	ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
617

    
   
617

   
618
	AST_RWLIST_WRLOCK(&backends);
618
	AST_RWLIST_WRLOCK(&backends);
619

    
   
619

   
620
	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) {
620
	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) {
621
		if (chan->tech == tech) {
621
		if (chan->tech == tech) {
622
			AST_LIST_REMOVE_CURRENT(list);
622
			AST_LIST_REMOVE_CURRENT(list);
623
			ast_free(chan);
623
			ast_free(chan);
624
			ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
624
			ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
625
			break;
625
			break;
626
		}
626
		}
627
	}
627
	}
628
	AST_LIST_TRAVERSE_SAFE_END;
628
	AST_LIST_TRAVERSE_SAFE_END;
629

    
   
629

   
630
	AST_RWLIST_UNLOCK(&backends);
630
	AST_RWLIST_UNLOCK(&backends);
631
}
631
}
632

    
   
632

   
633
/*! \brief Get handle to channel driver based on name */
633
/*! \brief Get handle to channel driver based on name */
634
const struct ast_channel_tech *ast_get_channel_tech(const char *name)
634
const struct ast_channel_tech *ast_get_channel_tech(const char *name)
635
{
635
{
636
	struct chanlist *chanls;
636
	struct chanlist *chanls;
637
	const struct ast_channel_tech *ret = NULL;
637
	const struct ast_channel_tech *ret = NULL;
638

    
   
638

   
639
	AST_RWLIST_RDLOCK(&backends);
639
	AST_RWLIST_RDLOCK(&backends);
640

    
   
640

   
641
	AST_RWLIST_TRAVERSE(&backends, chanls, list) {
641
	AST_RWLIST_TRAVERSE(&backends, chanls, list) {
642
		if (!strcasecmp(name, chanls->tech->type)) {
642
		if (!strcasecmp(name, chanls->tech->type)) {
643
			ret = chanls->tech;
643
			ret = chanls->tech;
644
			break;
644
			break;
645
		}
645
		}
646
	}
646
	}
647

    
   
647

   
648
	AST_RWLIST_UNLOCK(&backends);
648
	AST_RWLIST_UNLOCK(&backends);
649

    
   
649

   
650
	return ret;
650
	return ret;
651
}
651
}
652

    
   
652

   
653
/*! \brief Gives the string form of a given hangup cause */
653
/*! \brief Gives the string form of a given hangup cause */
654
const char *ast_cause2str(int cause)
654
const char *ast_cause2str(int cause)
655
{
655
{
656
	int x;
656
	int x;
657

    
   
657

   
658
	for (x = 0; x < ARRAY_LEN(causes); x++) {
658
	for (x = 0; x < ARRAY_LEN(causes); x++) {
659
		if (causes[x].cause == cause)
659
		if (causes[x].cause == cause)
660
			return causes[x].desc;
660
			return causes[x].desc;
661
	}
661
	}
662

    
   
662

   
663
	return "Unknown";
663
	return "Unknown";
664
}
664
}
665

    
   
665

   
666
/*! \brief Convert a symbolic hangup cause to number */
666
/*! \brief Convert a symbolic hangup cause to number */
667
int ast_str2cause(const char *name)
667
int ast_str2cause(const char *name)
668
{
668
{
669
	int x;
669
	int x;
670

    
   
670

   
671
	for (x = 0; x < ARRAY_LEN(causes); x++)
671
	for (x = 0; x < ARRAY_LEN(causes); x++)
672
		if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
672
		if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
673
			return causes[x].cause;
673
			return causes[x].cause;
674

    
   
674

   
675
	return -1;
675
	return -1;
676
}
676
}
677

    
   
677

   
678
static struct stasis_message *create_channel_snapshot_message(struct ast_channel *channel)
678
static struct stasis_message *create_channel_snapshot_message(struct ast_channel *channel)
679
{
679
{
680
	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
680
	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
681
	ast_channel_lock(channel);
681
	ast_channel_lock(channel);
682
	snapshot = ast_channel_snapshot_create(channel);
682
	snapshot = ast_channel_snapshot_create(channel);
683
	ast_channel_unlock(channel);
683
	ast_channel_unlock(channel);
684
	if (!snapshot) {
684
	if (!snapshot) {
685
		return NULL;
685
		return NULL;
686
	}
686
	}
687

    
   
687

   
688
	return stasis_message_create(ast_channel_snapshot_type(), snapshot);
688
	return stasis_message_create(ast_channel_snapshot_type(), snapshot);
689
}
689
}
690

    
   
690

   
691
static void publish_cache_clear(struct ast_channel *chan)
691
static void publish_cache_clear(struct ast_channel *chan)
692
{
692
{
693
	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
693
	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
694
	RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
694
	RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
695

    
   
695

   
696
	clear_msg = create_channel_snapshot_message(chan);
696
	clear_msg = create_channel_snapshot_message(chan);
697
	if (!clear_msg) {
697
	if (!clear_msg) {
698
		return;
698
		return;
699
	}
699
	}
700

    
   
700

   
701
	message = stasis_cache_clear_create(clear_msg);
701
	message = stasis_cache_clear_create(clear_msg);
702
	stasis_publish(ast_channel_topic(chan), message);
702
	stasis_publish(ast_channel_topic(chan), message);
703
}
703
}
704

    
   
704

   
705
/*! \brief Gives the string form of a given channel state.
705
/*! \brief Gives the string form of a given channel state.
706
 *
706
 *
707
 * \note This function is not reentrant.
707
 * \note This function is not reentrant.
708
 *
708
 *
709
 * \param state
709
 * \param state
710
 */
710
 */
711
const char *ast_state2str(enum ast_channel_state state)
711
const char *ast_state2str(enum ast_channel_state state)
712
{
712
{
713
	char *buf;
713
	char *buf;
714

    
   
714

   
715
	switch (state) {
715
	switch (state) {
716
	case AST_STATE_DOWN:
716
	case AST_STATE_DOWN:
717
		return "Down";
717
		return "Down";
718
	case AST_STATE_RESERVED:
718
	case AST_STATE_RESERVED:
719
		return "Rsrvd";
719
		return "Rsrvd";
720
	case AST_STATE_OFFHOOK:
720
	case AST_STATE_OFFHOOK:
721
		return "OffHook";
721
		return "OffHook";
722
	case AST_STATE_DIALING:
722
	case AST_STATE_DIALING:
723
		return "Dialing";
723
		return "Dialing";
724
	case AST_STATE_RING:
724
	case AST_STATE_RING:
725
		return "Ring";
725
		return "Ring";
726
	case AST_STATE_RINGING:
726
	case AST_STATE_RINGING:
727
		return "Ringing";
727
		return "Ringing";
728
	case AST_STATE_UP:
728
	case AST_STATE_UP:
729
		return "Up";
729
		return "Up";
730
	case AST_STATE_BUSY:
730
	case AST_STATE_BUSY:
731
		return "Busy";
731
		return "Busy";
732
	case AST_STATE_DIALING_OFFHOOK:
732
	case AST_STATE_DIALING_OFFHOOK:
733
		return "Dialing Offhook";
733
		return "Dialing Offhook";
734
	case AST_STATE_PRERING:
734
	case AST_STATE_PRERING:
735
		return "Pre-ring";
735
		return "Pre-ring";
736
	case AST_STATE_MUTE:
736
	case AST_STATE_MUTE:
737
		return "Mute";
737
		return "Mute";
738
	default:
738
	default:
739
		if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE)))
739
		if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE)))
740
			return "Unknown";
740
			return "Unknown";
741
		snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%u)", state);
741
		snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%u)", state);
742
		return buf;
742
		return buf;
743
	}
743
	}
744
}
744
}
745

    
   
745

   
746
/*! \brief Gives the string form of a given transfer capability */
746
/*! \brief Gives the string form of a given transfer capability */
747
char *ast_transfercapability2str(int transfercapability)
747
char *ast_transfercapability2str(int transfercapability)
748
{
748
{
749
	switch (transfercapability) {
749
	switch (transfercapability) {
750
	case AST_TRANS_CAP_SPEECH:
750
	case AST_TRANS_CAP_SPEECH:
751
		return "SPEECH";
751
		return "SPEECH";
752
	case AST_TRANS_CAP_DIGITAL:
752
	case AST_TRANS_CAP_DIGITAL:
753
		return "DIGITAL";
753
		return "DIGITAL";
754
	case AST_TRANS_CAP_RESTRICTED_DIGITAL:
754
	case AST_TRANS_CAP_RESTRICTED_DIGITAL:
755
		return "RESTRICTED_DIGITAL";
755
		return "RESTRICTED_DIGITAL";
756
	case AST_TRANS_CAP_3_1K_AUDIO:
756
	case AST_TRANS_CAP_3_1K_AUDIO:
757
		return "3K1AUDIO";
757
		return "3K1AUDIO";
758
	case AST_TRANS_CAP_DIGITAL_W_TONES:
758
	case AST_TRANS_CAP_DIGITAL_W_TONES:
759
		return "DIGITAL_W_TONES";
759
		return "DIGITAL_W_TONES";
760
	case AST_TRANS_CAP_VIDEO:
760
	case AST_TRANS_CAP_VIDEO:
761
		return "VIDEO";
761
		return "VIDEO";
762
	default:
762
	default:
763
		return "UNKNOWN";
763
		return "UNKNOWN";
764
	}
764
	}
765
}
765
}
766

    
   
766

   
767
/*! \brief Pick the best audio codec */
767
/*! \brief Pick the best audio codec */
768
struct ast_format *ast_best_codec(struct ast_format_cap *cap, struct ast_format *result)
768
struct ast_format *ast_best_codec(struct ast_format_cap *cap, struct ast_format *result)
769
{
769
{
770
	/* This just our opinion, expressed in code.  We are asked to choose
770
	/* This just our opinion, expressed in code.  We are asked to choose
771
	   the best codec to use, given no information */
771
	   the best codec to use, given no information */
772
	static const enum ast_format_id prefs[] =
772
	static const enum ast_format_id prefs[] =
773
	{
773
	{
774
		/*! Okay, ulaw is used by all telephony equipment, so start with it */
774
		/*! Okay, ulaw is used by all telephony equipment, so start with it */
775
		AST_FORMAT_ULAW,
775
		AST_FORMAT_ULAW,
776
		/*! Unless of course, you're a silly European, so then prefer ALAW */
776
		/*! Unless of course, you're a silly European, so then prefer ALAW */
777
		AST_FORMAT_ALAW,
777
		AST_FORMAT_ALAW,
778
		AST_FORMAT_G719,
778
		AST_FORMAT_G719,
779
		AST_FORMAT_SIREN14,
779
		AST_FORMAT_SIREN14,
780
		AST_FORMAT_SIREN7,
780
		AST_FORMAT_SIREN7,
781
		AST_FORMAT_TESTLAW,
781
		AST_FORMAT_TESTLAW,
782
		/*! G.722 is better then all below, but not as common as the above... so give ulaw and alaw priority */
782
		/*! G.722 is better then all below, but not as common as the above... so give ulaw and alaw priority */
783
		AST_FORMAT_G722,
783
		AST_FORMAT_G722,
784
		/*! Okay, well, signed linear is easy to translate into other stuff */
784
		/*! Okay, well, signed linear is easy to translate into other stuff */
785
		AST_FORMAT_SLINEAR192,
785
		AST_FORMAT_SLINEAR192,
786
		AST_FORMAT_SLINEAR96,
786
		AST_FORMAT_SLINEAR96,
787
		AST_FORMAT_SLINEAR48,
787
		AST_FORMAT_SLINEAR48,
788
		AST_FORMAT_SLINEAR44,
788
		AST_FORMAT_SLINEAR44,
789
		AST_FORMAT_SLINEAR32,
789
		AST_FORMAT_SLINEAR32,
790
		AST_FORMAT_SLINEAR24,
790
		AST_FORMAT_SLINEAR24,
791
		AST_FORMAT_SLINEAR16,
791
		AST_FORMAT_SLINEAR16,
792
		AST_FORMAT_SLINEAR12,
792
		AST_FORMAT_SLINEAR12,
793
		AST_FORMAT_SLINEAR,
793
		AST_FORMAT_SLINEAR,
794
		/*! G.726 is standard ADPCM, in RFC3551 packing order */
794
		/*! G.726 is standard ADPCM, in RFC3551 packing order */
795
		AST_FORMAT_G726,
795
		AST_FORMAT_G726,
796
		/*! G.726 is standard ADPCM, in AAL2 packing order */
796
		/*! G.726 is standard ADPCM, in AAL2 packing order */
797
		AST_FORMAT_G726_AAL2,
797
		AST_FORMAT_G726_AAL2,
798
		/*! ADPCM has great sound quality and is still pretty easy to translate */
798
		/*! ADPCM has great sound quality and is still pretty easy to translate */
799
		AST_FORMAT_ADPCM,
799
		AST_FORMAT_ADPCM,
800
		/*! Okay, we're down to vocoders now, so pick GSM because it's small and easier to
800
		/*! Okay, we're down to vocoders now, so pick GSM because it's small and easier to
801
		    translate and sounds pretty good */
801
		    translate and sounds pretty good */
802
		AST_FORMAT_GSM,
802
		AST_FORMAT_GSM,
803
		/*! iLBC is not too bad */
803
		/*! iLBC is not too bad */
804
		AST_FORMAT_ILBC,
804
		AST_FORMAT_ILBC,
805
		/*! Speex is free, but computationally more expensive than GSM */
805
		/*! Speex is free, but computationally more expensive than GSM */
806
		AST_FORMAT_SPEEX32,
806
		AST_FORMAT_SPEEX32,
807
		AST_FORMAT_SPEEX16,
807
		AST_FORMAT_SPEEX16,
808
		AST_FORMAT_SPEEX,
808
		AST_FORMAT_SPEEX,
809
		/*! Opus */
809
		/*! Opus */
810
		AST_FORMAT_OPUS,
810
		AST_FORMAT_OPUS,
811
		/*! SILK is pretty awesome. */
811
		/*! SILK is pretty awesome. */
812
		AST_FORMAT_SILK,
812
		AST_FORMAT_SILK,
813
		/*! CELT supports crazy high sample rates */
813
		/*! CELT supports crazy high sample rates */
814
		AST_FORMAT_CELT,
814
		AST_FORMAT_CELT,
815
		/*! Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
815
		/*! Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
816
		    to use it */
816
		    to use it */
817
		AST_FORMAT_LPC10,
817
		AST_FORMAT_LPC10,
818
		/*! G.729a is faster than 723 and slightly less expensive */
818
		/*! G.729a is faster than 723 and slightly less expensive */
819
		AST_FORMAT_G729A,
819
		AST_FORMAT_G729A,
820
		/*! Down to G.723.1 which is proprietary but at least designed for voice */
820
		/*! Down to G.723.1 which is proprietary but at least designed for voice */
821
		AST_FORMAT_G723_1,
821
		AST_FORMAT_G723_1,
822
	};
822
	};
823
	char buf[512];
823
	char buf[512];
824
	int x;
824
	int x;
825

    
   
825

   
826
	/* Find the first preferred codec in the format given */
826
	/* Find the first preferred codec in the format given */
827
	for (x = 0; x < ARRAY_LEN(prefs); x++) {
827
	for (x = 0; x < ARRAY_LEN(prefs); x++) {
828
		if (ast_format_cap_best_byid(cap, prefs[x], result)) {
828
		if (ast_format_cap_best_byid(cap, prefs[x], result)) {
829
			return result;
829
			return result;
830
		}
830
		}
831
	}
831
	}
832

    
   
832

   
833
	ast_format_clear(result);
833
	ast_format_clear(result);
834
	ast_log(LOG_WARNING, "Don't know any of %s formats\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
834
	ast_log(LOG_WARNING, "Don't know any of %s formats\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
835

    
   
835

   
836
	return NULL;
836
	return NULL;
837
}
837
}
838

    
   
838

   
839
/*! \brief Channel technology used to extract a channel from a running application. The
839
/*! \brief Channel technology used to extract a channel from a running application. The
840
 * channel created with this technology will be immediately hung up - most external
840
 * channel created with this technology will be immediately hung up - most external
841
 * applications won't ever want to see this.
841
 * applications won't ever want to see this.
842
 */
842
 */
843
static const struct ast_channel_tech surrogate_tech = {
843
static const struct ast_channel_tech surrogate_tech = {
844
	.type = "Surrogate",
844
	.type = "Surrogate",
845
	.description = "Surrogate channel used to pull channel from an application",
845
	.description = "Surrogate channel used to pull channel from an application",
846
	.properties = AST_CHAN_TP_INTERNAL,
846
	.properties = AST_CHAN_TP_INTERNAL,
847
};
847
};
848

    
   
848

   
849
static const struct ast_channel_tech null_tech = {
849
static const struct ast_channel_tech null_tech = {
850
	.type = "NULL",
850
	.type = "NULL",
851
	.description = "Null channel (should not see this)",
851
	.description = "Null channel (should not see this)",
852
};
852
};
853

    
   
853

   
854
static void ast_channel_destructor(void *obj);
854
static void ast_channel_destructor(void *obj);
855
static void ast_dummy_channel_destructor(void *obj);
855
static void ast_dummy_channel_destructor(void *obj);
856

    
   
856

   
857
/*! \brief Create a new channel structure */
857
/*! \brief Create a new channel structure */
858
static struct ast_channel * attribute_malloc __attribute__((format(printf, 13, 0)))
858
static struct ast_channel * attribute_malloc __attribute__((format(printf, 13, 0)))
859
__ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
859
__ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
860
		       const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
860
		       const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
861
		       const struct ast_channel *requestor, enum ama_flags amaflag, const char *file, int line,
861
		       const struct ast_channel *requestor, enum ama_flags amaflag, const char *file, int line,
862
		       const char *function, const char *name_fmt, va_list ap)
862
		       const char *function, const char *name_fmt, va_list ap)
863
{
863
{
864
	struct ast_channel *tmp;
864
	struct ast_channel *tmp;
865
	struct varshead *headp;
865
	struct varshead *headp;
866
	char *tech = "", *tech2 = NULL;
866
	char *tech = "", *tech2 = NULL;
867
	struct ast_format_cap *nativeformats;
867
	struct ast_format_cap *nativeformats;
868
	struct ast_sched_context *schedctx;
868
	struct ast_sched_context *schedctx;
869
	struct ast_timer *timer;
869
	struct ast_timer *timer;
870
	struct timeval now;
870
	struct timeval now;
871
	const struct ast_channel_tech *channel_tech;
871
	const struct ast_channel_tech *channel_tech;
872

    
   
872

   
873
	/* If shutting down, don't allocate any new channels */
873
	/* If shutting down, don't allocate any new channels */
874
	if (ast_shutting_down()) {
874
	if (ast_shutting_down()) {
875
		ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
875
		ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
876
		return NULL;
876
		return NULL;
877
	}
877
	}
878

    
   
878

   
879
	if (!(tmp = ast_channel_internal_alloc(ast_channel_destructor, assignedids, requestor))) {
879
	if (!(tmp = ast_channel_internal_alloc(ast_channel_destructor, assignedids, requestor))) {
880
		/* Channel structure allocation failure. */
880
		/* Channel structure allocation failure. */
881
		return NULL;
881
		return NULL;
882
	}
882
	}
883

    
   
883

   
884
	ast_channel_stage_snapshot(tmp);
884
	ast_channel_stage_snapshot(tmp);
885

    
   
885

   
886
	if (!(nativeformats = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_CACHE_STRINGS))) {
886
	if (!(nativeformats = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_CACHE_STRINGS))) {
887
		/* format capabilities structure allocation failure */
887
		/* format capabilities structure allocation failure */
888
		return ast_channel_unref(tmp);
888
		return ast_channel_unref(tmp);
889
	}
889
	}
890
	ast_channel_nativeformats_set(tmp, nativeformats);
890
	ast_channel_nativeformats_set(tmp, nativeformats);
891

    
   
891

   
892
	/*
892
	/*
893
	 * Init file descriptors to unopened state so
893
	 * Init file descriptors to unopened state so
894
	 * the destructor can know not to close them.
894
	 * the destructor can know not to close them.
895
	 */
895
	 */
896
	ast_channel_timingfd_set(tmp, -1);
896
	ast_channel_timingfd_set(tmp, -1);
897
	ast_channel_internal_alertpipe_clear(tmp);
897
	ast_channel_internal_alertpipe_clear(tmp);
898
	ast_channel_internal_fd_clear_all(tmp);
898
	ast_channel_internal_fd_clear_all(tmp);
899

    
   
899

   
900
#ifdef HAVE_EPOLL
900
#ifdef HAVE_EPOLL
901
	ast_channel_epfd_set(tmp, epoll_create(25));
901
	ast_channel_epfd_set(tmp, epoll_create(25));
902
#endif
902
#endif
903

    
   
903

   
904
	if (!(schedctx = ast_sched_context_create())) {
904
	if (!(schedctx = ast_sched_context_create())) {
905
		ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
905
		ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
906
		return ast_channel_unref(tmp);
906
		return ast_channel_unref(tmp);
907
	}
907
	}
908
	ast_channel_sched_set(tmp, schedctx);
908
	ast_channel_sched_set(tmp, schedctx);
909

    
   
909

   
910
	ast_party_dialed_init(ast_channel_dialed(tmp));
910
	ast_party_dialed_init(ast_channel_dialed(tmp));
911
	ast_party_caller_init(ast_channel_caller(tmp));
911
	ast_party_caller_init(ast_channel_caller(tmp));
912
	ast_party_connected_line_init(ast_channel_connected(tmp));
912
	ast_party_connected_line_init(ast_channel_connected(tmp));
913
	ast_party_connected_line_init(ast_channel_connected_indicated(tmp));
913
	ast_party_connected_line_init(ast_channel_connected_indicated(tmp));
914
	ast_party_redirecting_init(ast_channel_redirecting(tmp));
914
	ast_party_redirecting_init(ast_channel_redirecting(tmp));
915

    
   
915

   
916
	if (cid_name) {
916
	if (cid_name) {
917
		ast_channel_caller(tmp)->id.name.valid = 1;
917
		ast_channel_caller(tmp)->id.name.valid = 1;
918
		ast_channel_caller(tmp)->id.name.str = ast_strdup(cid_name);
918
		ast_channel_caller(tmp)->id.name.str = ast_strdup(cid_name);
919
		if (!ast_channel_caller(tmp)->id.name.str) {
919
		if (!ast_channel_caller(tmp)->id.name.str) {
920
			return ast_channel_unref(tmp);
920
			return ast_channel_unref(tmp);
921
		}
921
		}
922
	}
922
	}
923
	if (cid_num) {
923
	if (cid_num) {
924
		ast_channel_caller(tmp)->id.number.valid = 1;
924
		ast_channel_caller(tmp)->id.number.valid = 1;
925
		ast_channel_caller(tmp)->id.number.str = ast_strdup(cid_num);
925
		ast_channel_caller(tmp)->id.number.str = ast_strdup(cid_num);
926
		if (!ast_channel_caller(tmp)->id.number.str) {
926
		if (!ast_channel_caller(tmp)->id.number.str) {
927
			return ast_channel_unref(tmp);
927
			return ast_channel_unref(tmp);
928
		}
928
		}
929
	}
929
	}
930

    
   
930

   
931
	if ((timer = ast_timer_open())) {
931
	if ((timer = ast_timer_open())) {
932
		ast_channel_timer_set(tmp, timer);
932
		ast_channel_timer_set(tmp, timer);
933
		if (strcmp(ast_timer_get_name(ast_channel_timer(tmp)), "timerfd")) {
933
		if (strcmp(ast_timer_get_name(ast_channel_timer(tmp)), "timerfd")) {
934
			needqueue = 0;
934
			needqueue = 0;
935
		}
935
		}
936
		ast_channel_timingfd_set(tmp, ast_timer_fd(ast_channel_timer(tmp)));
936
		ast_channel_timingfd_set(tmp, ast_timer_fd(ast_channel_timer(tmp)));
937
	}
937
	}
938

    
   
938

   
939
	if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
939
	if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
940
		return ast_channel_unref(tmp);
940
		return ast_channel_unref(tmp);
941
	}
941
	}
942

    
   
942

   
943
	/* Always watch the alertpipe */
943
	/* Always watch the alertpipe */
944
	ast_channel_set_fd(tmp, AST_ALERT_FD, ast_channel_internal_alert_readfd(tmp));
944
	ast_channel_set_fd(tmp, AST_ALERT_FD, ast_channel_internal_alert_readfd(tmp));
945
	/* And timing pipe */
945
	/* And timing pipe */
946
	ast_channel_set_fd(tmp, AST_TIMING_FD, ast_channel_timingfd(tmp));
946
	ast_channel_set_fd(tmp, AST_TIMING_FD, ast_channel_timingfd(tmp));
947

    
   
947

   
948
	/* Initial state */
948
	/* Initial state */
949
	ast_channel_state_set(tmp, state);
949
	ast_channel_state_set(tmp, state);
950
	ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
950
	ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
951

    
   
951

   
952
	ast_channel_streamid_set(tmp, -1);
952
	ast_channel_streamid_set(tmp, -1);
953

    
   
953

   
954
	ast_channel_fin_set(tmp, global_fin);
954
	ast_channel_fin_set(tmp, global_fin);
955
	ast_channel_fout_set(tmp, global_fout);
955
	ast_channel_fout_set(tmp, global_fout);
956

    
   
956

   
957
	now = ast_tvnow();
957
	now = ast_tvnow();
958
	ast_channel_creationtime_set(tmp, &now);
958
	ast_channel_creationtime_set(tmp, &now);
959

    
   
959

   
960
	ast_channel_internal_setup_topics(tmp);
960
	ast_channel_internal_setup_topics(tmp);
961

    
   
961

   
962
	if (!ast_strlen_zero(name_fmt)) {
962
	if (!ast_strlen_zero(name_fmt)) {
963
		char *slash, *slash2;
963
		char *slash, *slash2;
964
		/* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
964
		/* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
965
		 * And they all use slightly different formats for their name string.
965
		 * And they all use slightly different formats for their name string.
966
		 * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
966
		 * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
967
		 * This means, that the stringfields must have a routine that takes the va_lists directly, and
967
		 * This means, that the stringfields must have a routine that takes the va_lists directly, and
968
		 * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
968
		 * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
969
		 * This new function was written so this can be accomplished.
969
		 * This new function was written so this can be accomplished.
970
		 */
970
		 */
971
		ast_channel_name_build_va(tmp, name_fmt, ap);
971
		ast_channel_name_build_va(tmp, name_fmt, ap);
972
		tech = ast_strdupa(ast_channel_name(tmp));
972
		tech = ast_strdupa(ast_channel_name(tmp));
973
		if ((slash = strchr(tech, '/'))) {
973
		if ((slash = strchr(tech, '/'))) {
974
			if ((slash2 = strchr(slash + 1, '/'))) {
974
			if ((slash2 = strchr(slash + 1, '/'))) {
975
				tech2 = slash + 1;
975
				tech2 = slash + 1;
976
				*slash2 = '\0';
976
				*slash2 = '\0';
977
			}
977
			}
978
			*slash = '\0';
978
			*slash = '\0';
979
		}
979
		}
980
	} else {
980
	} else {
981
		/*
981
		/*
982
		 * Start the string with '-' so it becomes an empty string
982
		 * Start the string with '-' so it becomes an empty string
983
		 * in the destructor.
983
		 * in the destructor.
984
		 */
984
		 */
985
		ast_channel_name_set(tmp, "-**Unknown**");
985
		ast_channel_name_set(tmp, "-**Unknown**");
986
	}
986
	}
987

    
   
987

   
988
	if (amaflag != AST_AMA_NONE) {
988
	if (amaflag != AST_AMA_NONE) {
989
		ast_channel_amaflags_set(tmp, amaflag);
989
		ast_channel_amaflags_set(tmp, amaflag);
990
	} else {
990
	} else {
991
		ast_channel_amaflags_set(tmp, DEFAULT_AMA_FLAGS);
991
		ast_channel_amaflags_set(tmp, DEFAULT_AMA_FLAGS);
992
	}
992
	}
993

    
   
993

   
994
	if (!ast_strlen_zero(acctcode)) {
994
	if (!ast_strlen_zero(acctcode)) {
995
		ast_channel_accountcode_set(tmp, acctcode);
995
		ast_channel_accountcode_set(tmp, acctcode);
996
	}
996
	}
997
	ast_channel_language_set(tmp, ast_defaultlanguage);
997
	ast_channel_language_set(tmp, ast_defaultlanguage);
998

    
   
998

   
999
	ast_channel_context_set(tmp, S_OR(context, "default"));
999
	ast_channel_context_set(tmp, S_OR(context, "default"));
1000
	ast_channel_exten_set(tmp, S_OR(exten, "s"));
1000
	ast_channel_exten_set(tmp, S_OR(exten, "s"));
1001
	ast_channel_priority_set(tmp, 1);
1001
	ast_channel_priority_set(tmp, 1);
1002

    
   
1002

   
1003
	headp = ast_channel_varshead(tmp);
1003
	headp = ast_channel_varshead(tmp);
1004
	AST_LIST_HEAD_INIT_NOLOCK(headp);
1004
	AST_LIST_HEAD_INIT_NOLOCK(headp);
1005

    
   
1005

   
1006
	ast_pbx_hangup_handler_init(tmp);
1006
	ast_pbx_hangup_handler_init(tmp);
1007
	AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
1007
	AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
1008
	AST_LIST_HEAD_INIT_NOLOCK(ast_channel_autochans(tmp));
1008
	AST_LIST_HEAD_INIT_NOLOCK(ast_channel_autochans(tmp));
1009

    
   
1009

   
1010
	channel_tech = ast_get_channel_tech(tech);
1010
	channel_tech = ast_get_channel_tech(tech);
1011
	if (!channel_tech && !ast_strlen_zero(tech2)) {
1011
	if (!channel_tech && !ast_strlen_zero(tech2)) {
1012
		channel_tech = ast_get_channel_tech(tech2);
1012
		channel_tech = ast_get_channel_tech(tech2);
1013
	}
1013
	}
1014
	if (channel_tech) {
1014
	if (channel_tech) {
1015
		ast_channel_tech_set(tmp, channel_tech);
1015
		ast_channel_tech_set(tmp, channel_tech);
1016
	} else {
1016
	} else {
1017
		ast_channel_tech_set(tmp, &null_tech);
1017
		ast_channel_tech_set(tmp, &null_tech);
1018
	}
1018
	}
1019

    
   
1019

   
1020
	ast_channel_internal_finalize(tmp);
1020
	ast_channel_internal_finalize(tmp);
1021

    
   
1021

   
1022
	ast_atomic_fetchadd_int(&chancount, +1);
1022
	ast_atomic_fetchadd_int(&chancount, +1);
1023

    
   
1023

   
1024
	/* You might scream "locking inversion" at seeing this but it is actually perfectly fine.
1024
	/* You might scream "locking inversion" at seeing this but it is actually perfectly fine.
1025
	 * Since the channel was just created nothing can know about it yet or even acquire it.
1025
	 * Since the channel was just created nothing can know about it yet or even acquire it.
1026
	 */
1026
	 */
1027
	ast_channel_lock(tmp);
1027
	ast_channel_lock(tmp);
1028

    
   
1028

   
1029
	ao2_link(channels, tmp);
1029
	ao2_link(channels, tmp);
1030

    
   
1030

   
1031
	/*
1031
	/*
1032
	 * And now, since the channel structure is built, and has its name, let
1032
	 * And now, since the channel structure is built, and has its name, let
1033
	 * the world know of its existance
1033
	 * the world know of its existance
1034
	 */
1034
	 */
1035
	ast_channel_stage_snapshot_done(tmp);
1035
	ast_channel_stage_snapshot_done(tmp);
1036
	return tmp;
1036
	return tmp;
1037
}
1037
}
1038

    
   
1038

   
1039
struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
1039
struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
1040
					const char *cid_name, const char *acctcode,
1040
					const char *cid_name, const char *acctcode,
1041
					const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
1041
					const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
1042
					const struct ast_channel *requestor, enum ama_flags amaflag,
1042
					const struct ast_channel *requestor, enum ama_flags amaflag,
1043
					const char *file, int line, const char *function,
1043
					const char *file, int line, const char *function,
1044
					const char *name_fmt, ...)
1044
					const char *name_fmt, ...)
1045
{
1045
{
1046
	va_list ap;
1046
	va_list ap;
1047
	struct ast_channel *result;
1047
	struct ast_channel *result;
1048

    
   
1048

   
1049
	va_start(ap, name_fmt);
1049
	va_start(ap, name_fmt);
1050
	result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
1050
	result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
1051
					assignedids, requestor, amaflag, file, line, function, name_fmt, ap);
1051
					assignedids, requestor, amaflag, file, line, function, name_fmt, ap);
1052
	va_end(ap);
1052
	va_end(ap);
1053

    
   
1053

   
1054
	return result;
1054
	return result;
1055
}
1055
}
1056

    
   
1056

   
1057
/* only do the minimum amount of work needed here to make a channel
1057
/* only do the minimum amount of work needed here to make a channel
1058
 * structure that can be used to expand channel vars */
1058
 * structure that can be used to expand channel vars */
1059
#if defined(REF_DEBUG) || defined(__AST_DEBUG_MALLOC)
1059
#if defined(REF_DEBUG) || defined(__AST_DEBUG_MALLOC)
1060
struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
1060
struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
1061
#else
1061
#else
1062
struct ast_channel *ast_dummy_channel_alloc(void)
1062
struct ast_channel *ast_dummy_channel_alloc(void)
1063
#endif
1063
#endif
1064
{
1064
{
1065
	struct ast_channel *tmp;
1065
	struct ast_channel *tmp;
1066
	struct varshead *headp;
1066
	struct varshead *headp;
1067

    
   
1067

   
1068
	if (!(tmp = ast_channel_internal_alloc(ast_dummy_channel_destructor, NULL, NULL))) {
1068
	if (!(tmp = ast_channel_internal_alloc(ast_dummy_channel_destructor, NULL, NULL))) {
1069
		/* Dummy channel structure allocation failure. */
1069
		/* Dummy channel structure allocation failure. */
1070
		return NULL;
1070
		return NULL;
1071
	}
1071
	}
1072

    
   
1072

   
1073
	ast_pbx_hangup_handler_init(tmp);
1073
	ast_pbx_hangup_handler_init(tmp);
1074
	AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
1074
	AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
1075

    
   
1075

   
1076
	/*
1076
	/*
1077
	 * Init file descriptors to unopened state just in case
1077
	 * Init file descriptors to unopened state just in case
1078
	 * autoservice is called on the channel or something tries to
1078
	 * autoservice is called on the channel or something tries to
1079
	 * read a frame from it.
1079
	 * read a frame from it.
1080
	 */
1080
	 */
1081
	ast_channel_timingfd_set(tmp, -1);
1081
	ast_channel_timingfd_set(tmp, -1);
1082
	ast_channel_internal_alertpipe_clear(tmp);
1082
	ast_channel_internal_alertpipe_clear(tmp);
1083
	ast_channel_internal_fd_clear_all(tmp);
1083
	ast_channel_internal_fd_clear_all(tmp);
1084
#ifdef HAVE_EPOLL
1084
#ifdef HAVE_EPOLL
1085
	ast_channel_epfd_set(tmp, -1);
1085
	ast_channel_epfd_set(tmp, -1);
1086
#endif
1086
#endif
1087

    
   
1087

   
1088
	ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
1088
	ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
1089

    
   
1089

   
1090
	ast_channel_internal_setup_topics(tmp);
1090
	ast_channel_internal_setup_topics(tmp);
1091

    
   
1091

   
1092
	headp = ast_channel_varshead(tmp);
1092
	headp = ast_channel_varshead(tmp);
1093
	AST_LIST_HEAD_INIT_NOLOCK(headp);
1093
	AST_LIST_HEAD_INIT_NOLOCK(headp);
1094

    
   
1094

   
1095
	return tmp;
1095
	return tmp;
1096
}
1096
}
1097

    
   
1097

   
1098
static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1098
static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1099
{
1099
{
1100
	struct ast_frame *f;
1100
	struct ast_frame *f;
1101
	struct ast_frame *cur;
1101
	struct ast_frame *cur;
1102
	unsigned int new_frames = 0;
1102
	unsigned int new_frames = 0;
1103
	unsigned int new_voice_frames = 0;
1103
	unsigned int new_voice_frames = 0;
1104
	unsigned int queued_frames = 0;
1104
	unsigned int queued_frames = 0;
1105
	unsigned int queued_voice_frames = 0;
1105
	unsigned int queued_voice_frames = 0;
1106
	AST_LIST_HEAD_NOLOCK(,ast_frame) frames;
1106
	AST_LIST_HEAD_NOLOCK(,ast_frame) frames;
1107

    
   
1107

   
1108
	ast_channel_lock(chan);
1108
	ast_channel_lock(chan);
1109

    
   
1109

   
1110
	/*
1110
	/*
1111
	 * Check the last frame on the queue if we are queuing the new
1111
	 * Check the last frame on the queue if we are queuing the new
1112
	 * frames after it.
1112
	 * frames after it.
1113
	 */
1113
	 */
1114
	cur = AST_LIST_LAST(ast_channel_readq(chan));
1114
	cur = AST_LIST_LAST(ast_channel_readq(chan));
1115
	if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1115
	if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1116
		switch (cur->subclass.integer) {
1116
		switch (cur->subclass.integer) {
1117
		case AST_CONTROL_END_OF_Q:
1117
		case AST_CONTROL_END_OF_Q:
1118
			if (fin->frametype == AST_FRAME_CONTROL
1118
			if (fin->frametype == AST_FRAME_CONTROL
1119
				&& fin->subclass.integer == AST_CONTROL_HANGUP) {
1119
				&& fin->subclass.integer == AST_CONTROL_HANGUP) {
1120
				/*
1120
				/*
1121
				 * Destroy the end-of-Q marker frame so we can queue the hangup
1121
				 * Destroy the end-of-Q marker frame so we can queue the hangup
1122
				 * frame in its place.
1122
				 * frame in its place.
1123
				 */
1123
				 */
1124
				AST_LIST_REMOVE(ast_channel_readq(chan), cur, frame_list);
1124
				AST_LIST_REMOVE(ast_channel_readq(chan), cur, frame_list);
1125
				ast_frfree(cur);
1125
				ast_frfree(cur);
1126

    
   
1126

   
1127
				/*
1127
				/*
1128
				 * This has degenerated to a normal queue append anyway.  Since
1128
				 * This has degenerated to a normal queue append anyway.  Since
1129
				 * we just destroyed the last frame in the queue we must make
1129
				 * we just destroyed the last frame in the queue we must make
1130
				 * sure that "after" is NULL or bad things will happen.
1130
				 * sure that "after" is NULL or bad things will happen.
1131
				 */
1131
				 */
1132
				after = NULL;
1132
				after = NULL;
1133
				break;
1133
				break;
1134
			}
1134
			}
1135
			/* Fall through */
1135
			/* Fall through */
1136
		case AST_CONTROL_HANGUP:
1136
		case AST_CONTROL_HANGUP:
1137
			/* Don't queue anything. */
1137
			/* Don't queue anything. */
1138
			ast_channel_unlock(chan);
1138
			ast_channel_unlock(chan);
1139
			return 0;
1139
			return 0;
1140
		default:
1140
		default:
1141
			break;
1141
			break;
1142
		}
1142
		}
1143
	}
1143
	}
1144

    
   
1144

   
1145
	/* Build copies of all the new frames and count them */
1145
	/* Build copies of all the new frames and count them */
1146
	AST_LIST_HEAD_INIT_NOLOCK(&frames);
1146
	AST_LIST_HEAD_INIT_NOLOCK(&frames);
1147
	for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1147
	for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1148
		if (!(f = ast_frdup(cur))) {
1148
		if (!(f = ast_frdup(cur))) {
1149
			if (AST_LIST_FIRST(&frames)) {
1149
			if (AST_LIST_FIRST(&frames)) {
1150
				ast_frfree(AST_LIST_FIRST(&frames));
1150
				ast_frfree(AST_LIST_FIRST(&frames));
1151
			}
1151
			}
1152
			ast_channel_unlock(chan);
1152
			ast_channel_unlock(chan);
1153
			return -1;
1153
			return -1;
1154
		}
1154
		}
1155

    
   
1155

   
1156
		AST_LIST_INSERT_TAIL(&frames, f, frame_list);
1156
		AST_LIST_INSERT_TAIL(&frames, f, frame_list);
1157
		new_frames++;
1157
		new_frames++;
1158
		if (f->frametype == AST_FRAME_VOICE) {
1158
		if (f->frametype == AST_FRAME_VOICE) {
1159
			new_voice_frames++;
1159
			new_voice_frames++;
1160
		}
1160
		}
1161
	}
1161
	}
1162

    
   
1162

   
1163
	/* Count how many frames exist on the queue */
1163
	/* Count how many frames exist on the queue */
1164
	AST_LIST_TRAVERSE(ast_channel_readq(chan), cur, frame_list) {
1164
	AST_LIST_TRAVERSE(ast_channel_readq(chan), cur, frame_list) {
1165
		queued_frames++;
1165
		queued_frames++;
1166
		if (cur->frametype == AST_FRAME_VOICE) {
1166
		if (cur->frametype == AST_FRAME_VOICE) {
1167
			queued_voice_frames++;
1167
			queued_voice_frames++;
1168
		}
1168
		}
1169
	}
1169
	}
1170

    
   
1170

   
1171
	if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1171
	if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1172
		int count = 0;
1172
		int count = 0;
1173
		ast_log(LOG_WARNING, "Exceptionally long %squeue length queuing to %s\n", queued_frames + new_frames > 128 ? "" : "voice ", ast_channel_name(chan));
1173
		ast_log(LOG_WARNING, "Exceptionally long %squeue length queuing to %s\n", queued_frames + new_frames > 128 ? "" : "voice ", ast_channel_name(chan));
1174
		AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), cur, frame_list) {
1174
		AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), cur, frame_list) {
1175
			/* Save the most recent frame */
1175
			/* Save the most recent frame */
1176
			if (!AST_LIST_NEXT(cur, frame_list)) {
1176
			if (!AST_LIST_NEXT(cur, frame_list)) {
1177
				break;
1177
				break;
1178
			} else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1178
			} else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1179
				if (++count > 64) {
1179
				if (++count > 64) {
1180
					break;
1180
					break;
1181
				}
1181
				}
1182
				AST_LIST_REMOVE_CURRENT(frame_list);
1182
				AST_LIST_REMOVE_CURRENT(frame_list);
1183
				ast_frfree(cur);
1183
				ast_frfree(cur);
1184
			}
1184
			}
1185
		}
1185
		}
1186
		AST_LIST_TRAVERSE_SAFE_END;
1186
		AST_LIST_TRAVERSE_SAFE_END;
1187
	}
1187
	}
1188

    
   
1188

   
1189
	if (after) {
1189
	if (after) {
1190
		AST_LIST_INSERT_LIST_AFTER(ast_channel_readq(chan), &frames, after, frame_list);
1190
		AST_LIST_INSERT_LIST_AFTER(ast_channel_readq(chan), &frames, after, frame_list);
1191
	} else {
1191
	} else {
1192
		if (head) {
1192
		if (head) {
1193
			AST_LIST_APPEND_LIST(&frames, ast_channel_readq(chan), frame_list);
1193
			AST_LIST_APPEND_LIST(&frames, ast_channel_readq(chan), frame_list);
1194
			AST_LIST_HEAD_INIT_NOLOCK(ast_channel_readq(chan));
1194
			AST_LIST_HEAD_INIT_NOLOCK(ast_channel_readq(chan));
1195
		}
1195
		}
1196
		AST_LIST_APPEND_LIST(ast_channel_readq(chan), &frames, frame_list);
1196
		AST_LIST_APPEND_LIST(ast_channel_readq(chan), &frames, frame_list);
1197
	}
1197
	}
1198

    
   
1198

   
1199
	if (ast_channel_alert_writable(chan)) {
1199
	if (ast_channel_alert_writable(chan)) {
1200
		if (ast_channel_alert_write(chan)) {
1200
		if (ast_channel_alert_write(chan)) {
1201
			ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %u): %s!\n",
1201
			ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %u): %s!\n",
1202
				ast_channel_name(chan), queued_frames, strerror(errno));
1202
				ast_channel_name(chan), queued_frames, strerror(errno));
1203
		}
1203
		}
1204
	} else if (ast_channel_timingfd(chan) > -1) {
1204
	} else if (ast_channel_timingfd(chan) > -1) {
1205
		ast_timer_enable_continuous(ast_channel_timer(chan));
1205
		ast_timer_enable_continuous(ast_channel_timer(chan));
1206
	} else if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
1206
	} else if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
1207
		pthread_kill(ast_channel_blocker(chan), SIGURG);
1207
		pthread_kill(ast_channel_blocker(chan), SIGURG);
1208
	}
1208
	}
1209

    
   
1209

   
1210
	ast_channel_unlock(chan);
1210
	ast_channel_unlock(chan);
1211

    
   
1211

   
1212
	return 0;
1212
	return 0;
1213
}
1213
}
1214

    
   
1214

   
1215
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1215
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1216
{
1216
{
1217
	return __ast_queue_frame(chan, fin, 0, NULL);
1217
	return __ast_queue_frame(chan, fin, 0, NULL);
1218
}
1218
}
1219

    
   
1219

   
1220
int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
1220
int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
1221
{
1221
{
1222
	return __ast_queue_frame(chan, fin, 1, NULL);
1222
	return __ast_queue_frame(chan, fin, 1, NULL);
1223
}
1223
}
1224

    
   
1224

   
1225
/*! \brief Queue a hangup frame for channel */
1225
/*! \brief Queue a hangup frame for channel */
1226
int ast_queue_hangup(struct ast_channel *chan)
1226
int ast_queue_hangup(struct ast_channel *chan)
1227
{
1227
{
1228
	struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1228
	struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1229
	int res;
1229
	int res;
1230

    
   
1230

   
1231
	/* Yeah, let's not change a lock-critical value without locking */
1231
	/* Yeah, let's not change a lock-critical value without locking */
1232
	ast_channel_lock(chan);
1232
	ast_channel_lock(chan);
1233
	ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1233
	ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1234
	ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), NULL);
1234
	ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), NULL);
1235

    
   
1235

   
1236
	res = ast_queue_frame(chan, &f);
1236
	res = ast_queue_frame(chan, &f);
1237
	ast_channel_unlock(chan);
1237
	ast_channel_unlock(chan);
1238
	return res;
1238
	return res;
1239
}
1239
}
1240

    
   
1240

   
1241
/*! \brief Queue a hangup frame for channel */
1241
/*! \brief Queue a hangup frame for channel */
1242
int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
1242
int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
1243
{
1243
{
1244
	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1244
	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1245
	struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1245
	struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1246
	int res;
1246
	int res;
1247

    
   
1247

   
1248
	if (cause >= 0) {
1248
	if (cause >= 0) {
1249
		f.data.uint32 = cause;
1249
		f.data.uint32 = cause;
1250
	}
1250
	}
1251

    
   
1251

   
1252
	/* Yeah, let's not change a lock-critical value without locking */
1252
	/* Yeah, let's not change a lock-critical value without locking */
1253
	ast_channel_lock(chan);
1253
	ast_channel_lock(chan);
1254
	ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1254
	ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1255
	if (cause < 0) {
1255
	if (cause < 0) {
1256
		f.data.uint32 = ast_channel_hangupcause(chan);
1256
		f.data.uint32 = ast_channel_hangupcause(chan);
1257
	}
1257
	}
1258
	blob = ast_json_pack("{s: i}",
1258
	blob = ast_json_pack("{s: i}",
1259
			     "cause", cause);
1259
			     "cause", cause);
1260
	ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
1260
	ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
1261

    
   
1261

   
1262
	res = ast_queue_frame(chan, &f);
1262
	res = ast_queue_frame(chan, &f);
1263
	ast_channel_unlock(chan);
1263
	ast_channel_unlock(chan);
1264
	return res;
1264
	return res;
1265
}
1265
}
1266

    
   
1266

   
1267
int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
1267
int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
1268
{
1268
{
1269
	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1269
	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1270
	struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HOLD };
1270
	struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HOLD };
1271
	int res;
1271
	int res;
1272

    
   
1272

   
1273
	ast_channel_lock(chan);
1273
	ast_channel_lock(chan);
1274
	if (!ast_strlen_zero(musicclass)) {
1274
	if (!ast_strlen_zero(musicclass)) {
1275
		f.data.ptr = (void *) musicclass;
1275
		f.data.ptr = (void *) musicclass;
1276
		f.datalen = strlen(musicclass) + 1;
1276
		f.datalen = strlen(musicclass) + 1;
1277

    
   
1277

   
1278
		blob = ast_json_pack("{s: s}",
1278
		blob = ast_json_pack("{s: s}",
1279
				     "musicclass", musicclass);
1279
				     "musicclass", musicclass);
1280
	}
1280
	}
1281

    
   
1281

   
1282
	ast_channel_publish_blob(chan, ast_channel_hold_type(), blob);
1282
	ast_channel_publish_blob(chan, ast_channel_hold_type(), blob);
1283

    
   
1283

   
1284
	res = ast_queue_frame(chan, &f);
1284
	res = ast_queue_frame(chan, &f);
1285
	ast_channel_unlock(chan);
1285
	ast_channel_unlock(chan);
1286
	return res;
1286
	return res;
1287
}
1287
}
1288

    
   
1288

   
1289
int ast_queue_unhold(struct ast_channel *chan)
1289
int ast_queue_unhold(struct ast_channel *chan)
1290
{
1290
{
1291
	struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_UNHOLD };
1291
	struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_UNHOLD };
1292
	int res;
1292
	int res;
1293

    
   
1293

   
1294
	ast_channel_lock(chan);
1294
	ast_channel_lock(chan);
1295
	ast_channel_publish_blob(chan, ast_channel_unhold_type(), NULL);
1295
	ast_channel_publish_blob(chan, ast_channel_unhold_type(), NULL);
1296

    
   
1296

   
1297
	res = ast_queue_frame(chan, &f);
1297
	res = ast_queue_frame(chan, &f);
1298
	ast_channel_unlock(chan);
1298
	ast_channel_unlock(chan);
1299
	return res;
1299
	return res;
1300
}
1300
}
1301

    
   
1301

   
1302
/*! \brief Queue a control frame */
1302
/*! \brief Queue a control frame */
1303
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
1303
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
1304
{
1304
{
1305
	struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1305
	struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1306
	return ast_queue_frame(chan, &f);
1306
	return ast_queue_frame(chan, &f);
1307
}
1307
}
1308

    
   
1308

   
1309
/*! \brief Queue a control frame with payload */
1309
/*! \brief Queue a control frame with payload */
1310
int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control,
1310
int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control,
1311
			   const void *data, size_t datalen)
1311
			   const void *data, size_t datalen)
1312
{
1312
{
1313
	struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1313
	struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1314
	return ast_queue_frame(chan, &f);
1314
	return ast_queue_frame(chan, &f);
1315
}
1315
}
1316

    
   
1316

   
1317
/*! \brief Set defer DTMF flag on channel */
1317
/*! \brief Set defer DTMF flag on channel */
1318
int ast_channel_defer_dtmf(struct ast_channel *chan)
1318
int ast_channel_defer_dtmf(struct ast_channel *chan)
1319
{
1319
{
1320
	int pre = 0;
1320
	int pre = 0;
1321

    
   
1321

   
1322
	if (chan) {
1322
	if (chan) {
1323
		pre = ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1323
		pre = ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1324
		ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1324
		ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1325
	}
1325
	}
1326
	return pre;
1326
	return pre;
1327
}
1327
}
1328

    
   
1328

   
1329
/*! \brief Unset defer DTMF flag on channel */
1329
/*! \brief Unset defer DTMF flag on channel */
1330
void ast_channel_undefer_dtmf(struct ast_channel *chan)
1330
void ast_channel_undefer_dtmf(struct ast_channel *chan)
1331
{
1331
{
1332
	if (chan)
1332
	if (chan)
1333
		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1333
		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1334
}
1334
}
1335

    
   
1335

   
1336
struct ast_channel *ast_channel_callback(ao2_callback_data_fn *cb_fn, void *arg,
1336
struct ast_channel *ast_channel_callback(ao2_callback_data_fn *cb_fn, void *arg,
1337
		void *data, int ao2_flags)
1337
		void *data, int ao2_flags)
1338
{
1338
{
1339
	return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
1339
	return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
1340
}
1340
}
1341

    
   
1341

   
1342
static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
1342
static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
1343
{
1343
{
1344
	struct ast_channel *chan = obj;
1344
	struct ast_channel *chan = obj;
1345
	const char *name = arg;
1345
	const char *name = arg;
1346
	size_t name_len = *(size_t *) data;
1346
	size_t name_len = *(size_t *) data;
1347
	int ret = CMP_MATCH;
1347
	int ret = CMP_MATCH;
1348

    
   
1348

   
1349
	if (ast_strlen_zero(name)) {
1349
	if (ast_strlen_zero(name)) {
1350
		ast_log(LOG_ERROR, "BUG! Must supply a channel name or partial name to match!\n");
1350
		ast_log(LOG_ERROR, "BUG! Must supply a channel name or partial name to match!\n");
1351
		return CMP_STOP;
1351
		return CMP_STOP;
1352
	}
1352
	}
1353

    
   
1353

   
1354
	ast_channel_lock(chan);
1354
	ast_channel_lock(chan);
1355
	if ((!name_len && strcasecmp(ast_channel_name(chan), name))
1355
	if ((!name_len && strcasecmp(ast_channel_name(chan), name))
1356
		|| (name_len && strncasecmp(ast_channel_name(chan), name, name_len))) {
1356
		|| (name_len && strncasecmp(ast_channel_name(chan), name, name_len))) {
1357
		ret = 0; /* name match failed, keep looking */
1357
		ret = 0; /* name match failed, keep looking */
1358
	}
1358
	}
1359
	ast_channel_unlock(chan);
1359
	ast_channel_unlock(chan);
1360

    
   
1360

   
1361
	return ret;
1361
	return ret;
1362
}
1362
}
1363

    
   
1363

   
1364
static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
1364
static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
1365
{
1365
{
1366
	struct ast_channel *chan = obj;
1366
	struct ast_channel *chan = obj;
1367
	char *context = arg;
1367
	char *context = arg;
1368
	char *exten = data;
1368
	char *exten = data;
1369
	int ret = CMP_MATCH;
1369
	int ret = CMP_MATCH;
1370

    
   
1370

   
1371
	if (ast_strlen_zero(exten) || ast_strlen_zero(context)) {
1371
	if (ast_strlen_zero(exten) || ast_strlen_zero(context)) {
1372
		ast_log(LOG_ERROR, "BUG! Must have a context and extension to match!\n");
1372
		ast_log(LOG_ERROR, "BUG! Must have a context and extension to match!\n");
1373
		return CMP_STOP;
1373
		return CMP_STOP;
1374
	}
1374
	}
1375

    
   
1375

   
1376
	ast_channel_lock(chan);
1376
	ast_channel_lock(chan);
1377
	if (strcasecmp(ast_channel_context(chan), context) && strcasecmp(ast_channel_macrocontext(chan), context)) {
1377
	if (strcasecmp(ast_channel_context(chan), context) && strcasecmp(ast_channel_macrocontext(chan), context)) {
1378
		ret = 0; /* Context match failed, continue */
1378
		ret = 0; /* Context match failed, continue */
1379
	} else if (strcasecmp(ast_channel_exten(chan), exten) && strcasecmp(ast_channel_macroexten(chan), exten)) {
1379
	} else if (strcasecmp(ast_channel_exten(chan), exten) && strcasecmp(ast_channel_macroexten(chan), exten)) {
1380
		ret = 0; /* Extension match failed, continue */
1380
		ret = 0; /* Extension match failed, continue */
1381
	}
1381
	}
1382
	ast_channel_unlock(chan);
1382
	ast_channel_unlock(chan);
1383

    
   
1383

   
1384
	return ret;
1384
	return ret;
1385
}
1385
}
1386

    
   
1386

   
1387
static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
1387
static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
1388
{
1388
{
1389
	struct ast_channel *chan = obj;
1389
	struct ast_channel *chan = obj;
1390
	char *uniqueid = arg;
1390
	char *uniqueid = arg;
1391
	size_t id_len = *(size_t *) data;
1391
	size_t id_len = *(size_t *) data;
1392
	int ret = CMP_MATCH;
1392
	int ret = CMP_MATCH;
1393

    
   
1393

   
1394
	if (ast_strlen_zero(uniqueid)) {
1394
	if (ast_strlen_zero(uniqueid)) {
1395
		ast_log(LOG_ERROR, "BUG! Must supply a uniqueid or partial uniqueid to match!\n");
1395
		ast_log(LOG_ERROR, "BUG! Must supply a uniqueid or partial uniqueid to match!\n");
1396
		return CMP_STOP;
1396
		return CMP_STOP;
1397
	}
1397
	}
1398

    
   
1398

   
1399
	ast_channel_lock(chan);
1399
	ast_channel_lock(chan);
1400
	if ((!id_len && strcasecmp(ast_channel_uniqueid(chan), uniqueid))
1400
	if ((!id_len && strcasecmp(ast_channel_uniqueid(chan), uniqueid))
1401
		|| (id_len && strncasecmp(ast_channel_uniqueid(chan), uniqueid, id_len))) {
1401
		|| (id_len && strncasecmp(ast_channel_uniqueid(chan), uniqueid, id_len))) {
1402
		ret = 0; /* uniqueid match failed, keep looking */
1402
		ret = 0; /* uniqueid match failed, keep looking */
1403
	}
1403
	}
1404
	ast_channel_unlock(chan);
1404
	ast_channel_unlock(chan);
1405

    
   
1405

   
1406
	return ret;
1406
	return ret;
1407
}
1407
}
1408

    
   
1408

   
1409
struct ast_channel_iterator {
1409
struct ast_channel_iterator {
1410
	/* storage for non-dynamically allocated iterator */
1410
	/* storage for non-dynamically allocated iterator */
1411
	struct ao2_iterator simple_iterator;
1411
	struct ao2_iterator simple_iterator;
1412
	/* pointer to the actual iterator (simple_iterator or a dynamically
1412
	/* pointer to the actual iterator (simple_iterator or a dynamically
1413
	 * allocated iterator)
1413
	 * allocated iterator)
1414
	 */
1414
	 */
1415
	struct ao2_iterator *active_iterator;
1415
	struct ao2_iterator *active_iterator;
1416
};
1416
};
1417

    
   
1417

   
1418
struct ast_channel_iterator *ast_channel_iterator_destroy(struct ast_channel_iterator *i)
1418
struct ast_channel_iterator *ast_channel_iterator_destroy(struct ast_channel_iterator *i)
1419
{
1419
{
1420
	ao2_iterator_destroy(i->active_iterator);
1420
	ao2_iterator_destroy(i->active_iterator);
1421
	ast_free(i);
1421
	ast_free(i);
1422

    
   
1422

   
1423
	return NULL;
1423
	return NULL;
1424
}
1424
}
1425

    
   
1425

   
1426
struct ast_channel_iterator *ast_channel_iterator_by_exten_new(const char *exten, const char *context)
1426
struct ast_channel_iterator *ast_channel_iterator_by_exten_new(const char *exten, const char *context)
1427
{
1427
{
1428
	struct ast_channel_iterator *i;
1428
	struct ast_channel_iterator *i;
1429
	char *l_exten = (char *) exten;
1429
	char *l_exten = (char *) exten;
1430
	char *l_context = (char *) context;
1430
	char *l_context = (char *) context;
1431

    
   
1431

   
1432
	if (!(i = ast_calloc(1, sizeof(*i)))) {
1432
	if (!(i = ast_calloc(1, sizeof(*i)))) {
1433
		return NULL;
1433
		return NULL;
1434
	}
1434
	}
1435

    
   
1435

   
1436
	i->active_iterator = (void *) ast_channel_callback(ast_channel_by_exten_cb,
1436
	i->active_iterator = (void *) ast_channel_callback(ast_channel_by_exten_cb,
1437
		l_context, l_exten, OBJ_MULTIPLE);
1437
		l_context, l_exten, OBJ_MULTIPLE);
1438
	if (!i->active_iterator) {
1438
	if (!i->active_iterator) {
1439
		ast_free(i);
1439
		ast_free(i);
1440
		return NULL;
1440
		return NULL;
1441
	}
1441
	}
1442

    
   
1442

   
1443
	return i;
1443
	return i;
1444
}
1444
}
1445

    
   
1445

   
1446
struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
1446
struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
1447
{
1447
{
1448
	struct ast_channel_iterator *i;
1448
	struct ast_channel_iterator *i;
1449
	char *l_name = (char *) name;
1449
	char *l_name = (char *) name;
1450

    
   
1450

   
1451
	if (!(i = ast_calloc(1, sizeof(*i)))) {
1451
	if (!(i = ast_calloc(1, sizeof(*i)))) {
1452
		return NULL;
1452
		return NULL;
1453
	}
1453
	}
1454

    
   
1454

   
1455
	i->active_iterator = (void *) ast_channel_callback(ast_channel_by_name_cb,
1455
	i->active_iterator = (void *) ast_channel_callback(ast_channel_by_name_cb,
1456
		l_name, &name_len,
1456
		l_name, &name_len,
1457
		OBJ_MULTIPLE | (name_len == 0 /* match the whole word, so optimize */ ? OBJ_KEY : 0));
1457
		OBJ_MULTIPLE | (name_len == 0 /* match the whole word, so optimize */ ? OBJ_KEY : 0));
1458
	if (!i->active_iterator) {
1458
	if (!i->active_iterator) {
1459
		ast_free(i);
1459
		ast_free(i);
1460
		return NULL;
1460
		return NULL;
1461
	}
1461
	}
1462

    
   
1462

   
1463
	return i;
1463
	return i;
1464
}
1464
}
1465

    
   
1465

   
1466
struct ast_channel_iterator *ast_channel_iterator_all_new(void)
1466
struct ast_channel_iterator *ast_channel_iterator_all_new(void)
1467
{
1467
{
1468
	struct ast_channel_iterator *i;
1468
	struct ast_channel_iterator *i;
1469

    
   
1469

   
1470
	if (!(i = ast_calloc(1, sizeof(*i)))) {
1470
	if (!(i = ast_calloc(1, sizeof(*i)))) {
1471
		return NULL;
1471
		return NULL;
1472
	}
1472
	}
1473

    
   
1473

   
1474
	i->simple_iterator = ao2_iterator_init(channels, 0);
1474
	i->simple_iterator = ao2_iterator_init(channels, 0);
1475
	i->active_iterator = &i->simple_iterator;
1475
	i->active_iterator = &i->simple_iterator;
1476

    
   
1476

   
1477
	return i;
1477
	return i;
1478
}
1478
}
1479

    
   
1479

   
1480
struct ast_channel *ast_channel_iterator_next(struct ast_channel_iterator *i)
1480
struct ast_channel *ast_channel_iterator_next(struct ast_channel_iterator *i)
1481
{
1481
{
1482
	return ao2_iterator_next(i->active_iterator);
1482
	return ao2_iterator_next(i->active_iterator);
1483
}
1483
}
1484

    
   
1484

   
1485
/* Legacy function, not currently used for lookups, but we need a cmp_fn */
1485
/* Legacy function, not currently used for lookups, but we need a cmp_fn */
1486
static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
1486
static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
1487
{
1487
{
1488
	ast_log(LOG_ERROR, "BUG! Should never be called!\n");
1488
	ast_log(LOG_ERROR, "BUG! Should never be called!\n");
1489
	return CMP_STOP;
1489
	return CMP_STOP;
1490
}
1490
}
1491

    
   
1491

   
1492
struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1492
struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1493
{
1493
{
1494
	struct ast_channel *chan;
1494
	struct ast_channel *chan;
1495
	char *l_name = (char *) name;
1495
	char *l_name = (char *) name;
1496

    
   
1496

   
1497
	chan = ast_channel_callback(ast_channel_by_name_cb, l_name, &name_len,
1497
	chan = ast_channel_callback(ast_channel_by_name_cb, l_name, &name_len,
1498
		(name_len == 0) /* optimize if it is a complete name match */ ? OBJ_KEY : 0);
1498
		(name_len == 0) /* optimize if it is a complete name match */ ? OBJ_KEY : 0);
1499
	if (chan) {
1499
	if (chan) {
1500
		return chan;
1500
		return chan;
1501
	}
1501
	}
1502

    
   
1502

   
1503
	if (ast_strlen_zero(l_name)) {
1503
	if (ast_strlen_zero(l_name)) {
1504
		/* We didn't have a name to search for so quit. */
1504
		/* We didn't have a name to search for so quit. */
1505
		return NULL;
1505
		return NULL;
1506
	}
1506
	}
1507

    
   
1507

   
1508
	/* Now try a search for uniqueid. */
1508
	/* Now try a search for uniqueid. */
1509
	return ast_channel_callback(ast_channel_by_uniqueid_cb, l_name, &name_len, 0);
1509
	return ast_channel_callback(ast_channel_by_uniqueid_cb, l_name, &name_len, 0);
1510
}
1510
}
1511

    
   
1511

   
1512
struct ast_channel *ast_channel_get_by_name(const char *name)
1512
struct ast_channel *ast_channel_get_by_name(const char *name)
1513
{
1513
{
1514
	return ast_channel_get_by_name_prefix(name, 0);
1514
	return ast_channel_get_by_name_prefix(name, 0);
1515
}
1515
}
1516

    
   
1516

   
1517
struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1517
struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1518
{
1518
{
1519
	char *l_exten = (char *) exten;
1519
	char *l_exten = (char *) exten;
1520
	char *l_context = (char *) context;
1520
	char *l_context = (char *) context;
1521

    
   
1521

   
1522
	return ast_channel_callback(ast_channel_by_exten_cb, l_context, l_exten, 0);
1522
	return ast_channel_callback(ast_channel_by_exten_cb, l_context, l_exten, 0);
1523
}
1523
}
1524

    
   
1524

   
1525
int ast_is_deferrable_frame(const struct ast_frame *frame)
1525
int ast_is_deferrable_frame(const struct ast_frame *frame)
1526
{
1526
{
1527
	/* Do not add a default entry in this switch statement.  Each new
1527
	/* Do not add a default entry in this switch statement.  Each new
1528
	 * frame type should be addressed directly as to whether it should
1528
	 * frame type should be addressed directly as to whether it should
1529
	 * be queued up or not.
1529
	 * be queued up or not.
1530
	 */
1530
	 */
1531
	switch (frame->frametype) {
1531
	switch (frame->frametype) {
1532
	case AST_FRAME_BRIDGE_ACTION:
1532
	case AST_FRAME_BRIDGE_ACTION:
1533
	case AST_FRAME_BRIDGE_ACTION_SYNC:
1533
	case AST_FRAME_BRIDGE_ACTION_SYNC:
1534
	case AST_FRAME_CONTROL:
1534
	case AST_FRAME_CONTROL:
1535
	case AST_FRAME_TEXT:
1535
	case AST_FRAME_TEXT:
1536
	case AST_FRAME_IMAGE:
1536
	case AST_FRAME_IMAGE:
1537
	case AST_FRAME_HTML:
1537
	case AST_FRAME_HTML:
1538
		return 1;
1538
		return 1;
1539

    
   
1539

   
1540
	case AST_FRAME_DTMF_END:
1540
	case AST_FRAME_DTMF_END:
1541
	case AST_FRAME_DTMF_BEGIN:
1541
	case AST_FRAME_DTMF_BEGIN:
1542
	case AST_FRAME_VOICE:
1542
	case AST_FRAME_VOICE:
1543
	case AST_FRAME_VIDEO:
1543
	case AST_FRAME_VIDEO:
1544
	case AST_FRAME_NULL:
1544
	case AST_FRAME_NULL:
1545
	case AST_FRAME_IAX:
1545
	case AST_FRAME_IAX:
1546
	case AST_FRAME_CNG:
1546
	case AST_FRAME_CNG:
1547
	case AST_FRAME_MODEM:
1547
	case AST_FRAME_MODEM:
1548
		return 0;
1548
		return 0;
1549
	}
1549
	}
1550
	return 0;
1550
	return 0;
1551
}
1551
}
1552

    
   
1552

   
1553
/*! \brief Wait, look for hangups and condition arg */
1553
/*! \brief Wait, look for hangups and condition arg */
1554
int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
1554
int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
1555
{
1555
{
1556
	struct ast_frame *f;
1556
	struct ast_frame *f;
1557
	struct ast_silence_generator *silgen = NULL;
1557
	struct ast_silence_generator *silgen = NULL;
1558
	int res = 0;
1558
	int res = 0;
1559
	struct timeval start;
1559
	struct timeval start;
1560
	int ms;
1560
	int ms;
1561
	AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1561
	AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1562

    
   
1562

   
1563
	AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1563
	AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1564

    
   
1564

   
1565
	/* If no other generator is present, start silencegen while waiting */
1565
	/* If no other generator is present, start silencegen while waiting */
1566
	if (ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
1566
	if (ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
1567
		silgen = ast_channel_start_silence_generator(chan);
1567
		silgen = ast_channel_start_silence_generator(chan);
1568
	}
1568
	}
1569

    
   
1569

   
1570
	start = ast_tvnow();
1570
	start = ast_tvnow();
1571
	while ((ms = ast_remaining_ms(start, timeout_ms))) {
1571
	while ((ms = ast_remaining_ms(start, timeout_ms))) {
1572
		struct ast_frame *dup_f = NULL;
1572
		struct ast_frame *dup_f = NULL;
1573

    
   
1573

   
1574
		if (cond && ((*cond)(data) == 0)) {
1574
		if (cond && ((*cond)(data) == 0)) {
1575
			break;
1575
			break;
1576
		}
1576
		}
1577
		ms = ast_waitfor(chan, ms);
1577
		ms = ast_waitfor(chan, ms);
1578
		if (ms < 0) {
1578
		if (ms < 0) {
1579
			res = -1;
1579
			res = -1;
1580
			break;
1580
			break;
1581
		}
1581
		}
1582
		if (ms > 0) {
1582
		if (ms > 0) {
1583
			f = ast_read(chan);
1583
			f = ast_read(chan);
1584
			if (!f) {
1584
			if (!f) {
1585
				res = -1;
1585
				res = -1;
1586
				break;
1586
				break;
1587
			}
1587
			}
1588

    
   
1588

   
1589
			if (!ast_is_deferrable_frame(f)) {
1589
			if (!ast_is_deferrable_frame(f)) {
1590
				ast_frfree(f);
1590
				ast_frfree(f);
1591
				continue;
1591
				continue;
1592
			}
1592
			}
1593

    
   
1593

   
1594
			if ((dup_f = ast_frisolate(f))) {
1594
			if ((dup_f = ast_frisolate(f))) {
1595
				if (dup_f != f) {
1595
				if (dup_f != f) {
1596
					ast_frfree(f);
1596
					ast_frfree(f);
1597
				}
1597
				}
1598
				AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1598
				AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1599
			}
1599
			}
1600
		}
1600
		}
1601
	}
1601
	}
1602

    
   
1602

   
1603
	/* stop silgen if present */
1603
	/* stop silgen if present */
1604
	if (silgen) {
1604
	if (silgen) {
1605
		ast_channel_stop_silence_generator(chan, silgen);
1605
		ast_channel_stop_silence_generator(chan, silgen);
1606
	}
1606
	}
1607

    
   
1607

   
1608
	/* We need to free all the deferred frames, but we only need to
1608
	/* We need to free all the deferred frames, but we only need to
1609
	 * queue the deferred frames if there was no error and no
1609
	 * queue the deferred frames if there was no error and no
1610
	 * hangup was received
1610
	 * hangup was received
1611
	 */
1611
	 */
1612
	ast_channel_lock(chan);
1612
	ast_channel_lock(chan);
1613
	while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1613
	while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1614
		if (!res) {
1614
		if (!res) {
1615
			ast_queue_frame_head(chan, f);
1615
			ast_queue_frame_head(chan, f);
1616
		}
1616
		}
1617
		ast_frfree(f);
1617
		ast_frfree(f);
1618
	}
1618
	}
1619
	ast_channel_unlock(chan);
1619
	ast_channel_unlock(chan);
1620

    
   
1620

   
1621
	return res;
1621
	return res;
1622
}
1622
}
1623

    
   
1623

   
1624
/*! \brief Wait, look for hangups */
1624
/*! \brief Wait, look for hangups */
1625
int ast_safe_sleep(struct ast_channel *chan, int ms)
1625
int ast_safe_sleep(struct ast_channel *chan, int ms)
1626
{
1626
{
1627
	return ast_safe_sleep_conditional(chan, ms, NULL, NULL);
1627
	return ast_safe_sleep_conditional(chan, ms, NULL, NULL);
1628
}
1628
}
1629

    
   
1629

   
1630
struct ast_channel *ast_channel_release(struct ast_channel *chan)
1630
struct ast_channel *ast_channel_release(struct ast_channel *chan)
1631
{
1631
{
1632
	/* Safe, even if already unlinked. */
1632
	/* Safe, even if already unlinked. */
1633
	ao2_unlink(channels, chan);
1633
	ao2_unlink(channels, chan);
1634
	return ast_channel_unref(chan);
1634
	return ast_channel_unref(chan);
1635
}
1635
}
1636

    
   
1636

   
1637
void ast_party_name_init(struct ast_party_name *init)
1637
void ast_party_name_init(struct ast_party_name *init)
1638
{
1638
{
1639
	init->str = NULL;
1639
	init->str = NULL;
1640
	init->char_set = AST_PARTY_CHAR_SET_ISO8859_1;
1640
	init->char_set = AST_PARTY_CHAR_SET_ISO8859_1;
1641
	init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1641
	init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1642
	init->valid = 0;
1642
	init->valid = 0;
1643
}
1643
}
1644

    
   
1644

   
1645
void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1645
void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1646
{
1646
{
1647
	if (dest == src) {
1647
	if (dest == src) {
1648
		/* Don't copy to self */
1648
		/* Don't copy to self */
1649
		return;
1649
		return;
1650
	}
1650
	}
1651

    
   
1651

   
1652
	ast_free(dest->str);
1652
	ast_free(dest->str);
1653
	dest->str = ast_strdup(src->str);
1653
	dest->str = ast_strdup(src->str);
1654
	dest->char_set = src->char_set;
1654
	dest->char_set = src->char_set;
1655
	dest->presentation = src->presentation;
1655
	dest->presentation = src->presentation;
1656
	dest->valid = src->valid;
1656
	dest->valid = src->valid;
1657
}
1657
}
1658

    
   
1658

   
1659
void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1659
void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1660
{
1660
{
1661
	init->str = NULL;
1661
	init->str = NULL;
1662
	init->char_set = guide->char_set;
1662
	init->char_set = guide->char_set;
1663
	init->presentation = guide->presentation;
1663
	init->presentation = guide->presentation;
1664
	init->valid = guide->valid;
1664
	init->valid = guide->valid;
1665
}
1665
}
1666

    
   
1666

   
1667
void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1667
void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1668
{
1668
{
1669
	if (dest == src) {
1669
	if (dest == src) {
1670
		/* Don't set to self */
1670
		/* Don't set to self */
1671
		return;
1671
		return;
1672
	}
1672
	}
1673

    
   
1673

   
1674
	if (src->str && src->str != dest->str) {
1674
	if (src->str && src->str != dest->str) {
1675
		ast_free(dest->str);
1675
		ast_free(dest->str);
1676
		dest->str = ast_strdup(src->str);
1676
		dest->str = ast_strdup(src->str);
1677
	}
1677
	}
1678

    
   
1678

   
1679
	dest->char_set = src->char_set;
1679
	dest->char_set = src->char_set;
1680
	dest->presentation = src->presentation;
1680
	dest->presentation = src->presentation;
1681
	dest->valid = src->valid;
1681
	dest->valid = src->valid;
1682
}
1682
}
1683

    
   
1683

   
1684
void ast_party_name_free(struct ast_party_name *doomed)
1684
void ast_party_name_free(struct ast_party_name *doomed)
1685
{
1685
{
1686
	ast_free(doomed->str);
1686
	ast_free(doomed->str);
1687
	doomed->str = NULL;
1687
	doomed->str = NULL;
1688
}
1688
}
1689

    
   
1689

   
1690
void ast_party_number_init(struct ast_party_number *init)
1690
void ast_party_number_init(struct ast_party_number *init)
1691
{
1691
{
1692
	init->str = NULL;
1692
	init->str = NULL;
1693
	init->plan = 0;/* Unknown */
1693
	init->plan = 0;/* Unknown */
1694
	init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1694
	init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1695
	init->valid = 0;
1695
	init->valid = 0;
1696
}
1696
}
1697

    
   
1697

   
1698
void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1698
void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1699
{
1699
{
1700
	if (dest == src) {
1700
	if (dest == src) {
1701
		/* Don't copy to self */
1701
		/* Don't copy to self */
1702
		return;
1702
		return;
1703
	}
1703
	}
1704

    
   
1704

   
1705
	ast_free(dest->str);
1705
	ast_free(dest->str);
1706
	dest->str = ast_strdup(src->str);
1706
	dest->str = ast_strdup(src->str);
1707
	dest->plan = src->plan;
1707
	dest->plan = src->plan;
1708
	dest->presentation = src->presentation;
1708
	dest->presentation = src->presentation;
1709
	dest->valid = src->valid;
1709
	dest->valid = src->valid;
1710
}
1710
}
1711

    
   
1711

   
1712
void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
1712
void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
1713
{
1713
{
1714
	init->str = NULL;
1714
	init->str = NULL;
1715
	init->plan = guide->plan;
1715
	init->plan = guide->plan;
1716
	init->presentation = guide->presentation;
1716
	init->presentation = guide->presentation;
1717
	init->valid = guide->valid;
1717
	init->valid = guide->valid;
1718
}
1718
}
1719

    
   
1719

   
1720
void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
1720
void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
1721
{
1721
{
1722
	if (dest == src) {
1722
	if (dest == src) {
1723
		/* Don't set to self */
1723
		/* Don't set to self */
1724
		return;
1724
		return;
1725
	}
1725
	}
1726

    
   
1726

   
1727
	if (src->str && src->str != dest->str) {
1727
	if (src->str && src->str != dest->str) {
1728
		ast_free(dest->str);
1728
		ast_free(dest->str);
1729
		dest->str = ast_strdup(src->str);
1729
		dest->str = ast_strdup(src->str);
1730
	}
1730
	}
1731

    
   
1731

   
1732
	dest->plan = src->plan;
1732
	dest->plan = src->plan;
1733
	dest->presentation = src->presentation;
1733
	dest->presentation = src->presentation;
1734
	dest->valid = src->valid;
1734
	dest->valid = src->valid;
1735
}
1735
}
1736

    
   
1736

   
1737
void ast_party_number_free(struct ast_party_number *doomed)
1737
void ast_party_number_free(struct ast_party_number *doomed)
1738
{
1738
{
1739
	ast_free(doomed->str);
1739
	ast_free(doomed->str);
1740
	doomed->str = NULL;
1740
	doomed->str = NULL;
1741
}
1741
}
1742

    
   
1742

   
1743
void ast_party_subaddress_init(struct ast_party_subaddress *init)
1743
void ast_party_subaddress_init(struct ast_party_subaddress *init)
1744
{
1744
{
1745
	init->str = NULL;
1745
	init->str = NULL;
1746
	init->type = 0;
1746
	init->type = 0;
1747
	init->odd_even_indicator = 0;
1747
	init->odd_even_indicator = 0;
1748
	init->valid = 0;
1748
	init->valid = 0;
1749
}
1749
}
1750

    
   
1750

   
1751
void ast_party_subaddress_copy(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1751
void ast_party_subaddress_copy(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1752
{
1752
{
1753
	if (dest == src) {
1753
	if (dest == src) {
1754
		/* Don't copy to self */
1754
		/* Don't copy to self */
1755
		return;
1755
		return;
1756
	}
1756
	}
1757

    
   
1757

   
1758
	ast_free(dest->str);
1758
	ast_free(dest->str);
1759
	dest->str = ast_strdup(src->str);
1759
	dest->str = ast_strdup(src->str);
1760
	dest->type = src->type;
1760
	dest->type = src->type;
1761
	dest->odd_even_indicator = src->odd_even_indicator;
1761
	dest->odd_even_indicator = src->odd_even_indicator;
1762
	dest->valid = src->valid;
1762
	dest->valid = src->valid;
1763
}
1763
}
1764

    
   
1764

   
1765
void ast_party_subaddress_set_init(struct ast_party_subaddress *init, const struct ast_party_subaddress *guide)
1765
void ast_party_subaddress_set_init(struct ast_party_subaddress *init, const struct ast_party_subaddress *guide)
1766
{
1766
{
1767
	init->str = NULL;
1767
	init->str = NULL;
1768
	init->type = guide->type;
1768
	init->type = guide->type;
1769
	init->odd_even_indicator = guide->odd_even_indicator;
1769
	init->odd_even_indicator = guide->odd_even_indicator;
1770
	init->valid = guide->valid;
1770
	init->valid = guide->valid;
1771
}
1771
}
1772

    
   
1772

   
1773
void ast_party_subaddress_set(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1773
void ast_party_subaddress_set(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1774
{
1774
{
1775
	if (dest == src) {
1775
	if (dest == src) {
1776
		/* Don't set to self */
1776
		/* Don't set to self */
1777
		return;
1777
		return;
1778
	}
1778
	}
1779

    
   
1779

   
1780
	if (src->str && src->str != dest->str) {
1780
	if (src->str && src->str != dest->str) {
1781
		ast_free(dest->str);
1781
		ast_free(dest->str);
1782
		dest->str = ast_strdup(src->str);
1782
		dest->str = ast_strdup(src->str);
1783
	}
1783
	}
1784

    
   
1784

   
1785
	dest->type = src->type;
1785
	dest->type = src->type;
1786
	dest->odd_even_indicator = src->odd_even_indicator;
1786
	dest->odd_even_indicator = src->odd_even_indicator;
1787
	dest->valid = src->valid;
1787
	dest->valid = src->valid;
1788
}
1788
}
1789

    
   
1789

   
1790
void ast_party_subaddress_free(struct ast_party_subaddress *doomed)
1790
void ast_party_subaddress_free(struct ast_party_subaddress *doomed)
1791
{
1791
{
1792
	ast_free(doomed->str);
1792
	ast_free(doomed->str);
1793
	doomed->str = NULL;
1793
	doomed->str = NULL;
1794
}
1794
}
1795

    
   
1795

   
1796
void ast_set_party_id_all(struct ast_set_party_id *update_id)
1796
void ast_set_party_id_all(struct ast_set_party_id *update_id)
1797
{
1797
{
1798
	update_id->name = 1;
1798
	update_id->name = 1;
1799
	update_id->number = 1;
1799
	update_id->number = 1;
1800
	update_id->subaddress = 1;
1800
	update_id->subaddress = 1;
1801
}
1801
}
1802

    
   
1802

   
1803
void ast_party_id_init(struct ast_party_id *init)
1803
void ast_party_id_init(struct ast_party_id *init)
1804
{
1804
{
1805
	ast_party_name_init(&init->name);
1805
	ast_party_name_init(&init->name);
1806
	ast_party_number_init(&init->number);
1806
	ast_party_number_init(&init->number);
1807
	ast_party_subaddress_init(&init->subaddress);
1807
	ast_party_subaddress_init(&init->subaddress);
1808
	init->tag = NULL;
1808
	init->tag = NULL;
1809
}
1809
}
1810

    
   
1810

   
1811
void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
1811
void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
1812
{
1812
{
1813
	if (dest == src) {
1813
	if (dest == src) {
1814
		/* Don't copy to self */
1814
		/* Don't copy to self */
1815
		return;
1815
		return;
1816
	}
1816
	}
1817

    
   
1817

   
1818
	ast_party_name_copy(&dest->name, &src->name);
1818
	ast_party_name_copy(&dest->name, &src->name);
1819
	ast_party_number_copy(&dest->number, &src->number);
1819
	ast_party_number_copy(&dest->number, &src->number);
1820
	ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1820
	ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1821

    
   
1821

   
1822
	ast_free(dest->tag);
1822
	ast_free(dest->tag);
1823
	dest->tag = ast_strdup(src->tag);
1823
	dest->tag = ast_strdup(src->tag);
1824
}
1824
}
1825

    
   
1825

   
1826
void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
1826
void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
1827
{
1827
{
1828
	ast_party_name_set_init(&init->name, &guide->name);
1828
	ast_party_name_set_init(&init->name, &guide->name);
1829
	ast_party_number_set_init(&init->number, &guide->number);
1829
	ast_party_number_set_init(&init->number, &guide->number);
1830
	ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1830
	ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1831
	init->tag = NULL;
1831
	init->tag = NULL;
1832
}
1832
}
1833

    
   
1833

   
1834
void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
1834
void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
1835
{
1835
{
1836
	if (dest == src) {
1836
	if (dest == src) {
1837
		/* Don't set to self */
1837
		/* Don't set to self */
1838
		return;
1838
		return;
1839
	}
1839
	}
1840

    
   
1840

   
1841
	if (!update || update->name) {
1841
	if (!update || update->name) {
1842
		ast_party_name_set(&dest->name, &src->name);
1842
		ast_party_name_set(&dest->name, &src->name);
1843
	}
1843
	}
1844
	if (!update || update->number) {
1844
	if (!update || update->number) {
1845
		ast_party_number_set(&dest->number, &src->number);
1845
		ast_party_number_set(&dest->number, &src->number);
1846
	}
1846
	}
1847
	if (!update || update->subaddress) {
1847
	if (!update || update->subaddress) {
1848
		ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
1848
		ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
1849
	}
1849
	}
1850

    
   
1850

   
1851
	if (src->tag && src->tag != dest->tag) {
1851
	if (src->tag && src->tag != dest->tag) {
1852
		ast_free(dest->tag);
1852
		ast_free(dest->tag);
1853
		dest->tag = ast_strdup(src->tag);
1853
		dest->tag = ast_strdup(src->tag);
1854
	}
1854
	}
1855
}
1855
}
1856

    
   
1856

   
1857
void ast_party_id_free(struct ast_party_id *doomed)
1857
void ast_party_id_free(struct ast_party_id *doomed)
1858
{
1858
{
1859
	ast_party_name_free(&doomed->name);
1859
	ast_party_name_free(&doomed->name);
1860
	ast_party_number_free(&doomed->number);
1860
	ast_party_number_free(&doomed->number);
1861
	ast_party_subaddress_free(&doomed->subaddress);
1861
	ast_party_subaddress_free(&doomed->subaddress);
1862

    
   
1862

   
1863
	ast_free(doomed->tag);
1863
	ast_free(doomed->tag);
1864
	doomed->tag = NULL;
1864
	doomed->tag = NULL;
1865
}
1865
}
1866

    
   
1866

   
1867
int ast_party_id_presentation(const struct ast_party_id *id)
1867
int ast_party_id_presentation(const struct ast_party_id *id)
1868
{
1868
{
1869
	int number_priority;
1869
	int number_priority;
1870
	int number_value;
1870
	int number_value;
1871
	int number_screening;
1871
	int number_screening;
1872
	int name_priority;
1872
	int name_priority;
1873
	int name_value;
1873
	int name_value;
1874

    
   
1874

   
1875
	/* Determine name presentation priority. */
1875
	/* Determine name presentation priority. */
1876
	if (!id->name.valid) {
1876
	if (!id->name.valid) {
1877
		name_value = AST_PRES_UNAVAILABLE;
1877
		name_value = AST_PRES_UNAVAILABLE;
1878
		name_priority = 3;
1878
		name_priority = 3;
1879
	} else {
1879
	} else {
1880
		name_value = id->name.presentation & AST_PRES_RESTRICTION;
1880
		name_value = id->name.presentation & AST_PRES_RESTRICTION;
1881
		switch (name_value) {
1881
		switch (name_value) {
1882
		case AST_PRES_RESTRICTED:
1882
		case AST_PRES_RESTRICTED:
1883
			name_priority = 0;
1883
			name_priority = 0;
1884
			break;
1884
			break;
1885
		case AST_PRES_ALLOWED:
1885
		case AST_PRES_ALLOWED:
1886
			name_priority = 1;
1886
			name_priority = 1;
1887
			break;
1887
			break;
1888
		case AST_PRES_UNAVAILABLE:
1888
		case AST_PRES_UNAVAILABLE:
1889
			name_priority = 2;
1889
			name_priority = 2;
1890
			break;
1890
			break;
1891
		default:
1891
		default:
1892
			name_value = AST_PRES_UNAVAILABLE;
1892
			name_value = AST_PRES_UNAVAILABLE;
1893
			name_priority = 3;
1893
			name_priority = 3;
1894
			break;
1894
			break;
1895
		}
1895
		}
1896
	}
1896
	}
1897

    
   
1897

   
1898
	/* Determine number presentation priority. */
1898
	/* Determine number presentation priority. */
1899
	if (!id->number.valid) {
1899
	if (!id->number.valid) {
1900
		number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1900
		number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1901
		number_value = AST_PRES_UNAVAILABLE;
1901
		number_value = AST_PRES_UNAVAILABLE;
1902
		number_priority = 3;
1902
		number_priority = 3;
1903
	} else {
1903
	} else {
1904
		number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
1904
		number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
1905
		number_value = id->number.presentation & AST_PRES_RESTRICTION;
1905
		number_value = id->number.presentation & AST_PRES_RESTRICTION;
1906
		switch (number_value) {
1906
		switch (number_value) {
1907
		case AST_PRES_RESTRICTED:
1907
		case AST_PRES_RESTRICTED:
1908
			number_priority = 0;
1908
			number_priority = 0;
1909
			break;
1909
			break;
1910
		case AST_PRES_ALLOWED:
1910
		case AST_PRES_ALLOWED:
1911
			number_priority = 1;
1911
			number_priority = 1;
1912
			break;
1912
			break;
1913
		case AST_PRES_UNAVAILABLE:
1913
		case AST_PRES_UNAVAILABLE:
1914
			number_priority = 2;
1914
			number_priority = 2;
1915
			break;
1915
			break;
1916
		default:
1916
		default:
1917
			number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1917
			number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1918
			number_value = AST_PRES_UNAVAILABLE;
1918
			number_value = AST_PRES_UNAVAILABLE;
1919
			number_priority = 3;
1919
			number_priority = 3;
1920
			break;
1920
			break;
1921
		}
1921
		}
1922
	}
1922
	}
1923

    
   
1923

   
1924
	/* Select the wining presentation value. */
1924
	/* Select the wining presentation value. */
1925
	if (name_priority < number_priority) {
1925
	if (name_priority < number_priority) {
1926
		number_value = name_value;
1926
		number_value = name_value;
1927
	}
1927
	}
1928
	if (number_value == AST_PRES_UNAVAILABLE) {
1928
	if (number_value == AST_PRES_UNAVAILABLE) {
1929
		return AST_PRES_NUMBER_NOT_AVAILABLE;
1929
		return AST_PRES_NUMBER_NOT_AVAILABLE;
1930
	}
1930
	}
1931

    
   
1931

   
1932
	return number_value | number_screening;
1932
	return number_value | number_screening;
1933
}
1933
}
1934

    
   
1934

   
1935
void ast_party_id_invalidate(struct ast_party_id *id)
1935
void ast_party_id_invalidate(struct ast_party_id *id)
1936
{
1936
{
1937
	id->name.valid = 0;
1937
	id->name.valid = 0;
1938
	id->number.valid = 0;
1938
	id->number.valid = 0;
1939
	id->subaddress.valid = 0;
1939
	id->subaddress.valid = 0;
1940
}
1940
}
1941

    
   
1941

   
1942
void ast_party_id_reset(struct ast_party_id *id)
1942
void ast_party_id_reset(struct ast_party_id *id)
1943
{
1943
{
1944
	ast_party_id_free(id);
1944
	ast_party_id_free(id);
1945
	ast_party_id_init(id);
1945
	ast_party_id_init(id);
1946
}
1946
}
1947

    
   
1947

   
1948
struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
1948
struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
1949
{
1949
{
1950
	struct ast_party_id merged;
1950
	struct ast_party_id merged;
1951

    
   
1951

   
1952
	merged = *base;
1952
	merged = *base;
1953
	if (overlay->name.valid) {
1953
	if (overlay->name.valid) {
1954
		merged.name = overlay->name;
1954
		merged.name = overlay->name;
1955
	}
1955
	}
1956
	if (overlay->number.valid) {
1956
	if (overlay->number.valid) {
1957
		merged.number = overlay->number;
1957
		merged.number = overlay->number;
1958
	}
1958
	}
1959
	if (overlay->subaddress.valid) {
1959
	if (overlay->subaddress.valid) {
1960
		merged.subaddress = overlay->subaddress;
1960
		merged.subaddress = overlay->subaddress;
1961
	}
1961
	}
1962
	/* Note the actual structure is returned and not a pointer to it! */
1962
	/* Note the actual structure is returned and not a pointer to it! */
1963
	return merged;
1963
	return merged;
1964
}
1964
}
1965

    
   
1965

   
1966
void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
1966
void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
1967
{
1967
{
1968
	struct ast_party_id merged;
1968
	struct ast_party_id merged;
1969

    
   
1969

   
1970
	merged = ast_party_id_merge(base, overlay);
1970
	merged = ast_party_id_merge(base, overlay);
1971
	ast_party_id_copy(dest, &merged);
1971
	ast_party_id_copy(dest, &merged);
1972
}
1972
}
1973

    
   
1973

   
1974
void ast_party_dialed_init(struct ast_party_dialed *init)
1974
void ast_party_dialed_init(struct ast_party_dialed *init)
1975
{
1975
{
1976
	init->number.str = NULL;
1976
	init->number.str = NULL;
1977
	init->number.plan = 0;/* Unknown */
1977
	init->number.plan = 0;/* Unknown */
1978
	ast_party_subaddress_init(&init->subaddress);
1978
	ast_party_subaddress_init(&init->subaddress);
1979
	init->transit_network_select = 0;
1979
	init->transit_network_select = 0;
1980
}
1980
}
1981

    
   
1981

   
1982
void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1982
void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1983
{
1983
{
1984
	if (dest == src) {
1984
	if (dest == src) {
1985
		/* Don't copy to self */
1985
		/* Don't copy to self */
1986
		return;
1986
		return;
1987
	}
1987
	}
1988

    
   
1988

   
1989
	ast_free(dest->number.str);
1989
	ast_free(dest->number.str);
1990
	dest->number.str = ast_strdup(src->number.str);
1990
	dest->number.str = ast_strdup(src->number.str);
1991
	dest->number.plan = src->number.plan;
1991
	dest->number.plan = src->number.plan;
1992
	ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1992
	ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1993
	dest->transit_network_select = src->transit_network_select;
1993
	dest->transit_network_select = src->transit_network_select;
1994
}
1994
}
1995

    
   
1995

   
1996
void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
1996
void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
1997
{
1997
{
1998
	init->number.str = NULL;
1998
	init->number.str = NULL;
1999
	init->number.plan = guide->number.plan;
1999
	init->number.plan = guide->number.plan;
2000
	ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
2000
	ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
2001
	init->transit_network_select = guide->transit_network_select;
2001
	init->transit_network_select = guide->transit_network_select;
2002
}
2002
}
2003

    
   
2003

   
2004
void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
2004
void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
2005
{
2005
{
2006
	if (src->number.str && src->number.str != dest->number.str) {
2006
	if (src->number.str && src->number.str != dest->number.str) {
2007
		ast_free(dest->number.str);
2007
		ast_free(dest->number.str);
2008
		dest->number.str = ast_strdup(src->number.str);
2008
		dest->number.str = ast_strdup(src->number.str);
2009
	}
2009
	}
2010
	dest->number.plan = src->number.plan;
2010
	dest->number.plan = src->number.plan;
2011

    
   
2011

   
2012
	ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
2012
	ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
2013

    
   
2013

   
2014
	dest->transit_network_select = src->transit_network_select;
2014
	dest->transit_network_select = src->transit_network_select;
2015
}
2015
}
2016

    
   
2016

   
2017
void ast_party_dialed_free(struct ast_party_dialed *doomed)
2017
void ast_party_dialed_free(struct ast_party_dialed *doomed)
2018
{
2018
{
2019
	ast_free(doomed->number.str);
2019
	ast_free(doomed->number.str);
2020
	doomed->number.str = NULL;
2020
	doomed->number.str = NULL;
2021
	ast_party_subaddress_free(&doomed->subaddress);
2021
	ast_party_subaddress_free(&doomed->subaddress);
2022
}
2022
}
2023

    
   
2023

   
2024
void ast_party_caller_init(struct ast_party_caller *init)
2024
void ast_party_caller_init(struct ast_party_caller *init)
2025
{
2025
{
2026
	ast_party_id_init(&init->id);
2026
	ast_party_id_init(&init->id);
2027
	ast_party_id_init(&init->ani);
2027
	ast_party_id_init(&init->ani);
2028
	ast_party_id_init(&init->priv);
2028
	ast_party_id_init(&init->priv);
2029
	init->ani2 = 0;
2029
	init->ani2 = 0;
2030
}
2030
}
2031

    
   
2031

   
2032
void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
2032
void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
2033
{
2033
{
2034
	if (dest == src) {
2034
	if (dest == src) {
2035
		/* Don't copy to self */
2035
		/* Don't copy to self */
2036
		return;
2036
		return;
2037
	}
2037
	}
2038

    
   
2038

   
2039
	ast_party_id_copy(&dest->id, &src->id);
2039
	ast_party_id_copy(&dest->id, &src->id);
2040
	ast_party_id_copy(&dest->ani, &src->ani);
2040
	ast_party_id_copy(&dest->ani, &src->ani);
2041
	ast_party_id_copy(&dest->priv, &src->priv);
2041
	ast_party_id_copy(&dest->priv, &src->priv);
2042
	dest->ani2 = src->ani2;
2042
	dest->ani2 = src->ani2;
2043
}
2043
}
2044

    
   
2044

   
2045
void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
2045
void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
2046
{
2046
{
2047
	ast_party_id_set_init(&init->id, &guide->id);
2047
	ast_party_id_set_init(&init->id, &guide->id);
2048
	ast_party_id_set_init(&init->ani, &guide->ani);
2048
	ast_party_id_set_init(&init->ani, &guide->ani);
2049
	ast_party_id_set_init(&init->priv, &guide->priv);
2049
	ast_party_id_set_init(&init->priv, &guide->priv);
2050
	init->ani2 = guide->ani2;
2050
	init->ani2 = guide->ani2;
2051
}
2051
}
2052

    
   
2052

   
2053
void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
2053
void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
2054
{
2054
{
2055
	ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2055
	ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2056
	ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2056
	ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2057
	ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2057
	ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2058
	dest->ani2 = src->ani2;
2058
	dest->ani2 = src->ani2;
2059
}
2059
}
2060

    
   
2060

   
2061
void ast_party_caller_free(struct ast_party_caller *doomed)
2061
void ast_party_caller_free(struct ast_party_caller *doomed)
2062
{
2062
{
2063
	ast_party_id_free(&doomed->id);
2063
	ast_party_id_free(&doomed->id);
2064
	ast_party_id_free(&doomed->ani);
2064
	ast_party_id_free(&doomed->ani);
2065
	ast_party_id_free(&doomed->priv);
2065
	ast_party_id_free(&doomed->priv);
2066
}
2066
}
2067

    
   
2067

   
2068
void ast_party_connected_line_init(struct ast_party_connected_line *init)
2068
void ast_party_connected_line_init(struct ast_party_connected_line *init)
2069
{
2069
{
2070
	ast_party_id_init(&init->id);
2070
	ast_party_id_init(&init->id);
2071
	ast_party_id_init(&init->ani);
2071
	ast_party_id_init(&init->ani);
2072
	ast_party_id_init(&init->priv);
2072
	ast_party_id_init(&init->priv);
2073
	init->ani2 = 0;
2073
	init->ani2 = 0;
2074
	init->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2074
	init->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2075
}
2075
}
2076

    
   
2076

   
2077
void ast_party_connected_line_copy(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
2077
void ast_party_connected_line_copy(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
2078
{
2078
{
2079
	if (dest == src) {
2079
	if (dest == src) {
2080
		/* Don't copy to self */
2080
		/* Don't copy to self */
2081
		return;
2081
		return;
2082
	}
2082
	}
2083

    
   
2083

   
2084
	ast_party_id_copy(&dest->id, &src->id);
2084
	ast_party_id_copy(&dest->id, &src->id);
2085
	ast_party_id_copy(&dest->ani, &src->ani);
2085
	ast_party_id_copy(&dest->ani, &src->ani);
2086
	ast_party_id_copy(&dest->priv, &src->priv);
2086
	ast_party_id_copy(&dest->priv, &src->priv);
2087
	dest->ani2 = src->ani2;
2087
	dest->ani2 = src->ani2;
2088
	dest->source = src->source;
2088
	dest->source = src->source;
2089
}
2089
}
2090

    
   
2090

   
2091
void ast_party_connected_line_set_init(struct ast_party_connected_line *init, const struct ast_party_connected_line *guide)
2091
void ast_party_connected_line_set_init(struct ast_party_connected_line *init, const struct ast_party_connected_line *guide)
2092
{
2092
{
2093
	ast_party_id_set_init(&init->id, &guide->id);
2093
	ast_party_id_set_init(&init->id, &guide->id);
2094
	ast_party_id_set_init(&init->ani, &guide->ani);
2094
	ast_party_id_set_init(&init->ani, &guide->ani);
2095
	ast_party_id_set_init(&init->priv, &guide->priv);
2095
	ast_party_id_set_init(&init->priv, &guide->priv);
2096
	init->ani2 = guide->ani2;
2096
	init->ani2 = guide->ani2;
2097
	init->source = guide->source;
2097
	init->source = guide->source;
2098
}
2098
}
2099

    
   
2099

   
2100
void ast_party_connected_line_set(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src, const struct ast_set_party_connected_line *update)
2100
void ast_party_connected_line_set(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src, const struct ast_set_party_connected_line *update)
2101
{
2101
{
2102
	ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2102
	ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2103
	ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2103
	ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2104
	ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2104
	ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2105
	dest->ani2 = src->ani2;
2105
	dest->ani2 = src->ani2;
2106
	dest->source = src->source;
2106
	dest->source = src->source;
2107
}
2107
}
2108

    
   
2108

   
2109
void ast_party_connected_line_collect_caller(struct ast_party_connected_line *connected, struct ast_party_caller *caller)
2109
void ast_party_connected_line_collect_caller(struct ast_party_connected_line *connected, struct ast_party_caller *caller)
2110
{
2110
{
2111
	connected->id = caller->id;
2111
	connected->id = caller->id;
2112
	connected->ani = caller->ani;
2112
	connected->ani = caller->ani;
2113
	connected->priv = caller->priv;
2113
	connected->priv = caller->priv;
2114
	connected->ani2 = caller->ani2;
2114
	connected->ani2 = caller->ani2;
2115
	connected->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2115
	connected->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2116
}
2116
}
2117

    
   
2117

   
2118
void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
2118
void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
2119
{
2119
{
2120
	ast_party_id_free(&doomed->id);
2120
	ast_party_id_free(&doomed->id);
2121
	ast_party_id_free(&doomed->ani);
2121
	ast_party_id_free(&doomed->ani);
2122
	ast_party_id_free(&doomed->priv);
2122
	ast_party_id_free(&doomed->priv);
2123
}
2123
}
2124

    
   
2124

   
2125
void ast_party_redirecting_reason_init(struct ast_party_redirecting_reason *init)
2125
void ast_party_redirecting_reason_init(struct ast_party_redirecting_reason *init)
2126
{
2126
{
2127
	init->str = NULL;
2127
	init->str = NULL;
2128
	init->code = AST_REDIRECTING_REASON_UNKNOWN;
2128
	init->code = AST_REDIRECTING_REASON_UNKNOWN;
2129
}
2129
}
2130

    
   
2130

   
2131
void ast_party_redirecting_reason_copy(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2131
void ast_party_redirecting_reason_copy(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2132
{
2132
{
2133
	if (dest == src) {
2133
	if (dest == src) {
2134
		return;
2134
		return;
2135
	}
2135
	}
2136

    
   
2136

   
2137
	ast_free(dest->str);
2137
	ast_free(dest->str);
2138
	dest->str = ast_strdup(src->str);
2138
	dest->str = ast_strdup(src->str);
2139
	dest->code = src->code;
2139
	dest->code = src->code;
2140
}
2140
}
2141

    
   
2141

   
2142
void ast_party_redirecting_reason_set_init(struct ast_party_redirecting_reason *init, const struct ast_party_redirecting_reason *guide)
2142
void ast_party_redirecting_reason_set_init(struct ast_party_redirecting_reason *init, const struct ast_party_redirecting_reason *guide)
2143
{
2143
{
2144
	init->str = NULL;
2144
	init->str = NULL;
2145
	init->code = guide->code;
2145
	init->code = guide->code;
2146
}
2146
}
2147

    
   
2147

   
2148
void ast_party_redirecting_reason_set(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2148
void ast_party_redirecting_reason_set(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2149
{
2149
{
2150
	if (dest == src) {
2150
	if (dest == src) {
2151
		return;
2151
		return;
2152
	}
2152
	}
2153

    
   
2153

   
2154
	if (src->str && src->str != dest->str) {
2154
	if (src->str && src->str != dest->str) {
2155
		ast_free(dest->str);
2155
		ast_free(dest->str);
2156
		dest->str = ast_strdup(src->str);
2156
		dest->str = ast_strdup(src->str);
2157
	}
2157
	}
2158

    
   
2158

   
2159
	dest->code = src->code;
2159
	dest->code = src->code;
2160
}
2160
}
2161

    
   
2161

   
2162
void ast_party_redirecting_reason_free(struct ast_party_redirecting_reason *doomed)
2162
void ast_party_redirecting_reason_free(struct ast_party_redirecting_reason *doomed)
2163
{
2163
{
2164
	ast_free(doomed->str);
2164
	ast_free(doomed->str);
2165
}
2165
}
2166

    
   
2166

   
2167

    
   
2167

   
2168
void ast_party_redirecting_init(struct ast_party_redirecting *init)
2168
void ast_party_redirecting_init(struct ast_party_redirecting *init)
2169
{
2169
{
2170
	ast_party_id_init(&init->orig);
2170
	ast_party_id_init(&init->orig);
2171
	ast_party_id_init(&init->from);
2171
	ast_party_id_init(&init->from);
2172
	ast_party_id_init(&init->to);
2172
	ast_party_id_init(&init->to);
2173
	ast_party_id_init(&init->priv_orig);
2173
	ast_party_id_init(&init->priv_orig);
2174
	ast_party_id_init(&init->priv_from);
2174
	ast_party_id_init(&init->priv_from);
2175
	ast_party_id_init(&init->priv_to);
2175
	ast_party_id_init(&init->priv_to);
2176
	ast_party_redirecting_reason_init(&init->reason);
2176
	ast_party_redirecting_reason_init(&init->reason);
2177
	ast_party_redirecting_reason_init(&init->orig_reason);
2177
	ast_party_redirecting_reason_init(&init->orig_reason);
2178
	init->count = 0;
2178
	init->count = 0;
2179
}
2179
}
2180

    
   
2180

   
2181
void ast_party_redirecting_copy(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src)
2181
void ast_party_redirecting_copy(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src)
2182
{
2182
{
2183
	if (dest == src) {
2183
	if (dest == src) {
2184
		/* Don't copy to self */
2184
		/* Don't copy to self */
2185
		return;
2185
		return;
2186
	}
2186
	}
2187

    
   
2187

   
2188
	ast_party_id_copy(&dest->orig, &src->orig);
2188
	ast_party_id_copy(&dest->orig, &src->orig);
2189
	ast_party_id_copy(&dest->from, &src->from);
2189
	ast_party_id_copy(&dest->from, &src->from);
2190
	ast_party_id_copy(&dest->to, &src->to);
2190
	ast_party_id_copy(&dest->to, &src->to);
2191
	ast_party_id_copy(&dest->priv_orig, &src->priv_orig);
2191
	ast_party_id_copy(&dest->priv_orig, &src->priv_orig);
2192
	ast_party_id_copy(&dest->priv_from, &src->priv_from);
2192
	ast_party_id_copy(&dest->priv_from, &src->priv_from);
2193
	ast_party_id_copy(&dest->priv_to, &src->priv_to);
2193
	ast_party_id_copy(&dest->priv_to, &src->priv_to);
2194
	ast_party_redirecting_reason_copy(&dest->reason, &src->reason);
2194
	ast_party_redirecting_reason_copy(&dest->reason, &src->reason);
2195
	ast_party_redirecting_reason_copy(&dest->orig_reason, &src->orig_reason);
2195
	ast_party_redirecting_reason_copy(&dest->orig_reason, &src->orig_reason);
2196
	dest->count = src->count;
2196
	dest->count = src->count;
2197
}
2197
}
2198

    
   
2198

   
2199
void ast_party_redirecting_set_init(struct ast_party_redirecting *init, const struct ast_party_redirecting *guide)
2199
void ast_party_redirecting_set_init(struct ast_party_redirecting *init, const struct ast_party_redirecting *guide)
2200
{
2200
{
2201
	ast_party_id_set_init(&init->orig, &guide->orig);
2201
	ast_party_id_set_init(&init->orig, &guide->orig);
2202
	ast_party_id_set_init(&init->from, &guide->from);
2202
	ast_party_id_set_init(&init->from, &guide->from);
2203
	ast_party_id_set_init(&init->to, &guide->to);
2203
	ast_party_id_set_init(&init->to, &guide->to);
2204
	ast_party_id_set_init(&init->priv_orig, &guide->priv_orig);
2204
	ast_party_id_set_init(&init->priv_orig, &guide->priv_orig);
2205
	ast_party_id_set_init(&init->priv_from, &guide->priv_from);
2205
	ast_party_id_set_init(&init->priv_from, &guide->priv_from);
2206
	ast_party_id_set_init(&init->priv_to, &guide->priv_to);
2206
	ast_party_id_set_init(&init->priv_to, &guide->priv_to);
2207
	ast_party_redirecting_reason_set_init(&init->reason, &guide->reason);
2207
	ast_party_redirecting_reason_set_init(&init->reason, &guide->reason);
2208
	ast_party_redirecting_reason_set_init(&init->orig_reason, &guide->orig_reason);
2208
	ast_party_redirecting_reason_set_init(&init->orig_reason, &guide->orig_reason);
2209
	init->count = guide->count;
2209
	init->count = guide->count;
2210
}
2210
}
2211

    
   
2211

   
2212
void ast_party_redirecting_set(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src, const struct ast_set_party_redirecting *update)
2212
void ast_party_redirecting_set(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src, const struct ast_set_party_redirecting *update)
2213
{
2213
{
2214
	ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
2214
	ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
2215
	ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2215
	ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2216
	ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2216
	ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2217
	ast_party_id_set(&dest->priv_orig, &src->priv_orig, update ? &update->priv_orig : NULL);
2217
	ast_party_id_set(&dest->priv_orig, &src->priv_orig, update ? &update->priv_orig : NULL);
2218
	ast_party_id_set(&dest->priv_from, &src->priv_from, update ? &update->priv_from : NULL);
2218
	ast_party_id_set(&dest->priv_from, &src->priv_from, update ? &update->priv_from : NULL);
2219
	ast_party_id_set(&dest->priv_to, &src->priv_to, update ? &update->priv_to : NULL);
2219
	ast_party_id_set(&dest->priv_to, &src->priv_to, update ? &update->priv_to : NULL);
2220
	ast_party_redirecting_reason_set(&dest->reason, &src->reason);
2220
	ast_party_redirecting_reason_set(&dest->reason, &src->reason);
2221
	ast_party_redirecting_reason_set(&dest->orig_reason, &src->orig_reason);
2221
	ast_party_redirecting_reason_set(&dest->orig_reason, &src->orig_reason);
2222
	dest->count = src->count;
2222
	dest->count = src->count;
2223
}
2223
}
2224

    
   
2224

   
2225
void ast_party_redirecting_free(struct ast_party_redirecting *doomed)
2225
void ast_party_redirecting_free(struct ast_party_redirecting *doomed)
2226
{
2226
{
2227
	ast_party_id_free(&doomed->orig);
2227
	ast_party_id_free(&doomed->orig);
2228
	ast_party_id_free(&doomed->from);
2228
	ast_party_id_free(&doomed->from);
2229
	ast_party_id_free(&doomed->to);
2229
	ast_party_id_free(&doomed->to);
2230
	ast_party_id_free(&doomed->priv_orig);
2230
	ast_party_id_free(&doomed->priv_orig);
2231
	ast_party_id_free(&doomed->priv_from);
2231
	ast_party_id_free(&doomed->priv_from);
2232
	ast_party_id_free(&doomed->priv_to);
2232
	ast_party_id_free(&doomed->priv_to);
2233
	ast_party_redirecting_reason_free(&doomed->reason);
2233
	ast_party_redirecting_reason_free(&doomed->reason);
2234
	ast_party_redirecting_reason_free(&doomed->orig_reason);
2234
	ast_party_redirecting_reason_free(&doomed->orig_reason);
2235
}
2235
}
2236

    
   
2236

   
2237
/*! \brief Free a channel structure */
2237
/*! \brief Free a channel structure */
2238
static void ast_channel_destructor(void *obj)
2238
static void ast_channel_destructor(void *obj)
2239
{
2239
{
2240
	struct ast_channel *chan = obj;
2240
	struct ast_channel *chan = obj;
2241
#ifdef HAVE_EPOLL
2241
#ifdef HAVE_EPOLL
2242
	int i;
2242
	int i;
2243
#endif
2243
#endif
2244
	struct ast_var_t *vardata;
2244
	struct ast_var_t *vardata;
2245
	struct ast_frame *f;
2245
	struct ast_frame *f;
2246
	struct varshead *headp;
2246
	struct varshead *headp;
2247
	struct ast_datastore *datastore;
2247
	struct ast_datastore *datastore;
2248
	char device_name[AST_CHANNEL_NAME];
2248
	char device_name[AST_CHANNEL_NAME];
2249
	struct ast_callid *callid;
2249
	struct ast_callid *callid;
2250

    
   
2250

   
2251
	/* Stop monitoring */
2251
	/* Stop monitoring */
2252
	if (ast_channel_monitor(chan)) {
2252
	if (ast_channel_monitor(chan)) {
2253
		ast_channel_monitor(chan)->stop(chan, 0);
2253
		ast_channel_monitor(chan)->stop(chan, 0);
2254
	}
2254
	}
2255

    
   
2255

   
2256
	/* If there is native format music-on-hold state, free it */
2256
	/* If there is native format music-on-hold state, free it */
2257
	if (ast_channel_music_state(chan)) {
2257
	if (ast_channel_music_state(chan)) {
2258
		ast_moh_cleanup(chan);
2258
		ast_moh_cleanup(chan);
2259
	}
2259
	}
2260

    
   
2260

   
2261
	ast_pbx_hangup_handler_destroy(chan);
2261
	ast_pbx_hangup_handler_destroy(chan);
2262

    
   
2262

   
2263
	/* Things that may possibly raise Stasis messages shouldn't occur after this point */
2263
	/* Things that may possibly raise Stasis messages shouldn't occur after this point */
2264
	ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEAD);
2264
	ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEAD);
2265

    
   
2265

   
2266
	if (ast_channel_internal_is_finalized(chan)) {
2266
	if (ast_channel_internal_is_finalized(chan)) {
2267
		/* A channel snapshot should not be in the process of being staged now. */
2267
		/* A channel snapshot should not be in the process of being staged now. */
2268
		ast_assert(!ast_test_flag(ast_channel_flags(chan), AST_FLAG_SNAPSHOT_STAGE));
2268
		ast_assert(!ast_test_flag(ast_channel_flags(chan), AST_FLAG_SNAPSHOT_STAGE));
2269

    
   
2269

   
2270
		ast_channel_lock(chan);
2270
		ast_channel_lock(chan);
2271
		ast_channel_publish_snapshot(chan);
2271
		ast_channel_publish_snapshot(chan);
2272
		ast_channel_unlock(chan);
2272
		ast_channel_unlock(chan);
2273
		publish_cache_clear(chan);
2273
		publish_cache_clear(chan);
2274
	}
2274
	}
2275

    
   
2275

   
2276
	ast_channel_lock(chan);
2276
	ast_channel_lock(chan);
2277

    
   
2277

   
2278
	/* Get rid of each of the data stores on the channel */
2278
	/* Get rid of each of the data stores on the channel */
2279
	while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
2279
	while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
2280
		/* Free the data store */
2280
		/* Free the data store */
2281
		ast_datastore_free(datastore);
2281
		ast_datastore_free(datastore);
2282

    
   
2282

   
2283
	/* While the channel is locked, take the reference to its callid while we tear down the call. */
2283
	/* While the channel is locked, take the reference to its callid while we tear down the call. */
2284
	callid = ast_channel_callid(chan);
2284
	callid = ast_channel_callid(chan);
2285
	ast_channel_callid_cleanup(chan);
2285
	ast_channel_callid_cleanup(chan);
2286

    
   
2286

   
2287
	ast_channel_unlock(chan);
2287
	ast_channel_unlock(chan);
2288

    
   
2288

   
2289
	/* Lock and unlock the channel just to be sure nobody has it locked still
2289
	/* Lock and unlock the channel just to be sure nobody has it locked still
2290
	   due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2290
	   due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2291
	ast_channel_lock(chan);
2291
	ast_channel_lock(chan);
2292
	ast_channel_unlock(chan);
2292
	ast_channel_unlock(chan);
2293

    
   
2293

   
2294
	if (ast_channel_tech_pvt(chan)) {
2294
	if (ast_channel_tech_pvt(chan)) {
2295
		ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
2295
		ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
2296
		ast_free(ast_channel_tech_pvt(chan));
2296
		ast_free(ast_channel_tech_pvt(chan));
2297
	}
2297
	}
2298

    
   
2298

   
2299
	if (ast_channel_sched(chan)) {
2299
	if (ast_channel_sched(chan)) {
2300
		ast_sched_context_destroy(ast_channel_sched(chan));
2300
		ast_sched_context_destroy(ast_channel_sched(chan));
2301
	}
2301
	}
2302

    
   
2302

   
2303
	if (ast_channel_internal_is_finalized(chan)) {
2303
	if (ast_channel_internal_is_finalized(chan)) {
2304
		char *dashptr;
2304
		char *dashptr;
2305

    
   
2305

   
2306
		ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
2306
		ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
2307
		if ((dashptr = strrchr(device_name, '-'))) {
2307
		if ((dashptr = strrchr(device_name, '-'))) {
2308
			*dashptr = '\0';
2308
			*dashptr = '\0';
2309
		}
2309
		}
2310
	} else {
2310
	} else {
2311
		device_name[0] = '\0';
2311
		device_name[0] = '\0';
2312
	}
2312
	}
2313

    
   
2313

   
2314
	/* Free translators */
2314
	/* Free translators */
2315
	if (ast_channel_readtrans(chan))
2315
	if (ast_channel_readtrans(chan))
2316
		ast_translator_free_path(ast_channel_readtrans(chan));
2316
		ast_translator_free_path(ast_channel_readtrans(chan));
2317
	if (ast_channel_writetrans(chan))
2317
	if (ast_channel_writetrans(chan))
2318
		ast_translator_free_path(ast_channel_writetrans(chan));
2318
		ast_translator_free_path(ast_channel_writetrans(chan));
2319
	if (ast_channel_pbx(chan))
2319
	if (ast_channel_pbx(chan))
2320
		ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
2320
		ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
2321

    
   
2321

   
2322
	ast_party_dialed_free(ast_channel_dialed(chan));
2322
	ast_party_dialed_free(ast_channel_dialed(chan));
2323
	ast_party_caller_free(ast_channel_caller(chan));
2323
	ast_party_caller_free(ast_channel_caller(chan));
2324
	ast_party_connected_line_free(ast_channel_connected(chan));
2324
	ast_party_connected_line_free(ast_channel_connected(chan));
2325
	ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2325
	ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2326
	ast_party_redirecting_free(ast_channel_redirecting(chan));
2326
	ast_party_redirecting_free(ast_channel_redirecting(chan));
2327

    
   
2327

   
2328
	/* Close pipes if appropriate */
2328
	/* Close pipes if appropriate */
2329
	ast_channel_internal_alertpipe_close(chan);
2329
	ast_channel_internal_alertpipe_close(chan);
2330
	if (ast_channel_timer(chan)) {
2330
	if (ast_channel_timer(chan)) {
2331
		ast_timer_close(ast_channel_timer(chan));
2331
		ast_timer_close(ast_channel_timer(chan));
2332
		ast_channel_timer_set(chan, NULL);
2332
		ast_channel_timer_set(chan, NULL);
2333
	}
2333
	}
2334
#ifdef HAVE_EPOLL
2334
#ifdef HAVE_EPOLL
2335
	for (i = 0; i < AST_MAX_FDS; i++) {
2335
	for (i = 0; i < AST_MAX_FDS; i++) {
2336
		if (ast_channel_internal_epfd_data(chan, i)) {
2336
		if (ast_channel_internal_epfd_data(chan, i)) {
2337
			ast_free(ast_channel_internal_epfd_data(chan, i));
2337
			ast_free(ast_channel_internal_epfd_data(chan, i));
2338
		}
2338
		}
2339
	}
2339
	}
2340
	close(ast_channel_epfd(chan));
2340
	close(ast_channel_epfd(chan));
2341
#endif
2341
#endif
2342
	while ((f = AST_LIST_REMOVE_HEAD(ast_channel_readq(chan), frame_list)))
2342
	while ((f = AST_LIST_REMOVE_HEAD(ast_channel_readq(chan), frame_list)))
2343
		ast_frfree(f);
2343
		ast_frfree(f);
2344

    
   
2344

   
2345
	/* loop over the variables list, freeing all data and deleting list items */
2345
	/* loop over the variables list, freeing all data and deleting list items */
2346
	/* no need to lock the list, as the channel is already locked */
2346
	/* no need to lock the list, as the channel is already locked */
2347
	headp = ast_channel_varshead(chan);
2347
	headp = ast_channel_varshead(chan);
2348
	while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2348
	while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2349
		ast_var_delete(vardata);
2349
		ast_var_delete(vardata);
2350

    
   
2350

   
2351
	ast_app_group_discard(chan);
2351
	ast_app_group_discard(chan);
2352

    
   
2352

   
2353
	/* Destroy the jitterbuffer */
2353
	/* Destroy the jitterbuffer */
2354
	ast_jb_destroy(chan);
2354
	ast_jb_destroy(chan);
2355

    
   
2355

   
2356
	if (ast_channel_cdr(chan)) {
2356
	if (ast_channel_cdr(chan)) {
2357
		ast_cdr_free(ast_channel_cdr(chan));
2357
		ast_cdr_free(ast_channel_cdr(chan));
2358
		ast_channel_cdr_set(chan, NULL);
2358
		ast_channel_cdr_set(chan, NULL);
2359
	}
2359
	}
2360

    
   
2360

   
2361
	if (ast_channel_zone(chan)) {
2361
	if (ast_channel_zone(chan)) {
2362
		ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
2362
		ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
2363
	}
2363
	}
2364

    
   
2364

   
2365
	ast_channel_internal_cleanup(chan);
2365
	ast_channel_internal_cleanup(chan);
2366

    
   
2366

   
2367
	if (device_name[0]) {
2367
	if (device_name[0]) {
2368
		/*
2368
		/*
2369
		 * We have a device name to notify of a new state.
2369
		 * We have a device name to notify of a new state.
2370
		 *
2370
		 *
2371
		 * Queue an unknown state, because, while we know that this particular
2371
		 * Queue an unknown state, because, while we know that this particular
2372
		 * instance is dead, we don't know the state of all other possible
2372
		 * instance is dead, we don't know the state of all other possible
2373
		 * instances.
2373
		 * instances.
2374
		 */
2374
		 */
2375
		ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE) ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), device_name);
2375
		ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE) ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), device_name);
2376
	}
2376
	}
2377

    
   
2377

   
2378
	ast_channel_nativeformats_set(chan, ast_format_cap_destroy(ast_channel_nativeformats(chan)));
2378
	ast_channel_nativeformats_set(chan, ast_format_cap_destroy(ast_channel_nativeformats(chan)));
2379
	if (callid) {
2379
	if (callid) {
2380
		ast_callid_unref(callid);
2380
		ast_callid_unref(callid);
2381
	}
2381
	}
2382

    
   
2382

   
2383
	ast_channel_named_callgroups_set(chan, NULL);
2383
	ast_channel_named_callgroups_set(chan, NULL);
2384
	ast_channel_named_pickupgroups_set(chan, NULL);
2384
	ast_channel_named_pickupgroups_set(chan, NULL);
2385

    
   
2385

   
2386
	ast_atomic_fetchadd_int(&chancount, -1);
2386
	ast_atomic_fetchadd_int(&chancount, -1);
2387
}
2387
}
2388

    
   
2388

   
2389
/*! \brief Free a dummy channel structure */
2389
/*! \brief Free a dummy channel structure */
2390
static void ast_dummy_channel_destructor(void *obj)
2390
static void ast_dummy_channel_destructor(void *obj)
2391
{
2391
{
2392
	struct ast_channel *chan = obj;
2392
	struct ast_channel *chan = obj;
2393
	struct ast_datastore *datastore;
2393
	struct ast_datastore *datastore;
2394
	struct ast_var_t *vardata;
2394
	struct ast_var_t *vardata;
2395
	struct varshead *headp;
2395
	struct varshead *headp;
2396

    
   
2396

   
2397
	ast_pbx_hangup_handler_destroy(chan);
2397
	ast_pbx_hangup_handler_destroy(chan);
2398

    
   
2398

   
2399
	/* Get rid of each of the data stores on the channel */
2399
	/* Get rid of each of the data stores on the channel */
2400
	while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
2400
	while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
2401
		/* Free the data store */
2401
		/* Free the data store */
2402
		ast_datastore_free(datastore);
2402
		ast_datastore_free(datastore);
2403
	}
2403
	}
2404

    
   
2404

   
2405
	ast_party_dialed_free(ast_channel_dialed(chan));
2405
	ast_party_dialed_free(ast_channel_dialed(chan));
2406
	ast_party_caller_free(ast_channel_caller(chan));
2406
	ast_party_caller_free(ast_channel_caller(chan));
2407
	ast_party_connected_line_free(ast_channel_connected(chan));
2407
	ast_party_connected_line_free(ast_channel_connected(chan));
2408
	ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2408
	ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2409
	ast_party_redirecting_free(ast_channel_redirecting(chan));
2409
	ast_party_redirecting_free(ast_channel_redirecting(chan));
2410

    
   
2410

   
2411
	/* loop over the variables list, freeing all data and deleting list items */
2411
	/* loop over the variables list, freeing all data and deleting list items */
2412
	/* no need to lock the list, as the channel is already locked */
2412
	/* no need to lock the list, as the channel is already locked */
2413
	headp = ast_channel_varshead(chan);
2413
	headp = ast_channel_varshead(chan);
2414
	while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2414
	while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2415
		ast_var_delete(vardata);
2415
		ast_var_delete(vardata);
2416

    
   
2416

   
2417
	if (ast_channel_cdr(chan)) {
2417
	if (ast_channel_cdr(chan)) {
2418
		ast_cdr_free(ast_channel_cdr(chan));
2418
		ast_cdr_free(ast_channel_cdr(chan));
2419
		ast_channel_cdr_set(chan, NULL);
2419
		ast_channel_cdr_set(chan, NULL);
2420
	}
2420
	}
2421

    
   
2421

   
2422
	ast_channel_internal_cleanup(chan);
2422
	ast_channel_internal_cleanup(chan);
2423
}
2423
}
2424

    
   
2424

   
2425
struct ast_datastore *ast_channel_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
2425
struct ast_datastore *ast_channel_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
2426
{
2426
{
2427
	return ast_datastore_alloc(info, uid);
2427
	return ast_datastore_alloc(info, uid);
2428
}
2428
}
2429

    
   
2429

   
2430
int ast_channel_datastore_free(struct ast_datastore *datastore)
2430
int ast_channel_datastore_free(struct ast_datastore *datastore)
2431
{
2431
{
2432
	return ast_datastore_free(datastore);
2432
	return ast_datastore_free(datastore);
2433
}
2433
}
2434

    
   
2434

   
2435
int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
2435
int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
2436
{
2436
{
2437
	struct ast_datastore *datastore = NULL, *datastore2;
2437
	struct ast_datastore *datastore = NULL, *datastore2;
2438

    
   
2438

   
2439
	AST_LIST_TRAVERSE(ast_channel_datastores(from), datastore, entry) {
2439
	AST_LIST_TRAVERSE(ast_channel_datastores(from), datastore, entry) {
2440
		if (datastore->inheritance > 0) {
2440
		if (datastore->inheritance > 0) {
2441
			datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2441
			datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2442
			if (datastore2) {
2442
			if (datastore2) {
2443
				datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2443
				datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2444
				datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2444
				datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2445
				AST_LIST_INSERT_TAIL(ast_channel_datastores(to), datastore2, entry);
2445
				AST_LIST_INSERT_TAIL(ast_channel_datastores(to), datastore2, entry);
2446
			}
2446
			}
2447
		}
2447
		}
2448
	}
2448
	}
2449
	return 0;
2449
	return 0;
2450
}
2450
}
2451

    
   
2451

   
2452
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2452
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2453
{
2453
{
2454
	int res = 0;
2454
	int res = 0;
2455

    
   
2455

   
2456
	AST_LIST_INSERT_HEAD(ast_channel_datastores(chan), datastore, entry);
2456
	AST_LIST_INSERT_HEAD(ast_channel_datastores(chan), datastore, entry);
2457

    
   
2457

   
2458
	return res;
2458
	return res;
2459
}
2459
}
2460

    
   
2460

   
2461
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2461
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2462
{
2462
{
2463
	return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
2463
	return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
2464
}
2464
}
2465

    
   
2465

   
2466
struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2466
struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2467
{
2467
{
2468
	struct ast_datastore *datastore = NULL;
2468
	struct ast_datastore *datastore = NULL;
2469

    
   
2469

   
2470
	if (info == NULL)
2470
	if (info == NULL)
2471
		return NULL;
2471
		return NULL;
2472

    
   
2472

   
2473
	AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) {
2473
	AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) {
2474
		if (datastore->info != info) {
2474
		if (datastore->info != info) {
2475
			continue;
2475
			continue;
2476
		}
2476
		}
2477

    
   
2477

   
2478
		if (uid == NULL) {
2478
		if (uid == NULL) {
2479
			/* matched by type only */
2479
			/* matched by type only */
2480
			break;
2480
			break;
2481
		}
2481
		}
2482

    
   
2482

   
2483
		if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2483
		if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2484
			/* Matched by type AND uid */
2484
			/* Matched by type AND uid */
2485
			break;
2485
			break;
2486
		}
2486
		}
2487
	}
2487
	}
2488

    
   
2488

   
2489
	return datastore;
2489
	return datastore;
2490
}
2490
}
2491

    
   
2491

   
2492
/*! Set the file descriptor on the channel */
2492
/*! Set the file descriptor on the channel */
2493
void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2493
void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2494
{
2494
{
2495
#ifdef HAVE_EPOLL
2495
#ifdef HAVE_EPOLL
2496
	struct epoll_event ev;
2496
	struct epoll_event ev;
2497
	struct ast_epoll_data *aed = NULL;
2497
	struct ast_epoll_data *aed = NULL;
2498

    
   
2498

   
2499
	if (ast_channel_fd_isset(chan, which)) {
2499
	if (ast_channel_fd_isset(chan, which)) {
2500
		epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_DEL, ast_channel_fd(chan, which), &ev);
2500
		epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_DEL, ast_channel_fd(chan, which), &ev);
2501
		aed = ast_channel_internal_epfd_data(chan, which);
2501
		aed = ast_channel_internal_epfd_data(chan, which);
2502
	}
2502
	}
2503

    
   
2503

   
2504
	/* If this new fd is valid, add it to the epoll */
2504
	/* If this new fd is valid, add it to the epoll */
2505
	if (fd > -1) {
2505
	if (fd > -1) {
2506
		if (!aed && (!(aed = ast_calloc(1, sizeof(*aed)))))
2506
		if (!aed && (!(aed = ast_calloc(1, sizeof(*aed)))))
2507
			return;
2507
			return;
2508

    
   
2508

   
2509
		ast_channel_internal_epfd_data_set(chan, which, aed);
2509
		ast_channel_internal_epfd_data_set(chan, which, aed);
2510
		aed->chan = chan;
2510
		aed->chan = chan;
2511
		aed->which = which;
2511
		aed->which = which;
2512

    
   
2512

   
2513
		ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2513
		ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2514
		ev.data.ptr = aed;
2514
		ev.data.ptr = aed;
2515
		epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_ADD, fd, &ev);
2515
		epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_ADD, fd, &ev);
2516
	} else if (aed) {
2516
	} else if (aed) {
2517
		/* We don't have to keep around this epoll data structure now */
2517
		/* We don't have to keep around this epoll data structure now */
2518
		ast_free(aed);
2518
		ast_free(aed);
2519
		ast_channel_epfd_data_set(chan, which, NULL);
2519
		ast_channel_epfd_data_set(chan, which, NULL);
2520
	}
2520
	}
2521
#endif
2521
#endif
2522
	ast_channel_internal_fd_set(chan, which, fd);
2522
	ast_channel_internal_fd_set(chan, which, fd);
2523
	return;
2523
	return;
2524
}
2524
}
2525

    
   
2525

   
2526
/*! Add a channel to an optimized waitfor */
2526
/*! Add a channel to an optimized waitfor */
2527
void ast_poll_channel_add(struct ast_channel *chan0, struct ast_channel *chan1)
2527
void ast_poll_channel_add(struct ast_channel *chan0, struct ast_channel *chan1)
2528
{
2528
{
2529
#ifdef HAVE_EPOLL
2529
#ifdef HAVE_EPOLL
2530
	struct epoll_event ev;
2530
	struct epoll_event ev;
2531
	int i = 0;
2531
	int i = 0;
2532

    
   
2532

   
2533
	if (ast_channel_epfd(chan0) == -1)
2533
	if (ast_channel_epfd(chan0) == -1)
2534
		return;
2534
		return;
2535

    
   
2535

   
2536
	/* Iterate through the file descriptors on chan1, adding them to chan0 */
2536
	/* Iterate through the file descriptors on chan1, adding them to chan0 */
2537
	for (i = 0; i < AST_MAX_FDS; i++) {
2537
	for (i = 0; i < AST_MAX_FDS; i++) {
2538
		if (!ast_channel_fd_isset(chan1, i)) {
2538
		if (!ast_channel_fd_isset(chan1, i)) {
2539
			continue;
2539
			continue;
2540
		}
2540
		}
2541
		ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2541
		ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2542
		ev.data.ptr = ast_channel_internal_epfd_data(chan1, i);
2542
		ev.data.ptr = ast_channel_internal_epfd_data(chan1, i);
2543
		epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_ADD, ast_channel_fd(chan1, i), &ev);
2543
		epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_ADD, ast_channel_fd(chan1, i), &ev);
2544
	}
2544
	}
2545

    
   
2545

   
2546
#endif
2546
#endif
2547
	return;
2547
	return;
2548
}
2548
}
2549

    
   
2549

   
2550
/*! Delete a channel from an optimized waitfor */
2550
/*! Delete a channel from an optimized waitfor */
2551
void ast_poll_channel_del(struct ast_channel *chan0, struct ast_channel *chan1)
2551
void ast_poll_channel_del(struct ast_channel *chan0, struct ast_channel *chan1)
2552
{
2552
{
2553
#ifdef HAVE_EPOLL
2553
#ifdef HAVE_EPOLL
2554
	struct epoll_event ev;
2554
	struct epoll_event ev;
2555
	int i = 0;
2555
	int i = 0;
2556

    
   
2556

   
2557
	if (ast_channel_epfd(chan0) == -1)
2557
	if (ast_channel_epfd(chan0) == -1)
2558
		return;
2558
		return;
2559

    
   
2559

   
2560
	for (i = 0; i < AST_MAX_FDS; i++) {
2560
	for (i = 0; i < AST_MAX_FDS; i++) {
2561
		if (!ast_channel_fd_isset(chan1, i)) {
2561
		if (!ast_channel_fd_isset(chan1, i)) {
2562
			continue;
2562
			continue;
2563
		}
2563
		}
2564
		epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_DEL, ast_channel_fd(chan1, i), &ev);
2564
		epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_DEL, ast_channel_fd(chan1, i), &ev);
2565
	}
2565
	}
2566

    
   
2566

   
2567
#endif
2567
#endif
2568
	return;
2568
	return;
2569
}
2569
}
2570

    
   
2570

   
2571
void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
2571
void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
2572
{
2572
{
2573
	ast_channel_lock(chan);
2573
	ast_channel_lock(chan);
2574

    
   
2574

   
2575
	ast_channel_softhangup_internal_flag_clear(chan, flag);
2575
	ast_channel_softhangup_internal_flag_clear(chan, flag);
2576

    
   
2576

   
2577
	if (!ast_channel_softhangup_internal_flag(chan)) {
2577
	if (!ast_channel_softhangup_internal_flag(chan)) {
2578
		struct ast_frame *fr;
2578
		struct ast_frame *fr;
2579

    
   
2579

   
2580
		/* If we have completely cleared the softhangup flag,
2580
		/* If we have completely cleared the softhangup flag,
2581
		 * then we need to fully abort the hangup process.  This requires
2581
		 * then we need to fully abort the hangup process.  This requires
2582
		 * pulling the END_OF_Q frame out of the channel frame queue if it
2582
		 * pulling the END_OF_Q frame out of the channel frame queue if it
2583
		 * still happens to be there. */
2583
		 * still happens to be there. */
2584

    
   
2584

   
2585
		fr = AST_LIST_LAST(ast_channel_readq(chan));
2585
		fr = AST_LIST_LAST(ast_channel_readq(chan));
2586
		if (fr && fr->frametype == AST_FRAME_CONTROL &&
2586
		if (fr && fr->frametype == AST_FRAME_CONTROL &&
2587
				fr->subclass.integer == AST_CONTROL_END_OF_Q) {
2587
				fr->subclass.integer == AST_CONTROL_END_OF_Q) {
2588
			AST_LIST_REMOVE(ast_channel_readq(chan), fr, frame_list);
2588
			AST_LIST_REMOVE(ast_channel_readq(chan), fr, frame_list);
2589
			ast_frfree(fr);
2589
			ast_frfree(fr);
2590
		}
2590
		}
2591
	}
2591
	}
2592

    
   
2592

   
2593
	ast_channel_unlock(chan);
2593
	ast_channel_unlock(chan);
2594
}
2594
}
2595

    
   
2595

   
2596
/*! \brief Softly hangup a channel, don't lock */
2596
/*! \brief Softly hangup a channel, don't lock */
2597
int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2597
int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2598
{
2598
{
2599
	ast_debug(1, "Soft-Hanging up channel '%s'\n", ast_channel_name(chan));
2599
	ast_debug(1, "Soft-Hanging up channel '%s'\n", ast_channel_name(chan));
2600
	/* Inform channel driver that we need to be hung up, if it cares */
2600
	/* Inform channel driver that we need to be hung up, if it cares */
2601
	ast_channel_softhangup_internal_flag_add(chan, cause);
2601
	ast_channel_softhangup_internal_flag_add(chan, cause);
2602
	ast_queue_frame(chan, &ast_null_frame);
2602
	ast_queue_frame(chan, &ast_null_frame);
2603
	/* Interrupt any poll call or such */
2603
	/* Interrupt any poll call or such */
2604
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING))
2604
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING))
2605
		pthread_kill(ast_channel_blocker(chan), SIGURG);
2605
		pthread_kill(ast_channel_blocker(chan), SIGURG);
2606
	return 0;
2606
	return 0;
2607
}
2607
}
2608

    
   
2608

   
2609
/*! \brief Softly hangup a channel, lock */
2609
/*! \brief Softly hangup a channel, lock */
2610
int ast_softhangup(struct ast_channel *chan, int cause)
2610
int ast_softhangup(struct ast_channel *chan, int cause)
2611
{
2611
{
2612
	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2612
	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2613
	int res;
2613
	int res;
2614

    
   
2614

   
2615
	ast_channel_lock(chan);
2615
	ast_channel_lock(chan);
2616
	res = ast_softhangup_nolock(chan, cause);
2616
	res = ast_softhangup_nolock(chan, cause);
2617
	blob = ast_json_pack("{s: i, s: b}",
2617
	blob = ast_json_pack("{s: i, s: b}",
2618
			     "cause", cause,
2618
			     "cause", cause,
2619
			     "soft", 1);
2619
			     "soft", 1);
2620
	ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
2620
	ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
2621
	ast_channel_unlock(chan);
2621
	ast_channel_unlock(chan);
2622

    
   
2622

   
2623
	return res;
2623
	return res;
2624
}
2624
}
2625

    
   
2625

   
2626
static void free_translation(struct ast_channel *clonechan)
2626
static void free_translation(struct ast_channel *clonechan)
2627
{
2627
{
2628
	if (ast_channel_writetrans(clonechan))
2628
	if (ast_channel_writetrans(clonechan))
2629
		ast_translator_free_path(ast_channel_writetrans(clonechan));
2629
		ast_translator_free_path(ast_channel_writetrans(clonechan));
2630
	if (ast_channel_readtrans(clonechan))
2630
	if (ast_channel_readtrans(clonechan))
2631
		ast_translator_free_path(ast_channel_readtrans(clonechan));
2631
		ast_translator_free_path(ast_channel_readtrans(clonechan));
2632
	ast_channel_writetrans_set(clonechan, NULL);
2632
	ast_channel_writetrans_set(clonechan, NULL);
2633
	ast_channel_readtrans_set(clonechan, NULL);
2633
	ast_channel_readtrans_set(clonechan, NULL);
2634
	if (ast_format_cap_is_empty(ast_channel_nativeformats(clonechan))) {
2634
	if (ast_format_cap_is_empty(ast_channel_nativeformats(clonechan))) {
2635
		ast_format_clear(ast_channel_rawwriteformat(clonechan));
2635
		ast_format_clear(ast_channel_rawwriteformat(clonechan));
2636
		ast_format_clear(ast_channel_rawreadformat(clonechan));
2636
		ast_format_clear(ast_channel_rawreadformat(clonechan));
2637
	} else {
2637
	} else {
2638
		struct ast_format tmpfmt;
2638
		struct ast_format tmpfmt;
2639
		ast_best_codec(ast_channel_nativeformats(clonechan), &tmpfmt);
2639
		ast_best_codec(ast_channel_nativeformats(clonechan), &tmpfmt);
2640
		ast_format_copy(ast_channel_rawwriteformat(clonechan), &tmpfmt);
2640
		ast_format_copy(ast_channel_rawwriteformat(clonechan), &tmpfmt);
2641
		ast_format_copy(ast_channel_rawreadformat(clonechan), &tmpfmt);
2641
		ast_format_copy(ast_channel_rawreadformat(clonechan), &tmpfmt);
2642
	}
2642
	}
2643
}
2643
}
2644

    
   
2644

   
2645
void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2645
void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2646
{
2646
{
2647
	RAII_VAR(struct ast_channel *, bridge, ast_channel_bridge_peer(chan), ast_channel_cleanup);
2647
	RAII_VAR(struct ast_channel *, bridge, ast_channel_bridge_peer(chan), ast_channel_cleanup);
2648

    
   
2648

   
2649
	ast_channel_lock(chan);
2649
	ast_channel_lock(chan);
2650
	if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
2650
	if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
2651
		ast_channel_hangupsource_set(chan, source);
2651
		ast_channel_hangupsource_set(chan, source);
2652
	}
2652
	}
2653
	ast_channel_unlock(chan);
2653
	ast_channel_unlock(chan);
2654

    
   
2654

   
2655
	if (bridge) {
2655
	if (bridge) {
2656
		ast_channel_lock(bridge);
2656
		ast_channel_lock(bridge);
2657
		if (force || ast_strlen_zero(ast_channel_hangupsource(bridge))) {
2657
		if (force || ast_strlen_zero(ast_channel_hangupsource(bridge))) {
2658
			ast_channel_hangupsource_set(bridge, source);
2658
			ast_channel_hangupsource_set(bridge, source);
2659
		}
2659
		}
2660
		ast_channel_unlock(bridge);
2660
		ast_channel_unlock(bridge);
2661
	}
2661
	}
2662
}
2662
}
2663

    
   
2663

   
2664
int ast_channel_has_audio_frame_or_monitor(struct ast_channel *chan)
2664
int ast_channel_has_audio_frame_or_monitor(struct ast_channel *chan)
2665
{
2665
{
2666
	return ast_channel_monitor(chan)
2666
	return ast_channel_monitor(chan)
2667
		|| !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2667
		|| !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2668
		|| !ast_framehook_list_contains_no_active(ast_channel_framehooks(chan));
2668
		|| !ast_framehook_list_contains_no_active(ast_channel_framehooks(chan));
2669
}
2669
}
2670

    
   
2670

   
2671
int ast_channel_has_hook_requiring_audio(struct ast_channel *chan)
2671
int ast_channel_has_hook_requiring_audio(struct ast_channel *chan)
2672
{
2672
{
2673
	return ast_channel_monitor(chan)
2673
	return ast_channel_monitor(chan)
2674
		|| !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2674
		|| !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2675
		|| !ast_framehook_list_contains_no_active_of_type(ast_channel_framehooks(chan), AST_FRAME_VOICE);
2675
		|| !ast_framehook_list_contains_no_active_of_type(ast_channel_framehooks(chan), AST_FRAME_VOICE);
2676
}
2676
}
2677

    
   
2677

   
2678
static void destroy_hooks(struct ast_channel *chan)
2678
static void destroy_hooks(struct ast_channel *chan)
2679
{
2679
{
2680
	if (ast_channel_audiohooks(chan)) {
2680
	if (ast_channel_audiohooks(chan)) {
2681
		ast_audiohook_detach_list(ast_channel_audiohooks(chan));
2681
		ast_audiohook_detach_list(ast_channel_audiohooks(chan));
2682
		ast_channel_audiohooks_set(chan, NULL);
2682
		ast_channel_audiohooks_set(chan, NULL);
2683
	}
2683
	}
2684

    
   
2684

   
2685
	ast_framehook_list_destroy(chan);
2685
	ast_framehook_list_destroy(chan);
2686
}
2686
}
2687

    
   
2687

   
2688
/*! \brief Hangup a channel */
2688
/*! \brief Hangup a channel */
2689
void ast_hangup(struct ast_channel *chan)
2689
void ast_hangup(struct ast_channel *chan)
2690
{
2690
{
2691
	/* Be NULL safe for RAII_VAR() usage. */
2691
	/* Be NULL safe for RAII_VAR() usage. */
2692
	if (!chan) {
2692
	if (!chan) {
2693
		return;
2693
		return;
2694
	}
2694
	}
2695

    
   
2695

   
2696
	ast_autoservice_stop(chan);
2696
	ast_autoservice_stop(chan);
2697

    
   
2697

   
2698
	ast_channel_lock(chan);
2698
	ast_channel_lock(chan);
2699

    
   
2699

   
2700
	while (ast_channel_masq(chan) || ast_channel_masqr(chan))  {
2700
	while (ast_channel_masq(chan) || ast_channel_masqr(chan))  {
2701
		CHANNEL_DEADLOCK_AVOIDANCE(chan);
2701
		CHANNEL_DEADLOCK_AVOIDANCE(chan);
2702
	}
2702
	}
2703

    
   
2703

   
2704
	/* Mark as a zombie so a masquerade cannot be setup on this channel. */
2704
	/* Mark as a zombie so a masquerade cannot be setup on this channel. */
2705
	ast_set_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE);
2705
	ast_set_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE);
2706

    
   
2706

   
2707
	ast_channel_unlock(chan);
2707
	ast_channel_unlock(chan);
2708

    
   
2708

   
2709
	/*
2709
	/*
2710
	 * XXX if running the hangup handlers here causes problems
2710
	 * XXX if running the hangup handlers here causes problems
2711
	 * because the handlers take too long to execute, we could move
2711
	 * because the handlers take too long to execute, we could move
2712
	 * the meat of this function into another thread.  A thread
2712
	 * the meat of this function into another thread.  A thread
2713
	 * where channels go to die.
2713
	 * where channels go to die.
2714
	 *
2714
	 *
2715
	 * If this is done, ast_autoservice_chan_hangup_peer() will no
2715
	 * If this is done, ast_autoservice_chan_hangup_peer() will no
2716
	 * longer be needed.
2716
	 * longer be needed.
2717
	 */
2717
	 */
2718
	ast_pbx_hangup_handler_run(chan);
2718
	ast_pbx_hangup_handler_run(chan);
2719
	ao2_unlink(channels, chan);
2719
	ao2_unlink(channels, chan);
2720
	ast_channel_lock(chan);
2720
	ast_channel_lock(chan);
2721

    
   
2721

   
2722
	destroy_hooks(chan);
2722
	destroy_hooks(chan);
2723

    
   
2723

   
2724
	free_translation(chan);
2724
	free_translation(chan);
2725
	/* Close audio stream */
2725
	/* Close audio stream */
2726
	if (ast_channel_stream(chan)) {
2726
	if (ast_channel_stream(chan)) {
2727
		ast_closestream(ast_channel_stream(chan));
2727
		ast_closestream(ast_channel_stream(chan));
2728
		ast_channel_stream_set(chan, NULL);
2728
		ast_channel_stream_set(chan, NULL);
2729
	}
2729
	}
2730
	/* Close video stream */
2730
	/* Close video stream */
2731
	if (ast_channel_vstream(chan)) {
2731
	if (ast_channel_vstream(chan)) {
2732
		ast_closestream(ast_channel_vstream(chan));
2732
		ast_closestream(ast_channel_vstream(chan));
2733
		ast_channel_vstream_set(chan, NULL);
2733
		ast_channel_vstream_set(chan, NULL);
2734
	}
2734
	}
2735
	if (ast_channel_sched(chan)) {
2735
	if (ast_channel_sched(chan)) {
2736
		ast_sched_context_destroy(ast_channel_sched(chan));
2736
		ast_sched_context_destroy(ast_channel_sched(chan));
2737
		ast_channel_sched_set(chan, NULL);
2737
		ast_channel_sched_set(chan, NULL);
2738
	}
2738
	}
2739

    
   
2739

   
2740
	if (ast_channel_generatordata(chan)) {	/* Clear any tone stuff remaining */
2740
	if (ast_channel_generatordata(chan)) {	/* Clear any tone stuff remaining */
2741
		if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2741
		if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2742
			ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
2742
			ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
2743
		}
2743
		}
2744
	}
2744
	}
2745
	ast_channel_generatordata_set(chan, NULL);
2745
	ast_channel_generatordata_set(chan, NULL);
2746
	ast_channel_generator_set(chan, NULL);
2746
	ast_channel_generator_set(chan, NULL);
2747

    
   
2747

   
2748
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
2748
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
2749
		ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
2749
		ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
2750
			"is blocked by thread %ld in procedure %s!  Expect a failure\n",
2750
			"is blocked by thread %ld in procedure %s!  Expect a failure\n",
2751
			(long) pthread_self(), ast_channel_name(chan), (long)ast_channel_blocker(chan), ast_channel_blockproc(chan));
2751
			(long) pthread_self(), ast_channel_name(chan), (long)ast_channel_blocker(chan), ast_channel_blockproc(chan));
2752
		ast_assert(ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING) == 0);
2752
		ast_assert(ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING) == 0);
2753
	}
2753
	}
2754

    
   
2754

   
2755
	ast_debug(1, "Hanging up channel '%s'\n", ast_channel_name(chan));
2755
	ast_debug(1, "Hanging up channel '%s'\n", ast_channel_name(chan));
2756
	if (ast_channel_tech(chan)->hangup) {
2756
	if (ast_channel_tech(chan)->hangup) {
2757
		ast_channel_tech(chan)->hangup(chan);
2757
		ast_channel_tech(chan)->hangup(chan);
2758
	}
2758
	}
2759

    
   
2759

   
2760
	ast_channel_unlock(chan);
2760
	ast_channel_unlock(chan);
2761

    
   
2761

   
2762
	ast_cc_offer(chan);
2762
	ast_cc_offer(chan);
2763

    
   
2763

   
2764
	ast_channel_unref(chan);
2764
	ast_channel_unref(chan);
2765
}
2765
}
2766

    
   
2766

   
2767
int ast_raw_answer(struct ast_channel *chan)
2767
int ast_raw_answer(struct ast_channel *chan)
2768
{
2768
{
2769
	int res = 0;
2769
	int res = 0;
2770
	struct timeval answertime;
2770
	struct timeval answertime;
2771

    
   
2771

   
2772
	ast_channel_lock(chan);
2772
	ast_channel_lock(chan);
2773

    
   
2773

   
2774
	/* You can't answer an outbound call */
2774
	/* You can't answer an outbound call */
2775
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
2775
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
2776
		ast_channel_unlock(chan);
2776
		ast_channel_unlock(chan);
2777
		return 0;
2777
		return 0;
2778
	}
2778
	}
2779

    
   
2779

   
2780
	/* Stop if we're a zombie or need a soft hangup */
2780
	/* Stop if we're a zombie or need a soft hangup */
2781
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
2781
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
2782
		ast_channel_unlock(chan);
2782
		ast_channel_unlock(chan);
2783
		return -1;
2783
		return -1;
2784
	}
2784
	}
2785

    
   
2785

   
2786
	answertime = ast_tvnow();
2786
	answertime = ast_tvnow();
2787
	ast_channel_answertime_set(chan, &answertime);
2787
	ast_channel_answertime_set(chan, &answertime);
2788

    
   
2788

   
2789
	ast_channel_unlock(chan);
2789
	ast_channel_unlock(chan);
2790

    
   
2790

   
2791
	switch (ast_channel_state(chan)) {
2791
	switch (ast_channel_state(chan)) {
2792
	case AST_STATE_RINGING:
2792
	case AST_STATE_RINGING:
2793
	case AST_STATE_RING:
2793
	case AST_STATE_RING:
2794
		ast_channel_lock(chan);
2794
		ast_channel_lock(chan);
2795
		if (ast_channel_tech(chan)->answer) {
2795
		if (ast_channel_tech(chan)->answer) {
2796
			res = ast_channel_tech(chan)->answer(chan);
2796
			res = ast_channel_tech(chan)->answer(chan);
2797
		}
2797
		}
2798
		ast_setstate(chan, AST_STATE_UP);
2798
		ast_setstate(chan, AST_STATE_UP);
2799
		ast_channel_unlock(chan);
2799
		ast_channel_unlock(chan);
2800
		break;
2800
		break;
2801
	case AST_STATE_UP:
2801
	case AST_STATE_UP:
2802
		break;
2802
		break;
2803
	default:
2803
	default:
2804
		break;
2804
		break;
2805
	}
2805
	}
2806

    
   
2806

   
2807
	ast_indicate(chan, -1);
2807
	ast_indicate(chan, -1);
2808

    
   
2808

   
2809
	return res;
2809
	return res;
2810
}
2810
}
2811

    
   
2811

   
2812
int __ast_answer(struct ast_channel *chan, unsigned int delay)
2812
int __ast_answer(struct ast_channel *chan, unsigned int delay)
2813
{
2813
{
2814
	int res = 0;
2814
	int res = 0;
2815
	enum ast_channel_state old_state;
2815
	enum ast_channel_state old_state;
2816

    
   
2816

   
2817
	old_state = ast_channel_state(chan);
2817
	old_state = ast_channel_state(chan);
2818
	if ((res = ast_raw_answer(chan))) {
2818
	if ((res = ast_raw_answer(chan))) {
2819
		return res;
2819
		return res;
2820
	}
2820
	}
2821

    
   
2821

   
2822
	switch (old_state) {
2822
	switch (old_state) {
2823
	case AST_STATE_RINGING:
2823
	case AST_STATE_RINGING:
2824
	case AST_STATE_RING:
2824
	case AST_STATE_RING:
2825
		/* wait for media to start flowing, but don't wait any longer
2825
		/* wait for media to start flowing, but don't wait any longer
2826
		 * than 'delay' or 500 milliseconds, whichever is longer
2826
		 * than 'delay' or 500 milliseconds, whichever is longer
2827
		 */
2827
		 */
2828
		do {
2828
		do {
2829
			AST_LIST_HEAD_NOLOCK(, ast_frame) frames;
2829
			AST_LIST_HEAD_NOLOCK(, ast_frame) frames;
2830
			struct ast_frame *cur;
2830
			struct ast_frame *cur;
2831
			struct ast_frame *new_frame;
2831
			struct ast_frame *new_frame;
2832
			int timeout_ms = MAX(delay, 500);
2832
			int timeout_ms = MAX(delay, 500);
2833
			unsigned int done = 0;
2833
			unsigned int done = 0;
2834
			struct timeval start;
2834
			struct timeval start;
2835

    
   
2835

   
2836
			AST_LIST_HEAD_INIT_NOLOCK(&frames);
2836
			AST_LIST_HEAD_INIT_NOLOCK(&frames);
2837

    
   
2837

   
2838
			start = ast_tvnow();
2838
			start = ast_tvnow();
2839
			for (;;) {
2839
			for (;;) {
2840
				int ms = ast_remaining_ms(start, timeout_ms);
2840
				int ms = ast_remaining_ms(start, timeout_ms);
2841
				ms = ast_waitfor(chan, ms);
2841
				ms = ast_waitfor(chan, ms);
2842
				if (ms < 0) {
2842
				if (ms < 0) {
2843
					ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
2843
					ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
2844
					res = -1;
2844
					res = -1;
2845
					break;
2845
					break;
2846
				}
2846
				}
2847
				if (ms == 0) {
2847
				if (ms == 0) {
2848
					ast_debug(2, "Didn't receive a media frame from %s within %u ms of answering. Continuing anyway\n", ast_channel_name(chan), MAX(delay, 500));
2848
					ast_debug(2, "Didn't receive a media frame from %s within %u ms of answering. Continuing anyway\n", ast_channel_name(chan), MAX(delay, 500));
2849
					break;
2849
					break;
2850
				}
2850
				}
2851
				cur = ast_read(chan);
2851
				cur = ast_read(chan);
2852
				if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
2852
				if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
2853
					     (cur->subclass.integer == AST_CONTROL_HANGUP))) {
2853
					     (cur->subclass.integer == AST_CONTROL_HANGUP))) {
2854
					if (cur) {
2854
					if (cur) {
2855
						ast_frfree(cur);
2855
						ast_frfree(cur);
2856
					}
2856
					}
2857
					res = -1;
2857
					res = -1;
2858
					ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
2858
					ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
2859
					break;
2859
					break;
2860
				}
2860
				}
2861

    
   
2861

   
2862
				if ((new_frame = ast_frisolate(cur)) != cur) {
2862
				if ((new_frame = ast_frisolate(cur)) != cur) {
2863
					ast_frfree(cur);
2863
					ast_frfree(cur);
2864
				}
2864
				}
2865

    
   
2865

   
2866
				AST_LIST_INSERT_HEAD(&frames, new_frame, frame_list);
2866
				AST_LIST_INSERT_HEAD(&frames, new_frame, frame_list);
2867

    
   
2867

   
2868
				/* if a specific delay period was requested, continue
2868
				/* if a specific delay period was requested, continue
2869
				 * until that delay has passed. don't stop just because
2869
				 * until that delay has passed. don't stop just because
2870
				 * incoming media has arrived.
2870
				 * incoming media has arrived.
2871
				 */
2871
				 */
2872
				if (delay) {
2872
				if (delay) {
2873
					continue;
2873
					continue;
2874
				}
2874
				}
2875

    
   
2875

   
2876
				switch (new_frame->frametype) {
2876
				switch (new_frame->frametype) {
2877
					/* all of these frametypes qualify as 'media' */
2877
					/* all of these frametypes qualify as 'media' */
2878
				case AST_FRAME_VOICE:
2878
				case AST_FRAME_VOICE:
2879
				case AST_FRAME_VIDEO:
2879
				case AST_FRAME_VIDEO:
2880
				case AST_FRAME_TEXT:
2880
				case AST_FRAME_TEXT:
2881
				case AST_FRAME_DTMF_BEGIN:
2881
				case AST_FRAME_DTMF_BEGIN:
2882
				case AST_FRAME_DTMF_END:
2882
				case AST_FRAME_DTMF_END:
2883
				case AST_FRAME_IMAGE:
2883
				case AST_FRAME_IMAGE:
2884
				case AST_FRAME_HTML:
2884
				case AST_FRAME_HTML:
2885
				case AST_FRAME_MODEM:
2885
				case AST_FRAME_MODEM:
2886
					done = 1;
2886
					done = 1;
2887
					break;
2887
					break;
2888
				case AST_FRAME_CONTROL:
2888
				case AST_FRAME_CONTROL:
2889
				case AST_FRAME_IAX:
2889
				case AST_FRAME_IAX:
2890
				case AST_FRAME_BRIDGE_ACTION:
2890
				case AST_FRAME_BRIDGE_ACTION:
2891
				case AST_FRAME_BRIDGE_ACTION_SYNC:
2891
				case AST_FRAME_BRIDGE_ACTION_SYNC:
2892
				case AST_FRAME_NULL:
2892
				case AST_FRAME_NULL:
2893
				case AST_FRAME_CNG:
2893
				case AST_FRAME_CNG:
2894
					break;
2894
					break;
2895
				}
2895
				}
2896

    
   
2896

   
2897
				if (done) {
2897
				if (done) {
2898
					break;
2898
					break;
2899
				}
2899
				}
2900
			}
2900
			}
2901

    
   
2901

   
2902
			if (res == 0) {
2902
			if (res == 0) {
2903
				ast_channel_lock(chan);
2903
				ast_channel_lock(chan);
2904
				while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
2904
				while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
2905
					ast_queue_frame_head(chan, cur);
2905
					ast_queue_frame_head(chan, cur);
2906
					ast_frfree(cur);
2906
					ast_frfree(cur);
2907
				}
2907
				}
2908
				ast_channel_unlock(chan);
2908
				ast_channel_unlock(chan);
2909
			}
2909
			}
2910
		} while (0);
2910
		} while (0);
2911
		break;
2911
		break;
2912
	default:
2912
	default:
2913
		break;
2913
		break;
2914
	}
2914
	}
2915

    
   
2915

   
2916
	return res;
2916
	return res;
2917
}
2917
}
2918

    
   
2918

   
2919
int ast_answer(struct ast_channel *chan)
2919
int ast_answer(struct ast_channel *chan)
2920
{
2920
{
2921
	return __ast_answer(chan, 0);
2921
	return __ast_answer(chan, 0);
2922
}
2922
}
2923

    
   
2923

   
2924
inline int ast_auto_answer(struct ast_channel *chan)
2924
inline int ast_auto_answer(struct ast_channel *chan)
2925
{
2925
{
2926
	if (ast_channel_state(chan) == AST_STATE_UP) {
2926
	if (ast_channel_state(chan) == AST_STATE_UP) {
2927
		/* Already answered */
2927
		/* Already answered */
2928
		return 0;
2928
		return 0;
2929
	}
2929
	}
2930
	return ast_answer(chan);
2930
	return ast_answer(chan);
2931
}
2931
}
2932

    
   
2932

   
2933
int ast_channel_get_duration(struct ast_channel *chan)
2933
int ast_channel_get_duration(struct ast_channel *chan)
2934
{
2934
{
2935
	ast_assert(NULL != chan);
2935
	ast_assert(NULL != chan);
2936

    
   
2936

   
2937
	if (ast_tvzero(ast_channel_creationtime(chan))) {
2937
	if (ast_tvzero(ast_channel_creationtime(chan))) {
2938
		return 0;
2938
		return 0;
2939
	}
2939
	}
2940
	return (ast_tvdiff_ms(ast_tvnow(), ast_channel_creationtime(chan)) / 1000);
2940
	return (ast_tvdiff_ms(ast_tvnow(), ast_channel_creationtime(chan)) / 1000);
2941
}
2941
}
2942

    
   
2942

   
2943
int ast_channel_get_up_time(struct ast_channel *chan)
2943
int ast_channel_get_up_time(struct ast_channel *chan)
2944
{
2944
{
2945
	ast_assert(NULL != chan);
2945
	ast_assert(NULL != chan);
2946

    
   
2946

   
2947
	if (ast_tvzero(ast_channel_answertime(chan))) {
2947
	if (ast_tvzero(ast_channel_answertime(chan))) {
2948
		return 0;
2948
		return 0;
2949
	}
2949
	}
2950
	return (ast_tvdiff_ms(ast_tvnow(), ast_channel_answertime(chan)) / 1000);
2950
	return (ast_tvdiff_ms(ast_tvnow(), ast_channel_answertime(chan)) / 1000);
2951
}
2951
}
2952

    
   
2952

   
2953
static void deactivate_generator_nolock(struct ast_channel *chan)
2953
static void deactivate_generator_nolock(struct ast_channel *chan)
2954
{
2954
{
2955
	if (ast_channel_generatordata(chan)) {
2955
	if (ast_channel_generatordata(chan)) {
2956
		struct ast_generator *generator = ast_channel_generator(chan);
2956
		struct ast_generator *generator = ast_channel_generator(chan);
2957

    
   
2957

   
2958
		if (generator && generator->release) {
2958
		if (generator && generator->release) {
2959
			generator->release(chan, ast_channel_generatordata(chan));
2959
			generator->release(chan, ast_channel_generatordata(chan));
2960
		}
2960
		}
2961
		ast_channel_generatordata_set(chan, NULL);
2961
		ast_channel_generatordata_set(chan, NULL);
2962
		ast_channel_generator_set(chan, NULL);
2962
		ast_channel_generator_set(chan, NULL);
2963
		ast_channel_set_fd(chan, AST_GENERATOR_FD, -1);
2963
		ast_channel_set_fd(chan, AST_GENERATOR_FD, -1);
2964
		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
2964
		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
2965
		ast_settimeout(chan, 0, NULL, NULL);
2965
		ast_settimeout(chan, 0, NULL, NULL);
2966
	}
2966
	}
2967
}
2967
}
2968

    
   
2968

   
2969
void ast_deactivate_generator(struct ast_channel *chan)
2969
void ast_deactivate_generator(struct ast_channel *chan)
2970
{
2970
{
2971
	ast_channel_lock(chan);
2971
	ast_channel_lock(chan);
2972
	deactivate_generator_nolock(chan);
2972
	deactivate_generator_nolock(chan);
2973
	ast_channel_unlock(chan);
2973
	ast_channel_unlock(chan);
2974
}
2974
}
2975

    
   
2975

   
2976
static void generator_write_format_change(struct ast_channel *chan)
2976
static void generator_write_format_change(struct ast_channel *chan)
2977
{
2977
{
2978
	struct ast_generator *generator;
2978
	struct ast_generator *generator;
2979

    
   
2979

   
2980
	ast_channel_lock(chan);
2980
	ast_channel_lock(chan);
2981
	generator = ast_channel_generator(chan);
2981
	generator = ast_channel_generator(chan);
2982
	if (generator && generator->write_format_change) {
2982
	if (generator && generator->write_format_change) {
2983
		generator->write_format_change(chan, ast_channel_generatordata(chan));
2983
		generator->write_format_change(chan, ast_channel_generatordata(chan));
2984
	}
2984
	}
2985
	ast_channel_unlock(chan);
2985
	ast_channel_unlock(chan);
2986
}
2986
}
2987

    
   
2987

   
2988
static int generator_force(const void *data)
2988
static int generator_force(const void *data)
2989
{
2989
{
2990
	/* Called if generator doesn't have data */
2990
	/* Called if generator doesn't have data */
2991
	void *tmp;
2991
	void *tmp;
2992
	int res;
2992
	int res;
2993
	int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
2993
	int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
2994
	struct ast_channel *chan = (struct ast_channel *)data;
2994
	struct ast_channel *chan = (struct ast_channel *)data;
2995

    
   
2995

   
2996
	ast_channel_lock(chan);
2996
	ast_channel_lock(chan);
2997
	tmp = ast_channel_generatordata(chan);
2997
	tmp = ast_channel_generatordata(chan);
2998
	ast_channel_generatordata_set(chan, NULL);
2998
	ast_channel_generatordata_set(chan, NULL);
2999
	if (ast_channel_generator(chan))
2999
	if (ast_channel_generator(chan))
3000
		generate = ast_channel_generator(chan)->generate;
3000
		generate = ast_channel_generator(chan)->generate;
3001
	ast_channel_unlock(chan);
3001
	ast_channel_unlock(chan);
3002

    
   
3002

   
3003
	if (!tmp || !generate)
3003
	if (!tmp || !generate)
3004
		return 0;
3004
		return 0;
3005

    
   
3005

   
3006
	res = generate(chan, tmp, 0, ast_format_rate(ast_channel_writeformat(chan)) / 50);
3006
	res = generate(chan, tmp, 0, ast_format_rate(ast_channel_writeformat(chan)) / 50);
3007

    
   
3007

   
3008
	ast_channel_lock(chan);
3008
	ast_channel_lock(chan);
3009
	if (ast_channel_generator(chan) && generate == ast_channel_generator(chan)->generate) {
3009
	if (ast_channel_generator(chan) && generate == ast_channel_generator(chan)->generate) {
3010
		ast_channel_generatordata_set(chan, tmp);
3010
		ast_channel_generatordata_set(chan, tmp);
3011
	}
3011
	}
3012
	ast_channel_unlock(chan);
3012
	ast_channel_unlock(chan);
3013

    
   
3013

   
3014
	if (res) {
3014
	if (res) {
3015
		ast_debug(1, "Auto-deactivating generator\n");
3015
		ast_debug(1, "Auto-deactivating generator\n");
3016
		ast_deactivate_generator(chan);
3016
		ast_deactivate_generator(chan);
3017
	}
3017
	}
3018

    
   
3018

   
3019
	return 0;
3019
	return 0;
3020
}
3020
}
3021

    
   
3021

   
3022
int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
3022
int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
3023
{
3023
{
3024
	int res = 0;
3024
	int res = 0;
3025
	void *generatordata = NULL;
3025
	void *generatordata = NULL;
3026

    
   
3026

   
3027
	ast_channel_lock(chan);
3027
	ast_channel_lock(chan);
3028
	if (ast_channel_generatordata(chan)) {
3028
	if (ast_channel_generatordata(chan)) {
3029
		struct ast_generator *generator_old = ast_channel_generator(chan);
3029
		struct ast_generator *generator_old = ast_channel_generator(chan);
3030

    
   
3030

   
3031
		if (generator_old && generator_old->release) {
3031
		if (generator_old && generator_old->release) {
3032
			generator_old->release(chan, ast_channel_generatordata(chan));
3032
			generator_old->release(chan, ast_channel_generatordata(chan));
3033
		}
3033
		}
3034
	}
3034
	}
3035
	if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
3035
	if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
3036
		res = -1;
3036
		res = -1;
3037
	}
3037
	}
3038
	ast_channel_generatordata_set(chan, generatordata);
3038
	ast_channel_generatordata_set(chan, generatordata);
3039
	if (!res) {
3039
	if (!res) {
3040
		ast_settimeout(chan, 50, generator_force, chan);
3040
		ast_settimeout(chan, 50, generator_force, chan);
3041
		ast_channel_generator_set(chan, gen);
3041
		ast_channel_generator_set(chan, gen);
3042
	}
3042
	}
3043
	ast_channel_unlock(chan);
3043
	ast_channel_unlock(chan);
3044

    
   
3044

   
3045
	ast_prod(chan);
3045
	ast_prod(chan);
3046

    
   
3046

   
3047
	return res;
3047
	return res;
3048
}
3048
}
3049

    
   
3049

   
3050
/*! \brief Wait for x amount of time on a file descriptor to have input.  */
3050
/*! \brief Wait for x amount of time on a file descriptor to have input.  */
3051
int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
3051
int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
3052
{
3052
{
3053
	int winner = -1;
3053
	int winner = -1;
3054
	ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
3054
	ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
3055
	return winner;
3055
	return winner;
3056
}
3056
}
3057

    
   
3057

   
3058
/*! \brief Wait for x amount of time on a file descriptor to have input.  */
3058
/*! \brief Wait for x amount of time on a file descriptor to have input.  */
3059
#ifdef HAVE_EPOLL
3059
#ifdef HAVE_EPOLL
3060
static struct ast_channel *ast_waitfor_nandfds_classic(struct ast_channel **c, int n, int *fds, int nfds,
3060
static struct ast_channel *ast_waitfor_nandfds_classic(struct ast_channel **c, int n, int *fds, int nfds,
3061
					int *exception, int *outfd, int *ms)
3061
					int *exception, int *outfd, int *ms)
3062
#else
3062
#else
3063
struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3063
struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3064
					int *exception, int *outfd, int *ms)
3064
					int *exception, int *outfd, int *ms)
3065
#endif
3065
#endif
3066
{
3066
{
3067
	struct timeval start = { 0 , 0 };
3067
	struct timeval start = { 0 , 0 };
3068
	struct pollfd *pfds = NULL;
3068
	struct pollfd *pfds = NULL;
3069
	int res;
3069
	int res;
3070
	long rms;
3070
	long rms;
3071
	int x, y, max;
3071
	int x, y, max;
3072
	int sz;
3072
	int sz;
3073
	struct timeval now = { 0, 0 };
3073
	struct timeval now = { 0, 0 };
3074
	struct timeval whentohangup = { 0, 0 }, diff;
3074
	struct timeval whentohangup = { 0, 0 }, diff;
3075
	struct ast_channel *winner = NULL;
3075
	struct ast_channel *winner = NULL;
3076
	struct fdmap {
3076
	struct fdmap {
3077
		int chan;
3077
		int chan;
3078
		int fdno;
3078
		int fdno;
3079
	} *fdmap = NULL;
3079
	} *fdmap = NULL;
3080

    
   
3080

   
3081
	if (outfd) {
3081
	if (outfd) {
3082
		*outfd = -99999;
3082
		*outfd = -99999;
3083
	}
3083
	}
3084
	if (exception) {
3084
	if (exception) {
3085
		*exception = 0;
3085
		*exception = 0;
3086
	}
3086
	}
3087

    
   
3087

   
3088
	if ((sz = n * AST_MAX_FDS + nfds)) {
3088
	if ((sz = n * AST_MAX_FDS + nfds)) {
3089
		pfds = ast_alloca(sizeof(*pfds) * sz);
3089
		pfds = ast_alloca(sizeof(*pfds) * sz);
3090
		fdmap = ast_alloca(sizeof(*fdmap) * sz);
3090
		fdmap = ast_alloca(sizeof(*fdmap) * sz);
3091
	} else {
3091
	} else {
3092
		/* nothing to allocate and no FDs to check */
3092
		/* nothing to allocate and no FDs to check */
3093
		return NULL;
3093
		return NULL;
3094
	}
3094
	}
3095

    
   
3095

   
3096
	for (x = 0; x < n; x++) {
3096
	for (x = 0; x < n; x++) {
3097
		ast_channel_lock(c[x]);
3097
		ast_channel_lock(c[x]);
3098
		if (!ast_tvzero(*ast_channel_whentohangup(c[x]))) {
3098
		if (!ast_tvzero(*ast_channel_whentohangup(c[x]))) {
3099
			if (ast_tvzero(whentohangup))
3099
			if (ast_tvzero(whentohangup))
3100
				now = ast_tvnow();
3100
				now = ast_tvnow();
3101
			diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
3101
			diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
3102
			if (diff.tv_sec < 0 || ast_tvzero(diff)) {
3102
			if (diff.tv_sec < 0 || ast_tvzero(diff)) {
3103
				ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3103
				ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3104
				/* Should already be hungup */
3104
				/* Should already be hungup */
3105
				ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3105
				ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3106
				ast_channel_unlock(c[x]);
3106
				ast_channel_unlock(c[x]);
3107
				return c[x];
3107
				return c[x];
3108
			}
3108
			}
3109
			if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3109
			if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3110
				whentohangup = diff;
3110
				whentohangup = diff;
3111
		}
3111
		}
3112
		ast_channel_unlock(c[x]);
3112
		ast_channel_unlock(c[x]);
3113
	}
3113
	}
3114
	/* Wait full interval */
3114
	/* Wait full interval */
3115
	rms = *ms;
3115
	rms = *ms;
3116
	/* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3116
	/* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3117
	if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3117
	if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3118
		rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000;              /* timeout in milliseconds */
3118
		rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000;              /* timeout in milliseconds */
3119
		if (*ms >= 0 && *ms < rms) {                                                 /* original *ms still smaller */
3119
		if (*ms >= 0 && *ms < rms) {                                                 /* original *ms still smaller */
3120
			rms =  *ms;
3120
			rms =  *ms;
3121
		}
3121
		}
3122
	} else if (!ast_tvzero(whentohangup) && rms < 0) {
3122
	} else if (!ast_tvzero(whentohangup) && rms < 0) {
3123
		/* Tiny corner case... call would need to last >24 days */
3123
		/* Tiny corner case... call would need to last >24 days */
3124
		rms = INT_MAX;
3124
		rms = INT_MAX;
3125
	}
3125
	}
3126
	/*
3126
	/*
3127
	 * Build the pollfd array, putting the channels' fds first,
3127
	 * Build the pollfd array, putting the channels' fds first,
3128
	 * followed by individual fds. Order is important because
3128
	 * followed by individual fds. Order is important because
3129
	 * individual fd's must have priority over channel fds.
3129
	 * individual fd's must have priority over channel fds.
3130
	 */
3130
	 */
3131
	max = 0;
3131
	max = 0;
3132
	for (x = 0; x < n; x++) {
3132
	for (x = 0; x < n; x++) {
3133
		for (y = 0; y < AST_MAX_FDS; y++) {
3133
		for (y = 0; y < AST_MAX_FDS; y++) {
3134
			fdmap[max].fdno = y;  /* fd y is linked to this pfds */
3134
			fdmap[max].fdno = y;  /* fd y is linked to this pfds */
3135
			fdmap[max].chan = x;  /* channel x is linked to this pfds */
3135
			fdmap[max].chan = x;  /* channel x is linked to this pfds */
3136
			max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
3136
			max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
3137
		}
3137
		}
3138
		CHECK_BLOCKING(c[x]);
3138
		CHECK_BLOCKING(c[x]);
3139
	}
3139
	}
3140
	/* Add the individual fds */
3140
	/* Add the individual fds */
3141
	for (x = 0; x < nfds; x++) {
3141
	for (x = 0; x < nfds; x++) {
3142
		fdmap[max].chan = -1;
3142
		fdmap[max].chan = -1;
3143
		max += ast_add_fd(&pfds[max], fds[x]);
3143
		max += ast_add_fd(&pfds[max], fds[x]);
3144
	}
3144
	}
3145

    
   
3145

   
3146
	if (*ms > 0) {
3146
	if (*ms > 0) {
3147
		start = ast_tvnow();
3147
		start = ast_tvnow();
3148
	}
3148
	}
3149

    
   
3149

   
3150
	if (sizeof(int) == 4) {	/* XXX fix timeout > 600000 on linux x86-32 */
3150
	if (sizeof(int) == 4) {	/* XXX fix timeout > 600000 on linux x86-32 */
3151
		do {
3151
		do {
3152
			int kbrms = rms;
3152
			int kbrms = rms;
3153
			if (kbrms > 600000) {
3153
			if (kbrms > 600000) {
3154
				kbrms = 600000;
3154
				kbrms = 600000;
3155
			}
3155
			}
3156
			res = ast_poll(pfds, max, kbrms);
3156
			res = ast_poll(pfds, max, kbrms);
3157
			if (!res) {
3157
			if (!res) {
3158
				rms -= kbrms;
3158
				rms -= kbrms;
3159
			}
3159
			}
3160
		} while (!res && (rms > 0));
3160
		} while (!res && (rms > 0));
3161
	} else {
3161
	} else {
3162
		res = ast_poll(pfds, max, rms);
3162
		res = ast_poll(pfds, max, rms);
3163
	}
3163
	}
3164
	for (x = 0; x < n; x++) {
3164
	for (x = 0; x < n; x++) {
3165
		ast_clear_flag(ast_channel_flags(c[x]), AST_FLAG_BLOCKING);
3165
		ast_clear_flag(ast_channel_flags(c[x]), AST_FLAG_BLOCKING);
3166
	}
3166
	}
3167
	if (res < 0) { /* Simulate a timeout if we were interrupted */
3167
	if (res < 0) { /* Simulate a timeout if we were interrupted */
3168
		if (errno != EINTR) {
3168
		if (errno != EINTR) {
3169
			*ms = -1;
3169
			*ms = -1;
3170
		}
3170
		}
3171
		return NULL;
3171
		return NULL;
3172
	}
3172
	}
3173
	if (!ast_tvzero(whentohangup)) {   /* if we have a timeout, check who expired */
3173
	if (!ast_tvzero(whentohangup)) {   /* if we have a timeout, check who expired */
3174
		now = ast_tvnow();
3174
		now = ast_tvnow();
3175
		for (x = 0; x < n; x++) {
3175
		for (x = 0; x < n; x++) {
3176
			if (!ast_tvzero(*ast_channel_whentohangup(c[x])) && ast_tvcmp(*ast_channel_whentohangup(c[x]), now) <= 0) {
3176
			if (!ast_tvzero(*ast_channel_whentohangup(c[x])) && ast_tvcmp(*ast_channel_whentohangup(c[x]), now) <= 0) {
3177
				ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3177
				ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3178
				ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3178
				ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3179
				if (winner == NULL) {
3179
				if (winner == NULL) {
3180
					winner = c[x];
3180
					winner = c[x];
3181
				}
3181
				}
3182
			}
3182
			}
3183
		}
3183
		}
3184
	}
3184
	}
3185
	if (res == 0) { /* no fd ready, reset timeout and done */
3185
	if (res == 0) { /* no fd ready, reset timeout and done */
3186
		*ms = 0;	/* XXX use 0 since we may not have an exact timeout. */
3186
		*ms = 0;	/* XXX use 0 since we may not have an exact timeout. */
3187
		return winner;
3187
		return winner;
3188
	}
3188
	}
3189
	/*
3189
	/*
3190
	 * Then check if any channel or fd has a pending event.
3190
	 * Then check if any channel or fd has a pending event.
3191
	 * Remember to check channels first and fds last, as they
3191
	 * Remember to check channels first and fds last, as they
3192
	 * must have priority on setting 'winner'
3192
	 * must have priority on setting 'winner'
3193
	 */
3193
	 */
3194
	for (x = 0; x < max; x++) {
3194
	for (x = 0; x < max; x++) {
3195
		res = pfds[x].revents;
3195
		res = pfds[x].revents;
3196
		if (res == 0) {
3196
		if (res == 0) {
3197
			continue;
3197
			continue;
3198
		}
3198
		}
3199
		if (fdmap[x].chan >= 0) {	/* this is a channel */
3199
		if (fdmap[x].chan >= 0) {	/* this is a channel */
3200
			winner = c[fdmap[x].chan];	/* override previous winners */
3200
			winner = c[fdmap[x].chan];	/* override previous winners */
3201
			if (res & POLLPRI) {
3201
			if (res & POLLPRI) {
3202
				ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3202
				ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3203
			} else {
3203
			} else {
3204
				ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3204
				ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3205
			}
3205
			}
3206
			ast_channel_fdno_set(winner, fdmap[x].fdno);
3206
			ast_channel_fdno_set(winner, fdmap[x].fdno);
3207
		} else {			/* this is an fd */
3207
		} else {			/* this is an fd */
3208
			if (outfd) {
3208
			if (outfd) {
3209
				*outfd = pfds[x].fd;
3209
				*outfd = pfds[x].fd;
3210
			}
3210
			}
3211
			if (exception) {
3211
			if (exception) {
3212
				*exception = (res & POLLPRI) ? -1 : 0;
3212
				*exception = (res & POLLPRI) ? -1 : 0;
3213
			}
3213
			}
3214
			winner = NULL;
3214
			winner = NULL;
3215
		}
3215
		}
3216
	}
3216
	}
3217
	if (*ms > 0) {
3217
	if (*ms > 0) {
3218
		*ms -= ast_tvdiff_ms(ast_tvnow(), start);
3218
		*ms -= ast_tvdiff_ms(ast_tvnow(), start);
3219
		if (*ms < 0) {
3219
		if (*ms < 0) {
3220
			*ms = 0;
3220
			*ms = 0;
3221
		}
3221
		}
3222
	}
3222
	}
3223
	return winner;
3223
	return winner;
3224
}
3224
}
3225

    
   
3225

   
3226
#ifdef HAVE_EPOLL
3226
#ifdef HAVE_EPOLL
3227
static struct ast_channel *ast_waitfor_nandfds_simple(struct ast_channel *chan, int *ms)
3227
static struct ast_channel *ast_waitfor_nandfds_simple(struct ast_channel *chan, int *ms)
3228
{
3228
{
3229
	struct timeval start = { 0 , 0 };
3229
	struct timeval start = { 0 , 0 };
3230
	int res = 0;
3230
	int res = 0;
3231
	struct epoll_event ev[1];
3231
	struct epoll_event ev[1];
3232
	long diff, rms = *ms;
3232
	long diff, rms = *ms;
3233
	struct ast_channel *winner = NULL;
3233
	struct ast_channel *winner = NULL;
3234
	struct ast_epoll_data *aed = NULL;
3234
	struct ast_epoll_data *aed = NULL;
3235

    
   
3235

   
3236
	ast_channel_lock(chan);
3236
	ast_channel_lock(chan);
3237
	/* Figure out their timeout */
3237
	/* Figure out their timeout */
3238
	if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3238
	if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3239
		if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow())) < 0) {
3239
		if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow())) < 0) {
3240
			/* They should already be hungup! */
3240
			/* They should already be hungup! */
3241
			ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3241
			ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3242
			ast_channel_unlock(chan);
3242
			ast_channel_unlock(chan);
3243
			return NULL;
3243
			return NULL;
3244
		}
3244
		}
3245
		/* If this value is smaller then the current one... make it priority */
3245
		/* If this value is smaller then the current one... make it priority */
3246
		if (rms > diff) {
3246
		if (rms > diff) {
3247
			rms = diff;
3247
			rms = diff;
3248
		}
3248
		}
3249
	}
3249
	}
3250

    
   
3250

   
3251
	ast_channel_unlock(chan);
3251
	ast_channel_unlock(chan);
3252

    
   
3252

   
3253
	/* Time to make this channel block... */
3253
	/* Time to make this channel block... */
3254
	CHECK_BLOCKING(chan);
3254
	CHECK_BLOCKING(chan);
3255

    
   
3255

   
3256
	if (*ms > 0) {
3256
	if (*ms > 0) {
3257
		start = ast_tvnow();
3257
		start = ast_tvnow();
3258
	}
3258
	}
3259

    
   
3259

   
3260
	/* We don't have to add any file descriptors... they are already added, we just have to wait! */
3260
	/* We don't have to add any file descriptors... they are already added, we just have to wait! */
3261
	res = epoll_wait(ast_channel_epfd(chan), ev, 1, rms);
3261
	res = epoll_wait(ast_channel_epfd(chan), ev, 1, rms);
3262

    
   
3262

   
3263
	/* Stop blocking */
3263
	/* Stop blocking */
3264
	ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
3264
	ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
3265

    
   
3265

   
3266
	/* Simulate a timeout if we were interrupted */
3266
	/* Simulate a timeout if we were interrupted */
3267
	if (res < 0) {
3267
	if (res < 0) {
3268
		if (errno != EINTR) {
3268
		if (errno != EINTR) {
3269
			*ms = -1;
3269
			*ms = -1;
3270
		}
3270
		}
3271
		return NULL;
3271
		return NULL;
3272
	}
3272
	}
3273

    
   
3273

   
3274
	/* If this channel has a timeout see if it expired */
3274
	/* If this channel has a timeout see if it expired */
3275
	if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3275
	if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3276
		if (ast_tvdiff_ms(ast_tvnow(), *ast_channel_whentohangup(chan)) >= 0) {
3276
		if (ast_tvdiff_ms(ast_tvnow(), *ast_channel_whentohangup(chan)) >= 0) {
3277
			ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3277
			ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3278
			winner = chan;
3278
			winner = chan;
3279
		}
3279
		}
3280
	}
3280
	}
3281

    
   
3281

   
3282
	/* No fd ready, reset timeout and be done for now */
3282
	/* No fd ready, reset timeout and be done for now */
3283
	if (!res) {
3283
	if (!res) {
3284
		*ms = 0;
3284
		*ms = 0;
3285
		return winner;
3285
		return winner;
3286
	}
3286
	}
3287

    
   
3287

   
3288
	/* See what events are pending */
3288
	/* See what events are pending */
3289
	aed = ev[0].data.ptr;
3289
	aed = ev[0].data.ptr;
3290
	ast_channel_fdno_set(chan, aed->which);
3290
	ast_channel_fdno_set(chan, aed->which);
3291
	if (ev[0].events & EPOLLPRI) {
3291
	if (ev[0].events & EPOLLPRI) {
3292
		ast_set_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3292
		ast_set_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3293
	} else {
3293
	} else {
3294
		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3294
		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3295
	}
3295
	}
3296

    
   
3296

   
3297
	if (*ms > 0) {
3297
	if (*ms > 0) {
3298
		*ms -= ast_tvdiff_ms(ast_tvnow(), start);
3298
		*ms -= ast_tvdiff_ms(ast_tvnow(), start);
3299
		if (*ms < 0) {
3299
		if (*ms < 0) {
3300
			*ms = 0;
3300
			*ms = 0;
3301
		}
3301
		}
3302
	}
3302
	}
3303

    
   
3303

   
3304
	return chan;
3304
	return chan;
3305
}
3305
}
3306

    
   
3306

   
3307
static struct ast_channel *ast_waitfor_nandfds_complex(struct ast_channel **c, int n, int *ms)
3307
static struct ast_channel *ast_waitfor_nandfds_complex(struct ast_channel **c, int n, int *ms)
3308
{
3308
{
3309
	struct timeval start = { 0 , 0 };
3309
	struct timeval start = { 0 , 0 };
3310
	int res = 0, i;
3310
	int res = 0, i;
3311
	struct epoll_event ev[25] = { { 0, } };
3311
	struct epoll_event ev[25] = { { 0, } };
3312
	struct timeval now = { 0, 0 };
3312
	struct timeval now = { 0, 0 };
3313
	long whentohangup = 0, diff = 0, rms = *ms;
3313
	long whentohangup = 0, diff = 0, rms = *ms;
3314
	struct ast_channel *winner = NULL;
3314
	struct ast_channel *winner = NULL;
3315

    
   
3315

   
3316
	for (i = 0; i < n; i++) {
3316
	for (i = 0; i < n; i++) {
3317
		ast_channel_lock(c[i]);
3317
		ast_channel_lock(c[i]);
3318
		if (!ast_tvzero(*ast_channel_whentohangup(c[i]))) {
3318
		if (!ast_tvzero(*ast_channel_whentohangup(c[i]))) {
3319
			if (whentohangup == 0) {
3319
			if (whentohangup == 0) {
3320
				now = ast_tvnow();
3320
				now = ast_tvnow();
3321
			}
3321
			}
3322
			if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(c[i]), now)) < 0) {
3322
			if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(c[i]), now)) < 0) {
3323
				ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3323
				ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3324
				ast_channel_unlock(c[i]);
3324
				ast_channel_unlock(c[i]);
3325
				return c[i];
3325
				return c[i];
3326
			}
3326
			}
3327
			if (!whentohangup || whentohangup > diff) {
3327
			if (!whentohangup || whentohangup > diff) {
3328
				whentohangup = diff;
3328
				whentohangup = diff;
3329
			}
3329
			}
3330
		}
3330
		}
3331
		ast_channel_unlock(c[i]);
3331
		ast_channel_unlock(c[i]);
3332
		CHECK_BLOCKING(c[i]);
3332
		CHECK_BLOCKING(c[i]);
3333
	}
3333
	}
3334

    
   
3334

   
3335
	rms = *ms;
3335
	rms = *ms;
3336
	if (whentohangup) {
3336
	if (whentohangup) {
3337
		rms = whentohangup;
3337
		rms = whentohangup;
3338
		if (*ms >= 0 && *ms < rms) {
3338
		if (*ms >= 0 && *ms < rms) {
3339
			rms = *ms;
3339
			rms = *ms;
3340
		}
3340
		}
3341
	}
3341
	}
3342

    
   
3342

   
3343
	if (*ms > 0) {
3343
	if (*ms > 0) {
3344
		start = ast_tvnow();
3344
		start = ast_tvnow();
3345
	}
3345
	}
3346

    
   
3346

   
3347
	res = epoll_wait(ast_channel_epfd(c[0]), ev, 25, rms);
3347
	res = epoll_wait(ast_channel_epfd(c[0]), ev, 25, rms);
3348

    
   
3348

   
3349
	for (i = 0; i < n; i++) {
3349
	for (i = 0; i < n; i++) {
3350
		ast_clear_flag(ast_channel_flags(c[i]), AST_FLAG_BLOCKING);
3350
		ast_clear_flag(ast_channel_flags(c[i]), AST_FLAG_BLOCKING);
3351
	}
3351
	}
3352

    
   
3352

   
3353
	if (res < 0) {
3353
	if (res < 0) {
3354
		if (errno != EINTR) {
3354
		if (errno != EINTR) {
3355
			*ms = -1;
3355
			*ms = -1;
3356
		}
3356
		}
3357
		return NULL;
3357
		return NULL;
3358
	}
3358
	}
3359

    
   
3359

   
3360
	if (whentohangup) {
3360
	if (whentohangup) {
3361
		now = ast_tvnow();
3361
		now = ast_tvnow();
3362
		for (i = 0; i < n; i++) {
3362
		for (i = 0; i < n; i++) {
3363
			if (!ast_tvzero(*ast_channel_whentohangup(c[i])) && ast_tvdiff_ms(now, *ast_channel_whentohangup(c[i])) >= 0) {
3363
			if (!ast_tvzero(*ast_channel_whentohangup(c[i])) && ast_tvdiff_ms(now, *ast_channel_whentohangup(c[i])) >= 0) {
3364
				ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3364
				ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3365
				if (!winner) {
3365
				if (!winner) {
3366
					winner = c[i];
3366
					winner = c[i];
3367
				}
3367
				}
3368
			}
3368
			}
3369
		}
3369
		}
3370
	}
3370
	}
3371

    
   
3371

   
3372
	if (!res) {
3372
	if (!res) {
3373
		*ms = 0;
3373
		*ms = 0;
3374
		return winner;
3374
		return winner;
3375
	}
3375
	}
3376

    
   
3376

   
3377
	for (i = 0; i < res; i++) {
3377
	for (i = 0; i < res; i++) {
3378
		struct ast_epoll_data *aed = ev[i].data.ptr;
3378
		struct ast_epoll_data *aed = ev[i].data.ptr;
3379

    
   
3379

   
3380
		if (!ev[i].events || !aed) {
3380
		if (!ev[i].events || !aed) {
3381
			continue;
3381
			continue;
3382
		}
3382
		}
3383

    
   
3383

   
3384
		winner = aed->chan;
3384
		winner = aed->chan;
3385
		if (ev[i].events & EPOLLPRI) {
3385
		if (ev[i].events & EPOLLPRI) {
3386
			ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3386
			ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3387
		} else {
3387
		} else {
3388
			ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3388
			ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3389
		}
3389
		}
3390
		ast_channel_fdno_set(winner, aed->which);
3390
		ast_channel_fdno_set(winner, aed->which);
3391
	}
3391
	}
3392

    
   
3392

   
3393
	if (*ms > 0) {
3393
	if (*ms > 0) {
3394
		*ms -= ast_tvdiff_ms(ast_tvnow(), start);
3394
		*ms -= ast_tvdiff_ms(ast_tvnow(), start);
3395
		if (*ms < 0) {
3395
		if (*ms < 0) {
3396
			*ms = 0;
3396
			*ms = 0;
3397
		}
3397
		}
3398
	}
3398
	}
3399

    
   
3399

   
3400
	return winner;
3400
	return winner;
3401
}
3401
}
3402

    
   
3402

   
3403
struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3403
struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3404
					int *exception, int *outfd, int *ms)
3404
					int *exception, int *outfd, int *ms)
3405
{
3405
{
3406
	/* Clear all provided values in one place. */
3406
	/* Clear all provided values in one place. */
3407
	if (outfd) {
3407
	if (outfd) {
3408
		*outfd = -99999;
3408
		*outfd = -99999;
3409
	}
3409
	}
3410
	if (exception) {
3410
	if (exception) {
3411
		*exception = 0;
3411
		*exception = 0;
3412
	}
3412
	}
3413

    
   
3413

   
3414
	/* If no epoll file descriptor is available resort to classic nandfds */
3414
	/* If no epoll file descriptor is available resort to classic nandfds */
3415
	if (!n || nfds || ast_channel_epfd(c[0]) == -1) {
3415
	if (!n || nfds || ast_channel_epfd(c[0]) == -1) {
3416
		return ast_waitfor_nandfds_classic(c, n, fds, nfds, exception, outfd, ms);
3416
		return ast_waitfor_nandfds_classic(c, n, fds, nfds, exception, outfd, ms);
3417
	} else if (!nfds && n == 1) {
3417
	} else if (!nfds && n == 1) {
3418
		return ast_waitfor_nandfds_simple(c[0], ms);
3418
		return ast_waitfor_nandfds_simple(c[0], ms);
3419
	} else {
3419
	} else {
3420
		return ast_waitfor_nandfds_complex(c, n, ms);
3420
		return ast_waitfor_nandfds_complex(c, n, ms);
3421
	}
3421
	}
3422
}
3422
}
3423
#endif
3423
#endif
3424

    
   
3424

   
3425
struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3425
struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3426
{
3426
{
3427
	return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3427
	return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3428
}
3428
}
3429

    
   
3429

   
3430
int ast_waitfor(struct ast_channel *c, int ms)
3430
int ast_waitfor(struct ast_channel *c, int ms)
3431
{
3431
{
3432
	if (ms < 0) {
3432
	if (ms < 0) {
3433
		do {
3433
		do {
3434
			ms = 100000;
3434
			ms = 100000;
3435
			ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3435
			ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3436
		} while (!ms);
3436
		} while (!ms);
3437
	} else {
3437
	} else {
3438
		ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3438
		ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3439
	}
3439
	}
3440
	return ms;
3440
	return ms;
3441
}
3441
}
3442

    
   
3442

   
3443
int ast_waitfordigit(struct ast_channel *c, int ms)
3443
int ast_waitfordigit(struct ast_channel *c, int ms)
3444
{
3444
{
3445
	return ast_waitfordigit_full(c, ms, -1, -1);
3445
	return ast_waitfordigit_full(c, ms, -1, -1);
3446
}
3446
}
3447

    
   
3447

   
3448
int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3448
int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3449
{
3449
{
3450
	return ast_settimeout_full(c, rate, func, data, 0);
3450
	return ast_settimeout_full(c, rate, func, data, 0);
3451
}
3451
}
3452

    
   
3452

   
3453
int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data, unsigned int is_ao2_obj)
3453
int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data, unsigned int is_ao2_obj)
3454
{
3454
{
3455
	int res;
3455
	int res;
3456
	unsigned int real_rate = rate, max_rate;
3456
	unsigned int real_rate = rate, max_rate;
3457

    
   
3457

   
3458
	ast_channel_lock(c);
3458
	ast_channel_lock(c);
3459

    
   
3459

   
3460
	if (ast_channel_timingfd(c) == -1) {
3460
	if (ast_channel_timingfd(c) == -1) {
3461
		ast_channel_unlock(c);
3461
		ast_channel_unlock(c);
3462
		return -1;
3462
		return -1;
3463
	}
3463
	}
3464

    
   
3464

   
3465
	if (!func) {
3465
	if (!func) {
3466
		rate = 0;
3466
		rate = 0;
3467
		data = NULL;
3467
		data = NULL;
3468
	}
3468
	}
3469

    
   
3469

   
3470
	if (rate && rate > (max_rate = ast_timer_get_max_rate(ast_channel_timer(c)))) {
3470
	if (rate && rate > (max_rate = ast_timer_get_max_rate(ast_channel_timer(c)))) {
3471
		real_rate = max_rate;
3471
		real_rate = max_rate;
3472
	}
3472
	}
3473

    
   
3473

   
3474
	ast_debug(1, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3474
	ast_debug(1, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3475

    
   
3475

   
3476
	res = ast_timer_set_rate(ast_channel_timer(c), real_rate);
3476
	res = ast_timer_set_rate(ast_channel_timer(c), real_rate);
3477

    
   
3477

   
3478
	if (ast_channel_timingdata(c) && ast_test_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3478
	if (ast_channel_timingdata(c) && ast_test_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3479
		ao2_ref(ast_channel_timingdata(c), -1);
3479
		ao2_ref(ast_channel_timingdata(c), -1);
3480
	}
3480
	}
3481

    
   
3481

   
3482
	ast_channel_timingfunc_set(c, func);
3482
	ast_channel_timingfunc_set(c, func);
3483
	ast_channel_timingdata_set(c, data);
3483
	ast_channel_timingdata_set(c, data);
3484

    
   
3484

   
3485
	if (data && is_ao2_obj) {
3485
	if (data && is_ao2_obj) {
3486
		ao2_ref(data, 1);
3486
		ao2_ref(data, 1);
3487
		ast_set_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3487
		ast_set_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3488
	} else {
3488
	} else {
3489
		ast_clear_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3489
		ast_clear_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3490
	}
3490
	}
3491

    
   
3491

   
3492
	if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
3492
	if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
3493
		/* Clearing the timing func and setting the rate to 0
3493
		/* Clearing the timing func and setting the rate to 0
3494
		 * means that we don't want to be reading from the timingfd
3494
		 * means that we don't want to be reading from the timingfd
3495
		 * any more. Setting c->fdno to -1 means we won't have any
3495
		 * any more. Setting c->fdno to -1 means we won't have any
3496
		 * errant reads from the timingfd, meaning we won't potentially
3496
		 * errant reads from the timingfd, meaning we won't potentially
3497
		 * miss any important frames.
3497
		 * miss any important frames.
3498
		 */
3498
		 */
3499
		ast_channel_fdno_set(c, -1);
3499
		ast_channel_fdno_set(c, -1);
3500
	}
3500
	}
3501

    
   
3501

   
3502
	ast_channel_unlock(c);
3502
	ast_channel_unlock(c);
3503

    
   
3503

   
3504
	return res;
3504
	return res;
3505
}
3505
}
3506

    
   
3506

   
3507
int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, int cmdfd)
3507
int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, int cmdfd)
3508
{
3508
{
3509
	struct timeval start = ast_tvnow();
3509
	struct timeval start = ast_tvnow();
3510
	int ms;
3510
	int ms;
3511

    
   
3511

   
3512
	/* Stop if we're a zombie or need a soft hangup */
3512
	/* Stop if we're a zombie or need a soft hangup */
3513
	if (ast_test_flag(ast_channel_flags(c), AST_FLAG_ZOMBIE) || ast_check_hangup(c))
3513
	if (ast_test_flag(ast_channel_flags(c), AST_FLAG_ZOMBIE) || ast_check_hangup(c))
3514
		return -1;
3514
		return -1;
3515

    
   
3515

   
3516
	/* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3516
	/* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3517
	ast_set_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3517
	ast_set_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3518

    
   
3518

   
3519
	/* Wait for a digit, no more than timeout_ms milliseconds total.
3519
	/* Wait for a digit, no more than timeout_ms milliseconds total.
3520
	 * Or, wait indefinitely if timeout_ms is <0.
3520
	 * Or, wait indefinitely if timeout_ms is <0.
3521
	 */
3521
	 */
3522
	while ((ms = ast_remaining_ms(start, timeout_ms))) {
3522
	while ((ms = ast_remaining_ms(start, timeout_ms))) {
3523
		struct ast_channel *rchan;
3523
		struct ast_channel *rchan;
3524
		int outfd = -1;
3524
		int outfd = -1;
3525

    
   
3525

   
3526
		errno = 0;
3526
		errno = 0;
3527
		/* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
3527
		/* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
3528
		 * it is unhelpful if it waited less than a millisecond.
3528
		 * it is unhelpful if it waited less than a millisecond.
3529
		 */
3529
		 */
3530
		rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3530
		rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3531

    
   
3531

   
3532
		if (!rchan && outfd < 0 && ms) {
3532
		if (!rchan && outfd < 0 && ms) {
3533
			if (errno == 0 || errno == EINTR)
3533
			if (errno == 0 || errno == EINTR)
3534
				continue;
3534
				continue;
3535
			ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3535
			ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3536
			ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3536
			ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3537
			return -1;
3537
			return -1;
3538
		} else if (outfd > -1) {
3538
		} else if (outfd > -1) {
3539
			/* The FD we were watching has something waiting */
3539
			/* The FD we were watching has something waiting */
3540
			ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3540
			ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3541
			ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3541
			ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3542
			return 1;
3542
			return 1;
3543
		} else if (rchan) {
3543
		} else if (rchan) {
3544
			int res;
3544
			int res;
3545
			struct ast_frame *f = ast_read(c);
3545
			struct ast_frame *f = ast_read(c);
3546
			if (!f)
3546
			if (!f)
3547
				return -1;
3547
				return -1;
3548

    
   
3548

   
3549
			switch (f->frametype) {
3549
			switch (f->frametype) {
3550
			case AST_FRAME_DTMF_BEGIN:
3550
			case AST_FRAME_DTMF_BEGIN:
3551
				break;
3551
				break;
3552
			case AST_FRAME_DTMF_END:
3552
			case AST_FRAME_DTMF_END:
3553
				res = f->subclass.integer;
3553
				res = f->subclass.integer;
3554
				ast_frfree(f);
3554
				ast_frfree(f);
3555
				ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3555
				ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3556
				return res;
3556
				return res;
3557
			case AST_FRAME_CONTROL:
3557
			case AST_FRAME_CONTROL:
3558
				switch (f->subclass.integer) {
3558
				switch (f->subclass.integer) {
3559
				case AST_CONTROL_HANGUP:
3559
				case AST_CONTROL_HANGUP:
3560
					ast_frfree(f);
3560
					ast_frfree(f);
3561
					ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3561
					ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3562
					return -1;
3562
					return -1;
3563
				case AST_CONTROL_STREAM_STOP:
3563
				case AST_CONTROL_STREAM_STOP:
3564
				case AST_CONTROL_STREAM_SUSPEND:
3564
				case AST_CONTROL_STREAM_SUSPEND:
3565
				case AST_CONTROL_STREAM_RESTART:
3565
				case AST_CONTROL_STREAM_RESTART:
3566
				case AST_CONTROL_STREAM_REVERSE:
3566
				case AST_CONTROL_STREAM_REVERSE:
3567
				case AST_CONTROL_STREAM_FORWARD:
3567
				case AST_CONTROL_STREAM_FORWARD:
3568
					/* Fall-through and treat as if it were a DTMF signal. Items
3568
					/* Fall-through and treat as if it were a DTMF signal. Items
3569
					 * that perform stream control will handle this. */
3569
					 * that perform stream control will handle this. */
3570
					res = f->subclass.integer;
3570
					res = f->subclass.integer;
3571
					ast_frfree(f);
3571
					ast_frfree(f);
3572
					ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3572
					ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3573
					return res;
3573
					return res;
3574
				case AST_CONTROL_PVT_CAUSE_CODE:
3574
				case AST_CONTROL_PVT_CAUSE_CODE:
3575
				case AST_CONTROL_RINGING:
3575
				case AST_CONTROL_RINGING:
3576
				case AST_CONTROL_ANSWER:
3576
				case AST_CONTROL_ANSWER:
3577
				case AST_CONTROL_SRCUPDATE:
3577
				case AST_CONTROL_SRCUPDATE:
3578
				case AST_CONTROL_SRCCHANGE:
3578
				case AST_CONTROL_SRCCHANGE:
3579
				case AST_CONTROL_CONNECTED_LINE:
3579
				case AST_CONTROL_CONNECTED_LINE:
3580
				case AST_CONTROL_REDIRECTING:
3580
				case AST_CONTROL_REDIRECTING:
3581
				case AST_CONTROL_UPDATE_RTP_PEER:
3581
				case AST_CONTROL_UPDATE_RTP_PEER:
3582
				case AST_CONTROL_HOLD:
3582
				case AST_CONTROL_HOLD:
3583
				case AST_CONTROL_UNHOLD:
3583
				case AST_CONTROL_UNHOLD:
3584
				case -1:
3584
				case -1:
3585
					/* Unimportant */
3585
					/* Unimportant */
3586
					break;
3586
					break;
3587
				default:
3587
				default:
3588
					ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3588
					ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3589
					break;
3589
					break;
3590
				}
3590
				}
3591
				break;
3591
				break;
3592
			case AST_FRAME_VOICE:
3592
			case AST_FRAME_VOICE:
3593
				/* Write audio if appropriate */
3593
				/* Write audio if appropriate */
3594
				if (audiofd > -1) {
3594
				if (audiofd > -1) {
3595
					if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3595
					if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3596
						ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3596
						ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3597
					}
3597
					}
3598
				}
3598
				}
3599
			default:
3599
			default:
3600
				/* Ignore */
3600
				/* Ignore */
3601
				break;
3601
				break;
3602
			}
3602
			}
3603
			ast_frfree(f);
3603
			ast_frfree(f);
3604
		}
3604
		}
3605
	}
3605
	}
3606

    
   
3606

   
3607
	ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3607
	ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3608

    
   
3608

   
3609
	return 0; /* Time is up */
3609
	return 0; /* Time is up */
3610
}
3610
}
3611

    
   
3611

   
3612
enum DtmfDirection {
3612
enum DtmfDirection {
3613
	DTMF_RECEIVED,
3613
	DTMF_RECEIVED,
3614
	DTMF_SENT
3614
	DTMF_SENT
3615
};
3615
};
3616

    
   
3616

   
3617
static const char *dtmf_direction_to_string(enum DtmfDirection direction)
3617
static const char *dtmf_direction_to_string(enum DtmfDirection direction)
3618
{
3618
{
3619
	switch (direction) {
3619
	switch (direction) {
3620
	case DTMF_RECEIVED:
3620
	case DTMF_RECEIVED:
3621
		return "Received";
3621
		return "Received";
3622
	case DTMF_SENT:
3622
	case DTMF_SENT:
3623
		return "Sent";
3623
		return "Sent";
3624
	}
3624
	}
3625

    
   
3625

   
3626
	return "?";
3626
	return "?";
3627
}
3627
}
3628

    
   
3628

   
3629
static void send_dtmf_begin_event(struct ast_channel *chan,
3629
static void send_dtmf_begin_event(struct ast_channel *chan,
3630
	enum DtmfDirection direction, const char digit)
3630
	enum DtmfDirection direction, const char digit)
3631
{
3631
{
3632
	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3632
	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3633
	char digit_str[] = { digit, '\0' };
3633
	char digit_str[] = { digit, '\0' };
3634

    
   
3634

   
3635
	blob = ast_json_pack("{ s: s, s: s }",
3635
	blob = ast_json_pack("{ s: s, s: s }",
3636
		"digit", digit_str,
3636
		"digit", digit_str,
3637
		"direction", dtmf_direction_to_string(direction));
3637
		"direction", dtmf_direction_to_string(direction));
3638
	if (!blob) {
3638
	if (!blob) {
3639
		return;
3639
		return;
3640
	}
3640
	}
3641

    
   
3641

   
3642
	ast_channel_publish_blob(chan, ast_channel_dtmf_begin_type(), blob);
3642
	ast_channel_publish_blob(chan, ast_channel_dtmf_begin_type(), blob);
3643
}
3643
}
3644

    
   
3644

   
3645
static void send_dtmf_end_event(struct ast_channel *chan,
3645
static void send_dtmf_end_event(struct ast_channel *chan,
3646
	enum DtmfDirection direction, const char digit, long duration_ms)
3646
	enum DtmfDirection direction, const char digit, long duration_ms)
3647
{
3647
{
3648
	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3648
	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3649
	char digit_str[] = { digit, '\0' };
3649
	char digit_str[] = { digit, '\0' };
3650

    
   
3650

   
3651
	blob = ast_json_pack("{ s: s, s: s, s: i }",
3651
	blob = ast_json_pack("{ s: s, s: s, s: i }",
3652
		"digit", digit_str,
3652
		"digit", digit_str,
3653
		"direction", dtmf_direction_to_string(direction),
3653
		"direction", dtmf_direction_to_string(direction),
3654
		"duration_ms", duration_ms);
3654
		"duration_ms", duration_ms);
3655
	if (!blob) {
3655
	if (!blob) {
3656
		return;
3656
		return;
3657
	}
3657
	}
3658

    
   
3658

   
3659
	ast_channel_publish_blob(chan, ast_channel_dtmf_end_type(), blob);
3659
	ast_channel_publish_blob(chan, ast_channel_dtmf_end_type(), blob);
3660
}
3660
}
3661

    
   
3661

   
3662
static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3662
static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3663
{
3663
{
3664
	struct ast_generator *generator;
3664
	struct ast_generator *generator;
3665
	void *gendata;
3665
	void *gendata;
3666
	int res;
3666
	int res;
3667
	int samples;
3667
	int samples;
3668

    
   
3668

   
3669
	generator = ast_channel_generator(chan);
3669
	generator = ast_channel_generator(chan);
3670
	if (!generator
3670
	if (!generator
3671
		|| !generator->generate
3671
		|| !generator->generate
3672
		|| f->frametype != AST_FRAME_VOICE
3672
		|| f->frametype != AST_FRAME_VOICE
3673
		|| !ast_channel_generatordata(chan)
3673
		|| !ast_channel_generatordata(chan)
3674
		|| ast_channel_timingfunc(chan)) {
3674
		|| ast_channel_timingfunc(chan)) {
3675
		return;
3675
		return;
3676
	}
3676
	}
3677

    
   
3677

   
3678
	/*
3678
	/*
3679
	 * We must generate frames in phase locked mode since
3679
	 * We must generate frames in phase locked mode since
3680
	 * we have no internal timer available.
3680
	 * we have no internal timer available.
3681
	 */
3681
	 */
3682

    
   
3682

   
3683
	if (ast_format_cmp(&f->subclass.format, ast_channel_writeformat(chan)) == AST_FORMAT_CMP_NOT_EQUAL) {
3683
	if (ast_format_cmp(&f->subclass.format, ast_channel_writeformat(chan)) == AST_FORMAT_CMP_NOT_EQUAL) {
3684
		float factor;
3684
		float factor;
3685

    
   
3685

   
3686
		factor = ((float) ast_format_rate(ast_channel_writeformat(chan))) / ((float) ast_format_rate(&f->subclass.format));
3686
		factor = ((float) ast_format_rate(ast_channel_writeformat(chan))) / ((float) ast_format_rate(&f->subclass.format));
3687
		samples = (int) (((float) f->samples) * factor);
3687
		samples = (int) (((float) f->samples) * factor);
3688
	} else {
3688
	} else {
3689
		samples = f->samples;
3689
		samples = f->samples;
3690
	}
3690
	}
3691

    
   
3691

   
3692
	gendata = ast_channel_generatordata(chan);
3692
	gendata = ast_channel_generatordata(chan);
3693
	ast_channel_generatordata_set(chan, NULL);     /* reset, to let writes go through */
3693
	ast_channel_generatordata_set(chan, NULL);     /* reset, to let writes go through */
3694

    
   
3694

   
3695
	/*
3695
	/*
3696
	 * This unlock is here based on two assumptions that hold true at
3696
	 * This unlock is here based on two assumptions that hold true at
3697
	 * this point in the code. 1) this function is only called from
3697
	 * this point in the code. 1) this function is only called from
3698
	 * within __ast_read() and 2) all generators call ast_write() in
3698
	 * within __ast_read() and 2) all generators call ast_write() in
3699
	 * their generate callback.
3699
	 * their generate callback.
3700
	 *
3700
	 *
3701
	 * The reason this is added is so that when ast_write is called,
3701
	 * The reason this is added is so that when ast_write is called,
3702
	 * the lock that occurs there will not recursively lock the
3702
	 * the lock that occurs there will not recursively lock the
3703
	 * channel.  Doing this will allow deadlock avoidance to work in
3703
	 * channel.  Doing this will allow deadlock avoidance to work in
3704
	 * deeper functions.
3704
	 * deeper functions.
3705
	 */
3705
	 */
3706
	ast_channel_unlock(chan);
3706
	ast_channel_unlock(chan);
3707
	res = generator->generate(chan, gendata, f->datalen, samples);
3707
	res = generator->generate(chan, gendata, f->datalen, samples);
3708
	ast_channel_lock(chan);
3708
	ast_channel_lock(chan);
3709
	if (generator == ast_channel_generator(chan)) {
3709
	if (generator == ast_channel_generator(chan)) {
3710
		ast_channel_generatordata_set(chan, gendata);
3710
		ast_channel_generatordata_set(chan, gendata);
3711
		if (res) {
3711
		if (res) {
3712
			ast_debug(1, "Auto-deactivating generator\n");
3712
			ast_debug(1, "Auto-deactivating generator\n");
3713
			ast_deactivate_generator(chan);
3713
			ast_deactivate_generator(chan);
3714
		}
3714
		}
3715
	}
3715
	}
3716
}
3716
}
3717

    
   
3717

   
3718
static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3718
static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3719
{
3719
{
3720
	struct ast_frame *fr = ast_channel_dtmff(chan);
3720
	struct ast_frame *fr = ast_channel_dtmff(chan);
3721

    
   
3721

   
3722
	fr->frametype = AST_FRAME_DTMF_END;
3722
	fr->frametype = AST_FRAME_DTMF_END;
3723
	fr->subclass.integer = f->subclass.integer;
3723
	fr->subclass.integer = f->subclass.integer;
3724
	fr->len = f->len;
3724
	fr->len = f->len;
3725

    
   
3725

   
3726
	/* The only time this function will be called is for a frame that just came
3726
	/* The only time this function will be called is for a frame that just came
3727
	 * out of the channel driver.  So, we want to stick it on the tail of the
3727
	 * out of the channel driver.  So, we want to stick it on the tail of the
3728
	 * readq. */
3728
	 * readq. */
3729

    
   
3729

   
3730
	ast_queue_frame(chan, fr);
3730
	ast_queue_frame(chan, fr);
3731
}
3731
}
3732

    
   
3732

   
3733
/*!
3733
/*!
3734
 * \brief Determine whether or not we should ignore DTMF in the readq
3734
 * \brief Determine whether or not we should ignore DTMF in the readq
3735
 */
3735
 */
3736
static inline int should_skip_dtmf(struct ast_channel *chan)
3736
static inline int should_skip_dtmf(struct ast_channel *chan)
3737
{
3737
{
3738
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_EMULATE_DTMF)) {
3738
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_EMULATE_DTMF)) {
3739
		/* We're in the middle of emulating a digit, or DTMF has been
3739
		/* We're in the middle of emulating a digit, or DTMF has been
3740
		 * explicitly deferred.  Skip this digit, then. */
3740
		 * explicitly deferred.  Skip this digit, then. */
3741
		return 1;
3741
		return 1;
3742
	}
3742
	}
3743

    
   
3743

   
3744
	if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3744
	if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3745
			ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
3745
			ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
3746
		/* We're not in the middle of a digit, but it hasn't been long enough
3746
		/* We're not in the middle of a digit, but it hasn't been long enough
3747
		 * since the last digit, so we'll have to skip DTMF for now. */
3747
		 * since the last digit, so we'll have to skip DTMF for now. */
3748
		return 1;
3748
		return 1;
3749
	}
3749
	}
3750

    
   
3750

   
3751
	return 0;
3751
	return 0;
3752
}
3752
}
3753

    
   
3753

   
3754
/*!
3754
/*!
3755
 * \brief calculates the number of samples to jump forward with in a monitor stream.
3755
 * \brief calculates the number of samples to jump forward with in a monitor stream.
3756

    
   
3756

   
3757
 * \note When using ast_seekstream() with the read and write streams of a monitor,
3757
 * \note When using ast_seekstream() with the read and write streams of a monitor,
3758
 * the number of samples to seek forward must be of the same sample rate as the stream
3758
 * the number of samples to seek forward must be of the same sample rate as the stream
3759
 * or else the jump will not be calculated correctly.
3759
 * or else the jump will not be calculated correctly.
3760
 *
3760
 *
3761
 * \retval number of samples to seek forward after rate conversion.
3761
 * \retval number of samples to seek forward after rate conversion.
3762
 */
3762
 */
3763
static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
3763
static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
3764
{
3764
{
3765
	int diff = sample_rate - seek_rate;
3765
	int diff = sample_rate - seek_rate;
3766

    
   
3766

   
3767
	if (diff > 0) {
3767
	if (diff > 0) {
3768
		samples = samples / (float) (sample_rate / seek_rate);
3768
		samples = samples / (float) (sample_rate / seek_rate);
3769
	} else if (diff < 0) {
3769
	} else if (diff < 0) {
3770
		samples = samples * (float) (seek_rate / sample_rate);
3770
		samples = samples * (float) (seek_rate / sample_rate);
3771
	}
3771
	}
3772

    
   
3772

   
3773
	return samples;
3773
	return samples;
3774
}
3774
}
3775

    
   
3775

   
3776
static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
3776
static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
3777
{
3777
{
3778
	struct ast_frame *f = NULL;	/* the return value */
3778
	struct ast_frame *f = NULL;	/* the return value */
3779
	int prestate;
3779
	int prestate;
3780
	int cause = 0;
3780
	int cause = 0;
3781

    
   
3781

   
3782
	/* this function is very long so make sure there is only one return
3782
	/* this function is very long so make sure there is only one return
3783
	 * point at the end (there are only two exceptions to this).
3783
	 * point at the end (there are only two exceptions to this).
3784
	 */
3784
	 */
3785
	ast_channel_lock(chan);
3785
	ast_channel_lock(chan);
3786

    
   
3786

   
3787
	/* Stop if we're a zombie or need a soft hangup */
3787
	/* Stop if we're a zombie or need a soft hangup */
3788
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
3788
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
3789
		if (ast_channel_generator(chan))
3789
		if (ast_channel_generator(chan))
3790
			ast_deactivate_generator(chan);
3790
			ast_deactivate_generator(chan);
3791

    
   
3791

   
3792
		/*
3792
		/*
3793
		 * It is possible for chan->_softhangup to be set and there
3793
		 * It is possible for chan->_softhangup to be set and there
3794
		 * still be control frames that need to be read.  Instead of
3794
		 * still be control frames that need to be read.  Instead of
3795
		 * just going to 'done' in the case of ast_check_hangup(), we
3795
		 * just going to 'done' in the case of ast_check_hangup(), we
3796
		 * need to queue the end-of-Q frame so that it can mark the end
3796
		 * need to queue the end-of-Q frame so that it can mark the end
3797
		 * of the read queue.  If there are frames to be read,
3797
		 * of the read queue.  If there are frames to be read,
3798
		 * ast_queue_control() will be called repeatedly, but will only
3798
		 * ast_queue_control() will be called repeatedly, but will only
3799
		 * queue the first end-of-Q frame.
3799
		 * queue the first end-of-Q frame.
3800
		 */
3800
		 */
3801
		if (ast_channel_softhangup_internal_flag(chan)) {
3801
		if (ast_channel_softhangup_internal_flag(chan)) {
3802
			ast_queue_control(chan, AST_CONTROL_END_OF_Q);
3802
			ast_queue_control(chan, AST_CONTROL_END_OF_Q);
3803
		} else {
3803
		} else {
3804
			goto done;
3804
			goto done;
3805
		}
3805
		}
3806
	} else {
3806
	} else {
3807
#ifdef AST_DEVMODE
3807
#ifdef AST_DEVMODE
3808
		/*
3808
		/*
3809
		 * The ast_waitfor() code records which of the channel's file
3809
		 * The ast_waitfor() code records which of the channel's file
3810
		 * descriptors reported that data is available.  In theory,
3810
		 * descriptors reported that data is available.  In theory,
3811
		 * ast_read() should only be called after ast_waitfor() reports
3811
		 * ast_read() should only be called after ast_waitfor() reports
3812
		 * that a channel has data available for reading.  However,
3812
		 * that a channel has data available for reading.  However,
3813
		 * there still may be some edge cases throughout the code where
3813
		 * there still may be some edge cases throughout the code where
3814
		 * ast_read() is called improperly.  This can potentially cause
3814
		 * ast_read() is called improperly.  This can potentially cause
3815
		 * problems, so if this is a developer build, make a lot of
3815
		 * problems, so if this is a developer build, make a lot of
3816
		 * noise if this happens so that it can be addressed.
3816
		 * noise if this happens so that it can be addressed.
3817
		 *
3817
		 *
3818
		 * One of the potential problems is blocking on a dead channel.
3818
		 * One of the potential problems is blocking on a dead channel.
3819
		 */
3819
		 */
3820
		if (ast_channel_fdno(chan) == -1) {
3820
		if (ast_channel_fdno(chan) == -1) {
3821
			ast_log(LOG_ERROR,
3821
			ast_log(LOG_ERROR,
3822
				"ast_read() on chan '%s' called with no recorded file descriptor.\n",
3822
				"ast_read() on chan '%s' called with no recorded file descriptor.\n",
3823
				ast_channel_name(chan));
3823
				ast_channel_name(chan));
3824
		}
3824
		}
3825
#endif
3825
#endif
3826
	}
3826
	}
3827

    
   
3827

   
3828
	prestate = ast_channel_state(chan);
3828
	prestate = ast_channel_state(chan);
3829

    
   
3829

   
3830
	if (ast_channel_timingfd(chan) > -1 && ast_channel_fdno(chan) == AST_TIMING_FD) {
3830
	if (ast_channel_timingfd(chan) > -1 && ast_channel_fdno(chan) == AST_TIMING_FD) {
3831
		enum ast_timer_event res;
3831
		enum ast_timer_event res;
3832

    
   
3832

   
3833
		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3833
		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3834

    
   
3834

   
3835
		res = ast_timer_get_event(ast_channel_timer(chan));
3835
		res = ast_timer_get_event(ast_channel_timer(chan));
3836

    
   
3836

   
3837
		switch (res) {
3837
		switch (res) {
3838
		case AST_TIMING_EVENT_EXPIRED:
3838
		case AST_TIMING_EVENT_EXPIRED:
3839
			if (ast_timer_ack(ast_channel_timer(chan), 1) < 0) {
3839
			if (ast_timer_ack(ast_channel_timer(chan), 1) < 0) {
3840
				ast_log(LOG_ERROR, "Failed to acknoweldge timer in ast_read\n");
3840
				ast_log(LOG_ERROR, "Failed to acknoweldge timer in ast_read\n");
3841
				goto done;
3841
				goto done;
3842
			}
3842
			}
3843

    
   
3843

   
3844
			if (ast_channel_timingfunc(chan)) {
3844
			if (ast_channel_timingfunc(chan)) {
3845
				/* save a copy of func/data before unlocking the channel */
3845
				/* save a copy of func/data before unlocking the channel */
3846
				ast_timing_func_t func = ast_channel_timingfunc(chan);
3846
				ast_timing_func_t func = ast_channel_timingfunc(chan);
3847
				void *data = ast_channel_timingdata(chan);
3847
				void *data = ast_channel_timingdata(chan);
3848
				int got_ref = 0;
3848
				int got_ref = 0;
3849
				if (data && ast_test_flag(ast_channel_flags(chan), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3849
				if (data && ast_test_flag(ast_channel_flags(chan), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3850
					ao2_ref(data, 1);
3850
					ao2_ref(data, 1);
3851
					got_ref = 1;
3851
					got_ref = 1;
3852
				}
3852
				}
3853
				ast_channel_fdno_set(chan, -1);
3853
				ast_channel_fdno_set(chan, -1);
3854
				ast_channel_unlock(chan);
3854
				ast_channel_unlock(chan);
3855
				func(data);
3855
				func(data);
3856
				if (got_ref) {
3856
				if (got_ref) {
3857
					ao2_ref(data, -1);
3857
					ao2_ref(data, -1);
3858
				}
3858
				}
3859
			} else {
3859
			} else {
3860
				ast_timer_set_rate(ast_channel_timer(chan), 0);
3860
				ast_timer_set_rate(ast_channel_timer(chan), 0);
3861
				ast_channel_fdno_set(chan, -1);
3861
				ast_channel_fdno_set(chan, -1);
3862
				ast_channel_unlock(chan);
3862
				ast_channel_unlock(chan);
3863
			}
3863
			}
3864

    
   
3864

   
3865
			/* cannot 'goto done' because the channel is already unlocked */
3865
			/* cannot 'goto done' because the channel is already unlocked */
3866
			return &ast_null_frame;
3866
			return &ast_null_frame;
3867

    
   
3867

   
3868
		case AST_TIMING_EVENT_CONTINUOUS:
3868
		case AST_TIMING_EVENT_CONTINUOUS:
3869
			if (AST_LIST_EMPTY(ast_channel_readq(chan)) ||
3869
			if (AST_LIST_EMPTY(ast_channel_readq(chan)) ||
3870
				!AST_LIST_NEXT(AST_LIST_FIRST(ast_channel_readq(chan)), frame_list)) {
3870
				!AST_LIST_NEXT(AST_LIST_FIRST(ast_channel_readq(chan)), frame_list)) {
3871
				ast_timer_disable_continuous(ast_channel_timer(chan));
3871
				ast_timer_disable_continuous(ast_channel_timer(chan));
3872
			}
3872
			}
3873
			break;
3873
			break;
3874
		}
3874
		}
3875

    
   
3875

   
3876
	} else if (ast_channel_fd_isset(chan, AST_GENERATOR_FD) && ast_channel_fdno(chan) == AST_GENERATOR_FD) {
3876
	} else if (ast_channel_fd_isset(chan, AST_GENERATOR_FD) && ast_channel_fdno(chan) == AST_GENERATOR_FD) {
3877
		/* if the AST_GENERATOR_FD is set, call the generator with args
3877
		/* if the AST_GENERATOR_FD is set, call the generator with args
3878
		 * set to -1 so it can do whatever it needs to.
3878
		 * set to -1 so it can do whatever it needs to.
3879
		 */
3879
		 */
3880
		void *tmp = ast_channel_generatordata(chan);
3880
		void *tmp = ast_channel_generatordata(chan);
3881
		ast_channel_generatordata_set(chan, NULL);     /* reset to let ast_write get through */
3881
		ast_channel_generatordata_set(chan, NULL);     /* reset to let ast_write get through */
3882
		ast_channel_generator(chan)->generate(chan, tmp, -1, -1);
3882
		ast_channel_generator(chan)->generate(chan, tmp, -1, -1);
3883
		ast_channel_generatordata_set(chan, tmp);
3883
		ast_channel_generatordata_set(chan, tmp);
3884
		f = &ast_null_frame;
3884
		f = &ast_null_frame;
3885
		ast_channel_fdno_set(chan, -1);
3885
		ast_channel_fdno_set(chan, -1);
3886
		goto done;
3886
		goto done;
3887
	} else if (ast_channel_fd_isset(chan, AST_JITTERBUFFER_FD) && ast_channel_fdno(chan) == AST_JITTERBUFFER_FD) {
3887
	} else if (ast_channel_fd_isset(chan, AST_JITTERBUFFER_FD) && ast_channel_fdno(chan) == AST_JITTERBUFFER_FD) {
3888
		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3888
		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3889
	}
3889
	}
3890

    
   
3890

   
3891
	/* Read and ignore anything on the alertpipe, but read only
3891
	/* Read and ignore anything on the alertpipe, but read only
3892
	   one sizeof(blah) per frame that we send from it */
3892
	   one sizeof(blah) per frame that we send from it */
3893
	if (ast_channel_internal_alert_read(chan) == AST_ALERT_READ_FATAL) {
3893
	if (ast_channel_internal_alert_read(chan) == AST_ALERT_READ_FATAL) {
3894
		f = &ast_null_frame;
3894
		f = &ast_null_frame;
3895
		goto done;
3895
		goto done;
3896
	}
3896
	}
3897

    
   
3897

   
3898
	/* Check for pending read queue */
3898
	/* Check for pending read queue */
3899
	if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
3899
	if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
3900
		int skip_dtmf = should_skip_dtmf(chan);
3900
		int skip_dtmf = should_skip_dtmf(chan);
3901

    
   
3901

   
3902
		AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), f, frame_list) {
3902
		AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), f, frame_list) {
3903
			/* We have to be picky about which frame we pull off of the readq because
3903
			/* We have to be picky about which frame we pull off of the readq because
3904
			 * there are cases where we want to leave DTMF frames on the queue until
3904
			 * there are cases where we want to leave DTMF frames on the queue until
3905
			 * some later time. */
3905
			 * some later time. */
3906

    
   
3906

   
3907
			if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
3907
			if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
3908
				continue;
3908
				continue;
3909
			}
3909
			}
3910

    
   
3910

   
3911
			AST_LIST_REMOVE_CURRENT(frame_list);
3911
			AST_LIST_REMOVE_CURRENT(frame_list);
3912
			break;
3912
			break;
3913
		}
3913
		}
3914
		AST_LIST_TRAVERSE_SAFE_END;
3914
		AST_LIST_TRAVERSE_SAFE_END;
3915

    
   
3915

   
3916
		if (!f) {
3916
		if (!f) {
3917
			/* There were no acceptable frames on the readq. */
3917
			/* There were no acceptable frames on the readq. */
3918
			f = &ast_null_frame;
3918
			f = &ast_null_frame;
3919
			ast_channel_alert_write(chan);
3919
			ast_channel_alert_write(chan);
3920
		}
3920
		}
3921

    
   
3921

   
3922
		/* Interpret hangup and end-of-Q frames to return NULL */
3922
		/* Interpret hangup and end-of-Q frames to return NULL */
3923
		/* XXX why not the same for frames from the channel ? */
3923
		/* XXX why not the same for frames from the channel ? */
3924
		if (f->frametype == AST_FRAME_CONTROL) {
3924
		if (f->frametype == AST_FRAME_CONTROL) {
3925
			switch (f->subclass.integer) {
3925
			switch (f->subclass.integer) {
3926
			case AST_CONTROL_HANGUP:
3926
			case AST_CONTROL_HANGUP:
3927
				ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
3927
				ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
3928
				cause = f->data.uint32;
3928
				cause = f->data.uint32;
3929
				/* Fall through */
3929
				/* Fall through */
3930
			case AST_CONTROL_END_OF_Q:
3930
			case AST_CONTROL_END_OF_Q:
3931
				ast_frfree(f);
3931
				ast_frfree(f);
3932
				f = NULL;
3932
				f = NULL;
3933
				break;
3933
				break;
3934
			default:
3934
			default:
3935
				break;
3935
				break;
3936
			}
3936
			}
3937
		}
3937
		}
3938
	} else {
3938
	} else {
3939
		ast_channel_blocker_set(chan, pthread_self());
3939
		ast_channel_blocker_set(chan, pthread_self());
3940
		if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION)) {
3940
		if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION)) {
3941
			if (ast_channel_tech(chan)->exception)
3941
			if (ast_channel_tech(chan)->exception)
3942
				f = ast_channel_tech(chan)->exception(chan);
3942
				f = ast_channel_tech(chan)->exception(chan);
3943
			else {
3943
			else {
3944
				ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", ast_channel_name(chan));
3944
				ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", ast_channel_name(chan));
3945
				f = &ast_null_frame;
3945
				f = &ast_null_frame;
3946
			}
3946
			}
3947
			/* Clear the exception flag */
3947
			/* Clear the exception flag */
3948
			ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3948
			ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3949
		} else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read)
3949
		} else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read)
3950
			f = ast_channel_tech(chan)->read(chan);
3950
			f = ast_channel_tech(chan)->read(chan);
3951
		else
3951
		else
3952
			ast_log(LOG_WARNING, "No read routine on channel %s\n", ast_channel_name(chan));
3952
			ast_log(LOG_WARNING, "No read routine on channel %s\n", ast_channel_name(chan));
3953
	}
3953
	}
3954

    
   
3954

   
3955
	/* Perform the framehook read event here. After the frame enters the framehook list
3955
	/* Perform the framehook read event here. After the frame enters the framehook list
3956
	 * there is no telling what will happen, <insert mad scientist laugh here>!!! */
3956
	 * there is no telling what will happen, <insert mad scientist laugh here>!!! */
3957
	f = ast_framehook_list_read_event(ast_channel_framehooks(chan), f);
3957
	f = ast_framehook_list_read_event(ast_channel_framehooks(chan), f);
3958

    
   
3958

   
3959
	/*
3959
	/*
3960
	 * Reset the recorded file descriptor that triggered this read so that we can
3960
	 * Reset the recorded file descriptor that triggered this read so that we can
3961
	 * easily detect when ast_read() is called without properly using ast_waitfor().
3961
	 * easily detect when ast_read() is called without properly using ast_waitfor().
3962
	 */
3962
	 */
3963
	ast_channel_fdno_set(chan, -1);
3963
	ast_channel_fdno_set(chan, -1);
3964

    
   
3964

   
3965
	if (f) {
3965
	if (f) {
3966
		struct ast_frame *readq_tail = AST_LIST_LAST(ast_channel_readq(chan));
3966
		struct ast_frame *readq_tail = AST_LIST_LAST(ast_channel_readq(chan));
3967
		struct ast_control_read_action_payload *read_action_payload;
3967
		struct ast_control_read_action_payload *read_action_payload;
3968
		struct ast_party_connected_line connected;
3968
		struct ast_party_connected_line connected;
3969

    
   
3969

   
3970
		/* if the channel driver returned more than one frame, stuff the excess
3970
		/* if the channel driver returned more than one frame, stuff the excess
3971
		   into the readq for the next ast_read call
3971
		   into the readq for the next ast_read call
3972
		*/
3972
		*/
3973
		if (AST_LIST_NEXT(f, frame_list)) {
3973
		if (AST_LIST_NEXT(f, frame_list)) {
3974
			ast_queue_frame(chan, AST_LIST_NEXT(f, frame_list));
3974
			ast_queue_frame(chan, AST_LIST_NEXT(f, frame_list));
3975
			ast_frfree(AST_LIST_NEXT(f, frame_list));
3975
			ast_frfree(AST_LIST_NEXT(f, frame_list));
3976
			AST_LIST_NEXT(f, frame_list) = NULL;
3976
			AST_LIST_NEXT(f, frame_list) = NULL;
3977
		}
3977
		}
3978

    
   
3978

   
3979
		switch (f->frametype) {
3979
		switch (f->frametype) {
3980
		case AST_FRAME_CONTROL:
3980
		case AST_FRAME_CONTROL:
3981
			if (f->subclass.integer == AST_CONTROL_ANSWER) {
3981
			if (f->subclass.integer == AST_CONTROL_ANSWER) {
3982
				if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
3982
				if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
3983
					ast_debug(1, "Ignoring answer on an inbound call!\n");
3983
					ast_debug(1, "Ignoring answer on an inbound call!\n");
3984
					ast_frfree(f);
3984
					ast_frfree(f);
3985
					f = &ast_null_frame;
3985
					f = &ast_null_frame;
3986
				} else if (prestate == AST_STATE_UP && ast_channel_is_bridged(chan)) {
3986
				} else if (prestate == AST_STATE_UP && ast_channel_is_bridged(chan)) {
3987
					ast_debug(1, "Dropping duplicate answer!\n");
3987
					ast_debug(1, "Dropping duplicate answer!\n");
3988
					ast_frfree(f);
3988
					ast_frfree(f);
3989
					f = &ast_null_frame;
3989
					f = &ast_null_frame;
3990
				} else {
3990
				} else {
3991
					/* Answer the CDR */
3991
					/* Answer the CDR */
3992
					ast_setstate(chan, AST_STATE_UP);
3992
					ast_setstate(chan, AST_STATE_UP);
3993
				}
3993
				}
3994
			} else if (f->subclass.integer == AST_CONTROL_READ_ACTION) {
3994
			} else if (f->subclass.integer == AST_CONTROL_READ_ACTION) {
3995
				read_action_payload = f->data.ptr;
3995
				read_action_payload = f->data.ptr;
3996
				switch (read_action_payload->action) {
3996
				switch (read_action_payload->action) {
3997
				case AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO:
3997
				case AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO:
3998
					ast_party_connected_line_init(&connected);
3998
					ast_party_connected_line_init(&connected);
3999
					ast_party_connected_line_copy(&connected, ast_channel_connected(chan));
3999
					ast_party_connected_line_copy(&connected, ast_channel_connected(chan));
4000
					if (ast_connected_line_parse_data(read_action_payload->payload,
4000
					if (ast_connected_line_parse_data(read_action_payload->payload,
4001
						read_action_payload->payload_size, &connected)) {
4001
						read_action_payload->payload_size, &connected)) {
4002
						ast_party_connected_line_free(&connected);
4002
						ast_party_connected_line_free(&connected);
4003
						break;
4003
						break;
4004
					}
4004
					}
4005
					ast_channel_unlock(chan);
4005
					ast_channel_unlock(chan);
4006
					if (ast_channel_connected_line_sub(NULL, chan, &connected, 0) &&
4006
					if (ast_channel_connected_line_sub(NULL, chan, &connected, 0) &&
4007
						ast_channel_connected_line_macro(NULL, chan, &connected, 1, 0)) {
4007
						ast_channel_connected_line_macro(NULL, chan, &connected, 1, 0)) {
4008
						ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE,
4008
						ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE,
4009
							read_action_payload->payload,
4009
							read_action_payload->payload,
4010
							read_action_payload->payload_size);
4010
							read_action_payload->payload_size);
4011
					}
4011
					}
4012
					ast_party_connected_line_free(&connected);
4012
					ast_party_connected_line_free(&connected);
4013
					ast_channel_lock(chan);
4013
					ast_channel_lock(chan);
4014
					break;
4014
					break;
4015
				}
4015
				}
4016
				ast_frfree(f);
4016
				ast_frfree(f);
4017
				f = &ast_null_frame;
4017
				f = &ast_null_frame;
4018
			}
4018
			}
4019
			break;
4019
			break;
4020
		case AST_FRAME_DTMF_END:
4020
		case AST_FRAME_DTMF_END:
4021
			send_dtmf_end_event(chan, DTMF_RECEIVED, f->subclass.integer, f->len);
4021
			send_dtmf_end_event(chan, DTMF_RECEIVED, f->subclass.integer, f->len);
4022
			ast_log(LOG_DTMF, "DTMF end '%c' received on %s, duration %ld ms\n", f->subclass.integer, ast_channel_name(chan), f->len);
4022
			ast_log(LOG_DTMF, "DTMF end '%c' received on %s, duration %ld ms\n", f->subclass.integer, ast_channel_name(chan), f->len);
4023
			/* Queue it up if DTMF is deferred, or if DTMF emulation is forced. */
4023
			/* Queue it up if DTMF is deferred, or if DTMF emulation is forced. */
4024
			if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF) || ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF)) {
4024
			if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF) || ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF)) {
4025
				queue_dtmf_readq(chan, f);
4025
				queue_dtmf_readq(chan, f);
4026
				ast_frfree(f);
4026
				ast_frfree(f);
4027
				f = &ast_null_frame;
4027
				f = &ast_null_frame;
4028
			} else if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF | AST_FLAG_END_DTMF_ONLY)) {
4028
			} else if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF | AST_FLAG_END_DTMF_ONLY)) {
4029
				if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
4029
				if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
4030
				    ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
4030
				    ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
4031
					/* If it hasn't been long enough, defer this digit */
4031
					/* If it hasn't been long enough, defer this digit */
4032
					queue_dtmf_readq(chan, f);
4032
					queue_dtmf_readq(chan, f);
4033
					ast_frfree(f);
4033
					ast_frfree(f);
4034
					f = &ast_null_frame;
4034
					f = &ast_null_frame;
4035
				} else {
4035
				} else {
4036
					/* There was no begin, turn this into a begin and send the end later */
4036
					/* There was no begin, turn this into a begin and send the end later */
4037
					struct timeval tv = ast_tvnow();
4037
					struct timeval tv = ast_tvnow();
4038
					f->frametype = AST_FRAME_DTMF_BEGIN;
4038
					f->frametype = AST_FRAME_DTMF_BEGIN;
4039
					ast_set_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF);
4039
					ast_set_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF);
4040
					ast_channel_dtmf_digit_to_emulate_set(chan, f->subclass.integer);
4040
					ast_channel_dtmf_digit_to_emulate_set(chan, f->subclass.integer);
4041
					ast_channel_dtmf_tv_set(chan, &tv);
4041
					ast_channel_dtmf_tv_set(chan, &tv);
4042
					if (f->len) {
4042
					if (f->len) {
4043
						if (f->len > option_dtmfminduration)
4043
						if (f->len > option_dtmfminduration)
4044
							ast_channel_emulate_dtmf_duration_set(chan, f->len);
4044
							ast_channel_emulate_dtmf_duration_set(chan, f->len);
4045
						else
4045
						else
4046
							ast_channel_emulate_dtmf_duration_set(chan, option_dtmfminduration);
4046
							ast_channel_emulate_dtmf_duration_set(chan, option_dtmfminduration);
4047
					} else
4047
					} else
4048
						ast_channel_emulate_dtmf_duration_set(chan, AST_DEFAULT_EMULATE_DTMF_DURATION);
4048
						ast_channel_emulate_dtmf_duration_set(chan, AST_DEFAULT_EMULATE_DTMF_DURATION);
4049
					ast_log(LOG_DTMF, "DTMF begin emulation of '%c' with duration %u queued on %s\n", f->subclass.integer, ast_channel_emulate_dtmf_duration(chan), ast_channel_name(chan));
4049
					ast_log(LOG_DTMF, "DTMF begin emulation of '%c' with duration %u queued on %s\n", f->subclass.integer, ast_channel_emulate_dtmf_duration(chan), ast_channel_name(chan));
4050
				}
4050
				}
4051
				if (ast_channel_audiohooks(chan)) {
4051
				if (ast_channel_audiohooks(chan)) {
4052
					struct ast_frame *old_frame = f;
4052
					struct ast_frame *old_frame = f;
4053
					/*!
4053
					/*!
4054
					 * \todo XXX It is possible to write a digit to the audiohook twice
4054
					 * \todo XXX It is possible to write a digit to the audiohook twice
4055
					 * if the digit was originally read while the channel was in autoservice. */
4055
					 * if the digit was originally read while the channel was in autoservice. */
4056
					f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
4056
					f = ast_audiohook_write_list(chan, ast_channel_audiohooks(chan), AST_AUDIOHOOK_DIRECTION_READ, f);
4057
					if (old_frame != f)
4057
					if (old_frame != f)
4058
						ast_frfree(old_frame);
4058
						ast_frfree(old_frame);
4059
				}
4059
				}
4060
			} else {
4060
			} else {
4061
				struct timeval now = ast_tvnow();
4061
				struct timeval now = ast_tvnow();
4062
				if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF)) {
4062
				if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF)) {
4063