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