From: Sipwise Development Team <support@sipwise.com>
Date: Thu, 26 Mar 2020 10:06:46 +0100
Subject: sca-debug

---
 src/modules/sca/sca_appearance.c | 11 ++++++++++-
 src/modules/sca/sca_call_info.c  | 39 +++++++++++++++++++++++++++++++++++++++
 src/modules/sca/sca_dialog.c     | 10 ++++++++++
 3 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/src/modules/sca/sca_appearance.c b/src/modules/sca/sca_appearance.c
index 78ee5d8..dca2421 100644
--- a/src/modules/sca/sca_appearance.c
+++ b/src/modules/sca/sca_appearance.c
@@ -911,8 +911,10 @@ int sca_appearance_update_index(sca_mod *scam, str *aor, int idx, int state,
 		rc = SCA_APPEARANCE_ERR_NOT_IN_USE;
 		goto done;
 	}
-
+	LM_DBG("search for index[%d] dialog.id[%.*s]\n", idx, STR_FMT(&dialog->id));
 	for(app = app_list->appearances; app != NULL; app = app->next) {
+		LM_DBG("app.index[%d] app.dialog.id[%.*s]\n", app->index,
+				STR_FMT(&app->dialog.id));
 		if(app->index == idx) {
 			break;
 		} else if(idx == 0) {
@@ -1157,7 +1159,12 @@ sca_appearance *sca_appearance_for_dialog_unsafe(
 		return (NULL);
 	}
 
+	LM_DBG("search for call_id[%.*s] from_tag[%.*s]\n",
+			STR_FMT(&dialog->call_id), STR_FMT(&dialog->from_tag));
 	for(app = app_list->appearances; app != NULL; app = app->next) {
+		LM_DBG("app.call_id[%.*s] app.from_tag[%.*s] app.to_tag[%.*s]\n",
+				STR_FMT(&app->dialog.call_id), STR_FMT(&app->dialog.from_tag),
+				STR_FMT(&app->dialog.to_tag));
 		if(SCA_STR_EQ(&app->dialog.call_id, &dialog->call_id)
 				&& SCA_STR_EQ(&app->dialog.from_tag, &dialog->from_tag)) {
 #ifdef notdef
@@ -1167,6 +1174,8 @@ sca_appearance *sca_appearance_for_dialog_unsafe(
 				continue;
 			}
 #endif // notdef
+			LM_DBG("%.*s appearance-index %d found\n", STR_FMT(aor),
+					app->index);
 			break;
 		}
 	}
diff --git a/src/modules/sca/sca_call_info.c b/src/modules/sca/sca_call_info.c
index 419df8c..2eb035e 100644
--- a/src/modules/sca/sca_call_info.c
+++ b/src/modules/sca/sca_call_info.c
@@ -975,6 +975,8 @@ int sca_call_info_invite_request_handler(sip_msg_t *msg,
 
 	if(!upstream && !SCA_CALL_INFO_IS_SHARED_CALLER(call_info)) {
 		// caller isn't SCA, no more to do. update callee in reply handler.
+		LM_DBG("caller isn't SCA, no more to do. update callee in reply "
+			   "handler\n");
 		rc = 1;
 		goto done;
 	}
@@ -992,6 +994,7 @@ int sca_call_info_invite_request_handler(sip_msg_t *msg,
 	}
 
 	if(sca_call_is_held(msg)) {
+		LM_DBG("call_is_held\n");
 		state = SCA_APPEARANCE_STATE_HELD;
 		if(call_info->state == SCA_APPEARANCE_STATE_HELD_PRIVATE) {
 			state = SCA_APPEARANCE_STATE_HELD_PRIVATE;
@@ -1000,13 +1003,17 @@ int sca_call_info_invite_request_handler(sip_msg_t *msg,
 		}
 	} else if(!SCA_STR_EMPTY(&to->tag_value)) {
 		// this is a reINVITE from an SCA line that put the call on hold
+		LM_DBG("reINVITE from an SCA line that put the call on hold\n");
 		state = SCA_APPEARANCE_STATE_ACTIVE;
 	} else if(sca_call_info_is_line_seize_reinvite(
 					  msg, call_info, from, to, from_aor, to_aor)) {
+		LM_DBG("line_seize_reinvite\n");
 		rc = sca_call_info_seize_held_call(
 				msg, call_info, from, to, from_aor, to_aor, contact_uri);
 		if(rc <= 0) {
 			goto done;
+		} else {
+			LM_DBG("Initial INVITE\n");
 		}
 	}
 
@@ -1179,6 +1186,8 @@ static int sca_call_info_insert_asserted_identity(
 	memcpy(hdr.s + hdr.len, CRLF, CRLF_LEN);
 	hdr.len += CRLF_LEN;
 
+	LM_DBG("hdr[%.*s](%d) display[%.*s](%d) aor[%.*s](%d)\n", STR_FMT(&hdr),
+			hdr.len, STR_FMT(display), display->len, STR_FMT(&aor), aor.len);
 	// append the PAI header before the sdp body
 	if(insert_new_lump_before(anchor, hdr.s, hdr.len, HDR_PAI_T) == NULL) {
 		LM_ERR("Failed to add PAI header %.*s\n", STR_FMT(&hdr));
@@ -1220,11 +1229,19 @@ static int sca_call_info_invite_reply_200_handler(sip_msg_t *msg,
 
 	if(SCA_CALL_INFO_IS_SHARED_CALLEE(call_info)
 			&& (!SCA_STR_EQ(from_aor, to_aor))) {
+		LM_DBG("SCA_CALL_INFO_IS_SHARED_CALLEE\n");
+		if(sca->rr_api->is_direction(msg, RR_FLOW_DOWNSTREAM) == 0) {
+			LM_DBG("downstream detected\n");
+		}
+		if(sca->rr_api->is_direction(msg, RR_FLOW_UPSTREAM) == 0) {
+			LM_DBG("upstream detected\n");
+		}
 		rc = sca_call_info_uri_update(
 				to_aor, call_info, from, to, contact_uri, &msg->callid->body);
 	}
 
 	if(!SCA_CALL_INFO_IS_SHARED_CALLER(call_info)) {
+		LM_DBG("NOT SCA_CALL_INFO_IS_SHARED_CALLER\n");
 		goto done;
 	}
 
@@ -1252,6 +1269,8 @@ static int sca_call_info_invite_reply_200_handler(sip_msg_t *msg,
 			&from->tag_value, NULL, slot_idx);
 	if(app == NULL) {
 		// no SCA line is involved with this call
+		LM_DBG("no SCA line is involved with this call[%.*s]\n",
+				STR_FMT(from_aor));
 		rc = 1;
 		goto done;
 	}
@@ -1426,6 +1445,8 @@ void sca_call_info_ack_from_handler(sip_msg_t *msg, str *from_aor, str *to_aor)
 			sca_appearance_update_state_unsafe(app, state);
 			// can't send NOTIFYs until we unlock the slot below
 		}
+	} else {
+		LM_DBG("from_aor[%.*s] not shared appearance\n", STR_FMT(from_aor));
 	}
 
 done:
@@ -1593,7 +1614,11 @@ static int sca_call_info_bye_handler(sip_msg_t *msg, sca_call_info *call_info,
 	str *tag = NULL;
 
 	if(msg->first_line.type == SIP_REQUEST) {
+		LM_DBG("SIP_REQUEST\n");
 		if(SCA_CALL_INFO_IS_SHARED_CALLER(call_info)) {
+			LM_DBG("SCA_CALL_INFO_IS_SHARED_CALLER "
+				   "from_aor[%.*s] to_aor[%.*s]\n",
+					STR_FMT(from_aor), STR_FMT(to_aor));
 			slot_idx = sca_uri_lock_shared_appearance(sca, from_aor);
 			if(slot_idx < 0) {
 				LM_ERR("sca_call_info_bye_handler: failed to acquire "
@@ -1647,6 +1672,10 @@ static int sca_call_info_bye_handler(sip_msg_t *msg, sca_call_info *call_info,
 					goto done;
 				}
 			}
+		} else {
+			LM_DBG("NOT SCA_CALL_INFO_IS_SHARED_CALLER "
+				   "from_aor[%.*s] to_aor[%.*s]\n",
+					STR_FMT(from_aor), STR_FMT(to_aor));
 		}
 
 		if(slot_idx >= 0) {
@@ -1702,6 +1731,7 @@ static int sca_call_info_bye_handler(sip_msg_t *msg, sca_call_info *call_info,
 			}
 		}
 	} else {
+		LM_DBG("SIP_REPLY\n");
 		// this is just a backup to catch anything missed on the BYE request
 		if(SCA_CALL_INFO_IS_SHARED_CALLEE(call_info)) {
 			slot_idx = sca_hash_table_index_for_key(sca->appearances, to_aor);
@@ -2087,28 +2117,37 @@ int sca_call_info_update(
 	if(sca_uri_is_shared_appearance(sca, &from_aor)) {
 		if((update_mask & SCA_CALL_INFO_SHARED_CALLER)) {
 			call_info.ua_shared |= SCA_CALL_INFO_SHARED_CALLER;
+			LM_DBG("SCA_CALL_INFO_SHARED_CALLER[%.*s]\n", STR_FMT(&from_aor));
 		}
 	}
 	if(sca_uri_is_shared_appearance(sca, &to_aor)) {
 		if((update_mask & SCA_CALL_INFO_SHARED_CALLEE)) {
 			call_info.ua_shared |= SCA_CALL_INFO_SHARED_CALLEE;
+			LM_DBG("SCA_CALL_INFO_SHARED_CALLEE[%.*s]\n", STR_FMT(&to_aor));
 		}
 	}
 
 	if(call_info_hdr == NULL) {
+		LM_DBG("call_info_hdr is NULL\n");
 		if(SCA_CALL_INFO_IS_SHARED_CALLER(&call_info)
 				&& msg->first_line.type == SIP_REQUEST) {
+			LM_DBG("SCA_CALL_INFO_IS_SHARED_CALLER && SIP_REQUEST\n");
 			if(!sca_subscription_aor_has_subscribers(
 					   SCA_EVENT_TYPE_CALL_INFO, &from_aor)) {
 				call_info.ua_shared &= ~SCA_CALL_INFO_SHARED_CALLER;
 				sca_appearance_unregister(sca, &from_aor);
+			} else {
+				LM_DBG("from_aor[%.*s] has subscribers\n", STR_FMT(&from_aor));
 			}
 		} else if(SCA_CALL_INFO_IS_SHARED_CALLEE(&call_info)
 				  && msg->first_line.type == SIP_REPLY) {
+			LM_DBG("SCA_CALL_INFO_IS_SHARED_CALLEE && SIP_REPLY\n");
 			if(!sca_subscription_aor_has_subscribers(
 					   SCA_EVENT_TYPE_CALL_INFO, &to_aor)) {
 				call_info.ua_shared &= ~SCA_CALL_INFO_SHARED_CALLEE;
 				sca_appearance_unregister(sca, &to_aor);
+			} else {
+				LM_DBG("to_aor[%.*s] has subscribers\n", STR_FMT(&from_aor));
 			}
 		}
 	}
diff --git a/src/modules/sca/sca_dialog.c b/src/modules/sca/sca_dialog.c
index 1076caa..0314159 100644
--- a/src/modules/sca/sca_dialog.c
+++ b/src/modules/sca/sca_dialog.c
@@ -45,6 +45,9 @@ int sca_dialog_build_from_tags(sca_dialog *dialog, int maxlen, str *call_id,
 		return (-1);
 	}
 
+	LM_DBG("call-id:%.*s from_tag:%.*s to_tag:%.*s\n", STR_FMT(call_id),
+			STR_FMT(from_tag), STR_FMT(to_tag));
+
 	memcpy(dialog->id.s, call_id->s, call_id->len);
 	dialog->call_id.s = dialog->id.s;
 	dialog->call_id.len = call_id->len;
@@ -58,9 +61,16 @@ int sca_dialog_build_from_tags(sca_dialog *dialog, int maxlen, str *call_id,
 				to_tag->len);
 		dialog->to_tag.s = dialog->id.s + call_id->len + from_tag->len;
 		dialog->to_tag.len = to_tag->len;
+	} else {
+		dialog->to_tag.s = NULL;
+		dialog->to_tag.len = 0;
 	}
 	dialog->id.len = len;
 
+	LM_DBG("id:%.*s call-id:%.*s from_tag:%.*s to_tag:%.*s\n",
+			STR_FMT(&dialog->id), STR_FMT(&dialog->call_id),
+			STR_FMT(&dialog->from_tag), STR_FMT(&dialog->to_tag));
+
 	return (len);
 }
 
