Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Enumerations | Functions | Variables
core_local.c File Reference

Local proxy channel driver. More...

#include "asterisk.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/cli.h"
#include "asterisk/manager.h"
#include "asterisk/devicestate.h"
#include "asterisk/astobj2.h"
#include "asterisk/bridge.h"
#include "asterisk/core_unreal.h"
#include "asterisk/core_local.h"
#include "asterisk/stasis.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/_private.h"
#include "asterisk/stream.h"
#include "asterisk/translate.h"

Go to the source code of this file.

Data Structures

struct  local_bridge
 
struct  local_pvt
 the local pvt structure for all channels More...
 

Enumerations

enum  local_call_action { LOCAL_CALL_ACTION_DIALPLAN, LOCAL_CALL_ACTION_BRIDGE, LOCAL_CALL_ACTION_MASQUERADE }
 

Functions

struct ast_channelast_local_get_peer (struct ast_channel *ast)
 Get the other local channel in the pair. More...
 
int ast_local_init (void)
 Initialize the local proxy channel. More...
 
void ast_local_lock_all (struct ast_channel *chan, void **tech_pvt, struct ast_channel **base_chan, struct ast_channel **base_owner)
 Add a reference to the local channel's private tech, lock the local channel's private base, and add references and lock both sides of the local channel. More...
 
int ast_local_setup_bridge (struct ast_channel *ast, struct ast_bridge *bridge, struct ast_channel *swap, struct ast_bridge_features *features)
 Setup the outgoing local channel to join a bridge on ast_call(). More...
 
int ast_local_setup_masquerade (struct ast_channel *ast, struct ast_channel *masq)
 Setup the outgoing local channel to masquerade into a channel on ast_call(). More...
 
void ast_local_unlock_all (void *tech_pvt, struct ast_channel *base_chan, struct ast_channel *base_owner)
 Remove a reference to the given local channel's private tech, unlock the given local channel's private base, and remove references and unlock both sides of given the local channel. More...
 
static struct local_pvtlocal_alloc (const char *data, struct ast_stream_topology *topology)
 Create a call structure.
 
static int local_call (struct ast_channel *ast, const char *dest, int timeout)
 Initiate new call, part of PBX interface dest is the dial string.
 
static struct ast_multi_channel_bloblocal_channel_optimization_blob (struct local_pvt *p, struct ast_json *json_object)
 
static int local_devicestate (const char *data)
 Adds devicestate to local channels.
 
static int local_hangup (struct ast_channel *ast)
 Hangup a call through the local proxy channel.
 
static struct ast_manager_event_bloblocal_message_to_ami (struct stasis_message *msg)
 
static void local_optimization_finished_cb (struct ast_unreal_pvt *base, int success, unsigned int id)
 Callback for ast_unreal_pvt_callbacks optimization_finished.
 
static void local_optimization_started_cb (struct ast_unreal_pvt *base, struct ast_channel *source, enum ast_unreal_channel_indicator dest, unsigned int id)
 Callback for ast_unreal_pvt_callbacks optimization_started.
 
static void local_pvt_destructor (void *vdoomed)
 
