btconfig.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2012 Intel Corporation
  7. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <getopt.h>
  17. #include <stdbool.h>
  18. #include "src/shared/mainloop.h"
  19. #include "src/shared/util.h"
  20. #include "src/shared/mgmt.h"
  21. static struct mgmt *mgmt = NULL;
  22. static void mgmt_debug(const char *str, void *user_data)
  23. {
  24. const char *prefix = user_data;
  25. printf("%s%s\n", prefix, str);
  26. }
  27. static void signal_callback(int signum, void *user_data)
  28. {
  29. static bool terminated = false;
  30. switch (signum) {
  31. case SIGINT:
  32. case SIGTERM:
  33. if (!terminated) {
  34. mainloop_quit();
  35. terminated = true;
  36. }
  37. break;
  38. }
  39. }
  40. static void usage(void)
  41. {
  42. printf("btconfig - Bluetooth configuration utility\n"
  43. "Usage:\n");
  44. printf("\tbtconfig [options]\n");
  45. printf("options:\n"
  46. "\t-h, --help Show help options\n");
  47. }
  48. static const struct option main_options[] = {
  49. { "version", no_argument, NULL, 'v' },
  50. { "help", no_argument, NULL, 'h' },
  51. { }
  52. };
  53. int main(int argc, char *argv[])
  54. {
  55. int exit_status;
  56. for (;;) {
  57. int opt;
  58. opt = getopt_long(argc, argv, "vh",
  59. main_options, NULL);
  60. if (opt < 0)
  61. break;
  62. switch (opt) {
  63. case 'v':
  64. printf("%s\n", VERSION);
  65. return EXIT_SUCCESS;
  66. case 'h':
  67. usage();
  68. return EXIT_SUCCESS;
  69. default:
  70. return EXIT_FAILURE;
  71. }
  72. }
  73. if (argc - optind > 0) {
  74. fprintf(stderr, "Invalid command line parameters\n");
  75. return EXIT_FAILURE;
  76. }
  77. mainloop_init();
  78. mgmt = mgmt_new_default();
  79. if (!mgmt) {
  80. fprintf(stderr, "Unable to open mgmt_socket\n");
  81. return EXIT_FAILURE;
  82. }
  83. if (getenv("MGMT_DEBUG"))
  84. mgmt_set_debug(mgmt, mgmt_debug, "mgmt: ", NULL);
  85. exit_status = mainloop_run_with_signal(signal_callback, NULL);
  86. mgmt_cancel_all(mgmt);
  87. mgmt_unregister_all(mgmt);
  88. mgmt_unref(mgmt);
  89. return exit_status;
  90. }