3211aec81d32a08dc526108cda2db5e7ffa7f58a
[platform/upstream/connman.git] / client / technology.c
1 /*
2  *
3  *  Connection Manager
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 as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #include "client/technology.h"
34 #include "src/connman.h"
35 #include "dbus.h"
36
37 void extract_properties(DBusMessageIter *dict)
38 {
39         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
40                 DBusMessageIter entry, value;
41                 const char *key, *sdata;
42                 dbus_bool_t bdata;
43
44                 dbus_message_iter_recurse(dict, &entry);
45                 dbus_message_iter_get_basic(&entry, &key);
46                 printf("  [%s] = ", key);
47
48                 dbus_message_iter_next(&entry);
49
50                 dbus_message_iter_recurse(&entry, &value);
51
52                 if (dbus_message_iter_get_arg_type(&value) ==
53                                                         DBUS_TYPE_BOOLEAN) {
54                         dbus_message_iter_get_basic(&value, &bdata);
55                         printf("%s\n", bdata ? "True" : "False");
56                 } else if (dbus_message_iter_get_arg_type(&value) ==
57                                                         DBUS_TYPE_STRING) {
58                         dbus_message_iter_get_basic(&value, &sdata);
59                         printf("%s\n", sdata);
60                 }
61                 dbus_message_iter_next(dict);
62         }
63 }
64
65 void match_tech_name(DBusMessage *message, char *tech_name,
66                                                 struct tech_data *tech)
67 {
68         DBusMessageIter iter, array;
69
70         dbus_message_iter_init(message, &iter);
71         dbus_message_iter_recurse(&iter, &array);
72         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
73                 DBusMessageIter entry;
74                 const char *path;
75                 const char *name;
76
77                 dbus_message_iter_recurse(&array, &entry);
78                 dbus_message_iter_get_basic(&entry, &path);
79                 tech->path = g_strdup(path);
80                 name = strrchr(path, '/') + 1;
81                 tech->name = g_strdup(name);
82                 if (g_strcmp0(tech_name, tech->name) == 0) {
83                         printf("    %-20s { %s } exists\n", tech->name,
84                                                                 tech->path);
85                         break;
86                 } else
87                         dbus_message_iter_next(&array);
88         }
89
90 }
91
92 void extract_tech(DBusMessage *message)
93 {
94         DBusMessageIter iter, array;
95
96         dbus_message_iter_init(message, &iter);
97         dbus_message_iter_recurse(&iter, &array);
98         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
99                 DBusMessageIter entry, dict;
100
101                 const char *path;
102
103                 dbus_message_iter_recurse(&array, &entry);
104                 dbus_message_iter_get_basic(&entry, &path);
105
106                 printf("{ %s }\n", path);
107
108                 dbus_message_iter_next(&entry);
109
110                 dbus_message_iter_recurse(&entry, &dict);
111                 extract_properties(&dict);
112
113                 dbus_message_iter_next(&array);
114         }
115 }
116
117 int scan_technology(DBusConnection *connection, DBusMessage *message,
118                                                                 char *tech)
119 {
120         DBusMessage *message_send;
121         struct tech_data technology;
122         DBusError err;
123
124         match_tech_name(message, tech, &technology);
125         if (g_strcmp0(tech, technology.name) != 0) {
126                 fprintf(stderr, "%s does not exist on the system\n", tech);
127                 fprintf(stderr, "Use the 'tech' command to find available "
128                                         "technologies on your system.\n");
129                 return -ENXIO;
130         }
131
132         message_send = dbus_message_new_method_call("net.connman",
133                                                 technology.path,
134                                                 "net.connman.Technology",
135                                                 "Scan");
136         if (message_send == NULL)
137                 return -ENOMEM;
138
139         dbus_error_init(&err);
140         dbus_connection_send_with_reply_and_block(connection, message_send, -1,
141                                                                         &err);
142
143         if (dbus_error_is_set(&err)) {
144                 printf("Scan failed; error: '%s'\n", err.message);
145                 return -EINVAL;
146         }
147
148         dbus_message_unref(message_send);
149         printf("Scanned for new services on %s.\n", technology.name);
150         g_free(technology.name);
151         g_free(technology.path);
152
153         return 0;
154 }
155
156 int set_technology(DBusConnection *connection, DBusMessage *message, char *key,
157                                                 char *tech, dbus_bool_t value)
158 {
159         DBusMessage *message_send;
160         DBusMessageIter iter;
161         struct tech_data technology;
162
163         match_tech_name(message, tech, &technology);
164         if (g_strcmp0(tech, technology.name) != 0) {
165                 fprintf(stderr, "%s does not exist on the system\n", tech);
166                 fprintf(stderr, "Use the 'tech' command to find available "
167                                         "technologies on your system.\n");
168                 return -ENXIO;
169         }
170
171         message_send = dbus_message_new_method_call("net.connman",
172                                                         technology.path,
173                                                         "net.connman.Technology",
174                                                         "SetProperty");
175         if (message_send == NULL)
176                 return -ENOMEM;
177
178         dbus_message_iter_init_append(message_send, &iter);
179         dbus_property_append_basic(&iter, (const char *) key,
180                                                 DBUS_TYPE_BOOLEAN, &value);
181         dbus_connection_send(connection, message_send, NULL);
182         dbus_connection_flush(connection);
183         dbus_message_unref(message_send);
184         g_free(technology.name);
185         g_free(technology.path);
186
187         return 0;
188 }