supplicant.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "supplicant.h"
  10. #include "binder_manager.h"
  11. namespace wpa_supplicant_binder {
  12. Supplicant::Supplicant(struct wpa_global *global) : wpa_global_(global) {}
  13. android::binder::Status Supplicant::CreateInterface(
  14. const android::os::PersistableBundle &params,
  15. android::sp<fi::w1::wpa_supplicant::IIface> *aidl_return)
  16. {
  17. android::String16 driver, ifname, confname, bridge_ifname;
  18. /* Check if required Ifname argument is missing */
  19. if (!params.getString(android::String16("Ifname"), &ifname))
  20. return android::binder::Status::fromServiceSpecificError(
  21. ERROR_INVALID_ARGS,
  22. android::String8("Ifname missing in params."));
  23. /* Retrieve the remaining params from the dictionary */
  24. params.getString(android::String16("Driver"), &driver);
  25. params.getString(android::String16("ConfigFile"), &confname);
  26. params.getString(android::String16("BridgeIfname"), &bridge_ifname);
  27. /*
  28. * Try to get the wpa_supplicant record for this iface, return
  29. * an error if we already control it.
  30. */
  31. if (wpa_supplicant_get_iface(
  32. wpa_global_, android::String8(ifname).string()) != NULL)
  33. return android::binder::Status::fromServiceSpecificError(
  34. ERROR_IFACE_EXISTS,
  35. android::String8("wpa_supplicant already controls this "
  36. "interface."));
  37. android::binder::Status status;
  38. struct wpa_supplicant *wpa_s = NULL;
  39. struct wpa_interface iface;
  40. os_memset(&iface, 0, sizeof(iface));
  41. iface.driver = os_strdup(android::String8(driver).string());
  42. iface.ifname = os_strdup(android::String8(ifname).string());
  43. iface.confname = os_strdup(android::String8(confname).string());
  44. iface.bridge_ifname =
  45. os_strdup(android::String8(bridge_ifname).string());
  46. /* Otherwise, have wpa_supplicant attach to it. */
  47. wpa_s = wpa_supplicant_add_iface(wpa_global_, &iface, NULL);
  48. /* The supplicant core creates a corresponding binder object via
  49. * BinderManager when |wpa_supplicant_add_iface| is called. */
  50. if (!wpa_s || !wpa_s->binder_object_key) {
  51. status = android::binder::Status::fromServiceSpecificError(
  52. ERROR_UNKNOWN,
  53. android::String8(
  54. "wpa_supplicant couldn't grab this interface."));
  55. } else {
  56. BinderManager *binder_manager = BinderManager::getInstance();
  57. if (!binder_manager ||
  58. binder_manager->getIfaceBinderObjectByKey(
  59. wpa_s->binder_object_key, aidl_return))
  60. status =
  61. android::binder::Status::fromServiceSpecificError(
  62. ERROR_UNKNOWN,
  63. android::String8("wpa_supplicant encountered a "
  64. "binder error."));
  65. else
  66. status = android::binder::Status::ok();
  67. }
  68. os_free((void *)iface.driver);
  69. os_free((void *)iface.ifname);
  70. os_free((void *)iface.confname);
  71. os_free((void *)iface.bridge_ifname);
  72. return status;
  73. }
  74. android::binder::Status Supplicant::RemoveInterface(const std::string &ifname)
  75. {
  76. struct wpa_supplicant *wpa_s;
  77. wpa_s = wpa_supplicant_get_iface(wpa_global_, ifname.c_str());
  78. if (!wpa_s || !wpa_s->binder_object_key)
  79. return android::binder::Status::fromServiceSpecificError(
  80. ERROR_IFACE_UNKNOWN,
  81. android::String8("wpa_supplicant does not control this "
  82. "interface."));
  83. if (wpa_supplicant_remove_iface(wpa_global_, wpa_s, 0))
  84. return android::binder::Status::fromServiceSpecificError(
  85. ERROR_UNKNOWN,
  86. android::String8(
  87. "wpa_supplicant couldn't remove this interface."));
  88. return android::binder::Status::ok();
  89. }
  90. android::binder::Status Supplicant::GetInterface(
  91. const std::string &ifname,
  92. android::sp<fi::w1::wpa_supplicant::IIface> *aidl_return)
  93. {
  94. struct wpa_supplicant *wpa_s;
  95. wpa_s = wpa_supplicant_get_iface(wpa_global_, ifname.c_str());
  96. if (!wpa_s || !wpa_s->binder_object_key)
  97. return android::binder::Status::fromServiceSpecificError(
  98. ERROR_IFACE_UNKNOWN,
  99. android::String8(
  100. "wpa_supplicant does not control this interface."));
  101. BinderManager *binder_manager = BinderManager::getInstance();
  102. if (!binder_manager ||
  103. binder_manager->getIfaceBinderObjectByKey(
  104. wpa_s->binder_object_key, aidl_return))
  105. return android::binder::Status::fromServiceSpecificError(
  106. ERROR_UNKNOWN,
  107. android::String8(
  108. "wpa_supplicant encountered a binder error."));
  109. return android::binder::Status::ok();
  110. }
  111. } /* namespace wpa_supplicant_binder */