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 45 #if defined __cplusplus 46 extern "C" { 47 #endif 48 49 #define MESH_DEVICE_KEY_INDEX 0xffff 50 51 #define MESH_NETWORK_PAYLOAD_MAX 29 52 #define MESH_ACCESS_PAYLOAD_MAX 384 53 54 #define MESH_ADDRESS_UNSASSIGNED 0x0000u 55 #define MESH_ADDRESS_ALL_PROXIES 0xFFFCu 56 #define MESH_ADDRESS_ALL_FRIENDS 0xFFFDu 57 #define MESH_ADDRESS_ALL_RELAYS 0xFFFEu 58 #define MESH_ADDRESS_ALL_NODES 0xFFFFu 59 60 typedef enum { 61 MESH_NETWORK_PDU_RECEIVED, 62 MESH_NETWORK_PDU_SENT, 63 } mesh_network_callback_type_t; 64 65 typedef enum { 66 MESH_PDU_TYPE_NETWORK = 0, 67 MESH_PDU_TYPE_TRANSPORT, 68 } mesh_pdu_type_t; 69 70 typedef struct mesh_pdu { 71 // allow for linked lists 72 btstack_linked_item_t item; 73 // type 74 mesh_pdu_type_t pdu_type; 75 } mesh_pdu_t; 76 77 // 78 #define MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION 1 79 #define MESH_NETWORK_PDU_FLAGS_GATT_BEARER 2 80 #define MESH_NETWORK_PDU_FLAGS_RELAY 4 81 82 typedef struct mesh_network_pdu { 83 mesh_pdu_t pdu_header; 84 85 // callback 86 void (*callback)(struct mesh_network_pdu * network_pdu); 87 88 // meta data network layer 89 uint16_t netkey_index; 90 // meta data transport layer 91 uint16_t appkey_index; 92 // flags: bit 0 indicates Proxy PDU 93 uint16_t flags; 94 95 // pdu 96 uint16_t len; 97 uint8_t data[MESH_NETWORK_PAYLOAD_MAX]; 98 } mesh_network_pdu_t; 99 100 typedef struct { 101 mesh_pdu_t pdu_header; 102 103 // rx/tx: acknowledgement timer / segment transmission timer 104 btstack_timer_source_t acknowledgement_timer; 105 // rx: incomplete timer / tx: resend timer 106 btstack_timer_source_t incomplete_timer; 107 // block access 108 uint32_t block_ack; 109 // meta data network layer 110 uint16_t netkey_index; 111 // meta data transport layer 112 uint16_t appkey_index; 113 // transmic size 114 uint8_t transmic_len; 115 // akf - aid 116 uint8_t akf_aid; 117 // network pdu header 118 uint8_t network_header[9]; 119 // acknowledgement timer active 120 uint8_t acknowledgement_timer_active; 121 // incomplete timer active 122 uint8_t incomplete_timer_active; 123 // message complete 124 uint8_t message_complete; 125 // seq_zero for segmented messages 126 uint16_t seq_zero; 127 // pdu 128 uint16_t len; 129 uint8_t data[MESH_ACCESS_PAYLOAD_MAX]; 130 } mesh_transport_pdu_t; 131 132 133 /** 134 * @brief Init Mesh Network Layer 135 */ 136 void mesh_network_init(void); 137 138 /** 139 * @brief Set higher layer Network PDU handler 140 * @param packet_handler 141 */ 142 void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)); 143 144 /** 145 * @brief Set higher layer Proxy PDU handler 146 * @param packet_handler 147 */ 148 void mesh_network_set_proxy_message_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)); 149 150 /** 151 * @brief Mark packet as processed 152 * @param newtork_pdu received via call packet_handler 153 */ 154 void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu); 155 156 /** 157 * @brief Configure address filter 158 */ 159 void mesh_network_set_primary_element_address(uint16_t addr); 160 161 /** 162 * @brief Send network_pdu after encryption 163 * @param network_pdu 164 */ 165 void mesh_network_send_pdu(mesh_network_pdu_t * network_pdu); 166 167 /* 168 * @brief Setup network pdu header 169 * @param netkey_index 170 * @param nid 171 * @param ctl 172 * @param ttl 173 * @param seq 174 * @param dst 175 * @param transport_pdu_data 176 * @param transport_pdu_len 177 */ 178 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); 179 180 /** 181 * Setup network pdu header without modifying len or payload 182 * @param network_pdu 183 * @param netkey_index 184 * @param nid 185 * @param ctl 186 * @param ttl 187 * @param seq 188 * @param src 189 * @param dest 190 */ 191 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); 192 193 /** 194 * @brief Validate network addresses 195 * @param ctl 196 * @param src 197 * @param dst 198 * @returns 1 if valid, 199 */ 200 int mesh_network_addresses_valid(uint8_t ctl, uint16_t src, uint16_t dst); 201 202 /** 203 * @brief Check if Unicast address 204 * @param addr 205 * @returns 1 if unicast 206 */ 207 int mesh_network_address_unicast(uint16_t addr); 208 209 /** 210 * @brief Check if Unicast address 211 * @param addr 212 * @returns 1 if unicast 213 */ 214 int mesh_network_address_group(uint16_t addr); 215 216 /** 217 * @brief Check if All Proxies address 218 * @param addr 219 * @returns 1 if all proxies 220 */ 221 int mesh_network_address_all_proxies(uint16_t addr); 222 223 /** 224 * @brief Check if All Nodes address 225 * @param addr 226 * @returns 1 if all nodes 227 */ 228 int mesh_network_address_all_nodes(uint16_t addr); 229 230 /** 231 * @brief Check if All Friends address 232 * @param addr 233 * @returns 1 if all friends 234 */ 235 int mesh_network_address_all_friends(uint16_t addr); 236 237 /** 238 * @brief Check if All Relays address 239 * @param addr 240 * @returns 1 if all relays 241 */ 242 int mesh_network_address_all_relays(uint16_t addr); 243 244 245 /** 246 * @brief Check if Virtual address 247 * @param addr 248 * @returns 1 if virtual 249 */ 250 int mesh_network_address_virtual(uint16_t addr); 251 252 // buffer pool 253 mesh_network_pdu_t * mesh_network_pdu_get(void); 254 void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu); 255 256 // Mesh Network PDU Getter 257 uint16_t mesh_network_control(mesh_network_pdu_t * network_pdu); 258 uint8_t mesh_network_nid(mesh_network_pdu_t * network_pdu); 259 uint8_t mesh_network_ttl(mesh_network_pdu_t * network_pdu); 260 uint32_t mesh_network_seq(mesh_network_pdu_t * network_pdu); 261 uint16_t mesh_network_src(mesh_network_pdu_t * network_pdu); 262 uint16_t mesh_network_dst(mesh_network_pdu_t * network_pdu); 263 int mesh_network_segmented(mesh_network_pdu_t * network_pdu); 264 uint8_t * mesh_network_pdu_data(mesh_network_pdu_t * network_pdu); 265 uint8_t mesh_network_pdu_len(mesh_network_pdu_t * network_pdu); 266 267 void mesh_set_iv_index(uint32_t iv_index); 268 uint32_t mesh_get_iv_index(void); 269 270 // Testing only 271 void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len, uint8_t flags); 272 void mesh_network_process_proxy_configuration_message(const uint8_t * pdu_data, uint8_t pdu_len); 273 void mesh_network_encrypt_proxy_configuration_message(mesh_network_pdu_t * network_pdu, void (* callback)(mesh_network_pdu_t * callback)); 274 void mesh_network_dump(void); 275 void mesh_network_reset(void); 276 277 #if defined __cplusplus 278 } 279 #endif 280 281 #endif 282