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