mesh: health_fault_test_handler and MESH_SUBEVENT_HEALTH_PERFORM_TEST event

This commit is contained in:
Milanka Ringwald 2019-07-18 14:47:46 +02:00 committed by Matthias Ringwald
parent 9a5e0954e7
commit 859ea3194d
4 changed files with 215 additions and 23 deletions

View File

@ -2555,4 +2555,28 @@ typedef uint8_t sm_key_t[16];
*/
#define MESH_SUBEVENT_GENERIC_LEVEL_STATUS 0x32
/**
* @format 112
* @param subevent_code
* @param element_index
* @param company_id
*/
#define MESH_SUBEVENT_HEALTH_CLEAR_REGISTERED_FAULTS 0x33
/**
* @format 11422221
* @param subevent_code
* @param element_index
* @param model_identifier
* @param dest
* @param netkey_index
* @param appkey_index
* @param company_id
* @param test_id
*/
#define MESH_SUBEVENT_HEALTH_PERFORM_TEST 0x34
#endif

View File

@ -7784,6 +7784,89 @@ static inline uint32_t mesh_subevent_generic_level_status_get_remaining_time_ms(
return little_endian_read_32(event, 12);
}
/**
* @brief Get field element_index from event MESH_SUBEVENT_HEALTH_CLEAR_REGISTERED_FAULTS
* @param event packet
* @return element_index
* @note: btstack_type 1
*/
static inline uint8_t mesh_subevent_health_clear_registered_faults_get_element_index(const uint8_t * event){
return event[3];
}
/**
* @brief Get field company_id from event MESH_SUBEVENT_HEALTH_CLEAR_REGISTERED_FAULTS
* @param event packet
* @return company_id
* @note: btstack_type 2
*/
static inline uint16_t mesh_subevent_health_clear_registered_faults_get_company_id(const uint8_t * event){
return little_endian_read_16(event, 4);
}
/**
* @brief Get field element_index from event MESH_SUBEVENT_HEALTH_PERFORM_TEST
* @param event packet
* @return element_index
* @note: btstack_type 1
*/
static inline uint8_t mesh_subevent_health_perform_test_get_element_index(const uint8_t * event){
return event[3];
}
/**
* @brief Get field model_identifier from event MESH_SUBEVENT_HEALTH_PERFORM_TEST
* @param event packet
* @return model_identifier
* @note: btstack_type 4
*/
static inline uint32_t mesh_subevent_health_perform_test_get_model_identifier(const uint8_t * event){
return little_endian_read_32(event, 4);
}
/**
* @brief Get field dest from event MESH_SUBEVENT_HEALTH_PERFORM_TEST
* @param event packet
* @return dest
* @note: btstack_type 2
*/
static inline uint16_t mesh_subevent_health_perform_test_get_dest(const uint8_t * event){
return little_endian_read_16(event, 8);
}
/**
* @brief Get field netkey_index from event MESH_SUBEVENT_HEALTH_PERFORM_TEST
* @param event packet
* @return netkey_index
* @note: btstack_type 2
*/
static inline uint16_t mesh_subevent_health_perform_test_get_netkey_index(const uint8_t * event){
return little_endian_read_16(event, 10);
}
/**
* @brief Get field appkey_index from event MESH_SUBEVENT_HEALTH_PERFORM_TEST
* @param event packet
* @return appkey_index
* @note: btstack_type 2
*/
static inline uint16_t mesh_subevent_health_perform_test_get_appkey_index(const uint8_t * event){
return little_endian_read_16(event, 12);
}
/**
* @brief Get field company_id from event MESH_SUBEVENT_HEALTH_PERFORM_TEST
* @param event packet
* @return company_id
* @note: btstack_type 2
*/
static inline uint16_t mesh_subevent_health_perform_test_get_company_id(const uint8_t * event){
return little_endian_read_16(event, 14);
}
/**
* @brief Get field test_id from event MESH_SUBEVENT_HEALTH_PERFORM_TEST
* @param event packet
* @return test_id
* @note: btstack_type 1
*/
static inline uint8_t mesh_subevent_health_perform_test_get_test_id(const uint8_t * event){
return event[16];
}
/* API_END */

View File

