From efb6f574209f0e22dec6b0e91fd55a830dda4501 Mon Sep 17 00:00:00 2001
From: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Date: Tue, 10 Sep 2013 13:22:47 -0400
Subject: [PATCH] * #29865: added presencemanager.cpp to android/ * #29866:
 debugging log

---
 daemon/src/audio/opensl/opensllayer.cpp       |  17 +-
 daemon/src/client/android/callmanager.cpp     |  13 +-
 daemon/src/client/android/client.cpp          |   3 +
 daemon/src/client/android/presencemanager.cpp | 147 ++++++++++++++++++
 daemon/src/client/presencemanager.h           |   2 +-
 5 files changed, 167 insertions(+), 15 deletions(-)
 create mode 100644 daemon/src/client/android/presencemanager.cpp

diff --git a/daemon/src/audio/opensl/opensllayer.cpp b/daemon/src/audio/opensl/opensllayer.cpp
index 4ef6c9dc16..70faa45d41 100644
--- a/daemon/src/audio/opensl/opensllayer.cpp
+++ b/daemon/src/audio/opensl/opensllayer.cpp
@@ -147,8 +147,8 @@ OpenSLLayer::OpenSLLayer()
     , recorderBufferQueue_(0)
     , playbackBufferIndex_(0)
     , recordBufferIndex_(0)
-    , playbackBufferStack_(ANDROID_BUFFER_QUEUE_LENGTH)
-    , recordBufferStack_(ANDROID_BUFFER_QUEUE_LENGTH)
+    , playbackBufferStack_(ANDROID_BUFFER_QUEUE_LENGTH, AudioBuffer(0))
+    , recordBufferStack_(ANDROID_BUFFER_QUEUE_LENGTH, AudioBuffer(0))
 {
 }
 
@@ -448,7 +448,7 @@ OpenSLLayer::startAudioPlayback()
 
         buffer.reset();
 
