xref: /btstack/src/mesh/mesh_configuration_client.c (revision b0e589ac8a5f467bad0bcb4d0b832000ffa6f7fd)
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_configuration_client.c"
39 
40 #include <string.h>
41 #include <stdio.h>
42 
43 #include "mesh/mesh_configuration_client.h"
44 
45 #include "bluetooth_company_id.h"
46 #include "btstack_debug.h"
47 #include "btstack_memory.h"
48 #include "btstack_util.h"
49 
50 #include "mesh/mesh_access.h"
51 #include "mesh/mesh_foundation.h"
52 #include "mesh/mesh_generic_model.h"
53 #include "mesh/mesh_keys.h"
54 #include "mesh/mesh_network.h"
55 #include "mesh/mesh_upper_transport.h"
56 
57 
58 static void mesh_configuration_client_send_acknowledged(uint16_t src, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, mesh_pdu_t *pdu, uint32_t ack_opcode){
59     uint8_t  ttl  = mesh_foundation_default_ttl_get();
60     mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0);
61     mesh_access_send_acknowledged_pdu(pdu, mesh_access_acknowledged_message_retransmissions(), ack_opcode);
62 }
63 
64 static uint8_t mesh_access_validate_envelop_params(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){
65     btstack_assert(mesh_model != NULL);
66     // TODO: validate other params
67     UNUSED(dest);
68     UNUSED(netkey_index);
69     UNUSED(appkey_index);
70 
71     return ERROR_CODE_SUCCESS;
72 }
73 
74 // Configuration client messages
75 
76 static const mesh_access_message_t mesh_configuration_client_beacon_get = {
77         MESH_FOUNDATION_OPERATION_BEACON_GET, ""
78 };
79 static const mesh_access_message_t mesh_configuration_client_beacon_set = {
80         MESH_FOUNDATION_OPERATION_BEACON_SET, "1"
81 };
82 
83 
84 static const mesh_access_message_t mesh_configuration_client_composition_data_get = {
85         MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_GET, "1"
86 };
87 
88 
89 static const mesh_access_message_t mesh_configuration_client_default_ttl_get = {
90         MESH_FOUNDATION_OPERATION_DEFAULT_TTL_GET, ""
91 };
92 static const mesh_access_message_t mesh_configuration_client_default_ttl_set = {
93         MESH_FOUNDATION_OPERATION_DEFAULT_TTL_SET, "1"
94 };
95 
96 
97 static const mesh_access_message_t mesh_configuration_client_gatt_proxy_get = {
98         MESH_FOUNDATION_OPERATION_GATT_PROXY_GET, ""
99 };
100 static const mesh_access_message_t mesh_configuration_client_gatt_proxy_set = {
101         MESH_FOUNDATION_OPERATION_GATT_PROXY_SET, "1"
102 };
103 
104 
105 static const mesh_access_message_t mesh_configuration_client_relay_get = {
106         MESH_FOUNDATION_OPERATION_RELAY_GET, ""
107 };
108 static const mesh_access_message_t mesh_configuration_client_relay_set = {
109         MESH_FOUNDATION_OPERATION_RELAY_SET, "11"
110 };
111 
112 #if 0
113 static const mesh_access_message_t mesh_configuration_client_publication_get = {
114         MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_GET, "2m"
115 };
116 static const mesh_access_message_t mesh_configuration_client_publication_set = {
117         MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_SET, "222111m"
118 };
119 static const mesh_access_message_t mesh_configuration_client_publication_virtual_address_set = {
120         MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_VIRTUAL_ADDRESS_SET, "2P2111m"
121 };
122 #endif
123 
124 uint8_t mesh_configuration_client_send_config_beacon_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){
125     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
126     if (status != ERROR_CODE_SUCCESS) return status;
127 
128     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_beacon_get);
129     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
130 
131     mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_BEACON_STATUS);
132     return ERROR_CODE_SUCCESS;
133 }
134 
135 uint8_t mesh_configuration_client_send_config_beacon_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t beacon){
136     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
137     if (status != ERROR_CODE_SUCCESS) return status;
138 
139     if (beacon > 1) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
140 
141     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_beacon_set, beacon);
142     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
143 
144     mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_BEACON_STATUS);
145     return ERROR_CODE_SUCCESS;
146 }
147 
148 uint8_t mesh_configuration_client_send_composition_data_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t page){
149     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
150     if (status != ERROR_CODE_SUCCESS) return status;
151 
152     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_composition_data_get, page);
153     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
154 
155     mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_GET);
156     return ERROR_CODE_SUCCESS;
157 }
158 
159 uint8_t mesh_configuration_client_send_default_ttl_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){
160     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
161     if (status != ERROR_CODE_SUCCESS) return status;
162 
163     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_default_ttl_get);
164     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
165 
166     mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_DEFAULT_TTL_GET);
167     return ERROR_CODE_SUCCESS;
168 }
169 
170 uint8_t mesh_configuration_client_send_default_ttl_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t ttl){
171     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
172     if (status != ERROR_CODE_SUCCESS) return status;
173 
174     if (ttl == 0x01 || ttl >= 0x80) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
175 
176     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_default_ttl_set, ttl);
177     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
178 
179     mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_DEFAULT_TTL_SET);
180     return ERROR_CODE_SUCCESS;
181 }
182 
183 uint8_t mesh_configuration_client_send_gatt_proxy_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){
184     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
185     if (status != ERROR_CODE_SUCCESS) return status;
186 
187     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_gatt_proxy_get);
188     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
189 
190     mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_GATT_PROXY_GET);
191     return ERROR_CODE_SUCCESS;
192 }
193 
194 uint8_t mesh_configuration_client_send_gatt_proxy_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t gatt_proxy_state){
195     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
196     if (status != ERROR_CODE_SUCCESS) return status;
197 
198     if (gatt_proxy_state > 2) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
199 
200     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_gatt_proxy_set, gatt_proxy_state);
201     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
202 
203     mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_GATT_PROXY_SET);
204     return ERROR_CODE_SUCCESS;
205 }
206 
207 uint8_t mesh_configuration_client_send_relay_get(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){
208     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
209     if (status != ERROR_CODE_SUCCESS) return status;
210 
211     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_relay_get);
212     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
213 
214     mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_RELAY_GET);
215     return ERROR_CODE_SUCCESS;
216 }
217 
218 uint8_t mesh_configuration_client_send_relay_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t relay, uint8_t relay_retransmit_count, uint8_t relay_retransmit_interval_steps){
219     uint8_t status = mesh_access_validate_envelop_params(mesh_model, dest, netkey_index, appkey_index);
220     if (status != ERROR_CODE_SUCCESS) return status;
221 
222     if (relay_retransmit_count > 0x07) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
223     if (relay_retransmit_interval_steps > 0x1F) return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
224 
225     mesh_network_pdu_t * network_pdu = mesh_access_setup_unsegmented_message(&mesh_configuration_client_relay_set, relay, (relay_retransmit_count << 5) | relay_retransmit_interval_steps);
226     if (!network_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
227 
228     mesh_configuration_client_send_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) network_pdu, MESH_FOUNDATION_OPERATION_RELAY_SET);
229     return ERROR_CODE_SUCCESS;
230 }
231 
232 // Model Operations
233 static void mesh_configuration_client_composition_data_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
234     // Composition Data has variable of element descriptions, with two lists of model lists
235     // Pass raw data to application but provide convenient setters instead of parsing pdu here
236 
237     // reuse part of the mesh_network_t / mesh_transport_t struct to create event without memcpy or allocation
238     uint8_t * data = mesh_pdu_data(pdu);
239     uint8_t * event = &data[-6];
240 
241     // TODO: list of element descriptions, see Table 4.4
242     int pos = 0;
243     event[pos++] = HCI_EVENT_MESH_META;
244     // Composite Data might be larger than 251 bytes - in this case only lower 8 bit are stored here. packet size is correct
245     event[pos++] = (uint8_t) (6 + mesh_pdu_len(pdu));
246     event[pos++] = MESH_SUBEVENT_CONFIGURATION_COMPOSITION_DATA;
247     // dest
248     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
249     pos += 2;
250     event[pos++] = ERROR_CODE_SUCCESS;
251 
252 
253     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
254     mesh_access_message_processed(pdu);
255 }
256 
257 static inline void mesh_configuration_client_handle_uint8_value(mesh_model_t *mesh_model, mesh_pdu_t * pdu, uint8_t subevent_type){
258     mesh_access_parser_state_t parser;
259     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
260 
261     uint8_t value = mesh_access_parser_get_u8(&parser);
262 
263     uint8_t event[7] = {HCI_EVENT_MESH_META, 5, subevent_type};
264     int pos = 3;
265     // dest
266     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
267     pos += 2;
268     event[pos++] = ERROR_CODE_SUCCESS;
269     event[pos++] = value;
270 
271     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
272     mesh_access_message_processed(pdu);
273 }
274 
275 static void mesh_configuration_client_beacon_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
276     mesh_configuration_client_handle_uint8_value(mesh_model, pdu, MESH_SUBEVENT_CONFIGURATION_BEACON);
277 }
278 
279 static void mesh_configuration_client_default_ttl_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
280     mesh_configuration_client_handle_uint8_value(mesh_model, pdu, MESH_SUBEVENT_CONFIGURATION_DEFAULT_TTL);
281 }
282 
283 static void mesh_configuration_client_gatt_proxy_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
284     mesh_configuration_client_handle_uint8_value(mesh_model, pdu, MESH_SUBEVENT_CONFIGURATION_GATT_PROXY);
285 }
286 
287 static void mesh_configuration_client_relay_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
288     mesh_access_parser_state_t parser;
289     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
290 
291     uint8_t relay = mesh_access_parser_get_u8(&parser);
292     uint8_t retransmition = mesh_access_parser_get_u8(&parser);
293 
294     uint8_t event[9] = {HCI_EVENT_MESH_META, 5, MESH_SUBEVENT_CONFIGURATION_RELAY};
295     int pos = 3;
296     // dest
297     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
298     pos += 2;
299     event[pos++] = ERROR_CODE_SUCCESS;
300     event[pos++] = relay;
301     event[pos++] = (retransmition >> 5) + 1;
302     event[pos++] = ((retransmition & 0x07) + 1) * 10;
303 
304     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
305     mesh_access_message_processed(pdu);
306 }
307 
308 const static mesh_operation_t mesh_configuration_client_model_operations[] = {
309     { MESH_FOUNDATION_OPERATION_BEACON_STATUS,            1, mesh_configuration_client_beacon_status_handler },
310     { MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_STATUS, 10, mesh_configuration_client_composition_data_status_handler },
311     { MESH_FOUNDATION_OPERATION_DEFAULT_TTL_STATUS,       1, mesh_configuration_client_default_ttl_handler },
312     { MESH_FOUNDATION_OPERATION_GATT_PROXY_STATUS,        1, mesh_configuration_client_gatt_proxy_handler },
313     { MESH_FOUNDATION_OPERATION_RELAY_STATUS,             2, mesh_configuration_client_relay_handler },
314     { 0, 0, NULL }
315 };
316 
317 const mesh_operation_t * mesh_configuration_client_get_operations(void){
318     return mesh_configuration_client_model_operations;
319 }
320 
321 void mesh_configuration_client_register_packet_handler(mesh_model_t *configuration_client_model, btstack_packet_handler_t events_packet_handler){
322     btstack_assert(events_packet_handler != NULL);
323     btstack_assert(configuration_client_model != NULL);
324 
325     configuration_client_model->model_packet_handler = events_packet_handler;
326 }
327 
328