xref: /btstack/src/mesh/mesh_node.h (revision 7d339f89ba7480c46fae26078e20199287f07331)
1 /*
2  * Copyright (C) 2019 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #ifndef __MESH_NODE_H
39 #define __MESH_NODE_H
40 
41 #include <stdint.h>
42 
43 #include "btstack_linked_list.h"
44 #include "mesh/mesh_network.h"
45 
46 #if defined __cplusplus
47 extern "C" {
48 #endif
49 
50 #define MESH_APPKEY_INVALID                     0xffffu
51 
52 #define MAX_NR_MESH_APPKEYS_PER_MODEL           3u
53 #define MAX_NR_MESH_SUBSCRIPTION_PER_MODEL      3u
54 
55 #define MESH_HEARTBEAT_PUBLICATION_FEATURE_RELAY      1
56 #define MESH_HEARTBEAT_PUBLICATION_FEATURE_PROXY      2
57 #define MESH_HEARTBEAT_PUBLICATION_FEATURE_FRIEND     4
58 #define MESH_HEARTBEAT_PUBLICATION_FEATURE_LOW_POWER  8
59 
60 struct mesh_model;
61 struct mesh_element;
62 
63 // function to handle model operation message
64 typedef void (*mesh_operation_handler)(struct mesh_model * mesh_model, mesh_pdu_t * pdu);
65 
66 // function to publish the current state of a model
67 // @param mesh_model to publish
68 // @returns mesh_pdu with status message
69 typedef mesh_pdu_t * (*mesh_publish_state_t)(struct mesh_model * mesh_model);
70 
71 typedef enum {
72     MESH_NODE_IDENTITY_STATE_ADVERTISING_STOPPED = 0,
73     MESH_NODE_IDENTITY_STATE_ADVERTISING_RUNNING,
74     MESH_NODE_IDENTITY_STATE_ADVERTISING_NOT_SUPPORTED
75 } mesh_node_identity_state_t;
76 
77 typedef enum {
78     MESH_FRIEND_STATE_DISABLED = 0,
79     MESH_FRIEND_STATE_ENABLED,
80     MESH_FRIEND_STATE_NOT_SUPPORTED
81 } mesh_friend_state_t;
82 
83 typedef enum {
84     MESH_MODEL_PUBLICATION_STATE_IDLE,
85     MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS,
86     MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY,
87     MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS,
88     MESH_MODEL_PUBLICATION_STATE_RETRANSMIT_READY,
89 } mesh_model_publication_state_t;
90 
91 typedef struct {
92     mesh_publish_state_t publish_state_fn;
93     mesh_model_publication_state_t state;
94     uint32_t next_publication_ms;
95     uint32_t next_retransmit_ms;
96     uint8_t  retransmit_count;
97 
98     uint16_t address;
99     uint16_t appkey_index;
100     uint8_t  friendship_credential_flag;
101     uint8_t  period;
102     uint8_t  period_divisor;  // divide period by 2 ^ period_divisor, default = 2^0 = 1, added for Health Server
103     uint8_t  ttl;
104     uint8_t  retransmit;
105 } mesh_publication_model_t;
106 
107 typedef struct {
108     uint8_t  count_log;  // Number of Heartbeat messages to be sent
109     uint8_t  period_log; // Period for sending Heartbeat messages
110     uint8_t  ttl;        // TTL to be used when sending Heartbeat messages
111     uint16_t features;   // Bit field indicating features that trigger Heartbeat messages when changed
112     uint16_t netkey_index;
113 } mesh_heartbeat_publication_state_t;
114 
115 typedef struct {
116     uint32_t opcode;
117     uint16_t minimum_length;
118     mesh_operation_handler handler;
119 } mesh_operation_t;
120 
121 typedef struct mesh_model {
122     // linked list item
123     btstack_linked_item_t item;
124 
125     // element
126     struct mesh_element * element;
127 
128     // internal model enumeration
129     uint16_t mid;
130 
131     // vendor_id << 16 | model id, use BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC for SIG models
132     uint32_t model_identifier;
133 
134     // model operations
135     const mesh_operation_t * operations;
136 
137     // publication model if supported
138     mesh_publication_model_t * publication_model;
139 
140     // data
141     void * model_data;
142 
143     // bound appkeys
144     uint16_t appkey_indices[MAX_NR_MESH_APPKEYS_PER_MODEL];
145 
146     // subscription list
147     uint16_t subscriptions[MAX_NR_MESH_SUBSCRIPTION_PER_MODEL];
148 
149     // packet handler for transition events in server, event callback handler in client
150     btstack_packet_handler_t model_packet_handler;
151 } mesh_model_t;
152 
153 typedef struct {
154     btstack_linked_list_iterator_t it;
155 } mesh_model_iterator_t;
156 
157 typedef struct mesh_element {
158     // linked list item
159     btstack_linked_item_t item;
160 
161     // element index
162     uint16_t element_index;
163 
164     // LOC
165     uint16_t loc;
166 
167     // models
168     btstack_linked_list_t models;
169     uint16_t models_count_sig;
170     uint16_t models_count_vendor;
171 
172 } mesh_element_t;
173 
174 typedef struct {
175     btstack_linked_list_iterator_t it;
176 } mesh_element_iterator_t;
177 
178 
179 void mesh_node_init(void);
180 
181 /**
182  * @brief Set unicast address of primary element
183  * @param unicast_address
184  */
185 void mesh_node_primary_element_address_set(uint16_t unicast_address);
186 
187 /**
188  * @brief Set location of primary element
189  * @note Returned by Configuration Server Composite Data
190  * @param location
191  */
192 void mesh_node_set_primary_element_location(uint16_t location);
193 
194 /**
195  * @brief Set location of element
196  * @param element
197  * @param location
198  */
199 void mesh_node_set_element_location(mesh_element_t * element, uint16_t location);
200 
201 /**
202  * @brief Get unicast address of primary element
203  */
204 uint16_t mesh_node_get_primary_element_address(void);
205 
206 /**
207  * @brief Get Primary Element of this node
208  */
209 mesh_element_t * mesh_node_get_primary_element(void);
210 
211 /**
212  * @brief Add secondary element
213  * @param element
214  */
215 void mesh_node_add_element(mesh_element_t * element);
216 
217 /**
218  * @brief Get number elements
219  * @returns number of elements on this node
220  */
221 uint16_t mesh_node_element_count(void);
222 
223 /**
224  * @brief Get element for given unicast address
225  * @param unicast_address
226  */
227 mesh_element_t * mesh_node_element_for_unicast_address(uint16_t unicast_address);
228 
229 /**
230  * @brief Get element by index
231  * @param element_index
232  */
233 mesh_element_t * mesh_node_element_for_index(uint16_t element_index);
234 
235 /**
236  * @brief Get element index for give model
237  * @param mesh_model
238  */
239 uint8_t mesh_access_get_element_index(mesh_model_t * mesh_model);
240 
241 /**
242  * @brief Get unicast address for give model
243  * @param mesh_model
244  */
245 uint16_t mesh_access_get_element_address(mesh_model_t * mesh_model);
246 
247 /**
248  * @brief Add model to element
249  * @param element
250  * @param mesh_model
251  */
252 void mesh_element_add_model(mesh_element_t * element, mesh_model_t * mesh_model);
253 
254 // Mesh Element Iterator
255 void mesh_element_iterator_init(mesh_element_iterator_t * iterator);
256 
257 int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator);
258 
259 mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator);
260 
261 // Mesh Model Iterator
262 
263 void mesh_model_iterator_init(mesh_model_iterator_t * iterator, mesh_element_t * element);
264 
265 int mesh_model_iterator_has_next(mesh_model_iterator_t * iterator);
266 
267 mesh_model_t * mesh_model_iterator_next(mesh_model_iterator_t * iterator);
268 
269 // Mesh Model Utility
270 
271 mesh_model_t * mesh_model_get_by_identifier(mesh_element_t * element, uint32_t model_identifier);
272 
273 uint32_t mesh_model_get_model_identifier_bluetooth_sig(uint16_t model_id);
274 
275 int mesh_model_is_bluetooth_sig(uint32_t model_identifier);
276 
277 uint16_t mesh_model_get_model_id(uint32_t model_identifier);
278 
279 uint32_t mesh_model_get_model_identifier(uint16_t vendor_id, uint16_t model_id);
280 
281 uint16_t mesh_model_get_vendor_id(uint32_t model_identifier);
282 
283 mesh_model_t * mesh_node_get_configuration_server(void);
284 
285 mesh_model_t * mesh_node_get_health_server(void);
286 
287 mesh_model_t * mesh_access_model_for_address_and_model_identifier(uint16_t element_address, uint32_t model_identifier, uint8_t * status);
288 
289 void mesh_model_reset_appkeys(mesh_model_t * mesh_model);
290 
291 // Mesh Model Subscriptions
292 int mesh_model_contains_subscription(mesh_model_t * mesh_model, uint16_t address);
293 
294 /**
295  * @brief Set Device UUID
296  * @param device_uuid
297  */
298 void mesh_node_set_device_uuid(const uint8_t * device_uuid);
299 
300 /**
301  * @brief Get Device UUID
302  * @returns device_uuid if set, NULL otherwise
303  */
304 const uint8_t * mesh_node_get_device_uuid(void);
305 
306 /**
307  * @brief Set node info reported in Composition Data Page 0
308  * @param company_id (cid)
309  * @param product_id (pid)
310  * @param product_version_id (vid)
311  */
312 void mesh_node_set_info(uint16_t company_id, uint16_t product_id, uint16_t product_version_id);
313 
314 /**
315  * @brief Get node info: company_id
316  * @returns company_id
317  */
318 uint16_t mesh_node_get_company_id(void);
319 
320 /**
321  * @brief Get node info: product_id
322  * @returns product_id
323  */
324 uint16_t mesh_node_get_product_id(void);
325 
326 /**
327  * @brief Get node info: product_version_id
328  * @returns product_version_id
329  */
330 uint16_t mesh_node_get_product_version_id(void);
331 
332 #if defined __cplusplus
333 }
334 #endif
335 
336 #endif //__MESH_NODE_H
337