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 = std::variant<tBTA_DM_API_SEARCH, tBTA_DM_REMOTE_NAME>;
61 
62 /* DM search state */
63 typedef enum {
64   BTA_DM_SEARCH_IDLE,
65   BTA_DM_SEARCH_ACTIVE,
66   BTA_DM_SEARCH_CANCELLING,
67 } tBTA_DM_DEVICE_SEARCH_STATE;
68 
bta_dm_state_text(const tBTA_DM_DEVICE_SEARCH_STATE & state)69 inline std::string bta_dm_state_text(const tBTA_DM_DEVICE_SEARCH_STATE& state) {
70   switch (state) {
71     CASE_RETURN_TEXT(BTA_DM_SEARCH_IDLE);
72     CASE_RETURN_TEXT(BTA_DM_SEARCH_ACTIVE);
73     CASE_RETURN_TEXT(BTA_DM_SEARCH_CANCELLING);
74   }
75 }
76 
77 /* DM search control block */
78 typedef struct {
79   tBTA_DM_SEARCH_CBACK* p_device_search_cback;
80   tBTM_INQ_INFO* p_btm_inq_info;
81   /* This covers device search state. That is scanning through android Settings
82    * to discover LE and Classic devices. Runs Name discovery on Inquiry Results
83    */
84   tBTA_DM_DEVICE_SEARCH_STATE search_state;
85   bool name_discover_done;
86   /* peer address used for name discovery */
87   RawAddress peer_bdaddr;
88   BD_NAME peer_name;
89   std::unique_ptr<tBTA_DM_SEARCH_MSG> p_pending_search;
90   tBTA_DM_SEARCH_CBACK* p_csis_scan_cback;
91 } tBTA_DM_SEARCH_CB;
92 
93 namespace std {
94 template <>
95 struct formatter<tBTA_DM_DEV_SEARCH_EVT> : enum_formatter<tBTA_DM_DEV_SEARCH_EVT> {};
96 template <>
97 struct formatter<tBTA_DM_DEVICE_SEARCH_STATE> : enum_formatter<tBTA_DM_DEVICE_SEARCH_STATE> {};
98 }  // namespace std
99