blob: a290533c0dc8f6b093425efa407fc56872dfff7a [file] [log] [blame]
Peter Qiu376e4042014-11-13 09:40:28 -08001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef APMANAGER_SERVICE_H_
6#define APMANAGER_SERVICE_H_
7
8#include <string>
9
10#include <base/macros.h>
11#include <chromeos/process.h>
12
Peter Qiufb39ba42014-11-21 09:09:59 -080013#include "apmanager/config.h"
Peter Qiu376e4042014-11-13 09:40:28 -080014#include "apmanager/dbus_adaptors/org.chromium.apmanager.Service.h"
Peter Qiubf8e36c2014-12-03 22:59:45 -080015#include "apmanager/dhcp_server_factory.h"
Peter Qiu77517302015-01-08 16:22:16 -080016#include "apmanager/file_writer.h"
Peter Qiufda548c2015-01-13 14:39:19 -080017#include "apmanager/hostapd_monitor.h"
Peter Qiu1dbf9fd2015-01-09 13:36:55 -080018#include "apmanager/process_factory.h"
Peter Qiu376e4042014-11-13 09:40:28 -080019
20namespace apmanager {
21
Peter Qiufb39ba42014-11-21 09:09:59 -080022class Manager;
Peter Qiu376e4042014-11-13 09:40:28 -080023
24class Service : public org::chromium::apmanager::ServiceAdaptor,
25 public org::chromium::apmanager::ServiceInterface {
26 public:
Peter Qiufb39ba42014-11-21 09:09:59 -080027 Service(Manager* manager, int service_identifier);
Peter Qiu376e4042014-11-13 09:40:28 -080028 virtual ~Service();
29
30 // Implementation of ServiceInterface.
31 virtual bool Start(chromeos::ErrorPtr* error);
32 virtual bool Stop(chromeos::ErrorPtr* error);
33
34 // Register Service DBus object.
35 void RegisterAsync(
36 chromeos::dbus_utils::ExportedObjectManager* object_manager,
Peter Qiuc9ce1f12014-12-05 11:14:29 -080037 const scoped_refptr<dbus::Bus>& bus,
Peter Qiu376e4042014-11-13 09:40:28 -080038 chromeos::dbus_utils::AsyncEventSequencer* sequencer);
39
40 const dbus::ObjectPath& dbus_path() const { return dbus_path_; }
41
42 private:
43 friend class ServiceTest;
44
45 static const char kHostapdPath[];
46 static const char kHostapdConfigPathFormat[];
Peter Qiu1dbf9fd2015-01-09 13:36:55 -080047 static const char kHostapdControlInterfacePath[];
Peter Qiu376e4042014-11-13 09:40:28 -080048 static const int kTerminationTimeoutSeconds;
49
50 // Return true if hostapd process is currently running.
51 bool IsHostapdRunning();
52
53 // Start hostapd process. Return true if process is created/started
54 // successfully, false otherwise.
55 bool StartHostapdProcess(const std::string& config_file_path);
56
57 // Stop the running hostapd process. Sending it a SIGTERM signal first, then
58 // a SIGKILL if failed to terminated with SIGTERM.
59 void StopHostapdProcess();
60
Peter Qiubf8e36c2014-12-03 22:59:45 -080061 // Release resources allocated to this service.
62 void ReleaseResources();
63
Peter Qiufda548c2015-01-13 14:39:19 -080064 void HostapdEventCallback(HostapdMonitor::Event event,
65 const std::string& data);
66
Peter Qiufb39ba42014-11-21 09:09:59 -080067 Manager* manager_;
Peter Qiu376e4042014-11-13 09:40:28 -080068 int service_identifier_;
69 std::string service_path_;
70 dbus::ObjectPath dbus_path_;
71 std::unique_ptr<Config> config_;
72 std::unique_ptr<chromeos::dbus_utils::DBusObject> dbus_object_;
73 std::unique_ptr<chromeos::Process> hostapd_process_;
Peter Qiubf8e36c2014-12-03 22:59:45 -080074 std::unique_ptr<DHCPServer> dhcp_server_;
75 DHCPServerFactory* dhcp_server_factory_;
Peter Qiu77517302015-01-08 16:22:16 -080076 FileWriter* file_writer_;
Peter Qiu1dbf9fd2015-01-09 13:36:55 -080077 ProcessFactory* process_factory_;
Peter Qiufda548c2015-01-13 14:39:19 -080078 std::unique_ptr<HostapdMonitor> hostapd_monitor_;
Peter Qiu376e4042014-11-13 09:40:28 -080079
80 DISALLOW_COPY_AND_ASSIGN(Service);
81};
82
83} // namespace apmanager
84
85#endif // APMANAGER_SERVICE_H_