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