vpn: Remove wrong include of <glib/ghash.h>
[framework/connectivity/connman.git] / plugins / iwmx.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <string.h>
29
30 #include <glib.h>
31
32 #define CONNMAN_API_SUBJECT_TO_CHANGE
33 #include <connman/plugin.h>
34 #include <connman/device.h>
35 #include <connman/inet.h>
36 #include <connman/log.h>
37
38 #include <WiMaxAPI.h>
39 #include <WiMaxAPIEx.h>
40
41 #include "iwmx.h"
42
43 /*
44  * Connman plugin interface
45  *
46  * This part deals with the connman internals
47  */
48
49 /* WiMAX network driver probe/remove, nops */
50 static int iwmx_cm_network_probe(struct connman_network *nw)
51 {
52         return 0;
53 }
54
55 static void iwmx_cm_network_remove(struct connman_network *nw)
56 {
57 }
58
59 /*
60  * Called by connman when it wants us to tell the device to connect to
61  * the network @network_el; the device is @network_el->parent.
62  *
63  * We do a synchronous call to start the connection; the logic
64  * attached to the status change callback will update the connman
65  * internals once the change happens.
66  */
67 static int iwmx_cm_network_connect(struct connman_network *nw)
68 {
69         int result;
70         struct wmxsdk *wmxsdk;
71         const char *station_name = connman_network_get_identifier(nw);
72
73         wmxsdk = connman_device_get_data(connman_network_get_device(nw));
74         result = iwmx_sdk_connect(wmxsdk, nw);
75         DBG("(nw %p [%s] wmxsdk %p) = %d\n", nw, station_name, wmxsdk, result);
76         return result;
77 }
78
79 /*
80  * Called by connman to have the device @nw->parent
81  * disconnected from @nw.
82  *
83  * We do a synchronous call to start the disconnection; the logic
84  * attached to the status change callback will update the connman
85  * internals once the change happens.
86  */
87 static int iwmx_cm_network_disconnect(struct connman_network *nw)
88 {
89         int result;
90         struct wmxsdk *wmxsdk;
91         const char *station_name = connman_network_get_identifier(nw);
92
93         wmxsdk = connman_device_get_data(connman_network_get_device(nw));
94         result = iwmx_sdk_disconnect(wmxsdk);
95         DBG("(nw %p [%s] wmxsdk %p) = %d\n", nw, station_name, wmxsdk, result);
96         return 0;
97 }
98
99 /*
100  * "Driver" for the networks detected by a device.
101  */
102 static struct connman_network_driver iwmx_cm_network_driver = {
103         .name           = "iwmx",
104         .type           = CONNMAN_NETWORK_TYPE_WIMAX,
105         .probe          = iwmx_cm_network_probe,
106         .remove         = iwmx_cm_network_remove,
107         .connect        = iwmx_cm_network_connect,
108         .disconnect     = iwmx_cm_network_disconnect,
109 };
110
111 /*
112  * A (maybe) new network is available, create/update its data
113  *
114  * If the network is new, we create and register a new element; if it
115  * is not, we reuse the one in the list.
116  *
117  * NOTE:
118  *   wmxsdk->network_mutex has to be locked
119  */
120 struct connman_network *__iwmx_cm_network_available(
121                         struct wmxsdk *wmxsdk, const char *station_name,
122                         const void *sdk_nspname, size_t sdk_nspname_size,
123                                                                 int strength)
124 {
125         struct connman_network *nw = NULL;
126         struct connman_device *dev = wmxsdk->dev;
127         char group[3 * strlen(station_name) + 1];
128         unsigned cnt;
129
130         nw = connman_device_get_network(dev, station_name);
131         if (nw == NULL) {
132                 DBG("new network %s", station_name);
133                 nw = connman_network_create(station_name,
134                                             CONNMAN_NETWORK_TYPE_WIMAX);
135                 connman_network_register(nw);
136                 connman_network_set_index(nw, connman_device_get_index(dev));
137                 connman_network_set_name(nw, station_name);
138                 connman_network_set_blob(nw, "WiMAX.NSP.name",
139                                          sdk_nspname, sdk_nspname_size);
140                 /* FIXME: add roaming info? */
141                 /* Set the group name -- this has to be a unique
142                  * [a-zA-Z0-9_] string common to all the networks that
143                  * are actually the same provider. In WiMAX each
144                  * network from the CAPI is a single provider, so we
145                  * just set this as the network name, encoded in
146                  * hex. */
147                 for (cnt = 0; station_name[cnt] != 0; cnt++)
148                         sprintf(group + 3 * cnt, "%02x", station_name[cnt]);
149                 group[3 * cnt + 1] = 0;
150                 connman_network_set_group(nw, station_name);
151                 if (connman_device_add_network(dev, nw) < 0) {
152                         connman_network_unregister(nw);
153                         connman_network_unref(nw);
154                         goto error_add;
155                 }
156         } else
157                 DBG("updating network %s nw %p\n", station_name, nw);
158         connman_network_set_available(nw, TRUE);
159         connman_network_set_strength(nw, strength);
160 error_add:
161         return nw;
162 }
163
164 /*
165  * A new network is available [locking version]
166  *
167  * See __iwmx_cm_network_available() for docs
168  */
169 struct connman_network *iwmx_cm_network_available(
170                         struct wmxsdk *wmxsdk, const char *station_name,
171                         const void *sdk_nspname, size_t sdk_nspname_size,
172                                                                 int strength)
173 {
174         struct connman_network *nw;
175
176         g_static_mutex_lock(&wmxsdk->network_mutex);
177         nw = __iwmx_cm_network_available(wmxsdk, station_name,
178                                         sdk_nspname, sdk_nspname_size,
179                                         strength);
180         g_static_mutex_unlock(&wmxsdk->network_mutex);
181         return nw;
182 }
183
184 /*
185  * The device has been enabled, make sure connman knows
186  */
187 static void iwmx_cm_dev_enabled(struct wmxsdk *wmxsdk)
188 {
189         struct connman_device *dev = wmxsdk->dev;
190         connman_inet_ifup(connman_device_get_index(dev));
191         connman_device_set_powered(dev, TRUE);
192 }
193
194 /*
195  * The device has been disabled, make sure connman is aware of it.
196  */
197 static void iwmx_cm_dev_disabled(struct wmxsdk *wmxsdk)
198 {
199         struct connman_device *dev = wmxsdk->dev;
200         connman_inet_ifdown(connman_device_get_index(dev));
201         connman_device_set_powered(dev, FALSE);
202 }
203
204 /*
205  * The device has been (externally to connman) connnected to a
206  * network, make sure connman knows.
207  *
208  * When the device is connected to a network, this function is called
209  * to change connman's internal state to reflect the fact.
210  *
211  * If the change came from an external entity, that means that our
212  * connect code wasn't called. Our connect code sets
213  * @wmxsdk->connecting_nw to the network we were connecting
214  * to. If it is unset, it means an external entity forced the device
215  * to connect. In that case, we need to find out which network it was
216  * connected to, and create/lookup a @nw for it.
217  *
218  * Once the nw is set, then we are done.
219  */
220 static void iwmx_cm_dev_connected(struct wmxsdk *wmxsdk)
221 {
222         struct connman_network *nw;
223
224         g_mutex_lock(wmxsdk->connect_mutex);
225         nw = wmxsdk->connecting_nw;
226         if (nw == NULL) {
227                 nw = __iwmx_sdk_get_connected_network(wmxsdk);
228                 if (nw == NULL) {
229                         connman_error("wmxsdk: can't find connected network\n");
230                         goto error_nw_find;
231                 }
232         }
233         wmxsdk->nw = connman_network_ref(nw);
234         wmxsdk->connecting_nw = NULL;
235         connman_network_set_ipv4_method(nw, CONNMAN_IPCONFIG_METHOD_DHCP);
236         connman_network_set_connected(nw, TRUE);
237         DBG("connected to network %s\n",
238             connman_network_get_identifier(nw));
239 error_nw_find:
240         g_mutex_unlock(wmxsdk->connect_mutex);
241 }
242
243 /*
244  * The device has been (externally to connman) disconnnected, make
245  * sure connman knows
246  *
247  * We need to reverse the steps done in iwmx_cm_dev_connected().
248  * If the event was caused by an external entity and we had no record
249  * of being connected to a network...well, bad luck. We'll just
250  * pretend it happened ok.
251  */
252 static void __iwmx_cm_dev_disconnected(struct wmxsdk *wmxsdk)
253 {
254         struct connman_network *nw = wmxsdk->nw;
255
256         if (nw != NULL) {
257                 DBG("disconnected from network %s\n",
258                                         connman_network_get_identifier(nw));
259                 connman_network_set_connected(nw, FALSE);
260                 connman_network_unregister(nw);
261                 connman_network_unref(nw);
262                 wmxsdk->nw = NULL;
263         } else
264                 DBG("disconnected from unknown network\n");
265 }
266
267 /*
268  * The device has been disconnnected, make sure connman knows
269  *
270  * See __iwmx_cm_dev_disconnect() for more information.
271  */
272 static void iwmx_cm_dev_disconnected(struct wmxsdk *wmxsdk)
273 {
274         g_mutex_lock(wmxsdk->connect_mutex);
275         __iwmx_cm_dev_disconnected(wmxsdk);
276         g_mutex_unlock(wmxsdk->connect_mutex);
277 }
278
279 /*
280  * Handle a change in state
281  *
282  * This is were most of the action happens. When the device changes
283  * state, this will catch it (through the state change callback or an
284  * explicit call) and call iwmx_cm_dev_*ed() to indicate to connman what
285  * happened.
286  *
287  * Finally, cache the new device status.
288  */
289 void __iwmx_cm_state_change(struct wmxsdk *wmxsdk,
290                                         WIMAX_API_DEVICE_STATUS __new_status)
291 {
292         WIMAX_API_DEVICE_STATUS __old_status = wmxsdk->status;
293         WIMAX_API_DEVICE_STATUS old_status;
294         WIMAX_API_DEVICE_STATUS new_status;
295
296         /*
297          * Simplify state transition computations.
298          *
299          * For practical effects, some states are the same
300          */
301
302 #if HAVE_IWMXSDK_STATUS_IDLE
303         /* Conection_Idle is the same as Data_Connected */
304         if (__old_status == WIMAX_API_DEVICE_STATUS_Connection_Idle)
305                 old_status = WIMAX_API_DEVICE_STATUS_Data_Connected;
306         else
307                 old_status = __old_status;
308         if (__new_status == WIMAX_API_DEVICE_STATUS_Connection_Idle)
309                 new_status = WIMAX_API_DEVICE_STATUS_Data_Connected;
310         else
311                 new_status = __new_status;
312 #endif /* #if HAVE_IWMXSDK_STATUS_IDLE */
313         /* Radio off: all are just RF_OFF_SW (the highest) */
314         switch (__old_status) {
315         case WIMAX_API_DEVICE_STATUS_RF_OFF_HW_SW:
316         case WIMAX_API_DEVICE_STATUS_RF_OFF_HW:
317         case WIMAX_API_DEVICE_STATUS_RF_OFF_SW:
318                 old_status = WIMAX_API_DEVICE_STATUS_RF_OFF_SW;
319                 break;
320         default:
321                 old_status = __old_status;
322                 break;
323         }
324
325         switch (__new_status) {
326         case WIMAX_API_DEVICE_STATUS_RF_OFF_HW_SW:
327         case WIMAX_API_DEVICE_STATUS_RF_OFF_HW:
328         case WIMAX_API_DEVICE_STATUS_RF_OFF_SW:
329                 new_status = WIMAX_API_DEVICE_STATUS_RF_OFF_SW;
330                 break;
331         default:
332                 new_status = __new_status;
333                 break;
334         }
335
336         /* If no real state change, do nothing */
337         if (old_status == new_status) {
338                 DBG("no state changed\n");
339                 return;
340         } else
341                 DBG("state change from %d (%d: %s) to %d (%d: %s)\n",
342                     old_status, __old_status,
343                     iwmx_sdk_dev_status_to_str(__old_status),
344                     new_status, __new_status,
345                     iwmx_sdk_dev_status_to_str(__new_status));
346
347         /* Cleanup old state */
348         switch (old_status) {
349         case WIMAX_API_DEVICE_STATUS_UnInitialized:
350                 /* This means the plugin is starting but the device is
351                  * in some state already, so we need to update our
352                  * internal knowledge of it. */
353                 if (new_status > WIMAX_API_DEVICE_STATUS_RF_OFF_SW)
354                         iwmx_cm_dev_enabled(wmxsdk);
355                 break;
356         case WIMAX_API_DEVICE_STATUS_RF_OFF_SW:
357                 /* This means the radio is being turned on, so enable
358                  * the device ( unless going to uninitialized). */
359                 if (new_status != WIMAX_API_DEVICE_STATUS_RF_OFF_SW)
360                         iwmx_cm_dev_enabled(wmxsdk);
361                 break;
362         case WIMAX_API_DEVICE_STATUS_Ready:
363                 break;
364         case WIMAX_API_DEVICE_STATUS_Scanning:
365                 break;
366         case WIMAX_API_DEVICE_STATUS_Connecting:
367                 break;
368         case WIMAX_API_DEVICE_STATUS_Data_Connected:
369                 iwmx_cm_dev_disconnected(wmxsdk);
370                 break;
371         default:
372                 connman_error("wmxsdk: unknown old status %d\n", old_status);
373                 return;
374         };
375
376         /* Implement new state */
377         switch (new_status) {
378         case WIMAX_API_DEVICE_STATUS_UnInitialized:
379                 break;
380         case WIMAX_API_DEVICE_STATUS_RF_OFF_SW:
381                 /* This means the radio is being turned off, so
382                  * disable the device unless coming from uninitialized. */
383                 if (old_status != WIMAX_API_DEVICE_STATUS_UnInitialized)
384                         iwmx_cm_dev_disabled(wmxsdk);
385                 break;
386         case WIMAX_API_DEVICE_STATUS_Ready:
387                 break;
388         case WIMAX_API_DEVICE_STATUS_Scanning:
389                 break;
390         case WIMAX_API_DEVICE_STATUS_Connecting:
391                 break;
392         case WIMAX_API_DEVICE_STATUS_Data_Connected:
393                 iwmx_cm_dev_connected(wmxsdk);
394                 break;
395         default:
396                 connman_error("wmxsdk: unknown new status %d\n", old_status);
397                 return;
398         };
399         wmxsdk->status = __new_status;
400 }
401
402 /*
403  * Implement a device state transition [locking version]
404  *
405  * See __iwmx_cm_state_change()
406  */
407 void iwmx_cm_state_change(struct wmxsdk *wmxsdk,
408                                  WIMAX_API_DEVICE_STATUS __new_status)
409 {
410         g_mutex_lock(wmxsdk->status_mutex);
411         __iwmx_cm_state_change(wmxsdk, __new_status);
412         g_mutex_unlock(wmxsdk->status_mutex);
413 }
414
415 /*
416  * Read the cached device status
417  */
418 WIMAX_API_DEVICE_STATUS iwmx_cm_status_get(struct wmxsdk *wmxsdk)
419 {
420         WIMAX_API_DEVICE_STATUS status;
421
422         g_mutex_lock(wmxsdk->status_mutex);
423         status = wmxsdk->status;
424         g_mutex_unlock(wmxsdk->status_mutex);
425         return status;
426 }
427
428 /*
429  * Called by connman when a device is enabled by the user
430  *
431  * We need to turn the radio on; the state change function will poke
432  * the internals.
433  */
434 static int iwmx_cm_enable(struct connman_device *dev)
435 {
436         int result;
437         struct wmxsdk *wmxsdk = connman_device_get_data(dev);
438
439         connman_inet_ifup(connman_device_get_index(dev));
440         result = iwmx_sdk_rf_state_set(wmxsdk, WIMAX_API_RF_ON);
441         return result;
442 }
443
444 /*
445  * Called by connman when a device is disabled by the user
446  *
447  * Simple: just make sure the radio is off; the state change function
448  * will poke the internals.
449  */
450 static int iwmx_cm_disable(struct connman_device *dev)
451 {
452         int result;
453         struct wmxsdk *wmxsdk = connman_device_get_data(dev);
454
455         result = iwmx_sdk_rf_state_set(wmxsdk, WIMAX_API_RF_OFF);
456         connman_inet_ifdown(connman_device_get_index(dev));
457         return 0;
458 }
459
460 /*
461  * Probe deferred call from when the mainloop is idle
462  *
463  * probe() schedules this to be called from the mainloop when idle to
464  * do a device status evaluation. Needed because of an internal race
465  * condition in connman. FIXME: deploy into _probe() when fixed.
466  */
467 static gboolean __iwmx_cm_probe_dpc(gpointer _wmxsdk)
468 {
469         int result;
470         struct wmxsdk *wmxsdk = _wmxsdk;
471         result = iwmx_sdk_get_device_status(wmxsdk);
472         if (result < 0)
473                 connman_error("wmxsdk: can't get status: %d\n", result);
474         else
475                 iwmx_cm_state_change(wmxsdk, result);
476         return FALSE;
477 }
478
479 /*
480  * Called by connman when a new device pops in
481  *
482  * We allocate our private structure, register with the WiMAX API,
483  * open their device, subscribe to all the callbacks.
484  *
485  * At the end, we launch a deferred call (to work around current
486  * connman issues that need to be fixed in the future) and update the
487  * device's status. This allows us to pick up the current status and
488  * adapt connman's idea of the device to it.
489  */
490 static int iwmx_cm_probe(struct connman_device *dev)
491 {
492         int result;
493         struct wmxsdk *wmxsdk = NULL;
494
495         wmxsdk = connman_device_get_data(dev);
496         if (wmxsdk == NULL)
497                 /* not called from a discovery done by the WiMAX
498                  * Network Service, ignore */
499                 return -ENODEV;
500
501         result = iwmx_sdk_setup(wmxsdk);
502         if (result < 0)
503                 goto error_setup;
504
505         /* There is a race condition in the connman core that doesn't
506          * allow us to call this directly and things to work properly
507          * FIXME FIXME FIXME: merge _dpc call in here when connman is fixed */
508         g_idle_add(__iwmx_cm_probe_dpc, wmxsdk);
509         return 0;
510
511         iwmx_sdk_remove(wmxsdk);
512 error_setup:
513         return result;
514 }
515
516 /*
517  * Called when a device is removed from connman
518  *
519  * Cleanup all that is done in _probe. Remove callbacks, unregister
520  * from the WiMAX API.
521  */
522 static void iwmx_cm_remove(struct connman_device *dev)
523 {
524         struct wmxsdk *wmxsdk = connman_device_get_data(dev);
525         iwmx_sdk_remove(wmxsdk);
526 }
527
528 /*
529  * Called by connman to ask the device to scan for networks
530  *
531  * We have set in the WiMAX API the scan result callbacks, so we just
532  * start a simple scan (not a wide one).
533  *
534  * First we obtain the current list of networks and pass it to the
535  * callback processor. Then we start an scan cycle.
536  */
537 static int iwmx_cm_scan(struct connman_device *dev)
538 {
539         struct wmxsdk *wmxsdk = connman_device_get_data(dev);
540         return iwmx_sdk_scan(wmxsdk);
541 }
542
543 /*
544  * Driver for a WiMAX API based device.
545  */
546 static struct connman_device_driver iwmx_cm_device_driver = {
547         .name           = "iwmx",
548         .type           = CONNMAN_DEVICE_TYPE_WIMAX,
549         .probe          = iwmx_cm_probe,
550         .remove         = iwmx_cm_remove,
551         .enable         = iwmx_cm_enable,
552         .disable        = iwmx_cm_disable,
553         .scan           = iwmx_cm_scan,
554 };
555
556 static int iwmx_cm_init(void)
557 {
558         int result;
559
560         result = connman_device_driver_register(&iwmx_cm_device_driver);
561         if (result < 0)
562                 goto error_driver_register;
563         result = connman_network_driver_register(&iwmx_cm_network_driver);
564         if (result < 0)
565                 goto error_network_driver_register;
566         result = iwmx_sdk_api_init();
567         if (result < 0)
568                 goto error_iwmx_sdk_init;
569         return 0;
570
571 error_iwmx_sdk_init:
572         connman_network_driver_unregister(&iwmx_cm_network_driver);
573 error_network_driver_register:
574         connman_device_driver_unregister(&iwmx_cm_device_driver);
575 error_driver_register:
576         return result;
577 }
578
579 static void iwmx_cm_exit(void)
580 {
581         iwmx_sdk_api_exit();
582         connman_network_driver_unregister(&iwmx_cm_network_driver);
583         connman_device_driver_unregister(&iwmx_cm_device_driver);
584 }
585
586 CONNMAN_PLUGIN_DEFINE(iwmx, "Intel WiMAX SDK / Common API plugin",
587                         CONNMAN_VERSION, CONNMAN_PLUGIN_PRIORITY_LOW,
588                                                 iwmx_cm_init, iwmx_cm_exit);