From 2910e06f48823fe9e5717119d01a59097919bc94 Mon Sep 17 00:00:00 2001
From: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Date: Thu, 1 Mar 2012 15:22:53 -0500
Subject: [PATCH] #9045: fix Changing the account expire is not taken applied
 in daemon

---
 daemon/src/sip/sipaccount.cpp  | 35 +++++++++++++++++++-----------
 daemon/src/sip/sipaccount.h    | 39 +++++++++++++++++++++++++++-------
 daemon/src/sip/sipvoiplink.cpp | 18 ++++++++++++++--
 3 files changed, 70 insertions(+), 22 deletions(-)

diff --git a/daemon/src/sip/sipaccount.cpp b/daemon/src/sip/sipaccount.cpp
index 23848f2b2a..68c81ea32f 100644
--- a/daemon/src/sip/sipaccount.cpp
+++ b/daemon/src/sip/sipaccount.cpp
@@ -497,7 +497,6 @@ std::map<std::string, std::string> SIPAccount::getAccountDetails() const
 
     std::stringstream registrationExpireStr;
     registrationExpireStr << registrationExpire_;
-    DEBUG("Registration expire %s, %s, %s", accountID_.c_str(), registrationExpireStr.str().c_str(), CONFIG_ACCOUNT_REGISTRATION_EXPIRE);
     a[CONFIG_ACCOUNT_REGISTRATION_EXPIRE] = registrationExpireStr.str();
     a[CONFIG_LOCAL_INTERFACE] = interface_;
     a[CONFIG_PUBLISHED_SAMEAS_LOCAL] = publishedSameasLocal_ ? "true" : "false";
@@ -594,6 +593,9 @@ void SIPAccount::startKeepAliveTimer() {
 
     DEBUG("SIP ACCOUNT: start keep alive timer");
 
+    // make sure here we have an entirely new timer
+    memset(&keepAliveTimer_, 0, sizeof(pj_timer_entry));
+
     pj_time_val keepAliveDelay_;
     keepAliveTimer_.cb = &SIPAccount::keepAliveRegistrationCb;
     keepAliveTimer_.user_data = this;
@@ -824,22 +826,31 @@ std::string SIPAccount::getContactHeader() const
 
 void SIPAccount::keepAliveRegistrationCb(UNUSED pj_timer_heap_t *th, pj_timer_entry *te)
 {
-   SIPAccount *sipAccount = reinterpret_cast<SIPAccount *>(te->user_data);
+    SIPAccount *sipAccount = reinterpret_cast<SIPAccount *>(te->user_data);
+
+    if(sipAccount == NULL) {
+        ERROR("Sip account is NULL while registering a new keep alive timer");
+    }
 
-   if (sipAccount->isTlsEnabled())
-       return;
+    // IP2IP default does not require keep-alive
+    if(sipAccount->getAccountID() == IP2IP_PROFILE)
+        return; 
 
-   if(sipAccount->isRegistered()) {
+    // TLS is connection oriented and does not require keep-alive 
+    if (sipAccount->isTlsEnabled())
+        return;
+
+    if(sipAccount->isRegistered()) {
 
-       // send a new register request
-       sipAccount->registerVoIPLink();
+        // send a new register request
+        sipAccount->registerVoIPLink();
 
-       // make sure the current timer is deactivated
-       sipAccount->stopKeepAliveTimer();
+        // make sure the current timer is deactivated
+        sipAccount->stopKeepAliveTimer();
 
-       // register a new timer
-       sipAccount->startKeepAliveTimer();
-   }
+        // register a new timer
+        sipAccount->startKeepAliveTimer();
+    }
 }
 
 namespace {
diff --git a/daemon/src/sip/sipaccount.h b/daemon/src/sip/sipaccount.h
index 0eed0065bd..676f6abea5 100644
--- a/daemon/src/sip/sipaccount.h
+++ b/daemon/src/sip/sipaccount.h
@@ -505,22 +505,25 @@ class SIPAccount : public Account {
     private:
         NON_COPYABLE(SIPAccount);
 
+        /**
+         * Map of credential for this account 
+         */
         std::vector< std::map<std::string, std::string > > credentials_;
 
-        /* Maps a string description of the SSL method
+        /** 
+         * Maps a string description of the SSL method
          * to the corresponding enum value in pjsip_ssl_method.
          * @param method The string representation
          * @return pjsip_ssl_method The corresponding value in the enum
          */
         static pjsip_ssl_method sslMethodStringToPjEnum(const std::string& method);
 
-        /*
+        /**
          * Initializes tls settings from configuration file.
-         *
          */
         void initTlsConfiguration();
 
-        /*
+        /**
          * Initializes STUN config from the config file
          */
         void initStunConfiguration();
@@ -543,21 +546,41 @@ class SIPAccount : public Account {
          */
         bool bRegister_;
 
-        // Network settings
+        /** 
+         * Network settings
+         */
         int registrationExpire_;
 
-        // interface name on which this account is bound
+        /** 
+         * interface name on which this account is bound
+         */
         std::string interface_;
 
-        // Flag which determine if localIpAddress_ or publishedIpAddress_ is used in
-        // sip headers
+        /**
+         * Flag which determine if localIpAddress_ or publishedIpAddress_ is used in
+         * sip headers
+         */
         bool publishedSameasLocal_;
 
+        /**
+         * Published IP address, ued only if defined by the user in account
+         * configuration
+         */
         std::string publishedIpAddress_;
 
+        /**
+         * Local port to whih this account is bound
+         */
         pj_uint16_t localPort_;
+
+        /**
+         * Published port, used only if defined by the user
+         */
         pj_uint16_t publishedPort_;
 
+        /**
+         * Optional list of SIP service this  
+         */
         std::string serviceRoute_;
 
         /**
diff --git a/daemon/src/sip/sipvoiplink.cpp b/daemon/src/sip/sipvoiplink.cpp
index 73518592a9..332f4062b1 100644
--- a/daemon/src/sip/sipvoiplink.cpp
+++ b/daemon/src/sip/sipvoiplink.cpp
@@ -657,9 +657,23 @@ void SIPVoIPLink::registerKeepAliveTimer(pj_timer_entry& timer, pj_time_val& del
 
     DEBUG("UserAgent: Registering keep alive timer");
 
+    if(timer.id == -1) {
+        WARN("UserAgent: Timer already scheduled");
+    }
+
     status = pjsip_endpt_schedule_timer(endpt_, &timer, &delay);
-    if (status != PJ_SUCCESS)
-        ERROR("Could not schedule new timer in pjsip endpoint");
+    if (status != PJ_SUCCESS) {
+        ERROR("UserAgent: Could not schedule new timer in pjsip endpoint");
+    }
+
+    if(status == PJ_EINVAL) {
+        ERROR("UserAgent: Invalid timer or delay entry");
+    }
+
+    if(status == PJ_EINVALIDOP) {
+        ERROR("Invalid timer entry, maybe already scheduled");
+    }
+
 }
 
 void SIPVoIPLink::cancelKeepAliveTimer(pj_timer_entry& timer)
-- 
GitLab