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_virtual_addresses.c" 39 40 #include "mesh/mesh_virtual_addresses.h" 41 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 46 #include "btstack_util.h" 47 #include "btstack_memory.h" 48 49 // virtual address management 50 51 static btstack_linked_list_t mesh_virtual_addresses; 52 static uint8_t mesh_virtual_addresses_used[MAX_NR_MESH_VIRTUAL_ADDRESSES]; 53 54 uint16_t mesh_virtual_addresses_get_free_pseudo_dst(void){ 55 uint16_t i; 56 for (i=0;i < MAX_NR_MESH_VIRTUAL_ADDRESSES ; i++){ 57 if (mesh_virtual_addresses_used[i] == 0){ 58 return 0x8000+i; 59 } 60 } 61 return MESH_ADDRESS_UNSASSIGNED; 62 } 63 64 void mesh_virtual_address_add(mesh_virtual_address_t * virtual_address){ 65 mesh_virtual_addresses_used[virtual_address->pseudo_dst-0x8000] = 1; 66 virtual_address->ref_count = 0; 67 btstack_linked_list_add(&mesh_virtual_addresses, (void *) virtual_address); 68 } 69 70 void mesh_virtual_address_remove(mesh_virtual_address_t * virtual_address){ 71 btstack_linked_list_remove(&mesh_virtual_addresses, (void *) virtual_address); 72 mesh_virtual_addresses_used[virtual_address->pseudo_dst-0x8000] = 0; 73 } 74 75 // helper 76 mesh_virtual_address_t * mesh_virtual_address_register(uint8_t * label_uuid, uint16_t hash){ 77 uint16_t pseudo_dst = mesh_virtual_addresses_get_free_pseudo_dst(); 78 if (pseudo_dst == 0) return NULL; 79 mesh_virtual_address_t * virtual_address = btstack_memory_mesh_virtual_address_get(); 80 if (virtual_address == NULL) return NULL; 81 82 virtual_address->hash = hash; 83 virtual_address->pseudo_dst = pseudo_dst; 84 memcpy(virtual_address->label_uuid, label_uuid, 16); 85 mesh_virtual_address_add(virtual_address); 86 87 return virtual_address; 88 } 89 90 mesh_virtual_address_t * mesh_virtual_address_for_pseudo_dst(uint16_t pseudo_dst){ 91 btstack_linked_list_iterator_t it; 92 btstack_linked_list_iterator_init(&it, &mesh_virtual_addresses); 93 while (btstack_linked_list_iterator_has_next(&it)){ 94 mesh_virtual_address_t * item = (mesh_virtual_address_t *) btstack_linked_list_iterator_next(&it); 95 if (item->pseudo_dst == pseudo_dst) return item; 96 } 97 return NULL; 98 } 99 100 mesh_virtual_address_t * mesh_virtual_address_for_label_uuid(uint8_t * label_uuid){ 101 btstack_linked_list_iterator_t it; 102 btstack_linked_list_iterator_init(&it, &mesh_virtual_addresses); 103 while (btstack_linked_list_iterator_has_next(&it)){ 104 mesh_virtual_address_t * item = (mesh_virtual_address_t *) btstack_linked_list_iterator_next(&it); 105 if (memcmp(item->label_uuid, label_uuid, 16) == 0) return item; 106 } 107 return NULL; 108 } 109 // virtual address iterator 110 111 void mesh_virtual_address_iterator_init(mesh_virtual_address_iterator_t * it, uint16_t hash){ 112 btstack_linked_list_iterator_init(&it->it, &mesh_virtual_addresses); 113 it->hash = hash; 114 it->address = NULL; 115 } 116 117 int mesh_virtual_address_iterator_has_more(mesh_virtual_address_iterator_t * it){ 118 // find next matching key 119 while (1){ 120 printf("check %p\n", it->address); 121 if (it->address && it->address->hash == it->hash) return 1; 122 if (!btstack_linked_list_iterator_has_next(&it->it)) break; 123 it->address = (mesh_virtual_address_t *) btstack_linked_list_iterator_next(&it->it); 124 } 125 return 0; 126 } 127 128 const mesh_virtual_address_t * mesh_virtual_address_iterator_get_next(mesh_virtual_address_iterator_t * it){ 129 mesh_virtual_address_t * address = it->address; 130 it->address = NULL; 131 return address; 132 } 133