59 | | |
---|
60 | | // prototypes & stuff for listing the OSC space |
---|
61 | | void list_osc_paths(lo_server s, lo_address t, const char *path); |
---|
62 | | void list_osc_params(lo_server s, lo_address t, const char *path); |
---|
63 | | string osc_param_get_value(lo_server s, lo_address t, const char *path, const char *param); |
---|
64 | | |
---|
65 | | vector<string> osc_paths; |
---|
66 | | vector<string> osc_params; |
---|
67 | | string osc_value; |
---|
68 | | |
---|
69 | | int osc_path_response_handler(const char *path, const char *types, lo_arg **argv, int argc, |
---|
70 | | void *data, void *user_data); |
---|
71 | | |
---|
72 | | int osc_param_response_handler(const char *path, const char *types, lo_arg **argv, int argc, |
---|
73 | | void *data, void *user_data); |
---|
74 | | |
---|
75 | | int osc_data_response_handler(const char *path, const char *types, lo_arg **argv, int argc, |
---|
76 | | void *data, void *user_data); |
---|
77 | | |
---|
78 | | void osc_error_handler(int num, const char *msg, const char *path); |
---|
79 | | |
---|
80 | | // DBUS stuff |
---|
81 | | DBus::BusDispatcher dispatcher; |
---|
82 | | |
---|
83 | | // signal handler |
---|
84 | | int run=1; |
---|
85 | | static void sighandler (int sig) |
---|
86 | | { |
---|
87 | | run = 0; |
---|
88 | | dispatcher.leave(); |
---|
89 | | } |
---|
386 | | } else if ( strcmp( arguments.args[0], "ListOscSpace" ) == 0 ) { |
---|
387 | | // list osc space by using OSC messages |
---|
388 | | // a server is assumed to be present |
---|
389 | | |
---|
390 | | /* start a new server. |
---|
391 | | when sending a message from this context, the response |
---|
392 | | address will be set to this server's address. |
---|
393 | | */ |
---|
394 | | lo_server s = lo_server_new(NULL, osc_error_handler); |
---|
395 | | lo_address t = lo_address_new(NULL, "17820"); |
---|
396 | | |
---|
397 | | list_osc_paths(s, t, "/"); |
---|
398 | | |
---|
399 | | lo_address_free(t); |
---|
400 | | lo_server_free(s); |
---|
401 | | |
---|
402 | | } else if ( strcmp( arguments.args[0], "OscServer" ) == 0 ) { |
---|
403 | | DeviceManager *m_deviceManager = new DeviceManager(); |
---|
404 | | if ( !m_deviceManager ) { |
---|
405 | | fprintf( stderr, "Could not allocate device manager\n" ); |
---|
406 | | return exitfunction(-1); |
---|
407 | | } |
---|
408 | | if ( !m_deviceManager->initialize( arguments.port ) ) { |
---|
409 | | fprintf( stderr, "Could not initialize device manager\n" ); |
---|
410 | | delete m_deviceManager; |
---|
411 | | return exitfunction(-1); |
---|
412 | | } |
---|
413 | | if ( arguments.verbose ) { |
---|
414 | | m_deviceManager->setVerboseLevel(arguments.verbose); |
---|
415 | | } |
---|
416 | | if ( !m_deviceManager->discover() ) { |
---|
417 | | fprintf( stderr, "Could not discover devices\n" ); |
---|
418 | | delete m_deviceManager; |
---|
419 | | return exitfunction(-1); |
---|
420 | | } |
---|
421 | | |
---|
422 | | printf("server started\n"); |
---|
423 | | printf("press ctrl-c to stop it & continue\n"); |
---|
424 | | |
---|
425 | | signal (SIGINT, sighandler); |
---|
426 | | |
---|
427 | | run=1; |
---|
428 | | while(run) { |
---|
429 | | sleep(1); |
---|
430 | | fflush(stdout); |
---|
431 | | fflush(stderr); |
---|
432 | | } |
---|
433 | | signal (SIGINT, SIG_DFL); |
---|
434 | | |
---|
435 | | printf("server stopped\n"); |
---|
436 | | delete m_deviceManager; |
---|
437 | | return exitfunction(0); |
---|
438 | | } else if ( strcmp( arguments.args[0], "DBus" ) == 0 ) { |
---|
439 | | DeviceManager *m_deviceManager = new DeviceManager(); |
---|
440 | | if ( !m_deviceManager ) { |
---|
441 | | fprintf( stderr, "Could not allocate device manager\n" ); |
---|
442 | | return exitfunction(-1); |
---|
443 | | } |
---|
444 | | if ( !m_deviceManager->initialize( arguments.port ) ) { |
---|
445 | | fprintf( stderr, "Could not initialize device manager\n" ); |
---|
446 | | delete m_deviceManager; |
---|
447 | | return exitfunction(-1); |
---|
448 | | } |
---|
449 | | if ( arguments.verbose ) { |
---|
450 | | m_deviceManager->setVerboseLevel(arguments.verbose); |
---|
451 | | } |
---|
452 | | if ( !m_deviceManager->discover() ) { |
---|
453 | | fprintf( stderr, "Could not discover devices\n" ); |
---|
454 | | delete m_deviceManager; |
---|
455 | | return exitfunction(-1); |
---|
456 | | } |
---|
457 | | |
---|
458 | | signal (SIGINT, sighandler); |
---|
459 | | |
---|
460 | | DBus::_init_threading(); |
---|
461 | | |
---|
462 | | // test DBUS stuff |
---|
463 | | DBus::default_dispatcher = &dispatcher; |
---|
464 | | |
---|
465 | | DBus::Connection conn = DBus::Connection::SessionBus(); |
---|
466 | | conn.request_name("org.ffado.Control"); |
---|
467 | | |
---|
468 | | DBusControl::Container *container |
---|
469 | | = new DBusControl::Container(conn, "/org/ffado/Control/DeviceManager", *m_deviceManager); |
---|
470 | | |
---|
471 | | printf("DBUS test service running\n"); |
---|
472 | | printf("press ctrl-c to stop it & continue\n"); |
---|
473 | | |
---|
474 | | dispatcher.enter(); |
---|
475 | | |
---|
476 | | delete container; |
---|
477 | | |
---|
478 | | signal (SIGINT, SIG_DFL); |
---|
479 | | |
---|
480 | | printf("server stopped\n"); |
---|
481 | | delete m_deviceManager; |
---|
482 | | return exitfunction(0); |
---|
483 | | } else { |
---|
484 | | printf( "unknown operation\n" ); |
---|
488 | | |
---|
489 | | void list_osc_paths(lo_server s, lo_address t, const char *path) { |
---|
490 | | vector<string> my_paths; |
---|
491 | | |
---|
492 | | printf("listing path: %s\n", path); |
---|
493 | | |
---|
494 | | osc_paths.clear(); |
---|
495 | | lo_server_add_method(s, "/response", NULL, osc_path_response_handler, NULL); |
---|
496 | | |
---|
497 | | if (lo_send(t, path, "s", "list") == -1) { |
---|
498 | | printf(" OSC error %d: %s\n", lo_address_errno(t), lo_address_errstr(t)); |
---|
499 | | } |
---|
500 | | |
---|
501 | | if (lo_server_recv_noblock(s, 1000) == 0) { |
---|
502 | | printf("timeout\n"); |
---|
503 | | return; |
---|
504 | | } |
---|
505 | | |
---|
506 | | lo_server_del_method(s, "/response", NULL); |
---|
507 | | |
---|
508 | | list_osc_params(s, t, path); |
---|
509 | | |
---|
510 | | my_paths=osc_paths; |
---|
511 | | for ( vector<string>::iterator it = my_paths.begin(); |
---|
512 | | it != my_paths.end(); |
---|
513 | | ++it ) |
---|
514 | | { |
---|
515 | | string new_path=string(path) + *it; |
---|
516 | | new_path += string("/"); |
---|
517 | | list_osc_paths(s, t, new_path.c_str()); |
---|
518 | | } |
---|
519 | | |
---|
520 | | } |
---|
521 | | |
---|
522 | | void list_osc_params(lo_server s, lo_address t, const char *path) { |
---|
523 | | vector<string> my_paths; |
---|
524 | | printf("params for: %s\n", path); |
---|
525 | | |
---|
526 | | osc_params.clear(); |
---|
527 | | lo_server_add_method(s, "/response", NULL, osc_param_response_handler, NULL); |
---|
528 | | |
---|
529 | | if (lo_send(t, path, "s", "params") == -1) { |
---|
530 | | printf(" OSC error %d: %s\n", lo_address_errno(t), lo_address_errstr(t)); |
---|
531 | | } |
---|
532 | | |
---|
533 | | if (lo_server_recv_noblock(s, 1000) == 0) { |
---|
534 | | printf("timeout\n"); |
---|
535 | | return; |
---|
536 | | } |
---|
537 | | |
---|
538 | | lo_server_del_method(s, "/response", NULL); |
---|
539 | | |
---|
540 | | vector<string> my_params=osc_params; |
---|
541 | | |
---|
542 | | for ( vector<string>::iterator it = my_params.begin(); |
---|
543 | | it != my_params.end(); |
---|
544 | | ++it ) |
---|
545 | | { |
---|
546 | | string value=osc_param_get_value(s, t, path, (*it).c_str()); |
---|
547 | | printf(" %20s = %s\n", (*it).c_str(), value.c_str()); |
---|
548 | | } |
---|
549 | | |
---|
550 | | } |
---|
551 | | |
---|
552 | | string osc_param_get_value(lo_server s, lo_address t, const char *path, const char *param) { |
---|
553 | | lo_server_add_method(s, "/response", NULL, osc_data_response_handler, NULL); |
---|
554 | | |
---|
555 | | if (lo_send(t, path, "ss", "get", param) == -1) { |
---|
556 | | printf(" OSC error %d: %s\n", lo_address_errno(t), lo_address_errstr(t)); |
---|
557 | | } |
---|
558 | | |
---|
559 | | if (lo_server_recv_noblock(s, 1000) == 0) { |
---|
560 | | return string("timeout"); |
---|
561 | | } |
---|
562 | | |
---|
563 | | lo_server_del_method(s, "/response", NULL); |
---|
564 | | return osc_value; |
---|
565 | | } |
---|
566 | | |
---|
567 | | void osc_error_handler(int num, const char *msg, const char *path) |
---|
568 | | { |
---|
569 | | printf("liblo server error %d in path %s: %s\n", num, path, msg); |
---|
570 | | } |
---|
571 | | |
---|
572 | | int osc_path_response_handler(const char *path, const char *types, lo_arg **argv, int argc, |
---|
573 | | void *data, void *user_data) |
---|
574 | | { |
---|
575 | | for (int i=0; i< argc;i++) { |
---|
576 | | switch (lo_type(types[i])) { |
---|
577 | | /** Standard C, NULL terminated string. */ |
---|
578 | | case LO_STRING: |
---|
579 | | osc_paths.push_back(string(&(argv[i]->s))); |
---|
580 | | break; |
---|
581 | | default: |
---|
582 | | printf("unexpected data type in response message\n"); |
---|
583 | | } |
---|
584 | | } |
---|
585 | | return 1; |
---|
586 | | } |
---|
587 | | |
---|
588 | | int osc_param_response_handler(const char *path, const char *types, lo_arg **argv, int argc, |
---|
589 | | void *data, void *user_data) |
---|
590 | | { |
---|
591 | | for (int i=0; i< argc;i++) { |
---|
592 | | switch (lo_type(types[i])) { |
---|
593 | | /** Standard C, NULL terminated string. */ |
---|
594 | | case LO_STRING: |
---|
595 | | osc_params.push_back(string(&(argv[i]->s))); |
---|
596 | | break; |
---|
597 | | default: |
---|
598 | | printf("unexpected data type in response message\n"); |
---|
599 | | } |
---|
600 | | } |
---|
601 | | return 1; |
---|
602 | | } |
---|
603 | | |
---|
604 | | int osc_data_response_handler(const char *path, const char *types, lo_arg **argv, int argc, |
---|
605 | | void *data, void *user_data) |
---|
606 | | { |
---|
607 | | std::ostringstream str; |
---|
608 | | |
---|
609 | | if(argc==1) { |
---|
610 | | switch (lo_type(types[0])) { |
---|
611 | | /* basic OSC types */ |
---|
612 | | /** 32 bit signed integer. */ |
---|
613 | | case LO_INT32: |
---|
614 | | str << "0x" << std::hex << argv[0]->i; |
---|
615 | | osc_value=str.str(); |
---|
616 | | break; |
---|
617 | | case LO_INT64: |
---|
618 | | str << "0x" << std::hex << argv[0]->h; |
---|
619 | | osc_value=str.str(); |
---|
620 | | break; |
---|
621 | | /** 32 bit IEEE-754 float. */ |
---|
622 | | case LO_FLOAT: |
---|
623 | | str << argv[0]->f; |
---|
624 | | osc_value=str.str(); |
---|
625 | | break; |
---|
626 | | /** Standard C, NULL terminated string. */ |
---|
627 | | case LO_STRING: |
---|
628 | | osc_value=string(&argv[0]->s); |
---|
629 | | break; |
---|
630 | | default: |
---|
631 | | osc_value="unsupported response datatype"; |
---|
632 | | } |
---|
633 | | } else { |
---|
634 | | osc_value="invalid response"; |
---|
635 | | } |
---|
636 | | return 1; |
---|
637 | | } |
---|