client: Use helper functions for setting domains, nameservers and timeservers
[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 <glib.h>
34
35 #include "technology.h"
36 #include "dbus.h"
37
38 void extract_properties(DBusMessageIter *dict)
39 {
40         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
41                 DBusMessageIter entry, value;
42                 const char *key, *sdata;
43                 dbus_bool_t bdata;
44
45                 dbus_message_iter_recurse(dict, &entry);
46                 dbus_message_iter_get_basic(&entry, &key);
47                 printf("  [%s] = ", key);
48
49                 dbus_message_iter_next(&entry);
50
51                 dbus_message_iter_recurse(&entry, &value);
52
53                 if (dbus_message_iter_get_arg_type(&value) ==
54                                                         DBUS_TYPE_BOOLEAN) {
55                         dbus_message_iter_get_basic(&value, &bdata);
56                         printf("%s\n", bdata ? "True" : "False");
57                 } else if (dbus_message_iter_get_arg_type(&value) ==
58                                                         DBUS_TYPE_STRING) {
59                         dbus_message_iter_get_basic(&value, &sdata);
60                         printf("%s\n", sdata);
61                 }
62                 dbus_message_iter_next(dict);
63         }
64 }
65
66 void match_tech_name(DBusMessage *message, char *tech_name,
67                                                 struct tech_data *tech)
68 {
69         DBusMessageIter iter, array;
70
71         dbus_message_iter_init(message, &iter);
72         dbus_message_iter_recurse(&iter, &array);
73         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
74                 DBusMessageIter entry;
75                 const char *path;
76                 const char *name;
77
78                 dbus_message_iter_recurse(&array, &entry);
79                 dbus_message_iter_get_basic(&entry, &path);
80                 tech->path = g_strdup(path);
81                 name = strrchr(path, '/') + 1;
82                 tech->name = g_strdup(name);
83                 if (g_strcmp0(tech_name, tech->name) == 0) {
84                         break;
85                 } else
86                         dbus_message_iter_next(&array);
87         }
88
89 }
90
91 void extract_tech(DBusMessage *message)
92 {
93         DBusMessageIter iter, array;
94
95         dbus_message_iter_init(message, &iter);
96         dbus_message_iter_recurse(&iter, &array);
97         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
98                 DBusMessageIter entry, dict;
99
100                 const char *path;
101
102                 dbus_message_iter_recurse(&array, &entry);
103                 dbus_message_iter_get_basic(&entry, &path);
104
105                 printf("{ %s }\n", path);
106
107                 dbus_message_iter_next(&entry);
108
109                 dbus_message_iter_recurse(&entry, &dict);
110                 extract_properties(&dict);
111
112                 dbus_message_iter_next(&array);
113         }
114 }
115
116 int scan_technology(DBusConnection *connection, DBusMessage *message,
117                                                                 char *tech)
118 {
119         DBusMessage *message_send;
120         struct tech_data technology;
121         DBusError err;
122
123         match_tech_name(message, tech, &technology);
124         if (g_strcmp0(tech, technology.name) != 0) {
125                 return -ENXIO;
126         }
127
128         message_send = dbus_message_new_method_call("net.connman",
129                                                 technology.path,
130                                                 "net.connman.Technology",
131                                                 "Scan");
132         if (message_send == NULL)
133                 return -ENOMEM;
134
135         dbus_error_init(&err);
136         dbus_connection_send_with_reply_and_block(connection, message_send, -1,
137                                                                         &err);
138
139         if (dbus_error_is_set(&err)) {
140                 printf("Error '%s' %s\n", technology.path, err.message);
141                 dbus_error_free(&err);
142                 return -ENXIO;
143         }
144
145         dbus_message_unref(message_send);
146         g_free(technology.name);
147         g_free(technology.path);
148
149         return 0;
150 }
151
152 int set_technology(DBusConnection *connection, DBusMessage *message, char *key,
153                                                 char *tech, dbus_bool_t value)
154 {
155         DBusMessage *message_send;
156         DBusMessageIter iter;
157         struct tech_data technology;
158         DBusError err;
159
160         match_tech_name(message, tech, &technology);
161         if (g_strcmp0(tech, technology.name) != 0) {
162                 return -ENXIO;
163         }
164
165         message_send = dbus_message_new_method_call("net.connman",
166                                                         technology.path,
167                                                         "net.connman.Technology",
168                                                         "SetProperty");
169         if (message_send == NULL)
170                 return -ENOMEM;
171
172         dbus_message_iter_init_append(message_send, &iter);
173         dbus_property_append_basic(&iter, (const char *) key,
174                                                 DBUS_TYPE_BOOLEAN, &value);
175
176         dbus_error_init(&err);
177         dbus_connection_send_with_reply_and_block(connection, message_send,
178                         -1, &err);
179         if (dbus_error_is_set(&err) == TRUE) {
180                 printf("Error '%s' %s\n", technology.path, err.message);
181                 dbus_error_free(&err);
182         }
183
184         g_free(technology.name);
185         g_free(technology.path);
186
187         return 0;
188 }