xref: /btstack/src/mesh/mesh_configuration_client.c (revision 344d65499b13e6e38d9ef78b7900663409400e77)
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 #if 0
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 
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 // Model Operations
184 static void mesh_configuration_client_beacon_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
185     mesh_access_parser_state_t parser;
186     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
187 
188     uint8_t beacon_status = mesh_access_parser_get_u8(&parser);
189 
190     uint8_t event[7] = {HCI_EVENT_MESH_META, 5, MESH_SUBEVENT_FOUNDATION_BEACON_STATUS};
191     int pos = 3;
192     // dest
193     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
194     pos += 2;
195     event[pos++] = ERROR_CODE_SUCCESS;
196     event[pos++] = beacon_status;
197 
198     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
199     mesh_access_message_processed(pdu);
200 }
201 
202 static void mesh_configuration_client_composition_data_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
203     mesh_access_parser_state_t parser;
204     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
205 
206     uint8_t page = mesh_access_parser_get_u8(&parser);
207     uint16_t cid = mesh_access_parser_get_u16(&parser);
208     uint16_t pid = mesh_access_parser_get_u16(&parser);
209     uint16_t vid = mesh_access_parser_get_u16(&parser);
210     uint16_t crpl = mesh_access_parser_get_u16(&parser);
211     uint16_t features = mesh_access_parser_get_u16(&parser);
212 
213     // TODO: list of element descriptions, see Table 4.4
214 
215     uint8_t event[17] = {HCI_EVENT_MESH_META, 5, MESH_SUBEVENT_FOUNDATION_COMPOSITION_DATA_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++] = page;
222 
223     little_endian_store_16(event, pos, cid);
224     pos += 2;
225     little_endian_store_16(event, pos, pid);
226     pos += 2;
227     little_endian_store_16(event, pos, vid);
228     pos += 2;
229     little_endian_store_16(event, pos, crpl);
230     pos += 2;
231     little_endian_store_16(event, pos, features);
232     pos += 2;
233 
234     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
235     mesh_access_message_processed(pdu);
236 }
237 
238 static void mesh_configuration_client_default_ttl_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
239     mesh_access_parser_state_t parser;
240     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
241 
242     uint8_t default_ttl = mesh_access_parser_get_u8(&parser);
243 
244     uint8_t event[7] = {HCI_EVENT_MESH_META, 5, MESH_SUBEVENT_FOUNDATION_DEFAULT_TTL_STATUS};
245     int pos = 3;
246     // dest
247     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
248     pos += 2;
249     event[pos++] = ERROR_CODE_SUCCESS;
250     event[pos++] = default_ttl;
251 
252     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
253     mesh_access_message_processed(pdu);
254 }
255 
256 const static mesh_operation_t mesh_configuration_client_model_operations[] = {
257     { MESH_FOUNDATION_OPERATION_BEACON_STATUS,           1, mesh_configuration_client_beacon_status_handler },
258     { MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_STATUS, 1, mesh_configuration_client_composition_data_status_handler },
259     { MESH_FOUNDATION_OPERATION_DEFAULT_TTL_STATUS,      1, mesh_configuration_client_default_ttl_handler },
260     { 0, 0, NULL }
261 };
262 
263 const mesh_operation_t * mesh_configuration_client_get_operations(void){
264     return mesh_configuration_client_model_operations;
265 }
266 
267 void mesh_configuration_client_register_packet_handler(mesh_model_t *configuration_client_model, btstack_packet_handler_t events_packet_handler){
268     btstack_assert(events_packet_handler != NULL);
269     btstack_assert(configuration_client_model != NULL);
270 
271     configuration_client_model->model_packet_handler = events_packet_handler;
272 }
273 
274