hostname.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <errno.h>
  14. #include <stdint.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "lib/bluetooth.h"
  18. #include "lib/sdp.h"
  19. #include "gdbus/gdbus.h"
  20. #include "src/dbus-common.h"
  21. #include "src/plugin.h"
  22. #include "src/adapter.h"
  23. #include "src/log.h"
  24. /* http://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm */
  25. #define MAJOR_CLASS_MISCELLANEOUS 0x00
  26. #define MAJOR_CLASS_COMPUTER 0x01
  27. #define MINOR_CLASS_UNCATEGORIZED 0x00
  28. #define MINOR_CLASS_DESKTOP 0x01
  29. #define MINOR_CLASS_SERVER 0x02
  30. #define MINOR_CLASS_LAPTOP 0x03
  31. #define MINOR_CLASS_HANDHELD 0x04
  32. #define MINOR_CLASS_PALM_SIZED 0x05
  33. #define MINOR_CLASS_WEARABLE 0x06
  34. #define MINOR_CLASS_TABLET 0x07
  35. static uint8_t major_class = MAJOR_CLASS_MISCELLANEOUS;
  36. static uint8_t minor_class = MINOR_CLASS_UNCATEGORIZED;
  37. static char *pretty_hostname = NULL;
  38. static char *static_hostname = NULL;
  39. /*
  40. * Fallback to static hostname only if empty pretty hostname was already
  41. * received.
  42. */
  43. static const char *get_hostname(void)
  44. {
  45. if (pretty_hostname) {
  46. if (g_str_equal(pretty_hostname, "") == FALSE)
  47. return pretty_hostname;
  48. if (static_hostname &&
  49. g_str_equal(static_hostname, "") == FALSE)
  50. return static_hostname;
  51. }
  52. return NULL;
  53. }
  54. static void update_name(struct btd_adapter *adapter, gpointer user_data)
  55. {
  56. const char *hostname = get_hostname();
  57. if (hostname == NULL)
  58. return;
  59. if (btd_adapter_is_default(adapter)) {
  60. DBG("name: %s", hostname);
  61. adapter_set_name(adapter, hostname);
  62. } else {
  63. uint16_t index = btd_adapter_get_index(adapter);
  64. char *str;
  65. /* Avoid "some device #0" names, start at #1 */
  66. str = g_strdup_printf("%s #%u", hostname, index + 1);
  67. DBG("name: %s", str);
  68. adapter_set_name(adapter, str);
  69. g_free(str);
  70. }
  71. }
  72. static void update_class(struct btd_adapter *adapter, gpointer user_data)
  73. {
  74. if (major_class == MAJOR_CLASS_MISCELLANEOUS)
  75. return;
  76. DBG("major: 0x%02x minor: 0x%02x", major_class, minor_class);
  77. btd_adapter_set_class(adapter, major_class, minor_class);
  78. }
  79. static const struct {
  80. const char *chassis;
  81. uint8_t major_class;
  82. uint8_t minor_class;
  83. } chassis_table[] = {
  84. { "desktop", MAJOR_CLASS_COMPUTER, MINOR_CLASS_DESKTOP },
  85. { "server", MAJOR_CLASS_COMPUTER, MINOR_CLASS_SERVER },
  86. { "laptop", MAJOR_CLASS_COMPUTER, MINOR_CLASS_LAPTOP },
  87. { "handset", MAJOR_CLASS_COMPUTER, MINOR_CLASS_HANDHELD },
  88. { "tablet", MAJOR_CLASS_COMPUTER, MINOR_CLASS_TABLET },
  89. { }
  90. };
  91. static void property_changed(GDBusProxy *proxy, const char *name,
  92. DBusMessageIter *iter, void *user_data)
  93. {
  94. if (g_str_equal(name, "PrettyHostname") == TRUE) {
  95. if (iter == NULL) {
  96. g_dbus_proxy_refresh_property(proxy, name);
  97. return;
  98. }
  99. if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
  100. const char *str;
  101. dbus_message_iter_get_basic(iter, &str);
  102. DBG("pretty hostname: %s", str);
  103. g_free(pretty_hostname);
  104. pretty_hostname = g_strdup(str);
  105. adapter_foreach(update_name, NULL);
  106. }
  107. } else if (g_str_equal(name, "StaticHostname") == TRUE) {
  108. if (iter == NULL) {
  109. g_dbus_proxy_refresh_property(proxy, name);
  110. return;
  111. }
  112. if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
  113. const char *str;
  114. dbus_message_iter_get_basic(iter, &str);
  115. DBG("static hostname: %s", str);
  116. g_free(static_hostname);
  117. static_hostname = g_strdup(str);
  118. adapter_foreach(update_name, NULL);
  119. }
  120. } else if (g_str_equal(name, "Chassis") == TRUE) {
  121. if (iter == NULL) {
  122. g_dbus_proxy_refresh_property(proxy, name);
  123. return;
  124. }
  125. if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
  126. const char *str;
  127. int i;
  128. dbus_message_iter_get_basic(iter, &str);
  129. DBG("chassis: %s", str);
  130. for (i = 0; chassis_table[i].chassis; i++) {
  131. if (strcmp(chassis_table[i].chassis, str))
  132. continue;
  133. major_class = chassis_table[i].major_class;
  134. minor_class = chassis_table[i].minor_class;
  135. adapter_foreach(update_class, NULL);
  136. break;
  137. }
  138. }
  139. }
  140. }
  141. static int hostname_probe(struct btd_adapter *adapter)
  142. {
  143. DBG("");
  144. update_name(adapter, NULL);
  145. update_class(adapter, NULL);
  146. return 0;
  147. }
  148. static void hostname_remove(struct btd_adapter *adapter)
  149. {
  150. DBG("");
  151. }
  152. static struct btd_adapter_driver hostname_driver = {
  153. .name = "hostname",
  154. .probe = hostname_probe,
  155. .remove = hostname_remove,
  156. };
  157. static void read_dmi_fallback(void)
  158. {
  159. char *contents;
  160. int i, type;
  161. const char *str;
  162. if (g_file_get_contents("/sys/class/dmi/id/chassis_type",
  163. &contents, NULL, NULL) == FALSE)
  164. return;
  165. type = atoi(contents);
  166. g_free(contents);
  167. if (type < 0 || type > 0x1D)
  168. return;
  169. /* from systemd hostname chassis list */
  170. switch (type) {
  171. case 0x3:
  172. case 0x4:
  173. case 0x6:
  174. case 0x7:
  175. str = "desktop";
  176. break;
  177. case 0x8:
  178. case 0x9:
  179. case 0xA:
  180. case 0xE:
  181. str = "laptop";
  182. break;
  183. case 0xB:
  184. str = "handset";
  185. break;
  186. case 0x11:
  187. case 0x1C:
  188. str = "server";
  189. break;
  190. default:
  191. return;
  192. }
  193. DBG("chassis: %s", str);
  194. for (i = 0; chassis_table[i].chassis; i++) {
  195. if (!strcmp(chassis_table[i].chassis, str)) {
  196. major_class = chassis_table[i].major_class;
  197. minor_class = chassis_table[i].minor_class;
  198. break;
  199. }
  200. }
  201. DBG("major: 0x%02x minor: 0x%02x", major_class, minor_class);
  202. }
  203. static GDBusClient *hostname_client = NULL;
  204. static GDBusProxy *hostname_proxy = NULL;
  205. static int hostname_init(void)
  206. {
  207. DBusConnection *conn = btd_get_dbus_connection();
  208. int err;
  209. read_dmi_fallback();
  210. hostname_client = g_dbus_client_new(conn, "org.freedesktop.hostname1",
  211. "/org/freedesktop/hostname1");
  212. if (!hostname_client)
  213. return -EIO;
  214. hostname_proxy = g_dbus_proxy_new(hostname_client,
  215. "/org/freedesktop/hostname1",
  216. "org.freedesktop.hostname1");
  217. if (!hostname_proxy) {
  218. g_dbus_client_unref(hostname_client);
  219. hostname_client = NULL;
  220. return -EIO;
  221. }
  222. g_dbus_proxy_set_property_watch(hostname_proxy, property_changed, NULL);
  223. err = btd_register_adapter_driver(&hostname_driver);
  224. if (err < 0) {
  225. g_dbus_proxy_unref(hostname_proxy);
  226. hostname_proxy = NULL;
  227. g_dbus_client_unref(hostname_client);
  228. hostname_client = NULL;
  229. }
  230. return err;
  231. }
  232. static void hostname_exit(void)
  233. {
  234. btd_unregister_adapter_driver(&hostname_driver);
  235. if (hostname_proxy) {
  236. g_dbus_proxy_unref(hostname_proxy);
  237. hostname_proxy = NULL;
  238. }
  239. if (hostname_client) {
  240. g_dbus_client_unref(hostname_client);
  241. hostname_client = NULL;
  242. }
  243. g_free(pretty_hostname);
  244. g_free(static_hostname);
  245. }
  246. BLUETOOTH_PLUGIN_DEFINE(hostname, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
  247. hostname_init, hostname_exit)