From 9df4c61950dce3c98d7cc10c1c87cfcf1cfbced0 Mon Sep 17 00:00:00 2001
From: Patrick Keroulas <patrick.keroulas@savoirfairelinux.com>
Date: Wed, 21 Aug 2013 10:28:17 -0400
Subject: [PATCH] * #28472 : add presenceSubcribe confirm functions,
 unhardacode variables.

---
 daemon/src/client/callmanager.h               |  2 +-
 .../src/client/dbus/callmanager-introspec.xml |  2 +-
 daemon/src/client/dbus/callmanager.cpp        |  4 +-
 daemon/src/sip/presence_subscription.cpp      | 31 +++---
 daemon/src/sip/presence_subscription.h        |  1 -
 daemon/src/sip/sipbuddy.cpp                   | 15 ++-
 daemon/src/sip/sipbuddy.h                     |  2 +-
 daemon/src/sip/sippresence.cpp                | 94 +++++++++++--------
 daemon/src/sip/sippresence.h                  | 10 +-
 daemon/src/sip/sippublish.cpp                 | 10 +-
 10 files changed, 91 insertions(+), 80 deletions(-)

diff --git a/daemon/src/client/callmanager.h b/daemon/src/client/callmanager.h
index 57eea4c938..5209243b6f 100644
--- a/daemon/src/client/callmanager.h
+++ b/daemon/src/client/callmanager.h
@@ -180,7 +180,7 @@ class CallManager
         void sipCallStateChanged(const std::string&, const std::string&, const int32_t&);
 #endif // __ANDROID__
 
-        /* Presence subscription */
+        /* Presence subscription/Notification. */
         void subscribePresence(const std::string& accountID, const std::string& buddySipUri);
         void unsubscribePresence(const std::string& accountID, const std::string& buddySipUri);
         void sendPresence(const std::string& accountID, const std::string& status, const std::string& note);
diff --git a/daemon/src/client/dbus/callmanager-introspec.xml b/daemon/src/client/dbus/callmanager-introspec.xml
index 47a22e47f7..152f622d7d 100644
--- a/daemon/src/client/dbus/callmanager-introspec.xml
+++ b/daemon/src/client/dbus/callmanager-introspec.xml
@@ -840,7 +840,7 @@
         </method>
 
         <method name="confirmPresenceSubscription" tp:name-for-bindings="confirmPresenceSubscription">
-          <arg type="b" name="confirm"/>
+          <arg type="b" name="confirm" direction="in"/>
         </method>
 
         <signal name="newPresenceNotification" tp:name-for-bindings="newPresenceNotification">
diff --git a/daemon/src/client/dbus/callmanager.cpp b/daemon/src/client/dbus/callmanager.cpp
index 4178ada940..c9502ee4be 100644
--- a/daemon/src/client/dbus/callmanager.cpp
+++ b/daemon/src/client/dbus/callmanager.cpp
@@ -438,7 +438,6 @@ CallManager::unsubscribePresence(const std::string& accountID, const std::string
 }
 
 void
