1 /* 2 * Copyright (C) 2024 The Android Open Source Project 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 #pragma once 18 19 #include <atomic> 20 #include <cstdint> 21 #include <future> 22 23 #include "aidl/android/hardware/bluetooth/socket/BnBluetoothSocket.h" 24 #include "bluetooth_socket_offload_link.h" 25 #include "bluetooth_socket_offload_link_callback.h" 26 #include "chre_host/generated/host_messages_generated.h" 27 28 namespace aidl::android::hardware::bluetooth::socket::impl { 29 30 /** 31 * Implementationof the BT Socket HAL using flatbuffer encoding and decoding for 32 * offload messages. 33 */ 34 class BluetoothSocketFbsHal : public BnBluetoothSocket, 35 public BluetoothSocketOffloadLinkCallback { 36 public: BluetoothSocketFbsHal(std::shared_ptr<BluetoothSocketOffloadLink> offloadLink)37 BluetoothSocketFbsHal(std::shared_ptr<BluetoothSocketOffloadLink> offloadLink) 38 : mOffloadLink(offloadLink) { 39 mOffloadLink->setBluetoothSocketCallback(this); 40 } 41 42 // Functions implementing IBluetoothSocket. 43 ndk::ScopedAStatus registerCallback( 44 const std::shared_ptr<IBluetoothSocketCallback> &callback) override; 45 ndk::ScopedAStatus getSocketCapabilities(SocketCapabilities *result) override; 46 ndk::ScopedAStatus opened(const SocketContext &context) override; 47 ndk::ScopedAStatus closed(int64_t socketId) override; 48 49 // Functions implementing BluetoothSocketOffloadLinkCallback. onOffloadLinkDisconnected()50 void onOffloadLinkDisconnected() override { 51 mOffloadLinkAvailable = false; 52 } onOffloadLinkReconnected()53 void onOffloadLinkReconnected() override { 54 mOffloadLinkAvailable = true; 55 } 56 void handleMessageFromOffloadStack(const void *message, 57 size_t length) override; 58 59 private: 60 std::shared_ptr<BluetoothSocketOffloadLink> mOffloadLink; 61 62 std::shared_ptr<IBluetoothSocketCallback> mCallback{}; 63 64 // A thread safe flag indicating if the offload link is ready for operations. 65 // Outside of the constructor, this boolean flag should only be written by 66 // onOffloadLinkDisconnected and onOffloadLinkReconnected, the order of which 67 // should be guaranteed by the BT Socket offload link's disconnection handler. 68 std::atomic_bool mOffloadLinkAvailable = true; 69 70 // A promise that is set when getSocketCapabilities is called and is fulfilled 71 // when a response is received from the offload stack. 72 std::promise<SocketCapabilities> mCapabilitiesPromise; 73 74 void sendOpenedCompleteMessage(int64_t socketId, Status status, 75 std::string reason); 76 77 void handleBtSocketOpenResponse( 78 const ::chre::fbs::BtSocketOpenResponseT &response); 79 80 void handleBtSocketClose(const ::chre::fbs::BtSocketCloseT &message); 81 82 void handleBtSocketCapabilitiesResponse( 83 const ::chre::fbs::BtSocketCapabilitiesResponseT &response); 84 }; 85 86 } // namespace aidl::android::hardware::bluetooth::socket::impl 87