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