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 <bluetooth/log.h>
20 
21 #include <string>
22 
23 #include "macros.h"
24 #include "stack/include/btm_ble_sec_api_types.h"
25 #include "stack/include/btm_sec_api_types.h"
26 #include "stack/include/btm_status.h"
27 #include "types/raw_address.h"
28 
29 typedef enum : uint8_t {
30   BTM_BLE_SEC_REQ_ACT_NONE = 0,
31   /* encrypt the link using current key or key refresh */
32   BTM_BLE_SEC_REQ_ACT_ENCRYPT = 1,
33   BTM_BLE_SEC_REQ_ACT_PAIR = 2,
34   /* discard the sec request while encryption is started but not completed */
35   BTM_BLE_SEC_REQ_ACT_DISCARD = 3,
36 } tBTM_BLE_SEC_REQ_ACT;
37 
btm_ble_sec_req_act_text(const tBTM_BLE_SEC_REQ_ACT & action)38 inline std::string btm_ble_sec_req_act_text(const tBTM_BLE_SEC_REQ_ACT& action) {
39   switch (action) {
40     CASE_RETURN_TEXT(BTM_BLE_SEC_REQ_ACT_NONE);
41     CASE_RETURN_TEXT(BTM_BLE_SEC_REQ_ACT_ENCRYPT);
42     CASE_RETURN_TEXT(BTM_BLE_SEC_REQ_ACT_PAIR);
43     CASE_RETURN_TEXT(BTM_BLE_SEC_REQ_ACT_DISCARD);
44     default:
45       return "UNKNOWN ACTION";
46   }
47 }
48 /* LE security function from btm_sec.cc */
49 void btm_ble_link_sec_check(const RawAddress& bd_addr, tBTM_LE_AUTH_REQ auth_req,
50                             tBTM_BLE_SEC_REQ_ACT* p_sec_req_act);
51 void btm_ble_ltk_request_reply(const RawAddress& bda, bool use_stk, const Octet16& stk);
52 tBTM_STATUS btm_proc_smp_cback(tSMP_EVT event, const RawAddress& bd_addr, tSMP_EVT_DATA* p_data);
53 tBTM_STATUS btm_ble_set_encryption(const RawAddress& bd_addr, tBTM_BLE_SEC_ACT sec_act,
54                                    uint8_t link_role);
55 tBTM_STATUS btm_ble_start_encrypt(const RawAddress& bda, bool use_stk, Octet16* p_stk);
56 void btm_ble_link_encrypted(const RawAddress& bd_addr, uint8_t encr_enable);
57 
58 void btm_ble_reset_id(void);
59 
60 bool btm_get_local_div(const RawAddress& bd_addr, uint16_t* p_div);
61 bool btm_ble_get_enc_key_type(const RawAddress& bd_addr, uint8_t* p_key_types);
62 
63 void btm_sec_save_le_key(const RawAddress& bd_addr, tBTM_LE_KEY_TYPE key_type,
64                          tBTM_LE_KEY_VALUE* p_keys, bool pass_to_application);
65 void btm_ble_update_sec_key_size(const RawAddress& bd_addr, uint8_t enc_key_size);
66 uint8_t btm_ble_read_sec_key_size(const RawAddress& bd_addr);
67 
68 tBTM_STATUS btm_ble_start_sec_check(const RawAddress& bd_addr, uint16_t psm, bool is_originator,
69                                     tBTM_SEC_CALLBACK* p_callback, void* p_ref_data);
70 
71 namespace std {
72 template <>
73 struct formatter<tBTM_BLE_SEC_REQ_ACT>
74     : string_formatter<tBTM_BLE_SEC_REQ_ACT, &btm_ble_sec_req_act_text> {};
75 }  // namespace std
76