xref: /btstack/src/mesh/mesh_foundation.c (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
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_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 = 0;
52 static uint8_t mesh_foundation_relay_retransmit = 0;
53 static uint8_t mesh_foundation_friend    = 0; // not supported
54 static uint8_t mesh_foundation_low_power = 0;
55 
mesh_foundation_gatt_proxy_set(uint8_t value)56 void mesh_foundation_gatt_proxy_set(uint8_t value){
57     if (value >= MESH_FOUNDATION_STATE_NOT_SUPPORTED) return;
58     mesh_foundation_gatt_proxy = value;
59     printf("MESH: GATT PROXY %x\n", mesh_foundation_gatt_proxy);
60 }
mesh_foundation_gatt_proxy_get(void)61 uint8_t mesh_foundation_gatt_proxy_get(void){
62 #ifdef ENABLE_MESH_PROXY_SERVER
63     return mesh_foundation_gatt_proxy;
64 #else
65     return MESH_FOUNDATION_STATE_NOT_SUPPORTED;
66 #endif
67 }
68 
mesh_foundation_low_power_set(uint8_t value)69 void mesh_foundation_low_power_set(uint8_t value){
70     if (value >= MESH_FOUNDATION_STATE_NOT_SUPPORTED) return;
71     mesh_foundation_low_power = value;
72     printf("MESH: LOW POWER %x\n", mesh_foundation_low_power);
73 }
mesh_foundation_low_power_get(void)74 uint8_t mesh_foundation_low_power_get(void){
75     return MESH_FOUNDATION_STATE_NOT_SUPPORTED;
76 }
77 
mesh_foundation_beacon_set(uint8_t value)78 void mesh_foundation_beacon_set(uint8_t value){
79     if (value >= MESH_FOUNDATION_STATE_NOT_SUPPORTED) return;
80     mesh_foundation_beacon = value;
81     printf("MESH: Secure Network Beacon %x\n", mesh_foundation_beacon);
82 }
mesh_foundation_beacon_get(void)83 uint8_t mesh_foundation_beacon_get(void){
84 #ifdef ENABLE_MESH_ADV_BEARER
85     return mesh_foundation_beacon;
86 #else
87     return MESH_FOUNDATION_STATE_NOT_SUPPORTED;
88 #endif
89 }
90 
mesh_foundation_default_ttl_set(uint8_t ttl)91 void mesh_foundation_default_ttl_set(uint8_t ttl){
92     mesh_foundation_default_ttl = ttl;
93     printf("MESH: Default TTL = 0x%x\n", mesh_foundation_default_ttl);
94 }
mesh_foundation_default_ttl_get(void)95 uint8_t mesh_foundation_default_ttl_get(void){
96     return mesh_foundation_default_ttl;
97 }
98 
mesh_foundation_friend_set(uint8_t value)99 void mesh_foundation_friend_set(uint8_t value){
100     if (value >= MESH_FOUNDATION_STATE_NOT_SUPPORTED) return;
101     mesh_foundation_friend = value;
102     printf("MESH: Friend = 0x%x\n", mesh_foundation_friend);
103 }
mesh_foundation_friend_get(void)104 uint8_t mesh_foundation_friend_get(void){
105     return MESH_FOUNDATION_STATE_NOT_SUPPORTED;
106 }
107 
mesh_foundation_network_transmit_set(uint8_t network_transmit)108 void mesh_foundation_network_transmit_set(uint8_t network_transmit){
109     mesh_foundation_network_transmit = network_transmit;
110     printf("MESH: Network Transmit = 0x%02x - %u transmissions, %u ms interval\n",
111             mesh_foundation_network_transmit, (mesh_foundation_network_transmit & 7) + 1, (mesh_foundation_network_transmit >> 3) * 10);
112 }
mesh_foundation_network_transmit_get(void)113 uint8_t mesh_foundation_network_transmit_get(void){
114     return mesh_foundation_network_transmit;
115 }
116 
mesh_foundation_relay_set(uint8_t value)117 void mesh_foundation_relay_set(uint8_t value){
118     if (value >= MESH_FOUNDATION_STATE_NOT_SUPPORTED) return;
119     mesh_foundation_relay = value;
120     printf("MESH: Relay = 0x%02x\n", mesh_foundation_relay);
121 }
mesh_foundation_relay_get(void)122 uint8_t mesh_foundation_relay_get(void){
123 #ifdef ENABLE_MESH_RELAY
124     return mesh_foundation_relay;
125 #else
126     return MESH_FOUNDATION_STATE_NOT_SUPPORTED;
127 #endif
128 }
129 
mesh_foundation_relay_retransmit_set(uint8_t relay_retransmit)130 void mesh_foundation_relay_retransmit_set(uint8_t relay_retransmit){
131     mesh_foundation_relay_retransmit = relay_retransmit;
132     printf("MESH: Relay Retransmit = 0x%02x - %u transmissions, %u ms interval\n",
133             mesh_foundation_relay_retransmit, (mesh_foundation_relay_retransmit & 7) + 1, (mesh_foundation_relay_retransmit >> 3) * 10);
134 }
mesh_foundation_relay_retransmit_get(void)135 uint8_t mesh_foundation_relay_retransmit_get(void){
136     return mesh_foundation_relay_retransmit;
137 }
138 
mesh_foundation_get_features(void)139 uint16_t mesh_foundation_get_features(void){
140     uint16_t active_features = 0;
141     if (mesh_foundation_low_power_get() == 1){
142         active_features |= 1 << 3;
143     }
144     if (mesh_foundation_friend_get() == 1){
145         active_features |= 1 << 2;
146     }
147     if (mesh_foundation_gatt_proxy_get() == 1){
148         active_features |= 1 << 1;
149     }
150     if (mesh_foundation_relay_get() == 1){
151         active_features |= 1 << 0;
152     }
153     return active_features;
154 }
155