@ -55,10 +55,10 @@
#include "mesh/mesh_network.h"
#include "mesh/mesh_upper_transport.h"
static void health_server_send_message(uint16_t src, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, mesh_pdu_t *pdu, uint32_t ack_opcode){
static void health_server_send_message(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_access_send_acknowledged_pdu(pdu, mesh_access_acknowledged_message_retransmissions(), ack_opcode);
mesh_access_send_unacknowledged_pdu(pdu);
}
// Health State
const mesh_access_message_t mesh_foundation_health_period_status = {
@ -69,21 +69,12 @@ const mesh_access_message_t mesh_foundation_health_attention_status = {
MESH_FOUNDATION_OPERATION_ATTENTION_STATUS, "1"
};
static mesh_pdu_t * health_status_regitered_faults(mesh_model_t * mesh_model, uint32_t opcode, uint16_t company_id){
if (mesh_model->element == NULL){
log_error("mesh_model->element == NULL");
}
mesh_health_state_t * state = (mesh_health_state_t *) mesh_model->model_data;
if (state == NULL){
log_error("mesh_health_state == NULL");
}
static mesh_pdu_t * health_current_status(btstack_linked_list_t * faults, uint32_t opcode, uint16_t company_id){
mesh_transport_pdu_t * transport_pdu = mesh_access_transport_init(opcode);
if (!transport_pdu) return NULL;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &state->registered_faults);
btstack_linked_list_iterator_init(&it, faults);
while (btstack_linked_list_iterator_has_next(&it)){
mesh_fault_t * fault = (mesh_fault_t *) btstack_linked_list_iterator_next(&it);
if (fault->company_id != company_id) continue;
@ -106,9 +97,10 @@ static void health_fault_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu)
mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
uint16_t company_id = mesh_access_parser_get_u16(&parser);
mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) health_status_regitered_faults(mesh_model, MESH_FOUNDATION_OPERATION_HEALTH_FAULT_STATUS, company_id);
mesh_health_state_t * state = (mesh_health_state_t *) mesh_model->model_data;
mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) health_current_status(&state->registered_faults, MESH_FOUNDATION_OPERATION_HEALTH_FAULT_STATUS, company_id);
if (!transport_pdu) return;
health_server_send_message(mesh_access_get_element_address(mesh_model), mesh_pdu_src(pdu), mesh_pdu_netkey_index(pdu), mesh_pdu_appkey_index(pdu),(mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_HEALTH_FAULT_STATUS);
health_server_send_message(mesh_access_get_element_address(mesh_model), mesh_pdu_src(pdu), mesh_pdu_netkey_index(pdu), mesh_pdu_appkey_index(pdu),(mesh_pdu_t *) transport_pdu);
mesh_access_message_processed(pdu);
}
@ -129,14 +121,11 @@ static void health_fault_clear_handler(mesh_model_t *mesh_model, mesh_pdu_t * pd
uint16_t company_id = mesh_access_parser_get_u16(&parser);
mesh_health_state_t * state = (mesh_health_state_t *) mesh_model->model_data;
if (state == NULL){
log_error("mesh_health_state == NULL");
}
clear_faults(&state->registered_faults, company_id);
mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) health_status_regitered_faults(mesh_model, MESH_FOUNDATION_OPERATION_HEALTH_FAULT_STATUS, company_id);
if (!transport_pdu) return;
health_server_send_message(mesh_access_get_element_address(mesh_model), mesh_pdu_src(pdu), mesh_pdu_netkey_index(pdu), mesh_pdu_appkey_index(pdu),(mesh_pdu_t *) transport_pdu, MESH_FOUNDATION_OPERATION_HEALTH_FAULT_STATUS);
clear_faults(&state->registered_faults, company_id);
mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) health_current_status(&state->registered_faults, MESH_FOUNDATION_OPERATION_HEALTH_FAULT_STATUS, company_id);
if (!transport_pdu) return;
health_server_send_message(mesh_access_get_element_address(mesh_model), mesh_pdu_src(pdu), mesh_pdu_netkey_index(pdu), mesh_pdu_appkey_index(pdu),(mesh_pdu_t *) transport_pdu);
mesh_access_message_processed(pdu);
}
@ -153,12 +142,88 @@ static void health_fault_clear_unacknowledged_handler(mesh_model_t *mesh_model,
mesh_access_message_processed(pdu);
}
static void health_fault_test_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){
mesh_access_parser_state_t parser;
mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu);
uint8_t test_id = mesh_access_parser_get_u8(&parser);
uint16_t company_id = mesh_access_parser_get_u16(&parser);
mesh_health_state_t * state = (mesh_health_state_t *) mesh_model->model_data;
if (state == NULL){
log_error("mesh_health_state == NULL");
}
uint8_t event[17 ];
int pos = 0;
event[pos++] = HCI_EVENT_MESH_META;
// reserve for size
pos++;
event[pos++] = MESH_SUBEVENT_HEALTH_PERFORM_TEST;
// element index
event[pos++] = mesh_model->element->element_index;
// model_id
little_endian_store_32(event, pos, mesh_model->model_identifier);
pos += 4;
little_endian_store_16(event, pos, mesh_pdu_src(pdu));
pos += 2;
little_endian_store_16(event, pos, mesh_pdu_netkey_index(pdu));
pos += 2;
little_endian_store_16(event, pos, mesh_pdu_appkey_index(pdu));
pos += 2;
little_endian_store_16(event, pos, company_id);
pos += 2;
event[pos++] = test_id;
event[1] = pos - 2;
(*mesh_model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
mesh_access_message_processed(pdu);
}
static mesh_pdu_t * health_fault_status(btstack_linked_list_t * faults, uint32_t opcode, uint8_t test_id, uint16_t company_id){
mesh_transport_pdu_t * transport_pdu = mesh_access_transport_init(opcode);
if (!transport_pdu) return NULL;
mesh_access_transport_add_uint8(transport_pdu, test_id);
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, faults);
while (btstack_linked_list_iterator_has_next(&it)){
mesh_fault_t * fault = (mesh_fault_t *) btstack_linked_list_iterator_next(&it);
if (fault->company_id != company_id) continue;
mesh_access_transport_add_uint8(transport_pdu, fault->test_id);
mesh_access_transport_add_uint16(transport_pdu, fault->company_id);
int i;
for (i = 0; i < fault->num_faults; i++){
mesh_access_transport_add_uint8(transport_pdu, fault->faults[i]);
}
return (mesh_pdu_t *) transport_pdu;
}
// no company with company_id found
mesh_access_transport_add_uint8(transport_pdu, 0);
mesh_access_transport_add_uint16(transport_pdu, company_id);
return (mesh_pdu_t *) transport_pdu;
}
void mesh_health_server_report_test_done(uint16_t element_index, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t test_id, uint16_t company_id){
mesh_element_t * element = mesh_element_for_index(element_index);
if (element == NULL) return;
mesh_model_t * mesh_model = mesh_model_get_by_identifier(element, mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_HEALTH_SERVER));
if (mesh_model == NULL) return;
mesh_health_state_t * state = (mesh_health_state_t *) mesh_model->model_data;
mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) health_fault_status(&state->registered_faults, MESH_FOUNDATION_OPERATION_HEALTH_FAULT_STATUS, test_id, company_id);
if (!transport_pdu) return;
health_server_send_message(element_index, dest, netkey_index, appkey_index, (mesh_pdu_t *) transport_pdu);
}
// Health Message
const static mesh_operation_t mesh_health_model_operations[] = {
{ MESH_FOUNDATION_OPERATION_HEALTH_FAULT_GET, 2, health_fault_get_handler },
{ MESH_FOUNDATION_OPERATION_HEALTH_FAULT_CLEAR, 2, health_fault_clear_handler },
{ MESH_FOUNDATION_OPERATION_HEALTH_FAULT_CLEAR_UNACKNOWLEDGED, 2, health_fault_clear_unacknowledged_handler },
// { MESH_FOUNDATION_OPERATION_HEALTH_FAULT_TEST, 3, health_fault_test_handler },
{ MESH_FOUNDATION_OPERATION_HEALTH_FAULT_TEST, 3, health_fault_test_handler },
// { MESH_FOUNDATION_OPERATION_HEALTH_FAULT_TEST_UNACKNOWLEDGED, 3, health_fault_test_unacknowledged_handler },
// { MESH_FOUNDATION_OPERATION_HEALTH_FAULT_STATUS_UNACKNOWLEDGED, 3, health_fault_test_status_unacknowledged_handler },
// { MESH_FOUNDATION_OPERATION_HEALTH_PERIOD_GET, 2, health_period_get_handler },
@ -174,3 +239,14 @@ const mesh_operation_t * mesh_health_server_get_operations(void){
return mesh_health_model_operations;
}
void mesh_health_server_register_packet_handler(mesh_model_t *mesh_model, btstack_packet_handler_t events_packet_handler){
if (events_packet_handler == NULL){
log_error(" mesh_health_server_register_packet_handler called with NULL callback");
return;
}
if (mesh_model == NULL){
log_error(" mesh_health_server_register_packet_handler called with NULL mesh_model");
return;
}
mesh_model->model_packet_handler = &events_packet_handler;
}

View File

@ -49,6 +49,15 @@ extern "C"
const mesh_operation_t * mesh_health_server_get_operations(void);
/**
* @brief Register packet handler
* @param generic_level_client_model
* @param events_packet_handler
*/
void mesh_health_server_register_packet_handler(mesh_model_t *mesh_model, btstack_packet_handler_t events_packet_handler);
void mesh_health_server_report_test_done(uint16_t element_index, uint16_t dest, uint16_t netkey_index, uint16_t appkey_index, uint8_t test_id, uint16_t company_id);
#ifdef __cplusplus
} /* end of extern "C" */
#endif