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):
#include <iostream>
#include <memory>
#include <signal.h>
namespace networking = com::ubuntu::connectivity::networking;
int
main(int, char *[])
{
auto mgr = networking::Manager::createInstance();
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;
}
}
});
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;
}