Peter Qiu | 326b6cf | 2015-09-02 11:11:42 -0700 | [diff] [blame] | 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 | // |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 16 | |
| 17 | #ifndef APMANAGER_SERVICE_H_ |
| 18 | #define APMANAGER_SERVICE_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | #include <base/macros.h> |
Alex Vakulenko | 8d0c31b | 2015-10-13 09:14:24 -0700 | [diff] [blame^] | 23 | #include <brillo/process.h> |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 24 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 25 | #include "apmanager/config.h" |
Peter Qiu | bf8e36c | 2014-12-03 22:59:45 -0800 | [diff] [blame] | 26 | #include "apmanager/dhcp_server_factory.h" |
Peter Qiu | 7751730 | 2015-01-08 16:22:16 -0800 | [diff] [blame] | 27 | #include "apmanager/file_writer.h" |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 28 | #include "apmanager/hostapd_monitor.h" |
Peter Qiu | 1dbf9fd | 2015-01-09 13:36:55 -0800 | [diff] [blame] | 29 | #include "apmanager/process_factory.h" |
Peter Qiu | 1ec4e7e | 2015-09-17 21:49:00 -0700 | [diff] [blame] | 30 | #include "dbus_bindings/org.chromium.apmanager.Service.h" |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 31 | |
| 32 | namespace apmanager { |
| 33 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 34 | class Manager; |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 35 | |
| 36 | class Service : public org::chromium::apmanager::ServiceAdaptor, |
| 37 | public org::chromium::apmanager::ServiceInterface { |
| 38 | public: |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 39 | Service(Manager* manager, int service_identifier); |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 40 | virtual ~Service(); |
| 41 | |
| 42 | // Implementation of ServiceInterface. |
Alex Vakulenko | 8d0c31b | 2015-10-13 09:14:24 -0700 | [diff] [blame^] | 43 | virtual bool Start(brillo::ErrorPtr* error); |
| 44 | virtual bool Stop(brillo::ErrorPtr* error); |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 45 | |
| 46 | // Register Service DBus object. |
| 47 | void RegisterAsync( |
Alex Vakulenko | 8d0c31b | 2015-10-13 09:14:24 -0700 | [diff] [blame^] | 48 | brillo::dbus_utils::ExportedObjectManager* object_manager, |
Peter Qiu | c9ce1f1 | 2014-12-05 11:14:29 -0800 | [diff] [blame] | 49 | const scoped_refptr<dbus::Bus>& bus, |
Alex Vakulenko | 8d0c31b | 2015-10-13 09:14:24 -0700 | [diff] [blame^] | 50 | brillo::dbus_utils::AsyncEventSequencer* sequencer); |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 51 | |
| 52 | const dbus::ObjectPath& dbus_path() const { return dbus_path_; } |
| 53 | |
Peter Qiu | fd02b6f | 2015-02-27 09:55:11 -0800 | [diff] [blame] | 54 | int identifier() const { return identifier_; } |
| 55 | |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 56 | private: |
| 57 | friend class ServiceTest; |
| 58 | |
| 59 | static const char kHostapdPath[]; |
| 60 | static const char kHostapdConfigPathFormat[]; |
Peter Qiu | 1dbf9fd | 2015-01-09 13:36:55 -0800 | [diff] [blame] | 61 | static const char kHostapdControlInterfacePath[]; |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 62 | static const int kTerminationTimeoutSeconds; |
Peter Qiu | e265785 | 2015-02-03 11:33:11 -0800 | [diff] [blame] | 63 | static const char kStateIdle[]; |
| 64 | static const char kStateStarting[]; |
| 65 | static const char kStateStarted[]; |
| 66 | static const char kStateFailed[]; |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 67 | |
| 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 Qiu | bf8e36c | 2014-12-03 22:59:45 -0800 | [diff] [blame] | 79 | // Release resources allocated to this service. |
| 80 | void ReleaseResources(); |
| 81 | |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 82 | void HostapdEventCallback(HostapdMonitor::Event event, |
| 83 | const std::string& data); |
| 84 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 85 | Manager* manager_; |
Peter Qiu | fd02b6f | 2015-02-27 09:55:11 -0800 | [diff] [blame] | 86 | int identifier_; |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 87 | std::string service_path_; |
| 88 | dbus::ObjectPath dbus_path_; |
| 89 | std::unique_ptr<Config> config_; |
Alex Vakulenko | 8d0c31b | 2015-10-13 09:14:24 -0700 | [diff] [blame^] | 90 | std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_; |
| 91 | std::unique_ptr<brillo::Process> hostapd_process_; |
Peter Qiu | bf8e36c | 2014-12-03 22:59:45 -0800 | [diff] [blame] | 92 | std::unique_ptr<DHCPServer> dhcp_server_; |
| 93 | DHCPServerFactory* dhcp_server_factory_; |
Peter Qiu | 7751730 | 2015-01-08 16:22:16 -0800 | [diff] [blame] | 94 | FileWriter* file_writer_; |
Peter Qiu | 1dbf9fd | 2015-01-09 13:36:55 -0800 | [diff] [blame] | 95 | ProcessFactory* process_factory_; |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 96 | std::unique_ptr<HostapdMonitor> hostapd_monitor_; |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 97 | |
| 98 | DISALLOW_COPY_AND_ASSIGN(Service); |
| 99 | }; |
| 100 | |
| 101 | } // namespace apmanager |
| 102 | |
| 103 | #endif // APMANAGER_SERVICE_H_ |