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 BLUEKITCHEN 24 * GMBH 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 "bluetooth_company_id.h" 41 #include "mesh/mesh_foundation.h" 42 43 #include "mesh/mesh_node.h" 44 45 #include "btstack_util.h" 46 47 #include <stddef.h> 48 #include <string.h> 49 50 static uint16_t primary_element_address; 51 52 static mesh_element_t primary_element; 53 54 static uint16_t mesh_element_index_next; 55 56 static btstack_linked_list_t mesh_elements; 57 58 static uint16_t mid_counter; 59 60 static uint8_t mesh_node_device_uuid[16]; 61 static int mesh_node_have_device_uuid; 62 63 static uint16_t mesh_node_company_id; 64 static uint16_t mesh_node_product_id; 65 static uint16_t mesh_node_product_version_id; 66 67 void mesh_node_primary_element_address_set(uint16_t unicast_address){ 68 primary_element_address = unicast_address; 69 } 70 71 uint16_t mesh_node_get_primary_element_address(void){ 72 return primary_element_address; 73 } 74 75 void mesh_node_init(void){ 76 // dd Primary Element to list of elements 77 mesh_node_add_element(&primary_element); 78 } 79 80 void mesh_node_set_info(uint16_t company_id, uint16_t product_id, uint16_t product_version_id){ 81 mesh_node_company_id = company_id; 82 mesh_node_product_id = product_id; 83 mesh_node_product_version_id = product_version_id; 84 } 85 86 uint16_t mesh_node_get_company_id(void){ 87 return mesh_node_company_id; 88 } 89 90 uint16_t mesh_node_get_product_id(void){ 91 return mesh_node_product_id; 92 } 93 94 uint16_t mesh_node_get_product_version_id(void){ 95 return mesh_node_product_version_id; 96 } 97 98 void mesh_node_add_element(mesh_element_t * element){ 99 element->element_index = mesh_element_index_next++; 100 btstack_linked_list_add_tail(&mesh_elements, (void*) element); 101 } 102 103 uint16_t mesh_node_element_count(void){ 104 return (uint16_t) btstack_linked_list_count(&mesh_elements); 105 } 106 107 mesh_element_t * mesh_node_get_primary_element(void){ 108 return &primary_element; 109 } 110 111 112 void mesh_node_set_element_location(mesh_element_t * element, uint16_t location){ 113 element->loc = location; 114 } 115 116 void mesh_node_set_primary_element_location(uint16_t location){ 117 mesh_node_set_element_location(&primary_element, location); 118 } 119 120 mesh_element_t * mesh_node_element_for_index(uint16_t element_index){ 121 btstack_linked_list_iterator_t it; 122 btstack_linked_list_iterator_init(&it, &mesh_elements); 123 while (btstack_linked_list_iterator_has_next(&it)){ 124 mesh_element_t * element = (mesh_element_t *) btstack_linked_list_iterator_next(&it); 125 if (element->element_index != element_index) continue; 126 return element; 127 } 128 return NULL; 129 } 130 131 mesh_element_t * mesh_node_element_for_unicast_address(uint16_t unicast_address){ 132 uint16_t element_index = unicast_address - mesh_node_get_primary_element_address(); 133 return mesh_node_element_for_index(element_index); 134 } 135 136 void mesh_element_iterator_init(mesh_element_iterator_t * iterator){ 137 btstack_linked_list_iterator_init(&iterator->it, &mesh_elements); 138 } 139 140 int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator){ 141 return btstack_linked_list_iterator_has_next(&iterator->it); 142 } 143 144 mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator){ 145 return (mesh_element_t *) btstack_linked_list_iterator_next(&iterator->it); 146 } 147 148 // Mesh Node Element functions 149 uint8_t mesh_access_get_element_index(mesh_model_t * mesh_model){ 150 return mesh_model->element->element_index; 151 } 152 153 uint16_t mesh_access_get_element_address(mesh_model_t * mesh_model){ 154 return mesh_node_get_primary_element_address() + mesh_model->element->element_index; 155 } 156 157 // Model Identifier utilities 158 159 uint32_t mesh_model_get_model_identifier(uint16_t vendor_id, uint16_t model_id){ 160 return (((uint32_t) vendor_id << 16)) | model_id; 161 } 162 163 uint32_t mesh_model_get_model_identifier_bluetooth_sig(uint16_t model_id){ 164 return ((uint32_t) BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC << 16) | model_id; 165 } 166 167 uint16_t mesh_model_get_model_id(uint32_t model_identifier){ 168 return model_identifier & 0xFFFFu; 169 } 170 171 uint16_t mesh_model_get_vendor_id(uint32_t model_identifier){ 172 return model_identifier >> 16; 173 } 174 175 int mesh_model_is_bluetooth_sig(uint32_t model_identifier){ 176 return mesh_model_get_vendor_id(model_identifier) == BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC; 177 } 178 179 mesh_model_t * mesh_node_get_configuration_server(void){ 180 return mesh_model_get_by_identifier(mesh_node_get_primary_element(), mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_CONFIGURATION_SERVER)); 181 } 182 183 mesh_model_t * mesh_node_get_health_server(void){ 184 return mesh_model_get_by_identifier(mesh_node_get_primary_element(), mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_HEALTH_SERVER)); 185 } 186 187 void mesh_model_reset_appkeys(mesh_model_t * mesh_model){ 188 uint16_t i; 189 for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){ 190 mesh_model->appkey_indices[i] = MESH_APPKEY_INVALID; 191 } 192 } 193 194 void mesh_element_add_model(mesh_element_t * element, mesh_model_t * mesh_model){ 195 // reset app keys 196 mesh_model_reset_appkeys(mesh_model); 197 198 if (mesh_model_is_bluetooth_sig(mesh_model->model_identifier)){ 199 element->models_count_sig++; 200 } else { 201 element->models_count_vendor++; 202 } 203 mesh_model->mid = mid_counter++; 204 mesh_model->element = element; 205 btstack_linked_list_add_tail(&element->models, (btstack_linked_item_t *) mesh_model); 206 } 207 208 void mesh_model_iterator_init(mesh_model_iterator_t * iterator, mesh_element_t * element){ 209 btstack_linked_list_iterator_init(&iterator->it, &element->models); 210 } 211 212 int mesh_model_iterator_has_next(mesh_model_iterator_t * iterator){ 213 return btstack_linked_list_iterator_has_next(&iterator->it); 214 } 215 216 mesh_model_t * mesh_model_iterator_next(mesh_model_iterator_t * iterator){ 217 return (mesh_model_t *) btstack_linked_list_iterator_next(&iterator->it); 218 } 219 220 mesh_model_t * mesh_model_get_by_identifier(mesh_element_t * element, uint32_t model_identifier){ 221 mesh_model_iterator_t it; 222 mesh_model_iterator_init(&it, element); 223 while (mesh_model_iterator_has_next(&it)){ 224 mesh_model_t * model = mesh_model_iterator_next(&it); 225 if (model->model_identifier != model_identifier) continue; 226 return model; 227 } 228 return NULL; 229 } 230 231 mesh_model_t * mesh_access_model_for_address_and_model_identifier(uint16_t element_address, uint32_t model_identifier, uint8_t * status){ 232 mesh_element_t * element = mesh_node_element_for_unicast_address(element_address); 233 if (element == NULL){ 234 *status = MESH_FOUNDATION_STATUS_INVALID_ADDRESS; 235 return NULL; 236 } 237 mesh_model_t * model = mesh_model_get_by_identifier(element, model_identifier); 238 if (model == NULL) { 239 *status = MESH_FOUNDATION_STATUS_INVALID_MODEL; 240 } else { 241 *status = MESH_FOUNDATION_STATUS_SUCCESS; 242 } 243 return model; 244 } 245 // Mesh Model Subscription 246 int mesh_model_contains_subscription(mesh_model_t * mesh_model, uint16_t address){ 247 uint16_t i; 248 for (i=0;i<MAX_NR_MESH_SUBSCRIPTION_PER_MODEL;i++){ 249 if (mesh_model->subscriptions[i] == address) return 1; 250 } 251 return 0; 252 } 253 254 void mesh_node_set_device_uuid(const uint8_t * device_uuid){ 255 (void)memcpy(mesh_node_device_uuid, device_uuid, 16); 256 mesh_node_have_device_uuid = 1; 257 } 258 259 /** 260 * @brief Get Device UUID 261 */ 262 const uint8_t * mesh_node_get_device_uuid(void){ 263 if (mesh_node_have_device_uuid == 0) return NULL; 264 return mesh_node_device_uuid; 265 } 266 267 268 // Heartbeat (helper) 269 uint16_t mesh_heartbeat_pwr2(uint8_t value){ 270 if (value == 0 ) return 0x0000; 271 if (value == 0xff || value == 0x11) return 0xffff; 272 return 1 << (value-1); 273 } 274 275 uint8_t mesh_heartbeat_count_log(uint16_t value){ 276 if (value == 0) return 0x00; 277 if (value == 0xffff) return 0xff; 278 // count leading zeros, supported by clang and gcc 279 // note: CountLog(8) == CountLog(7) = 3 280 return 33 - btstack_clz(value - 1); 281 } 282 283 uint8_t mesh_heartbeat_period_log(uint16_t value){ 284 if (value == 0) return 0x00; 285 // count leading zeros, supported by clang and gcc 286 // note: PeriodLog(8) == PeriodLog(7) = 3 287 return 33 - btstack_clz(value - 1); 288 } 289