~ubuntu-branches/ubuntu/wily/bluez/wily

« back to all changes in this revision

Viewing changes to cups/main.c

ImportĀ upstreamĀ versionĀ 4.81

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *
3
3
 *  BlueZ - Bluetooth protocol stack for Linux
4
4
 *
5
 
 *  Copyright (C) 2003-2009  Marcel Holtmann <marcel@holtmann.org>
 
5
 *  Copyright (C) 2003-2010  Marcel Holtmann <marcel@holtmann.org>
6
6
 *
7
7
 *
8
8
 *  This program is free software; you can redistribute it and/or modify
62
62
};
63
63
 
64
64
static void element_start(GMarkupParseContext *context,
65
 
                const gchar *element_name, const gchar **attribute_names,
66
 
                const gchar **attribute_values, gpointer user_data, GError **err)
 
65
                                const gchar *element_name,
 
66
                                const gchar **attribute_names,
 
67
                                const gchar **attribute_values,
 
68
                                gpointer user_data, GError **err)
67
69
{
68
70
        struct context_data *ctx_data = user_data;
69
71
 
73
75
        if (!strcmp(element_name, "attribute")) {
74
76
                int i;
75
77
                for (i = 0; attribute_names[i]; i++) {
76
 
                        if (!strcmp(attribute_names[i], "id")) {
77
 
                                if (strtol(attribute_values[i], 0, 0) == ATTRID_1284ID)
78
 
                                        ctx_data->found = TRUE;
79
 
                                break;
80
 
                        }
 
78
                        if (strcmp(attribute_names[i], "id") != 0)
 
79
                                continue;
 
80
                        if (strtol(attribute_values[i], 0, 0) == ATTRID_1284ID)
 
81
                                ctx_data->found = TRUE;
 
82
                        break;
81
83
                }
82
84
                return;
83
85
        }
153
155
        dbus_message_iter_recurse(&reply_iter, &reply_iter_entry);
154
156
 
155
157
        /* Hopefully we only get one handle, or take a punt */
156
 
        while (dbus_message_iter_get_arg_type(&reply_iter_entry) == DBUS_TYPE_DICT_ENTRY) {
 
158
        while (dbus_message_iter_get_arg_type(&reply_iter_entry) ==
 
159
                                                        DBUS_TYPE_DICT_ENTRY) {
157
160
                guint32 key;
158
161
                DBusMessageIter dict_entry;
159
162
 
185
188
        return id;
186
189
}
187
190
 
188
 
static void add_device_to_list(const char *name, const char *bdaddr, const char *id)
 
191
static void print_printer_details(const char *name, const char *bdaddr,
 
192
                                                                const char *id)
 
193
{
 
194
        char *uri, *escaped;
 
195
 
 
196
        escaped = g_strdelimit(g_strdup(name), "\"", '\'');
 
197
        uri = g_strdup_printf("bluetooth://%c%c%c%c%c%c%c%c%c%c%c%c",
 
198
                                bdaddr[0], bdaddr[1],
 
199
                                bdaddr[3], bdaddr[4],
 
200
                                bdaddr[6], bdaddr[7],
 
201
                                bdaddr[9], bdaddr[10],
 
202
                                bdaddr[12], bdaddr[13],
 
203
                                bdaddr[15], bdaddr[16]);
 
204
        printf("direct %s \"%s\" \"%s (Bluetooth)\"", uri, escaped, escaped);
 
205
        if (id != NULL)
 
206
                printf(" \"%s\"\n", id);
 
207
        else
 
208
                printf("\n");
 
209
        g_free(escaped);
 
210
        g_free(uri);
 
211
}
 
212
 
 
213
static void add_device_to_list(const char *name, const char *bdaddr,
 
214
                                                                const char *id)
189
215
{
190
216
        struct cups_device *device;
191
217
        GSList *l;
212
238
        device->id = g_strdup(id);
213
239
 
214
240
        device_list = g_slist_prepend(device_list, device);
215
 
}
216
 
 
217
 
static void print_printer_details(const char *name, const char *bdaddr, const char *id)
218
 
{
219
 
        char *uri, *escaped;
220
 
 
221
 
        escaped = g_strdelimit(g_strdup(name), "\"", '\'');
222
 
        uri = g_strdup_printf("bluetooth://%c%c%c%c%c%c%c%c%c%c%c%c",
223
 
                 bdaddr[0], bdaddr[1],
224
 
                 bdaddr[3], bdaddr[4],
225
 
                 bdaddr[6], bdaddr[7],
226
 
                 bdaddr[9], bdaddr[10],
227
 
                 bdaddr[12], bdaddr[13],
228
 
                 bdaddr[15], bdaddr[16]);
229
 
        printf("network %s \"Unknown\" \"%s (Bluetooth)\"", uri, escaped);
230
 
        if (id != NULL)
231
 
                printf(" \"%s\"\n", id);
232
 
        else
233
 
                printf("\n");
234
 
        g_free(escaped);
235
 
        g_free(uri);
236
 
}
237
 
 
238
 
static gboolean parse_device_properties(DBusMessageIter *reply_iter, char **name, char **bdaddr)
 
241
        print_printer_details(device->name, device->bdaddr, device->id);
 
242
}
 
243
 
 
244
static gboolean parse_device_properties(DBusMessageIter *reply_iter,
 
245
                                                char **name, char **bdaddr)
239
246
{
240
247
        guint32 class = 0;
241
248
        DBusMessageIter reply_iter_entry;
245
252
 
246
253
        dbus_message_iter_recurse(reply_iter, &reply_iter_entry);
247
254
 
248
 
        while (dbus_message_iter_get_arg_type(&reply_iter_entry) == DBUS_TYPE_DICT_ENTRY) {
 
255
        while (dbus_message_iter_get_arg_type(&reply_iter_entry) ==
 
256
                                                        DBUS_TYPE_DICT_ENTRY) {
249
257
                const char *key;
250
258
                DBusMessageIter dict_entry, iter_dict_val;
251
259
 
300
308
        gboolean retval;
301
309
 
302
310
        message = dbus_message_new_method_call("org.bluez", device_path,
303
 
                                               "org.bluez.Device",
304
 
                                               "GetProperties");
 
311
                                                        "org.bluez.Device",
 
312
                                                        "GetProperties");
305
313
 
306
314
        reply = dbus_connection_send_with_reply_and_block(conn,
307
315
                                                        message, -1, NULL);
320
328
        return retval;
321
329
}
322
330
 
323
 
static void remote_device_found(const char *adapter, const char *bdaddr, const char *name)
 
331
static void remote_device_found(const char *adapter, const char *bdaddr,
 
332
                                                        const char *name)
324
333
{
325
334
        DBusMessage *message, *reply, *adapter_reply;
326
335
        DBusMessageIter iter;
331
340
 
332
341
        if (adapter == NULL) {
333
342
                message = dbus_message_new_method_call("org.bluez", "/",
334
 
                                                       "org.bluez.Manager",
335
 
                                                       "DefaultAdapter");
 
343
                                                        "org.bluez.Manager",
 
344
                                                        "DefaultAdapter");
336
345
 
337
346
                adapter_reply = dbus_connection_send_with_reply_and_block(conn,
338
 
                                                                  message, -1, NULL);
 
347
                                                        message, -1, NULL);
339
348
 
340
349
                dbus_message_unref(message);
341
350
 
342
 
                if (dbus_message_get_args(adapter_reply, NULL, DBUS_TYPE_OBJECT_PATH, &adapter, DBUS_TYPE_INVALID) == FALSE)
 
351
                if (dbus_message_get_args(adapter_reply, NULL,
 
352
                                        DBUS_TYPE_OBJECT_PATH, &adapter,
 
353
                                        DBUS_TYPE_INVALID) == FALSE)
343
354
                        return;
344
355
        }
345
356
 
346
357
        message = dbus_message_new_method_call("org.bluez", adapter,
347
 
                                               "org.bluez.Adapter",
348
 
                                               "FindDevice");
 
358
                                                        "org.bluez.Adapter",
 
359
                                                        "FindDevice");
