1 //
2 // Copyright (C) 2015 Google, Inc.
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 //
16
17 #include "service/ipc/ipc_manager.h"
18
19 #include "service/ipc/binder/ipc_handler_binder.h"
20 #include "service/ipc/ipc_handler_linux.h"
21
22 namespace ipc {
23
IPCManager(bluetooth::Adapter * adapter)24 IPCManager::IPCManager(bluetooth::Adapter* adapter)
25 : adapter_(adapter) {
26 CHECK(adapter_);
27 }
28
~IPCManager()29 IPCManager::~IPCManager() {
30 // Don't rely on the handlers getting destroyed since another thread might be
31 // holding a reference to them. Instead, explicitly stop them here.
32 if (BinderStarted())
33 binder_handler_->Stop();
34 if (LinuxStarted())
35 linux_handler_->Stop();
36 }
37
Start(Type type,Delegate * delegate)38 bool IPCManager::Start(Type type, Delegate* delegate) {
39 switch (type) {
40 case TYPE_LINUX:
41 if (LinuxStarted()) {
42 LOG(ERROR) << "IPCManagerLinux already started.";
43 return false;
44 }
45
46 linux_handler_ = new IPCHandlerLinux(adapter_, delegate);
47 if (!linux_handler_->Run()) {
48 linux_handler_ = nullptr;
49 return false;
50 }
51 return true;
52
53 case TYPE_BINDER:
54 if (BinderStarted()) {
55 LOG(ERROR) << "IPCManagerBinder already started.";
56 return false;
57 }
58
59 binder_handler_ = new IPCHandlerBinder(adapter_, delegate);
60 if (!binder_handler_->Run()) {
61 binder_handler_ = nullptr;
62 return false;
63 }
64 return true;
65
66 default:
67 LOG(ERROR) << "Unsupported IPC type given: " << type;
68 }
69
70 return false;
71 }
72
BinderStarted() const73 bool IPCManager::BinderStarted() const {
74 return binder_handler_.get();
75 }
76
LinuxStarted() const77 bool IPCManager::LinuxStarted() const {
78 return linux_handler_.get();
79 }
80
81 } // namespace ipc
82