xref: /btstack/src/mesh/mesh_generic_on_off_server.c (revision 3548b7cb21c3d9a3416b041ef5f6ae699da53c48)
139018edaSMilanka Ringwald /*
239018edaSMilanka Ringwald  * Copyright (C) 2019 BlueKitchen GmbH
339018edaSMilanka Ringwald  *
439018edaSMilanka Ringwald  * Redistribution and use in source and binary forms, with or without
539018edaSMilanka Ringwald  * modification, are permitted provided that the following conditions
639018edaSMilanka Ringwald  * are met:
739018edaSMilanka Ringwald  *
839018edaSMilanka Ringwald  * 1. Redistributions of source code must retain the above copyright
939018edaSMilanka Ringwald  *    notice, this list of conditions and the following disclaimer.
1039018edaSMilanka Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
1139018edaSMilanka Ringwald  *    notice, this list of conditions and the following disclaimer in the
1239018edaSMilanka Ringwald  *    documentation and/or other materials provided with the distribution.
1339018edaSMilanka Ringwald  * 3. Neither the name of the copyright holders nor the names of
1439018edaSMilanka Ringwald  *    contributors may be used to endorse or promote products derived
1539018edaSMilanka Ringwald  *    from this software without specific prior written permission.
1639018edaSMilanka Ringwald  * 4. Any redistribution, use, or modification is done solely for
1739018edaSMilanka Ringwald  *    personal benefit and not for any commercial purpose or for
1839018edaSMilanka Ringwald  *    monetary gain.
1939018edaSMilanka Ringwald  *
2039018edaSMilanka Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
2139018edaSMilanka Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2239018edaSMilanka Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2539018edaSMilanka Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2639018edaSMilanka Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2739018edaSMilanka Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2839018edaSMilanka Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2939018edaSMilanka Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
3039018edaSMilanka Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3139018edaSMilanka Ringwald  * SUCH DAMAGE.
3239018edaSMilanka Ringwald  *
3339018edaSMilanka Ringwald  * Please inquire about commercial licensing options at
3439018edaSMilanka Ringwald  * [email protected]
3539018edaSMilanka Ringwald  *
3639018edaSMilanka Ringwald  */
3739018edaSMilanka Ringwald 
382d4000d1SMatthias Ringwald #define BTSTACK_FILE__ "mesh_generic_on_off_server.c"
3939018edaSMilanka Ringwald 
4039018edaSMilanka Ringwald #include "mesh/mesh_generic_on_off_server.h"
4139018edaSMilanka Ringwald 
4239018edaSMilanka Ringwald #include <string.h>
4339018edaSMilanka Ringwald #include <stdio.h>
4439018edaSMilanka Ringwald 
4539018edaSMilanka Ringwald #include "bluetooth_company_id.h"
4639018edaSMilanka Ringwald #include "btstack_debug.h"
4739018edaSMilanka Ringwald #include "btstack_memory.h"
4839018edaSMilanka Ringwald #include "btstack_util.h"
4939018edaSMilanka Ringwald 
5039018edaSMilanka Ringwald #include "mesh/mesh_access.h"
5139018edaSMilanka Ringwald #include "mesh/mesh_foundation.h"
5239018edaSMilanka Ringwald #include "mesh/mesh_generic_model.h"
5339018edaSMilanka Ringwald #include "mesh/mesh_keys.h"
5439018edaSMilanka Ringwald #include "mesh/mesh_network.h"
5539018edaSMilanka Ringwald #include "mesh/mesh_upper_transport.h"
5639018edaSMilanka Ringwald 
generic_server_send_message(uint16_t src,uint16_t dest,uint16_t netkey_index,uint16_t appkey_index,mesh_pdu_t * pdu)5739018edaSMilanka Ringwald static void generic_server_send_message(uint16_t src, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, mesh_pdu_t *pdu){
5839018edaSMilanka Ringwald     uint8_t  ttl  = mesh_foundation_default_ttl_get();
5939018edaSMilanka Ringwald     mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0);
6039018edaSMilanka Ringwald     mesh_access_send_unacknowledged_pdu(pdu);
6139018edaSMilanka Ringwald }
6239018edaSMilanka Ringwald 
6339018edaSMilanka Ringwald // Transaction management
6439018edaSMilanka Ringwald 
generic_on_off_server_get_base_transition(mesh_model_t * mesh_model)6539018edaSMilanka Ringwald static mesh_transition_t * generic_on_off_server_get_base_transition(mesh_model_t * mesh_model) {
6639018edaSMilanka Ringwald     mesh_generic_on_off_state_t * generic_on_off_server_state = (mesh_generic_on_off_state_t *)mesh_model->model_data;
6739018edaSMilanka Ringwald     return &generic_on_off_server_state->transition_data.base_transition;
6839018edaSMilanka Ringwald }
6939018edaSMilanka Ringwald 
mesh_server_transition_handler(mesh_transition_t * base_transition,model_state_update_reason_t event)70332b5256SMatthias Ringwald static void mesh_server_transition_handler(mesh_transition_t * base_transition, model_state_update_reason_t event){
71332b5256SMatthias Ringwald     mesh_transition_bool_t * transition = (mesh_transition_bool_t*) base_transition;
72332b5256SMatthias Ringwald     switch (event){
73332b5256SMatthias Ringwald         case MODEL_STATE_UPDATE_REASON_TRANSITION_START:
74332b5256SMatthias Ringwald             if (transition->target_value == 1){
75332b5256SMatthias Ringwald                 transition->current_value = 1;
76332b5256SMatthias Ringwald                 mesh_access_state_changed(transition->base_transition.mesh_model);
77332b5256SMatthias Ringwald             }
78332b5256SMatthias Ringwald             break;
79332b5256SMatthias Ringwald         case MODEL_STATE_UPDATE_REASON_SET:
80332b5256SMatthias Ringwald         case MODEL_STATE_UPDATE_REASON_TRANSITION_END:
81332b5256SMatthias Ringwald             transition->current_value = transition->target_value;
82332b5256SMatthias Ringwald             mesh_access_state_changed(transition->base_transition.mesh_model);
83332b5256SMatthias Ringwald             break;
84332b5256SMatthias Ringwald         default:
85332b5256SMatthias Ringwald             break;
86332b5256SMatthias Ringwald     }
87332b5256SMatthias Ringwald 
88332b5256SMatthias Ringwald     // notify app
89e7dae087SMatthias Ringwald     mesh_model_t * generic_on_off_server_model = transition->base_transition.mesh_model;
90e7dae087SMatthias Ringwald     mesh_access_emit_state_update_bool(generic_on_off_server_model->model_packet_handler,
91e7dae087SMatthias Ringwald         mesh_access_get_element_index(generic_on_off_server_model),
92e7dae087SMatthias Ringwald         generic_on_off_server_model->model_identifier,
93e7dae087SMatthias Ringwald         MODEL_STATE_ID_GENERIC_ON_OFF,
94332b5256SMatthias Ringwald         event,
95e7dae087SMatthias Ringwald         transition->current_value);
9639018edaSMilanka Ringwald }
9739018edaSMilanka Ringwald 
mesh_server_transition_setup_transition_or_instantaneous_update(mesh_model_t * mesh_model,uint8_t transition_time_gdtt,uint8_t delay_time_gdtt)98262d85eeSMatthias Ringwald 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){
9939018edaSMilanka Ringwald     mesh_generic_on_off_state_t * generic_on_off_server_state = (mesh_generic_on_off_state_t *)mesh_model->model_data;
100618a5fc3SMatthias Ringwald     mesh_transition_t * transition = &generic_on_off_server_state->transition_data.base_transition;
101332b5256SMatthias Ringwald     mesh_access_transition_setup(mesh_model, transition, transition_time_gdtt, delay_time_gdtt, &mesh_server_transition_handler);
10239018edaSMilanka Ringwald }
10339018edaSMilanka Ringwald 
10439018edaSMilanka Ringwald // Generic On Off State
10539018edaSMilanka Ringwald 
mesh_generic_on_off_server_register_packet_handler(mesh_model_t * generic_on_off_server_model,btstack_packet_handler_t transition_events_packet_handler)10639018edaSMilanka Ringwald 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){
107bcbd4955SMatthias Ringwald     btstack_assert(generic_on_off_server_model != NULL);
108bcbd4955SMatthias Ringwald     btstack_assert(transition_events_packet_handler != NULL);
10939018edaSMilanka Ringwald     generic_on_off_server_model->model_packet_handler = transition_events_packet_handler;
11039018edaSMilanka Ringwald }
11139018edaSMilanka Ringwald 
11239018edaSMilanka Ringwald const mesh_access_message_t mesh_generic_on_off_status_transition = {
113f715e571SMatthias Ringwald         MESH_GENERIC_ON_OFF_STATUS, "111"
11439018edaSMilanka Ringwald };
11539018edaSMilanka Ringwald 
11639018edaSMilanka Ringwald const mesh_access_message_t mesh_generic_on_off_status_instantaneous = {
117f715e571SMatthias Ringwald         MESH_GENERIC_ON_OFF_STATUS, "1"
11839018edaSMilanka Ringwald };
11939018edaSMilanka Ringwald 
mesh_generic_on_off_status_message(mesh_model_t * generic_on_off_server_model)12039018edaSMilanka Ringwald static mesh_pdu_t * mesh_generic_on_off_status_message(mesh_model_t *generic_on_off_server_model){
121bcbd4955SMatthias Ringwald     btstack_assert(generic_on_off_server_model->model_data != NULL);
12239018edaSMilanka Ringwald 
12339018edaSMilanka Ringwald     mesh_generic_on_off_state_t * state = (mesh_generic_on_off_state_t *) generic_on_off_server_model->model_data;
12439018edaSMilanka Ringwald     // setup message
125039cbf1dSMatthias Ringwald     mesh_upper_transport_pdu_t * transport_pdu = NULL;
126332b5256SMatthias Ringwald     if (state->transition_data.base_transition.num_steps > 0) {
127332b5256SMatthias Ringwald         uint8_t remaining_time = (((uint8_t)state->transition_data.base_transition.step_resolution) << 6) | (state->transition_data.base_transition.num_steps);
1280323b889SMatthias Ringwald         transport_pdu = mesh_access_setup_message(&mesh_generic_on_off_status_transition, state->transition_data.current_value,
129a20c23d6SMatthias Ringwald             state->transition_data.target_value, remaining_time);
13039018edaSMilanka Ringwald     } else {
131e7dae087SMatthias Ringwald         log_info("On/Off Status: value %u, no transition active", state->transition_data.current_value);
1320323b889SMatthias Ringwald         transport_pdu = mesh_access_setup_message(&mesh_generic_on_off_status_instantaneous, state->transition_data.current_value);
13339018edaSMilanka Ringwald     }
13439018edaSMilanka Ringwald     return (mesh_pdu_t *) transport_pdu;
13539018edaSMilanka Ringwald }
13639018edaSMilanka Ringwald 
generic_on_off_get_handler(mesh_model_t * generic_on_off_server_model,mesh_pdu_t * pdu)13739018edaSMilanka Ringwald static void generic_on_off_get_handler(mesh_model_t *generic_on_off_server_model, mesh_pdu_t * pdu){
138039cbf1dSMatthias Ringwald     mesh_upper_transport_pdu_t * transport_pdu = (mesh_upper_transport_pdu_t *) mesh_generic_on_off_status_message(generic_on_off_server_model);
13939018edaSMilanka Ringwald     if (!transport_pdu) return;
14039018edaSMilanka Ringwald     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);
14139018edaSMilanka Ringwald     mesh_access_message_processed(pdu);
14239018edaSMilanka Ringwald }
14339018edaSMilanka Ringwald 
1446445d71aSMatthias Ringwald // returns if set message was valid
generic_on_off_handle_set_message(mesh_model_t * mesh_model,mesh_pdu_t * pdu)1456445d71aSMatthias Ringwald static bool generic_on_off_handle_set_message(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
146bcbd4955SMatthias Ringwald     btstack_assert(mesh_model != NULL);
147bcbd4955SMatthias Ringwald     btstack_assert(mesh_model->model_data != NULL);
14839018edaSMilanka Ringwald 
14939018edaSMilanka Ringwald     mesh_access_parser_state_t parser;
15039018edaSMilanka Ringwald     mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
151ad3a646cSMatthias Ringwald     uint8_t on_off_value = mesh_access_parser_get_uint8(&parser);
15239018edaSMilanka Ringwald 
1536445d71aSMatthias Ringwald     // check for valid value
1546445d71aSMatthias Ringwald     if (on_off_value > 1) return false;
1556445d71aSMatthias Ringwald 
15639018edaSMilanka Ringwald     // The TID field is a transaction identifier indicating whether the message is
15739018edaSMilanka Ringwald     // a new message or a retransmission of a previously sent message
158bcbd4955SMatthias Ringwald     mesh_generic_on_off_state_t * generic_on_off_server_state = (mesh_generic_on_off_state_t *)mesh_model->model_data;
159ad3a646cSMatthias Ringwald     uint8_t tid = mesh_access_parser_get_uint8(&parser);
16039018edaSMilanka Ringwald     uint8_t transition_time_gdtt = 0;
16139018edaSMilanka Ringwald     uint8_t delay_time_gdtt = 0;
16239018edaSMilanka Ringwald 
16339018edaSMilanka Ringwald     mesh_transition_t * base_transition = generic_on_off_server_get_base_transition(mesh_model);
16439018edaSMilanka Ringwald     switch (mesh_access_transitions_transaction_status(base_transition, tid, mesh_pdu_src(pdu), mesh_pdu_dst(pdu))){
16539018edaSMilanka Ringwald         case MESH_TRANSACTION_STATUS_RETRANSMISSION:
16639018edaSMilanka Ringwald             // ignore on retransmission
16739018edaSMilanka Ringwald             break;
16839018edaSMilanka Ringwald         default:
169332b5256SMatthias Ringwald             mesh_access_transitions_init_transaction(base_transition, tid, mesh_pdu_src(pdu), mesh_pdu_dst(pdu));
17039018edaSMilanka Ringwald             generic_on_off_server_state->transition_data.target_value = on_off_value;
17139018edaSMilanka Ringwald 
17239018edaSMilanka Ringwald             if (mesh_access_parser_available(&parser) == 2){
17339018edaSMilanka Ringwald                 //  Generic Default Transition Time format - num_steps (higher 6 bits), step_resolution (lower 2 bits)
174ad3a646cSMatthias Ringwald                 transition_time_gdtt = mesh_access_parser_get_uint8(&parser);
175ad3a646cSMatthias Ringwald                 delay_time_gdtt = mesh_access_parser_get_uint8(&parser);
17639018edaSMilanka Ringwald             }
177262d85eeSMatthias Ringwald             mesh_server_transition_setup_transition_or_instantaneous_update(mesh_model, transition_time_gdtt, delay_time_gdtt);
17839018edaSMilanka Ringwald             mesh_access_state_changed(mesh_model);
17939018edaSMilanka Ringwald             break;
18039018edaSMilanka Ringwald     }
1816445d71aSMatthias Ringwald     return true;
18239018edaSMilanka Ringwald }
18339018edaSMilanka Ringwald 
generic_on_off_set_handler(mesh_model_t * generic_on_off_server_model,mesh_pdu_t * pdu)18439018edaSMilanka Ringwald static void generic_on_off_set_handler(mesh_model_t *generic_on_off_server_model, mesh_pdu_t * pdu){
1856445d71aSMatthias Ringwald     bool send_status = generic_on_off_handle_set_message(generic_on_off_server_model, pdu);
1866445d71aSMatthias Ringwald     if (send_status){
187039cbf1dSMatthias Ringwald         mesh_upper_transport_pdu_t * transport_pdu = (mesh_upper_transport_pdu_t *) mesh_generic_on_off_status_message(generic_on_off_server_model);
1886445d71aSMatthias Ringwald         if (transport_pdu) {
18939018edaSMilanka Ringwald             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);
1906445d71aSMatthias Ringwald         }
1916445d71aSMatthias Ringwald     }
19239018edaSMilanka Ringwald     mesh_access_message_processed(pdu);
19339018edaSMilanka Ringwald }
19439018edaSMilanka Ringwald 
generic_on_off_set_unacknowledged_handler(mesh_model_t * generic_on_off_server_model,mesh_pdu_t * pdu)19539018edaSMilanka Ringwald static void generic_on_off_set_unacknowledged_handler(mesh_model_t *generic_on_off_server_model, mesh_pdu_t * pdu){
19639018edaSMilanka Ringwald     generic_on_off_handle_set_message(generic_on_off_server_model, pdu);
197bcbd4955SMatthias Ringwald     mesh_access_message_processed(pdu);
19839018edaSMilanka Ringwald }
19939018edaSMilanka Ringwald 
20039018edaSMilanka Ringwald // Generic On Off Message
201*3548b7cbSDirk Helbig static const mesh_operation_t mesh_generic_on_off_model_operations[] = {
20239018edaSMilanka Ringwald     { MESH_GENERIC_ON_OFF_GET,                                   0, generic_on_off_get_handler },
20339018edaSMilanka Ringwald     { MESH_GENERIC_ON_OFF_SET,                                   2, generic_on_off_set_handler },
20439018edaSMilanka Ringwald     { MESH_GENERIC_ON_OFF_SET_UNACKNOWLEDGED,                    2, generic_on_off_set_unacknowledged_handler },
20539018edaSMilanka Ringwald     { 0, 0, NULL }
20639018edaSMilanka Ringwald };
20739018edaSMilanka Ringwald 
mesh_generic_on_off_server_get_operations(void)20839018edaSMilanka Ringwald const mesh_operation_t * mesh_generic_on_off_server_get_operations(void){
20939018edaSMilanka Ringwald     return mesh_generic_on_off_model_operations;
21039018edaSMilanka Ringwald }
21139018edaSMilanka Ringwald 
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)21239018edaSMilanka Ringwald 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){
21339018edaSMilanka Ringwald     mesh_generic_on_off_state_t * generic_on_off_server_state = (mesh_generic_on_off_state_t *)mesh_model->model_data;
21439018edaSMilanka Ringwald     generic_on_off_server_state->transition_data.target_value = on_off_value;
21539018edaSMilanka Ringwald 
216262d85eeSMatthias Ringwald     mesh_server_transition_setup_transition_or_instantaneous_update(mesh_model, transition_time_gdtt, delay_time_gdtt);
21739018edaSMilanka Ringwald     mesh_access_state_changed(mesh_model);
21839018edaSMilanka Ringwald }
21939018edaSMilanka Ringwald 
mesh_generic_on_off_server_get(mesh_model_t * generic_on_off_server_model)22039018edaSMilanka Ringwald uint8_t mesh_generic_on_off_server_get(mesh_model_t *generic_on_off_server_model){
22139018edaSMilanka Ringwald     mesh_generic_on_off_state_t * generic_on_off_server_state = (mesh_generic_on_off_state_t *)generic_on_off_server_model->model_data;
22239018edaSMilanka Ringwald     return generic_on_off_server_state->transition_data.current_value;
22339018edaSMilanka Ringwald }
22439018edaSMilanka Ringwald 
mesh_generic_on_off_server_set_publication_model(mesh_model_t * generic_on_off_server_model,mesh_publication_model_t * publication_model)22539018edaSMilanka Ringwald void mesh_generic_on_off_server_set_publication_model(mesh_model_t *generic_on_off_server_model, mesh_publication_model_t * publication_model){
22639018edaSMilanka Ringwald     if (generic_on_off_server_model == NULL) return;
22739018edaSMilanka Ringwald     if (publication_model == NULL) return;
22839018edaSMilanka Ringwald     publication_model->publish_state_fn = &mesh_generic_on_off_status_message;
22939018edaSMilanka Ringwald     generic_on_off_server_model->publication_model = publication_model;
23039018edaSMilanka Ringwald }
23139018edaSMilanka Ringwald 
232