From 9a6a784cda4f129629425f5043c84afbbb939ec5 Mon Sep 17 00:00:00 2001 From: Milanka Ringwald Date: Tue, 9 Jul 2019 11:31:33 +0200 Subject: [PATCH] mesh: use mesh_access_send_[un]acknowledged_pdu --- test/mesh/mesh_generic_client.c | 183 +++++++++++++++----------------- 1 file changed, 85 insertions(+), 98 deletions(-) diff --git a/test/mesh/mesh_generic_client.c b/test/mesh/mesh_generic_client.c index 1cd529fee..de1cb9f5a 100644 --- a/test/mesh/mesh_generic_client.c +++ b/test/mesh/mesh_generic_client.c @@ -51,25 +51,89 @@ #include "btstack_memory.h" #include "btstack_debug.h" -static void generic_client_send_message(uint16_t src, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, mesh_pdu_t *pdu){ +// Generic On Off Message + +static const mesh_access_message_t mesh_generic_on_off_set_with_transition = { + MESH_GENERIC_ON_OFF_SET, "1111" +}; + +static const mesh_access_message_t mesh_generic_on_off_set_instantaneous = { + MESH_GENERIC_ON_OFF_SET, "11" +}; + +static const mesh_access_message_t mesh_generic_on_off_set_unacknowledged_with_transition = { + MESH_GENERIC_ON_OFF_SET_UNACKNOWLEDGED, "1111" +}; + +static const mesh_access_message_t mesh_generic_on_off_set_unacknowledged_instantaneous = { + MESH_GENERIC_ON_OFF_SET_UNACKNOWLEDGED, "11" +}; + +static const mesh_access_message_t mesh_generic_on_off_get = { + MESH_GENERIC_ON_OFF_GET, "" +}; + + +// Generic On Off Client Functions + +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){ uint8_t ttl = mesh_foundation_default_ttl_get(); mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0); - mesh_upper_transport_send_access_pdu(pdu); + mesh_access_send_unacknowledged_pdu(pdu); } -void mesh_generic_on_off_client_register_packet_handler(mesh_model_t *mesh_model, btstack_packet_handler_t transition_events_packet_handler){ - if (transition_events_packet_handler == NULL){ - log_error("mesh_generic_on_off_client_register_packet_handler called with NULL callback"); - return; - } - if (mesh_model == NULL){ - log_error("mesh_generic_on_off_client_register_packet_handler called with NULL mesh_model"); - return; - } - mesh_model->model_packet_handler = &transition_events_packet_handler; +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){ + uint8_t ttl = mesh_foundation_default_ttl_get(); + mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0); + mesh_access_send_acknowledged_pdu(pdu, mesh_access_acknowledged_message_retransmissions(), ack_opcode); } -// Generic On Off State +uint8_t mesh_generic_on_off_client_get_value(mesh_model_t *mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){ + // setup message + mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_generic_on_off_get); + if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; + // send as segmented access pdu + 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); + return ERROR_CODE_SUCCESS; +} + +uint8_t mesh_generic_on_off_client_set_value(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, + uint8_t on_off_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id){ + + mesh_transport_pdu_t * transport_pdu; + if (transition_time_gdtt != 0) { + transport_pdu = mesh_access_setup_segmented_message(&mesh_generic_on_off_set_with_transition, on_off_value, transaction_id, transition_time_gdtt, delay_time_gdtt); + } else { + transport_pdu = mesh_access_setup_segmented_message(&mesh_generic_on_off_set_instantaneous, on_off_value, transaction_id); + } + if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; + + 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); + return ERROR_CODE_SUCCESS; +} + +uint8_t mesh_generic_on_off_client_set_value_unacknowledged(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, + uint8_t on_off_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id){ + mesh_transport_pdu_t * transport_pdu; + if (transition_time_gdtt != 0) { + transport_pdu = mesh_access_setup_segmented_message(&mesh_generic_on_off_set_unacknowledged_with_transition, on_off_value, transaction_id, transition_time_gdtt, delay_time_gdtt); + } else { + transport_pdu = mesh_access_setup_segmented_message(&mesh_generic_on_off_set_unacknowledged_instantaneous, on_off_value, transaction_id); + } + if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; + generic_client_send_message_unacknowledged(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu); + return ERROR_CODE_SUCCESS; +} + +uint8_t mesh_generic_on_off_client_publish_value(mesh_model_t * mesh_model, uint8_t on_off_value, uint8_t transaction_id){ + mesh_publication_model_t * publication_model = mesh_model->publication_model; + uint16_t appkey_index = publication_model->appkey_index; + mesh_transport_key_t * app_key = mesh_transport_key_get(appkey_index); + if (app_key == NULL) return MESH_ERROR_APPKEY_INDEX_INVALID; + return mesh_generic_on_off_client_set_value_unacknowledged(mesh_model, publication_model->address, app_key->netkey_index, appkey_index, on_off_value, 0, 0, transaction_id); +} + +// Model Operations static void generic_on_off_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ if (!mesh_model->model_packet_handler){ @@ -111,28 +175,6 @@ static void generic_on_off_status_handler(mesh_model_t *mesh_model, mesh_pdu_t * mesh_access_message_processed(pdu); } -// Generic On Off Message - -const mesh_access_message_t mesh_generic_on_off_set_with_transition = { - MESH_GENERIC_ON_OFF_SET, "1111" -}; - -const mesh_access_message_t mesh_generic_on_off_set_instantaneous = { - MESH_GENERIC_ON_OFF_SET, "11" -}; - -const mesh_access_message_t mesh_generic_on_off_set_unacknowledged_with_transition = { - MESH_GENERIC_ON_OFF_SET_UNACKNOWLEDGED, "1111" -}; - -const mesh_access_message_t mesh_generic_on_off_set_unacknowledged_instantaneous = { - MESH_GENERIC_ON_OFF_SET_UNACKNOWLEDGED, "11" -}; - -const mesh_access_message_t mesh_generic_on_off_get = { - MESH_GENERIC_ON_OFF_GET, "" -}; - const static mesh_operation_t mesh_generic_on_off_model_operations[] = { { MESH_GENERIC_ON_OFF_STATUS, 0, generic_on_off_status_handler }, { 0, 0, NULL } @@ -142,69 +184,14 @@ const mesh_operation_t * mesh_generic_on_off_client_get_operations(void){ return mesh_generic_on_off_model_operations; } -static uint8_t mesh_generic_on_off_client_set_with_transition_message(mesh_model_t *mesh_model, const mesh_access_message_t * message_template, - uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, - uint8_t on_off_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id){ - if (mesh_model->element == NULL){ - log_error("mesh_model->element == NULL"); +void mesh_generic_on_off_client_register_packet_handler(mesh_model_t *mesh_model, btstack_packet_handler_t transition_events_packet_handler){ + if (transition_events_packet_handler == NULL){ + log_error("mesh_generic_on_off_client_register_packet_handler called with NULL callback"); + return; } - // setup message - mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(message_template, on_off_value, transaction_id, transition_time_gdtt, delay_time_gdtt); - if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; - - // send as segmented access pdu - generic_client_send_message(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu); - return ERROR_CODE_SUCCESS; -} - -static uint8_t mesh_generic_on_off_client_set_instantaneous_message(mesh_model_t *mesh_model, const mesh_access_message_t * message_template, - uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t on_off_value, uint8_t transaction_id){ - if (mesh_model->element == NULL){ - log_error("mesh_model->element == NULL"); + if (mesh_model == NULL){ + log_error("mesh_generic_on_off_client_register_packet_handler called with NULL mesh_model"); + return; } - // setup message - mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(message_template, on_off_value, transaction_id); - if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; - - generic_client_send_message(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu); - return ERROR_CODE_SUCCESS; -} - -uint8_t mesh_generic_on_off_client_set_value(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, - uint8_t on_off_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id){ - if (transition_time_gdtt != 0) { - return mesh_generic_on_off_client_set_with_transition_message(mesh_model, &mesh_generic_on_off_set_with_transition, dest, netkey_index, appkey_index, on_off_value, transition_time_gdtt, delay_time_gdtt, transaction_id); - } else { - return mesh_generic_on_off_client_set_instantaneous_message(mesh_model, &mesh_generic_on_off_set_instantaneous, dest, netkey_index, appkey_index, on_off_value, transaction_id); - } -} - -uint8_t mesh_generic_on_off_client_set_value_unacknowledged(mesh_model_t * mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, - uint8_t on_off_value, uint8_t transition_time_gdtt, uint8_t delay_time_gdtt, uint8_t transaction_id){ - if (transition_time_gdtt != 0) { - return mesh_generic_on_off_client_set_with_transition_message(mesh_model, &mesh_generic_on_off_set_unacknowledged_with_transition, dest, netkey_index, appkey_index, on_off_value, transition_time_gdtt, delay_time_gdtt, transaction_id); - } else { - return mesh_generic_on_off_client_set_instantaneous_message(mesh_model, &mesh_generic_on_off_set_unacknowledged_instantaneous, dest, netkey_index, appkey_index, on_off_value, transaction_id); - } -} - -uint8_t mesh_generic_on_off_client_get_value(mesh_model_t *mesh_model, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index){ - if (mesh_model->element == NULL){ - log_error("mesh_model->element == NULL"); - } - // setup message - mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_generic_on_off_get); - if (!transport_pdu) return BTSTACK_MEMORY_ALLOC_FAILED; - // send as segmented access pdu - generic_client_send_message(mesh_access_get_element_address(mesh_model), dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu); - return ERROR_CODE_SUCCESS; -} - -uint8_t mesh_generic_on_off_client_publish_value(mesh_model_t * mesh_model, uint8_t on_off_value, uint8_t transaction_id){ - mesh_publication_model_t * publication_model = mesh_model->publication_model; - uint16_t appkey_index = publication_model->appkey_index; - mesh_transport_key_t * app_key = mesh_transport_key_get(appkey_index); - if (app_key == NULL) return MESH_ERROR_APPKEY_INDEX_INVALID; - - return mesh_generic_on_off_client_set_value_unacknowledged(mesh_model, publication_model->address, app_key->netkey_index, appkey_index, on_off_value, 0, 0, transaction_id); + mesh_model->model_packet_handler = &transition_events_packet_handler; }