connectivity-cpp  0.0.1
Networking Status

Applications needing to access the overall system networking status can access the top level com::ubuntu::connectivity::networking::Manager class. The class has properties for the networking status and connection characteristics of the system networking.

Setup

Manager is accessed by including the appropriate header:

namespace networking = com::ubuntu::connectivity::networking;

and then creating an instance of the manager:

auto mgr = networking::Manager::createInstance();

Networking Status

The status of the system networking can be accessed through the Manager::status property:

mgr->status().changed().connect(
[](networking::Manager::NetworkingStatus status)
{
switch(status) {
case networking::Manager::NetworkingStatus::offline:
{
std::cout << "System networking status changed to: offline" << std::endl;
break;
}
case networking::Manager::NetworkingStatus::connecting:
{
std::cout << "System networking status changed to: connecting" << std::endl;
break;
}
case networking::Manager::NetworkingStatus::online:
{
std::cout << "System networking status changed to: online" << std::endl;
break;
}
}
});

Networking Characteristics

The characteristics can be accessed through the Manager::characteristics property as a bitfield:

mgr->characteristics().changed().connect(
[](std::uint32_t characteristics)
{
std::cout << "System networking characteristics changed:" << std::endl;
if ((characteristics & networking::Link::Characteristics::has_monetary_costs) != 0) {
std::cout << " - has monetary costs" << std::endl;
}
if ((characteristics & networking::Link::Characteristics::is_bandwidth_limited) != 0) {
std::cout << " - is bandwith limited" << std::endl;
}
if ((characteristics & networking::Link::Characteristics::is_volume_limited) != 0) {
std::cout << " - is volume_limited" << std::endl;
}
});

The complete example (found in examples/example_networking_status.cpp):

/*
* Copyright © 2013 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser 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 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 program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Antti Kaijanmäki <antti.kaijanmaki@canonical.com>
*/
#include <iostream>
#include <memory>
#include <signal.h>
namespace networking = com::ubuntu::connectivity::networking;
int
main(int, char *[])
{
auto mgr = networking::Manager::createInstance();
// Subscribe to system networking changes
mgr->status().changed().connect(
[](networking::Manager::NetworkingStatus status)
{
switch(status) {
case networking::Manager::NetworkingStatus::offline:
{
std::cout << "System networking status changed to: offline" << std::endl;
break;
}
case networking::Manager::NetworkingStatus::connecting:
{
std::cout << "System networking status changed to: connecting" << std::endl;
break;
}
case networking::Manager::NetworkingStatus::online:
{
std::cout << "System networking status changed to: online" << std::endl;
break;
}
}
});
// Subscribe to characteristics changes
mgr->characteristics().changed().connect(
[](std::uint32_t characteristics)
{
std::cout << "System networking characteristics changed:" << std::endl;
if ((characteristics & networking::Link::Characteristics::has_monetary_costs) != 0) {
std::cout << " - has monetary costs" << std::endl;
}
if ((characteristics & networking::Link::Characteristics::is_bandwidth_limited) != 0) {
std::cout << " - is bandwith limited" << std::endl;
}
if ((characteristics & networking::Link::Characteristics::is_volume_limited) != 0) {
std::cout << " - is volume_limited" << std::endl;
}
});
sigset_t signal_set;
sigemptyset(&signal_set);
sigaddset(&signal_set, SIGINT);
int signal;
sigwait(&signal_set, &signal);
return 0;
}