blob: 04870b44e336059728c3f9190ebc9b19d6603ebb [file] [log] [blame]
Arman Ugurayfe65fb72015-07-24 19:14:42 -07001//
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07002// Copyright 2015 Google, Inc.
Arman Ugurayfe65fb72015-07-24 19:14:42 -07003//
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//
16
17#include "service/ipc/ipc_manager.h"
18
Jakub Pawlowski470adc82016-02-22 12:57:31 -080019#if !defined(OS_GENERIC)
Arman Ugurayb2286f32015-08-05 21:19:02 -070020#include "service/ipc/binder/ipc_handler_binder.h"
Jakub Pawlowski79c2ff92016-10-31 12:56:12 -070021#else
22#include "service/ipc/dbus/ipc_handler_dbus.h"
Jakub Pawlowski470adc82016-02-22 12:57:31 -080023#endif // !defined(OS_GENERIC)
Scott James Remnante415c052015-09-23 10:52:07 -070024#include "service/ipc/ipc_handler_linux.h"
Arman Ugurayfe65fb72015-07-24 19:14:42 -070025
26namespace ipc {
27
Myles Watson911d1ae2016-11-28 16:44:40 -080028IPCManager::IPCManager(bluetooth::Adapter* adapter) : adapter_(adapter) {
Arman Ugurayd6a4b0c2015-08-13 17:01:07 -070029 CHECK(adapter_);
Arman Ugurayfe65fb72015-07-24 19:14:42 -070030}
31
32IPCManager::~IPCManager() {
Arman Uguraye0d08c92015-08-04 18:59:27 -070033 // Don't rely on the handlers getting destroyed since another thread might be
34 // holding a reference to them. Instead, explicitly stop them here.
Myles Watson911d1ae2016-11-28 16:44:40 -080035 if (BinderStarted()) binder_handler_->Stop();
36 if (LinuxStarted()) linux_handler_->Stop();
Jakub Pawlowski79c2ff92016-10-31 12:56:12 -070037 if (DBusStarted()) dbus_handler_->Stop();
Arman Ugurayfe65fb72015-07-24 19:14:42 -070038}
39
Arman Uguraye0d08c92015-08-04 18:59:27 -070040bool IPCManager::Start(Type type, Delegate* delegate) {
Arman Ugurayfe65fb72015-07-24 19:14:42 -070041 switch (type) {
Myles Watson911d1ae2016-11-28 16:44:40 -080042 case TYPE_LINUX:
43 if (LinuxStarted()) {
44 LOG(ERROR) << "IPCManagerLinux already started.";
45 return false;
46 }
Arman Ugurayb2286f32015-08-05 21:19:02 -070047
Myles Watson911d1ae2016-11-28 16:44:40 -080048 linux_handler_ = new IPCHandlerLinux(adapter_, delegate);
49 if (!linux_handler_->Run()) {
50 linux_handler_ = nullptr;
51 return false;
52 }
53 return true;
Jakub Pawlowski79c2ff92016-10-31 12:56:12 -070054
Jakub Pawlowski470adc82016-02-22 12:57:31 -080055#if !defined(OS_GENERIC)
Myles Watson911d1ae2016-11-28 16:44:40 -080056 case TYPE_BINDER:
57 if (BinderStarted()) {
58 LOG(ERROR) << "IPCManagerBinder already started.";
59 return false;
60 }
Arman Ugurayb2286f32015-08-05 21:19:02 -070061
Myles Watson911d1ae2016-11-28 16:44:40 -080062 binder_handler_ = new IPCHandlerBinder(adapter_, delegate);
63 if (!binder_handler_->Run()) {
64 binder_handler_ = nullptr;
65 return false;
66 }
67 return true;
Jakub Pawlowski79c2ff92016-10-31 12:56:12 -070068#else
69 case TYPE_DBUS:
70 if (DBusStarted()) {
71 LOG(ERROR) << "IPCManagerDBus already started.";
72 return false;
73 }
74
75 dbus_handler_ = new IPCHandlerDBus(adapter_, delegate);
76 if (!dbus_handler_->Run()) {
77 dbus_handler_ = nullptr;
78 return false;
79 }
80 return true;
Jakub Pawlowski470adc82016-02-22 12:57:31 -080081#endif // !defined(OS_GENERIC)
Jakub Pawlowski79c2ff92016-10-31 12:56:12 -070082
Myles Watson911d1ae2016-11-28 16:44:40 -080083 default:
84 LOG(ERROR) << "Unsupported IPC type given: " << type;
Arman Ugurayfe65fb72015-07-24 19:14:42 -070085 }
86
87 return false;
88}
89
Myles Watson911d1ae2016-11-28 16:44:40 -080090bool IPCManager::BinderStarted() const { return binder_handler_.get(); }
Arman Ugurayfe65fb72015-07-24 19:14:42 -070091
Myles Watson911d1ae2016-11-28 16:44:40 -080092bool IPCManager::LinuxStarted() const { return linux_handler_.get(); }
Arman Ugurayfe65fb72015-07-24 19:14:42 -070093
Jakub Pawlowski79c2ff92016-10-31 12:56:12 -070094bool IPCManager::DBusStarted() const { return dbus_handler_.get(); }
95
Arman Ugurayfe65fb72015-07-24 19:14:42 -070096} // namespace ipc