static struct ast_channellocal_request (const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
 Part of PBX interface.
 
static struct ast_channellocal_request_with_stream_topology (const char *type, struct ast_stream_topology *topology, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
 Part of PBX interface.
 
static int local_setoption (struct ast_channel *chan, int option, void *data, int datalen)
 
static void local_shutdown (void)
 
static int locals_cmp_cb (void *obj, void *arg, int flags)
 
static char * locals_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 CLI command "local show channels".
 
static int manager_optimize_away (struct mansession *s, const struct message *m)
 
static void publish_local_bridge_message (struct local_pvt *p)
 
 STASIS_MESSAGE_TYPE_DEFN (ast_local_bridge_type,.to_ami=local_message_to_ami,)
 Define local channel message types.
 
 STASIS_MESSAGE_TYPE_DEFN (ast_local_optimization_begin_type,.to_ami=local_message_to_ami,)
 
 STASIS_MESSAGE_TYPE_DEFN (ast_local_optimization_end_type,.to_ami=local_message_to_ami,)
 

Variables

static struct ast_cli_entry cli_local []
 
static struct ast_channel_tech local_tech
 
struct ast_unreal_pvt_callbacks local_unreal_callbacks
 Callbacks from the unreal core when channel optimization occurs. More...
 
static struct ao2_containerlocals
 
static const char tdesc [] = "Local Proxy Channel Driver"
 

Detailed Description

Local proxy channel driver.

Author
Richard Mudgett rmudg.nosp@m.ett@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

See Also:

Definition in file core_local.c.

Enumeration Type Documentation

What to do with the ;2 channel when ast_call() happens.

Definition at line 197 of file core_local.c.

197  {
198  /* The ast_call() will run dialplan on the ;2 channel. */
199  LOCAL_CALL_ACTION_DIALPLAN,
200  /* The ast_call() will impart the ;2 channel into a bridge. */
201  LOCAL_CALL_ACTION_BRIDGE,
202  /* The ast_call() will masquerade the ;2 channel into a channel. */
203  LOCAL_CALL_ACTION_MASQUERADE,
204 };

Function Documentation

struct ast_channel* ast_local_get_peer ( struct ast_channel ast)

Get the other local channel in the pair.

Since
12.0.0
Parameters
astLocal channel to get peer.
Note
On entry, ast must be locked.
Return values
peerreffed on success.
NULLif no peer or error.

Definition at line 276 of file core_local.c.

References ao2_ref, ast_channel_ref, local_pvt::base, ast_unreal_pvt::chan, and ast_unreal_pvt::owner.

277 {
278  struct local_pvt *p = ast_channel_tech_pvt(ast);
279  struct local_pvt *found;
280  struct ast_channel *peer;
281 
282  if (!p) {
283  return NULL;
284  }
285 
286  found = p ? ao2_find(locals, p, 0) : NULL;
287  if (!found) {
288  /* ast is either not a local channel or it has alredy been hungup */
289  return NULL;
290  }
291  ao2_lock(found);
292  if (ast == p->base.owner) {
293  peer = p->base.chan;
294  } else if (ast == p->base.chan) {
295  peer = p->base.owner;
296  } else {
297  peer = NULL;
298  }
299  if (peer) {
300  ast_channel_ref(peer);
301  }
302  ao2_unlock(found);
303  ao2_ref(found, -1);
304  return peer;
305 }
Main Channel structure associated with a channel.
struct ast_channel * owner
Definition: core_unreal.h:93
struct ast_unreal_pvt base
Definition: core_local.c:225
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
struct ast_channel * chan
Definition: core_unreal.h:94
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2947
the local pvt structure for all channels
Definition: core_local.c:223
int ast_local_init ( void  )

Initialize the local proxy channel.

Since
12.0.0
Return values
0on success.
-1on error.

Definition at line 1139 of file core_local.c.

References AO2_ALLOC_OPT_LOCK_MUTEX, ao2_container_alloc_list, ast_channel_register(), ast_cli_register_multiple, ast_format_cap_alloc, ast_format_cap_append_by_type(), AST_FORMAT_CAP_FLAG_DEFAULT, ast_local_bridge_type(), ast_local_optimization_begin_type(), ast_local_optimization_end_type(), ast_manager_register_xml_core, ast_register_cleanup(), ast_channel_tech::capabilities, and STASIS_MESSAGE_TYPE_INIT.

1140 {
1142  return -1;
1143  }
1144 
1146  return -1;
1147  }
1148 
1150  return -1;
1151  }
1152 
1154  return -1;
1155  }
1156  ast_format_cap_append_by_type(local_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
1157 
1158  locals = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, locals_cmp_cb);
1159  if (!locals) {
1160  return -1;
1161  }
1162 
1163  /* Make sure we can register our channel type */
1164  if (ast_channel_register(&local_tech)) {
1165  ast_log(LOG_ERROR, "Unable to register channel class 'Local'\n");
1166 
1167  return -1;
1168  }
1169  ast_cli_register_multiple(cli_local, ARRAY_LEN(cli_local));
1170  ast_manager_register_xml_core("LocalOptimizeAway", EVENT_FLAG_SYSTEM|EVENT_FLAG_CALL, manager_optimize_away);
1171 
1172  ast_register_cleanup(local_shutdown);
1173  return 0;
1174 }
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1493
#define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn)
Allocate and initialize a list container.
Definition: astobj2.h:1327
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
int ast_format_cap_append_by_type(struct ast_format_cap *cap, enum ast_media_type type)
Add all codecs Asterisk knows about for a specific type to the capabilities structure.
Definition: format_cap.c:216
int ast_channel_register(const struct ast_channel_tech *tech)
Register a channel technology (a new channel driver) Called by a channel module to register the kind ...
Definition: channel.c:539
#define ast_manager_register_xml_core(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
Definition: manager.h:202
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition: format_cap.h:49
struct stasis_message_type * ast_local_optimization_end_type(void)
Message type for when a local channel optimization completes.
struct ast_format_cap * capabilities
Definition: channel.h:632
struct stasis_message_type * ast_local_bridge_type(void)
Message type for when two local channel halves are bridged together.
struct stasis_message_type * ast_local_optimization_begin_type(void)
Message type for when a local channel optimization begins.
void ast_local_lock_all ( struct ast_channel chan,
void **  tech_pvt,
struct ast_channel **  base_chan,
struct ast_channel **  base_owner 
)

