2 // Copyright (C) 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "service/ipc/binder/ipc_handler_binder.h"
19 #include <base/bind.h>
20 #include <base/logging.h>
21 #include <base/message_loop/message_loop.h>
22 #include <binder/IPCThreadState.h>
23 #include <binder/IServiceManager.h>
24 #include <binder/ProcessState.h>
26 #include "service/ipc/binder/bluetooth_binder_server.h"
28 using android::defaultServiceManager;
30 using android::status_t;
31 using android::String16;
35 IPCHandlerBinder::IPCHandlerBinder(
36 bluetooth::Adapter* adapter,
37 IPCManager::Delegate* delegate)
38 : IPCHandler(adapter, delegate) {
41 IPCHandlerBinder::~IPCHandlerBinder() {
44 bool IPCHandlerBinder::Run() {
47 // Register the IBluetooth service with the Android ServiceManager.
48 android::sp<binder::BluetoothBinderServer> bt_server =
49 new binder::BluetoothBinderServer(adapter());
50 status_t status = defaultServiceManager()->addService(
51 String16(binder::IBluetooth::kBluetoothServiceName),
53 if (status != android::NO_ERROR) {
54 LOG(ERROR) << "Failed to register Bluetooth service with ServiceManager";
58 // Notify the delegate. We do this in the message loop to avoid reentrancy.
60 base::MessageLoop::current()->task_runner()->PostTask(
62 base::Bind(&IPCHandlerBinder::NotifyStarted, this));
65 android::ProcessState::self()->startThreadPool();
70 void IPCHandlerBinder::Stop() {
71 // TODO(armansito): There are several methods in android::IPCThreadState that
72 // are related to shutting down the threadpool, however I haven't been able to
73 // make them shut things down cleanly. Figure out the right way to stop the
77 void IPCHandlerBinder::NotifyStarted() {
79 delegate()->OnIPCHandlerStarted(IPCManager::TYPE_BINDER);