419b3c98c9fc46daa2cd7dc422d3c16d6cb08a07
[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_set_index(nw, connman_device_get_index(dev));
136                 connman_network_set_name(nw, station_name);
137                 connman_network_set_blob(nw, "WiMAX.NSP.name",
138                                          sdk_nspname, sdk_nspname_size);
139                 /* FIXME: add roaming info? */
140                 /* Set the group name -- this has to be a unique
141                  * [a-zA-Z0-9_] string common to all the networks that
142                  * are actually the same provider. In WiMAX each
143                  * network from the CAPI is a single provider, so we
144                  * just set this as the network name, encoded in
145                  * hex. */
146                 for (cnt = 0; station_name[cnt] != 0; cnt++)
147                         sprintf(group + 3 * cnt, "%02x", station_name[cnt]);
148                 group[3 * cnt + 1] = 0;
149                 connman_network_set_group(nw, station_name);
150                 if (connman_device_add_network(dev, nw) < 0) {
151                         connman_network_unref(nw);
152                         goto error_add;
153                 }
154         } else
155                 DBG("updating network %s nw %p\n", station_name, nw);
156         connman_network_set_available(nw, TRUE);
157         connman_network_set_strength(nw, strength);
158 error_add:
159         return nw;
160 }
161
162 /*
163  * A new network is available [locking version]
164  *
165  * See __iwmx_cm_network_available() for docs
166  */
167 struct connman_network *iwmx_cm_network_available(
168                         struct wmxsdk *wmxsdk, const char *station_name,
169                         const void *sdk_nspname, size_t sdk_nspname_size,
170                                                                 int strength)
171 {
172         struct connman_network *nw;
173
174         g_static_mutex_lock(&wmxsdk->network_mutex);
175         nw = __iwmx_cm_network_available(wmxsdk, station_name,
176                                         sdk_nspname, sdk_nspname_size,
177                                         strength);
178         g_static_mutex_unlock(&wmxsdk->network_mutex);
179         return nw;
180 }
181
182 /*
183  * The device has been enabled, make sure connman knows
184  */
185 static void iwmx_cm_dev_enabled(struct wmxsdk *wmxsdk)
186 {
187         struct connman_device *dev = wmxsdk->dev;
188         connman_inet_ifup(connman_device_get_index(dev));
189         connman_device_set_powered(dev, TRUE);
190 }
191
192 /*
193  * The device has been disabled, make sure connman is aware of it.
194  */
195 static void iwmx_cm_dev_disabled(struct wmxsdk *wmxsdk)
196 {
197         struct connman_device *dev = wmxsdk->dev;
198         connman_inet_ifdown(connman_device_get_index(dev));
199         connman_device_set_powered(dev, FALSE);
200 }
201
202 /*
203  * The device has been (externally to connman) connnected to a
204  * network, make sure connman knows.
205  *
206  * When the device is connected to a network, this function is called
207  * to change connman's internal state to reflect the fact.
208  *
209  * If the change came from an external entity, that means that our
210  * connect code wasn't called. Our connect code sets
211  * @wmxsdk->connecting_nw to the network we were connecting
212  * to. If it is unset, it means an external entity forced the device
213  * to connect. In that case, we need to find out which network it was
214  * connected to, and create/lookup a @nw for it.
215  *
216  * Once the nw is set, then we are done.
217  */
218 static void iwmx_cm_dev_connected(struct wmxsdk *wmxsdk)
219 {
220         struct connman_network *nw;
221
222         g_mutex_lock(wmxsdk->connect_mutex);
223         nw = wmxsdk->connecting_nw;
224         if (nw == NULL) {
225                 nw = __iwmx_sdk_get_connected_network(wmxsdk);
226                 if (nw == NULL) {
227                         connman_error("wmxsdk: can't find connected network\n");
228                         goto error_nw_find;
229                 }
230         }
231         wmxsdk->nw = connman_network_ref(nw);
232         wmxsdk->connecting_nw = NULL;
233         connman_network_set_ipv4_method(nw, CONNMAN_IPCONFIG_METHOD_DHCP);
234         connman_network_set_connected(nw, TRUE);
235         DBG("connected to network %s\n",
236             connman_network_get_identifier(nw));
237 error_nw_find:
238         g_mutex_unlock(wmxsdk->connect_mutex);
239 }
240
241 /*
242  * The device has been (externally to connman) disconnnected, make
243  * sure connman knows
244  *
245  * We need to reverse the steps done in iwmx_cm_dev_connected().
246  * If the event was caused by an external entity and we had no record
247  * of being connected to a network...well, bad luck. We'll just
248  * pretend it happened ok.
249  */
250 static void __iwmx_cm_dev_disconnected(struct wmxsdk *wmxsdk)
251 {
252         struct connman_network *nw = wmxsdk->nw;
253
254         if (nw != NULL) {
255                 DBG("disconnected from network %s\n",
256                                         connman_network_get_identifier(nw));
257                 connman_network_set_connected(nw, FALSE);
258                 connman_network_unref(nw);
259                 wmxsdk->nw = NULL;
260         } else
261                 DBG("disconnected from unknown network\n");
262 }
263
264 /*
265  * The device has been disconnnected, make sure connman knows
266  *
267  * See __iwmx_cm_dev_disconnect() for more information.
268  */
269 static void iwmx_cm_dev_disconnected(struct wmxsdk *wmxsdk)
270 {
271         g_mutex_lock(wmxsdk->connect_mutex);
272         __iwmx_cm_dev_disconnected(wmxsdk);
273         g_mutex_unlock(wmxsdk->connect_mutex);
274 }
275
276 /*
277  * Handle a change in state
278  *
279  * This is were most of the action happens. When the device changes
280  * state, this will catch it (through the state change callback or an
281  * explicit call) and call iwmx_cm_dev_*ed() to indicate to connman what
282  * happened.
283  *
284  * Finally, cache the new device status.
285  */
286 void __iwmx_cm_state_change(struct wmxsdk *wmxsdk,
287                                         WIMAX_API_DEVICE_STATUS __new_status)
288 {
289         WIMAX_API_DEVICE_STATUS __old_status = wmxsdk->status;
290         WIMAX_API_DEVICE_STATUS old_status;
291         WIMAX_API_DEVICE_STATUS new_status;
292
293         /*
294          * Simplify state transition computations.
295          *
296          * For practical effects, some states are the same
297          */
298
299 #if HAVE_IWMXSDK_STATUS_IDLE
300         /* Conection_Idle is the same as Data_Connected */
301         if (__old_status == WIMAX_API_DEVICE_STATUS_Connection_Idle)
302                 old_status = WIMAX_API_DEVICE_STATUS_Data_Connected;
303         else
304                 old_status = __old_status;
305         if (__new_status == WIMAX_API_DEVICE_STATUS_Connection_Idle)
306                 new_status = WIMAX_API_DEVICE_STATUS_Data_Connected;
307         else
308                 new_status = __new_status;
309 #endif /* #if HAVE_IWMXSDK_STATUS_IDLE */
310         /* Radio off: all are just RF_OFF_SW (the highest) */
311         switch (__old_status) {
312         case WIMAX_API_DEVICE_STATUS_RF_OFF_HW_SW:
313         case WIMAX_API_DEVICE_STATUS_RF_OFF_HW:
314         case WIMAX_API_DEVICE_STATUS_RF_OFF_SW:
315                 old_status = WIMAX_API_DEVICE_STATUS_RF_OFF_SW;
316                 break;
317         default:
318                 old_status = __old_status;
319                 break;
320         }
321
322         switch (__new_status) {
323         case WIMAX_API_DEVICE_STATUS_RF_OFF_HW_SW:
324         case WIMAX_API_DEVICE_STATUS_RF_OFF_HW:
325         case WIMAX_API_DEVICE_STATUS_RF_OFF_SW:
326                 new_status = WIMAX_API_DEVICE_STATUS_RF_OFF_SW;
327                 break;
328         default:
329                 new_status = __new_status;
330                 break;
331         }
332
333         /* If no real state change, do nothing */
334         if (old_status == new_status) {
335                 DBG("no state changed\n");
336                 return;
337         } else
338                 DBG("state change from %d (%d: %s) to %d (%d: %s)\n",
339                     old_status, __old_status,
340                     iwmx_sdk_dev_status_to_str(__old_status),
341                     new_status, __new_status,
342                     iwmx_sdk_dev_status_to_str(__new_status));
343
344         /* Cleanup old state */
345         switch (old_status) {
346         case WIMAX_API_DEVICE_STATUS_UnInitialized:
347                 /* This means the plugin is starting but the device is
348                  * in some state already, so we need to update our
349                  * internal knowledge of it. */
350                 if (new_status > WIMAX_API_DEVICE_STATUS_RF_OFF_SW)
351                         iwmx_cm_dev_enabled(wmxsdk);
352                 break;
353         case WIMAX_API_DEVICE_STATUS_RF_OFF_SW:
354                 /* This means the radio is being turned on, so enable
355                  * the device ( unless going to uninitialized). */
356                 if (new_status != WIMAX_API_DEVICE_STATUS_RF_OFF_SW)
357                         iwmx_cm_dev_enabled(wmxsdk);
358                 break;
359         case WIMAX_API_DEVICE_STATUS_Ready:
360                 break;
361         case WIMAX_API_DEVICE_STATUS_Scanning:
362                 break;
363         case WIMAX_API_DEVICE_STATUS_Connecting:
364                 break;
365         case WIMAX_API_DEVICE_STATUS_Data_Connected:
366                 iwmx_cm_dev_disconnected(wmxsdk);
367                 break;
368         default:
369                 connman_error("wmxsdk: unknown old status %d\n", old_status);
370                 return;
371         };
372
373         /* Implement new state */
374         switch (new_status) {
375         case WIMAX_API_DEVICE_STATUS_UnInitialized:
376                 break;
377         case WIMAX_API_DEVICE_STATUS_RF_OFF_SW:
378                 /* This means the radio is being turned off, so
379                  * disable the device unless coming from uninitialized. */
380                 if (old_status != WIMAX_API_DEVICE_STATUS_UnInitialized)
381                         iwmx_cm_dev_disabled(wmxsdk);
382                 break;
383         case WIMAX_API_DEVICE_STATUS_Ready:
384                 break;
385         case WIMAX_API_DEVICE_STATUS_Scanning:
386                 break;
387         case WIMAX_API_DEVICE_STATUS_Connecting:
388                 break;
389         case WIMAX_API_DEVICE_STATUS_Data_Connected:
390                 iwmx_cm_dev_connected(wmxsdk);
391                 break;
392         default:
393                 connman_error("wmxsdk: unknown new status %d\n", old_status);
394                 return;
395         };
396         wmxsdk->status = __new_status;
397 }
398
399 /*
400  * Implement a device state transition [locking version]
401  *
402  * See __iwmx_cm_state_change()
403  */
404 void iwmx_cm_state_change(struct wmxsdk *wmxsdk,
405                                  WIMAX_API_DEVICE_STATUS __new_status)
406 {
407         g_mutex_lock(wmxsdk->status_mutex);
408         __iwmx_cm_state_change(wmxsdk, __new_status);
409         g_mutex_unlock(wmxsdk->status_mutex);
410 }
411
412 /*
413  * Read the cached device status
414  */
415 WIMAX_API_DEVICE_STATUS iwmx_cm_status_get(struct wmxsdk *wmxsdk)
416 {
417         WIMAX_API_DEVICE_STATUS status;
418
419         g_mutex_lock(wmxsdk->status_mutex);
420         status = wmxsdk->status;
421         g_mutex_unlock(wmxsdk->status_mutex);
422         return status;
423 }
424
425 /*
426  * Called by connman when a device is enabled by the user
427  *
428  * We need to turn the radio on; the state change function will poke
429  * the internals.
430  */
431 static int iwmx_cm_enable(struct connman_device *dev)
432 {
433         int result;
434         struct wmxsdk *wmxsdk = connman_device_get_data(dev);
435
436         connman_inet_ifup(connman_device_get_index(dev));
437         result = iwmx_sdk_rf_state_set(wmxsdk, WIMAX_API_RF_ON);
438         return result;
439 }
440
441 /*
442  * Called by connman when a device is disabled by the user
443  *
444  * Simple: just make sure the radio is off; the state change function
445  * will poke the internals.
446  */
447 static int iwmx_cm_disable(struct connman_device *dev)
448 {
449         int result;
450         struct wmxsdk *wmxsdk = connman_device_get_data(dev);
451
452         result = iwmx_sdk_rf_state_set(wmxsdk, WIMAX_API_RF_OFF);
453         connman_inet_ifdown(connman_device_get_index(dev));
454         return 0;
455 }
456
457 /*
458  * Probe deferred call from when the mainloop is idle
459  *
460  * probe() schedules this to be called from the mainloop when idle to
461  * do a device status evaluation. Needed because of an internal race
462  * condition in connman. FIXME: deploy into _probe() when fixed.
463  */
464 static gboolean __iwmx_cm_probe_dpc(gpointer _wmxsdk)
465 {
466         int result;
467         struct wmxsdk *wmxsdk = _wmxsdk;
468         result = iwmx_sdk_get_device_status(wmxsdk);
469         if (result < 0)
470                 connman_error("wmxsdk: can't get status: %d\n", result);
471         else
472                 iwmx_cm_state_change(wmxsdk, result);
473         return FALSE;
474 }
475
476 /*
477  * Called by connman when a new device pops in
478  *
479  * We allocate our private structure, register with the WiMAX API,
480  * open their device, subscribe to all the callbacks.
481  *
482  * At the end, we launch a deferred call (to work around current
483  * connman issues that need to be fixed in the future) and update the
484  * device's status. This allows us to pick up the current status and
485  * adapt connman's idea of the device to it.
486  */
487 static int iwmx_cm_probe(struct connman_device *dev)
488 {
489         int result;
490         struct wmxsdk *wmxsdk = NULL;
491
492         wmxsdk = connman_device_get_data(dev);
493         if (wmxsdk == NULL)
494                 /* not called from a discovery done by the WiMAX
495                  * Network Service, ignore */
496                 return -ENODEV;
497
498         result = iwmx_sdk_setup(wmxsdk);
499         if (result < 0)
500                 goto error_setup;
501
502         /* There is a race condition in the connman core that doesn't
503          * allow us to call this directly and things to work properly
504          * FIXME FIXME FIXME: merge _dpc call in here when connman is fixed */
505         g_idle_add(__iwmx_cm_probe_dpc, wmxsdk);
506         return 0;
507
508         iwmx_sdk_remove(wmxsdk);
509 error_setup:
510         return result;
511 }
512
513 /*
514  * Called when a device is removed from connman
515  *
516  * Cleanup all that is done in _probe. Remove callbacks, unregister
517  * from the WiMAX API.
518  */
519 static void iwmx_cm_remove(struct connman_device *dev)
520 {
521         struct wmxsdk *wmxsdk = connman_device_get_data(dev);
522         iwmx_sdk_remove(wmxsdk);
523 }
524
525 /*
526  * Called by connman to ask the device to scan for networks
527  *
528  * We have set in the WiMAX API the scan result callbacks, so we just
529  * start a simple scan (not a wide one).
530  *
531  * First we obtain the current list of networks and pass it to the
532  * callback processor. Then we start an scan cycle.
533  */
534 static int iwmx_cm_scan(struct connman_device *dev)
535 {
536         struct wmxsdk *wmxsdk = connman_device_get_data(dev);
537         return iwmx_sdk_scan(wmxsdk);
538 }
539
540 /*
541  * Driver for a WiMAX API based device.
542  */
543 static struct connman_device_driver iwmx_cm_device_driver = {
544         .name           = "iwmx",
545         .type           = CONNMAN_DEVICE_TYPE_WIMAX,
546         .probe          = iwmx_cm_probe,
547         .remove         = iwmx_cm_remove,
548         .enable         = iwmx_cm_enable,
549         .disable        = iwmx_cm_disable,
550         .scan           = iwmx_cm_scan,
551 };
552
553 static int iwmx_cm_init(void)
554 {
555         int result;
556
557         result = connman_device_driver_register(&iwmx_cm_device_driver);
558         if (result < 0)
559                 goto error_driver_register;
560         result = connman_network_driver_register(&iwmx_cm_network_driver);
561         if (result < 0)
562                 goto error_network_driver_register;
563         result = iwmx_sdk_api_init();
564         if (result < 0)
565                 goto error_iwmx_sdk_init;
566         return 0;
567
568 error_iwmx_sdk_init:
569         connman_network_driver_unregister(&iwmx_cm_network_driver);
570 error_network_driver_register:
571         connman_device_driver_unregister(&iwmx_cm_device_driver);
572 error_driver_register:
573         return result;
574 }
575
576 static void iwmx_cm_exit(void)
577 {
578         iwmx_sdk_api_exit();
579         connman_network_driver_unregister(&iwmx_cm_network_driver);
580         connman_device_driver_unregister(&iwmx_cm_device_driver);
581 }
582
583 CONNMAN_PLUGIN_DEFINE(iwmx, "Intel WiMAX SDK / Common API plugin",
584                         CONNMAN_VERSION, CONNMAN_PLUGIN_PRIORITY_LOW,
585                                                 iwmx_cm_init, iwmx_cm_exit);