349
360
        dbus_message_iter_init_append(message, &iter);
350
361
        dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &bdaddr);
351
362
 
359
370
 
360
371
        if (!reply) {
361
372
                message = dbus_message_new_method_call("org.bluez", adapter,
362
 
                                                       "org.bluez.Adapter",
363
 
                                                       "CreateDevice");
 
373
                                                        "org.bluez.Adapter",
 
374
                                                        "CreateDevice");
364
375
                dbus_message_iter_init_append(message, &iter);
365
376
                dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &bdaddr);
366
377
 
367
378
                reply = dbus_connection_send_with_reply_and_block(conn,
368
 
                                                                  message, -1, NULL);
 
379
                                                        message, -1, NULL);
369
380
 
370
381
                dbus_message_unref(message);
371
382
 
372
383
                if (!reply)
373
384
                        return;
374
 
        } else {
375
 
                if (dbus_message_get_args(reply, NULL, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID) == FALSE)
376
 
                        return;
377
385
        }
378
386
 
 
387
        if (dbus_message_get_args(reply, NULL,
 
388
                                        DBUS_TYPE_OBJECT_PATH, &object_path,
 
389
                                        DBUS_TYPE_INVALID) == FALSE)
 
390
                return;
 
391
 
379
392
        id = device_get_ieee1284_id(adapter, object_path);