Add a reference to the local channel's private tech, lock the local channel's private base, and add references and lock both sides of the local channel.

Note
None of these locks should be held prior to calling this function.
To undo this process call ast_local_unlock_all2.
Since
13.17.0, 14.6.0
Parameters
chanMust be a local channel
[out]tech_pvtchannel's private tech (ref and lock added)
[out]base_chanOne side of the local channel (ref and lock added)
[out]base_ownerOther side of the local channel (ref and lock added)

Definition at line 241 of file core_local.c.

References ao2_bump, ast_unreal_lock_all(), and local_pvt::base.

Referenced by attended_transfer_bridge().

243 {
244  struct local_pvt *p = ast_channel_tech_pvt(chan);
245 
246  *tech_pvt = NULL;
247  *base_chan = NULL;
248  *base_owner = NULL;
249 
250  if (p) {
251  *tech_pvt = ao2_bump(p);
252  ast_unreal_lock_all(&p->base, base_chan, base_owner);
253  }
254 }
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
struct ast_unreal_pvt base
Definition: core_local.c:225
the local pvt structure for all channels
Definition: core_local.c:223
void ast_unreal_lock_all(struct ast_unreal_pvt *p, struct ast_channel **outchan, struct ast_channel **outowner)
Send an unreal pvt in with no locks held and get all locks.
Definition: core_unreal.c:47
int ast_local_setup_bridge ( struct ast_channel ast,
struct ast_bridge bridge,
struct ast_channel swap,
struct ast_bridge_features features 
)

Setup the outgoing local channel to join a bridge on ast_call().

Since
12.0.0
Parameters
astEither channel of a local channel pair.
bridgeBridge to join.
swapChannel to swap with when joining.
featuresBridge features structure.
Note
The features parameter must be NULL or obtained by ast_bridge_features_new(). You must not dereference features after calling even if the call fails.
Intended to be called after ast_request() and before ast_call() on a local channel.
Return values
0on success.
-1on error.

Definition at line 601 of file core_local.c.

References local_pvt::action, ao2_ref, ast_bridge_features_destroy(), ast_channel_ref, AST_UNREAL_CARETAKER_THREAD, local_pvt::base, local_pvt::bridge, ast_unreal_pvt::chan, local_bridge::features, local_bridge::join, ast_unreal_pvt::owner, local_bridge::swap, and local_pvt::type.

Referenced by attended_transfer_bridge().

602 {
603  struct local_pvt *p;
604  struct local_pvt *found;
605  int res = -1;
606 
607  /* Sanity checks. */
608  if (!ast || !bridge) {
609  ast_bridge_features_destroy(features);
610  return -1;
611  }
612 
613  ast_channel_lock(ast);
614  p = ast_channel_tech_pvt(ast);
615  ast_channel_unlock(ast);
616 
617  found = p ? ao2_find(locals, p, 0) : NULL;
618  if (found) {
619  ao2_lock(found);
620  if (found->type == LOCAL_CALL_ACTION_DIALPLAN
621  && found->base.owner
622  && found->base.chan
623  && !ast_test_flag(&found->base, AST_UNREAL_CARETAKER_THREAD)) {
624  ao2_ref(bridge, +1);
625  if (swap) {
626  ast_channel_ref(swap);
627  }
628  found->type = LOCAL_CALL_ACTION_BRIDGE;
629  found->action.bridge.join = bridge;
630  found->action.bridge.swap = swap;
631  found->action.bridge.features = features;
632  res = 0;
633  } else {
634  ast_bridge_features_destroy(features);
635  }
636  ao2_unlock(found);
637  ao2_ref(found, -1);
638  }
639 
640  return res;
641 }
struct ast_channel * owner
Definition: core_unreal.h:93
struct ast_channel * swap
Definition: core_local.c:211
#define AST_UNREAL_CARETAKER_THREAD
Definition: core_unreal.h:107
void ast_bridge_features_destroy(struct ast_bridge_features *features)
Destroy an allocated bridge features struct.
Definition: bridge.c:3674
struct ast_unreal_pvt base
Definition: core_local.c:225
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
struct ast_bridge_features * features
Definition: core_local.c:213
struct ast_channel * chan
Definition: core_unreal.h:94
enum local_call_action type
Definition: core_local.c:234
union local_pvt::@342 action
struct ast_bridge * join
Definition: core_local.c:209
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2947
the local pvt structure for all channels
Definition: core_local.c:223
struct local_bridge bridge
Definition: core_local.c:229
int ast_local_setup_masquerade ( struct ast_channel ast,
struct ast_channel masq 
)

