xref: /btstack/src/mesh/mesh_node.h (revision 39f043d5d353f301576e65978b6a13e10c0bb73e)
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 struct mesh_model;
56 struct mesh_element;
57 
58 // function to handle model operation message
59 typedef void (*mesh_operation_handler)(struct mesh_model * mesh_model, mesh_pdu_t * pdu);
60 
61 // function to publish the current state of a model
62 // @param mesh_model to publish
63 // @returns mesh_pdu with status message
64 typedef mesh_pdu_t * (*mesh_publish_state_t)(struct mesh_model * mesh_model);
65 
66 typedef enum {
67     MESH_NODE_IDENTITY_STATE_ADVERTISING_STOPPED = 0,
68     MESH_NODE_IDENTITY_STATE_ADVERTISING_RUNNING,
69     MESH_NODE_IDENTITY_STATE_ADVERTISING_NOT_SUPPORTED
70 } mesh_node_identity_state_t;
71 
72 typedef enum {
73     MESH_FRIEND_STATE_DISABLED = 0,
74     MESH_FRIEND_STATE_ENABLED,
75     MESH_FRIEND_STATE_NOT_SUPPORTED
76 } mesh_friend_state_t;
77 
78 typedef enum {
79     MESH_MODEL_PUBLICATION_STATE_IDLE,
80     MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS,
81     MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY,
82     MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS,
83     MESH_MODEL_PUBLICATION_STATE_RETRANSMIT_READY,
84 } mesh_model_publication_state_t;
85 
86 typedef struct {
87     mesh_publish_state_t publish_state_fn;
88     mesh_model_publication_state_t state;
89     uint32_t next_publication_ms;
90     uint32_t next_retransmit_ms;
91     uint8_t  retransmit_count;
92 
93     uint16_t address;
94     uint16_t appkey_index;
95     uint8_t  friendship_credential_flag;
96     uint8_t  period;
97     uint8_t  period_divisor;  // divide period by 2 ^ period_divisor, default = 2^0 = 1, added for Health Server
98     uint8_t  ttl;
99     uint8_t  retransmit;
100 } mesh_publication_model_t;
101 
102 typedef struct {
103     uint32_t opcode;
104     uint16_t minimum_length;
105     mesh_operation_handler handler;
106 } mesh_operation_t;
107 
108 typedef struct mesh_model {
109     // linked list item
110     btstack_linked_item_t item;
111 
112     // element
113     struct mesh_element * element;
114 
115     // internal model enumeration
116     uint16_t mid;
117 
118     // vendor_id << 16 | model id, use BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC for SIG models
119     uint32_t model_identifier;
120 
121     // model operations
122     const mesh_operation_t * operations;
123 
124     // publication model if supported
125     mesh_publication_model_t * publication_model;
126 
127     // data
128     void * model_data;
129 
130     // bound appkeys
131     uint16_t appkey_indices[MAX_NR_MESH_APPKEYS_PER_MODEL];
132 
133     // subscription list
134     uint16_t subscriptions[MAX_NR_MESH_SUBSCRIPTION_PER_MODEL];
135 
136     // packet handler for transition events in server, event callback handler in client
137     btstack_packet_handler_t model_packet_handler;
138 } mesh_model_t;
139 
140 typedef struct {
141     btstack_linked_list_iterator_t it;
142 } mesh_model_iterator_t;
143 
144 typedef struct mesh_element {
145     // linked list item
146     btstack_linked_item_t item;
147 
148     // element index
149     uint16_t element_index;
150 
151     // LOC
152     uint16_t loc;
153 
154     // models
155     btstack_linked_list_t models;
156     uint16_t models_count_sig;
157     uint16_t models_count_vendor;
158 
159 } mesh_element_t;
160 
161 typedef struct {
162     btstack_linked_list_iterator_t it;
163 } mesh_element_iterator_t;
164 
165 
166 void mesh_node_init(void);
167 
168 /**
169  * @brief Set unicast address of primary element
170  * @param unicast_address
171  */
172 void mesh_node_primary_element_address_set(uint16_t unicast_address);
173 
174 /**
175  * @brief Set location of primary element
176  * @note Returned by Configuration Server Composite Data
177  * @param location
178  */
179 void mesh_node_set_primary_element_location(uint16_t location);
180 
181 /**
182  * @brief Set location of element
183  * @param element
184  * @param location
185  */
186 void mesh_node_set_element_location(mesh_element_t * element, uint16_t location);
187 
188 /**
189  * @brief Get unicast address of primary element
190  */
191 uint16_t mesh_node_get_primary_element_address(void);
192 
193 /**
194  * @brief Get Primary Element of this node
195  */
196 mesh_element_t * mesh_node_get_primary_element(void);
197 
198 /**
199  * @brief Add secondary element
200  * @param element
201  */
202 void mesh_node_add_element(mesh_element_t * element);
203 
204 /**
205  * @brief Get number elements
206  * @returns number of elements on this node
207  */
208 uint16_t mesh_node_element_count(void);
209 
210 /**
211  * @brief Get element for given unicast address
212  * @param unicast_address
213  */
214 mesh_element_t * mesh_node_element_for_unicast_address(uint16_t unicast_address);
215 
216 /**
217  * @brief Get element by index
218  * @param element_index
219  */
220 mesh_element_t * mesh_node_element_for_index(uint16_t element_index);
221 
222 /**
223  * @brief Get element index for give model
224  * @param mesh_model
225  */
226 uint8_t mesh_access_get_element_index(mesh_model_t * mesh_model);
227 
228 /**
229  * @brief Get unicast address for give model
230  * @param mesh_model
231  */
232 uint16_t mesh_access_get_element_address(mesh_model_t * mesh_model);
233 
234 /**
235  * @brief Add model to element
236  * @param element
237  * @param mesh_model
238  */
239 void mesh_element_add_model(mesh_element_t * element, mesh_model_t * mesh_model);
240 
241 // Mesh Element Iterator
242 void mesh_element_iterator_init(mesh_element_iterator_t * iterator);
243 
244 int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator);
245 
246 mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator);
247 
248 // Mesh Model Iterator
249 
250 void mesh_model_iterator_init(mesh_model_iterator_t * iterator, mesh_element_t * element);
251 
252 int mesh_model_iterator_has_next(mesh_model_iterator_t * iterator);
253 
254 mesh_model_t * mesh_model_iterator_next(mesh_model_iterator_t * iterator);
255 
256 // Mesh Model Utility
257 
258 mesh_model_t * mesh_model_get_by_identifier(mesh_element_t * element, uint32_t model_identifier);
259 
260 uint32_t mesh_model_get_model_identifier_bluetooth_sig(uint16_t model_id);
261 
262 int mesh_model_is_bluetooth_sig(uint32_t model_identifier);
263 
264 uint16_t mesh_model_get_model_id(uint32_t model_identifier);
265 
266 uint32_t mesh_model_get_model_identifier(uint16_t vendor_id, uint16_t model_id);
267 
268 uint16_t mesh_model_get_vendor_id(uint32_t model_identifier);
269 
270 mesh_model_t * mesh_node_get_configuration_server(void);
271 
272 mesh_model_t * mesh_node_get_health_server(void);
273 
274 mesh_model_t * mesh_access_model_for_address_and_model_identifier(uint16_t element_address, uint32_t model_identifier, uint8_t * status);
275 
276 void mesh_model_reset_appkeys(mesh_model_t * mesh_model);
277 
278 // Mesh Model Subscriptions
279 int mesh_model_contains_subscription(mesh_model_t * mesh_model, uint16_t address);
280 
281 /**
282  * @brief Set Device UUID
283  * @param device_uuid
284  */
285 void mesh_node_set_device_uuid(const uint8_t * device_uuid);
286 
287 /**
288  * @brief Get Device UUID
289  * @returns device_uuid if set, NULL otherwise
290  */
291 const uint8_t * mesh_node_get_device_uuid(void);
292 
293 /**
294  * @brief Set node info reported in Composition Data Page 0
295  * @param company_id (cid)
296  * @param product_id (pid)
297  * @param product_version_id (vid)
298  */
299 void mesh_node_set_info(uint16_t company_id, uint16_t product_id, uint16_t product_version_id);
300 
301 /**
302  * @brief Get node info: company_id
303  * @returns company_id
304  */
305 uint16_t mesh_node_get_company_id(void);
306 
307 /**
308  * @brief Get node info: product_id
309  * @returns product_id
310  */
311 uint16_t mesh_node_get_product_id(void);
312 
313 /**
314  * @brief Get node info: product_version_id
315  * @returns product_version_id
316  */
317 uint16_t mesh_node_get_product_version_id(void);
318 
319 #if defined __cplusplus
320 }
321 #endif
322 
323 #endif //__MESH_NODE_H
324