xref: /btstack/src/mesh/mesh_configuration_client.c (revision e38651d8f487f14ed0a12cde21a476e72f00d5ed)
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 #if 0
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 
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 
208 // Model Operations
209 static void mesh_configuration_client_beacon_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
210     mesh_access_parser_state_t parser;
211     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
212 
213     uint8_t beacon_status = mesh_access_parser_get_u8(&parser);
214 
215     uint8_t event[7] = {HCI_EVENT_MESH_META, 5, MESH_SUBEVENT_FOUNDATION_BEACON_STATUS};
216     int pos = 3;
217     // dest
218     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
219     pos += 2;
220     event[pos++] = ERROR_CODE_SUCCESS;
221     event[pos++] = beacon_status;
222 
223     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
224     mesh_access_message_processed(pdu);
225 }
226 
227 static void mesh_configuration_client_composition_data_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
228     mesh_access_parser_state_t parser;
229     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
230 
231     uint8_t page = mesh_access_parser_get_u8(&parser);
232     uint16_t cid = mesh_access_parser_get_u16(&parser);
233     uint16_t pid = mesh_access_parser_get_u16(&parser);
234     uint16_t vid = mesh_access_parser_get_u16(&parser);
235     uint16_t crpl = mesh_access_parser_get_u16(&parser);
236     uint16_t features = mesh_access_parser_get_u16(&parser);
237 
238     // TODO: list of element descriptions, see Table 4.4
239 
240     uint8_t event[17] = {HCI_EVENT_MESH_META, 5, MESH_SUBEVENT_FOUNDATION_COMPOSITION_DATA_STATUS};
241     int pos = 3;
242     // dest
243     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
244     pos += 2;
245     event[pos++] = ERROR_CODE_SUCCESS;
246     event[pos++] = page;
247 
248     little_endian_store_16(event, pos, cid);
249     pos += 2;
250     little_endian_store_16(event, pos, pid);
251     pos += 2;
252     little_endian_store_16(event, pos, vid);
253     pos += 2;
254     little_endian_store_16(event, pos, crpl);
255     pos += 2;
256     little_endian_store_16(event, pos, features);
257     pos += 2;
258 
259     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
260     mesh_access_message_processed(pdu);
261 }
262 
263 static void mesh_configuration_client_default_ttl_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
264     mesh_access_parser_state_t parser;
265     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
266 
267     uint8_t default_ttl = mesh_access_parser_get_u8(&parser);
268 
269     uint8_t event[7] = {HCI_EVENT_MESH_META, 5, MESH_SUBEVENT_FOUNDATION_DEFAULT_TTL_STATUS};
270     int pos = 3;
271     // dest
272     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
273     pos += 2;
274     event[pos++] = ERROR_CODE_SUCCESS;
275     event[pos++] = default_ttl;
276 
277     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
278     mesh_access_message_processed(pdu);
279 }
280 
281 static void mesh_configuration_client_gatt_proxy_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
282     mesh_access_parser_state_t parser;
283     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
284 
285     uint8_t gatt_proxy_state = mesh_access_parser_get_u8(&parser);
286 
287     uint8_t event[7] = {HCI_EVENT_MESH_META, 5, MESH_SUBEVENT_FOUNDATION_GATT_PROXY_STATUS};
288     int pos = 3;
289     // dest
290     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
291     pos += 2;
292     event[pos++] = ERROR_CODE_SUCCESS;
293     event[pos++] = gatt_proxy_state;
294 
295     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
296     mesh_access_message_processed(pdu);
297 }
298 
299 const static mesh_operation_t mesh_configuration_client_model_operations[] = {
300     { MESH_FOUNDATION_OPERATION_BEACON_STATUS,           1, mesh_configuration_client_beacon_status_handler },
301     { MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_STATUS, 1, mesh_configuration_client_composition_data_status_handler },
302     { MESH_FOUNDATION_OPERATION_DEFAULT_TTL_STATUS,      1, mesh_configuration_client_default_ttl_handler },
303     { MESH_FOUNDATION_OPERATION_GATT_PROXY_STATUS,       1, mesh_configuration_client_gatt_proxy_handler },
304     { 0, 0, NULL }
305 };
306 
307 const mesh_operation_t * mesh_configuration_client_get_operations(void){
308     return mesh_configuration_client_model_operations;
309 }
310 
311 void mesh_configuration_client_register_packet_handler(mesh_model_t *configuration_client_model, btstack_packet_handler_t events_packet_handler){
312     btstack_assert(events_packet_handler != NULL);
313     btstack_assert(configuration_client_model != NULL);
314 
315     configuration_client_model->model_packet_handler = events_packet_handler;
316 }
317 
318