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 40*f4854a5eSMatthias Ringwald #include "mesh/mesh_node.h" 41*f4854a5eSMatthias Ringwald 42e8625ff1SMatthias Ringwald #include <stddef.h> 43d0e44c14SMatthias Ringwald #include <string.h> 44e8625ff1SMatthias Ringwald 45683cf298SMatthias Ringwald static uint16_t primary_element_address; 46683cf298SMatthias Ringwald 47e8625ff1SMatthias Ringwald static mesh_element_t primary_element; 48e8625ff1SMatthias Ringwald 49e8625ff1SMatthias Ringwald static uint16_t mesh_element_index_next; 50e8625ff1SMatthias Ringwald 51e8625ff1SMatthias Ringwald static btstack_linked_list_t mesh_elements; 52e8625ff1SMatthias Ringwald 53d0e44c14SMatthias Ringwald static uint8_t mesh_node_device_uuid[16]; 5439cd8755SMatthias Ringwald static int mesh_node_have_device_uuid; 55d0e44c14SMatthias Ringwald 56683cf298SMatthias Ringwald void mesh_node_primary_element_address_set(uint16_t unicast_address){ 57683cf298SMatthias Ringwald primary_element_address = unicast_address; 58683cf298SMatthias Ringwald } 59683cf298SMatthias Ringwald 60001c65e0SMatthias Ringwald uint16_t mesh_node_get_primary_element_address(void){ 61683cf298SMatthias Ringwald return primary_element_address; 62683cf298SMatthias Ringwald } 63e8625ff1SMatthias Ringwald 64e8625ff1SMatthias Ringwald void mesh_node_init(void){ 65e8625ff1SMatthias Ringwald // dd Primary Element to list of elements 66001c65e0SMatthias Ringwald mesh_node_add_element(&primary_element); 67e8625ff1SMatthias Ringwald } 68e8625ff1SMatthias Ringwald 69001c65e0SMatthias Ringwald void mesh_node_add_element(mesh_element_t * element){ 70e8625ff1SMatthias Ringwald element->element_index = mesh_element_index_next++; 71e8625ff1SMatthias Ringwald btstack_linked_list_add_tail(&mesh_elements, (void*) element); 72e8625ff1SMatthias Ringwald } 73e8625ff1SMatthias Ringwald 74001c65e0SMatthias Ringwald uint16_t mesh_node_element_count(void){ 75e4058622SMatthias Ringwald return (uint16_t) btstack_linked_list_count(&mesh_elements); 76e4058622SMatthias Ringwald } 77e4058622SMatthias Ringwald 78001c65e0SMatthias Ringwald mesh_element_t * mesh_node_get_primary_element(void){ 79e8625ff1SMatthias Ringwald return &primary_element; 80e8625ff1SMatthias Ringwald } 81e8625ff1SMatthias Ringwald 826f175e03SMatthias Ringwald 836f175e03SMatthias Ringwald void mesh_node_set_element_location(mesh_element_t * element, uint16_t location){ 846f175e03SMatthias Ringwald element->loc = location; 856f175e03SMatthias Ringwald } 866f175e03SMatthias Ringwald 87001c65e0SMatthias Ringwald void mesh_node_set_primary_element_location(uint16_t location){ 886f175e03SMatthias Ringwald mesh_node_set_element_location(&primary_element, location); 89e8625ff1SMatthias Ringwald } 90e8625ff1SMatthias Ringwald 91001c65e0SMatthias Ringwald mesh_element_t * mesh_node_element_for_index(uint16_t element_index){ 92e8625ff1SMatthias Ringwald btstack_linked_list_iterator_t it; 93e8625ff1SMatthias Ringwald btstack_linked_list_iterator_init(&it, &mesh_elements); 94e8625ff1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 95e8625ff1SMatthias Ringwald mesh_element_t * element = (mesh_element_t *) btstack_linked_list_iterator_next(&it); 96e8625ff1SMatthias Ringwald if (element->element_index != element_index) continue; 97e8625ff1SMatthias Ringwald return element; 98e8625ff1SMatthias Ringwald } 99e8625ff1SMatthias Ringwald return NULL; 100e8625ff1SMatthias Ringwald } 101e8625ff1SMatthias Ringwald 102001c65e0SMatthias Ringwald mesh_element_t * mesh_node_element_for_unicast_address(uint16_t unicast_address){ 103001c65e0SMatthias Ringwald uint16_t element_index = unicast_address - mesh_node_get_primary_element_address(); 104001c65e0SMatthias Ringwald return mesh_node_element_for_index(element_index); 105e8625ff1SMatthias Ringwald } 106e8625ff1SMatthias Ringwald 107e8625ff1SMatthias Ringwald void mesh_element_iterator_init(mesh_element_iterator_t * iterator){ 108e8625ff1SMatthias Ringwald btstack_linked_list_iterator_init(&iterator->it, &mesh_elements); 109e8625ff1SMatthias Ringwald } 110e8625ff1SMatthias Ringwald 111e8625ff1SMatthias Ringwald int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator){ 112e8625ff1SMatthias Ringwald return btstack_linked_list_iterator_has_next(&iterator->it); 113e8625ff1SMatthias Ringwald } 114e8625ff1SMatthias Ringwald 115e8625ff1SMatthias Ringwald mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator){ 116e8625ff1SMatthias Ringwald return (mesh_element_t *) btstack_linked_list_iterator_next(&iterator->it); 117e8625ff1SMatthias Ringwald } 118d0e44c14SMatthias Ringwald 119d0e44c14SMatthias Ringwald void mesh_node_set_device_uuid(const uint8_t * device_uuid){ 12058a4c398SMatthias Ringwald memcpy(mesh_node_device_uuid, device_uuid, 16); 12139cd8755SMatthias Ringwald mesh_node_have_device_uuid = 1; 122d0e44c14SMatthias Ringwald } 123d0e44c14SMatthias Ringwald 124d0e44c14SMatthias Ringwald /** 125d0e44c14SMatthias Ringwald * @brief Get Device UUID 126d0e44c14SMatthias Ringwald */ 127d0e44c14SMatthias Ringwald const uint8_t * mesh_node_get_device_uuid(void){ 12839cd8755SMatthias Ringwald if (mesh_node_have_device_uuid == 0) return NULL; 12954274a76SMatthias Ringwald return mesh_node_device_uuid; 130d0e44c14SMatthias Ringwald } 131d0e44c14SMatthias Ringwald 132