1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 
9 #include <zephyr/bluetooth/bluetooth.h>
10 #include <zephyr/sys/util_macro.h>
11 #include <zephyr/sys_clock.h>
12 
13 #ifndef ZEPHYR_SUBSYS_BLUETOOTH_MESH_ADV_H_
14 #define ZEPHYR_SUBSYS_BLUETOOTH_MESH_ADV_H_
15 
16 /* Maximum advertising data payload for a single data type */
17 #define BT_MESH_ADV_DATA_SIZE 29
18 
19 #define BT_MESH_ADV_SCAN_UNIT(_ms) ((_ms) * 8 / 5)
20 #define BT_MESH_SCAN_INTERVAL_MS 30
21 #define BT_MESH_SCAN_WINDOW_MS   30
22 
23 enum bt_mesh_adv_type {
24 	BT_MESH_ADV_PROV,
25 	BT_MESH_ADV_DATA,
26 	BT_MESH_ADV_BEACON,
27 	BT_MESH_ADV_URI,
28 
29 	BT_MESH_ADV_TYPES,
30 };
31 
32 enum bt_mesh_adv_tag {
33 	BT_MESH_ADV_TAG_LOCAL,
34 	BT_MESH_ADV_TAG_RELAY,
35 	BT_MESH_ADV_TAG_PROXY,
36 	BT_MESH_ADV_TAG_FRIEND,
37 	BT_MESH_ADV_TAG_PROV,
38 };
39 
40 enum bt_mesh_adv_tag_bit {
41 	BT_MESH_ADV_TAG_BIT_LOCAL	= BIT(BT_MESH_ADV_TAG_LOCAL),
42 	BT_MESH_ADV_TAG_BIT_RELAY	= BIT(BT_MESH_ADV_TAG_RELAY),
43 	BT_MESH_ADV_TAG_BIT_PROXY	= BIT(BT_MESH_ADV_TAG_PROXY),
44 	BT_MESH_ADV_TAG_BIT_FRIEND	= BIT(BT_MESH_ADV_TAG_FRIEND),
45 	BT_MESH_ADV_TAG_BIT_PROV	= BIT(BT_MESH_ADV_TAG_PROV),
46 };
47 
48 struct bt_mesh_adv_ctx {
49 	const struct bt_mesh_send_cb *cb;
50 	void *cb_data;
51 
52 	uint8_t      type:2,
53 		  started:1,
54 		  busy:1,
55 		  tag:4;
56 
57 	uint8_t      xmit;
58 };
59 
60 struct bt_mesh_adv {
61 	sys_snode_t node;
62 
63 	struct bt_mesh_adv_ctx ctx;
64 
65 	struct net_buf_simple b;
66 
67 	uint8_t __ref;
68 
69 	uint8_t __bufs[BT_MESH_ADV_DATA_SIZE];
70 };
71 
72 /* Lookup table for Advertising data types for bt_mesh_adv_type: */
73 extern const uint8_t bt_mesh_adv_type[BT_MESH_ADV_TYPES];
74 
75 struct bt_mesh_adv *bt_mesh_adv_ref(struct bt_mesh_adv *adv);
76 void bt_mesh_adv_unref(struct bt_mesh_adv *adv);
77 
78 /* xmit_count: Number of retransmissions, i.e. 0 == 1 transmission */
79 struct bt_mesh_adv *bt_mesh_adv_create(enum bt_mesh_adv_type type,
80 				       enum bt_mesh_adv_tag tag,
81 				       uint8_t xmit, k_timeout_t timeout);
82 
83 void bt_mesh_adv_send(struct bt_mesh_adv *adv, const struct bt_mesh_send_cb *cb,
84 		      void *cb_data);
85 void bt_mesh_adv_send_end(int err, struct bt_mesh_adv_ctx const *ctx);
86 
87 struct bt_mesh_adv *bt_mesh_adv_get(k_timeout_t timeout);
88 
89 struct bt_mesh_adv *bt_mesh_adv_get_by_tag(enum bt_mesh_adv_tag_bit tags, k_timeout_t timeout);
90 
91 void bt_mesh_adv_gatt_update(void);
92 
93 void bt_mesh_adv_get_cancel(void);
94 
95 void bt_mesh_adv_init(void);
96 
97 int bt_mesh_scan_enable(void);
98 
99 int bt_mesh_scan_disable(void);
100 
101 int bt_mesh_adv_enable(void);
102 
103 int bt_mesh_adv_disable(void);
104 
105 void bt_mesh_adv_local_ready(void);
106 
107 void bt_mesh_adv_relay_ready(void);
108 
109 int bt_mesh_adv_terminate(struct bt_mesh_adv *adv);
110 
111 void bt_mesh_adv_friend_ready(void);
112 
113 int bt_mesh_adv_gatt_send(void);
114 
115 int bt_mesh_adv_gatt_start(const struct bt_le_adv_param *param, int32_t duration,
116 			   const struct bt_data *ad, size_t ad_len,
117 			   const struct bt_data *sd, size_t sd_len);
118 
119 void bt_mesh_adv_send_start(uint16_t duration, int err, struct bt_mesh_adv_ctx *ctx);
120 
121 int bt_mesh_scan_active_set(bool active);
122 
123 int bt_mesh_adv_bt_data_send(uint8_t num_events, uint16_t adv_interval,
124 			     const struct bt_data *ad, size_t ad_len);
125 
126 int bt_mesh_wq_submit(struct k_work *work);
127 
128 #endif /* ZEPHYR_SUBSYS_BLUETOOTH_MESH_ADV_H_ */
129