Skip to content
Snippets Groups Projects
channel_internal_api.c 51.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	return &chan->datastores;
    
    }
    struct ast_autochan_list *ast_channel_autochans(struct ast_channel *chan)
    {
    
    	return &chan->autochans;
    
    }
    struct ast_readq_list *ast_channel_readq(struct ast_channel *chan)
    {
    
    	return &chan->readq;
    
    }
    struct ast_frame *ast_channel_dtmff(struct ast_channel *chan)
    {
    
    	return &chan->dtmff;
    
    }
    struct ast_jb *ast_channel_jb(struct ast_channel *chan)
    {
    
    	return &chan->jb;
    
    }
    struct ast_party_caller *ast_channel_caller(struct ast_channel *chan)
    {
    
    	return &chan->caller;
    
    }
    struct ast_party_connected_line *ast_channel_connected(struct ast_channel *chan)
    {
    
    	return &chan->connected;
    
    struct ast_party_connected_line *ast_channel_connected_indicated(struct ast_channel *chan)
    {
    	return &chan->connected_indicated;
    }
    
    struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
    {
    	return ast_party_id_merge(&chan->connected.id, &chan->connected.priv);
    }
    
    struct ast_party_dialed *ast_channel_dialed(struct ast_channel *chan)
    {
    
    	return &chan->dialed;
    
    }
    struct ast_party_redirecting *ast_channel_redirecting(struct ast_channel *chan)
    {
    
    	return &chan->redirecting;
    
    struct ast_party_id ast_channel_redirecting_effective_orig(struct ast_channel *chan)
    {
    	return ast_party_id_merge(&chan->redirecting.orig, &chan->redirecting.priv_orig);
    }
    struct ast_party_id ast_channel_redirecting_effective_from(struct ast_channel *chan)
    {
    	return ast_party_id_merge(&chan->redirecting.from, &chan->redirecting.priv_from);
    }
    struct ast_party_id ast_channel_redirecting_effective_to(struct ast_channel *chan)
    {
    	return ast_party_id_merge(&chan->redirecting.to, &chan->redirecting.priv_to);
    }
    
    struct timeval *ast_channel_dtmf_tv(struct ast_channel *chan)
    {
    
    	return &chan->dtmf_tv;
    
    }
    struct timeval *ast_channel_whentohangup(struct ast_channel *chan)
    {
    
    	return &chan->whentohangup;
    
    }
    struct varshead *ast_channel_varshead(struct ast_channel *chan)
    {
    
    	return &chan->varshead;
    
    void ast_channel_dtmff_set(struct ast_channel *chan, struct ast_frame *value)
    {
    
    	chan->dtmff = *value;
    
    }
    void ast_channel_jb_set(struct ast_channel *chan, struct ast_jb *value)
    {
    
    	chan->jb = *value;
    
    void ast_channel_caller_set(struct ast_channel *chan, struct ast_party_caller *value)
    {
    
    	chan->caller = *value;
    
    }
    void ast_channel_connected_set(struct ast_channel *chan, struct ast_party_connected_line *value)
    {
    
    	chan->connected = *value;
    
    }
    void ast_channel_dialed_set(struct ast_channel *chan, struct ast_party_dialed *value)
    {
    
    	chan->dialed = *value;
    
    }
    void ast_channel_redirecting_set(struct ast_channel *chan, struct ast_party_redirecting *value)
    {
    
    	chan->redirecting = *value;
    
    }
    void ast_channel_dtmf_tv_set(struct ast_channel *chan, struct timeval *value)
    {
    
    	chan->dtmf_tv = *value;
    
    }
    void ast_channel_whentohangup_set(struct ast_channel *chan, struct timeval *value)
    {
    
    	chan->whentohangup = *value;
    
    }
    void ast_channel_varshead_set(struct ast_channel *chan, struct varshead *value)
    {
    
    	chan->varshead = *value;
    
    struct timeval ast_channel_creationtime(struct ast_channel *chan)
    {
    	return chan->creationtime;
    }
    void ast_channel_creationtime_set(struct ast_channel *chan, struct timeval *value)
    {
    	chan->creationtime = *value;
    }
    
    struct timeval ast_channel_answertime(struct ast_channel *chan)
    {
    	return chan->answertime;
    }
    
    void ast_channel_answertime_set(struct ast_channel *chan, struct timeval *value)
    {
    	chan->answertime = *value;
    }
    
    
    /* Evil softhangup accessors */
    
    int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
    
    	return chan->softhangup;
    
    }
    void ast_channel_softhangup_internal_flag_set(struct ast_channel *chan, int value)
    {
    
    	chan->softhangup = value;
    
    }
    void ast_channel_softhangup_internal_flag_add(struct ast_channel *chan, int value)
    {
    
    	chan->softhangup |= value;
    
    }
    void ast_channel_softhangup_internal_flag_clear(struct ast_channel *chan, int value)
    {
    
    	chan ->softhangup &= ~value;
    
    int ast_channel_unbridged_nolock(struct ast_channel *chan)
    {
    	return chan->unbridged;
    }
    
    int ast_channel_unbridged(struct ast_channel *chan)
    {
    	int res;
    	ast_channel_lock(chan);
    	res = ast_channel_unbridged_nolock(chan);
    	ast_channel_unlock(chan);
    	return res;
    }
    
    void ast_channel_set_unbridged_nolock(struct ast_channel *chan, int value)
    {
    
    	chan->unbridged = !!value;
    
    	ast_queue_frame(chan, &ast_null_frame);
    }
    
    void ast_channel_set_unbridged(struct ast_channel *chan, int value)
    {
    	ast_channel_lock(chan);
    	ast_channel_set_unbridged_nolock(chan, value);
    	ast_channel_unlock(chan);
    }
    
    
    int ast_channel_is_t38_active_nolock(struct ast_channel *chan)
    {
    	return chan->is_t38_active;
    }
    
    int ast_channel_is_t38_active(struct ast_channel *chan)
    {
    	int res;
    
    	ast_channel_lock(chan);
    	res = ast_channel_is_t38_active_nolock(chan);
    	ast_channel_unlock(chan);
    	return res;
    }
    
    void ast_channel_set_is_t38_active_nolock(struct ast_channel *chan, int is_t38_active)
    {
    	chan->is_t38_active = !!is_t38_active;
    }
    
    void ast_channel_set_is_t38_active(struct ast_channel *chan, int is_t38_active)
    {
    	ast_channel_lock(chan);
    	ast_channel_set_is_t38_active_nolock(chan, is_t38_active);
    	ast_channel_unlock(chan);
    }
    
    
    void ast_channel_callid_cleanup(struct ast_channel *chan)
    {
    	if (chan->callid) {
    		chan->callid = ast_callid_unref(chan->callid);
    	}
    }
    
    
    /* Typedef accessors */
    ast_group_t ast_channel_callgroup(const struct ast_channel *chan)
    {
    
    	return chan->callgroup;
    
    }
    void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
    {
    
    	chan->callgroup = value;
    
    }
    ast_group_t ast_channel_pickupgroup(const struct ast_channel *chan)
    {
    
    	return chan->pickupgroup;
    
    }
    void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
    {
    
    	chan->pickupgroup = value;
    
    struct ast_namedgroups *ast_channel_named_callgroups(const struct ast_channel *chan)
    {
    	return chan->named_callgroups;
    }
    void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
    {
    	ast_unref_namedgroups(chan->named_callgroups);
    	chan->named_callgroups = ast_ref_namedgroups(value);
    }
    struct ast_namedgroups *ast_channel_named_pickupgroups(const struct ast_channel *chan)
    {
    	return chan->named_pickupgroups;
    }
    void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
    {
    	ast_unref_namedgroups(chan->named_pickupgroups);
    	chan->named_pickupgroups = ast_ref_namedgroups(value);
    }
    
    
    /* Alertpipe functions */
    int ast_channel_alert_write(struct ast_channel *chan)
    {
    
    	return ast_alertpipe_write(chan->alertpipe);
    
    }
    
    ast_alert_status_t ast_channel_internal_alert_flush(struct ast_channel *chan)
    {
    
    	return ast_alertpipe_flush(chan->alertpipe);
    
    }
    
    ast_alert_status_t ast_channel_internal_alert_read(struct ast_channel *chan)
    {
    
    	return ast_alertpipe_read(chan->alertpipe);
    
    }
    
    int ast_channel_alert_writable(struct ast_channel *chan)
    {
    
    	return ast_alertpipe_writable(chan->alertpipe);
    
    }
    
    int ast_channel_internal_alert_readable(struct ast_channel *chan)
    {
    
    	return ast_alertpipe_readable(chan->alertpipe);
    
    
    void ast_channel_internal_alertpipe_clear(struct ast_channel *chan)
    {
    
    	ast_alertpipe_clear(chan->alertpipe);
    
    }
    
    void ast_channel_internal_alertpipe_close(struct ast_channel *chan)
    {
    
    	ast_alertpipe_close(chan->alertpipe);
    
    }
    
    int ast_channel_internal_alertpipe_init(struct ast_channel *chan)
    {
    
    	return ast_alertpipe_init(chan->alertpipe);
    
    }
    
    int ast_channel_internal_alert_readfd(struct ast_channel *chan)
    {
    
    	return ast_alertpipe_readfd(chan->alertpipe);
    
    }
    
    void ast_channel_internal_alertpipe_swap(struct ast_channel *chan1, struct ast_channel *chan2)
    {
    
    	ast_alertpipe_swap(chan1->alertpipe, chan2->alertpipe);
    
    }
    
    /* file descriptor array accessors */
    void ast_channel_internal_fd_set(struct ast_channel *chan, int which, int value)
    {
    
    	chan->fds[which] = value;
    
    }
    void ast_channel_internal_fd_clear(struct ast_channel *chan, int which)
    {
    	ast_channel_internal_fd_set(chan, which, -1);
    }
    void ast_channel_internal_fd_clear_all(struct ast_channel *chan)
    {
    	int i;
    	for (i = 0; i < AST_MAX_FDS; i++) {
    		ast_channel_internal_fd_clear(chan, i);
    	}
    }
    int ast_channel_fd(const struct ast_channel *chan, int which)
    {
    
    	return chan->fds[which];
    
    }
    int ast_channel_fd_isset(const struct ast_channel *chan, int which)
    {
    	return ast_channel_fd(chan, which) > -1;
    }
    
    #ifdef HAVE_EPOLL
    struct ast_epoll_data *ast_channel_internal_epfd_data(const struct ast_channel *chan, int which)
    {
    
    	return chan->epfd_data[which];
    
    }
    void ast_channel_internal_epfd_data_set(struct ast_channel *chan, int which , struct ast_epoll_data *value)
    {
    
    	chan->epfd_data[which] = value;
    
    
    pthread_t ast_channel_blocker(const struct ast_channel *chan)
    {
    	return chan->blocker;
    }
    void ast_channel_blocker_set(struct ast_channel *chan, pthread_t value)
    {
    	chan->blocker = value;
    }
    
    ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)
    {
    	return chan->timingfunc;
    }
    void ast_channel_timingfunc_set(struct ast_channel *chan, ast_timing_func_t value)
    {
    	chan->timingfunc = value;
    }
    
    struct ast_bridge *ast_channel_internal_bridge(const struct ast_channel *chan)
    {
    	return chan->bridge;
    }
    void ast_channel_internal_bridge_set(struct ast_channel *chan, struct ast_bridge *value)
    {
    	chan->bridge = value;
    
    struct ast_bridge_channel *ast_channel_internal_bridge_channel(const struct ast_channel *chan)
    {
    	return chan->bridge_channel;
    }
    void ast_channel_internal_bridge_channel_set(struct ast_channel *chan, struct ast_bridge_channel *value)
    {
    	chan->bridge_channel = value;
    }
    
    
    struct ast_flags *ast_channel_flags(struct ast_channel *chan)
    {
    	return &chan->flags;
    }
    
    
    static int collect_names_cb(void *obj, void *arg, int flags) {
    	struct ast_control_pvt_cause_code *cause_code = obj;
    	struct ast_str **str = arg;
    
    	ast_str_append(str, 0, "%s%s", (ast_str_strlen(*str) ? "," : ""), cause_code->chan_name);
    
    	return 0;
    }
    
    struct ast_str *ast_channel_dialed_causes_channels(const struct ast_channel *chan)
    {
    	struct ast_str *chanlist = ast_str_create(128);
    
    	if (!chanlist) {
    		return NULL;
    	}
    
    	ao2_callback(chan->dialed_causes, 0, collect_names_cb, &chanlist);
    
    	return chanlist;
    }
    
    struct ast_control_pvt_cause_code *ast_channel_dialed_causes_find(const struct ast_channel *chan, const char *chan_name)
    {
    	return ao2_find(chan->dialed_causes, chan_name, OBJ_KEY);
    }
    
    int ast_channel_dialed_causes_add(const struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
    {
    	struct ast_control_pvt_cause_code *ao2_cause_code;
    	ao2_find(chan->dialed_causes, cause_code->chan_name, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA);
    	ao2_cause_code = ao2_alloc(datalen, NULL);
    
    	if (ao2_cause_code) {
    		memcpy(ao2_cause_code, cause_code, datalen);
    		ao2_link(chan->dialed_causes, ao2_cause_code);
    		ao2_ref(ao2_cause_code, -1);
    		return 0;
    	} else {
    		return -1;
    	}
    }
    
    void ast_channel_dialed_causes_clear(const struct ast_channel *chan)
    {
    	ao2_callback(chan->dialed_causes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
    }
    
    /* \brief Hash function for pvt cause code frames */
    static int pvt_cause_hash_fn(const void *vpc, const int flags)
    {
    	const struct ast_control_pvt_cause_code *pc = vpc;
    	return ast_str_hash(ast_tech_to_upper(ast_strdupa(pc->chan_name)));
    }
    
    /* \brief Comparison function for pvt cause code frames */
    static int pvt_cause_cmp_fn(void *obj, void *vstr, int flags)
    {
    	struct ast_control_pvt_cause_code *pc = obj;
    	char *str = ast_tech_to_upper(ast_strdupa(vstr));
    	char *pc_str = ast_tech_to_upper(ast_strdupa(pc->chan_name));
    	return !strcmp(pc_str, str) ? CMP_MATCH | CMP_STOP : 0;
    }
    
    #define DIALED_CAUSES_BUCKETS 37
    
    
    struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj), const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *file, int line, const char *function)
    
    {
    	struct ast_channel *tmp;
    
    #if defined(REF_DEBUG)
    	tmp = __ao2_alloc_debug(sizeof(*tmp), destructor,
    		AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function, 1);
    #elif defined(__AST_DEBUG_MALLOC)
    	tmp = __ao2_alloc_debug(sizeof(*tmp), destructor,
    		AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function, 0);
    #else
    	tmp = ao2_alloc(sizeof(*tmp), destructor);
    #endif
    
    
    	if ((ast_string_field_init(tmp, 128))) {
    		return ast_channel_unref(tmp);
    	}
    
    
    	if (!(tmp->dialed_causes = ao2_container_alloc(DIALED_CAUSES_BUCKETS, pvt_cause_hash_fn, pvt_cause_cmp_fn))) {
    
    	/* set the creation time in the uniqueid */
    	tmp->uniqueid.creation_time = time(NULL);
    	tmp->uniqueid.creation_unique = ast_atomic_fetchadd_int(&uniqueint, 1);
    
    	/* use provided id or default to historical {system-}time.# format */
    	if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
    		ast_copy_string(tmp->uniqueid.unique_id, assignedids->uniqueid, sizeof(tmp->uniqueid.unique_id));
    	} else if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
    		snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%li.%d",
    			(long)(tmp->uniqueid.creation_time),
    			tmp->uniqueid.creation_unique);
    
    		snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%s-%li.%d",
    			ast_config_AST_SYSTEM_NAME,
    			(long)(tmp->uniqueid.creation_time),
    			tmp->uniqueid.creation_unique);
    
    	/* copy linked id from parent channel if known */
    	if (requestor) {
    		tmp->linkedid = requestor->linkedid;
    
    struct ast_channel *ast_channel_internal_oldest_linkedid(struct ast_channel *a, struct ast_channel *b)
    {
    	ast_assert(a->linkedid.creation_time != 0);
    	ast_assert(b->linkedid.creation_time != 0);
    
    	if (a->linkedid.creation_time < b->linkedid.creation_time) {
    		return a;
    	}
    	if (b->linkedid.creation_time < a->linkedid.creation_time) {
    		return b;
    	}
    	if (a->linkedid.creation_unique < b->linkedid.creation_unique) {
    		return a;
    	}
    	return b;
    }
    
    void ast_channel_internal_copy_linkedid(struct ast_channel *dest, struct ast_channel *source)
    {
    
    	if (dest->linkedid.creation_time == source->linkedid.creation_time
    		&& dest->linkedid.creation_unique == source->linkedid.creation_unique
    		&& !strcmp(dest->linkedid.unique_id, source->linkedid.unique_id)) {
    		return;
    	}
    
    	dest->linkedid = source->linkedid;
    
    	ast_channel_publish_snapshot(dest);
    
    }
    
    void ast_channel_internal_swap_uniqueid_and_linkedid(struct ast_channel *a, struct ast_channel *b)
    {
    	struct ast_channel_id temp;
    
    	temp = a->uniqueid;
    	a->uniqueid = b->uniqueid;
    	b->uniqueid = temp;
    
    	temp = a->linkedid;
    	a->linkedid = b->linkedid;
    	b->linkedid = temp;
    }
    
    
    void ast_channel_internal_swap_topics(struct ast_channel *a, struct ast_channel *b)
    {
    	struct stasis_cp_single *temp;
    
    	temp = a->topics;
    	a->topics = b->topics;
    	b->topics = temp;
    }
    
    
    void ast_channel_internal_set_fake_ids(struct ast_channel *chan, const char *uniqueid, const char *linkedid)
    {
    
    	ast_copy_string(chan->uniqueid.unique_id, uniqueid, sizeof(chan->uniqueid.unique_id));
    	ast_copy_string(chan->linkedid.unique_id, linkedid, sizeof(chan->linkedid.unique_id));
    
    void ast_channel_internal_cleanup(struct ast_channel *chan)
    {
    
    	if (chan->dialed_causes) {
    		ao2_t_ref(chan->dialed_causes, -1,
    			"done with dialed causes since the channel is going away");
    		chan->dialed_causes = NULL;
    	}
    
    
    	ast_string_field_free_memory(chan);
    
    	chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
    
    	chan->endpoint_cache_forward = stasis_forward_cancel(chan->endpoint_cache_forward);
    
    	stasis_cp_single_unsubscribe(chan->topics);
    	chan->topics = NULL;
    
    }
    
    void ast_channel_internal_finalize(struct ast_channel *chan)
    {
    	chan->finalized = 1;
    }
    
    int ast_channel_internal_is_finalized(struct ast_channel *chan)
    {
    	return chan->finalized;
    }
    
    
    struct stasis_topic *ast_channel_topic(struct ast_channel *chan)
    {
    
    	if (!chan) {
    		return ast_channel_topic_all();
    	}
    
    	return stasis_cp_single_topic(chan->topics);
    
    struct stasis_topic *ast_channel_topic_cached(struct ast_channel *chan)
    {
    	if (!chan) {
    		return ast_channel_topic_all_cached();
    	}
    
    	return stasis_cp_single_topic_cached(chan->topics);
    }
    
    int ast_channel_forward_endpoint(struct ast_channel *chan,
    	struct ast_endpoint *endpoint)
    
    {
    	ast_assert(chan != NULL);
    	ast_assert(endpoint != NULL);
    
    	chan->endpoint_forward =
    
    		stasis_forward_all(ast_channel_topic(chan),
    			ast_endpoint_topic(endpoint));
    
    	chan->endpoint_cache_forward = stasis_forward_all(ast_channel_topic_cached(chan),
    		ast_endpoint_topic(endpoint));
    	if (!chan->endpoint_cache_forward) {
    		chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
    
    int ast_channel_internal_setup_topics(struct ast_channel *chan)
    
    	const char *topic_name = chan->uniqueid.unique_id;
    
    	ast_assert(chan->topics == NULL);
    
    
    	if (ast_strlen_zero(topic_name)) {
    		topic_name = "<dummy-channel>";
    	}
    
    
    	chan->topics = stasis_cp_single_create(
    		ast_channel_cache_all(), topic_name);
    	if (!chan->topics) {
    		return -1;
    	}
    
    	return 0;
    
    
    AST_THREADSTORAGE(channel_errno);
    
    void ast_channel_internal_errno_set(enum ast_channel_error error)
    {
    	enum ast_channel_error *error_code = ast_threadstorage_get(&channel_errno, sizeof(*error_code));
    	if (!error_code) {
    		return;
    	}
    
    	*error_code = error;
    }
    
    enum ast_channel_error ast_channel_internal_errno(void)
    {
    	enum ast_channel_error *error_code = ast_threadstorage_get(&channel_errno, sizeof(*error_code));
    	if (!error_code) {
    		return AST_CHANNEL_ERROR_UNKNOWN;
    	}
    
    	return *error_code;
    }