1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 #ifndef H_BLE_HS_CONN_ 21 #define H_BLE_HS_CONN_ 22 23 #include <stdint.h> 24 #include "ble_l2cap_priv.h" 25 #include "ble_gatt_priv.h" 26 #include "ble_att_priv.h" 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 struct hci_le_conn_complete; 32 struct hci_create_conn; 33 struct ble_l2cap_chan; 34 35 typedef uint8_t ble_hs_conn_flags_t; 36 37 #define BLE_HS_CONN_F_MASTER 0x01 38 #define BLE_HS_CONN_F_TERMINATING 0x02 39 #define BLE_HS_CONN_F_TX_FRAG 0x04 /* Cur ACL packet partially txed. */ 40 41 #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) 42 #define BLE_HS_CONN_L2CAP_COC_CID_MASK_LEN_REM \ 43 ((MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) % (8 * sizeof(uint32_t))) ? 1 : 0) 44 45 #define BLE_HS_CONN_L2CAP_COC_CID_MASK_LEN \ 46 (MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) / (8 * sizeof(uint32_t)) + \ 47 BLE_HS_CONN_L2CAP_COC_CID_MASK_LEN_REM) 48 #endif 49 50 struct ble_hs_conn { 51 SLIST_ENTRY(ble_hs_conn) bhc_next; 52 uint16_t bhc_handle; 53 uint8_t bhc_our_addr_type; 54 #if MYNEWT_VAL(BLE_EXT_ADV) 55 uint8_t bhc_our_rnd_addr[6]; 56 #endif 57 ble_addr_t bhc_peer_addr; 58 ble_addr_t bhc_our_rpa_addr; 59 ble_addr_t bhc_peer_rpa_addr; 60 61 uint16_t bhc_itvl; 62 uint16_t bhc_latency; 63 uint16_t bhc_supervision_timeout; 64 uint8_t bhc_master_clock_accuracy; 65 66 uint32_t supported_feat; 67 uint16_t tx_ll_mtu; 68 69 ble_hs_conn_flags_t bhc_flags; 70 71 struct ble_l2cap_chan_list bhc_channels; 72 struct ble_l2cap_chan *bhc_rx_chan; /* Channel rxing current packet. */ 73 ble_npl_time_t bhc_rx_timeout; 74 #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) 75 uint32_t l2cap_coc_cid_mask[BLE_HS_CONN_L2CAP_COC_CID_MASK_LEN]; 76 #endif 77 78 /** 79 * Count of packets sent over this connection that the controller has not 80 * transmitted or flushed yet. 81 */ 82 uint16_t bhc_outstanding_pkts; 83 84 #if MYNEWT_VAL(BLE_HS_FLOW_CTRL) 85 /** 86 * Count of packets received over this connection that have been processed 87 * and freed. 88 */ 89 uint16_t bhc_completed_pkts; 90 #endif 91 92 /** Queue of outgoing packets that could not be sent. */ 93 STAILQ_HEAD(, os_mbuf_pkthdr) bhc_tx_q; 94 95 struct ble_att_svr_conn bhc_att_svr; 96 struct ble_gatts_conn bhc_gatt_svr; 97 98 struct ble_gap_sec_state bhc_sec_state; 99 100 ble_gap_event_fn *bhc_cb; 101 void *bhc_cb_arg; 102 103 #if MYNEWT_VAL(BLE_PERIODIC_ADV) 104 struct ble_hs_periodic_sync *psync; 105 #endif 106 }; 107 108 struct ble_hs_conn_addrs { 109 ble_addr_t our_id_addr; 110 ble_addr_t peer_id_addr; 111 ble_addr_t our_ota_addr; 112 ble_addr_t peer_ota_addr; 113 }; 114 115 int ble_hs_conn_can_alloc(void); 116 struct ble_hs_conn *ble_hs_conn_alloc(uint16_t conn_handle); 117 void ble_hs_conn_free(struct ble_hs_conn *conn); 118 void ble_hs_conn_insert(struct ble_hs_conn *conn); 119 void ble_hs_conn_remove(struct ble_hs_conn *conn); 120 struct ble_hs_conn *ble_hs_conn_find(uint16_t conn_handle); 121 struct ble_hs_conn *ble_hs_conn_find_assert(uint16_t conn_handle); 122 struct ble_hs_conn *ble_hs_conn_find_by_addr(const ble_addr_t *addr); 123 struct ble_hs_conn *ble_hs_conn_find_by_idx(int idx); 124 int ble_hs_conn_exists(uint16_t conn_handle); 125 struct ble_hs_conn *ble_hs_conn_first(void); 126 struct ble_l2cap_chan *ble_hs_conn_chan_find_by_scid(struct ble_hs_conn *conn, uint16_t cid); 127 struct ble_l2cap_chan *ble_hs_conn_chan_find_by_dcid(struct ble_hs_conn *conn, uint16_t cid); 128 bool ble_hs_conn_chan_exist(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan); 129 int ble_hs_conn_chan_insert(struct ble_hs_conn *conn, 130 struct ble_l2cap_chan *chan); 131 void ble_hs_conn_delete_chan(struct ble_hs_conn *conn, 132 struct ble_l2cap_chan *chan); 133 134 void ble_hs_conn_addrs(const struct ble_hs_conn *conn, 135 struct ble_hs_conn_addrs *addrs); 136 int32_t ble_hs_conn_timer(void); 137 138 typedef int ble_hs_conn_foreach_fn(struct ble_hs_conn *conn, void *arg); 139 void ble_hs_conn_foreach(ble_hs_conn_foreach_fn *cb, void *arg); 140 141 int ble_hs_conn_init(void); 142 143 #ifdef __cplusplus 144 } 145 #endif 146 147 #endif