summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorNingyuan Wang2017-03-08 10:44:39 -0600
committerNingyuan Wang2017-03-16 13:40:41 -0500
commitcf51f862b31b5eb2066729660fd2caf48ecc77f8 (patch)
treec7cd2fac9a67580eab640de72e1eefce1de2d78e /net
parent042abc94bc768131cd3341d677465b6aeffb5bf1 (diff)
downloadsystem-connectivity-wificond-cf51f862b31b5eb2066729660fd2caf48ecc77f8.tar.gz
system-connectivity-wificond-cf51f862b31b5eb2066729660fd2caf48ecc77f8.tar.xz
system-connectivity-wificond-cf51f862b31b5eb2066729660fd2caf48ecc77f8.zip
Monitor station events for hotspot mode
This enables NetlinkManager to monitor NL80211_CMD_NEW_STATION, and NL80211_CMD_DEL_STATION. This could be used for ApInterfaceImpl for monitoring associated stations. This also creates a new class LoggingUtils to avoid repeating code. This also adds corresponding unit tests. Bug: 36043922 Test: compile, unit tests, manual tests Change-Id: I060062c0cd6250051da90658b229d786f287fad4
Diffstat (limited to 'net')
-rw-r--r--net/netlink_manager.cpp41
-rw-r--r--net/netlink_manager.h26
-rw-r--r--net/netlink_utils.cpp9
-rw-r--r--net/netlink_utils.h10
4 files changed, 82 insertions, 4 deletions
diff --git a/net/netlink_manager.cpp b/net/netlink_manager.cpp
index 193efce..74b156c 100644
--- a/net/netlink_manager.cpp
+++ b/net/netlink_manager.cpp
@@ -502,6 +502,29 @@ void NetlinkManager::BroadcastHandler(unique_ptr<const NL80211Packet> packet) {
502 OnRegChangeEvent(std::move(packet)); 502 OnRegChangeEvent(std::move(packet));
503 return; 503 return;
504 } 504 }
505 // Station eventsFor AP mode.
506 if (command == NL80211_CMD_NEW_STATION ||
507 command == NL80211_CMD_DEL_STATION) {
508 uint32_t if_index;
509 if (!packet->GetAttributeValue(NL80211_ATTR_IFINDEX, &if_index)) {
510 LOG(WARNING) << "Failed to get interface index from station event";
511 return;
512 }
513 const auto handler = on_station_event_handler_.find(if_index);
514 if (handler != on_station_event_handler_.end()) {
515 vector<uint8_t> mac_address;
516 if (!packet->GetAttributeValue(NL80211_ATTR_MAC, &mac_address)) {
517 LOG(WARNING) << "Failed to get mac address from station event";
518 return;
519 }
520 if (command == NL80211_CMD_NEW_STATION) {
521 handler->second(NEW_STATION, mac_address);
522 } else {
523 handler->second(DEL_STATION, mac_address);
524 }
525 }
526 return;
527 }
505} 528}
506 529
507void NetlinkManager::OnRegChangeEvent(unique_ptr<const NL80211Packet> packet) { 530void NetlinkManager::OnRegChangeEvent(unique_ptr<const NL80211Packet> packet) {
@@ -540,7 +563,7 @@ void NetlinkManager::OnRegChangeEvent(unique_ptr<const NL80211Packet> packet) {
540 return; 563 return;
541 } 564 }
542 565
543 auto handler = on_reg_domain_changed_handler_.find(wiphy_index); 566 const auto handler = on_reg_domain_changed_handler_.find(wiphy_index);
544 if (handler == on_reg_domain_changed_handler_.end()) { 567 if (handler == on_reg_domain_changed_handler_.end()) {
545 LOG(DEBUG) << "No handler for country code changed event from wiphy" 568 LOG(DEBUG) << "No handler for country code changed event from wiphy"
546 << "with index: " << wiphy_index; 569 << "with index: " << wiphy_index;
@@ -556,7 +579,7 @@ void NetlinkManager::OnMlmeEvent(unique_ptr<const NL80211Packet> packet) {
556 LOG(ERROR) << "Failed to get interface index from a MLME event message"; 579 LOG(ERROR) << "Failed to get interface index from a MLME event message";
557 return; 580 return;
558 } 581 }
559 auto handler = on_mlme_event_handler_.find(if_index); 582 const auto handler = on_mlme_event_handler_.find(if_index);
560 if (handler == on_mlme_event_handler_.end()) { 583 if (handler == on_mlme_event_handler_.end()) {
561 LOG(DEBUG) << "No handler for mlme event from interface" 584 LOG(DEBUG) << "No handler for mlme event from interface"
562 << " with index: " << if_index; 585 << " with index: " << if_index;
@@ -608,7 +631,7 @@ void NetlinkManager::OnSchedScanResultsReady(unique_ptr<const NL80211Packet> pac
608 return; 631 return;
609 } 632 }
610 633
611 auto handler = on_sched_scan_result_ready_handler_.find(if_index); 634 const auto handler = on_sched_scan_result_ready_handler_.find(if_index);
612 if (handler == on_sched_scan_result_ready_handler_.end()) { 635 if (handler == on_sched_scan_result_ready_handler_.end()) {
613 LOG(DEBUG) << "No handler for scheduled scan result notification from" 636 LOG(DEBUG) << "No handler for scheduled scan result notification from"
614 << " interface with index: " << if_index; 637 << " interface with index: " << if_index;
@@ -629,7 +652,7 @@ void NetlinkManager::OnScanResultsReady(unique_ptr<const NL80211Packet> packet)
629 aborted = true; 652 aborted = true;
630 } 653 }
631 654
632 auto handler = on_scan_result_ready_handler_.find(if_index); 655 const auto handler = on_scan_result_ready_handler_.find(if_index);
633 if (handler == on_scan_result_ready_handler_.end()) { 656 if (handler == on_scan_result_ready_handler_.end()) {
634 LOG(WARNING) << "No handler for scan result notification from interface" 657 LOG(WARNING) << "No handler for scan result notification from interface"
635 << " with index: " << if_index; 658 << " with index: " << if_index;
@@ -662,6 +685,16 @@ void NetlinkManager::OnScanResultsReady(unique_ptr<const NL80211Packet> packet)
662 handler->second(if_index, aborted, ssids, freqs); 685 handler->second(if_index, aborted, ssids, freqs);
663} 686}
664 687
688void NetlinkManager::SubscribeStationEvent(
689 uint32_t interface_index,
690 OnStationEventHandler handler) {
691 on_station_event_handler_[interface_index] = handler;
692}
693
694void NetlinkManager::UnsubscribeStationEvent(uint32_t interface_index) {
695 on_station_event_handler_.erase(interface_index);
696}
697
665void NetlinkManager::SubscribeRegDomainChange( 698void NetlinkManager::SubscribeRegDomainChange(
666 uint32_t wiphy_index, 699 uint32_t wiphy_index,
667 OnRegDomainChangedHandler handler) { 700 OnRegDomainChangedHandler handler) {
diff --git a/net/netlink_manager.h b/net/netlink_manager.h
index 72b7d31..5d84d92 100644
--- a/net/netlink_manager.h
+++ b/net/netlink_manager.h
@@ -84,6 +84,20 @@ typedef std::function<void(
84typedef std::function<void( 84typedef std::function<void(
85 std::string& country_code)> OnRegDomainChangedHandler; 85 std::string& country_code)> OnRegDomainChangedHandler;
86 86
87// Enum used for identifying the type of a station event.
88// This is used by function |OnStationEventHandler|.
89enum StationEvent {
90 NEW_STATION,
91 DEL_STATION
92};
93
94// This describes a type of function handling station events.
95// |event| specifies the type of this event.
96// |mac_address| is the station mac address associated with this event.
97typedef std::function<void(
98 StationEvent event,
99 const std::vector<uint8_t>& mac_address)> OnStationEventHandler;
100
87class NetlinkManager { 101class NetlinkManager {
88 public: 102 public:
89 explicit NetlinkManager(EventLoop* event_loop); 103 explicit NetlinkManager(EventLoop* event_loop);
@@ -203,6 +217,16 @@ class NetlinkManager {
203 // from wiphy with index |wiphy_index|. 217 // from wiphy with index |wiphy_index|.
204 virtual void UnsubscribeRegDomainChange(uint32_t wiphy_index); 218 virtual void UnsubscribeRegDomainChange(uint32_t wiphy_index);
205 219
220 // Sign up to be notified when there is an station event.
221 // Only one handler can be registered per interface index.
222 // New handler will replace the registered handler if they are for the
223 // same interface index.
224 virtual void SubscribeStationEvent(uint32_t interface_index,
225 OnStationEventHandler handler);
226
227 // Cancel the sign-up of receiving station events.
228 virtual void UnsubscribeStationEvent(uint32_t interface_index);
229
206 private: 230 private:
207 bool SetupSocket(android::base::unique_fd* netlink_fd); 231 bool SetupSocket(android::base::unique_fd* netlink_fd);
208 bool WatchSocket(android::base::unique_fd* netlink_fd); 232 bool WatchSocket(android::base::unique_fd* netlink_fd);
@@ -248,6 +272,8 @@ class NetlinkManager {
248 // regulatory domain change notifications. 272 // regulatory domain change notifications.
249 std::map<uint32_t, OnRegDomainChangedHandler> on_reg_domain_changed_handler_; 273 std::map<uint32_t, OnRegDomainChangedHandler> on_reg_domain_changed_handler_;
250 274
275 std::map<uint32_t, OnStationEventHandler> on_station_event_handler_;
276
251 // Mapping from family name to family id, and group name to group id. 277 // Mapping from family name to family id, and group name to group id.
252 std::map<std::string, MessageType> message_types_; 278 std::map<std::string, MessageType> message_types_;
253 279
diff --git a/net/netlink_utils.cpp b/net/netlink_utils.cpp
index 6a33d67..0fa0116 100644
--- a/net/netlink_utils.cpp
+++ b/net/netlink_utils.cpp
@@ -400,5 +400,14 @@ void NetlinkUtils::UnsubscribeRegDomainChange(uint32_t wiphy_index) {
400 netlink_manager_->UnsubscribeRegDomainChange(wiphy_index); 400 netlink_manager_->UnsubscribeRegDomainChange(wiphy_index);
401} 401}
402 402
403void NetlinkUtils::SubscribeStationEvent(uint32_t interface_index,
404 OnStationEventHandler handler) {
405 netlink_manager_->SubscribeStationEvent(interface_index, handler);
406}
407
408void NetlinkUtils::UnsubscribeStationEvent(uint32_t interface_index) {
409 netlink_manager_->UnsubscribeStationEvent(interface_index);
410}
411
403} // namespace wificond 412} // namespace wificond
404} // namespace android 413} // namespace android
diff --git a/net/netlink_utils.h b/net/netlink_utils.h
index 048deb7..f8b9c0e 100644
--- a/net/netlink_utils.h
+++ b/net/netlink_utils.h
@@ -193,6 +193,16 @@ class NetlinkUtils {
193 // from wiphy with index |wiphy_index|. 193 // from wiphy with index |wiphy_index|.
194 virtual void UnsubscribeRegDomainChange(uint32_t wiphy_index); 194 virtual void UnsubscribeRegDomainChange(uint32_t wiphy_index);
195 195
196 // Sign up to be notified when there is an station event.
197 // Only one handler can be registered per interface index.
198 // New handler will replace the registered handler if they are for the
199 // same interface index.
200 virtual void SubscribeStationEvent(uint32_t interface_index,
201 OnStationEventHandler handler);
202
203 // Cancel the sign-up of receiving station events.
204 virtual void UnsubscribeStationEvent(uint32_t interface_index);
205
196 private: 206 private:
197 bool ParseBandInfo(const NL80211Packet* const packet, 207 bool ParseBandInfo(const NL80211Packet* const packet,
198 BandInfo* out_band_info); 208 BandInfo* out_band_info);