From 0266fde66fcb83c186867a667223b417ee8446d3 Mon Sep 17 00:00:00 2001
From: yanmorin <yanmorin>
Date: Thu, 27 Oct 2005 22:47:59 +0000
Subject: [PATCH] Debugging incoming message It's seems like we don't listen to
 the good port

---
 src/managerimpl.cpp |  17 +--
 src/managerimpl.h   |   2 +-
 src/sipcall.cpp     | 280 ++++++++++++++++++--------------------------
 src/sipcall.h       |   6 +-
 src/sipvoiplink.cpp |  34 +-----
 5 files changed, 131 insertions(+), 208 deletions(-)

diff --git a/src/managerimpl.cpp b/src/managerimpl.cpp
index 3f2d1d1dc8..369cc73879 100644
--- a/src/managerimpl.cpp
+++ b/src/managerimpl.cpp
@@ -679,7 +679,7 @@ ManagerImpl::callCanBeAnswered(CALLID id) {
  * SipEvent Thread
  */
 int 
-ManagerImpl::incomingCall (CALLID id)
+ManagerImpl::incomingCall (CALLID id, const std::string& name, const std::string& number)
 {
   ost::MutexLock m(_mutex);
   Call* call = getCall(id);
@@ -699,9 +699,9 @@ ManagerImpl::incomingCall (CALLID id)
 
   // TODO: Account not yet implemented
   std::string accountId = "acc1";
-  std::string from = call->getCallerIdName();
-  std::string number = call->getCallerIdNumber();
-  if ( number.length() ) {
+  std::string from = name;     call->setCallerIdName(name);
+  call->setCallerIdNumber(number);
+  if ( !number.empty() ) {
     from.append(" <");
     from.append(number);
     from.append(">");
@@ -873,10 +873,11 @@ void
 ManagerImpl::stopTone() {
   _toneMutex.enterMutex();
   _telephoneTone->setCurrentTone(Tone::TONE_NULL);
-//  if ( _toneType != ZT_TONE_NULL ) {
-//    _toneType = ZT_TONE_NULL;
-//    _tone->stopTone();
-//  }
+  // for ringing tone..
+  if ( _toneType != ZT_TONE_NULL ) {
+    _toneType = ZT_TONE_NULL;
+    _tone->stopTone();
+  }
   _toneMutex.leaveMutex();
   getAudioDriver()->stopStream();
 }
diff --git a/src/managerimpl.h b/src/managerimpl.h
index dcfa29a435..dd903e726d 100644
--- a/src/managerimpl.h
+++ b/src/managerimpl.h
@@ -156,7 +156,7 @@ public:
 	bool playTone ();
   void stopTone();
 
-	int incomingCall (CALLID id);
+	int incomingCall (CALLID id, const std::string& name, const std::string& number);
 	void peerAnsweredCall (CALLID id);
 	int peerRingingCall (CALLID id);
 	int peerHungupCall (CALLID id);
diff --git a/src/sipcall.cpp b/src/sipcall.cpp
index 63fe2303f0..c6eb8e8744 100644
--- a/src/sipcall.cpp
+++ b/src/sipcall.cpp
@@ -46,7 +46,6 @@ SipCall::SipCall (CALLID id, CodecDescriptorVector* cdv) : _localIp("127.0.0.1")
   _audiocodec = NULL;
   enable_audio = false;
 
-  _state = 0;
   _local_audio_port = 0;
   _remote_sdp_audio_port = 0;
   _local_sendrecv  = 0;           /* _SENDRECV, _SENDONLY, _RECVONLY */
@@ -158,7 +157,6 @@ SipCall::newIncomingCall (eXosip_event_t *event) {
   if (_did < 1 && _cid < 1) {
     return -1; /* not enough information for this event?? */
   }
-
   osip_strncpy (_textinfo, event->textinfo, 255);
 
   if (event->response != NULL) {
@@ -167,179 +165,132 @@ SipCall::newIncomingCall (eXosip_event_t *event) {
     _debug("  Status: %d %s\n", _status_code, _reason_phrase);
   }
 
-  	if (event->request != NULL) {
-      	char *tmp = NULL;
-
-      	osip_from_to_str (event->request->from, &tmp);
-      	if (tmp != NULL) {
-          	snprintf (_remote_uri, 255, "%s\n", tmp);
-            _debug("  Remote URI: %s\n", _remote_uri);
-          	osip_free (tmp);
-        }
-    }
+  strcpy(_remote_uri, "");
+  _name = "";
+  _number = "";
+  if (event->request != NULL) {
+    char *tmp = NULL;
 
-  	sdp_message_t *remote_sdp = NULL;
-	/* negotiate payloads */
-  	if (event->request != NULL) {
-      	remote_sdp = eXosip_get_sdp_info (event->request);
-    }
+    osip_from_to_str(event->request->from, &tmp);
+    if (tmp != NULL) {
+      snprintf (_remote_uri, 255, "%s", tmp);
+      osip_free (tmp);
 
-  	if (remote_sdp == NULL) {
-      	_debug("SipCall::newIncomingCall: missing SDP in INVITE request\n");
+      // Get the name/number
+      osip_from_t *from;
+      osip_from_init(&from);
+      osip_from_parse(from, _remote_uri);
+      _name = osip_from_get_displayname(from);
+      osip_uri_t* url = osip_from_get_url(from); 
+      if ( url != NULL ) {
+        _number = url->username;
+      }
+      osip_from_free(from);
     }
+  }
+  _debug("  Name: %s\n", _name.c_str());
+  _debug("  Number: %s\n", _number.c_str());
+  _debug("  Remote URI: %s\n", _remote_uri);
 
-  	if (remote_sdp != NULL) {      /* TODO: else build an offer */
-
-      	if (remote_sdp == NULL) {
-        	_debug("SipCall::newIncomingCall: No remote SDP body found for call\n");
-			// Send 400 BAD REQUEST
-          	eXosip_call_send_answer (_tid, 400, NULL);
-	  		    sdp_message_free (remote_sdp);
-          	return 0;
-        }
+  /* negotiate payloads */
+  sdp_message_t *remote_sdp = NULL;
+  if (event->request != NULL) {
+    remote_sdp = eXosip_get_sdp_info (event->request);
+  }
+  if (remote_sdp == NULL) {
+    _debug("SipCall::newIncomingCall: No remote SDP in INVITE request. Sending 400 BAD REQUEST\n");
+    // Send 400 BAD REQUEST
+    eXosip_call_send_answer (_tid, 400, NULL);
+    return 0;
+  }
+  /* TODO: else build an offer */
 
-        sdp_connection_t *conn = eXosip_get_audio_connection (remote_sdp);
-      	if (conn != NULL && conn->c_addr != NULL) {
-          	snprintf (_remote_sdp_audio_ip, 49, "%s", conn->c_addr);
-            _debug("  Remote Audio IP: %s\n", _remote_sdp_audio_ip);
-        }
-        sdp_media_t *remote_med = eXosip_get_audio_media (remote_sdp);
-
-      	if (remote_med == NULL || remote_med->m_port == NULL) {
-          	/* no audio media proposed */
-			// Send 415 Unsupported media type
-            eXosip_call_send_answer (_tid, 415, NULL);
-            sdp_message_free (remote_sdp);
-          	return 0;
-        }
+  // Remote Media IP
+  sdp_connection_t *conn = eXosip_get_audio_connection (remote_sdp);
+  if (conn != NULL && conn->c_addr != NULL) {
+      snprintf (_remote_sdp_audio_ip, 49, "%s", conn->c_addr);
+      _debug("  Remote Audio IP: %s\n", _remote_sdp_audio_ip);
+  }
 
-      	_remote_sdp_audio_port = atoi (remote_med->m_port);
-        _debug("  Remote Audio Port: %d\n", _remote_sdp_audio_port);
+  // Remote Media Port
+  sdp_media_t *remote_med = eXosip_get_audio_media (remote_sdp);
+  if (remote_med == NULL || remote_med->m_port == NULL) {
+    // no audio media proposed
+    _debug("< Sending 415 Unsupported media type\n");
+    eXosip_lock();
+    eXosip_call_send_answer (_tid, 415, NULL);
+    eXosip_unlock();
+    sdp_message_free (remote_sdp);
+    return 0;
+  }
+  _remote_sdp_audio_port = atoi(remote_med->m_port);
+  _debug("  Remote Audio Port: %d\n", _remote_sdp_audio_port);
 
-    char *tmp = NULL;
-		if (_remote_sdp_audio_port > 0 && _remote_sdp_audio_ip[0] != '\0') {
-			int pos;
-			pos = 0;
-			while (!osip_list_eol (remote_med->m_payloads, pos)) {
-				tmp = (char *) osip_list_get (remote_med->m_payloads, pos);
-				if (tmp != NULL &&
-					(0 == osip_strcasecmp (tmp, "0") 
-					 || 0 == osip_strcasecmp (tmp, "8"))) {
-					break;
-				}
-				tmp = NULL;
-				pos++;
-			}
-		}
-		if (tmp != NULL) {
-			payload = atoi (tmp);
-      _debug("  Payload: %d\n", payload);
-		} else {
-			// Send 415 Unsupported media type
-			eXosip_call_send_answer (_tid, 415, NULL);
-			sdp_message_free (remote_sdp);
-			return 0;
-		}
-
-		if (tmp != NULL && (payload == 0 || payload == 8)
-		   && _remote_sdp_audio_port > 0 && _remote_sdp_audio_ip[0] != '\0') {
-  		}
+  // Remote Payload
+  char *tmp = NULL;
+  if (_remote_sdp_audio_port > 0 && _remote_sdp_audio_ip[0] != '\0') {
+    int pos = 0;
+    while (!osip_list_eol (remote_med->m_payloads, pos)) {
+      tmp = (char *) osip_list_get (remote_med->m_payloads, pos);
+      if (tmp != NULL && ( 0 == osip_strcasecmp (tmp, "0") || 0 == osip_strcasecmp (tmp, "8") )) {
+        break;
+      }
+      tmp = NULL;
+      pos++;
     }
+  }
+  if (tmp != NULL) {
+    payload = atoi (tmp);
+    _debug("  Payload: %d\n", payload);
+    setAudioCodec(_cdv->at(0)->alloc(payload, "")); // codec builder for the mic
+  } else {
+    _debug("< Sending 415 Unsupported media type\n");
+    eXosip_lock();
+    eXosip_call_send_answer (_tid, 415, NULL);
+    eXosip_unlock();
+    sdp_message_free (remote_sdp);
+    return 0;
+  }
 
-  	if (remote_sdp != NULL) {       /* TODO: else build an offer */ 
-      	osip_message_t *answer;
-      	int i;
-
-      	eXosip_lock ();
-        _debug("< Building Answer 183\n");
-      	i = eXosip_call_build_answer (_tid, 183, &answer);
-      	if (i == 0) {
-          	i = sdp_complete_message (remote_sdp, answer);
-          	if (i != 0) {
-              	osip_message_free (answer);
-				// Send 415 Unsupported media type
-              	eXosip_call_send_answer (_tid, 415, NULL);
-                _debug("< Sending Answer 415\n");
-          	} else {
-              	/* start sending audio */
-              	if (enable_audio == true) {
-                  	enable_audio = false;
-                }
-              	if (enable_audio == false)        /* audio is started */ {
-                  	sdp_message_t *local_sdp;
-                  	local_sdp = eXosip_get_sdp_info (answer);
-                  	if (remote_sdp != NULL && local_sdp != NULL) {
-                      	sdp_connection_t *conn;
-                      	sdp_media_t *local_med;
-                      	sdp_media_t *remote_med;
-                      	char *tmp = NULL;
-                      	int audio_port = 0;
-
-                      	conn = eXosip_get_audio_connection (remote_sdp);
-                      	if (conn != NULL && conn->c_addr != NULL) {
-                          	snprintf (_remote_sdp_audio_ip, 49, "%s", conn->c_addr);
-                            _debug("  Remote Audio IP: %s\n", _remote_sdp_audio_ip);
-                        }
-                      	remote_med = eXosip_get_audio_media (remote_sdp);
-                      	if (remote_med != NULL && remote_med->m_port != NULL) {
-                          	_remote_sdp_audio_port = atoi (remote_med->m_port);
-                            _debug("  Remote Audio Port: %d\n", _remote_sdp_audio_port);
-                        }
-                      	local_med = eXosip_get_audio_media (local_sdp);
-                      	if (local_med != NULL && local_med->m_port != NULL) {
-                          	audio_port = atoi (local_med->m_port);
-                            _debug("  Local Audio Port: %d\n", audio_port);
-                        }
-
-                      	if (_remote_sdp_audio_port > 0
-                          && _remote_sdp_audio_ip[0] != '\0'
-                          && local_med != NULL) {
-                          	tmp = (char *) osip_list_get (local_med->m_payloads, 0);
-                        }
-                      if (tmp != NULL) {
-                            payload = atoi (tmp);
-                            _debug("  Remote Payload: %d\n", payload);
-                            setAudioCodec(_cdv->at(0)->alloc(payload, "")); // codec builder for the mic
-                      }
-                    if (tmp != NULL
-                          && audio_port > 0
-                          && _remote_sdp_audio_port > 0
-                          && _remote_sdp_audio_ip[0] != '\0') {
-
-                          	/* search if stream is sendonly or recvonly */
-                          	_remote_sendrecv =
-                            	sdp_analyse_attribute (remote_sdp, remote_med);
-                          	_local_sendrecv =
-                            	sdp_analyse_attribute (local_sdp, local_med);
-                            _debug("  Remote SendRecv: %d\n", _remote_sendrecv);
-                            _debug("  Local  SendRecv: %d\n", _local_sendrecv);
-                          	if (_local_sendrecv == _SENDRECV) {
-                              	if (_remote_sendrecv == _SENDONLY) {
-                                	_local_sendrecv = _RECVONLY;
-                                }
-                              	else if (_remote_sendrecv == _RECVONLY) {
-                                	_local_sendrecv = _SENDONLY;
-                                }
-                            }
-                            _debug("  Final Local SendRecv: %d\n", _local_sendrecv);
-						}
-                    }
-		  			sdp_message_free (local_sdp);
-           		}
-
-               	i = eXosip_call_send_answer (_tid, 183, answer);
-                _debug("  < Sending answer 183\n");
-            }
+  osip_message_t *answer = 0;
+  eXosip_lock();
+  _debug("< Building Answer 183\n");
+  if (0 == eXosip_call_build_answer (_tid, 183, &answer)) {
+    if ( 0 != sdp_complete_message(remote_sdp, answer)) {
+      osip_message_free(answer);
+      // Send 415 Unsupported media type
+      eXosip_call_send_answer (_tid, 415, NULL);
+      _debug("< Sending Answer 415\n");
+    } else {
+      enable_audio = false;
 
-            if (i != 0) {
-              	_debug("SipCall::newIncomingCall: cannot send 183 progress?\n");
-            }
-      	}
-      	eXosip_unlock ();
+      sdp_message_t *local_sdp = eXosip_get_sdp_info(answer);
+      sdp_media_t *local_med = NULL;
+      if (local_sdp != NULL) {
+         local_med = eXosip_get_audio_media(local_sdp);
+      }
+      if (local_sdp != NULL && local_med != NULL) {
+        /* search if stream is sendonly or recvonly */
+        _remote_sendrecv = sdp_analyse_attribute (remote_sdp, remote_med);
+        _local_sendrecv =  sdp_analyse_attribute (local_sdp, local_med);
+        _debug("  Remote SendRecv: %d\n", _remote_sendrecv);
+        _debug("  Local  SendRecv: %d\n", _local_sendrecv);
+        if (_local_sendrecv == _SENDRECV) {
+          if (_remote_sendrecv == _SENDONLY)      { _local_sendrecv = _RECVONLY; }
+          else if (_remote_sendrecv == _RECVONLY) { _local_sendrecv = _SENDONLY; }
+        }
+        _debug("  Final Local SendRecv: %d\n", _local_sendrecv);
+        sdp_message_free (local_sdp);
+      }
+      _debug("  < Sending answer 183\n");
+      if (0 != eXosip_call_send_answer (_tid, 183, answer)) {
+        _debug("SipCall::newIncomingCall: cannot send 183 progress?\n");
+      }
     }
-
-  _state = event->type;
+  }
+  eXosip_unlock ();
   sdp_message_free (remote_sdp);
+
   return 0;
 }
 
@@ -371,7 +322,6 @@ SipCall::ringingCall (eXosip_event_t *event) {
       osip_free (tmp);
     }
   }
-  	this->_state = event->type;;
   	return 0;
 }
 
@@ -380,7 +330,6 @@ SipCall::receivedAck (eXosip_event_t *event)
 {
   _cid = event->cid;
   _did = event->did;
-  _state = event->type;
   return 0;
 }
 
@@ -442,7 +391,6 @@ SipCall::answeredCall(eXosip_event_t *event) {
    }
   	eXosip_unlock ();
 
-	this->_state = event->type;
 	return 0;
 }
 
diff --git a/src/sipcall.h b/src/sipcall.h
index 95626bc963..f8d9904219 100644
--- a/src/sipcall.h
+++ b/src/sipcall.h
@@ -110,6 +110,9 @@ public:
 	inline void setStandBy (bool standby) { _standby = standby; }
 	inline bool getStandBy (void) { return _standby; }
 
+  std::string getName() const { return _name; }
+  std::string getNumber() const { return _number; }
+
 private:
 	void	alloc			(void);
 	void	dealloc			(void);
@@ -137,12 +140,13 @@ private:
   char*	_remote_uri;
   
   char*	_remote_sdp_audio_ip;
-  int  	_state;
   int		_local_audio_port;
   int  	_remote_sdp_audio_port;
   int 	_local_sendrecv;           /* _SENDRECV, _SENDONLY, _RECVONLY */
   int 	_remote_sendrecv;          /* _SENDRECV, _SENDONLY, _RECVONLY */
   std::string _localIp;
+  std::string _name;   // set by incoming call
+  std::string _number; // set by incoming call
 };
 
 #endif // __SIP_CALL_H__
diff --git a/src/sipvoiplink.cpp b/src/sipvoiplink.cpp
index 69311b79de..44a399b7cd 100644
--- a/src/sipvoiplink.cpp
+++ b/src/sipvoiplink.cpp
@@ -649,45 +649,15 @@ SipVoIPLink::getEvent (void)
     Manager::instance().pushBackNewCall(id, Incoming);
     _debug("  ID: %d [cid = %d, did = %d]\n",id, event->cid, event->did);
 
-    // Display the callerId-name
-    osip_from_t *from;
-    osip_from_init(&from);
-
-    sipcall = getSipCall(id);
-    if (event->request != NULL) {
-      char *tmp = NULL;
-
-      osip_from_to_str (event->request->from, &tmp);
-      if (tmp != NULL) {
-        snprintf (sipcall->getRemoteUri(), 256, "%s", tmp);
-        _debug("  Remote URI: %s\n", tmp);
-        osip_free (tmp);
-      }
-    }
-    osip_from_parse(from, sipcall->getRemoteUri());
-    {
-      std::string name = osip_from_get_displayname(from);
-      std::string urlUsername("");
-      osip_uri_t* url = osip_from_get_url(from); 
-      if ( url != NULL ) {
-        urlUsername = url->username;
-      }
-      Manager::instance().callSetInfo(id, name, urlUsername);
-      _debug("   Name/Username: %s/%s\n", name.c_str(), urlUsername.c_str());
-    }
-    //Don't need this display text message now that we send the name
-    //inside the Manager to the gui
-    //Manager::instance().displayTextMessage(id, name);
-    osip_from_free(from);
-
     // Associate an audio port with a call
+    sipcall = getSipCall(id);
     sipcall->setLocalAudioPort(_localPort);
     sipcall->setLocalIp(getLocalIpAddress());
     _debug("  Local listening port: %d\n", _localPort);
     _debug("  Local listening IP: %s\n", getLocalIpAddress().c_str());
 
     sipcall->newIncomingCall(event);
-    if (Manager::instance().incomingCall(id) < 0) {
+    if (Manager::instance().incomingCall(id, sipcall->getName(), sipcall->getNumber()) < 0) {
       Manager::instance().displayErrorText(id, "  Incoming Call Failed");
     }
     break;
-- 
GitLab