summaryrefslogtreecommitdiff
path: root/src/examples/ecore/efl_net_session_example.c
blob: 683f3414e0a0d48c370feef618cd5d3f8e1a9b7b (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <Efl_Net.h>
#include <Ecore_Getopt.h>

static const char *
_state_str(Efl_Net_Session_State state)
{
   static const char *strs[] = {
     [EFL_NET_SESSION_STATE_OFFLINE] = "offline",
     [EFL_NET_SESSION_STATE_LOCAL] = "local",
     [EFL_NET_SESSION_STATE_ONLINE] = "online",
   };

   if ((unsigned)state > sizeof(strs)/sizeof(strs[0])) return "???";
   if (!strs[state]) return "???";
   return strs[state];
}

static const char *
_technology_str(Efl_Net_Session_Technology tech)
{
   static const char *strs[] = {
     [EFL_NET_SESSION_TECHNOLOGY_UNKNOWN] = "none",
     [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(strs)/sizeof(strs[0])) return "???";
   if (!strs[tech]) return "???";
   return strs[tech];
}

static void
_changed(void *data EINA_UNUSED, const Efl_Event *event)
{
   Eo *session = event->object;
   const char *ip, *netmask, *gateway;
   uint8_t prefix;

   printf("INFO: session changed:\n"
          "INFO:  - name: '%s'\n"
          "INFO:  - state: %s\n"
          "INFO:  - technology: %s\n"
          "INFO:  - interface: '%s'\n",
          efl_net_session_network_name_get(session),
          _state_str(efl_net_session_state_get(session)),
          _technology_str(efl_net_session_technology_get(session)),
          efl_net_session_interface_get(session));

   efl_net_session_ipv4_get(session, &ip, &netmask, &gateway);
   if (ip)
     {
        printf("INFO:  - IPv4: %s, gateway=%s, netmask=%s\n",
               ip, gateway, netmask);
     }

   efl_net_session_ipv6_get(session, &ip, &prefix, &netmask, &gateway);
   if (ip)
     {
        printf("INFO:  - IPv6: %s/%hhu, gateway=%s, netmask=%s\n",
               ip, prefix, gateway, netmask);
     }
}

EFL_CALLBACKS_ARRAY_DEFINE(session_events_cbs,
                           { EFL_NET_SESSION_EVENT_CHANGED, _changed });

static const Ecore_Getopt options = {
  "efl_net_session_example", /* program name */
  NULL, /* usage line */
  "1", /* version */
  "(C) 2016 Enlightenment Project", /* copyright */
  "BSD 2-Clause", /* license */
  /* long description, may be multiline and contain \n */
  "Example of Efl_Net_Session to request or monitor a network session for an application.\n",
  EINA_FALSE,
  {
    ECORE_GETOPT_STORE_TRUE('c', "connect", "Require a connection to the internet (See -o/--require-online)."),
    ECORE_GETOPT_STORE_TRUE('o', "require-online", "When connecting (-c/--connect), require connection to the internet. Otherwise a local network connection is enough"),
    ECORE_GETOPT_APPEND('t', "technology", "Bearer technologies to use when connecting (ethernet, wifi, bluetooth, cellular, vpn, gadget or all)", ECORE_GETOPT_TYPE_STR),

    ECORE_GETOPT_VERSION('V', "version"),
    ECORE_GETOPT_COPYRIGHT('C', "copyright"),
    ECORE_GETOPT_LICENSE('L', "license"),
    ECORE_GETOPT_HELP('h', "help"),
    ECORE_GETOPT_SENTINEL
  }
};

static Eo *session = NULL;

EAPI_MAIN void
efl_pause(void *data EINA_UNUSED,
          const Efl_Event *ev EINA_UNUSED)
{
}

EAPI_MAIN void
efl_resume(void *data EINA_UNUSED,
           const Efl_Event *ev EINA_UNUSED)
{
}

EAPI_MAIN void
efl_terminate(void *data EINA_UNUSED,
              const Efl_Event *ev EINA_UNUSED)
{
   /* FIXME: For the moment the main loop doesn't get
      properly destroyed on shutdown which disallow
      relying on parent destroying their children */
   if (session)
     {
        efl_del(session);
        session = NULL;
     }

   fprintf(stderr, "INFO: main loop finished.\n");
}

EAPI_MAIN void
efl_main(void *data EINA_UNUSED,
         const Efl_Event *ev)
{
   char *str;
   Eina_List *techs = NULL;
   Eina_Bool do_connect = EINA_FALSE;
   Eina_Bool require_online = EINA_FALSE;
   Eina_Bool quit_option = EINA_FALSE;
   Efl_Net_Session_Technology technologies = EFL_NET_SESSION_TECHNOLOGY_ALL;
   Ecore_Getopt_Value values[] = {
     ECORE_GETOPT_VALUE_BOOL(do_connect),
     ECORE_GETOPT_VALUE_BOOL(require_online),
     ECORE_GETOPT_VALUE_LIST(techs),

     /* standard block to provide version, copyright, license and help */
     ECORE_GETOPT_VALUE_BOOL(quit_option), /* -V/--version quits */
     ECORE_GETOPT_VALUE_BOOL(quit_option), /* -C/--copyright quits */
     ECORE_GETOPT_VALUE_BOOL(quit_option), /* -L/--license quits */
     ECORE_GETOPT_VALUE_BOOL(quit_option), /* -h/--help quits */
     ECORE_GETOPT_VALUE_NONE /* sentinel */
   };
   int args;

   args = ecore_getopt_parse(&options, values, 0, NULL);
   if (args < 0)
     {
        fputs("ERROR: Could not parse command line options.\n", stderr);
        goto end;
     }

   if (quit_option) goto end;

   if (techs)
     {
        technologies = 0;
        EINA_LIST_FREE(techs, str)
          {
             if (0) {}
#define MAP(X) else if (strcasecmp(str, #X) == 0) technologies |= EFL_NET_SESSION_TECHNOLOGY_ ## X
             MAP(ETHERNET);
             MAP(WIFI);
             MAP(BLUETOOTH);
             MAP(CELLULAR);
             MAP(VPN);
             MAP(GADGET);
#undef MAP
             else fprintf(stderr, "WARNING: unknown technology '%s' ignored.\n", str);
             free(str);
          }
     }

   session = efl_add(EFL_NET_SESSION_CLASS, ev->object,
                     efl_event_callback_array_add(efl_added, session_events_cbs(), NULL));
   if (!session)
     {
        fputs("ERROR: Could not create Efl.Net.Session object.\n", stderr);
        goto end;
     }

   if (do_connect)
     {
        printf("INFO: requesting a %s connection.\n", require_online ? "online" : "local");
        efl_net_session_connect(session, require_online, technologies);
     }

   printf("INFO: the session will active while this application runs. Use ^C (Control + C) to close it\n");

   return ;

 end:
   efl_loop_quit(ev->object, eina_value_int_init(EXIT_FAILURE));
}

EFL_MAIN_EX();