1 /* 2 * Copyright (C) 2019 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 #define BTSTACK_FILE__ "mesh_configuration_client.c" 39 40 #include <string.h> 41 #include <stdio.h> 42 43 #include "mesh/mesh_configuration_client.h" 44 45 #include "bluetooth_company_id.h" 46 #include "btstack_debug.h" 47 #include "btstack_memory.h" 48 #include "btstack_util.h" 49 50 #include "mesh/mesh_access.h" 51 #include "mesh/mesh_foundation.h" 52 #include "mesh/mesh_generic_model.h" 53 #include "mesh/mesh_keys.h" 54 #include "mesh/mesh_network.h" 55 #include "mesh/mesh_upper_transport.h" 56 57 58 // Mesh Composition Data Element iterator 59 #define MESH_VENDOR_MODEL_SIZE 4 60 #define MESH_SIG_MODEL_SIZE 2 61 #define MESH_COMPOSITION_DATA_ELEMENT_DESCRIPTION_OFFSET 17 62 #define MESH_COMPOSITION_DATA_ELEMENT_LOCATION_DESCRIPTOR_LEN 2 63 #define MESH_COMPOSITION_DATA_ELEMENT_MODEL_SIZE_LEN 1 64 65 static inline uint16_t mesh_composition_data_iterator_sig_model_list_size(mesh_composite_data_iterator_t * it){ 66 return it->elements[it->offset + 2] * MESH_SIG_MODEL_SIZE; 67 } 68 69 static inline uint16_t mesh_composition_data_iterator_vendor_model_list_size(mesh_composite_data_iterator_t * it){ 70 return it->elements[it->offset + 3] * MESH_VENDOR_MODEL_SIZE; 71 } 72 73 static inline uint16_t mesh_composition_data_iterator_element_len(mesh_composite_data_iterator_t * it){ 74 uint16_t sig_model_list_size = mesh_composition_data_iterator_sig_model_list_size(it); 75 uint16_t vendor_model_list_size = mesh_composition_data_iterator_vendor_model_list_size(it); 76 uint16_t previous_fields_len = MESH_COMPOSITION_DATA_ELEMENT_LOCATION_DESCRIPTOR_LEN + 2 * MESH_COMPOSITION_DATA_ELEMENT_MODEL_SIZE_LEN; 77 78 return previous_fields_len + sig_model_list_size + vendor_model_list_size;; 79 } 80 81 uint16_t mesh_subevent_configuration_composition_data_get_num_elements(const uint8_t * event, uint16_t size){ 82 uint16_t pos = MESH_COMPOSITION_DATA_ELEMENT_DESCRIPTION_OFFSET; 83 uint16_t num_elements = 0; 84 85 while ((pos + 4) <= size){ 86 // location descriptor 87 pos += 2; 88 uint8_t num_sig_model_ids = event[pos++]; 89 uint8_t num_vendor_model_ids = event[pos++]; 90 pos += (num_sig_model_ids + num_vendor_model_ids) * 2; 91 num_elements++; 92 } 93 return num_elements; 94 } 95 96 void mesh_composition_data_iterator_init(mesh_composite_data_iterator_t * it, const uint8_t * elements, uint16_t size){ 97 it->elements = elements; 98 it->size = size; 99 it->offset = MESH_COMPOSITION_DATA_ELEMENT_DESCRIPTION_OFFSET; 100 } 101 102 bool mesh_composition_data_iterator_has_next_element(mesh_composite_data_iterator_t * it){ 103 return (it->offset + mesh_composition_data_iterator_element_len(it)) <= it->size; 104 } 105 106 void mesh_composition_data_iterator_next_element(mesh_composite_data_iterator_t * it){ 107 it->sig_model_iterator.models = &it->elements[it->offset + 4]; 108 it->sig_model_iterator.size = mesh_composition_data_iterator_sig_model_list_size(it); 109 it->sig_model_iterator.offset = 0; 110 111 it->vendor_model_iterator.models = &it->elements[it->offset + 4 + it->sig_model_iterator.size]; 112 it->vendor_model_iterator.size = mesh_composition_data_iterator_vendor_model_list_size(it); 113 it->vendor_model_iterator.offset = 0; 114 115 it->loc = little_endian_read_16(it->elements, it->offset); 116 it->offset += mesh_composition_data_iterator_element_len(it); 117 } 118 119 uint16_t mesh_composition_data_iterator_element_loc(mesh_composite_data_iterator_t * it){ 120 return it->loc; 121 } 122 123 bool mesh_composition_data_iterator_has_next_sig_model(mesh_composite_data_iterator_t * it){ 124 return (it->sig_model_iterator.offset + MESH_SIG_MODEL_SIZE) <= it->sig_model_iterator.size; 125 } 126 127 void mesh_composition_data_iterator_next_sig_model(mesh_composite_data_iterator_t * it){ 128 it->sig_model_iterator.id = little_endian_read_16(it->sig_model_iterator.models, it->sig_model_iterator.offset); 129 it->sig_model_iterator.offset += 2; 130 } 131 132 uint16_t mesh_composition_data_iterator_sig_model_id(mesh_composite_data_iterator_t * it){ 133 return (uint16_t)it->sig_model_iterator.id; 134 } 135 136 bool mesh_composition_data_iterator_has_next_vendor_model(mesh_composite_data_iterator_t * it){ 137 return (it->vendor_model_iterator.offset + MESH_VENDOR_MODEL_SIZE) <= it->vendor_model_iterator.size; 138 } 139 140 void mesh_composition_data_iterator_next_vendor_model(mesh_composite_data_iterator_t * it){ 141 uint16_t vendor_id = little_endian_read_16(it->vendor_model_iterator.models, it->vendor_model_iterator.offset); 142 it->vendor_model_iterator.offset += 2; 143 uint16_t model_id = little_endian_read_16(it->vendor_model_iterator.models, it->vendor_model_iterator.offset); 144 it->vendor_model_iterator.offset += 2; 145 it->vendor_model_iterator.id = mesh_model_get_model_identifier(vendor_id, model_id); 146 } 147 148 uint32_t mesh_composition_data_iterator_vendor_model_id(mesh_composite_data_iterator_t * it){ 149 return it->vendor_model_iterator.id; 150 } 151 152 // Configuration client messages 153 154 static const mesh_access_message_t mesh_configuration_client_beacon_get = { 155 MESH_FOUNDATION_OPERATION_BEACON_GET, "" 156 }; 157 static const mesh_access_message_t mesh_configuration_client_beacon_set = { 158 MESH_FOUNDATION_OPERATION_BEACON_SET, "1" 159 }; 160 161 162 static const mesh_access_message_t mesh_configuration_client_composition_data_get = { 163 MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_GET, "1" 164 }; 165 166 167 static const mesh_access_message_t mesh_configuration_client_default_ttl_get = { 168 MESH_FOUNDATION_OPERATION_DEFAULT_TTL_GET, "" 169 }; 170 static const mesh_access_message_t mesh_configuration_client_default_ttl_set = { 171 MESH_FOUNDATION_OPERATION_DEFAULT_TTL_SET, "1" 172 }; 173 174 175 static const mesh_access_message_t mesh_configuration_client_gatt_proxy_get = { 176 MESH_FOUNDATION_OPERATION_GATT_PROXY_GET, "" 177 }; 178 static const mesh_access_message_t mesh_configuration_client_gatt_proxy_set = { 179 MESH_FOUNDATION_OPERATION_GATT_PROXY_SET, "1" 180 }; 181 182 183 static const mesh_access_message_t mesh_configuration_client_relay_get = { 184 MESH_FOUNDATION_OPERATION_RELAY_GET, "" 185 }; 186 static const mesh_access_message_t mesh_configuration_client_relay_set = { 187 MESH_FOUNDATION_OPERATION_RELAY_SET, "11" 188 }; 189 190 static const mesh_access_message_t mesh_configuration_client_model_publication_get = { 191 MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_GET, "2m" 192 }; 193 static const mesh_access_message_t mesh_configuration_client_model_publication_set = { 194 MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_SET, "222111m" 195 }; 196 static const mesh_access_message_t mesh_configuration_client_model_publication_virtual_address_set = { 197 MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_VIRTUAL_ADDRESS_SET, "2P2111m" 198 }; 199 200 201 static const mesh_access_message_t mesh_configuration_client_model_subscription_add = { 202 MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_ADD, "22m" 203 }; 204 static const mesh_access_message_t mesh_configuration_client_model_subscription_virtual_address_add = { 205 MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_VIRTUAL_ADDRESS_ADD, "2Pm" 206 }; 207 static const mesh_access_message_t mesh_configuration_client_model_subscription_delete = { 208 MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_DELETE, "22m" 209 }; 210 static const mesh_access_message_t mesh_configuration_client_model_subscription_virtual_address_delete = { 211 MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_VIRTUAL_ADDRESS_DELETE, "2Pm" 212 }; 213 static const mesh_access_message_t mesh_configuration_client_model_subscription_overwrite = { 214 MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_OVERWRITE, "22m" 215 }; 216 static const mesh_access_message_t mesh_configuration_client_model_subscription_virtual_address_overwrite = { 217 MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_VIRTUAL_ADDRESS_OVERWRITE, "2Pm" 218 }; 219 static const mesh_access_message_t mesh_configuration_client_model_subscription_delete_all = { 220 MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_DELETE_ALL, "22m" 221 }; 222 223 224 static const mesh_access_message_t mesh_configuration_client_sig_model_subscription_get = { 225 MESH_FOUNDATION_OPERATION_SIG_MODEL_SUBSCRIPTION_GET, "22" 226 }; 227 228 static const mesh_access_message_t mesh_configuration_client_vendor_model_subscription_get = { 229 MESH_FOUNDATION_OPERATION_VENDOR_MODEL_SUBSCRIPTION_GET, "24" 230 }; 231 232 static const mesh_access_message_t mesh_configuration_client_netkey_add = { 233 MESH_FOUNDATION_OPERATION_NETKEY_ADD, "2P" 234 }; 235 static const mesh_access_message_t mesh_configuration_client_netkey_update = { 236 MESH_FOUNDATION_OPERATION_NETKEY_UPDATE, "2P" 237 }; 238 static const mesh_access_message_t mesh_configuration_client_netkey_delete = { 239 MESH_FOUNDATION_OPERATION_NETKEY_DELETE, "2" 240 }; 241 static const mesh_access_message_t mesh_configuration_client_netkey_get = { 242 MESH_FOUNDATION_OPERATION_NETKEY_GET, "" 243 }; 244 245 246 static const mesh_access_message_t mesh_configuration_client_appkey_add = { 247 MESH_FOUNDATION_OPERATION_APPKEY_ADD, "3P" 248 }; 249 static const mesh_access_message_t mesh_configuration_client_appkey_update = { 250 MESH_FOUNDATION_OPERATION_APPKEY_UPDATE, "3P" 251 }; 252 static const mesh_access_message_t mesh_configuration_client_appkey_delete = { 253 MESH_FOUNDATION_OPERATION_APPKEY_DELETE, "3" 254 }; 255 static const mesh_access_message_t mesh_configuration_client_appkey_get = { 256 MESH_FOUNDATION_OPERATION_APPKEY_GET, "2" 257 }; 258 259 static const mesh_access_message_t mesh_configuration_client_node_identity_get = { 260 MESH_FOUNDATION_OPERATION_NODE_IDENTITY_GET, "2" 261 }; 262 static const mesh_access_message_t mesh_configuration_client_node_identity_set = { 263 MESH_FOUNDATION_OPERATION_NODE_IDENTITY_SET, "21" 264 }; 265 266 static void mesh_configuration_client_send_acknowledged(uint16_t src, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, mesh_pdu_t *pdu, uint32_t ack_opcode){ 267 uint8_t ttl = mesh_foundation_default_ttl_get(); 268 mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0); 269 mesh_access_send_acknowledged_pdu(pdu, mesh_access_acknowledged_message_retransmissions(), ack_opcode); 270 } 271 272 static uint8_t mesh_access_validate_envelop_params(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){ 273 btstack_assert(mesh_model != NULL); 274 // TODO: validate other params 275 UNUSED(mesh_model); 276 UNUSED(dest); 277 UNUSED(netkey_index); 278 UNUSED(appkey_index); 279 280 return ERROR_CODE_SUCCESS; 281 } 282 283 uint8_t mesh_configuration_client_send_beacon_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){ 284 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 285 if (status != ERROR_CODE_SUCCESS) return status; 286 287 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_beacon_get); 288 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 289 290 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_BEACON_STATUS); 291 return ERROR_CODE_SUCCESS; 292 } 293 294 uint8_t mesh_configuration_client_send_beacon_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t beacon){ 295 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 296 if (status != ERROR_CODE_SUCCESS) return status; 297 298 if (beacon > 1) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 299 300 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_beacon_set, beacon); 301 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 302 303 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_BEACON_STATUS); 304 return ERROR_CODE_SUCCESS; 305 } 306 307 uint8_t mesh_configuration_client_send_composition_data_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t page){ 308 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 309 if (status != ERROR_CODE_SUCCESS) return status; 310 311 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_composition_data_get, page); 312 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 313 314 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_STATUS); 315 return ERROR_CODE_SUCCESS; 316 } 317 318 uint8_t mesh_configuration_client_send_default_ttl_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){ 319 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 320 if (status != ERROR_CODE_SUCCESS) return status; 321 322 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_default_ttl_get); 323 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 324 325 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_DEFAULT_TTL_STATUS); 326 return ERROR_CODE_SUCCESS; 327 } 328 329 uint8_t mesh_configuration_client_send_default_ttl_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t ttl){ 330 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 331 if (status != ERROR_CODE_SUCCESS) return status; 332 333 if (ttl == 0x01 || ttl >= 0x80) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 334 335 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_default_ttl_set, ttl); 336 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 337 338 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_DEFAULT_TTL_STATUS); 339 return ERROR_CODE_SUCCESS; 340 } 341 342 uint8_t mesh_configuration_client_send_gatt_proxy_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){ 343 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 344 if (status != ERROR_CODE_SUCCESS) return status; 345 346 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_gatt_proxy_get); 347 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 348 349 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_GATT_PROXY_STATUS); 350 return ERROR_CODE_SUCCESS; 351 } 352 353 uint8_t mesh_configuration_client_send_gatt_proxy_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t gatt_proxy_state){ 354 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 355 if (status != ERROR_CODE_SUCCESS) return status; 356 357 if (gatt_proxy_state > 2) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 358 359 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_gatt_proxy_set, gatt_proxy_state); 360 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 361 362 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_GATT_PROXY_STATUS); 363 return ERROR_CODE_SUCCESS; 364 } 365 366 uint8_t mesh_configuration_client_send_relay_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){ 367 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 368 if (status != ERROR_CODE_SUCCESS) return status; 369 370 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_relay_get); 371 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 372 373 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_RELAY_STATUS); 374 return ERROR_CODE_SUCCESS; 375 } 376 377 uint8_t mesh_configuration_client_send_relay_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t relay, uint8_t relay_retransmit_count, uint8_t relay_retransmit_interval_steps){ 378 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 379 if (status != ERROR_CODE_SUCCESS) return status; 380 381 if (relay_retransmit_count > 0x07) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 382 if (relay_retransmit_interval_steps > 0x1F) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 383 384 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_relay_set, relay, (relay_retransmit_count << 5) | relay_retransmit_interval_steps); 385 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 386 387 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_RELAY_SET); 388 return ERROR_CODE_SUCCESS; 389 } 390 391 uint8_t mesh_configuration_client_send_model_publication_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint32_t model_id){ 392 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 393 if (status != ERROR_CODE_SUCCESS) return status; 394 395 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_model_publication_get, dest, model_id); 396 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 397 398 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_STATUS); 399 return ERROR_CODE_SUCCESS; 400 } 401 402 static uint8_t mesh_validate_publication_model_config_parameters(mesh_publication_model_config_t * publication_config, bool use_unicast_address){ 403 if (publication_config->appkey_index > 0xFFF) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 404 if (publication_config->credential_flag > 1) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 405 if (publication_config->publish_retransmit_count > 0x07) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 406 if (publication_config->publish_retransmit_interval_steps > 0x1F) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 407 if (use_unicast_address && mesh_network_address_virtual(publication_config->publish_address_unicast)) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 408 return ERROR_CODE_SUCCESS; 409 } 410 411 uint8_t mesh_configuration_client_send_model_publication_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint32_t model_id, mesh_publication_model_config_t * publication_config){ 412 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 413 if (status != ERROR_CODE_SUCCESS) return status; 414 415 if (!mesh_network_address_unicast(dest) || 416 mesh_validate_publication_model_config_parameters(publication_config, true) != ERROR_CODE_SUCCESS){ 417 return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 418 } 419 420 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_model_publication_set, 421 dest, 422 publication_config->publish_address_unicast, 423 (publication_config->credential_flag << 12) | publication_config->appkey_index, 424 publication_config->publish_ttl, 425 publication_config->publish_period, 426 (publication_config->publish_retransmit_interval_steps << 3) | publication_config->publish_retransmit_count, 427 model_id); 428 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 429 430 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_STATUS); 431 return ERROR_CODE_SUCCESS; 432 433 } 434 435 uint8_t mesh_configuration_client_send_model_publication_virtual_address_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint32_t model_id, mesh_publication_model_config_t * publication_config){ 436 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 437 if (status != ERROR_CODE_SUCCESS) return status; 438 439 if (!mesh_network_address_unicast(dest) || 440 mesh_validate_publication_model_config_parameters(publication_config, false) != ERROR_CODE_SUCCESS){ 441 return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 442 } 443 444 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_model_publication_virtual_address_set, 445 dest, 446 publication_config->publish_address_virtual, 447 (publication_config->credential_flag << 12) | publication_config->appkey_index, 448 publication_config->publish_ttl, 449 publication_config->publish_period, 450 (publication_config->publish_retransmit_interval_steps << 3) | publication_config->publish_retransmit_count, 451 model_id); 452 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 453 454 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_STATUS); 455 return ERROR_CODE_SUCCESS; 456 } 457 458 459 uint8_t mesh_configuration_client_send_model_subscription_add(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint16_t address, uint32_t model_id){ 460 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 461 if (status != ERROR_CODE_SUCCESS) return status; 462 463 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_model_subscription_add, dest, address, model_id); 464 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 465 466 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_STATUS); 467 return ERROR_CODE_SUCCESS; 468 } 469 470 uint8_t mesh_configuration_client_send_model_subscription_virtual_address_add(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t * address, uint32_t model_id){ 471 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 472 if (status != ERROR_CODE_SUCCESS) return status; 473 474 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_model_subscription_virtual_address_add, dest, address, model_id); 475 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 476 477 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_STATUS); 478 return ERROR_CODE_SUCCESS; 479 } 480 481 uint8_t mesh_configuration_client_send_model_subscription_delete(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint16_t address, uint32_t model_id){ 482 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 483 if (status != ERROR_CODE_SUCCESS) return status; 484 485 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_model_subscription_delete, dest, address, model_id); 486 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 487 488 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_STATUS); 489 return ERROR_CODE_SUCCESS; 490 } 491 492 uint8_t mesh_configuration_client_send_model_subscription_virtual_address_delete(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t * address, uint32_t model_id){ 493 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 494 if (status != ERROR_CODE_SUCCESS) return status; 495 496 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_model_subscription_virtual_address_delete, dest, address, model_id); 497 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 498 499 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_STATUS); 500 return ERROR_CODE_SUCCESS; 501 } 502 503 uint8_t mesh_configuration_client_send_model_subscription_overwrite(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint16_t address, uint32_t model_id){ 504 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 505 if (status != ERROR_CODE_SUCCESS) return status; 506 507 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_model_subscription_overwrite, dest, address, model_id); 508 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 509 510 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_STATUS); 511 return ERROR_CODE_SUCCESS; 512 } 513 514 uint8_t mesh_configuration_client_send_model_subscription_virtual_address_overwrite(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t * address, uint32_t model_id){ 515 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 516 if (status != ERROR_CODE_SUCCESS) return status; 517 518 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_model_subscription_virtual_address_overwrite, dest, address, model_id); 519 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 520 521 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_STATUS); 522 return ERROR_CODE_SUCCESS; 523 } 524 525 uint8_t mesh_configuration_client_send_model_subscription_delete_all(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint16_t address, uint32_t model_id){ 526 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 527 if (status != ERROR_CODE_SUCCESS) return status; 528 529 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_model_subscription_delete_all, dest, address, model_id); 530 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 531 532 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_STATUS); 533 return ERROR_CODE_SUCCESS; 534 } 535 536 uint8_t mesh_configuration_client_send_model_subscription_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint32_t model_id){ 537 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 538 if (status != ERROR_CODE_SUCCESS) return status; 539 540 mesh_network_pdu_t * network_pdu = NULL; 541 uint32_t ack_opcode = MESH_FOUNDATION_OPERATION_SIG_MODEL_SUBSCRIPTION_LIST; 542 543 if (mesh_model_is_bluetooth_sig(model_id)){ 544 network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_sig_model_subscription_get, dest, model_id); 545 } else { 546 network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_vendor_model_subscription_get, dest, model_id); 547 ack_opcode = MESH_FOUNDATION_OPERATION_VENDOR_MODEL_SUBSCRIPTION_LIST; 548 } 549 550 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 551 552 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, ack_opcode); 553 return ERROR_CODE_SUCCESS; 554 } 555 556 uint8_t mesh_configuration_client_send_netkey_add(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint16_t index, uint8_t * netkey){ 557 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 558 if (status != ERROR_CODE_SUCCESS) return status; 559 560 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_netkey_add, index, netkey); 561 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 562 563 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_NETKEY_STATUS); 564 return ERROR_CODE_SUCCESS; 565 } 566 567 uint8_t mesh_configuration_client_send_netkey_update(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint16_t index, uint8_t * netkey){ 568 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 569 if (status != ERROR_CODE_SUCCESS) return status; 570 571 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_netkey_update, index, netkey); 572 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 573 574 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_NETKEY_STATUS); 575 return ERROR_CODE_SUCCESS; 576 } 577 578 uint8_t mesh_configuration_client_send_netkey_delete(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint16_t index){ 579 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 580 if (status != ERROR_CODE_SUCCESS) return status; 581 582 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_netkey_delete, index); 583 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 584 585 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_NETKEY_STATUS); 586 return ERROR_CODE_SUCCESS; 587 } 588 589 uint8_t mesh_configuration_client_send_netkey_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){ 590 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 591 if (status != ERROR_CODE_SUCCESS) return status; 592 593 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_netkey_get); 594 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 595 596 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_NETKEY_LIST); 597 return ERROR_CODE_SUCCESS; 598 } 599 600 uint8_t mesh_configuration_client_send_appkey_add(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint16_t netk_index, uint16_t appk_index, uint8_t * appkey){ 601 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 602 if (status != ERROR_CODE_SUCCESS) return status; 603 604 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_appkey_add, netk_index << 12 | appk_index, appkey); 605 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 606 607 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_APPKEY_STATUS); 608 return ERROR_CODE_SUCCESS; 609 } 610 611 uint8_t mesh_configuration_client_send_appkey_update(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint16_t netk_index, uint16_t appk_index, uint8_t * appkey){ 612 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 613 if (status != ERROR_CODE_SUCCESS) return status; 614 615 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_appkey_update, netk_index << 12 | appk_index, appkey); 616 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 617 618 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_APPKEY_STATUS); 619 return ERROR_CODE_SUCCESS; 620 } 621 622 uint8_t mesh_configuration_client_send_appkey_delete(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint16_t netk_index, uint16_t appk_index){ 623 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 624 if (status != ERROR_CODE_SUCCESS) return status; 625 626 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_appkey_delete, netk_index << 12 | appk_index); 627 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 628 629 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_APPKEY_STATUS); 630 return ERROR_CODE_SUCCESS; 631 } 632 633 uint8_t mesh_configuration_client_send_appkey_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint16_t netk_index){ 634 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 635 if (status != ERROR_CODE_SUCCESS) return status; 636 637 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_appkey_get, netk_index); 638 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 639 640 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_APPKEY_LIST); 641 return ERROR_CODE_SUCCESS; 642 } 643 644 uint8_t mesh_configuration_client_send_node_identity_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint16_t netk_index){ 645 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 646 if (status != ERROR_CODE_SUCCESS) return status; 647 648 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_node_identity_get, netk_index); 649 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 650 651 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_NODE_IDENTITY_STATUS); 652 return ERROR_CODE_SUCCESS; 653 } 654 655 uint8_t mesh_configuration_client_send_node_identity_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint16_t netk_index, mesh_node_identity_state_t node_identity_state){ 656 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 657 if (status != ERROR_CODE_SUCCESS) return status; 658 659 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_node_identity_set, netk_index, node_identity_state); 660 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 661 662 mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_NODE_IDENTITY_STATUS); 663 return ERROR_CODE_SUCCESS; 664 } 665 666 667 668 // Model Operations 669 static void mesh_configuration_client_composition_data_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 670 // Composition Data has variable of element descriptions, with two lists of model lists 671 // Pass raw data to application but provide convenient setters instead of parsing pdu here 672 673 // reuse part of the mesh_network_t / mesh_transport_t struct to create event without memcpy or allocation 674 uint8_t * data = mesh_pdu_data(pdu); 675 uint8_t * event = &data[-6]; 676 677 int pos = 0; 678 event[pos++] = HCI_EVENT_MESH_META; 679 // Composite Data might be larger than 251 bytes - in this case only lower 8 bit are stored here. packet size is correct 680 event[pos++] = (uint8_t) (6 + mesh_pdu_len(pdu)); 681 event[pos++] = MESH_SUBEVENT_CONFIGURATION_COMPOSITION_DATA; 682 // dest 683 little_endian_store_16(event, pos, mesh_pdu_src(pdu)); 684 pos += 2; 685 event[pos++] = ERROR_CODE_SUCCESS; 686 687 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 688 mesh_access_message_processed(pdu); 689 } 690 691 uint8_t mesh_subevent_configuration_composition_data_get_page(const uint8_t * event){ 692 return event[6]; 693 } 694 695 uint16_t mesh_subevent_configuration_composition_data_get_cid(const uint8_t * event){ 696 return little_endian_read_16(event, 7); 697 } 698 699 uint16_t mesh_subevent_configuration_composition_data_get_pid(const uint8_t * event){ 700 return little_endian_read_16(event, 9); 701 } 702 703 uint16_t mesh_subevent_configuration_composition_data_get_vid(const uint8_t * event){ 704 return little_endian_read_16(event, 11); 705 } 706 707 uint16_t mesh_subevent_configuration_composition_data_get_crpl(const uint8_t * event){ 708 return little_endian_read_16(event, 13); 709 } 710 711 uint16_t mesh_subevent_configuration_composition_data_get_features(const uint8_t * event){ 712 return little_endian_read_16(event, 15); 713 } 714 715 716 static inline void mesh_configuration_client_handle_uint8_value(mesh_model_t *mesh_model, mesh_pdu_t * pdu, uint8_t subevent_type){ 717 mesh_access_parser_state_t parser; 718 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 719 720 uint8_t value = mesh_access_parser_get_u8(&parser); 721 722 uint8_t event[7]; 723 int pos = 0; 724 725 event[pos++] = HCI_EVENT_MESH_META; 726 event[pos++] = sizeof(event) - 2; 727 event[pos++] = subevent_type; 728 // dest 729 little_endian_store_16(event, pos, mesh_pdu_src(pdu)); 730 pos += 2; 731 event[pos++] = ERROR_CODE_SUCCESS; 732 event[pos++] = value; 733 734 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 735 mesh_access_message_processed(pdu); 736 } 737 738 static void mesh_configuration_client_beacon_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 739 mesh_configuration_client_handle_uint8_value(mesh_model, pdu, MESH_SUBEVENT_CONFIGURATION_BEACON); 740 } 741 742 static void mesh_configuration_client_default_ttl_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 743 mesh_configuration_client_handle_uint8_value(mesh_model, pdu, MESH_SUBEVENT_CONFIGURATION_DEFAULT_TTL); 744 } 745 746 static void mesh_configuration_client_gatt_proxy_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 747 mesh_configuration_client_handle_uint8_value(mesh_model, pdu, MESH_SUBEVENT_CONFIGURATION_GATT_PROXY); 748 } 749 750 static void mesh_configuration_client_relay_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 751 mesh_access_parser_state_t parser; 752 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 753 754 uint8_t relay = mesh_access_parser_get_u8(&parser); 755 uint8_t retransmition = mesh_access_parser_get_u8(&parser); 756 757 uint8_t event[9]; 758 759 int pos = 0; 760 event[pos++] = HCI_EVENT_MESH_META; 761 event[pos++] = sizeof(event) - 2; 762 event[pos++] = MESH_SUBEVENT_CONFIGURATION_RELAY; 763 // dest 764 little_endian_store_16(event, pos, mesh_pdu_src(pdu)); 765 pos += 2; 766 event[pos++] = ERROR_CODE_SUCCESS; 767 event[pos++] = relay; 768 event[pos++] = (retransmition >> 5) + 1; 769 event[pos++] = ((retransmition & 0x07) + 1) * 10; 770 771 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 772 mesh_access_message_processed(pdu); 773 } 774 775 static void mesh_configuration_client_model_publication_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 776 mesh_access_parser_state_t parser; 777 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 778 uint8_t status = mesh_access_parser_get_u8(&parser); 779 uint16_t publish_addres = mesh_access_parser_get_u16(&parser); 780 781 uint16_t value = mesh_access_parser_get_u16(&parser); 782 uint16_t appkey_index = value & 0xFFF; 783 uint8_t credential_flag = (value & 0x1000) >> 12; 784 785 uint8_t publish_ttl = mesh_access_parser_get_u8(&parser); 786 uint8_t publish_period = mesh_access_parser_get_u8(&parser); 787 788 uint8_t retransmit = mesh_access_parser_get_u8(&parser); 789 uint8_t publish_retransmit_count = retransmit & 0x111; 790 uint8_t publish_retransmit_interval_steps = retransmit >> 5; 791 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 792 793 uint8_t event[19]; 794 int pos = 0; 795 event[pos++] = HCI_EVENT_MESH_META; 796 event[pos++] = sizeof(event) - 2; 797 event[pos++] = MESH_SUBEVENT_CONFIGURATION_MODEL_PUBLICATION; 798 // dest 799 little_endian_store_16(event, pos, mesh_pdu_src(pdu)); 800 pos += 2; 801 event[pos++] = status; 802 803 little_endian_store_16(event, pos, publish_addres); 804 pos += 2; 805 806 little_endian_store_16(event, pos, appkey_index); 807 pos += 2; 808 809 event[pos++] = credential_flag; 810 event[pos++] = publish_ttl; 811 event[pos++] = publish_period; 812 event[pos++] = publish_retransmit_count; 813 event[pos++] = publish_retransmit_interval_steps; 814 815 little_endian_store_32(event, pos, model_identifier); 816 pos += 4; 817 818 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 819 mesh_access_message_processed(pdu); 820 } 821 822 static void mesh_configuration_client_model_subscription_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 823 mesh_access_parser_state_t parser; 824 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 825 uint8_t status = mesh_access_parser_get_u8(&parser); 826 uint16_t address = mesh_access_parser_get_u16(&parser); 827 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 828 829 uint8_t event[12]; 830 int pos = 0; 831 event[pos++] = HCI_EVENT_MESH_META; 832 event[pos++] = sizeof(event) - 2; 833 event[pos++] = MESH_SUBEVENT_CONFIGURATION_MODEL_SUBSCRIPTION; 834 // dest 835 little_endian_store_16(event, pos, mesh_pdu_src(pdu)); 836 pos += 2; 837 event[pos++] = status; 838 839 little_endian_store_16(event, pos, address); 840 pos += 2; 841 842 little_endian_store_32(event, pos, model_identifier); 843 pos += 4; 844 845 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 846 mesh_access_message_processed(pdu); 847 } 848 849 static void mesh_configuration_client_model_subscription_event(mesh_model_t *mesh_model, mesh_pdu_t * pdu, uint8_t subevent_type){ 850 mesh_access_parser_state_t parser; 851 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 852 uint8_t status = mesh_access_parser_get_u8(&parser); 853 uint16_t address = mesh_access_parser_get_u16(&parser); 854 uint32_t model_identifier; 855 856 if (subevent_type == MESH_SUBEVENT_CONFIGURATION_SIG_MODEL_SUBSCRIPTION_LIST_ITEM) { 857 model_identifier = mesh_access_parser_get_sig_model_identifier(&parser); 858 } else { 859 model_identifier = mesh_access_parser_get_vendor_model_identifier(&parser); 860 } 861 uint8_t list_size = mesh_access_parser_available(&parser)/2; 862 863 uint8_t event[12]; 864 int pos = 0; 865 event[pos++] = HCI_EVENT_MESH_META; 866 event[pos++] = sizeof(event) - 2; 867 event[pos++] = subevent_type; 868 // dest 869 little_endian_store_16(event, pos, mesh_pdu_src(pdu)); 870 pos += 2; 871 event[pos++] = status; 872 873 little_endian_store_16(event, pos, address); 874 pos += 2; 875 876 event[pos++] = list_size; 877 uint8_t i; 878 for (i = 0; i < list_size; i++){ 879 event[pos++] = i; 880 little_endian_store_16(event, pos, mesh_access_parser_get_u16(&parser)); 881 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos + 2); 882 } 883 mesh_access_message_processed(pdu); 884 } 885 886 static void mesh_configuration_client_sig_model_subscription_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 887 mesh_configuration_client_model_subscription_event(mesh_model, pdu, MESH_SUBEVENT_CONFIGURATION_SIG_MODEL_SUBSCRIPTION_LIST_ITEM); 888 } 889 890 static void mesh_configuration_client_vendor_model_subscription_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 891 mesh_configuration_client_model_subscription_event(mesh_model, pdu, MESH_SUBEVENT_CONFIGURATION_VENDOR_MODEL_SUBSCRIPTION_LIST_ITEM); 892 } 893 894 static void mesh_configuration_client_netkey_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 895 mesh_access_parser_state_t parser; 896 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 897 uint8_t status = mesh_access_parser_get_u8(&parser); 898 899 uint8_t event[6]; 900 int pos = 0; 901 event[pos++] = HCI_EVENT_MESH_META; 902 event[pos++] = sizeof(event) - 2; 903 event[pos++] = MESH_SUBEVENT_CONFIGURATION_NETKEY_INDEX; 904 // dest 905 little_endian_store_16(event, pos, mesh_pdu_src(pdu)); 906 pos += 2; 907 event[pos++] = status; 908 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 909 mesh_access_message_processed(pdu); 910 } 911 912 static void mesh_configuration_client_netkey_list_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 913 mesh_access_parser_state_t parser; 914 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 915 uint8_t status = 0; 916 uint8_t list_size = mesh_access_parser_available(&parser)/2; 917 918 uint8_t event[10]; 919 int pos = 0; 920 event[pos++] = HCI_EVENT_MESH_META; 921 event[pos++] = sizeof(event) - 2; 922 event[pos++] = MESH_SUBEVENT_CONFIGURATION_NETKEY_INDEX_LIST_ITEM; 923 // dest 924 little_endian_store_16(event, pos, mesh_pdu_src(pdu)); 925 pos += 2; 926 event[pos++] = status; 927 928 event[pos++] = list_size; 929 uint8_t i; 930 for (i = 0; i < list_size; i++){ 931 event[pos++] = i; 932 little_endian_store_16(event, pos, mesh_access_parser_get_u16(&parser)); 933 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos + 2); 934 } 935 mesh_access_message_processed(pdu); 936 } 937 938 static void mesh_configuration_client_appkey_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 939 mesh_access_parser_state_t parser; 940 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 941 uint8_t status = mesh_access_parser_get_u8(&parser); 942 uint32_t netappkey_index = mesh_access_parser_get_u24(&parser); 943 uint16_t netkey_index = netappkey_index >> 12; 944 uint16_t appkey_index = netappkey_index & 0xFFF; 945 946 uint8_t event[10]; 947 int pos = 0; 948 event[pos++] = HCI_EVENT_MESH_META; 949 event[pos++] = sizeof(event) - 2; 950 event[pos++] = MESH_SUBEVENT_CONFIGURATION_APPKEY_INDEX; 951 // dest 952 little_endian_store_16(event, pos, mesh_pdu_src(pdu)); 953 pos += 2; 954 event[pos++] = status; 955 little_endian_store_16(event, pos, netkey_index); 956 pos += 2; 957 little_endian_store_16(event, pos, appkey_index); 958 pos += 2; 959 960 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 961 mesh_access_message_processed(pdu); 962 } 963 964 static void mesh_configuration_client_appkey_list_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 965 mesh_access_parser_state_t parser; 966 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 967 uint8_t status = 0; 968 uint8_t list_size = mesh_access_parser_available(&parser)/2; 969 970 uint8_t event[12]; 971 int pos = 0; 972 event[pos++] = HCI_EVENT_MESH_META; 973 event[pos++] = sizeof(event) - 2; 974 event[pos++] = MESH_SUBEVENT_CONFIGURATION_APPKEY_INDEX_LIST_ITEM; 975 // dest 976 little_endian_store_16(event, pos, mesh_pdu_src(pdu)); 977 pos += 2; 978 event[pos++] = status; 979 980 event[pos++] = list_size; 981 uint8_t i; 982 for (i = 0; i < list_size; i++){ 983 event[pos++] = i; 984 uint32_t netappkey_index = mesh_access_parser_get_u24(&parser); 985 little_endian_store_16(event, pos, netappkey_index >> 12); 986 little_endian_store_16(event, pos + 2, netappkey_index & 0xFFF); 987 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos + 4); 988 } 989 mesh_access_message_processed(pdu); 990 } 991 992 static void mesh_configuration_client_node_identity_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 993 mesh_access_parser_state_t parser; 994 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 995 uint8_t status = mesh_access_parser_get_u8(&parser); 996 uint16_t netkey_index = mesh_access_parser_get_u8(&parser); 997 uint8_t identity_status = mesh_access_parser_get_u8(&parser); 998 999 uint8_t event[9]; 1000 int pos = 0; 1001 event[pos++] = HCI_EVENT_MESH_META; 1002 event[pos++] = sizeof(event) - 2; 1003 event[pos++] = MESH_SUBEVENT_CONFIGURATION_NODE_IDENTITY; 1004 // dest 1005 little_endian_store_16(event, pos, mesh_pdu_src(pdu)); 1006 pos += 2; 1007 event[pos++] = status; 1008 little_endian_store_16(event, pos, netkey_index); 1009 pos += 2; 1010 event[pos++] = identity_status; 1011 1012 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 1013 mesh_access_message_processed(pdu); 1014 } 1015 1016 const static mesh_operation_t mesh_configuration_client_model_operations[] = { 1017 { MESH_FOUNDATION_OPERATION_BEACON_STATUS, 1, mesh_configuration_client_beacon_status_handler }, 1018 { MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_STATUS, 10, mesh_configuration_client_composition_data_status_handler }, 1019 { MESH_FOUNDATION_OPERATION_DEFAULT_TTL_STATUS, 1, mesh_configuration_client_default_ttl_handler }, 1020 { MESH_FOUNDATION_OPERATION_GATT_PROXY_STATUS, 1, mesh_configuration_client_gatt_proxy_handler }, 1021 { MESH_FOUNDATION_OPERATION_RELAY_STATUS, 2, mesh_configuration_client_relay_handler }, 1022 { MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_STATUS, 12, mesh_configuration_client_model_publication_handler }, 1023 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_STATUS, 7, mesh_configuration_client_model_subscription_handler }, 1024 { MESH_FOUNDATION_OPERATION_SIG_MODEL_SUBSCRIPTION_LIST, 5, mesh_configuration_client_sig_model_subscription_handler}, 1025 { MESH_FOUNDATION_OPERATION_VENDOR_MODEL_SUBSCRIPTION_LIST, 7, mesh_configuration_client_vendor_model_subscription_handler}, 1026 { MESH_FOUNDATION_OPERATION_NETKEY_STATUS, 3, mesh_configuration_client_netkey_handler }, 1027 { MESH_FOUNDATION_OPERATION_NETKEY_LIST, 0, mesh_configuration_client_netkey_list_handler }, 1028 { MESH_FOUNDATION_OPERATION_APPKEY_STATUS, 4, mesh_configuration_client_appkey_handler }, 1029 { MESH_FOUNDATION_OPERATION_APPKEY_LIST, 3, mesh_configuration_client_appkey_list_handler }, 1030 { MESH_FOUNDATION_OPERATION_NODE_IDENTITY_STATUS, 4, mesh_configuration_client_node_identity_handler }, 1031 { 0, 0, NULL } 1032 }; 1033 1034 const mesh_operation_t * mesh_configuration_client_get_operations(void){ 1035 return mesh_configuration_client_model_operations; 1036 } 1037 1038 void mesh_configuration_client_register_packet_handler(mesh_model_t *configuration_client_model, btstack_packet_handler_t events_packet_handler){ 1039 btstack_assert(events_packet_handler != NULL); 1040 btstack_assert(configuration_client_model != NULL); 1041 1042 configuration_client_model->model_packet_handler = events_packet_handler; 1043 } 1044 1045