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/common/bluetooth/binder/IBluetoothGattClient.h"
     18 
     19 #include <base/logging.h>
     20 #include <binder/Parcel.h>
     21 
     22 using android::IBinder;
     23 using android::interface_cast;
     24 using android::Parcel;
     25 using android::sp;
     26 using android::status_t;
     27 
     28 namespace ipc {
     29 namespace binder {
     30 
     31 // static
     32 const char IBluetoothGattClient::kServiceName[] =
     33     "bluetooth-gatt-client-service";
     34 
     35 // BnBluetoothGattClient (server) implementation
     36 // ========================================================
     37 
     38 status_t BnBluetoothGattClient::onTransact(
     39     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
     40   VLOG(2) << "IBluetoothGattClient: " << code;
     41   if (!data.checkInterface(this))
     42     return android::PERMISSION_DENIED;
     43 
     44   switch (code) {
     45   case REGISTER_CLIENT_TRANSACTION: {
     46     sp<IBinder> callback = data.readStrongBinder();
     47     bool result = RegisterClient(
     48         interface_cast<IBluetoothGattClientCallback>(callback));
     49     reply->writeInt32(result);
     50     return android::NO_ERROR;
     51   }
     52   case UNREGISTER_CLIENT_TRANSACTION: {
     53     int client_id = data.readInt32();
     54     UnregisterClient(client_id);
     55     return android::NO_ERROR;
     56   }
     57   case UNREGISTER_ALL_TRANSACTION: {
     58     UnregisterAll();
     59     return android::NO_ERROR;
     60   }
     61   default:
     62     return BBinder::onTransact(code, data, reply, flags);
     63   }
     64 }
     65 
     66 // BpBluetoothGattClient (client) implementation
     67 // ========================================================
     68 
     69 BpBluetoothGattClient::BpBluetoothGattClient(const sp<IBinder>& impl)
     70     : BpInterface<IBluetoothGattClient>(impl) {
     71 }
     72 
     73 bool BpBluetoothGattClient::RegisterClient(
     74     const android::sp<IBluetoothGattClientCallback>& callback) {
     75   Parcel data, reply;
     76 
     77   data.writeInterfaceToken(IBluetoothGattClient::getInterfaceDescriptor());
     78   data.writeStrongBinder(IInterface::asBinder(callback.get()));
     79 
     80   remote()->transact(IBluetoothGattClient::REGISTER_CLIENT_TRANSACTION,
     81                      data, &reply);
     82 
     83   return reply.readInt32();
     84 }
     85 
     86 void BpBluetoothGattClient::UnregisterClient(int client_id) {
     87   Parcel data, reply;
     88 
     89   data.writeInterfaceToken(IBluetoothGattClient::getInterfaceDescriptor());
     90   data.writeInt32(client_id);
     91 
     92   remote()->transact(IBluetoothGattClient::UNREGISTER_CLIENT_TRANSACTION,
     93                      data, &reply);
     94 }
     95 
     96 void BpBluetoothGattClient::UnregisterAll() {
     97   Parcel data, reply;
     98 
     99   data.writeInterfaceToken(IBluetoothGattClient::getInterfaceDescriptor());
    100 
    101   remote()->transact(IBluetoothGattClient::UNREGISTER_ALL_TRANSACTION,
    102                      data, &reply);
    103 }
    104 
    105 IMPLEMENT_META_INTERFACE(BluetoothGattClient,
    106                          IBluetoothGattClient::kServiceName);
    107 
    108 }  // namespace binder
    109 }  // namespace ipc
    110