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_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 static const mesh_access_message_t mesh_configuration_client_beacon_set = { 80 MESH_FOUNDATION_OPERATION_BEACON_SET, "1" 81 }; 82 83 84 static const mesh_access_message_t mesh_configuration_client_composition_data_get = { 85 MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_GET, "1" 86 }; 87 88 89 #if 0 90 static const mesh_access_message_t mesh_configuration_client_default_ttl_get = { 91 MESH_FOUNDATION_OPERATION_DEFAULT_TTL_GET, "" 92 }; 93 static const mesh_access_message_t mesh_configuration_client_default_ttl_set = { 94 MESH_FOUNDATION_OPERATION_DEFAULT_TTL_SET, "1" 95 }; 96 97 static const mesh_access_message_t mesh_configuration_client_gatt_proxy_get = { 98 MESH_FOUNDATION_OPERATION_GATT_PROXY_GET, "" 99 }; 100 static const mesh_access_message_t mesh_configuration_client_gatt_proxy_set = { 101 MESH_FOUNDATION_OPERATION_GATT_PROXY_SET, "1" 102 }; 103 104 105 static const mesh_access_message_t mesh_configuration_client_relay_get = { 106 MESH_FOUNDATION_OPERATION_RELAY_GET, "" 107 }; 108 static const mesh_access_message_t mesh_configuration_client_relay_set = { 109 MESH_FOUNDATION_OPERATION_RELAY_SET, "11" 110 }; 111 112 113 static const mesh_access_message_t mesh_configuration_client_publication_get = { 114 MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_GET, "2m" 115 }; 116 static const mesh_access_message_t mesh_configuration_client_publication_set = { 117 MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_SET, "222111m" 118 }; 119 static const mesh_access_message_t mesh_configuration_client_publication_virtual_address_set = { 120 MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_VIRTUAL_ADDRESS_SET, "2P2111m" 121 }; 122 #endif 123 124 uint8_t mesh_configuration_client_send_config_beacon_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){ 125 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 126 if (status != ERROR_CODE_SUCCESS) return status; 127 128 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_beacon_get); 129 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 130 131 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); 132 return ERROR_CODE_SUCCESS; 133 } 134 135 uint8_t mesh_configuration_client_send_config_beacon_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t beacon){ 136 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 137 if (status != ERROR_CODE_SUCCESS) return status; 138 139 if (beacon > 1) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 140 141 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_beacon_set, beacon); 142 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 143 144 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); 145 return ERROR_CODE_SUCCESS; 146 } 147 148 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){ 149 uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index); 150 if (status != ERROR_CODE_SUCCESS) return status; 151 152 mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_composition_data_get, page); 153 if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 154 155 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_GET); 156 return ERROR_CODE_SUCCESS; 157 } 158 159 // Model Operations 160 static void mesh_configuration_client_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 161 mesh_access_parser_state_t parser; 162 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 163 164 uint8_t beacon_status = mesh_access_parser_get_u8(&parser); 165 166 uint8_t event[7] = {HCI_EVENT_MESH_META, 5, MESH_SUBEVENT_FOUNDATION_BEACON_STATUS}; 167 int pos = 3; 168 // dest 169 little_endian_store_16(event, pos, mesh_pdu_src(pdu)); 170 pos += 2; 171 event[pos++] = ERROR_CODE_SUCCESS; 172 event[pos++] = beacon_status; 173 174 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 175 mesh_access_message_processed(pdu); 176 } 177 178 const static mesh_operation_t mesh_configuration_client_model_operations[] = { 179 { MESH_FOUNDATION_OPERATION_BEACON_STATUS, 0, mesh_configuration_client_status_handler }, 180 { 0, 0, NULL } 181 }; 182 183 const mesh_operation_t * mesh_configuration_client_get_operations(void){ 184 return mesh_configuration_client_model_operations; 185 } 186 187 void mesh_configuration_client_register_packet_handler(mesh_model_t *configuration_client_model, btstack_packet_handler_t events_packet_handler){ 188 btstack_assert(events_packet_handler != NULL); 189 btstack_assert(configuration_client_model != NULL); 190 191 configuration_client_model->model_packet_handler = events_packet_handler; 192 } 193 194