binder_manager.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/includes.h"
  14. #include "utils/common.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,
  38. android::IInterface::asBinder(supplicant_object_));
  39. return 0;
  40. }
  41. int BinderManager::registerInterface(struct wpa_supplicant *wpa_s)
  42. {
  43. if (!wpa_s)
  44. return 1;
  45. /* Using the corresponding wpa_supplicant pointer as key to our
  46. * object map. */
  47. const void *iface_key = wpa_s;
  48. /* Return failure if we already have an object for that iface_key. */
  49. if (iface_object_map_.find(iface_key) != iface_object_map_.end())
  50. return 1;
  51. iface_object_map_[iface_key] = new Iface(wpa_s);
  52. if (!iface_object_map_[iface_key].get())
  53. return 1;
  54. wpa_s->binder_object_key = iface_key;
  55. return 0;
  56. }
  57. int BinderManager::unregisterInterface(struct wpa_supplicant *wpa_s)
  58. {
  59. if (!wpa_s || !wpa_s->binder_object_key)
  60. return 1;
  61. const void *iface_key = wpa_s;
  62. if (iface_object_map_.find(iface_key) == iface_object_map_.end())
  63. return 1;
  64. /* Delete the corresponding iface object from our map. */
  65. iface_object_map_.erase(iface_key);
  66. wpa_s->binder_object_key = NULL;
  67. return 0;
  68. }
  69. int BinderManager::getIfaceBinderObjectByKey(
  70. const void *iface_object_key,
  71. android::sp<fi::w1::wpa_supplicant::IIface> *iface_object)
  72. {
  73. if (!iface_object_key || !iface_object)
  74. return 1;
  75. if (iface_object_map_.find(iface_object_key) == iface_object_map_.end())
  76. return 1;
  77. *iface_object = iface_object_map_[iface_object_key];
  78. return 0;
  79. }
  80. } /* namespace wpa_supplicant_binder */