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