6a69b4e84d8948a32060515e4dea85671c8b06a1
[platform/upstream/connman.git] / src / technology.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  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 /*
38  * List of devices with no technology associated with them either because of
39  * no compiled in support or the driver is not yet loaded.
40 */
41 static GSList *techless_device_list = NULL;
42 static GHashTable *rfkill_list;
43
44 static bool global_offlinemode;
45
46 struct connman_rfkill {
47         unsigned int index;
48         enum connman_service_type type;
49         bool softblock;
50         bool hardblock;
51 };
52
53 struct connman_technology {
54         int refcount;
55         enum connman_service_type type;
56         char *path;
57         GSList *device_list;
58         bool enabled;
59         char *regdom;
60         bool connected;
61
62         bool tethering;
63         bool tethering_persistent; /* Tells the save status, needed
64                                               * as offline mode might set
65                                               * tethering OFF.
66                                               */
67         char *tethering_ident;
68         char *tethering_passphrase;
69         bool tethering_hidden;
70
71         bool enable_persistent; /* Save the tech state */
72
73         GSList *driver_list;
74
75         DBusMessage *pending_reply;
76         guint pending_timeout;
77
78         GSList *scan_pending;
79
80         bool rfkill_driven;
81         bool softblocked;
82         bool hardblocked;
83         bool dbus_registered;
84 };
85
86 static GSList *driver_list = NULL;
87
88 static int technology_enabled(struct connman_technology *technology);
89 static int technology_disabled(struct connman_technology *technology);
90
91 static gint compare_priority(gconstpointer a, gconstpointer b)
92 {
93         const struct connman_technology_driver *driver1 = a;
94         const struct connman_technology_driver *driver2 = b;
95
96         return driver2->priority - driver1->priority;
97 }
98
99 static void rfkill_check(gpointer key, gpointer value, gpointer user_data)
100 {
101         struct connman_rfkill *rfkill = value;
102         enum connman_service_type type = GPOINTER_TO_INT(user_data);
103
104         /* Calling _technology_rfkill_add will update the tech. */
105         if (rfkill->type == type)
106                 __connman_technology_add_rfkill(rfkill->index, type,
107                                 rfkill->softblock, rfkill->hardblock);
108 }
109
110 bool
111 connman_technology_is_tethering_allowed(enum connman_service_type type)
112 {
113         static char *allowed_default[] = { "wifi", "bluetooth", "gadget",
114                                            NULL };
115         const char *type_str = __connman_service_type2string(type);
116         char **allowed;
117         int i;
118
119         if (!type_str)
120                 return false;
121
122         allowed = connman_setting_get_string_list("TetheringTechnologies");
123         if (!allowed)
124                 allowed = allowed_default;
125
126         for (i = 0; allowed[i]; i++) {
127                 if (g_strcmp0(allowed[i], type_str) == 0)
128                         return true;
129         }
130
131         return false;
132 }
133
134 static const char *get_name(enum connman_service_type type)
135 {
136         switch (type) {
137         case CONNMAN_SERVICE_TYPE_UNKNOWN:
138         case CONNMAN_SERVICE_TYPE_SYSTEM:
139         case CONNMAN_SERVICE_TYPE_GPS:
140         case CONNMAN_SERVICE_TYPE_VPN:
141                 break;
142         case CONNMAN_SERVICE_TYPE_GADGET:
143                 return "Gadget";
144         case CONNMAN_SERVICE_TYPE_ETHERNET:
145                 return "Wired";
146         case CONNMAN_SERVICE_TYPE_WIFI:
147                 return "WiFi";
148         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
149                 return "Bluetooth";
150         case CONNMAN_SERVICE_TYPE_CELLULAR:
151                 return "Cellular";
152         case CONNMAN_SERVICE_TYPE_P2P:
153                 return "P2P";
154         }
155
156         return NULL;
157 }
158
159 static void technology_save(struct connman_technology *technology)
160 {
161         GKeyFile *keyfile;
162         gchar *identifier;
163         const char *name = get_name(technology->type);
164
165         DBG("technology %p type %d name %s", technology, technology->type,
166                                                                         name);
167         if (!name)
168                 return;
169
170         keyfile = __connman_storage_load_global();
171         if (!keyfile)
172                 keyfile = g_key_file_new();
173
174         identifier = g_strdup_printf("%s", name);
175         if (!identifier)
176                 goto done;
177
178         g_key_file_set_boolean(keyfile, identifier, "Enable",
179                                 technology->enable_persistent);
180
181         g_key_file_set_boolean(keyfile, identifier, "Tethering",
182                                 technology->tethering_persistent);
183
184         g_key_file_set_boolean(keyfile, identifier, "Hidden",
185                                 technology->tethering_hidden);
186
187         if (technology->tethering_ident)
188                 g_key_file_set_string(keyfile, identifier,
189                                         "Tethering.Identifier",
190                                         technology->tethering_ident);
191
192         if (technology->tethering_passphrase)
193                 g_key_file_set_string(keyfile, identifier,
194                                         "Tethering.Passphrase",
195                                         technology->tethering_passphrase);
196
197 done:
198         g_free(identifier);
199
200         __connman_storage_save_global(keyfile);
201
202         g_key_file_free(keyfile);
203
204         return;
205 }
206
207 static void tethering_changed(struct connman_technology *technology)
208 {
209         dbus_bool_t tethering = technology->tethering;
210
211         connman_dbus_property_changed_basic(technology->path,
212                                 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
213                                                 DBUS_TYPE_BOOLEAN, &tethering);
214
215         technology_save(technology);
216 }
217
218 void connman_technology_tethering_notify(struct connman_technology *technology,
219                                                         bool enabled)
220 {
221         DBG("technology %p enabled %u", technology, enabled);
222
223         if (technology->tethering == enabled)
224                 return;
225
226         technology->tethering = enabled;
227
228         tethering_changed(technology);
229
230         if (enabled)
231                 __connman_tethering_set_enabled();
232         else
233                 __connman_tethering_set_disabled();
234 }
235
236 static int set_tethering(struct connman_technology *technology,
237                                 bool enabled)
238 {
239         int result = -EOPNOTSUPP;
240         int err;
241         const char *ident, *passphrase, *bridge;
242         GSList *tech_drivers;
243         bool hidden;
244
245         ident = technology->tethering_ident;
246         passphrase = technology->tethering_passphrase;
247         hidden = technology->tethering_hidden;
248
249         __sync_synchronize();
250         if (!technology->enabled)
251                 return -EACCES;
252
253         bridge = __connman_tethering_get_bridge();
254         if (!bridge)
255                 return -EOPNOTSUPP;
256
257         if (technology->type == CONNMAN_SERVICE_TYPE_WIFI && (!ident))
258                 return -EINVAL;
259
260         for (tech_drivers = technology->driver_list; tech_drivers;
261              tech_drivers = g_slist_next(tech_drivers)) {
262                 struct connman_technology_driver *driver = tech_drivers->data;
263
264                 if (!driver || !driver->set_tethering)
265                         continue;
266
267                 err = driver->set_tethering(technology, ident, passphrase,
268                                 bridge, enabled, hidden);
269
270                 if (result == -EINPROGRESS)
271                         continue;
272
273                 if (err == -EINPROGRESS || err == 0) {
274                         result = err;
275                         continue;
276                 }
277         }
278
279         return result;
280 }
281
282 void connman_technology_regdom_notify(struct connman_technology *technology,
283                                                         const char *alpha2)
284 {
285         DBG("");
286
287         if (!alpha2)
288                 connman_error("Failed to set regulatory domain");
289         else
290                 DBG("Regulatory domain set to %s", alpha2);
291
292         g_free(technology->regdom);
293         technology->regdom = g_strdup(alpha2);
294 }
295
296 static int set_regdom_by_device(struct connman_technology *technology,
297                                                         const char *alpha2)
298 {
299         GSList *list;
300
301         for (list = technology->device_list; list; list = list->next) {
302                 struct connman_device *device = list->data;
303
304                 if (connman_device_set_regdom(device, alpha2) != 0)
305                         return -ENOTSUP;
306         }
307
308         return 0;
309 }
310
311 int connman_technology_set_regdom(const char *alpha2)
312 {
313         GSList *list, *tech_drivers;
314
315         for (list = technology_list; list; list = list->next) {
316                 struct connman_technology *technology = list->data;
317
318                 if (set_regdom_by_device(technology, alpha2) != 0) {
319
320                         for (tech_drivers = technology->driver_list;
321                              tech_drivers;
322                              tech_drivers = g_slist_next(tech_drivers)) {
323
324                                 struct connman_technology_driver *driver =
325                                         tech_drivers->data;
326
327                                 if (driver->set_regdom)
328                                         driver->set_regdom(technology, alpha2);
329                         }
330                 }
331         }
332
333         return 0;
334 }
335
336 static struct connman_technology *technology_find(enum connman_service_type type)
337 {
338         GSList *list;
339
340         DBG("type %d", type);
341
342         for (list = technology_list; list; list = list->next) {
343                 struct connman_technology *technology = list->data;
344
345                 if (technology->type == type)
346                         return technology;
347         }
348
349         return NULL;
350 }
351
352 bool connman_technology_get_wifi_tethering(const char **ssid,
353                                                         const char **psk)
354 {
355         struct connman_technology *technology;
356
357         if (!ssid || !psk)
358                 return false;
359
360         *ssid = *psk = NULL;
361
362         technology = technology_find(CONNMAN_SERVICE_TYPE_WIFI);
363         if (!technology)
364                 return false;
365
366         if (!technology->tethering)
367                 return false;
368
369         *ssid = technology->tethering_ident;
370         *psk = technology->tethering_passphrase;
371
372         return true;
373 }
374
375 static void free_rfkill(gpointer data)
376 {
377         struct connman_rfkill *rfkill = data;
378
379         g_free(rfkill);
380 }
381
382 static void technology_load(struct connman_technology *technology)
383 {
384         GKeyFile *keyfile;
385         gchar *identifier;
386         GError *error = NULL;
387         bool enable, need_saving = false;
388
389         DBG("technology %p", technology);
390
391         keyfile = __connman_storage_load_global();
392         /* Fallback on disabling technology if file not found. */
393         if (!keyfile) {
394                 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
395                         /* We enable ethernet by default */
396                         technology->enable_persistent = true;
397                 else
398                         technology->enable_persistent = false;
399                 return;
400         }
401
402         identifier = g_strdup_printf("%s", get_name(technology->type));
403         if (!identifier)
404                 goto done;
405
406         enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
407         if (!error)
408                 technology->enable_persistent = enable;
409         else {
410                 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
411                         technology->enable_persistent = true;
412                 else
413                         technology->enable_persistent = false;
414
415                 need_saving = true;
416                 g_clear_error(&error);
417         }
418
419         enable = g_key_file_get_boolean(keyfile, identifier,
420                                         "Tethering", &error);
421         if (!error)
422                 technology->tethering_persistent = enable;
423         else {
424                 need_saving = true;
425                 g_clear_error(&error);
426         }
427
428         if (need_saving)
429                 technology_save(technology);
430
431         technology->tethering_ident = g_key_file_get_string(keyfile,
432                                 identifier, "Tethering.Identifier", NULL);
433
434         technology->tethering_passphrase = g_key_file_get_string(keyfile,
435                                 identifier, "Tethering.Passphrase", NULL);
436 done:
437         g_free(identifier);
438
439         g_key_file_free(keyfile);
440
441         return;
442 }
443
444 bool __connman_technology_get_offlinemode(void)
445 {
446         return global_offlinemode;
447 }
448
449 static void connman_technology_save_offlinemode(void)
450 {
451         GKeyFile *keyfile;
452
453         keyfile = __connman_storage_load_global();
454         if (!keyfile)
455                 keyfile = g_key_file_new();
456
457         g_key_file_set_boolean(keyfile, "global",
458                                         "OfflineMode", global_offlinemode);
459
460         __connman_storage_save_global(keyfile);
461
462         g_key_file_free(keyfile);
463
464         return;
465 }
466
467 static bool connman_technology_load_offlinemode(void)
468 {
469         GKeyFile *keyfile;
470         GError *error = NULL;
471         bool offlinemode;
472
473         /* If there is a error, we enable offlinemode */
474         keyfile = __connman_storage_load_global();
475         if (!keyfile)
476                 return false;
477
478         offlinemode = g_key_file_get_boolean(keyfile, "global",
479                                                 "OfflineMode", &error);
480         if (error) {
481                 offlinemode = false;
482                 g_clear_error(&error);
483         }
484
485         g_key_file_free(keyfile);
486
487         return offlinemode;
488 }
489
490 static void append_properties(DBusMessageIter *iter,
491                 struct connman_technology *technology)
492 {
493         DBusMessageIter dict;
494         dbus_bool_t val;
495         const char *str;
496
497         connman_dbus_dict_open(iter, &dict);
498
499         str = get_name(technology->type);
500         if (str)
501                 connman_dbus_dict_append_basic(&dict, "Name",
502                                                 DBUS_TYPE_STRING, &str);
503
504         str = __connman_service_type2string(technology->type);
505         if (str)
506                 connman_dbus_dict_append_basic(&dict, "Type",
507                                                 DBUS_TYPE_STRING, &str);
508
509         __sync_synchronize();
510         val = technology->enabled;
511         connman_dbus_dict_append_basic(&dict, "Powered",
512                                         DBUS_TYPE_BOOLEAN,
513                                         &val);
514
515         val = technology->connected;
516         connman_dbus_dict_append_basic(&dict, "Connected",
517                                         DBUS_TYPE_BOOLEAN,
518                                         &val);
519
520         val = technology->tethering;
521         connman_dbus_dict_append_basic(&dict, "Tethering",
522                                         DBUS_TYPE_BOOLEAN,
523                                         &val);
524
525         if (technology->tethering_ident)
526                 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
527                                         DBUS_TYPE_STRING,
528                                         &technology->tethering_ident);
529
530         if (technology->tethering_passphrase)
531                 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
532                                         DBUS_TYPE_STRING,
533                                         &technology->tethering_passphrase);
534
535         val = technology->tethering_hidden;
536         connman_dbus_dict_append_basic(&dict, "Hidden",
537                                         DBUS_TYPE_BOOLEAN,
538                                         &val);
539
540         connman_dbus_dict_close(iter, &dict);
541 }
542
543 static void technology_added_signal(struct connman_technology *technology)
544 {
545         DBusMessage *signal;
546         DBusMessageIter iter;
547
548         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
549                         CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
550         if (!signal)
551                 return;
552
553         dbus_message_iter_init_append(signal, &iter);
554         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
555                                                         &technology->path);
556         append_properties(&iter, technology);
557
558         dbus_connection_send(connection, signal, NULL);
559         dbus_message_unref(signal);
560 }
561
562 static void technology_removed_signal(struct connman_technology *technology)
563 {
564         g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
565                         CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
566                         DBUS_TYPE_OBJECT_PATH, &technology->path,
567                         DBUS_TYPE_INVALID);
568 }
569
570 static DBusMessage *get_properties(DBusConnection *conn,
571                                         DBusMessage *message, void *user_data)
572 {
573         struct connman_technology *technology = user_data;
574         DBusMessage *reply;
575         DBusMessageIter iter;
576
577         reply = dbus_message_new_method_return(message);
578         if (!reply)
579                 return NULL;
580
581         dbus_message_iter_init_append(reply, &iter);
582         append_properties(&iter, technology);
583
584         return reply;
585 }
586
587 void __connman_technology_list_struct(DBusMessageIter *array)
588 {
589         GSList *list;
590         DBusMessageIter entry;
591
592         for (list = technology_list; list; list = list->next) {
593                 struct connman_technology *technology = list->data;
594
595                 if (!technology->path ||
596                                 (technology->rfkill_driven &&
597                                  technology->hardblocked))
598                         continue;
599
600                 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
601                                 NULL, &entry);
602                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
603                                 &technology->path);
604                 append_properties(&entry, technology);
605                 dbus_message_iter_close_container(array, &entry);
606         }
607 }
608
609 static gboolean technology_pending_reply(gpointer user_data)
610 {
611         struct connman_technology *technology = user_data;
612         DBusMessage *reply;
613
614         /* Power request timedout, send ETIMEDOUT. */
615         if (technology->pending_reply) {
616                 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
617                 if (reply)
618                         g_dbus_send_message(connection, reply);
619
620                 dbus_message_unref(technology->pending_reply);
621                 technology->pending_reply = NULL;
622                 technology->pending_timeout = 0;
623         }
624
625         return FALSE;
626 }
627
628 static int technology_affect_devices(struct connman_technology *technology,
629                                                 bool enable_device)
630 {
631         int err = 0, err_dev;
632         GSList *list;
633
634         if (technology->type == CONNMAN_SERVICE_TYPE_P2P) {
635                 if (enable_device)
636                         __connman_technology_enabled(technology->type);
637                 else
638                         __connman_technology_disabled(technology->type);
639                 return 0;
640         }
641
642         for (list = technology->device_list; list; list = list->next) {
643                 struct connman_device *device = list->data;
644
645                 if (enable_device)
646                         err_dev = __connman_device_enable(device);
647                 else
648                         err_dev = __connman_device_disable(device);
649
650                 if (err_dev < 0 && err_dev != -EALREADY)
651                         err = err_dev;
652         }
653
654         return err;
655 }
656
657 static void powered_changed(struct connman_technology *technology)
658 {
659         dbus_bool_t enabled;
660
661         if (!technology->dbus_registered)
662                 return;
663
664         if (technology->pending_reply) {
665                 g_dbus_send_reply(connection,
666                                 technology->pending_reply, DBUS_TYPE_INVALID);
667                 dbus_message_unref(technology->pending_reply);
668                 technology->pending_reply = NULL;
669
670                 g_source_remove(technology->pending_timeout);
671                 technology->pending_timeout = 0;
672         }
673
674         __sync_synchronize();
675         enabled = technology->enabled;
676         connman_dbus_property_changed_basic(technology->path,
677                         CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
678                         DBUS_TYPE_BOOLEAN, &enabled);
679 }
680
681 static void enable_tethering(struct connman_technology *technology)
682 {
683         int ret;
684
685         if (!connman_setting_get_bool("PersistentTetheringMode"))
686                 return;
687
688         ret = set_tethering(technology, true);
689         if (ret < 0 && ret != -EALREADY)
690                 DBG("Cannot enable tethering yet for %s (%d/%s)",
691                         get_name(technology->type),
692                         -ret, strerror(-ret));
693 }
694
695 static int technology_enabled(struct connman_technology *technology)
696 {
697         __sync_synchronize();
698         if (technology->enabled)
699                 return -EALREADY;
700
701         technology->enabled = true;
702
703         if (technology->type == CONNMAN_SERVICE_TYPE_WIFI) {
704                 struct connman_technology *p2p;
705
706                 p2p = technology_find(CONNMAN_SERVICE_TYPE_P2P);
707                 if (p2p && !p2p->enabled && p2p->enable_persistent)
708                         technology_enabled(p2p);
709         }
710
711         if (technology->tethering_persistent)
712                 enable_tethering(technology);
713
714         powered_changed(technology);
715
716         return 0;
717 }
718
719 static int technology_enable(struct connman_technology *technology)
720 {
721         int err = 0;
722         int err_dev;
723
724         DBG("technology %p enable", technology);
725
726         __sync_synchronize();
727
728         if (technology->type == CONNMAN_SERVICE_TYPE_P2P) {
729                 struct connman_technology *wifi;
730
731                 wifi = technology_find(CONNMAN_SERVICE_TYPE_WIFI);
732                 if (wifi && wifi->enabled)
733                         return technology_enabled(technology);
734                 return 0;
735         }
736
737         if (technology->enabled)
738                 return -EALREADY;
739
740         if (technology->pending_reply)
741                 return -EBUSY;
742
743         if (connman_setting_get_bool("PersistentTetheringMode") &&
744                                         technology->tethering)
745                 set_tethering(technology, true);
746
747         if (technology->rfkill_driven)
748                 err = __connman_rfkill_block(technology->type, false);
749
750         err_dev = technology_affect_devices(technology, true);
751
752         if (!technology->rfkill_driven)
753                 err = err_dev;
754
755         return err;
756 }
757
758 static int technology_disabled(struct connman_technology *technology)
759 {
760         __sync_synchronize();
761         if (!technology->enabled)
762                 return -EALREADY;
763
764         technology->enabled = false;
765
766         powered_changed(technology);
767
768         return 0;
769 }
770
771 static int technology_disable(struct connman_technology *technology)
772 {
773         int err;
774
775         DBG("technology %p disable", technology);
776
777         __sync_synchronize();
778
779         if (technology->type == CONNMAN_SERVICE_TYPE_P2P) {
780                 technology->enable_persistent = false;
781                 return technology_disabled(technology);
782         } else if (technology->type == CONNMAN_SERVICE_TYPE_WIFI) {
783                 struct connman_technology *p2p;
784
785                 p2p = technology_find(CONNMAN_SERVICE_TYPE_P2P);
786                 if (p2p && p2p->enabled) {
787                         p2p->enable_persistent = true;
788                         technology_disabled(p2p);
789                 }
790         }
791
792         if (!technology->enabled)
793                 return -EALREADY;
794
795         if (technology->pending_reply)
796                 return -EBUSY;
797
798         if (technology->tethering)
799                 set_tethering(technology, false);
800
801         err = technology_affect_devices(technology, false);
802
803         if (technology->rfkill_driven)
804                 err = __connman_rfkill_block(technology->type, true);
805
806         return err;
807 }
808
809 static DBusMessage *set_powered(struct connman_technology *technology,
810                                 DBusMessage *msg, bool powered)
811 {
812         DBusMessage *reply = NULL;
813         int err = 0;
814
815         if (technology->rfkill_driven && technology->hardblocked) {
816                 err = -EACCES;
817                 goto make_reply;
818         }
819
820         if (powered)
821                 err = technology_enable(technology);
822         else
823                 err = technology_disable(technology);
824
825         if (err != -EBUSY) {
826                 technology->enable_persistent = powered;
827                 technology_save(technology);
828         }
829
830 make_reply:
831         if (err == -EINPROGRESS) {
832                 technology->pending_reply = dbus_message_ref(msg);
833                 technology->pending_timeout = g_timeout_add_seconds(10,
834                                         technology_pending_reply, technology);
835         } else if (err == -EALREADY) {
836                 if (powered)
837                         reply = __connman_error_already_enabled(msg);
838                 else
839                         reply = __connman_error_already_disabled(msg);
840         } else if (err < 0)
841                 reply = __connman_error_failed(msg, -err);
842         else
843                 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
844
845         return reply;
846 }
847
848 static DBusMessage *set_property(DBusConnection *conn,
849                                         DBusMessage *msg, void *data)
850 {
851         struct connman_technology *technology = data;
852         DBusMessageIter iter, value;
853         const char *name;
854         int type;
855
856         DBG("conn %p", conn);
857
858         if (!dbus_message_iter_init(msg, &iter))
859                 return __connman_error_invalid_arguments(msg);
860
861         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
862                 return __connman_error_invalid_arguments(msg);
863
864         dbus_message_iter_get_basic(&iter, &name);
865         dbus_message_iter_next(&iter);
866
867         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
868                 return __connman_error_invalid_arguments(msg);
869
870         dbus_message_iter_recurse(&iter, &value);
871
872         type = dbus_message_iter_get_arg_type(&value);
873
874         DBG("property %s", name);
875
876         if (technology->type == CONNMAN_SERVICE_TYPE_WIFI && technology->connected) {
877                 uid_t uid;
878                 if (connman_dbus_get_connection_unix_user_sync(conn,
879                                                 dbus_message_get_sender(msg),
880                                                 &uid) < 0) {
881                         DBG("Can not get unix user id!");
882                         return __connman_error_permission_denied(msg);
883                 }
884
885                 if (!__connman_service_is_user_allowed(CONNMAN_SERVICE_TYPE_WIFI, uid)) {
886                         DBG("Not allow this user to operate wifi technology now!");
887                         return __connman_error_permission_denied(msg);
888                 }
889         }
890
891         if (g_str_equal(name, "Tethering")) {
892                 dbus_bool_t tethering;
893                 int err;
894
895                 if (type != DBUS_TYPE_BOOLEAN)
896                         return __connman_error_invalid_arguments(msg);
897
898                 if (!connman_technology_is_tethering_allowed(technology->type)) {
899                         DBG("%s tethering not allowed by config file",
900                                 __connman_service_type2string(technology->type));
901                         return __connman_error_not_supported(msg);
902                 }
903
904                 dbus_message_iter_get_basic(&value, &tethering);
905
906                 if (technology->tethering == tethering) {
907                         if (!tethering)
908                                 return __connman_error_already_disabled(msg);
909                         else
910                                 return __connman_error_already_enabled(msg);
911                 }
912
913                 err = set_tethering(technology, tethering);
914                 if (err < 0)
915                         return __connman_error_failed(msg, -err);
916
917                 technology->tethering_persistent = tethering;
918
919                 technology_save(technology);
920
921         } else if (g_str_equal(name, "TetheringIdentifier")) {
922                 const char *str;
923
924                 dbus_message_iter_get_basic(&value, &str);
925
926                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
927                         return __connman_error_not_supported(msg);
928
929                 if (strlen(str) < 1 || strlen(str) > 32)
930                         return __connman_error_invalid_arguments(msg);
931
932                 if (g_strcmp0(technology->tethering_ident, str) != 0) {
933                         g_free(technology->tethering_ident);
934                         technology->tethering_ident = g_strdup(str);
935                         technology_save(technology);
936
937                         connman_dbus_property_changed_basic(technology->path,
938                                                 CONNMAN_TECHNOLOGY_INTERFACE,
939                                                 "TetheringIdentifier",
940                                                 DBUS_TYPE_STRING,
941                                                 &technology->tethering_ident);
942                 }
943         } else if (g_str_equal(name, "TetheringPassphrase")) {
944                 const char *str;
945
946                 dbus_message_iter_get_basic(&value, &str);
947
948                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
949                         return __connman_error_not_supported(msg);
950
951                 if (strlen(str) < 8 || strlen(str) > 63) {
952                         if (g_str_equal(str, "")) {
953                                 technology->tethering_passphrase = NULL;
954
955                                 connman_dbus_property_changed_basic(technology->path,
956                                                 CONNMAN_TECHNOLOGY_INTERFACE,
957                                                 "TetheringPassphrase",
958                                                 DBUS_TYPE_STRING,
959                                                 &str);
960                         }
961                         else
962                                 return __connman_error_passphrase_required(msg);
963                 } else {
964                         if (g_strcmp0(technology->tethering_passphrase, str) != 0) {
965                                 g_free(technology->tethering_passphrase);
966                                 technology->tethering_passphrase = g_strdup(str);
967                                 technology_save(technology);
968
969                                 connman_dbus_property_changed_basic(technology->path,
970                                                 CONNMAN_TECHNOLOGY_INTERFACE,
971                                                 "TetheringPassphrase",
972                                                 DBUS_TYPE_STRING,
973                                                 &technology->tethering_passphrase);
974                         }
975                 }
976         } else if (g_str_equal(name, "Hidden")) {
977                 dbus_bool_t hidden;
978
979                 if (type != DBUS_TYPE_BOOLEAN)
980                         return __connman_error_invalid_arguments(msg);
981
982                 dbus_message_iter_get_basic(&value, &hidden);
983
984                 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
985                         return __connman_error_not_supported(msg);
986
987                 technology->tethering_hidden = hidden;
988                 technology_save(technology);
989
990                 connman_dbus_property_changed_basic(technology->path,
991                                         CONNMAN_TECHNOLOGY_INTERFACE,
992                                         "Hidden",
993                                         DBUS_TYPE_BOOLEAN,
994                                         &hidden);
995         } else if (g_str_equal(name, "Powered")) {
996                 dbus_bool_t enable;
997
998                 if (type != DBUS_TYPE_BOOLEAN)
999                         return __connman_error_invalid_arguments(msg);
1000
1001                 dbus_message_iter_get_basic(&value, &enable);
1002
1003                 return set_powered(technology, msg, enable);
1004         } else
1005                 return __connman_error_invalid_property(msg);
1006
1007         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1008 }
1009
1010 static void reply_scan_pending(struct connman_technology *technology, int err)
1011 {
1012         DBusMessage *reply;
1013
1014         DBG("technology %p err %d", technology, err);
1015
1016         while (technology->scan_pending) {
1017                 DBusMessage *msg = technology->scan_pending->data;
1018
1019                 DBG("reply to %s", dbus_message_get_sender(msg));
1020
1021                 if (err == 0)
1022                         reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1023                 else
1024                         reply = __connman_error_failed(msg, -err);
1025                 g_dbus_send_message(connection, reply);
1026                 dbus_message_unref(msg);
1027
1028                 technology->scan_pending =
1029                         g_slist_delete_link(technology->scan_pending,
1030                                         technology->scan_pending);
1031         }
1032 }
1033
1034 #if defined TIZEN_EXT
1035 dbus_bool_t __connman_technology_notify_scan_changed(const char *key, void *val)
1036 {
1037         DBG("key %s", key);
1038         DBusMessage *signal;
1039         DBusMessageIter iter;
1040         dbus_bool_t result = FALSE;
1041
1042         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1043                         CONNMAN_MANAGER_INTERFACE, "ScanChanged");
1044         if (!signal)
1045                 return result;
1046
1047         dbus_message_iter_init_append(signal, &iter);
1048         connman_dbus_property_append_basic(&iter, key, DBUS_TYPE_BOOLEAN, val);
1049
1050         result = dbus_connection_send(connection, signal, NULL);
1051         dbus_message_unref(signal);
1052
1053         DBG("Successfuly sent signal");
1054
1055         return result;
1056 }
1057 #endif
1058
1059 void __connman_technology_scan_started(struct connman_device *device)
1060 {
1061         DBG("device %p", device);
1062 #if defined TIZEN_EXT
1063         dbus_bool_t status = 1;
1064         __connman_technology_notify_scan_changed("scan_started", &status);
1065 #endif
1066 }
1067
1068 void __connman_technology_scan_stopped(struct connman_device *device,
1069                                         enum connman_service_type type)
1070 {
1071         int count = 0;
1072         struct connman_technology *technology;
1073         GSList *list;
1074
1075         technology = technology_find(type);
1076
1077         DBG("technology %p device %p", technology, device);
1078
1079         if (!technology)
1080                 return;
1081
1082         for (list = technology->device_list; list; list = list->next) {
1083                 struct connman_device *other_device = list->data;
1084
1085                 if (device == other_device)
1086                         continue;
1087
1088                 if (__connman_device_get_service_type(other_device) != type)
1089                         continue;
1090
1091                 if (connman_device_get_scanning(other_device))
1092                         count += 1;
1093         }
1094
1095 #if defined TIZEN_EXT
1096         if (count == 0) {
1097                 DBusMessage *signal;
1098                 dbus_bool_t status = 0;
1099                 __connman_technology_notify_scan_changed("scan_done", &status);
1100
1101                 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1102                                                                                 CONNMAN_MANAGER_INTERFACE, "ScanDone");
1103                 if (!signal)
1104                         return;
1105
1106                 dbus_connection_send(connection, signal, NULL);
1107                 dbus_message_unref(signal);
1108                 reply_scan_pending(technology, 0);
1109         }
1110 #else
1111         if (count == 0)
1112                 reply_scan_pending(technology, 0);
1113 #endif
1114 }
1115
1116 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
1117                                                 int result, const char *alpha2)
1118 {
1119         bool regdom_set = false;
1120         struct connman_technology *technology;
1121         enum connman_service_type type;
1122         GSList *tech_drivers;
1123
1124         type = __connman_device_get_service_type(device);
1125         technology = technology_find(type);
1126
1127         if (!technology)
1128                 return;
1129
1130         if (result < 0) {
1131
1132                 for (tech_drivers = technology->driver_list;
1133                      tech_drivers;
1134                      tech_drivers = g_slist_next(tech_drivers)) {
1135                         struct connman_technology_driver *driver =
1136                                 tech_drivers->data;
1137
1138                         if (driver->set_regdom) {
1139                                 driver->set_regdom(technology, alpha2);
1140                                 regdom_set = true;
1141                         }
1142
1143                 }
1144
1145                 if (!regdom_set)
1146                         alpha2 = NULL;
1147         }
1148
1149         connman_technology_regdom_notify(technology, alpha2);
1150 }
1151
1152 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
1153 {
1154         struct connman_technology *technology = data;
1155         int err;
1156
1157         DBG("technology %p request from %s", technology,
1158                         dbus_message_get_sender(msg));
1159
1160         dbus_message_ref(msg);
1161         technology->scan_pending =
1162                 g_slist_prepend(technology->scan_pending, msg);
1163
1164         err = __connman_device_request_scan(technology->type);
1165         if (err < 0)
1166                 reply_scan_pending(technology, err);
1167
1168         return NULL;
1169 }
1170
1171 #if defined TIZEN_EXT
1172 static DBusMessage *get_scan_state(DBusConnection *conn, DBusMessage *msg, void *data)
1173 {
1174         DBusMessage *reply;
1175         DBusMessageIter iter, dict;
1176         GSList *list;
1177         struct connman_technology *technology = data;
1178         dbus_bool_t scanning = false;
1179
1180         DBG("technology %p", technology);
1181
1182         for (list = technology->device_list; list; list = list->next) {
1183                 struct connman_device *device = list->data;
1184                 scanning = connman_device_get_scanning(device);
1185                 if(scanning)
1186                         break;
1187         }
1188
1189         DBG("scanning : %d", scanning);
1190         reply = dbus_message_new_method_return(msg);
1191         if (!reply)
1192                 return NULL;
1193
1194         dbus_message_iter_init_append(reply, &iter);
1195
1196         connman_dbus_dict_open(&iter, &dict);
1197         connman_dbus_dict_append_basic(&dict, "Scanstate",
1198                                         DBUS_TYPE_BOOLEAN,
1199                                         &scanning);
1200
1201         connman_dbus_dict_close(&iter, &dict);
1202
1203         return reply;
1204 }
1205 #endif
1206
1207 static const GDBusMethodTable technology_methods[] = {
1208         { GDBUS_DEPRECATED_METHOD("GetProperties",
1209                         NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
1210                         get_properties) },
1211         { GDBUS_ASYNC_METHOD("SetProperty",
1212                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
1213                         NULL, set_property) },
1214         { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
1215 #if defined TIZEN_EXT
1216         { GDBUS_METHOD("GetScanState", NULL, GDBUS_ARGS({ "scan_state", "a{sv}" }),
1217                         get_scan_state) },
1218 #endif
1219         { },
1220 };
1221
1222 static const GDBusSignalTable technology_signals[] = {
1223         { GDBUS_SIGNAL("PropertyChanged",
1224                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
1225         { GDBUS_SIGNAL("DhcpConnected",
1226                         GDBUS_ARGS({ "aptype", "s" },
1227                                 { "ipaddr", "s" },
1228                                 { "macaddr", "s" },
1229                                 { "hostname", "s" })) },
1230         { GDBUS_SIGNAL("DhcpLeaseDeleted",
1231                         GDBUS_ARGS({ "aptype", "s" },
1232                                 { "ipaddr", "s" },
1233                                 { "macaddr", "s" },
1234                                 { "hostname", "s" })) },
1235         { },
1236 };
1237
1238 static bool technology_dbus_register(struct connman_technology *technology)
1239 {
1240         if (technology->dbus_registered ||
1241                                 (technology->rfkill_driven &&
1242                                  technology->hardblocked))
1243                 return true;
1244
1245         if (!g_dbus_register_interface(connection, technology->path,
1246                                         CONNMAN_TECHNOLOGY_INTERFACE,
1247                                         technology_methods, technology_signals,
1248                                         NULL, technology, NULL)) {
1249                 connman_error("Failed to register %s", technology->path);
1250                 return false;
1251         }
1252
1253         technology_added_signal(technology);
1254         technology->dbus_registered = true;
1255
1256         return true;
1257 }
1258
1259 static void technology_dbus_unregister(struct connman_technology *technology)
1260 {
1261         if (!technology->dbus_registered)
1262                 return;
1263
1264         technology_removed_signal(technology);
1265         g_dbus_unregister_interface(connection, technology->path,
1266                 CONNMAN_TECHNOLOGY_INTERFACE);
1267
1268         technology->dbus_registered = false;
1269 }
1270
1271 static void technology_put(struct connman_technology *technology)
1272 {
1273         DBG("technology %p", technology);
1274
1275         if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1276                 return;
1277
1278         reply_scan_pending(technology, -EINTR);
1279
1280         while (technology->driver_list) {
1281                 struct connman_technology_driver *driver;
1282
1283                 driver = technology->driver_list->data;
1284
1285                 if (driver->remove)
1286                         driver->remove(technology);
1287
1288                 technology->driver_list =
1289                         g_slist_delete_link(technology->driver_list,
1290                                         technology->driver_list);
1291         }
1292
1293         technology_list = g_slist_remove(technology_list, technology);
1294
1295         technology_dbus_unregister(technology);
1296
1297         g_slist_free(technology->device_list);
1298
1299         g_free(technology->path);
1300         g_free(technology->regdom);
1301         g_free(technology->tethering_ident);
1302         g_free(technology->tethering_passphrase);
1303         g_free(technology);
1304 }
1305
1306 static struct connman_technology *technology_get(enum connman_service_type type)
1307 {
1308         GSList *tech_drivers = NULL;
1309         struct connman_technology_driver *driver;
1310         struct connman_technology *technology;
1311         const char *str;
1312         GSList *list;
1313
1314         DBG("type %d", type);
1315
1316         str = __connman_service_type2string(type);
1317         if (!str)
1318                 return NULL;
1319
1320         technology = technology_find(type);
1321         if (technology) {
1322                 if (type != CONNMAN_SERVICE_TYPE_P2P)
1323                         __sync_fetch_and_add(&technology->refcount, 1);
1324                 return technology;
1325         }
1326
1327         /* First check if we have a driver for this technology type */
1328         for (list = driver_list; list; list = list->next) {
1329                 driver = list->data;
1330
1331                 if (driver->type == type) {
1332                         DBG("technology %p driver %p", technology, driver);
1333                         tech_drivers = g_slist_append(tech_drivers, driver);
1334                 }
1335         }
1336
1337         if (!tech_drivers) {
1338                 DBG("No matching drivers found for %s.",
1339                                 __connman_service_type2string(type));
1340                 return NULL;
1341         }
1342
1343         technology = g_try_new0(struct connman_technology, 1);
1344         if (!technology)
1345                 return NULL;
1346
1347         technology->refcount = 1;
1348         technology->type = type;
1349         technology->tethering_hidden = FALSE;
1350         technology->path = g_strdup_printf("%s/technology/%s",
1351                                                         CONNMAN_PATH, str);
1352
1353         technology_load(technology);
1354         technology_list = g_slist_prepend(technology_list, technology);
1355         technology->driver_list = tech_drivers;
1356
1357         for (list = tech_drivers; list; list = list->next) {
1358                 driver = list->data;
1359
1360                 if (driver->probe && driver->probe(technology) < 0)
1361                         DBG("Driver probe failed for technology %p",
1362                                         technology);
1363         }
1364
1365         if (!technology_dbus_register(technology)) {
1366                 technology_put(technology);
1367                 return NULL;
1368         }
1369
1370         if (type == CONNMAN_SERVICE_TYPE_P2P) {
1371                 struct connman_technology *wifi;
1372                 bool enable;
1373
1374                 enable = technology->enable_persistent;
1375
1376                 wifi = technology_find(CONNMAN_SERVICE_TYPE_WIFI);
1377                 if (enable && wifi)
1378                         enable = wifi->enabled;
1379
1380                 technology_affect_devices(technology, enable);
1381         }
1382
1383         DBG("technology %p %s", technology, get_name(technology->type));
1384
1385         return technology;
1386 }
1387
1388 int connman_technology_driver_register(struct connman_technology_driver *driver)
1389 {
1390         GSList *list;
1391         struct connman_device *device;
1392         enum connman_service_type type;
1393
1394         for (list = driver_list; list; list = list->next) {
1395                 if (list->data == driver)
1396                         goto exist;
1397         }
1398
1399         DBG("Registering %s driver", driver->name);
1400
1401         driver_list = g_slist_insert_sorted(driver_list, driver,
1402                                                         compare_priority);
1403
1404         /*
1405          * Check for technology less devices if this driver
1406          * can service any of them.
1407         */
1408         for (list = techless_device_list; list; list = list->next) {
1409                 device = list->data;
1410
1411                 type = __connman_device_get_service_type(device);
1412                 if (type != driver->type)
1413                         continue;
1414
1415                 techless_device_list = g_slist_remove(techless_device_list,
1416                                                                 device);
1417
1418                 __connman_technology_add_device(device);
1419         }
1420
1421         /* Check for orphaned rfkill switches. */
1422         g_hash_table_foreach(rfkill_list, rfkill_check,
1423                                         GINT_TO_POINTER(driver->type));
1424
1425 exist:
1426         if (driver->type == CONNMAN_SERVICE_TYPE_P2P) {
1427                 if (!technology_get(CONNMAN_SERVICE_TYPE_P2P))
1428                         return -ENOMEM;
1429         }
1430
1431         return 0;
1432 }
1433
1434 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
1435 {
1436         GSList *list, *tech_drivers;
1437         struct connman_technology *technology;
1438         struct connman_technology_driver *current;
1439
1440         DBG("Unregistering driver %p name %s", driver, driver->name);
1441
1442         for (list = technology_list; list; list = list->next) {
1443                 technology = list->data;
1444
1445                 for (tech_drivers = technology->driver_list; tech_drivers;
1446                                 tech_drivers = g_slist_next(tech_drivers)) {
1447                         current = tech_drivers->data;
1448                         if (driver != current)
1449                                 continue;
1450
1451                         if (driver->remove)
1452                                 driver->remove(technology);
1453
1454                         technology->driver_list =
1455                                 g_slist_remove(technology->driver_list,
1456                                                                 driver);
1457                         break;
1458                 }
1459         }
1460
1461         driver_list = g_slist_remove(driver_list, driver);
1462
1463         if (driver->type == CONNMAN_SERVICE_TYPE_P2P) {
1464                 technology = technology_find(CONNMAN_SERVICE_TYPE_P2P);
1465                 if (technology)
1466                         technology_put(technology);
1467         }
1468 }
1469
1470 void __connman_technology_add_interface(enum connman_service_type type,
1471                                 int index, const char *ident)
1472 {
1473         struct connman_technology *technology;
1474         GSList *tech_drivers;
1475         struct connman_technology_driver *driver;
1476         char *name;
1477
1478         switch (type) {
1479         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1480         case CONNMAN_SERVICE_TYPE_SYSTEM:
1481                 return;
1482         case CONNMAN_SERVICE_TYPE_ETHERNET:
1483         case CONNMAN_SERVICE_TYPE_WIFI:
1484         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1485         case CONNMAN_SERVICE_TYPE_CELLULAR:
1486         case CONNMAN_SERVICE_TYPE_GPS:
1487         case CONNMAN_SERVICE_TYPE_VPN:
1488         case CONNMAN_SERVICE_TYPE_GADGET:
1489         case CONNMAN_SERVICE_TYPE_P2P:
1490                 break;
1491         }
1492
1493         name = connman_inet_ifname(index);
1494         connman_info("Adding interface %s [ %s ]", name,
1495                                 __connman_service_type2string(type));
1496
1497         technology = technology_find(type);
1498
1499         if (!technology)
1500                 goto out;
1501
1502         for (tech_drivers = technology->driver_list; tech_drivers;
1503              tech_drivers = g_slist_next(tech_drivers)) {
1504                 driver = tech_drivers->data;
1505
1506                 if (driver->add_interface)
1507                         driver->add_interface(technology, index, name, ident);
1508         }
1509
1510         /*
1511          * At this point we can try to enable tethering automatically as
1512          * now the interfaces are set properly.
1513          */
1514         if (technology->tethering_persistent)
1515                 enable_tethering(technology);
1516
1517 out:
1518         g_free(name);
1519 }
1520
1521 void __connman_technology_remove_interface(enum connman_service_type type,
1522                                 int index, const char *ident)
1523 {
1524         struct connman_technology *technology;
1525         GSList *tech_drivers;
1526         struct connman_technology_driver *driver;
1527         char *name;
1528
1529         switch (type) {
1530         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1531         case CONNMAN_SERVICE_TYPE_SYSTEM:
1532                 return;
1533         case CONNMAN_SERVICE_TYPE_ETHERNET:
1534         case CONNMAN_SERVICE_TYPE_WIFI:
1535         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1536         case CONNMAN_SERVICE_TYPE_CELLULAR:
1537         case CONNMAN_SERVICE_TYPE_GPS:
1538         case CONNMAN_SERVICE_TYPE_VPN:
1539         case CONNMAN_SERVICE_TYPE_GADGET:
1540         case CONNMAN_SERVICE_TYPE_P2P:
1541                 break;
1542         }
1543
1544         name = connman_inet_ifname(index);
1545         connman_info("Remove interface %s [ %s ]", name,
1546                                 __connman_service_type2string(type));
1547         g_free(name);
1548
1549         technology = technology_find(type);
1550
1551         if (!technology)
1552                 return;
1553
1554         for (tech_drivers = technology->driver_list; tech_drivers;
1555              tech_drivers = g_slist_next(tech_drivers)) {
1556                 driver = tech_drivers->data;
1557
1558                 if (driver->remove_interface)
1559                         driver->remove_interface(technology, index);
1560         }
1561 }
1562
1563 int __connman_technology_add_device(struct connman_device *device)
1564 {
1565         struct connman_technology *technology;
1566         enum connman_service_type type;
1567
1568         type = __connman_device_get_service_type(device);
1569
1570         DBG("device %p type %s", device, get_name(type));
1571
1572         technology = technology_get(type);
1573         if (!technology) {
1574                 /*
1575                  * Since no driver can be found for this device at the moment we
1576                  * add it to the techless device list.
1577                 */
1578                 techless_device_list = g_slist_prepend(techless_device_list,
1579                                                                 device);
1580
1581                 return -ENXIO;
1582         }
1583
1584         __sync_synchronize();
1585         if (technology->rfkill_driven) {
1586                 if (technology->enabled)
1587                         __connman_device_enable(device);
1588                 else
1589                         __connman_device_disable(device);
1590
1591                 goto done;
1592         }
1593
1594         if (technology->enable_persistent &&
1595                                         !global_offlinemode) {
1596                 int err = __connman_device_enable(device);
1597                 /*
1598                  * connman_technology_add_device() calls __connman_device_enable()
1599                  * but since the device is already enabled, the calls does not
1600                  * propagate through to connman_technology_enabled via
1601                  * connman_device_set_powered.
1602                  */
1603                 if (err == -EALREADY)
1604                         __connman_technology_enabled(type);
1605         }
1606         /* if technology persistent state is offline */
1607         if (!technology->enable_persistent)
1608                 __connman_device_disable(device);
1609
1610 done:
1611         technology->device_list = g_slist_prepend(technology->device_list,
1612                                                                 device);
1613
1614         return 0;
1615 }
1616
1617 int __connman_technology_remove_device(struct connman_device *device)
1618 {
1619         struct connman_technology *technology;
1620         enum connman_service_type type;
1621
1622         DBG("device %p", device);
1623
1624         type = __connman_device_get_service_type(device);
1625
1626         technology = technology_find(type);
1627         if (!technology) {
1628                 techless_device_list = g_slist_remove(techless_device_list,
1629                                                                 device);
1630                 return -ENXIO;
1631         }
1632
1633         technology->device_list = g_slist_remove(technology->device_list,
1634                                                                 device);
1635
1636         if (technology->tethering)
1637                 set_tethering(technology, false);
1638
1639         technology_put(technology);
1640
1641         return 0;
1642 }
1643
1644 int __connman_technology_enabled(enum connman_service_type type)
1645 {
1646         struct connman_technology *technology;
1647
1648         technology = technology_find(type);
1649         if (!technology)
1650                 return -ENXIO;
1651
1652         DBG("technology %p type %s rfkill %d enabled %d", technology,
1653                 get_name(type), technology->rfkill_driven,
1654                 technology->enabled);
1655 #if !defined TIZEN_EXT
1656         if (technology->rfkill_driven) {
1657                 if (technology->tethering_persistent)
1658                         enable_tethering(technology);
1659                 return 0;
1660         }
1661 #endif
1662
1663         return technology_enabled(technology);
1664 }
1665
1666 int __connman_technology_disabled(enum connman_service_type type)
1667 {
1668         struct connman_technology *technology;
1669         GSList *list;
1670
1671         technology = technology_find(type);
1672         if (!technology)
1673                 return -ENXIO;
1674 #if !defined TIZEN_EXT
1675         if (technology->rfkill_driven)
1676                 return 0;
1677 #endif
1678         for (list = technology->device_list; list; list = list->next) {
1679                 struct connman_device *device = list->data;
1680
1681                 if (connman_device_get_powered(device))
1682                         return 0;
1683         }
1684
1685         return technology_disabled(technology);
1686 }
1687
1688 int __connman_technology_set_offlinemode(bool offlinemode)
1689 {
1690         GSList *list;
1691         int err = -EINVAL, enabled_tech_count = 0;
1692
1693         if (global_offlinemode == offlinemode)
1694                 return 0;
1695
1696         DBG("offlinemode %s", offlinemode ? "On" : "Off");
1697
1698         /*
1699          * This is a bit tricky. When you set offlinemode, there is no
1700          * way to differentiate between attempting offline mode and
1701          * resuming offlinemode from last saved profile. We need that
1702          * information in rfkill_update, otherwise it falls back on the
1703          * technology's persistent state. Hence we set the offline mode here
1704          * but save it & call the notifier only if its successful.
1705          */
1706
1707         global_offlinemode = offlinemode;
1708
1709         /* Traverse technology list, enable/disable each technology. */
1710         for (list = technology_list; list; list = list->next) {
1711                 struct connman_technology *technology = list->data;
1712
1713                 if (offlinemode)
1714                         err = technology_disable(technology);
1715                 else {
1716                         if (technology->hardblocked)
1717                                 continue;
1718
1719                         if (technology->enable_persistent) {
1720                                 err = technology_enable(technology);
1721                                 enabled_tech_count++;
1722                         }
1723                 }
1724         }
1725
1726         if (err == 0 || err == -EINPROGRESS || err == -EALREADY ||
1727                         (err == -EINVAL && enabled_tech_count == 0)) {
1728                 connman_technology_save_offlinemode();
1729                 __connman_notifier_offlinemode(offlinemode);
1730         } else
1731                 global_offlinemode = connman_technology_load_offlinemode();
1732
1733         return err;
1734 }
1735
1736 void __connman_technology_set_connected(enum connman_service_type type,
1737                 bool connected)
1738 {
1739         struct connman_technology *technology;
1740         dbus_bool_t val;
1741
1742         technology = technology_find(type);
1743         if (!technology)
1744                 return;
1745
1746         DBG("technology %p connected %d", technology, connected);
1747
1748         technology->connected = connected;
1749
1750         val = connected;
1751         connman_dbus_property_changed_basic(technology->path,
1752                         CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1753                         DBUS_TYPE_BOOLEAN, &val);
1754 }
1755
1756 static bool technology_apply_rfkill_change(struct connman_technology *technology,
1757                                                 bool softblock,
1758                                                 bool hardblock,
1759                                                 bool new_rfkill)
1760 {
1761         bool hardblock_changed = false;
1762         bool apply = true;
1763         GList *start, *list;
1764
1765         DBG("technology %p --> %d/%d vs %d/%d",
1766                         technology, softblock, hardblock,
1767                         technology->softblocked, technology->hardblocked);
1768
1769         if (technology->hardblocked == hardblock)
1770                 goto softblock_change;
1771
1772         if (!(new_rfkill && !hardblock)) {
1773                 start = g_hash_table_get_values(rfkill_list);
1774
1775                 for (list = start; list; list = list->next) {
1776                         struct connman_rfkill *rfkill = list->data;
1777
1778                         if (rfkill->type != technology->type)
1779                                 continue;
1780
1781                         if (rfkill->hardblock != hardblock)
1782                                 apply = false;
1783                 }
1784
1785                 g_list_free(start);
1786         }
1787
1788         if (!apply)
1789                 goto softblock_change;
1790
1791         technology->hardblocked = hardblock;
1792         hardblock_changed = true;
1793
1794 softblock_change:
1795         if (!apply && technology->softblocked != softblock)
1796                 apply = true;
1797
1798         if (!apply)
1799                 return technology->hardblocked;
1800
1801         technology->softblocked = softblock;
1802
1803         if (technology->hardblocked ||
1804                                         technology->softblocked) {
1805                 if (technology_disabled(technology) != -EALREADY)
1806                         technology_affect_devices(technology, false);
1807         } else if (!technology->hardblocked &&
1808                                         !technology->softblocked) {
1809                 if (technology_enabled(technology) != -EALREADY)
1810                         technology_affect_devices(technology, true);
1811         }
1812
1813         if (hardblock_changed) {
1814                 if (technology->hardblocked) {
1815                         DBG("%s is switched off.", get_name(technology->type));
1816                         technology_dbus_unregister(technology);
1817                 } else {
1818                         DBG("%s is switched on.", get_name(technology->type));
1819                         technology_dbus_register(technology);
1820
1821                         if (global_offlinemode)
1822                                 __connman_rfkill_block(technology->type, true);
1823                 }
1824         }
1825
1826         return technology->hardblocked;
1827 }
1828
1829 int __connman_technology_add_rfkill(unsigned int index,
1830                                         enum connman_service_type type,
1831                                                 bool softblock,
1832                                                 bool hardblock)
1833 {
1834         struct connman_technology *technology;
1835         struct connman_rfkill *rfkill;
1836
1837         DBG("index %u type %d soft %u hard %u", index, type,
1838                                                         softblock, hardblock);
1839
1840         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1841         if (rfkill)
1842                 goto done;
1843
1844         rfkill = g_try_new0(struct connman_rfkill, 1);
1845         if (!rfkill)
1846                 return -ENOMEM;
1847
1848         rfkill->index = index;
1849         rfkill->type = type;
1850         rfkill->softblock = softblock;
1851         rfkill->hardblock = hardblock;
1852
1853         g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1854
1855 done:
1856         technology = technology_get(type);
1857         /* If there is no driver for this type, ignore it. */
1858         if (!technology)
1859                 return -ENXIO;
1860
1861         technology->rfkill_driven = true;
1862
1863 #if !defined TIZEN_EXT
1864         /* If hardblocked, there is no need to handle softblocked state */
1865         if (technology_apply_rfkill_change(technology,
1866                                 softblock, hardblock, true))
1867                 return 0;
1868 #endif
1869         if (global_offlinemode)
1870                 return 0;
1871
1872         /*
1873          * Depending on softblocked state we unblock/block according to
1874          * offlinemode and persistente state.
1875          */
1876         if (technology->softblocked &&
1877                                 technology->enable_persistent)
1878                 return __connman_rfkill_block(type, false);
1879         else if (!technology->softblocked &&
1880                                 !technology->enable_persistent)
1881                 return __connman_rfkill_block(type, true);
1882
1883         return 0;
1884 }
1885
1886 int __connman_technology_update_rfkill(unsigned int index,
1887                                         enum connman_service_type type,
1888                                                 bool softblock,
1889                                                 bool hardblock)
1890 {
1891         struct connman_technology *technology;
1892         struct connman_rfkill *rfkill;
1893
1894         DBG("index %u soft %u hard %u", index, softblock, hardblock);
1895
1896         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1897         if (!rfkill)
1898                 return -ENXIO;
1899
1900         if (rfkill->softblock == softblock &&
1901                                 rfkill->hardblock == hardblock)
1902                 return 0;
1903
1904         rfkill->softblock = softblock;
1905         rfkill->hardblock = hardblock;
1906
1907         technology = technology_find(type);
1908         /* If there is no driver for this type, ignore it. */
1909         if (!technology)
1910                 return -ENXIO;
1911
1912         technology_apply_rfkill_change(technology, softblock, hardblock,
1913                                                                 false);
1914
1915         if (technology->hardblocked)
1916                 DBG("%s hardblocked", get_name(technology->type));
1917         else
1918                 DBG("%s is%s softblocked", get_name(technology->type),
1919                         technology->softblocked ? "" : " not");
1920
1921         return 0;
1922 }
1923
1924 int __connman_technology_remove_rfkill(unsigned int index,
1925                                         enum connman_service_type type)
1926 {
1927         struct connman_technology *technology;
1928         struct connman_rfkill *rfkill;
1929
1930         DBG("index %u", index);
1931
1932         rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1933         if (!rfkill)
1934                 return -ENXIO;
1935
1936         g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
1937
1938         technology = technology_find(type);
1939         if (!technology)
1940                 return -ENXIO;
1941
1942         technology_apply_rfkill_change(technology,
1943                 technology->softblocked, !technology->hardblocked, false);
1944
1945         technology_put(technology);
1946
1947         return 0;
1948 }
1949
1950 int __connman_technology_init(void)
1951 {
1952         DBG("");
1953
1954         connection = connman_dbus_get_connection();
1955
1956         rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1957                                                         NULL, free_rfkill);
1958
1959         global_offlinemode = connman_technology_load_offlinemode();
1960
1961         /* This will create settings file if it is missing */
1962         connman_technology_save_offlinemode();
1963
1964         return 0;
1965 }
1966
1967 void __connman_technology_cleanup(void)
1968 {
1969         DBG("");
1970
1971         while (technology_list) {
1972                 struct connman_technology *technology = technology_list->data;
1973                 technology_list = g_slist_remove(technology_list, technology);
1974                 technology_put(technology);
1975         }
1976
1977         g_hash_table_destroy(rfkill_list);
1978
1979         dbus_connection_unref(connection);
1980 }