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 typedef void (*bt_mesh_adv_func_t)(struct net_buf *buf, uint16_t duration,
29 				   int err, void *user_data);
30 
31 struct bt_mesh_adv {
32 	const struct bt_mesh_send_cb *cb;
33 	void *cb_data;
34 
35 	uint8_t      type:2,
36 		  started:1,
37 		  busy:1;
38 
39 	uint8_t      xmit;
40 };
41 
42 typedef struct bt_mesh_adv *(*bt_mesh_adv_alloc_t)(int id);
43 
44 extern struct k_fifo bt_mesh_adv_queue;
45 
46 /* Lookup table for Advertising data types for bt_mesh_adv_type: */
47 extern const uint8_t bt_mesh_adv_type[BT_MESH_ADV_TYPES];
48 
49 /* xmit_count: Number of retransmissions, i.e. 0 == 1 transmission */
50 struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type, uint8_t xmit,
51 				   k_timeout_t timeout);
52 
53 struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool,
54 					     bt_mesh_adv_alloc_t get_id,
55 					     enum bt_mesh_adv_type type,
56 					     uint8_t xmit, k_timeout_t timeout);
57 
58 void bt_mesh_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb,
59 		      void *cb_data);
60 
61 void bt_mesh_adv_update(void);
62 
63 void bt_mesh_adv_init(void);
64 
65 int bt_mesh_scan_enable(void);
66 
67 int bt_mesh_scan_disable(void);
68 
69 int bt_mesh_adv_enable(void);
70 
71 void bt_mesh_adv_buf_ready(void);
72 
73 int bt_mesh_adv_start(const struct bt_le_adv_param *param, int32_t duration,
74 		      const struct bt_data *ad, size_t ad_len,
75 		      const struct bt_data *sd, size_t sd_len);
76 
bt_mesh_adv_send_start(uint16_t duration,int err,struct bt_mesh_adv * adv)77 static inline void bt_mesh_adv_send_start(uint16_t duration, int err,
78 					  struct bt_mesh_adv *adv)
79 {
80 	if (!adv->started) {
81 		adv->started = 1;
82 
83 		if (adv->cb && adv->cb->start) {
84 			adv->cb->start(duration, err, adv->cb_data);
85 		}
86 
87 		if (err) {
88 			adv->cb = NULL;
89 		}
90 	}
91 }
92 
bt_mesh_adv_send_end(int err,struct bt_mesh_adv const * adv)93 static inline void bt_mesh_adv_send_end(
94 	int err, struct bt_mesh_adv const *adv)
95 {
96 	if (adv->started && adv->cb && adv->cb->end) {
97 		adv->cb->end(err, adv->cb_data);
98 	}
99 }
100