xref: /btstack/src/mesh/mesh_node.c (revision e8625ff1d444cb256f69fa3459ecedf340d2bb16)
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*e8625ff1SMatthias Ringwald #include <stddef.h>
41*e8625ff1SMatthias Ringwald 
42683cf298SMatthias Ringwald #include "mesh/mesh_node.h"
43683cf298SMatthias Ringwald 
44683cf298SMatthias Ringwald static uint16_t primary_element_address;
45683cf298SMatthias Ringwald 
46*e8625ff1SMatthias Ringwald static mesh_element_t primary_element;
47*e8625ff1SMatthias Ringwald 
48*e8625ff1SMatthias Ringwald static uint16_t mesh_element_index_next;
49*e8625ff1SMatthias Ringwald 
50*e8625ff1SMatthias Ringwald static btstack_linked_list_t mesh_elements;
51*e8625ff1SMatthias Ringwald 
52683cf298SMatthias Ringwald void mesh_node_primary_element_address_set(uint16_t unicast_address){
53683cf298SMatthias Ringwald     primary_element_address = unicast_address;
54683cf298SMatthias Ringwald }
55683cf298SMatthias Ringwald 
56683cf298SMatthias Ringwald uint16_t mesh_node_primary_element_address_get(void){
57683cf298SMatthias Ringwald     return primary_element_address;
58683cf298SMatthias Ringwald }
59*e8625ff1SMatthias Ringwald 
60*e8625ff1SMatthias Ringwald void mesh_node_init(void){
61*e8625ff1SMatthias Ringwald     // dd Primary Element to list of elements
62*e8625ff1SMatthias Ringwald     mesh_element_add(&primary_element);
63*e8625ff1SMatthias Ringwald }
64*e8625ff1SMatthias Ringwald 
65*e8625ff1SMatthias Ringwald void mesh_element_add(mesh_element_t * element){
66*e8625ff1SMatthias Ringwald     element->element_index = mesh_element_index_next++;
67*e8625ff1SMatthias Ringwald     btstack_linked_list_add_tail(&mesh_elements, (void*) element);
68*e8625ff1SMatthias Ringwald }
69*e8625ff1SMatthias Ringwald 
70*e8625ff1SMatthias Ringwald mesh_element_t * mesh_primary_element(void){
71*e8625ff1SMatthias Ringwald     return &primary_element;
72*e8625ff1SMatthias Ringwald }
73*e8625ff1SMatthias Ringwald 
74*e8625ff1SMatthias Ringwald void mesh_access_set_primary_element_location(uint16_t location){
75*e8625ff1SMatthias Ringwald     primary_element.loc = location;
76*e8625ff1SMatthias Ringwald }
77*e8625ff1SMatthias Ringwald 
78*e8625ff1SMatthias Ringwald mesh_element_t * mesh_element_for_index(uint16_t element_index){
79*e8625ff1SMatthias Ringwald     btstack_linked_list_iterator_t it;
80*e8625ff1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &mesh_elements);
81*e8625ff1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
82*e8625ff1SMatthias Ringwald         mesh_element_t * element = (mesh_element_t *) btstack_linked_list_iterator_next(&it);
83*e8625ff1SMatthias Ringwald         if (element->element_index != element_index) continue;
84*e8625ff1SMatthias Ringwald         return element;
85*e8625ff1SMatthias Ringwald     }
86*e8625ff1SMatthias Ringwald     return NULL;
87*e8625ff1SMatthias Ringwald }
88*e8625ff1SMatthias Ringwald 
89*e8625ff1SMatthias Ringwald mesh_element_t * mesh_element_for_unicast_address(uint16_t unicast_address){
90*e8625ff1SMatthias Ringwald     uint16_t element_index = unicast_address - mesh_node_primary_element_address_get();
91*e8625ff1SMatthias Ringwald     return mesh_element_for_index(element_index);
92*e8625ff1SMatthias Ringwald }
93*e8625ff1SMatthias Ringwald 
94*e8625ff1SMatthias Ringwald void mesh_element_iterator_init(mesh_element_iterator_t * iterator){
95*e8625ff1SMatthias Ringwald     btstack_linked_list_iterator_init(&iterator->it, &mesh_elements);
96*e8625ff1SMatthias Ringwald }
97*e8625ff1SMatthias Ringwald 
98*e8625ff1SMatthias Ringwald int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator){
99*e8625ff1SMatthias Ringwald     return btstack_linked_list_iterator_has_next(&iterator->it);
100*e8625ff1SMatthias Ringwald }
101*e8625ff1SMatthias Ringwald 
102*e8625ff1SMatthias Ringwald mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator){
103*e8625ff1SMatthias Ringwald     return (mesh_element_t *) btstack_linked_list_iterator_next(&iterator->it);
104*e8625ff1SMatthias Ringwald }
105