technology: Add 'Connected' Technology property
[platform/upstream/connman.git] / src / technology.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 <errno.h>
27 #include <string.h>
28
29 #include <gdbus.h>
30
31 #include "connman.h"
32
33 static DBusConnection *connection;
34
35 static GSList *technology_list = NULL;
36
37 static connman_bool_t global_offlinemode;
38
39 struct connman_rfkill {
40         unsigned int index;
41         enum connman_service_type type;
42         connman_bool_t softblock;
43         connman_bool_t hardblock;
44 };
45
46 enum connman_technology_state {
47         CONNMAN_TECHNOLOGY_STATE_UNKNOWN   = 0,
48         CONNMAN_TECHNOLOGY_STATE_OFFLINE   = 1,
49         CONNMAN_TECHNOLOGY_STATE_ENABLED   = 2,
50         CONNMAN_TECHNOLOGY_STATE_CONNECTED = 3,
51 };
52
53 struct connman_technology {
54         int refcount;
55         enum connman_service_type type;
56         enum connman_technology_state state;
57         char *path;
58         GHashTable *rfkill_list;
59         GSList *device_list;
60         int enabled;
61         char *regdom;
62         connman_bool_t connected;
63
64         connman_bool_t tethering;
65         char *tethering_ident;
66         char *tethering_passphrase;
67
68         connman_bool_t enable_persistent; /* Save the tech state */
69
70         struct connman_technology_driver *driver;
71         void *driver_data;
72
73         DBusMessage *pending_reply;
74         guint pending_timeout;
75 };
76
77 static GSList *driver_list = NULL;
78
79 static gint compare_priority(gconstpointer a, gconstpointer b)
80 {
81         const struct connman_technology_driver *driver1 = a;
82         const struct connman_technology_driver *driver2 = b;
83
84         return driver2->priority - driver1->priority;
85 }
86
87 /**
88  * connman_technology_driver_register:
89  * @driver: Technology driver definition
90  *
91  * Register a new technology driver
92  *
93  * Returns: %0 on success
94  */
95 int connman_technology_driver_register(struct connman_technology_driver *driver)
96 {
97         DBG("Registering %s driver", driver->name);
98
99         driver_list = g_slist_insert_sorted(driver_list, driver,
100                                                         compare_priority);
101
102         return 0;
103 }
104
105 /**
106  * connman_technology_driver_unregister:
107  * @driver: Technology driver definition
108  *
109  * Remove a previously registered technology driver
110  */
111 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
112 {
113         GSList *list;
114         struct connman_technology *technology;
115
116         DBG("Unregistering driver %p name %s", driver, driver->name);
117
118         for (list = technology_list; list; list = list->next) {
119                 technology = list->data;
120
121                 if (technology->driver == NULL)
122                         continue;
123
124                 if (technology->type == driver->type) {
125                         technology->driver->remove(technology);
126                         technology->driver = NULL;
127                 }
128         }
129
130         driver_list = g_slist_remove(driver_list, driver);
131 }
132
133 static void tethering_changed(struct connman_technology *technology)
134 {
135         connman_bool_t tethering = technology->tethering;
136
137         connman_dbus_property_changed_basic(technology->path,
138                                 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
139                                                 DBUS_TYPE_BOOLEAN, &tethering);
140 }
141
142 void connman_technology_tethering_notify(struct connman_technology *technology,
143                                                         connman_bool_t enabled)
144 {
145         GSList *list;
146
147         DBG("technology %p enabled %u", technology, enabled);
148
149         if (technology->tethering == enabled)
150                 return;
151
152         technology->tethering = enabled;
153
154         tethering_changed(technology);
155
156         if (enabled == TRUE)
157                 __connman_tethering_set_enabled();
158         else {
159                 for (list = technology_list; list; list = list->next) {
160                         struct connman_technology *other_tech = list->data;
161                         if (other_tech->tethering == TRUE)
162                                 break;
163                 }
164                 if (list == NULL)
165                         __connman_tethering_set_disabled();
166         }
167 }
168
169 static int set_tethering(struct connman_technology *technology,
170                                 connman_bool_t enabled)
171 {
172         const char *ident, *passphrase, *bridge;
173
174         ident = technology->tethering_ident;
175         passphrase = technology->tethering_passphrase;
176
177         if (technology->driver == NULL ||
178                         technology->driver->set_tethering == NULL)
179                 return -EOPNOTSUPP;
180
181         bridge = __connman_tethering_get_bridge();
182         if (bridge == NULL)
183                 return -EOPNOTSUPP;
184
185         if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
186             (ident == NULL || passphrase == NULL))
187                 return -EINVAL;
188
189         return technology->driver->set_tethering(technology, ident, passphrase,
190                                                         bridge, enabled);
191 }
192
193 void connman_technology_regdom_notify(struct connman_technology *technology,
194                                                         const char *alpha2)
195 {
196         DBG("");
197
198         if (alpha2 == NULL)
199                 connman_error("Failed to set regulatory domain");
200         else
201                 DBG("Regulatory domain set to %s", alpha2);
202
203         g_free(technology->regdom);
204         technology->regdom = g_strdup(alpha2);
205 }
206
207 int connman_technology_set_regdom(const char *alpha2)
208 {
209         GSList *list;
210
211         for (list = technology_list; list; list = list->next) {
212                 struct connman_technology *technology = list->data;
213
214                 if (technology->driver == NULL)
215                         continue;
216
217                 if (technology->driver->set_regdom)
218                         technology->driver->set_regdom(technology, alpha2);
219         }
220
221         return 0;
222 }
223
224 static void free_rfkill(gpointer data)
225 {
226         struct connman_rfkill *rfkill = data;
227
228         g_free(rfkill);
229 }
230
231 static const char *state2string(enum connman_technology_state state)
232 {
233         switch (state) {
234         case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
235                 break;
236         case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
237                 return "offline";
238         case CONNMAN_TECHNOLOGY_STATE_ENABLED:
239                 return "enabled";
240         case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
241                 return "connected";
242         }
243
244         return NULL;
245 }
246
247 static void state_changed(struct connman_technology *technology)
248 {
249         const char *str;
250
251         str = state2string(technology->state);
252         if (str == NULL)
253                 return;
254
255         connman_dbus_property_changed_basic(technology->path,
256                                 CONNMAN_TECHNOLOGY_INTERFACE, "State",
257                                                 DBUS_TYPE_STRING, &str);
258 }
259
260 static const char *get_name(enum connman_service_type type)
261 {
262         switch (type) {
263         case CONNMAN_SERVICE_TYPE_UNKNOWN:
264         case CONNMAN_SERVICE_TYPE_SYSTEM:
265         case CONNMAN_SERVICE_TYPE_GPS:
266         case CONNMAN_SERVICE_TYPE_VPN:
267         case CONNMAN_SERVICE_TYPE_GADGET:
268                 break;
269         case CONNMAN_SERVICE_TYPE_ETHERNET:
270                 return "Wired";
271         case CONNMAN_SERVICE_TYPE_WIFI:
272                 return "WiFi";
273         case CONNMAN_SERVICE_TYPE_WIMAX:
274                 return "WiMAX";
275         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
276                 return "Bluetooth";
277         case CONNMAN_SERVICE_TYPE_CELLULAR:
278                 return "Cellular";
279         }
280
281         return NULL;
282 }
283
284 static void load_state(struct connman_technology *technology)
285 {
286         GKeyFile *keyfile;
287         gchar *identifier;
288         GError *error = NULL;
289         connman_bool_t enable;
290
291         DBG("technology %p", technology);
292
293         keyfile = __connman_storage_load_global();
294         /* Fallback on disabling technology if file not found. */
295         if (keyfile == NULL) {
296                 technology->enable_persistent = FALSE;
297                 return;
298         }
299
300         identifier = g_strdup_printf("%s", get_name(technology->type));
301         if (identifier == NULL)
302                 goto done;
303
304         enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
305         if (error == NULL)
306                 technology->enable_persistent = enable;
307         else {
308                 technology->enable_persistent = FALSE;
309                 g_clear_error(&error);
310         }
311 done:
312         g_free(identifier);
313
314         g_key_file_free(keyfile);
315
316         return;
317 }
318
319 static void save_state(struct connman_technology *technology)
320 {
321         GKeyFile *keyfile;
322         gchar *identifier;
323
324         DBG("technology %p", technology);
325
326         keyfile = __connman_storage_load_global();
327         if (keyfile == NULL)
328                 keyfile = g_key_file_new();
329
330         identifier = g_strdup_printf("%s", get_name(technology->type));
331         if (identifier == NULL)
332                 goto done;
333
334         g_key_file_set_boolean(keyfile, identifier, "Enable",
335                                 technology->enable_persistent);
336
337 done:
338         g_free(identifier);
339
340         __connman_storage_save_global(keyfile);
341
342         g_key_file_free(keyfile);
343
344         return;
345 }
346
347 connman_bool_t __connman_technology_get_offlinemode(void)
348 {
349         return global_offlinemode;
350 }
351
352 static void connman_technology_save_offlinemode()
353 {
354         GKeyFile *keyfile;
355
356         keyfile = __connman_storage_load_global();
357         if (keyfile == NULL)
358                 keyfile = g_key_file_new();
359
360         g_key_file_set_boolean(keyfile, "global",
361                                         "OfflineMode", global_offlinemode);
362
363         __connman_storage_save_global(keyfile);
364
365         g_key_file_free(keyfile);
366
367         return;
368 }
369
370 static connman_bool_t connman_technology_load_offlinemode()
371 {
372         GKeyFile *keyfile;
373         GError *error = NULL;
374         connman_bool_t offlinemode;
375
376         /* If there is a error, we enable offlinemode */
377         keyfile = __connman_storage_load_global();
378         if (keyfile == NULL)
379                 return TRUE;
380
381         offlinemode = g_key_file_get_boolean(keyfile, "global",
382                                                 "OfflineMode", &error);
383         if (error != NULL) {
384                 offlinemode = TRUE;
385                 g_clear_error(&error);
386         }
387
388         g_key_file_free(keyfile);
389
390         return offlinemode;
391 }
392
393 static void append_properties(DBusMessageIter *iter,
394                 struct connman_technology *technology)
395 {
396         DBusMessageIter dict;
397         const char *str;
398         connman_bool_t powered;
399
400         connman_dbus_dict_open(iter, &dict);
401
402         str = state2string(technology->state);
403         if (str != NULL)
404                 connman_dbus_dict_append_basic(&dict, "State",
405                                                 DBUS_TYPE_STRING, &str);
406
407         str = get_name(technology->type);
408         if (str != NULL)
409                 connman_dbus_dict_append_basic(&dict, "Name",
410                                                 DBUS_TYPE_STRING, &str);
411
412         str = __connman_service_type2string(technology->type);
413         if (str != NULL)
414                 connman_dbus_dict_append_basic(&dict, "Type",
415                                                 DBUS_TYPE_STRING, &str);
416
417         __sync_synchronize();
418         if (technology->enabled > 0)
419                 powered = TRUE;
420         else
421                 powered = FALSE;
422         connman_dbus_dict_append_basic(&dict, "Powered",
423                                         DBUS_TYPE_BOOLEAN, &powered);
424
425         connman_dbus_dict_append_basic(&dict, "Connected",
426                                         DBUS_TYPE_BOOLEAN,
427                                         &technology->connected);
428
429         connman_dbus_dict_append_basic(&dict, "Tethering",
430                                         DBUS_TYPE_BOOLEAN,
431                                         &technology->tethering);
432
433         if (technology->tethering_ident != NULL)
434                 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
435                                                 DBUS_TYPE_STRING,
436                                                 &technology->tethering_ident);
437
438         if (technology->tethering_passphrase != NULL)
439                 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
440                                                 DBUS_TYPE_STRING,
441                                                 &technology->tethering_passphrase);
442
443         connman_dbus_dict_close(iter, &dict);
444 }
445
446 static void technology_added_signal(struct connman_technology *technology)
447 {
448         DBusMessage *signal;
449         DBusMessageIter iter;
450
451         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
452                         CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
453         if (signal == NULL)
454                 return;
455
456         dbus_message_iter_init_append(signal, &iter);
457         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
458                                                         &technology->path);
459         append_properties(&iter, technology);
460
461         dbus_connection_send(connection, signal, NULL);
462         dbus_message_unref(signal);
463 }
464
465 static void technology_removed_signal(struct connman_technology *technology)
466 {
467         g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
468                         CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
469                         DBUS_TYPE_OBJECT_PATH, &technology->path,
470                         DBUS_TYPE_INVALID);
471 }
472
473 static DBusMessage *get_properties(DBusConnection *conn,
474                                         DBusMessage *message, void *user_data)
475 {
476         struct connman_technology *technology = user_data;
477         DBusMessage *reply;
478         DBusMessageIter iter;
479
480         reply = dbus_message_new_method_return(message);
481         if (reply == NULL)
482                 return NULL;
483
484         dbus_message_iter_init_append(reply, &iter);
485         append_properties(&iter, technology);
486
487         return reply;
488 }
489
490 void __connman_technology_list_struct(DBusMessageIter *array)
491 {
492         GSList *list;
493         DBusMessageIter entry;
494
495         for (list = technology_list; list; list = list->next) {
496                 struct connman_technology *technology = list->data;
497
498                 if (technology->path == NULL)
499                         continue;
500
501                 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
502                                 NULL, &entry);
503                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
504                                 &technology->path);
505                 append_properties(&entry, technology);
506                 dbus_message_iter_close_container(array, &entry);
507         }
508 }
509
510 static gboolean technology_pending_reply(gpointer user_data)
511 {
512         struct connman_technology *technology = user_data;
513         DBusMessage *reply;
514
515         /* Power request timedout, send ETIMEDOUT. */
516         if (technology->pending_reply != NULL) {
517                 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
518                 if (reply != NULL)
519                         g_dbus_send_message(connection, reply);
520
521                 dbus_message_unref(technology->pending_reply);
522                 technology->pending_reply = NULL;
523                 technology->pending_timeout = 0;
524         }
525
526         return FALSE;
527 }
528
529 static int technology_enable(struct connman_technology *technology,
530                 DBusMessage *msg)
531 {
532         GSList *list;
533         int err = 0;
534         int ret = -ENODEV;
535         DBusMessage *reply;
536
537         DBG("technology %p enable", technology);
538
539         __sync_synchronize();
540         if (technology->enabled > 0) {
541                 err = -EALREADY;
542                 goto done;
543         }
544
545         if (technology->pending_reply != NULL) {
546                 err = -EBUSY;
547                 goto done;
548         }
549
550         if (msg != NULL) {
551                 /*
552                  * This is a bit of a trick. When msg is not NULL it means
553                  * thats technology_enable was invoked from the manager API.
554                  * Hence we save the state here.
555                  */
556                 technology->enable_persistent = TRUE;
557                 save_state(technology);
558         }
559
560         __connman_rfkill_block(technology->type, FALSE);
561
562         /*
563          * An empty device list means that devices in the technology
564          * were rfkill blocked. The unblock above will enable the devs.
565          */
566         if (technology->device_list == NULL) {
567                 ret = 0;
568                 goto done;
569         }
570
571         for (list = technology->device_list; list; list = list->next) {
572                 struct connman_device *device = list->data;
573
574                 err = __connman_device_enable(device);
575                 /*
576                  * err = 0 : Device was enabled right away.
577                  * If atleast one device gets enabled, we consider
578                  * the technology to be enabled.
579                  */
580                 if (err == 0)
581                         ret = 0;
582         }
583
584 done:
585         if (ret == 0) {
586                 if (msg != NULL)
587                         g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
588                 return ret;
589         }
590
591         if (msg != NULL) {
592                 if (err == -EINPROGRESS) {
593                         technology->pending_reply = dbus_message_ref(msg);
594                         technology->pending_timeout = g_timeout_add_seconds(10,
595                                         technology_pending_reply, technology);
596                 } else {
597                         reply = __connman_error_failed(msg, -err);
598                         if (reply != NULL)
599                                 g_dbus_send_message(connection, reply);
600                 }
601         }
602
603         return err;
604 }
605
606 static int technology_disable(struct connman_technology *technology,
607                 DBusMessage *msg)
608 {
609         GSList *list;
610         int err = 0;
611         int ret = -ENODEV;
612         DBusMessage *reply;
613
614         DBG("technology %p disable", technology);
615
616         __sync_synchronize();
617         if (technology->enabled == 0) {
618                 err = -EALREADY;
619                 goto done;
620         }
621
622         if (technology->pending_reply != NULL) {
623                 err = -EBUSY;
624                 goto done;
625         }
626
627         if (technology->tethering == TRUE)
628                 set_tethering(technology, FALSE);
629
630         if (msg != NULL) {
631                 technology->enable_persistent = FALSE;
632                 save_state(technology);
633         }
634
635         __connman_rfkill_block(technology->type, TRUE);
636
637         for (list = technology->device_list; list; list = list->next) {
638                 struct connman_device *device = list->data;
639
640                 err = __connman_device_disable(device);
641                 if (err == 0)
642                         ret = 0;
643         }
644
645 done:
646         if (ret == 0) {
647                 if (msg != NULL)
648                         g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
649                 return ret;
650         }
651
652         if (msg != NULL) {
653                 if (err == -EINPROGRESS) {
654                         technology->pending_reply = dbus_message_ref(msg);
655                         technology->pending_timeout = g_timeout_add_seconds(10,
656                                         technology_pending_reply, technology);
657                 } else {
658                         reply = __connman_error_failed(msg, -err);
659                         if (reply != NULL)
660                                 g_dbus_send_message(connection, reply);
661                 }
662         }
663
664         return err;
665 }
666
667 static DBusMessage *set_property(DBusConnection *conn,
668                                         DBusMessage *msg, void *data)
669 {
670         struct connman_technology *technology = data;
671         DBusMessageIter iter, value;
672         const char *name;
673         int type;
674
675         DBG("conn %p", conn);
676
677         if (dbus_message_iter_init(msg, &iter) == FALSE)
678                 return __connman_error_invalid_arguments(msg);
679
680         dbus_message_iter_get_basic(&iter, &name);
681         dbus_message_iter_next(&iter);
682         dbus_message_iter_recurse(&iter, &value);
683
684         type = dbus_message_iter_get_arg_type(&value);
685
686         DBG("property %s", name);
687
688         if (g_str_equal(name, "Tethering") == TRUE) {
689                 int err;
690                 connman_bool_t tethering;
691
692                 if (type != DBUS_TYPE_BOOLEAN)
693                         return __connman_error_invalid_arguments(msg);
694
695                 dbus_message_iter_get_basic(&value, &tethering);
696
697                 if (technology->tethering == tethering)
698                         return __connman_error_in_progress(msg);
699
700                 err = set_tethering(technology, tethering);
701                 if (err < 0)
702                         return __connman_error_failed(msg, -err);
703
704         } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
705                 const char *str;
706
707                 dbus_message_iter_get_basic(&value, &str);
708
709                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
710                         return __connman_error_not_supported(msg);
711
712                 technology->tethering_ident = g_strdup(str);
713         } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
714                 const char *str;
715
716                 dbus_message_iter_get_basic(&value, &str);
717
718                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
719                         return __connman_error_not_supported(msg);
720
721                 if (strlen(str) < 8)
722                         return __connman_error_invalid_arguments(msg);
723
724                 technology->tethering_passphrase = g_strdup(str);
725         } else if (g_str_equal(name, "Powered") == TRUE) {
726                 connman_bool_t enable;
727
728                 if (type != DBUS_TYPE_BOOLEAN)
729                         return __connman_error_invalid_arguments(msg);
730
731                 dbus_message_iter_get_basic(&value, &enable);
732                 if (enable == TRUE)
733                         technology_enable(technology, msg);
734                 else
735                         technology_disable(technology, msg);
736
737         } else
738                 return __connman_error_invalid_property(msg);
739
740         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
741 }
742
743 static GDBusMethodTable technology_methods[] = {
744         { "GetProperties", "",   "a{sv}", get_properties },
745         { "SetProperty",   "sv", "",      set_property   },
746         { },
747 };
748
749 static GDBusSignalTable technology_signals[] = {
750         { "PropertyChanged", "sv" },
751         { },
752 };
753
754 static struct connman_technology *technology_find(enum connman_service_type type)
755 {
756         GSList *list;
757
758         DBG("type %d", type);
759
760         for (list = technology_list; list; list = list->next) {
761                 struct connman_technology *technology = list->data;
762
763                 if (technology->type == type)
764                         return technology;
765         }
766
767         return NULL;
768 }
769
770 static struct connman_technology *technology_get(enum connman_service_type type)
771 {
772         struct connman_technology *technology;
773         struct connman_technology_driver *driver = NULL;
774         const char *str;
775         GSList *list;
776         int err;
777
778         DBG("type %d", type);
779
780         str = __connman_service_type2string(type);
781         if (str == NULL)
782                 return NULL;
783
784         technology = technology_find(type);
785         if (technology != NULL)
786                 return technology;
787
788         /* First check if we have a driver for this technology type */
789         for (list = driver_list; list; list = list->next) {
790                 driver = list->data;
791
792                 if (driver->type == type)
793                         break;
794                 else
795                         driver = NULL;
796         }
797
798         if (driver == NULL) {
799                 DBG("No matching driver found for %s.",
800                                 __connman_service_type2string(type));
801                 return NULL;
802         }
803
804         technology = g_try_new0(struct connman_technology, 1);
805         if (technology == NULL)
806                 return NULL;
807
808         technology->refcount = 1;
809
810         technology->type = type;
811         technology->path = g_strdup_printf("%s/technology/%s",
812                                                         CONNMAN_PATH, str);
813
814         technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
815                                                         NULL, free_rfkill);
816         technology->device_list = NULL;
817
818         technology->pending_reply = NULL;
819         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
820
821         load_state(technology);
822
823         if (g_dbus_register_interface(connection, technology->path,
824                                         CONNMAN_TECHNOLOGY_INTERFACE,
825                                         technology_methods, technology_signals,
826                                         NULL, technology, NULL) == FALSE) {
827                 connman_error("Failed to register %s", technology->path);
828                 g_free(technology);
829                 return NULL;
830         }
831
832         technology_list = g_slist_append(technology_list, technology);
833
834         technology_added_signal(technology);
835
836         technology->driver = driver;
837         err = driver->probe(technology);
838         if (err != 0)
839                 DBG("Driver probe failed for technology %p", technology);
840
841         DBG("technology %p", technology);
842
843         return technology;
844 }
845
846 static void technology_put(struct connman_technology *technology)
847 {
848         DBG("technology %p", technology);
849
850         if (__sync_fetch_and_sub(&technology->refcount, 1) != 1)
851                 return;
852
853         if (technology->driver) {
854                 technology->driver->remove(technology);
855                 technology->driver = NULL;
856         }
857
858         technology_list = g_slist_remove(technology_list, technology);
859
860         technology_removed_signal(technology);
861
862         g_dbus_unregister_interface(connection, technology->path,
863                                                 CONNMAN_TECHNOLOGY_INTERFACE);
864
865         g_slist_free(technology->device_list);
866         g_hash_table_destroy(technology->rfkill_list);
867
868         g_free(technology->path);
869         g_free(technology->regdom);
870         g_free(technology);
871 }
872
873 void __connman_technology_add_interface(enum connman_service_type type,
874                                 int index, const char *name, const char *ident)
875 {
876         struct connman_technology *technology;
877
878         switch (type) {
879         case CONNMAN_SERVICE_TYPE_UNKNOWN:
880         case CONNMAN_SERVICE_TYPE_SYSTEM:
881                 return;
882         case CONNMAN_SERVICE_TYPE_ETHERNET:
883         case CONNMAN_SERVICE_TYPE_WIFI:
884         case CONNMAN_SERVICE_TYPE_WIMAX:
885         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
886         case CONNMAN_SERVICE_TYPE_CELLULAR:
887         case CONNMAN_SERVICE_TYPE_GPS:
888         case CONNMAN_SERVICE_TYPE_VPN:
889         case CONNMAN_SERVICE_TYPE_GADGET:
890                 break;
891         }
892
893         connman_info("Adding interface %s [ %s ]", name,
894                                 __connman_service_type2string(type));
895
896         technology = technology_find(type);
897
898         if (technology == NULL || technology->driver == NULL
899                         || technology->driver->add_interface == NULL)
900                 return;
901
902         technology->driver->add_interface(technology,
903                                         index, name, ident);
904 }
905
906 void __connman_technology_remove_interface(enum connman_service_type type,
907                                 int index, const char *name, const char *ident)
908 {
909         struct connman_technology *technology;
910
911         switch (type) {
912         case CONNMAN_SERVICE_TYPE_UNKNOWN:
913         case CONNMAN_SERVICE_TYPE_SYSTEM:
914                 return;
915         case CONNMAN_SERVICE_TYPE_ETHERNET:
916         case CONNMAN_SERVICE_TYPE_WIFI:
917         case CONNMAN_SERVICE_TYPE_WIMAX:
918         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
919         case CONNMAN_SERVICE_TYPE_CELLULAR:
920         case CONNMAN_SERVICE_TYPE_GPS:
921         case CONNMAN_SERVICE_TYPE_VPN:
922         case CONNMAN_SERVICE_TYPE_GADGET:
923                 break;
924         }
925
926         connman_info("Remove interface %s [ %s ]", name,
927                                 __connman_service_type2string(type));
928
929         technology = technology_find(type);
930
931         if (technology == NULL || technology->driver == NULL)
932                 return;
933
934         if (technology->driver->remove_interface)
935                 technology->driver->remove_interface(technology, index);
936 }
937
938 int __connman_technology_add_device(struct connman_device *device)
939 {
940         struct connman_technology *technology;
941         enum connman_service_type type;
942
943         DBG("device %p", device);
944
945         type = __connman_device_get_service_type(device);
946
947         technology = technology_get(type);
948         if (technology == NULL)
949                 return -ENXIO;
950
951         if (technology->enable_persistent && !global_offlinemode)
952                 __connman_device_enable(device);
953         /* if technology persistent state is offline */
954         if (!technology->enable_persistent)
955                 __connman_device_disable(device);
956
957         technology->device_list = g_slist_append(technology->device_list,
958                                                                 device);
959
960         return 0;
961 }
962
963 int __connman_technology_remove_device(struct connman_device *device)
964 {
965         struct connman_technology *technology;
966         enum connman_service_type type;
967
968         DBG("device %p", device);
969
970         type = __connman_device_get_service_type(device);
971
972         technology = technology_find(type);
973         if (technology == NULL)
974                 return -ENXIO;
975
976         technology->device_list = g_slist_remove(technology->device_list,
977                                                                 device);
978         if (technology->device_list == NULL) {
979                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
980                 state_changed(technology);
981         }
982
983         return 0;
984 }
985
986 static void powered_changed(struct connman_technology *technology)
987 {
988         connman_bool_t powered;
989
990         __sync_synchronize();
991         if (technology->enabled >0)
992                 powered = TRUE;
993         else
994                 powered = FALSE;
995
996         connman_dbus_property_changed_basic(technology->path,
997                         CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
998                         DBUS_TYPE_BOOLEAN, &powered);
999 }
1000
1001 int __connman_technology_enabled(enum connman_service_type type)
1002 {
1003         struct connman_technology *technology;
1004
1005         technology = technology_find(type);
1006         if (technology == NULL)
1007                 return -ENXIO;
1008
1009         if (__sync_fetch_and_add(&technology->enabled, 1) == 0) {
1010                 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
1011                 state_changed(technology);
1012                 powered_changed(technology);
1013         }
1014
1015         if (technology->pending_reply != NULL) {
1016                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1017                 dbus_message_unref(technology->pending_reply);
1018                 g_source_remove(technology->pending_timeout);
1019                 technology->pending_reply = NULL;
1020                 technology->pending_timeout = 0;
1021         }
1022
1023         return 0;
1024 }
1025
1026 int __connman_technology_disabled(enum connman_service_type type)
1027 {
1028         struct connman_technology *technology;
1029
1030         technology = technology_find(type);
1031         if (technology == NULL)
1032                 return -ENXIO;
1033
1034         if (technology->pending_reply != NULL) {
1035                 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1036                 dbus_message_unref(technology->pending_reply);
1037                 g_source_remove(technology->pending_timeout);
1038                 technology->pending_reply = NULL;
1039                 technology->pending_timeout = 0;
1040         }
1041
1042         if (__sync_fetch_and_sub(&technology->enabled, 1) != 1)
1043                 return 0;
1044
1045         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
1046         state_changed(technology);
1047         powered_changed(technology);
1048
1049         return 0;
1050 }
1051
1052 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1053 {
1054         GSList *list;
1055         int err = -EINVAL;
1056
1057         if (global_offlinemode == offlinemode)
1058                 return 0;
1059
1060         DBG("offlinemode %s", offlinemode ? "On" : "Off");
1061
1062         /*
1063          * This is a bit tricky. When you set offlinemode, there is no
1064          * way to differentiate between attempting offline mode and
1065          * resuming offlinemode from last saved profile. We need that
1066          * information in rfkill_update, otherwise it falls back on the
1067          * technology's persistent state. Hence we set the offline mode here
1068          * but save it & call the notifier only if its successful.
1069          */
1070
1071         global_offlinemode = offlinemode;
1072
1073         /* Traverse technology list, enable/disable each technology. */
1074         for (list = technology_list; list; list = list->next) {
1075                 struct connman_technology *technology = list->data;
1076
1077                 if (offlinemode)
1078                         err = technology_disable(technology, NULL);
1079
1080                 if (!offlinemode && technology->enable_persistent)
1081                         err = technology_enable(technology, NULL);
1082         }
1083
1084         if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1085                 connman_technology_save_offlinemode();
1086                 __connman_notifier_offlinemode(offlinemode);
1087         } else
1088                 global_offlinemode = connman_technology_load_offlinemode();
1089
1090         return err;
1091 }
1092
1093 void __connman_technology_set_connected(enum connman_service_type type,
1094                 connman_bool_t connected)
1095 {
1096         struct connman_technology *technology;
1097
1098         technology = technology_find(type);
1099         if (technology == NULL)
1100                 return;
1101
1102         DBG("technology %p connected %d", technology, connected);
1103
1104         technology->connected = connected;
1105
1106         connman_dbus_property_changed_basic(technology->path,
1107                         CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1108                         DBUS_TYPE_BOOLEAN, &connected);
1109 }
1110
1111 int __connman_technology_add_rfkill(unsigned int index,
1112                                         enum connman_service_type type,
1113                                                 connman_bool_t softblock,
1114                                                 connman_bool_t hardblock)
1115 {
1116         struct connman_technology *technology;
1117         struct connman_rfkill *rfkill;
1118
1119         DBG("index %u type %d soft %u hard %u", index, type,
1120                                                         softblock, hardblock);
1121
1122         technology = technology_get(type);
1123         if (technology == NULL)
1124                 return -ENXIO;
1125
1126         rfkill = g_try_new0(struct connman_rfkill, 1);
1127         if (rfkill == NULL)
1128                 return -ENOMEM;
1129
1130         rfkill->index = index;
1131         rfkill->type = type;
1132         rfkill->softblock = softblock;
1133         rfkill->hardblock = hardblock;
1134
1135         g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
1136
1137         if (hardblock) {
1138                 DBG("%s is switched off.", get_name(type));
1139                 return 0;
1140         }
1141
1142         /*
1143          * If Offline mode is on, we softblock the device if it isnt already.
1144          * If Offline mode is off, we rely on the persistent state of tech.
1145          */
1146         if (global_offlinemode) {
1147                 if (!softblock)
1148                         return __connman_rfkill_block(type, TRUE);
1149         } else {
1150                 if (technology->enable_persistent && softblock)
1151                         return __connman_rfkill_block(type, FALSE);
1152                 /* if technology persistent state is offline */
1153                 if (!technology->enable_persistent && !softblock)
1154                         return __connman_rfkill_block(type, TRUE);
1155         }
1156
1157         return 0;
1158 }
1159
1160 int __connman_technology_update_rfkill(unsigned int index,
1161                                         enum connman_service_type type,
1162                                                 connman_bool_t softblock,
1163                                                 connman_bool_t hardblock)
1164 {
1165         struct connman_technology *technology;
1166         struct connman_rfkill *rfkill;
1167
1168         DBG("index %u soft %u hard %u", index, softblock, hardblock);
1169
1170         technology = technology_find(type);
1171         if (technology == NULL)
1172                 return -ENXIO;
1173
1174         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
1175         if (rfkill == NULL)
1176                 return -ENXIO;
1177
1178         if (rfkill->softblock == softblock &&
1179                 rfkill->hardblock == hardblock)
1180                 return 0;
1181
1182         rfkill->softblock = softblock;
1183         rfkill->hardblock = hardblock;
1184
1185         if (hardblock) {
1186                 DBG("%s is switched off.", get_name(type));
1187                 return 0;
1188         }
1189
1190         if (!global_offlinemode) {
1191                 if (technology->enable_persistent && softblock)
1192                         return __connman_rfkill_block(type, FALSE);
1193                 if (!technology->enable_persistent && !softblock)
1194                         return __connman_rfkill_block(type, TRUE);
1195         }
1196
1197         return 0;
1198 }
1199
1200 int __connman_technology_remove_rfkill(unsigned int index,
1201                                         enum connman_service_type type)
1202 {
1203         struct connman_technology *technology;
1204         struct connman_rfkill *rfkill;
1205
1206         DBG("index %u", index);
1207
1208         technology = technology_find(type);
1209         if (technology == NULL)
1210                 return -ENXIO;
1211
1212         rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
1213         if (rfkill == NULL)
1214                 return -ENXIO;
1215
1216         g_hash_table_remove(technology->rfkill_list, &index);
1217
1218         technology_put(technology);
1219
1220         return 0;
1221 }
1222
1223 int __connman_technology_init(void)
1224 {
1225         DBG("");
1226
1227         connection = connman_dbus_get_connection();
1228
1229         global_offlinemode = connman_technology_load_offlinemode();
1230
1231         return 0;
1232 }
1233
1234 void __connman_technology_cleanup(void)
1235 {
1236         DBG("");
1237
1238         dbus_connection_unref(connection);
1239 }