Index: pjproject/trunk/pjsip/src/pjsip-ua/sip_inv.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsip-ua/sip_inv.c	(revision 376)
+++ pjproject/trunk/pjsip/src/pjsip-ua/sip_inv.c	(revision 500)
@@ -159,4 +159,23 @@
 
 /*
+ * Set cause code.
+ */
+void inv_set_cause(pjsip_inv_session *inv, int cause_code,
+		   const pj_str_t *cause_text)
+{
+    if (cause_code > inv->cause) {
+	inv->cause = cause_code;
+	if (cause_text)
+	    pj_strdup(inv->pool, &inv->cause_text, cause_text);
+	else if (cause_code/100 == 2)
+	    inv->cause_text = pj_str("Normal call clearing");
+	else
+	    inv->cause_text = *pjsip_get_status_text(cause_code);
+    }
+}
+
+
+
+/*
  * Send ACK for 2xx response.
  */
@@ -899,5 +918,5 @@
 
     /* Set cause. */
-    inv->cause = st_code;
+    inv_set_cause(inv, st_code, NULL);
 
     /* Forcefully terminate the session if state is not DISCONNECTED */
@@ -1388,5 +1407,5 @@
 
     /* Set cause code. */
-    if (inv->cause==0) inv->cause = st_code;
+    inv_set_cause(inv, st_code, st_text);
 
     /* Create appropriate message. */
@@ -1698,5 +1717,5 @@
 
     if (inv->state != PJSIP_INV_STATE_DISCONNECTED) {
-	if (inv->cause==0) inv->cause=PJSIP_SC_OK;
+	inv_set_cause(inv, PJSIP_SC_OK, NULL);
 	inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
     }
@@ -1714,5 +1733,5 @@
     
     if (e->body.tsx_state.type != PJSIP_EVENT_RX_MSG) {
-	if (inv->cause==0) inv->cause=PJSIP_SC_OK;
+	inv_set_cause(inv, PJSIP_SC_OK, NULL);
 	inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 	return;
@@ -1734,5 +1753,5 @@
 	     * End the session anyway.
 	     */
-	    if (inv->cause==0) inv->cause=PJSIP_SC_OK;
+	    inv_set_cause(inv, PJSIP_SC_OK, NULL);
 	    inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 	    
@@ -1745,5 +1764,5 @@
 
 	/* End the session. */
-	if (inv->cause==0) inv->cause=PJSIP_SC_OK;
+	inv_set_cause(inv, PJSIP_SC_OK, NULL);
 	inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
     }
@@ -1863,5 +1882,5 @@
 		     * End the session.
 		     */
-		    if (inv->cause==0) inv->cause = tsx->status_code;
+		    inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 		    inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
@@ -1878,5 +1897,5 @@
 	    } else {
 
-		if (inv->cause==0) inv->cause = tsx->status_code;
+		inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 		inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 
@@ -1906,5 +1925,5 @@
 
 	    } else  {
-		if (inv->cause==0) inv->cause = tsx->status_code;
+		inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 		inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 	    }
@@ -1927,7 +1946,7 @@
 	    tsx->status_code == PJSIP_SC_REQUEST_TIMEOUT ||
 	    tsx->status_code == PJSIP_SC_TSX_TIMEOUT ||
-	    PJSIP_SC_TSX_TRANSPORT_ERROR)
+	    tsx->status_code == PJSIP_SC_TSX_TRANSPORT_ERROR)
 	{
-	    if (inv->cause==0) inv->cause = tsx->status_code;
+	    inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 	    inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 	}
@@ -1973,5 +1992,5 @@
 		inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e);
 	    } else {
-		if (inv->cause==0) inv->cause = tsx->status_code;
+		inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 		inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 	    }
@@ -1983,5 +2002,5 @@
 	     * response)
 	     */
-	    if (inv->cause==0) inv->cause = tsx->status_code;
+	    inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 	    inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 	    break;
@@ -2045,5 +2064,5 @@
 
 	    } else {
-		if (inv->cause==0) inv->cause = tsx->status_code;
+		inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 		inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 	    }
@@ -2079,5 +2098,5 @@
 
 	    } else  {
-		if (inv->cause==0) inv->cause = tsx->status_code;
+		inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 		inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 	    }
@@ -2113,7 +2132,7 @@
 	    tsx->status_code == PJSIP_SC_REQUEST_TIMEOUT ||
 	    tsx->status_code == PJSIP_SC_TSX_TIMEOUT ||
-	    PJSIP_SC_TSX_TRANSPORT_ERROR)
+	    tsx->status_code == PJSIP_SC_TSX_TRANSPORT_ERROR)
 	{
-	    if (inv->cause==0) inv->cause = tsx->status_code;
+	    inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 	    inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 	}
@@ -2149,5 +2168,5 @@
 	     */
 	    if (tsx->status_code/100 != 2) {
-		if (inv->cause==0) inv->cause = tsx->status_code;
+		inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 		inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 	    }
@@ -2341,5 +2360,5 @@
 	     * Handle responses that terminates dialog.
 	     */
-	    if (inv->cause==0) inv->cause = tsx->status_code;
+	    inv_set_cause(inv, tsx->status_code, &tsx->status_text);
 	    inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e);
 	}
