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 static void mesh_configuration_client_send_message_acknowledged(uint16_t src, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, mesh_pdu_t *pdu, uint32_t ack_opcode){ 59 uint8_t ttl = mesh_foundation_default_ttl_get(); 60 mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0); 61 mesh_access_send_acknowledged_pdu(pdu, mesh_access_acknowledged_message_retransmissions(), ack_opcode); 62 } 63 64 static uint8_t mesh_access_validate_envelop_params(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){ 65 btstack_assert(mesh_model != NULL); 66 // TODO: validate other params 67 UNUSED(dest); 68 UNUSED(netkey_index); 69 UNUSED(appkey_index); 70 71 return ERROR_CODE_SUCCESS; 72 } 73 74 // Configuration client messages 75 76 static const mesh_access_message_t mesh_configuration_client_beacon_get = { 77 MESH_FOUNDATION_OPERATION_BEACON_GET, "" 78 }; 79 80 #if 0 81 static const mesh_access_message_t mesh_configuration_client_beacon_set = { 82 MESH_FOUNDATION_OPERATION_BEACON_SET, "1" 83 }; 84 85 86 static const mesh_access_message_t mesh_configuration_client_composition_data_get = { 87 MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_GET, "1" 88 }; 89 90 91 static const mesh_access_message_t mesh_configuration_client_default_ttl_get = { 92 MESH_FOUNDATION_OPERATION_DEFAULT_TTL_GET, "" 93 }; 94 static const mesh_access_message_t mesh_configuration_client_default_ttl_set = { 95 MESH_FOUNDATION_OPERATION_DEFAULT_TTL_SET, "1" 96 }; 97 98 static const mesh_access_message_t mesh_configuration_client_gatt_proxy_get = { 99 MESH_FOUNDATION_OPERATION_GATT_PROXY_GET, "" 100 }; 101 static const mesh_access_message_t mesh_configuration_client_gatt_proxy_set = { 102 MESH_FOUNDATION_OPERATION_GATT_PROXY_SET, "1" 103 }; 104 105 106 static const mesh_access_message_t mesh_configuration_client_relay_get = { 107 MESH_FOUNDATION_OPERATION_RELAY_GET, "" 108 }; 109 static const mesh_access_message_t mesh_configuration_client_relay_set = { 110 MESH_FOUNDATION_OPERATION_RELAY_SET, "11" 111 }; 112 113 114 static const mesh_access_message_t mesh_configuration_client_publication_get = { 115 MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_GET, "2m" 116 }; 117 static const mesh_access_message_t mesh_configuration_client_publication_set = { 118 MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_SET, "222111m" 119 }; 120 static const mesh_access_message_t mesh_configuration_client_publication_virtual_address_set = { 121 MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_VIRTUAL_ADDRESS_SET, "2P2111m" 122 }; 123 #endif 124 125 uint8_t mesh_configuration_send_message_client_config_beacon_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){ 126 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 127 if (status != ERROR_CODE_SUCCESS) return status; 128 129 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_beacon_get); 130 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 131 132 mesh_configuration_client_send_message_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_BEACON_STATUS); 133 return ERROR_CODE_SUCCESS;; 134 } 135 136 void mesh_configuration_client_register_packet_handler(mesh_model_t *configuration_client_model, btstack_packet_handler_t events_packet_handler){ 137 btstack_assert(events_packet_handler != NULL); 138 btstack_assert(configuration_client_model != NULL); 139 140 configuration_client_model->model_packet_handler = events_packet_handler; 141 } 142 143