xref: /btstack/src/mesh/mesh_access.h (revision 63ad36438244b56ec91ccd4a4f964a856767db77)
1 /*
2  * Copyright (C) 2018 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 #ifndef __MESH_ACCESS_H
39 #define __MESH_ACCESS_H
40 
41 #include <stdint.h>
42 #include <stdarg.h>
43 
44 #include "bluetooth_company_id.h"
45 #include "btstack_linked_list.h"
46 
47 #include "mesh/mesh_lower_transport.h"
48 #include "mesh/mesh_keys.h"
49 #include "mesh/mesh_node.h"
50 
51 #ifdef __cplusplus
52 extern "C"
53 {
54 #endif
55 
56 #define MESH_SEQUENCE_NUMBER_STORAGE_INTERVAL 1000
57 
58 typedef enum {
59     MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_100ms = 0x00u,
60     MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_1s,
61     MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_10s,
62     MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_10min
63 } mesh_default_transition_step_resolution_t;
64 
65 typedef enum {
66     MODEL_STATE_UPDATE_REASON_SET = 0x00u,
67     MODEL_STATE_UPDATE_REASON_TRANSITION_START,
68     // MODEL_STATE_UPDATE_REASON_TRANSITION_ACTIVE,
69     MODEL_STATE_UPDATE_REASON_TRANSITION_END,
70     MODEL_STATE_UPDATE_REASON_TRANSITION_ABORT,
71     // MODEL_STATE_UPDATE_REASON_BOUND_STATE,
72     MODEL_STATE_UPDATE_REASON_APPLICATION_CHANGE
73  } model_state_update_reason_t;
74 
75 typedef enum {
76     TRANSITION_START,
77     TRANSITION_UPDATE
78 } transition_event_t;
79 
80 typedef enum {
81     MESH_TRANSITION_STATE_IDLE,
82     MESH_TRANSITION_STATE_DELAYED,
83     MESH_TRANSITION_STATE_ACTIVE
84 } mesh_transition_state_t;
85 
86 typedef enum {
87     MODEL_STATE_ID_GENERIC_ON_OFF = (BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC << 16) | 0u,
88     MODEL_STATE_ID_GENERIC_LEVEL  = (BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC << 16) | 1u,
89 } model_state_id_t;
90 
91 #define MESH_MAX_NUM_FAULTS 5
92 
93 typedef struct {
94     // linked list item
95     btstack_linked_item_t item;
96     uint8_t  test_id;
97     uint16_t company_id;
98     uint16_t num_faults;
99     uint8_t  faults[MESH_MAX_NUM_FAULTS];
100 } mesh_fault_t;
101 
102 typedef struct {
103     // linked list item
104     btstack_linked_item_t item;
105     uint8_t  test_id;
106     uint16_t company_id;
107     uint16_t num_current_faults;
108     uint16_t num_registered_faults;
109     uint8_t  current_faults[MESH_MAX_NUM_FAULTS];
110     uint8_t  registered_faults[MESH_MAX_NUM_FAULTS];
111 } mesh_health_fault_t;
112 
113 typedef struct {
114     // linked list of mesh_health_fault items
115     btstack_linked_list_t faults;
116     uint8_t fast_period_divisor;
117 } mesh_health_state_t;
118 
119 typedef struct {
120     uint32_t opcode;
121     uint8_t * data;
122     uint16_t len;
123 } mesh_access_parser_state_t;
124 
125 typedef struct {
126     uint32_t     opcode;
127     const char * format;
128 } mesh_access_message_t;
129 
130 typedef enum {
131     MESH_TRANSACTION_STATUS_NEW = 0,
132     MESH_TRANSACTION_STATUS_RETRANSMISSION,
133     MESH_TRANSACTION_STATUS_DIFFERENT_DST_OR_SRC
134 } mesh_transaction_status_t;
135 
136 typedef struct mesh_transition {
137     btstack_linked_item_t item;
138 
139     mesh_transition_state_t state;
140 
141     uint8_t  transaction_identifier;
142     uint32_t transaction_timestamp_ms;
143     uint16_t src_address;
144     uint16_t dst_address;
145 
146     mesh_default_transition_step_resolution_t step_duration_ms;
147     uint32_t phase_start_ms;
148     uint32_t remaining_delay_time_ms;
149     uint32_t remaining_transition_time_ms;
150     // to send events and/or publish changes
151     mesh_model_t * mesh_model;
152 
153     // to execute transition
154     void (* transition_callback)(struct mesh_transition * transition, transition_event_t event, uint32_t current_timestamp);
155 } mesh_transition_t;
156 
157 /**
158  * @brief Init access layer
159  */
160 void mesh_access_init(void);
161 
162 /**
163  * @brief Inform access layer that access message was processed by higher layer
164  * @param pdu
165  */
166 void mesh_access_message_processed(mesh_pdu_t * pdu);
167 
168 /**
169  * @brief Get number of retransmissions used by default
170  */
171 uint8_t mesh_access_acknowledged_message_retransmissions(void);
172 
173 /**
174  * @brief Get retransmission timeout
175  */
176 uint32_t mesh_access_acknowledged_message_timeout_ms(void);
177 
178 /**
179  * @brief Send unacknowledged message
180  * @param pdu
181  */
182 void mesh_access_send_unacknowledged_pdu(mesh_pdu_t * pdu);
183 
184 /**
185  * @brief Send acknowledged message. Retransmits message if no acknowledgement with expected opcode is received
186  * @param pdu
187  * @param retransmissions
188  * @param ack_opcode opcode of acknowledgement
189  */
190 void mesh_access_send_acknowledged_pdu(mesh_pdu_t * pdu, uint8_t retransmissions, uint32_t ack_opcode);
191 
192 uint8_t mesh_access_transitions_num_steps_from_gdtt(uint8_t time_gdtt);
193 uint32_t mesh_access_time_gdtt2ms(uint8_t time_gdtt);
194 
195 void mesh_access_emit_state_update_bool(btstack_packet_handler_t event_handler, uint8_t element_index, uint32_t model_identifier,
196     model_state_id_t state_identifier, model_state_update_reason_t reason, uint8_t value);
197 void mesh_access_emit_state_update_int16(btstack_packet_handler_t event_handler, uint8_t element_index, uint32_t model_identifier,
198     model_state_id_t state_identifier, model_state_update_reason_t reason, int16_t value);
199 
200 // Mesh Model Transitions
201 void mesh_access_transitions_setup_transaction(mesh_transition_t * transition, uint8_t transaction_identifier, uint16_t src_address, uint16_t dst_address);
202 void mesh_access_transitions_abort_transaction(mesh_transition_t * transition);
203 mesh_transaction_status_t mesh_access_transitions_transaction_status(mesh_transition_t * transition, uint8_t transaction_identifier, uint16_t src_address, uint16_t dst_address);
204 
205 void mesh_access_transitions_setup(mesh_transition_t * transition, mesh_model_t * mesh_model,
206     uint8_t transition_time_gdtt, uint8_t delay_gdtt,
207     void (* transition_callback)(struct mesh_transition * transition, transition_event_t event, uint32_t current_timestamp));
208 
209 void mesh_access_transitions_add(mesh_transition_t * transition);
210 void mesh_access_transitions_remove(mesh_transition_t * transition);
211 uint8_t mesh_access_transactions_get_next_transaction_id(void);
212 
213 // Mesh Model Publicaation
214 
215 /**
216  * Inform Mesh Access that the state of a model has changed. may trigger state publication
217  * @param mesh_model
218  */
219 void mesh_access_state_changed(mesh_model_t * mesh_model);
220 
221 /**
222  * Start Model Publication
223  * @param mesh_model
224  */
225 void mesh_model_publication_start(mesh_model_t * mesh_model);
226 
227 /**
228  * Stop Model Publication
229  * @param mesh_model
230  */
231 void mesh_model_publication_stop(mesh_model_t * mesh_model);
232 
233 // Mesh PDU Getter
234 uint16_t mesh_pdu_ctl(mesh_pdu_t * pdu);
235 uint16_t mesh_pdu_ttl(mesh_pdu_t * pdu);
236 uint16_t mesh_pdu_src(mesh_pdu_t * pdu);
237 uint16_t mesh_pdu_dst(mesh_pdu_t * pdu);
238 uint16_t mesh_pdu_netkey_index(mesh_pdu_t * pdu);
239 uint16_t mesh_pdu_appkey_index(mesh_pdu_t * pdu);
240 uint16_t mesh_pdu_len(mesh_pdu_t * pdu);
241 uint8_t * mesh_pdu_data(mesh_pdu_t * pdu);
242 uint8_t  mesh_pdu_control_opcode(mesh_pdu_t * pdu);
243 
244 // Mesh Access Parser
245 int mesh_access_pdu_get_opcode(mesh_pdu_t * pdu, uint32_t * opcode, uint16_t * opcode_size);
246 int  mesh_access_parser_init(mesh_access_parser_state_t * state, mesh_pdu_t * pdu);
247 void mesh_access_parser_skip(mesh_access_parser_state_t * state, uint16_t bytes_to_skip);
248 uint16_t mesh_access_parser_available(mesh_access_parser_state_t * state);
249 uint8_t mesh_access_parser_get_u8(mesh_access_parser_state_t * state);
250 uint16_t mesh_access_parser_get_u16(mesh_access_parser_state_t * state);
251 uint32_t mesh_access_parser_get_u24(mesh_access_parser_state_t * state);
252 uint32_t mesh_access_parser_get_u32(mesh_access_parser_state_t * state);
253 void mesh_access_parser_get_u128(mesh_access_parser_state_t * state, uint8_t * dest);
254 void mesh_access_parser_get_label_uuid(mesh_access_parser_state_t * state, uint8_t * dest);
255 void mesh_access_parser_get_key(mesh_access_parser_state_t * state, uint8_t * dest);
256 uint32_t mesh_access_parser_get_model_identifier(mesh_access_parser_state_t * parser);
257 
258 // message builder transport
259 mesh_transport_pdu_t * mesh_access_transport_init(uint32_t opcode);
260 void mesh_access_transport_add_uint8(mesh_transport_pdu_t * pdu, uint8_t value);
261 void mesh_access_transport_add_uint16(mesh_transport_pdu_t * pdu, uint16_t value);
262 void mesh_access_transport_add_uint24(mesh_transport_pdu_t * pdu, uint32_t value);
263 void mesh_access_transport_add_uint32(mesh_transport_pdu_t * pdu, uint32_t value);
264 void mesh_access_transport_add_model_identifier(mesh_transport_pdu_t * pdu, uint32_t model_identifier);
265 
266 // message builder network
267 mesh_network_pdu_t * mesh_access_network_init(uint32_t opcode);
268 void mesh_access_network_add_uint8(mesh_network_pdu_t * pdu, uint8_t value);
269 void mesh_access_network_add_uint16(mesh_network_pdu_t * pdu, uint16_t value);
270 void mesh_access_network_add_uint24(mesh_network_pdu_t * pdu, uint16_t value);
271 void mesh_access_network_add_uint32(mesh_network_pdu_t * pdu, uint16_t value);
272 void mesh_access_network_add_model_identifier(mesh_network_pdu_t * pdu, uint32_t model_identifier);
273 
274 // message builder using template
275 mesh_network_pdu_t * mesh_access_setup_unsegmented_message(const mesh_access_message_t *message_template, ...);
276 mesh_transport_pdu_t * mesh_access_setup_segmented_message(const mesh_access_message_t *message_template, ...);
277 
278 #ifdef __cplusplus
279 } /* end of extern "C" */
280 #endif
281 
282 #endif
283