1 /*  Bluetooth Mesh */
2 
3 /*
4  * SPDX-FileCopyrightText: 2017 Intel Corporation
5  * SPDX-FileContributor: 2018-2024 Espressif Systems (Shanghai) CO LTD
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #ifndef _ADV_H_
11 #define _ADV_H_
12 
13 #include "mesh_atomic.h"
14 #include "mesh_access.h"
15 #include "mesh_bearer_adapt.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /* Maximum advertising data payload for a single data type */
22 #define BLE_MESH_ADV_DATA_SIZE 29
23 
24 /* The user data is a pointer (4 bytes) to struct bt_mesh_adv */
25 #define BLE_MESH_ADV_USER_DATA_SIZE 4
26 
27 #define BLE_MESH_ADV(buf)               (*(struct bt_mesh_adv **)net_buf_user_data(buf))
28 #define BLE_MESH_ADV_BUSY(buf)          (BLE_MESH_ADV(buf)->busy)
29 
30 typedef struct bt_mesh_msg {
31     bool  relay;        /* Flag indicates if the packet is a relayed one */
32     void *arg;          /* Pointer to the struct net_buf */
33     uint16_t src;       /* Source address for relay packets */
34     uint16_t dst;       /* Destination address for relay packets */
35     uint32_t timestamp; /* Timestamp recorded when the relay packet is posted to queue */
36 } bt_mesh_msg_t;
37 
38 enum bt_mesh_adv_type {
39     BLE_MESH_ADV_PROV,
40     BLE_MESH_ADV_DATA,
41     BLE_MESH_ADV_BEACON,
42     BLE_MESH_ADV_URI,
43     BLE_MESH_ADV_BLE,
44 };
45 
46 struct bt_mesh_adv {
47     const struct bt_mesh_send_cb *cb;
48     void *cb_data;
49 
50     uint8_t type:3;
51 
52     bt_mesh_atomic_t busy;
53 
54     uint8_t xmit;
55 };
56 
57 typedef struct bt_mesh_adv *(*bt_mesh_adv_alloc_t)(int id);
58 
59 /* xmit_count: Number of retransmissions, i.e. 0 == 1 transmission */
60 struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type, uint8_t xmit,
61                                    int32_t timeout);
62 
63 typedef enum {
64     BLE_MESH_BUF_REF_EQUAL,
65     BLE_MESH_BUF_REF_SMALL,
66     BLE_MESH_BUF_REF_MAX,
67 } bt_mesh_buf_ref_flag_t;
68 
69 void bt_mesh_adv_buf_ref_debug(const char *func, struct net_buf *buf,
70                                uint8_t ref_cmp, bt_mesh_buf_ref_flag_t flag);
71 
72 struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool,
73                                              bt_mesh_adv_alloc_t get_id,
74                                              enum bt_mesh_adv_type type,
75                                              uint8_t xmit, int32_t timeout);
76 
77 void bt_mesh_unref_buf_from_pool(struct net_buf_pool *pool);
78 
79 void bt_mesh_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb,
80                       void *cb_data);
81 
82 struct net_buf *bt_mesh_relay_adv_create(enum bt_mesh_adv_type type, uint8_t xmit,
83                                          int32_t timeout);
84 
85 void bt_mesh_relay_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb,
86                             void *cb_data, uint16_t src, uint16_t dst);
87 
88 uint16_t bt_mesh_get_stored_relay_count(void);
89 
90 void bt_mesh_adv_update(void);
91 
92 void bt_mesh_adv_init(void);
93 void bt_mesh_adv_deinit(void);
94 
95 #if CONFIG_BLE_MESH_SUPPORT_BLE_ADV
96 int bt_mesh_start_ble_advertising(const struct bt_mesh_ble_adv_param *param,
97                                   const struct bt_mesh_ble_adv_data *data, uint8_t *index);
98 
99 int bt_mesh_stop_ble_advertising(uint8_t index);
100 #endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */
101 
102 #ifdef __cplusplus
103 }
104 #endif
105 
106 #endif /* _ADV_H_ */
107