blob: b0e7976d3227b5027ae6a1f287d62f43e63ff029 [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
22#include <base/macros.h>
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070023#include <brillo/process.h>
Peter Qiu376e4042014-11-13 09:40:28 -080024
Peter Qiufb39ba42014-11-21 09:09:59 -080025#include "apmanager/config.h"
Peter Qiubf8e36c2014-12-03 22:59:45 -080026#include "apmanager/dhcp_server_factory.h"
Peter Qiu77517302015-01-08 16:22:16 -080027#include "apmanager/file_writer.h"
Peter Qiufda548c2015-01-13 14:39:19 -080028#include "apmanager/hostapd_monitor.h"
Peter Qiu1dbf9fd2015-01-09 13:36:55 -080029#include "apmanager/process_factory.h"
Peter Qiu1ec4e7e2015-09-17 21:49:00 -070030#include "dbus_bindings/org.chromium.apmanager.Service.h"
Peter Qiu376e4042014-11-13 09:40:28 -080031
32namespace apmanager {
33
Peter Qiufb39ba42014-11-21 09:09:59 -080034class Manager;
Peter Qiu376e4042014-11-13 09:40:28 -080035
36class Service : public org::chromium::apmanager::ServiceAdaptor,
37 public org::chromium::apmanager::ServiceInterface {
38 public:
Peter Qiufb39ba42014-11-21 09:09:59 -080039 Service(Manager* manager, int service_identifier);
Peter Qiu376e4042014-11-13 09:40:28 -080040 virtual ~Service();
41
42 // Implementation of ServiceInterface.
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070043 virtual bool Start(brillo::ErrorPtr* error);
44 virtual bool Stop(brillo::ErrorPtr* error);
Peter Qiu376e4042014-11-13 09:40:28 -080045
46 // Register Service DBus object.
47 void RegisterAsync(
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070048 brillo::dbus_utils::ExportedObjectManager* object_manager,
Peter Qiuc9ce1f12014-12-05 11:14:29 -080049 const scoped_refptr<dbus::Bus>& bus,
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070050 brillo::dbus_utils::AsyncEventSequencer* sequencer);
Peter Qiu376e4042014-11-13 09:40:28 -080051
52 const dbus::ObjectPath& dbus_path() const { return dbus_path_; }
53
Peter Qiufd02b6f2015-02-27 09:55:11 -080054 int identifier() const { return identifier_; }
55
Peter Qiu376e4042014-11-13 09:40:28 -080056 private:
57 friend class ServiceTest;
58
59 static const char kHostapdPath[];
60 static const char kHostapdConfigPathFormat[];
Peter Qiu1dbf9fd2015-01-09 13:36:55 -080061 static const char kHostapdControlInterfacePath[];
Peter Qiu376e4042014-11-13 09:40:28 -080062 static const int kTerminationTimeoutSeconds;
Peter Qiue2657852015-02-03 11:33:11 -080063 static const char kStateIdle[];
64 static const char kStateStarting[];
65 static const char kStateStarted[];
66 static const char kStateFailed[];
Peter Qiu376e4042014-11-13 09:40:28 -080067
68 // Return true if hostapd process is currently running.
69 bool IsHostapdRunning();
70
71 // Start hostapd process. Return true if process is created/started
72 // successfully, false otherwise.
73 bool StartHostapdProcess(const std::string& config_file_path);
74
75 // Stop the running hostapd process. Sending it a SIGTERM signal first, then
76 // a SIGKILL if failed to terminated with SIGTERM.
77 void StopHostapdProcess();
78
Peter Qiubf8e36c2014-12-03 22:59:45 -080079 // Release resources allocated to this service.
80 void ReleaseResources();
81
Peter Qiufda548c2015-01-13 14:39:19 -080082 void HostapdEventCallback(HostapdMonitor::Event event,
83 const std::string& data);
84
Peter Qiufb39ba42014-11-21 09:09:59 -080085 Manager* manager_;
Peter Qiufd02b6f2015-02-27 09:55:11 -080086 int identifier_;
Peter Qiu376e4042014-11-13 09:40:28 -080087 std::string service_path_;
88 dbus::ObjectPath dbus_path_;
89 std::unique_ptr<Config> config_;
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070090 std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
91 std::unique_ptr<brillo::Process> hostapd_process_;
Peter Qiubf8e36c2014-12-03 22:59:45 -080092 std::unique_ptr<DHCPServer> dhcp_server_;
93 DHCPServerFactory* dhcp_server_factory_;
Peter Qiu77517302015-01-08 16:22:16 -080094 FileWriter* file_writer_;
Peter Qiu1dbf9fd2015-01-09 13:36:55 -080095 ProcessFactory* process_factory_;
Peter Qiufda548c2015-01-13 14:39:19 -080096 std::unique_ptr<HostapdMonitor> hostapd_monitor_;
Peter Qiu376e4042014-11-13 09:40:28 -080097
98 DISALLOW_COPY_AND_ASSIGN(Service);
99};
100
101} // namespace apmanager
102
103#endif // APMANAGER_SERVICE_H_