xref: /btstack/src/mesh/mesh_health_server.c (revision a5a7b6da93a734de273f68a4770fffd1d7ae3888)
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_generic_server.c"
39 
40 #include <string.h>
41 #include <stdio.h>
42 
43 #include "mesh/mesh_health_server.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_generic_server.h"
54 #include "mesh/mesh_keys.h"
55 #include "mesh/mesh_network.h"
56 #include "mesh/mesh_upper_transport.h"
57 
58 static void health_server_send_message(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 // Health State
64 const mesh_access_message_t mesh_foundation_health_period_status = {
65         MESH_FOUNDATION_OPERATION_HEALTH_PERIOD_STATUS, "1"
66 };
67 
68 const mesh_access_message_t mesh_foundation_health_attention_status = {
69         MESH_FOUNDATION_OPERATION_ATTENTION_STATUS, "1"
70 };
71 
72 static mesh_pdu_t * health_status(mesh_model_t * mesh_model, uint32_t opcode, uint16_t company_id){
73     if (mesh_model->element == NULL){
74         log_error("mesh_model->element == NULL");
75     }
76 
77     mesh_health_state_t * state = (mesh_health_state_t *) mesh_model->model_data;
78     if (state == NULL){
79         log_error("mesh_health_state ==  NULL");
80     }
81 
82     mesh_transport_pdu_t * transport_pdu = mesh_access_transport_init(opcode);
83     if (!transport_pdu) return NULL;
84 
85     btstack_linked_list_iterator_t it;
86     btstack_linked_list_iterator_init(&it, &state->registered_faults);
87     while (btstack_linked_list_iterator_has_next(&it)){
88         mesh_fault_t * fault = (mesh_fault_t *) btstack_linked_list_iterator_next(&it);
89         if (fault->company_id != company_id) continue;
90         mesh_access_transport_add_uint8(transport_pdu, fault->test_id);
91         mesh_access_transport_add_uint16(transport_pdu, fault->company_id);
92         int i;
93         for (i = 0; i < fault->num_faults; i++){
94              mesh_access_transport_add_uint8(transport_pdu, fault->faults[i]);
95         }
96         return (mesh_pdu_t *) transport_pdu;
97     }
98     // no company with company_id found
99     mesh_access_transport_add_uint8(transport_pdu, 0);
100     mesh_access_transport_add_uint16(transport_pdu, company_id);
101     return (mesh_pdu_t *) transport_pdu;
102 }
103 
104 static void health_fault_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
105     mesh_access_parser_state_t parser;
106     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
107     uint16_t cid = mesh_access_parser_get_u16(&parser);
108 
109     mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) health_status(mesh_model, MESH_FOUNDATION_OPERATION_HEALTH_FAULT_STATUS, cid);
110     if (!transport_pdu) return;
111     health_server_send_message(mesh_access_get_element_address(mesh_model), mesh_pdu_src(pdu), mesh_pdu_netkey_index(pdu), mesh_pdu_appkey_index(pdu),(mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_HEALTH_FAULT_STATUS);
112     mesh_access_message_processed(pdu);
113 }
114 
115 // Health Message
116 const static mesh_operation_t mesh_health_model_operations[] = {
117     { MESH_FOUNDATION_OPERATION_HEALTH_FAULT_GET,                                   2, health_fault_get_handler },
118     // { MESH_FOUNDATION_OPERATION_HEALTH_FAULT_CLEAR,                                 2, health_fault_clear_handler },
119     // { MESH_FOUNDATION_OPERATION_HEALTH_FAULT_CLEAR_UNACKNOWLEDGED,                  2, health_fault_clear_unacknowledged_handler },
120     // { MESH_FOUNDATION_OPERATION_HEALTH_FAULT_TEST,                                  3, health_fault_test_handler },
121     // { MESH_FOUNDATION_OPERATION_HEALTH_FAULT_TEST_UNACKNOWLEDGED,                   3, health_fault_test_unacknowledged_handler },
122     // { MESH_FOUNDATION_OPERATION_HEALTH_FAULT_STATUS_UNACKNOWLEDGED,                 3, health_fault_test_status_unacknowledged_handler },
123     // { MESH_FOUNDATION_OPERATION_HEALTH_PERIOD_GET,                                  2, health_period_get_handler },
124     // { MESH_FOUNDATION_OPERATION_HEALTH_PERIOD_SET,                                  2, health_period_set_handler },
125     // { MESH_FOUNDATION_OPERATION_HEALTH_PERIOD_SET_UNACKNOWLEDGED,                   2, health_period_set_unacknowledged_handler },
126     // { MESH_FOUNDATION_OPERATION_HEALTH_ATTENTION_GET,                               2, health_attention_get_handler },
127     // { MESH_FOUNDATION_OPERATION_HEALTH_ATTENTION_SET,                               2, health_attention_set_handler },
128     // { MESH_FOUNDATION_OPERATION_HEALTH_ATTENTION_SET_UNACKNOWLEDGED,                2, health_attention_set_unacknowledged_handler },
129     { 0, 0, NULL }
130 };
131 
132 const mesh_operation_t * mesh_health_server_get_operations(void){
133     return mesh_health_model_operations;
134 }
135 
136