<p>N A has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/19606">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">app_sla: Migrate SLA applications out of app_meetme.<br><br>This removes the dependency of the SLAStation and SLATrunk<br>applications on app_meetme, in anticipation of the imminent<br>removal of the deprecated app_meetme module.<br><br>The user interface for the SLA applications is exactly the<br>same, and in theory, users should not notice a difference.<br>However, the SLA applications now use ConfBridge under the<br>hood, rather than MeetMe, and they are now contained within<br>their own module.<br><br>ASTERISK-30309 #close<br><br>Change-Id: I8604cb332b219f6ed5394a84c381b5958d115aee<br>---<br>M apps/app_meetme.c<br>A apps/app_sla.c<br>A doc/UPGRADE-staging/app_sla.txt<br>3 files changed, 2,907 insertions(+), 2,620 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/06/19606/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/apps/app_meetme.c b/apps/app_meetme.c</span><br><span>index 8b5bd40..c264d63 100644</span><br><span>--- a/apps/app_meetme.c</span><br><span>+++ b/apps/app_meetme.c</span><br><span>@@ -5,9 +5,6 @@</span><br><span> *</span><br><span> * Mark Spencer <markster@digium.com></span><br><span> *</span><br><span style="color: hsl(0, 100%, 40%);">- * SLA Implementation by:</span><br><span style="color: hsl(0, 100%, 40%);">- * Russell Bryant <russell@digium.com></span><br><span style="color: hsl(0, 100%, 40%);">- *</span><br><span> * See http://www.asterisk.org for more information about</span><br><span> * the Asterisk project. Please do not directly contact</span><br><span> * any of the maintainers of this project for assistance;</span><br><span>@@ -21,10 +18,9 @@</span><br><span> </span><br><span> /*! \file</span><br><span> *</span><br><span style="color: hsl(0, 100%, 40%);">- * \brief Meet me conference bridge and Shared Line Appearances</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Meet me conference bridge</span><br><span> *</span><br><span> * \author Mark Spencer <markster@digium.com></span><br><span style="color: hsl(0, 100%, 40%);">- * \author (SLA) Russell Bryant <russell@digium.com></span><br><span> *</span><br><span> * \ingroup applications</span><br><span> */</span><br><span>@@ -409,67 +405,6 @@</span><br><span> <replaceable>channel</replaceable> in any conference.</para></span><br><span> </description></span><br><span> </application></span><br><span style="color: hsl(0, 100%, 40%);">- <application name="SLAStation" language="en_US"></span><br><span style="color: hsl(0, 100%, 40%);">- <synopsis></span><br><span style="color: hsl(0, 100%, 40%);">- Shared Line Appearance Station.</span><br><span style="color: hsl(0, 100%, 40%);">- </synopsis></span><br><span style="color: hsl(0, 100%, 40%);">- <syntax></span><br><span style="color: hsl(0, 100%, 40%);">- <parameter name="station" required="true"></span><br><span style="color: hsl(0, 100%, 40%);">- <para>Station name</para></span><br><span style="color: hsl(0, 100%, 40%);">- </parameter></span><br><span style="color: hsl(0, 100%, 40%);">- </syntax></span><br><span style="color: hsl(0, 100%, 40%);">- <description></span><br><span style="color: hsl(0, 100%, 40%);">- <para>This application should be executed by an SLA station. The argument depends</span><br><span style="color: hsl(0, 100%, 40%);">- on how the call was initiated. If the phone was just taken off hook, then the argument</span><br><span style="color: hsl(0, 100%, 40%);">- <replaceable>station</replaceable> should be just the station name. If the call was</span><br><span style="color: hsl(0, 100%, 40%);">- initiated by pressing a line key, then the station name should be preceded by an underscore</span><br><span style="color: hsl(0, 100%, 40%);">- and the trunk name associated with that line button.</para></span><br><span style="color: hsl(0, 100%, 40%);">- <para>For example: <literal>station1_line1</literal></para></span><br><span style="color: hsl(0, 100%, 40%);">- <para>On exit, this application will set the variable <variable>SLASTATION_STATUS</variable> to</span><br><span style="color: hsl(0, 100%, 40%);">- one of the following values:</para></span><br><span style="color: hsl(0, 100%, 40%);">- <variablelist></span><br><span style="color: hsl(0, 100%, 40%);">- <variable name="SLASTATION_STATUS"></span><br><span style="color: hsl(0, 100%, 40%);">- <value name="FAILURE" /></span><br><span style="color: hsl(0, 100%, 40%);">- <value name="CONGESTION" /></span><br><span style="color: hsl(0, 100%, 40%);">- <value name="SUCCESS" /></span><br><span style="color: hsl(0, 100%, 40%);">- </variable></span><br><span style="color: hsl(0, 100%, 40%);">- </variablelist></span><br><span style="color: hsl(0, 100%, 40%);">- </description></span><br><span style="color: hsl(0, 100%, 40%);">- </application></span><br><span style="color: hsl(0, 100%, 40%);">- <application name="SLATrunk" language="en_US"></span><br><span style="color: hsl(0, 100%, 40%);">- <synopsis></span><br><span style="color: hsl(0, 100%, 40%);">- Shared Line Appearance Trunk.</span><br><span style="color: hsl(0, 100%, 40%);">- </synopsis></span><br><span style="color: hsl(0, 100%, 40%);">- <syntax></span><br><span style="color: hsl(0, 100%, 40%);">- <parameter name="trunk" required="true"></span><br><span style="color: hsl(0, 100%, 40%);">- <para>Trunk name</para></span><br><span style="color: hsl(0, 100%, 40%);">- </parameter></span><br><span style="color: hsl(0, 100%, 40%);">- <parameter name="options"></span><br><span style="color: hsl(0, 100%, 40%);">- <optionlist></span><br><span style="color: hsl(0, 100%, 40%);">- <option name="M" hasparams="optional"></span><br><span style="color: hsl(0, 100%, 40%);">- <para>Play back the specified MOH <replaceable>class</replaceable></span><br><span style="color: hsl(0, 100%, 40%);">- instead of ringing</para></span><br><span style="color: hsl(0, 100%, 40%);">- <argument name="class" required="true" /></span><br><span style="color: hsl(0, 100%, 40%);">- </option></span><br><span style="color: hsl(0, 100%, 40%);">- </optionlist></span><br><span style="color: hsl(0, 100%, 40%);">- </parameter></span><br><span style="color: hsl(0, 100%, 40%);">- </syntax></span><br><span style="color: hsl(0, 100%, 40%);">- <description></span><br><span style="color: hsl(0, 100%, 40%);">- <para>This application should be executed by an SLA trunk on an inbound call. The channel calling</span><br><span style="color: hsl(0, 100%, 40%);">- this application should correspond to the SLA trunk with the name <replaceable>trunk</replaceable></span><br><span style="color: hsl(0, 100%, 40%);">- that is being passed as an argument.</para></span><br><span style="color: hsl(0, 100%, 40%);">- <para>On exit, this application will set the variable <variable>SLATRUNK_STATUS</variable> to</span><br><span style="color: hsl(0, 100%, 40%);">- one of the following values:</para></span><br><span style="color: hsl(0, 100%, 40%);">- <variablelist></span><br><span style="color: hsl(0, 100%, 40%);">- <variable name="SLATRUNK_STATUS"></span><br><span style="color: hsl(0, 100%, 40%);">- <value name="FAILURE" /></span><br><span style="color: hsl(0, 100%, 40%);">- <value name="SUCCESS" /></span><br><span style="color: hsl(0, 100%, 40%);">- <value name="UNANSWERED" /></span><br><span style="color: hsl(0, 100%, 40%);">- <value name="RINGTIMEOUT" /></span><br><span style="color: hsl(0, 100%, 40%);">- </variable></span><br><span style="color: hsl(0, 100%, 40%);">- </variablelist></span><br><span style="color: hsl(0, 100%, 40%);">- </description></span><br><span style="color: hsl(0, 100%, 40%);">- </application></span><br><span> <function name="MEETME_INFO" language="en_US"></span><br><span> <synopsis></span><br><span> Query a given conference of various properties.</span><br><span>@@ -718,7 +653,6 @@</span><br><span> ***/</span><br><span> </span><br><span> #define CONFIG_FILE_NAME "meetme.conf"</span><br><span style="color: hsl(0, 100%, 40%);">-#define SLA_CONFIG_FILE "sla.conf"</span><br><span> #define STR_CONCISE "concise"</span><br><span> </span><br><span> /*! each buffer is 20ms, so this is 640ms total */</span><br><span>@@ -811,12 +745,10 @@</span><br><span> CONFFLAG_STARTMUTED = (1 << 24),</span><br><span> /*! Pass DTMF through the conference */</span><br><span> CONFFLAG_PASS_DTMF = (1 << 25),</span><br><span style="color: hsl(0, 100%, 40%);">- CONFFLAG_SLA_STATION = (1 << 26),</span><br><span style="color: hsl(0, 100%, 40%);">- CONFFLAG_SLA_TRUNK = (1 << 27),</span><br><span> /*! If set, the user should continue in the dialplan if kicked out */</span><br><span style="color: hsl(0, 100%, 40%);">- CONFFLAG_KICK_CONTINUE = (1 << 28),</span><br><span style="color: hsl(0, 100%, 40%);">- CONFFLAG_DURATION_STOP = (1 << 29),</span><br><span style="color: hsl(0, 100%, 40%);">- CONFFLAG_DURATION_LIMIT = (1 << 30),</span><br><span style="color: hsl(120, 100%, 40%);">+ CONFFLAG_KICK_CONTINUE = (1 << 26),</span><br><span style="color: hsl(120, 100%, 40%);">+ CONFFLAG_DURATION_STOP = (1 << 27),</span><br><span style="color: hsl(120, 100%, 40%);">+ CONFFLAG_DURATION_LIMIT = (1 << 28),</span><br><span> };</span><br><span> </span><br><span> /* These flags are defined separately because we ran out of bits that an enum can be used to represent.</span><br><span>@@ -881,8 +813,6 @@</span><br><span> static const char * const app2 = "MeetMeCount";</span><br><span> static const char * const app3 = "MeetMeAdmin";</span><br><span> static const char * const app4 = "MeetMeChannelAdmin";</span><br><span style="color: hsl(0, 100%, 40%);">-static const char * const slastation_app = "SLAStation";</span><br><span style="color: hsl(0, 100%, 40%);">-static const char * const slatrunk_app = "SLATrunk";</span><br><span> </span><br><span> /* Lookup RealTime conferences based on confno and current time */</span><br><span> static int rt_schedule;</span><br><span>@@ -993,192 +923,6 @@</span><br><span> AST_LIST_ENTRY(ast_conf_user) list;</span><br><span> };</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-enum sla_which_trunk_refs {</span><br><span style="color: hsl(0, 100%, 40%);">- ALL_TRUNK_REFS,</span><br><span style="color: hsl(0, 100%, 40%);">- INACTIVE_TRUNK_REFS,</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-enum sla_trunk_state {</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_TRUNK_STATE_IDLE,</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_TRUNK_STATE_RINGING,</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_TRUNK_STATE_UP,</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_TRUNK_STATE_ONHOLD,</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_TRUNK_STATE_ONHOLD_BYME,</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-enum sla_hold_access {</span><br><span style="color: hsl(0, 100%, 40%);">- /*! This means that any station can put it on hold, and any station</span><br><span style="color: hsl(0, 100%, 40%);">- * can retrieve the call from hold. */</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_HOLD_OPEN,</span><br><span style="color: hsl(0, 100%, 40%);">- /*! This means that only the station that put the call on hold may</span><br><span style="color: hsl(0, 100%, 40%);">- * retrieve it from hold. */</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_HOLD_PRIVATE,</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-struct sla_trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-struct sla_station {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_RWLIST_ENTRY(sla_station) entry;</span><br><span style="color: hsl(0, 100%, 40%);">- AST_DECLARE_STRING_FIELDS(</span><br><span style="color: hsl(0, 100%, 40%);">- AST_STRING_FIELD(name);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_STRING_FIELD(device);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_STRING_FIELD(autocontext);</span><br><span style="color: hsl(0, 100%, 40%);">- );</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_HEAD_NOLOCK(, sla_trunk_ref) trunks;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_dial *dial;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Ring timeout for this station, for any trunk. If a ring timeout</span><br><span style="color: hsl(0, 100%, 40%);">- * is set for a specific trunk on this station, that will take</span><br><span style="color: hsl(0, 100%, 40%);">- * priority over this value. */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int ring_timeout;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Ring delay for this station, for any trunk. If a ring delay</span><br><span style="color: hsl(0, 100%, 40%);">- * is set for a specific trunk on this station, that will take</span><br><span style="color: hsl(0, 100%, 40%);">- * priority over this value. */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int ring_delay;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! This option uses the values in the sla_hold_access enum and sets the</span><br><span style="color: hsl(0, 100%, 40%);">- * access control type for hold on this station. */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int hold_access:1;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Mark used during reload processing */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int mark:1;</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*!</span><br><span style="color: hsl(0, 100%, 40%);">- * \brief A reference to a station</span><br><span style="color: hsl(0, 100%, 40%);">- *</span><br><span style="color: hsl(0, 100%, 40%);">- * This struct looks near useless at first glance. However, its existence</span><br><span style="color: hsl(0, 100%, 40%);">- * in the list of stations in sla_trunk means that this station references</span><br><span style="color: hsl(0, 100%, 40%);">- * that trunk. We use the mark to keep track of whether it needs to be</span><br><span style="color: hsl(0, 100%, 40%);">- * removed from the sla_trunk's list of stations during a reload.</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-struct sla_station_ref {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_ENTRY(sla_station_ref) entry;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Mark used during reload processing */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int mark:1;</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-struct sla_trunk {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_DECLARE_STRING_FIELDS(</span><br><span style="color: hsl(0, 100%, 40%);">- AST_STRING_FIELD(name);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_STRING_FIELD(device);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_STRING_FIELD(autocontext);</span><br><span style="color: hsl(0, 100%, 40%);">- );</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_HEAD_NOLOCK(, sla_station_ref) stations;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Number of stations that use this trunk */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int num_stations;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Number of stations currently on a call with this trunk */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int active_stations;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Number of stations that have this trunk on hold. */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int hold_stations;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_channel *chan;</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int ring_timeout;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! If set to 1, no station will be able to join an active call with</span><br><span style="color: hsl(0, 100%, 40%);">- * this trunk. */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int barge_disabled:1;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! This option uses the values in the sla_hold_access enum and sets the</span><br><span style="color: hsl(0, 100%, 40%);">- * access control type for hold on this trunk. */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int hold_access:1;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Whether this trunk is currently on hold, meaning that once a station</span><br><span style="color: hsl(0, 100%, 40%);">- * connects to it, the trunk channel needs to have UNHOLD indicated to it. */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int on_hold:1;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Mark used during reload processing */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int mark:1;</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*!</span><br><span style="color: hsl(0, 100%, 40%);">- * \brief A station's reference to a trunk</span><br><span style="color: hsl(0, 100%, 40%);">- *</span><br><span style="color: hsl(0, 100%, 40%);">- * An sla_station keeps a list of trunk_refs. This holds metadata about the</span><br><span style="color: hsl(0, 100%, 40%);">- * stations usage of the trunk.</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-struct sla_trunk_ref {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_ENTRY(sla_trunk_ref) entry;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk *trunk;</span><br><span style="color: hsl(0, 100%, 40%);">- enum sla_trunk_state state;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_channel *chan;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Ring timeout to use when this trunk is ringing on this specific</span><br><span style="color: hsl(0, 100%, 40%);">- * station. This takes higher priority than a ring timeout set at</span><br><span style="color: hsl(0, 100%, 40%);">- * the station level. */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int ring_timeout;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Ring delay to use when this trunk is ringing on this specific</span><br><span style="color: hsl(0, 100%, 40%);">- * station. This takes higher priority than a ring delay set at</span><br><span style="color: hsl(0, 100%, 40%);">- * the station level. */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int ring_delay;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Mark used during reload processing */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int mark:1;</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static struct ao2_container *sla_stations;</span><br><span style="color: hsl(0, 100%, 40%);">-static struct ao2_container *sla_trunks;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static const char sla_registrar[] = "SLA";</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Event types that can be queued up for the SLA thread */</span><br><span style="color: hsl(0, 100%, 40%);">-enum sla_event_type {</span><br><span style="color: hsl(0, 100%, 40%);">- /*! A station has put the call on hold */</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_EVENT_HOLD,</span><br><span style="color: hsl(0, 100%, 40%);">- /*! The state of a dial has changed */</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_EVENT_DIAL_STATE,</span><br><span style="color: hsl(0, 100%, 40%);">- /*! The state of a ringing trunk has changed */</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_EVENT_RINGING_TRUNK,</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-struct sla_event {</span><br><span style="color: hsl(0, 100%, 40%);">- enum sla_event_type type;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_ENTRY(sla_event) entry;</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief A station that failed to be dialed</span><br><span style="color: hsl(0, 100%, 40%);">- * \note Only used by the SLA thread. */</span><br><span style="color: hsl(0, 100%, 40%);">-struct sla_failed_station {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station;</span><br><span style="color: hsl(0, 100%, 40%);">- struct timeval last_try;</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_ENTRY(sla_failed_station) entry;</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief A trunk that is ringing */</span><br><span style="color: hsl(0, 100%, 40%);">-struct sla_ringing_trunk {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk *trunk;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! The time that this trunk started ringing */</span><br><span style="color: hsl(0, 100%, 40%);">- struct timeval ring_begin;</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_HEAD_NOLOCK(, sla_station_ref) timed_out_stations;</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_ENTRY(sla_ringing_trunk) entry;</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-enum sla_station_hangup {</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_STATION_HANGUP_NORMAL,</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_STATION_HANGUP_TIMEOUT,</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief A station that is ringing */</span><br><span style="color: hsl(0, 100%, 40%);">-struct sla_ringing_station {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! The time that this station started ringing */</span><br><span style="color: hsl(0, 100%, 40%);">- struct timeval ring_begin;</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_ENTRY(sla_ringing_station) entry;</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*!</span><br><span style="color: hsl(0, 100%, 40%);">- * \brief A structure for data used by the sla thread</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static struct {</span><br><span style="color: hsl(0, 100%, 40%);">- /*! The SLA thread ID */</span><br><span style="color: hsl(0, 100%, 40%);">- pthread_t thread;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_t cond;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_t lock;</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_HEAD_NOLOCK(, sla_ringing_trunk) ringing_trunks;</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_HEAD_NOLOCK(, sla_ringing_station) ringing_stations;</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_HEAD_NOLOCK(, sla_failed_station) failed_stations;</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_HEAD_NOLOCK(, sla_event) event_q;</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int stop:1;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Attempt to handle CallerID, even though it is known not to work</span><br><span style="color: hsl(0, 100%, 40%);">- * properly in some situations. */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int attempt_callerid:1;</span><br><span style="color: hsl(0, 100%, 40%);">-} sla = {</span><br><span style="color: hsl(0, 100%, 40%);">- .thread = AST_PTHREADT_NULL,</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> /*! \brief The number of audio buffers to be allocated on pseudo channels</span><br><span> * when in a conference */</span><br><span> static int audio_buffers;</span><br><span>@@ -2070,7 +1814,6 @@</span><br><span> return CLI_SHOWUSAGE;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> static char *meetme_cmd_helper(struct ast_cli_args *a)</span><br><span> {</span><br><span> /* Process the command */</span><br><span>@@ -2191,187 +1934,11 @@</span><br><span> return meetme_cmd_helper(a);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static const char *sla_hold_str(unsigned int hold_access)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- const char *hold = "Unknown";</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- switch (hold_access) {</span><br><span style="color: hsl(0, 100%, 40%);">- case SLA_HOLD_OPEN:</span><br><span style="color: hsl(0, 100%, 40%);">- hold = "Open";</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- case SLA_HOLD_PRIVATE:</span><br><span style="color: hsl(0, 100%, 40%);">- hold = "Private";</span><br><span style="color: hsl(0, 100%, 40%);">- default:</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return hold;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static char *sla_show_trunks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct ao2_iterator i;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk *trunk;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- switch (cmd) {</span><br><span style="color: hsl(0, 100%, 40%);">- case CLI_INIT:</span><br><span style="color: hsl(0, 100%, 40%);">- e->command = "sla show trunks";</span><br><span style="color: hsl(0, 100%, 40%);">- e->usage =</span><br><span style="color: hsl(0, 100%, 40%);">- "Usage: sla show trunks\n"</span><br><span style="color: hsl(0, 100%, 40%);">- " This will list all trunks defined in sla.conf\n";</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- case CLI_GENERATE:</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, "\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=============================================================\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== Configured SLA Trunks ===================================\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=============================================================\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "===\n");</span><br><span style="color: hsl(0, 100%, 40%);">- i = ao2_iterator_init(sla_trunks, 0);</span><br><span style="color: hsl(0, 100%, 40%);">- for (; (trunk = ao2_iterator_next(&i)); ao2_ref(trunk, -1)) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station_ref *station_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- char ring_timeout[16] = "(none)";</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(trunk);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk->ring_timeout) {</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(ring_timeout, sizeof(ring_timeout), "%u Seconds", trunk->ring_timeout);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, "=== ---------------------------------------------------------\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== Trunk Name: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> Device: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> AutoContext: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> RingTimeout: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> BargeAllowed: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> HoldAccess: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> Stations ...\n",</span><br><span style="color: hsl(0, 100%, 40%);">- trunk->name, trunk->device,</span><br><span style="color: hsl(0, 100%, 40%);">- S_OR(trunk->autocontext, "(none)"),</span><br><span style="color: hsl(0, 100%, 40%);">- ring_timeout,</span><br><span style="color: hsl(0, 100%, 40%);">- trunk->barge_disabled ? "No" : "Yes",</span><br><span style="color: hsl(0, 100%, 40%);">- sla_hold_str(trunk->hold_access));</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, "=== ==> Station name: %s\n", station_ref->station->name);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, "=== ---------------------------------------------------------\n===\n");</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(trunk);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_iterator_destroy(&i);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, "=============================================================\n\n");</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return CLI_SUCCESS;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static const char *trunkstate2str(enum sla_trunk_state state)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">-#define S(e) case e: return # e;</span><br><span style="color: hsl(0, 100%, 40%);">- switch (state) {</span><br><span style="color: hsl(0, 100%, 40%);">- S(SLA_TRUNK_STATE_IDLE)</span><br><span style="color: hsl(0, 100%, 40%);">- S(SLA_TRUNK_STATE_RINGING)</span><br><span style="color: hsl(0, 100%, 40%);">- S(SLA_TRUNK_STATE_UP)</span><br><span style="color: hsl(0, 100%, 40%);">- S(SLA_TRUNK_STATE_ONHOLD)</span><br><span style="color: hsl(0, 100%, 40%);">- S(SLA_TRUNK_STATE_ONHOLD_BYME)</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- return "Uknown State";</span><br><span style="color: hsl(0, 100%, 40%);">-#undef S</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static char *sla_show_stations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct ao2_iterator i;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- switch (cmd) {</span><br><span style="color: hsl(0, 100%, 40%);">- case CLI_INIT:</span><br><span style="color: hsl(0, 100%, 40%);">- e->command = "sla show stations";</span><br><span style="color: hsl(0, 100%, 40%);">- e->usage =</span><br><span style="color: hsl(0, 100%, 40%);">- "Usage: sla show stations\n"</span><br><span style="color: hsl(0, 100%, 40%);">- " This will list all stations defined in sla.conf\n";</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- case CLI_GENERATE:</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, "\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=============================================================\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== Configured SLA Stations =================================\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=============================================================\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "===\n");</span><br><span style="color: hsl(0, 100%, 40%);">- i = ao2_iterator_init(sla_stations, 0);</span><br><span style="color: hsl(0, 100%, 40%);">- for (; (station = ao2_iterator_next(&i)); ao2_ref(station, -1)) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- char ring_timeout[16] = "(none)";</span><br><span style="color: hsl(0, 100%, 40%);">- char ring_delay[16] = "(none)";</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(station);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (station->ring_timeout) {</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(ring_timeout, sizeof(ring_timeout),</span><br><span style="color: hsl(0, 100%, 40%);">- "%u", station->ring_timeout);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (station->ring_delay) {</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(ring_delay, sizeof(ring_delay),</span><br><span style="color: hsl(0, 100%, 40%);">- "%u", station->ring_delay);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, "=== ---------------------------------------------------------\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== Station Name: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> Device: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> AutoContext: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> RingTimeout: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> RingDelay: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> HoldAccess: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> Trunks ...\n",</span><br><span style="color: hsl(0, 100%, 40%);">- station->name, station->device,</span><br><span style="color: hsl(0, 100%, 40%);">- S_OR(station->autocontext, "(none)"),</span><br><span style="color: hsl(0, 100%, 40%);">- ring_timeout, ring_delay,</span><br><span style="color: hsl(0, 100%, 40%);">- sla_hold_str(station->hold_access));</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref->ring_timeout) {</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(ring_timeout, sizeof(ring_timeout),</span><br><span style="color: hsl(0, 100%, 40%);">- "%u", trunk_ref->ring_timeout);</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- strcpy(ring_timeout, "(none)");</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref->ring_delay) {</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(ring_delay, sizeof(ring_delay),</span><br><span style="color: hsl(0, 100%, 40%);">- "%u", trunk_ref->ring_delay);</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- strcpy(ring_delay, "(none)");</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, "=== ==> Trunk Name: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> State: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> RingTimeout: %s\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "=== ==> RingDelay: %s\n",</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->trunk->name,</span><br><span style="color: hsl(0, 100%, 40%);">- trunkstate2str(trunk_ref->state),</span><br><span style="color: hsl(0, 100%, 40%);">- ring_timeout, ring_delay);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, "=== ---------------------------------------------------------\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "===\n");</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(station);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_iterator_destroy(&i);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, "============================================================\n"</span><br><span style="color: hsl(0, 100%, 40%);">- "\n");</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return CLI_SUCCESS;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> static struct ast_cli_entry cli_meetme[] = {</span><br><span> AST_CLI_DEFINE(meetme_kick_cmd, "Kick a conference or a user in a conference."),</span><br><span> AST_CLI_DEFINE(meetme_show_cmd, "List all conferences or a specific conference."),</span><br><span> AST_CLI_DEFINE(meetme_lock_cmd, "Lock or unlock a conference to new users."),</span><br><span> AST_CLI_DEFINE(meetme_mute_cmd, "Mute or unmute a conference or a user in a conference."),</span><br><span style="color: hsl(0, 100%, 40%);">- AST_CLI_DEFINE(sla_show_trunks, "Show SLA Trunks"),</span><br><span style="color: hsl(0, 100%, 40%);">- AST_CLI_DEFINE(sla_show_stations, "Show SLA Stations"),</span><br><span> };</span><br><span> </span><br><span> static void conf_flush(int fd, struct ast_channel *chan)</span><br><span>@@ -2494,90 +2061,6 @@</span><br><span> ao2_iterator_destroy(&user_iter);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_queue_event_full(enum sla_event_type type,</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref, struct sla_station *station, int lock)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_event *event;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla.thread == AST_PTHREADT_NULL) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(station, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(trunk_ref, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- return;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(event = ast_calloc(1, sizeof(*event)))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(station, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(trunk_ref, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- return;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- event->type = type;</span><br><span style="color: hsl(0, 100%, 40%);">- event->trunk_ref = trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- event->station = station;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!lock) {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_INSERT_TAIL(&sla.event_q, event, entry);</span><br><span style="color: hsl(0, 100%, 40%);">- return;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_INSERT_TAIL(&sla.event_q, event, entry);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_signal(&sla.cond);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_queue_event_nolock(enum sla_event_type type)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- sla_queue_event_full(type, NULL, NULL, 0);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_queue_event(enum sla_event_type type)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- sla_queue_event_full(type, NULL, NULL, 1);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Queue a SLA event from the conference */</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_queue_event_conf(enum sla_event_type type, struct ast_channel *chan,</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_conference *conf)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- char *trunk_name;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ao2_iterator i;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_name = ast_strdupa(conf->confno);</span><br><span style="color: hsl(0, 100%, 40%);">- strsep(&trunk_name, "_");</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_strlen_zero(trunk_name)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Invalid conference name for SLA - '%s'!\n", conf->confno);</span><br><span style="color: hsl(0, 100%, 40%);">- return;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- i = ao2_iterator_init(sla_stations, 0);</span><br><span style="color: hsl(0, 100%, 40%);">- while ((station = ao2_iterator_next(&i))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(station);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref->chan == chan && !strcmp(trunk_ref->trunk->name, trunk_name)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(trunk_ref, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(station);</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* station reference given to sla_queue_event_full() */</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(station, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_iterator_destroy(&i);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!trunk_ref) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(1, "Trunk not found for event!\n");</span><br><span style="color: hsl(0, 100%, 40%);">- return;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- sla_queue_event_full(type, trunk_ref, station, 1);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> /*! \brief Decrement reference counts, as incremented by find_conf() */</span><br><span> static int dispose_conf(struct ast_conference *conf)</span><br><span> {</span><br><span>@@ -4347,14 +3830,6 @@</span><br><span> } else if ((f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END)</span><br><span> && ast_test_flag64(confflags, CONFFLAG_PASS_DTMF)) {</span><br><span> conf_queue_dtmf(conf, user, f);</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (ast_test_flag64(confflags, CONFFLAG_SLA_STATION) && f->frametype == AST_FRAME_CONTROL) {</span><br><span style="color: hsl(0, 100%, 40%);">- switch (f->subclass.integer) {</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_CONTROL_HOLD:</span><br><span style="color: hsl(0, 100%, 40%);">- sla_queue_event_conf(SLA_EVENT_HOLD, chan, conf);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- default:</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span> } else if (f->frametype == AST_FRAME_NULL) {</span><br><span> /* Ignore NULL frames. It is perfectly normal to get these if the person is muted. */</span><br><span> } else if (f->frametype == AST_FRAME_CONTROL) {</span><br><span>@@ -4764,7 +4239,6 @@</span><br><span> return cnf;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> static struct ast_conference *find_conf(struct ast_channel *chan, char *confno, int make, int dynamic,</span><br><span> char *dynamic_pin, size_t pin_buf_len, int refcount, struct ast_flags64 *confflags)</span><br><span> {</span><br><span>@@ -5939,2085 +5413,6 @@</span><br><span> ast_config_destroy(cfg);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-/*!</span><br><span style="color: hsl(0, 100%, 40%);">- * \internal</span><br><span style="color: hsl(0, 100%, 40%);">- * \brief Find an SLA trunk by name</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static struct sla_trunk *sla_find_trunk(const char *name)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk tmp_trunk = {</span><br><span style="color: hsl(0, 100%, 40%);">- .name = name,</span><br><span style="color: hsl(0, 100%, 40%);">- };</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return ao2_find(sla_trunks, &tmp_trunk, OBJ_POINTER);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*!</span><br><span style="color: hsl(0, 100%, 40%);">- * \internal</span><br><span style="color: hsl(0, 100%, 40%);">- * \brief Find an SLA station by name</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static struct sla_station *sla_find_station(const char *name)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station tmp_station = {</span><br><span style="color: hsl(0, 100%, 40%);">- .name = name,</span><br><span style="color: hsl(0, 100%, 40%);">- };</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return ao2_find(sla_stations, &tmp_station, OBJ_POINTER);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_check_station_hold_access(const struct sla_trunk *trunk,</span><br><span style="color: hsl(0, 100%, 40%);">- const struct sla_station *station)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station_ref *station_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* For each station that has this call on hold, check for private hold. */</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station_ref->station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref->trunk != trunk || station_ref->station == station)</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref->state == SLA_TRUNK_STATE_ONHOLD_BYME &&</span><br><span style="color: hsl(0, 100%, 40%);">- station_ref->station->hold_access == SLA_HOLD_PRIVATE)</span><br><span style="color: hsl(0, 100%, 40%);">- return 1;</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*!</span><br><span style="color: hsl(0, 100%, 40%);">- * \brief Find a trunk reference on a station by name</span><br><span style="color: hsl(0, 100%, 40%);">- * \param station the station</span><br><span style="color: hsl(0, 100%, 40%);">- * \param name the trunk's name</span><br><span style="color: hsl(0, 100%, 40%);">- * \pre sla_station is locked</span><br><span style="color: hsl(0, 100%, 40%);">- * \return a pointer to the station's trunk reference. If the trunk</span><br><span style="color: hsl(0, 100%, 40%);">- * is not found, it is not idle and barge is disabled, or if</span><br><span style="color: hsl(0, 100%, 40%);">- * it is on hold and private hold is set, then NULL will be returned.</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static struct sla_trunk_ref *sla_find_trunk_ref_byname(const struct sla_station *station,</span><br><span style="color: hsl(0, 100%, 40%);">- const char *name)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (strcasecmp(trunk_ref->trunk->name, name))</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if ( (trunk_ref->trunk->barge_disabled</span><br><span style="color: hsl(0, 100%, 40%);">- && trunk_ref->state == SLA_TRUNK_STATE_UP) ||</span><br><span style="color: hsl(0, 100%, 40%);">- (trunk_ref->trunk->hold_stations</span><br><span style="color: hsl(0, 100%, 40%);">- && trunk_ref->trunk->hold_access == SLA_HOLD_PRIVATE</span><br><span style="color: hsl(0, 100%, 40%);">- && trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) ||</span><br><span style="color: hsl(0, 100%, 40%);">- sla_check_station_hold_access(trunk_ref->trunk, station) )</span><br><span style="color: hsl(0, 100%, 40%);">- {</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(trunk_ref, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_station_ref_destructor(void *obj)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station_ref *station_ref = obj;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (station_ref->station) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(station_ref->station, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- station_ref->station = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static struct sla_station_ref *sla_create_station_ref(struct sla_station *station)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station_ref *station_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(station_ref = ao2_alloc(sizeof(*station_ref), sla_station_ref_destructor))) {</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(station, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- station_ref->station = station;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return station_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static struct sla_ringing_station *sla_create_ringing_station(struct sla_station *station)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(ringing_station = ast_calloc(1, sizeof(*ringing_station))))</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(station, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- ringing_station->station = station;</span><br><span style="color: hsl(0, 100%, 40%);">- ringing_station->ring_begin = ast_tvnow();</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return ringing_station;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_ringing_station_destroy(struct sla_ringing_station *ringing_station)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- if (ringing_station->station) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(ringing_station->station, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- ringing_station->station = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(ringing_station);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static struct sla_failed_station *sla_create_failed_station(struct sla_station *station)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_failed_station *failed_station;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(failed_station = ast_calloc(1, sizeof(*failed_station)))) {</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(station, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- failed_station->station = station;</span><br><span style="color: hsl(0, 100%, 40%);">- failed_station->last_try = ast_tvnow();</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return failed_station;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_failed_station_destroy(struct sla_failed_station *failed_station)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- if (failed_station->station) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(failed_station->station, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- failed_station->station = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(failed_station);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static enum ast_device_state sla_state_to_devstate(enum sla_trunk_state state)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- switch (state) {</span><br><span style="color: hsl(0, 100%, 40%);">- case SLA_TRUNK_STATE_IDLE:</span><br><span style="color: hsl(0, 100%, 40%);">- return AST_DEVICE_NOT_INUSE;</span><br><span style="color: hsl(0, 100%, 40%);">- case SLA_TRUNK_STATE_RINGING:</span><br><span style="color: hsl(0, 100%, 40%);">- return AST_DEVICE_RINGING;</span><br><span style="color: hsl(0, 100%, 40%);">- case SLA_TRUNK_STATE_UP:</span><br><span style="color: hsl(0, 100%, 40%);">- return AST_DEVICE_INUSE;</span><br><span style="color: hsl(0, 100%, 40%);">- case SLA_TRUNK_STATE_ONHOLD:</span><br><span style="color: hsl(0, 100%, 40%);">- case SLA_TRUNK_STATE_ONHOLD_BYME:</span><br><span style="color: hsl(0, 100%, 40%);">- return AST_DEVICE_ONHOLD;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return AST_DEVICE_UNKNOWN;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_change_trunk_state(const struct sla_trunk *trunk, enum sla_trunk_state state,</span><br><span style="color: hsl(0, 100%, 40%);">- enum sla_which_trunk_refs inactive_only, const struct sla_trunk_ref *exclude)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ao2_iterator i;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- i = ao2_iterator_init(sla_stations, 0);</span><br><span style="color: hsl(0, 100%, 40%);">- while ((station = ao2_iterator_next(&i))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(station);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref->trunk != trunk || (inactive_only ? trunk_ref->chan : 0)</span><br><span style="color: hsl(0, 100%, 40%);">- || trunk_ref == exclude) {</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->state = state;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_devstate_changed(sla_state_to_devstate(state), AST_DEVSTATE_CACHABLE,</span><br><span style="color: hsl(0, 100%, 40%);">- "SLA:%s_%s", station->name, trunk->name);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(station);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(station, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_iterator_destroy(&i);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-struct run_station_args {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_t *cond_lock;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_t *cond;</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void answer_trunk_chan(struct ast_channel *chan)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- ast_raw_answer(chan);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_indicate(chan, -1);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void *run_station(void *data)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct sla_station *, station, NULL, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct sla_trunk_ref *, trunk_ref, NULL, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_str *conf_name = ast_str_create(16);</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_flags64 conf_flags = { 0 };</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_conference *conf;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- {</span><br><span style="color: hsl(0, 100%, 40%);">- struct run_station_args *args = data;</span><br><span style="color: hsl(0, 100%, 40%);">- station = args->station;</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref = args->trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(args->cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_signal(args->cond);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(args->cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- /* args is no longer valid here. */</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_atomic_fetchadd_int((int *) &trunk_ref->trunk->active_stations, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_str_set(&conf_name, 0, "SLA_%s", trunk_ref->trunk->name);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_set_flag64(&conf_flags,</span><br><span style="color: hsl(0, 100%, 40%);">- CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_PASS_DTMF | CONFFLAG_SLA_STATION);</span><br><span style="color: hsl(0, 100%, 40%);">- answer_trunk_chan(trunk_ref->chan);</span><br><span style="color: hsl(0, 100%, 40%);">- conf = build_conf(ast_str_buffer(conf_name), "", "", 0, 0, 1, trunk_ref->chan, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- if (conf) {</span><br><span style="color: hsl(0, 100%, 40%);">- conf_run(trunk_ref->chan, conf, &conf_flags, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- dispose_conf(conf);</span><br><span style="color: hsl(0, 100%, 40%);">- conf = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->chan = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->active_stations) &&</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_str_append(&conf_name, 0, ",K");</span><br><span style="color: hsl(0, 100%, 40%);">- admin_exec(NULL, ast_str_buffer(conf_name));</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->trunk->hold_stations = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_join(station->dial);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_destroy(station->dial);</span><br><span style="color: hsl(0, 100%, 40%);">- station->dial = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(conf_name);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_ringing_trunk_destroy(struct sla_ringing_trunk *ringing_trunk);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_stop_ringing_trunk(struct sla_ringing_trunk *ringing_trunk)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- char buf[80];</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station_ref *station_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(buf, sizeof(buf), "SLA_%s,K", ringing_trunk->trunk->name);</span><br><span style="color: hsl(0, 100%, 40%);">- admin_exec(NULL, buf);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- while ((station_ref = AST_LIST_REMOVE_HEAD(&ringing_trunk->timed_out_stations, entry))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(station_ref, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- sla_ringing_trunk_destroy(ringing_trunk);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_stop_ringing_station(struct sla_ringing_station *ringing_station,</span><br><span style="color: hsl(0, 100%, 40%);">- enum sla_station_hangup hangup)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station_ref *station_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_join(ringing_station->station->dial);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_destroy(ringing_station->station->dial);</span><br><span style="color: hsl(0, 100%, 40%);">- ringing_station->station->dial = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (hangup == SLA_STATION_HANGUP_NORMAL)</span><br><span style="color: hsl(0, 100%, 40%);">- goto done;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* If the station is being hung up because of a timeout, then add it to the</span><br><span style="color: hsl(0, 100%, 40%);">- * list of timed out stations on each of the ringing trunks. This is so</span><br><span style="color: hsl(0, 100%, 40%);">- * that when doing further processing to figure out which stations should be</span><br><span style="color: hsl(0, 100%, 40%);">- * ringing, which trunk to answer, determining timeouts, etc., we know which</span><br><span style="color: hsl(0, 100%, 40%);">- * ringing trunks we should ignore. */</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ringing_trunk->trunk == trunk_ref->trunk)</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (!trunk_ref)</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(station_ref = sla_create_station_ref(ringing_station->station)))</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_INSERT_TAIL(&ringing_trunk->timed_out_stations, station_ref, entry);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-done:</span><br><span style="color: hsl(0, 100%, 40%);">- sla_ringing_station_destroy(ringing_station);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_dial_state_callback(struct ast_dial *dial)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- sla_queue_event(SLA_EVENT_DIAL_STATE);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Check to see if dialing this station already timed out for this ringing trunk</span><br><span style="color: hsl(0, 100%, 40%);">- * \note Assumes sla.lock is locked</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_check_timed_out_station(const struct sla_ringing_trunk *ringing_trunk,</span><br><span style="color: hsl(0, 100%, 40%);">- const struct sla_station *station)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station_ref *timed_out_station;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&ringing_trunk->timed_out_stations, timed_out_station, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (station == timed_out_station->station)</span><br><span style="color: hsl(0, 100%, 40%);">- return 1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Choose the highest priority ringing trunk for a station</span><br><span style="color: hsl(0, 100%, 40%);">- * \param station the station</span><br><span style="color: hsl(0, 100%, 40%);">- * \param rm remove the ringing trunk once selected</span><br><span style="color: hsl(0, 100%, 40%);">- * \param trunk_ref a place to store the pointer to this stations reference to</span><br><span style="color: hsl(0, 100%, 40%);">- * the selected trunk</span><br><span style="color: hsl(0, 100%, 40%);">- * \return a pointer to the selected ringing trunk, or NULL if none found</span><br><span style="color: hsl(0, 100%, 40%);">- * \note Assumes that sla.lock is locked</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static struct sla_ringing_trunk *sla_choose_ringing_trunk(struct sla_station *station,</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref **trunk_ref, int rm)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *s_trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_trunk *ringing_trunk = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station->trunks, s_trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* Make sure this is the trunk we're looking for */</span><br><span style="color: hsl(0, 100%, 40%);">- if (s_trunk_ref->trunk != ringing_trunk->trunk)</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* This trunk on the station is ringing. But, make sure this station</span><br><span style="color: hsl(0, 100%, 40%);">- * didn't already time out while this trunk was ringing. */</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla_check_timed_out_station(ringing_trunk, station))</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (rm)</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(s_trunk_ref, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- *trunk_ref = s_trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_END;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (ringing_trunk)</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return ringing_trunk;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_handle_dial_state_event(void)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct sla_trunk_ref *, s_trunk_ref, NULL, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_trunk *ringing_trunk = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- struct run_station_args args;</span><br><span style="color: hsl(0, 100%, 40%);">- enum ast_dial_result dial_res;</span><br><span style="color: hsl(0, 100%, 40%);">- pthread_t dont_care;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_t cond_lock;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_t cond;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- switch ((dial_res = ast_dial_state(ringing_station->station->dial))) {</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_HANGUP:</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_INVALID:</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_FAILED:</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_TIMEOUT:</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_UNANSWERED:</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_stop_ringing_station(ringing_station, SLA_STATION_HANGUP_NORMAL);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_ANSWERED:</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(0, 100%, 40%);">- /* Find the appropriate trunk to answer. */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ringing_trunk = sla_choose_ringing_trunk(ringing_station->station, &s_trunk_ref, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ringing_trunk) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* This case happens in a bit of a race condition. If two stations answer</span><br><span style="color: hsl(0, 100%, 40%);">- * the outbound call at the same time, the first one will get connected to</span><br><span style="color: hsl(0, 100%, 40%);">- * the trunk. When the second one gets here, it will not see any trunks</span><br><span style="color: hsl(0, 100%, 40%);">- * ringing so we have no idea what to conect it to. So, we just hang up</span><br><span style="color: hsl(0, 100%, 40%);">- * on it. */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(1, "Found no ringing trunk for station '%s' to answer!\n", ringing_station->station->name);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_join(ringing_station->station->dial);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_destroy(ringing_station->station->dial);</span><br><span style="color: hsl(0, 100%, 40%);">- ringing_station->station->dial = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- sla_ringing_station_destroy(ringing_station);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- /* Track the channel that answered this trunk */</span><br><span style="color: hsl(0, 100%, 40%);">- s_trunk_ref->chan = ast_dial_answered(ringing_station->station->dial);</span><br><span style="color: hsl(0, 100%, 40%);">- /* Actually answer the trunk */</span><br><span style="color: hsl(0, 100%, 40%);">- answer_trunk_chan(ringing_trunk->trunk->chan);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- /* Now, start a thread that will connect this station to the trunk. The rest of</span><br><span style="color: hsl(0, 100%, 40%);">- * the code here sets up the thread and ensures that it is able to save the arguments</span><br><span style="color: hsl(0, 100%, 40%);">- * before they are no longer valid since they are allocated on the stack. */</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(s_trunk_ref, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- args.trunk_ref = s_trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(ringing_station->station, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- args.station = ringing_station->station;</span><br><span style="color: hsl(0, 100%, 40%);">- args.cond = &cond;</span><br><span style="color: hsl(0, 100%, 40%);">- args.cond_lock = &cond_lock;</span><br><span style="color: hsl(0, 100%, 40%);">- sla_ringing_trunk_destroy(ringing_trunk);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_ringing_station_destroy(ringing_station);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_init(&cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_init(&cond, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(&cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_pthread_create_detached_background(&dont_care, NULL, run_station, &args);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_wait(&cond, &cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(&cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_destroy(&cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_destroy(&cond);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_TRYING:</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_RINGING:</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_PROGRESS:</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_PROCEEDING:</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (dial_res == AST_DIAL_RESULT_ANSWERED) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* Queue up reprocessing ringing trunks, and then ringing stations again */</span><br><span style="color: hsl(0, 100%, 40%);">- sla_queue_event(SLA_EVENT_RINGING_TRUNK);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_queue_event(SLA_EVENT_DIAL_STATE);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_END;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Check to see if this station is already ringing</span><br><span style="color: hsl(0, 100%, 40%);">- * \note Assumes sla.lock is locked</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_check_ringing_station(const struct sla_station *station)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&sla.ringing_stations, ringing_station, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (station == ringing_station->station)</span><br><span style="color: hsl(0, 100%, 40%);">- return 1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Check to see if this station has failed to be dialed in the past minute</span><br><span style="color: hsl(0, 100%, 40%);">- * \note assumes sla.lock is locked</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_check_failed_station(const struct sla_station *station)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_failed_station *failed_station;</span><br><span style="color: hsl(0, 100%, 40%);">- int res = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.failed_stations, failed_station, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (station != failed_station->station)</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_tvdiff_ms(ast_tvnow(), failed_station->last_try) > 1000) {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_failed_station_destroy(failed_station);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- res = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_END</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return res;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Ring a station</span><br><span style="color: hsl(0, 100%, 40%);">- * \note Assumes sla.lock is locked</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_ring_station(struct sla_ringing_trunk *ringing_trunk, struct sla_station *station)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- char *tech, *tech_data;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_dial *dial;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(0, 100%, 40%);">- enum ast_dial_result res;</span><br><span style="color: hsl(0, 100%, 40%);">- int caller_is_saved;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_party_caller caller;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(dial = ast_dial_create()))</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_set_state_callback(dial, sla_dial_state_callback);</span><br><span style="color: hsl(0, 100%, 40%);">- tech_data = ast_strdupa(station->device);</span><br><span style="color: hsl(0, 100%, 40%);">- tech = strsep(&tech_data, "/");</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_dial_append(dial, tech, tech_data, NULL) == -1) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_destroy(dial);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Do we need to save off the caller ID data? */</span><br><span style="color: hsl(0, 100%, 40%);">- caller_is_saved = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- if (!sla.attempt_callerid) {</span><br><span style="color: hsl(0, 100%, 40%);">- caller_is_saved = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- caller = *ast_channel_caller(ringing_trunk->trunk->chan);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_party_caller_init(ast_channel_caller(ringing_trunk->trunk->chan));</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- res = ast_dial_run(dial, ringing_trunk->trunk->chan, 1);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Restore saved caller ID */</span><br><span style="color: hsl(0, 100%, 40%);">- if (caller_is_saved) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_party_caller_free(ast_channel_caller(ringing_trunk->trunk->chan));</span><br><span style="color: hsl(0, 100%, 40%);">- ast_channel_caller_set(ringing_trunk->trunk->chan, &caller);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (res != AST_DIAL_RESULT_TRYING) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_failed_station *failed_station;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_destroy(dial);</span><br><span style="color: hsl(0, 100%, 40%);">- if ((failed_station = sla_create_failed_station(station))) {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_INSERT_HEAD(&sla.failed_stations, failed_station, entry);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(ringing_station = sla_create_ringing_station(station))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_join(dial);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_destroy(dial);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- station->dial = dial;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_INSERT_HEAD(&sla.ringing_stations, ringing_station, entry);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Check to see if a station is in use</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_check_inuse_station(const struct sla_station *station)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref->chan)</span><br><span style="color: hsl(0, 100%, 40%);">- return 1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static struct sla_trunk_ref *sla_find_trunk_ref(const struct sla_station *station,</span><br><span style="color: hsl(0, 100%, 40%);">- const struct sla_trunk *trunk)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref->trunk == trunk)</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(trunk_ref, 1);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Calculate the ring delay for a given ringing trunk on a station</span><br><span style="color: hsl(0, 100%, 40%);">- * \param station the station</span><br><span style="color: hsl(0, 100%, 40%);">- * \param ringing_trunk the trunk. If NULL, the highest priority ringing trunk will be used</span><br><span style="color: hsl(0, 100%, 40%);">- * \return the number of ms left before the delay is complete, or INT_MAX if there is no delay</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_check_station_delay(struct sla_station *station,</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_trunk *ringing_trunk)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct sla_trunk_ref *, trunk_ref, NULL, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int delay = UINT_MAX;</span><br><span style="color: hsl(0, 100%, 40%);">- int time_left, time_elapsed;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ringing_trunk)</span><br><span style="color: hsl(0, 100%, 40%);">- ringing_trunk = sla_choose_ringing_trunk(station, &trunk_ref, 0);</span><br><span style="color: hsl(0, 100%, 40%);">- else</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref = sla_find_trunk_ref(station, ringing_trunk->trunk);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ringing_trunk || !trunk_ref)</span><br><span style="color: hsl(0, 100%, 40%);">- return delay;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* If this station has a ring delay specific to the highest priority</span><br><span style="color: hsl(0, 100%, 40%);">- * ringing trunk, use that. Otherwise, use the ring delay specified</span><br><span style="color: hsl(0, 100%, 40%);">- * globally for the station. */</span><br><span style="color: hsl(0, 100%, 40%);">- delay = trunk_ref->ring_delay;</span><br><span style="color: hsl(0, 100%, 40%);">- if (!delay)</span><br><span style="color: hsl(0, 100%, 40%);">- delay = station->ring_delay;</span><br><span style="color: hsl(0, 100%, 40%);">- if (!delay)</span><br><span style="color: hsl(0, 100%, 40%);">- return INT_MAX;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin);</span><br><span style="color: hsl(0, 100%, 40%);">- time_left = (delay * 1000) - time_elapsed;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return time_left;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Ring stations based on current set of ringing trunks</span><br><span style="color: hsl(0, 100%, 40%);">- * \note Assumes that sla.lock is locked</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_ring_stations(void)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station_ref *station_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Make sure that every station that uses at least one of the ringing</span><br><span style="color: hsl(0, 100%, 40%);">- * trunks, is ringing. */</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&ringing_trunk->trunk->stations, station_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- int time_left;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Is this station already ringing? */</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla_check_ringing_station(station_ref->station))</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Is this station already in a call? */</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla_check_inuse_station(station_ref->station))</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Did we fail to dial this station earlier? If so, has it been</span><br><span style="color: hsl(0, 100%, 40%);">- * a minute since we tried? */</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla_check_failed_station(station_ref->station))</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* If this station already timed out while this trunk was ringing,</span><br><span style="color: hsl(0, 100%, 40%);">- * do not dial it again for this ringing trunk. */</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla_check_timed_out_station(ringing_trunk, station_ref->station))</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Check for a ring delay in progress */</span><br><span style="color: hsl(0, 100%, 40%);">- time_left = sla_check_station_delay(station_ref->station, ringing_trunk);</span><br><span style="color: hsl(0, 100%, 40%);">- if (time_left != INT_MAX && time_left > 0)</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* It is time to make this station begin to ring. Do it! */</span><br><span style="color: hsl(0, 100%, 40%);">- sla_ring_station(ringing_trunk, station_ref->station);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- /* Now, all of the stations that should be ringing, are ringing. */</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_hangup_stations(void)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref->trunk == ringing_trunk->trunk)</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- if (ringing_trunk)</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (!trunk_ref) {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_join(ringing_station->station->dial);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_destroy(ringing_station->station->dial);</span><br><span style="color: hsl(0, 100%, 40%);">- ringing_station->station->dial = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- sla_ringing_station_destroy(ringing_station);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_END</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_handle_ringing_trunk_event(void)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_ring_stations();</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Find stations that shouldn't be ringing anymore. */</span><br><span style="color: hsl(0, 100%, 40%);">- sla_hangup_stations();</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_handle_hold_event(struct sla_event *event)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- ast_atomic_fetchadd_int((int *) &event->trunk_ref->trunk->hold_stations, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- event->trunk_ref->state = SLA_TRUNK_STATE_ONHOLD_BYME;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_devstate_changed(AST_DEVICE_ONHOLD, AST_DEVSTATE_CACHABLE, "SLA:%s_%s",</span><br><span style="color: hsl(0, 100%, 40%);">- event->station->name, event->trunk_ref->trunk->name);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_change_trunk_state(event->trunk_ref->trunk, SLA_TRUNK_STATE_ONHOLD,</span><br><span style="color: hsl(0, 100%, 40%);">- INACTIVE_TRUNK_REFS, event->trunk_ref);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (event->trunk_ref->trunk->active_stations == 1) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* The station putting it on hold is the only one on the call, so start</span><br><span style="color: hsl(0, 100%, 40%);">- * Music on hold to the trunk. */</span><br><span style="color: hsl(0, 100%, 40%);">- event->trunk_ref->trunk->on_hold = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_indicate(event->trunk_ref->trunk->chan, AST_CONTROL_HOLD);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_softhangup(event->trunk_ref->chan, AST_SOFTHANGUP_DEV);</span><br><span style="color: hsl(0, 100%, 40%);">- event->trunk_ref->chan = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Process trunk ring timeouts</span><br><span style="color: hsl(0, 100%, 40%);">- * \note Called with sla.lock locked</span><br><span style="color: hsl(0, 100%, 40%);">- * \return non-zero if a change to the ringing trunks was made</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_calc_trunk_timeouts(unsigned int *timeout)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(0, 100%, 40%);">- int res = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- int time_left, time_elapsed;</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ringing_trunk->trunk->ring_timeout)</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin);</span><br><span style="color: hsl(0, 100%, 40%);">- time_left = (ringing_trunk->trunk->ring_timeout * 1000) - time_elapsed;</span><br><span style="color: hsl(0, 100%, 40%);">- if (time_left <= 0) {</span><br><span style="color: hsl(0, 100%, 40%);">- pbx_builtin_setvar_helper(ringing_trunk->trunk->chan, "SLATRUNK_STATUS", "RINGTIMEOUT");</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_stop_ringing_trunk(ringing_trunk);</span><br><span style="color: hsl(0, 100%, 40%);">- res = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (time_left < *timeout)</span><br><span style="color: hsl(0, 100%, 40%);">- *timeout = time_left;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_END;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return res;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Process station ring timeouts</span><br><span style="color: hsl(0, 100%, 40%);">- * \note Called with sla.lock locked</span><br><span style="color: hsl(0, 100%, 40%);">- * \return non-zero if a change to the ringing stations was made</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_calc_station_timeouts(unsigned int *timeout)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(0, 100%, 40%);">- int res = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int ring_timeout = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- int time_elapsed, time_left = INT_MAX, final_trunk_time_left = INT_MIN;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* If there are any ring timeouts specified for a specific trunk</span><br><span style="color: hsl(0, 100%, 40%);">- * on the station, then use the highest per-trunk ring timeout.</span><br><span style="color: hsl(0, 100%, 40%);">- * Otherwise, use the ring timeout set for the entire station. */</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station_ref *station_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- int trunk_time_elapsed, trunk_time_left;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ringing_trunk->trunk == trunk_ref->trunk)</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ringing_trunk)</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* If there is a trunk that is ringing without a timeout, then the</span><br><span style="color: hsl(0, 100%, 40%);">- * only timeout that could matter is a global station ring timeout. */</span><br><span style="color: hsl(0, 100%, 40%);">- if (!trunk_ref->ring_timeout)</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* This trunk on this station is ringing and has a timeout.</span><br><span style="color: hsl(0, 100%, 40%);">- * However, make sure this trunk isn't still ringing from a</span><br><span style="color: hsl(0, 100%, 40%);">- * previous timeout. If so, don't consider it. */</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&ringing_trunk->timed_out_stations, station_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (station_ref->station == ringing_station->station)</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (station_ref)</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin);</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_time_left = (trunk_ref->ring_timeout * 1000) - trunk_time_elapsed;</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_time_left > final_trunk_time_left)</span><br><span style="color: hsl(0, 100%, 40%);">- final_trunk_time_left = trunk_time_left;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* No timeout was found for ringing trunks, and no timeout for the entire station */</span><br><span style="color: hsl(0, 100%, 40%);">- if (final_trunk_time_left == INT_MIN && !ringing_station->station->ring_timeout)</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Compute how much time is left for a global station timeout */</span><br><span style="color: hsl(0, 100%, 40%);">- if (ringing_station->station->ring_timeout) {</span><br><span style="color: hsl(0, 100%, 40%);">- ring_timeout = ringing_station->station->ring_timeout;</span><br><span style="color: hsl(0, 100%, 40%);">- time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_station->ring_begin);</span><br><span style="color: hsl(0, 100%, 40%);">- time_left = (ring_timeout * 1000) - time_elapsed;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* If the time left based on the per-trunk timeouts is smaller than the</span><br><span style="color: hsl(0, 100%, 40%);">- * global station ring timeout, use that. */</span><br><span style="color: hsl(0, 100%, 40%);">- if (final_trunk_time_left > INT_MIN && final_trunk_time_left < time_left)</span><br><span style="color: hsl(0, 100%, 40%);">- time_left = final_trunk_time_left;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* If there is no time left, the station needs to stop ringing */</span><br><span style="color: hsl(0, 100%, 40%);">- if (time_left <= 0) {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_stop_ringing_station(ringing_station, SLA_STATION_HANGUP_TIMEOUT);</span><br><span style="color: hsl(0, 100%, 40%);">- res = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* There is still some time left for this station to ring, so save that</span><br><span style="color: hsl(0, 100%, 40%);">- * timeout if it is the first event scheduled to occur */</span><br><span style="color: hsl(0, 100%, 40%);">- if (time_left < *timeout)</span><br><span style="color: hsl(0, 100%, 40%);">- *timeout = time_left;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_END;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return res;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Calculate the ring delay for a station</span><br><span style="color: hsl(0, 100%, 40%);">- * \note Assumes sla.lock is locked</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_calc_station_delays(unsigned int *timeout)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station;</span><br><span style="color: hsl(0, 100%, 40%);">- int res = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ao2_iterator i;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- i = ao2_iterator_init(sla_stations, 0);</span><br><span style="color: hsl(0, 100%, 40%);">- for (; (station = ao2_iterator_next(&i)); ao2_ref(station, -1)) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(0, 100%, 40%);">- int time_left;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Ignore stations already ringing */</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla_check_ringing_station(station))</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Ignore stations already on a call */</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla_check_inuse_station(station))</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Ignore stations that don't have one of their trunks ringing */</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(ringing_trunk = sla_choose_ringing_trunk(station, NULL, 0)))</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if ((time_left = sla_check_station_delay(station, ringing_trunk)) == INT_MAX)</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* If there is no time left, then the station needs to start ringing.</span><br><span style="color: hsl(0, 100%, 40%);">- * Return non-zero so that an event will be queued up an event to</span><br><span style="color: hsl(0, 100%, 40%);">- * make that happen. */</span><br><span style="color: hsl(0, 100%, 40%);">- if (time_left <= 0) {</span><br><span style="color: hsl(0, 100%, 40%);">- res = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (time_left < *timeout)</span><br><span style="color: hsl(0, 100%, 40%);">- *timeout = time_left;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_iterator_destroy(&i);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return res;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*! \brief Calculate the time until the next known event</span><br><span style="color: hsl(0, 100%, 40%);">- * \note Called with sla.lock locked */</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_process_timers(struct timespec *ts)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int timeout = UINT_MAX;</span><br><span style="color: hsl(0, 100%, 40%);">- struct timeval wait;</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int change_made = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Check for ring timeouts on ringing trunks */</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla_calc_trunk_timeouts(&timeout))</span><br><span style="color: hsl(0, 100%, 40%);">- change_made = 1;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Check for ring timeouts on ringing stations */</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla_calc_station_timeouts(&timeout))</span><br><span style="color: hsl(0, 100%, 40%);">- change_made = 1;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Check for station ring delays */</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla_calc_station_delays(&timeout))</span><br><span style="color: hsl(0, 100%, 40%);">- change_made = 1;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* queue reprocessing of ringing trunks */</span><br><span style="color: hsl(0, 100%, 40%);">- if (change_made)</span><br><span style="color: hsl(0, 100%, 40%);">- sla_queue_event_nolock(SLA_EVENT_RINGING_TRUNK);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* No timeout */</span><br><span style="color: hsl(0, 100%, 40%);">- if (timeout == UINT_MAX)</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (ts) {</span><br><span style="color: hsl(0, 100%, 40%);">- wait = ast_tvadd(ast_tvnow(), ast_samp2tv(timeout, 1000));</span><br><span style="color: hsl(0, 100%, 40%);">- ts->tv_sec = wait.tv_sec;</span><br><span style="color: hsl(0, 100%, 40%);">- ts->tv_nsec = wait.tv_usec * 1000;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 1;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_event_destroy(struct sla_event *event)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- if (event->trunk_ref) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(event->trunk_ref, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- event->trunk_ref = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (event->station) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(event->station, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- event->station = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(event);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void *sla_thread(void *data)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_failed_station *failed_station;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- while (!sla.stop) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_event *event;</span><br><span style="color: hsl(0, 100%, 40%);">- struct timespec ts = { 0, };</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int have_timeout = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (AST_LIST_EMPTY(&sla.event_q)) {</span><br><span style="color: hsl(0, 100%, 40%);">- if ((have_timeout = sla_process_timers(&ts)))</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_timedwait(&sla.cond, &sla.lock, &ts);</span><br><span style="color: hsl(0, 100%, 40%);">- else</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_wait(&sla.cond, &sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla.stop)</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (have_timeout)</span><br><span style="color: hsl(0, 100%, 40%);">- sla_process_timers(NULL);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- while ((event = AST_LIST_REMOVE_HEAD(&sla.event_q, entry))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- switch (event->type) {</span><br><span style="color: hsl(0, 100%, 40%);">- case SLA_EVENT_HOLD:</span><br><span style="color: hsl(0, 100%, 40%);">- sla_handle_hold_event(event);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- case SLA_EVENT_DIAL_STATE:</span><br><span style="color: hsl(0, 100%, 40%);">- sla_handle_dial_state_event();</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- case SLA_EVENT_RINGING_TRUNK:</span><br><span style="color: hsl(0, 100%, 40%);">- sla_handle_ringing_trunk_event();</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- sla_event_destroy(event);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- while ((ringing_station = AST_LIST_REMOVE_HEAD(&sla.ringing_stations, entry))) {</span><br><span style="color: hsl(0, 100%, 40%);">- sla_ringing_station_destroy(ringing_station);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- while ((failed_station = AST_LIST_REMOVE_HEAD(&sla.failed_stations, entry))) {</span><br><span style="color: hsl(0, 100%, 40%);">- sla_failed_station_destroy(failed_station);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-struct dial_trunk_args {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_t *cond_lock;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_t *cond;</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void *dial_trunk(void *data)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct dial_trunk_args *args = data;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_dial *dial;</span><br><span style="color: hsl(0, 100%, 40%);">- char *tech, *tech_data;</span><br><span style="color: hsl(0, 100%, 40%);">- enum ast_dial_result dial_res;</span><br><span style="color: hsl(0, 100%, 40%);">- char conf_name[MAX_CONFNUM];</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_conference *conf;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_flags64 conf_flags = { 0 };</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct sla_trunk_ref *, trunk_ref, args->trunk_ref, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct sla_station *, station, args->station, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- int caller_is_saved;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_party_caller caller;</span><br><span style="color: hsl(0, 100%, 40%);">- int last_state = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- int current_state = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(dial = ast_dial_create())) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(args->cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_signal(args->cond);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(args->cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- tech_data = ast_strdupa(trunk_ref->trunk->device);</span><br><span style="color: hsl(0, 100%, 40%);">- tech = strsep(&tech_data, "/");</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_dial_append(dial, tech, tech_data, NULL) == -1) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(args->cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_signal(args->cond);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(args->cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_destroy(dial);</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Do we need to save of the caller ID data? */</span><br><span style="color: hsl(0, 100%, 40%);">- caller_is_saved = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- if (!sla.attempt_callerid) {</span><br><span style="color: hsl(0, 100%, 40%);">- caller_is_saved = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- caller = *ast_channel_caller(trunk_ref->chan);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_party_caller_init(ast_channel_caller(trunk_ref->chan));</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- dial_res = ast_dial_run(dial, trunk_ref->chan, 1);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Restore saved caller ID */</span><br><span style="color: hsl(0, 100%, 40%);">- if (caller_is_saved) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_party_caller_free(ast_channel_caller(trunk_ref->chan));</span><br><span style="color: hsl(0, 100%, 40%);">- ast_channel_caller_set(trunk_ref->chan, &caller);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (dial_res != AST_DIAL_RESULT_TRYING) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(args->cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_signal(args->cond);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(args->cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_destroy(dial);</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Wait for dial to end, while servicing the channel */</span><br><span style="color: hsl(0, 100%, 40%);">- while (ast_waitfor(trunk_ref->chan, 100)) {</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int done = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_frame *fr = ast_read(trunk_ref->chan);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!fr) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(1, "Channel %s did not return a frame, must have hung up\n", ast_channel_name(trunk_ref->chan));</span><br><span style="color: hsl(0, 100%, 40%);">- done = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_frfree(fr); /* Ignore while dialing */</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- switch ((dial_res = ast_dial_state(dial))) {</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_ANSWERED:</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->trunk->chan = ast_dial_answered(dial);</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_HANGUP:</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_INVALID:</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_FAILED:</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_TIMEOUT:</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_UNANSWERED:</span><br><span style="color: hsl(0, 100%, 40%);">- done = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_TRYING:</span><br><span style="color: hsl(0, 100%, 40%);">- current_state = AST_CONTROL_PROGRESS;</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_RINGING:</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_PROGRESS:</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_DIAL_RESULT_PROCEEDING:</span><br><span style="color: hsl(0, 100%, 40%);">- current_state = AST_CONTROL_RINGING;</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (done)</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* check that SLA station that originated trunk call is still alive */</span><br><span style="color: hsl(0, 100%, 40%);">- if (station && ast_device_state(station->device) == AST_DEVICE_NOT_INUSE) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(3, "Originating station device %s no longer active\n", station->device);</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->trunk->chan = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* If trunk line state changed, send indication back to originating SLA Station channel */</span><br><span style="color: hsl(0, 100%, 40%);">- if (current_state != last_state) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(3, "Indicating State Change %d to channel %s\n", current_state, ast_channel_name(trunk_ref->chan));</span><br><span style="color: hsl(0, 100%, 40%);">- ast_indicate(trunk_ref->chan, current_state);</span><br><span style="color: hsl(0, 100%, 40%);">- last_state = current_state;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!trunk_ref->trunk->chan) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(args->cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_signal(args->cond);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(args->cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_join(dial);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_destroy(dial);</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(conf_name, sizeof(conf_name), "SLA_%s", trunk_ref->trunk->name);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_set_flag64(&conf_flags,</span><br><span style="color: hsl(0, 100%, 40%);">- CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_MARKEDUSER |</span><br><span style="color: hsl(0, 100%, 40%);">- CONFFLAG_PASS_DTMF | CONFFLAG_SLA_TRUNK);</span><br><span style="color: hsl(0, 100%, 40%);">- conf = build_conf(conf_name, "", "", 1, 1, 1, trunk_ref->trunk->chan, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(args->cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_signal(args->cond);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(args->cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (conf) {</span><br><span style="color: hsl(0, 100%, 40%);">- conf_run(trunk_ref->trunk->chan, conf, &conf_flags, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- dispose_conf(conf);</span><br><span style="color: hsl(0, 100%, 40%);">- conf = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* If the trunk is going away, it is definitely now IDLE. */</span><br><span style="color: hsl(0, 100%, 40%);">- sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->trunk->chan = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->trunk->on_hold = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_join(dial);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dial_destroy(dial);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*!</span><br><span style="color: hsl(0, 100%, 40%);">- * \brief For a given station, choose the highest priority idle trunk</span><br><span style="color: hsl(0, 100%, 40%);">- * \pre sla_station is locked</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static struct sla_trunk_ref *sla_choose_idle_trunk(const struct sla_station *station)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref->state == SLA_TRUNK_STATE_IDLE) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(trunk_ref, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_station_exec(struct ast_channel *chan, const char *data)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- char *station_name, *trunk_name;</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct sla_station *, station, NULL, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct sla_trunk_ref *, trunk_ref, NULL, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- char conf_name[MAX_CONFNUM];</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_flags64 conf_flags = { 0 };</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_conference *conf;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_strlen_zero(data)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Invalid Arguments to SLAStation!\n");</span><br><span style="color: hsl(0, 100%, 40%);">- pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE");</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_name = ast_strdupa(data);</span><br><span style="color: hsl(0, 100%, 40%);">- station_name = strsep(&trunk_name, "_");</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_strlen_zero(station_name)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Invalid Arguments to SLAStation!\n");</span><br><span style="color: hsl(0, 100%, 40%);">- pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE");</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- station = sla_find_station(station_name);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!station) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Station '%s' not found!\n", station_name);</span><br><span style="color: hsl(0, 100%, 40%);">- pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE");</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(station);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ast_strlen_zero(trunk_name)) {</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref = sla_find_trunk_ref_byname(station, trunk_name);</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref = sla_choose_idle_trunk(station);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(station);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!trunk_ref) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_strlen_zero(trunk_name))</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_NOTICE, "No trunks available for call.\n");</span><br><span style="color: hsl(0, 100%, 40%);">- else {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_NOTICE, "Can't join existing call on trunk "</span><br><span style="color: hsl(0, 100%, 40%);">- "'%s' due to access controls.\n", trunk_name);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "CONGESTION");</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref->state == SLA_TRUNK_STATE_ONHOLD_BYME) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->hold_stations) == 1)</span><br><span style="color: hsl(0, 100%, 40%);">- sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- else {</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->state = SLA_TRUNK_STATE_UP;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_devstate_changed(AST_DEVICE_INUSE, AST_DEVSTATE_CACHABLE,</span><br><span style="color: hsl(0, 100%, 40%);">- "SLA:%s_%s", station->name, trunk_ref->trunk->name);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (trunk_ref->state == SLA_TRUNK_STATE_RINGING) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ringing_trunk->trunk == trunk_ref->trunk) {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_END</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (ringing_trunk) {</span><br><span style="color: hsl(0, 100%, 40%);">- answer_trunk_chan(ringing_trunk->trunk->chan);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- sla_ringing_trunk_destroy(ringing_trunk);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Queue up reprocessing ringing trunks, and then ringing stations again */</span><br><span style="color: hsl(0, 100%, 40%);">- sla_queue_event(SLA_EVENT_RINGING_TRUNK);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_queue_event(SLA_EVENT_DIAL_STATE);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->chan = chan;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!trunk_ref->trunk->chan) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_t cond_lock;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_t cond;</span><br><span style="color: hsl(0, 100%, 40%);">- pthread_t dont_care;</span><br><span style="color: hsl(0, 100%, 40%);">- struct dial_trunk_args args = {</span><br><span style="color: hsl(0, 100%, 40%);">- .trunk_ref = trunk_ref,</span><br><span style="color: hsl(0, 100%, 40%);">- .station = station,</span><br><span style="color: hsl(0, 100%, 40%);">- .cond_lock = &cond_lock,</span><br><span style="color: hsl(0, 100%, 40%);">- .cond = &cond,</span><br><span style="color: hsl(0, 100%, 40%);">- };</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(trunk_ref, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(station, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- /* Create a thread to dial the trunk and dump it into the conference.</span><br><span style="color: hsl(0, 100%, 40%);">- * However, we want to wait until the trunk has been dialed and the</span><br><span style="color: hsl(0, 100%, 40%);">- * conference is created before continuing on here.</span><br><span style="color: hsl(0, 100%, 40%);">- * Don't autoservice the channel or we'll have multiple threads</span><br><span style="color: hsl(0, 100%, 40%);">- * handling it. dial_trunk services the channel.</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_init(&cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_init(&cond, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(&cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_pthread_create_detached_background(&dont_care, NULL, dial_trunk, &args);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_wait(&cond, &cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(&cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_destroy(&cond_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_destroy(&cond);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!trunk_ref->trunk->chan) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(1, "Trunk didn't get created. chan: %lx\n", (unsigned long) trunk_ref->trunk->chan);</span><br><span style="color: hsl(0, 100%, 40%);">- pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "CONGESTION");</span><br><span style="color: hsl(0, 100%, 40%);">- sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->chan = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_atomic_fetchadd_int((int *) &trunk_ref->trunk->active_stations, 1) == 0 &&</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->trunk->on_hold) {</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->trunk->on_hold = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_indicate(trunk_ref->trunk->chan, AST_CONTROL_UNHOLD);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(conf_name, sizeof(conf_name), "SLA_%s", trunk_ref->trunk->name);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_set_flag64(&conf_flags,</span><br><span style="color: hsl(0, 100%, 40%);">- CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_PASS_DTMF | CONFFLAG_SLA_STATION);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_answer(chan);</span><br><span style="color: hsl(0, 100%, 40%);">- conf = build_conf(conf_name, "", "", 0, 0, 1, chan, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- if (conf) {</span><br><span style="color: hsl(0, 100%, 40%);">- conf_run(chan, conf, &conf_flags, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- dispose_conf(conf);</span><br><span style="color: hsl(0, 100%, 40%);">- conf = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->chan = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->active_stations) &&</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) {</span><br><span style="color: hsl(0, 100%, 40%);">- strncat(conf_name, ",K", sizeof(conf_name) - strlen(conf_name) - 1);</span><br><span style="color: hsl(0, 100%, 40%);">- admin_exec(NULL, conf_name);</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->trunk->hold_stations = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "SUCCESS");</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_trunk_ref_destructor(void *obj)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref = obj;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref->trunk) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(trunk_ref->trunk, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->trunk = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static struct sla_trunk_ref *create_trunk_ref(struct sla_trunk *trunk)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(trunk_ref = ao2_alloc(sizeof(*trunk_ref), sla_trunk_ref_destructor))) {</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(trunk, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->trunk = trunk;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static struct sla_ringing_trunk *queue_ringing_trunk(struct sla_trunk *trunk)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(ringing_trunk = ast_calloc(1, sizeof(*ringing_trunk)))) {</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(trunk, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- ringing_trunk->trunk = trunk;</span><br><span style="color: hsl(0, 100%, 40%);">- ringing_trunk->ring_begin = ast_tvnow();</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- sla_change_trunk_state(trunk, SLA_TRUNK_STATE_RINGING, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_INSERT_HEAD(&sla.ringing_trunks, ringing_trunk, entry);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- sla_queue_event(SLA_EVENT_RINGING_TRUNK);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return ringing_trunk;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_ringing_trunk_destroy(struct sla_ringing_trunk *ringing_trunk)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- if (ringing_trunk->trunk) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(ringing_trunk->trunk, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- ringing_trunk->trunk = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(ringing_trunk);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-enum {</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_TRUNK_OPT_MOH = (1 << 0),</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-enum {</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_TRUNK_OPT_ARG_MOH_CLASS = 0,</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_TRUNK_OPT_ARG_ARRAY_SIZE = 1,</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-AST_APP_OPTIONS(sla_trunk_opts, BEGIN_OPTIONS</span><br><span style="color: hsl(0, 100%, 40%);">- AST_APP_OPTION_ARG('M', SLA_TRUNK_OPT_MOH, SLA_TRUNK_OPT_ARG_MOH_CLASS),</span><br><span style="color: hsl(0, 100%, 40%);">-END_OPTIONS );</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_trunk_exec(struct ast_channel *chan, const char *data)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- char conf_name[MAX_CONFNUM];</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_conference *conf;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_flags64 conf_flags = { 0 };</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct sla_trunk *, trunk, NULL, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(0, 100%, 40%);">- AST_DECLARE_APP_ARGS(args,</span><br><span style="color: hsl(0, 100%, 40%);">- AST_APP_ARG(trunk_name);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_APP_ARG(options);</span><br><span style="color: hsl(0, 100%, 40%);">- );</span><br><span style="color: hsl(0, 100%, 40%);">- char *opts[SLA_TRUNK_OPT_ARG_ARRAY_SIZE] = { NULL, };</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_flags opt_flags = { 0 };</span><br><span style="color: hsl(0, 100%, 40%);">- char *parse;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_strlen_zero(data)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "The SLATrunk application requires an argument, the trunk name\n");</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- parse = ast_strdupa(data);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_STANDARD_APP_ARGS(args, parse);</span><br><span style="color: hsl(0, 100%, 40%);">- if (args.argc == 2) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_app_parse_options(sla_trunk_opts, &opt_flags, opts, args.options)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Error parsing options for SLATrunk\n");</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- trunk = sla_find_trunk(args.trunk_name);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!trunk) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "SLA Trunk '%s' not found!\n", args.trunk_name);</span><br><span style="color: hsl(0, 100%, 40%);">- pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk->chan) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Call came in on %s, but the trunk is already in use!\n",</span><br><span style="color: hsl(0, 100%, 40%);">- args.trunk_name);</span><br><span style="color: hsl(0, 100%, 40%);">- pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- trunk->chan = chan;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(ringing_trunk = queue_ringing_trunk(trunk))) {</span><br><span style="color: hsl(0, 100%, 40%);">- pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(conf_name, sizeof(conf_name), "SLA_%s", args.trunk_name);</span><br><span style="color: hsl(0, 100%, 40%);">- conf = build_conf(conf_name, "", "", 1, 1, 1, chan, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!conf) {</span><br><span style="color: hsl(0, 100%, 40%);">- pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_set_flag64(&conf_flags,</span><br><span style="color: hsl(0, 100%, 40%);">- CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_MARKEDUSER | CONFFLAG_PASS_DTMF | CONFFLAG_NO_AUDIO_UNTIL_UP);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_test_flag(&opt_flags, SLA_TRUNK_OPT_MOH)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_indicate(chan, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_set_flag64(&conf_flags, CONFFLAG_MOH);</span><br><span style="color: hsl(0, 100%, 40%);">- } else</span><br><span style="color: hsl(0, 100%, 40%);">- ast_indicate(chan, AST_CONTROL_RINGING);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- conf_run(chan, conf, &conf_flags, opts);</span><br><span style="color: hsl(0, 100%, 40%);">- dispose_conf(conf);</span><br><span style="color: hsl(0, 100%, 40%);">- conf = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- trunk->chan = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- trunk->on_hold = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- sla_change_trunk_state(trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!pbx_builtin_getvar_helper(chan, "SLATRUNK_STATUS"))</span><br><span style="color: hsl(0, 100%, 40%);">- pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "SUCCESS");</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Remove the entry from the list of ringing trunks if it is still there. */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ringing_trunk->trunk == trunk) {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_END;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- if (ringing_trunk) {</span><br><span style="color: hsl(0, 100%, 40%);">- sla_ringing_trunk_destroy(ringing_trunk);</span><br><span style="color: hsl(0, 100%, 40%);">- pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "UNANSWERED");</span><br><span style="color: hsl(0, 100%, 40%);">- /* Queue reprocessing of ringing trunks to make stations stop ringing</span><br><span style="color: hsl(0, 100%, 40%);">- * that shouldn't be ringing after this trunk stopped. */</span><br><span style="color: hsl(0, 100%, 40%);">- sla_queue_event(SLA_EVENT_RINGING_TRUNK);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static enum ast_device_state sla_state(const char *data)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- char *buf, *station_name, *trunk_name;</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct sla_station *, station, NULL, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- enum ast_device_state res = AST_DEVICE_INVALID;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_name = buf = ast_strdupa(data);</span><br><span style="color: hsl(0, 100%, 40%);">- station_name = strsep(&trunk_name, "_");</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- station = sla_find_station(station_name);</span><br><span style="color: hsl(0, 100%, 40%);">- if (station) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(station);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!strcasecmp(trunk_name, trunk_ref->trunk->name)) {</span><br><span style="color: hsl(0, 100%, 40%);">- res = sla_state_to_devstate(trunk_ref->state);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(station);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (res == AST_DEVICE_INVALID) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Could not determine state for trunk %s on station %s!\n",</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_name, station_name);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return res;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_trunk_release_refs(void *obj, void *arg, int flags)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk *trunk = obj;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station_ref *station_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- while ((station_ref = AST_LIST_REMOVE_HEAD(&trunk->stations, entry))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(station_ref, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_station_release_refs(void *obj, void *arg, int flags)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station = obj;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- while ((trunk_ref = AST_LIST_REMOVE_HEAD(&station->trunks, entry))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(trunk_ref, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_station_destructor(void *obj)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station = obj;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(1, "sla_station destructor for '%s'\n", station->name);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ast_strlen_zero(station->autocontext)) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- char exten[AST_MAX_EXTENSION];</span><br><span style="color: hsl(0, 100%, 40%);">- char hint[AST_MAX_APP];</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(exten, sizeof(exten), "%s_%s", station->name, trunk_ref->trunk->name);</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(hint, sizeof(hint), "SLA:%s", exten);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_context_remove_extension(station->autocontext, exten,</span><br><span style="color: hsl(0, 100%, 40%);">- 1, sla_registrar);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_context_remove_extension(station->autocontext, hint,</span><br><span style="color: hsl(0, 100%, 40%);">- PRIORITY_HINT, sla_registrar);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- sla_station_release_refs(station, NULL, 0);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_string_field_free_memory(station);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_trunk_cmp(void *obj, void *arg, int flags)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk *trunk = obj, *trunk2 = arg;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return !strcasecmp(trunk->name, trunk2->name) ? CMP_MATCH | CMP_STOP : 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_station_cmp(void *obj, void *arg, int flags)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station = obj, *station2 = arg;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return !strcasecmp(station->name, station2->name) ? CMP_MATCH | CMP_STOP : 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_destroy(void)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla.thread != AST_PTHREADT_NULL) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- sla.stop = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_signal(&sla.cond);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- pthread_join(sla.thread, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Drop any created contexts from the dialplan */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_context_destroy(NULL, sla_registrar);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_destroy(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_destroy(&sla.cond);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_callback(sla_trunks, 0, sla_trunk_release_refs, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_callback(sla_stations, 0, sla_station_release_refs, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(sla_trunks, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_trunks = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(sla_stations, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_stations = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_check_device(const char *device)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- char *tech, *tech_data;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- tech_data = ast_strdupa(device);</span><br><span style="color: hsl(0, 100%, 40%);">- tech = strsep(&tech_data, "/");</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_strlen_zero(tech) || ast_strlen_zero(tech_data))</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_trunk_destructor(void *obj)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk *trunk = obj;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(1, "sla_trunk destructor for '%s'\n", trunk->name);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ast_strlen_zero(trunk->autocontext)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_context_remove_extension(trunk->autocontext, "s", 1, sla_registrar);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- sla_trunk_release_refs(trunk, NULL, 0);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_string_field_free_memory(trunk);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_build_trunk(struct ast_config *cfg, const char *cat)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct sla_trunk *, trunk, NULL, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_variable *var;</span><br><span style="color: hsl(0, 100%, 40%);">- const char *dev;</span><br><span style="color: hsl(0, 100%, 40%);">- int existing_trunk = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(dev = ast_variable_retrieve(cfg, cat, "device"))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "SLA Trunk '%s' defined with no device!\n", cat);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla_check_device(dev)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "SLA Trunk '%s' defined with invalid device '%s'!\n",</span><br><span style="color: hsl(0, 100%, 40%);">- cat, dev);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if ((trunk = sla_find_trunk(cat))) {</span><br><span style="color: hsl(0, 100%, 40%);">- trunk->mark = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- existing_trunk = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- } else if ((trunk = ao2_alloc(sizeof(*trunk), sla_trunk_destructor))) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_string_field_init(trunk, 32)) {</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_string_field_set(trunk, name, cat);</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(trunk);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_string_field_set(trunk, device, dev);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- for (var = ast_variable_browse(cfg, cat); var; var = var->next) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!strcasecmp(var->name, "autocontext"))</span><br><span style="color: hsl(0, 100%, 40%);">- ast_string_field_set(trunk, autocontext, var->value);</span><br><span style="color: hsl(0, 100%, 40%);">- else if (!strcasecmp(var->name, "ringtimeout")) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (sscanf(var->value, "%30u", &trunk->ring_timeout) != 1) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Invalid ringtimeout '%s' specified for trunk '%s'\n",</span><br><span style="color: hsl(0, 100%, 40%);">- var->value, trunk->name);</span><br><span style="color: hsl(0, 100%, 40%);">- trunk->ring_timeout = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (!strcasecmp(var->name, "barge"))</span><br><span style="color: hsl(0, 100%, 40%);">- trunk->barge_disabled = ast_false(var->value);</span><br><span style="color: hsl(0, 100%, 40%);">- else if (!strcasecmp(var->name, "hold")) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!strcasecmp(var->value, "private"))</span><br><span style="color: hsl(0, 100%, 40%);">- trunk->hold_access = SLA_HOLD_PRIVATE;</span><br><span style="color: hsl(0, 100%, 40%);">- else if (!strcasecmp(var->value, "open"))</span><br><span style="color: hsl(0, 100%, 40%);">- trunk->hold_access = SLA_HOLD_OPEN;</span><br><span style="color: hsl(0, 100%, 40%);">- else {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Invalid value '%s' for hold on trunk %s\n",</span><br><span style="color: hsl(0, 100%, 40%);">- var->value, trunk->name);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (strcasecmp(var->name, "type") && strcasecmp(var->name, "device")) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Invalid option '%s' specified at line %d of %s!\n",</span><br><span style="color: hsl(0, 100%, 40%);">- var->name, var->lineno, SLA_CONFIG_FILE);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(trunk);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ast_strlen_zero(trunk->autocontext)) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ast_context_find_or_create(NULL, NULL, trunk->autocontext, sla_registrar)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Failed to automatically find or create "</span><br><span style="color: hsl(0, 100%, 40%);">- "context '%s' for SLA!\n", trunk->autocontext);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_add_extension(trunk->autocontext, 0 /* don't replace */, "s", 1,</span><br><span style="color: hsl(0, 100%, 40%);">- NULL, NULL, slatrunk_app, ast_strdup(trunk->name), ast_free_ptr, sla_registrar)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Failed to automatically create extension "</span><br><span style="color: hsl(0, 100%, 40%);">- "for trunk '%s'!\n", trunk->name);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!existing_trunk) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_link(sla_trunks, trunk);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-/*!</span><br><span style="color: hsl(0, 100%, 40%);">- * \internal</span><br><span style="color: hsl(0, 100%, 40%);">- * \pre station is not locked</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static void sla_add_trunk_to_station(struct sla_station *station, struct ast_variable *var)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct sla_trunk *, trunk, NULL, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station_ref *station_ref;</span><br><span style="color: hsl(0, 100%, 40%);">- char *trunk_name, *options, *cur;</span><br><span style="color: hsl(0, 100%, 40%);">- int existing_trunk_ref = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- int existing_station_ref = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- options = ast_strdupa(var->value);</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_name = strsep(&options, ",");</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- trunk = sla_find_trunk(trunk_name);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!trunk) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Trunk '%s' not found!\n", var->value);</span><br><span style="color: hsl(0, 100%, 40%);">- return;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk_ref->trunk == trunk) {</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->mark = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- existing_trunk_ref = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!trunk_ref && !(trunk_ref = create_trunk_ref(trunk))) {</span><br><span style="color: hsl(0, 100%, 40%);">- return;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->state = SLA_TRUNK_STATE_IDLE;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- while ((cur = strsep(&options, ","))) {</span><br><span style="color: hsl(0, 100%, 40%);">- char *name, *value = cur;</span><br><span style="color: hsl(0, 100%, 40%);">- name = strsep(&value, "=");</span><br><span style="color: hsl(0, 100%, 40%);">- if (!strcasecmp(name, "ringtimeout")) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (sscanf(value, "%30u", &trunk_ref->ring_timeout) != 1) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Invalid ringtimeout value '%s' for "</span><br><span style="color: hsl(0, 100%, 40%);">- "trunk '%s' on station '%s'\n", value, trunk->name, station->name);</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->ring_timeout = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (!strcasecmp(name, "ringdelay")) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (sscanf(value, "%30u", &trunk_ref->ring_delay) != 1) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Invalid ringdelay value '%s' for "</span><br><span style="color: hsl(0, 100%, 40%);">- "trunk '%s' on station '%s'\n", value, trunk->name, station->name);</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->ring_delay = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Invalid option '%s' for "</span><br><span style="color: hsl(0, 100%, 40%);">- "trunk '%s' on station '%s'\n", name, trunk->name, station->name);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (station_ref->station == station) {</span><br><span style="color: hsl(0, 100%, 40%);">- station_ref->mark = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- existing_station_ref = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!station_ref && !(station_ref = sla_create_station_ref(station))) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!existing_trunk_ref) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(trunk_ref, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->mark = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- return;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!existing_station_ref) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(trunk);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_INSERT_TAIL(&trunk->stations, station_ref, entry);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_atomic_fetchadd_int((int *) &trunk->num_stations, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(trunk);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!existing_trunk_ref) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(station);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_INSERT_TAIL(&station->trunks, trunk_ref, entry);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(station);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_build_station(struct ast_config *cfg, const char *cat)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- RAII_VAR(struct sla_station *, station, NULL, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_variable *var;</span><br><span style="color: hsl(0, 100%, 40%);">- const char *dev;</span><br><span style="color: hsl(0, 100%, 40%);">- int existing_station = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(dev = ast_variable_retrieve(cfg, cat, "device"))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "SLA Station '%s' defined with no device!\n", cat);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if ((station = sla_find_station(cat))) {</span><br><span style="color: hsl(0, 100%, 40%);">- station->mark = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- existing_station = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- } else if ((station = ao2_alloc(sizeof(*station), sla_station_destructor))) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_string_field_init(station, 32)) {</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_string_field_set(station, name, cat);</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(station);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_string_field_set(station, device, dev);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- for (var = ast_variable_browse(cfg, cat); var; var = var->next) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!strcasecmp(var->name, "trunk")) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(station);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_add_trunk_to_station(station, var);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(station);</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (!strcasecmp(var->name, "autocontext")) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_string_field_set(station, autocontext, var->value);</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (!strcasecmp(var->name, "ringtimeout")) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (sscanf(var->value, "%30u", &station->ring_timeout) != 1) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Invalid ringtimeout '%s' specified for station '%s'\n",</span><br><span style="color: hsl(0, 100%, 40%);">- var->value, station->name);</span><br><span style="color: hsl(0, 100%, 40%);">- station->ring_timeout = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (!strcasecmp(var->name, "ringdelay")) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (sscanf(var->value, "%30u", &station->ring_delay) != 1) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Invalid ringdelay '%s' specified for station '%s'\n",</span><br><span style="color: hsl(0, 100%, 40%);">- var->value, station->name);</span><br><span style="color: hsl(0, 100%, 40%);">- station->ring_delay = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (!strcasecmp(var->name, "hold")) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!strcasecmp(var->value, "private"))</span><br><span style="color: hsl(0, 100%, 40%);">- station->hold_access = SLA_HOLD_PRIVATE;</span><br><span style="color: hsl(0, 100%, 40%);">- else if (!strcasecmp(var->value, "open"))</span><br><span style="color: hsl(0, 100%, 40%);">- station->hold_access = SLA_HOLD_OPEN;</span><br><span style="color: hsl(0, 100%, 40%);">- else {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Invalid value '%s' for hold on station %s\n",</span><br><span style="color: hsl(0, 100%, 40%);">- var->value, station->name);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (strcasecmp(var->name, "type") && strcasecmp(var->name, "device")) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Invalid option '%s' specified at line %d of %s!\n",</span><br><span style="color: hsl(0, 100%, 40%);">- var->name, var->lineno, SLA_CONFIG_FILE);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(station);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ast_strlen_zero(station->autocontext)) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ast_context_find_or_create(NULL, NULL, station->autocontext, sla_registrar)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Failed to automatically find or create "</span><br><span style="color: hsl(0, 100%, 40%);">- "context '%s' for SLA!\n", station->autocontext);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- /* The extension for when the handset goes off-hook.</span><br><span style="color: hsl(0, 100%, 40%);">- * exten => station1,1,SLAStation(station1) */</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_add_extension(station->autocontext, 0 /* don't replace */, station->name, 1,</span><br><span style="color: hsl(0, 100%, 40%);">- NULL, NULL, slastation_app, ast_strdup(station->name), ast_free_ptr, sla_registrar)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Failed to automatically create extension "</span><br><span style="color: hsl(0, 100%, 40%);">- "for trunk '%s'!\n", station->name);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- char exten[AST_MAX_EXTENSION];</span><br><span style="color: hsl(0, 100%, 40%);">- char hint[AST_MAX_APP];</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(exten, sizeof(exten), "%s_%s", station->name, trunk_ref->trunk->name);</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(hint, sizeof(hint), "SLA:%s", exten);</span><br><span style="color: hsl(0, 100%, 40%);">- /* Extension for this line button</span><br><span style="color: hsl(0, 100%, 40%);">- * exten => station1_line1,1,SLAStation(station1_line1) */</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_add_extension(station->autocontext, 0 /* don't replace */, exten, 1,</span><br><span style="color: hsl(0, 100%, 40%);">- NULL, NULL, slastation_app, ast_strdup(exten), ast_free_ptr, sla_registrar)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Failed to automatically create extension "</span><br><span style="color: hsl(0, 100%, 40%);">- "for trunk '%s'!\n", station->name);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- /* Hint for this line button</span><br><span style="color: hsl(0, 100%, 40%);">- * exten => station1_line1,hint,SLA:station1_line1 */</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_add_extension(station->autocontext, 0 /* don't replace */, exten, PRIORITY_HINT,</span><br><span style="color: hsl(0, 100%, 40%);">- NULL, NULL, hint, NULL, NULL, sla_registrar)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Failed to automatically create hint "</span><br><span style="color: hsl(0, 100%, 40%);">- "for trunk '%s'!\n", station->name);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!existing_station) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_link(sla_stations, station);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_trunk_mark(void *obj, void *arg, int flags)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk *trunk = obj;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station_ref *station_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(trunk);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- trunk->mark = 1;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- station_ref->mark = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(trunk);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_station_mark(void *obj, void *arg, int flags)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station = obj;</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(station);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- station->mark = 1;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- trunk_ref->mark = 1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(station);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_trunk_is_marked(void *obj, void *arg, int flags)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk *trunk = obj;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(trunk);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (trunk->mark) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* Only remove all of the station references if the trunk itself is going away */</span><br><span style="color: hsl(0, 100%, 40%);">- sla_trunk_release_refs(trunk, NULL, 0);</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station_ref *station_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Otherwise only remove references to stations no longer in the config */</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_BEGIN(&trunk->stations, station_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!station_ref->mark) {</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(station_ref, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_END</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(trunk);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return trunk->mark ? CMP_MATCH : 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_station_is_marked(void *obj, void *arg, int flags)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_station *station = obj;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_lock(station);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (station->mark) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* Only remove all of the trunk references if the station itself is going away */</span><br><span style="color: hsl(0, 100%, 40%);">- sla_station_release_refs(station, NULL, 0);</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Otherwise only remove references to trunks no longer in the config */</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_BEGIN(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!trunk_ref->mark) {</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(trunk_ref, -1);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_END</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlock(station);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return station->mark ? CMP_MATCH : 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_in_use(void)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- return ao2_container_count(sla_trunks) || ao2_container_count(sla_stations);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int sla_load_config(int reload)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_config *cfg;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };</span><br><span style="color: hsl(0, 100%, 40%);">- const char *cat = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- int res = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- const char *val;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!reload) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_init(&sla.lock);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cond_init(&sla.cond, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_trunks = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0,</span><br><span style="color: hsl(0, 100%, 40%);">- NULL, sla_trunk_cmp);</span><br><span style="color: hsl(0, 100%, 40%);">- sla_stations = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0,</span><br><span style="color: hsl(0, 100%, 40%);">- NULL, sla_station_cmp);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(cfg = ast_config_load(SLA_CONFIG_FILE, config_flags))) {</span><br><span style="color: hsl(0, 100%, 40%);">- return 0; /* Treat no config as normal */</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (cfg == CONFIG_STATUS_FILEINVALID) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Config file " SLA_CONFIG_FILE " is in an invalid format. Aborting.\n");</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (reload) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_callback(sla_trunks, 0, sla_trunk_mark, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_callback(sla_stations, 0, sla_station_mark, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if ((val = ast_variable_retrieve(cfg, "general", "attemptcallerid")))</span><br><span style="color: hsl(0, 100%, 40%);">- sla.attempt_callerid = ast_true(val);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- while ((cat = ast_category_browse(cfg, cat)) && !res) {</span><br><span style="color: hsl(0, 100%, 40%);">- const char *type;</span><br><span style="color: hsl(0, 100%, 40%);">- if (!strcasecmp(cat, "general"))</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(type = ast_variable_retrieve(cfg, cat, "type"))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Invalid entry in %s defined with no type!\n",</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_CONFIG_FILE);</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- if (!strcasecmp(type, "trunk"))</span><br><span style="color: hsl(0, 100%, 40%);">- res = sla_build_trunk(cfg, cat);</span><br><span style="color: hsl(0, 100%, 40%);">- else if (!strcasecmp(type, "station"))</span><br><span style="color: hsl(0, 100%, 40%);">- res = sla_build_station(cfg, cat);</span><br><span style="color: hsl(0, 100%, 40%);">- else {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Entry in %s defined with invalid type '%s'!\n",</span><br><span style="color: hsl(0, 100%, 40%);">- SLA_CONFIG_FILE, type);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_config_destroy(cfg);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (reload) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_callback(sla_trunks, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, sla_trunk_is_marked, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_callback(sla_stations, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, sla_station_is_marked, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Start SLA event processing thread once SLA has been configured. */</span><br><span style="color: hsl(0, 100%, 40%);">- if (sla.thread == AST_PTHREADT_NULL && sla_in_use()) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_pthread_create(&sla.thread, NULL, sla_thread, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return res;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> static int acf_meetme_info_eval(const char *keyword, const struct ast_conference *conf)</span><br><span> {</span><br><span> if (!strcasecmp("lock", keyword)) {</span><br><span>@@ -8086,17 +5481,15 @@</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> static struct ast_custom_function meetme_info_acf = {</span><br><span> .name = "MEETME_INFO",</span><br><span> .read = acf_meetme_info,</span><br><span> };</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> static int load_config(int reload)</span><br><span> {</span><br><span> load_config_meetme(reload);</span><br><span style="color: hsl(0, 100%, 40%);">- return sla_load_config(reload);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span> }</span><br><span> </span><br><span> static int unload_module(void)</span><br><span>@@ -8112,13 +5505,8 @@</span><br><span> res |= ast_unregister_application(app3);</span><br><span> res |= ast_unregister_application(app2);</span><br><span> res |= ast_unregister_application(app);</span><br><span style="color: hsl(0, 100%, 40%);">- res |= ast_unregister_application(slastation_app);</span><br><span style="color: hsl(0, 100%, 40%);">- res |= ast_unregister_application(slatrunk_app);</span><br><span> </span><br><span> ast_devstate_prov_del("Meetme");</span><br><span style="color: hsl(0, 100%, 40%);">- ast_devstate_prov_del("SLA");</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- sla_destroy();</span><br><span> </span><br><span> res |= ast_custom_function_unregister(&meetme_info_acf);</span><br><span> ast_unload_realtime("meetme");</span><br><span>@@ -8155,11 +5543,8 @@</span><br><span> res |= ast_register_application_xml(app3, admin_exec);</span><br><span> res |= ast_register_application_xml(app2, count_exec);</span><br><span> res |= ast_register_application_xml(app, conf_exec);</span><br><span style="color: hsl(0, 100%, 40%);">- res |= ast_register_application_xml(slastation_app, sla_station_exec);</span><br><span style="color: hsl(0, 100%, 40%);">- res |= ast_register_application_xml(slatrunk_app, sla_trunk_exec);</span><br><span> </span><br><span> res |= ast_devstate_prov_add("Meetme", meetmestate);</span><br><span style="color: hsl(0, 100%, 40%);">- res |= ast_devstate_prov_add("SLA", sla_state);</span><br><span> </span><br><span> res |= ast_custom_function_register(&meetme_info_acf);</span><br><span> ast_realtime_require_field("meetme", "confno", RQ_UINTEGER2, 3, "members", RQ_UINTEGER1, 3, NULL);</span><br><span>diff --git a/apps/app_sla.c b/apps/app_sla.c</span><br><span>new file mode 100644</span><br><span>index 0000000..b9a4757</span><br><span>--- /dev/null</span><br><span>+++ b/apps/app_sla.c</span><br><span>@@ -0,0 +1,2875 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * Asterisk -- An open source telephony toolkit.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Copyright (C) 2022, Naveen Albert</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Based on previous MeetMe-based SLA Implementation by:</span><br><span style="color: hsl(120, 100%, 40%);">+ * Russell Bryant <russell@digium.com></span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * See http://www.asterisk.org for more information about</span><br><span style="color: hsl(120, 100%, 40%);">+ * the Asterisk project. Please do not directly contact</span><br><span style="color: hsl(120, 100%, 40%);">+ * any of the maintainers of this project for assistance;</span><br><span style="color: hsl(120, 100%, 40%);">+ * the project provides a web site, mailing lists and IRC</span><br><span style="color: hsl(120, 100%, 40%);">+ * channels for your use.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software, distributed under the terms of</span><br><span style="color: hsl(120, 100%, 40%);">+ * the GNU General Public License Version 2. See the LICENSE file</span><br><span style="color: hsl(120, 100%, 40%);">+ * at the top of the source tree.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \file</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Shared Line Appearances</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \author Naveen Albert <asterisk@phreaknet.org></span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \ingroup applications</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \li \ref app_sla.c uses configuration file \ref sla.conf</span><br><span style="color: hsl(120, 100%, 40%);">+ * \addtogroup configuration_file Configuration Files</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \page sla.conf sla.conf</span><br><span style="color: hsl(120, 100%, 40%);">+ * \verbinclude sla.conf.sample</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*** MODULEINFO</span><br><span style="color: hsl(120, 100%, 40%);">+ <depend>app_confbridge</depend></span><br><span style="color: hsl(120, 100%, 40%);">+ <support_level>extended</support_level></span><br><span style="color: hsl(120, 100%, 40%);">+ ***/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/lock.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/file.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/channel.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/pbx.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/module.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/config.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/app.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/cli.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/utils.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/astobj2.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/devicestate.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/dial.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/causes.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/format_compatibility.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*** DOCUMENTATION</span><br><span style="color: hsl(120, 100%, 40%);">+ <application name="SLAStation" language="en_US"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ Shared Line Appearance Station.</span><br><span style="color: hsl(120, 100%, 40%);">+ </synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ <syntax></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="station" required="true"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Station name</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </parameter></span><br><span style="color: hsl(120, 100%, 40%);">+ </syntax></span><br><span style="color: hsl(120, 100%, 40%);">+ <description></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>This application should be executed by an SLA station. The argument depends</span><br><span style="color: hsl(120, 100%, 40%);">+ on how the call was initiated. If the phone was just taken off hook, then the argument</span><br><span style="color: hsl(120, 100%, 40%);">+ <replaceable>station</replaceable> should be just the station name. If the call was</span><br><span style="color: hsl(120, 100%, 40%);">+ initiated by pressing a line key, then the station name should be preceded by an underscore</span><br><span style="color: hsl(120, 100%, 40%);">+ and the trunk name associated with that line button.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>For example: <literal>station1_line1</literal></para></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>On exit, this application will set the variable <variable>SLASTATION_STATUS</variable> to</span><br><span style="color: hsl(120, 100%, 40%);">+ one of the following values:</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <variablelist></span><br><span style="color: hsl(120, 100%, 40%);">+ <variable name="SLASTATION_STATUS"></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="FAILURE" /></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="CONGESTION" /></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="SUCCESS" /></span><br><span style="color: hsl(120, 100%, 40%);">+ </variable></span><br><span style="color: hsl(120, 100%, 40%);">+ </variablelist></span><br><span style="color: hsl(120, 100%, 40%);">+ </description></span><br><span style="color: hsl(120, 100%, 40%);">+ </application></span><br><span style="color: hsl(120, 100%, 40%);">+ <application name="SLATrunk" language="en_US"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ Shared Line Appearance Trunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ </synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ <syntax></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="trunk" required="true"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Trunk name</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </parameter></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="options"></span><br><span style="color: hsl(120, 100%, 40%);">+ <optionlist></span><br><span style="color: hsl(120, 100%, 40%);">+ <option name="M" hasparams="optional"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Play back the specified MOH <replaceable>class</replaceable></span><br><span style="color: hsl(120, 100%, 40%);">+ instead of ringing</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <argument name="class" required="true" /></span><br><span style="color: hsl(120, 100%, 40%);">+ </option></span><br><span style="color: hsl(120, 100%, 40%);">+ </optionlist></span><br><span style="color: hsl(120, 100%, 40%);">+ </parameter></span><br><span style="color: hsl(120, 100%, 40%);">+ </syntax></span><br><span style="color: hsl(120, 100%, 40%);">+ <description></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>This application should be executed by an SLA trunk on an inbound call. The channel calling</span><br><span style="color: hsl(120, 100%, 40%);">+ this application should correspond to the SLA trunk with the name <replaceable>trunk</replaceable></span><br><span style="color: hsl(120, 100%, 40%);">+ that is being passed as an argument.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>On exit, this application will set the variable <variable>SLATRUNK_STATUS</variable> to</span><br><span style="color: hsl(120, 100%, 40%);">+ one of the following values:</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <variablelist></span><br><span style="color: hsl(120, 100%, 40%);">+ <variable name="SLATRUNK_STATUS"></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="FAILURE" /></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="SUCCESS" /></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="UNANSWERED" /></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="RINGTIMEOUT" /></span><br><span style="color: hsl(120, 100%, 40%);">+ </variable></span><br><span style="color: hsl(120, 100%, 40%);">+ </variablelist></span><br><span style="color: hsl(120, 100%, 40%);">+ </description></span><br><span style="color: hsl(120, 100%, 40%);">+ </application></span><br><span style="color: hsl(120, 100%, 40%);">+ ***/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define SLA_CONFIG_FILE "sla.conf"</span><br><span style="color: hsl(120, 100%, 40%);">+#define MAX_CONFNUM 80</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static const char * const slastation_app = "SLAStation";</span><br><span style="color: hsl(120, 100%, 40%);">+static const char * const slatrunk_app = "SLATrunk";</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum {</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! If set there will be no enter or leave sounds */</span><br><span style="color: hsl(120, 100%, 40%);">+ CONFFLAG_QUIET = (1 << 0),</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Set to have music on hold when user is alone in conference */</span><br><span style="color: hsl(120, 100%, 40%);">+ CONFFLAG_MOH = (1 << 1),</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! If set, the channel will leave the conference if all marked users leave */</span><br><span style="color: hsl(120, 100%, 40%);">+ CONFFLAG_MARKEDEXIT = (1 << 2),</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! If set, the user will be marked */</span><br><span style="color: hsl(120, 100%, 40%);">+ CONFFLAG_MARKEDUSER = (1 << 3),</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Pass DTMF through the conference */</span><br><span style="color: hsl(120, 100%, 40%);">+ CONFFLAG_PASS_DTMF = (1 << 4),</span><br><span style="color: hsl(120, 100%, 40%);">+ CONFFLAG_SLA_STATION = (1 << 5),</span><br><span style="color: hsl(120, 100%, 40%);">+ CONFFLAG_SLA_TRUNK = (1 << 6),</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum {</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_TRUNK_OPT_MOH = (1 << 0),</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum {</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_TRUNK_OPT_ARG_MOH_CLASS = 0,</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_TRUNK_OPT_ARG_ARRAY_SIZE = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+AST_APP_OPTIONS(sla_trunk_opts, BEGIN_OPTIONS</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_OPTION_ARG('M', SLA_TRUNK_OPT_MOH, SLA_TRUNK_OPT_ARG_MOH_CLASS),</span><br><span style="color: hsl(120, 100%, 40%);">+END_OPTIONS );</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum sla_which_trunk_refs {</span><br><span style="color: hsl(120, 100%, 40%);">+ ALL_TRUNK_REFS,</span><br><span style="color: hsl(120, 100%, 40%);">+ INACTIVE_TRUNK_REFS,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum sla_trunk_state {</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_TRUNK_STATE_IDLE,</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_TRUNK_STATE_RINGING,</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_TRUNK_STATE_UP,</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_TRUNK_STATE_ONHOLD,</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_TRUNK_STATE_ONHOLD_BYME,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum sla_hold_access {</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! This means that any station can put it on hold, and any station</span><br><span style="color: hsl(120, 100%, 40%);">+ * can retrieve the call from hold. */</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_HOLD_OPEN,</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! This means that only the station that put the call on hold may</span><br><span style="color: hsl(120, 100%, 40%);">+ * retrieve it from hold. */</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_HOLD_PRIVATE,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct sla_trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct sla_station {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_RWLIST_ENTRY(sla_station) entry;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_DECLARE_STRING_FIELDS(</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STRING_FIELD(name);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STRING_FIELD(device);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STRING_FIELD(autocontext);</span><br><span style="color: hsl(120, 100%, 40%);">+ );</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_HEAD_NOLOCK(, sla_trunk_ref) trunks;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_dial *dial;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Ring timeout for this station, for any trunk. If a ring timeout</span><br><span style="color: hsl(120, 100%, 40%);">+ * is set for a specific trunk on this station, that will take</span><br><span style="color: hsl(120, 100%, 40%);">+ * priority over this value. */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int ring_timeout;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Ring delay for this station, for any trunk. If a ring delay</span><br><span style="color: hsl(120, 100%, 40%);">+ * is set for a specific trunk on this station, that will take</span><br><span style="color: hsl(120, 100%, 40%);">+ * priority over this value. */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int ring_delay;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! This option uses the values in the sla_hold_access enum and sets the</span><br><span style="color: hsl(120, 100%, 40%);">+ * access control type for hold on this station. */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int hold_access:1;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Mark used during reload processing */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int mark:1;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief A reference to a station</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This struct looks near useless at first glance. However, its existence</span><br><span style="color: hsl(120, 100%, 40%);">+ * in the list of stations in sla_trunk means that this station references</span><br><span style="color: hsl(120, 100%, 40%);">+ * that trunk. We use the mark to keep track of whether it needs to be</span><br><span style="color: hsl(120, 100%, 40%);">+ * removed from the sla_trunk's list of stations during a reload.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+struct sla_station_ref {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_ENTRY(sla_station_ref) entry;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Mark used during reload processing */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int mark:1;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct sla_trunk {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_DECLARE_STRING_FIELDS(</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STRING_FIELD(name);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STRING_FIELD(device);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STRING_FIELD(autocontext);</span><br><span style="color: hsl(120, 100%, 40%);">+ );</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_HEAD_NOLOCK(, sla_station_ref) stations;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Number of stations that use this trunk */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int num_stations;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Number of stations currently on a call with this trunk */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int active_stations;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Number of stations that have this trunk on hold. */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int hold_stations;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_channel *chan;</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int ring_timeout;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! If set to 1, no station will be able to join an active call with</span><br><span style="color: hsl(120, 100%, 40%);">+ * this trunk. */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int barge_disabled:1;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! This option uses the values in the sla_hold_access enum and sets the</span><br><span style="color: hsl(120, 100%, 40%);">+ * access control type for hold on this trunk. */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int hold_access:1;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Whether this trunk is currently on hold, meaning that once a station</span><br><span style="color: hsl(120, 100%, 40%);">+ * connects to it, the trunk channel needs to have UNHOLD indicated to it. */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int on_hold:1;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Mark used during reload processing */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int mark:1;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief A station's reference to a trunk</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * An sla_station keeps a list of trunk_refs. This holds metadata about the</span><br><span style="color: hsl(120, 100%, 40%);">+ * stations usage of the trunk.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+struct sla_trunk_ref {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_ENTRY(sla_trunk_ref) entry;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk *trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+ enum sla_trunk_state state;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_channel *chan;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Ring timeout to use when this trunk is ringing on this specific</span><br><span style="color: hsl(120, 100%, 40%);">+ * station. This takes higher priority than a ring timeout set at</span><br><span style="color: hsl(120, 100%, 40%);">+ * the station level. */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int ring_timeout;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Ring delay to use when this trunk is ringing on this specific</span><br><span style="color: hsl(120, 100%, 40%);">+ * station. This takes higher priority than a ring delay set at</span><br><span style="color: hsl(120, 100%, 40%);">+ * the station level. */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int ring_delay;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Mark used during reload processing */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int mark:1;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct ao2_container *sla_stations;</span><br><span style="color: hsl(120, 100%, 40%);">+static struct ao2_container *sla_trunks;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static const char sla_registrar[] = "SLA";</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Event types that can be queued up for the SLA thread */</span><br><span style="color: hsl(120, 100%, 40%);">+enum sla_event_type {</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! A station has put the call on hold */</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_EVENT_HOLD,</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! The state of a dial has changed */</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_EVENT_DIAL_STATE,</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! The state of a ringing trunk has changed */</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_EVENT_RINGING_TRUNK,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct sla_event {</span><br><span style="color: hsl(120, 100%, 40%);">+ enum sla_event_type type;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_ENTRY(sla_event) entry;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief A station that failed to be dialed</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note Only used by the SLA thread. */</span><br><span style="color: hsl(120, 100%, 40%);">+struct sla_failed_station {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct timeval last_try;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_ENTRY(sla_failed_station) entry;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief A trunk that is ringing */</span><br><span style="color: hsl(120, 100%, 40%);">+struct sla_ringing_trunk {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk *trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! The time that this trunk started ringing */</span><br><span style="color: hsl(120, 100%, 40%);">+ struct timeval ring_begin;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_HEAD_NOLOCK(, sla_station_ref) timed_out_stations;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_ENTRY(sla_ringing_trunk) entry;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum sla_station_hangup {</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_STATION_HANGUP_NORMAL,</span><br><span style="color: hsl(120, 100%, 40%);">+ SLA_STATION_HANGUP_TIMEOUT,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief A station that is ringing */</span><br><span style="color: hsl(120, 100%, 40%);">+struct sla_ringing_station {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! The time that this station started ringing */</span><br><span style="color: hsl(120, 100%, 40%);">+ struct timeval ring_begin;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_ENTRY(sla_ringing_station) entry;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief A structure for data used by the sla thread</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static struct {</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! The SLA thread ID */</span><br><span style="color: hsl(120, 100%, 40%);">+ pthread_t thread;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_t cond;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_t lock;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_HEAD_NOLOCK(, sla_ringing_trunk) ringing_trunks;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_HEAD_NOLOCK(, sla_ringing_station) ringing_stations;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_HEAD_NOLOCK(, sla_failed_station) failed_stations;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_HEAD_NOLOCK(, sla_event) event_q;</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int stop:1;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Attempt to handle CallerID, even though it is known not to work</span><br><span style="color: hsl(120, 100%, 40%);">+ * properly in some situations. */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int attempt_callerid:1;</span><br><span style="color: hsl(120, 100%, 40%);">+} sla = {</span><br><span style="color: hsl(120, 100%, 40%);">+ .thread = AST_PTHREADT_NULL,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static const char *sla_hold_str(unsigned int hold_access)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *hold = "Unknown";</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (hold_access) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case SLA_HOLD_OPEN:</span><br><span style="color: hsl(120, 100%, 40%);">+ hold = "Open";</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case SLA_HOLD_PRIVATE:</span><br><span style="color: hsl(120, 100%, 40%);">+ hold = "Private";</span><br><span style="color: hsl(120, 100%, 40%);">+ default:</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return hold;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static char *sla_show_trunks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_iterator i;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk *trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (cmd) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case CLI_INIT:</span><br><span style="color: hsl(120, 100%, 40%);">+ e->command = "sla show trunks";</span><br><span style="color: hsl(120, 100%, 40%);">+ e->usage =</span><br><span style="color: hsl(120, 100%, 40%);">+ "Usage: sla show trunks\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ " This will list all trunks defined in sla.conf\n";</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ case CLI_GENERATE:</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=============================================================\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== Configured SLA Trunks ===================================\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=============================================================\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "===\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ i = ao2_iterator_init(sla_trunks, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ for (; (trunk = ao2_iterator_next(&i)); ao2_ref(trunk, -1)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station_ref *station_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ char ring_timeout[23] = "(none)";</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk->ring_timeout) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(ring_timeout, sizeof(ring_timeout), "%u Seconds", trunk->ring_timeout);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "=== ---------------------------------------------------------\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== Trunk Name: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> Device: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> AutoContext: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> RingTimeout: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> BargeAllowed: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> HoldAccess: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> Stations ...\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk->name, trunk->device,</span><br><span style="color: hsl(120, 100%, 40%);">+ S_OR(trunk->autocontext, "(none)"),</span><br><span style="color: hsl(120, 100%, 40%);">+ ring_timeout,</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk->barge_disabled ? "No" : "Yes",</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_hold_str(trunk->hold_access));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "=== ==> Station name: %s\n", station_ref->station->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "=== ---------------------------------------------------------\n===\n");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_iterator_destroy(&i);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "=============================================================\n\n");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return CLI_SUCCESS;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static const char *trunkstate2str(enum sla_trunk_state state)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+#define S(e) case e: return # e;</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (state) {</span><br><span style="color: hsl(120, 100%, 40%);">+ S(SLA_TRUNK_STATE_IDLE)</span><br><span style="color: hsl(120, 100%, 40%);">+ S(SLA_TRUNK_STATE_RINGING)</span><br><span style="color: hsl(120, 100%, 40%);">+ S(SLA_TRUNK_STATE_UP)</span><br><span style="color: hsl(120, 100%, 40%);">+ S(SLA_TRUNK_STATE_ONHOLD)</span><br><span style="color: hsl(120, 100%, 40%);">+ S(SLA_TRUNK_STATE_ONHOLD_BYME)</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ return "Uknown State";</span><br><span style="color: hsl(120, 100%, 40%);">+#undef S</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static char *sla_show_stations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_iterator i;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (cmd) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case CLI_INIT:</span><br><span style="color: hsl(120, 100%, 40%);">+ e->command = "sla show stations";</span><br><span style="color: hsl(120, 100%, 40%);">+ e->usage =</span><br><span style="color: hsl(120, 100%, 40%);">+ "Usage: sla show stations\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ " This will list all stations defined in sla.conf\n";</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ case CLI_GENERATE:</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=============================================================\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== Configured SLA Stations =================================\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=============================================================\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "===\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ i = ao2_iterator_init(sla_stations, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ for (; (station = ao2_iterator_next(&i)); ao2_ref(station, -1)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ char ring_timeout[16] = "(none)";</span><br><span style="color: hsl(120, 100%, 40%);">+ char ring_delay[16] = "(none)";</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (station->ring_timeout) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(ring_timeout, sizeof(ring_timeout), "%u", station->ring_timeout);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (station->ring_delay) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(ring_delay, sizeof(ring_delay), "%u", station->ring_delay);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "=== ---------------------------------------------------------\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== Station Name: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> Device: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> AutoContext: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> RingTimeout: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> RingDelay: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> HoldAccess: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> Trunks ...\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ station->name, station->device,</span><br><span style="color: hsl(120, 100%, 40%);">+ S_OR(station->autocontext, "(none)"),</span><br><span style="color: hsl(120, 100%, 40%);">+ ring_timeout, ring_delay,</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_hold_str(station->hold_access));</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->ring_timeout) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(ring_timeout, sizeof(ring_timeout), "%u", trunk_ref->ring_timeout);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ strcpy(ring_timeout, "(none)");</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->ring_delay) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(ring_delay, sizeof(ring_delay), "%u", trunk_ref->ring_delay);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ strcpy(ring_delay, "(none)");</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "=== ==> Trunk Name: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> State: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> RingTimeout: %s\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "=== ==> RingDelay: %s\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->trunk->name,</span><br><span style="color: hsl(120, 100%, 40%);">+ trunkstate2str(trunk_ref->state),</span><br><span style="color: hsl(120, 100%, 40%);">+ ring_timeout, ring_delay);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "=== ---------------------------------------------------------\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "===\n");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_iterator_destroy(&i);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "============================================================\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "\n");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return CLI_SUCCESS;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct ast_cli_entry cli_sla[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_CLI_DEFINE(sla_show_trunks, "Show SLA Trunks"),</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_CLI_DEFINE(sla_show_stations, "Show SLA Stations"),</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_queue_event_full(enum sla_event_type type, struct sla_trunk_ref *trunk_ref, struct sla_station *station, int lock)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_event *event;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla.thread == AST_PTHREADT_NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(station, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(trunk_ref, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(event = ast_calloc(1, sizeof(*event)))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(station, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(trunk_ref, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ event->type = type;</span><br><span style="color: hsl(120, 100%, 40%);">+ event->trunk_ref = trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ event->station = station;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!lock) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_INSERT_TAIL(&sla.event_q, event, entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_INSERT_TAIL(&sla.event_q, event, entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_signal(&sla.cond);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_queue_event_nolock(enum sla_event_type type)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_queue_event_full(type, NULL, NULL, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_queue_event(enum sla_event_type type)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_queue_event_full(type, NULL, NULL, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Queue a SLA event from the conference */</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_queue_event_conf(enum sla_event_type type, struct ast_channel *chan, const char *confname)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *trunk_name;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_iterator i;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_name = ast_strdupa(confname);</span><br><span style="color: hsl(120, 100%, 40%);">+ strsep(&trunk_name, "_");</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_strlen_zero(trunk_name)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Invalid conference name for SLA - '%s'!\n", confname);</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ i = ao2_iterator_init(sla_stations, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((station = ao2_iterator_next(&i))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->chan == chan && !strcmp(trunk_ref->trunk->name, trunk_name)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(trunk_ref, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* station reference given to sla_queue_event_full() */</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(station, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_iterator_destroy(&i);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!trunk_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Trunk not found for event!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_queue_event_full(type, trunk_ref, station, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Framehook to support HOLD within the conference</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct sla_framehook_data {</span><br><span style="color: hsl(120, 100%, 40%);">+ int framehook_id;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *confname;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static const struct ast_datastore_info sla_framehook_datastore = {</span><br><span style="color: hsl(120, 100%, 40%);">+ .type = "app_sla",</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int remove_framehook(struct ast_channel *chan)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_datastore *datastore = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_framehook_data *data;</span><br><span style="color: hsl(120, 100%, 40%);">+ SCOPED_CHANNELLOCK(chan_lock, chan);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ datastore = ast_channel_datastore_find(chan, &sla_framehook_datastore, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!datastore) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(AST_LOG_WARNING, "Cannot remove framehook from %s: HOLD_INTERCEPT not currently enabled\n", ast_channel_name(chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ data = datastore->data;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(data->confname);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_framehook_detach(chan, data->framehook_id)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(AST_LOG_WARNING, "Failed to remove framehook from channel %s\n", ast_channel_name(chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_channel_datastore_remove(chan, datastore)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(AST_LOG_WARNING, "Failed to remove datastore from channel %s\n", ast_channel_name(chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_datastore_free(datastore);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct ast_frame *sla_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_framehook_data *sla_data = data;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return f;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_HOLD) {</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_queue_event_conf(SLA_EVENT_HOLD, chan, sla_data->confname);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ return f;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Callback function which informs upstream if we are consuming a frame of a specific type */</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_framehook_consume(void *data, enum ast_frame_type type)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ return (type == AST_FRAME_CONTROL ? 1 : 0);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int attach_framehook(struct ast_channel *chan, const char *confname)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_datastore *datastore;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_framehook_data *data;</span><br><span style="color: hsl(120, 100%, 40%);">+ static struct ast_framehook_interface sla_framehook_interface = {</span><br><span style="color: hsl(120, 100%, 40%);">+ .version = AST_FRAMEHOOK_INTERFACE_VERSION,</span><br><span style="color: hsl(120, 100%, 40%);">+ .event_cb = sla_framehook,</span><br><span style="color: hsl(120, 100%, 40%);">+ .consume_cb = sla_framehook_consume,</span><br><span style="color: hsl(120, 100%, 40%);">+ .disable_inheritance = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+ };</span><br><span style="color: hsl(120, 100%, 40%);">+ SCOPED_CHANNELLOCK(chan_lock, chan);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ datastore = ast_channel_datastore_find(chan, &sla_framehook_datastore, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (datastore) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(AST_LOG_WARNING, "SLA framehook already set on '%s'\n", ast_channel_name(chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ datastore = ast_datastore_alloc(&sla_framehook_datastore, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!datastore) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ data = ast_calloc(1, sizeof(*data));</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!data) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_datastore_free(datastore);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ data->framehook_id = ast_framehook_attach(chan, &sla_framehook_interface);</span><br><span style="color: hsl(120, 100%, 40%);">+ data->confname = ast_strdup(confname);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!data->confname || data->framehook_id < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(AST_LOG_WARNING, "Failed to attach SLA framehook to '%s'\n", ast_channel_name(chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_datastore_free(datastore);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(data);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ datastore->data = data;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_channel_datastore_add(chan, datastore);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \internal</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Find an SLA trunk by name</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static struct sla_trunk *sla_find_trunk(const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk tmp_trunk = {</span><br><span style="color: hsl(120, 100%, 40%);">+ .name = name,</span><br><span style="color: hsl(120, 100%, 40%);">+ };</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return ao2_find(sla_trunks, &tmp_trunk, OBJ_POINTER);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \internal</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Find an SLA station by name</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static struct sla_station *sla_find_station(const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station tmp_station = {</span><br><span style="color: hsl(120, 100%, 40%);">+ .name = name,</span><br><span style="color: hsl(120, 100%, 40%);">+ };</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return ao2_find(sla_stations, &tmp_station, OBJ_POINTER);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_check_station_hold_access(const struct sla_trunk *trunk, const struct sla_station *station)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station_ref *station_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* For each station that has this call on hold, check for private hold. */</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station_ref->station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->trunk != trunk || station_ref->station == station) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->state == SLA_TRUNK_STATE_ONHOLD_BYME && station_ref->station->hold_access == SLA_HOLD_PRIVATE) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Find a trunk reference on a station by name</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param station the station</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param name the trunk's name</span><br><span style="color: hsl(120, 100%, 40%);">+ * \pre sla_station is locked</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return a pointer to the station's trunk reference. If the trunk</span><br><span style="color: hsl(120, 100%, 40%);">+ * is not found, it is not idle and barge is disabled, or if</span><br><span style="color: hsl(120, 100%, 40%);">+ * it is on hold and private hold is set, then NULL will be returned.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static struct sla_trunk_ref *sla_find_trunk_ref_byname(const struct sla_station *station, const char *name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (strcasecmp(trunk_ref->trunk->name, name)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->trunk->barge_disabled && trunk_ref->state == SLA_TRUNK_STATE_UP) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(2, "Barge disabled, trunk not available\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (trunk_ref->trunk->hold_stations && trunk_ref->trunk->hold_access == SLA_HOLD_PRIVATE && trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(2, "Private hold by another station\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (sla_check_station_hold_access(trunk_ref->trunk, station)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(2, "No hold access\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(trunk_ref, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_station_ref_destructor(void *obj)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station_ref *station_ref = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (station_ref->station) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(station_ref->station, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ station_ref->station = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct sla_station_ref *sla_create_station_ref(struct sla_station *station)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station_ref *station_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(station_ref = ao2_alloc(sizeof(*station_ref), sla_station_ref_destructor))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(station, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ station_ref->station = station;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return station_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct sla_ringing_station *sla_create_ringing_station(struct sla_station *station)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(ringing_station = ast_calloc(1, sizeof(*ringing_station)))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(station, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ringing_station->station = station;</span><br><span style="color: hsl(120, 100%, 40%);">+ ringing_station->ring_begin = ast_tvnow();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return ringing_station;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_ringing_station_destroy(struct sla_ringing_station *ringing_station)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ringing_station->station) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(ringing_station->station, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ringing_station->station = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(ringing_station);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct sla_failed_station *sla_create_failed_station(struct sla_station *station)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_failed_station *failed_station;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(failed_station = ast_calloc(1, sizeof(*failed_station)))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(station, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ failed_station->station = station;</span><br><span style="color: hsl(120, 100%, 40%);">+ failed_station->last_try = ast_tvnow();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return failed_station;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_failed_station_destroy(struct sla_failed_station *failed_station)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ if (failed_station->station) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(failed_station->station, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ failed_station->station = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(failed_station);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static enum ast_device_state sla_state_to_devstate(enum sla_trunk_state state)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (state) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case SLA_TRUNK_STATE_IDLE:</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_DEVICE_NOT_INUSE;</span><br><span style="color: hsl(120, 100%, 40%);">+ case SLA_TRUNK_STATE_RINGING:</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_DEVICE_RINGING;</span><br><span style="color: hsl(120, 100%, 40%);">+ case SLA_TRUNK_STATE_UP:</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_DEVICE_INUSE;</span><br><span style="color: hsl(120, 100%, 40%);">+ case SLA_TRUNK_STATE_ONHOLD:</span><br><span style="color: hsl(120, 100%, 40%);">+ case SLA_TRUNK_STATE_ONHOLD_BYME:</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_DEVICE_ONHOLD;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_DEVICE_UNKNOWN;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_change_trunk_state(const struct sla_trunk *trunk, enum sla_trunk_state state,</span><br><span style="color: hsl(120, 100%, 40%);">+ enum sla_which_trunk_refs inactive_only, const struct sla_trunk_ref *exclude)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_iterator i;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ i = ao2_iterator_init(sla_stations, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((station = ao2_iterator_next(&i))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->trunk != trunk || (inactive_only ? trunk_ref->chan : 0) || trunk_ref == exclude) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->state = state;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_devstate_changed(sla_state_to_devstate(state), AST_DEVSTATE_CACHABLE, "SLA:%s_%s", station->name, trunk->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(station, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_iterator_destroy(&i);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct run_station_args {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_t *cond_lock;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_t *cond;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void answer_trunk_chan(struct ast_channel *chan)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_raw_answer(chan); /* Do NOT use ast_answer since that waits for media using ast_waitfor_nandfds. */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_indicate(chan, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int conf_run(struct ast_channel *chan, const char *confname, struct ast_flags *confflags, char *optargs[])</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ char confbridge_args[256];</span><br><span style="color: hsl(120, 100%, 40%);">+ int res = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(confbridge_args, sizeof(confbridge_args), "%s", confname);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_func_write(chan, "CONFBRIDGE(user,quiet)", ast_test_flag(confflags, CONFFLAG_QUIET) ? "1" : "0");</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_func_write(chan, "CONFBRIDGE(user,dtmf_passthrough)", ast_test_flag(confflags, CONFFLAG_PASS_DTMF) ? "1" : "0");</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_func_write(chan, "CONFBRIDGE(user,marked)", ast_test_flag(confflags, CONFFLAG_MARKEDUSER) ? "1" : "0");</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_func_write(chan, "CONFBRIDGE(user,end_marked)", ast_test_flag(confflags, CONFFLAG_MARKEDEXIT) ? "1" : "0");</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_func_write(chan, "CONFBRIDGE(user,music_on_hold_when_empty)", ast_test_flag(confflags, CONFFLAG_MOH) ? "1" : "0");</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_test_flag(confflags, CONFFLAG_MOH) && !ast_strlen_zero(optargs[SLA_TRUNK_OPT_ARG_MOH_CLASS])) {</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_func_write(chan, "CONFBRIDGE(user,music_on_hold_class)", optargs[SLA_TRUNK_OPT_ARG_MOH_CLASS]);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (res) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to set up conference, aborting\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Attach a framehook that we'll use to process HOLD from stations. */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_test_flag(confflags, CONFFLAG_SLA_STATION) && attach_framehook(chan, confname)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(2, "Channel %s is running ConfBridge(%s)\n", ast_channel_name(chan), confbridge_args);</span><br><span style="color: hsl(120, 100%, 40%);">+ res = ast_pbx_exec_application(chan, "ConfBridge", confbridge_args);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_test_flag(confflags, CONFFLAG_SLA_STATION)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ remove_framehook(chan);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int conf_kick_all(struct ast_channel *chan, const char *confname)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ char confkick_args[256];</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(confkick_args, sizeof(confkick_args), "%s,all", confname);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(2, "Kicking all participants from conference %s\n", confname);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (chan) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return ast_pbx_exec_application(chan, "ConfKick", confkick_args);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* We might not have a channel available to us, use a dummy channel in that case. */</span><br><span style="color: hsl(120, 100%, 40%);">+ chan = ast_dummy_channel_alloc();</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!chan) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Failed to allocate dummy channel\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ int res = ast_pbx_exec_application(chan, "ConfKick", confkick_args);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_channel_unref(chan);</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void *run_station(void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct sla_station *, station, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct sla_trunk_ref *, trunk_ref, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_str *conf_name = ast_str_create(16);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_flags conf_flags = { 0 };</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct run_station_args *args = data;</span><br><span style="color: hsl(120, 100%, 40%);">+ station = args->station;</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref = args->trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(args->cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_signal(args->cond);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(args->cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* args is no longer valid here. */</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_atomic_fetchadd_int((int *) &trunk_ref->trunk->active_stations, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_set(&conf_name, 0, "SLA_%s", trunk_ref->trunk->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_set_flag(&conf_flags, CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_PASS_DTMF | CONFFLAG_SLA_STATION);</span><br><span style="color: hsl(120, 100%, 40%);">+ answer_trunk_chan(trunk_ref->chan);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(2, "Station %s joining conference %s\n", station->name, ast_str_buffer(conf_name));</span><br><span style="color: hsl(120, 100%, 40%);">+ conf_run(trunk_ref->chan, ast_str_buffer(conf_name), &conf_flags, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->chan = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->active_stations) &&</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) {</span><br><span style="color: hsl(120, 100%, 40%);">+ conf_kick_all(NULL, ast_str_buffer(conf_name));</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->trunk->hold_stations = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_join(station->dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_destroy(station->dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ station->dial = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(conf_name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_ringing_trunk_destroy(struct sla_ringing_trunk *ringing_trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_stop_ringing_trunk(struct sla_ringing_trunk *ringing_trunk)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station_ref *station_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ conf_kick_all(ringing_trunk->trunk->chan, ringing_trunk->trunk->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((station_ref = AST_LIST_REMOVE_HEAD(&ringing_trunk->timed_out_stations, entry))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(station_ref, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_ringing_trunk_destroy(ringing_trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_stop_ringing_station(struct sla_ringing_station *ringing_station, enum sla_station_hangup hangup)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station_ref *station_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_join(ringing_station->station->dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_destroy(ringing_station->station->dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ ringing_station->station->dial = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (hangup == SLA_STATION_HANGUP_NORMAL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ goto done;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If the station is being hung up because of a timeout, then add it to the</span><br><span style="color: hsl(120, 100%, 40%);">+ * list of timed out stations on each of the ringing trunks. This is so</span><br><span style="color: hsl(120, 100%, 40%);">+ * that when doing further processing to figure out which stations should be</span><br><span style="color: hsl(120, 100%, 40%);">+ * ringing, which trunk to answer, determining timeouts, etc., we know which</span><br><span style="color: hsl(120, 100%, 40%);">+ * ringing trunks we should ignore. */</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ringing_trunk->trunk == trunk_ref->trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!trunk_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(station_ref = sla_create_station_ref(ringing_station->station))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_INSERT_TAIL(&ringing_trunk->timed_out_stations, station_ref, entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+done:</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_ringing_station_destroy(ringing_station);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_dial_state_callback(struct ast_dial *dial)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_queue_event(SLA_EVENT_DIAL_STATE);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Check to see if dialing this station already timed out for this ringing trunk</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note Assumes sla.lock is locked</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_check_timed_out_station(const struct sla_ringing_trunk *ringing_trunk, const struct sla_station *station)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station_ref *timed_out_station;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&ringing_trunk->timed_out_stations, timed_out_station, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (station == timed_out_station->station) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Choose the highest priority ringing trunk for a station</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param station the station</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param rm remove the ringing trunk once selected</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param trunk_ref a place to store the pointer to this stations reference to</span><br><span style="color: hsl(120, 100%, 40%);">+ * the selected trunk</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return a pointer to the selected ringing trunk, or NULL if none found</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note Assumes that sla.lock is locked</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static struct sla_ringing_trunk *sla_choose_ringing_trunk(struct sla_station *station, struct sla_trunk_ref **trunk_ref, int rm)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *s_trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_trunk *ringing_trunk = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station->trunks, s_trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Make sure this is the trunk we're looking for */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (s_trunk_ref->trunk != ringing_trunk->trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* This trunk on the station is ringing. But, make sure this station</span><br><span style="color: hsl(120, 100%, 40%);">+ * didn't already time out while this trunk was ringing. */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla_check_timed_out_station(ringing_trunk, station)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (rm) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(s_trunk_ref, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ *trunk_ref = s_trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_END;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ringing_trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return ringing_trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_handle_dial_state_event(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct sla_trunk_ref *, s_trunk_ref, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_trunk *ringing_trunk = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct run_station_args args;</span><br><span style="color: hsl(120, 100%, 40%);">+ enum ast_dial_result dial_res;</span><br><span style="color: hsl(120, 100%, 40%);">+ pthread_t dont_care;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_t cond_lock;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_t cond;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ switch ((dial_res = ast_dial_state(ringing_station->station->dial))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_HANGUP:</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_INVALID:</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_FAILED:</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_TIMEOUT:</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_UNANSWERED:</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_stop_ringing_station(ringing_station, SLA_STATION_HANGUP_NORMAL);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_ANSWERED:</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Find the appropriate trunk to answer. */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ringing_trunk = sla_choose_ringing_trunk(ringing_station->station, &s_trunk_ref, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ringing_trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* This case happens in a bit of a race condition. If two stations answer</span><br><span style="color: hsl(120, 100%, 40%);">+ * the outbound call at the same time, the first one will get connected to</span><br><span style="color: hsl(120, 100%, 40%);">+ * the trunk. When the second one gets here, it will not see any trunks</span><br><span style="color: hsl(120, 100%, 40%);">+ * ringing so we have no idea what to conect it to. So, we just hang up</span><br><span style="color: hsl(120, 100%, 40%);">+ * on it. */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Found no ringing trunk for station '%s' to answer!\n", ringing_station->station->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_join(ringing_station->station->dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_destroy(ringing_station->station->dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ ringing_station->station->dial = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_ringing_station_destroy(ringing_station);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Track the channel that answered this trunk */</span><br><span style="color: hsl(120, 100%, 40%);">+ s_trunk_ref->chan = ast_dial_answered(ringing_station->station->dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Actually answer the trunk */</span><br><span style="color: hsl(120, 100%, 40%);">+ answer_trunk_chan(ringing_trunk->trunk->chan);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Now, start a thread that will connect this station to the trunk. The rest of</span><br><span style="color: hsl(120, 100%, 40%);">+ * the code here sets up the thread and ensures that it is able to save the arguments</span><br><span style="color: hsl(120, 100%, 40%);">+ * before they are no longer valid since they are allocated on the stack. */</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(s_trunk_ref, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ args.trunk_ref = s_trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(ringing_station->station, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ args.station = ringing_station->station;</span><br><span style="color: hsl(120, 100%, 40%);">+ args.cond = &cond;</span><br><span style="color: hsl(120, 100%, 40%);">+ args.cond_lock = &cond_lock;</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_ringing_trunk_destroy(ringing_trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_ringing_station_destroy(ringing_station);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_init(&cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_init(&cond, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_pthread_create_detached_background(&dont_care, NULL, run_station, &args);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_wait(&cond, &cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_destroy(&cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_destroy(&cond);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_TRYING:</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_RINGING:</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_PROGRESS:</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_PROCEEDING:</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (dial_res == AST_DIAL_RESULT_ANSWERED) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Queue up reprocessing ringing trunks, and then ringing stations again */</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_queue_event(SLA_EVENT_RINGING_TRUNK);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_queue_event(SLA_EVENT_DIAL_STATE);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_END;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Check to see if this station is already ringing</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note Assumes sla.lock is locked</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_check_ringing_station(const struct sla_station *station)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&sla.ringing_stations, ringing_station, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (station == ringing_station->station) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Check to see if this station has failed to be dialed in the past minute</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note assumes sla.lock is locked</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_check_failed_station(const struct sla_station *station)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_failed_station *failed_station;</span><br><span style="color: hsl(120, 100%, 40%);">+ int res = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.failed_stations, failed_station, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (station != failed_station->station) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_tvdiff_ms(ast_tvnow(), failed_station->last_try) > 1000) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_failed_station_destroy(failed_station);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ res = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_END</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Ring a station</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note Assumes sla.lock is locked</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_ring_station(struct sla_ringing_trunk *ringing_trunk, struct sla_station *station)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ char *tech, *tech_data;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_dial *dial;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(120, 100%, 40%);">+ enum ast_dial_result res;</span><br><span style="color: hsl(120, 100%, 40%);">+ int caller_is_saved;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_party_caller caller;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(dial = ast_dial_create())) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_set_state_callback(dial, sla_dial_state_callback);</span><br><span style="color: hsl(120, 100%, 40%);">+ tech_data = ast_strdupa(station->device);</span><br><span style="color: hsl(120, 100%, 40%);">+ tech = strsep(&tech_data, "/");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_dial_append(dial, tech, tech_data, NULL) == -1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_destroy(dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Do we need to save off the caller ID data? */</span><br><span style="color: hsl(120, 100%, 40%);">+ caller_is_saved = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!sla.attempt_callerid) {</span><br><span style="color: hsl(120, 100%, 40%);">+ caller_is_saved = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ caller = *ast_channel_caller(ringing_trunk->trunk->chan);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_party_caller_init(ast_channel_caller(ringing_trunk->trunk->chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ res = ast_dial_run(dial, ringing_trunk->trunk->chan, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Restore saved caller ID */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (caller_is_saved) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_party_caller_free(ast_channel_caller(ringing_trunk->trunk->chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_channel_caller_set(ringing_trunk->trunk->chan, &caller);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (res != AST_DIAL_RESULT_TRYING) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_failed_station *failed_station;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_destroy(dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((failed_station = sla_create_failed_station(station))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_INSERT_HEAD(&sla.failed_stations, failed_station, entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(ringing_station = sla_create_ringing_station(station))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_join(dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_destroy(dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ station->dial = dial;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_INSERT_HEAD(&sla.ringing_stations, ringing_station, entry);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Check to see if a station is in use</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_check_inuse_station(const struct sla_station *station)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->chan) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct sla_trunk_ref *sla_find_trunk_ref(const struct sla_station *station, const struct sla_trunk *trunk)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->trunk == trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(trunk_ref, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Calculate the ring delay for a given ringing trunk on a station</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param station the station</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param ringing_trunk the trunk. If NULL, the highest priority ringing trunk will be used</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return the number of ms left before the delay is complete, or INT_MAX if there is no delay</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_check_station_delay(struct sla_station *station, struct sla_ringing_trunk *ringing_trunk)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct sla_trunk_ref *, trunk_ref, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int delay = UINT_MAX;</span><br><span style="color: hsl(120, 100%, 40%);">+ int time_left, time_elapsed;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ringing_trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ringing_trunk = sla_choose_ringing_trunk(station, &trunk_ref, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref = sla_find_trunk_ref(station, ringing_trunk->trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ringing_trunk || !trunk_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return delay;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If this station has a ring delay specific to the highest priority</span><br><span style="color: hsl(120, 100%, 40%);">+ * ringing trunk, use that. Otherwise, use the ring delay specified</span><br><span style="color: hsl(120, 100%, 40%);">+ * globally for the station. */</span><br><span style="color: hsl(120, 100%, 40%);">+ delay = trunk_ref->ring_delay;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!delay) {</span><br><span style="color: hsl(120, 100%, 40%);">+ delay = station->ring_delay;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!delay) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return INT_MAX;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin);</span><br><span style="color: hsl(120, 100%, 40%);">+ time_left = (delay * 1000) - time_elapsed;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return time_left;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Ring stations based on current set of ringing trunks</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note Assumes that sla.lock is locked</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_ring_stations(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station_ref *station_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Make sure that every station that uses at least one of the ringing</span><br><span style="color: hsl(120, 100%, 40%);">+ * trunks, is ringing. */</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&ringing_trunk->trunk->stations, station_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ int time_left;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Is this station already ringing? */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla_check_ringing_station(station_ref->station)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Is this station already in a call? */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla_check_inuse_station(station_ref->station)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Did we fail to dial this station earlier? If so, has it been</span><br><span style="color: hsl(120, 100%, 40%);">+ * a minute since we tried? */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla_check_failed_station(station_ref->station)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If this station already timed out while this trunk was ringing,</span><br><span style="color: hsl(120, 100%, 40%);">+ * do not dial it again for this ringing trunk. */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla_check_timed_out_station(ringing_trunk, station_ref->station)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Check for a ring delay in progress */</span><br><span style="color: hsl(120, 100%, 40%);">+ time_left = sla_check_station_delay(station_ref->station, ringing_trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (time_left != INT_MAX && time_left > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* It is time to make this station begin to ring. Do it! */</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_ring_station(ringing_trunk, station_ref->station);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Now, all of the stations that should be ringing, are ringing. */</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_hangup_stations(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->trunk == ringing_trunk->trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ringing_trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!trunk_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_join(ringing_station->station->dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_destroy(ringing_station->station->dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ ringing_station->station->dial = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_ringing_station_destroy(ringing_station);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_END</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_handle_ringing_trunk_event(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_ring_stations();</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Find stations that shouldn't be ringing anymore. */</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_hangup_stations();</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_handle_hold_event(struct sla_event *event)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_atomic_fetchadd_int((int *) &event->trunk_ref->trunk->hold_stations, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ event->trunk_ref->state = SLA_TRUNK_STATE_ONHOLD_BYME;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_devstate_changed(AST_DEVICE_ONHOLD, AST_DEVSTATE_CACHABLE, "SLA:%s_%s", event->station->name, event->trunk_ref->trunk->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_change_trunk_state(event->trunk_ref->trunk, SLA_TRUNK_STATE_ONHOLD, INACTIVE_TRUNK_REFS, event->trunk_ref);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (event->trunk_ref->trunk->active_stations == 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* The station putting it on hold is the only one on the call, so start</span><br><span style="color: hsl(120, 100%, 40%);">+ * Music on hold to the trunk. */</span><br><span style="color: hsl(120, 100%, 40%);">+ event->trunk_ref->trunk->on_hold = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_indicate(event->trunk_ref->trunk->chan, AST_CONTROL_HOLD);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_softhangup(event->trunk_ref->chan, AST_SOFTHANGUP_DEV);</span><br><span style="color: hsl(120, 100%, 40%);">+ event->trunk_ref->chan = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Process trunk ring timeouts</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note Called with sla.lock locked</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return non-zero if a change to the ringing trunks was made</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_calc_trunk_timeouts(unsigned int *timeout)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+ int res = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ int time_left, time_elapsed;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ringing_trunk->trunk->ring_timeout) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin);</span><br><span style="color: hsl(120, 100%, 40%);">+ time_left = (ringing_trunk->trunk->ring_timeout * 1000) - time_elapsed;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (time_left <= 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(ringing_trunk->trunk->chan, "SLATRUNK_STATUS", "RINGTIMEOUT");</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_stop_ringing_trunk(ringing_trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+ res = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (time_left < *timeout) {</span><br><span style="color: hsl(120, 100%, 40%);">+ *timeout = time_left;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_END;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Process station ring timeouts</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note Called with sla.lock locked</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return non-zero if a change to the ringing stations was made</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_calc_station_timeouts(unsigned int *timeout)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(120, 100%, 40%);">+ int res = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int ring_timeout = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ int time_elapsed, time_left = INT_MAX, final_trunk_time_left = INT_MIN;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If there are any ring timeouts specified for a specific trunk</span><br><span style="color: hsl(120, 100%, 40%);">+ * on the station, then use the highest per-trunk ring timeout.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Otherwise, use the ring timeout set for the entire station. */</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station_ref *station_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ int trunk_time_elapsed, trunk_time_left;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ringing_trunk->trunk == trunk_ref->trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ringing_trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If there is a trunk that is ringing without a timeout, then the</span><br><span style="color: hsl(120, 100%, 40%);">+ * only timeout that could matter is a global station ring timeout. */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!trunk_ref->ring_timeout) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* This trunk on this station is ringing and has a timeout.</span><br><span style="color: hsl(120, 100%, 40%);">+ * However, make sure this trunk isn't still ringing from a</span><br><span style="color: hsl(120, 100%, 40%);">+ * previous timeout. If so, don't consider it. */</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&ringing_trunk->timed_out_stations, station_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (station_ref->station == ringing_station->station) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (station_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin);</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_time_left = (trunk_ref->ring_timeout * 1000) - trunk_time_elapsed;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_time_left > final_trunk_time_left) {</span><br><span style="color: hsl(120, 100%, 40%);">+ final_trunk_time_left = trunk_time_left;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* No timeout was found for ringing trunks, and no timeout for the entire station */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (final_trunk_time_left == INT_MIN && !ringing_station->station->ring_timeout) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Compute how much time is left for a global station timeout */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ringing_station->station->ring_timeout) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ring_timeout = ringing_station->station->ring_timeout;</span><br><span style="color: hsl(120, 100%, 40%);">+ time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_station->ring_begin);</span><br><span style="color: hsl(120, 100%, 40%);">+ time_left = (ring_timeout * 1000) - time_elapsed;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If the time left based on the per-trunk timeouts is smaller than the</span><br><span style="color: hsl(120, 100%, 40%);">+ * global station ring timeout, use that. */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (final_trunk_time_left > INT_MIN && final_trunk_time_left < time_left) {</span><br><span style="color: hsl(120, 100%, 40%);">+ time_left = final_trunk_time_left;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If there is no time left, the station needs to stop ringing */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (time_left <= 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_stop_ringing_station(ringing_station, SLA_STATION_HANGUP_TIMEOUT);</span><br><span style="color: hsl(120, 100%, 40%);">+ res = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* There is still some time left for this station to ring, so save that</span><br><span style="color: hsl(120, 100%, 40%);">+ * timeout if it is the first event scheduled to occur */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (time_left < *timeout) {</span><br><span style="color: hsl(120, 100%, 40%);">+ *timeout = time_left;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_END;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Calculate the ring delay for a station</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note Assumes sla.lock is locked</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_calc_station_delays(unsigned int *timeout)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station;</span><br><span style="color: hsl(120, 100%, 40%);">+ int res = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_iterator i;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ i = ao2_iterator_init(sla_stations, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ for (; (station = ao2_iterator_next(&i)); ao2_ref(station, -1)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+ int time_left;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Ignore stations already ringing */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla_check_ringing_station(station)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Ignore stations already on a call */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla_check_inuse_station(station)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Ignore stations that don't have one of their trunks ringing */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(ringing_trunk = sla_choose_ringing_trunk(station, NULL, 0))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((time_left = sla_check_station_delay(station, ringing_trunk)) == INT_MAX) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If there is no time left, then the station needs to start ringing.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Return non-zero so that an event will be queued up an event to</span><br><span style="color: hsl(120, 100%, 40%);">+ * make that happen. */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (time_left <= 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ res = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (time_left < *timeout) {</span><br><span style="color: hsl(120, 100%, 40%);">+ *timeout = time_left;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_iterator_destroy(&i);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Calculate the time until the next known event</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note Called with sla.lock locked */</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_process_timers(struct timespec *ts)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int timeout = UINT_MAX;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct timeval wait;</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int change_made = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Check for ring timeouts on ringing trunks */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla_calc_trunk_timeouts(&timeout)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ change_made = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Check for ring timeouts on ringing stations */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla_calc_station_timeouts(&timeout)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ change_made = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Check for station ring delays */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla_calc_station_delays(&timeout)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ change_made = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* queue reprocessing of ringing trunks */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (change_made) {</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_queue_event_nolock(SLA_EVENT_RINGING_TRUNK);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* No timeout */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (timeout == UINT_MAX) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ts) {</span><br><span style="color: hsl(120, 100%, 40%);">+ wait = ast_tvadd(ast_tvnow(), ast_samp2tv(timeout, 1000));</span><br><span style="color: hsl(120, 100%, 40%);">+ ts->tv_sec = wait.tv_sec;</span><br><span style="color: hsl(120, 100%, 40%);">+ ts->tv_nsec = wait.tv_usec * 1000;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_event_destroy(struct sla_event *event)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ if (event->trunk_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(event->trunk_ref, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ event->trunk_ref = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (event->station) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(event->station, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ event->station = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(event);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void *sla_thread(void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_failed_station *failed_station;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_station *ringing_station;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ while (!sla.stop) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_event *event;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct timespec ts = { 0, };</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int have_timeout = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (AST_LIST_EMPTY(&sla.event_q)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((have_timeout = sla_process_timers(&ts))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_timedwait(&sla.cond, &sla.lock, &ts);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_wait(&sla.cond, &sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla.stop) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (have_timeout) {</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_process_timers(NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((event = AST_LIST_REMOVE_HEAD(&sla.event_q, entry))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (event->type) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case SLA_EVENT_HOLD:</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_handle_hold_event(event);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case SLA_EVENT_DIAL_STATE:</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_handle_dial_state_event();</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case SLA_EVENT_RINGING_TRUNK:</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_handle_ringing_trunk_event();</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_event_destroy(event);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((ringing_station = AST_LIST_REMOVE_HEAD(&sla.ringing_stations, entry))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_ringing_station_destroy(ringing_station);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((failed_station = AST_LIST_REMOVE_HEAD(&sla.failed_stations, entry))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_failed_station_destroy(failed_station);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct dial_trunk_args {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_t *cond_lock;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_t *cond;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void *dial_trunk(void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct dial_trunk_args *args = data;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_dial *dial;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *tech, *tech_data;</span><br><span style="color: hsl(120, 100%, 40%);">+ enum ast_dial_result dial_res;</span><br><span style="color: hsl(120, 100%, 40%);">+ char conf_name[MAX_CONFNUM];</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_flags conf_flags = { 0 };</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct sla_trunk_ref *, trunk_ref, args->trunk_ref, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct sla_station *, station, args->station, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ int caller_is_saved;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_party_caller caller;</span><br><span style="color: hsl(120, 100%, 40%);">+ int last_state = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ int current_state = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(dial = ast_dial_create())) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(args->cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_signal(args->cond);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(args->cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ tech_data = ast_strdupa(trunk_ref->trunk->device);</span><br><span style="color: hsl(120, 100%, 40%);">+ tech = strsep(&tech_data, "/");</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_dial_append(dial, tech, tech_data, NULL) == -1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(args->cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_signal(args->cond);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(args->cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_destroy(dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Do we need to save of the caller ID data? */</span><br><span style="color: hsl(120, 100%, 40%);">+ caller_is_saved = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!sla.attempt_callerid) {</span><br><span style="color: hsl(120, 100%, 40%);">+ caller_is_saved = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ caller = *ast_channel_caller(trunk_ref->chan);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_party_caller_init(ast_channel_caller(trunk_ref->chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ dial_res = ast_dial_run(dial, trunk_ref->chan, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Restore saved caller ID */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (caller_is_saved) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_party_caller_free(ast_channel_caller(trunk_ref->chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_channel_caller_set(trunk_ref->chan, &caller);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (dial_res != AST_DIAL_RESULT_TRYING) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(args->cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_signal(args->cond);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(args->cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_destroy(dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Wait for dial to end, while servicing the channel */</span><br><span style="color: hsl(120, 100%, 40%);">+ while (ast_waitfor(trunk_ref->chan, 100)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int done = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_frame *fr = ast_read(trunk_ref->chan);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!fr) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Channel %s did not return a frame, must have hung up\n", ast_channel_name(trunk_ref->chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ done = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_frfree(fr); /* Ignore while dialing */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ switch ((dial_res = ast_dial_state(dial))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_ANSWERED:</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->trunk->chan = ast_dial_answered(dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_HANGUP:</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_INVALID:</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_FAILED:</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_TIMEOUT:</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_UNANSWERED:</span><br><span style="color: hsl(120, 100%, 40%);">+ done = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_TRYING:</span><br><span style="color: hsl(120, 100%, 40%);">+ current_state = AST_CONTROL_PROGRESS;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_RINGING:</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_PROGRESS:</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_DIAL_RESULT_PROCEEDING:</span><br><span style="color: hsl(120, 100%, 40%);">+ current_state = AST_CONTROL_RINGING;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (done) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* check that SLA station that originated trunk call is still alive */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (station && ast_device_state(station->device) == AST_DEVICE_NOT_INUSE) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(3, "Originating station device %s no longer active\n", station->device);</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->trunk->chan = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If trunk line state changed, send indication back to originating SLA Station channel */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (current_state != last_state) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(3, "Indicating State Change %d to channel %s\n", current_state, ast_channel_name(trunk_ref->chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_indicate(trunk_ref->chan, current_state);</span><br><span style="color: hsl(120, 100%, 40%);">+ last_state = current_state;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!trunk_ref->trunk->chan) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(args->cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_signal(args->cond);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(args->cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_join(dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_destroy(dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(conf_name, sizeof(conf_name), "SLA_%s", trunk_ref->trunk->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_set_flag(&conf_flags, CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_MARKEDUSER | CONFFLAG_PASS_DTMF | CONFFLAG_SLA_TRUNK);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(args->cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_signal(args->cond);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(args->cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(2, "Trunk dial %s joining conference %s\n", trunk_ref->trunk->name, conf_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ conf_run(trunk_ref->trunk->chan, conf_name, &conf_flags, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If the trunk is going away, it is definitely now IDLE. */</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->trunk->chan = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->trunk->on_hold = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_join(dial);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dial_destroy(dial);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief For a given station, choose the highest priority idle trunk</span><br><span style="color: hsl(120, 100%, 40%);">+ * \pre sla_station is locked</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static struct sla_trunk_ref *sla_choose_idle_trunk(const struct sla_station *station)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->state == SLA_TRUNK_STATE_IDLE) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(trunk_ref, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_station_exec(struct ast_channel *chan, const char *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ char *station_name, *trunk_name;</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct sla_station *, station, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct sla_trunk_ref *, trunk_ref, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ char conf_name[MAX_CONFNUM];</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_flags conf_flags = { 0 };</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_strlen_zero(data)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid Arguments to SLAStation!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE");</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_name = ast_strdupa(data);</span><br><span style="color: hsl(120, 100%, 40%);">+ station_name = strsep(&trunk_name, "_");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_strlen_zero(station_name)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid Arguments to SLAStation!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE");</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ station = sla_find_station(station_name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!station) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Station '%s' not found!\n", station_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE");</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(trunk_name)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref = sla_find_trunk_ref_byname(station, trunk_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref = sla_choose_idle_trunk(station);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!trunk_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_strlen_zero(trunk_name)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_NOTICE, "No trunks available for call.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_NOTICE, "Can't join existing call on trunk '%s' due to access controls.\n", trunk_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "CONGESTION");</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->state == SLA_TRUNK_STATE_ONHOLD_BYME) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->hold_stations) == 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->state = SLA_TRUNK_STATE_UP;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_devstate_changed(AST_DEVICE_INUSE, AST_DEVSTATE_CACHABLE, "SLA:%s_%s", station->name, trunk_ref->trunk->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (trunk_ref->state == SLA_TRUNK_STATE_RINGING) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ringing_trunk->trunk == trunk_ref->trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_END</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ringing_trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ answer_trunk_chan(ringing_trunk->trunk->chan);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_ringing_trunk_destroy(ringing_trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Queue up reprocessing ringing trunks, and then ringing stations again */</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_queue_event(SLA_EVENT_RINGING_TRUNK);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_queue_event(SLA_EVENT_DIAL_STATE);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->chan = chan;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!trunk_ref->trunk->chan) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_t cond_lock;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_t cond;</span><br><span style="color: hsl(120, 100%, 40%);">+ pthread_t dont_care;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct dial_trunk_args args = {</span><br><span style="color: hsl(120, 100%, 40%);">+ .trunk_ref = trunk_ref,</span><br><span style="color: hsl(120, 100%, 40%);">+ .station = station,</span><br><span style="color: hsl(120, 100%, 40%);">+ .cond_lock = &cond_lock,</span><br><span style="color: hsl(120, 100%, 40%);">+ .cond = &cond,</span><br><span style="color: hsl(120, 100%, 40%);">+ };</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(trunk_ref, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(station, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Create a thread to dial the trunk and dump it into the conference.</span><br><span style="color: hsl(120, 100%, 40%);">+ * However, we want to wait until the trunk has been dialed and the</span><br><span style="color: hsl(120, 100%, 40%);">+ * conference is created before continuing on here.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Don't autoservice the channel or we'll have multiple threads</span><br><span style="color: hsl(120, 100%, 40%);">+ * handling it. dial_trunk services the channel.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_init(&cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_init(&cond, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_pthread_create_detached_background(&dont_care, NULL, dial_trunk, &args);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_wait(&cond, &cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_destroy(&cond_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_destroy(&cond);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!trunk_ref->trunk->chan) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Trunk didn't get created. chan: %lx\n", (unsigned long) trunk_ref->trunk->chan);</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "CONGESTION");</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->chan = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_atomic_fetchadd_int((int *) &trunk_ref->trunk->active_stations, 1) == 0 &&</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->trunk->on_hold) {</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->trunk->on_hold = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_indicate(trunk_ref->trunk->chan, AST_CONTROL_UNHOLD);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(conf_name, sizeof(conf_name), "SLA_%s", trunk_ref->trunk->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_set_flag(&conf_flags, CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_PASS_DTMF | CONFFLAG_SLA_STATION);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_answer(chan);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(2, "Station %s joining conference %s\n", station->name, conf_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ conf_run(chan, conf_name, &conf_flags, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->chan = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->active_stations) &&</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) {</span><br><span style="color: hsl(120, 100%, 40%);">+ conf_kick_all(chan, conf_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->trunk->hold_stations = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "SUCCESS");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_trunk_ref_destructor(void *obj)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(trunk_ref->trunk, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->trunk = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct sla_trunk_ref *create_trunk_ref(struct sla_trunk *trunk)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(trunk_ref = ao2_alloc(sizeof(*trunk_ref), sla_trunk_ref_destructor))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(trunk, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->trunk = trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct sla_ringing_trunk *queue_ringing_trunk(struct sla_trunk *trunk)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(ringing_trunk = ast_calloc(1, sizeof(*ringing_trunk)))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(trunk, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ringing_trunk->trunk = trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+ ringing_trunk->ring_begin = ast_tvnow();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_change_trunk_state(trunk, SLA_TRUNK_STATE_RINGING, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_INSERT_HEAD(&sla.ringing_trunks, ringing_trunk, entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_queue_event(SLA_EVENT_RINGING_TRUNK);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return ringing_trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_ringing_trunk_destroy(struct sla_ringing_trunk *ringing_trunk)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ringing_trunk->trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(ringing_trunk->trunk, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ringing_trunk->trunk = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(ringing_trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_trunk_exec(struct ast_channel *chan, const char *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ char conf_name[MAX_CONFNUM];</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_flags conf_flags = { 0 };</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct sla_trunk *, trunk, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_ringing_trunk *ringing_trunk;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_DECLARE_APP_ARGS(args,</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_ARG(trunk_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_ARG(options);</span><br><span style="color: hsl(120, 100%, 40%);">+ );</span><br><span style="color: hsl(120, 100%, 40%);">+ char *opts[SLA_TRUNK_OPT_ARG_ARRAY_SIZE] = { NULL, };</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_flags opt_flags = { 0 };</span><br><span style="color: hsl(120, 100%, 40%);">+ char *parse;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_strlen_zero(data)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "The SLATrunk application requires an argument, the trunk name\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ parse = ast_strdupa(data);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STANDARD_APP_ARGS(args, parse);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (args.argc == 2) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_app_parse_options(sla_trunk_opts, &opt_flags, opts, args.options)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Error parsing options for SLATrunk\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk = sla_find_trunk(args.trunk_name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "SLA Trunk '%s' not found!\n", args.trunk_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk->chan) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Call came in on %s, but the trunk is already in use!\n", args.trunk_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk->chan = chan;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(ringing_trunk = queue_ringing_trunk(trunk))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(conf_name, sizeof(conf_name), "SLA_%s", args.trunk_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_set_flag(&conf_flags, CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_MARKEDUSER | CONFFLAG_PASS_DTMF);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_test_flag(&opt_flags, SLA_TRUNK_OPT_MOH)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_indicate(chan, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_set_flag(&conf_flags, CONFFLAG_MOH);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_indicate(chan, AST_CONTROL_RINGING);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(2, "Trunk %s joining conference %s\n", args.trunk_name, conf_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ conf_run(chan, conf_name, &conf_flags, opts);</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk->chan = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk->on_hold = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_change_trunk_state(trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!pbx_builtin_getvar_helper(chan, "SLATRUNK_STATUS")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "SUCCESS");</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Remove the entry from the list of ringing trunks if it is still there. */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ringing_trunk->trunk == trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_END;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ringing_trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_ringing_trunk_destroy(ringing_trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "UNANSWERED");</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Queue reprocessing of ringing trunks to make stations stop ringing</span><br><span style="color: hsl(120, 100%, 40%);">+ * that shouldn't be ringing after this trunk stopped. */</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_queue_event(SLA_EVENT_RINGING_TRUNK);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static enum ast_device_state sla_state(const char *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ char *buf, *station_name, *trunk_name;</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct sla_station *, station, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ enum ast_device_state res = AST_DEVICE_INVALID;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_name = buf = ast_strdupa(data);</span><br><span style="color: hsl(120, 100%, 40%);">+ station_name = strsep(&trunk_name, "_");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ station = sla_find_station(station_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (station) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!strcasecmp(trunk_name, trunk_ref->trunk->name)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ res = sla_state_to_devstate(trunk_ref->state);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (res == AST_DEVICE_INVALID) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Could not determine state for trunk %s on station %s!\n", trunk_name, station_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_trunk_release_refs(void *obj, void *arg, int flags)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk *trunk = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station_ref *station_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((station_ref = AST_LIST_REMOVE_HEAD(&trunk->stations, entry))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(station_ref, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_station_release_refs(void *obj, void *arg, int flags)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((trunk_ref = AST_LIST_REMOVE_HEAD(&station->trunks, entry))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(trunk_ref, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_station_destructor(void *obj)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "sla_station destructor for '%s'\n", station->name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(station->autocontext)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ char exten[AST_MAX_EXTENSION];</span><br><span style="color: hsl(120, 100%, 40%);">+ char hint[AST_MAX_EXTENSION + 5];</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(exten, sizeof(exten), "%s_%s", station->name, trunk_ref->trunk->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(hint, sizeof(hint), "SLA:%s", exten);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_context_remove_extension(station->autocontext, exten, 1, sla_registrar);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_context_remove_extension(station->autocontext, hint, PRIORITY_HINT, sla_registrar);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_station_release_refs(station, NULL, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_string_field_free_memory(station);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_trunk_cmp(void *obj, void *arg, int flags)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk *trunk = obj, *trunk2 = arg;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return !strcasecmp(trunk->name, trunk2->name) ? CMP_MATCH | CMP_STOP : 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_station_cmp(void *obj, void *arg, int flags)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station = obj, *station2 = arg;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return !strcasecmp(station->name, station2->name) ? CMP_MATCH | CMP_STOP : 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_destroy(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla.thread != AST_PTHREADT_NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla.stop = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_signal(&sla.cond);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ pthread_join(sla.thread, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Drop any created contexts from the dialplan */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_context_destroy(NULL, sla_registrar);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_destroy(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_destroy(&sla.cond);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_callback(sla_trunks, 0, sla_trunk_release_refs, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_callback(sla_stations, 0, sla_station_release_refs, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(sla_trunks, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_trunks = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(sla_stations, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_stations = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_check_device(const char *device)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ char *tech, *tech_data;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ tech_data = ast_strdupa(device);</span><br><span style="color: hsl(120, 100%, 40%);">+ tech = strsep(&tech_data, "/");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_strlen_zero(tech) || ast_strlen_zero(tech_data)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_trunk_destructor(void *obj)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk *trunk = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "sla_trunk destructor for '%s'\n", trunk->name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(trunk->autocontext)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_context_remove_extension(trunk->autocontext, "s", 1, sla_registrar);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_trunk_release_refs(trunk, NULL, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_string_field_free_memory(trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_build_trunk(struct ast_config *cfg, const char *cat)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct sla_trunk *, trunk, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_variable *var;</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *dev;</span><br><span style="color: hsl(120, 100%, 40%);">+ int existing_trunk = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(dev = ast_variable_retrieve(cfg, cat, "device"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "SLA Trunk '%s' defined with no device!\n", cat);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla_check_device(dev)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "SLA Trunk '%s' defined with invalid device '%s'!\n", cat, dev);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((trunk = sla_find_trunk(cat))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk->mark = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ existing_trunk = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if ((trunk = ao2_alloc(sizeof(*trunk), sla_trunk_destructor))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_string_field_init(trunk, 32)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_string_field_set(trunk, name, cat);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_string_field_set(trunk, device, dev);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ for (var = ast_variable_browse(cfg, cat); var; var = var->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!strcasecmp(var->name, "autocontext")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_string_field_set(trunk, autocontext, var->value);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(var->name, "ringtimeout")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sscanf(var->value, "%30u", &trunk->ring_timeout) != 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid ringtimeout '%s' specified for trunk '%s'\n", var->value, trunk->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk->ring_timeout = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(var->name, "barge")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk->barge_disabled = ast_false(var->value);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(var->name, "hold")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!strcasecmp(var->value, "private")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk->hold_access = SLA_HOLD_PRIVATE;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(var->value, "open")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk->hold_access = SLA_HOLD_OPEN;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid value '%s' for hold on trunk %s\n", var->value, trunk->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (strcasecmp(var->name, "type") && strcasecmp(var->name, "device")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Invalid option '%s' specified at line %d of %s!\n", var->name, var->lineno, SLA_CONFIG_FILE);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(trunk->autocontext)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_context_find_or_create(NULL, NULL, trunk->autocontext, sla_registrar)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to automatically find or create context '%s' for SLA!\n", trunk->autocontext);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_add_extension(trunk->autocontext, 0 /* don't replace */, "s", 1,</span><br><span style="color: hsl(120, 100%, 40%);">+ NULL, NULL, slatrunk_app, ast_strdup(trunk->name), ast_free_ptr, sla_registrar)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to automatically create extension for trunk '%s'!\n", trunk->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!existing_trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_link(sla_trunks, trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \internal</span><br><span style="color: hsl(120, 100%, 40%);">+ * \pre station is not locked</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void sla_add_trunk_to_station(struct sla_station *station, struct ast_variable *var)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct sla_trunk *, trunk, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station_ref *station_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *trunk_name, *options, *cur;</span><br><span style="color: hsl(120, 100%, 40%);">+ int existing_trunk_ref = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ int existing_station_ref = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ options = ast_strdupa(var->value);</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_name = strsep(&options, ",");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk = sla_find_trunk(trunk_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Trunk '%s' not found!\n", var->value);</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk_ref->trunk == trunk) {</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->mark = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ existing_trunk_ref = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!trunk_ref && !(trunk_ref = create_trunk_ref(trunk))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->state = SLA_TRUNK_STATE_IDLE;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((cur = strsep(&options, ","))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ char *name, *value = cur;</span><br><span style="color: hsl(120, 100%, 40%);">+ name = strsep(&value, "=");</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!strcasecmp(name, "ringtimeout")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sscanf(value, "%30u", &trunk_ref->ring_timeout) != 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid ringtimeout value '%s' for trunk '%s' on station '%s'\n", value, trunk->name, station->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->ring_timeout = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(name, "ringdelay")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sscanf(value, "%30u", &trunk_ref->ring_delay) != 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid ringdelay value '%s' for trunk '%s' on station '%s'\n", value, trunk->name, station->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->ring_delay = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid option '%s' for trunk '%s' on station '%s'\n", name, trunk->name, station->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (station_ref->station == station) {</span><br><span style="color: hsl(120, 100%, 40%);">+ station_ref->mark = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ existing_station_ref = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!station_ref && !(station_ref = sla_create_station_ref(station))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!existing_trunk_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(trunk_ref, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->mark = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!existing_station_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_INSERT_TAIL(&trunk->stations, station_ref, entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_atomic_fetchadd_int((int *) &trunk->num_stations, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!existing_trunk_ref) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_INSERT_TAIL(&station->trunks, trunk_ref, entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_build_station(struct ast_config *cfg, const char *cat)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct sla_station *, station, NULL, ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_variable *var;</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *dev;</span><br><span style="color: hsl(120, 100%, 40%);">+ int existing_station = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(dev = ast_variable_retrieve(cfg, cat, "device"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "SLA Station '%s' defined with no device!\n", cat);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((station = sla_find_station(cat))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ station->mark = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ existing_station = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if ((station = ao2_alloc(sizeof(*station), sla_station_destructor))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_string_field_init(station, 32)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_string_field_set(station, name, cat);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_string_field_set(station, device, dev);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ for (var = ast_variable_browse(cfg, cat); var; var = var->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!strcasecmp(var->name, "trunk")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_add_trunk_to_station(station, var);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(var->name, "autocontext")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_string_field_set(station, autocontext, var->value);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(var->name, "ringtimeout")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sscanf(var->value, "%30u", &station->ring_timeout) != 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid ringtimeout '%s' specified for station '%s'\n", var->value, station->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ station->ring_timeout = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(var->name, "ringdelay")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sscanf(var->value, "%30u", &station->ring_delay) != 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid ringdelay '%s' specified for station '%s'\n", var->value, station->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ station->ring_delay = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(var->name, "hold")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!strcasecmp(var->value, "private")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ station->hold_access = SLA_HOLD_PRIVATE;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(var->value, "open")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ station->hold_access = SLA_HOLD_OPEN;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid value '%s' for hold on station %s\n", var->value, station->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (strcasecmp(var->name, "type") && strcasecmp(var->name, "device")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Invalid option '%s' specified at line %d of %s!\n", var->name, var->lineno, SLA_CONFIG_FILE);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(station->autocontext)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_context_find_or_create(NULL, NULL, station->autocontext, sla_registrar)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to automatically find or create context '%s' for SLA!\n", station->autocontext);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ /* The extension for when the handset goes off-hook.</span><br><span style="color: hsl(120, 100%, 40%);">+ * exten => station1,1,SLAStation(station1) */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_add_extension(station->autocontext, 0 /* don't replace */, station->name, 1,</span><br><span style="color: hsl(120, 100%, 40%);">+ NULL, NULL, slastation_app, ast_strdup(station->name), ast_free_ptr, sla_registrar)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to automatically create extension for trunk '%s'!\n", station->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ char exten[AST_MAX_EXTENSION];</span><br><span style="color: hsl(120, 100%, 40%);">+ char hint[AST_MAX_EXTENSION + 5];</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(exten, sizeof(exten), "%s_%s", station->name, trunk_ref->trunk->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(hint, sizeof(hint), "SLA:%s", exten);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Extension for this line button</span><br><span style="color: hsl(120, 100%, 40%);">+ * exten => station1_line1,1,SLAStation(station1_line1) */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_add_extension(station->autocontext, 0 /* don't replace */, exten, 1,</span><br><span style="color: hsl(120, 100%, 40%);">+ NULL, NULL, slastation_app, ast_strdup(exten), ast_free_ptr, sla_registrar)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to automatically create extension for trunk '%s'!\n", station->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Hint for this line button</span><br><span style="color: hsl(120, 100%, 40%);">+ * exten => station1_line1,hint,SLA:station1_line1 */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_add_extension(station->autocontext, 0 /* don't replace */, exten, PRIORITY_HINT,</span><br><span style="color: hsl(120, 100%, 40%);">+ NULL, NULL, hint, NULL, NULL, sla_registrar)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Failed to automatically create hint for trunk '%s'!\n", station->name);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!existing_station) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_link(sla_stations, station);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_trunk_mark(void *obj, void *arg, int flags)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk *trunk = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station_ref *station_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk->mark = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ station_ref->mark = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_station_mark(void *obj, void *arg, int flags)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ station->mark = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ trunk_ref->mark = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_trunk_is_marked(void *obj, void *arg, int flags)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk *trunk = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (trunk->mark) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Only remove all of the station references if the trunk itself is going away */</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_trunk_release_refs(trunk, NULL, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station_ref *station_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Otherwise only remove references to stations no longer in the config */</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_BEGIN(&trunk->stations, station_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!station_ref->mark) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(station_ref, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_END</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(trunk);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return trunk->mark ? CMP_MATCH : 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_station_is_marked(void *obj, void *arg, int flags)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_station *station = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (station->mark) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Only remove all of the trunk references if the station itself is going away */</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_station_release_refs(station, NULL, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sla_trunk_ref *trunk_ref;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Otherwise only remove references to trunks no longer in the config */</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_BEGIN(&station->trunks, trunk_ref, entry) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!trunk_ref->mark) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_REMOVE_CURRENT(entry);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(trunk_ref, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_END</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlock(station);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return station->mark ? CMP_MATCH : 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_in_use(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ return ao2_container_count(sla_trunks) || ao2_container_count(sla_stations);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int sla_load_config(int reload)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_config *cfg;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *cat = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ int res = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *val;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!reload) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_init(&sla.lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cond_init(&sla.cond, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_trunks = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, sla_trunk_cmp);</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_stations = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, sla_station_cmp);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(cfg = ast_config_load(SLA_CONFIG_FILE, config_flags))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0; /* Treat no config as normal */</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (cfg == CONFIG_STATUS_FILEINVALID) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Config file " SLA_CONFIG_FILE " is in an invalid format. Aborting.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (reload) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_callback(sla_trunks, 0, sla_trunk_mark, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_callback(sla_stations, 0, sla_station_mark, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((val = ast_variable_retrieve(cfg, "general", "attemptcallerid"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ sla.attempt_callerid = ast_true(val);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((cat = ast_category_browse(cfg, cat)) && !res) {</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *type;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!strcasecmp(cat, "general")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(type = ast_variable_retrieve(cfg, cat, "type"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid entry in %s defined with no type!\n", SLA_CONFIG_FILE);</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!strcasecmp(type, "trunk")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ res = sla_build_trunk(cfg, cat);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(type, "station")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ res = sla_build_station(cfg, cat);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Entry in %s defined with invalid type '%s'!\n", SLA_CONFIG_FILE, type);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_config_destroy(cfg);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (reload) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_callback(sla_trunks, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, sla_trunk_is_marked, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_callback(sla_stations, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, sla_station_is_marked, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Start SLA event processing thread once SLA has been configured. */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sla.thread == AST_PTHREADT_NULL && sla_in_use()) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_pthread_create(&sla.thread, NULL, sla_thread, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int load_config(int reload)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ return sla_load_config(reload);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int unload_module(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ int res = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli_unregister_multiple(cli_sla, ARRAY_LEN(cli_sla));</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_unregister_application(slastation_app);</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_unregister_application(slatrunk_app);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_devstate_prov_del("SLA");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ sla_destroy();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Load the module</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Module loading including tests for configuration or dependencies.</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,</span><br><span style="color: hsl(120, 100%, 40%);">+ * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails</span><br><span style="color: hsl(120, 100%, 40%);">+ * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the</span><br><span style="color: hsl(120, 100%, 40%);">+ * configuration file or other non-critical problem return</span><br><span style="color: hsl(120, 100%, 40%);">+ * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int load_module(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ int res = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= load_config(0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli_register_multiple(cli_sla, ARRAY_LEN(cli_sla));</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_register_application_xml(slastation_app, sla_station_exec);</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_register_application_xml(slatrunk_app, sla_trunk_exec);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_devstate_prov_add("SLA", sla_state);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int reload(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ return load_config(1);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Shared Line Appearances",</span><br><span style="color: hsl(120, 100%, 40%);">+ .support_level = AST_MODULE_SUPPORT_EXTENDED,</span><br><span style="color: hsl(120, 100%, 40%);">+ .load = load_module,</span><br><span style="color: hsl(120, 100%, 40%);">+ .unload = unload_module,</span><br><span style="color: hsl(120, 100%, 40%);">+ .reload = reload,</span><br><span style="color: hsl(120, 100%, 40%);">+ .load_pri = AST_MODPRI_DEVSTATE_PROVIDER,</span><br><span style="color: hsl(120, 100%, 40%);">+);</span><br><span>diff --git a/doc/UPGRADE-staging/app_sla.txt b/doc/UPGRADE-staging/app_sla.txt</span><br><span>new file mode 100644</span><br><span>index 0000000..6beaa9e</span><br><span>--- /dev/null</span><br><span>+++ b/doc/UPGRADE-staging/app_sla.txt</span><br><span>@@ -0,0 +1,6 @@</span><br><span style="color: hsl(120, 100%, 40%);">+Subject: app_sla</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+The SLAStation and SLATrunk applications have been</span><br><span style="color: hsl(120, 100%, 40%);">+moved from app_meetme to app_sla. If you are using</span><br><span style="color: hsl(120, 100%, 40%);">+these applications and have autoload=no, you will</span><br><span style="color: hsl(120, 100%, 40%);">+need to explicitly load this module in modules.conf.</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/19606">change 19606</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/19606"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I8604cb332b219f6ed5394a84c381b5958d115aee </div>
<div style="display:none"> Gerrit-Change-Number: 19606 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: N A <asterisk@phreaknet.org> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>