summaryrefslogtreecommitdiffstats
path: root/agent.c
blob: 66f23be6cc45050cf3f2cf6f92bc9233f562d8cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*  Copyright 2016 ALPS ELECTRIC CO., LTD.
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/

#include <stdio.h>
#include <errno.h>

#include <gio/gio.h>
#include "wifi-connman.h"

static GMainLoop *loop = NULL;

static GDBusNodeInfo *introspection_data = NULL;

GDBusConnection *connectionAgent;

GDBusMethodInvocation *invocation_passkey = NULL;

/* Introspection data for the agent service */
static const gchar introspection_xml[] = "<node>"
		"  <interface name='net.connman.Agent'>"
		"    <method name='RequestInput'>"
		"	   <arg type='o' name='service' direction='in'/>"
		"	   <arg type='a{sv}' name='fields' direction='in'/>"
		"	   <arg type='a{sv}' name='fields' direction='out'/>"
		"    </method>"
		"    <method name='ReportError'>"
		"	   <arg type='o' name='service' direction='in'/>"
		"	   <arg type='s' name='error' direction='in'/>"
		"    </method>"
		"  </interface>"
		"</node>";

callback password_callback;
callback wifiListChanged_callback;

static void handle_method_call(GDBusConnection *connection, const gchar *sender,
		const gchar *object_path, const gchar *interface_name,
		const gchar *method_name, GVariant *parameters,
		GDBusMethodInvocation *invocation, gpointer user_data) {
	//MyObject *myobj = user_data;

	if (g_strcmp0(method_name, "RequestInput") == 0) {
		printf("Input requested\n");

		invocation_passkey = invocation;

		GVariantIter *array;
		gchar * object_path;
		g_variant_get(parameters, "(oa{sv})", &object_path, &array);
		//TODO: get only object path for now, complete parameters are

		/*
		 object path "/net/connman/service/wifi_d85d4c880b1a_4c656e6f766f204b3520506c7573_managed_psk"
		 array [
		 dict entry(
		 string "Passphrase"
		 variant             array [
		 dict entry(
		 string "Type"
		 variant                      string "psk"
		 )
		 dict entry(
		 string "Requirement"
		 variant                      string "mandatory"
		 )
		 ]
		 )
		 ]
		 */
		printf("Passphrase requested for network : %s\n", object_path);
        (*password_callback)(0, object_path);


	}

	if (g_strcmp0(method_name, "ReportError") == 0) {
		printf("Error reported\n");

		gchar *error_string; // = NULL;

		gchar * object_path;
		g_variant_get(parameters, "(os)", &object_path, &error_string);

		printf("Error %s for %s\n", error_string, object_path);

		if (g_strcmp0(error_string, "invalid-key") == 0) {

			printf("Passkey is not correct.\n");
            (*password_callback)(1, "invalid-key");

		}

	}
}

GError* sendPasskey(gchar *passkey) {

	GVariantBuilder *builder;
	GVariant *value = NULL;

	printf("Passkey to send: %s\n", passkey);

	builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));

	g_variant_builder_add(builder, "{sv}", "Passphrase",
			g_variant_new_string(passkey));

	value = g_variant_new("(a{sv})", builder);

	g_dbus_method_invocation_return_value(invocation_passkey, value);

	return NULL;

}

static const GDBusInterfaceVTable interface_vtable = { handle_method_call, NULL,
		NULL };

static void on_bus_acquired(GDBusConnection *connection, const gchar *name,
		gpointer user_data) {
	//MyObject *myobj = user_data;
	guint registration_id;

	registration_id = g_dbus_connection_register_object(connection,
			"/net/connman/Agent", introspection_data->interfaces[0],
			&interface_vtable, NULL, NULL, /* user_data_free_func */
			NULL); /* GError** */
	//TODO: make some proper error message rather than exiting
	//g_assert(registration_id > 0);

	return NULL;
}

