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