binder.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * binder interface for wpa_supplicant daemon
  3. * Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2004-2016, Roshan Pius <rpius@google.com>
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include <binder/IPCThreadState.h>
  10. #include <binder/IServiceManager.h>
  11. #include <binder/ProcessState.h>
  12. #include "binder_manager.h"
  13. extern "C" {
  14. #include "binder.h"
  15. #include "binder_i.h"
  16. #include "utils/common.h"
  17. #include "utils/eloop.h"
  18. #include "utils/includes.h"
  19. }
  20. void wpas_binder_sock_handler(int sock, void *eloop_ctx, void *sock_ctx)
  21. {
  22. struct wpa_global *global = (wpa_global *)eloop_ctx;
  23. struct wpas_binder_priv *priv = (wpas_binder_priv *)sock_ctx;
  24. wpa_printf(
  25. MSG_DEBUG, "Processing binder events on FD %d", priv->binder_fd);
  26. android::IPCThreadState::self()->handlePolledCommands();
  27. }
  28. struct wpas_binder_priv *wpas_binder_init(struct wpa_global *global)
  29. {
  30. struct wpas_binder_priv *priv;
  31. wpa_supplicant_binder::BinderManager *binder_manager;
  32. priv = (wpas_binder_priv *)os_zalloc(sizeof(*priv));
  33. if (!priv)
  34. return NULL;
  35. priv->global = global;
  36. android::ProcessState::self()->setThreadPoolMaxThreadCount(0);
  37. android::IPCThreadState::self()->disableBackgroundScheduling(true);
  38. android::IPCThreadState::self()->setupPolling(&priv->binder_fd);
  39. wpa_printf(MSG_INFO, "Process binder events on FD %d", priv->binder_fd);
  40. if (priv->binder_fd < 0)
  41. goto err;
  42. /* Look for read events from the binder socket in the eloop. */
  43. if (eloop_register_read_sock(
  44. priv->binder_fd, wpas_binder_sock_handler, global, priv) < 0)
  45. goto err;
  46. binder_manager = wpa_supplicant_binder::BinderManager::getInstance();
  47. if (!binder_manager)
  48. goto err;
  49. binder_manager->registerBinderService(global);
  50. /* We may not need to store this binder manager reference in the
  51. * global data strucure because we've made it a singleton class. */
  52. priv->binder_manager = (void *)binder_manager;
  53. return priv;
  54. err:
  55. wpas_binder_deinit(priv);
  56. return NULL;
  57. }
  58. void wpas_binder_deinit(struct wpas_binder_priv *priv)
  59. {
  60. if (!priv)
  61. return;
  62. wpa_supplicant_binder::BinderManager::destroyInstance();
  63. eloop_unregister_read_sock(priv->binder_fd);
  64. android::IPCThreadState::shutdown();
  65. }
  66. int wpas_binder_register_interface(struct wpa_supplicant *wpa_s)
  67. {
  68. if (!wpa_s->global->binder)
  69. return 1;
  70. wpa_supplicant_binder::BinderManager *binder_manager =
  71. wpa_supplicant_binder::BinderManager::getInstance();
  72. if (!binder_manager)
  73. return 1;
  74. return binder_manager->registerInterface(wpa_s);
  75. }
  76. int wpas_binder_unregister_interface(struct wpa_supplicant *wpa_s)
  77. {
  78. if (!wpa_s->global->binder)
  79. return 1;
  80. wpa_supplicant_binder::BinderManager *binder_manager =
  81. wpa_supplicant_binder::BinderManager::getInstance();
  82. if (!binder_manager)
  83. return 1;
  84. return binder_manager->unregisterInterface(wpa_s);
  85. }