properties.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* SPDX-License-Identifier: LGPL-2.1-or-later */
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2013 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <sys/socket.h>
  15. #include <sys/un.h>
  16. #define PROPERTY_VALUE_MAX 32
  17. #define PROPERTY_KEY_MAX 32
  18. #define BLUETOOTH_MODE_PROPERTY_NAME "persist.sys.bluetooth.mode"
  19. #define BLUETOOTH_MODE_PROPERTY_HANDSFREE "persist.sys.bluetooth.handsfree"
  20. static inline int property_get(const char *key, char *value,
  21. const char *default_value)
  22. {
  23. const char *prop = NULL;
  24. if (!strcmp(key, BLUETOOTH_MODE_PROPERTY_NAME))
  25. prop = getenv("BLUETOOTH_MODE");
  26. if (!strcmp(key, BLUETOOTH_MODE_PROPERTY_HANDSFREE))
  27. prop = getenv("BLUETOOTH_HANDSFREE_MODE");
  28. if (!prop)
  29. prop = default_value;
  30. if (prop) {
  31. strncpy(value, prop, PROPERTY_VALUE_MAX);
  32. value[PROPERTY_VALUE_MAX - 1] = '\0';
  33. return strlen(value);
  34. }
  35. return 0;
  36. }
  37. /* property_set: returns 0 on success, < 0 on failure
  38. */
  39. static inline int property_set(const char *key, const char *value)
  40. {
  41. static const char SYSTEM_SOCKET_PATH[] = "\0android_system";
  42. struct sockaddr_un addr;
  43. char msg[256];
  44. int fd, len;
  45. fd = socket(PF_LOCAL, SOCK_DGRAM, 0);
  46. if (fd < 0)
  47. return -1;
  48. memset(&addr, 0, sizeof(addr));
  49. addr.sun_family = AF_UNIX;
  50. memcpy(addr.sun_path, SYSTEM_SOCKET_PATH, sizeof(SYSTEM_SOCKET_PATH));
  51. if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  52. close(fd);
  53. return 0;
  54. }
  55. len = snprintf(msg, sizeof(msg), "%s=%s", key, value);
  56. if (send(fd, msg, len + 1, 0) < 0) {
  57. close(fd);
  58. return -1;
  59. }
  60. close(fd);
  61. return 0;
  62. }