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_client.c" 39 40 #include "mesh/mesh_generic_on_off_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 On Off Message 58 59 static const mesh_access_message_t mesh_generic_on_off_set_with_transition = { 60 MESH_GENERIC_ON_OFF_SET, "1111" 61 }; 62 63 static const mesh_access_message_t mesh_generic_on_off_set_instantaneous = { 64 MESH_GENERIC_ON_OFF_SET, "11" 65 }; 66 67 static const mesh_access_message_t mesh_generic_on_off_set_unacknowledged_with_transition = { 68 MESH_GENERIC_ON_OFF_SET_UNACKNOWLEDGED, "1111" 69 }; 70 71 static const mesh_access_message_t mesh_generic_on_off_set_unacknowledged_instantaneous = { 72 MESH_GENERIC_ON_OFF_SET_UNACKNOWLEDGED, "11" 73 }; 74 75 static const mesh_access_message_t mesh_generic_on_off_get = { 76 MESH_GENERIC_ON_OFF_GET, "" 77 }; 78 79 80 // Generic On Off Client Functions 81 82 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){ 83 uint8_t ttl = mesh_foundation_default_ttl_get(); 84 mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0); 85 mesh_access_send_unacknowledged_pdu(pdu); 86 } 87 88 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){ 89 uint8_t ttl = mesh_foundation_default_ttl_get(); 90 mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0); 91 mesh_access_send_acknowledged_pdu(pdu, mesh_access_acknowledged_message_retransmissions(), ack_opcode); 92 } 93 94 uint8_t mesh_generic_on_off_client_get(mesh_model_t *mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){ 95 // setup message 96 mesh_upper_transport_pdu_t * transport_pdu = mesh_access_setup_message(&mesh_generic_on_off_get); 97 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 98 // send as segmented access pdu 99 generic_client_send_message_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_GENERIC_ON_OFF_STATUS); 100 return ERROR_CODE_SUCCESS; 101 } 102 103 uint8_t mesh_generic_on_off_client_set(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, 104 uint8_t on_off_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id){ 105 106 mesh_upper_transport_pdu_t * transport_pdu; 107 if (transition_time_gdtt != 0) { 108 transport_pdu = mesh_access_setup_message(&mesh_generic_on_off_set_with_transition, on_off_value, transaction_id, transition_time_gdtt, delay_time_gdtt); 109 } else { 110 transport_pdu = mesh_access_setup_message(&mesh_generic_on_off_set_instantaneous, on_off_value, transaction_id); 111 } 112 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 113 114 generic_client_send_message_acknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu, MESH_GENERIC_ON_OFF_STATUS); 115 return ERROR_CODE_SUCCESS; 116 } 117 118 uint8_t mesh_generic_on_off_client_set_unacknowledged(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, 119 uint8_t on_off_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id){ 120 mesh_upper_transport_pdu_t * transport_pdu; 121 if (transition_time_gdtt != 0) { 122 transport_pdu = mesh_access_setup_message(&mesh_generic_on_off_set_unacknowledged_with_transition, on_off_value, transaction_id, transition_time_gdtt, delay_time_gdtt); 123 } else { 124 transport_pdu = mesh_access_setup_message(&mesh_generic_on_off_set_unacknowledged_instantaneous, on_off_value, transaction_id); 125 } 126 if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; 127 generic_client_send_message_unacknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu); 128 return ERROR_CODE_SUCCESS; 129 } 130 131 uint8_t mesh_generic_on_off_client_publish(mesh_model_t * mesh_model, uint8_t on_off_value, uint8_t transaction_id){ 132 mesh_publication_model_t * publication_model = mesh_model->publication_model; 133 uint16_t appkey_index = publication_model->appkey_index; 134 mesh_transport_key_t * app_key = mesh_transport_key_get(appkey_index); 135 if (app_key == NULL) return MESH_ERROR_APPKEY_INDEX_INVALID; 136 return mesh_generic_on_off_client_set_unacknowledged(mesh_model, publication_model->address, app_key->netkey_index, appkey_index, on_off_value, 0, 0, transaction_id); 137 } 138 139 // Model Operations 140 141 static void generic_on_off_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 142 if (!mesh_model->model_packet_handler){ 143 log_error("model_packet_handler == NULL"); 144 } 145 146 mesh_access_parser_state_t parser; 147 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 148 149 uint8_t present_value = mesh_access_parser_get_uint8(&parser); 150 uint8_t target_value = 0; 151 uint8_t remaining_time_gdtt = 0; 152 153 if (mesh_access_parser_available(&parser) == 2){ 154 target_value = mesh_access_parser_get_uint8(&parser); 155 remaining_time_gdtt = mesh_access_parser_get_uint8(&parser); 156 } 157 158 uint8_t event[12] = {HCI_EVENT_MESH_META, 10, MESH_SUBEVENT_GENERIC_ON_OFF}; 159 int pos = 3; 160 // dest 161 little_endian_store_16(event, pos, mesh_pdu_src(pdu)); 162 pos += 2; 163 event[pos++] = ERROR_CODE_SUCCESS; 164 165 event[pos++] = present_value; 166 event[pos++] = target_value; 167 little_endian_store_32(event, pos, (uint32_t) mesh_access_time_gdtt2ms(remaining_time_gdtt)); 168 pos += 4; 169 170 (*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 171 mesh_access_message_processed(pdu); 172 } 173 174 static const mesh_operation_t mesh_generic_on_off_model_operations[] = { 175 { MESH_GENERIC_ON_OFF_STATUS, 0, generic_on_off_status_handler }, 176 { 0, 0, NULL } 177 }; 178 179 const mesh_operation_t * mesh_generic_on_off_client_get_operations(void){ 180 return mesh_generic_on_off_model_operations; 181 } 182 183 void mesh_generic_on_off_client_register_packet_handler(mesh_model_t *mesh_model, btstack_packet_handler_t transition_events_packet_handler){ 184 if (transition_events_packet_handler == NULL){ 185 log_error("mesh_generic_on_off_client_register_packet_handler called with NULL callback"); 186 return; 187 } 188 if (mesh_model == NULL){ 189 log_error("mesh_generic_on_off_client_register_packet_handler called with NULL mesh_model"); 190 return; 191 } 192 mesh_model->model_packet_handler = transition_events_packet_handler; 193 } 194