blob: a7cecacb440966abeb0e5b9c29f050ef5dae07cd [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 Qiu1dbf9fd2015-01-09 13:36:55 -080017#include "apmanager/process_factory.h"
Peter Qiu376e4042014-11-13 09:40:28 -080018
19namespace apmanager {
20
Peter Qiufb39ba42014-11-21 09:09:59 -080021class Manager;
Peter Qiu376e4042014-11-13 09:40:28 -080022
23class Service : public org::chromium::apmanager::ServiceAdaptor,
24 public org::chromium::apmanager::ServiceInterface {
25 public:
Peter Qiufb39ba42014-11-21 09:09:59 -080026 Service(Manager* manager, int service_identifier);
Peter Qiu376e4042014-11-13 09:40:28 -080027 virtual ~Service();
28
29 // Implementation of ServiceInterface.
30 virtual bool Start(chromeos::ErrorPtr* error);
31 virtual bool Stop(chromeos::ErrorPtr* error);
32
33 // Register Service DBus object.
34 void RegisterAsync(
35 chromeos::dbus_utils::ExportedObjectManager* object_manager,
Peter Qiuc9ce1f12014-12-05 11:14:29 -080036 const scoped_refptr<dbus::Bus>& bus,
Peter Qiu376e4042014-11-13 09:40:28 -080037 chromeos::dbus_utils::AsyncEventSequencer* sequencer);
38
39 const dbus::ObjectPath& dbus_path() const { return dbus_path_; }
40
41 private:
42 friend class ServiceTest;
43
44 static const char kHostapdPath[];
45 static const char kHostapdConfigPathFormat[];
Peter Qiu1dbf9fd2015-01-09 13:36:55 -080046 static const char kHostapdControlInterfacePath[];
Peter Qiu376e4042014-11-13 09:40:28 -080047 static const int kTerminationTimeoutSeconds;
48
49 // Return true if hostapd process is currently running.
50 bool IsHostapdRunning();
51
52 // Start hostapd process. Return true if process is created/started
53 // successfully, false otherwise.
54 bool StartHostapdProcess(const std::string& config_file_path);
55
56 // Stop the running hostapd process. Sending it a SIGTERM signal first, then
57 // a SIGKILL if failed to terminated with SIGTERM.
58 void StopHostapdProcess();
59
Peter Qiubf8e36c2014-12-03 22:59:45 -080060 // Release resources allocated to this service.
61 void ReleaseResources();
62
Peter Qiufb39ba42014-11-21 09:09:59 -080063 Manager* manager_;
Peter Qiu376e4042014-11-13 09:40:28 -080064 int service_identifier_;
65 std::string service_path_;
66 dbus::ObjectPath dbus_path_;
67 std::unique_ptr<Config> config_;
68 std::unique_ptr<chromeos::dbus_utils::DBusObject> dbus_object_;
69 std::unique_ptr<chromeos::Process> hostapd_process_;
Peter Qiubf8e36c2014-12-03 22:59:45 -080070 std::unique_ptr<DHCPServer> dhcp_server_;
71 DHCPServerFactory* dhcp_server_factory_;
Peter Qiu77517302015-01-08 16:22:16 -080072 FileWriter* file_writer_;
Peter Qiu1dbf9fd2015-01-09 13:36:55 -080073 ProcessFactory* process_factory_;
Peter Qiu376e4042014-11-13 09:40:28 -080074
75 DISALLOW_COPY_AND_ASSIGN(Service);
76};
77
78} // namespace apmanager
79
80#endif // APMANAGER_SERVICE_H_