-
 CallManager::sendPresence(const std::string& accountID, const std::string& status, const std::string& note)
 {
   DEBUG("sendPresence (acc:%s, status:%s).",accountID.c_str(),status.c_str());
@@ -446,9 +445,8 @@ CallManager::sendPresence(const std::string& accountID, const std::string& statu
 }
 
 void
-
 CallManager::confirmPresenceSubscription(const bool& confirm)
 {
-  DEBUG("confirmPresenceSubscription : %s",confirm);
+  DEBUG("confirmPresenceSubscription :");
   Manager::instance().confirmPresenceSubscription(confirm);
 }
diff --git a/daemon/src/sip/presence_subscription.cpp b/daemon/src/sip/presence_subscription.cpp
index d568de2804..f94d154846 100644
--- a/daemon/src/sip/presence_subscription.cpp
+++ b/daemon/src/sip/presence_subscription.cpp
@@ -50,7 +50,7 @@ void pres_evsub_on_srv_state(pjsip_evsub *sub, pjsip_event *event) {
     SIPPresence * pres = Manager::instance().getSipAccount("IP2IP")->getPresence();
     pres->lock();
     PresenceSubscription *presenceSub = (PresenceSubscription *) pjsip_evsub_get_mod_data(sub,pres->getModId());
-    WARN("Presence server subscription to %s is %s", presenceSub->remote, pjsip_evsub_get_state_name(sub));
+    DEBUG("Presence server subscription to %s is %s", presenceSub->remote, pjsip_evsub_get_state_name(sub));
 
     if (presenceSub) {
         pjsip_evsub_state state;
@@ -152,7 +152,9 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
     pjsip_evsub_set_mod_data(sub, pres->getModId(), presenceSub);
 
     /* Need client approvement.*/
-    pres->reportNewServerSubscription(presenceSub);
+    //pres->reportNewServerSubscription(presenceSub);
+    pres->addServerSubscription(presenceSub);
+
 
     /* Capture the value of Expires header. */
     expires_hdr = (pjsip_expires_hdr*) pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
@@ -195,7 +197,7 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
     tdata = NULL;
     status = pjsip_pres_notify(sub, ev_state, &stateStr, &reason, &tdata);
     if (status == PJ_SUCCESS) {
-        pres_process_msg_data(tdata, &msg_data);
+        pres->fillDoc(tdata, &msg_data);
         status = pjsip_pres_send_request(sub, tdata);
     }
 
@@ -211,24 +213,13 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
 
 
 
-
-
-
-
-
-
-
-
-
-PresenceSubscription::PresenceSubscription(SIPPresence * pres, pjsip_evsub *evsub, char *r,pjsip_dialog *d)
-    :sub(evsub)
-    , pres_(pres)
+PresenceSubscription::PresenceSubscription(SIPPresence * pres, pjsip_evsub *evsub, char *r, pjsip_dialog *d)
+    : pres_(pres)
+    , sub(evsub)
     , remote(r)
     , dlg(d)
     , expires (-1)
-{
-
-}
+{}
 
 
 void PresenceSubscription::setExpires(int ms) {
@@ -273,7 +264,7 @@ void PresenceSubscription::init(){
     pj_str_t stateStr = pj_str("");
     pj_status_t status = pjsip_pres_notify(sub, ev_state, &stateStr, &reason, &tdata);
     if (status == PJ_SUCCESS) {
-        pres_process_msg_data(tdata, &msg_data);
+        pres_->fillDoc(tdata, &msg_data);
         status = pjsip_pres_send_request(sub, tdata);
     }
 
@@ -298,7 +289,7 @@ void PresenceSubscription::notify() {
 
         if (pjsip_pres_current_notify(sub, &tdata) == PJ_SUCCESS) {
             // add msg header and send
-            pres_process_msg_data(tdata, NULL);
+            pres_->fillDoc(tdata, NULL);
             pjsip_pres_send_request(sub, tdata);
         }
         else{
diff --git a/daemon/src/sip/presence_subscription.h b/daemon/src/sip/presence_subscription.h
index f9860e48f3..c1e1eac35c 100644
--- a/daemon/src/sip/presence_subscription.h
+++ b/daemon/src/sip/presence_subscription.h
@@ -75,7 +75,6 @@ public:
     int getExpires();
     bool matches(PresenceSubscription * s);
     bool isActive();
-    //SIPPresence * getPresence();
 
     /**
      * Send the tirst notification.
diff --git a/daemon/src/sip/sipbuddy.cpp b/daemon/src/sip/sipbuddy.cpp
index ddaa5dd0e5..1b5b09d4d9 100644
--- a/daemon/src/sip/sipbuddy.cpp
+++ b/daemon/src/sip/sipbuddy.cpp
@@ -57,14 +57,14 @@
 
 int modId;
 
-static void buddy_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry) {
+void buddy_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry) {
     (void) th;
     SIPBuddy *b = (SIPBuddy *) entry->user_data;
     b->reportPresence();
 }
 
 /* Callback called when *client* subscription state has changed. */
-static void buddy_evsub_on_state(pjsip_evsub *sub, pjsip_event *event) {
+void buddy_evsub_on_state(pjsip_evsub *sub, pjsip_event *event) {
     SIPBuddy *buddy;
 
     PJ_UNUSED_ARG(event);
@@ -194,7 +194,7 @@ static void buddy_evsub_on_state(pjsip_evsub *sub, pjsip_event *event) {
 }
 
 /* Callback when transaction state has changed. */
-static void buddy_evsub_on_tsx_state(pjsip_evsub *sub, pjsip_transaction *tsx, pjsip_event *event) {
+void buddy_evsub_on_tsx_state(pjsip_evsub *sub, pjsip_transaction *tsx, pjsip_event *event) {
 
     SIPBuddy *buddy;
     pjsip_contact_hdr *contact_hdr;
@@ -346,7 +346,7 @@ void SIPBuddy::accept() {
     acc->getPresence()->addBuddy(this);
 }
 
-pj_status_t SIPBuddy::reportPresence() {
+void SIPBuddy::reportPresence() {
 
     //incLock();
     /* callback*/
@@ -377,7 +377,7 @@ pj_status_t SIPBuddy::updateSubscription() {
         WARN("Buddy %s: unsubscribing..", uri.ptr);
         retStatus = pjsip_pres_initiate(sub, 0, &tdata);
         if (retStatus == PJ_SUCCESS) {
-            pres_process_msg_data(tdata, NULL);
+            acc->getPresence()->fillDoc(tdata, NULL);
             /*if (tdata->msg->type == PJSIP_REQUEST_MSG) {
                 const pj_str_t STR_USER_AGENT = {"User-Agent", 10};
                 pj_str_t ua = pj_str(strdup(acc->getUserAgentName().c_str()));
@@ -409,7 +409,7 @@ pj_status_t SIPBuddy::updateSubscription() {
 
     //subscribe
     pjsip_evsub_user pres_callback;
-//    pj_pool_t *tmp_pool = NULL;
+//    pj_pool_t *tmp_pool = NULL; // related to "contact field. TODO: check if this is necessary"
 
     pjsip_tx_data *tdata;
     pj_status_t status;
@@ -480,7 +480,6 @@ pj_status_t SIPBuddy::updateSubscription() {
      */
     if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
         pjsip_tpselector tp_sel;
-
         pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
         pjsip_dlg_set_transport(buddy->dlg, &tp_sel);
     }
@@ -492,7 +491,7 @@ pj_status_t SIPBuddy::updateSubscription() {
 
     /* Set credentials */
     if (acc->cred_cnt) {
-        pjsip_auth_clt_set_credentials(&buddy->dlg->auth_sess,
+        pjsip_auth_clt_set_credentials(&buddy->dlg->auth_sess
                 acc->cred_cnt, acc->cred);
     }
 
diff --git a/daemon/src/sip/sipbuddy.h b/daemon/src/sip/sipbuddy.h
index ace2903028..dbb6853227 100644
--- a/daemon/src/sip/sipbuddy.h
+++ b/daemon/src/sip/sipbuddy.h
@@ -137,7 +137,7 @@ class SIPBuddy {
          * Callback after a presence notification was received.
          * Tranfert info to the SIP account.
          */
-        pj_status_t reportPresence();
+        void reportPresence();
         /**
          * Process the un/subscribe request transmission.
          */
diff --git a/daemon/src/sip/sippresence.cpp b/daemon/src/sip/sippresence.cpp
index ee3ddca859..3debbb7c4d 100644
--- a/daemon/src/sip/sippresence.cpp
+++ b/daemon/src/sip/sippresence.cpp
@@ -41,54 +41,22 @@
 #include "presence_subscription.h"
 #include "sipvoiplink.h"
 
-/**
- * Generic function.
- * Fill the header.
- */
-void pres_process_msg_data(pjsip_tx_data *tdata, const pres_msg_data *msg_data){
-    if (tdata->msg->type == PJSIP_REQUEST_MSG) {
-        const pj_str_t STR_USER_AGENT = pj_str("User-Agent");
-        pjsip_hdr *h;
-        pj_str_t ua = pj_str("SFLPhone");
-        h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool, &STR_USER_AGENT, &ua);
-        pjsip_msg_add_hdr(tdata->msg, h);
-    }
 
-    if(msg_data == NULL)
-        return;
-
-    const pjsip_hdr *hdr;
-    hdr = msg_data->hdr_list.next;
-    while (hdr && hdr != &msg_data->hdr_list) {
-        pjsip_hdr *new_hdr;
-        new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
-        DEBUG("adding header", new_hdr->name.ptr);
-        pjsip_msg_add_hdr(tdata->msg, new_hdr);
-        hdr = hdr->next;
-    }
-
-    if (msg_data->content_type.slen && msg_data->msg_body.slen) {
-        pjsip_msg_body *body;
-        pj_str_t type = pj_str("application");
-        pj_str_t subtype = pj_str("pidf+xml");
-        body = pjsip_msg_body_create(tdata->pool, &type, &subtype, &msg_data->msg_body);
-        tdata->msg->body = body;
-    }
-}
 
+/*TODO : clean this*/
 static pj_caching_pool cp;
 
 SIPPresence::SIPPresence(SIPAccount *acc)
-    : acc_(acc)
-    , pres_status_data()
+    : pres_status_data()
     , online_status()
     , rpid({PJRPID_ELEMENT_TYPE_PERSON,
-            pj_str("20"),
+            pj_str("20"),/*TODO : unhardcode this*/
             PJRPID_ACTIVITY_BUSY,
             pj_str("")})
     , publish_sess()
     , publish_state()
     , publish_enabled(true)
+    , acc_(acc)
     , newPresenceSubscription_(NULL)
     , serverSubscriptions_ ()
     , buddies_ ()
@@ -110,7 +78,10 @@ SIPPresence::SIPPresence(SIPAccount *acc)
 }
 
 
-SIPPresence::~SIPPresence(){}
+SIPPresence::~SIPPresence(){
+    /*TODO free buddy/subcriber lists and the pool*/
+
+}
 
 SIPAccount * SIPPresence::getAccount(){
     return acc_;
@@ -124,11 +95,15 @@ int SIPPresence::getModId(){
     return  ((SIPVoIPLink*) (acc_->getVoIPLink()))->getModId();
 }
 
+pj_pool_t*  SIPPresence::getPool(){
+    return pool_;
+}
+
 void SIPPresence::updateStatus(const std::string &status, const std::string &note){
     pjrpid_element rpid = {PJRPID_ELEMENT_TYPE_PERSON,
             pj_str("20"),
             PJRPID_ACTIVITY_UNKNOWN,
-            pj_str(strdup(note.c_str()))};
+            pj_str(strdup(note.c_str()))}; /*TODO : del strdup*/
 
     /* fill activity if user not available. */
     if(note=="away")
@@ -207,7 +182,7 @@ void SIPPresence::reportNewServerSubscription(PresenceSubscription *s){
 }
 
 void SIPPresence::confirmNewServerSubscription(const bool& confirm){
-    if(newPresenceSubscription_!=NULL)
+    if(newPresenceSubscription_=NULL)
         return;
 
     if(confirm){
@@ -268,3 +243,44 @@ bool SIPPresence::isLocked()
 {
     return mutex_owner_ == pj_thread_this();
 }
+
+void SIPPresence::fillDoc(pjsip_tx_data *tdata, const pres_msg_data *msg_data)
+{
+
+    if (tdata->msg->type == PJSIP_REQUEST_MSG) {
+        const pj_str_t STR_USER_AGENT = pj_str("User-Agent");
+        std::string useragent(acc_->getUserAgentName());
+        pj_str_t pJuseragent = pj_str((char*) useragent.c_str());
+        pjsip_hdr *h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool, &STR_USER_AGENT, &pJuseragent);
+        pjsip_msg_add_hdr(tdata->msg, h);
+
+    /*pjsip_hdr hdr_list;
+    pj_list_init(&hdr_list);
+    std::string useragent(account->getUserAgentName());
+    pj_str_t pJuseragent = pj_str((char*) useragent.c_str());
+    const pj_str_t STR_USER_AGENT = { (char*) "User-Agent", 10 };
+    pjsip_generic_string_hdr *h = pjsip_generic_string_hdr_create(pool_, &STR_USER_AGENT, &pJuseragent);
+    */
+    }
+
+    if(msg_data == NULL)
+        return;
+
+    const pjsip_hdr *hdr;
+    hdr = msg_data->hdr_list.next;
+    while (hdr && hdr != &msg_data->hdr_list) {
+        pjsip_hdr *new_hdr;
+        new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
+        DEBUG("adding header", new_hdr->name.ptr);
+        pjsip_msg_add_hdr(tdata->msg, new_hdr);
+        hdr = hdr->next;
+    }
+
+    if (msg_data->content_type.slen && msg_data->msg_body.slen) {
+        pjsip_msg_body *body;
+        pj_str_t type = pj_str("application");
+        pj_str_t subtype = pj_str("pidf+xml");
+        body = pjsip_msg_body_create(tdata->pool, &type, &subtype, &msg_data->msg_body);
+        tdata->msg->body = body;
+    }
+}
diff --git a/daemon/src/sip/sippresence.h b/daemon/src/sip/sippresence.h
index 98b8978da1..16a359ad8c 100644
--- a/daemon/src/sip/sippresence.h
+++ b/daemon/src/sip/sippresence.h
@@ -84,7 +84,7 @@ struct pres_msg_data
 };
 
 
-extern void pres_process_msg_data(pjsip_tx_data *tdata, const pres_msg_data *msg_data);
+//extern void pres_process_msg_data(pjsip_tx_data *tdata, const pres_msg_data *msg_data);
 
 
 class SIPAccount;
@@ -127,6 +127,14 @@ public:
      * Return presence module ID which is actually the same as the VOIP link
      */
     int getModId();
+    /**
+     *  Return a pool for generic functions.
+     */
+    pj_pool_t*  getPool();
+    /**
+     * Fill xml document, the header and the body
+     */
+    void fillDoc(pjsip_tx_data *tdata, const pres_msg_data *msg_data);
     /**
      * Modify the presence data
      * @param status is basically "open" or "close"
diff --git a/daemon/src/sip/sippublish.cpp b/daemon/src/sip/sippublish.cpp
index 6ea1eec7ab..cf04602b1a 100644
--- a/daemon/src/sip/sippublish.cpp
+++ b/daemon/src/sip/sippublish.cpp
@@ -32,6 +32,7 @@
 #include <pjsip/sip_endpoint.h>
 
 #include "sippresence.h"
+#include "sip_utils.h"
 #include "sippublish.h"
 #include "logger.h"
 #include "sipvoiplink.h"
@@ -95,7 +96,7 @@ pj_status_t pres_send_publish(SIPPresence * pres, pj_bool_t active)
     contactWithAngles.erase(contactWithAngles.find('>'));
     int semicolon = contactWithAngles.find_first_of(":");
     std::string contactWithoutAngles = contactWithAngles.substr(semicolon + 1);
-    pj_str_t contt = pj_str(strdup(contactWithoutAngles.c_str()));
+//    pj_str_t contact = pj_str(strdup(contactWithoutAngles.c_str()));
 //    pj_memcpy(&pres_status_data.info[0].contact, &contt, sizeof(pj_str_t));;
 
     /* Create PUBLISH request */
@@ -148,7 +149,7 @@ pj_status_t pres_send_publish(SIPPresence * pres, pj_bool_t active)
     pjsip_media_type_init(&msg_data.multipart_ctype, NULL, NULL);
     pj_list_init(&msg_data.multipart_parts);
 
-    pres_process_msg_data(tdata, &msg_data);
+    pres->fillDoc(tdata, &msg_data);
 
 
     /* Set Via sent-by */
@@ -227,9 +228,8 @@ pj_status_t pres_publish(SIPPresence *pres)
         }
 
 	/* Set route-set */
-	//pjsip_publishc_set_route_set(pres->publish_sess, &acc->route_set);
-        //if (acc->hasServiceRoute())
-        //    pjsip_regc_set_route_set(regc, sip_utils::createRouteSet(acc->getServiceRoute(), pool_));
+        if (acc->hasServiceRoute())
+            pjsip_regc_set_route_set(acc->getRegistrationInfo(), sip_utils::createRouteSet(acc->getServiceRoute(), pres->getPool()));
 
 	/* Send initial PUBLISH request */
         status = pres_send_publish(pres, PJ_TRUE);
-- 
GitLab