binder_manager.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/IServiceManager.h>
  10. #include "binder_constants.h"
  11. #include "binder_manager.h"
  12. extern "C" {
  13. #include "utils/common.h"
  14. #include "utils/includes.h"
  15. }
  16. namespace wpa_supplicant_binder {
  17. BinderManager *BinderManager::instance_ = NULL;
  18. BinderManager *BinderManager::getInstance()
  19. {
  20. if (!instance_)
  21. instance_ = new BinderManager();
  22. return instance_;
  23. }
  24. void BinderManager::destroyInstance()
  25. {
  26. if (instance_)
  27. delete instance_;
  28. instance_ = NULL;
  29. }
  30. int BinderManager::registerBinderService(struct wpa_global *global)
  31. {
  32. /* Create the main binder service object and register with
  33. * system service manager. */
  34. supplicant_object_ = new Supplicant(global);
  35. android::String16 service_name(binder_constants::kServiceName);
  36. android::defaultServiceManager()->addService(
  37. service_name, android::IInterface::asBinder(supplicant_object_));
  38. return 0;
  39. }
  40. int BinderManager::registerInterface(struct wpa_supplicant *wpa_s)
  41. {
  42. if (!wpa_s)
  43. return 1;
  44. /* Using the corresponding wpa_supplicant pointer as key to our
  45. * object map. */
  46. const void *iface_key = wpa_s;
  47. /* Return failure if we already have an object for that iface_key. */
  48. if (iface_object_map_.find(iface_key) != iface_object_map_.end())
  49. return 1;
  50. iface_object_map_[iface_key] = new Iface(wpa_s);
  51. if (!iface_object_map_[iface_key].get())
  52. return 1;
  53. wpa_s->binder_object_key = iface_key;
  54. return 0;
  55. }
  56. int BinderManager::unregisterInterface(struct wpa_supplicant *wpa_s)
  57. {
  58. if (!wpa_s || !wpa_s->binder_object_key)
  59. return 1;
  60. const void *iface_key = wpa_s;
  61. if (iface_object_map_.find(iface_key) == iface_object_map_.end())
  62. return 1;
  63. /* Delete the corresponding iface object from our map. */
  64. iface_object_map_.erase(iface_key);
  65. wpa_s->binder_object_key = NULL;
  66. return 0;
  67. }
  68. int BinderManager::getIfaceBinderObjectByKey(
  69. const void *iface_object_key,
  70. android::sp<fi::w1::wpa_supplicant::IIface> *iface_object)
  71. {
  72. if (!iface_object_key || !iface_object)
  73. return 1;
  74. if (iface_object_map_.find(iface_object_key) == iface_object_map_.end())
  75. return 1;
  76. *iface_object = iface_object_map_[iface_object_key];
  77. return 0;
  78. }
  79. } /* namespace wpa_supplicant_binder */