client: Technology API command line client support
[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 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 <stdio.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "client/technology.h"
29 #include "src/connman.h"
30
31 void extract_properties(DBusMessageIter *dict)
32 {
33         while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
34                 DBusMessageIter entry, value;
35                 const char *key, *sdata;
36                 dbus_bool_t bdata;
37
38                 dbus_message_iter_recurse(dict, &entry);
39                 dbus_message_iter_get_basic(&entry, &key);
40                 printf("  [%s] = ", key);
41
42                 dbus_message_iter_next(&entry);
43
44                 dbus_message_iter_recurse(&entry, &value);
45
46                 if (dbus_message_iter_get_arg_type(&value) ==
47                                                         DBUS_TYPE_BOOLEAN) {
48                         dbus_message_iter_get_basic(&value, &bdata);
49                         printf("%s\n", bdata ? "True" : "False");
50                 } else if (dbus_message_iter_get_arg_type(&value) ==
51                                                         DBUS_TYPE_STRING) {
52                         dbus_message_iter_get_basic(&value, &sdata);
53                         printf("%s\n", sdata);
54                 }
55                 dbus_message_iter_next(dict);
56         }
57 }
58
59 void match_tech_name(DBusMessage *message, char *tech_name,
60                                                 struct tech_data *tech)
61 {
62         DBusMessageIter iter, array;
63
64         dbus_message_iter_init(message, &iter);
65         dbus_message_iter_recurse(&iter, &array);
66         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
67                 DBusMessageIter entry;
68                 const char *path;
69                 const char *name;
70
71                 dbus_message_iter_recurse(&array, &entry);
72                 dbus_message_iter_get_basic(&entry, &path);
73                 tech->path = g_strdup(path);
74                 name = strrchr(path, '/') + 1;
75                 tech->name = g_strdup(name);
76                 if (g_strcmp0(tech_name, tech->name) == 0) {
77                         printf("    %-20s { %s } exists\n", tech->name,
78                                                                 tech->path);
79                         break;
80                 } else
81                         dbus_message_iter_next(&array);
82         }
83
84 }
85
86 void extract_tech(DBusMessage *message)
87 {
88         DBusMessageIter iter, array;
89
90         dbus_message_iter_init(message, &iter);
91         dbus_message_iter_recurse(&iter, &array);
92         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
93                 DBusMessageIter entry, dict;
94
95                 const char *path;
96
97                 dbus_message_iter_recurse(&array, &entry);
98                 dbus_message_iter_get_basic(&entry, &path);
99
100                 printf("{ %s }\n", path);
101
102                 dbus_message_iter_next(&entry);
103
104                 dbus_message_iter_recurse(&entry, &dict);
105                 extract_properties(&dict);
106
107                 dbus_message_iter_next(&array);
108         }
109 }
110
111 int scan_technology(DBusConnection *connection, DBusMessage *message,
112                                                                 char *tech)
113 {
114         DBusMessage *message_send;
115         struct tech_data technology;
116         DBusError err;
117
118         match_tech_name(message, tech, &technology);
119         if (g_strcmp0(tech, technology.name) != 0) {
120                 fprintf(stderr, "%s does not exist on the system\n", tech);
121                 fprintf(stderr, "Use the 'tech' command to find available "
122                                         "technologies on your system.\n");
123                 return -ENXIO;
124         }
125
126         message_send = dbus_message_new_method_call("net.connman",
127                                                 technology.path,
128                                                 "net.connman.Technology",
129                                                 "Scan");
130         if (message_send == NULL)
131                 return -ENOMEM;
132
133         dbus_error_init(&err);
134         dbus_connection_send_with_reply_and_block(connection, message_send, -1,
135                                                                         &err);
136
137         if (dbus_error_is_set(&err)) {
138                 printf("Scan failed; error: '%s'\n", err.message);
139                 return -EINVAL;
140         }
141
142         dbus_message_unref(message_send);
143         printf("Scanned for new services on %s.\n", technology.name);
144         g_free(technology.name);
145         g_free(technology.path);
146
147         return 0;
148 }
149
150 int set_technology(DBusConnection *connection, DBusMessage *message, char *key,
151                                                 char *tech, dbus_bool_t value)
152 {
153         DBusMessage *message_send;
154         DBusMessageIter iter;
155         struct tech_data technology;
156
157         match_tech_name(message, tech, &technology);
158         if (g_strcmp0(tech, technology.name) != 0) {
159                 fprintf(stderr, "%s does not exist on the system\n", tech);
160                 fprintf(stderr, "Use the 'tech' command to find available "
161                                         "technologies on your system.\n");
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         connman_dbus_property_append_basic(&iter, (const char *) key,
174                                                 DBUS_TYPE_BOOLEAN, &value);
175         dbus_connection_send(connection, message_send, NULL);
176         dbus_connection_flush(connection);
177         dbus_message_unref(message_send);
178         g_free(technology.name);
179         g_free(technology.path);
180
181         return 0;
182 }