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 #include <string.h> 42 43 #include "mesh/mesh_node.h" 44 45 static uint16_t primary_element_address; 46 47 static mesh_element_t primary_element; 48 49 static uint16_t mesh_element_index_next; 50 51 static btstack_linked_list_t mesh_elements; 52 53 static uint8_t mesh_node_device_uuid[16]; 54 55 void mesh_node_primary_element_address_set(uint16_t unicast_address){ 56 primary_element_address = unicast_address; 57 } 58 59 uint16_t mesh_node_get_primary_element_address(void){ 60 return primary_element_address; 61 } 62 63 void mesh_node_init(void){ 64 // dd Primary Element to list of elements 65 mesh_node_add_element(&primary_element); 66 } 67 68 void mesh_node_add_element(mesh_element_t * element){ 69 element->element_index = mesh_element_index_next++; 70 btstack_linked_list_add_tail(&mesh_elements, (void*) element); 71 } 72 73 uint16_t mesh_node_element_count(void){ 74 return (uint16_t) btstack_linked_list_count(&mesh_elements); 75 } 76 77 mesh_element_t * mesh_node_get_primary_element(void){ 78 return &primary_element; 79 } 80 81 void mesh_node_set_primary_element_location(uint16_t location){ 82 primary_element.loc = location; 83 } 84 85 mesh_element_t * mesh_node_element_for_index(uint16_t element_index){ 86 btstack_linked_list_iterator_t it; 87 btstack_linked_list_iterator_init(&it, &mesh_elements); 88 while (btstack_linked_list_iterator_has_next(&it)){ 89 mesh_element_t * element = (mesh_element_t *) btstack_linked_list_iterator_next(&it); 90 if (element->element_index != element_index) continue; 91 return element; 92 } 93 return NULL; 94 } 95 96 mesh_element_t * mesh_node_element_for_unicast_address(uint16_t unicast_address){ 97 uint16_t element_index = unicast_address - mesh_node_get_primary_element_address(); 98 return mesh_node_element_for_index(element_index); 99 } 100 101 void mesh_element_iterator_init(mesh_element_iterator_t * iterator){ 102 btstack_linked_list_iterator_init(&iterator->it, &mesh_elements); 103 } 104 105 int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator){ 106 return btstack_linked_list_iterator_has_next(&iterator->it); 107 } 108 109 mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator){ 110 return (mesh_element_t *) btstack_linked_list_iterator_next(&iterator->it); 111 } 112 113 void mesh_node_set_device_uuid(const uint8_t * device_uuid){ 114 memcpy(mesh_node_get_device_uuid, device_uuid, 16); 115 } 116 117 /** 118 * @brief Get Device UUID 119 */ 120 const uint8_t * mesh_node_get_device_uuid(void){ 121 return &mesh_node_get_device_uuid; 122 } 123 124