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