client: Factor out state command
[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                         printf("    %-20s { %s } exists\n", tech->name,
85                                                                 tech->path);
86                         break;
87                 } else
88                         dbus_message_iter_next(&array);
89         }
90
91 }
92
93 void extract_tech(DBusMessage *message)
94 {
95         DBusMessageIter iter, array;
96
97         dbus_message_iter_init(message, &iter);
98         dbus_message_iter_recurse(&iter, &array);
99         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
100                 DBusMessageIter entry, dict;
101
102                 const char *path;
103
104                 dbus_message_iter_recurse(&array, &entry);
105                 dbus_message_iter_get_basic(&entry, &path);
106
107                 printf("{ %s }\n", path);
108
109                 dbus_message_iter_next(&entry);
110
111                 dbus_message_iter_recurse(&entry, &dict);
112                 extract_properties(&dict);
113
114                 dbus_message_iter_next(&array);
115         }
116 }
117
118 int scan_technology(DBusConnection *connection, DBusMessage *message,
119                                                                 char *tech)
120 {
121         DBusMessage *message_send;
122         struct tech_data technology;
123         DBusError err;
124
125         match_tech_name(message, tech, &technology);
126         if (g_strcmp0(tech, technology.name) != 0) {
127                 fprintf(stderr, "%s does not exist on the system\n", tech);
128                 fprintf(stderr, "Use the 'tech' command to find available "
129                                         "technologies on your system.\n");
130                 return -ENXIO;
131         }
132
133         message_send = dbus_message_new_method_call("net.connman",
134                                                 technology.path,
135                                                 "net.connman.Technology",
136                                                 "Scan");
137         if (message_send == NULL)
138                 return -ENOMEM;
139
140         dbus_error_init(&err);
141         dbus_connection_send_with_reply_and_block(connection, message_send, -1,
142                                                                         &err);
143
144         if (dbus_error_is_set(&err)) {
145                 printf("Scan failed; error: '%s'\n", err.message);
146                 return -EINVAL;
147         }
148
149         dbus_message_unref(message_send);
150         printf("Scanned for new services on %s.\n", technology.name);
151         g_free(technology.name);
152         g_free(technology.path);
153
154         return 0;
155 }
156
157 int set_technology(DBusConnection *connection, DBusMessage *message, char *key,
158                                                 char *tech, dbus_bool_t value)
159 {
160         DBusMessage *message_send;
161         DBusMessageIter iter;
162         struct tech_data technology;
163
164         match_tech_name(message, tech, &technology);
165         if (g_strcmp0(tech, technology.name) != 0) {
166                 fprintf(stderr, "%s does not exist on the system\n", tech);
167                 fprintf(stderr, "Use the 'tech' command to find available "
168                                         "technologies on your system.\n");
169                 return -ENXIO;
170         }
171
172         message_send = dbus_message_new_method_call("net.connman",
173                                                         technology.path,
174                                                         "net.connman.Technology",
175                                                         "SetProperty");
176         if (message_send == NULL)
177                 return -ENOMEM;
178
179         dbus_message_iter_init_append(message_send, &iter);
180         dbus_property_append_basic(&iter, (const char *) key,
181                                                 DBUS_TYPE_BOOLEAN, &value);
182         dbus_connection_send(connection, message_send, NULL);
183         dbus_connection_flush(connection);
184         dbus_message_unref(message_send);
185         g_free(technology.name);
186         g_free(technology.path);
187
188         return 0;
189 }