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 | |
Peter Qiu | 2ea547f | 2015-11-20 14:18:11 -0800 | [diff] [blame] | 22 | #include <base/callback.h> |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 23 | #include <base/macros.h> |
Peter Qiu | 7758d8d | 2015-11-23 14:27:33 -0800 | [diff] [blame] | 24 | #include <base/memory/ref_counted.h> |
Peter Qiu | cbbefa2 | 2015-10-27 12:05:34 -0700 | [diff] [blame] | 25 | #include <base/memory/weak_ptr.h> |
Alex Vakulenko | 8d0c31b | 2015-10-13 09:14:24 -0700 | [diff] [blame] | 26 | #include <brillo/process.h> |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 27 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 28 | #include "apmanager/config.h" |
Peter Qiu | bf8e36c | 2014-12-03 22:59:45 -0800 | [diff] [blame] | 29 | #include "apmanager/dhcp_server_factory.h" |
Peter Qiu | 2ea547f | 2015-11-20 14:18:11 -0800 | [diff] [blame] | 30 | #include "apmanager/error.h" |
Peter Qiu | 7751730 | 2015-01-08 16:22:16 -0800 | [diff] [blame] | 31 | #include "apmanager/file_writer.h" |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 32 | #include "apmanager/hostapd_monitor.h" |
Peter Qiu | 1dbf9fd | 2015-01-09 13:36:55 -0800 | [diff] [blame] | 33 | #include "apmanager/process_factory.h" |
Peter Qiu | 2ea547f | 2015-11-20 14:18:11 -0800 | [diff] [blame] | 34 | #include "apmanager/service_adaptor_interface.h" |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 35 | |
| 36 | namespace apmanager { |
| 37 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 38 | class Manager; |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 39 | |
Peter Qiu | cbbefa2 | 2015-10-27 12:05:34 -0700 | [diff] [blame] | 40 | #if defined(__BRILLO__) |
| 41 | class EventDispatcher; |
| 42 | #endif // __BRILLO__ |
| 43 | |
Peter Qiu | 7758d8d | 2015-11-23 14:27:33 -0800 | [diff] [blame] | 44 | class Service : public base::RefCounted<Service> { |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 45 | public: |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 46 | Service(Manager* manager, int service_identifier); |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 47 | virtual ~Service(); |
| 48 | |
Peter Qiu | 2ea547f | 2015-11-20 14:18:11 -0800 | [diff] [blame] | 49 | void Start(const base::Callback<void(const Error&)>& result_callback); |
| 50 | bool Stop(Error* error); |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 51 | |
Peter Qiu | fd02b6f | 2015-02-27 09:55:11 -0800 | [diff] [blame] | 52 | int identifier() const { return identifier_; } |
| 53 | |
Peter Qiu | 2ea547f | 2015-11-20 14:18:11 -0800 | [diff] [blame] | 54 | ServiceAdaptorInterface* adaptor() const { return adaptor_.get(); } |
| 55 | |
| 56 | Config* config() const { return config_.get(); } |
| 57 | |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 58 | private: |
| 59 | friend class ServiceTest; |
| 60 | |
| 61 | static const char kHostapdPath[]; |
| 62 | static const char kHostapdConfigPathFormat[]; |
Peter Qiu | 1dbf9fd | 2015-01-09 13:36:55 -0800 | [diff] [blame] | 63 | static const char kHostapdControlInterfacePath[]; |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 64 | static const int kTerminationTimeoutSeconds; |
Peter Qiu | e265785 | 2015-02-03 11:33:11 -0800 | [diff] [blame] | 65 | static const char kStateIdle[]; |
| 66 | static const char kStateStarting[]; |
| 67 | static const char kStateStarted[]; |
| 68 | static const char kStateFailed[]; |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 69 | |
Peter Qiu | cbbefa2 | 2015-10-27 12:05:34 -0700 | [diff] [blame] | 70 | #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 Qiu | 2ea547f | 2015-11-20 14:18:11 -0800 | [diff] [blame] | 79 | const base::Callback<void(const Error&)>& result_callback); |
Peter Qiu | cbbefa2 | 2015-10-27 12:05:34 -0700 | [diff] [blame] | 80 | |
| 81 | // Handle asynchronous service start failures. |
| 82 | void HandleStartFailure(); |
| 83 | #endif // __BRILLO__ |
| 84 | |
Peter Qiu | 2ea547f | 2015-11-20 14:18:11 -0800 | [diff] [blame] | 85 | bool StartInternal(Error* error); |
Peter Qiu | 682a932 | 2015-10-27 09:41:52 -0700 | [diff] [blame] | 86 | |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 87 | // 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 Qiu | bf8e36c | 2014-12-03 22:59:45 -0800 | [diff] [blame] | 98 | // Release resources allocated to this service. |
| 99 | void ReleaseResources(); |
| 100 | |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 101 | void HostapdEventCallback(HostapdMonitor::Event event, |
| 102 | const std::string& data); |
| 103 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 104 | Manager* manager_; |
Peter Qiu | fd02b6f | 2015-02-27 09:55:11 -0800 | [diff] [blame] | 105 | int identifier_; |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 106 | std::unique_ptr<Config> config_; |
Peter Qiu | 2ea547f | 2015-11-20 14:18:11 -0800 | [diff] [blame] | 107 | std::unique_ptr<ServiceAdaptorInterface> adaptor_; |
Alex Vakulenko | 8d0c31b | 2015-10-13 09:14:24 -0700 | [diff] [blame] | 108 | std::unique_ptr<brillo::Process> hostapd_process_; |
Peter Qiu | bf8e36c | 2014-12-03 22:59:45 -0800 | [diff] [blame] | 109 | std::unique_ptr<DHCPServer> dhcp_server_; |
| 110 | DHCPServerFactory* dhcp_server_factory_; |
Peter Qiu | 7751730 | 2015-01-08 16:22:16 -0800 | [diff] [blame] | 111 | FileWriter* file_writer_; |
Peter Qiu | 1dbf9fd | 2015-01-09 13:36:55 -0800 | [diff] [blame] | 112 | ProcessFactory* process_factory_; |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 113 | std::unique_ptr<HostapdMonitor> hostapd_monitor_; |
Peter Qiu | cbbefa2 | 2015-10-27 12:05:34 -0700 | [diff] [blame] | 114 | #if defined(__BRILLO__) |
| 115 | EventDispatcher* event_dispatcher_; |
| 116 | bool start_in_progress_; |
| 117 | #endif // __BRILLO__ |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 118 | |
Peter Qiu | cbbefa2 | 2015-10-27 12:05:34 -0700 | [diff] [blame] | 119 | base::WeakPtrFactory<Service> weak_factory_{this}; |
Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 120 | DISALLOW_COPY_AND_ASSIGN(Service); |
| 121 | }; |
| 122 | |
| 123 | } // namespace apmanager |
| 124 | |
| 125 | #endif // APMANAGER_SERVICE_H_ |