Home | History | Annotate | Download | only in binder
      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/binder/ipc_handler_binder.h"
     18 
     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>
     25 
     26 #include "service/ipc/binder/bluetooth_binder_server.h"
     27 
     28 using android::defaultServiceManager;
     29 using android::sp;
     30 using android::status_t;
     31 using android::String8;
     32 using android::String16;
     33 
     34 namespace ipc {
     35 
     36 std::string kServiceName = "bluetooth-service";
     37 
     38 IPCHandlerBinder::IPCHandlerBinder(bluetooth::Adapter* adapter,
     39                                    IPCManager::Delegate* delegate)
     40     : IPCHandler(adapter, delegate) {}
     41 
     42 IPCHandlerBinder::~IPCHandlerBinder() {}
     43 
     44 bool IPCHandlerBinder::Run() {
     45   CHECK(adapter());
     46 
     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(String8(kServiceName.c_str())), bt_server);
     52   if (status != android::NO_ERROR) {
     53     LOG(ERROR) << "Failed to register Bluetooth service with ServiceManager";
     54     return false;
     55   }
     56 
     57   // Notify the delegate. We do this in the message loop to avoid reentrancy.
     58   if (delegate()) {
     59     base::MessageLoop::current()->task_runner()->PostTask(
     60         FROM_HERE, base::Bind(&IPCHandlerBinder::NotifyStarted, this));
     61   }
     62 
     63   android::ProcessState::self()->startThreadPool();
     64 
     65   return true;
     66 }
     67 
     68 void IPCHandlerBinder::Stop() {
     69   // TODO(armansito): There are several methods in android::IPCThreadState that
     70   // are related to shutting down the threadpool, however I haven't been able to
     71   // make them shut things down cleanly. Figure out the right way to stop the
     72   // Binder IPC here.
     73 }
     74 
     75 void IPCHandlerBinder::NotifyStarted() {
     76   if (delegate()) delegate()->OnIPCHandlerStarted(IPCManager::TYPE_BINDER);
     77 }
     78 
     79 }  // namespace ipc
     80