2005-11-19 20:07:43 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* switch_channel.c -- Media Channel Interface
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <switch_channel.h>
|
|
|
|
|
2006-04-22 18:12:17 +00:00
|
|
|
struct switch_cause_table {
|
|
|
|
const char *name;
|
|
|
|
switch_call_cause_t cause;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct switch_cause_table CAUSE_CHART[] = {
|
|
|
|
{ "UNALLOCATED", SWITCH_CAUSE_UNALLOCATED },
|
|
|
|
{ "NO_ROUTE_TRANSIT_NET", SWITCH_CAUSE_NO_ROUTE_TRANSIT_NET },
|
|
|
|
{ "NO_ROUTE_DESTINATION", SWITCH_CAUSE_NO_ROUTE_DESTINATION },
|
|
|
|
{ "CHANNEL_UNACCEPTABLE", SWITCH_CAUSE_CHANNEL_UNACCEPTABLE },
|
|
|
|
{ "CALL_AWARDED_DELIVERED", SWITCH_CAUSE_CALL_AWARDED_DELIVERED },
|
|
|
|
{ "NORMAL_CLEARING", SWITCH_CAUSE_NORMAL_CLEARING },
|
|
|
|
{ "USER_BUSY", SWITCH_CAUSE_USER_BUSY },
|
|
|
|
{ "NO_USER_RESPONSE", SWITCH_CAUSE_NO_USER_RESPONSE },
|
|
|
|
{ "NO_ANSWER", SWITCH_CAUSE_NO_ANSWER },
|
|
|
|
{ "CALL_REJECTED", SWITCH_CAUSE_CALL_REJECTED },
|
|
|
|
{ "NUMBER_CHANGED", SWITCH_CAUSE_NUMBER_CHANGED },
|
|
|
|
{ "DESTINATION_OUT_OF_ORDER", SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER },
|
|
|
|
{ "INVALID_NUMBER_FORMAT", SWITCH_CAUSE_INVALID_NUMBER_FORMAT },
|
|
|
|
{ "FACILITY_REJECTED", SWITCH_CAUSE_FACILITY_REJECTED },
|
|
|
|
{ "RESPONSE_TO_STATUS_ENQUIRY", SWITCH_CAUSE_RESPONSE_TO_STATUS_ENQUIRY },
|
|
|
|
{ "NORMAL_UNSPECIFIED", SWITCH_CAUSE_NORMAL_UNSPECIFIED },
|
|
|
|
{ "NORMAL_CIRCUIT_CONGESTION", SWITCH_CAUSE_NORMAL_CIRCUIT_CONGESTION },
|
|
|
|
{ "NETWORK_OUT_OF_ORDER", SWITCH_CAUSE_NETWORK_OUT_OF_ORDER },
|
|
|
|
{ "NORMAL_TEMPORARY_FAILURE", SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE },
|
|
|
|
{ "SWITCH_CONGESTION", SWITCH_CAUSE_SWITCH_CONGESTION },
|
|
|
|
{ "ACCESS_INFO_DISCARDED", SWITCH_CAUSE_ACCESS_INFO_DISCARDED },
|
|
|
|
{ "REQUESTED_CHAN_UNAVAIL", SWITCH_CAUSE_REQUESTED_CHAN_UNAVAIL },
|
|
|
|
{ "PRE_EMPTED", SWITCH_CAUSE_PRE_EMPTED },
|
|
|
|
{ "FACILITY_NOT_SUBSCRIBED", SWITCH_CAUSE_FACILITY_NOT_SUBSCRIBED },
|
|
|
|
{ "OUTGOING_CALL_BARRED", SWITCH_CAUSE_OUTGOING_CALL_BARRED },
|
|
|
|
{ "INCOMING_CALL_BARRED", SWITCH_CAUSE_INCOMING_CALL_BARRED },
|
|
|
|
{ "BEARERCAPABILITY_NOTAUTH", SWITCH_CAUSE_BEARERCAPABILITY_NOTAUTH },
|
|
|
|
{ "BEARERCAPABILITY_NOTAVAIL", SWITCH_CAUSE_BEARERCAPABILITY_NOTAVAIL },
|
|
|
|
{ "BEARERCAPABILITY_NOTIMPL", SWITCH_CAUSE_BEARERCAPABILITY_NOTIMPL },
|
|
|
|
{ "CHAN_NOT_IMPLEMENTED", SWITCH_CAUSE_CHAN_NOT_IMPLEMENTED },
|
|
|
|
{ "FACILITY_NOT_IMPLEMENTED", SWITCH_CAUSE_FACILITY_NOT_IMPLEMENTED },
|
|
|
|
{ "INVALID_CALL_REFERENCE", SWITCH_CAUSE_INVALID_CALL_REFERENCE },
|
|
|
|
{ "INCOMPATIBLE_DESTINATION", SWITCH_CAUSE_INCOMPATIBLE_DESTINATION },
|
|
|
|
{ "INVALID_MSG_UNSPECIFIED", SWITCH_CAUSE_INVALID_MSG_UNSPECIFIED },
|
|
|
|
{ "MANDATORY_IE_MISSING", SWITCH_CAUSE_MANDATORY_IE_MISSING },
|
|
|
|
{ "MESSAGE_TYPE_NONEXIST", SWITCH_CAUSE_MESSAGE_TYPE_NONEXIST },
|
|
|
|
{ "WRONG_MESSAGE", SWITCH_CAUSE_WRONG_MESSAGE },
|
|
|
|
{ "IE_NONEXIST", SWITCH_CAUSE_IE_NONEXIST },
|
|
|
|
{ "INVALID_IE_CONTENTS", SWITCH_CAUSE_INVALID_IE_CONTENTS },
|
|
|
|
{ "WRONG_CALL_STATE", SWITCH_CAUSE_WRONG_CALL_STATE },
|
|
|
|
{ "RECOVERY_ON_TIMER_EXPIRE", SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE },
|
|
|
|
{ "MANDATORY_IE_LENGTH_ERROR", SWITCH_CAUSE_MANDATORY_IE_LENGTH_ERROR },
|
|
|
|
{ "PROTOCOL_ERROR", SWITCH_CAUSE_PROTOCOL_ERROR },
|
|
|
|
{ "INTERWORKING", SWITCH_CAUSE_INTERWORKING },
|
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
struct switch_channel {
|
|
|
|
char *name;
|
|
|
|
switch_buffer *dtmf_buffer;
|
2005-12-06 21:25:56 +00:00
|
|
|
switch_mutex_t *dtmf_mutex;
|
2005-11-19 20:07:43 +00:00
|
|
|
switch_core_session *session;
|
|
|
|
switch_channel_state state;
|
2006-03-30 23:02:50 +00:00
|
|
|
uint32_t flags;
|
2005-11-19 20:07:43 +00:00
|
|
|
switch_caller_profile *caller_profile;
|
|
|
|
switch_caller_profile *originator_caller_profile;
|
2005-12-26 19:09:59 +00:00
|
|
|
switch_caller_profile *originatee_caller_profile;
|
2005-11-19 20:07:43 +00:00
|
|
|
switch_caller_extension *caller_extension;
|
2006-02-07 20:47:15 +00:00
|
|
|
const struct switch_state_handler_table *state_handlers[SWITCH_MAX_STATE_HANDLERS];
|
|
|
|
int state_handler_index;
|
2005-11-19 20:07:43 +00:00
|
|
|
switch_hash *variables;
|
2006-02-24 22:22:43 +00:00
|
|
|
switch_channel_timetable_t times;
|
2006-02-26 04:52:34 +00:00
|
|
|
void *private_info;
|
2006-04-21 23:16:26 +00:00
|
|
|
switch_call_cause_t hangup_cause;
|
2005-11-19 20:07:43 +00:00
|
|
|
int freq;
|
|
|
|
int bits;
|
|
|
|
int channels;
|
|
|
|
int ms;
|
|
|
|
int kbps;
|
|
|
|
};
|
|
|
|
|
2006-04-22 18:12:17 +00:00
|
|
|
|
|
|
|
SWITCH_DECLARE(char *) switch_channel_str2cause(switch_call_cause_t cause)
|
|
|
|
{
|
|
|
|
uint8_t x;
|
|
|
|
char *str = "UNALLOCATED";
|
|
|
|
|
|
|
|
for(x = 0; CAUSE_CHART[x].name; x++) {
|
|
|
|
if (CAUSE_CHART[x].cause == cause) {
|
|
|
|
str = (char *) CAUSE_CHART[x].name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_call_cause_t) switch_channel_cause2str(char *str)
|
|
|
|
{
|
|
|
|
uint8_t x;
|
|
|
|
switch_call_cause_t cause = SWITCH_CAUSE_UNALLOCATED;
|
|
|
|
|
|
|
|
for(x = 0; CAUSE_CHART[x].name; x++) {
|
|
|
|
if (!strcasecmp(CAUSE_CHART[x].name, str)) {
|
|
|
|
cause = CAUSE_CHART[x].cause;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cause;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_call_cause_t) switch_channel_get_cause(switch_channel *channel)
|
|
|
|
{
|
|
|
|
return channel->hangup_cause;
|
|
|
|
}
|
|
|
|
|
2006-02-24 22:22:43 +00:00
|
|
|
SWITCH_DECLARE(switch_channel_timetable_t *) switch_channel_get_timetable(switch_channel *channel)
|
|
|
|
{
|
|
|
|
return &channel->times;
|
|
|
|
}
|
2005-11-19 20:07:43 +00:00
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status) switch_channel_alloc(switch_channel **channel, switch_memory_pool *pool)
|
|
|
|
{
|
|
|
|
assert(pool != NULL);
|
|
|
|
|
2006-02-20 00:23:25 +00:00
|
|
|
if (((*channel) = switch_core_alloc(pool, sizeof(switch_channel))) == 0) {
|
2005-11-19 20:07:43 +00:00
|
|
|
return SWITCH_STATUS_MEMERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_core_hash_init(&(*channel)->variables, pool);
|
|
|
|
switch_buffer_create(pool, &(*channel)->dtmf_buffer, 128);
|
2005-12-06 21:25:56 +00:00
|
|
|
switch_mutex_init(&(*channel)->dtmf_mutex, SWITCH_MUTEX_NESTED, pool);
|
2006-02-24 22:22:43 +00:00
|
|
|
(*channel)->times.created = switch_time_now();
|
2006-04-22 18:12:17 +00:00
|
|
|
(*channel)->hangup_cause = SWITCH_CAUSE_UNALLOCATED;
|
2005-12-22 01:57:32 +00:00
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
SWITCH_DECLARE(switch_status) switch_channel_set_raw_mode(switch_channel *channel, int freq, int bits, int channels,
|
|
|
|
int ms, int kbps)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
assert(channel != NULL);
|
|
|
|
|
|
|
|
channel->freq = freq;
|
|
|
|
channel->bits = bits;
|
|
|
|
channel->channels = channels;
|
|
|
|
channel->ms = ms;
|
|
|
|
channel->kbps = kbps;
|
|
|
|
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
SWITCH_DECLARE(switch_status) switch_channel_get_raw_mode(switch_channel *channel, int *freq, int *bits, int *channels,
|
|
|
|
int *ms, int *kbps)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
|
|
|
if (freq) {
|
|
|
|
*freq = channel->freq;
|
|
|
|
}
|
|
|
|
if (bits) {
|
|
|
|
*bits = channel->bits;
|
|
|
|
}
|
|
|
|
if (channels) {
|
|
|
|
*channels = channel->channels;
|
|
|
|
}
|
|
|
|
if (ms) {
|
|
|
|
*ms = channel->ms;
|
|
|
|
}
|
|
|
|
if (kbps) {
|
|
|
|
*kbps = channel->kbps;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-30 23:02:50 +00:00
|
|
|
SWITCH_DECLARE(switch_size_t) switch_channel_has_dtmf(switch_channel *channel)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
2006-03-30 23:02:50 +00:00
|
|
|
switch_size_t has;
|
2005-11-19 20:07:43 +00:00
|
|
|
|
|
|
|
assert(channel != NULL);
|
2005-12-06 21:25:56 +00:00
|
|
|
switch_mutex_lock(channel->dtmf_mutex);
|
|
|
|
has = switch_buffer_inuse(channel->dtmf_buffer);
|
|
|
|
switch_mutex_unlock(channel->dtmf_mutex);
|
|
|
|
|
|
|
|
return has;
|
2005-11-19 20:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status) switch_channel_queue_dtmf(switch_channel *channel, char *dtmf)
|
|
|
|
{
|
2005-12-06 21:25:56 +00:00
|
|
|
switch_status status;
|
2006-03-30 23:02:50 +00:00
|
|
|
register switch_size_t len, inuse;
|
2005-12-21 22:25:22 +00:00
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
assert(channel != NULL);
|
|
|
|
|
2005-12-06 21:25:56 +00:00
|
|
|
switch_mutex_lock(channel->dtmf_mutex);
|
2006-03-30 23:02:50 +00:00
|
|
|
|
|
|
|
inuse = switch_buffer_inuse(channel->dtmf_buffer);
|
|
|
|
len = strlen(dtmf);
|
|
|
|
|
|
|
|
if (len + inuse > switch_buffer_len(channel->dtmf_buffer)) {
|
2005-11-19 20:07:43 +00:00
|
|
|
switch_buffer_toss(channel->dtmf_buffer, strlen(dtmf));
|
|
|
|
}
|
|
|
|
|
2006-03-30 23:02:50 +00:00
|
|
|
status = switch_buffer_write(channel->dtmf_buffer, dtmf, len) ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_MEMERR;
|
2005-12-06 21:25:56 +00:00
|
|
|
switch_mutex_unlock(channel->dtmf_mutex);
|
|
|
|
|
|
|
|
return status;
|
2005-11-19 20:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-31 16:10:00 +00:00
|
|
|
SWITCH_DECLARE(switch_size_t) switch_channel_dequeue_dtmf(switch_channel *channel, char *dtmf, switch_size_t len)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
2006-03-31 16:10:00 +00:00
|
|
|
switch_size_t bytes;
|
2005-12-21 22:25:22 +00:00
|
|
|
switch_event *event;
|
2005-11-19 20:07:43 +00:00
|
|
|
|
|
|
|
assert(channel != NULL);
|
2005-12-06 21:25:56 +00:00
|
|
|
|
|
|
|
switch_mutex_lock(channel->dtmf_mutex);
|
2005-11-19 20:07:43 +00:00
|
|
|
if ((bytes = switch_buffer_read(channel->dtmf_buffer, dtmf, len)) > 0) {
|
|
|
|
*(dtmf + bytes) = '\0';
|
|
|
|
}
|
2005-12-06 21:25:56 +00:00
|
|
|
switch_mutex_unlock(channel->dtmf_mutex);
|
|
|
|
|
2005-12-21 22:25:22 +00:00
|
|
|
if (bytes && switch_event_create(&event, SWITCH_EVENT_CHANNEL_ANSWER) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_channel_event_set_data(channel, event);
|
2005-12-23 21:09:36 +00:00
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "DTMF-String", dtmf);
|
2005-12-21 22:25:22 +00:00
|
|
|
switch_event_fire(&event);
|
|
|
|
}
|
2005-12-06 21:25:56 +00:00
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
return bytes;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status) switch_channel_init(switch_channel *channel,
|
2006-01-03 01:17:59 +00:00
|
|
|
switch_core_session *session,
|
2006-03-30 23:02:50 +00:00
|
|
|
switch_channel_state state, uint32_t flags)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
channel->state = state;
|
|
|
|
channel->flags = flags;
|
|
|
|
channel->session = session;
|
|
|
|
switch_channel_set_raw_mode(channel, 8000, 16, 1, 20, 8);
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(char *) switch_channel_get_variable(switch_channel *channel, char *varname)
|
|
|
|
{
|
|
|
|
return switch_core_hash_find(channel->variables, varname);
|
|
|
|
}
|
|
|
|
|
2006-02-26 04:52:34 +00:00
|
|
|
SWITCH_DECLARE(switch_status) switch_channel_set_private(switch_channel *channel, void *private_info)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
2006-02-26 04:52:34 +00:00
|
|
|
channel->private_info = private_info;
|
2005-11-19 20:07:43 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(void *) switch_channel_get_private(switch_channel *channel)
|
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
2006-02-26 04:52:34 +00:00
|
|
|
return channel->private_info;
|
2005-11-19 20:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status) switch_channel_set_name(switch_channel *channel, char *name)
|
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
channel->name = NULL;
|
|
|
|
if (name) {
|
|
|
|
channel->name = switch_core_session_strdup(channel->session, name);
|
|
|
|
}
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SWITCH_DECLARE(char *) switch_channel_get_name(switch_channel *channel)
|
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
return channel->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status) switch_channel_set_variable(switch_channel *channel, char *varname, char *value)
|
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
switch_core_hash_delete(channel->variables, varname);
|
|
|
|
|
2005-12-14 22:46:09 +00:00
|
|
|
switch_core_hash_insert_dup(channel->variables, varname, switch_core_session_strdup(channel->session, value));
|
2005-11-19 20:07:43 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-03-30 23:02:50 +00:00
|
|
|
SWITCH_DECLARE(int) switch_channel_test_flag(switch_channel *channel, switch_channel_flag flags)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
2006-03-30 23:02:50 +00:00
|
|
|
return switch_test_flag(channel, flags) ? 1 : 0;
|
2005-11-19 20:07:43 +00:00
|
|
|
}
|
|
|
|
|
2006-03-30 23:02:50 +00:00
|
|
|
SWITCH_DECLARE(void) switch_channel_set_flag(switch_channel *channel, switch_channel_flag flags)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
|
|
|
switch_set_flag(channel, flags);
|
|
|
|
}
|
|
|
|
|
2006-03-30 23:02:50 +00:00
|
|
|
SWITCH_DECLARE(void) switch_channel_clear_flag(switch_channel *channel, switch_channel_flag flags)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
2006-03-30 23:02:50 +00:00
|
|
|
switch_clear_flag(channel, flags);
|
2005-11-19 20:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_channel_state) switch_channel_get_state(switch_channel *channel)
|
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
return channel->state;
|
|
|
|
}
|
|
|
|
|
2006-03-01 22:55:28 +00:00
|
|
|
SWITCH_DECLARE(unsigned int) switch_channel_ready(switch_channel *channel)
|
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
return (channel->state > CS_RING && channel->state < CS_HANGUP) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2005-12-21 22:25:22 +00:00
|
|
|
static const char *state_names[] = {
|
|
|
|
"CS_NEW",
|
|
|
|
"CS_INIT",
|
|
|
|
"CS_RING",
|
|
|
|
"CS_TRANSMIT",
|
|
|
|
"CS_EXECUTE",
|
|
|
|
"CS_LOOPBACK",
|
|
|
|
"CS_HANGUP",
|
|
|
|
"CS_DONE"
|
|
|
|
};
|
|
|
|
|
|
|
|
SWITCH_DECLARE(const char *) switch_channel_state_name(switch_channel_state state)
|
|
|
|
{
|
|
|
|
return state_names[state];
|
|
|
|
}
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
SWITCH_DECLARE(switch_channel_state) switch_channel_set_state(switch_channel *channel, switch_channel_state state)
|
|
|
|
{
|
|
|
|
switch_channel_state last_state;
|
|
|
|
int ok = 0;
|
2005-12-21 22:25:22 +00:00
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
|
|
|
|
assert(channel != NULL);
|
|
|
|
last_state = channel->state;
|
|
|
|
|
|
|
|
if (last_state == state) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2006-02-13 17:37:10 +00:00
|
|
|
if (last_state >= CS_HANGUP && state < last_state) {
|
2005-12-06 17:18:56 +00:00
|
|
|
return last_state;
|
|
|
|
}
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
/* STUB for more dev
|
2006-01-20 15:05:05 +00:00
|
|
|
case CS_INIT:
|
|
|
|
switch(state) {
|
|
|
|
|
|
|
|
case CS_NEW:
|
|
|
|
case CS_INIT:
|
|
|
|
case CS_LOOPBACK:
|
|
|
|
case CS_TRANSMIT:
|
|
|
|
case CS_RING:
|
|
|
|
case CS_EXECUTE:
|
|
|
|
case CS_HANGUP:
|
|
|
|
case CS_DONE:
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (last_state) {
|
2006-01-20 02:02:03 +00:00
|
|
|
case CS_NEW:
|
2006-01-20 15:05:05 +00:00
|
|
|
switch (state) {
|
|
|
|
default:
|
|
|
|
ok++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2006-01-20 02:02:03 +00:00
|
|
|
case CS_INIT:
|
2006-01-20 15:05:05 +00:00
|
|
|
switch (state) {
|
|
|
|
case CS_LOOPBACK:
|
|
|
|
case CS_TRANSMIT:
|
|
|
|
case CS_RING:
|
|
|
|
case CS_EXECUTE:
|
|
|
|
case CS_HANGUP:
|
|
|
|
ok++;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
case CS_LOOPBACK:
|
2006-01-20 15:05:05 +00:00
|
|
|
switch (state) {
|
|
|
|
case CS_TRANSMIT:
|
|
|
|
case CS_RING:
|
|
|
|
case CS_EXECUTE:
|
|
|
|
case CS_HANGUP:
|
|
|
|
ok++;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
case CS_TRANSMIT:
|
2006-01-20 15:05:05 +00:00
|
|
|
switch (state) {
|
|
|
|
case CS_LOOPBACK:
|
|
|
|
case CS_RING:
|
|
|
|
case CS_EXECUTE:
|
|
|
|
case CS_HANGUP:
|
|
|
|
ok++;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
case CS_RING:
|
2006-01-20 15:05:05 +00:00
|
|
|
switch (state) {
|
|
|
|
case CS_LOOPBACK:
|
|
|
|
case CS_EXECUTE:
|
|
|
|
case CS_HANGUP:
|
|
|
|
case CS_TRANSMIT:
|
|
|
|
ok++;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
case CS_EXECUTE:
|
2006-01-20 15:05:05 +00:00
|
|
|
switch (state) {
|
|
|
|
case CS_LOOPBACK:
|
|
|
|
case CS_TRANSMIT:
|
|
|
|
case CS_RING:
|
|
|
|
case CS_HANGUP:
|
|
|
|
ok++;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
case CS_HANGUP:
|
2006-01-20 15:05:05 +00:00
|
|
|
switch (state) {
|
|
|
|
case CS_DONE:
|
|
|
|
ok++;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2005-11-19 20:07:43 +00:00
|
|
|
|
|
|
|
default:
|
2006-01-20 15:05:05 +00:00
|
|
|
break;
|
2005-11-19 20:07:43 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (ok) {
|
2006-04-25 23:11:56 +00:00
|
|
|
switch_event *event;
|
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s State Change %s -> %s\n", channel->name,
|
2006-01-20 15:05:05 +00:00
|
|
|
state_names[last_state], state_names[state]);
|
2005-11-19 20:07:43 +00:00
|
|
|
channel->state = state;
|
2006-04-22 18:12:17 +00:00
|
|
|
|
|
|
|
if (state == CS_HANGUP && channel->hangup_cause == SWITCH_CAUSE_UNALLOCATED) {
|
|
|
|
channel->hangup_cause = SWITCH_CAUSE_NORMAL_CLEARING;
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:11:56 +00:00
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_STATE) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_channel_event_set_data(channel, event);
|
|
|
|
switch_event_fire(&event);
|
|
|
|
}
|
|
|
|
|
2006-03-22 17:05:16 +00:00
|
|
|
if (state < CS_DONE) {
|
|
|
|
switch_core_session_signal_state_change(channel->session);
|
|
|
|
}
|
2005-11-19 20:07:43 +00:00
|
|
|
} else {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s Invalid State Change %s -> %s\n", channel->name,
|
2006-01-20 15:05:05 +00:00
|
|
|
state_names[last_state], state_names[state]);
|
2005-11-19 20:07:43 +00:00
|
|
|
|
|
|
|
//we won't tolerate an invalid state change so we can make sure we are as robust as a nice cup of dark coffee!
|
|
|
|
if (channel->state < CS_HANGUP) {
|
|
|
|
// not cool lets crash this bad boy and figure out wtf is going on
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return channel->state;
|
|
|
|
}
|
|
|
|
|
2005-12-21 22:25:22 +00:00
|
|
|
SWITCH_DECLARE(void) switch_channel_event_set_data(switch_channel *channel, switch_event *event)
|
|
|
|
{
|
2005-12-26 19:09:59 +00:00
|
|
|
switch_caller_profile *caller_profile, *originator_caller_profile, *originatee_caller_profile;
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_hash_index_t *hi;
|
2005-12-21 22:25:22 +00:00
|
|
|
void *val;
|
|
|
|
const void *var;
|
2006-04-04 21:26:21 +00:00
|
|
|
char state_num[25];
|
2005-12-21 22:25:22 +00:00
|
|
|
|
|
|
|
caller_profile = switch_channel_get_caller_profile(channel);
|
|
|
|
originator_caller_profile = switch_channel_get_originator_caller_profile(channel);
|
2005-12-26 19:09:59 +00:00
|
|
|
originatee_caller_profile = switch_channel_get_originatee_caller_profile(channel);
|
2006-01-03 01:17:59 +00:00
|
|
|
|
2006-04-04 21:26:21 +00:00
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Channel-State", (char *) switch_channel_state_name(channel->state));
|
|
|
|
snprintf(state_num, sizeof(state_num), "%d", channel->state);
|
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Channel-State-Number", (char *) state_num);
|
2005-12-23 21:09:36 +00:00
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Channel-Name", switch_channel_get_name(channel));
|
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Unique-ID", switch_core_session_get_uuid(channel->session));
|
2005-12-22 01:57:32 +00:00
|
|
|
|
2005-12-21 22:25:22 +00:00
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
/* Index Caller's Profile */
|
2005-12-21 22:25:22 +00:00
|
|
|
if (caller_profile) {
|
2005-12-26 19:09:59 +00:00
|
|
|
switch_caller_profile_event_set_data(caller_profile, "Caller", event);
|
2005-12-21 22:25:22 +00:00
|
|
|
}
|
2006-01-03 01:17:59 +00:00
|
|
|
|
2005-12-21 22:25:22 +00:00
|
|
|
/* Index Originator's Profile */
|
|
|
|
if (originator_caller_profile) {
|
2005-12-26 19:09:59 +00:00
|
|
|
switch_caller_profile_event_set_data(originator_caller_profile, "Originator", event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Index Originatee's Profile */
|
|
|
|
if (originatee_caller_profile) {
|
|
|
|
switch_caller_profile_event_set_data(originatee_caller_profile, "Originatee", event);
|
2005-12-21 22:25:22 +00:00
|
|
|
}
|
2005-12-23 21:09:36 +00:00
|
|
|
|
2005-12-21 22:25:22 +00:00
|
|
|
/* Index Variables */
|
2006-01-20 15:05:05 +00:00
|
|
|
for (hi = switch_hash_first(switch_core_session_get_pool(channel->session), channel->variables); hi;
|
|
|
|
hi = switch_hash_next(hi)) {
|
2005-12-21 22:25:22 +00:00
|
|
|
char buf[1024];
|
|
|
|
switch_hash_this(hi, &var, NULL, &val);
|
|
|
|
snprintf(buf, sizeof(buf), "variable_%s", (char *) var);
|
2005-12-23 21:09:36 +00:00
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, buf, (char *) val);
|
2005-12-21 22:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
SWITCH_DECLARE(void) switch_channel_set_caller_profile(switch_channel *channel, switch_caller_profile *caller_profile)
|
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
2006-02-22 02:50:33 +00:00
|
|
|
assert(channel->session != NULL);
|
|
|
|
|
|
|
|
if (!caller_profile->uuid) {
|
|
|
|
caller_profile->uuid = switch_core_session_strdup(channel->session, switch_core_session_get_uuid(channel->session));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!caller_profile->chan_name) {
|
|
|
|
caller_profile->chan_name = switch_core_session_strdup(channel->session, channel->name);
|
|
|
|
}
|
|
|
|
|
2006-04-04 21:26:21 +00:00
|
|
|
if (!channel->caller_profile) {
|
|
|
|
switch_event *event;
|
|
|
|
|
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_CREATE) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_channel_event_set_data(channel, event);
|
|
|
|
switch_event_fire(&event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
channel->caller_profile = caller_profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_caller_profile *) switch_channel_get_caller_profile(switch_channel *channel)
|
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
return channel->caller_profile;
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
SWITCH_DECLARE(void) switch_channel_set_originator_caller_profile(switch_channel *channel,
|
|
|
|
switch_caller_profile *caller_profile)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
channel->originator_caller_profile = caller_profile;
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
SWITCH_DECLARE(void) switch_channel_set_originatee_caller_profile(switch_channel *channel,
|
|
|
|
switch_caller_profile *caller_profile)
|
2005-12-26 19:09:59 +00:00
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
channel->originatee_caller_profile = caller_profile;
|
|
|
|
}
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
SWITCH_DECLARE(switch_caller_profile *) switch_channel_get_originator_caller_profile(switch_channel *channel)
|
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
return channel->originator_caller_profile;
|
|
|
|
}
|
|
|
|
|
2006-02-22 02:50:33 +00:00
|
|
|
SWITCH_DECLARE(char *) switch_channel_get_uuid(switch_channel *channel)
|
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
assert(channel->session != NULL);
|
|
|
|
return switch_core_session_get_uuid(channel->session);
|
|
|
|
}
|
|
|
|
|
2005-12-26 19:09:59 +00:00
|
|
|
SWITCH_DECLARE(switch_caller_profile *) switch_channel_get_originatee_caller_profile(switch_channel *channel)
|
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
return channel->originatee_caller_profile;
|
|
|
|
}
|
|
|
|
|
2006-02-07 20:47:15 +00:00
|
|
|
SWITCH_DECLARE(int) switch_channel_add_state_handler(switch_channel *channel,
|
|
|
|
const switch_state_handler_table *state_handler)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
2006-02-07 22:58:56 +00:00
|
|
|
int index;
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
assert(channel != NULL);
|
2006-02-07 22:58:56 +00:00
|
|
|
index = channel->state_handler_index++;
|
2006-02-07 20:47:15 +00:00
|
|
|
|
|
|
|
if (channel->state_handler_index >= SWITCH_MAX_STATE_HANDLERS) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel->state_handlers[index] = state_handler;
|
|
|
|
return index;
|
2005-11-19 20:07:43 +00:00
|
|
|
}
|
|
|
|
|
2006-02-07 20:47:15 +00:00
|
|
|
SWITCH_DECLARE(const switch_state_handler_table *) switch_channel_get_state_handler(switch_channel *channel, int index)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
2006-02-07 20:47:15 +00:00
|
|
|
|
|
|
|
if (index > SWITCH_MAX_STATE_HANDLERS || index > channel->state_handler_index) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return channel->state_handlers[index];
|
2005-11-19 20:07:43 +00:00
|
|
|
}
|
|
|
|
|
2006-04-25 18:02:12 +00:00
|
|
|
SWITCH_DECLARE(void) switch_channel_clear_state_handler(switch_channel *channel, const switch_state_handler_table *state_handler)
|
|
|
|
{
|
|
|
|
int index, i = 0;
|
|
|
|
const switch_state_handler_table *new_handlers[SWITCH_MAX_STATE_HANDLERS] = {0};
|
|
|
|
|
|
|
|
assert(channel != NULL);
|
|
|
|
|
|
|
|
|
|
|
|
for (index = 0; index < channel->state_handler_index; index++) {
|
|
|
|
if (channel->state_handlers[index] != state_handler) {
|
|
|
|
new_handlers[i++] = channel->state_handlers[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(channel->state_handlers, 0, sizeof(channel->state_handlers));
|
|
|
|
for (index = 0; index < i; index++) {
|
|
|
|
channel->state_handlers[index] = new_handlers[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
SWITCH_DECLARE(void) switch_channel_set_caller_extension(switch_channel *channel,
|
|
|
|
switch_caller_extension *caller_extension)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
channel->caller_extension = caller_extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_caller_extension *) switch_channel_get_caller_extension(switch_channel *channel)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(channel != NULL);
|
|
|
|
return channel->caller_extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-22 03:05:25 +00:00
|
|
|
SWITCH_DECLARE(switch_channel_state) switch_channel_hangup(switch_channel *channel, switch_call_cause_t hangup_cause)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
2006-04-03 14:53:34 +00:00
|
|
|
|
|
|
|
if (!channel->times.hungup) {
|
2006-02-24 22:22:43 +00:00
|
|
|
channel->times.hungup = switch_time_now();
|
2006-04-03 14:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (channel->state < CS_HANGUP) {
|
2006-04-25 23:11:56 +00:00
|
|
|
switch_event *event;
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
channel->state = CS_HANGUP;
|
2006-04-22 03:05:25 +00:00
|
|
|
channel->hangup_cause = hangup_cause;
|
2006-04-25 23:11:56 +00:00
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_STATE) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_channel_event_set_data(channel, event);
|
|
|
|
switch_event_fire(&event);
|
|
|
|
}
|
|
|
|
|
2006-03-22 17:05:16 +00:00
|
|
|
switch_core_session_kill_channel(channel->session, SWITCH_SIG_KILL);
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_core_session_signal_state_change(channel->session);
|
2005-11-19 20:07:43 +00:00
|
|
|
}
|
|
|
|
return channel->state;
|
|
|
|
}
|
|
|
|
|
2006-03-03 16:57:21 +00:00
|
|
|
SWITCH_DECLARE(switch_status) switch_channel_pre_answer(switch_channel *channel)
|
|
|
|
{
|
|
|
|
switch_core_session_message msg;
|
|
|
|
char *uuid = switch_core_session_get_uuid(channel->session);
|
|
|
|
switch_status status;
|
|
|
|
|
2006-03-21 00:20:24 +00:00
|
|
|
assert(channel != NULL);
|
|
|
|
|
|
|
|
if (channel->state >= CS_HANGUP) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2006-03-03 16:57:21 +00:00
|
|
|
msg.message_id = SWITCH_MESSAGE_INDICATE_PROGRESS;
|
|
|
|
msg.from = channel->name;
|
|
|
|
status = switch_core_session_message_send(uuid, &msg);
|
2006-03-03 17:49:22 +00:00
|
|
|
|
|
|
|
if (status == SWITCH_STATUS_SUCCESS) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Pre-Answer %s!\n", channel->name);
|
2006-03-03 17:49:22 +00:00
|
|
|
switch_channel_set_flag(channel, CF_EARLY_MEDIA);
|
|
|
|
}
|
2006-03-03 16:57:21 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2005-11-19 20:07:43 +00:00
|
|
|
SWITCH_DECLARE(switch_status) switch_channel_answer(switch_channel *channel)
|
|
|
|
{
|
|
|
|
assert(channel != NULL);
|
|
|
|
|
2006-03-21 00:20:24 +00:00
|
|
|
if (channel->state >= CS_HANGUP) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2005-11-19 20:07:43 +00:00
|
|
|
|
|
|
|
if (switch_core_session_answer_channel(channel->session) == SWITCH_STATUS_SUCCESS) {
|
2005-12-21 22:25:22 +00:00
|
|
|
switch_event *event;
|
|
|
|
|
2006-02-24 22:22:43 +00:00
|
|
|
channel->times.answered = switch_time_now();
|
2005-11-19 20:07:43 +00:00
|
|
|
switch_channel_set_flag(channel, CF_ANSWERED);
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Answer %s!\n", channel->name);
|
2005-12-21 22:25:22 +00:00
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_ANSWER) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_channel_event_set_data(channel, event);
|
|
|
|
switch_event_fire(&event);
|
|
|
|
}
|
2005-11-19 20:07:43 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
|
|
|
|
}
|