summaryrefslogtreecommitdiff
path: root/hostapd_monitor.h
blob: b5f008210e9f2085c28fb9eeba8f5b57d0a27a49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#ifndef APMANAGER_HOSTAPD_MONITOR_H_
#define APMANAGER_HOSTAPD_MONITOR_H_

#include <string>

#include <base/cancelable_callback.h>
#include <base/macros.h>
#include <base/memory/weak_ptr.h>

#include "apmanager/event_dispatcher.h"

namespace shill {

struct InputData;
class IOHandler;
class IOHandlerFactory;
class Sockets;

}  // namespace shill

namespace apmanager {

// Class for monitoring events from hostapd control interface.
class HostapdMonitor {
 public:
  enum Event {
    kHostapdFailed,
    kHostapdStarted,
    kStationConnected,
    kStationDisconnected,
  };

  typedef base::Callback<void(Event event, const std::string& data)>
      EventCallback;

  HostapdMonitor(const EventCallback& callback_,
                 const std::string& control_interface_path,
                 const std::string& network_interface_name);
  virtual ~HostapdMonitor();

  virtual void Start();

 private:
  friend class HostapdMonitorTest;

  static const char kLocalPathFormat[];
  static const char kHostapdCmdAttach[];
  static const char kHostapdRespOk[];
  static const char kHostapdEventStationConnected[];
  static const char kHostapdEventStationDisconnected[];
  static const int kHostapdCtrlIfaceCheckIntervalMs;
  static const int kHostapdCtrlIfaceCheckMaxAttempts;
  static const int kHostapdAttachTimeoutMs;
  static const int kInvalidSocket;

  // Task for checking if hostapd control interface is up or not.
  void HostapdCtrlIfaceCheckTask();

  // Attach to hostapd control interface to receive unsolicited event
  // notifications.
  void AttachToHostapd();
  void AttachTimeoutHandler();

  bool SendMessage(const char* message, size_t length);
  void ParseMessage(shill::InputData* data);
  void OnReadError(const std::string& error_msg);

  std::unique_ptr<shill::Sockets> sockets_;
  EventCallback event_callback_;

  // File path for interprocess communication with hostapd.
  std::string dest_path_;
  std::string local_path_;

  // Socket descriptor for communication with hostapd.
  int hostapd_socket_;

  base::Callback<void(shill::InputData *)> hostapd_callback_;
  std::unique_ptr<shill::IOHandler> hostapd_input_handler_;
  shill::IOHandlerFactory *io_handler_factory_;
  EventDispatcher* event_dispatcher_;
  base::WeakPtrFactory<HostapdMonitor> weak_ptr_factory_;

  int hostapd_ctrl_iface_check_count_;
  base::CancelableClosure attach_timeout_callback_;

  bool started_;

  DISALLOW_COPY_AND_ASSIGN(HostapdMonitor);
};

}  // namespace apmanager

#endif  // APMANAGER_HOSTAPD_MONITOR_H_