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
232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald * GMBH 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
382d4000d1SMatthias Ringwald #define BTSTACK_FILE__ "mesh_node.c"
39683cf298SMatthias Ringwald
400d42a059SMatthias Ringwald #include "bluetooth_company_id.h"
410d42a059SMatthias Ringwald #include "mesh/mesh_foundation.h"
420d42a059SMatthias Ringwald
43f4854a5eSMatthias Ringwald #include "mesh/mesh_node.h"
44f4854a5eSMatthias Ringwald
45*b8fd168eSMatthias Ringwald #include "btstack_util.h"
46*b8fd168eSMatthias Ringwald
47e8625ff1SMatthias Ringwald #include <stddef.h>
48d0e44c14SMatthias Ringwald #include <string.h>
49e8625ff1SMatthias Ringwald
50683cf298SMatthias Ringwald static uint16_t primary_element_address;
51683cf298SMatthias Ringwald
52e8625ff1SMatthias Ringwald static mesh_element_t primary_element;
53e8625ff1SMatthias Ringwald
54e8625ff1SMatthias Ringwald static uint16_t mesh_element_index_next;
55e8625ff1SMatthias Ringwald
56e8625ff1SMatthias Ringwald static btstack_linked_list_t mesh_elements;
57e8625ff1SMatthias Ringwald
580d42a059SMatthias Ringwald static uint16_t mid_counter;
590d42a059SMatthias Ringwald
60d0e44c14SMatthias Ringwald static uint8_t mesh_node_device_uuid[16];
6139cd8755SMatthias Ringwald static int mesh_node_have_device_uuid;
62d0e44c14SMatthias Ringwald
635d71beb8SMatthias Ringwald static uint16_t mesh_node_company_id;
645d71beb8SMatthias Ringwald static uint16_t mesh_node_product_id;
655d71beb8SMatthias Ringwald static uint16_t mesh_node_product_version_id;
665d71beb8SMatthias Ringwald
mesh_node_primary_element_address_set(uint16_t unicast_address)67683cf298SMatthias Ringwald void mesh_node_primary_element_address_set(uint16_t unicast_address){
68683cf298SMatthias Ringwald primary_element_address = unicast_address;
69683cf298SMatthias Ringwald }
70683cf298SMatthias Ringwald
mesh_node_get_primary_element_address(void)71001c65e0SMatthias Ringwald uint16_t mesh_node_get_primary_element_address(void){
72683cf298SMatthias Ringwald return primary_element_address;
73683cf298SMatthias Ringwald }
74e8625ff1SMatthias Ringwald
mesh_node_init(void)75e8625ff1SMatthias Ringwald void mesh_node_init(void){
76e8625ff1SMatthias Ringwald // dd Primary Element to list of elements
77001c65e0SMatthias Ringwald mesh_node_add_element(&primary_element);
78e8625ff1SMatthias Ringwald }
79e8625ff1SMatthias Ringwald
mesh_node_set_info(uint16_t company_id,uint16_t product_id,uint16_t product_version_id)805d71beb8SMatthias Ringwald void mesh_node_set_info(uint16_t company_id, uint16_t product_id, uint16_t product_version_id){
815d71beb8SMatthias Ringwald mesh_node_company_id = company_id;
825d71beb8SMatthias Ringwald mesh_node_product_id = product_id;
835d71beb8SMatthias Ringwald mesh_node_product_version_id = product_version_id;
845d71beb8SMatthias Ringwald }
855d71beb8SMatthias Ringwald
mesh_node_get_company_id(void)865d71beb8SMatthias Ringwald uint16_t mesh_node_get_company_id(void){
875d71beb8SMatthias Ringwald return mesh_node_company_id;
885d71beb8SMatthias Ringwald }
895d71beb8SMatthias Ringwald
mesh_node_get_product_id(void)905d71beb8SMatthias Ringwald uint16_t mesh_node_get_product_id(void){
915d71beb8SMatthias Ringwald return mesh_node_product_id;
925d71beb8SMatthias Ringwald }
935d71beb8SMatthias Ringwald
mesh_node_get_product_version_id(void)945d71beb8SMatthias Ringwald uint16_t mesh_node_get_product_version_id(void){
955d71beb8SMatthias Ringwald return mesh_node_product_version_id;
965d71beb8SMatthias Ringwald }
975d71beb8SMatthias Ringwald
mesh_node_add_element(mesh_element_t * element)98001c65e0SMatthias Ringwald void mesh_node_add_element(mesh_element_t * element){
99e8625ff1SMatthias Ringwald element->element_index = mesh_element_index_next++;
100e8625ff1SMatthias Ringwald btstack_linked_list_add_tail(&mesh_elements, (void*) element);
101e8625ff1SMatthias Ringwald }
102e8625ff1SMatthias Ringwald
mesh_node_element_count(void)103001c65e0SMatthias Ringwald uint16_t mesh_node_element_count(void){
104e4058622SMatthias Ringwald return (uint16_t) btstack_linked_list_count(&mesh_elements);
105e4058622SMatthias Ringwald }
106e4058622SMatthias Ringwald
mesh_node_get_primary_element(void)107001c65e0SMatthias Ringwald mesh_element_t * mesh_node_get_primary_element(void){
108e8625ff1SMatthias Ringwald return &primary_element;
109e8625ff1SMatthias Ringwald }
110e8625ff1SMatthias Ringwald
1116f175e03SMatthias Ringwald
mesh_node_set_element_location(mesh_element_t * element,uint16_t location)1126f175e03SMatthias Ringwald void mesh_node_set_element_location(mesh_element_t * element, uint16_t location){
1136f175e03SMatthias Ringwald element->loc = location;
1146f175e03SMatthias Ringwald }
1156f175e03SMatthias Ringwald
mesh_node_set_primary_element_location(uint16_t location)116001c65e0SMatthias Ringwald void mesh_node_set_primary_element_location(uint16_t location){
1176f175e03SMatthias Ringwald mesh_node_set_element_location(&primary_element, location);
118e8625ff1SMatthias Ringwald }
119e8625ff1SMatthias Ringwald
mesh_node_element_for_index(uint16_t element_index)120001c65e0SMatthias Ringwald mesh_element_t * mesh_node_element_for_index(uint16_t element_index){
121e8625ff1SMatthias Ringwald btstack_linked_list_iterator_t it;
122e8625ff1SMatthias Ringwald btstack_linked_list_iterator_init(&it, &mesh_elements);
123e8625ff1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
124e8625ff1SMatthias Ringwald mesh_element_t * element = (mesh_element_t *) btstack_linked_list_iterator_next(&it);
125e8625ff1SMatthias Ringwald if (element->element_index != element_index) continue;
126e8625ff1SMatthias Ringwald return element;
127e8625ff1SMatthias Ringwald }
128e8625ff1SMatthias Ringwald return NULL;
129e8625ff1SMatthias Ringwald }
130e8625ff1SMatthias Ringwald
mesh_node_element_for_unicast_address(uint16_t unicast_address)131001c65e0SMatthias Ringwald mesh_element_t * mesh_node_element_for_unicast_address(uint16_t unicast_address){
132001c65e0SMatthias Ringwald uint16_t element_index = unicast_address - mesh_node_get_primary_element_address();
133001c65e0SMatthias Ringwald return mesh_node_element_for_index(element_index);
134e8625ff1SMatthias Ringwald }
135e8625ff1SMatthias Ringwald
mesh_element_iterator_init(mesh_element_iterator_t * iterator)136e8625ff1SMatthias Ringwald void mesh_element_iterator_init(mesh_element_iterator_t * iterator){
137e8625ff1SMatthias Ringwald btstack_linked_list_iterator_init(&iterator->it, &mesh_elements);
138e8625ff1SMatthias Ringwald }
139e8625ff1SMatthias Ringwald
mesh_element_iterator_has_next(mesh_element_iterator_t * iterator)140e8625ff1SMatthias Ringwald int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator){
141e8625ff1SMatthias Ringwald return btstack_linked_list_iterator_has_next(&iterator->it);
142e8625ff1SMatthias Ringwald }
143e8625ff1SMatthias Ringwald
mesh_element_iterator_next(mesh_element_iterator_t * iterator)144e8625ff1SMatthias Ringwald mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator){
145e8625ff1SMatthias Ringwald return (mesh_element_t *) btstack_linked_list_iterator_next(&iterator->it);
146e8625ff1SMatthias Ringwald }
147d0e44c14SMatthias Ringwald
1480d42a059SMatthias Ringwald // Mesh Node Element functions
mesh_access_get_element_index(mesh_model_t * mesh_model)1490d42a059SMatthias Ringwald uint8_t mesh_access_get_element_index(mesh_model_t * mesh_model){
1500d42a059SMatthias Ringwald return mesh_model->element->element_index;
1510d42a059SMatthias Ringwald }
1520d42a059SMatthias Ringwald
mesh_access_get_element_address(mesh_model_t * mesh_model)1530d42a059SMatthias Ringwald uint16_t mesh_access_get_element_address(mesh_model_t * mesh_model){
1540d42a059SMatthias Ringwald return mesh_node_get_primary_element_address() + mesh_model->element->element_index;
1550d42a059SMatthias Ringwald }
1560d42a059SMatthias Ringwald
1570d42a059SMatthias Ringwald // Model Identifier utilities
1580d42a059SMatthias Ringwald
mesh_model_get_model_identifier(uint16_t vendor_id,uint16_t model_id)1590d42a059SMatthias Ringwald uint32_t mesh_model_get_model_identifier(uint16_t vendor_id, uint16_t model_id){
160df71a9a4SMatthias Ringwald return (((uint32_t) vendor_id << 16)) | model_id;
1610d42a059SMatthias Ringwald }
1620d42a059SMatthias Ringwald
mesh_model_get_model_identifier_bluetooth_sig(uint16_t model_id)1630d42a059SMatthias Ringwald uint32_t mesh_model_get_model_identifier_bluetooth_sig(uint16_t model_id){
164df71a9a4SMatthias Ringwald return ((uint32_t) BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC << 16) | model_id;
1650d42a059SMatthias Ringwald }
1660d42a059SMatthias Ringwald
mesh_model_get_model_id(uint32_t model_identifier)1670d42a059SMatthias Ringwald uint16_t mesh_model_get_model_id(uint32_t model_identifier){
1680d42a059SMatthias Ringwald return model_identifier & 0xFFFFu;
1690d42a059SMatthias Ringwald }
1700d42a059SMatthias Ringwald
mesh_model_get_vendor_id(uint32_t model_identifier)1710d42a059SMatthias Ringwald uint16_t mesh_model_get_vendor_id(uint32_t model_identifier){
1720d42a059SMatthias Ringwald return model_identifier >> 16;
1730d42a059SMatthias Ringwald }
1740d42a059SMatthias Ringwald
mesh_model_is_bluetooth_sig(uint32_t model_identifier)1750d42a059SMatthias Ringwald int mesh_model_is_bluetooth_sig(uint32_t model_identifier){
1760d42a059SMatthias Ringwald return mesh_model_get_vendor_id(model_identifier) == BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC;
1770d42a059SMatthias Ringwald }
1780d42a059SMatthias Ringwald
mesh_node_get_configuration_server(void)1796f8febaeSMatthias Ringwald mesh_model_t * mesh_node_get_configuration_server(void){
1800d42a059SMatthias Ringwald return mesh_model_get_by_identifier(mesh_node_get_primary_element(), mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_CONFIGURATION_SERVER));
1810d42a059SMatthias Ringwald }
1820d42a059SMatthias Ringwald
mesh_node_get_health_server(void)1836f8febaeSMatthias Ringwald mesh_model_t * mesh_node_get_health_server(void){
1846f8febaeSMatthias Ringwald return mesh_model_get_by_identifier(mesh_node_get_primary_element(), mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_HEALTH_SERVER));
1856f8febaeSMatthias Ringwald }
1866f8febaeSMatthias Ringwald
mesh_model_reset_appkeys(mesh_model_t * mesh_model)1870d42a059SMatthias Ringwald void mesh_model_reset_appkeys(mesh_model_t * mesh_model){
1880d42a059SMatthias Ringwald uint16_t i;
1890d42a059SMatthias Ringwald for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){
1900d42a059SMatthias Ringwald mesh_model->appkey_indices[i] = MESH_APPKEY_INVALID;
1910d42a059SMatthias Ringwald }
1920d42a059SMatthias Ringwald }
1930d42a059SMatthias Ringwald
mesh_element_add_model(mesh_element_t * element,mesh_model_t * mesh_model)1940d42a059SMatthias Ringwald void mesh_element_add_model(mesh_element_t * element, mesh_model_t * mesh_model){
1950d42a059SMatthias Ringwald // reset app keys
1960d42a059SMatthias Ringwald mesh_model_reset_appkeys(mesh_model);
1970d42a059SMatthias Ringwald
1980d42a059SMatthias Ringwald if (mesh_model_is_bluetooth_sig(mesh_model->model_identifier)){
1990d42a059SMatthias Ringwald element->models_count_sig++;
2000d42a059SMatthias Ringwald } else {
2010d42a059SMatthias Ringwald element->models_count_vendor++;
2020d42a059SMatthias Ringwald }
2030d42a059SMatthias Ringwald mesh_model->mid = mid_counter++;
2040d42a059SMatthias Ringwald mesh_model->element = element;
2050d42a059SMatthias Ringwald btstack_linked_list_add_tail(&element->models, (btstack_linked_item_t *) mesh_model);
2060d42a059SMatthias Ringwald }
2070d42a059SMatthias Ringwald
mesh_model_iterator_init(mesh_model_iterator_t * iterator,mesh_element_t * element)2080d42a059SMatthias Ringwald void mesh_model_iterator_init(mesh_model_iterator_t * iterator, mesh_element_t * element){
2090d42a059SMatthias Ringwald btstack_linked_list_iterator_init(&iterator->it, &element->models);
2100d42a059SMatthias Ringwald }
2110d42a059SMatthias Ringwald
mesh_model_iterator_has_next(mesh_model_iterator_t * iterator)2120d42a059SMatthias Ringwald int mesh_model_iterator_has_next(mesh_model_iterator_t * iterator){
2130d42a059SMatthias Ringwald return btstack_linked_list_iterator_has_next(&iterator->it);
2140d42a059SMatthias Ringwald }
2150d42a059SMatthias Ringwald
mesh_model_iterator_next(mesh_model_iterator_t * iterator)2160d42a059SMatthias Ringwald mesh_model_t * mesh_model_iterator_next(mesh_model_iterator_t * iterator){
2170d42a059SMatthias Ringwald return (mesh_model_t *) btstack_linked_list_iterator_next(&iterator->it);
2180d42a059SMatthias Ringwald }
2190d42a059SMatthias Ringwald
mesh_model_get_by_identifier(mesh_element_t * element,uint32_t model_identifier)2200d42a059SMatthias Ringwald mesh_model_t * mesh_model_get_by_identifier(mesh_element_t * element, uint32_t model_identifier){
2210d42a059SMatthias Ringwald mesh_model_iterator_t it;
2220d42a059SMatthias Ringwald mesh_model_iterator_init(&it, element);
2230d42a059SMatthias Ringwald while (mesh_model_iterator_has_next(&it)){
2240d42a059SMatthias Ringwald mesh_model_t * model = mesh_model_iterator_next(&it);
2250d42a059SMatthias Ringwald if (model->model_identifier != model_identifier) continue;
2260d42a059SMatthias Ringwald return model;
2270d42a059SMatthias Ringwald }
2280d42a059SMatthias Ringwald return NULL;
2290d42a059SMatthias Ringwald }
2300d42a059SMatthias Ringwald
mesh_access_model_for_address_and_model_identifier(uint16_t element_address,uint32_t model_identifier,uint8_t * status)2310d42a059SMatthias Ringwald mesh_model_t * mesh_access_model_for_address_and_model_identifier(uint16_t element_address, uint32_t model_identifier, uint8_t * status){
2320d42a059SMatthias Ringwald mesh_element_t * element = mesh_node_element_for_unicast_address(element_address);
2330d42a059SMatthias Ringwald if (element == NULL){
2340d42a059SMatthias Ringwald *status = MESH_FOUNDATION_STATUS_INVALID_ADDRESS;
2350d42a059SMatthias Ringwald return NULL;
2360d42a059SMatthias Ringwald }
2370d42a059SMatthias Ringwald mesh_model_t * model = mesh_model_get_by_identifier(element, model_identifier);
2380d42a059SMatthias Ringwald if (model == NULL) {
2390d42a059SMatthias Ringwald *status = MESH_FOUNDATION_STATUS_INVALID_MODEL;
2400d42a059SMatthias Ringwald } else {
2410d42a059SMatthias Ringwald *status = MESH_FOUNDATION_STATUS_SUCCESS;
2420d42a059SMatthias Ringwald }
2430d42a059SMatthias Ringwald return model;
2440d42a059SMatthias Ringwald }
2450d42a059SMatthias Ringwald // Mesh Model Subscription
mesh_model_contains_subscription(mesh_model_t * mesh_model,uint16_t address)2460d42a059SMatthias Ringwald int mesh_model_contains_subscription(mesh_model_t * mesh_model, uint16_t address){
2470d42a059SMatthias Ringwald uint16_t i;
2480d42a059SMatthias Ringwald for (i=0;i<MAX_NR_MESH_SUBSCRIPTION_PER_MODEL;i++){
2490d42a059SMatthias Ringwald if (mesh_model->subscriptions[i] == address) return 1;
2500d42a059SMatthias Ringwald }
2510d42a059SMatthias Ringwald return 0;
2520d42a059SMatthias Ringwald }
2530d42a059SMatthias Ringwald
mesh_node_set_device_uuid(const uint8_t * device_uuid)254d0e44c14SMatthias Ringwald void mesh_node_set_device_uuid(const uint8_t * device_uuid){
2556535961aSMatthias Ringwald (void)memcpy(mesh_node_device_uuid, device_uuid, 16);
25639cd8755SMatthias Ringwald mesh_node_have_device_uuid = 1;
257d0e44c14SMatthias Ringwald }
258d0e44c14SMatthias Ringwald
259d0e44c14SMatthias Ringwald /**
260d0e44c14SMatthias Ringwald * @brief Get Device UUID
261d0e44c14SMatthias Ringwald */
mesh_node_get_device_uuid(void)262d0e44c14SMatthias Ringwald const uint8_t * mesh_node_get_device_uuid(void){
26339cd8755SMatthias Ringwald if (mesh_node_have_device_uuid == 0) return NULL;
26454274a76SMatthias Ringwald return mesh_node_device_uuid;
265d0e44c14SMatthias Ringwald }
266d0e44c14SMatthias Ringwald
26712d66e05SMilanka Ringwald
26812d66e05SMilanka Ringwald // Heartbeat (helper)
mesh_heartbeat_pwr2(uint8_t value)26912d66e05SMilanka Ringwald uint16_t mesh_heartbeat_pwr2(uint8_t value){
27012d66e05SMilanka Ringwald if (value == 0 ) return 0x0000;
27112d66e05SMilanka Ringwald if (value == 0xff || value == 0x11) return 0xffff;
27212d66e05SMilanka Ringwald return 1 << (value-1);
27312d66e05SMilanka Ringwald }
27412d66e05SMilanka Ringwald
mesh_heartbeat_count_log(uint16_t value)27512d66e05SMilanka Ringwald uint8_t mesh_heartbeat_count_log(uint16_t value){
27612d66e05SMilanka Ringwald if (value == 0) return 0x00;
27712d66e05SMilanka Ringwald if (value == 0xffff) return 0xff;
27812d66e05SMilanka Ringwald // count leading zeros, supported by clang and gcc
27912d66e05SMilanka Ringwald // note: CountLog(8) == CountLog(7) = 3
280*b8fd168eSMatthias Ringwald return 33 - btstack_clz(value - 1);
28112d66e05SMilanka Ringwald }
28212d66e05SMilanka Ringwald
mesh_heartbeat_period_log(uint16_t value)28312d66e05SMilanka Ringwald uint8_t mesh_heartbeat_period_log(uint16_t value){
28412d66e05SMilanka Ringwald if (value == 0) return 0x00;
28512d66e05SMilanka Ringwald // count leading zeros, supported by clang and gcc
28612d66e05SMilanka Ringwald // note: PeriodLog(8) == PeriodLog(7) = 3
287*b8fd168eSMatthias Ringwald return 33 - btstack_clz(value - 1);
28812d66e05SMilanka Ringwald }
289