Setup the outgoing local channel to masquerade into a channel on ast_call().

Since
12.0.0
Parameters
astEither channel of a local channel pair.
masqChannel to masquerade into.
Note
Intended to be called after ast_request() and before ast_call() on a local channel.
Return values
0on success.
-1on error.

Definition at line 643 of file core_local.c.

References local_pvt::action, ao2_ref, ast_channel_ref, AST_UNREAL_CARETAKER_THREAD, local_pvt::base, ast_unreal_pvt::chan, local_pvt::masq, ast_unreal_pvt::owner, and local_pvt::type.

Referenced by attended_transfer_bridge().

644 {
645  struct local_pvt *p;
646  struct local_pvt *found;
647  int res = -1;
648 
649  /* Sanity checks. */
650  if (!ast || !masq) {
651  return -1;
652  }
653 
654  ast_channel_lock(ast);
655  p = ast_channel_tech_pvt(ast);
656  ast_channel_unlock(ast);
657 
658  found = p ? ao2_find(locals, p, 0) : NULL;
659  if (found) {
660  ao2_lock(found);
661  if (found->type == LOCAL_CALL_ACTION_DIALPLAN
662  && found->base.owner
663  && found->base.chan
664  && !ast_test_flag(&found->base, AST_UNREAL_CARETAKER_THREAD)) {
665  ast_channel_ref(masq);
666  found->type = LOCAL_CALL_ACTION_MASQUERADE;
667  found->action.masq = masq;
668  res = 0;
669  }
670  ao2_unlock(found);
671  ao2_ref(found, -1);
672  }
673 
674  return res;
675 }
struct ast_channel * owner
Definition: core_unreal.h:93
#define AST_UNREAL_CARETAKER_THREAD
Definition: core_unreal.h:107
struct ast_unreal_pvt base
Definition: core_local.c:225
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
struct ast_channel * chan
Definition: core_unreal.h:94
enum local_call_action type
Definition: core_local.c:234
union local_pvt::@342 action
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2947
the local pvt structure for all channels
Definition: core_local.c:223
struct ast_channel * masq
Definition: core_local.c:231
void ast_local_unlock_all ( void *  tech_pvt,
struct ast_channel base_chan,
struct ast_channel base_owner 
)

Remove a reference to the given local channel's private tech, unlock the given local channel's private base, and remove references and unlock both sides of given the local channel.

Note
This function should be used in conjunction with ast_local_lock_all2.
Since
13.17.0, 14.6.0
Parameters
tech_pvtchannel's private tech (ref and lock removed)
base_chanOne side of the local channel (ref and lock removed)
base_ownerOther side of the local channel (ref and lock removed)

Definition at line 256 of file core_local.c.

References ao2_ref, ast_channel_unref, and local_pvt::base.

Referenced by attended_transfer_bridge().

258 {
259  if (base_chan) {
260  ast_channel_unlock(base_chan);
261  ast_channel_unref(base_chan);
262  }
263 
264  if (base_owner) {
265  ast_channel_unlock(base_owner);
266  ast_channel_unref(base_owner);
267  }
268 
269  if (tech_pvt) {
270  struct local_pvt *p = tech_pvt;
271  ao2_unlock(&p->base);
272  ao2_ref(tech_pvt, -1);
273  }
274 }
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2958
struct ast_unreal_pvt base
Definition: core_local.c:225
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
the local pvt structure for all channels
Definition: core_local.c:223

Variable Documentation

struct ast_cli_entry cli_local[]
static
Initial value:
= {
{ .handler = locals_show , .summary = "List status of local channels" ,},
}
static char * locals_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
CLI command "local show channels".
Definition: core_local.c:1031

Definition at line 1070 of file core_local.c.

struct ast_unreal_pvt_callbacks local_unreal_callbacks
Initial value:
= {
.optimization_started = local_optimization_started_cb,
.optimization_finished = local_optimization_finished_cb,
}
static void local_optimization_finished_cb(struct ast_unreal_pvt *base, int success, unsigned int id)
Callback for ast_unreal_pvt_callbacks optimization_finished.
Definition: core_local.c:437
static void local_optimization_started_cb(struct ast_unreal_pvt *base, struct ast_channel *source, enum ast_unreal_channel_indicator dest, unsigned int id)
Callback for ast_unreal_pvt_callbacks optimization_started.
Definition: core_local.c:394

Callbacks from the unreal core when channel optimization occurs.

Definition at line 167 of file core_local.c.

Referenced by local_alloc().