deviceinfo.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2012 Texas Instruments, Inc.
  7. * Copyright (C) 2015 Google Inc.
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdbool.h>
  14. #include <errno.h>
  15. #include <glib.h>
  16. #include "lib/bluetooth.h"
  17. #include "lib/sdp.h"
  18. #include "lib/uuid.h"
  19. #include "src/plugin.h"
  20. #include "src/adapter.h"
  21. #include "src/device.h"
  22. #include "src/profile.h"
  23. #include "src/service.h"
  24. #include "attrib/gattrib.h"
  25. #include "src/shared/util.h"
  26. #include "src/shared/queue.h"
  27. #include "src/shared/gatt-db.h"
  28. #include "src/shared/gatt-client.h"
  29. #include "attrib/att.h"
  30. #include "src/log.h"
  31. #define PNP_ID_SIZE 7
  32. static void read_pnpid_cb(bool success, uint8_t att_ecode, const uint8_t *value,
  33. uint16_t length, void *user_data)
  34. {
  35. struct btd_device *device = user_data;
  36. if (!success) {
  37. error("Error reading PNP_ID value: %s",
  38. att_ecode2str(att_ecode));
  39. return;
  40. }
  41. if (length < PNP_ID_SIZE) {
  42. error("Error reading PNP_ID: Invalid pdu length received");
  43. return;
  44. }
  45. btd_device_set_pnpid(device, value[0], get_le16(&value[1]),
  46. get_le16(&value[3]), get_le16(&value[5]));
  47. }
  48. static void handle_pnpid(struct btd_device *device, uint16_t value_handle)
  49. {
  50. struct bt_gatt_client *client = btd_device_get_gatt_client(device);
  51. if (!bt_gatt_client_read_value(client, value_handle,
  52. read_pnpid_cb, device, NULL))
  53. DBG("Failed to send request to read pnpid");
  54. }
  55. static void handle_characteristic(struct gatt_db_attribute *attr,
  56. void *user_data)
  57. {
  58. struct btd_device *device = user_data;
  59. uint16_t value_handle;
  60. bt_uuid_t uuid, pnpid_uuid;
  61. bt_string_to_uuid(&pnpid_uuid, PNPID_UUID);
  62. if (!gatt_db_attribute_get_char_data(attr, NULL, &value_handle, NULL,
  63. NULL, &uuid)) {
  64. error("Failed to obtain characteristic data");
  65. return;
  66. }
  67. if (bt_uuid_cmp(&pnpid_uuid, &uuid) == 0)
  68. handle_pnpid(device, value_handle);
  69. else {
  70. char uuid_str[MAX_LEN_UUID_STR];
  71. bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
  72. DBG("Unsupported characteristic: %s", uuid_str);
  73. }
  74. }
  75. static void foreach_deviceinfo_service(struct gatt_db_attribute *attr,
  76. void *user_data)
  77. {
  78. struct btd_device *device = user_data;
  79. gatt_db_service_foreach_char(attr, handle_characteristic, device);
  80. }
  81. static int deviceinfo_probe(struct btd_service *service)
  82. {
  83. return 0;
  84. }
  85. static void deviceinfo_remove(struct btd_service *service)
  86. {
  87. }
  88. static int deviceinfo_accept(struct btd_service *service)
  89. {
  90. struct btd_device *device = btd_service_get_device(service);
  91. struct gatt_db *db = btd_device_get_gatt_db(device);
  92. char addr[18];
  93. bt_uuid_t deviceinfo_uuid;
  94. ba2str(device_get_address(device), addr);
  95. DBG("deviceinfo profile accept (%s)", addr);
  96. /* Handle the device info service */
  97. bt_string_to_uuid(&deviceinfo_uuid, DEVICE_INFORMATION_UUID);
  98. gatt_db_foreach_service(db, &deviceinfo_uuid,
  99. foreach_deviceinfo_service, device);
  100. btd_service_connecting_complete(service, 0);
  101. return 0;
  102. }
  103. static int deviceinfo_disconnect(struct btd_service *service)
  104. {
  105. btd_service_disconnecting_complete(service, 0);
  106. return 0;
  107. }
  108. static struct btd_profile deviceinfo_profile = {
  109. .name = "deviceinfo",
  110. .remote_uuid = DEVICE_INFORMATION_UUID,
  111. .external = true,
  112. .device_probe = deviceinfo_probe,
  113. .device_remove = deviceinfo_remove,
  114. .accept = deviceinfo_accept,
  115. .disconnect = deviceinfo_disconnect,
  116. };
  117. static int deviceinfo_init(void)
  118. {
  119. return btd_profile_register(&deviceinfo_profile);
  120. }
  121. static void deviceinfo_exit(void)
  122. {
  123. btd_profile_unregister(&deviceinfo_profile);
  124. }
  125. BLUETOOTH_PLUGIN_DEFINE(deviceinfo, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
  126. deviceinfo_init, deviceinfo_exit)