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