• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 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 #include "module.h"
18 
19 #include <hardware/bt_gatt.h>
20 
21 #include "btcore/include/module.h"
22 #include "osi/include/log.h"
23 #ifndef TARGET_FLOSS
24 #include "src/connection/ffi/connection_shim.h"
25 #include "src/core/ffi.rs.h"
26 #include "src/gatt/ffi.rs.h"
27 #endif
28 
29 #ifdef TARGET_FLOSS
30 
31 // Rust modules don't run on Floss yet (b/277643360)
32 const module_t rust_module = {.name = RUST_MODULE,
33                               .init = nullptr,
34                               .start_up = nullptr,
35                               .shut_down = nullptr,
36                               .clean_up = nullptr,
37                               .dependencies = {}};
38 
39 #else
40 
41 extern const btgatt_callbacks_t* bt_gatt_callbacks;
42 
43 namespace bluetooth {
44 namespace rust_shim {
45 
FutureReady(future_t & future)46 void FutureReady(future_t& future) { future_ready(&future, FUTURE_SUCCESS); }
47 
48 }  // namespace rust_shim
49 }  // namespace bluetooth
50 
51 namespace {
Start()52 future_t* Start() {
53   auto fut = future_new();
54 
55   if (bt_gatt_callbacks == nullptr) {
56     // We can't crash here since some adapter tests mis-use the stack
57     // startup/cleanup logic and start the stack without GATT, but don't fully
58     // mock out the native layer.
59     LOG_ERROR(
60         "GATT profile not started, so we cannot start the Rust loop - this "
61         "happens only in tests.");
62     bluetooth::rust_shim::FutureReady(*fut);
63     return fut;
64   }
65   bluetooth::rust_shim::start(
66       std::make_unique<bluetooth::gatt::GattServerCallbacks>(
67           *bt_gatt_callbacks->server),
68       std::make_unique<bluetooth::connection::LeAclManagerShim>(), *fut);
69 
70   return fut;
71 }
72 
Stop()73 future_t* Stop() {
74   bluetooth::rust_shim::stop();
75   return nullptr;
76 }
77 }  // namespace
78 
79 const module_t rust_module = {.name = RUST_MODULE,
80                               .init = nullptr,
81                               .start_up = Start,
82                               .shut_down = Stop,
83                               .clean_up = nullptr,
84                               .dependencies = {}};
85 
86 #endif
87