xref: /btstack/src/mesh/mesh_configuration_client.c (revision ccc4a51891a306aafe4c54fe343bde46ba1f3096)
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_publication_get = {
191         MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_GET, "2m"
192 };
193 static const mesh_access_message_t mesh_configuration_client_publication_set = {
194         MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_SET, "222111m"
195 };
196 static const mesh_access_message_t mesh_configuration_client_publication_virtual_address_set = {
197         MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_VIRTUAL_ADDRESS_SET, "2P2111m"
198 };
199 
200 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){
201     uint8_t  ttl  = mesh_foundation_default_ttl_get();
202     mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0);
203     mesh_access_send_acknowledged_pdu(pdu, mesh_access_acknowledged_message_retransmissions(), ack_opcode);
204 }
205 
206 static uint8_t mesh_access_validate_envelop_params(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){
207     btstack_assert(mesh_model != NULL);
208     // TODO: validate other params
209     UNUSED(dest);
210     UNUSED(netkey_index);
211     UNUSED(appkey_index);
212 
213     return ERROR_CODE_SUCCESS;
214 }
215 
216 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){
217     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
218     if (status != ERROR_CODE_SUCCESS) return status;
219 
220     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_beacon_get);
221     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
222 
223     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);
224     return ERROR_CODE_SUCCESS;
225 }
226 
227 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){
228     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
229     if (status != ERROR_CODE_SUCCESS) return status;
230 
231     if (beacon > 1) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
232 
233     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_beacon_set, beacon);
234     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
235 
236     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);
237     return ERROR_CODE_SUCCESS;
238 }
239 
240 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){
241     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
242     if (status != ERROR_CODE_SUCCESS) return status;
243 
244     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_composition_data_get, page);
245     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
246 
247     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);
248     return ERROR_CODE_SUCCESS;
249 }
250 
251 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){
252     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
253     if (status != ERROR_CODE_SUCCESS) return status;
254 
255     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_default_ttl_get);
256     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
257 
258     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_GET);
259     return ERROR_CODE_SUCCESS;
260 }
261 
262 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){
263     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
264     if (status != ERROR_CODE_SUCCESS) return status;
265 
266     if (ttl == 0x01 || ttl >= 0x80) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
267 
268     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_default_ttl_set, ttl);
269     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
270 
271     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_SET);
272     return ERROR_CODE_SUCCESS;
273 }
274 
275 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){
276     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
277     if (status != ERROR_CODE_SUCCESS) return status;
278 
279     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_gatt_proxy_get);
280     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
281 
282     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_GET);
283     return ERROR_CODE_SUCCESS;
284 }
285 
286 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){
287     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
288     if (status != ERROR_CODE_SUCCESS) return status;
289 
290     if (gatt_proxy_state > 2) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
291 
292     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_gatt_proxy_set, gatt_proxy_state);
293     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
294 
295     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_SET);
296     return ERROR_CODE_SUCCESS;
297 }
298 
299 uint8_t mesh_configuration_client_send_relay_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){
300     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
301     if (status != ERROR_CODE_SUCCESS) return status;
302 
303     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_relay_get);
304     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
305 
306     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_GET);
307     return ERROR_CODE_SUCCESS;
308 }
309 
310 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){
311     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
312     if (status != ERROR_CODE_SUCCESS) return status;
313 
314     if (relay_retransmit_count > 0x07) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
315     if (relay_retransmit_interval_steps > 0x1F) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
316 
317     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);
318     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
319 
320     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);
321     return ERROR_CODE_SUCCESS;
322 }
323 
324 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){
325     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
326     if (status != ERROR_CODE_SUCCESS) return status;
327 
328     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_publication_get, dest, model_id);
329     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
330 
331     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_GET);
332     return ERROR_CODE_SUCCESS;
333 }
334 
335 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){
336     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
337     if (status != ERROR_CODE_SUCCESS) return status;
338     if (mesh_network_address_virtual(publication_config->publish_address_unicast)) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
339 
340     if (!mesh_network_address_unicast(dest)) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
341     if (publication_config->appkey_index > 0xFFF) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
342     if (publication_config->credential_flag > 1) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
343     if (publication_config->publish_retransmit_count > 0x07) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
344     if (publication_config->publish_retransmit_interval_steps > 0x1F) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
345 
346     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_publication_set,
347         dest,
348         publication_config->publish_address_unicast,
349         (publication_config->credential_flag << 12) | publication_config->appkey_index,
350         publication_config->publish_ttl,
351         publication_config->publish_period,
352         (publication_config->publish_retransmit_interval_steps << 3) | publication_config->publish_retransmit_count,
353         model_id);
354     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
355 
356     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);
357     return ERROR_CODE_SUCCESS;
358 
359 }
360 
361 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){
362     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
363     if (status != ERROR_CODE_SUCCESS) return status;
364 
365     if (!mesh_network_address_unicast(dest)) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
366     if (publication_config->appkey_index > 0xFFF) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
367     if (publication_config->credential_flag > 1) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
368     if (publication_config->publish_retransmit_count > 0x07) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
369     if (publication_config->publish_retransmit_interval_steps > 0x1F) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
370 
371     mesh_transport_pdu_t * network_pdu = mesh_access_setup_segmented_message(&mesh_configuration_client_publication_virtual_address_set,
372         dest,
373         publication_config->publish_address_virtual,
374         (publication_config->credential_flag << 12) | publication_config->appkey_index,
375         publication_config->publish_ttl,
376         publication_config->publish_period,
377         (publication_config->publish_retransmit_interval_steps << 3) | publication_config->publish_retransmit_count,
378         model_id);
379     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
380 
381     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);
382     return ERROR_CODE_SUCCESS;
383 }
384 
385 
386 
387 // Model Operations
388 static void mesh_configuration_client_composition_data_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
389     // Composition Data has variable of element descriptions, with two lists of model lists
390     // Pass raw data to application but provide convenient setters instead of parsing pdu here
391 
392     // reuse part of the mesh_network_t / mesh_transport_t struct to create event without memcpy or allocation
393     uint8_t * data = mesh_pdu_data(pdu);
394     uint8_t * event = &data[-6];
395 
396     int pos = 0;
397     event[pos++] = HCI_EVENT_MESH_META;
398     // Composite Data might be larger than 251 bytes - in this case only lower 8 bit are stored here. packet size is correct
399     event[pos++] = (uint8_t) (6 + mesh_pdu_len(pdu));
400     event[pos++] = MESH_SUBEVENT_CONFIGURATION_COMPOSITION_DATA;
401     // dest
402     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
403     pos += 2;
404     event[pos++] = ERROR_CODE_SUCCESS;
405 
406     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
407     mesh_access_message_processed(pdu);
408 }
409 
410 uint8_t mesh_subevent_configuration_composition_data_get_page(const uint8_t * event){
411     return event[6];
412 }
413 
414 uint16_t mesh_subevent_configuration_composition_data_get_cid(const uint8_t * event){
415     return little_endian_read_16(event, 7);
416 }
417 
418 uint16_t mesh_subevent_configuration_composition_data_get_pid(const uint8_t * event){
419     return little_endian_read_16(event, 9);
420 }
421 
422 uint16_t mesh_subevent_configuration_composition_data_get_vid(const uint8_t * event){
423     return little_endian_read_16(event, 11);
424 }
425 
426 uint16_t mesh_subevent_configuration_composition_data_get_crpl(const uint8_t * event){
427     return little_endian_read_16(event, 13);
428 }
429 
430 uint16_t mesh_subevent_configuration_composition_data_get_features(const uint8_t * event){
431     return little_endian_read_16(event, 15);
432 }
433 
434 
435 static inline void mesh_configuration_client_handle_uint8_value(mesh_model_t *mesh_model, mesh_pdu_t * pdu, uint8_t subevent_type){
436     mesh_access_parser_state_t parser;
437     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
438 
439     uint8_t value = mesh_access_parser_get_u8(&parser);
440 
441     uint8_t event[7];
442     int pos = 0;
443 
444     event[pos++] = HCI_EVENT_MESH_META;
445     event[pos++] = sizeof(event) - 2;
446     event[pos++] = subevent_type;
447     // dest
448     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
449     pos += 2;
450     event[pos++] = ERROR_CODE_SUCCESS;
451     event[pos++] = value;
452 
453     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
454     mesh_access_message_processed(pdu);
455 }
456 
457 static void mesh_configuration_client_beacon_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
458     mesh_configuration_client_handle_uint8_value(mesh_model, pdu, MESH_SUBEVENT_CONFIGURATION_BEACON);
459 }
460 
461 static void mesh_configuration_client_default_ttl_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
462     mesh_configuration_client_handle_uint8_value(mesh_model, pdu, MESH_SUBEVENT_CONFIGURATION_DEFAULT_TTL);
463 }
464 
465 static void mesh_configuration_client_gatt_proxy_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
466     mesh_configuration_client_handle_uint8_value(mesh_model, pdu, MESH_SUBEVENT_CONFIGURATION_GATT_PROXY);
467 }
468 
469 static void mesh_configuration_client_relay_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
470     mesh_access_parser_state_t parser;
471     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
472 
473     uint8_t relay = mesh_access_parser_get_u8(&parser);
474     uint8_t retransmition = mesh_access_parser_get_u8(&parser);
475 
476     uint8_t event[9];
477 
478     int pos = 0;
479     event[pos++] = HCI_EVENT_MESH_META;
480     event[pos++] = sizeof(event) - 2;
481     event[pos++] = MESH_SUBEVENT_CONFIGURATION_RELAY;
482     // dest
483     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
484     pos += 2;
485     event[pos++] = ERROR_CODE_SUCCESS;
486     event[pos++] = relay;
487     event[pos++] = (retransmition >> 5) + 1;
488     event[pos++] = ((retransmition & 0x07) + 1) * 10;
489 
490     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
491     mesh_access_message_processed(pdu);
492 }
493 
494 static void mesh_configuration_client_model_publication_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
495     mesh_access_parser_state_t parser;
496     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
497     uint8_t  status = mesh_access_parser_get_u8(&parser);
498     uint16_t publish_addres = mesh_access_parser_get_u16(&parser);
499 
500     uint16_t value = mesh_access_parser_get_u16(&parser);
501     uint16_t appkey_index = value & 0xFFF;
502     uint8_t  credential_flag = (value & 0x1000) >> 12;
503 
504     uint8_t publish_ttl = mesh_access_parser_get_u8(&parser);
505     uint8_t publish_period = mesh_access_parser_get_u8(&parser);
506 
507     uint8_t retransmit = mesh_access_parser_get_u8(&parser);
508     uint8_t publish_retransmit_count = retransmit & 0x111;
509     uint8_t publish_retransmit_interval_steps = retransmit >> 5;
510     uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser);
511 
512     uint8_t event[19];
513     int pos = 0;
514     event[pos++] = HCI_EVENT_MESH_META;
515     event[pos++] = sizeof(event) - 2;
516     event[pos++] = MESH_SUBEVENT_CONFIGURATION_MODEL_PUBLICATION;
517     // dest
518     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
519     pos += 2;
520     event[pos++] = status;
521 
522     little_endian_store_16(event, pos, publish_addres);
523     pos += 2;
524 
525     little_endian_store_16(event, pos, appkey_index);
526     pos += 2;
527 
528     event[pos++] = credential_flag;
529     event[pos++] = publish_ttl;
530     event[pos++] = publish_period;
531     event[pos++] = publish_retransmit_count;
532     event[pos++] = publish_retransmit_interval_steps;
533 
534     little_endian_store_32(event, pos, model_identifier);
535     pos += 4;
536 
537     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
538     mesh_access_message_processed(pdu);
539 }
540 
541 const static mesh_operation_t mesh_configuration_client_model_operations[] = {
542     { MESH_FOUNDATION_OPERATION_BEACON_STATUS,             1, mesh_configuration_client_beacon_status_handler },
543     { MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_STATUS,  10, mesh_configuration_client_composition_data_status_handler },
544     { MESH_FOUNDATION_OPERATION_DEFAULT_TTL_STATUS,        1, mesh_configuration_client_default_ttl_handler },
545     { MESH_FOUNDATION_OPERATION_GATT_PROXY_STATUS,         1, mesh_configuration_client_gatt_proxy_handler },
546     { MESH_FOUNDATION_OPERATION_RELAY_STATUS,              2, mesh_configuration_client_relay_handler },
547     { MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_STATUS, 12, mesh_configuration_client_model_publication_handler },
548 
549     { 0, 0, NULL }
550 };
551 
552 const mesh_operation_t * mesh_configuration_client_get_operations(void){
553     return mesh_configuration_client_model_operations;
554 }
555 
556 void mesh_configuration_client_register_packet_handler(mesh_model_t *configuration_client_model, btstack_packet_handler_t events_packet_handler){
557     btstack_assert(events_packet_handler != NULL);
558     btstack_assert(configuration_client_model != NULL);
559 
560     configuration_client_model->model_packet_handler = events_packet_handler;
561 }
562 
563