Debian Codegraph @

lib/technology.c

raw symbols SCIP
1 /*
2 *
3 * Connection Manager UI
4 *
5 * Copyright (C) 2012 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 #include <gdbus/gdbus.h>
23
24 #include <connman-private.h>
25 #include <cui-dbus.h>
26
27 #define CONNMAN_TECHNOLOGY_INTERFACE CONNMAN_DBUS_NAME ".Technology"
28
29 #define PROPERTY(n) Technology_updatable_properties[n]
30
31 static void get_technologies_cb(DBusMessageIter *iter);
32
33 enum connman_technology_property {
34 TECHNOLOGY_POWERED = 0,
35 TECHNOLOGY_CONNECTED = 1,
36 TECHNOLOGY_TETHERING = 2,
37 TECHNOLOGY_TETHERING_IDENTIFIER = 3,
38 TECHNOLOGY_TETHERING_PASSPHRASE = 4,
39 TECHNOLOGY_MAX = 5,
40 };
41
42 static const char *Technology_updatable_properties[] = {
43 "Powered",
44 "Connected",
45 "Tethering",
46 "TetheringIdentifier",
47 "TetheringPassphrase",
48 };
49
50 struct connman_technology {
51 char *path;
52 char *name;
53 char *type;
54
55 gboolean powered;
56 gboolean connected;
57
58 gboolean tethering;
59 char *tethering_identifier;
60 char *tethering_passphrase;
61
62 int update_index;
63
64 guint property_changed_wid;
65 guint to_update[TECHNOLOGY_MAX];
66 connman_property_changed_cb_f property_changed_cb;
67 void *property_changed_user_data;
68
69 connman_scan_cb_f scan_cb;
70 DBusPendingCall *scan_call;
71 void *scan_user_data;
72
73 DBusPendingCall *call_modify[TECHNOLOGY_MAX];
74 guint to_error[TECHNOLOGY_MAX];
75 connman_property_set_cb_f property_set_error_cb;
76 void *property_set_error_user_data;
77 };
78
79 struct connman_technology_interface {
80 DBusConnection *dbus_cnx;
81
82 GHashTable *techs;
83 gboolean set;
84
85 connman_property_changed_cb_f property_changed_cb;
86 void *property_user_data;
87
88 connman_path_changed_cb_f added_cb;
89 connman_path_changed_cb_f removed_cb;
90 };
91
92 static struct connman_technology_interface *tech_if = NULL;
93
94 static void technology_free(gpointer data)
95 {
96 struct connman_technology *technology = data;
97 int i;
98
99 if (technology->property_changed_wid != 0)
100 g_dbus_remove_watch(tech_if->dbus_cnx,
101 technology->property_changed_wid);
102
103 for (i = 0; i < TECHNOLOGY_MAX; i++) {
104 if (technology->to_update[i] != 0)
105 g_source_remove(technology->to_update[i]);
106
107 if (technology->call_modify[i] != 0) {
108 dbus_pending_call_cancel(technology->call_modify[i]);
109 dbus_pending_call_unref(technology->call_modify[i]);
110 }
111
112 if (technology->to_error[i] != 0)
113 g_source_remove(technology->to_error[i]);
114 }
115
116 if (technology->scan_call != NULL) {
117 dbus_pending_call_cancel(technology->scan_call);
118 dbus_pending_call_unref(technology->scan_call);
119 }
120
121 g_free(technology->path);
122 g_free(technology->name);
123 g_free(technology->type);
124 g_free(technology->tethering_identifier);
125 g_free(technology->tethering_passphrase);
126
127 g_free(technology);
128 }
129
130 static struct connman_technology *get_technology(const char *path)
131 {
132 if (path == NULL || tech_if == NULL)
133 return NULL;
134
135 return g_hash_table_lookup(tech_if->techs, path);
136 }
137
138 static void destroy_property(gpointer user_data)
139 {
140 struct property_change *property = user_data;
141 struct connman_technology *technology;
142
143 technology = get_technology(property->path);
144 if (technology != NULL) {
145 if (property->error != 0)
146 technology->to_error[property->index] = 0;
147 else
148 technology->to_update[property->index] = 0;
149 }
150
151 g_free(property);
152 }
153
154 static gboolean property_changed_or_error(gpointer user_data)
155 {
156 struct property_change *property = user_data;
157 struct connman_technology *technology;
158
159 technology = get_technology(property->path);
160 if (technology == NULL)
161 return FALSE;
162
163 if (property->error != 0) {
164 if (technology->property_set_error_cb == NULL)
165 return FALSE;
166
167 technology->property_set_error_cb(property->path,
168 property->name, property->error,
169 technology->property_set_error_user_data);
170 return FALSE;
171 }
172
173 if (tech_if->property_changed_cb != NULL) {
174 tech_if->property_changed_cb(property->path, property->name,
175 tech_if->property_user_data);
176 }
177
178 if (technology->property_changed_cb != NULL) {
179 technology->property_changed_cb(property->path, property->name,
180 technology->property_changed_user_data);
181 }
182
183 return FALSE;
184 }
185
186 static void property_update(struct connman_technology *technology, int index)
187 {
188 struct property_change *property;
189
190 if (technology->property_changed_cb == NULL)
191 return;
192
193 property = g_try_malloc0(sizeof(struct property_change));
194 if (property == NULL)
195 return;
196
197 property->index = index;
198 property->name = PROPERTY(index);
199 property->path = technology->path;
200
201 if (technology->to_update[index] != 0)
202 g_source_remove(technology->to_update[index]);
203
204 technology->to_update[index] = g_timeout_add_full(G_PRIORITY_DEFAULT,
205 0, property_changed_or_error,
206 property, destroy_property);
207 }
208
209 static bool update_technology_property(DBusMessageIter *arg, void *user_data)
210 {
211 struct connman_technology *technology = user_data;
212 const char *name, *value;
213
214 if (cui_dbus_get_basic(arg, DBUS_TYPE_STRING, &name) != 0)
215 return FALSE;
216
217 dbus_message_iter_next(arg);
218
219 if (g_strcmp0(name, "Name") == 0) {
220 cui_dbus_get_basic_variant(arg, DBUS_TYPE_STRING, &value);
221 technology->name = g_strdup(value);
222 } else if (g_strcmp0(name, "Type") == 0) {
223 cui_dbus_get_basic_variant(arg, DBUS_TYPE_STRING, &value);
224 technology->type = g_strdup(value);
225 } else if (g_strcmp0(name, PROPERTY(TECHNOLOGY_POWERED)) == 0) {
226 cui_dbus_get_basic_variant(arg,
227 DBUS_TYPE_BOOLEAN, &technology->powered);
228 technology->update_index = TECHNOLOGY_POWERED;
229 } else if (g_strcmp0(name, PROPERTY(TECHNOLOGY_CONNECTED)) == 0) {
230 cui_dbus_get_basic_variant(arg,
231 DBUS_TYPE_BOOLEAN, &technology->connected);
232 technology->update_index = TECHNOLOGY_CONNECTED;
233 } else if (g_strcmp0(name, PROPERTY(TECHNOLOGY_TETHERING)) == 0) {
234 cui_dbus_get_basic_variant(arg,
235 DBUS_TYPE_BOOLEAN, &technology->tethering);
236 technology->update_index = TECHNOLOGY_TETHERING;;
237 } else if (g_strcmp0(name,
238 PROPERTY(TECHNOLOGY_TETHERING_IDENTIFIER)) == 0) {
239 cui_dbus_get_basic_variant(arg, DBUS_TYPE_STRING, &value);
240
241 g_free(technology->tethering_identifier);
242 technology->tethering_identifier = g_strdup(value);
243
244 technology->update_index = TECHNOLOGY_TETHERING_IDENTIFIER;
245 } else if (g_strcmp0(name,
246 PROPERTY(TECHNOLOGY_TETHERING_PASSPHRASE)) == 0) {
247 cui_dbus_get_basic_variant(arg, DBUS_TYPE_STRING, &value);
248
249 g_free(technology->tethering_passphrase);
250 technology->tethering_passphrase = g_strdup(value);
251
252 technology->update_index = TECHNOLOGY_TETHERING_PASSPHRASE;
253 }
254
255 if (technology->update_index < TECHNOLOGY_MAX)
256 property_update(technology, technology->update_index);
257
258 return FALSE;
259 }
260
261 static gboolean property_changed_signal_cb(DBusConnection *dbus_cnx,
262 DBusMessage *message, void *user_data)
263 {
264 struct connman_technology *technology = user_data;
265 DBusMessageIter arg;
266
267 if (message == NULL)
268 return TRUE;
269
270 if (dbus_message_iter_init(message, &arg) == FALSE)
271 return TRUE;
272
273 technology->update_index = TECHNOLOGY_MAX;
274
275 update_technology_property(&arg, technology);
276
277 return TRUE;
278 }
279
280 static void update_or_create_technology(const char *obj_path,
281 DBusMessageIter *dict)
282 {
283 struct connman_technology *technology;
284
285 if (tech_if == NULL)
286 return;
287
288 technology = g_hash_table_lookup(tech_if->techs, obj_path);
289 if (technology == NULL) {
290 technology = g_try_malloc0(sizeof(struct connman_technology));
291 if (technology == NULL)
292 return;
293
294 technology->path = g_strdup(obj_path);
295
296 g_hash_table_insert(tech_if->techs,
297 technology->path, technology);
298
299 technology->property_changed_wid = g_dbus_add_signal_watch(
300 tech_if->dbus_cnx,
301 CONNMAN_DBUS_NAME,
302 technology->path,
303 CONNMAN_TECHNOLOGY_INTERFACE,
304 "PropertyChanged",
305 property_changed_signal_cb,
306 technology, NULL);
307 }
308
309 cui_dbus_foreach_dict_entry(dict,
310 update_technology_property, technology);
311 }
312
313 static void technology_added_cb(DBusMessageIter *iter)
314 {
315 char *obj_path;
316
317 if (iter == NULL)
318 return;
319
320 cui_dbus_get_basic(iter, DBUS_TYPE_OBJECT_PATH, &obj_path);
321
322 dbus_message_iter_next(iter);
323
324 update_or_create_technology(obj_path, iter);
325
326 if (tech_if != NULL && tech_if->added_cb != NULL)
327 tech_if->added_cb(obj_path);
328
329 if (tech_if->set == TRUE)
330 __connman_manager_get_technologies(get_technologies_cb);
331 }
332
333 static void technology_removed_cb(DBusMessageIter *iter)
334 {
335 char *obj_path;
336
337 if (iter == NULL)
338 return;
339
340 cui_dbus_get_basic(iter, DBUS_TYPE_OBJECT_PATH, &obj_path);
341
342 if (tech_if != NULL && tech_if->removed_cb != NULL)
343 tech_if->removed_cb(obj_path);
344
345 g_hash_table_remove(tech_if->techs, obj_path);
346 }
347
348 static void get_technologies_cb(DBusMessageIter *iter)
349 {
350 DBusMessageIter array;
351 DBusMessageIter strt;
352 char *obj_path;
353 int arg_type;
354
355 if (iter == NULL || tech_if == NULL)
356 return;
357
358 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
359 return;
360
361 dbus_message_iter_recurse(iter, &array);
362
363 arg_type = dbus_message_iter_get_arg_type(&array);
364 while (arg_type != DBUS_TYPE_INVALID) {
365 dbus_message_iter_recurse(&array, &strt);
366
367 cui_dbus_get_basic(&strt, DBUS_TYPE_OBJECT_PATH, &obj_path);
368
369 dbus_message_iter_next(&strt);
370 update_or_create_technology(obj_path, &strt);
371
372 dbus_message_iter_next(&array);
373 arg_type = dbus_message_iter_get_arg_type(&array);
374 }
375
376 if (tech_if->set == FALSE) {
377 if (__connman_manager_register_technology_signals(
378 technology_added_cb,
379 technology_removed_cb) == 0)
380 tech_if->set = TRUE;
381
382 __connman_manager_get_technologies(get_technologies_cb);
383 }
384 }
385
386 static void scan_callback(DBusPendingCall *pending, void *user_data)
387 {
388 struct connman_technology *technology = user_data;
389
390 if (dbus_pending_call_get_completed(pending) == FALSE)
391 return;
392
393 dbus_pending_call_unref(pending);
394 technology->scan_call = NULL;
395
396 if (technology->scan_cb != NULL)
397 technology->scan_cb(technology->scan_user_data);
398 }
399
400 static void set_property_cb(DBusPendingCall *pending, void *user_data)
401 {
402 struct property_setting *set = user_data;
403 struct connman_technology *technology;
404 struct property_change *property;
405 DBusMessage *reply;
406 DBusError error;
407
408 if (dbus_pending_call_get_completed(pending) == FALSE)
409 return;
410
411 if (set == NULL)
412 return;
413
414 technology = set->data;
415
416 technology->call_modify[set->index] = NULL;
417
418 if (technology->property_set_error_cb == NULL)
419 return;
420
421 property = g_try_malloc0(sizeof(struct property_change));
422 if (property == NULL)
423 return;
424
425 reply = dbus_pending_call_steal_reply(pending);
426 if (reply == NULL)
427 return;
428
429 dbus_error_init(&error);
430 if (dbus_set_error_from_message(&error, reply) == FALSE)
431 goto done;
432
433 printf("SetProperty Error: %s\n", error.message);
434 dbus_error_free(&error);
435
436 if (technology->property_set_error_cb == NULL)
437 goto done;
438
439 property = g_try_malloc0(sizeof(struct property_change));
440 if (property == NULL)
441 goto done;
442
443 property->path = technology->path;
444 property->name = PROPERTY(set->index);
445 property->error = -1;
446
447 if (technology->to_error[set->index] != 0)
448 g_source_remove(technology->to_error[set->index]);
449
450 technology->to_error[set->index] = g_timeout_add_full(
451 G_PRIORITY_DEFAULT,
452 0, property_changed_or_error,
453 property, destroy_property);
454
455 done:
456 dbus_message_unref(reply);
457 }
458
459 static int set_technology_property(struct connman_technology *technology,
460 enum connman_technology_property property,
461 int dbus_type, void *data)
462 {
463 struct property_setting *set = NULL;
464 const char *property_name;
465 DBusMessage *message;
466 DBusMessageIter arg;
467
468 if (connman == NULL)
469 return -EINVAL;
470
471 if (technology->call_modify[property] != NULL)
472 return -EINVAL;
473
474 message = dbus_message_new_method_call(CONNMAN_DBUS_NAME,
475 technology->path,
476 CONNMAN_TECHNOLOGY_INTERFACE,
477 "SetProperty");
478 if (message == NULL)
479 return -ENOMEM;
480
481 set = g_try_malloc0(sizeof(struct property_setting));
482 if (set == NULL)
483 goto error;
484
485 set->data = technology;
486 set->index = property;
487
488 property_name = PROPERTY(property);
489
490 dbus_message_iter_init_append(message, &arg);
491
492 cui_dbus_append_basic(&arg, property_name, dbus_type, data);
493
494 if (dbus_connection_send_with_reply(tech_if->dbus_cnx, message,
495 &technology->call_modify[property],
496 DBUS_TIMEOUT_USE_DEFAULT) == FALSE)
497 goto error;
498
499 if (dbus_pending_call_set_notify(technology->call_modify[property],
500 set_property_cb, set, g_free) == FALSE)
501 goto error;
502
503 return 0;
504
505 error:
506 dbus_message_unref(message);
507
508 if (set == NULL)
509 return -ENOMEM;
510
511 g_free(set);
512
513 return -EINVAL;
514 }
515
516 int connman_technology_init(void)
517 {
518 int ret;
519
520 if (connman == NULL)
521 return -EINVAL;
522
523 if (tech_if != NULL)
524 return 0;
525
526 tech_if = g_try_malloc0(sizeof(struct connman_technology_interface));
527 if (tech_if == NULL)
528 return -ENOMEM;
529
530 tech_if->techs = g_hash_table_new_full(g_str_hash, g_str_equal,
531 NULL, technology_free);
532 if (tech_if->techs == NULL) {
533 g_free(tech_if);
534 return -ENOMEM;
535 }
536
537 ret = __connman_manager_get_technologies(get_technologies_cb);
538 if (ret < 0)
539 return ret;
540
541 tech_if->dbus_cnx = dbus_connection_ref(connman->dbus_cnx);
542
543 return 0;
544 }
545
546 void connman_technology_finalize(void)
547 {
548 if (tech_if == NULL)
549 return;
550
551 __connman_manager_register_technology_signals(NULL, NULL);
552
553 dbus_connection_unref(tech_if->dbus_cnx);
554
555 g_hash_table_destroy(tech_if->techs);
556
557 g_free(tech_if);
558
559 tech_if = NULL;
560 }
561
562 void connman_technology_set_global_property_callback(
563 connman_property_changed_cb_f property_changed_cb,
564 void *user_data)
565 {
566 if (tech_if == NULL)
567 return;
568
569 tech_if->property_changed_cb = property_changed_cb;
570 tech_if->property_user_data = user_data;
571 }
572
573 void connman_technology_set_property_changed_callback(const char *path,
574 connman_property_changed_cb_f property_changed_cb,
575 void *user_data)
576 {
577 struct connman_technology *technology;
578
579 technology = get_technology(path);
580 if (technology == NULL)
581 return;
582
583 technology->property_changed_cb = property_changed_cb;
584 technology->property_changed_user_data = user_data;
585 }
586
587 void connman_technology_set_property_error_callback(const char *path,
588 connman_property_set_cb_f property_set_cb,
589 void *user_data)
590 {
591 struct connman_technology *technology;
592
593 technology = get_technology(path);
594 if (technology == NULL)
595 return;
596
597 technology->property_set_error_cb = property_set_cb;
598 technology->property_set_error_user_data = user_data;
599 }
600
601 void connman_technology_set_removed_callback(
602 connman_path_changed_cb_f removed_cb)
603 {
604 if (tech_if != NULL)
605 tech_if->removed_cb = removed_cb;
606 }
607
608 void connman_technology_set_added_callback(connman_path_changed_cb_f added_cb)
609 {
610 if (tech_if != NULL)
611 tech_if->added_cb = added_cb;
612 }
613
614 GList *connman_technology_get_technologies(void)
615 {
616 if (tech_if == NULL || tech_if->techs == NULL)
617 return NULL;
618
619 return g_hash_table_get_keys(tech_if->techs);
620 }
621
622 int connman_technology_scan(const char *path,
623 connman_scan_cb_f callback, void *user_data)
624 {
625 struct connman_technology *technology;
626 DBusMessage *message;
627
628 technology = get_technology(path);
629 if (technology == NULL)
630 return -EINVAL;
631
632 message = dbus_message_new_method_call(CONNMAN_DBUS_NAME,
633 technology->path,
634 CONNMAN_TECHNOLOGY_INTERFACE,
635 "Scan");
636 if (message == NULL)
637 return -ENOMEM;
638
639 technology->scan_cb = callback;
640
641 if (dbus_connection_send_with_reply(tech_if->dbus_cnx, message,
642 &technology->scan_call,
643 DBUS_TIMEOUT_USE_DEFAULT) == FALSE)
644 goto error;
645
646 if (dbus_pending_call_set_notify(technology->scan_call,
647 scan_callback, technology, NULL) == FALSE)
648 goto error;
649
650 technology->scan_user_data = user_data;
651
652 dbus_message_unref(message);
653
654 return 0;
655
656 error:
657 dbus_message_unref(message);
658
659 technology->scan_cb = NULL;
660
661 return -EINVAL;
662 }
663
664 int connman_technology_enable(const char *path, gboolean enable)
665 {
666 struct connman_technology *technology;
667
668 technology = get_technology(path);
669 if (technology == NULL)
670 return -EINVAL;
671
672 if (technology->powered == enable)
673 return 0;
674
675 return set_technology_property(technology,
676 TECHNOLOGY_POWERED,
677 DBUS_TYPE_BOOLEAN, &enable);
678 }
679
680 int connman_technology_tether(const char *path, gboolean tethering)
681 {
682 struct connman_technology *technology;
683
684 technology = get_technology(path);
685 if (technology == NULL)
686 return -EINVAL;
687
688 if (technology->tethering == tethering)
689 return 0;
690
691 return set_technology_property(technology,
692 TECHNOLOGY_TETHERING,
693 DBUS_TYPE_BOOLEAN, &tethering);
694 }
695
696 int connman_technology_set_tethering_identifier(const char *path,
697 const char *identifier)
698 {
699 struct connman_technology *technology;
700
701 technology = get_technology(path);
702 if (technology == NULL)
703 return -EINVAL;
704
705 return set_technology_property(technology,
706 TECHNOLOGY_TETHERING_IDENTIFIER,
707 DBUS_TYPE_STRING, &identifier);
708 }
709
710 int connman_technology_set_tethering_passphrase(const char *path,
711 const char *passphrase)
712 {
713 struct connman_technology *technology;
714
715 technology = get_technology(path);
716 if (technology == NULL)
717 return -EINVAL;
718
719 return set_technology_property(technology,
720 TECHNOLOGY_TETHERING_PASSPHRASE,
721 DBUS_TYPE_STRING, &passphrase);
722 }
723
724 const char *connman_technology_get_name(const char *path)
725 {
726 struct connman_technology *technology;
727
728 technology = get_technology(path);
729 if (technology == NULL)
730 return NULL;
731
732 return technology->name;
733 }
734
735 const char *connman_technology_get_type(const char *path)
736 {
737 struct connman_technology *technology;
738
739 technology = get_technology(path);
740 if (technology == NULL)
741 return NULL;
742
743 return technology->type;
744 }
745
746 gboolean connman_technology_is_enabled(const char *path)
747 {
748 struct connman_technology *technology;
749
750 technology = get_technology(path);
751 if (technology == NULL)
752 return FALSE;
753
754 return technology->powered;
755 }
756
757 gboolean connman_technology_is_connected(const char *path)
758 {
759 struct connman_technology *technology;
760
761 technology = get_technology(path);
762 if (technology == NULL)
763 return FALSE;
764
765 return technology->connected;
766 }
767
768 gboolean connman_technology_is_tethering(const char *path)
769 {
770 struct connman_technology *technology;
771
772 technology = get_technology(path);
773 if (technology == NULL)
774 return FALSE;
775
776 return technology->tethering;
777 }
778
779 const char *connman_technology_get_tethering_identifier(const char *path)
780 {
781 struct connman_technology *technology;
782
783 technology = get_technology(path);
784 if (technology == NULL)
785 return NULL;
786
787 return technology->tethering_identifier;
788 }
789
790 const char *connman_technology_get_tethering_passphrase(const char *path)
791 {
792 struct connman_technology *technology;
793
794 technology = get_technology(path);
795 if (technology == NULL)
796 return NULL;
797
798 return technology->tethering_passphrase;
799 }
800