xref: /btstack/src/mesh/mesh_node.c (revision 54274a7650d548554d6c7b7f1611dd0011741d6f)
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>
41d0e44c14SMatthias Ringwald #include <string.h>
42e8625ff1SMatthias Ringwald 
43683cf298SMatthias Ringwald #include "mesh/mesh_node.h"
44683cf298SMatthias 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];
54d0e44c14SMatthias Ringwald 
55683cf298SMatthias Ringwald void mesh_node_primary_element_address_set(uint16_t unicast_address){
56683cf298SMatthias Ringwald     primary_element_address = unicast_address;
57683cf298SMatthias Ringwald }
58683cf298SMatthias Ringwald 
59001c65e0SMatthias Ringwald uint16_t mesh_node_get_primary_element_address(void){
60683cf298SMatthias Ringwald     return primary_element_address;
61683cf298SMatthias Ringwald }
62e8625ff1SMatthias Ringwald 
63e8625ff1SMatthias Ringwald void mesh_node_init(void){
64e8625ff1SMatthias Ringwald     // dd Primary Element to list of elements
65001c65e0SMatthias Ringwald     mesh_node_add_element(&primary_element);
66e8625ff1SMatthias Ringwald }
67e8625ff1SMatthias Ringwald 
68001c65e0SMatthias Ringwald void mesh_node_add_element(mesh_element_t * element){
69e8625ff1SMatthias Ringwald     element->element_index = mesh_element_index_next++;
70e8625ff1SMatthias Ringwald     btstack_linked_list_add_tail(&mesh_elements, (void*) element);
71e8625ff1SMatthias Ringwald }
72e8625ff1SMatthias Ringwald 
73001c65e0SMatthias Ringwald uint16_t mesh_node_element_count(void){
74e4058622SMatthias Ringwald 	return (uint16_t) btstack_linked_list_count(&mesh_elements);
75e4058622SMatthias Ringwald }
76e4058622SMatthias Ringwald 
77001c65e0SMatthias Ringwald mesh_element_t * mesh_node_get_primary_element(void){
78e8625ff1SMatthias Ringwald     return &primary_element;
79e8625ff1SMatthias Ringwald }
80e8625ff1SMatthias Ringwald 
81001c65e0SMatthias Ringwald void mesh_node_set_primary_element_location(uint16_t location){
82e8625ff1SMatthias Ringwald     primary_element.loc = location;
83e8625ff1SMatthias Ringwald }
84e8625ff1SMatthias Ringwald 
85001c65e0SMatthias Ringwald mesh_element_t * mesh_node_element_for_index(uint16_t element_index){
86e8625ff1SMatthias Ringwald     btstack_linked_list_iterator_t it;
87e8625ff1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &mesh_elements);
88e8625ff1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
89e8625ff1SMatthias Ringwald         mesh_element_t * element = (mesh_element_t *) btstack_linked_list_iterator_next(&it);
90e8625ff1SMatthias Ringwald         if (element->element_index != element_index) continue;
91e8625ff1SMatthias Ringwald         return element;
92e8625ff1SMatthias Ringwald     }
93e8625ff1SMatthias Ringwald     return NULL;
94e8625ff1SMatthias Ringwald }
95e8625ff1SMatthias Ringwald 
96001c65e0SMatthias Ringwald mesh_element_t * mesh_node_element_for_unicast_address(uint16_t unicast_address){
97001c65e0SMatthias Ringwald     uint16_t element_index = unicast_address - mesh_node_get_primary_element_address();
98001c65e0SMatthias Ringwald     return mesh_node_element_for_index(element_index);
99e8625ff1SMatthias Ringwald }
100e8625ff1SMatthias Ringwald 
101e8625ff1SMatthias Ringwald void mesh_element_iterator_init(mesh_element_iterator_t * iterator){
102e8625ff1SMatthias Ringwald     btstack_linked_list_iterator_init(&iterator->it, &mesh_elements);
103e8625ff1SMatthias Ringwald }
104e8625ff1SMatthias Ringwald 
105e8625ff1SMatthias Ringwald int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator){
106e8625ff1SMatthias Ringwald     return btstack_linked_list_iterator_has_next(&iterator->it);
107e8625ff1SMatthias Ringwald }
108e8625ff1SMatthias Ringwald 
109e8625ff1SMatthias Ringwald mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator){
110e8625ff1SMatthias Ringwald     return (mesh_element_t *) btstack_linked_list_iterator_next(&iterator->it);
111e8625ff1SMatthias Ringwald }
112d0e44c14SMatthias Ringwald 
113d0e44c14SMatthias Ringwald void mesh_node_set_device_uuid(const uint8_t * device_uuid){
114d0e44c14SMatthias Ringwald     memcpy(mesh_node_get_device_uuid, device_uuid, 16);
115d0e44c14SMatthias Ringwald }
116d0e44c14SMatthias Ringwald 
117d0e44c14SMatthias Ringwald /**
118d0e44c14SMatthias Ringwald  * @brief Get Device UUID
119d0e44c14SMatthias Ringwald  */
120d0e44c14SMatthias Ringwald const uint8_t * mesh_node_get_device_uuid(void){
121*54274a76SMatthias Ringwald     return mesh_node_device_uuid;
122d0e44c14SMatthias Ringwald }
123d0e44c14SMatthias Ringwald 
124