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_on_off_server.c" 39 40 #include "mesh/mesh_generic_on_off_server.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 static void generic_server_send_message(uint16_t src, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, mesh_pdu_t *pdu){ 58 uint8_t ttl = mesh_foundation_default_ttl_get(); 59 mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0); 60 mesh_access_send_unacknowledged_pdu(pdu); 61 } 62 63 // Transaction management 64 65 static mesh_transition_t * generic_on_off_server_get_base_transition(mesh_model_t * mesh_model) { 66 mesh_generic_on_off_state_t * generic_on_off_server_state = (mesh_generic_on_off_state_t *)mesh_model->model_data; 67 return &generic_on_off_server_state->transition_data.base_transition; 68 } 69 70 static void mesh_server_transition_handler(mesh_transition_t * base_transition, model_state_update_reason_t event){ 71 mesh_transition_bool_t * transition = (mesh_transition_bool_t*) base_transition; 72 switch (event){ 73 case MODEL_STATE_UPDATE_REASON_TRANSITION_START: 74 if (transition->target_value == 1){ 75 transition->current_value = 1; 76 mesh_access_state_changed(transition->base_transition.mesh_model); 77 } 78 break; 79 case MODEL_STATE_UPDATE_REASON_SET: 80 case MODEL_STATE_UPDATE_REASON_TRANSITION_END: 81 transition->current_value = transition->target_value; 82 mesh_access_state_changed(transition->base_transition.mesh_model); 83 break; 84 default: 85 break; 86 } 87 88 // notify app 89 mesh_model_t * generic_on_off_server_model = transition->base_transition.mesh_model; 90 mesh_access_emit_state_update_bool(generic_on_off_server_model->model_packet_handler, 91 mesh_access_get_element_index(generic_on_off_server_model), 92 generic_on_off_server_model->model_identifier, 93 MODEL_STATE_ID_GENERIC_ON_OFF, 94 event, 95 transition->current_value); 96 } 97 98 static void mesh_server_transition_setup_transition_or_instantaneous_update(mesh_model_t *mesh_model, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt){ 99 mesh_generic_on_off_state_t * generic_on_off_server_state = (mesh_generic_on_off_state_t *)mesh_model->model_data; 100 mesh_transition_t * transition = &generic_on_off_server_state->transition_data.base_transition; 101 mesh_access_transition_setup(mesh_model, transition, transition_time_gdtt, delay_time_gdtt, &mesh_server_transition_handler); 102 } 103 104 // Generic On Off State 105 106 void mesh_generic_on_off_server_register_packet_handler(mesh_model_t *generic_on_off_server_model, btstack_packet_handler_t transition_events_packet_handler){ 107 btstack_assert(generic_on_off_server_model != NULL); 108 btstack_assert(transition_events_packet_handler != NULL); 109 generic_on_off_server_model->model_packet_handler = transition_events_packet_handler; 110 } 111 112 const mesh_access_message_t mesh_generic_on_off_status_transition = { 113 MESH_GENERIC_ON_OFF_STATUS, "111" 114 }; 115 116 const mesh_access_message_t mesh_generic_on_off_status_instantaneous = { 117 MESH_GENERIC_ON_OFF_STATUS, "1" 118 }; 119 120 static mesh_pdu_t * mesh_generic_on_off_status_message(mesh_model_t *generic_on_off_server_model){ 121 btstack_assert(generic_on_off_server_model->model_data != NULL); 122 123 mesh_generic_on_off_state_t * state = (mesh_generic_on_off_state_t *) generic_on_off_server_model->model_data; 124 // setup message 125 mesh_upper_transport_pdu_t * transport_pdu = NULL; 126 if (state->transition_data.base_transition.num_steps > 0) { 127 uint8_t remaining_time = (((uint8_t)state->transition_data.base_transition.step_resolution) << 6) | (state->transition_data.base_transition.num_steps); 128 transport_pdu = mesh_access_setup_message(&mesh_generic_on_off_status_transition, state->transition_data.current_value, 129 state->transition_data.target_value, remaining_time); 130 } else { 131 log_info("On/Off Status: value %u, no transition active", state->transition_data.current_value); 132 transport_pdu = mesh_access_setup_message(&mesh_generic_on_off_status_instantaneous, state->transition_data.current_value); 133 } 134 return (mesh_pdu_t *) transport_pdu; 135 } 136 137 static void generic_on_off_get_handler(mesh_model_t *generic_on_off_server_model, mesh_pdu_t * pdu){ 138 mesh_upper_transport_pdu_t * transport_pdu = (mesh_upper_transport_pdu_t *) mesh_generic_on_off_status_message(generic_on_off_server_model); 139 if (!transport_pdu) return; 140 generic_server_send_message(mesh_access_get_element_address(generic_on_off_server_model), mesh_pdu_src(pdu), mesh_pdu_netkey_index(pdu), mesh_pdu_appkey_index(pdu),(mesh_pdu_t *) transport_pdu); 141 mesh_access_message_processed(pdu); 142 } 143 144 // returns if set message was valid 145 static bool generic_on_off_handle_set_message(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 146 btstack_assert(mesh_model != NULL); 147 btstack_assert(mesh_model->model_data != NULL); 148 149 mesh_access_parser_state_t parser; 150 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 151 uint8_t on_off_value = mesh_access_parser_get_uint8(&parser); 152 153 // check for valid value 154 if (on_off_value > 1) return false; 155 156 // The TID field is a transaction identifier indicating whether the message is 157 // a new message or a retransmission of a previously sent message 158 mesh_generic_on_off_state_t * generic_on_off_server_state = (mesh_generic_on_off_state_t *)mesh_model->model_data; 159 uint8_t tid = mesh_access_parser_get_uint8(&parser); 160 uint8_t transition_time_gdtt = 0; 161 uint8_t delay_time_gdtt = 0; 162 163 mesh_transition_t * base_transition = generic_on_off_server_get_base_transition(mesh_model); 164 switch (mesh_access_transitions_transaction_status(base_transition, tid, mesh_pdu_src(pdu), mesh_pdu_dst(pdu))){ 165 case MESH_TRANSACTION_STATUS_RETRANSMISSION: 166 // ignore on retransmission 167 break; 168 default: 169 mesh_access_transitions_init_transaction(base_transition, tid, mesh_pdu_src(pdu), mesh_pdu_dst(pdu)); 170 generic_on_off_server_state->transition_data.target_value = on_off_value; 171 172 if (mesh_access_parser_available(&parser) == 2){ 173 // Generic Default Transition Time format - num_steps (higher 6 bits), step_resolution (lower 2 bits) 174 transition_time_gdtt = mesh_access_parser_get_uint8(&parser); 175 delay_time_gdtt = mesh_access_parser_get_uint8(&parser); 176 } 177 mesh_server_transition_setup_transition_or_instantaneous_update(mesh_model, transition_time_gdtt, delay_time_gdtt); 178 mesh_access_state_changed(mesh_model); 179 break; 180 } 181 return true; 182 } 183 184 static void generic_on_off_set_handler(mesh_model_t *generic_on_off_server_model, mesh_pdu_t * pdu){ 185 bool send_status = generic_on_off_handle_set_message(generic_on_off_server_model, pdu); 186 if (send_status){ 187 mesh_upper_transport_pdu_t * transport_pdu = (mesh_upper_transport_pdu_t *) mesh_generic_on_off_status_message(generic_on_off_server_model); 188 if (transport_pdu) { 189 generic_server_send_message(mesh_access_get_element_address(generic_on_off_server_model), mesh_pdu_src(pdu), mesh_pdu_netkey_index(pdu), mesh_pdu_appkey_index(pdu),(mesh_pdu_t *) transport_pdu); 190 } 191 } 192 mesh_access_message_processed(pdu); 193 } 194 195 static void generic_on_off_set_unacknowledged_handler(mesh_model_t *generic_on_off_server_model, mesh_pdu_t * pdu){ 196 generic_on_off_handle_set_message(generic_on_off_server_model, pdu); 197 mesh_access_message_processed(pdu); 198 } 199 200 // Generic On Off Message 201 static const mesh_operation_t mesh_generic_on_off_model_operations[] = { 202 { MESH_GENERIC_ON_OFF_GET, 0, generic_on_off_get_handler }, 203 { MESH_GENERIC_ON_OFF_SET, 2, generic_on_off_set_handler }, 204 { MESH_GENERIC_ON_OFF_SET_UNACKNOWLEDGED, 2, generic_on_off_set_unacknowledged_handler }, 205 { 0, 0, NULL } 206 }; 207 208 const mesh_operation_t * mesh_generic_on_off_server_get_operations(void){ 209 return mesh_generic_on_off_model_operations; 210 } 211 212 void mesh_generic_on_off_server_set(mesh_model_t * mesh_model, uint8_t on_off_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt){ 213 mesh_generic_on_off_state_t * generic_on_off_server_state = (mesh_generic_on_off_state_t *)mesh_model->model_data; 214 generic_on_off_server_state->transition_data.target_value = on_off_value; 215 216 mesh_server_transition_setup_transition_or_instantaneous_update(mesh_model, transition_time_gdtt, delay_time_gdtt); 217 mesh_access_state_changed(mesh_model); 218 } 219 220 uint8_t mesh_generic_on_off_server_get(mesh_model_t *generic_on_off_server_model){ 221 mesh_generic_on_off_state_t * generic_on_off_server_state = (mesh_generic_on_off_state_t *)generic_on_off_server_model->model_data; 222 return generic_on_off_server_state->transition_data.current_value; 223 } 224 225 void mesh_generic_on_off_server_set_publication_model(mesh_model_t *generic_on_off_server_model, mesh_publication_model_t * publication_model){ 226 if (generic_on_off_server_model == NULL) return; 227 if (publication_model == NULL) return; 228 publication_model->publish_state_fn = &mesh_generic_on_off_status_message; 229 generic_on_off_server_model->publication_model = publication_model; 230 } 231 232