1 /* 2 * Copyright (C) 2018 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_NETWORK 39 #define __MESH_NETWORK 40 41 #include "btstack_linked_list.h" 42 #include "btstack_run_loop.h" 43 44 #include "mesh/provisioning.h" 45 #include "mesh/mesh_keys.h" 46 47 #if defined __cplusplus 48 extern "C" { 49 #endif 50 51 #define MESH_DEVICE_KEY_INDEX 0xffff 52 53 #define MESH_NETWORK_PAYLOAD_MAX 29 54 #define MESH_ACCESS_PAYLOAD_MAX 384 55 56 #define MESH_ADDRESS_UNSASSIGNED 0x0000u 57 #define MESH_ADDRESS_ALL_PROXIES 0xFFFCu 58 #define MESH_ADDRESS_ALL_FRIENDS 0xFFFDu 59 #define MESH_ADDRESS_ALL_RELAYS 0xFFFEu 60 #define MESH_ADDRESS_ALL_NODES 0xFFFFu 61 62 typedef enum { 63 MESH_NETWORK_PDU_RECEIVED, 64 MESH_NETWORK_PDU_SENT, 65 MESH_NETWORK_PDU_ENCRYPTED, 66 MESH_NETWORK_CAN_SEND_NOW, 67 } mesh_network_callback_type_t; 68 69 typedef enum { 70 MESH_PDU_TYPE_NETWORK = 0, 71 MESH_PDU_TYPE_TRANSPORT, 72 MESH_PDU_TYPE_SEGMENTED, 73 MESH_PDU_TYPE_UNSEGMENTED, 74 MESH_PDU_TYPE_ACCESS, 75 } mesh_pdu_type_t; 76 77 typedef struct mesh_pdu { 78 // allow for linked lists 79 btstack_linked_item_t item; 80 // type 81 mesh_pdu_type_t pdu_type; 82 83 } mesh_pdu_t; 84 85 // 86 #define MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION 1 87 #define MESH_NETWORK_PDU_FLAGS_GATT_BEARER 2 88 #define MESH_NETWORK_PDU_FLAGS_RELAY 4 89 90 typedef struct mesh_network_pdu { 91 mesh_pdu_t pdu_header; 92 93 // meta data network layer 94 uint16_t netkey_index; 95 // MESH_NETWORK_PDU_FLAGS 96 uint16_t flags; 97 98 // pdu 99 uint16_t len; 100 uint8_t data[MESH_NETWORK_PAYLOAD_MAX]; 101 } mesh_network_pdu_t; 102 103 #define MESH_TRANSPORT_FLAG_SEQ_RESERVED 1 104 #define MESH_TRANSPORT_FLAG_CONTROL 2 105 106 107 typedef struct { 108 // generic pdu header 109 mesh_pdu_t pdu_header; 110 // meta data transport layer 111 uint16_t appkey_index; 112 // MESH_TRANSPORT_FLAG 113 uint16_t flags; 114 // pdu segment 115 mesh_network_pdu_t * segment; 116 } mesh_unsegmented_pdu_t; 117 118 typedef struct { 119 mesh_pdu_t pdu_header; 120 121 // access acknowledged message 122 uint16_t retransmit_count; 123 uint32_t retransmit_timeout_ms; 124 uint32_t ack_opcode; 125 126 // rx/tx: acknowledgement timer / segment transmission timer 127 btstack_timer_source_t acknowledgement_timer; 128 // rx: incomplete timer / tx: resend timer 129 btstack_timer_source_t incomplete_timer; 130 // block access 131 uint32_t block_ack; 132 // meta data network layer 133 uint16_t netkey_index; 134 // meta data transport layer 135 uint16_t appkey_index; 136 // transmic size 137 uint8_t transmic_len; 138 // akf - aid for access, opcode for control 139 uint8_t akf_aid_control; 140 // network pdu header 141 uint8_t network_header[9]; 142 // MESH_TRANSPORT_FLAG 143 uint16_t flags; 144 // acknowledgement timer active 145 uint8_t acknowledgement_timer_active; 146 // incomplete timer active 147 uint8_t incomplete_timer_active; 148 // message complete 149 uint8_t message_complete; 150 // seq_zero for segmented messages 151 uint16_t seq_zero; 152 // pdu segments 153 uint16_t len; 154 btstack_linked_list_t segments; 155 } mesh_segmented_pdu_t; 156 157 typedef struct { 158 mesh_pdu_t pdu_header; 159 160 // access acknowledged message 161 uint16_t retransmit_count; 162 uint32_t retransmit_timeout_ms; 163 uint32_t ack_opcode; 164 165 // meta data network layer 166 uint16_t netkey_index; 167 // meta data transport layer 168 uint16_t appkey_index; 169 // transmic size 170 uint8_t transmic_len; 171 // akf - aid for access, opcode for control 172 uint8_t akf_aid_control; 173 // network pdu header 174 uint8_t network_header[9]; 175 // MESH_TRANSPORT_FLAG 176 uint16_t flags; 177 // pdu 178 uint16_t len; 179 uint8_t data[MESH_ACCESS_PAYLOAD_MAX]; 180 } mesh_transport_pdu_t; 181 182 typedef struct { 183 // generic pdu header 184 mesh_pdu_t pdu_header; 185 // meta data network layer 186 uint16_t netkey_index; 187 // meta data transport layer 188 uint16_t appkey_index; 189 // transmic size 190 uint8_t transmic_len; 191 // akf - aid for access, opcode for control 192 uint8_t akf_aid_control; 193 // network pdu header 194 uint8_t network_header[9]; 195 // MESH_TRANSPORT_FLAG 196 uint16_t flags; 197 // payload 198 uint16_t len; 199 uint8_t data[MESH_ACCESS_PAYLOAD_MAX]; 200 } mesh_access_pdu_t; 201 202 typedef enum { 203 MESH_KEY_REFRESH_NOT_ACTIVE = 0, 204 MESH_KEY_REFRESH_FIRST_PHASE, 205 MESH_KEY_REFRESH_SECOND_PHASE 206 } mesh_key_refresh_state_t; 207 208 typedef enum { 209 MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE, 210 MESH_SECURE_NETWORK_BEACON_W4_AUTH_VALUE, 211 MESH_SECURE_NETWORK_BEACON_AUTH_VALUE, 212 MESH_SECURE_NETWORK_BEACON_W2_SEND_ADV, 213 MESH_SECURE_NETWORK_BEACON_ADV_SENT, 214 MESH_SECURE_NETWORK_BEACON_W2_SEND_GATT, 215 MESH_SECURE_NETWORK_BEACON_GATT_SENT, 216 MESH_SECURE_NETWORK_BEACON_W4_INTERVAL 217 } mesh_secure_network_beacon_state_t; 218 219 typedef struct { 220 btstack_linked_item_t item; 221 222 // netkey index 223 uint16_t netkey_index; 224 225 // current / old key 226 mesh_network_key_t * old_key; 227 228 // new key (only set during key refresh) 229 mesh_network_key_t * new_key; 230 231 // key refresh state 232 mesh_key_refresh_state_t key_refresh; 233 234 // advertisement using node id active 235 uint8_t node_id_advertisement_running; 236 237 238 // advertisement using network id (used by proxy) 239 adv_bearer_connectable_advertisement_data_item_t advertisement_with_network_id; 240 241 // advertising using node id (used by proxy) 242 adv_bearer_connectable_advertisement_data_item_t advertisement_with_node_id; 243 244 // secure network beacons 245 mesh_secure_network_beacon_state_t beacon_state; 246 uint32_t beacon_interval_ms; 247 uint32_t beacon_observation_start_ms; 248 uint16_t beacon_observation_counter; 249 250 } mesh_subnet_t; 251 252 typedef struct { 253 btstack_linked_list_iterator_t it; 254 } mesh_subnet_iterator_t; 255 256 /** 257 * @brief Init Mesh Network Layer 258 */ 259 void mesh_network_init(void); 260 261 /** 262 * @brief Set higher layer Network PDU handler 263 * @param packet_handler 264 */ 265 void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)); 266 267 /** 268 * @brief Set higher layer Proxy PDU handler 269 * @param packet_handler 270 */ 271 void mesh_network_set_proxy_message_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)); 272 273 /** 274 * @brief Mark packet as processed 275 * @param newtork_pdu received via call packet_handler 276 */ 277 void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu); 278 279 /** 280 * @brief Send network_pdu after encryption 281 * @param network_pdu 282 */ 283 void mesh_network_send_pdu(mesh_network_pdu_t * network_pdu); 284 285 /* 286 * @brief Setup network pdu header 287 * @param netkey_index 288 * @param nid 289 * @param ctl 290 * @param ttl 291 * @param seq 292 * @param dst 293 * @param transport_pdu_data 294 * @param transport_pdu_len 295 */ 296 void mesh_network_setup_pdu(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint8_t nid, uint8_t ctl, uint8_t ttl, uint32_t seq, uint16_t src, uint16_t dst, const uint8_t * transport_pdu_data, uint8_t transport_pdu_len); 297 298 /** 299 * Setup network pdu header without modifying len or payload 300 * @param network_pdu 301 * @param netkey_index 302 * @param nid 303 * @param ctl 304 * @param ttl 305 * @param seq 306 * @param src 307 * @param dest 308 */ 309 void mesh_network_setup_pdu_header(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint8_t nid, uint8_t ctl, uint8_t ttl, uint32_t seq, uint16_t src, uint16_t dest); 310 311 /** 312 * @brief Validate network addresses 313 * @param ctl 314 * @param src 315 * @param dst 316 * @returns 1 if valid, 317 */ 318 int mesh_network_addresses_valid(uint8_t ctl, uint16_t src, uint16_t dst); 319 320 /** 321 * @brief Check if Unicast address 322 * @param addr 323 * @returns 1 if unicast 324 */ 325 int mesh_network_address_unicast(uint16_t addr); 326 327 /** 328 * @brief Check if Unicast address 329 * @param addr 330 * @returns 1 if unicast 331 */ 332 int mesh_network_address_group(uint16_t addr); 333 334 /** 335 * @brief Check if All Proxies address 336 * @param addr 337 * @returns 1 if all proxies 338 */ 339 int mesh_network_address_all_proxies(uint16_t addr); 340 341 /** 342 * @brief Check if All Nodes address 343 * @param addr 344 * @returns 1 if all nodes 345 */ 346 int mesh_network_address_all_nodes(uint16_t addr); 347 348 /** 349 * @brief Check if All Friends address 350 * @param addr 351 * @returns 1 if all friends 352 */ 353 int mesh_network_address_all_friends(uint16_t addr); 354 355 /** 356 * @brief Check if All Relays address 357 * @param addr 358 * @returns 1 if all relays 359 */ 360 int mesh_network_address_all_relays(uint16_t addr); 361 362 363 /** 364 * @brief Check if Virtual address 365 * @param addr 366 * @returns 1 if virtual 367 */ 368 int mesh_network_address_virtual(uint16_t addr); 369 370 371 /** 372 * @brief Add subnet to list 373 * @param subnet 374 */ 375 void mesh_subnet_add(mesh_subnet_t * subnet); 376 377 /** 378 * @brief Remove subnet from list 379 * @param subnet 380 */ 381 void mesh_subnet_remove(mesh_subnet_t * subnet); 382 383 /** 384 * @brief Get subnet for netkey_index 385 * @param netkey_index 386 * @returns mesh_subnet_t or NULL 387 */ 388 mesh_subnet_t * mesh_subnet_get_by_netkey_index(uint16_t netkey_index); 389 390 /** 391 * @brief Get number of stored subnets 392 * @returns count 393 */ 394 int mesh_subnet_list_count(void); 395 396 /** 397 * @brief Iterate over all subnets 398 * @param it 399 */ 400 void mesh_subnet_iterator_init(mesh_subnet_iterator_t *it); 401 402 /** 403 * @brief Check if another subnet is available 404 * @param it 405 * @return 406 */ 407 int mesh_subnet_iterator_has_more(mesh_subnet_iterator_t *it); 408 409 /** 410 * @brief Get next subnet 411 * @param it 412 * @return 413 */ 414 mesh_subnet_t * mesh_subnet_iterator_get_next(mesh_subnet_iterator_t *it); 415 416 /** 417 * @brief Setup subnet for given netkey index 418 */ 419 void mesh_subnet_setup_for_netkey_index(uint16_t netkey_index); 420 421 422 /** 423 * @brief Get outgoing network key for subnet based on key refresh phase 424 */ 425 mesh_network_key_t * mesh_subnet_get_outgoing_network_key(mesh_subnet_t * subnet); 426 427 // buffer pool 428 mesh_network_pdu_t * mesh_network_pdu_get(void); 429 void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu); 430 431 // Mesh Network PDU Getter 432 uint16_t mesh_network_control(mesh_network_pdu_t * network_pdu); 433 uint8_t mesh_network_nid(mesh_network_pdu_t * network_pdu); 434 uint8_t mesh_network_ttl(mesh_network_pdu_t * network_pdu); 435 uint32_t mesh_network_seq(mesh_network_pdu_t * network_pdu); 436 uint16_t mesh_network_src(mesh_network_pdu_t * network_pdu); 437 uint16_t mesh_network_dst(mesh_network_pdu_t * network_pdu); 438 int mesh_network_segmented(mesh_network_pdu_t * network_pdu); 439 uint8_t mesh_network_control_opcode(mesh_network_pdu_t * network_pdu); 440 uint8_t * mesh_network_pdu_data(mesh_network_pdu_t * network_pdu); 441 uint8_t mesh_network_pdu_len(mesh_network_pdu_t * network_pdu); 442 443 // Mesh Network PDU Setter 444 void mesh_network_pdu_set_seq(mesh_network_pdu_t * network_pdu, uint32_t seq); 445 446 // Testing only 447 void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len, uint8_t flags); 448 void mesh_network_process_proxy_configuration_message(const uint8_t * pdu_data, uint8_t pdu_len); 449 void mesh_network_encrypt_proxy_configuration_message(mesh_network_pdu_t * network_pdu); 450 void mesh_network_dump(void); 451 void mesh_network_reset(void); 452 453 #if defined __cplusplus 454 } 455 #endif 456 457 #endif 458