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_foundation.c" 39 40 #include <stdio.h> 41 42 #include "mesh/mesh_foundation.h" 43 44 #define MESH_TTL_MAX 0x7f 45 #define MESH_FOUNDATION_STATE_NOT_SUPPORTED 2 46 47 static uint8_t mesh_foundation_gatt_proxy = 0; 48 static uint8_t mesh_foundation_beacon = 0; 49 static uint8_t mesh_foundation_default_ttl = 7; 50 static uint8_t mesh_foundation_network_transmit = (10 << 3) | 2; // step 300 ms, send 3 times 51 static uint8_t mesh_foundation_relay = MESH_FOUNDATION_STATE_NOT_SUPPORTED; 52 static uint8_t mesh_foundation_relay_retransmit = 0; 53 static uint8_t mesh_foundation_friend = MESH_FOUNDATION_STATE_NOT_SUPPORTED; // not supported 54 static uint8_t mesh_foundation_low_power = MESH_FOUNDATION_STATE_NOT_SUPPORTED; 55 56 void mesh_foundation_gatt_proxy_set(uint8_t value){ 57 mesh_foundation_gatt_proxy = value; 58 printf("MESH: GATT PROXY %x\n", mesh_foundation_gatt_proxy); 59 } 60 uint8_t mesh_foundation_gatt_proxy_get(void){ 61 return mesh_foundation_gatt_proxy; 62 } 63 64 void mesh_foundation_low_power_set(uint8_t value){ 65 mesh_foundation_low_power = value; 66 printf("MESH: LOW POWER %x\n", mesh_foundation_low_power); 67 } 68 uint8_t mesh_foundation_low_power_get(void){ 69 return mesh_foundation_low_power; 70 } 71 72 void mesh_foundation_beacon_set(uint8_t value){ 73 mesh_foundation_beacon = value; 74 printf("MESH: Secure Network Beacon %x\n", mesh_foundation_beacon); 75 } 76 uint8_t mesh_foundation_beacon_get(void){ 77 return mesh_foundation_beacon; 78 } 79 80 void mesh_foundation_default_ttl_set(uint8_t ttl){ 81 mesh_foundation_default_ttl = ttl; 82 printf("MESH: Default TTL = 0x%x\n", mesh_foundation_default_ttl); 83 } 84 uint8_t mesh_foundation_default_ttl_get(void){ 85 return mesh_foundation_default_ttl; 86 } 87 88 void mesh_foundation_friend_set(uint8_t value){ 89 mesh_foundation_friend = value; 90 printf("MESH: Friend = 0x%x\n", mesh_foundation_friend); 91 } 92 uint8_t mesh_foundation_friend_get(void){ 93 return mesh_foundation_friend; 94 } 95 96 void mesh_foundation_network_transmit_set(uint8_t network_transmit){ 97 mesh_foundation_network_transmit = network_transmit; 98 printf("MESH: Network Transmit = 0x%02x - %u transmissions, %u ms interval\n", 99 mesh_foundation_network_transmit, (mesh_foundation_network_transmit & 7) + 1, (mesh_foundation_network_transmit >> 3) * 10); 100 } 101 uint8_t mesh_foundation_network_transmit_get(void){ 102 return mesh_foundation_network_transmit; 103 } 104 105 void mesh_foundation_relay_set(uint8_t relay){ 106 mesh_foundation_relay = relay; 107 printf("MESH: Relay = 0x%02x\n", mesh_foundation_relay); 108 } 109 uint8_t mesh_foundation_relay_get(void){ 110 return mesh_foundation_relay; 111 } 112 113 void mesh_foundation_relay_retransmit_set(uint8_t relay_retransmit){ 114 mesh_foundation_relay_retransmit = relay_retransmit; 115 printf("MESH: Relay Retransmit = 0x%02x - %u transmissions, %u ms interval\n", 116 mesh_foundation_relay_retransmit, (mesh_foundation_relay_retransmit & 7) + 1, (mesh_foundation_relay_retransmit >> 3) * 10); 117 } 118 uint8_t mesh_foundation_relay_retransmit_get(void){ 119 return mesh_foundation_relay_retransmit; 120 } 121 122 uint16_t mesh_foundation_get_features(void){ 123 uint16_t active_features = 0; 124 if (mesh_foundation_low_power_get() == 1){ 125 active_features |= 1 << 3; 126 } 127 if (mesh_foundation_friend_get() == 1){ 128 active_features |= 1 << 2; 129 } 130 if (mesh_foundation_gatt_proxy_get() == 1){ 131 active_features |= 1 << 1; 132 } 133 if (mesh_foundation_relay_get() == 1){ 134 active_features |= 1 << 0; 135 } 136 return active_features; 137 } 138