1683cf298SMatthias Ringwald /* 2683cf298SMatthias Ringwald * Copyright (C) 2019 BlueKitchen GmbH 3683cf298SMatthias Ringwald * 4683cf298SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5683cf298SMatthias Ringwald * modification, are permitted provided that the following conditions 6683cf298SMatthias Ringwald * are met: 7683cf298SMatthias Ringwald * 8683cf298SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9683cf298SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10683cf298SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11683cf298SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12683cf298SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13683cf298SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14683cf298SMatthias Ringwald * contributors may be used to endorse or promote products derived 15683cf298SMatthias Ringwald * from this software without specific prior written permission. 16683cf298SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17683cf298SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18683cf298SMatthias Ringwald * monetary gain. 19683cf298SMatthias Ringwald * 20683cf298SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21683cf298SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22683cf298SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23683cf298SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24683cf298SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25683cf298SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26683cf298SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27683cf298SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28683cf298SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29683cf298SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30683cf298SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31683cf298SMatthias Ringwald * SUCH DAMAGE. 32683cf298SMatthias Ringwald * 33683cf298SMatthias Ringwald * Please inquire about commercial licensing options at 34683cf298SMatthias Ringwald * [email protected] 35683cf298SMatthias Ringwald * 36683cf298SMatthias Ringwald */ 37683cf298SMatthias Ringwald 38683cf298SMatthias Ringwald #define __BTSTACK_FILE__ "mesh_node.c" 39683cf298SMatthias Ringwald 40e8625ff1SMatthias Ringwald #include <stddef.h> 41e8625ff1SMatthias Ringwald 42683cf298SMatthias Ringwald #include "mesh/mesh_node.h" 43683cf298SMatthias Ringwald 44683cf298SMatthias Ringwald static uint16_t primary_element_address; 45683cf298SMatthias Ringwald 46e8625ff1SMatthias Ringwald static mesh_element_t primary_element; 47e8625ff1SMatthias Ringwald 48e8625ff1SMatthias Ringwald static uint16_t mesh_element_index_next; 49e8625ff1SMatthias Ringwald 50e8625ff1SMatthias Ringwald static btstack_linked_list_t mesh_elements; 51e8625ff1SMatthias Ringwald 52683cf298SMatthias Ringwald void mesh_node_primary_element_address_set(uint16_t unicast_address){ 53683cf298SMatthias Ringwald primary_element_address = unicast_address; 54683cf298SMatthias Ringwald } 55683cf298SMatthias Ringwald 56*001c65e0SMatthias Ringwald uint16_t mesh_node_get_primary_element_address(void){ 57683cf298SMatthias Ringwald return primary_element_address; 58683cf298SMatthias Ringwald } 59e8625ff1SMatthias Ringwald 60e8625ff1SMatthias Ringwald void mesh_node_init(void){ 61e8625ff1SMatthias Ringwald // dd Primary Element to list of elements 62*001c65e0SMatthias Ringwald mesh_node_add_element(&primary_element); 63e8625ff1SMatthias Ringwald } 64e8625ff1SMatthias Ringwald 65*001c65e0SMatthias Ringwald void mesh_node_add_element(mesh_element_t * element){ 66e8625ff1SMatthias Ringwald element->element_index = mesh_element_index_next++; 67e8625ff1SMatthias Ringwald btstack_linked_list_add_tail(&mesh_elements, (void*) element); 68e8625ff1SMatthias Ringwald } 69e8625ff1SMatthias Ringwald 70*001c65e0SMatthias Ringwald uint16_t mesh_node_element_count(void){ 71e4058622SMatthias Ringwald return (uint16_t) btstack_linked_list_count(&mesh_elements); 72e4058622SMatthias Ringwald } 73e4058622SMatthias Ringwald 74*001c65e0SMatthias Ringwald mesh_element_t * mesh_node_get_primary_element(void){ 75e8625ff1SMatthias Ringwald return &primary_element; 76e8625ff1SMatthias Ringwald } 77e8625ff1SMatthias Ringwald 78*001c65e0SMatthias Ringwald void mesh_node_set_primary_element_location(uint16_t location){ 79e8625ff1SMatthias Ringwald primary_element.loc = location; 80e8625ff1SMatthias Ringwald } 81e8625ff1SMatthias Ringwald 82*001c65e0SMatthias Ringwald mesh_element_t * mesh_node_element_for_index(uint16_t element_index){ 83e8625ff1SMatthias Ringwald btstack_linked_list_iterator_t it; 84e8625ff1SMatthias Ringwald btstack_linked_list_iterator_init(&it, &mesh_elements); 85e8625ff1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 86e8625ff1SMatthias Ringwald mesh_element_t * element = (mesh_element_t *) btstack_linked_list_iterator_next(&it); 87e8625ff1SMatthias Ringwald if (element->element_index != element_index) continue; 88e8625ff1SMatthias Ringwald return element; 89e8625ff1SMatthias Ringwald } 90e8625ff1SMatthias Ringwald return NULL; 91e8625ff1SMatthias Ringwald } 92e8625ff1SMatthias Ringwald 93*001c65e0SMatthias Ringwald mesh_element_t * mesh_node_element_for_unicast_address(uint16_t unicast_address){ 94*001c65e0SMatthias Ringwald uint16_t element_index = unicast_address - mesh_node_get_primary_element_address(); 95*001c65e0SMatthias Ringwald return mesh_node_element_for_index(element_index); 96e8625ff1SMatthias Ringwald } 97e8625ff1SMatthias Ringwald 98e8625ff1SMatthias Ringwald void mesh_element_iterator_init(mesh_element_iterator_t * iterator){ 99e8625ff1SMatthias Ringwald btstack_linked_list_iterator_init(&iterator->it, &mesh_elements); 100e8625ff1SMatthias Ringwald } 101e8625ff1SMatthias Ringwald 102e8625ff1SMatthias Ringwald int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator){ 103e8625ff1SMatthias Ringwald return btstack_linked_list_iterator_has_next(&iterator->it); 104e8625ff1SMatthias Ringwald } 105e8625ff1SMatthias Ringwald 106e8625ff1SMatthias Ringwald mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator){ 107e8625ff1SMatthias Ringwald return (mesh_element_t *) btstack_linked_list_iterator_next(&iterator->it); 108e8625ff1SMatthias Ringwald } 109