client: Add support for registering and unregistering an agent
[platform/upstream/connman.git] / client / agent.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2013  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 <sys/types.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <stdbool.h>
32
33 #include <gdbus.h>
34
35 #include "dbus_helpers.h"
36 #include "agent.h"
37
38 static bool agent_registered = false;
39
40 #define AGENT_INTERFACE      "net.connman.Agent"
41
42 static char *agent_path(void)
43 {
44         static char *path = NULL;
45
46         if (path == NULL)
47                 path = g_strdup_printf("/net/connman/connmanctl%d", getpid());
48
49         return path;
50 }
51
52 static const GDBusMethodTable agent_methods[] = {
53         { },
54 };
55
56 static int agent_register_return(DBusMessageIter *iter, const char *error,
57                 void *user_data)
58 {
59         DBusConnection *connection = user_data;
60
61         if (error != NULL) {
62                 g_dbus_unregister_interface(connection, agent_path(),
63                                 AGENT_INTERFACE);
64                 fprintf(stderr, "Error registering Agent: %s\n", error);
65                 return 0;
66         }
67
68         agent_registered = true;
69         fprintf(stdout, "Agent registered\n");
70
71         return -EINPROGRESS;
72 }
73
74 int __connmanctl_agent_register(DBusConnection *connection)
75 {
76         char *path = agent_path();
77         int result;
78
79         if (agent_registered == true) {
80                 fprintf(stderr, "Agent already registered\n");
81                 return -EALREADY;
82         }
83
84         if (g_dbus_register_interface(connection, path,
85                                         AGENT_INTERFACE, agent_methods,
86                                         NULL, NULL, NULL, NULL) == FALSE) {
87                 fprintf(stderr, "Error: Failed to register Agent callbacks\n");
88                 return 0;
89         }
90
91         result = __connmanctl_dbus_method_call(connection, "/",
92                         "net.connman.Manager", "RegisterAgent",
93                         agent_register_return, connection,
94                         DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
95
96         if (result != -EINPROGRESS) {
97                 g_dbus_unregister_interface(connection, agent_path(),
98                                 AGENT_INTERFACE);
99
100                 fprintf(stderr, "Error: Failed to register Agent\n");
101         }
102
103         return result;
104 }
105
106 static int agent_unregister_return(DBusMessageIter *iter, const char *error,
107                 void *user_data)
108 {
109         if (error != NULL) {
110                 fprintf(stderr, "Error unregistering Agent: %s\n", error);
111                 return 0;
112         }
113
114         agent_registered = false;
115         fprintf(stdout, "Agent unregistered\n");
116
117         return 0;
118 }
119
120 int __connmanctl_agent_unregister(DBusConnection *connection)
121 {
122         char *path = agent_path();
123         int result;
124
125         if (agent_registered == false) {
126                 fprintf(stderr, "Agent not registered\n");
127                 return -EALREADY;
128         }
129
130         g_dbus_unregister_interface(connection, agent_path(), AGENT_INTERFACE);
131
132         result = __connmanctl_dbus_method_call(connection, "/",
133                         "net.connman.Manager", "UnregisterAgent",
134                         agent_unregister_return, NULL,
135                         DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
136
137         if (result != -EINPROGRESS)
138                 fprintf(stderr, "Error: Failed to unregister Agent\n");
139
140         return result;
141 }