1 /****************************************************************************** 2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") 3 * All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 *****************************************************************************/ 18 #ifndef APP_DEVICE_H_ 19 #define APP_DEVICE_H_ 20 21 #include "stack/ble/ble_common.h" 22 #include "stack/ble/hci/hci_event.h" 23 #include "vendor/common/user_config.h" 24 25 #ifndef MASTER_MAX_NUM 26 #define MASTER_MAX_NUM 4 27 #endif 28 29 #ifndef SLAVE_MAX_NUM 30 #define SLAVE_MAX_NUM 4 31 #endif 32 33 #define INVALID_CONN_IDX 0xFF 34 35 #define DEVICE_CHAR_INFO_MAX_NUM (MASTER_MAX_NUM + SLAVE_MAX_NUM) // 4 master, 3 slave most 36 37 #define CHAR_HANDLE_MAX 8 38 /***************** connection character device information **************************** 39 * 40 * Demo master device char_handle(ATT handle) define as follows, 41 * assuming that peer device(BLE slave) is TELINK HID device 42 * char_handle[0] : MIC 43 * char_handle[1] : Speaker 44 * char_handle[2] : OTA 45 * char_handle[3] : Consume Report In 46 * char_handle[4] : Key Report In 47 * char_handle[5] : 48 * char_handle[6] : BLE Module, SPP Server to Client 49 * char_handle[7] : BLE Module, SPP Client to Server 50 *************************************************************************************/ 51 52 typedef struct { 53 u8 id_adrType; // identity address type 54 u8 id_addr[6]; // identity address 55 u8 reserved; 56 u8 irk[16]; 57 } rpa_addr_t; 58 59 // Attention: manual set 4 byte aligned 60 typedef struct { 61 u16 conn_handle; 62 u8 conn_role; // 0: master; 1: slave 63 u8 conn_state; // 1: connect; 0: disconnect 64 65 u8 char_handle_valid; // 1: peer device's attHandle is available; 0: peer device's attHandle not available 66 u8 rsvd[3]; // for 4 Byte align 67 68 u8 peer_adrType; 69 u8 peer_addr[6]; 70 u8 peer_RPA; // RPA: resolvable private address 71 72 // rpa_addr_t *pPeer_RPA; // only when peer mac_address is RPA, this pointer is useful 73 74 u16 char_handle[CHAR_HANDLE_MAX]; 75 } dev_char_info_t; 76 77 extern dev_char_info_t conn_dev_list[]; 78 79 extern int conn_master_num; 80 extern int conn_slave_num; 81 82 /** 83 * @brief Used for add device information to conn_dev_list. 84 * @param[in] dev_char_info - Pointer point to data buffer. 85 * @return 0 ~ DEVICE_CHAR_INFO_MAX_NUM - 1: new connection index, insert success 86 * 0xFF: insert failed 87 */ 88 int dev_char_info_insert(dev_char_info_t *dev_char_info); 89 90 /** 91 * @brief Used for add device information to conn_dev_list. 92 * @param[in] pConnEvt - LE connection complete event data buffer address. 93 * @return 0 ~ DEVICE_CHAR_INFO_MAX_NUM - 1: new connection index, insert success 94 * 0xFF: insert failed 95 */ 96 int dev_char_info_insert_by_conn_event(hci_le_connectionCompleteEvt_t *pConnEvt); 97 98 /** 99 * @brief Used for add device information to conn_dev_list. 100 * @param[in] pConnEvt - LE enhanced connection complete event data buffer address. 101 * @return 0 ~ DEVICE_CHAR_INFO_MAX_NUM - 1: new connection index, insert success 102 * 0xFF: insert failed 103 */ 104 int dev_char_info_insert_by_enhanced_conn_event(hci_le_enhancedConnCompleteEvt_t *pConnEvt); 105 106 /** 107 * @brief Used for delete device information from conn_dev_list by connHandle 108 * @param[in] connhandle - connection handle. 109 * @return 0: success 110 * 1: no find 111 */ 112 int dev_char_info_delete_by_connhandle(u16 connhandle); 113 114 /** 115 * @brief Used for delete device information from conn_dev_list by peer mac_address 116 * @param[in] adr_type - peer address type. 117 * @param[in] addr - Pointer point to peer address. 118 * @return 0: success 119 * 1: no find 120 */ 121 int dev_char_info_delete_by_peer_mac_address(u8 adr_type, u8 *addr); 122 123 /** 124 * @brief Get device information by peer device address. 125 * @param[in] adr_type - peer address type. 126 * @param[in] addr - Pointer point to peer address. 127 * @return 0: no find 128 * !0: found 129 */ 130 dev_char_info_t *dev_char_info_search_by_peer_mac_address(u8 adr_type, u8 *addr); 131 132 /** 133 * @brief Get device information by connection handle. 134 * @param[in] connhandle - connection handle. 135 * @return 0: no find 136 * !0: found 137 */ 138 dev_char_info_t *dev_char_info_search_by_connhandle(u16 connhandle); 139 140 /** 141 * @brief Used for judge if current device conn_handle 142 * @param[in] connhandle - connection handle. 143 * @return 144 */ 145 bool dev_char_info_is_connection_state_by_conn_handle(u16 connhandle); 146 147 /** 148 * @brief Get ACL connection role by connection handle. 149 * @param[in] connhandle - connection handle. 150 * @return 0: LL_ROLE_MASTER 151 * 1: LL_ROLE_SLAVE 152 * 2: connection handle invalid 153 */ 154 int dev_char_get_conn_role_by_connhandle(u16 connhandle); 155 156 /** 157 * @brief Get ACL connection index by connection handle. 158 * @param[in] connhandle - connection handle. 159 * @return 0xFF: no connection index match 160 * others: connection index 161 */ 162 int dev_char_get_conn_index_by_connhandle(u16 connhandle); 163 164 #endif /* APP_DEVICE_H_ */ 165