136 lines
4.6 KiB
C#
136 lines
4.6 KiB
C#
/*
|
|
* Efl.Net session/connectivity examples.
|
|
*
|
|
* NOTE: This example currently requires the Connman backend to be running.
|
|
*
|
|
* This example uses the Efl.Net.Session APIs to get connectivity information
|
|
* about the current networking setup. It then sets up a callback for network
|
|
* changes and will print the details on any change.
|
|
*/
|
|
|
|
using System;
|
|
|
|
public class Example
|
|
{
|
|
// Convert a session state to a string for printing.
|
|
public static string StateName(efl.net.session.State state)
|
|
{
|
|
switch (state)
|
|
{
|
|
case efl.net.session.State.Offline:
|
|
return "offline";
|
|
case efl.net.session.State.Local:
|
|
return "local";
|
|
case efl.net.session.State.Online:
|
|
return "online";
|
|
default:
|
|
return "???";
|
|
}
|
|
}
|
|
|
|
private static readonly Dictionary<efl.net.session.Technology, string> names =
|
|
new Dictionary<efl.net.session.Technology, string> {
|
|
{ efl.net.session.Technology.Unknown, "unknown" },
|
|
{ efl.net.session.Technology.Ethernet, "ethernet" },
|
|
{ efl.net.session.Technology.Wifi, "wifi" },
|
|
{ efl.net.session.Technology.Bluetooth, "bluetooth" },
|
|
{ efl.net.session.Technology.Cellular, "cellular" },
|
|
{ efl.net.session.Technology.Vpn, "vpn" },
|
|
{ efl.net.session.Technology.Gadget, "gadget" }
|
|
};
|
|
|
|
// Convert a session technology to a string for printing.
|
|
public static string TechnologyName(efl.net.session.Technology tech)
|
|
{
|
|
string name;
|
|
if (!names.TryGetValue(tech, out name))
|
|
return "???";
|
|
return name;
|
|
}
|
|
|
|
// Tthe callback that indicates some connectivity changed within the session.
|
|
// Print information about the session to the console.
|
|
static void SessionChanged(efl.net.Session session, EventArgs e)
|
|
{
|
|
Console.WriteLine("Session changed:");
|
|
Console.WriteLine(" name: '{0}'", session.GetName());
|
|
Console.WriteLine(" state: {0}", StateName(session.GetState()));
|
|
Console.WriteLine(" technology: {0}", TechnologyName(session.GetTechnology()));
|
|
Console.WriteLine(" interface: '{0}'", session.GetInterface());
|
|
|
|
// print out additional information if we have an IPv4 session
|
|
string ip, netmask, gateway;
|
|
session.GetIpv4(out ip, out netmask, out gateway);
|
|
if (ip != null)
|
|
{
|
|
Console.WriteLine($" IPv4: {ip}, gateway={gateway}, netmask={netmask}");
|
|
}
|
|
|
|
// print out additional information if we have an IPv6 session
|
|
byte prefix;
|
|
session.GetIpv6(out ip, out prefix, out netmask, out gateway);
|
|
if (ip != null)
|
|
{
|
|
Console.WriteLine($" IPv6: {ip}/{prefix}, gateway={gateway}, netmask={netmask}");
|
|
}
|
|
}
|
|
|
|
// Quit the app after a timer tick.
|
|
static void QuitCb(object sender, EventArgs e)
|
|
{
|
|
// efl_exit(0); // TODO missing
|
|
efl.ui.Config.Exit();
|
|
}
|
|
|
|
public static void Main()
|
|
{
|
|
bool doConnect = true;
|
|
bool requireOnline = false;
|
|
|
|
efl.All.Init(efl.Components.Basic);
|
|
|
|
// efl.Loop loop = ev->object; // TODO missing
|
|
|
|
// create a session that watches specifically for ethernet, wifi and bluetooth
|
|
int technologies = efl.net.session.Technology.Ethernet |
|
|
efl.net.session.Technology.Wifi | efl.net.session.Technology.Bluetooth;
|
|
|
|
try
|
|
{
|
|
efl.net.Session session = new efl.net.SessionConcrete(loop, (efl.net.Session esession) => {
|
|
esession.SetName("Example Session");
|
|
// register the change callback for network state
|
|
esession.CHANGED += SessionChanged;
|
|
});
|
|
}
|
|
catch
|
|
{
|
|
eina.Log.Error("Could not create Efl.Net.Session object.\n");
|
|
// efl_exit(EXIT_FAILURE); // TODO missing
|
|
efl.ui.Config.Exit();
|
|
throw;
|
|
}
|
|
|
|
if (doConnect)
|
|
{
|
|
Console.WriteLine("Requesting a {0} connection.", requireOnline ? "online" : "local");
|
|
session.Connect(requireOnline, technologies);
|
|
}
|
|
|
|
Console.WriteLine("The session will remain active while this application runs.");
|
|
Console.WriteLine("Use ^C (Control + C) to close it");
|
|
|
|
// Wait for 10 seconds before exiting this example
|
|
new efl.loop.TimerConcrete(loop, (efl.loop.Timer etimer) => {
|
|
etimer.SetInterval(10.0);
|
|
etimer.TICK += QuitCb;
|
|
});
|
|
|
|
// start the main loop
|
|
efl.ui.Config.Run();
|
|
|
|
efl.All.Shutdown();
|
|
}
|
|
}
|
|
|