<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][17172] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=17172">17172</a></dd>
<dt>Author</dt> <dd>mikej</dd>
<dt>Date</dt> <dd>2010-04-01 15:16:36 -0500 (Thu, 01 Apr 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Skinny: avoid some crashes

- move api to own file
- really allow several profiles (one thread per profile)
- global pool
- pool per profile
- Remove unused globals.calls
- Check for closed listener before sending data
- use mutex for hashes</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnyMakefileam">freeswitch/trunk/src/mod/endpoints/mod_skinny/Makefile.am</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnymod_skinnyc">freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnymod_skinnyh">freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.h</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnyskinny_protocolc">freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_protocol.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnyskinny_protocolh">freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_protocol.h</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnyskinny_tablesc">freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_tables.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnyskinny_tablesh">freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_tables.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodendpointsmod_skinnyMakefileam"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/Makefile.am (17171 => 17172)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/Makefile.am        2010-04-01 20:16:27 UTC (rev 17171)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/Makefile.am        2010-04-01 20:16:36 UTC (rev 17172)
</span><span class="lines">@@ -3,7 +3,7 @@
</span><span class="cx"> MODNAME=mod_skinny
</span><span class="cx"> 
</span><span class="cx"> mod_LTLIBRARIES = mod_skinny.la
</span><del>-mod_skinny_la_SOURCES  = mod_skinny.c skinny_protocol.c skinny_tables.c
</del><ins>+mod_skinny_la_SOURCES  = mod_skinny.c skinny_protocol.c skinny_tables.c skinny_api.c
</ins><span class="cx"> mod_skinny_la_CFLAGS   = $(AM_CFLAGS) -DSKINNY_SVN_VERSION=\&quot;`cat $(switch_builddir)/.version`\&quot;
</span><span class="cx"> mod_skinny_la_LIBADD   = $(switch_builddir)/libfreeswitch.la
</span><span class="cx"> mod_skinny_la_LDFLAGS  = -avoid-version -module -no-undefined -shared
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_skinnymod_skinnyc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c (17171 => 17172)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c        2010-04-01 20:16:27 UTC (rev 17171)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c        2010-04-01 20:16:36 UTC (rev 17172)
</span><span class="lines">@@ -33,15 +33,14 @@
</span><span class="cx"> #include &quot;mod_skinny.h&quot;
</span><span class="cx"> #include &quot;skinny_protocol.h&quot;
</span><span class="cx"> #include &quot;skinny_tables.h&quot;
</span><ins>+#include &quot;skinny_api.h&quot;
</ins><span class="cx"> 
</span><span class="cx"> SWITCH_MODULE_LOAD_FUNCTION(mod_skinny_load);
</span><span class="cx"> SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_skinny_shutdown);
</span><del>-SWITCH_MODULE_RUNTIME_FUNCTION(mod_skinny_runtime);
</del><span class="cx"> 
</span><del>-SWITCH_MODULE_DEFINITION(mod_skinny, mod_skinny_load, mod_skinny_shutdown, mod_skinny_runtime);
</del><ins>+SWITCH_MODULE_DEFINITION(mod_skinny, mod_skinny_load, mod_skinny_shutdown, NULL);
</ins><span class="cx"> 
</span><span class="cx"> switch_endpoint_interface_t *skinny_endpoint_interface;
</span><del>-static switch_memory_pool_t *module_pool = NULL;
</del><span class="cx"> 
</span><span class="cx"> skinny_globals_t globals;
</span><span class="cx"> 
</span><span class="lines">@@ -65,6 +64,7 @@
</span><span class="cx">         &quot;   device_name          VARCHAR(16),\n&quot;
</span><span class="cx">         &quot;   device_instance      INTEGER,\n&quot;
</span><span class="cx">         &quot;   position             INTEGER,\n&quot;
</span><ins>+        &quot;   line_instance        INTEGER,\n&quot;
</ins><span class="cx">         &quot;   label                VARCHAR(40),\n&quot;
</span><span class="cx">         &quot;   value                VARCHAR(24),\n&quot;
</span><span class="cx">         &quot;   caller_name          VARCHAR(44),\n&quot;
</span><span class="lines">@@ -88,10 +88,20 @@
</span><span class="cx">         &quot;   settings         VARCHAR(44)\n&quot;
</span><span class="cx">         &quot;);\n&quot;;
</span><span class="cx"> 
</span><ins>+static char active_lines_sql[] =
+        &quot;CREATE TABLE skinny_active_lines (\n&quot;
+        &quot;   device_name      VARCHAR(16),\n&quot;
+        &quot;   device_instance  INTEGER,\n&quot;
+        &quot;   line_instance    INTEGER,\n&quot;
+        &quot;   channel_uuid     VARCHAR(256),\n&quot;
+        &quot;   call_id          INTEGER,\n&quot;
+        &quot;   call_state       INTEGER\n&quot;
+        &quot;);\n&quot;;
+
</ins><span class="cx"> /*****************************************************************************/
</span><span class="cx"> /* PROFILES FUNCTIONS */
</span><span class="cx"> /*****************************************************************************/
</span><del>-static switch_status_t dump_profile(const skinny_profile_t *profile, switch_stream_handle_t *stream)
</del><ins>+switch_status_t skinny_profile_dump(const skinny_profile_t *profile, switch_stream_handle_t *stream)
</ins><span class="cx"> {
</span><span class="cx">         const char *line = &quot;=================================================================================================&quot;;
</span><span class="cx">         switch_assert(profile);
</span><span class="lines">@@ -120,12 +130,16 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-static skinny_profile_t *skinny_find_profile(const char *profile_name)
</del><ins>+skinny_profile_t *skinny_find_profile(const char *profile_name)
</ins><span class="cx"> {
</span><del>-        return (skinny_profile_t *) switch_core_hash_find(globals.profile_hash, profile_name);
</del><ins>+    skinny_profile_t *profile;
+        switch_mutex_lock(globals.mutex);
+        profile = (skinny_profile_t *) switch_core_hash_find(globals.profile_hash, profile_name);
+        switch_mutex_unlock(globals.mutex);
+        return profile;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-static switch_status_t skinny_profile_find_listener_by_device_name(skinny_profile_t *profile, const char *device_name, listener_t **listener)
</del><ins>+switch_status_t skinny_profile_find_listener_by_device_name(skinny_profile_t *profile, const char *device_name, listener_t **listener)
</ins><span class="cx"> {
</span><span class="cx">         switch_mutex_lock(profile-&gt;listener_mutex);
</span><span class="cx">         for (listener_t *l = profile-&gt;listeners; l; l = l-&gt;next) {
</span><span class="lines">@@ -138,48 +152,106 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-struct skinny_profile_find_listener_helper {
-        skinny_profile_t *profile;
-        listener_t *listener;
-        uint32_t line;
</del><ins>+switch_status_t skinny_profile_find_listener_by_device_name_and_instance(skinny_profile_t *profile, const char *device_name, uint32_t device_instance, listener_t **listener)
+{
+        switch_mutex_lock(profile-&gt;listener_mutex);
+        for (listener_t *l = profile-&gt;listeners; l; l = l-&gt;next) {
+                if (!strcmp(l-&gt;device_name, device_name) &amp;&amp; (l-&gt;device_instance == device_instance)) {
+                        *listener = l;
+                }
+        }
+        switch_mutex_unlock(profile-&gt;listener_mutex);
+
+        return SWITCH_STATUS_SUCCESS;
+}
+
+struct skinny_profile_find_session_uuid_helper {
+    skinny_profile_t *profile;
+        char *channel_uuid;
+        uint32_t line_instance;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><del>-static int skinny_profile_find_listener_callback(void *pArg, int argc, char **argv, char **columnNames)
</del><ins>+int skinny_profile_find_session_uuid_callback(void *pArg, int argc, char **argv, char **columnNames)
</ins><span class="cx"> {
</span><del>-        struct skinny_profile_find_listener_helper *helper = pArg;
-        skinny_profile_t *profile = helper-&gt;profile;
-        char *device_name = argv[0];
-        /* uint32_t position = atoi(argv[1]); */
-        uint32_t relative_position = atoi(argv[2]);
</del><ins>+        struct skinny_profile_find_session_uuid_helper *helper = pArg;
</ins><span class="cx"> 
</span><del>-        skinny_profile_find_listener_by_device_name(profile, device_name, &amp;helper-&gt;listener);
</del><ins>+        char *channel_uuid = argv[0];
+        uint32_t line_instance = atoi(argv[1]);
</ins><span class="cx"> 
</span><del>-        if(helper-&gt;listener) {
-                helper-&gt;line = relative_position;
-        }
-        return 0;
</del><ins>+    if(helper-&gt;channel_uuid == NULL) {
+        helper-&gt;channel_uuid = switch_mprintf(&quot;%s&quot;, channel_uuid);
+        helper-&gt;line_instance = line_instance;
+    }
+
+    return 0;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-static switch_status_t skinny_profile_find_listener_by_dest(skinny_profile_t *profile, const char *dest, listener_t **l, uint32_t *line)
</del><ins>+char * skinny_profile_find_session_uuid(skinny_profile_t *profile, listener_t *listener, uint32_t *line_instance_p, uint32_t call_id)
</ins><span class="cx"> {
</span><ins>+        struct skinny_profile_find_session_uuid_helper helper = {0};
</ins><span class="cx">         char *sql;
</span><del>-        struct skinny_profile_find_listener_helper helper = {0};
</del><ins>+        char *device_condition = NULL;
+        char *line_instance_condition = NULL;
+        char *call_id_condition = NULL;
+        
+        switch_assert(profile);
</ins><span class="cx">         helper.profile = profile;
</span><ins>+        helper.channel_uuid = NULL;
</ins><span class="cx">         
</span><del>-        if ((sql = switch_mprintf(&quot;SELECT device_name, position, &quot;
-                                                                &quot;(SELECT count(*) from skinny_lines sl2 &quot;
-                                                                        &quot;WHERE sl2.device_name= sl1.device_name AND sl2.device_instance= sl1.device_instance AND sl2.position &lt;= sl1.position) AS relative_position &quot;
-                                                                &quot;FROM skinny_lines sl1 WHERE value='%s'&quot;,
-                                                                dest))) {
-                skinny_execute_sql_callback(profile, profile-&gt;listener_mutex, sql, skinny_profile_find_listener_callback, &amp;helper);
</del><ins>+        if(listener) {
+            device_condition = switch_mprintf(&quot;device_name='%s' AND device_instance=%d&quot;,
+                listener-&gt;device_name, listener-&gt;device_instance);
+        } else {
+            device_condition = switch_mprintf(&quot;1=1&quot;);
+        }
+        switch_assert(device_condition);
+        if(*line_instance_p &gt; 0) {
+            line_instance_condition = switch_mprintf(&quot;line_instance=%d&quot;, *line_instance_p);
+        } else {
+            line_instance_condition = switch_mprintf(&quot;1=1&quot;);
+        }
+        switch_assert(line_instance_condition);
+        if(call_id &gt; 0) {
+            call_id_condition = switch_mprintf(&quot;call_id=%d&quot;, call_id);
+        } else {
+            call_id_condition = switch_mprintf(&quot;1=1&quot;);
+        }
+        switch_assert(call_id_condition);
+        if((sql = switch_mprintf(
+                        &quot;SELECT channel_uuid, line_instance &quot;
+                                &quot;FROM skinny_active_lines &quot;
+                                &quot;WHERE %s AND %s AND %s &quot;
+                                &quot;ORDER BY channel_uuid DESC&quot;,
+                        device_condition, line_instance_condition, call_id_condition
+                        ))) {
+                skinny_execute_sql_callback(profile, profile-&gt;sql_mutex, sql,
+                    skinny_profile_find_session_uuid_callback, &amp;helper);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><ins>+        switch_safe_free(device_condition);
+        switch_safe_free(line_instance_condition);
+        switch_safe_free(call_id_condition);
+        *line_instance_p = helper.line_instance;
+        return helper.channel_uuid;
+}
</ins><span class="cx"> 
</span><ins>+switch_core_session_t * skinny_profile_find_session(skinny_profile_t *profile, listener_t *listener, uint32_t *line_instance_p, uint32_t call_id)
+{
+    char *uuid;
+    switch_core_session_t *result = NULL;
+    uuid = skinny_profile_find_session_uuid(profile, listener, line_instance_p, call_id);
</ins><span class="cx"> 
</span><del>-        *line = helper.line;
-        *l = helper.listener;
-        
-        return SWITCH_STATUS_SUCCESS;
</del><ins>+    if(!zstr(uuid)) {
+        /* TODO Why should we force? */
+        result = switch_core_session_force_locate(uuid);
+                if(!result) {
+                    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, 
+                        &quot;Unable to find session %s on %s:%d, line %d\n&quot;,
+                        uuid, listener-&gt;device_name, listener-&gt;device_instance, *line_instance_p);
+                }
+                switch_safe_free(uuid);
+    }
+    return result;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /*****************************************************************************/
</span><span class="lines">@@ -262,28 +334,73 @@
</span><span class="cx"> /*****************************************************************************/
</span><span class="cx"> /* CHANNEL FUNCTIONS */
</span><span class="cx"> /*****************************************************************************/
</span><del>-uint32_t skinny_line_perform_set_state(listener_t *listener, const char *file, const char *func, int line, uint32_t instance, uint32_t state, uint32_t call_id)
</del><ins>+void skinny_line_perform_set_state(const char *file, const char *func, int line, listener_t *listener, uint32_t line_instance, uint32_t call_id, uint32_t call_state)
</ins><span class="cx"> {
</span><del>-        switch_assert(listener);
-        
</del><ins>+        switch_event_t *event = NULL;
+    switch_assert(listener);
+
+        skinny_device_event(listener, &amp;event, SWITCH_EVENT_CUSTOM, SKINNY_EVENT_CALL_STATE);
+        switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Skinny-Line-Instance&quot;, &quot;%d&quot;, line_instance);
+        switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Skinny-Call-Id&quot;, &quot;%d&quot;, call_id);
+        switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Skinny-Call-State&quot;, &quot;%d&quot;, call_state);
+        switch_event_fire(&amp;event);
</ins><span class="cx">         switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_DEBUG,
</span><del>-                &quot;Device %s, line %d State Change %s (%d) -&gt; %s (%d) (no session)\n&quot;,
-                listener-&gt;device_name, instance,
-                skinny_soft_key_set2str(listener-&gt;line_state[instance]), listener-&gt;line_state[instance],
-                skinny_soft_key_set2str(state), state);
</del><ins>+                &quot;Device %s:%d, Line %d, Call %d Change State to %s (%d)\n&quot;,
+                listener-&gt;device_name, listener-&gt;device_instance, line_instance, call_id,
+                skinny_call_state2str(call_state), call_state);        
+}
</ins><span class="cx"> 
</span><del>-        send_select_soft_keys(listener, instance, call_id, state, 0xffff);
-        listener-&gt;line_state[instance] = state;
-        
-        return listener-&gt;line_state[instance];
</del><ins>+
+struct skinny_line_get_state_helper {
+        uint32_t call_state;
+};
+
+int skinny_line_get_state_callback(void *pArg, int argc, char **argv, char **columnNames)
+{
+        struct skinny_line_get_state_helper *helper = pArg;
+        helper-&gt;call_state = atoi(argv[0]);
+    return 0;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-uint32_t skinny_line_get_state(listener_t *listener, uint32_t instance)
</del><ins>+uint32_t skinny_line_get_state(listener_t *listener, uint32_t line_instance, uint32_t call_id)
</ins><span class="cx"> {
</span><del>-        switch_assert(listener);
</del><ins>+    char *line_instance_condition;
+    char *call_id_condition;
+    char *sql;
+        struct skinny_line_get_state_helper helper = {0};
</ins><span class="cx"> 
</span><del>-        return listener-&gt;line_state[instance];
</del><ins>+    switch_assert(listener);
+    
+    if(line_instance &gt; 0) {
+        line_instance_condition = switch_mprintf(&quot;line_instance=%d&quot;, line_instance);
+    } else {
+        line_instance_condition = switch_mprintf(&quot;1=1&quot;);
+    }
+    switch_assert(line_instance_condition);
+    if(call_id &gt; 0) {
+        call_id_condition = switch_mprintf(&quot;call_id=%d&quot;, call_id);
+    } else {
+        call_id_condition = switch_mprintf(&quot;1=1&quot;);
+    }
+    switch_assert(call_id_condition);
+
+        if ((sql = switch_mprintf(
+                        &quot;SELECT call_state FROM skinny_active_lines &quot;
+                        &quot;WHERE device_name='%s' AND device_instance=%d &quot;
+                        &quot;AND %s AND %s&quot;,
+                        listener-&gt;device_name, listener-&gt;device_instance,
+                        line_instance_condition, call_id_condition
+                        ))) {
+                skinny_execute_sql_callback(listener-&gt;profile, listener-&gt;profile-&gt;sql_mutex, sql, skinny_line_get_state_callback, &amp;helper);
+                switch_safe_free(sql);
+        }
+        switch_safe_free(line_instance_condition);
+        switch_safe_free(call_id_condition);
+        
+        return helper.call_state;
</ins><span class="cx"> }
</span><ins>+
+
</ins><span class="cx"> switch_status_t skinny_tech_set_codec(private_t *tech_pvt, int force)
</span><span class="cx"> {
</span><span class="cx">         int ms;
</span><span class="lines">@@ -358,9 +475,7 @@
</span><span class="cx">                                                                            tech_pvt-&gt;read_impl.microseconds_per_packet,
</span><span class="cx">                                                                            tech_pvt-&gt;read_impl.samples_per_packet
</span><span class="cx">                                                                            ) != SWITCH_STATUS_SUCCESS) {
</span><del>-                        switch_channel_t *channel = NULL;
-                        channel = switch_core_session_get_channel(tech_pvt-&gt;session);
-                        assert(channel != NULL);
</del><ins>+                        switch_channel_t *channel = switch_core_session_get_channel(tech_pvt-&gt;session);
</ins><span class="cx"> 
</span><span class="cx">                         switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
</span><span class="cx">                         switch_goto_status(SWITCH_STATUS_FALSE, end);                                
</span><span class="lines">@@ -407,28 +522,19 @@
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void tech_init(private_t *tech_pvt, switch_core_session_t *session, listener_t *listener, uint32_t line)
</del><ins>+void tech_init(private_t *tech_pvt, skinny_profile_t *profile, switch_core_session_t *session)
</ins><span class="cx"> {
</span><del>-        struct line_stat_res_message *button = NULL;
-
</del><span class="cx">         switch_assert(tech_pvt);
</span><span class="cx">         switch_assert(session);
</span><del>-        switch_assert(listener);
</del><span class="cx">         
</span><span class="cx">         tech_pvt-&gt;read_frame.data = tech_pvt-&gt;databuf;
</span><span class="cx">         tech_pvt-&gt;read_frame.buflen = sizeof(tech_pvt-&gt;databuf);
</span><span class="cx">         switch_mutex_init(&amp;tech_pvt-&gt;mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
</span><span class="cx">         switch_mutex_init(&amp;tech_pvt-&gt;flag_mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
</span><del>-        tech_pvt-&gt;call_id = listener-&gt;profile-&gt;next_call_id++;
-        tech_pvt-&gt;listener = listener;
-        tech_pvt-&gt;line = line;
</del><ins>+        tech_pvt-&gt;call_id = ++profile-&gt;next_call_id;
+        tech_pvt-&gt;profile = profile;
</ins><span class="cx">         switch_core_session_set_private(session, tech_pvt);
</span><span class="cx">         tech_pvt-&gt;session = session;
</span><del>-
-        skinny_line_get(listener, line, &amp;button);
-        tech_pvt-&gt;line_name = switch_core_strdup(listener-&gt;pool, button-&gt;name);
-        tech_pvt-&gt;line_shortname = switch_core_strdup(listener-&gt;pool, button-&gt;shortname);
-        tech_pvt-&gt;line_displayname = switch_core_strdup(listener-&gt;pool, button-&gt;displayname);
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /* 
</span><span class="lines">@@ -438,14 +544,9 @@
</span><span class="cx"> */
</span><span class="cx"> switch_status_t channel_on_init(switch_core_session_t *session)
</span><span class="cx"> {
</span><del>-        switch_channel_t *channel;
-        private_t *tech_pvt = NULL;
</del><ins>+        switch_channel_t *channel = switch_core_session_get_channel(session);
+        private_t *tech_pvt = switch_core_session_get_private(session);
</ins><span class="cx"> 
</span><del>-        tech_pvt = switch_core_session_get_private(session);
-        assert(tech_pvt != NULL);
-
-        channel = switch_core_session_get_channel(session);
-        assert(channel != NULL);
</del><span class="cx">         switch_set_flag_locked(tech_pvt, TFLAG_IO);
</span><span class="cx"> 
</span><span class="cx">         /* Move channel's state machine to ROUTING. This means the call is trying
</span><span class="lines">@@ -453,9 +554,6 @@
</span><span class="cx">            where a destination has been identified. If the channel is simply
</span><span class="cx">            left in the initial state, nothing will happen. */
</span><span class="cx">         switch_channel_set_state(channel, CS_ROUTING);
</span><del>-        switch_mutex_lock(globals.calls_mutex);
-        globals.calls++;
-        switch_mutex_unlock(globals.calls_mutex);
</del><span class="cx"> 
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;%s CHANNEL INIT\n&quot;, switch_channel_get_name(channel));
</span><span class="cx"> 
</span><span class="lines">@@ -464,15 +562,8 @@
</span><span class="cx"> 
</span><span class="cx"> switch_status_t channel_on_routing(switch_core_session_t *session)
</span><span class="cx"> {
</span><del>-        switch_channel_t *channel = NULL;
-        private_t *tech_pvt = NULL;
</del><ins>+    switch_channel_t *channel = switch_core_session_get_channel(session);
</ins><span class="cx"> 
</span><del>-        channel = switch_core_session_get_channel(session);
-        assert(channel != NULL);
-
-        tech_pvt = switch_core_session_get_private(session);
-        assert(tech_pvt != NULL);
-
</del><span class="cx">         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;%s CHANNEL ROUTING\n&quot;, switch_channel_get_name(channel));
</span><span class="cx"> 
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="lines">@@ -480,16 +571,8 @@
</span><span class="cx"> 
</span><span class="cx"> switch_status_t channel_on_execute(switch_core_session_t *session)
</span><span class="cx"> {
</span><ins>+    switch_channel_t *channel = switch_core_session_get_channel(session);
</ins><span class="cx"> 
</span><del>-        switch_channel_t *channel = NULL;
-        private_t *tech_pvt = NULL;
-
-        channel = switch_core_session_get_channel(session);
-        assert(channel != NULL);
-
-        tech_pvt = switch_core_session_get_private(session);
-        assert(tech_pvt != NULL);
-
</del><span class="cx">         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;%s CHANNEL EXECUTE\n&quot;, switch_channel_get_name(channel));
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -498,14 +581,9 @@
</span><span class="cx"> 
</span><span class="cx"> switch_status_t channel_on_destroy(switch_core_session_t *session)
</span><span class="cx"> {
</span><del>-        switch_channel_t *channel = NULL;
-        private_t *tech_pvt = NULL;
</del><ins>+        switch_channel_t *channel = switch_core_session_get_channel(session);
+        private_t *tech_pvt = switch_core_session_get_private(session);
</ins><span class="cx"> 
</span><del>-        channel = switch_core_session_get_channel(session);
-        assert(channel != NULL);
-
-        tech_pvt = switch_core_session_get_private(session);
-
</del><span class="cx">         if (tech_pvt) {
</span><span class="cx">                 if (switch_core_codec_ready(&amp;tech_pvt-&gt;read_codec)) {
</span><span class="cx">                         switch_core_codec_destroy(&amp;tech_pvt-&gt;read_codec);
</span><span class="lines">@@ -521,74 +599,93 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+struct channel_on_hangup_helper {
+        private_t *tech_pvt;
+};
</ins><span class="cx"> 
</span><del>-switch_status_t channel_on_hangup(switch_core_session_t *session)
</del><ins>+int channel_on_hangup_callback(void *pArg, int argc, char **argv, char **columnNames)
</ins><span class="cx"> {
</span><del>-        switch_channel_t *channel = NULL;
-        private_t *tech_pvt = NULL;
</del><ins>+        struct channel_on_hangup_helper *helper = pArg;
</ins><span class="cx">         listener_t *listener = NULL;
</span><span class="cx"> 
</span><del>-        channel = switch_core_session_get_channel(session);
-        assert(channel != NULL);
</del><ins>+        char *device_name = argv[0];
+        uint32_t device_instance = atoi(argv[1]);
+        /* uint32_t position = atoi(argv[2]); */
+        uint32_t line_instance = atoi(argv[3]);
+        /* char *label = argv[4]; */
+        /* char *value = argv[5]; */
+        /* char *caller_name = argv[6]; */
+        /* uint32_t ring_on_idle = atoi(argv[7]); */
+        /* uint32_t ring_on_active = atoi(argv[8]); */
+        /* uint32_t busy_trigger = atoi(argv[9]); */
+        /* char *forward_all = argv[10]; */
+        /* char *forward_busy = argv[11]; */
+        /* char *forward_noanswer = argv[12]; */
+        /* uint32_t noanswer_duration = atoi(argv[13]); */
+        /* char *channel_uuid = argv[14]; */
+        uint32_t call_id = atoi(argv[15]);
+        uint32_t call_state = atoi(argv[16]);
</ins><span class="cx"> 
</span><del>-        tech_pvt = switch_core_session_get_private(session);
-        assert(tech_pvt != NULL);
</del><ins>+        skinny_profile_find_listener_by_device_name_and_instance(helper-&gt;tech_pvt-&gt;profile, device_name, device_instance, &amp;listener);
+    if(listener) {
+        if(call_state == SKINNY_CONNECTED) {
+                stop_tone(listener, line_instance, call_id);
+            }
+        set_lamp(listener, SKINNY_BUTTON_LINE, line_instance, SKINNY_LAMP_OFF);
+        clear_prompt_status(listener, line_instance, call_id);
+        if(call_state == SKINNY_CONNECTED) { /* calling parties */
+                    close_receive_channel(listener,
+                            call_id, /* uint32_t conference_id, */
+                            helper-&gt;tech_pvt-&gt;party_id, /* uint32_t pass_thru_party_id, */
+                            call_id /* uint32_t conference_id2, */
+                    );
+                    stop_media_transmission(listener,
+                            call_id, /* uint32_t conference_id, */
+                            helper-&gt;tech_pvt-&gt;party_id, /* uint32_t pass_thru_party_id, */
+                            call_id /* uint32_t conference_id2, */
+                    );
+            }
</ins><span class="cx"> 
</span><del>-        listener = tech_pvt-&gt;listener;
-        assert(listener != NULL);
-        
</del><ins>+            skinny_line_set_state(listener, line_instance, call_id, SKINNY_ON_HOOK);
+            send_select_soft_keys(listener, line_instance, call_id, SKINNY_KEY_SET_ON_HOOK, 0xffff);
+        /* TODO: DefineTimeDate */
+        set_speaker_mode(listener, SKINNY_SPEAKER_OFF);
+        set_ringer(listener, SKINNY_RING_OFF, SKINNY_RING_FOREVER, 0, call_id);
+    
+    }
+    return 0;
+}
+
+switch_status_t channel_on_hangup(switch_core_session_t *session)
+{
+        struct channel_on_hangup_helper helper = {0};
+        switch_channel_t *channel = switch_core_session_get_channel(session);
+        private_t *tech_pvt = switch_core_session_get_private(session);
+        char *sql;
+
</ins><span class="cx">         switch_clear_flag_locked(tech_pvt, TFLAG_IO);
</span><span class="cx">         switch_clear_flag_locked(tech_pvt, TFLAG_VOICE);
</span><span class="cx"> 
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;%s CHANNEL HANGUP\n&quot;, switch_channel_get_name(channel));
</span><span class="cx"> 
</span><del>-        listener-&gt;session[tech_pvt-&gt;line] = NULL;
</del><ins>+    helper.tech_pvt= tech_pvt;
</ins><span class="cx"> 
</span><del>-        stop_tone(listener, tech_pvt-&gt;line, tech_pvt-&gt;call_id);
-        set_lamp(listener, SKINNY_BUTTON_LINE, tech_pvt-&gt;line, SKINNY_LAMP_OFF);
-        clear_prompt_status(listener, tech_pvt-&gt;line, tech_pvt-&gt;call_id);
-
-        if( skinny_line_get_state(tech_pvt-&gt;listener, tech_pvt-&gt;line) == SKINNY_KEY_SET_CONNECTED ) {
-                close_receive_channel(listener,
-                        tech_pvt-&gt;call_id, /* uint32_t conference_id, */
-                        tech_pvt-&gt;party_id, /* uint32_t pass_thru_party_id, */
-                        tech_pvt-&gt;call_id /* uint32_t conference_id2, */
-                );
-                stop_media_transmission(listener,
-                        tech_pvt-&gt;call_id, /* uint32_t conference_id, */
-                        tech_pvt-&gt;party_id, /* uint32_t pass_thru_party_id, */
-                        tech_pvt-&gt;call_id /* uint32_t conference_id2, */
-                );
-                switch_mutex_lock(globals.calls_mutex);
-                globals.calls--;
-                if (globals.calls &lt; 0) {
-                        globals.calls = 0;
-                }
-                switch_mutex_unlock(globals.calls_mutex);
</del><ins>+    skinny_session_walk_lines(tech_pvt-&gt;profile, switch_core_session_get_uuid(session), channel_on_hangup_callback, &amp;helper);
+        if ((sql = switch_mprintf(
+                        &quot;DELETE FROM skinny_active_lines WHERE channel_uuid='%s'&quot;,
+                        switch_core_session_get_uuid(session)
+                        ))) {
+                skinny_execute_sql(tech_pvt-&gt;profile, sql, tech_pvt-&gt;profile-&gt;sql_mutex);
+                switch_safe_free(sql);
</ins><span class="cx">         }
</span><del>-        send_call_state(listener,
-                SKINNY_ON_HOOK,
-                tech_pvt-&gt;line,
-                tech_pvt-&gt;call_id);
-        skinny_line_set_state(listener, tech_pvt-&gt;line, SKINNY_KEY_SET_ON_HOOK, tech_pvt-&gt;call_id);
-        /* TODO: DefineTimeDate */
-        set_speaker_mode(listener, SKINNY_SPEAKER_OFF);
-        set_ringer(listener, SKINNY_RING_OFF, SKINNY_RING_FOREVER, 0);
-
</del><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> switch_status_t channel_kill_channel(switch_core_session_t *session, int sig)
</span><span class="cx"> {
</span><del>-        switch_channel_t *channel = NULL;
-        private_t *tech_pvt = NULL;
</del><ins>+        switch_channel_t *channel = switch_core_session_get_channel(session);
+        private_t *tech_pvt = switch_core_session_get_private(session);
</ins><span class="cx"> 
</span><del>-        channel = switch_core_session_get_channel(session);
-        assert(channel != NULL);
-
-        tech_pvt = switch_core_session_get_private(session);
-        assert(tech_pvt != NULL);
-
</del><span class="cx">         switch (sig) {
</span><span class="cx">         case SWITCH_SIG_KILL:
</span><span class="cx">                 switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
</span><span class="lines">@@ -631,16 +728,10 @@
</span><span class="cx"> 
</span><span class="cx"> switch_status_t channel_read_frame(switch_core_session_t *session, switch_frame_t **frame, switch_io_flag_t flags, int stream_id)
</span><span class="cx"> {
</span><del>-        switch_channel_t *channel = NULL;
-        private_t *tech_pvt = NULL;
</del><ins>+    switch_channel_t *channel = switch_core_session_get_channel(session);
+        private_t *tech_pvt = switch_core_session_get_private(session);
</ins><span class="cx">         int payload = 0;
</span><span class="cx"> 
</span><del>-        channel = switch_core_session_get_channel(session);
-        assert(channel != NULL);
-
-        tech_pvt = switch_core_session_get_private(session);
-        assert(tech_pvt != NULL);
-
</del><span class="cx">         while (!(tech_pvt-&gt;read_codec.implementation &amp;&amp; switch_rtp_ready(tech_pvt-&gt;rtp_session))) {
</span><span class="cx">                 if (switch_channel_ready(channel)) {
</span><span class="cx">                         switch_yield(10000);
</span><span class="lines">@@ -705,17 +796,10 @@
</span><span class="cx"> 
</span><span class="cx"> switch_status_t channel_write_frame(switch_core_session_t *session, switch_frame_t *frame, switch_io_flag_t flags, int stream_id)
</span><span class="cx"> {
</span><del>-        switch_channel_t *channel = NULL;
-        private_t *tech_pvt = NULL;
</del><ins>+        private_t *tech_pvt = switch_core_session_get_private(session);
</ins><span class="cx">         //switch_frame_t *pframe;
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_SUCCESS;
</span><span class="cx">         
</span><del>-        channel = switch_core_session_get_channel(session);
-        assert(channel != NULL);
-
-        tech_pvt = switch_core_session_get_private(session);
-        assert(tech_pvt != NULL);
-
</del><span class="cx">         if (!switch_test_flag(tech_pvt, TFLAG_IO)) {
</span><span class="cx">                 return SWITCH_STATUS_FALSE;
</span><span class="cx">         }
</span><span class="lines">@@ -737,31 +821,12 @@
</span><span class="cx"> 
</span><span class="cx"> switch_status_t channel_answer_channel(switch_core_session_t *session)
</span><span class="cx"> {
</span><del>-        private_t *tech_pvt;
-        switch_channel_t *channel = NULL;
-
-        channel = switch_core_session_get_channel(session);
-        assert(channel != NULL);
-
-        tech_pvt = switch_core_session_get_private(session);
-        assert(tech_pvt != NULL);
-
-
</del><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> switch_status_t channel_receive_message(switch_core_session_t *session, switch_core_session_message_t *msg)
</span><span class="cx"> {
</span><del>-        switch_channel_t *channel;
-        private_t *tech_pvt;
-
-        channel = switch_core_session_get_channel(session);
-        assert(channel != NULL);
-
-        tech_pvt = (private_t *) switch_core_session_get_private(session);
-        assert(tech_pvt != NULL);
-
</del><span class="cx">         switch (msg-&gt;message_id) {
</span><span class="cx">         case SWITCH_MESSAGE_INDICATE_ANSWER:
</span><span class="cx">                 {
</span><span class="lines">@@ -788,8 +853,7 @@
</span><span class="cx">         
</span><span class="cx">         char *profile_name, *dest;
</span><span class="cx">         skinny_profile_t *profile = NULL;
</span><del>-        listener_t *listener = NULL;
-        uint32_t line = 0;
</del><ins>+        char *sql;
</ins><span class="cx">         char name[128];
</span><span class="cx">         switch_channel_t *channel;
</span><span class="cx">         switch_caller_profile_t *caller_profile;
</span><span class="lines">@@ -833,27 +897,8 @@
</span><span class="cx">         channel = switch_core_session_get_channel(nsession);
</span><span class="cx">         switch_channel_set_name(channel, name);
</span><span class="cx">         
</span><ins>+        tech_init(tech_pvt, profile, nsession);
</ins><span class="cx"> 
</span><del>-        if ((skinny_profile_find_listener_by_dest(profile, dest, &amp;listener, &amp;line) != SWITCH_STATUS_SUCCESS)) {
-                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, &quot;Problem while retrieving listener and line for destination %s in profile %s\n&quot;, dest, profile_name);
-                cause = SWITCH_CAUSE_UNALLOCATED_NUMBER;
-                goto error;
-        }
-        
-        if (!listener) {
-                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, &quot;Invalid destination or phone not registred %s in profile %s\n&quot;, dest, profile_name);
-                cause = SWITCH_CAUSE_UNALLOCATED_NUMBER;
-                goto error;
-        }
-
-        if (line == 0) {
-                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, &quot;Invalid destination or phone not registred %s in profile %s\n&quot;, dest, profile_name);
-                cause = SWITCH_CAUSE_UNALLOCATED_NUMBER;
-                goto error;
-        }
-        
-        tech_init(tech_pvt, nsession, listener, line);
-
</del><span class="cx">         caller_profile = switch_caller_profile_clone(nsession, outbound_profile);
</span><span class="cx">         switch_channel_set_caller_profile(channel, caller_profile);
</span><span class="cx">         tech_pvt-&gt;caller_profile = caller_profile;
</span><span class="lines">@@ -861,19 +906,23 @@
</span><span class="cx">         switch_channel_set_flag(channel, CF_OUTBOUND);
</span><span class="cx">         switch_set_flag_locked(tech_pvt, TFLAG_OUTBOUND);
</span><span class="cx"> 
</span><del>-        if(tech_pvt-&gt;listener-&gt;session[tech_pvt-&gt;line]) { /* Line is busy */
-                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, &quot;Device line is busy %s in profile %s\n&quot;, dest, profile_name);
-                cause = SWITCH_CAUSE_USER_BUSY;
</del><ins>+        if ((sql = switch_mprintf(
+                        &quot;INSERT INTO skinny_active_lines &quot;
+                                &quot;(device_name, device_instance, line_instance, channel_uuid, call_id, call_state) &quot;
+                                &quot;SELECT device_name, device_instance, line_instance, '%s', %d, %d &quot;
+                                &quot;FROM skinny_lines &quot;
+                                &quot;WHERE value='%s'&quot;,
+                        switch_core_session_get_uuid(nsession), tech_pvt-&gt;call_id, SKINNY_ON_HOOK, dest
+                        ))) {
+                skinny_execute_sql(profile, sql, profile-&gt;sql_mutex);
+                switch_safe_free(sql);
+        }
+        
+        cause = skinny_ring_lines(tech_pvt);
+        
+        if(cause != SWITCH_CAUSE_SUCCESS) {
</ins><span class="cx">                 goto error;
</span><span class="cx">         }
</span><del>-        tech_pvt-&gt;listener-&gt;session[tech_pvt-&gt;line] = nsession;
-        send_call_state(tech_pvt-&gt;listener, SKINNY_RING_IN, tech_pvt-&gt;line, tech_pvt-&gt;call_id);
-        skinny_line_set_state(tech_pvt-&gt;listener, tech_pvt-&gt;line, SKINNY_KEY_SET_RING_IN, tech_pvt-&gt;call_id);
-        display_prompt_status(tech_pvt-&gt;listener, 0, &quot;\200\027tel&quot;, tech_pvt-&gt;line, tech_pvt-&gt;call_id);
-        /* displayprinotifiymessage */
-        skinny_send_call_info(nsession);
-        set_lamp(tech_pvt-&gt;listener, SKINNY_BUTTON_LINE, tech_pvt-&gt;line, SKINNY_LAMP_BLINK);
-        set_ringer(tech_pvt-&gt;listener, SKINNY_RING_OUTSIDE, SKINNY_RING_FOREVER, 0);
</del><span class="cx"> 
</span><span class="cx">         *new_session = nsession;
</span><span class="cx"> 
</span><span class="lines">@@ -950,6 +999,14 @@
</span><span class="cx"> /* LISTENER FUNCTIONS */
</span><span class="cx"> /*****************************************************************************/
</span><span class="cx"> 
</span><ins>+uint8_t listener_is_ready(listener_t *listener)
+{
+    return globals.running
+        &amp;&amp; listener
+        &amp;&amp; switch_test_flag(listener, LFLAG_RUNNING)
+        &amp;&amp; listener-&gt;profile-&gt;listener_ready;
+}
+
</ins><span class="cx"> static void add_listener(listener_t *listener)
</span><span class="cx"> {
</span><span class="cx">         skinny_profile_t *profile;
</span><span class="lines">@@ -994,6 +1051,7 @@
</span><span class="cx">         listener_t *l;
</span><span class="cx">         
</span><span class="cx">         /* walk listeners */
</span><ins>+        switch_mutex_lock(globals.mutex);
</ins><span class="cx">         for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
</span><span class="cx">                 switch_hash_this(hi, NULL, NULL, &amp;val);
</span><span class="cx">                 profile = (skinny_profile_t *) val;
</span><span class="lines">@@ -1004,6 +1062,7 @@
</span><span class="cx">                 }
</span><span class="cx">                 switch_mutex_unlock(profile-&gt;listener_mutex);
</span><span class="cx">         }
</span><ins>+        switch_mutex_unlock(globals.mutex);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static void flush_listener(listener_t *listener, switch_bool_t flush_log, switch_bool_t flush_events)
</span><span class="lines">@@ -1017,7 +1076,7 @@
</span><span class="cx">                                 &quot;DELETE FROM skinny_devices &quot;
</span><span class="cx">                                         &quot;WHERE name='%s' and instance=%d&quot;,
</span><span class="cx">                                 listener-&gt;device_name, listener-&gt;device_instance))) {
</span><del>-                        skinny_execute_sql(profile, sql, profile-&gt;listener_mutex);
</del><ins>+                        skinny_execute_sql(profile, sql, profile-&gt;sql_mutex);
</ins><span class="cx">                         switch_safe_free(sql);
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="lines">@@ -1025,7 +1084,7 @@
</span><span class="cx">                                 &quot;DELETE FROM skinny_lines &quot;
</span><span class="cx">                                         &quot;WHERE device_name='%s' and device_instance=%d&quot;,
</span><span class="cx">                                 listener-&gt;device_name, listener-&gt;device_instance))) {
</span><del>-                        skinny_execute_sql(profile, sql, profile-&gt;listener_mutex);
</del><ins>+                        skinny_execute_sql(profile, sql, profile-&gt;sql_mutex);
</ins><span class="cx">                         switch_safe_free(sql);
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="lines">@@ -1033,7 +1092,7 @@
</span><span class="cx">                                 &quot;DELETE FROM skinny_buttons &quot;
</span><span class="cx">                                         &quot;WHERE device_name='%s' and device_instance=%d&quot;,
</span><span class="cx">                                 listener-&gt;device_name, listener-&gt;device_instance))) {
</span><del>-                        skinny_execute_sql(profile, sql, profile-&gt;listener_mutex);
</del><ins>+                        skinny_execute_sql(profile, sql, profile-&gt;sql_mutex);
</ins><span class="cx">                         switch_safe_free(sql);
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="lines">@@ -1069,12 +1128,13 @@
</span><span class="cx">         return 0;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static switch_status_t dump_device(skinny_profile_t *profile, const char *device_name, switch_stream_handle_t *stream)
</del><ins>+switch_status_t dump_device(skinny_profile_t *profile, const char *device_name, switch_stream_handle_t *stream)
</ins><span class="cx"> {
</span><span class="cx">         char *sql;
</span><del>-        if ((sql = switch_mprintf(&quot;SELECT * FROM skinny_devices WHERE name LIKE '%s'&quot;,
</del><ins>+        if ((sql = switch_mprintf(&quot;SELECT name, user_id, instance, ip, type, max_streams, port, codec_string &quot;
+                        &quot;FROM skinny_devices WHERE name='%s'&quot;,
</ins><span class="cx">                         device_name))) {
</span><del>-                skinny_execute_sql_callback(profile, profile-&gt;listener_mutex, sql, dump_device_callback, stream);
</del><ins>+                skinny_execute_sql_callback(profile, profile-&gt;sql_mutex, sql, dump_device_callback, stream);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -1161,7 +1221,7 @@
</span><span class="cx">         add_listener(listener);
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-        while (globals.running &amp;&amp; switch_test_flag(listener, LFLAG_RUNNING) &amp;&amp; profile-&gt;listener_ready) {
</del><ins>+        while (listener_is_ready(listener)) {
</ins><span class="cx">                 status = skinny_read_packet(listener, &amp;request);
</span><span class="cx"> 
</span><span class="cx">                 if (status != SWITCH_STATUS_SUCCESS) {
</span><span class="lines">@@ -1200,6 +1260,7 @@
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Connection Closed\n&quot;);
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+    /* TODO
</ins><span class="cx">         for(int line = 0 ; line &lt; SKINNY_MAX_BUTTON_COUNT ; line++) {
</span><span class="cx">                 if(listener-&gt;session[line]) {
</span><span class="cx">                         switch_channel_clear_flag(switch_core_session_get_channel(listener-&gt;session[line]), CF_CONTROLLED);
</span><span class="lines">@@ -1208,6 +1269,7 @@
</span><span class="cx">                         destroy_pool = 0;
</span><span class="cx">                 }
</span><span class="cx">         }
</span><ins>+        */
</ins><span class="cx">         if(destroy_pool == 0) {
</span><span class="cx">                 goto no_destroy_pool;
</span><span class="cx">         }
</span><span class="lines">@@ -1237,25 +1299,26 @@
</span><span class="cx">         switch_thread_create(&amp;thread, thd_attr, listener_run, listener, listener-&gt;pool);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-int skinny_socket_create_and_bind(skinny_profile_t *profile)
</del><ins>+static void *SWITCH_THREAD_FUNC skinny_profile_run(switch_thread_t *thread, void *obj)
</ins><span class="cx"> {
</span><ins>+        skinny_profile_t *profile = (skinny_profile_t *) obj;
</ins><span class="cx">         switch_status_t rv;
</span><span class="cx">         switch_sockaddr_t *sa;
</span><span class="cx">         switch_socket_t *inbound_socket = NULL;
</span><span class="cx">         listener_t *listener;
</span><del>-        switch_memory_pool_t *pool = NULL, *listener_pool = NULL;
</del><ins>+        switch_memory_pool_t *tmp_pool = NULL, *listener_pool = NULL;
</ins><span class="cx">         uint32_t errs = 0;
</span><span class="cx"> 
</span><del>-        if (switch_core_new_memory_pool(&amp;pool) != SWITCH_STATUS_SUCCESS) {
</del><ins>+        if (switch_core_new_memory_pool(&amp;tmp_pool) != SWITCH_STATUS_SUCCESS) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;OH OH no pool\n&quot;);
</span><del>-                return SWITCH_STATUS_TERM;
</del><ins>+                return NULL;
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         while(globals.running) {
</span><del>-                rv = switch_sockaddr_info_get(&amp;sa, profile-&gt;ip, SWITCH_INET, profile-&gt;port, 0, pool);
</del><ins>+                rv = switch_sockaddr_info_get(&amp;sa, profile-&gt;ip, SWITCH_INET, profile-&gt;port, 0, tmp_pool);
</ins><span class="cx">                 if (rv)
</span><span class="cx">                         goto fail;
</span><del>-                rv = switch_socket_create(&amp;profile-&gt;sock, switch_sockaddr_get_family(sa), SOCK_STREAM, SWITCH_PROTO_TCP, pool);
</del><ins>+                rv = switch_socket_create(&amp;profile-&gt;sock, switch_sockaddr_get_family(sa), SOCK_STREAM, SWITCH_PROTO_TCP, tmp_pool);
</ins><span class="cx">                 if (rv)
</span><span class="cx">                         goto sock_fail;
</span><span class="cx">                 rv = switch_socket_opt_set(profile-&gt;sock, SWITCH_SO_REUSEADDR, 1);
</span><span class="lines">@@ -1326,8 +1389,8 @@
</span><span class="cx"> 
</span><span class="cx">         close_socket(&amp;profile-&gt;sock, profile);
</span><span class="cx">         
</span><del>-        if (pool) {
-                switch_core_destroy_memory_pool(&amp;pool);
</del><ins>+        if (tmp_pool) {
+                switch_core_destroy_memory_pool(&amp;tmp_pool);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (listener_pool) {
</span><span class="lines">@@ -1336,7 +1399,7 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">   fail:
</span><del>-        return SWITCH_STATUS_TERM;
</del><ins>+        return NULL;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /*****************************************************************************/
</span><span class="lines">@@ -1353,18 +1416,18 @@
</span><span class="cx">                 return;
</span><span class="cx"> 
</span><span class="cx">         if (!strcasecmp(var, &quot;domain&quot;)) {
</span><del>-                profile-&gt;domain = switch_core_strdup(module_pool, val);
</del><ins>+                profile-&gt;domain = switch_core_strdup(profile-&gt;pool, val);
</ins><span class="cx">         } else if (!strcasecmp(var, &quot;ip&quot;)) {
</span><del>-                profile-&gt;ip = switch_core_strdup(module_pool, val);
</del><ins>+                profile-&gt;ip = switch_core_strdup(profile-&gt;pool, val);
</ins><span class="cx">         } else if (!strcasecmp(var, &quot;dialplan&quot;)) {
</span><del>-                profile-&gt;dialplan = switch_core_strdup(module_pool, val);
</del><ins>+                profile-&gt;dialplan = switch_core_strdup(profile-&gt;pool, val);
</ins><span class="cx">         } else if (!strcasecmp(var, &quot;context&quot;)) {
</span><del>-                profile-&gt;context = switch_core_strdup(module_pool, val);
</del><ins>+                profile-&gt;context = switch_core_strdup(profile-&gt;pool, val);
</ins><span class="cx">         } else if (!strcasecmp(var, &quot;date-format&quot;)) {
</span><span class="cx">                 strncpy(profile-&gt;date_format, val, 6);
</span><span class="cx">         } else if (!strcasecmp(var, &quot;odbc-dsn&quot;) &amp;&amp; !zstr(val)) {
</span><span class="cx">                 if (switch_odbc_available()) {
</span><del>-                        profile-&gt;odbc_dsn = switch_core_strdup(module_pool, val);
</del><ins>+                        profile-&gt;odbc_dsn = switch_core_strdup(profile-&gt;pool, val);
</ins><span class="cx">                         if ((profile-&gt;odbc_user = strchr(profile-&gt;odbc_dsn, ':'))) {
</span><span class="cx">                                 *profile-&gt;odbc_user++ = '\0';
</span><span class="cx">                                 if ((profile-&gt;odbc_pass = strchr(profile-&gt;odbc_user, ':'))) {
</span><span class="lines">@@ -1382,18 +1445,12 @@
</span><span class="cx">         char *cf = &quot;skinny.conf&quot;;
</span><span class="cx">         switch_xml_t xcfg, xml, xprofiles, xprofile;
</span><span class="cx"> 
</span><del>-        memset(&amp;globals, 0, sizeof(globals));
-        globals.running = 1;
-
-        switch_core_hash_init(&amp;globals.profile_hash, module_pool);
-
-        switch_mutex_init(&amp;globals.calls_mutex, SWITCH_MUTEX_NESTED, module_pool);
-        
</del><span class="cx">         if (!(xml = switch_xml_open_cfg(cf, &amp;xcfg, NULL))) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Open of %s failed\n&quot;, cf);
</span><span class="cx">                 return SWITCH_STATUS_TERM;
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        switch_mutex_lock(globals.mutex);
</ins><span class="cx">         if ((xprofiles = switch_xml_child(xcfg, &quot;profiles&quot;))) {
</span><span class="cx">                 for (xprofile = switch_xml_child(xprofiles, &quot;profile&quot;); xprofile; xprofile = xprofile-&gt;next) {
</span><span class="cx">                         char *profile_name = (char *) switch_xml_attr_soft(xprofile, &quot;name&quot;);
</span><span class="lines">@@ -1404,13 +1461,22 @@
</span><span class="cx">                                 continue;
</span><span class="cx">                         }
</span><span class="cx">                         if (xsettings) {
</span><ins>+                            switch_memory_pool_t *profile_pool = NULL;
</ins><span class="cx">                                 char dbname[256];
</span><span class="cx">                                 switch_core_db_t *db;
</span><span class="cx">                                 skinny_profile_t *profile = NULL;
</span><span class="cx">                                 switch_xml_t param;
</span><span class="cx">                                 
</span><del>-                                profile = switch_core_alloc(module_pool, sizeof(skinny_profile_t));
</del><ins>+                    if (switch_core_new_memory_pool(&amp;profile_pool) != SWITCH_STATUS_SUCCESS) {
+                            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;OH OH no pool\n&quot;);
+                            return SWITCH_STATUS_TERM;
+                    }
+                                profile = switch_core_alloc(profile_pool, sizeof(skinny_profile_t));
+                                profile-&gt;pool = profile_pool;
</ins><span class="cx">                                 profile-&gt;name = profile_name;
</span><ins>+                        switch_mutex_init(&amp;profile-&gt;listener_mutex, SWITCH_MUTEX_NESTED, profile-&gt;pool);
+                        switch_mutex_init(&amp;profile-&gt;sql_mutex, SWITCH_MUTEX_NESTED, profile-&gt;pool);
+                        switch_mutex_init(&amp;profile-&gt;sock_mutex, SWITCH_MUTEX_NESTED, profile-&gt;pool);
</ins><span class="cx">                                 
</span><span class="cx">                                 for (param = switch_xml_child(xsettings, &quot;param&quot;); param; param = param-&gt;next) {
</span><span class="cx">                                         char *var = (char *) switch_xml_attr_soft(param, &quot;name&quot;);
</span><span class="lines">@@ -1450,7 +1516,7 @@
</span><span class="cx">                                 }
</span><span class="cx"> 
</span><span class="cx">                                 switch_snprintf(dbname, sizeof(dbname), &quot;skinny_%s&quot;, profile-&gt;name);
</span><del>-                                profile-&gt;dbname = switch_core_strdup(module_pool, dbname);
</del><ins>+                                profile-&gt;dbname = switch_core_strdup(profile-&gt;pool, dbname);
</ins><span class="cx"> 
</span><span class="cx">                                 if (switch_odbc_available() &amp;&amp; profile-&gt;odbc_dsn) {
</span><span class="cx">                                         if (!(profile-&gt;master_odbc = switch_odbc_handle_new(profile-&gt;odbc_dsn, profile-&gt;odbc_user, profile-&gt;odbc_pass))) {
</span><span class="lines">@@ -1467,11 +1533,13 @@
</span><span class="cx">                                         switch_odbc_handle_exec(profile-&gt;master_odbc, devices_sql, NULL, NULL);
</span><span class="cx">                                         switch_odbc_handle_exec(profile-&gt;master_odbc, lines_sql, NULL, NULL);
</span><span class="cx">                                         switch_odbc_handle_exec(profile-&gt;master_odbc, buttons_sql, NULL, NULL);
</span><ins>+                                        switch_odbc_handle_exec(profile-&gt;master_odbc, active_lines_sql, NULL, NULL);
</ins><span class="cx">                                 } else {
</span><span class="cx">                                         if ((db = switch_core_db_open_file(profile-&gt;dbname))) {
</span><span class="cx">                                                 switch_core_db_test_reactive(db, &quot;SELECT * FROM skinny_devices&quot;, NULL, devices_sql);
</span><span class="cx">                                                 switch_core_db_test_reactive(db, &quot;SELECT * FROM skinny_lines&quot;, NULL, lines_sql);
</span><span class="cx">                                                 switch_core_db_test_reactive(db, &quot;SELECT * FROM skinny_buttons&quot;, NULL, buttons_sql);
</span><ins>+                                                switch_core_db_test_reactive(db, &quot;SELECT * FROM skinny_active_lines&quot;, NULL, active_lines_sql);
</ins><span class="cx">                                         } else {
</span><span class="cx">                                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Cannot Open SQL Database!\n&quot;);
</span><span class="cx">                                                 continue;
</span><span class="lines">@@ -1479,11 +1547,14 @@
</span><span class="cx">                                         switch_core_db_close(db);
</span><span class="cx">                                 }
</span><span class="cx">                                 
</span><del>-                                skinny_execute_sql_callback(profile, profile-&gt;listener_mutex, &quot;DELETE FROM skinny_devices&quot;, NULL, NULL);
-                                skinny_execute_sql_callback(profile, profile-&gt;listener_mutex, &quot;DELETE FROM skinny_lines&quot;, NULL, NULL);
-                                skinny_execute_sql_callback(profile, profile-&gt;listener_mutex, &quot;DELETE FROM skinny_buttons&quot;, NULL, NULL);
</del><ins>+                                skinny_execute_sql_callback(profile, profile-&gt;sql_mutex, &quot;DELETE FROM skinny_devices&quot;, NULL, NULL);
+                                skinny_execute_sql_callback(profile, profile-&gt;sql_mutex, &quot;DELETE FROM skinny_lines&quot;, NULL, NULL);
+                                skinny_execute_sql_callback(profile, profile-&gt;sql_mutex, &quot;DELETE FROM skinny_buttons&quot;, NULL, NULL);
+                                skinny_execute_sql_callback(profile, profile-&gt;sql_mutex, &quot;DELETE FROM skinny_active_lines&quot;, NULL, NULL);
</ins><span class="cx"> 
</span><del>-                                switch_core_hash_insert(globals.profile_hash, profile-&gt;name, profile);
</del><ins>+                    switch_mutex_lock(globals.mutex);
+                    switch_core_hash_insert(globals.profile_hash, profile-&gt;name, profile);
+                    switch_mutex_unlock(globals.mutex);
</ins><span class="cx">                                 profile = NULL;
</span><span class="cx">                         } else {
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
</span><span class="lines">@@ -1492,506 +1563,174 @@
</span><span class="cx">                 } /* profile */
</span><span class="cx">         }
</span><span class="cx">         switch_xml_free(xml);
</span><ins>+        switch_mutex_unlock(globals.mutex);
</ins><span class="cx"> 
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static switch_status_t cmd_status_profile(const char *profile_name, switch_stream_handle_t *stream)
-{
-        skinny_profile_t *profile;
-        if ((profile = skinny_find_profile(profile_name))) {
-                dump_profile(profile, stream);
-        } else {
-                stream-&gt;write_function(stream, &quot;Profile not found!\n&quot;);
-        }
-
-        return SWITCH_STATUS_SUCCESS;
-}
-
-static switch_status_t cmd_status_profile_device(const char *profile_name, const char *device_name, switch_stream_handle_t *stream)
-{
-        skinny_profile_t *profile;
-        if ((profile = skinny_find_profile(profile_name))) {
-                dump_device(profile, device_name, stream);
-        } else {
-                stream-&gt;write_function(stream, &quot;Profile not found!\n&quot;);
-        }
-
-        return SWITCH_STATUS_SUCCESS;
-}
-
-static switch_status_t cmd_profile_device_send_ringer_message(const char *profile_name, const char *device_name, const char *ring_type, const char *ring_mode, switch_stream_handle_t *stream)
-{
-        skinny_profile_t *profile;
-
-        if ((profile = skinny_find_profile(profile_name))) {
-                listener_t *listener = NULL;
-                skinny_profile_find_listener_by_device_name(profile, device_name, &amp;listener);
-                if(listener) {
-                        set_ringer(listener, skinny_str2ring_type(ring_type), skinny_str2ring_mode(ring_mode), 0);
-                } else {
-                        stream-&gt;write_function(stream, &quot;Listener not found!\n&quot;);
-                }
-        } else {
-                stream-&gt;write_function(stream, &quot;Profile not found!\n&quot;);
-        }
-
-        return SWITCH_STATUS_SUCCESS;
-}
-
-static switch_status_t cmd_profile_device_send_lamp_message(const char *profile_name, const char *device_name, const char *stimulus, const char *instance, const char *lamp_mode, switch_stream_handle_t *stream)
-{
-        skinny_profile_t *profile;
-
-        if ((profile = skinny_find_profile(profile_name))) {
-                listener_t *listener = NULL;
-                skinny_profile_find_listener_by_device_name(profile, device_name, &amp;listener);
-                if(listener) {
-                        set_lamp(listener, skinny_str2button(stimulus), atoi(instance), skinny_str2lamp_mode(lamp_mode));
-                } else {
-                        stream-&gt;write_function(stream, &quot;Listener not found!\n&quot;);
-                }
-        } else {
-                stream-&gt;write_function(stream, &quot;Profile not found!\n&quot;);
-        }
-
-        return SWITCH_STATUS_SUCCESS;
-}
-
-static switch_status_t cmd_profile_device_send_speaker_mode_message(const char *profile_name, const char *device_name, const char *speaker_mode, switch_stream_handle_t *stream)
-{
-        skinny_profile_t *profile;
-
-        if ((profile = skinny_find_profile(profile_name))) {
-                listener_t *listener = NULL;
-                skinny_profile_find_listener_by_device_name(profile, device_name, &amp;listener);
-                if(listener) {
-                        set_speaker_mode(listener, skinny_str2speaker_mode(speaker_mode));
-                } else {
-                        stream-&gt;write_function(stream, &quot;Listener not found!\n&quot;);
-                }
-        } else {
-                stream-&gt;write_function(stream, &quot;Profile not found!\n&quot;);
-        }
-
-        return SWITCH_STATUS_SUCCESS;
-}
-
-static switch_status_t cmd_profile_device_send_call_state_message(const char *profile_name, const char *device_name, const char *call_state, const char *line_instance, const char *call_id, switch_stream_handle_t *stream)
-{
-        skinny_profile_t *profile;
-
-        if ((profile = skinny_find_profile(profile_name))) {
-                listener_t *listener = NULL;
-                skinny_profile_find_listener_by_device_name(profile, device_name, &amp;listener);
-                if(listener) {
-                        send_call_state(listener, skinny_str2call_state(call_state), atoi(line_instance), atoi(call_id));
-                } else {
-                        stream-&gt;write_function(stream, &quot;Listener not found!\n&quot;);
-                }
-        } else {
-                stream-&gt;write_function(stream, &quot;Profile not found!\n&quot;);
-        }
-
-        return SWITCH_STATUS_SUCCESS;
-}
-
-static switch_status_t cmd_profile_device_send_reset_message(const char *profile_name, const char *device_name, const char *reset_type, switch_stream_handle_t *stream)
-{
-        skinny_profile_t *profile;
-
-        if ((profile = skinny_find_profile(profile_name))) {
-                listener_t *listener = NULL;
-                skinny_profile_find_listener_by_device_name(profile, device_name, &amp;listener);
-                if(listener) {
-                        send_reset(listener, skinny_str2device_reset_type(reset_type));
-                } else {
-                        stream-&gt;write_function(stream, &quot;Listener not found!\n&quot;);
-                }
-        } else {
-                stream-&gt;write_function(stream, &quot;Profile not found!\n&quot;);
-        }
-
-        return SWITCH_STATUS_SUCCESS;
-}
-
-SWITCH_STANDARD_API(skinny_function)
-{
-        char *argv[1024] = { 0 };
-        int argc = 0;
-        char *mycmd = NULL;
-        switch_status_t status = SWITCH_STATUS_SUCCESS;
-        const char *usage_string = &quot;USAGE:\n&quot;
-                &quot;--------------------------------------------------------------------------------\n&quot;
-                &quot;skinny help\n&quot;
-                &quot;skinny status profile &lt;profile_name&gt;\n&quot;
-                &quot;skinny status profile &lt;profile_name&gt; device &lt;device_name&gt;\n&quot;
-                &quot;skinny profile &lt;profile_name&gt; device &lt;device_name&gt; send ResetMessage [DeviceReset|DeviceRestart]\n&quot;
-                &quot;skinny profile &lt;profile_name&gt; device &lt;device_name&gt; send SetRingerMessage &lt;ring_type&gt; &lt;ring_mode&gt;\n&quot;
-                &quot;skinny profile &lt;profile_name&gt; device &lt;device_name&gt; send SetLampMessage &lt;stimulus&gt; &lt;instance&gt; &lt;lamp_mode&gt;\n&quot;
-                &quot;skinny profile &lt;profile_name&gt; device &lt;device_name&gt; send SetSpeakerModeMessage &lt;speaker_mode&gt;\n&quot;
-                &quot;skinny profile &lt;profile_name&gt; device &lt;device_name&gt; send CallStateMessage &lt;call_state&gt; &lt;line_instance&gt; &lt;call_id&gt;\n&quot;
-                &quot;--------------------------------------------------------------------------------\n&quot;;
-        if (session) {
-                return SWITCH_STATUS_FALSE;
-        }
-
-        if (zstr(cmd)) {
-                stream-&gt;write_function(stream, &quot;%s&quot;, usage_string);
-                goto done;
-        }
-
-        if (!(mycmd = strdup(cmd))) {
-                status = SWITCH_STATUS_MEMERR;
-                goto done;
-        }
-
-        if (!(argc = switch_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) || !argv[0]) {
-                stream-&gt;write_function(stream, &quot;%s&quot;, usage_string);
-                goto done;
-        }
-
-        if (!strcasecmp(argv[0], &quot;help&quot;)) {/* skinny help */
-                stream-&gt;write_function(stream, &quot;%s&quot;, usage_string);
-                goto done;
-        } else if (argc == 3 &amp;&amp; !strcasecmp(argv[0], &quot;status&quot;) &amp;&amp; !strcasecmp(argv[1], &quot;profile&quot;)) {
-                /* skinny status profile &lt;profile_name&gt; */
-                status = cmd_status_profile(argv[2], stream);
-        } else if (argc == 5 &amp;&amp; !strcasecmp(argv[0], &quot;status&quot;) &amp;&amp; !strcasecmp(argv[1], &quot;profile&quot;) &amp;&amp; !strcasecmp(argv[3], &quot;device&quot;)) {
-                /* skinny status profile &lt;profile_name&gt; device &lt;device_name&gt; */
-                status = cmd_status_profile_device(argv[2], argv[4], stream);
-        } else if (argc &gt;= 6 &amp;&amp; !strcasecmp(argv[0], &quot;profile&quot;) &amp;&amp; !strcasecmp(argv[2], &quot;device&quot;) &amp;&amp; !strcasecmp(argv[4], &quot;send&quot;)) {
-                /* skinny profile &lt;profile_name&gt; device &lt;device_name&gt; send ... */
-                switch(skinny_str2message_type(argv[5])) {
-                        case SET_RINGER_MESSAGE:
-                                if(argc == 8) {
-                                        /* SetRingerMessage &lt;ring_type&gt; &lt;ring_mode&gt; */
-                                        status = cmd_profile_device_send_ringer_message(argv[1], argv[3], argv[6], argv[7], stream);
-                                }
-                                break;
-                        case SET_LAMP_MESSAGE:
-                                if (argc == 9) {
-                                        /* SetLampMessage &lt;stimulus&gt; &lt;instance&gt; &lt;lamp_mode&gt; */
-                                        status = cmd_profile_device_send_lamp_message(argv[1], argv[3], argv[6], argv[7], argv[8], stream);
-                                }
-                                break;
-                        case SET_SPEAKER_MODE_MESSAGE:
-                                if (argc == 7) {
-                                        /* SetSpeakerModeMessage &lt;speaker_mode&gt; */
-                                        status = cmd_profile_device_send_speaker_mode_message(argv[1], argv[3], argv[6], stream);
-                                }
-                                break;
-                        case CALL_STATE_MESSAGE:
-                                if (argc == 9) {
-                                        /* CallStateMessage &lt;call_state&gt; &lt;line_instance&gt; &lt;call_id&gt; */
-                                        status = cmd_profile_device_send_call_state_message(argv[1], argv[3], argv[6], argv[7], argv[8], stream);
-                                }
-                                break;
-                        case RESET_MESSAGE:
-                                if (argc == 7) {
-                                        /* ResetMessage &lt;reset_type&gt; */
-                                        status = cmd_profile_device_send_reset_message(argv[1], argv[3], argv[6], stream);
-                                }
-                                break;
-                        default:
-                                stream-&gt;write_function(stream, &quot;Unhandled message %s\n&quot;, argv[5]);
-                }
-        } else {
-                stream-&gt;write_function(stream, &quot;Unknown Command [%s]\n&quot;, argv[0]);
-        }
-
-done:
-        switch_safe_free(mycmd);
-        return status;
-}
-
</del><span class="cx"> static void event_handler(switch_event_t *event)
</span><span class="cx"> {
</span><ins>+        char *subclass;
+
</ins><span class="cx">         if (event-&gt;event_id == SWITCH_EVENT_HEARTBEAT) {
</span><span class="cx">                 walk_listeners(kill_expired_listener, NULL);
</span><del>-        }
-}
</del><ins>+        } else if ((subclass = switch_event_get_header_nil(event, &quot;Event-Subclass&quot;)) &amp;&amp; !strcasecmp(subclass, SKINNY_EVENT_CALL_STATE)) {
+            char *profile_name = switch_event_get_header_nil(event, &quot;Skinny-Profile-Name&quot;);
+            char *device_name = switch_event_get_header_nil(event, &quot;Skinny-Device-Name&quot;);
+            uint32_t device_instance = atoi(switch_event_get_header_nil(event, &quot;Skinny-Device-Instance&quot;));
+                uint32_t line_instance = atoi(switch_event_get_header_nil(event, &quot;Skinny-Line-Instance&quot;));
+                uint32_t call_id = atoi(switch_event_get_header_nil(event, &quot;Skinny-Call-Id&quot;));
+                uint32_t call_state = atoi(switch_event_get_header_nil(event, &quot;Skinny-Call-State&quot;));
+            skinny_profile_t *profile;
+            listener_t *listener = NULL;
+        char *line_instance_condition, *call_id_condition;
+        char *sql;
</ins><span class="cx"> 
</span><del>-static switch_status_t skinny_list_profiles(const char *line, const char *cursor, switch_console_callback_match_t **matches)
-{
-        switch_console_callback_match_t *my_matches = NULL;
-        switch_status_t status = SWITCH_STATUS_FALSE;
-        switch_hash_index_t *hi;
-        void *val;
-        skinny_profile_t *profile;
-        
-        /* walk profiles */
-        for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
-                switch_hash_this(hi, NULL, NULL, &amp;val);
-                profile = (skinny_profile_t *) val;
</del><ins>+            if ((profile = skinny_find_profile(profile_name))) {
+                skinny_profile_find_listener_by_device_name_and_instance(profile, device_name, device_instance, &amp;listener);
+            if(listener) {
+                if(line_instance &gt; 0) {
+                    line_instance_condition = switch_mprintf(&quot;line_instance=%d&quot;, line_instance);
+                } else {
+                    line_instance_condition = switch_mprintf(&quot;1=1&quot;);
+                }
+                switch_assert(line_instance_condition);
+                if(call_id &gt; 0) {
+                    call_id_condition = switch_mprintf(&quot;call_id=%d&quot;, call_id);
+                } else {
+                    call_id_condition = switch_mprintf(&quot;1=1&quot;);
+                }
+                switch_assert(call_id_condition);
</ins><span class="cx"> 
</span><del>-                switch_console_push_match(&amp;my_matches, profile-&gt;name);
</del><ins>+                    if ((sql = switch_mprintf(
+                                    &quot;UPDATE skinny_active_lines &quot;
+                                    &quot;SET call_state=%d &quot;
+                                    &quot;WHERE device_name='%s' AND device_instance=%d &quot;
+                                    &quot;AND %s AND %s&quot;,
+                                    call_state,
+                                    listener-&gt;device_name, listener-&gt;device_instance,
+                                    line_instance_condition, call_id_condition
+                                    ))) {
+                            skinny_execute_sql(listener-&gt;profile, sql, listener-&gt;profile-&gt;sql_mutex);
+                            switch_safe_free(sql);
+                            send_call_state(listener, call_state, line_instance, call_id);
+                    }
+                    switch_safe_free(line_instance_condition);
+                    switch_safe_free(call_id_condition);
+                }
+            }
</ins><span class="cx">         }
</span><del>-        
-        if (my_matches) {
-                *matches = my_matches;
-                status = SWITCH_STATUS_SUCCESS;
-        }
-        
-        return status;
</del><span class="cx"> }
</span><span class="cx"> 
</span><del>-struct match_helper {
-        switch_console_callback_match_t *my_matches;
-};
</del><span class="cx"> 
</span><del>-static int skinny_list_devices_callback(void *pArg, int argc, char **argv, char **columnNames)
-{
-        struct match_helper *h = (struct match_helper *) pArg;
-        char *device_name = argv[0];
-
-        switch_console_push_match(&amp;h-&gt;my_matches, device_name);
-        return 0;
-}
-
-static switch_status_t skinny_list_devices(const char *line, const char *cursor, switch_console_callback_match_t **matches)
-{
-        struct match_helper h = { 0 };
-        switch_status_t status = SWITCH_STATUS_FALSE;
-        skinny_profile_t *profile = NULL;
-        char *sql;
-
-        char *myline;
-        char *argv[1024] = { 0 };
-        int argc = 0;
-
-        if (!(myline = strdup(line))) {
-                status = SWITCH_STATUS_MEMERR;
-                return status;
-        }
-        if (!(argc = switch_separate_string(myline, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) || argc != 5) {
-                return status;
-        }
-
-        if(!strcasecmp(argv[1], &quot;profile&quot;)) {/* skinny profile &lt;profile_name&gt; ... */
-                profile = skinny_find_profile(argv[2]);
-        } else if(!strcasecmp(argv[2], &quot;profile&quot;)) {/* skinny status profile &lt;profile_name&gt; ... */
-                profile = skinny_find_profile(argv[3]);
-        }
-
-        if(profile) {
-                if ((sql = switch_mprintf(&quot;SELECT name FROM skinny_devices&quot;))) {
-                        skinny_execute_sql_callback(profile, profile-&gt;listener_mutex, sql, skinny_list_devices_callback, &amp;h);
-                        switch_safe_free(sql);
-                }
-        }
-        
-        if (h.my_matches) {
-                *matches = h.my_matches;
-                status = SWITCH_STATUS_SUCCESS;
-        }
-        
-        return status;
-}
-
-static switch_status_t skinny_list_reset_types(const char *line, const char *cursor, switch_console_callback_match_t **matches)
-{
-        switch_status_t status = SWITCH_STATUS_FALSE;
-        SKINNY_PUSH_DEVICE_RESET_TYPES
-        return status;
-}
-
-static switch_status_t skinny_list_stimuli(const char *line, const char *cursor, switch_console_callback_match_t **matches)
-{
-        switch_status_t status = SWITCH_STATUS_FALSE;
-        SKINNY_PUSH_STIMULI
-        return status;
-}
-
-static switch_status_t skinny_list_ring_types(const char *line, const char *cursor, switch_console_callback_match_t **matches)
-{
-        switch_status_t status = SWITCH_STATUS_FALSE;
-        SKINNY_PUSH_RING_TYPES
-        return status;
-}
-
-static switch_status_t skinny_list_ring_modes(const char *line, const char *cursor, switch_console_callback_match_t **matches)
-{
-        switch_status_t status = SWITCH_STATUS_FALSE;
-        SKINNY_PUSH_RING_MODES
-        return status;
-}
-
-static switch_status_t skinny_list_stimulus_instances(const char *line, const char *cursor, switch_console_callback_match_t **matches)
-{
-        switch_status_t status = SWITCH_STATUS_FALSE;
-        switch_console_callback_match_t *my_matches = NULL;
-        
-        switch_console_push_match(&amp;my_matches, &quot;&lt;stimulus_instance&gt;&quot;);
-        switch_console_push_match(&amp;my_matches, &quot;0&quot;);
-        
-        if (my_matches) {
-                *matches = my_matches;
-                status = SWITCH_STATUS_SUCCESS;
-        }
-        return status;
-}
-
-static switch_status_t skinny_list_stimulus_modes(const char *line, const char *cursor, switch_console_callback_match_t **matches)
-{
-        switch_status_t status = SWITCH_STATUS_FALSE;
-        SKINNY_PUSH_LAMP_MODES
-        return status;
-}
-
-static switch_status_t skinny_list_speaker_modes(const char *line, const char *cursor, switch_console_callback_match_t **matches)
-{
-        switch_status_t status = SWITCH_STATUS_FALSE;
-        SKINNY_PUSH_SPEAKER_MODES
-        return status;
-}
-
-static switch_status_t skinny_list_call_states(const char *line, const char *cursor, switch_console_callback_match_t **matches)
-{
-        switch_status_t status = SWITCH_STATUS_FALSE;
-        SKINNY_PUSH_CALL_STATES
-        return status;
-}
-
-static switch_status_t skinny_list_line_instances(const char *line, const char *cursor, switch_console_callback_match_t **matches)
-{
-        switch_status_t status = SWITCH_STATUS_FALSE;
-        switch_console_callback_match_t *my_matches = NULL;
-        
-        /* TODO */
-        switch_console_push_match(&amp;my_matches, &quot;1&quot;);
-        switch_console_push_match(&amp;my_matches, &quot;&lt;line_instance&gt;&quot;);
-        
-        if (my_matches) {
-                *matches = my_matches;
-                status = SWITCH_STATUS_SUCCESS;
-        }
-        return status;
-}
-
-static switch_status_t skinny_list_call_ids(const char *line, const char *cursor, switch_console_callback_match_t **matches)
-{
-        switch_status_t status = SWITCH_STATUS_FALSE;
-        switch_console_callback_match_t *my_matches = NULL;
-        
-        /* TODO */
-        switch_console_push_match(&amp;my_matches, &quot;1345&quot;);
-        switch_console_push_match(&amp;my_matches, &quot;&lt;call_id&gt;&quot;);
-        
-        if (my_matches) {
-                *matches = my_matches;
-                status = SWITCH_STATUS_SUCCESS;
-        }
-        return status;
-}
-
</del><span class="cx"> /*****************************************************************************/
</span><span class="cx"> SWITCH_MODULE_LOAD_FUNCTION(mod_skinny_load)
</span><span class="cx"> {
</span><span class="cx">         switch_hash_index_t *hi;
</span><del>-        void *val;
-        skinny_profile_t *profile;
</del><ins>+    /* globals init */
+        memset(&amp;globals, 0, sizeof(globals));
</ins><span class="cx"> 
</span><del>-        switch_api_interface_t *api_interface;
-
-        module_pool = pool;
-
</del><ins>+    if (switch_core_new_memory_pool(&amp;globals.pool) != SWITCH_STATUS_SUCCESS) {
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;OH OH no pool\n&quot;);
+        return SWITCH_STATUS_TERM;
+    }
+        switch_mutex_init(&amp;globals.mutex, SWITCH_MUTEX_NESTED, globals.pool);
+        switch_core_hash_init(&amp;globals.profile_hash, globals.pool);
+        globals.running = 1;
+        
</ins><span class="cx">         load_skinny_config();
</span><span class="cx"> 
</span><del>-        /* init listeners */
-        for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
-                switch_hash_this(hi, NULL, NULL, &amp;val);
-                profile = (skinny_profile_t *) val;
-        
-                switch_mutex_init(&amp;profile-&gt;listener_mutex, SWITCH_MUTEX_NESTED, module_pool);
-                switch_mutex_init(&amp;profile-&gt;sock_mutex, SWITCH_MUTEX_NESTED, module_pool);
-
-        }
-
</del><ins>+    /* bind to events */
</ins><span class="cx">         if ((switch_event_bind_removable(modname, SWITCH_EVENT_HEARTBEAT, NULL, event_handler, NULL, &amp;globals.heartbeat_node) != SWITCH_STATUS_SUCCESS)) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Couldn't bind our heartbeat handler!\n&quot;);
</span><span class="cx">                 /* Not such severe to prevent loading */
</span><span class="cx">         }
</span><ins>+        if ((switch_event_bind_removable(modname, SWITCH_EVENT_CUSTOM, SKINNY_EVENT_CALL_STATE, event_handler, NULL, &amp;globals.call_state_node) != SWITCH_STATUS_SUCCESS)) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Couldn't bind our call_state handler!\n&quot;);
+                return SWITCH_STATUS_TERM;
+        }
</ins><span class="cx"> 
</span><ins>+    /* reserve events */
</ins><span class="cx">         if (switch_event_reserve_subclass(SKINNY_EVENT_REGISTER) != SWITCH_STATUS_SUCCESS) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Couldn't register subclass %s!\n&quot;, SKINNY_EVENT_REGISTER);
</span><span class="cx">                 return SWITCH_STATUS_TERM;
</span><span class="cx">         }
</span><ins>+        if (switch_event_reserve_subclass(SKINNY_EVENT_UNREGISTER) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Couldn't register subclass %s!\n&quot;, SKINNY_EVENT_UNREGISTER);
+                return SWITCH_STATUS_TERM;
+        }
+        if (switch_event_reserve_subclass(SKINNY_EVENT_EXPIRE) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Couldn't register subclass %s!\n&quot;, SKINNY_EVENT_EXPIRE);
+                return SWITCH_STATUS_TERM;
+        }
+        if (switch_event_reserve_subclass(SKINNY_EVENT_ALARM) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Couldn't register subclass %s!\n&quot;, SKINNY_EVENT_ALARM);
+                return SWITCH_STATUS_TERM;
+        }
+        if (switch_event_reserve_subclass(SKINNY_EVENT_CALL_STATE) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Couldn't register subclass %s!\n&quot;, SKINNY_EVENT_CALL_STATE);
+                return SWITCH_STATUS_TERM;
+        }
</ins><span class="cx"> 
</span><span class="cx">         /* connect my internal structure to the blank pointer passed to me */
</span><del>-        *module_interface = switch_loadable_module_create_module_interface(pool, modname);
</del><ins>+        *module_interface = switch_loadable_module_create_module_interface(globals.pool, modname);
</ins><span class="cx">         skinny_endpoint_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_ENDPOINT_INTERFACE);
</span><span class="cx">         skinny_endpoint_interface-&gt;interface_name = &quot;skinny&quot;;
</span><span class="cx">         skinny_endpoint_interface-&gt;io_routines = &amp;skinny_io_routines;
</span><span class="cx">         skinny_endpoint_interface-&gt;state_handler = &amp;skinny_state_handlers;
</span><span class="cx"> 
</span><ins>+    skinny_api_register(module_interface);
</ins><span class="cx"> 
</span><del>-        SWITCH_ADD_API(api_interface, &quot;skinny&quot;, &quot;Skinny Controls&quot;, skinny_function, &quot;&lt;cmd&gt; &lt;args&gt;&quot;);
-        switch_console_set_complete(&quot;add skinny help&quot;);
-
-        switch_console_set_complete(&quot;add skinny status profile ::skinny::list_profiles&quot;);
-        switch_console_set_complete(&quot;add skinny status profile ::skinny::list_profiles device ::skinny::list_devices&quot;);
-
-        switch_console_set_complete(&quot;add skinny profile ::skinny::list_profiles device ::skinny::list_devices send ResetMessage ::skinny::list_reset_types&quot;);
-        switch_console_set_complete(&quot;add skinny profile ::skinny::list_profiles device ::skinny::list_devices send SetRingerMessage ::skinny::list_ring_types ::skinny::list_ring_modes&quot;);
-        switch_console_set_complete(&quot;add skinny profile ::skinny::list_profiles device ::skinny::list_devices send SetLampMessage ::skinny::list_stimuli ::skinny::list_stimulus_instances ::skinny::list_stimulus_modes&quot;);
-        switch_console_set_complete(&quot;add skinny profile ::skinny::list_profiles device ::skinny::list_devices send SetSpeakerModeMessage ::skinny::list_speaker_modes&quot;);
-        switch_console_set_complete(&quot;add skinny profile ::skinny::list_profiles device ::skinny::list_devices send CallStateMessage ::skinny::list_call_states ::skinny::list_line_instances ::skinny::list_call_ids&quot;);
-
-        switch_console_add_complete_func(&quot;::skinny::list_profiles&quot;, skinny_list_profiles);
-        switch_console_add_complete_func(&quot;::skinny::list_devices&quot;, skinny_list_devices);
-        switch_console_add_complete_func(&quot;::skinny::list_reset_types&quot;, skinny_list_reset_types);
-        switch_console_add_complete_func(&quot;::skinny::list_ring_types&quot;, skinny_list_ring_types);
-        switch_console_add_complete_func(&quot;::skinny::list_ring_modes&quot;, skinny_list_ring_modes);
-        switch_console_add_complete_func(&quot;::skinny::list_stimuli&quot;, skinny_list_stimuli);
-        switch_console_add_complete_func(&quot;::skinny::list_stimulus_instances&quot;, skinny_list_stimulus_instances);
-        switch_console_add_complete_func(&quot;::skinny::list_stimulus_modes&quot;, skinny_list_stimulus_modes);
-        switch_console_add_complete_func(&quot;::skinny::list_speaker_modes&quot;, skinny_list_speaker_modes);
-        switch_console_add_complete_func(&quot;::skinny::list_call_states&quot;, skinny_list_call_states);
-        switch_console_add_complete_func(&quot;::skinny::list_line_instances&quot;, skinny_list_line_instances);
-        switch_console_add_complete_func(&quot;::skinny::list_call_ids&quot;, skinny_list_call_ids);
-
-        /* indicate that the module should continue to be loaded */
-        return SWITCH_STATUS_SUCCESS;
-}
-
-SWITCH_MODULE_RUNTIME_FUNCTION(mod_skinny_runtime)
-{
-        switch_status_t status = SWITCH_STATUS_SUCCESS;
-        switch_hash_index_t *hi;
-        void *val;
-        skinny_profile_t *profile;
-        
</del><span class="cx">         /* launch listeners */
</span><ins>+        switch_mutex_lock(globals.mutex);
</ins><span class="cx">         for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
</span><ins>+            void *val;
+            skinny_profile_t *profile;
+            switch_thread_t *thread;
+            switch_threadattr_t *thd_attr = NULL;
+
</ins><span class="cx">                 switch_hash_this(hi, NULL, NULL, &amp;val);
</span><span class="cx">                 profile = (skinny_profile_t *) val;
</span><span class="cx">         
</span><del>-                status = skinny_socket_create_and_bind(profile);
-                if(status != SWITCH_STATUS_SUCCESS) {
-                        return status;
-                }
</del><ins>+            switch_threadattr_create(&amp;thd_attr, profile-&gt;pool);
+            switch_threadattr_detach_set(thd_attr, 1);
+            switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
+            switch_thread_create(&amp;thread, thd_attr, skinny_profile_run, profile, profile-&gt;pool);
</ins><span class="cx">         }
</span><del>-        return status;
</del><ins>+        switch_mutex_unlock(globals.mutex);
+
+        /* indicate that the module should continue to be loaded */
+        return SWITCH_STATUS_SUCCESS;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_skinny_shutdown)
</span><span class="cx"> {
</span><span class="cx">         switch_hash_index_t *hi;
</span><span class="cx">         void *val;
</span><del>-        skinny_profile_t *profile;
</del><ins>+        switch_memory_pool_t *pool = globals.pool;
+        switch_mutex_t *mutex = globals.mutex;
</ins><span class="cx">         int sanity = 0;
</span><span class="cx"> 
</span><del>-        switch_event_free_subclass(SKINNY_EVENT_REGISTER);
</del><ins>+    /* release events */
</ins><span class="cx">         switch_event_unbind(&amp;globals.heartbeat_node);
</span><ins>+        switch_event_unbind(&amp;globals.call_state_node);
+        switch_event_free_subclass(SKINNY_EVENT_REGISTER);
+        switch_event_free_subclass(SKINNY_EVENT_UNREGISTER);
+        switch_event_free_subclass(SKINNY_EVENT_EXPIRE);
+        switch_event_free_subclass(SKINNY_EVENT_ALARM);
+        switch_event_free_subclass(SKINNY_EVENT_CALL_STATE);
</ins><span class="cx"> 
</span><ins>+        switch_mutex_lock(mutex);
+
</ins><span class="cx">         globals.running = 0;
</span><span class="cx"> 
</span><span class="cx">         /* kill listeners */
</span><span class="cx">         walk_listeners(kill_listener, NULL);
</span><span class="cx"> 
</span><span class="cx">         /* close sockets */
</span><ins>+        switch_mutex_lock(globals.mutex);
</ins><span class="cx">         for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
</span><ins>+            skinny_profile_t *profile;
</ins><span class="cx">                 switch_hash_this(hi, NULL, NULL, &amp;val);
</span><span class="cx">                 profile = (skinny_profile_t *) val;
</span><span class="cx"> 
</span><span class="lines">@@ -2004,8 +1743,14 @@
</span><span class="cx">                                 break;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><ins>+            switch_core_destroy_memory_pool(&amp;profile-&gt;pool);
</ins><span class="cx">         }
</span><ins>+        switch_mutex_unlock(globals.mutex);
</ins><span class="cx"> 
</span><ins>+        switch_core_hash_destroy(&amp;globals.profile_hash);
+        memset(&amp;globals, 0, sizeof(globals));
+        switch_mutex_unlock(mutex);
+        switch_core_destroy_memory_pool(&amp;pool);
</ins><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_skinnymod_skinnyh"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.h (17171 => 17172)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.h        2010-04-01 20:16:27 UTC (rev 17171)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.h        2010-04-01 20:16:36 UTC (rev 17172)
</span><span class="lines">@@ -42,14 +42,15 @@
</span><span class="cx"> #define SKINNY_EVENT_UNREGISTER &quot;skinny::unregister&quot;
</span><span class="cx"> #define SKINNY_EVENT_EXPIRE &quot;skinny::expire&quot;
</span><span class="cx"> #define SKINNY_EVENT_ALARM &quot;skinny::alarm&quot;
</span><ins>+#define SKINNY_EVENT_CALL_STATE &quot;skinny::call_state&quot;
</ins><span class="cx"> 
</span><span class="cx"> struct skinny_globals {
</span><del>-        /* data */
-        int calls;
-        switch_mutex_t *calls_mutex;
</del><ins>+        int running;
+        switch_memory_pool_t *pool;
+        switch_mutex_t *mutex;
</ins><span class="cx">         switch_hash_t *profile_hash;
</span><span class="cx">         switch_event_node_t *heartbeat_node;
</span><del>-        int running;
</del><ins>+        switch_event_node_t *call_state_node;
</ins><span class="cx"> };
</span><span class="cx"> typedef struct skinny_globals skinny_globals_t;
</span><span class="cx"> 
</span><span class="lines">@@ -72,6 +73,7 @@
</span><span class="cx">         char *odbc_user;
</span><span class="cx">         char *odbc_pass;
</span><span class="cx">         switch_odbc_handle_t *master_odbc;
</span><ins>+        switch_mutex_t *sql_mutex;        
</ins><span class="cx">         /* stats */
</span><span class="cx">         uint32_t ib_calls;
</span><span class="cx">         uint32_t ob_calls;
</span><span class="lines">@@ -86,6 +88,8 @@
</span><span class="cx">         uint8_t listener_ready;
</span><span class="cx">         /* call id */
</span><span class="cx">         uint32_t next_call_id;
</span><ins>+        /* others */
+        switch_memory_pool_t *pool;
</ins><span class="cx"> };
</span><span class="cx"> typedef struct skinny_profile skinny_profile_t;
</span><span class="cx"> 
</span><span class="lines">@@ -103,8 +107,6 @@
</span><span class="cx">         skinny_profile_t *profile;
</span><span class="cx">         char device_name[16];
</span><span class="cx">         uint32_t device_instance;
</span><del>-        switch_core_session_t *session[SKINNY_MAX_LINES];
-        uint32_t line_state[SKINNY_MAX_LINES]; /* See enum skinny_key_set */
</del><span class="cx"> 
</span><span class="cx">         switch_socket_t *sock;
</span><span class="cx">         switch_memory_pool_t *pool;
</span><span class="lines">@@ -153,14 +155,11 @@
</span><span class="cx">         switch_mutex_t *mutex;
</span><span class="cx">         switch_mutex_t *flag_mutex;
</span><span class="cx">         /* identification */
</span><del>-        struct listener *listener;
-        uint32_t line;
</del><span class="cx">         uint32_t call_id;
</span><span class="cx">         uint32_t party_id;
</span><del>-        char *line_name;
-        char *line_shortname;
-        char *line_displayname;
-        char dest[10];
</del><ins>+
+        skinny_profile_t *profile;
+
</ins><span class="cx">         /* codec */
</span><span class="cx">         char *iananame;        
</span><span class="cx">         switch_codec_t read_codec;
</span><span class="lines">@@ -183,6 +182,17 @@
</span><span class="cx"> typedef struct private_object private_t;
</span><span class="cx"> 
</span><span class="cx"> /*****************************************************************************/
</span><ins>+/* PROFILES FUNCTIONS */
+/*****************************************************************************/
+skinny_profile_t *skinny_find_profile(const char *profile_name);
+switch_status_t skinny_profile_dump(const skinny_profile_t *profile, switch_stream_handle_t *stream);
+switch_status_t skinny_profile_find_listener_by_device_name(skinny_profile_t *profile, const char *device_name, listener_t **listener);
+switch_status_t skinny_profile_find_listener_by_device_name_and_instance(skinny_profile_t *profile, const char *device_name, uint32_t device_instance, listener_t **listener);
+char * skinny_profile_find_session_uuid(skinny_profile_t *profile, listener_t *listener, uint32_t *line_instance_p, uint32_t call_id);
+switch_core_session_t * skinny_profile_find_session(skinny_profile_t *profile, listener_t *listener, uint32_t *line_instance_p, uint32_t call_id);
+switch_status_t dump_device(skinny_profile_t *profile, const char *device_name, switch_stream_handle_t *stream);
+
+/*****************************************************************************/
</ins><span class="cx"> /* SQL FUNCTIONS */
</span><span class="cx"> /*****************************************************************************/
</span><span class="cx"> void skinny_execute_sql(skinny_profile_t *profile, char *sql, switch_mutex_t *mutex);
</span><span class="lines">@@ -192,18 +202,19 @@
</span><span class="cx"> /*****************************************************************************/
</span><span class="cx"> /* LISTENER FUNCTIONS */
</span><span class="cx"> /*****************************************************************************/
</span><ins>+uint8_t listener_is_ready(listener_t *listener);
</ins><span class="cx"> switch_status_t keepalive_listener(listener_t *listener, void *pvt);
</span><span class="cx"> 
</span><span class="cx"> /*****************************************************************************/
</span><span class="cx"> /* CHANNEL FUNCTIONS */
</span><span class="cx"> /*****************************************************************************/
</span><del>-uint32_t skinny_line_perform_set_state(listener_t *listener, const char *file, const char *func, int line, uint32_t instance, uint32_t state, uint32_t call_id);
-#define  skinny_line_set_state(listener, instance, state, call_id)  skinny_line_perform_set_state(listener, __FILE__, __SWITCH_FUNC__, __LINE__, instance, state, call_id)
</del><ins>+void skinny_line_perform_set_state(const char *file, const char *func, int line, listener_t *listener, uint32_t line_instance, uint32_t call_id, uint32_t call_state);
+#define  skinny_line_set_state(listener, line_instance, call_id, call_state)  skinny_line_perform_set_state(__FILE__, __SWITCH_FUNC__, __LINE__, listener, line_instance, call_id, call_state)
</ins><span class="cx"> 
</span><del>-uint32_t skinny_line_get_state(listener_t *listener, uint32_t instance);
</del><ins>+uint32_t skinny_line_get_state(listener_t *listener, uint32_t line_instance, uint32_t call_id);
</ins><span class="cx"> 
</span><span class="cx"> switch_status_t skinny_tech_set_codec(private_t *tech_pvt, int force);
</span><del>-void tech_init(private_t *tech_pvt, switch_core_session_t *session, listener_t *listener, uint32_t line);
</del><ins>+void tech_init(private_t *tech_pvt, skinny_profile_t *profile, switch_core_session_t *session);
</ins><span class="cx"> switch_status_t channel_on_init(switch_core_session_t *session);
</span><span class="cx"> switch_status_t channel_on_hangup(switch_core_session_t *session);
</span><span class="cx"> switch_status_t channel_on_destroy(switch_core_session_t *session);
</span><span class="lines">@@ -224,3 +235,14 @@
</span><span class="cx"> 
</span><span class="cx"> #endif /* _MOD_SKINNY_H */
</span><span class="cx"> 
</span><ins>+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
+ */
+
</ins></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_skinnyskinny_protocolc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_protocol.c (17171 => 17172)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_protocol.c        2010-04-01 20:16:27 UTC (rev 17171)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_protocol.c        2010-04-01 20:16:36 UTC (rev 17172)
</span><span class="lines">@@ -142,13 +142,13 @@
</span><span class="cx">                 return SWITCH_STATUS_MEMERR;
</span><span class="cx">         }
</span><span class="cx">         
</span><del>-        if (!globals.running) {
</del><ins>+        if (!listener_is_ready(listener)) {
</ins><span class="cx">                 return SWITCH_STATUS_FALSE;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         ptr = mbuf;
</span><span class="cx"> 
</span><del>-        while (listener-&gt;sock &amp;&amp; globals.running) {
</del><ins>+        while (listener-&gt;sock &amp;&amp; listener_is_ready(listener)) {
</ins><span class="cx">                 uint8_t do_sleep = 1;
</span><span class="cx">                 if(bytes &lt; SKINNY_MESSAGE_FIELD_SIZE) {
</span><span class="cx">                         /* We have nothing yet, get length header field */
</span><span class="lines">@@ -160,7 +160,7 @@
</span><span class="cx"> 
</span><span class="cx">                 status = switch_socket_recv(listener-&gt;sock, ptr, &amp;mlen);
</span><span class="cx">                 
</span><del>-                if (!globals.running || (!SWITCH_STATUS_IS_BREAK(status) &amp;&amp; status != SWITCH_STATUS_SUCCESS)) {
</del><ins>+                if (!listener_is_ready(listener) || (!SWITCH_STATUS_IS_BREAK(status) &amp;&amp; status != SWITCH_STATUS_SUCCESS)) {
</ins><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;Socket break.\n&quot;);
</span><span class="cx">                         return SWITCH_STATUS_FALSE;
</span><span class="cx">                 }
</span><span class="lines">@@ -218,23 +218,25 @@
</span><span class="cx"> {
</span><span class="cx">         switch_event_t *event = (switch_event_t *) pArg;
</span><span class="cx"> 
</span><del>-        char *device_name = argv[0];
-        char *user_id = argv[1];
-        char *instance = argv[2];
-        char *ip = argv[3];
-        char *device_type = argv[4];
-        char *max_streams = argv[5];
-        char *port = argv[6];
-        char *codec_string = argv[7];
</del><ins>+        char *profile_name = argv[0];
+        char *device_name = argv[1];
+        char *user_id = argv[2];
+        char *device_instance = argv[3];
+        char *ip = argv[4];
+        char *device_type = argv[5];
+        char *max_streams = argv[6];
+        char *port = argv[7];
+        char *codec_string = argv[8];
</ins><span class="cx"> 
</span><del>-        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, &quot;Skinny-Device-Name&quot;, device_name);
-        switch_event_add_header(       event, SWITCH_STACK_BOTTOM, &quot;Skinny-User-Id&quot;, &quot;%s&quot;, user_id);
-        switch_event_add_header(       event, SWITCH_STACK_BOTTOM, &quot;Skinny-Instance&quot;, &quot;%s&quot;, instance);
-        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, &quot;Skinny-IP&quot;, ip);
-        switch_event_add_header(       event, SWITCH_STACK_BOTTOM, &quot;Skinny-Device-Type&quot;, &quot;%s&quot;, device_type);
-        switch_event_add_header(       event, SWITCH_STACK_BOTTOM, &quot;Skinny-Max-Streams&quot;, &quot;%s&quot;, max_streams);
-        switch_event_add_header(       event, SWITCH_STACK_BOTTOM, &quot;Skinny-Port&quot;, &quot;%s&quot;, port);
-        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, &quot;Skinny-Codecs&quot;, codec_string);
</del><ins>+        switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Skinny-Profile-Name&quot;, &quot;%s&quot;, profile_name);
+        switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Skinny-Device-Name&quot;, &quot;%s&quot;, device_name);
+        switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Skinny-Station-User-Id&quot;, &quot;%s&quot;, user_id);
+        switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Skinny-Station-Instance&quot;, &quot;%s&quot;, device_instance);
+        switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Skinny-IP-Address&quot;, &quot;%s&quot;, ip);
+        switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Skinny-Device-Type&quot;, &quot;%s&quot;, device_type);
+        switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Skinny-Max-Streams&quot;, &quot;%s&quot;, max_streams);
+        switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Skinny-Port&quot;, &quot;%s&quot;, port);
+        switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Skinny-Codecs&quot;, &quot;%s&quot;, codec_string);
</ins><span class="cx"> 
</span><span class="cx">         return 0;
</span><span class="cx"> }
</span><span class="lines">@@ -249,8 +251,12 @@
</span><span class="cx"> 
</span><span class="cx">         switch_event_create_subclass(&amp;event, event_id, subclass_name);
</span><span class="cx">         switch_assert(event);
</span><del>-        if ((sql = switch_mprintf(&quot;SELECT * FROM skinny_devices WHERE name='%s' AND instance=%d&quot;, listener-&gt;device_name, listener-&gt;device_instance))) {
-                skinny_execute_sql_callback(profile, profile-&gt;listener_mutex, sql, skinny_device_event_callback, event);
</del><ins>+        if ((sql = switch_mprintf(&quot;SELECT '%s', name, user_id, instance, ip, type, max_streams, port, codec_string &quot;
+                &quot;FROM skinny_devices &quot;
+                        &quot;WHERE name='%s' AND instance=%d&quot;,
+                        listener-&gt;profile-&gt;name,
+                        listener-&gt;device_name, listener-&gt;device_instance))) {
+                skinny_execute_sql_callback(profile, profile-&gt;sql_mutex, sql, skinny_device_event_callback, event);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -259,30 +265,30 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /*****************************************************************************/
</span><del>-switch_status_t skinny_send_call_info(switch_core_session_t *session)
</del><ins>+switch_status_t skinny_send_call_info(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
</ins><span class="cx"> {
</span><span class="cx">         private_t *tech_pvt;
</span><span class="cx">         switch_channel_t *channel;
</span><del>-        listener_t *listener;
</del><span class="cx">         
</span><span class="cx">         char calling_party_name[40] = &quot;UNKNOWN&quot;;
</span><span class="cx">         char calling_party[24] = &quot;0000000000&quot;;
</span><span class="cx">         char called_party_name[40] = &quot;UNKNOWN&quot;;
</span><span class="cx">         char called_party[24] = &quot;0000000000&quot;;
</span><span class="cx">         
</span><ins>+        channel = switch_core_session_get_channel(session);
</ins><span class="cx">         tech_pvt = switch_core_session_get_private(session);
</span><del>-        switch_assert(tech_pvt != NULL);
-        channel = switch_core_session_get_channel(session);
-        switch_assert(channel != NULL);
</del><span class="cx"> 
</span><del>-        listener = tech_pvt-&gt;listener;
-        switch_assert(listener != NULL);
-
</del><span class="cx">         switch_assert(tech_pvt-&gt;caller_profile != NULL);
</span><span class="cx">         
</span><span class="cx">         if(        switch_channel_test_flag(channel, CF_OUTBOUND) ) {
</span><del>-                strncpy(calling_party_name, tech_pvt-&gt;line_displayname, 40);
-                strncpy(calling_party, tech_pvt-&gt;line_name, 24);
</del><ins>+            struct line_stat_res_message *button = NULL;
+
+            skinny_line_get(listener, line_instance, &amp;button);
+
+            if (button) {
+                    strncpy(calling_party_name, button-&gt;displayname, 40);
+                    strncpy(calling_party, button-&gt;name, 24);
+            }        
</ins><span class="cx">                 strncpy(called_party_name, tech_pvt-&gt;caller_profile-&gt;caller_id_name, 40);
</span><span class="cx">                 strncpy(called_party, tech_pvt-&gt;caller_profile-&gt;caller_id_number, 24);
</span><span class="cx">         } else {
</span><span class="lines">@@ -290,12 +296,12 @@
</span><span class="cx">                 strncpy(calling_party, tech_pvt-&gt;caller_profile-&gt;caller_id_number, 24);
</span><span class="cx">                 /* TODO called party */
</span><span class="cx">         }
</span><del>-        send_call_info(tech_pvt-&gt;listener,
</del><ins>+        send_call_info(listener,
</ins><span class="cx">                 calling_party_name, /* char calling_party_name[40], */
</span><span class="cx">                 calling_party, /* char calling_party[24], */
</span><span class="cx">                 called_party_name, /* char called_party_name[40], */
</span><span class="cx">                 called_party, /* char called_party[24], */
</span><del>-                tech_pvt-&gt;line, /* uint32_t line_instance, */
</del><ins>+                line_instance, /* uint32_t line_instance, */
</ins><span class="cx">                 tech_pvt-&gt;call_id, /* uint32_t call_id, */
</span><span class="cx">                 SKINNY_OUTBOUND_CALL, /* uint32_t call_type, */
</span><span class="cx">                 &quot;&quot;, /* TODO char original_called_party_name[40], */
</span><span class="lines">@@ -316,169 +322,426 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /*****************************************************************************/
</span><del>-switch_status_t skinny_create_session(listener_t *listener, uint32_t line, uint32_t to_state)
</del><ins>+switch_status_t skinny_session_walk_lines(skinny_profile_t *profile, char *channel_uuid, switch_core_db_callback_func_t callback, void *data)
</ins><span class="cx"> {
</span><del>-        switch_core_session_t *session;
</del><ins>+        char *sql;
+        if ((sql = switch_mprintf(
+                        &quot;SELECT skinny_lines.*, channel_uuid, call_id, call_state &quot;
+                        &quot;FROM skinny_active_lines &quot;
+                        &quot;INNER JOIN skinny_lines &quot;
+                                &quot;ON skinny_active_lines.device_name = skinny_lines.device_name &quot;
+                                &quot;AND skinny_active_lines.device_instance = skinny_lines.device_instance &quot;
+                                &quot;AND skinny_active_lines.line_instance = skinny_lines.line_instance &quot;
+                        &quot;WHERE channel_uuid='%s'&quot;,
+                        channel_uuid))) {
+                skinny_execute_sql_callback(profile, profile-&gt;sql_mutex, sql, callback, data);
+                switch_safe_free(sql);
+        }
+        return SWITCH_STATUS_SUCCESS;
+}
+
+switch_status_t skinny_session_walk_lines_by_call_id(skinny_profile_t *profile, uint32_t call_id, switch_core_db_callback_func_t callback, void *data)
+{
+        char *sql;
+        if ((sql = switch_mprintf(
+                        &quot;SELECT skinny_lines.*, channel_uuid, call_id, call_state &quot;
+                        &quot;FROM skinny_active_lines &quot;
+                        &quot;INNER JOIN skinny_lines &quot;
+                                &quot;ON skinny_active_lines.device_name = skinny_lines.device_name &quot;
+                                &quot;AND skinny_active_lines.device_instance = skinny_lines.device_instance &quot;
+                                &quot;AND skinny_active_lines.line_instance = skinny_lines.line_instance &quot;
+                        &quot;WHERE call_id='%d'&quot;,
+                        call_id))) {
+                skinny_execute_sql_callback(profile, profile-&gt;sql_mutex, sql, callback, data);
+                switch_safe_free(sql);
+        }
+        return SWITCH_STATUS_SUCCESS;
+}
+
+/*****************************************************************************/
+struct skinny_ring_lines_helper {
+        private_t *tech_pvt;
+        uint32_t lines_count;
+};
+
+int skinny_ring_lines_callback(void *pArg, int argc, char **argv, char **columnNames)
+{
+        struct skinny_ring_lines_helper *helper = pArg;
+        char *tmp;
+
+        char *device_name = argv[0];
+        uint32_t device_instance = atoi(argv[1]);
+        /* uint32_t position = atoi(argv[2]); */
+        uint32_t line_instance = atoi(argv[3]);
+        /* char *label = argv[4]; */
+        /* char *value = argv[5]; */
+        /* char *caller_name = argv[6]; */
+        /* uint32_t ring_on_idle = atoi(argv[7]); */
+        /* uint32_t ring_on_active = atoi(argv[8]); */
+        /* uint32_t busy_trigger = atoi(argv[9]); */
+        /* char *forward_all = argv[10]; */
+        /* char *forward_busy = argv[11]; */
+        /* char *forward_noanswer = argv[12]; */
+        /* uint32_t noanswer_duration = atoi(argv[13]); */
+        /* char *channel_uuid = argv[14]; */
+        /* uint32_t call_id = atoi(argv[15]); */
+        /* uint32_t call_state = atoi(argv[16]); */
+
+        listener_t *listener = NULL;
+
+    skinny_profile_find_listener_by_device_name_and_instance(helper-&gt;tech_pvt-&gt;profile, 
+            device_name, device_instance, &amp;listener);
+        if(listener) {
+                helper-&gt;lines_count++;
+                skinny_line_set_state(listener, line_instance, helper-&gt;tech_pvt-&gt;call_id, SKINNY_RING_IN);
+                send_select_soft_keys(listener, line_instance, helper-&gt;tech_pvt-&gt;call_id, SKINNY_KEY_SET_RING_IN, 0xffff);
+            if ((tmp = switch_mprintf(&quot;\200\027%s&quot;, helper-&gt;tech_pvt-&gt;caller_profile-&gt;destination_number))) {
+                display_prompt_status(listener, 0, tmp, line_instance, helper-&gt;tech_pvt-&gt;call_id);
+                    switch_safe_free(tmp);
+            }
+            if ((tmp = switch_mprintf(&quot;\005\000\000\000%s&quot;, helper-&gt;tech_pvt-&gt;caller_profile-&gt;destination_number))) {
+                send_display_pri_notify(listener, 10 /* message_timeout */, 5 /* priority */, tmp);
+                    switch_safe_free(tmp);
+            }
+                skinny_send_call_info(helper-&gt;tech_pvt-&gt;session, listener, line_instance);
+                set_lamp(listener, SKINNY_BUTTON_LINE, line_instance, SKINNY_LAMP_BLINK);
+                set_ringer(listener, SKINNY_RING_INSIDE, SKINNY_RING_FOREVER, 0, helper-&gt;tech_pvt-&gt;call_id);
+        }
+        return 0;
+}
+
+switch_call_cause_t skinny_ring_lines(private_t *tech_pvt)
+{
+        switch_status_t status;
+        struct skinny_ring_lines_helper helper = {0};
+
+        switch_assert(tech_pvt);
+        switch_assert(tech_pvt-&gt;profile);
+        switch_assert(tech_pvt-&gt;session);
+        
+        helper.tech_pvt = tech_pvt;
+        helper.lines_count = 0;
+        
+        status = skinny_session_walk_lines(tech_pvt-&gt;profile,
+            switch_core_session_get_uuid(tech_pvt-&gt;session), skinny_ring_lines_callback, &amp;helper);
+        if(status != SWITCH_STATUS_SUCCESS) {
+                return SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
+        } else if(helper.lines_count == 0) {
+                return SWITCH_CAUSE_UNALLOCATED_NUMBER;
+        } else {
+                return SWITCH_CAUSE_SUCCESS;
+        }
+}
+
+/*****************************************************************************/
+switch_status_t skinny_create_ingoing_session(listener_t *listener, uint32_t *line_instance_p, switch_core_session_t **session)
+{
+        switch_core_session_t *nsession;
</ins><span class="cx">         switch_channel_t *channel;
</span><span class="cx">         private_t *tech_pvt;
</span><span class="cx">         char name[128];
</span><ins>+        char *sql;
+        struct line_stat_res_message *button = NULL;
</ins><span class="cx"> 
</span><del>-        if(listener-&gt;session[line]) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, &quot;There is already a session on line %d of device %s\n&quot;,
-                        line, listener-&gt;device_name);
</del><ins>+    if((nsession = skinny_profile_find_session(listener-&gt;profile, listener, line_instance_p, 0))) {
+        switch_core_session_rwunlock(nsession);
+        if(skinny_line_get_state(listener, *line_instance_p, 0) == SKINNY_OFF_HOOK) {
+            /* Reuse existing session */
+            *session = nsession;
+                return SWITCH_STATUS_SUCCESS;
+        }
+        skinny_session_hold_line(nsession, listener, *line_instance_p);
+    }
+    if(*line_instance_p == 0) {
+        *line_instance_p = 1;
+    }
</ins><span class="cx"> 
</span><del>-                session = listener-&gt;session[line];
</del><ins>+        skinny_line_get(listener, *line_instance_p, &amp;button);
</ins><span class="cx"> 
</span><del>-                channel = switch_core_session_get_channel(session);
-                assert(channel != NULL);
-
-                tech_pvt = switch_core_session_get_private(session);
-                assert(tech_pvt != NULL);
-        } else {
</del><ins>+        if (!button || !button-&gt;shortname) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Line %d not found on device %s %d\n&quot;,
+                    *line_instance_p, listener-&gt;device_name, listener-&gt;device_instance);
+                goto error;
+        }
</ins><span class="cx">         
</span><del>-                if (!(session = switch_core_session_request(skinny_get_endpoint_interface(), SWITCH_CALL_DIRECTION_INBOUND, NULL))) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Error Creating Session\n&quot;);
-                        goto error;
-                }
</del><ins>+        if (!(nsession = switch_core_session_request(skinny_get_endpoint_interface(),
+                SWITCH_CALL_DIRECTION_INBOUND, NULL))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Error Creating Session\n&quot;);
+                goto error;
+        }
</ins><span class="cx"> 
</span><del>-                if (!(tech_pvt = (struct private_object *) switch_core_session_alloc(session, sizeof(*tech_pvt)))) {
-                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, &quot;Error Creating Session private object\n&quot;);
-                        goto error;
-                }
</del><ins>+        if (!(tech_pvt = (struct private_object *) switch_core_session_alloc(nsession, sizeof(*tech_pvt)))) {
+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(nsession), SWITCH_LOG_CRIT, 
+                    &quot;Error Creating Session private object\n&quot;);
+                goto error;
+        }
</ins><span class="cx"> 
</span><del>-                switch_core_session_add_stream(session, NULL);
</del><ins>+        switch_core_session_add_stream(nsession, NULL);
</ins><span class="cx"> 
</span><del>-                tech_init(tech_pvt, session, listener, line);
</del><ins>+        tech_init(tech_pvt, listener-&gt;profile, nsession);
</ins><span class="cx"> 
</span><del>-                channel = switch_core_session_get_channel(session);
</del><ins>+        channel = switch_core_session_get_channel(nsession);
</ins><span class="cx"> 
</span><del>-                snprintf(name, sizeof(name), &quot;SKINNY/%s/%s:%d/%d&quot;, listener-&gt;profile-&gt;name, listener-&gt;device_name, listener-&gt;device_instance, tech_pvt-&gt;line);
-                switch_channel_set_name(channel, name);
</del><ins>+        snprintf(name, sizeof(name), &quot;SKINNY/%s/%s:%d/%d&quot;, listener-&gt;profile-&gt;name, 
+            listener-&gt;device_name, listener-&gt;device_instance, *line_instance_p);
+        switch_channel_set_name(channel, name);
</ins><span class="cx"> 
</span><del>-                if (switch_core_session_thread_launch(session) != SWITCH_STATUS_SUCCESS) {
-                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, &quot;Error Creating Session thread\n&quot;);
-                        goto error;
-                }
</del><ins>+        if (switch_core_session_thread_launch(nsession) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(nsession), SWITCH_LOG_CRIT, 
+                    &quot;Error Creating Session thread\n&quot;);
+                goto error;
+        }
</ins><span class="cx"> 
</span><del>-                if (!(tech_pvt-&gt;caller_profile = switch_caller_profile_new(switch_core_session_get_pool(session),
-                                                                                                                                  NULL, listener-&gt;profile-&gt;dialplan, tech_pvt-&gt;line_displayname, tech_pvt-&gt;line_name, listener-&gt;remote_ip, NULL, NULL, NULL, &quot;skinny&quot; /* modname */, listener-&gt;profile-&gt;context, tech_pvt-&gt;dest)) != 0) {
-                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, &quot;Error Creating Session caller profile\n&quot;);
-                        goto error;
-                }
</del><ins>+        if (!(tech_pvt-&gt;caller_profile = switch_caller_profile_new(switch_core_session_get_pool(nsession),
+                                                                                                                  NULL, listener-&gt;profile-&gt;dialplan, 
+                                                                                                                  button-&gt;shortname, button-&gt;name, 
+                                                                                                                  listener-&gt;remote_ip, NULL, NULL, NULL,
+                                                                                                                  &quot;skinny&quot; /* modname */,
+                                                                                                                  listener-&gt;profile-&gt;context, 
+                                                                                                                  &quot;&quot;)) != 0) {
+            switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(nsession), SWITCH_LOG_CRIT, 
+                                &quot;Error Creating Session caller profile\n&quot;);
+                goto error;
+        }
</ins><span class="cx"> 
</span><del>-                switch_channel_set_caller_profile(channel, tech_pvt-&gt;caller_profile);
</del><ins>+        switch_channel_set_caller_profile(channel, tech_pvt-&gt;caller_profile);
</ins><span class="cx"> 
</span><ins>+        if ((sql = switch_mprintf(
+                        &quot;INSERT INTO skinny_active_lines &quot;
+                                &quot;(device_name, device_instance, line_instance, channel_uuid, call_id, call_state) &quot;
+                                &quot;SELECT device_name, device_instance, line_instance, '%s', %d, %d &quot;
+                                &quot;FROM skinny_lines &quot;
+                                &quot;WHERE value='%s'&quot;,
+                        switch_core_session_get_uuid(nsession), tech_pvt-&gt;call_id, SKINNY_ON_HOOK, button-&gt;shortname
+                        ))) {
+                skinny_execute_sql(listener-&gt;profile, sql, listener-&gt;profile-&gt;sql_mutex);
+                switch_safe_free(sql);
</ins><span class="cx">         }
</span><del>-        
-        set_ringer(listener, SKINNY_RING_OFF, SKINNY_RING_FOREVER, 0);
</del><ins>+
+        set_ringer(listener, SKINNY_RING_OFF, SKINNY_RING_FOREVER, 0, tech_pvt-&gt;call_id);
</ins><span class="cx">         set_speaker_mode(listener, SKINNY_SPEAKER_ON);
</span><del>-        set_lamp(listener, SKINNY_BUTTON_LINE, tech_pvt-&gt;line, SKINNY_LAMP_ON);
-        send_call_state(listener,
-                SKINNY_OFF_HOOK,
-                tech_pvt-&gt;line,
-                tech_pvt-&gt;call_id);
-        skinny_line_set_state(listener, tech_pvt-&gt;line, to_state, tech_pvt-&gt;call_id);
-        display_prompt_status(listener,
-                0,
-                &quot;\200\000&quot;,
-                tech_pvt-&gt;line,
-                tech_pvt-&gt;call_id);
-        activate_call_plane(listener, tech_pvt-&gt;line);
-        start_tone(listener, SKINNY_TONE_DIALTONE, 0, tech_pvt-&gt;line, tech_pvt-&gt;call_id);
</del><ins>+        set_lamp(listener, SKINNY_BUTTON_LINE, *line_instance_p, SKINNY_LAMP_ON);
+    skinny_line_set_state(listener, *line_instance_p, tech_pvt-&gt;call_id, SKINNY_OFF_HOOK);
+    send_select_soft_keys(listener, *line_instance_p, tech_pvt-&gt;call_id, SKINNY_KEY_SET_OFF_HOOK, 0xffff);
+        display_prompt_status(listener, 0, &quot;\200\000&quot;,
+                *line_instance_p, tech_pvt-&gt;call_id);
+        activate_call_plane(listener, *line_instance_p);
</ins><span class="cx"> 
</span><span class="cx">         goto done;
</span><span class="cx"> error:
</span><del>-        if (session) {
-                switch_core_session_destroy(&amp;session);
</del><ins>+        if (nsession) {
+                switch_core_session_destroy(&amp;nsession);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         return SWITCH_STATUS_FALSE;
</span><span class="cx"> 
</span><span class="cx"> done:
</span><del>-        listener-&gt;session[line] = session;
</del><ins>+        *session = nsession;
</ins><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-switch_status_t skinny_process_dest(listener_t *listener, uint32_t line)
</del><ins>+switch_status_t skinny_session_process_dest(switch_core_session_t *session, listener_t *listener, uint32_t line_instance, char *dest, char append_dest, uint32_t backspace)
</ins><span class="cx"> {
</span><span class="cx">         switch_channel_t *channel = NULL;
</span><span class="cx">         private_t *tech_pvt = NULL;
</span><span class="cx"> 
</span><del>-        channel = switch_core_session_get_channel(listener-&gt;session[line]);
-        assert(channel != NULL);
</del><ins>+    switch_assert(session);
+    switch_assert(listener);
+    switch_assert(listener-&gt;profile);
+    
+        channel = switch_core_session_get_channel(session);
+        tech_pvt = switch_core_session_get_private(session);
</ins><span class="cx"> 
</span><del>-        tech_pvt = switch_core_session_get_private(listener-&gt;session[line]);
-        assert(tech_pvt != NULL);
</del><ins>+        if(!dest) {
+        if(append_dest == '\0') {/* no digit yet */
+                start_tone(listener, SKINNY_TONE_DIALTONE, 0, line_instance, tech_pvt-&gt;call_id);
+        } else {
+            if(strlen(tech_pvt-&gt;caller_profile-&gt;destination_number) == 0) {/* first digit */
+                        stop_tone(listener, line_instance, tech_pvt-&gt;call_id);
+                send_select_soft_keys(listener, line_instance, tech_pvt-&gt;call_id,
+                    SKINNY_KEY_SET_DIGITS_AFTER_DIALING_FIRST_DIGIT, 0xffff);
+            }
+            tech_pvt-&gt;caller_profile-&gt;destination_number = switch_core_sprintf(tech_pvt-&gt;caller_profile-&gt;pool,
+                &quot;%s%c&quot;, tech_pvt-&gt;caller_profile-&gt;destination_number, append_dest);
+        }
+    } else {
+            tech_pvt-&gt;caller_profile-&gt;destination_number = switch_core_strdup(tech_pvt-&gt;caller_profile-&gt;pool,
+                dest);
+    }
+    /* TODO Number is complete -&gt; check against dialplan */
+        if((strlen(tech_pvt-&gt;caller_profile-&gt;destination_number) &gt;= 4) || dest) {
+                send_dialed_number(listener, tech_pvt-&gt;caller_profile-&gt;destination_number, line_instance, tech_pvt-&gt;call_id);
+        skinny_line_set_state(listener, line_instance, tech_pvt-&gt;call_id, SKINNY_PROCEED);
+            skinny_send_call_info(session, listener, line_instance);
+        skinny_session_start_media(session, listener, line_instance);
+        }
</ins><span class="cx"> 
</span><del>-        tech_pvt-&gt;caller_profile-&gt;destination_number = switch_core_strdup(tech_pvt-&gt;caller_profile-&gt;pool, tech_pvt-&gt;dest);
-        if(strlen(tech_pvt-&gt;dest) &gt;= 4) { /* TODO Number is complete -&gt; check against dialplan */
-                if (switch_channel_get_state(channel) == CS_NEW) {
-                        switch_channel_set_state(channel, CS_INIT);
-                }
</del><ins>+        switch_core_session_rwunlock(session);
+        
+        return SWITCH_STATUS_SUCCESS;
+}
</ins><span class="cx"> 
</span><del>-                send_dialed_number(listener, tech_pvt-&gt;dest, tech_pvt-&gt;line, tech_pvt-&gt;call_id);
-                skinny_answer(listener-&gt;session[line]);
-        }
</del><ins>+switch_status_t skinny_session_ring_out(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
+{
+        switch_channel_t *channel = NULL;
+        private_t *tech_pvt = NULL;
+
+    switch_assert(session);
+    switch_assert(listener);
+    switch_assert(listener-&gt;profile);
+    
+        channel = switch_core_session_get_channel(session);
+        tech_pvt = switch_core_session_get_private(session);
+
+    skinny_line_set_state(listener, line_instance, tech_pvt-&gt;call_id, SKINNY_RING_OUT);
+    send_select_soft_keys(listener, line_instance, tech_pvt-&gt;call_id,
+        SKINNY_KEY_SET_RING_OUT, 0xffff);
+        display_prompt_status(listener, 0, &quot;\200\026&quot;,
+                line_instance, tech_pvt-&gt;call_id);
+    skinny_send_call_info(session, listener, line_instance);
+
+        switch_core_session_rwunlock(session);
+        
</ins><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-switch_status_t skinny_answer(switch_core_session_t *session)
-{
</del><ins>+
+struct skinny_session_answer_helper {
</ins><span class="cx">         private_t *tech_pvt;
</span><span class="cx">         listener_t *listener;
</span><del>-        
</del><ins>+        uint32_t line_instance;
+};
+
+int skinny_session_answer_callback(void *pArg, int argc, char **argv, char **columnNames)
+{
+        struct skinny_session_answer_helper *helper = pArg;
+        listener_t *listener = NULL;
+
+        char *device_name = argv[0];
+        uint32_t device_instance = atoi(argv[1]);
+        /* uint32_t position = atoi(argv[2]); */
+        uint32_t line_instance = atoi(argv[3]);
+        /* char *label = argv[4]; */
+        /* char *value = argv[5]; */
+        /* char *caller_name = argv[6]; */
+        /* uint32_t ring_on_idle = atoi(argv[7]); */
+        /* uint32_t ring_on_active = atoi(argv[8]); */
+        /* uint32_t busy_trigger = atoi(argv[9]); */
+        /* char *forward_all = argv[10]; */
+        /* char *forward_busy = argv[11]; */
+        /* char *forward_noanswer = argv[12]; */
+        /* uint32_t noanswer_duration = atoi(argv[13]); */
+        /* char *channel_uuid = argv[14]; */
+        /* uint32_t call_id = atoi(argv[15]); */
+        /* uint32_t call_state = atoi(argv[16]); */
+
+        skinny_profile_find_listener_by_device_name_and_instance(helper-&gt;tech_pvt-&gt;profile, device_name, device_instance, &amp;listener);
+    if(listener) {
+        if(!strcmp(device_name, helper-&gt;listener-&gt;device_name) 
+                &amp;&amp; (device_instance == helper-&gt;listener-&gt;device_instance)
+                &amp;&amp; (line_instance == helper-&gt;line_instance)) {/* the answering line */
+                   
+                   
+                set_ringer(listener, SKINNY_RING_OFF, SKINNY_RING_FOREVER, 0, helper-&gt;tech_pvt-&gt;call_id);
+                set_speaker_mode(listener, SKINNY_SPEAKER_ON);
+                set_lamp(listener, SKINNY_BUTTON_LINE, line_instance, SKINNY_LAMP_ON);
+            skinny_line_set_state(listener, line_instance, helper-&gt;tech_pvt-&gt;call_id, SKINNY_OFF_HOOK);
+            /* send_select_soft_keys(listener, line_instance, helper-&gt;tech_pvt-&gt;call_id, SKINNY_KEY_SET_OFF_HOOK, 0xffff); */
+                /* display_prompt_status(listener, 0, &quot;\200\000&quot;,
+                        line_instance, tech_pvt-&gt;call_id); */
+                activate_call_plane(listener, line_instance);
+        }
+    }
+    return 0;
+}
+
+switch_status_t skinny_session_answer(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
+{
+        struct skinny_session_answer_helper helper = {0};
+        switch_channel_t *channel = NULL;
+        private_t *tech_pvt = NULL;
+
+    switch_assert(session);
+    switch_assert(listener);
+    switch_assert(listener-&gt;profile);
+    
+        channel = switch_core_session_get_channel(session);
</ins><span class="cx">         tech_pvt = switch_core_session_get_private(session);
</span><del>-        switch_assert(tech_pvt != NULL);
</del><span class="cx">         
</span><del>-        listener = tech_pvt-&gt;listener;
-        switch_assert(listener != NULL);
</del><ins>+    helper.tech_pvt = tech_pvt;
+    helper.listener = listener;
+    helper.line_instance = line_instance;
</ins><span class="cx"> 
</span><del>-        set_ringer(listener, SKINNY_RING_OFF, SKINNY_RING_FOREVER, 0); /* TODO : here ? */
-        stop_tone(listener, tech_pvt-&gt;line, tech_pvt-&gt;call_id);
-        open_receive_channel(listener,
-                tech_pvt-&gt;call_id, /* uint32_t conference_id, */
-                0, /* uint32_t pass_thru_party_id, */
-                20, /* uint32_t packets, */
-                SKINNY_CODEC_ULAW_64K, /* uint32_t payload_capacity, */
-                0, /* uint32_t echo_cancel_type, */
-                0, /* uint32_t g723_bitrate, */
-                0, /* uint32_t conference_id2, */
-                0 /* uint32_t reserved[10] */
-        );
-        send_call_state(listener,
-                SKINNY_CONNECTED,
-                tech_pvt-&gt;line,
-                tech_pvt-&gt;call_id);
-        skinny_line_set_state(listener, tech_pvt-&gt;line, SKINNY_KEY_SET_CONNECTED, tech_pvt-&gt;call_id);
-        display_prompt_status(listener,
-                0,
-                &quot;\200\030&quot;,
-                tech_pvt-&gt;line,
-                tech_pvt-&gt;call_id);
-        skinny_send_call_info(session);
</del><ins>+    skinny_session_walk_lines(tech_pvt-&gt;profile, switch_core_session_get_uuid(session), skinny_session_answer_callback, &amp;helper);
+
+        skinny_session_start_media(session, listener, line_instance);
+
+        switch_core_session_rwunlock(session);
+        
</ins><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-switch_status_t skinny_hold_line(listener_t *listener, uint32_t line)
</del><ins>+switch_status_t skinny_session_start_media(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
</ins><span class="cx"> {
</span><span class="cx">         switch_channel_t *channel = NULL;
</span><span class="cx">         private_t *tech_pvt = NULL;
</span><span class="cx"> 
</span><del>-        channel = switch_core_session_get_channel(listener-&gt;session[line]);
-        assert(channel != NULL);
</del><ins>+    switch_assert(session);
+    switch_assert(listener);
+    switch_assert(listener-&gt;profile);
+    
+        channel = switch_core_session_get_channel(session);
+        tech_pvt = switch_core_session_get_private(session);
+     
+    stop_tone(listener, line_instance, tech_pvt-&gt;call_id);
+    open_receive_channel(listener,
+        tech_pvt-&gt;call_id, /* uint32_t conference_id, */
+        tech_pvt-&gt;call_id, /* uint32_t pass_thru_party_id, */
+        20, /* uint32_t packets, */
+        SKINNY_CODEC_ULAW_64K, /* uint32_t payload_capacity, */
+        0, /* uint32_t echo_cancel_type, */
+        0, /* uint32_t g723_bitrate, */
+        0, /* uint32_t conference_id2, */
+        0 /* uint32_t reserved[10] */
+    );
+    skinny_line_set_state(listener, line_instance, tech_pvt-&gt;call_id, SKINNY_CONNECTED);
+    send_select_soft_keys(listener, line_instance, tech_pvt-&gt;call_id,
+        SKINNY_KEY_SET_CONNECTED, 0xffff);
+    display_prompt_status(listener,
+        0,
+        &quot;\200\030&quot;,
+        line_instance,
+        tech_pvt-&gt;call_id);
+    skinny_send_call_info(session, listener, line_instance);
</ins><span class="cx"> 
</span><del>-        tech_pvt = switch_core_session_get_private(listener-&gt;session[line]);
-        assert(tech_pvt != NULL);
</del><ins>+        switch_core_session_rwunlock(session);
+        
+        return SWITCH_STATUS_SUCCESS;
+}
</ins><span class="cx"> 
</span><ins>+switch_status_t skinny_session_hold_line(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
+{
+        switch_channel_t *channel = NULL;
+        private_t *tech_pvt = NULL;
+
+    switch_assert(session);
+    switch_assert(listener);
+    switch_assert(listener-&gt;profile);
+    
+        channel = switch_core_session_get_channel(session);
+        tech_pvt = switch_core_session_get_private(session);
+                
</ins><span class="cx">         /* TODO */
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, &quot;Hold is not implemented yet. Hanging up the line.\n&quot;);
</span><span class="cx"> 
</span><span class="cx">         switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
</span><span class="cx"> 
</span><ins>+        switch_core_session_rwunlock(session);
+        
</ins><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-switch_status_t skinny_unhold_line(listener_t *listener, uint32_t line)
</del><ins>+switch_status_t skinny_session_unhold_line(switch_core_session_t *session, listener_t *listener, uint32_t line_instance)
</ins><span class="cx"> {
</span><span class="cx">         /* TODO */
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="lines">@@ -525,7 +788,7 @@
</span><span class="cx">                         instance,
</span><span class="cx">                         listener-&gt;device_name, listener-&gt;device_instance
</span><span class="cx">                         ))) {
</span><del>-                skinny_execute_sql_callback(listener-&gt;profile, listener-&gt;profile-&gt;listener_mutex, sql, skinny_line_get_callback, &amp;helper);
</del><ins>+                skinny_execute_sql_callback(listener-&gt;profile, listener-&gt;profile-&gt;sql_mutex, sql, skinny_line_get_callback, &amp;helper);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><span class="cx">         *button = helper.button;
</span><span class="lines">@@ -569,7 +832,7 @@
</span><span class="cx">                         listener-&gt;device_name, listener-&gt;device_instance,
</span><span class="cx">                         SKINNY_BUTTON_SPEED_DIAL
</span><span class="cx">                         ))) {
</span><del>-                skinny_execute_sql_callback(listener-&gt;profile, listener-&gt;profile-&gt;listener_mutex, sql, skinny_speed_dial_get_callback, &amp;helper);
</del><ins>+                skinny_execute_sql_callback(listener-&gt;profile, listener-&gt;profile-&gt;sql_mutex, sql, skinny_speed_dial_get_callback, &amp;helper);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><span class="cx">         *button = helper.button;
</span><span class="lines">@@ -614,7 +877,7 @@
</span><span class="cx">                         listener-&gt;device_instance,
</span><span class="cx">                         SKINNY_BUTTON_SERVICE_URL
</span><span class="cx">                         ))) {
</span><del>-                skinny_execute_sql_callback(listener-&gt;profile, listener-&gt;profile-&gt;listener_mutex, sql, skinny_service_url_get_callback, &amp;helper);
</del><ins>+                skinny_execute_sql_callback(listener-&gt;profile, listener-&gt;profile-&gt;sql_mutex, sql, skinny_service_url_get_callback, &amp;helper);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><span class="cx">         *button = helper.button;
</span><span class="lines">@@ -660,7 +923,7 @@
</span><span class="cx">                         listener-&gt;device_instance,
</span><span class="cx">                         SKINNY_BUTTON_SPEED_DIAL, SKINNY_BUTTON_SERVICE_URL
</span><span class="cx">                         ))) {
</span><del>-                skinny_execute_sql_callback(listener-&gt;profile, listener-&gt;profile-&gt;listener_mutex, sql, skinny_feature_get_callback, &amp;helper);
</del><ins>+                skinny_execute_sql_callback(listener-&gt;profile, listener-&gt;profile-&gt;sql_mutex, sql, skinny_feature_get_callback, &amp;helper);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><span class="cx">         *button = helper.button;
</span><span class="lines">@@ -704,7 +967,8 @@
</span><span class="cx"> switch_status_t set_ringer(listener_t *listener,
</span><span class="cx">         uint32_t ring_type,
</span><span class="cx">         uint32_t ring_mode,
</span><del>-        uint32_t unknown)
</del><ins>+        uint32_t line_instance,
+        uint32_t call_id)
</ins><span class="cx"> {
</span><span class="cx">         skinny_message_t *message;
</span><span class="cx">         message = switch_core_alloc(listener-&gt;pool, 12+sizeof(message-&gt;data.ringer));
</span><span class="lines">@@ -712,7 +976,8 @@
</span><span class="cx">         message-&gt;length = 4 + sizeof(message-&gt;data.ringer);
</span><span class="cx">         message-&gt;data.ringer.ring_type = ring_type;
</span><span class="cx">         message-&gt;data.ringer.ring_mode = ring_mode;
</span><del>-        message-&gt;data.ringer.unknown = unknown;
</del><ins>+        message-&gt;data.ringer.line_instance = line_instance;
+        message-&gt;data.ringer.call_id = call_id;
</ins><span class="cx">         skinny_send_reply(listener, message);
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="lines">@@ -990,10 +1255,26 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-switch_status_t send_reset(listener_t *listener,
-        uint32_t reset_type)
</del><ins>+switch_status_t send_display_pri_notify(listener_t *listener,
+        uint32_t message_timeout,
+        uint32_t priority,
+        char *notify)
</ins><span class="cx"> {
</span><span class="cx">         skinny_message_t *message;
</span><ins>+        message = switch_core_alloc(listener-&gt;pool, 12+sizeof(message-&gt;data.display_pri_notify));
+        message-&gt;type = DISPLAY_PRI_NOTIFY_MESSAGE;
+        message-&gt;length = 4 + sizeof(message-&gt;data.display_pri_notify);
+        message-&gt;data.display_pri_notify.message_timeout = message_timeout;
+        message-&gt;data.display_pri_notify.priority = priority;
+        strncpy(message-&gt;data.display_pri_notify.notify, notify, 32);
+        skinny_send_reply(listener, message);
+        return SWITCH_STATUS_SUCCESS;
+}
+
+
+switch_status_t send_reset(listener_t *listener, uint32_t reset_type)
+{
+        skinny_message_t *message;
</ins><span class="cx">         message = switch_core_alloc(listener-&gt;pool, 12+sizeof(message-&gt;data.reset));
</span><span class="cx">         message-&gt;type = RESET_MESSAGE;
</span><span class="cx">         message-&gt;length = 4 + sizeof(message-&gt;data.reset);
</span><span class="lines">@@ -1078,7 +1359,7 @@
</span><span class="cx">                         request-&gt;data.reg.max_streams,
</span><span class="cx">                         &quot;&quot; /* codec_string */
</span><span class="cx">                         ))) {
</span><del>-                skinny_execute_sql(profile, sql, profile-&gt;listener_mutex);
</del><ins>+                skinny_execute_sql(profile, sql, profile-&gt;sql_mutex);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -1090,6 +1371,7 @@
</span><span class="cx">         if (xskinny) {
</span><span class="cx">                 xbuttons = switch_xml_child(xskinny, &quot;buttons&quot;);
</span><span class="cx">                 if (xbuttons) {
</span><ins>+                        uint32_t line_instance = 1;
</ins><span class="cx">                         for (xbutton = switch_xml_child(xbuttons, &quot;button&quot;); xbutton; xbutton = xbutton-&gt;next) {
</span><span class="cx">                                 uint32_t position = atoi(switch_xml_attr_soft(xbutton, &quot;position&quot;));
</span><span class="cx">                                 uint32_t type = skinny_str2button(switch_xml_attr_soft(xbutton, &quot;type&quot;));
</span><span class="lines">@@ -1106,16 +1388,16 @@
</span><span class="cx">                                         uint32_t noanswer_duration = atoi(switch_xml_attr_soft(xbutton, &quot;noanswer-duration&quot;));
</span><span class="cx">                                         if ((sql = switch_mprintf(
</span><span class="cx">                                                         &quot;INSERT INTO skinny_lines &quot;
</span><del>-                                                                &quot;(device_name, device_instance, position, &quot;
</del><ins>+                                                                &quot;(device_name, device_instance, position, line_instance, &quot;
</ins><span class="cx">                                                                 &quot;label, value, caller_name, &quot;
</span><span class="cx">                                                                 &quot;ring_on_idle, ring_on_active, busy_trigger, &quot;
</span><span class="cx">                                                               &quot;forward_all, forward_busy, forward_noanswer, noanswer_duration) &quot;
</span><del>-                                                                &quot;VALUES('%s', %d, %d, '%s', '%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d)&quot;,
-                                                        request-&gt;data.reg.device_name, request-&gt;data.reg.instance, position,
</del><ins>+                                                                &quot;VALUES('%s', %d, %d, %d, '%s', '%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d)&quot;,
+                                                        request-&gt;data.reg.device_name, request-&gt;data.reg.instance, position, line_instance++,
</ins><span class="cx">                                                         label, value, caller_name,
</span><span class="cx">                                                         ring_on_idle, ring_on_active, busy_trigger,
</span><span class="cx">                                                       forward_all, forward_busy, forward_noanswer, noanswer_duration))) {
</span><del>-                                                skinny_execute_sql(profile, sql, profile-&gt;listener_mutex);
</del><ins>+                                                skinny_execute_sql(profile, sql, profile-&gt;sql_mutex);
</ins><span class="cx">                                                 switch_safe_free(sql);
</span><span class="cx">                                         }
</span><span class="cx">                                 } else {
</span><span class="lines">@@ -1131,7 +1413,7 @@
</span><span class="cx">                                                         label,
</span><span class="cx">                                                         value,
</span><span class="cx">                                                         settings))) {
</span><del>-                                                skinny_execute_sql(profile, sql, profile-&gt;listener_mutex);
</del><ins>+                                                skinny_execute_sql(profile, sql, profile-&gt;sql_mutex);
</ins><span class="cx">                                                 switch_safe_free(sql);
</span><span class="cx">                                         }
</span><span class="cx">                                 }
</span><span class="lines">@@ -1145,9 +1427,9 @@
</span><span class="cx">         message = switch_core_alloc(listener-&gt;pool, 12+sizeof(message-&gt;data.reg_ack));
</span><span class="cx">         message-&gt;type = REGISTER_ACK_MESSAGE;
</span><span class="cx">         message-&gt;length = 4 + sizeof(message-&gt;data.reg_ack);
</span><del>-        message-&gt;data.reg_ack.keepAlive = profile-&gt;keep_alive;
-        memcpy(message-&gt;data.reg_ack.dateFormat, profile-&gt;date_format, 6);
-        message-&gt;data.reg_ack.secondaryKeepAlive = profile-&gt;keep_alive;
</del><ins>+        message-&gt;data.reg_ack.keep_alive = profile-&gt;keep_alive;
+        memcpy(message-&gt;data.reg_ack.date_format, profile-&gt;date_format, 6);
+        message-&gt;data.reg_ack.secondary_keep_alive = profile-&gt;keep_alive;
</ins><span class="cx">         skinny_send_reply(listener, message);
</span><span class="cx"> 
</span><span class="cx">         /* Send CapabilitiesReqMessage */
</span><span class="lines">@@ -1227,7 +1509,7 @@
</span><span class="cx">                         SKINNY_BUTTON_SPEED_DIAL,
</span><span class="cx">                         listener-&gt;device_name
</span><span class="cx">                         ))) {
</span><del>-                skinny_execute_sql_callback(profile, profile-&gt;listener_mutex, sql, skinny_config_stat_res_callback, message);
</del><ins>+                skinny_execute_sql_callback(profile, profile-&gt;sql_mutex, sql, skinny_config_stat_res_callback, message);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><span class="cx">         skinny_send_reply(listener, message);
</span><span class="lines">@@ -1287,10 +1569,10 @@
</span><span class="cx">                         codec_string,
</span><span class="cx">                         listener-&gt;device_name
</span><span class="cx">                         ))) {
</span><del>-                skinny_execute_sql(profile, sql, profile-&gt;listener_mutex);
</del><ins>+                skinny_execute_sql(profile, sql, profile-&gt;sql_mutex);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><del>-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
</del><ins>+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
</ins><span class="cx">                 &quot;Codecs %s supported.\n&quot;, codec_string);
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="lines">@@ -1309,11 +1591,11 @@
</span><span class="cx"> 
</span><span class="cx">         if ((sql = switch_mprintf(
</span><span class="cx">                         &quot;UPDATE skinny_devices SET port=%d WHERE name='%s' and instance=%d&quot;,
</span><del>-                        request-&gt;data.as_uint16,
</del><ins>+                        request-&gt;data.port.port,
</ins><span class="cx">                         listener-&gt;device_name,
</span><span class="cx">                         listener-&gt;device_instance
</span><span class="cx">                         ))) {
</span><del>-                skinny_execute_sql(profile, sql, profile-&gt;listener_mutex);
</del><ins>+                skinny_execute_sql(profile, sql, profile-&gt;sql_mutex);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="lines">@@ -1378,7 +1660,7 @@
</span><span class="cx">                         SKINNY_BUTTON_UNDEFINED,
</span><span class="cx">                         listener-&gt;device_name, listener-&gt;device_instance
</span><span class="cx">                         ))) {
</span><del>-                skinny_execute_sql_callback(profile, profile-&gt;listener_mutex, sql, skinny_handle_button_template_request_callback, &amp;helper);
</del><ins>+                skinny_execute_sql_callback(profile, profile-&gt;sql_mutex, sql, skinny_handle_button_template_request_callback, &amp;helper);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><span class="cx">         
</span><span class="lines">@@ -1391,7 +1673,7 @@
</span><span class="cx">                         SKINNY_BUTTON_LINE,
</span><span class="cx">                         listener-&gt;device_name, listener-&gt;device_instance
</span><span class="cx">                         ))) {
</span><del>-                skinny_execute_sql_callback(profile, profile-&gt;listener_mutex, sql, skinny_handle_button_template_request_callback, &amp;helper);
</del><ins>+                skinny_execute_sql_callback(profile, profile-&gt;sql_mutex, sql, skinny_handle_button_template_request_callback, &amp;helper);
</ins><span class="cx">                 switch_safe_free(sql);
</span><span class="cx">         }
</span><span class="cx">         
</span><span class="lines">@@ -1464,7 +1746,7 @@
</span><span class="cx">         skinny_send_reply(listener, message);
</span><span class="cx"> 
</span><span class="cx">         /* Init the states */
</span><del>-        skinny_line_set_state(listener, 0, SKINNY_KEY_SET_ON_HOOK, 0);
</del><ins>+    send_select_soft_keys(listener, 0, 0, SKINNY_KEY_SET_ON_HOOK, 0xffff);
</ins><span class="cx">         
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="lines">@@ -1596,173 +1878,128 @@
</span><span class="cx"> switch_status_t skinny_handle_soft_key_event_message(listener_t *listener, skinny_message_t *request)
</span><span class="cx"> {
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_SUCCESS;
</span><del>-
-        skinny_profile_t *profile;
</del><ins>+        uint32_t line_instance = 0;
+        switch_core_session_t *session = NULL;
</ins><span class="cx">         switch_channel_t *channel = NULL;
</span><del>-        uint32_t line;
</del><span class="cx">         private_t *tech_pvt = NULL;
</span><span class="cx"> 
</span><ins>+    switch_assert(listener);
+    switch_assert(listener-&gt;profile);
+    
+        skinny_check_data_length(request, sizeof(request-&gt;data.soft_key_event));
</ins><span class="cx"> 
</span><del>-        switch_assert(listener-&gt;profile);
-        switch_assert(listener-&gt;device_name);
</del><ins>+        line_instance = request-&gt;data.soft_key_event.line_instance;
</ins><span class="cx"> 
</span><del>-        profile = listener-&gt;profile;
</del><ins>+        switch(request-&gt;data.soft_key_event.event) {
+                case SOFTKEY_REDIAL:
+                status = skinny_create_ingoing_session(listener, &amp;line_instance, &amp;session);
</ins><span class="cx"> 
</span><del>-        skinny_check_data_length(request, sizeof(request-&gt;data.soft_key_event));
</del><ins>+                    skinny_session_process_dest(session, listener, line_instance, &quot;redial&quot;, '\0', 0);
+                        break;
+                case SOFTKEY_NEWCALL:
+                status = skinny_create_ingoing_session(listener, &amp;line_instance, &amp;session);
+                    tech_pvt = switch_core_session_get_private(session);
+                    assert(tech_pvt != NULL);
</ins><span class="cx"> 
</span><del>-        if(request-&gt;data.soft_key_event.line_instance) {
-                line = request-&gt;data.soft_key_event.line_instance;
-        } else {
-                line = 1;
-        }
-        /* Close/Hold busy lines */
-        for(int i = 0 ; i &lt; SKINNY_MAX_BUTTON_COUNT ; i++) {
-                if(i == line) {
-                        continue;
-                }
-                if(listener-&gt;session[i]) {
-                        channel = switch_core_session_get_channel(listener-&gt;session[i]);
-                        assert(channel != NULL);
-                        if((skinny_line_get_state(listener, i) == SKINNY_KEY_SET_ON_HOOK)
-                                        || (skinny_line_get_state(listener, i) == SKINNY_KEY_SET_OFF_HOOK)
-                                        || (skinny_line_get_state(listener, i) == SKINNY_KEY_SET_RING_OUT)
-                                        || (skinny_line_get_state(listener, i) == SKINNY_KEY_SET_DIGITS_AFTER_DIALING_FIRST_DIGIT)
-                                        || (skinny_line_get_state(listener, i) == SKINNY_KEY_SET_OFF_HOOK_WITH_FEATURES)) {
</del><ins>+                    skinny_session_process_dest(session, listener, line_instance, NULL, '\0', 0);
+                        break;
+                case SOFTKEY_HOLD:
+            session = skinny_profile_find_session(listener-&gt;profile, listener, &amp;line_instance, request-&gt;data.soft_key_event.call_id);
</ins><span class="cx"> 
</span><del>-                                switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
-                                channel = NULL;
-                        } else if((skinny_line_get_state(listener, i) == SKINNY_KEY_SET_RING_IN)
-                                        || (skinny_line_get_state(listener, i) == SKINNY_KEY_SET_CONNECTED)
-                                        || (skinny_line_get_state(listener, i) == SKINNY_KEY_SET_CONNECTED_WITH_TRANSFER)
-                                        || (skinny_line_get_state(listener, i) == SKINNY_KEY_SET_CONNECTED_WITH_CONFERENCE)) {
-                                skinny_hold_line(listener, i);                        
-                        } /* remaining: SKINNY_KEY_SET_ON_HOLD */
-                }
-        }
-        
-        if(!listener-&gt;session[line]) { /*the line is not busy */
-                switch(request-&gt;data.soft_key_event.event) {
-                        case SOFTKEY_REDIAL:
-                                skinny_create_session(listener, line, SKINNY_KEY_SET_DIGITS_AFTER_DIALING_FIRST_DIGIT);
-        
-                                tech_pvt = switch_core_session_get_private(listener-&gt;session[line]);
-                                assert(tech_pvt != NULL);
-                
-                                strcpy(tech_pvt-&gt;dest, &quot;redial&quot;);
-                                skinny_process_dest(listener, line);
-                                break;
-                        case SOFTKEY_NEWCALL:
-                                skinny_create_session(listener, line, SKINNY_KEY_SET_OFF_HOOK);
-                                break;
-                        default:
-                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
-                                        &quot;Unknown SoftKeyEvent type while not busy: %d.\n&quot;, request-&gt;data.soft_key_event.event);
-                }
-        } else { /* the line is busy */
-                if(skinny_line_get_state(listener, line) == SKINNY_KEY_SET_ON_HOLD) {
-                        skinny_unhold_line(listener, line);                        
-                }
-                switch(request-&gt;data.soft_key_event.event) {
-                        case SOFTKEY_ANSWER:
-                                skinny_answer(listener-&gt;session[line]);
-                                break;
-                        case SOFTKEY_ENDCALL:
-                                channel = switch_core_session_get_channel(listener-&gt;session[line]);
-                                assert(channel != NULL);
</del><ins>+                    if(session) {
+                status = skinny_session_hold_line(session, listener, line_instance);
+            }
+                        break;
+                case SOFTKEY_ENDCALL:
+            session = skinny_profile_find_session(listener-&gt;profile, listener, &amp;line_instance, request-&gt;data.soft_key_event.call_id);
</ins><span class="cx"> 
</span><del>-                                switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
</del><ins>+                    if(session) {
+                            channel = switch_core_session_get_channel(session);
</ins><span class="cx"> 
</span><del>-                                break;
-                        default:
-                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
-                                        &quot;Unknown SoftKeyEvent type while busy: %d.\n&quot;, request-&gt;data.soft_key_event.event);
-                }
</del><ins>+                            switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
+            }
+                        break;
+                case SOFTKEY_RESUME:
+            session = skinny_profile_find_session(listener-&gt;profile, listener, &amp;line_instance, request-&gt;data.soft_key_event.call_id);
+
+                    if(session) {
+                status = skinny_session_unhold_line(session, listener, line_instance);
+            }
+                        break;
+                case SOFTKEY_ANSWER:
+            session = skinny_profile_find_session(listener-&gt;profile, listener, &amp;line_instance, request-&gt;data.soft_key_event.call_id);
+
+                    if(session) {
+                            status = skinny_session_answer(session, listener, line_instance);
+                    }
+                        break;
+                default:
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
+                                &quot;Unknown SoftKeyEvent type while busy: %d.\n&quot;, request-&gt;data.soft_key_event.event);
</ins><span class="cx">         }
</span><ins>+
+    if(session) {
+            switch_core_session_rwunlock(session);
+    }
+        
</ins><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> switch_status_t skinny_handle_off_hook_message(listener_t *listener, skinny_message_t *request)
</span><span class="cx"> {
</span><del>-        skinny_profile_t *profile;
-        uint32_t line;
</del><ins>+        uint32_t line_instance;
+        switch_core_session_t *session = NULL;
+        private_t *tech_pvt = NULL;
</ins><span class="cx"> 
</span><del>-        switch_assert(listener-&gt;profile);
-        switch_assert(listener-&gt;device_name);
-
-        profile = listener-&gt;profile;
-
</del><span class="cx">         skinny_check_data_length(request, sizeof(request-&gt;data.off_hook));
</span><span class="cx"> 
</span><del>-        if(request-&gt;data.off_hook.line_instance) {
-                line = request-&gt;data.off_hook.line_instance;
</del><ins>+        if(request-&gt;data.off_hook.line_instance &gt; 0) {
+                line_instance = request-&gt;data.off_hook.line_instance;
</ins><span class="cx">         } else {
</span><del>-                line = 1;
</del><ins>+                line_instance = 1;
</ins><span class="cx">         }
</span><del>-        if(listener-&gt;session[line]) { /*answering a call */
-                skinny_answer(listener-&gt;session[line]);
</del><ins>+
+    session = skinny_profile_find_session(listener-&gt;profile, listener, &amp;line_instance, request-&gt;data.off_hook.call_id);
+
+        if(session) { /*answering a call */
+                skinny_session_answer(session, listener, line_instance);
</ins><span class="cx">         } else { /* start a new call */
</span><del>-                skinny_create_session(listener, line, SKINNY_KEY_SET_OFF_HOOK);
</del><ins>+                skinny_create_ingoing_session(listener, &amp;line_instance, &amp;session);
+            tech_pvt = switch_core_session_get_private(session);
+            assert(tech_pvt != NULL);
+
+            skinny_session_process_dest(session, listener, line_instance, NULL, '\0', 0);
</ins><span class="cx">         }
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> switch_status_t skinny_handle_stimulus_message(listener_t *listener, skinny_message_t *request)
</span><span class="cx"> {
</span><del>-        skinny_profile_t *profile;
</del><span class="cx">         struct speed_dial_stat_res_message *button = NULL;
</span><del>-        uint32_t line = 0;
-        private_t *tech_pvt = NULL;
</del><ins>+        uint32_t line_instance = 0;
+        uint32_t call_id = 0;
+        switch_core_session_t *session = NULL;
</ins><span class="cx"> 
</span><del>-        switch_assert(listener-&gt;profile);
-        switch_assert(listener-&gt;device_name);
</del><ins>+        skinny_check_data_length(request, sizeof(request-&gt;data.stimulus)-sizeof(request-&gt;data.stimulus.call_id));
</ins><span class="cx"> 
</span><del>-        profile = listener-&gt;profile;
</del><ins>+        if(skinny_check_data_length_soft(request, sizeof(request-&gt;data.stimulus))) {
+            call_id = request-&gt;data.stimulus.call_id;
+        }
</ins><span class="cx"> 
</span><del>-        skinny_check_data_length(request, sizeof(request-&gt;data.stimulus));
-
-        if(request-&gt;data.stimulus.instance_type == SKINNY_BUTTON_LINE) {/* Choose the specified line */
-                line = request-&gt;data.stimulus.instance;
-        }
-        if(line == 0) {/* If none, find the first busy line */
-                for(int i = 0 ; i &lt; SKINNY_MAX_BUTTON_COUNT ; i++) {
-                        if(listener-&gt;session[i]) {
-                                line = i;
-                                break;
-                        }
-                }
-        }
-        if(line == 0) {/* If none, choose the first line */
-                line = 1;
-        }
</del><span class="cx">         switch(request-&gt;data.stimulus.instance_type) {
</span><span class="cx">                 case SKINNY_BUTTON_LAST_NUMBER_REDIAL:
</span><del>-                        skinny_create_session(listener, line, SKINNY_KEY_SET_DIGITS_AFTER_DIALING_FIRST_DIGIT);
-
-                        tech_pvt = switch_core_session_get_private(listener-&gt;session[line]);
-                        assert(tech_pvt != NULL);
-        
-                        strcpy(tech_pvt-&gt;dest, &quot;redial&quot;);
-                        skinny_process_dest(listener, line);
</del><ins>+                    skinny_create_ingoing_session(listener, &amp;line_instance, &amp;session);
+                        skinny_session_process_dest(session, listener, line_instance, &quot;redial&quot;, '\0', 0);
</ins><span class="cx">                         break;
</span><span class="cx">                 case SKINNY_BUTTON_VOICEMAIL:
</span><del>-                        skinny_create_session(listener, line, SKINNY_KEY_SET_DIGITS_AFTER_DIALING_FIRST_DIGIT);
-        
-                        tech_pvt = switch_core_session_get_private(listener-&gt;session[line]);
-                        assert(tech_pvt != NULL);
-                
-                        strcpy(tech_pvt-&gt;dest, &quot;vmain&quot;);
-                        skinny_process_dest(listener, line);
</del><ins>+                    skinny_create_ingoing_session(listener, &amp;line_instance, &amp;session);
+                        skinny_session_process_dest(session, listener, line_instance, &quot;vmain&quot;, '\0', 0);
</ins><span class="cx">                         break;
</span><span class="cx">                 case SKINNY_BUTTON_SPEED_DIAL:
</span><span class="cx">                         skinny_speed_dial_get(listener, request-&gt;data.stimulus.instance, &amp;button);
</span><span class="cx">                         if(strlen(button-&gt;line) &gt; 0) {
</span><del>-                                skinny_create_session(listener, line, SKINNY_KEY_SET_DIGITS_AFTER_DIALING_FIRST_DIGIT);
-
-                                tech_pvt = switch_core_session_get_private(listener-&gt;session[line]);
-                                assert(tech_pvt != NULL);
-                
-                                strcpy(tech_pvt-&gt;dest, button-&gt;line);
-                                skinny_process_dest(listener, line);
</del><ins>+                        skinny_create_ingoing_session(listener, &amp;line_instance, &amp;session);
+                            skinny_session_process_dest(session, listener, line_instance, button-&gt;line, '\0', 0);
</ins><span class="cx">                         }
</span><span class="cx">                         break;
</span><span class="cx">                 default:
</span><span class="lines">@@ -1774,35 +2011,21 @@
</span><span class="cx"> switch_status_t skinny_handle_open_receive_channel_ack_message(listener_t *listener, skinny_message_t *request)
</span><span class="cx"> {
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_SUCCESS;
</span><del>-        skinny_profile_t *profile;
-        uint32_t line = 0;
</del><ins>+        uint32_t line_instance = 0;
+        switch_core_session_t *session;
</ins><span class="cx"> 
</span><del>-        switch_assert(listener-&gt;profile);
-        switch_assert(listener-&gt;device_name);
-
-        profile = listener-&gt;profile;
-
</del><span class="cx">         skinny_check_data_length(request, sizeof(request-&gt;data.open_receive_channel_ack));
</span><span class="cx"> 
</span><del>-        for(int i = 0 ; i &lt; SKINNY_MAX_BUTTON_COUNT ; i++) {
-                if(listener-&gt;session[i]) {
-                        private_t *tech_pvt = NULL;
-                        tech_pvt = switch_core_session_get_private(listener-&gt;session[i]);
-                        
-                        if(tech_pvt-&gt;party_id == request-&gt;data.open_receive_channel_ack.pass_thru_party_id) {
-                                line = i;
-                        }
-                }
-        }
</del><ins>+    session = skinny_profile_find_session(listener-&gt;profile, listener, &amp;line_instance, request-&gt;data.open_receive_channel_ack.pass_thru_party_id);
</ins><span class="cx"> 
</span><del>-        if(listener-&gt;session[line]) {
</del><ins>+        if(session) {
</ins><span class="cx">                 const char *err = NULL;
</span><span class="cx">                 private_t *tech_pvt = NULL;
</span><span class="cx">                 switch_channel_t *channel = NULL;
</span><span class="cx">                 struct in_addr addr;
</span><span class="cx"> 
</span><del>-                tech_pvt = switch_core_session_get_private(listener-&gt;session[line]);
-                channel = switch_core_session_get_channel(listener-&gt;session[line]);
</del><ins>+                tech_pvt = switch_core_session_get_private(session);
+                channel = switch_core_session_get_channel(session);
</ins><span class="cx"> 
</span><span class="cx">                 /* Codec */
</span><span class="cx">                 tech_pvt-&gt;iananame = &quot;PCMU&quot;; /* TODO */
</span><span class="lines">@@ -1810,7 +2033,7 @@
</span><span class="cx">                 tech_pvt-&gt;rm_rate = 8000; /* TODO */
</span><span class="cx">                 tech_pvt-&gt;rm_fmtp = NULL; /* TODO */
</span><span class="cx">                 tech_pvt-&gt;agreed_pt = (switch_payload_t) 0; /* TODO */
</span><del>-                tech_pvt-&gt;rm_encoding = switch_core_strdup(switch_core_session_get_pool(listener-&gt;session[line]), &quot;&quot;);
</del><ins>+                tech_pvt-&gt;rm_encoding = switch_core_strdup(switch_core_session_get_pool(session), &quot;&quot;);
</ins><span class="cx">                 skinny_tech_set_codec(tech_pvt, 0);
</span><span class="cx">                 if ((status = skinny_tech_set_codec(tech_pvt, 0)) != SWITCH_STATUS_SUCCESS) {
</span><span class="cx">                         goto end;
</span><span class="lines">@@ -1821,7 +2044,7 @@
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt-&gt;session), SWITCH_LOG_CRIT, &quot;No RTP ports available!\n&quot;);
</span><span class="cx">                         return SWITCH_STATUS_FALSE;
</span><span class="cx">                 }
</span><del>-                tech_pvt-&gt;local_sdp_audio_ip = switch_core_strdup(switch_core_session_get_pool(listener-&gt;session[line]), listener-&gt;profile-&gt;ip);
</del><ins>+                tech_pvt-&gt;local_sdp_audio_ip = switch_core_strdup(switch_core_session_get_pool(session), listener-&gt;profile-&gt;ip);
</ins><span class="cx"> 
</span><span class="cx">                 tech_pvt-&gt;remote_sdp_audio_ip = inet_ntoa(request-&gt;data.open_receive_channel_ack.ip);
</span><span class="cx">                 tech_pvt-&gt;remote_sdp_audio_port = request-&gt;data.open_receive_channel_ack.port;
</span><span class="lines">@@ -1834,7 +2057,7 @@
</span><span class="cx">                                                                                            tech_pvt-&gt;read_impl.samples_per_packet,
</span><span class="cx">                                                                                            tech_pvt-&gt;codec_ms * 1000,
</span><span class="cx">                                                                                            (switch_rtp_flag_t) 0, &quot;soft&quot;, &amp;err,
</span><del>-                                                                                           switch_core_session_get_pool(listener-&gt;session[line]));
</del><ins>+                                                                                           switch_core_session_get_pool(session));
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt-&gt;session), SWITCH_LOG_DEBUG,
</span><span class="cx">                                                   &quot;AUDIO RTP [%s] %s:%d-&gt;%s:%d codec: %u ms: %d [%s]\n&quot;,
</span><span class="cx">                                                   switch_channel_get_name(channel),
</span><span class="lines">@@ -1858,7 +2081,12 @@
</span><span class="cx">                         0, /* uint16_t max_frames_per_packet, */
</span><span class="cx">                         0 /* uint32_t g723_bitrate */
</span><span class="cx">                 );
</span><ins>+            if (switch_channel_get_state(channel) == CS_NEW) {
+                    switch_channel_set_state(channel, CS_INIT);
+            }
</ins><span class="cx">                 switch_channel_mark_answered(channel);
</span><ins>+                
+                switch_core_session_rwunlock(session);
</ins><span class="cx">         }
</span><span class="cx"> end:
</span><span class="cx">         return status;
</span><span class="lines">@@ -1866,35 +2094,28 @@
</span><span class="cx"> 
</span><span class="cx"> switch_status_t skinny_handle_keypad_button_message(listener_t *listener, skinny_message_t *request)
</span><span class="cx"> {
</span><del>-        uint32_t line = 0;
</del><ins>+        uint32_t line_instance = 0;
+        switch_core_session_t *session;
</ins><span class="cx"> 
</span><span class="cx">         skinny_check_data_length(request, sizeof(request-&gt;data.keypad_button));
</span><ins>+    
+    if(request-&gt;data.keypad_button.line_instance) {
+        line_instance = request-&gt;data.keypad_button.line_instance;
+    } else {
+        line_instance = 1;
+    }
</ins><span class="cx"> 
</span><del>-        /* Choose the specified line */
-        line = request-&gt;data.keypad_button.line_instance;
-        if(line == 0) {/* If none, find the first busy line */
-                for(int i = 0 ; i &lt; SKINNY_MAX_BUTTON_COUNT ; i++) {
-                        if(listener-&gt;session[i]) {
-                                line = i;
-                                break;
-                        }
-                }
-        }
-        if(line == 0) {/* If none, choose the first line */
-                line = 1;
-        }
-        if(listener-&gt;session[line]) {
</del><ins>+    session = skinny_profile_find_session(listener-&gt;profile, listener, &amp;line_instance, request-&gt;data.keypad_button.call_id);
+
+        if(session) {
</ins><span class="cx">                 switch_channel_t *channel = NULL;
</span><span class="cx">                 private_t *tech_pvt = NULL;
</span><span class="cx">                 char digit = '\0';
</span><span class="cx"> 
</span><del>-                channel = switch_core_session_get_channel(listener-&gt;session[line]);
-                assert(channel != NULL);
-
-                tech_pvt = switch_core_session_get_private(listener-&gt;session[line]);
-                assert(tech_pvt != NULL);
</del><ins>+                channel = switch_core_session_get_channel(session);
+                tech_pvt = switch_core_session_get_private(session);
</ins><span class="cx">                 
</span><del>-                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(listener-&gt;session[line]), SWITCH_LOG_DEBUG, &quot;SEND DTMF ON CALL %d [%d]\n&quot;, tech_pvt-&gt;call_id, request-&gt;data.keypad_button.button);
</del><ins>+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;SEND DTMF ON CALL %d [%d]\n&quot;, tech_pvt-&gt;call_id, request-&gt;data.keypad_button.button);
</ins><span class="cx"> 
</span><span class="cx">                 if (request-&gt;data.keypad_button.button == 14) {
</span><span class="cx">                         digit = '*';
</span><span class="lines">@@ -1903,21 +2124,14 @@
</span><span class="cx">                 } else if (request-&gt;data.keypad_button.button &gt;= 0 &amp;&amp; request-&gt;data.keypad_button.button &lt;= 9) {
</span><span class="cx">                         digit = '0' + request-&gt;data.keypad_button.button;
</span><span class="cx">                 } else {
</span><del>-                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(listener-&gt;session[line]), SWITCH_LOG_WARNING, &quot;UNKNOW DTMF RECEIVED ON CALL %d [%d]\n&quot;, tech_pvt-&gt;call_id, request-&gt;data.keypad_button.button);
</del><ins>+                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, &quot;UNKNOW DTMF RECEIVED ON CALL %d [%d]\n&quot;, tech_pvt-&gt;call_id, request-&gt;data.keypad_button.button);
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /* TODO check call_id and line */
</span><span class="cx"> 
</span><del>-                if((skinny_line_get_state(listener, tech_pvt-&gt;line) == SKINNY_KEY_SET_OFF_HOOK)
-                                || (skinny_line_get_state(listener, tech_pvt-&gt;line) == SKINNY_KEY_SET_DIGITS_AFTER_DIALING_FIRST_DIGIT)) {
-                        if(strlen(tech_pvt-&gt;dest) == 0) {/* first digit */
-                                stop_tone(listener, tech_pvt-&gt;line, tech_pvt-&gt;call_id);
-                                skinny_line_set_state(listener, tech_pvt-&gt;line, SKINNY_KEY_SET_DIGITS_AFTER_DIALING_FIRST_DIGIT, tech_pvt-&gt;call_id);
-                        }
</del><ins>+                if((skinny_line_get_state(listener, line_instance, tech_pvt-&gt;call_id) == SKINNY_OFF_HOOK)) {
</ins><span class="cx">                         
</span><del>-                        tech_pvt-&gt;dest[strlen(tech_pvt-&gt;dest)] = digit;
-                        
-                        skinny_process_dest(listener, tech_pvt-&gt;line);
</del><ins>+                    skinny_session_process_dest(session, listener, line_instance, NULL, digit, 0);
</ins><span class="cx">                 } else {
</span><span class="cx">                         if(digit != '\0') {
</span><span class="cx">                                 switch_dtmf_t dtmf = { 0, switch_core_default_dtmf_duration(0)};
</span><span class="lines">@@ -1925,6 +2139,7 @@
</span><span class="cx">                                 switch_channel_queue_dtmf(channel, &amp;dtmf);
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><ins>+            switch_core_session_rwunlock(session);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="lines">@@ -1933,35 +2148,23 @@
</span><span class="cx"> switch_status_t skinny_handle_on_hook_message(listener_t *listener, skinny_message_t *request)
</span><span class="cx"> {
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_SUCCESS;
</span><del>-        skinny_profile_t *profile;
-        uint32_t line = 0;
</del><ins>+        switch_core_session_t *session = NULL;
+        uint32_t line_instance = 0;
</ins><span class="cx"> 
</span><del>-        switch_assert(listener-&gt;profile);
-        switch_assert(listener-&gt;device_name);
-
-        profile = listener-&gt;profile;
-
</del><span class="cx">         skinny_check_data_length(request, sizeof(request-&gt;data.on_hook));
</span><span class="cx"> 
</span><del>-        if(request-&gt;data.on_hook.line_instance != 0) {
-                line = request-&gt;data.on_hook.line_instance;
-        } else {
-                /* Find first active line */
-                for(int i = 0 ; i &lt; SKINNY_MAX_BUTTON_COUNT ; i++) {
-                        if(listener-&gt;session[i]) {
-                                line = i;
-                                break;
-                        }
-                }
-        }
</del><ins>+    line_instance = request-&gt;data.on_hook.line_instance;
+    
+    session = skinny_profile_find_session(listener-&gt;profile, listener, &amp;line_instance, request-&gt;data.on_hook.call_id);
</ins><span class="cx"> 
</span><del>-        if(listener-&gt;session[line]) {
</del><ins>+        if(session) {
</ins><span class="cx">                 switch_channel_t *channel = NULL;
</span><span class="cx"> 
</span><del>-                channel = switch_core_session_get_channel(listener-&gt;session[line]);
-                assert(channel != NULL);
</del><ins>+                channel = switch_core_session_get_channel(session);
</ins><span class="cx"> 
</span><span class="cx">                 switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
</span><ins>+                
+                switch_core_session_rwunlock(session);
</ins><span class="cx">         }
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="lines">@@ -2069,3 +2272,14 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
+ */
+
</ins></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_skinnyskinny_protocolh"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_protocol.h (17171 => 17172)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_protocol.h        2010-04-01 20:16:27 UTC (rev 17171)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_protocol.h        2010-04-01 20:16:36 UTC (rev 17172)
</span><span class="lines">@@ -59,6 +59,9 @@
</span><span class="cx"> 
</span><span class="cx"> /* PortMessage */
</span><span class="cx"> #define PORT_MESSAGE 0x0002
</span><ins>+struct port_message {
+        uint16_t port;
+};
</ins><span class="cx"> 
</span><span class="cx"> /* KeypadButtonMessage */
</span><span class="cx"> #define KEYPAD_BUTTON_MESSAGE 0x0003
</span><span class="lines">@@ -73,14 +76,14 @@
</span><span class="cx"> struct stimulus_message {
</span><span class="cx">         uint32_t instance_type; /* See enum skinny_button_definition */
</span><span class="cx">         uint32_t instance;
</span><del>-        /* uint32_t call_reference; */
</del><ins>+        uint32_t call_id;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> /* OffHookMessage */
</span><span class="cx"> #define OFF_HOOK_MESSAGE 0x0006
</span><span class="cx"> struct off_hook_message {
</span><span class="cx">         uint32_t line_instance;
</span><del>-        /* uint32_t call_id; */
</del><ins>+        uint32_t call_id;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> /* OnHookMessage */
</span><span class="lines">@@ -150,7 +153,7 @@
</span><span class="cx"> struct soft_key_event_message {
</span><span class="cx">         uint32_t event;
</span><span class="cx">         uint32_t line_instance;
</span><del>-        uint32_t callreference;
</del><ins>+        uint32_t call_id;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> /* UnregisterMessage */
</span><span class="lines">@@ -186,10 +189,10 @@
</span><span class="cx"> /* RegisterAckMessage */
</span><span class="cx"> #define REGISTER_ACK_MESSAGE 0x0081
</span><span class="cx"> struct register_ack_message {
</span><del>-        uint32_t keepAlive;
-        char dateFormat[6];
</del><ins>+        uint32_t keep_alive;
+        char date_format[6];
</ins><span class="cx">         char reserved[2];
</span><del>-        uint32_t secondaryKeepAlive;
</del><ins>+        uint32_t secondary_keep_alive;
</ins><span class="cx">         char reserved2[4];
</span><span class="cx"> };
</span><span class="cx"> 
</span><span class="lines">@@ -214,7 +217,8 @@
</span><span class="cx"> struct set_ringer_message {
</span><span class="cx">         uint32_t ring_type; /* See enum skinny_ring_type */
</span><span class="cx">         uint32_t ring_mode; /* See enum skinny_ring_mode */
</span><del>-        uint32_t unknown; /* ?? */
</del><ins>+        uint32_t line_instance;
+        uint32_t call_id;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> /* SetLampMessage */
</span><span class="lines">@@ -470,6 +474,14 @@
</span><span class="cx">         uint32_t status;
</span><span class="cx"> };
</span><span class="cx"> 
</span><ins>+/* DisplayPriNotifyMessage */
+#define DISPLAY_PRI_NOTIFY_MESSAGE 0x0120
+struct display_pri_notify_message {
+        uint32_t message_timeout;
+        uint32_t priority;
+        char notify[32];
+};
+
</ins><span class="cx"> /* ServiceUrlStatMessage */
</span><span class="cx"> #define SERVICE_URL_STAT_RES_MESSAGE 0x012F
</span><span class="cx"> struct service_url_stat_res_message {
</span><span class="lines">@@ -487,6 +499,7 @@
</span><span class="cx"> 
</span><span class="cx"> union skinny_data {
</span><span class="cx">         struct register_message reg;
</span><ins>+        struct port_message port;
</ins><span class="cx">         struct keypad_button_message keypad_button;
</span><span class="cx">         struct stimulus_message stimulus;
</span><span class="cx">         struct off_hook_message off_hook;
</span><span class="lines">@@ -529,6 +542,7 @@
</span><span class="cx">         struct unregister_ack_message unregister_ack;
</span><span class="cx">         struct dialed_number_message dialed_number;
</span><span class="cx">         struct feature_stat_res_message feature_res;
</span><ins>+        struct display_pri_notify_message display_pri_notify;
</ins><span class="cx">         struct service_url_stat_res_message service_url_res;
</span><span class="cx">         
</span><span class="cx">         uint16_t as_uint16;
</span><span class="lines">@@ -598,16 +612,24 @@
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Received Too Short Skinny Message (Expected %&quot; SWITCH_SIZE_T_FMT &quot;, got %d).\n&quot;, len+4, message-&gt;length);\
</span><span class="cx">                 return SWITCH_STATUS_FALSE;\
</span><span class="cx">         }
</span><ins>+#define skinny_check_data_length_soft(message, len) \
+    (message-&gt;length &gt;= len+4)
</ins><span class="cx"> 
</span><span class="cx"> switch_status_t skinny_read_packet(listener_t *listener, skinny_message_t **req);
</span><span class="cx"> 
</span><span class="cx"> switch_status_t skinny_device_event(listener_t *listener, switch_event_t **ev, switch_event_types_t event_id, const char *subclass_name);
</span><span class="cx"> 
</span><del>-switch_status_t skinny_send_call_info(switch_core_session_t *session);
</del><ins>+switch_status_t skinny_send_call_info(switch_core_session_t *session, listener_t *listener, uint32_t line_instance);
+switch_status_t skinny_session_walk_lines(skinny_profile_t *profile, char *channel_uuid, switch_core_db_callback_func_t callback, void *data);
+switch_call_cause_t skinny_ring_lines(private_t *tech_pvt);
</ins><span class="cx"> 
</span><del>-switch_status_t skinny_create_session(listener_t *listener, uint32_t line, uint32_t to_state);
-switch_status_t skinny_process_dest(listener_t *listener, uint32_t line);
-switch_status_t skinny_answer(switch_core_session_t *session);
</del><ins>+switch_status_t skinny_create_ingoing_session(listener_t *listener, uint32_t *line_instance, switch_core_session_t **session);
+switch_status_t skinny_session_process_dest(switch_core_session_t *session, listener_t *listener, uint32_t line_instance, char *dest, char append_dest, uint32_t backspace);
+switch_status_t skinny_session_ring_out(switch_core_session_t *session, listener_t *listener, uint32_t line_instance);
+switch_status_t skinny_session_answer(switch_core_session_t *session, listener_t *listener, uint32_t line_instance);
+switch_status_t skinny_session_start_media(switch_core_session_t *session, listener_t *listener, uint32_t line_instance);
+switch_status_t skinny_session_hold_line(switch_core_session_t *session, listener_t *listener, uint32_t line_instance);
+switch_status_t skinny_session_unhold_line(switch_core_session_t *session, listener_t *listener, uint32_t line_instance);
</ins><span class="cx"> 
</span><span class="cx"> void skinny_line_get(listener_t *listener, uint32_t instance, struct line_stat_res_message **button);
</span><span class="cx"> void skinny_speed_dial_get(listener_t *listener, uint32_t instance, struct speed_dial_stat_res_message **button);
</span><span class="lines">@@ -631,7 +653,8 @@
</span><span class="cx"> switch_status_t set_ringer(listener_t *listener,
</span><span class="cx">         uint32_t ring_type,
</span><span class="cx">         uint32_t ring_mode,
</span><del>-        uint32_t unknown);
</del><ins>+        uint32_t line_instance,
+        uint32_t call_id);
</ins><span class="cx"> switch_status_t set_lamp(listener_t *listener,
</span><span class="cx">         uint32_t stimulus,
</span><span class="cx">         uint32_t stimulus_instance,
</span><span class="lines">@@ -710,8 +733,23 @@
</span><span class="cx">         char called_party[24],
</span><span class="cx">         uint32_t line_instance,
</span><span class="cx">         uint32_t call_id);
</span><ins>+switch_status_t send_display_pri_notify(listener_t *listener,
+        uint32_t message_timeout,
+        uint32_t priority,
+        char *notify);
</ins><span class="cx"> switch_status_t send_reset(listener_t *listener,
</span><span class="cx">         uint32_t reset_type);
</span><span class="cx"> 
</span><span class="cx"> #endif /* _SKINNY_PROTOCOL_H */
</span><span class="cx"> 
</span><ins>+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
+ */
+
</ins></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_skinnyskinny_tablesc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_tables.c (17171 => 17172)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_tables.c        2010-04-01 20:16:27 UTC (rev 17171)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_tables.c        2010-04-01 20:16:36 UTC (rev 17172)
</span><span class="lines">@@ -77,7 +77,7 @@
</span><span class="cx">         {&quot;ResetMessage&quot;, RESET_MESSAGE},
</span><span class="cx">         {&quot;KeepAliveAckMessage&quot;, KEEP_ALIVE_ACK_MESSAGE},
</span><span class="cx">         {&quot;OpenReceiveChannelMessage&quot;, OPEN_RECEIVE_CHANNEL_MESSAGE},
</span><del>-        {&quot;OCloseReceiveChannelMessage&quot;, CLOSE_RECEIVE_CHANNEL_MESSAGE},
</del><ins>+        {&quot;CloseReceiveChannelMessage&quot;, CLOSE_RECEIVE_CHANNEL_MESSAGE},
</ins><span class="cx">         {&quot;SoftKeyTemplateResMessage&quot;, SOFT_KEY_TEMPLATE_RES_MESSAGE},
</span><span class="cx">         {&quot;SoftKeySetResMessage&quot;, SOFT_KEY_SET_RES_MESSAGE},
</span><span class="cx">         {&quot;SelectSoftKeysMessage&quot;, SELECT_SOFT_KEYS_MESSAGE},
</span><span class="lines">@@ -88,6 +88,7 @@
</span><span class="cx">         {&quot;UnregisterAckMessage&quot;, UNREGISTER_ACK_MESSAGE},
</span><span class="cx">         {&quot;DialedNumberMessage&quot;, DIALED_NUMBER_MESSAGE},
</span><span class="cx">         {&quot;FeatureResMessage&quot;, FEATURE_STAT_RES_MESSAGE},
</span><ins>+        {&quot;DisplayPriNotifyMessage&quot;, DISPLAY_PRI_NOTIFY_MESSAGE},
</ins><span class="cx">         {&quot;ServiceUrlStatMessage&quot;, SERVICE_URL_STAT_RES_MESSAGE},
</span><span class="cx">         {NULL, 0}
</span><span class="cx"> };
</span><span class="lines">@@ -168,13 +169,13 @@
</span><span class="cx">         {&quot;RingIn&quot;, SKINNY_RING_IN},
</span><span class="cx">         {&quot;Connected&quot;, SKINNY_CONNECTED},
</span><span class="cx">         {&quot;Busy&quot;, SKINNY_BUSY},
</span><del>-        {&quot;Congestion&quot;, SKINNY_CONGESTION},
</del><ins>+        {&quot;LineInUse&quot;, SKINNY_LINE_IN_USE},
</ins><span class="cx">         {&quot;Hold&quot;, SKINNY_HOLD},
</span><span class="cx">         {&quot;CallWaiting&quot;, SKINNY_CALL_WAITING},
</span><span class="cx">         {&quot;CallTransfer&quot;, SKINNY_CALL_TRANSFER},
</span><span class="cx">         {&quot;CallPark&quot;, SKINNY_CALL_PARK},
</span><span class="cx">         {&quot;Proceed&quot;, SKINNY_PROCEED},
</span><del>-        {&quot;CallRemoteMultiline&quot;, SKINNY_CALL_REMOTE_MULTILINE},
</del><ins>+        {&quot;InUseRemotely&quot;, SKINNY_IN_USE_REMOTELY},
</ins><span class="cx">         {&quot;InvalidNumber&quot;, SKINNY_INVALID_NUMBER},
</span><span class="cx">         {NULL, 0}
</span><span class="cx"> };
</span><span class="lines">@@ -189,3 +190,14 @@
</span><span class="cx"> SKINNY_DECLARE_ID2STR(skinny_device_reset_type2str, SKINNY_DEVICE_RESET_TYPES, &quot;DeviceResetTypeUnknown&quot;)
</span><span class="cx"> SKINNY_DECLARE_STR2ID(skinny_str2device_reset_type, SKINNY_DEVICE_RESET_TYPES, -1)
</span><span class="cx"> 
</span><ins>+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
+ */
+
</ins></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_skinnyskinny_tablesh"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_tables.h (17171 => 17172)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_tables.h        2010-04-01 20:16:27 UTC (rev 17171)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/skinny_tables.h        2010-04-01 20:16:36 UTC (rev 17172)
</span><span class="lines">@@ -84,7 +84,7 @@
</span><span class="cx">         }
</span><span class="cx">         
</span><span class="cx"> 
</span><del>-struct skinny_table SKINNY_MESSAGE_TYPES[55];
</del><ins>+struct skinny_table SKINNY_MESSAGE_TYPES[56];
</ins><span class="cx"> const char *skinny_message_type2str(uint32_t id);
</span><span class="cx"> uint32_t skinny_str2message_type(const char *str);
</span><span class="cx"> #define SKINNY_PUSH_MESSAGE_TYPES SKINNY_DECLARE_PUSH_MATCH(SKINNY_MESSAGE_TYPES)
</span><span class="lines">@@ -210,13 +210,13 @@
</span><span class="cx">         SKINNY_RING_IN = 4,
</span><span class="cx">         SKINNY_CONNECTED = 5,
</span><span class="cx">         SKINNY_BUSY = 6,
</span><del>-        SKINNY_CONGESTION = 7,
</del><ins>+        SKINNY_LINE_IN_USE = 7,
</ins><span class="cx">         SKINNY_HOLD = 8,
</span><span class="cx">         SKINNY_CALL_WAITING = 9,
</span><span class="cx">         SKINNY_CALL_TRANSFER = 10,
</span><span class="cx">         SKINNY_CALL_PARK = 11,
</span><span class="cx">         SKINNY_PROCEED = 12,
</span><del>-        SKINNY_CALL_REMOTE_MULTILINE = 13,
</del><ins>+        SKINNY_IN_USE_REMOTELY = 13,
</ins><span class="cx">         SKINNY_INVALID_NUMBER = 14
</span><span class="cx"> };
</span><span class="cx"> struct skinny_table SKINNY_CALL_STATES[15];
</span><span class="lines">@@ -235,3 +235,14 @@
</span><span class="cx"> 
</span><span class="cx"> #endif /* _SKINNY_TABLES_H */
</span><span class="cx"> 
</span><ins>+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
+ */
+
</ins></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>