xref: /btstack/src/mesh/mesh_generic_level_client.c (revision d13e5cf6603f8d92493ebd8a19ad22c4dfb7a126)
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_generic_level_client.c"
39 
40 #include <string.h>
41 #include <stdio.h>
42 
43 #include "mesh/mesh_generic_level_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 // Generic Level Messages
58 
59 // Level Messages
60 static const mesh_access_message_t mesh_generic_level_get = {
61         MESH_GENERIC_LEVEL_GET, ""
62 };
63 
64 static const mesh_access_message_t mesh_generic_level_set_with_transition = {
65         MESH_GENERIC_LEVEL_SET, "2111"
66 };
67 
68 static const mesh_access_message_t mesh_generic_level_set_instantaneous = {
69         MESH_GENERIC_LEVEL_SET, "21"
70 };
71 
72 static const mesh_access_message_t mesh_generic_level_set_unacknowledged_with_transition = {
73         MESH_GENERIC_LEVEL_SET_UNACKNOWLEDGED, "2111"
74 };
75 
76 static const mesh_access_message_t mesh_generic_level_set_unacknowledged_instantaneous = {
77         MESH_GENERIC_LEVEL_SET_UNACKNOWLEDGED, "21"
78 };
79 
80 // Delta Messages
81 static const mesh_access_message_t mesh_generic_delta_set_with_transition = {
82         MESH_GENERIC_DELTA_SET, "2111"
83 };
84 
85 static const mesh_access_message_t mesh_generic_delta_set_instantaneous = {
86         MESH_GENERIC_DELTA_SET, "21"
87 };
88 
89 static const mesh_access_message_t mesh_generic_delta_set_unacknowledged_with_transition = {
90         MESH_GENERIC_DELTA_SET_UNACKNOWLEDGED, "2111"
91 };
92 
93 static const mesh_access_message_t mesh_generic_delta_set_unacknowledged_instantaneous = {
94         MESH_GENERIC_DELTA_SET_UNACKNOWLEDGED, "21"
95 };
96 
97 // Move Messages
98 static const mesh_access_message_t mesh_generic_move_set_with_transition = {
99         MESH_GENERIC_MOVE_SET, "2111"
100 };
101 
102 static const mesh_access_message_t mesh_generic_move_set_instantaneous = {
103         MESH_GENERIC_MOVE_SET, "21"
104 };
105 
106 static const mesh_access_message_t mesh_generic_move_set_unacknowledged_with_transition = {
107         MESH_GENERIC_MOVE_SET_UNACKNOWLEDGED, "2111"
108 };
109 
110 static const mesh_access_message_t mesh_generic_move_set_unacknowledged_instantaneous = {
111         MESH_GENERIC_MOVE_SET_UNACKNOWLEDGED, "21"
112 };
113 
114 
115 // Generic Level Client functions
116 
117 static void generic_client_send_message_unacknowledged(uint16_t src, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, mesh_pdu_t *pdu){
118     uint8_t  ttl  = mesh_foundation_default_ttl_get();
119     mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0);
120     mesh_access_send_unacknowledged_pdu(pdu);
121 }
122 
123 static void generic_client_send_message_acknowledged(uint16_t src, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, mesh_pdu_t *pdu, uint32_t ack_opcode){
124     uint8_t  ttl  = mesh_foundation_default_ttl_get();
125     mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0);
126     mesh_access_send_acknowledged_pdu(pdu, mesh_access_acknowledged_message_retransmissions(), ack_opcode);
127 }
128 
129 static inline uint8_t mesh_generic_level_client_set_value(mesh_model_t * mesh_model,
130     const mesh_access_message_t * message_template_with_transition, const mesh_access_message_t * message_template_instantaneous,
131     uint16_t dest, uint16_t netkey_index, uint16_t appkey_index,
132     int16_t value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id, uint8_t acknowledged){
133 
134     mesh_upper_transport_pdu_t * transport_pdu;
135     if (transition_time_gdtt != 0) {
136         transport_pdu = mesh_access_setup_message(message_template_with_transition, value, transaction_id, transition_time_gdtt, delay_time_gdtt);
137     } else {
138         transport_pdu = mesh_access_setup_message(message_template_instantaneous, value, transaction_id);
139     }
140     if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
141 
142     if (acknowledged != 0){
143         generic_client_send_message_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_GENERIC_LEVEL_STATUS);
144     } else {
145         generic_client_send_message_unacknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu);
146     }
147     return ERROR_CODE_SUCCESS;
148 }
149 
150 // Level
151 
152 uint8_t mesh_generic_level_client_level_get(mesh_model_t *mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){
153     // setup message
154     mesh_upper_transport_pdu_t * transport_pdu = mesh_access_setup_message(&mesh_generic_level_get);
155     if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
156     // send as segmented access pdu
157     generic_client_send_message_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_GENERIC_LEVEL_STATUS);
158     return ERROR_CODE_SUCCESS;;
159 }
160 
161 uint8_t mesh_generic_level_client_level_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index,
162     int16_t level_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id){
163 
164     return mesh_generic_level_client_set_value(mesh_model, &mesh_generic_level_set_with_transition, &mesh_generic_level_set_instantaneous,
165         dest, netkey_index, appkey_index, level_value, transition_time_gdtt, delay_time_gdtt, transaction_id, 1);
166 }
167 
168 uint8_t mesh_generic_level_client_level_set_unacknowledged(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index,
169     int16_t level_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id){
170 
171     return mesh_generic_level_client_set_value(mesh_model, &mesh_generic_level_set_unacknowledged_with_transition, &mesh_generic_level_set_unacknowledged_instantaneous,
172         dest, netkey_index, appkey_index, level_value, transition_time_gdtt, delay_time_gdtt, transaction_id, 0);
173 }
174 
175 uint8_t mesh_generic_level_client_publish_level(mesh_model_t * mesh_model, int16_t level_value, uint8_t transaction_id){
176     mesh_publication_model_t * publication_model = mesh_model->publication_model;
177     uint16_t appkey_index = publication_model->appkey_index;
178     mesh_transport_key_t * app_key = mesh_transport_key_get(appkey_index);
179     if (app_key == NULL) return MESH_ERROR_APPKEY_INDEX_INVALID;
180 
181     return mesh_generic_level_client_level_set_unacknowledged(mesh_model, publication_model->address, app_key->netkey_index, appkey_index, level_value, 0, 0, transaction_id);
182 }
183 
184 // Delta
185 uint8_t mesh_generic_level_client_delta_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index,
186     int16_t delta_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id){
187 
188     return mesh_generic_level_client_set_value(mesh_model, &mesh_generic_delta_set_with_transition, &mesh_generic_delta_set_instantaneous,
189         dest, netkey_index, appkey_index, delta_value, transition_time_gdtt, delay_time_gdtt, transaction_id, 1);
190 }
191 
192 uint8_t mesh_generic_level_client_delta_set_unacknowledged(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index,
193     int16_t delta_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id){
194 
195     return mesh_generic_level_client_set_value(mesh_model, &mesh_generic_delta_set_unacknowledged_with_transition, &mesh_generic_delta_set_unacknowledged_instantaneous,
196         dest, netkey_index, appkey_index, delta_value, transition_time_gdtt, delay_time_gdtt, transaction_id, 0);
197 }
198 
199 // Move
200 uint8_t mesh_generic_level_client_move_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index,
201     int16_t delta_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id){
202 
203     return mesh_generic_level_client_set_value(mesh_model, &mesh_generic_move_set_with_transition, &mesh_generic_move_set_instantaneous,
204         dest, netkey_index, appkey_index, delta_value, transition_time_gdtt, delay_time_gdtt, transaction_id, 1);
205 }
206 
207 uint8_t mesh_generic_level_client_move_set_unacknowledged(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index,
208     int16_t delta_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id){
209 
210     return mesh_generic_level_client_set_value(mesh_model, &mesh_generic_move_set_unacknowledged_with_transition, &mesh_generic_move_set_unacknowledged_instantaneous,
211         dest, netkey_index, appkey_index, delta_value, transition_time_gdtt, delay_time_gdtt, transaction_id, 0);
212 }
213 
214 // Model Operations
215 
216 static void generic_level_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
217     if (!mesh_model->model_packet_handler){
218         log_error("model_packet_handler == NULL");
219     }
220 
221     mesh_access_parser_state_t parser;
222     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
223 
224     uint8_t present_value = mesh_access_parser_get_uint8(&parser);
225     uint8_t target_value = 0;
226     uint8_t remaining_time_gdtt = 0;
227 
228     if (mesh_access_parser_available(&parser) == 2){
229         target_value = mesh_access_parser_get_uint8(&parser);
230         remaining_time_gdtt = mesh_access_parser_get_uint8(&parser);
231     }
232 
233     uint8_t event[14] = {HCI_EVENT_MESH_META, 12, MESH_SUBEVENT_GENERIC_LEVEL};
234 
235     int pos = 3;
236     // dest
237     little_endian_store_16(event, pos, mesh_pdu_src(pdu));
238     pos += 2;
239     event[pos++] = ERROR_CODE_SUCCESS;
240 
241     little_endian_store_16(event, pos, present_value);
242     pos += 2;
243     little_endian_store_16(event, pos, target_value);
244     pos += 2;
245 
246     little_endian_store_32(event, pos, (uint32_t) mesh_access_time_gdtt2ms(remaining_time_gdtt));
247     pos += 4;
248 
249     (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
250     mesh_access_message_processed(pdu);
251 }
252 
253 static const mesh_operation_t mesh_generic_level_model_operations[] = {
254     { MESH_GENERIC_LEVEL_STATUS, 0, generic_level_status_handler },
255     { 0, 0, NULL }
256 };
257 
258 const mesh_operation_t * mesh_generic_level_client_get_operations(void){
259     return mesh_generic_level_model_operations;
260 }
261 
262 void mesh_generic_level_client_register_packet_handler(mesh_model_t *mesh_model, btstack_packet_handler_t transition_events_packet_handler){
263     if (transition_events_packet_handler == NULL){
264         log_error("mesh_generic_level_client_register_packet_handler called with NULL callback");
265         return;
266     }
267     if (mesh_model == NULL){
268         log_error("mesh_generic_level_client_register_packet_handler called with NULL mesh_model");
269         return;
270     }
271     mesh_model->model_packet_handler = transition_events_packet_handler;
272 }
273