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_node.c" 39 40 #include <stddef.h> 41 42 #include "mesh/mesh_node.h" 43 44 static uint16_t primary_element_address; 45 46 static mesh_element_t primary_element; 47 48 static uint16_t mesh_element_index_next; 49 50 static btstack_linked_list_t mesh_elements; 51 52 void mesh_node_primary_element_address_set(uint16_t unicast_address){ 53 primary_element_address = unicast_address; 54 } 55 56 uint16_t mesh_node_get_primary_element_address(void){ 57 return primary_element_address; 58 } 59 60 void mesh_node_init(void){ 61 // dd Primary Element to list of elements 62 mesh_node_add_element(&primary_element); 63 } 64 65 void mesh_node_add_element(mesh_element_t * element){ 66 element->element_index = mesh_element_index_next++; 67 btstack_linked_list_add_tail(&mesh_elements, (void*) element); 68 } 69 70 uint16_t mesh_node_element_count(void){ 71 return (uint16_t) btstack_linked_list_count(&mesh_elements); 72 } 73 74 mesh_element_t * mesh_node_get_primary_element(void){ 75 return &primary_element; 76 } 77 78 void mesh_node_set_primary_element_location(uint16_t location){ 79 primary_element.loc = location; 80 } 81 82 mesh_element_t * mesh_node_element_for_index(uint16_t element_index){ 83 btstack_linked_list_iterator_t it; 84 btstack_linked_list_iterator_init(&it, &mesh_elements); 85 while (btstack_linked_list_iterator_has_next(&it)){ 86 mesh_element_t * element = (mesh_element_t *) btstack_linked_list_iterator_next(&it); 87 if (element->element_index != element_index) continue; 88 return element; 89 } 90 return NULL; 91 } 92 93 mesh_element_t * mesh_node_element_for_unicast_address(uint16_t unicast_address){ 94 uint16_t element_index = unicast_address - mesh_node_get_primary_element_address(); 95 return mesh_node_element_for_index(element_index); 96 } 97 98 void mesh_element_iterator_init(mesh_element_iterator_t * iterator){ 99 btstack_linked_list_iterator_init(&iterator->it, &mesh_elements); 100 } 101 102 int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator){ 103 return btstack_linked_list_iterator_has_next(&iterator->it); 104 } 105 106 mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator){ 107 return (mesh_element_t *) btstack_linked_list_iterator_next(&iterator->it); 108 } 109