xref: /btstack/src/mesh/mesh_access.c (revision d58a1b5f11ada8ddf896c41fff5a35e7f140c37e)
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_access.c"
39 
40 #include <string.h>
41 #include <stdio.h>
42 #include <stdarg.h>
43 
44 #include "mesh/mesh_access.h"
45 
46 #include "btstack_debug.h"
47 #include "btstack_memory.h"
48 #include "btstack_tlv.h"
49 
50 #include "mesh/beacon.h"
51 #include "mesh/mesh_foundation.h"
52 #include "mesh/mesh_iv_index_seq_number.h"
53 #include "mesh/mesh_node.h"
54 #include "mesh/mesh_proxy.h"
55 #include "mesh/mesh_upper_transport.h"
56 #include "mesh/mesh.h"
57 
58 #define MEST_TRANSACTION_TIMEOUT_MS  6000
59 
60 static void mesh_access_message_process_handler(mesh_pdu_t * pdu);
61 static void mesh_access_upper_transport_handler(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu);
62 static const mesh_operation_t * mesh_model_lookup_operation_by_opcode(mesh_model_t * model, uint32_t opcode);
63 
64 // receive
65 static uint16_t mesh_access_received_pdu_refcount;
66 
67 // acknowledged messages
68 static btstack_linked_list_t  mesh_access_acknowledged_messages;
69 static btstack_timer_source_t mesh_access_acknowledged_timer;
70 static int                    mesh_access_acknowledged_timer_active;
71 
72 // Transitions
73 static uint8_t mesh_transaction_id_counter = 0;
74 
75 void mesh_access_init(void){
76     // register with upper transport
77     mesh_upper_transport_register_access_message_handler(&mesh_access_message_process_handler);
78     mesh_upper_transport_set_higher_layer_handler(&mesh_access_upper_transport_handler);
79 }
80 
81 void mesh_access_emit_state_update_bool(btstack_packet_handler_t event_handler, uint8_t element_index, uint32_t model_identifier,
82     model_state_id_t state_identifier, model_state_update_reason_t reason, uint8_t value){
83     if (event_handler == NULL) return;
84     uint8_t event[14] = {HCI_EVENT_MESH_META, 12, MESH_SUBEVENT_STATE_UPDATE_BOOL};
85     int pos = 3;
86     event[pos++] = element_index;
87     little_endian_store_32(event, pos, model_identifier);
88     pos += 4;
89     little_endian_store_32(event, pos, (uint32_t)state_identifier);
90     pos += 4;
91     event[pos++] = (uint8_t)reason;
92     event[pos++] = value;
93     (*event_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
94 }
95 
96 void mesh_access_emit_state_update_int16(btstack_packet_handler_t event_handler, uint8_t element_index, uint32_t model_identifier,
97     model_state_id_t state_identifier, model_state_update_reason_t reason, int16_t value){
98     if (event_handler == NULL) return;
99     uint8_t event[15] = {HCI_EVENT_MESH_META, 13, MESH_SUBEVENT_STATE_UPDATE_BOOL};
100     int pos = 3;
101     event[pos++] = element_index;
102     little_endian_store_32(event, pos, model_identifier);
103     pos += 4;
104     little_endian_store_32(event, pos, (uint32_t)state_identifier);
105     pos += 4;
106     event[pos++] = (uint8_t)reason;
107     little_endian_store_16(event, pos, (uint16_t) value);
108     pos += 2;
109     (*event_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
110 }
111 
112 uint8_t mesh_access_acknowledged_message_retransmissions(void){
113     return 3;
114 }
115 
116 uint32_t mesh_access_acknowledged_message_timeout_ms(void){
117     return 30000;
118 }
119 
120 #define MESH_ACCESS_OPCODE_INVALID 0xFFFFFFFFu
121 #define MESH_ACCESS_OPCODE_NOT_SET 0xFFFFFFFEu
122 
123 void mesh_access_send_unacknowledged_pdu(mesh_pdu_t * pdu){
124     pdu->ack_opcode = MESH_ACCESS_OPCODE_INVALID;
125     mesh_upper_transport_send_access_pdu(pdu);
126 }
127 
128 void mesh_access_send_acknowledged_pdu(mesh_pdu_t * pdu, uint8_t retransmissions, uint32_t ack_opcode){
129     pdu->retransmit_count = retransmissions;
130     pdu->ack_opcode = ack_opcode;
131 
132     mesh_upper_transport_send_access_pdu(pdu);
133 }
134 
135 #define MESH_SUBEVENT_MESSAGE_NOT_ACKNOWLEDGED                                        0x30
136 
137 static void mesh_access_acknowledged_run(btstack_timer_source_t * ts){
138     UNUSED(ts);
139 
140     uint32_t now = btstack_run_loop_get_time_ms();
141 
142     // handle timeouts
143     btstack_linked_list_iterator_t ack_it;
144     btstack_linked_list_iterator_init(&ack_it, &mesh_access_acknowledged_messages);
145     while (btstack_linked_list_iterator_has_next(&ack_it)){
146         mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_iterator_next(&ack_it);
147         if (btstack_time_delta(now, pdu->retransmit_timeout_ms) >= 0) {
148             // remove from list
149             btstack_linked_list_remove(&mesh_access_acknowledged_messages, (btstack_linked_item_t*) pdu);
150             // retransmit or report failure
151             if (pdu->retransmit_count){
152                 pdu->retransmit_count--;
153                 mesh_upper_transport_send_access_pdu(pdu);
154             } else {
155                 // find correct model and emit error
156                 uint16_t src = mesh_pdu_src(pdu);
157                 uint16_t dst = mesh_pdu_dst(pdu);
158                 mesh_element_t * element = mesh_node_element_for_unicast_address(src);
159                 if (element){
160                     // find
161                     mesh_model_iterator_t model_it;
162                     mesh_model_iterator_init(&model_it, element);
163                     while (mesh_model_iterator_has_next(&model_it)){
164                         mesh_model_t * model = mesh_model_iterator_next(&model_it);
165                         // find opcode in table
166                         const mesh_operation_t * operation = mesh_model_lookup_operation_by_opcode(model, pdu->ack_opcode);
167                         if (operation == NULL) continue;
168                         if (model->model_packet_handler == NULL) continue;
169                         // emit event
170                         uint8_t event[13];
171                         event[0] = HCI_EVENT_MESH_META;
172                         event[1] = sizeof(event) - 2;
173                         event[2] = element->element_index;
174                         little_endian_store_32(event, 3, model->model_identifier);
175                         little_endian_store_32(event, 7, pdu->ack_opcode);
176                         little_endian_store_16(event, 11, dst);
177                         (*model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
178                     }
179                 }
180 
181                 // free
182                 mesh_upper_transport_pdu_free(pdu);
183             }
184         }
185     }
186 
187     if (mesh_access_acknowledged_timer_active) return;
188 
189     // find earliest timeout and set timer
190     btstack_linked_list_iterator_init(&ack_it, &mesh_access_acknowledged_messages);
191     int32_t next_timeout_ms = 0;
192     while (btstack_linked_list_iterator_has_next(&ack_it)){
193         mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_iterator_next(&ack_it);
194         int32_t timeout_delta_ms = btstack_time_delta(pdu->retransmit_timeout_ms, now);
195         if (next_timeout_ms == 0 || timeout_delta_ms < next_timeout_ms){
196             next_timeout_ms = timeout_delta_ms;
197         }
198     }
199 
200     // set timer
201     if (next_timeout_ms == 0) return;
202 
203     btstack_run_loop_set_timer(&mesh_access_acknowledged_timer, next_timeout_ms);
204     btstack_run_loop_set_timer_handler(&mesh_access_acknowledged_timer, mesh_access_acknowledged_run);
205     btstack_run_loop_add_timer(&mesh_access_acknowledged_timer);
206     mesh_access_acknowledged_timer_active = 1;
207 }
208 
209 static void mesh_access_acknowledged_received(uint16_t rx_src, uint32_t opcode){
210     // check if received src matches our dest
211     // free acknowledged messages if we were waiting for this message
212 
213     btstack_linked_list_iterator_t ack_it;
214     btstack_linked_list_iterator_init(&ack_it, &mesh_access_acknowledged_messages);
215     while (btstack_linked_list_iterator_has_next(&ack_it)){
216         mesh_pdu_t * tx_pdu = (mesh_pdu_t *) btstack_linked_list_iterator_next(&ack_it);
217         uint16_t tx_dest = mesh_pdu_dst(tx_pdu);
218         if (tx_dest != rx_src) continue;
219         if (tx_pdu->ack_opcode != opcode) continue;
220         // got expected response from dest, remove from outgoing messages
221         mesh_upper_transport_pdu_free(tx_pdu);
222         return;
223     }
224 }
225 
226 static void mesh_access_upper_transport_handler(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu){
227     UNUSED(status);
228     switch (callback_type){
229         case MESH_TRANSPORT_PDU_SENT:
230             // unacknowledged -> free
231             if (pdu->ack_opcode == MESH_ACCESS_OPCODE_INVALID){
232                 mesh_upper_transport_pdu_free(pdu);
233                 break;
234             }
235             // setup timeout
236             pdu->retransmit_timeout_ms = btstack_run_loop_get_time_ms() + mesh_access_acknowledged_message_timeout_ms();
237             // add to mesh_access_acknowledged_messages
238             btstack_linked_list_add(&mesh_access_acknowledged_messages, (btstack_linked_item_t *) pdu);
239             // update timer
240             mesh_access_acknowledged_run(NULL);
241             break;
242         default:
243             break;
244     }
245 }
246 
247 // Mesh Model Transitions
248 
249 uint32_t mesh_access_time_gdtt2ms(uint8_t time_gdtt){
250     uint8_t num_steps  = mesh_access_transitions_num_steps_from_gdtt(time_gdtt);
251     if (num_steps > 0x3E) return 0;
252 
253     return mesh_access_transitions_step_ms_from_gdtt(time_gdtt) * num_steps;
254 }
255 
256 uint8_t mesh_access_transitions_num_steps_from_gdtt(uint8_t time_gdtt){
257     return time_gdtt & 0x3fu;
258 }
259 
260 uint32_t mesh_access_transitions_step_ms_from_gdtt(uint8_t time_gdtt){
261     mesh_default_transition_step_resolution_t step_resolution = (mesh_default_transition_step_resolution_t) (time_gdtt >> 6);
262     switch (step_resolution){
263         case MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_100ms:
264             return 100;
265         case MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_1s:
266             return 1000;
267         case MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_10s:
268             return 10000;
269         case MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_10min:
270             return 600000;
271         default:
272             return 0;
273     }
274 }
275 
276 uint8_t mesh_access_transactions_get_next_transaction_id(void){
277     mesh_transaction_id_counter++;
278     if (mesh_transaction_id_counter == 0){
279         mesh_transaction_id_counter = 1;
280     }
281     return mesh_transaction_id_counter;
282 }
283 
284 static int mesh_access_transitions_transaction_is_expired(mesh_transition_t * transition){
285     return (btstack_run_loop_get_time_ms() - transition->transaction_timestamp_ms) > MEST_TRANSACTION_TIMEOUT_MS;
286 }
287 
288 mesh_transaction_status_t mesh_access_transitions_transaction_status(mesh_transition_t * transition, uint8_t transaction_identifier, uint16_t src_address, uint16_t dst_address){
289     if (transition->src_address != src_address || transition->dst_address != dst_address) return MESH_TRANSACTION_STATUS_DIFFERENT_DST_OR_SRC;
290 
291     if (transition->transaction_identifier == transaction_identifier && !mesh_access_transitions_transaction_is_expired(transition)){
292             return MESH_TRANSACTION_STATUS_RETRANSMISSION;
293     }
294     return MESH_TRANSACTION_STATUS_NEW;
295 }
296 
297 void mesh_access_transitions_init_transaction(mesh_transition_t * transition, uint8_t transaction_identifier, uint16_t src_address, uint16_t dst_address){
298     transition->transaction_timestamp_ms = btstack_run_loop_get_time_ms();
299     transition->transaction_identifier = transaction_identifier;
300     transition->src_address = src_address;
301     transition->dst_address = dst_address;
302 }
303 
304 static void mesh_server_transition_timeout(btstack_timer_source_t * ts){
305     mesh_transition_t * base_transition = (mesh_transition_t*) btstack_run_loop_get_timer_context(ts);
306     switch (base_transition->state){
307         case MESH_TRANSITION_STATE_DELAYED:
308             base_transition->state = MESH_TRANSITION_STATE_ACTIVE;
309             (*base_transition->transition_callback)(base_transition, MODEL_STATE_UPDATE_REASON_TRANSITION_START);
310             if (base_transition->num_steps > 0){
311                 btstack_run_loop_set_timer(&base_transition->timer, base_transition->step_duration_ms);
312                 btstack_run_loop_add_timer(&base_transition->timer);
313                 return;
314             }
315             base_transition->state = MESH_TRANSITION_STATE_IDLE;
316             (*base_transition->transition_callback)(base_transition, MODEL_STATE_UPDATE_REASON_TRANSITION_END);
317             break;
318         case MESH_TRANSITION_STATE_ACTIVE:
319             if (base_transition->num_steps < MESH_TRANSITION_NUM_STEPS_INFINITE){
320                 base_transition->num_steps--;
321             }
322             (*base_transition->transition_callback)(base_transition, MODEL_STATE_UPDATE_REASON_TRANSITION_ACTIVE);
323             if (base_transition->num_steps > 0){
324                 btstack_run_loop_set_timer(&base_transition->timer, base_transition->step_duration_ms);
325                 btstack_run_loop_add_timer(&base_transition->timer);
326                 return;
327             }
328             base_transition->state = MESH_TRANSITION_STATE_IDLE;
329             (*base_transition->transition_callback)(base_transition, MODEL_STATE_UPDATE_REASON_TRANSITION_END);
330             break;
331         default:
332             break;
333     }
334 }
335 
336 void mesh_access_transition_setup(mesh_model_t *mesh_model, mesh_transition_t * base_transition, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, void (*transition_callback)(mesh_transition_t * base_transition, model_state_update_reason_t event)){
337 
338     base_transition->mesh_model          = mesh_model;
339     base_transition->num_steps           = mesh_access_transitions_num_steps_from_gdtt(transition_time_gdtt);
340     base_transition->step_resolution     = (mesh_default_transition_step_resolution_t) (transition_time_gdtt >> 6);
341     base_transition->step_duration_ms    = mesh_access_transitions_step_ms_from_gdtt(transition_time_gdtt);
342     base_transition->transition_callback = transition_callback;
343 
344     btstack_run_loop_set_timer_context(&base_transition->timer, base_transition);
345     btstack_run_loop_set_timer_handler(&base_transition->timer, &mesh_server_transition_timeout);
346 
347     // delayed
348     if (delay_time_gdtt > 0){
349         base_transition->state = MESH_TRANSITION_STATE_DELAYED;
350         btstack_run_loop_set_timer(&base_transition->timer, delay_time_gdtt * 5);
351         btstack_run_loop_add_timer(&base_transition->timer);
352         return;
353     }
354 
355     // started
356     if (base_transition->num_steps > 0){
357         base_transition->state = MESH_TRANSITION_STATE_ACTIVE;
358         btstack_run_loop_set_timer(&base_transition->timer, base_transition->step_duration_ms);
359         btstack_run_loop_add_timer(&base_transition->timer);
360         return;
361     }
362 
363     // instanteneous update
364     base_transition->state = MESH_TRANSITION_STATE_IDLE;
365     (*transition_callback)(base_transition, MODEL_STATE_UPDATE_REASON_SET);
366     return;
367 }
368 
369 void mesh_access_transitions_abort_transaction(mesh_transition_t * base_transition){
370     btstack_run_loop_add_timer(&base_transition->timer);
371 }
372 
373 uint16_t mesh_pdu_ctl(mesh_pdu_t * pdu){
374     switch (pdu->pdu_type){
375         case MESH_PDU_TYPE_TRANSPORT:
376             return mesh_transport_ctl((mesh_transport_pdu_t*) pdu);
377         case MESH_PDU_TYPE_NETWORK:
378             return mesh_network_control((mesh_network_pdu_t *) pdu);
379         default:
380             return 0;
381     }
382 }
383 
384 uint16_t mesh_pdu_ttl(mesh_pdu_t * pdu){
385     switch (pdu->pdu_type){
386         case MESH_PDU_TYPE_TRANSPORT:
387             return mesh_transport_ttl((mesh_transport_pdu_t*) pdu);
388         case MESH_PDU_TYPE_NETWORK:
389             return mesh_network_ttl((mesh_network_pdu_t *) pdu);
390         default:
391             return 0;
392     }
393 }
394 
395 uint16_t mesh_pdu_src(mesh_pdu_t * pdu){
396     switch (pdu->pdu_type){
397         case MESH_PDU_TYPE_TRANSPORT:
398             return mesh_transport_src((mesh_transport_pdu_t*) pdu);
399         case MESH_PDU_TYPE_NETWORK:
400             return mesh_network_src((mesh_network_pdu_t *) pdu);
401         default:
402             return MESH_ADDRESS_UNSASSIGNED;
403     }
404 }
405 
406 uint16_t mesh_pdu_dst(mesh_pdu_t * pdu){
407     switch (pdu->pdu_type){
408         case MESH_PDU_TYPE_TRANSPORT:
409             return mesh_transport_dst((mesh_transport_pdu_t*) pdu);
410         case MESH_PDU_TYPE_NETWORK:
411             return mesh_network_dst((mesh_network_pdu_t *) pdu);
412         default:
413             return MESH_ADDRESS_UNSASSIGNED;
414     }
415 }
416 
417 uint16_t mesh_pdu_netkey_index(mesh_pdu_t * pdu){
418     switch (pdu->pdu_type){
419         case MESH_PDU_TYPE_TRANSPORT:
420             return ((mesh_transport_pdu_t*) pdu)->netkey_index;
421         case MESH_PDU_TYPE_NETWORK:
422             return ((mesh_network_pdu_t *) pdu)->netkey_index;
423         default:
424             return 0;
425     }
426 }
427 
428 uint16_t mesh_pdu_appkey_index(mesh_pdu_t * pdu){
429     switch (pdu->pdu_type){
430         case MESH_PDU_TYPE_TRANSPORT:
431             return ((mesh_transport_pdu_t*) pdu)->appkey_index;
432         case MESH_PDU_TYPE_NETWORK:
433             return ((mesh_network_pdu_t *) pdu)->appkey_index;
434         default:
435             return 0;
436     }
437 }
438 
439 uint16_t mesh_pdu_len(mesh_pdu_t * pdu){
440     switch (pdu->pdu_type){
441         case MESH_PDU_TYPE_TRANSPORT:
442             return ((mesh_transport_pdu_t*) pdu)->len;
443         case MESH_PDU_TYPE_NETWORK:
444             return ((mesh_network_pdu_t *) pdu)->len - 10;
445         default:
446             return 0;
447     }
448 }
449 
450 uint8_t * mesh_pdu_data(mesh_pdu_t * pdu){
451     switch (pdu->pdu_type){
452         case MESH_PDU_TYPE_TRANSPORT:
453             return ((mesh_transport_pdu_t*) pdu)->data;
454         case MESH_PDU_TYPE_NETWORK:
455             return &((mesh_network_pdu_t *) pdu)->data[10];
456         default:
457             return NULL;
458     }
459 }
460 
461 uint8_t mesh_pdu_control_opcode(mesh_pdu_t * pdu){
462     switch (pdu->pdu_type){
463         case MESH_PDU_TYPE_TRANSPORT:
464             return mesh_transport_control_opcode((mesh_transport_pdu_t*) pdu);
465         case MESH_PDU_TYPE_NETWORK:
466             return mesh_network_control_opcode((mesh_network_pdu_t *) pdu);
467         default:
468             return 0xff;
469     }
470 }
471 
472 // message parser
473 
474 static int mesh_access_get_opcode(uint8_t * buffer, uint16_t buffer_size, uint32_t * opcode, uint16_t * opcode_size){
475     switch (buffer[0] >> 6){
476         case 0:
477         case 1:
478             if (buffer[0] == 0x7f) return 0;
479             *opcode = buffer[0];
480             *opcode_size = 1;
481             return 1;
482         case 2:
483             if (buffer_size < 2) return 0;
484             *opcode = big_endian_read_16(buffer, 0);
485             *opcode_size = 2;
486             return 1;
487         case 3:
488             if (buffer_size < 3) return 0;
489             *opcode = (buffer[0] << 16) | little_endian_read_16(buffer, 1);
490             *opcode_size = 3;
491             return 1;
492         default:
493             return 0;
494     }
495 }
496 
497 static int mesh_access_transport_get_opcode(mesh_transport_pdu_t * transport_pdu, uint32_t * opcode, uint16_t * opcode_size){
498     return mesh_access_get_opcode(transport_pdu->data, transport_pdu->len, opcode, opcode_size);
499 }
500 
501 static int mesh_access_network_get_opcode(mesh_network_pdu_t * network_pdu, uint32_t * opcode, uint16_t * opcode_size){
502     // TransMIC already removed by mesh_upper_transport_validate_unsegmented_message_ccm
503     return mesh_access_get_opcode(&network_pdu->data[10], network_pdu->len - 10, opcode, opcode_size);
504 }
505 
506 int mesh_access_pdu_get_opcode(mesh_pdu_t * pdu, uint32_t * opcode, uint16_t * opcode_size){
507     switch (pdu->pdu_type){
508         case MESH_PDU_TYPE_TRANSPORT:
509             return mesh_access_transport_get_opcode((mesh_transport_pdu_t*) pdu, opcode, opcode_size);
510         case MESH_PDU_TYPE_NETWORK:
511             return mesh_access_network_get_opcode((mesh_network_pdu_t *) pdu, opcode, opcode_size);
512         default:
513             return 0;
514     }
515 }
516 
517 void mesh_access_parser_skip(mesh_access_parser_state_t * state, uint16_t bytes_to_skip){
518     state->data += bytes_to_skip;
519     state->len  -= bytes_to_skip;
520 }
521 
522 int mesh_access_parser_init(mesh_access_parser_state_t * state, mesh_pdu_t * pdu){
523     state->data = mesh_pdu_data(pdu);
524     state->len  = mesh_pdu_len(pdu);
525 
526     uint16_t opcode_size = 0;
527     int ok = mesh_access_get_opcode(state->data, state->len, &state->opcode, &opcode_size);
528     if (ok){
529         mesh_access_parser_skip(state, opcode_size);
530     }
531     return ok;
532 }
533 
534 uint16_t mesh_access_parser_available(mesh_access_parser_state_t * state){
535     return state->len;
536 }
537 
538 uint8_t mesh_access_parser_get_u8(mesh_access_parser_state_t * state){
539     uint8_t value = *state->data;
540     mesh_access_parser_skip(state, 1);
541     return value;
542 }
543 
544 uint16_t mesh_access_parser_get_u16(mesh_access_parser_state_t * state){
545     uint16_t value = little_endian_read_16(state->data, 0);
546     mesh_access_parser_skip(state, 2);
547     return value;
548 }
549 
550 uint32_t mesh_access_parser_get_u24(mesh_access_parser_state_t * state){
551     uint32_t value = little_endian_read_24(state->data, 0);
552     mesh_access_parser_skip(state, 3);
553     return value;
554 }
555 
556 uint32_t mesh_access_parser_get_u32(mesh_access_parser_state_t * state){
557     uint32_t value = little_endian_read_32(state->data, 0);
558     mesh_access_parser_skip(state, 4);
559     return value;
560 }
561 
562 void mesh_access_parser_get_u128(mesh_access_parser_state_t * state, uint8_t * dest){
563     reverse_128( state->data, dest);
564     mesh_access_parser_skip(state, 16);
565 }
566 
567 void mesh_access_parser_get_label_uuid(mesh_access_parser_state_t * state, uint8_t * dest){
568     memcpy( dest, state->data, 16);
569     mesh_access_parser_skip(state, 16);
570 }
571 
572 void mesh_access_parser_get_key(mesh_access_parser_state_t * state, uint8_t * dest){
573     memcpy( dest, state->data, 16);
574     mesh_access_parser_skip(state, 16);
575 }
576 
577 uint32_t mesh_access_parser_get_model_identifier(mesh_access_parser_state_t * parser){
578     uint16_t vendor_id = BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC;
579     if (mesh_access_parser_available(parser) == 4){
580         vendor_id = mesh_access_parser_get_u16(parser);
581     }
582     uint16_t model_id = mesh_access_parser_get_u16(parser);
583     return mesh_model_get_model_identifier(vendor_id, model_id);
584 }
585 
586 // Mesh Access Message Builder
587 
588 // message builder
589 
590 static int mesh_access_setup_opcode(uint8_t * buffer, uint32_t opcode){
591     if (opcode < 0x100){
592         buffer[0] = opcode;
593         return 1;
594     }
595     if (opcode < 0x10000){
596         big_endian_store_16(buffer, 0, opcode);
597         return 2;
598     }
599     buffer[0] = opcode >> 16;
600     little_endian_store_16(buffer, 1, opcode & 0xffff);
601     return 3;
602 }
603 
604 mesh_transport_pdu_t * mesh_access_transport_init(uint32_t opcode){
605     mesh_transport_pdu_t * pdu = mesh_transport_pdu_get();
606     if (!pdu) return NULL;
607 
608     pdu->len  = mesh_access_setup_opcode(pdu->data, opcode);
609     pdu->pdu_header.ack_opcode = MESH_ACCESS_OPCODE_NOT_SET;
610     return pdu;
611 }
612 
613 void mesh_access_transport_add_uint8(mesh_transport_pdu_t * pdu, uint8_t value){
614     pdu->data[pdu->len++] = value;
615 }
616 
617 void mesh_access_transport_add_uint16(mesh_transport_pdu_t * pdu, uint16_t value){
618     little_endian_store_16(pdu->data, pdu->len, value);
619     pdu->len += 2;
620 }
621 
622 void mesh_access_transport_add_uint24(mesh_transport_pdu_t * pdu, uint32_t value){
623     little_endian_store_24(pdu->data, pdu->len, value);
624     pdu->len += 3;
625 }
626 
627 void mesh_access_transport_add_uint32(mesh_transport_pdu_t * pdu, uint32_t value){
628     little_endian_store_32(pdu->data, pdu->len, value);
629     pdu->len += 4;
630 }
631 void mesh_access_transport_add_model_identifier(mesh_transport_pdu_t * pdu, uint32_t model_identifier){
632     if (!mesh_model_is_bluetooth_sig(model_identifier)){
633         mesh_access_transport_add_uint16( pdu, mesh_model_get_vendor_id(model_identifier) );
634     }
635     mesh_access_transport_add_uint16( pdu, mesh_model_get_model_id(model_identifier) );
636 }
637 
638 mesh_network_pdu_t * mesh_access_network_init(uint32_t opcode){
639     mesh_network_pdu_t * pdu = mesh_network_pdu_get();
640     if (!pdu) return NULL;
641 
642     pdu->len  = mesh_access_setup_opcode(&pdu->data[10], opcode) + 10;
643     pdu->pdu_header.ack_opcode = MESH_ACCESS_OPCODE_NOT_SET;
644     return pdu;
645 }
646 
647 void mesh_access_network_add_uint8(mesh_network_pdu_t * pdu, uint8_t value){
648     pdu->data[pdu->len++] = value;
649 }
650 
651 void mesh_access_network_add_uint16(mesh_network_pdu_t * pdu, uint16_t value){
652     little_endian_store_16(pdu->data, pdu->len, value);
653     pdu->len += 2;
654 }
655 
656 void mesh_access_network_add_uint24(mesh_network_pdu_t * pdu, uint16_t value){
657     little_endian_store_24(pdu->data, pdu->len, value);
658     pdu->len += 3;
659 }
660 
661 void mesh_access_network_add_uint32(mesh_network_pdu_t * pdu, uint16_t value){
662     little_endian_store_32(pdu->data, pdu->len, value);
663     pdu->len += 4;
664 }
665 
666 void mesh_access_network_add_model_identifier(mesh_network_pdu_t * pdu, uint32_t model_identifier){
667     if (mesh_model_is_bluetooth_sig(model_identifier)){
668         mesh_access_network_add_uint16( pdu, mesh_model_get_model_id(model_identifier) );
669     } else {
670         mesh_access_network_add_uint32( pdu, model_identifier );
671     }
672 }
673 
674 // access message template
675 
676 mesh_network_pdu_t * mesh_access_setup_unsegmented_message(const mesh_access_message_t *message_template, ...){
677     mesh_network_pdu_t * network_pdu = mesh_access_network_init(message_template->opcode);
678     if (!network_pdu) return NULL;
679 
680     va_list argptr;
681     va_start(argptr, message_template);
682 
683     // add params
684     const char * format = message_template->format;
685     uint16_t word;
686     uint32_t longword;
687     while (*format){
688         switch (*format){
689             case '1':
690                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
691                 mesh_access_network_add_uint8( network_pdu, word);
692                 break;
693             case '2':
694                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
695                 mesh_access_network_add_uint16( network_pdu, word);
696                 break;
697             case '3':
698                 longword = va_arg(argptr, uint32_t);
699                 mesh_access_network_add_uint24( network_pdu, longword);
700                 break;
701             case '4':
702                 longword = va_arg(argptr, uint32_t);
703                 mesh_access_network_add_uint32( network_pdu, longword);
704                 break;
705             case 'm':
706                 longword = va_arg(argptr, uint32_t);
707                 mesh_access_network_add_model_identifier( network_pdu, longword);
708                 break;
709             default:
710                 log_error("Unsupported mesh message format specifier '%c", *format);
711                 break;
712         }
713         format++;
714     }
715 
716     va_end(argptr);
717 
718     return network_pdu;
719 }
720 
721 mesh_transport_pdu_t * mesh_access_setup_segmented_message(const mesh_access_message_t *message_template, ...){
722     mesh_transport_pdu_t * transport_pdu = mesh_access_transport_init(message_template->opcode);
723     if (!transport_pdu) return NULL;
724 
725     va_list argptr;
726     va_start(argptr, message_template);
727 
728     // add params
729     const char * format = message_template->format;
730     uint16_t word;
731     uint32_t longword;
732     while (*format){
733         switch (*format++){
734             case '1':
735                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
736                 mesh_access_transport_add_uint8( transport_pdu, word);
737                 break;
738             case '2':
739                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
740                 mesh_access_transport_add_uint16( transport_pdu, word);
741                 break;
742             case '3':
743                 longword = va_arg(argptr, uint32_t);
744                 mesh_access_transport_add_uint24( transport_pdu, longword);
745                 break;
746             case '4':
747                 longword = va_arg(argptr, uint32_t);
748                 mesh_access_transport_add_uint32( transport_pdu, longword);
749                 break;
750             case 'm':
751                 longword = va_arg(argptr, uint32_t);
752                 mesh_access_transport_add_model_identifier( transport_pdu, longword);
753                 break;
754             default:
755                 break;
756         }
757     }
758 
759     va_end(argptr);
760 
761     return transport_pdu;
762 }
763 
764 static const mesh_operation_t * mesh_model_lookup_operation_by_opcode(mesh_model_t * model, uint32_t opcode){
765     // find opcode in table
766     const mesh_operation_t * operation = model->operations;
767     if (operation == NULL) return NULL;
768     for ( ; operation->handler != NULL ; operation++){
769         if (operation->opcode != opcode) continue;
770         return operation;
771     }
772     return NULL;
773 }
774 
775 static const mesh_operation_t * mesh_model_lookup_operation(mesh_model_t * model, mesh_pdu_t * pdu){
776 
777     uint32_t opcode = 0;
778     uint16_t opcode_size = 0;
779     int ok = mesh_access_pdu_get_opcode( pdu, &opcode, &opcode_size);
780     if (!ok) return NULL;
781 
782     uint16_t len = mesh_pdu_len(pdu);
783 
784     // find opcode in table
785     const mesh_operation_t * operation = model->operations;
786     if (operation == NULL) return NULL;
787     for ( ; operation->handler != NULL ; operation++){
788         if (operation->opcode != opcode) continue;
789         if ((opcode_size + operation->minimum_length) > len) continue;
790         return operation;
791     }
792     return NULL;
793 }
794 
795 static int mesh_access_validate_appkey_index(mesh_model_t * model, uint16_t appkey_index){
796     // DeviceKey is valid for all models
797     if (appkey_index == MESH_DEVICE_KEY_INDEX) return 1;
798     // check if AppKey that is bound to this particular model
799     return mesh_model_contains_appkey(model, appkey_index);
800 }
801 
802 // decrease use count and report as free if done
803 void mesh_access_message_processed(mesh_pdu_t * pdu){
804     if (mesh_access_received_pdu_refcount > 0){
805         mesh_access_received_pdu_refcount--;
806     }
807     if (mesh_access_received_pdu_refcount == 0){
808         mesh_upper_transport_message_processed_by_higher_layer(pdu);
809     }
810 }
811 
812 static void mesh_access_message_process_handler(mesh_pdu_t * pdu){
813 
814     // init use count
815     mesh_access_received_pdu_refcount = 1;
816 
817     // get opcode and size
818     uint32_t opcode = 0;
819     uint16_t opcode_size = 0;
820 
821     int ok = mesh_access_pdu_get_opcode( pdu, &opcode, &opcode_size);
822     if (!ok) {
823         mesh_access_message_processed(pdu);
824         return;
825     }
826 
827     uint16_t len = mesh_pdu_len(pdu);
828     printf("MESH Access Message, Opcode = %x: ", opcode);
829     printf_hexdump(mesh_pdu_data(pdu), len);
830 
831     uint16_t src = mesh_pdu_src(pdu);
832     uint16_t dst = mesh_pdu_dst(pdu);
833     uint16_t appkey_index = mesh_pdu_appkey_index(pdu);
834     if (mesh_network_address_unicast(dst)){
835         // loookup element by unicast address
836         mesh_element_t * element = mesh_node_element_for_unicast_address(dst);
837         if (element != NULL){
838             // iterate over models, look for operation
839             mesh_model_iterator_t model_it;
840             mesh_model_iterator_init(&model_it, element);
841             while (mesh_model_iterator_has_next(&model_it)){
842                 mesh_model_t * model = mesh_model_iterator_next(&model_it);
843                 // find opcode in table
844                 const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu);
845                 if (operation == NULL) continue;
846                 if (mesh_access_validate_appkey_index(model, appkey_index) == 0) continue;
847                 mesh_access_acknowledged_received(src, opcode);
848                 mesh_access_received_pdu_refcount++;
849                 operation->handler(model, pdu);
850             }
851         }
852     }
853     else if (mesh_network_address_group(dst)){
854 
855         // handle fixed group address
856         if (dst >= 0xff00){
857             int deliver_to_primary_element = 1;
858             switch (dst){
859                 case MESH_ADDRESS_ALL_PROXIES:
860                     if (mesh_foundation_gatt_proxy_get() == 1){
861                         deliver_to_primary_element = 1;
862                     }
863                     break;
864                 case MESH_ADDRESS_ALL_FRIENDS:
865                     // TODO: not implemented
866                     break;
867                 case MESH_ADDRESS_ALL_RELAYS:
868                     if (mesh_foundation_relay_get() == 1){
869                         deliver_to_primary_element = 1;
870                     }
871                     break;
872                 case MESH_ADDRESS_ALL_NODES:
873                     deliver_to_primary_element = 1;
874                     break;
875                 default:
876                     break;
877             }
878             if (deliver_to_primary_element){
879                 mesh_model_iterator_t model_it;
880                 mesh_model_iterator_init(&model_it, mesh_node_get_primary_element());
881                 while (mesh_model_iterator_has_next(&model_it)){
882                     mesh_model_t * model = mesh_model_iterator_next(&model_it);
883                     // find opcode in table
884                     const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu);
885                     if (operation == NULL) continue;
886                     if (mesh_access_validate_appkey_index(model, appkey_index) == 0) continue;
887                     mesh_access_acknowledged_received(src, opcode);
888                     mesh_access_received_pdu_refcount++;
889                     operation->handler(model, pdu);
890                 }
891             }
892         }
893         else {
894             // iterate over all elements / models, check subscription list
895             mesh_element_iterator_t it;
896             mesh_element_iterator_init(&it);
897             while (mesh_element_iterator_has_next(&it)){
898                 mesh_element_t * element = (mesh_element_t *) mesh_element_iterator_next(&it);
899                 mesh_model_iterator_t model_it;
900                 mesh_model_iterator_init(&model_it, element);
901                 while (mesh_model_iterator_has_next(&model_it)){
902                     mesh_model_t * model = mesh_model_iterator_next(&model_it);
903                     if (mesh_model_contains_subscription(model, dst)){
904                         // find opcode in table
905                         const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu);
906                         if (operation == NULL) continue;
907                         if (mesh_access_validate_appkey_index(model, appkey_index) == 0) continue;
908                         mesh_access_acknowledged_received(src, opcode);
909                         mesh_access_received_pdu_refcount++;
910                         operation->handler(model, pdu);
911                     }
912                 }
913             }
914         }
915     }
916 
917     // we're done
918     mesh_access_message_processed(pdu);
919 }
920 
921 // Mesh Model Publication
922 static btstack_timer_source_t mesh_access_publication_timer;
923 
924 static uint32_t mesh_model_publication_retransmit_count(uint8_t retransmit){
925     return retransmit & 0x07u;
926 }
927 
928 static uint32_t mesh_model_publication_retransmission_period_ms(uint8_t retransmit){
929     return ((uint32_t)((retransmit >> 3) + 1)) * 50;
930 }
931 
932 static void mesh_model_publication_setup_publication(mesh_publication_model_t * publication_model, uint32_t now){
933 
934     // set retransmit counter
935     publication_model->retransmit_count = mesh_model_publication_retransmit_count(publication_model->retransmit);
936 
937     // schedule next publication or retransmission
938     uint32_t publication_period_ms = mesh_access_time_gdtt2ms(publication_model->period) >> publication_model->period_divisor;
939 
940     // set next publication
941     if (publication_period_ms != 0){
942         publication_model->next_publication_ms = now + publication_period_ms;
943         publication_model->state = MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS;
944     } else {
945         publication_model->state = MESH_MODEL_PUBLICATION_STATE_IDLE;
946     }
947 }
948 
949 // assumes retransmit_count is valid
950 static void mesh_model_publication_setup_retransmission(mesh_publication_model_t * publication_model, uint32_t now){
951     uint32_t publication_period_ms = mesh_access_time_gdtt2ms(publication_model->period) >> publication_model->period_divisor;
952 
953     // retransmission done
954     if (publication_model->retransmit_count == 0) {
955         // wait for next main event if periodic and retransmission complete
956         if (publication_period_ms != 0){
957             publication_model->state = MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS;
958         } else {
959             publication_model->state = MESH_MODEL_PUBLICATION_STATE_IDLE;
960         }
961         return;
962     }
963 
964     // calc next retransmit time
965     uint32_t retransmission_period_ms = mesh_model_publication_retransmission_period_ms(publication_model->retransmit) >> publication_model->period_divisor;
966     uint32_t retransmission_ms = now + retransmission_period_ms;
967 
968     // check next publication timeout is before next retransmission
969     if (publication_period_ms != 0){
970         if (btstack_time_delta(retransmission_ms, publication_model->next_publication_ms) > 0) return;
971     }
972 
973     // schedule next retransmission
974     publication_model->next_retransmit_ms = retransmission_ms;
975     publication_model->state = MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS;
976 }
977 
978 static void mesh_model_publication_publish_now_model(mesh_model_t * mesh_model){
979     mesh_publication_model_t * publication_model = mesh_model->publication_model;
980     if (publication_model == NULL) return;
981     if (publication_model->publish_state_fn == NULL) return;
982     uint16_t dest = publication_model->address;
983     if (dest == MESH_ADDRESS_UNSASSIGNED) return;
984     uint16_t appkey_index = publication_model->appkey_index;
985     mesh_transport_key_t * app_key = mesh_transport_key_get(appkey_index);
986     if (app_key == NULL) return;
987 
988     // compose message
989     mesh_pdu_t * pdu = (*publication_model->publish_state_fn)(mesh_model);
990     if (pdu == NULL) return;
991 
992     // handle ttl = default
993     uint8_t ttl = publication_model->ttl;
994     if (ttl == 0xff){
995         ttl = mesh_foundation_default_ttl_get();
996     }
997 
998     mesh_upper_transport_setup_access_pdu_header(pdu, app_key->netkey_index, appkey_index, ttl, mesh_access_get_element_address(mesh_model), dest, 0);
999     mesh_access_send_unacknowledged_pdu(pdu);
1000 }
1001 
1002 static void mesh_model_publication_run(btstack_timer_source_t * ts){
1003 
1004     uint32_t now = btstack_run_loop_get_time_ms();
1005 
1006     // iterate over elements and models and handle time-based transitions
1007     mesh_element_iterator_t element_it;
1008     mesh_element_iterator_init(&element_it);
1009     while (mesh_element_iterator_has_next(&element_it)){
1010         mesh_element_t * element = mesh_element_iterator_next(&element_it);
1011         mesh_model_iterator_t model_it;
1012         mesh_model_iterator_init(&model_it, element);
1013         while (mesh_model_iterator_has_next(&model_it)){
1014             mesh_model_t * mesh_model = mesh_model_iterator_next(&model_it);
1015             mesh_publication_model_t * publication_model = mesh_model->publication_model;
1016             if (publication_model == NULL) continue;
1017 
1018             // check if either timer fired
1019             switch (publication_model->state){
1020                 case MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS:
1021                     if (btstack_time_delta(publication_model->next_publication_ms, now) > 0) break;
1022                     publication_model->state = MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY;
1023                     break;
1024                 case MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS:
1025                     if (btstack_time_delta(publication_model->next_retransmit_ms, now) > 0) break;
1026                     publication_model->state = MESH_MODEL_PUBLICATION_STATE_RETRANSMIT_READY;
1027                     break;
1028                 default:
1029                     break;
1030             }
1031 
1032             switch (publication_model->state){
1033                 case MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY:
1034                     // schedule next publication and retransmission
1035                     mesh_model_publication_setup_publication(publication_model, now);
1036                     mesh_model_publication_setup_retransmission(publication_model, now);
1037                     mesh_model_publication_publish_now_model(mesh_model);
1038                     break;
1039                 case MESH_MODEL_PUBLICATION_STATE_RETRANSMIT_READY:
1040                     // schedule next retransmission
1041                     publication_model->retransmit_count--;
1042                     mesh_model_publication_setup_retransmission(publication_model, now);
1043                     mesh_model_publication_publish_now_model(mesh_model);
1044                     break;
1045                 default:
1046                     break;
1047             }
1048         }
1049     }
1050 
1051     int32_t next_timeout_ms = 0;
1052     mesh_element_iterator_init(&element_it);
1053     while (mesh_element_iterator_has_next(&element_it)){
1054         mesh_element_t * element = mesh_element_iterator_next(&element_it);
1055         mesh_model_iterator_t model_it;
1056         mesh_model_iterator_init(&model_it, element);
1057         while (mesh_model_iterator_has_next(&model_it)){
1058             mesh_model_t * mesh_model = mesh_model_iterator_next(&model_it);
1059             mesh_publication_model_t * publication_model = mesh_model->publication_model;
1060             if (publication_model == NULL) continue;
1061 
1062             // schedule next
1063             int32_t timeout_delta_ms;
1064             switch (publication_model->state){
1065                 case MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS:
1066                     timeout_delta_ms = btstack_time_delta(publication_model->next_publication_ms, now);
1067                     if (next_timeout_ms == 0 || timeout_delta_ms < next_timeout_ms){
1068                         next_timeout_ms = timeout_delta_ms;
1069                     }
1070                     break;
1071                 case MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS:
1072                     timeout_delta_ms = btstack_time_delta(publication_model->next_retransmit_ms, now);
1073                     if (next_timeout_ms == 0 || timeout_delta_ms < next_timeout_ms){
1074                         next_timeout_ms = timeout_delta_ms;
1075                     }
1076                     break;
1077                 default:
1078                     break;
1079             }
1080         }
1081     }
1082 
1083     // remove current timer if active
1084     if (ts == NULL){
1085         btstack_run_loop_remove_timer(&mesh_access_publication_timer);
1086 
1087     }
1088 
1089     // new timeout?
1090     if (next_timeout_ms == 0) return;
1091 
1092     // set timer
1093     btstack_run_loop_set_timer(&mesh_access_publication_timer, next_timeout_ms);
1094     btstack_run_loop_set_timer_handler(&mesh_access_publication_timer, mesh_model_publication_run);
1095     btstack_run_loop_add_timer(&mesh_access_publication_timer);
1096 }
1097 
1098 void mesh_model_publication_start(mesh_model_t * mesh_model){
1099     mesh_publication_model_t * publication_model = mesh_model->publication_model;
1100     if (publication_model == NULL) return;
1101 
1102     // publish right away
1103     publication_model->state = MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY;
1104     mesh_model_publication_run(NULL);
1105 }
1106 
1107 void mesh_model_publication_stop(mesh_model_t * mesh_model){
1108     mesh_publication_model_t * publication_model = mesh_model->publication_model;
1109     if (publication_model == NULL) return;
1110 
1111     // reset state
1112     publication_model->state = MESH_MODEL_PUBLICATION_STATE_IDLE;
1113 }
1114 
1115 void mesh_access_state_changed(mesh_model_t * mesh_model){
1116     mesh_publication_model_t * publication_model = mesh_model->publication_model;
1117     if (publication_model == NULL) return;
1118     publication_model->state = MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY;
1119     mesh_model_publication_run(NULL);
1120 }
1121 
1122