-        result = (*playbackBufferQueue_)->Enqueue(playbackBufferQueue_, buffer.getData()[0].data(), buffer.size());
+        result = (*playbackBufferQueue_)->Enqueue(playbackBufferQueue_, buffer.getData()[0].data(), buffer.getData()[0].size());
 
         if (SL_RESULT_SUCCESS != result) {
             DEBUG("Error could not enqueue initial buffers\n");
@@ -490,7 +490,8 @@ OpenSLLayer::startAudioCapture()
     buffer.reset();
 
     DEBUG("Enqueue record buffer\n");
-    result = (*recorderBufferQueue_)->Enqueue(recorderBufferQueue_, buffer.getData()[0].data(), buffer.size());
+	DEBUG("buffer.getData()[0].size():%d", buffer.getData()[0].size());
+    result = (*recorderBufferQueue_)->Enqueue(recorderBufferQueue_, buffer.getData()[0].data(), buffer.getData()[0].size());
 
     // the most likely other result is SL_RESULT_BUFFER_INSUFFICIENT,
     // which for this code example would indicate a programming error
@@ -566,9 +567,9 @@ OpenSLLayer::playback(SLAndroidSimpleBufferQueueItf queue)
 
     if (bufferFilled) {
 #ifdef RECORD_AUDIO_TODISK
-        opensl_outfile.write((char const *)(&buffer.getData()), buffer.size());
+        opensl_outfile.write((char const *)(buffer.getData()[0].data()), buffer.getData()[0].size());
 #endif
-        SLresult result = (*queue)->Enqueue(queue, &buffer.getData(), buffer.size());
+        SLresult result = (*queue)->Enqueue(queue, buffer.getData()[0].data(), buffer.getData()[0].size());
 
         if (SL_RESULT_SUCCESS != result) {
             DEBUG("Error could not enqueue buffers in playback callback\n");
@@ -597,14 +598,14 @@ OpenSLLayer::capture(SLAndroidSimpleBufferQueueItf queue)
 
     // enqueue an empty buffer to be filled by the recorder
     // (for streaming recording, we enqueue at least 2 empty buffers to start things off)
-    result = (*recorderBufferQueue_)->Enqueue(recorderBufferQueue_, &buffer.getData(), buffer.size());
+    result = (*recorderBufferQueue_)->Enqueue(recorderBufferQueue_, buffer.getData()[0].data(), buffer.getData()[0].size());
     // the most likely other result is SL_RESULT_BUFFER_INSUFFICIENT,
     // which for this code example would indicate a programming error
     assert(SL_RESULT_SUCCESS == result);
 
     audioCaptureFillBuffer(buffer);
 #ifdef RECORD_AUDIO_TODISK
-    opensl_infile.write((char const *)(&buffer.getData()), buffer.size());
+    opensl_infile.write((char const *)(buffer.getData()[0].data()), buffer.getData()[0].size());
 #endif
 }
 
diff --git a/daemon/src/client/android/callmanager.cpp b/daemon/src/client/android/callmanager.cpp
index 16f62778dc..8935abdeb6 100644
--- a/daemon/src/client/android/callmanager.cpp
+++ b/daemon/src/client/android/callmanager.cpp
@@ -490,15 +490,15 @@ void CallManager::registrationStateChanged(const std::string& accoundID, const s
     on_account_state_changed_with_code_wrapper(accoundID, state, code);
 }
 
-void CallManager::newPresSubClientNotification(const std::string& uri, const std::string& basic,	const std::string& note)
-{
+//void CallManager::newPresSubClientNotification(const std::string& uri, const std::string& basic,	const std::string& note)
+//{
 
-}
+//}
 
-void CallManager::newPresSubServerRequest(const std::string& remote)
-{
+//void CallManager::newPresSubServerRequest(const std::string& remote)
+//{
 
-}
+//}
 
 void CallManager::sipCallStateChanged(const std::string& accoundID, const std::string& state, const int32_t& code)
 {
@@ -507,4 +507,5 @@ void CallManager::sipCallStateChanged(const std::string& accoundID, const std::s
 
 void CallManager::updatePlaybackScale(const std::string&, const int32_t&, const int32_t&)
 {
+
 }
diff --git a/daemon/src/client/android/client.cpp b/daemon/src/client/android/client.cpp
index f4d231b006..69fe9f057a 100644
--- a/daemon/src/client/android/client.cpp
+++ b/daemon/src/client/android/client.cpp
@@ -35,9 +35,11 @@
 #include "client/client.h"
 #include "client/callmanager.h"
 #include "client/configurationmanager.h"
+#include "client/presencemanager.h"
 
 Client::Client() : callManager_(new CallManager)
     , configurationManager_(new ConfigurationManager)
+	, presenceManager_(new PresenceManager)
     , instanceManager_(0)
     , dispatcher_(0)
 #ifdef SFL_VIDEO
@@ -59,6 +61,7 @@ Client::~Client()
     delete dispatcher_;
     delete instanceManager_;
     delete configurationManager_;
+	delete presenceManager_;
     delete callManager_;
 }
 
diff --git a/daemon/src/client/android/presencemanager.cpp b/daemon/src/client/android/presencemanager.cpp
new file mode 100644
index 0000000000..ee9c30607b
--- /dev/null
+++ b/daemon/src/client/android/presencemanager.cpp
@@ -0,0 +1,147 @@
+/*
+ *  Copyright (C) 2013 Savoir-Faire Linux Inc.
+ *  Author: Patrick Keroulas <patrick.keroulas@savoirfairelinux.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
+ *
+ *  Additional permission under GNU GPL version 3 section 7:
+ *
+ *  If you modify this program, or any covered work, by linking or
+ *  combining it with the OpenSSL project's OpenSSL library (or a
+ *  modified version of that library), containing parts covered by the
+ *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
+ *  grants you additional permission to convey the resulting work.
+ *  Corresponding Source for a non-source form of such a combination
+ *  shall include the source code for the parts of OpenSSL used as well
+ *  as that of the covered work.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "client/presencemanager.h"
+
+#include <cerrno>
+#include <sstream>
+
+#include "logger.h"
+#include "sip/sipaccount.h"
+#include "manager.h"
+#include "sip/sippresence.h"
+#include "sip/pres_sub_client.h"
+
+namespace {
+    constexpr static const char* SERVER_PATH    = "/org/sflphone/SFLphone/PresenceManager";
+    constexpr static const char* STATUS_KEY     = "Status";
+    constexpr static const char* LINESTATUS_KEY = "LineStatus";
+    constexpr static const char* ONLINE_KEY     = "Online";
+    constexpr static const char* OFFLINE_KEY    = "Offline";
+}
+
+PresenceManager::PresenceManager()
+{}
+
+/**
+ * Un/subscribe to buddySipUri for an accountID
+ */
+void
+PresenceManager::subscribeBuddy(const std::string& accountID, const std::string& uri, const bool& flag)
+{
+
+    SIPAccount *sipaccount = Manager::instance().getSipAccount(accountID);
+    if (!sipaccount)
+        ERROR("Could not find account %s",accountID.c_str());
+    else{
+        DEBUG("%subscribePresence (acc:%s, buddy:%s)",flag? "S":"Uns", accountID.c_str(), uri.c_str());
+        sipaccount->getPresence()->subscribeClient(uri,flag);
+    }
+}
+
+/**
+ * push a presence for a account
+ * Notify for IP2IP account and publish for PBX account
+ */
+void
+PresenceManager::publish(const std::string& accountID, const bool& status, const std::string& note)
+{
+    SIPAccount *sipaccount = Manager::instance().getSipAccount(accountID);
+    if (!sipaccount)
+        ERROR("Could not find account %s.",accountID.c_str());
+    else{
+        DEBUG("Send Presence (acc:%s, status %s).",accountID.c_str(),status? "online":"offline");
+        sipaccount->getPresence()->sendPresence(status, note);
+    }
+}
+
+/**
+ * Accept or not a PresSubServer request for IP2IP account
+ */
+void
+PresenceManager::answerServerRequest(const std::string& uri, const bool& flag)
+{
+    SIPAccount *sipaccount = Manager::instance().getIP2IPAccount();
+    if (!sipaccount)
+        ERROR("Could not find account IP2IP");
+    else{
+        DEBUG("Approve presence (acc:IP2IP, serv:%s, flag:%s)", uri.c_str(), flag? "true":"false");
+        sipaccount->getPresence()->approvePresSubServer(uri, flag);
+    }
+}
+
+/**
+ * Get all active subscriptions for "accountID"
+ */
+std::vector<std::map<std::string, std::string> >
+PresenceManager::getSubscriptions(const std::string& accountID)
+{
+    std::vector<std::map<std::string, std::string> > ret;
+    SIPAccount *sipaccount = Manager::instance().getSipAccount(accountID);
+    if (sipaccount) {
+        for (auto s : sipaccount->getPresence()->getClientSubscriptions()) {
+            std::map<std::string, std::string> sub;
+            sub[ STATUS_KEY     ] = s->isPresent()?ONLINE_KEY:OFFLINE_KEY;
+            sub[ LINESTATUS_KEY ] = s->getLineStatus();
+            ret.push_back(sub);
+        }
+    }
+    return ret;
+}
+
+/**
+ * Batch subscribing of URIs
+ */
+void
+PresenceManager::setSubscriptions(const std::string& accountID, const std::vector<std::string>& uris)
+{
+    SIPAccount *sipaccount = Manager::instance().getSipAccount(accountID);
+    for (auto u:uris) {
+        sipaccount->getPresence()->subscribeClient(u,true);
+    }
+}
+
+
+
+void
+PresenceManager::newBuddySubscription(const std::string& uri, const std::string& basic, const std::string& note)
+{
+
+}
+
+void
+PresenceManager::newServerSubscriptionRequest(const std::string& remote)
+{
+
+}
diff --git a/daemon/src/client/presencemanager.h b/daemon/src/client/presencemanager.h
index 30d8890571..c49230ee0b 100644
--- a/daemon/src/client/presencemanager.h
+++ b/daemon/src/client/presencemanager.h
@@ -77,7 +77,7 @@ class PresenceManager
 #endif
 
 #ifdef __ANDROID__
-    void newBuddySubscription(const std::string& uri, const std::string& basic,     const std::string& note);
+    void newBuddySubscription(const std::string& uri, const std::string& basic, const std::string& note);
     void newServerSubscriptionRequest(const std::string& remote);
 #endif //__ANDROID__
 
-- 
GitLab