diff --git a/reference/c/net/src/meson.build b/reference/c/net/src/meson.build index 203a1cd2..22f5898c 100644 --- a/reference/c/net/src/meson.build +++ b/reference/c/net/src/meson.build @@ -7,3 +7,9 @@ executable('efl_reference_net_io', install : true ) +executable('efl_reference_net_session', + files(['net_session.c']), + dependencies : deps, + include_directories : inc, + install : true +) diff --git a/reference/c/net/src/net_io.c b/reference/c/net/src/net_io.c index f062a2cf..faf283a3 100644 --- a/reference/c/net/src/net_io.c +++ b/reference/c/net/src/net_io.c @@ -7,7 +7,7 @@ #include /* - * Efl.IO Networked examples. + * Efl.Net input/output examples. * * This example builds on the core_io example by connecting to a remote server * using a dialer and a command queue. The response is printed to stdout. diff --git a/reference/c/net/src/net_session.c b/reference/c/net/src/net_session.c new file mode 100644 index 00000000..7e403ad2 --- /dev/null +++ b/reference/c/net/src/net_session.c @@ -0,0 +1,138 @@ +#define EFL_EO_API_SUPPORT 1 +#define EFL_BETA_API_SUPPORT 1 + +#include + +#include +#include + +/* + * 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. + */ + +// Convert a session state to a string for printing. +static const char * +_state_name(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 "???"; + } +} + +// Convert a session technology to a string for printing. +static const char * +_technology_name(Efl_Net_Session_Technology tech) +{ + static const char *names[] = { + [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", + }; + + if ((unsigned)tech > sizeof(names)/sizeof(names[0]) + || !names[tech]) + return "???"; + + return names[tech]; +} + +// Tthe callback that indicates some connectivity changed within the session. +// Print information about the session to the console. +static void +_session_changed(void *data EINA_UNUSED, const Efl_Event *event) +{ + Eo *session = event->object; + const char *ip, *netmask, *gateway; + uint8_t prefix; + + printf("Session changed:\n" + " name: '%s'\n" + " state: %s\n" + " technology: %s\n" + " interface: '%s'\n", + efl_net_session_name_get(session), + _state_name(efl_net_session_state_get(session)), + _technology_name(efl_net_session_technology_get(session)), + efl_net_session_interface_get(session)); + + // print out additional information if we have an IPv4 session + efl_net_session_ipv4_get(session, &ip, &netmask, &gateway); + if (ip) + { + printf(" IPv4: %s, gateway=%s, netmask=%s\n", + ip, gateway, netmask); + } + + // print out additional information if we have an IPv6 session + efl_net_session_ipv6_get(session, &ip, &prefix, &netmask, &gateway); + if (ip) + { + printf(" IPv6: %s/%hhu, gateway=%s, netmask=%s\n", + ip, prefix, gateway, netmask); + } +} + +// Quit the app after a timer tick. +static void +_quit_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED) +{ + efl_exit(0); +} + +EAPI_MAIN void +efl_main(void *data EINA_UNUSED, const Efl_Event *ev) +{ + Eina_Bool do_connect = EINA_TRUE; + Eina_Bool require_online = EINA_FALSE; + Efl_Net_Session_Technology technologies; + Efl_Net_Session *session; + + // create a session that watches specifically for ethernet, wifi and bluetooth + technologies = EFL_NET_SESSION_TECHNOLOGY_ETHERNET | + EFL_NET_SESSION_TECHNOLOGY_WIFI | EFL_NET_SESSION_TECHNOLOGY_BLUETOOTH; + session = efl_add(EFL_NET_SESSION_CLASS, efl_loop_main_get(EFL_LOOP_CLASS), + efl_name_set(efl_added, "Example Session"), + // register the change callback for network state + efl_event_callback_add(efl_added, EFL_NET_SESSION_EVENT_CHANGED, + _session_changed, NULL)); + if (!session) + { + EINA_LOG_ERR("Could not create Efl.Net.Session object.\n"); + efl_exit(EXIT_FAILURE); + return; + } + + if (do_connect) + { + printf("Requesting a %s connection.\n", require_online ? "online" : "local"); + efl_net_session_connect(session, require_online, technologies); + } + + printf("The session will remain active while this application runs.\n") + printf("Use ^C (Control + C) to close it\n"); + + // Wait for 10 seconds before exiting this example + efl_add(EFL_LOOP_TIMER_CLASS, ev->object, + efl_loop_timer_interval_set(efl_added, 10.0), + efl_event_callback_add(efl_added, EFL_LOOP_TIMER_EVENT_TICK, + _quit_cb, NULL)); +} +EFL_MAIN()