1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /** Provisioning protocol timeout in seconds. */
8 #define PROTOCOL_TIMEOUT_SEC     60
9 #define PROTOCOL_TIMEOUT_EXT_SEC 120
10 
11 /** Provisioning protocol timeout. */
12 #define PROTOCOL_TIMEOUT     K_SECONDS(PROTOCOL_TIMEOUT_SEC)
13 #define PROTOCOL_TIMEOUT_EXT K_SECONDS(PROTOCOL_TIMEOUT_EXT_SEC)
14 
15 /** @def PROV_BEARER_BUF_HEADROOM
16  *
17  *  @brief Required headroom for the bearer packet buffers.
18  */
19 #if defined(CONFIG_BT_MESH_PB_GATT_COMMON)
20 #define PROV_BEARER_BUF_HEADROOM 5
21 #elif defined(CONFIG_BT_MESH_RPR_CLI) || defined(CONFIG_BT_MESH_RPR_SRV)
22 #define PROV_BEARER_BUF_HEADROOM 3
23 #else
24 #define PROV_BEARER_BUF_HEADROOM 0
25 #endif
26 
27 /**
28  *
29  *  @brief Required tailroom for the bearer packet buffers.
30  */
31 #if defined(CONFIG_BT_MESH_RPR_CLI) || defined(CONFIG_BT_MESH_RPR_SRV)
32 #define PROV_BEARER_BUF_TAILROOM 4
33 #else
34 #define PROV_BEARER_BUF_TAILROOM 0
35 #endif
36 
37 enum prov_bearer_link_status {
38 	PROV_BEARER_LINK_STATUS_SUCCESS,
39 	PROV_BEARER_LINK_STATUS_TIMEOUT,
40 	PROV_BEARER_LINK_STATUS_FAIL,
41 };
42 
43 struct prov_bearer;
44 
45 /** Callbacks from bearer to host */
46 struct prov_bearer_cb {
47 
48 	void (*link_opened)(const struct prov_bearer *bearer, void *cb_data);
49 
50 	void (*link_closed)(const struct prov_bearer *bearer, void *cb_data,
51 			    enum prov_bearer_link_status reason);
52 
53 	void (*error)(const struct prov_bearer *bearer, void *cb_data,
54 		      uint8_t err);
55 
56 	void (*recv)(const struct prov_bearer *bearer, void *cb_data,
57 		     struct net_buf_simple *buf);
58 };
59 
60 typedef void (*prov_bearer_send_complete_t)(int err, void *cb_data);
61 
62 /** Provisioning bearer API */
63 struct prov_bearer {
64 	/** Provisioning bearer type. */
65 	bt_mesh_prov_bearer_t type;
66 
67 	/** @brief Enable link establishment as a provisionee.
68 	 *
69 	 *  Prompts the bearer to make itself visible to provisioners, and
70 	 *  start accepting link open messages.
71 	 *
72 	 *  @param cb Bearer event callbacks used for the duration of the link.
73 	 *  @param cb_data Context parameter to pass to the bearer callbacks.
74 	 *
75 	 *  @return Zero on success, or (negative) error code otherwise.
76 	 */
77 	int (*link_accept)(const struct prov_bearer_cb *cb, void *cb_data);
78 
79 	/** @brief Send a packet on an established link.
80 	 *
81 	 *  @param buf     Payload buffer. Requires @ref
82 	 *                 PROV_BEARER_BUF_HEADROOM bytes of headroom.
83 	 *  @param cb      Callback to call when sending is complete.
84 	 *  @param cb_data Callback data.
85 	 *
86 	 *  @return Zero on success, or (negative) error code otherwise.
87 	 */
88 	int (*send)(struct net_buf_simple *buf, prov_bearer_send_complete_t cb,
89 		    void *cb_data);
90 
91 	/** @brief Clear any ongoing transmissions, if possible.
92 	 *
93 	 *  Bearers that don't support tx clearing must implement this callback
94 	 *  and leave it empty.
95 	 */
96 	void (*clear_tx)(void);
97 
98 	/* Only available in provisioners: */
99 
100 	/** @brief Open a new link as a provisioner.
101 	 *
102 	 *  Only available in provisioners. Bearers that don't support the
103 	 *  provisioner role should leave this as NULL.
104 	 *
105 	 *  @param uuid UUID of the node to establish a link to.
106 	 *  @param timeout Link open timeout in seconds.
107 	 *  @param cb Bearer event callbacks used for the duration of the link.
108 	 *  @param cb_data Context parameter to pass to the bearer callbacks.
109 	 *
110 	 *  @return Zero on success, or (negative) error code otherwise.
111 	 */
112 	int (*link_open)(const uint8_t uuid[16], uint8_t timeout,
113 			 const struct prov_bearer_cb *cb, void *cb_data);
114 
115 	/** @brief Close the current link.
116 	 *
117 	 *  Only available in provisioners. Bearers that don't support the
118 	 *  provisioner role should leave this as NULL.
119 	 *
120 	 *  @param status Link status for the link close message.
121 	 */
122 	void (*link_close)(enum prov_bearer_link_status status);
123 };
124 
125 struct pb_remote_ctx {
126 	struct bt_mesh_rpr_cli *cli;
127 	const struct bt_mesh_rpr_node *srv;
128 	enum bt_mesh_rpr_node_refresh refresh;
129 };
130 
131 extern const struct prov_bearer bt_mesh_pb_adv;
132 extern const struct prov_bearer bt_mesh_pb_gatt;
133 extern const struct prov_bearer pb_remote_cli;
134 extern const struct prov_bearer pb_remote_srv;
135 
136 void bt_mesh_pb_adv_init(void);
137 void bt_mesh_pb_gatt_init(void);
138 
139 void bt_mesh_pb_adv_reset(void);
140 void bt_mesh_pb_gatt_reset(void);
141