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 BLUEKITCHEN 24 * GMBH 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 // @return 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 btstack_context_callback_registration_t send_request; 94 mesh_model_publication_state_t state; 95 uint32_t next_publication_ms; 96 uint32_t next_retransmit_ms; 97 uint8_t retransmit_count; 98 99 uint16_t address; 100 uint16_t appkey_index; 101 uint8_t friendship_credential_flag; 102 uint8_t period; 103 uint8_t period_divisor; // divide period by 2 ^ period_divisor, default = 2^0 = 1, added for Health Server 104 uint8_t ttl; 105 uint8_t retransmit; 106 } mesh_publication_model_t; 107 108 typedef struct { 109 uint16_t destination; 110 uint16_t count; // Number of Heartbeat messages to be sent 111 uint16_t period_s; // Period for sending Heartbeat messages in seconds 112 uint16_t features; // Bit field indicating features that trigger Heartbeat messages when changed 113 uint16_t netkey_index; 114 uint8_t ttl; // TTL to be used when sending Heartbeat messages 115 } mesh_heartbeat_publication_state_t; 116 117 typedef struct { 118 uint32_t opcode; 119 uint16_t minimum_length; 120 mesh_operation_handler handler; 121 } mesh_operation_t; 122 123 typedef struct mesh_model { 124 // linked list item 125 btstack_linked_item_t item; 126 127 // element 128 struct mesh_element * element; 129 130 // internal model enumeration 131 uint16_t mid; 132 133 // vendor_id << 16 | model id, use BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC for SIG models 134 uint32_t model_identifier; 135 136 // model operations 137 const mesh_operation_t * operations; 138 139 // publication model if supported 140 mesh_publication_model_t * publication_model; 141 142 // data 143 void * model_data; 144 145 // bound appkeys 146 uint16_t appkey_indices[MAX_NR_MESH_APPKEYS_PER_MODEL]; 147 148 // subscription list 149 uint16_t subscriptions[MAX_NR_MESH_SUBSCRIPTION_PER_MODEL]; 150 151 // packet handler for transition events in server, event callback handler in client 152 btstack_packet_handler_t model_packet_handler; 153 } mesh_model_t; 154 155 typedef struct { 156 btstack_linked_list_iterator_t it; 157 } mesh_model_iterator_t; 158 159 typedef struct mesh_element { 160 // linked list item 161 btstack_linked_item_t item; 162 163 // element index 164 uint16_t element_index; 165 166 // LOC 167 uint16_t loc; 168 169 // models 170 btstack_linked_list_t models; 171 uint16_t models_count_sig; 172 uint16_t models_count_vendor; 173 174 } mesh_element_t; 175 176 typedef struct { 177 btstack_linked_list_iterator_t it; 178 } mesh_element_iterator_t; 179 180 181 void mesh_node_init(void); 182 183 /** 184 * @brief Set unicast address of primary element 185 * @param unicast_address 186 */ 187 void mesh_node_primary_element_address_set(uint16_t unicast_address); 188 189 /** 190 * @brief Set location of primary element 191 * @note Returned by Configuration Server Composite Data 192 * @param location 193 */ 194 void mesh_node_set_primary_element_location(uint16_t location); 195 196 /** 197 * @brief Set location of element 198 * @param element 199 * @param location 200 */ 201 void mesh_node_set_element_location(mesh_element_t * element, uint16_t location); 202 203 /** 204 * @brief Get unicast address of primary element 205 */ 206 uint16_t mesh_node_get_primary_element_address(void); 207 208 /** 209 * @brief Get Primary Element of this node 210 */ 211 mesh_element_t * mesh_node_get_primary_element(void); 212 213 /** 214 * @brief Add secondary element 215 * @param element 216 */ 217 void mesh_node_add_element(mesh_element_t * element); 218 219 /** 220 * @brief Get number elements 221 * @return number of elements on this node 222 */ 223 uint16_t mesh_node_element_count(void); 224 225 /** 226 * @brief Get element for given unicast address 227 * @param unicast_address 228 */ 229 mesh_element_t * mesh_node_element_for_unicast_address(uint16_t unicast_address); 230 231 /** 232 * @brief Get element by index 233 * @param element_index 234 */ 235 mesh_element_t * mesh_node_element_for_index(uint16_t element_index); 236 237 /** 238 * @brief Get element index for give model 239 * @param mesh_model 240 */ 241 uint8_t mesh_access_get_element_index(mesh_model_t * mesh_model); 242 243 /** 244 * @brief Get unicast address for give model 245 * @param mesh_model 246 */ 247 uint16_t mesh_access_get_element_address(mesh_model_t * mesh_model); 248 249 /** 250 * @brief Add model to element 251 * @param element 252 * @param mesh_model 253 */ 254 void mesh_element_add_model(mesh_element_t * element, mesh_model_t * mesh_model); 255 256 // Mesh Element Iterator 257 void mesh_element_iterator_init(mesh_element_iterator_t * iterator); 258 259 int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator); 260 261 mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator); 262 263 // Mesh Model Iterator 264 265 void mesh_model_iterator_init(mesh_model_iterator_t * iterator, mesh_element_t * element); 266 267 int mesh_model_iterator_has_next(mesh_model_iterator_t * iterator); 268 269 mesh_model_t * mesh_model_iterator_next(mesh_model_iterator_t * iterator); 270 271 // Mesh Model Utility 272 273 mesh_model_t * mesh_model_get_by_identifier(mesh_element_t * element, uint32_t model_identifier); 274 275 uint32_t mesh_model_get_model_identifier_bluetooth_sig(uint16_t model_id); 276 277 int mesh_model_is_bluetooth_sig(uint32_t model_identifier); 278 279 uint16_t mesh_model_get_model_id(uint32_t model_identifier); 280 281 uint32_t mesh_model_get_model_identifier(uint16_t vendor_id, uint16_t model_id); 282 283 uint16_t mesh_model_get_vendor_id(uint32_t model_identifier); 284 285 mesh_model_t * mesh_node_get_configuration_server(void); 286 287 mesh_model_t * mesh_node_get_health_server(void); 288 289 mesh_model_t * mesh_access_model_for_address_and_model_identifier(uint16_t element_address, uint32_t model_identifier, uint8_t * status); 290 291 void mesh_model_reset_appkeys(mesh_model_t * mesh_model); 292 293 // Mesh Model Subscriptions 294 int mesh_model_contains_subscription(mesh_model_t * mesh_model, uint16_t address); 295 296 /** 297 * @brief Set Device UUID 298 * @param device_uuid 299 */ 300 void mesh_node_set_device_uuid(const uint8_t * device_uuid); 301 302 /** 303 * @brief Get Device UUID 304 * @return device_uuid if set, NULL otherwise 305 */ 306 const uint8_t * mesh_node_get_device_uuid(void); 307 308 /** 309 * @brief Set node info reported in Composition Data Page 0 310 * @param company_id (cid) 311 * @param product_id (pid) 312 * @param product_version_id (vid) 313 */ 314 void mesh_node_set_info(uint16_t company_id, uint16_t product_id, uint16_t product_version_id); 315 316 /** 317 * @brief Get node info: company_id 318 * @return company_id 319 */ 320 uint16_t mesh_node_get_company_id(void); 321 322 /** 323 * @brief Get node info: product_id 324 * @return product_id 325 */ 326 uint16_t mesh_node_get_product_id(void); 327 328 /** 329 * @brief Get node info: product_version_id 330 * @return product_version_id 331 */ 332 uint16_t mesh_node_get_product_version_id(void); 333 334 335 // Heartbeat (helper) 336 uint16_t mesh_heartbeat_pwr2(uint8_t value); 337 uint8_t mesh_heartbeat_count_log(uint16_t value); 338 uint8_t mesh_heartbeat_period_log(uint16_t value); 339 340 #if defined __cplusplus 341 } 342 #endif 343 344 #endif //__MESH_NODE_H 345