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 BLUEKITCHEN 24 * GMBH 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_TRANSPORT_H 39 #define __MESH_TRANSPORT_H 40 41 #include <stdint.h> 42 43 #include "mesh/mesh_network.h" 44 #include "mesh/mesh_lower_transport.h" 45 46 #ifdef __cplusplus 47 extern "C" 48 { 49 #endif 50 51 // upper transport message builder 52 typedef struct { 53 mesh_upper_transport_pdu_t * pdu; 54 mesh_network_pdu_t * segment; 55 } mesh_upper_transport_builder_t; 56 57 /* 58 * @brief reserve 1 x mesh_upper_transport_pdu_t and 14 x mesh_network_pdu_t to allow composition of max access/control message 59 * @return true if enough buffer allocated 60 */ 61 bool mesh_upper_transport_message_reserve(void); 62 63 /* 64 * @brief init message builder for unsegmented access, segmented access, and segmented control messages 65 * @note uses buffers reserved by mesh_upper_transport_message_reserve 66 */ 67 void mesh_upper_transport_message_init(mesh_upper_transport_builder_t * builder, mesh_pdu_type_t pdu_type); 68 69 /** 70 * @brief append data to message 71 * @param builder 72 * @param data 73 * @param data_len 74 */ 75 void mesh_upper_transport_message_add_data(mesh_upper_transport_builder_t * builder, const uint8_t * data, uint16_t data_len); 76 77 /** 78 * @brief append single byte 79 * @param builder 80 * @param value 81 */ 82 void mesh_upper_transport_message_add_uint8(mesh_upper_transport_builder_t * builder, uint8_t value); 83 /** 84 * @brief append uint16 85 * @param builder 86 * @param value 87 */ 88 void mesh_upper_transport_message_add_uint16(mesh_upper_transport_builder_t * builder, uint16_t value); 89 /** 90 * @brief append uint24 91 * @param builder 92 * @param value 93 */ 94 void mesh_upper_transport_message_add_uint24(mesh_upper_transport_builder_t * builder, uint32_t value); 95 /** 96 * @brief append uint32 97 * @param builder 98 * @param value 99 */ 100 void mesh_upper_transport_message_add_uint32(mesh_upper_transport_builder_t * builder, uint32_t value); 101 /** 102 * @brief finalize message 103 * @param builder 104 * @return upper_transport_pdu if message was successfully constructed 105 */ 106 mesh_upper_transport_pdu_t * mesh_upper_transport_message_finalize(mesh_upper_transport_builder_t * builder); 107 108 /** 109 * @brief Initialize Upper Transport Layer 110 */ 111 void mesh_upper_transport_init(void); 112 113 /** 114 * @brief Register for control messages and events 115 * @param callback 116 */ 117 void mesh_upper_transport_register_control_message_handler(void (*callback)(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu)); 118 119 /** 120 * @brief Register for access messages and events 121 * @param callback 122 */ 123 void mesh_upper_transport_register_access_message_handler(void (*callback)(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu)); 124 125 /** 126 * @brief Report received PDU as processed 127 * @param pdu 128 * @note pdu cannot be accessed after call 129 */ 130 void mesh_upper_transport_message_processed_by_higher_layer(mesh_pdu_t * pdu); 131 132 /** 133 * @brief Request to send upper transport PDU 134 * @param request 135 */ 136 void mesh_upper_transport_request_to_send(btstack_context_callback_registration_t * request); 137 138 /** 139 * @brief Free any PDU type 140 * @param pdu 141 */ 142 void mesh_upper_transport_pdu_free(mesh_pdu_t * pdu); 143 144 // Control PDUs: setup and send 145 146 /** 147 * @brief Setup Unsegmented Control Pdu provided by network_pdu param 148 * @param network_pdu 149 * @param netkey_index 150 * @param ttl 151 * @param src 152 * @param dest 153 * @param opcode 154 * @param control_pdu_data 155 * @param control_pdu_len 156 * @return 0 on success 157 */ 158 uint8_t mesh_upper_transport_setup_unsegmented_control_pdu(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode, 159 const uint8_t * control_pdu_data, uint16_t control_pdu_len); 160 161 /** 162 * @brief Setup header of Segmented Control PDU created with mesh_upper_transport_message_finalize 163 * @param upper_pdu 164 * @param netkey_index 165 * @param ttl 166 * @param src 167 * @param dest 168 * @param opcode 169 * @return 0 on success 170 */ 171 uint8_t mesh_upper_transport_setup_segmented_control_pdu_header(mesh_upper_transport_pdu_t * upper_pdu, uint16_t netkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode); 172 173 /** 174 * @brief Send Control PDU 175 * @param pdu 176 */ 177 void mesh_upper_transport_send_control_pdu(mesh_pdu_t * pdu); 178 179 // Access PDUs: setup and send 180 /** 181 * @brief Setup header of Access PDU created with mesh_upper_transport_message_finalize 182 * @param pdu 183 * @param netkey_index 184 * @param appkey_index 185 * @param ttl 186 * @param src 187 * @param dest 188 * @param szmic 189 * @return 190 */ 191 uint8_t mesh_upper_transport_setup_access_pdu_header(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index, 192 uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic); 193 194 /** 195 * @brief Send Access PDU 196 * @param pdu 197 */ 198 void mesh_upper_transport_send_access_pdu(mesh_pdu_t * pdu); 199 200 201 // test 202 void mesh_upper_transport_dump(void); 203 void mesh_upper_transport_reset(void); 204 205 #ifdef __cplusplus 206 } /* end of extern "C" */ 207 #endif 208 209 #endif 210