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 #pragma once
18 
19 #include <base/strings/stringprintf.h>
20 #include <bluetooth/log.h>
21 
22 #include <string>
23 
24 #include "bta/include/bta_api.h"
25 #include "macros.h"
26 #include "stack/btm/neighbor_inquiry.h"
27 #include "types/raw_address.h"
28 
29 /* DM search events */
30 typedef enum : uint16_t {
31   /* DM search API events */
32   BTA_DM_API_SEARCH_EVT,
33   BTA_DM_API_SEARCH_CANCEL_EVT,
34   BTA_DM_INQUIRY_CMPL_EVT,
35   BTA_DM_REMT_NAME_EVT,
36   BTA_DM_SEARCH_CMPL_EVT,
37 } tBTA_DM_DEV_SEARCH_EVT;
38 
bta_dm_event_text(const tBTA_DM_DEV_SEARCH_EVT & event)39 inline std::string bta_dm_event_text(const tBTA_DM_DEV_SEARCH_EVT& event) {
40   switch (event) {
41     CASE_RETURN_TEXT(BTA_DM_API_SEARCH_EVT);
42     CASE_RETURN_TEXT(BTA_DM_API_SEARCH_CANCEL_EVT);
43     CASE_RETURN_TEXT(BTA_DM_INQUIRY_CMPL_EVT);
44     CASE_RETURN_TEXT(BTA_DM_REMT_NAME_EVT);
45     CASE_RETURN_TEXT(BTA_DM_SEARCH_CMPL_EVT);
46   }
47 }
48 
49 /* data type for BTA_DM_API_SEARCH_EVT */
50 typedef struct {
51   tBTA_DM_SEARCH_CBACK* p_cback;
52 } tBTA_DM_API_SEARCH;
53 
54 typedef struct {
55   RawAddress bd_addr;
56   BD_NAME bd_name; /* Name of peer device. */
57   tHCI_STATUS hci_status;
58 } tBTA_DM_REMOTE_NAME;
59 
60 using tBTA_DM_SEARCH_MSG =
61     std::variant<tBTA_DM_API_SEARCH, tBTA_DM_REMOTE_NAME>;
62 
63 /* DM search state */
64 typedef enum {
65   BTA_DM_SEARCH_IDLE,
66   BTA_DM_SEARCH_ACTIVE,
67   BTA_DM_SEARCH_CANCELLING,
68 } tBTA_DM_DEVICE_SEARCH_STATE;
69 
bta_dm_state_text(const tBTA_DM_DEVICE_SEARCH_STATE & state)70 inline std::string bta_dm_state_text(const tBTA_DM_DEVICE_SEARCH_STATE& state) {
71   switch (state) {
72     CASE_RETURN_TEXT(BTA_DM_SEARCH_IDLE);
73     CASE_RETURN_TEXT(BTA_DM_SEARCH_ACTIVE);
74     CASE_RETURN_TEXT(BTA_DM_SEARCH_CANCELLING);
75   }
76 }
77 
78 /* DM search control block */
79 typedef struct {
80   tBTA_DM_SEARCH_CBACK* p_device_search_cback;
81   tBTM_INQ_INFO* p_btm_inq_info;
82   /* This covers device search state. That is scanning through android Settings
83    * to discover LE and Classic devices. Runs Name discovery on Inquiry Results
84    */
85   tBTA_DM_DEVICE_SEARCH_STATE search_state;
86   bool name_discover_done;
87   /* peer address used for name discovery */
88   RawAddress peer_bdaddr;
89   BD_NAME peer_name;
90   std::unique_ptr<tBTA_DM_SEARCH_MSG> p_pending_search;
91   tBTA_DM_SEARCH_CBACK* p_csis_scan_cback;
92 } tBTA_DM_SEARCH_CB;
93 
94 namespace fmt {
95 template <>
96 struct formatter<tBTA_DM_DEV_SEARCH_EVT>
97     : enum_formatter<tBTA_DM_DEV_SEARCH_EVT> {};
98 template <>
99 struct formatter<tBTA_DM_DEVICE_SEARCH_STATE>
100     : enum_formatter<tBTA_DM_DEVICE_SEARCH_STATE> {};
101 }  // namespace fmt
102