Home | History | Annotate | Download | only in btm
      1 /******************************************************************************
      2  *
      3  *  Copyright 1999-2012 Broadcom Corporation
      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 
     19 #ifndef BTM_BLE_INT_TYPES_H
     20 #define BTM_BLE_INT_TYPES_H
     21 
     22 #include "osi/include/alarm.h"
     23 
     24 /* scanning enable status */
     25 #define BTM_BLE_SCAN_ENABLE 0x01
     26 #define BTM_BLE_SCAN_DISABLE 0x00
     27 
     28 /* advertising enable status */
     29 #define BTM_BLE_ADV_ENABLE 0x01
     30 #define BTM_BLE_ADV_DISABLE 0x00
     31 
     32 /* use the high 4 bits unused by inquiry mode */
     33 #define BTM_BLE_SELECT_SCAN 0x20
     34 #define BTM_BLE_NAME_REQUEST 0x40
     35 #define BTM_BLE_OBSERVE 0x80
     36 
     37 #define BTM_BLE_MAX_WL_ENTRY 1
     38 #define BTM_BLE_AD_DATA_LEN 31
     39 
     40 #define BTM_BLE_ENC_MASK 0x03
     41 
     42 #define BTM_BLE_DUPLICATE_ENABLE 1
     43 #define BTM_BLE_DUPLICATE_DISABLE 0
     44 
     45 /* Interval(scan_int) = 11.25 ms= 0x0010 * 0.625 ms */
     46 #define BTM_BLE_GAP_DISC_SCAN_INT 18
     47 /* scan_window = 11.25 ms= 0x0010 * 0.625 ms */
     48 #define BTM_BLE_GAP_DISC_SCAN_WIN 18
     49 /* Tgap(gen_disc) = 1.28 s= 512 * 0.625 ms */
     50 #define BTM_BLE_GAP_ADV_INT 512
     51 /* Tgap(lim_timeout) = 180s max */
     52 #define BTM_BLE_GAP_LIM_TIMEOUT_MS (180 * 1000)
     53 /* Interval(scan_int) = 5s= 8000 * 0.625 ms */
     54 #define BTM_BLE_LOW_LATENCY_SCAN_INT 8000
     55 /* scan_window = 5s= 8000 * 0.625 ms */
     56 #define BTM_BLE_LOW_LATENCY_SCAN_WIN 8000
     57 
     58 /* TGAP(adv_fast_interval1) = 30(used) ~ 60 ms  = 48 *0.625 */
     59 #define BTM_BLE_GAP_ADV_FAST_INT_1 48
     60 /* TGAP(adv_fast_interval2) = 100(used) ~ 150 ms = 160 * 0.625 ms */
     61 #define BTM_BLE_GAP_ADV_FAST_INT_2 160
     62 /* Tgap(adv_slow_interval) = 1.28 s= 512 * 0.625 ms */
     63 #define BTM_BLE_GAP_ADV_SLOW_INT 2048
     64 /* Tgap(dir_conn_adv_int_max) = 500 ms = 800 * 0.625 ms */
     65 #define BTM_BLE_GAP_ADV_DIR_MAX_INT 800
     66 /* Tgap(dir_conn_adv_int_min) = 250 ms = 400 * 0.625 ms */
     67 #define BTM_BLE_GAP_ADV_DIR_MIN_INT 400
     68 
     69 #define BTM_BLE_GAP_FAST_ADV_TIMEOUT_MS (30 * 1000)
     70 
     71 #define BTM_BLE_SEC_REQ_ACT_NONE 0
     72 /* encrypt the link using current key or key refresh */
     73 #define BTM_BLE_SEC_REQ_ACT_ENCRYPT 1
     74 #define BTM_BLE_SEC_REQ_ACT_PAIR 2
     75 /* discard the sec request while encryption is started but not completed */
     76 #define BTM_BLE_SEC_REQ_ACT_DISCARD 3
     77 typedef uint8_t tBTM_BLE_SEC_REQ_ACT;
     78 
     79 #define BLE_STATIC_PRIVATE_MSB_MASK 0x3f
     80 /*  most significant bit, bit7, bit6 is 01 to be resolvable random */
     81 #define BLE_RESOLVE_ADDR_MSB 0x40
     82 /* bit 6, and bit7 */
     83 #define BLE_RESOLVE_ADDR_MASK 0xc0
     84 inline bool BTM_BLE_IS_RESOLVE_BDA(const RawAddress& x) {
     85   return ((x.address)[0] & BLE_RESOLVE_ADDR_MASK) == BLE_RESOLVE_ADDR_MSB;
     86 }
     87 
     88 /* LE scan activity bit mask, continue with LE inquiry bits */
     89 /* observe is in progress */
     90 #define BTM_LE_OBSERVE_ACTIVE 0x80
     91 
     92 /* BLE scan activity mask checking */
     93 #define BTM_BLE_IS_SCAN_ACTIVE(x) ((x)&BTM_BLE_SCAN_ACTIVE_MASK)
     94 #define BTM_BLE_IS_INQ_ACTIVE(x) ((x)&BTM_BLE_INQUIRY_MASK)
     95 #define BTM_BLE_IS_OBS_ACTIVE(x) ((x)&BTM_LE_OBSERVE_ACTIVE)
     96 
     97 /* BLE ADDR type ID bit */
     98 #define BLE_ADDR_TYPE_ID_BIT 0x02
     99 
    100 #define BTM_VSC_CHIP_CAPABILITY_L_VERSION 55
    101 #define BTM_VSC_CHIP_CAPABILITY_M_VERSION 95
    102 
    103 typedef struct {
    104   uint16_t data_mask;
    105   uint8_t* p_flags;
    106   uint8_t ad_data[BTM_BLE_AD_DATA_LEN];
    107   uint8_t* p_pad;
    108 } tBTM_BLE_LOCAL_ADV_DATA;
    109 
    110 typedef struct {
    111   /* Used for determining if a response has already been received for the
    112    * current inquiry operation. (We do not want to flood the caller with
    113    * multiple responses from the same device. */
    114   uint32_t inq_count;
    115   bool scan_rsp;
    116   tBLE_BD_ADDR le_bda;
    117 } tINQ_LE_BDADDR;
    118 
    119 #define BTM_BLE_ISVALID_PARAM(x, min, max) \
    120   (((x) >= (min) && (x) <= (max)) || ((x) == BTM_BLE_CONN_PARAM_UNDEF))
    121 
    122 /* 15 minutes minimum for random address refreshing */
    123 #define BTM_BLE_PRIVATE_ADDR_INT_MS (15 * 60 * 1000)
    124 
    125 typedef struct {
    126   uint16_t discoverable_mode;
    127   uint16_t connectable_mode;
    128   uint32_t scan_window;
    129   uint32_t scan_interval;
    130   uint8_t scan_type;             /* current scan type: active or passive */
    131   uint8_t scan_duplicate_filter; /* duplicate filter enabled for scan */
    132   uint16_t adv_interval_min;
    133   uint16_t adv_interval_max;
    134   tBTM_BLE_AFP afp; /* advertising filter policy */
    135   tBTM_BLE_SFP sfp; /* scanning filter policy */
    136 
    137   tBLE_ADDR_TYPE adv_addr_type;
    138   uint8_t evt_type;
    139   uint8_t adv_mode;
    140   tBLE_BD_ADDR direct_bda;
    141   tBTM_BLE_EVT directed_conn;
    142   bool fast_adv_on;
    143   alarm_t* fast_adv_timer;
    144 
    145   /* inquiry BD addr database */
    146   uint8_t num_bd_entries;
    147   uint8_t max_bd_entries;
    148   tBTM_BLE_LOCAL_ADV_DATA adv_data;
    149   tBTM_BLE_ADV_CHNL_MAP adv_chnl_map;
    150 
    151   alarm_t* inquiry_timer;
    152   bool scan_rsp;
    153   uint8_t state; /* Current state that the inquiry process is in */
    154   int8_t tx_power;
    155 } tBTM_BLE_INQ_CB;
    156 
    157 /* random address resolving complete callback */
    158 typedef void(tBTM_BLE_RESOLVE_CBACK)(void* match_rec, void* p);
    159 
    160 typedef void(tBTM_BLE_ADDR_CBACK)(const RawAddress& static_random, void* p);
    161 
    162 /* random address management control block */
    163 typedef struct {
    164   tBLE_ADDR_TYPE own_addr_type; /* local device LE address type */
    165   RawAddress private_addr;
    166   RawAddress random_bda;
    167   tBTM_BLE_ADDR_CBACK* p_generate_cback;
    168   void* p;
    169   alarm_t* refresh_raddr_timer;
    170 } tBTM_LE_RANDOM_CB;
    171 
    172 typedef struct {
    173   uint16_t min_conn_int;
    174   uint16_t max_conn_int;
    175   uint16_t slave_latency;
    176   uint16_t supervision_tout;
    177 
    178 } tBTM_LE_CONN_PRAMS;
    179 
    180 typedef struct {
    181   RawAddress bd_addr;
    182   uint8_t attr;
    183   bool is_connected;
    184   bool in_use;
    185 } tBTM_LE_BG_CONN_DEV;
    186 
    187 /* white list using state as a bit mask */
    188 #define BTM_BLE_WL_IDLE 0
    189 #define BTM_BLE_WL_INIT 1
    190 typedef uint8_t tBTM_BLE_WL_STATE;
    191 
    192 /* resolving list using state as a bit mask */
    193 #define BTM_BLE_RL_IDLE 0
    194 #define BTM_BLE_RL_INIT 1
    195 #define BTM_BLE_RL_SCAN 2
    196 #define BTM_BLE_RL_ADV 4
    197 typedef uint8_t tBTM_BLE_RL_STATE;
    198 
    199 /* BLE connection state */
    200 #define BLE_CONN_IDLE 0
    201 #define BLE_DIR_CONN 1
    202 #define BLE_BG_CONN 2
    203 #define BLE_CONN_CANCEL 3
    204 typedef uint8_t tBTM_BLE_CONN_ST;
    205 
    206 typedef struct { void* p_param; } tBTM_BLE_CONN_REQ;
    207 
    208 /* LE state request */
    209 #define BTM_BLE_STATE_INVALID 0
    210 #define BTM_BLE_STATE_CONN_ADV 1
    211 #define BTM_BLE_STATE_INIT 2
    212 #define BTM_BLE_STATE_MASTER 3
    213 #define BTM_BLE_STATE_SLAVE 4
    214 #define BTM_BLE_STATE_LO_DUTY_DIR_ADV 5
    215 #define BTM_BLE_STATE_HI_DUTY_DIR_ADV 6
    216 #define BTM_BLE_STATE_NON_CONN_ADV 7
    217 #define BTM_BLE_STATE_PASSIVE_SCAN 8
    218 #define BTM_BLE_STATE_ACTIVE_SCAN 9
    219 #define BTM_BLE_STATE_SCAN_ADV 10
    220 #define BTM_BLE_STATE_MAX 11
    221 typedef uint8_t tBTM_BLE_STATE;
    222 
    223 #define BTM_BLE_STATE_CONN_ADV_BIT 0x0001
    224 #define BTM_BLE_STATE_INIT_BIT 0x0002
    225 #define BTM_BLE_STATE_MASTER_BIT 0x0004
    226 #define BTM_BLE_STATE_SLAVE_BIT 0x0008
    227 #define BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT 0x0010
    228 #define BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT 0x0020
    229 #define BTM_BLE_STATE_NON_CONN_ADV_BIT 0x0040
    230 #define BTM_BLE_STATE_PASSIVE_SCAN_BIT 0x0080
    231 #define BTM_BLE_STATE_ACTIVE_SCAN_BIT 0x0100
    232 #define BTM_BLE_STATE_SCAN_ADV_BIT 0x0200
    233 typedef uint16_t tBTM_BLE_STATE_MASK;
    234 
    235 #define BTM_BLE_STATE_ALL_MASK 0x03ff
    236 #define BTM_BLE_STATE_ALL_ADV_MASK                                  \
    237   (BTM_BLE_STATE_CONN_ADV_BIT | BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT | \
    238    BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT | BTM_BLE_STATE_SCAN_ADV_BIT)
    239 #define BTM_BLE_STATE_ALL_SCAN_MASK \
    240   (BTM_BLE_STATE_PASSIVE_SCAN_BIT | BTM_BLE_STATE_ACTIVE_SCAN_BIT)
    241 #define BTM_BLE_STATE_ALL_CONN_MASK \
    242   (BTM_BLE_STATE_MASTER_BIT | BTM_BLE_STATE_SLAVE_BIT)
    243 
    244 #ifndef BTM_LE_RESOLVING_LIST_MAX
    245 #define BTM_LE_RESOLVING_LIST_MAX 0x20
    246 #endif
    247 
    248 typedef struct {
    249   RawAddress* resolve_q_random_pseudo;
    250   uint8_t* resolve_q_action;
    251   uint8_t q_next;
    252   uint8_t q_pending;
    253 } tBTM_BLE_RESOLVE_Q;
    254 
    255 typedef struct {
    256   bool in_use;
    257   bool to_add;
    258   RawAddress bd_addr;
    259   uint8_t attr;
    260 } tBTM_BLE_WL_OP;
    261 
    262 /* BLE privacy mode */
    263 #define BTM_PRIVACY_NONE 0 /* BLE no privacy */
    264 #define BTM_PRIVACY_1_1 1  /* BLE privacy 1.1, do not support privacy 1.0 */
    265 #define BTM_PRIVACY_1_2 2  /* BLE privacy 1.2 */
    266 #define BTM_PRIVACY_MIXED \
    267   3 /* BLE privacy mixed mode, broadcom propietary mode */
    268 typedef uint8_t tBTM_PRIVACY_MODE;
    269 
    270 /* data length change event callback */
    271 typedef void(tBTM_DATA_LENGTH_CHANGE_CBACK)(uint16_t max_tx_length,
    272                                             uint16_t max_rx_length);
    273 
    274 /* Define BLE Device Management control structure
    275 */
    276 typedef struct {
    277   uint8_t scan_activity; /* LE scan activity mask */
    278 
    279   /*****************************************************
    280   **      BLE Inquiry
    281   *****************************************************/
    282   tBTM_BLE_INQ_CB inq_var;
    283 
    284   /* observer callback and timer */
    285   tBTM_INQ_RESULTS_CB* p_obs_results_cb;
    286   tBTM_CMPL_CB* p_obs_cmpl_cb;
    287   alarm_t* observer_timer;
    288 
    289   /* background connection procedure cb value */
    290   tBTM_BLE_CONN_TYPE bg_conn_type;
    291   uint32_t scan_int;
    292   uint32_t scan_win;
    293 
    294   /* white list information */
    295   tBTM_BLE_WL_STATE wl_state;
    296 
    297   fixed_queue_t* conn_pending_q;
    298   tBTM_BLE_CONN_ST conn_state;
    299 
    300   /* random address management control block */
    301   tBTM_LE_RANDOM_CB addr_mgnt_cb;
    302 
    303   bool enabled;
    304 
    305 #if (BLE_PRIVACY_SPT == TRUE)
    306   bool mixed_mode;                   /* privacy 1.2 mixed mode is on or not */
    307   tBTM_PRIVACY_MODE privacy_mode;    /* privacy mode */
    308   uint8_t resolving_list_avail_size; /* resolving list available size */
    309   tBTM_BLE_RESOLVE_Q resolving_list_pend_q; /* Resolving list queue */
    310   tBTM_BLE_RL_STATE suspended_rl_state;     /* Suspended resolving list state */
    311   uint8_t* irk_list_mask; /* IRK list availability mask, up to max entry bits */
    312   tBTM_BLE_RL_STATE rl_state; /* Resolving list state */
    313 #endif
    314 
    315   /* current BLE link state */
    316   tBTM_BLE_STATE_MASK cur_states; /* bit mask of tBTM_BLE_STATE */
    317   uint8_t link_count[2];          /* total link count master and slave*/
    318 } tBTM_BLE_CB;
    319 
    320 #endif  // BTM_BLE_INT_TYPES_H
    321