1 /*
2  * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _ESP_BLE_MESH_HEALTH_MODEL_API_H_
8 #define _ESP_BLE_MESH_HEALTH_MODEL_API_H_
9 
10 #include "esp_ble_mesh_defs.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 /** @def    ESP_BLE_MESH_MODEL_HEALTH_SRV
17  *
18  *  @brief  Define a new Health Server Model.
19  *
20  *  @note   The Health Server Model can only be included by a Primary Element.
21  *
22  *  @param  srv Pointer to the unique struct esp_ble_mesh_health_srv_t.
23  *  @param  pub Pointer to the unique struct esp_ble_mesh_model_pub_t.
24  *
25  *  @return New Health Server Model instance.
26  */
27 #define ESP_BLE_MESH_MODEL_HEALTH_SRV(srv, pub)                           \
28         ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_HEALTH_SRV,          \
29                            NULL, pub, srv)
30 
31 /** @def    ESP_BLE_MESH_MODEL_HEALTH_CLI
32  *
33  *  @brief  Define a new Health Client Model.
34  *
35  *  @note   This API needs to be called for each element on which
36  *          the application needs to have a Health Client Model.
37  *
38  *  @param  cli_data Pointer to the unique struct esp_ble_mesh_client_t.
39  *
40  *  @return New Health Client Model instance.
41  */
42 #define ESP_BLE_MESH_MODEL_HEALTH_CLI(cli_data)                           \
43         ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_HEALTH_CLI,          \
44                            NULL, NULL, cli_data)
45 
46 /** @def ESP_BLE_MESH_HEALTH_PUB_DEFINE
47  *
48  *  A helper to define a health publication context
49  *
50  *  @param _name Name given to the publication context variable.
51  *  @param _max  Maximum number of faults the element can have.
52  *  @param _role Role of the device which contains the model.
53  */
54 #define ESP_BLE_MESH_HEALTH_PUB_DEFINE(_name, _max, _role) \
55         ESP_BLE_MESH_MODEL_PUB_DEFINE(_name, (1 + 3 + (_max)), _role)
56 
57 /**
58  * SIG identifier of Health Fault Test.
59  * 0x01 ~ 0xFF: Vendor Specific Test.
60  */
61 #define ESP_BLE_MESH_HEALTH_STANDARD_TEST               0x00
62 
63 /**
64  * Fault values of Health Fault Test.
65  * 0x33 ~ 0x7F: Reserved for Future Use.
66  * 0x80 ~ 0xFF: Vendor Specific Warning/Error.
67  */
68 #define ESP_BLE_MESH_NO_FAULT                           0x00
69 #define ESP_BLE_MESH_BATTERY_LOW_WARNING                0x01
70 #define ESP_BLE_MESH_BATTERY_LOW_ERROR                  0x02
71 #define ESP_BLE_MESH_SUPPLY_VOLTAGE_TOO_LOW_WARNING     0x03
72 #define ESP_BLE_MESH_SUPPLY_VOLTAGE_TOO_LOW_ERROR       0x04
73 #define ESP_BLE_MESH_SUPPLY_VOLTAGE_TOO_HIGH_WARNING    0x05
74 #define ESP_BLE_MESH_SUPPLY_VOLTAGE_TOO_HIGH_ERROR      0x06
75 #define ESP_BLE_MESH_POWER_SUPPLY_INTERRUPTED_WARNING   0x07
76 #define ESP_BLE_MESH_POWER_SUPPLY_INTERRUPTED_ERROR     0x08
77 #define ESP_BLE_MESH_NO_LOAD_WARNING                    0x09
78 #define ESP_BLE_MESH_NO_LOAD_ERROR                      0x0A
79 #define ESP_BLE_MESH_OVERLOAD_WARNING                   0x0B
80 #define ESP_BLE_MESH_OVERLOAD_ERROR                     0x0C
81 #define ESP_BLE_MESH_OVERHEAT_WARNING                   0x0D
82 #define ESP_BLE_MESH_OVERHEAT_ERROR                     0x0E
83 #define ESP_BLE_MESH_CONDENSATION_WARNING               0x0F
84 #define ESP_BLE_MESH_CONDENSATION_ERROR                 0x10
85 #define ESP_BLE_MESH_VIBRATION_WARNING                  0x11
86 #define ESP_BLE_MESH_VIBRATION_ERROR                    0x12
87 #define ESP_BLE_MESH_CONFIGURATION_WARNING              0x13
88 #define ESP_BLE_MESH_CONFIGURATION_ERROR                0x14
89 #define ESP_BLE_MESH_ELEMENT_NOT_CALIBRATED_WARNING     0x15
90 #define ESP_BLE_MESH_ELEMENT_NOT_CALIBRATED_ERROR       0x16
91 #define ESP_BLE_MESH_MEMORY_WARNING                     0x17
92 #define ESP_BLE_MESH_MEMORY_ERROR                       0x18
93 #define ESP_BLE_MESH_SELF_TEST_WARNING                  0x19
94 #define ESP_BLE_MESH_SELF_TEST_ERROR                    0x1A
95 #define ESP_BLE_MESH_INPUT_TOO_LOW_WARNING              0x1B
96 #define ESP_BLE_MESH_INPUT_TOO_LOW_ERROR                0x1C
97 #define ESP_BLE_MESH_INPUT_TOO_HIGH_WARNING             0x1D
98 #define ESP_BLE_MESH_INPUT_TOO_HIGH_ERROR               0x1E
99 #define ESP_BLE_MESH_INPUT_NO_CHANGE_WARNING            0x1F
100 #define ESP_BLE_MESH_INPUT_NO_CHANGE_ERROR              0x20
101 #define ESP_BLE_MESH_ACTUATOR_BLOCKED_WARNING           0x21
102 #define ESP_BLE_MESH_ACTUATOR_BLOCKED_ERROR             0x22
103 #define ESP_BLE_MESH_HOUSING_OPENED_WARNING             0x23
104 #define ESP_BLE_MESH_HOUSING_OPENED_ERROR               0x24
105 #define ESP_BLE_MESH_TAMPER_WARNING                     0x25
106 #define ESP_BLE_MESH_TAMPER_ERROR                       0x26
107 #define ESP_BLE_MESH_DEVICE_MOVED_WARNING               0x27
108 #define ESP_BLE_MESH_DEVICE_MOVED_ERROR                 0x28
109 #define ESP_BLE_MESH_DEVICE_DROPPED_WARNING             0x29
110 #define ESP_BLE_MESH_DEVICE_DROPPED_ERROR               0x2A
111 #define ESP_BLE_MESH_OVERFLOW_WARNING                   0x2B
112 #define ESP_BLE_MESH_OVERFLOW_ERROR                     0x2C
113 #define ESP_BLE_MESH_EMPTY_WARNING                      0x2D
114 #define ESP_BLE_MESH_EMPTY_ERROR                        0x2E
115 #define ESP_BLE_MESH_INTERNAL_BUS_WARNING               0x2F
116 #define ESP_BLE_MESH_INTERNAL_BUS_ERROR                 0x30
117 #define ESP_BLE_MESH_MECHANISM_JAMMED_WARNING           0x31
118 #define ESP_BLE_MESH_MECHANISM_JAMMED_ERROR             0x32
119 
120 /** ESP BLE Mesh Health Server callback */
121 typedef struct {
122     /** Clear health registered faults. Initialized by the stack. */
123     esp_ble_mesh_cb_t fault_clear;
124 
125     /** Run a specific health test. Initialized by the stack. */
126     esp_ble_mesh_cb_t fault_test;
127 
128     /** Health attention on callback. Initialized by the stack. */
129     esp_ble_mesh_cb_t attention_on;
130 
131     /** Health attention off callback. Initialized by the stack. */
132     esp_ble_mesh_cb_t attention_off;
133 } esp_ble_mesh_health_srv_cb_t;
134 
135 #define ESP_BLE_MESH_HEALTH_FAULT_ARRAY_SIZE    32
136 
137 /** ESP BLE Mesh Health Server test Context */
138 typedef struct {
139     uint8_t  id_count;          /*!< Number of Health self-test ID */
140     const uint8_t *test_ids;    /*!< Array of Health self-test IDs */
141     uint16_t company_id;        /*!< Company ID used to identify the Health Fault state */
142     uint8_t  prev_test_id;      /*!< Current test ID of the health fault test */
143     uint8_t  current_faults[ESP_BLE_MESH_HEALTH_FAULT_ARRAY_SIZE];      /*!< Array of current faults */
144     uint8_t  registered_faults[ESP_BLE_MESH_HEALTH_FAULT_ARRAY_SIZE];   /*!< Array of registered faults */
145 } __attribute__((packed)) esp_ble_mesh_health_test_t;
146 
147 /** ESP BLE Mesh Health Server Model Context */
148 typedef struct {
149     /** Pointer to Health Server Model */
150     esp_ble_mesh_model_t *model;
151 
152     /** Health callback struct */
153     esp_ble_mesh_health_srv_cb_t health_cb;
154 
155     /** Attention Timer state */
156     struct k_delayed_work attention_timer;
157 
158     /** Attention Timer start flag */
159     bool attention_timer_start;
160 
161     /** Health Server fault test */
162     esp_ble_mesh_health_test_t health_test;
163 } esp_ble_mesh_health_srv_t;
164 
165 /** Parameter of Health Fault Get */
166 typedef struct {
167     uint16_t company_id;    /*!< Bluetooth assigned 16-bit Company ID */
168 } esp_ble_mesh_health_fault_get_t;
169 
170 /** Parameter of Health Attention Set */
171 typedef struct {
172     uint8_t attention;      /*!< Value of the Attention Timer state */
173 } esp_ble_mesh_health_attention_set_t;
174 
175 /** Parameter of Health Period Set */
176 typedef struct {
177     uint8_t fast_period_divisor;    /*!< Divider for the Publish Period */
178 } esp_ble_mesh_health_period_set_t;
179 
180 /** Parameter of Health Fault Test */
181 typedef struct {
182     uint16_t company_id;    /*!< Bluetooth assigned 16-bit Company ID */
183     uint8_t  test_id;       /*!< ID of a specific test to be performed */
184 } esp_ble_mesh_health_fault_test_t;
185 
186 /** Parameter of Health Fault Clear */
187 typedef struct {
188     uint16_t company_id;    /*!< Bluetooth assigned 16-bit Company ID */
189 } esp_ble_mesh_health_fault_clear_t;
190 
191 /**
192  * @brief For ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET
193  *            ESP_BLE_MESH_MODEL_OP_ATTENTION_GET
194  *            ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_GET
195  * the get_state parameter in the esp_ble_mesh_health_client_get_state function should not be set to NULL.
196  */
197 typedef union {
198     esp_ble_mesh_health_fault_get_t fault_get;          /*!< For ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET. */
199 } esp_ble_mesh_health_client_get_state_t;
200 
201 /**
202  * @brief For ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR
203  *            ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR_UNACK
204  *            ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST
205  *            ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST_UNACK
206  *            ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET
207  *            ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET_UNACK
208  *            ESP_BLE_MESH_MODEL_OP_ATTENTION_SET
209  *            ESP_BLE_MESH_MODEL_OP_ATTENTION_SET_UNACK
210  * the set_state parameter in the esp_ble_mesh_health_client_set_state function should not be set to NULL.
211  */
212 typedef union {
213     esp_ble_mesh_health_attention_set_t attention_set;    /*!< For ESP_BLE_MESH_MODEL_OP_ATTENTION_SET or ESP_BLE_MESH_MODEL_OP_ATTENTION_SET_UNACK. */
214     esp_ble_mesh_health_period_set_t    period_set;       /*!< For ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET or ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET_UNACK. */
215     esp_ble_mesh_health_fault_test_t    fault_test;       /*!< For ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST or ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST_UNACK. */
216     esp_ble_mesh_health_fault_clear_t   fault_clear;      /*!< For ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR or ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR_UNACK. */
217 } esp_ble_mesh_health_client_set_state_t;
218 
219 /** Parameters of Health Current Status */
220 typedef struct {
221     uint8_t  test_id;                       /*!< ID of a most recently performed test */
222     uint16_t company_id;                    /*!< Bluetooth assigned 16-bit Company ID */
223     struct net_buf_simple *fault_array;     /*!< FaultArray field contains a sequence of 1-octet fault values */
224 } esp_ble_mesh_health_current_status_cb_t;
225 
226 /** Parameters of Health Fault Status */
227 typedef struct {
228     uint8_t  test_id;                       /*!< ID of a most recently performed test */
229     uint16_t company_id;                    /*!< Bluetooth assigned 16-bit Company ID */
230     struct net_buf_simple *fault_array;     /*!< FaultArray field contains a sequence of 1-octet fault values */
231 } esp_ble_mesh_health_fault_status_cb_t;
232 
233 /** Parameter of Health Period Status */
234 typedef struct {
235     uint8_t fast_period_divisor;            /*!< Divider for the Publish Period */
236 } esp_ble_mesh_health_period_status_cb_t;
237 
238 /** Parameter of Health Attention Status */
239 typedef struct {
240     uint8_t attention;                      /*!< Value of the Attention Timer state */
241 } esp_ble_mesh_health_attention_status_cb_t;
242 
243 /**
244  * @brief Health Client Model received message union
245  */
246 typedef union {
247     esp_ble_mesh_health_current_status_cb_t   current_status;       /*!< The health current status value */
248     esp_ble_mesh_health_fault_status_cb_t     fault_status;         /*!< The health fault status value */
249     esp_ble_mesh_health_period_status_cb_t    period_status;        /*!< The health period status value */
250     esp_ble_mesh_health_attention_status_cb_t attention_status;     /*!< The health attention status value */
251 } esp_ble_mesh_health_client_common_cb_param_t;
252 
253 /** Health Client Model callback parameters */
254 typedef struct {
255     int error_code;                                         /*!< Appropriate error code */
256     esp_ble_mesh_client_common_param_t          *params;    /*!< The client common parameters. */
257     esp_ble_mesh_health_client_common_cb_param_t status_cb; /*!< The health message status callback values */
258 } esp_ble_mesh_health_client_cb_param_t;
259 
260 /** This enum value is the event of Health Client Model */
261 typedef enum {
262     ESP_BLE_MESH_HEALTH_CLIENT_GET_STATE_EVT,
263     ESP_BLE_MESH_HEALTH_CLIENT_SET_STATE_EVT,
264     ESP_BLE_MESH_HEALTH_CLIENT_PUBLISH_EVT,
265     ESP_BLE_MESH_HEALTH_CLIENT_TIMEOUT_EVT,
266     ESP_BLE_MESH_HEALTH_CLIENT_EVT_MAX,
267 } esp_ble_mesh_health_client_cb_event_t;
268 
269 /** Parameter of publishing Health Current Status completion event */
270 typedef struct {
271     int error_code;                 /*!< The result of publishing Health Current Status */
272     esp_ble_mesh_elem_t *element;   /*!< Pointer to the element which contains the Health Server Model */
273 } esp_ble_mesh_health_fault_update_comp_cb_t;
274 
275 /** Parameters of Health Fault Clear event */
276 typedef struct {
277     esp_ble_mesh_model_t *model;    /*!< Pointer to the Health Server Model */
278     uint16_t company_id;            /*!< Bluetooth assigned 16-bit Company ID */
279 } esp_ble_mesh_health_fault_clear_cb_t;
280 
281 /** Parameters of Health Fault Test event */
282 typedef struct {
283     esp_ble_mesh_model_t *model;    /*!< Pointer to the Health Server Model */
284     uint8_t  test_id;               /*!< ID of a specific test to be performed */
285     uint16_t company_id;            /*!< Bluetooth assigned 16-bit Company ID */
286 } esp_ble_mesh_health_fault_test_cb_t;
287 
288 /** Parameter of Health Attention On event */
289 typedef struct {
290     esp_ble_mesh_model_t *model;    /*!< Pointer to the Health Server Model */
291     uint8_t time;                   /*!< Duration of attention timer on (in seconds) */
292 } esp_ble_mesh_health_attention_on_cb_t;
293 
294 /** Parameter of Health Attention Off event */
295 typedef struct {
296     esp_ble_mesh_model_t *model;    /*!< Pointer to the Health Server Model */
297 } esp_ble_mesh_health_attention_off_cb_t;
298 
299 /**
300  * @brief Health Server Model callback parameters union
301  */
302 typedef union {
303     esp_ble_mesh_health_fault_update_comp_cb_t  fault_update_comp;  /*!< ESP_BLE_MESH_HEALTH_SERVER_FAULT_UPDATE_COMP_EVT */
304     esp_ble_mesh_health_fault_clear_cb_t        fault_clear;        /*!< ESP_BLE_MESH_HEALTH_SERVER_FAULT_CLEAR_EVT */
305     esp_ble_mesh_health_fault_test_cb_t         fault_test;         /*!< ESP_BLE_MESH_HEALTH_SERVER_FAULT_TEST_EVT */
306     esp_ble_mesh_health_attention_on_cb_t       attention_on;       /*!< ESP_BLE_MESH_HEALTH_SERVER_ATTENTION_ON_EVT */
307     esp_ble_mesh_health_attention_off_cb_t      attention_off;      /*!< ESP_BLE_MESH_HEALTH_SERVER_ATTENTION_OFF_EVT */
308 } esp_ble_mesh_health_server_cb_param_t;
309 
310 /** This enum value is the event of Health Server Model */
311 typedef enum {
312     ESP_BLE_MESH_HEALTH_SERVER_FAULT_UPDATE_COMP_EVT,
313     ESP_BLE_MESH_HEALTH_SERVER_FAULT_CLEAR_EVT,
314     ESP_BLE_MESH_HEALTH_SERVER_FAULT_TEST_EVT,
315     ESP_BLE_MESH_HEALTH_SERVER_ATTENTION_ON_EVT,
316     ESP_BLE_MESH_HEALTH_SERVER_ATTENTION_OFF_EVT,
317     ESP_BLE_MESH_HEALTH_SERVER_EVT_MAX,
318 } esp_ble_mesh_health_server_cb_event_t;
319 
320 /**
321  *  @brief Bluetooth Mesh Health Client and Server Model function.
322  */
323 
324 /**
325  * @brief   Health Client Model callback function type
326  * @param   event: Event type
327  * @param   param: Pointer to callback parameter
328  */
329 typedef void (* esp_ble_mesh_health_client_cb_t)(esp_ble_mesh_health_client_cb_event_t event,
330                                                  esp_ble_mesh_health_client_cb_param_t *param);
331 
332 /**
333  * @brief   Health Server Model callback function type
334  * @param   event: Event type
335  * @param   param: Pointer to callback parameter
336  */
337 typedef void (* esp_ble_mesh_health_server_cb_t)(esp_ble_mesh_health_server_cb_event_t event,
338                                                  esp_ble_mesh_health_server_cb_param_t *param);
339 
340 /**
341  * @brief         Register BLE Mesh Health Model callback, the callback will report Health Client & Server Model events.
342  *
343  * @param[in]     callback: Pointer to the callback function.
344  *
345  * @return        ESP_OK on success or error code otherwise.
346  *
347  */
348 esp_err_t esp_ble_mesh_register_health_client_callback(esp_ble_mesh_health_client_cb_t callback);
349 
350 /**
351  * @brief         Register BLE Mesh Health Server Model callback.
352  *
353  * @param[in]     callback: Pointer to the callback function.
354  *
355  * @return        ESP_OK on success or error code otherwise.
356  *
357  */
358 esp_err_t esp_ble_mesh_register_health_server_callback(esp_ble_mesh_health_server_cb_t callback);
359 
360 /**
361  * @brief         This function is called to get the Health Server states using the Health Client Model get messages.
362  *
363  * @note          If you want to find the opcodes and corresponding meanings accepted by this API,
364  *                please refer to esp_ble_mesh_opcode_health_client_get_t in esp_ble_mesh_defs.h
365  *
366  * @param[in]     params:    Pointer to BLE Mesh common client parameters.
367  * @param[in]     get_state: Pointer to a union, each kind of opcode corresponds to one structure inside.
368  *                           Shall not be set to NULL.
369  *
370  * @return        ESP_OK on success or error code otherwise.
371  *
372  */
373 esp_err_t esp_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param_t *params,
374                                                esp_ble_mesh_health_client_get_state_t *get_state);
375 
376 /**
377  * @brief         This function is called to set the Health Server states using the Health Client Model set messages.
378  *
379  * @note          If you want to find the opcodes and corresponding meanings accepted by this API,
380  *                please refer to esp_ble_mesh_opcode_health_client_set_t in esp_ble_mesh_defs.h
381  *
382  * @param[in]     params:    Pointer to BLE Mesh common client parameters.
383  * @param[in]     set_state: Pointer to a union, each kind of opcode corresponds to one structure inside.
384  *                           Shall not be set to NULL.
385  *
386  * @return        ESP_OK on success or error code otherwise.
387  *
388  */
389 esp_err_t esp_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param_t *params,
390                                                esp_ble_mesh_health_client_set_state_t *set_state);
391 
392 /**
393  * @brief         This function is called by the Health Server Model to update the context of its Health Current status.
394  *
395  * @param[in]     element: The element to which the Health Server Model belongs.
396  *
397  * @return        ESP_OK on success or error code otherwise.
398  *
399  */
400 esp_err_t esp_ble_mesh_health_server_fault_update(esp_ble_mesh_elem_t *element);
401 
402 #ifdef __cplusplus
403 }
404 #endif
405 
406 #endif /* _ESP_BLE_MESH_HEALTH_MODEL_API_H_ */
407