Peter Qiu | 326b6cf | 2015-09-02 11:11:42 -0700 | [diff] [blame^] | 1 | // |
| 2 | // Copyright (C) 2015 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 | // |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 16 | |
| 17 | #ifndef APMANAGER_HOSTAPD_MONITOR_H_ |
| 18 | #define APMANAGER_HOSTAPD_MONITOR_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | #include <base/cancelable_callback.h> |
| 23 | #include <base/macros.h> |
Peter Qiu | e265785 | 2015-02-03 11:33:11 -0800 | [diff] [blame] | 24 | #include <base/memory/weak_ptr.h> |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 25 | |
| 26 | #include "apmanager/event_dispatcher.h" |
| 27 | |
| 28 | namespace shill { |
| 29 | |
| 30 | struct InputData; |
| 31 | class IOHandler; |
| 32 | class IOHandlerFactory; |
| 33 | class Sockets; |
| 34 | |
| 35 | } // namespace shill |
| 36 | |
| 37 | namespace apmanager { |
| 38 | |
| 39 | // Class for monitoring events from hostapd control interface. |
| 40 | class HostapdMonitor { |
| 41 | public: |
| 42 | enum Event { |
Peter Qiu | e265785 | 2015-02-03 11:33:11 -0800 | [diff] [blame] | 43 | kHostapdFailed, |
| 44 | kHostapdStarted, |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 45 | kStationConnected, |
| 46 | kStationDisconnected, |
| 47 | }; |
| 48 | |
| 49 | typedef base::Callback<void(Event event, const std::string& data)> |
| 50 | EventCallback; |
| 51 | |
| 52 | HostapdMonitor(const EventCallback& callback_, |
| 53 | const std::string& control_interface_path, |
| 54 | const std::string& network_interface_name); |
| 55 | virtual ~HostapdMonitor(); |
| 56 | |
| 57 | virtual void Start(); |
| 58 | |
| 59 | private: |
| 60 | friend class HostapdMonitorTest; |
| 61 | |
| 62 | static const char kLocalPathFormat[]; |
| 63 | static const char kHostapdCmdAttach[]; |
| 64 | static const char kHostapdRespOk[]; |
| 65 | static const char kHostapdEventStationConnected[]; |
| 66 | static const char kHostapdEventStationDisconnected[]; |
| 67 | static const int kHostapdCtrlIfaceCheckIntervalMs; |
| 68 | static const int kHostapdCtrlIfaceCheckMaxAttempts; |
| 69 | static const int kHostapdAttachTimeoutMs; |
| 70 | static const int kInvalidSocket; |
| 71 | |
| 72 | // Task for checking if hostapd control interface is up or not. |
| 73 | void HostapdCtrlIfaceCheckTask(); |
| 74 | |
| 75 | // Attach to hostapd control interface to receive unsolicited event |
| 76 | // notifications. |
| 77 | void AttachToHostapd(); |
| 78 | void AttachTimeoutHandler(); |
| 79 | |
| 80 | bool SendMessage(const char* message, size_t length); |
| 81 | void ParseMessage(shill::InputData* data); |
| 82 | void OnReadError(const std::string& error_msg); |
| 83 | |
| 84 | std::unique_ptr<shill::Sockets> sockets_; |
| 85 | EventCallback event_callback_; |
| 86 | |
| 87 | // File path for interprocess communication with hostapd. |
| 88 | std::string dest_path_; |
| 89 | std::string local_path_; |
| 90 | |
| 91 | // Socket descriptor for communication with hostapd. |
| 92 | int hostapd_socket_; |
| 93 | |
| 94 | base::Callback<void(shill::InputData *)> hostapd_callback_; |
| 95 | std::unique_ptr<shill::IOHandler> hostapd_input_handler_; |
| 96 | shill::IOHandlerFactory *io_handler_factory_; |
| 97 | EventDispatcher* event_dispatcher_; |
Peter Qiu | e265785 | 2015-02-03 11:33:11 -0800 | [diff] [blame] | 98 | base::WeakPtrFactory<HostapdMonitor> weak_ptr_factory_; |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 99 | |
| 100 | int hostapd_ctrl_iface_check_count_; |
| 101 | base::CancelableClosure attach_timeout_callback_; |
| 102 | |
| 103 | bool started_; |
| 104 | |
| 105 | DISALLOW_COPY_AND_ASSIGN(HostapdMonitor); |
| 106 | }; |
| 107 | |
| 108 | } // namespace apmanager |
| 109 | |
| 110 | #endif // APMANAGER_HOSTAPD_MONITOR_H_ |