17397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang/*
27397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang * Copyright (C) 2016 The Android Open Source Project
37397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang *
47397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang * Licensed under the Apache License, Version 2.0 (the "License");
57397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang * you may not use this file except in compliance with the License.
67397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang * You may obtain a copy of the License at
77397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang *
87397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang *      http://www.apache.org/licenses/LICENSE-2.0
97397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang *
107397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang * Unless required by applicable law or agreed to in writing, software
117397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang * distributed under the License is distributed on an "AS IS" BASIS,
127397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang * See the License for the specific language governing permissions and
147397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang * limitations under the License.
157397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang */
167397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
177397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang#ifndef WIFICOND_NET_NETLINK_MANAGER_H_
187397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang#define WIFICOND_NET_NETLINK_MANAGER_H_
197397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
207397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang#include <functional>
217397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang#include <map>
2221ff4c7ad0b0ec8da8392c00eedbae022d6e21e5Ningyuan Wang#include <memory>
237397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
240bb07ecdb74d6e9af6271cec1bdc6232d546a878Ningyuan Wang#include <android-base/macros.h>
257397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang#include <android-base/unique_fd.h>
267397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
277397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang#include "event_loop.h"
287397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
297397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wangnamespace android {
307397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wangnamespace wificond {
317397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
32042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wangclass MlmeEventHandler;
337397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wangclass NL80211Packet;
347397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
357397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang// Encapsulates all the different things we know about a specific message
367397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang// type like its name, and its id.
377397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wangstruct MessageType {
387397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang   // This constructor is needed by map[key] operation.
397397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang   MessageType() {};
407397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang   explicit MessageType(uint16_t id) {
417397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang     family_id = id;
427397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang   };
437397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang   uint16_t family_id;
447397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang   // Multicast groups supported by the family.  The string and mapping to
457397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang   // a group id are extracted from the CTRL_CMD_NEWFAMILY message.
467397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang   std::map<std::string, uint32_t> groups;
477397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang};
487397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
49c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang// This describes a type of function handling scan results ready notification.
50c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang// |interface_index| is the index of interface which the scan results
51c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang// are from.
5259ab0a4ec0c5f3124bb63a683a1c46dba21faf3fNingyuan Wang// |aborted| is a boolean indicating if this scan was aborted or not.
5359ab0a4ec0c5f3124bb63a683a1c46dba21faf3fNingyuan Wang// According to nl80211.h document, part of the scan result might still be
5459ab0a4ec0c5f3124bb63a683a1c46dba21faf3fNingyuan Wang// available even when the scan was aborted.
55c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang// |ssids| is a vector of scan ssids associated with the corresponding
56c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang// scan request.
57c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang// |frequencies| is a vector of scan frequencies associated with the
58c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang// corresponding scan request.
59c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wangtypedef std::function<void(
60c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang    uint32_t interface_index,
61c9566b1dc5f3bd92e03ed8806e59af9cba2b672fNingyuan Wang    bool aborted,
62c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang    std::vector<std::vector<uint8_t>>& ssids,
63c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang    std::vector<uint32_t>& frequencies)> OnScanResultsReadyHandler;
64c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang
6559ab0a4ec0c5f3124bb63a683a1c46dba21faf3fNingyuan Wang// This describes a type of function handling scheduled scan results ready
6659ab0a4ec0c5f3124bb63a683a1c46dba21faf3fNingyuan Wang// notification. This can also be used for notificating the stopping of a
6759ab0a4ec0c5f3124bb63a683a1c46dba21faf3fNingyuan Wang// scheduled scan.
6859ab0a4ec0c5f3124bb63a683a1c46dba21faf3fNingyuan Wang// |interface_index| is the index of interface which the scan results
6959ab0a4ec0c5f3124bb63a683a1c46dba21faf3fNingyuan Wang// are from.
7059ab0a4ec0c5f3124bb63a683a1c46dba21faf3fNingyuan Wang// |scan_stopped| is a boolean indicating if this scheduled scan was stopped
7159ab0a4ec0c5f3124bb63a683a1c46dba21faf3fNingyuan Wang// or not.
728044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wangtypedef std::function<void(
7359ab0a4ec0c5f3124bb63a683a1c46dba21faf3fNingyuan Wang    uint32_t interface_index,
7459ab0a4ec0c5f3124bb63a683a1c46dba21faf3fNingyuan Wang    bool scan_stopped)> OnSchedScanResultsReadyHandler;
758044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang
76495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang// This describes a type of function handling regulatory domain change
77495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang// notification.
78495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang// If the regulatory domain set is one that pertains to a specific country,
79495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang// |country_code| will be set accordingly.
80495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang// If the regulatory domain set does not pertain to a specific country,
81495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang// |country_code| will be an empty string. This could be a world regulatory
82495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang// domain or a intersection regulatory domain.
83495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang// See details in defination of |nl80211_reg_type| from nl80211.h.
84495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wangtypedef std::function<void(
85495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang    std::string& country_code)> OnRegDomainChangedHandler;
86495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang
87cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang// Enum used for identifying the type of a station event.
88cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang// This is used by function |OnStationEventHandler|.
89cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wangenum StationEvent {
90cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang    NEW_STATION,
91cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang    DEL_STATION
92cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang};
93cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang
94cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang// This describes a type of function handling station events.
95cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang// |event| specifies the type of this event.
96cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang// |mac_address| is the station mac address associated with this event.
97cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wangtypedef std::function<void(
98cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang    StationEvent event,
99cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang    const std::vector<uint8_t>& mac_address)> OnStationEventHandler;
100cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang
1017397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wangclass NetlinkManager {
1027397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang public:
1037397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  explicit NetlinkManager(EventLoop* event_loop);
104e6dce59eaa5f1fcbdfb4b3fc6a60d5cd3b522b06Ningyuan Wang  virtual ~NetlinkManager();
1057397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  // Initialize netlink manager.
1067397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  // This includes setting up socket and requesting nl80211 family id from kernel.
1075ea9c07f01b5a7a617dbc99011a5d0952a7a87dbNingyuan Wang  // Returns true on success.
1084911e28b6555e42d12027d1b6999654a25feb758Ningyuan Wang  virtual bool Start();
1095ea9c07f01b5a7a617dbc99011a5d0952a7a87dbNingyuan Wang  // Returns true if this netlink manager object is started.
1104911e28b6555e42d12027d1b6999654a25feb758Ningyuan Wang  virtual bool IsStarted() const;
1117397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  // Returns a sequence number available for use.
1124911e28b6555e42d12027d1b6999654a25feb758Ningyuan Wang  virtual uint32_t GetSequenceNumber();
113e6dce59eaa5f1fcbdfb4b3fc6a60d5cd3b522b06Ningyuan Wang  // Get NL80211 netlink family id,
1144911e28b6555e42d12027d1b6999654a25feb758Ningyuan Wang  virtual uint16_t GetFamilyId();
11584b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang
1167397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  // Send |packet| to kernel.
1177397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  // This works in an asynchronous way.
1187397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  // |handler| will be run when we receive a valid reply from kernel.
1190c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  // Do not use this asynchronous interface to send a dump request.
1207397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  // Returns true on success.
1214911e28b6555e42d12027d1b6999654a25feb758Ningyuan Wang  virtual bool RegisterHandlerAndSendMessage(const NL80211Packet& packet,
12221ff4c7ad0b0ec8da8392c00eedbae022d6e21e5Ningyuan Wang      std::function<void(std::unique_ptr<const NL80211Packet>)> handler);
1230c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  // Synchronous version of |RegisterHandlerAndSendMessage|.
1240c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  // Returns true on successfully receiving an valid reply.
12584b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  // Reply packets will be stored in |*response|.
12621ff4c7ad0b0ec8da8392c00eedbae022d6e21e5Ningyuan Wang  virtual bool SendMessageAndGetResponses(
12721ff4c7ad0b0ec8da8392c00eedbae022d6e21e5Ningyuan Wang      const NL80211Packet& packet,
12821ff4c7ad0b0ec8da8392c00eedbae022d6e21e5Ningyuan Wang      std::vector<std::unique_ptr<const NL80211Packet>>* response);
12984b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  // Wrapper of |SendMessageAndGetResponses| for messages with a single
13084b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  // response.
13184b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  // Returns true on successfully receiving an valid reply.
132f4bed1cc3958c78754997d17cd6fb3839468331dNingyuan Wang  // This will returns false if a NLMSG_ERROR is received.
13384b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  // Reply packet will be stored in |*response|.
13484b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  virtual bool SendMessageAndGetSingleResponse(
13584b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang      const NL80211Packet& packet,
13684b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang      std::unique_ptr<const NL80211Packet>* response);
137f4bed1cc3958c78754997d17cd6fb3839468331dNingyuan Wang
138f4bed1cc3958c78754997d17cd6fb3839468331dNingyuan Wang  // Wrapper of |SendMessageAndGetResponses| for messages with a single
139f4bed1cc3958c78754997d17cd6fb3839468331dNingyuan Wang  // response.
140f4bed1cc3958c78754997d17cd6fb3839468331dNingyuan Wang  // Returns true on successfully receiving an valid reply.
141f4bed1cc3958c78754997d17cd6fb3839468331dNingyuan Wang  // This will returns true if a NLMSG_ERROR is received.
142f4bed1cc3958c78754997d17cd6fb3839468331dNingyuan Wang  // This is useful when the caller needs the error code from kernel.
143f4bed1cc3958c78754997d17cd6fb3839468331dNingyuan Wang  // Reply packet will be stored in |*response|.
144f4bed1cc3958c78754997d17cd6fb3839468331dNingyuan Wang  virtual bool SendMessageAndGetSingleResponseOrError(
145f4bed1cc3958c78754997d17cd6fb3839468331dNingyuan Wang      const NL80211Packet& packet,
146f4bed1cc3958c78754997d17cd6fb3839468331dNingyuan Wang      std::unique_ptr<const NL80211Packet>* response);
147f4bed1cc3958c78754997d17cd6fb3839468331dNingyuan Wang
14884b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  // Wrapper of |SendMessageAndGetResponses| for messages that trigger
14984b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  // only a NLMSG_ERROR response
15084b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  // Returns true if the message is successfully sent and a NLMSG_ERROR response
15184b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  // comes back, regardless of the error code.
15284b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  // Error code will be stored in |*error_code|
15384b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  virtual bool SendMessageAndGetAckOrError(const NL80211Packet& packet,
15484b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang                                           int* error_code);
15584b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  // Wrapper of |SendMessageAndGetResponses| that returns true iff the response
15684b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  // is an ACK.
15784b17dff63534af6957003f89fd4110c06d100e6Ningyuan Wang  virtual bool SendMessageAndGetAck(const NL80211Packet& packet);
1587397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
159c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang  // Sign up to receive and log multicast events of a specific type.
1601cd71d54940f028c1881c0962ff18ace2c94d9f3Ningyuan Wang  // |group| is one of the string NL80211_MULTICAST_GROUP_* in nl80211.h.
1611cd71d54940f028c1881c0962ff18ace2c94d9f3Ningyuan Wang  virtual bool SubscribeToEvents(const std::string& group);
1621cd71d54940f028c1881c0962ff18ace2c94d9f3Ningyuan Wang
163c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang  // Sign up to be notified when new scan results are available.
164c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang  // |handler| will be called when the kernel signals to wificond that a scan
165c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang  // has been completed on the given |interface_index|.  See the declaration of
166c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang  // OnScanResultsReadyHandler for documentation on the semantics of this
167c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang  // callback.
1688044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // Only one handler can be registered per interface index.
1698044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // New handler will replace the registered handler if they are for the
1708044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // same interface index.
171c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang  virtual void SubscribeScanResultNotification(
172c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang      uint32_t interface_index,
173c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang      OnScanResultsReadyHandler handler);
174c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang
175c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang  // Cancel the sign-up of receiving new scan result notification from
176c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang  // interface with index |interface_index|.
177c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang  virtual void UnsubscribeScanResultNotification(uint32_t interface_index);
178c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang
179042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang  // Sign up to be notified when there is MLME event.
180042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang  // Only one handler can be registered per interface index.
181042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang  // New handler will replace the registered handler if they are for the
182042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang  // same interface index.
183042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang  // NetlinkManager is not going to take ownership of this pointer, and that it
184042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang  // is the caller's responsibility to make sure that the object exists for the
185042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang  // duration of the subscription.
186042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang  virtual void SubscribeMlmeEvent(uint32_t interface_index,
187042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang                                  MlmeEventHandler* handler);
188042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang
189042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang  // Cancel the sign-up of receiving MLME event notification
190042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang  // from interface with index |interface_index|.
191042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang  virtual void UnsubscribeMlmeEvent(uint32_t interface_index);
192042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang
1938044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // Sign up to be notified when new scan results are available.
1948044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // |handler| will be called when the kernel signals to wificond that a
1958044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // scheduled scan has been completed on the given |interface_index|.
1968044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // See the declaration of OnSchedScanResultsReadyHandler for documentation
1978044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // on the semantics of this callback.
1988044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // Only one handler can be registered per interface index.
1998044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // New handler will replace the registered handler if they are for the
2008044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // same interface index.
2018044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  virtual void SubscribeSchedScanResultNotification(
2028044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang      uint32_t interface_index,
2038044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang      OnSchedScanResultsReadyHandler handler);
2048044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang
2058044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // Cancel the sign-up of receiving new scheduled scan result notification from
2068044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // interface with index |interface_index|.
2078044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  virtual void UnsubscribeSchedScanResultNotification(uint32_t interface_index);
2088044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang
209495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang  // Sign up to be notified when there is an regulatory domain change.
210495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang  // Only one handler can be registered per wiphy index.
211495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang  // New handler will replace the registered handler if they are for the
212495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang  // same wiphy index.
213495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang  virtual void SubscribeRegDomainChange(uint32_t wiphy_index,
214495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang                                        OnRegDomainChangedHandler handler);
215495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang
216495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang  // Cancel the sign-up of receiving regulatory domain change notification
217495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang  // from wiphy with index |wiphy_index|.
218495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang  virtual void UnsubscribeRegDomainChange(uint32_t wiphy_index);
219495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang
220cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang  // Sign up to be notified when there is an station event.
221cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang  // Only one handler can be registered per interface index.
222cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang  // New handler will replace the registered handler if they are for the
223cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang  // same interface index.
224cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang  virtual void SubscribeStationEvent(uint32_t interface_index,
225cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang                                     OnStationEventHandler handler);
226cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang
227cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang  // Cancel the sign-up of receiving station events.
228cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang  virtual void UnsubscribeStationEvent(uint32_t interface_index);
229cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang
2307397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang private:
2310c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  bool SetupSocket(android::base::unique_fd* netlink_fd);
2320c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  bool WatchSocket(android::base::unique_fd* netlink_fd);
2330c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  void ReceivePacketAndRunHandler(int fd);
2347397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  bool DiscoverFamilyId();
2350c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  bool SendMessageInternal(const NL80211Packet& packet, int fd);
23621ff4c7ad0b0ec8da8392c00eedbae022d6e21e5Ningyuan Wang  void BroadcastHandler(std::unique_ptr<const NL80211Packet> packet);
237495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang  void OnRegChangeEvent(std::unique_ptr<const NL80211Packet> packet);
238042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang  void OnMlmeEvent(std::unique_ptr<const NL80211Packet> packet);
23921ff4c7ad0b0ec8da8392c00eedbae022d6e21e5Ningyuan Wang  void OnScanResultsReady(std::unique_ptr<const NL80211Packet> packet);
2408044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  void OnSchedScanResultsReady(std::unique_ptr<const NL80211Packet> packet);
2417397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
2427397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  // This handler revceives mapping from NL80211 family name to family id,
2437397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  // as well as mapping from group name to group id.
2447397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  // These mappings are allocated by kernel.
24521ff4c7ad0b0ec8da8392c00eedbae022d6e21e5Ningyuan Wang  void OnNewFamily(std::unique_ptr<const NL80211Packet> packet);
2467397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
2475ea9c07f01b5a7a617dbc99011a5d0952a7a87dbNingyuan Wang  bool started_;
2480c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  // We use different sockets for synchronous and asynchronous interfaces.
2490c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  // Kernel will reply error message when we start a new request in the
2500c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  // middle of a dump request.
2510c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  // Using different sockets help us avoid the complexity of message
2520c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  // rescheduling.
2530c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  android::base::unique_fd sync_netlink_fd_;
2540c2afe0b1381efea4a2821a51b65fb2a255364d3Ningyuan Wang  android::base::unique_fd async_netlink_fd_;
2557397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  EventLoop* event_loop_;
2567397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
2577397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  // This is a collection of message handlers, for each sequence number.
25821ff4c7ad0b0ec8da8392c00eedbae022d6e21e5Ningyuan Wang  std::map<uint32_t,
25921ff4c7ad0b0ec8da8392c00eedbae022d6e21e5Ningyuan Wang      std::function<void(std::unique_ptr<const NL80211Packet>)>> message_handlers_;
2607397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
261c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang  // A mapping from interface index to the handler registered to receive
262c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang  // scan results notifications.
263c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang  std::map<uint32_t, OnScanResultsReadyHandler> on_scan_result_ready_handler_;
2648044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // A mapping from interface index to the handler registered to receive
2658044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  // scheduled scan results notifications.
2668044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang  std::map<uint32_t, OnSchedScanResultsReadyHandler>
2678044a946f4b350291ddd44c52db7ca34e21bad8fNingyuan Wang      on_sched_scan_result_ready_handler_;
268c67d730fd1da7d8290312cd7429221602df76e70Ningyuan Wang
269042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang  std::map<uint32_t, MlmeEventHandler*> on_mlme_event_handler_;
270042736e71ac9b9e18c38c00a3d588a5239a1da0fNingyuan Wang
271495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang  // A mapping from wiphy index to the handler registered to receive
272495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang  // regulatory domain change notifications.
273495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang  std::map<uint32_t, OnRegDomainChangedHandler> on_reg_domain_changed_handler_;
274495e5b35c9b7231574658f273f2aa061e46d0a08Ningyuan Wang
275cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang  std::map<uint32_t, OnStationEventHandler> on_station_event_handler_;
276cf51f862b31b5eb2066729660fd2caf48ecc77f8Ningyuan Wang
2777397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  // Mapping from family name to family id, and group name to group id.
2787397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  std::map<std::string, MessageType> message_types_;
2797397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
2807397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang  uint32_t sequence_number_;
2810bb07ecdb74d6e9af6271cec1bdc6232d546a878Ningyuan Wang
2820bb07ecdb74d6e9af6271cec1bdc6232d546a878Ningyuan Wang  DISALLOW_COPY_AND_ASSIGN(NetlinkManager);
2837397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang};
2847397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
2857397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang}  // namespace wificond
2867397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang}  // namespace android
2877397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang
2887397e6b7c3635e2121f6d2229557e684bc4756cfNingyuan Wang#endif  // WIFICOND_NET_NETLINK_MANAGER_H_
289