blob: e1b6a057ba6f5b56688d3d35c8acadcfb8c4449f [file] [log] [blame]
Peter Qiu326b6cf2015-09-02 11:11:42 -07001//
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//
Peter Qiu376e4042014-11-13 09:40:28 -080016
17#ifndef APMANAGER_SERVICE_H_
18#define APMANAGER_SERVICE_H_
19
20#include <string>
21
Peter Qiu2ea547f2015-11-20 14:18:11 -080022#include <base/callback.h>
Peter Qiu376e4042014-11-13 09:40:28 -080023#include <base/macros.h>
Peter Qiu7758d8d2015-11-23 14:27:33 -080024#include <base/memory/ref_counted.h>
Peter Qiucbbefa22015-10-27 12:05:34 -070025#include <base/memory/weak_ptr.h>
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070026#include <brillo/process.h>
Peter Qiu376e4042014-11-13 09:40:28 -080027
Peter Qiufb39ba42014-11-21 09:09:59 -080028#include "apmanager/config.h"
Peter Qiubf8e36c2014-12-03 22:59:45 -080029#include "apmanager/dhcp_server_factory.h"
Peter Qiu2ea547f2015-11-20 14:18:11 -080030#include "apmanager/error.h"
Peter Qiu77517302015-01-08 16:22:16 -080031#include "apmanager/file_writer.h"
Peter Qiufda548c2015-01-13 14:39:19 -080032#include "apmanager/hostapd_monitor.h"
Peter Qiu1dbf9fd2015-01-09 13:36:55 -080033#include "apmanager/process_factory.h"
Peter Qiu2ea547f2015-11-20 14:18:11 -080034#include "apmanager/service_adaptor_interface.h"
Peter Qiu376e4042014-11-13 09:40:28 -080035
36namespace apmanager {
37
Peter Qiufb39ba42014-11-21 09:09:59 -080038class Manager;
Peter Qiu376e4042014-11-13 09:40:28 -080039
Peter Qiucbbefa22015-10-27 12:05:34 -070040#if defined(__BRILLO__)
41class EventDispatcher;
42#endif // __BRILLO__
43
Peter Qiu7758d8d2015-11-23 14:27:33 -080044class Service : public base::RefCounted<Service> {
Peter Qiu376e4042014-11-13 09:40:28 -080045 public:
Peter Qiufb39ba42014-11-21 09:09:59 -080046 Service(Manager* manager, int service_identifier);
Peter Qiu376e4042014-11-13 09:40:28 -080047 virtual ~Service();
48
Peter Qiu2ea547f2015-11-20 14:18:11 -080049 void Start(const base::Callback<void(const Error&)>& result_callback);
50 bool Stop(Error* error);
Peter Qiu376e4042014-11-13 09:40:28 -080051
Peter Qiufd02b6f2015-02-27 09:55:11 -080052 int identifier() const { return identifier_; }
53
Peter Qiu2ea547f2015-11-20 14:18:11 -080054 ServiceAdaptorInterface* adaptor() const { return adaptor_.get(); }
55
56 Config* config() const { return config_.get(); }
57
Peter Qiu376e4042014-11-13 09:40:28 -080058 private:
59 friend class ServiceTest;
60
61 static const char kHostapdPath[];
62 static const char kHostapdConfigPathFormat[];
Peter Qiu1dbf9fd2015-01-09 13:36:55 -080063 static const char kHostapdControlInterfacePath[];
Peter Qiu376e4042014-11-13 09:40:28 -080064 static const int kTerminationTimeoutSeconds;
Peter Qiue2657852015-02-03 11:33:11 -080065 static const char kStateIdle[];
66 static const char kStateStarting[];
67 static const char kStateStarted[];
68 static const char kStateFailed[];
Peter Qiu376e4042014-11-13 09:40:28 -080069
Peter Qiucbbefa22015-10-27 12:05:34 -070070#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,
Peter Qiu2ea547f2015-11-20 14:18:11 -080079 const base::Callback<void(const Error&)>& result_callback);
Peter Qiucbbefa22015-10-27 12:05:34 -070080
81 // Handle asynchronous service start failures.
82 void HandleStartFailure();
83#endif // __BRILLO__
84
Peter Qiu2ea547f2015-11-20 14:18:11 -080085 bool StartInternal(Error* error);
Peter Qiu682a9322015-10-27 09:41:52 -070086
Peter Qiu376e4042014-11-13 09:40:28 -080087 // 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
Peter Qiubf8e36c2014-12-03 22:59:45 -080098 // Release resources allocated to this service.
99 void ReleaseResources();
100
Peter Qiufda548c2015-01-13 14:39:19 -0800101 void HostapdEventCallback(HostapdMonitor::Event event,
102 const std::string& data);
103
Peter Qiufb39ba42014-11-21 09:09:59 -0800104 Manager* manager_;
Peter Qiufd02b6f2015-02-27 09:55:11 -0800105 int identifier_;
Peter Qiu376e4042014-11-13 09:40:28 -0800106 std::unique_ptr<Config> config_;
Peter Qiu2ea547f2015-11-20 14:18:11 -0800107 std::unique_ptr<ServiceAdaptorInterface> adaptor_;
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700108 std::unique_ptr<brillo::Process> hostapd_process_;
Peter Qiubf8e36c2014-12-03 22:59:45 -0800109 std::unique_ptr<DHCPServer> dhcp_server_;
110 DHCPServerFactory* dhcp_server_factory_;
Peter Qiu77517302015-01-08 16:22:16 -0800111 FileWriter* file_writer_;
Peter Qiu1dbf9fd2015-01-09 13:36:55 -0800112 ProcessFactory* process_factory_;
Peter Qiufda548c2015-01-13 14:39:19 -0800113 std::unique_ptr<HostapdMonitor> hostapd_monitor_;
Peter Qiucbbefa22015-10-27 12:05:34 -0700114#if defined(__BRILLO__)
115 EventDispatcher* event_dispatcher_;
116 bool start_in_progress_;
117#endif // __BRILLO__
Peter Qiu376e4042014-11-13 09:40:28 -0800118
Peter Qiucbbefa22015-10-27 12:05:34 -0700119 base::WeakPtrFactory<Service> weak_factory_{this};
Peter Qiu376e4042014-11-13 09:40:28 -0800120 DISALLOW_COPY_AND_ASSIGN(Service);
121};
122
123} // namespace apmanager
124
125#endif // APMANAGER_SERVICE_H_