summaryrefslogtreecommitdiff
path: root/src/dbus/server/connman-client.cpp
blob: c8acf6fc4b71616771564eb2b2c8ecc8916e0892 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (C) 2011 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "connman-client.h"
#include "server.h"
#include "presence-status.h"

SE_BEGIN_CXX

ConnmanClient::ConnmanClient(Server &server):
    DBusRemoteObject(!strcmp(getEnv("DBUS_TEST_CONNMAN", ""), "none") ?
                     NULL : /* simulate missing ConnMan */
                     GDBusCXX::dbus_get_bus_connection(!strcmp(getEnv("DBUS_TEST_CONNMAN", ""), "session") ?
                                                       "SESSION" : /* use our own ConnMan stub */
                                                       "SYSTEM" /* use real ConnMan */,
                                                       NULL, true, NULL),
                     "/", "net.connman.Manager", "net.connman", true),
    m_available(false),
    m_server(server),
    m_propertyChanged(*this, "PropertyChanged")
{
    if (getConnection()) {
        typedef std::map <std::string, boost::variant<std::string> > PropDict;
        GDBusCXX::DBusClientCall1<PropDict>  getProp(*this,"GetProperties");
        getProp.start(boost::bind(&ConnmanClient::getPropCb, this, _1, _2));
        m_propertyChanged.activate(boost::bind(&ConnmanClient::propertyChanged, this, _1, _2));
    }else{
        SE_LOG_DEBUG(NULL, "DBus connection setup for connman failed");
    }
}

void ConnmanClient::getPropCb (const std::map <std::string,
                               boost::variant<std::string> >& props, const string &error){
    if (!error.empty()) {
        m_available = false;
        if (error == "org.freedesktop.DBus.Error.ServiceUnknown") {
            // ensure there is still first set of singal set in case of no
            // connman available
            m_server.getPresenceStatus().updatePresenceStatus (true, PresenceStatus::HTTP_TRANSPORT);
            SE_LOG_DEBUG(NULL, "No connman service available %s", error.c_str());
            return;
        }
        SE_LOG_DEBUG(NULL, "error in connmanCallback %s", error.c_str());
        return;
    }

    m_available = true;
    typedef std::pair <std::string, boost::variant<std::string> > element;
    bool httpPresence = false;
    BOOST_FOREACH (element entry, props) {
        // just check online state, we don't care how about the underlying technology
        if (entry.first == "State") {
            std::string state = boost::get<std::string>(entry.second);
            httpPresence = state == "online";
            break;
        }
    }

    //now delivering the signals
    m_server.getPresenceStatus().updatePresenceStatus (httpPresence, PresenceStatus::HTTP_TRANSPORT);
}

void ConnmanClient::propertyChanged(const std::string &name,
                                    const boost::variant<std::vector<std::string>, std::string> &prop)
{
    if (name == "State") {
        std::string state = boost::get<std::string>(prop);
        m_server.getPresenceStatus().updatePresenceStatus(state == "online",
                                                          PresenceStatus::HTTP_TRANSPORT);
    }
}

SE_END_CXX