Mercurial > audlegacy-plugins
comparison src/bluetooth/agent.c @ 2841:9637eab5a3dd
indentation
author | Paula Stanciu <paula.stanciu@gmail.com> |
---|---|
date | Fri, 18 Jul 2008 12:01:38 +0300 |
parents | 50507763fd31 |
children | d0cbf303869b |
comparison
equal
deleted
inserted
replaced
2840:50507763fd31 | 2841:9637eab5a3dd |
---|---|
108 static int volatile registered_auth = 0; | 108 static int volatile registered_auth = 0; |
109 | 109 |
110 static gboolean auto_authorize = FALSE; | 110 static gboolean auto_authorize = FALSE; |
111 | 111 |
112 typedef enum { | 112 typedef enum { |
113 AGENT_ERROR_REJECT | 113 AGENT_ERROR_REJECT |
114 } AgentError; | 114 } AgentError; |
115 | 115 |
116 #define AGENT_ERROR (agent_error_quark()) | 116 #define AGENT_ERROR (agent_error_quark()) |
117 | 117 |
118 #define AGENT_ERROR_TYPE (agent_error_get_type()) | 118 #define AGENT_ERROR_TYPE (agent_error_get_type()) |
119 | 119 |
120 static GQuark agent_error_quark(void) | 120 static GQuark agent_error_quark(void) |
121 { | 121 { |
122 static GQuark quark = 0; | 122 static GQuark quark = 0; |
123 if (!quark) | 123 if (!quark) |
124 quark = g_quark_from_static_string("agent"); | 124 quark = g_quark_from_static_string("agent"); |
125 | 125 |
126 return quark; | 126 return quark; |
127 } | 127 } |
128 | 128 |
129 #define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC } | 129 #define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC } |
130 | 130 |
131 static GType agent_error_get_type(void) | 131 static GType agent_error_get_type(void) |
132 { | 132 { |
133 static GType etype = 0; | 133 static GType etype = 0; |
134 if (etype == 0) { | 134 if (etype == 0) { |
135 static const GEnumValue values[] = { | 135 static const GEnumValue values[] = { |
136 ENUM_ENTRY(AGENT_ERROR_REJECT, "Rejected"), | 136 ENUM_ENTRY(AGENT_ERROR_REJECT, "Rejected"), |
137 { 0, 0, 0 } | 137 { 0, 0, 0 } |
138 }; | 138 }; |
139 | 139 |
140 etype = g_enum_register_static("agent", values); | 140 etype = g_enum_register_static("agent", values); |
141 } | 141 } |
142 | 142 |
143 return etype; | 143 return etype; |
144 } | 144 } |
145 | 145 |
146 static GList *input_list = NULL; | 146 static GList *input_list = NULL; |
147 | 147 |
148 struct input_data { | 148 struct input_data { |
149 char *path; | 149 char *path; |
150 char *address; | 150 char *address; |
151 char *service; | 151 char *service; |
152 char *uuid; | 152 char *uuid; |
153 DBusGMethodInvocation *context; | 153 DBusGMethodInvocation *context; |
154 }; | 154 }; |
155 | 155 |
156 static gint input_compare(gconstpointer a, gconstpointer b) | 156 static gint input_compare(gconstpointer a, gconstpointer b) |
157 { | 157 { |
158 struct input_data *a_data = (struct input_data *) a; | 158 struct input_data *a_data = (struct input_data *) a; |
159 struct input_data *b_data = (struct input_data *) b; | 159 struct input_data *b_data = (struct input_data *) b; |
160 | 160 |
161 return g_ascii_strcasecmp(a_data->address, b_data->address); | 161 return g_ascii_strcasecmp(a_data->address, b_data->address); |
162 } | 162 } |
163 | 163 |
164 static void input_free(struct input_data *input) | 164 static void input_free(struct input_data *input) |
165 { | 165 { |
166 | 166 |
167 input_list = g_list_remove(input_list, input); | 167 input_list = g_list_remove(input_list, input); |
168 | 168 |
169 g_free(input->uuid); | 169 g_free(input->uuid); |
170 g_free(input->service); | 170 g_free(input->service); |
171 g_free(input->address); | 171 g_free(input->address); |
172 g_free(input->path); | 172 g_free(input->path); |
173 g_free(input); | 173 g_free(input); |
174 | 174 |
175 // if (g_list_length(input_list) == 0) | 175 // if (g_list_length(input_list) == 0) |
176 // disable_blinking(); | 176 // disable_blinking(); |
177 } | 177 } |
178 | 178 |
179 static void passkey_callback(gint response, gpointer user_data) | 179 static void passkey_callback(gint response, gpointer user_data) |
180 { | 180 { |
181 struct input_data *input = user_data; | 181 struct input_data *input = user_data; |
182 | 182 |
183 if (response == GTK_RESPONSE_ACCEPT) { | 183 if (response == GTK_RESPONSE_ACCEPT) { |
184 const char *passkey; | 184 const char *passkey; |
185 /*!!!!!!!!! hardcoded passkey !!!!!!!!!!!!! to modify*/ | 185 /*!!!!!!!!! hardcoded passkey !!!!!!!!!!!!! to modify*/ |
186 passkey ="0000"; | 186 passkey ="0000"; |
187 dbus_g_method_return(input->context, passkey); | 187 dbus_g_method_return(input->context, passkey); |
188 } else { | 188 } else { |
189 GError *error; | 189 GError *error; |
190 error = g_error_new(AGENT_ERROR, AGENT_ERROR_REJECT, | 190 error = g_error_new(AGENT_ERROR, AGENT_ERROR_REJECT, |
191 "Pairing request rejected"); | 191 "Pairing request rejected"); |
192 dbus_g_method_return_error(input->context, error); | 192 dbus_g_method_return_error(input->context, error); |
193 } | 193 } |
194 | 194 |
195 input_free(input); | 195 input_free(input); |
196 } | 196 } |
197 | 197 |
198 static void confirm_callback(gint response, gpointer user_data) | 198 static void confirm_callback(gint response, gpointer user_data) |
199 { | 199 { |
200 struct input_data *input = user_data; | 200 struct input_data *input = user_data; |
201 | 201 |
202 if (response != GTK_RESPONSE_YES) { | 202 if (response != GTK_RESPONSE_YES) { |
203 GError *error; | 203 GError *error; |
204 error = g_error_new(AGENT_ERROR, AGENT_ERROR_REJECT, | 204 error = g_error_new(AGENT_ERROR, AGENT_ERROR_REJECT, |
205 "Confirmation request rejected"); | 205 "Confirmation request rejected"); |
206 dbus_g_method_return_error(input->context, error); | 206 dbus_g_method_return_error(input->context, error); |
207 } else | 207 } else |
208 dbus_g_method_return(input->context); | 208 dbus_g_method_return(input->context); |
209 | 209 |
210 input_free(input); | 210 input_free(input); |
211 } | 211 } |
212 | 212 |
213 static void set_trusted(struct input_data *input) | 213 static void set_trusted(struct input_data *input) |
214 { | 214 { |
215 DBusGProxy *object; | 215 DBusGProxy *object; |
216 gboolean active; | 216 gboolean active; |
217 | 217 |
218 //aways set trusted -- we dont have yet an active button - | 218 //aways set trusted -- we dont have yet an active button - |
219 //maybe I'll add one | 219 //maybe I'll add one |
220 object = dbus_g_proxy_new_for_name(connection, "org.bluez", | 220 object = dbus_g_proxy_new_for_name(connection, "org.bluez", |
221 input->path, "org.bluez.Adapter"); | 221 input->path, "org.bluez.Adapter"); |
222 | 222 |
223 dbus_g_proxy_call(object, "SetTrusted", NULL, | 223 dbus_g_proxy_call(object, "SetTrusted", NULL, |
224 G_TYPE_STRING, input->address, G_TYPE_INVALID, | 224 G_TYPE_STRING, input->address, G_TYPE_INVALID, |
225 G_TYPE_INVALID); | 225 G_TYPE_INVALID); |
226 } | 226 } |
227 | 227 |
228 static void auth_callback(gint response, gpointer user_data) | 228 static void auth_callback(gint response, gpointer user_data) |
229 { | 229 { |
230 | 230 |
231 /* I must call this function with the alway allow option and | 231 /* I must call this function with the alway allow option and |
232 * don't boder to aks the user - to many questions | 232 * don't boder to aks the user - to many questions |
233 */ | 233 */ |
234 struct input_data *input = user_data; | 234 struct input_data *input = user_data; |
235 | 235 |
236 if (response == GTK_RESPONSE_YES) { | 236 if (response == GTK_RESPONSE_YES) { |
237 set_trusted(input); | 237 set_trusted(input); |
238 dbus_g_method_return(input->context); | 238 dbus_g_method_return(input->context); |
239 } else { | 239 } else { |
240 GError *error; | 240 GError *error; |
241 error = g_error_new(AGENT_ERROR, AGENT_ERROR_REJECT, | 241 error = g_error_new(AGENT_ERROR, AGENT_ERROR_REJECT, |
242 "Authorization request rejected"); | 242 "Authorization request rejected"); |
243 dbus_g_method_return_error(input->context, error); | 243 dbus_g_method_return_error(input->context, error); |
244 } | 244 } |
245 | 245 |
246 input_free(input); | 246 input_free(input); |
247 } | 247 } |
248 | 248 |
249 | 249 |
250 static void passkey_dialog(const char *path, const char *address, | 250 static void passkey_dialog(const char *path, const char *address, |
251 const gchar *device, DBusGMethodInvocation *context) | 251 const gchar *device, DBusGMethodInvocation *context) |
252 { | 252 { |
253 struct input_data *input; | 253 struct input_data *input; |
254 | 254 |
255 input = g_try_malloc0(sizeof(*input)); | 255 input = g_try_malloc0(sizeof(*input)); |
256 if (!input) | 256 if (!input) |
257 return; | 257 return; |
258 | 258 |
259 input->path = g_strdup(path); | 259 input->path = g_strdup(path); |
260 input->address = g_strdup(address); | 260 input->address = g_strdup(address); |
261 | 261 |
262 input->context = context; | 262 input->context = context; |
263 | 263 |
264 | 264 |
265 | 265 |
266 /* | 266 /* |
267 g_signal_connect(G_OBJECT(entry), "changed", | 267 g_signal_connect(G_OBJECT(entry), "changed", |
268 G_CALLBACK(changed_callback), input); | 268 G_CALLBACK(changed_callback), input); |
269 | 269 |
270 | 270 |
271 g_signal_connect(G_OBJECT(button), "toggled", | 271 g_signal_connect(G_OBJECT(button), "toggled", |
272 G_CALLBACK(toggled_callback), input); | 272 G_CALLBACK(toggled_callback), input); |
273 | 273 |
274 gtk_container_add(GTK_CONTAINER(vbox), button); | 274 gtk_container_add(GTK_CONTAINER(vbox), button); |
275 | 275 |
276 input_list = g_list_append(input_list, input); | 276 input_list = g_list_append(input_list, input); |
277 | 277 |
278 g_signal_connect(G_OBJECT(dialog), "response", | 278 g_signal_connect(G_OBJECT(dialog), "response", |
279 G_CALLBACK(passkey_callback), input); | 279 G_CALLBACK(passkey_callback), input); |
280 | 280 |
281 enable_blinking(); | 281 enable_blinking(); |
282 */ | 282 */ |
283 printf("passkey callback\n"); | 283 printf("passkey callback\n"); |
284 passkey_callback(GTK_RESPONSE_ACCEPT,input); | 284 passkey_callback(GTK_RESPONSE_ACCEPT,input); |
285 } | 285 } |
286 | 286 |
287 static void confirm_dialog(const char *path, const char *address, | 287 static void confirm_dialog(const char *path, const char *address, |
288 const char *value, const gchar *device, | 288 const char *value, const gchar *device, |
289 DBusGMethodInvocation *context) | 289 DBusGMethodInvocation *context) |
290 { | 290 { |
291 struct input_data *input; | 291 struct input_data *input; |
292 | 292 |
293 input = g_try_malloc0(sizeof(*input)); | 293 input = g_try_malloc0(sizeof(*input)); |
294 if (!input) | 294 if (!input) |
295 return; | 295 return; |
296 | 296 |
297 input->path = g_strdup(path); | 297 input->path = g_strdup(path); |
298 input->address = g_strdup(address); | 298 input->address = g_strdup(address); |
299 | 299 |
300 input->context = context; | 300 input->context = context; |
301 | 301 |
302 | 302 |
303 // g_signal_connect(G_OBJECT(dialog), "response", | 303 // g_signal_connect(G_OBJECT(dialog), "response", |
304 // G_CALLBACK(confirm_callback), input); | 304 // G_CALLBACK(confirm_callback), input); |
305 | 305 |
306 //enable_blinking(); | 306 //enable_blinking(); |
307 } | 307 } |
308 | 308 |
309 static void auth_dialog(const char *path, const char *address, | 309 static void auth_dialog(const char *path, const char *address, |
310 const char *service, const char *uuid, const gchar *device, | 310 const char *service, const char *uuid, const gchar *device, |
311 const gchar *profile, DBusGMethodInvocation *context) | 311 const gchar *profile, DBusGMethodInvocation *context) |
312 { | 312 { |
313 GtkWidget *dialog; | 313 GtkWidget *dialog; |
314 GtkWidget *button; | 314 GtkWidget *button; |
315 GtkWidget *image; | 315 GtkWidget *image; |
316 GtkWidget *label; | 316 GtkWidget *label; |
317 GtkWidget *table; | 317 GtkWidget *table; |
318 GtkWidget *vbox; | 318 GtkWidget *vbox; |
319 gchar *markup, *text; | 319 gchar *markup, *text; |
320 struct input_data *input; | 320 struct input_data *input; |
321 | 321 |
322 input = g_try_malloc0(sizeof(*input)); | 322 input = g_try_malloc0(sizeof(*input)); |
323 if (!input) | 323 if (!input) |
324 return; | 324 return; |
325 | 325 |
326 input->path = g_strdup(path); | 326 input->path = g_strdup(path); |
327 input->address = g_strdup(address); | 327 input->address = g_strdup(address); |
328 input->service = g_strdup(service); | 328 input->service = g_strdup(service); |
329 input->uuid = g_strdup(uuid); | 329 input->uuid = g_strdup(uuid); |
330 | 330 |
331 input->context = context; | 331 input->context = context; |
332 | 332 |
333 | 333 |
334 /* translators: Whether to grant access to a particular service | 334 /* translators: Whether to grant access to a particular service |
335 * to the device mentioned */ | 335 * to the device mentioned */ |
336 | 336 |
337 // g_signal_connect(G_OBJECT(dialog), "response", | 337 // g_signal_connect(G_OBJECT(dialog), "response", |
338 // G_CALLBACK(auth_callback), input); | 338 // G_CALLBACK(auth_callback), input); |
339 | 339 |
340 //enable_blinking(); | 340 //enable_blinking(); |
341 } | 341 } |
342 | 342 |
343 typedef struct { | 343 typedef struct { |
344 GObject parent; | 344 GObject parent; |
345 } PasskeyAgent; | 345 } PasskeyAgent; |
346 | 346 |
347 typedef struct { | 347 typedef struct { |
348 GObjectClass parent; | 348 GObjectClass parent; |
349 } PasskeyAgentClass; | 349 } PasskeyAgentClass; |
350 | 350 |
351 static GObjectClass *passkey_agent_parent; | 351 static GObjectClass *passkey_agent_parent; |
352 | 352 |
353 G_DEFINE_TYPE(PasskeyAgent, passkey_agent, G_TYPE_OBJECT) | 353 G_DEFINE_TYPE(PasskeyAgent, passkey_agent, G_TYPE_OBJECT) |
354 | 354 |
355 #define PASSKEY_AGENT_OBJECT_TYPE (passkey_agent_get_type()) | 355 #define PASSKEY_AGENT_OBJECT_TYPE (passkey_agent_get_type()) |
356 | 356 |
357 #define PASSKEY_AGENT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \ | 357 #define PASSKEY_AGENT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \ |
358 PASSKEY_AGENT_OBJECT_TYPE, PasskeyAgent)) | 358 PASSKEY_AGENT_OBJECT_TYPE, PasskeyAgent)) |
359 | 359 |
360 static void passkey_agent_finalize(GObject *obj) | 360 static void passkey_agent_finalize(GObject *obj) |
361 { | 361 { |
362 passkey_agent_parent->finalize(obj); | 362 passkey_agent_parent->finalize(obj); |
363 } | 363 } |
364 | 364 |
365 static void passkey_agent_init(PasskeyAgent *obj) | 365 static void passkey_agent_init(PasskeyAgent *obj) |
366 { | 366 { |
367 g_printf("passkeyagent init\n"); | 367 g_printf("passkeyagent init\n"); |
368 } | 368 } |
369 | 369 |
370 static void passkey_agent_class_init(PasskeyAgentClass *klass) | 370 static void passkey_agent_class_init(PasskeyAgentClass *klass) |
371 { | 371 { |
372 GObjectClass *gobject_class; | 372 GObjectClass *gobject_class; |
373 | 373 |
374 passkey_agent_parent = g_type_class_peek_parent(klass); | 374 passkey_agent_parent = g_type_class_peek_parent(klass); |
375 | 375 |
376 gobject_class = G_OBJECT_CLASS(klass); | 376 gobject_class = G_OBJECT_CLASS(klass); |
377 gobject_class->finalize = passkey_agent_finalize; | 377 gobject_class->finalize = passkey_agent_finalize; |
378 } | 378 } |
379 | 379 |
380 static PasskeyAgent *passkey_agent_new(const char *path) | 380 static PasskeyAgent *passkey_agent_new(const char *path) |
381 { | 381 { |
382 PasskeyAgent *agent; | 382 PasskeyAgent *agent; |
383 | 383 |
384 agent = g_object_new(PASSKEY_AGENT_OBJECT_TYPE, NULL); | 384 agent = g_object_new(PASSKEY_AGENT_OBJECT_TYPE, NULL); |
385 | 385 |
386 dbus_g_connection_register_g_object(connection, path, G_OBJECT(agent)); | 386 dbus_g_connection_register_g_object(connection, path, G_OBJECT(agent)); |
387 g_printf("new passkey agent \n"); | 387 g_printf("new passkey agent \n"); |
388 return agent; | 388 return agent; |
389 } | 389 } |
390 | 390 |
391 | 391 |
392 | 392 |
393 static gboolean passkey_agent_request(PasskeyAgent *agent, | 393 static gboolean passkey_agent_request(PasskeyAgent *agent, |
394 const char *path, const char *address, | 394 const char *path, const char *address, |
395 DBusGMethodInvocation *context) | 395 DBusGMethodInvocation *context) |
396 { | 396 { |
397 printf("passkey_agent request\n"); | 397 printf("passkey_agent request\n"); |
398 DBusGProxy *object; | 398 DBusGProxy *object; |
399 const char *adapter = NULL, *name = NULL; | 399 const char *adapter = NULL, *name = NULL; |
400 gchar *device, *line; | 400 gchar *device, *line; |
401 | 401 |
402 object = dbus_g_proxy_new_for_name(connection, "org.bluez", | 402 object = dbus_g_proxy_new_for_name(connection, "org.bluez", |
403 path, "org.bluez.Adapter"); | 403 path, "org.bluez.Adapter"); |
404 | 404 |
405 dbus_g_proxy_call(object, "GetName", NULL, G_TYPE_INVALID, | 405 dbus_g_proxy_call(object, "GetName", NULL, G_TYPE_INVALID, |
406 G_TYPE_STRING, &adapter, G_TYPE_INVALID); | 406 G_TYPE_STRING, &adapter, G_TYPE_INVALID); |
407 | 407 |
408 dbus_g_proxy_call(object, "GetRemoteName", NULL, | 408 dbus_g_proxy_call(object, "GetRemoteName", NULL, |
409 G_TYPE_STRING, address, G_TYPE_INVALID, | 409 G_TYPE_STRING, address, G_TYPE_INVALID, |
410 G_TYPE_STRING, &name, G_TYPE_INVALID); | 410 G_TYPE_STRING, &name, G_TYPE_INVALID); |
411 | 411 |
412 if (name) { | 412 if (name) { |
413 if (g_strrstr(name, address)) | 413 if (g_strrstr(name, address)) |
414 device = g_strdup(name); | 414 device = g_strdup(name); |
415 else | 415 else |
416 device = g_strdup_printf("%s (%s)", name, address); | 416 device = g_strdup_printf("%s (%s)", name, address); |
417 } else | 417 } else |
418 device = g_strdup(address); | 418 device = g_strdup(address); |
419 | 419 |
420 passkey_dialog(path, address, device, context); | 420 passkey_dialog(path, address, device, context); |
421 printf ("pairing request for device :%s",address); | 421 printf ("pairing request for device :%s",address); |
422 /* translators: this is a popup telling you a particular device | 422 /* translators: this is a popup telling you a particular device |
423 * has asked for pairing */ | 423 * has asked for pairing */ |
424 line = g_strdup_printf(_("Pairing request for '%s'"), device); | 424 line = g_strdup_printf(_("Pairing request for '%s'"), device); |
425 g_free(device); | 425 g_free(device); |
426 | 426 |
427 /*show_notification(adapter ? adapter : _("Bluetooth device"), | 427 /*show_notification(adapter ? adapter : _("Bluetooth device"), |
428 line, _("Enter passkey"), 0, | 428 line, _("Enter passkey"), 0, |
429 G_CALLBACK(notification_closed)); | 429 G_CALLBACK(notification_closed)); |
430 */ | 430 */ |
431 g_free(line); | 431 g_free(line); |
432 | 432 |
433 return TRUE; | 433 return TRUE; |
434 } | 434 } |
435 | 435 |
436 static gboolean passkey_agent_confirm(PasskeyAgent *agent, | 436 static gboolean passkey_agent_confirm(PasskeyAgent *agent, |
437 const char *path, const char *address, | 437 const char *path, const char *address, |
438 const char *value, DBusGMethodInvocation *context) | 438 const char *value, DBusGMethodInvocation *context) |
439 { | 439 { |
440 DBusGProxy *object; | 440 DBusGProxy *object; |
441 const char *adapter = NULL, *name = NULL; | 441 const char *adapter = NULL, *name = NULL; |
442 gchar *device, *line; | 442 gchar *device, *line; |
443 | 443 |
444 object = dbus_g_proxy_new_for_name(connection, "org.bluez", | 444 object = dbus_g_proxy_new_for_name(connection, "org.bluez", |
445 path, "org.bluez.Adapter"); | 445 path, "org.bluez.Adapter"); |
446 | 446 |
447 dbus_g_proxy_call(object, "GetName", NULL, G_TYPE_INVALID, | 447 dbus_g_proxy_call(object, "GetName", NULL, G_TYPE_INVALID, |
448 G_TYPE_STRING, &adapter, G_TYPE_INVALID); | 448 G_TYPE_STRING, &adapter, G_TYPE_INVALID); |
449 | 449 |
450 dbus_g_proxy_call(object, "GetRemoteName", NULL, | 450 dbus_g_proxy_call(object, "GetRemoteName", NULL, |
451 G_TYPE_STRING, address, G_TYPE_INVALID, | 451 G_TYPE_STRING, address, G_TYPE_INVALID, |
452 G_TYPE_STRING, &name, G_TYPE_INVALID); | 452 G_TYPE_STRING, &name, G_TYPE_INVALID); |
453 | 453 |
454 if (name) { | 454 if (name) { |
455 if (g_strrstr(name, address)) | 455 if (g_strrstr(name, address)) |
456 device = g_strdup(name); | 456 device = g_strdup(name); |
457 else | 457 else |
458 device = g_strdup_printf("%s (%s)", name, address); | 458 device = g_strdup_printf("%s (%s)", name, address); |
459 } else | 459 } else |
460 device = g_strdup(address); | 460 device = g_strdup(address); |
461 | 461 |
462 confirm_dialog(path, address, value, device, context); | 462 confirm_dialog(path, address, value, device, context); |
463 | 463 |
464 line = g_strdup_printf(_("Pairing request for '%s'"), device); | 464 line = g_strdup_printf(_("Pairing request for '%s'"), device); |
465 g_free(device); | 465 g_free(device); |
466 | 466 |
467 /*show_notification(adapter ? adapter : _("Bluetooth device"), | 467 /*show_notification(adapter ? adapter : _("Bluetooth device"), |
468 line, _("Confirm pairing"), 0, | 468 line, _("Confirm pairing"), 0, |
469 G_CALLBACK(notification_closed)); | 469 G_CALLBACK(notification_closed)); |
470 */ | 470 */ |
471 g_free (line); | 471 g_free (line); |
472 | 472 |
473 return TRUE; | 473 return TRUE; |
474 } | 474 } |
475 | 475 |
476 static gboolean passkey_agent_cancel(PasskeyAgent *agent, | 476 static gboolean passkey_agent_cancel(PasskeyAgent *agent, |
477 const char *path, const char *address, GError **error) | 477 const char *path, const char *address, GError **error) |
478 { | 478 { |
479 GList *list; | 479 GList *list; |
480 GError *result; | 480 GError *result; |
481 struct input_data *input; | 481 struct input_data *input; |
482 | 482 |
483 input = g_try_malloc0(sizeof(*input)); | 483 input = g_try_malloc0(sizeof(*input)); |
484 if (!input) | 484 if (!input) |
485 return FALSE; | 485 return FALSE; |
486 | 486 |
487 input->path = g_strdup(path); | 487 input->path = g_strdup(path); |
488 input->address = g_strdup(address); | 488 input->address = g_strdup(address); |
489 | 489 |
490 list = g_list_find_custom(input_list, input, input_compare); | 490 list = g_list_find_custom(input_list, input, input_compare); |
491 | 491 |
492 g_free(input->address); | 492 g_free(input->address); |
493 g_free(input->path); | 493 g_free(input->path); |
494 g_free(input); | 494 g_free(input); |
495 | 495 |
496 if (!list || !list->data) | 496 if (!list || !list->data) |
497 return FALSE; | 497 return FALSE; |
498 | 498 |
499 input = list->data; | 499 input = list->data; |
500 | 500 |
501 //close_notification(); | 501 //close_notification(); |
502 | 502 |
503 result = g_error_new(AGENT_ERROR, AGENT_ERROR_REJECT, | 503 result = g_error_new(AGENT_ERROR, AGENT_ERROR_REJECT, |
504 "Agent callback canceled"); | 504 "Agent callback canceled"); |
505 | 505 |
506 dbus_g_method_return_error(input->context, result); | 506 dbus_g_method_return_error(input->context, result); |
507 | 507 |
508 input_free(input); | 508 input_free(input); |
509 | 509 |
510 return TRUE; | 510 return TRUE; |
511 } | 511 } |
512 | 512 |
513 static gboolean passkey_agent_release(PasskeyAgent *agent, GError **error) | 513 static gboolean passkey_agent_release(PasskeyAgent *agent, GError **error) |
514 { | 514 { |
515 registered_passkey = 0; | 515 registered_passkey = 0; |
516 | 516 |
517 return TRUE; | 517 return TRUE; |
518 } | 518 } |
519 | 519 |
520 #include "passkey-agent-glue.h" | 520 #include "passkey-agent-glue.h" |
521 | 521 |
522 typedef struct { | 522 typedef struct { |
523 GObject parent; | 523 GObject parent; |
524 } AuthAgent; | 524 } AuthAgent; |
525 | 525 |
526 typedef struct { | 526 typedef struct { |
527 GObjectClass parent; | 527 GObjectClass parent; |
528 } AuthAgentClass; | 528 } AuthAgentClass; |
529 | 529 |
530 static GObjectClass *auth_agent_parent; | 530 static GObjectClass *auth_agent_parent; |
531 | 531 |
532 G_DEFINE_TYPE(AuthAgent, auth_agent, G_TYPE_OBJECT) | 532 G_DEFINE_TYPE(AuthAgent, auth_agent, G_TYPE_OBJECT) |
533 | 533 |
534 #define AUTH_AGENT_OBJECT_TYPE (auth_agent_get_type()) | 534 #define AUTH_AGENT_OBJECT_TYPE (auth_agent_get_type()) |
535 | 535 |
536 #define AUTH_AGENT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \ | 536 #define AUTH_AGENT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \ |
537 AUTH_AGENT_OBJECT_TYPE, AuthAgent)) | 537 AUTH_AGENT_OBJECT_TYPE, AuthAgent)) |
538 | 538 |
539 static void auth_agent_finalize(GObject *obj) | 539 static void auth_agent_finalize(GObject *obj) |
540 { | 540 { |
541 auth_agent_parent->finalize(obj); | 541 auth_agent_parent->finalize(obj); |
542 } | 542 } |
543 | 543 |
544 static void auth_agent_init(AuthAgent *obj) | 544 static void auth_agent_init(AuthAgent *obj) |
545 { | 545 { |
546 } | 546 } |
547 | 547 |
548 static void auth_agent_class_init(AuthAgentClass *klass) | 548 static void auth_agent_class_init(AuthAgentClass *klass) |
549 { | 549 { |
550 GObjectClass *gobject_class; | 550 GObjectClass *gobject_class; |
551 | 551 |
552 auth_agent_parent = g_type_class_peek_parent(klass); | 552 auth_agent_parent = g_type_class_peek_parent(klass); |
553 | 553 |
554 gobject_class = G_OBJECT_CLASS(klass); | 554 gobject_class = G_OBJECT_CLASS(klass); |
555 gobject_class->finalize = auth_agent_finalize; | 555 gobject_class->finalize = auth_agent_finalize; |
556 } | 556 } |
557 | 557 |
558 static AuthAgent *auth_agent_new(const char *path) | 558 static AuthAgent *auth_agent_new(const char *path) |
559 { | 559 { |
560 AuthAgent *agent; | 560 AuthAgent *agent; |
561 | 561 |
562 agent = g_object_new(AUTH_AGENT_OBJECT_TYPE, NULL); | 562 agent = g_object_new(AUTH_AGENT_OBJECT_TYPE, NULL); |
563 | 563 |
564 dbus_g_connection_register_g_object(connection, path, G_OBJECT(agent)); | 564 dbus_g_connection_register_g_object(connection, path, G_OBJECT(agent)); |
565 | 565 |
566 return agent; | 566 return agent; |
567 } | 567 } |
568 | 568 |
569 static gboolean auth_agent_authorize(PasskeyAgent *agent, | 569 static gboolean auth_agent_authorize(PasskeyAgent *agent, |
570 const char *path, const char *address, const char *service, | 570 const char *path, const char *address, const char *service, |
571 const char *uuid, DBusGMethodInvocation *context) | 571 const char *uuid, DBusGMethodInvocation *context) |
572 { | 572 { |
573 DBusGProxy *object; | 573 DBusGProxy *object; |
574 const char *adapter = NULL, *name = NULL; | 574 const char *adapter = NULL, *name = NULL; |
575 gchar *device, *profile, *line; | 575 gchar *device, *profile, *line; |
576 | 576 |
577 if (auto_authorize == TRUE) { | 577 if (auto_authorize == TRUE) { |
578 dbus_g_method_return(context); | 578 dbus_g_method_return(context); |
579 return TRUE; | 579 return TRUE; |
580 } | 580 } |
581 | 581 |
582 object = dbus_g_proxy_new_for_name(connection, "org.bluez", | 582 object = dbus_g_proxy_new_for_name(connection, "org.bluez", |
583 path, "org.bluez.Adapter"); | 583 path, "org.bluez.Adapter"); |
584 | 584 |
585 dbus_g_proxy_call(object, "GetName", NULL, G_TYPE_INVALID, | 585 dbus_g_proxy_call(object, "GetName", NULL, G_TYPE_INVALID, |
586 G_TYPE_STRING, &adapter, G_TYPE_INVALID); | 586 G_TYPE_STRING, &adapter, G_TYPE_INVALID); |
587 | 587 |
588 dbus_g_proxy_call(object, "GetRemoteName", NULL, | 588 dbus_g_proxy_call(object, "GetRemoteName", NULL, |
589 G_TYPE_STRING, address, G_TYPE_INVALID, | 589 G_TYPE_STRING, address, G_TYPE_INVALID, |
590 G_TYPE_STRING, &name, G_TYPE_INVALID); | 590 G_TYPE_STRING, &name, G_TYPE_INVALID); |
591 | 591 |
592 object = dbus_g_proxy_new_for_name(connection, "org.bluez", | 592 object = dbus_g_proxy_new_for_name(connection, "org.bluez", |
593 service, "org.bluez.Service"); | 593 service, "org.bluez.Service"); |
594 | 594 |
595 dbus_g_proxy_call(object, "GetName", NULL, G_TYPE_INVALID, | 595 dbus_g_proxy_call(object, "GetName", NULL, G_TYPE_INVALID, |
596 G_TYPE_STRING, &profile, G_TYPE_INVALID); | 596 G_TYPE_STRING, &profile, G_TYPE_INVALID); |
597 | 597 |
598 if (name) { | 598 if (name) { |
599 if (g_strrstr(name, address)) | 599 if (g_strrstr(name, address)) |
600 device = g_strdup(name); | 600 device = g_strdup(name); |
601 else | 601 else |
602 device = g_strdup_printf("%s (%s)", name, address); | 602 device = g_strdup_printf("%s (%s)", name, address); |
603 } else | 603 } else |
604 device = g_strdup(address); | 604 device = g_strdup(address); |
605 | 605 |
606 auth_dialog(path, address, service, uuid, device, profile, context); | 606 auth_dialog(path, address, service, uuid, device, profile, context); |
607 | 607 |
608 line = g_strdup_printf(_("Authorization request for %s"), device); | 608 line = g_strdup_printf(_("Authorization request for %s"), device); |
609 g_free(device); | 609 g_free(device); |
610 | 610 |
611 /*show_notification(adapter ? adapter : _("Bluetooth device"), | 611 /*show_notification(adapter ? adapter : _("Bluetooth device"), |
612 line, _("Check authorization"), 0, | 612 line, _("Check authorization"), 0, |
613 G_CALLBACK(notification_closed)); | 613 G_CALLBACK(notification_closed)); |
614 */ | 614 */ |
615 g_free(line); | 615 g_free(line); |
616 | 616 |
617 return TRUE; | 617 return TRUE; |
618 } | 618 } |
619 | 619 |
620 static gboolean auth_agent_cancel(PasskeyAgent *agent, | 620 static gboolean auth_agent_cancel(PasskeyAgent *agent, |
621 const char *path, const char *address, const char *service, | 621 const char *path, const char *address, const char *service, |
622 const char *uuid, DBusGMethodInvocation *context) | 622 const char *uuid, DBusGMethodInvocation *context) |
623 { | 623 { |
624 GList *list; | 624 GList *list; |
625 GError *result; | 625 GError *result; |
626 struct input_data *input; | 626 struct input_data *input; |
627 | 627 |
628 input = g_try_malloc0(sizeof(*input)); | 628 input = g_try_malloc0(sizeof(*input)); |
629 if (!input) | 629 if (!input) |
630 return FALSE; | 630 return FALSE; |
631 | 631 |
632 input->path = g_strdup(path); | 632 input->path = g_strdup(path); |
633 input->address = g_strdup(address); | 633 input->address = g_strdup(address); |
634 input->service = g_strdup(service); | 634 input->service = g_strdup(service); |
635 input->uuid = g_strdup(uuid); | 635 input->uuid = g_strdup(uuid); |
636 | 636 |
637 list = g_list_find_custom(input_list, input, input_compare); | 637 list = g_list_find_custom(input_list, input, input_compare); |
638 | 638 |
639 g_free(input->uuid); | 639 g_free(input->uuid); |
640 g_free(input->service); | 640 g_free(input->service); |
641 g_free(input->address); | 641 g_free(input->address); |
642 g_free(input->path); | 642 g_free(input->path); |
643 g_free(input); | 643 g_free(input); |
644 | 644 |
645 if (!list || !list->data) | 645 if (!list || !list->data) |
646 return FALSE; | 646 return FALSE; |
647 | 647 |
648 input = list->data; | 648 input = list->data; |
649 | 649 |
650 //close_notification(); | 650 //close_notification(); |
651 | 651 |
652 result = g_error_new(AGENT_ERROR, AGENT_ERROR_REJECT, | 652 result = g_error_new(AGENT_ERROR, AGENT_ERROR_REJECT, |
653 "Agent callback canceled"); | 653 "Agent callback canceled"); |
654 | 654 |
655 dbus_g_method_return_error(input->context, result); | 655 dbus_g_method_return_error(input->context, result); |
656 | 656 |
657 input_free(input); | 657 input_free(input); |
658 | 658 |
659 return TRUE; | 659 return TRUE; |
660 } | 660 } |
661 | 661 |
662 static gboolean auth_agent_release(PasskeyAgent *agent, GError **error) | 662 static gboolean auth_agent_release(PasskeyAgent *agent, GError **error) |
663 { | 663 { |
664 registered_auth = 0; | 664 registered_auth = 0; |
665 | 665 |
666 return TRUE; | 666 return TRUE; |
667 } | 667 } |
668 | 668 |
669 #include "auth-agent-glue.h" | 669 #include "auth-agent-glue.h" |
670 | 670 |
671 int register_agents(void) | 671 int register_agents(void) |
672 { | 672 { |
673 DBusGProxy *object; | 673 DBusGProxy *object; |
674 GError *error = NULL; | 674 GError *error = NULL; |
675 | 675 |
676 if (registered_passkey && registered_auth) | 676 if (registered_passkey && registered_auth) |
677 return 0; | 677 return 0; |
678 | 678 |
679 object = dbus_g_proxy_new_for_name(connection, "org.bluez", | 679 object = dbus_g_proxy_new_for_name(connection, "org.bluez", |
680 "/org/bluez", "org.bluez.Security"); | 680 "/org/bluez", "org.bluez.Security"); |
681 | 681 |
682 if (!registered_passkey) { | 682 if (!registered_passkey) { |
683 | 683 |
684 dbus_g_proxy_call(object, "RegisterDefaultPasskeyAgent", | 684 dbus_g_proxy_call(object, "RegisterDefaultPasskeyAgent", |
685 &error, G_TYPE_STRING, PASSKEY_AGENT_PATH, | 685 &error, G_TYPE_STRING, PASSKEY_AGENT_PATH, |
686 G_TYPE_INVALID, G_TYPE_INVALID); | 686 G_TYPE_INVALID, G_TYPE_INVALID); |
687 | 687 |
688 if (error != NULL) { | 688 if (error != NULL) { |
689 g_error_free(error); | 689 g_error_free(error); |
690 return -1; | 690 return -1; |
691 } | 691 } |
692 | 692 |
693 registered_passkey = 1; | 693 registered_passkey = 1; |
694 } | 694 } |
695 | 695 |
696 if (!registered_auth) { | 696 if (!registered_auth) { |
697 dbus_g_proxy_call(object, "RegisterDefaultAuthorizationAgent", | 697 dbus_g_proxy_call(object, "RegisterDefaultAuthorizationAgent", |
698 &error, G_TYPE_STRING, AUTH_AGENT_PATH, | 698 &error, G_TYPE_STRING, AUTH_AGENT_PATH, |
699 G_TYPE_INVALID, G_TYPE_INVALID); | 699 G_TYPE_INVALID, G_TYPE_INVALID); |
700 | 700 |
701 if (error != NULL) { | 701 if (error != NULL) { |
702 g_error_free(error); | 702 g_error_free(error); |
703 return -1; | 703 return -1; |
704 } | 704 } |
705 | 705 |
706 registered_auth = 1; | 706 registered_auth = 1; |
707 } | 707 } |
708 | 708 |
709 return 0; | 709 return 0; |
710 } | 710 } |
711 | 711 |
712 void unregister_agents(void) | 712 void unregister_agents(void) |
713 { | 713 { |
714 registered_passkey = 0; | 714 registered_passkey = 0; |
715 registered_auth = 0; | 715 registered_auth = 0; |
716 } | 716 } |
717 | 717 |
718 int setup_agents(DBusGConnection *conn) | 718 int setup_agents(DBusGConnection *conn) |
719 { | 719 { |
720 void *agent; | 720 void *agent; |
721 | 721 |
722 connection = dbus_g_connection_ref(conn); | 722 connection = dbus_g_connection_ref(conn); |
723 | 723 |
724 dbus_g_object_type_install_info(PASSKEY_AGENT_OBJECT_TYPE, | 724 dbus_g_object_type_install_info(PASSKEY_AGENT_OBJECT_TYPE, |
725 &dbus_glib_passkey_agent_object_info); | 725 &dbus_glib_passkey_agent_object_info); |
726 | 726 |
727 dbus_g_object_type_install_info(AUTH_AGENT_OBJECT_TYPE, | 727 dbus_g_object_type_install_info(AUTH_AGENT_OBJECT_TYPE, |
728 &dbus_glib_auth_agent_object_info); | 728 &dbus_glib_auth_agent_object_info); |
729 | 729 |
730 dbus_g_error_domain_register(AGENT_ERROR, "org.bluez.Error", | 730 dbus_g_error_domain_register(AGENT_ERROR, "org.bluez.Error", |
731 AGENT_ERROR_TYPE); | 731 AGENT_ERROR_TYPE); |
732 | 732 |
733 agent = passkey_agent_new(PASSKEY_AGENT_PATH); | 733 agent = passkey_agent_new(PASSKEY_AGENT_PATH); |
734 | 734 |
735 agent = auth_agent_new(AUTH_AGENT_PATH); | 735 agent = auth_agent_new(AUTH_AGENT_PATH); |
736 | 736 |
737 return 0; | 737 return 0; |
738 } | 738 } |
739 | 739 |
740 void cleanup_agents(void) | 740 void cleanup_agents(void) |
741 { | 741 { |
742 unregister_agents(); | 742 unregister_agents(); |
743 | 743 |
744 dbus_g_connection_unref(connection); | 744 dbus_g_connection_unref(connection); |
745 } | 745 } |
746 | 746 |
747 void show_agents(void) | 747 void show_agents(void) |
748 { | 748 { |
749 printf("show_agents\n"); | 749 printf("show_agents\n"); |
750 //close_notification(); | 750 //close_notification(); |
751 | 751 |
752 // g_list_foreach(input_list, show_dialog, NULL); | 752 // g_list_foreach(input_list, show_dialog, NULL); |
753 | 753 |
754 // disable_blinking(); | 754 // disable_blinking(); |
755 } | 755 } |
756 | 756 |
757 void set_auto_authorize(gboolean value) | 757 void set_auto_authorize(gboolean value) |
758 { | 758 { |
759 auto_authorize = value; | 759 auto_authorize = value; |
760 } | 760 } |
761 void run_agents() | 761 void run_agents() |
762 { | 762 { |
763 setup_agents(bus); | 763 setup_agents(bus); |
764 | 764 |
765 register_agents(); | 765 register_agents(); |
766 | 766 |
767 } | 767 |
768 | 768 } |
769 |