static void test_signal_handler (GDBusConnection *connection, const gchar *sender_name, const gchar *object_path, const gchar *interface_name, const gchar *signal_name, GVariant *parameters, gpointer user_data) {

    //openlog("WIFI Binder", LOG_PID | LOG_CONS, LOG_USER);


    //do not parse, just check what has changed and make callback -
    // we need to refresh completelist anyway..
    if (g_strcmp0(signal_name, "PropertiesChanged") == 0) {

        //syslog(LOG_INFO, "PropertiesChanged");
        (*wifiListChanged_callback)(1, "PropertiesChanged");
    }
    else if (g_strcmp0(signal_name, "BSSRemoved") == 0) {
        //syslog(LOG_INFO, "BSSRemoved" );
        (*wifiListChanged_callback)(2, "BSSRemoved");
        }

        else if (g_strcmp0(signal_name, "BSSAdded") == 0) {
            //syslog(LOG_INFO, "BSSAdded" );
            (*wifiListChanged_callback)(2, "BSSAdded");
            }
    else     printf ("unhandled signal %s %s %s, %s", sender_name, object_path, interface_name, signal_name);//syslog(LOG_INFO, "unhandled signal %s %s %s, %s", sender_name, object_path, interface_name, signal_name);



    //closelog();




}

void* register_agent(void *data) {

    printf("Loop start\n");

	guint owner_id;

    guint networkChangedID;

	introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
	g_assert(introspection_data != NULL);

	//myobj = g_object_new(my_object_get_type(), NULL);

//	owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, "org.agent",
//			G_BUS_NAME_OWNER_FLAGS_NONE, on_bus_acquired, on_name_acquired,
//			on_name_lost, myobj,
//			NULL);

//FIXME: ALLOW_REPLACEMENT for now, make proper deinitialization
	owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, AGENT_SERVICE,
			G_BUS_NAME_OWNER_FLAGS_REPLACE, on_bus_acquired, NULL, NULL, NULL,
			NULL);
	//G_BUS_NAME_OWNER_FLAGS_NONE G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT


    //"net.connman.Manager", "ServicesChanged",
    networkChangedID = g_dbus_connection_signal_subscribe(connectionAgent, NULL, "fi.w1.wpa_supplicant1.Interface", NULL, NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE, test_signal_handler, NULL, NULL);

    g_assert(networkChangedID !=0);

	loop = g_main_loop_new(NULL, FALSE);

	//sleep(10);
	g_main_loop_run(loop);

	printf("Loop running\n");

	g_bus_unown_name(owner_id);

	g_dbus_node_info_unref(introspection_data);

	//g_object_unref(myobj);

	//printf("Loop end\n");

	return NULL;

}

GError* create_agent(GDBusConnection *connection) {

	int err = -1;
	pthread_t tid[1];


    connectionAgent = connection;

	err = pthread_create((&tid[0]), NULL, register_agent, NULL);

	if (err != 0) {
		printf("\ncan't create thread :[%d]", err);
		printf("Fatal error!\n\n");
		return NULL;
	}

	GVariant *message = NULL;
	GError *error = NULL;

	GVariant *params = NULL;

	char *agent_path = AGENT_PATH;

	params = g_variant_new("(o)", agent_path);

	message = g_dbus_connection_call_sync(connection, CONNMAN_SERVICE,
	CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE, "RegisterAgent", params,
			NULL, G_DBUS_CALL_FLAGS_NONE,
			DBUS_REPLY_TIMEOUT, NULL, &error);

	if (error) {
		printf("error: %d:%s\n", error->code, error->message);
		
		return error;

	} else {
        printf("Agent registered\n");
		return NULL;
	}

}

GError* stop_agent(GDBusConnection *connection) {

	GVariant *message = NULL;
	GError *error = NULL;


	GVariant *params = NULL;

	char *agent_path = AGENT_PATH;


	params = g_variant_new("(o)", agent_path);

	message = g_dbus_connection_call_sync(connection, CONNMAN_SERVICE,
	CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE, "UnregisterAgent", params,
			NULL, G_DBUS_CALL_FLAGS_NONE,
			DBUS_REPLY_TIMEOUT, NULL, &error);

	if (error) {
		printf("error: %d:%s\n", error->code, error->message);
		return error;

	} else {
		printf("Agent unregistered\n");
		return NULL;
	}

}

void register_callbackSecurity(callback callback_function) {

	password_callback = callback_function;

}

void register_callbackWiFiList(callback callback_function) {

    wifiListChanged_callback = callback_function;

}