manager.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <stdbool.h>
  15. #include <glib.h>
  16. #include "lib/bluetooth.h"
  17. #include "lib/bnep.h"
  18. #include "lib/sdp.h"
  19. #include "lib/uuid.h"
  20. #include "src/log.h"
  21. #include "src/plugin.h"
  22. #include "src/adapter.h"
  23. #include "src/device.h"
  24. #include "src/profile.h"
  25. #include "src/service.h"
  26. #include "bnep.h"
  27. #include "connection.h"
  28. #include "server.h"
  29. static gboolean conf_security = TRUE;
  30. static void read_config(const char *file)
  31. {
  32. GKeyFile *keyfile;
  33. GError *err = NULL;
  34. keyfile = g_key_file_new();
  35. if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
  36. g_clear_error(&err);
  37. goto done;
  38. }
  39. conf_security = !g_key_file_get_boolean(keyfile, "General",
  40. "DisableSecurity", &err);
  41. if (err) {
  42. DBG("%s: %s", file, err->message);
  43. g_clear_error(&err);
  44. }
  45. done:
  46. g_key_file_free(keyfile);
  47. DBG("Config options: Security=%s",
  48. conf_security ? "true" : "false");
  49. }
  50. static int panu_server_probe(struct btd_profile *p, struct btd_adapter *adapter)
  51. {
  52. const char *path = adapter_get_path(adapter);
  53. DBG("path %s", path);
  54. return server_register(adapter, BNEP_SVC_PANU);
  55. }
  56. static void panu_server_remove(struct btd_profile *p,
  57. struct btd_adapter *adapter)
  58. {
  59. const char *path = adapter_get_path(adapter);
  60. DBG("path %s", path);
  61. server_unregister(adapter, BNEP_SVC_PANU);
  62. }
  63. static int gn_server_probe(struct btd_profile *p, struct btd_adapter *adapter)
  64. {
  65. const char *path = adapter_get_path(adapter);
  66. DBG("path %s", path);
  67. return server_register(adapter, BNEP_SVC_GN);
  68. }
  69. static void gn_server_remove(struct btd_profile *p,
  70. struct btd_adapter *adapter)
  71. {
  72. const char *path = adapter_get_path(adapter);
  73. DBG("path %s", path);
  74. server_unregister(adapter, BNEP_SVC_GN);
  75. }
  76. static int nap_server_probe(struct btd_profile *p, struct btd_adapter *adapter)
  77. {
  78. const char *path = adapter_get_path(adapter);
  79. DBG("path %s", path);
  80. return server_register(adapter, BNEP_SVC_NAP);
  81. }
  82. static void nap_server_remove(struct btd_profile *p,
  83. struct btd_adapter *adapter)
  84. {
  85. const char *path = adapter_get_path(adapter);
  86. DBG("path %s", path);
  87. server_unregister(adapter, BNEP_SVC_NAP);
  88. }
  89. static struct btd_profile panu_profile = {
  90. .name = "network-panu",
  91. .local_uuid = NAP_UUID,
  92. .remote_uuid = PANU_UUID,
  93. .device_probe = connection_register,
  94. .device_remove = connection_unregister,
  95. .connect = connection_connect,
  96. .disconnect = connection_disconnect,
  97. .adapter_probe = panu_server_probe,
  98. .adapter_remove = panu_server_remove,
  99. };
  100. static struct btd_profile gn_profile = {
  101. .name = "network-gn",
  102. .local_uuid = PANU_UUID,
  103. .remote_uuid = GN_UUID,
  104. .device_probe = connection_register,
  105. .device_remove = connection_unregister,
  106. .connect = connection_connect,
  107. .disconnect = connection_disconnect,
  108. .adapter_probe = gn_server_probe,
  109. .adapter_remove = gn_server_remove,
  110. };
  111. static struct btd_profile nap_profile = {
  112. .name = "network-nap",
  113. .local_uuid = PANU_UUID,
  114. .remote_uuid = NAP_UUID,
  115. .device_probe = connection_register,
  116. .device_remove = connection_unregister,
  117. .connect = connection_connect,
  118. .disconnect = connection_disconnect,
  119. .adapter_probe = nap_server_probe,
  120. .adapter_remove = nap_server_remove,
  121. };
  122. static int network_init(void)
  123. {
  124. int err;
  125. read_config(CONFIGDIR "/network.conf");
  126. err = bnep_init();
  127. if (err) {
  128. if (err == -EPROTONOSUPPORT)
  129. err = -ENOSYS;
  130. return err;
  131. }
  132. /*
  133. * There is one socket to handle the incoming connections. NAP,
  134. * GN and PANU servers share the same PSM. The initial BNEP message
  135. * (setup connection request) contains the destination service
  136. * field that defines which service the source is connecting to.
  137. */
  138. if (server_init(conf_security) < 0)
  139. return -1;
  140. btd_profile_register(&panu_profile);
  141. btd_profile_register(&gn_profile);
  142. btd_profile_register(&nap_profile);
  143. return 0;
  144. }
  145. static void network_exit(void)
  146. {
  147. btd_profile_unregister(&panu_profile);
  148. btd_profile_unregister(&gn_profile);
  149. btd_profile_unregister(&nap_profile);
  150. bnep_cleanup();
  151. }
  152. BLUETOOTH_PLUGIN_DEFINE(network, VERSION,
  153. BLUETOOTH_PLUGIN_PRIORITY_DEFAULT, network_init, network_exit)