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