• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 #define LOG_TAG "bt_btif_sock_hal"
18 
19 #include "btif/include/btif_sock_hal.h"
20 
21 #include "btif/include/btif_sock_l2cap.h"
22 #include "btif/include/btif_sock_rfc.h"
23 #include "lpp/lpp_offload_interface.h"
24 #include "main/shim/entry.h"
25 #include "stack/include/main_thread.h"
26 
27 using namespace bluetooth;
28 
29 class BtifSocketHalCallback : public hal::SocketHalCallback {
30 public:
SocketOpenedComplete(uint64_t socket_id,hal::SocketStatus status) const31   void SocketOpenedComplete(uint64_t socket_id, hal::SocketStatus status) const override {
32     log::info("socket_id: {}, status: {}", socket_id, static_cast<int>(status));
33     if (btsock_l2cap_in_use(socket_id)) {
34       do_in_main_thread(base::BindOnce(on_btsocket_l2cap_opened_complete, socket_id,
35                                        (status == hal::SocketStatus::SUCCESS)));
36     } else if (btsock_rfc_in_use(socket_id)) {
37       do_in_main_thread(base::BindOnce(on_btsocket_rfc_opened_complete, socket_id,
38                                        (status == hal::SocketStatus::SUCCESS)));
39     } else {
40       log::error("Unable to find socket with socket_id:{}", socket_id);
41     }
42   }
43 
SocketClose(uint64_t socket_id) const44   void SocketClose(uint64_t socket_id) const override {
45     log::info("socket_id: {}", socket_id);
46     if (btsock_l2cap_in_use(socket_id)) {
47       do_in_main_thread(base::BindOnce(on_btsocket_l2cap_close, socket_id));
48     } else if (btsock_rfc_in_use(socket_id)) {
49       do_in_main_thread(base::BindOnce(on_btsocket_rfc_close, socket_id));
50     } else {
51       log::error("Unable to find socket with socket_id:{}", socket_id);
52     }
53   }
54 };
55 
56 static BtifSocketHalCallback btif_socket_hal_cb;
57 
btsock_hal_init()58 bt_status_t btsock_hal_init() {
59   log::info("");
60   auto lpp_offload_manager_interface = bluetooth::shim::GetLppOffloadManager();
61   if (lpp_offload_manager_interface == nullptr) {
62     log::warn("GetLppOffloadManager() returned nullptr!");
63     return BT_STATUS_FAIL;
64   }
65   if (!lpp_offload_manager_interface->RegisterSocketHalCallback(&btif_socket_hal_cb)) {
66     log::warn("RegisterSocketHalCallback() failed!");
67     return BT_STATUS_FAIL;
68   }
69   return BT_STATUS_SUCCESS;
70 }
71