xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/host/src/ble_hs_priv.h (revision 042d53a763ad75cb1465103098bb88c245d95138)
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_PRIV_
21 #define H_BLE_HS_PRIV_
22 
23 #include <assert.h>
24 #include <inttypes.h>
25 #include "ble_att_cmd_priv.h"
26 #include "ble_att_priv.h"
27 #include "ble_gap_priv.h"
28 #include "ble_gatt_priv.h"
29 #include "ble_hs_dbg_priv.h"
30 #include "ble_hs_hci_priv.h"
31 #include "ble_hs_atomic_priv.h"
32 #include "ble_hs_conn_priv.h"
33 #include "ble_hs_atomic_priv.h"
34 #include "ble_hs_mbuf_priv.h"
35 #include "ble_hs_startup_priv.h"
36 #include "ble_l2cap_priv.h"
37 #include "ble_l2cap_sig_priv.h"
38 #include "ble_l2cap_coc_priv.h"
39 #include "ble_sm_priv.h"
40 #include "ble_hs_adv_priv.h"
41 #include "ble_hs_flow_priv.h"
42 #include "ble_hs_pvcy_priv.h"
43 #include "ble_hs_id_priv.h"
44 #include "ble_uuid_priv.h"
45 #include "host/ble_hs.h"
46 #include "host/ble_monitor.h"
47 #include "nimble/nimble_opt.h"
48 #include "stats/stats.h"
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 struct ble_hs_conn;
54 struct ble_l2cap_chan;
55 struct os_mbuf;
56 struct os_mempool;
57 struct os_event;
58 
59 #define BLE_HS_SYNC_STATE_BAD           0
60 #define BLE_HS_SYNC_STATE_BRINGUP       1
61 #define BLE_HS_SYNC_STATE_GOOD          2
62 
63 #define BLE_HS_ENABLED_STATE_OFF        0
64 #define BLE_HS_ENABLED_STATE_STOPPING   1
65 #define BLE_HS_ENABLED_STATE_ON         2
66 
67 #if NIMBLE_BLE_CONNECT
68 #define BLE_HS_MAX_CONNECTIONS MYNEWT_VAL(BLE_MAX_CONNECTIONS)
69 #else
70 #define BLE_HS_MAX_CONNECTIONS 0
71 #endif
72 
73 #if !MYNEWT_VAL(BLE_ATT_SVR_QUEUED_WRITE)
74 #define BLE_HS_ATT_SVR_QUEUED_WRITE_TMO 0
75 #else
76 #define BLE_HS_ATT_SVR_QUEUED_WRITE_TMO \
77     MYNEWT_VAL(BLE_ATT_SVR_QUEUED_WRITE_TMO)
78 #endif
79 
80 STATS_SECT_START(ble_hs_stats)
81     STATS_SECT_ENTRY(conn_create)
82     STATS_SECT_ENTRY(conn_delete)
83     STATS_SECT_ENTRY(hci_cmd)
84     STATS_SECT_ENTRY(hci_event)
85     STATS_SECT_ENTRY(hci_invalid_ack)
86     STATS_SECT_ENTRY(hci_unknown_event)
87     STATS_SECT_ENTRY(hci_timeout)
88     STATS_SECT_ENTRY(reset)
89     STATS_SECT_ENTRY(sync)
90     STATS_SECT_ENTRY(pvcy_add_entry)
91     STATS_SECT_ENTRY(pvcy_add_entry_fail)
92 STATS_SECT_END
93 extern STATS_SECT_DECL(ble_hs_stats) ble_hs_stats;
94 
95 extern struct os_mbuf_pool ble_hs_mbuf_pool;
96 extern uint8_t ble_hs_sync_state;
97 extern uint8_t ble_hs_enabled_state;
98 
99 extern const uint8_t ble_hs_misc_null_addr[6];
100 
101 extern uint16_t ble_hs_max_attrs;
102 extern uint16_t ble_hs_max_services;
103 extern uint16_t ble_hs_max_client_configs;
104 
105 void ble_hs_process_rx_data_queue(void);
106 int ble_hs_tx_data(struct os_mbuf *om);
107 void ble_hs_wakeup_tx(void);
108 void ble_hs_enqueue_hci_event(uint8_t *hci_evt);
109 void ble_hs_event_enqueue(struct os_event *ev);
110 
111 int ble_hs_hci_rx_evt(uint8_t *hci_ev, void *arg);
112 int ble_hs_hci_evt_acl_process(struct os_mbuf *om);
113 
114 int ble_hs_misc_conn_chan_find(uint16_t conn_handle, uint16_t cid,
115                                struct ble_hs_conn **out_conn,
116                                struct ble_l2cap_chan **out_chan);
117 void ble_hs_misc_conn_chan_find_reqd(uint16_t conn_handle, uint16_t cid,
118                                      struct ble_hs_conn **out_conn,
119                                      struct ble_l2cap_chan **out_chan);
120 uint8_t ble_hs_misc_addr_type_to_id(uint8_t addr_type);
121 int ble_hs_misc_restore_irks(void);
122 
123 int ble_hs_locked_by_cur_task(void);
124 int ble_hs_is_parent_task(void);
125 void ble_hs_lock_nested(void);
126 void ble_hs_unlock_nested(void);
127 void ble_hs_lock(void);
128 void ble_hs_unlock(void);
129 void ble_hs_hw_error(uint8_t hw_code);
130 void ble_hs_timer_resched(void);
131 void ble_hs_notifications_sched(void);
132 struct ble_npl_eventq *ble_hs_evq_get(void);
133 void ble_hs_stop_init(void);
134 
135 struct ble_mqueue {
136     STAILQ_HEAD(, os_mbuf_pkthdr) head;
137     struct ble_npl_event ev;
138 };
139 
140 int ble_mqueue_init(struct ble_mqueue *mq, ble_npl_event_fn *ev_fn, void *ev_arg);
141 struct os_mbuf *ble_mqueue_get(struct ble_mqueue *mq);
142 int ble_mqueue_put(struct ble_mqueue *mq, struct ble_npl_eventq *evq, struct os_mbuf *om);
143 
144 #if MYNEWT_VAL(LOG_LEVEL) <= LOG_LEVEL_DEBUG && !BLE_MONITOR
145 
146 #define BLE_HS_LOG_CMD(is_tx, cmd_type, cmd_name, conn_handle,                \
147                        log_cb, cmd) do                                        \
148 {                                                                             \
149     BLE_HS_LOG(DEBUG, "%sed %s command: %s; conn=%d ",                        \
150                (is_tx) ? "tx" : "rx", (cmd_type), (cmd_name), (conn_handle)); \
151     (log_cb)(cmd);                                                            \
152     BLE_HS_LOG(DEBUG, "\n");                                                  \
153 } while (0)
154 
155 #define BLE_HS_LOG_EMPTY_CMD(is_tx, cmd_type, cmd_name, conn_handle) do       \
156 {                                                                             \
157     BLE_HS_LOG(DEBUG, "%sed %s command: %s; conn=%d ",                        \
158                (is_tx) ? "tx" : "rx", (cmd_type), (cmd_name), (conn_handle)); \
159     BLE_HS_LOG(DEBUG, "\n");                                                  \
160 } while (0)
161 
162 #else
163 
164 #define BLE_HS_LOG_CMD(is_tx, cmd_type, cmd_name, conn_handle, log_cb, cmd)
165 #define BLE_HS_LOG_EMPTY_CMD(is_tx, cmd_type, cmd_name, conn_handle)
166 
167 #endif
168 
169 #if MYNEWT_VAL(BLE_HS_DEBUG)
170     #define BLE_HS_DBG_ASSERT(x) assert(x)
171     #define BLE_HS_DBG_ASSERT_EVAL(x) assert(x)
172 #else
173     #define BLE_HS_DBG_ASSERT(x)
174     #define BLE_HS_DBG_ASSERT_EVAL(x) ((void)(x))
175 #endif
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 
181 #endif
182