1 /*
2  * Copyright 2022 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_headless_messenger"
18 
19 #include "test/headless/messenger.h"
20 
21 #include <condition_variable>
22 #include <deque>
23 #include <memory>
24 #include <mutex>
25 
26 #include "test/headless/interface.h"
27 #include "test/headless/log.h"
28 
29 using namespace bluetooth::test::headless;
30 using namespace std::chrono_literals;
31 
32 template <typename T>
33 struct callback_queue_t {
callback_queue_tcallback_queue_t34   callback_queue_t(const std::string name) : name_(name) {}
35   // Must be held with lock
sizecallback_queue_t36   size_t size() const { return callback_queue.size(); }
37 
38  private:
39   const std::string name_;
40   std::deque<T> callback_queue;
41 
42  public:
Pushcallback_queue_t43   void Push(T elem) { callback_queue.push_back(elem); }
44   // Must be held with lock
Popcallback_queue_t45   T Pop() {
46     T p = callback_queue.front();
47     callback_queue.pop_front();
48     return p;
49   }
50   // Must be held with lock
emptycallback_queue_t51   bool empty() const { return callback_queue.empty(); }
52 };
53 
54 struct messenger_t {
55   mutable std::mutex mutex;
56   std::condition_variable cv;
57   callback_queue_t<std::shared_ptr<callback_params_t>> callback_queue =
58       callback_queue_t<std::shared_ptr<callback_params_t>>("callbacks");
Pushmessenger_t59   void Push(std::shared_ptr<callback_params_t> elem) {
60     std::unique_lock<std::mutex> lk(mutex);
61     callback_queue.Push(elem);
62     cv.notify_all();
63   }
64 };
65 
66 namespace bluetooth {
67 namespace test {
68 namespace headless {
69 namespace messenger {
70 
71 // Called by client to await any callback for the given callbacks
72 messenger_t callback_data_;
73 
await_callback(Context & context)74 bool await_callback(Context& context) {
75   std::unique_lock<std::mutex> lk(callback_data_.mutex);
76   while (!callback_data_.callback_queue.empty()) {
77     std::shared_ptr<callback_params_t> cb = callback_data_.callback_queue.Pop();
78     if (std::find(context.callbacks.begin(), context.callbacks.end(),
79                   cb->CallbackType()) != context.callbacks.end()) {
80       context.callback_ready_q.push_back(cb);
81     }
82   }
83   if (context.callback_ready_q.size() == 0) {
84     callback_data_.cv.wait_for(lk, context.timeout);
85   }
86   return true;
87 }
88 
89 }  // namespace messenger
90 }  // namespace headless
91 }  // namespace test
92 }  // namespace bluetooth
93 
94 namespace bluetooth::test::headless {
95 
messenger_stats()96 void messenger_stats() {
97   //  LOG_CONSOLE("%30s cnt:%zu", "device_found",
98   //  discovered::device_found_.size()); LOG_CONSOLE("%30s cnt:%zu",
99   //  "remote_device_properties",
100   //              properties::remote_device_properties_.size());
101 }
102 
103 // Callbacks that the messenger will handle from the bluetooth stack
start_messenger()104 void start_messenger() {
105   headless_add_callback("acl_state_changed", [](callback_data_t* data) {
106     log::assert_that(data != nullptr, "Received nullptr callback data");
107     messenger::callback_data_.Push(std::make_shared<acl_state_changed_params_t>(
108         *(static_cast<acl_state_changed_params_t*>(data))));
109   });
110   headless_add_callback("adapter_properties", [](callback_data_t* data) {
111     log::assert_that(data != nullptr, "Received nullptr callback data");
112     messenger::callback_data_.Push(
113         std::make_shared<adapter_properties_params_t>(
114             *(static_cast<adapter_properties_params_t*>(data))));
115   });
116   headless_add_callback("device_found", [](callback_data_t* data) {
117     log::assert_that(data != nullptr, "Received nullptr callback data");
118     messenger::callback_data_.Push(std::make_shared<device_found_params_t>(
119         *(static_cast<device_found_params_t*>(data))));
120   });
121   headless_add_callback("remote_device_properties", [](callback_data_t* data) {
122     log::assert_that(data != nullptr, "Received nullptr callback data");
123     messenger::callback_data_.Push(
124         std::make_shared<remote_device_properties_params_t>(
125             *(static_cast<remote_device_properties_params_t*>(data))));
126   });
127   LOG_CONSOLE("Started messenger service");
128 }
129 
stop_messenger()130 void stop_messenger() {
131   headless_remove_callback("remote_device_properties");
132   headless_remove_callback("device_found");
133   headless_remove_callback("adapter_properties");
134   headless_remove_callback("acl_state_changed");
135 
136   LOG_CONSOLE("Stopped messenger service");
137 
138   messenger_stats();
139 }
140 
141 }  // namespace bluetooth::test::headless
142