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