~morphis/aethercast/audio-streaming

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (C) 2015 Canonical, Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include <sstream>

#include <ac/keep_alive.h>
#include <ac/logger.h>
#include <ac/utils.h>

#include "peerstub.h"

namespace w11tng {

PeerStub::Ptr PeerStub::Create(const std::string &object_path) {
    return std::shared_ptr<PeerStub>(new PeerStub)->FinalizeConstruction(object_path);
}

PeerStub::Ptr PeerStub::FinalizeConstruction(const std::string &object_path) {
    auto sp = shared_from_this();

    GError *error = nullptr;
    connection_.reset(g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error));
    if (!connection_) {
        AC_ERROR("Failed to connect to system bus: %s", error->message);
        g_error_free(error);
        return sp;
    }

    wpa_supplicant_peer_proxy_new(connection_.get(), G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
                                  kBusName,
                                  object_path.c_str(),
                                  nullptr,
                                  [](GObject *source, GAsyncResult *res, gpointer user_data) {

        auto inst = static_cast<ac::SharedKeepAlive<PeerStub>*>(user_data)->ShouldDie();

        GError *error = nullptr;
        inst->proxy_.reset(wpa_supplicant_peer_proxy_new_finish(res, &error));
        if (!inst->proxy_) {
            AC_ERROR("Failed to connect with Peer proxy: %s", error->message);
            g_error_free(error);
            return;
        }

        inst->ConnectSignals();
        inst->SyncProperties(false);

        if (auto sp = inst->delegate_.lock())
            sp->OnPeerReady();

    }, new ac::SharedKeepAlive<PeerStub>{shared_from_this()});

    return sp;
}

void PeerStub::OnPropertyChanged(GObject *source, GParamSpec *spec, gpointer user_data) {
    auto inst = static_cast<ac::WeakKeepAlive<PeerStub>*>(user_data)->GetInstance().lock();

    if (not inst)
        return;

    inst->SyncProperties();
}

void PeerStub::ConnectSignals() {
    g_signal_connect_data(proxy_.get(), "notify::device-name",
                          G_CALLBACK(&PeerStub::OnPropertyChanged), new ac::WeakKeepAlive<PeerStub>(shared_from_this()),
                          [](gpointer data, GClosure *) { delete static_cast<ac::WeakKeepAlive<PeerStub>*>(data); },
                          GConnectFlags(0));

    g_signal_connect_data(proxy_.get(), "notify::device-address",
                          G_CALLBACK(&PeerStub::OnPropertyChanged), new ac::WeakKeepAlive<PeerStub>(shared_from_this()),
                          [](gpointer data, GClosure *) { delete static_cast<ac::WeakKeepAlive<PeerStub>*>(data); },
                          GConnectFlags(0));

}

std::string ByteArrayToMacAddress(const gchar *data) {
    if (!data)
        return "";

    std::stringstream ss;
    for (int n = 0; n < 6; n++) {
        ss << ac::Utils::Sprintf("%02x", (uint16_t) (data[n] & 0xff));
        //ss << buf;
        if (n < 5)
            ss << ":";
    }
    return ss.str();
}

std::string PeerStub::RetrieveAddressFromProxy() {
    // NOTE We have to parse the 'DeviceAddress' property here manually as
    // gdbus-codegen doesn't properly give us a value of type 'ay'. Or it
    // could be still that I am to stupid using it but this now works
    // perfectly.

    WpaSupplicantPeerProxy *proxy = WPA_SUPPLICANT_PEER_PROXY(proxy_.get());
    auto variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "DeviceAddress");

    if (!variant)
        return "";

    GVariantIter iter;
    gchar raw_addr[6] = { 0x0 };

    g_variant_iter_init(&iter, variant);

    for (int n = 0; n < 6; n++) {
        if (!g_variant_iter_next(&iter, "y", &raw_addr[n]))
            break;
    }

    auto address = ByteArrayToMacAddress(raw_addr);

    g_variant_unref (variant);

    return address;
}

void PeerStub::SyncProperties(bool update_delegate) {
    if (!proxy_)
        return;

    name_ = wpa_supplicant_peer_get_device_name(proxy_.get()) ? : "";
    address_ = RetrieveAddressFromProxy();

    if (!update_delegate)
        return;

    if (auto sp = delegate_.lock())
        sp->OnPeerChanged();
}

void PeerStub::SetDelegate(const std::weak_ptr<Delegate> &delegate) {
    delegate_ = delegate;
}

void PeerStub::ResetDelegate() {
    delegate_.reset();
}

ac::MacAddress PeerStub::Address() const {
    return address_;
}

std::string PeerStub::Name() const {
    return name_;
}

} // namespace w11tng