ap_interface_impl.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "wificond/ap_interface_impl.h"
  17. #include <android-base/logging.h>
  18. #include "wificond/net/netlink_utils.h"
  19. #include "wificond/ap_interface_binder.h"
  20. #include "wificond/logging_utils.h"
  21. using android::net::wifi::IApInterface;
  22. using android::wifi_system::InterfaceTool;
  23. using std::array;
  24. using std::endl;
  25. using std::string;
  26. using std::unique_ptr;
  27. using namespace std::placeholders;
  28. namespace android {
  29. namespace wificond {
  30. ApInterfaceImpl::ApInterfaceImpl(const string& interface_name,
  31. uint32_t interface_index,
  32. NetlinkUtils* netlink_utils,
  33. InterfaceTool* if_tool)
  34. : interface_name_(interface_name),
  35. interface_index_(interface_index),
  36. netlink_utils_(netlink_utils),
  37. if_tool_(if_tool),
  38. binder_(new ApInterfaceBinder(this)),
  39. number_of_associated_stations_(0) {
  40. // This log keeps compiler happy.
  41. LOG(DEBUG) << "Created ap interface " << interface_name_
  42. << " with index " << interface_index_;
  43. netlink_utils_->SubscribeStationEvent(
  44. interface_index_,
  45. std::bind(&ApInterfaceImpl::OnStationEvent,
  46. this,
  47. _1, _2));
  48. netlink_utils_->SubscribeChannelSwitchEvent(
  49. interface_index_,
  50. std::bind(&ApInterfaceImpl::OnChannelSwitchEvent, this, _1, _2));
  51. }
  52. ApInterfaceImpl::~ApInterfaceImpl() {
  53. binder_->NotifyImplDead();
  54. if_tool_->SetUpState(interface_name_.c_str(), false);
  55. netlink_utils_->UnsubscribeStationEvent(interface_index_);
  56. netlink_utils_->UnsubscribeChannelSwitchEvent(interface_index_);
  57. }
  58. sp<IApInterface> ApInterfaceImpl::GetBinder() const {
  59. return binder_;
  60. }
  61. void ApInterfaceImpl::Dump(std::stringstream* ss) const {
  62. *ss << "------- Dump of AP interface with index: "
  63. << interface_index_ << " and name: " << interface_name_
  64. << "-------" << endl;
  65. *ss << "Number of associated stations: "
  66. << number_of_associated_stations_ << endl;
  67. *ss << "------- Dump End -------" << endl;
  68. }
  69. void ApInterfaceImpl::OnStationEvent(
  70. StationEvent event,
  71. const array<uint8_t, ETH_ALEN>& mac_address) {
  72. if (event == NEW_STATION) {
  73. LOG(INFO) << "New station "
  74. << LoggingUtils::GetMacString(mac_address)
  75. << " associated with hotspot";
  76. number_of_associated_stations_++;
  77. } else if (event == DEL_STATION) {
  78. LOG(INFO) << "Station "
  79. << LoggingUtils::GetMacString(mac_address)
  80. << " disassociated from hotspot";
  81. if (number_of_associated_stations_ <= 0) {
  82. LOG(ERROR) << "Received DEL_STATION event when station counter is: "
  83. << number_of_associated_stations_;
  84. return;
  85. } else {
  86. number_of_associated_stations_--;
  87. }
  88. }
  89. if (event == NEW_STATION || event == DEL_STATION) {
  90. binder_->NotifyNumAssociatedStationsChanged(number_of_associated_stations_);
  91. }
  92. }
  93. void ApInterfaceImpl::OnChannelSwitchEvent(uint32_t frequency,
  94. ChannelBandwidth bandwidth) {
  95. LOG(INFO) << "New channel on frequency: " << frequency
  96. << " with bandwidth: " << LoggingUtils::GetBandwidthString(bandwidth);
  97. binder_->NotifySoftApChannelSwitched(frequency, bandwidth);
  98. }
  99. int ApInterfaceImpl::GetNumberOfAssociatedStations() const {
  100. return number_of_associated_stations_;
  101. }
  102. } // namespace wificond
  103. } // namespace android