380
393
        add_device_to_list(name, bdaddr, id);
381
394
        g_free(id);
383
396
 
384
397
static void discovery_completed(void)
385
398
{
386
 
        GSList *l;
387
 
 
388
 
        for (l = device_list; l != NULL; l = l->next) {
389
 
                struct cups_device *device = (struct cups_device *) l->data;
390
 
 
391
 
                if (device->name == NULL)
392
 
                        device->name = g_strdelimit(g_strdup(device->bdaddr), ":", '-');
393
 
                /* Give another try to getting an ID for the device */
394
 
                if (device->id == NULL)
395
 
                        remote_device_found(NULL, device->bdaddr, device->name);
396
 
                print_printer_details(device->name, device->bdaddr, device->id);
397
 
                g_free(device->name);
398
 
                g_free(device->bdaddr);
399
 
                g_free(device->id);
400
 
                g_free(device);
401
 
        }
402
 
 
403
399
        g_slist_free(device_list);
404
400
        device_list = NULL;
405
401
 
411
407
        GSList *l;
412
408
 
413
409
        for (l = device_list; l != NULL; l = l->next) {
414
 
                struct cups_device *device = (struct cups_device *) l->data;
 
410
                struct cups_device *device = l->data;
415
411
 
416
412
                if (strcmp(device->bdaddr, bdaddr) == 0) {
417
413
                        g_free(device->name);
429
425
        DBusError error;
430
426
        DBusMessage *message, *reply;
431
427
 
432
 
        message = dbus_message_new_method_call ("org.bluez", adapter,
 
428
        message = dbus_message_new_method_call("org.bluez", adapter,
433
429
                                                "org.bluez.Adapter",
434
430
                                                "ListDevices");
435
431
        if (message == NULL)
436
432
                return FALSE;
437
433
 
438
434
        dbus_error_init(&error);
439
 
        reply = dbus_connection_send_with_reply_and_block(conn,
440
 
                                                        message, -1, &error);
 
435
        reply = dbus_connection_send_with_reply_and_block(conn, message,
 
436
                                                                -1, &error);
441
437
 
442
438
        dbus_message_unref(message);
443
439
 
444
 
        if (&error != NULL && dbus_error_is_set(&error))
 
440
        if (dbus_error_is_set(&error))
445
441
                return FALSE;
446
442
 
447
443
        dbus_message_iter_init(reply, &reply_iter);
451
447
        }
452
448
 
453
449
        dbus_message_iter_recurse(&reply_iter, &iter_array);
454
 
        while (dbus_message_iter_get_arg_type(&iter_array) == DBUS_TYPE_OBJECT_PATH) {
 
450
        while (dbus_message_iter_get_arg_type(&iter_array) ==
 
451
                                                DBUS_TYPE_OBJECT_PATH) {
455
452
                const char *object_path;
456
453
                char *name = NULL;
457
454
                char *bdaddr = NULL;
474
471
        return FALSE;
475
472
}
476
473
 
477
 
static DBusHandlerResult filter_func(DBusConnection *connection, DBusMessage *message, void *user_data)
 
474
static DBusHandlerResult filter_func(DBusConnection *connection,
 
475
                                        DBusMessage *message, void *user_data)
478
476
{
479
477
        if (dbus_message_is_signal(message, "org.bluez.Adapter",
480
478
                                                "DeviceFound")) {
492
490
                g_free (name);
493
491
        } else if (dbus_message_is_signal(message, "org.bluez.Adapter",
494
492
                                                "DeviceDisappeared")) {
495
 
                char *bdaddr;
 
493
                const char *bdaddr;
496
494
 
497
495
                dbus_message_get_args(message, NULL,
498
496
                                        DBUS_TYPE_STRING, &bdaddr,
506
504
 
507
505
                dbus_message_iter_init(message, &iter);
508
506
                dbus_message_iter_get_basic(&iter, &name);
 
507
                if (name == NULL || strcmp(name, "Discovering") != 0)
 
508
                        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
509
509
                dbus_message_iter_next(&iter);
510
510
                dbus_message_iter_recurse(&iter, &value_iter);
511
511
                dbus_message_iter_get_basic(&value_iter, &discovering);
536
536
 
537
537
        conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);
538
538
        if (conn == NULL)
539
 
                return FALSE;
 
539
                return TRUE;
540
540
 
541
541
        dbus_error_init(&error);
542
542
        hcid_exists = dbus_bus_name_has_owner(conn, "org.bluez", &error);
543
 
        if (&error != NULL && dbus_error_is_set(&error))
544
 
                return FALSE;
 
543
        if (dbus_error_is_set(&error))
 
544
                return TRUE;
545
545
 
546
546
        if (!hcid_exists)
547
 
                return FALSE;
 
547
                return TRUE;
548
548
 
549
549
        /* Get the default adapter */
550
550
        message = dbus_message_new_method_call("org.bluez", "/",
560
560
 
561
561
        dbus_message_unref(message);
562
562
 
563
 
        if (&error != NULL && dbus_error_is_set(&error)) {
 
563
        if (dbus_error_is_set(&error)) {
564
564
                dbus_connection_unref(conn);
565
 
                return FALSE;
 
565
                /* No adapter */
 
566
                return TRUE;
566
567
        }
567
568
 
568
569
        dbus_message_iter_init(reply, &reply_iter);
569
 
        if (dbus_message_iter_get_arg_type(&reply_iter) != DBUS_TYPE_OBJECT_PATH) {
 
570
        if (dbus_message_iter_get_arg_type(&reply_iter) !=
 
571
                                                DBUS_TYPE_OBJECT_PATH) {
570
572
                dbus_message_unref(reply);
571
573
                dbus_connection_unref(conn);
572
574
                return FALSE;
611
613
        loop = g_main_loop_new(NULL, TRUE);
612
614
        g_main_loop_run(loop);
613
615
 
 
616
        g_free(adapter);
614
617
        dbus_connection_unref(conn);
615
618
 
616
619
        return TRUE;
617
620
}
618
621
 
 
622
static gboolean print_ieee1284(const char *bdaddr)
 
623
{
 
624
        DBusMessage *message, *reply, *adapter_reply;
 
625
        DBusMessageIter iter;
 
626
        char *object_path = NULL;
 
627
        char *adapter;
 
628
        char *id;
 
629
 
 
630
        adapter_reply = NULL;
 
631
 
 
632
        conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);
 
633
        if (conn == NULL)
 
634
                return FALSE;
 
635
 
 
636
        message = dbus_message_new_method_call("org.bluez", "/",
 
637
                        "org.bluez.Manager",
 
638
                        "DefaultAdapter");
 
639
 
 
640
        adapter_reply = dbus_connection_send_with_reply_and_block(conn,
 
641
                        message, -1, NULL);
 
642
 
 
643
        dbus_message_unref(message);
 
644
 
 
645
        if (dbus_message_get_args(adapter_reply, NULL,
 
646
                        DBUS_TYPE_OBJECT_PATH, &adapter,
 
647
                        DBUS_TYPE_INVALID) == FALSE)
 
648
                return FALSE;
 
649
 
 
650
        message = dbus_message_new_method_call("org.bluez", adapter,
 
651
                        "org.bluez.Adapter",
 
652
                        "FindDevice");
 
653
        dbus_message_iter_init_append(message, &iter);
 
654
        dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &bdaddr);
 
655
 
 
656
        if (adapter_reply != NULL)
 
657
                dbus_message_unref(adapter_reply);
 
658
 
 
659
        reply = dbus_connection_send_with_reply_and_block(conn,
 
660
                        message, -1, NULL);
 
661
 
 
662
        dbus_message_unref(message);
 
663
 
 
664
        if (!reply) {
 
665
                message = dbus_message_new_method_call("org.bluez", adapter,
 
666
                                "org.bluez.Adapter",
 
667
                                "CreateDevice");
 
668
                dbus_message_iter_init_append(message, &iter);
 
669
                dbus_message_iter_append_basic(&iter,
 
670
                                DBUS_TYPE_STRING, &bdaddr);
 
671
 
 
672
                reply = dbus_connection_send_with_reply_and_block(conn,
 
673
                                message, -1, NULL);
 
674
 
 
675
                dbus_message_unref(message);
 
676
 
 
677
                if (!reply)
 
678
                        return FALSE;
 
679
        }
 
680
 
 
681
        if (dbus_message_get_args(reply, NULL,
 
682
                                        DBUS_TYPE_OBJECT_PATH, &object_path,
 
683
                                        DBUS_TYPE_INVALID) == FALSE)
 
684
                return FALSE;
 
685
 
 
686
        id = device_get_ieee1284_id(adapter, object_path);
 
687
        if (id == NULL)
 
688
                return FALSE;
 
689
        printf("%s", id);
 
690
        g_free(id);
 
691
 
 
692
        return TRUE;
 
693
}
 
694
 
619
695
/*
620
696
 *  Usage: printer-uri job-id user title copies options [file]
621
697
 *
634
710
        /* Make sure status messages are not buffered */
635
711
        setbuf(stderr, NULL);
636
712
 
 
713
        /* Make sure output is not buffered */
 
714
        setbuf(stdout, NULL);
 
715
 
637
716
        /* Ignore SIGPIPE signals */
638
717
#ifdef HAVE_SIGSET
639
718
        sigset(SIGPIPE, SIG_IGN);
650
729
                        return CUPS_BACKEND_OK;
651
730
                else
652
731
                        return CUPS_BACKEND_FAILED;
 
732
        } else if (argc == 3 && strcmp(argv[1], "--get-deviceid") == 0) {
 
733
                if (bachk(argv[2]) < 0) {
 
734
                        fprintf(stderr, "Invalid Bluetooth address '%s'\n",
 
735
                                        argv[2]);
 
736
                        return CUPS_BACKEND_FAILED;
 
737
                }
 
738
                if (print_ieee1284(argv[2]) == FALSE)
 
739
                        return CUPS_BACKEND_FAILED;
 
740
                return CUPS_BACKEND_OK;
653
741
        }
654
742
 
655
743
        if (argc < 6 || argc > 7) {
656
 
                fprintf(stderr, "Usage: bluetooth job-id user title copies options [file]\n");
 
744
                fprintf(stderr, "Usage: bluetooth job-id user title copies"
 
745
                                " options [file]\n");
 
746
                fprintf(stderr, "       bluetooth --get-deviceid [bdaddr]\n");
657
747
                return CUPS_BACKEND_FAILED;
658
748
        }
659
749
 
684
774
                ptr += 2;
685
775
        }
686
776
        sprintf(device, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
687
 
                b[0], b[1], b[2], b[3], b[4], b[5]);
 
777
                        b[0], b[1], b[2], b[3], b[4], b[5]);
688
778
 
689
779
        str2ba(device, &bdaddr);
690
780
 
705
795
 
706
796
        cups_class = getenv("CLASS");
707
797
 
708
 
        fprintf(stderr, "DEBUG: %s device %s service %s fd %d copies %d class %s\n",
 
798
        fprintf(stderr,
 
799
                "DEBUG: %s device %s service %s fd %d copies %d class %s\n",
709
800
                        argv[0], device, service, fd, copies,
710
 
                                        cups_class ? cups_class : "(none)");
 
801
                        cups_class ? cups_class : "(none)");
711
802
 
712
803
        fputs("STATE: +connecting-to-device\n", stderr);
713
804