[go: up one dir, main page]

Menu

[r91]: / trunk / dbus.c  Maximize  Restore  History

Download this file

785 lines (688 with data), 22.1 kB

  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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/*
* $Id$
*
* scanbd - KMUX scanner button daemon
*
* Copyright (C) 2008 - 2012 Wilhelm Meier (wilhelm.meier@fh-kl.de)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "scanbd.h"
#include "scanbd_dbus.h"
#include "scanbuttond_wrapper.h"
static DBusConnection* conn = NULL;
static pthread_t dbus_tid = 0;
#ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
// this is non-portable
static pthread_mutex_t dbus_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
#else
static pthread_mutex_t dbus_mutex;
#endif
void dbus_send_signal_argv(const char* signal_name, char** argv) {
// TODO: we need a better dbus mainloop integrations, so that we
// can wakeup the main thread to sendout the messages we generate here
DBusMessage* signal = NULL;
if (conn == NULL) {
if (!dbus_init()) {
return;
}
}
assert(conn);
if (!conn) {
slog(SLOG_DEBUG, "No dbus connection");
return;
}
assert(signal_name != NULL);
if ((signal = dbus_message_new_signal(SCANBD_DBUS_OBJECTPATH,
SCANBD_DBUS_INTERFACE,
signal_name)) == NULL) {
slog(SLOG_ERROR, "Can't create signal");
return;
}
if (argv != NULL) {
DBusMessageIter args;
DBusMessageIter sub;
dbus_message_iter_init_append(signal, &args);
if (dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY,
DBUS_TYPE_STRING_AS_STRING,
&sub) != TRUE) {
slog(SLOG_ERROR, "Can't initialize dbus container");
}
while(*argv != NULL) {
slog(SLOG_DEBUG, "append string %s to signal %s", *argv, signal_name);
if (dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, argv) != TRUE) {
slog(SLOG_ERROR, "Can't append signal argument");
}
argv++;
}
if (dbus_message_iter_close_container(&args, &sub) != TRUE) {
slog(SLOG_ERROR, "Can't close dbus container");
}
}
slog(SLOG_DEBUG, "now sending signal %s", signal_name);
dbus_uint32_t serial;
if (dbus_connection_send(conn, signal, &serial) != TRUE) {
slog(SLOG_ERROR, "Can't send signal");
}
slog(SLOG_DEBUG, "now flushing the dbus");
// TODO: with the standard-main loop this would block without
// using a timeout
dbus_connection_flush(conn);
slog(SLOG_DEBUG, "unref the signal");
dbus_message_unref(signal);
}
void dbus_send_signal(const char* signal_name, const char* arg) {
DBusMessage* signal = NULL;
if (conn == NULL) {
if (!dbus_init()) {
return;
}
}
assert(conn);
if (!conn) {
slog(SLOG_DEBUG, "No dbus connection");
return;
}
assert(signal_name != NULL);
if ((signal = dbus_message_new_signal(SCANBD_DBUS_OBJECTPATH,
SCANBD_DBUS_INTERFACE,
signal_name)) == NULL) {
slog(SLOG_ERROR, "Can't create signal");
return;
}
if (arg != NULL) {
DBusMessageIter args;
dbus_message_iter_init_append(signal, &args);
slog(SLOG_DEBUG, "append string %s to signal %s", arg, signal_name);
if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &arg) != TRUE) {
slog(SLOG_ERROR, "Can't append signal argument");
}
}
dbus_uint32_t serial;
slog(SLOG_DEBUG, "now sending signal %s", signal_name);
if (dbus_connection_send(conn, signal, &serial) != TRUE) {
slog(SLOG_ERROR, "Can't send signal");
}
dbus_connection_flush(conn);
dbus_message_unref(signal);
}
#ifdef USE_HAL
static void hal_device_added(LibHalContext* ctx, const char *udi) {
(void)ctx;
slog(SLOG_DEBUG, "New Device: %s", udi);
DBusError dbus_error;
dbus_error_init(&dbus_error);
if (libhal_device_query_capability(ctx, udi,
HAL_SCANNER_CAPABILITY, &dbus_error) == TRUE) {
// found new scanner
slog(SLOG_INFO, "New Scanner: %s", udi);
#ifdef USE_SANE
stop_sane_threads();
#else
stop_scbtn_threads();
#endif
slog(SLOG_DEBUG, "sane_exit");
#ifdef USE_SANE
sane_exit();
#else
scbtn_shutdown();
#endif
#ifdef SANE_REINIT_TIMEOUT
sleep(SANE_REINIT_TIMEOUT); // TODO: don't know if this is
// really neccessary
#endif
slog(SLOG_DEBUG, "sane_init");
#ifdef USE_SANE
sane_init(NULL, NULL);
get_sane_devices();
start_sane_threads();
#else
if (scanbtnd_init() < 0) {
slog(SLOG_INFO, "Could not initialize scanbuttond modules!\n");
exit(EXIT_FAILURE);
}
assert(backend);
get_scbtn_devices();
start_scbtn_threads();
#endif
}
}
#endif
#ifdef USE_HAL
static void hal_device_removed(LibHalContext* ctx, const char *udi) {
(void)ctx;
slog(SLOG_DEBUG, "Removed Device: %s", udi);
DBusError dbus_error;
dbus_error_init(&dbus_error);
// if the device is removed, the device informations isn't
// available anymore, so we unconditionally stop/start the sane threads
if (libhal_device_query_capability(ctx, udi,
HAL_SCANNER_CAPABILITY, &dbus_error) == TRUE) {
slog(SLOG_INFO, "Removed Scanner: %s", udi);
}
#ifdef USE_SANE
stop_sane_threads();
#else
stop_scbtn_threads();
#endif
slog(SLOG_DEBUG, "sane_exit");
#ifdef USE_SANE
sane_exit();
#else
scbtn_shutdown();
#endif
#ifdef SANE_REINIT_TIMEOUT
sleep(SANE_REINIT_TIMEOUT);// TODO: don't know if this is really neccessary
#endif
slog(SLOG_DEBUG, "sane_init");
#ifdef USE_SANE
sane_init(NULL, NULL);
get_sane_devices();
start_sane_threads();
#else
if (scanbtnd_init() < 0) {
slog(SLOG_INFO, "Could not initialize scanbuttond modules!\n");
exit(EXIT_FAILURE);
}
assert(backend);
get_scbtn_devices();
start_scbtn_threads();
#endif
}
#endif
// this function is used from dbus_thread and udev_thread
void dbus_signal_device_added(void) {
if (pthread_mutex_lock(&dbus_mutex)) {
slog(SLOG_ERROR, "Can't lock mutex");
}
#ifndef USE_HAL
slog(SLOG_DEBUG, "dbus_signal_device_added");
// look for new scanner
#ifdef USE_SANE
stop_sane_threads();
#else
stop_scbtn_threads();
#endif // USE_SANE
slog(SLOG_DEBUG, "sane_exit");
#ifdef USE_SANE
sane_exit();
#else
scbtn_shutdown();
#endif // USE_SANE
#ifdef SANE_REINIT_TIMEOUT
sleep(SANE_REINIT_TIMEOUT); // TODO: don't know if this is
// really neccessary
#endif
#ifdef USE_SANE
slog(SLOG_DEBUG, "sane_init");
sane_init(NULL, NULL);
get_sane_devices();
start_sane_threads();
#else
if (scanbtnd_init() < 0) {
slog(SLOG_INFO, "Could not initialize scanbuttond modules!\n");
exit(EXIT_FAILURE);
}
assert(backend);
get_scbtn_devices();
start_scbtn_threads();
#endif // USE_SANE
#endif // USE_HAL
if (pthread_mutex_unlock(&dbus_mutex)) {
slog(SLOG_ERROR, "Can't unlock mutex");
}
}
void dbus_signal_device_removed(void) {
if (pthread_mutex_lock(&dbus_mutex)) {
slog(SLOG_ERROR, "Can't lock mutex");
}
#ifndef USE_HAL
slog(SLOG_DEBUG, "dbus_signal_device_removed");
// look for removed scanner
#ifdef USE_SANE
stop_sane_threads();
#else
stop_scbtn_threads();
#endif
slog(SLOG_DEBUG, "sane_exit");
#ifdef USE_SANE
sane_exit();
#else
scbtn_shutdown();
#endif
#ifdef SANE_REINIT_TIMEOUT
sleep(SANE_REINIT_TIMEOUT); // TODO: don't know if this is
// really neccessary
#endif
slog(SLOG_DEBUG, "sane_init");
#ifdef USE_SANE
sane_init(NULL, NULL);
get_sane_devices();
start_sane_threads();
#else
if (scanbtnd_init() < 0) {
slog(SLOG_INFO, "Could not initialize scanbuttond modules!\n");
exit(EXIT_FAILURE);
}
assert(backend);
#endif
#endif
if (pthread_mutex_unlock(&dbus_mutex)) {
slog(SLOG_ERROR, "Can't unlock mutex");
}
}
// is called when saned exited
static void dbus_method_release(void) {
slog(SLOG_DEBUG, "dbus_method_release");
// start all threads
#ifdef USE_SANE
start_sane_threads();
#else
start_scbtn_threads();
#endif
}
// is called before saned started
static void dbus_method_acquire(void) {
slog(SLOG_DEBUG, "dbus_method_acquire");
// stop all threads
#ifdef USE_SANE
stop_sane_threads();
#else
stop_scbtn_threads();
#endif
}
struct sane_trigger_arg {
void (*f)(void*);
int device;
int action;
};
typedef struct sane_trigger_arg sane_trigger_arg_t;
void* dbus_call_sane_trigger_action_thread(void* arg) {
sane_trigger_arg_t* a = (sane_trigger_arg_t*) arg;
assert(a != NULL);
#ifdef USE_SANE
sane_trigger_action(a->device, a->action);
#else
scbtn_trigger_action(a->device, a->action);
#endif
return NULL;
}
void sane_trigger_action_async(int device, int action) {
sane_trigger_arg_t* arg = calloc(1, sizeof(sane_trigger_arg_t));
assert(arg != NULL);
arg->device = device;
arg->action = action;
pthread_t tid;
if (pthread_create(&tid, NULL, dbus_call_sane_trigger_action_thread, arg) < 0) {
slog(SLOG_WARN, "pthread_create: %s", strerror(errno));
}
}
static void dbus_method_trigger(DBusMessage *message) {
DBusMessageIter args;
dbus_uint32_t device = -1;
dbus_uint32_t action = -1;
if (!dbus_message_iter_init(message, &args)) {
slog(SLOG_WARN, "trigger has no arguments");
return;
}
if (dbus_message_iter_get_arg_type(&args) == DBUS_TYPE_UINT32) {
dbus_message_iter_get_basic(&args, &device);
slog(SLOG_INFO, "trigger device %d", device);
}
else {
slog(SLOG_WARN, "trigger has wrong argument type");
return;
}
if (!dbus_message_iter_next(&args)) {
slog(SLOG_WARN, "trigger has too few arguments");
return;
}
if (dbus_message_iter_get_arg_type(&args) == DBUS_TYPE_UINT32) {
dbus_message_iter_get_basic(&args, &action);
slog(SLOG_INFO, "trigger action %d", action);
}
else {
slog(SLOG_WARN, "trigger has wrong argument type");
return;
}
sane_trigger_action_async(device, action);
}
static void unregister_func(DBusConnection* connection, void* user_data) {
(void)connection;
(void)user_data;
slog(SLOG_DEBUG, "unregister_func_called");
}
static DBusHandlerResult message_func(DBusConnection *connection, DBusMessage *message,
void *user_data) {
(void)connection;
(void)user_data;
slog(SLOG_DEBUG, "message_func");
DBusMessage* reply = NULL;
if (dbus_message_is_method_call(message,
SCANBD_DBUS_INTERFACE,
SCANBD_DBUS_METHOD_ACQUIRE)) {
dbus_method_acquire();
}
else if (dbus_message_is_method_call(message,
SCANBD_DBUS_INTERFACE,
SCANBD_DBUS_METHOD_RELEASE)) {
dbus_method_release();
}
else if (dbus_message_is_method_call(message,
SCANBD_DBUS_INTERFACE,
SCANBD_DBUS_METHOD_TRIGGER)) {
dbus_method_trigger(message);
}
else if (dbus_message_is_signal(message,
DBUS_HAL_INTERFACE,
DBUS_HAL_SIGNAL_DEV_ADDED)) {
dbus_signal_device_added();
}
else if (dbus_message_is_signal(message,
DBUS_HAL_INTERFACE,
DBUS_HAL_SIGNAL_DEV_REMOVED)) {
dbus_signal_device_removed();
}
/* If the message was handled, send back the reply */
if (reply != NULL) {
dbus_connection_send(conn, reply, NULL);
dbus_message_unref(reply);
}
return reply ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
static DBusObjectPathVTable dbus_vtable = {
unregister_func,
message_func,
NULL,
NULL,
NULL,
NULL
};
void dbus_thread_cleanup(void* arg) {
assert(arg);
slog(SLOG_DEBUG, "dbus_thread_cleanup");
(void)arg;
}
static void* dbus_thread(void* arg) {
(void)arg;
// we only expect the main thread to handle signals
sigset_t mask;
sigfillset(&mask);
pthread_sigmask(SIG_BLOCK, &mask, NULL);
cfg_t* cfg_sec_global = NULL;
cfg_sec_global = cfg_getsec(cfg, C_GLOBAL);
assert(cfg_sec_global);
int timeout = cfg_getint(cfg_sec_global, C_TIMEOUT);
if (timeout <= 0) {
timeout = C_TIMEOUT_DEF;
}
slog(SLOG_DEBUG, "timeout: %d ms", timeout);
while(dbus_connection_read_write_dispatch(conn, timeout)) {
slog(SLOG_DEBUG, "Iteration on dbus call");
usleep(timeout * 1000);
}
return NULL;
}
bool dbus_init(void) {
slog(SLOG_DEBUG, "dbus_init");
#ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
slog(SLOG_INFO, "dbus init mutex");
pthread_mutexattr_t mutexattr;
if (pthread_mutexattr_init(&mutexattr) < 0) {
slog(SLOG_ERROR, "Can't initialize mutex attr");
exit(EXIT_FAILURE);
}
if (pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE) < 0) {
slog(SLOG_ERROR, "Can't set mutex attr");
exit(EXIT_FAILURE);
}
if (pthread_mutex_init(&dbus_mutex, &mutexattr) < 0) {
slog(SLOG_ERROR, "Can't init mutex");
exit(EXIT_FAILURE);
}
#endif
if (!dbus_threads_init_default()) {
slog(SLOG_ERROR, "DBus thread initialization failure");
}
DBusError dbus_error;
dbus_error_init(&dbus_error);
if (conn == NULL) {
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
if (dbus_error_is_set(&dbus_error)) {
conn = NULL;
slog(SLOG_ERROR, "DBus connection error: %s", dbus_error.message);
dbus_error_free(&dbus_error);
return false;
}
}
else {
slog(SLOG_WARN, "dbus connection already established");
}
if (!conn) {
slog(SLOG_DEBUG, "No dbus connection");
return false;
}
assert(conn);
#ifdef USE_HAL
LibHalContext *hal_ctx = NULL;
hal_ctx = (LibHalContext*)libhal_ctx_new();
if (!hal_ctx) {
slog(SLOG_WARN, "Failed to create HAL context!");
return false;
}
libhal_ctx_set_dbus_connection(hal_ctx, conn);
libhal_ctx_set_device_added(hal_ctx, hal_device_added);
libhal_ctx_set_device_removed(hal_ctx, hal_device_removed);
if (!libhal_ctx_init(hal_ctx, &dbus_error)) {
slog(SLOG_WARN, "Couldn't initialise HAL!");
return false;
}
#else
char match[PATH_MAX];
snprintf(match, PATH_MAX, "type='signal',interface='%s'", DBUS_HAL_INTERFACE);
slog(SLOG_ERROR, "dbus match %s", match);
dbus_bus_add_match(conn, match, &dbus_error);
if (dbus_error_is_set(&dbus_error)) {
slog(SLOG_ERROR, "DBus match error: %s", dbus_error.message);
dbus_error_free(&dbus_error);
return false;
}
dbus_connection_flush(conn);
if (dbus_error_is_set(&dbus_error)) {
slog(SLOG_ERROR, "DBus match error: %s", dbus_error.message);
dbus_error_free(&dbus_error);
return false;
}
#endif
return true;
}
void dbus_start_dbus_thread(void) {
slog(SLOG_DEBUG, "start dbus thread");
if (conn == NULL) {
if (!dbus_init()) {
return;
}
}
assert(conn);
if (!conn) {
slog(SLOG_DEBUG, "No dbus connection");
return;
}
DBusError dbus_error;
dbus_error_init(&dbus_error);
if (dbus_connection_register_object_path(conn, SCANBD_DBUS_OBJECTPATH,
&dbus_vtable, NULL) == FALSE) {
slog(SLOG_ERROR, "Can't register object path: %s", SCANBD_DBUS_OBJECTPATH);
return;
}
dbus_error_init(&dbus_error);
int ret = dbus_bus_request_name(conn, SCANBD_DBUS_ADDRESS,
0, &dbus_error);
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
slog(SLOG_WARN, "Not Primary Owner (%d)", ret);
if (dbus_error_is_set(&dbus_error)) {
slog(SLOG_WARN, "Name Error (%s)", dbus_error.message);
dbus_error_free(&dbus_error);
return;
}
}
if (dbus_tid != 0) {
slog(SLOG_DEBUG, "dbus thread already running");
dbus_stop_dbus_thread();
}
if (pthread_create(&dbus_tid, NULL, dbus_thread, NULL) < 0){
slog(SLOG_ERROR, "Can't create dbus thread: %s", strerror(errno));
return;
}
return;
}
void dbus_stop_dbus_thread(void) {
slog(SLOG_DEBUG, "stop dbus thread");
if (dbus_tid == 0) {
return;
}
if (pthread_cancel(dbus_tid) < 0) {
if (errno == ESRCH) {
slog(SLOG_WARN, "dbus thread was already cancelled");
}
else {
slog(SLOG_WARN, "unknown error from pthread_cancel: %s", strerror(errno));
}
}
slog(SLOG_DEBUG, "join dbus thread");
if (pthread_join(dbus_tid, NULL) < 0) {
slog(SLOG_ERROR, "pthread_join: %s", strerror(errno));
}
dbus_tid = 0;
}
void dbus_call_method(const char* method, const char* value) {
slog(SLOG_DEBUG, "dbus_call_method");
if (conn == NULL) {
if (!dbus_init()) {
return;
}
}
assert(conn);
if (!conn) {
slog(SLOG_DEBUG, "No dbus connection");
return;
}
DBusMessage* msg = NULL;
if ((msg = dbus_message_new_method_call(SCANBD_DBUS_ADDRESS,
SCANBD_DBUS_OBJECTPATH,
SCANBD_DBUS_INTERFACE,
method)) == NULL) {
slog(SLOG_ERROR, "Can't compose message");
return;
}
assert(msg);
if (value != NULL) {
DBusMessageIter args;
dbus_message_iter_init_append(msg, &args);
if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, value) != TRUE) {
slog(SLOG_ERROR, "Can't compose message");
return;
}
}
DBusPendingCall* pending = NULL;
// send message and get a handle for a reply
if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) {
slog(SLOG_WARN, "Can't send message");
return;
}
if (NULL == pending) {
slog(SLOG_ERROR, "Disconnected from bus");
return;
}
dbus_connection_flush(conn);
assert(msg);
dbus_message_unref(msg);
slog(SLOG_DEBUG, "waiting for reply");
assert(pending);
dbus_pending_call_block(pending);
// get the reply message
DBusMessage* reply = NULL;
if ((reply = dbus_pending_call_steal_reply(pending)) == NULL) {
slog(SLOG_DEBUG, "Reply Null\n");
return;
}
dbus_pending_call_unref(pending);
DBusMessageIter args;
if (!dbus_message_iter_init(reply, &args)) {
slog(SLOG_INFO, "Reply has no arguments");
}
return;
}
void dbus_call_trigger(unsigned int device, unsigned int action) {
slog(SLOG_DEBUG, "dbus_call_trigger for dev %d, action %d", device, action);
if (conn == NULL) {
if (!dbus_init()) {
return;
}
}
assert(conn);
if (!conn) {
slog(SLOG_DEBUG, "No dbus connection");
return;
}
DBusMessage* msg = NULL;
if ((msg = dbus_message_new_method_call(SCANBD_DBUS_ADDRESS,
SCANBD_DBUS_OBJECTPATH,
SCANBD_DBUS_INTERFACE,
SCANBD_DBUS_METHOD_TRIGGER)) == NULL) {
slog(SLOG_ERROR, "Can't compose message");
return;
}
assert(msg);
dbus_uint32_t dev = device;
dbus_uint32_t act = action;
DBusMessageIter args;
dbus_message_iter_init_append(msg, &args);
if (dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &dev) != TRUE) {
slog(SLOG_ERROR, "Can't compose message");
return;
}
if (dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &act) != TRUE) {
slog(SLOG_ERROR, "Can't compose message");
return;
}
DBusPendingCall* pending = NULL;
// send message and get a handle for a reply
if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) {
slog(SLOG_WARN, "Can't send message");
return;
}
if (NULL == pending) {
slog(SLOG_ERROR, "Disconnected from bus");
return;
}
dbus_connection_flush(conn);
assert(msg);
dbus_message_unref(msg);
slog(SLOG_DEBUG, "waiting for reply");
assert(pending);
dbus_pending_call_block(pending);
// get the reply message
DBusMessage* reply = NULL;
if ((reply = dbus_pending_call_steal_reply(pending)) == NULL) {
slog(SLOG_DEBUG, "Reply Null\n");
return;
}
dbus_pending_call_unref(pending);
if (!dbus_message_iter_init(reply, &args)) {
slog(SLOG_INFO, "Reply has no arguments");
}
return;
}