1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /* Maximum advertising data payload for a single data type */
8 #define BT_MESH_ADV_DATA_SIZE 29
9 
10 /* The user data is a pointer (4 bytes) to struct bt_mesh_adv */
11 #define BT_MESH_ADV_USER_DATA_SIZE 4
12 
13 #define BT_MESH_ADV(buf) (*(struct bt_mesh_adv **)net_buf_user_data(buf))
14 
15 #define BT_MESH_ADV_SCAN_UNIT(_ms) ((_ms) * 8 / 5)
16 #define BT_MESH_SCAN_INTERVAL_MS 30
17 #define BT_MESH_SCAN_WINDOW_MS   30
18 
19 enum bt_mesh_adv_type {
20 	BT_MESH_ADV_PROV,
21 	BT_MESH_ADV_DATA,
22 	BT_MESH_ADV_BEACON,
23 	BT_MESH_ADV_URI,
24 
25 	BT_MESH_ADV_TYPES,
26 };
27 
28 enum bt_mesh_adv_tag {
29 	BT_MESH_LOCAL_ADV = BIT(0),
30 	BT_MESH_RELAY_ADV = BIT(1),
31 	BT_MESH_PROXY_ADV = BIT(2),
32 	BT_MESH_FRIEND_ADV = BIT(3),
33 };
34 
35 struct bt_mesh_adv {
36 	const struct bt_mesh_send_cb *cb;
37 	void *cb_data;
38 
39 	uint8_t      type:2,
40 		  started:1,
41 		  busy:1,
42 		  tag:4;
43 
44 	uint8_t      xmit;
45 };
46 
47 /* Lookup table for Advertising data types for bt_mesh_adv_type: */
48 extern const uint8_t bt_mesh_adv_type[BT_MESH_ADV_TYPES];
49 
50 /* xmit_count: Number of retransmissions, i.e. 0 == 1 transmission */
51 struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type,
52 				   enum bt_mesh_adv_tag tag,
53 				   uint8_t xmit, k_timeout_t timeout);
54 
55 void bt_mesh_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb,
56 		      void *cb_data);
57 
58 struct net_buf *bt_mesh_adv_buf_get(k_timeout_t timeout);
59 
60 struct net_buf *bt_mesh_adv_buf_get_by_tag(uint8_t tag, k_timeout_t timeout);
61 
62 void bt_mesh_adv_gatt_update(void);
63 
64 void bt_mesh_adv_buf_get_cancel(void);
65 
66 void bt_mesh_adv_init(void);
67 
68 int bt_mesh_scan_enable(void);
69 
70 int bt_mesh_scan_disable(void);
71 
72 int bt_mesh_adv_enable(void);
73 
74 void bt_mesh_adv_buf_local_ready(void);
75 
76 void bt_mesh_adv_buf_relay_ready(void);
77 
78 void bt_mesh_adv_buf_friend_ready(void);
79 
80 int bt_mesh_adv_gatt_send(void);
81 
82 int bt_mesh_adv_gatt_start(const struct bt_le_adv_param *param, int32_t duration,
83 			   const struct bt_data *ad, size_t ad_len,
84 			   const struct bt_data *sd, size_t sd_len);
85 
bt_mesh_adv_send_start(uint16_t duration,int err,struct bt_mesh_adv * adv)86 static inline void bt_mesh_adv_send_start(uint16_t duration, int err,
87 					  struct bt_mesh_adv *adv)
88 {
89 	if (!adv->started) {
90 		adv->started = 1;
91 
92 		if (adv->cb && adv->cb->start) {
93 			adv->cb->start(duration, err, adv->cb_data);
94 		}
95 
96 		if (err) {
97 			adv->cb = NULL;
98 		}
99 	}
100 }
101 
bt_mesh_adv_send_end(int err,struct bt_mesh_adv const * adv)102 static inline void bt_mesh_adv_send_end(
103 	int err, struct bt_mesh_adv const *adv)
104 {
105 	if (adv->started && adv->cb && adv->cb->end) {
106 		adv->cb->end(err, adv->cb_data);
107 	}
108 }
109 
110 int bt_mesh_scan_active_set(bool active);
111 
112 int bt_mesh_adv_bt_data_send(uint8_t num_events, uint16_t adv_interval,
113 			     const struct bt_data *ad, size_t ad_len);
114