hfp.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <unistd.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/socket.h>
  20. #include <sys/un.h>
  21. #include "src/shared/mainloop.h"
  22. #include "src/shared/hfp.h"
  23. static void hfp_debug(const char *str, void *user_data)
  24. {
  25. const char *prefix = user_data;
  26. printf("%s%s\n", prefix, str);
  27. }
  28. static void command_handler(const char *command, void *user_data)
  29. {
  30. struct hfp_gw *hfp = user_data;
  31. printf("Command: %s\n", command);
  32. hfp_gw_send_result(hfp, HFP_RESULT_ERROR);
  33. }
  34. static bool open_connection(void)
  35. {
  36. static const char SOCKET_PATH[] = "\0hfp-headset";
  37. struct hfp_gw *hfp;
  38. struct sockaddr_un addr;
  39. int fd;
  40. fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
  41. if (fd < 0) {
  42. perror("Failed to create Unix socket");
  43. return false;
  44. }
  45. memset(&addr, 0, sizeof(addr));
  46. addr.sun_family = AF_UNIX;
  47. memcpy(addr.sun_path, SOCKET_PATH, sizeof(SOCKET_PATH));
  48. if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  49. perror("Failed to connect Unix socket");
  50. close(fd);
  51. return false;
  52. }
  53. hfp = hfp_gw_new(fd);
  54. if (!hfp) {
  55. close(fd);
  56. return false;
  57. }
  58. hfp_gw_set_close_on_unref(hfp, true);
  59. hfp_gw_set_debug(hfp, hfp_debug, "HFP: ", NULL);
  60. hfp_gw_set_command_handler(hfp, command_handler, hfp, NULL);
  61. return true;
  62. }
  63. static void signal_callback(int signum, void *user_data)
  64. {
  65. switch (signum) {
  66. case SIGINT:
  67. case SIGTERM:
  68. mainloop_quit();
  69. break;
  70. }
  71. }
  72. int main(int argc, char *argv[])
  73. {
  74. mainloop_init();
  75. if (!open_connection())
  76. return EXIT_FAILURE;
  77. return mainloop_run_with_signal(signal_callback, NULL);
  78. }