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_default_transition_time_client.c"
39
40 #include "mesh/mesh_generic_default_transition_time_client.h"
41
42 #include <string.h>
43 #include <stdio.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 Default Transition Time Message
58
59 static const mesh_access_message_t mesh_generic_default_transition_time_set = {
60 MESH_GENERIC_DEFAULT_TRANSITION_TIME_SET, "1"
61 };
62
63 static const mesh_access_message_t mesh_generic_default_transition_time_set_unacknowledged = {
64 MESH_GENERIC_DEFAULT_TRANSITION_TIME_SET_UNACKNOWLEDGED, "1"
65 };
66
67 static const mesh_access_message_t mesh_generic_default_transition_time_get = {
68 MESH_GENERIC_DEFAULT_TRANSITION_TIME_GET, ""
69 };
70
71
72 // Generic Default Transition Time Client Functions
73
generic_client_send_message_unacknowledged(uint16_t src,uint16_t dest,uint16_t netkey_index,uint16_t appkey_index,mesh_pdu_t * pdu)74 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){
75 uint8_t ttl = mesh_foundation_default_ttl_get();
76 mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0);
77 mesh_access_send_unacknowledged_pdu(pdu);
78 }
79
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)80 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){
81 uint8_t ttl = mesh_foundation_default_ttl_get();
82 mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0);
83 mesh_access_send_acknowledged_pdu(pdu, mesh_access_acknowledged_message_retransmissions(), ack_opcode);
84 }
85
mesh_generic_default_transition_time_client_get(mesh_model_t * mesh_model,uint16_t dest,uint16_t netkey_index,uint16_t appkey_index)86 uint8_t mesh_generic_default_transition_time_client_get(mesh_model_t *mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){
87 // setup message
88 mesh_upper_transport_pdu_t * transport_pdu = mesh_access_setup_message(&mesh_generic_default_transition_time_get);
89 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
90 // send as segmented access pdu
91 generic_client_send_message_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_GENERIC_DEFAULT_TRANSITION_TIME_STATUS);
92 return ERROR_CODE_SUCCESS;
93 }
94
mesh_generic_default_transition_time_client_set(mesh_model_t * mesh_model,uint16_t dest,uint16_t netkey_index,uint16_t appkey_index,uint8_t transition_time_gdtt)95 uint8_t mesh_generic_default_transition_time_client_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index,
96 uint8_t transition_time_gdtt){
97
98 mesh_upper_transport_pdu_t * transport_pdu;
99
100 transport_pdu = mesh_access_setup_message(&mesh_generic_default_transition_time_set, transition_time_gdtt);
101
102 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
103
104 generic_client_send_message_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_GENERIC_DEFAULT_TRANSITION_TIME_STATUS);
105 return ERROR_CODE_SUCCESS;
106 }
107
mesh_generic_default_transition_time_client_set_unacknowledged(mesh_model_t * mesh_model,uint16_t dest,uint16_t netkey_index,uint16_t appkey_index,uint8_t transition_time_gdtt)108 uint8_t mesh_generic_default_transition_time_client_set_unacknowledged(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index,
109 uint8_t transition_time_gdtt){
110 mesh_upper_transport_pdu_t * transport_pdu;
111 transport_pdu = mesh_access_setup_message(&mesh_generic_default_transition_time_set_unacknowledged, transition_time_gdtt);
112
113 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED;
114 generic_client_send_message_unacknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu);
115 return ERROR_CODE_SUCCESS;
116 }
117
118 // Model Operations
119
generic_default_transition_time_status_handler(mesh_model_t * mesh_model,mesh_pdu_t * pdu)120 static void generic_default_transition_time_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
121 if (!mesh_model->model_packet_handler){
122 log_error("model_packet_handler == NULL");
123 }
124
125 mesh_access_parser_state_t parser;
126 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
127
128 uint8_t transition_time_gdtt = mesh_access_parser_get_uint8(&parser);
129
130 uint8_t event[7] = {HCI_EVENT_MESH_META, 5, MESH_SUBEVENT_GENERIC_DEFAULT_TRANSITION_TIME};
131 int pos = 3;
132 // dest
133 little_endian_store_16(event, pos, mesh_pdu_src(pdu));
134 pos += 2;
135 event[pos++] = ERROR_CODE_SUCCESS;
136 event[pos++] = transition_time_gdtt;
137
138 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
139 mesh_access_message_processed(pdu);
140 }
141
142 static const mesh_operation_t mesh_generic_default_transition_time_model_operations[] = {
143 { MESH_GENERIC_DEFAULT_TRANSITION_TIME_STATUS, 0, generic_default_transition_time_status_handler },
144 { 0, 0, NULL }
145 };
146
mesh_generic_default_transition_time_client_get_operations(void)147 const mesh_operation_t * mesh_generic_default_transition_time_client_get_operations(void){
148 return mesh_generic_default_transition_time_model_operations;
149 }
150
mesh_generic_default_transition_time_client_register_packet_handler(mesh_model_t * mesh_model,btstack_packet_handler_t transition_events_packet_handler)151 void mesh_generic_default_transition_time_client_register_packet_handler(mesh_model_t *mesh_model, btstack_packet_handler_t transition_events_packet_handler){
152 if (transition_events_packet_handler == NULL){
153 log_error("mesh_generic_default_transition_time_client_register_packet_handler called with NULL callback");
154 return;
155 }
156 if (mesh_model == NULL){
157 log_error("mesh_generic_default_transition_time_client_register_packet_handler called with NULL mesh_model");
158 return;
159 }
160 mesh_model->model_packet_handler = transition_events_packet_handler;
161 }
162