utils.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011 Nokia Corporation
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdlib.h>
  14. #include <glib.h>
  15. #include "lib/bluetooth.h"
  16. #include "lib/hci.h"
  17. #include "lib/hci_lib.h"
  18. #include "lib/sdp.h"
  19. #include "lib/uuid.h"
  20. #include "btio/btio.h"
  21. #include "att.h"
  22. #include "gattrib.h"
  23. #include "gatt.h"
  24. #include "gatttool.h"
  25. GIOChannel *gatt_connect(const char *src, const char *dst,
  26. const char *dst_type, const char *sec_level,
  27. int psm, int mtu, BtIOConnect connect_cb,
  28. GError **gerr)
  29. {
  30. GIOChannel *chan;
  31. bdaddr_t sba, dba;
  32. uint8_t dest_type;
  33. GError *tmp_err = NULL;
  34. BtIOSecLevel sec;
  35. str2ba(dst, &dba);
  36. /* Local adapter */
  37. if (src != NULL) {
  38. if (!strncmp(src, "hci", 3))
  39. hci_devba(atoi(src + 3), &sba);
  40. else
  41. str2ba(src, &sba);
  42. } else
  43. bacpy(&sba, BDADDR_ANY);
  44. /* Not used for BR/EDR */
  45. if (strcmp(dst_type, "random") == 0)
  46. dest_type = BDADDR_LE_RANDOM;
  47. else
  48. dest_type = BDADDR_LE_PUBLIC;
  49. if (strcmp(sec_level, "medium") == 0)
  50. sec = BT_IO_SEC_MEDIUM;
  51. else if (strcmp(sec_level, "high") == 0)
  52. sec = BT_IO_SEC_HIGH;
  53. else
  54. sec = BT_IO_SEC_LOW;
  55. if (psm == 0)
  56. chan = bt_io_connect(connect_cb, NULL, NULL, &tmp_err,
  57. BT_IO_OPT_SOURCE_BDADDR, &sba,
  58. BT_IO_OPT_SOURCE_TYPE, BDADDR_LE_PUBLIC,
  59. BT_IO_OPT_DEST_BDADDR, &dba,
  60. BT_IO_OPT_DEST_TYPE, dest_type,
  61. BT_IO_OPT_CID, ATT_CID,
  62. BT_IO_OPT_SEC_LEVEL, sec,
  63. BT_IO_OPT_INVALID);
  64. else
  65. chan = bt_io_connect(connect_cb, NULL, NULL, &tmp_err,
  66. BT_IO_OPT_SOURCE_BDADDR, &sba,
  67. BT_IO_OPT_DEST_BDADDR, &dba,
  68. BT_IO_OPT_PSM, psm,
  69. BT_IO_OPT_IMTU, mtu,
  70. BT_IO_OPT_SEC_LEVEL, sec,
  71. BT_IO_OPT_INVALID);
  72. if (tmp_err) {
  73. g_propagate_error(gerr, tmp_err);
  74. return NULL;
  75. }
  76. return chan;
  77. }
  78. size_t gatt_attr_data_from_string(const char *str, uint8_t **data)
  79. {
  80. char tmp[3];
  81. size_t size, i;
  82. size = strlen(str) / 2;
  83. *data = g_try_malloc0(size);
  84. if (*data == NULL)
  85. return 0;
  86. tmp[2] = '\0';
  87. for (i = 0; i < size; i++) {
  88. memcpy(tmp, str + (i * 2), 2);
  89. (*data)[i] = (uint8_t) strtol(tmp, NULL, 16);
  90. }
  91. return size;
  92. }