1//
2// Copyright (C) 2014 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//
16
17#ifndef APMANAGER_SERVICE_H_
18#define APMANAGER_SERVICE_H_
19
20#include <string>
21
22#include <base/callback.h>
23#include <base/macros.h>
24#include <base/memory/ref_counted.h>
25#include <base/memory/weak_ptr.h>
26#include <brillo/process.h>
27
28#include "apmanager/config.h"
29#include "apmanager/dhcp_server_factory.h"
30#include "apmanager/error.h"
31#include "apmanager/file_writer.h"
32#include "apmanager/hostapd_monitor.h"
33#include "apmanager/process_factory.h"
34#include "apmanager/service_adaptor_interface.h"
35
36namespace apmanager {
37
38class Manager;
39
40#if defined(__BRILLO__)
41class EventDispatcher;
42#endif  // __BRILLO__
43
44class Service : public base::RefCounted<Service> {
45 public:
46  Service(Manager* manager, int service_identifier);
47  virtual ~Service();
48
49  void Start(const base::Callback<void(const Error&)>& result_callback);
50  bool Stop(Error* error);
51
52  int identifier() const { return identifier_; }
53
54  ServiceAdaptorInterface* adaptor() const { return adaptor_.get(); }
55
56  Config* config() const { return config_.get(); }
57
58 private:
59  friend class ServiceTest;
60
61  static const char kHostapdPath[];
62  static const char kHostapdConfigPathFormat[];
63  static const char kHostapdControlInterfacePath[];
64  static const int kTerminationTimeoutSeconds;
65  static const char kStateIdle[];
66  static const char kStateStarting[];
67  static const char kStateStarted[];
68  static const char kStateFailed[];
69
70#if defined(__BRILLO__)
71  static const int kAPInterfaceCheckIntervalMilliseconds;
72  static const int kAPInterfaceCheckMaxAttempts;
73
74  // Task to check enumeration status of the specified AP interface
75  // |interface_name|.
76  void APInterfaceCheckTask(
77      const std::string& interface_name,
78      int check_count,
79      const base::Callback<void(const Error&)>& result_callback);
80
81  // Handle asynchronous service start failures.
82  void HandleStartFailure();
83#endif  // __BRILLO__
84
85  bool StartInternal(Error* error);
86
87  // Return true if hostapd process is currently running.
88  bool IsHostapdRunning();
89
90  // Start hostapd process. Return true if process is created/started
91  // successfully, false otherwise.
92  bool StartHostapdProcess(const std::string& config_file_path);
93
94  // Stop the running hostapd process. Sending it a SIGTERM signal first, then
95  // a SIGKILL if failed to terminated with SIGTERM.
96  void StopHostapdProcess();
97
98  // Release resources allocated to this service.
99  void ReleaseResources();
100
101  void HostapdEventCallback(HostapdMonitor::Event event,
102                            const std::string& data);
103
104  Manager* manager_;
105  int identifier_;
106  std::unique_ptr<Config> config_;
107  std::unique_ptr<ServiceAdaptorInterface> adaptor_;
108  std::unique_ptr<brillo::Process> hostapd_process_;
109  std::unique_ptr<DHCPServer> dhcp_server_;
110  DHCPServerFactory* dhcp_server_factory_;
111  FileWriter* file_writer_;
112  ProcessFactory* process_factory_;
113  std::unique_ptr<HostapdMonitor> hostapd_monitor_;
114#if defined(__BRILLO__)
115  EventDispatcher* event_dispatcher_;
116  bool start_in_progress_;
117#endif  // __BRILLO__
118
119  base::WeakPtrFactory<Service> weak_factory_{this};
120  DISALLOW_COPY_AND_ASSIGN(Service);
121};
122
123}  // namespace apmanager
124
125#endif  // APMANAGER_SERVICE_H_
126