system/connectivity/wificond
Révision | 9f0fd68f7877ac8dc234eea35ab2e6e960a2b947 (tree) |
---|---|
l'heure | 2017-03-17 10:03:31 |
Auteur | Ningyuan Wang <nywang@goog...> |
Commiter | Ningyuan Wang |
Add wificond API to expose number of associated stations
Bug: 31230864
Test: compile, unit tests, manual test
Change-Id: I28cc755a79c0fb3905a6684ea1281428a5bc2afc
@@ -50,4 +50,8 @@ interface IApInterface { | ||
50 | 50 | @utf8InCpp |
51 | 51 | String getInterfaceName(); |
52 | 52 | |
53 | + // @return Returns the number of associated devices to this hotspot. | |
54 | + // Returns -1 on failure. | |
55 | + int getNumberOfAssociatedStations(); | |
56 | + | |
53 | 57 | } |
@@ -96,5 +96,17 @@ binder::Status ApInterfaceBinder::getInterfaceName(std::string* out_name) { | ||
96 | 96 | return binder::Status::ok(); |
97 | 97 | } |
98 | 98 | |
99 | +binder::Status ApInterfaceBinder::getNumberOfAssociatedStations( | |
100 | + int* out_num_of_stations) { | |
101 | + if (!impl_) { | |
102 | + LOG(WARNING) << "Cannot get number of associated stations " | |
103 | + << "from dead ApInterface"; | |
104 | + *out_num_of_stations = -1; | |
105 | + return binder::Status::ok(); | |
106 | + } | |
107 | + *out_num_of_stations = impl_->GetNumberOfAssociatedStations(); | |
108 | + return binder::Status::ok(); | |
109 | +} | |
110 | + | |
99 | 111 | } // namespace wificond |
100 | 112 | } // namespace android |
@@ -45,6 +45,8 @@ class ApInterfaceBinder : public android::net::wifi::BnApInterface { | ||
45 | 45 | const std::vector<uint8_t>& passphrase, |
46 | 46 | bool* out_success) override; |
47 | 47 | binder::Status getInterfaceName(std::string* out_name) override; |
48 | + binder::Status getNumberOfAssociatedStations( | |
49 | + int* out_num_of_stations) override; | |
48 | 50 | |
49 | 51 | private: |
50 | 52 | ApInterfaceImpl* impl_; |
@@ -47,7 +47,8 @@ ApInterfaceImpl::ApInterfaceImpl(const string& interface_name, | ||
47 | 47 | netlink_utils_(netlink_utils), |
48 | 48 | if_tool_(if_tool), |
49 | 49 | hostapd_manager_(hostapd_manager), |
50 | - binder_(new ApInterfaceBinder(this)) { | |
50 | + binder_(new ApInterfaceBinder(this)), | |
51 | + number_of_associated_stations_(0) { | |
51 | 52 | // This log keeps compiler happy. |
52 | 53 | LOG(DEBUG) << "Created ap interface " << interface_name_ |
53 | 54 | << " with index " << interface_index_; |
@@ -120,12 +121,23 @@ void ApInterfaceImpl::OnStationEvent(StationEvent event, | ||
120 | 121 | LOG(INFO) << "New station " |
121 | 122 | << LoggingUtils::GetMacString(mac_address) |
122 | 123 | << " associated with hotspot"; |
124 | + number_of_associated_stations_++; | |
123 | 125 | } else if (event == DEL_STATION) { |
124 | 126 | LOG(INFO) << "Station " |
125 | 127 | << LoggingUtils::GetMacString(mac_address) |
126 | 128 | << " disassociated from hotspot"; |
129 | + if (number_of_associated_stations_ <= 0) { | |
130 | + LOG(ERROR) << "Received DEL_STATION event when station counter is: " | |
131 | + << number_of_associated_stations_; | |
132 | + } else { | |
133 | + number_of_associated_stations_--; | |
134 | + } | |
127 | 135 | } |
128 | 136 | } |
129 | 137 | |
138 | +int ApInterfaceImpl::GetNumberOfAssociatedStations() const { | |
139 | + return number_of_associated_stations_; | |
140 | +} | |
141 | + | |
130 | 142 | } // namespace wificond |
131 | 143 | } // namespace android |
@@ -59,6 +59,7 @@ class ApInterfaceImpl { | ||
59 | 59 | wifi_system::HostapdManager::EncryptionType encryption_type, |
60 | 60 | const std::vector<uint8_t>& passphrase); |
61 | 61 | std::string GetInterfaceName() { return interface_name_; } |
62 | + int GetNumberOfAssociatedStations() const; | |
62 | 63 | |
63 | 64 | private: |
64 | 65 | const std::string interface_name_; |
@@ -68,6 +69,9 @@ class ApInterfaceImpl { | ||
68 | 69 | wifi_system::HostapdManager* const hostapd_manager_; |
69 | 70 | const android::sp<ApInterfaceBinder> binder_; |
70 | 71 | |
72 | + // Number of associated stations. | |
73 | + int number_of_associated_stations_; | |
74 | + | |
71 | 75 | void OnStationEvent(StationEvent event, |
72 | 76 | const std::vector<uint8_t>& mac_address); |
73 | 77 |
@@ -30,9 +30,12 @@ | ||
30 | 30 | using android::wifi_system::HostapdManager; |
31 | 31 | using android::wifi_system::MockHostapdManager; |
32 | 32 | using android::wifi_system::MockInterfaceTool; |
33 | +using std::placeholders::_1; | |
34 | +using std::placeholders::_2; | |
33 | 35 | using std::unique_ptr; |
34 | 36 | using std::vector; |
35 | 37 | using testing::NiceMock; |
38 | +using testing::Invoke; | |
36 | 39 | using testing::Return; |
37 | 40 | using testing::Sequence; |
38 | 41 | using testing::StrEq; |
@@ -44,6 +47,14 @@ namespace { | ||
44 | 47 | |
45 | 48 | const char kTestInterfaceName[] = "testwifi0"; |
46 | 49 | const uint32_t kTestInterfaceIndex = 42; |
50 | +const uint8_t kFakeMacAddress[] = {0x45, 0x54, 0xad, 0x67, 0x98, 0xf6}; | |
51 | + | |
52 | +void CaptureStationEventHandler( | |
53 | + OnStationEventHandler* out_handler, | |
54 | + uint32_t interface_index, | |
55 | + OnStationEventHandler handler) { | |
56 | + *out_handler = handler; | |
57 | +} | |
47 | 58 | |
48 | 59 | class ApInterfaceImplTest : public ::testing::Test { |
49 | 60 | protected: |
@@ -59,9 +70,6 @@ class ApInterfaceImplTest : public ::testing::Test { | ||
59 | 70 | unique_ptr<ApInterfaceImpl> ap_interface_; |
60 | 71 | |
61 | 72 | void SetUp() override { |
62 | - EXPECT_CALL(*netlink_utils_, | |
63 | - SubscribeStationEvent(kTestInterfaceIndex, _)); | |
64 | - | |
65 | 73 | ap_interface_.reset(new ApInterfaceImpl( |
66 | 74 | kTestInterfaceName, |
67 | 75 | kTestInterfaceIndex, |
@@ -115,5 +123,29 @@ TEST_F(ApInterfaceImplTest, ShouldRejectInvalidConfig) { | ||
115 | 123 | vector<uint8_t>())); |
116 | 124 | } |
117 | 125 | |
126 | +TEST_F(ApInterfaceImplTest, CanGetNumberOfAssociatedStations) { | |
127 | + OnStationEventHandler handler; | |
128 | + EXPECT_CALL(*netlink_utils_, | |
129 | + SubscribeStationEvent(kTestInterfaceIndex, _)). | |
130 | + WillOnce(Invoke(bind(CaptureStationEventHandler, &handler, _1, _2))); | |
131 | + | |
132 | + ap_interface_.reset(new ApInterfaceImpl( | |
133 | + kTestInterfaceName, | |
134 | + kTestInterfaceIndex, | |
135 | + netlink_utils_.get(), | |
136 | + if_tool_.get(), | |
137 | + hostapd_manager_.get())); | |
138 | + | |
139 | + vector<uint8_t> fake_mac_address(kFakeMacAddress, | |
140 | + kFakeMacAddress + sizeof(kFakeMacAddress)); | |
141 | + EXPECT_EQ(0, ap_interface_->GetNumberOfAssociatedStations()); | |
142 | + handler(NEW_STATION, fake_mac_address); | |
143 | + EXPECT_EQ(1, ap_interface_->GetNumberOfAssociatedStations()); | |
144 | + handler(NEW_STATION, fake_mac_address); | |
145 | + EXPECT_EQ(2, ap_interface_->GetNumberOfAssociatedStations()); | |
146 | + handler(DEL_STATION, fake_mac_address); | |
147 | + EXPECT_EQ(1, ap_interface_->GetNumberOfAssociatedStations()); | |
148 | +} | |
149 | + | |
118 | 150 | } // namespace wificond |
119 | 151 | } // namespace android |